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