]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/platform/x86/toshiba_acpi.c
toshiba_acpi: Fix illumination not available on certain models
[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
74/* Toshiba HCI interface definitions
75 *
76 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
77 * be uniform across all their models. Ideally we would just call
78 * dedicated ACPI methods instead of using this primitive interface.
79 * However the ACPI methods seem to be incomplete in some areas (for
80 * example they allow setting, but not reading, the LCD brightness value),
81 * so this is still useful.
ea6b31f4 82 *
84a6273f
AA
83 * SCI stands for "System Configuration Interface" which aim is to
84 * conceal differences in hardware between different models.
1da177e4
LT
85 */
86
87#define HCI_WORDS 6
88
89/* operations */
90#define HCI_SET 0xff00
91#define HCI_GET 0xfe00
84a6273f
AA
92#define SCI_OPEN 0xf100
93#define SCI_CLOSE 0xf200
94#define SCI_GET 0xf300
95#define SCI_SET 0xf400
1da177e4
LT
96
97/* return codes */
98#define HCI_SUCCESS 0x0000
99#define HCI_FAILURE 0x1000
100#define HCI_NOT_SUPPORTED 0x8000
101#define HCI_EMPTY 0x8c00
5a2813e9
AA
102#define HCI_DATA_NOT_AVAILABLE 0x8d20
103#define HCI_NOT_INITIALIZED 0x8d50
84a6273f
AA
104#define SCI_OPEN_CLOSE_OK 0x0044
105#define SCI_ALREADY_OPEN 0x8100
106#define SCI_NOT_OPENED 0x8200
fdb79081 107#define SCI_INPUT_DATA_ERROR 0x8300
84a6273f 108#define SCI_NOT_PRESENT 0x8600
1da177e4
LT
109
110/* registers */
111#define HCI_FAN 0x0004
121b7b0d 112#define HCI_TR_BACKLIGHT 0x0005
1da177e4
LT
113#define HCI_SYSTEM_EVENT 0x0016
114#define HCI_VIDEO_OUT 0x001c
115#define HCI_HOTKEY_EVENT 0x001e
116#define HCI_LCD_BRIGHTNESS 0x002a
c41a40c5 117#define HCI_WIRELESS 0x0056
5a2813e9 118#define HCI_ACCELEROMETER 0x006d
360f0f39 119#define HCI_KBD_ILLUMINATION 0x0095
def6c4e2 120#define HCI_ECO_MODE 0x0097
5a2813e9 121#define HCI_ACCELEROMETER2 0x00a6
fdb79081 122#define SCI_ILLUMINATION 0x014e
360f0f39 123#define SCI_KBD_ILLUM_STATUS 0x015c
9d8658ac 124#define SCI_TOUCHPAD 0x050e
1da177e4
LT
125
126/* field definitions */
5a2813e9 127#define HCI_ACCEL_MASK 0x7fff
29cd293f
SF
128#define HCI_HOTKEY_DISABLE 0x0b
129#define HCI_HOTKEY_ENABLE 0x09
1da177e4
LT
130#define HCI_LCD_BRIGHTNESS_BITS 3
131#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
132#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
360f0f39 133#define HCI_MISC_SHIFT 0x10
1da177e4
LT
134#define HCI_VIDEO_OUT_LCD 0x1
135#define HCI_VIDEO_OUT_CRT 0x2
136#define HCI_VIDEO_OUT_TV 0x4
c41a40c5 137#define HCI_WIRELESS_KILL_SWITCH 0x01
138#define HCI_WIRELESS_BT_PRESENT 0x0f
139#define HCI_WIRELESS_BT_ATTACH 0x40
140#define HCI_WIRELESS_BT_POWER 0x80
360f0f39
AA
141#define SCI_KBD_MODE_FNZ 0x1
142#define SCI_KBD_MODE_AUTO 0x2
1da177e4 143
135740de
SF
144struct toshiba_acpi_dev {
145 struct acpi_device *acpi_dev;
146 const char *method_hci;
147 struct rfkill *bt_rfk;
148 struct input_dev *hotkey_dev;
29cd293f 149 struct work_struct hotkey_work;
135740de
SF
150 struct backlight_device *backlight_dev;
151 struct led_classdev led_dev;
360f0f39 152 struct led_classdev kbd_led;
def6c4e2 153 struct led_classdev eco_led;
36d03f93 154
135740de
SF
155 int force_fan;
156 int last_key_event;
157 int key_event_valid;
360f0f39
AA
158 int kbd_mode;
159 int kbd_time;
135740de 160
592b746c
DC
161 unsigned int illumination_supported:1;
162 unsigned int video_supported:1;
163 unsigned int fan_supported:1;
164 unsigned int system_event_supported:1;
29cd293f
SF
165 unsigned int ntfy_supported:1;
166 unsigned int info_supported:1;
121b7b0d 167 unsigned int tr_backlight_supported:1;
360f0f39
AA
168 unsigned int kbd_illum_supported:1;
169 unsigned int kbd_led_registered:1;
9d8658ac 170 unsigned int touchpad_supported:1;
def6c4e2 171 unsigned int eco_supported:1;
5a2813e9 172 unsigned int accelerometer_supported:1;
360f0f39 173 unsigned int sysfs_created:1;
36d03f93 174
135740de
SF
175 struct mutex mutex;
176};
177
29cd293f
SF
178static struct toshiba_acpi_dev *toshiba_acpi;
179
4db42c51 180static const struct acpi_device_id toshiba_device_ids[] = {
181 {"TOS6200", 0},
c41a40c5 182 {"TOS6208", 0},
4db42c51 183 {"TOS1900", 0},
184 {"", 0},
185};
186MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
187
b859f159 188static const struct key_entry toshiba_acpi_keymap[] = {
fec278a1 189 { KE_KEY, 0x9e, { KEY_RFKILL } },
384a7cd9
DT
190 { KE_KEY, 0x101, { KEY_MUTE } },
191 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
192 { KE_KEY, 0x103, { KEY_ZOOMIN } },
408a5d13 193 { KE_KEY, 0x10f, { KEY_TAB } },
af502837
AA
194 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
195 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
384a7cd9
DT
196 { KE_KEY, 0x13b, { KEY_COFFEE } },
197 { KE_KEY, 0x13c, { KEY_BATTERY } },
198 { KE_KEY, 0x13d, { KEY_SLEEP } },
199 { KE_KEY, 0x13e, { KEY_SUSPEND } },
200 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
201 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
202 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
203 { KE_KEY, 0x142, { KEY_WLAN } },
af502837 204 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
a49010f5 205 { KE_KEY, 0x17f, { KEY_FN } },
384a7cd9
DT
206 { KE_KEY, 0xb05, { KEY_PROG2 } },
207 { KE_KEY, 0xb06, { KEY_WWW } },
208 { KE_KEY, 0xb07, { KEY_MAIL } },
209 { KE_KEY, 0xb30, { KEY_STOP } },
210 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
211 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
212 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
213 { KE_KEY, 0xb5a, { KEY_MEDIA } },
408a5d13
AA
214 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
215 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
216 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
217 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
218 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
384a7cd9 219 { KE_END, 0 },
6335e4d5
MG
220};
221
fe808bfb
TI
222/* alternative keymap */
223static const struct dmi_system_id toshiba_alt_keymap_dmi[] = {
224 {
225 .matches = {
226 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
227 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite M840"),
228 },
229 },
e6efad7f
AA
230 {
231 .matches = {
232 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
233 DMI_MATCH(DMI_PRODUCT_NAME, "Qosmio X75-A"),
234 },
235 },
fe808bfb
TI
236 {}
237};
238
239static const struct key_entry toshiba_acpi_alt_keymap[] = {
240 { KE_KEY, 0x157, { KEY_MUTE } },
241 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
242 { KE_KEY, 0x103, { KEY_ZOOMIN } },
e6efad7f 243 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
fe808bfb
TI
244 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
245 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
246 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
247 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
248 { KE_KEY, 0x158, { KEY_WLAN } },
249 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
250 { KE_END, 0 },
251};
252
1da177e4
LT
253/* utility
254 */
255
4be44fcd 256static __inline__ void _set_bit(u32 * word, u32 mask, int value)
1da177e4
LT
257{
258 *word = (*word & ~mask) | (mask * value);
259}
260
261/* acpi interface wrappers
262 */
263
4be44fcd 264static int write_acpi_int(const char *methodName, int val)
1da177e4 265{
1da177e4
LT
266 acpi_status status;
267
619400da 268 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
32bcd5cb 269 return (status == AE_OK) ? 0 : -EIO;
1da177e4
LT
270}
271
1da177e4
LT
272/* Perform a raw HCI call. Here we don't care about input or output buffer
273 * format.
274 */
135740de
SF
275static acpi_status hci_raw(struct toshiba_acpi_dev *dev,
276 const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
1da177e4
LT
277{
278 struct acpi_object_list params;
279 union acpi_object in_objs[HCI_WORDS];
280 struct acpi_buffer results;
4be44fcd 281 union acpi_object out_objs[HCI_WORDS + 1];
1da177e4
LT
282 acpi_status status;
283 int i;
284
285 params.count = HCI_WORDS;
286 params.pointer = in_objs;
287 for (i = 0; i < HCI_WORDS; ++i) {
288 in_objs[i].type = ACPI_TYPE_INTEGER;
289 in_objs[i].integer.value = in[i];
290 }
291
292 results.length = sizeof(out_objs);
293 results.pointer = out_objs;
294
6e02cc7e
SF
295 status = acpi_evaluate_object(dev->acpi_dev->handle,
296 (char *)dev->method_hci, &params,
4be44fcd 297 &results);
1da177e4
LT
298 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
299 for (i = 0; i < out_objs->package.count; ++i) {
300 out[i] = out_objs->package.elements[i].integer.value;
301 }
302 }
303
304 return status;
305}
306
c41a40c5 307/* common hci tasks (get or set one or two value)
1da177e4
LT
308 *
309 * In addition to the ACPI status, the HCI system returns a result which
310 * may be useful (such as "not supported").
311 */
312
135740de
SF
313static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg,
314 u32 in1, u32 *result)
1da177e4
LT
315{
316 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
317 u32 out[HCI_WORDS];
135740de 318 acpi_status status = hci_raw(dev, in, out);
1da177e4
LT
319 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
320 return status;
321}
322
135740de
SF
323static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg,
324 u32 *out1, u32 *result)
1da177e4
LT
325{
326 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
327 u32 out[HCI_WORDS];
135740de 328 acpi_status status = hci_raw(dev, in, out);
1da177e4
LT
329 *out1 = out[2];
330 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
331 return status;
332}
333
135740de
SF
334static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg,
335 u32 in1, u32 in2, u32 *result)
c41a40c5 336{
337 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
338 u32 out[HCI_WORDS];
135740de 339 acpi_status status = hci_raw(dev, in, out);
c41a40c5 340 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
341 return status;
342}
343
135740de
SF
344static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg,
345 u32 *out1, u32 *out2, u32 *result)
c41a40c5 346{
347 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
348 u32 out[HCI_WORDS];
135740de 349 acpi_status status = hci_raw(dev, in, out);
c41a40c5 350 *out1 = out[2];
351 *out2 = out[3];
352 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
353 return status;
354}
355
84a6273f
AA
356/* common sci tasks
357 */
358
359static int sci_open(struct toshiba_acpi_dev *dev)
360{
361 u32 in[HCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
362 u32 out[HCI_WORDS];
363 acpi_status status;
364
365 status = hci_raw(dev, in, out);
366 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
367 pr_err("ACPI call to open SCI failed\n");
368 return 0;
369 }
370
371 if (out[0] == SCI_OPEN_CLOSE_OK) {
372 return 1;
373 } else if (out[0] == SCI_ALREADY_OPEN) {
374 pr_info("Toshiba SCI already opened\n");
375 return 1;
376 } else if (out[0] == SCI_NOT_PRESENT) {
377 pr_info("Toshiba SCI is not present\n");
378 }
379
380 return 0;
381}
382
383static void sci_close(struct toshiba_acpi_dev *dev)
384{
385 u32 in[HCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
386 u32 out[HCI_WORDS];
387 acpi_status status;
388
389 status = hci_raw(dev, in, out);
390 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
391 pr_err("ACPI call to close SCI failed\n");
392 return;
393 }
394
395 if (out[0] == SCI_OPEN_CLOSE_OK)
396 return;
397 else if (out[0] == SCI_NOT_OPENED)
398 pr_info("Toshiba SCI not opened\n");
399 else if (out[0] == SCI_NOT_PRESENT)
400 pr_info("Toshiba SCI is not present\n");
401}
402
403static acpi_status sci_read(struct toshiba_acpi_dev *dev, u32 reg,
404 u32 *out1, u32 *result)
405{
406 u32 in[HCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
407 u32 out[HCI_WORDS];
408 acpi_status status = hci_raw(dev, in, out);
409 *out1 = out[2];
410 *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
411 return status;
412}
413
414static acpi_status sci_write(struct toshiba_acpi_dev *dev, u32 reg,
415 u32 in1, u32 *result)
416{
417 u32 in[HCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
418 u32 out[HCI_WORDS];
419 acpi_status status = hci_raw(dev, in, out);
420 *result = (ACPI_SUCCESS(status)) ? out[0] : HCI_FAILURE;
421 return status;
422}
423
6c3f6e6c 424/* Illumination support */
135740de 425static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
6c3f6e6c 426{
fdb79081 427 u32 in[HCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
6c3f6e6c
PD
428 u32 out[HCI_WORDS];
429 acpi_status status;
430
fdb79081
AA
431 if (!sci_open(dev))
432 return 0;
433
135740de 434 status = hci_raw(dev, in, out);
fdb79081
AA
435 sci_close(dev);
436 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
437 pr_err("ACPI call to query Illumination support failed\n");
438 return 0;
12962878 439 } else if (out[0] == HCI_NOT_SUPPORTED) {
7e33460d 440 pr_info("Illumination device not available\n");
6c3f6e6c
PD
441 return 0;
442 }
fdb79081 443
6c3f6e6c
PD
444 return 1;
445}
446
447static void toshiba_illumination_set(struct led_classdev *cdev,
448 enum led_brightness brightness)
449{
135740de
SF
450 struct toshiba_acpi_dev *dev = container_of(cdev,
451 struct toshiba_acpi_dev, led_dev);
fdb79081 452 u32 state, result;
6c3f6e6c
PD
453 acpi_status status;
454
455 /* First request : initialize communication. */
fdb79081 456 if (!sci_open(dev))
6c3f6e6c 457 return;
6c3f6e6c 458
fdb79081
AA
459 /* Switch the illumination on/off */
460 state = brightness ? 1 : 0;
461 status = sci_write(dev, SCI_ILLUMINATION, state, &result);
462 sci_close(dev);
463 if (ACPI_FAILURE(status)) {
464 pr_err("ACPI call for illumination failed\n");
465 return;
466 } else if (result == HCI_NOT_SUPPORTED) {
467 pr_info("Illumination not supported\n");
468 return;
6c3f6e6c 469 }
6c3f6e6c
PD
470}
471
472static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
473{
135740de
SF
474 struct toshiba_acpi_dev *dev = container_of(cdev,
475 struct toshiba_acpi_dev, led_dev);
fdb79081 476 u32 state, result;
6c3f6e6c 477 acpi_status status;
6c3f6e6c
PD
478
479 /* First request : initialize communication. */
fdb79081 480 if (!sci_open(dev))
6c3f6e6c 481 return LED_OFF;
6c3f6e6c
PD
482
483 /* Check the illumination */
fdb79081
AA
484 status = sci_read(dev, SCI_ILLUMINATION, &state, &result);
485 sci_close(dev);
486 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
487 pr_err("ACPI call for illumination failed\n");
488 return LED_OFF;
489 } else if (result == HCI_NOT_SUPPORTED) {
490 pr_info("Illumination not supported\n");
6c3f6e6c
PD
491 return LED_OFF;
492 }
493
fdb79081 494 return state ? LED_FULL : LED_OFF;
6c3f6e6c 495}
ea6b31f4 496
360f0f39
AA
497/* KBD Illumination */
498static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
499{
500 u32 result;
501 acpi_status status;
502
503 if (!sci_open(dev))
504 return -EIO;
505
506 status = sci_write(dev, SCI_KBD_ILLUM_STATUS, time, &result);
507 sci_close(dev);
508 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
509 pr_err("ACPI call to set KBD backlight status failed\n");
510 return -EIO;
511 } else if (result == HCI_NOT_SUPPORTED) {
512 pr_info("Keyboard backlight status not supported\n");
513 return -ENODEV;
514 }
515
516 return 0;
517}
518
519static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
520{
521 u32 result;
522 acpi_status status;
523
524 if (!sci_open(dev))
525 return -EIO;
526
527 status = sci_read(dev, SCI_KBD_ILLUM_STATUS, time, &result);
528 sci_close(dev);
529 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
530 pr_err("ACPI call to get KBD backlight status failed\n");
531 return -EIO;
532 } else if (result == HCI_NOT_SUPPORTED) {
533 pr_info("Keyboard backlight status not supported\n");
534 return -ENODEV;
535 }
536
537 return 0;
538}
539
540static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
541{
542 struct toshiba_acpi_dev *dev = container_of(cdev,
543 struct toshiba_acpi_dev, kbd_led);
544 u32 state, result;
545 acpi_status status;
546
547 /* Check the keyboard backlight state */
548 status = hci_read1(dev, HCI_KBD_ILLUMINATION, &state, &result);
549 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
550 pr_err("ACPI call to get the keyboard backlight failed\n");
551 return LED_OFF;
552 } else if (result == HCI_NOT_SUPPORTED) {
553 pr_info("Keyboard backlight not supported\n");
554 return LED_OFF;
555 }
556
557 return state ? LED_FULL : LED_OFF;
558}
559
560static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
561 enum led_brightness brightness)
562{
563 struct toshiba_acpi_dev *dev = container_of(cdev,
564 struct toshiba_acpi_dev, kbd_led);
565 u32 state, result;
566 acpi_status status;
567
568 /* Set the keyboard backlight state */
569 state = brightness ? 1 : 0;
570 status = hci_write1(dev, HCI_KBD_ILLUMINATION, state, &result);
571 if (ACPI_FAILURE(status) || result == SCI_INPUT_DATA_ERROR) {
572 pr_err("ACPI call to set KBD Illumination mode failed\n");
573 return;
574 } else if (result == HCI_NOT_SUPPORTED) {
575 pr_info("Keyboard backlight not supported\n");
576 return;
577 }
578}
ea6b31f4 579
9d8658ac
AA
580/* TouchPad support */
581static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
582{
583 u32 result;
584 acpi_status status;
585
586 if (!sci_open(dev))
587 return -EIO;
588
589 status = sci_write(dev, SCI_TOUCHPAD, state, &result);
590 sci_close(dev);
591 if (ACPI_FAILURE(status)) {
592 pr_err("ACPI call to set the touchpad failed\n");
593 return -EIO;
594 } else if (result == HCI_NOT_SUPPORTED) {
595 return -ENODEV;
596 }
597
598 return 0;
599}
600
601static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
602{
603 u32 result;
604 acpi_status status;
605
606 if (!sci_open(dev))
607 return -EIO;
608
609 status = sci_read(dev, SCI_TOUCHPAD, state, &result);
610 sci_close(dev);
611 if (ACPI_FAILURE(status)) {
612 pr_err("ACPI call to query the touchpad failed\n");
613 return -EIO;
614 } else if (result == HCI_NOT_SUPPORTED) {
615 return -ENODEV;
616 }
617
618 return 0;
619}
6c3f6e6c 620
def6c4e2
AA
621/* Eco Mode support */
622static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
623{
624 acpi_status status;
625 u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
626 u32 out[HCI_WORDS];
627
628 status = hci_raw(dev, in, out);
629 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
630 pr_info("ACPI call to get ECO led failed\n");
631 return 0;
632 }
633
634 return 1;
635}
636
637static enum led_brightness toshiba_eco_mode_get_status(struct led_classdev *cdev)
638{
639 struct toshiba_acpi_dev *dev = container_of(cdev,
640 struct toshiba_acpi_dev, eco_led);
641 u32 in[HCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
642 u32 out[HCI_WORDS];
643 acpi_status status;
644
645 status = hci_raw(dev, in, out);
646 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
647 pr_err("ACPI call to get ECO led failed\n");
648 return LED_OFF;
649 }
650
651 return out[2] ? LED_FULL : LED_OFF;
652}
653
654static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
655 enum led_brightness brightness)
656{
657 struct toshiba_acpi_dev *dev = container_of(cdev,
658 struct toshiba_acpi_dev, eco_led);
659 u32 in[HCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
660 u32 out[HCI_WORDS];
661 acpi_status status;
662
663 /* Switch the Eco Mode led on/off */
664 in[2] = (brightness) ? 1 : 0;
665 status = hci_raw(dev, in, out);
666 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
667 pr_err("ACPI call to set ECO led failed\n");
668 return;
669 }
670}
ea6b31f4 671
5a2813e9
AA
672/* Accelerometer support */
673static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
674{
675 u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
676 u32 out[HCI_WORDS];
677 acpi_status status;
678
679 /* Check if the accelerometer call exists,
680 * this call also serves as initialization
681 */
682 status = hci_raw(dev, in, out);
683 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
684 pr_err("ACPI call to query the accelerometer failed\n");
685 return -EIO;
686 } else if (out[0] == HCI_DATA_NOT_AVAILABLE ||
687 out[0] == HCI_NOT_INITIALIZED) {
688 pr_err("Accelerometer not initialized\n");
689 return -EIO;
690 } else if (out[0] == HCI_NOT_SUPPORTED) {
691 pr_info("Accelerometer not supported\n");
692 return -ENODEV;
693 }
694
695 return 0;
696}
697
698static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
699 u32 *xy, u32 *z)
700{
701 u32 in[HCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
702 u32 out[HCI_WORDS];
703 acpi_status status;
704
705 /* Check the Accelerometer status */
706 status = hci_raw(dev, in, out);
707 if (ACPI_FAILURE(status) || out[0] == SCI_INPUT_DATA_ERROR) {
708 pr_err("ACPI call to query the accelerometer failed\n");
709 return -EIO;
710 }
711
712 *xy = out[2];
713 *z = out[4];
714
715 return 0;
716}
def6c4e2 717
c41a40c5 718/* Bluetooth rfkill handlers */
719
135740de 720static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
c41a40c5 721{
722 u32 hci_result;
723 u32 value, value2;
724
725 value = 0;
726 value2 = 0;
135740de 727 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
c41a40c5 728 if (hci_result == HCI_SUCCESS)
729 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
730
731 return hci_result;
732}
733
135740de 734static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
c41a40c5 735{
736 u32 hci_result;
737 u32 value, value2;
738
739 value = 0;
740 value2 = 0x0001;
135740de 741 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result);
c41a40c5 742
743 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
744 return hci_result;
745}
746
19d337df 747static int bt_rfkill_set_block(void *data, bool blocked)
c41a40c5 748{
19d337df 749 struct toshiba_acpi_dev *dev = data;
c41a40c5 750 u32 result1, result2;
751 u32 value;
19d337df 752 int err;
c41a40c5 753 bool radio_state;
c41a40c5 754
19d337df 755 value = (blocked == false);
c41a40c5 756
19d337df 757 mutex_lock(&dev->mutex);
135740de 758 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) {
32bcd5cb 759 err = -EIO;
19d337df
JB
760 goto out;
761 }
c41a40c5 762
19d337df
JB
763 if (!radio_state) {
764 err = 0;
765 goto out;
c41a40c5 766 }
767
135740de
SF
768 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
769 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
c41a40c5 770
771 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
32bcd5cb 772 err = -EIO;
19d337df
JB
773 else
774 err = 0;
775 out:
776 mutex_unlock(&dev->mutex);
777 return err;
c41a40c5 778}
779
19d337df 780static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
c41a40c5 781{
c41a40c5 782 bool new_rfk_state;
783 bool value;
784 u32 hci_result;
19d337df
JB
785 struct toshiba_acpi_dev *dev = data;
786
787 mutex_lock(&dev->mutex);
c41a40c5 788
135740de 789 hci_result = hci_get_radio_state(dev, &value);
19d337df
JB
790 if (hci_result != HCI_SUCCESS) {
791 /* Can't do anything useful */
792 mutex_unlock(&dev->mutex);
82e7784f 793 return;
19d337df 794 }
c41a40c5 795
796 new_rfk_state = value;
797
c41a40c5 798 mutex_unlock(&dev->mutex);
799
19d337df
JB
800 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
801 bt_rfkill_set_block(data, true);
c41a40c5 802}
803
19d337df
JB
804static const struct rfkill_ops toshiba_rfk_ops = {
805 .set_block = bt_rfkill_set_block,
806 .poll = bt_rfkill_poll,
807};
808
121b7b0d
AI
809static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
810{
811 u32 hci_result;
812 u32 status;
813
814 hci_read1(dev, HCI_TR_BACKLIGHT, &status, &hci_result);
815 *enabled = !status;
816 return hci_result == HCI_SUCCESS ? 0 : -EIO;
817}
818
819static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
820{
821 u32 hci_result;
822 u32 value = !enable;
823
824 hci_write1(dev, HCI_TR_BACKLIGHT, value, &hci_result);
825 return hci_result == HCI_SUCCESS ? 0 : -EIO;
826}
827
4be44fcd 828static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
1da177e4 829
62cce752 830static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
1da177e4
LT
831{
832 u32 hci_result;
833 u32 value;
121b7b0d
AI
834 int brightness = 0;
835
836 if (dev->tr_backlight_supported) {
837 bool enabled;
838 int ret = get_tr_backlight_status(dev, &enabled);
839 if (ret)
840 return ret;
841 if (enabled)
842 return 0;
843 brightness++;
844 }
1da177e4 845
135740de 846 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result);
32bcd5cb 847 if (hci_result == HCI_SUCCESS)
121b7b0d 848 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
32bcd5cb
SF
849
850 return -EIO;
c9263557
HM
851}
852
62cce752
SF
853static int get_lcd_brightness(struct backlight_device *bd)
854{
855 struct toshiba_acpi_dev *dev = bl_get_data(bd);
856 return __get_lcd_brightness(dev);
857}
858
936c8bcd 859static int lcd_proc_show(struct seq_file *m, void *v)
c9263557 860{
135740de
SF
861 struct toshiba_acpi_dev *dev = m->private;
862 int value;
121b7b0d 863 int levels;
135740de
SF
864
865 if (!dev->backlight_dev)
866 return -ENODEV;
c9263557 867
121b7b0d 868 levels = dev->backlight_dev->props.max_brightness + 1;
62cce752 869 value = get_lcd_brightness(dev->backlight_dev);
c9263557 870 if (value >= 0) {
936c8bcd 871 seq_printf(m, "brightness: %d\n", value);
121b7b0d 872 seq_printf(m, "brightness_levels: %d\n", levels);
32bcd5cb 873 return 0;
1da177e4
LT
874 }
875
32bcd5cb
SF
876 pr_err("Error reading LCD brightness\n");
877 return -EIO;
936c8bcd
AD
878}
879
880static int lcd_proc_open(struct inode *inode, struct file *file)
881{
d9dda78b 882 return single_open(file, lcd_proc_show, PDE_DATA(inode));
1da177e4
LT
883}
884
62cce752 885static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
c9263557 886{
f6aac652
AA
887 u32 in[HCI_WORDS] = { HCI_SET, HCI_LCD_BRIGHTNESS, 0, 0, 0, 0 };
888 u32 out[HCI_WORDS];
889 acpi_status status;
c9263557 890
121b7b0d
AI
891 if (dev->tr_backlight_supported) {
892 bool enable = !value;
893 int ret = set_tr_backlight_status(dev, enable);
894 if (ret)
895 return ret;
896 if (value)
897 value--;
898 }
899
f6aac652
AA
900 in[2] = value << HCI_LCD_BRIGHTNESS_SHIFT;
901 status = hci_raw(dev, in, out);
902 if (ACPI_FAILURE(status) || out[0] == HCI_FAILURE) {
903 pr_err("ACPI call to set brightness failed");
904 return -EIO;
905 }
906 /* Extra check for "incomplete" backlight method, where the AML code
907 * doesn't check for HCI_SET or HCI_GET and returns HCI_SUCCESS,
908 * the actual brightness, and in some cases the max brightness.
909 */
910 if (out[2] > 0 || out[3] == 0xE000)
911 return -ENODEV;
912
913 return out[0] == HCI_SUCCESS ? 0 : -EIO;
c9263557
HM
914}
915
916static int set_lcd_status(struct backlight_device *bd)
917{
135740de 918 struct toshiba_acpi_dev *dev = bl_get_data(bd);
62cce752 919 return set_lcd_brightness(dev, bd->props.brightness);
c9263557
HM
920}
921
936c8bcd
AD
922static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
923 size_t count, loff_t *pos)
1da177e4 924{
d9dda78b 925 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
936c8bcd
AD
926 char cmd[42];
927 size_t len;
1da177e4 928 int value;
c8af57eb 929 int ret;
121b7b0d 930 int levels = dev->backlight_dev->props.max_brightness + 1;
1da177e4 931
936c8bcd
AD
932 len = min(count, sizeof(cmd) - 1);
933 if (copy_from_user(cmd, buf, len))
934 return -EFAULT;
935 cmd[len] = '\0';
936
937 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
121b7b0d 938 value >= 0 && value < levels) {
62cce752 939 ret = set_lcd_brightness(dev, value);
c8af57eb
MO
940 if (ret == 0)
941 ret = count;
942 } else {
c9263557 943 ret = -EINVAL;
c8af57eb 944 }
c9263557 945 return ret;
1da177e4
LT
946}
947
936c8bcd
AD
948static const struct file_operations lcd_proc_fops = {
949 .owner = THIS_MODULE,
950 .open = lcd_proc_open,
951 .read = seq_read,
952 .llseek = seq_lseek,
953 .release = single_release,
954 .write = lcd_proc_write,
955};
956
36d03f93
SF
957static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
958{
959 u32 hci_result;
960
961 hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result);
962 return hci_result == HCI_SUCCESS ? 0 : -EIO;
963}
964
936c8bcd 965static int video_proc_show(struct seq_file *m, void *v)
1da177e4 966{
135740de 967 struct toshiba_acpi_dev *dev = m->private;
1da177e4 968 u32 value;
36d03f93 969 int ret;
1da177e4 970
36d03f93
SF
971 ret = get_video_status(dev, &value);
972 if (!ret) {
1da177e4
LT
973 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
974 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
4be44fcd 975 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
936c8bcd
AD
976 seq_printf(m, "lcd_out: %d\n", is_lcd);
977 seq_printf(m, "crt_out: %d\n", is_crt);
978 seq_printf(m, "tv_out: %d\n", is_tv);
1da177e4
LT
979 }
980
36d03f93 981 return ret;
1da177e4
LT
982}
983
936c8bcd 984static int video_proc_open(struct inode *inode, struct file *file)
1da177e4 985{
d9dda78b 986 return single_open(file, video_proc_show, PDE_DATA(inode));
936c8bcd
AD
987}
988
989static ssize_t video_proc_write(struct file *file, const char __user *buf,
990 size_t count, loff_t *pos)
991{
d9dda78b 992 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
936c8bcd 993 char *cmd, *buffer;
36d03f93 994 int ret;
1da177e4
LT
995 int value;
996 int remain = count;
997 int lcd_out = -1;
998 int crt_out = -1;
999 int tv_out = -1;
b4482a4b 1000 u32 video_out;
1da177e4 1001
936c8bcd
AD
1002 cmd = kmalloc(count + 1, GFP_KERNEL);
1003 if (!cmd)
1004 return -ENOMEM;
1005 if (copy_from_user(cmd, buf, count)) {
1006 kfree(cmd);
1007 return -EFAULT;
1008 }
1009 cmd[count] = '\0';
1010
1011 buffer = cmd;
1012
1da177e4
LT
1013 /* scan expression. Multiple expressions may be delimited with ;
1014 *
1015 * NOTE: to keep scanning simple, invalid fields are ignored
1016 */
1017 while (remain) {
1018 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1019 lcd_out = value & 1;
1020 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1021 crt_out = value & 1;
1022 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1023 tv_out = value & 1;
1024 /* advance to one character past the next ; */
1025 do {
1026 ++buffer;
1027 --remain;
1028 }
4be44fcd 1029 while (remain && *(buffer - 1) != ';');
1da177e4
LT
1030 }
1031
936c8bcd
AD
1032 kfree(cmd);
1033
36d03f93
SF
1034 ret = get_video_status(dev, &video_out);
1035 if (!ret) {
9e113e00 1036 unsigned int new_video_out = video_out;
1da177e4
LT
1037 if (lcd_out != -1)
1038 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1039 if (crt_out != -1)
1040 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1041 if (tv_out != -1)
1042 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1043 /* To avoid unnecessary video disruption, only write the new
1044 * video setting if something changed. */
1045 if (new_video_out != video_out)
32bcd5cb 1046 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1da177e4
LT
1047 }
1048
32bcd5cb 1049 return ret ? ret : count;
1da177e4
LT
1050}
1051
936c8bcd
AD
1052static const struct file_operations video_proc_fops = {
1053 .owner = THIS_MODULE,
1054 .open = video_proc_open,
1055 .read = seq_read,
1056 .llseek = seq_lseek,
1057 .release = single_release,
1058 .write = video_proc_write,
1059};
1060
36d03f93
SF
1061static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1062{
1063 u32 hci_result;
1064
1065 hci_read1(dev, HCI_FAN, status, &hci_result);
1066 return hci_result == HCI_SUCCESS ? 0 : -EIO;
1067}
1068
936c8bcd 1069static int fan_proc_show(struct seq_file *m, void *v)
1da177e4 1070{
135740de 1071 struct toshiba_acpi_dev *dev = m->private;
36d03f93 1072 int ret;
1da177e4
LT
1073 u32 value;
1074
36d03f93
SF
1075 ret = get_fan_status(dev, &value);
1076 if (!ret) {
936c8bcd 1077 seq_printf(m, "running: %d\n", (value > 0));
135740de 1078 seq_printf(m, "force_on: %d\n", dev->force_fan);
1da177e4
LT
1079 }
1080
36d03f93 1081 return ret;
936c8bcd
AD
1082}
1083
1084static int fan_proc_open(struct inode *inode, struct file *file)
1085{
d9dda78b 1086 return single_open(file, fan_proc_show, PDE_DATA(inode));
1da177e4
LT
1087}
1088
936c8bcd
AD
1089static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1090 size_t count, loff_t *pos)
1da177e4 1091{
d9dda78b 1092 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
936c8bcd
AD
1093 char cmd[42];
1094 size_t len;
1da177e4
LT
1095 int value;
1096 u32 hci_result;
1097
936c8bcd
AD
1098 len = min(count, sizeof(cmd) - 1);
1099 if (copy_from_user(cmd, buf, len))
1100 return -EFAULT;
1101 cmd[len] = '\0';
1102
1103 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
4be44fcd 1104 value >= 0 && value <= 1) {
135740de 1105 hci_write1(dev, HCI_FAN, value, &hci_result);
1da177e4 1106 if (hci_result != HCI_SUCCESS)
32bcd5cb 1107 return -EIO;
1da177e4 1108 else
135740de 1109 dev->force_fan = value;
1da177e4
LT
1110 } else {
1111 return -EINVAL;
1112 }
1113
1114 return count;
1115}
1116
936c8bcd
AD
1117static const struct file_operations fan_proc_fops = {
1118 .owner = THIS_MODULE,
1119 .open = fan_proc_open,
1120 .read = seq_read,
1121 .llseek = seq_lseek,
1122 .release = single_release,
1123 .write = fan_proc_write,
1124};
1125
1126static int keys_proc_show(struct seq_file *m, void *v)
1da177e4 1127{
135740de 1128 struct toshiba_acpi_dev *dev = m->private;
1da177e4
LT
1129 u32 hci_result;
1130 u32 value;
1131
11948b93 1132 if (!dev->key_event_valid && dev->system_event_supported) {
135740de 1133 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1da177e4 1134 if (hci_result == HCI_SUCCESS) {
135740de
SF
1135 dev->key_event_valid = 1;
1136 dev->last_key_event = value;
1da177e4
LT
1137 } else if (hci_result == HCI_EMPTY) {
1138 /* better luck next time */
1139 } else if (hci_result == HCI_NOT_SUPPORTED) {
1140 /* This is a workaround for an unresolved issue on
1141 * some machines where system events sporadically
1142 * become disabled. */
135740de 1143 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
7e33460d 1144 pr_notice("Re-enabled hotkeys\n");
1da177e4 1145 } else {
7e33460d 1146 pr_err("Error reading hotkey status\n");
32bcd5cb 1147 return -EIO;
1da177e4
LT
1148 }
1149 }
1150
135740de
SF
1151 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1152 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
936c8bcd
AD
1153 return 0;
1154}
1da177e4 1155
936c8bcd
AD
1156static int keys_proc_open(struct inode *inode, struct file *file)
1157{
d9dda78b 1158 return single_open(file, keys_proc_show, PDE_DATA(inode));
1da177e4
LT
1159}
1160
936c8bcd
AD
1161static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1162 size_t count, loff_t *pos)
1da177e4 1163{
d9dda78b 1164 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
936c8bcd
AD
1165 char cmd[42];
1166 size_t len;
1da177e4
LT
1167 int value;
1168
936c8bcd
AD
1169 len = min(count, sizeof(cmd) - 1);
1170 if (copy_from_user(cmd, buf, len))
1171 return -EFAULT;
1172 cmd[len] = '\0';
1173
1174 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) {
135740de 1175 dev->key_event_valid = 0;
1da177e4
LT
1176 } else {
1177 return -EINVAL;
1178 }
1179
1180 return count;
1181}
1182
936c8bcd
AD
1183static const struct file_operations keys_proc_fops = {
1184 .owner = THIS_MODULE,
1185 .open = keys_proc_open,
1186 .read = seq_read,
1187 .llseek = seq_lseek,
1188 .release = single_release,
1189 .write = keys_proc_write,
1190};
1191
1192static int version_proc_show(struct seq_file *m, void *v)
1da177e4 1193{
936c8bcd
AD
1194 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1195 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1196 return 0;
1da177e4
LT
1197}
1198
936c8bcd
AD
1199static int version_proc_open(struct inode *inode, struct file *file)
1200{
d9dda78b 1201 return single_open(file, version_proc_show, PDE_DATA(inode));
936c8bcd
AD
1202}
1203
1204static const struct file_operations version_proc_fops = {
1205 .owner = THIS_MODULE,
1206 .open = version_proc_open,
1207 .read = seq_read,
1208 .llseek = seq_lseek,
1209 .release = single_release,
1210};
1211
1da177e4
LT
1212/* proc and module init
1213 */
1214
1215#define PROC_TOSHIBA "toshiba"
1216
b859f159 1217static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1da177e4 1218{
36d03f93
SF
1219 if (dev->backlight_dev)
1220 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1221 &lcd_proc_fops, dev);
1222 if (dev->video_supported)
1223 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1224 &video_proc_fops, dev);
1225 if (dev->fan_supported)
1226 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1227 &fan_proc_fops, dev);
1228 if (dev->hotkey_dev)
1229 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1230 &keys_proc_fops, dev);
135740de
SF
1231 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1232 &version_proc_fops, dev);
1da177e4
LT
1233}
1234
36d03f93 1235static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1da177e4 1236{
36d03f93
SF
1237 if (dev->backlight_dev)
1238 remove_proc_entry("lcd", toshiba_proc_dir);
1239 if (dev->video_supported)
1240 remove_proc_entry("video", toshiba_proc_dir);
1241 if (dev->fan_supported)
1242 remove_proc_entry("fan", toshiba_proc_dir);
1243 if (dev->hotkey_dev)
1244 remove_proc_entry("keys", toshiba_proc_dir);
936c8bcd 1245 remove_proc_entry("version", toshiba_proc_dir);
1da177e4
LT
1246}
1247
acc2472e 1248static const struct backlight_ops toshiba_backlight_data = {
121b7b0d 1249 .options = BL_CORE_SUSPENDRESUME,
62cce752
SF
1250 .get_brightness = get_lcd_brightness,
1251 .update_status = set_lcd_status,
c9263557 1252};
ea6b31f4 1253
360f0f39
AA
1254/*
1255 * Sysfs files
1256 */
1257
1258static ssize_t toshiba_kbd_bl_mode_store(struct device *dev,
1259 struct device_attribute *attr,
1260 const char *buf, size_t count)
1261{
1262 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
aeaac098
DC
1263 int mode;
1264 int time;
1265 int ret;
1266
360f0f39 1267
aeaac098
DC
1268 ret = kstrtoint(buf, 0, &mode);
1269 if (ret)
1270 return ret;
1271 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
360f0f39
AA
1272 return -EINVAL;
1273
1274 /* Set the Keyboard Backlight Mode where:
1275 * Mode - Auto (2) | FN-Z (1)
1276 * Auto - KBD backlight turns off automatically in given time
1277 * FN-Z - KBD backlight "toggles" when hotkey pressed
1278 */
aeaac098 1279 if (toshiba->kbd_mode != mode) {
360f0f39
AA
1280 time = toshiba->kbd_time << HCI_MISC_SHIFT;
1281 time = time + toshiba->kbd_mode;
aeaac098
DC
1282 ret = toshiba_kbd_illum_status_set(toshiba, time);
1283 if (ret)
1284 return ret;
360f0f39
AA
1285 toshiba->kbd_mode = mode;
1286 }
1287
1288 return count;
1289}
1290
1291static ssize_t toshiba_kbd_bl_mode_show(struct device *dev,
1292 struct device_attribute *attr,
1293 char *buf)
1294{
1295 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1296 u32 time;
1297
1298 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1299 return -EIO;
1300
1301 return sprintf(buf, "%i\n", time & 0x07);
1302}
1303
1304static ssize_t toshiba_kbd_bl_timeout_store(struct device *dev,
1305 struct device_attribute *attr,
1306 const char *buf, size_t count)
1307{
1308 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1309 int time = -1;
1310
1311 if (sscanf(buf, "%i", &time) != 1 && (time < 0 || time > 60))
1312 return -EINVAL;
1313
1314 /* Set the Keyboard Backlight Timeout: 0-60 seconds */
1315 if (time != -1 && toshiba->kbd_time != time) {
1316 time = time << HCI_MISC_SHIFT;
1317 time = (toshiba->kbd_mode == SCI_KBD_MODE_AUTO) ?
1318 time + 1 : time + 2;
1319 if (toshiba_kbd_illum_status_set(toshiba, time) < 0)
1320 return -EIO;
1321 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1322 }
1323
1324 return count;
1325}
1326
1327static ssize_t toshiba_kbd_bl_timeout_show(struct device *dev,
1328 struct device_attribute *attr,
1329 char *buf)
1330{
1331 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1332 u32 time;
1333
1334 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1335 return -EIO;
1336
1337 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1338}
ea6b31f4 1339
9d8658ac
AA
1340static ssize_t toshiba_touchpad_store(struct device *dev,
1341 struct device_attribute *attr,
1342 const char *buf, size_t count)
1343{
1344 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1345 int state;
1346
1347 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1348 if (sscanf(buf, "%i", &state) == 1 && (state == 0 || state == 1)) {
1349 if (toshiba_touchpad_set(toshiba, state) < 0)
1350 return -EIO;
1351 }
1352
1353 return count;
1354}
1355
1356static ssize_t toshiba_touchpad_show(struct device *dev,
1357 struct device_attribute *attr, char *buf)
1358{
1359 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1360 u32 state;
1361 int ret;
1362
1363 ret = toshiba_touchpad_get(toshiba, &state);
1364 if (ret < 0)
1365 return ret;
1366
1367 return sprintf(buf, "%i\n", state);
1368}
ea6b31f4 1369
5a2813e9
AA
1370static ssize_t toshiba_position_show(struct device *dev,
1371 struct device_attribute *attr, char *buf)
1372{
1373 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1374 u32 xyval, zval, tmp;
1375 u16 x, y, z;
1376 int ret;
1377
1378 xyval = zval = 0;
1379 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1380 if (ret < 0)
1381 return ret;
1382
1383 x = xyval & HCI_ACCEL_MASK;
1384 tmp = xyval >> HCI_MISC_SHIFT;
1385 y = tmp & HCI_ACCEL_MASK;
1386 z = zval & HCI_ACCEL_MASK;
1387
1388 return sprintf(buf, "%d %d %d\n", x, y, z);
1389}
360f0f39
AA
1390
1391static DEVICE_ATTR(kbd_backlight_mode, S_IRUGO | S_IWUSR,
1392 toshiba_kbd_bl_mode_show, toshiba_kbd_bl_mode_store);
1393static DEVICE_ATTR(kbd_backlight_timeout, S_IRUGO | S_IWUSR,
1394 toshiba_kbd_bl_timeout_show, toshiba_kbd_bl_timeout_store);
9d8658ac
AA
1395static DEVICE_ATTR(touchpad, S_IRUGO | S_IWUSR,
1396 toshiba_touchpad_show, toshiba_touchpad_store);
5a2813e9 1397static DEVICE_ATTR(position, S_IRUGO, toshiba_position_show, NULL);
360f0f39
AA
1398
1399static struct attribute *toshiba_attributes[] = {
1400 &dev_attr_kbd_backlight_mode.attr,
1401 &dev_attr_kbd_backlight_timeout.attr,
9d8658ac 1402 &dev_attr_touchpad.attr,
5a2813e9 1403 &dev_attr_position.attr,
360f0f39
AA
1404 NULL,
1405};
1406
1407static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
1408 struct attribute *attr, int idx)
1409{
1410 struct device *dev = container_of(kobj, struct device, kobj);
1411 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
1412 bool exists = true;
1413
1414 if (attr == &dev_attr_kbd_backlight_mode.attr)
1415 exists = (drv->kbd_illum_supported) ? true : false;
1416 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
1417 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
9d8658ac
AA
1418 else if (attr == &dev_attr_touchpad.attr)
1419 exists = (drv->touchpad_supported) ? true : false;
5a2813e9
AA
1420 else if (attr == &dev_attr_position.attr)
1421 exists = (drv->accelerometer_supported) ? true : false;
360f0f39
AA
1422
1423 return exists ? attr->mode : 0;
1424}
1425
1426static struct attribute_group toshiba_attr_group = {
1427 .is_visible = toshiba_sysfs_is_visible,
1428 .attrs = toshiba_attributes,
1429};
c9263557 1430
29cd293f
SF
1431static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
1432 struct serio *port)
1433{
1434 if (str & 0x20)
1435 return false;
1436
1437 if (unlikely(data == 0xe0))
1438 return false;
1439
1440 if ((data & 0x7f) == TOS1900_FN_SCAN) {
1441 schedule_work(&toshiba_acpi->hotkey_work);
1442 return true;
1443 }
1444
1445 return false;
1446}
1447
1448static void toshiba_acpi_hotkey_work(struct work_struct *work)
1449{
1450 acpi_handle ec_handle = ec_get_handle();
1451 acpi_status status;
1452
1453 if (!ec_handle)
1454 return;
1455
1456 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
1457 if (ACPI_FAILURE(status))
1458 pr_err("ACPI NTFY method execution failed\n");
1459}
1460
1461/*
1462 * Returns hotkey scancode, or < 0 on failure.
1463 */
1464static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
1465{
74facaf7 1466 unsigned long long value;
29cd293f
SF
1467 acpi_status status;
1468
74facaf7
ZR
1469 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
1470 NULL, &value);
1471 if (ACPI_FAILURE(status)) {
29cd293f
SF
1472 pr_err("ACPI INFO method execution failed\n");
1473 return -EIO;
1474 }
1475
74facaf7 1476 return value;
29cd293f
SF
1477}
1478
1479static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
1480 int scancode)
1481{
1482 if (scancode == 0x100)
1483 return;
1484
1485 /* act on key press; ignore key release */
1486 if (scancode & 0x80)
1487 return;
1488
1489 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
1490 pr_info("Unknown key %x\n", scancode);
1491}
1492
b859f159 1493static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
6335e4d5
MG
1494{
1495 acpi_status status;
e2e19606 1496 acpi_handle ec_handle;
384a7cd9 1497 int error;
29cd293f 1498 u32 hci_result;
fe808bfb 1499 const struct key_entry *keymap = toshiba_acpi_keymap;
6335e4d5 1500
135740de 1501 dev->hotkey_dev = input_allocate_device();
b222cca6 1502 if (!dev->hotkey_dev)
6335e4d5 1503 return -ENOMEM;
6335e4d5 1504
135740de 1505 dev->hotkey_dev->name = "Toshiba input device";
6e02cc7e 1506 dev->hotkey_dev->phys = "toshiba_acpi/input0";
135740de 1507 dev->hotkey_dev->id.bustype = BUS_HOST;
6335e4d5 1508
fe808bfb
TI
1509 if (dmi_check_system(toshiba_alt_keymap_dmi))
1510 keymap = toshiba_acpi_alt_keymap;
1511 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
384a7cd9
DT
1512 if (error)
1513 goto err_free_dev;
1514
29cd293f
SF
1515 /*
1516 * For some machines the SCI responsible for providing hotkey
1517 * notification doesn't fire. We can trigger the notification
1518 * whenever the Fn key is pressed using the NTFY method, if
1519 * supported, so if it's present set up an i8042 key filter
1520 * for this purpose.
1521 */
1522 status = AE_ERROR;
1523 ec_handle = ec_get_handle();
e2e19606 1524 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
29cd293f
SF
1525 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
1526
1527 error = i8042_install_filter(toshiba_acpi_i8042_filter);
1528 if (error) {
1529 pr_err("Error installing key filter\n");
1530 goto err_free_keymap;
1531 }
1532
1533 dev->ntfy_supported = 1;
1534 }
1535
1536 /*
1537 * Determine hotkey query interface. Prefer using the INFO
1538 * method when it is available.
1539 */
e2e19606 1540 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
29cd293f 1541 dev->info_supported = 1;
e2e19606 1542 else {
29cd293f
SF
1543 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result);
1544 if (hci_result == HCI_SUCCESS)
1545 dev->system_event_supported = 1;
1546 }
1547
1548 if (!dev->info_supported && !dev->system_event_supported) {
1549 pr_warn("No hotkey query interface found\n");
1550 goto err_remove_filter;
1551 }
1552
6e02cc7e 1553 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL);
384a7cd9 1554 if (ACPI_FAILURE(status)) {
7e33460d 1555 pr_info("Unable to enable hotkeys\n");
384a7cd9 1556 error = -ENODEV;
29cd293f 1557 goto err_remove_filter;
6335e4d5
MG
1558 }
1559
135740de 1560 error = input_register_device(dev->hotkey_dev);
384a7cd9 1561 if (error) {
7e33460d 1562 pr_info("Unable to register input device\n");
29cd293f 1563 goto err_remove_filter;
6335e4d5
MG
1564 }
1565
29cd293f 1566 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result);
6335e4d5 1567 return 0;
384a7cd9 1568
29cd293f
SF
1569 err_remove_filter:
1570 if (dev->ntfy_supported)
1571 i8042_remove_filter(toshiba_acpi_i8042_filter);
384a7cd9 1572 err_free_keymap:
135740de 1573 sparse_keymap_free(dev->hotkey_dev);
384a7cd9 1574 err_free_dev:
135740de
SF
1575 input_free_device(dev->hotkey_dev);
1576 dev->hotkey_dev = NULL;
384a7cd9 1577 return error;
6335e4d5
MG
1578}
1579
b859f159 1580static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
62cce752
SF
1581{
1582 struct backlight_properties props;
1583 int brightness;
1584 int ret;
121b7b0d 1585 bool enabled;
62cce752
SF
1586
1587 /*
1588 * Some machines don't support the backlight methods at all, and
1589 * others support it read-only. Either of these is pretty useless,
1590 * so only register the backlight device if the backlight method
1591 * supports both reads and writes.
1592 */
1593 brightness = __get_lcd_brightness(dev);
1594 if (brightness < 0)
1595 return 0;
1596 ret = set_lcd_brightness(dev, brightness);
1597 if (ret) {
1598 pr_debug("Backlight method is read-only, disabling backlight support\n");
1599 return 0;
1600 }
1601
121b7b0d
AI
1602 /* Determine whether or not BIOS supports transflective backlight */
1603 ret = get_tr_backlight_status(dev, &enabled);
1604 dev->tr_backlight_supported = !ret;
1605
53039f22 1606 memset(&props, 0, sizeof(props));
62cce752
SF
1607 props.type = BACKLIGHT_PLATFORM;
1608 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
62cce752 1609
121b7b0d
AI
1610 /* adding an extra level and having 0 change to transflective mode */
1611 if (dev->tr_backlight_supported)
1612 props.max_brightness++;
1613
62cce752
SF
1614 dev->backlight_dev = backlight_device_register("toshiba",
1615 &dev->acpi_dev->dev,
1616 dev,
1617 &toshiba_backlight_data,
1618 &props);
1619 if (IS_ERR(dev->backlight_dev)) {
1620 ret = PTR_ERR(dev->backlight_dev);
1621 pr_err("Could not register toshiba backlight device\n");
1622 dev->backlight_dev = NULL;
1623 return ret;
1624 }
1625
1626 dev->backlight_dev->props.brightness = brightness;
1627 return 0;
1628}
1629
51fac838 1630static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
c9263557 1631{
135740de 1632 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
6335e4d5 1633
36d03f93 1634 remove_toshiba_proc_entries(dev);
ea6b31f4 1635
360f0f39
AA
1636 if (dev->sysfs_created)
1637 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
1638 &toshiba_attr_group);
c41a40c5 1639
29cd293f
SF
1640 if (dev->ntfy_supported) {
1641 i8042_remove_filter(toshiba_acpi_i8042_filter);
1642 cancel_work_sync(&dev->hotkey_work);
1643 }
1644
135740de
SF
1645 if (dev->hotkey_dev) {
1646 input_unregister_device(dev->hotkey_dev);
1647 sparse_keymap_free(dev->hotkey_dev);
1648 }
c9263557 1649
135740de
SF
1650 if (dev->bt_rfk) {
1651 rfkill_unregister(dev->bt_rfk);
1652 rfkill_destroy(dev->bt_rfk);
1653 }
c9263557 1654
135740de
SF
1655 if (dev->backlight_dev)
1656 backlight_device_unregister(dev->backlight_dev);
c9263557 1657
36d03f93 1658 if (dev->illumination_supported)
135740de 1659 led_classdev_unregister(&dev->led_dev);
ea6b31f4 1660
360f0f39
AA
1661 if (dev->kbd_led_registered)
1662 led_classdev_unregister(&dev->kbd_led);
ea6b31f4 1663
def6c4e2
AA
1664 if (dev->eco_supported)
1665 led_classdev_unregister(&dev->eco_led);
6c3f6e6c 1666
29cd293f
SF
1667 if (toshiba_acpi)
1668 toshiba_acpi = NULL;
1669
135740de 1670 kfree(dev);
c41a40c5 1671
135740de 1672 return 0;
c9263557
HM
1673}
1674
b859f159 1675static const char *find_hci_method(acpi_handle handle)
a540d6b5 1676{
e2e19606 1677 if (acpi_has_method(handle, "GHCI"))
a540d6b5
SF
1678 return "GHCI";
1679
e2e19606 1680 if (acpi_has_method(handle, "SPFC"))
a540d6b5
SF
1681 return "SPFC";
1682
1683 return NULL;
1684}
1685
b859f159 1686static int toshiba_acpi_add(struct acpi_device *acpi_dev)
1da177e4 1687{
135740de 1688 struct toshiba_acpi_dev *dev;
a540d6b5 1689 const char *hci_method;
36d03f93 1690 u32 dummy;
c41a40c5 1691 bool bt_present;
c41a40c5 1692 int ret = 0;
1da177e4 1693
29cd293f
SF
1694 if (toshiba_acpi)
1695 return -EBUSY;
1696
135740de
SF
1697 pr_info("Toshiba Laptop ACPI Extras version %s\n",
1698 TOSHIBA_ACPI_VERSION);
1699
a540d6b5
SF
1700 hci_method = find_hci_method(acpi_dev->handle);
1701 if (!hci_method) {
1702 pr_err("HCI interface not found\n");
6e02cc7e 1703 return -ENODEV;
a540d6b5 1704 }
6e02cc7e 1705
135740de
SF
1706 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1707 if (!dev)
1708 return -ENOMEM;
1709 dev->acpi_dev = acpi_dev;
a540d6b5 1710 dev->method_hci = hci_method;
135740de 1711 acpi_dev->driver_data = dev;
360f0f39 1712 dev_set_drvdata(&acpi_dev->dev, dev);
fb9802fa 1713
6e02cc7e
SF
1714 if (toshiba_acpi_setup_keyboard(dev))
1715 pr_info("Unable to activate hotkeys\n");
135740de
SF
1716
1717 mutex_init(&dev->mutex);
1da177e4 1718
62cce752
SF
1719 ret = toshiba_acpi_setup_backlight(dev);
1720 if (ret)
135740de 1721 goto error;
1da177e4 1722
c41a40c5 1723 /* Register rfkill switch for Bluetooth */
135740de
SF
1724 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) {
1725 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
1726 &acpi_dev->dev,
1727 RFKILL_TYPE_BLUETOOTH,
1728 &toshiba_rfk_ops,
1729 dev);
1730 if (!dev->bt_rfk) {
7e33460d 1731 pr_err("unable to allocate rfkill device\n");
135740de
SF
1732 ret = -ENOMEM;
1733 goto error;
c41a40c5 1734 }
1735
135740de 1736 ret = rfkill_register(dev->bt_rfk);
c41a40c5 1737 if (ret) {
7e33460d 1738 pr_err("unable to register rfkill device\n");
135740de
SF
1739 rfkill_destroy(dev->bt_rfk);
1740 goto error;
38aefbc5 1741 }
c41a40c5 1742 }
1743
135740de
SF
1744 if (toshiba_illumination_available(dev)) {
1745 dev->led_dev.name = "toshiba::illumination";
1746 dev->led_dev.max_brightness = 1;
1747 dev->led_dev.brightness_set = toshiba_illumination_set;
1748 dev->led_dev.brightness_get = toshiba_illumination_get;
1749 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
36d03f93 1750 dev->illumination_supported = 1;
6c3f6e6c 1751 }
ea6b31f4 1752
def6c4e2
AA
1753 if (toshiba_eco_mode_available(dev)) {
1754 dev->eco_led.name = "toshiba::eco_mode";
1755 dev->eco_led.max_brightness = 1;
1756 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
1757 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
1758 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
1759 dev->eco_supported = 1;
1760 }
ea6b31f4 1761
360f0f39
AA
1762 ret = toshiba_kbd_illum_status_get(dev, &dummy);
1763 if (!ret) {
1764 dev->kbd_time = dummy >> HCI_MISC_SHIFT;
1765 dev->kbd_mode = dummy & 0x07;
1766 }
1767 dev->kbd_illum_supported = !ret;
1768 /*
1769 * Only register the LED if KBD illumination is supported
1770 * and the keyboard backlight operation mode is set to FN-Z
1771 */
1772 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
1773 dev->kbd_led.name = "toshiba::kbd_backlight";
1774 dev->kbd_led.max_brightness = 1;
1775 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
1776 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
1777 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
1778 dev->kbd_led_registered = 1;
1779 }
ea6b31f4 1780
9d8658ac
AA
1781 ret = toshiba_touchpad_get(dev, &dummy);
1782 dev->touchpad_supported = !ret;
ea6b31f4 1783
5a2813e9
AA
1784 ret = toshiba_accelerometer_supported(dev);
1785 dev->accelerometer_supported = !ret;
6c3f6e6c 1786
36d03f93
SF
1787 /* Determine whether or not BIOS supports fan and video interfaces */
1788
1789 ret = get_video_status(dev, &dummy);
1790 dev->video_supported = !ret;
1791
1792 ret = get_fan_status(dev, &dummy);
1793 dev->fan_supported = !ret;
1794
360f0f39
AA
1795 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
1796 &toshiba_attr_group);
1797 if (ret) {
1798 dev->sysfs_created = 0;
1799 goto error;
1800 }
1801 dev->sysfs_created = !ret;
1802
36d03f93
SF
1803 create_toshiba_proc_entries(dev);
1804
29cd293f
SF
1805 toshiba_acpi = dev;
1806
c41a40c5 1807 return 0;
135740de
SF
1808
1809error:
51fac838 1810 toshiba_acpi_remove(acpi_dev);
135740de
SF
1811 return ret;
1812}
1813
1814static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
1815{
1816 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
1817 u32 hci_result, value;
11948b93 1818 int retries = 3;
29cd293f 1819 int scancode;
135740de 1820
29cd293f 1821 if (event != 0x80)
135740de 1822 return;
11948b93 1823
29cd293f
SF
1824 if (dev->info_supported) {
1825 scancode = toshiba_acpi_query_hotkey(dev);
1826 if (scancode < 0)
1827 pr_err("Failed to query hotkey event\n");
1828 else if (scancode != 0)
1829 toshiba_acpi_report_hotkey(dev, scancode);
1830 } else if (dev->system_event_supported) {
1831 do {
1832 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result);
1833 switch (hci_result) {
1834 case HCI_SUCCESS:
1835 toshiba_acpi_report_hotkey(dev, (int)value);
1836 break;
1837 case HCI_NOT_SUPPORTED:
1838 /*
1839 * This is a workaround for an unresolved
1840 * issue on some machines where system events
1841 * sporadically become disabled.
1842 */
1843 hci_write1(dev, HCI_SYSTEM_EVENT, 1,
1844 &hci_result);
1845 pr_notice("Re-enabled hotkeys\n");
1846 /* fall through */
1847 default:
1848 retries--;
1849 break;
135740de 1850 }
29cd293f
SF
1851 } while (retries && hci_result != HCI_EMPTY);
1852 }
135740de
SF
1853}
1854
3567a4e2 1855#ifdef CONFIG_PM_SLEEP
43d2fd3b 1856static int toshiba_acpi_suspend(struct device *device)
29cd293f 1857{
43d2fd3b 1858 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
29cd293f
SF
1859 u32 result;
1860
1861 if (dev->hotkey_dev)
1862 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result);
1863
1864 return 0;
1865}
1866
43d2fd3b 1867static int toshiba_acpi_resume(struct device *device)
29cd293f 1868{
43d2fd3b 1869 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
29cd293f 1870 u32 result;
e7fdb762
BT
1871 acpi_status status;
1872
1873 if (dev->hotkey_dev) {
1874 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB",
1875 NULL, NULL);
1876 if (ACPI_FAILURE(status))
1877 pr_info("Unable to re-enable hotkeys\n");
29cd293f 1878
29cd293f 1879 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result);
e7fdb762 1880 }
29cd293f
SF
1881
1882 return 0;
1883}
3567a4e2 1884#endif
135740de 1885
43d2fd3b
RW
1886static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
1887 toshiba_acpi_suspend, toshiba_acpi_resume);
1888
135740de
SF
1889static struct acpi_driver toshiba_acpi_driver = {
1890 .name = "Toshiba ACPI driver",
1891 .owner = THIS_MODULE,
1892 .ids = toshiba_device_ids,
1893 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1894 .ops = {
1895 .add = toshiba_acpi_add,
1896 .remove = toshiba_acpi_remove,
1897 .notify = toshiba_acpi_notify,
1898 },
43d2fd3b 1899 .drv.pm = &toshiba_acpi_pm,
135740de
SF
1900};
1901
1902static int __init toshiba_acpi_init(void)
1903{
1904 int ret;
1905
f11f999e
SF
1906 /*
1907 * Machines with this WMI guid aren't supported due to bugs in
1908 * their AML. This check relies on wmi initializing before
1909 * toshiba_acpi to guarantee guids have been identified.
1910 */
1911 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
1912 return -ENODEV;
1913
135740de
SF
1914 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
1915 if (!toshiba_proc_dir) {
1916 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
1917 return -ENODEV;
1918 }
1919
1920 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
1921 if (ret) {
1922 pr_err("Failed to register ACPI driver: %d\n", ret);
1923 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1924 }
1925
1926 return ret;
1927}
1928
1929static void __exit toshiba_acpi_exit(void)
1930{
1931 acpi_bus_unregister_driver(&toshiba_acpi_driver);
1932 if (toshiba_proc_dir)
1933 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
1da177e4
LT
1934}
1935
1936module_init(toshiba_acpi_init);
1937module_exit(toshiba_acpi_exit);