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