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