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