blob: ba75701986e508730466c207cc92c679bd4e44bb [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 Avalos360f0f32014-03-25 20:38:31 -0600141#define SCI_KBD_MODE_FNZ 0x1
142#define SCI_KBD_MODE_AUTO 0x2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Seth Forshee135740d2011-09-20 16:55:49 -0500144struct toshiba_acpi_dev {
145 struct acpi_device *acpi_dev;
146 const char *method_hci;
147 struct rfkill *bt_rfk;
148 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600149 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500150 struct backlight_device *backlight_dev;
151 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600152 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600153 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500154
Seth Forshee135740d2011-09-20 16:55:49 -0500155 int force_fan;
156 int last_key_event;
157 int key_event_valid;
Azael Avalos360f0f32014-03-25 20:38:31 -0600158 int kbd_mode;
159 int kbd_time;
Seth Forshee135740d2011-09-20 16:55:49 -0500160
Dan Carpenter592b7462012-01-15 14:28:20 +0300161 unsigned int illumination_supported:1;
162 unsigned int video_supported:1;
163 unsigned int fan_supported:1;
164 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600165 unsigned int ntfy_supported:1;
166 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900167 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600168 unsigned int kbd_illum_supported:1;
169 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600170 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600171 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600172 unsigned int accelerometer_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600173 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500174
Seth Forshee135740d2011-09-20 16:55:49 -0500175 struct mutex mutex;
176};
177
Seth Forshee29cd2932012-01-18 13:44:09 -0600178static struct toshiba_acpi_dev *toshiba_acpi;
179
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800180static const struct acpi_device_id toshiba_device_ids[] = {
181 {"TOS6200", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400182 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800183 {"TOS1900", 0},
184 {"", 0},
185};
186MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
187
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800188static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100189 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700190 { KE_KEY, 0x101, { KEY_MUTE } },
191 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
192 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600193 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
194 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700195 { KE_KEY, 0x13b, { KEY_COFFEE } },
196 { KE_KEY, 0x13c, { KEY_BATTERY } },
197 { KE_KEY, 0x13d, { KEY_SLEEP } },
198 { KE_KEY, 0x13e, { KEY_SUSPEND } },
199 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
200 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
201 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
202 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600203 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100204 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700205 { KE_KEY, 0xb05, { KEY_PROG2 } },
206 { KE_KEY, 0xb06, { KEY_WWW } },
207 { KE_KEY, 0xb07, { KEY_MAIL } },
208 { KE_KEY, 0xb30, { KEY_STOP } },
209 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
210 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
211 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
212 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalosaf502832012-01-18 13:44:10 -0600213 { KE_IGNORE, 0x1430, { KEY_RESERVED } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700214 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500215};
216
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200217/* alternative keymap */
218static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
219 {
220 .matches = {
221 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
222 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
223 },
224 },
Azael Avalose6efad72014-08-04 09:21:02 -0600225 {
226 .matches = {
227 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
228 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
229 },
230 },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200231 {}
232};
233
234static const struct key_entry toshiba_acpi_alt_keymap[] = {
235 { KE_KEY, 0x157, { KEY_MUTE } },
236 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
237 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600238 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200239 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
240 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
241 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
242 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
243 { KE_KEY, 0x158, { KEY_WLAN } },
244 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
245 { KE_END, 0 },
246};
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* utility
249 */
250
Len Brown4be44fc2005-08-05 00:44:28 -0400251static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 *word = (*word & ~mask) | (mask * value);
254}
255
256/* acpi interface wrappers
257 */
258
Len Brown4be44fc2005-08-05 00:44:28 -0400259static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 acpi_status status;
262
Zhang Rui619400d2013-09-03 08:31:56 +0800263 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500264 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/* Perform a raw HCI call. Here we don't care about input or output buffer
268 * format.
269 */
Seth Forshee135740d2011-09-20 16:55:49 -0500270static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
271 const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct acpi_object_list params;
274 union acpi_object in_objs[HCI_WORDS];
275 struct acpi_buffer results;
Len Brown4be44fc2005-08-05 00:44:28 -0400276 union acpi_object out_objs[HCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 acpi_status status;
278 int i;
279
280 params.count = HCI_WORDS;
281 params.pointer = in_objs;
282 for (i = 0; i < HCI_WORDS; ++i) {
283 in_objs[i].type = ACPI_TYPE_INTEGER;
284 in_objs[i].integer.value = in[i];
285 }
286
287 results.length = sizeof(out_objs);
288 results.pointer = out_objs;
289
Seth Forshee6e02cc72011-09-20 16:55:51 -0500290 status = acpi_evaluate_object(dev->acpi_dev->handle,
291 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400292 &results);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
294 for (i = 0; i < out_objs->package.count; ++i) {
295 out[i] = out_objs->package.elements[i].integer.value;
296 }
297 }
298
299 return status;
300}
301
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400302/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 *
304 * In addition to the ACPI status, the HCI system returns a result which
305 * may be useful (such as "not supported").
306 */
307
Seth Forshee135740d2011-09-20 16:55:49 -0500308static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
309 u32 in1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
312 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500313 acpi_status status = hci_raw(dev, in, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
315 return status;
316}
317
Seth Forshee135740d2011-09-20 16:55:49 -0500318static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
319 u32 *out1, u32 *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 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 *out1 = out[2];
325 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
326 return status;
327}
328
Seth Forshee135740d2011-09-20 16:55:49 -0500329static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
330 u32 in1, u32 in2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400331{
332 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
333 u32 out[HCI_WORDS];
Seth Forshee135740d2011-09-20 16:55:49 -0500334 acpi_status status = hci_raw(dev, in, out);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400335 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
336 return status;
337}
338
Seth Forshee135740d2011-09-20 16:55:49 -0500339static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
340 u32 *out1, u32 *out2, u32 *result)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400341{
342 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 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 *out1 = out[2];
346 *out2 = out[3];
347 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
348 return status;
349}
350
Azael Avalos84a62732014-03-25 20:38:29 -0600351/* common sci tasks
352 */
353
354static int sci_open(struct toshiba_acpi_dev *dev)
355{
356 u32 in[HCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
357 u32 out[HCI_WORDS];
358 acpi_status status;
359
360 status = hci_raw(dev, in, out);
361 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
362 pr_err("ACPI call to open SCI failed\n");
363 return 0;
364 }
365
366 if (out[0] == SCI_OPEN_CLOSE_OK) {
367 return 1;
368 } else if (out[0] == SCI_ALREADY_OPEN) {
369 pr_info("Toshiba SCI already opened\n");
370 return 1;
371 } else if (out[0] == SCI_NOT_PRESENT) {
372 pr_info("Toshiba SCI is not present\n");
373 }
374
375 return 0;
376}
377
378static void sci_close(struct toshiba_acpi_dev *dev)
379{
380 u32 in[HCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
381 u32 out[HCI_WORDS];
382 acpi_status status;
383
384 status = hci_raw(dev, in, out);
385 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
386 pr_err("ACPI call to close SCI failed\n");
387 return;
388 }
389
390 if (out[0] == SCI_OPEN_CLOSE_OK)
391 return;
392 else if (out[0] == SCI_NOT_OPENED)
393 pr_info("Toshiba SCI not opened\n");
394 else if (out[0] == SCI_NOT_PRESENT)
395 pr_info("Toshiba SCI is not present\n");
396}
397
398static acpi_status sci_read(struct toshiba_acpi_dev *dev, u32 reg,
399 u32 *out1, u32 *result)
400{
401 u32 in[HCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
402 u32 out[HCI_WORDS];
403 acpi_status status = hci_raw(dev, in, out);
404 *out1 = out[2];
405 *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
406 return status;
407}
408
409static acpi_status sci_write(struct toshiba_acpi_dev *dev, u32 reg,
410 u32 in1, u32 *result)
411{
412 u32 in[HCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
413 u32 out[HCI_WORDS];
414 acpi_status status = hci_raw(dev, in, out);
415 *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
416 return status;
417}
418
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200419/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500420static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200421{
Azael Avalosfdb79082014-03-25 20:38:30 -0600422 u32 in[HCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200423 u32 out[HCI_WORDS];
424 acpi_status status;
425
Azael Avalosfdb79082014-03-25 20:38:30 -0600426 if (!sci_open(dev))
427 return 0;
428
Seth Forshee135740d2011-09-20 16:55:49 -0500429 status = hci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600430 sci_close(dev);
431 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
432 pr_err("ACPI call to query Illumination support failed\n");
433 return 0;
434 } else if (out[0] == HCI_NOT_SUPPORTED || out[1] != 1) {
Joe Perches7e334602011-03-29 15:21:52 -0700435 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200436 return 0;
437 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600438
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200439 return 1;
440}
441
442static void toshiba_illumination_set(struct led_classdev *cdev,
443 enum led_brightness brightness)
444{
Seth Forshee135740d2011-09-20 16:55:49 -0500445 struct toshiba_acpi_dev *dev = container_of(cdev,
446 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600447 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200448 acpi_status status;
449
450 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600451 if (!sci_open(dev))
452 return;
453
454 /* Switch the illumination on/off */
455 state = brightness ? 1 : 0;
456 status = sci_write(dev, SCI_ILLUMINATION, state, &result);
457 sci_close(dev);
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200458 if (ACPI_FAILURE(status)) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600459 pr_err("ACPI call for illumination failed\n");
460 return;
461 } else if (result == HCI_NOT_SUPPORTED) {
462 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200463 return;
464 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200465}
466
467static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
468{
Seth Forshee135740d2011-09-20 16:55:49 -0500469 struct toshiba_acpi_dev *dev = container_of(cdev,
470 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600471 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200472 acpi_status status;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200473
474 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600475 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200476 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200477
478 /* Check the illumination */
Azael Avalosfdb79082014-03-25 20:38:30 -0600479 status = sci_read(dev, SCI_ILLUMINATION, &state, &result);
480 sci_close(dev);
481 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
482 pr_err("ACPI call for illumination failed\n");
483 return LED_OFF;
484 } else if (result == HCI_NOT_SUPPORTED) {
485 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200486 return LED_OFF;
487 }
488
Azael Avalosfdb79082014-03-25 20:38:30 -0600489 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200490}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400491
Azael Avalos360f0f32014-03-25 20:38:31 -0600492/* KBD Illumination */
493static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
494{
495 u32 result;
496 acpi_status status;
497
498 if (!sci_open(dev))
499 return -EIO;
500
501 status = sci_write(dev, SCI_KBD_ILLUM_STATUS, time, &result);
502 sci_close(dev);
503 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
504 pr_err("ACPI call to set KBD backlight status failed\n");
505 return -EIO;
506 } else if (result == HCI_NOT_SUPPORTED) {
507 pr_info("Keyboard backlight status not supported\n");
508 return -ENODEV;
509 }
510
511 return 0;
512}
513
514static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
515{
516 u32 result;
517 acpi_status status;
518
519 if (!sci_open(dev))
520 return -EIO;
521
522 status = sci_read(dev, SCI_KBD_ILLUM_STATUS, time, &result);
523 sci_close(dev);
524 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
525 pr_err("ACPI call to get KBD backlight status failed\n");
526 return -EIO;
527 } else if (result == HCI_NOT_SUPPORTED) {
528 pr_info("Keyboard backlight status not supported\n");
529 return -ENODEV;
530 }
531
532 return 0;
533}
534
535static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
536{
537 struct toshiba_acpi_dev *dev = container_of(cdev,
538 struct toshiba_acpi_dev, kbd_led);
539 u32 state, result;
540 acpi_status status;
541
542 /* Check the keyboard backlight state */
543 status = hci_read1(dev, HCI_KBD_ILLUMINATION, &state, &result);
544 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
545 pr_err("ACPI call to get the keyboard backlight failed\n");
546 return LED_OFF;
547 } else if (result == HCI_NOT_SUPPORTED) {
548 pr_info("Keyboard backlight not supported\n");
549 return LED_OFF;
550 }
551
552 return state ? LED_FULL : LED_OFF;
553}
554
555static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
556 enum led_brightness brightness)
557{
558 struct toshiba_acpi_dev *dev = container_of(cdev,
559 struct toshiba_acpi_dev, kbd_led);
560 u32 state, result;
561 acpi_status status;
562
563 /* Set the keyboard backlight state */
564 state = brightness ? 1 : 0;
565 status = hci_write1(dev, HCI_KBD_ILLUMINATION, state, &result);
566 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
567 pr_err("ACPI call to set KBD Illumination mode failed\n");
568 return;
569 } else if (result == HCI_NOT_SUPPORTED) {
570 pr_info("Keyboard backlight not supported\n");
571 return;
572 }
573}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400574
Azael Avalos9d8658a2014-03-25 20:38:32 -0600575/* TouchPad support */
576static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
577{
578 u32 result;
579 acpi_status status;
580
581 if (!sci_open(dev))
582 return -EIO;
583
584 status = sci_write(dev, SCI_TOUCHPAD, state, &result);
585 sci_close(dev);
586 if (ACPI_FAILURE(status)) {
587 pr_err("ACPI call to set the touchpad failed\n");
588 return -EIO;
589 } else if (result == HCI_NOT_SUPPORTED) {
590 return -ENODEV;
591 }
592
593 return 0;
594}
595
596static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
597{
598 u32 result;
599 acpi_status status;
600
601 if (!sci_open(dev))
602 return -EIO;
603
604 status = sci_read(dev, SCI_TOUCHPAD, state, &result);
605 sci_close(dev);
606 if (ACPI_FAILURE(status)) {
607 pr_err("ACPI call to query the touchpad failed\n");
608 return -EIO;
609 } else if (result == HCI_NOT_SUPPORTED) {
610 return -ENODEV;
611 }
612
613 return 0;
614}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200615
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600616/* Eco Mode support */
617static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
618{
619 acpi_status status;
620 u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
621 u32 out[HCI_WORDS];
622
623 status = hci_raw(dev, in, out);
624 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
625 pr_info("ACPI call to get ECO led failed\n");
626 return 0;
627 }
628
629 return 1;
630}
631
632static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
633{
634 struct toshiba_acpi_dev *dev = container_of(cdev,
635 struct toshiba_acpi_dev, eco_led);
636 u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
637 u32 out[HCI_WORDS];
638 acpi_status status;
639
640 status = hci_raw(dev, in, out);
641 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
642 pr_err("ACPI call to get ECO led failed\n");
643 return LED_OFF;
644 }
645
646 return out[2] ? LED_FULL : LED_OFF;
647}
648
649static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
650 enum led_brightness brightness)
651{
652 struct toshiba_acpi_dev *dev = container_of(cdev,
653 struct toshiba_acpi_dev, eco_led);
654 u32 in[HCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
655 u32 out[HCI_WORDS];
656 acpi_status status;
657
658 /* Switch the Eco Mode led on/off */
659 in[2] = (brightness) ? 1 : 0;
660 status = hci_raw(dev, in, out);
661 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
662 pr_err("ACPI call to set ECO led failed\n");
663 return;
664 }
665}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400666
Azael Avalos5a2813e2014-03-25 20:38:34 -0600667/* Accelerometer support */
668static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
669{
670 u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
671 u32 out[HCI_WORDS];
672 acpi_status status;
673
674 /* Check if the accelerometer call exists,
675 * this call also serves as initialization
676 */
677 status = hci_raw(dev, in, out);
678 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
679 pr_err("ACPI call to query the accelerometer failed\n");
680 return -EIO;
681 } else if (out[0] == HCI_DATA_NOT_AVAILABLE ||
682 out[0] == HCI_NOT_INITIALIZED) {
683 pr_err("Accelerometer not initialized\n");
684 return -EIO;
685 } else if (out[0] == HCI_NOT_SUPPORTED) {
686 pr_info("Accelerometer not supported\n");
687 return -ENODEV;
688 }
689
690 return 0;
691}
692
693static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
694 u32 *xy, u32 *z)
695{
696 u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
697 u32 out[HCI_WORDS];
698 acpi_status status;
699
700 /* Check the Accelerometer status */
701 status = hci_raw(dev, in, out);
702 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
703 pr_err("ACPI call to query the accelerometer failed\n");
704 return -EIO;
705 }
706
707 *xy = out[2];
708 *z = out[4];
709
710 return 0;
711}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600712
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400713/* Bluetooth rfkill handlers */
714
Seth Forshee135740d2011-09-20 16:55:49 -0500715static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400716{
717 u32 hci_result;
718 u32 value, value2;
719
720 value = 0;
721 value2 = 0;
Seth Forshee135740d2011-09-20 16:55:49 -0500722 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400723 if (hci_result == HCI_SUCCESS)
724 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
725
726 return hci_result;
727}
728
Seth Forshee135740d2011-09-20 16:55:49 -0500729static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400730{
731 u32 hci_result;
732 u32 value, value2;
733
734 value = 0;
735 value2 = 0x0001;
Seth Forshee135740d2011-09-20 16:55:49 -0500736 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400737
738 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
739 return hci_result;
740}
741
Johannes Berg19d337d2009-06-02 13:01:37 +0200742static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400743{
Johannes Berg19d337d2009-06-02 13:01:37 +0200744 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400745 u32 result1, result2;
746 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +0200747 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400748 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400749
Johannes Berg19d337d2009-06-02 13:01:37 +0200750 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400751
752 mutex_lock(&dev->mutex);
Seth Forshee135740d2011-09-20 16:55:49 -0500753 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500754 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200755 goto out;
756 }
757
758 if (!radio_state) {
759 err = 0;
760 goto out;
761 }
762
Seth Forshee135740d2011-09-20 16:55:49 -0500763 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
764 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400765
766 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500767 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +0200768 else
769 err = 0;
770 out:
771 mutex_unlock(&dev->mutex);
772 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400773}
774
Johannes Berg19d337d2009-06-02 13:01:37 +0200775static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400776{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400777 bool new_rfk_state;
778 bool value;
779 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +0200780 struct toshiba_acpi_dev *dev = data;
781
782 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400783
Seth Forshee135740d2011-09-20 16:55:49 -0500784 hci_result = hci_get_radio_state(dev, &value);
Johannes Berg19d337d2009-06-02 13:01:37 +0200785 if (hci_result != HCI_SUCCESS) {
786 /* Can't do anything useful */
787 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -0700788 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200789 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400790
791 new_rfk_state = value;
792
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400793 mutex_unlock(&dev->mutex);
794
Johannes Berg19d337d2009-06-02 13:01:37 +0200795 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
796 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400797}
798
Johannes Berg19d337d2009-06-02 13:01:37 +0200799static const struct rfkill_ops toshiba_rfk_ops = {
800 .set_block = bt_rfkill_set_block,
801 .poll = bt_rfkill_poll,
802};
803
Akio Idehara121b7b02012-04-06 01:46:43 +0900804static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
805{
806 u32 hci_result;
807 u32 status;
808
809 hci_read1(dev, HCI_TR_BACKLIGHT, &status, &hci_result);
810 *enabled = !status;
811 return hci_result == HCI_SUCCESS ? 0 : -EIO;
812}
813
814static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
815{
816 u32 hci_result;
817 u32 value = !enable;
818
819 hci_write1(dev, HCI_TR_BACKLIGHT, value, &hci_result);
820 return hci_result == HCI_SUCCESS ? 0 : -EIO;
821}
822
Len Brown4be44fc2005-08-05 00:44:28 -0400823static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Seth Forshee62cce752012-04-19 11:23:50 -0500825static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 u32 hci_result;
828 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +0900829 int brightness = 0;
830
831 if (dev->tr_backlight_supported) {
832 bool enabled;
833 int ret = get_tr_backlight_status(dev, &enabled);
834 if (ret)
835 return ret;
836 if (enabled)
837 return 0;
838 brightness++;
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Seth Forshee135740d2011-09-20 16:55:49 -0500841 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500842 if (hci_result == HCI_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +0900843 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500844
845 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700846}
847
Seth Forshee62cce752012-04-19 11:23:50 -0500848static int get_lcd_brightness(struct backlight_device *bd)
849{
850 struct toshiba_acpi_dev *dev = bl_get_data(bd);
851 return __get_lcd_brightness(dev);
852}
853
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800854static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -0700855{
Seth Forshee135740d2011-09-20 16:55:49 -0500856 struct toshiba_acpi_dev *dev = m->private;
857 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +0900858 int levels;
Holger Machtc9263552006-10-20 14:30:29 -0700859
Seth Forshee135740d2011-09-20 16:55:49 -0500860 if (!dev->backlight_dev)
861 return -ENODEV;
862
Akio Idehara121b7b02012-04-06 01:46:43 +0900863 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -0500864 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -0700865 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800866 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +0900867 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500871 pr_err("Error reading LCD brightness\n");
872 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800873}
874
875static int lcd_proc_open(struct inode *inode, struct file *file)
876{
Al Virod9dda782013-03-31 18:16:14 -0400877 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Seth Forshee62cce752012-04-19 11:23:50 -0500880static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -0700881{
Azael Avalosf6aac652014-08-04 09:21:01 -0600882 u32 in[HCI_WORDS] = { HCI_SET, HCI_LCD_BRIGHTNESS, 0, 0, 0, 0 };
883 u32 out[HCI_WORDS];
884 acpi_status status;
Holger Machtc9263552006-10-20 14:30:29 -0700885
Akio Idehara121b7b02012-04-06 01:46:43 +0900886 if (dev->tr_backlight_supported) {
887 bool enable = !value;
888 int ret = set_tr_backlight_status(dev, enable);
889 if (ret)
890 return ret;
891 if (value)
892 value--;
893 }
894
Azael Avalosf6aac652014-08-04 09:21:01 -0600895 in[2] = value << HCI_LCD_BRIGHTNESS_SHIFT;
896 status = hci_raw(dev, in, out);
897 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
898 pr_err("ACPI call to set brightness failed");
899 return -EIO;
900 }
901 /* Extra check for "incomplete" backlight method, where the AML code
902 * doesn't check for HCI_SET or HCI_GET and returns HCI_SUCCESS,
903 * the actual brightness, and in some cases the max brightness.
904 */
905 if (out[2] > 0 || out[3] == 0xE000)
906 return -ENODEV;
907
908 return out[0] == HCI_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -0700909}
910
911static int set_lcd_status(struct backlight_device *bd)
912{
Seth Forshee135740d2011-09-20 16:55:49 -0500913 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Seth Forshee62cce752012-04-19 11:23:50 -0500914 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -0700915}
916
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800917static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
918 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Al Virod9dda782013-03-31 18:16:14 -0400920 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800921 char cmd[42];
922 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800924 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +0900925 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800927 len = min(count, sizeof(cmd) - 1);
928 if (copy_from_user(cmd, buf, len))
929 return -EFAULT;
930 cmd[len] = '\0';
931
932 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +0900933 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -0500934 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800935 if (ret == 0)
936 ret = count;
937 } else {
Holger Machtc9263552006-10-20 14:30:29 -0700938 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800939 }
Holger Machtc9263552006-10-20 14:30:29 -0700940 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800943static const struct file_operations lcd_proc_fops = {
944 .owner = THIS_MODULE,
945 .open = lcd_proc_open,
946 .read = seq_read,
947 .llseek = seq_lseek,
948 .release = single_release,
949 .write = lcd_proc_write,
950};
951
Seth Forshee36d03f92011-09-20 16:55:53 -0500952static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
953{
954 u32 hci_result;
955
956 hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
957 return hci_result == HCI_SUCCESS ? 0 : -EIO;
958}
959
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800960static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Seth Forshee135740d2011-09-20 16:55:49 -0500962 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -0500964 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Seth Forshee36d03f92011-09-20 16:55:53 -0500966 ret = get_video_status(dev, &value);
967 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
969 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400970 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800971 seq_printf(m, "lcd_out: %d\n", is_lcd);
972 seq_printf(m, "crt_out: %d\n", is_crt);
973 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975
Seth Forshee36d03f92011-09-20 16:55:53 -0500976 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800979static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Al Virod9dda782013-03-31 18:16:14 -0400981 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800982}
983
984static ssize_t video_proc_write(struct file *file, const char __user *buf,
985 size_t count, loff_t *pos)
986{
Al Virod9dda782013-03-31 18:16:14 -0400987 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800988 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -0500989 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 int value;
991 int remain = count;
992 int lcd_out = -1;
993 int crt_out = -1;
994 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +0100995 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -0800997 cmd = kmalloc(count + 1, GFP_KERNEL);
998 if (!cmd)
999 return -ENOMEM;
1000 if (copy_from_user(cmd, buf, count)) {
1001 kfree(cmd);
1002 return -EFAULT;
1003 }
1004 cmd[count] = '\0';
1005
1006 buffer = cmd;
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* scan expression. Multiple expressions may be delimited with ;
1009 *
1010 * NOTE: to keep scanning simple, invalid fields are ignored
1011 */
1012 while (remain) {
1013 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1014 lcd_out = value & 1;
1015 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1016 crt_out = value & 1;
1017 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1018 tv_out = value & 1;
1019 /* advance to one character past the next ; */
1020 do {
1021 ++buffer;
1022 --remain;
1023 }
Len Brown4be44fc2005-08-05 00:44:28 -04001024 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001027 kfree(cmd);
1028
Seth Forshee36d03f92011-09-20 16:55:53 -05001029 ret = get_video_status(dev, &video_out);
1030 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001031 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (lcd_out != -1)
1033 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1034 if (crt_out != -1)
1035 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1036 if (tv_out != -1)
1037 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1038 /* To avoid unnecessary video disruption, only write the new
1039 * video setting if something changed. */
1040 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001041 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001044 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001047static const struct file_operations video_proc_fops = {
1048 .owner = THIS_MODULE,
1049 .open = video_proc_open,
1050 .read = seq_read,
1051 .llseek = seq_lseek,
1052 .release = single_release,
1053 .write = video_proc_write,
1054};
1055
Seth Forshee36d03f92011-09-20 16:55:53 -05001056static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1057{
1058 u32 hci_result;
1059
1060 hci_read1(dev, HCI_FAN, status, &hci_result);
1061 return hci_result == HCI_SUCCESS ? 0 : -EIO;
1062}
1063
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001064static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Seth Forshee135740d2011-09-20 16:55:49 -05001066 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001067 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 u32 value;
1069
Seth Forshee36d03f92011-09-20 16:55:53 -05001070 ret = get_fan_status(dev, &value);
1071 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001072 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001073 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
Seth Forshee36d03f92011-09-20 16:55:53 -05001076 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001079static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Al Virod9dda782013-03-31 18:16:14 -04001081 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001082}
1083
1084static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1085 size_t count, loff_t *pos)
1086{
Al Virod9dda782013-03-31 18:16:14 -04001087 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001088 char cmd[42];
1089 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int value;
1091 u32 hci_result;
1092
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001093 len = min(count, sizeof(cmd) - 1);
1094 if (copy_from_user(cmd, buf, len))
1095 return -EFAULT;
1096 cmd[len] = '\0';
1097
1098 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001099 value >= 0 && value <= 1) {
Seth Forshee135740d2011-09-20 16:55:49 -05001100 hci_write1(dev, HCI_FAN, value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (hci_result != HCI_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001102 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 else
Seth Forshee135740d2011-09-20 16:55:49 -05001104 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else {
1106 return -EINVAL;
1107 }
1108
1109 return count;
1110}
1111
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001112static const struct file_operations fan_proc_fops = {
1113 .owner = THIS_MODULE,
1114 .open = fan_proc_open,
1115 .read = seq_read,
1116 .llseek = seq_lseek,
1117 .release = single_release,
1118 .write = fan_proc_write,
1119};
1120
1121static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Seth Forshee135740d2011-09-20 16:55:49 -05001123 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 u32 hci_result;
1125 u32 value;
1126
Seth Forshee11948b92011-11-16 17:37:45 -06001127 if (!dev->key_event_valid && dev->system_event_supported) {
Seth Forshee135740d2011-09-20 16:55:49 -05001128 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (hci_result == HCI_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001130 dev->key_event_valid = 1;
1131 dev->last_key_event = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 } else if (hci_result == HCI_EMPTY) {
1133 /* better luck next time */
1134 } else if (hci_result == HCI_NOT_SUPPORTED) {
1135 /* This is a workaround for an unresolved issue on
1136 * some machines where system events sporadically
1137 * become disabled. */
Seth Forshee135740d2011-09-20 16:55:49 -05001138 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
Joe Perches7e334602011-03-29 15:21:52 -07001139 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001141 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001142 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 }
1145
Seth Forshee135740d2011-09-20 16:55:49 -05001146 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1147 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001151static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Al Virod9dda782013-03-31 18:16:14 -04001153 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001154}
1155
1156static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1157 size_t count, loff_t *pos)
1158{
Al Virod9dda782013-03-31 18:16:14 -04001159 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001160 char cmd[42];
1161 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int value;
1163
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001164 len = min(count, sizeof(cmd) - 1);
1165 if (copy_from_user(cmd, buf, len))
1166 return -EFAULT;
1167 cmd[len] = '\0';
1168
1169 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
Seth Forshee135740d2011-09-20 16:55:49 -05001170 dev->key_event_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 } else {
1172 return -EINVAL;
1173 }
1174
1175 return count;
1176}
1177
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001178static const struct file_operations keys_proc_fops = {
1179 .owner = THIS_MODULE,
1180 .open = keys_proc_open,
1181 .read = seq_read,
1182 .llseek = seq_lseek,
1183 .release = single_release,
1184 .write = keys_proc_write,
1185};
1186
1187static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001189 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1190 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001194static int version_proc_open(struct inode *inode, struct file *file)
1195{
Al Virod9dda782013-03-31 18:16:14 -04001196 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001197}
1198
1199static const struct file_operations version_proc_fops = {
1200 .owner = THIS_MODULE,
1201 .open = version_proc_open,
1202 .read = seq_read,
1203 .llseek = seq_lseek,
1204 .release = single_release,
1205};
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/* proc and module init
1208 */
1209
1210#define PROC_TOSHIBA "toshiba"
1211
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001212static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Seth Forshee36d03f92011-09-20 16:55:53 -05001214 if (dev->backlight_dev)
1215 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1216 &lcd_proc_fops, dev);
1217 if (dev->video_supported)
1218 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1219 &video_proc_fops, dev);
1220 if (dev->fan_supported)
1221 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1222 &fan_proc_fops, dev);
1223 if (dev->hotkey_dev)
1224 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1225 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001226 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1227 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Seth Forshee36d03f92011-09-20 16:55:53 -05001230static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Seth Forshee36d03f92011-09-20 16:55:53 -05001232 if (dev->backlight_dev)
1233 remove_proc_entry("lcd", toshiba_proc_dir);
1234 if (dev->video_supported)
1235 remove_proc_entry("video", toshiba_proc_dir);
1236 if (dev->fan_supported)
1237 remove_proc_entry("fan", toshiba_proc_dir);
1238 if (dev->hotkey_dev)
1239 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001240 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Lionel Debrouxacc24722010-11-16 14:14:02 +01001243static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001244 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001245 .get_brightness = get_lcd_brightness,
1246 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001247};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001248
Azael Avalos360f0f32014-03-25 20:38:31 -06001249/*
1250 * Sysfs files
1251 */
1252
1253static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1254 struct device_attribute *attr,
1255 const char *buf, size_t count)
1256{
1257 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1258 int mode = -1;
1259 int time = -1;
1260
Matthew Garrett8039aab2014-08-20 08:18:18 -07001261 if (sscanf(buf, "%i", &mode) != 1 && (mode != 2 || mode != 1))
Azael Avalos360f0f32014-03-25 20:38:31 -06001262 return -EINVAL;
1263
1264 /* Set the Keyboard Backlight Mode where:
1265 * Mode - Auto (2) | FN-Z (1)
1266 * Auto - KBD backlight turns off automatically in given time
1267 * FN-Z - KBD backlight "toggles" when hotkey pressed
1268 */
1269 if (mode != -1 && toshiba->kbd_mode != mode) {
1270 time = toshiba->kbd_time << HCI_MISC_SHIFT;
1271 time = time + toshiba->kbd_mode;
1272 if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1273 return -EIO;
1274 toshiba->kbd_mode = mode;
1275 }
1276
1277 return count;
1278}
1279
1280static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1281 struct device_attribute *attr,
1282 char *buf)
1283{
1284 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1285 u32 time;
1286
1287 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1288 return -EIO;
1289
1290 return sprintf(buf, "%i\n", time & 0x07);
1291}
1292
1293static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1294 struct device_attribute *attr,
1295 const char *buf, size_t count)
1296{
1297 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1298 int time = -1;
1299
1300 if (sscanf(buf, "%i", &time) != 1 && (time < 0 || time > 60))
1301 return -EINVAL;
1302
1303 /* Set the Keyboard Backlight Timeout: 0-60 seconds */
1304 if (time != -1 && toshiba->kbd_time != time) {
1305 time = time << HCI_MISC_SHIFT;
1306 time = (toshiba->kbd_mode == SCI_KBD_MODE_AUTO) ?
1307 time + 1 : time + 2;
1308 if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1309 return -EIO;
1310 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1311 }
1312
1313 return count;
1314}
1315
1316static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1317 struct device_attribute *attr,
1318 char *buf)
1319{
1320 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1321 u32 time;
1322
1323 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1324 return -EIO;
1325
1326 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1327}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001328
Azael Avalos9d8658a2014-03-25 20:38:32 -06001329static ssize_t toshiba_touchpad_store(struct device *dev,
1330 struct device_attribute *attr,
1331 const char *buf, size_t count)
1332{
1333 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1334 int state;
1335
1336 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1337 if (sscanf(buf, "%i", &state) == 1 && (state == 0 || state == 1)) {
1338 if (toshiba_touchpad_set(toshiba, state) < 0)
1339 return -EIO;
1340 }
1341
1342 return count;
1343}
1344
1345static ssize_t toshiba_touchpad_show(struct device *dev,
1346 struct device_attribute *attr, char *buf)
1347{
1348 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1349 u32 state;
1350 int ret;
1351
1352 ret = toshiba_touchpad_get(toshiba, &state);
1353 if (ret < 0)
1354 return ret;
1355
1356 return sprintf(buf, "%i\n", state);
1357}
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001358
Azael Avalos5a2813e2014-03-25 20:38:34 -06001359static ssize_t toshiba_position_show(struct device *dev,
1360 struct device_attribute *attr, char *buf)
1361{
1362 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1363 u32 xyval, zval, tmp;
1364 u16 x, y, z;
1365 int ret;
1366
1367 xyval = zval = 0;
1368 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1369 if (ret < 0)
1370 return ret;
1371
1372 x = xyval & HCI_ACCEL_MASK;
1373 tmp = xyval >> HCI_MISC_SHIFT;
1374 y = tmp & HCI_ACCEL_MASK;
1375 z = zval & HCI_ACCEL_MASK;
1376
1377 return sprintf(buf, "%d %d %d\n", x, y, z);
1378}
Azael Avalos360f0f32014-03-25 20:38:31 -06001379
1380static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1381 toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1382static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1383 toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
Azael Avalos9d8658a2014-03-25 20:38:32 -06001384static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1385 toshiba_touchpad_show, toshiba_touchpad_store);
Azael Avalos5a2813e2014-03-25 20:38:34 -06001386static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
Azael Avalos360f0f32014-03-25 20:38:31 -06001387
1388static struct attribute *toshiba_attributes[] = {
1389 &dev_attr_kbd_backlight_mode.attr,
1390 &dev_attr_kbd_backlight_timeout.attr,
Azael Avalos9d8658a2014-03-25 20:38:32 -06001391 &dev_attr_touchpad.attr,
Azael Avalos5a2813e2014-03-25 20:38:34 -06001392 &dev_attr_position.attr,
Azael Avalos360f0f32014-03-25 20:38:31 -06001393 NULL,
1394};
1395
1396static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
1397 struct attribute *attr, int idx)
1398{
1399 struct device *dev = container_of(kobj, struct device, kobj);
1400 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
1401 bool exists = true;
1402
1403 if (attr == &dev_attr_kbd_backlight_mode.attr)
1404 exists = (drv->kbd_illum_supported) ? true : false;
1405 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
1406 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001407 else if (attr == &dev_attr_touchpad.attr)
1408 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06001409 else if (attr == &dev_attr_position.attr)
1410 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06001411
1412 return exists ? attr->mode : 0;
1413}
1414
1415static struct attribute_group toshiba_attr_group = {
1416 .is_visible = toshiba_sysfs_is_visible,
1417 .attrs = toshiba_attributes,
1418};
Holger Machtc9263552006-10-20 14:30:29 -07001419
Seth Forshee29cd2932012-01-18 13:44:09 -06001420static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
1421 struct serio *port)
1422{
1423 if (str & 0x20)
1424 return false;
1425
1426 if (unlikely(data == 0xe0))
1427 return false;
1428
1429 if ((data & 0x7f) == TOS1900_FN_SCAN) {
1430 schedule_work(&toshiba_acpi->hotkey_work);
1431 return true;
1432 }
1433
1434 return false;
1435}
1436
1437static void toshiba_acpi_hotkey_work(struct work_struct *work)
1438{
1439 acpi_handle ec_handle = ec_get_handle();
1440 acpi_status status;
1441
1442 if (!ec_handle)
1443 return;
1444
1445 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
1446 if (ACPI_FAILURE(status))
1447 pr_err("ACPI NTFY method execution failed\n");
1448}
1449
1450/*
1451 * Returns hotkey scancode, or < 0 on failure.
1452 */
1453static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
1454{
Zhang Rui74facaf2013-09-03 08:32:15 +08001455 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06001456 acpi_status status;
1457
Zhang Rui74facaf2013-09-03 08:32:15 +08001458 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
1459 NULL, &value);
1460 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06001461 pr_err("ACPI INFO method execution failed\n");
1462 return -EIO;
1463 }
1464
Zhang Rui74facaf2013-09-03 08:32:15 +08001465 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06001466}
1467
1468static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
1469 int scancode)
1470{
1471 if (scancode == 0x100)
1472 return;
1473
1474 /* act on key press; ignore key release */
1475 if (scancode & 0x80)
1476 return;
1477
1478 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
1479 pr_info("Unknown key %x\n", scancode);
1480}
1481
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001482static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001483{
Seth Forshee135740d2011-09-20 16:55:49 -05001484 acpi_status status;
Zhang Ruie2e19602013-09-03 08:32:06 +08001485 acpi_handle ec_handle;
Seth Forshee135740d2011-09-20 16:55:49 -05001486 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06001487 u32 hci_result;
Takashi Iwaife808bfb2014-04-29 15:15:38 +02001488 const struct key_entry *keymap = toshiba_acpi_keymap;
Seth Forshee135740d2011-09-20 16:55:49 -05001489
Seth Forshee135740d2011-09-20 16:55:49 -05001490 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07001491 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001492 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05001493
1494 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05001495 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05001496 dev->hotkey_dev->id.bustype = BUS_HOST;
1497
Takashi Iwaife808bfb2014-04-29 15:15:38 +02001498 if (dmi_check_system(toshiba_alt_keymap_dmi))
1499 keymap = toshiba_acpi_alt_keymap;
1500 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05001501 if (error)
1502 goto err_free_dev;
1503
Seth Forshee29cd2932012-01-18 13:44:09 -06001504 /*
1505 * For some machines the SCI responsible for providing hotkey
1506 * notification doesn't fire. We can trigger the notification
1507 * whenever the Fn key is pressed using the NTFY method, if
1508 * supported, so if it's present set up an i8042 key filter
1509 * for this purpose.
1510 */
1511 status = AE_ERROR;
1512 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08001513 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06001514 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
1515
1516 error = i8042_install_filter(toshiba_acpi_i8042_filter);
1517 if (error) {
1518 pr_err("Error installing key filter\n");
1519 goto err_free_keymap;
1520 }
1521
1522 dev->ntfy_supported = 1;
1523 }
1524
1525 /*
1526 * Determine hotkey query interface. Prefer using the INFO
1527 * method when it is available.
1528 */
Zhang Ruie2e19602013-09-03 08:32:06 +08001529 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06001530 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08001531 else {
Seth Forshee29cd2932012-01-18 13:44:09 -06001532 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
1533 if (hci_result == HCI_SUCCESS)
1534 dev->system_event_supported = 1;
1535 }
1536
1537 if (!dev->info_supported && !dev->system_event_supported) {
1538 pr_warn("No hotkey query interface found\n");
1539 goto err_remove_filter;
1540 }
1541
Seth Forshee6e02cc72011-09-20 16:55:51 -05001542 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05001543 if (ACPI_FAILURE(status)) {
1544 pr_info("Unable to enable hotkeys\n");
1545 error = -ENODEV;
Seth Forshee29cd2932012-01-18 13:44:09 -06001546 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05001547 }
1548
1549 error = input_register_device(dev->hotkey_dev);
1550 if (error) {
1551 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06001552 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05001553 }
1554
Seth Forshee29cd2932012-01-18 13:44:09 -06001555 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result);
Seth Forshee135740d2011-09-20 16:55:49 -05001556 return 0;
1557
Seth Forshee29cd2932012-01-18 13:44:09 -06001558 err_remove_filter:
1559 if (dev->ntfy_supported)
1560 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05001561 err_free_keymap:
1562 sparse_keymap_free(dev->hotkey_dev);
1563 err_free_dev:
1564 input_free_device(dev->hotkey_dev);
1565 dev->hotkey_dev = NULL;
1566 return error;
1567}
1568
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001569static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05001570{
1571 struct backlight_properties props;
1572 int brightness;
1573 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001574 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05001575
1576 /*
1577 * Some machines don't support the backlight methods at all, and
1578 * others support it read-only. Either of these is pretty useless,
1579 * so only register the backlight device if the backlight method
1580 * supports both reads and writes.
1581 */
1582 brightness = __get_lcd_brightness(dev);
1583 if (brightness < 0)
1584 return 0;
1585 ret = set_lcd_brightness(dev, brightness);
1586 if (ret) {
1587 pr_debug("Backlight method is read-only, disabling backlight support\n");
1588 return 0;
1589 }
1590
Akio Idehara121b7b02012-04-06 01:46:43 +09001591 /* Determine whether or not BIOS supports transflective backlight */
1592 ret = get_tr_backlight_status(dev, &enabled);
1593 dev->tr_backlight_supported = !ret;
1594
Matthew Garrett53039f22012-06-01 11:02:36 -04001595 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05001596 props.type = BACKLIGHT_PLATFORM;
1597 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001598
Akio Idehara121b7b02012-04-06 01:46:43 +09001599 /* adding an extra level and having 0 change to transflective mode */
1600 if (dev->tr_backlight_supported)
1601 props.max_brightness++;
1602
Seth Forshee62cce752012-04-19 11:23:50 -05001603 dev->backlight_dev = backlight_device_register("toshiba",
1604 &dev->acpi_dev->dev,
1605 dev,
1606 &toshiba_backlight_data,
1607 &props);
1608 if (IS_ERR(dev->backlight_dev)) {
1609 ret = PTR_ERR(dev->backlight_dev);
1610 pr_err("Could not register toshiba backlight device\n");
1611 dev->backlight_dev = NULL;
1612 return ret;
1613 }
1614
1615 dev->backlight_dev->props.brightness = brightness;
1616 return 0;
1617}
1618
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001619static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001620{
1621 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1622
Seth Forshee36d03f92011-09-20 16:55:53 -05001623 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001624
Azael Avalos360f0f32014-03-25 20:38:31 -06001625 if (dev->sysfs_created)
1626 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
1627 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05001628
Seth Forshee29cd2932012-01-18 13:44:09 -06001629 if (dev->ntfy_supported) {
1630 i8042_remove_filter(toshiba_acpi_i8042_filter);
1631 cancel_work_sync(&dev->hotkey_work);
1632 }
1633
Seth Forshee135740d2011-09-20 16:55:49 -05001634 if (dev->hotkey_dev) {
1635 input_unregister_device(dev->hotkey_dev);
1636 sparse_keymap_free(dev->hotkey_dev);
1637 }
1638
1639 if (dev->bt_rfk) {
1640 rfkill_unregister(dev->bt_rfk);
1641 rfkill_destroy(dev->bt_rfk);
1642 }
1643
1644 if (dev->backlight_dev)
1645 backlight_device_unregister(dev->backlight_dev);
1646
Seth Forshee36d03f92011-09-20 16:55:53 -05001647 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05001648 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001649
Azael Avalos360f0f32014-03-25 20:38:31 -06001650 if (dev->kbd_led_registered)
1651 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001652
Azael Avalosdef6c4e2014-03-25 20:38:33 -06001653 if (dev->eco_supported)
1654 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05001655
Seth Forshee29cd2932012-01-18 13:44:09 -06001656 if (toshiba_acpi)
1657 toshiba_acpi = NULL;
1658
Seth Forshee135740d2011-09-20 16:55:49 -05001659 kfree(dev);
1660
1661 return 0;
1662}
1663
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001664static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05001665{
Zhang Ruie2e19602013-09-03 08:32:06 +08001666 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05001667 return "GHCI";
1668
Zhang Ruie2e19602013-09-03 08:32:06 +08001669 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05001670 return "SPFC";
1671
1672 return NULL;
1673}
1674
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001675static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05001676{
1677 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001678 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05001679 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05001680 bool bt_present;
1681 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05001682
Seth Forshee29cd2932012-01-18 13:44:09 -06001683 if (toshiba_acpi)
1684 return -EBUSY;
1685
Seth Forshee135740d2011-09-20 16:55:49 -05001686 pr_info("Toshiba Laptop ACPI Extras version %s\n",
1687 TOSHIBA_ACPI_VERSION);
1688
Seth Forsheea540d6b2011-09-20 16:55:52 -05001689 hci_method = find_hci_method(acpi_dev->handle);
1690 if (!hci_method) {
1691 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05001692 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001693 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05001694
Seth Forshee135740d2011-09-20 16:55:49 -05001695 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1696 if (!dev)
1697 return -ENOMEM;
1698 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05001699 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05001700 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06001701 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001702
Seth Forshee6e02cc72011-09-20 16:55:51 -05001703 if (toshiba_acpi_setup_keyboard(dev))
1704 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05001705
1706 mutex_init(&dev->mutex);
1707
Seth Forshee62cce752012-04-19 11:23:50 -05001708 ret = toshiba_acpi_setup_backlight(dev);
1709 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05001710 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05001711
1712 /* Register rfkill switch for Bluetooth */
1713 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
1714 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
1715 &acpi_dev->dev,
1716 RFKILL_TYPE_BLUETOOTH,
1717 &toshiba_rfk_ops,
1718 dev);
1719 if (!dev->bt_rfk) {
1720 pr_err("unable to allocate rfkill device\n");
1721 ret = -ENOMEM;
1722 goto error;
1723 }
1724
1725 ret = rfkill_register(dev->bt_rfk);
1726 if (ret) {
1727 pr_err("unable to register rfkill device\n");
1728 rfkill_destroy(dev->bt_rfk);
1729 goto error;
1730 }
1731 }
1732
1733 if (toshiba_illumination_available(dev)) {
1734 dev->led_dev.name = "toshiba::illumination";
1735 dev->led_dev.max_brightness = 1;
1736 dev->led_dev.brightness_set = toshiba_illumination_set;
1737 dev->led_dev.brightness_get = toshiba_illumination_get;
1738 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05001739 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05001740 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001741
Azael Avalosdef6c4e2014-03-25 20:38:33 -06001742 if (toshiba_eco_mode_available(dev)) {
1743 dev->eco_led.name = "toshiba::eco_mode";
1744 dev->eco_led.max_brightness = 1;
1745 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
1746 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
1747 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
1748 dev->eco_supported = 1;
1749 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001750
Azael Avalos360f0f32014-03-25 20:38:31 -06001751 ret = toshiba_kbd_illum_status_get(dev, &dummy);
1752 if (!ret) {
1753 dev->kbd_time = dummy >> HCI_MISC_SHIFT;
1754 dev->kbd_mode = dummy & 0x07;
1755 }
1756 dev->kbd_illum_supported = !ret;
1757 /*
1758 * Only register the LED if KBD illumination is supported
1759 * and the keyboard backlight operation mode is set to FN-Z
1760 */
1761 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
1762 dev->kbd_led.name = "toshiba::kbd_backlight";
1763 dev->kbd_led.max_brightness = 1;
1764 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
1765 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
1766 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
1767 dev->kbd_led_registered = 1;
1768 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001769
Azael Avalos9d8658a2014-03-25 20:38:32 -06001770 ret = toshiba_touchpad_get(dev, &dummy);
1771 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001772
Azael Avalos5a2813e2014-03-25 20:38:34 -06001773 ret = toshiba_accelerometer_supported(dev);
1774 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05001775
Seth Forshee36d03f92011-09-20 16:55:53 -05001776 /* Determine whether or not BIOS supports fan and video interfaces */
1777
1778 ret = get_video_status(dev, &dummy);
1779 dev->video_supported = !ret;
1780
1781 ret = get_fan_status(dev, &dummy);
1782 dev->fan_supported = !ret;
1783
Azael Avalos360f0f32014-03-25 20:38:31 -06001784 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
1785 &toshiba_attr_group);
1786 if (ret) {
1787 dev->sysfs_created = 0;
1788 goto error;
1789 }
1790 dev->sysfs_created = !ret;
1791
Seth Forshee36d03f92011-09-20 16:55:53 -05001792 create_toshiba_proc_entries(dev);
1793
Seth Forshee29cd2932012-01-18 13:44:09 -06001794 toshiba_acpi = dev;
1795
Seth Forshee135740d2011-09-20 16:55:49 -05001796 return 0;
1797
1798error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001799 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001800 return ret;
1801}
1802
1803static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1804{
1805 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001806 u32 hci_result, value;
Seth Forshee11948b92011-11-16 17:37:45 -06001807 int retries = 3;
Seth Forshee29cd2932012-01-18 13:44:09 -06001808 int scancode;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001809
Seth Forshee29cd2932012-01-18 13:44:09 -06001810 if (event != 0x80)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001811 return;
Seth Forshee11948b92011-11-16 17:37:45 -06001812
Seth Forshee29cd2932012-01-18 13:44:09 -06001813 if (dev->info_supported) {
1814 scancode = toshiba_acpi_query_hotkey(dev);
1815 if (scancode < 0)
1816 pr_err("Failed to query hotkey event\n");
1817 else if (scancode != 0)
1818 toshiba_acpi_report_hotkey(dev, scancode);
1819 } else if (dev->system_event_supported) {
1820 do {
1821 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1822 switch (hci_result) {
1823 case HCI_SUCCESS:
1824 toshiba_acpi_report_hotkey(dev, (int)value);
1825 break;
1826 case HCI_NOT_SUPPORTED:
1827 /*
1828 * This is a workaround for an unresolved
1829 * issue on some machines where system events
1830 * sporadically become disabled.
1831 */
1832 hci_write1(dev, HCI_SYSTEM_EVENT, 1,
1833 &hci_result);
1834 pr_notice("Re-enabled hotkeys\n");
1835 /* fall through */
1836 default:
1837 retries--;
1838 break;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001839 }
Seth Forshee29cd2932012-01-18 13:44:09 -06001840 } while (retries && hci_result != HCI_EMPTY);
1841 }
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001842}
1843
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02001844#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001845static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06001846{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001847 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06001848 u32 result;
1849
1850 if (dev->hotkey_dev)
1851 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result);
1852
1853 return 0;
1854}
1855
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001856static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06001857{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001858 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06001859 u32 result;
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04001860 acpi_status status;
Seth Forshee29cd2932012-01-18 13:44:09 -06001861
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04001862 if (dev->hotkey_dev) {
1863 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB",
1864 NULL, NULL);
1865 if (ACPI_FAILURE(status))
1866 pr_info("Unable to re-enable hotkeys\n");
1867
Seth Forshee29cd2932012-01-18 13:44:09 -06001868 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result);
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04001869 }
Seth Forshee29cd2932012-01-18 13:44:09 -06001870
1871 return 0;
1872}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02001873#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05001874
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001875static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
1876 toshiba_acpi_suspend, toshiba_acpi_resume);
1877
Seth Forshee135740d2011-09-20 16:55:49 -05001878static struct acpi_driver toshiba_acpi_driver = {
1879 .name = "Toshiba ACPI driver",
1880 .owner = THIS_MODULE,
1881 .ids = toshiba_device_ids,
1882 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1883 .ops = {
1884 .add = toshiba_acpi_add,
1885 .remove = toshiba_acpi_remove,
1886 .notify = toshiba_acpi_notify,
1887 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02001888 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05001889};
Holger Machtc9263552006-10-20 14:30:29 -07001890
Len Brown4be44fc2005-08-05 00:44:28 -04001891static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Seth Forshee135740d2011-09-20 16:55:49 -05001893 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Seth Forsheef11f9992012-01-18 13:44:11 -06001895 /*
1896 * Machines with this WMI guid aren't supported due to bugs in
1897 * their AML. This check relies on wmi initializing before
1898 * toshiba_acpi to guarantee guids have been identified.
1899 */
1900 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
1901 return -ENODEV;
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1904 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05001905 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001906 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
Seth Forshee135740d2011-09-20 16:55:49 -05001909 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1910 if (ret) {
1911 pr_err("Failed to register ACPI driver: %d\n", ret);
1912 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07001913 }
1914
Seth Forshee135740d2011-09-20 16:55:49 -05001915 return ret;
1916}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001917
Seth Forshee135740d2011-09-20 16:55:49 -05001918static void __exit toshiba_acpi_exit(void)
1919{
1920 acpi_bus_unregister_driver(&toshiba_acpi_driver);
1921 if (toshiba_proc_dir)
1922 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925module_init(toshiba_acpi_init);
1926module_exit(toshiba_acpi_exit);