]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/platform/x86/toshiba_acpi.c
toshiba_acpi: Add support for USB Sleep functions under battery
[mirror_ubuntu-bionic-kernel.git] / drivers / platform / x86 / toshiba_acpi.c
CommitLineData
1da177e4
LT
1/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
4 *
5 * Copyright (C) 2002-2004 John Belmonte
c41a40c5 6 * Copyright (C) 2008 Philip Langdale
6c3f6e6c 7 * Copyright (C) 2010 Pierre Ducroquet
548c4306 8 * Copyright (C) 2014 Azael Avalos
1da177e4
LT
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 *
25 * The devolpment page for this driver is located at
26 * http://memebeam.org/toys/ToshibaAcpiDriver.
27 *
28 * Credits:
29 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
30 * engineering the Windows drivers
31 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
32 * Rob Miller - TV out and hotkeys help
33 *
34 *
35 * TODO
36 *
37 */
38
7e33460d
JP
39#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
548c4306 41#define TOSHIBA_ACPI_VERSION "0.20"
1da177e4
LT
42#define PROC_INTERFACE_VERSION 1
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/init.h>
47#include <linux/types.h>
48#include <linux/proc_fs.h>
936c8bcd 49#include <linux/seq_file.h>
c9263557 50#include <linux/backlight.h>
c41a40c5 51#include <linux/rfkill.h>
6335e4d5 52#include <linux/input.h>
384a7cd9 53#include <linux/input/sparse-keymap.h>
6c3f6e6c 54#include <linux/leds.h>
5a0e3ad6 55#include <linux/slab.h>
29cd293f
SF
56#include <linux/workqueue.h>
57#include <linux/i8042.h>
8b48463f 58#include <linux/acpi.h>
fe808bfb 59#include <linux/dmi.h>
1da177e4
LT
60#include <asm/uaccess.h>
61
1da177e4
LT
62MODULE_AUTHOR("John Belmonte");
63MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
64MODULE_LICENSE("GPL");
65
f11f999e
SF
66#define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
67
29cd293f
SF
68/* Scan code for Fn key on TOS1900 models */
69#define TOS1900_FN_SCAN 0x6e
70
1da177e4 71/* Toshiba ACPI method paths */
1da177e4
LT
72#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
73
258c5903
AA
74/* The Toshiba configuration interface is composed of the HCI and the SCI,
75 * which are defined as follows:
1da177e4
LT
76 *
77 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
78 * be uniform across all their models. Ideally we would just call
79 * dedicated ACPI methods instead of using this primitive interface.
80 * However the ACPI methods seem to be incomplete in some areas (for
81 * example they allow setting, but not reading, the LCD brightness value),
82 * so this is still useful.
ea6b31f4 83 *
84a6273f
AA
84 * SCI stands for "System Configuration Interface" which aim is to
85 * conceal differences in hardware between different models.
1da177e4
LT
86 */
87
258c5903 88#define TCI_WORDS 6
1da177e4
LT
89
90/* operations */
91#define HCI_SET 0xff00
92#define HCI_GET 0xfe00
84a6273f
AA
93#define SCI_OPEN 0xf100
94#define SCI_CLOSE 0xf200
95#define SCI_GET 0xf300
96#define SCI_SET 0xf400
1da177e4
LT
97
98/* return codes */
1864bbc2
AA
99#define TOS_SUCCESS 0x0000
100#define TOS_OPEN_CLOSE_OK 0x0044
101#define TOS_FAILURE 0x1000
102#define TOS_NOT_SUPPORTED 0x8000
103#define TOS_ALREADY_OPEN 0x8100
104#define TOS_NOT_OPENED 0x8200
105#define TOS_INPUT_DATA_ERROR 0x8300
106#define TOS_WRITE_PROTECTED 0x8400
107#define TOS_NOT_PRESENT 0x8600
108#define TOS_FIFO_EMPTY 0x8c00
109#define TOS_DATA_NOT_AVAILABLE 0x8d20
110#define TOS_NOT_INITIALIZED 0x8d50
1da177e4
LT
111
112/* registers */
113#define HCI_FAN 0x0004
121b7b0d 114#define HCI_TR_BACKLIGHT 0x0005
1da177e4
LT
115#define HCI_SYSTEM_EVENT 0x0016
116#define HCI_VIDEO_OUT 0x001c
117#define HCI_HOTKEY_EVENT 0x001e
118#define HCI_LCD_BRIGHTNESS 0x002a
c41a40c5 119#define HCI_WIRELESS 0x0056
5a2813e9 120#define HCI_ACCELEROMETER 0x006d
360f0f39 121#define HCI_KBD_ILLUMINATION 0x0095
def6c4e2 122#define HCI_ECO_MODE 0x0097
5a2813e9 123#define HCI_ACCELEROMETER2 0x00a6
fdb79081 124#define SCI_ILLUMINATION 0x014e
e26ffe51 125#define SCI_USB_SLEEP_CHARGE 0x0150
360f0f39 126#define SCI_KBD_ILLUM_STATUS 0x015c
9d8658ac 127#define SCI_TOUCHPAD 0x050e
1da177e4
LT
128
129/* field definitions */
5a2813e9 130#define HCI_ACCEL_MASK 0x7fff
29cd293f
SF
131#define HCI_HOTKEY_DISABLE 0x0b
132#define HCI_HOTKEY_ENABLE 0x09
1da177e4
LT
133#define HCI_LCD_BRIGHTNESS_BITS 3
134#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
135#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
360f0f39 136#define HCI_MISC_SHIFT 0x10
1da177e4
LT
137#define HCI_VIDEO_OUT_LCD 0x1
138#define HCI_VIDEO_OUT_CRT 0x2
139#define HCI_VIDEO_OUT_TV 0x4
c41a40c5 140#define HCI_WIRELESS_KILL_SWITCH 0x01
141#define HCI_WIRELESS_BT_PRESENT 0x0f
142#define HCI_WIRELESS_BT_ATTACH 0x40
143#define HCI_WIRELESS_BT_POWER 0x80
93f8c16d 144#define SCI_KBD_MODE_MASK 0x1f
360f0f39
AA
145#define SCI_KBD_MODE_FNZ 0x1
146#define SCI_KBD_MODE_AUTO 0x2
93f8c16d
AA
147#define SCI_KBD_MODE_ON 0x8
148#define SCI_KBD_MODE_OFF 0x10
149#define SCI_KBD_TIME_MAX 0x3c001a
e26ffe51
AA
150#define SCI_USB_CHARGE_MODE_MASK 0xff
151#define SCI_USB_CHARGE_DISABLED 0x30000
152#define SCI_USB_CHARGE_ALTERNATE 0x30009
153#define SCI_USB_CHARGE_AUTO 0x30021
182bcaa5
AA
154#define SCI_USB_CHARGE_BAT_MASK 0x7
155#define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
156#define SCI_USB_CHARGE_BAT_LVL_ON 0x4
157#define SCI_USB_CHARGE_BAT_LVL 0x0200
1da177e4 158
135740de
SF
159struct toshiba_acpi_dev {
160 struct acpi_device *acpi_dev;
161 const char *method_hci;
162 struct rfkill *bt_rfk;
163 struct input_dev *hotkey_dev;
29cd293f 164 struct work_struct hotkey_work;
135740de
SF
165 struct backlight_device *backlight_dev;
166 struct led_classdev led_dev;
360f0f39 167 struct led_classdev kbd_led;
def6c4e2 168 struct led_classdev eco_led;
36d03f93 169
135740de
SF
170 int force_fan;
171 int last_key_event;
172 int key_event_valid;
93f8c16d 173 int kbd_type;
360f0f39
AA
174 int kbd_mode;
175 int kbd_time;
182bcaa5 176 int usbsc_bat_level;
135740de 177
592b746c
DC
178 unsigned int illumination_supported:1;
179 unsigned int video_supported:1;
180 unsigned int fan_supported:1;
181 unsigned int system_event_supported:1;
29cd293f
SF
182 unsigned int ntfy_supported:1;
183 unsigned int info_supported:1;
121b7b0d 184 unsigned int tr_backlight_supported:1;
360f0f39
AA
185 unsigned int kbd_illum_supported:1;
186 unsigned int kbd_led_registered:1;
9d8658ac 187 unsigned int touchpad_supported:1;
def6c4e2 188 unsigned int eco_supported:1;
5a2813e9 189 unsigned int accelerometer_supported:1;
e26ffe51 190 unsigned int usb_sleep_charge_supported:1;
360f0f39 191 unsigned int sysfs_created:1;
36d03f93 192
135740de
SF
193 struct mutex mutex;
194};
195
29cd293f
SF
196static struct toshiba_acpi_dev *toshiba_acpi;
197
4db42c51 198static const struct acpi_device_id toshiba_device_ids[] = {
199 {"TOS6200", 0},
63a9e016 200 {"TOS6207", 0},
c41a40c5 201 {"TOS6208", 0},
4db42c51 202 {"TOS1900", 0},
203 {"", 0},
204};
205MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
206
b859f159 207static const struct key_entry toshiba_acpi_keymap[] = {
fec278a1 208 { KE_KEY, 0x9e, { KEY_RFKILL } },
384a7cd9
DT
209 { KE_KEY, 0x101, { KEY_MUTE } },
210 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
211 { KE_KEY, 0x103, { KEY_ZOOMIN } },
408a5d13 212 { KE_KEY, 0x10f, { KEY_TAB } },
af502837
AA
213 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
214 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
384a7cd9
DT
215 { KE_KEY, 0x13b, { KEY_COFFEE } },
216 { KE_KEY, 0x13c, { KEY_BATTERY } },
217 { KE_KEY, 0x13d, { KEY_SLEEP } },
218 { KE_KEY, 0x13e, { KEY_SUSPEND } },
219 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
220 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
221 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
222 { KE_KEY, 0x142, { KEY_WLAN } },
af502837 223 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
a49010f5 224 { KE_KEY, 0x17f, { KEY_FN } },
384a7cd9
DT
225 { KE_KEY, 0xb05, { KEY_PROG2 } },
226 { KE_KEY, 0xb06, { KEY_WWW } },
227 { KE_KEY, 0xb07, { KEY_MAIL } },
228 { KE_KEY, 0xb30, { KEY_STOP } },
229 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
230 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
231 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
232 { KE_KEY, 0xb5a, { KEY_MEDIA } },
408a5d13
AA
233 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
234 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
235 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
236 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
237 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
384a7cd9 238 { KE_END, 0 },
6335e4d5
MG
239};
240
fe808bfb
TI
241/* alternative keymap */
242static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
243 {
244 .matches = {
245 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
246 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
247 },
248 },
e6efad7f
AA
249 {
250 .matches = {
251 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
252 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
253 },
254 },
b1bde689
AL
255 {
256 .matches = {
257 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
258 DMI_MATCH(DMI_PRODUCT_NAME, "TECRA A50-A"),
259 },
260 },
fe808bfb
TI
261 {}
262};
263
264static const struct key_entry toshiba_acpi_alt_keymap[] = {
265 { KE_KEY, 0x157, { KEY_MUTE } },
266 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
267 { KE_KEY, 0x103, { KEY_ZOOMIN } },
e6efad7f 268 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
fe808bfb
TI
269 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
270 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
271 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
272 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
273 { KE_KEY, 0x158, { KEY_WLAN } },
274 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
275 { KE_END, 0 },
276};
277
1da177e4
LT
278/* utility
279 */
280
4be44fcd 281static __inline__ void _set_bit(u32 * word, u32 mask, int value)
1da177e4
LT
282{
283 *word = (*word & ~mask) | (mask * value);
284}
285
286/* acpi interface wrappers
287 */
288
4be44fcd 289static int write_acpi_int(const char *methodName, int val)
1da177e4 290{
1da177e4
LT
291 acpi_status status;
292
619400da 293 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
32bcd5cb 294 return (status == AE_OK) ? 0 : -EIO;
1da177e4
LT
295}
296
258c5903
AA
297/* Perform a raw configuration call. Here we don't care about input or output
298 * buffer format.
1da177e4 299 */
258c5903
AA
300static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
301 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
1da177e4
LT
302{
303 struct acpi_object_list params;
258c5903 304 union acpi_object in_objs[TCI_WORDS];
1da177e4 305 struct acpi_buffer results;
258c5903 306 union acpi_object out_objs[TCI_WORDS + 1];
1da177e4
LT
307 acpi_status status;
308 int i;
309
258c5903 310 params.count = TCI_WORDS;
1da177e4 311 params.pointer = in_objs;
258c5903 312 for (i = 0; i < TCI_WORDS; ++i) {
1da177e4
LT
313 in_objs[i].type = ACPI_TYPE_INTEGER;
314 in_objs[i].integer.value = in[i];
315 }
316
317 results.length = sizeof(out_objs);
318 results.pointer = out_objs;
319
6e02cc7e
SF
320 status = acpi_evaluate_object(dev->acpi_dev->handle,
321 (char *)dev->method_hci, &params,
4be44fcd 322 &results);
258c5903 323 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
1da177e4
LT
324 for (i = 0; i < out_objs->package.count; ++i) {
325 out[i] = out_objs->package.elements[i].integer.value;
326 }
327 }
328
329 return status;
330}
331
c41a40c5 332/* common hci tasks (get or set one or two value)
1da177e4
LT
333 *
334 * In addition to the ACPI status, the HCI system returns a result which
335 * may be useful (such as "not supported").
336 */
337
893f3f62 338static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
1da177e4 339{
258c5903
AA
340 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
341 u32 out[TCI_WORDS];
342 acpi_status status = tci_raw(dev, in, out);
893f3f62
AA
343
344 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
1da177e4
LT
345}
346
893f3f62 347static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
1da177e4 348{
258c5903
AA
349 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
350 u32 out[TCI_WORDS];
351 acpi_status status = tci_raw(dev, in, out);
893f3f62
AA
352 if (ACPI_FAILURE(status))
353 return TOS_FAILURE;
354
1da177e4 355 *out1 = out[2];
893f3f62
AA
356
357 return out[0];
1da177e4
LT
358}
359
893f3f62 360static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
c41a40c5 361{
258c5903
AA
362 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
363 u32 out[TCI_WORDS];
364 acpi_status status = tci_raw(dev, in, out);
893f3f62
AA
365
366 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
c41a40c5 367}
368
893f3f62 369static u32 hci_read2(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1, u32 *out2)
c41a40c5 370{
258c5903
AA
371 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
372 u32 out[TCI_WORDS];
373 acpi_status status = tci_raw(dev, in, out);
893f3f62
AA
374 if (ACPI_FAILURE(status))
375 return TOS_FAILURE;
376
c41a40c5 377 *out1 = out[2];
378 *out2 = out[3];
893f3f62
AA
379
380 return out[0];
c41a40c5 381}
382
84a6273f
AA
383/* common sci tasks
384 */
385
386static int sci_open(struct toshiba_acpi_dev *dev)
387{
258c5903
AA
388 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
389 u32 out[TCI_WORDS];
84a6273f
AA
390 acpi_status status;
391
258c5903 392 status = tci_raw(dev, in, out);
1864bbc2 393 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
84a6273f
AA
394 pr_err("ACPI call to open SCI failed\n");
395 return 0;
396 }
397
1864bbc2 398 if (out[0] == TOS_OPEN_CLOSE_OK) {
84a6273f 399 return 1;
1864bbc2 400 } else if (out[0] == TOS_ALREADY_OPEN) {
84a6273f
AA
401 pr_info("Toshiba SCI already opened\n");
402 return 1;
1864bbc2 403 } else if (out[0] == TOS_NOT_PRESENT) {
84a6273f
AA
404 pr_info("Toshiba SCI is not present\n");
405 }
406
407 return 0;
408}
409
410static void sci_close(struct toshiba_acpi_dev *dev)
411{
258c5903
AA
412 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
413 u32 out[TCI_WORDS];
84a6273f
AA
414 acpi_status status;
415
258c5903 416 status = tci_raw(dev, in, out);
1864bbc2 417 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
84a6273f
AA
418 pr_err("ACPI call to close SCI failed\n");
419 return;
420 }
421
1864bbc2 422 if (out[0] == TOS_OPEN_CLOSE_OK)
84a6273f 423 return;
1864bbc2 424 else if (out[0] == TOS_NOT_OPENED)
84a6273f 425 pr_info("Toshiba SCI not opened\n");
1864bbc2 426 else if (out[0] == TOS_NOT_PRESENT)
84a6273f
AA
427 pr_info("Toshiba SCI is not present\n");
428}
429
893f3f62 430static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
84a6273f 431{
258c5903
AA
432 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
433 u32 out[TCI_WORDS];
434 acpi_status status = tci_raw(dev, in, out);
893f3f62
AA
435 if (ACPI_FAILURE(status))
436 return TOS_FAILURE;
437
84a6273f 438 *out1 = out[2];
893f3f62
AA
439
440 return out[0];
84a6273f
AA
441}
442
893f3f62 443static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
84a6273f 444{
258c5903
AA
445 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
446 u32 out[TCI_WORDS];
447 acpi_status status = tci_raw(dev, in, out);
893f3f62
AA
448
449 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
84a6273f
AA
450}
451
6c3f6e6c 452/* Illumination support */
135740de 453static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
6c3f6e6c 454{
258c5903
AA
455 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
456 u32 out[TCI_WORDS];
6c3f6e6c
PD
457 acpi_status status;
458
fdb79081
AA
459 if (!sci_open(dev))
460 return 0;
461
258c5903 462 status = tci_raw(dev, in, out);
fdb79081 463 sci_close(dev);
1864bbc2 464 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
fdb79081
AA
465 pr_err("ACPI call to query Illumination support failed\n");
466 return 0;
1864bbc2 467 } else if (out[0] == TOS_NOT_SUPPORTED) {
7e33460d 468 pr_info("Illumination device not available\n");
6c3f6e6c
PD
469 return 0;
470 }
fdb79081 471
6c3f6e6c
PD
472 return 1;
473}
474
475static void toshiba_illumination_set(struct led_classdev *cdev,
476 enum led_brightness brightness)
477{
135740de
SF
478 struct toshiba_acpi_dev *dev = container_of(cdev,
479 struct toshiba_acpi_dev, led_dev);
fdb79081 480 u32 state, result;
6c3f6e6c
PD
481
482 /* First request : initialize communication. */
fdb79081 483 if (!sci_open(dev))
6c3f6e6c 484 return;
6c3f6e6c 485
fdb79081
AA
486 /* Switch the illumination on/off */
487 state = brightness ? 1 : 0;
893f3f62 488 result = sci_write(dev, SCI_ILLUMINATION, state);
fdb79081 489 sci_close(dev);
893f3f62 490 if (result == TOS_FAILURE) {
fdb79081
AA
491 pr_err("ACPI call for illumination failed\n");
492 return;
1864bbc2 493 } else if (result == TOS_NOT_SUPPORTED) {
fdb79081
AA
494 pr_info("Illumination not supported\n");
495 return;
6c3f6e6c 496 }
6c3f6e6c
PD
497}
498
499static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
500{
135740de
SF
501 struct toshiba_acpi_dev *dev = container_of(cdev,
502 struct toshiba_acpi_dev, led_dev);
fdb79081 503 u32 state, result;
6c3f6e6c
PD
504
505 /* First request : initialize communication. */
fdb79081 506 if (!sci_open(dev))
6c3f6e6c 507 return LED_OFF;
6c3f6e6c
PD
508
509 /* Check the illumination */
893f3f62 510 result = sci_read(dev, SCI_ILLUMINATION, &state);
fdb79081 511 sci_close(dev);
893f3f62 512 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
fdb79081
AA
513 pr_err("ACPI call for illumination failed\n");
514 return LED_OFF;
1864bbc2 515 } else if (result == TOS_NOT_SUPPORTED) {
fdb79081 516 pr_info("Illumination not supported\n");
6c3f6e6c
PD
517 return LED_OFF;
518 }
519
fdb79081 520 return state ? LED_FULL : LED_OFF;
6c3f6e6c 521}
ea6b31f4 522
360f0f39 523/* KBD Illumination */
93f8c16d
AA
524static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
525{
258c5903
AA
526 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
527 u32 out[TCI_WORDS];
93f8c16d
AA
528 acpi_status status;
529
530 if (!sci_open(dev))
531 return 0;
532
258c5903 533 status = tci_raw(dev, in, out);
93f8c16d 534 sci_close(dev);
1864bbc2 535 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
93f8c16d
AA
536 pr_err("ACPI call to query kbd illumination support failed\n");
537 return 0;
1864bbc2 538 } else if (out[0] == TOS_NOT_SUPPORTED) {
93f8c16d
AA
539 pr_info("Keyboard illumination not available\n");
540 return 0;
541 }
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
557 return 1;
558}
559
360f0f39
AA
560static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
561{
562 u32 result;
360f0f39
AA
563
564 if (!sci_open(dev))
565 return -EIO;
566
893f3f62 567 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
360f0f39 568 sci_close(dev);
893f3f62 569 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
360f0f39
AA
570 pr_err("ACPI call to set KBD backlight status failed\n");
571 return -EIO;
1864bbc2 572 } else if (result == TOS_NOT_SUPPORTED) {
360f0f39
AA
573 pr_info("Keyboard backlight status not supported\n");
574 return -ENODEV;
575 }
576
577 return 0;
578}
579
580static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
581{
582 u32 result;
360f0f39
AA
583
584 if (!sci_open(dev))
585 return -EIO;
586
893f3f62 587 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
360f0f39 588 sci_close(dev);
893f3f62 589 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
360f0f39
AA
590 pr_err("ACPI call to get KBD backlight status failed\n");
591 return -EIO;
1864bbc2 592 } else if (result == TOS_NOT_SUPPORTED) {
360f0f39
AA
593 pr_info("Keyboard backlight status not supported\n");
594 return -ENODEV;
595 }
596
597 return 0;
598}
599
600static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
601{
602 struct toshiba_acpi_dev *dev = container_of(cdev,
603 struct toshiba_acpi_dev, kbd_led);
604 u32 state, result;
360f0f39
AA
605
606 /* Check the keyboard backlight state */
893f3f62
AA
607 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
608 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
360f0f39
AA
609 pr_err("ACPI call to get the keyboard backlight failed\n");
610 return LED_OFF;
1864bbc2 611 } else if (result == TOS_NOT_SUPPORTED) {
360f0f39
AA
612 pr_info("Keyboard backlight not supported\n");
613 return LED_OFF;
614 }
615
616 return state ? LED_FULL : LED_OFF;
617}
618
619static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
620 enum led_brightness brightness)
621{
622 struct toshiba_acpi_dev *dev = container_of(cdev,
623 struct toshiba_acpi_dev, kbd_led);
624 u32 state, result;
360f0f39
AA
625
626 /* Set the keyboard backlight state */
627 state = brightness ? 1 : 0;
893f3f62
AA
628 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
629 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
360f0f39
AA
630 pr_err("ACPI call to set KBD Illumination mode failed\n");
631 return;
1864bbc2 632 } else if (result == TOS_NOT_SUPPORTED) {
360f0f39
AA
633 pr_info("Keyboard backlight not supported\n");
634 return;
635 }
636}
ea6b31f4 637
9d8658ac
AA
638/* TouchPad support */
639static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
640{
641 u32 result;
9d8658ac
AA
642
643 if (!sci_open(dev))
644 return -EIO;
645
893f3f62 646 result = sci_write(dev, SCI_TOUCHPAD, state);
9d8658ac 647 sci_close(dev);
893f3f62 648 if (result == TOS_FAILURE) {
9d8658ac
AA
649 pr_err("ACPI call to set the touchpad failed\n");
650 return -EIO;
1864bbc2 651 } else if (result == TOS_NOT_SUPPORTED) {
9d8658ac
AA
652 return -ENODEV;
653 }
654
655 return 0;
656}
657
658static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
659{
660 u32 result;
9d8658ac
AA
661
662 if (!sci_open(dev))
663 return -EIO;
664
893f3f62 665 result = sci_read(dev, SCI_TOUCHPAD, state);
9d8658ac 666 sci_close(dev);
893f3f62 667 if (result == TOS_FAILURE) {
9d8658ac
AA
668 pr_err("ACPI call to query the touchpad failed\n");
669 return -EIO;
1864bbc2 670 } else if (result == TOS_NOT_SUPPORTED) {
9d8658ac
AA
671 return -ENODEV;
672 }
673
674 return 0;
675}
6c3f6e6c 676
def6c4e2
AA
677/* Eco Mode support */
678static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
679{
680 acpi_status status;
258c5903
AA
681 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
682 u32 out[TCI_WORDS];
def6c4e2 683
258c5903 684 status = tci_raw(dev, in, out);
1864bbc2 685 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
def6c4e2
AA
686 pr_info("ACPI call to get ECO led failed\n");
687 return 0;
688 }
689
690 return 1;
691}
692
693static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
694{
695 struct toshiba_acpi_dev *dev = container_of(cdev,
696 struct toshiba_acpi_dev, eco_led);
258c5903
AA
697 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
698 u32 out[TCI_WORDS];
def6c4e2
AA
699 acpi_status status;
700
258c5903 701 status = tci_raw(dev, in, out);
1864bbc2 702 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
def6c4e2
AA
703 pr_err("ACPI call to get ECO led failed\n");
704 return LED_OFF;
705 }
706
707 return out[2] ? LED_FULL : LED_OFF;
708}
709
710static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
711 enum led_brightness brightness)
712{
713 struct toshiba_acpi_dev *dev = container_of(cdev,
714 struct toshiba_acpi_dev, eco_led);
258c5903
AA
715 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
716 u32 out[TCI_WORDS];
def6c4e2
AA
717 acpi_status status;
718
719 /* Switch the Eco Mode led on/off */
720 in[2] = (brightness) ? 1 : 0;
258c5903 721 status = tci_raw(dev, in, out);
1864bbc2 722 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
def6c4e2
AA
723 pr_err("ACPI call to set ECO led failed\n");
724 return;
725 }
726}
ea6b31f4 727
5a2813e9
AA
728/* Accelerometer support */
729static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
730{
258c5903
AA
731 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
732 u32 out[TCI_WORDS];
5a2813e9
AA
733 acpi_status status;
734
735 /* Check if the accelerometer call exists,
736 * this call also serves as initialization
737 */
258c5903 738 status = tci_raw(dev, in, out);
1864bbc2 739 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
5a2813e9
AA
740 pr_err("ACPI call to query the accelerometer failed\n");
741 return -EIO;
1864bbc2
AA
742 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
743 out[0] == TOS_NOT_INITIALIZED) {
5a2813e9
AA
744 pr_err("Accelerometer not initialized\n");
745 return -EIO;
1864bbc2 746 } else if (out[0] == TOS_NOT_SUPPORTED) {
5a2813e9
AA
747 pr_info("Accelerometer not supported\n");
748 return -ENODEV;
749 }
750
751 return 0;
752}
753
754static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
755 u32 *xy, u32 *z)
756{
258c5903
AA
757 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
758 u32 out[TCI_WORDS];
5a2813e9
AA
759 acpi_status status;
760
761 /* Check the Accelerometer status */
258c5903 762 status = tci_raw(dev, in, out);
1864bbc2 763 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
5a2813e9
AA
764 pr_err("ACPI call to query the accelerometer failed\n");
765 return -EIO;
766 }
767
768 *xy = out[2];
769 *z = out[4];
770
771 return 0;
772}
def6c4e2 773
e26ffe51
AA
774/* Sleep (Charge and Music) utilities support */
775static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
776 u32 *mode)
777{
778 u32 result;
779
780 if (!sci_open(dev))
781 return -EIO;
782
783 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
784 sci_close(dev);
785 if (result == TOS_FAILURE) {
786 pr_err("ACPI call to set USB S&C mode failed\n");
787 return -EIO;
788 } else if (result == TOS_NOT_SUPPORTED) {
789 pr_info("USB Sleep and Charge not supported\n");
790 return -ENODEV;
791 } else if (result == TOS_INPUT_DATA_ERROR) {
792 return -EIO;
793 }
794
795 return 0;
796}
797
798static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
799 u32 mode)
800{
801 u32 result;
802
803 if (!sci_open(dev))
804 return -EIO;
805
806 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
807 sci_close(dev);
808 if (result == TOS_FAILURE) {
809 pr_err("ACPI call to set USB S&C mode failed\n");
810 return -EIO;
811 } else if (result == TOS_NOT_SUPPORTED) {
812 pr_info("USB Sleep and Charge not supported\n");
813 return -ENODEV;
814 } else if (result == TOS_INPUT_DATA_ERROR) {
815 return -EIO;
816 }
817
818 return 0;
819}
820
182bcaa5
AA
821static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
822 u32 *mode)
823{
824 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
825 u32 out[TCI_WORDS];
826 acpi_status status;
827
828 if (!sci_open(dev))
829 return -EIO;
830
831 in[5] = SCI_USB_CHARGE_BAT_LVL;
832 status = tci_raw(dev, in, out);
833 sci_close(dev);
834 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
835 pr_err("ACPI call to get USB S&C battery level failed\n");
836 return -EIO;
837 } else if (out[0] == TOS_NOT_SUPPORTED) {
838 pr_info("USB Sleep and Charge not supported\n");
839 return -ENODEV;
840 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
841 return -EIO;
842 }
843
844 *mode = out[2];
845
846 return 0;
847}
848
849static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
850 u32 mode)
851{
852 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
853 u32 out[TCI_WORDS];
854 acpi_status status;
855
856 if (!sci_open(dev))
857 return -EIO;
858
859 in[2] = mode;
860 in[5] = SCI_USB_CHARGE_BAT_LVL;
861 status = tci_raw(dev, in, out);
862 sci_close(dev);
863 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
864 pr_err("ACPI call to set USB S&C battery level failed\n");
865 return -EIO;
866 } else if (out[0] == TOS_NOT_SUPPORTED) {
867 pr_info("USB Sleep and Charge not supported\n");
868 return -ENODEV;
869 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
870 return -EIO;
871 }
872
873 return 0;
874}
875
c41a40c5 876/* Bluetooth rfkill handlers */
877
135740de 878static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
c41a40c5 879{
880 u32 hci_result;
881 u32 value, value2;
882
883 value = 0;
884 value2 = 0;
893f3f62 885 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
1864bbc2 886 if (hci_result == TOS_SUCCESS)
c41a40c5 887 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
888
889 return hci_result;
890}
891
135740de 892static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
c41a40c5 893{
894 u32 hci_result;
895 u32 value, value2;
896
897 value = 0;
898 value2 = 0x0001;
893f3f62 899 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
c41a40c5 900
901 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
902 return hci_result;
903}
904
19d337df 905static int bt_rfkill_set_block(void *data, bool blocked)
c41a40c5 906{
19d337df 907 struct toshiba_acpi_dev *dev = data;
c41a40c5 908 u32 result1, result2;
909 u32 value;
19d337df 910 int err;
c41a40c5 911 bool radio_state;
c41a40c5 912
19d337df 913 value = (blocked == false);
c41a40c5 914
19d337df 915 mutex_lock(&dev->mutex);
1864bbc2 916 if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
32bcd5cb 917 err = -EIO;
19d337df
JB
918 goto out;
919 }
c41a40c5 920
19d337df
JB
921 if (!radio_state) {
922 err = 0;
923 goto out;
c41a40c5 924 }
925
893f3f62
AA
926 result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
927 result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
c41a40c5 928
1864bbc2 929 if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
32bcd5cb 930 err = -EIO;
19d337df
JB
931 else
932 err = 0;
933 out:
934 mutex_unlock(&dev->mutex);
935 return err;
c41a40c5 936}
937
19d337df 938static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
c41a40c5 939{
c41a40c5 940 bool new_rfk_state;
941 bool value;
942 u32 hci_result;
19d337df
JB
943 struct toshiba_acpi_dev *dev = data;
944
945 mutex_lock(&dev->mutex);
c41a40c5 946
135740de 947 hci_result = hci_get_radio_state(dev, &value);
1864bbc2 948 if (hci_result != TOS_SUCCESS) {
19d337df
JB
949 /* Can't do anything useful */
950 mutex_unlock(&dev->mutex);
82e7784f 951 return;
19d337df 952 }
c41a40c5 953
954 new_rfk_state = value;
955
c41a40c5 956 mutex_unlock(&dev->mutex);
957
19d337df
JB
958 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
959 bt_rfkill_set_block(data, true);
c41a40c5 960}
961
19d337df
JB
962static const struct rfkill_ops toshiba_rfk_ops = {
963 .set_block = bt_rfkill_set_block,
964 .poll = bt_rfkill_poll,
965};
966
121b7b0d
AI
967static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
968{
969 u32 hci_result;
970 u32 status;
971
893f3f62 972 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
121b7b0d 973 *enabled = !status;
1864bbc2 974 return hci_result == TOS_SUCCESS ? 0 : -EIO;
121b7b0d
AI
975}
976
977static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
978{
979 u32 hci_result;
980 u32 value = !enable;
981
893f3f62 982 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
1864bbc2 983 return hci_result == TOS_SUCCESS ? 0 : -EIO;
121b7b0d
AI
984}
985
4be44fcd 986static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
1da177e4 987
62cce752 988static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
1da177e4
LT
989{
990 u32 hci_result;
991 u32 value;
121b7b0d
AI
992 int brightness = 0;
993
994 if (dev->tr_backlight_supported) {
995 bool enabled;
996 int ret = get_tr_backlight_status(dev, &enabled);
997 if (ret)
998 return ret;
999 if (enabled)
1000 return 0;
1001 brightness++;
1002 }
1da177e4 1003
893f3f62 1004 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
1864bbc2 1005 if (hci_result == TOS_SUCCESS)
121b7b0d 1006 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
32bcd5cb
SF
1007
1008 return -EIO;
c9263557
HM
1009}
1010
62cce752
SF
1011static int get_lcd_brightness(struct backlight_device *bd)
1012{
1013 struct toshiba_acpi_dev *dev = bl_get_data(bd);
1014 return __get_lcd_brightness(dev);
1015}
1016
936c8bcd 1017static int lcd_proc_show(struct seq_file *m, void *v)
c9263557 1018{
135740de
SF
1019 struct toshiba_acpi_dev *dev = m->private;
1020 int value;
121b7b0d 1021 int levels;
135740de
SF
1022
1023 if (!dev->backlight_dev)
1024 return -ENODEV;
c9263557 1025
121b7b0d 1026 levels = dev->backlight_dev->props.max_brightness + 1;
62cce752 1027 value = get_lcd_brightness(dev->backlight_dev);
c9263557 1028 if (value >= 0) {
936c8bcd 1029 seq_printf(m, "brightness: %d\n", value);
121b7b0d 1030 seq_printf(m, "brightness_levels: %d\n", levels);
32bcd5cb 1031 return 0;
1da177e4
LT
1032 }
1033
32bcd5cb
SF
1034 pr_err("Error reading LCD brightness\n");
1035 return -EIO;
936c8bcd
AD
1036}
1037
1038static int lcd_proc_open(struct inode *inode, struct file *file)
1039{
d9dda78b 1040 return single_open(file, lcd_proc_show, PDE_DATA(inode));
1da177e4
LT
1041}
1042
62cce752 1043static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
c9263557 1044{
a39f46df 1045 u32 hci_result;
c9263557 1046
121b7b0d
AI
1047 if (dev->tr_backlight_supported) {
1048 bool enable = !value;
1049 int ret = set_tr_backlight_status(dev, enable);
1050 if (ret)
1051 return ret;
1052 if (value)
1053 value--;
1054 }
1055
a39f46df
AA
1056 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1057 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1058 return hci_result == TOS_SUCCESS ? 0 : -EIO;
c9263557
HM
1059}
1060
1061static int set_lcd_status(struct backlight_device *bd)
1062{
135740de 1063 struct toshiba_acpi_dev *dev = bl_get_data(bd);
62cce752 1064 return set_lcd_brightness(dev, bd->props.brightness);
c9263557
HM
1065}
1066
936c8bcd
AD
1067static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1068 size_t count, loff_t *pos)
1da177e4 1069{
d9dda78b 1070 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
936c8bcd
AD
1071 char cmd[42];
1072 size_t len;
1da177e4 1073 int value;
c8af57eb 1074 int ret;
121b7b0d 1075 int levels = dev->backlight_dev->props.max_brightness + 1;
1da177e4 1076
936c8bcd
AD
1077 len = min(count, sizeof(cmd) - 1);
1078 if (copy_from_user(cmd, buf, len))
1079 return -EFAULT;
1080 cmd[len] = '\0';
1081
1082 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
121b7b0d 1083 value >= 0 && value < levels) {
62cce752 1084 ret = set_lcd_brightness(dev, value);
c8af57eb
MO
1085 if (ret == 0)
1086 ret = count;
1087 } else {
c9263557 1088 ret = -EINVAL;
c8af57eb 1089 }
c9263557 1090 return ret;
1da177e4
LT
1091}
1092
936c8bcd
AD
1093static const struct file_operations lcd_proc_fops = {
1094 .owner = THIS_MODULE,
1095 .open = lcd_proc_open,
1096 .read = seq_read,
1097 .llseek = seq_lseek,
1098 .release = single_release,
1099 .write = lcd_proc_write,
1100};
1101
36d03f93
SF
1102static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1103{
1104 u32 hci_result;
1105
893f3f62 1106 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
1864bbc2 1107 return hci_result == TOS_SUCCESS ? 0 : -EIO;
36d03f93
SF
1108}
1109
936c8bcd 1110static int video_proc_show(struct seq_file *m, void *v)
1da177e4 1111{
135740de 1112 struct toshiba_acpi_dev *dev = m->private;
1da177e4 1113 u32 value;
36d03f93 1114 int ret;
1da177e4 1115
36d03f93
SF
1116 ret = get_video_status(dev, &value);
1117 if (!ret) {
1da177e4
LT
1118 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1119 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
4be44fcd 1120 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
936c8bcd
AD
1121 seq_printf(m, "lcd_out: %d\n", is_lcd);
1122 seq_printf(m, "crt_out: %d\n", is_crt);
1123 seq_printf(m, "tv_out: %d\n", is_tv);
1da177e4
LT
1124 }
1125
36d03f93 1126 return ret;
1da177e4
LT
1127}
1128
936c8bcd 1129static int video_proc_open(struct inode *inode, struct file *file)
1da177e4 1130{
d9dda78b 1131 return single_open(file, video_proc_show, PDE_DATA(inode));
936c8bcd
AD
1132}
1133
1134static ssize_t video_proc_write(struct file *file, const char __user *buf,
1135 size_t count, loff_t *pos)
1136{
d9dda78b 1137 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
936c8bcd 1138 char *cmd, *buffer;
36d03f93 1139 int ret;
1da177e4
LT
1140 int value;
1141 int remain = count;
1142 int lcd_out = -1;
1143 int crt_out = -1;
1144 int tv_out = -1;
b4482a4b 1145 u32 video_out;
1da177e4 1146
936c8bcd
AD
1147 cmd = kmalloc(count + 1, GFP_KERNEL);
1148 if (!cmd)
1149 return -ENOMEM;
1150 if (copy_from_user(cmd, buf, count)) {
1151 kfree(cmd);
1152 return -EFAULT;
1153 }
1154 cmd[count] = '\0';
1155
1156 buffer = cmd;
1157
1da177e4
LT
1158 /* scan expression. Multiple expressions may be delimited with ;
1159 *
1160 * NOTE: to keep scanning simple, invalid fields are ignored
1161 */
1162 while (remain) {
1163 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1164 lcd_out = value & 1;
1165 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1166 crt_out = value & 1;
1167 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1168 tv_out = value & 1;
1169 /* advance to one character past the next ; */
1170 do {
1171 ++buffer;
1172 --remain;
1173 }
4be44fcd 1174 while (remain && *(buffer - 1) != ';');
1da177e4
LT
1175 }
1176
936c8bcd
AD
1177 kfree(cmd);
1178
36d03f93
SF
1179 ret = get_video_status(dev, &video_out);
1180 if (!ret) {
9e113e00 1181 unsigned int new_video_out = video_out;
1da177e4
LT
1182 if (lcd_out != -1)
1183 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1184 if (crt_out != -1)
1185 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1186 if (tv_out != -1)
1187 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1188 /* To avoid unnecessary video disruption, only write the new
1189 * video setting if something changed. */
1190 if (new_video_out != video_out)
32bcd5cb 1191 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1da177e4
LT
1192 }
1193
32bcd5cb 1194 return ret ? ret : count;
1da177e4
LT
1195}
1196
936c8bcd
AD
1197static const struct file_operations video_proc_fops = {
1198 .owner = THIS_MODULE,
1199 .open = video_proc_open,
1200 .read = seq_read,
1201 .llseek = seq_lseek,
1202 .release = single_release,
1203 .write = video_proc_write,
1204};
1205
36d03f93
SF
1206static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1207{
1208 u32 hci_result;
1209
893f3f62 1210 hci_result = hci_read1(dev, HCI_FAN, status);
1864bbc2 1211 return hci_result == TOS_SUCCESS ? 0 : -EIO;
36d03f93
SF
1212}
1213
936c8bcd 1214static int fan_proc_show(struct seq_file *m, void *v)
1da177e4 1215{
135740de 1216 struct toshiba_acpi_dev *dev = m->private;
36d03f93 1217 int ret;
1da177e4
LT
1218 u32 value;
1219
36d03f93
SF
1220 ret = get_fan_status(dev, &value);
1221 if (!ret) {
936c8bcd 1222 seq_printf(m, "running: %d\n", (value > 0));
135740de 1223 seq_printf(m, "force_on: %d\n", dev->force_fan);
1da177e4
LT
1224 }
1225
36d03f93 1226 return ret;
936c8bcd
AD
1227}
1228
1229static int fan_proc_open(struct inode *inode, struct file *file)
1230{
d9dda78b 1231 return single_open(file, fan_proc_show, PDE_DATA(inode));
1da177e4
LT
1232}
1233
936c8bcd
AD
1234static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1235 size_t count, loff_t *pos)
1da177e4 1236{
d9dda78b 1237 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
936c8bcd
AD
1238 char cmd[42];
1239 size_t len;
1da177e4
LT
1240 int value;
1241 u32 hci_result;
1242
936c8bcd
AD
1243 len = min(count, sizeof(cmd) - 1);
1244 if (copy_from_user(cmd, buf, len))
1245 return -EFAULT;
1246 cmd[len] = '\0';
1247
1248 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
4be44fcd 1249 value >= 0 && value <= 1) {
893f3f62 1250 hci_result = hci_write1(dev, HCI_FAN, value);
1864bbc2 1251 if (hci_result != TOS_SUCCESS)
32bcd5cb 1252 return -EIO;
1da177e4 1253 else
135740de 1254 dev->force_fan = value;
1da177e4
LT
1255 } else {
1256 return -EINVAL;
1257 }
1258
1259 return count;
1260}
1261
936c8bcd
AD
1262static const struct file_operations fan_proc_fops = {
1263 .owner = THIS_MODULE,
1264 .open = fan_proc_open,
1265 .read = seq_read,
1266 .llseek = seq_lseek,
1267 .release = single_release,
1268 .write = fan_proc_write,
1269};
1270
1271static int keys_proc_show(struct seq_file *m, void *v)
1da177e4 1272{
135740de 1273 struct toshiba_acpi_dev *dev = m->private;
1da177e4
LT
1274 u32 hci_result;
1275 u32 value;
1276
11948b93 1277 if (!dev->key_event_valid && dev->system_event_supported) {
893f3f62 1278 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
1864bbc2 1279 if (hci_result == TOS_SUCCESS) {
135740de
SF
1280 dev->key_event_valid = 1;
1281 dev->last_key_event = value;
1864bbc2 1282 } else if (hci_result == TOS_FIFO_EMPTY) {
1da177e4 1283 /* better luck next time */
1864bbc2 1284 } else if (hci_result == TOS_NOT_SUPPORTED) {
1da177e4
LT
1285 /* This is a workaround for an unresolved issue on
1286 * some machines where system events sporadically
1287 * become disabled. */
893f3f62 1288 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
7e33460d 1289 pr_notice("Re-enabled hotkeys\n");
1da177e4 1290 } else {
7e33460d 1291 pr_err("Error reading hotkey status\n");
32bcd5cb 1292 return -EIO;
1da177e4
LT
1293 }
1294 }
1295
135740de
SF
1296 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1297 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
936c8bcd
AD
1298 return 0;
1299}
1da177e4 1300
936c8bcd
AD
1301static int keys_proc_open(struct inode *inode, struct file *file)
1302{
d9dda78b 1303 return single_open(file, keys_proc_show, PDE_DATA(inode));
1da177e4
LT
1304}
1305
936c8bcd
AD
1306static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1307 size_t count, loff_t *pos)
1da177e4 1308{
d9dda78b 1309 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
936c8bcd
AD
1310 char cmd[42];
1311 size_t len;
1da177e4
LT
1312 int value;
1313
936c8bcd
AD
1314 len = min(count, sizeof(cmd) - 1);
1315 if (copy_from_user(cmd, buf, len))
1316 return -EFAULT;
1317 cmd[len] = '\0';
1318
1319 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
135740de 1320 dev->key_event_valid = 0;
1da177e4
LT
1321 } else {
1322 return -EINVAL;
1323 }
1324
1325 return count;
1326}
1327
936c8bcd
AD
1328static const struct file_operations keys_proc_fops = {
1329 .owner = THIS_MODULE,
1330 .open = keys_proc_open,
1331 .read = seq_read,
1332 .llseek = seq_lseek,
1333 .release = single_release,
1334 .write = keys_proc_write,
1335};
1336
1337static int version_proc_show(struct seq_file *m, void *v)
1da177e4 1338{
936c8bcd
AD
1339 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1340 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1341 return 0;
1da177e4
LT
1342}
1343
936c8bcd
AD
1344static int version_proc_open(struct inode *inode, struct file *file)
1345{
d9dda78b 1346 return single_open(file, version_proc_show, PDE_DATA(inode));
936c8bcd
AD
1347}
1348
1349static const struct file_operations version_proc_fops = {
1350 .owner = THIS_MODULE,
1351 .open = version_proc_open,
1352 .read = seq_read,
1353 .llseek = seq_lseek,
1354 .release = single_release,
1355};
1356
1da177e4
LT
1357/* proc and module init
1358 */
1359
1360#define PROC_TOSHIBA "toshiba"
1361
b859f159 1362static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1da177e4 1363{
36d03f93
SF
1364 if (dev->backlight_dev)
1365 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1366 &lcd_proc_fops, dev);
1367 if (dev->video_supported)
1368 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1369 &video_proc_fops, dev);
1370 if (dev->fan_supported)
1371 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1372 &fan_proc_fops, dev);
1373 if (dev->hotkey_dev)
1374 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1375 &keys_proc_fops, dev);
135740de
SF
1376 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1377 &version_proc_fops, dev);
1da177e4
LT
1378}
1379
36d03f93 1380static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1da177e4 1381{
36d03f93
SF
1382 if (dev->backlight_dev)
1383 remove_proc_entry("lcd", toshiba_proc_dir);
1384 if (dev->video_supported)
1385 remove_proc_entry("video", toshiba_proc_dir);
1386 if (dev->fan_supported)
1387 remove_proc_entry("fan", toshiba_proc_dir);
1388 if (dev->hotkey_dev)
1389 remove_proc_entry("keys", toshiba_proc_dir);
936c8bcd 1390 remove_proc_entry("version", toshiba_proc_dir);
1da177e4
LT
1391}
1392
acc2472e 1393static const struct backlight_ops toshiba_backlight_data = {
121b7b0d 1394 .options = BL_CORE_SUSPENDRESUME,
62cce752
SF
1395 .get_brightness = get_lcd_brightness,
1396 .update_status = set_lcd_status,
c9263557 1397};
ea6b31f4 1398
360f0f39
AA
1399/*
1400 * Sysfs files
1401 */
93f8c16d
AA
1402static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1403 struct device_attribute *attr,
1404 const char *buf, size_t count);
1405static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1406 struct device_attribute *attr,
1407 char *buf);
1408static ssize_t toshiba_kbd_type_show(struct device *dev,
1409 struct device_attribute *attr,
1410 char *buf);
1411static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1412 struct device_attribute *attr,
1413 char *buf);
1414static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1415 struct device_attribute *attr,
1416 const char *buf, size_t count);
1417static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1418 struct device_attribute *attr,
1419 char *buf);
1420static ssize_t toshiba_touchpad_store(struct device *dev,
1421 struct device_attribute *attr,
1422 const char *buf, size_t count);
1423static ssize_t toshiba_touchpad_show(struct device *dev,
1424 struct device_attribute *attr,
1425 char *buf);
1426static ssize_t toshiba_position_show(struct device *dev,
1427 struct device_attribute *attr,
1428 char *buf);
e26ffe51
AA
1429static ssize_t toshiba_usb_sleep_charge_show(struct device *dev,
1430 struct device_attribute *attr,
1431 char *buf);
1432static ssize_t toshiba_usb_sleep_charge_store(struct device *dev,
1433 struct device_attribute *attr,
1434 const char *buf, size_t count);
182bcaa5
AA
1435static ssize_t sleep_functions_on_battery_show(struct device *dev,
1436 struct device_attribute *attr,
1437 char *buf);
1438static ssize_t sleep_functions_on_battery_store(struct device *dev,
1439 struct device_attribute *attr,
1440 const char *buf, size_t count);
93f8c16d
AA
1441
1442static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1443 toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1444static DEVICE_ATTR(kbd_type, S_IRUGO, toshiba_kbd_type_show, NULL);
1445static DEVICE_ATTR(available_kbd_modes, S_IRUGO,
1446 toshiba_available_kbd_modes_show, NULL);
1447static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1448 toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
1449static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1450 toshiba_touchpad_show, toshiba_touchpad_store);
1451static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
e26ffe51
AA
1452static DEVICE_ATTR(usb_sleep_charge, S_IRUGO | S_IWUSR,
1453 toshiba_usb_sleep_charge_show,
1454 toshiba_usb_sleep_charge_store);
182bcaa5
AA
1455static DEVICE_ATTR(sleep_functions_on_battery, S_IRUGO | S_IWUSR,
1456 sleep_functions_on_battery_show,
1457 sleep_functions_on_battery_store);
93f8c16d
AA
1458
1459static struct attribute *toshiba_attributes[] = {
1460 &dev_attr_kbd_backlight_mode.attr,
1461 &dev_attr_kbd_type.attr,
1462 &dev_attr_available_kbd_modes.attr,
1463 &dev_attr_kbd_backlight_timeout.attr,
1464 &dev_attr_touchpad.attr,
1465 &dev_attr_position.attr,
e26ffe51 1466 &dev_attr_usb_sleep_charge.attr,
182bcaa5 1467 &dev_attr_sleep_functions_on_battery.attr,
93f8c16d
AA
1468 NULL,
1469};
1470
1471static umode_t toshiba_sysfs_is_visible(struct kobject *,
1472 struct attribute *, int);
1473
1474static struct attribute_group toshiba_attr_group = {
1475 .is_visible = toshiba_sysfs_is_visible,
1476 .attrs = toshiba_attributes,
1477};
360f0f39
AA
1478
1479static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1480 struct device_attribute *attr,
1481 const char *buf, size_t count)
1482{
1483 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
aeaac098
DC
1484 int mode;
1485 int time;
1486 int ret;
1487
360f0f39 1488
aeaac098
DC
1489 ret = kstrtoint(buf, 0, &mode);
1490 if (ret)
1491 return ret;
93f8c16d
AA
1492
1493 /* Check for supported modes depending on keyboard backlight type */
1494 if (toshiba->kbd_type == 1) {
1495 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1496 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1497 return -EINVAL;
1498 } else if (toshiba->kbd_type == 2) {
1499 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1500 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1501 mode != SCI_KBD_MODE_OFF)
1502 return -EINVAL;
1503 }
360f0f39
AA
1504
1505 /* Set the Keyboard Backlight Mode where:
360f0f39
AA
1506 * Auto - KBD backlight turns off automatically in given time
1507 * FN-Z - KBD backlight "toggles" when hotkey pressed
93f8c16d
AA
1508 * ON - KBD backlight is always on
1509 * OFF - KBD backlight is always off
360f0f39 1510 */
93f8c16d
AA
1511
1512 /* Only make a change if the actual mode has changed */
aeaac098 1513 if (toshiba->kbd_mode != mode) {
93f8c16d 1514 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
360f0f39 1515 time = toshiba->kbd_time << HCI_MISC_SHIFT;
93f8c16d
AA
1516
1517 /* OR the "base time" to the actual method format */
1518 if (toshiba->kbd_type == 1) {
1519 /* Type 1 requires the current mode */
1520 time |= toshiba->kbd_mode;
1521 } else if (toshiba->kbd_type == 2) {
1522 /* Type 2 requires the desired mode */
1523 time |= mode;
1524 }
1525
aeaac098
DC
1526 ret = toshiba_kbd_illum_status_set(toshiba, time);
1527 if (ret)
1528 return ret;
93f8c16d 1529
360f0f39
AA
1530 toshiba->kbd_mode = mode;
1531 }
1532
1533 return count;
1534}
1535
1536static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1537 struct device_attribute *attr,
1538 char *buf)
1539{
1540 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1541 u32 time;
1542
1543 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1544 return -EIO;
1545
93f8c16d
AA
1546 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1547}
1548
1549static ssize_t toshiba_kbd_type_show(struct device *dev,
1550 struct device_attribute *attr,
1551 char *buf)
1552{
1553 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1554
1555 return sprintf(buf, "%d\n", toshiba->kbd_type);
1556}
1557
1558static ssize_t toshiba_available_kbd_modes_show(struct device *dev,
1559 struct device_attribute *attr,
1560 char *buf)
1561{
1562 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1563
1564 if (toshiba->kbd_type == 1)
1565 return sprintf(buf, "%x %x\n",
1566 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1567
1568 return sprintf(buf, "%x %x %x\n",
1569 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
360f0f39
AA
1570}
1571
1572static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1573 struct device_attribute *attr,
1574 const char *buf, size_t count)
1575{
1576 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
eabde0fa
AA
1577 int time;
1578 int ret;
360f0f39 1579
eabde0fa
AA
1580 ret = kstrtoint(buf, 0, &time);
1581 if (ret)
1582 return ret;
1583
1584 /* Check for supported values depending on kbd_type */
1585 if (toshiba->kbd_type == 1) {
1586 if (time < 0 || time > 60)
1587 return -EINVAL;
1588 } else if (toshiba->kbd_type == 2) {
1589 if (time < 1 || time > 60)
1590 return -EINVAL;
1591 }
1592
1593 /* Set the Keyboard Backlight Timeout */
360f0f39 1594
eabde0fa
AA
1595 /* Only make a change if the actual timeout has changed */
1596 if (toshiba->kbd_time != time) {
1597 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
360f0f39 1598 time = time << HCI_MISC_SHIFT;
eabde0fa
AA
1599 /* OR the "base time" to the actual method format */
1600 if (toshiba->kbd_type == 1)
1601 time |= SCI_KBD_MODE_FNZ;
1602 else if (toshiba->kbd_type == 2)
1603 time |= SCI_KBD_MODE_AUTO;
1604
1605 ret = toshiba_kbd_illum_status_set(toshiba, time);
1606 if (ret)
1607 return ret;
1608
360f0f39
AA
1609 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1610 }
1611
1612 return count;
1613}
1614
1615static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1616 struct device_attribute *attr,
1617 char *buf)
1618{
1619 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1620 u32 time;
1621
1622 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1623 return -EIO;
1624
1625 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1626}
ea6b31f4 1627
9d8658ac
AA
1628static ssize_t toshiba_touchpad_store(struct device *dev,
1629 struct device_attribute *attr,
1630 const char *buf, size_t count)
1631{
1632 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1633 int state;
c8a41669 1634 int ret;
9d8658ac
AA
1635
1636 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
c8a41669
AA
1637 ret = kstrtoint(buf, 0, &state);
1638 if (ret)
1639 return ret;
1640 if (state != 0 && state != 1)
1641 return -EINVAL;
1642
1643 ret = toshiba_touchpad_set(toshiba, state);
1644 if (ret)
1645 return ret;
9d8658ac
AA
1646
1647 return count;
1648}
1649
1650static ssize_t toshiba_touchpad_show(struct device *dev,
1651 struct device_attribute *attr, char *buf)
1652{
1653 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1654 u32 state;
1655 int ret;
1656
1657 ret = toshiba_touchpad_get(toshiba, &state);
1658 if (ret < 0)
1659 return ret;
1660
1661 return sprintf(buf, "%i\n", state);
1662}
ea6b31f4 1663
5a2813e9
AA
1664static ssize_t toshiba_position_show(struct device *dev,
1665 struct device_attribute *attr, char *buf)
1666{
1667 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1668 u32 xyval, zval, tmp;
1669 u16 x, y, z;
1670 int ret;
1671
1672 xyval = zval = 0;
1673 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1674 if (ret < 0)
1675 return ret;
1676
1677 x = xyval & HCI_ACCEL_MASK;
1678 tmp = xyval >> HCI_MISC_SHIFT;
1679 y = tmp & HCI_ACCEL_MASK;
1680 z = zval & HCI_ACCEL_MASK;
1681
1682 return sprintf(buf, "%d %d %d\n", x, y, z);
1683}
360f0f39 1684
e26ffe51
AA
1685static ssize_t toshiba_usb_sleep_charge_show(struct device *dev,
1686 struct device_attribute *attr,
1687 char *buf)
1688{
1689 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1690 u32 mode;
1691 int ret;
1692
1693 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1694 if (ret < 0)
1695 return ret;
1696
1697 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1698}
1699
1700static ssize_t toshiba_usb_sleep_charge_store(struct device *dev,
1701 struct device_attribute *attr,
1702 const char *buf, size_t count)
1703{
1704 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1705 u32 mode;
1706 int state;
1707 int ret;
1708
1709 ret = kstrtoint(buf, 0, &state);
1710 if (ret)
1711 return ret;
1712 /* Check for supported values, where:
1713 * 0 - Disabled
1714 * 1 - Alternate (Non USB conformant devices that require more power)
1715 * 2 - Auto (USB conformant devices)
1716 */
1717 if (state != 0 && state != 1 && state != 2)
1718 return -EINVAL;
1719
1720 /* Set the USB charging mode to internal value */
1721 if (state == 0)
1722 mode = SCI_USB_CHARGE_DISABLED;
1723 else if (state == 1)
1724 mode = SCI_USB_CHARGE_ALTERNATE;
1725 else if (state == 2)
1726 mode = SCI_USB_CHARGE_AUTO;
1727
1728 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1729 if (ret)
1730 return ret;
1731
1732 return count;
1733}
1734
182bcaa5
AA
1735static ssize_t sleep_functions_on_battery_show(struct device *dev,
1736 struct device_attribute *attr,
1737 char *buf)
1738{
1739 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1740 u32 state;
1741 int bat_lvl;
1742 int status;
1743 int ret;
1744 int tmp;
1745
1746 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1747 if (ret < 0)
1748 return ret;
1749
1750 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1751 tmp = state & SCI_USB_CHARGE_BAT_MASK;
1752 status = (tmp == 0x4) ? 1 : 0;
1753 /* Determine the battery level set */
1754 bat_lvl = state >> HCI_MISC_SHIFT;
1755
1756 return sprintf(buf, "%d %d\n", status, bat_lvl);
1757}
1758
1759static ssize_t sleep_functions_on_battery_store(struct device *dev,
1760 struct device_attribute *attr,
1761 const char *buf, size_t count)
1762{
1763 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1764 u32 status;
1765 int value;
1766 int ret;
1767 int tmp;
1768
1769 ret = kstrtoint(buf, 0, &value);
1770 if (ret)
1771 return ret;
1772
1773 /* Set the status of the function:
1774 * 0 - Disabled
1775 * 1-100 - Enabled
1776 */
1777 if (value < 0 || value > 100)
1778 return -EINVAL;
1779
1780 if (value == 0) {
1781 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
1782 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
1783 } else {
1784 tmp = value << HCI_MISC_SHIFT;
1785 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
1786 }
1787 ret = toshiba_sleep_functions_status_set(toshiba, status);
1788 if (ret < 0)
1789 return ret;
1790
1791 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
1792
1793 return count;
1794}
1795
360f0f39
AA
1796static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
1797 struct attribute *attr, int idx)
1798{
1799 struct device *dev = container_of(kobj, struct device, kobj);
1800 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
1801 bool exists = true;
1802
1803 if (attr == &dev_attr_kbd_backlight_mode.attr)
1804 exists = (drv->kbd_illum_supported) ? true : false;
1805 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
1806 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
9d8658ac
AA
1807 else if (attr == &dev_attr_touchpad.attr)
1808 exists = (drv->touchpad_supported) ? true : false;
5a2813e9
AA
1809 else if (attr == &dev_attr_position.attr)
1810 exists = (drv->accelerometer_supported) ? true : false;
e26ffe51
AA
1811 else if (attr == &dev_attr_usb_sleep_charge.attr)
1812 exists = (drv->usb_sleep_charge_supported) ? true : false;
182bcaa5
AA
1813 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
1814 exists = (drv->usb_sleep_charge_supported) ? true : false;
360f0f39
AA
1815
1816 return exists ? attr->mode : 0;
1817}
1818
1f28f290
AA
1819/*
1820 * Hotkeys
1821 */
1822static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
1823{
1824 acpi_status status;
1825 u32 result;
1826
1827 status = acpi_evaluate_object(dev->acpi_dev->handle,
1828 "ENAB", NULL, NULL);
1829 if (ACPI_FAILURE(status))
1830 return -ENODEV;
1831
1832 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
1833 if (result == TOS_FAILURE)
1834 return -EIO;
1835 else if (result == TOS_NOT_SUPPORTED)
1836 return -ENODEV;
1837
1838 return 0;
1839}
1840
29cd293f
SF
1841static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
1842 struct serio *port)
1843{
98280374 1844 if (str & I8042_STR_AUXDATA)
29cd293f
SF
1845 return false;
1846
1847 if (unlikely(data == 0xe0))
1848 return false;
1849
1850 if ((data & 0x7f) == TOS1900_FN_SCAN) {
1851 schedule_work(&toshiba_acpi->hotkey_work);
1852 return true;
1853 }
1854
1855 return false;
1856}
1857
1858static void toshiba_acpi_hotkey_work(struct work_struct *work)
1859{
1860 acpi_handle ec_handle = ec_get_handle();
1861 acpi_status status;
1862
1863 if (!ec_handle)
1864 return;
1865
1866 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
1867 if (ACPI_FAILURE(status))
1868 pr_err("ACPI NTFY method execution failed\n");
1869}
1870
1871/*
1872 * Returns hotkey scancode, or < 0 on failure.
1873 */
1874static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
1875{
74facaf7 1876 unsigned long long value;
29cd293f
SF
1877 acpi_status status;
1878
74facaf7
ZR
1879 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
1880 NULL, &value);
1881 if (ACPI_FAILURE(status)) {
29cd293f
SF
1882 pr_err("ACPI INFO method execution failed\n");
1883 return -EIO;
1884 }
1885
74facaf7 1886 return value;
29cd293f
SF
1887}
1888
1889static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
1890 int scancode)
1891{
1892 if (scancode == 0x100)
1893 return;
1894
1895 /* act on key press; ignore key release */
1896 if (scancode & 0x80)
1897 return;
1898
1899 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
1900 pr_info("Unknown key %x\n", scancode);
1901}
1902
71454d78
AA
1903static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
1904{
1905 u32 hci_result, value;
1906 int retries = 3;
1907 int scancode;
1908
1909 if (dev->info_supported) {
1910 scancode = toshiba_acpi_query_hotkey(dev);
1911 if (scancode < 0)
1912 pr_err("Failed to query hotkey event\n");
1913 else if (scancode != 0)
1914 toshiba_acpi_report_hotkey(dev, scancode);
1915 } else if (dev->system_event_supported) {
1916 do {
1917 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
1918 switch (hci_result) {
1919 case TOS_SUCCESS:
1920 toshiba_acpi_report_hotkey(dev, (int)value);
1921 break;
1922 case TOS_NOT_SUPPORTED:
1923 /*
1924 * This is a workaround for an unresolved
1925 * issue on some machines where system events
1926 * sporadically become disabled.
1927 */
1928 hci_result =
1929 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
1930 pr_notice("Re-enabled hotkeys\n");
1931 /* fall through */
1932 default:
1933 retries--;
1934 break;
1935 }
1936 } while (retries && hci_result != TOS_FIFO_EMPTY);
1937 }
1938}
1939
b859f159 1940static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
6335e4d5 1941{
e2e19606 1942 acpi_handle ec_handle;
384a7cd9 1943 int error;
29cd293f 1944 u32 hci_result;
fe808bfb 1945 const struct key_entry *keymap = toshiba_acpi_keymap;
6335e4d5 1946
135740de 1947 dev->hotkey_dev = input_allocate_device();
b222cca6 1948 if (!dev->hotkey_dev)
6335e4d5 1949 return -ENOMEM;
6335e4d5 1950
135740de 1951 dev->hotkey_dev->name = "Toshiba input device";
6e02cc7e 1952 dev->hotkey_dev->phys = "toshiba_acpi/input0";
135740de 1953 dev->hotkey_dev->id.bustype = BUS_HOST;
6335e4d5 1954
fe808bfb
TI
1955 if (dmi_check_system(toshiba_alt_keymap_dmi))
1956 keymap = toshiba_acpi_alt_keymap;
1957 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
384a7cd9
DT
1958 if (error)
1959 goto err_free_dev;
1960
29cd293f
SF
1961 /*
1962 * For some machines the SCI responsible for providing hotkey
1963 * notification doesn't fire. We can trigger the notification
1964 * whenever the Fn key is pressed using the NTFY method, if
1965 * supported, so if it's present set up an i8042 key filter
1966 * for this purpose.
1967 */
29cd293f 1968 ec_handle = ec_get_handle();
e2e19606 1969 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
29cd293f
SF
1970 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
1971
1972 error = i8042_install_filter(toshiba_acpi_i8042_filter);
1973 if (error) {
1974 pr_err("Error installing key filter\n");
1975 goto err_free_keymap;
1976 }
1977
1978 dev->ntfy_supported = 1;
1979 }
1980
1981 /*
1982 * Determine hotkey query interface. Prefer using the INFO
1983 * method when it is available.
1984 */
e2e19606 1985 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
29cd293f 1986 dev->info_supported = 1;
e2e19606 1987 else {
893f3f62 1988 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
1864bbc2 1989 if (hci_result == TOS_SUCCESS)
29cd293f
SF
1990 dev->system_event_supported = 1;
1991 }
1992
1993 if (!dev->info_supported && !dev->system_event_supported) {
1994 pr_warn("No hotkey query interface found\n");
1995 goto err_remove_filter;
1996 }
1997
1f28f290
AA
1998 error = toshiba_acpi_enable_hotkeys(dev);
1999 if (error) {
7e33460d 2000 pr_info("Unable to enable hotkeys\n");
29cd293f 2001 goto err_remove_filter;
6335e4d5
MG
2002 }
2003
135740de 2004 error = input_register_device(dev->hotkey_dev);
384a7cd9 2005 if (error) {
7e33460d 2006 pr_info("Unable to register input device\n");
29cd293f 2007 goto err_remove_filter;
6335e4d5
MG
2008 }
2009
2010 return 0;
384a7cd9 2011
29cd293f
SF
2012 err_remove_filter:
2013 if (dev->ntfy_supported)
2014 i8042_remove_filter(toshiba_acpi_i8042_filter);
384a7cd9 2015 err_free_keymap:
135740de 2016 sparse_keymap_free(dev->hotkey_dev);
384a7cd9 2017 err_free_dev:
135740de
SF
2018 input_free_device(dev->hotkey_dev);
2019 dev->hotkey_dev = NULL;
384a7cd9 2020 return error;
6335e4d5
MG
2021}
2022
b859f159 2023static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
62cce752
SF
2024{
2025 struct backlight_properties props;
2026 int brightness;
2027 int ret;
121b7b0d 2028 bool enabled;
62cce752
SF
2029
2030 /*
2031 * Some machines don't support the backlight methods at all, and
2032 * others support it read-only. Either of these is pretty useless,
2033 * so only register the backlight device if the backlight method
2034 * supports both reads and writes.
2035 */
2036 brightness = __get_lcd_brightness(dev);
2037 if (brightness < 0)
2038 return 0;
2039 ret = set_lcd_brightness(dev, brightness);
2040 if (ret) {
2041 pr_debug("Backlight method is read-only, disabling backlight support\n");
2042 return 0;
2043 }
2044
121b7b0d
AI
2045 /* Determine whether or not BIOS supports transflective backlight */
2046 ret = get_tr_backlight_status(dev, &enabled);
2047 dev->tr_backlight_supported = !ret;
2048
53039f22 2049 memset(&props, 0, sizeof(props));
62cce752
SF
2050 props.type = BACKLIGHT_PLATFORM;
2051 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
62cce752 2052
121b7b0d
AI
2053 /* adding an extra level and having 0 change to transflective mode */
2054 if (dev->tr_backlight_supported)
2055 props.max_brightness++;
2056
62cce752
SF
2057 dev->backlight_dev = backlight_device_register("toshiba",
2058 &dev->acpi_dev->dev,
2059 dev,
2060 &toshiba_backlight_data,
2061 &props);
2062 if (IS_ERR(dev->backlight_dev)) {
2063 ret = PTR_ERR(dev->backlight_dev);
2064 pr_err("Could not register toshiba backlight device\n");
2065 dev->backlight_dev = NULL;
2066 return ret;
2067 }
2068
2069 dev->backlight_dev->props.brightness = brightness;
2070 return 0;
2071}
2072
51fac838 2073static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
c9263557 2074{
135740de 2075 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
6335e4d5 2076
36d03f93 2077 remove_toshiba_proc_entries(dev);
ea6b31f4 2078
360f0f39
AA
2079 if (dev->sysfs_created)
2080 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2081 &toshiba_attr_group);
c41a40c5 2082
29cd293f
SF
2083 if (dev->ntfy_supported) {
2084 i8042_remove_filter(toshiba_acpi_i8042_filter);
2085 cancel_work_sync(&dev->hotkey_work);
2086 }
2087
135740de
SF
2088 if (dev->hotkey_dev) {
2089 input_unregister_device(dev->hotkey_dev);
2090 sparse_keymap_free(dev->hotkey_dev);
2091 }
c9263557 2092
135740de
SF
2093 if (dev->bt_rfk) {
2094 rfkill_unregister(dev->bt_rfk);
2095 rfkill_destroy(dev->bt_rfk);
2096 }
c9263557 2097
00981810 2098 backlight_device_unregister(dev->backlight_dev);
c9263557 2099
36d03f93 2100 if (dev->illumination_supported)
135740de 2101 led_classdev_unregister(&dev->led_dev);
ea6b31f4 2102
360f0f39
AA
2103 if (dev->kbd_led_registered)
2104 led_classdev_unregister(&dev->kbd_led);
ea6b31f4 2105
def6c4e2
AA
2106 if (dev->eco_supported)
2107 led_classdev_unregister(&dev->eco_led);
6c3f6e6c 2108
29cd293f
SF
2109 if (toshiba_acpi)
2110 toshiba_acpi = NULL;
2111
135740de 2112 kfree(dev);
c41a40c5 2113
135740de 2114 return 0;
c9263557
HM
2115}
2116
b859f159 2117static const char *find_hci_method(acpi_handle handle)
a540d6b5 2118{
e2e19606 2119 if (acpi_has_method(handle, "GHCI"))
a540d6b5
SF
2120 return "GHCI";
2121
e2e19606 2122 if (acpi_has_method(handle, "SPFC"))
a540d6b5
SF
2123 return "SPFC";
2124
2125 return NULL;
2126}
2127
b859f159 2128static int toshiba_acpi_add(struct acpi_device *acpi_dev)
1da177e4 2129{
135740de 2130 struct toshiba_acpi_dev *dev;
a540d6b5 2131 const char *hci_method;
36d03f93 2132 u32 dummy;
c41a40c5 2133 bool bt_present;
c41a40c5 2134 int ret = 0;
1da177e4 2135
29cd293f
SF
2136 if (toshiba_acpi)
2137 return -EBUSY;
2138
135740de
SF
2139 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2140 TOSHIBA_ACPI_VERSION);
2141
a540d6b5
SF
2142 hci_method = find_hci_method(acpi_dev->handle);
2143 if (!hci_method) {
2144 pr_err("HCI interface not found\n");
6e02cc7e 2145 return -ENODEV;
a540d6b5 2146 }
6e02cc7e 2147
135740de
SF
2148 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2149 if (!dev)
2150 return -ENOMEM;
2151 dev->acpi_dev = acpi_dev;
a540d6b5 2152 dev->method_hci = hci_method;
135740de 2153 acpi_dev->driver_data = dev;
360f0f39 2154 dev_set_drvdata(&acpi_dev->dev, dev);
fb9802fa 2155
6e02cc7e
SF
2156 if (toshiba_acpi_setup_keyboard(dev))
2157 pr_info("Unable to activate hotkeys\n");
135740de
SF
2158
2159 mutex_init(&dev->mutex);
1da177e4 2160
62cce752
SF
2161 ret = toshiba_acpi_setup_backlight(dev);
2162 if (ret)
135740de 2163 goto error;
1da177e4 2164
c41a40c5 2165 /* Register rfkill switch for Bluetooth */
1864bbc2 2166 if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
135740de
SF
2167 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2168 &acpi_dev->dev,
2169 RFKILL_TYPE_BLUETOOTH,
2170 &toshiba_rfk_ops,
2171 dev);
2172 if (!dev->bt_rfk) {
7e33460d 2173 pr_err("unable to allocate rfkill device\n");
135740de
SF
2174 ret = -ENOMEM;
2175 goto error;
c41a40c5 2176 }
2177
135740de 2178 ret = rfkill_register(dev->bt_rfk);
c41a40c5 2179 if (ret) {
7e33460d 2180 pr_err("unable to register rfkill device\n");
135740de
SF
2181 rfkill_destroy(dev->bt_rfk);
2182 goto error;
38aefbc5 2183 }
c41a40c5 2184 }
2185
135740de
SF
2186 if (toshiba_illumination_available(dev)) {
2187 dev->led_dev.name = "toshiba::illumination";
2188 dev->led_dev.max_brightness = 1;
2189 dev->led_dev.brightness_set = toshiba_illumination_set;
2190 dev->led_dev.brightness_get = toshiba_illumination_get;
2191 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
36d03f93 2192 dev->illumination_supported = 1;
6c3f6e6c 2193 }
ea6b31f4 2194
def6c4e2
AA
2195 if (toshiba_eco_mode_available(dev)) {
2196 dev->eco_led.name = "toshiba::eco_mode";
2197 dev->eco_led.max_brightness = 1;
2198 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2199 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2200 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2201 dev->eco_supported = 1;
2202 }
ea6b31f4 2203
93f8c16d 2204 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
360f0f39
AA
2205 /*
2206 * Only register the LED if KBD illumination is supported
2207 * and the keyboard backlight operation mode is set to FN-Z
2208 */
2209 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2210 dev->kbd_led.name = "toshiba::kbd_backlight";
2211 dev->kbd_led.max_brightness = 1;
2212 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2213 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2214 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2215 dev->kbd_led_registered = 1;
2216 }
ea6b31f4 2217
9d8658ac
AA
2218 ret = toshiba_touchpad_get(dev, &dummy);
2219 dev->touchpad_supported = !ret;
ea6b31f4 2220
5a2813e9
AA
2221 ret = toshiba_accelerometer_supported(dev);
2222 dev->accelerometer_supported = !ret;
6c3f6e6c 2223
e26ffe51
AA
2224 ret = toshiba_usb_sleep_charge_get(dev, &dummy);
2225 dev->usb_sleep_charge_supported = !ret;
2226
36d03f93
SF
2227 /* Determine whether or not BIOS supports fan and video interfaces */
2228
2229 ret = get_video_status(dev, &dummy);
2230 dev->video_supported = !ret;
2231
2232 ret = get_fan_status(dev, &dummy);
2233 dev->fan_supported = !ret;
2234
360f0f39
AA
2235 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2236 &toshiba_attr_group);
2237 if (ret) {
2238 dev->sysfs_created = 0;
2239 goto error;
2240 }
2241 dev->sysfs_created = !ret;
2242
36d03f93
SF
2243 create_toshiba_proc_entries(dev);
2244
29cd293f
SF
2245 toshiba_acpi = dev;
2246
c41a40c5 2247 return 0;
135740de
SF
2248
2249error:
51fac838 2250 toshiba_acpi_remove(acpi_dev);
135740de
SF
2251 return ret;
2252}
2253
2254static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2255{
2256 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
80546905 2257 int ret;
135740de 2258
71454d78
AA
2259 switch (event) {
2260 case 0x80: /* Hotkeys and some system events */
2261 toshiba_acpi_process_hotkeys(dev);
2262 break;
80546905
AA
2263 case 0x92: /* Keyboard backlight mode changed */
2264 /* Update sysfs entries */
2265 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2266 &toshiba_attr_group);
2267 if (ret)
2268 pr_err("Unable to update sysfs entries\n");
2269 break;
71454d78
AA
2270 case 0x81: /* Unknown */
2271 case 0x82: /* Unknown */
2272 case 0x83: /* Unknown */
2273 case 0x8c: /* Unknown */
2274 case 0x8e: /* Unknown */
2275 case 0x8f: /* Unknown */
2276 case 0x90: /* Unknown */
2277 default:
2278 pr_info("Unknown event received %x\n", event);
2279 break;
29cd293f 2280 }
135740de
SF
2281}
2282
3567a4e2 2283#ifdef CONFIG_PM_SLEEP
43d2fd3b 2284static int toshiba_acpi_suspend(struct device *device)
29cd293f 2285{
43d2fd3b 2286 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
29cd293f
SF
2287 u32 result;
2288
2289 if (dev->hotkey_dev)
893f3f62 2290 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
29cd293f
SF
2291
2292 return 0;
2293}
2294
43d2fd3b 2295static int toshiba_acpi_resume(struct device *device)
29cd293f 2296{
43d2fd3b 2297 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
1f28f290 2298 int error;
e7fdb762
BT
2299
2300 if (dev->hotkey_dev) {
1f28f290
AA
2301 error = toshiba_acpi_enable_hotkeys(dev);
2302 if (error)
e7fdb762 2303 pr_info("Unable to re-enable hotkeys\n");
e7fdb762 2304 }
29cd293f
SF
2305
2306 return 0;
2307}
3567a4e2 2308#endif
135740de 2309
43d2fd3b
RW
2310static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2311 toshiba_acpi_suspend, toshiba_acpi_resume);
2312
135740de
SF
2313static struct acpi_driver toshiba_acpi_driver = {
2314 .name = "Toshiba ACPI driver",
2315 .owner = THIS_MODULE,
2316 .ids = toshiba_device_ids,
2317 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2318 .ops = {
2319 .add = toshiba_acpi_add,
2320 .remove = toshiba_acpi_remove,
2321 .notify = toshiba_acpi_notify,
2322 },
43d2fd3b 2323 .drv.pm = &toshiba_acpi_pm,
135740de
SF
2324};
2325
2326static int __init toshiba_acpi_init(void)
2327{
2328 int ret;
2329
f11f999e
SF
2330 /*
2331 * Machines with this WMI guid aren't supported due to bugs in
2332 * their AML. This check relies on wmi initializing before
2333 * toshiba_acpi to guarantee guids have been identified.
2334 */
2335 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2336 return -ENODEV;
2337
135740de
SF
2338 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2339 if (!toshiba_proc_dir) {
2340 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
2341 return -ENODEV;
2342 }
2343
2344 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2345 if (ret) {
2346 pr_err("Failed to register ACPI driver: %d\n", ret);
2347 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
2348 }
2349
2350 return ret;
2351}
2352
2353static void __exit toshiba_acpi_exit(void)
2354{
2355 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2356 if (toshiba_proc_dir)
2357 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1da177e4
LT
2358}
2359
2360module_init(toshiba_acpi_init);
2361module_exit(toshiba_acpi_exit);