blob: ed3671cfddab48b19f07e519d69adfe81e2a9b56 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
4 *
5 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04006 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02007 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos548c4302014-03-25 20:38:35 -06008 * Copyright (C) 2014 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 *
25 * The devolpment page for this driver is located at
26 * http://memebeam.org/toys/ToshibaAcpiDriver.
27 *
28 * Credits:
29 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
30 * engineering the Windows drivers
31 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
32 * Rob Miller - TV out and hotkeys help
33 *
34 *
35 * TODO
36 *
37 */
38
Joe Perches7e334602011-03-29 15:21:52 -070039#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
Azael Avalos548c4302014-03-25 20:38:35 -060041#define TOSHIBA_ACPI_VERSION "0.20"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define PROC_INTERFACE_VERSION 1
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/init.h>
47#include <linux/types.h>
48#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080049#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070050#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040051#include <linux/rfkill.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050052#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070053#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020054#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090055#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060056#include <linux/workqueue.h>
57#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080058#include <linux/acpi.h>
Takashi Iwaife808bfb2014-04-29 15:15:38 +020059#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/uaccess.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_AUTHOR("John Belmonte");
63MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
64MODULE_LICENSE("GPL");
65
Seth Forsheef11f9992012-01-18 13:44:11 -060066#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
67
Seth Forshee29cd2932012-01-18 13:44:09 -060068/* Scan code for Fn key on TOS1900 models */
69#define TOS1900_FN_SCAN 0x6e
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
73
Azael Avalos258c5902014-09-29 20:40:07 -060074/* The Toshiba configuration interface is composed of the HCI and the SCI,
75 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
78 * be uniform across all their models. Ideally we would just call
79 * dedicated ACPI methods instead of using this primitive interface.
80 * However the ACPI methods seem to be incomplete in some areas (for
81 * example they allow setting, but not reading, the LCD brightness value),
82 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040083 *
Azael Avalos84a62732014-03-25 20:38:29 -060084 * SCI stands for "System Configuration Interface" which aim is to
85 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87
Azael Avalos258c5902014-09-29 20:40:07 -060088#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* operations */
91#define HCI_SET 0xff00
92#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060093#define SCI_OPEN 0xf100
94#define SCI_CLOSE 0xf200
95#define SCI_GET 0xf300
96#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* return codes */
99#define HCI_SUCCESS 0x0000
100#define HCI_FAILURE 0x1000
101#define HCI_NOT_SUPPORTED 0x8000
102#define HCI_EMPTY 0x8c00
Azael Avalos5a2813e2014-03-25 20:38:34 -0600103#define HCI_DATA_NOT_AVAILABLE 0x8d20
104#define HCI_NOT_INITIALIZED 0x8d50
Azael Avalos84a62732014-03-25 20:38:29 -0600105#define SCI_OPEN_CLOSE_OK 0x0044
106#define SCI_ALREADY_OPEN 0x8100
107#define SCI_NOT_OPENED 0x8200
Azael Avalosfdb79082014-03-25 20:38:30 -0600108#define SCI_INPUT_DATA_ERROR 0x8300
Azael Avalos84a62732014-03-25 20:38:29 -0600109#define SCI_NOT_PRESENT 0x8600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/* registers */
112#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900113#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define HCI_SYSTEM_EVENT 0x0016
115#define HCI_VIDEO_OUT 0x001c
116#define HCI_HOTKEY_EVENT 0x001e
117#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400118#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600119#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600120#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600121#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600122#define HCI_ACCELEROMETER2 0x00a6
Azael Avalosfdb79082014-03-25 20:38:30 -0600123#define SCI_ILLUMINATION 0x014e
Azael Avalos360f0f32014-03-25 20:38:31 -0600124#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos9d8658a2014-03-25 20:38:32 -0600125#define SCI_TOUCHPAD 0x050e
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600128#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600129#define HCI_HOTKEY_DISABLE 0x0b
130#define HCI_HOTKEY_ENABLE 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define HCI_LCD_BRIGHTNESS_BITS 3
132#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
133#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600134#define HCI_MISC_SHIFT 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define HCI_VIDEO_OUT_LCD 0x1
136#define HCI_VIDEO_OUT_CRT 0x2
137#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400138#define HCI_WIRELESS_KILL_SWITCH 0x01
139#define HCI_WIRELESS_BT_PRESENT 0x0f
140#define HCI_WIRELESS_BT_ATTACH 0x40
141#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600142#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600143#define SCI_KBD_MODE_FNZ 0x1
144#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600145#define SCI_KBD_MODE_ON 0x8
146#define SCI_KBD_MODE_OFF 0x10
147#define SCI_KBD_TIME_MAX 0x3c001a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Seth Forshee135740d2011-09-20 16:55:49 -0500149struct toshiba_acpi_dev {
150 struct acpi_device *acpi_dev;
151 const char *method_hci;
152 struct rfkill *bt_rfk;
153 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600154 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500155 struct backlight_device *backlight_dev;
156 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600157 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600158 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500159
Seth Forshee135740d2011-09-20 16:55:49 -0500160 int force_fan;
161 int last_key_event;
162 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600163 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600164 int kbd_mode;
165 int kbd_time;
Seth Forshee135740d2011-09-20 16:55:49 -0500166
Dan Carpenter592b7462012-01-15 14:28:20 +0300167 unsigned int illumination_supported:1;
168 unsigned int video_supported:1;
169 unsigned int fan_supported:1;
170 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600171 unsigned int ntfy_supported:1;
172 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900173 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600174 unsigned int kbd_illum_supported:1;
175 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600176 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600177 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600178 unsigned int accelerometer_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600179 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500180
Seth Forshee135740d2011-09-20 16:55:49 -0500181 struct mutex mutex;
182};
183
Seth Forshee29cd2932012-01-18 13:44:09 -0600184static struct toshiba_acpi_dev *toshiba_acpi;
185
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800186static const struct acpi_device_id toshiba_device_ids[] = {
187 {"TOS6200", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400188 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800189 {"TOS1900", 0},
190 {"", 0},
191};
192MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
193
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800194static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100195 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700196 { KE_KEY, 0x101, { KEY_MUTE } },
197 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
198 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600199 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600200 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
201 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700202 { KE_KEY, 0x13b, { KEY_COFFEE } },
203 { KE_KEY, 0x13c, { KEY_BATTERY } },
204 { KE_KEY, 0x13d, { KEY_SLEEP } },
205 { KE_KEY, 0x13e, { KEY_SUSPEND } },
206 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
207 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
208 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
209 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600210 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100211 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700212 { KE_KEY, 0xb05, { KEY_PROG2 } },
213 { KE_KEY, 0xb06, { KEY_WWW } },
214 { KE_KEY, 0xb07, { KEY_MAIL } },
215 { KE_KEY, 0xb30, { KEY_STOP } },
216 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
217 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
218 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
219 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600220 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
221 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
222 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
223 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
224 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700225 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500226};
227
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200228/* alternative keymap */
229static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
230 {
231 .matches = {
232 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
233 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
234 },
235 },
Azael Avalose6efad72014-08-04 09:21:02 -0600236 {
237 .matches = {
238 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
239 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
240 },
241 },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200242 {}
243};
244
245static const struct key_entry toshiba_acpi_alt_keymap[] = {
246 { KE_KEY, 0x157, { KEY_MUTE } },
247 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
248 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600249 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200250 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
251 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
252 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
253 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
254 { KE_KEY, 0x158, { KEY_WLAN } },
255 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
256 { KE_END, 0 },
257};
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/* utility
260 */
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 *word = (*word & ~mask) | (mask * value);
265}
266
267/* acpi interface wrappers
268 */
269
Len Brown4be44fc2005-08-05 00:44:28 -0400270static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 acpi_status status;
273
Zhang Rui619400d2013-09-03 08:31:56 +0800274 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500275 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Azael Avalos258c5902014-09-29 20:40:07 -0600278/* Perform a raw configuration call. Here we don't care about input or output
279 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Azael Avalos258c5902014-09-29 20:40:07 -0600281static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
282 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600285 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600287 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 acpi_status status;
289 int i;
290
Azael Avalos258c5902014-09-29 20:40:07 -0600291 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600293 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 in_objs[i].type = ACPI_TYPE_INTEGER;
295 in_objs[i].integer.value = in[i];
296 }
297
298 results.length = sizeof(out_objs);
299 results.pointer = out_objs;
300
Seth Forshee6e02cc72011-09-20 16:55:51 -0500301 status = acpi_evaluate_object(dev->acpi_dev->handle,
302 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400303 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600304 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 for (i = 0; i < out_objs->package.count; ++i) {
306 out[i] = out_objs->package.elements[i].integer.value;
307 }
308 }
309
310 return status;
311}
312
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400313/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
315 * In addition to the ACPI status, the HCI system returns a result which
316 * may be useful (such as "not supported").
317 */
318
Seth Forshee135740d2011-09-20 16:55:49 -0500319static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
320 u32 in1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Azael Avalos258c5902014-09-29 20:40:07 -0600322 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
323 u32 out[TCI_WORDS];
324 acpi_status status = tci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
326 return status;
327}
328
Seth Forshee135740d2011-09-20 16:55:49 -0500329static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
330 u32 *out1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Azael Avalos258c5902014-09-29 20:40:07 -0600332 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
333 u32 out[TCI_WORDS];
334 acpi_status status = tci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 *out1 = out[2];
336 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
337 return status;
338}
339
Seth Forshee135740d2011-09-20 16:55:49 -0500340static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
341 u32 in1, u32 in2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400342{
Azael Avalos258c5902014-09-29 20:40:07 -0600343 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
344 u32 out[TCI_WORDS];
345 acpi_status status = tci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400346 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
347 return status;
348}
349
Seth Forshee135740d2011-09-20 16:55:49 -0500350static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
351 u32 *out1, u32 *out2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400352{
Azael Avalos258c5902014-09-29 20:40:07 -0600353 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
354 u32 out[TCI_WORDS];
355 acpi_status status = tci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400356 *out1 = out[2];
357 *out2 = out[3];
358 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
359 return status;
360}
361
Azael Avalos84a62732014-03-25 20:38:29 -0600362/* common sci tasks
363 */
364
365static int sci_open(struct toshiba_acpi_dev *dev)
366{
Azael Avalos258c5902014-09-29 20:40:07 -0600367 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
368 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600369 acpi_status status;
370
Azael Avalos258c5902014-09-29 20:40:07 -0600371 status = tci_raw(dev, in, out);
Azael Avalos84a62732014-03-25 20:38:29 -0600372 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
373 pr_err("ACPI call to open SCI failed\n");
374 return 0;
375 }
376
377 if (out[0] == SCI_OPEN_CLOSE_OK) {
378 return 1;
379 } else if (out[0] == SCI_ALREADY_OPEN) {
380 pr_info("Toshiba SCI already opened\n");
381 return 1;
382 } else if (out[0] == SCI_NOT_PRESENT) {
383 pr_info("Toshiba SCI is not present\n");
384 }
385
386 return 0;
387}
388
389static void sci_close(struct toshiba_acpi_dev *dev)
390{
Azael Avalos258c5902014-09-29 20:40:07 -0600391 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
392 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600393 acpi_status status;
394
Azael Avalos258c5902014-09-29 20:40:07 -0600395 status = tci_raw(dev, in, out);
Azael Avalos84a62732014-03-25 20:38:29 -0600396 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
397 pr_err("ACPI call to close SCI failed\n");
398 return;
399 }
400
401 if (out[0] == SCI_OPEN_CLOSE_OK)
402 return;
403 else if (out[0] == SCI_NOT_OPENED)
404 pr_info("Toshiba SCI not opened\n");
405 else if (out[0] == SCI_NOT_PRESENT)
406 pr_info("Toshiba SCI is not present\n");
407}
408
409static acpi_status sci_read(struct toshiba_acpi_dev *dev, u32 reg,
410 u32 *out1, u32 *result)
411{
Azael Avalos258c5902014-09-29 20:40:07 -0600412 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
413 u32 out[TCI_WORDS];
414 acpi_status status = tci_raw(dev, in, out);
Azael Avalos84a62732014-03-25 20:38:29 -0600415 *out1 = out[2];
416 *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
417 return status;
418}
419
420static acpi_status sci_write(struct toshiba_acpi_dev *dev, u32 reg,
421 u32 in1, u32 *result)
422{
Azael Avalos258c5902014-09-29 20:40:07 -0600423 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
424 u32 out[TCI_WORDS];
425 acpi_status status = tci_raw(dev, in, out);
Azael Avalos84a62732014-03-25 20:38:29 -0600426 *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
427 return status;
428}
429
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200430/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500431static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200432{
Azael Avalos258c5902014-09-29 20:40:07 -0600433 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
434 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200435 acpi_status status;
436
Azael Avalosfdb79082014-03-25 20:38:30 -0600437 if (!sci_open(dev))
438 return 0;
439
Azael Avalos258c5902014-09-29 20:40:07 -0600440 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600441 sci_close(dev);
442 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
443 pr_err("ACPI call to query Illumination support failed\n");
444 return 0;
Azael Avalos12962872014-09-05 11:14:04 -0600445 } else if (out[0] == HCI_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700446 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200447 return 0;
448 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600449
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200450 return 1;
451}
452
453static void toshiba_illumination_set(struct led_classdev *cdev,
454 enum led_brightness brightness)
455{
Seth Forshee135740d2011-09-20 16:55:49 -0500456 struct toshiba_acpi_dev *dev = container_of(cdev,
457 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600458 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200459 acpi_status status;
460
461 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600462 if (!sci_open(dev))
463 return;
464
465 /* Switch the illumination on/off */
466 state = brightness ? 1 : 0;
467 status = sci_write(dev, SCI_ILLUMINATION, state, &result);
468 sci_close(dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200469 if (ACPI_FAILURE(status)) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600470 pr_err("ACPI call for illumination failed\n");
471 return;
472 } else if (result == HCI_NOT_SUPPORTED) {
473 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200474 return;
475 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200476}
477
478static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
479{
Seth Forshee135740d2011-09-20 16:55:49 -0500480 struct toshiba_acpi_dev *dev = container_of(cdev,
481 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600482 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200483 acpi_status status;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200484
485 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600486 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200487 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200488
489 /* Check the illumination */
Azael Avalosfdb79082014-03-25 20:38:30 -0600490 status = sci_read(dev, SCI_ILLUMINATION, &state, &result);
491 sci_close(dev);
492 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
493 pr_err("ACPI call for illumination failed\n");
494 return LED_OFF;
495 } else if (result == HCI_NOT_SUPPORTED) {
496 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200497 return LED_OFF;
498 }
499
Azael Avalosfdb79082014-03-25 20:38:30 -0600500 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200501}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400502
Azael Avalos360f0f32014-03-25 20:38:31 -0600503/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600504static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
505{
Azael Avalos258c5902014-09-29 20:40:07 -0600506 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
507 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600508 acpi_status status;
509
510 if (!sci_open(dev))
511 return 0;
512
Azael Avalos258c5902014-09-29 20:40:07 -0600513 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600514 sci_close(dev);
515 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
516 pr_err("ACPI call to query kbd illumination support failed\n");
517 return 0;
518 } else if (out[0] == HCI_NOT_SUPPORTED) {
519 pr_info("Keyboard illumination not available\n");
520 return 0;
521 }
522
523 /* Check for keyboard backlight timeout max value,
524 * previous kbd backlight implementation set this to
525 * 0x3c0003, and now the new implementation set this
526 * to 0x3c001a, use this to distinguish between them
527 */
528 if (out[3] == SCI_KBD_TIME_MAX)
529 dev->kbd_type = 2;
530 else
531 dev->kbd_type = 1;
532 /* Get the current keyboard backlight mode */
533 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
534 /* Get the current time (1-60 seconds) */
535 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
536
537 return 1;
538}
539
Azael Avalos360f0f32014-03-25 20:38:31 -0600540static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
541{
542 u32 result;
543 acpi_status status;
544
545 if (!sci_open(dev))
546 return -EIO;
547
548 status = sci_write(dev, SCI_KBD_ILLUM_STATUS, time, &result);
549 sci_close(dev);
550 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
551 pr_err("ACPI call to set KBD backlight status failed\n");
552 return -EIO;
553 } else if (result == HCI_NOT_SUPPORTED) {
554 pr_info("Keyboard backlight status not supported\n");
555 return -ENODEV;
556 }
557
558 return 0;
559}
560
561static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
562{
563 u32 result;
564 acpi_status status;
565
566 if (!sci_open(dev))
567 return -EIO;
568
569 status = sci_read(dev, SCI_KBD_ILLUM_STATUS, time, &result);
570 sci_close(dev);
571 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
572 pr_err("ACPI call to get KBD backlight status failed\n");
573 return -EIO;
574 } else if (result == HCI_NOT_SUPPORTED) {
575 pr_info("Keyboard backlight status not supported\n");
576 return -ENODEV;
577 }
578
579 return 0;
580}
581
582static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
583{
584 struct toshiba_acpi_dev *dev = container_of(cdev,
585 struct toshiba_acpi_dev, kbd_led);
586 u32 state, result;
587 acpi_status status;
588
589 /* Check the keyboard backlight state */
590 status = hci_read1(dev, HCI_KBD_ILLUMINATION, &state, &result);
591 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
592 pr_err("ACPI call to get the keyboard backlight failed\n");
593 return LED_OFF;
594 } else if (result == HCI_NOT_SUPPORTED) {
595 pr_info("Keyboard backlight not supported\n");
596 return LED_OFF;
597 }
598
599 return state ? LED_FULL : LED_OFF;
600}
601
602static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
603 enum led_brightness brightness)
604{
605 struct toshiba_acpi_dev *dev = container_of(cdev,
606 struct toshiba_acpi_dev, kbd_led);
607 u32 state, result;
608 acpi_status status;
609
610 /* Set the keyboard backlight state */
611 state = brightness ? 1 : 0;
612 status = hci_write1(dev, HCI_KBD_ILLUMINATION, state, &result);
613 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
614 pr_err("ACPI call to set KBD Illumination mode failed\n");
615 return;
616 } else if (result == HCI_NOT_SUPPORTED) {
617 pr_info("Keyboard backlight not supported\n");
618 return;
619 }
620}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400621
Azael Avalos9d8658a2014-03-25 20:38:32 -0600622/* TouchPad support */
623static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
624{
625 u32 result;
626 acpi_status status;
627
628 if (!sci_open(dev))
629 return -EIO;
630
631 status = sci_write(dev, SCI_TOUCHPAD, state, &result);
632 sci_close(dev);
633 if (ACPI_FAILURE(status)) {
634 pr_err("ACPI call to set the touchpad failed\n");
635 return -EIO;
636 } else if (result == HCI_NOT_SUPPORTED) {
637 return -ENODEV;
638 }
639
640 return 0;
641}
642
643static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
644{
645 u32 result;
646 acpi_status status;
647
648 if (!sci_open(dev))
649 return -EIO;
650
651 status = sci_read(dev, SCI_TOUCHPAD, state, &result);
652 sci_close(dev);
653 if (ACPI_FAILURE(status)) {
654 pr_err("ACPI call to query the touchpad failed\n");
655 return -EIO;
656 } else if (result == HCI_NOT_SUPPORTED) {
657 return -ENODEV;
658 }
659
660 return 0;
661}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200662
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600663/* Eco Mode support */
664static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
665{
666 acpi_status status;
Azael Avalos258c5902014-09-29 20:40:07 -0600667 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
668 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600669
Azael Avalos258c5902014-09-29 20:40:07 -0600670 status = tci_raw(dev, in, out);
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600671 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
672 pr_info("ACPI call to get ECO led failed\n");
673 return 0;
674 }
675
676 return 1;
677}
678
679static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
680{
681 struct toshiba_acpi_dev *dev = container_of(cdev,
682 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600683 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
684 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600685 acpi_status status;
686
Azael Avalos258c5902014-09-29 20:40:07 -0600687 status = tci_raw(dev, in, out);
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600688 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
689 pr_err("ACPI call to get ECO led failed\n");
690 return LED_OFF;
691 }
692
693 return out[2] ? LED_FULL : LED_OFF;
694}
695
696static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
697 enum led_brightness brightness)
698{
699 struct toshiba_acpi_dev *dev = container_of(cdev,
700 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600701 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
702 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600703 acpi_status status;
704
705 /* Switch the Eco Mode led on/off */
706 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600707 status = tci_raw(dev, in, out);
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600708 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
709 pr_err("ACPI call to set ECO led failed\n");
710 return;
711 }
712}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400713
Azael Avalos5a2813e2014-03-25 20:38:34 -0600714/* Accelerometer support */
715static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
716{
Azael Avalos258c5902014-09-29 20:40:07 -0600717 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
718 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600719 acpi_status status;
720
721 /* Check if the accelerometer call exists,
722 * this call also serves as initialization
723 */
Azael Avalos258c5902014-09-29 20:40:07 -0600724 status = tci_raw(dev, in, out);
Azael Avalos5a2813e2014-03-25 20:38:34 -0600725 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
726 pr_err("ACPI call to query the accelerometer failed\n");
727 return -EIO;
728 } else if (out[0] == HCI_DATA_NOT_AVAILABLE ||
729 out[0] == HCI_NOT_INITIALIZED) {
730 pr_err("Accelerometer not initialized\n");
731 return -EIO;
732 } else if (out[0] == HCI_NOT_SUPPORTED) {
733 pr_info("Accelerometer not supported\n");
734 return -ENODEV;
735 }
736
737 return 0;
738}
739
740static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
741 u32 *xy, u32 *z)
742{
Azael Avalos258c5902014-09-29 20:40:07 -0600743 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
744 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600745 acpi_status status;
746
747 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600748 status = tci_raw(dev, in, out);
Azael Avalos5a2813e2014-03-25 20:38:34 -0600749 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
750 pr_err("ACPI call to query the accelerometer failed\n");
751 return -EIO;
752 }
753
754 *xy = out[2];
755 *z = out[4];
756
757 return 0;
758}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600759
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400760/* Bluetooth rfkill handlers */
761
Seth Forshee135740d2011-09-20 16:55:49 -0500762static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400763{
764 u32 hci_result;
765 u32 value, value2;
766
767 value = 0;
768 value2 = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500769 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400770 if (hci_result == HCI_SUCCESS)
771 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
772
773 return hci_result;
774}
775
Seth Forshee135740d2011-09-20 16:55:49 -0500776static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400777{
778 u32 hci_result;
779 u32 value, value2;
780
781 value = 0;
782 value2 = 0x0001;
Seth Forshee135740d2011-09-20 16:55:49 -0500783 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400784
785 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
786 return hci_result;
787}
788
Johannes Berg19d337d2009-06-02 13:01:37 +0200789static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400790{
Johannes Berg19d337d2009-06-02 13:01:37 +0200791 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400792 u32 result1, result2;
793 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +0200794 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400795 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400796
Johannes Berg19d337d2009-06-02 13:01:37 +0200797 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400798
799 mutex_lock(&dev->mutex);
Seth Forshee135740d2011-09-20 16:55:49 -0500800 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500801 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200802 goto out;
803 }
804
805 if (!radio_state) {
806 err = 0;
807 goto out;
808 }
809
Seth Forshee135740d2011-09-20 16:55:49 -0500810 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
811 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400812
813 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500814 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200815 else
816 err = 0;
817 out:
818 mutex_unlock(&dev->mutex);
819 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400820}
821
Johannes Berg19d337d2009-06-02 13:01:37 +0200822static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400823{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400824 bool new_rfk_state;
825 bool value;
826 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +0200827 struct toshiba_acpi_dev *dev = data;
828
829 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400830
Seth Forshee135740d2011-09-20 16:55:49 -0500831 hci_result = hci_get_radio_state(dev, &value);
Johannes Berg19d337d2009-06-02 13:01:37 +0200832 if (hci_result != HCI_SUCCESS) {
833 /* Can't do anything useful */
834 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -0700835 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200836 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400837
838 new_rfk_state = value;
839
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400840 mutex_unlock(&dev->mutex);
841
Johannes Berg19d337d2009-06-02 13:01:37 +0200842 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
843 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400844}
845
Johannes Berg19d337d2009-06-02 13:01:37 +0200846static const struct rfkill_ops toshiba_rfk_ops = {
847 .set_block = bt_rfkill_set_block,
848 .poll = bt_rfkill_poll,
849};
850
Akio Idehara121b7b02012-04-06 01:46:43 +0900851static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
852{
853 u32 hci_result;
854 u32 status;
855
856 hci_read1(dev, HCI_TR_BACKLIGHT, &status, &hci_result);
857 *enabled = !status;
858 return hci_result == HCI_SUCCESS ? 0 : -EIO;
859}
860
861static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
862{
863 u32 hci_result;
864 u32 value = !enable;
865
866 hci_write1(dev, HCI_TR_BACKLIGHT, value, &hci_result);
867 return hci_result == HCI_SUCCESS ? 0 : -EIO;
868}
869
Len Brown4be44fc2005-08-05 00:44:28 -0400870static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Seth Forshee62cce752012-04-19 11:23:50 -0500872static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 u32 hci_result;
875 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +0900876 int brightness = 0;
877
878 if (dev->tr_backlight_supported) {
879 bool enabled;
880 int ret = get_tr_backlight_status(dev, &enabled);
881 if (ret)
882 return ret;
883 if (enabled)
884 return 0;
885 brightness++;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Seth Forshee135740d2011-09-20 16:55:49 -0500888 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500889 if (hci_result == HCI_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +0900890 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500891
892 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700893}
894
Seth Forshee62cce752012-04-19 11:23:50 -0500895static int get_lcd_brightness(struct backlight_device *bd)
896{
897 struct toshiba_acpi_dev *dev = bl_get_data(bd);
898 return __get_lcd_brightness(dev);
899}
900
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800901static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -0700902{
Seth Forshee135740d2011-09-20 16:55:49 -0500903 struct toshiba_acpi_dev *dev = m->private;
904 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +0900905 int levels;
Holger Machtc9263552006-10-20 14:30:29 -0700906
Seth Forshee135740d2011-09-20 16:55:49 -0500907 if (!dev->backlight_dev)
908 return -ENODEV;
909
Akio Idehara121b7b02012-04-06 01:46:43 +0900910 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -0500911 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -0700912 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800913 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +0900914 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500918 pr_err("Error reading LCD brightness\n");
919 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800920}
921
922static int lcd_proc_open(struct inode *inode, struct file *file)
923{
Al Virod9dda782013-03-31 18:16:14 -0400924 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Seth Forshee62cce752012-04-19 11:23:50 -0500927static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -0700928{
Azael Avalos258c5902014-09-29 20:40:07 -0600929 u32 in[TCI_WORDS] = { HCI_SET, HCI_LCD_BRIGHTNESS, 0, 0, 0, 0 };
930 u32 out[TCI_WORDS];
Azael Avalosf6aac652014-08-04 09:21:01 -0600931 acpi_status status;
Holger Machtc9263552006-10-20 14:30:29 -0700932
Akio Idehara121b7b02012-04-06 01:46:43 +0900933 if (dev->tr_backlight_supported) {
934 bool enable = !value;
935 int ret = set_tr_backlight_status(dev, enable);
936 if (ret)
937 return ret;
938 if (value)
939 value--;
940 }
941
Azael Avalosf6aac652014-08-04 09:21:01 -0600942 in[2] = value << HCI_LCD_BRIGHTNESS_SHIFT;
Azael Avalos258c5902014-09-29 20:40:07 -0600943 status = tci_raw(dev, in, out);
Azael Avalosf6aac652014-08-04 09:21:01 -0600944 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
945 pr_err("ACPI call to set brightness failed");
946 return -EIO;
947 }
948 /* Extra check for "incomplete" backlight method, where the AML code
949 * doesn't check for HCI_SET or HCI_GET and returns HCI_SUCCESS,
950 * the actual brightness, and in some cases the max brightness.
951 */
952 if (out[2] > 0 || out[3] == 0xE000)
953 return -ENODEV;
954
955 return out[0] == HCI_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700956}
957
958static int set_lcd_status(struct backlight_device *bd)
959{
Seth Forshee135740d2011-09-20 16:55:49 -0500960 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Seth Forshee62cce752012-04-19 11:23:50 -0500961 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -0700962}
963
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800964static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
965 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Al Virod9dda782013-03-31 18:16:14 -0400967 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800968 char cmd[42];
969 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800971 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +0900972 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800974 len = min(count, sizeof(cmd) - 1);
975 if (copy_from_user(cmd, buf, len))
976 return -EFAULT;
977 cmd[len] = '\0';
978
979 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +0900980 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -0500981 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800982 if (ret == 0)
983 ret = count;
984 } else {
Holger Machtc9263552006-10-20 14:30:29 -0700985 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800986 }
Holger Machtc9263552006-10-20 14:30:29 -0700987 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800990static const struct file_operations lcd_proc_fops = {
991 .owner = THIS_MODULE,
992 .open = lcd_proc_open,
993 .read = seq_read,
994 .llseek = seq_lseek,
995 .release = single_release,
996 .write = lcd_proc_write,
997};
998
Seth Forshee36d03f92011-09-20 16:55:53 -0500999static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1000{
1001 u32 hci_result;
1002
1003 hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
1004 return hci_result == HCI_SUCCESS ? 0 : -EIO;
1005}
1006
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001007static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Seth Forshee135740d2011-09-20 16:55:49 -05001009 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001011 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Seth Forshee36d03f92011-09-20 16:55:53 -05001013 ret = get_video_status(dev, &value);
1014 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1016 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001017 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001018 seq_printf(m, "lcd_out: %d\n", is_lcd);
1019 seq_printf(m, "crt_out: %d\n", is_crt);
1020 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022
Seth Forshee36d03f92011-09-20 16:55:53 -05001023 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001026static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Al Virod9dda782013-03-31 18:16:14 -04001028 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001029}
1030
1031static ssize_t video_proc_write(struct file *file, const char __user *buf,
1032 size_t count, loff_t *pos)
1033{
Al Virod9dda782013-03-31 18:16:14 -04001034 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001035 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001036 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 int value;
1038 int remain = count;
1039 int lcd_out = -1;
1040 int crt_out = -1;
1041 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001042 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001044 cmd = kmalloc(count + 1, GFP_KERNEL);
1045 if (!cmd)
1046 return -ENOMEM;
1047 if (copy_from_user(cmd, buf, count)) {
1048 kfree(cmd);
1049 return -EFAULT;
1050 }
1051 cmd[count] = '\0';
1052
1053 buffer = cmd;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* scan expression. Multiple expressions may be delimited with ;
1056 *
1057 * NOTE: to keep scanning simple, invalid fields are ignored
1058 */
1059 while (remain) {
1060 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1061 lcd_out = value & 1;
1062 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1063 crt_out = value & 1;
1064 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1065 tv_out = value & 1;
1066 /* advance to one character past the next ; */
1067 do {
1068 ++buffer;
1069 --remain;
1070 }
Len Brown4be44fc2005-08-05 00:44:28 -04001071 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001074 kfree(cmd);
1075
Seth Forshee36d03f92011-09-20 16:55:53 -05001076 ret = get_video_status(dev, &video_out);
1077 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001078 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (lcd_out != -1)
1080 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1081 if (crt_out != -1)
1082 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1083 if (tv_out != -1)
1084 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1085 /* To avoid unnecessary video disruption, only write the new
1086 * video setting if something changed. */
1087 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001088 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001091 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001094static const struct file_operations video_proc_fops = {
1095 .owner = THIS_MODULE,
1096 .open = video_proc_open,
1097 .read = seq_read,
1098 .llseek = seq_lseek,
1099 .release = single_release,
1100 .write = video_proc_write,
1101};
1102
Seth Forshee36d03f92011-09-20 16:55:53 -05001103static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1104{
1105 u32 hci_result;
1106
1107 hci_read1(dev, HCI_FAN, status, &hci_result);
1108 return hci_result == HCI_SUCCESS ? 0 : -EIO;
1109}
1110
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001111static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Seth Forshee135740d2011-09-20 16:55:49 -05001113 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001114 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 u32 value;
1116
Seth Forshee36d03f92011-09-20 16:55:53 -05001117 ret = get_fan_status(dev, &value);
1118 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001119 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001120 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122
Seth Forshee36d03f92011-09-20 16:55:53 -05001123 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001126static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Al Virod9dda782013-03-31 18:16:14 -04001128 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001129}
1130
1131static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1132 size_t count, loff_t *pos)
1133{
Al Virod9dda782013-03-31 18:16:14 -04001134 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001135 char cmd[42];
1136 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int value;
1138 u32 hci_result;
1139
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001140 len = min(count, sizeof(cmd) - 1);
1141 if (copy_from_user(cmd, buf, len))
1142 return -EFAULT;
1143 cmd[len] = '\0';
1144
1145 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001146 value >= 0 && value <= 1) {
Seth Forshee135740d2011-09-20 16:55:49 -05001147 hci_write1(dev, HCI_FAN, value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (hci_result != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001149 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 else
Seth Forshee135740d2011-09-20 16:55:49 -05001151 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 } else {
1153 return -EINVAL;
1154 }
1155
1156 return count;
1157}
1158
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001159static const struct file_operations fan_proc_fops = {
1160 .owner = THIS_MODULE,
1161 .open = fan_proc_open,
1162 .read = seq_read,
1163 .llseek = seq_lseek,
1164 .release = single_release,
1165 .write = fan_proc_write,
1166};
1167
1168static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Seth Forshee135740d2011-09-20 16:55:49 -05001170 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 u32 hci_result;
1172 u32 value;
1173
Seth Forshee11948b92011-11-16 17:37:45 -06001174 if (!dev->key_event_valid && dev->system_event_supported) {
Seth Forshee135740d2011-09-20 16:55:49 -05001175 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (hci_result == HCI_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001177 dev->key_event_valid = 1;
1178 dev->last_key_event = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 } else if (hci_result == HCI_EMPTY) {
1180 /* better luck next time */
1181 } else if (hci_result == HCI_NOT_SUPPORTED) {
1182 /* This is a workaround for an unresolved issue on
1183 * some machines where system events sporadically
1184 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -05001185 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -07001186 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001188 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001189 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191 }
1192
Seth Forshee135740d2011-09-20 16:55:49 -05001193 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1194 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001198static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Al Virod9dda782013-03-31 18:16:14 -04001200 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001201}
1202
1203static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1204 size_t count, loff_t *pos)
1205{
Al Virod9dda782013-03-31 18:16:14 -04001206 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001207 char cmd[42];
1208 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int value;
1210
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001211 len = min(count, sizeof(cmd) - 1);
1212 if (copy_from_user(cmd, buf, len))
1213 return -EFAULT;
1214 cmd[len] = '\0';
1215
1216 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -05001217 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 } else {
1219 return -EINVAL;
1220 }
1221
1222 return count;
1223}
1224
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001225static const struct file_operations keys_proc_fops = {
1226 .owner = THIS_MODULE,
1227 .open = keys_proc_open,
1228 .read = seq_read,
1229 .llseek = seq_lseek,
1230 .release = single_release,
1231 .write = keys_proc_write,
1232};
1233
1234static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001236 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1237 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001241static int version_proc_open(struct inode *inode, struct file *file)
1242{
Al Virod9dda782013-03-31 18:16:14 -04001243 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001244}
1245
1246static const struct file_operations version_proc_fops = {
1247 .owner = THIS_MODULE,
1248 .open = version_proc_open,
1249 .read = seq_read,
1250 .llseek = seq_lseek,
1251 .release = single_release,
1252};
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/* proc and module init
1255 */
1256
1257#define PROC_TOSHIBA "toshiba"
1258
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001259static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Seth Forshee36d03f92011-09-20 16:55:53 -05001261 if (dev->backlight_dev)
1262 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1263 &lcd_proc_fops, dev);
1264 if (dev->video_supported)
1265 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1266 &video_proc_fops, dev);
1267 if (dev->fan_supported)
1268 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1269 &fan_proc_fops, dev);
1270 if (dev->hotkey_dev)
1271 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1272 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001273 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1274 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Seth Forshee36d03f92011-09-20 16:55:53 -05001277static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Seth Forshee36d03f92011-09-20 16:55:53 -05001279 if (dev->backlight_dev)
1280 remove_proc_entry("lcd", toshiba_proc_dir);
1281 if (dev->video_supported)
1282 remove_proc_entry("video", toshiba_proc_dir);
1283 if (dev->fan_supported)
1284 remove_proc_entry("fan", toshiba_proc_dir);
1285 if (dev->hotkey_dev)
1286 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001287 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Lionel Debrouxacc24722010-11-16 14:14:02 +01001290static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001291 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001292 .get_brightness = get_lcd_brightness,
1293 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001294};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001295
Azael Avalos360f0f32014-03-25 20:38:31 -06001296/*
1297 * Sysfs files
1298 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001299static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1300 struct device_attribute *attr,
1301 const char *buf, size_t count);
1302static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1303 struct device_attribute *attr,
1304 char *buf);
1305static ssize_t toshiba_kbd_type_show(struct device *dev,
1306 struct device_attribute *attr,
1307 char *buf);
1308static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1309 struct device_attribute *attr,
1310 char *buf);
1311static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1312 struct device_attribute *attr,
1313 const char *buf, size_t count);
1314static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1315 struct device_attribute *attr,
1316 char *buf);
1317static ssize_t toshiba_touchpad_store(struct device *dev,
1318 struct device_attribute *attr,
1319 const char *buf, size_t count);
1320static ssize_t toshiba_touchpad_show(struct device *dev,
1321 struct device_attribute *attr,
1322 char *buf);
1323static ssize_t toshiba_position_show(struct device *dev,
1324 struct device_attribute *attr,
1325 char *buf);
1326
1327static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1328 toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1329static DEVICE_ATTR(kbd_type, S_IRUGO, toshiba_kbd_type_show, NULL);
1330static DEVICE_ATTR(available_kbd_modes, S_IRUGO,
1331 toshiba_available_kbd_modes_show, NULL);
1332static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1333 toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
1334static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1335 toshiba_touchpad_show, toshiba_touchpad_store);
1336static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
1337
1338static struct attribute *toshiba_attributes[] = {
1339 &dev_attr_kbd_backlight_mode.attr,
1340 &dev_attr_kbd_type.attr,
1341 &dev_attr_available_kbd_modes.attr,
1342 &dev_attr_kbd_backlight_timeout.attr,
1343 &dev_attr_touchpad.attr,
1344 &dev_attr_position.attr,
1345 NULL,
1346};
1347
1348static umode_t toshiba_sysfs_is_visible(struct kobject *,
1349 struct attribute *, int);
1350
1351static struct attribute_group toshiba_attr_group = {
1352 .is_visible = toshiba_sysfs_is_visible,
1353 .attrs = toshiba_attributes,
1354};
Azael Avalos360f0f32014-03-25 20:38:31 -06001355
1356static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1357 struct device_attribute *attr,
1358 const char *buf, size_t count)
1359{
1360 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001361 int mode;
1362 int time;
1363 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001364
Dan Carpenteraeaac092014-09-03 14:44:37 +03001365
1366 ret = kstrtoint(buf, 0, &mode);
1367 if (ret)
1368 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001369
1370 /* Check for supported modes depending on keyboard backlight type */
1371 if (toshiba->kbd_type == 1) {
1372 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1373 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1374 return -EINVAL;
1375 } else if (toshiba->kbd_type == 2) {
1376 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1377 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1378 mode != SCI_KBD_MODE_OFF)
1379 return -EINVAL;
1380 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001381
1382 /* Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001383 * Auto - KBD backlight turns off automatically in given time
1384 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001385 * ON - KBD backlight is always on
1386 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001387 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001388
1389 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001390 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001391 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001392 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001393
1394 /* OR the "base time" to the actual method format */
1395 if (toshiba->kbd_type == 1) {
1396 /* Type 1 requires the current mode */
1397 time |= toshiba->kbd_mode;
1398 } else if (toshiba->kbd_type == 2) {
1399 /* Type 2 requires the desired mode */
1400 time |= mode;
1401 }
1402
Dan Carpenteraeaac092014-09-03 14:44:37 +03001403 ret = toshiba_kbd_illum_status_set(toshiba, time);
1404 if (ret)
1405 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001406
1407 /* Update sysfs entries on successful mode change*/
1408 ret = sysfs_update_group(&toshiba->acpi_dev->dev.kobj,
1409 &toshiba_attr_group);
1410 if (ret)
1411 return ret;
1412
Azael Avalos360f0f32014-03-25 20:38:31 -06001413 toshiba->kbd_mode = mode;
1414 }
1415
1416 return count;
1417}
1418
1419static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1420 struct device_attribute *attr,
1421 char *buf)
1422{
1423 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1424 u32 time;
1425
1426 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1427 return -EIO;
1428
Azael Avalos93f8c162014-09-12 18:50:36 -06001429 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1430}
1431
1432static ssize_t toshiba_kbd_type_show(struct device *dev,
1433 struct device_attribute *attr,
1434 char *buf)
1435{
1436 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1437
1438 return sprintf(buf, "%d\n", toshiba->kbd_type);
1439}
1440
1441static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1442 struct device_attribute *attr,
1443 char *buf)
1444{
1445 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1446
1447 if (toshiba->kbd_type == 1)
1448 return sprintf(buf, "%x %x\n",
1449 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1450
1451 return sprintf(buf, "%x %x %x\n",
1452 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001453}
1454
1455static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1456 struct device_attribute *attr,
1457 const char *buf, size_t count)
1458{
1459 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1460 int time = -1;
1461
1462 if (sscanf(buf, "%i", &time) != 1 && (time < 0 || time > 60))
1463 return -EINVAL;
1464
1465 /* Set the Keyboard Backlight Timeout: 0-60 seconds */
1466 if (time != -1 && toshiba->kbd_time != time) {
1467 time = time << HCI_MISC_SHIFT;
1468 time = (toshiba->kbd_mode == SCI_KBD_MODE_AUTO) ?
1469 time + 1 : time + 2;
1470 if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1471 return -EIO;
1472 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1473 }
1474
1475 return count;
1476}
1477
1478static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1479 struct device_attribute *attr,
1480 char *buf)
1481{
1482 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1483 u32 time;
1484
1485 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1486 return -EIO;
1487
1488 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1489}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001490
Azael Avalos9d8658a2014-03-25 20:38:32 -06001491static ssize_t toshiba_touchpad_store(struct device *dev,
1492 struct device_attribute *attr,
1493 const char *buf, size_t count)
1494{
1495 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1496 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001497 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001498
1499 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001500 ret = kstrtoint(buf, 0, &state);
1501 if (ret)
1502 return ret;
1503 if (state != 0 && state != 1)
1504 return -EINVAL;
1505
1506 ret = toshiba_touchpad_set(toshiba, state);
1507 if (ret)
1508 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001509
1510 return count;
1511}
1512
1513static ssize_t toshiba_touchpad_show(struct device *dev,
1514 struct device_attribute *attr, char *buf)
1515{
1516 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1517 u32 state;
1518 int ret;
1519
1520 ret = toshiba_touchpad_get(toshiba, &state);
1521 if (ret < 0)
1522 return ret;
1523
1524 return sprintf(buf, "%i\n", state);
1525}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001526
Azael Avalos5a2813e2014-03-25 20:38:34 -06001527static ssize_t toshiba_position_show(struct device *dev,
1528 struct device_attribute *attr, char *buf)
1529{
1530 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1531 u32 xyval, zval, tmp;
1532 u16 x, y, z;
1533 int ret;
1534
1535 xyval = zval = 0;
1536 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1537 if (ret < 0)
1538 return ret;
1539
1540 x = xyval & HCI_ACCEL_MASK;
1541 tmp = xyval >> HCI_MISC_SHIFT;
1542 y = tmp & HCI_ACCEL_MASK;
1543 z = zval & HCI_ACCEL_MASK;
1544
1545 return sprintf(buf, "%d %d %d\n", x, y, z);
1546}
Azael Avalos360f0f32014-03-25 20:38:31 -06001547
Azael Avalos360f0f32014-03-25 20:38:31 -06001548static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
1549 struct attribute *attr, int idx)
1550{
1551 struct device *dev = container_of(kobj, struct device, kobj);
1552 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
1553 bool exists = true;
1554
1555 if (attr == &dev_attr_kbd_backlight_mode.attr)
1556 exists = (drv->kbd_illum_supported) ? true : false;
1557 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
1558 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001559 else if (attr == &dev_attr_touchpad.attr)
1560 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06001561 else if (attr == &dev_attr_position.attr)
1562 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06001563
1564 return exists ? attr->mode : 0;
1565}
1566
Seth Forshee29cd2932012-01-18 13:44:09 -06001567static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
1568 struct serio *port)
1569{
1570 if (str & 0x20)
1571 return false;
1572
1573 if (unlikely(data == 0xe0))
1574 return false;
1575
1576 if ((data & 0x7f) == TOS1900_FN_SCAN) {
1577 schedule_work(&toshiba_acpi->hotkey_work);
1578 return true;
1579 }
1580
1581 return false;
1582}
1583
1584static void toshiba_acpi_hotkey_work(struct work_struct *work)
1585{
1586 acpi_handle ec_handle = ec_get_handle();
1587 acpi_status status;
1588
1589 if (!ec_handle)
1590 return;
1591
1592 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
1593 if (ACPI_FAILURE(status))
1594 pr_err("ACPI NTFY method execution failed\n");
1595}
1596
1597/*
1598 * Returns hotkey scancode, or < 0 on failure.
1599 */
1600static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
1601{
Zhang Rui74facaf2013-09-03 08:32:15 +08001602 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06001603 acpi_status status;
1604
Zhang Rui74facaf2013-09-03 08:32:15 +08001605 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
1606 NULL, &value);
1607 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06001608 pr_err("ACPI INFO method execution failed\n");
1609 return -EIO;
1610 }
1611
Zhang Rui74facaf2013-09-03 08:32:15 +08001612 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06001613}
1614
1615static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
1616 int scancode)
1617{
1618 if (scancode == 0x100)
1619 return;
1620
1621 /* act on key press; ignore key release */
1622 if (scancode & 0x80)
1623 return;
1624
1625 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
1626 pr_info("Unknown key %x\n", scancode);
1627}
1628
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001629static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001630{
Seth Forshee135740d2011-09-20 16:55:49 -05001631 acpi_status status;
Zhang Ruie2e19602013-09-03 08:32:06 +08001632 acpi_handle ec_handle;
Seth Forshee135740d2011-09-20 16:55:49 -05001633 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06001634 u32 hci_result;
Takashi Iwaife808bfb2014-04-29 15:15:38 +02001635 const struct key_entry *keymap = toshiba_acpi_keymap;
Seth Forshee135740d2011-09-20 16:55:49 -05001636
Seth Forshee135740d2011-09-20 16:55:49 -05001637 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07001638 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001639 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05001640
1641 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05001642 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05001643 dev->hotkey_dev->id.bustype = BUS_HOST;
1644
Takashi Iwaife808bfb2014-04-29 15:15:38 +02001645 if (dmi_check_system(toshiba_alt_keymap_dmi))
1646 keymap = toshiba_acpi_alt_keymap;
1647 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05001648 if (error)
1649 goto err_free_dev;
1650
Seth Forshee29cd2932012-01-18 13:44:09 -06001651 /*
1652 * For some machines the SCI responsible for providing hotkey
1653 * notification doesn't fire. We can trigger the notification
1654 * whenever the Fn key is pressed using the NTFY method, if
1655 * supported, so if it's present set up an i8042 key filter
1656 * for this purpose.
1657 */
1658 status = AE_ERROR;
1659 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08001660 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06001661 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
1662
1663 error = i8042_install_filter(toshiba_acpi_i8042_filter);
1664 if (error) {
1665 pr_err("Error installing key filter\n");
1666 goto err_free_keymap;
1667 }
1668
1669 dev->ntfy_supported = 1;
1670 }
1671
1672 /*
1673 * Determine hotkey query interface. Prefer using the INFO
1674 * method when it is available.
1675 */
Zhang Ruie2e19602013-09-03 08:32:06 +08001676 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06001677 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08001678 else {
Seth Forshee29cd2932012-01-18 13:44:09 -06001679 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
1680 if (hci_result == HCI_SUCCESS)
1681 dev->system_event_supported = 1;
1682 }
1683
1684 if (!dev->info_supported && !dev->system_event_supported) {
1685 pr_warn("No hotkey query interface found\n");
1686 goto err_remove_filter;
1687 }
1688
Seth Forshee6e02cc72011-09-20 16:55:51 -05001689 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05001690 if (ACPI_FAILURE(status)) {
1691 pr_info("Unable to enable hotkeys\n");
1692 error = -ENODEV;
Seth Forshee29cd2932012-01-18 13:44:09 -06001693 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05001694 }
1695
1696 error = input_register_device(dev->hotkey_dev);
1697 if (error) {
1698 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06001699 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05001700 }
1701
Seth Forshee29cd2932012-01-18 13:44:09 -06001702 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result);
Seth Forshee135740d2011-09-20 16:55:49 -05001703 return 0;
1704
Seth Forshee29cd2932012-01-18 13:44:09 -06001705 err_remove_filter:
1706 if (dev->ntfy_supported)
1707 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05001708 err_free_keymap:
1709 sparse_keymap_free(dev->hotkey_dev);
1710 err_free_dev:
1711 input_free_device(dev->hotkey_dev);
1712 dev->hotkey_dev = NULL;
1713 return error;
1714}
1715
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001716static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05001717{
1718 struct backlight_properties props;
1719 int brightness;
1720 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001721 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05001722
1723 /*
1724 * Some machines don't support the backlight methods at all, and
1725 * others support it read-only. Either of these is pretty useless,
1726 * so only register the backlight device if the backlight method
1727 * supports both reads and writes.
1728 */
1729 brightness = __get_lcd_brightness(dev);
1730 if (brightness < 0)
1731 return 0;
1732 ret = set_lcd_brightness(dev, brightness);
1733 if (ret) {
1734 pr_debug("Backlight method is read-only, disabling backlight support\n");
1735 return 0;
1736 }
1737
Akio Idehara121b7b02012-04-06 01:46:43 +09001738 /* Determine whether or not BIOS supports transflective backlight */
1739 ret = get_tr_backlight_status(dev, &enabled);
1740 dev->tr_backlight_supported = !ret;
1741
Matthew Garrett53039f22012-06-01 11:02:36 -04001742 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05001743 props.type = BACKLIGHT_PLATFORM;
1744 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001745
Akio Idehara121b7b02012-04-06 01:46:43 +09001746 /* adding an extra level and having 0 change to transflective mode */
1747 if (dev->tr_backlight_supported)
1748 props.max_brightness++;
1749
Seth Forshee62cce752012-04-19 11:23:50 -05001750 dev->backlight_dev = backlight_device_register("toshiba",
1751 &dev->acpi_dev->dev,
1752 dev,
1753 &toshiba_backlight_data,
1754 &props);
1755 if (IS_ERR(dev->backlight_dev)) {
1756 ret = PTR_ERR(dev->backlight_dev);
1757 pr_err("Could not register toshiba backlight device\n");
1758 dev->backlight_dev = NULL;
1759 return ret;
1760 }
1761
1762 dev->backlight_dev->props.brightness = brightness;
1763 return 0;
1764}
1765
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001766static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001767{
1768 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1769
Seth Forshee36d03f92011-09-20 16:55:53 -05001770 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001771
Azael Avalos360f0f32014-03-25 20:38:31 -06001772 if (dev->sysfs_created)
1773 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
1774 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05001775
Seth Forshee29cd2932012-01-18 13:44:09 -06001776 if (dev->ntfy_supported) {
1777 i8042_remove_filter(toshiba_acpi_i8042_filter);
1778 cancel_work_sync(&dev->hotkey_work);
1779 }
1780
Seth Forshee135740d2011-09-20 16:55:49 -05001781 if (dev->hotkey_dev) {
1782 input_unregister_device(dev->hotkey_dev);
1783 sparse_keymap_free(dev->hotkey_dev);
1784 }
1785
1786 if (dev->bt_rfk) {
1787 rfkill_unregister(dev->bt_rfk);
1788 rfkill_destroy(dev->bt_rfk);
1789 }
1790
1791 if (dev->backlight_dev)
1792 backlight_device_unregister(dev->backlight_dev);
1793
Seth Forshee36d03f92011-09-20 16:55:53 -05001794 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05001795 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001796
Azael Avalos360f0f32014-03-25 20:38:31 -06001797 if (dev->kbd_led_registered)
1798 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001799
Azael Avalosdef6c4e2014-03-25 20:38:33 -06001800 if (dev->eco_supported)
1801 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05001802
Seth Forshee29cd2932012-01-18 13:44:09 -06001803 if (toshiba_acpi)
1804 toshiba_acpi = NULL;
1805
Seth Forshee135740d2011-09-20 16:55:49 -05001806 kfree(dev);
1807
1808 return 0;
1809}
1810
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001811static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05001812{
Zhang Ruie2e19602013-09-03 08:32:06 +08001813 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05001814 return "GHCI";
1815
Zhang Ruie2e19602013-09-03 08:32:06 +08001816 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05001817 return "SPFC";
1818
1819 return NULL;
1820}
1821
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001822static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001823{
1824 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001825 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05001826 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05001827 bool bt_present;
1828 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05001829
Seth Forshee29cd2932012-01-18 13:44:09 -06001830 if (toshiba_acpi)
1831 return -EBUSY;
1832
Seth Forshee135740d2011-09-20 16:55:49 -05001833 pr_info("Toshiba Laptop ACPI Extras version %s\n",
1834 TOSHIBA_ACPI_VERSION);
1835
Seth Forsheea540d6b2011-09-20 16:55:52 -05001836 hci_method = find_hci_method(acpi_dev->handle);
1837 if (!hci_method) {
1838 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05001839 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001840 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05001841
Seth Forshee135740d2011-09-20 16:55:49 -05001842 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1843 if (!dev)
1844 return -ENOMEM;
1845 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001846 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05001847 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06001848 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001849
Seth Forshee6e02cc72011-09-20 16:55:51 -05001850 if (toshiba_acpi_setup_keyboard(dev))
1851 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05001852
1853 mutex_init(&dev->mutex);
1854
Seth Forshee62cce752012-04-19 11:23:50 -05001855 ret = toshiba_acpi_setup_backlight(dev);
1856 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05001857 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05001858
1859 /* Register rfkill switch for Bluetooth */
1860 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
1861 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
1862 &acpi_dev->dev,
1863 RFKILL_TYPE_BLUETOOTH,
1864 &toshiba_rfk_ops,
1865 dev);
1866 if (!dev->bt_rfk) {
1867 pr_err("unable to allocate rfkill device\n");
1868 ret = -ENOMEM;
1869 goto error;
1870 }
1871
1872 ret = rfkill_register(dev->bt_rfk);
1873 if (ret) {
1874 pr_err("unable to register rfkill device\n");
1875 rfkill_destroy(dev->bt_rfk);
1876 goto error;
1877 }
1878 }
1879
1880 if (toshiba_illumination_available(dev)) {
1881 dev->led_dev.name = "toshiba::illumination";
1882 dev->led_dev.max_brightness = 1;
1883 dev->led_dev.brightness_set = toshiba_illumination_set;
1884 dev->led_dev.brightness_get = toshiba_illumination_get;
1885 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05001886 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05001887 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001888
Azael Avalosdef6c4e2014-03-25 20:38:33 -06001889 if (toshiba_eco_mode_available(dev)) {
1890 dev->eco_led.name = "toshiba::eco_mode";
1891 dev->eco_led.max_brightness = 1;
1892 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
1893 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
1894 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
1895 dev->eco_supported = 1;
1896 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001897
Azael Avalos93f8c162014-09-12 18:50:36 -06001898 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06001899 /*
1900 * Only register the LED if KBD illumination is supported
1901 * and the keyboard backlight operation mode is set to FN-Z
1902 */
1903 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
1904 dev->kbd_led.name = "toshiba::kbd_backlight";
1905 dev->kbd_led.max_brightness = 1;
1906 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
1907 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
1908 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
1909 dev->kbd_led_registered = 1;
1910 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001911
Azael Avalos9d8658a2014-03-25 20:38:32 -06001912 ret = toshiba_touchpad_get(dev, &dummy);
1913 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001914
Azael Avalos5a2813e2014-03-25 20:38:34 -06001915 ret = toshiba_accelerometer_supported(dev);
1916 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05001917
Seth Forshee36d03f92011-09-20 16:55:53 -05001918 /* Determine whether or not BIOS supports fan and video interfaces */
1919
1920 ret = get_video_status(dev, &dummy);
1921 dev->video_supported = !ret;
1922
1923 ret = get_fan_status(dev, &dummy);
1924 dev->fan_supported = !ret;
1925
Azael Avalos360f0f32014-03-25 20:38:31 -06001926 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
1927 &toshiba_attr_group);
1928 if (ret) {
1929 dev->sysfs_created = 0;
1930 goto error;
1931 }
1932 dev->sysfs_created = !ret;
1933
Seth Forshee36d03f92011-09-20 16:55:53 -05001934 create_toshiba_proc_entries(dev);
1935
Seth Forshee29cd2932012-01-18 13:44:09 -06001936 toshiba_acpi = dev;
1937
Seth Forshee135740d2011-09-20 16:55:49 -05001938 return 0;
1939
1940error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001941 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001942 return ret;
1943}
1944
1945static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1946{
1947 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001948 u32 hci_result, value;
Seth Forshee11948b92011-11-16 17:37:45 -06001949 int retries = 3;
Seth Forshee29cd2932012-01-18 13:44:09 -06001950 int scancode;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001951
Seth Forshee29cd2932012-01-18 13:44:09 -06001952 if (event != 0x80)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001953 return;
Seth Forshee11948b92011-11-16 17:37:45 -06001954
Seth Forshee29cd2932012-01-18 13:44:09 -06001955 if (dev->info_supported) {
1956 scancode = toshiba_acpi_query_hotkey(dev);
1957 if (scancode < 0)
1958 pr_err("Failed to query hotkey event\n");
1959 else if (scancode != 0)
1960 toshiba_acpi_report_hotkey(dev, scancode);
1961 } else if (dev->system_event_supported) {
1962 do {
1963 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1964 switch (hci_result) {
1965 case HCI_SUCCESS:
1966 toshiba_acpi_report_hotkey(dev, (int)value);
1967 break;
1968 case HCI_NOT_SUPPORTED:
1969 /*
1970 * This is a workaround for an unresolved
1971 * issue on some machines where system events
1972 * sporadically become disabled.
1973 */
1974 hci_write1(dev, HCI_SYSTEM_EVENT, 1,
1975 &hci_result);
1976 pr_notice("Re-enabled hotkeys\n");
1977 /* fall through */
1978 default:
1979 retries--;
1980 break;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001981 }
Seth Forshee29cd2932012-01-18 13:44:09 -06001982 } while (retries && hci_result != HCI_EMPTY);
1983 }
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001984}
1985
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02001986#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001987static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06001988{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001989 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06001990 u32 result;
1991
1992 if (dev->hotkey_dev)
1993 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result);
1994
1995 return 0;
1996}
1997
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001998static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06001999{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002000 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002001 u32 result;
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002002 acpi_status status;
Seth Forshee29cd2932012-01-18 13:44:09 -06002003
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002004 if (dev->hotkey_dev) {
2005 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB",
2006 NULL, NULL);
2007 if (ACPI_FAILURE(status))
2008 pr_info("Unable to re-enable hotkeys\n");
2009
Seth Forshee29cd2932012-01-18 13:44:09 -06002010 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result);
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002011 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002012
2013 return 0;
2014}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002015#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002016
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002017static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2018 toshiba_acpi_suspend, toshiba_acpi_resume);
2019
Seth Forshee135740d2011-09-20 16:55:49 -05002020static struct acpi_driver toshiba_acpi_driver = {
2021 .name = "Toshiba ACPI driver",
2022 .owner = THIS_MODULE,
2023 .ids = toshiba_device_ids,
2024 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2025 .ops = {
2026 .add = toshiba_acpi_add,
2027 .remove = toshiba_acpi_remove,
2028 .notify = toshiba_acpi_notify,
2029 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002030 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002031};
Holger Machtc9263552006-10-20 14:30:29 -07002032
Len Brown4be44fc2005-08-05 00:44:28 -04002033static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Seth Forshee135740d2011-09-20 16:55:49 -05002035 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Seth Forsheef11f9992012-01-18 13:44:11 -06002037 /*
2038 * Machines with this WMI guid aren't supported due to bugs in
2039 * their AML. This check relies on wmi initializing before
2040 * toshiba_acpi to guarantee guids have been identified.
2041 */
2042 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2043 return -ENODEV;
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2046 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002047 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002048 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050
Seth Forshee135740d2011-09-20 16:55:49 -05002051 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2052 if (ret) {
2053 pr_err("Failed to register ACPI driver: %d\n", ret);
2054 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002055 }
2056
Seth Forshee135740d2011-09-20 16:55:49 -05002057 return ret;
2058}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002059
Seth Forshee135740d2011-09-20 16:55:49 -05002060static void __exit toshiba_acpi_exit(void)
2061{
2062 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2063 if (toshiba_proc_dir)
2064 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067module_init(toshiba_acpi_init);
2068module_exit(toshiba_acpi_exit);