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