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