06f304f46e0229427e039180abb6731bceb403bd
[pandora-kernel.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 TOSHIBA_ACPI_VERSION    "0.19"
39 #define PROC_INTERFACE_VERSION  1
40
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/input/sparse-keymap.h>
52 #include <linux/leds.h>
53 #include <linux/slab.h>
54
55 #include <asm/uaccess.h>
56
57 #include <acpi/acpi_drivers.h>
58
59 MODULE_AUTHOR("John Belmonte");
60 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
61 MODULE_LICENSE("GPL");
62
63 #define MY_LOGPREFIX "toshiba_acpi: "
64 #define MY_ERR KERN_ERR MY_LOGPREFIX
65 #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
66 #define MY_INFO KERN_INFO MY_LOGPREFIX
67
68 /* Toshiba ACPI method paths */
69 #define METHOD_LCD_BRIGHTNESS   "\\_SB_.PCI0.VGA_.LCD_._BCM"
70 #define TOSH_INTERFACE_1        "\\_SB_.VALD"
71 #define TOSH_INTERFACE_2        "\\_SB_.VALZ"
72 #define METHOD_VIDEO_OUT        "\\_SB_.VALX.DSSX"
73 #define GHCI_METHOD             ".GHCI"
74
75 /* Toshiba HCI interface definitions
76  *
77  * HCI is Toshiba's "Hardware Control Interface" which is supposed to
78  * be uniform across all their models.  Ideally we would just call
79  * dedicated ACPI methods instead of using this primitive interface.
80  * However the ACPI methods seem to be incomplete in some areas (for
81  * example they allow setting, but not reading, the LCD brightness value),
82  * so this is still useful.
83  */
84
85 #define HCI_WORDS                       6
86
87 /* operations */
88 #define HCI_SET                         0xff00
89 #define HCI_GET                         0xfe00
90
91 /* return codes */
92 #define HCI_SUCCESS                     0x0000
93 #define HCI_FAILURE                     0x1000
94 #define HCI_NOT_SUPPORTED               0x8000
95 #define HCI_EMPTY                       0x8c00
96
97 /* registers */
98 #define HCI_FAN                         0x0004
99 #define HCI_SYSTEM_EVENT                0x0016
100 #define HCI_VIDEO_OUT                   0x001c
101 #define HCI_HOTKEY_EVENT                0x001e
102 #define HCI_LCD_BRIGHTNESS              0x002a
103 #define HCI_WIRELESS                    0x0056
104
105 /* field definitions */
106 #define HCI_LCD_BRIGHTNESS_BITS         3
107 #define HCI_LCD_BRIGHTNESS_SHIFT        (16-HCI_LCD_BRIGHTNESS_BITS)
108 #define HCI_LCD_BRIGHTNESS_LEVELS       (1 << HCI_LCD_BRIGHTNESS_BITS)
109 #define HCI_VIDEO_OUT_LCD               0x1
110 #define HCI_VIDEO_OUT_CRT               0x2
111 #define HCI_VIDEO_OUT_TV                0x4
112 #define HCI_WIRELESS_KILL_SWITCH        0x01
113 #define HCI_WIRELESS_BT_PRESENT         0x0f
114 #define HCI_WIRELESS_BT_ATTACH          0x40
115 #define HCI_WIRELESS_BT_POWER           0x80
116
117 static const struct acpi_device_id toshiba_device_ids[] = {
118         {"TOS6200", 0},
119         {"TOS6208", 0},
120         {"TOS1900", 0},
121         {"", 0},
122 };
123 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
124
125 static const struct key_entry toshiba_acpi_keymap[] __initconst = {
126         { KE_KEY, 0x101, { KEY_MUTE } },
127         { KE_KEY, 0x102, { KEY_ZOOMOUT } },
128         { KE_KEY, 0x103, { KEY_ZOOMIN } },
129         { KE_KEY, 0x13b, { KEY_COFFEE } },
130         { KE_KEY, 0x13c, { KEY_BATTERY } },
131         { KE_KEY, 0x13d, { KEY_SLEEP } },
132         { KE_KEY, 0x13e, { KEY_SUSPEND } },
133         { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
134         { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
135         { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
136         { KE_KEY, 0x142, { KEY_WLAN } },
137         { KE_KEY, 0x143, { KEY_PROG1 } },
138         { KE_KEY, 0xb05, { KEY_PROG2 } },
139         { KE_KEY, 0xb06, { KEY_WWW } },
140         { KE_KEY, 0xb07, { KEY_MAIL } },
141         { KE_KEY, 0xb30, { KEY_STOP } },
142         { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
143         { KE_KEY, 0xb32, { KEY_NEXTSONG } },
144         { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
145         { KE_KEY, 0xb5a, { KEY_MEDIA } },
146         { KE_END, 0 },
147 };
148
149 /* utility
150  */
151
152 static __inline__ void _set_bit(u32 * word, u32 mask, int value)
153 {
154         *word = (*word & ~mask) | (mask * value);
155 }
156
157 /* acpi interface wrappers
158  */
159
160 static int is_valid_acpi_path(const char *methodName)
161 {
162         acpi_handle handle;
163         acpi_status status;
164
165         status = acpi_get_handle(NULL, (char *)methodName, &handle);
166         return !ACPI_FAILURE(status);
167 }
168
169 static int write_acpi_int(const char *methodName, int val)
170 {
171         struct acpi_object_list params;
172         union acpi_object in_objs[1];
173         acpi_status status;
174
175         params.count = ARRAY_SIZE(in_objs);
176         params.pointer = in_objs;
177         in_objs[0].type = ACPI_TYPE_INTEGER;
178         in_objs[0].integer.value = val;
179
180         status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
181         return (status == AE_OK);
182 }
183
184 #if 0
185 static int read_acpi_int(const char *methodName, int *pVal)
186 {
187         struct acpi_buffer results;
188         union acpi_object out_objs[1];
189         acpi_status status;
190
191         results.length = sizeof(out_objs);
192         results.pointer = out_objs;
193
194         status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
195         *pVal = out_objs[0].integer.value;
196
197         return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
198 }
199 #endif
200
201 static const char *method_hci /*= 0*/ ;
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(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
207 {
208         struct acpi_object_list params;
209         union acpi_object in_objs[HCI_WORDS];
210         struct acpi_buffer results;
211         union acpi_object out_objs[HCI_WORDS + 1];
212         acpi_status status;
213         int i;
214
215         params.count = HCI_WORDS;
216         params.pointer = in_objs;
217         for (i = 0; i < HCI_WORDS; ++i) {
218                 in_objs[i].type = ACPI_TYPE_INTEGER;
219                 in_objs[i].integer.value = in[i];
220         }
221
222         results.length = sizeof(out_objs);
223         results.pointer = out_objs;
224
225         status = acpi_evaluate_object(NULL, (char *)method_hci, &params,
226                                       &results);
227         if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
228                 for (i = 0; i < out_objs->package.count; ++i) {
229                         out[i] = out_objs->package.elements[i].integer.value;
230                 }
231         }
232
233         return status;
234 }
235
236 /* common hci tasks (get or set one or two value)
237  *
238  * In addition to the ACPI status, the HCI system returns a result which
239  * may be useful (such as "not supported").
240  */
241
242 static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
243 {
244         u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
245         u32 out[HCI_WORDS];
246         acpi_status status = hci_raw(in, out);
247         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
248         return status;
249 }
250
251 static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
252 {
253         u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
254         u32 out[HCI_WORDS];
255         acpi_status status = hci_raw(in, out);
256         *out1 = out[2];
257         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
258         return status;
259 }
260
261 static acpi_status hci_write2(u32 reg, u32 in1, u32 in2, u32 *result)
262 {
263         u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
264         u32 out[HCI_WORDS];
265         acpi_status status = hci_raw(in, out);
266         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
267         return status;
268 }
269
270 static acpi_status hci_read2(u32 reg, u32 *out1, u32 *out2, u32 *result)
271 {
272         u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
273         u32 out[HCI_WORDS];
274         acpi_status status = hci_raw(in, out);
275         *out1 = out[2];
276         *out2 = out[3];
277         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
278         return status;
279 }
280
281 struct toshiba_acpi_dev {
282         struct platform_device *p_dev;
283         struct rfkill *bt_rfk;
284         struct input_dev *hotkey_dev;
285         int illumination_installed;
286         acpi_handle handle;
287
288         const char *bt_name;
289
290         struct mutex mutex;
291 };
292
293 /* Illumination support */
294 static int toshiba_illumination_available(void)
295 {
296         u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
297         u32 out[HCI_WORDS];
298         acpi_status status;
299
300         in[0] = 0xf100;
301         status = hci_raw(in, out);
302         if (ACPI_FAILURE(status)) {
303                 printk(MY_INFO "Illumination device not available\n");
304                 return 0;
305         }
306         in[0] = 0xf400;
307         status = hci_raw(in, out);
308         return 1;
309 }
310
311 static void toshiba_illumination_set(struct led_classdev *cdev,
312                                      enum led_brightness brightness)
313 {
314         u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
315         u32 out[HCI_WORDS];
316         acpi_status status;
317
318         /* First request : initialize communication. */
319         in[0] = 0xf100;
320         status = hci_raw(in, out);
321         if (ACPI_FAILURE(status)) {
322                 printk(MY_INFO "Illumination device not available\n");
323                 return;
324         }
325
326         if (brightness) {
327                 /* Switch the illumination on */
328                 in[0] = 0xf400;
329                 in[1] = 0x14e;
330                 in[2] = 1;
331                 status = hci_raw(in, out);
332                 if (ACPI_FAILURE(status)) {
333                         printk(MY_INFO "ACPI call for illumination failed.\n");
334                         return;
335                 }
336         } else {
337                 /* Switch the illumination off */
338                 in[0] = 0xf400;
339                 in[1] = 0x14e;
340                 in[2] = 0;
341                 status = hci_raw(in, out);
342                 if (ACPI_FAILURE(status)) {
343                         printk(MY_INFO "ACPI call for illumination failed.\n");
344                         return;
345                 }
346         }
347
348         /* Last request : close communication. */
349         in[0] = 0xf200;
350         in[1] = 0;
351         in[2] = 0;
352         hci_raw(in, out);
353 }
354
355 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
356 {
357         u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 };
358         u32 out[HCI_WORDS];
359         acpi_status status;
360         enum led_brightness result;
361
362         /* First request : initialize communication. */
363         in[0] = 0xf100;
364         status = hci_raw(in, out);
365         if (ACPI_FAILURE(status)) {
366                 printk(MY_INFO "Illumination device not available\n");
367                 return LED_OFF;
368         }
369
370         /* Check the illumination */
371         in[0] = 0xf300;
372         in[1] = 0x14e;
373         status = hci_raw(in, out);
374         if (ACPI_FAILURE(status)) {
375                 printk(MY_INFO "ACPI call for illumination failed.\n");
376                 return LED_OFF;
377         }
378
379         result = out[2] ? LED_FULL : LED_OFF;
380
381         /* Last request : close communication. */
382         in[0] = 0xf200;
383         in[1] = 0;
384         in[2] = 0;
385         hci_raw(in, out);
386
387         return result;
388 }
389
390 static struct led_classdev toshiba_led = {
391         .name           = "toshiba::illumination",
392         .max_brightness = 1,
393         .brightness_set = toshiba_illumination_set,
394         .brightness_get = toshiba_illumination_get,
395 };
396
397 static struct toshiba_acpi_dev toshiba_acpi = {
398         .bt_name = "Toshiba Bluetooth",
399 };
400
401 /* Bluetooth rfkill handlers */
402
403 static u32 hci_get_bt_present(bool *present)
404 {
405         u32 hci_result;
406         u32 value, value2;
407
408         value = 0;
409         value2 = 0;
410         hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
411         if (hci_result == HCI_SUCCESS)
412                 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
413
414         return hci_result;
415 }
416
417 static u32 hci_get_radio_state(bool *radio_state)
418 {
419         u32 hci_result;
420         u32 value, value2;
421
422         value = 0;
423         value2 = 0x0001;
424         hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
425
426         *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
427         return hci_result;
428 }
429
430 static int bt_rfkill_set_block(void *data, bool blocked)
431 {
432         struct toshiba_acpi_dev *dev = data;
433         u32 result1, result2;
434         u32 value;
435         int err;
436         bool radio_state;
437
438         value = (blocked == false);
439
440         mutex_lock(&dev->mutex);
441         if (hci_get_radio_state(&radio_state) != HCI_SUCCESS) {
442                 err = -EBUSY;
443                 goto out;
444         }
445
446         if (!radio_state) {
447                 err = 0;
448                 goto out;
449         }
450
451         hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
452         hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
453
454         if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
455                 err = -EBUSY;
456         else
457                 err = 0;
458  out:
459         mutex_unlock(&dev->mutex);
460         return err;
461 }
462
463 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
464 {
465         bool new_rfk_state;
466         bool value;
467         u32 hci_result;
468         struct toshiba_acpi_dev *dev = data;
469
470         mutex_lock(&dev->mutex);
471
472         hci_result = hci_get_radio_state(&value);
473         if (hci_result != HCI_SUCCESS) {
474                 /* Can't do anything useful */
475                 mutex_unlock(&dev->mutex);
476                 return;
477         }
478
479         new_rfk_state = value;
480
481         mutex_unlock(&dev->mutex);
482
483         if (rfkill_set_hw_state(rfkill, !new_rfk_state))
484                 bt_rfkill_set_block(data, true);
485 }
486
487 static const struct rfkill_ops toshiba_rfk_ops = {
488         .set_block = bt_rfkill_set_block,
489         .poll = bt_rfkill_poll,
490 };
491
492 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
493 static struct backlight_device *toshiba_backlight_device;
494 static int force_fan;
495 static int last_key_event;
496 static int key_event_valid;
497
498 static int get_lcd(struct backlight_device *bd)
499 {
500         u32 hci_result;
501         u32 value;
502
503         hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
504         if (hci_result == HCI_SUCCESS) {
505                 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
506         } else
507                 return -EFAULT;
508 }
509
510 static int lcd_proc_show(struct seq_file *m, void *v)
511 {
512         int value = get_lcd(NULL);
513
514         if (value >= 0) {
515                 seq_printf(m, "brightness:              %d\n", value);
516                 seq_printf(m, "brightness_levels:       %d\n",
517                              HCI_LCD_BRIGHTNESS_LEVELS);
518         } else {
519                 printk(MY_ERR "Error reading LCD brightness\n");
520         }
521
522         return 0;
523 }
524
525 static int lcd_proc_open(struct inode *inode, struct file *file)
526 {
527         return single_open(file, lcd_proc_show, NULL);
528 }
529
530 static int set_lcd(int value)
531 {
532         u32 hci_result;
533
534         value = value << HCI_LCD_BRIGHTNESS_SHIFT;
535         hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
536         if (hci_result != HCI_SUCCESS)
537                 return -EFAULT;
538
539         return 0;
540 }
541
542 static int set_lcd_status(struct backlight_device *bd)
543 {
544         return set_lcd(bd->props.brightness);
545 }
546
547 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
548                               size_t count, loff_t *pos)
549 {
550         char cmd[42];
551         size_t len;
552         int value;
553         int ret;
554
555         len = min(count, sizeof(cmd) - 1);
556         if (copy_from_user(cmd, buf, len))
557                 return -EFAULT;
558         cmd[len] = '\0';
559
560         if (sscanf(cmd, " brightness : %i", &value) == 1 &&
561             value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
562                 ret = set_lcd(value);
563                 if (ret == 0)
564                         ret = count;
565         } else {
566                 ret = -EINVAL;
567         }
568         return ret;
569 }
570
571 static const struct file_operations lcd_proc_fops = {
572         .owner          = THIS_MODULE,
573         .open           = lcd_proc_open,
574         .read           = seq_read,
575         .llseek         = seq_lseek,
576         .release        = single_release,
577         .write          = lcd_proc_write,
578 };
579
580 static int video_proc_show(struct seq_file *m, void *v)
581 {
582         u32 hci_result;
583         u32 value;
584
585         hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
586         if (hci_result == HCI_SUCCESS) {
587                 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
588                 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
589                 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
590                 seq_printf(m, "lcd_out:                 %d\n", is_lcd);
591                 seq_printf(m, "crt_out:                 %d\n", is_crt);
592                 seq_printf(m, "tv_out:                  %d\n", is_tv);
593         } else {
594                 printk(MY_ERR "Error reading video out status\n");
595         }
596
597         return 0;
598 }
599
600 static int video_proc_open(struct inode *inode, struct file *file)
601 {
602         return single_open(file, video_proc_show, NULL);
603 }
604
605 static ssize_t video_proc_write(struct file *file, const char __user *buf,
606                                 size_t count, loff_t *pos)
607 {
608         char *cmd, *buffer;
609         int value;
610         int remain = count;
611         int lcd_out = -1;
612         int crt_out = -1;
613         int tv_out = -1;
614         u32 hci_result;
615         u32 video_out;
616
617         cmd = kmalloc(count + 1, GFP_KERNEL);
618         if (!cmd)
619                 return -ENOMEM;
620         if (copy_from_user(cmd, buf, count)) {
621                 kfree(cmd);
622                 return -EFAULT;
623         }
624         cmd[count] = '\0';
625
626         buffer = cmd;
627
628         /* scan expression.  Multiple expressions may be delimited with ;
629          *
630          *  NOTE: to keep scanning simple, invalid fields are ignored
631          */
632         while (remain) {
633                 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
634                         lcd_out = value & 1;
635                 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
636                         crt_out = value & 1;
637                 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
638                         tv_out = value & 1;
639                 /* advance to one character past the next ; */
640                 do {
641                         ++buffer;
642                         --remain;
643                 }
644                 while (remain && *(buffer - 1) != ';');
645         }
646
647         kfree(cmd);
648
649         hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
650         if (hci_result == HCI_SUCCESS) {
651                 unsigned int new_video_out = video_out;
652                 if (lcd_out != -1)
653                         _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
654                 if (crt_out != -1)
655                         _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
656                 if (tv_out != -1)
657                         _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
658                 /* To avoid unnecessary video disruption, only write the new
659                  * video setting if something changed. */
660                 if (new_video_out != video_out)
661                         write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
662         } else {
663                 return -EFAULT;
664         }
665
666         return count;
667 }
668
669 static const struct file_operations video_proc_fops = {
670         .owner          = THIS_MODULE,
671         .open           = video_proc_open,
672         .read           = seq_read,
673         .llseek         = seq_lseek,
674         .release        = single_release,
675         .write          = video_proc_write,
676 };
677
678 static int fan_proc_show(struct seq_file *m, void *v)
679 {
680         u32 hci_result;
681         u32 value;
682
683         hci_read1(HCI_FAN, &value, &hci_result);
684         if (hci_result == HCI_SUCCESS) {
685                 seq_printf(m, "running:                 %d\n", (value > 0));
686                 seq_printf(m, "force_on:                %d\n", force_fan);
687         } else {
688                 printk(MY_ERR "Error reading fan status\n");
689         }
690
691         return 0;
692 }
693
694 static int fan_proc_open(struct inode *inode, struct file *file)
695 {
696         return single_open(file, fan_proc_show, NULL);
697 }
698
699 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
700                               size_t count, loff_t *pos)
701 {
702         char cmd[42];
703         size_t len;
704         int value;
705         u32 hci_result;
706
707         len = min(count, sizeof(cmd) - 1);
708         if (copy_from_user(cmd, buf, len))
709                 return -EFAULT;
710         cmd[len] = '\0';
711
712         if (sscanf(cmd, " force_on : %i", &value) == 1 &&
713             value >= 0 && value <= 1) {
714                 hci_write1(HCI_FAN, value, &hci_result);
715                 if (hci_result != HCI_SUCCESS)
716                         return -EFAULT;
717                 else
718                         force_fan = value;
719         } else {
720                 return -EINVAL;
721         }
722
723         return count;
724 }
725
726 static const struct file_operations fan_proc_fops = {
727         .owner          = THIS_MODULE,
728         .open           = fan_proc_open,
729         .read           = seq_read,
730         .llseek         = seq_lseek,
731         .release        = single_release,
732         .write          = fan_proc_write,
733 };
734
735 static int keys_proc_show(struct seq_file *m, void *v)
736 {
737         u32 hci_result;
738         u32 value;
739
740         if (!key_event_valid) {
741                 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
742                 if (hci_result == HCI_SUCCESS) {
743                         key_event_valid = 1;
744                         last_key_event = value;
745                 } else if (hci_result == HCI_EMPTY) {
746                         /* better luck next time */
747                 } else if (hci_result == HCI_NOT_SUPPORTED) {
748                         /* This is a workaround for an unresolved issue on
749                          * some machines where system events sporadically
750                          * become disabled. */
751                         hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
752                         printk(MY_NOTICE "Re-enabled hotkeys\n");
753                 } else {
754                         printk(MY_ERR "Error reading hotkey status\n");
755                         goto end;
756                 }
757         }
758
759         seq_printf(m, "hotkey_ready:            %d\n", key_event_valid);
760         seq_printf(m, "hotkey:                  0x%04x\n", last_key_event);
761 end:
762         return 0;
763 }
764
765 static int keys_proc_open(struct inode *inode, struct file *file)
766 {
767         return single_open(file, keys_proc_show, NULL);
768 }
769
770 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
771                                size_t count, loff_t *pos)
772 {
773         char cmd[42];
774         size_t len;
775         int value;
776
777         len = min(count, sizeof(cmd) - 1);
778         if (copy_from_user(cmd, buf, len))
779                 return -EFAULT;
780         cmd[len] = '\0';
781
782         if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
783                 key_event_valid = 0;
784         } else {
785                 return -EINVAL;
786         }
787
788         return count;
789 }
790
791 static const struct file_operations keys_proc_fops = {
792         .owner          = THIS_MODULE,
793         .open           = keys_proc_open,
794         .read           = seq_read,
795         .llseek         = seq_lseek,
796         .release        = single_release,
797         .write          = keys_proc_write,
798 };
799
800 static int version_proc_show(struct seq_file *m, void *v)
801 {
802         seq_printf(m, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
803         seq_printf(m, "proc_interface:          %d\n", PROC_INTERFACE_VERSION);
804         return 0;
805 }
806
807 static int version_proc_open(struct inode *inode, struct file *file)
808 {
809         return single_open(file, version_proc_show, PDE(inode)->data);
810 }
811
812 static const struct file_operations version_proc_fops = {
813         .owner          = THIS_MODULE,
814         .open           = version_proc_open,
815         .read           = seq_read,
816         .llseek         = seq_lseek,
817         .release        = single_release,
818 };
819
820 /* proc and module init
821  */
822
823 #define PROC_TOSHIBA            "toshiba"
824
825 static void __init create_toshiba_proc_entries(void)
826 {
827         proc_create("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir, &lcd_proc_fops);
828         proc_create("video", S_IRUGO | S_IWUSR, toshiba_proc_dir, &video_proc_fops);
829         proc_create("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir, &fan_proc_fops);
830         proc_create("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir, &keys_proc_fops);
831         proc_create("version", S_IRUGO, toshiba_proc_dir, &version_proc_fops);
832 }
833
834 static void remove_toshiba_proc_entries(void)
835 {
836         remove_proc_entry("lcd", toshiba_proc_dir);
837         remove_proc_entry("video", toshiba_proc_dir);
838         remove_proc_entry("fan", toshiba_proc_dir);
839         remove_proc_entry("keys", toshiba_proc_dir);
840         remove_proc_entry("version", toshiba_proc_dir);
841 }
842
843 static struct backlight_ops toshiba_backlight_data = {
844         .get_brightness = get_lcd,
845         .update_status  = set_lcd_status,
846 };
847
848 static void toshiba_acpi_notify(acpi_handle handle, u32 event, void *context)
849 {
850         u32 hci_result, value;
851
852         if (event != 0x80)
853                 return;
854         do {
855                 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
856                 if (hci_result == HCI_SUCCESS) {
857                         if (value == 0x100)
858                                 continue;
859                         /* act on key press; ignore key release */
860                         if (value & 0x80)
861                                 continue;
862
863                         if (!sparse_keymap_report_event(toshiba_acpi.hotkey_dev,
864                                                         value, 1, true)) {
865                                 printk(MY_INFO "Unknown key %x\n",
866                                        value);
867                         }
868                 } else if (hci_result == HCI_NOT_SUPPORTED) {
869                         /* This is a workaround for an unresolved issue on
870                          * some machines where system events sporadically
871                          * become disabled. */
872                         hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
873                         printk(MY_NOTICE "Re-enabled hotkeys\n");
874                 }
875         } while (hci_result != HCI_EMPTY);
876 }
877
878 static int __init toshiba_acpi_setup_keyboard(char *device)
879 {
880         acpi_status status;
881         int error;
882
883         status = acpi_get_handle(NULL, device, &toshiba_acpi.handle);
884         if (ACPI_FAILURE(status)) {
885                 printk(MY_INFO "Unable to get notification device\n");
886                 return -ENODEV;
887         }
888
889         toshiba_acpi.hotkey_dev = input_allocate_device();
890         if (!toshiba_acpi.hotkey_dev) {
891                 printk(MY_INFO "Unable to register input device\n");
892                 return -ENOMEM;
893         }
894
895         toshiba_acpi.hotkey_dev->name = "Toshiba input device";
896         toshiba_acpi.hotkey_dev->phys = device;
897         toshiba_acpi.hotkey_dev->id.bustype = BUS_HOST;
898
899         error = sparse_keymap_setup(toshiba_acpi.hotkey_dev,
900                                     toshiba_acpi_keymap, NULL);
901         if (error)
902                 goto err_free_dev;
903
904         status = acpi_install_notify_handler(toshiba_acpi.handle,
905                                 ACPI_DEVICE_NOTIFY, toshiba_acpi_notify, NULL);
906         if (ACPI_FAILURE(status)) {
907                 printk(MY_INFO "Unable to install hotkey notification\n");
908                 error = -ENODEV;
909                 goto err_free_keymap;
910         }
911
912         status = acpi_evaluate_object(toshiba_acpi.handle, "ENAB", NULL, NULL);
913         if (ACPI_FAILURE(status)) {
914                 printk(MY_INFO "Unable to enable hotkeys\n");
915                 error = -ENODEV;
916                 goto err_remove_notify;
917         }
918
919         error = input_register_device(toshiba_acpi.hotkey_dev);
920         if (error) {
921                 printk(MY_INFO "Unable to register input device\n");
922                 goto err_remove_notify;
923         }
924
925         return 0;
926
927  err_remove_notify:
928         acpi_remove_notify_handler(toshiba_acpi.handle,
929                                    ACPI_DEVICE_NOTIFY, toshiba_acpi_notify);
930  err_free_keymap:
931         sparse_keymap_free(toshiba_acpi.hotkey_dev);
932  err_free_dev:
933         input_free_device(toshiba_acpi.hotkey_dev);
934         toshiba_acpi.hotkey_dev = NULL;
935         return error;
936 }
937
938 static void toshiba_acpi_exit(void)
939 {
940         if (toshiba_acpi.hotkey_dev) {
941                 acpi_remove_notify_handler(toshiba_acpi.handle,
942                                 ACPI_DEVICE_NOTIFY, toshiba_acpi_notify);
943                 sparse_keymap_free(toshiba_acpi.hotkey_dev);
944                 input_unregister_device(toshiba_acpi.hotkey_dev);
945         }
946
947         if (toshiba_acpi.bt_rfk) {
948                 rfkill_unregister(toshiba_acpi.bt_rfk);
949                 rfkill_destroy(toshiba_acpi.bt_rfk);
950         }
951
952         if (toshiba_backlight_device)
953                 backlight_device_unregister(toshiba_backlight_device);
954
955         remove_toshiba_proc_entries();
956
957         if (toshiba_proc_dir)
958                 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
959
960         if (toshiba_acpi.illumination_installed)
961                 led_classdev_unregister(&toshiba_led);
962
963         platform_device_unregister(toshiba_acpi.p_dev);
964
965         return;
966 }
967
968 static int __init toshiba_acpi_init(void)
969 {
970         u32 hci_result;
971         bool bt_present;
972         int ret = 0;
973         struct backlight_properties props;
974
975         if (acpi_disabled)
976                 return -ENODEV;
977
978         /* simple device detection: look for HCI method */
979         if (is_valid_acpi_path(TOSH_INTERFACE_1 GHCI_METHOD)) {
980                 method_hci = TOSH_INTERFACE_1 GHCI_METHOD;
981                 if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_1))
982                         printk(MY_INFO "Unable to activate hotkeys\n");
983         } else if (is_valid_acpi_path(TOSH_INTERFACE_2 GHCI_METHOD)) {
984                 method_hci = TOSH_INTERFACE_2 GHCI_METHOD;
985                 if (toshiba_acpi_setup_keyboard(TOSH_INTERFACE_2))
986                         printk(MY_INFO "Unable to activate hotkeys\n");
987         } else
988                 return -ENODEV;
989
990         printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
991                TOSHIBA_ACPI_VERSION);
992         printk(MY_INFO "    HCI method: %s\n", method_hci);
993
994         mutex_init(&toshiba_acpi.mutex);
995
996         toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
997                                                               -1, NULL, 0);
998         if (IS_ERR(toshiba_acpi.p_dev)) {
999                 ret = PTR_ERR(toshiba_acpi.p_dev);
1000                 printk(MY_ERR "unable to register platform device\n");
1001                 toshiba_acpi.p_dev = NULL;
1002                 toshiba_acpi_exit();
1003                 return ret;
1004         }
1005
1006         force_fan = 0;
1007         key_event_valid = 0;
1008
1009         /* enable event fifo */
1010         hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
1011
1012         toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1013         if (!toshiba_proc_dir) {
1014                 toshiba_acpi_exit();
1015                 return -ENODEV;
1016         } else {
1017                 create_toshiba_proc_entries();
1018         }
1019
1020         props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
1021         toshiba_backlight_device = backlight_device_register("toshiba",
1022                                                              &toshiba_acpi.p_dev->dev,
1023                                                              NULL,
1024                                                              &toshiba_backlight_data,
1025                                                              &props);
1026         if (IS_ERR(toshiba_backlight_device)) {
1027                 ret = PTR_ERR(toshiba_backlight_device);
1028
1029                 printk(KERN_ERR "Could not register toshiba backlight device\n");
1030                 toshiba_backlight_device = NULL;
1031                 toshiba_acpi_exit();
1032                 return ret;
1033         }
1034
1035         /* Register rfkill switch for Bluetooth */
1036         if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
1037                 toshiba_acpi.bt_rfk = rfkill_alloc(toshiba_acpi.bt_name,
1038                                                    &toshiba_acpi.p_dev->dev,
1039                                                    RFKILL_TYPE_BLUETOOTH,
1040                                                    &toshiba_rfk_ops,
1041                                                    &toshiba_acpi);
1042                 if (!toshiba_acpi.bt_rfk) {
1043                         printk(MY_ERR "unable to allocate rfkill device\n");
1044                         toshiba_acpi_exit();
1045                         return -ENOMEM;
1046                 }
1047
1048                 ret = rfkill_register(toshiba_acpi.bt_rfk);
1049                 if (ret) {
1050                         printk(MY_ERR "unable to register rfkill device\n");
1051                         rfkill_destroy(toshiba_acpi.bt_rfk);
1052                         toshiba_acpi_exit();
1053                         return ret;
1054                 }
1055         }
1056
1057         toshiba_acpi.illumination_installed = 0;
1058         if (toshiba_illumination_available()) {
1059                 if (!led_classdev_register(&(toshiba_acpi.p_dev->dev),
1060                                            &toshiba_led))
1061                         toshiba_acpi.illumination_installed = 1;
1062         }
1063
1064         return 0;
1065 }
1066
1067 module_init(toshiba_acpi_init);
1068 module_exit(toshiba_acpi_exit);