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