gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / platform / x86 / toshiba_acpi.c
blob35882dd690a648796d356994bb102fb8a69ba9bd
1 /*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
4 * Copyright (C) 2002-2004 John Belmonte
5 * Copyright (C) 2008 Philip Langdale
6 * Copyright (C) 2010 Pierre Ducroquet
7 * Copyright (C) 2014-2015 Azael Avalos
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
25 * Credits:
26 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27 * engineering the Windows drivers
28 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29 * Rob Miller - TV out and hotkeys help
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #define TOSHIBA_ACPI_VERSION "0.21"
35 #define PROC_INTERFACE_VERSION 1
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/backlight.h>
44 #include <linux/rfkill.h>
45 #include <linux/input.h>
46 #include <linux/input/sparse-keymap.h>
47 #include <linux/leds.h>
48 #include <linux/slab.h>
49 #include <linux/workqueue.h>
50 #include <linux/i8042.h>
51 #include <linux/acpi.h>
52 #include <linux/dmi.h>
53 #include <linux/uaccess.h>
54 #include <acpi/video.h>
56 MODULE_AUTHOR("John Belmonte");
57 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
58 MODULE_LICENSE("GPL");
60 #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
62 /* Scan code for Fn key on TOS1900 models */
63 #define TOS1900_FN_SCAN 0x6e
65 /* Toshiba ACPI method paths */
66 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
69 * The Toshiba configuration interface is composed of the HCI and the SCI,
70 * which are defined as follows:
72 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
73 * be uniform across all their models. Ideally we would just call
74 * dedicated ACPI methods instead of using this primitive interface.
75 * However the ACPI methods seem to be incomplete in some areas (for
76 * example they allow setting, but not reading, the LCD brightness value),
77 * so this is still useful.
79 * SCI stands for "System Configuration Interface" which aim is to
80 * conceal differences in hardware between different models.
83 #define TCI_WORDS 6
85 /* operations */
86 #define HCI_SET 0xff00
87 #define HCI_GET 0xfe00
88 #define SCI_OPEN 0xf100
89 #define SCI_CLOSE 0xf200
90 #define SCI_GET 0xf300
91 #define SCI_SET 0xf400
93 /* return codes */
94 #define TOS_SUCCESS 0x0000
95 #define TOS_OPEN_CLOSE_OK 0x0044
96 #define TOS_FAILURE 0x1000
97 #define TOS_NOT_SUPPORTED 0x8000
98 #define TOS_ALREADY_OPEN 0x8100
99 #define TOS_NOT_OPENED 0x8200
100 #define TOS_INPUT_DATA_ERROR 0x8300
101 #define TOS_WRITE_PROTECTED 0x8400
102 #define TOS_NOT_PRESENT 0x8600
103 #define TOS_FIFO_EMPTY 0x8c00
104 #define TOS_DATA_NOT_AVAILABLE 0x8d20
105 #define TOS_NOT_INITIALIZED 0x8d50
106 #define TOS_NOT_INSTALLED 0x8e00
108 /* registers */
109 #define HCI_FAN 0x0004
110 #define HCI_TR_BACKLIGHT 0x0005
111 #define HCI_SYSTEM_EVENT 0x0016
112 #define HCI_VIDEO_OUT 0x001c
113 #define HCI_HOTKEY_EVENT 0x001e
114 #define HCI_LCD_BRIGHTNESS 0x002a
115 #define HCI_WIRELESS 0x0056
116 #define HCI_ACCELEROMETER 0x006d
117 #define HCI_KBD_ILLUMINATION 0x0095
118 #define HCI_ECO_MODE 0x0097
119 #define HCI_ACCELEROMETER2 0x00a6
120 #define HCI_SYSTEM_INFO 0xc000
121 #define SCI_PANEL_POWER_ON 0x010d
122 #define SCI_ILLUMINATION 0x014e
123 #define SCI_USB_SLEEP_CHARGE 0x0150
124 #define SCI_KBD_ILLUM_STATUS 0x015c
125 #define SCI_USB_SLEEP_MUSIC 0x015e
126 #define SCI_USB_THREE 0x0169
127 #define SCI_TOUCHPAD 0x050e
128 #define SCI_KBD_FUNCTION_KEYS 0x0522
130 /* field definitions */
131 #define HCI_ACCEL_MASK 0x7fff
132 #define HCI_HOTKEY_DISABLE 0x0b
133 #define HCI_HOTKEY_ENABLE 0x09
134 #define HCI_HOTKEY_SPECIAL_FUNCTIONS 0x10
135 #define HCI_LCD_BRIGHTNESS_BITS 3
136 #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
137 #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
138 #define HCI_MISC_SHIFT 0x10
139 #define HCI_SYSTEM_TYPE1 0x10
140 #define HCI_SYSTEM_TYPE2 0x11
141 #define HCI_VIDEO_OUT_LCD 0x1
142 #define HCI_VIDEO_OUT_CRT 0x2
143 #define HCI_VIDEO_OUT_TV 0x4
144 #define HCI_WIRELESS_KILL_SWITCH 0x01
145 #define HCI_WIRELESS_BT_PRESENT 0x0f
146 #define HCI_WIRELESS_BT_ATTACH 0x40
147 #define HCI_WIRELESS_BT_POWER 0x80
148 #define SCI_KBD_MODE_MASK 0x1f
149 #define SCI_KBD_MODE_FNZ 0x1
150 #define SCI_KBD_MODE_AUTO 0x2
151 #define SCI_KBD_MODE_ON 0x8
152 #define SCI_KBD_MODE_OFF 0x10
153 #define SCI_KBD_TIME_MAX 0x3c001a
154 #define SCI_USB_CHARGE_MODE_MASK 0xff
155 #define SCI_USB_CHARGE_DISABLED 0x00
156 #define SCI_USB_CHARGE_ALTERNATE 0x09
157 #define SCI_USB_CHARGE_TYPICAL 0x11
158 #define SCI_USB_CHARGE_AUTO 0x21
159 #define SCI_USB_CHARGE_BAT_MASK 0x7
160 #define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
161 #define SCI_USB_CHARGE_BAT_LVL_ON 0x4
162 #define SCI_USB_CHARGE_BAT_LVL 0x0200
163 #define SCI_USB_CHARGE_RAPID_DSP 0x0300
165 struct toshiba_acpi_dev {
166 struct acpi_device *acpi_dev;
167 const char *method_hci;
168 struct rfkill *bt_rfk;
169 struct input_dev *hotkey_dev;
170 struct work_struct hotkey_work;
171 struct backlight_device *backlight_dev;
172 struct led_classdev led_dev;
173 struct led_classdev kbd_led;
174 struct led_classdev eco_led;
176 int force_fan;
177 int last_key_event;
178 int key_event_valid;
179 int kbd_type;
180 int kbd_mode;
181 int kbd_time;
182 int usbsc_bat_level;
183 int usbsc_mode_base;
184 int hotkey_event_type;
186 unsigned int illumination_supported:1;
187 unsigned int video_supported:1;
188 unsigned int fan_supported:1;
189 unsigned int system_event_supported:1;
190 unsigned int ntfy_supported:1;
191 unsigned int info_supported:1;
192 unsigned int tr_backlight_supported:1;
193 unsigned int kbd_illum_supported:1;
194 unsigned int kbd_led_registered:1;
195 unsigned int touchpad_supported:1;
196 unsigned int eco_supported:1;
197 unsigned int accelerometer_supported:1;
198 unsigned int usb_sleep_charge_supported:1;
199 unsigned int usb_rapid_charge_supported:1;
200 unsigned int usb_sleep_music_supported:1;
201 unsigned int kbd_function_keys_supported:1;
202 unsigned int panel_power_on_supported:1;
203 unsigned int usb_three_supported:1;
204 unsigned int sysfs_created:1;
206 struct mutex mutex;
209 static struct toshiba_acpi_dev *toshiba_acpi;
211 static const struct acpi_device_id toshiba_device_ids[] = {
212 {"TOS6200", 0},
213 {"TOS6207", 0},
214 {"TOS6208", 0},
215 {"TOS1900", 0},
216 {"", 0},
218 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
220 static const struct key_entry toshiba_acpi_keymap[] = {
221 { KE_KEY, 0x9e, { KEY_RFKILL } },
222 { KE_KEY, 0x101, { KEY_MUTE } },
223 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
224 { KE_KEY, 0x103, { KEY_ZOOMIN } },
225 { KE_KEY, 0x10f, { KEY_TAB } },
226 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
227 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
228 { KE_KEY, 0x13b, { KEY_COFFEE } },
229 { KE_KEY, 0x13c, { KEY_BATTERY } },
230 { KE_KEY, 0x13d, { KEY_SLEEP } },
231 { KE_KEY, 0x13e, { KEY_SUSPEND } },
232 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
233 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
234 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
235 { KE_KEY, 0x142, { KEY_WLAN } },
236 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
237 { KE_KEY, 0x17f, { KEY_FN } },
238 { KE_KEY, 0xb05, { KEY_PROG2 } },
239 { KE_KEY, 0xb06, { KEY_WWW } },
240 { KE_KEY, 0xb07, { KEY_MAIL } },
241 { KE_KEY, 0xb30, { KEY_STOP } },
242 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
243 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
244 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
245 { KE_KEY, 0xb5a, { KEY_MEDIA } },
246 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
247 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
248 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
249 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
250 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
251 { KE_END, 0 },
254 static const struct key_entry toshiba_acpi_alt_keymap[] = {
255 { KE_KEY, 0x157, { KEY_MUTE } },
256 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
257 { KE_KEY, 0x103, { KEY_ZOOMIN } },
258 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
259 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
260 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
261 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
262 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
263 { KE_KEY, 0x158, { KEY_WLAN } },
264 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
265 { KE_END, 0 },
269 * List of models which have a broken acpi-video backlight interface and thus
270 * need to use the toshiba (vendor) interface instead.
272 static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
277 * Utility
280 static inline void _set_bit(u32 *word, u32 mask, int value)
282 *word = (*word & ~mask) | (mask * value);
286 * ACPI interface wrappers
289 static int write_acpi_int(const char *methodName, int val)
291 acpi_status status;
293 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
294 return (status == AE_OK) ? 0 : -EIO;
298 * Perform a raw configuration call. Here we don't care about input or output
299 * buffer format.
301 static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
302 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
304 struct acpi_object_list params;
305 union acpi_object in_objs[TCI_WORDS];
306 struct acpi_buffer results;
307 union acpi_object out_objs[TCI_WORDS + 1];
308 acpi_status status;
309 int i;
311 params.count = TCI_WORDS;
312 params.pointer = in_objs;
313 for (i = 0; i < TCI_WORDS; ++i) {
314 in_objs[i].type = ACPI_TYPE_INTEGER;
315 in_objs[i].integer.value = in[i];
318 results.length = sizeof(out_objs);
319 results.pointer = out_objs;
321 status = acpi_evaluate_object(dev->acpi_dev->handle,
322 (char *)dev->method_hci, &params,
323 &results);
324 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
325 for (i = 0; i < out_objs->package.count; ++i)
326 out[i] = out_objs->package.elements[i].integer.value;
329 return status;
333 * Common hci tasks (get or set one or two value)
335 * In addition to the ACPI status, the HCI system returns a result which
336 * may be useful (such as "not supported").
339 static u32 hci_write1(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
341 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
342 u32 out[TCI_WORDS];
343 acpi_status status = tci_raw(dev, in, out);
345 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
348 static u32 hci_read1(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
350 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
351 u32 out[TCI_WORDS];
352 acpi_status status = tci_raw(dev, in, out);
354 if (ACPI_FAILURE(status))
355 return TOS_FAILURE;
357 *out1 = out[2];
359 return out[0];
362 static u32 hci_write2(struct toshiba_acpi_dev *dev, u32 reg, u32 in1, u32 in2)
364 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
365 u32 out[TCI_WORDS];
366 acpi_status status = tci_raw(dev, in, out);
368 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
371 static u32 hci_read2(struct toshiba_acpi_dev *dev,
372 u32 reg, u32 *out1, u32 *out2)
374 u32 in[TCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
375 u32 out[TCI_WORDS];
376 acpi_status status = tci_raw(dev, in, out);
378 if (ACPI_FAILURE(status))
379 return TOS_FAILURE;
381 *out1 = out[2];
382 *out2 = out[3];
384 return out[0];
388 * Common sci tasks
391 static int sci_open(struct toshiba_acpi_dev *dev)
393 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
394 u32 out[TCI_WORDS];
395 acpi_status status;
397 status = tci_raw(dev, in, out);
398 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
399 pr_err("ACPI call to open SCI failed\n");
400 return 0;
403 if (out[0] == TOS_OPEN_CLOSE_OK) {
404 return 1;
405 } else if (out[0] == TOS_ALREADY_OPEN) {
406 pr_info("Toshiba SCI already opened\n");
407 return 1;
408 } else if (out[0] == TOS_NOT_SUPPORTED) {
410 * Some BIOSes do not have the SCI open/close functions
411 * implemented and return 0x8000 (Not Supported), failing to
412 * register some supported features.
414 * Simply return 1 if we hit those affected laptops to make the
415 * supported features work.
417 * In the case that some laptops really do not support the SCI,
418 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
419 * and thus, not registering support for the queried feature.
421 return 1;
422 } else if (out[0] == TOS_NOT_PRESENT) {
423 pr_info("Toshiba SCI is not present\n");
426 return 0;
429 static void sci_close(struct toshiba_acpi_dev *dev)
431 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
432 u32 out[TCI_WORDS];
433 acpi_status status;
435 status = tci_raw(dev, in, out);
436 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
437 pr_err("ACPI call to close SCI failed\n");
438 return;
441 if (out[0] == TOS_OPEN_CLOSE_OK)
442 return;
443 else if (out[0] == TOS_NOT_OPENED)
444 pr_info("Toshiba SCI not opened\n");
445 else if (out[0] == TOS_NOT_PRESENT)
446 pr_info("Toshiba SCI is not present\n");
449 static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
451 u32 in[TCI_WORDS] = { SCI_GET, reg, 0, 0, 0, 0 };
452 u32 out[TCI_WORDS];
453 acpi_status status = tci_raw(dev, in, out);
455 if (ACPI_FAILURE(status))
456 return TOS_FAILURE;
458 *out1 = out[2];
460 return out[0];
463 static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
465 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
466 u32 out[TCI_WORDS];
467 acpi_status status = tci_raw(dev, in, out);
469 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
472 /* Illumination support */
473 static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
475 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
476 u32 out[TCI_WORDS];
477 acpi_status status;
479 if (!sci_open(dev))
480 return 0;
482 status = tci_raw(dev, in, out);
483 sci_close(dev);
484 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
485 pr_err("ACPI call to query Illumination support failed\n");
486 return 0;
487 } else if (out[0] == TOS_NOT_SUPPORTED) {
488 pr_info("Illumination device not available\n");
489 return 0;
492 return 1;
495 static void toshiba_illumination_set(struct led_classdev *cdev,
496 enum led_brightness brightness)
498 struct toshiba_acpi_dev *dev = container_of(cdev,
499 struct toshiba_acpi_dev, led_dev);
500 u32 state, result;
502 /* First request : initialize communication. */
503 if (!sci_open(dev))
504 return;
506 /* Switch the illumination on/off */
507 state = brightness ? 1 : 0;
508 result = sci_write(dev, SCI_ILLUMINATION, state);
509 sci_close(dev);
510 if (result == TOS_FAILURE) {
511 pr_err("ACPI call for illumination failed\n");
512 return;
513 } else if (result == TOS_NOT_SUPPORTED) {
514 pr_info("Illumination not supported\n");
515 return;
519 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
521 struct toshiba_acpi_dev *dev = container_of(cdev,
522 struct toshiba_acpi_dev, led_dev);
523 u32 state, result;
525 /* First request : initialize communication. */
526 if (!sci_open(dev))
527 return LED_OFF;
529 /* Check the illumination */
530 result = sci_read(dev, SCI_ILLUMINATION, &state);
531 sci_close(dev);
532 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
533 pr_err("ACPI call for illumination failed\n");
534 return LED_OFF;
535 } else if (result == TOS_NOT_SUPPORTED) {
536 pr_info("Illumination not supported\n");
537 return LED_OFF;
540 return state ? LED_FULL : LED_OFF;
543 /* KBD Illumination */
544 static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
546 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
547 u32 out[TCI_WORDS];
548 acpi_status status;
550 if (!sci_open(dev))
551 return 0;
553 status = tci_raw(dev, in, out);
554 sci_close(dev);
555 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
556 pr_err("ACPI call to query kbd illumination support failed\n");
557 return 0;
558 } else if (out[0] == TOS_NOT_SUPPORTED) {
559 pr_info("Keyboard illumination not available\n");
560 return 0;
564 * Check for keyboard backlight timeout max value,
565 * previous kbd backlight implementation set this to
566 * 0x3c0003, and now the new implementation set this
567 * to 0x3c001a, use this to distinguish between them.
569 if (out[3] == SCI_KBD_TIME_MAX)
570 dev->kbd_type = 2;
571 else
572 dev->kbd_type = 1;
573 /* Get the current keyboard backlight mode */
574 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
575 /* Get the current time (1-60 seconds) */
576 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
578 return 1;
581 static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
583 u32 result;
585 if (!sci_open(dev))
586 return -EIO;
588 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
589 sci_close(dev);
590 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
591 pr_err("ACPI call to set KBD backlight status failed\n");
592 return -EIO;
593 } else if (result == TOS_NOT_SUPPORTED) {
594 pr_info("Keyboard backlight status not supported\n");
595 return -ENODEV;
598 return 0;
601 static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
603 u32 result;
605 if (!sci_open(dev))
606 return -EIO;
608 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
609 sci_close(dev);
610 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
611 pr_err("ACPI call to get KBD backlight status failed\n");
612 return -EIO;
613 } else if (result == TOS_NOT_SUPPORTED) {
614 pr_info("Keyboard backlight status not supported\n");
615 return -ENODEV;
618 return 0;
621 static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
623 struct toshiba_acpi_dev *dev = container_of(cdev,
624 struct toshiba_acpi_dev, kbd_led);
625 u32 state, result;
627 /* Check the keyboard backlight state */
628 result = hci_read1(dev, HCI_KBD_ILLUMINATION, &state);
629 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
630 pr_err("ACPI call to get the keyboard backlight failed\n");
631 return LED_OFF;
632 } else if (result == TOS_NOT_SUPPORTED) {
633 pr_info("Keyboard backlight not supported\n");
634 return LED_OFF;
637 return state ? LED_FULL : LED_OFF;
640 static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
641 enum led_brightness brightness)
643 struct toshiba_acpi_dev *dev = container_of(cdev,
644 struct toshiba_acpi_dev, kbd_led);
645 u32 state, result;
647 /* Set the keyboard backlight state */
648 state = brightness ? 1 : 0;
649 result = hci_write1(dev, HCI_KBD_ILLUMINATION, state);
650 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
651 pr_err("ACPI call to set KBD Illumination mode failed\n");
652 return;
653 } else if (result == TOS_NOT_SUPPORTED) {
654 pr_info("Keyboard backlight not supported\n");
655 return;
659 /* TouchPad support */
660 static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
662 u32 result;
664 if (!sci_open(dev))
665 return -EIO;
667 result = sci_write(dev, SCI_TOUCHPAD, state);
668 sci_close(dev);
669 if (result == TOS_FAILURE) {
670 pr_err("ACPI call to set the touchpad failed\n");
671 return -EIO;
672 } else if (result == TOS_NOT_SUPPORTED) {
673 return -ENODEV;
676 return 0;
679 static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
681 u32 result;
683 if (!sci_open(dev))
684 return -EIO;
686 result = sci_read(dev, SCI_TOUCHPAD, state);
687 sci_close(dev);
688 if (result == TOS_FAILURE) {
689 pr_err("ACPI call to query the touchpad failed\n");
690 return -EIO;
691 } else if (result == TOS_NOT_SUPPORTED) {
692 return -ENODEV;
695 return 0;
698 /* Eco Mode support */
699 static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
701 acpi_status status;
702 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
703 u32 out[TCI_WORDS];
705 status = tci_raw(dev, in, out);
706 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
707 pr_err("ACPI call to get ECO led failed\n");
708 } else if (out[0] == TOS_NOT_INSTALLED) {
709 pr_info("ECO led not installed");
710 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
712 * If we receive 0x8300 (Input Data Error), it means that the
713 * LED device is present, but that we just screwed the input
714 * parameters.
716 * Let's query the status of the LED to see if we really have a
717 * success response, indicating the actual presense of the LED,
718 * bail out otherwise.
720 in[3] = 1;
721 status = tci_raw(dev, in, out);
722 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
723 pr_err("ACPI call to get ECO led failed\n");
724 else if (out[0] == TOS_SUCCESS)
725 return 1;
728 return 0;
731 static enum led_brightness
732 toshiba_eco_mode_get_status(struct led_classdev *cdev)
734 struct toshiba_acpi_dev *dev = container_of(cdev,
735 struct toshiba_acpi_dev, eco_led);
736 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
737 u32 out[TCI_WORDS];
738 acpi_status status;
740 status = tci_raw(dev, in, out);
741 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
742 pr_err("ACPI call to get ECO led failed\n");
743 return LED_OFF;
746 return out[2] ? LED_FULL : LED_OFF;
749 static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
750 enum led_brightness brightness)
752 struct toshiba_acpi_dev *dev = container_of(cdev,
753 struct toshiba_acpi_dev, eco_led);
754 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
755 u32 out[TCI_WORDS];
756 acpi_status status;
758 /* Switch the Eco Mode led on/off */
759 in[2] = (brightness) ? 1 : 0;
760 status = tci_raw(dev, in, out);
761 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
762 pr_err("ACPI call to set ECO led failed\n");
763 return;
767 /* Accelerometer support */
768 static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
770 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
771 u32 out[TCI_WORDS];
772 acpi_status status;
775 * Check if the accelerometer call exists,
776 * this call also serves as initialization
778 status = tci_raw(dev, in, out);
779 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
780 pr_err("ACPI call to query the accelerometer failed\n");
781 return -EIO;
782 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
783 out[0] == TOS_NOT_INITIALIZED) {
784 pr_err("Accelerometer not initialized\n");
785 return -EIO;
786 } else if (out[0] == TOS_NOT_SUPPORTED) {
787 pr_info("Accelerometer not supported\n");
788 return -ENODEV;
791 return 0;
794 static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
795 u32 *xy, u32 *z)
797 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
798 u32 out[TCI_WORDS];
799 acpi_status status;
801 /* Check the Accelerometer status */
802 status = tci_raw(dev, in, out);
803 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
804 pr_err("ACPI call to query the accelerometer failed\n");
805 return -EIO;
808 *xy = out[2];
809 *z = out[4];
811 return 0;
814 /* Sleep (Charge and Music) utilities support */
815 static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
817 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
818 u32 out[TCI_WORDS];
819 acpi_status status;
821 /* Set the feature to "not supported" in case of error */
822 dev->usb_sleep_charge_supported = 0;
824 if (!sci_open(dev))
825 return;
827 status = tci_raw(dev, in, out);
828 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
829 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
830 sci_close(dev);
831 return;
832 } else if (out[0] == TOS_NOT_SUPPORTED) {
833 pr_info("USB Sleep and Charge not supported\n");
834 sci_close(dev);
835 return;
836 } else if (out[0] == TOS_SUCCESS) {
837 dev->usbsc_mode_base = out[4];
840 in[5] = SCI_USB_CHARGE_BAT_LVL;
841 status = tci_raw(dev, in, out);
842 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
843 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
844 sci_close(dev);
845 return;
846 } else if (out[0] == TOS_NOT_SUPPORTED) {
847 pr_info("USB Sleep and Charge not supported\n");
848 sci_close(dev);
849 return;
850 } else if (out[0] == TOS_SUCCESS) {
851 dev->usbsc_bat_level = out[2];
853 * If we reach this point, it means that the laptop has support
854 * for this feature and all values are initialized.
855 * Set it as supported.
857 dev->usb_sleep_charge_supported = 1;
860 sci_close(dev);
863 static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
864 u32 *mode)
866 u32 result;
868 if (!sci_open(dev))
869 return -EIO;
871 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
872 sci_close(dev);
873 if (result == TOS_FAILURE) {
874 pr_err("ACPI call to set USB S&C mode failed\n");
875 return -EIO;
876 } else if (result == TOS_NOT_SUPPORTED) {
877 pr_info("USB Sleep and Charge not supported\n");
878 return -ENODEV;
879 } else if (result == TOS_INPUT_DATA_ERROR) {
880 return -EIO;
883 return 0;
886 static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
887 u32 mode)
889 u32 result;
891 if (!sci_open(dev))
892 return -EIO;
894 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
895 sci_close(dev);
896 if (result == TOS_FAILURE) {
897 pr_err("ACPI call to set USB S&C mode failed\n");
898 return -EIO;
899 } else if (result == TOS_NOT_SUPPORTED) {
900 pr_info("USB Sleep and Charge not supported\n");
901 return -ENODEV;
902 } else if (result == TOS_INPUT_DATA_ERROR) {
903 return -EIO;
906 return 0;
909 static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
910 u32 *mode)
912 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
913 u32 out[TCI_WORDS];
914 acpi_status status;
916 if (!sci_open(dev))
917 return -EIO;
919 in[5] = SCI_USB_CHARGE_BAT_LVL;
920 status = tci_raw(dev, in, out);
921 sci_close(dev);
922 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
923 pr_err("ACPI call to get USB S&C battery level failed\n");
924 return -EIO;
925 } else if (out[0] == TOS_NOT_SUPPORTED) {
926 pr_info("USB Sleep and Charge not supported\n");
927 return -ENODEV;
928 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
929 return -EIO;
932 *mode = out[2];
934 return 0;
937 static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
938 u32 mode)
940 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
941 u32 out[TCI_WORDS];
942 acpi_status status;
944 if (!sci_open(dev))
945 return -EIO;
947 in[2] = mode;
948 in[5] = SCI_USB_CHARGE_BAT_LVL;
949 status = tci_raw(dev, in, out);
950 sci_close(dev);
951 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
952 pr_err("ACPI call to set USB S&C battery level failed\n");
953 return -EIO;
954 } else if (out[0] == TOS_NOT_SUPPORTED) {
955 pr_info("USB Sleep and Charge not supported\n");
956 return -ENODEV;
957 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
958 return -EIO;
961 return 0;
964 static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
965 u32 *state)
967 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
968 u32 out[TCI_WORDS];
969 acpi_status status;
971 if (!sci_open(dev))
972 return -EIO;
974 in[5] = SCI_USB_CHARGE_RAPID_DSP;
975 status = tci_raw(dev, in, out);
976 sci_close(dev);
977 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
978 pr_err("ACPI call to get USB Rapid Charge failed\n");
979 return -EIO;
980 } else if (out[0] == TOS_NOT_SUPPORTED ||
981 out[0] == TOS_INPUT_DATA_ERROR) {
982 pr_info("USB Rapid Charge not supported\n");
983 return -ENODEV;
986 *state = out[2];
988 return 0;
991 static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
992 u32 state)
994 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
995 u32 out[TCI_WORDS];
996 acpi_status status;
998 if (!sci_open(dev))
999 return -EIO;
1001 in[2] = state;
1002 in[5] = SCI_USB_CHARGE_RAPID_DSP;
1003 status = tci_raw(dev, in, out);
1004 sci_close(dev);
1005 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE) {
1006 pr_err("ACPI call to set USB Rapid Charge failed\n");
1007 return -EIO;
1008 } else if (out[0] == TOS_NOT_SUPPORTED) {
1009 pr_info("USB Rapid Charge not supported\n");
1010 return -ENODEV;
1011 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
1012 return -EIO;
1015 return 0;
1018 static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
1020 u32 result;
1022 if (!sci_open(dev))
1023 return -EIO;
1025 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
1026 sci_close(dev);
1027 if (result == TOS_FAILURE) {
1028 pr_err("ACPI call to get Sleep and Music failed\n");
1029 return -EIO;
1030 } else if (result == TOS_NOT_SUPPORTED) {
1031 pr_info("Sleep and Music not supported\n");
1032 return -ENODEV;
1033 } else if (result == TOS_INPUT_DATA_ERROR) {
1034 return -EIO;
1037 return 0;
1040 static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1042 u32 result;
1044 if (!sci_open(dev))
1045 return -EIO;
1047 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1048 sci_close(dev);
1049 if (result == TOS_FAILURE) {
1050 pr_err("ACPI call to set Sleep and Music failed\n");
1051 return -EIO;
1052 } else if (result == TOS_NOT_SUPPORTED) {
1053 pr_info("Sleep and Music not supported\n");
1054 return -ENODEV;
1055 } else if (result == TOS_INPUT_DATA_ERROR) {
1056 return -EIO;
1059 return 0;
1062 /* Keyboard function keys */
1063 static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1065 u32 result;
1067 if (!sci_open(dev))
1068 return -EIO;
1070 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1071 sci_close(dev);
1072 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1073 pr_err("ACPI call to get KBD function keys failed\n");
1074 return -EIO;
1075 } else if (result == TOS_NOT_SUPPORTED) {
1076 pr_info("KBD function keys not supported\n");
1077 return -ENODEV;
1080 return 0;
1083 static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1085 u32 result;
1087 if (!sci_open(dev))
1088 return -EIO;
1090 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1091 sci_close(dev);
1092 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1093 pr_err("ACPI call to set KBD function keys failed\n");
1094 return -EIO;
1095 } else if (result == TOS_NOT_SUPPORTED) {
1096 pr_info("KBD function keys not supported\n");
1097 return -ENODEV;
1100 return 0;
1103 /* Panel Power ON */
1104 static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1106 u32 result;
1108 if (!sci_open(dev))
1109 return -EIO;
1111 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1112 sci_close(dev);
1113 if (result == TOS_FAILURE) {
1114 pr_err("ACPI call to get Panel Power ON failed\n");
1115 return -EIO;
1116 } else if (result == TOS_NOT_SUPPORTED) {
1117 pr_info("Panel Power on not supported\n");
1118 return -ENODEV;
1119 } else if (result == TOS_INPUT_DATA_ERROR) {
1120 return -EIO;
1123 return 0;
1126 static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1128 u32 result;
1130 if (!sci_open(dev))
1131 return -EIO;
1133 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1134 sci_close(dev);
1135 if (result == TOS_FAILURE) {
1136 pr_err("ACPI call to set Panel Power ON failed\n");
1137 return -EIO;
1138 } else if (result == TOS_NOT_SUPPORTED) {
1139 pr_info("Panel Power ON not supported\n");
1140 return -ENODEV;
1141 } else if (result == TOS_INPUT_DATA_ERROR) {
1142 return -EIO;
1145 return 0;
1148 /* USB Three */
1149 static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1151 u32 result;
1153 if (!sci_open(dev))
1154 return -EIO;
1156 result = sci_read(dev, SCI_USB_THREE, state);
1157 sci_close(dev);
1158 if (result == TOS_FAILURE) {
1159 pr_err("ACPI call to get USB 3 failed\n");
1160 return -EIO;
1161 } else if (result == TOS_NOT_SUPPORTED) {
1162 pr_info("USB 3 not supported\n");
1163 return -ENODEV;
1164 } else if (result == TOS_INPUT_DATA_ERROR) {
1165 return -EIO;
1168 return 0;
1171 static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1173 u32 result;
1175 if (!sci_open(dev))
1176 return -EIO;
1178 result = sci_write(dev, SCI_USB_THREE, state);
1179 sci_close(dev);
1180 if (result == TOS_FAILURE) {
1181 pr_err("ACPI call to set USB 3 failed\n");
1182 return -EIO;
1183 } else if (result == TOS_NOT_SUPPORTED) {
1184 pr_info("USB 3 not supported\n");
1185 return -ENODEV;
1186 } else if (result == TOS_INPUT_DATA_ERROR) {
1187 return -EIO;
1190 return 0;
1193 /* Hotkey Event type */
1194 static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1195 u32 *type)
1197 u32 val1 = 0x03;
1198 u32 val2 = 0;
1199 u32 result;
1201 result = hci_read2(dev, HCI_SYSTEM_INFO, &val1, &val2);
1202 if (result == TOS_FAILURE) {
1203 pr_err("ACPI call to get System type failed\n");
1204 return -EIO;
1205 } else if (result == TOS_NOT_SUPPORTED) {
1206 pr_info("System type not supported\n");
1207 return -ENODEV;
1210 *type = val2;
1212 return 0;
1215 /* Bluetooth rfkill handlers */
1217 static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present)
1219 u32 hci_result;
1220 u32 value, value2;
1222 value = 0;
1223 value2 = 0;
1224 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
1225 if (hci_result == TOS_SUCCESS)
1226 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
1228 return hci_result;
1231 static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state)
1233 u32 hci_result;
1234 u32 value, value2;
1236 value = 0;
1237 value2 = 0x0001;
1238 hci_result = hci_read2(dev, HCI_WIRELESS, &value, &value2);
1240 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
1241 return hci_result;
1244 static int bt_rfkill_set_block(void *data, bool blocked)
1246 struct toshiba_acpi_dev *dev = data;
1247 u32 result1, result2;
1248 u32 value;
1249 int err;
1250 bool radio_state;
1252 value = (blocked == false);
1254 mutex_lock(&dev->mutex);
1255 if (hci_get_radio_state(dev, &radio_state) != TOS_SUCCESS) {
1256 err = -EIO;
1257 goto out;
1260 if (!radio_state) {
1261 err = 0;
1262 goto out;
1265 result1 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER);
1266 result2 = hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH);
1268 if (result1 != TOS_SUCCESS || result2 != TOS_SUCCESS)
1269 err = -EIO;
1270 else
1271 err = 0;
1272 out:
1273 mutex_unlock(&dev->mutex);
1274 return err;
1277 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
1279 bool new_rfk_state;
1280 bool value;
1281 u32 hci_result;
1282 struct toshiba_acpi_dev *dev = data;
1284 mutex_lock(&dev->mutex);
1286 hci_result = hci_get_radio_state(dev, &value);
1287 if (hci_result != TOS_SUCCESS) {
1288 /* Can't do anything useful */
1289 mutex_unlock(&dev->mutex);
1290 return;
1293 new_rfk_state = value;
1295 mutex_unlock(&dev->mutex);
1297 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
1298 bt_rfkill_set_block(data, true);
1301 static const struct rfkill_ops toshiba_rfk_ops = {
1302 .set_block = bt_rfkill_set_block,
1303 .poll = bt_rfkill_poll,
1306 static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1308 u32 hci_result;
1309 u32 status;
1311 hci_result = hci_read1(dev, HCI_TR_BACKLIGHT, &status);
1312 *enabled = !status;
1313 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1316 static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1318 u32 hci_result;
1319 u32 value = !enable;
1321 hci_result = hci_write1(dev, HCI_TR_BACKLIGHT, value);
1322 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1325 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/;
1327 static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
1329 u32 hci_result;
1330 u32 value;
1331 int brightness = 0;
1333 if (dev->tr_backlight_supported) {
1334 bool enabled;
1335 int ret = get_tr_backlight_status(dev, &enabled);
1337 if (ret)
1338 return ret;
1339 if (enabled)
1340 return 0;
1341 brightness++;
1344 hci_result = hci_read1(dev, HCI_LCD_BRIGHTNESS, &value);
1345 if (hci_result == TOS_SUCCESS)
1346 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
1348 return -EIO;
1351 static int get_lcd_brightness(struct backlight_device *bd)
1353 struct toshiba_acpi_dev *dev = bl_get_data(bd);
1355 return __get_lcd_brightness(dev);
1358 static int lcd_proc_show(struct seq_file *m, void *v)
1360 struct toshiba_acpi_dev *dev = m->private;
1361 int value;
1362 int levels;
1364 if (!dev->backlight_dev)
1365 return -ENODEV;
1367 levels = dev->backlight_dev->props.max_brightness + 1;
1368 value = get_lcd_brightness(dev->backlight_dev);
1369 if (value >= 0) {
1370 seq_printf(m, "brightness: %d\n", value);
1371 seq_printf(m, "brightness_levels: %d\n", levels);
1372 return 0;
1375 pr_err("Error reading LCD brightness\n");
1376 return -EIO;
1379 static int lcd_proc_open(struct inode *inode, struct file *file)
1381 return single_open(file, lcd_proc_show, PDE_DATA(inode));
1384 static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
1386 u32 hci_result;
1388 if (dev->tr_backlight_supported) {
1389 bool enable = !value;
1390 int ret = set_tr_backlight_status(dev, enable);
1392 if (ret)
1393 return ret;
1394 if (value)
1395 value--;
1398 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1399 hci_result = hci_write1(dev, HCI_LCD_BRIGHTNESS, value);
1400 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1403 static int set_lcd_status(struct backlight_device *bd)
1405 struct toshiba_acpi_dev *dev = bl_get_data(bd);
1407 return set_lcd_brightness(dev, bd->props.brightness);
1410 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1411 size_t count, loff_t *pos)
1413 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1414 char cmd[42];
1415 size_t len;
1416 int value;
1417 int ret;
1418 int levels = dev->backlight_dev->props.max_brightness + 1;
1420 len = min(count, sizeof(cmd) - 1);
1421 if (copy_from_user(cmd, buf, len))
1422 return -EFAULT;
1423 cmd[len] = '\0';
1425 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
1426 value >= 0 && value < levels) {
1427 ret = set_lcd_brightness(dev, value);
1428 if (ret == 0)
1429 ret = count;
1430 } else {
1431 ret = -EINVAL;
1433 return ret;
1436 static const struct file_operations lcd_proc_fops = {
1437 .owner = THIS_MODULE,
1438 .open = lcd_proc_open,
1439 .read = seq_read,
1440 .llseek = seq_lseek,
1441 .release = single_release,
1442 .write = lcd_proc_write,
1445 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1447 u32 hci_result;
1449 hci_result = hci_read1(dev, HCI_VIDEO_OUT, status);
1450 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1453 static int video_proc_show(struct seq_file *m, void *v)
1455 struct toshiba_acpi_dev *dev = m->private;
1456 u32 value;
1457 int ret;
1459 ret = get_video_status(dev, &value);
1460 if (!ret) {
1461 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1462 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
1463 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
1465 seq_printf(m, "lcd_out: %d\n", is_lcd);
1466 seq_printf(m, "crt_out: %d\n", is_crt);
1467 seq_printf(m, "tv_out: %d\n", is_tv);
1470 return ret;
1473 static int video_proc_open(struct inode *inode, struct file *file)
1475 return single_open(file, video_proc_show, PDE_DATA(inode));
1478 static ssize_t video_proc_write(struct file *file, const char __user *buf,
1479 size_t count, loff_t *pos)
1481 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1482 char *cmd, *buffer;
1483 int ret;
1484 int value;
1485 int remain = count;
1486 int lcd_out = -1;
1487 int crt_out = -1;
1488 int tv_out = -1;
1489 u32 video_out;
1491 cmd = kmalloc(count + 1, GFP_KERNEL);
1492 if (!cmd)
1493 return -ENOMEM;
1494 if (copy_from_user(cmd, buf, count)) {
1495 kfree(cmd);
1496 return -EFAULT;
1498 cmd[count] = '\0';
1500 buffer = cmd;
1503 * Scan expression. Multiple expressions may be delimited with ;
1504 * NOTE: To keep scanning simple, invalid fields are ignored.
1506 while (remain) {
1507 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1508 lcd_out = value & 1;
1509 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1510 crt_out = value & 1;
1511 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1512 tv_out = value & 1;
1513 /* Advance to one character past the next ; */
1514 do {
1515 ++buffer;
1516 --remain;
1517 } while (remain && *(buffer - 1) != ';');
1520 kfree(cmd);
1522 ret = get_video_status(dev, &video_out);
1523 if (!ret) {
1524 unsigned int new_video_out = video_out;
1526 if (lcd_out != -1)
1527 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1528 if (crt_out != -1)
1529 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1530 if (tv_out != -1)
1531 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1533 * To avoid unnecessary video disruption, only write the new
1534 * video setting if something changed. */
1535 if (new_video_out != video_out)
1536 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1539 return ret ? ret : count;
1542 static const struct file_operations video_proc_fops = {
1543 .owner = THIS_MODULE,
1544 .open = video_proc_open,
1545 .read = seq_read,
1546 .llseek = seq_lseek,
1547 .release = single_release,
1548 .write = video_proc_write,
1551 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1553 u32 hci_result;
1555 hci_result = hci_read1(dev, HCI_FAN, status);
1556 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1559 static int fan_proc_show(struct seq_file *m, void *v)
1561 struct toshiba_acpi_dev *dev = m->private;
1562 int ret;
1563 u32 value;
1565 ret = get_fan_status(dev, &value);
1566 if (!ret) {
1567 seq_printf(m, "running: %d\n", (value > 0));
1568 seq_printf(m, "force_on: %d\n", dev->force_fan);
1571 return ret;
1574 static int fan_proc_open(struct inode *inode, struct file *file)
1576 return single_open(file, fan_proc_show, PDE_DATA(inode));
1579 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1580 size_t count, loff_t *pos)
1582 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1583 char cmd[42];
1584 size_t len;
1585 int value;
1586 u32 hci_result;
1588 len = min(count, sizeof(cmd) - 1);
1589 if (copy_from_user(cmd, buf, len))
1590 return -EFAULT;
1591 cmd[len] = '\0';
1593 if (sscanf(cmd, " force_on : %i", &value) == 1 &&
1594 value >= 0 && value <= 1) {
1595 hci_result = hci_write1(dev, HCI_FAN, value);
1596 if (hci_result == TOS_SUCCESS)
1597 dev->force_fan = value;
1598 else
1599 return -EIO;
1600 } else {
1601 return -EINVAL;
1604 return count;
1607 static const struct file_operations fan_proc_fops = {
1608 .owner = THIS_MODULE,
1609 .open = fan_proc_open,
1610 .read = seq_read,
1611 .llseek = seq_lseek,
1612 .release = single_release,
1613 .write = fan_proc_write,
1616 static int keys_proc_show(struct seq_file *m, void *v)
1618 struct toshiba_acpi_dev *dev = m->private;
1619 u32 hci_result;
1620 u32 value;
1622 if (!dev->key_event_valid && dev->system_event_supported) {
1623 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
1624 if (hci_result == TOS_SUCCESS) {
1625 dev->key_event_valid = 1;
1626 dev->last_key_event = value;
1627 } else if (hci_result == TOS_FIFO_EMPTY) {
1628 /* Better luck next time */
1629 } else if (hci_result == TOS_NOT_SUPPORTED) {
1631 * This is a workaround for an unresolved issue on
1632 * some machines where system events sporadically
1633 * become disabled.
1635 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
1636 pr_notice("Re-enabled hotkeys\n");
1637 } else {
1638 pr_err("Error reading hotkey status\n");
1639 return -EIO;
1643 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1644 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
1645 return 0;
1648 static int keys_proc_open(struct inode *inode, struct file *file)
1650 return single_open(file, keys_proc_show, PDE_DATA(inode));
1653 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1654 size_t count, loff_t *pos)
1656 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1657 char cmd[42];
1658 size_t len;
1659 int value;
1661 len = min(count, sizeof(cmd) - 1);
1662 if (copy_from_user(cmd, buf, len))
1663 return -EFAULT;
1664 cmd[len] = '\0';
1666 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
1667 dev->key_event_valid = 0;
1668 else
1669 return -EINVAL;
1671 return count;
1674 static const struct file_operations keys_proc_fops = {
1675 .owner = THIS_MODULE,
1676 .open = keys_proc_open,
1677 .read = seq_read,
1678 .llseek = seq_lseek,
1679 .release = single_release,
1680 .write = keys_proc_write,
1683 static int version_proc_show(struct seq_file *m, void *v)
1685 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1686 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1687 return 0;
1690 static int version_proc_open(struct inode *inode, struct file *file)
1692 return single_open(file, version_proc_show, PDE_DATA(inode));
1695 static const struct file_operations version_proc_fops = {
1696 .owner = THIS_MODULE,
1697 .open = version_proc_open,
1698 .read = seq_read,
1699 .llseek = seq_lseek,
1700 .release = single_release,
1704 * Proc and module init
1707 #define PROC_TOSHIBA "toshiba"
1709 static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1711 if (dev->backlight_dev)
1712 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1713 &lcd_proc_fops, dev);
1714 if (dev->video_supported)
1715 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1716 &video_proc_fops, dev);
1717 if (dev->fan_supported)
1718 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1719 &fan_proc_fops, dev);
1720 if (dev->hotkey_dev)
1721 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1722 &keys_proc_fops, dev);
1723 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1724 &version_proc_fops, dev);
1727 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1729 if (dev->backlight_dev)
1730 remove_proc_entry("lcd", toshiba_proc_dir);
1731 if (dev->video_supported)
1732 remove_proc_entry("video", toshiba_proc_dir);
1733 if (dev->fan_supported)
1734 remove_proc_entry("fan", toshiba_proc_dir);
1735 if (dev->hotkey_dev)
1736 remove_proc_entry("keys", toshiba_proc_dir);
1737 remove_proc_entry("version", toshiba_proc_dir);
1740 static const struct backlight_ops toshiba_backlight_data = {
1741 .options = BL_CORE_SUSPENDRESUME,
1742 .get_brightness = get_lcd_brightness,
1743 .update_status = set_lcd_status,
1747 * Sysfs files
1749 static ssize_t version_show(struct device *dev,
1750 struct device_attribute *attr, char *buf)
1752 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1754 static DEVICE_ATTR_RO(version);
1756 static ssize_t fan_store(struct device *dev,
1757 struct device_attribute *attr,
1758 const char *buf, size_t count)
1760 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1761 u32 result;
1762 int state;
1763 int ret;
1765 ret = kstrtoint(buf, 0, &state);
1766 if (ret)
1767 return ret;
1769 if (state != 0 && state != 1)
1770 return -EINVAL;
1772 result = hci_write1(toshiba, HCI_FAN, state);
1773 if (result == TOS_FAILURE)
1774 return -EIO;
1775 else if (result == TOS_NOT_SUPPORTED)
1776 return -ENODEV;
1778 return count;
1781 static ssize_t fan_show(struct device *dev,
1782 struct device_attribute *attr, char *buf)
1784 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1785 u32 value;
1786 int ret;
1788 ret = get_fan_status(toshiba, &value);
1789 if (ret)
1790 return ret;
1792 return sprintf(buf, "%d\n", value);
1794 static DEVICE_ATTR_RW(fan);
1796 static ssize_t kbd_backlight_mode_store(struct device *dev,
1797 struct device_attribute *attr,
1798 const char *buf, size_t count)
1800 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1801 int mode;
1802 int time;
1803 int ret;
1806 ret = kstrtoint(buf, 0, &mode);
1807 if (ret)
1808 return ret;
1810 /* Check for supported modes depending on keyboard backlight type */
1811 if (toshiba->kbd_type == 1) {
1812 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1813 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1814 return -EINVAL;
1815 } else if (toshiba->kbd_type == 2) {
1816 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1817 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1818 mode != SCI_KBD_MODE_OFF)
1819 return -EINVAL;
1823 * Set the Keyboard Backlight Mode where:
1824 * Auto - KBD backlight turns off automatically in given time
1825 * FN-Z - KBD backlight "toggles" when hotkey pressed
1826 * ON - KBD backlight is always on
1827 * OFF - KBD backlight is always off
1830 /* Only make a change if the actual mode has changed */
1831 if (toshiba->kbd_mode != mode) {
1832 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1833 time = toshiba->kbd_time << HCI_MISC_SHIFT;
1835 /* OR the "base time" to the actual method format */
1836 if (toshiba->kbd_type == 1) {
1837 /* Type 1 requires the current mode */
1838 time |= toshiba->kbd_mode;
1839 } else if (toshiba->kbd_type == 2) {
1840 /* Type 2 requires the desired mode */
1841 time |= mode;
1844 ret = toshiba_kbd_illum_status_set(toshiba, time);
1845 if (ret)
1846 return ret;
1848 toshiba->kbd_mode = mode;
1851 return count;
1854 static ssize_t kbd_backlight_mode_show(struct device *dev,
1855 struct device_attribute *attr,
1856 char *buf)
1858 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1859 u32 time;
1861 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1862 return -EIO;
1864 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1866 static DEVICE_ATTR_RW(kbd_backlight_mode);
1868 static ssize_t kbd_type_show(struct device *dev,
1869 struct device_attribute *attr, char *buf)
1871 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1873 return sprintf(buf, "%d\n", toshiba->kbd_type);
1875 static DEVICE_ATTR_RO(kbd_type);
1877 static ssize_t available_kbd_modes_show(struct device *dev,
1878 struct device_attribute *attr,
1879 char *buf)
1881 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1883 if (toshiba->kbd_type == 1)
1884 return sprintf(buf, "%x %x\n",
1885 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1887 return sprintf(buf, "%x %x %x\n",
1888 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
1890 static DEVICE_ATTR_RO(available_kbd_modes);
1892 static ssize_t kbd_backlight_timeout_store(struct device *dev,
1893 struct device_attribute *attr,
1894 const char *buf, size_t count)
1896 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1897 int time;
1898 int ret;
1900 ret = kstrtoint(buf, 0, &time);
1901 if (ret)
1902 return ret;
1904 /* Check for supported values depending on kbd_type */
1905 if (toshiba->kbd_type == 1) {
1906 if (time < 0 || time > 60)
1907 return -EINVAL;
1908 } else if (toshiba->kbd_type == 2) {
1909 if (time < 1 || time > 60)
1910 return -EINVAL;
1913 /* Set the Keyboard Backlight Timeout */
1915 /* Only make a change if the actual timeout has changed */
1916 if (toshiba->kbd_time != time) {
1917 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1918 time = time << HCI_MISC_SHIFT;
1919 /* OR the "base time" to the actual method format */
1920 if (toshiba->kbd_type == 1)
1921 time |= SCI_KBD_MODE_FNZ;
1922 else if (toshiba->kbd_type == 2)
1923 time |= SCI_KBD_MODE_AUTO;
1925 ret = toshiba_kbd_illum_status_set(toshiba, time);
1926 if (ret)
1927 return ret;
1929 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1932 return count;
1935 static ssize_t kbd_backlight_timeout_show(struct device *dev,
1936 struct device_attribute *attr,
1937 char *buf)
1939 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1940 u32 time;
1942 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1943 return -EIO;
1945 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1947 static DEVICE_ATTR_RW(kbd_backlight_timeout);
1949 static ssize_t touchpad_store(struct device *dev,
1950 struct device_attribute *attr,
1951 const char *buf, size_t count)
1953 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1954 int state;
1955 int ret;
1957 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1958 ret = kstrtoint(buf, 0, &state);
1959 if (ret)
1960 return ret;
1961 if (state != 0 && state != 1)
1962 return -EINVAL;
1964 ret = toshiba_touchpad_set(toshiba, state);
1965 if (ret)
1966 return ret;
1968 return count;
1971 static ssize_t touchpad_show(struct device *dev,
1972 struct device_attribute *attr, char *buf)
1974 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1975 u32 state;
1976 int ret;
1978 ret = toshiba_touchpad_get(toshiba, &state);
1979 if (ret < 0)
1980 return ret;
1982 return sprintf(buf, "%i\n", state);
1984 static DEVICE_ATTR_RW(touchpad);
1986 static ssize_t position_show(struct device *dev,
1987 struct device_attribute *attr, char *buf)
1989 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1990 u32 xyval, zval, tmp;
1991 u16 x, y, z;
1992 int ret;
1994 xyval = zval = 0;
1995 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1996 if (ret < 0)
1997 return ret;
1999 x = xyval & HCI_ACCEL_MASK;
2000 tmp = xyval >> HCI_MISC_SHIFT;
2001 y = tmp & HCI_ACCEL_MASK;
2002 z = zval & HCI_ACCEL_MASK;
2004 return sprintf(buf, "%d %d %d\n", x, y, z);
2006 static DEVICE_ATTR_RO(position);
2008 static ssize_t usb_sleep_charge_show(struct device *dev,
2009 struct device_attribute *attr, char *buf)
2011 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2012 u32 mode;
2013 int ret;
2015 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
2016 if (ret < 0)
2017 return ret;
2019 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
2022 static ssize_t usb_sleep_charge_store(struct device *dev,
2023 struct device_attribute *attr,
2024 const char *buf, size_t count)
2026 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2027 u32 mode;
2028 int state;
2029 int ret;
2031 ret = kstrtoint(buf, 0, &state);
2032 if (ret)
2033 return ret;
2035 * Check for supported values, where:
2036 * 0 - Disabled
2037 * 1 - Alternate (Non USB conformant devices that require more power)
2038 * 2 - Auto (USB conformant devices)
2039 * 3 - Typical
2041 if (state != 0 && state != 1 && state != 2 && state != 3)
2042 return -EINVAL;
2044 /* Set the USB charging mode to internal value */
2045 mode = toshiba->usbsc_mode_base;
2046 if (state == 0)
2047 mode |= SCI_USB_CHARGE_DISABLED;
2048 else if (state == 1)
2049 mode |= SCI_USB_CHARGE_ALTERNATE;
2050 else if (state == 2)
2051 mode |= SCI_USB_CHARGE_AUTO;
2052 else if (state == 3)
2053 mode |= SCI_USB_CHARGE_TYPICAL;
2055 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
2056 if (ret)
2057 return ret;
2059 return count;
2061 static DEVICE_ATTR_RW(usb_sleep_charge);
2063 static ssize_t sleep_functions_on_battery_show(struct device *dev,
2064 struct device_attribute *attr,
2065 char *buf)
2067 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2068 u32 state;
2069 int bat_lvl;
2070 int status;
2071 int ret;
2072 int tmp;
2074 ret = toshiba_sleep_functions_status_get(toshiba, &state);
2075 if (ret < 0)
2076 return ret;
2078 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
2079 tmp = state & SCI_USB_CHARGE_BAT_MASK;
2080 status = (tmp == 0x4) ? 1 : 0;
2081 /* Determine the battery level set */
2082 bat_lvl = state >> HCI_MISC_SHIFT;
2084 return sprintf(buf, "%d %d\n", status, bat_lvl);
2087 static ssize_t sleep_functions_on_battery_store(struct device *dev,
2088 struct device_attribute *attr,
2089 const char *buf, size_t count)
2091 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2092 u32 status;
2093 int value;
2094 int ret;
2095 int tmp;
2097 ret = kstrtoint(buf, 0, &value);
2098 if (ret)
2099 return ret;
2102 * Set the status of the function:
2103 * 0 - Disabled
2104 * 1-100 - Enabled
2106 if (value < 0 || value > 100)
2107 return -EINVAL;
2109 if (value == 0) {
2110 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
2111 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
2112 } else {
2113 tmp = value << HCI_MISC_SHIFT;
2114 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
2116 ret = toshiba_sleep_functions_status_set(toshiba, status);
2117 if (ret < 0)
2118 return ret;
2120 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2122 return count;
2124 static DEVICE_ATTR_RW(sleep_functions_on_battery);
2126 static ssize_t usb_rapid_charge_show(struct device *dev,
2127 struct device_attribute *attr, char *buf)
2129 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2130 u32 state;
2131 int ret;
2133 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2134 if (ret < 0)
2135 return ret;
2137 return sprintf(buf, "%d\n", state);
2140 static ssize_t usb_rapid_charge_store(struct device *dev,
2141 struct device_attribute *attr,
2142 const char *buf, size_t count)
2144 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2145 int state;
2146 int ret;
2148 ret = kstrtoint(buf, 0, &state);
2149 if (ret)
2150 return ret;
2151 if (state != 0 && state != 1)
2152 return -EINVAL;
2154 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2155 if (ret)
2156 return ret;
2158 return count;
2160 static DEVICE_ATTR_RW(usb_rapid_charge);
2162 static ssize_t usb_sleep_music_show(struct device *dev,
2163 struct device_attribute *attr, char *buf)
2165 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2166 u32 state;
2167 int ret;
2169 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2170 if (ret < 0)
2171 return ret;
2173 return sprintf(buf, "%d\n", state);
2176 static ssize_t usb_sleep_music_store(struct device *dev,
2177 struct device_attribute *attr,
2178 const char *buf, size_t count)
2180 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2181 int state;
2182 int ret;
2184 ret = kstrtoint(buf, 0, &state);
2185 if (ret)
2186 return ret;
2187 if (state != 0 && state != 1)
2188 return -EINVAL;
2190 ret = toshiba_usb_sleep_music_set(toshiba, state);
2191 if (ret)
2192 return ret;
2194 return count;
2196 static DEVICE_ATTR_RW(usb_sleep_music);
2198 static ssize_t kbd_function_keys_show(struct device *dev,
2199 struct device_attribute *attr, char *buf)
2201 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2202 int mode;
2203 int ret;
2205 ret = toshiba_function_keys_get(toshiba, &mode);
2206 if (ret < 0)
2207 return ret;
2209 return sprintf(buf, "%d\n", mode);
2212 static ssize_t kbd_function_keys_store(struct device *dev,
2213 struct device_attribute *attr,
2214 const char *buf, size_t count)
2216 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2217 int mode;
2218 int ret;
2220 ret = kstrtoint(buf, 0, &mode);
2221 if (ret)
2222 return ret;
2224 * Check for the function keys mode where:
2225 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2226 * 1 - Special functions (Opposite of the above setting)
2228 if (mode != 0 && mode != 1)
2229 return -EINVAL;
2231 ret = toshiba_function_keys_set(toshiba, mode);
2232 if (ret)
2233 return ret;
2235 pr_info("Reboot for changes to KBD Function Keys to take effect");
2237 return count;
2239 static DEVICE_ATTR_RW(kbd_function_keys);
2241 static ssize_t panel_power_on_show(struct device *dev,
2242 struct device_attribute *attr, char *buf)
2244 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2245 u32 state;
2246 int ret;
2248 ret = toshiba_panel_power_on_get(toshiba, &state);
2249 if (ret < 0)
2250 return ret;
2252 return sprintf(buf, "%d\n", state);
2255 static ssize_t panel_power_on_store(struct device *dev,
2256 struct device_attribute *attr,
2257 const char *buf, size_t count)
2259 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2260 int state;
2261 int ret;
2263 ret = kstrtoint(buf, 0, &state);
2264 if (ret)
2265 return ret;
2266 if (state != 0 && state != 1)
2267 return -EINVAL;
2269 ret = toshiba_panel_power_on_set(toshiba, state);
2270 if (ret)
2271 return ret;
2273 pr_info("Reboot for changes to Panel Power ON to take effect");
2275 return count;
2277 static DEVICE_ATTR_RW(panel_power_on);
2279 static ssize_t usb_three_show(struct device *dev,
2280 struct device_attribute *attr, char *buf)
2282 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2283 u32 state;
2284 int ret;
2286 ret = toshiba_usb_three_get(toshiba, &state);
2287 if (ret < 0)
2288 return ret;
2290 return sprintf(buf, "%d\n", state);
2293 static ssize_t usb_three_store(struct device *dev,
2294 struct device_attribute *attr,
2295 const char *buf, size_t count)
2297 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2298 int state;
2299 int ret;
2301 ret = kstrtoint(buf, 0, &state);
2302 if (ret)
2303 return ret;
2305 * Check for USB 3 mode where:
2306 * 0 - Disabled (Acts like a USB 2 port, saving power)
2307 * 1 - Enabled
2309 if (state != 0 && state != 1)
2310 return -EINVAL;
2312 ret = toshiba_usb_three_set(toshiba, state);
2313 if (ret)
2314 return ret;
2316 pr_info("Reboot for changes to USB 3 to take effect");
2318 return count;
2320 static DEVICE_ATTR_RW(usb_three);
2322 static struct attribute *toshiba_attributes[] = {
2323 &dev_attr_version.attr,
2324 &dev_attr_fan.attr,
2325 &dev_attr_kbd_backlight_mode.attr,
2326 &dev_attr_kbd_type.attr,
2327 &dev_attr_available_kbd_modes.attr,
2328 &dev_attr_kbd_backlight_timeout.attr,
2329 &dev_attr_touchpad.attr,
2330 &dev_attr_position.attr,
2331 &dev_attr_usb_sleep_charge.attr,
2332 &dev_attr_sleep_functions_on_battery.attr,
2333 &dev_attr_usb_rapid_charge.attr,
2334 &dev_attr_usb_sleep_music.attr,
2335 &dev_attr_kbd_function_keys.attr,
2336 &dev_attr_panel_power_on.attr,
2337 &dev_attr_usb_three.attr,
2338 NULL,
2341 static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2342 struct attribute *attr, int idx)
2344 struct device *dev = container_of(kobj, struct device, kobj);
2345 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2346 bool exists = true;
2348 if (attr == &dev_attr_fan.attr)
2349 exists = (drv->fan_supported) ? true : false;
2350 else if (attr == &dev_attr_kbd_backlight_mode.attr)
2351 exists = (drv->kbd_illum_supported) ? true : false;
2352 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2353 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
2354 else if (attr == &dev_attr_touchpad.attr)
2355 exists = (drv->touchpad_supported) ? true : false;
2356 else if (attr == &dev_attr_position.attr)
2357 exists = (drv->accelerometer_supported) ? true : false;
2358 else if (attr == &dev_attr_usb_sleep_charge.attr)
2359 exists = (drv->usb_sleep_charge_supported) ? true : false;
2360 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2361 exists = (drv->usb_sleep_charge_supported) ? true : false;
2362 else if (attr == &dev_attr_usb_rapid_charge.attr)
2363 exists = (drv->usb_rapid_charge_supported) ? true : false;
2364 else if (attr == &dev_attr_usb_sleep_music.attr)
2365 exists = (drv->usb_sleep_music_supported) ? true : false;
2366 else if (attr == &dev_attr_kbd_function_keys.attr)
2367 exists = (drv->kbd_function_keys_supported) ? true : false;
2368 else if (attr == &dev_attr_panel_power_on.attr)
2369 exists = (drv->panel_power_on_supported) ? true : false;
2370 else if (attr == &dev_attr_usb_three.attr)
2371 exists = (drv->usb_three_supported) ? true : false;
2373 return exists ? attr->mode : 0;
2376 static struct attribute_group toshiba_attr_group = {
2377 .is_visible = toshiba_sysfs_is_visible,
2378 .attrs = toshiba_attributes,
2382 * Hotkeys
2384 static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2386 acpi_status status;
2387 u32 result;
2389 status = acpi_evaluate_object(dev->acpi_dev->handle,
2390 "ENAB", NULL, NULL);
2391 if (ACPI_FAILURE(status))
2392 return -ENODEV;
2394 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2395 if (result == TOS_FAILURE)
2396 return -EIO;
2397 else if (result == TOS_NOT_SUPPORTED)
2398 return -ENODEV;
2400 return 0;
2403 static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2405 u32 result;
2408 * Re-activate the hotkeys, but this time, we are using the
2409 * "Special Functions" mode.
2411 result = hci_write1(dev, HCI_HOTKEY_EVENT,
2412 HCI_HOTKEY_SPECIAL_FUNCTIONS);
2413 if (result != TOS_SUCCESS)
2414 pr_err("Could not enable the Special Function mode\n");
2417 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2418 struct serio *port)
2420 if (str & I8042_STR_AUXDATA)
2421 return false;
2423 if (unlikely(data == 0xe0))
2424 return false;
2426 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2427 schedule_work(&toshiba_acpi->hotkey_work);
2428 return true;
2431 return false;
2434 static void toshiba_acpi_hotkey_work(struct work_struct *work)
2436 acpi_handle ec_handle = ec_get_handle();
2437 acpi_status status;
2439 if (!ec_handle)
2440 return;
2442 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2443 if (ACPI_FAILURE(status))
2444 pr_err("ACPI NTFY method execution failed\n");
2448 * Returns hotkey scancode, or < 0 on failure.
2450 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2452 unsigned long long value;
2453 acpi_status status;
2455 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2456 NULL, &value);
2457 if (ACPI_FAILURE(status)) {
2458 pr_err("ACPI INFO method execution failed\n");
2459 return -EIO;
2462 return value;
2465 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2466 int scancode)
2468 if (scancode == 0x100)
2469 return;
2471 /* Act on key press; ignore key release */
2472 if (scancode & 0x80)
2473 return;
2475 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2476 pr_info("Unknown key %x\n", scancode);
2479 static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2481 u32 hci_result, value;
2482 int retries = 3;
2483 int scancode;
2485 if (dev->info_supported) {
2486 scancode = toshiba_acpi_query_hotkey(dev);
2487 if (scancode < 0)
2488 pr_err("Failed to query hotkey event\n");
2489 else if (scancode != 0)
2490 toshiba_acpi_report_hotkey(dev, scancode);
2491 } else if (dev->system_event_supported) {
2492 do {
2493 hci_result = hci_read1(dev, HCI_SYSTEM_EVENT, &value);
2494 switch (hci_result) {
2495 case TOS_SUCCESS:
2496 toshiba_acpi_report_hotkey(dev, (int)value);
2497 break;
2498 case TOS_NOT_SUPPORTED:
2500 * This is a workaround for an unresolved
2501 * issue on some machines where system events
2502 * sporadically become disabled.
2504 hci_result =
2505 hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2506 pr_notice("Re-enabled hotkeys\n");
2507 /* Fall through */
2508 default:
2509 retries--;
2510 break;
2512 } while (retries && hci_result != TOS_FIFO_EMPTY);
2516 static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
2518 const struct key_entry *keymap = toshiba_acpi_keymap;
2519 acpi_handle ec_handle;
2520 u32 events_type;
2521 u32 hci_result;
2522 int error;
2524 error = toshiba_acpi_enable_hotkeys(dev);
2525 if (error)
2526 return error;
2528 if (toshiba_hotkey_event_type_get(dev, &events_type))
2529 pr_notice("Unable to query Hotkey Event Type\n");
2531 dev->hotkey_event_type = events_type;
2533 dev->hotkey_dev = input_allocate_device();
2534 if (!dev->hotkey_dev)
2535 return -ENOMEM;
2537 dev->hotkey_dev->name = "Toshiba input device";
2538 dev->hotkey_dev->phys = "toshiba_acpi/input0";
2539 dev->hotkey_dev->id.bustype = BUS_HOST;
2541 if (events_type == HCI_SYSTEM_TYPE1 ||
2542 !dev->kbd_function_keys_supported)
2543 keymap = toshiba_acpi_keymap;
2544 else if (events_type == HCI_SYSTEM_TYPE2 ||
2545 dev->kbd_function_keys_supported)
2546 keymap = toshiba_acpi_alt_keymap;
2547 else
2548 pr_info("Unknown event type received %x\n", events_type);
2549 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
2550 if (error)
2551 goto err_free_dev;
2554 * For some machines the SCI responsible for providing hotkey
2555 * notification doesn't fire. We can trigger the notification
2556 * whenever the Fn key is pressed using the NTFY method, if
2557 * supported, so if it's present set up an i8042 key filter
2558 * for this purpose.
2560 ec_handle = ec_get_handle();
2561 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
2562 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2564 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2565 if (error) {
2566 pr_err("Error installing key filter\n");
2567 goto err_free_keymap;
2570 dev->ntfy_supported = 1;
2574 * Determine hotkey query interface. Prefer using the INFO
2575 * method when it is available.
2577 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
2578 dev->info_supported = 1;
2579 else {
2580 hci_result = hci_write1(dev, HCI_SYSTEM_EVENT, 1);
2581 if (hci_result == TOS_SUCCESS)
2582 dev->system_event_supported = 1;
2585 if (!dev->info_supported && !dev->system_event_supported) {
2586 pr_warn("No hotkey query interface found\n");
2587 goto err_remove_filter;
2590 error = input_register_device(dev->hotkey_dev);
2591 if (error) {
2592 pr_info("Unable to register input device\n");
2593 goto err_remove_filter;
2596 return 0;
2598 err_remove_filter:
2599 if (dev->ntfy_supported)
2600 i8042_remove_filter(toshiba_acpi_i8042_filter);
2601 err_free_keymap:
2602 sparse_keymap_free(dev->hotkey_dev);
2603 err_free_dev:
2604 input_free_device(dev->hotkey_dev);
2605 dev->hotkey_dev = NULL;
2606 return error;
2609 static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
2611 struct backlight_properties props;
2612 int brightness;
2613 int ret;
2614 bool enabled;
2617 * Some machines don't support the backlight methods at all, and
2618 * others support it read-only. Either of these is pretty useless,
2619 * so only register the backlight device if the backlight method
2620 * supports both reads and writes.
2622 brightness = __get_lcd_brightness(dev);
2623 if (brightness < 0)
2624 return 0;
2625 ret = set_lcd_brightness(dev, brightness);
2626 if (ret) {
2627 pr_debug("Backlight method is read-only, disabling backlight support\n");
2628 return 0;
2631 /* Determine whether or not BIOS supports transflective backlight */
2632 ret = get_tr_backlight_status(dev, &enabled);
2633 dev->tr_backlight_supported = !ret;
2636 * Tell acpi-video-detect code to prefer vendor backlight on all
2637 * systems with transflective backlight and on dmi matched systems.
2639 if (dev->tr_backlight_supported ||
2640 dmi_check_system(toshiba_vendor_backlight_dmi))
2641 acpi_video_dmi_promote_vendor();
2643 if (acpi_video_backlight_support())
2644 return 0;
2646 /* acpi-video may have loaded before we called dmi_promote_vendor() */
2647 acpi_video_unregister_backlight();
2649 memset(&props, 0, sizeof(props));
2650 props.type = BACKLIGHT_PLATFORM;
2651 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
2653 /* Adding an extra level and having 0 change to transflective mode */
2654 if (dev->tr_backlight_supported)
2655 props.max_brightness++;
2657 dev->backlight_dev = backlight_device_register("toshiba",
2658 &dev->acpi_dev->dev,
2659 dev,
2660 &toshiba_backlight_data,
2661 &props);
2662 if (IS_ERR(dev->backlight_dev)) {
2663 ret = PTR_ERR(dev->backlight_dev);
2664 pr_err("Could not register toshiba backlight device\n");
2665 dev->backlight_dev = NULL;
2666 return ret;
2669 dev->backlight_dev->props.brightness = brightness;
2670 return 0;
2673 static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
2675 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2677 remove_toshiba_proc_entries(dev);
2679 if (dev->sysfs_created)
2680 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2681 &toshiba_attr_group);
2683 if (dev->ntfy_supported) {
2684 i8042_remove_filter(toshiba_acpi_i8042_filter);
2685 cancel_work_sync(&dev->hotkey_work);
2688 if (dev->hotkey_dev) {
2689 input_unregister_device(dev->hotkey_dev);
2690 sparse_keymap_free(dev->hotkey_dev);
2693 if (dev->bt_rfk) {
2694 rfkill_unregister(dev->bt_rfk);
2695 rfkill_destroy(dev->bt_rfk);
2698 backlight_device_unregister(dev->backlight_dev);
2700 if (dev->illumination_supported)
2701 led_classdev_unregister(&dev->led_dev);
2703 if (dev->kbd_led_registered)
2704 led_classdev_unregister(&dev->kbd_led);
2706 if (dev->eco_supported)
2707 led_classdev_unregister(&dev->eco_led);
2709 if (toshiba_acpi)
2710 toshiba_acpi = NULL;
2712 kfree(dev);
2714 return 0;
2717 static const char *find_hci_method(acpi_handle handle)
2719 if (acpi_has_method(handle, "GHCI"))
2720 return "GHCI";
2722 if (acpi_has_method(handle, "SPFC"))
2723 return "SPFC";
2725 return NULL;
2728 static int toshiba_acpi_add(struct acpi_device *acpi_dev)
2730 struct toshiba_acpi_dev *dev;
2731 const char *hci_method;
2732 u32 special_functions;
2733 u32 dummy;
2734 bool bt_present;
2735 int ret = 0;
2737 if (toshiba_acpi)
2738 return -EBUSY;
2740 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2741 TOSHIBA_ACPI_VERSION);
2743 hci_method = find_hci_method(acpi_dev->handle);
2744 if (!hci_method) {
2745 pr_err("HCI interface not found\n");
2746 return -ENODEV;
2749 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2750 if (!dev)
2751 return -ENOMEM;
2752 dev->acpi_dev = acpi_dev;
2753 dev->method_hci = hci_method;
2754 acpi_dev->driver_data = dev;
2755 dev_set_drvdata(&acpi_dev->dev, dev);
2757 /* Query the BIOS for supported features */
2760 * The "Special Functions" are always supported by the laptops
2761 * with the new keyboard layout, query for its presence to help
2762 * determine the keymap layout to use.
2764 ret = toshiba_function_keys_get(dev, &special_functions);
2765 dev->kbd_function_keys_supported = !ret;
2767 dev->hotkey_event_type = 0;
2768 if (toshiba_acpi_setup_keyboard(dev))
2769 pr_info("Unable to activate hotkeys\n");
2771 mutex_init(&dev->mutex);
2773 ret = toshiba_acpi_setup_backlight(dev);
2774 if (ret)
2775 goto error;
2777 /* Register rfkill switch for Bluetooth */
2778 if (hci_get_bt_present(dev, &bt_present) == TOS_SUCCESS && bt_present) {
2779 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth",
2780 &acpi_dev->dev,
2781 RFKILL_TYPE_BLUETOOTH,
2782 &toshiba_rfk_ops,
2783 dev);
2784 if (!dev->bt_rfk) {
2785 pr_err("unable to allocate rfkill device\n");
2786 ret = -ENOMEM;
2787 goto error;
2790 ret = rfkill_register(dev->bt_rfk);
2791 if (ret) {
2792 pr_err("unable to register rfkill device\n");
2793 rfkill_destroy(dev->bt_rfk);
2794 goto error;
2798 if (toshiba_illumination_available(dev)) {
2799 dev->led_dev.name = "toshiba::illumination";
2800 dev->led_dev.max_brightness = 1;
2801 dev->led_dev.brightness_set = toshiba_illumination_set;
2802 dev->led_dev.brightness_get = toshiba_illumination_get;
2803 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
2804 dev->illumination_supported = 1;
2807 if (toshiba_eco_mode_available(dev)) {
2808 dev->eco_led.name = "toshiba::eco_mode";
2809 dev->eco_led.max_brightness = 1;
2810 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2811 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2812 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2813 dev->eco_supported = 1;
2816 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
2818 * Only register the LED if KBD illumination is supported
2819 * and the keyboard backlight operation mode is set to FN-Z
2821 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2822 dev->kbd_led.name = "toshiba::kbd_backlight";
2823 dev->kbd_led.max_brightness = 1;
2824 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2825 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2826 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2827 dev->kbd_led_registered = 1;
2830 ret = toshiba_touchpad_get(dev, &dummy);
2831 dev->touchpad_supported = !ret;
2833 ret = toshiba_accelerometer_supported(dev);
2834 dev->accelerometer_supported = !ret;
2836 toshiba_usb_sleep_charge_available(dev);
2838 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2839 dev->usb_rapid_charge_supported = !ret;
2841 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2842 dev->usb_sleep_music_supported = !ret;
2844 ret = toshiba_panel_power_on_get(dev, &dummy);
2845 dev->panel_power_on_supported = !ret;
2847 ret = toshiba_usb_three_get(dev, &dummy);
2848 dev->usb_three_supported = !ret;
2850 ret = get_video_status(dev, &dummy);
2851 dev->video_supported = !ret;
2853 ret = get_fan_status(dev, &dummy);
2854 dev->fan_supported = !ret;
2857 * Enable the "Special Functions" mode only if they are
2858 * supported and if they are activated.
2860 if (dev->kbd_function_keys_supported && special_functions)
2861 toshiba_acpi_enable_special_functions(dev);
2863 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2864 &toshiba_attr_group);
2865 if (ret) {
2866 dev->sysfs_created = 0;
2867 goto error;
2869 dev->sysfs_created = !ret;
2871 create_toshiba_proc_entries(dev);
2873 toshiba_acpi = dev;
2875 return 0;
2877 error:
2878 toshiba_acpi_remove(acpi_dev);
2879 return ret;
2882 static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2884 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2885 int ret;
2887 switch (event) {
2888 case 0x80: /* Hotkeys and some system events */
2889 toshiba_acpi_process_hotkeys(dev);
2890 break;
2891 case 0x81: /* Dock events */
2892 case 0x82:
2893 case 0x83:
2894 pr_info("Dock event received %x\n", event);
2895 break;
2896 case 0x88: /* Thermal events */
2897 pr_info("Thermal event received\n");
2898 break;
2899 case 0x8f: /* LID closed */
2900 case 0x90: /* LID is closed and Dock has been ejected */
2901 break;
2902 case 0x8c: /* SATA power events */
2903 case 0x8b:
2904 pr_info("SATA power event received %x\n", event);
2905 break;
2906 case 0x92: /* Keyboard backlight mode changed */
2907 /* Update sysfs entries */
2908 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2909 &toshiba_attr_group);
2910 if (ret)
2911 pr_err("Unable to update sysfs entries\n");
2912 break;
2913 case 0x85: /* Unknown */
2914 case 0x8d: /* Unknown */
2915 case 0x8e: /* Unknown */
2916 case 0x94: /* Unknown */
2917 case 0x95: /* Unknown */
2918 default:
2919 pr_info("Unknown event received %x\n", event);
2920 break;
2923 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2924 dev_name(&acpi_dev->dev),
2925 event, 0);
2928 #ifdef CONFIG_PM_SLEEP
2929 static int toshiba_acpi_suspend(struct device *device)
2931 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
2932 u32 result;
2934 if (dev->hotkey_dev)
2935 result = hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
2937 return 0;
2940 static int toshiba_acpi_resume(struct device *device)
2942 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
2943 int error;
2945 if (dev->hotkey_dev) {
2946 error = toshiba_acpi_enable_hotkeys(dev);
2947 if (error)
2948 pr_info("Unable to re-enable hotkeys\n");
2951 return 0;
2953 #endif
2955 static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2956 toshiba_acpi_suspend, toshiba_acpi_resume);
2958 static struct acpi_driver toshiba_acpi_driver = {
2959 .name = "Toshiba ACPI driver",
2960 .owner = THIS_MODULE,
2961 .ids = toshiba_device_ids,
2962 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2963 .ops = {
2964 .add = toshiba_acpi_add,
2965 .remove = toshiba_acpi_remove,
2966 .notify = toshiba_acpi_notify,
2968 .drv.pm = &toshiba_acpi_pm,
2971 static int __init toshiba_acpi_init(void)
2973 int ret;
2976 * Machines with this WMI guid aren't supported due to bugs in
2977 * their AML. This check relies on wmi initializing before
2978 * toshiba_acpi to guarantee guids have been identified.
2980 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2981 return -ENODEV;
2983 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2984 if (!toshiba_proc_dir) {
2985 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
2986 return -ENODEV;
2989 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2990 if (ret) {
2991 pr_err("Failed to register ACPI driver: %d\n", ret);
2992 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
2995 return ret;
2998 static void __exit toshiba_acpi_exit(void)
3000 acpi_bus_unregister_driver(&toshiba_acpi_driver);
3001 if (toshiba_proc_dir)
3002 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
3005 module_init(toshiba_acpi_init);
3006 module_exit(toshiba_acpi_exit);