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