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