blob: 73833079bac89a0b97381037b7eacef125ca15fe [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 Avalos495078f2015-07-31 21:58:16 -060034#define TOSHIBA_ACPI_VERSION "0.23"
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>
Matthew Garrett6335e4d2010-02-25 15:20:54 -050044#include <linux/input.h>
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -070045#include <linux/input/sparse-keymap.h>
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +020046#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Seth Forshee29cd2932012-01-18 13:44:09 -060048#include <linux/workqueue.h>
49#include <linux/i8042.h>
Lv Zheng8b484632013-12-03 08:49:16 +080050#include <linux/acpi.h>
Hans de Goede358d6a22015-04-21 12:01:32 +020051#include <linux/dmi.h>
Azael Avalosb5163992015-02-10 23:43:56 -070052#include <linux/uaccess.h>
Azael Avalosfc5462f2015-07-22 18:09:11 -060053#include <linux/miscdevice.h>
Azael Avalos2fdde832015-11-23 10:49:11 -070054#include <linux/rfkill.h>
Azael Avalosfc5462f2015-07-22 18:09:11 -060055#include <linux/toshiba.h>
Hans de Goede358d6a22015-04-21 12:01:32 +020056#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_AUTHOR("John Belmonte");
59MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
60MODULE_LICENSE("GPL");
61
Seth Forsheef11f9992012-01-18 13:44:11 -060062#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
63
Seth Forshee29cd2932012-01-18 13:44:09 -060064/* Scan code for Fn key on TOS1900 models */
65#define TOS1900_FN_SCAN 0x6e
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* Toshiba ACPI method paths */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
69
Darren Harte0769fe2015-02-11 20:50:08 -080070/*
71 * The Toshiba configuration interface is composed of the HCI and the SCI,
Azael Avalos258c5902014-09-29 20:40:07 -060072 * which are defined as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
75 * be uniform across all their models. Ideally we would just call
76 * dedicated ACPI methods instead of using this primitive interface.
77 * However the ACPI methods seem to be incomplete in some areas (for
78 * example they allow setting, but not reading, the LCD brightness value),
79 * so this is still useful.
Matthew Garrettea6b31f2014-04-04 14:22:34 -040080 *
Azael Avalos84a62732014-03-25 20:38:29 -060081 * SCI stands for "System Configuration Interface" which aim is to
82 * conceal differences in hardware between different models.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
Azael Avalos258c5902014-09-29 20:40:07 -060085#define TCI_WORDS 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Azael Avalos3f75bbe2015-05-06 09:35:11 -060087/* Operations */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define HCI_SET 0xff00
89#define HCI_GET 0xfe00
Azael Avalos84a62732014-03-25 20:38:29 -060090#define SCI_OPEN 0xf100
91#define SCI_CLOSE 0xf200
92#define SCI_GET 0xf300
93#define SCI_SET 0xf400
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Azael Avalos3f75bbe2015-05-06 09:35:11 -060095/* Return codes */
Azael Avalos1864bbc2014-09-29 20:40:08 -060096#define TOS_SUCCESS 0x0000
Azael Avalose1a949c2015-07-31 21:58:14 -060097#define TOS_SUCCESS2 0x0001
Azael Avalos1864bbc2014-09-29 20:40:08 -060098#define TOS_OPEN_CLOSE_OK 0x0044
99#define TOS_FAILURE 0x1000
100#define TOS_NOT_SUPPORTED 0x8000
101#define TOS_ALREADY_OPEN 0x8100
102#define TOS_NOT_OPENED 0x8200
103#define TOS_INPUT_DATA_ERROR 0x8300
104#define TOS_WRITE_PROTECTED 0x8400
105#define TOS_NOT_PRESENT 0x8600
106#define TOS_FIFO_EMPTY 0x8c00
107#define TOS_DATA_NOT_AVAILABLE 0x8d20
108#define TOS_NOT_INITIALIZED 0x8d50
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700109#define TOS_NOT_INSTALLED 0x8e00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600111/* Registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define HCI_FAN 0x0004
Akio Idehara121b7b02012-04-06 01:46:43 +0900113#define HCI_TR_BACKLIGHT 0x0005
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define HCI_SYSTEM_EVENT 0x0016
115#define HCI_VIDEO_OUT 0x001c
116#define HCI_HOTKEY_EVENT 0x001e
117#define HCI_LCD_BRIGHTNESS 0x002a
Azael Avalos6873f462015-11-23 10:49:10 -0700118#define HCI_WIRELESS 0x0056
Azael Avalos5a2813e2014-03-25 20:38:34 -0600119#define HCI_ACCELEROMETER 0x006d
Azael Avalos360f0f32014-03-25 20:38:31 -0600120#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600121#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600122#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos56e6b352015-03-20 16:55:16 -0600123#define HCI_SYSTEM_INFO 0xc000
Azael Avalos35d53ce2015-02-10 21:09:19 -0700124#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600125#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700126#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600127#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700128#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700129#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600130#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700131#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600133/* Field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600134#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600135#define HCI_HOTKEY_DISABLE 0x0b
Azael Avalos52cbae02015-09-09 11:28:20 -0600136#define HCI_HOTKEY_ENABLE 0x01
Azael Avalosfb42d1f2015-03-20 16:55:18 -0600137#define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define HCI_LCD_BRIGHTNESS_BITS 3
139#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
140#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600141#define HCI_MISC_SHIFT 0x10
Azael Avalos56e6b352015-03-20 16:55:16 -0600142#define HCI_SYSTEM_TYPE1 0x10
143#define HCI_SYSTEM_TYPE2 0x11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define HCI_VIDEO_OUT_LCD 0x1
145#define HCI_VIDEO_OUT_CRT 0x2
146#define HCI_VIDEO_OUT_TV 0x4
Azael Avalos93f8c162014-09-12 18:50:36 -0600147#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600148#define SCI_KBD_MODE_FNZ 0x1
149#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600150#define SCI_KBD_MODE_ON 0x8
151#define SCI_KBD_MODE_OFF 0x10
152#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalos6873f462015-11-23 10:49:10 -0700153#define HCI_WIRELESS_STATUS 0x1
154#define HCI_WIRELESS_WWAN 0x3
155#define HCI_WIRELESS_WWAN_STATUS 0x2000
156#define HCI_WIRELESS_WWAN_POWER 0x4000
Azael Avalose26ffe52015-01-18 18:30:22 -0700157#define SCI_USB_CHARGE_MODE_MASK 0xff
Azael Avalosc8c91842015-04-02 19:26:20 -0600158#define SCI_USB_CHARGE_DISABLED 0x00
159#define SCI_USB_CHARGE_ALTERNATE 0x09
160#define SCI_USB_CHARGE_TYPICAL 0x11
161#define SCI_USB_CHARGE_AUTO 0x21
Azael Avalos182bcaa2015-01-18 18:30:23 -0700162#define SCI_USB_CHARGE_BAT_MASK 0x7
163#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
164#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
165#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700166#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Seth Forshee135740d2011-09-20 16:55:49 -0500168struct toshiba_acpi_dev {
169 struct acpi_device *acpi_dev;
170 const char *method_hci;
Seth Forshee135740d2011-09-20 16:55:49 -0500171 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600172 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500173 struct backlight_device *backlight_dev;
174 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600175 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600176 struct led_classdev eco_led;
Azael Avalosfc5462f2015-07-22 18:09:11 -0600177 struct miscdevice miscdev;
Azael Avalos2fdde832015-11-23 10:49:11 -0700178 struct rfkill *wwan_rfk;
Seth Forshee36d03f92011-09-20 16:55:53 -0500179
Seth Forshee135740d2011-09-20 16:55:49 -0500180 int force_fan;
181 int last_key_event;
182 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600183 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600184 int kbd_mode;
185 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700186 int usbsc_bat_level;
Azael Avalosc8c91842015-04-02 19:26:20 -0600187 int usbsc_mode_base;
Azael Avalosa2b34712015-03-20 16:55:17 -0600188 int hotkey_event_type;
Seth Forshee135740d2011-09-20 16:55:49 -0500189
Dan Carpenter592b7462012-01-15 14:28:20 +0300190 unsigned int illumination_supported:1;
191 unsigned int video_supported:1;
192 unsigned int fan_supported:1;
193 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600194 unsigned int ntfy_supported:1;
195 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900196 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600197 unsigned int kbd_illum_supported:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600198 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600199 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600200 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700201 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700202 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700203 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700204 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700205 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700206 unsigned int usb_three_supported:1;
Azael Avalos6873f462015-11-23 10:49:10 -0700207 unsigned int wwan_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600208 unsigned int sysfs_created:1;
Azael Avalosb116fd002015-09-09 11:28:19 -0600209 unsigned int special_functions;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600210
Azael Avalos65e3cf92015-11-23 10:51:30 -0700211 bool kbd_event_generated;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600212 bool kbd_led_registered;
213 bool illumination_led_registered;
214 bool eco_led_registered;
Azael Avalos6873f462015-11-23 10:49:10 -0700215 bool killswitch;
Seth Forshee135740d2011-09-20 16:55:49 -0500216};
217
Seth Forshee29cd2932012-01-18 13:44:09 -0600218static struct toshiba_acpi_dev *toshiba_acpi;
219
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800220static const struct acpi_device_id toshiba_device_ids[] = {
221 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100222 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400223 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800224 {"TOS1900", 0},
225 {"", 0},
226};
227MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
228
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800229static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100230 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700231 { KE_KEY, 0x101, { KEY_MUTE } },
232 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
233 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600234 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600235 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
236 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700237 { KE_KEY, 0x13b, { KEY_COFFEE } },
238 { KE_KEY, 0x13c, { KEY_BATTERY } },
239 { KE_KEY, 0x13d, { KEY_SLEEP } },
240 { KE_KEY, 0x13e, { KEY_SUSPEND } },
241 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
242 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
243 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
244 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600245 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100246 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700247 { KE_KEY, 0xb05, { KEY_PROG2 } },
248 { KE_KEY, 0xb06, { KEY_WWW } },
249 { KE_KEY, 0xb07, { KEY_MAIL } },
250 { KE_KEY, 0xb30, { KEY_STOP } },
251 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
252 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
253 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
254 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600255 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
256 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
257 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
258 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
259 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700260 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500261};
262
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200263static const struct key_entry toshiba_acpi_alt_keymap[] = {
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200264 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
265 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600266 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200267 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200268 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
269 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
Azael Avalosd50c9002015-07-22 19:37:46 -0600270 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200271 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
Azael Avalosd50c9002015-07-22 19:37:46 -0600272 { KE_KEY, 0x157, { KEY_MUTE } },
273 { KE_KEY, 0x158, { KEY_WLAN } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200274 { KE_END, 0 },
275};
276
Darren Harte0769fe2015-02-11 20:50:08 -0800277/*
Hans de Goede358d6a22015-04-21 12:01:32 +0200278 * List of models which have a broken acpi-video backlight interface and thus
279 * need to use the toshiba (vendor) interface instead.
280 */
281static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
282 {}
283};
284
285/*
Darren Harte0769fe2015-02-11 20:50:08 -0800286 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288
Azael Avalosb5163992015-02-10 23:43:56 -0700289static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 *word = (*word & ~mask) | (mask * value);
292}
293
Darren Harte0769fe2015-02-11 20:50:08 -0800294/*
295 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
297
Len Brown4be44fc2005-08-05 00:44:28 -0400298static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 acpi_status status;
301
Zhang Rui619400d2013-09-03 08:31:56 +0800302 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500303 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Darren Harte0769fe2015-02-11 20:50:08 -0800306/*
307 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600308 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Azael Avalos258c5902014-09-29 20:40:07 -0600310static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
311 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600314 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600316 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 acpi_status status;
318 int i;
319
Azael Avalos258c5902014-09-29 20:40:07 -0600320 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600322 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 in_objs[i].type = ACPI_TYPE_INTEGER;
324 in_objs[i].integer.value = in[i];
325 }
326
327 results.length = sizeof(out_objs);
328 results.pointer = out_objs;
329
Seth Forshee6e02cc72011-09-20 16:55:51 -0500330 status = acpi_evaluate_object(dev->acpi_dev->handle,
331 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400332 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600333 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700334 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
338 return status;
339}
340
Darren Harte0769fe2015-02-11 20:50:08 -0800341/*
Azael Avalosd37782b2015-05-06 09:35:10 -0600342 * Common hci tasks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *
344 * In addition to the ACPI status, the HCI system returns a result which
345 * may be useful (such as "not supported").
346 */
347
Azael Avalosd37782b2015-05-06 09:35:10 -0600348static u32 hci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Azael Avalos258c5902014-09-29 20:40:07 -0600350 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
351 u32 out[TCI_WORDS];
352 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600353
354 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Azael Avalosd37782b2015-05-06 09:35:10 -0600357static u32 hci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Azael Avalos258c5902014-09-29 20:40:07 -0600359 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
360 u32 out[TCI_WORDS];
361 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700362
Azael Avalos893f3f62014-09-29 20:40:09 -0600363 if (ACPI_FAILURE(status))
364 return TOS_FAILURE;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600367
368 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Darren Harte0769fe2015-02-11 20:50:08 -0800371/*
372 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600373 */
374
375static int sci_open(struct toshiba_acpi_dev *dev)
376{
Azael Avalos258c5902014-09-29 20:40:07 -0600377 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
378 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600379 acpi_status status;
380
Azael Avalos258c5902014-09-29 20:40:07 -0600381 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600382 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600383 pr_err("ACPI call to open SCI failed\n");
384 return 0;
385 }
386
Azael Avalos1864bbc2014-09-29 20:40:08 -0600387 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600388 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600389 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600390 pr_info("Toshiba SCI already opened\n");
391 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700392 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800393 /*
394 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700395 * implemented and return 0x8000 (Not Supported), failing to
396 * register some supported features.
397 *
398 * Simply return 1 if we hit those affected laptops to make the
399 * supported features work.
400 *
401 * In the case that some laptops really do not support the SCI,
402 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
403 * and thus, not registering support for the queried feature.
404 */
405 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600406 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600407 pr_info("Toshiba SCI is not present\n");
408 }
409
410 return 0;
411}
412
413static void sci_close(struct toshiba_acpi_dev *dev)
414{
Azael Avalos258c5902014-09-29 20:40:07 -0600415 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
416 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600417 acpi_status status;
418
Azael Avalos258c5902014-09-29 20:40:07 -0600419 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600420 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600421 pr_err("ACPI call to close SCI failed\n");
422 return;
423 }
424
Azael Avalos1864bbc2014-09-29 20:40:08 -0600425 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600426 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600427 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600428 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600429 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600430 pr_info("Toshiba SCI is not present\n");
431}
432
Azael Avalos893f3f62014-09-29 20:40:09 -0600433static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600434{
Azael Avalos258c5902014-09-29 20:40:07 -0600435 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
436 u32 out[TCI_WORDS];
437 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700438
Azael Avalos893f3f62014-09-29 20:40:09 -0600439 if (ACPI_FAILURE(status))
440 return TOS_FAILURE;
441
Azael Avalos84a62732014-03-25 20:38:29 -0600442 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600443
444 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600445}
446
Azael Avalos893f3f62014-09-29 20:40:09 -0600447static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600448{
Azael Avalos258c5902014-09-29 20:40:07 -0600449 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
450 u32 out[TCI_WORDS];
451 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600452
453 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600454}
455
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200456/* Illumination support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600457static void toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200458{
Azael Avalos258c5902014-09-29 20:40:07 -0600459 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
460 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200461 acpi_status status;
462
Azael Avalosea215a3f2015-07-31 21:58:12 -0600463 dev->illumination_supported = 0;
464 dev->illumination_led_registered = false;
465
Azael Avalosfdb79082014-03-25 20:38:30 -0600466 if (!sci_open(dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -0600467 return;
Azael Avalosfdb79082014-03-25 20:38:30 -0600468
Azael Avalos258c5902014-09-29 20:40:07 -0600469 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600470 sci_close(dev);
Azael Avalosea215a3f2015-07-31 21:58:12 -0600471 if (ACPI_FAILURE(status))
Azael Avalosfdb79082014-03-25 20:38:30 -0600472 pr_err("ACPI call to query Illumination support failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600473 else if (out[0] == TOS_SUCCESS)
474 dev->illumination_supported = 1;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200475}
476
477static void toshiba_illumination_set(struct led_classdev *cdev,
478 enum led_brightness brightness)
479{
Seth Forshee135740d2011-09-20 16:55:49 -0500480 struct toshiba_acpi_dev *dev = container_of(cdev,
481 struct toshiba_acpi_dev, led_dev);
Azael Avalose1a949c2015-07-31 21:58:14 -0600482 u32 result;
483 u32 state;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200484
485 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600486 if (!sci_open(dev))
487 return;
488
489 /* Switch the illumination on/off */
490 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600491 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600492 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600493 if (result == TOS_FAILURE)
Azael Avalosfdb79082014-03-25 20:38:30 -0600494 pr_err("ACPI call for illumination failed\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200495}
496
497static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
498{
Seth Forshee135740d2011-09-20 16:55:49 -0500499 struct toshiba_acpi_dev *dev = container_of(cdev,
500 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600501 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200502
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600503 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600504 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200505 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200506
507 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600508 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600509 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600510 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600511 pr_err("ACPI call for illumination failed\n");
512 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600513 } else if (result != TOS_SUCCESS) {
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200514 return LED_OFF;
515 }
516
Azael Avalosfdb79082014-03-25 20:38:30 -0600517 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200518}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400519
Azael Avalos360f0f32014-03-25 20:38:31 -0600520/* KBD Illumination */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600521static void toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
Azael Avalos93f8c162014-09-12 18:50:36 -0600522{
Azael Avalos258c5902014-09-29 20:40:07 -0600523 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
524 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600525 acpi_status status;
526
Azael Avalosea215a3f2015-07-31 21:58:12 -0600527 dev->kbd_illum_supported = 0;
528 dev->kbd_led_registered = false;
Azael Avalos65e3cf92015-11-23 10:51:30 -0700529 dev->kbd_event_generated = false;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600530
Azael Avalos93f8c162014-09-12 18:50:36 -0600531 if (!sci_open(dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -0600532 return;
Azael Avalos93f8c162014-09-12 18:50:36 -0600533
Azael Avalos258c5902014-09-29 20:40:07 -0600534 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600535 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600536 if (ACPI_FAILURE(status)) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600537 pr_err("ACPI call to query kbd illumination support failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600538 } else if (out[0] == TOS_SUCCESS) {
539 /*
540 * Check for keyboard backlight timeout max value,
541 * previous kbd backlight implementation set this to
542 * 0x3c0003, and now the new implementation set this
543 * to 0x3c001a, use this to distinguish between them.
544 */
545 if (out[3] == SCI_KBD_TIME_MAX)
546 dev->kbd_type = 2;
547 else
548 dev->kbd_type = 1;
549 /* Get the current keyboard backlight mode */
550 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
551 /* Get the current time (1-60 seconds) */
552 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
553 /* Flag as supported */
554 dev->kbd_illum_supported = 1;
Azael Avalos93f8c162014-09-12 18:50:36 -0600555 }
Azael Avalos93f8c162014-09-12 18:50:36 -0600556}
557
Azael Avalos360f0f32014-03-25 20:38:31 -0600558static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
559{
560 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600561
562 if (!sci_open(dev))
563 return -EIO;
564
Azael Avalos893f3f62014-09-29 20:40:09 -0600565 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600566 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600567 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600568 pr_err("ACPI call to set KBD backlight status failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600569 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos360f0f32014-03-25 20:38:31 -0600570 return -ENODEV;
Azael Avalos360f0f32014-03-25 20:38:31 -0600571
Azael Avalose1a949c2015-07-31 21:58:14 -0600572 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos360f0f32014-03-25 20:38:31 -0600573}
574
575static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
576{
577 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600578
579 if (!sci_open(dev))
580 return -EIO;
581
Azael Avalos893f3f62014-09-29 20:40:09 -0600582 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600583 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600584 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600585 pr_err("ACPI call to get KBD backlight status failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600586 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos360f0f32014-03-25 20:38:31 -0600587 return -ENODEV;
Azael Avalos360f0f32014-03-25 20:38:31 -0600588
Azael Avalose1a949c2015-07-31 21:58:14 -0600589 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos360f0f32014-03-25 20:38:31 -0600590}
591
592static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
593{
594 struct toshiba_acpi_dev *dev = container_of(cdev,
595 struct toshiba_acpi_dev, kbd_led);
Azael Avalose1a949c2015-07-31 21:58:14 -0600596 u32 result;
597 u32 state;
Azael Avalos360f0f32014-03-25 20:38:31 -0600598
599 /* Check the keyboard backlight state */
Azael Avalosd37782b2015-05-06 09:35:10 -0600600 result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
Azael Avalosa6b53542015-07-31 21:58:15 -0600601 if (result == TOS_FAILURE) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600602 pr_err("ACPI call to get the keyboard backlight failed\n");
603 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600604 } else if (result != TOS_SUCCESS) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600605 return LED_OFF;
606 }
607
608 return state ? LED_FULL : LED_OFF;
609}
610
611static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
612 enum led_brightness brightness)
613{
614 struct toshiba_acpi_dev *dev = container_of(cdev,
615 struct toshiba_acpi_dev, kbd_led);
Azael Avalose1a949c2015-07-31 21:58:14 -0600616 u32 result;
617 u32 state;
Azael Avalos360f0f32014-03-25 20:38:31 -0600618
619 /* Set the keyboard backlight state */
620 state = brightness ? 1 : 0;
Azael Avalosd37782b2015-05-06 09:35:10 -0600621 result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
Azael Avalosa6b53542015-07-31 21:58:15 -0600622 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600623 pr_err("ACPI call to set KBD Illumination mode failed\n");
Azael Avalos360f0f32014-03-25 20:38:31 -0600624}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400625
Azael Avalos9d8658a2014-03-25 20:38:32 -0600626/* TouchPad support */
627static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
628{
629 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600630
631 if (!sci_open(dev))
632 return -EIO;
633
Azael Avalos893f3f62014-09-29 20:40:09 -0600634 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600635 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600636 if (result == TOS_FAILURE)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600637 pr_err("ACPI call to set the touchpad failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600638 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600639 return -ENODEV;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600640
Azael Avalose1a949c2015-07-31 21:58:14 -0600641 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600642}
643
644static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
645{
646 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600647
648 if (!sci_open(dev))
649 return -EIO;
650
Azael Avalos893f3f62014-09-29 20:40:09 -0600651 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600652 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600653 if (result == TOS_FAILURE)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600654 pr_err("ACPI call to query the touchpad failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600655 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600656 return -ENODEV;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600657
Azael Avalose1a949c2015-07-31 21:58:14 -0600658 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600659}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200660
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600661/* Eco Mode support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600662static void toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600663{
664 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700665 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600666 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600667
Azael Avalosea215a3f2015-07-31 21:58:12 -0600668 dev->eco_supported = 0;
669 dev->eco_led_registered = false;
670
Azael Avalos258c5902014-09-29 20:40:07 -0600671 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600672 if (ACPI_FAILURE(status)) {
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700673 pr_err("ACPI call to get ECO led failed\n");
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700674 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800675 /*
676 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700677 * LED device is present, but that we just screwed the input
678 * parameters.
679 *
680 * Let's query the status of the LED to see if we really have a
681 * success response, indicating the actual presense of the LED,
682 * bail out otherwise.
683 */
684 in[3] = 1;
685 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600686 if (ACPI_FAILURE(status))
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700687 pr_err("ACPI call to get ECO led failed\n");
688 else if (out[0] == TOS_SUCCESS)
Azael Avalosea215a3f2015-07-31 21:58:12 -0600689 dev->eco_supported = 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600690 }
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600691}
692
Azael Avalosb5163992015-02-10 23:43:56 -0700693static enum led_brightness
694toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600695{
696 struct toshiba_acpi_dev *dev = container_of(cdev,
697 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600698 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
699 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600700 acpi_status status;
701
Azael Avalos258c5902014-09-29 20:40:07 -0600702 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600703 if (ACPI_FAILURE(status)) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600704 pr_err("ACPI call to get ECO led failed\n");
705 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600706 } else if (out[0] != TOS_SUCCESS) {
707 return LED_OFF;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600708 }
709
710 return out[2] ? LED_FULL : LED_OFF;
711}
712
713static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
714 enum led_brightness brightness)
715{
716 struct toshiba_acpi_dev *dev = container_of(cdev,
717 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600718 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
719 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600720 acpi_status status;
721
722 /* Switch the Eco Mode led on/off */
723 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600724 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600725 if (ACPI_FAILURE(status))
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600726 pr_err("ACPI call to set ECO led failed\n");
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600727}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400728
Azael Avalos5a2813e2014-03-25 20:38:34 -0600729/* Accelerometer support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600730static void toshiba_accelerometer_available(struct toshiba_acpi_dev *dev)
Azael Avalos5a2813e2014-03-25 20:38:34 -0600731{
Azael Avalos258c5902014-09-29 20:40:07 -0600732 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
733 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600734 acpi_status status;
735
Azael Avalosea215a3f2015-07-31 21:58:12 -0600736 dev->accelerometer_supported = 0;
737
Darren Harte0769fe2015-02-11 20:50:08 -0800738 /*
739 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600740 * this call also serves as initialization
741 */
Azael Avalos258c5902014-09-29 20:40:07 -0600742 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600743 if (ACPI_FAILURE(status))
Azael Avalos5a2813e2014-03-25 20:38:34 -0600744 pr_err("ACPI call to query the accelerometer failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600745 else if (out[0] == TOS_SUCCESS)
746 dev->accelerometer_supported = 1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600747}
748
749static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
Azael Avalosa6b53542015-07-31 21:58:15 -0600750 u32 *xy, u32 *z)
Azael Avalos5a2813e2014-03-25 20:38:34 -0600751{
Azael Avalos258c5902014-09-29 20:40:07 -0600752 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
753 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600754 acpi_status status;
755
756 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600757 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600758 if (ACPI_FAILURE(status)) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600759 pr_err("ACPI call to query the accelerometer failed\n");
760 return -EIO;
Azael Avalose1a949c2015-07-31 21:58:14 -0600761 } else if (out[0] == TOS_NOT_SUPPORTED) {
762 return -ENODEV;
763 } else if (out[0] == TOS_SUCCESS) {
764 *xy = out[2];
765 *z = out[4];
766 return 0;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600767 }
768
Azael Avalose1a949c2015-07-31 21:58:14 -0600769 return -EIO;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600770}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600771
Azael Avalose26ffe52015-01-18 18:30:22 -0700772/* Sleep (Charge and Music) utilities support */
Azael Avalosc8c91842015-04-02 19:26:20 -0600773static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
774{
775 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
776 u32 out[TCI_WORDS];
777 acpi_status status;
778
Azael Avalosc8c91842015-04-02 19:26:20 -0600779 dev->usb_sleep_charge_supported = 0;
780
781 if (!sci_open(dev))
782 return;
783
784 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600785 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600786 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
787 sci_close(dev);
788 return;
789 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600790 sci_close(dev);
791 return;
792 } else if (out[0] == TOS_SUCCESS) {
793 dev->usbsc_mode_base = out[4];
794 }
795
796 in[5] = SCI_USB_CHARGE_BAT_LVL;
797 status = tci_raw(dev, in, out);
Azael Avalosea215a3f2015-07-31 21:58:12 -0600798 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600799 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600800 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
Azael Avalosc8c91842015-04-02 19:26:20 -0600801 } else if (out[0] == TOS_SUCCESS) {
802 dev->usbsc_bat_level = out[2];
Azael Avalosea215a3f2015-07-31 21:58:12 -0600803 /* Flag as supported */
Azael Avalosc8c91842015-04-02 19:26:20 -0600804 dev->usb_sleep_charge_supported = 1;
805 }
806
Azael Avalosc8c91842015-04-02 19:26:20 -0600807}
808
Azael Avalose26ffe52015-01-18 18:30:22 -0700809static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
810 u32 *mode)
811{
812 u32 result;
813
814 if (!sci_open(dev))
815 return -EIO;
816
817 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
818 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600819 if (result == TOS_FAILURE)
Azael Avalose26ffe52015-01-18 18:30:22 -0700820 pr_err("ACPI call to set USB S&C mode failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600821 else if (result == TOS_NOT_SUPPORTED)
Azael Avalose26ffe52015-01-18 18:30:22 -0700822 return -ENODEV;
Azael Avalose26ffe52015-01-18 18:30:22 -0700823
Azael Avalose1a949c2015-07-31 21:58:14 -0600824 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalose26ffe52015-01-18 18:30:22 -0700825}
826
827static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
828 u32 mode)
829{
830 u32 result;
831
832 if (!sci_open(dev))
833 return -EIO;
834
835 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
836 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600837 if (result == TOS_FAILURE)
Azael Avalose26ffe52015-01-18 18:30:22 -0700838 pr_err("ACPI call to set USB S&C mode failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600839 else if (result == TOS_NOT_SUPPORTED)
Azael Avalose26ffe52015-01-18 18:30:22 -0700840 return -ENODEV;
Azael Avalose26ffe52015-01-18 18:30:22 -0700841
Azael Avalose1a949c2015-07-31 21:58:14 -0600842 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalose26ffe52015-01-18 18:30:22 -0700843}
844
Azael Avalos182bcaa2015-01-18 18:30:23 -0700845static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
846 u32 *mode)
847{
848 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
849 u32 out[TCI_WORDS];
850 acpi_status status;
851
852 if (!sci_open(dev))
853 return -EIO;
854
855 in[5] = SCI_USB_CHARGE_BAT_LVL;
856 status = tci_raw(dev, in, out);
857 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600858 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700859 pr_err("ACPI call to get USB S&C battery level failed\n");
Azael Avalos182bcaa2015-01-18 18:30:23 -0700860 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700861 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -0600862 } else if (out[0] == TOS_SUCCESS) {
863 *mode = out[2];
864 return 0;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700865 }
866
Azael Avalose1a949c2015-07-31 21:58:14 -0600867 return -EIO;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700868}
869
870static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
871 u32 mode)
872{
873 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
874 u32 out[TCI_WORDS];
875 acpi_status status;
876
877 if (!sci_open(dev))
878 return -EIO;
879
880 in[2] = mode;
881 in[5] = SCI_USB_CHARGE_BAT_LVL;
882 status = tci_raw(dev, in, out);
883 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600884 if (ACPI_FAILURE(status))
Azael Avalos182bcaa2015-01-18 18:30:23 -0700885 pr_err("ACPI call to set USB S&C battery level failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600886 else if (out[0] == TOS_NOT_SUPPORTED)
Azael Avalos182bcaa2015-01-18 18:30:23 -0700887 return -ENODEV;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700888
Azael Avalose1a949c2015-07-31 21:58:14 -0600889 return out[0] == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700890}
891
Azael Avalosbb3fe012015-01-18 18:30:24 -0700892static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
893 u32 *state)
894{
895 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
896 u32 out[TCI_WORDS];
897 acpi_status status;
898
899 if (!sci_open(dev))
900 return -EIO;
901
902 in[5] = SCI_USB_CHARGE_RAPID_DSP;
903 status = tci_raw(dev, in, out);
904 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600905 if (ACPI_FAILURE(status)) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600906 pr_err("ACPI call to get USB Rapid Charge failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600907 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosbb3fe012015-01-18 18:30:24 -0700908 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -0600909 } else if (out[0] == TOS_SUCCESS || out[0] == TOS_SUCCESS2) {
910 *state = out[2];
911 return 0;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700912 }
913
Azael Avalose1a949c2015-07-31 21:58:14 -0600914 return -EIO;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700915}
916
917static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
918 u32 state)
919{
920 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
921 u32 out[TCI_WORDS];
922 acpi_status status;
923
924 if (!sci_open(dev))
925 return -EIO;
926
927 in[2] = state;
928 in[5] = SCI_USB_CHARGE_RAPID_DSP;
929 status = tci_raw(dev, in, out);
930 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600931 if (ACPI_FAILURE(status))
Azael Avalosbb26f182015-04-02 19:26:21 -0600932 pr_err("ACPI call to set USB Rapid Charge failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600933 else if (out[0] == TOS_NOT_SUPPORTED)
Azael Avalosbb3fe012015-01-18 18:30:24 -0700934 return -ENODEV;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700935
Azael Avalose1a949c2015-07-31 21:58:14 -0600936 return (out[0] == TOS_SUCCESS || out[0] == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700937}
938
Azael Avalos172ce0a2015-01-18 18:30:25 -0700939static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
940{
941 u32 result;
942
943 if (!sci_open(dev))
944 return -EIO;
945
946 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
947 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600948 if (result == TOS_FAILURE)
Azael Avalosbb26f182015-04-02 19:26:21 -0600949 pr_err("ACPI call to get Sleep and Music failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600950 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos172ce0a2015-01-18 18:30:25 -0700951 return -ENODEV;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700952
Azael Avaloscf680ea2015-09-09 11:25:44 -0600953 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700954}
955
956static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
957{
958 u32 result;
959
960 if (!sci_open(dev))
961 return -EIO;
962
963 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
964 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600965 if (result == TOS_FAILURE)
Azael Avalosbb26f182015-04-02 19:26:21 -0600966 pr_err("ACPI call to set Sleep and Music failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600967 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos172ce0a2015-01-18 18:30:25 -0700968 return -ENODEV;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700969
Azael Avalose1a949c2015-07-31 21:58:14 -0600970 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700971}
972
Azael Avalosbae84192015-02-10 21:09:18 -0700973/* Keyboard function keys */
974static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
975{
976 u32 result;
977
978 if (!sci_open(dev))
979 return -EIO;
980
981 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
982 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600983 if (result == TOS_FAILURE)
Azael Avalosbae84192015-02-10 21:09:18 -0700984 pr_err("ACPI call to get KBD function keys failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600985 else if (result == TOS_NOT_SUPPORTED)
Azael Avalosbae84192015-02-10 21:09:18 -0700986 return -ENODEV;
Azael Avalosbae84192015-02-10 21:09:18 -0700987
Azael Avalose1a949c2015-07-31 21:58:14 -0600988 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbae84192015-02-10 21:09:18 -0700989}
990
991static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
992{
993 u32 result;
994
995 if (!sci_open(dev))
996 return -EIO;
997
998 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
999 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001000 if (result == TOS_FAILURE)
Azael Avalosbae84192015-02-10 21:09:18 -07001001 pr_err("ACPI call to set KBD function keys failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001002 else if (result == TOS_NOT_SUPPORTED)
Azael Avalosbae84192015-02-10 21:09:18 -07001003 return -ENODEV;
Azael Avalosbae84192015-02-10 21:09:18 -07001004
Azael Avalose1a949c2015-07-31 21:58:14 -06001005 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbae84192015-02-10 21:09:18 -07001006}
1007
Azael Avalos35d53ce2015-02-10 21:09:19 -07001008/* Panel Power ON */
1009static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1010{
1011 u32 result;
1012
1013 if (!sci_open(dev))
1014 return -EIO;
1015
1016 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1017 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001018 if (result == TOS_FAILURE)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001019 pr_err("ACPI call to get Panel Power ON failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001020 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001021 return -ENODEV;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001022
Azael Avalose1a949c2015-07-31 21:58:14 -06001023 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001024}
1025
1026static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1027{
1028 u32 result;
1029
1030 if (!sci_open(dev))
1031 return -EIO;
1032
1033 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1034 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001035 if (result == TOS_FAILURE)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001036 pr_err("ACPI call to set Panel Power ON failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001037 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001038 return -ENODEV;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001039
Azael Avalose1a949c2015-07-31 21:58:14 -06001040 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001041}
1042
Azael Avalos17fe4b32015-02-10 21:09:20 -07001043/* USB Three */
1044static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1045{
1046 u32 result;
1047
1048 if (!sci_open(dev))
1049 return -EIO;
1050
1051 result = sci_read(dev, SCI_USB_THREE, state);
1052 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001053 if (result == TOS_FAILURE)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001054 pr_err("ACPI call to get USB 3 failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001055 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001056 return -ENODEV;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001057
Azael Avalose1a949c2015-07-31 21:58:14 -06001058 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001059}
1060
1061static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1062{
1063 u32 result;
1064
1065 if (!sci_open(dev))
1066 return -EIO;
1067
1068 result = sci_write(dev, SCI_USB_THREE, state);
1069 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001070 if (result == TOS_FAILURE)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001071 pr_err("ACPI call to set USB 3 failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001072 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001073 return -ENODEV;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001074
Azael Avalose1a949c2015-07-31 21:58:14 -06001075 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001076}
1077
Azael Avalos56e6b352015-03-20 16:55:16 -06001078/* Hotkey Event type */
1079static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1080 u32 *type)
1081{
Azael Avalos3b876002015-05-06 09:35:09 -06001082 u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
1083 u32 out[TCI_WORDS];
1084 acpi_status status;
Azael Avalos56e6b352015-03-20 16:55:16 -06001085
Azael Avalos3b876002015-05-06 09:35:09 -06001086 status = tci_raw(dev, in, out);
1087 if (ACPI_FAILURE(status)) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001088 pr_err("ACPI call to get System type failed\n");
Azael Avalos3b876002015-05-06 09:35:09 -06001089 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001090 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -06001091 } else if (out[0] == TOS_SUCCESS) {
1092 *type = out[3];
1093 return 0;
Azael Avalos56e6b352015-03-20 16:55:16 -06001094 }
1095
Azael Avalose1a949c2015-07-31 21:58:14 -06001096 return -EIO;
Azael Avalos56e6b352015-03-20 16:55:16 -06001097}
1098
Azael Avalos6873f462015-11-23 10:49:10 -07001099/* Wireless status (RFKill, WLAN, BT, WWAN) */
1100static int toshiba_wireless_status(struct toshiba_acpi_dev *dev)
1101{
1102 u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
1103 u32 out[TCI_WORDS];
1104 acpi_status status;
1105
1106 in[3] = HCI_WIRELESS_STATUS;
1107 status = tci_raw(dev, in, out);
1108
1109 if (ACPI_FAILURE(status)) {
1110 pr_err("ACPI call to get Wireless status failed\n");
1111 return -EIO;
1112 }
1113
1114 if (out[0] == TOS_NOT_SUPPORTED)
1115 return -ENODEV;
1116
1117 if (out[0] != TOS_SUCCESS)
1118 return -EIO;
1119
1120 dev->killswitch = !!(out[2] & HCI_WIRELESS_STATUS);
1121
1122 return 0;
1123}
1124
1125/* WWAN */
1126static void toshiba_wwan_available(struct toshiba_acpi_dev *dev)
1127{
1128 u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
1129 u32 out[TCI_WORDS];
1130 acpi_status status;
1131
1132 dev->wwan_supported = 0;
1133
1134 /*
1135 * WWAN support can be queried by setting the in[3] value to
1136 * HCI_WIRELESS_WWAN (0x03).
1137 *
1138 * If supported, out[0] contains TOS_SUCCESS and out[2] contains
1139 * HCI_WIRELESS_WWAN_STATUS (0x2000).
1140 *
1141 * If not supported, out[0] contains TOS_INPUT_DATA_ERROR (0x8300)
1142 * or TOS_NOT_SUPPORTED (0x8000).
1143 */
1144 in[3] = HCI_WIRELESS_WWAN;
1145 status = tci_raw(dev, in, out);
1146
1147 if (ACPI_FAILURE(status)) {
1148 pr_err("ACPI call to get WWAN status failed\n");
1149 return;
1150 }
1151
1152 if (out[0] != TOS_SUCCESS)
1153 return;
1154
1155 dev->wwan_supported = (out[2] == HCI_WIRELESS_WWAN_STATUS);
1156}
1157
1158static int toshiba_wwan_set(struct toshiba_acpi_dev *dev, u32 state)
1159{
1160 u32 in[TCI_WORDS] = { HCI_SET, HCI_WIRELESS, state, 0, 0, 0 };
1161 u32 out[TCI_WORDS];
1162 acpi_status status;
1163
1164 in[3] = HCI_WIRELESS_WWAN_STATUS;
1165 status = tci_raw(dev, in, out);
1166
1167 if (ACPI_FAILURE(status)) {
1168 pr_err("ACPI call to set WWAN status failed\n");
1169 return -EIO;
1170 }
1171
1172 if (out[0] == TOS_NOT_SUPPORTED)
1173 return -ENODEV;
1174
1175 if (out[0] != TOS_SUCCESS)
1176 return -EIO;
1177
1178 /*
1179 * Some devices only need to call HCI_WIRELESS_WWAN_STATUS to
1180 * (de)activate the device, but some others need the
1181 * HCI_WIRELESS_WWAN_POWER call as well.
1182 */
1183 in[3] = HCI_WIRELESS_WWAN_POWER;
1184 status = tci_raw(dev, in, out);
1185
1186 if (ACPI_FAILURE(status)) {
1187 pr_err("ACPI call to set WWAN power failed\n");
1188 return -EIO;
1189 }
1190
1191 if (out[0] == TOS_NOT_SUPPORTED)
1192 return -ENODEV;
1193
1194 return out[0] == TOS_SUCCESS ? 0 : -EIO;
1195}
1196
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001197/* Transflective Backlight */
Azael Avalos695f6062015-07-22 18:09:13 -06001198static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 *status)
Akio Idehara121b7b02012-04-06 01:46:43 +09001199{
Azael Avalose1a949c2015-07-31 21:58:14 -06001200 u32 result = hci_read(dev, HCI_TR_BACKLIGHT, status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001201
Azael Avalose1a949c2015-07-31 21:58:14 -06001202 if (result == TOS_FAILURE)
1203 pr_err("ACPI call to get Transflective Backlight failed\n");
1204 else if (result == TOS_NOT_SUPPORTED)
1205 return -ENODEV;
1206
1207 return result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001208}
1209
Azael Avalos695f6062015-07-22 18:09:13 -06001210static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 status)
Akio Idehara121b7b02012-04-06 01:46:43 +09001211{
Azael Avalose1a949c2015-07-31 21:58:14 -06001212 u32 result = hci_write(dev, HCI_TR_BACKLIGHT, !status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001213
Azael Avalose1a949c2015-07-31 21:58:14 -06001214 if (result == TOS_FAILURE)
1215 pr_err("ACPI call to set Transflective Backlight failed\n");
1216 else if (result == TOS_NOT_SUPPORTED)
1217 return -ENODEV;
1218
1219 return result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001220}
1221
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001222static struct proc_dir_entry *toshiba_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001224/* LCD Brightness */
Seth Forshee62cce752012-04-19 11:23:50 -05001225static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Azael Avalose1a949c2015-07-31 21:58:14 -06001227 u32 result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001229 int brightness = 0;
1230
1231 if (dev->tr_backlight_supported) {
Azael Avalos695f6062015-07-22 18:09:13 -06001232 int ret = get_tr_backlight_status(dev, &value);
Azael Avalosb5163992015-02-10 23:43:56 -07001233
Akio Idehara121b7b02012-04-06 01:46:43 +09001234 if (ret)
1235 return ret;
Azael Avalos695f6062015-07-22 18:09:13 -06001236 if (value)
Akio Idehara121b7b02012-04-06 01:46:43 +09001237 return 0;
1238 brightness++;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Azael Avalose1a949c2015-07-31 21:58:14 -06001241 result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
1242 if (result == TOS_FAILURE)
1243 pr_err("ACPI call to get LCD Brightness failed\n");
1244 else if (result == TOS_NOT_SUPPORTED)
1245 return -ENODEV;
1246 if (result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001247 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001248
1249 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001250}
1251
Seth Forshee62cce752012-04-19 11:23:50 -05001252static int get_lcd_brightness(struct backlight_device *bd)
1253{
1254 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001255
Seth Forshee62cce752012-04-19 11:23:50 -05001256 return __get_lcd_brightness(dev);
1257}
1258
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001259static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001260{
Seth Forshee135740d2011-09-20 16:55:49 -05001261 struct toshiba_acpi_dev *dev = m->private;
Akio Idehara121b7b02012-04-06 01:46:43 +09001262 int levels;
Azael Avalose1a949c2015-07-31 21:58:14 -06001263 int value;
Holger Machtc9263552006-10-20 14:30:29 -07001264
Seth Forshee135740d2011-09-20 16:55:49 -05001265 if (!dev->backlight_dev)
1266 return -ENODEV;
1267
Akio Idehara121b7b02012-04-06 01:46:43 +09001268 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001269 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001270 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001271 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001272 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001276 pr_err("Error reading LCD brightness\n");
Azael Avalose1a949c2015-07-31 21:58:14 -06001277
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001278 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001279}
1280
1281static int lcd_proc_open(struct inode *inode, struct file *file)
1282{
Al Virod9dda782013-03-31 18:16:14 -04001283 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Seth Forshee62cce752012-04-19 11:23:50 -05001286static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001287{
Azael Avalose1a949c2015-07-31 21:58:14 -06001288 u32 result;
Holger Machtc9263552006-10-20 14:30:29 -07001289
Akio Idehara121b7b02012-04-06 01:46:43 +09001290 if (dev->tr_backlight_supported) {
Azael Avalos695f6062015-07-22 18:09:13 -06001291 int ret = set_tr_backlight_status(dev, !value);
Azael Avalosb5163992015-02-10 23:43:56 -07001292
Akio Idehara121b7b02012-04-06 01:46:43 +09001293 if (ret)
1294 return ret;
1295 if (value)
1296 value--;
1297 }
1298
Azael Avalosa39f46d2014-11-24 19:29:36 -07001299 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Azael Avalose1a949c2015-07-31 21:58:14 -06001300 result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
1301 if (result == TOS_FAILURE)
1302 pr_err("ACPI call to set LCD Brightness failed\n");
1303 else if (result == TOS_NOT_SUPPORTED)
1304 return -ENODEV;
1305
1306 return result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001307}
1308
1309static int set_lcd_status(struct backlight_device *bd)
1310{
Seth Forshee135740d2011-09-20 16:55:49 -05001311 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001312
Seth Forshee62cce752012-04-19 11:23:50 -05001313 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001314}
1315
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001316static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1317 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Al Virod9dda782013-03-31 18:16:14 -04001319 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001320 char cmd[42];
1321 size_t len;
Akio Idehara121b7b02012-04-06 01:46:43 +09001322 int levels = dev->backlight_dev->props.max_brightness + 1;
Azael Avalose1a949c2015-07-31 21:58:14 -06001323 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001325 len = min(count, sizeof(cmd) - 1);
1326 if (copy_from_user(cmd, buf, len))
1327 return -EFAULT;
1328 cmd[len] = '\0';
1329
Azael Avalose1a949c2015-07-31 21:58:14 -06001330 if (sscanf(cmd, " brightness : %i", &value) != 1 &&
1331 value < 0 && value > levels)
1332 return -EINVAL;
1333
1334 if (set_lcd_brightness(dev, value))
1335 return -EIO;
1336
1337 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001340static const struct file_operations lcd_proc_fops = {
1341 .owner = THIS_MODULE,
1342 .open = lcd_proc_open,
1343 .read = seq_read,
1344 .llseek = seq_lseek,
1345 .release = single_release,
1346 .write = lcd_proc_write,
1347};
1348
Azael Avalose1a949c2015-07-31 21:58:14 -06001349/* Video-Out */
Seth Forshee36d03f92011-09-20 16:55:53 -05001350static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1351{
Azael Avalose1a949c2015-07-31 21:58:14 -06001352 u32 result = hci_read(dev, HCI_VIDEO_OUT, status);
Seth Forshee36d03f92011-09-20 16:55:53 -05001353
Azael Avalose1a949c2015-07-31 21:58:14 -06001354 if (result == TOS_FAILURE)
1355 pr_err("ACPI call to get Video-Out failed\n");
1356 else if (result == TOS_NOT_SUPPORTED)
1357 return -ENODEV;
1358
1359 return result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001360}
1361
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001362static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Seth Forshee135740d2011-09-20 16:55:49 -05001364 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 u32 value;
1366
Azael Avalose1a949c2015-07-31 21:58:14 -06001367 if (!get_video_status(dev, &value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1369 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001370 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001371
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001372 seq_printf(m, "lcd_out: %d\n", is_lcd);
1373 seq_printf(m, "crt_out: %d\n", is_crt);
1374 seq_printf(m, "tv_out: %d\n", is_tv);
Azael Avalose1a949c2015-07-31 21:58:14 -06001375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
Azael Avalose1a949c2015-07-31 21:58:14 -06001378 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001381static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Al Virod9dda782013-03-31 18:16:14 -04001383 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001384}
1385
1386static ssize_t video_proc_write(struct file *file, const char __user *buf,
1387 size_t count, loff_t *pos)
1388{
Al Virod9dda782013-03-31 18:16:14 -04001389 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Azael Avalose1a949c2015-07-31 21:58:14 -06001390 char *buffer;
1391 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int remain = count;
1393 int lcd_out = -1;
1394 int crt_out = -1;
1395 int tv_out = -1;
Azael Avalose1a949c2015-07-31 21:58:14 -06001396 int value;
1397 int ret;
Al Virob4482a42007-10-14 19:35:40 +01001398 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001400 cmd = kmalloc(count + 1, GFP_KERNEL);
1401 if (!cmd)
1402 return -ENOMEM;
1403 if (copy_from_user(cmd, buf, count)) {
1404 kfree(cmd);
1405 return -EFAULT;
1406 }
1407 cmd[count] = '\0';
1408
1409 buffer = cmd;
1410
Darren Harte0769fe2015-02-11 20:50:08 -08001411 /*
1412 * Scan expression. Multiple expressions may be delimited with ;
1413 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
1415 while (remain) {
1416 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1417 lcd_out = value & 1;
1418 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1419 crt_out = value & 1;
1420 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1421 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001422 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 do {
1424 ++buffer;
1425 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001426 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001429 kfree(cmd);
1430
Seth Forshee36d03f92011-09-20 16:55:53 -05001431 ret = get_video_status(dev, &video_out);
1432 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001433 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (lcd_out != -1)
1436 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1437 if (crt_out != -1)
1438 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1439 if (tv_out != -1)
1440 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001441 /*
1442 * To avoid unnecessary video disruption, only write the new
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001443 * video setting if something changed.
1444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001446 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448
Azael Avalose1a949c2015-07-31 21:58:14 -06001449 return ret ? -EIO : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001452static const struct file_operations video_proc_fops = {
1453 .owner = THIS_MODULE,
1454 .open = video_proc_open,
1455 .read = seq_read,
1456 .llseek = seq_lseek,
1457 .release = single_release,
1458 .write = video_proc_write,
1459};
1460
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001461/* Fan status */
Seth Forshee36d03f92011-09-20 16:55:53 -05001462static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1463{
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001464 u32 result = hci_read(dev, HCI_FAN, status);
Seth Forshee36d03f92011-09-20 16:55:53 -05001465
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001466 if (result == TOS_FAILURE)
1467 pr_err("ACPI call to get Fan status failed\n");
1468 else if (result == TOS_NOT_SUPPORTED)
1469 return -ENODEV;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001470
Azael Avalose1a949c2015-07-31 21:58:14 -06001471 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001472}
1473
1474static int set_fan_status(struct toshiba_acpi_dev *dev, u32 status)
1475{
1476 u32 result = hci_write(dev, HCI_FAN, status);
1477
1478 if (result == TOS_FAILURE)
1479 pr_err("ACPI call to set Fan status failed\n");
1480 else if (result == TOS_NOT_SUPPORTED)
1481 return -ENODEV;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001482
Azael Avalose1a949c2015-07-31 21:58:14 -06001483 return result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001484}
1485
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001486static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Seth Forshee135740d2011-09-20 16:55:49 -05001488 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 u32 value;
1490
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001491 if (get_fan_status(dev, &value))
1492 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001494 seq_printf(m, "running: %d\n", (value > 0));
1495 seq_printf(m, "force_on: %d\n", dev->force_fan);
1496
1497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001500static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Al Virod9dda782013-03-31 18:16:14 -04001502 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001503}
1504
1505static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1506 size_t count, loff_t *pos)
1507{
Al Virod9dda782013-03-31 18:16:14 -04001508 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001509 char cmd[42];
1510 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001513 len = min(count, sizeof(cmd) - 1);
1514 if (copy_from_user(cmd, buf, len))
1515 return -EFAULT;
1516 cmd[len] = '\0';
1517
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001518 if (sscanf(cmd, " force_on : %i", &value) != 1 &&
1519 value != 0 && value != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return -EINVAL;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001521
1522 if (set_fan_status(dev, value))
1523 return -EIO;
1524
1525 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 return count;
1528}
1529
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001530static const struct file_operations fan_proc_fops = {
1531 .owner = THIS_MODULE,
1532 .open = fan_proc_open,
1533 .read = seq_read,
1534 .llseek = seq_lseek,
1535 .release = single_release,
1536 .write = fan_proc_write,
1537};
1538
1539static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Seth Forshee135740d2011-09-20 16:55:49 -05001541 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Seth Forshee135740d2011-09-20 16:55:49 -05001543 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1544 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Azael Avalos7deef552015-07-22 18:09:10 -06001545
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001549static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Al Virod9dda782013-03-31 18:16:14 -04001551 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001552}
1553
1554static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1555 size_t count, loff_t *pos)
1556{
Al Virod9dda782013-03-31 18:16:14 -04001557 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001558 char cmd[42];
1559 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int value;
1561
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001562 len = min(count, sizeof(cmd) - 1);
1563 if (copy_from_user(cmd, buf, len))
1564 return -EFAULT;
1565 cmd[len] = '\0';
1566
Azael Avalosb5163992015-02-10 23:43:56 -07001567 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001568 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001569 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 return count;
1573}
1574
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001575static const struct file_operations keys_proc_fops = {
1576 .owner = THIS_MODULE,
1577 .open = keys_proc_open,
1578 .read = seq_read,
1579 .llseek = seq_lseek,
1580 .release = single_release,
1581 .write = keys_proc_write,
1582};
1583
1584static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001586 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1587 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001591static int version_proc_open(struct inode *inode, struct file *file)
1592{
Al Virod9dda782013-03-31 18:16:14 -04001593 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001594}
1595
1596static const struct file_operations version_proc_fops = {
1597 .owner = THIS_MODULE,
1598 .open = version_proc_open,
1599 .read = seq_read,
1600 .llseek = seq_lseek,
1601 .release = single_release,
1602};
1603
Darren Harte0769fe2015-02-11 20:50:08 -08001604/*
1605 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 */
1607
1608#define PROC_TOSHIBA "toshiba"
1609
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001610static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Seth Forshee36d03f92011-09-20 16:55:53 -05001612 if (dev->backlight_dev)
1613 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1614 &lcd_proc_fops, dev);
1615 if (dev->video_supported)
1616 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1617 &video_proc_fops, dev);
1618 if (dev->fan_supported)
1619 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1620 &fan_proc_fops, dev);
1621 if (dev->hotkey_dev)
1622 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1623 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001624 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1625 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
Seth Forshee36d03f92011-09-20 16:55:53 -05001628static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Seth Forshee36d03f92011-09-20 16:55:53 -05001630 if (dev->backlight_dev)
1631 remove_proc_entry("lcd", toshiba_proc_dir);
1632 if (dev->video_supported)
1633 remove_proc_entry("video", toshiba_proc_dir);
1634 if (dev->fan_supported)
1635 remove_proc_entry("fan", toshiba_proc_dir);
1636 if (dev->hotkey_dev)
1637 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001638 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Lionel Debrouxacc24722010-11-16 14:14:02 +01001641static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001642 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001643 .get_brightness = get_lcd_brightness,
1644 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001645};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001646
Azael Avalos65e3cf92015-11-23 10:51:30 -07001647/* Keyboard backlight work */
1648static void toshiba_acpi_kbd_bl_work(struct work_struct *work);
1649
1650static DECLARE_WORK(kbd_bl_work, toshiba_acpi_kbd_bl_work);
1651
Azael Avalos360f0f32014-03-25 20:38:31 -06001652/*
1653 * Sysfs files
1654 */
Azael Avalos9d309842015-02-10 23:43:59 -07001655static ssize_t version_show(struct device *dev,
1656 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001657{
1658 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1659}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001660static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001661
Azael Avalos9d309842015-02-10 23:43:59 -07001662static ssize_t fan_store(struct device *dev,
1663 struct device_attribute *attr,
1664 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001665{
1666 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avalos94477d42015-02-10 21:09:17 -07001667 int state;
1668 int ret;
1669
1670 ret = kstrtoint(buf, 0, &state);
1671 if (ret)
1672 return ret;
1673
1674 if (state != 0 && state != 1)
1675 return -EINVAL;
1676
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001677 ret = set_fan_status(toshiba, state);
1678 if (ret)
1679 return ret;
Azael Avalos94477d42015-02-10 21:09:17 -07001680
1681 return count;
1682}
1683
Azael Avalos9d309842015-02-10 23:43:59 -07001684static ssize_t fan_show(struct device *dev,
1685 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001686{
1687 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1688 u32 value;
1689 int ret;
1690
1691 ret = get_fan_status(toshiba, &value);
1692 if (ret)
1693 return ret;
1694
1695 return sprintf(buf, "%d\n", value);
1696}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001697static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001698
Azael Avalos9d309842015-02-10 23:43:59 -07001699static ssize_t kbd_backlight_mode_store(struct device *dev,
1700 struct device_attribute *attr,
1701 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001702{
1703 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001704 int mode;
Dan Carpenteraeaac092014-09-03 14:44:37 +03001705 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001706
Dan Carpenteraeaac092014-09-03 14:44:37 +03001707
1708 ret = kstrtoint(buf, 0, &mode);
1709 if (ret)
1710 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001711
1712 /* Check for supported modes depending on keyboard backlight type */
1713 if (toshiba->kbd_type == 1) {
1714 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1715 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1716 return -EINVAL;
1717 } else if (toshiba->kbd_type == 2) {
1718 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1719 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1720 mode != SCI_KBD_MODE_OFF)
1721 return -EINVAL;
1722 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001723
Darren Harte0769fe2015-02-11 20:50:08 -08001724 /*
1725 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001726 * Auto - KBD backlight turns off automatically in given time
1727 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001728 * ON - KBD backlight is always on
1729 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001730 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001731
1732 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001733 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001734 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos1e574db2015-07-22 19:37:49 -06001735 int time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001736
1737 /* OR the "base time" to the actual method format */
1738 if (toshiba->kbd_type == 1) {
1739 /* Type 1 requires the current mode */
1740 time |= toshiba->kbd_mode;
1741 } else if (toshiba->kbd_type == 2) {
1742 /* Type 2 requires the desired mode */
1743 time |= mode;
1744 }
1745
Dan Carpenteraeaac092014-09-03 14:44:37 +03001746 ret = toshiba_kbd_illum_status_set(toshiba, time);
1747 if (ret)
1748 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001749
Azael Avalos360f0f32014-03-25 20:38:31 -06001750 toshiba->kbd_mode = mode;
Azael Avalos65e3cf92015-11-23 10:51:30 -07001751
1752 /*
1753 * Some laptop models with the second generation backlit
1754 * keyboard (type 2) do not generate the keyboard backlight
1755 * changed event (0x92), and thus, the driver will never update
1756 * the sysfs entries.
1757 *
1758 * The event is generated right when changing the keyboard
1759 * backlight mode and the *notify function will set the
1760 * kbd_event_generated to true.
1761 *
1762 * In case the event is not generated, schedule the keyboard
1763 * backlight work to update the sysfs entries and emulate the
1764 * event via genetlink.
1765 */
1766 if (toshiba->kbd_type == 2 &&
1767 !toshiba_acpi->kbd_event_generated)
1768 schedule_work(&kbd_bl_work);
Azael Avalos360f0f32014-03-25 20:38:31 -06001769 }
1770
1771 return count;
1772}
1773
Azael Avalos9d309842015-02-10 23:43:59 -07001774static ssize_t kbd_backlight_mode_show(struct device *dev,
1775 struct device_attribute *attr,
1776 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001777{
1778 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1779 u32 time;
1780
1781 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1782 return -EIO;
1783
Azael Avalos93f8c162014-09-12 18:50:36 -06001784 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1785}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001786static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001787
Azael Avalos9d309842015-02-10 23:43:59 -07001788static ssize_t kbd_type_show(struct device *dev,
1789 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001790{
1791 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1792
1793 return sprintf(buf, "%d\n", toshiba->kbd_type);
1794}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001795static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001796
Azael Avalos9d309842015-02-10 23:43:59 -07001797static ssize_t available_kbd_modes_show(struct device *dev,
1798 struct device_attribute *attr,
1799 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001800{
1801 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1802
1803 if (toshiba->kbd_type == 1)
Azael Avalos0b498202015-09-09 11:30:09 -06001804 return sprintf(buf, "0x%x 0x%x\n",
Azael Avalos93f8c162014-09-12 18:50:36 -06001805 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1806
Azael Avalos0b498202015-09-09 11:30:09 -06001807 return sprintf(buf, "0x%x 0x%x 0x%x\n",
Azael Avalos93f8c162014-09-12 18:50:36 -06001808 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001809}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001810static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001811
Azael Avalos9d309842015-02-10 23:43:59 -07001812static ssize_t kbd_backlight_timeout_store(struct device *dev,
1813 struct device_attribute *attr,
1814 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001815{
1816 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001817 int time;
1818 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001819
Azael Avaloseabde0f2014-10-04 12:02:21 -06001820 ret = kstrtoint(buf, 0, &time);
1821 if (ret)
1822 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001823
Azael Avaloseabde0f2014-10-04 12:02:21 -06001824 /* Check for supported values depending on kbd_type */
1825 if (toshiba->kbd_type == 1) {
1826 if (time < 0 || time > 60)
1827 return -EINVAL;
1828 } else if (toshiba->kbd_type == 2) {
1829 if (time < 1 || time > 60)
1830 return -EINVAL;
1831 }
1832
1833 /* Set the Keyboard Backlight Timeout */
1834
1835 /* Only make a change if the actual timeout has changed */
1836 if (toshiba->kbd_time != time) {
1837 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001838 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001839 /* OR the "base time" to the actual method format */
1840 if (toshiba->kbd_type == 1)
1841 time |= SCI_KBD_MODE_FNZ;
1842 else if (toshiba->kbd_type == 2)
1843 time |= SCI_KBD_MODE_AUTO;
1844
1845 ret = toshiba_kbd_illum_status_set(toshiba, time);
1846 if (ret)
1847 return ret;
1848
Azael Avalos360f0f32014-03-25 20:38:31 -06001849 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1850 }
1851
1852 return count;
1853}
1854
Azael Avalos9d309842015-02-10 23:43:59 -07001855static ssize_t kbd_backlight_timeout_show(struct device *dev,
1856 struct device_attribute *attr,
1857 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001858{
1859 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1860 u32 time;
1861
1862 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1863 return -EIO;
1864
1865 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1866}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001867static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001868
Azael Avalos9d309842015-02-10 23:43:59 -07001869static ssize_t touchpad_store(struct device *dev,
1870 struct device_attribute *attr,
1871 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001872{
1873 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1874 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001875 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001876
1877 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001878 ret = kstrtoint(buf, 0, &state);
1879 if (ret)
1880 return ret;
1881 if (state != 0 && state != 1)
1882 return -EINVAL;
1883
1884 ret = toshiba_touchpad_set(toshiba, state);
1885 if (ret)
1886 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001887
1888 return count;
1889}
1890
Azael Avalos9d309842015-02-10 23:43:59 -07001891static ssize_t touchpad_show(struct device *dev,
1892 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001893{
1894 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1895 u32 state;
1896 int ret;
1897
1898 ret = toshiba_touchpad_get(toshiba, &state);
1899 if (ret < 0)
1900 return ret;
1901
1902 return sprintf(buf, "%i\n", state);
1903}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001904static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001905
Azael Avalos9d309842015-02-10 23:43:59 -07001906static ssize_t position_show(struct device *dev,
1907 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001908{
1909 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1910 u32 xyval, zval, tmp;
1911 u16 x, y, z;
1912 int ret;
1913
1914 xyval = zval = 0;
1915 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1916 if (ret < 0)
1917 return ret;
1918
1919 x = xyval & HCI_ACCEL_MASK;
1920 tmp = xyval >> HCI_MISC_SHIFT;
1921 y = tmp & HCI_ACCEL_MASK;
1922 z = zval & HCI_ACCEL_MASK;
1923
1924 return sprintf(buf, "%d %d %d\n", x, y, z);
1925}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001926static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001927
Azael Avalos9d309842015-02-10 23:43:59 -07001928static ssize_t usb_sleep_charge_show(struct device *dev,
1929 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001930{
1931 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1932 u32 mode;
1933 int ret;
1934
1935 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1936 if (ret < 0)
1937 return ret;
1938
1939 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1940}
1941
Azael Avalos9d309842015-02-10 23:43:59 -07001942static ssize_t usb_sleep_charge_store(struct device *dev,
1943 struct device_attribute *attr,
1944 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001945{
1946 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1947 u32 mode;
1948 int state;
1949 int ret;
1950
1951 ret = kstrtoint(buf, 0, &state);
1952 if (ret)
1953 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08001954 /*
1955 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07001956 * 0 - Disabled
1957 * 1 - Alternate (Non USB conformant devices that require more power)
1958 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06001959 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07001960 */
Azael Avalosc8c91842015-04-02 19:26:20 -06001961 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07001962 return -EINVAL;
1963
1964 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06001965 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07001966 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06001967 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07001968 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06001969 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07001970 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06001971 mode |= SCI_USB_CHARGE_AUTO;
1972 else if (state == 3)
1973 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07001974
1975 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1976 if (ret)
1977 return ret;
1978
1979 return count;
1980}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001981static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07001982
Azael Avalos182bcaa2015-01-18 18:30:23 -07001983static ssize_t sleep_functions_on_battery_show(struct device *dev,
1984 struct device_attribute *attr,
1985 char *buf)
1986{
1987 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1988 u32 state;
1989 int bat_lvl;
1990 int status;
1991 int ret;
1992 int tmp;
1993
1994 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1995 if (ret < 0)
1996 return ret;
1997
1998 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1999 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2000 status = (tmp == 0x4) ? 1 : 0;
2001 /* Determine the battery level set */
2002 bat_lvl = state >> HCI_MISC_SHIFT;
2003
2004 return sprintf(buf, "%d %d\n", status, bat_lvl);
2005}
2006
2007static ssize_t sleep_functions_on_battery_store(struct device *dev,
2008 struct device_attribute *attr,
2009 const char *buf, size_t count)
2010{
2011 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2012 u32 status;
2013 int value;
2014 int ret;
2015 int tmp;
2016
2017 ret = kstrtoint(buf, 0, &value);
2018 if (ret)
2019 return ret;
2020
Darren Harte0769fe2015-02-11 20:50:08 -08002021 /*
2022 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07002023 * 0 - Disabled
2024 * 1-100 - Enabled
2025 */
2026 if (value < 0 || value > 100)
2027 return -EINVAL;
2028
2029 if (value == 0) {
2030 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2031 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2032 } else {
2033 tmp = value << HCI_MISC_SHIFT;
2034 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2035 }
2036 ret = toshiba_sleep_functions_status_set(toshiba, status);
2037 if (ret < 0)
2038 return ret;
2039
2040 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2041
2042 return count;
2043}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002044static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002045
Azael Avalos9d309842015-02-10 23:43:59 -07002046static ssize_t usb_rapid_charge_show(struct device *dev,
2047 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002048{
2049 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2050 u32 state;
2051 int ret;
2052
2053 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2054 if (ret < 0)
2055 return ret;
2056
2057 return sprintf(buf, "%d\n", state);
2058}
2059
Azael Avalos9d309842015-02-10 23:43:59 -07002060static ssize_t usb_rapid_charge_store(struct device *dev,
2061 struct device_attribute *attr,
2062 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002063{
2064 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2065 int state;
2066 int ret;
2067
2068 ret = kstrtoint(buf, 0, &state);
2069 if (ret)
2070 return ret;
2071 if (state != 0 && state != 1)
2072 return -EINVAL;
2073
2074 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2075 if (ret)
2076 return ret;
2077
2078 return count;
2079}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002080static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002081
Azael Avalos9d309842015-02-10 23:43:59 -07002082static ssize_t usb_sleep_music_show(struct device *dev,
2083 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002084{
2085 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2086 u32 state;
2087 int ret;
2088
2089 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2090 if (ret < 0)
2091 return ret;
2092
2093 return sprintf(buf, "%d\n", state);
2094}
2095
Azael Avalos9d309842015-02-10 23:43:59 -07002096static ssize_t usb_sleep_music_store(struct device *dev,
2097 struct device_attribute *attr,
2098 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002099{
2100 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2101 int state;
2102 int ret;
2103
2104 ret = kstrtoint(buf, 0, &state);
2105 if (ret)
2106 return ret;
2107 if (state != 0 && state != 1)
2108 return -EINVAL;
2109
2110 ret = toshiba_usb_sleep_music_set(toshiba, state);
2111 if (ret)
2112 return ret;
2113
2114 return count;
2115}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002116static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002117
Azael Avalos9d309842015-02-10 23:43:59 -07002118static ssize_t kbd_function_keys_show(struct device *dev,
2119 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002120{
2121 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2122 int mode;
2123 int ret;
2124
2125 ret = toshiba_function_keys_get(toshiba, &mode);
2126 if (ret < 0)
2127 return ret;
2128
2129 return sprintf(buf, "%d\n", mode);
2130}
2131
Azael Avalos9d309842015-02-10 23:43:59 -07002132static ssize_t kbd_function_keys_store(struct device *dev,
2133 struct device_attribute *attr,
2134 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002135{
2136 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2137 int mode;
2138 int ret;
2139
2140 ret = kstrtoint(buf, 0, &mode);
2141 if (ret)
2142 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002143 /*
2144 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002145 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2146 * 1 - Special functions (Opposite of the above setting)
2147 */
2148 if (mode != 0 && mode != 1)
2149 return -EINVAL;
2150
2151 ret = toshiba_function_keys_set(toshiba, mode);
2152 if (ret)
2153 return ret;
2154
2155 pr_info("Reboot for changes to KBD Function Keys to take effect");
2156
2157 return count;
2158}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002159static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002160
Azael Avalos9d309842015-02-10 23:43:59 -07002161static ssize_t panel_power_on_show(struct device *dev,
2162 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002163{
2164 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2165 u32 state;
2166 int ret;
2167
2168 ret = toshiba_panel_power_on_get(toshiba, &state);
2169 if (ret < 0)
2170 return ret;
2171
2172 return sprintf(buf, "%d\n", state);
2173}
2174
Azael Avalos9d309842015-02-10 23:43:59 -07002175static ssize_t panel_power_on_store(struct device *dev,
2176 struct device_attribute *attr,
2177 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002178{
2179 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2180 int state;
2181 int ret;
2182
2183 ret = kstrtoint(buf, 0, &state);
2184 if (ret)
2185 return ret;
2186 if (state != 0 && state != 1)
2187 return -EINVAL;
2188
2189 ret = toshiba_panel_power_on_set(toshiba, state);
2190 if (ret)
2191 return ret;
2192
2193 pr_info("Reboot for changes to Panel Power ON to take effect");
2194
2195 return count;
2196}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002197static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002198
Azael Avalos9d309842015-02-10 23:43:59 -07002199static ssize_t usb_three_show(struct device *dev,
2200 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002201{
2202 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2203 u32 state;
2204 int ret;
2205
2206 ret = toshiba_usb_three_get(toshiba, &state);
2207 if (ret < 0)
2208 return ret;
2209
2210 return sprintf(buf, "%d\n", state);
2211}
2212
Azael Avalos9d309842015-02-10 23:43:59 -07002213static ssize_t usb_three_store(struct device *dev,
2214 struct device_attribute *attr,
2215 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002216{
2217 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2218 int state;
2219 int ret;
2220
2221 ret = kstrtoint(buf, 0, &state);
2222 if (ret)
2223 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002224 /*
2225 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002226 * 0 - Disabled (Acts like a USB 2 port, saving power)
2227 * 1 - Enabled
2228 */
2229 if (state != 0 && state != 1)
2230 return -EINVAL;
2231
2232 ret = toshiba_usb_three_set(toshiba, state);
2233 if (ret)
2234 return ret;
2235
2236 pr_info("Reboot for changes to USB 3 to take effect");
2237
2238 return count;
2239}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002240static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002241
2242static struct attribute *toshiba_attributes[] = {
2243 &dev_attr_version.attr,
2244 &dev_attr_fan.attr,
2245 &dev_attr_kbd_backlight_mode.attr,
2246 &dev_attr_kbd_type.attr,
2247 &dev_attr_available_kbd_modes.attr,
2248 &dev_attr_kbd_backlight_timeout.attr,
2249 &dev_attr_touchpad.attr,
2250 &dev_attr_position.attr,
2251 &dev_attr_usb_sleep_charge.attr,
2252 &dev_attr_sleep_functions_on_battery.attr,
2253 &dev_attr_usb_rapid_charge.attr,
2254 &dev_attr_usb_sleep_music.attr,
2255 &dev_attr_kbd_function_keys.attr,
2256 &dev_attr_panel_power_on.attr,
2257 &dev_attr_usb_three.attr,
2258 NULL,
2259};
2260
Azael Avalos360f0f32014-03-25 20:38:31 -06002261static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2262 struct attribute *attr, int idx)
2263{
2264 struct device *dev = container_of(kobj, struct device, kobj);
2265 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2266 bool exists = true;
2267
Azael Avalos94477d42015-02-10 21:09:17 -07002268 if (attr == &dev_attr_fan.attr)
2269 exists = (drv->fan_supported) ? true : false;
2270 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002271 exists = (drv->kbd_illum_supported) ? true : false;
2272 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2273 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002274 else if (attr == &dev_attr_touchpad.attr)
2275 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002276 else if (attr == &dev_attr_position.attr)
2277 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002278 else if (attr == &dev_attr_usb_sleep_charge.attr)
2279 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002280 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2281 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002282 else if (attr == &dev_attr_usb_rapid_charge.attr)
2283 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002284 else if (attr == &dev_attr_usb_sleep_music.attr)
2285 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002286 else if (attr == &dev_attr_kbd_function_keys.attr)
2287 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002288 else if (attr == &dev_attr_panel_power_on.attr)
2289 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002290 else if (attr == &dev_attr_usb_three.attr)
2291 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002292
2293 return exists ? attr->mode : 0;
2294}
2295
Azael Avalos9bd12132015-02-10 23:43:58 -07002296static struct attribute_group toshiba_attr_group = {
2297 .is_visible = toshiba_sysfs_is_visible,
2298 .attrs = toshiba_attributes,
2299};
2300
Azael Avalos65e3cf92015-11-23 10:51:30 -07002301static void toshiba_acpi_kbd_bl_work(struct work_struct *work)
2302{
2303 struct acpi_device *acpi_dev = toshiba_acpi->acpi_dev;
2304
2305 /* Update the sysfs entries */
2306 if (sysfs_update_group(&acpi_dev->dev.kobj,
2307 &toshiba_attr_group))
2308 pr_err("Unable to update sysfs entries\n");
2309
2310 /* Emulate the keyboard backlight event */
2311 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2312 dev_name(&acpi_dev->dev),
2313 0x92, 0);
2314}
2315
Azael Avalos1f28f292014-12-04 20:22:45 -07002316/*
Azael Avalosfc5462f2015-07-22 18:09:11 -06002317 * Misc device
2318 */
2319static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
2320{
2321 u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
2322 regs->edx, regs->esi, regs->edi };
2323 u32 out[TCI_WORDS];
2324 acpi_status status;
2325
2326 status = tci_raw(toshiba_acpi, in, out);
2327 if (ACPI_FAILURE(status)) {
2328 pr_err("ACPI call to query SMM registers failed\n");
2329 return -EIO;
2330 }
2331
2332 /* Fillout the SMM struct with the TCI call results */
2333 regs->eax = out[0];
2334 regs->ebx = out[1];
2335 regs->ecx = out[2];
2336 regs->edx = out[3];
2337 regs->esi = out[4];
2338 regs->edi = out[5];
2339
2340 return 0;
2341}
2342
2343static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
2344 unsigned long arg)
2345{
2346 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
2347 SMMRegisters regs;
2348 int ret;
2349
2350 if (!argp)
2351 return -EINVAL;
2352
2353 switch (cmd) {
2354 case TOSH_SMM:
2355 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2356 return -EFAULT;
2357 ret = toshiba_acpi_smm_bridge(&regs);
2358 if (ret)
2359 return ret;
2360 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2361 return -EFAULT;
2362 break;
2363 case TOSHIBA_ACPI_SCI:
2364 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2365 return -EFAULT;
2366 /* Ensure we are being called with a SCI_{GET, SET} register */
2367 if (regs.eax != SCI_GET && regs.eax != SCI_SET)
2368 return -EINVAL;
2369 if (!sci_open(toshiba_acpi))
2370 return -EIO;
2371 ret = toshiba_acpi_smm_bridge(&regs);
2372 sci_close(toshiba_acpi);
2373 if (ret)
2374 return ret;
2375 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2376 return -EFAULT;
2377 break;
2378 default:
2379 return -EINVAL;
2380 }
2381
2382 return 0;
2383}
2384
2385static const struct file_operations toshiba_acpi_fops = {
2386 .owner = THIS_MODULE,
2387 .unlocked_ioctl = toshiba_acpi_ioctl,
2388 .llseek = noop_llseek,
2389};
2390
2391/*
Azael Avalos2fdde832015-11-23 10:49:11 -07002392 * WWAN RFKill handlers
2393 */
2394static int toshiba_acpi_wwan_set_block(void *data, bool blocked)
2395{
2396 struct toshiba_acpi_dev *dev = data;
2397 int ret;
2398
2399 ret = toshiba_wireless_status(dev);
2400 if (ret)
2401 return ret;
2402
2403 if (!dev->killswitch)
2404 return 0;
2405
2406 return toshiba_wwan_set(dev, !blocked);
2407}
2408
2409static void toshiba_acpi_wwan_poll(struct rfkill *rfkill, void *data)
2410{
2411 struct toshiba_acpi_dev *dev = data;
2412
2413 if (toshiba_wireless_status(dev))
2414 return;
2415
2416 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
2417}
2418
2419static const struct rfkill_ops wwan_rfk_ops = {
2420 .set_block = toshiba_acpi_wwan_set_block,
2421 .poll = toshiba_acpi_wwan_poll,
2422};
2423
2424static int toshiba_acpi_setup_wwan_rfkill(struct toshiba_acpi_dev *dev)
2425{
2426 int ret = toshiba_wireless_status(dev);
2427
2428 if (ret)
2429 return ret;
2430
2431 dev->wwan_rfk = rfkill_alloc("Toshiba WWAN",
2432 &dev->acpi_dev->dev,
2433 RFKILL_TYPE_WWAN,
2434 &wwan_rfk_ops,
2435 dev);
2436 if (!dev->wwan_rfk) {
2437 pr_err("Unable to allocate WWAN rfkill device\n");
2438 return -ENOMEM;
2439 }
2440
2441 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
2442
2443 ret = rfkill_register(dev->wwan_rfk);
2444 if (ret) {
2445 pr_err("Unable to register WWAN rfkill device\n");
2446 rfkill_destroy(dev->wwan_rfk);
2447 }
2448
2449 return ret;
2450}
2451
2452/*
Azael Avalos1f28f292014-12-04 20:22:45 -07002453 * Hotkeys
2454 */
2455static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2456{
2457 acpi_status status;
2458 u32 result;
2459
2460 status = acpi_evaluate_object(dev->acpi_dev->handle,
2461 "ENAB", NULL, NULL);
2462 if (ACPI_FAILURE(status))
2463 return -ENODEV;
2464
Azael Avalosb116fd002015-09-09 11:28:19 -06002465 /*
2466 * Enable the "Special Functions" mode only if they are
2467 * supported and if they are activated.
2468 */
2469 if (dev->kbd_function_keys_supported && dev->special_functions)
2470 result = hci_write(dev, HCI_HOTKEY_EVENT,
2471 HCI_HOTKEY_SPECIAL_FUNCTIONS);
2472 else
2473 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2474
Azael Avalos1f28f292014-12-04 20:22:45 -07002475 if (result == TOS_FAILURE)
2476 return -EIO;
2477 else if (result == TOS_NOT_SUPPORTED)
2478 return -ENODEV;
2479
2480 return 0;
2481}
2482
Seth Forshee29cd2932012-01-18 13:44:09 -06002483static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2484 struct serio *port)
2485{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002486 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002487 return false;
2488
2489 if (unlikely(data == 0xe0))
2490 return false;
2491
2492 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2493 schedule_work(&toshiba_acpi->hotkey_work);
2494 return true;
2495 }
2496
2497 return false;
2498}
2499
2500static void toshiba_acpi_hotkey_work(struct work_struct *work)
2501{
2502 acpi_handle ec_handle = ec_get_handle();
2503 acpi_status status;
2504
2505 if (!ec_handle)
2506 return;
2507
2508 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2509 if (ACPI_FAILURE(status))
2510 pr_err("ACPI NTFY method execution failed\n");
2511}
2512
2513/*
2514 * Returns hotkey scancode, or < 0 on failure.
2515 */
2516static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2517{
Zhang Rui74facaf2013-09-03 08:32:15 +08002518 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002519 acpi_status status;
2520
Zhang Rui74facaf2013-09-03 08:32:15 +08002521 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2522 NULL, &value);
2523 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002524 pr_err("ACPI INFO method execution failed\n");
2525 return -EIO;
2526 }
2527
Zhang Rui74facaf2013-09-03 08:32:15 +08002528 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002529}
2530
2531static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2532 int scancode)
2533{
2534 if (scancode == 0x100)
2535 return;
2536
Darren Harte0769fe2015-02-11 20:50:08 -08002537 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002538 if (scancode & 0x80)
2539 return;
2540
2541 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2542 pr_info("Unknown key %x\n", scancode);
2543}
2544
Azael Avalos71454d72014-12-04 20:22:46 -07002545static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2546{
Azael Avalos71454d72014-12-04 20:22:46 -07002547 if (dev->info_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002548 int scancode = toshiba_acpi_query_hotkey(dev);
2549
2550 if (scancode < 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002551 pr_err("Failed to query hotkey event\n");
Azael Avalos7deef552015-07-22 18:09:10 -06002552 } else if (scancode != 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002553 toshiba_acpi_report_hotkey(dev, scancode);
Azael Avalos7deef552015-07-22 18:09:10 -06002554 dev->key_event_valid = 1;
2555 dev->last_key_event = scancode;
2556 }
Azael Avalos71454d72014-12-04 20:22:46 -07002557 } else if (dev->system_event_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002558 u32 result;
2559 u32 value;
2560 int retries = 3;
2561
Azael Avalos71454d72014-12-04 20:22:46 -07002562 do {
Azael Avalos7deef552015-07-22 18:09:10 -06002563 result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
2564 switch (result) {
Azael Avalos71454d72014-12-04 20:22:46 -07002565 case TOS_SUCCESS:
2566 toshiba_acpi_report_hotkey(dev, (int)value);
Azael Avalos7deef552015-07-22 18:09:10 -06002567 dev->key_event_valid = 1;
2568 dev->last_key_event = value;
Azael Avalos71454d72014-12-04 20:22:46 -07002569 break;
2570 case TOS_NOT_SUPPORTED:
2571 /*
2572 * This is a workaround for an unresolved
2573 * issue on some machines where system events
2574 * sporadically become disabled.
2575 */
Azael Avalos7deef552015-07-22 18:09:10 -06002576 result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
2577 if (result == TOS_SUCCESS)
2578 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002579 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002580 default:
2581 retries--;
2582 break;
2583 }
Azael Avalos7deef552015-07-22 18:09:10 -06002584 } while (retries && result != TOS_FIFO_EMPTY);
Azael Avalos71454d72014-12-04 20:22:46 -07002585 }
2586}
2587
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002588static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002589{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002590 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002591 acpi_handle ec_handle;
Azael Avalosa2b34712015-03-20 16:55:17 -06002592 int error;
2593
Azael Avalosa88bc062015-07-22 18:09:12 -06002594 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID)) {
2595 pr_info("WMI event detected, hotkeys will not be monitored\n");
2596 return 0;
2597 }
2598
Azael Avalosa2b34712015-03-20 16:55:17 -06002599 error = toshiba_acpi_enable_hotkeys(dev);
2600 if (error)
2601 return error;
2602
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002603 if (toshiba_hotkey_event_type_get(dev, &dev->hotkey_event_type))
Azael Avalos53147b62015-09-09 11:25:45 -06002604 pr_notice("Unable to query Hotkey Event Type\n");
2605
Seth Forshee135740d2011-09-20 16:55:49 -05002606 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002607 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002608 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002609
2610 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002611 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002612 dev->hotkey_dev->id.bustype = BUS_HOST;
2613
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002614 if (dev->hotkey_event_type == HCI_SYSTEM_TYPE1 ||
Azael Avalosa2b34712015-03-20 16:55:17 -06002615 !dev->kbd_function_keys_supported)
2616 keymap = toshiba_acpi_keymap;
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002617 else if (dev->hotkey_event_type == HCI_SYSTEM_TYPE2 ||
Azael Avalosa2b34712015-03-20 16:55:17 -06002618 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002619 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002620 else
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002621 pr_info("Unknown event type received %x\n",
2622 dev->hotkey_event_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002623 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002624 if (error)
2625 goto err_free_dev;
2626
Seth Forshee29cd2932012-01-18 13:44:09 -06002627 /*
2628 * For some machines the SCI responsible for providing hotkey
2629 * notification doesn't fire. We can trigger the notification
2630 * whenever the Fn key is pressed using the NTFY method, if
2631 * supported, so if it's present set up an i8042 key filter
2632 * for this purpose.
2633 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002634 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002635 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002636 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2637
2638 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2639 if (error) {
2640 pr_err("Error installing key filter\n");
2641 goto err_free_keymap;
2642 }
2643
2644 dev->ntfy_supported = 1;
2645 }
2646
2647 /*
2648 * Determine hotkey query interface. Prefer using the INFO
2649 * method when it is available.
2650 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002651 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002652 dev->info_supported = 1;
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002653 else if (hci_write(dev, HCI_SYSTEM_EVENT, 1) == TOS_SUCCESS)
2654 dev->system_event_supported = 1;
Seth Forshee29cd2932012-01-18 13:44:09 -06002655
2656 if (!dev->info_supported && !dev->system_event_supported) {
2657 pr_warn("No hotkey query interface found\n");
2658 goto err_remove_filter;
2659 }
2660
Seth Forshee135740d2011-09-20 16:55:49 -05002661 error = input_register_device(dev->hotkey_dev);
2662 if (error) {
2663 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002664 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002665 }
2666
2667 return 0;
2668
Seth Forshee29cd2932012-01-18 13:44:09 -06002669 err_remove_filter:
2670 if (dev->ntfy_supported)
2671 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002672 err_free_keymap:
2673 sparse_keymap_free(dev->hotkey_dev);
2674 err_free_dev:
2675 input_free_device(dev->hotkey_dev);
2676 dev->hotkey_dev = NULL;
2677 return error;
2678}
2679
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002680static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002681{
2682 struct backlight_properties props;
2683 int brightness;
2684 int ret;
2685
2686 /*
2687 * Some machines don't support the backlight methods at all, and
2688 * others support it read-only. Either of these is pretty useless,
2689 * so only register the backlight device if the backlight method
2690 * supports both reads and writes.
2691 */
2692 brightness = __get_lcd_brightness(dev);
2693 if (brightness < 0)
2694 return 0;
Azael Avalosbae53362015-11-15 20:32:47 -07002695 /*
2696 * If transflective backlight is supported and the brightness is zero
2697 * (lowest brightness level), the set_lcd_brightness function will
2698 * activate the transflective backlight, making the LCD appear to be
2699 * turned off, simply increment the brightness level to avoid that.
2700 */
2701 if (dev->tr_backlight_supported && brightness == 0)
2702 brightness++;
Seth Forshee62cce752012-04-19 11:23:50 -05002703 ret = set_lcd_brightness(dev, brightness);
2704 if (ret) {
2705 pr_debug("Backlight method is read-only, disabling backlight support\n");
2706 return 0;
2707 }
2708
Hans de Goede358d6a22015-04-21 12:01:32 +02002709 /*
2710 * Tell acpi-video-detect code to prefer vendor backlight on all
2711 * systems with transflective backlight and on dmi matched systems.
2712 */
2713 if (dev->tr_backlight_supported ||
2714 dmi_check_system(toshiba_vendor_backlight_dmi))
Hans de Goede234b7cf2015-06-16 16:28:11 +02002715 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
Hans de Goede358d6a22015-04-21 12:01:32 +02002716
Hans de Goede234b7cf2015-06-16 16:28:11 +02002717 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
Hans de Goede358d6a22015-04-21 12:01:32 +02002718 return 0;
2719
Matthew Garrett53039f22012-06-01 11:02:36 -04002720 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002721 props.type = BACKLIGHT_PLATFORM;
2722 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002723
Darren Harte0769fe2015-02-11 20:50:08 -08002724 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002725 if (dev->tr_backlight_supported)
2726 props.max_brightness++;
2727
Seth Forshee62cce752012-04-19 11:23:50 -05002728 dev->backlight_dev = backlight_device_register("toshiba",
2729 &dev->acpi_dev->dev,
2730 dev,
2731 &toshiba_backlight_data,
2732 &props);
2733 if (IS_ERR(dev->backlight_dev)) {
2734 ret = PTR_ERR(dev->backlight_dev);
2735 pr_err("Could not register toshiba backlight device\n");
2736 dev->backlight_dev = NULL;
2737 return ret;
2738 }
2739
2740 dev->backlight_dev->props.brightness = brightness;
2741 return 0;
2742}
2743
Azael Avalos0409cbc2015-07-31 21:58:13 -06002744static void print_supported_features(struct toshiba_acpi_dev *dev)
2745{
2746 pr_info("Supported laptop features:");
2747
2748 if (dev->hotkey_dev)
2749 pr_cont(" hotkeys");
2750 if (dev->backlight_dev)
2751 pr_cont(" backlight");
2752 if (dev->video_supported)
2753 pr_cont(" video-out");
2754 if (dev->fan_supported)
2755 pr_cont(" fan");
2756 if (dev->tr_backlight_supported)
2757 pr_cont(" transflective-backlight");
2758 if (dev->illumination_supported)
2759 pr_cont(" illumination");
2760 if (dev->kbd_illum_supported)
2761 pr_cont(" keyboard-backlight");
2762 if (dev->touchpad_supported)
2763 pr_cont(" touchpad");
2764 if (dev->eco_supported)
2765 pr_cont(" eco-led");
2766 if (dev->accelerometer_supported)
2767 pr_cont(" accelerometer-axes");
2768 if (dev->usb_sleep_charge_supported)
2769 pr_cont(" usb-sleep-charge");
2770 if (dev->usb_rapid_charge_supported)
2771 pr_cont(" usb-rapid-charge");
2772 if (dev->usb_sleep_music_supported)
2773 pr_cont(" usb-sleep-music");
2774 if (dev->kbd_function_keys_supported)
2775 pr_cont(" special-function-keys");
2776 if (dev->panel_power_on_supported)
2777 pr_cont(" panel-power-on");
2778 if (dev->usb_three_supported)
2779 pr_cont(" usb3");
Azael Avalos6873f462015-11-23 10:49:10 -07002780 if (dev->wwan_supported)
2781 pr_cont(" wwan");
Azael Avalos0409cbc2015-07-31 21:58:13 -06002782
2783 pr_cont("\n");
2784}
2785
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002786static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002787{
2788 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2789
Azael Avalosfc5462f2015-07-22 18:09:11 -06002790 misc_deregister(&dev->miscdev);
2791
Seth Forshee36d03f92011-09-20 16:55:53 -05002792 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002793
Azael Avalos360f0f32014-03-25 20:38:31 -06002794 if (dev->sysfs_created)
2795 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2796 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002797
Seth Forshee29cd2932012-01-18 13:44:09 -06002798 if (dev->ntfy_supported) {
2799 i8042_remove_filter(toshiba_acpi_i8042_filter);
2800 cancel_work_sync(&dev->hotkey_work);
2801 }
2802
Seth Forshee135740d2011-09-20 16:55:49 -05002803 if (dev->hotkey_dev) {
2804 input_unregister_device(dev->hotkey_dev);
2805 sparse_keymap_free(dev->hotkey_dev);
2806 }
2807
Markus Elfring00981812014-11-24 20:30:29 +01002808 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002809
Azael Avalosea215a3f2015-07-31 21:58:12 -06002810 if (dev->illumination_led_registered)
Seth Forshee135740d2011-09-20 16:55:49 -05002811 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002812
Azael Avalos360f0f32014-03-25 20:38:31 -06002813 if (dev->kbd_led_registered)
2814 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002815
Azael Avalosea215a3f2015-07-31 21:58:12 -06002816 if (dev->eco_led_registered)
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002817 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002818
Azael Avalos2fdde832015-11-23 10:49:11 -07002819 if (dev->wwan_rfk) {
2820 rfkill_unregister(dev->wwan_rfk);
2821 rfkill_destroy(dev->wwan_rfk);
2822 }
2823
Seth Forshee29cd2932012-01-18 13:44:09 -06002824 if (toshiba_acpi)
2825 toshiba_acpi = NULL;
2826
Seth Forshee135740d2011-09-20 16:55:49 -05002827 kfree(dev);
2828
2829 return 0;
2830}
2831
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002832static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002833{
Zhang Ruie2e19602013-09-03 08:32:06 +08002834 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002835 return "GHCI";
2836
Zhang Ruie2e19602013-09-03 08:32:06 +08002837 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002838 return "SPFC";
2839
2840 return NULL;
2841}
2842
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002843static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002844{
2845 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002846 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05002847 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002848 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002849
Seth Forshee29cd2932012-01-18 13:44:09 -06002850 if (toshiba_acpi)
2851 return -EBUSY;
2852
Seth Forshee135740d2011-09-20 16:55:49 -05002853 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2854 TOSHIBA_ACPI_VERSION);
2855
Seth Forsheea540d6b2011-09-20 16:55:52 -05002856 hci_method = find_hci_method(acpi_dev->handle);
2857 if (!hci_method) {
2858 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002859 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002860 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002861
Seth Forshee135740d2011-09-20 16:55:49 -05002862 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2863 if (!dev)
2864 return -ENOMEM;
2865 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002866 dev->method_hci = hci_method;
Azael Avalosfc5462f2015-07-22 18:09:11 -06002867 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2868 dev->miscdev.name = "toshiba_acpi";
2869 dev->miscdev.fops = &toshiba_acpi_fops;
2870
2871 ret = misc_register(&dev->miscdev);
2872 if (ret) {
2873 pr_err("Failed to register miscdevice\n");
2874 kfree(dev);
2875 return ret;
2876 }
2877
Seth Forshee135740d2011-09-20 16:55:49 -05002878 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002879 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002880
Azael Avalosa2b34712015-03-20 16:55:17 -06002881 /* Query the BIOS for supported features */
2882
2883 /*
2884 * The "Special Functions" are always supported by the laptops
2885 * with the new keyboard layout, query for its presence to help
2886 * determine the keymap layout to use.
2887 */
Azael Avalosb116fd002015-09-09 11:28:19 -06002888 ret = toshiba_function_keys_get(dev, &dev->special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002889 dev->kbd_function_keys_supported = !ret;
2890
Azael Avalosd2f20612015-11-04 09:28:26 -07002891 dev->hotkey_event_type = 0;
Seth Forshee6e02cc72011-09-20 16:55:51 -05002892 if (toshiba_acpi_setup_keyboard(dev))
2893 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002894
Azael Avalos695f6062015-07-22 18:09:13 -06002895 /* Determine whether or not BIOS supports transflective backlight */
2896 ret = get_tr_backlight_status(dev, &dummy);
2897 dev->tr_backlight_supported = !ret;
2898
Seth Forshee62cce752012-04-19 11:23:50 -05002899 ret = toshiba_acpi_setup_backlight(dev);
2900 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002901 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002902
Azael Avalosea215a3f2015-07-31 21:58:12 -06002903 toshiba_illumination_available(dev);
2904 if (dev->illumination_supported) {
Seth Forshee135740d2011-09-20 16:55:49 -05002905 dev->led_dev.name = "toshiba::illumination";
2906 dev->led_dev.max_brightness = 1;
2907 dev->led_dev.brightness_set = toshiba_illumination_set;
2908 dev->led_dev.brightness_get = toshiba_illumination_get;
2909 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -06002910 dev->illumination_led_registered = true;
Seth Forshee135740d2011-09-20 16:55:49 -05002911 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002912
Azael Avalosea215a3f2015-07-31 21:58:12 -06002913 toshiba_eco_mode_available(dev);
2914 if (dev->eco_supported) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002915 dev->eco_led.name = "toshiba::eco_mode";
2916 dev->eco_led.max_brightness = 1;
2917 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2918 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2919 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
Azael Avalosea215a3f2015-07-31 21:58:12 -06002920 dev->eco_led_registered = true;
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002921 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002922
Azael Avalosea215a3f2015-07-31 21:58:12 -06002923 toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002924 /*
2925 * Only register the LED if KBD illumination is supported
2926 * and the keyboard backlight operation mode is set to FN-Z
2927 */
2928 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2929 dev->kbd_led.name = "toshiba::kbd_backlight";
2930 dev->kbd_led.max_brightness = 1;
2931 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2932 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2933 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
Azael Avalosea215a3f2015-07-31 21:58:12 -06002934 dev->kbd_led_registered = true;
Azael Avalos360f0f32014-03-25 20:38:31 -06002935 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002936
Azael Avalos9d8658a2014-03-25 20:38:32 -06002937 ret = toshiba_touchpad_get(dev, &dummy);
2938 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002939
Azael Avalosea215a3f2015-07-31 21:58:12 -06002940 toshiba_accelerometer_available(dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002941
Azael Avalosc8c91842015-04-02 19:26:20 -06002942 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07002943
Azael Avalosbb3fe012015-01-18 18:30:24 -07002944 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2945 dev->usb_rapid_charge_supported = !ret;
2946
Azael Avalos172ce0a2015-01-18 18:30:25 -07002947 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2948 dev->usb_sleep_music_supported = !ret;
2949
Azael Avalos35d53ce2015-02-10 21:09:19 -07002950 ret = toshiba_panel_power_on_get(dev, &dummy);
2951 dev->panel_power_on_supported = !ret;
2952
Azael Avalos17fe4b32015-02-10 21:09:20 -07002953 ret = toshiba_usb_three_get(dev, &dummy);
2954 dev->usb_three_supported = !ret;
2955
Seth Forshee36d03f92011-09-20 16:55:53 -05002956 ret = get_video_status(dev, &dummy);
2957 dev->video_supported = !ret;
2958
2959 ret = get_fan_status(dev, &dummy);
2960 dev->fan_supported = !ret;
2961
Azael Avalos6873f462015-11-23 10:49:10 -07002962 toshiba_wwan_available(dev);
Azael Avalos2fdde832015-11-23 10:49:11 -07002963 if (dev->wwan_supported)
2964 toshiba_acpi_setup_wwan_rfkill(dev);
Azael Avalos6873f462015-11-23 10:49:10 -07002965
Azael Avalos0409cbc2015-07-31 21:58:13 -06002966 print_supported_features(dev);
2967
Azael Avalos360f0f32014-03-25 20:38:31 -06002968 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2969 &toshiba_attr_group);
2970 if (ret) {
2971 dev->sysfs_created = 0;
2972 goto error;
2973 }
2974 dev->sysfs_created = !ret;
2975
Seth Forshee36d03f92011-09-20 16:55:53 -05002976 create_toshiba_proc_entries(dev);
2977
Seth Forshee29cd2932012-01-18 13:44:09 -06002978 toshiba_acpi = dev;
2979
Seth Forshee135740d2011-09-20 16:55:49 -05002980 return 0;
2981
2982error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002983 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002984 return ret;
2985}
2986
2987static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2988{
2989 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002990
Azael Avalos71454d72014-12-04 20:22:46 -07002991 switch (event) {
2992 case 0x80: /* Hotkeys and some system events */
Azael Avalosa88bc062015-07-22 18:09:12 -06002993 /*
2994 * Machines with this WMI GUID aren't supported due to bugs in
2995 * their AML.
2996 *
2997 * Return silently to avoid triggering a netlink event.
2998 */
2999 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
3000 return;
Azael Avalos71454d72014-12-04 20:22:46 -07003001 toshiba_acpi_process_hotkeys(dev);
3002 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07003003 case 0x81: /* Dock events */
3004 case 0x82:
3005 case 0x83:
3006 pr_info("Dock event received %x\n", event);
3007 break;
3008 case 0x88: /* Thermal events */
3009 pr_info("Thermal event received\n");
3010 break;
3011 case 0x8f: /* LID closed */
3012 case 0x90: /* LID is closed and Dock has been ejected */
3013 break;
3014 case 0x8c: /* SATA power events */
3015 case 0x8b:
3016 pr_info("SATA power event received %x\n", event);
3017 break;
Azael Avalos80546902014-12-04 20:22:47 -07003018 case 0x92: /* Keyboard backlight mode changed */
Azael Avalos65e3cf92015-11-23 10:51:30 -07003019 toshiba_acpi->kbd_event_generated = true;
Azael Avalos80546902014-12-04 20:22:47 -07003020 /* Update sysfs entries */
Azael Avalos65e3cf92015-11-23 10:51:30 -07003021 if (sysfs_update_group(&acpi_dev->dev.kobj,
3022 &toshiba_attr_group))
Azael Avalos80546902014-12-04 20:22:47 -07003023 pr_err("Unable to update sysfs entries\n");
3024 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07003025 case 0x85: /* Unknown */
3026 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07003027 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07003028 case 0x94: /* Unknown */
3029 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07003030 default:
3031 pr_info("Unknown event received %x\n", event);
3032 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06003033 }
Azael Avalosbab09e22015-03-06 18:14:41 -07003034
3035 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
3036 dev_name(&acpi_dev->dev),
Azael Avalos13ae84f2015-11-15 20:33:46 -07003037 event, (event == 0x80) ?
3038 dev->last_key_event : 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05003039}
3040
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02003041#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003042static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06003043{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003044 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06003045
Azael Avalos1e574db2015-07-22 19:37:49 -06003046 if (dev->hotkey_dev) {
3047 u32 result;
3048
Azael Avalosd37782b2015-05-06 09:35:10 -06003049 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Azael Avalos1e574db2015-07-22 19:37:49 -06003050 if (result != TOS_SUCCESS)
3051 pr_info("Unable to disable hotkeys\n");
3052 }
Seth Forshee29cd2932012-01-18 13:44:09 -06003053
3054 return 0;
3055}
3056
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003057static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06003058{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003059 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06003060
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003061 if (dev->hotkey_dev) {
Azael Avalos2fdde832015-11-23 10:49:11 -07003062 if (toshiba_acpi_enable_hotkeys(dev))
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003063 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003064 }
Seth Forshee29cd2932012-01-18 13:44:09 -06003065
Azael Avalos2fdde832015-11-23 10:49:11 -07003066 if (dev->wwan_rfk) {
3067 if (!toshiba_wireless_status(dev))
3068 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
3069 }
3070
Seth Forshee29cd2932012-01-18 13:44:09 -06003071 return 0;
3072}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02003073#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05003074
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003075static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
3076 toshiba_acpi_suspend, toshiba_acpi_resume);
3077
Seth Forshee135740d2011-09-20 16:55:49 -05003078static struct acpi_driver toshiba_acpi_driver = {
3079 .name = "Toshiba ACPI driver",
3080 .owner = THIS_MODULE,
3081 .ids = toshiba_device_ids,
3082 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
3083 .ops = {
3084 .add = toshiba_acpi_add,
3085 .remove = toshiba_acpi_remove,
3086 .notify = toshiba_acpi_notify,
3087 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003088 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05003089};
Holger Machtc9263552006-10-20 14:30:29 -07003090
Len Brown4be44fc2005-08-05 00:44:28 -04003091static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Seth Forshee135740d2011-09-20 16:55:49 -05003093 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
3096 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05003097 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04003098 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
3100
Seth Forshee135740d2011-09-20 16:55:49 -05003101 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
3102 if (ret) {
3103 pr_err("Failed to register ACPI driver: %d\n", ret);
3104 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07003105 }
3106
Seth Forshee135740d2011-09-20 16:55:49 -05003107 return ret;
3108}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04003109
Seth Forshee135740d2011-09-20 16:55:49 -05003110static void __exit toshiba_acpi_exit(void)
3111{
3112 acpi_bus_unregister_driver(&toshiba_acpi_driver);
3113 if (toshiba_proc_dir)
3114 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117module_init(toshiba_acpi_init);
3118module_exit(toshiba_acpi_exit);