1 /*
2  *  toshiba_acpi.c - Toshiba Laptop ACPI Extras
3  *
4  *
5  *  Copyright (C) 2002-2004 John Belmonte
6  *  Copyright (C) 2008 Philip Langdale
7  *  Copyright (C) 2010 Pierre Ducroquet
8  *  Copyright (C) 2014 Azael Avalos
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  *
25  *  The devolpment page for this driver is located at
26  *  http://memebeam.org/toys/ToshibaAcpiDriver.
27  *
28  *  Credits:
29  *	Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
30  *		engineering the Windows drivers
31  *	Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
32  *	Rob Miller - TV out and hotkeys help
33  *
34  *
35  *  TODO
36  *
37  */
38 
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 
41 #define TOSHIBA_ACPI_VERSION	"0.20"
42 #define PROC_INTERFACE_VERSION	1
43 
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/types.h>
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.h>
50 #include <linux/backlight.h>
51 #include <linux/rfkill.h>
52 #include <linux/input.h>
53 #include <linux/input/sparse-keymap.h>
54 #include <linux/leds.h>
55 #include <linux/slab.h>
56 #include <linux/workqueue.h>
57 #include <linux/i8042.h>
58 #include <linux/acpi.h>
59 #include <asm/uaccess.h>
60 
61 MODULE_AUTHOR("John Belmonte");
62 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
63 MODULE_LICENSE("GPL");
64 
65 #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
66 
67 /* Scan code for Fn key on TOS1900 models */
68 #define TOS1900_FN_SCAN		0x6e
69 
70 /* Toshiba ACPI method paths */
71 #define METHOD_VIDEO_OUT	"\\_SB_.VALX.DSSX"
72 
73 /* Toshiba HCI interface definitions
74  *
75  * HCI is Toshiba's "Hardware Control Interface" which is supposed to
76  * be uniform across all their models.  Ideally we would just call
77  * dedicated ACPI methods instead of using this primitive interface.
78  * However the ACPI methods seem to be incomplete in some areas (for
79  * example they allow setting, but not reading, the LCD brightness value),
80  * so this is still useful.
81  *
82  * SCI stands for "System Configuration Interface" which aim is to
83  * conceal differences in hardware between different models.
84  */
85 
86 #define HCI_WORDS			6
87 
88 /* operations */
89 #define HCI_SET				0xff00
90 #define HCI_GET				0xfe00
91 #define SCI_OPEN			0xf100
92 #define SCI_CLOSE			0xf200
93 #define SCI_GET				0xf300
94 #define SCI_SET				0xf400
95 
96 /* return codes */
97 #define HCI_SUCCESS			0x0000
98 #define HCI_FAILURE			0x1000
99 #define HCI_NOT_SUPPORTED		0x8000
100 #define HCI_EMPTY			0x8c00
101 #define HCI_DATA_NOT_AVAILABLE		0x8d20
102 #define HCI_NOT_INITIALIZED		0x8d50
103 #define SCI_OPEN_CLOSE_OK		0x0044
104 #define SCI_ALREADY_OPEN		0x8100
105 #define SCI_NOT_OPENED			0x8200
106 #define SCI_INPUT_DATA_ERROR		0x8300
107 #define SCI_NOT_PRESENT			0x8600
108 
109 /* registers */
110 #define HCI_FAN				0x0004
111 #define HCI_TR_BACKLIGHT		0x0005
112 #define HCI_SYSTEM_EVENT		0x0016
113 #define HCI_VIDEO_OUT			0x001c
114 #define HCI_HOTKEY_EVENT		0x001e
115 #define HCI_LCD_BRIGHTNESS		0x002a
116 #define HCI_WIRELESS			0x0056
117 #define HCI_ACCELEROMETER		0x006d
118 #define HCI_KBD_ILLUMINATION		0x0095
119 #define HCI_ECO_MODE			0x0097
120 #define HCI_ACCELEROMETER2		0x00a6
121 #define SCI_ILLUMINATION		0x014e
122 #define SCI_KBD_ILLUM_STATUS		0x015c
123 #define SCI_TOUCHPAD			0x050e
124 
125 /* field definitions */
126 #define HCI_ACCEL_MASK			0x7fff
127 #define HCI_HOTKEY_DISABLE		0x0b
128 #define HCI_HOTKEY_ENABLE		0x09
129 #define HCI_LCD_BRIGHTNESS_BITS		3
130 #define HCI_LCD_BRIGHTNESS_SHIFT	(16-HCI_LCD_BRIGHTNESS_BITS)
131 #define HCI_LCD_BRIGHTNESS_LEVELS	(1 << HCI_LCD_BRIGHTNESS_BITS)
132 #define HCI_MISC_SHIFT			0x10
133 #define HCI_VIDEO_OUT_LCD		0x1
134 #define HCI_VIDEO_OUT_CRT		0x2
135 #define HCI_VIDEO_OUT_TV		0x4
136 #define HCI_WIRELESS_KILL_SWITCH	0x01
137 #define HCI_WIRELESS_BT_PRESENT		0x0f
138 #define HCI_WIRELESS_BT_ATTACH		0x40
139 #define HCI_WIRELESS_BT_POWER		0x80
140 #define SCI_KBD_MODE_FNZ		0x1
141 #define SCI_KBD_MODE_AUTO		0x2
142 
143 struct toshiba_acpi_dev {
144 	struct acpi_device *acpi_dev;
145 	const char *method_hci;
146 	struct rfkill *bt_rfk;
147 	struct input_dev *hotkey_dev;
148 	struct work_struct hotkey_work;
149 	struct backlight_device *backlight_dev;
150 	struct led_classdev led_dev;
151 	struct led_classdev kbd_led;
152 	struct led_classdev eco_led;
153 
154 	int force_fan;
155 	int last_key_event;
156 	int key_event_valid;
157 	int kbd_mode;
158 	int kbd_time;
159 
160 	unsigned int illumination_supported:1;
161 	unsigned int video_supported:1;
162 	unsigned int fan_supported:1;
163 	unsigned int system_event_supported:1;
164 	unsigned int ntfy_supported:1;
165 	unsigned int info_supported:1;
166 	unsigned int tr_backlight_supported:1;
167 	unsigned int kbd_illum_supported:1;
168 	unsigned int kbd_led_registered:1;
169 	unsigned int touchpad_supported:1;
170 	unsigned int eco_supported:1;
171 	unsigned int accelerometer_supported:1;
172 	unsigned int sysfs_created:1;
173 
174 	struct mutex mutex;
175 };
176 
177 static struct toshiba_acpi_dev *toshiba_acpi;
178 
179 static const struct acpi_device_id toshiba_device_ids[] = {
180 	{"TOS6200", 0},
181 	{"TOS6208", 0},
182 	{"TOS1900", 0},
183 	{"", 0},
184 };
185 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
186 
187 static const struct key_entry toshiba_acpi_keymap[] = {
188 	{ KE_KEY, 0x9e, { KEY_RFKILL } },
189 	{ KE_KEY, 0x101, { KEY_MUTE } },
190 	{ KE_KEY, 0x102, { KEY_ZOOMOUT } },
191 	{ KE_KEY, 0x103, { KEY_ZOOMIN } },
192 	{ KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
193 	{ KE_KEY, 0x139, { KEY_ZOOMRESET } },
194 	{ KE_KEY, 0x13b, { KEY_COFFEE } },
195 	{ KE_KEY, 0x13c, { KEY_BATTERY } },
196 	{ KE_KEY, 0x13d, { KEY_SLEEP } },
197 	{ KE_KEY, 0x13e, { KEY_SUSPEND } },
198 	{ KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
199 	{ KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
200 	{ KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
201 	{ KE_KEY, 0x142, { KEY_WLAN } },
202 	{ KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
203 	{ KE_KEY, 0x17f, { KEY_FN } },
204 	{ KE_KEY, 0xb05, { KEY_PROG2 } },
205 	{ KE_KEY, 0xb06, { KEY_WWW } },
206 	{ KE_KEY, 0xb07, { KEY_MAIL } },
207 	{ KE_KEY, 0xb30, { KEY_STOP } },
208 	{ KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
209 	{ KE_KEY, 0xb32, { KEY_NEXTSONG } },
210 	{ KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
211 	{ KE_KEY, 0xb5a, { KEY_MEDIA } },
212 	{ KE_IGNORE, 0x1430, { KEY_RESERVED } },
213 	{ KE_END, 0 },
214 };
215 
216 /* utility
217  */
218 
219 static __inline__ void _set_bit(u32 * word, u32 mask, int value)
220 {
221 	*word = (*word & ~mask) | (mask * value);
222 }
223 
224 /* acpi interface wrappers
225  */
226 
227 static int write_acpi_int(const char *methodName, int val)
228 {
229 	acpi_status status;
230 
231 	status = acpi_execute_simple_method(NULL, (char *)methodName, val);
232 	return (status == AE_OK) ? 0 : -EIO;
233 }
234 
235 /* Perform a raw HCI call.  Here we don't care about input or output buffer
236  * format.
237  */
238 static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
239 			   const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
240 {
241 	struct acpi_object_list params;
242 	union acpi_object in_objs[HCI_WORDS];
243 	struct acpi_buffer results;
244 	union acpi_object out_objs[HCI_WORDS + 1];
245 	acpi_status status;
246 	int i;
247 
248 	params.count = HCI_WORDS;
249 	params.pointer = in_objs;
250 	for (i = 0; i < HCI_WORDS; ++i) {
251 		in_objs[i].type = ACPI_TYPE_INTEGER;
252 		in_objs[i].integer.value = in[i];
253 	}
254 
255 	results.length = sizeof(out_objs);
256 	results.pointer = out_objs;
257 
258 	status = acpi_evaluate_object(dev->acpi_dev->handle,
259 				      (char *)dev->method_hci, &params,
260 				      &results);
261 	if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
262 		for (i = 0; i < out_objs->package.count; ++i) {
263 			out[i] = out_objs->package.elements[i].integer.value;
264 		}
265 	}
266 
267 	return status;
268 }
269 
270 /* common hci tasks (get or set one or two value)
271  *
272  * In addition to the ACPI status, the HCI system returns a result which
273  * may be useful (such as "not supported").
274  */
275 
276 static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
277 			      u32 in1, u32 *result)
278 {
279 	u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
280 	u32 out[HCI_WORDS];
281 	acpi_status status = hci_raw(dev, in, out);
282 	*result = (status == AE_OK) ? out[0] : HCI_FAILURE;
283 	return status;
284 }
285 
286 static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
287 			     u32 *out1, u32 *result)
288 {
289 	u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
290 	u32 out[HCI_WORDS];
291 	acpi_status status = hci_raw(dev, in, out);
292 	*out1 = out[2];
293 	*result = (status == AE_OK) ? out[0] : HCI_FAILURE;
294 	return status;
295 }
296 
297 static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
298 			      u32 in1, u32 in2, u32 *result)
299 {
300 	u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
301 	u32 out[HCI_WORDS];
302 	acpi_status status = hci_raw(dev, in, out);
303 	*result = (status == AE_OK) ? out[0] : HCI_FAILURE;
304 	return status;
305 }
306 
307 static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
308 			     u32 *out1, u32 *out2, u32 *result)
309 {
310 	u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
311 	u32 out[HCI_WORDS];
312 	acpi_status status = hci_raw(dev, in, out);
313 	*out1 = out[2];
314 	*out2 = out[3];
315 	*result = (status == AE_OK) ? out[0] : HCI_FAILURE;
316 	return status;
317 }
318 
319 /* common sci tasks
320  */
321 
322 static int sci_open(struct toshiba_acpi_dev *dev)
323 {
324 	u32 in[HCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
325 	u32 out[HCI_WORDS];
326 	acpi_status status;
327 
328 	status = hci_raw(dev, in, out);
329 	if  (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
330 		pr_err("ACPI call to open SCI failed\n");
331 		return 0;
332 	}
333 
334 	if (out[0] == SCI_OPEN_CLOSE_OK) {
335 		return 1;
336 	} else if (out[0] == SCI_ALREADY_OPEN) {
337 		pr_info("Toshiba SCI already opened\n");
338 		return 1;
339 	} else if (out[0] == SCI_NOT_PRESENT) {
340 		pr_info("Toshiba SCI is not present\n");
341 	}
342 
343 	return 0;
344 }
345 
346 static void sci_close(struct toshiba_acpi_dev *dev)
347 {
348 	u32 in[HCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
349 	u32 out[HCI_WORDS];
350 	acpi_status status;
351 
352 	status = hci_raw(dev, in, out);
353 	if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
354 		pr_err("ACPI call to close SCI failed\n");
355 		return;
356 	}
357 
358 	if (out[0] == SCI_OPEN_CLOSE_OK)
359 		return;
360 	else if (out[0] == SCI_NOT_OPENED)
361 		pr_info("Toshiba SCI not opened\n");
362 	else if (out[0] == SCI_NOT_PRESENT)
363 		pr_info("Toshiba SCI is not present\n");
364 }
365 
366 static acpi_status sci_read(struct toshiba_acpi_dev *dev, u32 reg,
367 			    u32 *out1, u32 *result)
368 {
369 	u32 in[HCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
370 	u32 out[HCI_WORDS];
371 	acpi_status status = hci_raw(dev, in, out);
372 	*out1 = out[2];
373 	*result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
374 	return status;
375 }
376 
377 static acpi_status sci_write(struct toshiba_acpi_dev *dev, u32 reg,
378 			     u32 in1, u32 *result)
379 {
380 	u32 in[HCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
381 	u32 out[HCI_WORDS];
382 	acpi_status status = hci_raw(dev, in, out);
383 	*result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
384 	return status;
385 }
386 
387 /* Illumination support */
388 static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
389 {
390 	u32 in[HCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
391 	u32 out[HCI_WORDS];
392 	acpi_status status;
393 
394 	if (!sci_open(dev))
395 		return 0;
396 
397 	status = hci_raw(dev, in, out);
398 	sci_close(dev);
399 	if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
400 		pr_err("ACPI call to query Illumination support failed\n");
401 		return 0;
402 	} else if (out[0] == HCI_NOT_SUPPORTED || out[1] != 1) {
403 		pr_info("Illumination device not available\n");
404 		return 0;
405 	}
406 
407 	return 1;
408 }
409 
410 static void toshiba_illumination_set(struct led_classdev *cdev,
411 				     enum led_brightness brightness)
412 {
413 	struct toshiba_acpi_dev *dev = container_of(cdev,
414 			struct toshiba_acpi_dev, led_dev);
415 	u32 state, result;
416 	acpi_status status;
417 
418 	/* First request : initialize communication. */
419 	if (!sci_open(dev))
420 		return;
421 
422 	/* Switch the illumination on/off */
423 	state = brightness ? 1 : 0;
424 	status = sci_write(dev, SCI_ILLUMINATION, state, &result);
425 	sci_close(dev);
426 	if (ACPI_FAILURE(status)) {
427 		pr_err("ACPI call for illumination failed\n");
428 		return;
429 	} else if (result == HCI_NOT_SUPPORTED) {
430 		pr_info("Illumination not supported\n");
431 		return;
432 	}
433 }
434 
435 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
436 {
437 	struct toshiba_acpi_dev *dev = container_of(cdev,
438 			struct toshiba_acpi_dev, led_dev);
439 	u32 state, result;
440 	acpi_status status;
441 
442 	/* First request : initialize communication. */
443 	if (!sci_open(dev))
444 		return LED_OFF;
445 
446 	/* Check the illumination */
447 	status = sci_read(dev, SCI_ILLUMINATION, &state, &result);
448 	sci_close(dev);
449 	if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
450 		pr_err("ACPI call for illumination failed\n");
451 		return LED_OFF;
452 	} else if (result == HCI_NOT_SUPPORTED) {
453 		pr_info("Illumination not supported\n");
454 		return LED_OFF;
455 	}
456 
457 	return state ? LED_FULL : LED_OFF;
458 }
459 
460 /* KBD Illumination */
461 static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
462 {
463 	u32 result;
464 	acpi_status status;
465 
466 	if (!sci_open(dev))
467 		return -EIO;
468 
469 	status = sci_write(dev, SCI_KBD_ILLUM_STATUS, time, &result);
470 	sci_close(dev);
471 	if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
472 		pr_err("ACPI call to set KBD backlight status failed\n");
473 		return -EIO;
474 	} else if (result == HCI_NOT_SUPPORTED) {
475 		pr_info("Keyboard backlight status not supported\n");
476 		return -ENODEV;
477 	}
478 
479 	return 0;
480 }
481 
482 static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
483 {
484 	u32 result;
485 	acpi_status status;
486 
487 	if (!sci_open(dev))
488 		return -EIO;
489 
490 	status = sci_read(dev, SCI_KBD_ILLUM_STATUS, time, &result);
491 	sci_close(dev);
492 	if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
493 		pr_err("ACPI call to get KBD backlight status failed\n");
494 		return -EIO;
495 	} else if (result == HCI_NOT_SUPPORTED) {
496 		pr_info("Keyboard backlight status not supported\n");
497 		return -ENODEV;
498 	}
499 
500 	return 0;
501 }
502 
503 static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
504 {
505 	struct toshiba_acpi_dev *dev = container_of(cdev,
506 			struct toshiba_acpi_dev, kbd_led);
507 	u32 state, result;
508 	acpi_status status;
509 
510 	/* Check the keyboard backlight state */
511 	status = hci_read1(dev, HCI_KBD_ILLUMINATION, &state, &result);
512 	if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
513 		pr_err("ACPI call to get the keyboard backlight failed\n");
514 		return LED_OFF;
515 	} else if (result == HCI_NOT_SUPPORTED) {
516 		pr_info("Keyboard backlight not supported\n");
517 		return LED_OFF;
518 	}
519 
520 	return state ? LED_FULL : LED_OFF;
521 }
522 
523 static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
524 				     enum led_brightness brightness)
525 {
526 	struct toshiba_acpi_dev *dev = container_of(cdev,
527 			struct toshiba_acpi_dev, kbd_led);
528 	u32 state, result;
529 	acpi_status status;
530 
531 	/* Set the keyboard backlight state */
532 	state = brightness ? 1 : 0;
533 	status = hci_write1(dev, HCI_KBD_ILLUMINATION, state, &result);
534 	if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
535 		pr_err("ACPI call to set KBD Illumination mode failed\n");
536 		return;
537 	} else if (result == HCI_NOT_SUPPORTED) {
538 		pr_info("Keyboard backlight not supported\n");
539 		return;
540 	}
541 }
542 
543 /* TouchPad support */
544 static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
545 {
546 	u32 result;
547 	acpi_status status;
548 
549 	if (!sci_open(dev))
550 		return -EIO;
551 
552 	status = sci_write(dev, SCI_TOUCHPAD, state, &result);
553 	sci_close(dev);
554 	if (ACPI_FAILURE(status)) {
555 		pr_err("ACPI call to set the touchpad failed\n");
556 		return -EIO;
557 	} else if (result == HCI_NOT_SUPPORTED) {
558 		return -ENODEV;
559 	}
560 
561 	return 0;
562 }
563 
564 static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
565 {
566 	u32 result;
567 	acpi_status status;
568 
569 	if (!sci_open(dev))
570 		return -EIO;
571 
572 	status = sci_read(dev, SCI_TOUCHPAD, state, &result);
573 	sci_close(dev);
574 	if (ACPI_FAILURE(status)) {
575 		pr_err("ACPI call to query the touchpad failed\n");
576 		return -EIO;
577 	} else if (result == HCI_NOT_SUPPORTED) {
578 		return -ENODEV;
579 	}
580 
581 	return 0;
582 }
583 
584 /* Eco Mode support */
585 static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
586 {
587 	acpi_status status;
588 	u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
589 	u32 out[HCI_WORDS];
590 
591 	status = hci_raw(dev, in, out);
592 	if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
593 		pr_info("ACPI call to get ECO led failed\n");
594 		return 0;
595 	}
596 
597 	return 1;
598 }
599 
600 static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
601 {
602 	struct toshiba_acpi_dev *dev = container_of(cdev,
603 			struct toshiba_acpi_dev, eco_led);
604 	u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
605 	u32 out[HCI_WORDS];
606 	acpi_status status;
607 
608 	status = hci_raw(dev, in, out);
609 	if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
610 		pr_err("ACPI call to get ECO led failed\n");
611 		return LED_OFF;
612 	}
613 
614 	return out[2] ? LED_FULL : LED_OFF;
615 }
616 
617 static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
618 				     enum led_brightness brightness)
619 {
620 	struct toshiba_acpi_dev *dev = container_of(cdev,
621 			struct toshiba_acpi_dev, eco_led);
622 	u32 in[HCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
623 	u32 out[HCI_WORDS];
624 	acpi_status status;
625 
626 	/* Switch the Eco Mode led on/off */
627 	in[2] = (brightness) ? 1 : 0;
628 	status = hci_raw(dev, in, out);
629 	if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
630 		pr_err("ACPI call to set ECO led failed\n");
631 		return;
632 	}
633 }
634 
635 /* Accelerometer support */
636 static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
637 {
638 	u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
639 	u32 out[HCI_WORDS];
640 	acpi_status status;
641 
642 	/* Check if the accelerometer call exists,
643 	 * this call also serves as initialization
644 	 */
645 	status = hci_raw(dev, in, out);
646 	if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
647 		pr_err("ACPI call to query the accelerometer failed\n");
648 		return -EIO;
649 	} else if (out[0] == HCI_DATA_NOT_AVAILABLE ||
650 		   out[0] == HCI_NOT_INITIALIZED) {
651 		pr_err("Accelerometer not initialized\n");
652 		return -EIO;
653 	} else if (out[0] == HCI_NOT_SUPPORTED) {
654 		pr_info("Accelerometer not supported\n");
655 		return -ENODEV;
656 	}
657 
658 	return 0;
659 }
660 
661 static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
662 				      u32 *xy, u32 *z)
663 {
664 	u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
665 	u32 out[HCI_WORDS];
666 	acpi_status status;
667 
668 	/* Check the Accelerometer status */
669 	status = hci_raw(dev, in, out);
670 	if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
671 		pr_err("ACPI call to query the accelerometer failed\n");
672 		return -EIO;
673 	}
674 
675 	*xy = out[2];
676 	*z = out[4];
677 
678 	return 0;
679 }
680 
681 /* Bluetooth rfkill handlers */
682 
683 static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
684 {
685 	u32 hci_result;
686 	u32 value, value2;
687 
688 	value = 0;
689 	value2 = 0;
690 	hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
691 	if (hci_result == HCI_SUCCESS)
692 		*present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
693 
694 	return hci_result;
695 }
696 
697 static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
698 {
699 	u32 hci_result;
700 	u32 value, value2;
701 
702 	value = 0;
703 	value2 = 0x0001;
704 	hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
705 
706 	*radio_state = value & HCI_WIRELESS_KILL_SWITCH;
707 	return hci_result;
708 }
709 
710 static int bt_rfkill_set_block(void *data, bool blocked)
711 {
712 	struct toshiba_acpi_dev *dev = data;
713 	u32 result1, result2;
714 	u32 value;
715 	int err;
716 	bool radio_state;
717 
718 	value = (blocked == false);
719 
720 	mutex_lock(&dev->mutex);
721 	if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
722 		err = -EIO;
723 		goto out;
724 	}
725 
726 	if (!radio_state) {
727 		err = 0;
728 		goto out;
729 	}
730 
731 	hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
732 	hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
733 
734 	if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
735 		err = -EIO;
736 	else
737 		err = 0;
738  out:
739 	mutex_unlock(&dev->mutex);
740 	return err;
741 }
742 
743 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
744 {
745 	bool new_rfk_state;
746 	bool value;
747 	u32 hci_result;
748 	struct toshiba_acpi_dev *dev = data;
749 
750 	mutex_lock(&dev->mutex);
751 
752 	hci_result = hci_get_radio_state(dev, &value);
753 	if (hci_result != HCI_SUCCESS) {
754 		/* Can't do anything useful */
755 		mutex_unlock(&dev->mutex);
756 		return;
757 	}
758 
759 	new_rfk_state = value;
760 
761 	mutex_unlock(&dev->mutex);
762 
763 	if (rfkill_set_hw_state(rfkill, !new_rfk_state))
764 		bt_rfkill_set_block(data, true);
765 }
766 
767 static const struct rfkill_ops toshiba_rfk_ops = {
768 	.set_block = bt_rfkill_set_block,
769 	.poll = bt_rfkill_poll,
770 };
771 
772 static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
773 {
774 	u32 hci_result;
775 	u32 status;
776 
777 	hci_read1(dev, HCI_TR_BACKLIGHT, &status, &hci_result);
778 	*enabled = !status;
779 	return hci_result == HCI_SUCCESS ? 0 : -EIO;
780 }
781 
782 static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
783 {
784 	u32 hci_result;
785 	u32 value = !enable;
786 
787 	hci_write1(dev, HCI_TR_BACKLIGHT, value, &hci_result);
788 	return hci_result == HCI_SUCCESS ? 0 : -EIO;
789 }
790 
791 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
792 
793 static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
794 {
795 	u32 hci_result;
796 	u32 value;
797 	int brightness = 0;
798 
799 	if (dev->tr_backlight_supported) {
800 		bool enabled;
801 		int ret = get_tr_backlight_status(dev, &enabled);
802 		if (ret)
803 			return ret;
804 		if (enabled)
805 			return 0;
806 		brightness++;
807 	}
808 
809 	hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
810 	if (hci_result == HCI_SUCCESS)
811 		return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
812 
813 	return -EIO;
814 }
815 
816 static int get_lcd_brightness(struct backlight_device *bd)
817 {
818 	struct toshiba_acpi_dev *dev = bl_get_data(bd);
819 	return __get_lcd_brightness(dev);
820 }
821 
822 static int lcd_proc_show(struct seq_file *m, void *v)
823 {
824 	struct toshiba_acpi_dev *dev = m->private;
825 	int value;
826 	int levels;
827 
828 	if (!dev->backlight_dev)
829 		return -ENODEV;
830 
831 	levels = dev->backlight_dev->props.max_brightness + 1;
832 	value = get_lcd_brightness(dev->backlight_dev);
833 	if (value >= 0) {
834 		seq_printf(m, "brightness:              %d\n", value);
835 		seq_printf(m, "brightness_levels:       %d\n", levels);
836 		return 0;
837 	}
838 
839 	pr_err("Error reading LCD brightness\n");
840 	return -EIO;
841 }
842 
843 static int lcd_proc_open(struct inode *inode, struct file *file)
844 {
845 	return single_open(file, lcd_proc_show, PDE_DATA(inode));
846 }
847 
848 static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
849 {
850 	u32 hci_result;
851 
852 	if (dev->tr_backlight_supported) {
853 		bool enable = !value;
854 		int ret = set_tr_backlight_status(dev, enable);
855 		if (ret)
856 			return ret;
857 		if (value)
858 			value--;
859 	}
860 
861 	value = value << HCI_LCD_BRIGHTNESS_SHIFT;
862 	hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result);
863 	return hci_result == HCI_SUCCESS ? 0 : -EIO;
864 }
865 
866 static int set_lcd_status(struct backlight_device *bd)
867 {
868 	struct toshiba_acpi_dev *dev = bl_get_data(bd);
869 	return set_lcd_brightness(dev, bd->props.brightness);
870 }
871 
872 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
873 			      size_t count, loff_t *pos)
874 {
875 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
876 	char cmd[42];
877 	size_t len;
878 	int value;
879 	int ret;
880 	int levels = dev->backlight_dev->props.max_brightness + 1;
881 
882 	len = min(count, sizeof(cmd) - 1);
883 	if (copy_from_user(cmd, buf, len))
884 		return -EFAULT;
885 	cmd[len] = '\0';
886 
887 	if (sscanf(cmd, " brightness : %i", &value) == 1 &&
888 	    value >= 0 && value < levels) {
889 		ret = set_lcd_brightness(dev, value);
890 		if (ret == 0)
891 			ret = count;
892 	} else {
893 		ret = -EINVAL;
894 	}
895 	return ret;
896 }
897 
898 static const struct file_operations lcd_proc_fops = {
899 	.owner		= THIS_MODULE,
900 	.open		= lcd_proc_open,
901 	.read		= seq_read,
902 	.llseek		= seq_lseek,
903 	.release	= single_release,
904 	.write		= lcd_proc_write,
905 };
906 
907 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
908 {
909 	u32 hci_result;
910 
911 	hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
912 	return hci_result == HCI_SUCCESS ? 0 : -EIO;
913 }
914 
915 static int video_proc_show(struct seq_file *m, void *v)
916 {
917 	struct toshiba_acpi_dev *dev = m->private;
918 	u32 value;
919 	int ret;
920 
921 	ret = get_video_status(dev, &value);
922 	if (!ret) {
923 		int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
924 		int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
925 		int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
926 		seq_printf(m, "lcd_out:                 %d\n", is_lcd);
927 		seq_printf(m, "crt_out:                 %d\n", is_crt);
928 		seq_printf(m, "tv_out:                  %d\n", is_tv);
929 	}
930 
931 	return ret;
932 }
933 
934 static int video_proc_open(struct inode *inode, struct file *file)
935 {
936 	return single_open(file, video_proc_show, PDE_DATA(inode));
937 }
938 
939 static ssize_t video_proc_write(struct file *file, const char __user *buf,
940 				size_t count, loff_t *pos)
941 {
942 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
943 	char *cmd, *buffer;
944 	int ret;
945 	int value;
946 	int remain = count;
947 	int lcd_out = -1;
948 	int crt_out = -1;
949 	int tv_out = -1;
950 	u32 video_out;
951 
952 	cmd = kmalloc(count + 1, GFP_KERNEL);
953 	if (!cmd)
954 		return -ENOMEM;
955 	if (copy_from_user(cmd, buf, count)) {
956 		kfree(cmd);
957 		return -EFAULT;
958 	}
959 	cmd[count] = '\0';
960 
961 	buffer = cmd;
962 
963 	/* scan expression.  Multiple expressions may be delimited with ;
964 	 *
965 	 *  NOTE: to keep scanning simple, invalid fields are ignored
966 	 */
967 	while (remain) {
968 		if (sscanf(buffer, " lcd_out : %i", &value) == 1)
969 			lcd_out = value & 1;
970 		else if (sscanf(buffer, " crt_out : %i", &value) == 1)
971 			crt_out = value & 1;
972 		else if (sscanf(buffer, " tv_out : %i", &value) == 1)
973 			tv_out = value & 1;
974 		/* advance to one character past the next ; */
975 		do {
976 			++buffer;
977 			--remain;
978 		}
979 		while (remain && *(buffer - 1) != ';');
980 	}
981 
982 	kfree(cmd);
983 
984 	ret = get_video_status(dev, &video_out);
985 	if (!ret) {
986 		unsigned int new_video_out = video_out;
987 		if (lcd_out != -1)
988 			_set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
989 		if (crt_out != -1)
990 			_set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
991 		if (tv_out != -1)
992 			_set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
993 		/* To avoid unnecessary video disruption, only write the new
994 		 * video setting if something changed. */
995 		if (new_video_out != video_out)
996 			ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
997 	}
998 
999 	return ret ? ret : count;
1000 }
1001 
1002 static const struct file_operations video_proc_fops = {
1003 	.owner		= THIS_MODULE,
1004 	.open		= video_proc_open,
1005 	.read		= seq_read,
1006 	.llseek		= seq_lseek,
1007 	.release	= single_release,
1008 	.write		= video_proc_write,
1009 };
1010 
1011 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1012 {
1013 	u32 hci_result;
1014 
1015 	hci_read1(dev, HCI_FAN, status, &hci_result);
1016 	return hci_result == HCI_SUCCESS ? 0 : -EIO;
1017 }
1018 
1019 static int fan_proc_show(struct seq_file *m, void *v)
1020 {
1021 	struct toshiba_acpi_dev *dev = m->private;
1022 	int ret;
1023 	u32 value;
1024 
1025 	ret = get_fan_status(dev, &value);
1026 	if (!ret) {
1027 		seq_printf(m, "running:                 %d\n", (value > 0));
1028 		seq_printf(m, "force_on:                %d\n", dev->force_fan);
1029 	}
1030 
1031 	return ret;
1032 }
1033 
1034 static int fan_proc_open(struct inode *inode, struct file *file)
1035 {
1036 	return single_open(file, fan_proc_show, PDE_DATA(inode));
1037 }
1038 
1039 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1040 			      size_t count, loff_t *pos)
1041 {
1042 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1043 	char cmd[42];
1044 	size_t len;
1045 	int value;
1046 	u32 hci_result;
1047 
1048 	len = min(count, sizeof(cmd) - 1);
1049 	if (copy_from_user(cmd, buf, len))
1050 		return -EFAULT;
1051 	cmd[len] = '\0';
1052 
1053 	if (sscanf(cmd, " force_on : %i", &value) == 1 &&
1054 	    value >= 0 && value <= 1) {
1055 		hci_write1(dev, HCI_FAN, value, &hci_result);
1056 		if (hci_result != HCI_SUCCESS)
1057 			return -EIO;
1058 		else
1059 			dev->force_fan = value;
1060 	} else {
1061 		return -EINVAL;
1062 	}
1063 
1064 	return count;
1065 }
1066 
1067 static const struct file_operations fan_proc_fops = {
1068 	.owner		= THIS_MODULE,
1069 	.open		= fan_proc_open,
1070 	.read		= seq_read,
1071 	.llseek		= seq_lseek,
1072 	.release	= single_release,
1073 	.write		= fan_proc_write,
1074 };
1075 
1076 static int keys_proc_show(struct seq_file *m, void *v)
1077 {
1078 	struct toshiba_acpi_dev *dev = m->private;
1079 	u32 hci_result;
1080 	u32 value;
1081 
1082 	if (!dev->key_event_valid && dev->system_event_supported) {
1083 		hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1084 		if (hci_result == HCI_SUCCESS) {
1085 			dev->key_event_valid = 1;
1086 			dev->last_key_event = value;
1087 		} else if (hci_result == HCI_EMPTY) {
1088 			/* better luck next time */
1089 		} else if (hci_result == HCI_NOT_SUPPORTED) {
1090 			/* This is a workaround for an unresolved issue on
1091 			 * some machines where system events sporadically
1092 			 * become disabled. */
1093 			hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
1094 			pr_notice("Re-enabled hotkeys\n");
1095 		} else {
1096 			pr_err("Error reading hotkey status\n");
1097 			return -EIO;
1098 		}
1099 	}
1100 
1101 	seq_printf(m, "hotkey_ready:            %d\n", dev->key_event_valid);
1102 	seq_printf(m, "hotkey:                  0x%04x\n", dev->last_key_event);
1103 	return 0;
1104 }
1105 
1106 static int keys_proc_open(struct inode *inode, struct file *file)
1107 {
1108 	return single_open(file, keys_proc_show, PDE_DATA(inode));
1109 }
1110 
1111 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1112 			       size_t count, loff_t *pos)
1113 {
1114 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1115 	char cmd[42];
1116 	size_t len;
1117 	int value;
1118 
1119 	len = min(count, sizeof(cmd) - 1);
1120 	if (copy_from_user(cmd, buf, len))
1121 		return -EFAULT;
1122 	cmd[len] = '\0';
1123 
1124 	if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
1125 		dev->key_event_valid = 0;
1126 	} else {
1127 		return -EINVAL;
1128 	}
1129 
1130 	return count;
1131 }
1132 
1133 static const struct file_operations keys_proc_fops = {
1134 	.owner		= THIS_MODULE,
1135 	.open		= keys_proc_open,
1136 	.read		= seq_read,
1137 	.llseek		= seq_lseek,
1138 	.release	= single_release,
1139 	.write		= keys_proc_write,
1140 };
1141 
1142 static int version_proc_show(struct seq_file *m, void *v)
1143 {
1144 	seq_printf(m, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
1145 	seq_printf(m, "proc_interface:          %d\n", PROC_INTERFACE_VERSION);
1146 	return 0;
1147 }
1148 
1149 static int version_proc_open(struct inode *inode, struct file *file)
1150 {
1151 	return single_open(file, version_proc_show, PDE_DATA(inode));
1152 }
1153 
1154 static const struct file_operations version_proc_fops = {
1155 	.owner		= THIS_MODULE,
1156 	.open		= version_proc_open,
1157 	.read		= seq_read,
1158 	.llseek		= seq_lseek,
1159 	.release	= single_release,
1160 };
1161 
1162 /* proc and module init
1163  */
1164 
1165 #define PROC_TOSHIBA		"toshiba"
1166 
1167 static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1168 {
1169 	if (dev->backlight_dev)
1170 		proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1171 				 &lcd_proc_fops, dev);
1172 	if (dev->video_supported)
1173 		proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1174 				 &video_proc_fops, dev);
1175 	if (dev->fan_supported)
1176 		proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1177 				 &fan_proc_fops, dev);
1178 	if (dev->hotkey_dev)
1179 		proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1180 				 &keys_proc_fops, dev);
1181 	proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1182 			 &version_proc_fops, dev);
1183 }
1184 
1185 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1186 {
1187 	if (dev->backlight_dev)
1188 		remove_proc_entry("lcd", toshiba_proc_dir);
1189 	if (dev->video_supported)
1190 		remove_proc_entry("video", toshiba_proc_dir);
1191 	if (dev->fan_supported)
1192 		remove_proc_entry("fan", toshiba_proc_dir);
1193 	if (dev->hotkey_dev)
1194 		remove_proc_entry("keys", toshiba_proc_dir);
1195 	remove_proc_entry("version", toshiba_proc_dir);
1196 }
1197 
1198 static const struct backlight_ops toshiba_backlight_data = {
1199 	.options = BL_CORE_SUSPENDRESUME,
1200 	.get_brightness = get_lcd_brightness,
1201 	.update_status  = set_lcd_status,
1202 };
1203 
1204 /*
1205  * Sysfs files
1206  */
1207 
1208 static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1209 					 struct device_attribute *attr,
1210 					 const char *buf, size_t count)
1211 {
1212 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1213 	int mode = -1;
1214 	int time = -1;
1215 
1216 	if (sscanf(buf, "%i", &mode) != 1 && (mode != 2 || mode != 1))
1217 		return -EINVAL;
1218 
1219 	/* Set the Keyboard Backlight Mode where:
1220 	 * Mode - Auto (2) | FN-Z (1)
1221 	 *	Auto - KBD backlight turns off automatically in given time
1222 	 *	FN-Z - KBD backlight "toggles" when hotkey pressed
1223 	 */
1224 	if (mode != -1 && toshiba->kbd_mode != mode) {
1225 		time = toshiba->kbd_time << HCI_MISC_SHIFT;
1226 		time = time + toshiba->kbd_mode;
1227 		if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1228 			return -EIO;
1229 		toshiba->kbd_mode = mode;
1230 	}
1231 
1232 	return count;
1233 }
1234 
1235 static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1236 					struct device_attribute *attr,
1237 					char *buf)
1238 {
1239 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1240 	u32 time;
1241 
1242 	if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1243 		return -EIO;
1244 
1245 	return sprintf(buf, "%i\n", time & 0x07);
1246 }
1247 
1248 static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1249 					    struct device_attribute *attr,
1250 					    const char *buf, size_t count)
1251 {
1252 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1253 	int time = -1;
1254 
1255 	if (sscanf(buf, "%i", &time) != 1 && (time < 0 || time > 60))
1256 		return -EINVAL;
1257 
1258 	/* Set the Keyboard Backlight Timeout: 0-60 seconds */
1259 	if (time != -1 && toshiba->kbd_time != time) {
1260 		time = time << HCI_MISC_SHIFT;
1261 		time = (toshiba->kbd_mode == SCI_KBD_MODE_AUTO) ?
1262 							time + 1 : time + 2;
1263 		if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1264 			return -EIO;
1265 		toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1266 	}
1267 
1268 	return count;
1269 }
1270 
1271 static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1272 					   struct device_attribute *attr,
1273 					   char *buf)
1274 {
1275 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1276 	u32 time;
1277 
1278 	if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1279 		return -EIO;
1280 
1281 	return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1282 }
1283 
1284 static ssize_t toshiba_touchpad_store(struct device *dev,
1285 				      struct device_attribute *attr,
1286 				      const char *buf, size_t count)
1287 {
1288 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1289 	int state;
1290 
1291 	/* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1292 	if (sscanf(buf, "%i", &state) == 1 && (state == 0 || state == 1)) {
1293 		if (toshiba_touchpad_set(toshiba, state) < 0)
1294 			return -EIO;
1295 	}
1296 
1297 	return count;
1298 }
1299 
1300 static ssize_t toshiba_touchpad_show(struct device *dev,
1301 				     struct device_attribute *attr, char *buf)
1302 {
1303 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1304 	u32 state;
1305 	int ret;
1306 
1307 	ret = toshiba_touchpad_get(toshiba, &state);
1308 	if (ret < 0)
1309 		return ret;
1310 
1311 	return sprintf(buf, "%i\n", state);
1312 }
1313 
1314 static ssize_t toshiba_position_show(struct device *dev,
1315 				     struct device_attribute *attr, char *buf)
1316 {
1317 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1318 	u32 xyval, zval, tmp;
1319 	u16 x, y, z;
1320 	int ret;
1321 
1322 	xyval = zval = 0;
1323 	ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1324 	if (ret < 0)
1325 		return ret;
1326 
1327 	x = xyval & HCI_ACCEL_MASK;
1328 	tmp = xyval >> HCI_MISC_SHIFT;
1329 	y = tmp & HCI_ACCEL_MASK;
1330 	z = zval & HCI_ACCEL_MASK;
1331 
1332 	return sprintf(buf, "%d %d %d\n", x, y, z);
1333 }
1334 
1335 static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1336 		   toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1337 static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1338 		   toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
1339 static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1340 		   toshiba_touchpad_show, toshiba_touchpad_store);
1341 static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
1342 
1343 static struct attribute *toshiba_attributes[] = {
1344 	&dev_attr_kbd_backlight_mode.attr,
1345 	&dev_attr_kbd_backlight_timeout.attr,
1346 	&dev_attr_touchpad.attr,
1347 	&dev_attr_position.attr,
1348 	NULL,
1349 };
1350 
1351 static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
1352 					struct attribute *attr, int idx)
1353 {
1354 	struct device *dev = container_of(kobj, struct device, kobj);
1355 	struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
1356 	bool exists = true;
1357 
1358 	if (attr == &dev_attr_kbd_backlight_mode.attr)
1359 		exists = (drv->kbd_illum_supported) ? true : false;
1360 	else if (attr == &dev_attr_kbd_backlight_timeout.attr)
1361 		exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
1362 	else if (attr == &dev_attr_touchpad.attr)
1363 		exists = (drv->touchpad_supported) ? true : false;
1364 	else if (attr == &dev_attr_position.attr)
1365 		exists = (drv->accelerometer_supported) ? true : false;
1366 
1367 	return exists ? attr->mode : 0;
1368 }
1369 
1370 static struct attribute_group toshiba_attr_group = {
1371 	.is_visible = toshiba_sysfs_is_visible,
1372 	.attrs = toshiba_attributes,
1373 };
1374 
1375 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
1376 				      struct serio *port)
1377 {
1378 	if (str & 0x20)
1379 		return false;
1380 
1381 	if (unlikely(data == 0xe0))
1382 		return false;
1383 
1384 	if ((data & 0x7f) == TOS1900_FN_SCAN) {
1385 		schedule_work(&toshiba_acpi->hotkey_work);
1386 		return true;
1387 	}
1388 
1389 	return false;
1390 }
1391 
1392 static void toshiba_acpi_hotkey_work(struct work_struct *work)
1393 {
1394 	acpi_handle ec_handle = ec_get_handle();
1395 	acpi_status status;
1396 
1397 	if (!ec_handle)
1398 		return;
1399 
1400 	status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
1401 	if (ACPI_FAILURE(status))
1402 		pr_err("ACPI NTFY method execution failed\n");
1403 }
1404 
1405 /*
1406  * Returns hotkey scancode, or < 0 on failure.
1407  */
1408 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
1409 {
1410 	unsigned long long value;
1411 	acpi_status status;
1412 
1413 	status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
1414 				      NULL, &value);
1415 	if (ACPI_FAILURE(status)) {
1416 		pr_err("ACPI INFO method execution failed\n");
1417 		return -EIO;
1418 	}
1419 
1420 	return value;
1421 }
1422 
1423 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
1424 				       int scancode)
1425 {
1426 	if (scancode == 0x100)
1427 		return;
1428 
1429 	/* act on key press; ignore key release */
1430 	if (scancode & 0x80)
1431 		return;
1432 
1433 	if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
1434 		pr_info("Unknown key %x\n", scancode);
1435 }
1436 
1437 static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
1438 {
1439 	acpi_status status;
1440 	acpi_handle ec_handle;
1441 	int error;
1442 	u32 hci_result;
1443 
1444 	dev->hotkey_dev = input_allocate_device();
1445 	if (!dev->hotkey_dev)
1446 		return -ENOMEM;
1447 
1448 	dev->hotkey_dev->name = "Toshiba input device";
1449 	dev->hotkey_dev->phys = "toshiba_acpi/input0";
1450 	dev->hotkey_dev->id.bustype = BUS_HOST;
1451 
1452 	error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL);
1453 	if (error)
1454 		goto err_free_dev;
1455 
1456 	/*
1457 	 * For some machines the SCI responsible for providing hotkey
1458 	 * notification doesn't fire. We can trigger the notification
1459 	 * whenever the Fn key is pressed using the NTFY method, if
1460 	 * supported, so if it's present set up an i8042 key filter
1461 	 * for this purpose.
1462 	 */
1463 	status = AE_ERROR;
1464 	ec_handle = ec_get_handle();
1465 	if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
1466 		INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
1467 
1468 		error = i8042_install_filter(toshiba_acpi_i8042_filter);
1469 		if (error) {
1470 			pr_err("Error installing key filter\n");
1471 			goto err_free_keymap;
1472 		}
1473 
1474 		dev->ntfy_supported = 1;
1475 	}
1476 
1477 	/*
1478 	 * Determine hotkey query interface. Prefer using the INFO
1479 	 * method when it is available.
1480 	 */
1481 	if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
1482 		dev->info_supported = 1;
1483 	else {
1484 		hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
1485 		if (hci_result == HCI_SUCCESS)
1486 			dev->system_event_supported = 1;
1487 	}
1488 
1489 	if (!dev->info_supported && !dev->system_event_supported) {
1490 		pr_warn("No hotkey query interface found\n");
1491 		goto err_remove_filter;
1492 	}
1493 
1494 	status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
1495 	if (ACPI_FAILURE(status)) {
1496 		pr_info("Unable to enable hotkeys\n");
1497 		error = -ENODEV;
1498 		goto err_remove_filter;
1499 	}
1500 
1501 	error = input_register_device(dev->hotkey_dev);
1502 	if (error) {
1503 		pr_info("Unable to register input device\n");
1504 		goto err_remove_filter;
1505 	}
1506 
1507 	hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result);
1508 	return 0;
1509 
1510  err_remove_filter:
1511 	if (dev->ntfy_supported)
1512 		i8042_remove_filter(toshiba_acpi_i8042_filter);
1513  err_free_keymap:
1514 	sparse_keymap_free(dev->hotkey_dev);
1515  err_free_dev:
1516 	input_free_device(dev->hotkey_dev);
1517 	dev->hotkey_dev = NULL;
1518 	return error;
1519 }
1520 
1521 static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
1522 {
1523 	struct backlight_properties props;
1524 	int brightness;
1525 	int ret;
1526 	bool enabled;
1527 
1528 	/*
1529 	 * Some machines don't support the backlight methods at all, and
1530 	 * others support it read-only. Either of these is pretty useless,
1531 	 * so only register the backlight device if the backlight method
1532 	 * supports both reads and writes.
1533 	 */
1534 	brightness = __get_lcd_brightness(dev);
1535 	if (brightness < 0)
1536 		return 0;
1537 	ret = set_lcd_brightness(dev, brightness);
1538 	if (ret) {
1539 		pr_debug("Backlight method is read-only, disabling backlight support\n");
1540 		return 0;
1541 	}
1542 
1543 	/* Determine whether or not BIOS supports transflective backlight */
1544 	ret = get_tr_backlight_status(dev, &enabled);
1545 	dev->tr_backlight_supported = !ret;
1546 
1547 	memset(&props, 0, sizeof(props));
1548 	props.type = BACKLIGHT_PLATFORM;
1549 	props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
1550 
1551 	/* adding an extra level and having 0 change to transflective mode */
1552 	if (dev->tr_backlight_supported)
1553 		props.max_brightness++;
1554 
1555 	dev->backlight_dev = backlight_device_register("toshiba",
1556 						       &dev->acpi_dev->dev,
1557 						       dev,
1558 						       &toshiba_backlight_data,
1559 						       &props);
1560 	if (IS_ERR(dev->backlight_dev)) {
1561 		ret = PTR_ERR(dev->backlight_dev);
1562 		pr_err("Could not register toshiba backlight device\n");
1563 		dev->backlight_dev = NULL;
1564 		return ret;
1565 	}
1566 
1567 	dev->backlight_dev->props.brightness = brightness;
1568 	return 0;
1569 }
1570 
1571 static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
1572 {
1573 	struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1574 
1575 	remove_toshiba_proc_entries(dev);
1576 
1577 	if (dev->sysfs_created)
1578 		sysfs_remove_group(&dev->acpi_dev->dev.kobj,
1579 				   &toshiba_attr_group);
1580 
1581 	if (dev->ntfy_supported) {
1582 		i8042_remove_filter(toshiba_acpi_i8042_filter);
1583 		cancel_work_sync(&dev->hotkey_work);
1584 	}
1585 
1586 	if (dev->hotkey_dev) {
1587 		input_unregister_device(dev->hotkey_dev);
1588 		sparse_keymap_free(dev->hotkey_dev);
1589 	}
1590 
1591 	if (dev->bt_rfk) {
1592 		rfkill_unregister(dev->bt_rfk);
1593 		rfkill_destroy(dev->bt_rfk);
1594 	}
1595 
1596 	if (dev->backlight_dev)
1597 		backlight_device_unregister(dev->backlight_dev);
1598 
1599 	if (dev->illumination_supported)
1600 		led_classdev_unregister(&dev->led_dev);
1601 
1602 	if (dev->kbd_led_registered)
1603 		led_classdev_unregister(&dev->kbd_led);
1604 
1605 	if (dev->eco_supported)
1606 		led_classdev_unregister(&dev->eco_led);
1607 
1608 	if (toshiba_acpi)
1609 		toshiba_acpi = NULL;
1610 
1611 	kfree(dev);
1612 
1613 	return 0;
1614 }
1615 
1616 static const char *find_hci_method(acpi_handle handle)
1617 {
1618 	if (acpi_has_method(handle, "GHCI"))
1619 		return "GHCI";
1620 
1621 	if (acpi_has_method(handle, "SPFC"))
1622 		return "SPFC";
1623 
1624 	return NULL;
1625 }
1626 
1627 static int toshiba_acpi_add(struct acpi_device *acpi_dev)
1628 {
1629 	struct toshiba_acpi_dev *dev;
1630 	const char *hci_method;
1631 	u32 dummy;
1632 	bool bt_present;
1633 	int ret = 0;
1634 
1635 	if (toshiba_acpi)
1636 		return -EBUSY;
1637 
1638 	pr_info("Toshiba Laptop ACPI Extras version %s\n",
1639 	       TOSHIBA_ACPI_VERSION);
1640 
1641 	hci_method = find_hci_method(acpi_dev->handle);
1642 	if (!hci_method) {
1643 		pr_err("HCI interface not found\n");
1644 		return -ENODEV;
1645 	}
1646 
1647 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1648 	if (!dev)
1649 		return -ENOMEM;
1650 	dev->acpi_dev = acpi_dev;
1651 	dev->method_hci = hci_method;
1652 	acpi_dev->driver_data = dev;
1653 	dev_set_drvdata(&acpi_dev->dev, dev);
1654 
1655 	if (toshiba_acpi_setup_keyboard(dev))
1656 		pr_info("Unable to activate hotkeys\n");
1657 
1658 	mutex_init(&dev->mutex);
1659 
1660 	ret = toshiba_acpi_setup_backlight(dev);
1661 	if (ret)
1662 		goto error;
1663 
1664 	/* Register rfkill switch for Bluetooth */
1665 	if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
1666 		dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
1667 					   &acpi_dev->dev,
1668 					   RFKILL_TYPE_BLUETOOTH,
1669 					   &toshiba_rfk_ops,
1670 					   dev);
1671 		if (!dev->bt_rfk) {
1672 			pr_err("unable to allocate rfkill device\n");
1673 			ret = -ENOMEM;
1674 			goto error;
1675 		}
1676 
1677 		ret = rfkill_register(dev->bt_rfk);
1678 		if (ret) {
1679 			pr_err("unable to register rfkill device\n");
1680 			rfkill_destroy(dev->bt_rfk);
1681 			goto error;
1682 		}
1683 	}
1684 
1685 	if (toshiba_illumination_available(dev)) {
1686 		dev->led_dev.name = "toshiba::illumination";
1687 		dev->led_dev.max_brightness = 1;
1688 		dev->led_dev.brightness_set = toshiba_illumination_set;
1689 		dev->led_dev.brightness_get = toshiba_illumination_get;
1690 		if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
1691 			dev->illumination_supported = 1;
1692 	}
1693 
1694 	if (toshiba_eco_mode_available(dev)) {
1695 		dev->eco_led.name = "toshiba::eco_mode";
1696 		dev->eco_led.max_brightness = 1;
1697 		dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
1698 		dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
1699 		if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
1700 			dev->eco_supported = 1;
1701 	}
1702 
1703 	ret = toshiba_kbd_illum_status_get(dev, &dummy);
1704 	if (!ret) {
1705 		dev->kbd_time = dummy >> HCI_MISC_SHIFT;
1706 		dev->kbd_mode = dummy & 0x07;
1707 	}
1708 	dev->kbd_illum_supported = !ret;
1709 	/*
1710 	 * Only register the LED if KBD illumination is supported
1711 	 * and the keyboard backlight operation mode is set to FN-Z
1712 	 */
1713 	if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
1714 		dev->kbd_led.name = "toshiba::kbd_backlight";
1715 		dev->kbd_led.max_brightness = 1;
1716 		dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
1717 		dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
1718 		if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
1719 			dev->kbd_led_registered = 1;
1720 	}
1721 
1722 	ret = toshiba_touchpad_get(dev, &dummy);
1723 	dev->touchpad_supported = !ret;
1724 
1725 	ret = toshiba_accelerometer_supported(dev);
1726 	dev->accelerometer_supported = !ret;
1727 
1728 	/* Determine whether or not BIOS supports fan and video interfaces */
1729 
1730 	ret = get_video_status(dev, &dummy);
1731 	dev->video_supported = !ret;
1732 
1733 	ret = get_fan_status(dev, &dummy);
1734 	dev->fan_supported = !ret;
1735 
1736 	ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
1737 				 &toshiba_attr_group);
1738 	if (ret) {
1739 		dev->sysfs_created = 0;
1740 		goto error;
1741 	}
1742 	dev->sysfs_created = !ret;
1743 
1744 	create_toshiba_proc_entries(dev);
1745 
1746 	toshiba_acpi = dev;
1747 
1748 	return 0;
1749 
1750 error:
1751 	toshiba_acpi_remove(acpi_dev);
1752 	return ret;
1753 }
1754 
1755 static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1756 {
1757 	struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1758 	u32 hci_result, value;
1759 	int retries = 3;
1760 	int scancode;
1761 
1762 	if (event != 0x80)
1763 		return;
1764 
1765 	if (dev->info_supported) {
1766 		scancode = toshiba_acpi_query_hotkey(dev);
1767 		if (scancode < 0)
1768 			pr_err("Failed to query hotkey event\n");
1769 		else if (scancode != 0)
1770 			toshiba_acpi_report_hotkey(dev, scancode);
1771 	} else if (dev->system_event_supported) {
1772 		do {
1773 			hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1774 			switch (hci_result) {
1775 			case HCI_SUCCESS:
1776 				toshiba_acpi_report_hotkey(dev, (int)value);
1777 				break;
1778 			case HCI_NOT_SUPPORTED:
1779 				/*
1780 				 * This is a workaround for an unresolved
1781 				 * issue on some machines where system events
1782 				 * sporadically become disabled.
1783 				 */
1784 				hci_write1(dev, HCI_SYSTEM_EVENT, 1,
1785 					   &hci_result);
1786 				pr_notice("Re-enabled hotkeys\n");
1787 				/* fall through */
1788 			default:
1789 				retries--;
1790 				break;
1791 			}
1792 		} while (retries && hci_result != HCI_EMPTY);
1793 	}
1794 }
1795 
1796 #ifdef CONFIG_PM_SLEEP
1797 static int toshiba_acpi_suspend(struct device *device)
1798 {
1799 	struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
1800 	u32 result;
1801 
1802 	if (dev->hotkey_dev)
1803 		hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result);
1804 
1805 	return 0;
1806 }
1807 
1808 static int toshiba_acpi_resume(struct device *device)
1809 {
1810 	struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
1811 	u32 result;
1812 
1813 	if (dev->hotkey_dev)
1814 		hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result);
1815 
1816 	return 0;
1817 }
1818 #endif
1819 
1820 static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
1821 			 toshiba_acpi_suspend, toshiba_acpi_resume);
1822 
1823 static struct acpi_driver toshiba_acpi_driver = {
1824 	.name	= "Toshiba ACPI driver",
1825 	.owner	= THIS_MODULE,
1826 	.ids	= toshiba_device_ids,
1827 	.flags	= ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1828 	.ops	= {
1829 		.add		= toshiba_acpi_add,
1830 		.remove		= toshiba_acpi_remove,
1831 		.notify		= toshiba_acpi_notify,
1832 	},
1833 	.drv.pm	= &toshiba_acpi_pm,
1834 };
1835 
1836 static int __init toshiba_acpi_init(void)
1837 {
1838 	int ret;
1839 
1840 	/*
1841 	 * Machines with this WMI guid aren't supported due to bugs in
1842 	 * their AML. This check relies on wmi initializing before
1843 	 * toshiba_acpi to guarantee guids have been identified.
1844 	 */
1845 	if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
1846 		return -ENODEV;
1847 
1848 	toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1849 	if (!toshiba_proc_dir) {
1850 		pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
1851 		return -ENODEV;
1852 	}
1853 
1854 	ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1855 	if (ret) {
1856 		pr_err("Failed to register ACPI driver: %d\n", ret);
1857 		remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1858 	}
1859 
1860 	return ret;
1861 }
1862 
1863 static void __exit toshiba_acpi_exit(void)
1864 {
1865 	acpi_bus_unregister_driver(&toshiba_acpi_driver);
1866 	if (toshiba_proc_dir)
1867 		remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1868 }
1869 
1870 module_init(toshiba_acpi_init);
1871 module_exit(toshiba_acpi_exit);
1872