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