2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
5 * Copyright (C) 2002-2004 John Belmonte
6 * Copyright (C) 2008 Philip Langdale
7 * Copyright (C) 2010 Pierre Ducroquet
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * The devolpment page for this driver is located at
25 * http://memebeam.org/toys/ToshibaAcpiDriver.
28 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
29 * engineering the Windows drivers
30 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
31 * Rob Miller - TV out and hotkeys help
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 #define TOSHIBA_ACPI_VERSION "0.19"
41 #define PROC_INTERFACE_VERSION 1
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/types.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
49 #include <linux/backlight.h>
50 #include <linux/rfkill.h>
51 #include <linux/input.h>
52 #include <linux/input/sparse-keymap.h>
53 #include <linux/leds.h>
54 #include <linux/slab.h>
55 #include <linux/workqueue.h>
56 #include <linux/i8042.h>
58 #include <asm/uaccess.h>
60 #include <acpi/acpi_drivers.h>
62 MODULE_AUTHOR("John Belmonte");
63 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
64 MODULE_LICENSE("GPL");
66 #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
68 /* Scan code for Fn key on TOS1900 models */
69 #define TOS1900_FN_SCAN 0x6e
71 /* Toshiba ACPI method paths */
72 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
74 /* Toshiba HCI interface definitions
76 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
77 * be uniform across all their models. Ideally we would just call
78 * dedicated ACPI methods instead of using this primitive interface.
79 * However the ACPI methods seem to be incomplete in some areas (for
80 * example they allow setting, but not reading, the LCD brightness value),
81 * so this is still useful.
87 #define HCI_SET 0xff00
88 #define HCI_GET 0xfe00
91 #define HCI_SUCCESS 0x0000
92 #define HCI_FAILURE 0x1000
93 #define HCI_NOT_SUPPORTED 0x8000
94 #define HCI_EMPTY 0x8c00
97 #define HCI_FAN 0x0004
98 #define HCI_SYSTEM_EVENT 0x0016
99 #define HCI_VIDEO_OUT 0x001c
100 #define HCI_HOTKEY_EVENT 0x001e
101 #define HCI_LCD_BRIGHTNESS 0x002a
102 #define HCI_WIRELESS 0x0056
104 /* field definitions */
105 #define HCI_HOTKEY_DISABLE 0x0b
106 #define HCI_HOTKEY_ENABLE 0x09
107 #define HCI_LCD_BRIGHTNESS_BITS 3
108 #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
109 #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
110 #define HCI_VIDEO_OUT_LCD 0x1
111 #define HCI_VIDEO_OUT_CRT 0x2
112 #define HCI_VIDEO_OUT_TV 0x4
113 #define HCI_WIRELESS_KILL_SWITCH 0x01
114 #define HCI_WIRELESS_BT_PRESENT 0x0f
115 #define HCI_WIRELESS_BT_ATTACH 0x40
116 #define HCI_WIRELESS_BT_POWER 0x80
118 struct toshiba_acpi_dev {
119 struct acpi_device *acpi_dev;
120 const char *method_hci;
121 struct rfkill *bt_rfk;
122 struct input_dev *hotkey_dev;
123 struct work_struct hotkey_work;
124 struct backlight_device *backlight_dev;
125 struct led_classdev led_dev;
131 unsigned int illumination_supported:1;
132 unsigned int video_supported:1;
133 unsigned int fan_supported:1;
134 unsigned int system_event_supported:1;
135 unsigned int ntfy_supported:1;
136 unsigned int info_supported:1;
141 static struct toshiba_acpi_dev *toshiba_acpi;
143 static const struct acpi_device_id toshiba_device_ids[] = {
149 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
151 static const struct key_entry toshiba_acpi_keymap[] __devinitconst = {
152 { KE_KEY, 0x101, { KEY_MUTE } },
153 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
154 { KE_KEY, 0x103, { KEY_ZOOMIN } },
155 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
156 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
157 { KE_KEY, 0x13b, { KEY_COFFEE } },
158 { KE_KEY, 0x13c, { KEY_BATTERY } },
159 { KE_KEY, 0x13d, { KEY_SLEEP } },
160 { KE_KEY, 0x13e, { KEY_SUSPEND } },
161 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
162 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
163 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
164 { KE_KEY, 0x142, { KEY_WLAN } },
165 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
166 { KE_KEY, 0x17f, { KEY_FN } },
167 { KE_KEY, 0xb05, { KEY_PROG2 } },
168 { KE_KEY, 0xb06, { KEY_WWW } },
169 { KE_KEY, 0xb07, { KEY_MAIL } },
170 { KE_KEY, 0xb30, { KEY_STOP } },
171 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
172 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
173 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
174 { KE_KEY, 0xb5a, { KEY_MEDIA } },
175 { KE_IGNORE, 0x1430, { KEY_RESERVED } },
182 static __inline__ void _set_bit(u32 * word, u32 mask, int value)
184 *word = (*word & ~mask) | (mask * value);
187 /* acpi interface wrappers
190 static int write_acpi_int(const char *methodName, int val)
192 struct acpi_object_list params;
193 union acpi_object in_objs[1];
196 params.count = ARRAY_SIZE(in_objs);
197 params.pointer = in_objs;
198 in_objs[0].type = ACPI_TYPE_INTEGER;
199 in_objs[0].integer.value = val;
201 status = acpi_evaluate_object(NULL, (char *)methodName, ¶ms, NULL);
202 return (status == AE_OK) ? 0 : -EIO;
205 /* Perform a raw HCI call. Here we don't care about input or output buffer
208 static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
209 const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
211 struct acpi_object_list params;
212 union acpi_object in_objs[HCI_WORDS];
213 struct acpi_buffer results;
214 union acpi_object out_objs[HCI_WORDS + 1];
218 params.count = HCI_WORDS;
219 params.pointer = in_objs;
220 for (i = 0; i < HCI_WORDS; ++i) {
221 in_objs[i].type = ACPI_TYPE_INTEGER;
222 in_objs[i].integer.value = in[i];
225 results.length = sizeof(out_objs);
226 results.pointer = out_objs;
228 status = acpi_evaluate_object(dev->acpi_dev->handle,
229 (char *)dev->method_hci, ¶ms,
231 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
232 for (i = 0; i < out_objs->package.count; ++i) {
233 out[i] = out_objs->package.elements[i].integer.value;
240 /* common hci tasks (get or set one or two value)
242 * In addition to the ACPI status, the HCI system returns a result which
243 * may be useful (such as "not supported").
246 static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
247 u32 in1, u32 *result)
249 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
251 acpi_status status = hci_raw(dev, in, out);
252 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
256 static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
257 u32 *out1, u32 *result)
259 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
261 acpi_status status = hci_raw(dev, in, out);
263 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
267 static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
268 u32 in1, u32 in2, u32 *result)
270 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
272 acpi_status status = hci_raw(dev, in, out);
273 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
277 static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
278 u32 *out1, u32 *out2, u32 *result)
280 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
282 acpi_status status = hci_raw(dev, in, out);
285 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
289 /* Illumination support */
290 static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
292 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
297 status = hci_raw(dev, in, out);
298 if (ACPI_FAILURE(status)) {
299 pr_info("Illumination device not available\n");
303 status = hci_raw(dev, in, out);
307 static void toshiba_illumination_set(struct led_classdev *cdev,
308 enum led_brightness brightness)
310 struct toshiba_acpi_dev *dev = container_of(cdev,
311 struct toshiba_acpi_dev, led_dev);
312 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
316 /* First request : initialize communication. */
318 status = hci_raw(dev, in, out);
319 if (ACPI_FAILURE(status)) {
320 pr_info("Illumination device not available\n");
325 /* Switch the illumination on */
329 status = hci_raw(dev, in, out);
330 if (ACPI_FAILURE(status)) {
331 pr_info("ACPI call for illumination failed\n");
335 /* Switch the illumination off */
339 status = hci_raw(dev, in, out);
340 if (ACPI_FAILURE(status)) {
341 pr_info("ACPI call for illumination failed.\n");
346 /* Last request : close communication. */
350 hci_raw(dev, in, out);
353 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
355 struct toshiba_acpi_dev *dev = container_of(cdev,
356 struct toshiba_acpi_dev, led_dev);
357 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
360 enum led_brightness result;
362 /*Â First request : initialize communication. */
364 status = hci_raw(dev, in, out);
365 if (ACPI_FAILURE(status)) {
366 pr_info("Illumination device not available\n");
370 /* Check the illumination */
373 status = hci_raw(dev, in, out);
374 if (ACPI_FAILURE(status)) {
375 pr_info("ACPI call for illumination failed.\n");
379 result = out[2] ? LED_FULL : LED_OFF;
381 /* Last request : close communication. */
385 hci_raw(dev, in, out);
390 /* Bluetooth rfkill handlers */
392 static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
399 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
400 if (hci_result == HCI_SUCCESS)
401 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
406 static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
413 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
415 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
419 static int bt_rfkill_set_block(void *data, bool blocked)
421 struct toshiba_acpi_dev *dev = data;
422 u32 result1, result2;
427 value = (blocked == false);
429 mutex_lock(&dev->mutex);
430 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
440 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
441 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
443 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
448 mutex_unlock(&dev->mutex);
452 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
457 struct toshiba_acpi_dev *dev = data;
459 mutex_lock(&dev->mutex);
461 hci_result = hci_get_radio_state(dev, &value);
462 if (hci_result != HCI_SUCCESS) {
463 /* Can't do anything useful */
464 mutex_unlock(&dev->mutex);
468 new_rfk_state = value;
470 mutex_unlock(&dev->mutex);
472 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
473 bt_rfkill_set_block(data, true);
476 static const struct rfkill_ops toshiba_rfk_ops = {
477 .set_block = bt_rfkill_set_block,
478 .poll = bt_rfkill_poll,
481 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
483 static int get_lcd(struct backlight_device *bd)
485 struct toshiba_acpi_dev *dev = bl_get_data(bd);
489 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
490 if (hci_result == HCI_SUCCESS)
491 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
496 static int lcd_proc_show(struct seq_file *m, void *v)
498 struct toshiba_acpi_dev *dev = m->private;
501 if (!dev->backlight_dev)
504 value = get_lcd(dev->backlight_dev);
506 seq_printf(m, "brightness: %d\n", value);
507 seq_printf(m, "brightness_levels: %d\n",
508 HCI_LCD_BRIGHTNESS_LEVELS);
512 pr_err("Error reading LCD brightness\n");
516 static int lcd_proc_open(struct inode *inode, struct file *file)
518 return single_open(file, lcd_proc_show, PDE(inode)->data);
521 static int set_lcd(struct toshiba_acpi_dev *dev, int value)
525 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
526 hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
527 return hci_result == HCI_SUCCESS ? 0 : -EIO;
530 static int set_lcd_status(struct backlight_device *bd)
532 struct toshiba_acpi_dev *dev = bl_get_data(bd);
533 return set_lcd(dev, bd->props.brightness);
536 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
537 size_t count, loff_t *pos)
539 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
545 len = min(count, sizeof(cmd) - 1);
546 if (copy_from_user(cmd, buf, len))
550 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
551 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
552 ret = set_lcd(dev, value);
561 static const struct file_operations lcd_proc_fops = {
562 .owner = THIS_MODULE,
563 .open = lcd_proc_open,
566 .release = single_release,
567 .write = lcd_proc_write,
570 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
574 hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
575 return hci_result == HCI_SUCCESS ? 0 : -EIO;
578 static int video_proc_show(struct seq_file *m, void *v)
580 struct toshiba_acpi_dev *dev = m->private;
584 ret = get_video_status(dev, &value);
586 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
587 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
588 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
589 seq_printf(m, "lcd_out: %d\n", is_lcd);
590 seq_printf(m, "crt_out: %d\n", is_crt);
591 seq_printf(m, "tv_out: %d\n", is_tv);
597 static int video_proc_open(struct inode *inode, struct file *file)
599 return single_open(file, video_proc_show, PDE(inode)->data);
602 static ssize_t video_proc_write(struct file *file, const char __user *buf,
603 size_t count, loff_t *pos)
605 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
615 cmd = kmalloc(count + 1, GFP_KERNEL);
618 if (copy_from_user(cmd, buf, count)) {
626 /* scan expression. Multiple expressions may be delimited with ;
628 * NOTE: to keep scanning simple, invalid fields are ignored
631 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
633 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
635 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
637 /* advance to one character past the next ; */
642 while (remain && *(buffer - 1) != ';');
647 ret = get_video_status(dev, &video_out);
649 unsigned int new_video_out = video_out;
651 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
653 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
655 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
656 /* To avoid unnecessary video disruption, only write the new
657 * video setting if something changed. */
658 if (new_video_out != video_out)
659 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
662 return ret ? ret : count;
665 static const struct file_operations video_proc_fops = {
666 .owner = THIS_MODULE,
667 .open = video_proc_open,
670 .release = single_release,
671 .write = video_proc_write,
674 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
678 hci_read1(dev, HCI_FAN, status, &hci_result);
679 return hci_result == HCI_SUCCESS ? 0 : -EIO;
682 static int fan_proc_show(struct seq_file *m, void *v)
684 struct toshiba_acpi_dev *dev = m->private;
688 ret = get_fan_status(dev, &value);
690 seq_printf(m, "running: %d\n", (value > 0));
691 seq_printf(m, "force_on: %d\n", dev->force_fan);
697 static int fan_proc_open(struct inode *inode, struct file *file)
699 return single_open(file, fan_proc_show, PDE(inode)->data);
702 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
703 size_t count, loff_t *pos)
705 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
711 len = min(count, sizeof(cmd) - 1);
712 if (copy_from_user(cmd, buf, len))
716 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
717 value >= 0 && value <= 1) {
718 hci_write1(dev, HCI_FAN, value, &hci_result);
719 if (hci_result != HCI_SUCCESS)
722 dev->force_fan = value;
730 static const struct file_operations fan_proc_fops = {
731 .owner = THIS_MODULE,
732 .open = fan_proc_open,
735 .release = single_release,
736 .write = fan_proc_write,
739 static int keys_proc_show(struct seq_file *m, void *v)
741 struct toshiba_acpi_dev *dev = m->private;
745 if (!dev->key_event_valid && dev->system_event_supported) {
746 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
747 if (hci_result == HCI_SUCCESS) {
748 dev->key_event_valid = 1;
749 dev->last_key_event = value;
750 } else if (hci_result == HCI_EMPTY) {
751 /* better luck next time */
752 } else if (hci_result == HCI_NOT_SUPPORTED) {
753 /* This is a workaround for an unresolved issue on
754 * some machines where system events sporadically
755 * become disabled. */
756 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
757 pr_notice("Re-enabled hotkeys\n");
759 pr_err("Error reading hotkey status\n");
764 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
765 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
769 static int keys_proc_open(struct inode *inode, struct file *file)
771 return single_open(file, keys_proc_show, PDE(inode)->data);
774 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
775 size_t count, loff_t *pos)
777 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
782 len = min(count, sizeof(cmd) - 1);
783 if (copy_from_user(cmd, buf, len))
787 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
788 dev->key_event_valid = 0;
796 static const struct file_operations keys_proc_fops = {
797 .owner = THIS_MODULE,
798 .open = keys_proc_open,
801 .release = single_release,
802 .write = keys_proc_write,
805 static int version_proc_show(struct seq_file *m, void *v)
807 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
808 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
812 static int version_proc_open(struct inode *inode, struct file *file)
814 return single_open(file, version_proc_show, PDE(inode)->data);
817 static const struct file_operations version_proc_fops = {
818 .owner = THIS_MODULE,
819 .open = version_proc_open,
822 .release = single_release,
825 /* proc and module init
828 #define PROC_TOSHIBA "toshiba"
830 static void __devinit
831 create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
833 if (dev->backlight_dev)
834 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
835 &lcd_proc_fops, dev);
836 if (dev->video_supported)
837 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
838 &video_proc_fops, dev);
839 if (dev->fan_supported)
840 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
841 &fan_proc_fops, dev);
843 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
844 &keys_proc_fops, dev);
845 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
846 &version_proc_fops, dev);
849 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
851 if (dev->backlight_dev)
852 remove_proc_entry("lcd", toshiba_proc_dir);
853 if (dev->video_supported)
854 remove_proc_entry("video", toshiba_proc_dir);
855 if (dev->fan_supported)
856 remove_proc_entry("fan", toshiba_proc_dir);
858 remove_proc_entry("keys", toshiba_proc_dir);
859 remove_proc_entry("version", toshiba_proc_dir);
862 static const struct backlight_ops toshiba_backlight_data = {
863 .get_brightness = get_lcd,
864 .update_status = set_lcd_status,
867 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
873 if (unlikely(data == 0xe0))
876 if ((data & 0x7f) == TOS1900_FN_SCAN) {
877 schedule_work(&toshiba_acpi->hotkey_work);
884 static void toshiba_acpi_hotkey_work(struct work_struct *work)
886 acpi_handle ec_handle = ec_get_handle();
892 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
893 if (ACPI_FAILURE(status))
894 pr_err("ACPI NTFY method execution failed\n");
898 * Returns hotkey scancode, or < 0 on failure.
900 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
902 struct acpi_buffer buf;
903 union acpi_object out_obj;
906 buf.pointer = &out_obj;
907 buf.length = sizeof(out_obj);
909 status = acpi_evaluate_object(dev->acpi_dev->handle, "INFO",
911 if (ACPI_FAILURE(status) || out_obj.type != ACPI_TYPE_INTEGER) {
912 pr_err("ACPI INFO method execution failed\n");
916 return out_obj.integer.value;
919 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
922 if (scancode == 0x100)
925 /* act on key press; ignore key release */
929 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
930 pr_info("Unknown key %x\n", scancode);
933 static int __devinit toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
936 acpi_handle ec_handle, handle;
940 dev->hotkey_dev = input_allocate_device();
941 if (!dev->hotkey_dev) {
942 pr_info("Unable to register input device\n");
946 dev->hotkey_dev->name = "Toshiba input device";
947 dev->hotkey_dev->phys = "toshiba_acpi/input0";
948 dev->hotkey_dev->id.bustype = BUS_HOST;
950 error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL);
955 * For some machines the SCI responsible for providing hotkey
956 * notification doesn't fire. We can trigger the notification
957 * whenever the Fn key is pressed using the NTFY method, if
958 * supported, so if it's present set up an i8042 key filter
962 ec_handle = ec_get_handle();
964 status = acpi_get_handle(ec_handle, "NTFY", &handle);
966 if (ACPI_SUCCESS(status)) {
967 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
969 error = i8042_install_filter(toshiba_acpi_i8042_filter);
971 pr_err("Error installing key filter\n");
972 goto err_free_keymap;
975 dev->ntfy_supported = 1;
979 * Determine hotkey query interface. Prefer using the INFO
980 * method when it is available.
982 status = acpi_get_handle(dev->acpi_dev->handle, "INFO", &handle);
983 if (ACPI_SUCCESS(status)) {
984 dev->info_supported = 1;
986 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
987 if (hci_result == HCI_SUCCESS)
988 dev->system_event_supported = 1;
991 if (!dev->info_supported && !dev->system_event_supported) {
992 pr_warn("No hotkey query interface found\n");
993 goto err_remove_filter;
996 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
997 if (ACPI_FAILURE(status)) {
998 pr_info("Unable to enable hotkeys\n");
1000 goto err_remove_filter;
1003 error = input_register_device(dev->hotkey_dev);
1005 pr_info("Unable to register input device\n");
1006 goto err_remove_filter;
1009 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result);
1013 if (dev->ntfy_supported)
1014 i8042_remove_filter(toshiba_acpi_i8042_filter);
1016 sparse_keymap_free(dev->hotkey_dev);
1018 input_free_device(dev->hotkey_dev);
1019 dev->hotkey_dev = NULL;
1023 static int toshiba_acpi_remove(struct acpi_device *acpi_dev, int type)
1025 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1027 remove_toshiba_proc_entries(dev);
1029 if (dev->ntfy_supported) {
1030 i8042_remove_filter(toshiba_acpi_i8042_filter);
1031 cancel_work_sync(&dev->hotkey_work);
1034 if (dev->hotkey_dev) {
1035 input_unregister_device(dev->hotkey_dev);
1036 sparse_keymap_free(dev->hotkey_dev);
1040 rfkill_unregister(dev->bt_rfk);
1041 rfkill_destroy(dev->bt_rfk);
1044 if (dev->backlight_dev)
1045 backlight_device_unregister(dev->backlight_dev);
1047 if (dev->illumination_supported)
1048 led_classdev_unregister(&dev->led_dev);
1051 toshiba_acpi = NULL;
1058 static const char * __devinit find_hci_method(acpi_handle handle)
1061 acpi_handle hci_handle;
1063 status = acpi_get_handle(handle, "GHCI", &hci_handle);
1064 if (ACPI_SUCCESS(status))
1067 status = acpi_get_handle(handle, "SPFC", &hci_handle);
1068 if (ACPI_SUCCESS(status))
1074 static int __devinit toshiba_acpi_add(struct acpi_device *acpi_dev)
1076 struct toshiba_acpi_dev *dev;
1077 const char *hci_method;
1081 struct backlight_properties props;
1086 pr_info("Toshiba Laptop ACPI Extras version %s\n",
1087 TOSHIBA_ACPI_VERSION);
1089 hci_method = find_hci_method(acpi_dev->handle);
1091 pr_err("HCI interface not found\n");
1095 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1098 dev->acpi_dev = acpi_dev;
1099 dev->method_hci = hci_method;
1100 acpi_dev->driver_data = dev;
1102 if (toshiba_acpi_setup_keyboard(dev))
1103 pr_info("Unable to activate hotkeys\n");
1105 mutex_init(&dev->mutex);
1107 props.type = BACKLIGHT_PLATFORM;
1108 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
1109 dev->backlight_dev = backlight_device_register("toshiba",
1112 &toshiba_backlight_data,
1114 if (IS_ERR(dev->backlight_dev)) {
1115 ret = PTR_ERR(dev->backlight_dev);
1117 pr_err("Could not register toshiba backlight device\n");
1118 dev->backlight_dev = NULL;
1121 dev->backlight_dev->props.brightness = get_lcd(dev->backlight_dev);
1123 /* Register rfkill switch for Bluetooth */
1124 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
1125 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
1127 RFKILL_TYPE_BLUETOOTH,
1131 pr_err("unable to allocate rfkill device\n");
1136 ret = rfkill_register(dev->bt_rfk);
1138 pr_err("unable to register rfkill device\n");
1139 rfkill_destroy(dev->bt_rfk);
1144 if (toshiba_illumination_available(dev)) {
1145 dev->led_dev.name = "toshiba::illumination";
1146 dev->led_dev.max_brightness = 1;
1147 dev->led_dev.brightness_set = toshiba_illumination_set;
1148 dev->led_dev.brightness_get = toshiba_illumination_get;
1149 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
1150 dev->illumination_supported = 1;
1153 /* Determine whether or not BIOS supports fan and video interfaces */
1155 ret = get_video_status(dev, &dummy);
1156 dev->video_supported = !ret;
1158 ret = get_fan_status(dev, &dummy);
1159 dev->fan_supported = !ret;
1161 create_toshiba_proc_entries(dev);
1168 toshiba_acpi_remove(acpi_dev, 0);
1172 static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1174 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1175 u32 hci_result, value;
1182 if (dev->info_supported) {
1183 scancode = toshiba_acpi_query_hotkey(dev);
1185 pr_err("Failed to query hotkey event\n");
1186 else if (scancode != 0)
1187 toshiba_acpi_report_hotkey(dev, scancode);
1188 } else if (dev->system_event_supported) {
1190 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1191 switch (hci_result) {
1193 toshiba_acpi_report_hotkey(dev, (int)value);
1195 case HCI_NOT_SUPPORTED:
1197 * This is a workaround for an unresolved
1198 * issue on some machines where system events
1199 * sporadically become disabled.
1201 hci_write1(dev, HCI_SYSTEM_EVENT, 1,
1203 pr_notice("Re-enabled hotkeys\n");
1209 } while (retries && hci_result != HCI_EMPTY);
1213 static int toshiba_acpi_suspend(struct acpi_device *acpi_dev,
1216 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1219 if (dev->hotkey_dev)
1220 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result);
1225 static int toshiba_acpi_resume(struct acpi_device *acpi_dev)
1227 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1230 if (dev->hotkey_dev)
1231 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result);
1236 static struct acpi_driver toshiba_acpi_driver = {
1237 .name = "Toshiba ACPI driver",
1238 .owner = THIS_MODULE,
1239 .ids = toshiba_device_ids,
1240 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1242 .add = toshiba_acpi_add,
1243 .remove = toshiba_acpi_remove,
1244 .notify = toshiba_acpi_notify,
1245 .suspend = toshiba_acpi_suspend,
1246 .resume = toshiba_acpi_resume,
1250 static int __init toshiba_acpi_init(void)
1255 * Machines with this WMI guid aren't supported due to bugs in
1256 * their AML. This check relies on wmi initializing before
1257 * toshiba_acpi to guarantee guids have been identified.
1259 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
1262 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1263 if (!toshiba_proc_dir) {
1264 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
1268 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1270 pr_err("Failed to register ACPI driver: %d\n", ret);
1271 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1277 static void __exit toshiba_acpi_exit(void)
1279 acpi_bus_unregister_driver(&toshiba_acpi_driver);
1280 if (toshiba_proc_dir)
1281 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1284 module_init(toshiba_acpi_init);
1285 module_exit(toshiba_acpi_exit);