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