]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - drivers/acpi/video.c
Merge branch 'pnp' into release
[mirror_ubuntu-focal-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 printk(KERN_INFO PREFIX
736 "%s is registered as cooling_device%d\n",
737 device->dev->dev.bus_id, device->cdev->id);
738 result = sysfs_create_link(&device->dev->dev.kobj,
739 &device->cdev->device.kobj,
740 "thermal_cooling");
741 if (result)
742 printk(KERN_ERR PREFIX "Create sysfs link\n");
743 result = sysfs_create_link(&device->cdev->device.kobj,
744 &device->dev->dev.kobj, "device");
745 if (result)
746 printk(KERN_ERR PREFIX "Create sysfs link\n");
747
748 }
749 if (device->cap._DCS && device->cap._DSS){
750 static int count = 0;
751 char *name;
752 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
753 if (!name)
754 return;
755 sprintf(name, "acpi_video%d", count++);
756 device->output_dev = video_output_register(name,
757 NULL, device, &acpi_output_properties);
758 kfree(name);
759 }
760 return;
761 }
762
763 /*
764 * Arg:
765 * device : video output device (VGA)
766 *
767 * Return Value:
768 * None
769 *
770 * Find out all required AML methods defined under the video bus device.
771 */
772
773 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
774 {
775 acpi_handle h_dummy1;
776
777 memset(&video->cap, 0, sizeof(video->cap));
778 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
779 video->cap._DOS = 1;
780 }
781 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
782 video->cap._DOD = 1;
783 }
784 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
785 video->cap._ROM = 1;
786 }
787 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
788 video->cap._GPD = 1;
789 }
790 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
791 video->cap._SPD = 1;
792 }
793 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
794 video->cap._VPO = 1;
795 }
796 }
797
798 /*
799 * Check whether the video bus device has required AML method to
800 * support the desired features
801 */
802
803 static int acpi_video_bus_check(struct acpi_video_bus *video)
804 {
805 acpi_status status = -ENOENT;
806
807
808 if (!video)
809 return -EINVAL;
810
811 /* Since there is no HID, CID and so on for VGA driver, we have
812 * to check well known required nodes.
813 */
814
815 /* Does this device support video switching? */
816 if (video->cap._DOS) {
817 video->flags.multihead = 1;
818 status = 0;
819 }
820
821 /* Does this device support retrieving a video ROM? */
822 if (video->cap._ROM) {
823 video->flags.rom = 1;
824 status = 0;
825 }
826
827 /* Does this device support configuring which video device to POST? */
828 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
829 video->flags.post = 1;
830 status = 0;
831 }
832
833 return status;
834 }
835
836 /* --------------------------------------------------------------------------
837 FS Interface (/proc)
838 -------------------------------------------------------------------------- */
839
840 static struct proc_dir_entry *acpi_video_dir;
841
842 /* video devices */
843
844 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
845 {
846 struct acpi_video_device *dev = seq->private;
847
848
849 if (!dev)
850 goto end;
851
852 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
853 seq_printf(seq, "type: ");
854 if (dev->flags.crt)
855 seq_printf(seq, "CRT\n");
856 else if (dev->flags.lcd)
857 seq_printf(seq, "LCD\n");
858 else if (dev->flags.tvout)
859 seq_printf(seq, "TVOUT\n");
860 else if (dev->flags.dvi)
861 seq_printf(seq, "DVI\n");
862 else
863 seq_printf(seq, "UNKNOWN\n");
864
865 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
866
867 end:
868 return 0;
869 }
870
871 static int
872 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
873 {
874 return single_open(file, acpi_video_device_info_seq_show,
875 PDE(inode)->data);
876 }
877
878 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
879 {
880 int status;
881 struct acpi_video_device *dev = seq->private;
882 unsigned long state;
883
884
885 if (!dev)
886 goto end;
887
888 status = acpi_video_device_get_state(dev, &state);
889 seq_printf(seq, "state: ");
890 if (ACPI_SUCCESS(status))
891 seq_printf(seq, "0x%02lx\n", state);
892 else
893 seq_printf(seq, "<not supported>\n");
894
895 status = acpi_video_device_query(dev, &state);
896 seq_printf(seq, "query: ");
897 if (ACPI_SUCCESS(status))
898 seq_printf(seq, "0x%02lx\n", state);
899 else
900 seq_printf(seq, "<not supported>\n");
901
902 end:
903 return 0;
904 }
905
906 static int
907 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
908 {
909 return single_open(file, acpi_video_device_state_seq_show,
910 PDE(inode)->data);
911 }
912
913 static ssize_t
914 acpi_video_device_write_state(struct file *file,
915 const char __user * buffer,
916 size_t count, loff_t * data)
917 {
918 int status;
919 struct seq_file *m = file->private_data;
920 struct acpi_video_device *dev = m->private;
921 char str[12] = { 0 };
922 u32 state = 0;
923
924
925 if (!dev || count + 1 > sizeof str)
926 return -EINVAL;
927
928 if (copy_from_user(str, buffer, count))
929 return -EFAULT;
930
931 str[count] = 0;
932 state = simple_strtoul(str, NULL, 0);
933 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
934
935 status = acpi_video_device_set_state(dev, state);
936
937 if (status)
938 return -EFAULT;
939
940 return count;
941 }
942
943 static int
944 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
945 {
946 struct acpi_video_device *dev = seq->private;
947 int i;
948
949
950 if (!dev || !dev->brightness) {
951 seq_printf(seq, "<not supported>\n");
952 return 0;
953 }
954
955 seq_printf(seq, "levels: ");
956 for (i = 0; i < dev->brightness->count; i++)
957 seq_printf(seq, " %d", dev->brightness->levels[i]);
958 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
959
960 return 0;
961 }
962
963 static int
964 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
965 {
966 return single_open(file, acpi_video_device_brightness_seq_show,
967 PDE(inode)->data);
968 }
969
970 static ssize_t
971 acpi_video_device_write_brightness(struct file *file,
972 const char __user * buffer,
973 size_t count, loff_t * data)
974 {
975 struct seq_file *m = file->private_data;
976 struct acpi_video_device *dev = m->private;
977 char str[5] = { 0 };
978 unsigned int level = 0;
979 int i;
980
981
982 if (!dev || !dev->brightness || count + 1 > sizeof str)
983 return -EINVAL;
984
985 if (copy_from_user(str, buffer, count))
986 return -EFAULT;
987
988 str[count] = 0;
989 level = simple_strtoul(str, NULL, 0);
990
991 if (level > 100)
992 return -EFAULT;
993
994 /* validate through the list of available levels */
995 for (i = 0; i < dev->brightness->count; i++)
996 if (level == dev->brightness->levels[i]) {
997 if (ACPI_SUCCESS
998 (acpi_video_device_lcd_set_level(dev, level)))
999 dev->brightness->curr = level;
1000 break;
1001 }
1002
1003 return count;
1004 }
1005
1006 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1007 {
1008 struct acpi_video_device *dev = seq->private;
1009 int status;
1010 int i;
1011 union acpi_object *edid = NULL;
1012
1013
1014 if (!dev)
1015 goto out;
1016
1017 status = acpi_video_device_EDID(dev, &edid, 128);
1018 if (ACPI_FAILURE(status)) {
1019 status = acpi_video_device_EDID(dev, &edid, 256);
1020 }
1021
1022 if (ACPI_FAILURE(status)) {
1023 goto out;
1024 }
1025
1026 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1027 for (i = 0; i < edid->buffer.length; i++)
1028 seq_putc(seq, edid->buffer.pointer[i]);
1029 }
1030
1031 out:
1032 if (!edid)
1033 seq_printf(seq, "<not supported>\n");
1034 else
1035 kfree(edid);
1036
1037 return 0;
1038 }
1039
1040 static int
1041 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1042 {
1043 return single_open(file, acpi_video_device_EDID_seq_show,
1044 PDE(inode)->data);
1045 }
1046
1047 static int acpi_video_device_add_fs(struct acpi_device *device)
1048 {
1049 struct proc_dir_entry *entry, *device_dir;
1050 struct acpi_video_device *vid_dev;
1051
1052 vid_dev = acpi_driver_data(device);
1053 if (!vid_dev)
1054 return -ENODEV;
1055
1056 device_dir = proc_mkdir(acpi_device_bid(device),
1057 vid_dev->video->dir);
1058 if (!device_dir)
1059 return -ENOMEM;
1060
1061 device_dir->owner = THIS_MODULE;
1062
1063 /* 'info' [R] */
1064 entry = create_proc_entry("info", S_IRUGO, device_dir);
1065 if (!entry)
1066 goto err_remove_dir;
1067
1068 entry->proc_fops = &acpi_video_device_info_fops;
1069 entry->data = acpi_driver_data(device);
1070 entry->owner = THIS_MODULE;
1071
1072 /* 'state' [R/W] */
1073 entry = create_proc_entry("state", S_IFREG | S_IRUGO | S_IWUSR,
1074 device_dir);
1075 if (!entry)
1076 goto err_remove_info;
1077
1078 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1079 entry->proc_fops = &acpi_video_device_state_fops;
1080 entry->data = acpi_driver_data(device);
1081 entry->owner = THIS_MODULE;
1082
1083 /* 'brightness' [R/W] */
1084 entry = create_proc_entry("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1085 device_dir);
1086 if (!entry)
1087 goto err_remove_state;
1088
1089 acpi_video_device_brightness_fops.write =
1090 acpi_video_device_write_brightness;
1091 entry->proc_fops = &acpi_video_device_brightness_fops;
1092 entry->data = acpi_driver_data(device);
1093 entry->owner = THIS_MODULE;
1094
1095 /* 'EDID' [R] */
1096 entry = create_proc_entry("EDID", S_IRUGO, device_dir);
1097 if (!entry)
1098 goto err_remove_brightness;
1099
1100 entry->proc_fops = &acpi_video_device_EDID_fops;
1101 entry->data = acpi_driver_data(device);
1102 entry->owner = THIS_MODULE;
1103
1104 acpi_device_dir(device) = device_dir;
1105 return 0;
1106
1107 err_remove_brightness:
1108 remove_proc_entry("brightness", device_dir);
1109 err_remove_state:
1110 remove_proc_entry("state", device_dir);
1111 err_remove_info:
1112 remove_proc_entry("info", device_dir);
1113 err_remove_dir:
1114 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1115 return -ENOMEM;
1116 }
1117
1118 static int acpi_video_device_remove_fs(struct acpi_device *device)
1119 {
1120 struct acpi_video_device *vid_dev;
1121 struct proc_dir_entry *device_dir;
1122
1123 vid_dev = acpi_driver_data(device);
1124 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1125 return -ENODEV;
1126
1127 device_dir = acpi_device_dir(device);
1128 if (device_dir) {
1129 remove_proc_entry("info", device_dir);
1130 remove_proc_entry("state", device_dir);
1131 remove_proc_entry("brightness", device_dir);
1132 remove_proc_entry("EDID", device_dir);
1133 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1134 acpi_device_dir(device) = NULL;
1135 }
1136
1137 return 0;
1138 }
1139
1140 /* video bus */
1141 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1142 {
1143 struct acpi_video_bus *video = seq->private;
1144
1145
1146 if (!video)
1147 goto end;
1148
1149 seq_printf(seq, "Switching heads: %s\n",
1150 video->flags.multihead ? "yes" : "no");
1151 seq_printf(seq, "Video ROM: %s\n",
1152 video->flags.rom ? "yes" : "no");
1153 seq_printf(seq, "Device to be POSTed on boot: %s\n",
1154 video->flags.post ? "yes" : "no");
1155
1156 end:
1157 return 0;
1158 }
1159
1160 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1161 {
1162 return single_open(file, acpi_video_bus_info_seq_show,
1163 PDE(inode)->data);
1164 }
1165
1166 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1167 {
1168 struct acpi_video_bus *video = seq->private;
1169
1170
1171 if (!video)
1172 goto end;
1173
1174 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1175 seq_printf(seq, "<TODO>\n");
1176
1177 end:
1178 return 0;
1179 }
1180
1181 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1182 {
1183 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1184 }
1185
1186 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1187 {
1188 struct acpi_video_bus *video = seq->private;
1189 unsigned long options;
1190 int status;
1191
1192
1193 if (!video)
1194 goto end;
1195
1196 status = acpi_video_bus_POST_options(video, &options);
1197 if (ACPI_SUCCESS(status)) {
1198 if (!(options & 1)) {
1199 printk(KERN_WARNING PREFIX
1200 "The motherboard VGA device is not listed as a possible POST device.\n");
1201 printk(KERN_WARNING PREFIX
1202 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1203 }
1204 printk("%lx\n", options);
1205 seq_printf(seq, "can POST: <integrated video>");
1206 if (options & 2)
1207 seq_printf(seq, " <PCI video>");
1208 if (options & 4)
1209 seq_printf(seq, " <AGP video>");
1210 seq_putc(seq, '\n');
1211 } else
1212 seq_printf(seq, "<not supported>\n");
1213 end:
1214 return 0;
1215 }
1216
1217 static int
1218 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1219 {
1220 return single_open(file, acpi_video_bus_POST_info_seq_show,
1221 PDE(inode)->data);
1222 }
1223
1224 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1225 {
1226 struct acpi_video_bus *video = seq->private;
1227 int status;
1228 unsigned long id;
1229
1230
1231 if (!video)
1232 goto end;
1233
1234 status = acpi_video_bus_get_POST(video, &id);
1235 if (!ACPI_SUCCESS(status)) {
1236 seq_printf(seq, "<not supported>\n");
1237 goto end;
1238 }
1239 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1240
1241 end:
1242 return 0;
1243 }
1244
1245 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1246 {
1247 struct acpi_video_bus *video = seq->private;
1248
1249
1250 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1251
1252 return 0;
1253 }
1254
1255 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1256 {
1257 return single_open(file, acpi_video_bus_POST_seq_show,
1258 PDE(inode)->data);
1259 }
1260
1261 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1262 {
1263 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1264 }
1265
1266 static ssize_t
1267 acpi_video_bus_write_POST(struct file *file,
1268 const char __user * buffer,
1269 size_t count, loff_t * data)
1270 {
1271 int status;
1272 struct seq_file *m = file->private_data;
1273 struct acpi_video_bus *video = m->private;
1274 char str[12] = { 0 };
1275 unsigned long opt, options;
1276
1277
1278 if (!video || count + 1 > sizeof str)
1279 return -EINVAL;
1280
1281 status = acpi_video_bus_POST_options(video, &options);
1282 if (!ACPI_SUCCESS(status))
1283 return -EINVAL;
1284
1285 if (copy_from_user(str, buffer, count))
1286 return -EFAULT;
1287
1288 str[count] = 0;
1289 opt = strtoul(str, NULL, 0);
1290 if (opt > 3)
1291 return -EFAULT;
1292
1293 /* just in case an OEM 'forgot' the motherboard... */
1294 options |= 1;
1295
1296 if (options & (1ul << opt)) {
1297 status = acpi_video_bus_set_POST(video, opt);
1298 if (!ACPI_SUCCESS(status))
1299 return -EFAULT;
1300
1301 }
1302
1303 return count;
1304 }
1305
1306 static ssize_t
1307 acpi_video_bus_write_DOS(struct file *file,
1308 const char __user * buffer,
1309 size_t count, loff_t * data)
1310 {
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 opt;
1316
1317
1318 if (!video || count + 1 > sizeof str)
1319 return -EINVAL;
1320
1321 if (copy_from_user(str, buffer, count))
1322 return -EFAULT;
1323
1324 str[count] = 0;
1325 opt = strtoul(str, NULL, 0);
1326 if (opt > 7)
1327 return -EFAULT;
1328
1329 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1330
1331 if (!ACPI_SUCCESS(status))
1332 return -EFAULT;
1333
1334 return count;
1335 }
1336
1337 static int acpi_video_bus_add_fs(struct acpi_device *device)
1338 {
1339 struct acpi_video_bus *video = acpi_driver_data(device);
1340 struct proc_dir_entry *device_dir;
1341 struct proc_dir_entry *entry;
1342
1343 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1344 if (!device_dir)
1345 return -ENOMEM;
1346
1347 device_dir->owner = THIS_MODULE;
1348
1349 /* 'info' [R] */
1350 entry = create_proc_entry("info", S_IRUGO, device_dir);
1351 if (!entry)
1352 goto err_remove_dir;
1353
1354 entry->proc_fops = &acpi_video_bus_info_fops;
1355 entry->data = acpi_driver_data(device);
1356 entry->owner = THIS_MODULE;
1357
1358 /* 'ROM' [R] */
1359 entry = create_proc_entry("ROM", S_IRUGO, device_dir);
1360 if (!entry)
1361 goto err_remove_info;
1362
1363 entry->proc_fops = &acpi_video_bus_ROM_fops;
1364 entry->data = acpi_driver_data(device);
1365 entry->owner = THIS_MODULE;
1366
1367 /* 'POST_info' [R] */
1368 entry = create_proc_entry("POST_info", S_IRUGO, device_dir);
1369 if (!entry)
1370 goto err_remove_rom;
1371
1372 entry->proc_fops = &acpi_video_bus_POST_info_fops;
1373 entry->data = acpi_driver_data(device);
1374 entry->owner = THIS_MODULE;
1375
1376 /* 'POST' [R/W] */
1377 entry = create_proc_entry("POST", S_IFREG | S_IRUGO | S_IWUSR,
1378 device_dir);
1379 if (!entry)
1380 goto err_remove_post_info;
1381
1382 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
1383 entry->proc_fops = &acpi_video_bus_POST_fops;
1384 entry->data = acpi_driver_data(device);
1385 entry->owner = THIS_MODULE;
1386
1387 /* 'DOS' [R/W] */
1388 entry = create_proc_entry("DOS", S_IFREG | S_IRUGO | S_IWUSR,
1389 device_dir);
1390 if (!entry)
1391 goto err_remove_post;
1392
1393 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
1394 entry->proc_fops = &acpi_video_bus_DOS_fops;
1395 entry->data = acpi_driver_data(device);
1396 entry->owner = THIS_MODULE;
1397
1398 video->dir = acpi_device_dir(device) = device_dir;
1399 return 0;
1400
1401 err_remove_post:
1402 remove_proc_entry("POST", device_dir);
1403 err_remove_post_info:
1404 remove_proc_entry("POST_info", device_dir);
1405 err_remove_rom:
1406 remove_proc_entry("ROM", device_dir);
1407 err_remove_info:
1408 remove_proc_entry("info", device_dir);
1409 err_remove_dir:
1410 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1411 return -ENOMEM;
1412 }
1413
1414 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1415 {
1416 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1417
1418 if (device_dir) {
1419 remove_proc_entry("info", device_dir);
1420 remove_proc_entry("ROM", device_dir);
1421 remove_proc_entry("POST_info", device_dir);
1422 remove_proc_entry("POST", device_dir);
1423 remove_proc_entry("DOS", device_dir);
1424 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1425 acpi_device_dir(device) = NULL;
1426 }
1427
1428 return 0;
1429 }
1430
1431 /* --------------------------------------------------------------------------
1432 Driver Interface
1433 -------------------------------------------------------------------------- */
1434
1435 /* device interface */
1436 static struct acpi_video_device_attrib*
1437 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1438 {
1439 struct acpi_video_enumerated_device *ids;
1440 int i;
1441
1442 for (i = 0; i < video->attached_count; i++) {
1443 ids = &video->attached_array[i];
1444 if ((ids->value.int_val & 0xffff) == device_id)
1445 return &ids->value.attrib;
1446 }
1447
1448 return NULL;
1449 }
1450
1451 static int
1452 acpi_video_bus_get_one_device(struct acpi_device *device,
1453 struct acpi_video_bus *video)
1454 {
1455 unsigned long device_id;
1456 int status;
1457 struct acpi_video_device *data;
1458 struct acpi_video_device_attrib* attribute;
1459
1460 if (!device || !video)
1461 return -EINVAL;
1462
1463 status =
1464 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1465 if (ACPI_SUCCESS(status)) {
1466
1467 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1468 if (!data)
1469 return -ENOMEM;
1470
1471 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1472 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1473 acpi_driver_data(device) = data;
1474
1475 data->device_id = device_id;
1476 data->video = video;
1477 data->dev = device;
1478
1479 attribute = acpi_video_get_device_attr(video, device_id);
1480
1481 if((attribute != NULL) && attribute->device_id_scheme) {
1482 switch (attribute->display_type) {
1483 case ACPI_VIDEO_DISPLAY_CRT:
1484 data->flags.crt = 1;
1485 break;
1486 case ACPI_VIDEO_DISPLAY_TV:
1487 data->flags.tvout = 1;
1488 break;
1489 case ACPI_VIDEO_DISPLAY_DVI:
1490 data->flags.dvi = 1;
1491 break;
1492 case ACPI_VIDEO_DISPLAY_LCD:
1493 data->flags.lcd = 1;
1494 break;
1495 default:
1496 data->flags.unknown = 1;
1497 break;
1498 }
1499 if(attribute->bios_can_detect)
1500 data->flags.bios = 1;
1501 } else
1502 data->flags.unknown = 1;
1503
1504 acpi_video_device_bind(video, data);
1505 acpi_video_device_find_cap(data);
1506
1507 status = acpi_install_notify_handler(device->handle,
1508 ACPI_DEVICE_NOTIFY,
1509 acpi_video_device_notify,
1510 data);
1511 if (ACPI_FAILURE(status)) {
1512 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1513 "Error installing notify handler\n"));
1514 if(data->brightness)
1515 kfree(data->brightness->levels);
1516 kfree(data->brightness);
1517 kfree(data);
1518 return -ENODEV;
1519 }
1520
1521 mutex_lock(&video->device_list_lock);
1522 list_add_tail(&data->entry, &video->video_device_list);
1523 mutex_unlock(&video->device_list_lock);
1524
1525 acpi_video_device_add_fs(device);
1526
1527 return 0;
1528 }
1529
1530 return -ENOENT;
1531 }
1532
1533 /*
1534 * Arg:
1535 * video : video bus device
1536 *
1537 * Return:
1538 * none
1539 *
1540 * Enumerate the video device list of the video bus,
1541 * bind the ids with the corresponding video devices
1542 * under the video bus.
1543 */
1544
1545 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1546 {
1547 struct acpi_video_device *dev;
1548
1549 mutex_lock(&video->device_list_lock);
1550
1551 list_for_each_entry(dev, &video->video_device_list, entry)
1552 acpi_video_device_bind(video, dev);
1553
1554 mutex_unlock(&video->device_list_lock);
1555 }
1556
1557 /*
1558 * Arg:
1559 * video : video bus device
1560 * device : video output device under the video
1561 * bus
1562 *
1563 * Return:
1564 * none
1565 *
1566 * Bind the ids with the corresponding video devices
1567 * under the video bus.
1568 */
1569
1570 static void
1571 acpi_video_device_bind(struct acpi_video_bus *video,
1572 struct acpi_video_device *device)
1573 {
1574 struct acpi_video_enumerated_device *ids;
1575 int i;
1576
1577 for (i = 0; i < video->attached_count; i++) {
1578 ids = &video->attached_array[i];
1579 if (device->device_id == (ids->value.int_val & 0xffff)) {
1580 ids->bind_info = device;
1581 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1582 }
1583 }
1584 }
1585
1586 /*
1587 * Arg:
1588 * video : video bus device
1589 *
1590 * Return:
1591 * < 0 : error
1592 *
1593 * Call _DOD to enumerate all devices attached to display adapter
1594 *
1595 */
1596
1597 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1598 {
1599 int status;
1600 int count;
1601 int i;
1602 struct acpi_video_enumerated_device *active_list;
1603 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1604 union acpi_object *dod = NULL;
1605 union acpi_object *obj;
1606
1607 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1608 if (!ACPI_SUCCESS(status)) {
1609 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1610 return status;
1611 }
1612
1613 dod = buffer.pointer;
1614 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1615 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1616 status = -EFAULT;
1617 goto out;
1618 }
1619
1620 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1621 dod->package.count));
1622
1623 active_list = kcalloc(1 + dod->package.count,
1624 sizeof(struct acpi_video_enumerated_device),
1625 GFP_KERNEL);
1626 if (!active_list) {
1627 status = -ENOMEM;
1628 goto out;
1629 }
1630
1631 count = 0;
1632 for (i = 0; i < dod->package.count; i++) {
1633 obj = &dod->package.elements[i];
1634
1635 if (obj->type != ACPI_TYPE_INTEGER) {
1636 printk(KERN_ERR PREFIX
1637 "Invalid _DOD data in element %d\n", i);
1638 continue;
1639 }
1640
1641 active_list[count].value.int_val = obj->integer.value;
1642 active_list[count].bind_info = NULL;
1643 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1644 (int)obj->integer.value));
1645 count++;
1646 }
1647
1648 kfree(video->attached_array);
1649
1650 video->attached_array = active_list;
1651 video->attached_count = count;
1652
1653 out:
1654 kfree(buffer.pointer);
1655 return status;
1656 }
1657
1658 static int
1659 acpi_video_get_next_level(struct acpi_video_device *device,
1660 u32 level_current, u32 event)
1661 {
1662 int min, max, min_above, max_below, i, l, delta = 255;
1663 max = max_below = 0;
1664 min = min_above = 255;
1665 /* Find closest level to level_current */
1666 for (i = 0; i < device->brightness->count; i++) {
1667 l = device->brightness->levels[i];
1668 if (abs(l - level_current) < abs(delta)) {
1669 delta = l - level_current;
1670 if (!delta)
1671 break;
1672 }
1673 }
1674 /* Ajust level_current to closest available level */
1675 level_current += delta;
1676 for (i = 0; i < device->brightness->count; i++) {
1677 l = device->brightness->levels[i];
1678 if (l < min)
1679 min = l;
1680 if (l > max)
1681 max = l;
1682 if (l < min_above && l > level_current)
1683 min_above = l;
1684 if (l > max_below && l < level_current)
1685 max_below = l;
1686 }
1687
1688 switch (event) {
1689 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1690 return (level_current < max) ? min_above : min;
1691 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1692 return (level_current < max) ? min_above : max;
1693 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1694 return (level_current > min) ? max_below : min;
1695 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1696 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1697 return 0;
1698 default:
1699 return level_current;
1700 }
1701 }
1702
1703 static void
1704 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1705 {
1706 unsigned long level_current, level_next;
1707 acpi_video_device_lcd_get_level_current(device, &level_current);
1708 level_next = acpi_video_get_next_level(device, level_current, event);
1709 acpi_video_device_lcd_set_level(device, level_next);
1710 }
1711
1712 static int
1713 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1714 struct acpi_device *device)
1715 {
1716 int status = 0;
1717 struct acpi_device *dev;
1718
1719 acpi_video_device_enumerate(video);
1720
1721 list_for_each_entry(dev, &device->children, node) {
1722
1723 status = acpi_video_bus_get_one_device(dev, video);
1724 if (ACPI_FAILURE(status)) {
1725 ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
1726 continue;
1727 }
1728 }
1729 return status;
1730 }
1731
1732 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1733 {
1734 acpi_status status;
1735 struct acpi_video_bus *video;
1736
1737
1738 if (!device || !device->video)
1739 return -ENOENT;
1740
1741 video = device->video;
1742
1743 acpi_video_device_remove_fs(device->dev);
1744
1745 status = acpi_remove_notify_handler(device->dev->handle,
1746 ACPI_DEVICE_NOTIFY,
1747 acpi_video_device_notify);
1748 backlight_device_unregister(device->backlight);
1749 if (device->cdev) {
1750 sysfs_remove_link(&device->dev->dev.kobj,
1751 "thermal_cooling");
1752 sysfs_remove_link(&device->cdev->device.kobj,
1753 "device");
1754 thermal_cooling_device_unregister(device->cdev);
1755 device->cdev = NULL;
1756 }
1757 video_output_unregister(device->output_dev);
1758
1759 return 0;
1760 }
1761
1762 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1763 {
1764 int status;
1765 struct acpi_video_device *dev, *next;
1766
1767 mutex_lock(&video->device_list_lock);
1768
1769 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1770
1771 status = acpi_video_bus_put_one_device(dev);
1772 if (ACPI_FAILURE(status))
1773 printk(KERN_WARNING PREFIX
1774 "hhuuhhuu bug in acpi video driver.\n");
1775
1776 if (dev->brightness) {
1777 kfree(dev->brightness->levels);
1778 kfree(dev->brightness);
1779 }
1780 list_del(&dev->entry);
1781 kfree(dev);
1782 }
1783
1784 mutex_unlock(&video->device_list_lock);
1785
1786 return 0;
1787 }
1788
1789 /* acpi_video interface */
1790
1791 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1792 {
1793 return acpi_video_bus_DOS(video, 0, 0);
1794 }
1795
1796 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1797 {
1798 return acpi_video_bus_DOS(video, 0, 1);
1799 }
1800
1801 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1802 {
1803 struct acpi_video_bus *video = data;
1804 struct acpi_device *device = NULL;
1805 struct input_dev *input;
1806 int keycode;
1807
1808 if (!video)
1809 return;
1810
1811 device = video->device;
1812 input = video->input;
1813
1814 switch (event) {
1815 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1816 * most likely via hotkey. */
1817 acpi_bus_generate_proc_event(device, event, 0);
1818 keycode = KEY_SWITCHVIDEOMODE;
1819 break;
1820
1821 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1822 * connector. */
1823 acpi_video_device_enumerate(video);
1824 acpi_video_device_rebind(video);
1825 acpi_bus_generate_proc_event(device, event, 0);
1826 keycode = KEY_SWITCHVIDEOMODE;
1827 break;
1828
1829 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1830 acpi_bus_generate_proc_event(device, event, 0);
1831 keycode = KEY_SWITCHVIDEOMODE;
1832 break;
1833 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1834 acpi_bus_generate_proc_event(device, event, 0);
1835 keycode = KEY_VIDEO_NEXT;
1836 break;
1837 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1838 acpi_bus_generate_proc_event(device, event, 0);
1839 keycode = KEY_VIDEO_PREV;
1840 break;
1841
1842 default:
1843 keycode = KEY_UNKNOWN;
1844 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1845 "Unsupported event [0x%x]\n", event));
1846 break;
1847 }
1848
1849 acpi_notifier_call_chain(device, event, 0);
1850 input_report_key(input, keycode, 1);
1851 input_sync(input);
1852 input_report_key(input, keycode, 0);
1853 input_sync(input);
1854
1855 return;
1856 }
1857
1858 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1859 {
1860 struct acpi_video_device *video_device = data;
1861 struct acpi_device *device = NULL;
1862 struct acpi_video_bus *bus;
1863 struct input_dev *input;
1864 int keycode;
1865
1866 if (!video_device)
1867 return;
1868
1869 device = video_device->dev;
1870 bus = video_device->video;
1871 input = bus->input;
1872
1873 switch (event) {
1874 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1875 if (brightness_switch_enabled)
1876 acpi_video_switch_brightness(video_device, event);
1877 acpi_bus_generate_proc_event(device, event, 0);
1878 keycode = KEY_BRIGHTNESS_CYCLE;
1879 break;
1880 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1881 if (brightness_switch_enabled)
1882 acpi_video_switch_brightness(video_device, event);
1883 acpi_bus_generate_proc_event(device, event, 0);
1884 keycode = KEY_BRIGHTNESSUP;
1885 break;
1886 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1887 if (brightness_switch_enabled)
1888 acpi_video_switch_brightness(video_device, event);
1889 acpi_bus_generate_proc_event(device, event, 0);
1890 keycode = KEY_BRIGHTNESSDOWN;
1891 break;
1892 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1893 if (brightness_switch_enabled)
1894 acpi_video_switch_brightness(video_device, event);
1895 acpi_bus_generate_proc_event(device, event, 0);
1896 keycode = KEY_BRIGHTNESS_ZERO;
1897 break;
1898 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1899 if (brightness_switch_enabled)
1900 acpi_video_switch_brightness(video_device, event);
1901 acpi_bus_generate_proc_event(device, event, 0);
1902 keycode = KEY_DISPLAY_OFF;
1903 break;
1904 default:
1905 keycode = KEY_UNKNOWN;
1906 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1907 "Unsupported event [0x%x]\n", event));
1908 break;
1909 }
1910
1911 acpi_notifier_call_chain(device, event, 0);
1912 input_report_key(input, keycode, 1);
1913 input_sync(input);
1914 input_report_key(input, keycode, 0);
1915 input_sync(input);
1916
1917 return;
1918 }
1919
1920 static int instance;
1921 static int acpi_video_resume(struct acpi_device *device)
1922 {
1923 struct acpi_video_bus *video;
1924 struct acpi_video_device *video_device;
1925 int i;
1926
1927 if (!device || !acpi_driver_data(device))
1928 return -EINVAL;
1929
1930 video = acpi_driver_data(device);
1931
1932 for (i = 0; i < video->attached_count; i++) {
1933 video_device = video->attached_array[i].bind_info;
1934 if (video_device && video_device->backlight)
1935 acpi_video_set_brightness(video_device->backlight);
1936 }
1937 return AE_OK;
1938 }
1939
1940 static int acpi_video_bus_add(struct acpi_device *device)
1941 {
1942 acpi_status status;
1943 struct acpi_video_bus *video;
1944 struct input_dev *input;
1945 int error;
1946
1947 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1948 if (!video)
1949 return -ENOMEM;
1950
1951 /* a hack to fix the duplicate name "VID" problem on T61 */
1952 if (!strcmp(device->pnp.bus_id, "VID")) {
1953 if (instance)
1954 device->pnp.bus_id[3] = '0' + instance;
1955 instance ++;
1956 }
1957
1958 video->device = device;
1959 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1960 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1961 acpi_driver_data(device) = video;
1962
1963 acpi_video_bus_find_cap(video);
1964 error = acpi_video_bus_check(video);
1965 if (error)
1966 goto err_free_video;
1967
1968 error = acpi_video_bus_add_fs(device);
1969 if (error)
1970 goto err_free_video;
1971
1972 mutex_init(&video->device_list_lock);
1973 INIT_LIST_HEAD(&video->video_device_list);
1974
1975 acpi_video_bus_get_devices(video, device);
1976 acpi_video_bus_start_devices(video);
1977
1978 status = acpi_install_notify_handler(device->handle,
1979 ACPI_DEVICE_NOTIFY,
1980 acpi_video_bus_notify, video);
1981 if (ACPI_FAILURE(status)) {
1982 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1983 "Error installing notify handler\n"));
1984 error = -ENODEV;
1985 goto err_stop_video;
1986 }
1987
1988 video->input = input = input_allocate_device();
1989 if (!input) {
1990 error = -ENOMEM;
1991 goto err_uninstall_notify;
1992 }
1993
1994 snprintf(video->phys, sizeof(video->phys),
1995 "%s/video/input0", acpi_device_hid(video->device));
1996
1997 input->name = acpi_device_name(video->device);
1998 input->phys = video->phys;
1999 input->id.bustype = BUS_HOST;
2000 input->id.product = 0x06;
2001 input->dev.parent = &device->dev;
2002 input->evbit[0] = BIT(EV_KEY);
2003 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2004 set_bit(KEY_VIDEO_NEXT, input->keybit);
2005 set_bit(KEY_VIDEO_PREV, input->keybit);
2006 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2007 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2008 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2009 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2010 set_bit(KEY_DISPLAY_OFF, input->keybit);
2011 set_bit(KEY_UNKNOWN, input->keybit);
2012
2013 error = input_register_device(input);
2014 if (error)
2015 goto err_free_input_dev;
2016
2017 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
2018 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2019 video->flags.multihead ? "yes" : "no",
2020 video->flags.rom ? "yes" : "no",
2021 video->flags.post ? "yes" : "no");
2022
2023 return 0;
2024
2025 err_free_input_dev:
2026 input_free_device(input);
2027 err_uninstall_notify:
2028 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2029 acpi_video_bus_notify);
2030 err_stop_video:
2031 acpi_video_bus_stop_devices(video);
2032 acpi_video_bus_put_devices(video);
2033 kfree(video->attached_array);
2034 acpi_video_bus_remove_fs(device);
2035 err_free_video:
2036 kfree(video);
2037 acpi_driver_data(device) = NULL;
2038
2039 return error;
2040 }
2041
2042 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2043 {
2044 acpi_status status = 0;
2045 struct acpi_video_bus *video = NULL;
2046
2047
2048 if (!device || !acpi_driver_data(device))
2049 return -EINVAL;
2050
2051 video = acpi_driver_data(device);
2052
2053 acpi_video_bus_stop_devices(video);
2054
2055 status = acpi_remove_notify_handler(video->device->handle,
2056 ACPI_DEVICE_NOTIFY,
2057 acpi_video_bus_notify);
2058
2059 acpi_video_bus_put_devices(video);
2060 acpi_video_bus_remove_fs(device);
2061
2062 input_unregister_device(video->input);
2063 kfree(video->attached_array);
2064 kfree(video);
2065
2066 return 0;
2067 }
2068
2069 static int __init acpi_video_init(void)
2070 {
2071 int result = 0;
2072
2073
2074 /*
2075 acpi_dbg_level = 0xFFFFFFFF;
2076 acpi_dbg_layer = 0x08000000;
2077 */
2078
2079 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2080 if (!acpi_video_dir)
2081 return -ENODEV;
2082 acpi_video_dir->owner = THIS_MODULE;
2083
2084 result = acpi_bus_register_driver(&acpi_video_bus);
2085 if (result < 0) {
2086 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2087 return -ENODEV;
2088 }
2089
2090 return 0;
2091 }
2092
2093 static void __exit acpi_video_exit(void)
2094 {
2095
2096 acpi_bus_unregister_driver(&acpi_video_bus);
2097
2098 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2099
2100 return;
2101 }
2102
2103 module_init(acpi_video_init);
2104 module_exit(acpi_video_exit);