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