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