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