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