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