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