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