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