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