2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
5 * Copyright (C) 2002-2004 John Belmonte
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
26 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27 * engineering the Windows drivers
28 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29 * Rob Miller - TV out and hotkeys help
36 #define TOSHIBA_ACPI_VERSION "0.18"
37 #define PROC_INTERFACE_VERSION 1
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/types.h>
43 #include <linux/proc_fs.h>
44 #include <linux/backlight.h>
46 #include <asm/uaccess.h>
48 #include <acpi/acpi_drivers.h>
50 MODULE_AUTHOR("John Belmonte");
51 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
52 MODULE_LICENSE("GPL");
54 #define MY_LOGPREFIX "toshiba_acpi: "
55 #define MY_ERR KERN_ERR MY_LOGPREFIX
56 #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
57 #define MY_INFO KERN_INFO MY_LOGPREFIX
59 /* Toshiba ACPI method paths */
60 #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
61 #define METHOD_HCI_1 "\\_SB_.VALD.GHCI"
62 #define METHOD_HCI_2 "\\_SB_.VALZ.GHCI"
63 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
65 /* Toshiba HCI interface definitions
67 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
68 * be uniform across all their models. Ideally we would just call
69 * dedicated ACPI methods instead of using this primitive interface.
70 * However the ACPI methods seem to be incomplete in some areas (for
71 * example they allow setting, but not reading, the LCD brightness value),
72 * so this is still useful.
78 #define HCI_SET 0xff00
79 #define HCI_GET 0xfe00
82 #define HCI_SUCCESS 0x0000
83 #define HCI_FAILURE 0x1000
84 #define HCI_NOT_SUPPORTED 0x8000
85 #define HCI_EMPTY 0x8c00
88 #define HCI_FAN 0x0004
89 #define HCI_SYSTEM_EVENT 0x0016
90 #define HCI_VIDEO_OUT 0x001c
91 #define HCI_HOTKEY_EVENT 0x001e
92 #define HCI_LCD_BRIGHTNESS 0x002a
94 /* field definitions */
95 #define HCI_LCD_BRIGHTNESS_BITS 3
96 #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
97 #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
98 #define HCI_VIDEO_OUT_LCD 0x1
99 #define HCI_VIDEO_OUT_CRT 0x2
100 #define HCI_VIDEO_OUT_TV 0x4
105 static __inline__ void _set_bit(u32 * word, u32 mask, int value)
107 *word = (*word & ~mask) | (mask * value);
110 /* acpi interface wrappers
113 static int is_valid_acpi_path(const char *methodName)
118 status = acpi_get_handle(NULL, (char *)methodName, &handle);
119 return !ACPI_FAILURE(status);
122 static int write_acpi_int(const char *methodName, int val)
124 struct acpi_object_list params;
125 union acpi_object in_objs[1];
128 params.count = sizeof(in_objs) / sizeof(in_objs[0]);
129 params.pointer = in_objs;
130 in_objs[0].type = ACPI_TYPE_INTEGER;
131 in_objs[0].integer.value = val;
133 status = acpi_evaluate_object(NULL, (char *)methodName, ¶ms, NULL);
134 return (status == AE_OK);
138 static int read_acpi_int(const char *methodName, int *pVal)
140 struct acpi_buffer results;
141 union acpi_object out_objs[1];
144 results.length = sizeof(out_objs);
145 results.pointer = out_objs;
147 status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
148 *pVal = out_objs[0].integer.value;
150 return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
154 static const char *method_hci /*= 0*/ ;
156 /* Perform a raw HCI call. Here we don't care about input or output buffer
159 static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
161 struct acpi_object_list params;
162 union acpi_object in_objs[HCI_WORDS];
163 struct acpi_buffer results;
164 union acpi_object out_objs[HCI_WORDS + 1];
168 params.count = HCI_WORDS;
169 params.pointer = in_objs;
170 for (i = 0; i < HCI_WORDS; ++i) {
171 in_objs[i].type = ACPI_TYPE_INTEGER;
172 in_objs[i].integer.value = in[i];
175 results.length = sizeof(out_objs);
176 results.pointer = out_objs;
178 status = acpi_evaluate_object(NULL, (char *)method_hci, ¶ms,
180 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
181 for (i = 0; i < out_objs->package.count; ++i) {
182 out[i] = out_objs->package.elements[i].integer.value;
189 /* common hci tasks (get or set one value)
191 * In addition to the ACPI status, the HCI system returns a result which
192 * may be useful (such as "not supported").
195 static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
197 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
199 acpi_status status = hci_raw(in, out);
200 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
204 static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
206 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
208 acpi_status status = hci_raw(in, out);
210 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
214 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
215 static struct backlight_device *toshiba_backlight_device;
216 static int force_fan;
217 static int last_key_event;
218 static int key_event_valid;
220 typedef struct _ProcItem {
222 char *(*read_func) (char *);
223 unsigned long (*write_func) (const char *, unsigned long);
226 /* proc file handlers
230 dispatch_read(char *page, char **start, off_t off, int count, int *eof,
237 p = item->read_func(p);
239 /* ISSUE: I don't understand this code */
241 if (len <= off + count)
253 dispatch_write(struct file *file, const char __user * buffer,
254 unsigned long count, ProcItem * item)
259 /* Arg buffer points to userspace memory, which can't be accessed
260 * directly. Since we're making a copy, zero-terminate the
261 * destination so that sscanf can be used on it safely.
263 tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
267 if (copy_from_user(tmp_buffer, buffer, count)) {
270 tmp_buffer[count] = 0;
271 result = item->write_func(tmp_buffer, count);
277 static int get_lcd(struct backlight_device *bd)
282 hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
283 if (hci_result == HCI_SUCCESS) {
284 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
289 static char *read_lcd(char *p)
291 int value = get_lcd(NULL);
294 p += sprintf(p, "brightness: %d\n", value);
295 p += sprintf(p, "brightness_levels: %d\n",
296 HCI_LCD_BRIGHTNESS_LEVELS);
298 printk(MY_ERR "Error reading LCD brightness\n");
304 static int set_lcd(int value)
308 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
309 hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
310 if (hci_result != HCI_SUCCESS)
316 static int set_lcd_status(struct backlight_device *bd)
318 return set_lcd(bd->props->brightness);
321 static unsigned long write_lcd(const char *buffer, unsigned long count)
326 if (sscanf(buffer, " brightness : %i", &value) == 1 &&
327 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS)
328 ret = set_lcd(value);
334 static char *read_video(char *p)
339 hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
340 if (hci_result == HCI_SUCCESS) {
341 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
342 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
343 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
344 p += sprintf(p, "lcd_out: %d\n", is_lcd);
345 p += sprintf(p, "crt_out: %d\n", is_crt);
346 p += sprintf(p, "tv_out: %d\n", is_tv);
348 printk(MY_ERR "Error reading video out status\n");
354 static unsigned long write_video(const char *buffer, unsigned long count)
364 /* scan expression. Multiple expressions may be delimited with ;
366 * NOTE: to keep scanning simple, invalid fields are ignored
369 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
371 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
373 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
375 /* advance to one character past the next ; */
380 while (remain && *(buffer - 1) != ';');
383 hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
384 if (hci_result == HCI_SUCCESS) {
385 int new_video_out = video_out;
387 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
389 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
391 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
392 /* To avoid unnecessary video disruption, only write the new
393 * video setting if something changed. */
394 if (new_video_out != video_out)
395 write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
403 static char *read_fan(char *p)
408 hci_read1(HCI_FAN, &value, &hci_result);
409 if (hci_result == HCI_SUCCESS) {
410 p += sprintf(p, "running: %d\n", (value > 0));
411 p += sprintf(p, "force_on: %d\n", force_fan);
413 printk(MY_ERR "Error reading fan status\n");
419 static unsigned long write_fan(const char *buffer, unsigned long count)
424 if (sscanf(buffer, " force_on : %i", &value) == 1 &&
425 value >= 0 && value <= 1) {
426 hci_write1(HCI_FAN, value, &hci_result);
427 if (hci_result != HCI_SUCCESS)
438 static char *read_keys(char *p)
443 if (!key_event_valid) {
444 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
445 if (hci_result == HCI_SUCCESS) {
447 last_key_event = value;
448 } else if (hci_result == HCI_EMPTY) {
449 /* better luck next time */
450 } else if (hci_result == HCI_NOT_SUPPORTED) {
451 /* This is a workaround for an unresolved issue on
452 * some machines where system events sporadically
453 * become disabled. */
454 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
455 printk(MY_NOTICE "Re-enabled hotkeys\n");
457 printk(MY_ERR "Error reading hotkey status\n");
462 p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
463 p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
469 static unsigned long write_keys(const char *buffer, unsigned long count)
473 if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 && value == 0) {
482 static char *read_version(char *p)
484 p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
485 p += sprintf(p, "proc_interface: %d\n",
486 PROC_INTERFACE_VERSION);
490 /* proc and module init
493 #define PROC_TOSHIBA "toshiba"
495 static ProcItem proc_items[] = {
496 {"lcd", read_lcd, write_lcd},
497 {"video", read_video, write_video},
498 {"fan", read_fan, write_fan},
499 {"keys", read_keys, write_keys},
500 {"version", read_version, NULL},
504 static acpi_status __init add_device(void)
506 struct proc_dir_entry *proc;
509 for (item = proc_items; item->name; ++item) {
510 proc = create_proc_read_entry(item->name,
511 S_IFREG | S_IRUGO | S_IWUSR,
513 (read_proc_t *) dispatch_read,
516 proc->owner = THIS_MODULE;
517 if (proc && item->write_func)
518 proc->write_proc = (write_proc_t *) dispatch_write;
524 static acpi_status __exit remove_device(void)
528 for (item = proc_items; item->name; ++item)
529 remove_proc_entry(item->name, toshiba_proc_dir);
533 static struct backlight_properties toshiba_backlight_data = {
534 .owner = THIS_MODULE,
535 .get_brightness = get_lcd,
536 .update_status = set_lcd_status,
537 .max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1,
540 static void __exit toshiba_acpi_exit(void)
542 if (toshiba_backlight_device)
543 backlight_device_unregister(toshiba_backlight_device);
547 if (toshiba_proc_dir)
548 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
553 static int __init toshiba_acpi_init(void)
555 acpi_status status = AE_OK;
561 if (!acpi_specific_hotkey_enabled) {
562 printk(MY_INFO "Using generic hotkey driver\n");
565 /* simple device detection: look for HCI method */
566 if (is_valid_acpi_path(METHOD_HCI_1))
567 method_hci = METHOD_HCI_1;
568 else if (is_valid_acpi_path(METHOD_HCI_2))
569 method_hci = METHOD_HCI_2;
573 printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
574 TOSHIBA_ACPI_VERSION);
575 printk(MY_INFO " HCI method: %s\n", method_hci);
580 /* enable event fifo */
581 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
583 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
584 if (!toshiba_proc_dir) {
587 toshiba_proc_dir->owner = THIS_MODULE;
588 status = add_device();
589 if (ACPI_FAILURE(status))
590 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
593 toshiba_backlight_device = backlight_device_register("toshiba", NULL,
594 &toshiba_backlight_data);
595 if (IS_ERR(toshiba_backlight_device)) {
596 printk(KERN_ERR "Could not register toshiba backlight device\n");
597 toshiba_backlight_device = NULL;
601 return (ACPI_SUCCESS(status)) ? 0 : -ENODEV;
604 module_init(toshiba_acpi_init);
605 module_exit(toshiba_acpi_exit);