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