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