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