toshiba_acpi: Support additional hotkey scancodes
[linux-2.6.git] / drivers / platform / x86 / toshiba_acpi.c
1 /*
2  *  toshiba_acpi.c - Toshiba Laptop ACPI Extras
3  *
4  *
5  *  Copyright (C) 2002-2004 John Belmonte
6  *  Copyright (C) 2008 Philip Langdale
7  *  Copyright (C) 2010 Pierre Ducroquet
8  *
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.
13  *
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.
18  *
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
22  *
23  *
24  *  The devolpment page for this driver is located at
25  *  http://memebeam.org/toys/ToshibaAcpiDriver.
26  *
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
32  *
33  *
34  *  TODO
35  *
36  */
37
38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39
40 #define TOSHIBA_ACPI_VERSION    "0.19"
41 #define PROC_INTERFACE_VERSION  1
42
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>
57
58 #include <asm/uaccess.h>
59
60 #include <acpi/acpi_drivers.h>
61
62 MODULE_AUTHOR("John Belmonte");
63 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
64 MODULE_LICENSE("GPL");
65
66 /* Scan code for Fn key on TOS1900 models */
67 #define TOS1900_FN_SCAN         0x6e
68
69 /* Toshiba ACPI method paths */
70 #define METHOD_VIDEO_OUT        "\\_SB_.VALX.DSSX"
71
72 /* Toshiba HCI interface definitions
73  *
74  * HCI is Toshiba's "Hardware Control Interface" which is supposed to
75  * be uniform across all their models.  Ideally we would just call
76  * dedicated ACPI methods instead of using this primitive interface.
77  * However the ACPI methods seem to be incomplete in some areas (for
78  * example they allow setting, but not reading, the LCD brightness value),
79  * so this is still useful.
80  */
81
82 #define HCI_WORDS                       6
83
84 /* operations */
85 #define HCI_SET                         0xff00
86 #define HCI_GET                         0xfe00
87
88 /* return codes */
89 #define HCI_SUCCESS                     0x0000
90 #define HCI_FAILURE                     0x1000
91 #define HCI_NOT_SUPPORTED               0x8000
92 #define HCI_EMPTY                       0x8c00
93
94 /* registers */
95 #define HCI_FAN                         0x0004
96 #define HCI_SYSTEM_EVENT                0x0016
97 #define HCI_VIDEO_OUT                   0x001c
98 #define HCI_HOTKEY_EVENT                0x001e
99 #define HCI_LCD_BRIGHTNESS              0x002a
100 #define HCI_WIRELESS                    0x0056
101
102 /* field definitions */
103 #define HCI_HOTKEY_DISABLE              0x0b
104 #define HCI_HOTKEY_ENABLE               0x09
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
115
116 struct toshiba_acpi_dev {
117         struct acpi_device *acpi_dev;
118         const char *method_hci;
119         struct rfkill *bt_rfk;
120         struct input_dev *hotkey_dev;
121         struct work_struct hotkey_work;
122         struct backlight_device *backlight_dev;
123         struct led_classdev led_dev;
124
125         int force_fan;
126         int last_key_event;
127         int key_event_valid;
128
129         unsigned int illumination_supported:1;
130         unsigned int video_supported:1;
131         unsigned int fan_supported:1;
132         unsigned int system_event_supported:1;
133         unsigned int ntfy_supported:1;
134         unsigned int info_supported:1;
135
136         struct mutex mutex;
137 };
138
139 static struct toshiba_acpi_dev *toshiba_acpi;
140
141 static const struct acpi_device_id toshiba_device_ids[] = {
142         {"TOS6200", 0},
143         {"TOS6208", 0},
144         {"TOS1900", 0},
145         {"", 0},
146 };
147 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
148
149 static const struct key_entry toshiba_acpi_keymap[] __devinitconst = {
150         { KE_KEY, 0x101, { KEY_MUTE } },
151         { KE_KEY, 0x102, { KEY_ZOOMOUT } },
152         { KE_KEY, 0x103, { KEY_ZOOMIN } },
153         { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
154         { KE_KEY, 0x139, { KEY_ZOOMRESET } },
155         { KE_KEY, 0x13b, { KEY_COFFEE } },
156         { KE_KEY, 0x13c, { KEY_BATTERY } },
157         { KE_KEY, 0x13d, { KEY_SLEEP } },
158         { KE_KEY, 0x13e, { KEY_SUSPEND } },
159         { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
160         { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
161         { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
162         { KE_KEY, 0x142, { KEY_WLAN } },
163         { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
164         { KE_KEY, 0x17f, { KEY_FN } },
165         { KE_KEY, 0xb05, { KEY_PROG2 } },
166         { KE_KEY, 0xb06, { KEY_WWW } },
167         { KE_KEY, 0xb07, { KEY_MAIL } },
168         { KE_KEY, 0xb30, { KEY_STOP } },
169         { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
170         { KE_KEY, 0xb32, { KEY_NEXTSONG } },
171         { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
172         { KE_KEY, 0xb5a, { KEY_MEDIA } },
173         { KE_IGNORE, 0x1430, { KEY_RESERVED } },
174         { KE_END, 0 },
175 };
176
177 /* utility
178  */
179
180 static __inline__ void _set_bit(u32 * word, u32 mask, int value)
181 {
182         *word = (*word & ~mask) | (mask * value);
183 }
184
185 /* acpi interface wrappers
186  */
187
188 static int write_acpi_int(const char *methodName, int val)
189 {
190         struct acpi_object_list params;
191         union acpi_object in_objs[1];
192         acpi_status status;
193
194         params.count = ARRAY_SIZE(in_objs);
195         params.pointer = in_objs;
196         in_objs[0].type = ACPI_TYPE_INTEGER;
197         in_objs[0].integer.value = val;
198
199         status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
200         return (status == AE_OK) ? 0 : -EIO;
201 }
202
203 /* Perform a raw HCI call.  Here we don't care about input or output buffer
204  * format.
205  */
206 static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
207                            const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
208 {
209         struct acpi_object_list params;
210         union acpi_object in_objs[HCI_WORDS];
211         struct acpi_buffer results;
212         union acpi_object out_objs[HCI_WORDS + 1];
213         acpi_status status;
214         int i;
215
216         params.count = HCI_WORDS;
217         params.pointer = in_objs;
218         for (i = 0; i < HCI_WORDS; ++i) {
219                 in_objs[i].type = ACPI_TYPE_INTEGER;
220                 in_objs[i].integer.value = in[i];
221         }
222
223         results.length = sizeof(out_objs);
224         results.pointer = out_objs;
225
226         status = acpi_evaluate_object(dev->acpi_dev->handle,
227                                       (char *)dev->method_hci, &params,
228                                       &results);
229         if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
230                 for (i = 0; i < out_objs->package.count; ++i) {
231                         out[i] = out_objs->package.elements[i].integer.value;
232                 }
233         }
234
235         return status;
236 }
237
238 /* common hci tasks (get or set one or two value)
239  *
240  * In addition to the ACPI status, the HCI system returns a result which
241  * may be useful (such as "not supported").
242  */
243
244 static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
245                               u32 in1, u32 *result)
246 {
247         u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
248         u32 out[HCI_WORDS];
249         acpi_status status = hci_raw(dev, in, out);
250         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
251         return status;
252 }
253
254 static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
255                              u32 *out1, u32 *result)
256 {
257         u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
258         u32 out[HCI_WORDS];
259         acpi_status status = hci_raw(dev, in, out);
260         *out1 = out[2];
261         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
262         return status;
263 }
264
265 static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
266                               u32 in1, u32 in2, u32 *result)
267 {
268         u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
269         u32 out[HCI_WORDS];
270         acpi_status status = hci_raw(dev, in, out);
271         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
272         return status;
273 }
274
275 static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
276                              u32 *out1, u32 *out2, u32 *result)
277 {
278         u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
279         u32 out[HCI_WORDS];
280         acpi_status status = hci_raw(dev, in, out);
281         *out1 = out[2];
282         *out2 = out[3];
283         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
284         return status;
285 }
286
287 /* Illumination support */
288 static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
289 {
290         u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
291         u32 out[HCI_WORDS];
292         acpi_status status;
293
294         in[0] = 0xf100;
295         status = hci_raw(dev, in, out);
296         if (ACPI_FAILURE(status)) {
297                 pr_info("Illumination device not available\n");
298                 return 0;
299         }
300         in[0] = 0xf400;
301         status = hci_raw(dev, in, out);
302         return 1;
303 }
304
305 static void toshiba_illumination_set(struct led_classdev *cdev,
306                                      enum led_brightness brightness)
307 {
308         struct toshiba_acpi_dev *dev = container_of(cdev,
309                         struct toshiba_acpi_dev, led_dev);
310         u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
311         u32 out[HCI_WORDS];
312         acpi_status status;
313
314         /* First request : initialize communication. */
315         in[0] = 0xf100;
316         status = hci_raw(dev, in, out);
317         if (ACPI_FAILURE(status)) {
318                 pr_info("Illumination device not available\n");
319                 return;
320         }
321
322         if (brightness) {
323                 /* Switch the illumination on */
324                 in[0] = 0xf400;
325                 in[1] = 0x14e;
326                 in[2] = 1;
327                 status = hci_raw(dev, in, out);
328                 if (ACPI_FAILURE(status)) {
329                         pr_info("ACPI call for illumination failed\n");
330                         return;
331                 }
332         } else {
333                 /* Switch the illumination off */
334                 in[0] = 0xf400;
335                 in[1] = 0x14e;
336                 in[2] = 0;
337                 status = hci_raw(dev, in, out);
338                 if (ACPI_FAILURE(status)) {
339                         pr_info("ACPI call for illumination failed.\n");
340                         return;
341                 }
342         }
343
344         /* Last request : close communication. */
345         in[0] = 0xf200;
346         in[1] = 0;
347         in[2] = 0;
348         hci_raw(dev, in, out);
349 }
350
351 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
352 {
353         struct toshiba_acpi_dev *dev = container_of(cdev,
354                         struct toshiba_acpi_dev, led_dev);
355         u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
356         u32 out[HCI_WORDS];
357         acpi_status status;
358         enum led_brightness result;
359
360         /*¬†First request : initialize communication. */
361         in[0] = 0xf100;
362         status = hci_raw(dev, in, out);
363         if (ACPI_FAILURE(status)) {
364                 pr_info("Illumination device not available\n");
365                 return LED_OFF;
366         }
367
368         /* Check the illumination */
369         in[0] = 0xf300;
370         in[1] = 0x14e;
371         status = hci_raw(dev, in, out);
372         if (ACPI_FAILURE(status)) {
373                 pr_info("ACPI call for illumination failed.\n");
374                 return LED_OFF;
375         }
376
377         result = out[2] ? LED_FULL : LED_OFF;
378
379         /* Last request : close communication. */
380         in[0] = 0xf200;
381         in[1] = 0;
382         in[2] = 0;
383         hci_raw(dev, in, out);
384
385         return result;
386 }
387
388 /* Bluetooth rfkill handlers */
389
390 static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
391 {
392         u32 hci_result;
393         u32 value, value2;
394
395         value = 0;
396         value2 = 0;
397         hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
398         if (hci_result == HCI_SUCCESS)
399                 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
400
401         return hci_result;
402 }
403
404 static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
405 {
406         u32 hci_result;
407         u32 value, value2;
408
409         value = 0;
410         value2 = 0x0001;
411         hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
412
413         *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
414         return hci_result;
415 }
416
417 static int bt_rfkill_set_block(void *data, bool blocked)
418 {
419         struct toshiba_acpi_dev *dev = data;
420         u32 result1, result2;
421         u32 value;
422         int err;
423         bool radio_state;
424
425         value = (blocked == false);
426
427         mutex_lock(&dev->mutex);
428         if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
429                 err = -EIO;
430                 goto out;
431         }
432
433         if (!radio_state) {
434                 err = 0;
435                 goto out;
436         }
437
438         hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
439         hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
440
441         if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
442                 err = -EIO;
443         else
444                 err = 0;
445  out:
446         mutex_unlock(&dev->mutex);
447         return err;
448 }
449
450 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
451 {
452         bool new_rfk_state;
453         bool value;
454         u32 hci_result;
455         struct toshiba_acpi_dev *dev = data;
456
457         mutex_lock(&dev->mutex);
458
459         hci_result = hci_get_radio_state(dev, &value);
460         if (hci_result != HCI_SUCCESS) {
461                 /* Can't do anything useful */
462                 mutex_unlock(&dev->mutex);
463                 return;
464         }
465
466         new_rfk_state = value;
467
468         mutex_unlock(&dev->mutex);
469
470         if (rfkill_set_hw_state(rfkill, !new_rfk_state))
471                 bt_rfkill_set_block(data, true);
472 }
473
474 static const struct rfkill_ops toshiba_rfk_ops = {
475         .set_block = bt_rfkill_set_block,
476         .poll = bt_rfkill_poll,
477 };
478
479 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
480
481 static int get_lcd(struct backlight_device *bd)
482 {
483         struct toshiba_acpi_dev *dev = bl_get_data(bd);
484         u32 hci_result;
485         u32 value;
486
487         hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
488         if (hci_result == HCI_SUCCESS)
489                 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
490
491         return -EIO;
492 }
493
494 static int lcd_proc_show(struct seq_file *m, void *v)
495 {
496         struct toshiba_acpi_dev *dev = m->private;
497         int value;
498
499         if (!dev->backlight_dev)
500                 return -ENODEV;
501
502         value = get_lcd(dev->backlight_dev);
503         if (value >= 0) {
504                 seq_printf(m, "brightness:              %d\n", value);
505                 seq_printf(m, "brightness_levels:       %d\n",
506                              HCI_LCD_BRIGHTNESS_LEVELS);
507                 return 0;
508         }
509
510         pr_err("Error reading LCD brightness\n");
511         return -EIO;
512 }
513
514 static int lcd_proc_open(struct inode *inode, struct file *file)
515 {
516         return single_open(file, lcd_proc_show, PDE(inode)->data);
517 }
518
519 static int set_lcd(struct toshiba_acpi_dev *dev, int value)
520 {
521         u32 hci_result;
522
523         value = value << HCI_LCD_BRIGHTNESS_SHIFT;
524         hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
525         return hci_result == HCI_SUCCESS ? 0 : -EIO;
526 }
527
528 static int set_lcd_status(struct backlight_device *bd)
529 {
530         struct toshiba_acpi_dev *dev = bl_get_data(bd);
531         return set_lcd(dev, bd->props.brightness);
532 }
533
534 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
535                               size_t count, loff_t *pos)
536 {
537         struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
538         char cmd[42];
539         size_t len;
540         int value;
541         int ret;
542
543         len = min(count, sizeof(cmd) - 1);
544         if (copy_from_user(cmd, buf, len))
545                 return -EFAULT;
546         cmd[len] = '\0';
547
548         if (sscanf(cmd, " brightness : %i", &value) == 1 &&
549             value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
550                 ret = set_lcd(dev, value);
551                 if (ret == 0)
552                         ret = count;
553         } else {
554                 ret = -EINVAL;
555         }
556         return ret;
557 }
558
559 static const struct file_operations lcd_proc_fops = {
560         .owner          = THIS_MODULE,
561         .open           = lcd_proc_open,
562         .read           = seq_read,
563         .llseek         = seq_lseek,
564         .release        = single_release,
565         .write          = lcd_proc_write,
566 };
567
568 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
569 {
570         u32 hci_result;
571
572         hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
573         return hci_result == HCI_SUCCESS ? 0 : -EIO;
574 }
575
576 static int video_proc_show(struct seq_file *m, void *v)
577 {
578         struct toshiba_acpi_dev *dev = m->private;
579         u32 value;
580         int ret;
581
582         ret = get_video_status(dev, &value);
583         if (!ret) {
584                 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
585                 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
586                 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
587                 seq_printf(m, "lcd_out:                 %d\n", is_lcd);
588                 seq_printf(m, "crt_out:                 %d\n", is_crt);
589                 seq_printf(m, "tv_out:                  %d\n", is_tv);
590         }
591
592         return ret;
593 }
594
595 static int video_proc_open(struct inode *inode, struct file *file)
596 {
597         return single_open(file, video_proc_show, PDE(inode)->data);
598 }
599
600 static ssize_t video_proc_write(struct file *file, const char __user *buf,
601                                 size_t count, loff_t *pos)
602 {
603         struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
604         char *cmd, *buffer;
605         int ret;
606         int value;
607         int remain = count;
608         int lcd_out = -1;
609         int crt_out = -1;
610         int tv_out = -1;
611         u32 video_out;
612
613         cmd = kmalloc(count + 1, GFP_KERNEL);
614         if (!cmd)
615                 return -ENOMEM;
616         if (copy_from_user(cmd, buf, count)) {
617                 kfree(cmd);
618                 return -EFAULT;
619         }
620         cmd[count] = '\0';
621
622         buffer = cmd;
623
624         /* scan expression.  Multiple expressions may be delimited with ;
625          *
626          *  NOTE: to keep scanning simple, invalid fields are ignored
627          */
628         while (remain) {
629                 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
630                         lcd_out = value & 1;
631                 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
632                         crt_out = value & 1;
633                 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
634                         tv_out = value & 1;
635                 /* advance to one character past the next ; */
636                 do {
637                         ++buffer;
638                         --remain;
639                 }
640                 while (remain && *(buffer - 1) != ';');
641         }
642
643         kfree(cmd);
644
645         ret = get_video_status(dev, &video_out);
646         if (!ret) {
647                 unsigned int new_video_out = video_out;
648                 if (lcd_out != -1)
649                         _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
650                 if (crt_out != -1)
651                         _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
652                 if (tv_out != -1)
653                         _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
654                 /* To avoid unnecessary video disruption, only write the new
655                  * video setting if something changed. */
656                 if (new_video_out != video_out)
657                         ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
658         }
659
660         return ret ? ret : count;
661 }
662
663 static const struct file_operations video_proc_fops = {
664         .owner          = THIS_MODULE,
665         .open           = video_proc_open,
666         .read           = seq_read,
667         .llseek         = seq_lseek,
668         .release        = single_release,
669         .write          = video_proc_write,
670 };
671
672 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
673 {
674         u32 hci_result;
675
676         hci_read1(dev, HCI_FAN, status, &hci_result);
677         return hci_result == HCI_SUCCESS ? 0 : -EIO;
678 }
679
680 static int fan_proc_show(struct seq_file *m, void *v)
681 {
682         struct toshiba_acpi_dev *dev = m->private;
683         int ret;
684         u32 value;
685
686         ret = get_fan_status(dev, &value);
687         if (!ret) {
688                 seq_printf(m, "running:                 %d\n", (value > 0));
689                 seq_printf(m, "force_on:                %d\n", dev->force_fan);
690         }
691
692         return ret;
693 }
694
695 static int fan_proc_open(struct inode *inode, struct file *file)
696 {
697         return single_open(file, fan_proc_show, PDE(inode)->data);
698 }
699
700 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
701                               size_t count, loff_t *pos)
702 {
703         struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
704         char cmd[42];
705         size_t len;
706         int value;
707         u32 hci_result;
708
709         len = min(count, sizeof(cmd) - 1);
710         if (copy_from_user(cmd, buf, len))
711                 return -EFAULT;
712         cmd[len] = '\0';
713
714         if (sscanf(cmd, " force_on : %i", &value) == 1 &&
715             value >= 0 && value <= 1) {
716                 hci_write1(dev, HCI_FAN, value, &hci_result);
717                 if (hci_result != HCI_SUCCESS)
718                         return -EIO;
719                 else
720                         dev->force_fan = value;
721         } else {
722                 return -EINVAL;
723         }
724
725         return count;
726 }
727
728 static const struct file_operations fan_proc_fops = {
729         .owner          = THIS_MODULE,
730         .open           = fan_proc_open,
731         .read           = seq_read,
732         .llseek         = seq_lseek,
733         .release        = single_release,
734         .write          = fan_proc_write,
735 };
736
737 static int keys_proc_show(struct seq_file *m, void *v)
738 {
739         struct toshiba_acpi_dev *dev = m->private;
740         u32 hci_result;
741         u32 value;
742
743         if (!dev->key_event_valid && dev->system_event_supported) {
744                 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
745                 if (hci_result == HCI_SUCCESS) {
746                         dev->key_event_valid = 1;
747                         dev->last_key_event = value;
748                 } else if (hci_result == HCI_EMPTY) {
749                         /* better luck next time */
750                 } else if (hci_result == HCI_NOT_SUPPORTED) {
751                         /* This is a workaround for an unresolved issue on
752                          * some machines where system events sporadically
753                          * become disabled. */
754                         hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
755                         pr_notice("Re-enabled hotkeys\n");
756                 } else {
757                         pr_err("Error reading hotkey status\n");
758                         return -EIO;
759                 }
760         }
761
762         seq_printf(m, "hotkey_ready:            %d\n", dev->key_event_valid);
763         seq_printf(m, "hotkey:                  0x%04x\n", dev->last_key_event);
764         return 0;
765 }
766
767 static int keys_proc_open(struct inode *inode, struct file *file)
768 {
769         return single_open(file, keys_proc_show, PDE(inode)->data);
770 }
771
772 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
773                                size_t count, loff_t *pos)
774 {
775         struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data;
776         char cmd[42];
777         size_t len;
778         int value;
779
780         len = min(count, sizeof(cmd) - 1);
781         if (copy_from_user(cmd, buf, len))
782                 return -EFAULT;
783         cmd[len] = '\0';
784
785         if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
786                 dev->key_event_valid = 0;
787         } else {
788                 return -EINVAL;
789         }
790
791         return count;
792 }
793
794 static const struct file_operations keys_proc_fops = {
795         .owner          = THIS_MODULE,
796         .open           = keys_proc_open,
797         .read           = seq_read,
798         .llseek         = seq_lseek,
799         .release        = single_release,
800         .write          = keys_proc_write,
801 };
802
803 static int version_proc_show(struct seq_file *m, void *v)
804 {
805         seq_printf(m, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
806         seq_printf(m, "proc_interface:          %d\n", PROC_INTERFACE_VERSION);
807         return 0;
808 }
809
810 static int version_proc_open(struct inode *inode, struct file *file)
811 {
812         return single_open(file, version_proc_show, PDE(inode)->data);
813 }
814
815 static const struct file_operations version_proc_fops = {
816         .owner          = THIS_MODULE,
817         .open           = version_proc_open,
818         .read           = seq_read,
819         .llseek         = seq_lseek,
820         .release        = single_release,
821 };
822
823 /* proc and module init
824  */
825
826 #define PROC_TOSHIBA            "toshiba"
827
828 static void __devinit
829 create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
830 {
831         if (dev->backlight_dev)
832                 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
833                                  &lcd_proc_fops, dev);
834         if (dev->video_supported)
835                 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
836                                  &video_proc_fops, dev);
837         if (dev->fan_supported)
838                 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
839                                  &fan_proc_fops, dev);
840         if (dev->hotkey_dev)
841                 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
842                                  &keys_proc_fops, dev);
843         proc_create_data("version", S_IRUGO, toshiba_proc_dir,
844                          &version_proc_fops, dev);
845 }
846
847 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
848 {
849         if (dev->backlight_dev)
850                 remove_proc_entry("lcd", toshiba_proc_dir);
851         if (dev->video_supported)
852                 remove_proc_entry("video", toshiba_proc_dir);
853         if (dev->fan_supported)
854                 remove_proc_entry("fan", toshiba_proc_dir);
855         if (dev->hotkey_dev)
856                 remove_proc_entry("keys", toshiba_proc_dir);
857         remove_proc_entry("version", toshiba_proc_dir);
858 }
859
860 static const struct backlight_ops toshiba_backlight_data = {
861         .get_brightness = get_lcd,
862         .update_status  = set_lcd_status,
863 };
864
865 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
866                                       struct serio *port)
867 {
868         if (str & 0x20)
869                 return false;
870
871         if (unlikely(data == 0xe0))
872                 return false;
873
874         if ((data & 0x7f) == TOS1900_FN_SCAN) {
875                 schedule_work(&toshiba_acpi->hotkey_work);
876                 return true;
877         }
878
879         return false;
880 }
881
882 static void toshiba_acpi_hotkey_work(struct work_struct *work)
883 {
884         acpi_handle ec_handle = ec_get_handle();
885         acpi_status status;
886
887         if (!ec_handle)
888                 return;
889
890         status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
891         if (ACPI_FAILURE(status))
892                 pr_err("ACPI NTFY method execution failed\n");
893 }
894
895 /*
896  * Returns hotkey scancode, or < 0 on failure.
897  */
898 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
899 {
900         struct acpi_buffer buf;
901         union acpi_object out_obj;
902         acpi_status status;
903
904         buf.pointer = &out_obj;
905         buf.length = sizeof(out_obj);
906
907         status = acpi_evaluate_object(dev->acpi_dev->handle, "INFO",
908                                       NULL, &buf);
909         if (ACPI_FAILURE(status) || out_obj.type != ACPI_TYPE_INTEGER) {
910                 pr_err("ACPI INFO method execution failed\n");
911                 return -EIO;
912         }
913
914         return out_obj.integer.value;
915 }
916
917 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
918                                        int scancode)
919 {
920         if (scancode == 0x100)
921                 return;
922
923         /* act on key press; ignore key release */
924         if (scancode & 0x80)
925                 return;
926
927         if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
928                 pr_info("Unknown key %x\n", scancode);
929 }
930
931 static int __devinit toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
932 {
933         acpi_status status;
934         acpi_handle ec_handle, handle;
935         int error;
936         u32 hci_result;
937
938         dev->hotkey_dev = input_allocate_device();
939         if (!dev->hotkey_dev) {
940                 pr_info("Unable to register input device\n");
941                 return -ENOMEM;
942         }
943
944         dev->hotkey_dev->name = "Toshiba input device";
945         dev->hotkey_dev->phys = "toshiba_acpi/input0";
946         dev->hotkey_dev->id.bustype = BUS_HOST;
947
948         error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL);
949         if (error)
950                 goto err_free_dev;
951
952         /*
953          * For some machines the SCI responsible for providing hotkey
954          * notification doesn't fire. We can trigger the notification
955          * whenever the Fn key is pressed using the NTFY method, if
956          * supported, so if it's present set up an i8042 key filter
957          * for this purpose.
958          */
959         status = AE_ERROR;
960         ec_handle = ec_get_handle();
961         if (ec_handle)
962                 status = acpi_get_handle(ec_handle, "NTFY", &handle);
963
964         if (ACPI_SUCCESS(status)) {
965                 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
966
967                 error = i8042_install_filter(toshiba_acpi_i8042_filter);
968                 if (error) {
969                         pr_err("Error installing key filter\n");
970                         goto err_free_keymap;
971                 }
972
973                 dev->ntfy_supported = 1;
974         }
975
976         /*
977          * Determine hotkey query interface. Prefer using the INFO
978          * method when it is available.
979          */
980         status = acpi_get_handle(dev->acpi_dev->handle, "INFO", &handle);
981         if (ACPI_SUCCESS(status)) {
982                 dev->info_supported = 1;
983         } else {
984                 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
985                 if (hci_result == HCI_SUCCESS)
986                         dev->system_event_supported = 1;
987         }
988
989         if (!dev->info_supported && !dev->system_event_supported) {
990                 pr_warn("No hotkey query interface found\n");
991                 goto err_remove_filter;
992         }
993
994         status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
995         if (ACPI_FAILURE(status)) {
996                 pr_info("Unable to enable hotkeys\n");
997                 error = -ENODEV;
998                 goto err_remove_filter;
999         }
1000
1001         error = input_register_device(dev->hotkey_dev);
1002         if (error) {
1003                 pr_info("Unable to register input device\n");
1004                 goto err_remove_filter;
1005         }
1006
1007         hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result);
1008         return 0;
1009
1010  err_remove_filter:
1011         if (dev->ntfy_supported)
1012                 i8042_remove_filter(toshiba_acpi_i8042_filter);
1013  err_free_keymap:
1014         sparse_keymap_free(dev->hotkey_dev);
1015  err_free_dev:
1016         input_free_device(dev->hotkey_dev);
1017         dev->hotkey_dev = NULL;
1018         return error;
1019 }
1020
1021 static int toshiba_acpi_remove(struct acpi_device *acpi_dev, int type)
1022 {
1023         struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1024
1025         remove_toshiba_proc_entries(dev);
1026
1027         if (dev->ntfy_supported) {
1028                 i8042_remove_filter(toshiba_acpi_i8042_filter);
1029                 cancel_work_sync(&dev->hotkey_work);
1030         }
1031
1032         if (dev->hotkey_dev) {
1033                 input_unregister_device(dev->hotkey_dev);
1034                 sparse_keymap_free(dev->hotkey_dev);
1035         }
1036
1037         if (dev->bt_rfk) {
1038                 rfkill_unregister(dev->bt_rfk);
1039                 rfkill_destroy(dev->bt_rfk);
1040         }
1041
1042         if (dev->backlight_dev)
1043                 backlight_device_unregister(dev->backlight_dev);
1044
1045         if (dev->illumination_supported)
1046                 led_classdev_unregister(&dev->led_dev);
1047
1048         if (toshiba_acpi)
1049                 toshiba_acpi = NULL;
1050
1051         kfree(dev);
1052
1053         return 0;
1054 }
1055
1056 static const char * __devinit find_hci_method(acpi_handle handle)
1057 {
1058         acpi_status status;
1059         acpi_handle hci_handle;
1060
1061         status = acpi_get_handle(handle, "GHCI", &hci_handle);
1062         if (ACPI_SUCCESS(status))
1063                 return "GHCI";
1064
1065         status = acpi_get_handle(handle, "SPFC", &hci_handle);
1066         if (ACPI_SUCCESS(status))
1067                 return "SPFC";
1068
1069         return NULL;
1070 }
1071
1072 static int __devinit toshiba_acpi_add(struct acpi_device *acpi_dev)
1073 {
1074         struct toshiba_acpi_dev *dev;
1075         const char *hci_method;
1076         u32 dummy;
1077         bool bt_present;
1078         int ret = 0;
1079         struct backlight_properties props;
1080
1081         if (toshiba_acpi)
1082                 return -EBUSY;
1083
1084         pr_info("Toshiba Laptop ACPI Extras version %s\n",
1085                TOSHIBA_ACPI_VERSION);
1086
1087         hci_method = find_hci_method(acpi_dev->handle);
1088         if (!hci_method) {
1089                 pr_err("HCI interface not found\n");
1090                 return -ENODEV;
1091         }
1092
1093         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1094         if (!dev)
1095                 return -ENOMEM;
1096         dev->acpi_dev = acpi_dev;
1097         dev->method_hci = hci_method;
1098         acpi_dev->driver_data = dev;
1099
1100         if (toshiba_acpi_setup_keyboard(dev))
1101                 pr_info("Unable to activate hotkeys\n");
1102
1103         mutex_init(&dev->mutex);
1104
1105         props.type = BACKLIGHT_PLATFORM;
1106         props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
1107         dev->backlight_dev = backlight_device_register("toshiba",
1108                                                        &acpi_dev->dev,
1109                                                        dev,
1110                                                        &toshiba_backlight_data,
1111                                                        &props);
1112         if (IS_ERR(dev->backlight_dev)) {
1113                 ret = PTR_ERR(dev->backlight_dev);
1114
1115                 pr_err("Could not register toshiba backlight device\n");
1116                 dev->backlight_dev = NULL;
1117                 goto error;
1118         }
1119         dev->backlight_dev->props.brightness = get_lcd(dev->backlight_dev);
1120
1121         /* Register rfkill switch for Bluetooth */
1122         if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
1123                 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
1124                                            &acpi_dev->dev,
1125                                            RFKILL_TYPE_BLUETOOTH,
1126                                            &toshiba_rfk_ops,
1127                                            dev);
1128                 if (!dev->bt_rfk) {
1129                         pr_err("unable to allocate rfkill device\n");
1130                         ret = -ENOMEM;
1131                         goto error;
1132                 }
1133
1134                 ret = rfkill_register(dev->bt_rfk);
1135                 if (ret) {
1136                         pr_err("unable to register rfkill device\n");
1137                         rfkill_destroy(dev->bt_rfk);
1138                         goto error;
1139                 }
1140         }
1141
1142         if (toshiba_illumination_available(dev)) {
1143                 dev->led_dev.name = "toshiba::illumination";
1144                 dev->led_dev.max_brightness = 1;
1145                 dev->led_dev.brightness_set = toshiba_illumination_set;
1146                 dev->led_dev.brightness_get = toshiba_illumination_get;
1147                 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
1148                         dev->illumination_supported = 1;
1149         }
1150
1151         /* Determine whether or not BIOS supports fan and video interfaces */
1152
1153         ret = get_video_status(dev, &dummy);
1154         dev->video_supported = !ret;
1155
1156         ret = get_fan_status(dev, &dummy);
1157         dev->fan_supported = !ret;
1158
1159         create_toshiba_proc_entries(dev);
1160
1161         toshiba_acpi = dev;
1162
1163         return 0;
1164
1165 error:
1166         toshiba_acpi_remove(acpi_dev, 0);
1167         return ret;
1168 }
1169
1170 static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1171 {
1172         struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1173         u32 hci_result, value;
1174         int retries = 3;
1175         int scancode;
1176
1177         if (event != 0x80)
1178                 return;
1179
1180         if (dev->info_supported) {
1181                 scancode = toshiba_acpi_query_hotkey(dev);
1182                 if (scancode < 0)
1183                         pr_err("Failed to query hotkey event\n");
1184                 else if (scancode != 0)
1185                         toshiba_acpi_report_hotkey(dev, scancode);
1186         } else if (dev->system_event_supported) {
1187                 do {
1188                         hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1189                         switch (hci_result) {
1190                         case HCI_SUCCESS:
1191                                 toshiba_acpi_report_hotkey(dev, (int)value);
1192                                 break;
1193                         case HCI_NOT_SUPPORTED:
1194                                 /*
1195                                  * This is a workaround for an unresolved
1196                                  * issue on some machines where system events
1197                                  * sporadically become disabled.
1198                                  */
1199                                 hci_write1(dev, HCI_SYSTEM_EVENT, 1,
1200                                            &hci_result);
1201                                 pr_notice("Re-enabled hotkeys\n");
1202                                 /* fall through */
1203                         default:
1204                                 retries--;
1205                                 break;
1206                         }
1207                 } while (retries && hci_result != HCI_EMPTY);
1208         }
1209 }
1210
1211 static int toshiba_acpi_suspend(struct acpi_device *acpi_dev,
1212                                 pm_message_t state)
1213 {
1214         struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1215         u32 result;
1216
1217         if (dev->hotkey_dev)
1218                 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result);
1219
1220         return 0;
1221 }
1222
1223 static int toshiba_acpi_resume(struct acpi_device *acpi_dev)
1224 {
1225         struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1226         u32 result;
1227
1228         if (dev->hotkey_dev)
1229                 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result);
1230
1231         return 0;
1232 }
1233
1234 static struct acpi_driver toshiba_acpi_driver = {
1235         .name   = "Toshiba ACPI driver",
1236         .owner  = THIS_MODULE,
1237         .ids    = toshiba_device_ids,
1238         .flags  = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1239         .ops    = {
1240                 .add            = toshiba_acpi_add,
1241                 .remove         = toshiba_acpi_remove,
1242                 .notify         = toshiba_acpi_notify,
1243                 .suspend        = toshiba_acpi_suspend,
1244                 .resume         = toshiba_acpi_resume,
1245         },
1246 };
1247
1248 static int __init toshiba_acpi_init(void)
1249 {
1250         int ret;
1251
1252         toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1253         if (!toshiba_proc_dir) {
1254                 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
1255                 return -ENODEV;
1256         }
1257
1258         ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1259         if (ret) {
1260                 pr_err("Failed to register ACPI driver: %d\n", ret);
1261                 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1262         }
1263
1264         return ret;
1265 }
1266
1267 static void __exit toshiba_acpi_exit(void)
1268 {
1269         acpi_bus_unregister_driver(&toshiba_acpi_driver);
1270         if (toshiba_proc_dir)
1271                 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1272 }
1273
1274 module_init(toshiba_acpi_init);
1275 module_exit(toshiba_acpi_exit);