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