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