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