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