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