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