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