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