]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/acpi/video.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-2.6-mn10300
[mirror_ubuntu-artful-kernel.git] / drivers / acpi / video.c
CommitLineData
1da177e4
LT
1/*
2 * video.c - ACPI Video Driver ($Revision:$)
3 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
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>
1da177e4
LT
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
e9dab196 35#include <linux/input.h>
2f3d000a 36#include <linux/backlight.h>
702ed512 37#include <linux/thermal.h>
23b0f015 38#include <linux/video_output.h>
935e5f29 39#include <linux/sort.h>
74a365b3
MG
40#include <linux/pci.h>
41#include <linux/pci_ids.h>
1da177e4
LT
42#include <asm/uaccess.h>
43
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46
1da177e4 47#define ACPI_VIDEO_CLASS "video"
1da177e4
LT
48#define ACPI_VIDEO_BUS_NAME "Video Bus"
49#define ACPI_VIDEO_DEVICE_NAME "Video Device"
50#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
51#define ACPI_VIDEO_NOTIFY_PROBE 0x81
52#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
53#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
54#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
55
f4715189
TT
56#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
57#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
58#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
59#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
60#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
1da177e4 61
2f3d000a 62#define MAX_NAME_LEN 20
1da177e4 63
82cae999
RZ
64#define ACPI_VIDEO_DISPLAY_CRT 1
65#define ACPI_VIDEO_DISPLAY_TV 2
66#define ACPI_VIDEO_DISPLAY_DVI 3
67#define ACPI_VIDEO_DISPLAY_LCD 4
68
1da177e4 69#define _COMPONENT ACPI_VIDEO_COMPONENT
f52fd66d 70ACPI_MODULE_NAME("video");
1da177e4 71
f52fd66d 72MODULE_AUTHOR("Bruno Ducrot");
7cda93e0 73MODULE_DESCRIPTION("ACPI Video Driver");
1da177e4
LT
74MODULE_LICENSE("GPL");
75
8a681a4d
ZR
76static int brightness_switch_enabled = 1;
77module_param(brightness_switch_enabled, bool, 0644);
78
4be44fcd
LB
79static int acpi_video_bus_add(struct acpi_device *device);
80static int acpi_video_bus_remove(struct acpi_device *device, int type);
863c1490 81static int acpi_video_resume(struct acpi_device *device);
7015558f 82static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
1da177e4 83
1ba90e3a
TR
84static const struct acpi_device_id video_device_ids[] = {
85 {ACPI_VIDEO_HID, 0},
86 {"", 0},
87};
88MODULE_DEVICE_TABLE(acpi, video_device_ids);
89
1da177e4 90static struct acpi_driver acpi_video_bus = {
c2b6705b 91 .name = "video",
1da177e4 92 .class = ACPI_VIDEO_CLASS,
1ba90e3a 93 .ids = video_device_ids,
1da177e4
LT
94 .ops = {
95 .add = acpi_video_bus_add,
96 .remove = acpi_video_bus_remove,
863c1490 97 .resume = acpi_video_resume,
7015558f 98 .notify = acpi_video_bus_notify,
4be44fcd 99 },
1da177e4
LT
100};
101
102struct acpi_video_bus_flags {
4be44fcd
LB
103 u8 multihead:1; /* can switch video heads */
104 u8 rom:1; /* can retrieve a video rom */
105 u8 post:1; /* can configure the head to */
106 u8 reserved:5;
1da177e4
LT
107};
108
109struct acpi_video_bus_cap {
4be44fcd
LB
110 u8 _DOS:1; /*Enable/Disable output switching */
111 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
112 u8 _ROM:1; /*Get ROM Data */
113 u8 _GPD:1; /*Get POST Device */
114 u8 _SPD:1; /*Set POST Device */
115 u8 _VPO:1; /*Video POST Options */
116 u8 reserved:2;
1da177e4
LT
117};
118
4be44fcd
LB
119struct acpi_video_device_attrib {
120 u32 display_index:4; /* A zero-based instance of the Display */
98fb8fe1 121 u32 display_port_attachment:4; /*This field differentiates the display type */
4be44fcd 122 u32 display_type:4; /*Describe the specific type in use */
98fb8fe1 123 u32 vendor_specific:4; /*Chipset Vendor Specific */
4be44fcd
LB
124 u32 bios_can_detect:1; /*BIOS can detect the device */
125 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
126 the VGA device. */
127 u32 pipe_id:3; /*For VGA multiple-head devices. */
128 u32 reserved:10; /*Must be 0 */
129 u32 device_id_scheme:1; /*Device ID Scheme */
1da177e4
LT
130};
131
132struct acpi_video_enumerated_device {
133 union {
134 u32 int_val;
4be44fcd 135 struct acpi_video_device_attrib attrib;
1da177e4
LT
136 } value;
137 struct acpi_video_device *bind_info;
138};
139
140struct acpi_video_bus {
e6afa0de 141 struct acpi_device *device;
4be44fcd 142 u8 dos_setting;
1da177e4 143 struct acpi_video_enumerated_device *attached_array;
4be44fcd
LB
144 u8 attached_count;
145 struct acpi_video_bus_cap cap;
1da177e4 146 struct acpi_video_bus_flags flags;
4be44fcd 147 struct list_head video_device_list;
bbac81f5 148 struct mutex device_list_lock; /* protects video_device_list */
4be44fcd 149 struct proc_dir_entry *dir;
e9dab196
LY
150 struct input_dev *input;
151 char phys[32]; /* for input device */
1da177e4
LT
152};
153
154struct acpi_video_device_flags {
4be44fcd
LB
155 u8 crt:1;
156 u8 lcd:1;
157 u8 tvout:1;
82cae999 158 u8 dvi:1;
4be44fcd
LB
159 u8 bios:1;
160 u8 unknown:1;
82cae999 161 u8 reserved:2;
1da177e4
LT
162};
163
164struct acpi_video_device_cap {
4be44fcd
LB
165 u8 _ADR:1; /*Return the unique ID */
166 u8 _BCL:1; /*Query list of brightness control levels supported */
167 u8 _BCM:1; /*Set the brightness level */
2f3d000a 168 u8 _BQC:1; /* Get current brightness level */
c60d638e 169 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
4be44fcd
LB
170 u8 _DDC:1; /*Return the EDID for this device */
171 u8 _DCS:1; /*Return status of output device */
172 u8 _DGS:1; /*Query graphics state */
173 u8 _DSS:1; /*Device state set */
1da177e4
LT
174};
175
d32f6947
ZR
176struct acpi_video_brightness_flags {
177 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
d80fb99f 178 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
1a7c618a
ZR
179 u8 _BCL_use_index:1; /* levels in _BCL are index values */
180 u8 _BCM_use_index:1; /* input of _BCM is an index value */
181 u8 _BQC_use_index:1; /* _BQC returns an index value */
d32f6947
ZR
182};
183
1da177e4 184struct acpi_video_device_brightness {
4be44fcd
LB
185 int curr;
186 int count;
187 int *levels;
d32f6947 188 struct acpi_video_brightness_flags flags;
1da177e4
LT
189};
190
191struct acpi_video_device {
4be44fcd
LB
192 unsigned long device_id;
193 struct acpi_video_device_flags flags;
194 struct acpi_video_device_cap cap;
195 struct list_head entry;
196 struct acpi_video_bus *video;
197 struct acpi_device *dev;
1da177e4 198 struct acpi_video_device_brightness *brightness;
2f3d000a 199 struct backlight_device *backlight;
702ed512 200 struct thermal_cooling_device *cdev;
23b0f015 201 struct output_device *output_dev;
1da177e4
LT
202};
203
1da177e4
LT
204/* bus */
205static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
070d8eb1 206static const struct file_operations acpi_video_bus_info_fops = {
cf7acfab 207 .owner = THIS_MODULE,
4be44fcd
LB
208 .open = acpi_video_bus_info_open_fs,
209 .read = seq_read,
210 .llseek = seq_lseek,
211 .release = single_release,
1da177e4
LT
212};
213
214static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
070d8eb1 215static const struct file_operations acpi_video_bus_ROM_fops = {
cf7acfab 216 .owner = THIS_MODULE,
4be44fcd
LB
217 .open = acpi_video_bus_ROM_open_fs,
218 .read = seq_read,
219 .llseek = seq_lseek,
220 .release = single_release,
1da177e4
LT
221};
222
4be44fcd
LB
223static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
224 struct file *file);
070d8eb1 225static const struct file_operations acpi_video_bus_POST_info_fops = {
cf7acfab 226 .owner = THIS_MODULE,
4be44fcd
LB
227 .open = acpi_video_bus_POST_info_open_fs,
228 .read = seq_read,
229 .llseek = seq_lseek,
230 .release = single_release,
1da177e4
LT
231};
232
233static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
c07c9a78 234static ssize_t acpi_video_bus_write_POST(struct file *file,
b7171ae7
JE
235 const char __user *buffer, size_t count, loff_t *data);
236static const struct file_operations acpi_video_bus_POST_fops = {
cf7acfab 237 .owner = THIS_MODULE,
4be44fcd
LB
238 .open = acpi_video_bus_POST_open_fs,
239 .read = seq_read,
b7171ae7 240 .write = acpi_video_bus_write_POST,
4be44fcd
LB
241 .llseek = seq_lseek,
242 .release = single_release,
1da177e4
LT
243};
244
1da177e4 245static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
c07c9a78 246static ssize_t acpi_video_bus_write_DOS(struct file *file,
b7171ae7
JE
247 const char __user *buffer, size_t count, loff_t *data);
248static const struct file_operations acpi_video_bus_DOS_fops = {
cf7acfab 249 .owner = THIS_MODULE,
4be44fcd
LB
250 .open = acpi_video_bus_DOS_open_fs,
251 .read = seq_read,
b7171ae7 252 .write = acpi_video_bus_write_DOS,
4be44fcd
LB
253 .llseek = seq_lseek,
254 .release = single_release,
1da177e4
LT
255};
256
257/* device */
4be44fcd
LB
258static int acpi_video_device_info_open_fs(struct inode *inode,
259 struct file *file);
070d8eb1 260static const struct file_operations acpi_video_device_info_fops = {
cf7acfab 261 .owner = THIS_MODULE,
4be44fcd
LB
262 .open = acpi_video_device_info_open_fs,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = single_release,
1da177e4
LT
266};
267
4be44fcd
LB
268static int acpi_video_device_state_open_fs(struct inode *inode,
269 struct file *file);
c07c9a78 270static ssize_t acpi_video_device_write_state(struct file *file,
b7171ae7
JE
271 const char __user *buffer, size_t count, loff_t *data);
272static const struct file_operations acpi_video_device_state_fops = {
cf7acfab 273 .owner = THIS_MODULE,
4be44fcd
LB
274 .open = acpi_video_device_state_open_fs,
275 .read = seq_read,
b7171ae7 276 .write = acpi_video_device_write_state,
4be44fcd
LB
277 .llseek = seq_lseek,
278 .release = single_release,
1da177e4
LT
279};
280
4be44fcd
LB
281static int acpi_video_device_brightness_open_fs(struct inode *inode,
282 struct file *file);
c07c9a78 283static ssize_t acpi_video_device_write_brightness(struct file *file,
b7171ae7 284 const char __user *buffer, size_t count, loff_t *data);
1da177e4 285static struct file_operations acpi_video_device_brightness_fops = {
cf7acfab 286 .owner = THIS_MODULE,
4be44fcd
LB
287 .open = acpi_video_device_brightness_open_fs,
288 .read = seq_read,
b7171ae7 289 .write = acpi_video_device_write_brightness,
4be44fcd
LB
290 .llseek = seq_lseek,
291 .release = single_release,
1da177e4
LT
292};
293
4be44fcd
LB
294static int acpi_video_device_EDID_open_fs(struct inode *inode,
295 struct file *file);
070d8eb1 296static const struct file_operations acpi_video_device_EDID_fops = {
cf7acfab 297 .owner = THIS_MODULE,
4be44fcd
LB
298 .open = acpi_video_device_EDID_open_fs,
299 .read = seq_read,
300 .llseek = seq_lseek,
301 .release = single_release,
1da177e4
LT
302};
303
b7171ae7 304static const char device_decode[][30] = {
1da177e4
LT
305 "motherboard VGA device",
306 "PCI VGA device",
307 "AGP VGA device",
308 "UNKNOWN",
309};
310
4be44fcd
LB
311static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
312static void acpi_video_device_rebind(struct acpi_video_bus *video);
313static void acpi_video_device_bind(struct acpi_video_bus *video,
314 struct acpi_video_device *device);
1da177e4 315static int acpi_video_device_enumerate(struct acpi_video_bus *video);
2f3d000a
YL
316static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
317 int level);
318static int acpi_video_device_lcd_get_level_current(
319 struct acpi_video_device *device,
27663c58 320 unsigned long long *level);
4be44fcd
LB
321static int acpi_video_get_next_level(struct acpi_video_device *device,
322 u32 level_current, u32 event);
c8890f90 323static int acpi_video_switch_brightness(struct acpi_video_device *device,
4be44fcd 324 int event);
23b0f015 325static int acpi_video_device_get_state(struct acpi_video_device *device,
27663c58 326 unsigned long long *state);
23b0f015
LY
327static int acpi_video_output_get(struct output_device *od);
328static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
1da177e4 329
2f3d000a
YL
330/*backlight device sysfs support*/
331static int acpi_video_get_brightness(struct backlight_device *bd)
332{
27663c58 333 unsigned long long cur_level;
38531e6f 334 int i;
2f3d000a 335 struct acpi_video_device *vd =
655bfd7a 336 (struct acpi_video_device *)bl_get_data(bd);
c8890f90
ZR
337
338 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
339 return -EINVAL;
38531e6f
MG
340 for (i = 2; i < vd->brightness->count; i++) {
341 if (vd->brightness->levels[i] == cur_level)
342 /* The first two entries are special - see page 575
343 of the ACPI spec 3.0 */
344 return i-2;
345 }
346 return 0;
2f3d000a
YL
347}
348
349static int acpi_video_set_brightness(struct backlight_device *bd)
350{
24450c7a 351 int request_level = bd->props.brightness + 2;
2f3d000a 352 struct acpi_video_device *vd =
655bfd7a 353 (struct acpi_video_device *)bl_get_data(bd);
24450c7a
ZR
354
355 return acpi_video_device_lcd_set_level(vd,
356 vd->brightness->levels[request_level]);
2f3d000a
YL
357}
358
599a52d1
RP
359static struct backlight_ops acpi_backlight_ops = {
360 .get_brightness = acpi_video_get_brightness,
361 .update_status = acpi_video_set_brightness,
362};
363
23b0f015
LY
364/*video output device sysfs support*/
365static int acpi_video_output_get(struct output_device *od)
366{
27663c58 367 unsigned long long state;
23b0f015 368 struct acpi_video_device *vd =
60043428 369 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
23b0f015
LY
370 acpi_video_device_get_state(vd, &state);
371 return (int)state;
372}
373
374static int acpi_video_output_set(struct output_device *od)
375{
376 unsigned long state = od->request_state;
377 struct acpi_video_device *vd=
60043428 378 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
23b0f015
LY
379 return acpi_video_device_set_state(vd, state);
380}
381
382static struct output_properties acpi_output_properties = {
383 .set_state = acpi_video_output_set,
384 .get_status = acpi_video_output_get,
385};
702ed512
ZR
386
387
388/* thermal cooling device callbacks */
6503e5df
MG
389static int video_get_max_state(struct thermal_cooling_device *cdev, unsigned
390 long *state)
702ed512
ZR
391{
392 struct acpi_device *device = cdev->devdata;
393 struct acpi_video_device *video = acpi_driver_data(device);
394
6503e5df
MG
395 *state = video->brightness->count - 3;
396 return 0;
702ed512
ZR
397}
398
6503e5df
MG
399static int video_get_cur_state(struct thermal_cooling_device *cdev, unsigned
400 long *state)
702ed512
ZR
401{
402 struct acpi_device *device = cdev->devdata;
403 struct acpi_video_device *video = acpi_driver_data(device);
27663c58 404 unsigned long long level;
6503e5df 405 int offset;
702ed512 406
c8890f90
ZR
407 if (acpi_video_device_lcd_get_level_current(video, &level))
408 return -EINVAL;
6503e5df
MG
409 for (offset = 2; offset < video->brightness->count; offset++)
410 if (level == video->brightness->levels[offset]) {
411 *state = video->brightness->count - offset - 1;
412 return 0;
413 }
702ed512
ZR
414
415 return -EINVAL;
416}
417
418static int
6503e5df 419video_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
702ed512
ZR
420{
421 struct acpi_device *device = cdev->devdata;
422 struct acpi_video_device *video = acpi_driver_data(device);
423 int level;
424
425 if ( state >= video->brightness->count - 2)
426 return -EINVAL;
427
428 state = video->brightness->count - state;
429 level = video->brightness->levels[state -1];
430 return acpi_video_device_lcd_set_level(video, level);
431}
432
433static struct thermal_cooling_device_ops video_cooling_ops = {
434 .get_max_state = video_get_max_state,
435 .get_cur_state = video_get_cur_state,
436 .set_cur_state = video_set_cur_state,
437};
438
1da177e4
LT
439/* --------------------------------------------------------------------------
440 Video Management
441 -------------------------------------------------------------------------- */
442
443/* device */
444
445static int
27663c58 446acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
1da177e4 447{
4be44fcd 448 int status;
90130268
PM
449
450 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
1da177e4 451
d550d98d 452 return status;
1da177e4
LT
453}
454
455static int
4be44fcd 456acpi_video_device_get_state(struct acpi_video_device *device,
27663c58 457 unsigned long long *state)
1da177e4 458{
4be44fcd 459 int status;
1da177e4 460
90130268 461 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
1da177e4 462
d550d98d 463 return status;
1da177e4
LT
464}
465
466static int
4be44fcd 467acpi_video_device_set_state(struct acpi_video_device *device, int state)
1da177e4 468{
4be44fcd
LB
469 int status;
470 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
471 struct acpi_object_list args = { 1, &arg0 };
27663c58 472 unsigned long long ret;
1da177e4 473
1da177e4
LT
474
475 arg0.integer.value = state;
90130268 476 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
1da177e4 477
d550d98d 478 return status;
1da177e4
LT
479}
480
481static int
4be44fcd
LB
482acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
483 union acpi_object **levels)
1da177e4 484{
4be44fcd
LB
485 int status;
486 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
487 union acpi_object *obj;
1da177e4 488
1da177e4
LT
489
490 *levels = NULL;
491
90130268 492 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
1da177e4 493 if (!ACPI_SUCCESS(status))
d550d98d 494 return status;
4be44fcd 495 obj = (union acpi_object *)buffer.pointer;
6665bda7 496 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6468463a 497 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
1da177e4
LT
498 status = -EFAULT;
499 goto err;
500 }
501
502 *levels = obj;
503
d550d98d 504 return 0;
1da177e4 505
4be44fcd 506 err:
6044ec88 507 kfree(buffer.pointer);
1da177e4 508
d550d98d 509 return status;
1da177e4
LT
510}
511
512static int
4be44fcd 513acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
1da177e4 514{
24450c7a 515 int status;
4be44fcd
LB
516 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
517 struct acpi_object_list args = { 1, &arg0 };
9e6dada9 518 int state;
1da177e4 519
1da177e4 520 arg0.integer.value = level;
1da177e4 521
24450c7a
ZR
522 status = acpi_evaluate_object(device->dev->handle, "_BCM",
523 &args, NULL);
524 if (ACPI_FAILURE(status)) {
525 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
526 return -EIO;
527 }
528
4500ca8e 529 device->brightness->curr = level;
9e6dada9 530 for (state = 2; state < device->brightness->count; state++)
24450c7a 531 if (level == device->brightness->levels[state]) {
1a7c618a
ZR
532 if (device->backlight)
533 device->backlight->props.brightness = state - 2;
24450c7a
ZR
534 return 0;
535 }
9e6dada9 536
24450c7a
ZR
537 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
538 return -EINVAL;
1da177e4
LT
539}
540
541static int
4be44fcd 542acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
27663c58 543 unsigned long long *level)
1da177e4 544{
c8890f90
ZR
545 acpi_status status = AE_OK;
546
c60d638e
ZR
547 if (device->cap._BQC || device->cap._BCQ) {
548 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
549
550 status = acpi_evaluate_integer(device->dev->handle, buf,
c8890f90
ZR
551 NULL, level);
552 if (ACPI_SUCCESS(status)) {
1a7c618a
ZR
553 if (device->brightness->flags._BQC_use_index) {
554 if (device->brightness->flags._BCL_reversed)
555 *level = device->brightness->count
556 - 3 - (*level);
557 *level = device->brightness->levels[*level + 2];
558
559 }
c8890f90
ZR
560 device->brightness->curr = *level;
561 return 0;
562 } else {
563 /* Fixme:
564 * should we return an error or ignore this failure?
565 * dev->brightness->curr is a cached value which stores
566 * the correct current backlight level in most cases.
567 * ACPI video backlight still works w/ buggy _BQC.
568 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
569 */
c60d638e
ZR
570 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
571 device->cap._BQC = device->cap._BCQ = 0;
c8890f90
ZR
572 }
573 }
574
4500ca8e 575 *level = device->brightness->curr;
c8890f90 576 return 0;
1da177e4
LT
577}
578
579static int
4be44fcd
LB
580acpi_video_device_EDID(struct acpi_video_device *device,
581 union acpi_object **edid, ssize_t length)
1da177e4 582{
4be44fcd
LB
583 int status;
584 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
585 union acpi_object *obj;
586 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
587 struct acpi_object_list args = { 1, &arg0 };
1da177e4 588
1da177e4
LT
589
590 *edid = NULL;
591
592 if (!device)
d550d98d 593 return -ENODEV;
1da177e4
LT
594 if (length == 128)
595 arg0.integer.value = 1;
596 else if (length == 256)
597 arg0.integer.value = 2;
598 else
d550d98d 599 return -EINVAL;
1da177e4 600
90130268 601 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
1da177e4 602 if (ACPI_FAILURE(status))
d550d98d 603 return -ENODEV;
1da177e4 604
50dd0969 605 obj = buffer.pointer;
1da177e4
LT
606
607 if (obj && obj->type == ACPI_TYPE_BUFFER)
608 *edid = obj;
609 else {
6468463a 610 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
1da177e4
LT
611 status = -EFAULT;
612 kfree(obj);
613 }
614
d550d98d 615 return status;
1da177e4
LT
616}
617
1da177e4
LT
618/* bus */
619
620static int
4be44fcd 621acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
1da177e4 622{
4be44fcd 623 int status;
27663c58 624 unsigned long long tmp;
4be44fcd
LB
625 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
626 struct acpi_object_list args = { 1, &arg0 };
1da177e4 627
1da177e4
LT
628
629 arg0.integer.value = option;
630
90130268 631 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
1da177e4 632 if (ACPI_SUCCESS(status))
4be44fcd 633 status = tmp ? (-EINVAL) : (AE_OK);
1da177e4 634
d550d98d 635 return status;
1da177e4
LT
636}
637
638static int
27663c58 639acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
1da177e4
LT
640{
641 int status;
642
90130268 643 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
1da177e4 644
d550d98d 645 return status;
1da177e4
LT
646}
647
648static int
4be44fcd 649acpi_video_bus_POST_options(struct acpi_video_bus *video,
27663c58 650 unsigned long long *options)
1da177e4 651{
4be44fcd 652 int status;
1da177e4 653
90130268 654 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
1da177e4
LT
655 *options &= 3;
656
d550d98d 657 return status;
1da177e4
LT
658}
659
660/*
661 * Arg:
662 * video : video bus device pointer
663 * bios_flag :
664 * 0. The system BIOS should NOT automatically switch(toggle)
665 * the active display output.
666 * 1. The system BIOS should automatically switch (toggle) the
98fb8fe1 667 * active display output. No switch event.
1da177e4
LT
668 * 2. The _DGS value should be locked.
669 * 3. The system BIOS should not automatically switch (toggle) the
670 * active display output, but instead generate the display switch
671 * event notify code.
672 * lcd_flag :
673 * 0. The system BIOS should automatically control the brightness level
98fb8fe1 674 * of the LCD when the power changes from AC to DC
1da177e4 675 * 1. The system BIOS should NOT automatically control the brightness
98fb8fe1 676 * level of the LCD when the power changes from AC to DC.
1da177e4
LT
677 * Return Value:
678 * -1 wrong arg.
679 */
680
681static int
4be44fcd 682acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
1da177e4 683{
4be44fcd
LB
684 acpi_integer status = 0;
685 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
686 struct acpi_object_list args = { 1, &arg0 };
1da177e4 687
1da177e4 688
4be44fcd 689 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
1da177e4
LT
690 status = -1;
691 goto Failed;
692 }
693 arg0.integer.value = (lcd_flag << 2) | bios_flag;
694 video->dos_setting = arg0.integer.value;
90130268 695 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
1da177e4 696
4be44fcd 697 Failed:
d550d98d 698 return status;
1da177e4
LT
699}
700
935e5f29
ZR
701/*
702 * Simple comparison function used to sort backlight levels.
703 */
704
705static int
706acpi_video_cmp_level(const void *a, const void *b)
707{
708 return *(int *)a - *(int *)b;
709}
710
1da177e4
LT
711/*
712 * Arg:
713 * device : video output device (LCD, CRT, ..)
714 *
715 * Return Value:
469778c1
JJ
716 * Maximum brightness level
717 *
718 * Allocate and initialize device->brightness.
719 */
720
721static int
722acpi_video_init_brightness(struct acpi_video_device *device)
723{
724 union acpi_object *obj = NULL;
d32f6947 725 int i, max_level = 0, count = 0, level_ac_battery = 0;
1a7c618a 726 unsigned long long level, level_old;
469778c1
JJ
727 union acpi_object *o;
728 struct acpi_video_device_brightness *br = NULL;
1a7c618a 729 int result = -EINVAL;
469778c1
JJ
730
731 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
732 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
733 "LCD brightness level\n"));
734 goto out;
735 }
736
737 if (obj->package.count < 2)
738 goto out;
739
740 br = kzalloc(sizeof(*br), GFP_KERNEL);
741 if (!br) {
742 printk(KERN_ERR "can't allocate memory\n");
1a7c618a 743 result = -ENOMEM;
469778c1
JJ
744 goto out;
745 }
746
d32f6947 747 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
469778c1 748 GFP_KERNEL);
1a7c618a
ZR
749 if (!br->levels) {
750 result = -ENOMEM;
469778c1 751 goto out_free;
1a7c618a 752 }
469778c1
JJ
753
754 for (i = 0; i < obj->package.count; i++) {
755 o = (union acpi_object *)&obj->package.elements[i];
756 if (o->type != ACPI_TYPE_INTEGER) {
757 printk(KERN_ERR PREFIX "Invalid data\n");
758 continue;
759 }
760 br->levels[count] = (u32) o->integer.value;
761
762 if (br->levels[count] > max_level)
763 max_level = br->levels[count];
764 count++;
765 }
766
d32f6947
ZR
767 /*
768 * some buggy BIOS don't export the levels
769 * when machine is on AC/Battery in _BCL package.
770 * In this case, the first two elements in _BCL packages
771 * are also supported brightness levels that OS should take care of.
772 */
773 for (i = 2; i < count; i++)
774 if (br->levels[i] == br->levels[0] ||
775 br->levels[i] == br->levels[1])
776 level_ac_battery++;
777
778 if (level_ac_battery < 2) {
779 level_ac_battery = 2 - level_ac_battery;
780 br->flags._BCL_no_ac_battery_levels = 1;
781 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
782 br->levels[i] = br->levels[i - level_ac_battery];
783 count += level_ac_battery;
784 } else if (level_ac_battery > 2)
785 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
786
d80fb99f
ZR
787 /* Check if the _BCL package is in a reversed order */
788 if (max_level == br->levels[2]) {
789 br->flags._BCL_reversed = 1;
790 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
791 acpi_video_cmp_level, NULL);
792 } else if (max_level != br->levels[count - 1])
793 ACPI_ERROR((AE_INFO,
794 "Found unordered _BCL package\n"));
469778c1
JJ
795
796 br->count = count;
797 device->brightness = br;
1a7c618a
ZR
798
799 /* Check the input/output of _BQC/_BCL/_BCM */
800 if ((max_level < 100) && (max_level <= (count - 2)))
801 br->flags._BCL_use_index = 1;
802
803 /*
804 * _BCM is always consistent with _BCL,
805 * at least for all the laptops we have ever seen.
806 */
807 br->flags._BCM_use_index = br->flags._BCL_use_index;
808
809 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
810 br->curr = max_level;
811 result = acpi_video_device_lcd_get_level_current(device, &level_old);
812 if (result)
813 goto out_free_levels;
814
815 result = acpi_video_device_lcd_set_level(device, br->curr);
816 if (result)
817 goto out_free_levels;
818
819 result = acpi_video_device_lcd_get_level_current(device, &level);
820 if (result)
821 goto out_free_levels;
822
823 if ((level != level_old) && !br->flags._BCM_use_index) {
824 /* Note:
825 * This piece of code does not work correctly if the current
826 * brightness levels is 0.
827 * But I guess boxes that boot with such a dark screen are rare
828 * and no more code is needed to cover this specifial case.
829 */
830
831 if (level_ac_battery != 2) {
832 /*
833 * For now, we don't support the _BCL like this:
834 * 16, 15, 0, 1, 2, 3, ..., 14, 15, 16
835 * because we may mess up the index returned by _BQC.
836 * Plus: we have not got a box like this.
837 */
838 ACPI_ERROR((AE_INFO, "_BCL not supported\n"));
839 }
840 br->flags._BQC_use_index = 1;
841 }
842
d32f6947
ZR
843 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
844 "found %d brightness levels\n", count - 2));
469778c1 845 kfree(obj);
1a7c618a 846 return result;
469778c1
JJ
847
848out_free_levels:
849 kfree(br->levels);
850out_free:
851 kfree(br);
852out:
853 device->brightness = NULL;
854 kfree(obj);
1a7c618a 855 return result;
469778c1
JJ
856}
857
858/*
859 * Arg:
860 * device : video output device (LCD, CRT, ..)
861 *
862 * Return Value:
1da177e4
LT
863 * None
864 *
98fb8fe1 865 * Find out all required AML methods defined under the output
1da177e4
LT
866 * device.
867 */
868
4be44fcd 869static void acpi_video_device_find_cap(struct acpi_video_device *device)
1da177e4 870{
1da177e4 871 acpi_handle h_dummy1;
1da177e4 872
1da177e4 873
98934def 874 memset(&device->cap, 0, sizeof(device->cap));
1da177e4 875
90130268 876 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
1da177e4
LT
877 device->cap._ADR = 1;
878 }
90130268 879 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
4be44fcd 880 device->cap._BCL = 1;
1da177e4 881 }
90130268 882 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
4be44fcd 883 device->cap._BCM = 1;
1da177e4 884 }
2f3d000a
YL
885 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
886 device->cap._BQC = 1;
c60d638e
ZR
887 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
888 &h_dummy1))) {
889 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
890 device->cap._BCQ = 1;
891 }
892
90130268 893 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
4be44fcd 894 device->cap._DDC = 1;
1da177e4 895 }
90130268 896 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
1da177e4
LT
897 device->cap._DCS = 1;
898 }
90130268 899 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
1da177e4
LT
900 device->cap._DGS = 1;
901 }
90130268 902 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
1da177e4
LT
903 device->cap._DSS = 1;
904 }
905
1a7c618a 906 if (acpi_video_backlight_support()) {
702ed512 907 int result;
2f3d000a
YL
908 static int count = 0;
909 char *name;
1a7c618a
ZR
910
911 result = acpi_video_init_brightness(device);
912 if (result)
913 return;
2f3d000a
YL
914 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
915 if (!name)
916 return;
917
2f3d000a 918 sprintf(name, "acpi_video%d", count++);
2f3d000a 919 device->backlight = backlight_device_register(name,
599a52d1 920 NULL, device, &acpi_backlight_ops);
38531e6f 921 device->backlight->props.max_brightness = device->brightness->count-3;
2f3d000a 922 kfree(name);
702ed512
ZR
923
924 device->cdev = thermal_cooling_device_register("LCD",
925 device->dev, &video_cooling_ops);
43ff39f2
TS
926 if (IS_ERR(device->cdev))
927 return;
928
fc3a8828
GKH
929 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
930 device->cdev->id);
9030062f
JL
931 result = sysfs_create_link(&device->dev->dev.kobj,
932 &device->cdev->device.kobj,
933 "thermal_cooling");
934 if (result)
935 printk(KERN_ERR PREFIX "Create sysfs link\n");
936 result = sysfs_create_link(&device->cdev->device.kobj,
937 &device->dev->dev.kobj, "device");
938 if (result)
939 printk(KERN_ERR PREFIX "Create sysfs link\n");
940
2f3d000a 941 }
c3d6de69
TR
942
943 if (acpi_video_display_switch_support()) {
944
945 if (device->cap._DCS && device->cap._DSS) {
946 static int count;
947 char *name;
948 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
949 if (!name)
950 return;
951 sprintf(name, "acpi_video%d", count++);
952 device->output_dev = video_output_register(name,
953 NULL, device, &acpi_output_properties);
954 kfree(name);
955 }
23b0f015 956 }
1da177e4
LT
957}
958
959/*
960 * Arg:
961 * device : video output device (VGA)
962 *
963 * Return Value:
964 * None
965 *
98fb8fe1 966 * Find out all required AML methods defined under the video bus device.
1da177e4
LT
967 */
968
4be44fcd 969static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1da177e4 970{
4be44fcd 971 acpi_handle h_dummy1;
1da177e4 972
98934def 973 memset(&video->cap, 0, sizeof(video->cap));
90130268 974 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
1da177e4
LT
975 video->cap._DOS = 1;
976 }
90130268 977 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
1da177e4
LT
978 video->cap._DOD = 1;
979 }
90130268 980 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
1da177e4
LT
981 video->cap._ROM = 1;
982 }
90130268 983 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
1da177e4
LT
984 video->cap._GPD = 1;
985 }
90130268 986 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
1da177e4
LT
987 video->cap._SPD = 1;
988 }
90130268 989 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
1da177e4
LT
990 video->cap._VPO = 1;
991 }
992}
993
994/*
995 * Check whether the video bus device has required AML method to
996 * support the desired features
997 */
998
4be44fcd 999static int acpi_video_bus_check(struct acpi_video_bus *video)
1da177e4 1000{
4be44fcd 1001 acpi_status status = -ENOENT;
22c13f9d 1002 struct device *dev;
1da177e4
LT
1003
1004 if (!video)
d550d98d 1005 return -EINVAL;
1da177e4 1006
22c13f9d
TR
1007 dev = acpi_get_physical_pci_device(video->device->handle);
1008 if (!dev)
1009 return -ENODEV;
1010 put_device(dev);
1011
1da177e4
LT
1012 /* Since there is no HID, CID and so on for VGA driver, we have
1013 * to check well known required nodes.
1014 */
1015
98fb8fe1 1016 /* Does this device support video switching? */
4be44fcd 1017 if (video->cap._DOS) {
1da177e4
LT
1018 video->flags.multihead = 1;
1019 status = 0;
1020 }
1021
98fb8fe1 1022 /* Does this device support retrieving a video ROM? */
4be44fcd 1023 if (video->cap._ROM) {
1da177e4
LT
1024 video->flags.rom = 1;
1025 status = 0;
1026 }
1027
98fb8fe1 1028 /* Does this device support configuring which video device to POST? */
4be44fcd 1029 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1da177e4
LT
1030 video->flags.post = 1;
1031 status = 0;
1032 }
1033
d550d98d 1034 return status;
1da177e4
LT
1035}
1036
1037/* --------------------------------------------------------------------------
1038 FS Interface (/proc)
1039 -------------------------------------------------------------------------- */
1040
4be44fcd 1041static struct proc_dir_entry *acpi_video_dir;
1da177e4
LT
1042
1043/* video devices */
1044
4be44fcd 1045static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
1da177e4 1046{
50dd0969 1047 struct acpi_video_device *dev = seq->private;
1da177e4 1048
1da177e4
LT
1049
1050 if (!dev)
1051 goto end;
1052
1053 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1054 seq_printf(seq, "type: ");
1055 if (dev->flags.crt)
1056 seq_printf(seq, "CRT\n");
1057 else if (dev->flags.lcd)
1058 seq_printf(seq, "LCD\n");
1059 else if (dev->flags.tvout)
1060 seq_printf(seq, "TVOUT\n");
82cae999
RZ
1061 else if (dev->flags.dvi)
1062 seq_printf(seq, "DVI\n");
1da177e4
LT
1063 else
1064 seq_printf(seq, "UNKNOWN\n");
1065
4be44fcd 1066 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
1da177e4 1067
4be44fcd 1068 end:
d550d98d 1069 return 0;
1da177e4
LT
1070}
1071
1072static int
4be44fcd 1073acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1074{
1075 return single_open(file, acpi_video_device_info_seq_show,
1076 PDE(inode)->data);
1077}
1078
4be44fcd 1079static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
1da177e4 1080{
4be44fcd 1081 int status;
50dd0969 1082 struct acpi_video_device *dev = seq->private;
27663c58 1083 unsigned long long state;
1da177e4 1084
1da177e4
LT
1085
1086 if (!dev)
1087 goto end;
1088
1089 status = acpi_video_device_get_state(dev, &state);
1090 seq_printf(seq, "state: ");
1091 if (ACPI_SUCCESS(status))
27663c58 1092 seq_printf(seq, "0x%02llx\n", state);
1da177e4
LT
1093 else
1094 seq_printf(seq, "<not supported>\n");
1095
1096 status = acpi_video_device_query(dev, &state);
1097 seq_printf(seq, "query: ");
1098 if (ACPI_SUCCESS(status))
27663c58 1099 seq_printf(seq, "0x%02llx\n", state);
1da177e4
LT
1100 else
1101 seq_printf(seq, "<not supported>\n");
1102
4be44fcd 1103 end:
d550d98d 1104 return 0;
1da177e4
LT
1105}
1106
1107static int
4be44fcd 1108acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1109{
1110 return single_open(file, acpi_video_device_state_seq_show,
1111 PDE(inode)->data);
1112}
1113
1114static ssize_t
4be44fcd
LB
1115acpi_video_device_write_state(struct file *file,
1116 const char __user * buffer,
1117 size_t count, loff_t * data)
1da177e4 1118{
4be44fcd 1119 int status;
50dd0969
JE
1120 struct seq_file *m = file->private_data;
1121 struct acpi_video_device *dev = m->private;
4be44fcd
LB
1122 char str[12] = { 0 };
1123 u32 state = 0;
1da177e4 1124
1da177e4
LT
1125
1126 if (!dev || count + 1 > sizeof str)
d550d98d 1127 return -EINVAL;
1da177e4
LT
1128
1129 if (copy_from_user(str, buffer, count))
d550d98d 1130 return -EFAULT;
1da177e4
LT
1131
1132 str[count] = 0;
1133 state = simple_strtoul(str, NULL, 0);
4be44fcd 1134 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
1da177e4
LT
1135
1136 status = acpi_video_device_set_state(dev, state);
1137
1138 if (status)
d550d98d 1139 return -EFAULT;
1da177e4 1140
d550d98d 1141 return count;
1da177e4
LT
1142}
1143
1144static int
4be44fcd 1145acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
1da177e4 1146{
50dd0969 1147 struct acpi_video_device *dev = seq->private;
4be44fcd 1148 int i;
1da177e4 1149
1da177e4
LT
1150
1151 if (!dev || !dev->brightness) {
1152 seq_printf(seq, "<not supported>\n");
d550d98d 1153 return 0;
1da177e4
LT
1154 }
1155
1156 seq_printf(seq, "levels: ");
0a3db1ce 1157 for (i = 2; i < dev->brightness->count; i++)
1da177e4
LT
1158 seq_printf(seq, " %d", dev->brightness->levels[i]);
1159 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1160
d550d98d 1161 return 0;
1da177e4
LT
1162}
1163
1164static int
4be44fcd 1165acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1166{
1167 return single_open(file, acpi_video_device_brightness_seq_show,
1168 PDE(inode)->data);
1169}
1170
1171static ssize_t
4be44fcd
LB
1172acpi_video_device_write_brightness(struct file *file,
1173 const char __user * buffer,
1174 size_t count, loff_t * data)
1da177e4 1175{
50dd0969
JE
1176 struct seq_file *m = file->private_data;
1177 struct acpi_video_device *dev = m->private;
c88c5786 1178 char str[5] = { 0 };
4be44fcd
LB
1179 unsigned int level = 0;
1180 int i;
1da177e4 1181
1da177e4 1182
59d399d3 1183 if (!dev || !dev->brightness || count + 1 > sizeof str)
d550d98d 1184 return -EINVAL;
1da177e4
LT
1185
1186 if (copy_from_user(str, buffer, count))
d550d98d 1187 return -EFAULT;
1da177e4
LT
1188
1189 str[count] = 0;
1190 level = simple_strtoul(str, NULL, 0);
4be44fcd 1191
1da177e4 1192 if (level > 100)
d550d98d 1193 return -EFAULT;
1da177e4 1194
98fb8fe1 1195 /* validate through the list of available levels */
0a3db1ce 1196 for (i = 2; i < dev->brightness->count; i++)
1da177e4 1197 if (level == dev->brightness->levels[i]) {
24450c7a
ZR
1198 if (!acpi_video_device_lcd_set_level(dev, level))
1199 return count;
1da177e4
LT
1200 break;
1201 }
1202
24450c7a 1203 return -EINVAL;
1da177e4
LT
1204}
1205
4be44fcd 1206static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1da177e4 1207{
50dd0969 1208 struct acpi_video_device *dev = seq->private;
4be44fcd
LB
1209 int status;
1210 int i;
1211 union acpi_object *edid = NULL;
1da177e4 1212
1da177e4
LT
1213
1214 if (!dev)
1215 goto out;
1216
4be44fcd 1217 status = acpi_video_device_EDID(dev, &edid, 128);
1da177e4 1218 if (ACPI_FAILURE(status)) {
4be44fcd 1219 status = acpi_video_device_EDID(dev, &edid, 256);
1da177e4
LT
1220 }
1221
1222 if (ACPI_FAILURE(status)) {
1223 goto out;
1224 }
1225
1226 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1227 for (i = 0; i < edid->buffer.length; i++)
1228 seq_putc(seq, edid->buffer.pointer[i]);
1229 }
1230
4be44fcd 1231 out:
1da177e4
LT
1232 if (!edid)
1233 seq_printf(seq, "<not supported>\n");
1234 else
1235 kfree(edid);
1236
d550d98d 1237 return 0;
1da177e4
LT
1238}
1239
1240static int
4be44fcd 1241acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1242{
1243 return single_open(file, acpi_video_device_EDID_seq_show,
1244 PDE(inode)->data);
1245}
1246
4be44fcd 1247static int acpi_video_device_add_fs(struct acpi_device *device)
1da177e4 1248{
251cb0bc 1249 struct proc_dir_entry *entry, *device_dir;
1da177e4
LT
1250 struct acpi_video_device *vid_dev;
1251
50dd0969 1252 vid_dev = acpi_driver_data(device);
1da177e4 1253 if (!vid_dev)
d550d98d 1254 return -ENODEV;
1da177e4 1255
251cb0bc
DT
1256 device_dir = proc_mkdir(acpi_device_bid(device),
1257 vid_dev->video->dir);
1258 if (!device_dir)
1259 return -ENOMEM;
1260
1da177e4 1261 /* 'info' [R] */
e0066c4e 1262 entry = proc_create_data("info", S_IRUGO, device_dir,
cf7acfab 1263 &acpi_video_device_info_fops, acpi_driver_data(device));
1da177e4 1264 if (!entry)
251cb0bc 1265 goto err_remove_dir;
1da177e4
LT
1266
1267 /* 'state' [R/W] */
cf7acfab 1268 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
e0066c4e 1269 device_dir,
cf7acfab
DL
1270 &acpi_video_device_state_fops,
1271 acpi_driver_data(device));
1da177e4 1272 if (!entry)
251cb0bc 1273 goto err_remove_info;
1da177e4
LT
1274
1275 /* 'brightness' [R/W] */
cf7acfab 1276 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
e0066c4e 1277 device_dir,
cf7acfab
DL
1278 &acpi_video_device_brightness_fops,
1279 acpi_driver_data(device));
1da177e4 1280 if (!entry)
251cb0bc 1281 goto err_remove_state;
1da177e4
LT
1282
1283 /* 'EDID' [R] */
e0066c4e 1284 entry = proc_create_data("EDID", S_IRUGO, device_dir,
cf7acfab
DL
1285 &acpi_video_device_EDID_fops,
1286 acpi_driver_data(device));
1da177e4 1287 if (!entry)
251cb0bc 1288 goto err_remove_brightness;
1da177e4 1289
e0066c4e
AD
1290 acpi_device_dir(device) = device_dir;
1291
d550d98d 1292 return 0;
251cb0bc
DT
1293
1294 err_remove_brightness:
1295 remove_proc_entry("brightness", device_dir);
1296 err_remove_state:
1297 remove_proc_entry("state", device_dir);
1298 err_remove_info:
1299 remove_proc_entry("info", device_dir);
1300 err_remove_dir:
1301 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1302 return -ENOMEM;
1da177e4
LT
1303}
1304
4be44fcd 1305static int acpi_video_device_remove_fs(struct acpi_device *device)
1da177e4
LT
1306{
1307 struct acpi_video_device *vid_dev;
251cb0bc 1308 struct proc_dir_entry *device_dir;
1da177e4 1309
50dd0969 1310 vid_dev = acpi_driver_data(device);
1da177e4 1311 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
d550d98d 1312 return -ENODEV;
1da177e4 1313
251cb0bc
DT
1314 device_dir = acpi_device_dir(device);
1315 if (device_dir) {
1316 remove_proc_entry("info", device_dir);
1317 remove_proc_entry("state", device_dir);
1318 remove_proc_entry("brightness", device_dir);
1319 remove_proc_entry("EDID", device_dir);
4be44fcd 1320 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1da177e4
LT
1321 acpi_device_dir(device) = NULL;
1322 }
1323
d550d98d 1324 return 0;
1da177e4
LT
1325}
1326
1da177e4 1327/* video bus */
4be44fcd 1328static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1da177e4 1329{
50dd0969 1330 struct acpi_video_bus *video = seq->private;
1da177e4 1331
1da177e4
LT
1332
1333 if (!video)
1334 goto end;
1335
1336 seq_printf(seq, "Switching heads: %s\n",
4be44fcd 1337 video->flags.multihead ? "yes" : "no");
1da177e4 1338 seq_printf(seq, "Video ROM: %s\n",
4be44fcd 1339 video->flags.rom ? "yes" : "no");
1da177e4 1340 seq_printf(seq, "Device to be POSTed on boot: %s\n",
4be44fcd 1341 video->flags.post ? "yes" : "no");
1da177e4 1342
4be44fcd 1343 end:
d550d98d 1344 return 0;
1da177e4
LT
1345}
1346
4be44fcd 1347static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1da177e4 1348{
4be44fcd
LB
1349 return single_open(file, acpi_video_bus_info_seq_show,
1350 PDE(inode)->data);
1da177e4
LT
1351}
1352
4be44fcd 1353static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1da177e4 1354{
50dd0969 1355 struct acpi_video_bus *video = seq->private;
1da177e4 1356
1da177e4
LT
1357
1358 if (!video)
1359 goto end;
1360
96b2dd1f 1361 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1da177e4
LT
1362 seq_printf(seq, "<TODO>\n");
1363
4be44fcd 1364 end:
d550d98d 1365 return 0;
1da177e4
LT
1366}
1367
4be44fcd 1368static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1369{
1370 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1371}
1372
4be44fcd 1373static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1da177e4 1374{
50dd0969 1375 struct acpi_video_bus *video = seq->private;
27663c58 1376 unsigned long long options;
4be44fcd 1377 int status;
1da177e4 1378
1da177e4
LT
1379
1380 if (!video)
1381 goto end;
1382
1383 status = acpi_video_bus_POST_options(video, &options);
1384 if (ACPI_SUCCESS(status)) {
1385 if (!(options & 1)) {
4be44fcd
LB
1386 printk(KERN_WARNING PREFIX
1387 "The motherboard VGA device is not listed as a possible POST device.\n");
1388 printk(KERN_WARNING PREFIX
98fb8fe1 1389 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1da177e4 1390 }
4d939155 1391 printk(KERN_WARNING "%llx\n", options);
98fb8fe1 1392 seq_printf(seq, "can POST: <integrated video>");
1da177e4
LT
1393 if (options & 2)
1394 seq_printf(seq, " <PCI video>");
1395 if (options & 4)
1396 seq_printf(seq, " <AGP video>");
1397 seq_putc(seq, '\n');
1398 } else
1399 seq_printf(seq, "<not supported>\n");
4be44fcd 1400 end:
d550d98d 1401 return 0;
1da177e4
LT
1402}
1403
1404static int
4be44fcd 1405acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1da177e4 1406{
4be44fcd
LB
1407 return single_open(file, acpi_video_bus_POST_info_seq_show,
1408 PDE(inode)->data);
1da177e4
LT
1409}
1410
4be44fcd 1411static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1da177e4 1412{
50dd0969 1413 struct acpi_video_bus *video = seq->private;
4be44fcd 1414 int status;
27663c58 1415 unsigned long long id;
1da177e4 1416
1da177e4
LT
1417
1418 if (!video)
1419 goto end;
1420
4be44fcd 1421 status = acpi_video_bus_get_POST(video, &id);
1da177e4
LT
1422 if (!ACPI_SUCCESS(status)) {
1423 seq_printf(seq, "<not supported>\n");
1424 goto end;
1425 }
98fb8fe1 1426 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1da177e4 1427
4be44fcd 1428 end:
d550d98d 1429 return 0;
1da177e4
LT
1430}
1431
4be44fcd 1432static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1da177e4 1433{
50dd0969 1434 struct acpi_video_bus *video = seq->private;
1da177e4 1435
1da177e4 1436
4be44fcd 1437 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1da177e4 1438
d550d98d 1439 return 0;
1da177e4
LT
1440}
1441
4be44fcd 1442static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1da177e4 1443{
4be44fcd
LB
1444 return single_open(file, acpi_video_bus_POST_seq_show,
1445 PDE(inode)->data);
1da177e4
LT
1446}
1447
4be44fcd 1448static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1449{
1450 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1451}
1452
1453static ssize_t
4be44fcd
LB
1454acpi_video_bus_write_POST(struct file *file,
1455 const char __user * buffer,
1456 size_t count, loff_t * data)
1da177e4 1457{
4be44fcd 1458 int status;
50dd0969
JE
1459 struct seq_file *m = file->private_data;
1460 struct acpi_video_bus *video = m->private;
4be44fcd 1461 char str[12] = { 0 };
27663c58 1462 unsigned long long opt, options;
1da177e4 1463
1da177e4 1464
1da177e4 1465 if (!video || count + 1 > sizeof str)
d550d98d 1466 return -EINVAL;
1da177e4
LT
1467
1468 status = acpi_video_bus_POST_options(video, &options);
1469 if (!ACPI_SUCCESS(status))
d550d98d 1470 return -EINVAL;
1da177e4
LT
1471
1472 if (copy_from_user(str, buffer, count))
d550d98d 1473 return -EFAULT;
1da177e4
LT
1474
1475 str[count] = 0;
1476 opt = strtoul(str, NULL, 0);
1477 if (opt > 3)
d550d98d 1478 return -EFAULT;
1da177e4 1479
98fb8fe1 1480 /* just in case an OEM 'forgot' the motherboard... */
1da177e4
LT
1481 options |= 1;
1482
1483 if (options & (1ul << opt)) {
4be44fcd 1484 status = acpi_video_bus_set_POST(video, opt);
1da177e4 1485 if (!ACPI_SUCCESS(status))
d550d98d 1486 return -EFAULT;
1da177e4
LT
1487
1488 }
1489
d550d98d 1490 return count;
1da177e4
LT
1491}
1492
1493static ssize_t
4be44fcd
LB
1494acpi_video_bus_write_DOS(struct file *file,
1495 const char __user * buffer,
1496 size_t count, loff_t * data)
1da177e4 1497{
4be44fcd 1498 int status;
50dd0969
JE
1499 struct seq_file *m = file->private_data;
1500 struct acpi_video_bus *video = m->private;
4be44fcd
LB
1501 char str[12] = { 0 };
1502 unsigned long opt;
1da177e4 1503
1da177e4 1504
1da177e4 1505 if (!video || count + 1 > sizeof str)
d550d98d 1506 return -EINVAL;
1da177e4
LT
1507
1508 if (copy_from_user(str, buffer, count))
d550d98d 1509 return -EFAULT;
1da177e4
LT
1510
1511 str[count] = 0;
1512 opt = strtoul(str, NULL, 0);
1513 if (opt > 7)
d550d98d 1514 return -EFAULT;
1da177e4 1515
4be44fcd 1516 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1da177e4
LT
1517
1518 if (!ACPI_SUCCESS(status))
d550d98d 1519 return -EFAULT;
1da177e4 1520
d550d98d 1521 return count;
1da177e4
LT
1522}
1523
4be44fcd 1524static int acpi_video_bus_add_fs(struct acpi_device *device)
1da177e4 1525{
251cb0bc
DT
1526 struct acpi_video_bus *video = acpi_driver_data(device);
1527 struct proc_dir_entry *device_dir;
1528 struct proc_dir_entry *entry;
1da177e4 1529
251cb0bc
DT
1530 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1531 if (!device_dir)
1532 return -ENOMEM;
1da177e4 1533
1da177e4 1534 /* 'info' [R] */
e0066c4e 1535 entry = proc_create_data("info", S_IRUGO, device_dir,
cf7acfab
DL
1536 &acpi_video_bus_info_fops,
1537 acpi_driver_data(device));
1da177e4 1538 if (!entry)
251cb0bc 1539 goto err_remove_dir;
1da177e4
LT
1540
1541 /* 'ROM' [R] */
e0066c4e 1542 entry = proc_create_data("ROM", S_IRUGO, device_dir,
cf7acfab
DL
1543 &acpi_video_bus_ROM_fops,
1544 acpi_driver_data(device));
1da177e4 1545 if (!entry)
251cb0bc 1546 goto err_remove_info;
1da177e4
LT
1547
1548 /* 'POST_info' [R] */
e0066c4e 1549 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
cf7acfab
DL
1550 &acpi_video_bus_POST_info_fops,
1551 acpi_driver_data(device));
1da177e4 1552 if (!entry)
251cb0bc 1553 goto err_remove_rom;
1da177e4
LT
1554
1555 /* 'POST' [R/W] */
08acd4f8 1556 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
e0066c4e 1557 device_dir,
cf7acfab
DL
1558 &acpi_video_bus_POST_fops,
1559 acpi_driver_data(device));
1da177e4 1560 if (!entry)
251cb0bc 1561 goto err_remove_post_info;
1da177e4
LT
1562
1563 /* 'DOS' [R/W] */
08acd4f8 1564 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
e0066c4e 1565 device_dir,
cf7acfab
DL
1566 &acpi_video_bus_DOS_fops,
1567 acpi_driver_data(device));
1da177e4 1568 if (!entry)
251cb0bc 1569 goto err_remove_post;
1da177e4 1570
251cb0bc 1571 video->dir = acpi_device_dir(device) = device_dir;
d550d98d 1572 return 0;
251cb0bc
DT
1573
1574 err_remove_post:
1575 remove_proc_entry("POST", device_dir);
1576 err_remove_post_info:
1577 remove_proc_entry("POST_info", device_dir);
1578 err_remove_rom:
1579 remove_proc_entry("ROM", device_dir);
1580 err_remove_info:
1581 remove_proc_entry("info", device_dir);
1582 err_remove_dir:
1583 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1584 return -ENOMEM;
1da177e4
LT
1585}
1586
4be44fcd 1587static int acpi_video_bus_remove_fs(struct acpi_device *device)
1da177e4 1588{
251cb0bc
DT
1589 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1590
1591 if (device_dir) {
1592 remove_proc_entry("info", device_dir);
1593 remove_proc_entry("ROM", device_dir);
1594 remove_proc_entry("POST_info", device_dir);
1595 remove_proc_entry("POST", device_dir);
1596 remove_proc_entry("DOS", device_dir);
4be44fcd 1597 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1da177e4
LT
1598 acpi_device_dir(device) = NULL;
1599 }
1600
d550d98d 1601 return 0;
1da177e4
LT
1602}
1603
1604/* --------------------------------------------------------------------------
1605 Driver Interface
1606 -------------------------------------------------------------------------- */
1607
1608/* device interface */
82cae999
RZ
1609static struct acpi_video_device_attrib*
1610acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1611{
78eed028
DT
1612 struct acpi_video_enumerated_device *ids;
1613 int i;
1614
1615 for (i = 0; i < video->attached_count; i++) {
1616 ids = &video->attached_array[i];
1617 if ((ids->value.int_val & 0xffff) == device_id)
1618 return &ids->value.attrib;
1619 }
82cae999 1620
82cae999
RZ
1621 return NULL;
1622}
1da177e4
LT
1623
1624static int
4be44fcd
LB
1625acpi_video_bus_get_one_device(struct acpi_device *device,
1626 struct acpi_video_bus *video)
1da177e4 1627{
27663c58 1628 unsigned long long device_id;
973bf491 1629 int status;
4be44fcd 1630 struct acpi_video_device *data;
82cae999 1631 struct acpi_video_device_attrib* attribute;
1da177e4
LT
1632
1633 if (!device || !video)
d550d98d 1634 return -EINVAL;
1da177e4 1635
4be44fcd
LB
1636 status =
1637 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1da177e4
LT
1638 if (ACPI_SUCCESS(status)) {
1639
36bcbec7 1640 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1da177e4 1641 if (!data)
d550d98d 1642 return -ENOMEM;
1da177e4 1643
1da177e4
LT
1644 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1645 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
db89b4f0 1646 device->driver_data = data;
1da177e4
LT
1647
1648 data->device_id = device_id;
1649 data->video = video;
1650 data->dev = device;
1651
82cae999
RZ
1652 attribute = acpi_video_get_device_attr(video, device_id);
1653
1654 if((attribute != NULL) && attribute->device_id_scheme) {
1655 switch (attribute->display_type) {
1656 case ACPI_VIDEO_DISPLAY_CRT:
1657 data->flags.crt = 1;
1658 break;
1659 case ACPI_VIDEO_DISPLAY_TV:
1660 data->flags.tvout = 1;
1661 break;
1662 case ACPI_VIDEO_DISPLAY_DVI:
1663 data->flags.dvi = 1;
1664 break;
1665 case ACPI_VIDEO_DISPLAY_LCD:
1666 data->flags.lcd = 1;
1667 break;
1668 default:
1669 data->flags.unknown = 1;
1670 break;
1671 }
1672 if(attribute->bios_can_detect)
1673 data->flags.bios = 1;
1674 } else
1da177e4 1675 data->flags.unknown = 1;
4be44fcd 1676
1da177e4
LT
1677 acpi_video_device_bind(video, data);
1678 acpi_video_device_find_cap(data);
1679
90130268 1680 status = acpi_install_notify_handler(device->handle,
4be44fcd
LB
1681 ACPI_DEVICE_NOTIFY,
1682 acpi_video_device_notify,
1683 data);
1da177e4 1684 if (ACPI_FAILURE(status)) {
55ac9a01
LM
1685 printk(KERN_ERR PREFIX
1686 "Error installing notify handler\n");
973bf491
YL
1687 if(data->brightness)
1688 kfree(data->brightness->levels);
1689 kfree(data->brightness);
1690 kfree(data);
1691 return -ENODEV;
1da177e4
LT
1692 }
1693
bbac81f5 1694 mutex_lock(&video->device_list_lock);
1da177e4 1695 list_add_tail(&data->entry, &video->video_device_list);
bbac81f5 1696 mutex_unlock(&video->device_list_lock);
1da177e4
LT
1697
1698 acpi_video_device_add_fs(device);
1699
d550d98d 1700 return 0;
1da177e4
LT
1701 }
1702
d550d98d 1703 return -ENOENT;
1da177e4
LT
1704}
1705
1706/*
1707 * Arg:
1708 * video : video bus device
1709 *
1710 * Return:
1711 * none
1712 *
1713 * Enumerate the video device list of the video bus,
1714 * bind the ids with the corresponding video devices
1715 * under the video bus.
4be44fcd 1716 */
1da177e4 1717
4be44fcd 1718static void acpi_video_device_rebind(struct acpi_video_bus *video)
1da177e4 1719{
ff102ea9
DT
1720 struct acpi_video_device *dev;
1721
bbac81f5 1722 mutex_lock(&video->device_list_lock);
ff102ea9
DT
1723
1724 list_for_each_entry(dev, &video->video_device_list, entry)
4be44fcd 1725 acpi_video_device_bind(video, dev);
ff102ea9 1726
bbac81f5 1727 mutex_unlock(&video->device_list_lock);
1da177e4
LT
1728}
1729
1730/*
1731 * Arg:
1732 * video : video bus device
1733 * device : video output device under the video
1734 * bus
1735 *
1736 * Return:
1737 * none
1738 *
1739 * Bind the ids with the corresponding video devices
1740 * under the video bus.
4be44fcd 1741 */
1da177e4
LT
1742
1743static void
4be44fcd
LB
1744acpi_video_device_bind(struct acpi_video_bus *video,
1745 struct acpi_video_device *device)
1da177e4 1746{
78eed028 1747 struct acpi_video_enumerated_device *ids;
4be44fcd 1748 int i;
1da177e4 1749
78eed028
DT
1750 for (i = 0; i < video->attached_count; i++) {
1751 ids = &video->attached_array[i];
1752 if (device->device_id == (ids->value.int_val & 0xffff)) {
1753 ids->bind_info = device;
1da177e4
LT
1754 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1755 }
1756 }
1da177e4
LT
1757}
1758
1759/*
1760 * Arg:
1761 * video : video bus device
1762 *
1763 * Return:
1764 * < 0 : error
1765 *
1766 * Call _DOD to enumerate all devices attached to display adapter
1767 *
4be44fcd 1768 */
1da177e4
LT
1769
1770static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1771{
4be44fcd
LB
1772 int status;
1773 int count;
1774 int i;
78eed028 1775 struct acpi_video_enumerated_device *active_list;
4be44fcd
LB
1776 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1777 union acpi_object *dod = NULL;
1778 union acpi_object *obj;
1da177e4 1779
90130268 1780 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1da177e4 1781 if (!ACPI_SUCCESS(status)) {
a6fc6720 1782 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
d550d98d 1783 return status;
1da177e4
LT
1784 }
1785
50dd0969 1786 dod = buffer.pointer;
1da177e4 1787 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
a6fc6720 1788 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1da177e4
LT
1789 status = -EFAULT;
1790 goto out;
1791 }
1792
1793 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
4be44fcd 1794 dod->package.count));
1da177e4 1795
78eed028
DT
1796 active_list = kcalloc(1 + dod->package.count,
1797 sizeof(struct acpi_video_enumerated_device),
1798 GFP_KERNEL);
1799 if (!active_list) {
1da177e4
LT
1800 status = -ENOMEM;
1801 goto out;
1802 }
1803
1804 count = 0;
1805 for (i = 0; i < dod->package.count; i++) {
50dd0969 1806 obj = &dod->package.elements[i];
1da177e4
LT
1807
1808 if (obj->type != ACPI_TYPE_INTEGER) {
78eed028
DT
1809 printk(KERN_ERR PREFIX
1810 "Invalid _DOD data in element %d\n", i);
1811 continue;
1da177e4 1812 }
78eed028
DT
1813
1814 active_list[count].value.int_val = obj->integer.value;
1815 active_list[count].bind_info = NULL;
4be44fcd
LB
1816 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1817 (int)obj->integer.value));
1da177e4
LT
1818 count++;
1819 }
1da177e4 1820
6044ec88 1821 kfree(video->attached_array);
4be44fcd 1822
78eed028 1823 video->attached_array = active_list;
1da177e4 1824 video->attached_count = count;
78eed028
DT
1825
1826 out:
02438d87 1827 kfree(buffer.pointer);
d550d98d 1828 return status;
1da177e4
LT
1829}
1830
4be44fcd
LB
1831static int
1832acpi_video_get_next_level(struct acpi_video_device *device,
1833 u32 level_current, u32 event)
1da177e4 1834{
63f0edfc 1835 int min, max, min_above, max_below, i, l, delta = 255;
f4715189
TT
1836 max = max_below = 0;
1837 min = min_above = 255;
63f0edfc 1838 /* Find closest level to level_current */
0a3db1ce 1839 for (i = 2; i < device->brightness->count; i++) {
63f0edfc
AS
1840 l = device->brightness->levels[i];
1841 if (abs(l - level_current) < abs(delta)) {
1842 delta = l - level_current;
1843 if (!delta)
1844 break;
1845 }
1846 }
1847 /* Ajust level_current to closest available level */
1848 level_current += delta;
0a3db1ce 1849 for (i = 2; i < device->brightness->count; i++) {
f4715189
TT
1850 l = device->brightness->levels[i];
1851 if (l < min)
1852 min = l;
1853 if (l > max)
1854 max = l;
1855 if (l < min_above && l > level_current)
1856 min_above = l;
1857 if (l > max_below && l < level_current)
1858 max_below = l;
1859 }
1860
1861 switch (event) {
1862 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1863 return (level_current < max) ? min_above : min;
1864 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1865 return (level_current < max) ? min_above : max;
1866 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1867 return (level_current > min) ? max_below : min;
1868 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1869 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1870 return 0;
1871 default:
1872 return level_current;
1873 }
1da177e4
LT
1874}
1875
c8890f90 1876static int
4be44fcd 1877acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1da177e4 1878{
27663c58 1879 unsigned long long level_current, level_next;
c8890f90
ZR
1880 int result = -EINVAL;
1881
469778c1 1882 if (!device->brightness)
c8890f90
ZR
1883 goto out;
1884
1885 result = acpi_video_device_lcd_get_level_current(device,
1886 &level_current);
1887 if (result)
1888 goto out;
1889
1da177e4 1890 level_next = acpi_video_get_next_level(device, level_current, event);
c8890f90 1891
24450c7a 1892 result = acpi_video_device_lcd_set_level(device, level_next);
c8890f90
ZR
1893
1894out:
1895 if (result)
1896 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1897
1898 return result;
1da177e4
LT
1899}
1900
1901static int
4be44fcd
LB
1902acpi_video_bus_get_devices(struct acpi_video_bus *video,
1903 struct acpi_device *device)
1da177e4 1904{
4be44fcd 1905 int status = 0;
ff102ea9 1906 struct acpi_device *dev;
1da177e4
LT
1907
1908 acpi_video_device_enumerate(video);
1909
ff102ea9 1910 list_for_each_entry(dev, &device->children, node) {
1da177e4
LT
1911
1912 status = acpi_video_bus_get_one_device(dev, video);
1913 if (ACPI_FAILURE(status)) {
55ac9a01
LM
1914 printk(KERN_WARNING PREFIX
1915 "Cant attach device");
1da177e4
LT
1916 continue;
1917 }
1da177e4 1918 }
d550d98d 1919 return status;
1da177e4
LT
1920}
1921
4be44fcd 1922static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1da177e4 1923{
031ec77b 1924 acpi_status status;
1da177e4
LT
1925 struct acpi_video_bus *video;
1926
1da177e4
LT
1927
1928 if (!device || !device->video)
d550d98d 1929 return -ENOENT;
1da177e4
LT
1930
1931 video = device->video;
1932
1da177e4
LT
1933 acpi_video_device_remove_fs(device->dev);
1934
90130268 1935 status = acpi_remove_notify_handler(device->dev->handle,
4be44fcd
LB
1936 ACPI_DEVICE_NOTIFY,
1937 acpi_video_device_notify);
599a52d1 1938 backlight_device_unregister(device->backlight);
702ed512
ZR
1939 if (device->cdev) {
1940 sysfs_remove_link(&device->dev->dev.kobj,
1941 "thermal_cooling");
1942 sysfs_remove_link(&device->cdev->device.kobj,
1943 "device");
1944 thermal_cooling_device_unregister(device->cdev);
1945 device->cdev = NULL;
1946 }
23b0f015 1947 video_output_unregister(device->output_dev);
ff102ea9 1948
d550d98d 1949 return 0;
1da177e4
LT
1950}
1951
4be44fcd 1952static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1da177e4 1953{
4be44fcd 1954 int status;
ff102ea9 1955 struct acpi_video_device *dev, *next;
1da177e4 1956
bbac81f5 1957 mutex_lock(&video->device_list_lock);
1da177e4 1958
ff102ea9 1959 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1da177e4 1960
ff102ea9 1961 status = acpi_video_bus_put_one_device(dev);
4be44fcd
LB
1962 if (ACPI_FAILURE(status))
1963 printk(KERN_WARNING PREFIX
1964 "hhuuhhuu bug in acpi video driver.\n");
1da177e4 1965
ff102ea9
DT
1966 if (dev->brightness) {
1967 kfree(dev->brightness->levels);
1968 kfree(dev->brightness);
1969 }
1970 list_del(&dev->entry);
1971 kfree(dev);
1da177e4
LT
1972 }
1973
bbac81f5 1974 mutex_unlock(&video->device_list_lock);
ff102ea9 1975
d550d98d 1976 return 0;
1da177e4
LT
1977}
1978
1979/* acpi_video interface */
1980
4be44fcd 1981static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1da177e4 1982{
a21101c4 1983 return acpi_video_bus_DOS(video, 0, 0);
1da177e4
LT
1984}
1985
4be44fcd 1986static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1da177e4
LT
1987{
1988 return acpi_video_bus_DOS(video, 0, 1);
1989}
1990
7015558f 1991static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1da177e4 1992{
7015558f 1993 struct acpi_video_bus *video = acpi_driver_data(device);
e9dab196
LY
1994 struct input_dev *input;
1995 int keycode;
1996
1da177e4 1997 if (!video)
d550d98d 1998 return;
1da177e4 1999
e9dab196 2000 input = video->input;
1da177e4
LT
2001
2002 switch (event) {
98fb8fe1 2003 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1da177e4 2004 * most likely via hotkey. */
14e04fb3 2005 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 2006 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
2007 break;
2008
98fb8fe1 2009 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1da177e4
LT
2010 * connector. */
2011 acpi_video_device_enumerate(video);
2012 acpi_video_device_rebind(video);
14e04fb3 2013 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 2014 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
2015 break;
2016
4be44fcd 2017 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
25c87f7f 2018 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2019 keycode = KEY_SWITCHVIDEOMODE;
2020 break;
4be44fcd 2021 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
25c87f7f 2022 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2023 keycode = KEY_VIDEO_NEXT;
2024 break;
4be44fcd 2025 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
14e04fb3 2026 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 2027 keycode = KEY_VIDEO_PREV;
1da177e4
LT
2028 break;
2029
2030 default:
e9dab196 2031 keycode = KEY_UNKNOWN;
1da177e4 2032 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 2033 "Unsupported event [0x%x]\n", event));
1da177e4
LT
2034 break;
2035 }
2036
7761f638 2037 acpi_notifier_call_chain(device, event, 0);
e9dab196
LY
2038 input_report_key(input, keycode, 1);
2039 input_sync(input);
2040 input_report_key(input, keycode, 0);
2041 input_sync(input);
2042
d550d98d 2043 return;
1da177e4
LT
2044}
2045
4be44fcd 2046static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1da177e4 2047{
50dd0969 2048 struct acpi_video_device *video_device = data;
4be44fcd 2049 struct acpi_device *device = NULL;
e9dab196
LY
2050 struct acpi_video_bus *bus;
2051 struct input_dev *input;
2052 int keycode;
1da177e4 2053
1da177e4 2054 if (!video_device)
d550d98d 2055 return;
1da177e4 2056
e6afa0de 2057 device = video_device->dev;
e9dab196
LY
2058 bus = video_device->video;
2059 input = bus->input;
1da177e4
LT
2060
2061 switch (event) {
4be44fcd 2062 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
8a681a4d
ZR
2063 if (brightness_switch_enabled)
2064 acpi_video_switch_brightness(video_device, event);
14e04fb3 2065 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2066 keycode = KEY_BRIGHTNESS_CYCLE;
2067 break;
4be44fcd 2068 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
8a681a4d
ZR
2069 if (brightness_switch_enabled)
2070 acpi_video_switch_brightness(video_device, event);
25c87f7f 2071 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2072 keycode = KEY_BRIGHTNESSUP;
2073 break;
4be44fcd 2074 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
8a681a4d
ZR
2075 if (brightness_switch_enabled)
2076 acpi_video_switch_brightness(video_device, event);
25c87f7f 2077 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2078 keycode = KEY_BRIGHTNESSDOWN;
2079 break;
4be44fcd 2080 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
8a681a4d
ZR
2081 if (brightness_switch_enabled)
2082 acpi_video_switch_brightness(video_device, event);
25c87f7f 2083 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2084 keycode = KEY_BRIGHTNESS_ZERO;
2085 break;
4be44fcd 2086 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
8a681a4d
ZR
2087 if (brightness_switch_enabled)
2088 acpi_video_switch_brightness(video_device, event);
14e04fb3 2089 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 2090 keycode = KEY_DISPLAY_OFF;
1da177e4
LT
2091 break;
2092 default:
e9dab196 2093 keycode = KEY_UNKNOWN;
1da177e4 2094 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 2095 "Unsupported event [0x%x]\n", event));
1da177e4
LT
2096 break;
2097 }
e9dab196 2098
7761f638 2099 acpi_notifier_call_chain(device, event, 0);
e9dab196
LY
2100 input_report_key(input, keycode, 1);
2101 input_sync(input);
2102 input_report_key(input, keycode, 0);
2103 input_sync(input);
2104
d550d98d 2105 return;
1da177e4
LT
2106}
2107
e6d9da1d 2108static int instance;
863c1490
MG
2109static int acpi_video_resume(struct acpi_device *device)
2110{
2111 struct acpi_video_bus *video;
2112 struct acpi_video_device *video_device;
2113 int i;
2114
2115 if (!device || !acpi_driver_data(device))
2116 return -EINVAL;
2117
2118 video = acpi_driver_data(device);
2119
2120 for (i = 0; i < video->attached_count; i++) {
2121 video_device = video->attached_array[i].bind_info;
2122 if (video_device && video_device->backlight)
2123 acpi_video_set_brightness(video_device->backlight);
2124 }
2125 return AE_OK;
2126}
2127
4be44fcd 2128static int acpi_video_bus_add(struct acpi_device *device)
1da177e4 2129{
f51e8391 2130 struct acpi_video_bus *video;
e9dab196 2131 struct input_dev *input;
f51e8391 2132 int error;
1da177e4 2133
36bcbec7 2134 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1da177e4 2135 if (!video)
d550d98d 2136 return -ENOMEM;
1da177e4 2137
e6d9da1d
ZR
2138 /* a hack to fix the duplicate name "VID" problem on T61 */
2139 if (!strcmp(device->pnp.bus_id, "VID")) {
2140 if (instance)
2141 device->pnp.bus_id[3] = '0' + instance;
2142 instance ++;
2143 }
f3b39f13
ZY
2144 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2145 if (!strcmp(device->pnp.bus_id, "VGA")) {
2146 if (instance)
2147 device->pnp.bus_id[3] = '0' + instance;
2148 instance++;
2149 }
e6d9da1d 2150
e6afa0de 2151 video->device = device;
1da177e4
LT
2152 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2153 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
db89b4f0 2154 device->driver_data = video;
1da177e4
LT
2155
2156 acpi_video_bus_find_cap(video);
f51e8391
DT
2157 error = acpi_video_bus_check(video);
2158 if (error)
2159 goto err_free_video;
1da177e4 2160
f51e8391
DT
2161 error = acpi_video_bus_add_fs(device);
2162 if (error)
2163 goto err_free_video;
1da177e4 2164
bbac81f5 2165 mutex_init(&video->device_list_lock);
1da177e4
LT
2166 INIT_LIST_HEAD(&video->video_device_list);
2167
2168 acpi_video_bus_get_devices(video, device);
2169 acpi_video_bus_start_devices(video);
2170
e9dab196 2171 video->input = input = input_allocate_device();
f51e8391
DT
2172 if (!input) {
2173 error = -ENOMEM;
7015558f 2174 goto err_stop_video;
f51e8391 2175 }
e9dab196
LY
2176
2177 snprintf(video->phys, sizeof(video->phys),
2178 "%s/video/input0", acpi_device_hid(video->device));
2179
2180 input->name = acpi_device_name(video->device);
2181 input->phys = video->phys;
2182 input->id.bustype = BUS_HOST;
2183 input->id.product = 0x06;
91c05c66 2184 input->dev.parent = &device->dev;
e9dab196
LY
2185 input->evbit[0] = BIT(EV_KEY);
2186 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2187 set_bit(KEY_VIDEO_NEXT, input->keybit);
2188 set_bit(KEY_VIDEO_PREV, input->keybit);
2189 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2190 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2191 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2192 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2193 set_bit(KEY_DISPLAY_OFF, input->keybit);
2194 set_bit(KEY_UNKNOWN, input->keybit);
e9dab196 2195
f51e8391
DT
2196 error = input_register_device(input);
2197 if (error)
2198 goto err_free_input_dev;
e9dab196 2199
1da177e4 2200 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
4be44fcd
LB
2201 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2202 video->flags.multihead ? "yes" : "no",
2203 video->flags.rom ? "yes" : "no",
2204 video->flags.post ? "yes" : "no");
1da177e4 2205
f51e8391
DT
2206 return 0;
2207
2208 err_free_input_dev:
2209 input_free_device(input);
f51e8391
DT
2210 err_stop_video:
2211 acpi_video_bus_stop_devices(video);
2212 acpi_video_bus_put_devices(video);
2213 kfree(video->attached_array);
2214 acpi_video_bus_remove_fs(device);
2215 err_free_video:
2216 kfree(video);
db89b4f0 2217 device->driver_data = NULL;
1da177e4 2218
f51e8391 2219 return error;
1da177e4
LT
2220}
2221
4be44fcd 2222static int acpi_video_bus_remove(struct acpi_device *device, int type)
1da177e4 2223{
4be44fcd 2224 struct acpi_video_bus *video = NULL;
1da177e4 2225
1da177e4
LT
2226
2227 if (!device || !acpi_driver_data(device))
d550d98d 2228 return -EINVAL;
1da177e4 2229
50dd0969 2230 video = acpi_driver_data(device);
1da177e4
LT
2231
2232 acpi_video_bus_stop_devices(video);
1da177e4
LT
2233 acpi_video_bus_put_devices(video);
2234 acpi_video_bus_remove_fs(device);
2235
e9dab196 2236 input_unregister_device(video->input);
6044ec88 2237 kfree(video->attached_array);
1da177e4
LT
2238 kfree(video);
2239
d550d98d 2240 return 0;
1da177e4
LT
2241}
2242
74a365b3
MG
2243static int __init intel_opregion_present(void)
2244{
2245#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2246 struct pci_dev *dev = NULL;
2247 u32 address;
2248
2249 for_each_pci_dev(dev) {
2250 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2251 continue;
2252 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2253 continue;
2254 pci_read_config_dword(dev, 0xfc, &address);
2255 if (!address)
2256 continue;
2257 return 1;
2258 }
2259#endif
2260 return 0;
2261}
2262
2263int acpi_video_register(void)
1da177e4 2264{
4be44fcd 2265 int result = 0;
1da177e4 2266
1da177e4
LT
2267 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2268 if (!acpi_video_dir)
d550d98d 2269 return -ENODEV;
1da177e4
LT
2270
2271 result = acpi_bus_register_driver(&acpi_video_bus);
2272 if (result < 0) {
2273 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
d550d98d 2274 return -ENODEV;
1da177e4
LT
2275 }
2276
d550d98d 2277 return 0;
1da177e4 2278}
74a365b3
MG
2279EXPORT_SYMBOL(acpi_video_register);
2280
2281/*
2282 * This is kind of nasty. Hardware using Intel chipsets may require
2283 * the video opregion code to be run first in order to initialise
2284 * state before any ACPI video calls are made. To handle this we defer
2285 * registration of the video class until the opregion code has run.
2286 */
2287
2288static int __init acpi_video_init(void)
2289{
2290 if (intel_opregion_present())
2291 return 0;
2292
2293 return acpi_video_register();
2294}
1da177e4 2295
4be44fcd 2296static void __exit acpi_video_exit(void)
1da177e4 2297{
1da177e4
LT
2298
2299 acpi_bus_unregister_driver(&acpi_video_bus);
2300
2301 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2302
d550d98d 2303 return;
1da177e4
LT
2304}
2305
2306module_init(acpi_video_init);
2307module_exit(acpi_video_exit);