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