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