OSDN Git Service

f624dd56051f303965d5677dabcc848347f75514
[uclinux-h8/linux.git] / drivers / platform / x86 / toshiba_acpi.c
1 /*
2  *  toshiba_acpi.c - Toshiba Laptop ACPI Extras
3  *
4  *  Copyright (C) 2002-2004 John Belmonte
5  *  Copyright (C) 2008 Philip Langdale
6  *  Copyright (C) 2010 Pierre Ducroquet
7  *  Copyright (C) 2014-2015 Azael Avalos
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  *  The full GNU General Public License is included in this distribution in
20  *  the file called "COPYING".
21  *
22  *  The devolpment page for this driver is located at
23  *  http://memebeam.org/toys/ToshibaAcpiDriver.
24  *
25  *  Credits:
26  *      Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27  *              engineering the Windows drivers
28  *      Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29  *      Rob Miller - TV out and hotkeys help
30  */
31
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
34 #define TOSHIBA_ACPI_VERSION    "0.21"
35 #define PROC_INTERFACE_VERSION  1
36
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/backlight.h>
44 #include <linux/rfkill.h>
45 #include <linux/input.h>
46 #include <linux/input/sparse-keymap.h>
47 #include <linux/leds.h>
48 #include <linux/slab.h>
49 #include <linux/workqueue.h>
50 #include <linux/i8042.h>
51 #include <linux/acpi.h>
52 #include <linux/uaccess.h>
53
54 MODULE_AUTHOR("John Belmonte");
55 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
56 MODULE_LICENSE("GPL");
57
58 #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
59
60 /* Scan code for Fn key on TOS1900 models */
61 #define TOS1900_FN_SCAN         0x6e
62
63 /* Toshiba ACPI method paths */
64 #define METHOD_VIDEO_OUT        "\\_SB_.VALX.DSSX"
65
66 /*
67  * The Toshiba configuration interface is composed of the HCI and the SCI,
68  * which are defined as follows:
69  *
70  * HCI is Toshiba's "Hardware Control Interface" which is supposed to
71  * be uniform across all their models.  Ideally we would just call
72  * dedicated ACPI methods instead of using this primitive interface.
73  * However the ACPI methods seem to be incomplete in some areas (for
74  * example they allow setting, but not reading, the LCD brightness value),
75  * so this is still useful.
76  *
77  * SCI stands for "System Configuration Interface" which aim is to
78  * conceal differences in hardware between different models.
79  */
80
81 #define TCI_WORDS                       6
82
83 /* operations */
84 #define HCI_SET                         0xff00
85 #define HCI_GET                         0xfe00
86 #define SCI_OPEN                        0xf100
87 #define SCI_CLOSE                       0xf200
88 #define SCI_GET                         0xf300
89 #define SCI_SET                         0xf400
90
91 /* return codes */
92 #define TOS_SUCCESS                     0x0000
93 #define TOS_OPEN_CLOSE_OK               0x0044
94 #define TOS_FAILURE                     0x1000
95 #define TOS_NOT_SUPPORTED               0x8000
96 #define TOS_ALREADY_OPEN                0x8100
97 #define TOS_NOT_OPENED                  0x8200
98 #define TOS_INPUT_DATA_ERROR            0x8300
99 #define TOS_WRITE_PROTECTED             0x8400
100 #define TOS_NOT_PRESENT                 0x8600
101 #define TOS_FIFO_EMPTY                  0x8c00
102 #define TOS_DATA_NOT_AVAILABLE          0x8d20
103 #define TOS_NOT_INITIALIZED             0x8d50
104 #define TOS_NOT_INSTALLED               0x8e00
105
106 /* registers */
107 #define HCI_FAN                         0x0004
108 #define HCI_TR_BACKLIGHT                0x0005
109 #define HCI_SYSTEM_EVENT                0x0016
110 #define HCI_VIDEO_OUT                   0x001c
111 #define HCI_HOTKEY_EVENT                0x001e
112 #define HCI_LCD_BRIGHTNESS              0x002a
113 #define HCI_WIRELESS                    0x0056
114 #define HCI_ACCELEROMETER               0x006d
115 #define HCI_KBD_ILLUMINATION            0x0095
116 #define HCI_ECO_MODE                    0x0097
117 #define HCI_ACCELEROMETER2              0x00a6
118 #define HCI_SYSTEM_INFO                 0xc000
119 #define SCI_PANEL_POWER_ON              0x010d
120 #define SCI_ILLUMINATION                0x014e
121 #define SCI_USB_SLEEP_CHARGE            0x0150
122 #define SCI_KBD_ILLUM_STATUS            0x015c
123 #define SCI_USB_SLEEP_MUSIC             0x015e
124 #define SCI_USB_THREE                   0x0169
125 #define SCI_TOUCHPAD                    0x050e
126 #define SCI_KBD_FUNCTION_KEYS           0x0522
127
128 /* field definitions */
129 #define HCI_ACCEL_MASK                  0x7fff
130 #define HCI_HOTKEY_DISABLE              0x0b
131 #define HCI_HOTKEY_ENABLE               0x09
132 #define HCI_HOTKEY_SPECIAL_FUNCTIONS    0x10
133 #define HCI_LCD_BRIGHTNESS_BITS         3
134 #define HCI_LCD_BRIGHTNESS_SHIFT        (16-HCI_LCD_BRIGHTNESS_BITS)
135 #define HCI_LCD_BRIGHTNESS_LEVELS       (1 << HCI_LCD_BRIGHTNESS_BITS)
136 #define HCI_MISC_SHIFT                  0x10
137 #define HCI_SYSTEM_TYPE1                0x10
138 #define HCI_SYSTEM_TYPE2                0x11
139 #define HCI_VIDEO_OUT_LCD               0x1
140 #define HCI_VIDEO_OUT_CRT               0x2
141 #define HCI_VIDEO_OUT_TV                0x4
142 #define HCI_WIRELESS_KILL_SWITCH        0x01
143 #define HCI_WIRELESS_BT_PRESENT         0x0f
144 #define HCI_WIRELESS_BT_ATTACH          0x40
145 #define HCI_WIRELESS_BT_POWER           0x80
146 #define SCI_KBD_MODE_MASK               0x1f
147 #define SCI_KBD_MODE_FNZ                0x1
148 #define SCI_KBD_MODE_AUTO               0x2
149 #define SCI_KBD_MODE_ON                 0x8
150 #define SCI_KBD_MODE_OFF                0x10
151 #define SCI_KBD_TIME_MAX                0x3c001a
152 #define SCI_USB_CHARGE_MODE_MASK        0xff
153 #define SCI_USB_CHARGE_DISABLED         0x00
154 #define SCI_USB_CHARGE_ALTERNATE        0x09
155 #define SCI_USB_CHARGE_TYPICAL          0x11
156 #define SCI_USB_CHARGE_AUTO             0x21
157 #define SCI_USB_CHARGE_BAT_MASK         0x7
158 #define SCI_USB_CHARGE_BAT_LVL_OFF      0x1
159 #define SCI_USB_CHARGE_BAT_LVL_ON       0x4
160 #define SCI_USB_CHARGE_BAT_LVL          0x0200
161 #define SCI_USB_CHARGE_RAPID_DSP        0x0300
162
163 struct toshiba_acpi_dev {
164         struct acpi_device *acpi_dev;
165         const char *method_hci;
166         struct rfkill *bt_rfk;
167         struct input_dev *hotkey_dev;
168         struct work_struct hotkey_work;
169         struct backlight_device *backlight_dev;
170         struct led_classdev led_dev;
171         struct led_classdev kbd_led;
172         struct led_classdev eco_led;
173
174         int force_fan;
175         int last_key_event;
176         int key_event_valid;
177         int kbd_type;
178         int kbd_mode;
179         int kbd_time;
180         int usbsc_bat_level;
181         int usbsc_mode_base;
182         int hotkey_event_type;
183
184         unsigned int illumination_supported:1;
185         unsigned int video_supported:1;
186         unsigned int fan_supported:1;
187         unsigned int system_event_supported:1;
188         unsigned int ntfy_supported:1;
189         unsigned int info_supported:1;
190         unsigned int tr_backlight_supported:1;
191         unsigned int kbd_illum_supported:1;
192         unsigned int kbd_led_registered:1;
193         unsigned int touchpad_supported:1;
194         unsigned int eco_supported:1;
195         unsigned int accelerometer_supported:1;
196         unsigned int usb_sleep_charge_supported:1;
197         unsigned int usb_rapid_charge_supported:1;
198         unsigned int usb_sleep_music_supported:1;
199         unsigned int kbd_function_keys_supported:1;
200         unsigned int panel_power_on_supported:1;
201         unsigned int usb_three_supported:1;
202         unsigned int sysfs_created:1;
203
204         struct mutex mutex;
205 };
206
207 static struct toshiba_acpi_dev *toshiba_acpi;
208
209 static const struct acpi_device_id toshiba_device_ids[] = {
210         {"TOS6200", 0},
211         {"TOS6207", 0},
212         {"TOS6208", 0},
213         {"TOS1900", 0},
214         {"", 0},
215 };
216 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
217
218 static const struct key_entry toshiba_acpi_keymap[] = {
219         { KE_KEY, 0x9e, { KEY_RFKILL } },
220         { KE_KEY, 0x101, { KEY_MUTE } },
221         { KE_KEY, 0x102, { KEY_ZOOMOUT } },
222         { KE_KEY, 0x103, { KEY_ZOOMIN } },
223         { KE_KEY, 0x10f, { KEY_TAB } },
224         { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
225         { KE_KEY, 0x139, { KEY_ZOOMRESET } },
226         { KE_KEY, 0x13b, { KEY_COFFEE } },
227         { KE_KEY, 0x13c, { KEY_BATTERY } },
228         { KE_KEY, 0x13d, { KEY_SLEEP } },
229         { KE_KEY, 0x13e, { KEY_SUSPEND } },
230         { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
231         { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
232         { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
233         { KE_KEY, 0x142, { KEY_WLAN } },
234         { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
235         { KE_KEY, 0x17f, { KEY_FN } },
236         { KE_KEY, 0xb05, { KEY_PROG2 } },
237         { KE_KEY, 0xb06, { KEY_WWW } },
238         { KE_KEY, 0xb07, { KEY_MAIL } },
239         { KE_KEY, 0xb30, { KEY_STOP } },
240         { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
241         { KE_KEY, 0xb32, { KEY_NEXTSONG } },
242         { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
243         { KE_KEY, 0xb5a, { KEY_MEDIA } },
244         { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
245         { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
246         { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
247         { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
248         { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
249         { KE_END, 0 },
250 };
251
252 static const struct key_entry toshiba_acpi_alt_keymap[] = {
253         { KE_KEY, 0x157, { KEY_MUTE } },
254         { KE_KEY, 0x102, { KEY_ZOOMOUT } },
255         { KE_KEY, 0x103, { KEY_ZOOMIN } },
256         { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
257         { KE_KEY, 0x139, { KEY_ZOOMRESET } },
258         { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
259         { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
260         { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
261         { KE_KEY, 0x158, { KEY_WLAN } },
262         { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
263         { KE_END, 0 },
264 };
265
266 /*
267  * Utility
268  */
269
270 static inline void _set_bit(u32 *word, u32 mask, int value)
271 {
272         *word = (*word & ~mask) | (mask * value);
273 }
274
275 /*
276  * ACPI interface wrappers
277  */
278
279 static int write_acpi_int(const char *methodName, int val)
280 {
281         acpi_status status;
282
283         status = acpi_execute_simple_method(NULL, (char *)methodName, val);
284         return (status == AE_OK) ? 0 : -EIO;
285 }
286
287 /*
288  * Perform a raw configuration call.  Here we don't care about input or output
289  * buffer format.
290  */
291 static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
292                            const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
293 {
294         struct acpi_object_list params;
295         union acpi_object in_objs[TCI_WORDS];
296         struct acpi_buffer results;
297         union acpi_object out_objs[TCI_WORDS + 1];
298         acpi_status status;
299         int i;
300
301         params.count = TCI_WORDS;
302         params.pointer = in_objs;
303         for (i = 0; i < TCI_WORDS; ++i) {
304                 in_objs[i].type = ACPI_TYPE_INTEGER;
305                 in_objs[i].integer.value = in[i];
306         }
307
308         results.length = sizeof(out_objs);
309         results.pointer = out_objs;
310
311         status = acpi_evaluate_object(dev->acpi_dev->handle,
312                                       (char *)dev->method_hci, &params,
313                                       &results);
314         if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
315                 for (i = 0; i < out_objs->package.count; ++i)
316                         out[i] = out_objs->package.elements[i].integer.value;
317         }
318
319         return status;
320 }
321
322 /*
323  * Common hci tasks (get or set one or two value)
324  *
325  * In addition to the ACPI status, the HCI system returns a result which
326  * may be useful (such as "not supported").
327  */
328
329 static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
330 {
331         u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
332         u32 out[TCI_WORDS];
333         acpi_status status = tci_raw(dev, in, out);
334
335         return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
336 }
337
338 static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
339 {
340         u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
341         u32 out[TCI_WORDS];
342         acpi_status status = tci_raw(dev, in, out);
343
344         if (ACPI_FAILURE(status))
345                 return TOS_FAILURE;
346
347         *out1 = out[2];
348
349         return out[0];
350 }
351
352 static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
353 {
354         u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
355         u32 out[TCI_WORDS];
356         acpi_status status = tci_raw(dev, in, out);
357
358         return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
359 }
360
361 static u32 hci_read2(struct toshiba_acpi_dev *dev,
362                      u32 reg, u32 *out1, u32 *out2)
363 {
364         u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
365         u32 out[TCI_WORDS];
366         acpi_status status = tci_raw(dev, in, out);
367
368         if (ACPI_FAILURE(status))
369                 return TOS_FAILURE;
370
371         *out1 = out[2];
372         *out2 = out[3];
373
374         return out[0];
375 }
376
377 /*
378  * Common sci tasks
379  */
380
381 static int sci_open(struct toshiba_acpi_dev *dev)
382 {
383         u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
384         u32 out[TCI_WORDS];
385         acpi_status status;
386
387         status = tci_raw(dev, in, out);
388         if  (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
389                 pr_err("ACPI call to open SCI failed\n");
390                 return 0;
391         }
392
393         if (out[0] == TOS_OPEN_CLOSE_OK) {
394                 return 1;
395         } else if (out[0] == TOS_ALREADY_OPEN) {
396                 pr_info("Toshiba SCI already opened\n");
397                 return 1;
398         } else if (out[0] == TOS_NOT_SUPPORTED) {
399                 /*
400                  * Some BIOSes do not have the SCI open/close functions
401                  * implemented and return 0x8000 (Not Supported), failing to
402                  * register some supported features.
403                  *
404                  * Simply return 1 if we hit those affected laptops to make the
405                  * supported features work.
406                  *
407                  * In the case that some laptops really do not support the SCI,
408                  * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
409                  * and thus, not registering support for the queried feature.
410                  */
411                 return 1;
412         } else if (out[0] == TOS_NOT_PRESENT) {
413                 pr_info("Toshiba SCI is not present\n");
414         }
415
416         return 0;
417 }
418
419 static void sci_close(struct toshiba_acpi_dev *dev)
420 {
421         u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
422         u32 out[TCI_WORDS];
423         acpi_status status;
424
425         status = tci_raw(dev, in, out);
426         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
427                 pr_err("ACPI call to close SCI failed\n");
428                 return;
429         }
430
431         if (out[0] == TOS_OPEN_CLOSE_OK)
432                 return;
433         else if (out[0] == TOS_NOT_OPENED)
434                 pr_info("Toshiba SCI not opened\n");
435         else if (out[0] == TOS_NOT_PRESENT)
436                 pr_info("Toshiba SCI is not present\n");
437 }
438
439 static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
440 {
441         u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
442         u32 out[TCI_WORDS];
443         acpi_status status = tci_raw(dev, in, out);
444
445         if (ACPI_FAILURE(status))
446                 return TOS_FAILURE;
447
448         *out1 = out[2];
449
450         return out[0];
451 }
452
453 static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
454 {
455         u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
456         u32 out[TCI_WORDS];
457         acpi_status status = tci_raw(dev, in, out);
458
459         return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
460 }
461
462 /* Illumination support */
463 static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
464 {
465         u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
466         u32 out[TCI_WORDS];
467         acpi_status status;
468
469         if (!sci_open(dev))
470                 return 0;
471
472         status = tci_raw(dev, in, out);
473         sci_close(dev);
474         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
475                 pr_err("ACPI call to query Illumination support failed\n");
476                 return 0;
477         } else if (out[0] == TOS_NOT_SUPPORTED) {
478                 pr_info("Illumination device not available\n");
479                 return 0;
480         }
481
482         return 1;
483 }
484
485 static void toshiba_illumination_set(struct led_classdev *cdev,
486                                      enum led_brightness brightness)
487 {
488         struct toshiba_acpi_dev *dev = container_of(cdev,
489                         struct toshiba_acpi_dev, led_dev);
490         u32 state, result;
491
492         /* First request : initialize communication. */
493         if (!sci_open(dev))
494                 return;
495
496         /* Switch the illumination on/off */
497         state = brightness ? 1 : 0;
498         result = sci_write(dev, SCI_ILLUMINATION, state);
499         sci_close(dev);
500         if (result == TOS_FAILURE) {
501                 pr_err("ACPI call for illumination failed\n");
502                 return;
503         } else if (result == TOS_NOT_SUPPORTED) {
504                 pr_info("Illumination not supported\n");
505                 return;
506         }
507 }
508
509 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
510 {
511         struct toshiba_acpi_dev *dev = container_of(cdev,
512                         struct toshiba_acpi_dev, led_dev);
513         u32 state, result;
514
515         /* First request : initialize communication. */
516         if (!sci_open(dev))
517                 return LED_OFF;
518
519         /* Check the illumination */
520         result = sci_read(dev, SCI_ILLUMINATION, &state);
521         sci_close(dev);
522         if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
523                 pr_err("ACPI call for illumination failed\n");
524                 return LED_OFF;
525         } else if (result == TOS_NOT_SUPPORTED) {
526                 pr_info("Illumination not supported\n");
527                 return LED_OFF;
528         }
529
530         return state ? LED_FULL : LED_OFF;
531 }
532
533 /* KBD Illumination */
534 static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
535 {
536         u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
537         u32 out[TCI_WORDS];
538         acpi_status status;
539
540         if (!sci_open(dev))
541                 return 0;
542
543         status = tci_raw(dev, in, out);
544         sci_close(dev);
545         if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
546                 pr_err("ACPI call to query kbd illumination support failed\n");
547                 return 0;
548         } else if (out[0] == TOS_NOT_SUPPORTED) {
549                 pr_info("Keyboard illumination not available\n");
550                 return 0;
551         }
552
553         /*
554          * Check for keyboard backlight timeout max value,
555          * previous kbd backlight implementation set this to
556          * 0x3c0003, and now the new implementation set this
557          * to 0x3c001a, use this to distinguish between them.
558          */
559         if (out[3] == SCI_KBD_TIME_MAX)
560                 dev->kbd_type = 2;
561         else
562                 dev->kbd_type = 1;
563         /* Get the current keyboard backlight mode */
564         dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
565         /* Get the current time (1-60 seconds) */
566         dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
567
568         return 1;
569 }
570
571 static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
572 {
573         u32 result;
574
575         if (!sci_open(dev))
576                 return -EIO;
577
578         result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
579         sci_close(dev);
580         if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
581                 pr_err("ACPI call to set KBD backlight status failed\n");
582                 return -EIO;
583         } else if (result == TOS_NOT_SUPPORTED) {
584                 pr_info("Keyboard backlight status not supported\n");
585                 return -ENODEV;
586         }
587
588         return 0;
589 }
590
591 static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
592 {
593         u32 result;
594
595         if (!sci_open(dev))
596                 return -EIO;
597
598         result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
599         sci_close(dev);
600         if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
601                 pr_err("ACPI call to get KBD backlight status failed\n");
602                 return -EIO;
603         } else if (result == TOS_NOT_SUPPORTED) {
604                 pr_info("Keyboard backlight status not supported\n");
605                 return -ENODEV;
606         }
607
608         return 0;
609 }
610
611 static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
612 {
613         struct toshiba_acpi_dev *dev = container_of(cdev,
614                         struct toshiba_acpi_dev, kbd_led);
615         u32 state, result;
616
617         /* Check the keyboard backlight state */
618         result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
619         if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
620                 pr_err("ACPI call to get the keyboard backlight failed\n");
621                 return LED_OFF;
622         } else if (result == TOS_NOT_SUPPORTED) {
623                 pr_info("Keyboard backlight not supported\n");
624                 return LED_OFF;
625         }
626
627         return state ? LED_FULL : LED_OFF;
628 }
629
630 static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
631                                      enum led_brightness brightness)
632 {
633         struct toshiba_acpi_dev *dev = container_of(cdev,
634                         struct toshiba_acpi_dev, kbd_led);
635         u32 state, result;
636
637         /* Set the keyboard backlight state */
638         state = brightness ? 1 : 0;
639         result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
640         if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
641                 pr_err("ACPI call to set KBD Illumination mode failed\n");
642                 return;
643         } else if (result == TOS_NOT_SUPPORTED) {
644                 pr_info("Keyboard backlight not supported\n");
645                 return;
646         }
647 }
648
649 /* TouchPad support */
650 static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
651 {
652         u32 result;
653
654         if (!sci_open(dev))
655                 return -EIO;
656
657         result = sci_write(dev, SCI_TOUCHPAD, state);
658         sci_close(dev);
659         if (result == TOS_FAILURE) {
660                 pr_err("ACPI call to set the touchpad failed\n");
661                 return -EIO;
662         } else if (result == TOS_NOT_SUPPORTED) {
663                 return -ENODEV;
664         }
665
666         return 0;
667 }
668
669 static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
670 {
671         u32 result;
672
673         if (!sci_open(dev))
674                 return -EIO;
675
676         result = sci_read(dev, SCI_TOUCHPAD, state);
677         sci_close(dev);
678         if (result == TOS_FAILURE) {
679                 pr_err("ACPI call to query the touchpad failed\n");
680                 return -EIO;
681         } else if (result == TOS_NOT_SUPPORTED) {
682                 return -ENODEV;
683         }
684
685         return 0;
686 }
687
688 /* Eco Mode support */
689 static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
690 {
691         acpi_status status;
692         u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
693         u32 out[TCI_WORDS];
694
695         status = tci_raw(dev, in, out);
696         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
697                 pr_err("ACPI call to get ECO led failed\n");
698         } else if (out[0] == TOS_NOT_INSTALLED) {
699                 pr_info("ECO led not installed");
700         } else if (out[0] == TOS_INPUT_DATA_ERROR) {
701                 /*
702                  * If we receive 0x8300 (Input Data Error), it means that the
703                  * LED device is present, but that we just screwed the input
704                  * parameters.
705                  *
706                  * Let's query the status of the LED to see if we really have a
707                  * success response, indicating the actual presense of the LED,
708                  * bail out otherwise.
709                  */
710                 in[3] = 1;
711                 status = tci_raw(dev, in, out);
712                 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
713                         pr_err("ACPI call to get ECO led failed\n");
714                 else if (out[0] == TOS_SUCCESS)
715                         return 1;
716         }
717
718         return 0;
719 }
720
721 static enum led_brightness
722 toshiba_eco_mode_get_status(struct led_classdev *cdev)
723 {
724         struct toshiba_acpi_dev *dev = container_of(cdev,
725                         struct toshiba_acpi_dev, eco_led);
726         u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
727         u32 out[TCI_WORDS];
728         acpi_status status;
729
730         status = tci_raw(dev, in, out);
731         if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
732                 pr_err("ACPI call to get ECO led failed\n");
733                 return LED_OFF;
734         }
735
736         return out[2] ? LED_FULL : LED_OFF;
737 }
738
739 static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
740                                      enum led_brightness brightness)
741 {
742         struct toshiba_acpi_dev *dev = container_of(cdev,
743                         struct toshiba_acpi_dev, eco_led);
744         u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
745         u32 out[TCI_WORDS];
746         acpi_status status;
747
748         /* Switch the Eco Mode led on/off */
749         in[2] = (brightness) ? 1 : 0;
750         status = tci_raw(dev, in, out);
751         if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
752                 pr_err("ACPI call to set ECO led failed\n");
753                 return;
754         }
755 }
756
757 /* Accelerometer support */
758 static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
759 {
760         u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
761         u32 out[TCI_WORDS];
762         acpi_status status;
763
764         /*
765          * Check if the accelerometer call exists,
766          * this call also serves as initialization
767          */
768         status = tci_raw(dev, in, out);
769         if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
770                 pr_err("ACPI call to query the accelerometer failed\n");
771                 return -EIO;
772         } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
773                    out[0] == TOS_NOT_INITIALIZED) {
774                 pr_err("Accelerometer not initialized\n");
775                 return -EIO;
776         } else if (out[0] == TOS_NOT_SUPPORTED) {
777                 pr_info("Accelerometer not supported\n");
778                 return -ENODEV;
779         }
780
781         return 0;
782 }
783
784 static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
785                                       u32 *xy, u32 *z)
786 {
787         u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
788         u32 out[TCI_WORDS];
789         acpi_status status;
790
791         /* Check the Accelerometer status */
792         status = tci_raw(dev, in, out);
793         if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
794                 pr_err("ACPI call to query the accelerometer failed\n");
795                 return -EIO;
796         }
797
798         *xy = out[2];
799         *z = out[4];
800
801         return 0;
802 }
803
804 /* Sleep (Charge and Music) utilities support */
805 static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
806 {
807         u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
808         u32 out[TCI_WORDS];
809         acpi_status status;
810
811         /* Set the feature to "not supported" in case of error */
812         dev->usb_sleep_charge_supported = 0;
813
814         if (!sci_open(dev))
815                 return;
816
817         status = tci_raw(dev, in, out);
818         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
819                 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
820                 sci_close(dev);
821                 return;
822         } else if (out[0] == TOS_NOT_SUPPORTED) {
823                 pr_info("USB Sleep and Charge not supported\n");
824                 sci_close(dev);
825                 return;
826         } else if (out[0] == TOS_SUCCESS) {
827                 dev->usbsc_mode_base = out[4];
828         }
829
830         in[5] = SCI_USB_CHARGE_BAT_LVL;
831         status = tci_raw(dev, in, out);
832         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
833                 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
834                 sci_close(dev);
835                 return;
836         } else if (out[0] == TOS_NOT_SUPPORTED) {
837                 pr_info("USB Sleep and Charge not supported\n");
838                 sci_close(dev);
839                 return;
840         } else if (out[0] == TOS_SUCCESS) {
841                 dev->usbsc_bat_level = out[2];
842                 /*
843                  * If we reach this point, it means that the laptop has support
844                  * for this feature and all values are initialized.
845                  * Set it as supported.
846                  */
847                 dev->usb_sleep_charge_supported = 1;
848         }
849
850         sci_close(dev);
851 }
852
853 static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
854                                         u32 *mode)
855 {
856         u32 result;
857
858         if (!sci_open(dev))
859                 return -EIO;
860
861         result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
862         sci_close(dev);
863         if (result == TOS_FAILURE) {
864                 pr_err("ACPI call to set USB S&C mode failed\n");
865                 return -EIO;
866         } else if (result == TOS_NOT_SUPPORTED) {
867                 pr_info("USB Sleep and Charge not supported\n");
868                 return -ENODEV;
869         } else if (result == TOS_INPUT_DATA_ERROR) {
870                 return -EIO;
871         }
872
873         return 0;
874 }
875
876 static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
877                                         u32 mode)
878 {
879         u32 result;
880
881         if (!sci_open(dev))
882                 return -EIO;
883
884         result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
885         sci_close(dev);
886         if (result == TOS_FAILURE) {
887                 pr_err("ACPI call to set USB S&C mode failed\n");
888                 return -EIO;
889         } else if (result == TOS_NOT_SUPPORTED) {
890                 pr_info("USB Sleep and Charge not supported\n");
891                 return -ENODEV;
892         } else if (result == TOS_INPUT_DATA_ERROR) {
893                 return -EIO;
894         }
895
896         return 0;
897 }
898
899 static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
900                                               u32 *mode)
901 {
902         u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
903         u32 out[TCI_WORDS];
904         acpi_status status;
905
906         if (!sci_open(dev))
907                 return -EIO;
908
909         in[5] = SCI_USB_CHARGE_BAT_LVL;
910         status = tci_raw(dev, in, out);
911         sci_close(dev);
912         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
913                 pr_err("ACPI call to get USB S&C battery level failed\n");
914                 return -EIO;
915         } else if (out[0] == TOS_NOT_SUPPORTED) {
916                 pr_info("USB Sleep and Charge not supported\n");
917                 return -ENODEV;
918         } else if (out[0] == TOS_INPUT_DATA_ERROR) {
919                 return -EIO;
920         }
921
922         *mode = out[2];
923
924         return 0;
925 }
926
927 static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
928                                               u32 mode)
929 {
930         u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
931         u32 out[TCI_WORDS];
932         acpi_status status;
933
934         if (!sci_open(dev))
935                 return -EIO;
936
937         in[2] = mode;
938         in[5] = SCI_USB_CHARGE_BAT_LVL;
939         status = tci_raw(dev, in, out);
940         sci_close(dev);
941         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
942                 pr_err("ACPI call to set USB S&C battery level failed\n");
943                 return -EIO;
944         } else if (out[0] == TOS_NOT_SUPPORTED) {
945                 pr_info("USB Sleep and Charge not supported\n");
946                 return -ENODEV;
947         } else if (out[0] == TOS_INPUT_DATA_ERROR) {
948                 return -EIO;
949         }
950
951         return 0;
952 }
953
954 static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
955                                         u32 *state)
956 {
957         u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
958         u32 out[TCI_WORDS];
959         acpi_status status;
960
961         if (!sci_open(dev))
962                 return -EIO;
963
964         in[5] = SCI_USB_CHARGE_RAPID_DSP;
965         status = tci_raw(dev, in, out);
966         sci_close(dev);
967         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
968                 pr_err("ACPI call to get USB Rapid Charge failed\n");
969                 return -EIO;
970         } else if (out[0] == TOS_NOT_SUPPORTED ||
971                    out[0] == TOS_INPUT_DATA_ERROR) {
972                 pr_info("USB Rapid Charge not supported\n");
973                 return -ENODEV;
974         }
975
976         *state = out[2];
977
978         return 0;
979 }
980
981 static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
982                                         u32 state)
983 {
984         u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
985         u32 out[TCI_WORDS];
986         acpi_status status;
987
988         if (!sci_open(dev))
989                 return -EIO;
990
991         in[2] = state;
992         in[5] = SCI_USB_CHARGE_RAPID_DSP;
993         status = tci_raw(dev, in, out);
994         sci_close(dev);
995         if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
996                 pr_err("ACPI call to set USB Rapid Charge failed\n");
997                 return -EIO;
998         } else if (out[0] == TOS_NOT_SUPPORTED) {
999                 pr_info("USB Rapid Charge not supported\n");
1000                 return -ENODEV;
1001         } else if (out[0] == TOS_INPUT_DATA_ERROR) {
1002                 return -EIO;
1003         }
1004
1005         return 0;
1006 }
1007
1008 static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
1009 {
1010         u32 result;
1011
1012         if (!sci_open(dev))
1013                 return -EIO;
1014
1015         result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
1016         sci_close(dev);
1017         if (result == TOS_FAILURE) {
1018                 pr_err("ACPI call to get Sleep and Music failed\n");
1019                 return -EIO;
1020         } else if (result == TOS_NOT_SUPPORTED) {
1021                 pr_info("Sleep and Music not supported\n");
1022                 return -ENODEV;
1023         } else if (result == TOS_INPUT_DATA_ERROR) {
1024                 return -EIO;
1025         }
1026
1027         return 0;
1028 }
1029
1030 static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1031 {
1032         u32 result;
1033
1034         if (!sci_open(dev))
1035                 return -EIO;
1036
1037         result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1038         sci_close(dev);
1039         if (result == TOS_FAILURE) {
1040                 pr_err("ACPI call to set Sleep and Music failed\n");
1041                 return -EIO;
1042         } else if (result == TOS_NOT_SUPPORTED) {
1043                 pr_info("Sleep and Music not supported\n");
1044                 return -ENODEV;
1045         } else if (result == TOS_INPUT_DATA_ERROR) {
1046                 return -EIO;
1047         }
1048
1049         return 0;
1050 }
1051
1052 /* Keyboard function keys */
1053 static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1054 {
1055         u32 result;
1056
1057         if (!sci_open(dev))
1058                 return -EIO;
1059
1060         result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1061         sci_close(dev);
1062         if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1063                 pr_err("ACPI call to get KBD function keys failed\n");
1064                 return -EIO;
1065         } else if (result == TOS_NOT_SUPPORTED) {
1066                 pr_info("KBD function keys not supported\n");
1067                 return -ENODEV;
1068         }
1069
1070         return 0;
1071 }
1072
1073 static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1074 {
1075         u32 result;
1076
1077         if (!sci_open(dev))
1078                 return -EIO;
1079
1080         result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1081         sci_close(dev);
1082         if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1083                 pr_err("ACPI call to set KBD function keys failed\n");
1084                 return -EIO;
1085         } else if (result == TOS_NOT_SUPPORTED) {
1086                 pr_info("KBD function keys not supported\n");
1087                 return -ENODEV;
1088         }
1089
1090         return 0;
1091 }
1092
1093 /* Panel Power ON */
1094 static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1095 {
1096         u32 result;
1097
1098         if (!sci_open(dev))
1099                 return -EIO;
1100
1101         result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1102         sci_close(dev);
1103         if (result == TOS_FAILURE) {
1104                 pr_err("ACPI call to get Panel Power ON failed\n");
1105                 return -EIO;
1106         } else if (result == TOS_NOT_SUPPORTED) {
1107                 pr_info("Panel Power on not supported\n");
1108                 return -ENODEV;
1109         } else if (result == TOS_INPUT_DATA_ERROR) {
1110                 return -EIO;
1111         }
1112
1113         return 0;
1114 }
1115
1116 static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1117 {
1118         u32 result;
1119
1120         if (!sci_open(dev))
1121                 return -EIO;
1122
1123         result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1124         sci_close(dev);
1125         if (result == TOS_FAILURE) {
1126                 pr_err("ACPI call to set Panel Power ON failed\n");
1127                 return -EIO;
1128         } else if (result == TOS_NOT_SUPPORTED) {
1129                 pr_info("Panel Power ON not supported\n");
1130                 return -ENODEV;
1131         } else if (result == TOS_INPUT_DATA_ERROR) {
1132                 return -EIO;
1133         }
1134
1135         return 0;
1136 }
1137
1138 /* USB Three */
1139 static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1140 {
1141         u32 result;
1142
1143         if (!sci_open(dev))
1144                 return -EIO;
1145
1146         result = sci_read(dev, SCI_USB_THREE, state);
1147         sci_close(dev);
1148         if (result == TOS_FAILURE) {
1149                 pr_err("ACPI call to get USB 3 failed\n");
1150                 return -EIO;
1151         } else if (result == TOS_NOT_SUPPORTED) {
1152                 pr_info("USB 3 not supported\n");
1153                 return -ENODEV;
1154         } else if (result == TOS_INPUT_DATA_ERROR) {
1155                 return -EIO;
1156         }
1157
1158         return 0;
1159 }
1160
1161 static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1162 {
1163         u32 result;
1164
1165         if (!sci_open(dev))
1166                 return -EIO;
1167
1168         result = sci_write(dev, SCI_USB_THREE, state);
1169         sci_close(dev);
1170         if (result == TOS_FAILURE) {
1171                 pr_err("ACPI call to set USB 3 failed\n");
1172                 return -EIO;
1173         } else if (result == TOS_NOT_SUPPORTED) {
1174                 pr_info("USB 3 not supported\n");
1175                 return -ENODEV;
1176         } else if (result == TOS_INPUT_DATA_ERROR) {
1177                 return -EIO;
1178         }
1179
1180         return 0;
1181 }
1182
1183 /* Hotkey Event type */
1184 static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1185                                          u32 *type)
1186 {
1187         u32 val1 = 0x03;
1188         u32 val2 = 0;
1189         u32 result;
1190
1191         result = hci_read2(dev, HCI_SYSTEM_INFO, &val1, &val2);
1192         if (result == TOS_FAILURE) {
1193                 pr_err("ACPI call to get System type failed\n");
1194                 return -EIO;
1195         } else if (result == TOS_NOT_SUPPORTED) {
1196                 pr_info("System type not supported\n");
1197                 return -ENODEV;
1198         }
1199
1200         *type = val2;
1201
1202         return 0;
1203 }
1204
1205 /* Bluetooth rfkill handlers */
1206
1207 static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
1208 {
1209         u32 hci_result;
1210         u32 value, value2;
1211
1212         value = 0;
1213         value2 = 0;
1214         hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
1215         if (hci_result == TOS_SUCCESS)
1216                 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
1217
1218         return hci_result;
1219 }
1220
1221 static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
1222 {
1223         u32 hci_result;
1224         u32 value, value2;
1225
1226         value = 0;
1227         value2 = 0x0001;
1228         hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
1229
1230         *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
1231         return hci_result;
1232 }
1233
1234 static int bt_rfkill_set_block(void *data, bool blocked)
1235 {
1236         struct toshiba_acpi_dev *dev = data;
1237         u32 result1, result2;
1238         u32 value;
1239         int err;
1240         bool radio_state;
1241
1242         value = (blocked == false);
1243
1244         mutex_lock(&dev->mutex);
1245         if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
1246                 err = -EIO;
1247                 goto out;
1248         }
1249
1250         if (!radio_state) {
1251                 err = 0;
1252                 goto out;
1253         }
1254
1255         result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
1256         result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
1257
1258         if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
1259                 err = -EIO;
1260         else
1261                 err = 0;
1262  out:
1263         mutex_unlock(&dev->mutex);
1264         return err;
1265 }
1266
1267 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
1268 {
1269         bool new_rfk_state;
1270         bool value;
1271         u32 hci_result;
1272         struct toshiba_acpi_dev *dev = data;
1273
1274         mutex_lock(&dev->mutex);
1275
1276         hci_result = hci_get_radio_state(dev, &value);
1277         if (hci_result != TOS_SUCCESS) {
1278                 /* Can't do anything useful */
1279                 mutex_unlock(&dev->mutex);
1280                 return;
1281         }
1282
1283         new_rfk_state = value;
1284
1285         mutex_unlock(&dev->mutex);
1286
1287         if (rfkill_set_hw_state(rfkill, !new_rfk_state))
1288                 bt_rfkill_set_block(data, true);
1289 }
1290
1291 static const struct rfkill_ops toshiba_rfk_ops = {
1292         .set_block = bt_rfkill_set_block,
1293         .poll = bt_rfkill_poll,
1294 };
1295
1296 static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1297 {
1298         u32 hci_result;
1299         u32 status;
1300
1301         hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
1302         *enabled = !status;
1303         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1304 }
1305
1306 static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1307 {
1308         u32 hci_result;
1309         u32 value = !enable;
1310
1311         hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
1312         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1313 }
1314
1315 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
1316
1317 static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
1318 {
1319         u32 hci_result;
1320         u32 value;
1321         int brightness = 0;
1322
1323         if (dev->tr_backlight_supported) {
1324                 bool enabled;
1325                 int ret = get_tr_backlight_status(dev, &enabled);
1326
1327                 if (ret)
1328                         return ret;
1329                 if (enabled)
1330                         return 0;
1331                 brightness++;
1332         }
1333
1334         hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
1335         if (hci_result == TOS_SUCCESS)
1336                 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
1337
1338         return -EIO;
1339 }
1340
1341 static int get_lcd_brightness(struct backlight_device *bd)
1342 {
1343         struct toshiba_acpi_dev *dev = bl_get_data(bd);
1344
1345         return __get_lcd_brightness(dev);
1346 }
1347
1348 static int lcd_proc_show(struct seq_file *m, void *v)
1349 {
1350         struct toshiba_acpi_dev *dev = m->private;
1351         int value;
1352         int levels;
1353
1354         if (!dev->backlight_dev)
1355                 return -ENODEV;
1356
1357         levels = dev->backlight_dev->props.max_brightness + 1;
1358         value = get_lcd_brightness(dev->backlight_dev);
1359         if (value >= 0) {
1360                 seq_printf(m, "brightness:              %d\n", value);
1361                 seq_printf(m, "brightness_levels:       %d\n", levels);
1362                 return 0;
1363         }
1364
1365         pr_err("Error reading LCD brightness\n");
1366         return -EIO;
1367 }
1368
1369 static int lcd_proc_open(struct inode *inode, struct file *file)
1370 {
1371         return single_open(file, lcd_proc_show, PDE_DATA(inode));
1372 }
1373
1374 static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
1375 {
1376         u32 hci_result;
1377
1378         if (dev->tr_backlight_supported) {
1379                 bool enable = !value;
1380                 int ret = set_tr_backlight_status(dev, enable);
1381
1382                 if (ret)
1383                         return ret;
1384                 if (value)
1385                         value--;
1386         }
1387
1388         value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1389         hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1390         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1391 }
1392
1393 static int set_lcd_status(struct backlight_device *bd)
1394 {
1395         struct toshiba_acpi_dev *dev = bl_get_data(bd);
1396
1397         return set_lcd_brightness(dev, bd->props.brightness);
1398 }
1399
1400 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1401                               size_t count, loff_t *pos)
1402 {
1403         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1404         char cmd[42];
1405         size_t len;
1406         int value;
1407         int ret;
1408         int levels = dev->backlight_dev->props.max_brightness + 1;
1409
1410         len = min(count, sizeof(cmd) - 1);
1411         if (copy_from_user(cmd, buf, len))
1412                 return -EFAULT;
1413         cmd[len] = '\0';
1414
1415         if (sscanf(cmd, " brightness : %i", &value) == 1 &&
1416             value >= 0 && value < levels) {
1417                 ret = set_lcd_brightness(dev, value);
1418                 if (ret == 0)
1419                         ret = count;
1420         } else {
1421                 ret = -EINVAL;
1422         }
1423         return ret;
1424 }
1425
1426 static const struct file_operations lcd_proc_fops = {
1427         .owner          = THIS_MODULE,
1428         .open           = lcd_proc_open,
1429         .read           = seq_read,
1430         .llseek         = seq_lseek,
1431         .release        = single_release,
1432         .write          = lcd_proc_write,
1433 };
1434
1435 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1436 {
1437         u32 hci_result;
1438
1439         hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
1440         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1441 }
1442
1443 static int video_proc_show(struct seq_file *m, void *v)
1444 {
1445         struct toshiba_acpi_dev *dev = m->private;
1446         u32 value;
1447         int ret;
1448
1449         ret = get_video_status(dev, &value);
1450         if (!ret) {
1451                 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1452                 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
1453                 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
1454
1455                 seq_printf(m, "lcd_out:                 %d\n", is_lcd);
1456                 seq_printf(m, "crt_out:                 %d\n", is_crt);
1457                 seq_printf(m, "tv_out:                  %d\n", is_tv);
1458         }
1459
1460         return ret;
1461 }
1462
1463 static int video_proc_open(struct inode *inode, struct file *file)
1464 {
1465         return single_open(file, video_proc_show, PDE_DATA(inode));
1466 }
1467
1468 static ssize_t video_proc_write(struct file *file, const char __user *buf,
1469                                 size_t count, loff_t *pos)
1470 {
1471         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1472         char *cmd, *buffer;
1473         int ret;
1474         int value;
1475         int remain = count;
1476         int lcd_out = -1;
1477         int crt_out = -1;
1478         int tv_out = -1;
1479         u32 video_out;
1480
1481         cmd = kmalloc(count + 1, GFP_KERNEL);
1482         if (!cmd)
1483                 return -ENOMEM;
1484         if (copy_from_user(cmd, buf, count)) {
1485                 kfree(cmd);
1486                 return -EFAULT;
1487         }
1488         cmd[count] = '\0';
1489
1490         buffer = cmd;
1491
1492         /*
1493          * Scan expression.  Multiple expressions may be delimited with ;
1494          * NOTE: To keep scanning simple, invalid fields are ignored.
1495          */
1496         while (remain) {
1497                 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1498                         lcd_out = value & 1;
1499                 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1500                         crt_out = value & 1;
1501                 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1502                         tv_out = value & 1;
1503                 /* Advance to one character past the next ; */
1504                 do {
1505                         ++buffer;
1506                         --remain;
1507                 } while (remain && *(buffer - 1) != ';');
1508         }
1509
1510         kfree(cmd);
1511
1512         ret = get_video_status(dev, &video_out);
1513         if (!ret) {
1514                 unsigned int new_video_out = video_out;
1515
1516                 if (lcd_out != -1)
1517                         _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1518                 if (crt_out != -1)
1519                         _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1520                 if (tv_out != -1)
1521                         _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1522                 /*
1523                  * To avoid unnecessary video disruption, only write the new
1524                  * video setting if something changed. */
1525                 if (new_video_out != video_out)
1526                         ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1527         }
1528
1529         return ret ? ret : count;
1530 }
1531
1532 static const struct file_operations video_proc_fops = {
1533         .owner          = THIS_MODULE,
1534         .open           = video_proc_open,
1535         .read           = seq_read,
1536         .llseek         = seq_lseek,
1537         .release        = single_release,
1538         .write          = video_proc_write,
1539 };
1540
1541 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1542 {
1543         u32 hci_result;
1544
1545         hci_result = hci_read1(dev, HCI_FAN, status);
1546         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1547 }
1548
1549 static int fan_proc_show(struct seq_file *m, void *v)
1550 {
1551         struct toshiba_acpi_dev *dev = m->private;
1552         int ret;
1553         u32 value;
1554
1555         ret = get_fan_status(dev, &value);
1556         if (!ret) {
1557                 seq_printf(m, "running:                 %d\n", (value > 0));
1558                 seq_printf(m, "force_on:                %d\n", dev->force_fan);
1559         }
1560
1561         return ret;
1562 }
1563
1564 static int fan_proc_open(struct inode *inode, struct file *file)
1565 {
1566         return single_open(file, fan_proc_show, PDE_DATA(inode));
1567 }
1568
1569 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1570                               size_t count, loff_t *pos)
1571 {
1572         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1573         char cmd[42];
1574         size_t len;
1575         int value;
1576         u32 hci_result;
1577
1578         len = min(count, sizeof(cmd) - 1);
1579         if (copy_from_user(cmd, buf, len))
1580                 return -EFAULT;
1581         cmd[len] = '\0';
1582
1583         if (sscanf(cmd, " force_on : %i", &value) == 1 &&
1584             value >= 0 && value <= 1) {
1585                 hci_result = hci_write1(dev, HCI_FAN, value);
1586                 if (hci_result == TOS_SUCCESS)
1587                         dev->force_fan = value;
1588                 else
1589                         return -EIO;
1590         } else {
1591                 return -EINVAL;
1592         }
1593
1594         return count;
1595 }
1596
1597 static const struct file_operations fan_proc_fops = {
1598         .owner          = THIS_MODULE,
1599         .open           = fan_proc_open,
1600         .read           = seq_read,
1601         .llseek         = seq_lseek,
1602         .release        = single_release,
1603         .write          = fan_proc_write,
1604 };
1605
1606 static int keys_proc_show(struct seq_file *m, void *v)
1607 {
1608         struct toshiba_acpi_dev *dev = m->private;
1609         u32 hci_result;
1610         u32 value;
1611
1612         if (!dev->key_event_valid && dev->system_event_supported) {
1613                 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
1614                 if (hci_result == TOS_SUCCESS) {
1615                         dev->key_event_valid = 1;
1616                         dev->last_key_event = value;
1617                 } else if (hci_result == TOS_FIFO_EMPTY) {
1618                         /* Better luck next time */
1619                 } else if (hci_result == TOS_NOT_SUPPORTED) {
1620                         /*
1621                          * This is a workaround for an unresolved issue on
1622                          * some machines where system events sporadically
1623                          * become disabled.
1624                          */
1625                         hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
1626                         pr_notice("Re-enabled hotkeys\n");
1627                 } else {
1628                         pr_err("Error reading hotkey status\n");
1629                         return -EIO;
1630                 }
1631         }
1632
1633         seq_printf(m, "hotkey_ready:            %d\n", dev->key_event_valid);
1634         seq_printf(m, "hotkey:                  0x%04x\n", dev->last_key_event);
1635         return 0;
1636 }
1637
1638 static int keys_proc_open(struct inode *inode, struct file *file)
1639 {
1640         return single_open(file, keys_proc_show, PDE_DATA(inode));
1641 }
1642
1643 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1644                                size_t count, loff_t *pos)
1645 {
1646         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1647         char cmd[42];
1648         size_t len;
1649         int value;
1650
1651         len = min(count, sizeof(cmd) - 1);
1652         if (copy_from_user(cmd, buf, len))
1653                 return -EFAULT;
1654         cmd[len] = '\0';
1655
1656         if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
1657                 dev->key_event_valid = 0;
1658         else
1659                 return -EINVAL;
1660
1661         return count;
1662 }
1663
1664 static const struct file_operations keys_proc_fops = {
1665         .owner          = THIS_MODULE,
1666         .open           = keys_proc_open,
1667         .read           = seq_read,
1668         .llseek         = seq_lseek,
1669         .release        = single_release,
1670         .write          = keys_proc_write,
1671 };
1672
1673 static int version_proc_show(struct seq_file *m, void *v)
1674 {
1675         seq_printf(m, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
1676         seq_printf(m, "proc_interface:          %d\n", PROC_INTERFACE_VERSION);
1677         return 0;
1678 }
1679
1680 static int version_proc_open(struct inode *inode, struct file *file)
1681 {
1682         return single_open(file, version_proc_show, PDE_DATA(inode));
1683 }
1684
1685 static const struct file_operations version_proc_fops = {
1686         .owner          = THIS_MODULE,
1687         .open           = version_proc_open,
1688         .read           = seq_read,
1689         .llseek         = seq_lseek,
1690         .release        = single_release,
1691 };
1692
1693 /*
1694  * Proc and module init
1695  */
1696
1697 #define PROC_TOSHIBA            "toshiba"
1698
1699 static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1700 {
1701         if (dev->backlight_dev)
1702                 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1703                                  &lcd_proc_fops, dev);
1704         if (dev->video_supported)
1705                 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1706                                  &video_proc_fops, dev);
1707         if (dev->fan_supported)
1708                 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1709                                  &fan_proc_fops, dev);
1710         if (dev->hotkey_dev)
1711                 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1712                                  &keys_proc_fops, dev);
1713         proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1714                          &version_proc_fops, dev);
1715 }
1716
1717 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1718 {
1719         if (dev->backlight_dev)
1720                 remove_proc_entry("lcd", toshiba_proc_dir);
1721         if (dev->video_supported)
1722                 remove_proc_entry("video", toshiba_proc_dir);
1723         if (dev->fan_supported)
1724                 remove_proc_entry("fan", toshiba_proc_dir);
1725         if (dev->hotkey_dev)
1726                 remove_proc_entry("keys", toshiba_proc_dir);
1727         remove_proc_entry("version", toshiba_proc_dir);
1728 }
1729
1730 static const struct backlight_ops toshiba_backlight_data = {
1731         .options = BL_CORE_SUSPENDRESUME,
1732         .get_brightness = get_lcd_brightness,
1733         .update_status  = set_lcd_status,
1734 };
1735
1736 /*
1737  * Sysfs files
1738  */
1739 static ssize_t version_show(struct device *dev,
1740                             struct device_attribute *attr, char *buf)
1741 {
1742         return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1743 }
1744 static DEVICE_ATTR_RO(version);
1745
1746 static ssize_t fan_store(struct device *dev,
1747                          struct device_attribute *attr,
1748                          const char *buf, size_t count)
1749 {
1750         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1751         u32 result;
1752         int state;
1753         int ret;
1754
1755         ret = kstrtoint(buf, 0, &state);
1756         if (ret)
1757                 return ret;
1758
1759         if (state != 0 && state != 1)
1760                 return -EINVAL;
1761
1762         result = hci_write1(toshiba, HCI_FAN, state);
1763         if (result == TOS_FAILURE)
1764                 return -EIO;
1765         else if (result == TOS_NOT_SUPPORTED)
1766                 return -ENODEV;
1767
1768         return count;
1769 }
1770
1771 static ssize_t fan_show(struct device *dev,
1772                         struct device_attribute *attr, char *buf)
1773 {
1774         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1775         u32 value;
1776         int ret;
1777
1778         ret = get_fan_status(toshiba, &value);
1779         if (ret)
1780                 return ret;
1781
1782         return sprintf(buf, "%d\n", value);
1783 }
1784 static DEVICE_ATTR_RW(fan);
1785
1786 static ssize_t kbd_backlight_mode_store(struct device *dev,
1787                                         struct device_attribute *attr,
1788                                         const char *buf, size_t count)
1789 {
1790         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1791         int mode;
1792         int time;
1793         int ret;
1794
1795
1796         ret = kstrtoint(buf, 0, &mode);
1797         if (ret)
1798                 return ret;
1799
1800         /* Check for supported modes depending on keyboard backlight type */
1801         if (toshiba->kbd_type == 1) {
1802                 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1803                 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1804                         return -EINVAL;
1805         } else if (toshiba->kbd_type == 2) {
1806                 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1807                 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1808                     mode != SCI_KBD_MODE_OFF)
1809                         return -EINVAL;
1810         }
1811
1812         /*
1813          * Set the Keyboard Backlight Mode where:
1814          *      Auto - KBD backlight turns off automatically in given time
1815          *      FN-Z - KBD backlight "toggles" when hotkey pressed
1816          *      ON   - KBD backlight is always on
1817          *      OFF  - KBD backlight is always off
1818          */
1819
1820         /* Only make a change if the actual mode has changed */
1821         if (toshiba->kbd_mode != mode) {
1822                 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1823                 time = toshiba->kbd_time << HCI_MISC_SHIFT;
1824
1825                 /* OR the "base time" to the actual method format */
1826                 if (toshiba->kbd_type == 1) {
1827                         /* Type 1 requires the current mode */
1828                         time |= toshiba->kbd_mode;
1829                 } else if (toshiba->kbd_type == 2) {
1830                         /* Type 2 requires the desired mode */
1831                         time |= mode;
1832                 }
1833
1834                 ret = toshiba_kbd_illum_status_set(toshiba, time);
1835                 if (ret)
1836                         return ret;
1837
1838                 toshiba->kbd_mode = mode;
1839         }
1840
1841         return count;
1842 }
1843
1844 static ssize_t kbd_backlight_mode_show(struct device *dev,
1845                                        struct device_attribute *attr,
1846                                        char *buf)
1847 {
1848         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1849         u32 time;
1850
1851         if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1852                 return -EIO;
1853
1854         return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1855 }
1856 static DEVICE_ATTR_RW(kbd_backlight_mode);
1857
1858 static ssize_t kbd_type_show(struct device *dev,
1859                              struct device_attribute *attr, char *buf)
1860 {
1861         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1862
1863         return sprintf(buf, "%d\n", toshiba->kbd_type);
1864 }
1865 static DEVICE_ATTR_RO(kbd_type);
1866
1867 static ssize_t available_kbd_modes_show(struct device *dev,
1868                                         struct device_attribute *attr,
1869                                         char *buf)
1870 {
1871         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1872
1873         if (toshiba->kbd_type == 1)
1874                 return sprintf(buf, "%x %x\n",
1875                                SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1876
1877         return sprintf(buf, "%x %x %x\n",
1878                        SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
1879 }
1880 static DEVICE_ATTR_RO(available_kbd_modes);
1881
1882 static ssize_t kbd_backlight_timeout_store(struct device *dev,
1883                                            struct device_attribute *attr,
1884                                            const char *buf, size_t count)
1885 {
1886         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1887         int time;
1888         int ret;
1889
1890         ret = kstrtoint(buf, 0, &time);
1891         if (ret)
1892                 return ret;
1893
1894         /* Check for supported values depending on kbd_type */
1895         if (toshiba->kbd_type == 1) {
1896                 if (time < 0 || time > 60)
1897                         return -EINVAL;
1898         } else if (toshiba->kbd_type == 2) {
1899                 if (time < 1 || time > 60)
1900                         return -EINVAL;
1901         }
1902
1903         /* Set the Keyboard Backlight Timeout */
1904
1905         /* Only make a change if the actual timeout has changed */
1906         if (toshiba->kbd_time != time) {
1907                 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1908                 time = time << HCI_MISC_SHIFT;
1909                 /* OR the "base time" to the actual method format */
1910                 if (toshiba->kbd_type == 1)
1911                         time |= SCI_KBD_MODE_FNZ;
1912                 else if (toshiba->kbd_type == 2)
1913                         time |= SCI_KBD_MODE_AUTO;
1914
1915                 ret = toshiba_kbd_illum_status_set(toshiba, time);
1916                 if (ret)
1917                         return ret;
1918
1919                 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1920         }
1921
1922         return count;
1923 }
1924
1925 static ssize_t kbd_backlight_timeout_show(struct device *dev,
1926                                           struct device_attribute *attr,
1927                                           char *buf)
1928 {
1929         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1930         u32 time;
1931
1932         if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1933                 return -EIO;
1934
1935         return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1936 }
1937 static DEVICE_ATTR_RW(kbd_backlight_timeout);
1938
1939 static ssize_t touchpad_store(struct device *dev,
1940                               struct device_attribute *attr,
1941                               const char *buf, size_t count)
1942 {
1943         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1944         int state;
1945         int ret;
1946
1947         /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1948         ret = kstrtoint(buf, 0, &state);
1949         if (ret)
1950                 return ret;
1951         if (state != 0 && state != 1)
1952                 return -EINVAL;
1953
1954         ret = toshiba_touchpad_set(toshiba, state);
1955         if (ret)
1956                 return ret;
1957
1958         return count;
1959 }
1960
1961 static ssize_t touchpad_show(struct device *dev,
1962                              struct device_attribute *attr, char *buf)
1963 {
1964         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1965         u32 state;
1966         int ret;
1967
1968         ret = toshiba_touchpad_get(toshiba, &state);
1969         if (ret < 0)
1970                 return ret;
1971
1972         return sprintf(buf, "%i\n", state);
1973 }
1974 static DEVICE_ATTR_RW(touchpad);
1975
1976 static ssize_t position_show(struct device *dev,
1977                              struct device_attribute *attr, char *buf)
1978 {
1979         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1980         u32 xyval, zval, tmp;
1981         u16 x, y, z;
1982         int ret;
1983
1984         xyval = zval = 0;
1985         ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1986         if (ret < 0)
1987                 return ret;
1988
1989         x = xyval & HCI_ACCEL_MASK;
1990         tmp = xyval >> HCI_MISC_SHIFT;
1991         y = tmp & HCI_ACCEL_MASK;
1992         z = zval & HCI_ACCEL_MASK;
1993
1994         return sprintf(buf, "%d %d %d\n", x, y, z);
1995 }
1996 static DEVICE_ATTR_RO(position);
1997
1998 static ssize_t usb_sleep_charge_show(struct device *dev,
1999                                      struct device_attribute *attr, char *buf)
2000 {
2001         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2002         u32 mode;
2003         int ret;
2004
2005         ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
2006         if (ret < 0)
2007                 return ret;
2008
2009         return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
2010 }
2011
2012 static ssize_t usb_sleep_charge_store(struct device *dev,
2013                                       struct device_attribute *attr,
2014                                       const char *buf, size_t count)
2015 {
2016         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2017         u32 mode;
2018         int state;
2019         int ret;
2020
2021         ret = kstrtoint(buf, 0, &state);
2022         if (ret)
2023                 return ret;
2024         /*
2025          * Check for supported values, where:
2026          * 0 - Disabled
2027          * 1 - Alternate (Non USB conformant devices that require more power)
2028          * 2 - Auto (USB conformant devices)
2029          * 3 - Typical
2030          */
2031         if (state != 0 && state != 1 && state != 2 && state != 3)
2032                 return -EINVAL;
2033
2034         /* Set the USB charging mode to internal value */
2035         mode = toshiba->usbsc_mode_base;
2036         if (state == 0)
2037                 mode |= SCI_USB_CHARGE_DISABLED;
2038         else if (state == 1)
2039                 mode |= SCI_USB_CHARGE_ALTERNATE;
2040         else if (state == 2)
2041                 mode |= SCI_USB_CHARGE_AUTO;
2042         else if (state == 3)
2043                 mode |= SCI_USB_CHARGE_TYPICAL;
2044
2045         ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2046         if (ret)
2047                 return ret;
2048
2049         return count;
2050 }
2051 static DEVICE_ATTR_RW(usb_sleep_charge);
2052
2053 static ssize_t sleep_functions_on_battery_show(struct device *dev,
2054                                                struct device_attribute *attr,
2055                                                char *buf)
2056 {
2057         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2058         u32 state;
2059         int bat_lvl;
2060         int status;
2061         int ret;
2062         int tmp;
2063
2064         ret = toshiba_sleep_functions_status_get(toshiba, &state);
2065         if (ret < 0)
2066                 return ret;
2067
2068         /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2069         tmp = state & SCI_USB_CHARGE_BAT_MASK;
2070         status = (tmp == 0x4) ? 1 : 0;
2071         /* Determine the battery level set */
2072         bat_lvl = state >> HCI_MISC_SHIFT;
2073
2074         return sprintf(buf, "%d %d\n", status, bat_lvl);
2075 }
2076
2077 static ssize_t sleep_functions_on_battery_store(struct device *dev,
2078                                                 struct device_attribute *attr,
2079                                                 const char *buf, size_t count)
2080 {
2081         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2082         u32 status;
2083         int value;
2084         int ret;
2085         int tmp;
2086
2087         ret = kstrtoint(buf, 0, &value);
2088         if (ret)
2089                 return ret;
2090
2091         /*
2092          * Set the status of the function:
2093          * 0 - Disabled
2094          * 1-100 - Enabled
2095          */
2096         if (value < 0 || value > 100)
2097                 return -EINVAL;
2098
2099         if (value == 0) {
2100                 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2101                 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2102         } else {
2103                 tmp = value << HCI_MISC_SHIFT;
2104                 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2105         }
2106         ret = toshiba_sleep_functions_status_set(toshiba, status);
2107         if (ret < 0)
2108                 return ret;
2109
2110         toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2111
2112         return count;
2113 }
2114 static DEVICE_ATTR_RW(sleep_functions_on_battery);
2115
2116 static ssize_t usb_rapid_charge_show(struct device *dev,
2117                                      struct device_attribute *attr, char *buf)
2118 {
2119         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2120         u32 state;
2121         int ret;
2122
2123         ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2124         if (ret < 0)
2125                 return ret;
2126
2127         return sprintf(buf, "%d\n", state);
2128 }
2129
2130 static ssize_t usb_rapid_charge_store(struct device *dev,
2131                                       struct device_attribute *attr,
2132                                       const char *buf, size_t count)
2133 {
2134         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2135         int state;
2136         int ret;
2137
2138         ret = kstrtoint(buf, 0, &state);
2139         if (ret)
2140                 return ret;
2141         if (state != 0 && state != 1)
2142                 return -EINVAL;
2143
2144         ret = toshiba_usb_rapid_charge_set(toshiba, state);
2145         if (ret)
2146                 return ret;
2147
2148         return count;
2149 }
2150 static DEVICE_ATTR_RW(usb_rapid_charge);
2151
2152 static ssize_t usb_sleep_music_show(struct device *dev,
2153                                     struct device_attribute *attr, char *buf)
2154 {
2155         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2156         u32 state;
2157         int ret;
2158
2159         ret = toshiba_usb_sleep_music_get(toshiba, &state);
2160         if (ret < 0)
2161                 return ret;
2162
2163         return sprintf(buf, "%d\n", state);
2164 }
2165
2166 static ssize_t usb_sleep_music_store(struct device *dev,
2167                                      struct device_attribute *attr,
2168                                      const char *buf, size_t count)
2169 {
2170         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2171         int state;
2172         int ret;
2173
2174         ret = kstrtoint(buf, 0, &state);
2175         if (ret)
2176                 return ret;
2177         if (state != 0 && state != 1)
2178                 return -EINVAL;
2179
2180         ret = toshiba_usb_sleep_music_set(toshiba, state);
2181         if (ret)
2182                 return ret;
2183
2184         return count;
2185 }
2186 static DEVICE_ATTR_RW(usb_sleep_music);
2187
2188 static ssize_t kbd_function_keys_show(struct device *dev,
2189                                       struct device_attribute *attr, char *buf)
2190 {
2191         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2192         int mode;
2193         int ret;
2194
2195         ret = toshiba_function_keys_get(toshiba, &mode);
2196         if (ret < 0)
2197                 return ret;
2198
2199         return sprintf(buf, "%d\n", mode);
2200 }
2201
2202 static ssize_t kbd_function_keys_store(struct device *dev,
2203                                        struct device_attribute *attr,
2204                                        const char *buf, size_t count)
2205 {
2206         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2207         int mode;
2208         int ret;
2209
2210         ret = kstrtoint(buf, 0, &mode);
2211         if (ret)
2212                 return ret;
2213         /*
2214          * Check for the function keys mode where:
2215          * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2216          * 1 - Special functions (Opposite of the above setting)
2217          */
2218         if (mode != 0 && mode != 1)
2219                 return -EINVAL;
2220
2221         ret = toshiba_function_keys_set(toshiba, mode);
2222         if (ret)
2223                 return ret;
2224
2225         pr_info("Reboot for changes to KBD Function Keys to take effect");
2226
2227         return count;
2228 }
2229 static DEVICE_ATTR_RW(kbd_function_keys);
2230
2231 static ssize_t panel_power_on_show(struct device *dev,
2232                                    struct device_attribute *attr, char *buf)
2233 {
2234         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2235         u32 state;
2236         int ret;
2237
2238         ret = toshiba_panel_power_on_get(toshiba, &state);
2239         if (ret < 0)
2240                 return ret;
2241
2242         return sprintf(buf, "%d\n", state);
2243 }
2244
2245 static ssize_t panel_power_on_store(struct device *dev,
2246                                     struct device_attribute *attr,
2247                                     const char *buf, size_t count)
2248 {
2249         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2250         int state;
2251         int ret;
2252
2253         ret = kstrtoint(buf, 0, &state);
2254         if (ret)
2255                 return ret;
2256         if (state != 0 && state != 1)
2257                 return -EINVAL;
2258
2259         ret = toshiba_panel_power_on_set(toshiba, state);
2260         if (ret)
2261                 return ret;
2262
2263         pr_info("Reboot for changes to Panel Power ON to take effect");
2264
2265         return count;
2266 }
2267 static DEVICE_ATTR_RW(panel_power_on);
2268
2269 static ssize_t usb_three_show(struct device *dev,
2270                               struct device_attribute *attr, char *buf)
2271 {
2272         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2273         u32 state;
2274         int ret;
2275
2276         ret = toshiba_usb_three_get(toshiba, &state);
2277         if (ret < 0)
2278                 return ret;
2279
2280         return sprintf(buf, "%d\n", state);
2281 }
2282
2283 static ssize_t usb_three_store(struct device *dev,
2284                                struct device_attribute *attr,
2285                                const char *buf, size_t count)
2286 {
2287         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2288         int state;
2289         int ret;
2290
2291         ret = kstrtoint(buf, 0, &state);
2292         if (ret)
2293                 return ret;
2294         /*
2295          * Check for USB 3 mode where:
2296          * 0 - Disabled (Acts like a USB 2 port, saving power)
2297          * 1 - Enabled
2298          */
2299         if (state != 0 && state != 1)
2300                 return -EINVAL;
2301
2302         ret = toshiba_usb_three_set(toshiba, state);
2303         if (ret)
2304                 return ret;
2305
2306         pr_info("Reboot for changes to USB 3 to take effect");
2307
2308         return count;
2309 }
2310 static DEVICE_ATTR_RW(usb_three);
2311
2312 static struct attribute *toshiba_attributes[] = {
2313         &dev_attr_version.attr,
2314         &dev_attr_fan.attr,
2315         &dev_attr_kbd_backlight_mode.attr,
2316         &dev_attr_kbd_type.attr,
2317         &dev_attr_available_kbd_modes.attr,
2318         &dev_attr_kbd_backlight_timeout.attr,
2319         &dev_attr_touchpad.attr,
2320         &dev_attr_position.attr,
2321         &dev_attr_usb_sleep_charge.attr,
2322         &dev_attr_sleep_functions_on_battery.attr,
2323         &dev_attr_usb_rapid_charge.attr,
2324         &dev_attr_usb_sleep_music.attr,
2325         &dev_attr_kbd_function_keys.attr,
2326         &dev_attr_panel_power_on.attr,
2327         &dev_attr_usb_three.attr,
2328         NULL,
2329 };
2330
2331 static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2332                                         struct attribute *attr, int idx)
2333 {
2334         struct device *dev = container_of(kobj, struct device, kobj);
2335         struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2336         bool exists = true;
2337
2338         if (attr == &dev_attr_fan.attr)
2339                 exists = (drv->fan_supported) ? true : false;
2340         else if (attr == &dev_attr_kbd_backlight_mode.attr)
2341                 exists = (drv->kbd_illum_supported) ? true : false;
2342         else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2343                 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
2344         else if (attr == &dev_attr_touchpad.attr)
2345                 exists = (drv->touchpad_supported) ? true : false;
2346         else if (attr == &dev_attr_position.attr)
2347                 exists = (drv->accelerometer_supported) ? true : false;
2348         else if (attr == &dev_attr_usb_sleep_charge.attr)
2349                 exists = (drv->usb_sleep_charge_supported) ? true : false;
2350         else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2351                 exists = (drv->usb_sleep_charge_supported) ? true : false;
2352         else if (attr == &dev_attr_usb_rapid_charge.attr)
2353                 exists = (drv->usb_rapid_charge_supported) ? true : false;
2354         else if (attr == &dev_attr_usb_sleep_music.attr)
2355                 exists = (drv->usb_sleep_music_supported) ? true : false;
2356         else if (attr == &dev_attr_kbd_function_keys.attr)
2357                 exists = (drv->kbd_function_keys_supported) ? true : false;
2358         else if (attr == &dev_attr_panel_power_on.attr)
2359                 exists = (drv->panel_power_on_supported) ? true : false;
2360         else if (attr == &dev_attr_usb_three.attr)
2361                 exists = (drv->usb_three_supported) ? true : false;
2362
2363         return exists ? attr->mode : 0;
2364 }
2365
2366 static struct attribute_group toshiba_attr_group = {
2367         .is_visible = toshiba_sysfs_is_visible,
2368         .attrs = toshiba_attributes,
2369 };
2370
2371 /*
2372  * Hotkeys
2373  */
2374 static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2375 {
2376         acpi_status status;
2377         u32 result;
2378
2379         status = acpi_evaluate_object(dev->acpi_dev->handle,
2380                                       "ENAB", NULL, NULL);
2381         if (ACPI_FAILURE(status))
2382                 return -ENODEV;
2383
2384         result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2385         if (result == TOS_FAILURE)
2386                 return -EIO;
2387         else if (result == TOS_NOT_SUPPORTED)
2388                 return -ENODEV;
2389
2390         return 0;
2391 }
2392
2393 static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2394 {
2395         u32 result;
2396
2397         /*
2398          * Re-activate the hotkeys, but this time, we are using the
2399          * "Special Functions" mode.
2400          */
2401         result = hci_write1(dev, HCI_HOTKEY_EVENT,
2402                             HCI_HOTKEY_SPECIAL_FUNCTIONS);
2403         if (result != TOS_SUCCESS)
2404                 pr_err("Could not enable the Special Function mode\n");
2405 }
2406
2407 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2408                                       struct serio *port)
2409 {
2410         if (str & I8042_STR_AUXDATA)
2411                 return false;
2412
2413         if (unlikely(data == 0xe0))
2414                 return false;
2415
2416         if ((data & 0x7f) == TOS1900_FN_SCAN) {
2417                 schedule_work(&toshiba_acpi->hotkey_work);
2418                 return true;
2419         }
2420
2421         return false;
2422 }
2423
2424 static void toshiba_acpi_hotkey_work(struct work_struct *work)
2425 {
2426         acpi_handle ec_handle = ec_get_handle();
2427         acpi_status status;
2428
2429         if (!ec_handle)
2430                 return;
2431
2432         status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2433         if (ACPI_FAILURE(status))
2434                 pr_err("ACPI NTFY method execution failed\n");
2435 }
2436
2437 /*
2438  * Returns hotkey scancode, or < 0 on failure.
2439  */
2440 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2441 {
2442         unsigned long long value;
2443         acpi_status status;
2444
2445         status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2446                                       NULL, &value);
2447         if (ACPI_FAILURE(status)) {
2448                 pr_err("ACPI INFO method execution failed\n");
2449                 return -EIO;
2450         }
2451
2452         return value;
2453 }
2454
2455 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2456                                        int scancode)
2457 {
2458         if (scancode == 0x100)
2459                 return;
2460
2461         /* Act on key press; ignore key release */
2462         if (scancode & 0x80)
2463                 return;
2464
2465         if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2466                 pr_info("Unknown key %x\n", scancode);
2467 }
2468
2469 static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2470 {
2471         u32 hci_result, value;
2472         int retries = 3;
2473         int scancode;
2474
2475         if (dev->info_supported) {
2476                 scancode = toshiba_acpi_query_hotkey(dev);
2477                 if (scancode < 0)
2478                         pr_err("Failed to query hotkey event\n");
2479                 else if (scancode != 0)
2480                         toshiba_acpi_report_hotkey(dev, scancode);
2481         } else if (dev->system_event_supported) {
2482                 do {
2483                         hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2484                         switch (hci_result) {
2485                         case TOS_SUCCESS:
2486                                 toshiba_acpi_report_hotkey(dev, (int)value);
2487                                 break;
2488                         case TOS_NOT_SUPPORTED:
2489                                 /*
2490                                  * This is a workaround for an unresolved
2491                                  * issue on some machines where system events
2492                                  * sporadically become disabled.
2493                                  */
2494                                 hci_result =
2495                                         hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2496                                 pr_notice("Re-enabled hotkeys\n");
2497                                 /* Fall through */
2498                         default:
2499                                 retries--;
2500                                 break;
2501                         }
2502                 } while (retries && hci_result != TOS_FIFO_EMPTY);
2503         }
2504 }
2505
2506 static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
2507 {
2508         const struct key_entry *keymap = toshiba_acpi_keymap;
2509         acpi_handle ec_handle;
2510         u32 events_type;
2511         u32 hci_result;
2512         int error;
2513
2514         error = toshiba_acpi_enable_hotkeys(dev);
2515         if (error)
2516                 return error;
2517
2518         error = toshiba_hotkey_event_type_get(dev, &events_type);
2519         if (error) {
2520                 pr_err("Unable to query Hotkey Event Type\n");
2521                 return error;
2522         }
2523         dev->hotkey_event_type = events_type;
2524
2525         dev->hotkey_dev = input_allocate_device();
2526         if (!dev->hotkey_dev)
2527                 return -ENOMEM;
2528
2529         dev->hotkey_dev->name = "Toshiba input device";
2530         dev->hotkey_dev->phys = "toshiba_acpi/input0";
2531         dev->hotkey_dev->id.bustype = BUS_HOST;
2532
2533         if (events_type == HCI_SYSTEM_TYPE1 ||
2534             !dev->kbd_function_keys_supported)
2535                 keymap = toshiba_acpi_keymap;
2536         else if (events_type == HCI_SYSTEM_TYPE2 ||
2537                  dev->kbd_function_keys_supported)
2538                 keymap = toshiba_acpi_alt_keymap;
2539         else
2540                 pr_info("Unknown event type received %x\n", events_type);
2541         error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
2542         if (error)
2543                 goto err_free_dev;
2544
2545         /*
2546          * For some machines the SCI responsible for providing hotkey
2547          * notification doesn't fire. We can trigger the notification
2548          * whenever the Fn key is pressed using the NTFY method, if
2549          * supported, so if it's present set up an i8042 key filter
2550          * for this purpose.
2551          */
2552         ec_handle = ec_get_handle();
2553         if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
2554                 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2555
2556                 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2557                 if (error) {
2558                         pr_err("Error installing key filter\n");
2559                         goto err_free_keymap;
2560                 }
2561
2562                 dev->ntfy_supported = 1;
2563         }
2564
2565         /*
2566          * Determine hotkey query interface. Prefer using the INFO
2567          * method when it is available.
2568          */
2569         if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
2570                 dev->info_supported = 1;
2571         else {
2572                 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2573                 if (hci_result == TOS_SUCCESS)
2574                         dev->system_event_supported = 1;
2575         }
2576
2577         if (!dev->info_supported && !dev->system_event_supported) {
2578                 pr_warn("No hotkey query interface found\n");
2579                 goto err_remove_filter;
2580         }
2581
2582         error = input_register_device(dev->hotkey_dev);
2583         if (error) {
2584                 pr_info("Unable to register input device\n");
2585                 goto err_remove_filter;
2586         }
2587
2588         return 0;
2589
2590  err_remove_filter:
2591         if (dev->ntfy_supported)
2592                 i8042_remove_filter(toshiba_acpi_i8042_filter);
2593  err_free_keymap:
2594         sparse_keymap_free(dev->hotkey_dev);
2595  err_free_dev:
2596         input_free_device(dev->hotkey_dev);
2597         dev->hotkey_dev = NULL;
2598         return error;
2599 }
2600
2601 static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
2602 {
2603         struct backlight_properties props;
2604         int brightness;
2605         int ret;
2606         bool enabled;
2607
2608         /*
2609          * Some machines don't support the backlight methods at all, and
2610          * others support it read-only. Either of these is pretty useless,
2611          * so only register the backlight device if the backlight method
2612          * supports both reads and writes.
2613          */
2614         brightness = __get_lcd_brightness(dev);
2615         if (brightness < 0)
2616                 return 0;
2617         ret = set_lcd_brightness(dev, brightness);
2618         if (ret) {
2619                 pr_debug("Backlight method is read-only, disabling backlight support\n");
2620                 return 0;
2621         }
2622
2623         /* Determine whether or not BIOS supports transflective backlight */
2624         ret = get_tr_backlight_status(dev, &enabled);
2625         dev->tr_backlight_supported = !ret;
2626
2627         memset(&props, 0, sizeof(props));
2628         props.type = BACKLIGHT_PLATFORM;
2629         props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
2630
2631         /* Adding an extra level and having 0 change to transflective mode */
2632         if (dev->tr_backlight_supported)
2633                 props.max_brightness++;
2634
2635         dev->backlight_dev = backlight_device_register("toshiba",
2636                                                        &dev->acpi_dev->dev,
2637                                                        dev,
2638                                                        &toshiba_backlight_data,
2639                                                        &props);
2640         if (IS_ERR(dev->backlight_dev)) {
2641                 ret = PTR_ERR(dev->backlight_dev);
2642                 pr_err("Could not register toshiba backlight device\n");
2643                 dev->backlight_dev = NULL;
2644                 return ret;
2645         }
2646
2647         dev->backlight_dev->props.brightness = brightness;
2648         return 0;
2649 }
2650
2651 static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
2652 {
2653         struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2654
2655         remove_toshiba_proc_entries(dev);
2656
2657         if (dev->sysfs_created)
2658                 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2659                                    &toshiba_attr_group);
2660
2661         if (dev->ntfy_supported) {
2662                 i8042_remove_filter(toshiba_acpi_i8042_filter);
2663                 cancel_work_sync(&dev->hotkey_work);
2664         }
2665
2666         if (dev->hotkey_dev) {
2667                 input_unregister_device(dev->hotkey_dev);
2668                 sparse_keymap_free(dev->hotkey_dev);
2669         }
2670
2671         if (dev->bt_rfk) {
2672                 rfkill_unregister(dev->bt_rfk);
2673                 rfkill_destroy(dev->bt_rfk);
2674         }
2675
2676         backlight_device_unregister(dev->backlight_dev);
2677
2678         if (dev->illumination_supported)
2679                 led_classdev_unregister(&dev->led_dev);
2680
2681         if (dev->kbd_led_registered)
2682                 led_classdev_unregister(&dev->kbd_led);
2683
2684         if (dev->eco_supported)
2685                 led_classdev_unregister(&dev->eco_led);
2686
2687         if (toshiba_acpi)
2688                 toshiba_acpi = NULL;
2689
2690         kfree(dev);
2691
2692         return 0;
2693 }
2694
2695 static const char *find_hci_method(acpi_handle handle)
2696 {
2697         if (acpi_has_method(handle, "GHCI"))
2698                 return "GHCI";
2699
2700         if (acpi_has_method(handle, "SPFC"))
2701                 return "SPFC";
2702
2703         return NULL;
2704 }
2705
2706 static int toshiba_acpi_add(struct acpi_device *acpi_dev)
2707 {
2708         struct toshiba_acpi_dev *dev;
2709         const char *hci_method;
2710         u32 special_functions;
2711         u32 dummy;
2712         bool bt_present;
2713         int ret = 0;
2714
2715         if (toshiba_acpi)
2716                 return -EBUSY;
2717
2718         pr_info("Toshiba Laptop ACPI Extras version %s\n",
2719                TOSHIBA_ACPI_VERSION);
2720
2721         hci_method = find_hci_method(acpi_dev->handle);
2722         if (!hci_method) {
2723                 pr_err("HCI interface not found\n");
2724                 return -ENODEV;
2725         }
2726
2727         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2728         if (!dev)
2729                 return -ENOMEM;
2730         dev->acpi_dev = acpi_dev;
2731         dev->method_hci = hci_method;
2732         acpi_dev->driver_data = dev;
2733         dev_set_drvdata(&acpi_dev->dev, dev);
2734
2735         /* Query the BIOS for supported features */
2736
2737         /*
2738          * The "Special Functions" are always supported by the laptops
2739          * with the new keyboard layout, query for its presence to help
2740          * determine the keymap layout to use.
2741          */
2742         ret = toshiba_function_keys_get(dev, &special_functions);
2743         dev->kbd_function_keys_supported = !ret;
2744
2745         if (toshiba_acpi_setup_keyboard(dev))
2746                 pr_info("Unable to activate hotkeys\n");
2747
2748         mutex_init(&dev->mutex);
2749
2750         ret = toshiba_acpi_setup_backlight(dev);
2751         if (ret)
2752                 goto error;
2753
2754         /* Register rfkill switch for Bluetooth */
2755         if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
2756                 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2757                                            &acpi_dev->dev,
2758                                            RFKILL_TYPE_BLUETOOTH,
2759                                            &toshiba_rfk_ops,
2760                                            dev);
2761                 if (!dev->bt_rfk) {
2762                         pr_err("unable to allocate rfkill device\n");
2763                         ret = -ENOMEM;
2764                         goto error;
2765                 }
2766
2767                 ret = rfkill_register(dev->bt_rfk);
2768                 if (ret) {
2769                         pr_err("unable to register rfkill device\n");
2770                         rfkill_destroy(dev->bt_rfk);
2771                         goto error;
2772                 }
2773         }
2774
2775         if (toshiba_illumination_available(dev)) {
2776                 dev->led_dev.name = "toshiba::illumination";
2777                 dev->led_dev.max_brightness = 1;
2778                 dev->led_dev.brightness_set = toshiba_illumination_set;
2779                 dev->led_dev.brightness_get = toshiba_illumination_get;
2780                 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
2781                         dev->illumination_supported = 1;
2782         }
2783
2784         if (toshiba_eco_mode_available(dev)) {
2785                 dev->eco_led.name = "toshiba::eco_mode";
2786                 dev->eco_led.max_brightness = 1;
2787                 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2788                 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2789                 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2790                         dev->eco_supported = 1;
2791         }
2792
2793         dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
2794         /*
2795          * Only register the LED if KBD illumination is supported
2796          * and the keyboard backlight operation mode is set to FN-Z
2797          */
2798         if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2799                 dev->kbd_led.name = "toshiba::kbd_backlight";
2800                 dev->kbd_led.max_brightness = 1;
2801                 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2802                 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2803                 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2804                         dev->kbd_led_registered = 1;
2805         }
2806
2807         ret = toshiba_touchpad_get(dev, &dummy);
2808         dev->touchpad_supported = !ret;
2809
2810         ret = toshiba_accelerometer_supported(dev);
2811         dev->accelerometer_supported = !ret;
2812
2813         toshiba_usb_sleep_charge_available(dev);
2814
2815         ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2816         dev->usb_rapid_charge_supported = !ret;
2817
2818         ret = toshiba_usb_sleep_music_get(dev, &dummy);
2819         dev->usb_sleep_music_supported = !ret;
2820
2821         ret = toshiba_panel_power_on_get(dev, &dummy);
2822         dev->panel_power_on_supported = !ret;
2823
2824         ret = toshiba_usb_three_get(dev, &dummy);
2825         dev->usb_three_supported = !ret;
2826
2827         ret = get_video_status(dev, &dummy);
2828         dev->video_supported = !ret;
2829
2830         ret = get_fan_status(dev, &dummy);
2831         dev->fan_supported = !ret;
2832
2833         /*
2834          * Enable the "Special Functions" mode only if they are
2835          * supported and if they are activated.
2836          */
2837         if (dev->kbd_function_keys_supported && special_functions)
2838                 toshiba_acpi_enable_special_functions(dev);
2839
2840         ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2841                                  &toshiba_attr_group);
2842         if (ret) {
2843                 dev->sysfs_created = 0;
2844                 goto error;
2845         }
2846         dev->sysfs_created = !ret;
2847
2848         create_toshiba_proc_entries(dev);
2849
2850         toshiba_acpi = dev;
2851
2852         return 0;
2853
2854 error:
2855         toshiba_acpi_remove(acpi_dev);
2856         return ret;
2857 }
2858
2859 static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2860 {
2861         struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2862         int ret;
2863
2864         switch (event) {
2865         case 0x80: /* Hotkeys and some system events */
2866                 toshiba_acpi_process_hotkeys(dev);
2867                 break;
2868         case 0x81: /* Dock events */
2869         case 0x82:
2870         case 0x83:
2871                 pr_info("Dock event received %x\n", event);
2872                 break;
2873         case 0x88: /* Thermal events */
2874                 pr_info("Thermal event received\n");
2875                 break;
2876         case 0x8f: /* LID closed */
2877         case 0x90: /* LID is closed and Dock has been ejected */
2878                 break;
2879         case 0x8c: /* SATA power events */
2880         case 0x8b:
2881                 pr_info("SATA power event received %x\n", event);
2882                 break;
2883         case 0x92: /* Keyboard backlight mode changed */
2884                 /* Update sysfs entries */
2885                 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2886                                          &toshiba_attr_group);
2887                 if (ret)
2888                         pr_err("Unable to update sysfs entries\n");
2889                 break;
2890         case 0x85: /* Unknown */
2891         case 0x8d: /* Unknown */
2892         case 0x8e: /* Unknown */
2893         case 0x94: /* Unknown */
2894         case 0x95: /* Unknown */
2895         default:
2896                 pr_info("Unknown event received %x\n", event);
2897                 break;
2898         }
2899
2900         acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2901                                         dev_name(&acpi_dev->dev),
2902                                         event, 0);
2903 }
2904
2905 #ifdef CONFIG_PM_SLEEP
2906 static int toshiba_acpi_suspend(struct device *device)
2907 {
2908         struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
2909         u32 result;
2910
2911         if (dev->hotkey_dev)
2912                 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
2913
2914         return 0;
2915 }
2916
2917 static int toshiba_acpi_resume(struct device *device)
2918 {
2919         struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
2920         int error;
2921
2922         if (dev->hotkey_dev) {
2923                 error = toshiba_acpi_enable_hotkeys(dev);
2924                 if (error)
2925                         pr_info("Unable to re-enable hotkeys\n");
2926         }
2927
2928         return 0;
2929 }
2930 #endif
2931
2932 static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2933                          toshiba_acpi_suspend, toshiba_acpi_resume);
2934
2935 static struct acpi_driver toshiba_acpi_driver = {
2936         .name   = "Toshiba ACPI driver",
2937         .owner  = THIS_MODULE,
2938         .ids    = toshiba_device_ids,
2939         .flags  = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2940         .ops    = {
2941                 .add            = toshiba_acpi_add,
2942                 .remove         = toshiba_acpi_remove,
2943                 .notify         = toshiba_acpi_notify,
2944         },
2945         .drv.pm = &toshiba_acpi_pm,
2946 };
2947
2948 static int __init toshiba_acpi_init(void)
2949 {
2950         int ret;
2951
2952         /*
2953          * Machines with this WMI guid aren't supported due to bugs in
2954          * their AML. This check relies on wmi initializing before
2955          * toshiba_acpi to guarantee guids have been identified.
2956          */
2957         if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2958                 return -ENODEV;
2959
2960         toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2961         if (!toshiba_proc_dir) {
2962                 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
2963                 return -ENODEV;
2964         }
2965
2966         ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2967         if (ret) {
2968                 pr_err("Failed to register ACPI driver: %d\n", ret);
2969                 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
2970         }
2971
2972         return ret;
2973 }
2974
2975 static void __exit toshiba_acpi_exit(void)
2976 {
2977         acpi_bus_unregister_driver(&toshiba_acpi_driver);
2978         if (toshiba_proc_dir)
2979                 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
2980 }
2981
2982 module_init(toshiba_acpi_init);
2983 module_exit(toshiba_acpi_exit);