2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
5 * Copyright (C) 2002-2004 John Belmonte
6 * Copyright (C) 2008 Philip Langdale
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * The devolpment page for this driver is located at
24 * http://memebeam.org/toys/ToshibaAcpiDriver.
27 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
28 * engineering the Windows drivers
29 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
30 * Rob Miller - TV out and hotkeys help
37 #define TOSHIBA_ACPI_VERSION "0.19"
38 #define PROC_INTERFACE_VERSION 1
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/types.h>
44 #include <linux/proc_fs.h>
45 #include <linux/backlight.h>
46 #include <linux/platform_device.h>
47 #include <linux/rfkill.h>
48 #include <linux/input-polldev.h>
50 #include <asm/uaccess.h>
52 #include <acpi/acpi_drivers.h>
54 MODULE_AUTHOR("John Belmonte");
55 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
56 MODULE_LICENSE("GPL");
58 #define MY_LOGPREFIX "toshiba_acpi: "
59 #define MY_ERR KERN_ERR MY_LOGPREFIX
60 #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
61 #define MY_INFO KERN_INFO MY_LOGPREFIX
63 /* Toshiba ACPI method paths */
64 #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
65 #define METHOD_HCI_1 "\\_SB_.VALD.GHCI"
66 #define METHOD_HCI_2 "\\_SB_.VALZ.GHCI"
67 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
69 /* Toshiba HCI interface definitions
71 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
72 * be uniform across all their models. Ideally we would just call
73 * dedicated ACPI methods instead of using this primitive interface.
74 * However the ACPI methods seem to be incomplete in some areas (for
75 * example they allow setting, but not reading, the LCD brightness value),
76 * so this is still useful.
82 #define HCI_SET 0xff00
83 #define HCI_GET 0xfe00
86 #define HCI_SUCCESS 0x0000
87 #define HCI_FAILURE 0x1000
88 #define HCI_NOT_SUPPORTED 0x8000
89 #define HCI_EMPTY 0x8c00
92 #define HCI_FAN 0x0004
93 #define HCI_SYSTEM_EVENT 0x0016
94 #define HCI_VIDEO_OUT 0x001c
95 #define HCI_HOTKEY_EVENT 0x001e
96 #define HCI_LCD_BRIGHTNESS 0x002a
97 #define HCI_WIRELESS 0x0056
99 /* field definitions */
100 #define HCI_LCD_BRIGHTNESS_BITS 3
101 #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
102 #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
103 #define HCI_VIDEO_OUT_LCD 0x1
104 #define HCI_VIDEO_OUT_CRT 0x2
105 #define HCI_VIDEO_OUT_TV 0x4
106 #define HCI_WIRELESS_KILL_SWITCH 0x01
107 #define HCI_WIRELESS_BT_PRESENT 0x0f
108 #define HCI_WIRELESS_BT_ATTACH 0x40
109 #define HCI_WIRELESS_BT_POWER 0x80
111 static const struct acpi_device_id toshiba_device_ids[] = {
117 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
122 static __inline__ void _set_bit(u32 * word, u32 mask, int value)
124 *word = (*word & ~mask) | (mask * value);
127 /* acpi interface wrappers
130 static int is_valid_acpi_path(const char *methodName)
135 status = acpi_get_handle(NULL, (char *)methodName, &handle);
136 return !ACPI_FAILURE(status);
139 static int write_acpi_int(const char *methodName, int val)
141 struct acpi_object_list params;
142 union acpi_object in_objs[1];
145 params.count = ARRAY_SIZE(in_objs);
146 params.pointer = in_objs;
147 in_objs[0].type = ACPI_TYPE_INTEGER;
148 in_objs[0].integer.value = val;
150 status = acpi_evaluate_object(NULL, (char *)methodName, ¶ms, NULL);
151 return (status == AE_OK);
155 static int read_acpi_int(const char *methodName, int *pVal)
157 struct acpi_buffer results;
158 union acpi_object out_objs[1];
161 results.length = sizeof(out_objs);
162 results.pointer = out_objs;
164 status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
165 *pVal = out_objs[0].integer.value;
167 return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
171 static const char *method_hci /*= 0*/ ;
173 /* Perform a raw HCI call. Here we don't care about input or output buffer
176 static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
178 struct acpi_object_list params;
179 union acpi_object in_objs[HCI_WORDS];
180 struct acpi_buffer results;
181 union acpi_object out_objs[HCI_WORDS + 1];
185 params.count = HCI_WORDS;
186 params.pointer = in_objs;
187 for (i = 0; i < HCI_WORDS; ++i) {
188 in_objs[i].type = ACPI_TYPE_INTEGER;
189 in_objs[i].integer.value = in[i];
192 results.length = sizeof(out_objs);
193 results.pointer = out_objs;
195 status = acpi_evaluate_object(NULL, (char *)method_hci, ¶ms,
197 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
198 for (i = 0; i < out_objs->package.count; ++i) {
199 out[i] = out_objs->package.elements[i].integer.value;
206 /* common hci tasks (get or set one or two value)
208 * In addition to the ACPI status, the HCI system returns a result which
209 * may be useful (such as "not supported").
212 static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
214 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
216 acpi_status status = hci_raw(in, out);
217 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
221 static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
223 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
225 acpi_status status = hci_raw(in, out);
227 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
231 static acpi_status hci_write2(u32 reg, u32 in1, u32 in2, u32 *result)
233 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
235 acpi_status status = hci_raw(in, out);
236 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
240 static acpi_status hci_read2(u32 reg, u32 *out1, u32 *out2, u32 *result)
242 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
244 acpi_status status = hci_raw(in, out);
247 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
251 struct toshiba_acpi_dev {
252 struct platform_device *p_dev;
253 struct rfkill *rfk_dev;
254 struct input_polled_dev *poll_dev;
257 const char *rfk_name;
264 static struct toshiba_acpi_dev toshiba_acpi = {
265 .bt_name = "Toshiba Bluetooth",
266 .rfk_name = "Toshiba RFKill Switch",
267 .last_rfk_state = false,
270 /* Bluetooth rfkill handlers */
272 static u32 hci_get_bt_present(bool *present)
279 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
280 if (hci_result == HCI_SUCCESS)
281 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
286 static u32 hci_get_bt_on(bool *on)
293 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
294 if (hci_result == HCI_SUCCESS)
295 *on = (value & HCI_WIRELESS_BT_POWER) &&
296 (value & HCI_WIRELESS_BT_ATTACH);
301 static u32 hci_get_radio_state(bool *radio_state)
308 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
310 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
314 static int bt_rfkill_toggle_radio(void *data, enum rfkill_state state)
316 u32 result1, result2;
319 struct toshiba_acpi_dev *dev = data;
321 value = (state == RFKILL_STATE_UNBLOCKED);
323 if (hci_get_radio_state(&radio_state) != HCI_SUCCESS)
327 case RFKILL_STATE_UNBLOCKED:
331 case RFKILL_STATE_SOFT_BLOCKED:
337 mutex_lock(&dev->mutex);
338 hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
339 hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
340 mutex_unlock(&dev->mutex);
342 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
348 static void bt_poll_rfkill(struct input_polled_dev *poll_dev)
354 struct toshiba_acpi_dev *dev = poll_dev->private;
356 hci_result = hci_get_radio_state(&value);
357 if (hci_result != HCI_SUCCESS)
358 return; /* Can't do anything useful */
360 new_rfk_state = value;
362 mutex_lock(&dev->mutex);
363 state_changed = new_rfk_state != dev->last_rfk_state;
364 dev->last_rfk_state = new_rfk_state;
365 mutex_unlock(&dev->mutex);
367 if (unlikely(state_changed)) {
368 rfkill_force_state(dev->rfk_dev,
370 RFKILL_STATE_SOFT_BLOCKED :
371 RFKILL_STATE_HARD_BLOCKED);
372 input_report_switch(poll_dev->input, SW_RFKILL_ALL,
377 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
378 static struct backlight_device *toshiba_backlight_device;
379 static int force_fan;
380 static int last_key_event;
381 static int key_event_valid;
383 typedef struct _ProcItem {
385 char *(*read_func) (char *);
386 unsigned long (*write_func) (const char *, unsigned long);
389 /* proc file handlers
393 dispatch_read(char *page, char **start, off_t off, int count, int *eof,
400 p = item->read_func(p);
402 /* ISSUE: I don't understand this code */
404 if (len <= off + count)
416 dispatch_write(struct file *file, const char __user * buffer,
417 unsigned long count, ProcItem * item)
422 /* Arg buffer points to userspace memory, which can't be accessed
423 * directly. Since we're making a copy, zero-terminate the
424 * destination so that sscanf can be used on it safely.
426 tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
430 if (copy_from_user(tmp_buffer, buffer, count)) {
433 tmp_buffer[count] = 0;
434 result = item->write_func(tmp_buffer, count);
440 static int get_lcd(struct backlight_device *bd)
445 hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
446 if (hci_result == HCI_SUCCESS) {
447 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
452 static char *read_lcd(char *p)
454 int value = get_lcd(NULL);
457 p += sprintf(p, "brightness: %d\n", value);
458 p += sprintf(p, "brightness_levels: %d\n",
459 HCI_LCD_BRIGHTNESS_LEVELS);
461 printk(MY_ERR "Error reading LCD brightness\n");
467 static int set_lcd(int value)
471 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
472 hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
473 if (hci_result != HCI_SUCCESS)
479 static int set_lcd_status(struct backlight_device *bd)
481 return set_lcd(bd->props.brightness);
484 static unsigned long write_lcd(const char *buffer, unsigned long count)
489 if (sscanf(buffer, " brightness : %i", &value) == 1 &&
490 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
491 ret = set_lcd(value);
500 static char *read_video(char *p)
505 hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
506 if (hci_result == HCI_SUCCESS) {
507 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
508 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
509 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
510 p += sprintf(p, "lcd_out: %d\n", is_lcd);
511 p += sprintf(p, "crt_out: %d\n", is_crt);
512 p += sprintf(p, "tv_out: %d\n", is_tv);
514 printk(MY_ERR "Error reading video out status\n");
520 static unsigned long write_video(const char *buffer, unsigned long count)
530 /* scan expression. Multiple expressions may be delimited with ;
532 * NOTE: to keep scanning simple, invalid fields are ignored
535 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
537 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
539 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
541 /* advance to one character past the next ; */
546 while (remain && *(buffer - 1) != ';');
549 hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
550 if (hci_result == HCI_SUCCESS) {
551 int new_video_out = video_out;
553 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
555 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
557 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
558 /* To avoid unnecessary video disruption, only write the new
559 * video setting if something changed. */
560 if (new_video_out != video_out)
561 write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
569 static char *read_fan(char *p)
574 hci_read1(HCI_FAN, &value, &hci_result);
575 if (hci_result == HCI_SUCCESS) {
576 p += sprintf(p, "running: %d\n", (value > 0));
577 p += sprintf(p, "force_on: %d\n", force_fan);
579 printk(MY_ERR "Error reading fan status\n");
585 static unsigned long write_fan(const char *buffer, unsigned long count)
590 if (sscanf(buffer, " force_on : %i", &value) == 1 &&
591 value >= 0 && value <= 1) {
592 hci_write1(HCI_FAN, value, &hci_result);
593 if (hci_result != HCI_SUCCESS)
604 static char *read_keys(char *p)
609 if (!key_event_valid) {
610 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
611 if (hci_result == HCI_SUCCESS) {
613 last_key_event = value;
614 } else if (hci_result == HCI_EMPTY) {
615 /* better luck next time */
616 } else if (hci_result == HCI_NOT_SUPPORTED) {
617 /* This is a workaround for an unresolved issue on
618 * some machines where system events sporadically
619 * become disabled. */
620 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
621 printk(MY_NOTICE "Re-enabled hotkeys\n");
623 printk(MY_ERR "Error reading hotkey status\n");
628 p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
629 p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
635 static unsigned long write_keys(const char *buffer, unsigned long count)
639 if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 && value == 0) {
648 static char *read_version(char *p)
650 p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
651 p += sprintf(p, "proc_interface: %d\n",
652 PROC_INTERFACE_VERSION);
656 /* proc and module init
659 #define PROC_TOSHIBA "toshiba"
661 static ProcItem proc_items[] = {
662 {"lcd", read_lcd, write_lcd},
663 {"video", read_video, write_video},
664 {"fan", read_fan, write_fan},
665 {"keys", read_keys, write_keys},
666 {"version", read_version, NULL},
670 static acpi_status __init add_device(void)
672 struct proc_dir_entry *proc;
675 for (item = proc_items; item->name; ++item) {
676 proc = create_proc_read_entry(item->name,
677 S_IFREG | S_IRUGO | S_IWUSR,
679 (read_proc_t *) dispatch_read,
682 proc->owner = THIS_MODULE;
683 if (proc && item->write_func)
684 proc->write_proc = (write_proc_t *) dispatch_write;
690 static acpi_status remove_device(void)
694 for (item = proc_items; item->name; ++item)
695 remove_proc_entry(item->name, toshiba_proc_dir);
699 static struct backlight_ops toshiba_backlight_data = {
700 .get_brightness = get_lcd,
701 .update_status = set_lcd_status,
704 static void toshiba_acpi_exit(void)
706 if (toshiba_acpi.poll_dev) {
707 input_unregister_polled_device(toshiba_acpi.poll_dev);
708 input_free_polled_device(toshiba_acpi.poll_dev);
711 if (toshiba_acpi.rfk_dev)
712 rfkill_unregister(toshiba_acpi.rfk_dev);
714 if (toshiba_backlight_device)
715 backlight_device_unregister(toshiba_backlight_device);
719 if (toshiba_proc_dir)
720 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
722 platform_device_unregister(toshiba_acpi.p_dev);
727 static int __init toshiba_acpi_init(void)
729 acpi_status status = AE_OK;
739 /* simple device detection: look for HCI method */
740 if (is_valid_acpi_path(METHOD_HCI_1))
741 method_hci = METHOD_HCI_1;
742 else if (is_valid_acpi_path(METHOD_HCI_2))
743 method_hci = METHOD_HCI_2;
747 printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
748 TOSHIBA_ACPI_VERSION);
749 printk(MY_INFO " HCI method: %s\n", method_hci);
751 mutex_init(&toshiba_acpi.mutex);
753 toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
755 if (IS_ERR(toshiba_acpi.p_dev)) {
756 ret = PTR_ERR(toshiba_acpi.p_dev);
757 printk(MY_ERR "unable to register platform device\n");
758 toshiba_acpi.p_dev = NULL;
766 /* enable event fifo */
767 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
769 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
770 if (!toshiba_proc_dir) {
774 toshiba_proc_dir->owner = THIS_MODULE;
775 status = add_device();
776 if (ACPI_FAILURE(status)) {
782 toshiba_backlight_device = backlight_device_register("toshiba",
783 &toshiba_acpi.p_dev->dev,
785 &toshiba_backlight_data);
786 if (IS_ERR(toshiba_backlight_device)) {
787 ret = PTR_ERR(toshiba_backlight_device);
789 printk(KERN_ERR "Could not register toshiba backlight device\n");
790 toshiba_backlight_device = NULL;
794 toshiba_backlight_device->props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
796 /* Register rfkill switch for Bluetooth */
797 if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
798 toshiba_acpi.rfk_dev = rfkill_allocate(&toshiba_acpi.p_dev->dev,
799 RFKILL_TYPE_BLUETOOTH);
800 if (!toshiba_acpi.rfk_dev) {
801 printk(MY_ERR "unable to allocate rfkill device\n");
806 toshiba_acpi.rfk_dev->name = toshiba_acpi.bt_name;
807 toshiba_acpi.rfk_dev->toggle_radio = bt_rfkill_toggle_radio;
808 toshiba_acpi.rfk_dev->user_claim_unsupported = 1;
809 toshiba_acpi.rfk_dev->data = &toshiba_acpi;
811 if (hci_get_bt_on(&bt_on) == HCI_SUCCESS && bt_on) {
812 toshiba_acpi.rfk_dev->state = RFKILL_STATE_UNBLOCKED;
813 } else if (hci_get_radio_state(&radio_on) == HCI_SUCCESS &&
815 toshiba_acpi.rfk_dev->state = RFKILL_STATE_SOFT_BLOCKED;
817 toshiba_acpi.rfk_dev->state = RFKILL_STATE_HARD_BLOCKED;
820 ret = rfkill_register(toshiba_acpi.rfk_dev);
822 printk(MY_ERR "unable to register rfkill device\n");
828 /* Register input device for kill switch */
829 toshiba_acpi.poll_dev = input_allocate_polled_device();
830 if (!toshiba_acpi.poll_dev) {
831 printk(MY_ERR "unable to allocate kill-switch input device\n");
835 toshiba_acpi.poll_dev->private = &toshiba_acpi;
836 toshiba_acpi.poll_dev->poll = bt_poll_rfkill;
837 toshiba_acpi.poll_dev->poll_interval = 1000; /* msecs */
839 toshiba_acpi.poll_dev->input->name = toshiba_acpi.rfk_name;
840 toshiba_acpi.poll_dev->input->id.bustype = BUS_HOST;
841 toshiba_acpi.poll_dev->input->id.vendor = 0x0930; /* Toshiba USB ID */
842 set_bit(EV_SW, toshiba_acpi.poll_dev->input->evbit);
843 set_bit(SW_RFKILL_ALL, toshiba_acpi.poll_dev->input->swbit);
844 input_report_switch(toshiba_acpi.poll_dev->input, SW_RFKILL_ALL, TRUE);
846 ret = input_register_polled_device(toshiba_acpi.poll_dev);
848 printk(MY_ERR "unable to register kill-switch input device\n");
849 rfkill_free(toshiba_acpi.rfk_dev);
850 toshiba_acpi.rfk_dev = NULL;
858 module_init(toshiba_acpi_init);
859 module_exit(toshiba_acpi_exit);