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