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