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