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