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