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