toshiba-acpi: Add support for Toshiba Illumination.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / toshiba_acpi.c
blob7d67a45bb2b0328c34919ec4de44dff490110852
1 /*
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.
27 * Credits:
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
34 * TODO
38 #define TOSHIBA_ACPI_VERSION "0.19"
39 #define PROC_INTERFACE_VERSION 1
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/types.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/backlight.h>
48 #include <linux/platform_device.h>
49 #include <linux/rfkill.h>
50 #include <linux/input.h>
51 #include <linux/leds.h>
52 #include <linux/slab.h>
54 #include <asm/uaccess.h>
56 #include <acpi/acpi_drivers.h>
58 MODULE_AUTHOR("John Belmonte");
59 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
60 MODULE_LICENSE("GPL");
62 #define MY_LOGPREFIX "toshiba_acpi: "
63 #define MY_ERR KERN_ERR MY_LOGPREFIX
64 #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
65 #define MY_INFO KERN_INFO MY_LOGPREFIX
67 /* Toshiba ACPI method paths */
68 #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
69 #define TOSH_INTERFACE_1 "\\_SB_.VALD"
70 #define TOSH_INTERFACE_2 "\\_SB_.VALZ"
71 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
72 #define GHCI_METHOD ".GHCI"
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.
84 #define HCI_WORDS 6
86 /* operations */
87 #define HCI_SET 0xff00
88 #define HCI_GET 0xfe00
90 /* return codes */
91 #define HCI_SUCCESS 0x0000
92 #define HCI_FAILURE 0x1000
93 #define HCI_NOT_SUPPORTED 0x8000
94 #define HCI_EMPTY 0x8c00
96 /* registers */
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_LCD_BRIGHTNESS_BITS 3
106 #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
107 #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
108 #define HCI_VIDEO_OUT_LCD 0x1
109 #define HCI_VIDEO_OUT_CRT 0x2
110 #define HCI_VIDEO_OUT_TV 0x4
111 #define HCI_WIRELESS_KILL_SWITCH 0x01
112 #define HCI_WIRELESS_BT_PRESENT 0x0f
113 #define HCI_WIRELESS_BT_ATTACH 0x40
114 #define HCI_WIRELESS_BT_POWER 0x80
116 static const struct acpi_device_id toshiba_device_ids[] = {
117 {"TOS6200", 0},
118 {"TOS6208", 0},
119 {"TOS1900", 0},
120 {"", 0},
122 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
124 struct key_entry {
125 char type;
126 u16 code;
127 u16 keycode;
130 enum {KE_KEY, KE_END};
132 static struct key_entry toshiba_acpi_keymap[] = {
133 {KE_KEY, 0x101, KEY_MUTE},
134 {KE_KEY, 0x102, KEY_ZOOMOUT},
135 {KE_KEY, 0x103, KEY_ZOOMIN},
136 {KE_KEY, 0x13b, KEY_COFFEE},
137 {KE_KEY, 0x13c, KEY_BATTERY},
138 {KE_KEY, 0x13d, KEY_SLEEP},
139 {KE_KEY, 0x13e, KEY_SUSPEND},
140 {KE_KEY, 0x13f, KEY_SWITCHVIDEOMODE},
141 {KE_KEY, 0x140, KEY_BRIGHTNESSDOWN},
142 {KE_KEY, 0x141, KEY_BRIGHTNESSUP},
143 {KE_KEY, 0x142, KEY_WLAN},
144 {KE_KEY, 0x143, KEY_PROG1},
145 {KE_KEY, 0xb05, KEY_PROG2},
146 {KE_KEY, 0xb06, KEY_WWW},
147 {KE_KEY, 0xb07, KEY_MAIL},
148 {KE_KEY, 0xb30, KEY_STOP},
149 {KE_KEY, 0xb31, KEY_PREVIOUSSONG},
150 {KE_KEY, 0xb32, KEY_NEXTSONG},
151 {KE_KEY, 0xb33, KEY_PLAYPAUSE},
152 {KE_KEY, 0xb5a, KEY_MEDIA},
153 {KE_END, 0, 0},
156 /* utility
159 static __inline__ void _set_bit(u32 * word, u32 mask, int value)
161 *word = (*word & ~mask) | (mask * value);
164 /* acpi interface wrappers
167 static int is_valid_acpi_path(const char *methodName)
169 acpi_handle handle;
170 acpi_status status;
172 status = acpi_get_handle(NULL, (char *)methodName, &handle);
173 return !ACPI_FAILURE(status);
176 static int write_acpi_int(const char *methodName, int val)
178 struct acpi_object_list params;
179 union acpi_object in_objs[1];
180 acpi_status status;
182 params.count = ARRAY_SIZE(in_objs);
183 params.pointer = in_objs;
184 in_objs[0].type = ACPI_TYPE_INTEGER;
185 in_objs[0].integer.value = val;
187 status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
188 return (status == AE_OK);
191 #if 0
192 static int read_acpi_int(const char *methodName, int *pVal)
194 struct acpi_buffer results;
195 union acpi_object out_objs[1];
196 acpi_status status;
198 results.length = sizeof(out_objs);
199 results.pointer = out_objs;
201 status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
202 *pVal = out_objs[0].integer.value;
204 return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
206 #endif
208 static const char *method_hci /*= 0*/ ;
210 /* Perform a raw HCI call. Here we don't care about input or output buffer
211 * format.
213 static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
215 struct acpi_object_list params;
216 union acpi_object in_objs[HCI_WORDS];
217 struct acpi_buffer results;
218 union acpi_object out_objs[HCI_WORDS + 1];
219 acpi_status status;
220 int i;
222 params.count = HCI_WORDS;
223 params.pointer = in_objs;
224 for (i = 0; i < HCI_WORDS; ++i) {
225 in_objs[i].type = ACPI_TYPE_INTEGER;
226 in_objs[i].integer.value = in[i];
229 results.length = sizeof(out_objs);
230 results.pointer = out_objs;
232 status = acpi_evaluate_object(NULL, (char *)method_hci, &params,
233 &results);
234 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
235 for (i = 0; i < out_objs->package.count; ++i) {
236 out[i] = out_objs->package.elements[i].integer.value;
240 return status;
243 /* common hci tasks (get or set one or two value)
245 * In addition to the ACPI status, the HCI system returns a result which
246 * may be useful (such as "not supported").
249 static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
251 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
252 u32 out[HCI_WORDS];
253 acpi_status status = hci_raw(in, out);
254 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
255 return status;
258 static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
260 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
261 u32 out[HCI_WORDS];
262 acpi_status status = hci_raw(in, out);
263 *out1 = out[2];
264 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
265 return status;
268 static acpi_status hci_write2(u32 reg, u32 in1, u32 in2, u32 *result)
270 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
271 u32 out[HCI_WORDS];
272 acpi_status status = hci_raw(in, out);
273 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
274 return status;
277 static acpi_status hci_read2(u32 reg, u32 *out1, u32 *out2, u32 *result)
279 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
280 u32 out[HCI_WORDS];
281 acpi_status status = hci_raw(in, out);
282 *out1 = out[2];
283 *out2 = out[3];
284 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
285 return status;
288 struct toshiba_acpi_dev {
289 struct platform_device *p_dev;
290 struct rfkill *bt_rfk;
291 struct input_dev *hotkey_dev;
292 int illumination_installed;
293 acpi_handle handle;
295 const char *bt_name;
297 struct mutex mutex;
300 /* Illumination support */
301 static int toshiba_illumination_available(void)
303 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
304 u32 out[HCI_WORDS];
305 acpi_status status;
307 in[0] = 0xf100;
308 status = hci_raw(in, out);
309 if (ACPI_FAILURE(status)) {
310 printk(MY_INFO "Illumination device not available\n");
311 return 0;
313 in[0] = 0xf400;
314 status = hci_raw(in, out);
315 return 1;
318 static void toshiba_illumination_set(struct led_classdev *cdev,
319 enum led_brightness brightness)
321 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
322 u32 out[HCI_WORDS];
323 acpi_status status;
325 /* First request : initialize communication. */
326 in[0] = 0xf100;
327 status = hci_raw(in, out);
328 if (ACPI_FAILURE(status)) {
329 printk(MY_INFO "Illumination device not available\n");
330 return;
333 if (brightness) {
334 /* Switch the illumination on */
335 in[0] = 0xf400;
336 in[1] = 0x14e;
337 in[2] = 1;
338 status = hci_raw(in, out);
339 if (ACPI_FAILURE(status)) {
340 printk(MY_INFO "ACPI call for illumination failed.\n");
341 return;
343 } else {
344 /* Switch the illumination off */
345 in[0] = 0xf400;
346 in[1] = 0x14e;
347 in[2] = 0;
348 status = hci_raw(in, out);
349 if (ACPI_FAILURE(status)) {
350 printk(MY_INFO "ACPI call for illumination failed.\n");
351 return;
355 /* Last request : close communication. */
356 in[0] = 0xf200;
357 in[1] = 0;
358 in[2] = 0;
359 hci_raw(in, out);
362 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
364 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
365 u32 out[HCI_WORDS];
366 acpi_status status;
367 enum led_brightness result;
369 /* First request : initialize communication. */
370 in[0] = 0xf100;
371 status = hci_raw(in, out);
372 if (ACPI_FAILURE(status)) {
373 printk(MY_INFO "Illumination device not available\n");
374 return LED_OFF;
377 /* Check the illumination */
378 in[0] = 0xf300;
379 in[1] = 0x14e;
380 status = hci_raw(in, out);
381 if (ACPI_FAILURE(status)) {
382 printk(MY_INFO "ACPI call for illumination failed.\n");
383 return LED_OFF;
386 result = out[2] ? LED_FULL : LED_OFF;
388 /* Last request : close communication. */
389 in[0] = 0xf200;
390 in[1] = 0;
391 in[2] = 0;
392 hci_raw(in, out);
394 return result;
397 static struct led_classdev toshiba_led = {
398 .name = "toshiba::illumination",
399 .max_brightness = 1,
400 .brightness_set = toshiba_illumination_set,
401 .brightness_get = toshiba_illumination_get,
404 static struct toshiba_acpi_dev toshiba_acpi = {
405 .bt_name = "Toshiba Bluetooth",
408 /* Bluetooth rfkill handlers */
410 static u32 hci_get_bt_present(bool *present)
412 u32 hci_result;
413 u32 value, value2;
415 value = 0;
416 value2 = 0;
417 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
418 if (hci_result == HCI_SUCCESS)
419 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
421 return hci_result;
424 static u32 hci_get_radio_state(bool *radio_state)
426 u32 hci_result;
427 u32 value, value2;
429 value = 0;
430 value2 = 0x0001;
431 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
433 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
434 return hci_result;
437 static int bt_rfkill_set_block(void *data, bool blocked)
439 struct toshiba_acpi_dev *dev = data;
440 u32 result1, result2;
441 u32 value;
442 int err;
443 bool radio_state;
445 value = (blocked == false);
447 mutex_lock(&dev->mutex);
448 if (hci_get_radio_state(&radio_state) != HCI_SUCCESS) {
449 err = -EBUSY;
450 goto out;
453 if (!radio_state) {
454 err = 0;
455 goto out;
458 hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
459 hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
461 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
462 err = -EBUSY;
463 else
464 err = 0;
465 out:
466 mutex_unlock(&dev->mutex);
467 return err;
470 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
472 bool new_rfk_state;
473 bool value;
474 u32 hci_result;
475 struct toshiba_acpi_dev *dev = data;
477 mutex_lock(&dev->mutex);
479 hci_result = hci_get_radio_state(&value);
480 if (hci_result != HCI_SUCCESS) {
481 /* Can't do anything useful */
482 mutex_unlock(&dev->mutex);
483 return;
486 new_rfk_state = value;
488 mutex_unlock(&dev->mutex);
490 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
491 bt_rfkill_set_block(data, true);
494 static const struct rfkill_ops toshiba_rfk_ops = {
495 .set_block = bt_rfkill_set_block,
496 .poll = bt_rfkill_poll,
499 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
500 static struct backlight_device *toshiba_backlight_device;
501 static int force_fan;
502 static int last_key_event;
503 static int key_event_valid;
505 static int get_lcd(struct backlight_device *bd)
507 u32 hci_result;
508 u32 value;
510 hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
511 if (hci_result == HCI_SUCCESS) {
512 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
513 } else
514 return -EFAULT;
517 static int lcd_proc_show(struct seq_file *m, void *v)
519 int value = get_lcd(NULL);
521 if (value >= 0) {
522 seq_printf(m, "brightness: %d\n", value);
523 seq_printf(m, "brightness_levels: %d\n",
524 HCI_LCD_BRIGHTNESS_LEVELS);
525 } else {
526 printk(MY_ERR "Error reading LCD brightness\n");
529 return 0;
532 static int lcd_proc_open(struct inode *inode, struct file *file)
534 return single_open(file, lcd_proc_show, NULL);
537 static int set_lcd(int value)
539 u32 hci_result;
541 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
542 hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
543 if (hci_result != HCI_SUCCESS)
544 return -EFAULT;
546 return 0;
549 static int set_lcd_status(struct backlight_device *bd)
551 return set_lcd(bd->props.brightness);
554 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
555 size_t count, loff_t *pos)
557 char cmd[42];
558 size_t len;
559 int value;
560 int ret;
562 len = min(count, sizeof(cmd) - 1);
563 if (copy_from_user(cmd, buf, len))
564 return -EFAULT;
565 cmd[len] = '\0';
567 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
568 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
569 ret = set_lcd(value);
570 if (ret == 0)
571 ret = count;
572 } else {
573 ret = -EINVAL;
575 return ret;
578 static const struct file_operations lcd_proc_fops = {
579 .owner = THIS_MODULE,
580 .open = lcd_proc_open,
581 .read = seq_read,
582 .llseek = seq_lseek,
583 .release = single_release,
584 .write = lcd_proc_write,
587 static int video_proc_show(struct seq_file *m, void *v)
589 u32 hci_result;
590 u32 value;
592 hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
593 if (hci_result == HCI_SUCCESS) {
594 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
595 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
596 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
597 seq_printf(m, "lcd_out: %d\n", is_lcd);
598 seq_printf(m, "crt_out: %d\n", is_crt);
599 seq_printf(m, "tv_out: %d\n", is_tv);
600 } else {
601 printk(MY_ERR "Error reading video out status\n");
604 return 0;
607 static int video_proc_open(struct inode *inode, struct file *file)
609 return single_open(file, video_proc_show, NULL);
612 static ssize_t video_proc_write(struct file *file, const char __user *buf,
613 size_t count, loff_t *pos)
615 char *cmd, *buffer;
616 int value;
617 int remain = count;
618 int lcd_out = -1;
619 int crt_out = -1;
620 int tv_out = -1;
621 u32 hci_result;
622 u32 video_out;
624 cmd = kmalloc(count + 1, GFP_KERNEL);
625 if (!cmd)
626 return -ENOMEM;
627 if (copy_from_user(cmd, buf, count)) {
628 kfree(cmd);
629 return -EFAULT;
631 cmd[count] = '\0';
633 buffer = cmd;
635 /* scan expression. Multiple expressions may be delimited with ;
637 * NOTE: to keep scanning simple, invalid fields are ignored
639 while (remain) {
640 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
641 lcd_out = value & 1;
642 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
643 crt_out = value & 1;
644 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
645 tv_out = value & 1;
646 /* advance to one character past the next ; */
647 do {
648 ++buffer;
649 --remain;
651 while (remain && *(buffer - 1) != ';');
654 kfree(cmd);
656 hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
657 if (hci_result == HCI_SUCCESS) {
658 unsigned int new_video_out = video_out;
659 if (lcd_out != -1)
660 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
661 if (crt_out != -1)
662 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
663 if (tv_out != -1)
664 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
665 /* To avoid unnecessary video disruption, only write the new
666 * video setting if something changed. */
667 if (new_video_out != video_out)
668 write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
669 } else {
670 return -EFAULT;
673 return count;
676 static const struct file_operations video_proc_fops = {
677 .owner = THIS_MODULE,
678 .open = video_proc_open,
679 .read = seq_read,
680 .llseek = seq_lseek,
681 .release = single_release,
682 .write = video_proc_write,
685 static int fan_proc_show(struct seq_file *m, void *v)
687 u32 hci_result;
688 u32 value;
690 hci_read1(HCI_FAN, &value, &hci_result);
691 if (hci_result == HCI_SUCCESS) {
692 seq_printf(m, "running: %d\n", (value > 0));
693 seq_printf(m, "force_on: %d\n", force_fan);
694 } else {
695 printk(MY_ERR "Error reading fan status\n");
698 return 0;
701 static int fan_proc_open(struct inode *inode, struct file *file)
703 return single_open(file, fan_proc_show, NULL);
706 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
707 size_t count, loff_t *pos)
709 char cmd[42];
710 size_t len;
711 int value;
712 u32 hci_result;
714 len = min(count, sizeof(cmd) - 1);
715 if (copy_from_user(cmd, buf, len))
716 return -EFAULT;
717 cmd[len] = '\0';
719 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
720 value >= 0 && value <= 1) {
721 hci_write1(HCI_FAN, value, &hci_result);
722 if (hci_result != HCI_SUCCESS)
723 return -EFAULT;
724 else
725 force_fan = value;
726 } else {
727 return -EINVAL;
730 return count;
733 static const struct file_operations fan_proc_fops = {
734 .owner = THIS_MODULE,
735 .open = fan_proc_open,
736 .read = seq_read,
737 .llseek = seq_lseek,
738 .release = single_release,
739 .write = fan_proc_write,
742 static int keys_proc_show(struct seq_file *m, void *v)
744 u32 hci_result;
745 u32 value;
747 if (!key_event_valid) {
748 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
749 if (hci_result == HCI_SUCCESS) {
750 key_event_valid = 1;
751 last_key_event = value;
752 } else if (hci_result == HCI_EMPTY) {
753 /* better luck next time */
754 } else if (hci_result == HCI_NOT_SUPPORTED) {
755 /* This is a workaround for an unresolved issue on
756 * some machines where system events sporadically
757 * become disabled. */
758 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
759 printk(MY_NOTICE "Re-enabled hotkeys\n");
760 } else {
761 printk(MY_ERR "Error reading hotkey status\n");
762 goto end;
766 seq_printf(m, "hotkey_ready: %d\n", key_event_valid);
767 seq_printf(m, "hotkey: 0x%04x\n", last_key_event);
768 end:
769 return 0;
772 static int keys_proc_open(struct inode *inode, struct file *file)
774 return single_open(file, keys_proc_show, NULL);
777 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
778 size_t count, loff_t *pos)
780 char cmd[42];
781 size_t len;
782 int value;
784 len = min(count, sizeof(cmd) - 1);
785 if (copy_from_user(cmd, buf, len))
786 return -EFAULT;
787 cmd[len] = '\0';
789 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
790 key_event_valid = 0;
791 } else {
792 return -EINVAL;
795 return count;
798 static const struct file_operations keys_proc_fops = {
799 .owner = THIS_MODULE,
800 .open = keys_proc_open,
801 .read = seq_read,
802 .llseek = seq_lseek,
803 .release = single_release,
804 .write = keys_proc_write,
807 static int version_proc_show(struct seq_file *m, void *v)
809 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
810 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
811 return 0;
814 static int version_proc_open(struct inode *inode, struct file *file)
816 return single_open(file, version_proc_show, PDE(inode)->data);
819 static const struct file_operations version_proc_fops = {
820 .owner = THIS_MODULE,
821 .open = version_proc_open,
822 .read = seq_read,
823 .llseek = seq_lseek,
824 .release = single_release,
827 /* proc and module init
830 #define PROC_TOSHIBA "toshiba"
832 static void __init create_toshiba_proc_entries(void)
834 proc_create("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir, &lcd_proc_fops);
835 proc_create("video", S_IRUGO | S_IWUSR, toshiba_proc_dir, &video_proc_fops);
836 proc_create("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir, &fan_proc_fops);
837 proc_create("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir, &keys_proc_fops);
838 proc_create("version", S_IRUGO, toshiba_proc_dir, &version_proc_fops);
841 static void remove_toshiba_proc_entries(void)
843 remove_proc_entry("lcd", toshiba_proc_dir);
844 remove_proc_entry("video", toshiba_proc_dir);
845 remove_proc_entry("fan", toshiba_proc_dir);
846 remove_proc_entry("keys", toshiba_proc_dir);
847 remove_proc_entry("version", toshiba_proc_dir);
850 static struct backlight_ops toshiba_backlight_data = {
851 .get_brightness = get_lcd,
852 .update_status = set_lcd_status,
855 static struct key_entry *toshiba_acpi_get_entry_by_scancode(unsigned int code)
857 struct key_entry *key;
859 for (key = toshiba_acpi_keymap; key->type != KE_END; key++)
860 if (code == key->code)
861 return key;
863 return NULL;
866 static struct key_entry *toshiba_acpi_get_entry_by_keycode(unsigned int code)
868 struct key_entry *key;
870 for (key = toshiba_acpi_keymap; key->type != KE_END; key++)
871 if (code == key->keycode && key->type == KE_KEY)
872 return key;
874 return NULL;
877 static int toshiba_acpi_getkeycode(struct input_dev *dev,
878 unsigned int scancode, unsigned int *keycode)
880 struct key_entry *key = toshiba_acpi_get_entry_by_scancode(scancode);
882 if (key && key->type == KE_KEY) {
883 *keycode = key->keycode;
884 return 0;
887 return -EINVAL;
890 static int toshiba_acpi_setkeycode(struct input_dev *dev,
891 unsigned int scancode, unsigned int keycode)
893 struct key_entry *key;
894 unsigned int old_keycode;
896 key = toshiba_acpi_get_entry_by_scancode(scancode);
897 if (key && key->type == KE_KEY) {
898 old_keycode = key->keycode;
899 key->keycode = keycode;
900 set_bit(keycode, dev->keybit);
901 if (!toshiba_acpi_get_entry_by_keycode(old_keycode))
902 clear_bit(old_keycode, dev->keybit);
903 return 0;
906 return -EINVAL;
909 static void toshiba_acpi_notify(acpi_handle handle, u32 event, void *context)
911 u32 hci_result, value;
912 struct key_entry *key;
914 if (event != 0x80)
915 return;
916 do {
917 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
918 if (hci_result == HCI_SUCCESS) {
919 if (value == 0x100)
920 continue;
921 /* act on key press; ignore key release */
922 if (value & 0x80)
923 continue;
925 key = toshiba_acpi_get_entry_by_scancode
926 (value);
927 if (!key) {
928 printk(MY_INFO "Unknown key %x\n",
929 value);
930 continue;
932 input_report_key(toshiba_acpi.hotkey_dev,
933 key->keycode, 1);
934 input_sync(toshiba_acpi.hotkey_dev);
935 input_report_key(toshiba_acpi.hotkey_dev,
936 key->keycode, 0);
937 input_sync(toshiba_acpi.hotkey_dev);
938 } else if (hci_result == HCI_NOT_SUPPORTED) {
939 /* This is a workaround for an unresolved issue on
940 * some machines where system events sporadically
941 * become disabled. */
942 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
943 printk(MY_NOTICE "Re-enabled hotkeys\n");
945 } while (hci_result != HCI_EMPTY);
948 static int toshiba_acpi_setup_keyboard(char *device)
950 acpi_status status;
951 acpi_handle handle;
952 int result;
953 const struct key_entry *key;
955 status = acpi_get_handle(NULL, device, &handle);
956 if (ACPI_FAILURE(status)) {
957 printk(MY_INFO "Unable to get notification device\n");
958 return -ENODEV;
961 toshiba_acpi.handle = handle;
963 status = acpi_evaluate_object(handle, "ENAB", NULL, NULL);
964 if (ACPI_FAILURE(status)) {
965 printk(MY_INFO "Unable to enable hotkeys\n");
966 return -ENODEV;
969 status = acpi_install_notify_handler(handle, ACPI_DEVICE_NOTIFY,
970 toshiba_acpi_notify, NULL);
971 if (ACPI_FAILURE(status)) {
972 printk(MY_INFO "Unable to install hotkey notification\n");
973 return -ENODEV;
976 toshiba_acpi.hotkey_dev = input_allocate_device();
977 if (!toshiba_acpi.hotkey_dev) {
978 printk(MY_INFO "Unable to register input device\n");
979 return -ENOMEM;
982 toshiba_acpi.hotkey_dev->name = "Toshiba input device";
983 toshiba_acpi.hotkey_dev->phys = device;
984 toshiba_acpi.hotkey_dev->id.bustype = BUS_HOST;
985 toshiba_acpi.hotkey_dev->getkeycode = toshiba_acpi_getkeycode;
986 toshiba_acpi.hotkey_dev->setkeycode = toshiba_acpi_setkeycode;
988 for (key = toshiba_acpi_keymap; key->type != KE_END; key++) {
989 set_bit(EV_KEY, toshiba_acpi.hotkey_dev->evbit);
990 set_bit(key->keycode, toshiba_acpi.hotkey_dev->keybit);
993 result = input_register_device(toshiba_acpi.hotkey_dev);
994 if (result) {
995 printk(MY_INFO "Unable to register input device\n");
996 return result;
999 return 0;
1002 static void toshiba_acpi_exit(void)
1004 if (toshiba_acpi.hotkey_dev)
1005 input_unregister_device(toshiba_acpi.hotkey_dev);
1007 if (toshiba_acpi.bt_rfk) {
1008 rfkill_unregister(toshiba_acpi.bt_rfk);
1009 rfkill_destroy(toshiba_acpi.bt_rfk);
1012 if (toshiba_backlight_device)
1013 backlight_device_unregister(toshiba_backlight_device);
1015 remove_toshiba_proc_entries();
1017 if (toshiba_proc_dir)
1018 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1020 acpi_remove_notify_handler(toshiba_acpi.handle, ACPI_DEVICE_NOTIFY,
1021 toshiba_acpi_notify);
1023 if (toshiba_acpi.illumination_installed)
1024 led_classdev_unregister(&toshiba_led);
1026 platform_device_unregister(toshiba_acpi.p_dev);
1028 return;
1031 static int __init toshiba_acpi_init(void)
1033 u32 hci_result;
1034 bool bt_present;
1035 int ret = 0;
1036 struct backlight_properties props;
1038 if (acpi_disabled)
1039 return -ENODEV;
1041 /* simple device detection: look for HCI method */
1042 if (is_valid_acpi_path(TOSH_INTERFACE_1 GHCI_METHOD)) {
1043 method_hci = TOSH_INTERFACE_1 GHCI_METHOD;
1044 if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_1))
1045 printk(MY_INFO "Unable to activate hotkeys\n");
1046 } else if (is_valid_acpi_path(TOSH_INTERFACE_2 GHCI_METHOD)) {
1047 method_hci = TOSH_INTERFACE_2 GHCI_METHOD;
1048 if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_2))
1049 printk(MY_INFO "Unable to activate hotkeys\n");
1050 } else
1051 return -ENODEV;
1053 printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
1054 TOSHIBA_ACPI_VERSION);
1055 printk(MY_INFO " HCI method: %s\n", method_hci);
1057 mutex_init(&toshiba_acpi.mutex);
1059 toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
1060 -1, NULL, 0);
1061 if (IS_ERR(toshiba_acpi.p_dev)) {
1062 ret = PTR_ERR(toshiba_acpi.p_dev);
1063 printk(MY_ERR "unable to register platform device\n");
1064 toshiba_acpi.p_dev = NULL;
1065 toshiba_acpi_exit();
1066 return ret;
1069 force_fan = 0;
1070 key_event_valid = 0;
1072 /* enable event fifo */
1073 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
1075 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1076 if (!toshiba_proc_dir) {
1077 toshiba_acpi_exit();
1078 return -ENODEV;
1079 } else {
1080 create_toshiba_proc_entries();
1083 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
1084 toshiba_backlight_device = backlight_device_register("toshiba",
1085 &toshiba_acpi.p_dev->dev,
1086 NULL,
1087 &toshiba_backlight_data,
1088 &props);
1089 if (IS_ERR(toshiba_backlight_device)) {
1090 ret = PTR_ERR(toshiba_backlight_device);
1092 printk(KERN_ERR "Could not register toshiba backlight device\n");
1093 toshiba_backlight_device = NULL;
1094 toshiba_acpi_exit();
1095 return ret;
1098 /* Register rfkill switch for Bluetooth */
1099 if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
1100 toshiba_acpi.bt_rfk = rfkill_alloc(toshiba_acpi.bt_name,
1101 &toshiba_acpi.p_dev->dev,
1102 RFKILL_TYPE_BLUETOOTH,
1103 &toshiba_rfk_ops,
1104 &toshiba_acpi);
1105 if (!toshiba_acpi.bt_rfk) {
1106 printk(MY_ERR "unable to allocate rfkill device\n");
1107 toshiba_acpi_exit();
1108 return -ENOMEM;
1111 ret = rfkill_register(toshiba_acpi.bt_rfk);
1112 if (ret) {
1113 printk(MY_ERR "unable to register rfkill device\n");
1114 rfkill_destroy(toshiba_acpi.bt_rfk);
1115 toshiba_acpi_exit();
1116 return ret;
1120 toshiba_acpi.illumination_installed = 0;
1121 if (toshiba_illumination_available()) {
1122 if (!led_classdev_register(&(toshiba_acpi.p_dev->dev),
1123 &toshiba_led))
1124 toshiba_acpi.illumination_installed = 1;
1127 return 0;
1130 module_init(toshiba_acpi_init);
1131 module_exit(toshiba_acpi_exit);