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