ACPI: video: Use acpi_device's handle instead of driver's
[linux-2.6] / 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
277         status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
278
279         return status;
280 }
281
282 static int
283 acpi_video_device_get_state(struct acpi_video_device *device,
284                             unsigned long *state)
285 {
286         int status;
287
288         status = acpi_evaluate_integer(device->dev->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->dev->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->dev->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->dev->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->dev->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->dev->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->device->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         status = acpi_evaluate_integer(video->device->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->device->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->device->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->dev->handle, "_ADR", &h_dummy1))) {
510                 device->cap._ADR = 1;
511         }
512         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
513                 device->cap._BCL = 1;
514         }
515         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
516                 device->cap._BCM = 1;
517         }
518         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
519                 device->cap._DDC = 1;
520         }
521         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
522                 device->cap._DCS = 1;
523         }
524         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
525                 device->cap._DGS = 1;
526         }
527         if (ACPI_SUCCESS(acpi_get_handle(device->dev->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->device->handle, "_DOS", &h_dummy1))) {
592                 video->cap._DOS = 1;
593         }
594         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
595                 video->cap._DOD = 1;
596         }
597         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
598                 video->cap._ROM = 1;
599         }
600         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
601                 video->cap._GPD = 1;
602         }
603         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
604                 video->cap._SPD = 1;
605         }
606         if (ACPI_SUCCESS(acpi_get_handle(video->device->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(device->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         status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1404         if (!ACPI_SUCCESS(status)) {
1405                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1406                 return status;
1407         }
1408
1409         dod = (union acpi_object *)buffer.pointer;
1410         if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1411                 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1412                 status = -EFAULT;
1413                 goto out;
1414         }
1415
1416         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1417                           dod->package.count));
1418
1419         active_device_list = kmalloc((1 +
1420                                       dod->package.count) *
1421                                      sizeof(struct
1422                                             acpi_video_enumerated_device),
1423                                      GFP_KERNEL);
1424
1425         if (!active_device_list) {
1426                 status = -ENOMEM;
1427                 goto out;
1428         }
1429
1430         count = 0;
1431         for (i = 0; i < dod->package.count; i++) {
1432                 obj = (union acpi_object *)&dod->package.elements[i];
1433
1434                 if (obj->type != ACPI_TYPE_INTEGER) {
1435                         printk(KERN_ERR PREFIX "Invalid _DOD data\n");
1436                         active_device_list[i].value.int_val =
1437                             ACPI_VIDEO_HEAD_INVALID;
1438                 }
1439                 active_device_list[i].value.int_val = obj->integer.value;
1440                 active_device_list[i].bind_info = NULL;
1441                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1442                                   (int)obj->integer.value));
1443                 count++;
1444         }
1445         active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
1446
1447         kfree(video->attached_array);
1448
1449         video->attached_array = active_device_list;
1450         video->attached_count = count;
1451       out:
1452         acpi_os_free(buffer.pointer);
1453         return status;
1454 }
1455
1456 /*
1457  *  Arg:
1458  *      video   : video bus device 
1459  *      event   : Nontify Event
1460  *
1461  *  Return:
1462  *      < 0     : error
1463  *  
1464  *      1. Find out the current active output device.
1465  *      2. Identify the next output device to switch
1466  *      3. call _DSS to do actual switch.
1467  */
1468
1469 static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
1470 {
1471         struct list_head *node, *next;
1472         struct acpi_video_device *dev = NULL;
1473         struct acpi_video_device *dev_next = NULL;
1474         struct acpi_video_device *dev_prev = NULL;
1475         unsigned long state;
1476         int status = 0;
1477
1478
1479         list_for_each_safe(node, next, &video->video_device_list) {
1480                 dev = container_of(node, struct acpi_video_device, entry);
1481                 status = acpi_video_device_get_state(dev, &state);
1482                 if (state & 0x2) {
1483                         dev_next =
1484                             container_of(node->next, struct acpi_video_device,
1485                                          entry);
1486                         dev_prev =
1487                             container_of(node->prev, struct acpi_video_device,
1488                                          entry);
1489                         goto out;
1490                 }
1491         }
1492         dev_next = container_of(node->next, struct acpi_video_device, entry);
1493         dev_prev = container_of(node->prev, struct acpi_video_device, entry);
1494       out:
1495         switch (event) {
1496         case ACPI_VIDEO_NOTIFY_CYCLE:
1497         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
1498                 acpi_video_device_set_state(dev, 0);
1499                 acpi_video_device_set_state(dev_next, 0x80000001);
1500                 break;
1501         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
1502                 acpi_video_device_set_state(dev, 0);
1503                 acpi_video_device_set_state(dev_prev, 0x80000001);
1504         default:
1505                 break;
1506         }
1507
1508         return status;
1509 }
1510
1511 static int
1512 acpi_video_get_next_level(struct acpi_video_device *device,
1513                           u32 level_current, u32 event)
1514 {
1515         /*Fix me */
1516         return level_current;
1517 }
1518
1519 static void
1520 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1521 {
1522         unsigned long level_current, level_next;
1523         acpi_video_device_lcd_get_level_current(device, &level_current);
1524         level_next = acpi_video_get_next_level(device, level_current, event);
1525         acpi_video_device_lcd_set_level(device, level_next);
1526 }
1527
1528 static int
1529 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1530                            struct acpi_device *device)
1531 {
1532         int status = 0;
1533         struct list_head *node, *next;
1534
1535
1536         acpi_video_device_enumerate(video);
1537
1538         list_for_each_safe(node, next, &device->children) {
1539                 struct acpi_device *dev =
1540                     list_entry(node, struct acpi_device, node);
1541
1542                 if (!dev)
1543                         continue;
1544
1545                 status = acpi_video_bus_get_one_device(dev, video);
1546                 if (ACPI_FAILURE(status)) {
1547                         ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
1548                         continue;
1549                 }
1550
1551         }
1552         return status;
1553 }
1554
1555 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1556 {
1557         acpi_status status;
1558         struct acpi_video_bus *video;
1559
1560
1561         if (!device || !device->video)
1562                 return -ENOENT;
1563
1564         video = device->video;
1565
1566         down(&video->sem);
1567         list_del(&device->entry);
1568         up(&video->sem);
1569         acpi_video_device_remove_fs(device->dev);
1570
1571         status = acpi_remove_notify_handler(device->dev->handle,
1572                                             ACPI_DEVICE_NOTIFY,
1573                                             acpi_video_device_notify);
1574
1575         return 0;
1576 }
1577
1578 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1579 {
1580         int status;
1581         struct list_head *node, *next;
1582
1583
1584         list_for_each_safe(node, next, &video->video_device_list) {
1585                 struct acpi_video_device *data =
1586                     list_entry(node, struct acpi_video_device, entry);
1587                 if (!data)
1588                         continue;
1589
1590                 status = acpi_video_bus_put_one_device(data);
1591                 if (ACPI_FAILURE(status))
1592                         printk(KERN_WARNING PREFIX
1593                                "hhuuhhuu bug in acpi video driver.\n");
1594
1595                 if (data->brightness)
1596                         kfree(data->brightness->levels);
1597                 kfree(data->brightness);
1598                 kfree(data);
1599         }
1600
1601         return 0;
1602 }
1603
1604 /* acpi_video interface */
1605
1606 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1607 {
1608         return acpi_video_bus_DOS(video, 1, 0);
1609 }
1610
1611 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1612 {
1613         return acpi_video_bus_DOS(video, 0, 1);
1614 }
1615
1616 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1617 {
1618         struct acpi_video_bus *video = (struct acpi_video_bus *)data;
1619         struct acpi_device *device = NULL;
1620
1621         printk("video bus notify\n");
1622
1623         if (!video)
1624                 return;
1625
1626         device = video->device;
1627
1628         switch (event) {
1629         case ACPI_VIDEO_NOTIFY_SWITCH:  /* User request that a switch occur,
1630                                          * most likely via hotkey. */
1631                 acpi_bus_generate_event(device, event, 0);
1632                 break;
1633
1634         case ACPI_VIDEO_NOTIFY_PROBE:   /* User plug or remove a video
1635                                          * connector. */
1636                 acpi_video_device_enumerate(video);
1637                 acpi_video_device_rebind(video);
1638                 acpi_video_switch_output(video, event);
1639                 acpi_bus_generate_event(device, event, 0);
1640                 break;
1641
1642         case ACPI_VIDEO_NOTIFY_CYCLE:   /* Cycle Display output hotkey pressed. */
1643         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:     /* Next Display output hotkey pressed. */
1644         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:     /* previous Display output hotkey pressed. */
1645                 acpi_video_switch_output(video, event);
1646                 acpi_bus_generate_event(device, event, 0);
1647                 break;
1648
1649         default:
1650                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1651                                   "Unsupported event [0x%x]\n", event));
1652                 break;
1653         }
1654
1655         return;
1656 }
1657
1658 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1659 {
1660         struct acpi_video_device *video_device =
1661             (struct acpi_video_device *)data;
1662         struct acpi_device *device = NULL;
1663
1664
1665         printk("video device notify\n");
1666         if (!video_device)
1667                 return;
1668
1669         device = video_device->dev;
1670
1671         switch (event) {
1672         case ACPI_VIDEO_NOTIFY_SWITCH:  /* change in status (cycle output device) */
1673         case ACPI_VIDEO_NOTIFY_PROBE:   /* change in status (output device status) */
1674                 acpi_bus_generate_event(device, event, 0);
1675                 break;
1676         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:        /* Cycle brightness */
1677         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:  /* Increase brightness */
1678         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:  /* Decrease brightness */
1679         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1680         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:     /* display device off */
1681                 acpi_video_switch_brightness(video_device, event);
1682                 acpi_bus_generate_event(device, event, 0);
1683                 break;
1684         default:
1685                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1686                                   "Unsupported event [0x%x]\n", event));
1687                 break;
1688         }
1689         return;
1690 }
1691
1692 static int acpi_video_bus_add(struct acpi_device *device)
1693 {
1694         int result = 0;
1695         acpi_status status = 0;
1696         struct acpi_video_bus *video = NULL;
1697
1698
1699         if (!device)
1700                 return -EINVAL;
1701
1702         video = kmalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1703         if (!video)
1704                 return -ENOMEM;
1705         memset(video, 0, sizeof(struct acpi_video_bus));
1706
1707         video->handle = device->handle;
1708         video->device = device;
1709         strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1710         strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1711         acpi_driver_data(device) = video;
1712
1713         acpi_video_bus_find_cap(video);
1714         result = acpi_video_bus_check(video);
1715         if (result)
1716                 goto end;
1717
1718         result = acpi_video_bus_add_fs(device);
1719         if (result)
1720                 goto end;
1721
1722         init_MUTEX(&video->sem);
1723         INIT_LIST_HEAD(&video->video_device_list);
1724
1725         acpi_video_bus_get_devices(video, device);
1726         acpi_video_bus_start_devices(video);
1727
1728         status = acpi_install_notify_handler(device->handle,
1729                                              ACPI_DEVICE_NOTIFY,
1730                                              acpi_video_bus_notify, video);
1731         if (ACPI_FAILURE(status)) {
1732                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1733                                   "Error installing notify handler\n"));
1734                 acpi_video_bus_stop_devices(video);
1735                 acpi_video_bus_put_devices(video);
1736                 kfree(video->attached_array);
1737                 acpi_video_bus_remove_fs(device);
1738                 result = -ENODEV;
1739                 goto end;
1740         }
1741
1742         printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
1743                ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1744                video->flags.multihead ? "yes" : "no",
1745                video->flags.rom ? "yes" : "no",
1746                video->flags.post ? "yes" : "no");
1747
1748       end:
1749         if (result)
1750                 kfree(video);
1751
1752         return result;
1753 }
1754
1755 static int acpi_video_bus_remove(struct acpi_device *device, int type)
1756 {
1757         acpi_status status = 0;
1758         struct acpi_video_bus *video = NULL;
1759
1760
1761         if (!device || !acpi_driver_data(device))
1762                 return -EINVAL;
1763
1764         video = (struct acpi_video_bus *)acpi_driver_data(device);
1765
1766         acpi_video_bus_stop_devices(video);
1767
1768         status = acpi_remove_notify_handler(video->device->handle,
1769                                             ACPI_DEVICE_NOTIFY,
1770                                             acpi_video_bus_notify);
1771
1772         acpi_video_bus_put_devices(video);
1773         acpi_video_bus_remove_fs(device);
1774
1775         kfree(video->attached_array);
1776         kfree(video);
1777
1778         return 0;
1779 }
1780
1781 static int
1782 acpi_video_bus_match(struct acpi_device *device, struct acpi_driver *driver)
1783 {
1784         acpi_handle h_dummy1;
1785         acpi_handle h_dummy2;
1786         acpi_handle h_dummy3;
1787
1788
1789         if (!device || !driver)
1790                 return -EINVAL;
1791
1792         /* Since there is no HID, CID for ACPI Video drivers, we have
1793          * to check well known required nodes for each feature we support.
1794          */
1795
1796         /* Does this device able to support video switching ? */
1797         if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
1798             ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
1799                 return 0;
1800
1801         /* Does this device able to retrieve a video ROM ? */
1802         if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
1803                 return 0;
1804
1805         /* Does this device able to configure which video head to be POSTed ? */
1806         if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
1807             ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
1808             ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
1809                 return 0;
1810
1811         return -ENODEV;
1812 }
1813
1814 static int __init acpi_video_init(void)
1815 {
1816         int result = 0;
1817
1818
1819         /*
1820            acpi_dbg_level = 0xFFFFFFFF;
1821            acpi_dbg_layer = 0x08000000;
1822          */
1823
1824         acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
1825         if (!acpi_video_dir)
1826                 return -ENODEV;
1827         acpi_video_dir->owner = THIS_MODULE;
1828
1829         result = acpi_bus_register_driver(&acpi_video_bus);
1830         if (result < 0) {
1831                 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
1832                 return -ENODEV;
1833         }
1834
1835         return 0;
1836 }
1837
1838 static void __exit acpi_video_exit(void)
1839 {
1840
1841         acpi_bus_unregister_driver(&acpi_video_bus);
1842
1843         remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
1844
1845         return;
1846 }
1847
1848 module_init(acpi_video_init);
1849 module_exit(acpi_video_exit);