xref: /linux/drivers/platform/x86/toshiba_acpi.c (revision bb26f1893a086db77edca73529d5f59a8338ff30)
1b4f9fe12SLen Brown /*
2b4f9fe12SLen Brown  *  toshiba_acpi.c - Toshiba Laptop ACPI Extras
3b4f9fe12SLen Brown  *
4b4f9fe12SLen Brown  *  Copyright (C) 2002-2004 John Belmonte
5b4f9fe12SLen Brown  *  Copyright (C) 2008 Philip Langdale
66c3f6e6cSPierre Ducroquet  *  Copyright (C) 2010 Pierre Ducroquet
77216d702SAzael Avalos  *  Copyright (C) 2014-2015 Azael Avalos
8b4f9fe12SLen Brown  *
9b4f9fe12SLen Brown  *  This program is free software; you can redistribute it and/or modify
10b4f9fe12SLen Brown  *  it under the terms of the GNU General Public License as published by
11b4f9fe12SLen Brown  *  the Free Software Foundation; either version 2 of the License, or
12b4f9fe12SLen Brown  *  (at your option) any later version.
13b4f9fe12SLen Brown  *
14b4f9fe12SLen Brown  *  This program is distributed in the hope that it will be useful,
15b4f9fe12SLen Brown  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16b4f9fe12SLen Brown  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17b4f9fe12SLen Brown  *  GNU General Public License for more details.
18b4f9fe12SLen Brown  *
19c57c0fa4SDarren Hart  *  The full GNU General Public License is included in this distribution in
20c57c0fa4SDarren Hart  *  the file called "COPYING".
21b4f9fe12SLen Brown  *
22b4f9fe12SLen Brown  *  The devolpment page for this driver is located at
23b4f9fe12SLen Brown  *  http://memebeam.org/toys/ToshibaAcpiDriver.
24b4f9fe12SLen Brown  *
25b4f9fe12SLen Brown  *  Credits:
26b4f9fe12SLen Brown  *	Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27b4f9fe12SLen Brown  *		engineering the Windows drivers
28b4f9fe12SLen Brown  *	Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29b4f9fe12SLen Brown  *	Rob Miller - TV out and hotkeys help
30b4f9fe12SLen Brown  */
31b4f9fe12SLen Brown 
327e33460dSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
337e33460dSJoe Perches 
347216d702SAzael Avalos #define TOSHIBA_ACPI_VERSION	"0.21"
35b4f9fe12SLen Brown #define PROC_INTERFACE_VERSION	1
36b4f9fe12SLen Brown 
37b4f9fe12SLen Brown #include <linux/kernel.h>
38b4f9fe12SLen Brown #include <linux/module.h>
39b4f9fe12SLen Brown #include <linux/init.h>
40b4f9fe12SLen Brown #include <linux/types.h>
41b4f9fe12SLen Brown #include <linux/proc_fs.h>
42936c8bcdSAlexey Dobriyan #include <linux/seq_file.h>
43b4f9fe12SLen Brown #include <linux/backlight.h>
44b4f9fe12SLen Brown #include <linux/rfkill.h>
456335e4d5SMatthew Garrett #include <linux/input.h>
46384a7cd9SDmitry Torokhov #include <linux/input/sparse-keymap.h>
476c3f6e6cSPierre Ducroquet #include <linux/leds.h>
485a0e3ad6STejun Heo #include <linux/slab.h>
4929cd293fSSeth Forshee #include <linux/workqueue.h>
5029cd293fSSeth Forshee #include <linux/i8042.h>
518b48463fSLv Zheng #include <linux/acpi.h>
52b5163992SAzael Avalos #include <linux/uaccess.h>
53b4f9fe12SLen Brown 
54b4f9fe12SLen Brown MODULE_AUTHOR("John Belmonte");
55b4f9fe12SLen Brown MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
56b4f9fe12SLen Brown MODULE_LICENSE("GPL");
57b4f9fe12SLen Brown 
58f11f999eSSeth Forshee #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
59f11f999eSSeth Forshee 
6029cd293fSSeth Forshee /* Scan code for Fn key on TOS1900 models */
6129cd293fSSeth Forshee #define TOS1900_FN_SCAN		0x6e
6229cd293fSSeth Forshee 
63b4f9fe12SLen Brown /* Toshiba ACPI method paths */
64b4f9fe12SLen Brown #define METHOD_VIDEO_OUT	"\\_SB_.VALX.DSSX"
65b4f9fe12SLen Brown 
66e0769fe6SDarren Hart /*
67e0769fe6SDarren Hart  * The Toshiba configuration interface is composed of the HCI and the SCI,
68258c5903SAzael Avalos  * which are defined as follows:
69b4f9fe12SLen Brown  *
70b4f9fe12SLen Brown  * HCI is Toshiba's "Hardware Control Interface" which is supposed to
71b4f9fe12SLen Brown  * be uniform across all their models.  Ideally we would just call
72b4f9fe12SLen Brown  * dedicated ACPI methods instead of using this primitive interface.
73b4f9fe12SLen Brown  * However the ACPI methods seem to be incomplete in some areas (for
74b4f9fe12SLen Brown  * example they allow setting, but not reading, the LCD brightness value),
75b4f9fe12SLen Brown  * so this is still useful.
7684a6273fSAzael Avalos  *
7784a6273fSAzael Avalos  * SCI stands for "System Configuration Interface" which aim is to
7884a6273fSAzael Avalos  * conceal differences in hardware between different models.
79b4f9fe12SLen Brown  */
80b4f9fe12SLen Brown 
81258c5903SAzael Avalos #define TCI_WORDS			6
82b4f9fe12SLen Brown 
83b4f9fe12SLen Brown /* operations */
84b4f9fe12SLen Brown #define HCI_SET				0xff00
85b4f9fe12SLen Brown #define HCI_GET				0xfe00
8684a6273fSAzael Avalos #define SCI_OPEN			0xf100
8784a6273fSAzael Avalos #define SCI_CLOSE			0xf200
8884a6273fSAzael Avalos #define SCI_GET				0xf300
8984a6273fSAzael Avalos #define SCI_SET				0xf400
90b4f9fe12SLen Brown 
91b4f9fe12SLen Brown /* return codes */
921864bbc2SAzael Avalos #define TOS_SUCCESS			0x0000
931864bbc2SAzael Avalos #define TOS_OPEN_CLOSE_OK		0x0044
941864bbc2SAzael Avalos #define TOS_FAILURE			0x1000
951864bbc2SAzael Avalos #define TOS_NOT_SUPPORTED		0x8000
961864bbc2SAzael Avalos #define TOS_ALREADY_OPEN		0x8100
971864bbc2SAzael Avalos #define TOS_NOT_OPENED			0x8200
981864bbc2SAzael Avalos #define TOS_INPUT_DATA_ERROR		0x8300
991864bbc2SAzael Avalos #define TOS_WRITE_PROTECTED		0x8400
1001864bbc2SAzael Avalos #define TOS_NOT_PRESENT			0x8600
1011864bbc2SAzael Avalos #define TOS_FIFO_EMPTY			0x8c00
1021864bbc2SAzael Avalos #define TOS_DATA_NOT_AVAILABLE		0x8d20
1031864bbc2SAzael Avalos #define TOS_NOT_INITIALIZED		0x8d50
10498fc4ec6SAzael Avalos #define TOS_NOT_INSTALLED		0x8e00
105b4f9fe12SLen Brown 
106b4f9fe12SLen Brown /* registers */
107b4f9fe12SLen Brown #define HCI_FAN				0x0004
108121b7b0dSAkio Idehara #define HCI_TR_BACKLIGHT		0x0005
109b4f9fe12SLen Brown #define HCI_SYSTEM_EVENT		0x0016
110b4f9fe12SLen Brown #define HCI_VIDEO_OUT			0x001c
111b4f9fe12SLen Brown #define HCI_HOTKEY_EVENT		0x001e
112b4f9fe12SLen Brown #define HCI_LCD_BRIGHTNESS		0x002a
113b4f9fe12SLen Brown #define HCI_WIRELESS			0x0056
1145a2813e9SAzael Avalos #define HCI_ACCELEROMETER		0x006d
115360f0f39SAzael Avalos #define HCI_KBD_ILLUMINATION		0x0095
116def6c4e2SAzael Avalos #define HCI_ECO_MODE			0x0097
1175a2813e9SAzael Avalos #define HCI_ACCELEROMETER2		0x00a6
11856e6b353SAzael Avalos #define HCI_SYSTEM_INFO			0xc000
11935d53ceaSAzael Avalos #define SCI_PANEL_POWER_ON		0x010d
120fdb79081SAzael Avalos #define SCI_ILLUMINATION		0x014e
121e26ffe51SAzael Avalos #define SCI_USB_SLEEP_CHARGE		0x0150
122360f0f39SAzael Avalos #define SCI_KBD_ILLUM_STATUS		0x015c
123172ce0a9SAzael Avalos #define SCI_USB_SLEEP_MUSIC		0x015e
12417fe4b3dSAzael Avalos #define SCI_USB_THREE			0x0169
1259d8658acSAzael Avalos #define SCI_TOUCHPAD			0x050e
126bae84195SAzael Avalos #define SCI_KBD_FUNCTION_KEYS		0x0522
127b4f9fe12SLen Brown 
128b4f9fe12SLen Brown /* field definitions */
1295a2813e9SAzael Avalos #define HCI_ACCEL_MASK			0x7fff
13029cd293fSSeth Forshee #define HCI_HOTKEY_DISABLE		0x0b
13129cd293fSSeth Forshee #define HCI_HOTKEY_ENABLE		0x09
132fb42d1f4SAzael Avalos #define HCI_HOTKEY_SPECIAL_FUNCTIONS	0x10
133b4f9fe12SLen Brown #define HCI_LCD_BRIGHTNESS_BITS		3
134b4f9fe12SLen Brown #define HCI_LCD_BRIGHTNESS_SHIFT	(16-HCI_LCD_BRIGHTNESS_BITS)
135b4f9fe12SLen Brown #define HCI_LCD_BRIGHTNESS_LEVELS	(1 << HCI_LCD_BRIGHTNESS_BITS)
136360f0f39SAzael Avalos #define HCI_MISC_SHIFT			0x10
13756e6b353SAzael Avalos #define HCI_SYSTEM_TYPE1		0x10
13856e6b353SAzael Avalos #define HCI_SYSTEM_TYPE2		0x11
139b4f9fe12SLen Brown #define HCI_VIDEO_OUT_LCD		0x1
140b4f9fe12SLen Brown #define HCI_VIDEO_OUT_CRT		0x2
141b4f9fe12SLen Brown #define HCI_VIDEO_OUT_TV		0x4
142b4f9fe12SLen Brown #define HCI_WIRELESS_KILL_SWITCH	0x01
143b4f9fe12SLen Brown #define HCI_WIRELESS_BT_PRESENT		0x0f
144b4f9fe12SLen Brown #define HCI_WIRELESS_BT_ATTACH		0x40
145b4f9fe12SLen Brown #define HCI_WIRELESS_BT_POWER		0x80
14693f8c16dSAzael Avalos #define SCI_KBD_MODE_MASK		0x1f
147360f0f39SAzael Avalos #define SCI_KBD_MODE_FNZ		0x1
148360f0f39SAzael Avalos #define SCI_KBD_MODE_AUTO		0x2
14993f8c16dSAzael Avalos #define SCI_KBD_MODE_ON			0x8
15093f8c16dSAzael Avalos #define SCI_KBD_MODE_OFF		0x10
15193f8c16dSAzael Avalos #define SCI_KBD_TIME_MAX		0x3c001a
152e26ffe51SAzael Avalos #define SCI_USB_CHARGE_MODE_MASK	0xff
153c8c91842SAzael Avalos #define SCI_USB_CHARGE_DISABLED		0x00
154c8c91842SAzael Avalos #define SCI_USB_CHARGE_ALTERNATE	0x09
155c8c91842SAzael Avalos #define SCI_USB_CHARGE_TYPICAL		0x11
156c8c91842SAzael Avalos #define SCI_USB_CHARGE_AUTO		0x21
157182bcaa5SAzael Avalos #define SCI_USB_CHARGE_BAT_MASK		0x7
158182bcaa5SAzael Avalos #define SCI_USB_CHARGE_BAT_LVL_OFF	0x1
159182bcaa5SAzael Avalos #define SCI_USB_CHARGE_BAT_LVL_ON	0x4
160182bcaa5SAzael Avalos #define SCI_USB_CHARGE_BAT_LVL		0x0200
161bb3fe01fSAzael Avalos #define SCI_USB_CHARGE_RAPID_DSP	0x0300
162b4f9fe12SLen Brown 
163135740deSSeth Forshee struct toshiba_acpi_dev {
164135740deSSeth Forshee 	struct acpi_device *acpi_dev;
165135740deSSeth Forshee 	const char *method_hci;
166135740deSSeth Forshee 	struct rfkill *bt_rfk;
167135740deSSeth Forshee 	struct input_dev *hotkey_dev;
16829cd293fSSeth Forshee 	struct work_struct hotkey_work;
169135740deSSeth Forshee 	struct backlight_device *backlight_dev;
170135740deSSeth Forshee 	struct led_classdev led_dev;
171360f0f39SAzael Avalos 	struct led_classdev kbd_led;
172def6c4e2SAzael Avalos 	struct led_classdev eco_led;
17336d03f93SSeth Forshee 
174135740deSSeth Forshee 	int force_fan;
175135740deSSeth Forshee 	int last_key_event;
176135740deSSeth Forshee 	int key_event_valid;
17793f8c16dSAzael Avalos 	int kbd_type;
178360f0f39SAzael Avalos 	int kbd_mode;
179360f0f39SAzael Avalos 	int kbd_time;
180182bcaa5SAzael Avalos 	int usbsc_bat_level;
181c8c91842SAzael Avalos 	int usbsc_mode_base;
182a2b3471bSAzael Avalos 	int hotkey_event_type;
183135740deSSeth Forshee 
184592b746cSDan Carpenter 	unsigned int illumination_supported:1;
185592b746cSDan Carpenter 	unsigned int video_supported:1;
186592b746cSDan Carpenter 	unsigned int fan_supported:1;
187592b746cSDan Carpenter 	unsigned int system_event_supported:1;
18829cd293fSSeth Forshee 	unsigned int ntfy_supported:1;
18929cd293fSSeth Forshee 	unsigned int info_supported:1;
190121b7b0dSAkio Idehara 	unsigned int tr_backlight_supported:1;
191360f0f39SAzael Avalos 	unsigned int kbd_illum_supported:1;
192360f0f39SAzael Avalos 	unsigned int kbd_led_registered:1;
1939d8658acSAzael Avalos 	unsigned int touchpad_supported:1;
194def6c4e2SAzael Avalos 	unsigned int eco_supported:1;
1955a2813e9SAzael Avalos 	unsigned int accelerometer_supported:1;
196e26ffe51SAzael Avalos 	unsigned int usb_sleep_charge_supported:1;
197bb3fe01fSAzael Avalos 	unsigned int usb_rapid_charge_supported:1;
198172ce0a9SAzael Avalos 	unsigned int usb_sleep_music_supported:1;
199bae84195SAzael Avalos 	unsigned int kbd_function_keys_supported:1;
20035d53ceaSAzael Avalos 	unsigned int panel_power_on_supported:1;
20117fe4b3dSAzael Avalos 	unsigned int usb_three_supported:1;
202360f0f39SAzael Avalos 	unsigned int sysfs_created:1;
20336d03f93SSeth Forshee 
204135740deSSeth Forshee 	struct mutex mutex;
205135740deSSeth Forshee };
206135740deSSeth Forshee 
20729cd293fSSeth Forshee static struct toshiba_acpi_dev *toshiba_acpi;
20829cd293fSSeth Forshee 
209b4f9fe12SLen Brown static const struct acpi_device_id toshiba_device_ids[] = {
210b4f9fe12SLen Brown 	{"TOS6200", 0},
21163a9e016SOndrej Zary 	{"TOS6207", 0},
212b4f9fe12SLen Brown 	{"TOS6208", 0},
213b4f9fe12SLen Brown 	{"TOS1900", 0},
214b4f9fe12SLen Brown 	{"", 0},
215b4f9fe12SLen Brown };
216b4f9fe12SLen Brown MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
217b4f9fe12SLen Brown 
218b859f159SGreg Kroah-Hartman static const struct key_entry toshiba_acpi_keymap[] = {
219fec278a1SUnai Uribarri 	{ KE_KEY, 0x9e, { KEY_RFKILL } },
220384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x101, { KEY_MUTE } },
221384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x102, { KEY_ZOOMOUT } },
222384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x103, { KEY_ZOOMIN } },
223408a5d13SAzael Avalos 	{ KE_KEY, 0x10f, { KEY_TAB } },
224af502837SAzael Avalos 	{ KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
225af502837SAzael Avalos 	{ KE_KEY, 0x139, { KEY_ZOOMRESET } },
226384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13b, { KEY_COFFEE } },
227384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13c, { KEY_BATTERY } },
228384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13d, { KEY_SLEEP } },
229384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13e, { KEY_SUSPEND } },
230384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
231384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
232384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
233384a7cd9SDmitry Torokhov 	{ KE_KEY, 0x142, { KEY_WLAN } },
234af502837SAzael Avalos 	{ KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
235a49010f5SJon Dowland 	{ KE_KEY, 0x17f, { KEY_FN } },
236384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb05, { KEY_PROG2 } },
237384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb06, { KEY_WWW } },
238384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb07, { KEY_MAIL } },
239384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb30, { KEY_STOP } },
240384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
241384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb32, { KEY_NEXTSONG } },
242384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
243384a7cd9SDmitry Torokhov 	{ KE_KEY, 0xb5a, { KEY_MEDIA } },
244408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
245408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
246408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
247408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
248408a5d13SAzael Avalos 	{ KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
249384a7cd9SDmitry Torokhov 	{ KE_END, 0 },
2506335e4d5SMatthew Garrett };
2516335e4d5SMatthew Garrett 
252fe808bfbSTakashi Iwai static const struct key_entry toshiba_acpi_alt_keymap[] = {
253fe808bfbSTakashi Iwai 	{ KE_KEY, 0x157, { KEY_MUTE } },
254fe808bfbSTakashi Iwai 	{ KE_KEY, 0x102, { KEY_ZOOMOUT } },
255fe808bfbSTakashi Iwai 	{ KE_KEY, 0x103, { KEY_ZOOMIN } },
256e6efad7fSAzael Avalos 	{ KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
257fe808bfbSTakashi Iwai 	{ KE_KEY, 0x139, { KEY_ZOOMRESET } },
258fe808bfbSTakashi Iwai 	{ KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
259fe808bfbSTakashi Iwai 	{ KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
260fe808bfbSTakashi Iwai 	{ KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
261fe808bfbSTakashi Iwai 	{ KE_KEY, 0x158, { KEY_WLAN } },
262fe808bfbSTakashi Iwai 	{ KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
263fe808bfbSTakashi Iwai 	{ KE_END, 0 },
264fe808bfbSTakashi Iwai };
265fe808bfbSTakashi Iwai 
266e0769fe6SDarren Hart /*
267e0769fe6SDarren Hart  * Utility
268b4f9fe12SLen Brown  */
269b4f9fe12SLen Brown 
270b5163992SAzael Avalos static inline void _set_bit(u32 *word, u32 mask, int value)
271b4f9fe12SLen Brown {
272b4f9fe12SLen Brown 	*word = (*word & ~mask) | (mask * value);
273b4f9fe12SLen Brown }
274b4f9fe12SLen Brown 
275e0769fe6SDarren Hart /*
276e0769fe6SDarren Hart  * ACPI interface wrappers
277b4f9fe12SLen Brown  */
278b4f9fe12SLen Brown 
279b4f9fe12SLen Brown static int write_acpi_int(const char *methodName, int val)
280b4f9fe12SLen Brown {
281b4f9fe12SLen Brown 	acpi_status status;
282b4f9fe12SLen Brown 
283619400daSZhang Rui 	status = acpi_execute_simple_method(NULL, (char *)methodName, val);
28432bcd5cbSSeth Forshee 	return (status == AE_OK) ? 0 : -EIO;
285b4f9fe12SLen Brown }
286b4f9fe12SLen Brown 
287e0769fe6SDarren Hart /*
288e0769fe6SDarren Hart  * Perform a raw configuration call.  Here we don't care about input or output
289258c5903SAzael Avalos  * buffer format.
290b4f9fe12SLen Brown  */
291258c5903SAzael Avalos static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
292258c5903SAzael Avalos 			   const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
293b4f9fe12SLen Brown {
294b4f9fe12SLen Brown 	struct acpi_object_list params;
295258c5903SAzael Avalos 	union acpi_object in_objs[TCI_WORDS];
296b4f9fe12SLen Brown 	struct acpi_buffer results;
297258c5903SAzael Avalos 	union acpi_object out_objs[TCI_WORDS + 1];
298b4f9fe12SLen Brown 	acpi_status status;
299b4f9fe12SLen Brown 	int i;
300b4f9fe12SLen Brown 
301258c5903SAzael Avalos 	params.count = TCI_WORDS;
302b4f9fe12SLen Brown 	params.pointer = in_objs;
303258c5903SAzael Avalos 	for (i = 0; i < TCI_WORDS; ++i) {
304b4f9fe12SLen Brown 		in_objs[i].type = ACPI_TYPE_INTEGER;
305b4f9fe12SLen Brown 		in_objs[i].integer.value = in[i];
306b4f9fe12SLen Brown 	}
307b4f9fe12SLen Brown 
308b4f9fe12SLen Brown 	results.length = sizeof(out_objs);
309b4f9fe12SLen Brown 	results.pointer = out_objs;
310b4f9fe12SLen Brown 
3116e02cc7eSSeth Forshee 	status = acpi_evaluate_object(dev->acpi_dev->handle,
3126e02cc7eSSeth Forshee 				      (char *)dev->method_hci, &params,
313b4f9fe12SLen Brown 				      &results);
314258c5903SAzael Avalos 	if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
315b5163992SAzael Avalos 		for (i = 0; i < out_objs->package.count; ++i)
316b4f9fe12SLen Brown 			out[i] = out_objs->package.elements[i].integer.value;
317b4f9fe12SLen Brown 	}
318b4f9fe12SLen Brown 
319b4f9fe12SLen Brown 	return status;
320b4f9fe12SLen Brown }
321b4f9fe12SLen Brown 
322e0769fe6SDarren Hart /*
323e0769fe6SDarren Hart  * Common hci tasks (get or set one or two value)
324b4f9fe12SLen Brown  *
325b4f9fe12SLen Brown  * In addition to the ACPI status, the HCI system returns a result which
326b4f9fe12SLen Brown  * may be useful (such as "not supported").
327b4f9fe12SLen Brown  */
328b4f9fe12SLen Brown 
329893f3f62SAzael Avalos static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
330b4f9fe12SLen Brown {
331258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
332258c5903SAzael Avalos 	u32 out[TCI_WORDS];
333258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
334893f3f62SAzael Avalos 
335893f3f62SAzael Avalos 	return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
336b4f9fe12SLen Brown }
337b4f9fe12SLen Brown 
338893f3f62SAzael Avalos static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
339b4f9fe12SLen Brown {
340258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
341258c5903SAzael Avalos 	u32 out[TCI_WORDS];
342258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
343b5163992SAzael Avalos 
344893f3f62SAzael Avalos 	if (ACPI_FAILURE(status))
345893f3f62SAzael Avalos 		return TOS_FAILURE;
346893f3f62SAzael Avalos 
347b4f9fe12SLen Brown 	*out1 = out[2];
348893f3f62SAzael Avalos 
349893f3f62SAzael Avalos 	return out[0];
350b4f9fe12SLen Brown }
351b4f9fe12SLen Brown 
352893f3f62SAzael Avalos static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
353b4f9fe12SLen Brown {
354258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
355258c5903SAzael Avalos 	u32 out[TCI_WORDS];
356258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
357893f3f62SAzael Avalos 
358893f3f62SAzael Avalos 	return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
359b4f9fe12SLen Brown }
360b4f9fe12SLen Brown 
361b5163992SAzael Avalos static u32 hci_read2(struct toshiba_acpi_dev *dev,
362b5163992SAzael Avalos 		     u32 reg, u32 *out1, u32 *out2)
363b4f9fe12SLen Brown {
364258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
365258c5903SAzael Avalos 	u32 out[TCI_WORDS];
366258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
367b5163992SAzael Avalos 
368893f3f62SAzael Avalos 	if (ACPI_FAILURE(status))
369893f3f62SAzael Avalos 		return TOS_FAILURE;
370893f3f62SAzael Avalos 
371b4f9fe12SLen Brown 	*out1 = out[2];
372b4f9fe12SLen Brown 	*out2 = out[3];
373893f3f62SAzael Avalos 
374893f3f62SAzael Avalos 	return out[0];
375b4f9fe12SLen Brown }
376b4f9fe12SLen Brown 
377e0769fe6SDarren Hart /*
378e0769fe6SDarren Hart  * Common sci tasks
37984a6273fSAzael Avalos  */
38084a6273fSAzael Avalos 
38184a6273fSAzael Avalos static int sci_open(struct toshiba_acpi_dev *dev)
38284a6273fSAzael Avalos {
383258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
384258c5903SAzael Avalos 	u32 out[TCI_WORDS];
38584a6273fSAzael Avalos 	acpi_status status;
38684a6273fSAzael Avalos 
387258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
3881864bbc2SAzael Avalos 	if  (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
38984a6273fSAzael Avalos 		pr_err("ACPI call to open SCI failed\n");
39084a6273fSAzael Avalos 		return 0;
39184a6273fSAzael Avalos 	}
39284a6273fSAzael Avalos 
3931864bbc2SAzael Avalos 	if (out[0] == TOS_OPEN_CLOSE_OK) {
39484a6273fSAzael Avalos 		return 1;
3951864bbc2SAzael Avalos 	} else if (out[0] == TOS_ALREADY_OPEN) {
39684a6273fSAzael Avalos 		pr_info("Toshiba SCI already opened\n");
39784a6273fSAzael Avalos 		return 1;
398fa465739SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
399e0769fe6SDarren Hart 		/*
400e0769fe6SDarren Hart 		 * Some BIOSes do not have the SCI open/close functions
401fa465739SAzael Avalos 		 * implemented and return 0x8000 (Not Supported), failing to
402fa465739SAzael Avalos 		 * register some supported features.
403fa465739SAzael Avalos 		 *
404fa465739SAzael Avalos 		 * Simply return 1 if we hit those affected laptops to make the
405fa465739SAzael Avalos 		 * supported features work.
406fa465739SAzael Avalos 		 *
407fa465739SAzael Avalos 		 * In the case that some laptops really do not support the SCI,
408fa465739SAzael Avalos 		 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
409fa465739SAzael Avalos 		 * and thus, not registering support for the queried feature.
410fa465739SAzael Avalos 		 */
411fa465739SAzael Avalos 		return 1;
4121864bbc2SAzael Avalos 	} else if (out[0] == TOS_NOT_PRESENT) {
41384a6273fSAzael Avalos 		pr_info("Toshiba SCI is not present\n");
41484a6273fSAzael Avalos 	}
41584a6273fSAzael Avalos 
41684a6273fSAzael Avalos 	return 0;
41784a6273fSAzael Avalos }
41884a6273fSAzael Avalos 
41984a6273fSAzael Avalos static void sci_close(struct toshiba_acpi_dev *dev)
42084a6273fSAzael Avalos {
421258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
422258c5903SAzael Avalos 	u32 out[TCI_WORDS];
42384a6273fSAzael Avalos 	acpi_status status;
42484a6273fSAzael Avalos 
425258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
4261864bbc2SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
42784a6273fSAzael Avalos 		pr_err("ACPI call to close SCI failed\n");
42884a6273fSAzael Avalos 		return;
42984a6273fSAzael Avalos 	}
43084a6273fSAzael Avalos 
4311864bbc2SAzael Avalos 	if (out[0] == TOS_OPEN_CLOSE_OK)
43284a6273fSAzael Avalos 		return;
4331864bbc2SAzael Avalos 	else if (out[0] == TOS_NOT_OPENED)
43484a6273fSAzael Avalos 		pr_info("Toshiba SCI not opened\n");
4351864bbc2SAzael Avalos 	else if (out[0] == TOS_NOT_PRESENT)
43684a6273fSAzael Avalos 		pr_info("Toshiba SCI is not present\n");
43784a6273fSAzael Avalos }
43884a6273fSAzael Avalos 
439893f3f62SAzael Avalos static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
44084a6273fSAzael Avalos {
441258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
442258c5903SAzael Avalos 	u32 out[TCI_WORDS];
443258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
444b5163992SAzael Avalos 
445893f3f62SAzael Avalos 	if (ACPI_FAILURE(status))
446893f3f62SAzael Avalos 		return TOS_FAILURE;
447893f3f62SAzael Avalos 
44884a6273fSAzael Avalos 	*out1 = out[2];
449893f3f62SAzael Avalos 
450893f3f62SAzael Avalos 	return out[0];
45184a6273fSAzael Avalos }
45284a6273fSAzael Avalos 
453893f3f62SAzael Avalos static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
45484a6273fSAzael Avalos {
455258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
456258c5903SAzael Avalos 	u32 out[TCI_WORDS];
457258c5903SAzael Avalos 	acpi_status status = tci_raw(dev, in, out);
458893f3f62SAzael Avalos 
459893f3f62SAzael Avalos 	return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
46084a6273fSAzael Avalos }
46184a6273fSAzael Avalos 
4626c3f6e6cSPierre Ducroquet /* Illumination support */
463135740deSSeth Forshee static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
4646c3f6e6cSPierre Ducroquet {
465258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
466258c5903SAzael Avalos 	u32 out[TCI_WORDS];
4676c3f6e6cSPierre Ducroquet 	acpi_status status;
4686c3f6e6cSPierre Ducroquet 
469fdb79081SAzael Avalos 	if (!sci_open(dev))
470fdb79081SAzael Avalos 		return 0;
471fdb79081SAzael Avalos 
472258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
473fdb79081SAzael Avalos 	sci_close(dev);
4741864bbc2SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
475fdb79081SAzael Avalos 		pr_err("ACPI call to query Illumination support failed\n");
476fdb79081SAzael Avalos 		return 0;
4771864bbc2SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
4787e33460dSJoe Perches 		pr_info("Illumination device not available\n");
4796c3f6e6cSPierre Ducroquet 		return 0;
4806c3f6e6cSPierre Ducroquet 	}
481fdb79081SAzael Avalos 
4826c3f6e6cSPierre Ducroquet 	return 1;
4836c3f6e6cSPierre Ducroquet }
4846c3f6e6cSPierre Ducroquet 
4856c3f6e6cSPierre Ducroquet static void toshiba_illumination_set(struct led_classdev *cdev,
4866c3f6e6cSPierre Ducroquet 				     enum led_brightness brightness)
4876c3f6e6cSPierre Ducroquet {
488135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = container_of(cdev,
489135740deSSeth Forshee 			struct toshiba_acpi_dev, led_dev);
490fdb79081SAzael Avalos 	u32 state, result;
4916c3f6e6cSPierre Ducroquet 
4926c3f6e6cSPierre Ducroquet 	/* First request : initialize communication. */
493fdb79081SAzael Avalos 	if (!sci_open(dev))
4946c3f6e6cSPierre Ducroquet 		return;
4956c3f6e6cSPierre Ducroquet 
496fdb79081SAzael Avalos 	/* Switch the illumination on/off */
497fdb79081SAzael Avalos 	state = brightness ? 1 : 0;
498893f3f62SAzael Avalos 	result = sci_write(dev, SCI_ILLUMINATION, state);
499fdb79081SAzael Avalos 	sci_close(dev);
500893f3f62SAzael Avalos 	if (result == TOS_FAILURE) {
501fdb79081SAzael Avalos 		pr_err("ACPI call for illumination failed\n");
502fdb79081SAzael Avalos 		return;
5031864bbc2SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
504fdb79081SAzael Avalos 		pr_info("Illumination not supported\n");
5056c3f6e6cSPierre Ducroquet 		return;
5066c3f6e6cSPierre Ducroquet 	}
5076c3f6e6cSPierre Ducroquet }
5086c3f6e6cSPierre Ducroquet 
5096c3f6e6cSPierre Ducroquet static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
5106c3f6e6cSPierre Ducroquet {
511135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = container_of(cdev,
512135740deSSeth Forshee 			struct toshiba_acpi_dev, led_dev);
513fdb79081SAzael Avalos 	u32 state, result;
5146c3f6e6cSPierre Ducroquet 
5156c3f6e6cSPierre Ducroquet 	/* First request : initialize communication. */
516fdb79081SAzael Avalos 	if (!sci_open(dev))
5176c3f6e6cSPierre Ducroquet 		return LED_OFF;
5186c3f6e6cSPierre Ducroquet 
5196c3f6e6cSPierre Ducroquet 	/* Check the illumination */
520893f3f62SAzael Avalos 	result = sci_read(dev, SCI_ILLUMINATION, &state);
521fdb79081SAzael Avalos 	sci_close(dev);
522893f3f62SAzael Avalos 	if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
523fdb79081SAzael Avalos 		pr_err("ACPI call for illumination failed\n");
524fdb79081SAzael Avalos 		return LED_OFF;
5251864bbc2SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
526fdb79081SAzael Avalos 		pr_info("Illumination not supported\n");
5276c3f6e6cSPierre Ducroquet 		return LED_OFF;
5286c3f6e6cSPierre Ducroquet 	}
5296c3f6e6cSPierre Ducroquet 
530fdb79081SAzael Avalos 	return state ? LED_FULL : LED_OFF;
5316c3f6e6cSPierre Ducroquet }
5326c3f6e6cSPierre Ducroquet 
533360f0f39SAzael Avalos /* KBD Illumination */
53493f8c16dSAzael Avalos static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
53593f8c16dSAzael Avalos {
536258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
537258c5903SAzael Avalos 	u32 out[TCI_WORDS];
53893f8c16dSAzael Avalos 	acpi_status status;
53993f8c16dSAzael Avalos 
54093f8c16dSAzael Avalos 	if (!sci_open(dev))
54193f8c16dSAzael Avalos 		return 0;
54293f8c16dSAzael Avalos 
543258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
54493f8c16dSAzael Avalos 	sci_close(dev);
5451864bbc2SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
54693f8c16dSAzael Avalos 		pr_err("ACPI call to query kbd illumination support failed\n");
54793f8c16dSAzael Avalos 		return 0;
5481864bbc2SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
54993f8c16dSAzael Avalos 		pr_info("Keyboard illumination not available\n");
55093f8c16dSAzael Avalos 		return 0;
55193f8c16dSAzael Avalos 	}
55293f8c16dSAzael Avalos 
553e0769fe6SDarren Hart 	/*
554e0769fe6SDarren Hart 	 * Check for keyboard backlight timeout max value,
55593f8c16dSAzael Avalos 	 * previous kbd backlight implementation set this to
55693f8c16dSAzael Avalos 	 * 0x3c0003, and now the new implementation set this
557e0769fe6SDarren Hart 	 * to 0x3c001a, use this to distinguish between them.
55893f8c16dSAzael Avalos 	 */
55993f8c16dSAzael Avalos 	if (out[3] == SCI_KBD_TIME_MAX)
56093f8c16dSAzael Avalos 		dev->kbd_type = 2;
56193f8c16dSAzael Avalos 	else
56293f8c16dSAzael Avalos 		dev->kbd_type = 1;
56393f8c16dSAzael Avalos 	/* Get the current keyboard backlight mode */
56493f8c16dSAzael Avalos 	dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
56593f8c16dSAzael Avalos 	/* Get the current time (1-60 seconds) */
56693f8c16dSAzael Avalos 	dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
56793f8c16dSAzael Avalos 
56893f8c16dSAzael Avalos 	return 1;
56993f8c16dSAzael Avalos }
57093f8c16dSAzael Avalos 
571360f0f39SAzael Avalos static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
572360f0f39SAzael Avalos {
573360f0f39SAzael Avalos 	u32 result;
574360f0f39SAzael Avalos 
575360f0f39SAzael Avalos 	if (!sci_open(dev))
576360f0f39SAzael Avalos 		return -EIO;
577360f0f39SAzael Avalos 
578893f3f62SAzael Avalos 	result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
579360f0f39SAzael Avalos 	sci_close(dev);
580893f3f62SAzael Avalos 	if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
581360f0f39SAzael Avalos 		pr_err("ACPI call to set KBD backlight status failed\n");
582360f0f39SAzael Avalos 		return -EIO;
5831864bbc2SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
584360f0f39SAzael Avalos 		pr_info("Keyboard backlight status not supported\n");
585360f0f39SAzael Avalos 		return -ENODEV;
586360f0f39SAzael Avalos 	}
587360f0f39SAzael Avalos 
588360f0f39SAzael Avalos 	return 0;
589360f0f39SAzael Avalos }
590360f0f39SAzael Avalos 
591360f0f39SAzael Avalos static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
592360f0f39SAzael Avalos {
593360f0f39SAzael Avalos 	u32 result;
594360f0f39SAzael Avalos 
595360f0f39SAzael Avalos 	if (!sci_open(dev))
596360f0f39SAzael Avalos 		return -EIO;
597360f0f39SAzael Avalos 
598893f3f62SAzael Avalos 	result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
599360f0f39SAzael Avalos 	sci_close(dev);
600893f3f62SAzael Avalos 	if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
601360f0f39SAzael Avalos 		pr_err("ACPI call to get KBD backlight status failed\n");
602360f0f39SAzael Avalos 		return -EIO;
6031864bbc2SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
604360f0f39SAzael Avalos 		pr_info("Keyboard backlight status not supported\n");
605360f0f39SAzael Avalos 		return -ENODEV;
606360f0f39SAzael Avalos 	}
607360f0f39SAzael Avalos 
608360f0f39SAzael Avalos 	return 0;
609360f0f39SAzael Avalos }
610360f0f39SAzael Avalos 
611360f0f39SAzael Avalos static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
612360f0f39SAzael Avalos {
613360f0f39SAzael Avalos 	struct toshiba_acpi_dev *dev = container_of(cdev,
614360f0f39SAzael Avalos 			struct toshiba_acpi_dev, kbd_led);
615360f0f39SAzael Avalos 	u32 state, result;
616360f0f39SAzael Avalos 
617360f0f39SAzael Avalos 	/* Check the keyboard backlight state */
618893f3f62SAzael Avalos 	result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
619893f3f62SAzael Avalos 	if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
620360f0f39SAzael Avalos 		pr_err("ACPI call to get the keyboard backlight failed\n");
621360f0f39SAzael Avalos 		return LED_OFF;
6221864bbc2SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
623360f0f39SAzael Avalos 		pr_info("Keyboard backlight not supported\n");
624360f0f39SAzael Avalos 		return LED_OFF;
625360f0f39SAzael Avalos 	}
626360f0f39SAzael Avalos 
627360f0f39SAzael Avalos 	return state ? LED_FULL : LED_OFF;
628360f0f39SAzael Avalos }
629360f0f39SAzael Avalos 
630360f0f39SAzael Avalos static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
631360f0f39SAzael Avalos 				     enum led_brightness brightness)
632360f0f39SAzael Avalos {
633360f0f39SAzael Avalos 	struct toshiba_acpi_dev *dev = container_of(cdev,
634360f0f39SAzael Avalos 			struct toshiba_acpi_dev, kbd_led);
635360f0f39SAzael Avalos 	u32 state, result;
636360f0f39SAzael Avalos 
637360f0f39SAzael Avalos 	/* Set the keyboard backlight state */
638360f0f39SAzael Avalos 	state = brightness ? 1 : 0;
639893f3f62SAzael Avalos 	result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
640893f3f62SAzael Avalos 	if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
641360f0f39SAzael Avalos 		pr_err("ACPI call to set KBD Illumination mode failed\n");
642360f0f39SAzael Avalos 		return;
6431864bbc2SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
644360f0f39SAzael Avalos 		pr_info("Keyboard backlight not supported\n");
645360f0f39SAzael Avalos 		return;
646360f0f39SAzael Avalos 	}
647360f0f39SAzael Avalos }
648360f0f39SAzael Avalos 
6499d8658acSAzael Avalos /* TouchPad support */
6509d8658acSAzael Avalos static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
6519d8658acSAzael Avalos {
6529d8658acSAzael Avalos 	u32 result;
6539d8658acSAzael Avalos 
6549d8658acSAzael Avalos 	if (!sci_open(dev))
6559d8658acSAzael Avalos 		return -EIO;
6569d8658acSAzael Avalos 
657893f3f62SAzael Avalos 	result = sci_write(dev, SCI_TOUCHPAD, state);
6589d8658acSAzael Avalos 	sci_close(dev);
659893f3f62SAzael Avalos 	if (result == TOS_FAILURE) {
6609d8658acSAzael Avalos 		pr_err("ACPI call to set the touchpad failed\n");
6619d8658acSAzael Avalos 		return -EIO;
6621864bbc2SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
6639d8658acSAzael Avalos 		return -ENODEV;
6649d8658acSAzael Avalos 	}
6659d8658acSAzael Avalos 
6669d8658acSAzael Avalos 	return 0;
6679d8658acSAzael Avalos }
6689d8658acSAzael Avalos 
6699d8658acSAzael Avalos static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
6709d8658acSAzael Avalos {
6719d8658acSAzael Avalos 	u32 result;
6729d8658acSAzael Avalos 
6739d8658acSAzael Avalos 	if (!sci_open(dev))
6749d8658acSAzael Avalos 		return -EIO;
6759d8658acSAzael Avalos 
676893f3f62SAzael Avalos 	result = sci_read(dev, SCI_TOUCHPAD, state);
6779d8658acSAzael Avalos 	sci_close(dev);
678893f3f62SAzael Avalos 	if (result == TOS_FAILURE) {
6799d8658acSAzael Avalos 		pr_err("ACPI call to query the touchpad failed\n");
6809d8658acSAzael Avalos 		return -EIO;
6811864bbc2SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
6829d8658acSAzael Avalos 		return -ENODEV;
6839d8658acSAzael Avalos 	}
6849d8658acSAzael Avalos 
6859d8658acSAzael Avalos 	return 0;
6869d8658acSAzael Avalos }
6879d8658acSAzael Avalos 
688def6c4e2SAzael Avalos /* Eco Mode support */
689def6c4e2SAzael Avalos static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
690def6c4e2SAzael Avalos {
691def6c4e2SAzael Avalos 	acpi_status status;
69298fc4ec6SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
693258c5903SAzael Avalos 	u32 out[TCI_WORDS];
694def6c4e2SAzael Avalos 
695258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
69698fc4ec6SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
69798fc4ec6SAzael Avalos 		pr_err("ACPI call to get ECO led failed\n");
69898fc4ec6SAzael Avalos 	} else if (out[0] == TOS_NOT_INSTALLED) {
69998fc4ec6SAzael Avalos 		pr_info("ECO led not installed");
70098fc4ec6SAzael Avalos 	} else if (out[0] == TOS_INPUT_DATA_ERROR) {
701e0769fe6SDarren Hart 		/*
702e0769fe6SDarren Hart 		 * If we receive 0x8300 (Input Data Error), it means that the
70398fc4ec6SAzael Avalos 		 * LED device is present, but that we just screwed the input
70498fc4ec6SAzael Avalos 		 * parameters.
70598fc4ec6SAzael Avalos 		 *
70698fc4ec6SAzael Avalos 		 * Let's query the status of the LED to see if we really have a
70798fc4ec6SAzael Avalos 		 * success response, indicating the actual presense of the LED,
70898fc4ec6SAzael Avalos 		 * bail out otherwise.
70998fc4ec6SAzael Avalos 		 */
71098fc4ec6SAzael Avalos 		in[3] = 1;
71198fc4ec6SAzael Avalos 		status = tci_raw(dev, in, out);
71298fc4ec6SAzael Avalos 		if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
71398fc4ec6SAzael Avalos 			pr_err("ACPI call to get ECO led failed\n");
71498fc4ec6SAzael Avalos 		else if (out[0] == TOS_SUCCESS)
71598fc4ec6SAzael Avalos 			return 1;
716def6c4e2SAzael Avalos 	}
717def6c4e2SAzael Avalos 
71898fc4ec6SAzael Avalos 	return 0;
719def6c4e2SAzael Avalos }
720def6c4e2SAzael Avalos 
721b5163992SAzael Avalos static enum led_brightness
722b5163992SAzael Avalos toshiba_eco_mode_get_status(struct led_classdev *cdev)
723def6c4e2SAzael Avalos {
724def6c4e2SAzael Avalos 	struct toshiba_acpi_dev *dev = container_of(cdev,
725def6c4e2SAzael Avalos 			struct toshiba_acpi_dev, eco_led);
726258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
727258c5903SAzael Avalos 	u32 out[TCI_WORDS];
728def6c4e2SAzael Avalos 	acpi_status status;
729def6c4e2SAzael Avalos 
730258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
7311864bbc2SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
732def6c4e2SAzael Avalos 		pr_err("ACPI call to get ECO led failed\n");
733def6c4e2SAzael Avalos 		return LED_OFF;
734def6c4e2SAzael Avalos 	}
735def6c4e2SAzael Avalos 
736def6c4e2SAzael Avalos 	return out[2] ? LED_FULL : LED_OFF;
737def6c4e2SAzael Avalos }
738def6c4e2SAzael Avalos 
739def6c4e2SAzael Avalos static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
740def6c4e2SAzael Avalos 				     enum led_brightness brightness)
741def6c4e2SAzael Avalos {
742def6c4e2SAzael Avalos 	struct toshiba_acpi_dev *dev = container_of(cdev,
743def6c4e2SAzael Avalos 			struct toshiba_acpi_dev, eco_led);
744258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
745258c5903SAzael Avalos 	u32 out[TCI_WORDS];
746def6c4e2SAzael Avalos 	acpi_status status;
747def6c4e2SAzael Avalos 
748def6c4e2SAzael Avalos 	/* Switch the Eco Mode led on/off */
749def6c4e2SAzael Avalos 	in[2] = (brightness) ? 1 : 0;
750258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
7511864bbc2SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
752def6c4e2SAzael Avalos 		pr_err("ACPI call to set ECO led failed\n");
753def6c4e2SAzael Avalos 		return;
754def6c4e2SAzael Avalos 	}
755def6c4e2SAzael Avalos }
756def6c4e2SAzael Avalos 
7575a2813e9SAzael Avalos /* Accelerometer support */
7585a2813e9SAzael Avalos static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
7595a2813e9SAzael Avalos {
760258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
761258c5903SAzael Avalos 	u32 out[TCI_WORDS];
7625a2813e9SAzael Avalos 	acpi_status status;
7635a2813e9SAzael Avalos 
764e0769fe6SDarren Hart 	/*
765e0769fe6SDarren Hart 	 * Check if the accelerometer call exists,
7665a2813e9SAzael Avalos 	 * this call also serves as initialization
7675a2813e9SAzael Avalos 	 */
768258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
7691864bbc2SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
7705a2813e9SAzael Avalos 		pr_err("ACPI call to query the accelerometer failed\n");
7715a2813e9SAzael Avalos 		return -EIO;
7721864bbc2SAzael Avalos 	} else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
7731864bbc2SAzael Avalos 		   out[0] == TOS_NOT_INITIALIZED) {
7745a2813e9SAzael Avalos 		pr_err("Accelerometer not initialized\n");
7755a2813e9SAzael Avalos 		return -EIO;
7761864bbc2SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
7775a2813e9SAzael Avalos 		pr_info("Accelerometer not supported\n");
7785a2813e9SAzael Avalos 		return -ENODEV;
7795a2813e9SAzael Avalos 	}
7805a2813e9SAzael Avalos 
7815a2813e9SAzael Avalos 	return 0;
7825a2813e9SAzael Avalos }
7835a2813e9SAzael Avalos 
7845a2813e9SAzael Avalos static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
7855a2813e9SAzael Avalos 				      u32 *xy, u32 *z)
7865a2813e9SAzael Avalos {
787258c5903SAzael Avalos 	u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
788258c5903SAzael Avalos 	u32 out[TCI_WORDS];
7895a2813e9SAzael Avalos 	acpi_status status;
7905a2813e9SAzael Avalos 
7915a2813e9SAzael Avalos 	/* Check the Accelerometer status */
792258c5903SAzael Avalos 	status = tci_raw(dev, in, out);
7931864bbc2SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
7945a2813e9SAzael Avalos 		pr_err("ACPI call to query the accelerometer failed\n");
7955a2813e9SAzael Avalos 		return -EIO;
7965a2813e9SAzael Avalos 	}
7975a2813e9SAzael Avalos 
7985a2813e9SAzael Avalos 	*xy = out[2];
7995a2813e9SAzael Avalos 	*z = out[4];
8005a2813e9SAzael Avalos 
8015a2813e9SAzael Avalos 	return 0;
8025a2813e9SAzael Avalos }
8035a2813e9SAzael Avalos 
804e26ffe51SAzael Avalos /* Sleep (Charge and Music) utilities support */
805c8c91842SAzael Avalos static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
806c8c91842SAzael Avalos {
807c8c91842SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
808c8c91842SAzael Avalos 	u32 out[TCI_WORDS];
809c8c91842SAzael Avalos 	acpi_status status;
810c8c91842SAzael Avalos 
811c8c91842SAzael Avalos 	/* Set the feature to "not supported" in case of error */
812c8c91842SAzael Avalos 	dev->usb_sleep_charge_supported = 0;
813c8c91842SAzael Avalos 
814c8c91842SAzael Avalos 	if (!sci_open(dev))
815c8c91842SAzael Avalos 		return;
816c8c91842SAzael Avalos 
817c8c91842SAzael Avalos 	status = tci_raw(dev, in, out);
818c8c91842SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
819c8c91842SAzael Avalos 		pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
820c8c91842SAzael Avalos 		sci_close(dev);
821c8c91842SAzael Avalos 		return;
822c8c91842SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
823c8c91842SAzael Avalos 		pr_info("USB Sleep and Charge not supported\n");
824c8c91842SAzael Avalos 		sci_close(dev);
825c8c91842SAzael Avalos 		return;
826c8c91842SAzael Avalos 	} else if (out[0] == TOS_SUCCESS) {
827c8c91842SAzael Avalos 		dev->usbsc_mode_base = out[4];
828c8c91842SAzael Avalos 	}
829c8c91842SAzael Avalos 
830c8c91842SAzael Avalos 	in[5] = SCI_USB_CHARGE_BAT_LVL;
831c8c91842SAzael Avalos 	status = tci_raw(dev, in, out);
832c8c91842SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
833c8c91842SAzael Avalos 		pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
834c8c91842SAzael Avalos 		sci_close(dev);
835c8c91842SAzael Avalos 		return;
836c8c91842SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
837c8c91842SAzael Avalos 		pr_info("USB Sleep and Charge not supported\n");
838c8c91842SAzael Avalos 		sci_close(dev);
839c8c91842SAzael Avalos 		return;
840c8c91842SAzael Avalos 	} else if (out[0] == TOS_SUCCESS) {
841c8c91842SAzael Avalos 		dev->usbsc_bat_level = out[2];
842c8c91842SAzael Avalos 		/*
843c8c91842SAzael Avalos 		 * If we reach this point, it means that the laptop has support
844c8c91842SAzael Avalos 		 * for this feature and all values are initialized.
845c8c91842SAzael Avalos 		 * Set it as supported.
846c8c91842SAzael Avalos 		 */
847c8c91842SAzael Avalos 		dev->usb_sleep_charge_supported = 1;
848c8c91842SAzael Avalos 	}
849c8c91842SAzael Avalos 
850c8c91842SAzael Avalos 	sci_close(dev);
851c8c91842SAzael Avalos }
852c8c91842SAzael Avalos 
853e26ffe51SAzael Avalos static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
854e26ffe51SAzael Avalos 					u32 *mode)
855e26ffe51SAzael Avalos {
856e26ffe51SAzael Avalos 	u32 result;
857e26ffe51SAzael Avalos 
858e26ffe51SAzael Avalos 	if (!sci_open(dev))
859e26ffe51SAzael Avalos 		return -EIO;
860e26ffe51SAzael Avalos 
861e26ffe51SAzael Avalos 	result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
862e26ffe51SAzael Avalos 	sci_close(dev);
863e26ffe51SAzael Avalos 	if (result == TOS_FAILURE) {
864e26ffe51SAzael Avalos 		pr_err("ACPI call to set USB S&C mode failed\n");
865e26ffe51SAzael Avalos 		return -EIO;
866e26ffe51SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
867e26ffe51SAzael Avalos 		pr_info("USB Sleep and Charge not supported\n");
868e26ffe51SAzael Avalos 		return -ENODEV;
869e26ffe51SAzael Avalos 	} else if (result == TOS_INPUT_DATA_ERROR) {
870e26ffe51SAzael Avalos 		return -EIO;
871e26ffe51SAzael Avalos 	}
872e26ffe51SAzael Avalos 
873e26ffe51SAzael Avalos 	return 0;
874e26ffe51SAzael Avalos }
875e26ffe51SAzael Avalos 
876e26ffe51SAzael Avalos static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
877e26ffe51SAzael Avalos 					u32 mode)
878e26ffe51SAzael Avalos {
879e26ffe51SAzael Avalos 	u32 result;
880e26ffe51SAzael Avalos 
881e26ffe51SAzael Avalos 	if (!sci_open(dev))
882e26ffe51SAzael Avalos 		return -EIO;
883e26ffe51SAzael Avalos 
884e26ffe51SAzael Avalos 	result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
885e26ffe51SAzael Avalos 	sci_close(dev);
886e26ffe51SAzael Avalos 	if (result == TOS_FAILURE) {
887e26ffe51SAzael Avalos 		pr_err("ACPI call to set USB S&C mode failed\n");
888e26ffe51SAzael Avalos 		return -EIO;
889e26ffe51SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
890e26ffe51SAzael Avalos 		pr_info("USB Sleep and Charge not supported\n");
891e26ffe51SAzael Avalos 		return -ENODEV;
892e26ffe51SAzael Avalos 	} else if (result == TOS_INPUT_DATA_ERROR) {
893e26ffe51SAzael Avalos 		return -EIO;
894e26ffe51SAzael Avalos 	}
895e26ffe51SAzael Avalos 
896e26ffe51SAzael Avalos 	return 0;
897e26ffe51SAzael Avalos }
898e26ffe51SAzael Avalos 
899182bcaa5SAzael Avalos static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
900182bcaa5SAzael Avalos 					      u32 *mode)
901182bcaa5SAzael Avalos {
902182bcaa5SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
903182bcaa5SAzael Avalos 	u32 out[TCI_WORDS];
904182bcaa5SAzael Avalos 	acpi_status status;
905182bcaa5SAzael Avalos 
906182bcaa5SAzael Avalos 	if (!sci_open(dev))
907182bcaa5SAzael Avalos 		return -EIO;
908182bcaa5SAzael Avalos 
909182bcaa5SAzael Avalos 	in[5] = SCI_USB_CHARGE_BAT_LVL;
910182bcaa5SAzael Avalos 	status = tci_raw(dev, in, out);
911182bcaa5SAzael Avalos 	sci_close(dev);
912182bcaa5SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
913182bcaa5SAzael Avalos 		pr_err("ACPI call to get USB S&C battery level failed\n");
914182bcaa5SAzael Avalos 		return -EIO;
915182bcaa5SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
916182bcaa5SAzael Avalos 		pr_info("USB Sleep and Charge not supported\n");
917182bcaa5SAzael Avalos 		return -ENODEV;
918182bcaa5SAzael Avalos 	} else if (out[0] == TOS_INPUT_DATA_ERROR) {
919182bcaa5SAzael Avalos 		return -EIO;
920182bcaa5SAzael Avalos 	}
921182bcaa5SAzael Avalos 
922182bcaa5SAzael Avalos 	*mode = out[2];
923182bcaa5SAzael Avalos 
924182bcaa5SAzael Avalos 	return 0;
925182bcaa5SAzael Avalos }
926182bcaa5SAzael Avalos 
927182bcaa5SAzael Avalos static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
928182bcaa5SAzael Avalos 					      u32 mode)
929182bcaa5SAzael Avalos {
930182bcaa5SAzael Avalos 	u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
931182bcaa5SAzael Avalos 	u32 out[TCI_WORDS];
932182bcaa5SAzael Avalos 	acpi_status status;
933182bcaa5SAzael Avalos 
934182bcaa5SAzael Avalos 	if (!sci_open(dev))
935182bcaa5SAzael Avalos 		return -EIO;
936182bcaa5SAzael Avalos 
937182bcaa5SAzael Avalos 	in[2] = mode;
938182bcaa5SAzael Avalos 	in[5] = SCI_USB_CHARGE_BAT_LVL;
939182bcaa5SAzael Avalos 	status = tci_raw(dev, in, out);
940182bcaa5SAzael Avalos 	sci_close(dev);
941182bcaa5SAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
942182bcaa5SAzael Avalos 		pr_err("ACPI call to set USB S&C battery level failed\n");
943182bcaa5SAzael Avalos 		return -EIO;
944182bcaa5SAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
945182bcaa5SAzael Avalos 		pr_info("USB Sleep and Charge not supported\n");
946182bcaa5SAzael Avalos 		return -ENODEV;
947182bcaa5SAzael Avalos 	} else if (out[0] == TOS_INPUT_DATA_ERROR) {
948182bcaa5SAzael Avalos 		return -EIO;
949182bcaa5SAzael Avalos 	}
950182bcaa5SAzael Avalos 
951182bcaa5SAzael Avalos 	return 0;
952182bcaa5SAzael Avalos }
953182bcaa5SAzael Avalos 
954bb3fe01fSAzael Avalos static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
955bb3fe01fSAzael Avalos 					u32 *state)
956bb3fe01fSAzael Avalos {
957bb3fe01fSAzael Avalos 	u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
958bb3fe01fSAzael Avalos 	u32 out[TCI_WORDS];
959bb3fe01fSAzael Avalos 	acpi_status status;
960bb3fe01fSAzael Avalos 
961bb3fe01fSAzael Avalos 	if (!sci_open(dev))
962bb3fe01fSAzael Avalos 		return -EIO;
963bb3fe01fSAzael Avalos 
964bb3fe01fSAzael Avalos 	in[5] = SCI_USB_CHARGE_RAPID_DSP;
965bb3fe01fSAzael Avalos 	status = tci_raw(dev, in, out);
966bb3fe01fSAzael Avalos 	sci_close(dev);
967bb3fe01fSAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
968*bb26f189SAzael Avalos 		pr_err("ACPI call to get USB Rapid Charge failed\n");
969bb3fe01fSAzael Avalos 		return -EIO;
970bb3fe01fSAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED ||
971bb3fe01fSAzael Avalos 		   out[0] == TOS_INPUT_DATA_ERROR) {
972*bb26f189SAzael Avalos 		pr_info("USB Rapid Charge not supported\n");
973bb3fe01fSAzael Avalos 		return -ENODEV;
974bb3fe01fSAzael Avalos 	}
975bb3fe01fSAzael Avalos 
976bb3fe01fSAzael Avalos 	*state = out[2];
977bb3fe01fSAzael Avalos 
978bb3fe01fSAzael Avalos 	return 0;
979bb3fe01fSAzael Avalos }
980bb3fe01fSAzael Avalos 
981bb3fe01fSAzael Avalos static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
982bb3fe01fSAzael Avalos 					u32 state)
983bb3fe01fSAzael Avalos {
984bb3fe01fSAzael Avalos 	u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
985bb3fe01fSAzael Avalos 	u32 out[TCI_WORDS];
986bb3fe01fSAzael Avalos 	acpi_status status;
987bb3fe01fSAzael Avalos 
988bb3fe01fSAzael Avalos 	if (!sci_open(dev))
989bb3fe01fSAzael Avalos 		return -EIO;
990bb3fe01fSAzael Avalos 
991bb3fe01fSAzael Avalos 	in[2] = state;
992bb3fe01fSAzael Avalos 	in[5] = SCI_USB_CHARGE_RAPID_DSP;
993bb3fe01fSAzael Avalos 	status = tci_raw(dev, in, out);
994bb3fe01fSAzael Avalos 	sci_close(dev);
995bb3fe01fSAzael Avalos 	if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
996*bb26f189SAzael Avalos 		pr_err("ACPI call to set USB Rapid Charge failed\n");
997bb3fe01fSAzael Avalos 		return -EIO;
998bb3fe01fSAzael Avalos 	} else if (out[0] == TOS_NOT_SUPPORTED) {
999*bb26f189SAzael Avalos 		pr_info("USB Rapid Charge not supported\n");
1000bb3fe01fSAzael Avalos 		return -ENODEV;
1001bb3fe01fSAzael Avalos 	} else if (out[0] == TOS_INPUT_DATA_ERROR) {
1002bb3fe01fSAzael Avalos 		return -EIO;
1003bb3fe01fSAzael Avalos 	}
1004bb3fe01fSAzael Avalos 
1005bb3fe01fSAzael Avalos 	return 0;
1006bb3fe01fSAzael Avalos }
1007bb3fe01fSAzael Avalos 
1008172ce0a9SAzael Avalos static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
1009172ce0a9SAzael Avalos {
1010172ce0a9SAzael Avalos 	u32 result;
1011172ce0a9SAzael Avalos 
1012172ce0a9SAzael Avalos 	if (!sci_open(dev))
1013172ce0a9SAzael Avalos 		return -EIO;
1014172ce0a9SAzael Avalos 
1015172ce0a9SAzael Avalos 	result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
1016172ce0a9SAzael Avalos 	sci_close(dev);
1017172ce0a9SAzael Avalos 	if (result == TOS_FAILURE) {
1018*bb26f189SAzael Avalos 		pr_err("ACPI call to get Sleep and Music failed\n");
1019172ce0a9SAzael Avalos 		return -EIO;
1020172ce0a9SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
1021*bb26f189SAzael Avalos 		pr_info("Sleep and Music not supported\n");
1022172ce0a9SAzael Avalos 		return -ENODEV;
1023172ce0a9SAzael Avalos 	} else if (result == TOS_INPUT_DATA_ERROR) {
1024172ce0a9SAzael Avalos 		return -EIO;
1025172ce0a9SAzael Avalos 	}
1026172ce0a9SAzael Avalos 
1027172ce0a9SAzael Avalos 	return 0;
1028172ce0a9SAzael Avalos }
1029172ce0a9SAzael Avalos 
1030172ce0a9SAzael Avalos static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1031172ce0a9SAzael Avalos {
1032172ce0a9SAzael Avalos 	u32 result;
1033172ce0a9SAzael Avalos 
1034172ce0a9SAzael Avalos 	if (!sci_open(dev))
1035172ce0a9SAzael Avalos 		return -EIO;
1036172ce0a9SAzael Avalos 
1037172ce0a9SAzael Avalos 	result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1038172ce0a9SAzael Avalos 	sci_close(dev);
1039172ce0a9SAzael Avalos 	if (result == TOS_FAILURE) {
1040*bb26f189SAzael Avalos 		pr_err("ACPI call to set Sleep and Music failed\n");
1041172ce0a9SAzael Avalos 		return -EIO;
1042172ce0a9SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
1043*bb26f189SAzael Avalos 		pr_info("Sleep and Music not supported\n");
1044172ce0a9SAzael Avalos 		return -ENODEV;
1045172ce0a9SAzael Avalos 	} else if (result == TOS_INPUT_DATA_ERROR) {
1046172ce0a9SAzael Avalos 		return -EIO;
1047172ce0a9SAzael Avalos 	}
1048172ce0a9SAzael Avalos 
1049172ce0a9SAzael Avalos 	return 0;
1050172ce0a9SAzael Avalos }
1051172ce0a9SAzael Avalos 
1052bae84195SAzael Avalos /* Keyboard function keys */
1053bae84195SAzael Avalos static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1054bae84195SAzael Avalos {
1055bae84195SAzael Avalos 	u32 result;
1056bae84195SAzael Avalos 
1057bae84195SAzael Avalos 	if (!sci_open(dev))
1058bae84195SAzael Avalos 		return -EIO;
1059bae84195SAzael Avalos 
1060bae84195SAzael Avalos 	result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1061bae84195SAzael Avalos 	sci_close(dev);
1062bae84195SAzael Avalos 	if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1063bae84195SAzael Avalos 		pr_err("ACPI call to get KBD function keys failed\n");
1064bae84195SAzael Avalos 		return -EIO;
1065bae84195SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
1066bae84195SAzael Avalos 		pr_info("KBD function keys not supported\n");
1067bae84195SAzael Avalos 		return -ENODEV;
1068bae84195SAzael Avalos 	}
1069bae84195SAzael Avalos 
1070bae84195SAzael Avalos 	return 0;
1071bae84195SAzael Avalos }
1072bae84195SAzael Avalos 
1073bae84195SAzael Avalos static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1074bae84195SAzael Avalos {
1075bae84195SAzael Avalos 	u32 result;
1076bae84195SAzael Avalos 
1077bae84195SAzael Avalos 	if (!sci_open(dev))
1078bae84195SAzael Avalos 		return -EIO;
1079bae84195SAzael Avalos 
1080bae84195SAzael Avalos 	result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1081bae84195SAzael Avalos 	sci_close(dev);
1082bae84195SAzael Avalos 	if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1083bae84195SAzael Avalos 		pr_err("ACPI call to set KBD function keys failed\n");
1084bae84195SAzael Avalos 		return -EIO;
1085bae84195SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
1086bae84195SAzael Avalos 		pr_info("KBD function keys not supported\n");
1087bae84195SAzael Avalos 		return -ENODEV;
1088bae84195SAzael Avalos 	}
1089bae84195SAzael Avalos 
1090bae84195SAzael Avalos 	return 0;
1091bae84195SAzael Avalos }
1092bae84195SAzael Avalos 
109335d53ceaSAzael Avalos /* Panel Power ON */
109435d53ceaSAzael Avalos static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
109535d53ceaSAzael Avalos {
109635d53ceaSAzael Avalos 	u32 result;
109735d53ceaSAzael Avalos 
109835d53ceaSAzael Avalos 	if (!sci_open(dev))
109935d53ceaSAzael Avalos 		return -EIO;
110035d53ceaSAzael Avalos 
110135d53ceaSAzael Avalos 	result = sci_read(dev, SCI_PANEL_POWER_ON, state);
110235d53ceaSAzael Avalos 	sci_close(dev);
110335d53ceaSAzael Avalos 	if (result == TOS_FAILURE) {
110435d53ceaSAzael Avalos 		pr_err("ACPI call to get Panel Power ON failed\n");
110535d53ceaSAzael Avalos 		return -EIO;
110635d53ceaSAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
110735d53ceaSAzael Avalos 		pr_info("Panel Power on not supported\n");
110835d53ceaSAzael Avalos 		return -ENODEV;
110935d53ceaSAzael Avalos 	} else if (result == TOS_INPUT_DATA_ERROR) {
111035d53ceaSAzael Avalos 		return -EIO;
111135d53ceaSAzael Avalos 	}
111235d53ceaSAzael Avalos 
111335d53ceaSAzael Avalos 	return 0;
111435d53ceaSAzael Avalos }
111535d53ceaSAzael Avalos 
111635d53ceaSAzael Avalos static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
111735d53ceaSAzael Avalos {
111835d53ceaSAzael Avalos 	u32 result;
111935d53ceaSAzael Avalos 
112035d53ceaSAzael Avalos 	if (!sci_open(dev))
112135d53ceaSAzael Avalos 		return -EIO;
112235d53ceaSAzael Avalos 
112335d53ceaSAzael Avalos 	result = sci_write(dev, SCI_PANEL_POWER_ON, state);
112435d53ceaSAzael Avalos 	sci_close(dev);
112535d53ceaSAzael Avalos 	if (result == TOS_FAILURE) {
112635d53ceaSAzael Avalos 		pr_err("ACPI call to set Panel Power ON failed\n");
112735d53ceaSAzael Avalos 		return -EIO;
112835d53ceaSAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
112935d53ceaSAzael Avalos 		pr_info("Panel Power ON not supported\n");
113035d53ceaSAzael Avalos 		return -ENODEV;
113135d53ceaSAzael Avalos 	} else if (result == TOS_INPUT_DATA_ERROR) {
113235d53ceaSAzael Avalos 		return -EIO;
113335d53ceaSAzael Avalos 	}
113435d53ceaSAzael Avalos 
113535d53ceaSAzael Avalos 	return 0;
113635d53ceaSAzael Avalos }
113735d53ceaSAzael Avalos 
113817fe4b3dSAzael Avalos /* USB Three */
113917fe4b3dSAzael Avalos static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
114017fe4b3dSAzael Avalos {
114117fe4b3dSAzael Avalos 	u32 result;
114217fe4b3dSAzael Avalos 
114317fe4b3dSAzael Avalos 	if (!sci_open(dev))
114417fe4b3dSAzael Avalos 		return -EIO;
114517fe4b3dSAzael Avalos 
114617fe4b3dSAzael Avalos 	result = sci_read(dev, SCI_USB_THREE, state);
114717fe4b3dSAzael Avalos 	sci_close(dev);
114817fe4b3dSAzael Avalos 	if (result == TOS_FAILURE) {
114917fe4b3dSAzael Avalos 		pr_err("ACPI call to get USB 3 failed\n");
115017fe4b3dSAzael Avalos 		return -EIO;
115117fe4b3dSAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
115217fe4b3dSAzael Avalos 		pr_info("USB 3 not supported\n");
115317fe4b3dSAzael Avalos 		return -ENODEV;
115417fe4b3dSAzael Avalos 	} else if (result == TOS_INPUT_DATA_ERROR) {
115517fe4b3dSAzael Avalos 		return -EIO;
115617fe4b3dSAzael Avalos 	}
115717fe4b3dSAzael Avalos 
115817fe4b3dSAzael Avalos 	return 0;
115917fe4b3dSAzael Avalos }
116017fe4b3dSAzael Avalos 
116117fe4b3dSAzael Avalos static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
116217fe4b3dSAzael Avalos {
116317fe4b3dSAzael Avalos 	u32 result;
116417fe4b3dSAzael Avalos 
116517fe4b3dSAzael Avalos 	if (!sci_open(dev))
116617fe4b3dSAzael Avalos 		return -EIO;
116717fe4b3dSAzael Avalos 
116817fe4b3dSAzael Avalos 	result = sci_write(dev, SCI_USB_THREE, state);
116917fe4b3dSAzael Avalos 	sci_close(dev);
117017fe4b3dSAzael Avalos 	if (result == TOS_FAILURE) {
117117fe4b3dSAzael Avalos 		pr_err("ACPI call to set USB 3 failed\n");
117217fe4b3dSAzael Avalos 		return -EIO;
117317fe4b3dSAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
117417fe4b3dSAzael Avalos 		pr_info("USB 3 not supported\n");
117517fe4b3dSAzael Avalos 		return -ENODEV;
117617fe4b3dSAzael Avalos 	} else if (result == TOS_INPUT_DATA_ERROR) {
117717fe4b3dSAzael Avalos 		return -EIO;
117817fe4b3dSAzael Avalos 	}
117917fe4b3dSAzael Avalos 
118017fe4b3dSAzael Avalos 	return 0;
118117fe4b3dSAzael Avalos }
118217fe4b3dSAzael Avalos 
118356e6b353SAzael Avalos /* Hotkey Event type */
118456e6b353SAzael Avalos static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
118556e6b353SAzael Avalos 					 u32 *type)
118656e6b353SAzael Avalos {
118756e6b353SAzael Avalos 	u32 val1 = 0x03;
118856e6b353SAzael Avalos 	u32 val2 = 0;
118956e6b353SAzael Avalos 	u32 result;
119056e6b353SAzael Avalos 
119156e6b353SAzael Avalos 	result = hci_read2(dev, HCI_SYSTEM_INFO, &val1, &val2);
119256e6b353SAzael Avalos 	if (result == TOS_FAILURE) {
119356e6b353SAzael Avalos 		pr_err("ACPI call to get System type failed\n");
119456e6b353SAzael Avalos 		return -EIO;
119556e6b353SAzael Avalos 	} else if (result == TOS_NOT_SUPPORTED) {
119656e6b353SAzael Avalos 		pr_info("System type not supported\n");
119756e6b353SAzael Avalos 		return -ENODEV;
119856e6b353SAzael Avalos 	}
119956e6b353SAzael Avalos 
120056e6b353SAzael Avalos 	*type = val2;
120156e6b353SAzael Avalos 
120256e6b353SAzael Avalos 	return 0;
120356e6b353SAzael Avalos }
120456e6b353SAzael Avalos 
1205b4f9fe12SLen Brown /* Bluetooth rfkill handlers */
1206b4f9fe12SLen Brown 
1207135740deSSeth Forshee static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
1208b4f9fe12SLen Brown {
1209b4f9fe12SLen Brown 	u32 hci_result;
1210b4f9fe12SLen Brown 	u32 value, value2;
1211b4f9fe12SLen Brown 
1212b4f9fe12SLen Brown 	value = 0;
1213b4f9fe12SLen Brown 	value2 = 0;
1214893f3f62SAzael Avalos 	hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
12151864bbc2SAzael Avalos 	if (hci_result == TOS_SUCCESS)
1216b4f9fe12SLen Brown 		*present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
1217b4f9fe12SLen Brown 
1218b4f9fe12SLen Brown 	return hci_result;
1219b4f9fe12SLen Brown }
1220b4f9fe12SLen Brown 
1221135740deSSeth Forshee static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
1222b4f9fe12SLen Brown {
1223b4f9fe12SLen Brown 	u32 hci_result;
1224b4f9fe12SLen Brown 	u32 value, value2;
1225b4f9fe12SLen Brown 
1226b4f9fe12SLen Brown 	value = 0;
1227b4f9fe12SLen Brown 	value2 = 0x0001;
1228893f3f62SAzael Avalos 	hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
1229b4f9fe12SLen Brown 
1230b4f9fe12SLen Brown 	*radio_state = value & HCI_WIRELESS_KILL_SWITCH;
1231b4f9fe12SLen Brown 	return hci_result;
1232b4f9fe12SLen Brown }
1233b4f9fe12SLen Brown 
123419d337dfSJohannes Berg static int bt_rfkill_set_block(void *data, bool blocked)
1235b4f9fe12SLen Brown {
123619d337dfSJohannes Berg 	struct toshiba_acpi_dev *dev = data;
1237b4f9fe12SLen Brown 	u32 result1, result2;
1238b4f9fe12SLen Brown 	u32 value;
123919d337dfSJohannes Berg 	int err;
1240b4f9fe12SLen Brown 	bool radio_state;
1241b4f9fe12SLen Brown 
124219d337dfSJohannes Berg 	value = (blocked == false);
1243b4f9fe12SLen Brown 
1244b4f9fe12SLen Brown 	mutex_lock(&dev->mutex);
12451864bbc2SAzael Avalos 	if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
124632bcd5cbSSeth Forshee 		err = -EIO;
124719d337dfSJohannes Berg 		goto out;
1248b4f9fe12SLen Brown 	}
1249b4f9fe12SLen Brown 
125019d337dfSJohannes Berg 	if (!radio_state) {
125119d337dfSJohannes Berg 		err = 0;
125219d337dfSJohannes Berg 		goto out;
125319d337dfSJohannes Berg 	}
125419d337dfSJohannes Berg 
1255893f3f62SAzael Avalos 	result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
1256893f3f62SAzael Avalos 	result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
125719d337dfSJohannes Berg 
12581864bbc2SAzael Avalos 	if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
125932bcd5cbSSeth Forshee 		err = -EIO;
126019d337dfSJohannes Berg 	else
126119d337dfSJohannes Berg 		err = 0;
126219d337dfSJohannes Berg  out:
126319d337dfSJohannes Berg 	mutex_unlock(&dev->mutex);
126419d337dfSJohannes Berg 	return err;
126519d337dfSJohannes Berg }
126619d337dfSJohannes Berg 
126719d337dfSJohannes Berg static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
1268b4f9fe12SLen Brown {
1269b4f9fe12SLen Brown 	bool new_rfk_state;
1270b4f9fe12SLen Brown 	bool value;
1271b4f9fe12SLen Brown 	u32 hci_result;
127219d337dfSJohannes Berg 	struct toshiba_acpi_dev *dev = data;
127319d337dfSJohannes Berg 
127419d337dfSJohannes Berg 	mutex_lock(&dev->mutex);
1275b4f9fe12SLen Brown 
1276135740deSSeth Forshee 	hci_result = hci_get_radio_state(dev, &value);
12771864bbc2SAzael Avalos 	if (hci_result != TOS_SUCCESS) {
127819d337dfSJohannes Berg 		/* Can't do anything useful */
127919d337dfSJohannes Berg 		mutex_unlock(&dev->mutex);
128082e7784fSJiri Slaby 		return;
128119d337dfSJohannes Berg 	}
1282b4f9fe12SLen Brown 
1283b4f9fe12SLen Brown 	new_rfk_state = value;
1284b4f9fe12SLen Brown 
1285b4f9fe12SLen Brown 	mutex_unlock(&dev->mutex);
1286b4f9fe12SLen Brown 
128719d337dfSJohannes Berg 	if (rfkill_set_hw_state(rfkill, !new_rfk_state))
128819d337dfSJohannes Berg 		bt_rfkill_set_block(data, true);
1289b4f9fe12SLen Brown }
129019d337dfSJohannes Berg 
129119d337dfSJohannes Berg static const struct rfkill_ops toshiba_rfk_ops = {
129219d337dfSJohannes Berg 	.set_block = bt_rfkill_set_block,
129319d337dfSJohannes Berg 	.poll = bt_rfkill_poll,
129419d337dfSJohannes Berg };
1295b4f9fe12SLen Brown 
1296121b7b0dSAkio Idehara static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1297121b7b0dSAkio Idehara {
1298121b7b0dSAkio Idehara 	u32 hci_result;
1299121b7b0dSAkio Idehara 	u32 status;
1300121b7b0dSAkio Idehara 
1301893f3f62SAzael Avalos 	hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
1302121b7b0dSAkio Idehara 	*enabled = !status;
13031864bbc2SAzael Avalos 	return hci_result == TOS_SUCCESS ? 0 : -EIO;
1304121b7b0dSAkio Idehara }
1305121b7b0dSAkio Idehara 
1306121b7b0dSAkio Idehara static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1307121b7b0dSAkio Idehara {
1308121b7b0dSAkio Idehara 	u32 hci_result;
1309121b7b0dSAkio Idehara 	u32 value = !enable;
1310121b7b0dSAkio Idehara 
1311893f3f62SAzael Avalos 	hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
13121864bbc2SAzael Avalos 	return hci_result == TOS_SUCCESS ? 0 : -EIO;
1313121b7b0dSAkio Idehara }
1314121b7b0dSAkio Idehara 
1315b4f9fe12SLen Brown static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
1316b4f9fe12SLen Brown 
131762cce752SSeth Forshee static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
1318b4f9fe12SLen Brown {
1319b4f9fe12SLen Brown 	u32 hci_result;
1320b4f9fe12SLen Brown 	u32 value;
1321121b7b0dSAkio Idehara 	int brightness = 0;
1322121b7b0dSAkio Idehara 
1323121b7b0dSAkio Idehara 	if (dev->tr_backlight_supported) {
1324121b7b0dSAkio Idehara 		bool enabled;
1325121b7b0dSAkio Idehara 		int ret = get_tr_backlight_status(dev, &enabled);
1326b5163992SAzael Avalos 
1327121b7b0dSAkio Idehara 		if (ret)
1328121b7b0dSAkio Idehara 			return ret;
1329121b7b0dSAkio Idehara 		if (enabled)
1330121b7b0dSAkio Idehara 			return 0;
1331121b7b0dSAkio Idehara 		brightness++;
1332121b7b0dSAkio Idehara 	}
1333b4f9fe12SLen Brown 
1334893f3f62SAzael Avalos 	hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
13351864bbc2SAzael Avalos 	if (hci_result == TOS_SUCCESS)
1336121b7b0dSAkio Idehara 		return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
133732bcd5cbSSeth Forshee 
133832bcd5cbSSeth Forshee 	return -EIO;
1339b4f9fe12SLen Brown }
1340b4f9fe12SLen Brown 
134162cce752SSeth Forshee static int get_lcd_brightness(struct backlight_device *bd)
134262cce752SSeth Forshee {
134362cce752SSeth Forshee 	struct toshiba_acpi_dev *dev = bl_get_data(bd);
1344b5163992SAzael Avalos 
134562cce752SSeth Forshee 	return __get_lcd_brightness(dev);
134662cce752SSeth Forshee }
134762cce752SSeth Forshee 
1348936c8bcdSAlexey Dobriyan static int lcd_proc_show(struct seq_file *m, void *v)
1349b4f9fe12SLen Brown {
1350135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = m->private;
1351135740deSSeth Forshee 	int value;
1352121b7b0dSAkio Idehara 	int levels;
1353b4f9fe12SLen Brown 
1354135740deSSeth Forshee 	if (!dev->backlight_dev)
1355135740deSSeth Forshee 		return -ENODEV;
1356135740deSSeth Forshee 
1357121b7b0dSAkio Idehara 	levels = dev->backlight_dev->props.max_brightness + 1;
135862cce752SSeth Forshee 	value = get_lcd_brightness(dev->backlight_dev);
1359b4f9fe12SLen Brown 	if (value >= 0) {
1360936c8bcdSAlexey Dobriyan 		seq_printf(m, "brightness:              %d\n", value);
1361121b7b0dSAkio Idehara 		seq_printf(m, "brightness_levels:       %d\n", levels);
136232bcd5cbSSeth Forshee 		return 0;
1363b4f9fe12SLen Brown 	}
1364b4f9fe12SLen Brown 
136532bcd5cbSSeth Forshee 	pr_err("Error reading LCD brightness\n");
136632bcd5cbSSeth Forshee 	return -EIO;
1367936c8bcdSAlexey Dobriyan }
1368936c8bcdSAlexey Dobriyan 
1369936c8bcdSAlexey Dobriyan static int lcd_proc_open(struct inode *inode, struct file *file)
1370936c8bcdSAlexey Dobriyan {
1371d9dda78bSAl Viro 	return single_open(file, lcd_proc_show, PDE_DATA(inode));
1372b4f9fe12SLen Brown }
1373b4f9fe12SLen Brown 
137462cce752SSeth Forshee static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
1375b4f9fe12SLen Brown {
1376a39f46dfSAzael Avalos 	u32 hci_result;
1377b4f9fe12SLen Brown 
1378121b7b0dSAkio Idehara 	if (dev->tr_backlight_supported) {
1379121b7b0dSAkio Idehara 		bool enable = !value;
1380121b7b0dSAkio Idehara 		int ret = set_tr_backlight_status(dev, enable);
1381b5163992SAzael Avalos 
1382121b7b0dSAkio Idehara 		if (ret)
1383121b7b0dSAkio Idehara 			return ret;
1384121b7b0dSAkio Idehara 		if (value)
1385121b7b0dSAkio Idehara 			value--;
1386121b7b0dSAkio Idehara 	}
1387121b7b0dSAkio Idehara 
1388a39f46dfSAzael Avalos 	value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1389a39f46dfSAzael Avalos 	hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1390a39f46dfSAzael Avalos 	return hci_result == TOS_SUCCESS ? 0 : -EIO;
1391b4f9fe12SLen Brown }
1392b4f9fe12SLen Brown 
1393b4f9fe12SLen Brown static int set_lcd_status(struct backlight_device *bd)
1394b4f9fe12SLen Brown {
1395135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = bl_get_data(bd);
1396b5163992SAzael Avalos 
139762cce752SSeth Forshee 	return set_lcd_brightness(dev, bd->props.brightness);
1398b4f9fe12SLen Brown }
1399b4f9fe12SLen Brown 
1400936c8bcdSAlexey Dobriyan static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1401936c8bcdSAlexey Dobriyan 			      size_t count, loff_t *pos)
1402b4f9fe12SLen Brown {
1403d9dda78bSAl Viro 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1404936c8bcdSAlexey Dobriyan 	char cmd[42];
1405936c8bcdSAlexey Dobriyan 	size_t len;
1406b4f9fe12SLen Brown 	int value;
1407b4f9fe12SLen Brown 	int ret;
1408121b7b0dSAkio Idehara 	int levels = dev->backlight_dev->props.max_brightness + 1;
1409b4f9fe12SLen Brown 
1410936c8bcdSAlexey Dobriyan 	len = min(count, sizeof(cmd) - 1);
1411936c8bcdSAlexey Dobriyan 	if (copy_from_user(cmd, buf, len))
1412936c8bcdSAlexey Dobriyan 		return -EFAULT;
1413936c8bcdSAlexey Dobriyan 	cmd[len] = '\0';
1414936c8bcdSAlexey Dobriyan 
1415936c8bcdSAlexey Dobriyan 	if (sscanf(cmd, " brightness : %i", &value) == 1 &&
1416121b7b0dSAkio Idehara 	    value >= 0 && value < levels) {
141762cce752SSeth Forshee 		ret = set_lcd_brightness(dev, value);
1418b4f9fe12SLen Brown 		if (ret == 0)
1419b4f9fe12SLen Brown 			ret = count;
1420b4f9fe12SLen Brown 	} else {
1421b4f9fe12SLen Brown 		ret = -EINVAL;
1422b4f9fe12SLen Brown 	}
1423b4f9fe12SLen Brown 	return ret;
1424b4f9fe12SLen Brown }
1425b4f9fe12SLen Brown 
1426936c8bcdSAlexey Dobriyan static const struct file_operations lcd_proc_fops = {
1427936c8bcdSAlexey Dobriyan 	.owner		= THIS_MODULE,
1428936c8bcdSAlexey Dobriyan 	.open		= lcd_proc_open,
1429936c8bcdSAlexey Dobriyan 	.read		= seq_read,
1430936c8bcdSAlexey Dobriyan 	.llseek		= seq_lseek,
1431936c8bcdSAlexey Dobriyan 	.release	= single_release,
1432936c8bcdSAlexey Dobriyan 	.write		= lcd_proc_write,
1433936c8bcdSAlexey Dobriyan };
1434936c8bcdSAlexey Dobriyan 
143536d03f93SSeth Forshee static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
143636d03f93SSeth Forshee {
143736d03f93SSeth Forshee 	u32 hci_result;
143836d03f93SSeth Forshee 
1439893f3f62SAzael Avalos 	hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
14401864bbc2SAzael Avalos 	return hci_result == TOS_SUCCESS ? 0 : -EIO;
144136d03f93SSeth Forshee }
144236d03f93SSeth Forshee 
1443936c8bcdSAlexey Dobriyan static int video_proc_show(struct seq_file *m, void *v)
1444b4f9fe12SLen Brown {
1445135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = m->private;
1446b4f9fe12SLen Brown 	u32 value;
144736d03f93SSeth Forshee 	int ret;
1448b4f9fe12SLen Brown 
144936d03f93SSeth Forshee 	ret = get_video_status(dev, &value);
145036d03f93SSeth Forshee 	if (!ret) {
1451b4f9fe12SLen Brown 		int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1452b4f9fe12SLen Brown 		int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
1453b4f9fe12SLen Brown 		int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
1454b5163992SAzael Avalos 
1455936c8bcdSAlexey Dobriyan 		seq_printf(m, "lcd_out:                 %d\n", is_lcd);
1456936c8bcdSAlexey Dobriyan 		seq_printf(m, "crt_out:                 %d\n", is_crt);
1457936c8bcdSAlexey Dobriyan 		seq_printf(m, "tv_out:                  %d\n", is_tv);
1458b4f9fe12SLen Brown 	}
1459b4f9fe12SLen Brown 
146036d03f93SSeth Forshee 	return ret;
1461b4f9fe12SLen Brown }
1462b4f9fe12SLen Brown 
1463936c8bcdSAlexey Dobriyan static int video_proc_open(struct inode *inode, struct file *file)
1464b4f9fe12SLen Brown {
1465d9dda78bSAl Viro 	return single_open(file, video_proc_show, PDE_DATA(inode));
1466936c8bcdSAlexey Dobriyan }
1467936c8bcdSAlexey Dobriyan 
1468936c8bcdSAlexey Dobriyan static ssize_t video_proc_write(struct file *file, const char __user *buf,
1469936c8bcdSAlexey Dobriyan 				size_t count, loff_t *pos)
1470936c8bcdSAlexey Dobriyan {
1471d9dda78bSAl Viro 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1472936c8bcdSAlexey Dobriyan 	char *cmd, *buffer;
147336d03f93SSeth Forshee 	int ret;
1474b4f9fe12SLen Brown 	int value;
1475b4f9fe12SLen Brown 	int remain = count;
1476b4f9fe12SLen Brown 	int lcd_out = -1;
1477b4f9fe12SLen Brown 	int crt_out = -1;
1478b4f9fe12SLen Brown 	int tv_out = -1;
1479b4f9fe12SLen Brown 	u32 video_out;
1480b4f9fe12SLen Brown 
1481936c8bcdSAlexey Dobriyan 	cmd = kmalloc(count + 1, GFP_KERNEL);
1482936c8bcdSAlexey Dobriyan 	if (!cmd)
1483936c8bcdSAlexey Dobriyan 		return -ENOMEM;
1484936c8bcdSAlexey Dobriyan 	if (copy_from_user(cmd, buf, count)) {
1485936c8bcdSAlexey Dobriyan 		kfree(cmd);
1486936c8bcdSAlexey Dobriyan 		return -EFAULT;
1487936c8bcdSAlexey Dobriyan 	}
1488936c8bcdSAlexey Dobriyan 	cmd[count] = '\0';
1489936c8bcdSAlexey Dobriyan 
1490936c8bcdSAlexey Dobriyan 	buffer = cmd;
1491936c8bcdSAlexey Dobriyan 
1492e0769fe6SDarren Hart 	/*
1493e0769fe6SDarren Hart 	 * Scan expression.  Multiple expressions may be delimited with ;
1494e0769fe6SDarren Hart 	 * NOTE: To keep scanning simple, invalid fields are ignored.
1495b4f9fe12SLen Brown 	 */
1496b4f9fe12SLen Brown 	while (remain) {
1497b4f9fe12SLen Brown 		if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1498b4f9fe12SLen Brown 			lcd_out = value & 1;
1499b4f9fe12SLen Brown 		else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1500b4f9fe12SLen Brown 			crt_out = value & 1;
1501b4f9fe12SLen Brown 		else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1502b4f9fe12SLen Brown 			tv_out = value & 1;
1503e0769fe6SDarren Hart 		/* Advance to one character past the next ; */
1504b4f9fe12SLen Brown 		do {
1505b4f9fe12SLen Brown 			++buffer;
1506b4f9fe12SLen Brown 			--remain;
1507b5163992SAzael Avalos 		} while (remain && *(buffer - 1) != ';');
1508b4f9fe12SLen Brown 	}
1509b4f9fe12SLen Brown 
1510936c8bcdSAlexey Dobriyan 	kfree(cmd);
1511936c8bcdSAlexey Dobriyan 
151236d03f93SSeth Forshee 	ret = get_video_status(dev, &video_out);
151336d03f93SSeth Forshee 	if (!ret) {
1514b4f9fe12SLen Brown 		unsigned int new_video_out = video_out;
1515b5163992SAzael Avalos 
1516b4f9fe12SLen Brown 		if (lcd_out != -1)
1517b4f9fe12SLen Brown 			_set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1518b4f9fe12SLen Brown 		if (crt_out != -1)
1519b4f9fe12SLen Brown 			_set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1520b4f9fe12SLen Brown 		if (tv_out != -1)
1521b4f9fe12SLen Brown 			_set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1522e0769fe6SDarren Hart 		/*
1523e0769fe6SDarren Hart 		 * To avoid unnecessary video disruption, only write the new
1524b4f9fe12SLen Brown 		 * video setting if something changed. */
1525b4f9fe12SLen Brown 		if (new_video_out != video_out)
152632bcd5cbSSeth Forshee 			ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1527b4f9fe12SLen Brown 	}
1528b4f9fe12SLen Brown 
152932bcd5cbSSeth Forshee 	return ret ? ret : count;
1530b4f9fe12SLen Brown }
1531b4f9fe12SLen Brown 
1532936c8bcdSAlexey Dobriyan static const struct file_operations video_proc_fops = {
1533936c8bcdSAlexey Dobriyan 	.owner		= THIS_MODULE,
1534936c8bcdSAlexey Dobriyan 	.open		= video_proc_open,
1535936c8bcdSAlexey Dobriyan 	.read		= seq_read,
1536936c8bcdSAlexey Dobriyan 	.llseek		= seq_lseek,
1537936c8bcdSAlexey Dobriyan 	.release	= single_release,
1538936c8bcdSAlexey Dobriyan 	.write		= video_proc_write,
1539936c8bcdSAlexey Dobriyan };
1540936c8bcdSAlexey Dobriyan 
154136d03f93SSeth Forshee static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
154236d03f93SSeth Forshee {
154336d03f93SSeth Forshee 	u32 hci_result;
154436d03f93SSeth Forshee 
1545893f3f62SAzael Avalos 	hci_result = hci_read1(dev, HCI_FAN, status);
15461864bbc2SAzael Avalos 	return hci_result == TOS_SUCCESS ? 0 : -EIO;
154736d03f93SSeth Forshee }
154836d03f93SSeth Forshee 
1549936c8bcdSAlexey Dobriyan static int fan_proc_show(struct seq_file *m, void *v)
1550b4f9fe12SLen Brown {
1551135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = m->private;
155236d03f93SSeth Forshee 	int ret;
1553b4f9fe12SLen Brown 	u32 value;
1554b4f9fe12SLen Brown 
155536d03f93SSeth Forshee 	ret = get_fan_status(dev, &value);
155636d03f93SSeth Forshee 	if (!ret) {
1557936c8bcdSAlexey Dobriyan 		seq_printf(m, "running:                 %d\n", (value > 0));
1558135740deSSeth Forshee 		seq_printf(m, "force_on:                %d\n", dev->force_fan);
1559b4f9fe12SLen Brown 	}
1560b4f9fe12SLen Brown 
156136d03f93SSeth Forshee 	return ret;
1562b4f9fe12SLen Brown }
1563b4f9fe12SLen Brown 
1564936c8bcdSAlexey Dobriyan static int fan_proc_open(struct inode *inode, struct file *file)
1565b4f9fe12SLen Brown {
1566d9dda78bSAl Viro 	return single_open(file, fan_proc_show, PDE_DATA(inode));
1567936c8bcdSAlexey Dobriyan }
1568936c8bcdSAlexey Dobriyan 
1569936c8bcdSAlexey Dobriyan static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1570936c8bcdSAlexey Dobriyan 			      size_t count, loff_t *pos)
1571936c8bcdSAlexey Dobriyan {
1572d9dda78bSAl Viro 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1573936c8bcdSAlexey Dobriyan 	char cmd[42];
1574936c8bcdSAlexey Dobriyan 	size_t len;
1575b4f9fe12SLen Brown 	int value;
1576b4f9fe12SLen Brown 	u32 hci_result;
1577b4f9fe12SLen Brown 
1578936c8bcdSAlexey Dobriyan 	len = min(count, sizeof(cmd) - 1);
1579936c8bcdSAlexey Dobriyan 	if (copy_from_user(cmd, buf, len))
1580936c8bcdSAlexey Dobriyan 		return -EFAULT;
1581936c8bcdSAlexey Dobriyan 	cmd[len] = '\0';
1582936c8bcdSAlexey Dobriyan 
1583936c8bcdSAlexey Dobriyan 	if (sscanf(cmd, " force_on : %i", &value) == 1 &&
1584b4f9fe12SLen Brown 	    value >= 0 && value <= 1) {
1585893f3f62SAzael Avalos 		hci_result = hci_write1(dev, HCI_FAN, value);
1586b5163992SAzael Avalos 		if (hci_result == TOS_SUCCESS)
1587135740deSSeth Forshee 			dev->force_fan = value;
1588b5163992SAzael Avalos 		else
1589b5163992SAzael Avalos 			return -EIO;
1590b4f9fe12SLen Brown 	} else {
1591b4f9fe12SLen Brown 		return -EINVAL;
1592b4f9fe12SLen Brown 	}
1593b4f9fe12SLen Brown 
1594b4f9fe12SLen Brown 	return count;
1595b4f9fe12SLen Brown }
1596b4f9fe12SLen Brown 
1597936c8bcdSAlexey Dobriyan static const struct file_operations fan_proc_fops = {
1598936c8bcdSAlexey Dobriyan 	.owner		= THIS_MODULE,
1599936c8bcdSAlexey Dobriyan 	.open		= fan_proc_open,
1600936c8bcdSAlexey Dobriyan 	.read		= seq_read,
1601936c8bcdSAlexey Dobriyan 	.llseek		= seq_lseek,
1602936c8bcdSAlexey Dobriyan 	.release	= single_release,
1603936c8bcdSAlexey Dobriyan 	.write		= fan_proc_write,
1604936c8bcdSAlexey Dobriyan };
1605936c8bcdSAlexey Dobriyan 
1606936c8bcdSAlexey Dobriyan static int keys_proc_show(struct seq_file *m, void *v)
1607b4f9fe12SLen Brown {
1608135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = m->private;
1609b4f9fe12SLen Brown 	u32 hci_result;
1610b4f9fe12SLen Brown 	u32 value;
1611b4f9fe12SLen Brown 
161211948b93SSeth Forshee 	if (!dev->key_event_valid && dev->system_event_supported) {
1613893f3f62SAzael Avalos 		hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
16141864bbc2SAzael Avalos 		if (hci_result == TOS_SUCCESS) {
1615135740deSSeth Forshee 			dev->key_event_valid = 1;
1616135740deSSeth Forshee 			dev->last_key_event = value;
16171864bbc2SAzael Avalos 		} else if (hci_result == TOS_FIFO_EMPTY) {
1618e0769fe6SDarren Hart 			/* Better luck next time */
16191864bbc2SAzael Avalos 		} else if (hci_result == TOS_NOT_SUPPORTED) {
1620e0769fe6SDarren Hart 			/*
1621e0769fe6SDarren Hart 			 * This is a workaround for an unresolved issue on
1622b4f9fe12SLen Brown 			 * some machines where system events sporadically
1623e0769fe6SDarren Hart 			 * become disabled.
1624e0769fe6SDarren Hart 			 */
1625893f3f62SAzael Avalos 			hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
16267e33460dSJoe Perches 			pr_notice("Re-enabled hotkeys\n");
1627b4f9fe12SLen Brown 		} else {
16287e33460dSJoe Perches 			pr_err("Error reading hotkey status\n");
162932bcd5cbSSeth Forshee 			return -EIO;
1630b4f9fe12SLen Brown 		}
1631b4f9fe12SLen Brown 	}
1632b4f9fe12SLen Brown 
1633135740deSSeth Forshee 	seq_printf(m, "hotkey_ready:            %d\n", dev->key_event_valid);
1634135740deSSeth Forshee 	seq_printf(m, "hotkey:                  0x%04x\n", dev->last_key_event);
1635936c8bcdSAlexey Dobriyan 	return 0;
1636b4f9fe12SLen Brown }
1637b4f9fe12SLen Brown 
1638936c8bcdSAlexey Dobriyan static int keys_proc_open(struct inode *inode, struct file *file)
1639b4f9fe12SLen Brown {
1640d9dda78bSAl Viro 	return single_open(file, keys_proc_show, PDE_DATA(inode));
1641936c8bcdSAlexey Dobriyan }
1642936c8bcdSAlexey Dobriyan 
1643936c8bcdSAlexey Dobriyan static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1644936c8bcdSAlexey Dobriyan 			       size_t count, loff_t *pos)
1645936c8bcdSAlexey Dobriyan {
1646d9dda78bSAl Viro 	struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1647936c8bcdSAlexey Dobriyan 	char cmd[42];
1648936c8bcdSAlexey Dobriyan 	size_t len;
1649b4f9fe12SLen Brown 	int value;
1650b4f9fe12SLen Brown 
1651936c8bcdSAlexey Dobriyan 	len = min(count, sizeof(cmd) - 1);
1652936c8bcdSAlexey Dobriyan 	if (copy_from_user(cmd, buf, len))
1653936c8bcdSAlexey Dobriyan 		return -EFAULT;
1654936c8bcdSAlexey Dobriyan 	cmd[len] = '\0';
1655936c8bcdSAlexey Dobriyan 
1656b5163992SAzael Avalos 	if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
1657135740deSSeth Forshee 		dev->key_event_valid = 0;
1658b5163992SAzael Avalos 	else
1659b4f9fe12SLen Brown 		return -EINVAL;
1660b4f9fe12SLen Brown 
1661b4f9fe12SLen Brown 	return count;
1662b4f9fe12SLen Brown }
1663b4f9fe12SLen Brown 
1664936c8bcdSAlexey Dobriyan static const struct file_operations keys_proc_fops = {
1665936c8bcdSAlexey Dobriyan 	.owner		= THIS_MODULE,
1666936c8bcdSAlexey Dobriyan 	.open		= keys_proc_open,
1667936c8bcdSAlexey Dobriyan 	.read		= seq_read,
1668936c8bcdSAlexey Dobriyan 	.llseek		= seq_lseek,
1669936c8bcdSAlexey Dobriyan 	.release	= single_release,
1670936c8bcdSAlexey Dobriyan 	.write		= keys_proc_write,
1671936c8bcdSAlexey Dobriyan };
1672936c8bcdSAlexey Dobriyan 
1673936c8bcdSAlexey Dobriyan static int version_proc_show(struct seq_file *m, void *v)
1674b4f9fe12SLen Brown {
1675936c8bcdSAlexey Dobriyan 	seq_printf(m, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
1676936c8bcdSAlexey Dobriyan 	seq_printf(m, "proc_interface:          %d\n", PROC_INTERFACE_VERSION);
1677936c8bcdSAlexey Dobriyan 	return 0;
1678b4f9fe12SLen Brown }
1679b4f9fe12SLen Brown 
1680936c8bcdSAlexey Dobriyan static int version_proc_open(struct inode *inode, struct file *file)
1681936c8bcdSAlexey Dobriyan {
1682d9dda78bSAl Viro 	return single_open(file, version_proc_show, PDE_DATA(inode));
1683936c8bcdSAlexey Dobriyan }
1684936c8bcdSAlexey Dobriyan 
1685936c8bcdSAlexey Dobriyan static const struct file_operations version_proc_fops = {
1686936c8bcdSAlexey Dobriyan 	.owner		= THIS_MODULE,
1687936c8bcdSAlexey Dobriyan 	.open		= version_proc_open,
1688936c8bcdSAlexey Dobriyan 	.read		= seq_read,
1689936c8bcdSAlexey Dobriyan 	.llseek		= seq_lseek,
1690936c8bcdSAlexey Dobriyan 	.release	= single_release,
1691936c8bcdSAlexey Dobriyan };
1692936c8bcdSAlexey Dobriyan 
1693e0769fe6SDarren Hart /*
1694e0769fe6SDarren Hart  * Proc and module init
1695b4f9fe12SLen Brown  */
1696b4f9fe12SLen Brown 
1697b4f9fe12SLen Brown #define PROC_TOSHIBA		"toshiba"
1698b4f9fe12SLen Brown 
1699b859f159SGreg Kroah-Hartman static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1700b4f9fe12SLen Brown {
170136d03f93SSeth Forshee 	if (dev->backlight_dev)
1702135740deSSeth Forshee 		proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1703135740deSSeth Forshee 				 &lcd_proc_fops, dev);
170436d03f93SSeth Forshee 	if (dev->video_supported)
1705135740deSSeth Forshee 		proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1706135740deSSeth Forshee 				 &video_proc_fops, dev);
170736d03f93SSeth Forshee 	if (dev->fan_supported)
1708135740deSSeth Forshee 		proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1709135740deSSeth Forshee 				 &fan_proc_fops, dev);
171036d03f93SSeth Forshee 	if (dev->hotkey_dev)
1711135740deSSeth Forshee 		proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1712135740deSSeth Forshee 				 &keys_proc_fops, dev);
1713135740deSSeth Forshee 	proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1714135740deSSeth Forshee 			 &version_proc_fops, dev);
1715b4f9fe12SLen Brown }
1716b4f9fe12SLen Brown 
171736d03f93SSeth Forshee static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1718b4f9fe12SLen Brown {
171936d03f93SSeth Forshee 	if (dev->backlight_dev)
1720936c8bcdSAlexey Dobriyan 		remove_proc_entry("lcd", toshiba_proc_dir);
172136d03f93SSeth Forshee 	if (dev->video_supported)
1722936c8bcdSAlexey Dobriyan 		remove_proc_entry("video", toshiba_proc_dir);
172336d03f93SSeth Forshee 	if (dev->fan_supported)
1724936c8bcdSAlexey Dobriyan 		remove_proc_entry("fan", toshiba_proc_dir);
172536d03f93SSeth Forshee 	if (dev->hotkey_dev)
1726936c8bcdSAlexey Dobriyan 		remove_proc_entry("keys", toshiba_proc_dir);
1727936c8bcdSAlexey Dobriyan 	remove_proc_entry("version", toshiba_proc_dir);
1728b4f9fe12SLen Brown }
1729b4f9fe12SLen Brown 
1730acc2472eSLionel Debroux static const struct backlight_ops toshiba_backlight_data = {
1731121b7b0dSAkio Idehara 	.options = BL_CORE_SUSPENDRESUME,
173262cce752SSeth Forshee 	.get_brightness = get_lcd_brightness,
1733b4f9fe12SLen Brown 	.update_status  = set_lcd_status,
1734b4f9fe12SLen Brown };
1735b4f9fe12SLen Brown 
1736360f0f39SAzael Avalos /*
1737360f0f39SAzael Avalos  * Sysfs files
1738360f0f39SAzael Avalos  */
17399d309848SAzael Avalos static ssize_t version_show(struct device *dev,
1740c6c68ff8SAzael Avalos 			    struct device_attribute *attr, char *buf)
1741c6c68ff8SAzael Avalos {
1742c6c68ff8SAzael Avalos 	return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1743c6c68ff8SAzael Avalos }
17440c3c0f10SAzael Avalos static DEVICE_ATTR_RO(version);
1745c6c68ff8SAzael Avalos 
17469d309848SAzael Avalos static ssize_t fan_store(struct device *dev,
174794477d4cSAzael Avalos 			 struct device_attribute *attr,
174894477d4cSAzael Avalos 			 const char *buf, size_t count)
174994477d4cSAzael Avalos {
175094477d4cSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
175194477d4cSAzael Avalos 	u32 result;
175294477d4cSAzael Avalos 	int state;
175394477d4cSAzael Avalos 	int ret;
175494477d4cSAzael Avalos 
175594477d4cSAzael Avalos 	ret = kstrtoint(buf, 0, &state);
175694477d4cSAzael Avalos 	if (ret)
175794477d4cSAzael Avalos 		return ret;
175894477d4cSAzael Avalos 
175994477d4cSAzael Avalos 	if (state != 0 && state != 1)
176094477d4cSAzael Avalos 		return -EINVAL;
176194477d4cSAzael Avalos 
176294477d4cSAzael Avalos 	result = hci_write1(toshiba, HCI_FAN, state);
176394477d4cSAzael Avalos 	if (result == TOS_FAILURE)
176494477d4cSAzael Avalos 		return -EIO;
176594477d4cSAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
176694477d4cSAzael Avalos 		return -ENODEV;
176794477d4cSAzael Avalos 
176894477d4cSAzael Avalos 	return count;
176994477d4cSAzael Avalos }
177094477d4cSAzael Avalos 
17719d309848SAzael Avalos static ssize_t fan_show(struct device *dev,
177294477d4cSAzael Avalos 			struct device_attribute *attr, char *buf)
177394477d4cSAzael Avalos {
177494477d4cSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
177594477d4cSAzael Avalos 	u32 value;
177694477d4cSAzael Avalos 	int ret;
177794477d4cSAzael Avalos 
177894477d4cSAzael Avalos 	ret = get_fan_status(toshiba, &value);
177994477d4cSAzael Avalos 	if (ret)
178094477d4cSAzael Avalos 		return ret;
178194477d4cSAzael Avalos 
178294477d4cSAzael Avalos 	return sprintf(buf, "%d\n", value);
178394477d4cSAzael Avalos }
17840c3c0f10SAzael Avalos static DEVICE_ATTR_RW(fan);
178594477d4cSAzael Avalos 
17869d309848SAzael Avalos static ssize_t kbd_backlight_mode_store(struct device *dev,
1787360f0f39SAzael Avalos 					struct device_attribute *attr,
1788360f0f39SAzael Avalos 					const char *buf, size_t count)
1789360f0f39SAzael Avalos {
1790360f0f39SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1791aeaac098SDan Carpenter 	int mode;
1792aeaac098SDan Carpenter 	int time;
1793aeaac098SDan Carpenter 	int ret;
1794360f0f39SAzael Avalos 
1795aeaac098SDan Carpenter 
1796aeaac098SDan Carpenter 	ret = kstrtoint(buf, 0, &mode);
1797aeaac098SDan Carpenter 	if (ret)
1798aeaac098SDan Carpenter 		return ret;
179993f8c16dSAzael Avalos 
180093f8c16dSAzael Avalos 	/* Check for supported modes depending on keyboard backlight type */
180193f8c16dSAzael Avalos 	if (toshiba->kbd_type == 1) {
180293f8c16dSAzael Avalos 		/* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1803aeaac098SDan Carpenter 		if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1804360f0f39SAzael Avalos 			return -EINVAL;
180593f8c16dSAzael Avalos 	} else if (toshiba->kbd_type == 2) {
180693f8c16dSAzael Avalos 		/* Type 2 doesn't support SCI_KBD_MODE_FNZ */
180793f8c16dSAzael Avalos 		if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
180893f8c16dSAzael Avalos 		    mode != SCI_KBD_MODE_OFF)
180993f8c16dSAzael Avalos 			return -EINVAL;
181093f8c16dSAzael Avalos 	}
1811360f0f39SAzael Avalos 
1812e0769fe6SDarren Hart 	/*
1813e0769fe6SDarren Hart 	 * Set the Keyboard Backlight Mode where:
1814360f0f39SAzael Avalos 	 *	Auto - KBD backlight turns off automatically in given time
1815360f0f39SAzael Avalos 	 *	FN-Z - KBD backlight "toggles" when hotkey pressed
181693f8c16dSAzael Avalos 	 *	ON   - KBD backlight is always on
181793f8c16dSAzael Avalos 	 *	OFF  - KBD backlight is always off
1818360f0f39SAzael Avalos 	 */
181993f8c16dSAzael Avalos 
182093f8c16dSAzael Avalos 	/* Only make a change if the actual mode has changed */
1821aeaac098SDan Carpenter 	if (toshiba->kbd_mode != mode) {
182293f8c16dSAzael Avalos 		/* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1823360f0f39SAzael Avalos 		time = toshiba->kbd_time << HCI_MISC_SHIFT;
182493f8c16dSAzael Avalos 
182593f8c16dSAzael Avalos 		/* OR the "base time" to the actual method format */
182693f8c16dSAzael Avalos 		if (toshiba->kbd_type == 1) {
182793f8c16dSAzael Avalos 			/* Type 1 requires the current mode */
182893f8c16dSAzael Avalos 			time |= toshiba->kbd_mode;
182993f8c16dSAzael Avalos 		} else if (toshiba->kbd_type == 2) {
183093f8c16dSAzael Avalos 			/* Type 2 requires the desired mode */
183193f8c16dSAzael Avalos 			time |= mode;
183293f8c16dSAzael Avalos 		}
183393f8c16dSAzael Avalos 
1834aeaac098SDan Carpenter 		ret = toshiba_kbd_illum_status_set(toshiba, time);
1835aeaac098SDan Carpenter 		if (ret)
1836aeaac098SDan Carpenter 			return ret;
183793f8c16dSAzael Avalos 
1838360f0f39SAzael Avalos 		toshiba->kbd_mode = mode;
1839360f0f39SAzael Avalos 	}
1840360f0f39SAzael Avalos 
1841360f0f39SAzael Avalos 	return count;
1842360f0f39SAzael Avalos }
1843360f0f39SAzael Avalos 
18449d309848SAzael Avalos static ssize_t kbd_backlight_mode_show(struct device *dev,
1845360f0f39SAzael Avalos 				       struct device_attribute *attr,
1846360f0f39SAzael Avalos 				       char *buf)
1847360f0f39SAzael Avalos {
1848360f0f39SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1849360f0f39SAzael Avalos 	u32 time;
1850360f0f39SAzael Avalos 
1851360f0f39SAzael Avalos 	if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1852360f0f39SAzael Avalos 		return -EIO;
1853360f0f39SAzael Avalos 
185493f8c16dSAzael Avalos 	return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
185593f8c16dSAzael Avalos }
18560c3c0f10SAzael Avalos static DEVICE_ATTR_RW(kbd_backlight_mode);
185793f8c16dSAzael Avalos 
18589d309848SAzael Avalos static ssize_t kbd_type_show(struct device *dev,
18599d309848SAzael Avalos 			     struct device_attribute *attr, char *buf)
186093f8c16dSAzael Avalos {
186193f8c16dSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
186293f8c16dSAzael Avalos 
186393f8c16dSAzael Avalos 	return sprintf(buf, "%d\n", toshiba->kbd_type);
186493f8c16dSAzael Avalos }
18650c3c0f10SAzael Avalos static DEVICE_ATTR_RO(kbd_type);
186693f8c16dSAzael Avalos 
18679d309848SAzael Avalos static ssize_t available_kbd_modes_show(struct device *dev,
186893f8c16dSAzael Avalos 					struct device_attribute *attr,
186993f8c16dSAzael Avalos 					char *buf)
187093f8c16dSAzael Avalos {
187193f8c16dSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
187293f8c16dSAzael Avalos 
187393f8c16dSAzael Avalos 	if (toshiba->kbd_type == 1)
187493f8c16dSAzael Avalos 		return sprintf(buf, "%x %x\n",
187593f8c16dSAzael Avalos 			       SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
187693f8c16dSAzael Avalos 
187793f8c16dSAzael Avalos 	return sprintf(buf, "%x %x %x\n",
187893f8c16dSAzael Avalos 		       SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
1879360f0f39SAzael Avalos }
18800c3c0f10SAzael Avalos static DEVICE_ATTR_RO(available_kbd_modes);
1881360f0f39SAzael Avalos 
18829d309848SAzael Avalos static ssize_t kbd_backlight_timeout_store(struct device *dev,
1883360f0f39SAzael Avalos 					   struct device_attribute *attr,
1884360f0f39SAzael Avalos 					   const char *buf, size_t count)
1885360f0f39SAzael Avalos {
1886360f0f39SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1887eabde0faSAzael Avalos 	int time;
1888eabde0faSAzael Avalos 	int ret;
1889360f0f39SAzael Avalos 
1890eabde0faSAzael Avalos 	ret = kstrtoint(buf, 0, &time);
1891eabde0faSAzael Avalos 	if (ret)
1892eabde0faSAzael Avalos 		return ret;
1893eabde0faSAzael Avalos 
1894eabde0faSAzael Avalos 	/* Check for supported values depending on kbd_type */
1895eabde0faSAzael Avalos 	if (toshiba->kbd_type == 1) {
1896eabde0faSAzael Avalos 		if (time < 0 || time > 60)
1897360f0f39SAzael Avalos 			return -EINVAL;
1898eabde0faSAzael Avalos 	} else if (toshiba->kbd_type == 2) {
1899eabde0faSAzael Avalos 		if (time < 1 || time > 60)
1900eabde0faSAzael Avalos 			return -EINVAL;
1901eabde0faSAzael Avalos 	}
1902360f0f39SAzael Avalos 
1903eabde0faSAzael Avalos 	/* Set the Keyboard Backlight Timeout */
1904eabde0faSAzael Avalos 
1905eabde0faSAzael Avalos 	/* Only make a change if the actual timeout has changed */
1906eabde0faSAzael Avalos 	if (toshiba->kbd_time != time) {
1907eabde0faSAzael Avalos 		/* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1908360f0f39SAzael Avalos 		time = time << HCI_MISC_SHIFT;
1909eabde0faSAzael Avalos 		/* OR the "base time" to the actual method format */
1910eabde0faSAzael Avalos 		if (toshiba->kbd_type == 1)
1911eabde0faSAzael Avalos 			time |= SCI_KBD_MODE_FNZ;
1912eabde0faSAzael Avalos 		else if (toshiba->kbd_type == 2)
1913eabde0faSAzael Avalos 			time |= SCI_KBD_MODE_AUTO;
1914eabde0faSAzael Avalos 
1915eabde0faSAzael Avalos 		ret = toshiba_kbd_illum_status_set(toshiba, time);
1916eabde0faSAzael Avalos 		if (ret)
1917eabde0faSAzael Avalos 			return ret;
1918eabde0faSAzael Avalos 
1919360f0f39SAzael Avalos 		toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1920360f0f39SAzael Avalos 	}
1921360f0f39SAzael Avalos 
1922360f0f39SAzael Avalos 	return count;
1923360f0f39SAzael Avalos }
1924360f0f39SAzael Avalos 
19259d309848SAzael Avalos static ssize_t kbd_backlight_timeout_show(struct device *dev,
1926360f0f39SAzael Avalos 					  struct device_attribute *attr,
1927360f0f39SAzael Avalos 					  char *buf)
1928360f0f39SAzael Avalos {
1929360f0f39SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1930360f0f39SAzael Avalos 	u32 time;
1931360f0f39SAzael Avalos 
1932360f0f39SAzael Avalos 	if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1933360f0f39SAzael Avalos 		return -EIO;
1934360f0f39SAzael Avalos 
1935360f0f39SAzael Avalos 	return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1936360f0f39SAzael Avalos }
19370c3c0f10SAzael Avalos static DEVICE_ATTR_RW(kbd_backlight_timeout);
1938360f0f39SAzael Avalos 
19399d309848SAzael Avalos static ssize_t touchpad_store(struct device *dev,
19409d8658acSAzael Avalos 			      struct device_attribute *attr,
19419d8658acSAzael Avalos 			      const char *buf, size_t count)
19429d8658acSAzael Avalos {
19439d8658acSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
19449d8658acSAzael Avalos 	int state;
1945c8a41669SAzael Avalos 	int ret;
19469d8658acSAzael Avalos 
19479d8658acSAzael Avalos 	/* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1948c8a41669SAzael Avalos 	ret = kstrtoint(buf, 0, &state);
1949c8a41669SAzael Avalos 	if (ret)
1950c8a41669SAzael Avalos 		return ret;
1951c8a41669SAzael Avalos 	if (state != 0 && state != 1)
1952c8a41669SAzael Avalos 		return -EINVAL;
1953c8a41669SAzael Avalos 
1954c8a41669SAzael Avalos 	ret = toshiba_touchpad_set(toshiba, state);
1955c8a41669SAzael Avalos 	if (ret)
1956c8a41669SAzael Avalos 		return ret;
19579d8658acSAzael Avalos 
19589d8658acSAzael Avalos 	return count;
19599d8658acSAzael Avalos }
19609d8658acSAzael Avalos 
19619d309848SAzael Avalos static ssize_t touchpad_show(struct device *dev,
19629d8658acSAzael Avalos 			     struct device_attribute *attr, char *buf)
19639d8658acSAzael Avalos {
19649d8658acSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
19659d8658acSAzael Avalos 	u32 state;
19669d8658acSAzael Avalos 	int ret;
19679d8658acSAzael Avalos 
19689d8658acSAzael Avalos 	ret = toshiba_touchpad_get(toshiba, &state);
19699d8658acSAzael Avalos 	if (ret < 0)
19709d8658acSAzael Avalos 		return ret;
19719d8658acSAzael Avalos 
19729d8658acSAzael Avalos 	return sprintf(buf, "%i\n", state);
19739d8658acSAzael Avalos }
19740c3c0f10SAzael Avalos static DEVICE_ATTR_RW(touchpad);
19759d8658acSAzael Avalos 
19769d309848SAzael Avalos static ssize_t position_show(struct device *dev,
19775a2813e9SAzael Avalos 			     struct device_attribute *attr, char *buf)
19785a2813e9SAzael Avalos {
19795a2813e9SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
19805a2813e9SAzael Avalos 	u32 xyval, zval, tmp;
19815a2813e9SAzael Avalos 	u16 x, y, z;
19825a2813e9SAzael Avalos 	int ret;
19835a2813e9SAzael Avalos 
19845a2813e9SAzael Avalos 	xyval = zval = 0;
19855a2813e9SAzael Avalos 	ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
19865a2813e9SAzael Avalos 	if (ret < 0)
19875a2813e9SAzael Avalos 		return ret;
19885a2813e9SAzael Avalos 
19895a2813e9SAzael Avalos 	x = xyval & HCI_ACCEL_MASK;
19905a2813e9SAzael Avalos 	tmp = xyval >> HCI_MISC_SHIFT;
19915a2813e9SAzael Avalos 	y = tmp & HCI_ACCEL_MASK;
19925a2813e9SAzael Avalos 	z = zval & HCI_ACCEL_MASK;
19935a2813e9SAzael Avalos 
19945a2813e9SAzael Avalos 	return sprintf(buf, "%d %d %d\n", x, y, z);
19955a2813e9SAzael Avalos }
19960c3c0f10SAzael Avalos static DEVICE_ATTR_RO(position);
19975a2813e9SAzael Avalos 
19989d309848SAzael Avalos static ssize_t usb_sleep_charge_show(struct device *dev,
19999d309848SAzael Avalos 				     struct device_attribute *attr, char *buf)
2000e26ffe51SAzael Avalos {
2001e26ffe51SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2002e26ffe51SAzael Avalos 	u32 mode;
2003e26ffe51SAzael Avalos 	int ret;
2004e26ffe51SAzael Avalos 
2005e26ffe51SAzael Avalos 	ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
2006e26ffe51SAzael Avalos 	if (ret < 0)
2007e26ffe51SAzael Avalos 		return ret;
2008e26ffe51SAzael Avalos 
2009e26ffe51SAzael Avalos 	return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
2010e26ffe51SAzael Avalos }
2011e26ffe51SAzael Avalos 
20129d309848SAzael Avalos static ssize_t usb_sleep_charge_store(struct device *dev,
2013e26ffe51SAzael Avalos 				      struct device_attribute *attr,
2014e26ffe51SAzael Avalos 				      const char *buf, size_t count)
2015e26ffe51SAzael Avalos {
2016e26ffe51SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2017e26ffe51SAzael Avalos 	u32 mode;
2018e26ffe51SAzael Avalos 	int state;
2019e26ffe51SAzael Avalos 	int ret;
2020e26ffe51SAzael Avalos 
2021e26ffe51SAzael Avalos 	ret = kstrtoint(buf, 0, &state);
2022e26ffe51SAzael Avalos 	if (ret)
2023e26ffe51SAzael Avalos 		return ret;
2024e0769fe6SDarren Hart 	/*
2025e0769fe6SDarren Hart 	 * Check for supported values, where:
2026e26ffe51SAzael Avalos 	 * 0 - Disabled
2027e26ffe51SAzael Avalos 	 * 1 - Alternate (Non USB conformant devices that require more power)
2028e26ffe51SAzael Avalos 	 * 2 - Auto (USB conformant devices)
2029c8c91842SAzael Avalos 	 * 3 - Typical
2030e26ffe51SAzael Avalos 	 */
2031c8c91842SAzael Avalos 	if (state != 0 && state != 1 && state != 2 && state != 3)
2032e26ffe51SAzael Avalos 		return -EINVAL;
2033e26ffe51SAzael Avalos 
2034e26ffe51SAzael Avalos 	/* Set the USB charging mode to internal value */
2035c8c91842SAzael Avalos 	mode = toshiba->usbsc_mode_base;
2036e26ffe51SAzael Avalos 	if (state == 0)
2037c8c91842SAzael Avalos 		mode |= SCI_USB_CHARGE_DISABLED;
2038e26ffe51SAzael Avalos 	else if (state == 1)
2039c8c91842SAzael Avalos 		mode |= SCI_USB_CHARGE_ALTERNATE;
2040e26ffe51SAzael Avalos 	else if (state == 2)
2041c8c91842SAzael Avalos 		mode |= SCI_USB_CHARGE_AUTO;
2042c8c91842SAzael Avalos 	else if (state == 3)
2043c8c91842SAzael Avalos 		mode |= SCI_USB_CHARGE_TYPICAL;
2044e26ffe51SAzael Avalos 
2045e26ffe51SAzael Avalos 	ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2046e26ffe51SAzael Avalos 	if (ret)
2047e26ffe51SAzael Avalos 		return ret;
2048e26ffe51SAzael Avalos 
2049e26ffe51SAzael Avalos 	return count;
2050e26ffe51SAzael Avalos }
20510c3c0f10SAzael Avalos static DEVICE_ATTR_RW(usb_sleep_charge);
2052e26ffe51SAzael Avalos 
2053182bcaa5SAzael Avalos static ssize_t sleep_functions_on_battery_show(struct device *dev,
2054182bcaa5SAzael Avalos 					       struct device_attribute *attr,
2055182bcaa5SAzael Avalos 					       char *buf)
2056182bcaa5SAzael Avalos {
2057182bcaa5SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2058182bcaa5SAzael Avalos 	u32 state;
2059182bcaa5SAzael Avalos 	int bat_lvl;
2060182bcaa5SAzael Avalos 	int status;
2061182bcaa5SAzael Avalos 	int ret;
2062182bcaa5SAzael Avalos 	int tmp;
2063182bcaa5SAzael Avalos 
2064182bcaa5SAzael Avalos 	ret = toshiba_sleep_functions_status_get(toshiba, &state);
2065182bcaa5SAzael Avalos 	if (ret < 0)
2066182bcaa5SAzael Avalos 		return ret;
2067182bcaa5SAzael Avalos 
2068182bcaa5SAzael Avalos 	/* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2069182bcaa5SAzael Avalos 	tmp = state & SCI_USB_CHARGE_BAT_MASK;
2070182bcaa5SAzael Avalos 	status = (tmp == 0x4) ? 1 : 0;
2071182bcaa5SAzael Avalos 	/* Determine the battery level set */
2072182bcaa5SAzael Avalos 	bat_lvl = state >> HCI_MISC_SHIFT;
2073182bcaa5SAzael Avalos 
2074182bcaa5SAzael Avalos 	return sprintf(buf, "%d %d\n", status, bat_lvl);
2075182bcaa5SAzael Avalos }
2076182bcaa5SAzael Avalos 
2077182bcaa5SAzael Avalos static ssize_t sleep_functions_on_battery_store(struct device *dev,
2078182bcaa5SAzael Avalos 						struct device_attribute *attr,
2079182bcaa5SAzael Avalos 						const char *buf, size_t count)
2080182bcaa5SAzael Avalos {
2081182bcaa5SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2082182bcaa5SAzael Avalos 	u32 status;
2083182bcaa5SAzael Avalos 	int value;
2084182bcaa5SAzael Avalos 	int ret;
2085182bcaa5SAzael Avalos 	int tmp;
2086182bcaa5SAzael Avalos 
2087182bcaa5SAzael Avalos 	ret = kstrtoint(buf, 0, &value);
2088182bcaa5SAzael Avalos 	if (ret)
2089182bcaa5SAzael Avalos 		return ret;
2090182bcaa5SAzael Avalos 
2091e0769fe6SDarren Hart 	/*
2092e0769fe6SDarren Hart 	 * Set the status of the function:
2093182bcaa5SAzael Avalos 	 * 0 - Disabled
2094182bcaa5SAzael Avalos 	 * 1-100 - Enabled
2095182bcaa5SAzael Avalos 	 */
2096182bcaa5SAzael Avalos 	if (value < 0 || value > 100)
2097182bcaa5SAzael Avalos 		return -EINVAL;
2098182bcaa5SAzael Avalos 
2099182bcaa5SAzael Avalos 	if (value == 0) {
2100182bcaa5SAzael Avalos 		tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2101182bcaa5SAzael Avalos 		status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2102182bcaa5SAzael Avalos 	} else {
2103182bcaa5SAzael Avalos 		tmp = value << HCI_MISC_SHIFT;
2104182bcaa5SAzael Avalos 		status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2105182bcaa5SAzael Avalos 	}
2106182bcaa5SAzael Avalos 	ret = toshiba_sleep_functions_status_set(toshiba, status);
2107182bcaa5SAzael Avalos 	if (ret < 0)
2108182bcaa5SAzael Avalos 		return ret;
2109182bcaa5SAzael Avalos 
2110182bcaa5SAzael Avalos 	toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2111182bcaa5SAzael Avalos 
2112182bcaa5SAzael Avalos 	return count;
2113182bcaa5SAzael Avalos }
21140c3c0f10SAzael Avalos static DEVICE_ATTR_RW(sleep_functions_on_battery);
2115182bcaa5SAzael Avalos 
21169d309848SAzael Avalos static ssize_t usb_rapid_charge_show(struct device *dev,
21179d309848SAzael Avalos 				     struct device_attribute *attr, char *buf)
2118bb3fe01fSAzael Avalos {
2119bb3fe01fSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2120bb3fe01fSAzael Avalos 	u32 state;
2121bb3fe01fSAzael Avalos 	int ret;
2122bb3fe01fSAzael Avalos 
2123bb3fe01fSAzael Avalos 	ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2124bb3fe01fSAzael Avalos 	if (ret < 0)
2125bb3fe01fSAzael Avalos 		return ret;
2126bb3fe01fSAzael Avalos 
2127bb3fe01fSAzael Avalos 	return sprintf(buf, "%d\n", state);
2128bb3fe01fSAzael Avalos }
2129bb3fe01fSAzael Avalos 
21309d309848SAzael Avalos static ssize_t usb_rapid_charge_store(struct device *dev,
2131bb3fe01fSAzael Avalos 				      struct device_attribute *attr,
2132bb3fe01fSAzael Avalos 				      const char *buf, size_t count)
2133bb3fe01fSAzael Avalos {
2134bb3fe01fSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2135bb3fe01fSAzael Avalos 	int state;
2136bb3fe01fSAzael Avalos 	int ret;
2137bb3fe01fSAzael Avalos 
2138bb3fe01fSAzael Avalos 	ret = kstrtoint(buf, 0, &state);
2139bb3fe01fSAzael Avalos 	if (ret)
2140bb3fe01fSAzael Avalos 		return ret;
2141bb3fe01fSAzael Avalos 	if (state != 0 && state != 1)
2142bb3fe01fSAzael Avalos 		return -EINVAL;
2143bb3fe01fSAzael Avalos 
2144bb3fe01fSAzael Avalos 	ret = toshiba_usb_rapid_charge_set(toshiba, state);
2145bb3fe01fSAzael Avalos 	if (ret)
2146bb3fe01fSAzael Avalos 		return ret;
2147bb3fe01fSAzael Avalos 
2148bb3fe01fSAzael Avalos 	return count;
2149bb3fe01fSAzael Avalos }
21500c3c0f10SAzael Avalos static DEVICE_ATTR_RW(usb_rapid_charge);
2151bb3fe01fSAzael Avalos 
21529d309848SAzael Avalos static ssize_t usb_sleep_music_show(struct device *dev,
21539d309848SAzael Avalos 				    struct device_attribute *attr, char *buf)
2154172ce0a9SAzael Avalos {
2155172ce0a9SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2156172ce0a9SAzael Avalos 	u32 state;
2157172ce0a9SAzael Avalos 	int ret;
2158172ce0a9SAzael Avalos 
2159172ce0a9SAzael Avalos 	ret = toshiba_usb_sleep_music_get(toshiba, &state);
2160172ce0a9SAzael Avalos 	if (ret < 0)
2161172ce0a9SAzael Avalos 		return ret;
2162172ce0a9SAzael Avalos 
2163172ce0a9SAzael Avalos 	return sprintf(buf, "%d\n", state);
2164172ce0a9SAzael Avalos }
2165172ce0a9SAzael Avalos 
21669d309848SAzael Avalos static ssize_t usb_sleep_music_store(struct device *dev,
2167172ce0a9SAzael Avalos 				     struct device_attribute *attr,
2168172ce0a9SAzael Avalos 				     const char *buf, size_t count)
2169172ce0a9SAzael Avalos {
2170172ce0a9SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2171172ce0a9SAzael Avalos 	int state;
2172172ce0a9SAzael Avalos 	int ret;
2173172ce0a9SAzael Avalos 
2174172ce0a9SAzael Avalos 	ret = kstrtoint(buf, 0, &state);
2175172ce0a9SAzael Avalos 	if (ret)
2176172ce0a9SAzael Avalos 		return ret;
2177172ce0a9SAzael Avalos 	if (state != 0 && state != 1)
2178172ce0a9SAzael Avalos 		return -EINVAL;
2179172ce0a9SAzael Avalos 
2180172ce0a9SAzael Avalos 	ret = toshiba_usb_sleep_music_set(toshiba, state);
2181172ce0a9SAzael Avalos 	if (ret)
2182172ce0a9SAzael Avalos 		return ret;
2183172ce0a9SAzael Avalos 
2184172ce0a9SAzael Avalos 	return count;
2185172ce0a9SAzael Avalos }
21860c3c0f10SAzael Avalos static DEVICE_ATTR_RW(usb_sleep_music);
2187172ce0a9SAzael Avalos 
21889d309848SAzael Avalos static ssize_t kbd_function_keys_show(struct device *dev,
21899d309848SAzael Avalos 				      struct device_attribute *attr, char *buf)
2190bae84195SAzael Avalos {
2191bae84195SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2192bae84195SAzael Avalos 	int mode;
2193bae84195SAzael Avalos 	int ret;
2194bae84195SAzael Avalos 
2195bae84195SAzael Avalos 	ret = toshiba_function_keys_get(toshiba, &mode);
2196bae84195SAzael Avalos 	if (ret < 0)
2197bae84195SAzael Avalos 		return ret;
2198bae84195SAzael Avalos 
2199bae84195SAzael Avalos 	return sprintf(buf, "%d\n", mode);
2200bae84195SAzael Avalos }
2201bae84195SAzael Avalos 
22029d309848SAzael Avalos static ssize_t kbd_function_keys_store(struct device *dev,
2203bae84195SAzael Avalos 				       struct device_attribute *attr,
2204bae84195SAzael Avalos 				       const char *buf, size_t count)
2205bae84195SAzael Avalos {
2206bae84195SAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2207bae84195SAzael Avalos 	int mode;
2208bae84195SAzael Avalos 	int ret;
2209bae84195SAzael Avalos 
2210bae84195SAzael Avalos 	ret = kstrtoint(buf, 0, &mode);
2211bae84195SAzael Avalos 	if (ret)
2212bae84195SAzael Avalos 		return ret;
2213e0769fe6SDarren Hart 	/*
2214e0769fe6SDarren Hart 	 * Check for the function keys mode where:
2215bae84195SAzael Avalos 	 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2216bae84195SAzael Avalos 	 * 1 - Special functions (Opposite of the above setting)
2217bae84195SAzael Avalos 	 */
2218bae84195SAzael Avalos 	if (mode != 0 && mode != 1)
2219bae84195SAzael Avalos 		return -EINVAL;
2220bae84195SAzael Avalos 
2221bae84195SAzael Avalos 	ret = toshiba_function_keys_set(toshiba, mode);
2222bae84195SAzael Avalos 	if (ret)
2223bae84195SAzael Avalos 		return ret;
2224bae84195SAzael Avalos 
2225bae84195SAzael Avalos 	pr_info("Reboot for changes to KBD Function Keys to take effect");
2226bae84195SAzael Avalos 
2227bae84195SAzael Avalos 	return count;
2228bae84195SAzael Avalos }
22290c3c0f10SAzael Avalos static DEVICE_ATTR_RW(kbd_function_keys);
2230bae84195SAzael Avalos 
22319d309848SAzael Avalos static ssize_t panel_power_on_show(struct device *dev,
22329d309848SAzael Avalos 				   struct device_attribute *attr, char *buf)
223335d53ceaSAzael Avalos {
223435d53ceaSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
223535d53ceaSAzael Avalos 	u32 state;
223635d53ceaSAzael Avalos 	int ret;
223735d53ceaSAzael Avalos 
223835d53ceaSAzael Avalos 	ret = toshiba_panel_power_on_get(toshiba, &state);
223935d53ceaSAzael Avalos 	if (ret < 0)
224035d53ceaSAzael Avalos 		return ret;
224135d53ceaSAzael Avalos 
224235d53ceaSAzael Avalos 	return sprintf(buf, "%d\n", state);
224335d53ceaSAzael Avalos }
224435d53ceaSAzael Avalos 
22459d309848SAzael Avalos static ssize_t panel_power_on_store(struct device *dev,
224635d53ceaSAzael Avalos 				    struct device_attribute *attr,
224735d53ceaSAzael Avalos 				    const char *buf, size_t count)
224835d53ceaSAzael Avalos {
224935d53ceaSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
225035d53ceaSAzael Avalos 	int state;
225135d53ceaSAzael Avalos 	int ret;
225235d53ceaSAzael Avalos 
225335d53ceaSAzael Avalos 	ret = kstrtoint(buf, 0, &state);
225435d53ceaSAzael Avalos 	if (ret)
225535d53ceaSAzael Avalos 		return ret;
225635d53ceaSAzael Avalos 	if (state != 0 && state != 1)
225735d53ceaSAzael Avalos 		return -EINVAL;
225835d53ceaSAzael Avalos 
225935d53ceaSAzael Avalos 	ret = toshiba_panel_power_on_set(toshiba, state);
226035d53ceaSAzael Avalos 	if (ret)
226135d53ceaSAzael Avalos 		return ret;
226235d53ceaSAzael Avalos 
226335d53ceaSAzael Avalos 	pr_info("Reboot for changes to Panel Power ON to take effect");
226435d53ceaSAzael Avalos 
226535d53ceaSAzael Avalos 	return count;
226635d53ceaSAzael Avalos }
22670c3c0f10SAzael Avalos static DEVICE_ATTR_RW(panel_power_on);
226835d53ceaSAzael Avalos 
22699d309848SAzael Avalos static ssize_t usb_three_show(struct device *dev,
22709d309848SAzael Avalos 			      struct device_attribute *attr, char *buf)
227117fe4b3dSAzael Avalos {
227217fe4b3dSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
227317fe4b3dSAzael Avalos 	u32 state;
227417fe4b3dSAzael Avalos 	int ret;
227517fe4b3dSAzael Avalos 
227617fe4b3dSAzael Avalos 	ret = toshiba_usb_three_get(toshiba, &state);
227717fe4b3dSAzael Avalos 	if (ret < 0)
227817fe4b3dSAzael Avalos 		return ret;
227917fe4b3dSAzael Avalos 
228017fe4b3dSAzael Avalos 	return sprintf(buf, "%d\n", state);
228117fe4b3dSAzael Avalos }
228217fe4b3dSAzael Avalos 
22839d309848SAzael Avalos static ssize_t usb_three_store(struct device *dev,
228417fe4b3dSAzael Avalos 			       struct device_attribute *attr,
228517fe4b3dSAzael Avalos 			       const char *buf, size_t count)
228617fe4b3dSAzael Avalos {
228717fe4b3dSAzael Avalos 	struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
228817fe4b3dSAzael Avalos 	int state;
228917fe4b3dSAzael Avalos 	int ret;
229017fe4b3dSAzael Avalos 
229117fe4b3dSAzael Avalos 	ret = kstrtoint(buf, 0, &state);
229217fe4b3dSAzael Avalos 	if (ret)
229317fe4b3dSAzael Avalos 		return ret;
2294e0769fe6SDarren Hart 	/*
2295e0769fe6SDarren Hart 	 * Check for USB 3 mode where:
229617fe4b3dSAzael Avalos 	 * 0 - Disabled (Acts like a USB 2 port, saving power)
229717fe4b3dSAzael Avalos 	 * 1 - Enabled
229817fe4b3dSAzael Avalos 	 */
229917fe4b3dSAzael Avalos 	if (state != 0 && state != 1)
230017fe4b3dSAzael Avalos 		return -EINVAL;
230117fe4b3dSAzael Avalos 
230217fe4b3dSAzael Avalos 	ret = toshiba_usb_three_set(toshiba, state);
230317fe4b3dSAzael Avalos 	if (ret)
230417fe4b3dSAzael Avalos 		return ret;
230517fe4b3dSAzael Avalos 
230617fe4b3dSAzael Avalos 	pr_info("Reboot for changes to USB 3 to take effect");
230717fe4b3dSAzael Avalos 
230817fe4b3dSAzael Avalos 	return count;
230917fe4b3dSAzael Avalos }
23100c3c0f10SAzael Avalos static DEVICE_ATTR_RW(usb_three);
23119bd1213bSAzael Avalos 
23129bd1213bSAzael Avalos static struct attribute *toshiba_attributes[] = {
23139bd1213bSAzael Avalos 	&dev_attr_version.attr,
23149bd1213bSAzael Avalos 	&dev_attr_fan.attr,
23159bd1213bSAzael Avalos 	&dev_attr_kbd_backlight_mode.attr,
23169bd1213bSAzael Avalos 	&dev_attr_kbd_type.attr,
23179bd1213bSAzael Avalos 	&dev_attr_available_kbd_modes.attr,
23189bd1213bSAzael Avalos 	&dev_attr_kbd_backlight_timeout.attr,
23199bd1213bSAzael Avalos 	&dev_attr_touchpad.attr,
23209bd1213bSAzael Avalos 	&dev_attr_position.attr,
23219bd1213bSAzael Avalos 	&dev_attr_usb_sleep_charge.attr,
23229bd1213bSAzael Avalos 	&dev_attr_sleep_functions_on_battery.attr,
23239bd1213bSAzael Avalos 	&dev_attr_usb_rapid_charge.attr,
23249bd1213bSAzael Avalos 	&dev_attr_usb_sleep_music.attr,
23259bd1213bSAzael Avalos 	&dev_attr_kbd_function_keys.attr,
23269bd1213bSAzael Avalos 	&dev_attr_panel_power_on.attr,
23279bd1213bSAzael Avalos 	&dev_attr_usb_three.attr,
23289bd1213bSAzael Avalos 	NULL,
23299bd1213bSAzael Avalos };
23309bd1213bSAzael Avalos 
2331360f0f39SAzael Avalos static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2332360f0f39SAzael Avalos 					struct attribute *attr, int idx)
2333360f0f39SAzael Avalos {
2334360f0f39SAzael Avalos 	struct device *dev = container_of(kobj, struct device, kobj);
2335360f0f39SAzael Avalos 	struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2336360f0f39SAzael Avalos 	bool exists = true;
2337360f0f39SAzael Avalos 
233894477d4cSAzael Avalos 	if (attr == &dev_attr_fan.attr)
233994477d4cSAzael Avalos 		exists = (drv->fan_supported) ? true : false;
234094477d4cSAzael Avalos 	else if (attr == &dev_attr_kbd_backlight_mode.attr)
2341360f0f39SAzael Avalos 		exists = (drv->kbd_illum_supported) ? true : false;
2342360f0f39SAzael Avalos 	else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2343360f0f39SAzael Avalos 		exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
23449d8658acSAzael Avalos 	else if (attr == &dev_attr_touchpad.attr)
23459d8658acSAzael Avalos 		exists = (drv->touchpad_supported) ? true : false;
23465a2813e9SAzael Avalos 	else if (attr == &dev_attr_position.attr)
23475a2813e9SAzael Avalos 		exists = (drv->accelerometer_supported) ? true : false;
2348e26ffe51SAzael Avalos 	else if (attr == &dev_attr_usb_sleep_charge.attr)
2349e26ffe51SAzael Avalos 		exists = (drv->usb_sleep_charge_supported) ? true : false;
2350182bcaa5SAzael Avalos 	else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2351182bcaa5SAzael Avalos 		exists = (drv->usb_sleep_charge_supported) ? true : false;
2352bb3fe01fSAzael Avalos 	else if (attr == &dev_attr_usb_rapid_charge.attr)
2353bb3fe01fSAzael Avalos 		exists = (drv->usb_rapid_charge_supported) ? true : false;
2354172ce0a9SAzael Avalos 	else if (attr == &dev_attr_usb_sleep_music.attr)
2355172ce0a9SAzael Avalos 		exists = (drv->usb_sleep_music_supported) ? true : false;
2356bae84195SAzael Avalos 	else if (attr == &dev_attr_kbd_function_keys.attr)
2357bae84195SAzael Avalos 		exists = (drv->kbd_function_keys_supported) ? true : false;
235835d53ceaSAzael Avalos 	else if (attr == &dev_attr_panel_power_on.attr)
235935d53ceaSAzael Avalos 		exists = (drv->panel_power_on_supported) ? true : false;
236017fe4b3dSAzael Avalos 	else if (attr == &dev_attr_usb_three.attr)
236117fe4b3dSAzael Avalos 		exists = (drv->usb_three_supported) ? true : false;
2362360f0f39SAzael Avalos 
2363360f0f39SAzael Avalos 	return exists ? attr->mode : 0;
2364360f0f39SAzael Avalos }
2365360f0f39SAzael Avalos 
23669bd1213bSAzael Avalos static struct attribute_group toshiba_attr_group = {
23679bd1213bSAzael Avalos 	.is_visible = toshiba_sysfs_is_visible,
23689bd1213bSAzael Avalos 	.attrs = toshiba_attributes,
23699bd1213bSAzael Avalos };
23709bd1213bSAzael Avalos 
23711f28f290SAzael Avalos /*
23721f28f290SAzael Avalos  * Hotkeys
23731f28f290SAzael Avalos  */
23741f28f290SAzael Avalos static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
23751f28f290SAzael Avalos {
23761f28f290SAzael Avalos 	acpi_status status;
23771f28f290SAzael Avalos 	u32 result;
23781f28f290SAzael Avalos 
23791f28f290SAzael Avalos 	status = acpi_evaluate_object(dev->acpi_dev->handle,
23801f28f290SAzael Avalos 				      "ENAB", NULL, NULL);
23811f28f290SAzael Avalos 	if (ACPI_FAILURE(status))
23821f28f290SAzael Avalos 		return -ENODEV;
23831f28f290SAzael Avalos 
23841f28f290SAzael Avalos 	result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
23851f28f290SAzael Avalos 	if (result == TOS_FAILURE)
23861f28f290SAzael Avalos 		return -EIO;
23871f28f290SAzael Avalos 	else if (result == TOS_NOT_SUPPORTED)
23881f28f290SAzael Avalos 		return -ENODEV;
23891f28f290SAzael Avalos 
23901f28f290SAzael Avalos 	return 0;
23911f28f290SAzael Avalos }
23921f28f290SAzael Avalos 
2393fb42d1f4SAzael Avalos static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2394fb42d1f4SAzael Avalos {
2395fb42d1f4SAzael Avalos 	u32 result;
2396fb42d1f4SAzael Avalos 
2397fb42d1f4SAzael Avalos 	/*
2398fb42d1f4SAzael Avalos 	 * Re-activate the hotkeys, but this time, we are using the
2399fb42d1f4SAzael Avalos 	 * "Special Functions" mode.
2400fb42d1f4SAzael Avalos 	 */
2401fb42d1f4SAzael Avalos 	result = hci_write1(dev, HCI_HOTKEY_EVENT,
2402fb42d1f4SAzael Avalos 			    HCI_HOTKEY_SPECIAL_FUNCTIONS);
2403fb42d1f4SAzael Avalos 	if (result != TOS_SUCCESS)
2404fb42d1f4SAzael Avalos 		pr_err("Could not enable the Special Function mode\n");
2405fb42d1f4SAzael Avalos }
2406fb42d1f4SAzael Avalos 
240729cd293fSSeth Forshee static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
240829cd293fSSeth Forshee 				      struct serio *port)
240929cd293fSSeth Forshee {
241098280374SGiedrius Statkevičius 	if (str & I8042_STR_AUXDATA)
241129cd293fSSeth Forshee 		return false;
241229cd293fSSeth Forshee 
241329cd293fSSeth Forshee 	if (unlikely(data == 0xe0))
241429cd293fSSeth Forshee 		return false;
241529cd293fSSeth Forshee 
241629cd293fSSeth Forshee 	if ((data & 0x7f) == TOS1900_FN_SCAN) {
241729cd293fSSeth Forshee 		schedule_work(&toshiba_acpi->hotkey_work);
241829cd293fSSeth Forshee 		return true;
241929cd293fSSeth Forshee 	}
242029cd293fSSeth Forshee 
242129cd293fSSeth Forshee 	return false;
242229cd293fSSeth Forshee }
242329cd293fSSeth Forshee 
242429cd293fSSeth Forshee static void toshiba_acpi_hotkey_work(struct work_struct *work)
242529cd293fSSeth Forshee {
242629cd293fSSeth Forshee 	acpi_handle ec_handle = ec_get_handle();
242729cd293fSSeth Forshee 	acpi_status status;
242829cd293fSSeth Forshee 
242929cd293fSSeth Forshee 	if (!ec_handle)
243029cd293fSSeth Forshee 		return;
243129cd293fSSeth Forshee 
243229cd293fSSeth Forshee 	status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
243329cd293fSSeth Forshee 	if (ACPI_FAILURE(status))
243429cd293fSSeth Forshee 		pr_err("ACPI NTFY method execution failed\n");
243529cd293fSSeth Forshee }
243629cd293fSSeth Forshee 
243729cd293fSSeth Forshee /*
243829cd293fSSeth Forshee  * Returns hotkey scancode, or < 0 on failure.
243929cd293fSSeth Forshee  */
244029cd293fSSeth Forshee static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
244129cd293fSSeth Forshee {
244274facaf7SZhang Rui 	unsigned long long value;
244329cd293fSSeth Forshee 	acpi_status status;
244429cd293fSSeth Forshee 
244574facaf7SZhang Rui 	status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
244674facaf7SZhang Rui 				      NULL, &value);
244774facaf7SZhang Rui 	if (ACPI_FAILURE(status)) {
244829cd293fSSeth Forshee 		pr_err("ACPI INFO method execution failed\n");
244929cd293fSSeth Forshee 		return -EIO;
245029cd293fSSeth Forshee 	}
245129cd293fSSeth Forshee 
245274facaf7SZhang Rui 	return value;
245329cd293fSSeth Forshee }
245429cd293fSSeth Forshee 
245529cd293fSSeth Forshee static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
245629cd293fSSeth Forshee 				       int scancode)
245729cd293fSSeth Forshee {
245829cd293fSSeth Forshee 	if (scancode == 0x100)
245929cd293fSSeth Forshee 		return;
246029cd293fSSeth Forshee 
2461e0769fe6SDarren Hart 	/* Act on key press; ignore key release */
246229cd293fSSeth Forshee 	if (scancode & 0x80)
246329cd293fSSeth Forshee 		return;
246429cd293fSSeth Forshee 
246529cd293fSSeth Forshee 	if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
246629cd293fSSeth Forshee 		pr_info("Unknown key %x\n", scancode);
246729cd293fSSeth Forshee }
246829cd293fSSeth Forshee 
246971454d78SAzael Avalos static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
247071454d78SAzael Avalos {
247171454d78SAzael Avalos 	u32 hci_result, value;
247271454d78SAzael Avalos 	int retries = 3;
247371454d78SAzael Avalos 	int scancode;
247471454d78SAzael Avalos 
247571454d78SAzael Avalos 	if (dev->info_supported) {
247671454d78SAzael Avalos 		scancode = toshiba_acpi_query_hotkey(dev);
247771454d78SAzael Avalos 		if (scancode < 0)
247871454d78SAzael Avalos 			pr_err("Failed to query hotkey event\n");
247971454d78SAzael Avalos 		else if (scancode != 0)
248071454d78SAzael Avalos 			toshiba_acpi_report_hotkey(dev, scancode);
248171454d78SAzael Avalos 	} else if (dev->system_event_supported) {
248271454d78SAzael Avalos 		do {
248371454d78SAzael Avalos 			hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
248471454d78SAzael Avalos 			switch (hci_result) {
248571454d78SAzael Avalos 			case TOS_SUCCESS:
248671454d78SAzael Avalos 				toshiba_acpi_report_hotkey(dev, (int)value);
248771454d78SAzael Avalos 				break;
248871454d78SAzael Avalos 			case TOS_NOT_SUPPORTED:
248971454d78SAzael Avalos 				/*
249071454d78SAzael Avalos 				 * This is a workaround for an unresolved
249171454d78SAzael Avalos 				 * issue on some machines where system events
249271454d78SAzael Avalos 				 * sporadically become disabled.
249371454d78SAzael Avalos 				 */
249471454d78SAzael Avalos 				hci_result =
249571454d78SAzael Avalos 					hci_write1(dev, HCI_SYSTEM_EVENT, 1);
249671454d78SAzael Avalos 				pr_notice("Re-enabled hotkeys\n");
2497e0769fe6SDarren Hart 				/* Fall through */
249871454d78SAzael Avalos 			default:
249971454d78SAzael Avalos 				retries--;
250071454d78SAzael Avalos 				break;
250171454d78SAzael Avalos 			}
250271454d78SAzael Avalos 		} while (retries && hci_result != TOS_FIFO_EMPTY);
250371454d78SAzael Avalos 	}
250471454d78SAzael Avalos }
250571454d78SAzael Avalos 
2506b859f159SGreg Kroah-Hartman static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
25076335e4d5SMatthew Garrett {
2508fe808bfbSTakashi Iwai 	const struct key_entry *keymap = toshiba_acpi_keymap;
2509a2b3471bSAzael Avalos 	acpi_handle ec_handle;
2510a2b3471bSAzael Avalos 	u32 events_type;
2511a2b3471bSAzael Avalos 	u32 hci_result;
2512a2b3471bSAzael Avalos 	int error;
2513a2b3471bSAzael Avalos 
2514a2b3471bSAzael Avalos 	error = toshiba_acpi_enable_hotkeys(dev);
2515a2b3471bSAzael Avalos 	if (error)
2516a2b3471bSAzael Avalos 		return error;
2517a2b3471bSAzael Avalos 
2518a2b3471bSAzael Avalos 	error = toshiba_hotkey_event_type_get(dev, &events_type);
2519a2b3471bSAzael Avalos 	if (error) {
2520a2b3471bSAzael Avalos 		pr_err("Unable to query Hotkey Event Type\n");
2521a2b3471bSAzael Avalos 		return error;
2522a2b3471bSAzael Avalos 	}
2523a2b3471bSAzael Avalos 	dev->hotkey_event_type = events_type;
2524135740deSSeth Forshee 
2525135740deSSeth Forshee 	dev->hotkey_dev = input_allocate_device();
2526b222cca6SJoe Perches 	if (!dev->hotkey_dev)
2527135740deSSeth Forshee 		return -ENOMEM;
2528135740deSSeth Forshee 
2529135740deSSeth Forshee 	dev->hotkey_dev->name = "Toshiba input device";
25306e02cc7eSSeth Forshee 	dev->hotkey_dev->phys = "toshiba_acpi/input0";
2531135740deSSeth Forshee 	dev->hotkey_dev->id.bustype = BUS_HOST;
2532135740deSSeth Forshee 
2533a2b3471bSAzael Avalos 	if (events_type == HCI_SYSTEM_TYPE1 ||
2534a2b3471bSAzael Avalos 	    !dev->kbd_function_keys_supported)
2535a2b3471bSAzael Avalos 		keymap = toshiba_acpi_keymap;
2536a2b3471bSAzael Avalos 	else if (events_type == HCI_SYSTEM_TYPE2 ||
2537a2b3471bSAzael Avalos 		 dev->kbd_function_keys_supported)
2538fe808bfbSTakashi Iwai 		keymap = toshiba_acpi_alt_keymap;
2539a2b3471bSAzael Avalos 	else
2540a2b3471bSAzael Avalos 		pr_info("Unknown event type received %x\n", events_type);
2541fe808bfbSTakashi Iwai 	error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
2542135740deSSeth Forshee 	if (error)
2543135740deSSeth Forshee 		goto err_free_dev;
2544135740deSSeth Forshee 
254529cd293fSSeth Forshee 	/*
254629cd293fSSeth Forshee 	 * For some machines the SCI responsible for providing hotkey
254729cd293fSSeth Forshee 	 * notification doesn't fire. We can trigger the notification
254829cd293fSSeth Forshee 	 * whenever the Fn key is pressed using the NTFY method, if
254929cd293fSSeth Forshee 	 * supported, so if it's present set up an i8042 key filter
255029cd293fSSeth Forshee 	 * for this purpose.
255129cd293fSSeth Forshee 	 */
255229cd293fSSeth Forshee 	ec_handle = ec_get_handle();
2553e2e19606SZhang Rui 	if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
255429cd293fSSeth Forshee 		INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
255529cd293fSSeth Forshee 
255629cd293fSSeth Forshee 		error = i8042_install_filter(toshiba_acpi_i8042_filter);
255729cd293fSSeth Forshee 		if (error) {
255829cd293fSSeth Forshee 			pr_err("Error installing key filter\n");
255929cd293fSSeth Forshee 			goto err_free_keymap;
256029cd293fSSeth Forshee 		}
256129cd293fSSeth Forshee 
256229cd293fSSeth Forshee 		dev->ntfy_supported = 1;
256329cd293fSSeth Forshee 	}
256429cd293fSSeth Forshee 
256529cd293fSSeth Forshee 	/*
256629cd293fSSeth Forshee 	 * Determine hotkey query interface. Prefer using the INFO
256729cd293fSSeth Forshee 	 * method when it is available.
256829cd293fSSeth Forshee 	 */
2569e2e19606SZhang Rui 	if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
257029cd293fSSeth Forshee 		dev->info_supported = 1;
2571e2e19606SZhang Rui 	else {
2572893f3f62SAzael Avalos 		hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
25731864bbc2SAzael Avalos 		if (hci_result == TOS_SUCCESS)
257429cd293fSSeth Forshee 			dev->system_event_supported = 1;
257529cd293fSSeth Forshee 	}
257629cd293fSSeth Forshee 
257729cd293fSSeth Forshee 	if (!dev->info_supported && !dev->system_event_supported) {
257829cd293fSSeth Forshee 		pr_warn("No hotkey query interface found\n");
257929cd293fSSeth Forshee 		goto err_remove_filter;
258029cd293fSSeth Forshee 	}
258129cd293fSSeth Forshee 
2582135740deSSeth Forshee 	error = input_register_device(dev->hotkey_dev);
2583135740deSSeth Forshee 	if (error) {
2584135740deSSeth Forshee 		pr_info("Unable to register input device\n");
258529cd293fSSeth Forshee 		goto err_remove_filter;
2586135740deSSeth Forshee 	}
2587135740deSSeth Forshee 
2588135740deSSeth Forshee 	return 0;
2589135740deSSeth Forshee 
259029cd293fSSeth Forshee  err_remove_filter:
259129cd293fSSeth Forshee 	if (dev->ntfy_supported)
259229cd293fSSeth Forshee 		i8042_remove_filter(toshiba_acpi_i8042_filter);
2593135740deSSeth Forshee  err_free_keymap:
2594135740deSSeth Forshee 	sparse_keymap_free(dev->hotkey_dev);
2595135740deSSeth Forshee  err_free_dev:
2596135740deSSeth Forshee 	input_free_device(dev->hotkey_dev);
2597135740deSSeth Forshee 	dev->hotkey_dev = NULL;
2598135740deSSeth Forshee 	return error;
2599135740deSSeth Forshee }
2600135740deSSeth Forshee 
2601b859f159SGreg Kroah-Hartman static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
260262cce752SSeth Forshee {
260362cce752SSeth Forshee 	struct backlight_properties props;
260462cce752SSeth Forshee 	int brightness;
260562cce752SSeth Forshee 	int ret;
2606121b7b0dSAkio Idehara 	bool enabled;
260762cce752SSeth Forshee 
260862cce752SSeth Forshee 	/*
260962cce752SSeth Forshee 	 * Some machines don't support the backlight methods at all, and
261062cce752SSeth Forshee 	 * others support it read-only. Either of these is pretty useless,
261162cce752SSeth Forshee 	 * so only register the backlight device if the backlight method
261262cce752SSeth Forshee 	 * supports both reads and writes.
261362cce752SSeth Forshee 	 */
261462cce752SSeth Forshee 	brightness = __get_lcd_brightness(dev);
261562cce752SSeth Forshee 	if (brightness < 0)
261662cce752SSeth Forshee 		return 0;
261762cce752SSeth Forshee 	ret = set_lcd_brightness(dev, brightness);
261862cce752SSeth Forshee 	if (ret) {
261962cce752SSeth Forshee 		pr_debug("Backlight method is read-only, disabling backlight support\n");
262062cce752SSeth Forshee 		return 0;
262162cce752SSeth Forshee 	}
262262cce752SSeth Forshee 
2623121b7b0dSAkio Idehara 	/* Determine whether or not BIOS supports transflective backlight */
2624121b7b0dSAkio Idehara 	ret = get_tr_backlight_status(dev, &enabled);
2625121b7b0dSAkio Idehara 	dev->tr_backlight_supported = !ret;
2626121b7b0dSAkio Idehara 
262753039f22SMatthew Garrett 	memset(&props, 0, sizeof(props));
262862cce752SSeth Forshee 	props.type = BACKLIGHT_PLATFORM;
262962cce752SSeth Forshee 	props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
263062cce752SSeth Forshee 
2631e0769fe6SDarren Hart 	/* Adding an extra level and having 0 change to transflective mode */
2632121b7b0dSAkio Idehara 	if (dev->tr_backlight_supported)
2633121b7b0dSAkio Idehara 		props.max_brightness++;
2634121b7b0dSAkio Idehara 
263562cce752SSeth Forshee 	dev->backlight_dev = backlight_device_register("toshiba",
263662cce752SSeth Forshee 						       &dev->acpi_dev->dev,
263762cce752SSeth Forshee 						       dev,
263862cce752SSeth Forshee 						       &toshiba_backlight_data,
263962cce752SSeth Forshee 						       &props);
264062cce752SSeth Forshee 	if (IS_ERR(dev->backlight_dev)) {
264162cce752SSeth Forshee 		ret = PTR_ERR(dev->backlight_dev);
264262cce752SSeth Forshee 		pr_err("Could not register toshiba backlight device\n");
264362cce752SSeth Forshee 		dev->backlight_dev = NULL;
264462cce752SSeth Forshee 		return ret;
264562cce752SSeth Forshee 	}
264662cce752SSeth Forshee 
264762cce752SSeth Forshee 	dev->backlight_dev->props.brightness = brightness;
264862cce752SSeth Forshee 	return 0;
264962cce752SSeth Forshee }
265062cce752SSeth Forshee 
265151fac838SRafael J. Wysocki static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
2652135740deSSeth Forshee {
2653135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2654135740deSSeth Forshee 
265536d03f93SSeth Forshee 	remove_toshiba_proc_entries(dev);
2656135740deSSeth Forshee 
2657360f0f39SAzael Avalos 	if (dev->sysfs_created)
2658360f0f39SAzael Avalos 		sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2659360f0f39SAzael Avalos 				   &toshiba_attr_group);
2660360f0f39SAzael Avalos 
266129cd293fSSeth Forshee 	if (dev->ntfy_supported) {
266229cd293fSSeth Forshee 		i8042_remove_filter(toshiba_acpi_i8042_filter);
266329cd293fSSeth Forshee 		cancel_work_sync(&dev->hotkey_work);
266429cd293fSSeth Forshee 	}
266529cd293fSSeth Forshee 
2666135740deSSeth Forshee 	if (dev->hotkey_dev) {
2667135740deSSeth Forshee 		input_unregister_device(dev->hotkey_dev);
2668135740deSSeth Forshee 		sparse_keymap_free(dev->hotkey_dev);
2669135740deSSeth Forshee 	}
2670135740deSSeth Forshee 
2671135740deSSeth Forshee 	if (dev->bt_rfk) {
2672135740deSSeth Forshee 		rfkill_unregister(dev->bt_rfk);
2673135740deSSeth Forshee 		rfkill_destroy(dev->bt_rfk);
2674135740deSSeth Forshee 	}
2675135740deSSeth Forshee 
2676135740deSSeth Forshee 	backlight_device_unregister(dev->backlight_dev);
2677135740deSSeth Forshee 
267836d03f93SSeth Forshee 	if (dev->illumination_supported)
2679135740deSSeth Forshee 		led_classdev_unregister(&dev->led_dev);
2680135740deSSeth Forshee 
2681360f0f39SAzael Avalos 	if (dev->kbd_led_registered)
2682360f0f39SAzael Avalos 		led_classdev_unregister(&dev->kbd_led);
2683360f0f39SAzael Avalos 
2684def6c4e2SAzael Avalos 	if (dev->eco_supported)
2685def6c4e2SAzael Avalos 		led_classdev_unregister(&dev->eco_led);
2686def6c4e2SAzael Avalos 
268729cd293fSSeth Forshee 	if (toshiba_acpi)
268829cd293fSSeth Forshee 		toshiba_acpi = NULL;
268929cd293fSSeth Forshee 
2690135740deSSeth Forshee 	kfree(dev);
2691135740deSSeth Forshee 
2692135740deSSeth Forshee 	return 0;
2693135740deSSeth Forshee }
2694135740deSSeth Forshee 
2695b859f159SGreg Kroah-Hartman static const char *find_hci_method(acpi_handle handle)
2696a540d6b5SSeth Forshee {
2697e2e19606SZhang Rui 	if (acpi_has_method(handle, "GHCI"))
2698a540d6b5SSeth Forshee 		return "GHCI";
2699a540d6b5SSeth Forshee 
2700e2e19606SZhang Rui 	if (acpi_has_method(handle, "SPFC"))
2701a540d6b5SSeth Forshee 		return "SPFC";
2702a540d6b5SSeth Forshee 
2703a540d6b5SSeth Forshee 	return NULL;
2704a540d6b5SSeth Forshee }
2705a540d6b5SSeth Forshee 
2706b859f159SGreg Kroah-Hartman static int toshiba_acpi_add(struct acpi_device *acpi_dev)
2707135740deSSeth Forshee {
2708135740deSSeth Forshee 	struct toshiba_acpi_dev *dev;
2709a540d6b5SSeth Forshee 	const char *hci_method;
2710fb42d1f4SAzael Avalos 	u32 special_functions;
271136d03f93SSeth Forshee 	u32 dummy;
2712135740deSSeth Forshee 	bool bt_present;
2713135740deSSeth Forshee 	int ret = 0;
2714135740deSSeth Forshee 
271529cd293fSSeth Forshee 	if (toshiba_acpi)
271629cd293fSSeth Forshee 		return -EBUSY;
271729cd293fSSeth Forshee 
2718135740deSSeth Forshee 	pr_info("Toshiba Laptop ACPI Extras version %s\n",
2719135740deSSeth Forshee 	       TOSHIBA_ACPI_VERSION);
2720135740deSSeth Forshee 
2721a540d6b5SSeth Forshee 	hci_method = find_hci_method(acpi_dev->handle);
2722a540d6b5SSeth Forshee 	if (!hci_method) {
2723a540d6b5SSeth Forshee 		pr_err("HCI interface not found\n");
27246e02cc7eSSeth Forshee 		return -ENODEV;
2725a540d6b5SSeth Forshee 	}
27266e02cc7eSSeth Forshee 
2727135740deSSeth Forshee 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2728135740deSSeth Forshee 	if (!dev)
2729135740deSSeth Forshee 		return -ENOMEM;
2730135740deSSeth Forshee 	dev->acpi_dev = acpi_dev;
2731a540d6b5SSeth Forshee 	dev->method_hci = hci_method;
2732135740deSSeth Forshee 	acpi_dev->driver_data = dev;
2733360f0f39SAzael Avalos 	dev_set_drvdata(&acpi_dev->dev, dev);
2734135740deSSeth Forshee 
2735a2b3471bSAzael Avalos 	/* Query the BIOS for supported features */
2736a2b3471bSAzael Avalos 
2737a2b3471bSAzael Avalos 	/*
2738a2b3471bSAzael Avalos 	 * The "Special Functions" are always supported by the laptops
2739a2b3471bSAzael Avalos 	 * with the new keyboard layout, query for its presence to help
2740a2b3471bSAzael Avalos 	 * determine the keymap layout to use.
2741a2b3471bSAzael Avalos 	 */
2742fb42d1f4SAzael Avalos 	ret = toshiba_function_keys_get(dev, &special_functions);
2743a2b3471bSAzael Avalos 	dev->kbd_function_keys_supported = !ret;
2744a2b3471bSAzael Avalos 
27456e02cc7eSSeth Forshee 	if (toshiba_acpi_setup_keyboard(dev))
2746135740deSSeth Forshee 		pr_info("Unable to activate hotkeys\n");
2747135740deSSeth Forshee 
2748135740deSSeth Forshee 	mutex_init(&dev->mutex);
2749135740deSSeth Forshee 
275062cce752SSeth Forshee 	ret = toshiba_acpi_setup_backlight(dev);
275162cce752SSeth Forshee 	if (ret)
2752135740deSSeth Forshee 		goto error;
2753135740deSSeth Forshee 
2754135740deSSeth Forshee 	/* Register rfkill switch for Bluetooth */
27551864bbc2SAzael Avalos 	if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
2756135740deSSeth Forshee 		dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2757135740deSSeth Forshee 					   &acpi_dev->dev,
2758135740deSSeth Forshee 					   RFKILL_TYPE_BLUETOOTH,
2759135740deSSeth Forshee 					   &toshiba_rfk_ops,
2760135740deSSeth Forshee 					   dev);
2761135740deSSeth Forshee 		if (!dev->bt_rfk) {
2762135740deSSeth Forshee 			pr_err("unable to allocate rfkill device\n");
2763135740deSSeth Forshee 			ret = -ENOMEM;
2764135740deSSeth Forshee 			goto error;
2765135740deSSeth Forshee 		}
2766135740deSSeth Forshee 
2767135740deSSeth Forshee 		ret = rfkill_register(dev->bt_rfk);
2768135740deSSeth Forshee 		if (ret) {
2769135740deSSeth Forshee 			pr_err("unable to register rfkill device\n");
2770135740deSSeth Forshee 			rfkill_destroy(dev->bt_rfk);
2771135740deSSeth Forshee 			goto error;
2772135740deSSeth Forshee 		}
2773135740deSSeth Forshee 	}
2774135740deSSeth Forshee 
2775135740deSSeth Forshee 	if (toshiba_illumination_available(dev)) {
2776135740deSSeth Forshee 		dev->led_dev.name = "toshiba::illumination";
2777135740deSSeth Forshee 		dev->led_dev.max_brightness = 1;
2778135740deSSeth Forshee 		dev->led_dev.brightness_set = toshiba_illumination_set;
2779135740deSSeth Forshee 		dev->led_dev.brightness_get = toshiba_illumination_get;
2780135740deSSeth Forshee 		if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
278136d03f93SSeth Forshee 			dev->illumination_supported = 1;
2782135740deSSeth Forshee 	}
2783135740deSSeth Forshee 
2784def6c4e2SAzael Avalos 	if (toshiba_eco_mode_available(dev)) {
2785def6c4e2SAzael Avalos 		dev->eco_led.name = "toshiba::eco_mode";
2786def6c4e2SAzael Avalos 		dev->eco_led.max_brightness = 1;
2787def6c4e2SAzael Avalos 		dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2788def6c4e2SAzael Avalos 		dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2789def6c4e2SAzael Avalos 		if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2790def6c4e2SAzael Avalos 			dev->eco_supported = 1;
2791def6c4e2SAzael Avalos 	}
2792def6c4e2SAzael Avalos 
279393f8c16dSAzael Avalos 	dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
2794360f0f39SAzael Avalos 	/*
2795360f0f39SAzael Avalos 	 * Only register the LED if KBD illumination is supported
2796360f0f39SAzael Avalos 	 * and the keyboard backlight operation mode is set to FN-Z
2797360f0f39SAzael Avalos 	 */
2798360f0f39SAzael Avalos 	if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2799360f0f39SAzael Avalos 		dev->kbd_led.name = "toshiba::kbd_backlight";
2800360f0f39SAzael Avalos 		dev->kbd_led.max_brightness = 1;
2801360f0f39SAzael Avalos 		dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2802360f0f39SAzael Avalos 		dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2803360f0f39SAzael Avalos 		if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2804360f0f39SAzael Avalos 			dev->kbd_led_registered = 1;
2805360f0f39SAzael Avalos 	}
2806360f0f39SAzael Avalos 
28079d8658acSAzael Avalos 	ret = toshiba_touchpad_get(dev, &dummy);
28089d8658acSAzael Avalos 	dev->touchpad_supported = !ret;
28099d8658acSAzael Avalos 
28105a2813e9SAzael Avalos 	ret = toshiba_accelerometer_supported(dev);
28115a2813e9SAzael Avalos 	dev->accelerometer_supported = !ret;
28125a2813e9SAzael Avalos 
2813c8c91842SAzael Avalos 	toshiba_usb_sleep_charge_available(dev);
2814e26ffe51SAzael Avalos 
2815bb3fe01fSAzael Avalos 	ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2816bb3fe01fSAzael Avalos 	dev->usb_rapid_charge_supported = !ret;
2817bb3fe01fSAzael Avalos 
2818172ce0a9SAzael Avalos 	ret = toshiba_usb_sleep_music_get(dev, &dummy);
2819172ce0a9SAzael Avalos 	dev->usb_sleep_music_supported = !ret;
2820172ce0a9SAzael Avalos 
282135d53ceaSAzael Avalos 	ret = toshiba_panel_power_on_get(dev, &dummy);
282235d53ceaSAzael Avalos 	dev->panel_power_on_supported = !ret;
282335d53ceaSAzael Avalos 
282417fe4b3dSAzael Avalos 	ret = toshiba_usb_three_get(dev, &dummy);
282517fe4b3dSAzael Avalos 	dev->usb_three_supported = !ret;
282617fe4b3dSAzael Avalos 
282736d03f93SSeth Forshee 	ret = get_video_status(dev, &dummy);
282836d03f93SSeth Forshee 	dev->video_supported = !ret;
282936d03f93SSeth Forshee 
283036d03f93SSeth Forshee 	ret = get_fan_status(dev, &dummy);
283136d03f93SSeth Forshee 	dev->fan_supported = !ret;
283236d03f93SSeth Forshee 
2833fb42d1f4SAzael Avalos 	/*
2834fb42d1f4SAzael Avalos 	 * Enable the "Special Functions" mode only if they are
2835fb42d1f4SAzael Avalos 	 * supported and if they are activated.
2836fb42d1f4SAzael Avalos 	 */
2837fb42d1f4SAzael Avalos 	if (dev->kbd_function_keys_supported && special_functions)
2838fb42d1f4SAzael Avalos 		toshiba_acpi_enable_special_functions(dev);
2839fb42d1f4SAzael Avalos 
2840360f0f39SAzael Avalos 	ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2841360f0f39SAzael Avalos 				 &toshiba_attr_group);
2842360f0f39SAzael Avalos 	if (ret) {
2843360f0f39SAzael Avalos 		dev->sysfs_created = 0;
2844360f0f39SAzael Avalos 		goto error;
2845360f0f39SAzael Avalos 	}
2846360f0f39SAzael Avalos 	dev->sysfs_created = !ret;
2847360f0f39SAzael Avalos 
284836d03f93SSeth Forshee 	create_toshiba_proc_entries(dev);
284936d03f93SSeth Forshee 
285029cd293fSSeth Forshee 	toshiba_acpi = dev;
285129cd293fSSeth Forshee 
2852135740deSSeth Forshee 	return 0;
2853135740deSSeth Forshee 
2854135740deSSeth Forshee error:
285551fac838SRafael J. Wysocki 	toshiba_acpi_remove(acpi_dev);
2856135740deSSeth Forshee 	return ret;
2857135740deSSeth Forshee }
2858135740deSSeth Forshee 
2859135740deSSeth Forshee static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2860135740deSSeth Forshee {
2861135740deSSeth Forshee 	struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
286280546905SAzael Avalos 	int ret;
28636335e4d5SMatthew Garrett 
286471454d78SAzael Avalos 	switch (event) {
286571454d78SAzael Avalos 	case 0x80: /* Hotkeys and some system events */
286671454d78SAzael Avalos 		toshiba_acpi_process_hotkeys(dev);
286711948b93SSeth Forshee 		break;
2868bab09e23SAzael Avalos 	case 0x81: /* Dock events */
2869bab09e23SAzael Avalos 	case 0x82:
2870bab09e23SAzael Avalos 	case 0x83:
2871bab09e23SAzael Avalos 		pr_info("Dock event received %x\n", event);
2872bab09e23SAzael Avalos 		break;
2873bab09e23SAzael Avalos 	case 0x88: /* Thermal events */
2874bab09e23SAzael Avalos 		pr_info("Thermal event received\n");
2875bab09e23SAzael Avalos 		break;
2876bab09e23SAzael Avalos 	case 0x8f: /* LID closed */
2877bab09e23SAzael Avalos 	case 0x90: /* LID is closed and Dock has been ejected */
2878bab09e23SAzael Avalos 		break;
2879bab09e23SAzael Avalos 	case 0x8c: /* SATA power events */
2880bab09e23SAzael Avalos 	case 0x8b:
2881bab09e23SAzael Avalos 		pr_info("SATA power event received %x\n", event);
2882bab09e23SAzael Avalos 		break;
288380546905SAzael Avalos 	case 0x92: /* Keyboard backlight mode changed */
288480546905SAzael Avalos 		/* Update sysfs entries */
288580546905SAzael Avalos 		ret = sysfs_update_group(&acpi_dev->dev.kobj,
288680546905SAzael Avalos 					 &toshiba_attr_group);
288780546905SAzael Avalos 		if (ret)
288880546905SAzael Avalos 			pr_err("Unable to update sysfs entries\n");
288980546905SAzael Avalos 		break;
2890bab09e23SAzael Avalos 	case 0x85: /* Unknown */
2891bab09e23SAzael Avalos 	case 0x8d: /* Unknown */
289271454d78SAzael Avalos 	case 0x8e: /* Unknown */
2893bab09e23SAzael Avalos 	case 0x94: /* Unknown */
2894bab09e23SAzael Avalos 	case 0x95: /* Unknown */
289511948b93SSeth Forshee 	default:
289671454d78SAzael Avalos 		pr_info("Unknown event received %x\n", event);
289711948b93SSeth Forshee 		break;
28986335e4d5SMatthew Garrett 	}
2899bab09e23SAzael Avalos 
2900bab09e23SAzael Avalos 	acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2901bab09e23SAzael Avalos 					dev_name(&acpi_dev->dev),
2902bab09e23SAzael Avalos 					event, 0);
290329cd293fSSeth Forshee }
29046335e4d5SMatthew Garrett 
29053567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
290643d2fd3bSRafael J. Wysocki static int toshiba_acpi_suspend(struct device *device)
290729cd293fSSeth Forshee {
290843d2fd3bSRafael J. Wysocki 	struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
290929cd293fSSeth Forshee 	u32 result;
291029cd293fSSeth Forshee 
291129cd293fSSeth Forshee 	if (dev->hotkey_dev)
2912893f3f62SAzael Avalos 		result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
291329cd293fSSeth Forshee 
291429cd293fSSeth Forshee 	return 0;
291529cd293fSSeth Forshee }
291629cd293fSSeth Forshee 
291743d2fd3bSRafael J. Wysocki static int toshiba_acpi_resume(struct device *device)
291829cd293fSSeth Forshee {
291943d2fd3bSRafael J. Wysocki 	struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
29201f28f290SAzael Avalos 	int error;
292129cd293fSSeth Forshee 
2922e7fdb762SBenjamin Tissoires 	if (dev->hotkey_dev) {
29231f28f290SAzael Avalos 		error = toshiba_acpi_enable_hotkeys(dev);
29241f28f290SAzael Avalos 		if (error)
2925e7fdb762SBenjamin Tissoires 			pr_info("Unable to re-enable hotkeys\n");
2926e7fdb762SBenjamin Tissoires 	}
292729cd293fSSeth Forshee 
292829cd293fSSeth Forshee 	return 0;
292929cd293fSSeth Forshee }
29303567a4e2SRafael J. Wysocki #endif
29316335e4d5SMatthew Garrett 
293243d2fd3bSRafael J. Wysocki static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
293343d2fd3bSRafael J. Wysocki 			 toshiba_acpi_suspend, toshiba_acpi_resume);
293443d2fd3bSRafael J. Wysocki 
2935135740deSSeth Forshee static struct acpi_driver toshiba_acpi_driver = {
2936135740deSSeth Forshee 	.name	= "Toshiba ACPI driver",
2937135740deSSeth Forshee 	.owner	= THIS_MODULE,
2938135740deSSeth Forshee 	.ids	= toshiba_device_ids,
2939135740deSSeth Forshee 	.flags	= ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2940135740deSSeth Forshee 	.ops	= {
2941135740deSSeth Forshee 		.add		= toshiba_acpi_add,
2942135740deSSeth Forshee 		.remove		= toshiba_acpi_remove,
2943135740deSSeth Forshee 		.notify		= toshiba_acpi_notify,
2944135740deSSeth Forshee 	},
294543d2fd3bSRafael J. Wysocki 	.drv.pm	= &toshiba_acpi_pm,
2946135740deSSeth Forshee };
2947b4f9fe12SLen Brown 
2948b4f9fe12SLen Brown static int __init toshiba_acpi_init(void)
2949b4f9fe12SLen Brown {
2950135740deSSeth Forshee 	int ret;
2951b4f9fe12SLen Brown 
2952f11f999eSSeth Forshee 	/*
2953f11f999eSSeth Forshee 	 * Machines with this WMI guid aren't supported due to bugs in
2954f11f999eSSeth Forshee 	 * their AML. This check relies on wmi initializing before
2955f11f999eSSeth Forshee 	 * toshiba_acpi to guarantee guids have been identified.
2956f11f999eSSeth Forshee 	 */
2957f11f999eSSeth Forshee 	if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2958f11f999eSSeth Forshee 		return -ENODEV;
2959f11f999eSSeth Forshee 
2960b4f9fe12SLen Brown 	toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2961b4f9fe12SLen Brown 	if (!toshiba_proc_dir) {
2962135740deSSeth Forshee 		pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
2963b4f9fe12SLen Brown 		return -ENODEV;
2964b4f9fe12SLen Brown 	}
2965b4f9fe12SLen Brown 
2966135740deSSeth Forshee 	ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2967b4f9fe12SLen Brown 	if (ret) {
2968135740deSSeth Forshee 		pr_err("Failed to register ACPI driver: %d\n", ret);
2969135740deSSeth Forshee 		remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
2970135740deSSeth Forshee 	}
2971135740deSSeth Forshee 
2972b4f9fe12SLen Brown 	return ret;
2973b4f9fe12SLen Brown }
2974b4f9fe12SLen Brown 
2975135740deSSeth Forshee static void __exit toshiba_acpi_exit(void)
2976135740deSSeth Forshee {
2977135740deSSeth Forshee 	acpi_bus_unregister_driver(&toshiba_acpi_driver);
2978135740deSSeth Forshee 	if (toshiba_proc_dir)
2979135740deSSeth Forshee 		remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
2980b4f9fe12SLen Brown }
2981b4f9fe12SLen Brown 
2982b4f9fe12SLen Brown module_init(toshiba_acpi_init);
2983b4f9fe12SLen Brown module_exit(toshiba_acpi_exit);
2984