ACPI video: if no ACPI backlight support, use vendor drivers
[linux-kbuild.git] / drivers / acpi / video.c
blob2097c399dd064a47f81ccc317fe6e9b4e6ac0bfe
1 /*
2 * video.c - ACPI Video Driver ($Revision:$)
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/mutex.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/input.h>
36 #include <linux/backlight.h>
37 #include <linux/thermal.h>
38 #include <linux/video_output.h>
39 #include <asm/uaccess.h>
41 #include <acpi/acpi_bus.h>
42 #include <acpi/acpi_drivers.h>
44 #define ACPI_VIDEO_COMPONENT 0x08000000
45 #define ACPI_VIDEO_CLASS "video"
46 #define ACPI_VIDEO_BUS_NAME "Video Bus"
47 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
48 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
49 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
50 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
51 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
52 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
54 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
55 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
56 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
57 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
58 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
60 #define MAX_NAME_LEN 20
62 #define ACPI_VIDEO_DISPLAY_CRT 1
63 #define ACPI_VIDEO_DISPLAY_TV 2
64 #define ACPI_VIDEO_DISPLAY_DVI 3
65 #define ACPI_VIDEO_DISPLAY_LCD 4
67 #define _COMPONENT ACPI_VIDEO_COMPONENT
68 ACPI_MODULE_NAME("video");
70 MODULE_AUTHOR("Bruno Ducrot");
71 MODULE_DESCRIPTION("ACPI Video Driver");
72 MODULE_LICENSE("GPL");
74 static int brightness_switch_enabled = 1;
75 module_param(brightness_switch_enabled, bool, 0644);
77 static int acpi_video_bus_add(struct acpi_device *device);
78 static int acpi_video_bus_remove(struct acpi_device *device, int type);
79 static int acpi_video_resume(struct acpi_device *device);
81 static const struct acpi_device_id video_device_ids[] = {
82 {ACPI_VIDEO_HID, 0},
83 {"", 0},
85 MODULE_DEVICE_TABLE(acpi, video_device_ids);
87 static struct acpi_driver acpi_video_bus = {
88 .name = "video",
89 .class = ACPI_VIDEO_CLASS,
90 .ids = video_device_ids,
91 .ops = {
92 .add = acpi_video_bus_add,
93 .remove = acpi_video_bus_remove,
94 .resume = acpi_video_resume,
98 struct acpi_video_bus_flags {
99 u8 multihead:1; /* can switch video heads */
100 u8 rom:1; /* can retrieve a video rom */
101 u8 post:1; /* can configure the head to */
102 u8 reserved:5;
105 struct acpi_video_bus_cap {
106 u8 _DOS:1; /*Enable/Disable output switching */
107 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
108 u8 _ROM:1; /*Get ROM Data */
109 u8 _GPD:1; /*Get POST Device */
110 u8 _SPD:1; /*Set POST Device */
111 u8 _VPO:1; /*Video POST Options */
112 u8 reserved:2;
115 struct acpi_video_device_attrib {
116 u32 display_index:4; /* A zero-based instance of the Display */
117 u32 display_port_attachment:4; /*This field differentiates the display type */
118 u32 display_type:4; /*Describe the specific type in use */
119 u32 vendor_specific:4; /*Chipset Vendor Specific */
120 u32 bios_can_detect:1; /*BIOS can detect the device */
121 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
122 the VGA device. */
123 u32 pipe_id:3; /*For VGA multiple-head devices. */
124 u32 reserved:10; /*Must be 0 */
125 u32 device_id_scheme:1; /*Device ID Scheme */
128 struct acpi_video_enumerated_device {
129 union {
130 u32 int_val;
131 struct acpi_video_device_attrib attrib;
132 } value;
133 struct acpi_video_device *bind_info;
136 struct acpi_video_bus {
137 struct acpi_device *device;
138 u8 dos_setting;
139 struct acpi_video_enumerated_device *attached_array;
140 u8 attached_count;
141 struct acpi_video_bus_cap cap;
142 struct acpi_video_bus_flags flags;
143 struct list_head video_device_list;
144 struct mutex device_list_lock; /* protects video_device_list */
145 struct proc_dir_entry *dir;
146 struct input_dev *input;
147 char phys[32]; /* for input device */
150 struct acpi_video_device_flags {
151 u8 crt:1;
152 u8 lcd:1;
153 u8 tvout:1;
154 u8 dvi:1;
155 u8 bios:1;
156 u8 unknown:1;
157 u8 reserved:2;
160 struct acpi_video_device_cap {
161 u8 _ADR:1; /*Return the unique ID */
162 u8 _BCL:1; /*Query list of brightness control levels supported */
163 u8 _BCM:1; /*Set the brightness level */
164 u8 _BQC:1; /* Get current brightness level */
165 u8 _DDC:1; /*Return the EDID for this device */
166 u8 _DCS:1; /*Return status of output device */
167 u8 _DGS:1; /*Query graphics state */
168 u8 _DSS:1; /*Device state set */
171 struct acpi_video_device_brightness {
172 int curr;
173 int count;
174 int *levels;
177 struct acpi_video_device {
178 unsigned long device_id;
179 struct acpi_video_device_flags flags;
180 struct acpi_video_device_cap cap;
181 struct list_head entry;
182 struct acpi_video_bus *video;
183 struct acpi_device *dev;
184 struct acpi_video_device_brightness *brightness;
185 struct backlight_device *backlight;
186 struct thermal_cooling_device *cdev;
187 struct output_device *output_dev;
190 /* bus */
191 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
192 static struct file_operations acpi_video_bus_info_fops = {
193 .owner = THIS_MODULE,
194 .open = acpi_video_bus_info_open_fs,
195 .read = seq_read,
196 .llseek = seq_lseek,
197 .release = single_release,
200 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
201 static struct file_operations acpi_video_bus_ROM_fops = {
202 .owner = THIS_MODULE,
203 .open = acpi_video_bus_ROM_open_fs,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = single_release,
209 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
210 struct file *file);
211 static struct file_operations acpi_video_bus_POST_info_fops = {
212 .owner = THIS_MODULE,
213 .open = acpi_video_bus_POST_info_open_fs,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = single_release,
219 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
220 static struct file_operations acpi_video_bus_POST_fops = {
221 .owner = THIS_MODULE,
222 .open = acpi_video_bus_POST_open_fs,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
228 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
229 static struct file_operations acpi_video_bus_DOS_fops = {
230 .owner = THIS_MODULE,
231 .open = acpi_video_bus_DOS_open_fs,
232 .read = seq_read,
233 .llseek = seq_lseek,
234 .release = single_release,
237 /* device */
238 static int acpi_video_device_info_open_fs(struct inode *inode,
239 struct file *file);
240 static struct file_operations acpi_video_device_info_fops = {
241 .owner = THIS_MODULE,
242 .open = acpi_video_device_info_open_fs,
243 .read = seq_read,
244 .llseek = seq_lseek,
245 .release = single_release,
248 static int acpi_video_device_state_open_fs(struct inode *inode,
249 struct file *file);
250 static struct file_operations acpi_video_device_state_fops = {
251 .owner = THIS_MODULE,
252 .open = acpi_video_device_state_open_fs,
253 .read = seq_read,
254 .llseek = seq_lseek,
255 .release = single_release,
258 static int acpi_video_device_brightness_open_fs(struct inode *inode,
259 struct file *file);
260 static struct file_operations acpi_video_device_brightness_fops = {
261 .owner = THIS_MODULE,
262 .open = acpi_video_device_brightness_open_fs,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = single_release,
268 static int acpi_video_device_EDID_open_fs(struct inode *inode,
269 struct file *file);
270 static struct file_operations acpi_video_device_EDID_fops = {
271 .owner = THIS_MODULE,
272 .open = acpi_video_device_EDID_open_fs,
273 .read = seq_read,
274 .llseek = seq_lseek,
275 .release = single_release,
278 static char device_decode[][30] = {
279 "motherboard VGA device",
280 "PCI VGA device",
281 "AGP VGA device",
282 "UNKNOWN",
285 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
286 static void acpi_video_device_rebind(struct acpi_video_bus *video);
287 static void acpi_video_device_bind(struct acpi_video_bus *video,
288 struct acpi_video_device *device);
289 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
290 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
291 int level);
292 static int acpi_video_device_lcd_get_level_current(
293 struct acpi_video_device *device,
294 unsigned long long *level);
295 static int acpi_video_get_next_level(struct acpi_video_device *device,
296 u32 level_current, u32 event);
297 static void acpi_video_switch_brightness(struct acpi_video_device *device,
298 int event);
299 static int acpi_video_device_get_state(struct acpi_video_device *device,
300 unsigned long long *state);
301 static int acpi_video_output_get(struct output_device *od);
302 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
304 /*backlight device sysfs support*/
305 static int acpi_video_get_brightness(struct backlight_device *bd)
307 unsigned long long cur_level;
308 int i;
309 struct acpi_video_device *vd =
310 (struct acpi_video_device *)bl_get_data(bd);
311 acpi_video_device_lcd_get_level_current(vd, &cur_level);
312 for (i = 2; i < vd->brightness->count; i++) {
313 if (vd->brightness->levels[i] == cur_level)
314 /* The first two entries are special - see page 575
315 of the ACPI spec 3.0 */
316 return i-2;
318 return 0;
321 static int acpi_video_set_brightness(struct backlight_device *bd)
323 int request_level = bd->props.brightness+2;
324 struct acpi_video_device *vd =
325 (struct acpi_video_device *)bl_get_data(bd);
326 acpi_video_device_lcd_set_level(vd,
327 vd->brightness->levels[request_level]);
328 return 0;
331 static struct backlight_ops acpi_backlight_ops = {
332 .get_brightness = acpi_video_get_brightness,
333 .update_status = acpi_video_set_brightness,
336 /*video output device sysfs support*/
337 static int acpi_video_output_get(struct output_device *od)
339 unsigned long long state;
340 struct acpi_video_device *vd =
341 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
342 acpi_video_device_get_state(vd, &state);
343 return (int)state;
346 static int acpi_video_output_set(struct output_device *od)
348 unsigned long state = od->request_state;
349 struct acpi_video_device *vd=
350 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
351 return acpi_video_device_set_state(vd, state);
354 static struct output_properties acpi_output_properties = {
355 .set_state = acpi_video_output_set,
356 .get_status = acpi_video_output_get,
360 /* thermal cooling device callbacks */
361 static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
363 struct acpi_device *device = cdev->devdata;
364 struct acpi_video_device *video = acpi_driver_data(device);
366 return sprintf(buf, "%d\n", video->brightness->count - 3);
369 static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
371 struct acpi_device *device = cdev->devdata;
372 struct acpi_video_device *video = acpi_driver_data(device);
373 unsigned long long level;
374 int state;
376 acpi_video_device_lcd_get_level_current(video, &level);
377 for (state = 2; state < video->brightness->count; state++)
378 if (level == video->brightness->levels[state])
379 return sprintf(buf, "%d\n",
380 video->brightness->count - state - 1);
382 return -EINVAL;
385 static int
386 video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
388 struct acpi_device *device = cdev->devdata;
389 struct acpi_video_device *video = acpi_driver_data(device);
390 int level;
392 if ( state >= video->brightness->count - 2)
393 return -EINVAL;
395 state = video->brightness->count - state;
396 level = video->brightness->levels[state -1];
397 return acpi_video_device_lcd_set_level(video, level);
400 static struct thermal_cooling_device_ops video_cooling_ops = {
401 .get_max_state = video_get_max_state,
402 .get_cur_state = video_get_cur_state,
403 .set_cur_state = video_set_cur_state,
406 /* --------------------------------------------------------------------------
407 Video Management
408 -------------------------------------------------------------------------- */
410 /* device */
412 static int
413 acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
415 int status;
417 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
419 return status;
422 static int
423 acpi_video_device_get_state(struct acpi_video_device *device,
424 unsigned long long *state)
426 int status;
428 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
430 return status;
433 static int
434 acpi_video_device_set_state(struct acpi_video_device *device, int state)
436 int status;
437 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
438 struct acpi_object_list args = { 1, &arg0 };
439 unsigned long long ret;
442 arg0.integer.value = state;
443 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
445 return status;
448 static int
449 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
450 union acpi_object **levels)
452 int status;
453 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
454 union acpi_object *obj;
457 *levels = NULL;
459 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
460 if (!ACPI_SUCCESS(status))
461 return status;
462 obj = (union acpi_object *)buffer.pointer;
463 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
464 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
465 status = -EFAULT;
466 goto err;
469 *levels = obj;
471 return 0;
473 err:
474 kfree(buffer.pointer);
476 return status;
479 static int
480 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
482 int status = AE_OK;
483 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
484 struct acpi_object_list args = { 1, &arg0 };
487 arg0.integer.value = level;
489 if (device->cap._BCM)
490 status = acpi_evaluate_object(device->dev->handle, "_BCM",
491 &args, NULL);
492 device->brightness->curr = level;
493 return status;
496 static int
497 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
498 unsigned long long *level)
500 if (device->cap._BQC)
501 return acpi_evaluate_integer(device->dev->handle, "_BQC", NULL,
502 level);
503 *level = device->brightness->curr;
504 return AE_OK;
507 static int
508 acpi_video_device_EDID(struct acpi_video_device *device,
509 union acpi_object **edid, ssize_t length)
511 int status;
512 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
513 union acpi_object *obj;
514 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
515 struct acpi_object_list args = { 1, &arg0 };
518 *edid = NULL;
520 if (!device)
521 return -ENODEV;
522 if (length == 128)
523 arg0.integer.value = 1;
524 else if (length == 256)
525 arg0.integer.value = 2;
526 else
527 return -EINVAL;
529 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
530 if (ACPI_FAILURE(status))
531 return -ENODEV;
533 obj = buffer.pointer;
535 if (obj && obj->type == ACPI_TYPE_BUFFER)
536 *edid = obj;
537 else {
538 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
539 status = -EFAULT;
540 kfree(obj);
543 return status;
546 /* bus */
548 static int
549 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
551 int status;
552 unsigned long long tmp;
553 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
554 struct acpi_object_list args = { 1, &arg0 };
557 arg0.integer.value = option;
559 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
560 if (ACPI_SUCCESS(status))
561 status = tmp ? (-EINVAL) : (AE_OK);
563 return status;
566 static int
567 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
569 int status;
571 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
573 return status;
576 static int
577 acpi_video_bus_POST_options(struct acpi_video_bus *video,
578 unsigned long long *options)
580 int status;
582 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
583 *options &= 3;
585 return status;
589 * Arg:
590 * video : video bus device pointer
591 * bios_flag :
592 * 0. The system BIOS should NOT automatically switch(toggle)
593 * the active display output.
594 * 1. The system BIOS should automatically switch (toggle) the
595 * active display output. No switch event.
596 * 2. The _DGS value should be locked.
597 * 3. The system BIOS should not automatically switch (toggle) the
598 * active display output, but instead generate the display switch
599 * event notify code.
600 * lcd_flag :
601 * 0. The system BIOS should automatically control the brightness level
602 * of the LCD when the power changes from AC to DC
603 * 1. The system BIOS should NOT automatically control the brightness
604 * level of the LCD when the power changes from AC to DC.
605 * Return Value:
606 * -1 wrong arg.
609 static int
610 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
612 acpi_integer status = 0;
613 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
614 struct acpi_object_list args = { 1, &arg0 };
617 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
618 status = -1;
619 goto Failed;
621 arg0.integer.value = (lcd_flag << 2) | bios_flag;
622 video->dos_setting = arg0.integer.value;
623 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
625 Failed:
626 return status;
630 * Arg:
631 * device : video output device (LCD, CRT, ..)
633 * Return Value:
634 * Maximum brightness level
636 * Allocate and initialize device->brightness.
639 static int
640 acpi_video_init_brightness(struct acpi_video_device *device)
642 union acpi_object *obj = NULL;
643 int i, max_level = 0, count = 0;
644 union acpi_object *o;
645 struct acpi_video_device_brightness *br = NULL;
647 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
648 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
649 "LCD brightness level\n"));
650 goto out;
653 if (obj->package.count < 2)
654 goto out;
656 br = kzalloc(sizeof(*br), GFP_KERNEL);
657 if (!br) {
658 printk(KERN_ERR "can't allocate memory\n");
659 goto out;
662 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
663 GFP_KERNEL);
664 if (!br->levels)
665 goto out_free;
667 for (i = 0; i < obj->package.count; i++) {
668 o = (union acpi_object *)&obj->package.elements[i];
669 if (o->type != ACPI_TYPE_INTEGER) {
670 printk(KERN_ERR PREFIX "Invalid data\n");
671 continue;
673 br->levels[count] = (u32) o->integer.value;
675 if (br->levels[count] > max_level)
676 max_level = br->levels[count];
677 count++;
680 if (count < 2)
681 goto out_free_levels;
683 br->count = count;
684 device->brightness = br;
685 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
686 kfree(obj);
687 return max_level;
689 out_free_levels:
690 kfree(br->levels);
691 out_free:
692 kfree(br);
693 out:
694 device->brightness = NULL;
695 kfree(obj);
696 return 0;
700 * Arg:
701 * device : video output device (LCD, CRT, ..)
703 * Return Value:
704 * None
706 * Find out all required AML methods defined under the output
707 * device.
710 static void acpi_video_device_find_cap(struct acpi_video_device *device)
712 acpi_handle h_dummy1;
713 u32 max_level = 0;
716 memset(&device->cap, 0, sizeof(device->cap));
718 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
719 device->cap._ADR = 1;
721 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
722 device->cap._BCL = 1;
724 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
725 device->cap._BCM = 1;
727 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
728 device->cap._BQC = 1;
729 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
730 device->cap._DDC = 1;
732 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
733 device->cap._DCS = 1;
735 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
736 device->cap._DGS = 1;
738 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
739 device->cap._DSS = 1;
742 if (acpi_video_backlight_support())
743 max_level = acpi_video_init_brightness(device);
745 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
746 int result;
747 static int count = 0;
748 char *name;
749 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
750 if (!name)
751 return;
753 sprintf(name, "acpi_video%d", count++);
754 device->backlight = backlight_device_register(name,
755 NULL, device, &acpi_backlight_ops);
756 device->backlight->props.max_brightness = device->brightness->count-3;
758 * If there exists the _BQC object, the _BQC object will be
759 * called to get the current backlight brightness. Otherwise
760 * the brightness will be set to the maximum.
762 if (device->cap._BQC)
763 device->backlight->props.brightness =
764 acpi_video_get_brightness(device->backlight);
765 else
766 device->backlight->props.brightness =
767 device->backlight->props.max_brightness;
768 backlight_update_status(device->backlight);
769 kfree(name);
771 device->cdev = thermal_cooling_device_register("LCD",
772 device->dev, &video_cooling_ops);
773 if (IS_ERR(device->cdev))
774 return;
776 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
777 device->cdev->id);
778 result = sysfs_create_link(&device->dev->dev.kobj,
779 &device->cdev->device.kobj,
780 "thermal_cooling");
781 if (result)
782 printk(KERN_ERR PREFIX "Create sysfs link\n");
783 result = sysfs_create_link(&device->cdev->device.kobj,
784 &device->dev->dev.kobj, "device");
785 if (result)
786 printk(KERN_ERR PREFIX "Create sysfs link\n");
790 if (acpi_video_display_switch_support()) {
792 if (device->cap._DCS && device->cap._DSS) {
793 static int count;
794 char *name;
795 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
796 if (!name)
797 return;
798 sprintf(name, "acpi_video%d", count++);
799 device->output_dev = video_output_register(name,
800 NULL, device, &acpi_output_properties);
801 kfree(name);
807 * Arg:
808 * device : video output device (VGA)
810 * Return Value:
811 * None
813 * Find out all required AML methods defined under the video bus device.
816 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
818 acpi_handle h_dummy1;
820 memset(&video->cap, 0, sizeof(video->cap));
821 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
822 video->cap._DOS = 1;
824 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
825 video->cap._DOD = 1;
827 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
828 video->cap._ROM = 1;
830 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
831 video->cap._GPD = 1;
833 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
834 video->cap._SPD = 1;
836 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
837 video->cap._VPO = 1;
842 * Check whether the video bus device has required AML method to
843 * support the desired features
846 static int acpi_video_bus_check(struct acpi_video_bus *video)
848 acpi_status status = -ENOENT;
849 struct device *dev;
851 if (!video)
852 return -EINVAL;
854 dev = acpi_get_physical_pci_device(video->device->handle);
855 if (!dev)
856 return -ENODEV;
857 put_device(dev);
859 /* Since there is no HID, CID and so on for VGA driver, we have
860 * to check well known required nodes.
863 /* Does this device support video switching? */
864 if (video->cap._DOS) {
865 video->flags.multihead = 1;
866 status = 0;
869 /* Does this device support retrieving a video ROM? */
870 if (video->cap._ROM) {
871 video->flags.rom = 1;
872 status = 0;
875 /* Does this device support configuring which video device to POST? */
876 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
877 video->flags.post = 1;
878 status = 0;
881 return status;
884 /* --------------------------------------------------------------------------
885 FS Interface (/proc)
886 -------------------------------------------------------------------------- */
888 static struct proc_dir_entry *acpi_video_dir;
890 /* video devices */
892 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
894 struct acpi_video_device *dev = seq->private;
897 if (!dev)
898 goto end;
900 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
901 seq_printf(seq, "type: ");
902 if (dev->flags.crt)
903 seq_printf(seq, "CRT\n");
904 else if (dev->flags.lcd)
905 seq_printf(seq, "LCD\n");
906 else if (dev->flags.tvout)
907 seq_printf(seq, "TVOUT\n");
908 else if (dev->flags.dvi)
909 seq_printf(seq, "DVI\n");
910 else
911 seq_printf(seq, "UNKNOWN\n");
913 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
915 end:
916 return 0;
919 static int
920 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
922 return single_open(file, acpi_video_device_info_seq_show,
923 PDE(inode)->data);
926 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
928 int status;
929 struct acpi_video_device *dev = seq->private;
930 unsigned long long state;
933 if (!dev)
934 goto end;
936 status = acpi_video_device_get_state(dev, &state);
937 seq_printf(seq, "state: ");
938 if (ACPI_SUCCESS(status))
939 seq_printf(seq, "0x%02llx\n", state);
940 else
941 seq_printf(seq, "<not supported>\n");
943 status = acpi_video_device_query(dev, &state);
944 seq_printf(seq, "query: ");
945 if (ACPI_SUCCESS(status))
946 seq_printf(seq, "0x%02llx\n", state);
947 else
948 seq_printf(seq, "<not supported>\n");
950 end:
951 return 0;
954 static int
955 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
957 return single_open(file, acpi_video_device_state_seq_show,
958 PDE(inode)->data);
961 static ssize_t
962 acpi_video_device_write_state(struct file *file,
963 const char __user * buffer,
964 size_t count, loff_t * data)
966 int status;
967 struct seq_file *m = file->private_data;
968 struct acpi_video_device *dev = m->private;
969 char str[12] = { 0 };
970 u32 state = 0;
973 if (!dev || count + 1 > sizeof str)
974 return -EINVAL;
976 if (copy_from_user(str, buffer, count))
977 return -EFAULT;
979 str[count] = 0;
980 state = simple_strtoul(str, NULL, 0);
981 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
983 status = acpi_video_device_set_state(dev, state);
985 if (status)
986 return -EFAULT;
988 return count;
991 static int
992 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
994 struct acpi_video_device *dev = seq->private;
995 int i;
998 if (!dev || !dev->brightness) {
999 seq_printf(seq, "<not supported>\n");
1000 return 0;
1003 seq_printf(seq, "levels: ");
1004 for (i = 0; i < dev->brightness->count; i++)
1005 seq_printf(seq, " %d", dev->brightness->levels[i]);
1006 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1008 return 0;
1011 static int
1012 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1014 return single_open(file, acpi_video_device_brightness_seq_show,
1015 PDE(inode)->data);
1018 static ssize_t
1019 acpi_video_device_write_brightness(struct file *file,
1020 const char __user * buffer,
1021 size_t count, loff_t * data)
1023 struct seq_file *m = file->private_data;
1024 struct acpi_video_device *dev = m->private;
1025 char str[5] = { 0 };
1026 unsigned int level = 0;
1027 int i;
1030 if (!dev || !dev->brightness || count + 1 > sizeof str)
1031 return -EINVAL;
1033 if (copy_from_user(str, buffer, count))
1034 return -EFAULT;
1036 str[count] = 0;
1037 level = simple_strtoul(str, NULL, 0);
1039 if (level > 100)
1040 return -EFAULT;
1042 /* validate through the list of available levels */
1043 for (i = 0; i < dev->brightness->count; i++)
1044 if (level == dev->brightness->levels[i]) {
1045 if (ACPI_SUCCESS
1046 (acpi_video_device_lcd_set_level(dev, level)))
1047 dev->brightness->curr = level;
1048 break;
1051 return count;
1054 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1056 struct acpi_video_device *dev = seq->private;
1057 int status;
1058 int i;
1059 union acpi_object *edid = NULL;
1062 if (!dev)
1063 goto out;
1065 status = acpi_video_device_EDID(dev, &edid, 128);
1066 if (ACPI_FAILURE(status)) {
1067 status = acpi_video_device_EDID(dev, &edid, 256);
1070 if (ACPI_FAILURE(status)) {
1071 goto out;
1074 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1075 for (i = 0; i < edid->buffer.length; i++)
1076 seq_putc(seq, edid->buffer.pointer[i]);
1079 out:
1080 if (!edid)
1081 seq_printf(seq, "<not supported>\n");
1082 else
1083 kfree(edid);
1085 return 0;
1088 static int
1089 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1091 return single_open(file, acpi_video_device_EDID_seq_show,
1092 PDE(inode)->data);
1095 static int acpi_video_device_add_fs(struct acpi_device *device)
1097 struct proc_dir_entry *entry, *device_dir;
1098 struct acpi_video_device *vid_dev;
1100 vid_dev = acpi_driver_data(device);
1101 if (!vid_dev)
1102 return -ENODEV;
1104 device_dir = proc_mkdir(acpi_device_bid(device),
1105 vid_dev->video->dir);
1106 if (!device_dir)
1107 return -ENOMEM;
1109 device_dir->owner = THIS_MODULE;
1111 /* 'info' [R] */
1112 entry = proc_create_data("info", S_IRUGO, device_dir,
1113 &acpi_video_device_info_fops, acpi_driver_data(device));
1114 if (!entry)
1115 goto err_remove_dir;
1117 /* 'state' [R/W] */
1118 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1119 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
1120 device_dir,
1121 &acpi_video_device_state_fops,
1122 acpi_driver_data(device));
1123 if (!entry)
1124 goto err_remove_info;
1126 /* 'brightness' [R/W] */
1127 acpi_video_device_brightness_fops.write =
1128 acpi_video_device_write_brightness;
1129 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1130 device_dir,
1131 &acpi_video_device_brightness_fops,
1132 acpi_driver_data(device));
1133 if (!entry)
1134 goto err_remove_state;
1136 /* 'EDID' [R] */
1137 entry = proc_create_data("EDID", S_IRUGO, device_dir,
1138 &acpi_video_device_EDID_fops,
1139 acpi_driver_data(device));
1140 if (!entry)
1141 goto err_remove_brightness;
1143 acpi_device_dir(device) = device_dir;
1145 return 0;
1147 err_remove_brightness:
1148 remove_proc_entry("brightness", device_dir);
1149 err_remove_state:
1150 remove_proc_entry("state", device_dir);
1151 err_remove_info:
1152 remove_proc_entry("info", device_dir);
1153 err_remove_dir:
1154 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1155 return -ENOMEM;
1158 static int acpi_video_device_remove_fs(struct acpi_device *device)
1160 struct acpi_video_device *vid_dev;
1161 struct proc_dir_entry *device_dir;
1163 vid_dev = acpi_driver_data(device);
1164 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1165 return -ENODEV;
1167 device_dir = acpi_device_dir(device);
1168 if (device_dir) {
1169 remove_proc_entry("info", device_dir);
1170 remove_proc_entry("state", device_dir);
1171 remove_proc_entry("brightness", device_dir);
1172 remove_proc_entry("EDID", device_dir);
1173 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1174 acpi_device_dir(device) = NULL;
1177 return 0;
1180 /* video bus */
1181 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1183 struct acpi_video_bus *video = seq->private;
1186 if (!video)
1187 goto end;
1189 seq_printf(seq, "Switching heads: %s\n",
1190 video->flags.multihead ? "yes" : "no");
1191 seq_printf(seq, "Video ROM: %s\n",
1192 video->flags.rom ? "yes" : "no");
1193 seq_printf(seq, "Device to be POSTed on boot: %s\n",
1194 video->flags.post ? "yes" : "no");
1196 end:
1197 return 0;
1200 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1202 return single_open(file, acpi_video_bus_info_seq_show,
1203 PDE(inode)->data);
1206 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1208 struct acpi_video_bus *video = seq->private;
1211 if (!video)
1212 goto end;
1214 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1215 seq_printf(seq, "<TODO>\n");
1217 end:
1218 return 0;
1221 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1223 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1226 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1228 struct acpi_video_bus *video = seq->private;
1229 unsigned long long options;
1230 int status;
1233 if (!video)
1234 goto end;
1236 status = acpi_video_bus_POST_options(video, &options);
1237 if (ACPI_SUCCESS(status)) {
1238 if (!(options & 1)) {
1239 printk(KERN_WARNING PREFIX
1240 "The motherboard VGA device is not listed as a possible POST device.\n");
1241 printk(KERN_WARNING PREFIX
1242 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1244 printk("%llx\n", options);
1245 seq_printf(seq, "can POST: <integrated video>");
1246 if (options & 2)
1247 seq_printf(seq, " <PCI video>");
1248 if (options & 4)
1249 seq_printf(seq, " <AGP video>");
1250 seq_putc(seq, '\n');
1251 } else
1252 seq_printf(seq, "<not supported>\n");
1253 end:
1254 return 0;
1257 static int
1258 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1260 return single_open(file, acpi_video_bus_POST_info_seq_show,
1261 PDE(inode)->data);
1264 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1266 struct acpi_video_bus *video = seq->private;
1267 int status;
1268 unsigned long long id;
1271 if (!video)
1272 goto end;
1274 status = acpi_video_bus_get_POST(video, &id);
1275 if (!ACPI_SUCCESS(status)) {
1276 seq_printf(seq, "<not supported>\n");
1277 goto end;
1279 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1281 end:
1282 return 0;
1285 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1287 struct acpi_video_bus *video = seq->private;
1290 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1292 return 0;
1295 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1297 return single_open(file, acpi_video_bus_POST_seq_show,
1298 PDE(inode)->data);
1301 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1303 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1306 static ssize_t
1307 acpi_video_bus_write_POST(struct file *file,
1308 const char __user * buffer,
1309 size_t count, loff_t * data)
1311 int status;
1312 struct seq_file *m = file->private_data;
1313 struct acpi_video_bus *video = m->private;
1314 char str[12] = { 0 };
1315 unsigned long long opt, options;
1318 if (!video || count + 1 > sizeof str)
1319 return -EINVAL;
1321 status = acpi_video_bus_POST_options(video, &options);
1322 if (!ACPI_SUCCESS(status))
1323 return -EINVAL;
1325 if (copy_from_user(str, buffer, count))
1326 return -EFAULT;
1328 str[count] = 0;
1329 opt = strtoul(str, NULL, 0);
1330 if (opt > 3)
1331 return -EFAULT;
1333 /* just in case an OEM 'forgot' the motherboard... */
1334 options |= 1;
1336 if (options & (1ul << opt)) {
1337 status = acpi_video_bus_set_POST(video, opt);
1338 if (!ACPI_SUCCESS(status))
1339 return -EFAULT;
1343 return count;
1346 static ssize_t
1347 acpi_video_bus_write_DOS(struct file *file,
1348 const char __user * buffer,
1349 size_t count, loff_t * data)
1351 int status;
1352 struct seq_file *m = file->private_data;
1353 struct acpi_video_bus *video = m->private;
1354 char str[12] = { 0 };
1355 unsigned long opt;
1358 if (!video || count + 1 > sizeof str)
1359 return -EINVAL;
1361 if (copy_from_user(str, buffer, count))
1362 return -EFAULT;
1364 str[count] = 0;
1365 opt = strtoul(str, NULL, 0);
1366 if (opt > 7)
1367 return -EFAULT;
1369 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1371 if (!ACPI_SUCCESS(status))
1372 return -EFAULT;
1374 return count;
1377 static int acpi_video_bus_add_fs(struct acpi_device *device)
1379 struct acpi_video_bus *video = acpi_driver_data(device);
1380 struct proc_dir_entry *device_dir;
1381 struct proc_dir_entry *entry;
1383 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1384 if (!device_dir)
1385 return -ENOMEM;
1387 device_dir->owner = THIS_MODULE;
1389 /* 'info' [R] */
1390 entry = proc_create_data("info", S_IRUGO, device_dir,
1391 &acpi_video_bus_info_fops,
1392 acpi_driver_data(device));
1393 if (!entry)
1394 goto err_remove_dir;
1396 /* 'ROM' [R] */
1397 entry = proc_create_data("ROM", S_IRUGO, device_dir,
1398 &acpi_video_bus_ROM_fops,
1399 acpi_driver_data(device));
1400 if (!entry)
1401 goto err_remove_info;
1403 /* 'POST_info' [R] */
1404 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
1405 &acpi_video_bus_POST_info_fops,
1406 acpi_driver_data(device));
1407 if (!entry)
1408 goto err_remove_rom;
1410 /* 'POST' [R/W] */
1411 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
1412 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
1413 device_dir,
1414 &acpi_video_bus_POST_fops,
1415 acpi_driver_data(device));
1416 if (!entry)
1417 goto err_remove_post_info;
1419 /* 'DOS' [R/W] */
1420 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
1421 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
1422 device_dir,
1423 &acpi_video_bus_DOS_fops,
1424 acpi_driver_data(device));
1425 if (!entry)
1426 goto err_remove_post;
1428 video->dir = acpi_device_dir(device) = device_dir;
1429 return 0;
1431 err_remove_post:
1432 remove_proc_entry("POST", device_dir);
1433 err_remove_post_info:
1434 remove_proc_entry("POST_info", device_dir);
1435 err_remove_rom:
1436 remove_proc_entry("ROM", device_dir);
1437 err_remove_info:
1438 remove_proc_entry("info", device_dir);
1439 err_remove_dir:
1440 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1441 return -ENOMEM;
1444 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1446 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1448 if (device_dir) {
1449 remove_proc_entry("info", device_dir);
1450 remove_proc_entry("ROM", device_dir);
1451 remove_proc_entry("POST_info", device_dir);
1452 remove_proc_entry("POST", device_dir);
1453 remove_proc_entry("DOS", device_dir);
1454 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1455 acpi_device_dir(device) = NULL;
1458 return 0;
1461 /* --------------------------------------------------------------------------
1462 Driver Interface
1463 -------------------------------------------------------------------------- */
1465 /* device interface */
1466 static struct acpi_video_device_attrib*
1467 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1469 struct acpi_video_enumerated_device *ids;
1470 int i;
1472 for (i = 0; i < video->attached_count; i++) {
1473 ids = &video->attached_array[i];
1474 if ((ids->value.int_val & 0xffff) == device_id)
1475 return &ids->value.attrib;
1478 return NULL;
1481 static int
1482 acpi_video_bus_get_one_device(struct acpi_device *device,
1483 struct acpi_video_bus *video)
1485 unsigned long long device_id;
1486 int status;
1487 struct acpi_video_device *data;
1488 struct acpi_video_device_attrib* attribute;
1490 if (!device || !video)
1491 return -EINVAL;
1493 status =
1494 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1495 if (ACPI_SUCCESS(status)) {
1497 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1498 if (!data)
1499 return -ENOMEM;
1501 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1502 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1503 device->driver_data = data;
1505 data->device_id = device_id;
1506 data->video = video;
1507 data->dev = device;
1509 attribute = acpi_video_get_device_attr(video, device_id);
1511 if((attribute != NULL) && attribute->device_id_scheme) {
1512 switch (attribute->display_type) {
1513 case ACPI_VIDEO_DISPLAY_CRT:
1514 data->flags.crt = 1;
1515 break;
1516 case ACPI_VIDEO_DISPLAY_TV:
1517 data->flags.tvout = 1;
1518 break;
1519 case ACPI_VIDEO_DISPLAY_DVI:
1520 data->flags.dvi = 1;
1521 break;
1522 case ACPI_VIDEO_DISPLAY_LCD:
1523 data->flags.lcd = 1;
1524 break;
1525 default:
1526 data->flags.unknown = 1;
1527 break;
1529 if(attribute->bios_can_detect)
1530 data->flags.bios = 1;
1531 } else
1532 data->flags.unknown = 1;
1534 acpi_video_device_bind(video, data);
1535 acpi_video_device_find_cap(data);
1537 status = acpi_install_notify_handler(device->handle,
1538 ACPI_DEVICE_NOTIFY,
1539 acpi_video_device_notify,
1540 data);
1541 if (ACPI_FAILURE(status)) {
1542 printk(KERN_ERR PREFIX
1543 "Error installing notify handler\n");
1544 if(data->brightness)
1545 kfree(data->brightness->levels);
1546 kfree(data->brightness);
1547 kfree(data);
1548 return -ENODEV;
1551 mutex_lock(&video->device_list_lock);
1552 list_add_tail(&data->entry, &video->video_device_list);
1553 mutex_unlock(&video->device_list_lock);
1555 acpi_video_device_add_fs(device);
1557 return 0;
1560 return -ENOENT;
1564 * Arg:
1565 * video : video bus device
1567 * Return:
1568 * none
1570 * Enumerate the video device list of the video bus,
1571 * bind the ids with the corresponding video devices
1572 * under the video bus.
1575 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1577 struct acpi_video_device *dev;
1579 mutex_lock(&video->device_list_lock);
1581 list_for_each_entry(dev, &video->video_device_list, entry)
1582 acpi_video_device_bind(video, dev);
1584 mutex_unlock(&video->device_list_lock);
1588 * Arg:
1589 * video : video bus device
1590 * device : video output device under the video
1591 * bus
1593 * Return:
1594 * none
1596 * Bind the ids with the corresponding video devices
1597 * under the video bus.
1600 static void
1601 acpi_video_device_bind(struct acpi_video_bus *video,
1602 struct acpi_video_device *device)
1604 struct acpi_video_enumerated_device *ids;
1605 int i;
1607 for (i = 0; i < video->attached_count; i++) {
1608 ids = &video->attached_array[i];
1609 if (device->device_id == (ids->value.int_val & 0xffff)) {
1610 ids->bind_info = device;
1611 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1617 * Arg:
1618 * video : video bus device
1620 * Return:
1621 * < 0 : error
1623 * Call _DOD to enumerate all devices attached to display adapter
1627 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1629 int status;
1630 int count;
1631 int i;
1632 struct acpi_video_enumerated_device *active_list;
1633 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1634 union acpi_object *dod = NULL;
1635 union acpi_object *obj;
1637 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1638 if (!ACPI_SUCCESS(status)) {
1639 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1640 return status;
1643 dod = buffer.pointer;
1644 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1645 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1646 status = -EFAULT;
1647 goto out;
1650 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1651 dod->package.count));
1653 active_list = kcalloc(1 + dod->package.count,
1654 sizeof(struct acpi_video_enumerated_device),
1655 GFP_KERNEL);
1656 if (!active_list) {
1657 status = -ENOMEM;
1658 goto out;
1661 count = 0;
1662 for (i = 0; i < dod->package.count; i++) {
1663 obj = &dod->package.elements[i];
1665 if (obj->type != ACPI_TYPE_INTEGER) {
1666 printk(KERN_ERR PREFIX
1667 "Invalid _DOD data in element %d\n", i);
1668 continue;
1671 active_list[count].value.int_val = obj->integer.value;
1672 active_list[count].bind_info = NULL;
1673 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1674 (int)obj->integer.value));
1675 count++;
1678 kfree(video->attached_array);
1680 video->attached_array = active_list;
1681 video->attached_count = count;
1683 out:
1684 kfree(buffer.pointer);
1685 return status;
1688 static int
1689 acpi_video_get_next_level(struct acpi_video_device *device,
1690 u32 level_current, u32 event)
1692 int min, max, min_above, max_below, i, l, delta = 255;
1693 max = max_below = 0;
1694 min = min_above = 255;
1695 /* Find closest level to level_current */
1696 for (i = 0; i < device->brightness->count; i++) {
1697 l = device->brightness->levels[i];
1698 if (abs(l - level_current) < abs(delta)) {
1699 delta = l - level_current;
1700 if (!delta)
1701 break;
1704 /* Ajust level_current to closest available level */
1705 level_current += delta;
1706 for (i = 0; i < device->brightness->count; i++) {
1707 l = device->brightness->levels[i];
1708 if (l < min)
1709 min = l;
1710 if (l > max)
1711 max = l;
1712 if (l < min_above && l > level_current)
1713 min_above = l;
1714 if (l > max_below && l < level_current)
1715 max_below = l;
1718 switch (event) {
1719 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1720 return (level_current < max) ? min_above : min;
1721 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1722 return (level_current < max) ? min_above : max;
1723 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1724 return (level_current > min) ? max_below : min;
1725 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1726 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1727 return 0;
1728 default:
1729 return level_current;
1733 static void
1734 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1736 unsigned long long level_current, level_next;
1737 if (!device->brightness)
1738 return;
1739 acpi_video_device_lcd_get_level_current(device, &level_current);
1740 level_next = acpi_video_get_next_level(device, level_current, event);
1741 acpi_video_device_lcd_set_level(device, level_next);
1744 static int
1745 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1746 struct acpi_device *device)
1748 int status = 0;
1749 struct acpi_device *dev;
1751 acpi_video_device_enumerate(video);
1753 list_for_each_entry(dev, &device->children, node) {
1755 status = acpi_video_bus_get_one_device(dev, video);
1756 if (ACPI_FAILURE(status)) {
1757 printk(KERN_WARNING PREFIX
1758 "Cant attach device");
1759 continue;
1762 return status;
1765 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1767 acpi_status status;
1768 struct acpi_video_bus *video;
1771 if (!device || !device->video)
1772 return -ENOENT;
1774 video = device->video;
1776 acpi_video_device_remove_fs(device->dev);
1778 status = acpi_remove_notify_handler(device->dev->handle,
1779 ACPI_DEVICE_NOTIFY,
1780 acpi_video_device_notify);
1781 backlight_device_unregister(device->backlight);
1782 if (device->cdev) {
1783 sysfs_remove_link(&device->dev->dev.kobj,
1784 "thermal_cooling");
1785 sysfs_remove_link(&device->cdev->device.kobj,
1786 "device");
1787 thermal_cooling_device_unregister(device->cdev);
1788 device->cdev = NULL;
1790 video_output_unregister(device->output_dev);
1792 return 0;
1795 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1797 int status;
1798 struct acpi_video_device *dev, *next;
1800 mutex_lock(&video->device_list_lock);
1802 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1804 status = acpi_video_bus_put_one_device(dev);
1805 if (ACPI_FAILURE(status))
1806 printk(KERN_WARNING PREFIX
1807 "hhuuhhuu bug in acpi video driver.\n");
1809 if (dev->brightness) {
1810 kfree(dev->brightness->levels);
1811 kfree(dev->brightness);
1813 list_del(&dev->entry);
1814 kfree(dev);
1817 mutex_unlock(&video->device_list_lock);
1819 return 0;
1822 /* acpi_video interface */
1824 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1826 return acpi_video_bus_DOS(video, 0, 0);
1829 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1831 return acpi_video_bus_DOS(video, 0, 1);
1834 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1836 struct acpi_video_bus *video = data;
1837 struct acpi_device *device = NULL;
1838 struct input_dev *input;
1839 int keycode;
1841 if (!video)
1842 return;
1844 device = video->device;
1845 input = video->input;
1847 switch (event) {
1848 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1849 * most likely via hotkey. */
1850 acpi_bus_generate_proc_event(device, event, 0);
1851 keycode = KEY_SWITCHVIDEOMODE;
1852 break;
1854 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1855 * connector. */
1856 acpi_video_device_enumerate(video);
1857 acpi_video_device_rebind(video);
1858 acpi_bus_generate_proc_event(device, event, 0);
1859 keycode = KEY_SWITCHVIDEOMODE;
1860 break;
1862 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1863 acpi_bus_generate_proc_event(device, event, 0);
1864 keycode = KEY_SWITCHVIDEOMODE;
1865 break;
1866 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1867 acpi_bus_generate_proc_event(device, event, 0);
1868 keycode = KEY_VIDEO_NEXT;
1869 break;
1870 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1871 acpi_bus_generate_proc_event(device, event, 0);
1872 keycode = KEY_VIDEO_PREV;
1873 break;
1875 default:
1876 keycode = KEY_UNKNOWN;
1877 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1878 "Unsupported event [0x%x]\n", event));
1879 break;
1882 acpi_notifier_call_chain(device, event, 0);
1883 input_report_key(input, keycode, 1);
1884 input_sync(input);
1885 input_report_key(input, keycode, 0);
1886 input_sync(input);
1888 return;
1891 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1893 struct acpi_video_device *video_device = data;
1894 struct acpi_device *device = NULL;
1895 struct acpi_video_bus *bus;
1896 struct input_dev *input;
1897 int keycode;
1899 if (!video_device)
1900 return;
1902 device = video_device->dev;
1903 bus = video_device->video;
1904 input = bus->input;
1906 switch (event) {
1907 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1908 if (brightness_switch_enabled)
1909 acpi_video_switch_brightness(video_device, event);
1910 acpi_bus_generate_proc_event(device, event, 0);
1911 keycode = KEY_BRIGHTNESS_CYCLE;
1912 break;
1913 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1914 if (brightness_switch_enabled)
1915 acpi_video_switch_brightness(video_device, event);
1916 acpi_bus_generate_proc_event(device, event, 0);
1917 keycode = KEY_BRIGHTNESSUP;
1918 break;
1919 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1920 if (brightness_switch_enabled)
1921 acpi_video_switch_brightness(video_device, event);
1922 acpi_bus_generate_proc_event(device, event, 0);
1923 keycode = KEY_BRIGHTNESSDOWN;
1924 break;
1925 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1926 if (brightness_switch_enabled)
1927 acpi_video_switch_brightness(video_device, event);
1928 acpi_bus_generate_proc_event(device, event, 0);
1929 keycode = KEY_BRIGHTNESS_ZERO;
1930 break;
1931 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1932 if (brightness_switch_enabled)
1933 acpi_video_switch_brightness(video_device, event);
1934 acpi_bus_generate_proc_event(device, event, 0);
1935 keycode = KEY_DISPLAY_OFF;
1936 break;
1937 default:
1938 keycode = KEY_UNKNOWN;
1939 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1940 "Unsupported event [0x%x]\n", event));
1941 break;
1944 acpi_notifier_call_chain(device, event, 0);
1945 input_report_key(input, keycode, 1);
1946 input_sync(input);
1947 input_report_key(input, keycode, 0);
1948 input_sync(input);
1950 return;
1953 static int instance;
1954 static int acpi_video_resume(struct acpi_device *device)
1956 struct acpi_video_bus *video;
1957 struct acpi_video_device *video_device;
1958 int i;
1960 if (!device || !acpi_driver_data(device))
1961 return -EINVAL;
1963 video = acpi_driver_data(device);
1965 for (i = 0; i < video->attached_count; i++) {
1966 video_device = video->attached_array[i].bind_info;
1967 if (video_device && video_device->backlight)
1968 acpi_video_set_brightness(video_device->backlight);
1970 return AE_OK;
1973 static int acpi_video_bus_add(struct acpi_device *device)
1975 acpi_status status;
1976 struct acpi_video_bus *video;
1977 struct input_dev *input;
1978 int error;
1980 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1981 if (!video)
1982 return -ENOMEM;
1984 /* a hack to fix the duplicate name "VID" problem on T61 */
1985 if (!strcmp(device->pnp.bus_id, "VID")) {
1986 if (instance)
1987 device->pnp.bus_id[3] = '0' + instance;
1988 instance ++;
1991 video->device = device;
1992 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1993 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1994 device->driver_data = video;
1996 acpi_video_bus_find_cap(video);
1997 error = acpi_video_bus_check(video);
1998 if (error)
1999 goto err_free_video;
2001 error = acpi_video_bus_add_fs(device);
2002 if (error)
2003 goto err_free_video;
2005 mutex_init(&video->device_list_lock);
2006 INIT_LIST_HEAD(&video->video_device_list);
2008 acpi_video_bus_get_devices(video, device);
2009 acpi_video_bus_start_devices(video);
2011 status = acpi_install_notify_handler(device->handle,
2012 ACPI_DEVICE_NOTIFY,
2013 acpi_video_bus_notify, video);
2014 if (ACPI_FAILURE(status)) {
2015 printk(KERN_ERR PREFIX
2016 "Error installing notify handler\n");
2017 error = -ENODEV;
2018 goto err_stop_video;
2021 video->input = input = input_allocate_device();
2022 if (!input) {
2023 error = -ENOMEM;
2024 goto err_uninstall_notify;
2027 snprintf(video->phys, sizeof(video->phys),
2028 "%s/video/input0", acpi_device_hid(video->device));
2030 input->name = acpi_device_name(video->device);
2031 input->phys = video->phys;
2032 input->id.bustype = BUS_HOST;
2033 input->id.product = 0x06;
2034 input->dev.parent = &device->dev;
2035 input->evbit[0] = BIT(EV_KEY);
2036 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2037 set_bit(KEY_VIDEO_NEXT, input->keybit);
2038 set_bit(KEY_VIDEO_PREV, input->keybit);
2039 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2040 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2041 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2042 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2043 set_bit(KEY_DISPLAY_OFF, input->keybit);
2044 set_bit(KEY_UNKNOWN, input->keybit);
2046 error = input_register_device(input);
2047 if (error)
2048 goto err_free_input_dev;
2050 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
2051 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2052 video->flags.multihead ? "yes" : "no",
2053 video->flags.rom ? "yes" : "no",
2054 video->flags.post ? "yes" : "no");
2056 return 0;
2058 err_free_input_dev:
2059 input_free_device(input);
2060 err_uninstall_notify:
2061 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2062 acpi_video_bus_notify);
2063 err_stop_video:
2064 acpi_video_bus_stop_devices(video);
2065 acpi_video_bus_put_devices(video);
2066 kfree(video->attached_array);
2067 acpi_video_bus_remove_fs(device);
2068 err_free_video:
2069 kfree(video);
2070 device->driver_data = NULL;
2072 return error;
2075 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2077 acpi_status status = 0;
2078 struct acpi_video_bus *video = NULL;
2081 if (!device || !acpi_driver_data(device))
2082 return -EINVAL;
2084 video = acpi_driver_data(device);
2086 acpi_video_bus_stop_devices(video);
2088 status = acpi_remove_notify_handler(video->device->handle,
2089 ACPI_DEVICE_NOTIFY,
2090 acpi_video_bus_notify);
2092 acpi_video_bus_put_devices(video);
2093 acpi_video_bus_remove_fs(device);
2095 input_unregister_device(video->input);
2096 kfree(video->attached_array);
2097 kfree(video);
2099 return 0;
2102 static int __init acpi_video_init(void)
2104 int result = 0;
2108 acpi_dbg_level = 0xFFFFFFFF;
2109 acpi_dbg_layer = 0x08000000;
2112 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2113 if (!acpi_video_dir)
2114 return -ENODEV;
2115 acpi_video_dir->owner = THIS_MODULE;
2117 result = acpi_bus_register_driver(&acpi_video_bus);
2118 if (result < 0) {
2119 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2120 return -ENODEV;
2123 return 0;
2126 static void __exit acpi_video_exit(void)
2129 acpi_bus_unregister_driver(&acpi_video_bus);
2131 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2133 return;
2136 module_init(acpi_video_init);
2137 module_exit(acpi_video_exit);