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