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