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