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