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