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