blob: edd8f3dad6b47a3d3ac303d137dac012811abddf [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
74/* Toshiba HCI interface definitions
75 *
76 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
77 * be uniform across all their models. Ideally we would just call
78 * dedicated ACPI methods instead of using this primitive interface.
79 * However the ACPI methods seem to be incomplete in some areas (for
80 * example they allow setting, but not reading, the LCD brightness value),
81 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040082 *
Azael Avalos84a62732014-03-25 20:38:29 -060083 * SCI stands for "System Configuration Interface" which aim is to
84 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86
87#define HCI_WORDS 6
88
89/* operations */
90#define HCI_SET 0xff00
91#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060092#define SCI_OPEN 0xf100
93#define SCI_CLOSE 0xf200
94#define SCI_GET 0xf300
95#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* return codes */
98#define HCI_SUCCESS 0x0000
99#define HCI_FAILURE 0x1000
100#define HCI_NOT_SUPPORTED 0x8000
101#define HCI_EMPTY 0x8c00
Azael Avalos5a2813e2014-03-25 20:38:34 -0600102#define HCI_DATA_NOT_AVAILABLE 0x8d20
103#define HCI_NOT_INITIALIZED 0x8d50
Azael Avalos84a62732014-03-25 20:38:29 -0600104#define SCI_OPEN_CLOSE_OK 0x0044
105#define SCI_ALREADY_OPEN 0x8100
106#define SCI_NOT_OPENED 0x8200
Azael Avalosfdb79082014-03-25 20:38:30 -0600107#define SCI_INPUT_DATA_ERROR 0x8300
Azael Avalos84a62732014-03-25 20:38:29 -0600108#define SCI_NOT_PRESENT 0x8600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/* registers */
111#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900112#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define HCI_SYSTEM_EVENT 0x0016
114#define HCI_VIDEO_OUT 0x001c
115#define HCI_HOTKEY_EVENT 0x001e
116#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400117#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600118#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600119#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600120#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600121#define HCI_ACCELEROMETER2 0x00a6
Azael Avalosfdb79082014-03-25 20:38:30 -0600122#define SCI_ILLUMINATION 0x014e
Azael Avalos360f0f32014-03-25 20:38:31 -0600123#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos9d8658a2014-03-25 20:38:32 -0600124#define SCI_TOUCHPAD 0x050e
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600127#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600128#define HCI_HOTKEY_DISABLE 0x0b
129#define HCI_HOTKEY_ENABLE 0x09
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define HCI_LCD_BRIGHTNESS_BITS 3
131#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
132#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600133#define HCI_MISC_SHIFT 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define HCI_VIDEO_OUT_LCD 0x1
135#define HCI_VIDEO_OUT_CRT 0x2
136#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400137#define HCI_WIRELESS_KILL_SWITCH 0x01
138#define HCI_WIRELESS_BT_PRESENT 0x0f
139#define HCI_WIRELESS_BT_ATTACH 0x40
140#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600141#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600142#define SCI_KBD_MODE_FNZ 0x1
143#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600144#define SCI_KBD_MODE_ON 0x8
145#define SCI_KBD_MODE_OFF 0x10
146#define SCI_KBD_TIME_MAX 0x3c001a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Seth Forshee135740d2011-09-20 16:55:49 -0500148struct toshiba_acpi_dev {
149 struct acpi_device *acpi_dev;
150 const char *method_hci;
151 struct rfkill *bt_rfk;
152 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600153 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500154 struct backlight_device *backlight_dev;
155 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600156 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600157 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500158
Seth Forshee135740d2011-09-20 16:55:49 -0500159 int force_fan;
160 int last_key_event;
161 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600162 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600163 int kbd_mode;
164 int kbd_time;
Seth Forshee135740d2011-09-20 16:55:49 -0500165
Dan Carpenter592b7462012-01-15 14:28:20 +0300166 unsigned int illumination_supported:1;
167 unsigned int video_supported:1;
168 unsigned int fan_supported:1;
169 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600170 unsigned int ntfy_supported:1;
171 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900172 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600173 unsigned int kbd_illum_supported:1;
174 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600175 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600176 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600177 unsigned int accelerometer_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600178 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500179
Seth Forshee135740d2011-09-20 16:55:49 -0500180 struct mutex mutex;
181};
182
Seth Forshee29cd2932012-01-18 13:44:09 -0600183static struct toshiba_acpi_dev *toshiba_acpi;
184
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800185static const struct acpi_device_id toshiba_device_ids[] = {
186 {"TOS6200", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400187 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800188 {"TOS1900", 0},
189 {"", 0},
190};
191MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
192
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800193static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100194 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700195 { KE_KEY, 0x101, { KEY_MUTE } },
196 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
197 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600198 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600199 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
200 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700201 { KE_KEY, 0x13b, { KEY_COFFEE } },
202 { KE_KEY, 0x13c, { KEY_BATTERY } },
203 { KE_KEY, 0x13d, { KEY_SLEEP } },
204 { KE_KEY, 0x13e, { KEY_SUSPEND } },
205 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
206 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
207 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
208 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600209 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100210 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700211 { KE_KEY, 0xb05, { KEY_PROG2 } },
212 { KE_KEY, 0xb06, { KEY_WWW } },
213 { KE_KEY, 0xb07, { KEY_MAIL } },
214 { KE_KEY, 0xb30, { KEY_STOP } },
215 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
216 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
217 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
218 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600219 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
220 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
221 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
222 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
223 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700224 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500225};
226
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200227/* alternative keymap */
228static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
229 {
230 .matches = {
231 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
232 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
233 },
234 },
Azael Avalose6efad72014-08-04 09:21:02 -0600235 {
236 .matches = {
237 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
238 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
239 },
240 },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200241 {}
242};
243
244static const struct key_entry toshiba_acpi_alt_keymap[] = {
245 { KE_KEY, 0x157, { KEY_MUTE } },
246 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
247 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600248 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200249 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
250 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
251 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
252 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
253 { KE_KEY, 0x158, { KEY_WLAN } },
254 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
255 { KE_END, 0 },
256};
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/* utility
259 */
260
Len Brown4be44fc2005-08-05 00:44:28 -0400261static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 *word = (*word & ~mask) | (mask * value);
264}
265
266/* acpi interface wrappers
267 */
268
Len Brown4be44fc2005-08-05 00:44:28 -0400269static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 acpi_status status;
272
Zhang Rui619400d2013-09-03 08:31:56 +0800273 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500274 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/* Perform a raw HCI call. Here we don't care about input or output buffer
278 * format.
279 */
Seth Forshee135740d2011-09-20 16:55:49 -0500280static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
281 const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct acpi_object_list params;
284 union acpi_object in_objs[HCI_WORDS];
285 struct acpi_buffer results;
Len Brown4be44fc2005-08-05 00:44:28 -0400286 union acpi_object out_objs[HCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 acpi_status status;
288 int i;
289
290 params.count = HCI_WORDS;
291 params.pointer = in_objs;
292 for (i = 0; i < HCI_WORDS; ++i) {
293 in_objs[i].type = ACPI_TYPE_INTEGER;
294 in_objs[i].integer.value = in[i];
295 }
296
297 results.length = sizeof(out_objs);
298 results.pointer = out_objs;
299
Seth Forshee6e02cc72011-09-20 16:55:51 -0500300 status = acpi_evaluate_object(dev->acpi_dev->handle,
301 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400302 &results);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
304 for (i = 0; i < out_objs->package.count; ++i) {
305 out[i] = out_objs->package.elements[i].integer.value;
306 }
307 }
308
309 return status;
310}
311
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400312/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
314 * In addition to the ACPI status, the HCI system returns a result which
315 * may be useful (such as "not supported").
316 */
317
Seth Forshee135740d2011-09-20 16:55:49 -0500318static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
319 u32 in1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
322 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500323 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
325 return status;
326}
327
Seth Forshee135740d2011-09-20 16:55:49 -0500328static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
329 u32 *out1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
332 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500333 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *out1 = out[2];
335 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
336 return status;
337}
338
Seth Forshee135740d2011-09-20 16:55:49 -0500339static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
340 u32 in1, u32 in2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400341{
342 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
343 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500344 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400345 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
346 return status;
347}
348
Seth Forshee135740d2011-09-20 16:55:49 -0500349static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
350 u32 *out1, u32 *out2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400351{
352 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
353 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500354 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400355 *out1 = out[2];
356 *out2 = out[3];
357 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
358 return status;
359}
360
Azael Avalos84a62732014-03-25 20:38:29 -0600361/* common sci tasks
362 */
363
364static int sci_open(struct toshiba_acpi_dev *dev)
365{
366 u32 in[HCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
367 u32 out[HCI_WORDS];
368 acpi_status status;
369
370 status = hci_raw(dev, in, out);
371 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
372 pr_err("ACPI call to open SCI failed\n");
373 return 0;
374 }
375
376 if (out[0] == SCI_OPEN_CLOSE_OK) {
377 return 1;
378 } else if (out[0] == SCI_ALREADY_OPEN) {
379 pr_info("Toshiba SCI already opened\n");
380 return 1;
381 } else if (out[0] == SCI_NOT_PRESENT) {
382 pr_info("Toshiba SCI is not present\n");
383 }
384
385 return 0;
386}
387
388static void sci_close(struct toshiba_acpi_dev *dev)
389{
390 u32 in[HCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
391 u32 out[HCI_WORDS];
392 acpi_status status;
393
394 status = hci_raw(dev, in, out);
395 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
396 pr_err("ACPI call to close SCI failed\n");
397 return;
398 }
399
400 if (out[0] == SCI_OPEN_CLOSE_OK)
401 return;
402 else if (out[0] == SCI_NOT_OPENED)
403 pr_info("Toshiba SCI not opened\n");
404 else if (out[0] == SCI_NOT_PRESENT)
405 pr_info("Toshiba SCI is not present\n");
406}
407
408static acpi_status sci_read(struct toshiba_acpi_dev *dev, u32 reg,
409 u32 *out1, u32 *result)
410{
411 u32 in[HCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
412 u32 out[HCI_WORDS];
413 acpi_status status = hci_raw(dev, in, out);
414 *out1 = out[2];
415 *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
416 return status;
417}
418
419static acpi_status sci_write(struct toshiba_acpi_dev *dev, u32 reg,
420 u32 in1, u32 *result)
421{
422 u32 in[HCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
423 u32 out[HCI_WORDS];
424 acpi_status status = hci_raw(dev, in, out);
425 *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
426 return status;
427}
428
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200429/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500430static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200431{
Azael Avalosfdb79082014-03-25 20:38:30 -0600432 u32 in[HCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200433 u32 out[HCI_WORDS];
434 acpi_status status;
435
Azael Avalosfdb79082014-03-25 20:38:30 -0600436 if (!sci_open(dev))
437 return 0;
438
Seth Forshee135740d2011-09-20 16:55:49 -0500439 status = hci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600440 sci_close(dev);
441 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
442 pr_err("ACPI call to query Illumination support failed\n");
443 return 0;
Azael Avalos12962872014-09-05 11:14:04 -0600444 } else if (out[0] == HCI_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700445 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200446 return 0;
447 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600448
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200449 return 1;
450}
451
452static void toshiba_illumination_set(struct led_classdev *cdev,
453 enum led_brightness brightness)
454{
Seth Forshee135740d2011-09-20 16:55:49 -0500455 struct toshiba_acpi_dev *dev = container_of(cdev,
456 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600457 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200458 acpi_status status;
459
460 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600461 if (!sci_open(dev))
462 return;
463
464 /* Switch the illumination on/off */
465 state = brightness ? 1 : 0;
466 status = sci_write(dev, SCI_ILLUMINATION, state, &result);
467 sci_close(dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200468 if (ACPI_FAILURE(status)) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600469 pr_err("ACPI call for illumination failed\n");
470 return;
471 } else if (result == HCI_NOT_SUPPORTED) {
472 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200473 return;
474 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200475}
476
477static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
478{
Seth Forshee135740d2011-09-20 16:55:49 -0500479 struct toshiba_acpi_dev *dev = container_of(cdev,
480 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600481 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200482 acpi_status status;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200483
484 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600485 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200486 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200487
488 /* Check the illumination */
Azael Avalosfdb79082014-03-25 20:38:30 -0600489 status = sci_read(dev, SCI_ILLUMINATION, &state, &result);
490 sci_close(dev);
491 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
492 pr_err("ACPI call for illumination failed\n");
493 return LED_OFF;
494 } else if (result == HCI_NOT_SUPPORTED) {
495 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200496 return LED_OFF;
497 }
498
Azael Avalosfdb79082014-03-25 20:38:30 -0600499 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200500}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400501
Azael Avalos360f0f32014-03-25 20:38:31 -0600502/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600503static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
504{
505 u32 in[HCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
506 u32 out[HCI_WORDS];
507 acpi_status status;
508
509 if (!sci_open(dev))
510 return 0;
511
512 status = hci_raw(dev, in, out);
513 sci_close(dev);
514 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
515 pr_err("ACPI call to query kbd illumination support failed\n");
516 return 0;
517 } else if (out[0] == HCI_NOT_SUPPORTED) {
518 pr_info("Keyboard illumination not available\n");
519 return 0;
520 }
521
522 /* Check for keyboard backlight timeout max value,
523 * previous kbd backlight implementation set this to
524 * 0x3c0003, and now the new implementation set this
525 * to 0x3c001a, use this to distinguish between them
526 */
527 if (out[3] == SCI_KBD_TIME_MAX)
528 dev->kbd_type = 2;
529 else
530 dev->kbd_type = 1;
531 /* Get the current keyboard backlight mode */
532 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
533 /* Get the current time (1-60 seconds) */
534 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
535
536 return 1;
537}
538
Azael Avalos360f0f32014-03-25 20:38:31 -0600539static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
540{
541 u32 result;
542 acpi_status status;
543
544 if (!sci_open(dev))
545 return -EIO;
546
547 status = sci_write(dev, SCI_KBD_ILLUM_STATUS, time, &result);
548 sci_close(dev);
549 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
550 pr_err("ACPI call to set KBD backlight status failed\n");
551 return -EIO;
552 } else if (result == HCI_NOT_SUPPORTED) {
553 pr_info("Keyboard backlight status not supported\n");
554 return -ENODEV;
555 }
556
557 return 0;
558}
559
560static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
561{
562 u32 result;
563 acpi_status status;
564
565 if (!sci_open(dev))
566 return -EIO;
567
568 status = sci_read(dev, SCI_KBD_ILLUM_STATUS, time, &result);
569 sci_close(dev);
570 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
571 pr_err("ACPI call to get KBD backlight status failed\n");
572 return -EIO;
573 } else if (result == HCI_NOT_SUPPORTED) {
574 pr_info("Keyboard backlight status not supported\n");
575 return -ENODEV;
576 }
577
578 return 0;
579}
580
581static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
582{
583 struct toshiba_acpi_dev *dev = container_of(cdev,
584 struct toshiba_acpi_dev, kbd_led);
585 u32 state, result;
586 acpi_status status;
587
588 /* Check the keyboard backlight state */
589 status = hci_read1(dev, HCI_KBD_ILLUMINATION, &state, &result);
590 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
591 pr_err("ACPI call to get the keyboard backlight failed\n");
592 return LED_OFF;
593 } else if (result == HCI_NOT_SUPPORTED) {
594 pr_info("Keyboard backlight not supported\n");
595 return LED_OFF;
596 }
597
598 return state ? LED_FULL : LED_OFF;
599}
600
601static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
602 enum led_brightness brightness)
603{
604 struct toshiba_acpi_dev *dev = container_of(cdev,
605 struct toshiba_acpi_dev, kbd_led);
606 u32 state, result;
607 acpi_status status;
608
609 /* Set the keyboard backlight state */
610 state = brightness ? 1 : 0;
611 status = hci_write1(dev, HCI_KBD_ILLUMINATION, state, &result);
612 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
613 pr_err("ACPI call to set KBD Illumination mode failed\n");
614 return;
615 } else if (result == HCI_NOT_SUPPORTED) {
616 pr_info("Keyboard backlight not supported\n");
617 return;
618 }
619}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400620
Azael Avalos9d8658a2014-03-25 20:38:32 -0600621/* TouchPad support */
622static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
623{
624 u32 result;
625 acpi_status status;
626
627 if (!sci_open(dev))
628 return -EIO;
629
630 status = sci_write(dev, SCI_TOUCHPAD, state, &result);
631 sci_close(dev);
632 if (ACPI_FAILURE(status)) {
633 pr_err("ACPI call to set the touchpad failed\n");
634 return -EIO;
635 } else if (result == HCI_NOT_SUPPORTED) {
636 return -ENODEV;
637 }
638
639 return 0;
640}
641
642static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
643{
644 u32 result;
645 acpi_status status;
646
647 if (!sci_open(dev))
648 return -EIO;
649
650 status = sci_read(dev, SCI_TOUCHPAD, state, &result);
651 sci_close(dev);
652 if (ACPI_FAILURE(status)) {
653 pr_err("ACPI call to query the touchpad failed\n");
654 return -EIO;
655 } else if (result == HCI_NOT_SUPPORTED) {
656 return -ENODEV;
657 }
658
659 return 0;
660}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200661
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600662/* Eco Mode support */
663static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
664{
665 acpi_status status;
666 u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
667 u32 out[HCI_WORDS];
668
669 status = hci_raw(dev, in, out);
670 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
671 pr_info("ACPI call to get ECO led failed\n");
672 return 0;
673 }
674
675 return 1;
676}
677
678static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
679{
680 struct toshiba_acpi_dev *dev = container_of(cdev,
681 struct toshiba_acpi_dev, eco_led);
682 u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
683 u32 out[HCI_WORDS];
684 acpi_status status;
685
686 status = hci_raw(dev, in, out);
687 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
688 pr_err("ACPI call to get ECO led failed\n");
689 return LED_OFF;
690 }
691
692 return out[2] ? LED_FULL : LED_OFF;
693}
694
695static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
696 enum led_brightness brightness)
697{
698 struct toshiba_acpi_dev *dev = container_of(cdev,
699 struct toshiba_acpi_dev, eco_led);
700 u32 in[HCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
701 u32 out[HCI_WORDS];
702 acpi_status status;
703
704 /* Switch the Eco Mode led on/off */
705 in[2] = (brightness) ? 1 : 0;
706 status = hci_raw(dev, in, out);
707 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
708 pr_err("ACPI call to set ECO led failed\n");
709 return;
710 }
711}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400712
Azael Avalos5a2813e2014-03-25 20:38:34 -0600713/* Accelerometer support */
714static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
715{
716 u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
717 u32 out[HCI_WORDS];
718 acpi_status status;
719
720 /* Check if the accelerometer call exists,
721 * this call also serves as initialization
722 */
723 status = hci_raw(dev, in, out);
724 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
725 pr_err("ACPI call to query the accelerometer failed\n");
726 return -EIO;
727 } else if (out[0] == HCI_DATA_NOT_AVAILABLE ||
728 out[0] == HCI_NOT_INITIALIZED) {
729 pr_err("Accelerometer not initialized\n");
730 return -EIO;
731 } else if (out[0] == HCI_NOT_SUPPORTED) {
732 pr_info("Accelerometer not supported\n");
733 return -ENODEV;
734 }
735
736 return 0;
737}
738
739static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
740 u32 *xy, u32 *z)
741{
742 u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
743 u32 out[HCI_WORDS];
744 acpi_status status;
745
746 /* Check the Accelerometer status */
747 status = hci_raw(dev, in, out);
748 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
749 pr_err("ACPI call to query the accelerometer failed\n");
750 return -EIO;
751 }
752
753 *xy = out[2];
754 *z = out[4];
755
756 return 0;
757}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600758
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400759/* Bluetooth rfkill handlers */
760
Seth Forshee135740d2011-09-20 16:55:49 -0500761static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400762{
763 u32 hci_result;
764 u32 value, value2;
765
766 value = 0;
767 value2 = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500768 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400769 if (hci_result == HCI_SUCCESS)
770 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
771
772 return hci_result;
773}
774
Seth Forshee135740d2011-09-20 16:55:49 -0500775static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400776{
777 u32 hci_result;
778 u32 value, value2;
779
780 value = 0;
781 value2 = 0x0001;
Seth Forshee135740d2011-09-20 16:55:49 -0500782 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400783
784 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
785 return hci_result;
786}
787
Johannes Berg19d337d2009-06-02 13:01:37 +0200788static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400789{
Johannes Berg19d337d2009-06-02 13:01:37 +0200790 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400791 u32 result1, result2;
792 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +0200793 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400794 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400795
Johannes Berg19d337d2009-06-02 13:01:37 +0200796 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400797
798 mutex_lock(&dev->mutex);
Seth Forshee135740d2011-09-20 16:55:49 -0500799 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500800 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200801 goto out;
802 }
803
804 if (!radio_state) {
805 err = 0;
806 goto out;
807 }
808
Seth Forshee135740d2011-09-20 16:55:49 -0500809 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
810 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400811
812 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500813 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200814 else
815 err = 0;
816 out:
817 mutex_unlock(&dev->mutex);
818 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400819}
820
Johannes Berg19d337d2009-06-02 13:01:37 +0200821static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400822{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400823 bool new_rfk_state;
824 bool value;
825 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +0200826 struct toshiba_acpi_dev *dev = data;
827
828 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400829
Seth Forshee135740d2011-09-20 16:55:49 -0500830 hci_result = hci_get_radio_state(dev, &value);
Johannes Berg19d337d2009-06-02 13:01:37 +0200831 if (hci_result != HCI_SUCCESS) {
832 /* Can't do anything useful */
833 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -0700834 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200835 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400836
837 new_rfk_state = value;
838
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400839 mutex_unlock(&dev->mutex);
840
Johannes Berg19d337d2009-06-02 13:01:37 +0200841 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
842 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400843}
844
Johannes Berg19d337d2009-06-02 13:01:37 +0200845static const struct rfkill_ops toshiba_rfk_ops = {
846 .set_block = bt_rfkill_set_block,
847 .poll = bt_rfkill_poll,
848};
849
Akio Idehara121b7b02012-04-06 01:46:43 +0900850static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
851{
852 u32 hci_result;
853 u32 status;
854
855 hci_read1(dev, HCI_TR_BACKLIGHT, &status, &hci_result);
856 *enabled = !status;
857 return hci_result == HCI_SUCCESS ? 0 : -EIO;
858}
859
860static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
861{
862 u32 hci_result;
863 u32 value = !enable;
864
865 hci_write1(dev, HCI_TR_BACKLIGHT, value, &hci_result);
866 return hci_result == HCI_SUCCESS ? 0 : -EIO;
867}
868
Len Brown4be44fc2005-08-05 00:44:28 -0400869static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Seth Forshee62cce752012-04-19 11:23:50 -0500871static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 u32 hci_result;
874 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +0900875 int brightness = 0;
876
877 if (dev->tr_backlight_supported) {
878 bool enabled;
879 int ret = get_tr_backlight_status(dev, &enabled);
880 if (ret)
881 return ret;
882 if (enabled)
883 return 0;
884 brightness++;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Seth Forshee135740d2011-09-20 16:55:49 -0500887 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500888 if (hci_result == HCI_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +0900889 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500890
891 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700892}
893
Seth Forshee62cce752012-04-19 11:23:50 -0500894static int get_lcd_brightness(struct backlight_device *bd)
895{
896 struct toshiba_acpi_dev *dev = bl_get_data(bd);
897 return __get_lcd_brightness(dev);
898}
899
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800900static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -0700901{
Seth Forshee135740d2011-09-20 16:55:49 -0500902 struct toshiba_acpi_dev *dev = m->private;
903 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +0900904 int levels;
Holger Machtc9263552006-10-20 14:30:29 -0700905
Seth Forshee135740d2011-09-20 16:55:49 -0500906 if (!dev->backlight_dev)
907 return -ENODEV;
908
Akio Idehara121b7b02012-04-06 01:46:43 +0900909 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -0500910 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -0700911 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800912 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +0900913 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500917 pr_err("Error reading LCD brightness\n");
918 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800919}
920
921static int lcd_proc_open(struct inode *inode, struct file *file)
922{
Al Virod9dda782013-03-31 18:16:14 -0400923 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Seth Forshee62cce752012-04-19 11:23:50 -0500926static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -0700927{
Azael Avalosf6aac652014-08-04 09:21:01 -0600928 u32 in[HCI_WORDS] = { HCI_SET, HCI_LCD_BRIGHTNESS, 0, 0, 0, 0 };
929 u32 out[HCI_WORDS];
930 acpi_status status;
Holger Machtc9263552006-10-20 14:30:29 -0700931
Akio Idehara121b7b02012-04-06 01:46:43 +0900932 if (dev->tr_backlight_supported) {
933 bool enable = !value;
934 int ret = set_tr_backlight_status(dev, enable);
935 if (ret)
936 return ret;
937 if (value)
938 value--;
939 }
940
Azael Avalosf6aac652014-08-04 09:21:01 -0600941 in[2] = value << HCI_LCD_BRIGHTNESS_SHIFT;
942 status = hci_raw(dev, in, out);
943 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
944 pr_err("ACPI call to set brightness failed");
945 return -EIO;
946 }
947 /* Extra check for "incomplete" backlight method, where the AML code
948 * doesn't check for HCI_SET or HCI_GET and returns HCI_SUCCESS,
949 * the actual brightness, and in some cases the max brightness.
950 */
951 if (out[2] > 0 || out[3] == 0xE000)
952 return -ENODEV;
953
954 return out[0] == HCI_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700955}
956
957static int set_lcd_status(struct backlight_device *bd)
958{
Seth Forshee135740d2011-09-20 16:55:49 -0500959 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Seth Forshee62cce752012-04-19 11:23:50 -0500960 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -0700961}
962
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800963static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
964 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Al Virod9dda782013-03-31 18:16:14 -0400966 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800967 char cmd[42];
968 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800970 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +0900971 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800973 len = min(count, sizeof(cmd) - 1);
974 if (copy_from_user(cmd, buf, len))
975 return -EFAULT;
976 cmd[len] = '\0';
977
978 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +0900979 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -0500980 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800981 if (ret == 0)
982 ret = count;
983 } else {
Holger Machtc9263552006-10-20 14:30:29 -0700984 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800985 }
Holger Machtc9263552006-10-20 14:30:29 -0700986 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800989static const struct file_operations lcd_proc_fops = {
990 .owner = THIS_MODULE,
991 .open = lcd_proc_open,
992 .read = seq_read,
993 .llseek = seq_lseek,
994 .release = single_release,
995 .write = lcd_proc_write,
996};
997
Seth Forshee36d03f92011-09-20 16:55:53 -0500998static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
999{
1000 u32 hci_result;
1001
1002 hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
1003 return hci_result == HCI_SUCCESS ? 0 : -EIO;
1004}
1005
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001006static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Seth Forshee135740d2011-09-20 16:55:49 -05001008 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001010 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Seth Forshee36d03f92011-09-20 16:55:53 -05001012 ret = get_video_status(dev, &value);
1013 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1015 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001016 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001017 seq_printf(m, "lcd_out: %d\n", is_lcd);
1018 seq_printf(m, "crt_out: %d\n", is_crt);
1019 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Seth Forshee36d03f92011-09-20 16:55:53 -05001022 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001025static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Al Virod9dda782013-03-31 18:16:14 -04001027 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001028}
1029
1030static ssize_t video_proc_write(struct file *file, const char __user *buf,
1031 size_t count, loff_t *pos)
1032{
Al Virod9dda782013-03-31 18:16:14 -04001033 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001034 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001035 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int value;
1037 int remain = count;
1038 int lcd_out = -1;
1039 int crt_out = -1;
1040 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001041 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001043 cmd = kmalloc(count + 1, GFP_KERNEL);
1044 if (!cmd)
1045 return -ENOMEM;
1046 if (copy_from_user(cmd, buf, count)) {
1047 kfree(cmd);
1048 return -EFAULT;
1049 }
1050 cmd[count] = '\0';
1051
1052 buffer = cmd;
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* scan expression. Multiple expressions may be delimited with ;
1055 *
1056 * NOTE: to keep scanning simple, invalid fields are ignored
1057 */
1058 while (remain) {
1059 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1060 lcd_out = value & 1;
1061 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1062 crt_out = value & 1;
1063 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1064 tv_out = value & 1;
1065 /* advance to one character past the next ; */
1066 do {
1067 ++buffer;
1068 --remain;
1069 }
Len Brown4be44fc2005-08-05 00:44:28 -04001070 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001073 kfree(cmd);
1074
Seth Forshee36d03f92011-09-20 16:55:53 -05001075 ret = get_video_status(dev, &video_out);
1076 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001077 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (lcd_out != -1)
1079 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1080 if (crt_out != -1)
1081 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1082 if (tv_out != -1)
1083 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1084 /* To avoid unnecessary video disruption, only write the new
1085 * video setting if something changed. */
1086 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001087 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001090 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001093static const struct file_operations video_proc_fops = {
1094 .owner = THIS_MODULE,
1095 .open = video_proc_open,
1096 .read = seq_read,
1097 .llseek = seq_lseek,
1098 .release = single_release,
1099 .write = video_proc_write,
1100};
1101
Seth Forshee36d03f92011-09-20 16:55:53 -05001102static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1103{
1104 u32 hci_result;
1105
1106 hci_read1(dev, HCI_FAN, status, &hci_result);
1107 return hci_result == HCI_SUCCESS ? 0 : -EIO;
1108}
1109
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001110static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Seth Forshee135740d2011-09-20 16:55:49 -05001112 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001113 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 u32 value;
1115
Seth Forshee36d03f92011-09-20 16:55:53 -05001116 ret = get_fan_status(dev, &value);
1117 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001118 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001119 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
Seth Forshee36d03f92011-09-20 16:55:53 -05001122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001125static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Al Virod9dda782013-03-31 18:16:14 -04001127 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001128}
1129
1130static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1131 size_t count, loff_t *pos)
1132{
Al Virod9dda782013-03-31 18:16:14 -04001133 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001134 char cmd[42];
1135 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 int value;
1137 u32 hci_result;
1138
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001139 len = min(count, sizeof(cmd) - 1);
1140 if (copy_from_user(cmd, buf, len))
1141 return -EFAULT;
1142 cmd[len] = '\0';
1143
1144 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001145 value >= 0 && value <= 1) {
Seth Forshee135740d2011-09-20 16:55:49 -05001146 hci_write1(dev, HCI_FAN, value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (hci_result != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001148 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 else
Seth Forshee135740d2011-09-20 16:55:49 -05001150 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 } else {
1152 return -EINVAL;
1153 }
1154
1155 return count;
1156}
1157
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001158static const struct file_operations fan_proc_fops = {
1159 .owner = THIS_MODULE,
1160 .open = fan_proc_open,
1161 .read = seq_read,
1162 .llseek = seq_lseek,
1163 .release = single_release,
1164 .write = fan_proc_write,
1165};
1166
1167static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Seth Forshee135740d2011-09-20 16:55:49 -05001169 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 u32 hci_result;
1171 u32 value;
1172
Seth Forshee11948b92011-11-16 17:37:45 -06001173 if (!dev->key_event_valid && dev->system_event_supported) {
Seth Forshee135740d2011-09-20 16:55:49 -05001174 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (hci_result == HCI_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001176 dev->key_event_valid = 1;
1177 dev->last_key_event = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 } else if (hci_result == HCI_EMPTY) {
1179 /* better luck next time */
1180 } else if (hci_result == HCI_NOT_SUPPORTED) {
1181 /* This is a workaround for an unresolved issue on
1182 * some machines where system events sporadically
1183 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -05001184 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -07001185 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001187 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001188 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 }
1191
Seth Forshee135740d2011-09-20 16:55:49 -05001192 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1193 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001197static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Al Virod9dda782013-03-31 18:16:14 -04001199 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001200}
1201
1202static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1203 size_t count, loff_t *pos)
1204{
Al Virod9dda782013-03-31 18:16:14 -04001205 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001206 char cmd[42];
1207 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int value;
1209
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001210 len = min(count, sizeof(cmd) - 1);
1211 if (copy_from_user(cmd, buf, len))
1212 return -EFAULT;
1213 cmd[len] = '\0';
1214
1215 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -05001216 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 } else {
1218 return -EINVAL;
1219 }
1220
1221 return count;
1222}
1223
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001224static const struct file_operations keys_proc_fops = {
1225 .owner = THIS_MODULE,
1226 .open = keys_proc_open,
1227 .read = seq_read,
1228 .llseek = seq_lseek,
1229 .release = single_release,
1230 .write = keys_proc_write,
1231};
1232
1233static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001235 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1236 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001240static int version_proc_open(struct inode *inode, struct file *file)
1241{
Al Virod9dda782013-03-31 18:16:14 -04001242 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001243}
1244
1245static const struct file_operations version_proc_fops = {
1246 .owner = THIS_MODULE,
1247 .open = version_proc_open,
1248 .read = seq_read,
1249 .llseek = seq_lseek,
1250 .release = single_release,
1251};
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253/* proc and module init
1254 */
1255
1256#define PROC_TOSHIBA "toshiba"
1257
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001258static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Seth Forshee36d03f92011-09-20 16:55:53 -05001260 if (dev->backlight_dev)
1261 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1262 &lcd_proc_fops, dev);
1263 if (dev->video_supported)
1264 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1265 &video_proc_fops, dev);
1266 if (dev->fan_supported)
1267 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1268 &fan_proc_fops, dev);
1269 if (dev->hotkey_dev)
1270 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1271 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001272 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1273 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Seth Forshee36d03f92011-09-20 16:55:53 -05001276static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Seth Forshee36d03f92011-09-20 16:55:53 -05001278 if (dev->backlight_dev)
1279 remove_proc_entry("lcd", toshiba_proc_dir);
1280 if (dev->video_supported)
1281 remove_proc_entry("video", toshiba_proc_dir);
1282 if (dev->fan_supported)
1283 remove_proc_entry("fan", toshiba_proc_dir);
1284 if (dev->hotkey_dev)
1285 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001286 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Lionel Debrouxacc24722010-11-16 14:14:02 +01001289static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001290 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001291 .get_brightness = get_lcd_brightness,
1292 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001293};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001294
Azael Avalos360f0f32014-03-25 20:38:31 -06001295/*
1296 * Sysfs files
1297 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001298static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1299 struct device_attribute *attr,
1300 const char *buf, size_t count);
1301static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1302 struct device_attribute *attr,
1303 char *buf);
1304static ssize_t toshiba_kbd_type_show(struct device *dev,
1305 struct device_attribute *attr,
1306 char *buf);
1307static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1308 struct device_attribute *attr,
1309 char *buf);
1310static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1311 struct device_attribute *attr,
1312 const char *buf, size_t count);
1313static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1314 struct device_attribute *attr,
1315 char *buf);
1316static ssize_t toshiba_touchpad_store(struct device *dev,
1317 struct device_attribute *attr,
1318 const char *buf, size_t count);
1319static ssize_t toshiba_touchpad_show(struct device *dev,
1320 struct device_attribute *attr,
1321 char *buf);
1322static ssize_t toshiba_position_show(struct device *dev,
1323 struct device_attribute *attr,
1324 char *buf);
1325
1326static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1327 toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1328static DEVICE_ATTR(kbd_type, S_IRUGO, toshiba_kbd_type_show, NULL);
1329static DEVICE_ATTR(available_kbd_modes, S_IRUGO,
1330 toshiba_available_kbd_modes_show, NULL);
1331static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1332 toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
1333static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1334 toshiba_touchpad_show, toshiba_touchpad_store);
1335static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
1336
1337static struct attribute *toshiba_attributes[] = {
1338 &dev_attr_kbd_backlight_mode.attr,
1339 &dev_attr_kbd_type.attr,
1340 &dev_attr_available_kbd_modes.attr,
1341 &dev_attr_kbd_backlight_timeout.attr,
1342 &dev_attr_touchpad.attr,
1343 &dev_attr_position.attr,
1344 NULL,
1345};
1346
1347static umode_t toshiba_sysfs_is_visible(struct kobject *,
1348 struct attribute *, int);
1349
1350static struct attribute_group toshiba_attr_group = {
1351 .is_visible = toshiba_sysfs_is_visible,
1352 .attrs = toshiba_attributes,
1353};
Azael Avalos360f0f32014-03-25 20:38:31 -06001354
1355static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1356 struct device_attribute *attr,
1357 const char *buf, size_t count)
1358{
1359 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001360 int mode;
1361 int time;
1362 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001363
Dan Carpenteraeaac092014-09-03 14:44:37 +03001364
1365 ret = kstrtoint(buf, 0, &mode);
1366 if (ret)
1367 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001368
1369 /* Check for supported modes depending on keyboard backlight type */
1370 if (toshiba->kbd_type == 1) {
1371 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1372 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1373 return -EINVAL;
1374 } else if (toshiba->kbd_type == 2) {
1375 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1376 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1377 mode != SCI_KBD_MODE_OFF)
1378 return -EINVAL;
1379 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001380
1381 /* Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001382 * Auto - KBD backlight turns off automatically in given time
1383 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001384 * ON - KBD backlight is always on
1385 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001386 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001387
1388 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001389 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001390 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001391 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001392
1393 /* OR the "base time" to the actual method format */
1394 if (toshiba->kbd_type == 1) {
1395 /* Type 1 requires the current mode */
1396 time |= toshiba->kbd_mode;
1397 } else if (toshiba->kbd_type == 2) {
1398 /* Type 2 requires the desired mode */
1399 time |= mode;
1400 }
1401
Dan Carpenteraeaac092014-09-03 14:44:37 +03001402 ret = toshiba_kbd_illum_status_set(toshiba, time);
1403 if (ret)
1404 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001405
1406 /* Update sysfs entries on successful mode change*/
1407 ret = sysfs_update_group(&toshiba->acpi_dev->dev.kobj,
1408 &toshiba_attr_group);
1409 if (ret)
1410 return ret;
1411
Azael Avalos360f0f32014-03-25 20:38:31 -06001412 toshiba->kbd_mode = mode;
1413 }
1414
1415 return count;
1416}
1417
1418static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1419 struct device_attribute *attr,
1420 char *buf)
1421{
1422 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1423 u32 time;
1424
1425 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1426 return -EIO;
1427
Azael Avalos93f8c162014-09-12 18:50:36 -06001428 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1429}
1430
1431static ssize_t toshiba_kbd_type_show(struct device *dev,
1432 struct device_attribute *attr,
1433 char *buf)
1434{
1435 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1436
1437 return sprintf(buf, "%d\n", toshiba->kbd_type);
1438}
1439
1440static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1441 struct device_attribute *attr,
1442 char *buf)
1443{
1444 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1445
1446 if (toshiba->kbd_type == 1)
1447 return sprintf(buf, "%x %x\n",
1448 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1449
1450 return sprintf(buf, "%x %x %x\n",
1451 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001452}
1453
1454static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1455 struct device_attribute *attr,
1456 const char *buf, size_t count)
1457{
1458 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1459 int time = -1;
1460
1461 if (sscanf(buf, "%i", &time) != 1 && (time < 0 || time > 60))
1462 return -EINVAL;
1463
1464 /* Set the Keyboard Backlight Timeout: 0-60 seconds */
1465 if (time != -1 && toshiba->kbd_time != time) {
1466 time = time << HCI_MISC_SHIFT;
1467 time = (toshiba->kbd_mode == SCI_KBD_MODE_AUTO) ?
1468 time + 1 : time + 2;
1469 if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1470 return -EIO;
1471 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1472 }
1473
1474 return count;
1475}
1476
1477static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1478 struct device_attribute *attr,
1479 char *buf)
1480{
1481 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1482 u32 time;
1483
1484 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1485 return -EIO;
1486
1487 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1488}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001489
Azael Avalos9d8658a2014-03-25 20:38:32 -06001490static ssize_t toshiba_touchpad_store(struct device *dev,
1491 struct device_attribute *attr,
1492 const char *buf, size_t count)
1493{
1494 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1495 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001496 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001497
1498 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001499 ret = kstrtoint(buf, 0, &state);
1500 if (ret)
1501 return ret;
1502 if (state != 0 && state != 1)
1503 return -EINVAL;
1504
1505 ret = toshiba_touchpad_set(toshiba, state);
1506 if (ret)
1507 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001508
1509 return count;
1510}
1511
1512static ssize_t toshiba_touchpad_show(struct device *dev,
1513 struct device_attribute *attr, char *buf)
1514{
1515 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1516 u32 state;
1517 int ret;
1518
1519 ret = toshiba_touchpad_get(toshiba, &state);
1520 if (ret < 0)
1521 return ret;
1522
1523 return sprintf(buf, "%i\n", state);
1524}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001525
Azael Avalos5a2813e2014-03-25 20:38:34 -06001526static ssize_t toshiba_position_show(struct device *dev,
1527 struct device_attribute *attr, char *buf)
1528{
1529 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1530 u32 xyval, zval, tmp;
1531 u16 x, y, z;
1532 int ret;
1533
1534 xyval = zval = 0;
1535 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1536 if (ret < 0)
1537 return ret;
1538
1539 x = xyval & HCI_ACCEL_MASK;
1540 tmp = xyval >> HCI_MISC_SHIFT;
1541 y = tmp & HCI_ACCEL_MASK;
1542 z = zval & HCI_ACCEL_MASK;
1543
1544 return sprintf(buf, "%d %d %d\n", x, y, z);
1545}
Azael Avalos360f0f32014-03-25 20:38:31 -06001546
Azael Avalos360f0f32014-03-25 20:38:31 -06001547static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
1548 struct attribute *attr, int idx)
1549{
1550 struct device *dev = container_of(kobj, struct device, kobj);
1551 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
1552 bool exists = true;
1553
1554 if (attr == &dev_attr_kbd_backlight_mode.attr)
1555 exists = (drv->kbd_illum_supported) ? true : false;
1556 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
1557 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001558 else if (attr == &dev_attr_touchpad.attr)
1559 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06001560 else if (attr == &dev_attr_position.attr)
1561 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06001562
1563 return exists ? attr->mode : 0;
1564}
1565
Seth Forshee29cd2932012-01-18 13:44:09 -06001566static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
1567 struct serio *port)
1568{
1569 if (str & 0x20)
1570 return false;
1571
1572 if (unlikely(data == 0xe0))
1573 return false;
1574
1575 if ((data & 0x7f) == TOS1900_FN_SCAN) {
1576 schedule_work(&toshiba_acpi->hotkey_work);
1577 return true;
1578 }
1579
1580 return false;
1581}
1582
1583static void toshiba_acpi_hotkey_work(struct work_struct *work)
1584{
1585 acpi_handle ec_handle = ec_get_handle();
1586 acpi_status status;
1587
1588 if (!ec_handle)
1589 return;
1590
1591 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
1592 if (ACPI_FAILURE(status))
1593 pr_err("ACPI NTFY method execution failed\n");
1594}
1595
1596/*
1597 * Returns hotkey scancode, or < 0 on failure.
1598 */
1599static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
1600{
Zhang Rui74facaf2013-09-03 08:32:15 +08001601 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06001602 acpi_status status;
1603
Zhang Rui74facaf2013-09-03 08:32:15 +08001604 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
1605 NULL, &value);
1606 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06001607 pr_err("ACPI INFO method execution failed\n");
1608 return -EIO;
1609 }
1610
Zhang Rui74facaf2013-09-03 08:32:15 +08001611 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06001612}
1613
1614static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
1615 int scancode)
1616{
1617 if (scancode == 0x100)
1618 return;
1619
1620 /* act on key press; ignore key release */
1621 if (scancode & 0x80)
1622 return;
1623
1624 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
1625 pr_info("Unknown key %x\n", scancode);
1626}
1627
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001628static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001629{
Seth Forshee135740d2011-09-20 16:55:49 -05001630 acpi_status status;
Zhang Ruie2e19602013-09-03 08:32:06 +08001631 acpi_handle ec_handle;
Seth Forshee135740d2011-09-20 16:55:49 -05001632 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06001633 u32 hci_result;
Takashi Iwaife808bfb2014-04-29 15:15:38 +02001634 const struct key_entry *keymap = toshiba_acpi_keymap;
Seth Forshee135740d2011-09-20 16:55:49 -05001635
Seth Forshee135740d2011-09-20 16:55:49 -05001636 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07001637 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001638 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05001639
1640 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05001641 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05001642 dev->hotkey_dev->id.bustype = BUS_HOST;
1643
Takashi Iwaife808bfb2014-04-29 15:15:38 +02001644 if (dmi_check_system(toshiba_alt_keymap_dmi))
1645 keymap = toshiba_acpi_alt_keymap;
1646 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05001647 if (error)
1648 goto err_free_dev;
1649
Seth Forshee29cd2932012-01-18 13:44:09 -06001650 /*
1651 * For some machines the SCI responsible for providing hotkey
1652 * notification doesn't fire. We can trigger the notification
1653 * whenever the Fn key is pressed using the NTFY method, if
1654 * supported, so if it's present set up an i8042 key filter
1655 * for this purpose.
1656 */
1657 status = AE_ERROR;
1658 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08001659 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06001660 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
1661
1662 error = i8042_install_filter(toshiba_acpi_i8042_filter);
1663 if (error) {
1664 pr_err("Error installing key filter\n");
1665 goto err_free_keymap;
1666 }
1667
1668 dev->ntfy_supported = 1;
1669 }
1670
1671 /*
1672 * Determine hotkey query interface. Prefer using the INFO
1673 * method when it is available.
1674 */
Zhang Ruie2e19602013-09-03 08:32:06 +08001675 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06001676 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08001677 else {
Seth Forshee29cd2932012-01-18 13:44:09 -06001678 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
1679 if (hci_result == HCI_SUCCESS)
1680 dev->system_event_supported = 1;
1681 }
1682
1683 if (!dev->info_supported && !dev->system_event_supported) {
1684 pr_warn("No hotkey query interface found\n");
1685 goto err_remove_filter;
1686 }
1687
Seth Forshee6e02cc72011-09-20 16:55:51 -05001688 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05001689 if (ACPI_FAILURE(status)) {
1690 pr_info("Unable to enable hotkeys\n");
1691 error = -ENODEV;
Seth Forshee29cd2932012-01-18 13:44:09 -06001692 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05001693 }
1694
1695 error = input_register_device(dev->hotkey_dev);
1696 if (error) {
1697 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06001698 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05001699 }
1700
Seth Forshee29cd2932012-01-18 13:44:09 -06001701 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result);
Seth Forshee135740d2011-09-20 16:55:49 -05001702 return 0;
1703
Seth Forshee29cd2932012-01-18 13:44:09 -06001704 err_remove_filter:
1705 if (dev->ntfy_supported)
1706 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05001707 err_free_keymap:
1708 sparse_keymap_free(dev->hotkey_dev);
1709 err_free_dev:
1710 input_free_device(dev->hotkey_dev);
1711 dev->hotkey_dev = NULL;
1712 return error;
1713}
1714
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001715static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05001716{
1717 struct backlight_properties props;
1718 int brightness;
1719 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001720 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05001721
1722 /*
1723 * Some machines don't support the backlight methods at all, and
1724 * others support it read-only. Either of these is pretty useless,
1725 * so only register the backlight device if the backlight method
1726 * supports both reads and writes.
1727 */
1728 brightness = __get_lcd_brightness(dev);
1729 if (brightness < 0)
1730 return 0;
1731 ret = set_lcd_brightness(dev, brightness);
1732 if (ret) {
1733 pr_debug("Backlight method is read-only, disabling backlight support\n");
1734 return 0;
1735 }
1736
Akio Idehara121b7b02012-04-06 01:46:43 +09001737 /* Determine whether or not BIOS supports transflective backlight */
1738 ret = get_tr_backlight_status(dev, &enabled);
1739 dev->tr_backlight_supported = !ret;
1740
Matthew Garrett53039f22012-06-01 11:02:36 -04001741 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05001742 props.type = BACKLIGHT_PLATFORM;
1743 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001744
Akio Idehara121b7b02012-04-06 01:46:43 +09001745 /* adding an extra level and having 0 change to transflective mode */
1746 if (dev->tr_backlight_supported)
1747 props.max_brightness++;
1748
Seth Forshee62cce752012-04-19 11:23:50 -05001749 dev->backlight_dev = backlight_device_register("toshiba",
1750 &dev->acpi_dev->dev,
1751 dev,
1752 &toshiba_backlight_data,
1753 &props);
1754 if (IS_ERR(dev->backlight_dev)) {
1755 ret = PTR_ERR(dev->backlight_dev);
1756 pr_err("Could not register toshiba backlight device\n");
1757 dev->backlight_dev = NULL;
1758 return ret;
1759 }
1760
1761 dev->backlight_dev->props.brightness = brightness;
1762 return 0;
1763}
1764
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001765static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001766{
1767 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1768
Seth Forshee36d03f92011-09-20 16:55:53 -05001769 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001770
Azael Avalos360f0f32014-03-25 20:38:31 -06001771 if (dev->sysfs_created)
1772 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
1773 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05001774
Seth Forshee29cd2932012-01-18 13:44:09 -06001775 if (dev->ntfy_supported) {
1776 i8042_remove_filter(toshiba_acpi_i8042_filter);
1777 cancel_work_sync(&dev->hotkey_work);
1778 }
1779
Seth Forshee135740d2011-09-20 16:55:49 -05001780 if (dev->hotkey_dev) {
1781 input_unregister_device(dev->hotkey_dev);
1782 sparse_keymap_free(dev->hotkey_dev);
1783 }
1784
1785 if (dev->bt_rfk) {
1786 rfkill_unregister(dev->bt_rfk);
1787 rfkill_destroy(dev->bt_rfk);
1788 }
1789
1790 if (dev->backlight_dev)
1791 backlight_device_unregister(dev->backlight_dev);
1792
Seth Forshee36d03f92011-09-20 16:55:53 -05001793 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05001794 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001795
Azael Avalos360f0f32014-03-25 20:38:31 -06001796 if (dev->kbd_led_registered)
1797 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001798
Azael Avalosdef6c4e2014-03-25 20:38:33 -06001799 if (dev->eco_supported)
1800 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05001801
Seth Forshee29cd2932012-01-18 13:44:09 -06001802 if (toshiba_acpi)
1803 toshiba_acpi = NULL;
1804
Seth Forshee135740d2011-09-20 16:55:49 -05001805 kfree(dev);
1806
1807 return 0;
1808}
1809
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001810static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05001811{
Zhang Ruie2e19602013-09-03 08:32:06 +08001812 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05001813 return "GHCI";
1814
Zhang Ruie2e19602013-09-03 08:32:06 +08001815 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05001816 return "SPFC";
1817
1818 return NULL;
1819}
1820
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001821static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001822{
1823 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001824 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05001825 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05001826 bool bt_present;
1827 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05001828
Seth Forshee29cd2932012-01-18 13:44:09 -06001829 if (toshiba_acpi)
1830 return -EBUSY;
1831
Seth Forshee135740d2011-09-20 16:55:49 -05001832 pr_info("Toshiba Laptop ACPI Extras version %s\n",
1833 TOSHIBA_ACPI_VERSION);
1834
Seth Forsheea540d6b2011-09-20 16:55:52 -05001835 hci_method = find_hci_method(acpi_dev->handle);
1836 if (!hci_method) {
1837 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05001838 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001839 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05001840
Seth Forshee135740d2011-09-20 16:55:49 -05001841 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1842 if (!dev)
1843 return -ENOMEM;
1844 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001845 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05001846 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06001847 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001848
Seth Forshee6e02cc72011-09-20 16:55:51 -05001849 if (toshiba_acpi_setup_keyboard(dev))
1850 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05001851
1852 mutex_init(&dev->mutex);
1853
Seth Forshee62cce752012-04-19 11:23:50 -05001854 ret = toshiba_acpi_setup_backlight(dev);
1855 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05001856 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05001857
1858 /* Register rfkill switch for Bluetooth */
1859 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
1860 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
1861 &acpi_dev->dev,
1862 RFKILL_TYPE_BLUETOOTH,
1863 &toshiba_rfk_ops,
1864 dev);
1865 if (!dev->bt_rfk) {
1866 pr_err("unable to allocate rfkill device\n");
1867 ret = -ENOMEM;
1868 goto error;
1869 }
1870
1871 ret = rfkill_register(dev->bt_rfk);
1872 if (ret) {
1873 pr_err("unable to register rfkill device\n");
1874 rfkill_destroy(dev->bt_rfk);
1875 goto error;
1876 }
1877 }
1878
1879 if (toshiba_illumination_available(dev)) {
1880 dev->led_dev.name = "toshiba::illumination";
1881 dev->led_dev.max_brightness = 1;
1882 dev->led_dev.brightness_set = toshiba_illumination_set;
1883 dev->led_dev.brightness_get = toshiba_illumination_get;
1884 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05001885 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05001886 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001887
Azael Avalosdef6c4e2014-03-25 20:38:33 -06001888 if (toshiba_eco_mode_available(dev)) {
1889 dev->eco_led.name = "toshiba::eco_mode";
1890 dev->eco_led.max_brightness = 1;
1891 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
1892 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
1893 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
1894 dev->eco_supported = 1;
1895 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001896
Azael Avalos93f8c162014-09-12 18:50:36 -06001897 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06001898 /*
1899 * Only register the LED if KBD illumination is supported
1900 * and the keyboard backlight operation mode is set to FN-Z
1901 */
1902 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
1903 dev->kbd_led.name = "toshiba::kbd_backlight";
1904 dev->kbd_led.max_brightness = 1;
1905 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
1906 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
1907 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
1908 dev->kbd_led_registered = 1;
1909 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001910
Azael Avalos9d8658a2014-03-25 20:38:32 -06001911 ret = toshiba_touchpad_get(dev, &dummy);
1912 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001913
Azael Avalos5a2813e2014-03-25 20:38:34 -06001914 ret = toshiba_accelerometer_supported(dev);
1915 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05001916
Seth Forshee36d03f92011-09-20 16:55:53 -05001917 /* Determine whether or not BIOS supports fan and video interfaces */
1918
1919 ret = get_video_status(dev, &dummy);
1920 dev->video_supported = !ret;
1921
1922 ret = get_fan_status(dev, &dummy);
1923 dev->fan_supported = !ret;
1924
Azael Avalos360f0f32014-03-25 20:38:31 -06001925 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
1926 &toshiba_attr_group);
1927 if (ret) {
1928 dev->sysfs_created = 0;
1929 goto error;
1930 }
1931 dev->sysfs_created = !ret;
1932
Seth Forshee36d03f92011-09-20 16:55:53 -05001933 create_toshiba_proc_entries(dev);
1934
Seth Forshee29cd2932012-01-18 13:44:09 -06001935 toshiba_acpi = dev;
1936
Seth Forshee135740d2011-09-20 16:55:49 -05001937 return 0;
1938
1939error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001940 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001941 return ret;
1942}
1943
1944static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1945{
1946 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001947 u32 hci_result, value;
Seth Forshee11948b92011-11-16 17:37:45 -06001948 int retries = 3;
Seth Forshee29cd2932012-01-18 13:44:09 -06001949 int scancode;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001950
Seth Forshee29cd2932012-01-18 13:44:09 -06001951 if (event != 0x80)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001952 return;
Seth Forshee11948b92011-11-16 17:37:45 -06001953
Seth Forshee29cd2932012-01-18 13:44:09 -06001954 if (dev->info_supported) {
1955 scancode = toshiba_acpi_query_hotkey(dev);
1956 if (scancode < 0)
1957 pr_err("Failed to query hotkey event\n");
1958 else if (scancode != 0)
1959 toshiba_acpi_report_hotkey(dev, scancode);
1960 } else if (dev->system_event_supported) {
1961 do {
1962 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1963 switch (hci_result) {
1964 case HCI_SUCCESS:
1965 toshiba_acpi_report_hotkey(dev, (int)value);
1966 break;
1967 case HCI_NOT_SUPPORTED:
1968 /*
1969 * This is a workaround for an unresolved
1970 * issue on some machines where system events
1971 * sporadically become disabled.
1972 */
1973 hci_write1(dev, HCI_SYSTEM_EVENT, 1,
1974 &hci_result);
1975 pr_notice("Re-enabled hotkeys\n");
1976 /* fall through */
1977 default:
1978 retries--;
1979 break;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001980 }
Seth Forshee29cd2932012-01-18 13:44:09 -06001981 } while (retries && hci_result != HCI_EMPTY);
1982 }
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001983}
1984
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02001985#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001986static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06001987{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001988 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06001989 u32 result;
1990
1991 if (dev->hotkey_dev)
1992 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result);
1993
1994 return 0;
1995}
1996
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001997static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06001998{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001999 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002000 u32 result;
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002001 acpi_status status;
Seth Forshee29cd2932012-01-18 13:44:09 -06002002
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002003 if (dev->hotkey_dev) {
2004 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB",
2005 NULL, NULL);
2006 if (ACPI_FAILURE(status))
2007 pr_info("Unable to re-enable hotkeys\n");
2008
Seth Forshee29cd2932012-01-18 13:44:09 -06002009 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result);
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002010 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002011
2012 return 0;
2013}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002014#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002015
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002016static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2017 toshiba_acpi_suspend, toshiba_acpi_resume);
2018
Seth Forshee135740d2011-09-20 16:55:49 -05002019static struct acpi_driver toshiba_acpi_driver = {
2020 .name = "Toshiba ACPI driver",
2021 .owner = THIS_MODULE,
2022 .ids = toshiba_device_ids,
2023 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2024 .ops = {
2025 .add = toshiba_acpi_add,
2026 .remove = toshiba_acpi_remove,
2027 .notify = toshiba_acpi_notify,
2028 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002029 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002030};
Holger Machtc9263552006-10-20 14:30:29 -07002031
Len Brown4be44fc2005-08-05 00:44:28 -04002032static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Seth Forshee135740d2011-09-20 16:55:49 -05002034 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Seth Forsheef11f9992012-01-18 13:44:11 -06002036 /*
2037 * Machines with this WMI guid aren't supported due to bugs in
2038 * their AML. This check relies on wmi initializing before
2039 * toshiba_acpi to guarantee guids have been identified.
2040 */
2041 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2042 return -ENODEV;
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2045 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002046 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002047 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049
Seth Forshee135740d2011-09-20 16:55:49 -05002050 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2051 if (ret) {
2052 pr_err("Failed to register ACPI driver: %d\n", ret);
2053 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002054 }
2055
Seth Forshee135740d2011-09-20 16:55:49 -05002056 return ret;
2057}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002058
Seth Forshee135740d2011-09-20 16:55:49 -05002059static void __exit toshiba_acpi_exit(void)
2060{
2061 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2062 if (toshiba_proc_dir)
2063 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066module_init(toshiba_acpi_init);
2067module_exit(toshiba_acpi_exit);