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