Merge branch 'bnxt_en-fixes'
[linux-2.6-block.git] / drivers / acpi / 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 *
1da177e4
LT
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>
bbac81f5 28#include <linux/mutex.h>
e9dab196 29#include <linux/input.h>
2f3d000a 30#include <linux/backlight.h>
702ed512 31#include <linux/thermal.h>
935e5f29 32#include <linux/sort.h>
74a365b3
MG
33#include <linux/pci.h>
34#include <linux/pci_ids.h>
5a0e3ad6 35#include <linux/slab.h>
eb27cae8 36#include <linux/dmi.h>
ac7729da 37#include <linux/suspend.h>
8b48463f 38#include <linux/acpi.h>
e92a7162 39#include <acpi/video.h>
8b48463f 40#include <asm/uaccess.h>
1da177e4 41
14ca7a47 42#define PREFIX "ACPI: "
8c5bd7ad 43
1da177e4
LT
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
f4715189
TT
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
1da177e4 57
2f3d000a 58#define MAX_NAME_LEN 20
1da177e4 59
1da177e4 60#define _COMPONENT ACPI_VIDEO_COMPONENT
f52fd66d 61ACPI_MODULE_NAME("video");
1da177e4 62
f52fd66d 63MODULE_AUTHOR("Bruno Ducrot");
7cda93e0 64MODULE_DESCRIPTION("ACPI Video Driver");
1da177e4
LT
65MODULE_LICENSE("GPL");
66
2843768b 67static bool brightness_switch_enabled = 1;
8a681a4d
ZR
68module_param(brightness_switch_enabled, bool, 0644);
69
c504f8cb
ZR
70/*
71 * By default, we don't allow duplicate ACPI video bus devices
72 * under the same VGA controller
73 */
90ab5ee9 74static bool allow_duplicates;
c504f8cb
ZR
75module_param(allow_duplicates, bool, 0644);
76
654a182d
HG
77static int disable_backlight_sysfs_if = -1;
78module_param(disable_backlight_sysfs_if, int, 0444);
79
915ea7e4 80static int register_count;
2a8b18e9 81static DEFINE_MUTEX(register_count_mutex);
67b662e1
AL
82static struct mutex video_list_lock;
83static struct list_head video_bus_head;
4be44fcd 84static int acpi_video_bus_add(struct acpi_device *device);
51fac838 85static int acpi_video_bus_remove(struct acpi_device *device);
7015558f 86static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
93a291df 87void acpi_video_detect_exit(void);
1da177e4 88
1ba90e3a
TR
89static const struct acpi_device_id video_device_ids[] = {
90 {ACPI_VIDEO_HID, 0},
91 {"", 0},
92};
93MODULE_DEVICE_TABLE(acpi, video_device_ids);
94
1da177e4 95static struct acpi_driver acpi_video_bus = {
c2b6705b 96 .name = "video",
1da177e4 97 .class = ACPI_VIDEO_CLASS,
1ba90e3a 98 .ids = video_device_ids,
1da177e4
LT
99 .ops = {
100 .add = acpi_video_bus_add,
101 .remove = acpi_video_bus_remove,
7015558f 102 .notify = acpi_video_bus_notify,
4be44fcd 103 },
1da177e4
LT
104};
105
106struct acpi_video_bus_flags {
4be44fcd
LB
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;
1da177e4
LT
111};
112
113struct acpi_video_bus_cap {
21fcb34e
FC
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 */
4be44fcd 120 u8 reserved:2;
1da177e4
LT
121};
122
4be44fcd
LB
123struct acpi_video_device_attrib {
124 u32 display_index:4; /* A zero-based instance of the Display */
21fcb34e
FC
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
4be44fcd 130 the VGA device. */
21fcb34e
FC
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 */
1da177e4
LT
134};
135
136struct acpi_video_enumerated_device {
137 union {
138 u32 int_val;
4be44fcd 139 struct acpi_video_device_attrib attrib;
1da177e4
LT
140 } value;
141 struct acpi_video_device *bind_info;
142};
143
144struct acpi_video_bus {
e6afa0de 145 struct acpi_device *device;
99678ed7 146 bool backlight_registered;
4be44fcd 147 u8 dos_setting;
1da177e4 148 struct acpi_video_enumerated_device *attached_array;
4be44fcd 149 u8 attached_count;
b4df4636 150 u8 child_count;
4be44fcd 151 struct acpi_video_bus_cap cap;
1da177e4 152 struct acpi_video_bus_flags flags;
4be44fcd 153 struct list_head video_device_list;
bbac81f5 154 struct mutex device_list_lock; /* protects video_device_list */
67b662e1 155 struct list_head entry;
e9dab196
LY
156 struct input_dev *input;
157 char phys[32]; /* for input device */
ac7729da 158 struct notifier_block pm_nb;
1da177e4
LT
159};
160
161struct acpi_video_device_flags {
4be44fcd
LB
162 u8 crt:1;
163 u8 lcd:1;
164 u8 tvout:1;
82cae999 165 u8 dvi:1;
4be44fcd
LB
166 u8 bios:1;
167 u8 unknown:1;
91e13aa3
AL
168 u8 notify:1;
169 u8 reserved:1;
1da177e4
LT
170};
171
172struct acpi_video_device_cap {
21fcb34e
FC
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 */
2f3d000a 176 u8 _BQC:1; /* Get current brightness level */
c60d638e 177 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
21fcb34e 178 u8 _DDC:1; /* Return the EDID for this device */
1da177e4
LT
179};
180
d32f6947
ZR
181struct acpi_video_brightness_flags {
182 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
21fcb34e 183 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
1a7c618a 184 u8 _BQC_use_index:1; /* _BQC returns an index value */
d32f6947
ZR
185};
186
1da177e4 187struct acpi_video_device_brightness {
4be44fcd
LB
188 int curr;
189 int count;
190 int *levels;
d32f6947 191 struct acpi_video_brightness_flags flags;
1da177e4
LT
192};
193
194struct acpi_video_device {
4be44fcd
LB
195 unsigned long device_id;
196 struct acpi_video_device_flags flags;
197 struct acpi_video_device_cap cap;
198 struct list_head entry;
8ab58e8e
LT
199 struct delayed_work switch_brightness_work;
200 int switch_brightness_event;
4be44fcd
LB
201 struct acpi_video_bus *video;
202 struct acpi_device *dev;
1da177e4 203 struct acpi_video_device_brightness *brightness;
2f3d000a 204 struct backlight_device *backlight;
4a703a8f 205 struct thermal_cooling_device *cooling_dev;
1da177e4
LT
206};
207
b7171ae7 208static const char device_decode[][30] = {
1da177e4
LT
209 "motherboard VGA device",
210 "PCI VGA device",
211 "AGP VGA device",
212 "UNKNOWN",
213};
214
4be44fcd
LB
215static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
216static void acpi_video_device_rebind(struct acpi_video_bus *video);
217static void acpi_video_device_bind(struct acpi_video_bus *video,
218 struct acpi_video_device *device);
1da177e4 219static int acpi_video_device_enumerate(struct acpi_video_bus *video);
2f3d000a
YL
220static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
221 int level);
222static int acpi_video_device_lcd_get_level_current(
223 struct acpi_video_device *device,
a89803df 224 unsigned long long *level, bool raw);
4be44fcd
LB
225static int acpi_video_get_next_level(struct acpi_video_device *device,
226 u32 level_current, u32 event);
8ab58e8e 227static void acpi_video_switch_brightness(struct work_struct *work);
1da177e4 228
21fcb34e 229/* backlight device sysfs support */
2f3d000a
YL
230static int acpi_video_get_brightness(struct backlight_device *bd)
231{
27663c58 232 unsigned long long cur_level;
38531e6f 233 int i;
7c364e79 234 struct acpi_video_device *vd = bl_get_data(bd);
c8890f90 235
a89803df 236 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
c8890f90 237 return -EINVAL;
38531e6f
MG
238 for (i = 2; i < vd->brightness->count; i++) {
239 if (vd->brightness->levels[i] == cur_level)
21fcb34e
FC
240 /*
241 * The first two entries are special - see page 575
242 * of the ACPI spec 3.0
243 */
915ea7e4 244 return i - 2;
38531e6f
MG
245 }
246 return 0;
2f3d000a
YL
247}
248
249static int acpi_video_set_brightness(struct backlight_device *bd)
250{
24450c7a 251 int request_level = bd->props.brightness + 2;
7c364e79 252 struct acpi_video_device *vd = bl_get_data(bd);
24450c7a 253
8ab58e8e 254 cancel_delayed_work(&vd->switch_brightness_work);
24450c7a
ZR
255 return acpi_video_device_lcd_set_level(vd,
256 vd->brightness->levels[request_level]);
2f3d000a
YL
257}
258
acc2472e 259static const struct backlight_ops acpi_backlight_ops = {
599a52d1
RP
260 .get_brightness = acpi_video_get_brightness,
261 .update_status = acpi_video_set_brightness,
262};
263
702ed512 264/* thermal cooling device callbacks */
4a703a8f 265static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
6503e5df 266 long *state)
702ed512 267{
4a703a8f 268 struct acpi_device *device = cooling_dev->devdata;
702ed512
ZR
269 struct acpi_video_device *video = acpi_driver_data(device);
270
6503e5df
MG
271 *state = video->brightness->count - 3;
272 return 0;
702ed512
ZR
273}
274
4a703a8f 275static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
6503e5df 276 long *state)
702ed512 277{
4a703a8f 278 struct acpi_device *device = cooling_dev->devdata;
702ed512 279 struct acpi_video_device *video = acpi_driver_data(device);
27663c58 280 unsigned long long level;
6503e5df 281 int offset;
702ed512 282
a89803df 283 if (acpi_video_device_lcd_get_level_current(video, &level, false))
c8890f90 284 return -EINVAL;
6503e5df
MG
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 }
702ed512
ZR
290
291 return -EINVAL;
292}
293
294static int
4a703a8f 295video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
702ed512 296{
4a703a8f 297 struct acpi_device *device = cooling_dev->devdata;
702ed512
ZR
298 struct acpi_video_device *video = acpi_driver_data(device);
299 int level;
300
915ea7e4 301 if (state >= video->brightness->count - 2)
702ed512
ZR
302 return -EINVAL;
303
304 state = video->brightness->count - state;
915ea7e4 305 level = video->brightness->levels[state - 1];
702ed512
ZR
306 return acpi_video_device_lcd_set_level(video, level);
307}
308
9c8b04be 309static const struct thermal_cooling_device_ops video_cooling_ops = {
702ed512
ZR
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
21fcb34e
FC
315/*
316 * --------------------------------------------------------------------------
317 * Video Management
318 * --------------------------------------------------------------------------
319 */
1da177e4 320
1da177e4 321static int
4be44fcd
LB
322acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
323 union acpi_object **levels)
1da177e4 324{
4be44fcd
LB
325 int status;
326 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
327 union acpi_object *obj;
1da177e4 328
1da177e4
LT
329
330 *levels = NULL;
331
90130268 332 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
1da177e4 333 if (!ACPI_SUCCESS(status))
d550d98d 334 return status;
4be44fcd 335 obj = (union acpi_object *)buffer.pointer;
6665bda7 336 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6468463a 337 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
1da177e4
LT
338 status = -EFAULT;
339 goto err;
340 }
341
342 *levels = obj;
343
d550d98d 344 return 0;
1da177e4 345
915ea7e4 346err:
6044ec88 347 kfree(buffer.pointer);
1da177e4 348
d550d98d 349 return status;
1da177e4
LT
350}
351
352static int
4be44fcd 353acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
1da177e4 354{
24450c7a 355 int status;
9e6dada9 356 int state;
1da177e4 357
0db98202
JL
358 status = acpi_execute_simple_method(device->dev->handle,
359 "_BCM", level);
24450c7a
ZR
360 if (ACPI_FAILURE(status)) {
361 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
362 return -EIO;
363 }
364
4500ca8e 365 device->brightness->curr = level;
9e6dada9 366 for (state = 2; state < device->brightness->count; state++)
24450c7a 367 if (level == device->brightness->levels[state]) {
1a7c618a
ZR
368 if (device->backlight)
369 device->backlight->props.brightness = state - 2;
24450c7a
ZR
370 return 0;
371 }
9e6dada9 372
24450c7a
ZR
373 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
374 return -EINVAL;
1da177e4
LT
375}
376
45cb50e6
ZR
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
382static int bqc_offset_aml_bug_workaround;
7ee33baa 383static int video_set_bqc_offset(const struct dmi_system_id *d)
45cb50e6
ZR
384{
385 bqc_offset_aml_bug_workaround = 9;
386 return 0;
387}
388
7ee33baa 389static int video_disable_backlight_sysfs_if(
654a182d
HG
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
7ee33baa 397static struct dmi_system_id video_dmi_table[] = {
45cb50e6
ZR
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 },
5afc4abe
LB
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 },
34ac272b
ZR
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 },
93bcece2
ZR
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 },
152a4e63
ZR
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 },
5f24079b 441
654a182d
HG
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 },
45cb50e6
ZR
458 {}
459};
460
994fa63c
DB
461static unsigned long long
462acpi_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
1da177e4 486static int
4be44fcd 487acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
a89803df 488 unsigned long long *level, bool raw)
1da177e4 489{
c8890f90 490 acpi_status status = AE_OK;
4e231fa4 491 int i;
c8890f90 492
c60d638e
ZR
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,
c8890f90
ZR
497 NULL, level);
498 if (ACPI_SUCCESS(status)) {
a89803df
DB
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
994fa63c 508 *level = acpi_video_bqc_value_to_level(device, *level);
1a7c618a 509
4e231fa4
VS
510 for (i = 2; i < device->brightness->count; i++)
511 if (device->brightness->levels[i] == *level) {
512 device->brightness->curr = *level;
513 return 0;
915ea7e4 514 }
a89803df
DB
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;
c8890f90 523 } else {
21fcb34e
FC
524 /*
525 * Fixme:
c8890f90
ZR
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 */
c60d638e
ZR
532 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
533 device->cap._BQC = device->cap._BCQ = 0;
c8890f90
ZR
534 }
535 }
536
4500ca8e 537 *level = device->brightness->curr;
c8890f90 538 return 0;
1da177e4
LT
539}
540
541static int
4be44fcd
LB
542acpi_video_device_EDID(struct acpi_video_device *device,
543 union acpi_object **edid, ssize_t length)
1da177e4 544{
4be44fcd
LB
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 };
1da177e4 550
1da177e4
LT
551
552 *edid = NULL;
553
554 if (!device)
d550d98d 555 return -ENODEV;
1da177e4
LT
556 if (length == 128)
557 arg0.integer.value = 1;
558 else if (length == 256)
559 arg0.integer.value = 2;
560 else
d550d98d 561 return -EINVAL;
1da177e4 562
90130268 563 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
1da177e4 564 if (ACPI_FAILURE(status))
d550d98d 565 return -ENODEV;
1da177e4 566
50dd0969 567 obj = buffer.pointer;
1da177e4
LT
568
569 if (obj && obj->type == ACPI_TYPE_BUFFER)
570 *edid = obj;
571 else {
6468463a 572 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
1da177e4
LT
573 status = -EFAULT;
574 kfree(obj);
575 }
576
d550d98d 577 return status;
1da177e4
LT
578}
579
1da177e4
LT
580/* bus */
581
1da177e4
LT
582/*
583 * Arg:
21fcb34e
FC
584 * video : video bus device pointer
585 * bios_flag :
1da177e4
LT
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
98fb8fe1 589 * active display output. No switch event.
1da177e4
LT
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
98fb8fe1 596 * of the LCD when the power changes from AC to DC
21fcb34e 597 * 1. The system BIOS should NOT automatically control the brightness
98fb8fe1 598 * level of the LCD when the power changes from AC to DC.
21fcb34e 599 * Return Value:
ea9f8856 600 * -EINVAL wrong arg.
1da177e4
LT
601 */
602
603static int
4be44fcd 604acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
1da177e4 605{
ea9f8856 606 acpi_status status;
1da177e4 607
b0373843
ZR
608 if (!video->cap._DOS)
609 return 0;
1da177e4 610
ea9f8856
IM
611 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
612 return -EINVAL;
0db98202
JL
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);
ea9f8856
IM
616 if (ACPI_FAILURE(status))
617 return -EIO;
1da177e4 618
ea9f8856 619 return 0;
1da177e4
LT
620}
621
935e5f29
ZR
622/*
623 * Simple comparison function used to sort backlight levels.
624 */
625
626static int
627acpi_video_cmp_level(const void *a, const void *b)
628{
629 return *(int *)a - *(int *)b;
630}
631
a50188da
AL
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 */
639static 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 */
b3b301c5 655 test_level = current_level == max_level ? br->levels[3] : max_level;
a50188da
AL
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
1da177e4 682/*
21fcb34e
FC
683 * Arg:
684 * device : video output device (LCD, CRT, ..)
1da177e4
LT
685 *
686 * Return Value:
469778c1
JJ
687 * Maximum brightness level
688 *
689 * Allocate and initialize device->brightness.
690 */
691
692static int
693acpi_video_init_brightness(struct acpi_video_device *device)
694{
695 union acpi_object *obj = NULL;
d32f6947 696 int i, max_level = 0, count = 0, level_ac_battery = 0;
1a7c618a 697 unsigned long long level, level_old;
469778c1
JJ
698 union acpi_object *o;
699 struct acpi_video_device_brightness *br = NULL;
1a7c618a 700 int result = -EINVAL;
bd8ba205 701 u32 value;
469778c1
JJ
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");
1a7c618a 715 result = -ENOMEM;
469778c1
JJ
716 goto out;
717 }
718
d32f6947 719 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
469778c1 720 GFP_KERNEL);
1a7c618a
ZR
721 if (!br->levels) {
722 result = -ENOMEM;
469778c1 723 goto out_free;
1a7c618a 724 }
469778c1
JJ
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 }
bd8ba205
HG
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;
469778c1
JJ
738
739 if (br->levels[count] > max_level)
740 max_level = br->levels[count];
741 count++;
742 }
743
d32f6947
ZR
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 */
90af2cf6
ZR
750 for (i = 2; i < count; i++) {
751 if (br->levels[i] == br->levels[0])
d32f6947 752 level_ac_battery++;
90af2cf6
ZR
753 if (br->levels[i] == br->levels[1])
754 level_ac_battery++;
755 }
d32f6947
ZR
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)
bf6787eb 764 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
d32f6947 765
d80fb99f
ZR
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,
bf6787eb 773 "Found unordered _BCL package"));
469778c1
JJ
774
775 br->count = count;
776 device->brightness = br;
1a7c618a 777
1a7c618a 778 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
90c53ca4 779 br->curr = level = max_level;
e047cca6
ZR
780
781 if (!device->cap._BQC)
782 goto set_level;
783
a89803df
DB
784 result = acpi_video_device_lcd_get_level_current(device,
785 &level_old, true);
1a7c618a
ZR
786 if (result)
787 goto out_free_levels;
788
a50188da 789 result = acpi_video_bqc_quirk(device, max_level, level_old);
1a7c618a
ZR
790 if (result)
791 goto out_free_levels;
a50188da
AL
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;
e047cca6 798
545ef368
AL
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;
e047cca6 811
e047cca6 812set_level:
90c53ca4 813 result = acpi_video_device_lcd_set_level(device, level);
e047cca6
ZR
814 if (result)
815 goto out_free_levels;
1a7c618a 816
d32f6947
ZR
817 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
818 "found %d brightness levels\n", count - 2));
469778c1 819 kfree(obj);
1a7c618a 820 return result;
469778c1
JJ
821
822out_free_levels:
823 kfree(br->levels);
824out_free:
825 kfree(br);
826out:
827 device->brightness = NULL;
828 kfree(obj);
1a7c618a 829 return result;
469778c1
JJ
830}
831
832/*
833 * Arg:
834 * device : video output device (LCD, CRT, ..)
835 *
836 * Return Value:
21fcb34e 837 * None
1da177e4 838 *
98fb8fe1 839 * Find out all required AML methods defined under the output
1da177e4
LT
840 * device.
841 */
842
4be44fcd 843static void acpi_video_device_find_cap(struct acpi_video_device *device)
1da177e4 844{
952c63e9 845 if (acpi_has_method(device->dev->handle, "_ADR"))
1da177e4 846 device->cap._ADR = 1;
952c63e9 847 if (acpi_has_method(device->dev->handle, "_BCL"))
4be44fcd 848 device->cap._BCL = 1;
952c63e9 849 if (acpi_has_method(device->dev->handle, "_BCM"))
4be44fcd 850 device->cap._BCM = 1;
952c63e9 851 if (acpi_has_method(device->dev->handle, "_BQC")) {
2f3d000a 852 device->cap._BQC = 1;
952c63e9 853 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
c60d638e
ZR
854 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
855 device->cap._BCQ = 1;
856 }
857
952c63e9 858 if (acpi_has_method(device->dev->handle, "_DDC"))
4be44fcd 859 device->cap._DDC = 1;
1da177e4
LT
860}
861
862/*
21fcb34e
FC
863 * Arg:
864 * device : video output device (VGA)
1da177e4
LT
865 *
866 * Return Value:
21fcb34e 867 * None
1da177e4 868 *
98fb8fe1 869 * Find out all required AML methods defined under the video bus device.
1da177e4
LT
870 */
871
4be44fcd 872static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1da177e4 873{
952c63e9 874 if (acpi_has_method(video->device->handle, "_DOS"))
1da177e4 875 video->cap._DOS = 1;
952c63e9 876 if (acpi_has_method(video->device->handle, "_DOD"))
1da177e4 877 video->cap._DOD = 1;
952c63e9 878 if (acpi_has_method(video->device->handle, "_ROM"))
1da177e4 879 video->cap._ROM = 1;
952c63e9 880 if (acpi_has_method(video->device->handle, "_GPD"))
1da177e4 881 video->cap._GPD = 1;
952c63e9 882 if (acpi_has_method(video->device->handle, "_SPD"))
1da177e4 883 video->cap._SPD = 1;
952c63e9 884 if (acpi_has_method(video->device->handle, "_VPO"))
1da177e4 885 video->cap._VPO = 1;
1da177e4
LT
886}
887
888/*
889 * Check whether the video bus device has required AML method to
890 * support the desired features
891 */
892
4be44fcd 893static int acpi_video_bus_check(struct acpi_video_bus *video)
1da177e4 894{
4be44fcd 895 acpi_status status = -ENOENT;
1e4cffe7 896 struct pci_dev *dev;
1da177e4
LT
897
898 if (!video)
d550d98d 899 return -EINVAL;
1da177e4 900
1e4cffe7 901 dev = acpi_get_pci_dev(video->device->handle);
22c13f9d
TR
902 if (!dev)
903 return -ENODEV;
1e4cffe7 904 pci_dev_put(dev);
22c13f9d 905
21fcb34e
FC
906 /*
907 * Since there is no HID, CID and so on for VGA driver, we have
1da177e4
LT
908 * to check well known required nodes.
909 */
910
98fb8fe1 911 /* Does this device support video switching? */
3a1151e3
SB
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 }
1da177e4
LT
918 video->flags.multihead = 1;
919 status = 0;
920 }
921
98fb8fe1 922 /* Does this device support retrieving a video ROM? */
4be44fcd 923 if (video->cap._ROM) {
1da177e4
LT
924 video->flags.rom = 1;
925 status = 0;
926 }
927
98fb8fe1 928 /* Does this device support configuring which video device to POST? */
4be44fcd 929 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1da177e4
LT
930 video->flags.post = 1;
931 status = 0;
932 }
933
d550d98d 934 return status;
1da177e4
LT
935}
936
21fcb34e
FC
937/*
938 * --------------------------------------------------------------------------
939 * Driver Interface
940 * --------------------------------------------------------------------------
941 */
1da177e4
LT
942
943/* device interface */
915ea7e4 944static struct acpi_video_device_attrib *
82cae999
RZ
945acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
946{
78eed028
DT
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 }
82cae999 955
82cae999
RZ
956 return NULL;
957}
1da177e4 958
e92a7162
MG
959static int
960acpi_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
1da177e4 975static int
4be44fcd
LB
976acpi_video_bus_get_one_device(struct acpi_device *device,
977 struct acpi_video_bus *video)
1da177e4 978{
27663c58 979 unsigned long long device_id;
e92a7162 980 int status, device_type;
4be44fcd 981 struct acpi_video_device *data;
915ea7e4 982 struct acpi_video_device_attrib *attribute;
1da177e4 983
4be44fcd
LB
984 status =
985 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
91e13aa3
AL
986 /* Some device omits _ADR, we skip them instead of fail */
987 if (ACPI_FAILURE(status))
988 return 0;
1da177e4 989
91e13aa3
AL
990 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
991 if (!data)
992 return -ENOMEM;
82cae999 993
91e13aa3
AL
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;
8ab58e8e
LT
1001 INIT_DELAYED_WORK(&data->switch_brightness_work,
1002 acpi_video_switch_brightness);
91e13aa3
AL
1003
1004 attribute = acpi_video_get_device_attr(video, device_id);
1005
915ea7e4 1006 if (attribute && attribute->device_id_scheme) {
91e13aa3
AL
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 }
915ea7e4 1024 if (attribute->bios_can_detect)
91e13aa3
AL
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) {
915ea7e4
FC
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;
e92a7162 1042 }
91e13aa3 1043 }
4be44fcd 1044
91e13aa3
AL
1045 acpi_video_device_bind(video, data);
1046 acpi_video_device_find_cap(data);
1da177e4 1047
91e13aa3
AL
1048 mutex_lock(&video->device_list_lock);
1049 list_add_tail(&data->entry, &video->video_device_list);
1050 mutex_unlock(&video->device_list_lock);
1da177e4 1051
91e13aa3 1052 return status;
1da177e4
LT
1053}
1054
1055/*
1056 * Arg:
21fcb34e 1057 * video : video bus device
1da177e4
LT
1058 *
1059 * Return:
21fcb34e
FC
1060 * none
1061 *
1062 * Enumerate the video device list of the video bus,
1da177e4
LT
1063 * bind the ids with the corresponding video devices
1064 * under the video bus.
4be44fcd 1065 */
1da177e4 1066
4be44fcd 1067static void acpi_video_device_rebind(struct acpi_video_bus *video)
1da177e4 1068{
ff102ea9
DT
1069 struct acpi_video_device *dev;
1070
bbac81f5 1071 mutex_lock(&video->device_list_lock);
ff102ea9
DT
1072
1073 list_for_each_entry(dev, &video->video_device_list, entry)
4be44fcd 1074 acpi_video_device_bind(video, dev);
ff102ea9 1075
bbac81f5 1076 mutex_unlock(&video->device_list_lock);
1da177e4
LT
1077}
1078
1079/*
1080 * Arg:
21fcb34e
FC
1081 * video : video bus device
1082 * device : video output device under the video
1083 * bus
1da177e4
LT
1084 *
1085 * Return:
21fcb34e
FC
1086 * none
1087 *
1da177e4
LT
1088 * Bind the ids with the corresponding video devices
1089 * under the video bus.
4be44fcd 1090 */
1da177e4
LT
1091
1092static void
4be44fcd
LB
1093acpi_video_device_bind(struct acpi_video_bus *video,
1094 struct acpi_video_device *device)
1da177e4 1095{
78eed028 1096 struct acpi_video_enumerated_device *ids;
4be44fcd 1097 int i;
1da177e4 1098
78eed028
DT
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;
1da177e4
LT
1103 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1104 }
1105 }
1da177e4
LT
1106}
1107
0b8db271
AL
1108static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1109{
1110 struct acpi_video_bus *video = device->video;
1111 int i;
1112
b4df4636
AL
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)
0b8db271
AL
1119 return true;
1120
1121 for (i = 0; i < video->attached_count; i++) {
35d0565b
AL
1122 if ((video->attached_array[i].value.int_val & 0xfff) ==
1123 (device->device_id & 0xfff))
0b8db271
AL
1124 return true;
1125 }
1126
1127 return false;
1128}
1129
1da177e4
LT
1130/*
1131 * Arg:
21fcb34e 1132 * video : video bus device
1da177e4
LT
1133 *
1134 * Return:
21fcb34e
FC
1135 * < 0 : error
1136 *
1da177e4
LT
1137 * Call _DOD to enumerate all devices attached to display adapter
1138 *
4be44fcd 1139 */
1da177e4
LT
1140
1141static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1142{
4be44fcd
LB
1143 int status;
1144 int count;
1145 int i;
78eed028 1146 struct acpi_video_enumerated_device *active_list;
4be44fcd
LB
1147 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1148 union acpi_object *dod = NULL;
1149 union acpi_object *obj;
1da177e4 1150
90130268 1151 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1da177e4 1152 if (!ACPI_SUCCESS(status)) {
a6fc6720 1153 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
d550d98d 1154 return status;
1da177e4
LT
1155 }
1156
50dd0969 1157 dod = buffer.pointer;
1da177e4 1158 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
a6fc6720 1159 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1da177e4
LT
1160 status = -EFAULT;
1161 goto out;
1162 }
1163
1164 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
4be44fcd 1165 dod->package.count));
1da177e4 1166
78eed028
DT
1167 active_list = kcalloc(1 + dod->package.count,
1168 sizeof(struct acpi_video_enumerated_device),
1169 GFP_KERNEL);
1170 if (!active_list) {
1da177e4
LT
1171 status = -ENOMEM;
1172 goto out;
1173 }
1174
1175 count = 0;
1176 for (i = 0; i < dod->package.count; i++) {
50dd0969 1177 obj = &dod->package.elements[i];
1da177e4
LT
1178
1179 if (obj->type != ACPI_TYPE_INTEGER) {
78eed028
DT
1180 printk(KERN_ERR PREFIX
1181 "Invalid _DOD data in element %d\n", i);
1182 continue;
1da177e4 1183 }
78eed028
DT
1184
1185 active_list[count].value.int_val = obj->integer.value;
1186 active_list[count].bind_info = NULL;
4be44fcd
LB
1187 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1188 (int)obj->integer.value));
1da177e4
LT
1189 count++;
1190 }
1da177e4 1191
6044ec88 1192 kfree(video->attached_array);
4be44fcd 1193
78eed028 1194 video->attached_array = active_list;
1da177e4 1195 video->attached_count = count;
78eed028 1196
915ea7e4 1197out:
02438d87 1198 kfree(buffer.pointer);
d550d98d 1199 return status;
1da177e4
LT
1200}
1201
4be44fcd
LB
1202static int
1203acpi_video_get_next_level(struct acpi_video_device *device,
1204 u32 level_current, u32 event)
1da177e4 1205{
63f0edfc 1206 int min, max, min_above, max_below, i, l, delta = 255;
f4715189
TT
1207 max = max_below = 0;
1208 min = min_above = 255;
63f0edfc 1209 /* Find closest level to level_current */
0a3db1ce 1210 for (i = 2; i < device->brightness->count; i++) {
63f0edfc
AS
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;
0a3db1ce 1220 for (i = 2; i < device->brightness->count; i++) {
f4715189
TT
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 }
1da177e4
LT
1245}
1246
8ab58e8e
LT
1247static void
1248acpi_video_switch_brightness(struct work_struct *work)
1da177e4 1249{
8ab58e8e
LT
1250 struct acpi_video_device *device = container_of(to_delayed_work(work),
1251 struct acpi_video_device, switch_brightness_work);
27663c58 1252 unsigned long long level_current, level_next;
8ab58e8e 1253 int event = device->switch_brightness_event;
c8890f90
ZR
1254 int result = -EINVAL;
1255
fbc9fe1b 1256 /* no warning message if acpi_backlight=vendor or a quirk is used */
654a182d 1257 if (!device->backlight)
8ab58e8e 1258 return;
28c32e99 1259
469778c1 1260 if (!device->brightness)
c8890f90
ZR
1261 goto out;
1262
1263 result = acpi_video_device_lcd_get_level_current(device,
a89803df
DB
1264 &level_current,
1265 false);
c8890f90
ZR
1266 if (result)
1267 goto out;
1268
1da177e4 1269 level_next = acpi_video_get_next_level(device, level_current, event);
c8890f90 1270
24450c7a 1271 result = acpi_video_device_lcd_set_level(device, level_next);
c8890f90 1272
36342742
MG
1273 if (!result)
1274 backlight_force_update(device->backlight,
1275 BACKLIGHT_UPDATE_HOTKEY);
1276
c8890f90
ZR
1277out:
1278 if (result)
1279 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1da177e4
LT
1280}
1281
e92a7162
MG
1282int 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
82069552
ZR
1303 if (!video_device->cap._DDC)
1304 continue;
1305
e92a7162
MG
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}
1348EXPORT_SYMBOL(acpi_video_get_edid);
1349
1da177e4 1350static int
4be44fcd
LB
1351acpi_video_bus_get_devices(struct acpi_video_bus *video,
1352 struct acpi_device *device)
1da177e4 1353{
fba4e087 1354 int status = 0;
ff102ea9 1355 struct acpi_device *dev;
1da177e4 1356
fba4e087
IM
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);
1da177e4 1363
ff102ea9 1364 list_for_each_entry(dev, &device->children, node) {
1da177e4
LT
1365
1366 status = acpi_video_bus_get_one_device(dev, video);
ea9f8856 1367 if (status) {
91e13aa3
AL
1368 dev_err(&dev->dev, "Can't attach device\n");
1369 break;
1da177e4 1370 }
b4df4636 1371 video->child_count++;
1da177e4 1372 }
d550d98d 1373 return status;
1da177e4
LT
1374}
1375
1da177e4
LT
1376/* acpi_video interface */
1377
efaa14c7
AL
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 */
4be44fcd 1382static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1da177e4 1383{
efaa14c7 1384 return acpi_video_bus_DOS(video, 0,
fbc9fe1b 1385 acpi_osi_is_win8() ? 1 : 0);
1da177e4
LT
1386}
1387
4be44fcd 1388static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1da177e4 1389{
efaa14c7 1390 return acpi_video_bus_DOS(video, 0,
fbc9fe1b 1391 acpi_osi_is_win8() ? 0 : 1);
1da177e4
LT
1392}
1393
7015558f 1394static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1da177e4 1395{
7015558f 1396 struct acpi_video_bus *video = acpi_driver_data(device);
e9dab196 1397 struct input_dev *input;
17c452f9 1398 int keycode = 0;
e9dab196 1399
67b662e1 1400 if (!video || !video->input)
d550d98d 1401 return;
1da177e4 1402
e9dab196 1403 input = video->input;
1da177e4
LT
1404
1405 switch (event) {
98fb8fe1 1406 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1da177e4 1407 * most likely via hotkey. */
8a37c65d 1408 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
1409 break;
1410
98fb8fe1 1411 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1da177e4
LT
1412 * connector. */
1413 acpi_video_device_enumerate(video);
1414 acpi_video_device_rebind(video);
e9dab196 1415 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
1416 break;
1417
4be44fcd 1418 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
e9dab196
LY
1419 keycode = KEY_SWITCHVIDEOMODE;
1420 break;
4be44fcd 1421 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
e9dab196
LY
1422 keycode = KEY_VIDEO_NEXT;
1423 break;
4be44fcd 1424 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
e9dab196 1425 keycode = KEY_VIDEO_PREV;
1da177e4
LT
1426 break;
1427
1428 default:
1429 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1430 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1431 break;
1432 }
1433
8a37c65d
LT
1434 if (acpi_notifier_call_chain(device, event, 0))
1435 /* Something vetoed the keypress. */
1436 keycode = 0;
17c452f9
MG
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 }
e9dab196 1444
d550d98d 1445 return;
1da177e4
LT
1446}
1447
8ab58e8e
LT
1448static 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
4be44fcd 1458static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1da177e4 1459{
50dd0969 1460 struct acpi_video_device *video_device = data;
4be44fcd 1461 struct acpi_device *device = NULL;
e9dab196
LY
1462 struct acpi_video_bus *bus;
1463 struct input_dev *input;
17c452f9 1464 int keycode = 0;
1da177e4 1465
1da177e4 1466 if (!video_device)
d550d98d 1467 return;
1da177e4 1468
e6afa0de 1469 device = video_device->dev;
e9dab196
LY
1470 bus = video_device->video;
1471 input = bus->input;
1da177e4
LT
1472
1473 switch (event) {
4be44fcd 1474 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
8ab58e8e 1475 brightness_switch_event(video_device, event);
e9dab196
LY
1476 keycode = KEY_BRIGHTNESS_CYCLE;
1477 break;
4be44fcd 1478 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
8ab58e8e 1479 brightness_switch_event(video_device, event);
e9dab196
LY
1480 keycode = KEY_BRIGHTNESSUP;
1481 break;
4be44fcd 1482 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
8ab58e8e 1483 brightness_switch_event(video_device, event);
e9dab196
LY
1484 keycode = KEY_BRIGHTNESSDOWN;
1485 break;
70f23fd6 1486 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
8ab58e8e 1487 brightness_switch_event(video_device, event);
e9dab196
LY
1488 keycode = KEY_BRIGHTNESS_ZERO;
1489 break;
4be44fcd 1490 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
8ab58e8e 1491 brightness_switch_event(video_device, event);
e9dab196 1492 keycode = KEY_DISPLAY_OFF;
1da177e4
LT
1493 break;
1494 default:
1495 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1496 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1497 break;
1498 }
e9dab196 1499
7761f638 1500 acpi_notifier_call_chain(device, event, 0);
17c452f9
MG
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 }
e9dab196 1508
d550d98d 1509 return;
1da177e4
LT
1510}
1511
ac7729da
RW
1512static int acpi_video_resume(struct notifier_block *nb,
1513 unsigned long val, void *ign)
863c1490
MG
1514{
1515 struct acpi_video_bus *video;
1516 struct acpi_video_device *video_device;
1517 int i;
1518
ac7729da
RW
1519 switch (val) {
1520 case PM_HIBERNATION_PREPARE:
1521 case PM_SUSPEND_PREPARE:
1522 case PM_RESTORE_PREPARE:
1523 return NOTIFY_DONE;
1524 }
863c1490 1525
ac7729da
RW
1526 video = container_of(nb, struct acpi_video_bus, pm_nb);
1527
1528 dev_info(&video->device->dev, "Restoring backlight state\n");
863c1490
MG
1529
1530 for (i = 0; i < video->attached_count; i++) {
1531 video_device = video->attached_array[i].bind_info;
654a182d
HG
1532 if (video_device && video_device->brightness)
1533 acpi_video_device_lcd_set_level(video_device,
1534 video_device->brightness->curr);
863c1490 1535 }
ac7729da
RW
1536
1537 return NOTIFY_OK;
863c1490
MG
1538}
1539
c504f8cb
ZR
1540static acpi_status
1541acpi_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
67b662e1
AL
1561static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1562{
99678ed7
HG
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;
67b662e1 1570
0b8db271
AL
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
99678ed7
HG
1580 result = acpi_video_init_brightness(device);
1581 if (result)
1582 return;
654a182d
HG
1583
1584 if (disable_backlight_sysfs_if > 0)
1585 return;
1586
99678ed7
HG
1587 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1588 if (!name)
1589 return;
1590 count++;
67b662e1 1591
99678ed7 1592 acpi_get_parent(device->dev->handle, &acpi_parent);
67b662e1 1593
99678ed7
HG
1594 pdev = acpi_get_pci_dev(acpi_parent);
1595 if (pdev) {
1596 parent = &pdev->dev;
1597 pci_dev_put(pdev);
1598 }
67b662e1 1599
99678ed7
HG
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);
654a182d
HG
1609 if (IS_ERR(device->backlight)) {
1610 device->backlight = NULL;
99678ed7 1611 return;
654a182d 1612 }
67b662e1 1613
99678ed7
HG
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);
67b662e1 1620
99678ed7
HG
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;
67b662e1 1632 }
99678ed7
HG
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");
67b662e1
AL
1645}
1646
dce4ec2e
AL
1647static 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
67b662e1
AL
1660static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1661{
1662 struct acpi_video_device *dev;
1663
53a92ba7
HG
1664 if (video->backlight_registered)
1665 return 0;
1666
dce4ec2e
AL
1667 acpi_video_run_bcl_for_osi(video);
1668
3bd6bce3 1669 if (acpi_video_get_backlight_type() != acpi_backlight_video)
99678ed7
HG
1670 return 0;
1671
67b662e1
AL
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
99678ed7
HG
1677 video->backlight_registered = true;
1678
67b662e1
AL
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
1684static 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
1703static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1704{
1705 struct acpi_video_device *dev;
99678ed7
HG
1706 int error;
1707
1708 if (!video->backlight_registered)
1709 return 0;
1710
1711 error = unregister_pm_notifier(&video->pm_nb);
67b662e1
AL
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
99678ed7
HG
1718 video->backlight_registered = false;
1719
67b662e1
AL
1720 return error;
1721}
1722
1723static 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
1736static 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
1781err_stop_dev:
1782 acpi_video_bus_stop_devices(video);
1783err_free_input:
1784 input_free_device(input);
1785 video->input = NULL;
1786out:
1787 return error;
1788}
1789
1790static 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
1799static 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
1813static 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
ac7729da
RW
1827static int instance;
1828
4be44fcd 1829static int acpi_video_bus_add(struct acpi_device *device)
1da177e4 1830{
f51e8391 1831 struct acpi_video_bus *video;
f51e8391 1832 int error;
c504f8cb
ZR
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 }
1da177e4 1848
36bcbec7 1849 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1da177e4 1850 if (!video)
d550d98d 1851 return -ENOMEM;
1da177e4 1852
e6d9da1d
ZR
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;
915ea7e4 1857 instance++;
e6d9da1d 1858 }
f3b39f13
ZY
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 }
e6d9da1d 1865
e6afa0de 1866 video->device = device;
1da177e4
LT
1867 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1868 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
db89b4f0 1869 device->driver_data = video;
1da177e4
LT
1870
1871 acpi_video_bus_find_cap(video);
f51e8391
DT
1872 error = acpi_video_bus_check(video);
1873 if (error)
1874 goto err_free_video;
1da177e4 1875
bbac81f5 1876 mutex_init(&video->device_list_lock);
1da177e4
LT
1877 INIT_LIST_HEAD(&video->video_device_list);
1878
ea9f8856
IM
1879 error = acpi_video_bus_get_devices(video, device);
1880 if (error)
91e13aa3 1881 goto err_put_video;
1da177e4 1882
1da177e4 1883 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
4be44fcd
LB
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");
67b662e1
AL
1888 mutex_lock(&video_list_lock);
1889 list_add_tail(&video->entry, &video_bus_head);
1890 mutex_unlock(&video_list_lock);
1da177e4 1891
67b662e1
AL
1892 acpi_video_bus_register_backlight(video);
1893 acpi_video_bus_add_notify_handler(video);
ac7729da 1894
f51e8391
DT
1895 return 0;
1896
67b662e1 1897err_put_video:
f51e8391
DT
1898 acpi_video_bus_put_devices(video);
1899 kfree(video->attached_array);
67b662e1 1900err_free_video:
f51e8391 1901 kfree(video);
db89b4f0 1902 device->driver_data = NULL;
1da177e4 1903
f51e8391 1904 return error;
1da177e4
LT
1905}
1906
51fac838 1907static int acpi_video_bus_remove(struct acpi_device *device)
1da177e4 1908{
4be44fcd 1909 struct acpi_video_bus *video = NULL;
1da177e4 1910
1da177e4
LT
1911
1912 if (!device || !acpi_driver_data(device))
d550d98d 1913 return -EINVAL;
1da177e4 1914
50dd0969 1915 video = acpi_driver_data(device);
1da177e4 1916
67b662e1
AL
1917 acpi_video_bus_remove_notify_handler(video);
1918 acpi_video_bus_unregister_backlight(video);
1da177e4 1919 acpi_video_bus_put_devices(video);
1da177e4 1920
67b662e1
AL
1921 mutex_lock(&video_list_lock);
1922 list_del(&video->entry);
1923 mutex_unlock(&video_list_lock);
1924
6044ec88 1925 kfree(video->attached_array);
1da177e4
LT
1926 kfree(video);
1927
d550d98d 1928 return 0;
1da177e4
LT
1929}
1930
c6996bdd
AC
1931static 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
74a365b3
MG
1940static int __init intel_opregion_present(void)
1941{
c6996bdd 1942 int opregion = 0;
74a365b3
MG
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;
c6996bdd
AC
1951 /* We don't want to poke around undefined i740 registers */
1952 if (is_i740(dev))
1953 continue;
74a365b3
MG
1954 pci_read_config_dword(dev, 0xfc, &address);
1955 if (!address)
1956 continue;
c6996bdd 1957 opregion = 1;
74a365b3 1958 }
c6996bdd 1959 return opregion;
74a365b3
MG
1960}
1961
8e5c2b77 1962int acpi_video_register(void)
1da177e4 1963{
2a8b18e9 1964 int ret = 0;
28d63403 1965
2a8b18e9 1966 mutex_lock(&register_count_mutex);
86e437f0
ZY
1967 if (register_count) {
1968 /*
8e5c2b77
RW
1969 * if the function of acpi_video_register is already called,
1970 * don't register the acpi_vide_bus again and return no error.
86e437f0 1971 */
2a8b18e9 1972 goto leave;
86e437f0 1973 }
1da177e4 1974
67b662e1
AL
1975 mutex_init(&video_list_lock);
1976 INIT_LIST_HEAD(&video_bus_head);
1977
7ee33baa
HG
1978 dmi_check_system(video_dmi_table);
1979
28d63403
CW
1980 ret = acpi_bus_register_driver(&acpi_video_bus);
1981 if (ret)
2a8b18e9 1982 goto leave;
1da177e4 1983
86e437f0
ZY
1984 /*
1985 * When the acpi_video_bus is loaded successfully, increase
1986 * the counter reference.
1987 */
1988 register_count = 1;
1989
2a8b18e9
HG
1990leave:
1991 mutex_unlock(&register_count_mutex);
1992 return ret;
1da177e4 1993}
8e5c2b77 1994EXPORT_SYMBOL(acpi_video_register);
74a365b3 1995
86e437f0
ZY
1996void acpi_video_unregister(void)
1997{
2a8b18e9
HG
1998 mutex_lock(&register_count_mutex);
1999 if (register_count) {
2000 acpi_bus_unregister_driver(&acpi_video_bus);
2001 register_count = 0;
86e437f0 2002 }
2a8b18e9 2003 mutex_unlock(&register_count_mutex);
86e437f0
ZY
2004}
2005EXPORT_SYMBOL(acpi_video_unregister);
2006
1b7f37e1
HG
2007void acpi_video_unregister_backlight(void)
2008{
2009 struct acpi_video_bus *video;
2010
2a8b18e9
HG
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);
1b7f37e1 2019}
1b7f37e1 2020
74a365b3
MG
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
2028static int __init acpi_video_init(void)
2029{
6e17cb12
CW
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
74a365b3
MG
2041 if (intel_opregion_present())
2042 return 0;
2043
2044 return acpi_video_register();
2045}
1da177e4 2046
86e437f0 2047static void __exit acpi_video_exit(void)
1da177e4 2048{
93a291df 2049 acpi_video_detect_exit();
86e437f0 2050 acpi_video_unregister();
1da177e4 2051
d550d98d 2052 return;
1da177e4
LT
2053}
2054
2055module_init(acpi_video_init);
2056module_exit(acpi_video_exit);