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