blob: c6f92ec636e7d40cedd896b8f3b7f25111389600 [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 Avalos763ff322016-01-25 11:29:10 -0700120#define HCI_COOLING_METHOD 0x007f
Azael Avalos360f0f32014-03-25 20:38:31 -0600121#define HCI_KBD_ILLUMINATION 0x0095
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600122#define HCI_ECO_MODE 0x0097
Azael Avalos5a2813e2014-03-25 20:38:34 -0600123#define HCI_ACCELEROMETER2 0x00a6
Azael Avalos56e6b352015-03-20 16:55:16 -0600124#define HCI_SYSTEM_INFO 0xc000
Azael Avalos35d53ce2015-02-10 21:09:19 -0700125#define SCI_PANEL_POWER_ON 0x010d
Azael Avalosfdb79082014-03-25 20:38:30 -0600126#define SCI_ILLUMINATION 0x014e
Azael Avalose26ffe52015-01-18 18:30:22 -0700127#define SCI_USB_SLEEP_CHARGE 0x0150
Azael Avalos360f0f32014-03-25 20:38:31 -0600128#define SCI_KBD_ILLUM_STATUS 0x015c
Azael Avalos172ce0a2015-01-18 18:30:25 -0700129#define SCI_USB_SLEEP_MUSIC 0x015e
Azael Avalos17fe4b32015-02-10 21:09:20 -0700130#define SCI_USB_THREE 0x0169
Azael Avalos9d8658a2014-03-25 20:38:32 -0600131#define SCI_TOUCHPAD 0x050e
Azael Avalosbae84192015-02-10 21:09:18 -0700132#define SCI_KBD_FUNCTION_KEYS 0x0522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600134/* Field definitions */
Azael Avalos5a2813e2014-03-25 20:38:34 -0600135#define HCI_ACCEL_MASK 0x7fff
Seth Forshee29cd2932012-01-18 13:44:09 -0600136#define HCI_HOTKEY_DISABLE 0x0b
Azael Avalos52cbae02015-09-09 11:28:20 -0600137#define HCI_HOTKEY_ENABLE 0x01
Azael Avalosfb42d1f2015-03-20 16:55:18 -0600138#define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define HCI_LCD_BRIGHTNESS_BITS 3
140#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
141#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
Azael Avalos360f0f32014-03-25 20:38:31 -0600142#define HCI_MISC_SHIFT 0x10
Azael Avalos56e6b352015-03-20 16:55:16 -0600143#define HCI_SYSTEM_TYPE1 0x10
144#define HCI_SYSTEM_TYPE2 0x11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define HCI_VIDEO_OUT_LCD 0x1
146#define HCI_VIDEO_OUT_CRT 0x2
147#define HCI_VIDEO_OUT_TV 0x4
Azael Avalos93f8c162014-09-12 18:50:36 -0600148#define SCI_KBD_MODE_MASK 0x1f
Azael Avalos360f0f32014-03-25 20:38:31 -0600149#define SCI_KBD_MODE_FNZ 0x1
150#define SCI_KBD_MODE_AUTO 0x2
Azael Avalos93f8c162014-09-12 18:50:36 -0600151#define SCI_KBD_MODE_ON 0x8
152#define SCI_KBD_MODE_OFF 0x10
153#define SCI_KBD_TIME_MAX 0x3c001a
Azael Avalos6873f462015-11-23 10:49:10 -0700154#define HCI_WIRELESS_STATUS 0x1
155#define HCI_WIRELESS_WWAN 0x3
156#define HCI_WIRELESS_WWAN_STATUS 0x2000
157#define HCI_WIRELESS_WWAN_POWER 0x4000
Azael Avalose26ffe52015-01-18 18:30:22 -0700158#define SCI_USB_CHARGE_MODE_MASK 0xff
Azael Avalosc8c91842015-04-02 19:26:20 -0600159#define SCI_USB_CHARGE_DISABLED 0x00
160#define SCI_USB_CHARGE_ALTERNATE 0x09
161#define SCI_USB_CHARGE_TYPICAL 0x11
162#define SCI_USB_CHARGE_AUTO 0x21
Azael Avalos182bcaa2015-01-18 18:30:23 -0700163#define SCI_USB_CHARGE_BAT_MASK 0x7
164#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
165#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
166#define SCI_USB_CHARGE_BAT_LVL 0x0200
Azael Avalosbb3fe012015-01-18 18:30:24 -0700167#define SCI_USB_CHARGE_RAPID_DSP 0x0300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Seth Forshee135740d2011-09-20 16:55:49 -0500169struct toshiba_acpi_dev {
170 struct acpi_device *acpi_dev;
171 const char *method_hci;
Seth Forshee135740d2011-09-20 16:55:49 -0500172 struct input_dev *hotkey_dev;
Seth Forshee29cd2932012-01-18 13:44:09 -0600173 struct work_struct hotkey_work;
Seth Forshee135740d2011-09-20 16:55:49 -0500174 struct backlight_device *backlight_dev;
175 struct led_classdev led_dev;
Azael Avalos360f0f32014-03-25 20:38:31 -0600176 struct led_classdev kbd_led;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600177 struct led_classdev eco_led;
Azael Avalosfc5462f2015-07-22 18:09:11 -0600178 struct miscdevice miscdev;
Azael Avalos2fdde832015-11-23 10:49:11 -0700179 struct rfkill *wwan_rfk;
Seth Forshee36d03f92011-09-20 16:55:53 -0500180
Seth Forshee135740d2011-09-20 16:55:49 -0500181 int force_fan;
182 int last_key_event;
183 int key_event_valid;
Azael Avalos93f8c162014-09-12 18:50:36 -0600184 int kbd_type;
Azael Avalos360f0f32014-03-25 20:38:31 -0600185 int kbd_mode;
186 int kbd_time;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700187 int usbsc_bat_level;
Azael Avalosc8c91842015-04-02 19:26:20 -0600188 int usbsc_mode_base;
Azael Avalosa2b34712015-03-20 16:55:17 -0600189 int hotkey_event_type;
Azael Avalos763ff322016-01-25 11:29:10 -0700190 int max_cooling_method;
Seth Forshee135740d2011-09-20 16:55:49 -0500191
Dan Carpenter592b7462012-01-15 14:28:20 +0300192 unsigned int illumination_supported:1;
193 unsigned int video_supported:1;
194 unsigned int fan_supported:1;
195 unsigned int system_event_supported:1;
Seth Forshee29cd2932012-01-18 13:44:09 -0600196 unsigned int ntfy_supported:1;
197 unsigned int info_supported:1;
Akio Idehara121b7b02012-04-06 01:46:43 +0900198 unsigned int tr_backlight_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600199 unsigned int kbd_illum_supported:1;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600200 unsigned int touchpad_supported:1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600201 unsigned int eco_supported:1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600202 unsigned int accelerometer_supported:1;
Azael Avalose26ffe52015-01-18 18:30:22 -0700203 unsigned int usb_sleep_charge_supported:1;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700204 unsigned int usb_rapid_charge_supported:1;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700205 unsigned int usb_sleep_music_supported:1;
Azael Avalosbae84192015-02-10 21:09:18 -0700206 unsigned int kbd_function_keys_supported:1;
Azael Avalos35d53ce2015-02-10 21:09:19 -0700207 unsigned int panel_power_on_supported:1;
Azael Avalos17fe4b32015-02-10 21:09:20 -0700208 unsigned int usb_three_supported:1;
Azael Avalos6873f462015-11-23 10:49:10 -0700209 unsigned int wwan_supported:1;
Azael Avalos763ff322016-01-25 11:29:10 -0700210 unsigned int cooling_method_supported:1;
Azael Avalos360f0f32014-03-25 20:38:31 -0600211 unsigned int sysfs_created:1;
Azael Avalosb116fd002015-09-09 11:28:19 -0600212 unsigned int special_functions;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600213
Azael Avalos65e3cf92015-11-23 10:51:30 -0700214 bool kbd_event_generated;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600215 bool kbd_led_registered;
216 bool illumination_led_registered;
217 bool eco_led_registered;
Azael Avalos6873f462015-11-23 10:49:10 -0700218 bool killswitch;
Seth Forshee135740d2011-09-20 16:55:49 -0500219};
220
Seth Forshee29cd2932012-01-18 13:44:09 -0600221static struct toshiba_acpi_dev *toshiba_acpi;
222
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800223static const struct acpi_device_id toshiba_device_ids[] = {
224 {"TOS6200", 0},
Ondrej Zary63a9e012014-11-10 00:11:54 +0100225 {"TOS6207", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400226 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800227 {"TOS1900", 0},
228 {"", 0},
229};
230MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
231
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800232static const struct key_entry toshiba_acpi_keymap[] = {
Unai Uribarrifec278a2014-01-14 11:06:47 +0100233 { KE_KEY, 0x9e, { KEY_RFKILL } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700234 { KE_KEY, 0x101, { KEY_MUTE } },
235 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
236 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600237 { KE_KEY, 0x10f, { KEY_TAB } },
Azael Avalosaf502832012-01-18 13:44:10 -0600238 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
239 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700240 { KE_KEY, 0x13b, { KEY_COFFEE } },
241 { KE_KEY, 0x13c, { KEY_BATTERY } },
242 { KE_KEY, 0x13d, { KEY_SLEEP } },
243 { KE_KEY, 0x13e, { KEY_SUSPEND } },
244 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
245 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
246 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
247 { KE_KEY, 0x142, { KEY_WLAN } },
Azael Avalosaf502832012-01-18 13:44:10 -0600248 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
Jon Dowlanda49010f2010-10-27 00:24:59 +0100249 { KE_KEY, 0x17f, { KEY_FN } },
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700250 { KE_KEY, 0xb05, { KEY_PROG2 } },
251 { KE_KEY, 0xb06, { KEY_WWW } },
252 { KE_KEY, 0xb07, { KEY_MAIL } },
253 { KE_KEY, 0xb30, { KEY_STOP } },
254 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
255 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
256 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
257 { KE_KEY, 0xb5a, { KEY_MEDIA } },
Azael Avalos408a5d12014-09-05 11:14:03 -0600258 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
259 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
260 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
261 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
262 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
Dmitry Torokhov384a7cd2010-08-04 22:30:19 -0700263 { KE_END, 0 },
Matthew Garrett6335e4d2010-02-25 15:20:54 -0500264};
265
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200266static const struct key_entry toshiba_acpi_alt_keymap[] = {
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200267 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
268 { KE_KEY, 0x103, { KEY_ZOOMIN } },
Azael Avalose6efad72014-08-04 09:21:02 -0600269 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200270 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200271 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
272 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
Azael Avalosd50c9002015-07-22 19:37:46 -0600273 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200274 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
Azael Avalosd50c9002015-07-22 19:37:46 -0600275 { KE_KEY, 0x157, { KEY_MUTE } },
276 { KE_KEY, 0x158, { KEY_WLAN } },
Takashi Iwaife808bfb2014-04-29 15:15:38 +0200277 { KE_END, 0 },
278};
279
Darren Harte0769fe2015-02-11 20:50:08 -0800280/*
Hans de Goede358d6a22015-04-21 12:01:32 +0200281 * List of models which have a broken acpi-video backlight interface and thus
282 * need to use the toshiba (vendor) interface instead.
283 */
284static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
285 {}
286};
287
288/*
Darren Harte0769fe2015-02-11 20:50:08 -0800289 * Utility
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
291
Azael Avalosb5163992015-02-10 23:43:56 -0700292static inline void _set_bit(u32 *word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 *word = (*word & ~mask) | (mask * value);
295}
296
Darren Harte0769fe2015-02-11 20:50:08 -0800297/*
298 * ACPI interface wrappers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
300
Len Brown4be44fc2005-08-05 00:44:28 -0400301static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 acpi_status status;
304
Zhang Rui619400d2013-09-03 08:31:56 +0800305 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
Seth Forshee32bcd5c2011-09-20 16:55:50 -0500306 return (status == AE_OK) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Darren Harte0769fe2015-02-11 20:50:08 -0800309/*
310 * Perform a raw configuration call. Here we don't care about input or output
Azael Avalos258c5902014-09-29 20:40:07 -0600311 * buffer format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Azael Avalos258c5902014-09-29 20:40:07 -0600313static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
314 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct acpi_object_list params;
Azael Avalos258c5902014-09-29 20:40:07 -0600317 union acpi_object in_objs[TCI_WORDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct acpi_buffer results;
Azael Avalos258c5902014-09-29 20:40:07 -0600319 union acpi_object out_objs[TCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 acpi_status status;
321 int i;
322
Azael Avalos258c5902014-09-29 20:40:07 -0600323 params.count = TCI_WORDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 params.pointer = in_objs;
Azael Avalos258c5902014-09-29 20:40:07 -0600325 for (i = 0; i < TCI_WORDS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 in_objs[i].type = ACPI_TYPE_INTEGER;
327 in_objs[i].integer.value = in[i];
328 }
329
330 results.length = sizeof(out_objs);
331 results.pointer = out_objs;
332
Seth Forshee6e02cc72011-09-20 16:55:51 -0500333 status = acpi_evaluate_object(dev->acpi_dev->handle,
334 (char *)dev->method_hci, &params,
Len Brown4be44fc2005-08-05 00:44:28 -0400335 &results);
Azael Avalos258c5902014-09-29 20:40:07 -0600336 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
Azael Avalosb5163992015-02-10 23:43:56 -0700337 for (i = 0; i < out_objs->package.count; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 out[i] = out_objs->package.elements[i].integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 return status;
342}
343
Darren Harte0769fe2015-02-11 20:50:08 -0800344/*
Azael Avalosd37782b2015-05-06 09:35:10 -0600345 * Common hci tasks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
347 * In addition to the ACPI status, the HCI system returns a result which
348 * may be useful (such as "not supported").
349 */
350
Azael Avalosd37782b2015-05-06 09:35:10 -0600351static u32 hci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Azael Avalos258c5902014-09-29 20:40:07 -0600353 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
354 u32 out[TCI_WORDS];
355 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600356
357 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Azael Avalosd37782b2015-05-06 09:35:10 -0600360static u32 hci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Azael Avalos258c5902014-09-29 20:40:07 -0600362 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
363 u32 out[TCI_WORDS];
364 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700365
Azael Avalos893f3f62014-09-29 20:40:09 -0600366 if (ACPI_FAILURE(status))
367 return TOS_FAILURE;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600370
371 return out[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Darren Harte0769fe2015-02-11 20:50:08 -0800374/*
375 * Common sci tasks
Azael Avalos84a62732014-03-25 20:38:29 -0600376 */
377
378static int sci_open(struct toshiba_acpi_dev *dev)
379{
Azael Avalos258c5902014-09-29 20:40:07 -0600380 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
381 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600382 acpi_status status;
383
Azael Avalos258c5902014-09-29 20:40:07 -0600384 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600385 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600386 pr_err("ACPI call to open SCI failed\n");
387 return 0;
388 }
389
Azael Avalos1864bbc2014-09-29 20:40:08 -0600390 if (out[0] == TOS_OPEN_CLOSE_OK) {
Azael Avalos84a62732014-03-25 20:38:29 -0600391 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600392 } else if (out[0] == TOS_ALREADY_OPEN) {
Azael Avalos84a62732014-03-25 20:38:29 -0600393 pr_info("Toshiba SCI already opened\n");
394 return 1;
Azael Avalosfa465732015-01-18 19:17:12 -0700395 } else if (out[0] == TOS_NOT_SUPPORTED) {
Darren Harte0769fe2015-02-11 20:50:08 -0800396 /*
397 * Some BIOSes do not have the SCI open/close functions
Azael Avalosfa465732015-01-18 19:17:12 -0700398 * implemented and return 0x8000 (Not Supported), failing to
399 * register some supported features.
400 *
401 * Simply return 1 if we hit those affected laptops to make the
402 * supported features work.
403 *
404 * In the case that some laptops really do not support the SCI,
405 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
406 * and thus, not registering support for the queried feature.
407 */
408 return 1;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600409 } else if (out[0] == TOS_NOT_PRESENT) {
Azael Avalos84a62732014-03-25 20:38:29 -0600410 pr_info("Toshiba SCI is not present\n");
411 }
412
413 return 0;
414}
415
416static void sci_close(struct toshiba_acpi_dev *dev)
417{
Azael Avalos258c5902014-09-29 20:40:07 -0600418 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
419 u32 out[TCI_WORDS];
Azael Avalos84a62732014-03-25 20:38:29 -0600420 acpi_status status;
421
Azael Avalos258c5902014-09-29 20:40:07 -0600422 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600423 if (ACPI_FAILURE(status)) {
Azael Avalos84a62732014-03-25 20:38:29 -0600424 pr_err("ACPI call to close SCI failed\n");
425 return;
426 }
427
Azael Avalos1864bbc2014-09-29 20:40:08 -0600428 if (out[0] == TOS_OPEN_CLOSE_OK)
Azael Avalos84a62732014-03-25 20:38:29 -0600429 return;
Azael Avalos1864bbc2014-09-29 20:40:08 -0600430 else if (out[0] == TOS_NOT_OPENED)
Azael Avalos84a62732014-03-25 20:38:29 -0600431 pr_info("Toshiba SCI not opened\n");
Azael Avalos1864bbc2014-09-29 20:40:08 -0600432 else if (out[0] == TOS_NOT_PRESENT)
Azael Avalos84a62732014-03-25 20:38:29 -0600433 pr_info("Toshiba SCI is not present\n");
434}
435
Azael Avalos893f3f62014-09-29 20:40:09 -0600436static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
Azael Avalos84a62732014-03-25 20:38:29 -0600437{
Azael Avalos258c5902014-09-29 20:40:07 -0600438 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
439 u32 out[TCI_WORDS];
440 acpi_status status = tci_raw(dev, in, out);
Azael Avalosb5163992015-02-10 23:43:56 -0700441
Azael Avalos893f3f62014-09-29 20:40:09 -0600442 if (ACPI_FAILURE(status))
443 return TOS_FAILURE;
444
Azael Avalos84a62732014-03-25 20:38:29 -0600445 *out1 = out[2];
Azael Avalos893f3f62014-09-29 20:40:09 -0600446
447 return out[0];
Azael Avalos84a62732014-03-25 20:38:29 -0600448}
449
Azael Avalos893f3f62014-09-29 20:40:09 -0600450static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
Azael Avalos84a62732014-03-25 20:38:29 -0600451{
Azael Avalos258c5902014-09-29 20:40:07 -0600452 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
453 u32 out[TCI_WORDS];
454 acpi_status status = tci_raw(dev, in, out);
Azael Avalos893f3f62014-09-29 20:40:09 -0600455
456 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
Azael Avalos84a62732014-03-25 20:38:29 -0600457}
458
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200459/* Illumination support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600460static void toshiba_illumination_available(struct toshiba_acpi_dev *dev)
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200461{
Azael Avalos258c5902014-09-29 20:40:07 -0600462 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
463 u32 out[TCI_WORDS];
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200464 acpi_status status;
465
Azael Avalosea215a3f2015-07-31 21:58:12 -0600466 dev->illumination_supported = 0;
467 dev->illumination_led_registered = false;
468
Azael Avalosfdb79082014-03-25 20:38:30 -0600469 if (!sci_open(dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -0600470 return;
Azael Avalosfdb79082014-03-25 20:38:30 -0600471
Azael Avalos258c5902014-09-29 20:40:07 -0600472 status = tci_raw(dev, in, out);
Azael Avalosfdb79082014-03-25 20:38:30 -0600473 sci_close(dev);
Azael Avalosea215a3f2015-07-31 21:58:12 -0600474 if (ACPI_FAILURE(status))
Azael Avalosfdb79082014-03-25 20:38:30 -0600475 pr_err("ACPI call to query Illumination support failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600476 else if (out[0] == TOS_SUCCESS)
477 dev->illumination_supported = 1;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200478}
479
480static void toshiba_illumination_set(struct led_classdev *cdev,
481 enum led_brightness brightness)
482{
Seth Forshee135740d2011-09-20 16:55:49 -0500483 struct toshiba_acpi_dev *dev = container_of(cdev,
484 struct toshiba_acpi_dev, led_dev);
Azael Avalose1a949c2015-07-31 21:58:14 -0600485 u32 result;
486 u32 state;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200487
488 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600489 if (!sci_open(dev))
490 return;
491
492 /* Switch the illumination on/off */
493 state = brightness ? 1 : 0;
Azael Avalos893f3f62014-09-29 20:40:09 -0600494 result = sci_write(dev, SCI_ILLUMINATION, state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600495 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600496 if (result == TOS_FAILURE)
Azael Avalosfdb79082014-03-25 20:38:30 -0600497 pr_err("ACPI call for illumination failed\n");
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200498}
499
500static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
501{
Seth Forshee135740d2011-09-20 16:55:49 -0500502 struct toshiba_acpi_dev *dev = container_of(cdev,
503 struct toshiba_acpi_dev, led_dev);
Azael Avalosfdb79082014-03-25 20:38:30 -0600504 u32 state, result;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200505
Azael Avalos3f75bbe2015-05-06 09:35:11 -0600506 /* First request : initialize communication. */
Azael Avalosfdb79082014-03-25 20:38:30 -0600507 if (!sci_open(dev))
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200508 return LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200509
510 /* Check the illumination */
Azael Avalos893f3f62014-09-29 20:40:09 -0600511 result = sci_read(dev, SCI_ILLUMINATION, &state);
Azael Avalosfdb79082014-03-25 20:38:30 -0600512 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600513 if (result == TOS_FAILURE) {
Azael Avalosfdb79082014-03-25 20:38:30 -0600514 pr_err("ACPI call for illumination failed\n");
515 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600516 } else if (result != TOS_SUCCESS) {
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200517 return LED_OFF;
518 }
519
Azael Avalosfdb79082014-03-25 20:38:30 -0600520 return state ? LED_FULL : LED_OFF;
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200521}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400522
Azael Avalos360f0f32014-03-25 20:38:31 -0600523/* KBD Illumination */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600524static void toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
Azael Avalos93f8c162014-09-12 18:50:36 -0600525{
Azael Avalos258c5902014-09-29 20:40:07 -0600526 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
527 u32 out[TCI_WORDS];
Azael Avalos93f8c162014-09-12 18:50:36 -0600528 acpi_status status;
529
Azael Avalosea215a3f2015-07-31 21:58:12 -0600530 dev->kbd_illum_supported = 0;
531 dev->kbd_led_registered = false;
Azael Avalos65e3cf92015-11-23 10:51:30 -0700532 dev->kbd_event_generated = false;
Azael Avalosea215a3f2015-07-31 21:58:12 -0600533
Azael Avalos93f8c162014-09-12 18:50:36 -0600534 if (!sci_open(dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -0600535 return;
Azael Avalos93f8c162014-09-12 18:50:36 -0600536
Azael Avalos258c5902014-09-29 20:40:07 -0600537 status = tci_raw(dev, in, out);
Azael Avalos93f8c162014-09-12 18:50:36 -0600538 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600539 if (ACPI_FAILURE(status)) {
Azael Avalos93f8c162014-09-12 18:50:36 -0600540 pr_err("ACPI call to query kbd illumination support failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600541 } else if (out[0] == TOS_SUCCESS) {
542 /*
543 * Check for keyboard backlight timeout max value,
544 * previous kbd backlight implementation set this to
545 * 0x3c0003, and now the new implementation set this
546 * to 0x3c001a, use this to distinguish between them.
547 */
548 if (out[3] == SCI_KBD_TIME_MAX)
549 dev->kbd_type = 2;
550 else
551 dev->kbd_type = 1;
552 /* Get the current keyboard backlight mode */
553 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
554 /* Get the current time (1-60 seconds) */
555 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
556 /* Flag as supported */
557 dev->kbd_illum_supported = 1;
Azael Avalos93f8c162014-09-12 18:50:36 -0600558 }
Azael Avalos93f8c162014-09-12 18:50:36 -0600559}
560
Azael Avalos360f0f32014-03-25 20:38:31 -0600561static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
562{
563 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600564
565 if (!sci_open(dev))
566 return -EIO;
567
Azael Avalos893f3f62014-09-29 20:40:09 -0600568 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600569 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600570 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600571 pr_err("ACPI call to set KBD backlight status failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600572 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos360f0f32014-03-25 20:38:31 -0600573 return -ENODEV;
Azael Avalos360f0f32014-03-25 20:38:31 -0600574
Azael Avalose1a949c2015-07-31 21:58:14 -0600575 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos360f0f32014-03-25 20:38:31 -0600576}
577
578static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
579{
580 u32 result;
Azael Avalos360f0f32014-03-25 20:38:31 -0600581
582 if (!sci_open(dev))
583 return -EIO;
584
Azael Avalos893f3f62014-09-29 20:40:09 -0600585 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
Azael Avalos360f0f32014-03-25 20:38:31 -0600586 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600587 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600588 pr_err("ACPI call to get KBD backlight status failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600589 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos360f0f32014-03-25 20:38:31 -0600590 return -ENODEV;
Azael Avalos360f0f32014-03-25 20:38:31 -0600591
Azael Avalose1a949c2015-07-31 21:58:14 -0600592 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos360f0f32014-03-25 20:38:31 -0600593}
594
595static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
596{
597 struct toshiba_acpi_dev *dev = container_of(cdev,
598 struct toshiba_acpi_dev, kbd_led);
Azael Avalose1a949c2015-07-31 21:58:14 -0600599 u32 result;
600 u32 state;
Azael Avalos360f0f32014-03-25 20:38:31 -0600601
602 /* Check the keyboard backlight state */
Azael Avalosd37782b2015-05-06 09:35:10 -0600603 result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
Azael Avalosa6b53542015-07-31 21:58:15 -0600604 if (result == TOS_FAILURE) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600605 pr_err("ACPI call to get the keyboard backlight failed\n");
606 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600607 } else if (result != TOS_SUCCESS) {
Azael Avalos360f0f32014-03-25 20:38:31 -0600608 return LED_OFF;
609 }
610
611 return state ? LED_FULL : LED_OFF;
612}
613
614static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
615 enum led_brightness brightness)
616{
617 struct toshiba_acpi_dev *dev = container_of(cdev,
618 struct toshiba_acpi_dev, kbd_led);
Azael Avalose1a949c2015-07-31 21:58:14 -0600619 u32 result;
620 u32 state;
Azael Avalos360f0f32014-03-25 20:38:31 -0600621
622 /* Set the keyboard backlight state */
623 state = brightness ? 1 : 0;
Azael Avalosd37782b2015-05-06 09:35:10 -0600624 result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
Azael Avalosa6b53542015-07-31 21:58:15 -0600625 if (result == TOS_FAILURE)
Azael Avalos360f0f32014-03-25 20:38:31 -0600626 pr_err("ACPI call to set KBD Illumination mode failed\n");
Azael Avalos360f0f32014-03-25 20:38:31 -0600627}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400628
Azael Avalos9d8658a2014-03-25 20:38:32 -0600629/* TouchPad support */
630static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
631{
632 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600633
634 if (!sci_open(dev))
635 return -EIO;
636
Azael Avalos893f3f62014-09-29 20:40:09 -0600637 result = sci_write(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600638 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600639 if (result == TOS_FAILURE)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600640 pr_err("ACPI call to set the touchpad failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600641 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600642 return -ENODEV;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600643
Azael Avalose1a949c2015-07-31 21:58:14 -0600644 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600645}
646
647static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
648{
649 u32 result;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600650
651 if (!sci_open(dev))
652 return -EIO;
653
Azael Avalos893f3f62014-09-29 20:40:09 -0600654 result = sci_read(dev, SCI_TOUCHPAD, state);
Azael Avalos9d8658a2014-03-25 20:38:32 -0600655 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600656 if (result == TOS_FAILURE)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600657 pr_err("ACPI call to query the touchpad failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600658 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos9d8658a2014-03-25 20:38:32 -0600659 return -ENODEV;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600660
Azael Avalose1a949c2015-07-31 21:58:14 -0600661 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos9d8658a2014-03-25 20:38:32 -0600662}
Pierre Ducroquet6c3f6e62010-07-29 11:56:59 +0200663
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600664/* Eco Mode support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600665static void toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600666{
667 acpi_status status;
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700668 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
Azael Avalos258c5902014-09-29 20:40:07 -0600669 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600670
Azael Avalosea215a3f2015-07-31 21:58:12 -0600671 dev->eco_supported = 0;
672 dev->eco_led_registered = false;
673
Azael Avalos258c5902014-09-29 20:40:07 -0600674 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600675 if (ACPI_FAILURE(status)) {
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700676 pr_err("ACPI call to get ECO led failed\n");
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700677 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
Darren Harte0769fe2015-02-11 20:50:08 -0800678 /*
679 * If we receive 0x8300 (Input Data Error), it means that the
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700680 * LED device is present, but that we just screwed the input
681 * parameters.
682 *
683 * Let's query the status of the LED to see if we really have a
684 * success response, indicating the actual presense of the LED,
685 * bail out otherwise.
686 */
687 in[3] = 1;
688 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600689 if (ACPI_FAILURE(status))
Azael Avalos98fc4ec2015-02-09 20:55:02 -0700690 pr_err("ACPI call to get ECO led failed\n");
691 else if (out[0] == TOS_SUCCESS)
Azael Avalosea215a3f2015-07-31 21:58:12 -0600692 dev->eco_supported = 1;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600693 }
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600694}
695
Azael Avalosb5163992015-02-10 23:43:56 -0700696static enum led_brightness
697toshiba_eco_mode_get_status(struct led_classdev *cdev)
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600698{
699 struct toshiba_acpi_dev *dev = container_of(cdev,
700 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600701 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
702 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600703 acpi_status status;
704
Azael Avalos258c5902014-09-29 20:40:07 -0600705 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600706 if (ACPI_FAILURE(status)) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600707 pr_err("ACPI call to get ECO led failed\n");
708 return LED_OFF;
Azael Avalose1a949c2015-07-31 21:58:14 -0600709 } else if (out[0] != TOS_SUCCESS) {
710 return LED_OFF;
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600711 }
712
713 return out[2] ? LED_FULL : LED_OFF;
714}
715
716static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
717 enum led_brightness brightness)
718{
719 struct toshiba_acpi_dev *dev = container_of(cdev,
720 struct toshiba_acpi_dev, eco_led);
Azael Avalos258c5902014-09-29 20:40:07 -0600721 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
722 u32 out[TCI_WORDS];
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600723 acpi_status status;
724
725 /* Switch the Eco Mode led on/off */
726 in[2] = (brightness) ? 1 : 0;
Azael Avalos258c5902014-09-29 20:40:07 -0600727 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600728 if (ACPI_FAILURE(status))
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600729 pr_err("ACPI call to set ECO led failed\n");
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600730}
Matthew Garrettea6b31f2014-04-04 14:22:34 -0400731
Azael Avalos5a2813e2014-03-25 20:38:34 -0600732/* Accelerometer support */
Azael Avalosea215a3f2015-07-31 21:58:12 -0600733static void toshiba_accelerometer_available(struct toshiba_acpi_dev *dev)
Azael Avalos5a2813e2014-03-25 20:38:34 -0600734{
Azael Avalos258c5902014-09-29 20:40:07 -0600735 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
736 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600737 acpi_status status;
738
Azael Avalosea215a3f2015-07-31 21:58:12 -0600739 dev->accelerometer_supported = 0;
740
Darren Harte0769fe2015-02-11 20:50:08 -0800741 /*
742 * Check if the accelerometer call exists,
Azael Avalos5a2813e2014-03-25 20:38:34 -0600743 * this call also serves as initialization
744 */
Azael Avalos258c5902014-09-29 20:40:07 -0600745 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600746 if (ACPI_FAILURE(status))
Azael Avalos5a2813e2014-03-25 20:38:34 -0600747 pr_err("ACPI call to query the accelerometer failed\n");
Azael Avalosea215a3f2015-07-31 21:58:12 -0600748 else if (out[0] == TOS_SUCCESS)
749 dev->accelerometer_supported = 1;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600750}
751
752static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
Azael Avalosa6b53542015-07-31 21:58:15 -0600753 u32 *xy, u32 *z)
Azael Avalos5a2813e2014-03-25 20:38:34 -0600754{
Azael Avalos258c5902014-09-29 20:40:07 -0600755 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
756 u32 out[TCI_WORDS];
Azael Avalos5a2813e2014-03-25 20:38:34 -0600757 acpi_status status;
758
759 /* Check the Accelerometer status */
Azael Avalos258c5902014-09-29 20:40:07 -0600760 status = tci_raw(dev, in, out);
Azael Avalosa6b53542015-07-31 21:58:15 -0600761 if (ACPI_FAILURE(status)) {
Azael Avalos5a2813e2014-03-25 20:38:34 -0600762 pr_err("ACPI call to query the accelerometer failed\n");
763 return -EIO;
Azael Avalose1a949c2015-07-31 21:58:14 -0600764 } else if (out[0] == TOS_NOT_SUPPORTED) {
765 return -ENODEV;
766 } else if (out[0] == TOS_SUCCESS) {
767 *xy = out[2];
768 *z = out[4];
769 return 0;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600770 }
771
Azael Avalose1a949c2015-07-31 21:58:14 -0600772 return -EIO;
Azael Avalos5a2813e2014-03-25 20:38:34 -0600773}
Azael Avalosdef6c4e2014-03-25 20:38:33 -0600774
Azael Avalose26ffe52015-01-18 18:30:22 -0700775/* Sleep (Charge and Music) utilities support */
Azael Avalosc8c91842015-04-02 19:26:20 -0600776static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
777{
778 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
779 u32 out[TCI_WORDS];
780 acpi_status status;
781
Azael Avalosc8c91842015-04-02 19:26:20 -0600782 dev->usb_sleep_charge_supported = 0;
783
784 if (!sci_open(dev))
785 return;
786
787 status = tci_raw(dev, in, out);
Azael Avalos8baec452015-05-06 09:35:12 -0600788 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600789 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
790 sci_close(dev);
791 return;
792 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600793 sci_close(dev);
794 return;
795 } else if (out[0] == TOS_SUCCESS) {
796 dev->usbsc_mode_base = out[4];
797 }
798
799 in[5] = SCI_USB_CHARGE_BAT_LVL;
800 status = tci_raw(dev, in, out);
Azael Avalosea215a3f2015-07-31 21:58:12 -0600801 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600802 if (ACPI_FAILURE(status)) {
Azael Avalosc8c91842015-04-02 19:26:20 -0600803 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
Azael Avalosc8c91842015-04-02 19:26:20 -0600804 } else if (out[0] == TOS_SUCCESS) {
805 dev->usbsc_bat_level = out[2];
Azael Avalosea215a3f2015-07-31 21:58:12 -0600806 /* Flag as supported */
Azael Avalosc8c91842015-04-02 19:26:20 -0600807 dev->usb_sleep_charge_supported = 1;
808 }
809
Azael Avalosc8c91842015-04-02 19:26:20 -0600810}
811
Azael Avalose26ffe52015-01-18 18:30:22 -0700812static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
813 u32 *mode)
814{
815 u32 result;
816
817 if (!sci_open(dev))
818 return -EIO;
819
820 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
821 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600822 if (result == TOS_FAILURE)
Azael Avalose26ffe52015-01-18 18:30:22 -0700823 pr_err("ACPI call to set USB S&C mode failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600824 else if (result == TOS_NOT_SUPPORTED)
Azael Avalose26ffe52015-01-18 18:30:22 -0700825 return -ENODEV;
Azael Avalose26ffe52015-01-18 18:30:22 -0700826
Azael Avalose1a949c2015-07-31 21:58:14 -0600827 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalose26ffe52015-01-18 18:30:22 -0700828}
829
830static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
831 u32 mode)
832{
833 u32 result;
834
835 if (!sci_open(dev))
836 return -EIO;
837
838 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
839 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600840 if (result == TOS_FAILURE)
Azael Avalose26ffe52015-01-18 18:30:22 -0700841 pr_err("ACPI call to set USB S&C mode failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600842 else if (result == TOS_NOT_SUPPORTED)
Azael Avalose26ffe52015-01-18 18:30:22 -0700843 return -ENODEV;
Azael Avalose26ffe52015-01-18 18:30:22 -0700844
Azael Avalose1a949c2015-07-31 21:58:14 -0600845 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalose26ffe52015-01-18 18:30:22 -0700846}
847
Azael Avalos182bcaa2015-01-18 18:30:23 -0700848static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
849 u32 *mode)
850{
851 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
852 u32 out[TCI_WORDS];
853 acpi_status status;
854
855 if (!sci_open(dev))
856 return -EIO;
857
858 in[5] = SCI_USB_CHARGE_BAT_LVL;
859 status = tci_raw(dev, in, out);
860 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600861 if (ACPI_FAILURE(status)) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700862 pr_err("ACPI call to get USB S&C battery level failed\n");
Azael Avalos182bcaa2015-01-18 18:30:23 -0700863 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos182bcaa2015-01-18 18:30:23 -0700864 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -0600865 } else if (out[0] == TOS_SUCCESS) {
866 *mode = out[2];
867 return 0;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700868 }
869
Azael Avalose1a949c2015-07-31 21:58:14 -0600870 return -EIO;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700871}
872
873static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
874 u32 mode)
875{
876 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
877 u32 out[TCI_WORDS];
878 acpi_status status;
879
880 if (!sci_open(dev))
881 return -EIO;
882
883 in[2] = mode;
884 in[5] = SCI_USB_CHARGE_BAT_LVL;
885 status = tci_raw(dev, in, out);
886 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600887 if (ACPI_FAILURE(status))
Azael Avalos182bcaa2015-01-18 18:30:23 -0700888 pr_err("ACPI call to set USB S&C battery level failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600889 else if (out[0] == TOS_NOT_SUPPORTED)
Azael Avalos182bcaa2015-01-18 18:30:23 -0700890 return -ENODEV;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700891
Azael Avalose1a949c2015-07-31 21:58:14 -0600892 return out[0] == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos182bcaa2015-01-18 18:30:23 -0700893}
894
Azael Avalosbb3fe012015-01-18 18:30:24 -0700895static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
896 u32 *state)
897{
898 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
899 u32 out[TCI_WORDS];
900 acpi_status status;
901
902 if (!sci_open(dev))
903 return -EIO;
904
905 in[5] = SCI_USB_CHARGE_RAPID_DSP;
906 status = tci_raw(dev, in, out);
907 sci_close(dev);
Azael Avalos8baec452015-05-06 09:35:12 -0600908 if (ACPI_FAILURE(status)) {
Azael Avalosbb26f182015-04-02 19:26:21 -0600909 pr_err("ACPI call to get USB Rapid Charge failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600910 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalosbb3fe012015-01-18 18:30:24 -0700911 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -0600912 } else if (out[0] == TOS_SUCCESS || out[0] == TOS_SUCCESS2) {
913 *state = out[2];
914 return 0;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700915 }
916
Azael Avalose1a949c2015-07-31 21:58:14 -0600917 return -EIO;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700918}
919
920static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
921 u32 state)
922{
923 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
924 u32 out[TCI_WORDS];
925 acpi_status status;
926
927 if (!sci_open(dev))
928 return -EIO;
929
930 in[2] = state;
931 in[5] = SCI_USB_CHARGE_RAPID_DSP;
932 status = tci_raw(dev, in, out);
933 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600934 if (ACPI_FAILURE(status))
Azael Avalosbb26f182015-04-02 19:26:21 -0600935 pr_err("ACPI call to set USB Rapid Charge failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600936 else if (out[0] == TOS_NOT_SUPPORTED)
Azael Avalosbb3fe012015-01-18 18:30:24 -0700937 return -ENODEV;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700938
Azael Avalose1a949c2015-07-31 21:58:14 -0600939 return (out[0] == TOS_SUCCESS || out[0] == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbb3fe012015-01-18 18:30:24 -0700940}
941
Azael Avalos172ce0a2015-01-18 18:30:25 -0700942static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
943{
944 u32 result;
945
946 if (!sci_open(dev))
947 return -EIO;
948
949 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
950 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600951 if (result == TOS_FAILURE)
Azael Avalosbb26f182015-04-02 19:26:21 -0600952 pr_err("ACPI call to get Sleep and Music failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600953 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos172ce0a2015-01-18 18:30:25 -0700954 return -ENODEV;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700955
Azael Avaloscf680ea2015-09-09 11:25:44 -0600956 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700957}
958
959static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
960{
961 u32 result;
962
963 if (!sci_open(dev))
964 return -EIO;
965
966 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
967 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600968 if (result == TOS_FAILURE)
Azael Avalosbb26f182015-04-02 19:26:21 -0600969 pr_err("ACPI call to set Sleep and Music failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600970 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos172ce0a2015-01-18 18:30:25 -0700971 return -ENODEV;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700972
Azael Avalose1a949c2015-07-31 21:58:14 -0600973 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos172ce0a2015-01-18 18:30:25 -0700974}
975
Azael Avalosbae84192015-02-10 21:09:18 -0700976/* Keyboard function keys */
977static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
978{
979 u32 result;
980
981 if (!sci_open(dev))
982 return -EIO;
983
984 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
985 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -0600986 if (result == TOS_FAILURE)
Azael Avalosbae84192015-02-10 21:09:18 -0700987 pr_err("ACPI call to get KBD function keys failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -0600988 else if (result == TOS_NOT_SUPPORTED)
Azael Avalosbae84192015-02-10 21:09:18 -0700989 return -ENODEV;
Azael Avalosbae84192015-02-10 21:09:18 -0700990
Azael Avalose1a949c2015-07-31 21:58:14 -0600991 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbae84192015-02-10 21:09:18 -0700992}
993
994static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
995{
996 u32 result;
997
998 if (!sci_open(dev))
999 return -EIO;
1000
1001 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1002 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001003 if (result == TOS_FAILURE)
Azael Avalosbae84192015-02-10 21:09:18 -07001004 pr_err("ACPI call to set KBD function keys failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001005 else if (result == TOS_NOT_SUPPORTED)
Azael Avalosbae84192015-02-10 21:09:18 -07001006 return -ENODEV;
Azael Avalosbae84192015-02-10 21:09:18 -07001007
Azael Avalose1a949c2015-07-31 21:58:14 -06001008 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalosbae84192015-02-10 21:09:18 -07001009}
1010
Azael Avalos35d53ce2015-02-10 21:09:19 -07001011/* Panel Power ON */
1012static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1013{
1014 u32 result;
1015
1016 if (!sci_open(dev))
1017 return -EIO;
1018
1019 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1020 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001021 if (result == TOS_FAILURE)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001022 pr_err("ACPI call to get Panel Power ON failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001023 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001024 return -ENODEV;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001025
Azael Avalose1a949c2015-07-31 21:58:14 -06001026 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001027}
1028
1029static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1030{
1031 u32 result;
1032
1033 if (!sci_open(dev))
1034 return -EIO;
1035
1036 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1037 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001038 if (result == TOS_FAILURE)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001039 pr_err("ACPI call to set Panel Power ON failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001040 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos35d53ce2015-02-10 21:09:19 -07001041 return -ENODEV;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001042
Azael Avalose1a949c2015-07-31 21:58:14 -06001043 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos35d53ce2015-02-10 21:09:19 -07001044}
1045
Azael Avalos17fe4b32015-02-10 21:09:20 -07001046/* USB Three */
1047static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1048{
1049 u32 result;
1050
1051 if (!sci_open(dev))
1052 return -EIO;
1053
1054 result = sci_read(dev, SCI_USB_THREE, state);
1055 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001056 if (result == TOS_FAILURE)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001057 pr_err("ACPI call to get USB 3 failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001058 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001059 return -ENODEV;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001060
Azael Avalose1a949c2015-07-31 21:58:14 -06001061 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001062}
1063
1064static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1065{
1066 u32 result;
1067
1068 if (!sci_open(dev))
1069 return -EIO;
1070
1071 result = sci_write(dev, SCI_USB_THREE, state);
1072 sci_close(dev);
Azael Avalosa6b53542015-07-31 21:58:15 -06001073 if (result == TOS_FAILURE)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001074 pr_err("ACPI call to set USB 3 failed\n");
Azael Avalosa6b53542015-07-31 21:58:15 -06001075 else if (result == TOS_NOT_SUPPORTED)
Azael Avalos17fe4b32015-02-10 21:09:20 -07001076 return -ENODEV;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001077
Azael Avalose1a949c2015-07-31 21:58:14 -06001078 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
Azael Avalos17fe4b32015-02-10 21:09:20 -07001079}
1080
Azael Avalos56e6b352015-03-20 16:55:16 -06001081/* Hotkey Event type */
1082static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1083 u32 *type)
1084{
Azael Avalos3b876002015-05-06 09:35:09 -06001085 u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
1086 u32 out[TCI_WORDS];
1087 acpi_status status;
Azael Avalos56e6b352015-03-20 16:55:16 -06001088
Azael Avalos3b876002015-05-06 09:35:09 -06001089 status = tci_raw(dev, in, out);
1090 if (ACPI_FAILURE(status)) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001091 pr_err("ACPI call to get System type failed\n");
Azael Avalos3b876002015-05-06 09:35:09 -06001092 } else if (out[0] == TOS_NOT_SUPPORTED) {
Azael Avalos56e6b352015-03-20 16:55:16 -06001093 return -ENODEV;
Azael Avalose1a949c2015-07-31 21:58:14 -06001094 } else if (out[0] == TOS_SUCCESS) {
1095 *type = out[3];
1096 return 0;
Azael Avalos56e6b352015-03-20 16:55:16 -06001097 }
1098
Azael Avalose1a949c2015-07-31 21:58:14 -06001099 return -EIO;
Azael Avalos56e6b352015-03-20 16:55:16 -06001100}
1101
Azael Avalos6873f462015-11-23 10:49:10 -07001102/* Wireless status (RFKill, WLAN, BT, WWAN) */
1103static int toshiba_wireless_status(struct toshiba_acpi_dev *dev)
1104{
1105 u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
1106 u32 out[TCI_WORDS];
1107 acpi_status status;
1108
1109 in[3] = HCI_WIRELESS_STATUS;
1110 status = tci_raw(dev, in, out);
1111
1112 if (ACPI_FAILURE(status)) {
1113 pr_err("ACPI call to get Wireless status failed\n");
1114 return -EIO;
1115 }
1116
1117 if (out[0] == TOS_NOT_SUPPORTED)
1118 return -ENODEV;
1119
1120 if (out[0] != TOS_SUCCESS)
1121 return -EIO;
1122
1123 dev->killswitch = !!(out[2] & HCI_WIRELESS_STATUS);
1124
1125 return 0;
1126}
1127
1128/* WWAN */
1129static void toshiba_wwan_available(struct toshiba_acpi_dev *dev)
1130{
1131 u32 in[TCI_WORDS] = { HCI_GET, HCI_WIRELESS, 0, 0, 0, 0 };
1132 u32 out[TCI_WORDS];
1133 acpi_status status;
1134
1135 dev->wwan_supported = 0;
1136
1137 /*
1138 * WWAN support can be queried by setting the in[3] value to
1139 * HCI_WIRELESS_WWAN (0x03).
1140 *
1141 * If supported, out[0] contains TOS_SUCCESS and out[2] contains
1142 * HCI_WIRELESS_WWAN_STATUS (0x2000).
1143 *
1144 * If not supported, out[0] contains TOS_INPUT_DATA_ERROR (0x8300)
1145 * or TOS_NOT_SUPPORTED (0x8000).
1146 */
1147 in[3] = HCI_WIRELESS_WWAN;
1148 status = tci_raw(dev, in, out);
1149
1150 if (ACPI_FAILURE(status)) {
1151 pr_err("ACPI call to get WWAN status failed\n");
1152 return;
1153 }
1154
1155 if (out[0] != TOS_SUCCESS)
1156 return;
1157
1158 dev->wwan_supported = (out[2] == HCI_WIRELESS_WWAN_STATUS);
1159}
1160
1161static int toshiba_wwan_set(struct toshiba_acpi_dev *dev, u32 state)
1162{
1163 u32 in[TCI_WORDS] = { HCI_SET, HCI_WIRELESS, state, 0, 0, 0 };
1164 u32 out[TCI_WORDS];
1165 acpi_status status;
1166
1167 in[3] = HCI_WIRELESS_WWAN_STATUS;
1168 status = tci_raw(dev, in, out);
1169
1170 if (ACPI_FAILURE(status)) {
1171 pr_err("ACPI call to set WWAN status failed\n");
1172 return -EIO;
1173 }
1174
1175 if (out[0] == TOS_NOT_SUPPORTED)
1176 return -ENODEV;
1177
1178 if (out[0] != TOS_SUCCESS)
1179 return -EIO;
1180
1181 /*
1182 * Some devices only need to call HCI_WIRELESS_WWAN_STATUS to
1183 * (de)activate the device, but some others need the
1184 * HCI_WIRELESS_WWAN_POWER call as well.
1185 */
1186 in[3] = HCI_WIRELESS_WWAN_POWER;
1187 status = tci_raw(dev, in, out);
1188
1189 if (ACPI_FAILURE(status)) {
1190 pr_err("ACPI call to set WWAN power failed\n");
1191 return -EIO;
1192 }
1193
1194 if (out[0] == TOS_NOT_SUPPORTED)
1195 return -ENODEV;
1196
1197 return out[0] == TOS_SUCCESS ? 0 : -EIO;
1198}
1199
Azael Avalos763ff322016-01-25 11:29:10 -07001200/* Cooling Method */
1201static void toshiba_cooling_method_available(struct toshiba_acpi_dev *dev)
1202{
1203 u32 in[TCI_WORDS] = { HCI_GET, HCI_COOLING_METHOD, 0, 0, 0, 0 };
1204 u32 out[TCI_WORDS];
1205 acpi_status status;
1206
1207 dev->cooling_method_supported = 0;
1208 dev->max_cooling_method = 0;
1209
1210 status = tci_raw(dev, in, out);
1211 if (ACPI_FAILURE(status))
1212 pr_err("ACPI call to get Cooling Method failed\n");
1213
1214 if (out[0] != TOS_SUCCESS && out[0] != TOS_SUCCESS2)
1215 return;
1216
1217 dev->cooling_method_supported = 1;
1218 dev->max_cooling_method = out[3];
1219}
1220
1221static int toshiba_cooling_method_get(struct toshiba_acpi_dev *dev, u32 *state)
1222{
1223 u32 result = hci_read(dev, HCI_COOLING_METHOD, state);
1224
1225 if (result == TOS_FAILURE)
1226 pr_err("ACPI call to get Cooling Method failed\n");
1227
1228 if (result == TOS_NOT_SUPPORTED)
1229 return -ENODEV;
1230
1231 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
1232}
1233
1234static int toshiba_cooling_method_set(struct toshiba_acpi_dev *dev, u32 state)
1235{
1236 u32 result = hci_write(dev, HCI_COOLING_METHOD, state);
1237
1238 if (result == TOS_FAILURE)
1239 pr_err("ACPI call to get Cooling Method failed\n");
1240
1241 if (result == TOS_NOT_SUPPORTED)
1242 return -ENODEV;
1243
1244 return (result == TOS_SUCCESS || result == TOS_SUCCESS2) ? 0 : -EIO;
1245}
1246
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001247/* Transflective Backlight */
Azael Avalos695f6062015-07-22 18:09:13 -06001248static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 *status)
Akio Idehara121b7b02012-04-06 01:46:43 +09001249{
Azael Avalose1a949c2015-07-31 21:58:14 -06001250 u32 result = hci_read(dev, HCI_TR_BACKLIGHT, status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001251
Azael Avalose1a949c2015-07-31 21:58:14 -06001252 if (result == TOS_FAILURE)
1253 pr_err("ACPI call to get Transflective Backlight failed\n");
1254 else if (result == TOS_NOT_SUPPORTED)
1255 return -ENODEV;
1256
1257 return result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001258}
1259
Azael Avalos695f6062015-07-22 18:09:13 -06001260static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 status)
Akio Idehara121b7b02012-04-06 01:46:43 +09001261{
Azael Avalose1a949c2015-07-31 21:58:14 -06001262 u32 result = hci_write(dev, HCI_TR_BACKLIGHT, !status);
Akio Idehara121b7b02012-04-06 01:46:43 +09001263
Azael Avalose1a949c2015-07-31 21:58:14 -06001264 if (result == TOS_FAILURE)
1265 pr_err("ACPI call to set Transflective Backlight failed\n");
1266 else if (result == TOS_NOT_SUPPORTED)
1267 return -ENODEV;
1268
1269 return result == TOS_SUCCESS ? 0 : -EIO;
Akio Idehara121b7b02012-04-06 01:46:43 +09001270}
1271
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001272static struct proc_dir_entry *toshiba_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001274/* LCD Brightness */
Seth Forshee62cce752012-04-19 11:23:50 -05001275static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Azael Avalose1a949c2015-07-31 21:58:14 -06001277 u32 result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 u32 value;
Akio Idehara121b7b02012-04-06 01:46:43 +09001279 int brightness = 0;
1280
1281 if (dev->tr_backlight_supported) {
Azael Avalos695f6062015-07-22 18:09:13 -06001282 int ret = get_tr_backlight_status(dev, &value);
Azael Avalosb5163992015-02-10 23:43:56 -07001283
Akio Idehara121b7b02012-04-06 01:46:43 +09001284 if (ret)
1285 return ret;
Azael Avalos695f6062015-07-22 18:09:13 -06001286 if (value)
Akio Idehara121b7b02012-04-06 01:46:43 +09001287 return 0;
1288 brightness++;
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Azael Avalose1a949c2015-07-31 21:58:14 -06001291 result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
1292 if (result == TOS_FAILURE)
1293 pr_err("ACPI call to get LCD Brightness failed\n");
1294 else if (result == TOS_NOT_SUPPORTED)
1295 return -ENODEV;
1296 if (result == TOS_SUCCESS)
Akio Idehara121b7b02012-04-06 01:46:43 +09001297 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001298
1299 return -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001300}
1301
Seth Forshee62cce752012-04-19 11:23:50 -05001302static int get_lcd_brightness(struct backlight_device *bd)
1303{
1304 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001305
Seth Forshee62cce752012-04-19 11:23:50 -05001306 return __get_lcd_brightness(dev);
1307}
1308
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001309static int lcd_proc_show(struct seq_file *m, void *v)
Holger Machtc9263552006-10-20 14:30:29 -07001310{
Seth Forshee135740d2011-09-20 16:55:49 -05001311 struct toshiba_acpi_dev *dev = m->private;
Akio Idehara121b7b02012-04-06 01:46:43 +09001312 int levels;
Azael Avalose1a949c2015-07-31 21:58:14 -06001313 int value;
Holger Machtc9263552006-10-20 14:30:29 -07001314
Seth Forshee135740d2011-09-20 16:55:49 -05001315 if (!dev->backlight_dev)
1316 return -ENODEV;
1317
Akio Idehara121b7b02012-04-06 01:46:43 +09001318 levels = dev->backlight_dev->props.max_brightness + 1;
Seth Forshee62cce752012-04-19 11:23:50 -05001319 value = get_lcd_brightness(dev->backlight_dev);
Holger Machtc9263552006-10-20 14:30:29 -07001320 if (value >= 0) {
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001321 seq_printf(m, "brightness: %d\n", value);
Akio Idehara121b7b02012-04-06 01:46:43 +09001322 seq_printf(m, "brightness_levels: %d\n", levels);
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001326 pr_err("Error reading LCD brightness\n");
Azael Avalose1a949c2015-07-31 21:58:14 -06001327
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001328 return -EIO;
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001329}
1330
1331static int lcd_proc_open(struct inode *inode, struct file *file)
1332{
Al Virod9dda782013-03-31 18:16:14 -04001333 return single_open(file, lcd_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Seth Forshee62cce752012-04-19 11:23:50 -05001336static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
Holger Machtc9263552006-10-20 14:30:29 -07001337{
Azael Avalose1a949c2015-07-31 21:58:14 -06001338 u32 result;
Holger Machtc9263552006-10-20 14:30:29 -07001339
Akio Idehara121b7b02012-04-06 01:46:43 +09001340 if (dev->tr_backlight_supported) {
Azael Avalos695f6062015-07-22 18:09:13 -06001341 int ret = set_tr_backlight_status(dev, !value);
Azael Avalosb5163992015-02-10 23:43:56 -07001342
Akio Idehara121b7b02012-04-06 01:46:43 +09001343 if (ret)
1344 return ret;
1345 if (value)
1346 value--;
1347 }
1348
Azael Avalosa39f46d2014-11-24 19:29:36 -07001349 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
Azael Avalose1a949c2015-07-31 21:58:14 -06001350 result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
1351 if (result == TOS_FAILURE)
1352 pr_err("ACPI call to set LCD Brightness failed\n");
1353 else if (result == TOS_NOT_SUPPORTED)
1354 return -ENODEV;
1355
1356 return result == TOS_SUCCESS ? 0 : -EIO;
Holger Machtc9263552006-10-20 14:30:29 -07001357}
1358
1359static int set_lcd_status(struct backlight_device *bd)
1360{
Seth Forshee135740d2011-09-20 16:55:49 -05001361 struct toshiba_acpi_dev *dev = bl_get_data(bd);
Azael Avalosb5163992015-02-10 23:43:56 -07001362
Seth Forshee62cce752012-04-19 11:23:50 -05001363 return set_lcd_brightness(dev, bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -07001364}
1365
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001366static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1367 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Al Virod9dda782013-03-31 18:16:14 -04001369 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001370 char cmd[42];
1371 size_t len;
Akio Idehara121b7b02012-04-06 01:46:43 +09001372 int levels = dev->backlight_dev->props.max_brightness + 1;
Azael Avalose1a949c2015-07-31 21:58:14 -06001373 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001375 len = min(count, sizeof(cmd) - 1);
1376 if (copy_from_user(cmd, buf, len))
1377 return -EFAULT;
1378 cmd[len] = '\0';
1379
Azael Avalose1a949c2015-07-31 21:58:14 -06001380 if (sscanf(cmd, " brightness : %i", &value) != 1 &&
1381 value < 0 && value > levels)
1382 return -EINVAL;
1383
1384 if (set_lcd_brightness(dev, value))
1385 return -EIO;
1386
1387 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001390static const struct file_operations lcd_proc_fops = {
1391 .owner = THIS_MODULE,
1392 .open = lcd_proc_open,
1393 .read = seq_read,
1394 .llseek = seq_lseek,
1395 .release = single_release,
1396 .write = lcd_proc_write,
1397};
1398
Azael Avalose1a949c2015-07-31 21:58:14 -06001399/* Video-Out */
Seth Forshee36d03f92011-09-20 16:55:53 -05001400static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1401{
Azael Avalose1a949c2015-07-31 21:58:14 -06001402 u32 result = hci_read(dev, HCI_VIDEO_OUT, status);
Seth Forshee36d03f92011-09-20 16:55:53 -05001403
Azael Avalose1a949c2015-07-31 21:58:14 -06001404 if (result == TOS_FAILURE)
1405 pr_err("ACPI call to get Video-Out failed\n");
1406 else if (result == TOS_NOT_SUPPORTED)
1407 return -ENODEV;
1408
1409 return result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001410}
1411
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001412static int video_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Seth Forshee135740d2011-09-20 16:55:49 -05001414 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 u32 value;
1416
Azael Avalose1a949c2015-07-31 21:58:14 -06001417 if (!get_video_status(dev, &value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1419 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001420 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001421
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001422 seq_printf(m, "lcd_out: %d\n", is_lcd);
1423 seq_printf(m, "crt_out: %d\n", is_crt);
1424 seq_printf(m, "tv_out: %d\n", is_tv);
Azael Avalose1a949c2015-07-31 21:58:14 -06001425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Azael Avalose1a949c2015-07-31 21:58:14 -06001428 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001431static int video_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Al Virod9dda782013-03-31 18:16:14 -04001433 return single_open(file, video_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001434}
1435
1436static ssize_t video_proc_write(struct file *file, const char __user *buf,
1437 size_t count, loff_t *pos)
1438{
Al Virod9dda782013-03-31 18:16:14 -04001439 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Azael Avalose1a949c2015-07-31 21:58:14 -06001440 char *buffer;
1441 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int remain = count;
1443 int lcd_out = -1;
1444 int crt_out = -1;
1445 int tv_out = -1;
Azael Avalose1a949c2015-07-31 21:58:14 -06001446 int value;
1447 int ret;
Al Virob4482a42007-10-14 19:35:40 +01001448 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001450 cmd = kmalloc(count + 1, GFP_KERNEL);
1451 if (!cmd)
1452 return -ENOMEM;
1453 if (copy_from_user(cmd, buf, count)) {
1454 kfree(cmd);
1455 return -EFAULT;
1456 }
1457 cmd[count] = '\0';
1458
1459 buffer = cmd;
1460
Darren Harte0769fe2015-02-11 20:50:08 -08001461 /*
1462 * Scan expression. Multiple expressions may be delimited with ;
1463 * NOTE: To keep scanning simple, invalid fields are ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
1465 while (remain) {
1466 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1467 lcd_out = value & 1;
1468 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1469 crt_out = value & 1;
1470 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1471 tv_out = value & 1;
Darren Harte0769fe2015-02-11 20:50:08 -08001472 /* Advance to one character past the next ; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 do {
1474 ++buffer;
1475 --remain;
Azael Avalosb5163992015-02-10 23:43:56 -07001476 } while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001479 kfree(cmd);
1480
Seth Forshee36d03f92011-09-20 16:55:53 -05001481 ret = get_video_status(dev, &video_out);
1482 if (!ret) {
Harvey Harrison9e113e02008-09-22 14:37:29 -07001483 unsigned int new_video_out = video_out;
Azael Avalosb5163992015-02-10 23:43:56 -07001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (lcd_out != -1)
1486 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1487 if (crt_out != -1)
1488 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1489 if (tv_out != -1)
1490 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
Darren Harte0769fe2015-02-11 20:50:08 -08001491 /*
1492 * To avoid unnecessary video disruption, only write the new
Azael Avalos3f75bbe2015-05-06 09:35:11 -06001493 * video setting if something changed.
1494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (new_video_out != video_out)
Seth Forshee32bcd5c2011-09-20 16:55:50 -05001496 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
Azael Avalose1a949c2015-07-31 21:58:14 -06001499 return ret ? -EIO : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001502static const struct file_operations video_proc_fops = {
1503 .owner = THIS_MODULE,
1504 .open = video_proc_open,
1505 .read = seq_read,
1506 .llseek = seq_lseek,
1507 .release = single_release,
1508 .write = video_proc_write,
1509};
1510
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001511/* Fan status */
Seth Forshee36d03f92011-09-20 16:55:53 -05001512static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1513{
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001514 u32 result = hci_read(dev, HCI_FAN, status);
Seth Forshee36d03f92011-09-20 16:55:53 -05001515
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001516 if (result == TOS_FAILURE)
1517 pr_err("ACPI call to get Fan status failed\n");
1518 else if (result == TOS_NOT_SUPPORTED)
1519 return -ENODEV;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001520
Azael Avalose1a949c2015-07-31 21:58:14 -06001521 return result == TOS_SUCCESS ? 0 : -EIO;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001522}
1523
1524static int set_fan_status(struct toshiba_acpi_dev *dev, u32 status)
1525{
1526 u32 result = hci_write(dev, HCI_FAN, status);
1527
1528 if (result == TOS_FAILURE)
1529 pr_err("ACPI call to set Fan status failed\n");
1530 else if (result == TOS_NOT_SUPPORTED)
1531 return -ENODEV;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001532
Azael Avalose1a949c2015-07-31 21:58:14 -06001533 return result == TOS_SUCCESS ? 0 : -EIO;
Seth Forshee36d03f92011-09-20 16:55:53 -05001534}
1535
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001536static int fan_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Seth Forshee135740d2011-09-20 16:55:49 -05001538 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 u32 value;
1540
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001541 if (get_fan_status(dev, &value))
1542 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001544 seq_printf(m, "running: %d\n", (value > 0));
1545 seq_printf(m, "force_on: %d\n", dev->force_fan);
1546
1547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001550static int fan_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Al Virod9dda782013-03-31 18:16:14 -04001552 return single_open(file, fan_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001553}
1554
1555static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1556 size_t count, loff_t *pos)
1557{
Al Virod9dda782013-03-31 18:16:14 -04001558 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001559 char cmd[42];
1560 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001563 len = min(count, sizeof(cmd) - 1);
1564 if (copy_from_user(cmd, buf, len))
1565 return -EFAULT;
1566 cmd[len] = '\0';
1567
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001568 if (sscanf(cmd, " force_on : %i", &value) != 1 &&
1569 value != 0 && value != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return -EINVAL;
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001571
1572 if (set_fan_status(dev, value))
1573 return -EIO;
1574
1575 dev->force_fan = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 return count;
1578}
1579
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001580static const struct file_operations fan_proc_fops = {
1581 .owner = THIS_MODULE,
1582 .open = fan_proc_open,
1583 .read = seq_read,
1584 .llseek = seq_lseek,
1585 .release = single_release,
1586 .write = fan_proc_write,
1587};
1588
1589static int keys_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Seth Forshee135740d2011-09-20 16:55:49 -05001591 struct toshiba_acpi_dev *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Seth Forshee135740d2011-09-20 16:55:49 -05001593 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1594 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
Azael Avalos7deef552015-07-22 18:09:10 -06001595
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001599static int keys_proc_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Al Virod9dda782013-03-31 18:16:14 -04001601 return single_open(file, keys_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001602}
1603
1604static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1605 size_t count, loff_t *pos)
1606{
Al Virod9dda782013-03-31 18:16:14 -04001607 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001608 char cmd[42];
1609 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 int value;
1611
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001612 len = min(count, sizeof(cmd) - 1);
1613 if (copy_from_user(cmd, buf, len))
1614 return -EFAULT;
1615 cmd[len] = '\0';
1616
Azael Avalosb5163992015-02-10 23:43:56 -07001617 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
Seth Forshee135740d2011-09-20 16:55:49 -05001618 dev->key_event_valid = 0;
Azael Avalosb5163992015-02-10 23:43:56 -07001619 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 return count;
1623}
1624
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001625static const struct file_operations keys_proc_fops = {
1626 .owner = THIS_MODULE,
1627 .open = keys_proc_open,
1628 .read = seq_read,
1629 .llseek = seq_lseek,
1630 .release = single_release,
1631 .write = keys_proc_write,
1632};
1633
1634static int version_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001636 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1637 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001641static int version_proc_open(struct inode *inode, struct file *file)
1642{
Al Virod9dda782013-03-31 18:16:14 -04001643 return single_open(file, version_proc_show, PDE_DATA(inode));
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001644}
1645
1646static const struct file_operations version_proc_fops = {
1647 .owner = THIS_MODULE,
1648 .open = version_proc_open,
1649 .read = seq_read,
1650 .llseek = seq_lseek,
1651 .release = single_release,
1652};
1653
Darren Harte0769fe2015-02-11 20:50:08 -08001654/*
1655 * Proc and module init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
1657
1658#define PROC_TOSHIBA "toshiba"
1659
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08001660static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Seth Forshee36d03f92011-09-20 16:55:53 -05001662 if (dev->backlight_dev)
1663 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1664 &lcd_proc_fops, dev);
1665 if (dev->video_supported)
1666 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1667 &video_proc_fops, dev);
1668 if (dev->fan_supported)
1669 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1670 &fan_proc_fops, dev);
1671 if (dev->hotkey_dev)
1672 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1673 &keys_proc_fops, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05001674 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1675 &version_proc_fops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Seth Forshee36d03f92011-09-20 16:55:53 -05001678static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Seth Forshee36d03f92011-09-20 16:55:53 -05001680 if (dev->backlight_dev)
1681 remove_proc_entry("lcd", toshiba_proc_dir);
1682 if (dev->video_supported)
1683 remove_proc_entry("video", toshiba_proc_dir);
1684 if (dev->fan_supported)
1685 remove_proc_entry("fan", toshiba_proc_dir);
1686 if (dev->hotkey_dev)
1687 remove_proc_entry("keys", toshiba_proc_dir);
Alexey Dobriyan936c8bc2009-12-21 16:20:02 -08001688 remove_proc_entry("version", toshiba_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Lionel Debrouxacc24722010-11-16 14:14:02 +01001691static const struct backlight_ops toshiba_backlight_data = {
Akio Idehara121b7b02012-04-06 01:46:43 +09001692 .options = BL_CORE_SUSPENDRESUME,
Seth Forshee62cce752012-04-19 11:23:50 -05001693 .get_brightness = get_lcd_brightness,
1694 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -07001695};
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001696
Azael Avalos65e3cf92015-11-23 10:51:30 -07001697/* Keyboard backlight work */
1698static void toshiba_acpi_kbd_bl_work(struct work_struct *work);
1699
1700static DECLARE_WORK(kbd_bl_work, toshiba_acpi_kbd_bl_work);
1701
Azael Avalos360f0f32014-03-25 20:38:31 -06001702/*
1703 * Sysfs files
1704 */
Azael Avalos9d309842015-02-10 23:43:59 -07001705static ssize_t version_show(struct device *dev,
1706 struct device_attribute *attr, char *buf)
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001707{
1708 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1709}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001710static DEVICE_ATTR_RO(version);
Azael Avalosc6c68ff2015-02-10 21:09:16 -07001711
Azael Avalos9d309842015-02-10 23:43:59 -07001712static ssize_t fan_store(struct device *dev,
1713 struct device_attribute *attr,
1714 const char *buf, size_t count)
Azael Avalos94477d42015-02-10 21:09:17 -07001715{
1716 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avalos94477d42015-02-10 21:09:17 -07001717 int state;
1718 int ret;
1719
1720 ret = kstrtoint(buf, 0, &state);
1721 if (ret)
1722 return ret;
1723
1724 if (state != 0 && state != 1)
1725 return -EINVAL;
1726
Azael Avalos3e07e5b2015-07-27 19:22:23 -06001727 ret = set_fan_status(toshiba, state);
1728 if (ret)
1729 return ret;
Azael Avalos94477d42015-02-10 21:09:17 -07001730
1731 return count;
1732}
1733
Azael Avalos9d309842015-02-10 23:43:59 -07001734static ssize_t fan_show(struct device *dev,
1735 struct device_attribute *attr, char *buf)
Azael Avalos94477d42015-02-10 21:09:17 -07001736{
1737 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1738 u32 value;
1739 int ret;
1740
1741 ret = get_fan_status(toshiba, &value);
1742 if (ret)
1743 return ret;
1744
1745 return sprintf(buf, "%d\n", value);
1746}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001747static DEVICE_ATTR_RW(fan);
Azael Avalos94477d42015-02-10 21:09:17 -07001748
Azael Avalos9d309842015-02-10 23:43:59 -07001749static ssize_t kbd_backlight_mode_store(struct device *dev,
1750 struct device_attribute *attr,
1751 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001752{
1753 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Dan Carpenteraeaac092014-09-03 14:44:37 +03001754 int mode;
Dan Carpenteraeaac092014-09-03 14:44:37 +03001755 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001756
Dan Carpenteraeaac092014-09-03 14:44:37 +03001757
1758 ret = kstrtoint(buf, 0, &mode);
1759 if (ret)
1760 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001761
1762 /* Check for supported modes depending on keyboard backlight type */
1763 if (toshiba->kbd_type == 1) {
1764 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1765 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1766 return -EINVAL;
1767 } else if (toshiba->kbd_type == 2) {
1768 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1769 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1770 mode != SCI_KBD_MODE_OFF)
1771 return -EINVAL;
1772 }
Azael Avalos360f0f32014-03-25 20:38:31 -06001773
Darren Harte0769fe2015-02-11 20:50:08 -08001774 /*
1775 * Set the Keyboard Backlight Mode where:
Azael Avalos360f0f32014-03-25 20:38:31 -06001776 * Auto - KBD backlight turns off automatically in given time
1777 * FN-Z - KBD backlight "toggles" when hotkey pressed
Azael Avalos93f8c162014-09-12 18:50:36 -06001778 * ON - KBD backlight is always on
1779 * OFF - KBD backlight is always off
Azael Avalos360f0f32014-03-25 20:38:31 -06001780 */
Azael Avalos93f8c162014-09-12 18:50:36 -06001781
1782 /* Only make a change if the actual mode has changed */
Dan Carpenteraeaac092014-09-03 14:44:37 +03001783 if (toshiba->kbd_mode != mode) {
Azael Avalos93f8c162014-09-12 18:50:36 -06001784 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos1e574db2015-07-22 19:37:49 -06001785 int time = toshiba->kbd_time << HCI_MISC_SHIFT;
Azael Avalos93f8c162014-09-12 18:50:36 -06001786
1787 /* OR the "base time" to the actual method format */
1788 if (toshiba->kbd_type == 1) {
1789 /* Type 1 requires the current mode */
1790 time |= toshiba->kbd_mode;
1791 } else if (toshiba->kbd_type == 2) {
1792 /* Type 2 requires the desired mode */
1793 time |= mode;
1794 }
1795
Dan Carpenteraeaac092014-09-03 14:44:37 +03001796 ret = toshiba_kbd_illum_status_set(toshiba, time);
1797 if (ret)
1798 return ret;
Azael Avalos93f8c162014-09-12 18:50:36 -06001799
Azael Avalos360f0f32014-03-25 20:38:31 -06001800 toshiba->kbd_mode = mode;
Azael Avalos65e3cf92015-11-23 10:51:30 -07001801
1802 /*
1803 * Some laptop models with the second generation backlit
1804 * keyboard (type 2) do not generate the keyboard backlight
1805 * changed event (0x92), and thus, the driver will never update
1806 * the sysfs entries.
1807 *
1808 * The event is generated right when changing the keyboard
1809 * backlight mode and the *notify function will set the
1810 * kbd_event_generated to true.
1811 *
1812 * In case the event is not generated, schedule the keyboard
1813 * backlight work to update the sysfs entries and emulate the
1814 * event via genetlink.
1815 */
1816 if (toshiba->kbd_type == 2 &&
1817 !toshiba_acpi->kbd_event_generated)
1818 schedule_work(&kbd_bl_work);
Azael Avalos360f0f32014-03-25 20:38:31 -06001819 }
1820
1821 return count;
1822}
1823
Azael Avalos9d309842015-02-10 23:43:59 -07001824static ssize_t kbd_backlight_mode_show(struct device *dev,
1825 struct device_attribute *attr,
1826 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001827{
1828 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1829 u32 time;
1830
1831 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1832 return -EIO;
1833
Azael Avalos93f8c162014-09-12 18:50:36 -06001834 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1835}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001836static DEVICE_ATTR_RW(kbd_backlight_mode);
Azael Avalos93f8c162014-09-12 18:50:36 -06001837
Azael Avalos9d309842015-02-10 23:43:59 -07001838static ssize_t kbd_type_show(struct device *dev,
1839 struct device_attribute *attr, char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001840{
1841 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1842
1843 return sprintf(buf, "%d\n", toshiba->kbd_type);
1844}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001845static DEVICE_ATTR_RO(kbd_type);
Azael Avalos93f8c162014-09-12 18:50:36 -06001846
Azael Avalos9d309842015-02-10 23:43:59 -07001847static ssize_t available_kbd_modes_show(struct device *dev,
1848 struct device_attribute *attr,
1849 char *buf)
Azael Avalos93f8c162014-09-12 18:50:36 -06001850{
1851 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1852
1853 if (toshiba->kbd_type == 1)
Azael Avalos0b498202015-09-09 11:30:09 -06001854 return sprintf(buf, "0x%x 0x%x\n",
Azael Avalos93f8c162014-09-12 18:50:36 -06001855 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1856
Azael Avalos0b498202015-09-09 11:30:09 -06001857 return sprintf(buf, "0x%x 0x%x 0x%x\n",
Azael Avalos93f8c162014-09-12 18:50:36 -06001858 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
Azael Avalos360f0f32014-03-25 20:38:31 -06001859}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001860static DEVICE_ATTR_RO(available_kbd_modes);
Azael Avalos360f0f32014-03-25 20:38:31 -06001861
Azael Avalos9d309842015-02-10 23:43:59 -07001862static ssize_t kbd_backlight_timeout_store(struct device *dev,
1863 struct device_attribute *attr,
1864 const char *buf, size_t count)
Azael Avalos360f0f32014-03-25 20:38:31 -06001865{
1866 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
Azael Avaloseabde0f2014-10-04 12:02:21 -06001867 int time;
1868 int ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001869
Azael Avaloseabde0f2014-10-04 12:02:21 -06001870 ret = kstrtoint(buf, 0, &time);
1871 if (ret)
1872 return ret;
Azael Avalos360f0f32014-03-25 20:38:31 -06001873
Azael Avaloseabde0f2014-10-04 12:02:21 -06001874 /* Check for supported values depending on kbd_type */
1875 if (toshiba->kbd_type == 1) {
1876 if (time < 0 || time > 60)
1877 return -EINVAL;
1878 } else if (toshiba->kbd_type == 2) {
1879 if (time < 1 || time > 60)
1880 return -EINVAL;
1881 }
1882
1883 /* Set the Keyboard Backlight Timeout */
1884
1885 /* Only make a change if the actual timeout has changed */
1886 if (toshiba->kbd_time != time) {
1887 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
Azael Avalos360f0f32014-03-25 20:38:31 -06001888 time = time << HCI_MISC_SHIFT;
Azael Avaloseabde0f2014-10-04 12:02:21 -06001889 /* OR the "base time" to the actual method format */
1890 if (toshiba->kbd_type == 1)
1891 time |= SCI_KBD_MODE_FNZ;
1892 else if (toshiba->kbd_type == 2)
1893 time |= SCI_KBD_MODE_AUTO;
1894
1895 ret = toshiba_kbd_illum_status_set(toshiba, time);
1896 if (ret)
1897 return ret;
1898
Azael Avalos360f0f32014-03-25 20:38:31 -06001899 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1900 }
1901
1902 return count;
1903}
1904
Azael Avalos9d309842015-02-10 23:43:59 -07001905static ssize_t kbd_backlight_timeout_show(struct device *dev,
1906 struct device_attribute *attr,
1907 char *buf)
Azael Avalos360f0f32014-03-25 20:38:31 -06001908{
1909 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1910 u32 time;
1911
1912 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1913 return -EIO;
1914
1915 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1916}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001917static DEVICE_ATTR_RW(kbd_backlight_timeout);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001918
Azael Avalos9d309842015-02-10 23:43:59 -07001919static ssize_t touchpad_store(struct device *dev,
1920 struct device_attribute *attr,
1921 const char *buf, size_t count)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001922{
1923 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1924 int state;
Azael Avalosc8a41662014-09-10 21:01:57 -06001925 int ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001926
1927 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
Azael Avalosc8a41662014-09-10 21:01:57 -06001928 ret = kstrtoint(buf, 0, &state);
1929 if (ret)
1930 return ret;
1931 if (state != 0 && state != 1)
1932 return -EINVAL;
1933
1934 ret = toshiba_touchpad_set(toshiba, state);
1935 if (ret)
1936 return ret;
Azael Avalos9d8658a2014-03-25 20:38:32 -06001937
1938 return count;
1939}
1940
Azael Avalos9d309842015-02-10 23:43:59 -07001941static ssize_t touchpad_show(struct device *dev,
1942 struct device_attribute *attr, char *buf)
Azael Avalos9d8658a2014-03-25 20:38:32 -06001943{
1944 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1945 u32 state;
1946 int ret;
1947
1948 ret = toshiba_touchpad_get(toshiba, &state);
1949 if (ret < 0)
1950 return ret;
1951
1952 return sprintf(buf, "%i\n", state);
1953}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001954static DEVICE_ATTR_RW(touchpad);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04001955
Azael Avalos9d309842015-02-10 23:43:59 -07001956static ssize_t position_show(struct device *dev,
1957 struct device_attribute *attr, char *buf)
Azael Avalos5a2813e2014-03-25 20:38:34 -06001958{
1959 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1960 u32 xyval, zval, tmp;
1961 u16 x, y, z;
1962 int ret;
1963
1964 xyval = zval = 0;
1965 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1966 if (ret < 0)
1967 return ret;
1968
1969 x = xyval & HCI_ACCEL_MASK;
1970 tmp = xyval >> HCI_MISC_SHIFT;
1971 y = tmp & HCI_ACCEL_MASK;
1972 z = zval & HCI_ACCEL_MASK;
1973
1974 return sprintf(buf, "%d %d %d\n", x, y, z);
1975}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07001976static DEVICE_ATTR_RO(position);
Azael Avalos360f0f32014-03-25 20:38:31 -06001977
Azael Avalos9d309842015-02-10 23:43:59 -07001978static ssize_t usb_sleep_charge_show(struct device *dev,
1979 struct device_attribute *attr, char *buf)
Azael Avalose26ffe52015-01-18 18:30:22 -07001980{
1981 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1982 u32 mode;
1983 int ret;
1984
1985 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1986 if (ret < 0)
1987 return ret;
1988
1989 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1990}
1991
Azael Avalos9d309842015-02-10 23:43:59 -07001992static ssize_t usb_sleep_charge_store(struct device *dev,
1993 struct device_attribute *attr,
1994 const char *buf, size_t count)
Azael Avalose26ffe52015-01-18 18:30:22 -07001995{
1996 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1997 u32 mode;
1998 int state;
1999 int ret;
2000
2001 ret = kstrtoint(buf, 0, &state);
2002 if (ret)
2003 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002004 /*
2005 * Check for supported values, where:
Azael Avalose26ffe52015-01-18 18:30:22 -07002006 * 0 - Disabled
2007 * 1 - Alternate (Non USB conformant devices that require more power)
2008 * 2 - Auto (USB conformant devices)
Azael Avalosc8c91842015-04-02 19:26:20 -06002009 * 3 - Typical
Azael Avalose26ffe52015-01-18 18:30:22 -07002010 */
Azael Avalosc8c91842015-04-02 19:26:20 -06002011 if (state != 0 && state != 1 && state != 2 && state != 3)
Azael Avalose26ffe52015-01-18 18:30:22 -07002012 return -EINVAL;
2013
2014 /* Set the USB charging mode to internal value */
Azael Avalosc8c91842015-04-02 19:26:20 -06002015 mode = toshiba->usbsc_mode_base;
Azael Avalose26ffe52015-01-18 18:30:22 -07002016 if (state == 0)
Azael Avalosc8c91842015-04-02 19:26:20 -06002017 mode |= SCI_USB_CHARGE_DISABLED;
Azael Avalose26ffe52015-01-18 18:30:22 -07002018 else if (state == 1)
Azael Avalosc8c91842015-04-02 19:26:20 -06002019 mode |= SCI_USB_CHARGE_ALTERNATE;
Azael Avalose26ffe52015-01-18 18:30:22 -07002020 else if (state == 2)
Azael Avalosc8c91842015-04-02 19:26:20 -06002021 mode |= SCI_USB_CHARGE_AUTO;
2022 else if (state == 3)
2023 mode |= SCI_USB_CHARGE_TYPICAL;
Azael Avalose26ffe52015-01-18 18:30:22 -07002024
2025 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2026 if (ret)
2027 return ret;
2028
2029 return count;
2030}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002031static DEVICE_ATTR_RW(usb_sleep_charge);
Azael Avalose26ffe52015-01-18 18:30:22 -07002032
Azael Avalos182bcaa2015-01-18 18:30:23 -07002033static ssize_t sleep_functions_on_battery_show(struct device *dev,
2034 struct device_attribute *attr,
2035 char *buf)
2036{
2037 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2038 u32 state;
2039 int bat_lvl;
2040 int status;
2041 int ret;
2042 int tmp;
2043
2044 ret = toshiba_sleep_functions_status_get(toshiba, &state);
2045 if (ret < 0)
2046 return ret;
2047
2048 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2049 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2050 status = (tmp == 0x4) ? 1 : 0;
2051 /* Determine the battery level set */
2052 bat_lvl = state >> HCI_MISC_SHIFT;
2053
2054 return sprintf(buf, "%d %d\n", status, bat_lvl);
2055}
2056
2057static ssize_t sleep_functions_on_battery_store(struct device *dev,
2058 struct device_attribute *attr,
2059 const char *buf, size_t count)
2060{
2061 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2062 u32 status;
2063 int value;
2064 int ret;
2065 int tmp;
2066
2067 ret = kstrtoint(buf, 0, &value);
2068 if (ret)
2069 return ret;
2070
Darren Harte0769fe2015-02-11 20:50:08 -08002071 /*
2072 * Set the status of the function:
Azael Avalos182bcaa2015-01-18 18:30:23 -07002073 * 0 - Disabled
2074 * 1-100 - Enabled
2075 */
2076 if (value < 0 || value > 100)
2077 return -EINVAL;
2078
2079 if (value == 0) {
2080 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2081 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2082 } else {
2083 tmp = value << HCI_MISC_SHIFT;
2084 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2085 }
2086 ret = toshiba_sleep_functions_status_set(toshiba, status);
2087 if (ret < 0)
2088 return ret;
2089
2090 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2091
2092 return count;
2093}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002094static DEVICE_ATTR_RW(sleep_functions_on_battery);
Azael Avalos182bcaa2015-01-18 18:30:23 -07002095
Azael Avalos9d309842015-02-10 23:43:59 -07002096static ssize_t usb_rapid_charge_show(struct device *dev,
2097 struct device_attribute *attr, char *buf)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002098{
2099 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2100 u32 state;
2101 int ret;
2102
2103 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2104 if (ret < 0)
2105 return ret;
2106
2107 return sprintf(buf, "%d\n", state);
2108}
2109
Azael Avalos9d309842015-02-10 23:43:59 -07002110static ssize_t usb_rapid_charge_store(struct device *dev,
2111 struct device_attribute *attr,
2112 const char *buf, size_t count)
Azael Avalosbb3fe012015-01-18 18:30:24 -07002113{
2114 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2115 int state;
2116 int ret;
2117
2118 ret = kstrtoint(buf, 0, &state);
2119 if (ret)
2120 return ret;
2121 if (state != 0 && state != 1)
2122 return -EINVAL;
2123
2124 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2125 if (ret)
2126 return ret;
2127
2128 return count;
2129}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002130static DEVICE_ATTR_RW(usb_rapid_charge);
Azael Avalosbb3fe012015-01-18 18:30:24 -07002131
Azael Avalos9d309842015-02-10 23:43:59 -07002132static ssize_t usb_sleep_music_show(struct device *dev,
2133 struct device_attribute *attr, char *buf)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002134{
2135 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2136 u32 state;
2137 int ret;
2138
2139 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2140 if (ret < 0)
2141 return ret;
2142
2143 return sprintf(buf, "%d\n", state);
2144}
2145
Azael Avalos9d309842015-02-10 23:43:59 -07002146static ssize_t usb_sleep_music_store(struct device *dev,
2147 struct device_attribute *attr,
2148 const char *buf, size_t count)
Azael Avalos172ce0a2015-01-18 18:30:25 -07002149{
2150 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2151 int state;
2152 int ret;
2153
2154 ret = kstrtoint(buf, 0, &state);
2155 if (ret)
2156 return ret;
2157 if (state != 0 && state != 1)
2158 return -EINVAL;
2159
2160 ret = toshiba_usb_sleep_music_set(toshiba, state);
2161 if (ret)
2162 return ret;
2163
2164 return count;
2165}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002166static DEVICE_ATTR_RW(usb_sleep_music);
Azael Avalos172ce0a2015-01-18 18:30:25 -07002167
Azael Avalos9d309842015-02-10 23:43:59 -07002168static ssize_t kbd_function_keys_show(struct device *dev,
2169 struct device_attribute *attr, char *buf)
Azael Avalosbae84192015-02-10 21:09:18 -07002170{
2171 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2172 int mode;
2173 int ret;
2174
2175 ret = toshiba_function_keys_get(toshiba, &mode);
2176 if (ret < 0)
2177 return ret;
2178
2179 return sprintf(buf, "%d\n", mode);
2180}
2181
Azael Avalos9d309842015-02-10 23:43:59 -07002182static ssize_t kbd_function_keys_store(struct device *dev,
2183 struct device_attribute *attr,
2184 const char *buf, size_t count)
Azael Avalosbae84192015-02-10 21:09:18 -07002185{
2186 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2187 int mode;
2188 int ret;
2189
2190 ret = kstrtoint(buf, 0, &mode);
2191 if (ret)
2192 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002193 /*
2194 * Check for the function keys mode where:
Azael Avalosbae84192015-02-10 21:09:18 -07002195 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2196 * 1 - Special functions (Opposite of the above setting)
2197 */
2198 if (mode != 0 && mode != 1)
2199 return -EINVAL;
2200
2201 ret = toshiba_function_keys_set(toshiba, mode);
2202 if (ret)
2203 return ret;
2204
2205 pr_info("Reboot for changes to KBD Function Keys to take effect");
2206
2207 return count;
2208}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002209static DEVICE_ATTR_RW(kbd_function_keys);
Azael Avalosbae84192015-02-10 21:09:18 -07002210
Azael Avalos9d309842015-02-10 23:43:59 -07002211static ssize_t panel_power_on_show(struct device *dev,
2212 struct device_attribute *attr, char *buf)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002213{
2214 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2215 u32 state;
2216 int ret;
2217
2218 ret = toshiba_panel_power_on_get(toshiba, &state);
2219 if (ret < 0)
2220 return ret;
2221
2222 return sprintf(buf, "%d\n", state);
2223}
2224
Azael Avalos9d309842015-02-10 23:43:59 -07002225static ssize_t panel_power_on_store(struct device *dev,
2226 struct device_attribute *attr,
2227 const char *buf, size_t count)
Azael Avalos35d53ce2015-02-10 21:09:19 -07002228{
2229 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2230 int state;
2231 int ret;
2232
2233 ret = kstrtoint(buf, 0, &state);
2234 if (ret)
2235 return ret;
2236 if (state != 0 && state != 1)
2237 return -EINVAL;
2238
2239 ret = toshiba_panel_power_on_set(toshiba, state);
2240 if (ret)
2241 return ret;
2242
2243 pr_info("Reboot for changes to Panel Power ON to take effect");
2244
2245 return count;
2246}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002247static DEVICE_ATTR_RW(panel_power_on);
Azael Avalos35d53ce2015-02-10 21:09:19 -07002248
Azael Avalos9d309842015-02-10 23:43:59 -07002249static ssize_t usb_three_show(struct device *dev,
2250 struct device_attribute *attr, char *buf)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002251{
2252 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2253 u32 state;
2254 int ret;
2255
2256 ret = toshiba_usb_three_get(toshiba, &state);
2257 if (ret < 0)
2258 return ret;
2259
2260 return sprintf(buf, "%d\n", state);
2261}
2262
Azael Avalos9d309842015-02-10 23:43:59 -07002263static ssize_t usb_three_store(struct device *dev,
2264 struct device_attribute *attr,
2265 const char *buf, size_t count)
Azael Avalos17fe4b32015-02-10 21:09:20 -07002266{
2267 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2268 int state;
2269 int ret;
2270
2271 ret = kstrtoint(buf, 0, &state);
2272 if (ret)
2273 return ret;
Darren Harte0769fe2015-02-11 20:50:08 -08002274 /*
2275 * Check for USB 3 mode where:
Azael Avalos17fe4b32015-02-10 21:09:20 -07002276 * 0 - Disabled (Acts like a USB 2 port, saving power)
2277 * 1 - Enabled
2278 */
2279 if (state != 0 && state != 1)
2280 return -EINVAL;
2281
2282 ret = toshiba_usb_three_set(toshiba, state);
2283 if (ret)
2284 return ret;
2285
2286 pr_info("Reboot for changes to USB 3 to take effect");
2287
2288 return count;
2289}
Azael Avalos0c3c0f12015-02-10 23:44:00 -07002290static DEVICE_ATTR_RW(usb_three);
Azael Avalos9bd12132015-02-10 23:43:58 -07002291
2292static struct attribute *toshiba_attributes[] = {
2293 &dev_attr_version.attr,
2294 &dev_attr_fan.attr,
2295 &dev_attr_kbd_backlight_mode.attr,
2296 &dev_attr_kbd_type.attr,
2297 &dev_attr_available_kbd_modes.attr,
2298 &dev_attr_kbd_backlight_timeout.attr,
2299 &dev_attr_touchpad.attr,
2300 &dev_attr_position.attr,
2301 &dev_attr_usb_sleep_charge.attr,
2302 &dev_attr_sleep_functions_on_battery.attr,
2303 &dev_attr_usb_rapid_charge.attr,
2304 &dev_attr_usb_sleep_music.attr,
2305 &dev_attr_kbd_function_keys.attr,
2306 &dev_attr_panel_power_on.attr,
2307 &dev_attr_usb_three.attr,
2308 NULL,
2309};
2310
Azael Avalos360f0f32014-03-25 20:38:31 -06002311static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2312 struct attribute *attr, int idx)
2313{
2314 struct device *dev = container_of(kobj, struct device, kobj);
2315 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2316 bool exists = true;
2317
Azael Avalos94477d42015-02-10 21:09:17 -07002318 if (attr == &dev_attr_fan.attr)
2319 exists = (drv->fan_supported) ? true : false;
2320 else if (attr == &dev_attr_kbd_backlight_mode.attr)
Azael Avalos360f0f32014-03-25 20:38:31 -06002321 exists = (drv->kbd_illum_supported) ? true : false;
2322 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2323 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
Azael Avalos9d8658a2014-03-25 20:38:32 -06002324 else if (attr == &dev_attr_touchpad.attr)
2325 exists = (drv->touchpad_supported) ? true : false;
Azael Avalos5a2813e2014-03-25 20:38:34 -06002326 else if (attr == &dev_attr_position.attr)
2327 exists = (drv->accelerometer_supported) ? true : false;
Azael Avalose26ffe52015-01-18 18:30:22 -07002328 else if (attr == &dev_attr_usb_sleep_charge.attr)
2329 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalos182bcaa2015-01-18 18:30:23 -07002330 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2331 exists = (drv->usb_sleep_charge_supported) ? true : false;
Azael Avalosbb3fe012015-01-18 18:30:24 -07002332 else if (attr == &dev_attr_usb_rapid_charge.attr)
2333 exists = (drv->usb_rapid_charge_supported) ? true : false;
Azael Avalos172ce0a2015-01-18 18:30:25 -07002334 else if (attr == &dev_attr_usb_sleep_music.attr)
2335 exists = (drv->usb_sleep_music_supported) ? true : false;
Azael Avalosbae84192015-02-10 21:09:18 -07002336 else if (attr == &dev_attr_kbd_function_keys.attr)
2337 exists = (drv->kbd_function_keys_supported) ? true : false;
Azael Avalos35d53ce2015-02-10 21:09:19 -07002338 else if (attr == &dev_attr_panel_power_on.attr)
2339 exists = (drv->panel_power_on_supported) ? true : false;
Azael Avalos17fe4b32015-02-10 21:09:20 -07002340 else if (attr == &dev_attr_usb_three.attr)
2341 exists = (drv->usb_three_supported) ? true : false;
Azael Avalos360f0f32014-03-25 20:38:31 -06002342
2343 return exists ? attr->mode : 0;
2344}
2345
Azael Avalos9bd12132015-02-10 23:43:58 -07002346static struct attribute_group toshiba_attr_group = {
2347 .is_visible = toshiba_sysfs_is_visible,
2348 .attrs = toshiba_attributes,
2349};
2350
Azael Avalos65e3cf92015-11-23 10:51:30 -07002351static void toshiba_acpi_kbd_bl_work(struct work_struct *work)
2352{
2353 struct acpi_device *acpi_dev = toshiba_acpi->acpi_dev;
2354
2355 /* Update the sysfs entries */
2356 if (sysfs_update_group(&acpi_dev->dev.kobj,
2357 &toshiba_attr_group))
2358 pr_err("Unable to update sysfs entries\n");
2359
2360 /* Emulate the keyboard backlight event */
2361 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2362 dev_name(&acpi_dev->dev),
2363 0x92, 0);
2364}
2365
Azael Avalos1f28f292014-12-04 20:22:45 -07002366/*
Azael Avalosfc5462f2015-07-22 18:09:11 -06002367 * Misc device
2368 */
2369static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
2370{
2371 u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
2372 regs->edx, regs->esi, regs->edi };
2373 u32 out[TCI_WORDS];
2374 acpi_status status;
2375
2376 status = tci_raw(toshiba_acpi, in, out);
2377 if (ACPI_FAILURE(status)) {
2378 pr_err("ACPI call to query SMM registers failed\n");
2379 return -EIO;
2380 }
2381
2382 /* Fillout the SMM struct with the TCI call results */
2383 regs->eax = out[0];
2384 regs->ebx = out[1];
2385 regs->ecx = out[2];
2386 regs->edx = out[3];
2387 regs->esi = out[4];
2388 regs->edi = out[5];
2389
2390 return 0;
2391}
2392
2393static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
2394 unsigned long arg)
2395{
2396 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
2397 SMMRegisters regs;
2398 int ret;
2399
2400 if (!argp)
2401 return -EINVAL;
2402
2403 switch (cmd) {
2404 case TOSH_SMM:
2405 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2406 return -EFAULT;
2407 ret = toshiba_acpi_smm_bridge(&regs);
2408 if (ret)
2409 return ret;
2410 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2411 return -EFAULT;
2412 break;
2413 case TOSHIBA_ACPI_SCI:
2414 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2415 return -EFAULT;
2416 /* Ensure we are being called with a SCI_{GET, SET} register */
2417 if (regs.eax != SCI_GET && regs.eax != SCI_SET)
2418 return -EINVAL;
2419 if (!sci_open(toshiba_acpi))
2420 return -EIO;
2421 ret = toshiba_acpi_smm_bridge(&regs);
2422 sci_close(toshiba_acpi);
2423 if (ret)
2424 return ret;
2425 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2426 return -EFAULT;
2427 break;
2428 default:
2429 return -EINVAL;
2430 }
2431
2432 return 0;
2433}
2434
2435static const struct file_operations toshiba_acpi_fops = {
2436 .owner = THIS_MODULE,
2437 .unlocked_ioctl = toshiba_acpi_ioctl,
2438 .llseek = noop_llseek,
2439};
2440
2441/*
Azael Avalos2fdde832015-11-23 10:49:11 -07002442 * WWAN RFKill handlers
2443 */
2444static int toshiba_acpi_wwan_set_block(void *data, bool blocked)
2445{
2446 struct toshiba_acpi_dev *dev = data;
2447 int ret;
2448
2449 ret = toshiba_wireless_status(dev);
2450 if (ret)
2451 return ret;
2452
2453 if (!dev->killswitch)
2454 return 0;
2455
2456 return toshiba_wwan_set(dev, !blocked);
2457}
2458
2459static void toshiba_acpi_wwan_poll(struct rfkill *rfkill, void *data)
2460{
2461 struct toshiba_acpi_dev *dev = data;
2462
2463 if (toshiba_wireless_status(dev))
2464 return;
2465
2466 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
2467}
2468
2469static const struct rfkill_ops wwan_rfk_ops = {
2470 .set_block = toshiba_acpi_wwan_set_block,
2471 .poll = toshiba_acpi_wwan_poll,
2472};
2473
2474static int toshiba_acpi_setup_wwan_rfkill(struct toshiba_acpi_dev *dev)
2475{
2476 int ret = toshiba_wireless_status(dev);
2477
2478 if (ret)
2479 return ret;
2480
2481 dev->wwan_rfk = rfkill_alloc("Toshiba WWAN",
2482 &dev->acpi_dev->dev,
2483 RFKILL_TYPE_WWAN,
2484 &wwan_rfk_ops,
2485 dev);
2486 if (!dev->wwan_rfk) {
2487 pr_err("Unable to allocate WWAN rfkill device\n");
2488 return -ENOMEM;
2489 }
2490
2491 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
2492
2493 ret = rfkill_register(dev->wwan_rfk);
2494 if (ret) {
2495 pr_err("Unable to register WWAN rfkill device\n");
2496 rfkill_destroy(dev->wwan_rfk);
2497 }
2498
2499 return ret;
2500}
2501
2502/*
Azael Avalos1f28f292014-12-04 20:22:45 -07002503 * Hotkeys
2504 */
2505static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2506{
2507 acpi_status status;
2508 u32 result;
2509
2510 status = acpi_evaluate_object(dev->acpi_dev->handle,
2511 "ENAB", NULL, NULL);
2512 if (ACPI_FAILURE(status))
2513 return -ENODEV;
2514
Azael Avalosb116fd002015-09-09 11:28:19 -06002515 /*
2516 * Enable the "Special Functions" mode only if they are
2517 * supported and if they are activated.
2518 */
2519 if (dev->kbd_function_keys_supported && dev->special_functions)
2520 result = hci_write(dev, HCI_HOTKEY_EVENT,
2521 HCI_HOTKEY_SPECIAL_FUNCTIONS);
2522 else
2523 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2524
Azael Avalos1f28f292014-12-04 20:22:45 -07002525 if (result == TOS_FAILURE)
2526 return -EIO;
2527 else if (result == TOS_NOT_SUPPORTED)
2528 return -ENODEV;
2529
2530 return 0;
2531}
2532
Seth Forshee29cd2932012-01-18 13:44:09 -06002533static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2534 struct serio *port)
2535{
Giedrius Statkevičius98280372014-10-18 02:57:20 +03002536 if (str & I8042_STR_AUXDATA)
Seth Forshee29cd2932012-01-18 13:44:09 -06002537 return false;
2538
2539 if (unlikely(data == 0xe0))
2540 return false;
2541
2542 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2543 schedule_work(&toshiba_acpi->hotkey_work);
2544 return true;
2545 }
2546
2547 return false;
2548}
2549
2550static void toshiba_acpi_hotkey_work(struct work_struct *work)
2551{
2552 acpi_handle ec_handle = ec_get_handle();
2553 acpi_status status;
2554
2555 if (!ec_handle)
2556 return;
2557
2558 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2559 if (ACPI_FAILURE(status))
2560 pr_err("ACPI NTFY method execution failed\n");
2561}
2562
2563/*
2564 * Returns hotkey scancode, or < 0 on failure.
2565 */
2566static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2567{
Zhang Rui74facaf2013-09-03 08:32:15 +08002568 unsigned long long value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002569 acpi_status status;
2570
Zhang Rui74facaf2013-09-03 08:32:15 +08002571 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2572 NULL, &value);
2573 if (ACPI_FAILURE(status)) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002574 pr_err("ACPI INFO method execution failed\n");
2575 return -EIO;
2576 }
2577
Zhang Rui74facaf2013-09-03 08:32:15 +08002578 return value;
Seth Forshee29cd2932012-01-18 13:44:09 -06002579}
2580
2581static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2582 int scancode)
2583{
2584 if (scancode == 0x100)
2585 return;
2586
Darren Harte0769fe2015-02-11 20:50:08 -08002587 /* Act on key press; ignore key release */
Seth Forshee29cd2932012-01-18 13:44:09 -06002588 if (scancode & 0x80)
2589 return;
2590
2591 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2592 pr_info("Unknown key %x\n", scancode);
2593}
2594
Azael Avalos71454d72014-12-04 20:22:46 -07002595static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2596{
Azael Avalos71454d72014-12-04 20:22:46 -07002597 if (dev->info_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002598 int scancode = toshiba_acpi_query_hotkey(dev);
2599
2600 if (scancode < 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002601 pr_err("Failed to query hotkey event\n");
Azael Avalos7deef552015-07-22 18:09:10 -06002602 } else if (scancode != 0) {
Azael Avalos71454d72014-12-04 20:22:46 -07002603 toshiba_acpi_report_hotkey(dev, scancode);
Azael Avalos7deef552015-07-22 18:09:10 -06002604 dev->key_event_valid = 1;
2605 dev->last_key_event = scancode;
2606 }
Azael Avalos71454d72014-12-04 20:22:46 -07002607 } else if (dev->system_event_supported) {
Azael Avalos7deef552015-07-22 18:09:10 -06002608 u32 result;
2609 u32 value;
2610 int retries = 3;
2611
Azael Avalos71454d72014-12-04 20:22:46 -07002612 do {
Azael Avalos7deef552015-07-22 18:09:10 -06002613 result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
2614 switch (result) {
Azael Avalos71454d72014-12-04 20:22:46 -07002615 case TOS_SUCCESS:
2616 toshiba_acpi_report_hotkey(dev, (int)value);
Azael Avalos7deef552015-07-22 18:09:10 -06002617 dev->key_event_valid = 1;
2618 dev->last_key_event = value;
Azael Avalos71454d72014-12-04 20:22:46 -07002619 break;
2620 case TOS_NOT_SUPPORTED:
2621 /*
2622 * This is a workaround for an unresolved
2623 * issue on some machines where system events
2624 * sporadically become disabled.
2625 */
Azael Avalos7deef552015-07-22 18:09:10 -06002626 result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
2627 if (result == TOS_SUCCESS)
2628 pr_notice("Re-enabled hotkeys\n");
Darren Harte0769fe2015-02-11 20:50:08 -08002629 /* Fall through */
Azael Avalos71454d72014-12-04 20:22:46 -07002630 default:
2631 retries--;
2632 break;
2633 }
Azael Avalos7deef552015-07-22 18:09:10 -06002634 } while (retries && result != TOS_FIFO_EMPTY);
Azael Avalos71454d72014-12-04 20:22:46 -07002635 }
2636}
2637
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002638static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
Matthew Garrett6335e4d2010-02-25 15:20:54 -05002639{
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002640 const struct key_entry *keymap = toshiba_acpi_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002641 acpi_handle ec_handle;
Azael Avalosa2b34712015-03-20 16:55:17 -06002642 int error;
2643
Azael Avalosa88bc062015-07-22 18:09:12 -06002644 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID)) {
2645 pr_info("WMI event detected, hotkeys will not be monitored\n");
2646 return 0;
2647 }
2648
Azael Avalosa2b34712015-03-20 16:55:17 -06002649 error = toshiba_acpi_enable_hotkeys(dev);
2650 if (error)
2651 return error;
2652
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002653 if (toshiba_hotkey_event_type_get(dev, &dev->hotkey_event_type))
Azael Avalos53147b62015-09-09 11:25:45 -06002654 pr_notice("Unable to query Hotkey Event Type\n");
2655
Seth Forshee135740d2011-09-20 16:55:49 -05002656 dev->hotkey_dev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -07002657 if (!dev->hotkey_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002658 return -ENOMEM;
Seth Forshee135740d2011-09-20 16:55:49 -05002659
2660 dev->hotkey_dev->name = "Toshiba input device";
Seth Forshee6e02cc72011-09-20 16:55:51 -05002661 dev->hotkey_dev->phys = "toshiba_acpi/input0";
Seth Forshee135740d2011-09-20 16:55:49 -05002662 dev->hotkey_dev->id.bustype = BUS_HOST;
2663
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002664 if (dev->hotkey_event_type == HCI_SYSTEM_TYPE1 ||
Azael Avalosa2b34712015-03-20 16:55:17 -06002665 !dev->kbd_function_keys_supported)
2666 keymap = toshiba_acpi_keymap;
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002667 else if (dev->hotkey_event_type == HCI_SYSTEM_TYPE2 ||
Azael Avalosa2b34712015-03-20 16:55:17 -06002668 dev->kbd_function_keys_supported)
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002669 keymap = toshiba_acpi_alt_keymap;
Azael Avalosa2b34712015-03-20 16:55:17 -06002670 else
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002671 pr_info("Unknown event type received %x\n",
2672 dev->hotkey_event_type);
Takashi Iwaife808bfb2014-04-29 15:15:38 +02002673 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
Seth Forshee135740d2011-09-20 16:55:49 -05002674 if (error)
2675 goto err_free_dev;
2676
Seth Forshee29cd2932012-01-18 13:44:09 -06002677 /*
2678 * For some machines the SCI responsible for providing hotkey
2679 * notification doesn't fire. We can trigger the notification
2680 * whenever the Fn key is pressed using the NTFY method, if
2681 * supported, so if it's present set up an i8042 key filter
2682 * for this purpose.
2683 */
Seth Forshee29cd2932012-01-18 13:44:09 -06002684 ec_handle = ec_get_handle();
Zhang Ruie2e19602013-09-03 08:32:06 +08002685 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
Seth Forshee29cd2932012-01-18 13:44:09 -06002686 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2687
2688 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2689 if (error) {
2690 pr_err("Error installing key filter\n");
2691 goto err_free_keymap;
2692 }
2693
2694 dev->ntfy_supported = 1;
2695 }
2696
2697 /*
2698 * Determine hotkey query interface. Prefer using the INFO
2699 * method when it is available.
2700 */
Zhang Ruie2e19602013-09-03 08:32:06 +08002701 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
Seth Forshee29cd2932012-01-18 13:44:09 -06002702 dev->info_supported = 1;
Azael Avalos10e6aaa2015-09-18 22:45:34 -06002703 else if (hci_write(dev, HCI_SYSTEM_EVENT, 1) == TOS_SUCCESS)
2704 dev->system_event_supported = 1;
Seth Forshee29cd2932012-01-18 13:44:09 -06002705
2706 if (!dev->info_supported && !dev->system_event_supported) {
2707 pr_warn("No hotkey query interface found\n");
2708 goto err_remove_filter;
2709 }
2710
Seth Forshee135740d2011-09-20 16:55:49 -05002711 error = input_register_device(dev->hotkey_dev);
2712 if (error) {
2713 pr_info("Unable to register input device\n");
Seth Forshee29cd2932012-01-18 13:44:09 -06002714 goto err_remove_filter;
Seth Forshee135740d2011-09-20 16:55:49 -05002715 }
2716
2717 return 0;
2718
Seth Forshee29cd2932012-01-18 13:44:09 -06002719 err_remove_filter:
2720 if (dev->ntfy_supported)
2721 i8042_remove_filter(toshiba_acpi_i8042_filter);
Seth Forshee135740d2011-09-20 16:55:49 -05002722 err_free_keymap:
2723 sparse_keymap_free(dev->hotkey_dev);
2724 err_free_dev:
2725 input_free_device(dev->hotkey_dev);
2726 dev->hotkey_dev = NULL;
2727 return error;
2728}
2729
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002730static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
Seth Forshee62cce752012-04-19 11:23:50 -05002731{
2732 struct backlight_properties props;
2733 int brightness;
2734 int ret;
2735
2736 /*
2737 * Some machines don't support the backlight methods at all, and
2738 * others support it read-only. Either of these is pretty useless,
2739 * so only register the backlight device if the backlight method
2740 * supports both reads and writes.
2741 */
2742 brightness = __get_lcd_brightness(dev);
2743 if (brightness < 0)
2744 return 0;
Azael Avalosbae53362015-11-15 20:32:47 -07002745 /*
2746 * If transflective backlight is supported and the brightness is zero
2747 * (lowest brightness level), the set_lcd_brightness function will
2748 * activate the transflective backlight, making the LCD appear to be
2749 * turned off, simply increment the brightness level to avoid that.
2750 */
2751 if (dev->tr_backlight_supported && brightness == 0)
2752 brightness++;
Seth Forshee62cce752012-04-19 11:23:50 -05002753 ret = set_lcd_brightness(dev, brightness);
2754 if (ret) {
2755 pr_debug("Backlight method is read-only, disabling backlight support\n");
2756 return 0;
2757 }
2758
Hans de Goede358d6a22015-04-21 12:01:32 +02002759 /*
2760 * Tell acpi-video-detect code to prefer vendor backlight on all
2761 * systems with transflective backlight and on dmi matched systems.
2762 */
2763 if (dev->tr_backlight_supported ||
2764 dmi_check_system(toshiba_vendor_backlight_dmi))
Hans de Goede234b7cf2015-06-16 16:28:11 +02002765 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
Hans de Goede358d6a22015-04-21 12:01:32 +02002766
Hans de Goede234b7cf2015-06-16 16:28:11 +02002767 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
Hans de Goede358d6a22015-04-21 12:01:32 +02002768 return 0;
2769
Matthew Garrett53039f22012-06-01 11:02:36 -04002770 memset(&props, 0, sizeof(props));
Seth Forshee62cce752012-04-19 11:23:50 -05002771 props.type = BACKLIGHT_PLATFORM;
2772 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Seth Forshee62cce752012-04-19 11:23:50 -05002773
Darren Harte0769fe2015-02-11 20:50:08 -08002774 /* Adding an extra level and having 0 change to transflective mode */
Akio Idehara121b7b02012-04-06 01:46:43 +09002775 if (dev->tr_backlight_supported)
2776 props.max_brightness++;
2777
Seth Forshee62cce752012-04-19 11:23:50 -05002778 dev->backlight_dev = backlight_device_register("toshiba",
2779 &dev->acpi_dev->dev,
2780 dev,
2781 &toshiba_backlight_data,
2782 &props);
2783 if (IS_ERR(dev->backlight_dev)) {
2784 ret = PTR_ERR(dev->backlight_dev);
2785 pr_err("Could not register toshiba backlight device\n");
2786 dev->backlight_dev = NULL;
2787 return ret;
2788 }
2789
2790 dev->backlight_dev->props.brightness = brightness;
2791 return 0;
2792}
2793
Azael Avalos0409cbc2015-07-31 21:58:13 -06002794static void print_supported_features(struct toshiba_acpi_dev *dev)
2795{
2796 pr_info("Supported laptop features:");
2797
2798 if (dev->hotkey_dev)
2799 pr_cont(" hotkeys");
2800 if (dev->backlight_dev)
2801 pr_cont(" backlight");
2802 if (dev->video_supported)
2803 pr_cont(" video-out");
2804 if (dev->fan_supported)
2805 pr_cont(" fan");
2806 if (dev->tr_backlight_supported)
2807 pr_cont(" transflective-backlight");
2808 if (dev->illumination_supported)
2809 pr_cont(" illumination");
2810 if (dev->kbd_illum_supported)
2811 pr_cont(" keyboard-backlight");
2812 if (dev->touchpad_supported)
2813 pr_cont(" touchpad");
2814 if (dev->eco_supported)
2815 pr_cont(" eco-led");
2816 if (dev->accelerometer_supported)
2817 pr_cont(" accelerometer-axes");
2818 if (dev->usb_sleep_charge_supported)
2819 pr_cont(" usb-sleep-charge");
2820 if (dev->usb_rapid_charge_supported)
2821 pr_cont(" usb-rapid-charge");
2822 if (dev->usb_sleep_music_supported)
2823 pr_cont(" usb-sleep-music");
2824 if (dev->kbd_function_keys_supported)
2825 pr_cont(" special-function-keys");
2826 if (dev->panel_power_on_supported)
2827 pr_cont(" panel-power-on");
2828 if (dev->usb_three_supported)
2829 pr_cont(" usb3");
Azael Avalos6873f462015-11-23 10:49:10 -07002830 if (dev->wwan_supported)
2831 pr_cont(" wwan");
Azael Avalos763ff322016-01-25 11:29:10 -07002832 if (dev->cooling_method_supported)
2833 pr_cont(" cooling-method");
Azael Avalos0409cbc2015-07-31 21:58:13 -06002834
2835 pr_cont("\n");
2836}
2837
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002838static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002839{
2840 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2841
Azael Avalosfc5462f2015-07-22 18:09:11 -06002842 misc_deregister(&dev->miscdev);
2843
Seth Forshee36d03f92011-09-20 16:55:53 -05002844 remove_toshiba_proc_entries(dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002845
Azael Avalos360f0f32014-03-25 20:38:31 -06002846 if (dev->sysfs_created)
2847 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2848 &toshiba_attr_group);
Seth Forshee135740d2011-09-20 16:55:49 -05002849
Seth Forshee29cd2932012-01-18 13:44:09 -06002850 if (dev->ntfy_supported) {
2851 i8042_remove_filter(toshiba_acpi_i8042_filter);
2852 cancel_work_sync(&dev->hotkey_work);
2853 }
2854
Seth Forshee135740d2011-09-20 16:55:49 -05002855 if (dev->hotkey_dev) {
2856 input_unregister_device(dev->hotkey_dev);
2857 sparse_keymap_free(dev->hotkey_dev);
2858 }
2859
Markus Elfring00981812014-11-24 20:30:29 +01002860 backlight_device_unregister(dev->backlight_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002861
Azael Avalosea215a3f2015-07-31 21:58:12 -06002862 if (dev->illumination_led_registered)
Seth Forshee135740d2011-09-20 16:55:49 -05002863 led_classdev_unregister(&dev->led_dev);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002864
Azael Avalos360f0f32014-03-25 20:38:31 -06002865 if (dev->kbd_led_registered)
2866 led_classdev_unregister(&dev->kbd_led);
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002867
Azael Avalosea215a3f2015-07-31 21:58:12 -06002868 if (dev->eco_led_registered)
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002869 led_classdev_unregister(&dev->eco_led);
Seth Forshee135740d2011-09-20 16:55:49 -05002870
Azael Avalos2fdde832015-11-23 10:49:11 -07002871 if (dev->wwan_rfk) {
2872 rfkill_unregister(dev->wwan_rfk);
2873 rfkill_destroy(dev->wwan_rfk);
2874 }
2875
Seth Forshee29cd2932012-01-18 13:44:09 -06002876 if (toshiba_acpi)
2877 toshiba_acpi = NULL;
2878
Seth Forshee135740d2011-09-20 16:55:49 -05002879 kfree(dev);
2880
2881 return 0;
2882}
2883
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002884static const char *find_hci_method(acpi_handle handle)
Seth Forsheea540d6b2011-09-20 16:55:52 -05002885{
Zhang Ruie2e19602013-09-03 08:32:06 +08002886 if (acpi_has_method(handle, "GHCI"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002887 return "GHCI";
2888
Zhang Ruie2e19602013-09-03 08:32:06 +08002889 if (acpi_has_method(handle, "SPFC"))
Seth Forsheea540d6b2011-09-20 16:55:52 -05002890 return "SPFC";
2891
2892 return NULL;
2893}
2894
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -08002895static int toshiba_acpi_add(struct acpi_device *acpi_dev)
Seth Forshee135740d2011-09-20 16:55:49 -05002896{
2897 struct toshiba_acpi_dev *dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002898 const char *hci_method;
Seth Forshee36d03f92011-09-20 16:55:53 -05002899 u32 dummy;
Seth Forshee135740d2011-09-20 16:55:49 -05002900 int ret = 0;
Seth Forshee135740d2011-09-20 16:55:49 -05002901
Seth Forshee29cd2932012-01-18 13:44:09 -06002902 if (toshiba_acpi)
2903 return -EBUSY;
2904
Seth Forshee135740d2011-09-20 16:55:49 -05002905 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2906 TOSHIBA_ACPI_VERSION);
2907
Seth Forsheea540d6b2011-09-20 16:55:52 -05002908 hci_method = find_hci_method(acpi_dev->handle);
2909 if (!hci_method) {
2910 pr_err("HCI interface not found\n");
Seth Forshee6e02cc72011-09-20 16:55:51 -05002911 return -ENODEV;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002912 }
Seth Forshee6e02cc72011-09-20 16:55:51 -05002913
Seth Forshee135740d2011-09-20 16:55:49 -05002914 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2915 if (!dev)
2916 return -ENOMEM;
2917 dev->acpi_dev = acpi_dev;
Seth Forsheea540d6b2011-09-20 16:55:52 -05002918 dev->method_hci = hci_method;
Azael Avalosfc5462f2015-07-22 18:09:11 -06002919 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2920 dev->miscdev.name = "toshiba_acpi";
2921 dev->miscdev.fops = &toshiba_acpi_fops;
2922
2923 ret = misc_register(&dev->miscdev);
2924 if (ret) {
2925 pr_err("Failed to register miscdevice\n");
2926 kfree(dev);
2927 return ret;
2928 }
2929
Seth Forshee135740d2011-09-20 16:55:49 -05002930 acpi_dev->driver_data = dev;
Azael Avalos360f0f32014-03-25 20:38:31 -06002931 dev_set_drvdata(&acpi_dev->dev, dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002932
Azael Avalosa2b34712015-03-20 16:55:17 -06002933 /* Query the BIOS for supported features */
2934
2935 /*
2936 * The "Special Functions" are always supported by the laptops
2937 * with the new keyboard layout, query for its presence to help
2938 * determine the keymap layout to use.
2939 */
Azael Avalosb116fd002015-09-09 11:28:19 -06002940 ret = toshiba_function_keys_get(dev, &dev->special_functions);
Azael Avalosa2b34712015-03-20 16:55:17 -06002941 dev->kbd_function_keys_supported = !ret;
2942
Azael Avalosd2f20612015-11-04 09:28:26 -07002943 dev->hotkey_event_type = 0;
Seth Forshee6e02cc72011-09-20 16:55:51 -05002944 if (toshiba_acpi_setup_keyboard(dev))
2945 pr_info("Unable to activate hotkeys\n");
Seth Forshee135740d2011-09-20 16:55:49 -05002946
Azael Avalos695f6062015-07-22 18:09:13 -06002947 /* Determine whether or not BIOS supports transflective backlight */
2948 ret = get_tr_backlight_status(dev, &dummy);
2949 dev->tr_backlight_supported = !ret;
2950
Seth Forshee62cce752012-04-19 11:23:50 -05002951 ret = toshiba_acpi_setup_backlight(dev);
2952 if (ret)
Seth Forshee135740d2011-09-20 16:55:49 -05002953 goto error;
Seth Forshee135740d2011-09-20 16:55:49 -05002954
Azael Avalosea215a3f2015-07-31 21:58:12 -06002955 toshiba_illumination_available(dev);
2956 if (dev->illumination_supported) {
Seth Forshee135740d2011-09-20 16:55:49 -05002957 dev->led_dev.name = "toshiba::illumination";
2958 dev->led_dev.max_brightness = 1;
2959 dev->led_dev.brightness_set = toshiba_illumination_set;
2960 dev->led_dev.brightness_get = toshiba_illumination_get;
2961 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
Azael Avalosea215a3f2015-07-31 21:58:12 -06002962 dev->illumination_led_registered = true;
Seth Forshee135740d2011-09-20 16:55:49 -05002963 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002964
Azael Avalosea215a3f2015-07-31 21:58:12 -06002965 toshiba_eco_mode_available(dev);
2966 if (dev->eco_supported) {
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002967 dev->eco_led.name = "toshiba::eco_mode";
2968 dev->eco_led.max_brightness = 1;
2969 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2970 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2971 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
Azael Avalosea215a3f2015-07-31 21:58:12 -06002972 dev->eco_led_registered = true;
Azael Avalosdef6c4e2014-03-25 20:38:33 -06002973 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002974
Azael Avalosea215a3f2015-07-31 21:58:12 -06002975 toshiba_kbd_illum_available(dev);
Azael Avalos360f0f32014-03-25 20:38:31 -06002976 /*
2977 * Only register the LED if KBD illumination is supported
2978 * and the keyboard backlight operation mode is set to FN-Z
2979 */
2980 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2981 dev->kbd_led.name = "toshiba::kbd_backlight";
2982 dev->kbd_led.max_brightness = 1;
2983 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2984 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2985 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
Azael Avalosea215a3f2015-07-31 21:58:12 -06002986 dev->kbd_led_registered = true;
Azael Avalos360f0f32014-03-25 20:38:31 -06002987 }
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002988
Azael Avalos9d8658a2014-03-25 20:38:32 -06002989 ret = toshiba_touchpad_get(dev, &dummy);
2990 dev->touchpad_supported = !ret;
Matthew Garrettea6b31f2014-04-04 14:22:34 -04002991
Azael Avalosea215a3f2015-07-31 21:58:12 -06002992 toshiba_accelerometer_available(dev);
Seth Forshee135740d2011-09-20 16:55:49 -05002993
Azael Avalosc8c91842015-04-02 19:26:20 -06002994 toshiba_usb_sleep_charge_available(dev);
Azael Avalose26ffe52015-01-18 18:30:22 -07002995
Azael Avalosbb3fe012015-01-18 18:30:24 -07002996 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2997 dev->usb_rapid_charge_supported = !ret;
2998
Azael Avalos172ce0a2015-01-18 18:30:25 -07002999 ret = toshiba_usb_sleep_music_get(dev, &dummy);
3000 dev->usb_sleep_music_supported = !ret;
3001
Azael Avalos35d53ce2015-02-10 21:09:19 -07003002 ret = toshiba_panel_power_on_get(dev, &dummy);
3003 dev->panel_power_on_supported = !ret;
3004
Azael Avalos17fe4b32015-02-10 21:09:20 -07003005 ret = toshiba_usb_three_get(dev, &dummy);
3006 dev->usb_three_supported = !ret;
3007
Seth Forshee36d03f92011-09-20 16:55:53 -05003008 ret = get_video_status(dev, &dummy);
3009 dev->video_supported = !ret;
3010
3011 ret = get_fan_status(dev, &dummy);
3012 dev->fan_supported = !ret;
3013
Azael Avalos6873f462015-11-23 10:49:10 -07003014 toshiba_wwan_available(dev);
Azael Avalos2fdde832015-11-23 10:49:11 -07003015 if (dev->wwan_supported)
3016 toshiba_acpi_setup_wwan_rfkill(dev);
Azael Avalos6873f462015-11-23 10:49:10 -07003017
Azael Avalos763ff322016-01-25 11:29:10 -07003018 toshiba_cooling_method_available(dev);
3019
Azael Avalos0409cbc2015-07-31 21:58:13 -06003020 print_supported_features(dev);
3021
Azael Avalos360f0f32014-03-25 20:38:31 -06003022 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
3023 &toshiba_attr_group);
3024 if (ret) {
3025 dev->sysfs_created = 0;
3026 goto error;
3027 }
3028 dev->sysfs_created = !ret;
3029
Seth Forshee36d03f92011-09-20 16:55:53 -05003030 create_toshiba_proc_entries(dev);
3031
Seth Forshee29cd2932012-01-18 13:44:09 -06003032 toshiba_acpi = dev;
3033
Seth Forshee135740d2011-09-20 16:55:49 -05003034 return 0;
3035
3036error:
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01003037 toshiba_acpi_remove(acpi_dev);
Seth Forshee135740d2011-09-20 16:55:49 -05003038 return ret;
3039}
3040
3041static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
3042{
3043 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05003044
Azael Avalos71454d72014-12-04 20:22:46 -07003045 switch (event) {
3046 case 0x80: /* Hotkeys and some system events */
Azael Avalosa88bc062015-07-22 18:09:12 -06003047 /*
3048 * Machines with this WMI GUID aren't supported due to bugs in
3049 * their AML.
3050 *
3051 * Return silently to avoid triggering a netlink event.
3052 */
3053 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
3054 return;
Azael Avalos71454d72014-12-04 20:22:46 -07003055 toshiba_acpi_process_hotkeys(dev);
3056 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07003057 case 0x81: /* Dock events */
3058 case 0x82:
3059 case 0x83:
3060 pr_info("Dock event received %x\n", event);
3061 break;
3062 case 0x88: /* Thermal events */
3063 pr_info("Thermal event received\n");
3064 break;
3065 case 0x8f: /* LID closed */
3066 case 0x90: /* LID is closed and Dock has been ejected */
3067 break;
3068 case 0x8c: /* SATA power events */
3069 case 0x8b:
3070 pr_info("SATA power event received %x\n", event);
3071 break;
Azael Avalos80546902014-12-04 20:22:47 -07003072 case 0x92: /* Keyboard backlight mode changed */
Azael Avalos65e3cf92015-11-23 10:51:30 -07003073 toshiba_acpi->kbd_event_generated = true;
Azael Avalos80546902014-12-04 20:22:47 -07003074 /* Update sysfs entries */
Azael Avalos65e3cf92015-11-23 10:51:30 -07003075 if (sysfs_update_group(&acpi_dev->dev.kobj,
3076 &toshiba_attr_group))
Azael Avalos80546902014-12-04 20:22:47 -07003077 pr_err("Unable to update sysfs entries\n");
3078 break;
Azael Avalosbab09e22015-03-06 18:14:41 -07003079 case 0x85: /* Unknown */
3080 case 0x8d: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07003081 case 0x8e: /* Unknown */
Azael Avalosbab09e22015-03-06 18:14:41 -07003082 case 0x94: /* Unknown */
3083 case 0x95: /* Unknown */
Azael Avalos71454d72014-12-04 20:22:46 -07003084 default:
3085 pr_info("Unknown event received %x\n", event);
3086 break;
Seth Forshee29cd2932012-01-18 13:44:09 -06003087 }
Azael Avalosbab09e22015-03-06 18:14:41 -07003088
3089 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
3090 dev_name(&acpi_dev->dev),
Azael Avalos13ae84f2015-11-15 20:33:46 -07003091 event, (event == 0x80) ?
3092 dev->last_key_event : 0);
Matthew Garrett6335e4d2010-02-25 15:20:54 -05003093}
3094
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02003095#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003096static int toshiba_acpi_suspend(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06003097{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003098 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06003099
Azael Avalos1e574db2015-07-22 19:37:49 -06003100 if (dev->hotkey_dev) {
3101 u32 result;
3102
Azael Avalosd37782b2015-05-06 09:35:10 -06003103 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
Azael Avalos1e574db2015-07-22 19:37:49 -06003104 if (result != TOS_SUCCESS)
3105 pr_info("Unable to disable hotkeys\n");
3106 }
Seth Forshee29cd2932012-01-18 13:44:09 -06003107
3108 return 0;
3109}
3110
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003111static int toshiba_acpi_resume(struct device *device)
Seth Forshee29cd2932012-01-18 13:44:09 -06003112{
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003113 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
Seth Forshee29cd2932012-01-18 13:44:09 -06003114
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003115 if (dev->hotkey_dev) {
Azael Avalos2fdde832015-11-23 10:49:11 -07003116 if (toshiba_acpi_enable_hotkeys(dev))
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003117 pr_info("Unable to re-enable hotkeys\n");
Benjamin Tissoirese7fdb762014-09-02 14:04:19 -04003118 }
Seth Forshee29cd2932012-01-18 13:44:09 -06003119
Azael Avalos2fdde832015-11-23 10:49:11 -07003120 if (dev->wwan_rfk) {
3121 if (!toshiba_wireless_status(dev))
3122 rfkill_set_hw_state(dev->wwan_rfk, !dev->killswitch);
3123 }
3124
Seth Forshee29cd2932012-01-18 13:44:09 -06003125 return 0;
3126}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +02003127#endif
Matthew Garrett6335e4d2010-02-25 15:20:54 -05003128
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003129static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
3130 toshiba_acpi_suspend, toshiba_acpi_resume);
3131
Seth Forshee135740d2011-09-20 16:55:49 -05003132static struct acpi_driver toshiba_acpi_driver = {
3133 .name = "Toshiba ACPI driver",
3134 .owner = THIS_MODULE,
3135 .ids = toshiba_device_ids,
3136 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
3137 .ops = {
3138 .add = toshiba_acpi_add,
3139 .remove = toshiba_acpi_remove,
3140 .notify = toshiba_acpi_notify,
3141 },
Rafael J. Wysocki43d2fd32012-06-27 23:27:16 +02003142 .drv.pm = &toshiba_acpi_pm,
Seth Forshee135740d2011-09-20 16:55:49 -05003143};
Holger Machtc9263552006-10-20 14:30:29 -07003144
Len Brown4be44fc2005-08-05 00:44:28 -04003145static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Seth Forshee135740d2011-09-20 16:55:49 -05003147 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
3150 if (!toshiba_proc_dir) {
Seth Forshee135740d2011-09-20 16:55:49 -05003151 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04003152 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
3154
Seth Forshee135740d2011-09-20 16:55:49 -05003155 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
3156 if (ret) {
3157 pr_err("Failed to register ACPI driver: %d\n", ret);
3158 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Holger Machtc9263552006-10-20 14:30:29 -07003159 }
3160
Seth Forshee135740d2011-09-20 16:55:49 -05003161 return ret;
3162}
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04003163
Seth Forshee135740d2011-09-20 16:55:49 -05003164static void __exit toshiba_acpi_exit(void)
3165{
3166 acpi_bus_unregister_driver(&toshiba_acpi_driver);
3167 if (toshiba_proc_dir)
3168 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171module_init(toshiba_acpi_init);
3172module_exit(toshiba_acpi_exit);