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