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