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