blob: 37f5f64976f1093ae646c12af9e6b280e684c37d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04005 * Copyright (C) 2008 Philip Langdale
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +02006 * Copyright (C) 2010 Pierre Ducroquet
Azael Avalos7216d702015-02-10 21:09:21 -07007 * Copyright (C) 2014-2015 Azael Avalos
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
Darren Hartc57c0fa2015-02-11 21:25:22 -080019 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
24 *
25 * Credits:
26 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27 * engineering the Windows drivers
28 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29 * Rob Miller - TV out and hotkeys help
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Joe Perches7e334602011-03-29 15:21:52 -070032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Azael Avalos7216d702015-02-10 21:09:21 -070034#define TOSHIBA_ACPI_VERSION "0.21"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define PROC_INTERFACE_VERSION 1
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/proc_fs.h>
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -080042#include <linux/seq_file.h>
Holger Machtc9263552006-10-20 14:30:29 -070043#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040044#include <linux/rfkill.h>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050045#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070046#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020047#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060049#include <linux/workqueue.h>
50#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080051#include <linux/acpi.h>
Azael Avalosb5163992015-02-10 23:43:56 -070052#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_AUTHOR("John Belmonte");
55MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
56MODULE_LICENSE("GPL");
57
Seth Forsheef11f9992012-01-18 13:44:11 -060058#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
59
Seth Forshee29cd2932012-01-18 13:44:09 -060060/* Scan code for Fn key on TOS1900 models */
61#define TOS1900_FN_SCAN 0x6e
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
65
Darren Harte0769fe2015-02-11 20:50:08 -080066/*
67 * The Toshiba configuration interface is composed of the HCI and the SCI,
Azael Avalos258c5902014-09-29 20:40:07 -060068 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 *
70 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
71 * be uniform across all their models. Ideally we would just call
72 * dedicated ACPI methods instead of using this primitive interface.
73 * However the ACPI methods seem to be incomplete in some areas (for
74 * example they allow setting, but not reading, the LCD brightness value),
75 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040076 *
Azael Avalos84a62732014-03-25 20:38:29 -060077 * SCI stands for "System Configuration Interface" which aim is to
78 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
80
Azael Avalos258c5902014-09-29 20:40:07 -060081#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/* operations */
84#define HCI_SET 0xff00
85#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060086#define SCI_OPEN 0xf100
87#define SCI_CLOSE 0xf200
88#define SCI_GET 0xf300
89#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/* return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060092#define TOS_SUCCESS 0x0000
93#define TOS_OPEN_CLOSE_OK 0x0044
94#define TOS_FAILURE 0x1000
95#define TOS_NOT_SUPPORTED 0x8000
96#define TOS_ALREADY_OPEN 0x8100
97#define TOS_NOT_OPENED 0x8200
98#define TOS_INPUT_DATA_ERROR 0x8300
99#define TOS_WRITE_PROTECTED 0x8400
100#define TOS_NOT_PRESENT 0x8600
101#define TOS_FIFO_EMPTY 0x8c00
102#define TOS_DATA_NOT_AVAILABLE 0x8d20
103#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700104#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* registers */
107#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900108#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define HCI_SYSTEM_EVENT 0x0016
110#define HCI_VIDEO_OUT 0x001c
111#define HCI_HOTKEY_EVENT 0x001e
112#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400113#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600114#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600115#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600116#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600117#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos56e6b352015-03-20 16:55:16 -0600118#define HCI_SYSTEM_INFO 0xc000
Azael Avalos35d53ce2015-02-10 21:09:19 -0700119#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600120#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700121#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600122#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700123#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700124#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600125#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700126#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600129#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600130#define HCI_HOTKEY_DISABLE 0x0b
131#define HCI_HOTKEY_ENABLE 0x09
Azael Avalosfb42d1f2015-03-20 16:55:18 -0600132#define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define HCI_LCD_BRIGHTNESS_BITS 3
134#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
135#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600136#define HCI_MISC_SHIFT 0x10
Azael Avalos56e6b352015-03-20 16:55:16 -0600137#define HCI_SYSTEM_TYPE1 0x10
138#define HCI_SYSTEM_TYPE2 0x11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define HCI_VIDEO_OUT_LCD 0x1
140#define HCI_VIDEO_OUT_CRT 0x2
141#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400142#define HCI_WIRELESS_KILL_SWITCH 0x01
143#define HCI_WIRELESS_BT_PRESENT 0x0f
144#define HCI_WIRELESS_BT_ATTACH 0x40
145#define HCI_WIRELESS_BT_POWER 0x80
Azael Avalos93f8c162014-09-12 18:50:36 -0600146#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600147#define SCI_KBD_MODE_FNZ 0x1
148#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600149#define SCI_KBD_MODE_ON 0x8
150#define SCI_KBD_MODE_OFF 0x10
151#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalose26ffe52015-01-18 18:30:22 -0700152#define SCI_USB_CHARGE_MODE_MASK 0xff
Azael Avalosc8c91842015-04-02 19:26:20 -0600153#define SCI_USB_CHARGE_DISABLED 0x00
154#define SCI_USB_CHARGE_ALTERNATE 0x09
155#define SCI_USB_CHARGE_TYPICAL 0x11
156#define SCI_USB_CHARGE_AUTO 0x21
Azael Avalos182bcaa2015-01-18 18:30:23 -0700157#define SCI_USB_CHARGE_BAT_MASK 0x7
158#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
159#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
160#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700161#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Seth Forshee135740d2011-09-20 16:55:49 -0500163struct toshiba_acpi_dev {
164 struct acpi_device *acpi_dev;
165 const char *method_hci;
166 struct rfkill *bt_rfk;
167 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600168 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500169 struct backlight_device *backlight_dev;
170 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600171 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600172 struct led_classdev eco_led;
Seth Forshee36d03f92011-09-20 16:55:53 -0500173
Seth Forshee135740d2011-09-20 16:55:49 -0500174 int force_fan;
175 int last_key_event;
176 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600177 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600178 int kbd_mode;
179 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700180 int usbsc_bat_level;
Azael Avalosc8c91842015-04-02 19:26:20 -0600181 int usbsc_mode_base;
Azael Avalosa2b34712015-03-20 16:55:17 -0600182 int hotkey_event_type;
Seth Forshee135740d2011-09-20 16:55:49 -0500183
Dan Carpenter592b7462012-01-15 14:28:20 +0300184 unsigned int illumination_supported:1;
185 unsigned int video_supported:1;
186 unsigned int fan_supported:1;
187 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600188 unsigned int ntfy_supported:1;
189 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900190 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600191 unsigned int kbd_illum_supported:1;
192 unsigned int kbd_led_registered:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600193 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600194 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600195 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700196 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700197 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700198 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700199 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700200 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700201 unsigned int usb_three_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600202 unsigned int sysfs_created:1;
Seth Forshee36d03f92011-09-20 16:55:53 -0500203
Seth Forshee135740d2011-09-20 16:55:49 -0500204 struct mutex mutex;
205};
206
Seth Forshee29cd2932012-01-18 13:44:09 -0600207static struct toshiba_acpi_dev *toshiba_acpi;
208
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800209static const struct acpi_device_id toshiba_device_ids[] = {
210 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100211 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400212 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800213 {"TOS1900", 0},
214 {"", 0},
215};
216MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
217
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800218static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100219 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700220 { KE_KEY, 0x101, { KEY_MUTE } },
221 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
222 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600223 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600224 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
225 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700226 { KE_KEY, 0x13b, { KEY_COFFEE } },
227 { KE_KEY, 0x13c, { KEY_BATTERY } },
228 { KE_KEY, 0x13d, { KEY_SLEEP } },
229 { KE_KEY, 0x13e, { KEY_SUSPEND } },
230 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
231 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
232 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
233 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600234 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100235 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700236 { KE_KEY, 0xb05, { KEY_PROG2 } },
237 { KE_KEY, 0xb06, { KEY_WWW } },
238 { KE_KEY, 0xb07, { KEY_MAIL } },
239 { KE_KEY, 0xb30, { KEY_STOP } },
240 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
241 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
242 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
243 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600244 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
245 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
246 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
247 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
248 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700249 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500250};
251
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200252static const struct key_entry toshiba_acpi_alt_keymap[] = {
253 { KE_KEY, 0x157, { KEY_MUTE } },
254 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
255 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600256 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200257 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
258 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
259 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
260 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
261 { KE_KEY, 0x158, { KEY_WLAN } },
262 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
263 { KE_END, 0 },
264};
265
Darren Harte0769fe2015-02-11 20:50:08 -0800266/*
267 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269
Azael Avalosb5163992015-02-10 23:43:56 -0700270static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 *word = (*word & ~mask) | (mask * value);
273}
274
Darren Harte0769fe2015-02-11 20:50:08 -0800275/*
276 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
278
Len Brown4be44fc2005-08-05 00:44:28 -0400279static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 acpi_status status;
282
Zhang Rui619400d2013-09-03 08:31:56 +0800283 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500284 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Darren Harte0769fe2015-02-11 20:50:08 -0800287/*
288 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600289 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
Azael Avalos258c5902014-09-29 20:40:07 -0600291static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
292 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600295 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600297 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 acpi_status status;
299 int i;
300
Azael Avalos258c5902014-09-29 20:40:07 -0600301 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600303 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 in_objs[i].type = ACPI_TYPE_INTEGER;
305 in_objs[i].integer.value = in[i];
306 }
307
308 results.length = sizeof(out_objs);
309 results.pointer = out_objs;
310
Seth Forshee6e02cc72011-09-20 16:55:51 -0500311 status = acpi_evaluate_object(dev->acpi_dev->handle,
312 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400313 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600314 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700315 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 return status;
320}
321
Darren Harte0769fe2015-02-11 20:50:08 -0800322/*
323 * Common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *
325 * In addition to the ACPI status, the HCI system returns a result which
326 * may be useful (such as "not supported").
327 */
328
Azael Avalos893f3f62014-09-29 20:40:09 -0600329static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Azael Avalos258c5902014-09-29 20:40:07 -0600331 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
332 u32 out[TCI_WORDS];
333 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600334
335 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Azael Avalos893f3f62014-09-29 20:40:09 -0600338static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Azael Avalos258c5902014-09-29 20:40:07 -0600340 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
341 u32 out[TCI_WORDS];
342 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700343
Azael Avalos893f3f62014-09-29 20:40:09 -0600344 if (ACPI_FAILURE(status))
345 return TOS_FAILURE;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600348
349 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Azael Avalos893f3f62014-09-29 20:40:09 -0600352static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400353{
Azael Avalos258c5902014-09-29 20:40:07 -0600354 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
355 u32 out[TCI_WORDS];
356 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600357
358 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400359}
360
Azael Avalosb5163992015-02-10 23:43:56 -0700361static u32 hci_read2(struct toshiba_acpi_dev *dev,
362 u32 reg, u32 *out1, u32 *out2)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400363{
Azael Avalos258c5902014-09-29 20:40:07 -0600364 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
365 u32 out[TCI_WORDS];
366 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700367
Azael Avalos893f3f62014-09-29 20:40:09 -0600368 if (ACPI_FAILURE(status))
369 return TOS_FAILURE;
370
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400371 *out1 = out[2];
372 *out2 = out[3];
Azael Avalos893f3f62014-09-29 20:40:09 -0600373
374 return out[0];
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400375}
376
Darren Harte0769fe2015-02-11 20:50:08 -0800377/*
378 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600379 */
380
381static int sci_open(struct toshiba_acpi_dev *dev)
382{
Azael Avalos258c5902014-09-29 20:40:07 -0600383 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
384 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600385 acpi_status status;
386
Azael Avalos258c5902014-09-29 20:40:07 -0600387 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600388 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600389 pr_err("ACPI call to open SCI failed\n");
390 return 0;
391 }
392
Azael Avalos1864bbc2014-09-29 20:40:08 -0600393 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600394 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600395 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600396 pr_info("Toshiba SCI already opened\n");
397 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700398 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800399 /*
400 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700401 * implemented and return 0x8000 (Not Supported), failing to
402 * register some supported features.
403 *
404 * Simply return 1 if we hit those affected laptops to make the
405 * supported features work.
406 *
407 * In the case that some laptops really do not support the SCI,
408 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
409 * and thus, not registering support for the queried feature.
410 */
411 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600412 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600413 pr_info("Toshiba SCI is not present\n");
414 }
415
416 return 0;
417}
418
419static void sci_close(struct toshiba_acpi_dev *dev)
420{
Azael Avalos258c5902014-09-29 20:40:07 -0600421 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
422 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600423 acpi_status status;
424
Azael Avalos258c5902014-09-29 20:40:07 -0600425 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600426 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalos84a62732014-03-25 20:38:29 -0600427 pr_err("ACPI call to close SCI failed\n");
428 return;
429 }
430
Azael Avalos1864bbc2014-09-29 20:40:08 -0600431 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600432 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600433 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600434 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600435 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600436 pr_info("Toshiba SCI is not present\n");
437}
438
Azael Avalos893f3f62014-09-29 20:40:09 -0600439static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600440{
Azael Avalos258c5902014-09-29 20:40:07 -0600441 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
442 u32 out[TCI_WORDS];
443 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700444
Azael Avalos893f3f62014-09-29 20:40:09 -0600445 if (ACPI_FAILURE(status))
446 return TOS_FAILURE;
447
Azael Avalos84a62732014-03-25 20:38:29 -0600448 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600449
450 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600451}
452
Azael Avalos893f3f62014-09-29 20:40:09 -0600453static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600454{
Azael Avalos258c5902014-09-29 20:40:07 -0600455 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
456 u32 out[TCI_WORDS];
457 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600458
459 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600460}
461
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200462/* Illumination support */
Seth Forshee135740d2011-09-20 16:55:49 -0500463static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200464{
Azael Avalos258c5902014-09-29 20:40:07 -0600465 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
466 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200467 acpi_status status;
468
Azael Avalosfdb79082014-03-25 20:38:30 -0600469 if (!sci_open(dev))
470 return 0;
471
Azael Avalos258c5902014-09-29 20:40:07 -0600472 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600473 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600474 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600475 pr_err("ACPI call to query Illumination support failed\n");
476 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600477 } else if (out[0] == TOS_NOT_SUPPORTED) {
Joe Perches7e334602011-03-29 15:21:52 -0700478 pr_info("Illumination device not available\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200479 return 0;
480 }
Azael Avalosfdb79082014-03-25 20:38:30 -0600481
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200482 return 1;
483}
484
485static void toshiba_illumination_set(struct led_classdev *cdev,
486 enum led_brightness brightness)
487{
Seth Forshee135740d2011-09-20 16:55:49 -0500488 struct toshiba_acpi_dev *dev = container_of(cdev,
489 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600490 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200491
492 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600493 if (!sci_open(dev))
494 return;
495
496 /* Switch the illumination on/off */
497 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600498 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600499 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600500 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600501 pr_err("ACPI call for illumination failed\n");
502 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600503 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600504 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200505 return;
506 }
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200507}
508
509static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
510{
Seth Forshee135740d2011-09-20 16:55:49 -0500511 struct toshiba_acpi_dev *dev = container_of(cdev,
512 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600513 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200514
515 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600516 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200517 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200518
519 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600520 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600521 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600522 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600523 pr_err("ACPI call for illumination failed\n");
524 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600525 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600526 pr_info("Illumination not supported\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200527 return LED_OFF;
528 }
529
Azael Avalosfdb79082014-03-25 20:38:30 -0600530 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200531}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400532
Azael Avalos360f0f32014-03-25 20:38:31 -0600533/* KBD Illumination */
Azael Avalos93f8c162014-09-12 18:50:36 -0600534static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
535{
Azael Avalos258c5902014-09-29 20:40:07 -0600536 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
537 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600538 acpi_status status;
539
540 if (!sci_open(dev))
541 return 0;
542
Azael Avalos258c5902014-09-29 20:40:07 -0600543 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600544 sci_close(dev);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600545 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600546 pr_err("ACPI call to query kbd illumination support failed\n");
547 return 0;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600548 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600549 pr_info("Keyboard illumination not available\n");
550 return 0;
551 }
552
Darren Harte0769fe2015-02-11 20:50:08 -0800553 /*
554 * Check for keyboard backlight timeout max value,
Azael Avalos93f8c162014-09-12 18:50:36 -0600555 * previous kbd backlight implementation set this to
556 * 0x3c0003, and now the new implementation set this
Darren Harte0769fe2015-02-11 20:50:08 -0800557 * to 0x3c001a, use this to distinguish between them.
Azael Avalos93f8c162014-09-12 18:50:36 -0600558 */
559 if (out[3] == SCI_KBD_TIME_MAX)
560 dev->kbd_type = 2;
561 else
562 dev->kbd_type = 1;
563 /* Get the current keyboard backlight mode */
564 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
565 /* Get the current time (1-60 seconds) */
566 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
567
568 return 1;
569}
570
Azael Avalos360f0f32014-03-25 20:38:31 -0600571static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
572{
573 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600574
575 if (!sci_open(dev))
576 return -EIO;
577
Azael Avalos893f3f62014-09-29 20:40:09 -0600578 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600579 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600580 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600581 pr_err("ACPI call to set KBD backlight status failed\n");
582 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600583 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600584 pr_info("Keyboard backlight status not supported\n");
585 return -ENODEV;
586 }
587
588 return 0;
589}
590
591static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
592{
593 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600594
595 if (!sci_open(dev))
596 return -EIO;
597
Azael Avalos893f3f62014-09-29 20:40:09 -0600598 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600599 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600600 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600601 pr_err("ACPI call to get KBD backlight status failed\n");
602 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600603 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600604 pr_info("Keyboard backlight status not supported\n");
605 return -ENODEV;
606 }
607
608 return 0;
609}
610
611static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
612{
613 struct toshiba_acpi_dev *dev = container_of(cdev,
614 struct toshiba_acpi_dev, kbd_led);
615 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600616
617 /* Check the keyboard backlight state */
Azael Avalos893f3f62014-09-29 20:40:09 -0600618 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
619 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600620 pr_err("ACPI call to get the keyboard backlight failed\n");
621 return LED_OFF;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600622 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600623 pr_info("Keyboard backlight not supported\n");
624 return LED_OFF;
625 }
626
627 return state ? LED_FULL : LED_OFF;
628}
629
630static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
631 enum led_brightness brightness)
632{
633 struct toshiba_acpi_dev *dev = container_of(cdev,
634 struct toshiba_acpi_dev, kbd_led);
635 u32 state, result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600636
637 /* Set the keyboard backlight state */
638 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600639 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
640 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600641 pr_err("ACPI call to set KBD Illumination mode failed\n");
642 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600643 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600644 pr_info("Keyboard backlight not supported\n");
645 return;
646 }
647}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400648
Azael Avalos9d8658a2014-03-25 20:38:32 -0600649/* TouchPad support */
650static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
651{
652 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600653
654 if (!sci_open(dev))
655 return -EIO;
656
Azael Avalos893f3f62014-09-29 20:40:09 -0600657 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600658 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600659 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600660 pr_err("ACPI call to set the touchpad failed\n");
661 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600662 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600663 return -ENODEV;
664 }
665
666 return 0;
667}
668
669static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
670{
671 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600672
673 if (!sci_open(dev))
674 return -EIO;
675
Azael Avalos893f3f62014-09-29 20:40:09 -0600676 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600677 sci_close(dev);
Azael Avalos893f3f62014-09-29 20:40:09 -0600678 if (result == TOS_FAILURE) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600679 pr_err("ACPI call to query the touchpad failed\n");
680 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600681 } else if (result == TOS_NOT_SUPPORTED) {
Azael Avalos9d8658a2014-03-25 20:38:32 -0600682 return -ENODEV;
683 }
684
685 return 0;
686}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200687
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600688/* Eco Mode support */
689static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
690{
691 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700692 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600693 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600694
Azael Avalos258c5902014-09-29 20:40:07 -0600695 status = tci_raw(dev, in, out);
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700696 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
697 pr_err("ACPI call to get ECO led failed\n");
698 } else if (out[0] == TOS_NOT_INSTALLED) {
699 pr_info("ECO led not installed");
700 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800701 /*
702 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700703 * LED device is present, but that we just screwed the input
704 * parameters.
705 *
706 * Let's query the status of the LED to see if we really have a
707 * success response, indicating the actual presense of the LED,
708 * bail out otherwise.
709 */
710 in[3] = 1;
711 status = tci_raw(dev, in, out);
712 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
713 pr_err("ACPI call to get ECO led failed\n");
714 else if (out[0] == TOS_SUCCESS)
715 return 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600716 }
717
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700718 return 0;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600719}
720
Azael Avalosb5163992015-02-10 23:43:56 -0700721static enum led_brightness
722toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600723{
724 struct toshiba_acpi_dev *dev = container_of(cdev,
725 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600726 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
727 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600728 acpi_status status;
729
Azael Avalos258c5902014-09-29 20:40:07 -0600730 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600731 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600732 pr_err("ACPI call to get ECO led failed\n");
733 return LED_OFF;
734 }
735
736 return out[2] ? LED_FULL : LED_OFF;
737}
738
739static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
740 enum led_brightness brightness)
741{
742 struct toshiba_acpi_dev *dev = container_of(cdev,
743 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600744 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
745 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600746 acpi_status status;
747
748 /* Switch the Eco Mode led on/off */
749 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600750 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600751 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600752 pr_err("ACPI call to set ECO led failed\n");
753 return;
754 }
755}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400756
Azael Avalos5a2813e2014-03-25 20:38:34 -0600757/* Accelerometer support */
758static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
759{
Azael Avalos258c5902014-09-29 20:40:07 -0600760 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
761 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600762 acpi_status status;
763
Darren Harte0769fe2015-02-11 20:50:08 -0800764 /*
765 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600766 * this call also serves as initialization
767 */
Azael Avalos258c5902014-09-29 20:40:07 -0600768 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600769 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600770 pr_err("ACPI call to query the accelerometer failed\n");
771 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600772 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
773 out[0] == TOS_NOT_INITIALIZED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600774 pr_err("Accelerometer not initialized\n");
775 return -EIO;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600776 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600777 pr_info("Accelerometer not supported\n");
778 return -ENODEV;
779 }
780
781 return 0;
782}
783
784static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
785 u32 *xy, u32 *z)
786{
Azael Avalos258c5902014-09-29 20:40:07 -0600787 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
788 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600789 acpi_status status;
790
791 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600792 status = tci_raw(dev, in, out);
Azael Avalos1864bbc2014-09-29 20:40:08 -0600793 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600794 pr_err("ACPI call to query the accelerometer failed\n");
795 return -EIO;
796 }
797
798 *xy = out[2];
799 *z = out[4];
800
801 return 0;
802}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600803
Azael Avalose26ffe52015-01-18 18:30:22 -0700804/* Sleep (Charge and Music) utilities support */
Azael Avalosc8c91842015-04-02 19:26:20 -0600805static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
806{
807 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
808 u32 out[TCI_WORDS];
809 acpi_status status;
810
811 /* Set the feature to "not supported" in case of error */
812 dev->usb_sleep_charge_supported = 0;
813
814 if (!sci_open(dev))
815 return;
816
817 status = tci_raw(dev, in, out);
818 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
819 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
820 sci_close(dev);
821 return;
822 } else if (out[0] == TOS_NOT_SUPPORTED) {
823 pr_info("USB Sleep and Charge not supported\n");
824 sci_close(dev);
825 return;
826 } else if (out[0] == TOS_SUCCESS) {
827 dev->usbsc_mode_base = out[4];
828 }
829
830 in[5] = SCI_USB_CHARGE_BAT_LVL;
831 status = tci_raw(dev, in, out);
832 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
833 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
834 sci_close(dev);
835 return;
836 } else if (out[0] == TOS_NOT_SUPPORTED) {
837 pr_info("USB Sleep and Charge not supported\n");
838 sci_close(dev);
839 return;
840 } else if (out[0] == TOS_SUCCESS) {
841 dev->usbsc_bat_level = out[2];
842 /*
843 * If we reach this point, it means that the laptop has support
844 * for this feature and all values are initialized.
845 * Set it as supported.
846 */
847 dev->usb_sleep_charge_supported = 1;
848 }
849
850 sci_close(dev);
851}
852
Azael Avalose26ffe52015-01-18 18:30:22 -0700853static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
854 u32 *mode)
855{
856 u32 result;
857
858 if (!sci_open(dev))
859 return -EIO;
860
861 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
862 sci_close(dev);
863 if (result == TOS_FAILURE) {
864 pr_err("ACPI call to set USB S&C mode failed\n");
865 return -EIO;
866 } else if (result == TOS_NOT_SUPPORTED) {
867 pr_info("USB Sleep and Charge not supported\n");
868 return -ENODEV;
869 } else if (result == TOS_INPUT_DATA_ERROR) {
870 return -EIO;
871 }
872
873 return 0;
874}
875
876static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
877 u32 mode)
878{
879 u32 result;
880
881 if (!sci_open(dev))
882 return -EIO;
883
884 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
885 sci_close(dev);
886 if (result == TOS_FAILURE) {
887 pr_err("ACPI call to set USB S&C mode failed\n");
888 return -EIO;
889 } else if (result == TOS_NOT_SUPPORTED) {
890 pr_info("USB Sleep and Charge not supported\n");
891 return -ENODEV;
892 } else if (result == TOS_INPUT_DATA_ERROR) {
893 return -EIO;
894 }
895
896 return 0;
897}
898
Azael Avalos182bcaa2015-01-18 18:30:23 -0700899static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
900 u32 *mode)
901{
902 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
903 u32 out[TCI_WORDS];
904 acpi_status status;
905
906 if (!sci_open(dev))
907 return -EIO;
908
909 in[5] = SCI_USB_CHARGE_BAT_LVL;
910 status = tci_raw(dev, in, out);
911 sci_close(dev);
912 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
913 pr_err("ACPI call to get USB S&C battery level failed\n");
914 return -EIO;
915 } else if (out[0] == TOS_NOT_SUPPORTED) {
916 pr_info("USB Sleep and Charge not supported\n");
917 return -ENODEV;
918 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
919 return -EIO;
920 }
921
922 *mode = out[2];
923
924 return 0;
925}
926
927static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
928 u32 mode)
929{
930 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
931 u32 out[TCI_WORDS];
932 acpi_status status;
933
934 if (!sci_open(dev))
935 return -EIO;
936
937 in[2] = mode;
938 in[5] = SCI_USB_CHARGE_BAT_LVL;
939 status = tci_raw(dev, in, out);
940 sci_close(dev);
941 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
942 pr_err("ACPI call to set USB S&C battery level failed\n");
943 return -EIO;
944 } else if (out[0] == TOS_NOT_SUPPORTED) {
945 pr_info("USB Sleep and Charge not supported\n");
946 return -ENODEV;
947 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
948 return -EIO;
949 }
950
951 return 0;
952}
953
Azael Avalosbb3fe012015-01-18 18:30:24 -0700954static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
955 u32 *state)
956{
957 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
958 u32 out[TCI_WORDS];
959 acpi_status status;
960
961 if (!sci_open(dev))
962 return -EIO;
963
964 in[5] = SCI_USB_CHARGE_RAPID_DSP;
965 status = tci_raw(dev, in, out);
966 sci_close(dev);
967 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
968 pr_err("ACPI call to get USB S&C battery level failed\n");
969 return -EIO;
970 } else if (out[0] == TOS_NOT_SUPPORTED ||
971 out[0] == TOS_INPUT_DATA_ERROR) {
972 pr_info("USB Sleep and Charge not supported\n");
973 return -ENODEV;
974 }
975
976 *state = out[2];
977
978 return 0;
979}
980
981static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
982 u32 state)
983{
984 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
985 u32 out[TCI_WORDS];
986 acpi_status status;
987
988 if (!sci_open(dev))
989 return -EIO;
990
991 in[2] = state;
992 in[5] = SCI_USB_CHARGE_RAPID_DSP;
993 status = tci_raw(dev, in, out);
994 sci_close(dev);
995 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
996 pr_err("ACPI call to set USB S&C battery level failed\n");
997 return -EIO;
998 } else if (out[0] == TOS_NOT_SUPPORTED) {
999 pr_info("USB Sleep and Charge not supported\n");
1000 return -ENODEV;
1001 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
1002 return -EIO;
1003 }
1004
1005 return 0;
1006}
1007
Azael Avalos172ce0a2015-01-18 18:30:25 -07001008static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
1009{
1010 u32 result;
1011
1012 if (!sci_open(dev))
1013 return -EIO;
1014
1015 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
1016 sci_close(dev);
1017 if (result == TOS_FAILURE) {
1018 pr_err("ACPI call to set USB S&C mode failed\n");
1019 return -EIO;
1020 } else if (result == TOS_NOT_SUPPORTED) {
1021 pr_info("USB Sleep and Charge not supported\n");
1022 return -ENODEV;
1023 } else if (result == TOS_INPUT_DATA_ERROR) {
1024 return -EIO;
1025 }
1026
1027 return 0;
1028}
1029
1030static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1031{
1032 u32 result;
1033
1034 if (!sci_open(dev))
1035 return -EIO;
1036
1037 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1038 sci_close(dev);
1039 if (result == TOS_FAILURE) {
1040 pr_err("ACPI call to set USB S&C mode failed\n");
1041 return -EIO;
1042 } else if (result == TOS_NOT_SUPPORTED) {
1043 pr_info("USB Sleep and Charge not supported\n");
1044 return -ENODEV;
1045 } else if (result == TOS_INPUT_DATA_ERROR) {
1046 return -EIO;
1047 }
1048
1049 return 0;
1050}
1051
Azael Avalosbae84192015-02-10 21:09:18 -07001052/* Keyboard function keys */
1053static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1054{
1055 u32 result;
1056
1057 if (!sci_open(dev))
1058 return -EIO;
1059
1060 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1061 sci_close(dev);
1062 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1063 pr_err("ACPI call to get KBD function keys failed\n");
1064 return -EIO;
1065 } else if (result == TOS_NOT_SUPPORTED) {
1066 pr_info("KBD function keys not supported\n");
1067 return -ENODEV;
1068 }
1069
1070 return 0;
1071}
1072
1073static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1074{
1075 u32 result;
1076
1077 if (!sci_open(dev))
1078 return -EIO;
1079
1080 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1081 sci_close(dev);
1082 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1083 pr_err("ACPI call to set KBD function keys failed\n");
1084 return -EIO;
1085 } else if (result == TOS_NOT_SUPPORTED) {
1086 pr_info("KBD function keys not supported\n");
1087 return -ENODEV;
1088 }
1089
1090 return 0;
1091}
1092
Azael Avalos35d53ce2015-02-10 21:09:19 -07001093/* Panel Power ON */
1094static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1095{
1096 u32 result;
1097
1098 if (!sci_open(dev))
1099 return -EIO;
1100
1101 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1102 sci_close(dev);
1103 if (result == TOS_FAILURE) {
1104 pr_err("ACPI call to get Panel Power ON failed\n");
1105 return -EIO;
1106 } else if (result == TOS_NOT_SUPPORTED) {
1107 pr_info("Panel Power on not supported\n");
1108 return -ENODEV;
1109 } else if (result == TOS_INPUT_DATA_ERROR) {
1110 return -EIO;
1111 }
1112
1113 return 0;
1114}
1115
1116static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1117{
1118 u32 result;
1119
1120 if (!sci_open(dev))
1121 return -EIO;
1122
1123 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1124 sci_close(dev);
1125 if (result == TOS_FAILURE) {
1126 pr_err("ACPI call to set Panel Power ON failed\n");
1127 return -EIO;
1128 } else if (result == TOS_NOT_SUPPORTED) {
1129 pr_info("Panel Power ON not supported\n");
1130 return -ENODEV;
1131 } else if (result == TOS_INPUT_DATA_ERROR) {
1132 return -EIO;
1133 }
1134
1135 return 0;
1136}
1137
Azael Avalos17fe4b32015-02-10 21:09:20 -07001138/* USB Three */
1139static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1140{
1141 u32 result;
1142
1143 if (!sci_open(dev))
1144 return -EIO;
1145
1146 result = sci_read(dev, SCI_USB_THREE, state);
1147 sci_close(dev);
1148 if (result == TOS_FAILURE) {
1149 pr_err("ACPI call to get USB 3 failed\n");
1150 return -EIO;
1151 } else if (result == TOS_NOT_SUPPORTED) {
1152 pr_info("USB 3 not supported\n");
1153 return -ENODEV;
1154 } else if (result == TOS_INPUT_DATA_ERROR) {
1155 return -EIO;
1156 }
1157
1158 return 0;
1159}
1160
1161static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1162{
1163 u32 result;
1164
1165 if (!sci_open(dev))
1166 return -EIO;
1167
1168 result = sci_write(dev, SCI_USB_THREE, state);
1169 sci_close(dev);
1170 if (result == TOS_FAILURE) {
1171 pr_err("ACPI call to set USB 3 failed\n");
1172 return -EIO;
1173 } else if (result == TOS_NOT_SUPPORTED) {
1174 pr_info("USB 3 not supported\n");
1175 return -ENODEV;
1176 } else if (result == TOS_INPUT_DATA_ERROR) {
1177 return -EIO;
1178 }
1179
1180 return 0;
1181}
1182
Azael Avalos56e6b352015-03-20 16:55:16 -06001183/* Hotkey Event type */
1184static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1185 u32 *type)
1186{
1187 u32 val1 = 0x03;
1188 u32 val2 = 0;
1189 u32 result;
1190
1191 result = hci_read2(dev, HCI_SYSTEM_INFO, &val1, &val2);
1192 if (result == TOS_FAILURE) {
1193 pr_err("ACPI call to get System type failed\n");
1194 return -EIO;
1195 } else if (result == TOS_NOT_SUPPORTED) {
1196 pr_info("System type not supported\n");
1197 return -ENODEV;
1198 }
1199
1200 *type = val2;
1201
1202 return 0;
1203}
1204
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001205/* Bluetooth rfkill handlers */
1206
Seth Forshee135740d2011-09-20 16:55:49 -05001207static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001208{
1209 u32 hci_result;
1210 u32 value, value2;
1211
1212 value = 0;
1213 value2 = 0;
Azael Avalos893f3f62014-09-29 20:40:09 -06001214 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001215 if (hci_result == TOS_SUCCESS)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001216 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
1217
1218 return hci_result;
1219}
1220
Seth Forshee135740d2011-09-20 16:55:49 -05001221static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001222{
1223 u32 hci_result;
1224 u32 value, value2;
1225
1226 value = 0;
1227 value2 = 0x0001;
Azael Avalos893f3f62014-09-29 20:40:09 -06001228 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001229
1230 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
1231 return hci_result;
1232}
1233
Johannes Berg19d337d2009-06-02 13:01:37 +02001234static int bt_rfkill_set_block(void *data, bool blocked)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001235{
Johannes Berg19d337d2009-06-02 13:01:37 +02001236 struct toshiba_acpi_dev *dev = data;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001237 u32 result1, result2;
1238 u32 value;
Johannes Berg19d337d2009-06-02 13:01:37 +02001239 int err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001240 bool radio_state;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001241
Johannes Berg19d337d2009-06-02 13:01:37 +02001242 value = (blocked == false);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001243
1244 mutex_lock(&dev->mutex);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001245 if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001246 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001247 goto out;
1248 }
1249
1250 if (!radio_state) {
1251 err = 0;
1252 goto out;
1253 }
1254
Azael Avalos893f3f62014-09-29 20:40:09 -06001255 result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
1256 result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001257
Azael Avalos1864bbc2014-09-29 20:40:08 -06001258 if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001259 err = -EIO;
Johannes Berg19d337d2009-06-02 13:01:37 +02001260 else
1261 err = 0;
1262 out:
1263 mutex_unlock(&dev->mutex);
1264 return err;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001265}
1266
Johannes Berg19d337d2009-06-02 13:01:37 +02001267static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001268{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001269 bool new_rfk_state;
1270 bool value;
1271 u32 hci_result;
Johannes Berg19d337d2009-06-02 13:01:37 +02001272 struct toshiba_acpi_dev *dev = data;
1273
1274 mutex_lock(&dev->mutex);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001275
Seth Forshee135740d2011-09-20 16:55:49 -05001276 hci_result = hci_get_radio_state(dev, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001277 if (hci_result != TOS_SUCCESS) {
Johannes Berg19d337d2009-06-02 13:01:37 +02001278 /* Can't do anything useful */
1279 mutex_unlock(&dev->mutex);
Jiri Slaby82e77842009-08-06 15:57:51 -07001280 return;
Johannes Berg19d337d2009-06-02 13:01:37 +02001281 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001282
1283 new_rfk_state = value;
1284
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001285 mutex_unlock(&dev->mutex);
1286
Johannes Berg19d337d2009-06-02 13:01:37 +02001287 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
1288 bt_rfkill_set_block(data, true);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04001289}
1290
Johannes Berg19d337d2009-06-02 13:01:37 +02001291static const struct rfkill_ops toshiba_rfk_ops = {
1292 .set_block = bt_rfkill_set_block,
1293 .poll = bt_rfkill_poll,
1294};
1295
Akio Idehara121b7b02012-04-06 01:46:43 +09001296static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1297{
1298 u32 hci_result;
1299 u32 status;
1300
Azael Avalos893f3f62014-09-29 20:40:09 -06001301 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001302 *enabled = !status;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001303 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001304}
1305
1306static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1307{
1308 u32 hci_result;
1309 u32 value = !enable;
1310
Azael Avalos893f3f62014-09-29 20:40:09 -06001311 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001312 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001313}
1314
Azael Avalosb5163992015-02-10 23:43:56 -07001315static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Seth Forshee62cce752012-04-19 11:23:50 -05001317static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 u32 hci_result;
1320 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001321 int brightness = 0;
1322
1323 if (dev->tr_backlight_supported) {
1324 bool enabled;
1325 int ret = get_tr_backlight_status(dev, &enabled);
Azael Avalosb5163992015-02-10 23:43:56 -07001326
Akio Idehara121b7b02012-04-06 01:46:43 +09001327 if (ret)
1328 return ret;
1329 if (enabled)
1330 return 0;
1331 brightness++;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Azael Avalos893f3f62014-09-29 20:40:09 -06001334 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001335 if (hci_result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001336 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001337
1338 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001339}
1340
Seth Forshee62cce752012-04-19 11:23:50 -05001341static int get_lcd_brightness(struct backlight_device *bd)
1342{
1343 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001344
Seth Forshee62cce752012-04-19 11:23:50 -05001345 return __get_lcd_brightness(dev);
1346}
1347
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001348static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001349{
Seth Forshee135740d2011-09-20 16:55:49 -05001350 struct toshiba_acpi_dev *dev = m->private;
1351 int value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001352 int levels;
Holger Machtc9263552006-10-20 14:30:29 -07001353
Seth Forshee135740d2011-09-20 16:55:49 -05001354 if (!dev->backlight_dev)
1355 return -ENODEV;
1356
Akio Idehara121b7b02012-04-06 01:46:43 +09001357 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001358 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001359 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001360 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001361 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001365 pr_err("Error reading LCD brightness\n");
1366 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001367}
1368
1369static int lcd_proc_open(struct inode *inode, struct file *file)
1370{
Al Virod9dda782013-03-31 18:16:14 -04001371 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
Seth Forshee62cce752012-04-19 11:23:50 -05001374static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001375{
Azael Avalosa39f46d2014-11-24 19:29:36 -07001376 u32 hci_result;
Holger Machtc9263552006-10-20 14:30:29 -07001377
Akio Idehara121b7b02012-04-06 01:46:43 +09001378 if (dev->tr_backlight_supported) {
1379 bool enable = !value;
1380 int ret = set_tr_backlight_status(dev, enable);
Azael Avalosb5163992015-02-10 23:43:56 -07001381
Akio Idehara121b7b02012-04-06 01:46:43 +09001382 if (ret)
1383 return ret;
1384 if (value)
1385 value--;
1386 }
1387
Azael Avalosa39f46d2014-11-24 19:29:36 -07001388 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1389 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1390 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001391}
1392
1393static int set_lcd_status(struct backlight_device *bd)
1394{
Seth Forshee135740d2011-09-20 16:55:49 -05001395 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001396
Seth Forshee62cce752012-04-19 11:23:50 -05001397 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001398}
1399
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001400static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1401 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Al Virod9dda782013-03-31 18:16:14 -04001403 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001404 char cmd[42];
1405 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001407 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09001408 int levels = dev->backlight_dev->props.max_brightness + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001410 len = min(count, sizeof(cmd) - 1);
1411 if (copy_from_user(cmd, buf, len))
1412 return -EFAULT;
1413 cmd[len] = '\0';
1414
1415 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
Akio Idehara121b7b02012-04-06 01:46:43 +09001416 value >= 0 && value < levels) {
Seth Forshee62cce752012-04-19 11:23:50 -05001417 ret = set_lcd_brightness(dev, value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001418 if (ret == 0)
1419 ret = count;
1420 } else {
Holger Machtc9263552006-10-20 14:30:29 -07001421 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -08001422 }
Holger Machtc9263552006-10-20 14:30:29 -07001423 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001426static const struct file_operations lcd_proc_fops = {
1427 .owner = THIS_MODULE,
1428 .open = lcd_proc_open,
1429 .read = seq_read,
1430 .llseek = seq_lseek,
1431 .release = single_release,
1432 .write = lcd_proc_write,
1433};
1434
Seth Forshee36d03f92011-09-20 16:55:53 -05001435static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1436{
1437 u32 hci_result;
1438
Azael Avalos893f3f62014-09-29 20:40:09 -06001439 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001440 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001441}
1442
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001443static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Seth Forshee135740d2011-09-20 16:55:49 -05001445 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 u32 value;
Seth Forshee36d03f92011-09-20 16:55:53 -05001447 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Seth Forshee36d03f92011-09-20 16:55:53 -05001449 ret = get_video_status(dev, &value);
1450 if (!ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1452 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001453 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001454
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001455 seq_printf(m, "lcd_out: %d\n", is_lcd);
1456 seq_printf(m, "crt_out: %d\n", is_crt);
1457 seq_printf(m, "tv_out: %d\n", is_tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
Seth Forshee36d03f92011-09-20 16:55:53 -05001460 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001463static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Al Virod9dda782013-03-31 18:16:14 -04001465 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001466}
1467
1468static ssize_t video_proc_write(struct file *file, const char __user *buf,
1469 size_t count, loff_t *pos)
1470{
Al Virod9dda782013-03-31 18:16:14 -04001471 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001472 char *cmd, *buffer;
Seth Forshee36d03f92011-09-20 16:55:53 -05001473 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int value;
1475 int remain = count;
1476 int lcd_out = -1;
1477 int crt_out = -1;
1478 int tv_out = -1;
Al Virob4482a42007-10-14 19:35:40 +01001479 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001481 cmd = kmalloc(count + 1, GFP_KERNEL);
1482 if (!cmd)
1483 return -ENOMEM;
1484 if (copy_from_user(cmd, buf, count)) {
1485 kfree(cmd);
1486 return -EFAULT;
1487 }
1488 cmd[count] = '\0';
1489
1490 buffer = cmd;
1491
Darren Harte0769fe2015-02-11 20:50:08 -08001492 /*
1493 * Scan expression. Multiple expressions may be delimited with ;
1494 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
1496 while (remain) {
1497 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1498 lcd_out = value & 1;
1499 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1500 crt_out = value & 1;
1501 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1502 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001503 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 do {
1505 ++buffer;
1506 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001507 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001510 kfree(cmd);
1511
Seth Forshee36d03f92011-09-20 16:55:53 -05001512 ret = get_video_status(dev, &video_out);
1513 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001514 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (lcd_out != -1)
1517 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1518 if (crt_out != -1)
1519 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1520 if (tv_out != -1)
1521 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001522 /*
1523 * To avoid unnecessary video disruption, only write the new
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 * video setting if something changed. */
1525 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001526 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001529 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001532static const struct file_operations video_proc_fops = {
1533 .owner = THIS_MODULE,
1534 .open = video_proc_open,
1535 .read = seq_read,
1536 .llseek = seq_lseek,
1537 .release = single_release,
1538 .write = video_proc_write,
1539};
1540
Seth Forshee36d03f92011-09-20 16:55:53 -05001541static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1542{
1543 u32 hci_result;
1544
Azael Avalos893f3f62014-09-29 20:40:09 -06001545 hci_result = hci_read1(dev, HCI_FAN, status);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001546 return hci_result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001547}
1548
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001549static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Seth Forshee135740d2011-09-20 16:55:49 -05001551 struct toshiba_acpi_dev *dev = m->private;
Seth Forshee36d03f92011-09-20 16:55:53 -05001552 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 u32 value;
1554
Seth Forshee36d03f92011-09-20 16:55:53 -05001555 ret = get_fan_status(dev, &value);
1556 if (!ret) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001557 seq_printf(m, "running: %d\n", (value > 0));
Seth Forshee135740d2011-09-20 16:55:49 -05001558 seq_printf(m, "force_on: %d\n", dev->force_fan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560
Seth Forshee36d03f92011-09-20 16:55:53 -05001561 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001564static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Al Virod9dda782013-03-31 18:16:14 -04001566 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001567}
1568
1569static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1570 size_t count, loff_t *pos)
1571{
Al Virod9dda782013-03-31 18:16:14 -04001572 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001573 char cmd[42];
1574 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int value;
1576 u32 hci_result;
1577
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001578 len = min(count, sizeof(cmd) - 1);
1579 if (copy_from_user(cmd, buf, len))
1580 return -EFAULT;
1581 cmd[len] = '\0';
1582
1583 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -04001584 value >= 0 && value <= 1) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001585 hci_result = hci_write1(dev, HCI_FAN, value);
Azael Avalosb5163992015-02-10 23:43:56 -07001586 if (hci_result == TOS_SUCCESS)
Seth Forshee135740d2011-09-20 16:55:49 -05001587 dev->force_fan = value;
Azael Avalosb5163992015-02-10 23:43:56 -07001588 else
1589 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 } else {
1591 return -EINVAL;
1592 }
1593
1594 return count;
1595}
1596
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001597static const struct file_operations fan_proc_fops = {
1598 .owner = THIS_MODULE,
1599 .open = fan_proc_open,
1600 .read = seq_read,
1601 .llseek = seq_lseek,
1602 .release = single_release,
1603 .write = fan_proc_write,
1604};
1605
1606static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Seth Forshee135740d2011-09-20 16:55:49 -05001608 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 u32 hci_result;
1610 u32 value;
1611
Seth Forshee11948b92011-11-16 17:37:45 -06001612 if (!dev->key_event_valid && dev->system_event_supported) {
Azael Avalos893f3f62014-09-29 20:40:09 -06001613 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
Azael Avalos1864bbc2014-09-29 20:40:08 -06001614 if (hci_result == TOS_SUCCESS) {
Seth Forshee135740d2011-09-20 16:55:49 -05001615 dev->key_event_valid = 1;
1616 dev->last_key_event = value;
Azael Avalos1864bbc2014-09-29 20:40:08 -06001617 } else if (hci_result == TOS_FIFO_EMPTY) {
Darren Harte0769fe2015-02-11 20:50:08 -08001618 /* Better luck next time */
Azael Avalos1864bbc2014-09-29 20:40:08 -06001619 } else if (hci_result == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -08001620 /*
1621 * This is a workaround for an unresolved issue on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * some machines where system events sporadically
Darren Harte0769fe2015-02-11 20:50:08 -08001623 * become disabled.
1624 */
Azael Avalos893f3f62014-09-29 20:40:09 -06001625 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Joe Perches7e334602011-03-29 15:21:52 -07001626 pr_notice("Re-enabled hotkeys\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 } else {
Joe Perches7e334602011-03-29 15:21:52 -07001628 pr_err("Error reading hotkey status\n");
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001629 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 }
1632
Seth Forshee135740d2011-09-20 16:55:49 -05001633 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1634 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001638static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Al Virod9dda782013-03-31 18:16:14 -04001640 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001641}
1642
1643static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1644 size_t count, loff_t *pos)
1645{
Al Virod9dda782013-03-31 18:16:14 -04001646 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001647 char cmd[42];
1648 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 int value;
1650
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001651 len = min(count, sizeof(cmd) - 1);
1652 if (copy_from_user(cmd, buf, len))
1653 return -EFAULT;
1654 cmd[len] = '\0';
1655
Azael Avalosb5163992015-02-10 23:43:56 -07001656 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001657 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001658 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 return count;
1662}
1663
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001664static const struct file_operations keys_proc_fops = {
1665 .owner = THIS_MODULE,
1666 .open = keys_proc_open,
1667 .read = seq_read,
1668 .llseek = seq_lseek,
1669 .release = single_release,
1670 .write = keys_proc_write,
1671};
1672
1673static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001675 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1676 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001680static int version_proc_open(struct inode *inode, struct file *file)
1681{
Al Virod9dda782013-03-31 18:16:14 -04001682 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001683}
1684
1685static const struct file_operations version_proc_fops = {
1686 .owner = THIS_MODULE,
1687 .open = version_proc_open,
1688 .read = seq_read,
1689 .llseek = seq_lseek,
1690 .release = single_release,
1691};
1692
Darren Harte0769fe2015-02-11 20:50:08 -08001693/*
1694 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
1696
1697#define PROC_TOSHIBA "toshiba"
1698
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001699static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Seth Forshee36d03f92011-09-20 16:55:53 -05001701 if (dev->backlight_dev)
1702 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1703 &lcd_proc_fops, dev);
1704 if (dev->video_supported)
1705 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1706 &video_proc_fops, dev);
1707 if (dev->fan_supported)
1708 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1709 &fan_proc_fops, dev);
1710 if (dev->hotkey_dev)
1711 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1712 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001713 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1714 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Seth Forshee36d03f92011-09-20 16:55:53 -05001717static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Seth Forshee36d03f92011-09-20 16:55:53 -05001719 if (dev->backlight_dev)
1720 remove_proc_entry("lcd", toshiba_proc_dir);
1721 if (dev->video_supported)
1722 remove_proc_entry("video", toshiba_proc_dir);
1723 if (dev->fan_supported)
1724 remove_proc_entry("fan", toshiba_proc_dir);
1725 if (dev->hotkey_dev)
1726 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001727 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Lionel Debrouxacc24722010-11-16 14:14:02 +01001730static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001731 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001732 .get_brightness = get_lcd_brightness,
1733 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001734};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001735
Azael Avalos360f0f32014-03-25 20:38:31 -06001736/*
1737 * Sysfs files
1738 */
Azael Avalos9d309842015-02-10 23:43:59 -07001739static ssize_t version_show(struct device *dev,
1740 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001741{
1742 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1743}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001744static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001745
Azael Avalos9d309842015-02-10 23:43:59 -07001746static ssize_t fan_store(struct device *dev,
1747 struct device_attribute *attr,
1748 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001749{
1750 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1751 u32 result;
1752 int state;
1753 int ret;
1754
1755 ret = kstrtoint(buf, 0, &state);
1756 if (ret)
1757 return ret;
1758
1759 if (state != 0 && state != 1)
1760 return -EINVAL;
1761
1762 result = hci_write1(toshiba, HCI_FAN, state);
1763 if (result == TOS_FAILURE)
1764 return -EIO;
1765 else if (result == TOS_NOT_SUPPORTED)
1766 return -ENODEV;
1767
1768 return count;
1769}
1770
Azael Avalos9d309842015-02-10 23:43:59 -07001771static ssize_t fan_show(struct device *dev,
1772 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001773{
1774 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1775 u32 value;
1776 int ret;
1777
1778 ret = get_fan_status(toshiba, &value);
1779 if (ret)
1780 return ret;
1781
1782 return sprintf(buf, "%d\n", value);
1783}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001784static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001785
Azael Avalos9d309842015-02-10 23:43:59 -07001786static ssize_t kbd_backlight_mode_store(struct device *dev,
1787 struct device_attribute *attr,
1788 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001789{
1790 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001791 int mode;
1792 int time;
1793 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001794
Dan Carpenteraeaac092014-09-03 14:44:37 +03001795
1796 ret = kstrtoint(buf, 0, &mode);
1797 if (ret)
1798 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001799
1800 /* Check for supported modes depending on keyboard backlight type */
1801 if (toshiba->kbd_type == 1) {
1802 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1803 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1804 return -EINVAL;
1805 } else if (toshiba->kbd_type == 2) {
1806 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1807 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1808 mode != SCI_KBD_MODE_OFF)
1809 return -EINVAL;
1810 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001811
Darren Harte0769fe2015-02-11 20:50:08 -08001812 /*
1813 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001814 * Auto - KBD backlight turns off automatically in given time
1815 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001816 * ON - KBD backlight is always on
1817 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001818 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001819
1820 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001821 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001822 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001823 time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001824
1825 /* OR the "base time" to the actual method format */
1826 if (toshiba->kbd_type == 1) {
1827 /* Type 1 requires the current mode */
1828 time |= toshiba->kbd_mode;
1829 } else if (toshiba->kbd_type == 2) {
1830 /* Type 2 requires the desired mode */
1831 time |= mode;
1832 }
1833
Dan Carpenteraeaac092014-09-03 14:44:37 +03001834 ret = toshiba_kbd_illum_status_set(toshiba, time);
1835 if (ret)
1836 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001837
Azael Avalos360f0f32014-03-25 20:38:31 -06001838 toshiba->kbd_mode = mode;
1839 }
1840
1841 return count;
1842}
1843
Azael Avalos9d309842015-02-10 23:43:59 -07001844static ssize_t kbd_backlight_mode_show(struct device *dev,
1845 struct device_attribute *attr,
1846 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001847{
1848 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1849 u32 time;
1850
1851 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1852 return -EIO;
1853
Azael Avalos93f8c162014-09-12 18:50:36 -06001854 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1855}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001856static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001857
Azael Avalos9d309842015-02-10 23:43:59 -07001858static ssize_t kbd_type_show(struct device *dev,
1859 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001860{
1861 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1862
1863 return sprintf(buf, "%d\n", toshiba->kbd_type);
1864}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001865static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001866
Azael Avalos9d309842015-02-10 23:43:59 -07001867static ssize_t available_kbd_modes_show(struct device *dev,
1868 struct device_attribute *attr,
1869 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001870{
1871 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1872
1873 if (toshiba->kbd_type == 1)
1874 return sprintf(buf, "%x %x\n",
1875 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1876
1877 return sprintf(buf, "%x %x %x\n",
1878 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001879}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001880static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001881
Azael Avalos9d309842015-02-10 23:43:59 -07001882static ssize_t kbd_backlight_timeout_store(struct device *dev,
1883 struct device_attribute *attr,
1884 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001885{
1886 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001887 int time;
1888 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001889
Azael Avaloseabde0f2014-10-04 12:02:21 -06001890 ret = kstrtoint(buf, 0, &time);
1891 if (ret)
1892 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001893
Azael Avaloseabde0f2014-10-04 12:02:21 -06001894 /* Check for supported values depending on kbd_type */
1895 if (toshiba->kbd_type == 1) {
1896 if (time < 0 || time > 60)
1897 return -EINVAL;
1898 } else if (toshiba->kbd_type == 2) {
1899 if (time < 1 || time > 60)
1900 return -EINVAL;
1901 }
1902
1903 /* Set the Keyboard Backlight Timeout */
1904
1905 /* Only make a change if the actual timeout has changed */
1906 if (toshiba->kbd_time != time) {
1907 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001908 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001909 /* OR the "base time" to the actual method format */
1910 if (toshiba->kbd_type == 1)
1911 time |= SCI_KBD_MODE_FNZ;
1912 else if (toshiba->kbd_type == 2)
1913 time |= SCI_KBD_MODE_AUTO;
1914
1915 ret = toshiba_kbd_illum_status_set(toshiba, time);
1916 if (ret)
1917 return ret;
1918
Azael Avalos360f0f32014-03-25 20:38:31 -06001919 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1920 }
1921
1922 return count;
1923}
1924
Azael Avalos9d309842015-02-10 23:43:59 -07001925static ssize_t kbd_backlight_timeout_show(struct device *dev,
1926 struct device_attribute *attr,
1927 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001928{
1929 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1930 u32 time;
1931
1932 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1933 return -EIO;
1934
1935 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1936}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001937static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001938
Azael Avalos9d309842015-02-10 23:43:59 -07001939static ssize_t touchpad_store(struct device *dev,
1940 struct device_attribute *attr,
1941 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001942{
1943 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1944 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001945 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001946
1947 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001948 ret = kstrtoint(buf, 0, &state);
1949 if (ret)
1950 return ret;
1951 if (state != 0 && state != 1)
1952 return -EINVAL;
1953
1954 ret = toshiba_touchpad_set(toshiba, state);
1955 if (ret)
1956 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001957
1958 return count;
1959}
1960
Azael Avalos9d309842015-02-10 23:43:59 -07001961static ssize_t touchpad_show(struct device *dev,
1962 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001963{
1964 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1965 u32 state;
1966 int ret;
1967
1968 ret = toshiba_touchpad_get(toshiba, &state);
1969 if (ret < 0)
1970 return ret;
1971
1972 return sprintf(buf, "%i\n", state);
1973}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001974static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001975
Azael Avalos9d309842015-02-10 23:43:59 -07001976static ssize_t position_show(struct device *dev,
1977 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001978{
1979 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1980 u32 xyval, zval, tmp;
1981 u16 x, y, z;
1982 int ret;
1983
1984 xyval = zval = 0;
1985 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1986 if (ret < 0)
1987 return ret;
1988
1989 x = xyval & HCI_ACCEL_MASK;
1990 tmp = xyval >> HCI_MISC_SHIFT;
1991 y = tmp & HCI_ACCEL_MASK;
1992 z = zval & HCI_ACCEL_MASK;
1993
1994 return sprintf(buf, "%d %d %d\n", x, y, z);
1995}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001996static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001997
Azael Avalos9d309842015-02-10 23:43:59 -07001998static ssize_t usb_sleep_charge_show(struct device *dev,
1999 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07002000{
2001 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2002 u32 mode;
2003 int ret;
2004
2005 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
2006 if (ret < 0)
2007 return ret;
2008
2009 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
2010}
2011
Azael Avalos9d309842015-02-10 23:43:59 -07002012static ssize_t usb_sleep_charge_store(struct device *dev,
2013 struct device_attribute *attr,
2014 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07002015{
2016 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2017 u32 mode;
2018 int state;
2019 int ret;
2020
2021 ret = kstrtoint(buf, 0, &state);
2022 if (ret)
2023 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002024 /*
2025 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07002026 * 0 - Disabled
2027 * 1 - Alternate (Non USB conformant devices that require more power)
2028 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06002029 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07002030 */
Azael Avalosc8c91842015-04-02 19:26:20 -06002031 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07002032 return -EINVAL;
2033
2034 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06002035 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07002036 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06002037 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07002038 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06002039 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07002040 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06002041 mode |= SCI_USB_CHARGE_AUTO;
2042 else if (state == 3)
2043 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07002044
2045 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2046 if (ret)
2047 return ret;
2048
2049 return count;
2050}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002051static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07002052
Azael Avalos182bcaa2015-01-18 18:30:23 -07002053static ssize_t sleep_functions_on_battery_show(struct device *dev,
2054 struct device_attribute *attr,
2055 char *buf)
2056{
2057 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2058 u32 state;
2059 int bat_lvl;
2060 int status;
2061 int ret;
2062 int tmp;
2063
2064 ret = toshiba_sleep_functions_status_get(toshiba, &state);
2065 if (ret < 0)
2066 return ret;
2067
2068 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2069 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2070 status = (tmp == 0x4) ? 1 : 0;
2071 /* Determine the battery level set */
2072 bat_lvl = state >> HCI_MISC_SHIFT;
2073
2074 return sprintf(buf, "%d %d\n", status, bat_lvl);
2075}
2076
2077static ssize_t sleep_functions_on_battery_store(struct device *dev,
2078 struct device_attribute *attr,
2079 const char *buf, size_t count)
2080{
2081 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2082 u32 status;
2083 int value;
2084 int ret;
2085 int tmp;
2086
2087 ret = kstrtoint(buf, 0, &value);
2088 if (ret)
2089 return ret;
2090
Darren Harte0769fe2015-02-11 20:50:08 -08002091 /*
2092 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07002093 * 0 - Disabled
2094 * 1-100 - Enabled
2095 */
2096 if (value < 0 || value > 100)
2097 return -EINVAL;
2098
2099 if (value == 0) {
2100 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2101 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2102 } else {
2103 tmp = value << HCI_MISC_SHIFT;
2104 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2105 }
2106 ret = toshiba_sleep_functions_status_set(toshiba, status);
2107 if (ret < 0)
2108 return ret;
2109
2110 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2111
2112 return count;
2113}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002114static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002115
Azael Avalos9d309842015-02-10 23:43:59 -07002116static ssize_t usb_rapid_charge_show(struct device *dev,
2117 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002118{
2119 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2120 u32 state;
2121 int ret;
2122
2123 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2124 if (ret < 0)
2125 return ret;
2126
2127 return sprintf(buf, "%d\n", state);
2128}
2129
Azael Avalos9d309842015-02-10 23:43:59 -07002130static ssize_t usb_rapid_charge_store(struct device *dev,
2131 struct device_attribute *attr,
2132 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002133{
2134 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2135 int state;
2136 int ret;
2137
2138 ret = kstrtoint(buf, 0, &state);
2139 if (ret)
2140 return ret;
2141 if (state != 0 && state != 1)
2142 return -EINVAL;
2143
2144 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2145 if (ret)
2146 return ret;
2147
2148 return count;
2149}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002150static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002151
Azael Avalos9d309842015-02-10 23:43:59 -07002152static ssize_t usb_sleep_music_show(struct device *dev,
2153 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002154{
2155 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2156 u32 state;
2157 int ret;
2158
2159 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2160 if (ret < 0)
2161 return ret;
2162
2163 return sprintf(buf, "%d\n", state);
2164}
2165
Azael Avalos9d309842015-02-10 23:43:59 -07002166static ssize_t usb_sleep_music_store(struct device *dev,
2167 struct device_attribute *attr,
2168 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002169{
2170 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2171 int state;
2172 int ret;
2173
2174 ret = kstrtoint(buf, 0, &state);
2175 if (ret)
2176 return ret;
2177 if (state != 0 && state != 1)
2178 return -EINVAL;
2179
2180 ret = toshiba_usb_sleep_music_set(toshiba, state);
2181 if (ret)
2182 return ret;
2183
2184 return count;
2185}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002186static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002187
Azael Avalos9d309842015-02-10 23:43:59 -07002188static ssize_t kbd_function_keys_show(struct device *dev,
2189 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002190{
2191 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2192 int mode;
2193 int ret;
2194
2195 ret = toshiba_function_keys_get(toshiba, &mode);
2196 if (ret < 0)
2197 return ret;
2198
2199 return sprintf(buf, "%d\n", mode);
2200}
2201
Azael Avalos9d309842015-02-10 23:43:59 -07002202static ssize_t kbd_function_keys_store(struct device *dev,
2203 struct device_attribute *attr,
2204 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002205{
2206 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2207 int mode;
2208 int ret;
2209
2210 ret = kstrtoint(buf, 0, &mode);
2211 if (ret)
2212 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002213 /*
2214 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002215 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2216 * 1 - Special functions (Opposite of the above setting)
2217 */
2218 if (mode != 0 && mode != 1)
2219 return -EINVAL;
2220
2221 ret = toshiba_function_keys_set(toshiba, mode);
2222 if (ret)
2223 return ret;
2224
2225 pr_info("Reboot for changes to KBD Function Keys to take effect");
2226
2227 return count;
2228}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002229static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002230
Azael Avalos9d309842015-02-10 23:43:59 -07002231static ssize_t panel_power_on_show(struct device *dev,
2232 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002233{
2234 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2235 u32 state;
2236 int ret;
2237
2238 ret = toshiba_panel_power_on_get(toshiba, &state);
2239 if (ret < 0)
2240 return ret;
2241
2242 return sprintf(buf, "%d\n", state);
2243}
2244
Azael Avalos9d309842015-02-10 23:43:59 -07002245static ssize_t panel_power_on_store(struct device *dev,
2246 struct device_attribute *attr,
2247 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002248{
2249 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2250 int state;
2251 int ret;
2252
2253 ret = kstrtoint(buf, 0, &state);
2254 if (ret)
2255 return ret;
2256 if (state != 0 && state != 1)
2257 return -EINVAL;
2258
2259 ret = toshiba_panel_power_on_set(toshiba, state);
2260 if (ret)
2261 return ret;
2262
2263 pr_info("Reboot for changes to Panel Power ON to take effect");
2264
2265 return count;
2266}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002267static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002268
Azael Avalos9d309842015-02-10 23:43:59 -07002269static ssize_t usb_three_show(struct device *dev,
2270 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002271{
2272 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2273 u32 state;
2274 int ret;
2275
2276 ret = toshiba_usb_three_get(toshiba, &state);
2277 if (ret < 0)
2278 return ret;
2279
2280 return sprintf(buf, "%d\n", state);
2281}
2282
Azael Avalos9d309842015-02-10 23:43:59 -07002283static ssize_t usb_three_store(struct device *dev,
2284 struct device_attribute *attr,
2285 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002286{
2287 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2288 int state;
2289 int ret;
2290
2291 ret = kstrtoint(buf, 0, &state);
2292 if (ret)
2293 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002294 /*
2295 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002296 * 0 - Disabled (Acts like a USB 2 port, saving power)
2297 * 1 - Enabled
2298 */
2299 if (state != 0 && state != 1)
2300 return -EINVAL;
2301
2302 ret = toshiba_usb_three_set(toshiba, state);
2303 if (ret)
2304 return ret;
2305
2306 pr_info("Reboot for changes to USB 3 to take effect");
2307
2308 return count;
2309}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002310static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002311
2312static struct attribute *toshiba_attributes[] = {
2313 &dev_attr_version.attr,
2314 &dev_attr_fan.attr,
2315 &dev_attr_kbd_backlight_mode.attr,
2316 &dev_attr_kbd_type.attr,
2317 &dev_attr_available_kbd_modes.attr,
2318 &dev_attr_kbd_backlight_timeout.attr,
2319 &dev_attr_touchpad.attr,
2320 &dev_attr_position.attr,
2321 &dev_attr_usb_sleep_charge.attr,
2322 &dev_attr_sleep_functions_on_battery.attr,
2323 &dev_attr_usb_rapid_charge.attr,
2324 &dev_attr_usb_sleep_music.attr,
2325 &dev_attr_kbd_function_keys.attr,
2326 &dev_attr_panel_power_on.attr,
2327 &dev_attr_usb_three.attr,
2328 NULL,
2329};
2330
Azael Avalos360f0f32014-03-25 20:38:31 -06002331static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2332 struct attribute *attr, int idx)
2333{
2334 struct device *dev = container_of(kobj, struct device, kobj);
2335 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2336 bool exists = true;
2337
Azael Avalos94477d42015-02-10 21:09:17 -07002338 if (attr == &dev_attr_fan.attr)
2339 exists = (drv->fan_supported) ? true : false;
2340 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002341 exists = (drv->kbd_illum_supported) ? true : false;
2342 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2343 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002344 else if (attr == &dev_attr_touchpad.attr)
2345 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002346 else if (attr == &dev_attr_position.attr)
2347 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002348 else if (attr == &dev_attr_usb_sleep_charge.attr)
2349 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002350 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2351 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002352 else if (attr == &dev_attr_usb_rapid_charge.attr)
2353 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002354 else if (attr == &dev_attr_usb_sleep_music.attr)
2355 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002356 else if (attr == &dev_attr_kbd_function_keys.attr)
2357 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002358 else if (attr == &dev_attr_panel_power_on.attr)
2359 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002360 else if (attr == &dev_attr_usb_three.attr)
2361 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002362
2363 return exists ? attr->mode : 0;
2364}
2365
Azael Avalos9bd12132015-02-10 23:43:58 -07002366static struct attribute_group toshiba_attr_group = {
2367 .is_visible = toshiba_sysfs_is_visible,
2368 .attrs = toshiba_attributes,
2369};
2370
Azael Avalos1f28f292014-12-04 20:22:45 -07002371/*
2372 * Hotkeys
2373 */
2374static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2375{
2376 acpi_status status;
2377 u32 result;
2378
2379 status = acpi_evaluate_object(dev->acpi_dev->handle,
2380 "ENAB", NULL, NULL);
2381 if (ACPI_FAILURE(status))
2382 return -ENODEV;
2383
2384 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2385 if (result == TOS_FAILURE)
2386 return -EIO;
2387 else if (result == TOS_NOT_SUPPORTED)
2388 return -ENODEV;
2389
2390 return 0;
2391}
2392
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002393static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2394{
2395 u32 result;
2396
2397 /*
2398 * Re-activate the hotkeys, but this time, we are using the
2399 * "Special Functions" mode.
2400 */
2401 result = hci_write1(dev, HCI_HOTKEY_EVENT,
2402 HCI_HOTKEY_SPECIAL_FUNCTIONS);
2403 if (result != TOS_SUCCESS)
2404 pr_err("Could not enable the Special Function mode\n");
2405}
2406
Seth Forshee29cd2932012-01-18 13:44:09 -06002407static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2408 struct serio *port)
2409{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002410 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002411 return false;
2412
2413 if (unlikely(data == 0xe0))
2414 return false;
2415
2416 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2417 schedule_work(&toshiba_acpi->hotkey_work);
2418 return true;
2419 }
2420
2421 return false;
2422}
2423
2424static void toshiba_acpi_hotkey_work(struct work_struct *work)
2425{
2426 acpi_handle ec_handle = ec_get_handle();
2427 acpi_status status;
2428
2429 if (!ec_handle)
2430 return;
2431
2432 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2433 if (ACPI_FAILURE(status))
2434 pr_err("ACPI NTFY method execution failed\n");
2435}
2436
2437/*
2438 * Returns hotkey scancode, or < 0 on failure.
2439 */
2440static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2441{
Zhang Rui74facaf2013-09-03 08:32:15 +08002442 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002443 acpi_status status;
2444
Zhang Rui74facaf2013-09-03 08:32:15 +08002445 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2446 NULL, &value);
2447 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002448 pr_err("ACPI INFO method execution failed\n");
2449 return -EIO;
2450 }
2451
Zhang Rui74facaf2013-09-03 08:32:15 +08002452 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002453}
2454
2455static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2456 int scancode)
2457{
2458 if (scancode == 0x100)
2459 return;
2460
Darren Harte0769fe2015-02-11 20:50:08 -08002461 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002462 if (scancode & 0x80)
2463 return;
2464
2465 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2466 pr_info("Unknown key %x\n", scancode);
2467}
2468
Azael Avalos71454d72014-12-04 20:22:46 -07002469static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2470{
2471 u32 hci_result, value;
2472 int retries = 3;
2473 int scancode;
2474
2475 if (dev->info_supported) {
2476 scancode = toshiba_acpi_query_hotkey(dev);
2477 if (scancode < 0)
2478 pr_err("Failed to query hotkey event\n");
2479 else if (scancode != 0)
2480 toshiba_acpi_report_hotkey(dev, scancode);
2481 } else if (dev->system_event_supported) {
2482 do {
2483 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2484 switch (hci_result) {
2485 case TOS_SUCCESS:
2486 toshiba_acpi_report_hotkey(dev, (int)value);
2487 break;
2488 case TOS_NOT_SUPPORTED:
2489 /*
2490 * This is a workaround for an unresolved
2491 * issue on some machines where system events
2492 * sporadically become disabled.
2493 */
2494 hci_result =
2495 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2496 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002497 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002498 default:
2499 retries--;
2500 break;
2501 }
2502 } while (retries && hci_result != TOS_FIFO_EMPTY);
2503 }
2504}
2505
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002506static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002507{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002508 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002509 acpi_handle ec_handle;
2510 u32 events_type;
2511 u32 hci_result;
2512 int error;
2513
2514 error = toshiba_acpi_enable_hotkeys(dev);
2515 if (error)
2516 return error;
2517
2518 error = toshiba_hotkey_event_type_get(dev, &events_type);
2519 if (error) {
2520 pr_err("Unable to query Hotkey Event Type\n");
2521 return error;
2522 }
2523 dev->hotkey_event_type = events_type;
Seth Forshee135740d2011-09-20 16:55:49 -05002524
Seth Forshee135740d2011-09-20 16:55:49 -05002525 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002526 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002527 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002528
2529 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002530 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002531 dev->hotkey_dev->id.bustype = BUS_HOST;
2532
Azael Avalosa2b34712015-03-20 16:55:17 -06002533 if (events_type == HCI_SYSTEM_TYPE1 ||
2534 !dev->kbd_function_keys_supported)
2535 keymap = toshiba_acpi_keymap;
2536 else if (events_type == HCI_SYSTEM_TYPE2 ||
2537 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002538 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002539 else
2540 pr_info("Unknown event type received %x\n", events_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002541 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002542 if (error)
2543 goto err_free_dev;
2544
Seth Forshee29cd2932012-01-18 13:44:09 -06002545 /*
2546 * For some machines the SCI responsible for providing hotkey
2547 * notification doesn't fire. We can trigger the notification
2548 * whenever the Fn key is pressed using the NTFY method, if
2549 * supported, so if it's present set up an i8042 key filter
2550 * for this purpose.
2551 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002552 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002553 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002554 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2555
2556 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2557 if (error) {
2558 pr_err("Error installing key filter\n");
2559 goto err_free_keymap;
2560 }
2561
2562 dev->ntfy_supported = 1;
2563 }
2564
2565 /*
2566 * Determine hotkey query interface. Prefer using the INFO
2567 * method when it is available.
2568 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002569 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002570 dev->info_supported = 1;
Zhang Ruie2e19602013-09-03 08:32:06 +08002571 else {
Azael Avalos893f3f62014-09-29 20:40:09 -06002572 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
Azael Avalos1864bbc2014-09-29 20:40:08 -06002573 if (hci_result == TOS_SUCCESS)
Seth Forshee29cd2932012-01-18 13:44:09 -06002574 dev->system_event_supported = 1;
2575 }
2576
2577 if (!dev->info_supported && !dev->system_event_supported) {
2578 pr_warn("No hotkey query interface found\n");
2579 goto err_remove_filter;
2580 }
2581
Seth Forshee135740d2011-09-20 16:55:49 -05002582 error = input_register_device(dev->hotkey_dev);
2583 if (error) {
2584 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002585 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002586 }
2587
2588 return 0;
2589
Seth Forshee29cd2932012-01-18 13:44:09 -06002590 err_remove_filter:
2591 if (dev->ntfy_supported)
2592 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002593 err_free_keymap:
2594 sparse_keymap_free(dev->hotkey_dev);
2595 err_free_dev:
2596 input_free_device(dev->hotkey_dev);
2597 dev->hotkey_dev = NULL;
2598 return error;
2599}
2600
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002601static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002602{
2603 struct backlight_properties props;
2604 int brightness;
2605 int ret;
Akio Idehara121b7b02012-04-06 01:46:43 +09002606 bool enabled;
Seth Forshee62cce752012-04-19 11:23:50 -05002607
2608 /*
2609 * Some machines don't support the backlight methods at all, and
2610 * others support it read-only. Either of these is pretty useless,
2611 * so only register the backlight device if the backlight method
2612 * supports both reads and writes.
2613 */
2614 brightness = __get_lcd_brightness(dev);
2615 if (brightness < 0)
2616 return 0;
2617 ret = set_lcd_brightness(dev, brightness);
2618 if (ret) {
2619 pr_debug("Backlight method is read-only, disabling backlight support\n");
2620 return 0;
2621 }
2622
Akio Idehara121b7b02012-04-06 01:46:43 +09002623 /* Determine whether or not BIOS supports transflective backlight */
2624 ret = get_tr_backlight_status(dev, &enabled);
2625 dev->tr_backlight_supported = !ret;
2626
Matthew Garrett53039f22012-06-01 11:02:36 -04002627 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002628 props.type = BACKLIGHT_PLATFORM;
2629 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002630
Darren Harte0769fe2015-02-11 20:50:08 -08002631 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002632 if (dev->tr_backlight_supported)
2633 props.max_brightness++;
2634
Seth Forshee62cce752012-04-19 11:23:50 -05002635 dev->backlight_dev = backlight_device_register("toshiba",
2636 &dev->acpi_dev->dev,
2637 dev,
2638 &toshiba_backlight_data,
2639 &props);
2640 if (IS_ERR(dev->backlight_dev)) {
2641 ret = PTR_ERR(dev->backlight_dev);
2642 pr_err("Could not register toshiba backlight device\n");
2643 dev->backlight_dev = NULL;
2644 return ret;
2645 }
2646
2647 dev->backlight_dev->props.brightness = brightness;
2648 return 0;
2649}
2650
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002651static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002652{
2653 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2654
Seth Forshee36d03f92011-09-20 16:55:53 -05002655 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002656
Azael Avalos360f0f32014-03-25 20:38:31 -06002657 if (dev->sysfs_created)
2658 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2659 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002660
Seth Forshee29cd2932012-01-18 13:44:09 -06002661 if (dev->ntfy_supported) {
2662 i8042_remove_filter(toshiba_acpi_i8042_filter);
2663 cancel_work_sync(&dev->hotkey_work);
2664 }
2665
Seth Forshee135740d2011-09-20 16:55:49 -05002666 if (dev->hotkey_dev) {
2667 input_unregister_device(dev->hotkey_dev);
2668 sparse_keymap_free(dev->hotkey_dev);
2669 }
2670
2671 if (dev->bt_rfk) {
2672 rfkill_unregister(dev->bt_rfk);
2673 rfkill_destroy(dev->bt_rfk);
2674 }
2675
Markus Elfring00981812014-11-24 20:30:29 +01002676 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002677
Seth Forshee36d03f92011-09-20 16:55:53 -05002678 if (dev->illumination_supported)
Seth Forshee135740d2011-09-20 16:55:49 -05002679 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002680
Azael Avalos360f0f32014-03-25 20:38:31 -06002681 if (dev->kbd_led_registered)
2682 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002683
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002684 if (dev->eco_supported)
2685 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002686
Seth Forshee29cd2932012-01-18 13:44:09 -06002687 if (toshiba_acpi)
2688 toshiba_acpi = NULL;
2689
Seth Forshee135740d2011-09-20 16:55:49 -05002690 kfree(dev);
2691
2692 return 0;
2693}
2694
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002695static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002696{
Zhang Ruie2e19602013-09-03 08:32:06 +08002697 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002698 return "GHCI";
2699
Zhang Ruie2e19602013-09-03 08:32:06 +08002700 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002701 return "SPFC";
2702
2703 return NULL;
2704}
2705
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002706static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002707{
2708 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002709 const char *hci_method;
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002710 u32 special_functions;
Seth Forshee36d03f92011-09-20 16:55:53 -05002711 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002712 bool bt_present;
2713 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002714
Seth Forshee29cd2932012-01-18 13:44:09 -06002715 if (toshiba_acpi)
2716 return -EBUSY;
2717
Seth Forshee135740d2011-09-20 16:55:49 -05002718 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2719 TOSHIBA_ACPI_VERSION);
2720
Seth Forsheea540d6b2011-09-20 16:55:52 -05002721 hci_method = find_hci_method(acpi_dev->handle);
2722 if (!hci_method) {
2723 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002724 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002725 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002726
Seth Forshee135740d2011-09-20 16:55:49 -05002727 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2728 if (!dev)
2729 return -ENOMEM;
2730 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002731 dev->method_hci = hci_method;
Seth Forshee135740d2011-09-20 16:55:49 -05002732 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002733 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002734
Azael Avalosa2b34712015-03-20 16:55:17 -06002735 /* Query the BIOS for supported features */
2736
2737 /*
2738 * The "Special Functions" are always supported by the laptops
2739 * with the new keyboard layout, query for its presence to help
2740 * determine the keymap layout to use.
2741 */
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002742 ret = toshiba_function_keys_get(dev, &special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002743 dev->kbd_function_keys_supported = !ret;
2744
Seth Forshee6e02cc72011-09-20 16:55:51 -05002745 if (toshiba_acpi_setup_keyboard(dev))
2746 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002747
2748 mutex_init(&dev->mutex);
2749
Seth Forshee62cce752012-04-19 11:23:50 -05002750 ret = toshiba_acpi_setup_backlight(dev);
2751 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002752 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002753
2754 /* Register rfkill switch for Bluetooth */
Azael Avalos1864bbc2014-09-29 20:40:08 -06002755 if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
Seth Forshee135740d2011-09-20 16:55:49 -05002756 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2757 &acpi_dev->dev,
2758 RFKILL_TYPE_BLUETOOTH,
2759 &toshiba_rfk_ops,
2760 dev);
2761 if (!dev->bt_rfk) {
2762 pr_err("unable to allocate rfkill device\n");
2763 ret = -ENOMEM;
2764 goto error;
2765 }
2766
2767 ret = rfkill_register(dev->bt_rfk);
2768 if (ret) {
2769 pr_err("unable to register rfkill device\n");
2770 rfkill_destroy(dev->bt_rfk);
2771 goto error;
2772 }
2773 }
2774
2775 if (toshiba_illumination_available(dev)) {
2776 dev->led_dev.name = "toshiba::illumination";
2777 dev->led_dev.max_brightness = 1;
2778 dev->led_dev.brightness_set = toshiba_illumination_set;
2779 dev->led_dev.brightness_get = toshiba_illumination_get;
2780 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Seth Forshee36d03f92011-09-20 16:55:53 -05002781 dev->illumination_supported = 1;
Seth Forshee135740d2011-09-20 16:55:49 -05002782 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002783
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002784 if (toshiba_eco_mode_available(dev)) {
2785 dev->eco_led.name = "toshiba::eco_mode";
2786 dev->eco_led.max_brightness = 1;
2787 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2788 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2789 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2790 dev->eco_supported = 1;
2791 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002792
Azael Avalos93f8c162014-09-12 18:50:36 -06002793 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002794 /*
2795 * Only register the LED if KBD illumination is supported
2796 * and the keyboard backlight operation mode is set to FN-Z
2797 */
2798 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2799 dev->kbd_led.name = "toshiba::kbd_backlight";
2800 dev->kbd_led.max_brightness = 1;
2801 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2802 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2803 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2804 dev->kbd_led_registered = 1;
2805 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002806
Azael Avalos9d8658a2014-03-25 20:38:32 -06002807 ret = toshiba_touchpad_get(dev, &dummy);
2808 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002809
Azael Avalos5a2813e2014-03-25 20:38:34 -06002810 ret = toshiba_accelerometer_supported(dev);
2811 dev->accelerometer_supported = !ret;
Seth Forshee135740d2011-09-20 16:55:49 -05002812
Azael Avalosc8c91842015-04-02 19:26:20 -06002813 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07002814
Azael Avalosbb3fe012015-01-18 18:30:24 -07002815 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2816 dev->usb_rapid_charge_supported = !ret;
2817
Azael Avalos172ce0a2015-01-18 18:30:25 -07002818 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2819 dev->usb_sleep_music_supported = !ret;
2820
Azael Avalos35d53ce2015-02-10 21:09:19 -07002821 ret = toshiba_panel_power_on_get(dev, &dummy);
2822 dev->panel_power_on_supported = !ret;
2823
Azael Avalos17fe4b32015-02-10 21:09:20 -07002824 ret = toshiba_usb_three_get(dev, &dummy);
2825 dev->usb_three_supported = !ret;
2826
Seth Forshee36d03f92011-09-20 16:55:53 -05002827 ret = get_video_status(dev, &dummy);
2828 dev->video_supported = !ret;
2829
2830 ret = get_fan_status(dev, &dummy);
2831 dev->fan_supported = !ret;
2832
Azael Avalosfb42d1f2015-03-20 16:55:18 -06002833 /*
2834 * Enable the "Special Functions" mode only if they are
2835 * supported and if they are activated.
2836 */
2837 if (dev->kbd_function_keys_supported && special_functions)
2838 toshiba_acpi_enable_special_functions(dev);
2839
Azael Avalos360f0f32014-03-25 20:38:31 -06002840 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2841 &toshiba_attr_group);
2842 if (ret) {
2843 dev->sysfs_created = 0;
2844 goto error;
2845 }
2846 dev->sysfs_created = !ret;
2847
Seth Forshee36d03f92011-09-20 16:55:53 -05002848 create_toshiba_proc_entries(dev);
2849
Seth Forshee29cd2932012-01-18 13:44:09 -06002850 toshiba_acpi = dev;
2851
Seth Forshee135740d2011-09-20 16:55:49 -05002852 return 0;
2853
2854error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002855 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002856 return ret;
2857}
2858
2859static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2860{
2861 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Azael Avalos80546902014-12-04 20:22:47 -07002862 int ret;
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002863
Azael Avalos71454d72014-12-04 20:22:46 -07002864 switch (event) {
2865 case 0x80: /* Hotkeys and some system events */
2866 toshiba_acpi_process_hotkeys(dev);
2867 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002868 case 0x81: /* Dock events */
2869 case 0x82:
2870 case 0x83:
2871 pr_info("Dock event received %x\n", event);
2872 break;
2873 case 0x88: /* Thermal events */
2874 pr_info("Thermal event received\n");
2875 break;
2876 case 0x8f: /* LID closed */
2877 case 0x90: /* LID is closed and Dock has been ejected */
2878 break;
2879 case 0x8c: /* SATA power events */
2880 case 0x8b:
2881 pr_info("SATA power event received %x\n", event);
2882 break;
Azael Avalos80546902014-12-04 20:22:47 -07002883 case 0x92: /* Keyboard backlight mode changed */
2884 /* Update sysfs entries */
2885 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2886 &toshiba_attr_group);
2887 if (ret)
2888 pr_err("Unable to update sysfs entries\n");
2889 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07002890 case 0x85: /* Unknown */
2891 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002892 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07002893 case 0x94: /* Unknown */
2894 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07002895 default:
2896 pr_info("Unknown event received %x\n", event);
2897 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06002898 }
Azael Avalosbab09e22015-03-06 18:14:41 -07002899
2900 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2901 dev_name(&acpi_dev->dev),
2902 event, 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002903}
2904
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002905#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002906static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002907{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002908 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06002909 u32 result;
2910
2911 if (dev->hotkey_dev)
Azael Avalos893f3f62014-09-29 20:40:09 -06002912 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Seth Forshee29cd2932012-01-18 13:44:09 -06002913
2914 return 0;
2915}
2916
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002917static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06002918{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002919 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Azael Avalos1f28f292014-12-04 20:22:45 -07002920 int error;
Seth Forshee29cd2932012-01-18 13:44:09 -06002921
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002922 if (dev->hotkey_dev) {
Azael Avalos1f28f292014-12-04 20:22:45 -07002923 error = toshiba_acpi_enable_hotkeys(dev);
2924 if (error)
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002925 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04002926 }
Seth Forshee29cd2932012-01-18 13:44:09 -06002927
2928 return 0;
2929}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02002930#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002931
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002932static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2933 toshiba_acpi_suspend, toshiba_acpi_resume);
2934
Seth Forshee135740d2011-09-20 16:55:49 -05002935static struct acpi_driver toshiba_acpi_driver = {
2936 .name = "Toshiba ACPI driver",
2937 .owner = THIS_MODULE,
2938 .ids = toshiba_device_ids,
2939 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2940 .ops = {
2941 .add = toshiba_acpi_add,
2942 .remove = toshiba_acpi_remove,
2943 .notify = toshiba_acpi_notify,
2944 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02002945 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05002946};
Holger Machtc9263552006-10-20 14:30:29 -07002947
Len Brown4be44fc2005-08-05 00:44:28 -04002948static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Seth Forshee135740d2011-09-20 16:55:49 -05002950 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Seth Forsheef11f9992012-01-18 13:44:11 -06002952 /*
2953 * Machines with this WMI guid aren't supported due to bugs in
2954 * their AML. This check relies on wmi initializing before
2955 * toshiba_acpi to guarantee guids have been identified.
2956 */
2957 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2958 return -ENODEV;
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2961 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05002962 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002963 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965
Seth Forshee135740d2011-09-20 16:55:49 -05002966 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2967 if (ret) {
2968 pr_err("Failed to register ACPI driver: %d\n", ret);
2969 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07002970 }
2971
Seth Forshee135740d2011-09-20 16:55:49 -05002972 return ret;
2973}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04002974
Seth Forshee135740d2011-09-20 16:55:49 -05002975static void __exit toshiba_acpi_exit(void)
2976{
2977 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2978 if (toshiba_proc_dir)
2979 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982module_init(toshiba_acpi_init);
2983module_exit(toshiba_acpi_exit);