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