Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[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  *  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_BUS_NAME             "Video Bus"
44 #define ACPI_VIDEO_DEVICE_NAME          "Video Device"
45 #define ACPI_VIDEO_NOTIFY_SWITCH        0x80
46 #define ACPI_VIDEO_NOTIFY_PROBE         0x81
47 #define ACPI_VIDEO_NOTIFY_CYCLE         0x82
48 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT   0x83
49 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT   0x84
50
51 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS      0x85
52 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS        0x86
53 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS        0x87
54 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS       0x88
55 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF           0x89
56
57 #define ACPI_VIDEO_HEAD_INVALID         (~0u - 1)
58 #define ACPI_VIDEO_HEAD_END             (~0u)
59 #define MAX_NAME_LEN    20
60
61 #define ACPI_VIDEO_DISPLAY_CRT  1
62 #define ACPI_VIDEO_DISPLAY_TV   2
63 #define ACPI_VIDEO_DISPLAY_DVI  3
64 #define ACPI_VIDEO_DISPLAY_LCD  4
65
66 #define _COMPONENT              ACPI_VIDEO_COMPONENT
67 ACPI_MODULE_NAME("video");
68
69 MODULE_AUTHOR("Bruno Ducrot");
70 MODULE_DESCRIPTION("ACPI Video Driver");
71 MODULE_LICENSE("GPL");
72
73 static int acpi_video_bus_add(struct acpi_device *device);
74 static int acpi_video_bus_remove(struct acpi_device *device, int type);
75
76 static struct acpi_driver acpi_video_bus = {
77         .name = "video",
78         .class = ACPI_VIDEO_CLASS,
79         .ids = ACPI_VIDEO_HID,
80         .ops = {
81                 .add = acpi_video_bus_add,
82                 .remove = acpi_video_bus_remove,
83                 },
84 };
85
86 struct acpi_video_bus_flags {
87         u8 multihead:1;         /* can switch video heads */
88         u8 rom:1;               /* can retrieve a video rom */
89         u8 post:1;              /* can configure the head to */
90         u8 reserved:5;
91 };
92
93 struct acpi_video_bus_cap {
94         u8 _DOS:1;              /*Enable/Disable output switching */
95         u8 _DOD:1;              /*Enumerate all devices attached to display adapter */
96         u8 _ROM:1;              /*Get ROM Data */
97         u8 _GPD:1;              /*Get POST Device */
98         u8 _SPD:1;              /*Set POST Device */
99         u8 _VPO:1;              /*Video POST Options */
100         u8 reserved:2;
101 };
102
103 struct acpi_video_device_attrib {
104         u32 display_index:4;    /* A zero-based instance of the Display */
105         u32 display_port_attachment:4;  /*This field differenates displays type */
106         u32 display_type:4;     /*Describe the specific type in use */
107         u32 vendor_specific:4;  /*Chipset Vendor Specifi */
108         u32 bios_can_detect:1;  /*BIOS can detect the device */
109         u32 depend_on_vga:1;    /*Non-VGA output device whose power is related to 
110                                    the VGA device. */
111         u32 pipe_id:3;          /*For VGA multiple-head devices. */
112         u32 reserved:10;        /*Must be 0 */
113         u32 device_id_scheme:1; /*Device ID Scheme */
114 };
115
116 struct acpi_video_enumerated_device {
117         union {
118                 u32 int_val;
119                 struct acpi_video_device_attrib attrib;
120         } value;
121         struct acpi_video_device *bind_info;
122 };
123
124 struct acpi_video_bus {
125         struct acpi_device *device;
126         u8 dos_setting;
127         struct acpi_video_enumerated_device *attached_array;
128         u8 attached_count;
129         struct acpi_video_bus_cap cap;
130         struct acpi_video_bus_flags flags;
131         struct semaphore sem;
132         struct list_head video_device_list;
133         struct proc_dir_entry *dir;
134 };
135
136 struct acpi_video_device_flags {
137         u8 crt:1;
138         u8 lcd:1;
139         u8 tvout:1;
140         u8 dvi:1;
141         u8 bios:1;
142         u8 unknown:1;
143         u8 reserved:2;
144 };
145
146 struct acpi_video_device_cap {
147         u8 _ADR:1;              /*Return the unique ID */
148         u8 _BCL:1;              /*Query list of brightness control levels supported */
149         u8 _BCM:1;              /*Set the brightness level */
150         u8 _BQC:1;              /* Get current brightness level */
151         u8 _DDC:1;              /*Return the EDID for this device */
152         u8 _DCS:1;              /*Return status of output device */
153         u8 _DGS:1;              /*Query graphics state */
154         u8 _DSS:1;              /*Device state set */
155 };
156
157 struct acpi_video_device_brightness {
158         int curr;
159         int count;
160         int *levels;
161 };
162
163 struct acpi_video_device {
164         unsigned long device_id;
165         struct acpi_video_device_flags flags;
166         struct acpi_video_device_cap cap;
167         struct list_head entry;
168         struct acpi_video_bus *video;
169         struct acpi_device *dev;
170         struct acpi_video_device_brightness *brightness;
171         struct backlight_device *backlight;
172         struct backlight_properties *data;
173 };
174
175 /* bus */
176 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
177 static struct file_operations acpi_video_bus_info_fops = {
178         .open = acpi_video_bus_info_open_fs,
179         .read = seq_read,
180         .llseek = seq_lseek,
181         .release = single_release,
182 };
183
184 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
185 static struct file_operations acpi_video_bus_ROM_fops = {
186         .open = acpi_video_bus_ROM_open_fs,
187         .read = seq_read,
188         .llseek = seq_lseek,
189         .release = single_release,
190 };
191
192 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
193                                             struct file *file);
194 static struct file_operations acpi_video_bus_POST_info_fops = {
195         .open = acpi_video_bus_POST_info_open_fs,
196         .read = seq_read,
197         .llseek = seq_lseek,
198         .release = single_release,
199 };
200
201 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
202 static struct file_operations acpi_video_bus_POST_fops = {
203         .open = acpi_video_bus_POST_open_fs,
204         .read = seq_read,
205         .llseek = seq_lseek,
206         .release = single_release,
207 };
208
209 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
210 static struct file_operations acpi_video_bus_DOS_fops = {
211         .open = acpi_video_bus_DOS_open_fs,
212         .read = seq_read,
213         .llseek = seq_lseek,
214         .release = single_release,
215 };
216
217 /* device */
218 static int acpi_video_device_info_open_fs(struct inode *inode,
219                                           struct file *file);
220 static struct file_operations acpi_video_device_info_fops = {
221         .open = acpi_video_device_info_open_fs,
222         .read = seq_read,
223         .llseek = seq_lseek,
224         .release = single_release,
225 };
226
227 static int acpi_video_device_state_open_fs(struct inode *inode,
228                                            struct file *file);
229 static struct file_operations acpi_video_device_state_fops = {
230         .open = acpi_video_device_state_open_fs,
231         .read = seq_read,
232         .llseek = seq_lseek,
233         .release = single_release,
234 };
235
236 static int acpi_video_device_brightness_open_fs(struct inode *inode,
237                                                 struct file *file);
238 static struct file_operations acpi_video_device_brightness_fops = {
239         .open = acpi_video_device_brightness_open_fs,
240         .read = seq_read,
241         .llseek = seq_lseek,
242         .release = single_release,
243 };
244
245 static int acpi_video_device_EDID_open_fs(struct inode *inode,
246                                           struct file *file);
247 static struct file_operations acpi_video_device_EDID_fops = {
248         .open = acpi_video_device_EDID_open_fs,
249         .read = seq_read,
250         .llseek = seq_lseek,
251         .release = single_release,
252 };
253
254 static char device_decode[][30] = {
255         "motherboard VGA device",
256         "PCI VGA device",
257         "AGP VGA device",
258         "UNKNOWN",
259 };
260
261 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
262 static void acpi_video_device_rebind(struct acpi_video_bus *video);
263 static void acpi_video_device_bind(struct acpi_video_bus *video,
264                                    struct acpi_video_device *device);
265 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
266 static int acpi_video_switch_output(struct acpi_video_bus *video, int event);
267 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
268                         int level);
269 static int acpi_video_device_lcd_get_level_current(
270                         struct acpi_video_device *device,
271                         unsigned long *level);
272 static int acpi_video_get_next_level(struct acpi_video_device *device,
273                                      u32 level_current, u32 event);
274 static void acpi_video_switch_brightness(struct acpi_video_device *device,
275                                          int event);
276
277 /*backlight device sysfs support*/
278 static int acpi_video_get_brightness(struct backlight_device *bd)
279 {
280         unsigned long cur_level;
281         struct acpi_video_device *vd =
282                 (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
283         acpi_video_device_lcd_get_level_current(vd, &cur_level);
284         return (int) cur_level;
285 }
286
287 static int acpi_video_set_brightness(struct backlight_device *bd)
288 {
289         int request_level = bd->props->brightness;
290         struct acpi_video_device *vd =
291                 (struct acpi_video_device *)class_get_devdata(&bd->class_dev);
292         acpi_video_device_lcd_set_level(vd, request_level);
293         return 0;
294 }
295
296 /* --------------------------------------------------------------------------
297                                Video Management
298    -------------------------------------------------------------------------- */
299
300 /* device */
301
302 static int
303 acpi_video_device_query(struct acpi_video_device *device, unsigned long *state)
304 {
305         int status;
306
307         status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
308
309         return status;
310 }
311
312 static int
313 acpi_video_device_get_state(struct acpi_video_device *device,
314                             unsigned long *state)
315 {
316         int status;
317
318         status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
319
320         return status;
321 }
322
323 static int
324 acpi_video_device_set_state(struct acpi_video_device *device, int state)
325 {
326         int status;
327         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
328         struct acpi_object_list args = { 1, &arg0 };
329         unsigned long ret;
330
331
332         arg0.integer.value = state;
333         status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
334
335         return status;
336 }
337
338 static int
339 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
340                                    union acpi_object **levels)
341 {
342         int status;
343         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
344         union acpi_object *obj;
345
346
347         *levels = NULL;
348
349         status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
350         if (!ACPI_SUCCESS(status))
351                 return status;
352         obj = (union acpi_object *)buffer.pointer;
353         if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
354                 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
355                 status = -EFAULT;
356                 goto err;
357         }
358
359         *levels = obj;
360
361         return 0;
362
363       err:
364         kfree(buffer.pointer);
365
366         return status;
367 }
368
369 static int
370 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
371 {
372         int status;
373         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
374         struct acpi_object_list args = { 1, &arg0 };
375
376
377         arg0.integer.value = level;
378         status = acpi_evaluate_object(device->dev->handle, "_BCM", &args, NULL);
379
380         printk(KERN_DEBUG "set_level status: %x\n", status);
381         return status;
382 }
383
384 static int
385 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
386                                         unsigned long *level)
387 {
388         int status;
389
390         status = acpi_evaluate_integer(device->dev->handle, "_BQC", NULL, level);
391
392         return status;
393 }
394
395 static int
396 acpi_video_device_EDID(struct acpi_video_device *device,
397                        union acpi_object **edid, ssize_t length)
398 {
399         int status;
400         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
401         union acpi_object *obj;
402         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
403         struct acpi_object_list args = { 1, &arg0 };
404
405
406         *edid = NULL;
407
408         if (!device)
409                 return -ENODEV;
410         if (length == 128)
411                 arg0.integer.value = 1;
412         else if (length == 256)
413                 arg0.integer.value = 2;
414         else
415                 return -EINVAL;
416
417         status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
418         if (ACPI_FAILURE(status))
419                 return -ENODEV;
420
421         obj = buffer.pointer;
422
423         if (obj && obj->type == ACPI_TYPE_BUFFER)
424                 *edid = obj;
425         else {
426                 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
427                 status = -EFAULT;
428                 kfree(obj);
429         }
430
431         return status;
432 }
433
434 /* bus */
435
436 static int
437 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
438 {
439         int status;
440         unsigned long tmp;
441         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
442         struct acpi_object_list args = { 1, &arg0 };
443
444
445         arg0.integer.value = option;
446
447         status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
448         if (ACPI_SUCCESS(status))
449                 status = tmp ? (-EINVAL) : (AE_OK);
450
451         return status;
452 }
453
454 static int
455 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long *id)
456 {
457         int status;
458
459         status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
460
461         return status;
462 }
463
464 static int
465 acpi_video_bus_POST_options(struct acpi_video_bus *video,
466                             unsigned long *options)
467 {
468         int status;
469
470         status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
471         *options &= 3;
472
473         return status;
474 }
475
476 /*
477  *  Arg:
478  *      video           : video bus device pointer
479  *      bios_flag       : 
480  *              0.      The system BIOS should NOT automatically switch(toggle)
481  *                      the active display output.
482  *              1.      The system BIOS should automatically switch (toggle) the
483  *                      active display output. No swich event.
484  *              2.      The _DGS value should be locked.
485  *              3.      The system BIOS should not automatically switch (toggle) the
486  *                      active display output, but instead generate the display switch
487  *                      event notify code.
488  *      lcd_flag        :
489  *              0.      The system BIOS should automatically control the brightness level
490  *                      of the LCD, when the power changes from AC to DC
491  *              1.      The system BIOS should NOT automatically control the brightness 
492  *                      level of the LCD, when the power changes from AC to DC.
493  * Return Value:
494  *              -1      wrong arg.
495  */
496
497 static int
498 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
499 {
500         acpi_integer status = 0;
501         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
502         struct acpi_object_list args = { 1, &arg0 };
503
504
505         if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
506                 status = -1;
507                 goto Failed;
508         }
509         arg0.integer.value = (lcd_flag << 2) | bios_flag;
510         video->dos_setting = arg0.integer.value;
511         acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
512
513       Failed:
514         return status;
515 }
516
517 /*
518  *  Arg:        
519  *      device  : video output device (LCD, CRT, ..)
520  *
521  *  Return Value:
522  *      None
523  *
524  *  Find out all required AML method defined under the output
525  *  device.
526  */
527
528 static void acpi_video_device_find_cap(struct acpi_video_device *device)
529 {
530         acpi_integer status;
531         acpi_handle h_dummy1;
532         int i;
533         u32 max_level = 0;
534         union acpi_object *obj = NULL;
535         struct acpi_video_device_brightness *br = NULL;
536
537
538         memset(&device->cap, 0, 4);
539
540         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
541                 device->cap._ADR = 1;
542         }
543         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
544                 device->cap._BCL = 1;
545         }
546         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
547                 device->cap._BCM = 1;
548         }
549         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
550                 device->cap._BQC = 1;
551         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
552                 device->cap._DDC = 1;
553         }
554         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
555                 device->cap._DCS = 1;
556         }
557         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
558                 device->cap._DGS = 1;
559         }
560         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
561                 device->cap._DSS = 1;
562         }
563
564         status = acpi_video_device_lcd_query_levels(device, &obj);
565
566         if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count >= 2) {
567                 int count = 0;
568                 union acpi_object *o;
569
570                 br = kzalloc(sizeof(*br), GFP_KERNEL);
571                 if (!br) {
572                         printk(KERN_ERR "can't allocate memory\n");
573                 } else {
574                         br->levels = kmalloc(obj->package.count *
575                                              sizeof *(br->levels), GFP_KERNEL);
576                         if (!br->levels)
577                                 goto out;
578
579                         for (i = 0; i < obj->package.count; i++) {
580                                 o = (union acpi_object *)&obj->package.
581                                     elements[i];
582                                 if (o->type != ACPI_TYPE_INTEGER) {
583                                         printk(KERN_ERR PREFIX "Invalid data\n");
584                                         continue;
585                                 }
586                                 br->levels[count] = (u32) o->integer.value;
587                                 if (br->levels[count] > max_level)
588                                         max_level = br->levels[count];
589                                 count++;
590                         }
591                       out:
592                         if (count < 2) {
593                                 kfree(br->levels);
594                                 kfree(br);
595                         } else {
596                                 br->count = count;
597                                 device->brightness = br;
598                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
599                                                   "found %d brightness levels\n",
600                                                   count));
601                         }
602                 }
603         }
604
605         kfree(obj);
606
607         if (device->cap._BCL && device->cap._BCM && device->cap._BQC){
608                 unsigned long tmp;
609                 static int count = 0;
610                 char *name;
611                 struct backlight_properties *acpi_video_data;
612
613                 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
614                 if (!name)
615                         return;
616
617                 acpi_video_data = kzalloc(
618                         sizeof(struct backlight_properties),
619                         GFP_KERNEL);
620                 if (!acpi_video_data){
621                         kfree(name);
622                         return;
623                 }
624                 acpi_video_data->owner = THIS_MODULE;
625                 acpi_video_data->get_brightness =
626                         acpi_video_get_brightness;
627                 acpi_video_data->update_status =
628                         acpi_video_set_brightness;
629                 sprintf(name, "acpi_video%d", count++);
630                 device->data = acpi_video_data;
631                 acpi_video_data->max_brightness = max_level;
632                 acpi_video_device_lcd_get_level_current(device, &tmp);
633                 acpi_video_data->brightness = (int)tmp;
634                 device->backlight = backlight_device_register(name,
635                         NULL, device, acpi_video_data);
636                 kfree(name);
637         }
638         return;
639 }
640
641 /*
642  *  Arg:        
643  *      device  : video output device (VGA)
644  *
645  *  Return Value:
646  *      None
647  *
648  *  Find out all required AML method defined under the video bus device.
649  */
650
651 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
652 {
653         acpi_handle h_dummy1;
654
655         memset(&video->cap, 0, 4);
656         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
657                 video->cap._DOS = 1;
658         }
659         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
660                 video->cap._DOD = 1;
661         }
662         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
663                 video->cap._ROM = 1;
664         }
665         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
666                 video->cap._GPD = 1;
667         }
668         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
669                 video->cap._SPD = 1;
670         }
671         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
672                 video->cap._VPO = 1;
673         }
674 }
675
676 /*
677  * Check whether the video bus device has required AML method to
678  * support the desired features
679  */
680
681 static int acpi_video_bus_check(struct acpi_video_bus *video)
682 {
683         acpi_status status = -ENOENT;
684
685
686         if (!video)
687                 return -EINVAL;
688
689         /* Since there is no HID, CID and so on for VGA driver, we have
690          * to check well known required nodes.
691          */
692
693         /* Does this device able to support video switching ? */
694         if (video->cap._DOS) {
695                 video->flags.multihead = 1;
696                 status = 0;
697         }
698
699         /* Does this device able to retrieve a retrieve a video ROM ? */
700         if (video->cap._ROM) {
701                 video->flags.rom = 1;
702                 status = 0;
703         }
704
705         /* Does this device able to configure which video device to POST ? */
706         if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
707                 video->flags.post = 1;
708                 status = 0;
709         }
710
711         return status;
712 }
713
714 /* --------------------------------------------------------------------------
715                               FS Interface (/proc)
716    -------------------------------------------------------------------------- */
717
718 static struct proc_dir_entry *acpi_video_dir;
719
720 /* video devices */
721
722 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
723 {
724         struct acpi_video_device *dev = seq->private;
725
726
727         if (!dev)
728                 goto end;
729
730         seq_printf(seq, "device_id:    0x%04x\n", (u32) dev->device_id);
731         seq_printf(seq, "type:         ");
732         if (dev->flags.crt)
733                 seq_printf(seq, "CRT\n");
734         else if (dev->flags.lcd)
735                 seq_printf(seq, "LCD\n");
736         else if (dev->flags.tvout)
737                 seq_printf(seq, "TVOUT\n");
738         else if (dev->flags.dvi)
739                 seq_printf(seq, "DVI\n");
740         else
741                 seq_printf(seq, "UNKNOWN\n");
742
743         seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
744
745       end:
746         return 0;
747 }
748
749 static int
750 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
751 {
752         return single_open(file, acpi_video_device_info_seq_show,
753                            PDE(inode)->data);
754 }
755
756 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
757 {
758         int status;
759         struct acpi_video_device *dev = seq->private;
760         unsigned long state;
761
762
763         if (!dev)
764                 goto end;
765
766         status = acpi_video_device_get_state(dev, &state);
767         seq_printf(seq, "state:     ");
768         if (ACPI_SUCCESS(status))
769                 seq_printf(seq, "0x%02lx\n", state);
770         else
771                 seq_printf(seq, "<not supported>\n");
772
773         status = acpi_video_device_query(dev, &state);
774         seq_printf(seq, "query:     ");
775         if (ACPI_SUCCESS(status))
776                 seq_printf(seq, "0x%02lx\n", state);
777         else
778                 seq_printf(seq, "<not supported>\n");
779
780       end:
781         return 0;
782 }
783
784 static int
785 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
786 {
787         return single_open(file, acpi_video_device_state_seq_show,
788                            PDE(inode)->data);
789 }
790
791 static ssize_t
792 acpi_video_device_write_state(struct file *file,
793                               const char __user * buffer,
794                               size_t count, loff_t * data)
795 {
796         int status;
797         struct seq_file *m = file->private_data;
798         struct acpi_video_device *dev = m->private;
799         char str[12] = { 0 };
800         u32 state = 0;
801
802
803         if (!dev || count + 1 > sizeof str)
804                 return -EINVAL;
805
806         if (copy_from_user(str, buffer, count))
807                 return -EFAULT;
808
809         str[count] = 0;
810         state = simple_strtoul(str, NULL, 0);
811         state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
812
813         status = acpi_video_device_set_state(dev, state);
814
815         if (status)
816                 return -EFAULT;
817
818         return count;
819 }
820
821 static int
822 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
823 {
824         struct acpi_video_device *dev = seq->private;
825         int i;
826
827
828         if (!dev || !dev->brightness) {
829                 seq_printf(seq, "<not supported>\n");
830                 return 0;
831         }
832
833         seq_printf(seq, "levels: ");
834         for (i = 0; i < dev->brightness->count; i++)
835                 seq_printf(seq, " %d", dev->brightness->levels[i]);
836         seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
837
838         return 0;
839 }
840
841 static int
842 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
843 {
844         return single_open(file, acpi_video_device_brightness_seq_show,
845                            PDE(inode)->data);
846 }
847
848 static ssize_t
849 acpi_video_device_write_brightness(struct file *file,
850                                    const char __user * buffer,
851                                    size_t count, loff_t * data)
852 {
853         struct seq_file *m = file->private_data;
854         struct acpi_video_device *dev = m->private;
855         char str[4] = { 0 };
856         unsigned int level = 0;
857         int i;
858
859
860         if (!dev || !dev->brightness || count + 1 > sizeof str)
861                 return -EINVAL;
862
863         if (copy_from_user(str, buffer, count))
864                 return -EFAULT;
865
866         str[count] = 0;
867         level = simple_strtoul(str, NULL, 0);
868
869         if (level > 100)
870                 return -EFAULT;
871
872         /* validate though the list of available levels */
873         for (i = 0; i < dev->brightness->count; i++)
874                 if (level == dev->brightness->levels[i]) {
875                         if (ACPI_SUCCESS
876                             (acpi_video_device_lcd_set_level(dev, level)))
877                                 dev->brightness->curr = level;
878                         break;
879                 }
880
881         return count;
882 }
883
884 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
885 {
886         struct acpi_video_device *dev = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = file->private_data;
1148         struct acpi_video_bus *video = 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 = file->private_data;
1188         struct acpi_video_bus *video = 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 = 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 = 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 static struct acpi_video_device_attrib*
1315 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1316 {
1317         int count;
1318
1319         for(count = 0; count < video->attached_count; count++)
1320                 if((video->attached_array[count].value.int_val & 0xffff) == device_id)
1321                         return &(video->attached_array[count].value.attrib);
1322         return NULL;
1323 }
1324
1325 static int
1326 acpi_video_bus_get_one_device(struct acpi_device *device,
1327                               struct acpi_video_bus *video)
1328 {
1329         unsigned long device_id;
1330         int status;
1331         struct acpi_video_device *data;
1332         struct acpi_video_device_attrib* attribute;
1333
1334         if (!device || !video)
1335                 return -EINVAL;
1336
1337         status =
1338             acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1339         if (ACPI_SUCCESS(status)) {
1340
1341                 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1342                 if (!data)
1343                         return -ENOMEM;
1344
1345                 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1346                 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1347                 acpi_driver_data(device) = data;
1348
1349                 data->device_id = device_id;
1350                 data->video = video;
1351                 data->dev = device;
1352
1353                 attribute = acpi_video_get_device_attr(video, device_id);
1354
1355                 if((attribute != NULL) && attribute->device_id_scheme) {
1356                         switch (attribute->display_type) {
1357                         case ACPI_VIDEO_DISPLAY_CRT:
1358                                 data->flags.crt = 1;
1359                                 break;
1360                         case ACPI_VIDEO_DISPLAY_TV:
1361                                 data->flags.tvout = 1;
1362                                 break;
1363                         case ACPI_VIDEO_DISPLAY_DVI:
1364                                 data->flags.dvi = 1;
1365                                 break;
1366                         case ACPI_VIDEO_DISPLAY_LCD:
1367                                 data->flags.lcd = 1;
1368                                 break;
1369                         default:
1370                                 data->flags.unknown = 1;
1371                                 break;
1372                         }
1373                         if(attribute->bios_can_detect)
1374                                 data->flags.bios = 1;
1375                 } else
1376                         data->flags.unknown = 1;
1377
1378                 acpi_video_device_bind(video, data);
1379                 acpi_video_device_find_cap(data);
1380
1381                 status = acpi_install_notify_handler(device->handle,
1382                                                      ACPI_DEVICE_NOTIFY,
1383                                                      acpi_video_device_notify,
1384                                                      data);
1385                 if (ACPI_FAILURE(status)) {
1386                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1387                                           "Error installing notify handler\n"));
1388                         if(data->brightness)
1389                                 kfree(data->brightness->levels);
1390                         kfree(data->brightness);
1391                         kfree(data);
1392                         return -ENODEV;
1393                 }
1394
1395                 down(&video->sem);
1396                 list_add_tail(&data->entry, &video->video_device_list);
1397                 up(&video->sem);
1398
1399                 acpi_video_device_add_fs(device);
1400
1401                 return 0;
1402         }
1403
1404         return -ENOENT;
1405 }
1406
1407 /*
1408  *  Arg:
1409  *      video   : video bus device 
1410  *
1411  *  Return:
1412  *      none
1413  *  
1414  *  Enumerate the video device list of the video bus, 
1415  *  bind the ids with the corresponding video devices
1416  *  under the video bus.
1417  */
1418
1419 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1420 {
1421         struct list_head *node, *next;
1422         list_for_each_safe(node, next, &video->video_device_list) {
1423                 struct acpi_video_device *dev =
1424                     container_of(node, struct acpi_video_device, entry);
1425                 acpi_video_device_bind(video, dev);
1426         }
1427 }
1428
1429 /*
1430  *  Arg:
1431  *      video   : video bus device 
1432  *      device  : video output device under the video 
1433  *              bus
1434  *
1435  *  Return:
1436  *      none
1437  *  
1438  *  Bind the ids with the corresponding video devices
1439  *  under the video bus.
1440  */
1441
1442 static void
1443 acpi_video_device_bind(struct acpi_video_bus *video,
1444                        struct acpi_video_device *device)
1445 {
1446         int i;
1447
1448 #define IDS_VAL(i) video->attached_array[i].value.int_val
1449 #define IDS_BIND(i) video->attached_array[i].bind_info
1450
1451         for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
1452              i < video->attached_count; i++) {
1453                 if (device->device_id == (IDS_VAL(i) & 0xffff)) {
1454                         IDS_BIND(i) = device;
1455                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1456                 }
1457         }
1458 #undef IDS_VAL
1459 #undef IDS_BIND
1460 }
1461
1462 /*
1463  *  Arg:
1464  *      video   : video bus device 
1465  *
1466  *  Return:
1467  *      < 0     : error
1468  *  
1469  *  Call _DOD to enumerate all devices attached to display adapter
1470  *
1471  */
1472
1473 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1474 {
1475         int status;
1476         int count;
1477         int i;
1478         struct acpi_video_enumerated_device *active_device_list;
1479         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1480         union acpi_object *dod = NULL;
1481         union acpi_object *obj;
1482
1483         status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1484         if (!ACPI_SUCCESS(status)) {
1485                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1486                 return status;
1487         }
1488
1489         dod = buffer.pointer;
1490         if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1491                 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1492                 status = -EFAULT;
1493                 goto out;
1494         }
1495
1496         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1497                           dod->package.count));
1498
1499         active_device_list = kmalloc((1 +
1500                                       dod->package.count) *
1501                                      sizeof(struct
1502                                             acpi_video_enumerated_device),
1503                                      GFP_KERNEL);
1504
1505         if (!active_device_list) {
1506                 status = -ENOMEM;
1507                 goto out;
1508         }
1509
1510         count = 0;
1511         for (i = 0; i < dod->package.count; i++) {
1512                 obj = &dod->package.elements[i];
1513
1514                 if (obj->type != ACPI_TYPE_INTEGER) {
1515                         printk(KERN_ERR PREFIX "Invalid _DOD data\n");
1516                         active_device_list[i].value.int_val =
1517                             ACPI_VIDEO_HEAD_INVALID;
1518                 }
1519                 active_device_list[i].value.int_val = obj->integer.value;
1520                 active_device_list[i].bind_info = NULL;
1521                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1522                                   (int)obj->integer.value));
1523                 count++;
1524         }
1525         active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
1526
1527         kfree(video->attached_array);
1528
1529         video->attached_array = active_device_list;
1530         video->attached_count = count;
1531       out:
1532         kfree(buffer.pointer);
1533         return status;
1534 }
1535
1536 /*
1537  *  Arg:
1538  *      video   : video bus device 
1539  *      event   : Nontify Event
1540  *
1541  *  Return:
1542  *      < 0     : error
1543  *  
1544  *      1. Find out the current active output device.
1545  *      2. Identify the next output device to switch
1546  *      3. call _DSS to do actual switch.
1547  */
1548
1549 static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
1550 {
1551         struct list_head *node, *next;
1552         struct acpi_video_device *dev = NULL;
1553         struct acpi_video_device *dev_next = NULL;
1554         struct acpi_video_device *dev_prev = NULL;
1555         unsigned long state;
1556         int status = 0;
1557
1558
1559         list_for_each_safe(node, next, &video->video_device_list) {
1560                 dev = container_of(node, struct acpi_video_device, entry);
1561                 status = acpi_video_device_get_state(dev, &state);
1562                 if (state & 0x2) {
1563                         dev_next =
1564                             container_of(node->next, struct acpi_video_device,
1565                                          entry);
1566                         dev_prev =
1567                             container_of(node->prev, struct acpi_video_device,
1568                                          entry);
1569                         goto out;
1570                 }
1571         }
1572         dev_next = container_of(node->next, struct acpi_video_device, entry);
1573         dev_prev = container_of(node->prev, struct acpi_video_device, entry);
1574       out:
1575         switch (event) {
1576         case ACPI_VIDEO_NOTIFY_CYCLE:
1577         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
1578                 acpi_video_device_set_state(dev, 0);
1579                 acpi_video_device_set_state(dev_next, 0x80000001);
1580                 break;
1581         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
1582                 acpi_video_device_set_state(dev, 0);
1583                 acpi_video_device_set_state(dev_prev, 0x80000001);
1584         default:
1585                 break;
1586         }
1587
1588         return status;
1589 }
1590
1591 static int
1592 acpi_video_get_next_level(struct acpi_video_device *device,
1593                           u32 level_current, u32 event)
1594 {
1595         int min, max, min_above, max_below, i, l;
1596         max = max_below = 0;
1597         min = min_above = 255;
1598         for (i = 0; i < device->brightness->count; i++) {
1599                 l = device->brightness->levels[i];
1600                 if (l < min)
1601                         min = l;
1602                 if (l > max)
1603                         max = l;
1604                 if (l < min_above && l > level_current)
1605                         min_above = l;
1606                 if (l > max_below && l < level_current)
1607                         max_below = l;
1608         }
1609
1610         switch (event) {
1611         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1612                 return (level_current < max) ? min_above : min;
1613         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1614                 return (level_current < max) ? min_above : max;
1615         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1616                 return (level_current > min) ? max_below : min;
1617         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1618         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1619                 return 0;
1620         default:
1621                 return level_current;
1622         }
1623 }
1624
1625 static void
1626 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1627 {
1628         unsigned long level_current, level_next;
1629         acpi_video_device_lcd_get_level_current(device, &level_current);
1630         level_next = acpi_video_get_next_level(device, level_current, event);
1631         acpi_video_device_lcd_set_level(device, level_next);
1632 }
1633
1634 static int
1635 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1636                            struct acpi_device *device)
1637 {
1638         int status = 0;
1639         struct list_head *node, *next;
1640
1641
1642         acpi_video_device_enumerate(video);
1643
1644         list_for_each_safe(node, next, &device->children) {
1645                 struct acpi_device *dev =
1646                     list_entry(node, struct acpi_device, node);
1647
1648                 if (!dev)
1649                         continue;
1650
1651                 status = acpi_video_bus_get_one_device(dev, video);
1652                 if (ACPI_FAILURE(status)) {
1653                         ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
1654                         continue;
1655                 }
1656
1657         }
1658         return status;
1659 }
1660
1661 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1662 {
1663         acpi_status status;
1664         struct acpi_video_bus *video;
1665
1666
1667         if (!device || !device->video)
1668                 return -ENOENT;
1669
1670         video = device->video;
1671
1672         down(&video->sem);
1673         list_del(&device->entry);
1674         up(&video->sem);
1675         acpi_video_device_remove_fs(device->dev);
1676
1677         status = acpi_remove_notify_handler(device->dev->handle,
1678                                             ACPI_DEVICE_NOTIFY,
1679                                             acpi_video_device_notify);
1680         if (device->backlight){
1681                 backlight_device_unregister(device->backlight);
1682                 kfree(device->data);
1683         }
1684         return 0;
1685 }
1686
1687 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1688 {
1689         int status;
1690         struct list_head *node, *next;
1691
1692
1693         list_for_each_safe(node, next, &video->video_device_list) {
1694                 struct acpi_video_device *data =
1695                     list_entry(node, struct acpi_video_device, entry);
1696                 if (!data)
1697                         continue;
1698
1699                 status = acpi_video_bus_put_one_device(data);
1700                 if (ACPI_FAILURE(status))
1701                         printk(KERN_WARNING PREFIX
1702                                "hhuuhhuu bug in acpi video driver.\n");
1703
1704                 if (data->brightness)
1705                         kfree(data->brightness->levels);
1706                 kfree(data->brightness);
1707                 kfree(data);
1708         }
1709
1710         return 0;
1711 }
1712
1713 /* acpi_video interface */
1714
1715 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1716 {
1717         return acpi_video_bus_DOS(video, 1, 0);
1718 }
1719
1720 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1721 {
1722         return acpi_video_bus_DOS(video, 0, 1);
1723 }
1724
1725 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1726 {
1727         struct acpi_video_bus *video = data;
1728         struct acpi_device *device = NULL;
1729
1730         printk("video bus notify\n");
1731
1732         if (!video)
1733                 return;
1734
1735         device = video->device;
1736
1737         switch (event) {
1738         case ACPI_VIDEO_NOTIFY_SWITCH:  /* User request that a switch occur,
1739                                          * most likely via hotkey. */
1740                 acpi_bus_generate_event(device, event, 0);
1741                 break;
1742
1743         case ACPI_VIDEO_NOTIFY_PROBE:   /* User plug or remove a video
1744                                          * connector. */
1745                 acpi_video_device_enumerate(video);
1746                 acpi_video_device_rebind(video);
1747                 acpi_video_switch_output(video, event);
1748                 acpi_bus_generate_event(device, event, 0);
1749                 break;
1750
1751         case ACPI_VIDEO_NOTIFY_CYCLE:   /* Cycle Display output hotkey pressed. */
1752         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:     /* Next Display output hotkey pressed. */
1753         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:     /* previous Display output hotkey pressed. */
1754                 acpi_video_switch_output(video, event);
1755                 acpi_bus_generate_event(device, event, 0);
1756                 break;
1757
1758         default:
1759                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1760                                   "Unsupported event [0x%x]\n", event));
1761                 break;
1762         }
1763
1764         return;
1765 }
1766
1767 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1768 {
1769         struct acpi_video_device *video_device = data;
1770         struct acpi_device *device = NULL;
1771
1772         if (!video_device)
1773                 return;
1774
1775         device = video_device->dev;
1776
1777         switch (event) {
1778         case ACPI_VIDEO_NOTIFY_SWITCH:  /* change in status (cycle output device) */
1779         case ACPI_VIDEO_NOTIFY_PROBE:   /* change in status (output device status) */
1780                 acpi_bus_generate_event(device, event, 0);
1781                 break;
1782         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:        /* Cycle brightness */
1783         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:  /* Increase brightness */
1784         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:  /* Decrease brightness */
1785         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1786         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:     /* display device off */
1787                 acpi_video_switch_brightness(video_device, event);
1788                 acpi_bus_generate_event(device, event, 0);
1789                 break;
1790         default:
1791                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1792                                   "Unsupported event [0x%x]\n", event));
1793                 break;
1794         }
1795         return;
1796 }
1797
1798 static int acpi_video_bus_add(struct acpi_device *device)
1799 {
1800         int result = 0;
1801         acpi_status status = 0;
1802         struct acpi_video_bus *video = NULL;
1803
1804
1805         if (!device)
1806                 return -EINVAL;
1807
1808         video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1809         if (!video)
1810                 return -ENOMEM;
1811
1812         video->device = device;
1813         strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1814         strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1815         acpi_driver_data(device) = video;
1816
1817         acpi_video_bus_find_cap(video);
1818         result = acpi_video_bus_check(video);
1819         if (result)
1820                 goto end;
1821
1822         result = acpi_video_bus_add_fs(device);
1823         if (result)
1824                 goto end;
1825
1826         init_MUTEX(&video->sem);
1827         INIT_LIST_HEAD(&video->video_device_list);
1828
1829         acpi_video_bus_get_devices(video, device);
1830         acpi_video_bus_start_devices(video);
1831
1832         status = acpi_install_notify_handler(device->handle,
1833                                              ACPI_DEVICE_NOTIFY,
1834                                              acpi_video_bus_notify, video);
1835         if (ACPI_FAILURE(status)) {
1836                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1837                                   "Error installing notify handler\n"));
1838                 acpi_video_bus_stop_devices(video);
1839                 acpi_video_bus_put_devices(video);
1840                 kfree(video->attached_array);
1841                 acpi_video_bus_remove_fs(device);
1842                 result = -ENODEV;
1843                 goto end;
1844         }
1845
1846         printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
1847                ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1848                video->flags.multihead ? "yes" : "no",
1849                video->flags.rom ? "yes" : "no",
1850                video->flags.post ? "yes" : "no");
1851
1852       end:
1853         if (result)
1854                 kfree(video);
1855
1856         return result;
1857 }
1858
1859 static int acpi_video_bus_remove(struct acpi_device *device, int type)
1860 {
1861         acpi_status status = 0;
1862         struct acpi_video_bus *video = NULL;
1863
1864
1865         if (!device || !acpi_driver_data(device))
1866                 return -EINVAL;
1867
1868         video = acpi_driver_data(device);
1869
1870         acpi_video_bus_stop_devices(video);
1871
1872         status = acpi_remove_notify_handler(video->device->handle,
1873                                             ACPI_DEVICE_NOTIFY,
1874                                             acpi_video_bus_notify);
1875
1876         acpi_video_bus_put_devices(video);
1877         acpi_video_bus_remove_fs(device);
1878
1879         kfree(video->attached_array);
1880         kfree(video);
1881
1882         return 0;
1883 }
1884
1885 static int __init acpi_video_init(void)
1886 {
1887         int result = 0;
1888
1889
1890         /*
1891            acpi_dbg_level = 0xFFFFFFFF;
1892            acpi_dbg_layer = 0x08000000;
1893          */
1894
1895         acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
1896         if (!acpi_video_dir)
1897                 return -ENODEV;
1898         acpi_video_dir->owner = THIS_MODULE;
1899
1900         result = acpi_bus_register_driver(&acpi_video_bus);
1901         if (result < 0) {
1902                 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
1903                 return -ENODEV;
1904         }
1905
1906         return 0;
1907 }
1908
1909 static void __exit acpi_video_exit(void)
1910 {
1911
1912         acpi_bus_unregister_driver(&acpi_video_bus);
1913
1914         remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
1915
1916         return;
1917 }
1918
1919 module_init(acpi_video_init);
1920 module_exit(acpi_video_exit);