Merge tag 'hsi-for-3.16-fixes1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.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  *  Copyright (C) 2014 Azael Avalos
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  *
25  *  The devolpment page for this driver is located at
26  *  http://memebeam.org/toys/ToshibaAcpiDriver.
27  *
28  *  Credits:
29  *      Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
30  *              engineering the Windows drivers
31  *      Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
32  *      Rob Miller - TV out and hotkeys help
33  *
34  *
35  *  TODO
36  *
37  */
38
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
41 #define TOSHIBA_ACPI_VERSION    "0.20"
42 #define PROC_INTERFACE_VERSION  1
43
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/types.h>
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.h>
50 #include <linux/backlight.h>
51 #include <linux/rfkill.h>
52 #include <linux/input.h>
53 #include <linux/input/sparse-keymap.h>
54 #include <linux/leds.h>
55 #include <linux/slab.h>
56 #include <linux/workqueue.h>
57 #include <linux/i8042.h>
58 #include <linux/acpi.h>
59 #include <linux/dmi.h>
60 #include <asm/uaccess.h>
61
62 MODULE_AUTHOR("John Belmonte");
63 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
64 MODULE_LICENSE("GPL");
65
66 #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
67
68 /* Scan code for Fn key on TOS1900 models */
69 #define TOS1900_FN_SCAN         0x6e
70
71 /* Toshiba ACPI method paths */
72 #define METHOD_VIDEO_OUT        "\\_SB_.VALX.DSSX"
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  * SCI stands for "System Configuration Interface" which aim is to
84  * conceal differences in hardware between different models.
85  */
86
87 #define HCI_WORDS                       6
88
89 /* operations */
90 #define HCI_SET                         0xff00
91 #define HCI_GET                         0xfe00
92 #define SCI_OPEN                        0xf100
93 #define SCI_CLOSE                       0xf200
94 #define SCI_GET                         0xf300
95 #define SCI_SET                         0xf400
96
97 /* return codes */
98 #define HCI_SUCCESS                     0x0000
99 #define HCI_FAILURE                     0x1000
100 #define HCI_NOT_SUPPORTED               0x8000
101 #define HCI_EMPTY                       0x8c00
102 #define HCI_DATA_NOT_AVAILABLE          0x8d20
103 #define HCI_NOT_INITIALIZED             0x8d50
104 #define SCI_OPEN_CLOSE_OK               0x0044
105 #define SCI_ALREADY_OPEN                0x8100
106 #define SCI_NOT_OPENED                  0x8200
107 #define SCI_INPUT_DATA_ERROR            0x8300
108 #define SCI_NOT_PRESENT                 0x8600
109
110 /* registers */
111 #define HCI_FAN                         0x0004
112 #define HCI_TR_BACKLIGHT                0x0005
113 #define HCI_SYSTEM_EVENT                0x0016
114 #define HCI_VIDEO_OUT                   0x001c
115 #define HCI_HOTKEY_EVENT                0x001e
116 #define HCI_LCD_BRIGHTNESS              0x002a
117 #define HCI_WIRELESS                    0x0056
118 #define HCI_ACCELEROMETER               0x006d
119 #define HCI_KBD_ILLUMINATION            0x0095
120 #define HCI_ECO_MODE                    0x0097
121 #define HCI_ACCELEROMETER2              0x00a6
122 #define SCI_ILLUMINATION                0x014e
123 #define SCI_KBD_ILLUM_STATUS            0x015c
124 #define SCI_TOUCHPAD                    0x050e
125
126 /* field definitions */
127 #define HCI_ACCEL_MASK                  0x7fff
128 #define HCI_HOTKEY_DISABLE              0x0b
129 #define HCI_HOTKEY_ENABLE               0x09
130 #define HCI_LCD_BRIGHTNESS_BITS         3
131 #define HCI_LCD_BRIGHTNESS_SHIFT        (16-HCI_LCD_BRIGHTNESS_BITS)
132 #define HCI_LCD_BRIGHTNESS_LEVELS       (1 << HCI_LCD_BRIGHTNESS_BITS)
133 #define HCI_MISC_SHIFT                  0x10
134 #define HCI_VIDEO_OUT_LCD               0x1
135 #define HCI_VIDEO_OUT_CRT               0x2
136 #define HCI_VIDEO_OUT_TV                0x4
137 #define HCI_WIRELESS_KILL_SWITCH        0x01
138 #define HCI_WIRELESS_BT_PRESENT         0x0f
139 #define HCI_WIRELESS_BT_ATTACH          0x40
140 #define HCI_WIRELESS_BT_POWER           0x80
141 #define SCI_KBD_MODE_FNZ                0x1
142 #define SCI_KBD_MODE_AUTO               0x2
143
144 struct toshiba_acpi_dev {
145         struct acpi_device *acpi_dev;
146         const char *method_hci;
147         struct rfkill *bt_rfk;
148         struct input_dev *hotkey_dev;
149         struct work_struct hotkey_work;
150         struct backlight_device *backlight_dev;
151         struct led_classdev led_dev;
152         struct led_classdev kbd_led;
153         struct led_classdev eco_led;
154
155         int force_fan;
156         int last_key_event;
157         int key_event_valid;
158         int kbd_mode;
159         int kbd_time;
160
161         unsigned int illumination_supported:1;
162         unsigned int video_supported:1;
163         unsigned int fan_supported:1;
164         unsigned int system_event_supported:1;
165         unsigned int ntfy_supported:1;
166         unsigned int info_supported:1;
167         unsigned int tr_backlight_supported:1;
168         unsigned int kbd_illum_supported:1;
169         unsigned int kbd_led_registered:1;
170         unsigned int touchpad_supported:1;
171         unsigned int eco_supported:1;
172         unsigned int accelerometer_supported:1;
173         unsigned int sysfs_created:1;
174
175         struct mutex mutex;
176 };
177
178 static struct toshiba_acpi_dev *toshiba_acpi;
179
180 static const struct acpi_device_id toshiba_device_ids[] = {
181         {"TOS6200", 0},
182         {"TOS6208", 0},
183         {"TOS1900", 0},
184         {"", 0},
185 };
186 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
187
188 static const struct key_entry toshiba_acpi_keymap[] = {
189         { KE_KEY, 0x9e, { KEY_RFKILL } },
190         { KE_KEY, 0x101, { KEY_MUTE } },
191         { KE_KEY, 0x102, { KEY_ZOOMOUT } },
192         { KE_KEY, 0x103, { KEY_ZOOMIN } },
193         { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
194         { KE_KEY, 0x139, { KEY_ZOOMRESET } },
195         { KE_KEY, 0x13b, { KEY_COFFEE } },
196         { KE_KEY, 0x13c, { KEY_BATTERY } },
197         { KE_KEY, 0x13d, { KEY_SLEEP } },
198         { KE_KEY, 0x13e, { KEY_SUSPEND } },
199         { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
200         { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
201         { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
202         { KE_KEY, 0x142, { KEY_WLAN } },
203         { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
204         { KE_KEY, 0x17f, { KEY_FN } },
205         { KE_KEY, 0xb05, { KEY_PROG2 } },
206         { KE_KEY, 0xb06, { KEY_WWW } },
207         { KE_KEY, 0xb07, { KEY_MAIL } },
208         { KE_KEY, 0xb30, { KEY_STOP } },
209         { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
210         { KE_KEY, 0xb32, { KEY_NEXTSONG } },
211         { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
212         { KE_KEY, 0xb5a, { KEY_MEDIA } },
213         { KE_IGNORE, 0x1430, { KEY_RESERVED } },
214         { KE_END, 0 },
215 };
216
217 /* alternative keymap */
218 static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
219         {
220                 .matches = {
221                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
222                         DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
223                 },
224         },
225         {}
226 };
227
228 static const struct key_entry toshiba_acpi_alt_keymap[] = {
229         { KE_KEY, 0x157, { KEY_MUTE } },
230         { KE_KEY, 0x102, { KEY_ZOOMOUT } },
231         { KE_KEY, 0x103, { KEY_ZOOMIN } },
232         { KE_KEY, 0x139, { KEY_ZOOMRESET } },
233         { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
234         { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
235         { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
236         { KE_KEY, 0x158, { KEY_WLAN } },
237         { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
238         { KE_END, 0 },
239 };
240
241 /* utility
242  */
243
244 static __inline__ void _set_bit(u32 * word, u32 mask, int value)
245 {
246         *word = (*word & ~mask) | (mask * value);
247 }
248
249 /* acpi interface wrappers
250  */
251
252 static int write_acpi_int(const char *methodName, int val)
253 {
254         acpi_status status;
255
256         status = acpi_execute_simple_method(NULL, (char *)methodName, val);
257         return (status == AE_OK) ? 0 : -EIO;
258 }
259
260 /* Perform a raw HCI call.  Here we don't care about input or output buffer
261  * format.
262  */
263 static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
264                            const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
265 {
266         struct acpi_object_list params;
267         union acpi_object in_objs[HCI_WORDS];
268         struct acpi_buffer results;
269         union acpi_object out_objs[HCI_WORDS + 1];
270         acpi_status status;
271         int i;
272
273         params.count = HCI_WORDS;
274         params.pointer = in_objs;
275         for (i = 0; i < HCI_WORDS; ++i) {
276                 in_objs[i].type = ACPI_TYPE_INTEGER;
277                 in_objs[i].integer.value = in[i];
278         }
279
280         results.length = sizeof(out_objs);
281         results.pointer = out_objs;
282
283         status = acpi_evaluate_object(dev->acpi_dev->handle,
284                                       (char *)dev->method_hci, &params,
285                                       &results);
286         if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
287                 for (i = 0; i < out_objs->package.count; ++i) {
288                         out[i] = out_objs->package.elements[i].integer.value;
289                 }
290         }
291
292         return status;
293 }
294
295 /* common hci tasks (get or set one or two value)
296  *
297  * In addition to the ACPI status, the HCI system returns a result which
298  * may be useful (such as "not supported").
299  */
300
301 static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
302                               u32 in1, u32 *result)
303 {
304         u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
305         u32 out[HCI_WORDS];
306         acpi_status status = hci_raw(dev, in, out);
307         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
308         return status;
309 }
310
311 static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
312                              u32 *out1, u32 *result)
313 {
314         u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
315         u32 out[HCI_WORDS];
316         acpi_status status = hci_raw(dev, in, out);
317         *out1 = out[2];
318         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
319         return status;
320 }
321
322 static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
323                               u32 in1, u32 in2, u32 *result)
324 {
325         u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
326         u32 out[HCI_WORDS];
327         acpi_status status = hci_raw(dev, in, out);
328         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
329         return status;
330 }
331
332 static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
333                              u32 *out1, u32 *out2, u32 *result)
334 {
335         u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
336         u32 out[HCI_WORDS];
337         acpi_status status = hci_raw(dev, in, out);
338         *out1 = out[2];
339         *out2 = out[3];
340         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
341         return status;
342 }
343
344 /* common sci tasks
345  */
346
347 static int sci_open(struct toshiba_acpi_dev *dev)
348 {
349         u32 in[HCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
350         u32 out[HCI_WORDS];
351         acpi_status status;
352
353         status = hci_raw(dev, in, out);
354         if  (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
355                 pr_err("ACPI call to open SCI failed\n");
356                 return 0;
357         }
358
359         if (out[0] == SCI_OPEN_CLOSE_OK) {
360                 return 1;
361         } else if (out[0] == SCI_ALREADY_OPEN) {
362                 pr_info("Toshiba SCI already opened\n");
363                 return 1;
364         } else if (out[0] == SCI_NOT_PRESENT) {
365                 pr_info("Toshiba SCI is not present\n");
366         }
367
368         return 0;
369 }
370
371 static void sci_close(struct toshiba_acpi_dev *dev)
372 {
373         u32 in[HCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
374         u32 out[HCI_WORDS];
375         acpi_status status;
376
377         status = hci_raw(dev, in, out);
378         if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
379                 pr_err("ACPI call to close SCI failed\n");
380                 return;
381         }
382
383         if (out[0] == SCI_OPEN_CLOSE_OK)
384                 return;
385         else if (out[0] == SCI_NOT_OPENED)
386                 pr_info("Toshiba SCI not opened\n");
387         else if (out[0] == SCI_NOT_PRESENT)
388                 pr_info("Toshiba SCI is not present\n");
389 }
390
391 static acpi_status sci_read(struct toshiba_acpi_dev *dev, u32 reg,
392                             u32 *out1, u32 *result)
393 {
394         u32 in[HCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
395         u32 out[HCI_WORDS];
396         acpi_status status = hci_raw(dev, in, out);
397         *out1 = out[2];
398         *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
399         return status;
400 }
401
402 static acpi_status sci_write(struct toshiba_acpi_dev *dev, u32 reg,
403                              u32 in1, u32 *result)
404 {
405         u32 in[HCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
406         u32 out[HCI_WORDS];
407         acpi_status status = hci_raw(dev, in, out);
408         *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
409         return status;
410 }
411
412 /* Illumination support */
413 static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
414 {
415         u32 in[HCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
416         u32 out[HCI_WORDS];
417         acpi_status status;
418
419         if (!sci_open(dev))
420                 return 0;
421
422         status = hci_raw(dev, in, out);
423         sci_close(dev);
424         if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
425                 pr_err("ACPI call to query Illumination support failed\n");
426                 return 0;
427         } else if (out[0] == HCI_NOT_SUPPORTED || out[1] != 1) {
428                 pr_info("Illumination device not available\n");
429                 return 0;
430         }
431
432         return 1;
433 }
434
435 static void toshiba_illumination_set(struct led_classdev *cdev,
436                                      enum led_brightness brightness)
437 {
438         struct toshiba_acpi_dev *dev = container_of(cdev,
439                         struct toshiba_acpi_dev, led_dev);
440         u32 state, result;
441         acpi_status status;
442
443         /* First request : initialize communication. */
444         if (!sci_open(dev))
445                 return;
446
447         /* Switch the illumination on/off */
448         state = brightness ? 1 : 0;
449         status = sci_write(dev, SCI_ILLUMINATION, state, &result);
450         sci_close(dev);
451         if (ACPI_FAILURE(status)) {
452                 pr_err("ACPI call for illumination failed\n");
453                 return;
454         } else if (result == HCI_NOT_SUPPORTED) {
455                 pr_info("Illumination not supported\n");
456                 return;
457         }
458 }
459
460 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
461 {
462         struct toshiba_acpi_dev *dev = container_of(cdev,
463                         struct toshiba_acpi_dev, led_dev);
464         u32 state, result;
465         acpi_status status;
466
467         /* First request : initialize communication. */
468         if (!sci_open(dev))
469                 return LED_OFF;
470
471         /* Check the illumination */
472         status = sci_read(dev, SCI_ILLUMINATION, &state, &result);
473         sci_close(dev);
474         if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
475                 pr_err("ACPI call for illumination failed\n");
476                 return LED_OFF;
477         } else if (result == HCI_NOT_SUPPORTED) {
478                 pr_info("Illumination not supported\n");
479                 return LED_OFF;
480         }
481
482         return state ? LED_FULL : LED_OFF;
483 }
484
485 /* KBD Illumination */
486 static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
487 {
488         u32 result;
489         acpi_status status;
490
491         if (!sci_open(dev))
492                 return -EIO;
493
494         status = sci_write(dev, SCI_KBD_ILLUM_STATUS, time, &result);
495         sci_close(dev);
496         if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
497                 pr_err("ACPI call to set KBD backlight status failed\n");
498                 return -EIO;
499         } else if (result == HCI_NOT_SUPPORTED) {
500                 pr_info("Keyboard backlight status not supported\n");
501                 return -ENODEV;
502         }
503
504         return 0;
505 }
506
507 static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
508 {
509         u32 result;
510         acpi_status status;
511
512         if (!sci_open(dev))
513                 return -EIO;
514
515         status = sci_read(dev, SCI_KBD_ILLUM_STATUS, time, &result);
516         sci_close(dev);
517         if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
518                 pr_err("ACPI call to get KBD backlight status failed\n");
519                 return -EIO;
520         } else if (result == HCI_NOT_SUPPORTED) {
521                 pr_info("Keyboard backlight status not supported\n");
522                 return -ENODEV;
523         }
524
525         return 0;
526 }
527
528 static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
529 {
530         struct toshiba_acpi_dev *dev = container_of(cdev,
531                         struct toshiba_acpi_dev, kbd_led);
532         u32 state, result;
533         acpi_status status;
534
535         /* Check the keyboard backlight state */
536         status = hci_read1(dev, HCI_KBD_ILLUMINATION, &state, &result);
537         if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
538                 pr_err("ACPI call to get the keyboard backlight failed\n");
539                 return LED_OFF;
540         } else if (result == HCI_NOT_SUPPORTED) {
541                 pr_info("Keyboard backlight not supported\n");
542                 return LED_OFF;
543         }
544
545         return state ? LED_FULL : LED_OFF;
546 }
547
548 static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
549                                      enum led_brightness brightness)
550 {
551         struct toshiba_acpi_dev *dev = container_of(cdev,
552                         struct toshiba_acpi_dev, kbd_led);
553         u32 state, result;
554         acpi_status status;
555
556         /* Set the keyboard backlight state */
557         state = brightness ? 1 : 0;
558         status = hci_write1(dev, HCI_KBD_ILLUMINATION, state, &result);
559         if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
560                 pr_err("ACPI call to set KBD Illumination mode failed\n");
561                 return;
562         } else if (result == HCI_NOT_SUPPORTED) {
563                 pr_info("Keyboard backlight not supported\n");
564                 return;
565         }
566 }
567
568 /* TouchPad support */
569 static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
570 {
571         u32 result;
572         acpi_status status;
573
574         if (!sci_open(dev))
575                 return -EIO;
576
577         status = sci_write(dev, SCI_TOUCHPAD, state, &result);
578         sci_close(dev);
579         if (ACPI_FAILURE(status)) {
580                 pr_err("ACPI call to set the touchpad failed\n");
581                 return -EIO;
582         } else if (result == HCI_NOT_SUPPORTED) {
583                 return -ENODEV;
584         }
585
586         return 0;
587 }
588
589 static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
590 {
591         u32 result;
592         acpi_status status;
593
594         if (!sci_open(dev))
595                 return -EIO;
596
597         status = sci_read(dev, SCI_TOUCHPAD, state, &result);
598         sci_close(dev);
599         if (ACPI_FAILURE(status)) {
600                 pr_err("ACPI call to query the touchpad failed\n");
601                 return -EIO;
602         } else if (result == HCI_NOT_SUPPORTED) {
603                 return -ENODEV;
604         }
605
606         return 0;
607 }
608
609 /* Eco Mode support */
610 static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
611 {
612         acpi_status status;
613         u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
614         u32 out[HCI_WORDS];
615
616         status = hci_raw(dev, in, out);
617         if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
618                 pr_info("ACPI call to get ECO led failed\n");
619                 return 0;
620         }
621
622         return 1;
623 }
624
625 static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
626 {
627         struct toshiba_acpi_dev *dev = container_of(cdev,
628                         struct toshiba_acpi_dev, eco_led);
629         u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
630         u32 out[HCI_WORDS];
631         acpi_status status;
632
633         status = hci_raw(dev, in, out);
634         if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
635                 pr_err("ACPI call to get ECO led failed\n");
636                 return LED_OFF;
637         }
638
639         return out[2] ? LED_FULL : LED_OFF;
640 }
641
642 static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
643                                      enum led_brightness brightness)
644 {
645         struct toshiba_acpi_dev *dev = container_of(cdev,
646                         struct toshiba_acpi_dev, eco_led);
647         u32 in[HCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
648         u32 out[HCI_WORDS];
649         acpi_status status;
650
651         /* Switch the Eco Mode led on/off */
652         in[2] = (brightness) ? 1 : 0;
653         status = hci_raw(dev, in, out);
654         if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
655                 pr_err("ACPI call to set ECO led failed\n");
656                 return;
657         }
658 }
659
660 /* Accelerometer support */
661 static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
662 {
663         u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
664         u32 out[HCI_WORDS];
665         acpi_status status;
666
667         /* Check if the accelerometer call exists,
668          * this call also serves as initialization
669          */
670         status = hci_raw(dev, in, out);
671         if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
672                 pr_err("ACPI call to query the accelerometer failed\n");
673                 return -EIO;
674         } else if (out[0] == HCI_DATA_NOT_AVAILABLE ||
675                    out[0] == HCI_NOT_INITIALIZED) {
676                 pr_err("Accelerometer not initialized\n");
677                 return -EIO;
678         } else if (out[0] == HCI_NOT_SUPPORTED) {
679                 pr_info("Accelerometer not supported\n");
680                 return -ENODEV;
681         }
682
683         return 0;
684 }
685
686 static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
687                                       u32 *xy, u32 *z)
688 {
689         u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
690         u32 out[HCI_WORDS];
691         acpi_status status;
692
693         /* Check the Accelerometer status */
694         status = hci_raw(dev, in, out);
695         if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
696                 pr_err("ACPI call to query the accelerometer failed\n");
697                 return -EIO;
698         }
699
700         *xy = out[2];
701         *z = out[4];
702
703         return 0;
704 }
705
706 /* Bluetooth rfkill handlers */
707
708 static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
709 {
710         u32 hci_result;
711         u32 value, value2;
712
713         value = 0;
714         value2 = 0;
715         hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
716         if (hci_result == HCI_SUCCESS)
717                 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
718
719         return hci_result;
720 }
721
722 static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
723 {
724         u32 hci_result;
725         u32 value, value2;
726
727         value = 0;
728         value2 = 0x0001;
729         hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
730
731         *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
732         return hci_result;
733 }
734
735 static int bt_rfkill_set_block(void *data, bool blocked)
736 {
737         struct toshiba_acpi_dev *dev = data;
738         u32 result1, result2;
739         u32 value;
740         int err;
741         bool radio_state;
742
743         value = (blocked == false);
744
745         mutex_lock(&dev->mutex);
746         if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
747                 err = -EIO;
748                 goto out;
749         }
750
751         if (!radio_state) {
752                 err = 0;
753                 goto out;
754         }
755
756         hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
757         hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
758
759         if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
760                 err = -EIO;
761         else
762                 err = 0;
763  out:
764         mutex_unlock(&dev->mutex);
765         return err;
766 }
767
768 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
769 {
770         bool new_rfk_state;
771         bool value;
772         u32 hci_result;
773         struct toshiba_acpi_dev *dev = data;
774
775         mutex_lock(&dev->mutex);
776
777         hci_result = hci_get_radio_state(dev, &value);
778         if (hci_result != HCI_SUCCESS) {
779                 /* Can't do anything useful */
780                 mutex_unlock(&dev->mutex);
781                 return;
782         }
783
784         new_rfk_state = value;
785
786         mutex_unlock(&dev->mutex);
787
788         if (rfkill_set_hw_state(rfkill, !new_rfk_state))
789                 bt_rfkill_set_block(data, true);
790 }
791
792 static const struct rfkill_ops toshiba_rfk_ops = {
793         .set_block = bt_rfkill_set_block,
794         .poll = bt_rfkill_poll,
795 };
796
797 static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
798 {
799         u32 hci_result;
800         u32 status;
801
802         hci_read1(dev, HCI_TR_BACKLIGHT, &status, &hci_result);
803         *enabled = !status;
804         return hci_result == HCI_SUCCESS ? 0 : -EIO;
805 }
806
807 static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
808 {
809         u32 hci_result;
810         u32 value = !enable;
811
812         hci_write1(dev, HCI_TR_BACKLIGHT, value, &hci_result);
813         return hci_result == HCI_SUCCESS ? 0 : -EIO;
814 }
815
816 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
817
818 static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
819 {
820         u32 hci_result;
821         u32 value;
822         int brightness = 0;
823
824         if (dev->tr_backlight_supported) {
825                 bool enabled;
826                 int ret = get_tr_backlight_status(dev, &enabled);
827                 if (ret)
828                         return ret;
829                 if (enabled)
830                         return 0;
831                 brightness++;
832         }
833
834         hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
835         if (hci_result == HCI_SUCCESS)
836                 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
837
838         return -EIO;
839 }
840
841 static int get_lcd_brightness(struct backlight_device *bd)
842 {
843         struct toshiba_acpi_dev *dev = bl_get_data(bd);
844         return __get_lcd_brightness(dev);
845 }
846
847 static int lcd_proc_show(struct seq_file *m, void *v)
848 {
849         struct toshiba_acpi_dev *dev = m->private;
850         int value;
851         int levels;
852
853         if (!dev->backlight_dev)
854                 return -ENODEV;
855
856         levels = dev->backlight_dev->props.max_brightness + 1;
857         value = get_lcd_brightness(dev->backlight_dev);
858         if (value >= 0) {
859                 seq_printf(m, "brightness:              %d\n", value);
860                 seq_printf(m, "brightness_levels:       %d\n", levels);
861                 return 0;
862         }
863
864         pr_err("Error reading LCD brightness\n");
865         return -EIO;
866 }
867
868 static int lcd_proc_open(struct inode *inode, struct file *file)
869 {
870         return single_open(file, lcd_proc_show, PDE_DATA(inode));
871 }
872
873 static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
874 {
875         u32 hci_result;
876
877         if (dev->tr_backlight_supported) {
878                 bool enable = !value;
879                 int ret = set_tr_backlight_status(dev, enable);
880                 if (ret)
881                         return ret;
882                 if (value)
883                         value--;
884         }
885
886         value = value << HCI_LCD_BRIGHTNESS_SHIFT;
887         hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
888         return hci_result == HCI_SUCCESS ? 0 : -EIO;
889 }
890
891 static int set_lcd_status(struct backlight_device *bd)
892 {
893         struct toshiba_acpi_dev *dev = bl_get_data(bd);
894         return set_lcd_brightness(dev, bd->props.brightness);
895 }
896
897 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
898                               size_t count, loff_t *pos)
899 {
900         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
901         char cmd[42];
902         size_t len;
903         int value;
904         int ret;
905         int levels = dev->backlight_dev->props.max_brightness + 1;
906
907         len = min(count, sizeof(cmd) - 1);
908         if (copy_from_user(cmd, buf, len))
909                 return -EFAULT;
910         cmd[len] = '\0';
911
912         if (sscanf(cmd, " brightness : %i", &value) == 1 &&
913             value >= 0 && value < levels) {
914                 ret = set_lcd_brightness(dev, value);
915                 if (ret == 0)
916                         ret = count;
917         } else {
918                 ret = -EINVAL;
919         }
920         return ret;
921 }
922
923 static const struct file_operations lcd_proc_fops = {
924         .owner          = THIS_MODULE,
925         .open           = lcd_proc_open,
926         .read           = seq_read,
927         .llseek         = seq_lseek,
928         .release        = single_release,
929         .write          = lcd_proc_write,
930 };
931
932 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
933 {
934         u32 hci_result;
935
936         hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
937         return hci_result == HCI_SUCCESS ? 0 : -EIO;
938 }
939
940 static int video_proc_show(struct seq_file *m, void *v)
941 {
942         struct toshiba_acpi_dev *dev = m->private;
943         u32 value;
944         int ret;
945
946         ret = get_video_status(dev, &value);
947         if (!ret) {
948                 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
949                 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
950                 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
951                 seq_printf(m, "lcd_out:                 %d\n", is_lcd);
952                 seq_printf(m, "crt_out:                 %d\n", is_crt);
953                 seq_printf(m, "tv_out:                  %d\n", is_tv);
954         }
955
956         return ret;
957 }
958
959 static int video_proc_open(struct inode *inode, struct file *file)
960 {
961         return single_open(file, video_proc_show, PDE_DATA(inode));
962 }
963
964 static ssize_t video_proc_write(struct file *file, const char __user *buf,
965                                 size_t count, loff_t *pos)
966 {
967         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
968         char *cmd, *buffer;
969         int ret;
970         int value;
971         int remain = count;
972         int lcd_out = -1;
973         int crt_out = -1;
974         int tv_out = -1;
975         u32 video_out;
976
977         cmd = kmalloc(count + 1, GFP_KERNEL);
978         if (!cmd)
979                 return -ENOMEM;
980         if (copy_from_user(cmd, buf, count)) {
981                 kfree(cmd);
982                 return -EFAULT;
983         }
984         cmd[count] = '\0';
985
986         buffer = cmd;
987
988         /* scan expression.  Multiple expressions may be delimited with ;
989          *
990          *  NOTE: to keep scanning simple, invalid fields are ignored
991          */
992         while (remain) {
993                 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
994                         lcd_out = value & 1;
995                 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
996                         crt_out = value & 1;
997                 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
998                         tv_out = value & 1;
999                 /* advance to one character past the next ; */
1000                 do {
1001                         ++buffer;
1002                         --remain;
1003                 }
1004                 while (remain && *(buffer - 1) != ';');
1005         }
1006
1007         kfree(cmd);
1008
1009         ret = get_video_status(dev, &video_out);
1010         if (!ret) {
1011                 unsigned int new_video_out = video_out;
1012                 if (lcd_out != -1)
1013                         _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1014                 if (crt_out != -1)
1015                         _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1016                 if (tv_out != -1)
1017                         _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1018                 /* To avoid unnecessary video disruption, only write the new
1019                  * video setting if something changed. */
1020                 if (new_video_out != video_out)
1021                         ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1022         }
1023
1024         return ret ? ret : count;
1025 }
1026
1027 static const struct file_operations video_proc_fops = {
1028         .owner          = THIS_MODULE,
1029         .open           = video_proc_open,
1030         .read           = seq_read,
1031         .llseek         = seq_lseek,
1032         .release        = single_release,
1033         .write          = video_proc_write,
1034 };
1035
1036 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1037 {
1038         u32 hci_result;
1039
1040         hci_read1(dev, HCI_FAN, status, &hci_result);
1041         return hci_result == HCI_SUCCESS ? 0 : -EIO;
1042 }
1043
1044 static int fan_proc_show(struct seq_file *m, void *v)
1045 {
1046         struct toshiba_acpi_dev *dev = m->private;
1047         int ret;
1048         u32 value;
1049
1050         ret = get_fan_status(dev, &value);
1051         if (!ret) {
1052                 seq_printf(m, "running:                 %d\n", (value > 0));
1053                 seq_printf(m, "force_on:                %d\n", dev->force_fan);
1054         }
1055
1056         return ret;
1057 }
1058
1059 static int fan_proc_open(struct inode *inode, struct file *file)
1060 {
1061         return single_open(file, fan_proc_show, PDE_DATA(inode));
1062 }
1063
1064 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1065                               size_t count, loff_t *pos)
1066 {
1067         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1068         char cmd[42];
1069         size_t len;
1070         int value;
1071         u32 hci_result;
1072
1073         len = min(count, sizeof(cmd) - 1);
1074         if (copy_from_user(cmd, buf, len))
1075                 return -EFAULT;
1076         cmd[len] = '\0';
1077
1078         if (sscanf(cmd, " force_on : %i", &value) == 1 &&
1079             value >= 0 && value <= 1) {
1080                 hci_write1(dev, HCI_FAN, value, &hci_result);
1081                 if (hci_result != HCI_SUCCESS)
1082                         return -EIO;
1083                 else
1084                         dev->force_fan = value;
1085         } else {
1086                 return -EINVAL;
1087         }
1088
1089         return count;
1090 }
1091
1092 static const struct file_operations fan_proc_fops = {
1093         .owner          = THIS_MODULE,
1094         .open           = fan_proc_open,
1095         .read           = seq_read,
1096         .llseek         = seq_lseek,
1097         .release        = single_release,
1098         .write          = fan_proc_write,
1099 };
1100
1101 static int keys_proc_show(struct seq_file *m, void *v)
1102 {
1103         struct toshiba_acpi_dev *dev = m->private;
1104         u32 hci_result;
1105         u32 value;
1106
1107         if (!dev->key_event_valid && dev->system_event_supported) {
1108                 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1109                 if (hci_result == HCI_SUCCESS) {
1110                         dev->key_event_valid = 1;
1111                         dev->last_key_event = value;
1112                 } else if (hci_result == HCI_EMPTY) {
1113                         /* better luck next time */
1114                 } else if (hci_result == HCI_NOT_SUPPORTED) {
1115                         /* This is a workaround for an unresolved issue on
1116                          * some machines where system events sporadically
1117                          * become disabled. */
1118                         hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
1119                         pr_notice("Re-enabled hotkeys\n");
1120                 } else {
1121                         pr_err("Error reading hotkey status\n");
1122                         return -EIO;
1123                 }
1124         }
1125
1126         seq_printf(m, "hotkey_ready:            %d\n", dev->key_event_valid);
1127         seq_printf(m, "hotkey:                  0x%04x\n", dev->last_key_event);
1128         return 0;
1129 }
1130
1131 static int keys_proc_open(struct inode *inode, struct file *file)
1132 {
1133         return single_open(file, keys_proc_show, PDE_DATA(inode));
1134 }
1135
1136 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1137                                size_t count, loff_t *pos)
1138 {
1139         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1140         char cmd[42];
1141         size_t len;
1142         int value;
1143
1144         len = min(count, sizeof(cmd) - 1);
1145         if (copy_from_user(cmd, buf, len))
1146                 return -EFAULT;
1147         cmd[len] = '\0';
1148
1149         if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
1150                 dev->key_event_valid = 0;
1151         } else {
1152                 return -EINVAL;
1153         }
1154
1155         return count;
1156 }
1157
1158 static const struct file_operations keys_proc_fops = {
1159         .owner          = THIS_MODULE,
1160         .open           = keys_proc_open,
1161         .read           = seq_read,
1162         .llseek         = seq_lseek,
1163         .release        = single_release,
1164         .write          = keys_proc_write,
1165 };
1166
1167 static int version_proc_show(struct seq_file *m, void *v)
1168 {
1169         seq_printf(m, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
1170         seq_printf(m, "proc_interface:          %d\n", PROC_INTERFACE_VERSION);
1171         return 0;
1172 }
1173
1174 static int version_proc_open(struct inode *inode, struct file *file)
1175 {
1176         return single_open(file, version_proc_show, PDE_DATA(inode));
1177 }
1178
1179 static const struct file_operations version_proc_fops = {
1180         .owner          = THIS_MODULE,
1181         .open           = version_proc_open,
1182         .read           = seq_read,
1183         .llseek         = seq_lseek,
1184         .release        = single_release,
1185 };
1186
1187 /* proc and module init
1188  */
1189
1190 #define PROC_TOSHIBA            "toshiba"
1191
1192 static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1193 {
1194         if (dev->backlight_dev)
1195                 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1196                                  &lcd_proc_fops, dev);
1197         if (dev->video_supported)
1198                 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1199                                  &video_proc_fops, dev);
1200         if (dev->fan_supported)
1201                 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1202                                  &fan_proc_fops, dev);
1203         if (dev->hotkey_dev)
1204                 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1205                                  &keys_proc_fops, dev);
1206         proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1207                          &version_proc_fops, dev);
1208 }
1209
1210 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1211 {
1212         if (dev->backlight_dev)
1213                 remove_proc_entry("lcd", toshiba_proc_dir);
1214         if (dev->video_supported)
1215                 remove_proc_entry("video", toshiba_proc_dir);
1216         if (dev->fan_supported)
1217                 remove_proc_entry("fan", toshiba_proc_dir);
1218         if (dev->hotkey_dev)
1219                 remove_proc_entry("keys", toshiba_proc_dir);
1220         remove_proc_entry("version", toshiba_proc_dir);
1221 }
1222
1223 static const struct backlight_ops toshiba_backlight_data = {
1224         .options = BL_CORE_SUSPENDRESUME,
1225         .get_brightness = get_lcd_brightness,
1226         .update_status  = set_lcd_status,
1227 };
1228
1229 /*
1230  * Sysfs files
1231  */
1232
1233 static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1234                                          struct device_attribute *attr,
1235                                          const char *buf, size_t count)
1236 {
1237         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1238         int mode = -1;
1239         int time = -1;
1240
1241         if (sscanf(buf, "%i", &mode) != 1 && (mode != 2 || mode != 1))
1242                 return -EINVAL;
1243
1244         /* Set the Keyboard Backlight Mode where:
1245          * Mode - Auto (2) | FN-Z (1)
1246          *      Auto - KBD backlight turns off automatically in given time
1247          *      FN-Z - KBD backlight "toggles" when hotkey pressed
1248          */
1249         if (mode != -1 && toshiba->kbd_mode != mode) {
1250                 time = toshiba->kbd_time << HCI_MISC_SHIFT;
1251                 time = time + toshiba->kbd_mode;
1252                 if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1253                         return -EIO;
1254                 toshiba->kbd_mode = mode;
1255         }
1256
1257         return count;
1258 }
1259
1260 static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1261                                         struct device_attribute *attr,
1262                                         char *buf)
1263 {
1264         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1265         u32 time;
1266
1267         if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1268                 return -EIO;
1269
1270         return sprintf(buf, "%i\n", time & 0x07);
1271 }
1272
1273 static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1274                                             struct device_attribute *attr,
1275                                             const char *buf, size_t count)
1276 {
1277         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1278         int time = -1;
1279
1280         if (sscanf(buf, "%i", &time) != 1 && (time < 0 || time > 60))
1281                 return -EINVAL;
1282
1283         /* Set the Keyboard Backlight Timeout: 0-60 seconds */
1284         if (time != -1 && toshiba->kbd_time != time) {
1285                 time = time << HCI_MISC_SHIFT;
1286                 time = (toshiba->kbd_mode == SCI_KBD_MODE_AUTO) ?
1287                                                         time + 1 : time + 2;
1288                 if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1289                         return -EIO;
1290                 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1291         }
1292
1293         return count;
1294 }
1295
1296 static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1297                                            struct device_attribute *attr,
1298                                            char *buf)
1299 {
1300         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1301         u32 time;
1302
1303         if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1304                 return -EIO;
1305
1306         return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1307 }
1308
1309 static ssize_t toshiba_touchpad_store(struct device *dev,
1310                                       struct device_attribute *attr,
1311                                       const char *buf, size_t count)
1312 {
1313         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1314         int state;
1315
1316         /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1317         if (sscanf(buf, "%i", &state) == 1 && (state == 0 || state == 1)) {
1318                 if (toshiba_touchpad_set(toshiba, state) < 0)
1319                         return -EIO;
1320         }
1321
1322         return count;
1323 }
1324
1325 static ssize_t toshiba_touchpad_show(struct device *dev,
1326                                      struct device_attribute *attr, char *buf)
1327 {
1328         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1329         u32 state;
1330         int ret;
1331
1332         ret = toshiba_touchpad_get(toshiba, &state);
1333         if (ret < 0)
1334                 return ret;
1335
1336         return sprintf(buf, "%i\n", state);
1337 }
1338
1339 static ssize_t toshiba_position_show(struct device *dev,
1340                                      struct device_attribute *attr, char *buf)
1341 {
1342         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1343         u32 xyval, zval, tmp;
1344         u16 x, y, z;
1345         int ret;
1346
1347         xyval = zval = 0;
1348         ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1349         if (ret < 0)
1350                 return ret;
1351
1352         x = xyval & HCI_ACCEL_MASK;
1353         tmp = xyval >> HCI_MISC_SHIFT;
1354         y = tmp & HCI_ACCEL_MASK;
1355         z = zval & HCI_ACCEL_MASK;
1356
1357         return sprintf(buf, "%d %d %d\n", x, y, z);
1358 }
1359
1360 static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1361                    toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1362 static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1363                    toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
1364 static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1365                    toshiba_touchpad_show, toshiba_touchpad_store);
1366 static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
1367
1368 static struct attribute *toshiba_attributes[] = {
1369         &dev_attr_kbd_backlight_mode.attr,
1370         &dev_attr_kbd_backlight_timeout.attr,
1371         &dev_attr_touchpad.attr,
1372         &dev_attr_position.attr,
1373         NULL,
1374 };
1375
1376 static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
1377                                         struct attribute *attr, int idx)
1378 {
1379         struct device *dev = container_of(kobj, struct device, kobj);
1380         struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
1381         bool exists = true;
1382
1383         if (attr == &dev_attr_kbd_backlight_mode.attr)
1384                 exists = (drv->kbd_illum_supported) ? true : false;
1385         else if (attr == &dev_attr_kbd_backlight_timeout.attr)
1386                 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
1387         else if (attr == &dev_attr_touchpad.attr)
1388                 exists = (drv->touchpad_supported) ? true : false;
1389         else if (attr == &dev_attr_position.attr)
1390                 exists = (drv->accelerometer_supported) ? true : false;
1391
1392         return exists ? attr->mode : 0;
1393 }
1394
1395 static struct attribute_group toshiba_attr_group = {
1396         .is_visible = toshiba_sysfs_is_visible,
1397         .attrs = toshiba_attributes,
1398 };
1399
1400 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
1401                                       struct serio *port)
1402 {
1403         if (str & 0x20)
1404                 return false;
1405
1406         if (unlikely(data == 0xe0))
1407                 return false;
1408
1409         if ((data & 0x7f) == TOS1900_FN_SCAN) {
1410                 schedule_work(&toshiba_acpi->hotkey_work);
1411                 return true;
1412         }
1413
1414         return false;
1415 }
1416
1417 static void toshiba_acpi_hotkey_work(struct work_struct *work)
1418 {
1419         acpi_handle ec_handle = ec_get_handle();
1420         acpi_status status;
1421
1422         if (!ec_handle)
1423                 return;
1424
1425         status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
1426         if (ACPI_FAILURE(status))
1427                 pr_err("ACPI NTFY method execution failed\n");
1428 }
1429
1430 /*
1431  * Returns hotkey scancode, or < 0 on failure.
1432  */
1433 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
1434 {
1435         unsigned long long value;
1436         acpi_status status;
1437
1438         status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
1439                                       NULL, &value);
1440         if (ACPI_FAILURE(status)) {
1441                 pr_err("ACPI INFO method execution failed\n");
1442                 return -EIO;
1443         }
1444
1445         return value;
1446 }
1447
1448 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
1449                                        int scancode)
1450 {
1451         if (scancode == 0x100)
1452                 return;
1453
1454         /* act on key press; ignore key release */
1455         if (scancode & 0x80)
1456                 return;
1457
1458         if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
1459                 pr_info("Unknown key %x\n", scancode);
1460 }
1461
1462 static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
1463 {
1464         acpi_status status;
1465         acpi_handle ec_handle;
1466         int error;
1467         u32 hci_result;
1468         const struct key_entry *keymap = toshiba_acpi_keymap;
1469
1470         dev->hotkey_dev = input_allocate_device();
1471         if (!dev->hotkey_dev)
1472                 return -ENOMEM;
1473
1474         dev->hotkey_dev->name = "Toshiba input device";
1475         dev->hotkey_dev->phys = "toshiba_acpi/input0";
1476         dev->hotkey_dev->id.bustype = BUS_HOST;
1477
1478         if (dmi_check_system(toshiba_alt_keymap_dmi))
1479                 keymap = toshiba_acpi_alt_keymap;
1480         error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
1481         if (error)
1482                 goto err_free_dev;
1483
1484         /*
1485          * For some machines the SCI responsible for providing hotkey
1486          * notification doesn't fire. We can trigger the notification
1487          * whenever the Fn key is pressed using the NTFY method, if
1488          * supported, so if it's present set up an i8042 key filter
1489          * for this purpose.
1490          */
1491         status = AE_ERROR;
1492         ec_handle = ec_get_handle();
1493         if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
1494                 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
1495
1496                 error = i8042_install_filter(toshiba_acpi_i8042_filter);
1497                 if (error) {
1498                         pr_err("Error installing key filter\n");
1499                         goto err_free_keymap;
1500                 }
1501
1502                 dev->ntfy_supported = 1;
1503         }
1504
1505         /*
1506          * Determine hotkey query interface. Prefer using the INFO
1507          * method when it is available.
1508          */
1509         if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
1510                 dev->info_supported = 1;
1511         else {
1512                 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
1513                 if (hci_result == HCI_SUCCESS)
1514                         dev->system_event_supported = 1;
1515         }
1516
1517         if (!dev->info_supported && !dev->system_event_supported) {
1518                 pr_warn("No hotkey query interface found\n");
1519                 goto err_remove_filter;
1520         }
1521
1522         status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
1523         if (ACPI_FAILURE(status)) {
1524                 pr_info("Unable to enable hotkeys\n");
1525                 error = -ENODEV;
1526                 goto err_remove_filter;
1527         }
1528
1529         error = input_register_device(dev->hotkey_dev);
1530         if (error) {
1531                 pr_info("Unable to register input device\n");
1532                 goto err_remove_filter;
1533         }
1534
1535         hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result);
1536         return 0;
1537
1538  err_remove_filter:
1539         if (dev->ntfy_supported)
1540                 i8042_remove_filter(toshiba_acpi_i8042_filter);
1541  err_free_keymap:
1542         sparse_keymap_free(dev->hotkey_dev);
1543  err_free_dev:
1544         input_free_device(dev->hotkey_dev);
1545         dev->hotkey_dev = NULL;
1546         return error;
1547 }
1548
1549 static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
1550 {
1551         struct backlight_properties props;
1552         int brightness;
1553         int ret;
1554         bool enabled;
1555
1556         /*
1557          * Some machines don't support the backlight methods at all, and
1558          * others support it read-only. Either of these is pretty useless,
1559          * so only register the backlight device if the backlight method
1560          * supports both reads and writes.
1561          */
1562         brightness = __get_lcd_brightness(dev);
1563         if (brightness < 0)
1564                 return 0;
1565         ret = set_lcd_brightness(dev, brightness);
1566         if (ret) {
1567                 pr_debug("Backlight method is read-only, disabling backlight support\n");
1568                 return 0;
1569         }
1570
1571         /* Determine whether or not BIOS supports transflective backlight */
1572         ret = get_tr_backlight_status(dev, &enabled);
1573         dev->tr_backlight_supported = !ret;
1574
1575         memset(&props, 0, sizeof(props));
1576         props.type = BACKLIGHT_PLATFORM;
1577         props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
1578
1579         /* adding an extra level and having 0 change to transflective mode */
1580         if (dev->tr_backlight_supported)
1581                 props.max_brightness++;
1582
1583         dev->backlight_dev = backlight_device_register("toshiba",
1584                                                        &dev->acpi_dev->dev,
1585                                                        dev,
1586                                                        &toshiba_backlight_data,
1587                                                        &props);
1588         if (IS_ERR(dev->backlight_dev)) {
1589                 ret = PTR_ERR(dev->backlight_dev);
1590                 pr_err("Could not register toshiba backlight device\n");
1591                 dev->backlight_dev = NULL;
1592                 return ret;
1593         }
1594
1595         dev->backlight_dev->props.brightness = brightness;
1596         return 0;
1597 }
1598
1599 static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
1600 {
1601         struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1602
1603         remove_toshiba_proc_entries(dev);
1604
1605         if (dev->sysfs_created)
1606                 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
1607                                    &toshiba_attr_group);
1608
1609         if (dev->ntfy_supported) {
1610                 i8042_remove_filter(toshiba_acpi_i8042_filter);
1611                 cancel_work_sync(&dev->hotkey_work);
1612         }
1613
1614         if (dev->hotkey_dev) {
1615                 input_unregister_device(dev->hotkey_dev);
1616                 sparse_keymap_free(dev->hotkey_dev);
1617         }
1618
1619         if (dev->bt_rfk) {
1620                 rfkill_unregister(dev->bt_rfk);
1621                 rfkill_destroy(dev->bt_rfk);
1622         }
1623
1624         if (dev->backlight_dev)
1625                 backlight_device_unregister(dev->backlight_dev);
1626
1627         if (dev->illumination_supported)
1628                 led_classdev_unregister(&dev->led_dev);
1629
1630         if (dev->kbd_led_registered)
1631                 led_classdev_unregister(&dev->kbd_led);
1632
1633         if (dev->eco_supported)
1634                 led_classdev_unregister(&dev->eco_led);
1635
1636         if (toshiba_acpi)
1637                 toshiba_acpi = NULL;
1638
1639         kfree(dev);
1640
1641         return 0;
1642 }
1643
1644 static const char *find_hci_method(acpi_handle handle)
1645 {
1646         if (acpi_has_method(handle, "GHCI"))
1647                 return "GHCI";
1648
1649         if (acpi_has_method(handle, "SPFC"))
1650                 return "SPFC";
1651
1652         return NULL;
1653 }
1654
1655 static int toshiba_acpi_add(struct acpi_device *acpi_dev)
1656 {
1657         struct toshiba_acpi_dev *dev;
1658         const char *hci_method;
1659         u32 dummy;
1660         bool bt_present;
1661         int ret = 0;
1662
1663         if (toshiba_acpi)
1664                 return -EBUSY;
1665
1666         pr_info("Toshiba Laptop ACPI Extras version %s\n",
1667                TOSHIBA_ACPI_VERSION);
1668
1669         hci_method = find_hci_method(acpi_dev->handle);
1670         if (!hci_method) {
1671                 pr_err("HCI interface not found\n");
1672                 return -ENODEV;
1673         }
1674
1675         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1676         if (!dev)
1677                 return -ENOMEM;
1678         dev->acpi_dev = acpi_dev;
1679         dev->method_hci = hci_method;
1680         acpi_dev->driver_data = dev;
1681         dev_set_drvdata(&acpi_dev->dev, dev);
1682
1683         if (toshiba_acpi_setup_keyboard(dev))
1684                 pr_info("Unable to activate hotkeys\n");
1685
1686         mutex_init(&dev->mutex);
1687
1688         ret = toshiba_acpi_setup_backlight(dev);
1689         if (ret)
1690                 goto error;
1691
1692         /* Register rfkill switch for Bluetooth */
1693         if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
1694                 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
1695                                            &acpi_dev->dev,
1696                                            RFKILL_TYPE_BLUETOOTH,
1697                                            &toshiba_rfk_ops,
1698                                            dev);
1699                 if (!dev->bt_rfk) {
1700                         pr_err("unable to allocate rfkill device\n");
1701                         ret = -ENOMEM;
1702                         goto error;
1703                 }
1704
1705                 ret = rfkill_register(dev->bt_rfk);
1706                 if (ret) {
1707                         pr_err("unable to register rfkill device\n");
1708                         rfkill_destroy(dev->bt_rfk);
1709                         goto error;
1710                 }
1711         }
1712
1713         if (toshiba_illumination_available(dev)) {
1714                 dev->led_dev.name = "toshiba::illumination";
1715                 dev->led_dev.max_brightness = 1;
1716                 dev->led_dev.brightness_set = toshiba_illumination_set;
1717                 dev->led_dev.brightness_get = toshiba_illumination_get;
1718                 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
1719                         dev->illumination_supported = 1;
1720         }
1721
1722         if (toshiba_eco_mode_available(dev)) {
1723                 dev->eco_led.name = "toshiba::eco_mode";
1724                 dev->eco_led.max_brightness = 1;
1725                 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
1726                 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
1727                 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
1728                         dev->eco_supported = 1;
1729         }
1730
1731         ret = toshiba_kbd_illum_status_get(dev, &dummy);
1732         if (!ret) {
1733                 dev->kbd_time = dummy >> HCI_MISC_SHIFT;
1734                 dev->kbd_mode = dummy & 0x07;
1735         }
1736         dev->kbd_illum_supported = !ret;
1737         /*
1738          * Only register the LED if KBD illumination is supported
1739          * and the keyboard backlight operation mode is set to FN-Z
1740          */
1741         if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
1742                 dev->kbd_led.name = "toshiba::kbd_backlight";
1743                 dev->kbd_led.max_brightness = 1;
1744                 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
1745                 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
1746                 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
1747                         dev->kbd_led_registered = 1;
1748         }
1749
1750         ret = toshiba_touchpad_get(dev, &dummy);
1751         dev->touchpad_supported = !ret;
1752
1753         ret = toshiba_accelerometer_supported(dev);
1754         dev->accelerometer_supported = !ret;
1755
1756         /* Determine whether or not BIOS supports fan and video interfaces */
1757
1758         ret = get_video_status(dev, &dummy);
1759         dev->video_supported = !ret;
1760
1761         ret = get_fan_status(dev, &dummy);
1762         dev->fan_supported = !ret;
1763
1764         ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
1765                                  &toshiba_attr_group);
1766         if (ret) {
1767                 dev->sysfs_created = 0;
1768                 goto error;
1769         }
1770         dev->sysfs_created = !ret;
1771
1772         create_toshiba_proc_entries(dev);
1773
1774         toshiba_acpi = dev;
1775
1776         return 0;
1777
1778 error:
1779         toshiba_acpi_remove(acpi_dev);
1780         return ret;
1781 }
1782
1783 static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1784 {
1785         struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1786         u32 hci_result, value;
1787         int retries = 3;
1788         int scancode;
1789
1790         if (event != 0x80)
1791                 return;
1792
1793         if (dev->info_supported) {
1794                 scancode = toshiba_acpi_query_hotkey(dev);
1795                 if (scancode < 0)
1796                         pr_err("Failed to query hotkey event\n");
1797                 else if (scancode != 0)
1798                         toshiba_acpi_report_hotkey(dev, scancode);
1799         } else if (dev->system_event_supported) {
1800                 do {
1801                         hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1802                         switch (hci_result) {
1803                         case HCI_SUCCESS:
1804                                 toshiba_acpi_report_hotkey(dev, (int)value);
1805                                 break;
1806                         case HCI_NOT_SUPPORTED:
1807                                 /*
1808                                  * This is a workaround for an unresolved
1809                                  * issue on some machines where system events
1810                                  * sporadically become disabled.
1811                                  */
1812                                 hci_write1(dev, HCI_SYSTEM_EVENT, 1,
1813                                            &hci_result);
1814                                 pr_notice("Re-enabled hotkeys\n");
1815                                 /* fall through */
1816                         default:
1817                                 retries--;
1818                                 break;
1819                         }
1820                 } while (retries && hci_result != HCI_EMPTY);
1821         }
1822 }
1823
1824 #ifdef CONFIG_PM_SLEEP
1825 static int toshiba_acpi_suspend(struct device *device)
1826 {
1827         struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
1828         u32 result;
1829
1830         if (dev->hotkey_dev)
1831                 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result);
1832
1833         return 0;
1834 }
1835
1836 static int toshiba_acpi_resume(struct device *device)
1837 {
1838         struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
1839         u32 result;
1840
1841         if (dev->hotkey_dev)
1842                 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result);
1843
1844         return 0;
1845 }
1846 #endif
1847
1848 static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
1849                          toshiba_acpi_suspend, toshiba_acpi_resume);
1850
1851 static struct acpi_driver toshiba_acpi_driver = {
1852         .name   = "Toshiba ACPI driver",
1853         .owner  = THIS_MODULE,
1854         .ids    = toshiba_device_ids,
1855         .flags  = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1856         .ops    = {
1857                 .add            = toshiba_acpi_add,
1858                 .remove         = toshiba_acpi_remove,
1859                 .notify         = toshiba_acpi_notify,
1860         },
1861         .drv.pm = &toshiba_acpi_pm,
1862 };
1863
1864 static int __init toshiba_acpi_init(void)
1865 {
1866         int ret;
1867
1868         /*
1869          * Machines with this WMI guid aren't supported due to bugs in
1870          * their AML. This check relies on wmi initializing before
1871          * toshiba_acpi to guarantee guids have been identified.
1872          */
1873         if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
1874                 return -ENODEV;
1875
1876         toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1877         if (!toshiba_proc_dir) {
1878                 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
1879                 return -ENODEV;
1880         }
1881
1882         ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1883         if (ret) {
1884                 pr_err("Failed to register ACPI driver: %d\n", ret);
1885                 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1886         }
1887
1888         return ret;
1889 }
1890
1891 static void __exit toshiba_acpi_exit(void)
1892 {
1893         acpi_bus_unregister_driver(&toshiba_acpi_driver);
1894         if (toshiba_proc_dir)
1895                 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1896 }
1897
1898 module_init(toshiba_acpi_init);
1899 module_exit(toshiba_acpi_exit);