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