]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/acpi/video.c
ACPI: video: call ACPI notifier chain for ACPI video notifications
[net-next-2.6.git] / drivers / acpi / video.c
1 /*
2  *  video.c - ACPI Video Driver ($Revision:$)
3  *
4  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5  *  Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
6  *  Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/mutex.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/input.h>
36 #include <linux/backlight.h>
37 #include <linux/video_output.h>
38 #include <asm/uaccess.h>
39
40 #include <acpi/acpi_bus.h>
41 #include <acpi/acpi_drivers.h>
42
43 #define ACPI_VIDEO_COMPONENT            0x08000000
44 #define ACPI_VIDEO_CLASS                "video"
45 #define ACPI_VIDEO_BUS_NAME             "Video Bus"
46 #define ACPI_VIDEO_DEVICE_NAME          "Video Device"
47 #define ACPI_VIDEO_NOTIFY_SWITCH        0x80
48 #define ACPI_VIDEO_NOTIFY_PROBE         0x81
49 #define ACPI_VIDEO_NOTIFY_CYCLE         0x82
50 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT   0x83
51 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT   0x84
52
53 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS      0x85
54 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS        0x86
55 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS        0x87
56 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS       0x88
57 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF           0x89
58
59 #define ACPI_VIDEO_HEAD_INVALID         (~0u - 1)
60 #define ACPI_VIDEO_HEAD_END             (~0u)
61 #define MAX_NAME_LEN    20
62
63 #define ACPI_VIDEO_DISPLAY_CRT  1
64 #define ACPI_VIDEO_DISPLAY_TV   2
65 #define ACPI_VIDEO_DISPLAY_DVI  3
66 #define ACPI_VIDEO_DISPLAY_LCD  4
67
68 #define _COMPONENT              ACPI_VIDEO_COMPONENT
69 ACPI_MODULE_NAME("video");
70
71 MODULE_AUTHOR("Bruno Ducrot");
72 MODULE_DESCRIPTION("ACPI Video Driver");
73 MODULE_LICENSE("GPL");
74
75 static int brightness_switch_enabled = 1;
76 module_param(brightness_switch_enabled, bool, 0644);
77
78 static int acpi_video_bus_add(struct acpi_device *device);
79 static int acpi_video_bus_remove(struct acpi_device *device, int type);
80
81 static const struct acpi_device_id video_device_ids[] = {
82         {ACPI_VIDEO_HID, 0},
83         {"", 0},
84 };
85 MODULE_DEVICE_TABLE(acpi, video_device_ids);
86
87 static struct acpi_driver acpi_video_bus = {
88         .name = "video",
89         .class = ACPI_VIDEO_CLASS,
90         .ids = video_device_ids,
91         .ops = {
92                 .add = acpi_video_bus_add,
93                 .remove = acpi_video_bus_remove,
94                 },
95 };
96
97 struct acpi_video_bus_flags {
98         u8 multihead:1;         /* can switch video heads */
99         u8 rom:1;               /* can retrieve a video rom */
100         u8 post:1;              /* can configure the head to */
101         u8 reserved:5;
102 };
103
104 struct acpi_video_bus_cap {
105         u8 _DOS:1;              /*Enable/Disable output switching */
106         u8 _DOD:1;              /*Enumerate all devices attached to display adapter */
107         u8 _ROM:1;              /*Get ROM Data */
108         u8 _GPD:1;              /*Get POST Device */
109         u8 _SPD:1;              /*Set POST Device */
110         u8 _VPO:1;              /*Video POST Options */
111         u8 reserved:2;
112 };
113
114 struct acpi_video_device_attrib {
115         u32 display_index:4;    /* A zero-based instance of the Display */
116         u32 display_port_attachment:4;  /*This field differentiates the display type */
117         u32 display_type:4;     /*Describe the specific type in use */
118         u32 vendor_specific:4;  /*Chipset Vendor Specific */
119         u32 bios_can_detect:1;  /*BIOS can detect the device */
120         u32 depend_on_vga:1;    /*Non-VGA output device whose power is related to 
121                                    the VGA device. */
122         u32 pipe_id:3;          /*For VGA multiple-head devices. */
123         u32 reserved:10;        /*Must be 0 */
124         u32 device_id_scheme:1; /*Device ID Scheme */
125 };
126
127 struct acpi_video_enumerated_device {
128         union {
129                 u32 int_val;
130                 struct acpi_video_device_attrib attrib;
131         } value;
132         struct acpi_video_device *bind_info;
133 };
134
135 struct acpi_video_bus {
136         struct acpi_device *device;
137         u8 dos_setting;
138         struct acpi_video_enumerated_device *attached_array;
139         u8 attached_count;
140         struct acpi_video_bus_cap cap;
141         struct acpi_video_bus_flags flags;
142         struct list_head video_device_list;
143         struct mutex device_list_lock;  /* protects video_device_list */
144         struct proc_dir_entry *dir;
145         struct input_dev *input;
146         char phys[32];  /* for input device */
147 };
148
149 struct acpi_video_device_flags {
150         u8 crt:1;
151         u8 lcd:1;
152         u8 tvout:1;
153         u8 dvi:1;
154         u8 bios:1;
155         u8 unknown:1;
156         u8 reserved:2;
157 };
158
159 struct acpi_video_device_cap {
160         u8 _ADR:1;              /*Return the unique ID */
161         u8 _BCL:1;              /*Query list of brightness control levels supported */
162         u8 _BCM:1;              /*Set the brightness level */
163         u8 _BQC:1;              /* Get current brightness level */
164         u8 _DDC:1;              /*Return the EDID for this device */
165         u8 _DCS:1;              /*Return status of output device */
166         u8 _DGS:1;              /*Query graphics state */
167         u8 _DSS:1;              /*Device state set */
168 };
169
170 struct acpi_video_device_brightness {
171         int curr;
172         int count;
173         int *levels;
174 };
175
176 struct acpi_video_device {
177         unsigned long device_id;
178         struct acpi_video_device_flags flags;
179         struct acpi_video_device_cap cap;
180         struct list_head entry;
181         struct acpi_video_bus *video;
182         struct acpi_device *dev;
183         struct acpi_video_device_brightness *brightness;
184         struct backlight_device *backlight;
185         struct output_device *output_dev;
186 };
187
188 /* bus */
189 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
190 static struct file_operations acpi_video_bus_info_fops = {
191         .open = acpi_video_bus_info_open_fs,
192         .read = seq_read,
193         .llseek = seq_lseek,
194         .release = single_release,
195 };
196
197 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
198 static struct file_operations acpi_video_bus_ROM_fops = {
199         .open = acpi_video_bus_ROM_open_fs,
200         .read = seq_read,
201         .llseek = seq_lseek,
202         .release = single_release,
203 };
204
205 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
206                                             struct file *file);
207 static struct file_operations acpi_video_bus_POST_info_fops = {
208         .open = acpi_video_bus_POST_info_open_fs,
209         .read = seq_read,
210         .llseek = seq_lseek,
211         .release = single_release,
212 };
213
214 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
215 static struct file_operations acpi_video_bus_POST_fops = {
216         .open = acpi_video_bus_POST_open_fs,
217         .read = seq_read,
218         .llseek = seq_lseek,
219         .release = single_release,
220 };
221
222 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
223 static struct file_operations acpi_video_bus_DOS_fops = {
224         .open = acpi_video_bus_DOS_open_fs,
225         .read = seq_read,
226         .llseek = seq_lseek,
227         .release = single_release,
228 };
229
230 /* device */
231 static int acpi_video_device_info_open_fs(struct inode *inode,
232                                           struct file *file);
233 static struct file_operations acpi_video_device_info_fops = {
234         .open = acpi_video_device_info_open_fs,
235         .read = seq_read,
236         .llseek = seq_lseek,
237         .release = single_release,
238 };
239
240 static int acpi_video_device_state_open_fs(struct inode *inode,
241                                            struct file *file);
242 static struct file_operations acpi_video_device_state_fops = {
243         .open = acpi_video_device_state_open_fs,
244         .read = seq_read,
245         .llseek = seq_lseek,
246         .release = single_release,
247 };
248
249 static int acpi_video_device_brightness_open_fs(struct inode *inode,
250                                                 struct file *file);
251 static struct file_operations acpi_video_device_brightness_fops = {
252         .open = acpi_video_device_brightness_open_fs,
253         .read = seq_read,
254         .llseek = seq_lseek,
255         .release = single_release,
256 };
257
258 static int acpi_video_device_EDID_open_fs(struct inode *inode,
259                                           struct file *file);
260 static struct file_operations acpi_video_device_EDID_fops = {
261         .open = acpi_video_device_EDID_open_fs,
262         .read = seq_read,
263         .llseek = seq_lseek,
264         .release = single_release,
265 };
266
267 static char device_decode[][30] = {
268         "motherboard VGA device",
269         "PCI VGA device",
270         "AGP VGA device",
271         "UNKNOWN",
272 };
273
274 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
275 static void acpi_video_device_rebind(struct acpi_video_bus *video);
276 static void acpi_video_device_bind(struct acpi_video_bus *video,
277                                    struct acpi_video_device *device);
278 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
279 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
280                         int level);
281 static int acpi_video_device_lcd_get_level_current(
282                         struct acpi_video_device *device,
283                         unsigned long *level);
284 static int acpi_video_get_next_level(struct acpi_video_device *device,
285                                      u32 level_current, u32 event);
286 static void acpi_video_switch_brightness(struct acpi_video_device *device,
287                                          int event);
288 static int acpi_video_device_get_state(struct acpi_video_device *device,
289                             unsigned long *state);
290 static int acpi_video_output_get(struct output_device *od);
291 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
292
293 /*backlight device sysfs support*/
294 static int acpi_video_get_brightness(struct backlight_device *bd)
295 {
296         unsigned long cur_level;
297         struct acpi_video_device *vd =
298                 (struct acpi_video_device *)bl_get_data(bd);
299         acpi_video_device_lcd_get_level_current(vd, &cur_level);
300         return (int) cur_level;
301 }
302
303 static int acpi_video_set_brightness(struct backlight_device *bd)
304 {
305         int request_level = bd->props.brightness;
306         struct acpi_video_device *vd =
307                 (struct acpi_video_device *)bl_get_data(bd);
308         acpi_video_device_lcd_set_level(vd, request_level);
309         return 0;
310 }
311
312 static struct backlight_ops acpi_backlight_ops = {
313         .get_brightness = acpi_video_get_brightness,
314         .update_status  = acpi_video_set_brightness,
315 };
316
317 /*video output device sysfs support*/
318 static int acpi_video_output_get(struct output_device *od)
319 {
320         unsigned long state;
321         struct acpi_video_device *vd =
322                 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
323         acpi_video_device_get_state(vd, &state);
324         return (int)state;
325 }
326
327 static int acpi_video_output_set(struct output_device *od)
328 {
329         unsigned long state = od->request_state;
330         struct acpi_video_device *vd=
331                 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
332         return acpi_video_device_set_state(vd, state);
333 }
334
335 static struct output_properties acpi_output_properties = {
336         .set_state = acpi_video_output_set,
337         .get_status = acpi_video_output_get,
338 };
339 /* --------------------------------------------------------------------------
340                                Video Management
341    -------------------------------------------------------------------------- */
342
343 /* device */
344
345 static int
346 acpi_video_device_query(struct acpi_video_device *device, unsigned long *state)
347 {
348         int status;
349
350         status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
351
352         return status;
353 }
354
355 static int
356 acpi_video_device_get_state(struct acpi_video_device *device,
357                             unsigned long *state)
358 {
359         int status;
360
361         status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
362
363         return status;
364 }
365
366 static int
367 acpi_video_device_set_state(struct acpi_video_device *device, int state)
368 {
369         int status;
370         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
371         struct acpi_object_list args = { 1, &arg0 };
372         unsigned long ret;
373
374
375         arg0.integer.value = state;
376         status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
377
378         return status;
379 }
380
381 static int
382 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
383                                    union acpi_object **levels)
384 {
385         int status;
386         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
387         union acpi_object *obj;
388
389
390         *levels = NULL;
391
392         status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
393         if (!ACPI_SUCCESS(status))
394                 return status;
395         obj = (union acpi_object *)buffer.pointer;
396         if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
397                 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
398                 status = -EFAULT;
399                 goto err;
400         }
401
402         *levels = obj;
403
404         return 0;
405
406       err:
407         kfree(buffer.pointer);
408
409         return status;
410 }
411
412 static int
413 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
414 {
415         int status = AE_OK;
416         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
417         struct acpi_object_list args = { 1, &arg0 };
418
419
420         arg0.integer.value = level;
421
422         if (device->cap._BCM)
423                 status = acpi_evaluate_object(device->dev->handle, "_BCM",
424                                               &args, NULL);
425         device->brightness->curr = level;
426         return status;
427 }
428
429 static int
430 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
431                                         unsigned long *level)
432 {
433         if (device->cap._BQC)
434                 return acpi_evaluate_integer(device->dev->handle, "_BQC", NULL,
435                                              level);
436         *level = device->brightness->curr;
437         return AE_OK;
438 }
439
440 static int
441 acpi_video_device_EDID(struct acpi_video_device *device,
442                        union acpi_object **edid, ssize_t length)
443 {
444         int status;
445         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
446         union acpi_object *obj;
447         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
448         struct acpi_object_list args = { 1, &arg0 };
449
450
451         *edid = NULL;
452
453         if (!device)
454                 return -ENODEV;
455         if (length == 128)
456                 arg0.integer.value = 1;
457         else if (length == 256)
458                 arg0.integer.value = 2;
459         else
460                 return -EINVAL;
461
462         status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
463         if (ACPI_FAILURE(status))
464                 return -ENODEV;
465
466         obj = buffer.pointer;
467
468         if (obj && obj->type == ACPI_TYPE_BUFFER)
469                 *edid = obj;
470         else {
471                 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
472                 status = -EFAULT;
473                 kfree(obj);
474         }
475
476         return status;
477 }
478
479 /* bus */
480
481 static int
482 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
483 {
484         int status;
485         unsigned long tmp;
486         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
487         struct acpi_object_list args = { 1, &arg0 };
488
489
490         arg0.integer.value = option;
491
492         status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
493         if (ACPI_SUCCESS(status))
494                 status = tmp ? (-EINVAL) : (AE_OK);
495
496         return status;
497 }
498
499 static int
500 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long *id)
501 {
502         int status;
503
504         status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
505
506         return status;
507 }
508
509 static int
510 acpi_video_bus_POST_options(struct acpi_video_bus *video,
511                             unsigned long *options)
512 {
513         int status;
514
515         status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
516         *options &= 3;
517
518         return status;
519 }
520
521 /*
522  *  Arg:
523  *      video           : video bus device pointer
524  *      bios_flag       : 
525  *              0.      The system BIOS should NOT automatically switch(toggle)
526  *                      the active display output.
527  *              1.      The system BIOS should automatically switch (toggle) the
528  *                      active display output. No switch event.
529  *              2.      The _DGS value should be locked.
530  *              3.      The system BIOS should not automatically switch (toggle) the
531  *                      active display output, but instead generate the display switch
532  *                      event notify code.
533  *      lcd_flag        :
534  *              0.      The system BIOS should automatically control the brightness level
535  *                      of the LCD when the power changes from AC to DC
536  *              1.      The system BIOS should NOT automatically control the brightness 
537  *                      level of the LCD when the power changes from AC to DC.
538  * Return Value:
539  *              -1      wrong arg.
540  */
541
542 static int
543 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
544 {
545         acpi_integer status = 0;
546         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
547         struct acpi_object_list args = { 1, &arg0 };
548
549
550         if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
551                 status = -1;
552                 goto Failed;
553         }
554         arg0.integer.value = (lcd_flag << 2) | bios_flag;
555         video->dos_setting = arg0.integer.value;
556         acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
557
558       Failed:
559         return status;
560 }
561
562 /*
563  *  Arg:        
564  *      device  : video output device (LCD, CRT, ..)
565  *
566  *  Return Value:
567  *      None
568  *
569  *  Find out all required AML methods defined under the output
570  *  device.
571  */
572
573 static void acpi_video_device_find_cap(struct acpi_video_device *device)
574 {
575         acpi_handle h_dummy1;
576         int i;
577         u32 max_level = 0;
578         union acpi_object *obj = NULL;
579         struct acpi_video_device_brightness *br = NULL;
580
581
582         memset(&device->cap, 0, sizeof(device->cap));
583
584         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
585                 device->cap._ADR = 1;
586         }
587         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
588                 device->cap._BCL = 1;
589         }
590         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
591                 device->cap._BCM = 1;
592         }
593         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
594                 device->cap._BQC = 1;
595         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
596                 device->cap._DDC = 1;
597         }
598         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
599                 device->cap._DCS = 1;
600         }
601         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
602                 device->cap._DGS = 1;
603         }
604         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
605                 device->cap._DSS = 1;
606         }
607
608         if (ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
609
610                 if (obj->package.count >= 2) {
611                         int count = 0;
612                         union acpi_object *o;
613
614                         br = kzalloc(sizeof(*br), GFP_KERNEL);
615                         if (!br) {
616                                 printk(KERN_ERR "can't allocate memory\n");
617                         } else {
618                                 br->levels = kmalloc(obj->package.count *
619                                                      sizeof *(br->levels), GFP_KERNEL);
620                                 if (!br->levels)
621                                         goto out;
622
623                                 for (i = 0; i < obj->package.count; i++) {
624                                         o = (union acpi_object *)&obj->package.
625                                             elements[i];
626                                         if (o->type != ACPI_TYPE_INTEGER) {
627                                                 printk(KERN_ERR PREFIX "Invalid data\n");
628                                                 continue;
629                                         }
630                                         br->levels[count] = (u32) o->integer.value;
631
632                                         if (br->levels[count] > max_level)
633                                                 max_level = br->levels[count];
634                                         count++;
635                                 }
636                               out:
637                                 if (count < 2) {
638                                         kfree(br->levels);
639                                         kfree(br);
640                                 } else {
641                                         br->count = count;
642                                         device->brightness = br;
643                                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
644                                                           "found %d brightness levels\n",
645                                                           count));
646                                 }
647                         }
648                 }
649
650         } else {
651                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available LCD brightness level\n"));
652         }
653
654         kfree(obj);
655
656         if (device->cap._BCL && device->cap._BCM && device->cap._BQC && max_level > 0){
657                 unsigned long tmp;
658                 static int count = 0;
659                 char *name;
660                 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
661                 if (!name)
662                         return;
663
664                 sprintf(name, "acpi_video%d", count++);
665                 acpi_video_device_lcd_get_level_current(device, &tmp);
666                 device->backlight = backlight_device_register(name,
667                         NULL, device, &acpi_backlight_ops);
668                 device->backlight->props.max_brightness = max_level;
669                 device->backlight->props.brightness = (int)tmp;
670                 backlight_update_status(device->backlight);
671
672                 kfree(name);
673         }
674         if (device->cap._DCS && device->cap._DSS){
675                 static int count = 0;
676                 char *name;
677                 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
678                 if (!name)
679                         return;
680                 sprintf(name, "acpi_video%d", count++);
681                 device->output_dev = video_output_register(name,
682                                 NULL, device, &acpi_output_properties);
683                 kfree(name);
684         }
685         return;
686 }
687
688 /*
689  *  Arg:        
690  *      device  : video output device (VGA)
691  *
692  *  Return Value:
693  *      None
694  *
695  *  Find out all required AML methods defined under the video bus device.
696  */
697
698 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
699 {
700         acpi_handle h_dummy1;
701
702         memset(&video->cap, 0, sizeof(video->cap));
703         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
704                 video->cap._DOS = 1;
705         }
706         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
707                 video->cap._DOD = 1;
708         }
709         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
710                 video->cap._ROM = 1;
711         }
712         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
713                 video->cap._GPD = 1;
714         }
715         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
716                 video->cap._SPD = 1;
717         }
718         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
719                 video->cap._VPO = 1;
720         }
721 }
722
723 /*
724  * Check whether the video bus device has required AML method to
725  * support the desired features
726  */
727
728 static int acpi_video_bus_check(struct acpi_video_bus *video)
729 {
730         acpi_status status = -ENOENT;
731
732
733         if (!video)
734                 return -EINVAL;
735
736         /* Since there is no HID, CID and so on for VGA driver, we have
737          * to check well known required nodes.
738          */
739
740         /* Does this device support video switching? */
741         if (video->cap._DOS) {
742                 video->flags.multihead = 1;
743                 status = 0;
744         }
745
746         /* Does this device support retrieving a video ROM? */
747         if (video->cap._ROM) {
748                 video->flags.rom = 1;
749                 status = 0;
750         }
751
752         /* Does this device support configuring which video device to POST? */
753         if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
754                 video->flags.post = 1;
755                 status = 0;
756         }
757
758         return status;
759 }
760
761 /* --------------------------------------------------------------------------
762                               FS Interface (/proc)
763    -------------------------------------------------------------------------- */
764
765 static struct proc_dir_entry *acpi_video_dir;
766
767 /* video devices */
768
769 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
770 {
771         struct acpi_video_device *dev = seq->private;
772
773
774         if (!dev)
775                 goto end;
776
777         seq_printf(seq, "device_id:    0x%04x\n", (u32) dev->device_id);
778         seq_printf(seq, "type:         ");
779         if (dev->flags.crt)
780                 seq_printf(seq, "CRT\n");
781         else if (dev->flags.lcd)
782                 seq_printf(seq, "LCD\n");
783         else if (dev->flags.tvout)
784                 seq_printf(seq, "TVOUT\n");
785         else if (dev->flags.dvi)
786                 seq_printf(seq, "DVI\n");
787         else
788                 seq_printf(seq, "UNKNOWN\n");
789
790         seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
791
792       end:
793         return 0;
794 }
795
796 static int
797 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
798 {
799         return single_open(file, acpi_video_device_info_seq_show,
800                            PDE(inode)->data);
801 }
802
803 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
804 {
805         int status;
806         struct acpi_video_device *dev = seq->private;
807         unsigned long state;
808
809
810         if (!dev)
811                 goto end;
812
813         status = acpi_video_device_get_state(dev, &state);
814         seq_printf(seq, "state:     ");
815         if (ACPI_SUCCESS(status))
816                 seq_printf(seq, "0x%02lx\n", state);
817         else
818                 seq_printf(seq, "<not supported>\n");
819
820         status = acpi_video_device_query(dev, &state);
821         seq_printf(seq, "query:     ");
822         if (ACPI_SUCCESS(status))
823                 seq_printf(seq, "0x%02lx\n", state);
824         else
825                 seq_printf(seq, "<not supported>\n");
826
827       end:
828         return 0;
829 }
830
831 static int
832 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
833 {
834         return single_open(file, acpi_video_device_state_seq_show,
835                            PDE(inode)->data);
836 }
837
838 static ssize_t
839 acpi_video_device_write_state(struct file *file,
840                               const char __user * buffer,
841                               size_t count, loff_t * data)
842 {
843         int status;
844         struct seq_file *m = file->private_data;
845         struct acpi_video_device *dev = m->private;
846         char str[12] = { 0 };
847         u32 state = 0;
848
849
850         if (!dev || count + 1 > sizeof str)
851                 return -EINVAL;
852
853         if (copy_from_user(str, buffer, count))
854                 return -EFAULT;
855
856         str[count] = 0;
857         state = simple_strtoul(str, NULL, 0);
858         state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
859
860         status = acpi_video_device_set_state(dev, state);
861
862         if (status)
863                 return -EFAULT;
864
865         return count;
866 }
867
868 static int
869 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
870 {
871         struct acpi_video_device *dev = seq->private;
872         int i;
873
874
875         if (!dev || !dev->brightness) {
876                 seq_printf(seq, "<not supported>\n");
877                 return 0;
878         }
879
880         seq_printf(seq, "levels: ");
881         for (i = 0; i < dev->brightness->count; i++)
882                 seq_printf(seq, " %d", dev->brightness->levels[i]);
883         seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
884
885         return 0;
886 }
887
888 static int
889 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
890 {
891         return single_open(file, acpi_video_device_brightness_seq_show,
892                            PDE(inode)->data);
893 }
894
895 static ssize_t
896 acpi_video_device_write_brightness(struct file *file,
897                                    const char __user * buffer,
898                                    size_t count, loff_t * data)
899 {
900         struct seq_file *m = file->private_data;
901         struct acpi_video_device *dev = m->private;
902         char str[5] = { 0 };
903         unsigned int level = 0;
904         int i;
905
906
907         if (!dev || !dev->brightness || count + 1 > sizeof str)
908                 return -EINVAL;
909
910         if (copy_from_user(str, buffer, count))
911                 return -EFAULT;
912
913         str[count] = 0;
914         level = simple_strtoul(str, NULL, 0);
915
916         if (level > 100)
917                 return -EFAULT;
918
919         /* validate through the list of available levels */
920         for (i = 0; i < dev->brightness->count; i++)
921                 if (level == dev->brightness->levels[i]) {
922                         if (ACPI_SUCCESS
923                             (acpi_video_device_lcd_set_level(dev, level)))
924                                 dev->brightness->curr = level;
925                         break;
926                 }
927
928         return count;
929 }
930
931 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
932 {
933         struct acpi_video_device *dev = seq->private;
934         int status;
935         int i;
936         union acpi_object *edid = NULL;
937
938
939         if (!dev)
940                 goto out;
941
942         status = acpi_video_device_EDID(dev, &edid, 128);
943         if (ACPI_FAILURE(status)) {
944                 status = acpi_video_device_EDID(dev, &edid, 256);
945         }
946
947         if (ACPI_FAILURE(status)) {
948                 goto out;
949         }
950
951         if (edid && edid->type == ACPI_TYPE_BUFFER) {
952                 for (i = 0; i < edid->buffer.length; i++)
953                         seq_putc(seq, edid->buffer.pointer[i]);
954         }
955
956       out:
957         if (!edid)
958                 seq_printf(seq, "<not supported>\n");
959         else
960                 kfree(edid);
961
962         return 0;
963 }
964
965 static int
966 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
967 {
968         return single_open(file, acpi_video_device_EDID_seq_show,
969                            PDE(inode)->data);
970 }
971
972 static int acpi_video_device_add_fs(struct acpi_device *device)
973 {
974         struct proc_dir_entry *entry = NULL;
975         struct acpi_video_device *vid_dev;
976
977
978         if (!device)
979                 return -ENODEV;
980
981         vid_dev = acpi_driver_data(device);
982         if (!vid_dev)
983                 return -ENODEV;
984
985         if (!acpi_device_dir(device)) {
986                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
987                                                      vid_dev->video->dir);
988                 if (!acpi_device_dir(device))
989                         return -ENODEV;
990                 acpi_device_dir(device)->owner = THIS_MODULE;
991         }
992
993         /* 'info' [R] */
994         entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
995         if (!entry)
996                 return -ENODEV;
997         else {
998                 entry->proc_fops = &acpi_video_device_info_fops;
999                 entry->data = acpi_driver_data(device);
1000                 entry->owner = THIS_MODULE;
1001         }
1002
1003         /* 'state' [R/W] */
1004         entry =
1005             create_proc_entry("state", S_IFREG | S_IRUGO | S_IWUSR,
1006                               acpi_device_dir(device));
1007         if (!entry)
1008                 return -ENODEV;
1009         else {
1010                 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1011                 entry->proc_fops = &acpi_video_device_state_fops;
1012                 entry->data = acpi_driver_data(device);
1013                 entry->owner = THIS_MODULE;
1014         }
1015
1016         /* 'brightness' [R/W] */
1017         entry =
1018             create_proc_entry("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1019                               acpi_device_dir(device));
1020         if (!entry)
1021                 return -ENODEV;
1022         else {
1023                 acpi_video_device_brightness_fops.write = acpi_video_device_write_brightness;
1024                 entry->proc_fops = &acpi_video_device_brightness_fops;
1025                 entry->data = acpi_driver_data(device);
1026                 entry->owner = THIS_MODULE;
1027         }
1028
1029         /* 'EDID' [R] */
1030         entry = create_proc_entry("EDID", S_IRUGO, acpi_device_dir(device));
1031         if (!entry)
1032                 return -ENODEV;
1033         else {
1034                 entry->proc_fops = &acpi_video_device_EDID_fops;
1035                 entry->data = acpi_driver_data(device);
1036                 entry->owner = THIS_MODULE;
1037         }
1038
1039         return 0;
1040 }
1041
1042 static int acpi_video_device_remove_fs(struct acpi_device *device)
1043 {
1044         struct acpi_video_device *vid_dev;
1045
1046         vid_dev = acpi_driver_data(device);
1047         if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1048                 return -ENODEV;
1049
1050         if (acpi_device_dir(device)) {
1051                 remove_proc_entry("info", acpi_device_dir(device));
1052                 remove_proc_entry("state", acpi_device_dir(device));
1053                 remove_proc_entry("brightness", acpi_device_dir(device));
1054                 remove_proc_entry("EDID", acpi_device_dir(device));
1055                 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1056                 acpi_device_dir(device) = NULL;
1057         }
1058
1059         return 0;
1060 }
1061
1062 /* video bus */
1063 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1064 {
1065         struct acpi_video_bus *video = seq->private;
1066
1067
1068         if (!video)
1069                 goto end;
1070
1071         seq_printf(seq, "Switching heads:              %s\n",
1072                    video->flags.multihead ? "yes" : "no");
1073         seq_printf(seq, "Video ROM:                    %s\n",
1074                    video->flags.rom ? "yes" : "no");
1075         seq_printf(seq, "Device to be POSTed on boot:  %s\n",
1076                    video->flags.post ? "yes" : "no");
1077
1078       end:
1079         return 0;
1080 }
1081
1082 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1083 {
1084         return single_open(file, acpi_video_bus_info_seq_show,
1085                            PDE(inode)->data);
1086 }
1087
1088 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1089 {
1090         struct acpi_video_bus *video = seq->private;
1091
1092
1093         if (!video)
1094                 goto end;
1095
1096         printk(KERN_INFO PREFIX "Please implement %s\n", __FUNCTION__);
1097         seq_printf(seq, "<TODO>\n");
1098
1099       end:
1100         return 0;
1101 }
1102
1103 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1104 {
1105         return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1106 }
1107
1108 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1109 {
1110         struct acpi_video_bus *video = seq->private;
1111         unsigned long options;
1112         int status;
1113
1114
1115         if (!video)
1116                 goto end;
1117
1118         status = acpi_video_bus_POST_options(video, &options);
1119         if (ACPI_SUCCESS(status)) {
1120                 if (!(options & 1)) {
1121                         printk(KERN_WARNING PREFIX
1122                                "The motherboard VGA device is not listed as a possible POST device.\n");
1123                         printk(KERN_WARNING PREFIX
1124                                "This indicates a BIOS bug. Please contact the manufacturer.\n");
1125                 }
1126                 printk("%lx\n", options);
1127                 seq_printf(seq, "can POST: <integrated video>");
1128                 if (options & 2)
1129                         seq_printf(seq, " <PCI video>");
1130                 if (options & 4)
1131                         seq_printf(seq, " <AGP video>");
1132                 seq_putc(seq, '\n');
1133         } else
1134                 seq_printf(seq, "<not supported>\n");
1135       end:
1136         return 0;
1137 }
1138
1139 static int
1140 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1141 {
1142         return single_open(file, acpi_video_bus_POST_info_seq_show,
1143                            PDE(inode)->data);
1144 }
1145
1146 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1147 {
1148         struct acpi_video_bus *video = seq->private;
1149         int status;
1150         unsigned long id;
1151
1152
1153         if (!video)
1154                 goto end;
1155
1156         status = acpi_video_bus_get_POST(video, &id);
1157         if (!ACPI_SUCCESS(status)) {
1158                 seq_printf(seq, "<not supported>\n");
1159                 goto end;
1160         }
1161         seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1162
1163       end:
1164         return 0;
1165 }
1166
1167 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1168 {
1169         struct acpi_video_bus *video = seq->private;
1170
1171
1172         seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1173
1174         return 0;
1175 }
1176
1177 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1178 {
1179         return single_open(file, acpi_video_bus_POST_seq_show,
1180                            PDE(inode)->data);
1181 }
1182
1183 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1184 {
1185         return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1186 }
1187
1188 static ssize_t
1189 acpi_video_bus_write_POST(struct file *file,
1190                           const char __user * buffer,
1191                           size_t count, loff_t * data)
1192 {
1193         int status;
1194         struct seq_file *m = file->private_data;
1195         struct acpi_video_bus *video = m->private;
1196         char str[12] = { 0 };
1197         unsigned long opt, options;
1198
1199
1200         if (!video || count + 1 > sizeof str)
1201                 return -EINVAL;
1202
1203         status = acpi_video_bus_POST_options(video, &options);
1204         if (!ACPI_SUCCESS(status))
1205                 return -EINVAL;
1206
1207         if (copy_from_user(str, buffer, count))
1208                 return -EFAULT;
1209
1210         str[count] = 0;
1211         opt = strtoul(str, NULL, 0);
1212         if (opt > 3)
1213                 return -EFAULT;
1214
1215         /* just in case an OEM 'forgot' the motherboard... */
1216         options |= 1;
1217
1218         if (options & (1ul << opt)) {
1219                 status = acpi_video_bus_set_POST(video, opt);
1220                 if (!ACPI_SUCCESS(status))
1221                         return -EFAULT;
1222
1223         }
1224
1225         return count;
1226 }
1227
1228 static ssize_t
1229 acpi_video_bus_write_DOS(struct file *file,
1230                          const char __user * buffer,
1231                          size_t count, loff_t * data)
1232 {
1233         int status;
1234         struct seq_file *m = file->private_data;
1235         struct acpi_video_bus *video = m->private;
1236         char str[12] = { 0 };
1237         unsigned long opt;
1238
1239
1240         if (!video || count + 1 > sizeof str)
1241                 return -EINVAL;
1242
1243         if (copy_from_user(str, buffer, count))
1244                 return -EFAULT;
1245
1246         str[count] = 0;
1247         opt = strtoul(str, NULL, 0);
1248         if (opt > 7)
1249                 return -EFAULT;
1250
1251         status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1252
1253         if (!ACPI_SUCCESS(status))
1254                 return -EFAULT;
1255
1256         return count;
1257 }
1258
1259 static int acpi_video_bus_add_fs(struct acpi_device *device)
1260 {
1261         struct proc_dir_entry *entry = NULL;
1262         struct acpi_video_bus *video;
1263
1264
1265         video = acpi_driver_data(device);
1266
1267         if (!acpi_device_dir(device)) {
1268                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1269                                                      acpi_video_dir);
1270                 if (!acpi_device_dir(device))
1271                         return -ENODEV;
1272                 video->dir = acpi_device_dir(device);
1273                 acpi_device_dir(device)->owner = THIS_MODULE;
1274         }
1275
1276         /* 'info' [R] */
1277         entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
1278         if (!entry)
1279                 return -ENODEV;
1280         else {
1281                 entry->proc_fops = &acpi_video_bus_info_fops;
1282                 entry->data = acpi_driver_data(device);
1283                 entry->owner = THIS_MODULE;
1284         }
1285
1286         /* 'ROM' [R] */
1287         entry = create_proc_entry("ROM", S_IRUGO, acpi_device_dir(device));
1288         if (!entry)
1289                 return -ENODEV;
1290         else {
1291                 entry->proc_fops = &acpi_video_bus_ROM_fops;
1292                 entry->data = acpi_driver_data(device);
1293                 entry->owner = THIS_MODULE;
1294         }
1295
1296         /* 'POST_info' [R] */
1297         entry =
1298             create_proc_entry("POST_info", S_IRUGO, acpi_device_dir(device));
1299         if (!entry)
1300                 return -ENODEV;
1301         else {
1302                 entry->proc_fops = &acpi_video_bus_POST_info_fops;
1303                 entry->data = acpi_driver_data(device);
1304                 entry->owner = THIS_MODULE;
1305         }
1306
1307         /* 'POST' [R/W] */
1308         entry =
1309             create_proc_entry("POST", S_IFREG | S_IRUGO | S_IRUSR,
1310                               acpi_device_dir(device));
1311         if (!entry)
1312                 return -ENODEV;
1313         else {
1314                 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
1315                 entry->proc_fops = &acpi_video_bus_POST_fops;
1316                 entry->data = acpi_driver_data(device);
1317                 entry->owner = THIS_MODULE;
1318         }
1319
1320         /* 'DOS' [R/W] */
1321         entry =
1322             create_proc_entry("DOS", S_IFREG | S_IRUGO | S_IRUSR,
1323                               acpi_device_dir(device));
1324         if (!entry)
1325                 return -ENODEV;
1326         else {
1327                 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
1328                 entry->proc_fops = &acpi_video_bus_DOS_fops;
1329                 entry->data = acpi_driver_data(device);
1330                 entry->owner = THIS_MODULE;
1331         }
1332
1333         return 0;
1334 }
1335
1336 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1337 {
1338         struct acpi_video_bus *video;
1339
1340
1341         video = acpi_driver_data(device);
1342
1343         if (acpi_device_dir(device)) {
1344                 remove_proc_entry("info", acpi_device_dir(device));
1345                 remove_proc_entry("ROM", acpi_device_dir(device));
1346                 remove_proc_entry("POST_info", acpi_device_dir(device));
1347                 remove_proc_entry("POST", acpi_device_dir(device));
1348                 remove_proc_entry("DOS", acpi_device_dir(device));
1349                 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1350                 acpi_device_dir(device) = NULL;
1351         }
1352
1353         return 0;
1354 }
1355
1356 /* --------------------------------------------------------------------------
1357                                  Driver Interface
1358    -------------------------------------------------------------------------- */
1359
1360 /* device interface */
1361 static struct acpi_video_device_attrib*
1362 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1363 {
1364         int count;
1365
1366         for(count = 0; count < video->attached_count; count++)
1367                 if((video->attached_array[count].value.int_val & 0xffff) == device_id)
1368                         return &(video->attached_array[count].value.attrib);
1369         return NULL;
1370 }
1371
1372 static int
1373 acpi_video_bus_get_one_device(struct acpi_device *device,
1374                               struct acpi_video_bus *video)
1375 {
1376         unsigned long device_id;
1377         int status;
1378         struct acpi_video_device *data;
1379         struct acpi_video_device_attrib* attribute;
1380
1381         if (!device || !video)
1382                 return -EINVAL;
1383
1384         status =
1385             acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1386         if (ACPI_SUCCESS(status)) {
1387
1388                 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1389                 if (!data)
1390                         return -ENOMEM;
1391
1392                 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1393                 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1394                 acpi_driver_data(device) = data;
1395
1396                 data->device_id = device_id;
1397                 data->video = video;
1398                 data->dev = device;
1399
1400                 attribute = acpi_video_get_device_attr(video, device_id);
1401
1402                 if((attribute != NULL) && attribute->device_id_scheme) {
1403                         switch (attribute->display_type) {
1404                         case ACPI_VIDEO_DISPLAY_CRT:
1405                                 data->flags.crt = 1;
1406                                 break;
1407                         case ACPI_VIDEO_DISPLAY_TV:
1408                                 data->flags.tvout = 1;
1409                                 break;
1410                         case ACPI_VIDEO_DISPLAY_DVI:
1411                                 data->flags.dvi = 1;
1412                                 break;
1413                         case ACPI_VIDEO_DISPLAY_LCD:
1414                                 data->flags.lcd = 1;
1415                                 break;
1416                         default:
1417                                 data->flags.unknown = 1;
1418                                 break;
1419                         }
1420                         if(attribute->bios_can_detect)
1421                                 data->flags.bios = 1;
1422                 } else
1423                         data->flags.unknown = 1;
1424
1425                 acpi_video_device_bind(video, data);
1426                 acpi_video_device_find_cap(data);
1427
1428                 status = acpi_install_notify_handler(device->handle,
1429                                                      ACPI_DEVICE_NOTIFY,
1430                                                      acpi_video_device_notify,
1431                                                      data);
1432                 if (ACPI_FAILURE(status)) {
1433                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1434                                           "Error installing notify handler\n"));
1435                         if(data->brightness)
1436                                 kfree(data->brightness->levels);
1437                         kfree(data->brightness);
1438                         kfree(data);
1439                         return -ENODEV;
1440                 }
1441
1442                 mutex_lock(&video->device_list_lock);
1443                 list_add_tail(&data->entry, &video->video_device_list);
1444                 mutex_unlock(&video->device_list_lock);
1445
1446                 acpi_video_device_add_fs(device);
1447
1448                 return 0;
1449         }
1450
1451         return -ENOENT;
1452 }
1453
1454 /*
1455  *  Arg:
1456  *      video   : video bus device 
1457  *
1458  *  Return:
1459  *      none
1460  *  
1461  *  Enumerate the video device list of the video bus, 
1462  *  bind the ids with the corresponding video devices
1463  *  under the video bus.
1464  */
1465
1466 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1467 {
1468         struct acpi_video_device *dev;
1469
1470         mutex_lock(&video->device_list_lock);
1471
1472         list_for_each_entry(dev, &video->video_device_list, entry)
1473                 acpi_video_device_bind(video, dev);
1474
1475         mutex_unlock(&video->device_list_lock);
1476 }
1477
1478 /*
1479  *  Arg:
1480  *      video   : video bus device 
1481  *      device  : video output device under the video 
1482  *              bus
1483  *
1484  *  Return:
1485  *      none
1486  *  
1487  *  Bind the ids with the corresponding video devices
1488  *  under the video bus.
1489  */
1490
1491 static void
1492 acpi_video_device_bind(struct acpi_video_bus *video,
1493                        struct acpi_video_device *device)
1494 {
1495         int i;
1496
1497 #define IDS_VAL(i) video->attached_array[i].value.int_val
1498 #define IDS_BIND(i) video->attached_array[i].bind_info
1499
1500         for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
1501              i < video->attached_count; i++) {
1502                 if (device->device_id == (IDS_VAL(i) & 0xffff)) {
1503                         IDS_BIND(i) = device;
1504                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1505                 }
1506         }
1507 #undef IDS_VAL
1508 #undef IDS_BIND
1509 }
1510
1511 /*
1512  *  Arg:
1513  *      video   : video bus device 
1514  *
1515  *  Return:
1516  *      < 0     : error
1517  *  
1518  *  Call _DOD to enumerate all devices attached to display adapter
1519  *
1520  */
1521
1522 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1523 {
1524         int status;
1525         int count;
1526         int i;
1527         struct acpi_video_enumerated_device *active_device_list;
1528         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1529         union acpi_object *dod = NULL;
1530         union acpi_object *obj;
1531
1532         status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1533         if (!ACPI_SUCCESS(status)) {
1534                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1535                 return status;
1536         }
1537
1538         dod = buffer.pointer;
1539         if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1540                 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1541                 status = -EFAULT;
1542                 goto out;
1543         }
1544
1545         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1546                           dod->package.count));
1547
1548         active_device_list = kmalloc((1 +
1549                                       dod->package.count) *
1550                                      sizeof(struct
1551                                             acpi_video_enumerated_device),
1552                                      GFP_KERNEL);
1553
1554         if (!active_device_list) {
1555                 status = -ENOMEM;
1556                 goto out;
1557         }
1558
1559         count = 0;
1560         for (i = 0; i < dod->package.count; i++) {
1561                 obj = &dod->package.elements[i];
1562
1563                 if (obj->type != ACPI_TYPE_INTEGER) {
1564                         printk(KERN_ERR PREFIX "Invalid _DOD data\n");
1565                         active_device_list[i].value.int_val =
1566                             ACPI_VIDEO_HEAD_INVALID;
1567                 }
1568                 active_device_list[i].value.int_val = obj->integer.value;
1569                 active_device_list[i].bind_info = NULL;
1570                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1571                                   (int)obj->integer.value));
1572                 count++;
1573         }
1574         active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
1575
1576         kfree(video->attached_array);
1577
1578         video->attached_array = active_device_list;
1579         video->attached_count = count;
1580       out:
1581         kfree(buffer.pointer);
1582         return status;
1583 }
1584
1585 static int
1586 acpi_video_get_next_level(struct acpi_video_device *device,
1587                           u32 level_current, u32 event)
1588 {
1589         int min, max, min_above, max_below, i, l, delta = 255;
1590         max = max_below = 0;
1591         min = min_above = 255;
1592         /* Find closest level to level_current */
1593         for (i = 0; i < device->brightness->count; i++) {
1594                 l = device->brightness->levels[i];
1595                 if (abs(l - level_current) < abs(delta)) {
1596                         delta = l - level_current;
1597                         if (!delta)
1598                                 break;
1599                 }
1600         }
1601         /* Ajust level_current to closest available level */
1602         level_current += delta;
1603         for (i = 0; i < device->brightness->count; i++) {
1604                 l = device->brightness->levels[i];
1605                 if (l < min)
1606                         min = l;
1607                 if (l > max)
1608                         max = l;
1609                 if (l < min_above && l > level_current)
1610                         min_above = l;
1611                 if (l > max_below && l < level_current)
1612                         max_below = l;
1613         }
1614
1615         switch (event) {
1616         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1617                 return (level_current < max) ? min_above : min;
1618         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1619                 return (level_current < max) ? min_above : max;
1620         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1621                 return (level_current > min) ? max_below : min;
1622         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1623         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1624                 return 0;
1625         default:
1626                 return level_current;
1627         }
1628 }
1629
1630 static void
1631 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1632 {
1633         unsigned long level_current, level_next;
1634         acpi_video_device_lcd_get_level_current(device, &level_current);
1635         level_next = acpi_video_get_next_level(device, level_current, event);
1636         acpi_video_device_lcd_set_level(device, level_next);
1637 }
1638
1639 static int
1640 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1641                            struct acpi_device *device)
1642 {
1643         int status = 0;
1644         struct acpi_device *dev;
1645
1646         acpi_video_device_enumerate(video);
1647
1648         list_for_each_entry(dev, &device->children, node) {
1649
1650                 status = acpi_video_bus_get_one_device(dev, video);
1651                 if (ACPI_FAILURE(status)) {
1652                         ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
1653                         continue;
1654                 }
1655         }
1656         return status;
1657 }
1658
1659 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1660 {
1661         acpi_status status;
1662         struct acpi_video_bus *video;
1663
1664
1665         if (!device || !device->video)
1666                 return -ENOENT;
1667
1668         video = device->video;
1669
1670         acpi_video_device_remove_fs(device->dev);
1671
1672         status = acpi_remove_notify_handler(device->dev->handle,
1673                                             ACPI_DEVICE_NOTIFY,
1674                                             acpi_video_device_notify);
1675         backlight_device_unregister(device->backlight);
1676         video_output_unregister(device->output_dev);
1677
1678         return 0;
1679 }
1680
1681 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1682 {
1683         int status;
1684         struct acpi_video_device *dev, *next;
1685
1686         mutex_lock(&video->device_list_lock);
1687
1688         list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1689
1690                 status = acpi_video_bus_put_one_device(dev);
1691                 if (ACPI_FAILURE(status))
1692                         printk(KERN_WARNING PREFIX
1693                                "hhuuhhuu bug in acpi video driver.\n");
1694
1695                 if (dev->brightness) {
1696                         kfree(dev->brightness->levels);
1697                         kfree(dev->brightness);
1698                 }
1699                 list_del(&dev->entry);
1700                 kfree(dev);
1701         }
1702
1703         mutex_unlock(&video->device_list_lock);
1704
1705         return 0;
1706 }
1707
1708 /* acpi_video interface */
1709
1710 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1711 {
1712         return acpi_video_bus_DOS(video, 0, 0);
1713 }
1714
1715 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1716 {
1717         return acpi_video_bus_DOS(video, 0, 1);
1718 }
1719
1720 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1721 {
1722         struct acpi_video_bus *video = data;
1723         struct acpi_device *device = NULL;
1724         struct input_dev *input;
1725         int keycode;
1726
1727         if (!video)
1728                 return;
1729
1730         device = video->device;
1731         input = video->input;
1732
1733         switch (event) {
1734         case ACPI_VIDEO_NOTIFY_SWITCH:  /* User requested a switch,
1735                                          * most likely via hotkey. */
1736                 acpi_bus_generate_proc_event(device, event, 0);
1737                 keycode = KEY_SWITCHVIDEOMODE;
1738                 break;
1739
1740         case ACPI_VIDEO_NOTIFY_PROBE:   /* User plugged in or removed a video
1741                                          * connector. */
1742                 acpi_video_device_enumerate(video);
1743                 acpi_video_device_rebind(video);
1744                 acpi_bus_generate_proc_event(device, event, 0);
1745                 keycode = KEY_SWITCHVIDEOMODE;
1746                 break;
1747
1748         case ACPI_VIDEO_NOTIFY_CYCLE:   /* Cycle Display output hotkey pressed. */
1749                 acpi_bus_generate_proc_event(device, event, 0);
1750                 keycode = KEY_SWITCHVIDEOMODE;
1751                 break;
1752         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:     /* Next Display output hotkey pressed. */
1753                 acpi_bus_generate_proc_event(device, event, 0);
1754                 keycode = KEY_VIDEO_NEXT;
1755                 break;
1756         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:     /* previous Display output hotkey pressed. */
1757                 acpi_bus_generate_proc_event(device, event, 0);
1758                 keycode = KEY_VIDEO_PREV;
1759                 break;
1760
1761         default:
1762                 keycode = KEY_UNKNOWN;
1763                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1764                                   "Unsupported event [0x%x]\n", event));
1765                 break;
1766         }
1767
1768         acpi_notifier_call_chain(device, event, 0);
1769         input_report_key(input, keycode, 1);
1770         input_sync(input);
1771         input_report_key(input, keycode, 0);
1772         input_sync(input);
1773
1774         return;
1775 }
1776
1777 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1778 {
1779         struct acpi_video_device *video_device = data;
1780         struct acpi_device *device = NULL;
1781         struct acpi_video_bus *bus;
1782         struct input_dev *input;
1783         int keycode;
1784
1785         if (!video_device)
1786                 return;
1787
1788         device = video_device->dev;
1789         bus = video_device->video;
1790         input = bus->input;
1791
1792         switch (event) {
1793         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:        /* Cycle brightness */
1794                 if (brightness_switch_enabled)
1795                         acpi_video_switch_brightness(video_device, event);
1796                 acpi_bus_generate_proc_event(device, event, 0);
1797                 keycode = KEY_BRIGHTNESS_CYCLE;
1798                 break;
1799         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:  /* Increase brightness */
1800                 if (brightness_switch_enabled)
1801                         acpi_video_switch_brightness(video_device, event);
1802                 acpi_bus_generate_proc_event(device, event, 0);
1803                 keycode = KEY_BRIGHTNESSUP;
1804                 break;
1805         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:  /* Decrease brightness */
1806                 if (brightness_switch_enabled)
1807                         acpi_video_switch_brightness(video_device, event);
1808                 acpi_bus_generate_proc_event(device, event, 0);
1809                 keycode = KEY_BRIGHTNESSDOWN;
1810                 break;
1811         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1812                 if (brightness_switch_enabled)
1813                         acpi_video_switch_brightness(video_device, event);
1814                 acpi_bus_generate_proc_event(device, event, 0);
1815                 keycode = KEY_BRIGHTNESS_ZERO;
1816                 break;
1817         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:     /* display device off */
1818                 if (brightness_switch_enabled)
1819                         acpi_video_switch_brightness(video_device, event);
1820                 acpi_bus_generate_proc_event(device, event, 0);
1821                 keycode = KEY_DISPLAY_OFF;
1822                 break;
1823         default:
1824                 keycode = KEY_UNKNOWN;
1825                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1826                                   "Unsupported event [0x%x]\n", event));
1827                 break;
1828         }
1829
1830         acpi_notifier_call_chain(device, event, 0);
1831         input_report_key(input, keycode, 1);
1832         input_sync(input);
1833         input_report_key(input, keycode, 0);
1834         input_sync(input);
1835
1836         return;
1837 }
1838
1839 static int instance;
1840 static int acpi_video_bus_add(struct acpi_device *device)
1841 {
1842         acpi_status status;
1843         struct acpi_video_bus *video;
1844         struct input_dev *input;
1845         int error;
1846
1847         video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1848         if (!video)
1849                 return -ENOMEM;
1850
1851         /* a hack to fix the duplicate name "VID" problem on T61 */
1852         if (!strcmp(device->pnp.bus_id, "VID")) {
1853                 if (instance)
1854                         device->pnp.bus_id[3] = '0' + instance;
1855                 instance ++;
1856         }
1857
1858         video->device = device;
1859         strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1860         strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1861         acpi_driver_data(device) = video;
1862
1863         acpi_video_bus_find_cap(video);
1864         error = acpi_video_bus_check(video);
1865         if (error)
1866                 goto err_free_video;
1867
1868         error = acpi_video_bus_add_fs(device);
1869         if (error)
1870                 goto err_free_video;
1871
1872         mutex_init(&video->device_list_lock);
1873         INIT_LIST_HEAD(&video->video_device_list);
1874
1875         acpi_video_bus_get_devices(video, device);
1876         acpi_video_bus_start_devices(video);
1877
1878         status = acpi_install_notify_handler(device->handle,
1879                                              ACPI_DEVICE_NOTIFY,
1880                                              acpi_video_bus_notify, video);
1881         if (ACPI_FAILURE(status)) {
1882                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1883                                   "Error installing notify handler\n"));
1884                 error = -ENODEV;
1885                 goto err_stop_video;
1886         }
1887
1888         video->input = input = input_allocate_device();
1889         if (!input) {
1890                 error = -ENOMEM;
1891                 goto err_uninstall_notify;
1892         }
1893
1894         snprintf(video->phys, sizeof(video->phys),
1895                 "%s/video/input0", acpi_device_hid(video->device));
1896
1897         input->name = acpi_device_name(video->device);
1898         input->phys = video->phys;
1899         input->id.bustype = BUS_HOST;
1900         input->id.product = 0x06;
1901         input->dev.parent = &device->dev;
1902         input->evbit[0] = BIT(EV_KEY);
1903         set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1904         set_bit(KEY_VIDEO_NEXT, input->keybit);
1905         set_bit(KEY_VIDEO_PREV, input->keybit);
1906         set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1907         set_bit(KEY_BRIGHTNESSUP, input->keybit);
1908         set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1909         set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1910         set_bit(KEY_DISPLAY_OFF, input->keybit);
1911         set_bit(KEY_UNKNOWN, input->keybit);
1912
1913         error = input_register_device(input);
1914         if (error)
1915                 goto err_free_input_dev;
1916
1917         printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
1918                ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1919                video->flags.multihead ? "yes" : "no",
1920                video->flags.rom ? "yes" : "no",
1921                video->flags.post ? "yes" : "no");
1922
1923         return 0;
1924
1925  err_free_input_dev:
1926         input_free_device(input);
1927  err_uninstall_notify:
1928         acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1929                                    acpi_video_bus_notify);
1930  err_stop_video:
1931         acpi_video_bus_stop_devices(video);
1932         acpi_video_bus_put_devices(video);
1933         kfree(video->attached_array);
1934         acpi_video_bus_remove_fs(device);
1935  err_free_video:
1936         kfree(video);
1937         acpi_driver_data(device) = NULL;
1938
1939         return error;
1940 }
1941
1942 static int acpi_video_bus_remove(struct acpi_device *device, int type)
1943 {
1944         acpi_status status = 0;
1945         struct acpi_video_bus *video = NULL;
1946
1947
1948         if (!device || !acpi_driver_data(device))
1949                 return -EINVAL;
1950
1951         video = acpi_driver_data(device);
1952
1953         acpi_video_bus_stop_devices(video);
1954
1955         status = acpi_remove_notify_handler(video->device->handle,
1956                                             ACPI_DEVICE_NOTIFY,
1957                                             acpi_video_bus_notify);
1958
1959         acpi_video_bus_put_devices(video);
1960         acpi_video_bus_remove_fs(device);
1961
1962         input_unregister_device(video->input);
1963         kfree(video->attached_array);
1964         kfree(video);
1965
1966         return 0;
1967 }
1968
1969 static int __init acpi_video_init(void)
1970 {
1971         int result = 0;
1972
1973
1974         /*
1975            acpi_dbg_level = 0xFFFFFFFF;
1976            acpi_dbg_layer = 0x08000000;
1977          */
1978
1979         acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
1980         if (!acpi_video_dir)
1981                 return -ENODEV;
1982         acpi_video_dir->owner = THIS_MODULE;
1983
1984         result = acpi_bus_register_driver(&acpi_video_bus);
1985         if (result < 0) {
1986                 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
1987                 return -ENODEV;
1988         }
1989
1990         return 0;
1991 }
1992
1993 static void __exit acpi_video_exit(void)
1994 {
1995
1996         acpi_bus_unregister_driver(&acpi_video_bus);
1997
1998         remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
1999
2000         return;
2001 }
2002
2003 module_init(acpi_video_init);
2004 module_exit(acpi_video_exit);