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 <asm/uaccess.h>
45 #include <linux/version.h>
47 #include <acpi/acpi_drivers.h>
49 MODULE_AUTHOR("John Belmonte");
50 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
51 MODULE_LICENSE("GPL");
53 #define MY_LOGPREFIX "toshiba_acpi: "
54 #define MY_ERR KERN_ERR MY_LOGPREFIX
55 #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
56 #define MY_INFO KERN_INFO MY_LOGPREFIX
58 /* Toshiba ACPI method paths */
59 #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
60 #define METHOD_HCI_1 "\\_SB_.VALD.GHCI"
61 #define METHOD_HCI_2 "\\_SB_.VALZ.GHCI"
62 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
64 /* Toshiba HCI interface definitions
66 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
67 * be uniform across all their models. Ideally we would just call
68 * dedicated ACPI methods instead of using this primitive interface.
69 * However the ACPI methods seem to be incomplete in some areas (for
70 * example they allow setting, but not reading, the LCD brightness value),
71 * so this is still useful.
77 #define HCI_SET 0xff00
78 #define HCI_GET 0xfe00
81 #define HCI_SUCCESS 0x0000
82 #define HCI_FAILURE 0x1000
83 #define HCI_NOT_SUPPORTED 0x8000
84 #define HCI_EMPTY 0x8c00
87 #define HCI_FAN 0x0004
88 #define HCI_SYSTEM_EVENT 0x0016
89 #define HCI_VIDEO_OUT 0x001c
90 #define HCI_HOTKEY_EVENT 0x001e
91 #define HCI_LCD_BRIGHTNESS 0x002a
93 /* field definitions */
94 #define HCI_LCD_BRIGHTNESS_BITS 3
95 #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
96 #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
97 #define HCI_VIDEO_OUT_LCD 0x1
98 #define HCI_VIDEO_OUT_CRT 0x2
99 #define HCI_VIDEO_OUT_TV 0x4
104 static __inline__ void
105 _set_bit(u32* word, u32 mask, int value)
107 *word = (*word & ~mask) | (mask * value);
110 /* acpi interface wrappers
114 is_valid_acpi_path(const char* methodName)
119 status = acpi_get_handle(0, (char*)methodName, &handle);
120 return !ACPI_FAILURE(status);
124 write_acpi_int(const char* methodName, int val)
126 struct acpi_object_list params;
127 union acpi_object in_objs[1];
130 params.count = sizeof(in_objs)/sizeof(in_objs[0]);
131 params.pointer = in_objs;
132 in_objs[0].type = ACPI_TYPE_INTEGER;
133 in_objs[0].integer.value = val;
135 status = acpi_evaluate_object(0, (char*)methodName, ¶ms, 0);
136 return (status == AE_OK);
141 read_acpi_int(const char* methodName, int* pVal)
143 struct acpi_buffer results;
144 union acpi_object out_objs[1];
147 results.length = sizeof(out_objs);
148 results.pointer = out_objs;
150 status = acpi_evaluate_object(0, (char*)methodName, 0, &results);
151 *pVal = out_objs[0].integer.value;
153 return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
157 static const char* method_hci /*= 0*/;
159 /* Perform a raw HCI call. Here we don't care about input or output buffer
163 hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
165 struct acpi_object_list params;
166 union acpi_object in_objs[HCI_WORDS];
167 struct acpi_buffer results;
168 union acpi_object out_objs[HCI_WORDS+1];
172 params.count = HCI_WORDS;
173 params.pointer = in_objs;
174 for (i = 0; i < HCI_WORDS; ++i) {
175 in_objs[i].type = ACPI_TYPE_INTEGER;
176 in_objs[i].integer.value = in[i];
179 results.length = sizeof(out_objs);
180 results.pointer = out_objs;
182 status = acpi_evaluate_object(0, (char*)method_hci, ¶ms,
184 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
185 for (i = 0; i < out_objs->package.count; ++i) {
186 out[i] = out_objs->package.elements[i].integer.value;
193 /* common hci tasks (get or set one value)
195 * In addition to the ACPI status, the HCI system returns a result which
196 * may be useful (such as "not supported").
200 hci_write1(u32 reg, u32 in1, u32* result)
202 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
204 acpi_status status = hci_raw(in, out);
205 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
210 hci_read1(u32 reg, u32* out1, u32* result)
212 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
214 acpi_status status = hci_raw(in, out);
216 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
220 static struct proc_dir_entry* toshiba_proc_dir /*= 0*/;
221 static int force_fan;
222 static int last_key_event;
223 static int key_event_valid;
225 typedef struct _ProcItem
228 char* (*read_func)(char*);
229 unsigned long (*write_func)(const char*, unsigned long);
232 /* proc file handlers
236 dispatch_read(char* page, char** start, off_t off, int count, int* eof,
243 p = item->read_func(p);
245 /* ISSUE: I don't understand this code */
247 if (len <= off+count) *eof = 1;
250 if (len>count) len = count;
256 dispatch_write(struct file* file, const char* buffer,
257 unsigned long count, ProcItem* item)
262 /* Arg buffer points to userspace memory, which can't be accessed
263 * directly. Since we're making a copy, zero-terminate the
264 * destination so that sscanf can be used on it safely.
266 tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
267 if (copy_from_user(tmp_buffer, buffer, count)) {
271 tmp_buffer[count] = 0;
272 result = item->write_func(tmp_buffer, count);
284 hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
285 if (hci_result == HCI_SUCCESS) {
286 value = value >> HCI_LCD_BRIGHTNESS_SHIFT;
287 p += sprintf(p, "brightness: %d\n", value);
288 p += sprintf(p, "brightness_levels: %d\n",
289 HCI_LCD_BRIGHTNESS_LEVELS);
291 printk(MY_ERR "Error reading LCD brightness\n");
298 write_lcd(const char* buffer, unsigned long count)
303 if (sscanf(buffer, " brightness : %i", &value) == 1 &&
304 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
305 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
306 hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
307 if (hci_result != HCI_SUCCESS)
322 hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
323 if (hci_result == HCI_SUCCESS) {
324 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
325 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
326 int is_tv = (value & HCI_VIDEO_OUT_TV ) ? 1 : 0;
327 p += sprintf(p, "lcd_out: %d\n", is_lcd);
328 p += sprintf(p, "crt_out: %d\n", is_crt);
329 p += sprintf(p, "tv_out: %d\n", is_tv);
331 printk(MY_ERR "Error reading video out status\n");
338 write_video(const char* buffer, unsigned long count)
348 /* scan expression. Multiple expressions may be delimited with ;
350 * NOTE: to keep scanning simple, invalid fields are ignored
353 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
355 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
357 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
359 /* advance to one character past the next ; */
364 while (remain && *(buffer-1) != ';');
367 hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
368 if (hci_result == HCI_SUCCESS) {
369 int new_video_out = video_out;
371 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
373 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
375 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
376 /* To avoid unnecessary video disruption, only write the new
377 * video setting if something changed. */
378 if (new_video_out != video_out)
379 write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
393 hci_read1(HCI_FAN, &value, &hci_result);
394 if (hci_result == HCI_SUCCESS) {
395 p += sprintf(p, "running: %d\n", (value > 0));
396 p += sprintf(p, "force_on: %d\n", force_fan);
398 printk(MY_ERR "Error reading fan status\n");
405 write_fan(const char* buffer, unsigned long count)
410 if (sscanf(buffer, " force_on : %i", &value) == 1 &&
411 value >= 0 && value <= 1) {
412 hci_write1(HCI_FAN, value, &hci_result);
413 if (hci_result != HCI_SUCCESS)
430 if (!key_event_valid) {
431 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
432 if (hci_result == HCI_SUCCESS) {
434 last_key_event = value;
435 } else if (hci_result == HCI_EMPTY) {
436 /* better luck next time */
437 } else if (hci_result == HCI_NOT_SUPPORTED) {
438 /* This is a workaround for an unresolved issue on
439 * some machines where system events sporadically
440 * become disabled. */
441 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
442 printk(MY_NOTICE "Re-enabled hotkeys\n");
444 printk(MY_ERR "Error reading hotkey status\n");
449 p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
450 p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
457 write_keys(const char* buffer, unsigned long count)
461 if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 &&
472 read_version(char* p)
474 p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
475 p += sprintf(p, "proc_interface: %d\n",
476 PROC_INTERFACE_VERSION);
480 /* proc and module init
483 #define PROC_TOSHIBA "toshiba"
485 ProcItem proc_items[] =
487 { "lcd" , read_lcd , write_lcd },
488 { "video" , read_video , write_video },
489 { "fan" , read_fan , write_fan },
490 { "keys" , read_keys , write_keys },
491 { "version" , read_version , 0 },
495 static acpi_status __init
498 struct proc_dir_entry* proc;
501 for (item = proc_items; item->name; ++item)
503 proc = create_proc_read_entry(item->name,
504 S_IFREG | S_IRUGO | S_IWUSR,
505 toshiba_proc_dir, (read_proc_t*)dispatch_read, item);
507 proc->owner = THIS_MODULE;
508 if (proc && item->write_func)
509 proc->write_proc = (write_proc_t*)dispatch_write;
515 static acpi_status __exit
520 for (item = proc_items; item->name; ++item)
521 remove_proc_entry(item->name, toshiba_proc_dir);
526 toshiba_acpi_init(void)
528 acpi_status status = AE_OK;
533 /* simple device detection: look for HCI method */
534 if (is_valid_acpi_path(METHOD_HCI_1))
535 method_hci = METHOD_HCI_1;
536 else if (is_valid_acpi_path(METHOD_HCI_2))
537 method_hci = METHOD_HCI_2;
541 printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
542 TOSHIBA_ACPI_VERSION);
543 printk(MY_INFO " HCI method: %s\n", method_hci);
548 /* enable event fifo */
549 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
551 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
552 if (!toshiba_proc_dir) {
555 toshiba_proc_dir->owner = THIS_MODULE;
556 status = add_device();
557 if (ACPI_FAILURE(status))
558 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
561 return (ACPI_SUCCESS(status)) ? 0 : -ENODEV;
565 toshiba_acpi_exit(void)
569 if (toshiba_proc_dir)
570 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
575 module_init(toshiba_acpi_init);
576 module_exit(toshiba_acpi_exit);