Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livep...
[deliverable/linux.git] / drivers / acpi / acpi_video.c
1 /*
2 * video.c - ACPI Video Driver
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 */
22
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/list.h>
28 #include <linux/mutex.h>
29 #include <linux/input.h>
30 #include <linux/backlight.h>
31 #include <linux/thermal.h>
32 #include <linux/sort.h>
33 #include <linux/pci.h>
34 #include <linux/pci_ids.h>
35 #include <linux/slab.h>
36 #include <linux/dmi.h>
37 #include <linux/suspend.h>
38 #include <linux/acpi.h>
39 #include <acpi/video.h>
40 #include <asm/uaccess.h>
41
42 #define PREFIX "ACPI: "
43
44 #define ACPI_VIDEO_BUS_NAME "Video Bus"
45 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
46 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
47 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
48 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
49 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
50 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
51
52 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
53 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
54 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
55 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
56 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
57
58 #define MAX_NAME_LEN 20
59
60 #define _COMPONENT ACPI_VIDEO_COMPONENT
61 ACPI_MODULE_NAME("video");
62
63 MODULE_AUTHOR("Bruno Ducrot");
64 MODULE_DESCRIPTION("ACPI Video Driver");
65 MODULE_LICENSE("GPL");
66
67 static bool brightness_switch_enabled = 1;
68 module_param(brightness_switch_enabled, bool, 0644);
69
70 /*
71 * By default, we don't allow duplicate ACPI video bus devices
72 * under the same VGA controller
73 */
74 static bool allow_duplicates;
75 module_param(allow_duplicates, bool, 0644);
76
77 static int disable_backlight_sysfs_if = -1;
78 module_param(disable_backlight_sysfs_if, int, 0444);
79
80 static int register_count;
81 static DEFINE_MUTEX(register_count_mutex);
82 static struct mutex video_list_lock;
83 static struct list_head video_bus_head;
84 static int acpi_video_bus_add(struct acpi_device *device);
85 static int acpi_video_bus_remove(struct acpi_device *device);
86 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
87 void acpi_video_detect_exit(void);
88
89 static const struct acpi_device_id video_device_ids[] = {
90 {ACPI_VIDEO_HID, 0},
91 {"", 0},
92 };
93 MODULE_DEVICE_TABLE(acpi, video_device_ids);
94
95 static struct acpi_driver acpi_video_bus = {
96 .name = "video",
97 .class = ACPI_VIDEO_CLASS,
98 .ids = video_device_ids,
99 .ops = {
100 .add = acpi_video_bus_add,
101 .remove = acpi_video_bus_remove,
102 .notify = acpi_video_bus_notify,
103 },
104 };
105
106 struct acpi_video_bus_flags {
107 u8 multihead:1; /* can switch video heads */
108 u8 rom:1; /* can retrieve a video rom */
109 u8 post:1; /* can configure the head to */
110 u8 reserved:5;
111 };
112
113 struct acpi_video_bus_cap {
114 u8 _DOS:1; /* Enable/Disable output switching */
115 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
116 u8 _ROM:1; /* Get ROM Data */
117 u8 _GPD:1; /* Get POST Device */
118 u8 _SPD:1; /* Set POST Device */
119 u8 _VPO:1; /* Video POST Options */
120 u8 reserved:2;
121 };
122
123 struct acpi_video_device_attrib {
124 u32 display_index:4; /* A zero-based instance of the Display */
125 u32 display_port_attachment:4; /* This field differentiates the display type */
126 u32 display_type:4; /* Describe the specific type in use */
127 u32 vendor_specific:4; /* Chipset Vendor Specific */
128 u32 bios_can_detect:1; /* BIOS can detect the device */
129 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
130 the VGA device. */
131 u32 pipe_id:3; /* For VGA multiple-head devices. */
132 u32 reserved:10; /* Must be 0 */
133 u32 device_id_scheme:1; /* Device ID Scheme */
134 };
135
136 struct acpi_video_enumerated_device {
137 union {
138 u32 int_val;
139 struct acpi_video_device_attrib attrib;
140 } value;
141 struct acpi_video_device *bind_info;
142 };
143
144 struct acpi_video_bus {
145 struct acpi_device *device;
146 bool backlight_registered;
147 u8 dos_setting;
148 struct acpi_video_enumerated_device *attached_array;
149 u8 attached_count;
150 u8 child_count;
151 struct acpi_video_bus_cap cap;
152 struct acpi_video_bus_flags flags;
153 struct list_head video_device_list;
154 struct mutex device_list_lock; /* protects video_device_list */
155 struct list_head entry;
156 struct input_dev *input;
157 char phys[32]; /* for input device */
158 struct notifier_block pm_nb;
159 };
160
161 struct acpi_video_device_flags {
162 u8 crt:1;
163 u8 lcd:1;
164 u8 tvout:1;
165 u8 dvi:1;
166 u8 bios:1;
167 u8 unknown:1;
168 u8 notify:1;
169 u8 reserved:1;
170 };
171
172 struct acpi_video_device_cap {
173 u8 _ADR:1; /* Return the unique ID */
174 u8 _BCL:1; /* Query list of brightness control levels supported */
175 u8 _BCM:1; /* Set the brightness level */
176 u8 _BQC:1; /* Get current brightness level */
177 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
178 u8 _DDC:1; /* Return the EDID for this device */
179 };
180
181 struct acpi_video_brightness_flags {
182 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
183 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
184 u8 _BQC_use_index:1; /* _BQC returns an index value */
185 };
186
187 struct acpi_video_device_brightness {
188 int curr;
189 int count;
190 int *levels;
191 struct acpi_video_brightness_flags flags;
192 };
193
194 struct acpi_video_device {
195 unsigned long device_id;
196 struct acpi_video_device_flags flags;
197 struct acpi_video_device_cap cap;
198 struct list_head entry;
199 struct delayed_work switch_brightness_work;
200 int switch_brightness_event;
201 struct acpi_video_bus *video;
202 struct acpi_device *dev;
203 struct acpi_video_device_brightness *brightness;
204 struct backlight_device *backlight;
205 struct thermal_cooling_device *cooling_dev;
206 };
207
208 static const char device_decode[][30] = {
209 "motherboard VGA device",
210 "PCI VGA device",
211 "AGP VGA device",
212 "UNKNOWN",
213 };
214
215 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
216 static void acpi_video_device_rebind(struct acpi_video_bus *video);
217 static void acpi_video_device_bind(struct acpi_video_bus *video,
218 struct acpi_video_device *device);
219 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
220 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
221 int level);
222 static int acpi_video_device_lcd_get_level_current(
223 struct acpi_video_device *device,
224 unsigned long long *level, bool raw);
225 static int acpi_video_get_next_level(struct acpi_video_device *device,
226 u32 level_current, u32 event);
227 static void acpi_video_switch_brightness(struct work_struct *work);
228
229 /* backlight device sysfs support */
230 static int acpi_video_get_brightness(struct backlight_device *bd)
231 {
232 unsigned long long cur_level;
233 int i;
234 struct acpi_video_device *vd = bl_get_data(bd);
235
236 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
237 return -EINVAL;
238 for (i = 2; i < vd->brightness->count; i++) {
239 if (vd->brightness->levels[i] == cur_level)
240 /*
241 * The first two entries are special - see page 575
242 * of the ACPI spec 3.0
243 */
244 return i - 2;
245 }
246 return 0;
247 }
248
249 static int acpi_video_set_brightness(struct backlight_device *bd)
250 {
251 int request_level = bd->props.brightness + 2;
252 struct acpi_video_device *vd = bl_get_data(bd);
253
254 cancel_delayed_work(&vd->switch_brightness_work);
255 return acpi_video_device_lcd_set_level(vd,
256 vd->brightness->levels[request_level]);
257 }
258
259 static const struct backlight_ops acpi_backlight_ops = {
260 .get_brightness = acpi_video_get_brightness,
261 .update_status = acpi_video_set_brightness,
262 };
263
264 /* thermal cooling device callbacks */
265 static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
266 long *state)
267 {
268 struct acpi_device *device = cooling_dev->devdata;
269 struct acpi_video_device *video = acpi_driver_data(device);
270
271 *state = video->brightness->count - 3;
272 return 0;
273 }
274
275 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
276 long *state)
277 {
278 struct acpi_device *device = cooling_dev->devdata;
279 struct acpi_video_device *video = acpi_driver_data(device);
280 unsigned long long level;
281 int offset;
282
283 if (acpi_video_device_lcd_get_level_current(video, &level, false))
284 return -EINVAL;
285 for (offset = 2; offset < video->brightness->count; offset++)
286 if (level == video->brightness->levels[offset]) {
287 *state = video->brightness->count - offset - 1;
288 return 0;
289 }
290
291 return -EINVAL;
292 }
293
294 static int
295 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
296 {
297 struct acpi_device *device = cooling_dev->devdata;
298 struct acpi_video_device *video = acpi_driver_data(device);
299 int level;
300
301 if (state >= video->brightness->count - 2)
302 return -EINVAL;
303
304 state = video->brightness->count - state;
305 level = video->brightness->levels[state - 1];
306 return acpi_video_device_lcd_set_level(video, level);
307 }
308
309 static const struct thermal_cooling_device_ops video_cooling_ops = {
310 .get_max_state = video_get_max_state,
311 .get_cur_state = video_get_cur_state,
312 .set_cur_state = video_set_cur_state,
313 };
314
315 /*
316 * --------------------------------------------------------------------------
317 * Video Management
318 * --------------------------------------------------------------------------
319 */
320
321 static int
322 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
323 union acpi_object **levels)
324 {
325 int status;
326 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
327 union acpi_object *obj;
328
329
330 *levels = NULL;
331
332 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
333 if (!ACPI_SUCCESS(status))
334 return status;
335 obj = (union acpi_object *)buffer.pointer;
336 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
337 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
338 status = -EFAULT;
339 goto err;
340 }
341
342 *levels = obj;
343
344 return 0;
345
346 err:
347 kfree(buffer.pointer);
348
349 return status;
350 }
351
352 static int
353 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
354 {
355 int status;
356 int state;
357
358 status = acpi_execute_simple_method(device->dev->handle,
359 "_BCM", level);
360 if (ACPI_FAILURE(status)) {
361 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
362 return -EIO;
363 }
364
365 device->brightness->curr = level;
366 for (state = 2; state < device->brightness->count; state++)
367 if (level == device->brightness->levels[state]) {
368 if (device->backlight)
369 device->backlight->props.brightness = state - 2;
370 return 0;
371 }
372
373 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
374 return -EINVAL;
375 }
376
377 /*
378 * For some buggy _BQC methods, we need to add a constant value to
379 * the _BQC return value to get the actual current brightness level
380 */
381
382 static int bqc_offset_aml_bug_workaround;
383 static int video_set_bqc_offset(const struct dmi_system_id *d)
384 {
385 bqc_offset_aml_bug_workaround = 9;
386 return 0;
387 }
388
389 static int video_disable_backlight_sysfs_if(
390 const struct dmi_system_id *d)
391 {
392 if (disable_backlight_sysfs_if == -1)
393 disable_backlight_sysfs_if = 1;
394 return 0;
395 }
396
397 static struct dmi_system_id video_dmi_table[] = {
398 /*
399 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
400 */
401 {
402 .callback = video_set_bqc_offset,
403 .ident = "Acer Aspire 5720",
404 .matches = {
405 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
406 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
407 },
408 },
409 {
410 .callback = video_set_bqc_offset,
411 .ident = "Acer Aspire 5710Z",
412 .matches = {
413 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
414 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
415 },
416 },
417 {
418 .callback = video_set_bqc_offset,
419 .ident = "eMachines E510",
420 .matches = {
421 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
422 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
423 },
424 },
425 {
426 .callback = video_set_bqc_offset,
427 .ident = "Acer Aspire 5315",
428 .matches = {
429 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
430 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
431 },
432 },
433 {
434 .callback = video_set_bqc_offset,
435 .ident = "Acer Aspire 7720",
436 .matches = {
437 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
438 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
439 },
440 },
441
442 /*
443 * Some machines have a broken acpi-video interface for brightness
444 * control, but still need an acpi_video_device_lcd_set_level() call
445 * on resume to turn the backlight power on. We Enable backlight
446 * control on these systems, but do not register a backlight sysfs
447 * as brightness control does not work.
448 */
449 {
450 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
451 .callback = video_disable_backlight_sysfs_if,
452 .ident = "Toshiba Portege R830",
453 .matches = {
454 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
455 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
456 },
457 },
458 {}
459 };
460
461 static unsigned long long
462 acpi_video_bqc_value_to_level(struct acpi_video_device *device,
463 unsigned long long bqc_value)
464 {
465 unsigned long long level;
466
467 if (device->brightness->flags._BQC_use_index) {
468 /*
469 * _BQC returns an index that doesn't account for
470 * the first 2 items with special meaning, so we need
471 * to compensate for that by offsetting ourselves
472 */
473 if (device->brightness->flags._BCL_reversed)
474 bqc_value = device->brightness->count - 3 - bqc_value;
475
476 level = device->brightness->levels[bqc_value + 2];
477 } else {
478 level = bqc_value;
479 }
480
481 level += bqc_offset_aml_bug_workaround;
482
483 return level;
484 }
485
486 static int
487 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
488 unsigned long long *level, bool raw)
489 {
490 acpi_status status = AE_OK;
491 int i;
492
493 if (device->cap._BQC || device->cap._BCQ) {
494 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
495
496 status = acpi_evaluate_integer(device->dev->handle, buf,
497 NULL, level);
498 if (ACPI_SUCCESS(status)) {
499 if (raw) {
500 /*
501 * Caller has indicated he wants the raw
502 * value returned by _BQC, so don't furtherly
503 * mess with the value.
504 */
505 return 0;
506 }
507
508 *level = acpi_video_bqc_value_to_level(device, *level);
509
510 for (i = 2; i < device->brightness->count; i++)
511 if (device->brightness->levels[i] == *level) {
512 device->brightness->curr = *level;
513 return 0;
514 }
515 /*
516 * BQC returned an invalid level.
517 * Stop using it.
518 */
519 ACPI_WARNING((AE_INFO,
520 "%s returned an invalid level",
521 buf));
522 device->cap._BQC = device->cap._BCQ = 0;
523 } else {
524 /*
525 * Fixme:
526 * should we return an error or ignore this failure?
527 * dev->brightness->curr is a cached value which stores
528 * the correct current backlight level in most cases.
529 * ACPI video backlight still works w/ buggy _BQC.
530 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
531 */
532 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
533 device->cap._BQC = device->cap._BCQ = 0;
534 }
535 }
536
537 *level = device->brightness->curr;
538 return 0;
539 }
540
541 static int
542 acpi_video_device_EDID(struct acpi_video_device *device,
543 union acpi_object **edid, ssize_t length)
544 {
545 int status;
546 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
547 union acpi_object *obj;
548 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
549 struct acpi_object_list args = { 1, &arg0 };
550
551
552 *edid = NULL;
553
554 if (!device)
555 return -ENODEV;
556 if (length == 128)
557 arg0.integer.value = 1;
558 else if (length == 256)
559 arg0.integer.value = 2;
560 else
561 return -EINVAL;
562
563 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
564 if (ACPI_FAILURE(status))
565 return -ENODEV;
566
567 obj = buffer.pointer;
568
569 if (obj && obj->type == ACPI_TYPE_BUFFER)
570 *edid = obj;
571 else {
572 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
573 status = -EFAULT;
574 kfree(obj);
575 }
576
577 return status;
578 }
579
580 /* bus */
581
582 /*
583 * Arg:
584 * video : video bus device pointer
585 * bios_flag :
586 * 0. The system BIOS should NOT automatically switch(toggle)
587 * the active display output.
588 * 1. The system BIOS should automatically switch (toggle) the
589 * active display output. No switch event.
590 * 2. The _DGS value should be locked.
591 * 3. The system BIOS should not automatically switch (toggle) the
592 * active display output, but instead generate the display switch
593 * event notify code.
594 * lcd_flag :
595 * 0. The system BIOS should automatically control the brightness level
596 * of the LCD when the power changes from AC to DC
597 * 1. The system BIOS should NOT automatically control the brightness
598 * level of the LCD when the power changes from AC to DC.
599 * Return Value:
600 * -EINVAL wrong arg.
601 */
602
603 static int
604 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
605 {
606 acpi_status status;
607
608 if (!video->cap._DOS)
609 return 0;
610
611 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
612 return -EINVAL;
613 video->dos_setting = (lcd_flag << 2) | bios_flag;
614 status = acpi_execute_simple_method(video->device->handle, "_DOS",
615 (lcd_flag << 2) | bios_flag);
616 if (ACPI_FAILURE(status))
617 return -EIO;
618
619 return 0;
620 }
621
622 /*
623 * Simple comparison function used to sort backlight levels.
624 */
625
626 static int
627 acpi_video_cmp_level(const void *a, const void *b)
628 {
629 return *(int *)a - *(int *)b;
630 }
631
632 /*
633 * Decides if _BQC/_BCQ for this system is usable
634 *
635 * We do this by changing the level first and then read out the current
636 * brightness level, if the value does not match, find out if it is using
637 * index. If not, clear the _BQC/_BCQ capability.
638 */
639 static int acpi_video_bqc_quirk(struct acpi_video_device *device,
640 int max_level, int current_level)
641 {
642 struct acpi_video_device_brightness *br = device->brightness;
643 int result;
644 unsigned long long level;
645 int test_level;
646
647 /* don't mess with existing known broken systems */
648 if (bqc_offset_aml_bug_workaround)
649 return 0;
650
651 /*
652 * Some systems always report current brightness level as maximum
653 * through _BQC, we need to test another value for them.
654 */
655 test_level = current_level == max_level ? br->levels[3] : max_level;
656
657 result = acpi_video_device_lcd_set_level(device, test_level);
658 if (result)
659 return result;
660
661 result = acpi_video_device_lcd_get_level_current(device, &level, true);
662 if (result)
663 return result;
664
665 if (level != test_level) {
666 /* buggy _BQC found, need to find out if it uses index */
667 if (level < br->count) {
668 if (br->flags._BCL_reversed)
669 level = br->count - 3 - level;
670 if (br->levels[level + 2] == test_level)
671 br->flags._BQC_use_index = 1;
672 }
673
674 if (!br->flags._BQC_use_index)
675 device->cap._BQC = device->cap._BCQ = 0;
676 }
677
678 return 0;
679 }
680
681
682 /*
683 * Arg:
684 * device : video output device (LCD, CRT, ..)
685 *
686 * Return Value:
687 * Maximum brightness level
688 *
689 * Allocate and initialize device->brightness.
690 */
691
692 static int
693 acpi_video_init_brightness(struct acpi_video_device *device)
694 {
695 union acpi_object *obj = NULL;
696 int i, max_level = 0, count = 0, level_ac_battery = 0;
697 unsigned long long level, level_old;
698 union acpi_object *o;
699 struct acpi_video_device_brightness *br = NULL;
700 int result = -EINVAL;
701 u32 value;
702
703 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
704 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
705 "LCD brightness level\n"));
706 goto out;
707 }
708
709 if (obj->package.count < 2)
710 goto out;
711
712 br = kzalloc(sizeof(*br), GFP_KERNEL);
713 if (!br) {
714 printk(KERN_ERR "can't allocate memory\n");
715 result = -ENOMEM;
716 goto out;
717 }
718
719 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
720 GFP_KERNEL);
721 if (!br->levels) {
722 result = -ENOMEM;
723 goto out_free;
724 }
725
726 for (i = 0; i < obj->package.count; i++) {
727 o = (union acpi_object *)&obj->package.elements[i];
728 if (o->type != ACPI_TYPE_INTEGER) {
729 printk(KERN_ERR PREFIX "Invalid data\n");
730 continue;
731 }
732 value = (u32) o->integer.value;
733 /* Skip duplicate entries */
734 if (count > 2 && br->levels[count - 1] == value)
735 continue;
736
737 br->levels[count] = value;
738
739 if (br->levels[count] > max_level)
740 max_level = br->levels[count];
741 count++;
742 }
743
744 /*
745 * some buggy BIOS don't export the levels
746 * when machine is on AC/Battery in _BCL package.
747 * In this case, the first two elements in _BCL packages
748 * are also supported brightness levels that OS should take care of.
749 */
750 for (i = 2; i < count; i++) {
751 if (br->levels[i] == br->levels[0])
752 level_ac_battery++;
753 if (br->levels[i] == br->levels[1])
754 level_ac_battery++;
755 }
756
757 if (level_ac_battery < 2) {
758 level_ac_battery = 2 - level_ac_battery;
759 br->flags._BCL_no_ac_battery_levels = 1;
760 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
761 br->levels[i] = br->levels[i - level_ac_battery];
762 count += level_ac_battery;
763 } else if (level_ac_battery > 2)
764 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
765
766 /* Check if the _BCL package is in a reversed order */
767 if (max_level == br->levels[2]) {
768 br->flags._BCL_reversed = 1;
769 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
770 acpi_video_cmp_level, NULL);
771 } else if (max_level != br->levels[count - 1])
772 ACPI_ERROR((AE_INFO,
773 "Found unordered _BCL package"));
774
775 br->count = count;
776 device->brightness = br;
777
778 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
779 br->curr = level = max_level;
780
781 if (!device->cap._BQC)
782 goto set_level;
783
784 result = acpi_video_device_lcd_get_level_current(device,
785 &level_old, true);
786 if (result)
787 goto out_free_levels;
788
789 result = acpi_video_bqc_quirk(device, max_level, level_old);
790 if (result)
791 goto out_free_levels;
792 /*
793 * cap._BQC may get cleared due to _BQC is found to be broken
794 * in acpi_video_bqc_quirk, so check again here.
795 */
796 if (!device->cap._BQC)
797 goto set_level;
798
799 level = acpi_video_bqc_value_to_level(device, level_old);
800 /*
801 * On some buggy laptops, _BQC returns an uninitialized
802 * value when invoked for the first time, i.e.
803 * level_old is invalid (no matter whether it's a level
804 * or an index). Set the backlight to max_level in this case.
805 */
806 for (i = 2; i < br->count; i++)
807 if (level == br->levels[i])
808 break;
809 if (i == br->count || !level)
810 level = max_level;
811
812 set_level:
813 result = acpi_video_device_lcd_set_level(device, level);
814 if (result)
815 goto out_free_levels;
816
817 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
818 "found %d brightness levels\n", count - 2));
819 kfree(obj);
820 return result;
821
822 out_free_levels:
823 kfree(br->levels);
824 out_free:
825 kfree(br);
826 out:
827 device->brightness = NULL;
828 kfree(obj);
829 return result;
830 }
831
832 /*
833 * Arg:
834 * device : video output device (LCD, CRT, ..)
835 *
836 * Return Value:
837 * None
838 *
839 * Find out all required AML methods defined under the output
840 * device.
841 */
842
843 static void acpi_video_device_find_cap(struct acpi_video_device *device)
844 {
845 if (acpi_has_method(device->dev->handle, "_ADR"))
846 device->cap._ADR = 1;
847 if (acpi_has_method(device->dev->handle, "_BCL"))
848 device->cap._BCL = 1;
849 if (acpi_has_method(device->dev->handle, "_BCM"))
850 device->cap._BCM = 1;
851 if (acpi_has_method(device->dev->handle, "_BQC")) {
852 device->cap._BQC = 1;
853 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
854 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
855 device->cap._BCQ = 1;
856 }
857
858 if (acpi_has_method(device->dev->handle, "_DDC"))
859 device->cap._DDC = 1;
860 }
861
862 /*
863 * Arg:
864 * device : video output device (VGA)
865 *
866 * Return Value:
867 * None
868 *
869 * Find out all required AML methods defined under the video bus device.
870 */
871
872 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
873 {
874 if (acpi_has_method(video->device->handle, "_DOS"))
875 video->cap._DOS = 1;
876 if (acpi_has_method(video->device->handle, "_DOD"))
877 video->cap._DOD = 1;
878 if (acpi_has_method(video->device->handle, "_ROM"))
879 video->cap._ROM = 1;
880 if (acpi_has_method(video->device->handle, "_GPD"))
881 video->cap._GPD = 1;
882 if (acpi_has_method(video->device->handle, "_SPD"))
883 video->cap._SPD = 1;
884 if (acpi_has_method(video->device->handle, "_VPO"))
885 video->cap._VPO = 1;
886 }
887
888 /*
889 * Check whether the video bus device has required AML method to
890 * support the desired features
891 */
892
893 static int acpi_video_bus_check(struct acpi_video_bus *video)
894 {
895 acpi_status status = -ENOENT;
896 struct pci_dev *dev;
897
898 if (!video)
899 return -EINVAL;
900
901 dev = acpi_get_pci_dev(video->device->handle);
902 if (!dev)
903 return -ENODEV;
904 pci_dev_put(dev);
905
906 /*
907 * Since there is no HID, CID and so on for VGA driver, we have
908 * to check well known required nodes.
909 */
910
911 /* Does this device support video switching? */
912 if (video->cap._DOS || video->cap._DOD) {
913 if (!video->cap._DOS) {
914 printk(KERN_WARNING FW_BUG
915 "ACPI(%s) defines _DOD but not _DOS\n",
916 acpi_device_bid(video->device));
917 }
918 video->flags.multihead = 1;
919 status = 0;
920 }
921
922 /* Does this device support retrieving a video ROM? */
923 if (video->cap._ROM) {
924 video->flags.rom = 1;
925 status = 0;
926 }
927
928 /* Does this device support configuring which video device to POST? */
929 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
930 video->flags.post = 1;
931 status = 0;
932 }
933
934 return status;
935 }
936
937 /*
938 * --------------------------------------------------------------------------
939 * Driver Interface
940 * --------------------------------------------------------------------------
941 */
942
943 /* device interface */
944 static struct acpi_video_device_attrib *
945 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
946 {
947 struct acpi_video_enumerated_device *ids;
948 int i;
949
950 for (i = 0; i < video->attached_count; i++) {
951 ids = &video->attached_array[i];
952 if ((ids->value.int_val & 0xffff) == device_id)
953 return &ids->value.attrib;
954 }
955
956 return NULL;
957 }
958
959 static int
960 acpi_video_get_device_type(struct acpi_video_bus *video,
961 unsigned long device_id)
962 {
963 struct acpi_video_enumerated_device *ids;
964 int i;
965
966 for (i = 0; i < video->attached_count; i++) {
967 ids = &video->attached_array[i];
968 if ((ids->value.int_val & 0xffff) == device_id)
969 return ids->value.int_val;
970 }
971
972 return 0;
973 }
974
975 static int
976 acpi_video_bus_get_one_device(struct acpi_device *device,
977 struct acpi_video_bus *video)
978 {
979 unsigned long long device_id;
980 int status, device_type;
981 struct acpi_video_device *data;
982 struct acpi_video_device_attrib *attribute;
983
984 status =
985 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
986 /* Some device omits _ADR, we skip them instead of fail */
987 if (ACPI_FAILURE(status))
988 return 0;
989
990 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
991 if (!data)
992 return -ENOMEM;
993
994 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
995 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
996 device->driver_data = data;
997
998 data->device_id = device_id;
999 data->video = video;
1000 data->dev = device;
1001 INIT_DELAYED_WORK(&data->switch_brightness_work,
1002 acpi_video_switch_brightness);
1003
1004 attribute = acpi_video_get_device_attr(video, device_id);
1005
1006 if (attribute && attribute->device_id_scheme) {
1007 switch (attribute->display_type) {
1008 case ACPI_VIDEO_DISPLAY_CRT:
1009 data->flags.crt = 1;
1010 break;
1011 case ACPI_VIDEO_DISPLAY_TV:
1012 data->flags.tvout = 1;
1013 break;
1014 case ACPI_VIDEO_DISPLAY_DVI:
1015 data->flags.dvi = 1;
1016 break;
1017 case ACPI_VIDEO_DISPLAY_LCD:
1018 data->flags.lcd = 1;
1019 break;
1020 default:
1021 data->flags.unknown = 1;
1022 break;
1023 }
1024 if (attribute->bios_can_detect)
1025 data->flags.bios = 1;
1026 } else {
1027 /* Check for legacy IDs */
1028 device_type = acpi_video_get_device_type(video, device_id);
1029 /* Ignore bits 16 and 18-20 */
1030 switch (device_type & 0xffe2ffff) {
1031 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1032 data->flags.crt = 1;
1033 break;
1034 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1035 data->flags.lcd = 1;
1036 break;
1037 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1038 data->flags.tvout = 1;
1039 break;
1040 default:
1041 data->flags.unknown = 1;
1042 }
1043 }
1044
1045 acpi_video_device_bind(video, data);
1046 acpi_video_device_find_cap(data);
1047
1048 mutex_lock(&video->device_list_lock);
1049 list_add_tail(&data->entry, &video->video_device_list);
1050 mutex_unlock(&video->device_list_lock);
1051
1052 return status;
1053 }
1054
1055 /*
1056 * Arg:
1057 * video : video bus device
1058 *
1059 * Return:
1060 * none
1061 *
1062 * Enumerate the video device list of the video bus,
1063 * bind the ids with the corresponding video devices
1064 * under the video bus.
1065 */
1066
1067 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1068 {
1069 struct acpi_video_device *dev;
1070
1071 mutex_lock(&video->device_list_lock);
1072
1073 list_for_each_entry(dev, &video->video_device_list, entry)
1074 acpi_video_device_bind(video, dev);
1075
1076 mutex_unlock(&video->device_list_lock);
1077 }
1078
1079 /*
1080 * Arg:
1081 * video : video bus device
1082 * device : video output device under the video
1083 * bus
1084 *
1085 * Return:
1086 * none
1087 *
1088 * Bind the ids with the corresponding video devices
1089 * under the video bus.
1090 */
1091
1092 static void
1093 acpi_video_device_bind(struct acpi_video_bus *video,
1094 struct acpi_video_device *device)
1095 {
1096 struct acpi_video_enumerated_device *ids;
1097 int i;
1098
1099 for (i = 0; i < video->attached_count; i++) {
1100 ids = &video->attached_array[i];
1101 if (device->device_id == (ids->value.int_val & 0xffff)) {
1102 ids->bind_info = device;
1103 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1104 }
1105 }
1106 }
1107
1108 static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1109 {
1110 struct acpi_video_bus *video = device->video;
1111 int i;
1112
1113 /*
1114 * If we have a broken _DOD or we have more than 8 output devices
1115 * under the graphics controller node that we can't proper deal with
1116 * in the operation region code currently, no need to test.
1117 */
1118 if (!video->attached_count || video->child_count > 8)
1119 return true;
1120
1121 for (i = 0; i < video->attached_count; i++) {
1122 if ((video->attached_array[i].value.int_val & 0xfff) ==
1123 (device->device_id & 0xfff))
1124 return true;
1125 }
1126
1127 return false;
1128 }
1129
1130 /*
1131 * Arg:
1132 * video : video bus device
1133 *
1134 * Return:
1135 * < 0 : error
1136 *
1137 * Call _DOD to enumerate all devices attached to display adapter
1138 *
1139 */
1140
1141 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1142 {
1143 int status;
1144 int count;
1145 int i;
1146 struct acpi_video_enumerated_device *active_list;
1147 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1148 union acpi_object *dod = NULL;
1149 union acpi_object *obj;
1150
1151 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1152 if (!ACPI_SUCCESS(status)) {
1153 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1154 return status;
1155 }
1156
1157 dod = buffer.pointer;
1158 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1159 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1160 status = -EFAULT;
1161 goto out;
1162 }
1163
1164 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1165 dod->package.count));
1166
1167 active_list = kcalloc(1 + dod->package.count,
1168 sizeof(struct acpi_video_enumerated_device),
1169 GFP_KERNEL);
1170 if (!active_list) {
1171 status = -ENOMEM;
1172 goto out;
1173 }
1174
1175 count = 0;
1176 for (i = 0; i < dod->package.count; i++) {
1177 obj = &dod->package.elements[i];
1178
1179 if (obj->type != ACPI_TYPE_INTEGER) {
1180 printk(KERN_ERR PREFIX
1181 "Invalid _DOD data in element %d\n", i);
1182 continue;
1183 }
1184
1185 active_list[count].value.int_val = obj->integer.value;
1186 active_list[count].bind_info = NULL;
1187 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1188 (int)obj->integer.value));
1189 count++;
1190 }
1191
1192 kfree(video->attached_array);
1193
1194 video->attached_array = active_list;
1195 video->attached_count = count;
1196
1197 out:
1198 kfree(buffer.pointer);
1199 return status;
1200 }
1201
1202 static int
1203 acpi_video_get_next_level(struct acpi_video_device *device,
1204 u32 level_current, u32 event)
1205 {
1206 int min, max, min_above, max_below, i, l, delta = 255;
1207 max = max_below = 0;
1208 min = min_above = 255;
1209 /* Find closest level to level_current */
1210 for (i = 2; i < device->brightness->count; i++) {
1211 l = device->brightness->levels[i];
1212 if (abs(l - level_current) < abs(delta)) {
1213 delta = l - level_current;
1214 if (!delta)
1215 break;
1216 }
1217 }
1218 /* Ajust level_current to closest available level */
1219 level_current += delta;
1220 for (i = 2; i < device->brightness->count; i++) {
1221 l = device->brightness->levels[i];
1222 if (l < min)
1223 min = l;
1224 if (l > max)
1225 max = l;
1226 if (l < min_above && l > level_current)
1227 min_above = l;
1228 if (l > max_below && l < level_current)
1229 max_below = l;
1230 }
1231
1232 switch (event) {
1233 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1234 return (level_current < max) ? min_above : min;
1235 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1236 return (level_current < max) ? min_above : max;
1237 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1238 return (level_current > min) ? max_below : min;
1239 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1240 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1241 return 0;
1242 default:
1243 return level_current;
1244 }
1245 }
1246
1247 static void
1248 acpi_video_switch_brightness(struct work_struct *work)
1249 {
1250 struct acpi_video_device *device = container_of(to_delayed_work(work),
1251 struct acpi_video_device, switch_brightness_work);
1252 unsigned long long level_current, level_next;
1253 int event = device->switch_brightness_event;
1254 int result = -EINVAL;
1255
1256 /* no warning message if acpi_backlight=vendor or a quirk is used */
1257 if (!device->backlight)
1258 return;
1259
1260 if (!device->brightness)
1261 goto out;
1262
1263 result = acpi_video_device_lcd_get_level_current(device,
1264 &level_current,
1265 false);
1266 if (result)
1267 goto out;
1268
1269 level_next = acpi_video_get_next_level(device, level_current, event);
1270
1271 result = acpi_video_device_lcd_set_level(device, level_next);
1272
1273 if (!result)
1274 backlight_force_update(device->backlight,
1275 BACKLIGHT_UPDATE_HOTKEY);
1276
1277 out:
1278 if (result)
1279 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1280 }
1281
1282 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1283 void **edid)
1284 {
1285 struct acpi_video_bus *video;
1286 struct acpi_video_device *video_device;
1287 union acpi_object *buffer = NULL;
1288 acpi_status status;
1289 int i, length;
1290
1291 if (!device || !acpi_driver_data(device))
1292 return -EINVAL;
1293
1294 video = acpi_driver_data(device);
1295
1296 for (i = 0; i < video->attached_count; i++) {
1297 video_device = video->attached_array[i].bind_info;
1298 length = 256;
1299
1300 if (!video_device)
1301 continue;
1302
1303 if (!video_device->cap._DDC)
1304 continue;
1305
1306 if (type) {
1307 switch (type) {
1308 case ACPI_VIDEO_DISPLAY_CRT:
1309 if (!video_device->flags.crt)
1310 continue;
1311 break;
1312 case ACPI_VIDEO_DISPLAY_TV:
1313 if (!video_device->flags.tvout)
1314 continue;
1315 break;
1316 case ACPI_VIDEO_DISPLAY_DVI:
1317 if (!video_device->flags.dvi)
1318 continue;
1319 break;
1320 case ACPI_VIDEO_DISPLAY_LCD:
1321 if (!video_device->flags.lcd)
1322 continue;
1323 break;
1324 }
1325 } else if (video_device->device_id != device_id) {
1326 continue;
1327 }
1328
1329 status = acpi_video_device_EDID(video_device, &buffer, length);
1330
1331 if (ACPI_FAILURE(status) || !buffer ||
1332 buffer->type != ACPI_TYPE_BUFFER) {
1333 length = 128;
1334 status = acpi_video_device_EDID(video_device, &buffer,
1335 length);
1336 if (ACPI_FAILURE(status) || !buffer ||
1337 buffer->type != ACPI_TYPE_BUFFER) {
1338 continue;
1339 }
1340 }
1341
1342 *edid = buffer->buffer.pointer;
1343 return length;
1344 }
1345
1346 return -ENODEV;
1347 }
1348 EXPORT_SYMBOL(acpi_video_get_edid);
1349
1350 static int
1351 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1352 struct acpi_device *device)
1353 {
1354 int status = 0;
1355 struct acpi_device *dev;
1356
1357 /*
1358 * There are systems where video module known to work fine regardless
1359 * of broken _DOD and ignoring returned value here doesn't cause
1360 * any issues later.
1361 */
1362 acpi_video_device_enumerate(video);
1363
1364 list_for_each_entry(dev, &device->children, node) {
1365
1366 status = acpi_video_bus_get_one_device(dev, video);
1367 if (status) {
1368 dev_err(&dev->dev, "Can't attach device\n");
1369 break;
1370 }
1371 video->child_count++;
1372 }
1373 return status;
1374 }
1375
1376 /* acpi_video interface */
1377
1378 /*
1379 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1380 * preform any automatic brightness change on receiving a notification.
1381 */
1382 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1383 {
1384 return acpi_video_bus_DOS(video, 0,
1385 acpi_osi_is_win8() ? 1 : 0);
1386 }
1387
1388 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1389 {
1390 return acpi_video_bus_DOS(video, 0,
1391 acpi_osi_is_win8() ? 0 : 1);
1392 }
1393
1394 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1395 {
1396 struct acpi_video_bus *video = acpi_driver_data(device);
1397 struct input_dev *input;
1398 int keycode = 0;
1399
1400 if (!video || !video->input)
1401 return;
1402
1403 input = video->input;
1404
1405 switch (event) {
1406 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1407 * most likely via hotkey. */
1408 keycode = KEY_SWITCHVIDEOMODE;
1409 break;
1410
1411 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1412 * connector. */
1413 acpi_video_device_enumerate(video);
1414 acpi_video_device_rebind(video);
1415 keycode = KEY_SWITCHVIDEOMODE;
1416 break;
1417
1418 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1419 keycode = KEY_SWITCHVIDEOMODE;
1420 break;
1421 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1422 keycode = KEY_VIDEO_NEXT;
1423 break;
1424 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1425 keycode = KEY_VIDEO_PREV;
1426 break;
1427
1428 default:
1429 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1430 "Unsupported event [0x%x]\n", event));
1431 break;
1432 }
1433
1434 if (acpi_notifier_call_chain(device, event, 0))
1435 /* Something vetoed the keypress. */
1436 keycode = 0;
1437
1438 if (keycode) {
1439 input_report_key(input, keycode, 1);
1440 input_sync(input);
1441 input_report_key(input, keycode, 0);
1442 input_sync(input);
1443 }
1444
1445 return;
1446 }
1447
1448 static void brightness_switch_event(struct acpi_video_device *video_device,
1449 u32 event)
1450 {
1451 if (!brightness_switch_enabled)
1452 return;
1453
1454 video_device->switch_brightness_event = event;
1455 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1456 }
1457
1458 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1459 {
1460 struct acpi_video_device *video_device = data;
1461 struct acpi_device *device = NULL;
1462 struct acpi_video_bus *bus;
1463 struct input_dev *input;
1464 int keycode = 0;
1465
1466 if (!video_device)
1467 return;
1468
1469 device = video_device->dev;
1470 bus = video_device->video;
1471 input = bus->input;
1472
1473 switch (event) {
1474 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1475 brightness_switch_event(video_device, event);
1476 keycode = KEY_BRIGHTNESS_CYCLE;
1477 break;
1478 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1479 brightness_switch_event(video_device, event);
1480 keycode = KEY_BRIGHTNESSUP;
1481 break;
1482 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1483 brightness_switch_event(video_device, event);
1484 keycode = KEY_BRIGHTNESSDOWN;
1485 break;
1486 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
1487 brightness_switch_event(video_device, event);
1488 keycode = KEY_BRIGHTNESS_ZERO;
1489 break;
1490 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1491 brightness_switch_event(video_device, event);
1492 keycode = KEY_DISPLAY_OFF;
1493 break;
1494 default:
1495 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1496 "Unsupported event [0x%x]\n", event));
1497 break;
1498 }
1499
1500 acpi_notifier_call_chain(device, event, 0);
1501
1502 if (keycode) {
1503 input_report_key(input, keycode, 1);
1504 input_sync(input);
1505 input_report_key(input, keycode, 0);
1506 input_sync(input);
1507 }
1508
1509 return;
1510 }
1511
1512 static int acpi_video_resume(struct notifier_block *nb,
1513 unsigned long val, void *ign)
1514 {
1515 struct acpi_video_bus *video;
1516 struct acpi_video_device *video_device;
1517 int i;
1518
1519 switch (val) {
1520 case PM_HIBERNATION_PREPARE:
1521 case PM_SUSPEND_PREPARE:
1522 case PM_RESTORE_PREPARE:
1523 return NOTIFY_DONE;
1524 }
1525
1526 video = container_of(nb, struct acpi_video_bus, pm_nb);
1527
1528 dev_info(&video->device->dev, "Restoring backlight state\n");
1529
1530 for (i = 0; i < video->attached_count; i++) {
1531 video_device = video->attached_array[i].bind_info;
1532 if (video_device && video_device->brightness)
1533 acpi_video_device_lcd_set_level(video_device,
1534 video_device->brightness->curr);
1535 }
1536
1537 return NOTIFY_OK;
1538 }
1539
1540 static acpi_status
1541 acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1542 void **return_value)
1543 {
1544 struct acpi_device *device = context;
1545 struct acpi_device *sibling;
1546 int result;
1547
1548 if (handle == device->handle)
1549 return AE_CTRL_TERMINATE;
1550
1551 result = acpi_bus_get_device(handle, &sibling);
1552 if (result)
1553 return AE_OK;
1554
1555 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1556 return AE_ALREADY_EXISTS;
1557
1558 return AE_OK;
1559 }
1560
1561 static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1562 {
1563 struct backlight_properties props;
1564 struct pci_dev *pdev;
1565 acpi_handle acpi_parent;
1566 struct device *parent = NULL;
1567 int result;
1568 static int count;
1569 char *name;
1570
1571 /*
1572 * Do not create backlight device for video output
1573 * device that is not in the enumerated list.
1574 */
1575 if (!acpi_video_device_in_dod(device)) {
1576 dev_dbg(&device->dev->dev, "not in _DOD list, ignore\n");
1577 return;
1578 }
1579
1580 result = acpi_video_init_brightness(device);
1581 if (result)
1582 return;
1583
1584 if (disable_backlight_sysfs_if > 0)
1585 return;
1586
1587 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1588 if (!name)
1589 return;
1590 count++;
1591
1592 acpi_get_parent(device->dev->handle, &acpi_parent);
1593
1594 pdev = acpi_get_pci_dev(acpi_parent);
1595 if (pdev) {
1596 parent = &pdev->dev;
1597 pci_dev_put(pdev);
1598 }
1599
1600 memset(&props, 0, sizeof(struct backlight_properties));
1601 props.type = BACKLIGHT_FIRMWARE;
1602 props.max_brightness = device->brightness->count - 3;
1603 device->backlight = backlight_device_register(name,
1604 parent,
1605 device,
1606 &acpi_backlight_ops,
1607 &props);
1608 kfree(name);
1609 if (IS_ERR(device->backlight)) {
1610 device->backlight = NULL;
1611 return;
1612 }
1613
1614 /*
1615 * Save current brightness level in case we have to restore it
1616 * before acpi_video_device_lcd_set_level() is called next time.
1617 */
1618 device->backlight->props.brightness =
1619 acpi_video_get_brightness(device->backlight);
1620
1621 device->cooling_dev = thermal_cooling_device_register("LCD",
1622 device->dev, &video_cooling_ops);
1623 if (IS_ERR(device->cooling_dev)) {
1624 /*
1625 * Set cooling_dev to NULL so we don't crash trying to free it.
1626 * Also, why the hell we are returning early and not attempt to
1627 * register video output if cooling device registration failed?
1628 * -- dtor
1629 */
1630 device->cooling_dev = NULL;
1631 return;
1632 }
1633
1634 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1635 device->cooling_dev->id);
1636 result = sysfs_create_link(&device->dev->dev.kobj,
1637 &device->cooling_dev->device.kobj,
1638 "thermal_cooling");
1639 if (result)
1640 printk(KERN_ERR PREFIX "Create sysfs link\n");
1641 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1642 &device->dev->dev.kobj, "device");
1643 if (result)
1644 printk(KERN_ERR PREFIX "Create sysfs link\n");
1645 }
1646
1647 static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1648 {
1649 struct acpi_video_device *dev;
1650 union acpi_object *levels;
1651
1652 mutex_lock(&video->device_list_lock);
1653 list_for_each_entry(dev, &video->video_device_list, entry) {
1654 if (!acpi_video_device_lcd_query_levels(dev, &levels))
1655 kfree(levels);
1656 }
1657 mutex_unlock(&video->device_list_lock);
1658 }
1659
1660 static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1661 {
1662 struct acpi_video_device *dev;
1663
1664 if (video->backlight_registered)
1665 return 0;
1666
1667 acpi_video_run_bcl_for_osi(video);
1668
1669 if (acpi_video_get_backlight_type() != acpi_backlight_video)
1670 return 0;
1671
1672 mutex_lock(&video->device_list_lock);
1673 list_for_each_entry(dev, &video->video_device_list, entry)
1674 acpi_video_dev_register_backlight(dev);
1675 mutex_unlock(&video->device_list_lock);
1676
1677 video->backlight_registered = true;
1678
1679 video->pm_nb.notifier_call = acpi_video_resume;
1680 video->pm_nb.priority = 0;
1681 return register_pm_notifier(&video->pm_nb);
1682 }
1683
1684 static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1685 {
1686 if (device->backlight) {
1687 backlight_device_unregister(device->backlight);
1688 device->backlight = NULL;
1689 }
1690 if (device->brightness) {
1691 kfree(device->brightness->levels);
1692 kfree(device->brightness);
1693 device->brightness = NULL;
1694 }
1695 if (device->cooling_dev) {
1696 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1697 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1698 thermal_cooling_device_unregister(device->cooling_dev);
1699 device->cooling_dev = NULL;
1700 }
1701 }
1702
1703 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1704 {
1705 struct acpi_video_device *dev;
1706 int error;
1707
1708 if (!video->backlight_registered)
1709 return 0;
1710
1711 error = unregister_pm_notifier(&video->pm_nb);
1712
1713 mutex_lock(&video->device_list_lock);
1714 list_for_each_entry(dev, &video->video_device_list, entry)
1715 acpi_video_dev_unregister_backlight(dev);
1716 mutex_unlock(&video->device_list_lock);
1717
1718 video->backlight_registered = false;
1719
1720 return error;
1721 }
1722
1723 static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1724 {
1725 acpi_status status;
1726 struct acpi_device *adev = device->dev;
1727
1728 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1729 acpi_video_device_notify, device);
1730 if (ACPI_FAILURE(status))
1731 dev_err(&adev->dev, "Error installing notify handler\n");
1732 else
1733 device->flags.notify = 1;
1734 }
1735
1736 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1737 {
1738 struct input_dev *input;
1739 struct acpi_video_device *dev;
1740 int error;
1741
1742 video->input = input = input_allocate_device();
1743 if (!input) {
1744 error = -ENOMEM;
1745 goto out;
1746 }
1747
1748 error = acpi_video_bus_start_devices(video);
1749 if (error)
1750 goto err_free_input;
1751
1752 snprintf(video->phys, sizeof(video->phys),
1753 "%s/video/input0", acpi_device_hid(video->device));
1754
1755 input->name = acpi_device_name(video->device);
1756 input->phys = video->phys;
1757 input->id.bustype = BUS_HOST;
1758 input->id.product = 0x06;
1759 input->dev.parent = &video->device->dev;
1760 input->evbit[0] = BIT(EV_KEY);
1761 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1762 set_bit(KEY_VIDEO_NEXT, input->keybit);
1763 set_bit(KEY_VIDEO_PREV, input->keybit);
1764 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1765 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1766 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1767 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1768 set_bit(KEY_DISPLAY_OFF, input->keybit);
1769
1770 error = input_register_device(input);
1771 if (error)
1772 goto err_stop_dev;
1773
1774 mutex_lock(&video->device_list_lock);
1775 list_for_each_entry(dev, &video->video_device_list, entry)
1776 acpi_video_dev_add_notify_handler(dev);
1777 mutex_unlock(&video->device_list_lock);
1778
1779 return 0;
1780
1781 err_stop_dev:
1782 acpi_video_bus_stop_devices(video);
1783 err_free_input:
1784 input_free_device(input);
1785 video->input = NULL;
1786 out:
1787 return error;
1788 }
1789
1790 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1791 {
1792 if (dev->flags.notify) {
1793 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1794 acpi_video_device_notify);
1795 dev->flags.notify = 0;
1796 }
1797 }
1798
1799 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1800 {
1801 struct acpi_video_device *dev;
1802
1803 mutex_lock(&video->device_list_lock);
1804 list_for_each_entry(dev, &video->video_device_list, entry)
1805 acpi_video_dev_remove_notify_handler(dev);
1806 mutex_unlock(&video->device_list_lock);
1807
1808 acpi_video_bus_stop_devices(video);
1809 input_unregister_device(video->input);
1810 video->input = NULL;
1811 }
1812
1813 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1814 {
1815 struct acpi_video_device *dev, *next;
1816
1817 mutex_lock(&video->device_list_lock);
1818 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1819 list_del(&dev->entry);
1820 kfree(dev);
1821 }
1822 mutex_unlock(&video->device_list_lock);
1823
1824 return 0;
1825 }
1826
1827 static int instance;
1828
1829 static int acpi_video_bus_add(struct acpi_device *device)
1830 {
1831 struct acpi_video_bus *video;
1832 int error;
1833 acpi_status status;
1834
1835 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1836 device->parent->handle, 1,
1837 acpi_video_bus_match, NULL,
1838 device, NULL);
1839 if (status == AE_ALREADY_EXISTS) {
1840 printk(KERN_WARNING FW_BUG
1841 "Duplicate ACPI video bus devices for the"
1842 " same VGA controller, please try module "
1843 "parameter \"video.allow_duplicates=1\""
1844 "if the current driver doesn't work.\n");
1845 if (!allow_duplicates)
1846 return -ENODEV;
1847 }
1848
1849 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1850 if (!video)
1851 return -ENOMEM;
1852
1853 /* a hack to fix the duplicate name "VID" problem on T61 */
1854 if (!strcmp(device->pnp.bus_id, "VID")) {
1855 if (instance)
1856 device->pnp.bus_id[3] = '0' + instance;
1857 instance++;
1858 }
1859 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1860 if (!strcmp(device->pnp.bus_id, "VGA")) {
1861 if (instance)
1862 device->pnp.bus_id[3] = '0' + instance;
1863 instance++;
1864 }
1865
1866 video->device = device;
1867 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1868 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1869 device->driver_data = video;
1870
1871 acpi_video_bus_find_cap(video);
1872 error = acpi_video_bus_check(video);
1873 if (error)
1874 goto err_free_video;
1875
1876 mutex_init(&video->device_list_lock);
1877 INIT_LIST_HEAD(&video->video_device_list);
1878
1879 error = acpi_video_bus_get_devices(video, device);
1880 if (error)
1881 goto err_put_video;
1882
1883 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
1884 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1885 video->flags.multihead ? "yes" : "no",
1886 video->flags.rom ? "yes" : "no",
1887 video->flags.post ? "yes" : "no");
1888 mutex_lock(&video_list_lock);
1889 list_add_tail(&video->entry, &video_bus_head);
1890 mutex_unlock(&video_list_lock);
1891
1892 acpi_video_bus_register_backlight(video);
1893 acpi_video_bus_add_notify_handler(video);
1894
1895 return 0;
1896
1897 err_put_video:
1898 acpi_video_bus_put_devices(video);
1899 kfree(video->attached_array);
1900 err_free_video:
1901 kfree(video);
1902 device->driver_data = NULL;
1903
1904 return error;
1905 }
1906
1907 static int acpi_video_bus_remove(struct acpi_device *device)
1908 {
1909 struct acpi_video_bus *video = NULL;
1910
1911
1912 if (!device || !acpi_driver_data(device))
1913 return -EINVAL;
1914
1915 video = acpi_driver_data(device);
1916
1917 acpi_video_bus_remove_notify_handler(video);
1918 acpi_video_bus_unregister_backlight(video);
1919 acpi_video_bus_put_devices(video);
1920
1921 mutex_lock(&video_list_lock);
1922 list_del(&video->entry);
1923 mutex_unlock(&video_list_lock);
1924
1925 kfree(video->attached_array);
1926 kfree(video);
1927
1928 return 0;
1929 }
1930
1931 static int __init is_i740(struct pci_dev *dev)
1932 {
1933 if (dev->device == 0x00D1)
1934 return 1;
1935 if (dev->device == 0x7000)
1936 return 1;
1937 return 0;
1938 }
1939
1940 static int __init intel_opregion_present(void)
1941 {
1942 int opregion = 0;
1943 struct pci_dev *dev = NULL;
1944 u32 address;
1945
1946 for_each_pci_dev(dev) {
1947 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1948 continue;
1949 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1950 continue;
1951 /* We don't want to poke around undefined i740 registers */
1952 if (is_i740(dev))
1953 continue;
1954 pci_read_config_dword(dev, 0xfc, &address);
1955 if (!address)
1956 continue;
1957 opregion = 1;
1958 }
1959 return opregion;
1960 }
1961
1962 int acpi_video_register(void)
1963 {
1964 int ret = 0;
1965
1966 mutex_lock(&register_count_mutex);
1967 if (register_count) {
1968 /*
1969 * if the function of acpi_video_register is already called,
1970 * don't register the acpi_vide_bus again and return no error.
1971 */
1972 goto leave;
1973 }
1974
1975 mutex_init(&video_list_lock);
1976 INIT_LIST_HEAD(&video_bus_head);
1977
1978 dmi_check_system(video_dmi_table);
1979
1980 ret = acpi_bus_register_driver(&acpi_video_bus);
1981 if (ret)
1982 goto leave;
1983
1984 /*
1985 * When the acpi_video_bus is loaded successfully, increase
1986 * the counter reference.
1987 */
1988 register_count = 1;
1989
1990 leave:
1991 mutex_unlock(&register_count_mutex);
1992 return ret;
1993 }
1994 EXPORT_SYMBOL(acpi_video_register);
1995
1996 void acpi_video_unregister(void)
1997 {
1998 mutex_lock(&register_count_mutex);
1999 if (register_count) {
2000 acpi_bus_unregister_driver(&acpi_video_bus);
2001 register_count = 0;
2002 }
2003 mutex_unlock(&register_count_mutex);
2004 }
2005 EXPORT_SYMBOL(acpi_video_unregister);
2006
2007 void acpi_video_unregister_backlight(void)
2008 {
2009 struct acpi_video_bus *video;
2010
2011 mutex_lock(&register_count_mutex);
2012 if (register_count) {
2013 mutex_lock(&video_list_lock);
2014 list_for_each_entry(video, &video_bus_head, entry)
2015 acpi_video_bus_unregister_backlight(video);
2016 mutex_unlock(&video_list_lock);
2017 }
2018 mutex_unlock(&register_count_mutex);
2019 }
2020
2021 /*
2022 * This is kind of nasty. Hardware using Intel chipsets may require
2023 * the video opregion code to be run first in order to initialise
2024 * state before any ACPI video calls are made. To handle this we defer
2025 * registration of the video class until the opregion code has run.
2026 */
2027
2028 static int __init acpi_video_init(void)
2029 {
2030 /*
2031 * Let the module load even if ACPI is disabled (e.g. due to
2032 * a broken BIOS) so that i915.ko can still be loaded on such
2033 * old systems without an AcpiOpRegion.
2034 *
2035 * acpi_video_register() will report -ENODEV later as well due
2036 * to acpi_disabled when i915.ko tries to register itself afterwards.
2037 */
2038 if (acpi_disabled)
2039 return 0;
2040
2041 if (intel_opregion_present())
2042 return 0;
2043
2044 return acpi_video_register();
2045 }
2046
2047 static void __exit acpi_video_exit(void)
2048 {
2049 acpi_video_detect_exit();
2050 acpi_video_unregister();
2051
2052 return;
2053 }
2054
2055 module_init(acpi_video_init);
2056 module_exit(acpi_video_exit);
This page took 0.069903 seconds and 6 git commands to generate.