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