toshiba_acpi: Add set_fan_status function
[deliverable/linux.git] / drivers / platform / x86 / toshiba_acpi.c
1 /*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
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
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 *
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
24 *
25 * Credits:
26 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27 * engineering the Windows drivers
28 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29 * Rob Miller - TV out and hotkeys help
30 */
31
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
34 #define TOSHIBA_ACPI_VERSION "0.22"
35 #define PROC_INTERFACE_VERSION 1
36
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/backlight.h>
44 #include <linux/input.h>
45 #include <linux/input/sparse-keymap.h>
46 #include <linux/leds.h>
47 #include <linux/slab.h>
48 #include <linux/workqueue.h>
49 #include <linux/i8042.h>
50 #include <linux/acpi.h>
51 #include <linux/dmi.h>
52 #include <linux/uaccess.h>
53 #include <linux/miscdevice.h>
54 #include <linux/toshiba.h>
55 #include <acpi/video.h>
56
57 MODULE_AUTHOR("John Belmonte");
58 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
59 MODULE_LICENSE("GPL");
60
61 #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100"
62
63 /* Scan code for Fn key on TOS1900 models */
64 #define TOS1900_FN_SCAN 0x6e
65
66 /* Toshiba ACPI method paths */
67 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
68
69 /*
70 * The Toshiba configuration interface is composed of the HCI and the SCI,
71 * which are defined as follows:
72 *
73 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
74 * be uniform across all their models. Ideally we would just call
75 * dedicated ACPI methods instead of using this primitive interface.
76 * However the ACPI methods seem to be incomplete in some areas (for
77 * example they allow setting, but not reading, the LCD brightness value),
78 * so this is still useful.
79 *
80 * SCI stands for "System Configuration Interface" which aim is to
81 * conceal differences in hardware between different models.
82 */
83
84 #define TCI_WORDS 6
85
86 /* Operations */
87 #define HCI_SET 0xff00
88 #define HCI_GET 0xfe00
89 #define SCI_OPEN 0xf100
90 #define SCI_CLOSE 0xf200
91 #define SCI_GET 0xf300
92 #define SCI_SET 0xf400
93
94 /* Return codes */
95 #define TOS_SUCCESS 0x0000
96 #define TOS_OPEN_CLOSE_OK 0x0044
97 #define TOS_FAILURE 0x1000
98 #define TOS_NOT_SUPPORTED 0x8000
99 #define TOS_ALREADY_OPEN 0x8100
100 #define TOS_NOT_OPENED 0x8200
101 #define TOS_INPUT_DATA_ERROR 0x8300
102 #define TOS_WRITE_PROTECTED 0x8400
103 #define TOS_NOT_PRESENT 0x8600
104 #define TOS_FIFO_EMPTY 0x8c00
105 #define TOS_DATA_NOT_AVAILABLE 0x8d20
106 #define TOS_NOT_INITIALIZED 0x8d50
107 #define TOS_NOT_INSTALLED 0x8e00
108
109 /* Registers */
110 #define HCI_FAN 0x0004
111 #define HCI_TR_BACKLIGHT 0x0005
112 #define HCI_SYSTEM_EVENT 0x0016
113 #define HCI_VIDEO_OUT 0x001c
114 #define HCI_HOTKEY_EVENT 0x001e
115 #define HCI_LCD_BRIGHTNESS 0x002a
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
129
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 SCI_KBD_MODE_MASK 0x1f
145 #define SCI_KBD_MODE_FNZ 0x1
146 #define SCI_KBD_MODE_AUTO 0x2
147 #define SCI_KBD_MODE_ON 0x8
148 #define SCI_KBD_MODE_OFF 0x10
149 #define SCI_KBD_TIME_MAX 0x3c001a
150 #define SCI_USB_CHARGE_MODE_MASK 0xff
151 #define SCI_USB_CHARGE_DISABLED 0x00
152 #define SCI_USB_CHARGE_ALTERNATE 0x09
153 #define SCI_USB_CHARGE_TYPICAL 0x11
154 #define SCI_USB_CHARGE_AUTO 0x21
155 #define SCI_USB_CHARGE_BAT_MASK 0x7
156 #define SCI_USB_CHARGE_BAT_LVL_OFF 0x1
157 #define SCI_USB_CHARGE_BAT_LVL_ON 0x4
158 #define SCI_USB_CHARGE_BAT_LVL 0x0200
159 #define SCI_USB_CHARGE_RAPID_DSP 0x0300
160
161 struct toshiba_acpi_dev {
162 struct acpi_device *acpi_dev;
163 const char *method_hci;
164 struct input_dev *hotkey_dev;
165 struct work_struct hotkey_work;
166 struct backlight_device *backlight_dev;
167 struct led_classdev led_dev;
168 struct led_classdev kbd_led;
169 struct led_classdev eco_led;
170 struct miscdevice miscdev;
171
172 int force_fan;
173 int last_key_event;
174 int key_event_valid;
175 int kbd_type;
176 int kbd_mode;
177 int kbd_time;
178 int usbsc_bat_level;
179 int usbsc_mode_base;
180 int hotkey_event_type;
181
182 unsigned int illumination_supported:1;
183 unsigned int video_supported:1;
184 unsigned int fan_supported:1;
185 unsigned int system_event_supported:1;
186 unsigned int ntfy_supported:1;
187 unsigned int info_supported:1;
188 unsigned int tr_backlight_supported:1;
189 unsigned int kbd_illum_supported:1;
190 unsigned int kbd_led_registered:1;
191 unsigned int touchpad_supported:1;
192 unsigned int eco_supported:1;
193 unsigned int accelerometer_supported:1;
194 unsigned int usb_sleep_charge_supported:1;
195 unsigned int usb_rapid_charge_supported:1;
196 unsigned int usb_sleep_music_supported:1;
197 unsigned int kbd_function_keys_supported:1;
198 unsigned int panel_power_on_supported:1;
199 unsigned int usb_three_supported:1;
200 unsigned int sysfs_created:1;
201 };
202
203 static struct toshiba_acpi_dev *toshiba_acpi;
204
205 static const struct acpi_device_id toshiba_device_ids[] = {
206 {"TOS6200", 0},
207 {"TOS6207", 0},
208 {"TOS6208", 0},
209 {"TOS1900", 0},
210 {"", 0},
211 };
212 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
213
214 static const struct key_entry toshiba_acpi_keymap[] = {
215 { KE_KEY, 0x9e, { KEY_RFKILL } },
216 { KE_KEY, 0x101, { KEY_MUTE } },
217 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
218 { KE_KEY, 0x103, { KEY_ZOOMIN } },
219 { KE_KEY, 0x10f, { KEY_TAB } },
220 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
221 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
222 { KE_KEY, 0x13b, { KEY_COFFEE } },
223 { KE_KEY, 0x13c, { KEY_BATTERY } },
224 { KE_KEY, 0x13d, { KEY_SLEEP } },
225 { KE_KEY, 0x13e, { KEY_SUSPEND } },
226 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } },
227 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } },
228 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } },
229 { KE_KEY, 0x142, { KEY_WLAN } },
230 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } },
231 { KE_KEY, 0x17f, { KEY_FN } },
232 { KE_KEY, 0xb05, { KEY_PROG2 } },
233 { KE_KEY, 0xb06, { KEY_WWW } },
234 { KE_KEY, 0xb07, { KEY_MAIL } },
235 { KE_KEY, 0xb30, { KEY_STOP } },
236 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } },
237 { KE_KEY, 0xb32, { KEY_NEXTSONG } },
238 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } },
239 { KE_KEY, 0xb5a, { KEY_MEDIA } },
240 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, /* Wake from sleep */
241 { KE_IGNORE, 0x1501, { KEY_RESERVED } }, /* Output changed */
242 { KE_IGNORE, 0x1502, { KEY_RESERVED } }, /* HDMI plugged/unplugged */
243 { KE_IGNORE, 0x1ABE, { KEY_RESERVED } }, /* Protection level set */
244 { KE_IGNORE, 0x1ABF, { KEY_RESERVED } }, /* Protection level off */
245 { KE_END, 0 },
246 };
247
248 static const struct key_entry toshiba_acpi_alt_keymap[] = {
249 { KE_KEY, 0x102, { KEY_ZOOMOUT } },
250 { KE_KEY, 0x103, { KEY_ZOOMIN } },
251 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } },
252 { KE_KEY, 0x139, { KEY_ZOOMRESET } },
253 { KE_KEY, 0x13c, { KEY_BRIGHTNESSDOWN } },
254 { KE_KEY, 0x13d, { KEY_BRIGHTNESSUP } },
255 { KE_KEY, 0x13e, { KEY_SWITCHVIDEOMODE } },
256 { KE_KEY, 0x13f, { KEY_TOUCHPAD_TOGGLE } },
257 { KE_KEY, 0x157, { KEY_MUTE } },
258 { KE_KEY, 0x158, { KEY_WLAN } },
259 { KE_END, 0 },
260 };
261
262 /*
263 * List of models which have a broken acpi-video backlight interface and thus
264 * need to use the toshiba (vendor) interface instead.
265 */
266 static const struct dmi_system_id toshiba_vendor_backlight_dmi[] = {
267 {}
268 };
269
270 /*
271 * Utility
272 */
273
274 static inline void _set_bit(u32 *word, u32 mask, int value)
275 {
276 *word = (*word & ~mask) | (mask * value);
277 }
278
279 /*
280 * ACPI interface wrappers
281 */
282
283 static int write_acpi_int(const char *methodName, int val)
284 {
285 acpi_status status;
286
287 status = acpi_execute_simple_method(NULL, (char *)methodName, val);
288 return (status == AE_OK) ? 0 : -EIO;
289 }
290
291 /*
292 * Perform a raw configuration call. Here we don't care about input or output
293 * buffer format.
294 */
295 static acpi_status tci_raw(struct toshiba_acpi_dev *dev,
296 const u32 in[TCI_WORDS], u32 out[TCI_WORDS])
297 {
298 struct acpi_object_list params;
299 union acpi_object in_objs[TCI_WORDS];
300 struct acpi_buffer results;
301 union acpi_object out_objs[TCI_WORDS + 1];
302 acpi_status status;
303 int i;
304
305 params.count = TCI_WORDS;
306 params.pointer = in_objs;
307 for (i = 0; i < TCI_WORDS; ++i) {
308 in_objs[i].type = ACPI_TYPE_INTEGER;
309 in_objs[i].integer.value = in[i];
310 }
311
312 results.length = sizeof(out_objs);
313 results.pointer = out_objs;
314
315 status = acpi_evaluate_object(dev->acpi_dev->handle,
316 (char *)dev->method_hci, &params,
317 &results);
318 if ((status == AE_OK) && (out_objs->package.count <= TCI_WORDS)) {
319 for (i = 0; i < out_objs->package.count; ++i)
320 out[i] = out_objs->package.elements[i].integer.value;
321 }
322
323 return status;
324 }
325
326 /*
327 * Common hci tasks
328 *
329 * In addition to the ACPI status, the HCI system returns a result which
330 * may be useful (such as "not supported").
331 */
332
333 static u32 hci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
334 {
335 u32 in[TCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
336 u32 out[TCI_WORDS];
337 acpi_status status = tci_raw(dev, in, out);
338
339 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
340 }
341
342 static u32 hci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
343 {
344 u32 in[TCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
345 u32 out[TCI_WORDS];
346 acpi_status status = tci_raw(dev, in, out);
347
348 if (ACPI_FAILURE(status))
349 return TOS_FAILURE;
350
351 *out1 = out[2];
352
353 return out[0];
354 }
355
356 /*
357 * Common sci tasks
358 */
359
360 static int sci_open(struct toshiba_acpi_dev *dev)
361 {
362 u32 in[TCI_WORDS] = { SCI_OPEN, 0, 0, 0, 0, 0 };
363 u32 out[TCI_WORDS];
364 acpi_status status;
365
366 status = tci_raw(dev, in, out);
367 if (ACPI_FAILURE(status)) {
368 pr_err("ACPI call to open SCI failed\n");
369 return 0;
370 }
371
372 if (out[0] == TOS_OPEN_CLOSE_OK) {
373 return 1;
374 } else if (out[0] == TOS_ALREADY_OPEN) {
375 pr_info("Toshiba SCI already opened\n");
376 return 1;
377 } else if (out[0] == TOS_NOT_SUPPORTED) {
378 /*
379 * Some BIOSes do not have the SCI open/close functions
380 * implemented and return 0x8000 (Not Supported), failing to
381 * register some supported features.
382 *
383 * Simply return 1 if we hit those affected laptops to make the
384 * supported features work.
385 *
386 * In the case that some laptops really do not support the SCI,
387 * all the SCI dependent functions check for TOS_NOT_SUPPORTED,
388 * and thus, not registering support for the queried feature.
389 */
390 return 1;
391 } else if (out[0] == TOS_NOT_PRESENT) {
392 pr_info("Toshiba SCI is not present\n");
393 }
394
395 return 0;
396 }
397
398 static void sci_close(struct toshiba_acpi_dev *dev)
399 {
400 u32 in[TCI_WORDS] = { SCI_CLOSE, 0, 0, 0, 0, 0 };
401 u32 out[TCI_WORDS];
402 acpi_status status;
403
404 status = tci_raw(dev, in, out);
405 if (ACPI_FAILURE(status)) {
406 pr_err("ACPI call to close SCI failed\n");
407 return;
408 }
409
410 if (out[0] == TOS_OPEN_CLOSE_OK)
411 return;
412 else if (out[0] == TOS_NOT_OPENED)
413 pr_info("Toshiba SCI not opened\n");
414 else if (out[0] == TOS_NOT_PRESENT)
415 pr_info("Toshiba SCI is not present\n");
416 }
417
418 static u32 sci_read(struct toshiba_acpi_dev *dev, u32 reg, u32 *out1)
419 {
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);
423
424 if (ACPI_FAILURE(status))
425 return TOS_FAILURE;
426
427 *out1 = out[2];
428
429 return out[0];
430 }
431
432 static u32 sci_write(struct toshiba_acpi_dev *dev, u32 reg, u32 in1)
433 {
434 u32 in[TCI_WORDS] = { SCI_SET, reg, in1, 0, 0, 0 };
435 u32 out[TCI_WORDS];
436 acpi_status status = tci_raw(dev, in, out);
437
438 return ACPI_SUCCESS(status) ? out[0] : TOS_FAILURE;
439 }
440
441 /* Illumination support */
442 static int toshiba_illumination_available(struct toshiba_acpi_dev *dev)
443 {
444 u32 in[TCI_WORDS] = { SCI_GET, SCI_ILLUMINATION, 0, 0, 0, 0 };
445 u32 out[TCI_WORDS];
446 acpi_status status;
447
448 if (!sci_open(dev))
449 return 0;
450
451 status = tci_raw(dev, in, out);
452 sci_close(dev);
453 if (ACPI_FAILURE(status)) {
454 pr_err("ACPI call to query Illumination support failed\n");
455 return 0;
456 } else if (out[0] == TOS_NOT_SUPPORTED) {
457 pr_info("Illumination device not available\n");
458 return 0;
459 }
460
461 return 1;
462 }
463
464 static void toshiba_illumination_set(struct led_classdev *cdev,
465 enum led_brightness brightness)
466 {
467 struct toshiba_acpi_dev *dev = container_of(cdev,
468 struct toshiba_acpi_dev, led_dev);
469 u32 state, result;
470
471 /* First request : initialize communication. */
472 if (!sci_open(dev))
473 return;
474
475 /* Switch the illumination on/off */
476 state = brightness ? 1 : 0;
477 result = sci_write(dev, SCI_ILLUMINATION, state);
478 sci_close(dev);
479 if (result == TOS_FAILURE) {
480 pr_err("ACPI call for illumination failed\n");
481 return;
482 } else if (result == TOS_NOT_SUPPORTED) {
483 pr_info("Illumination not supported\n");
484 return;
485 }
486 }
487
488 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev)
489 {
490 struct toshiba_acpi_dev *dev = container_of(cdev,
491 struct toshiba_acpi_dev, led_dev);
492 u32 state, result;
493
494 /* First request : initialize communication. */
495 if (!sci_open(dev))
496 return LED_OFF;
497
498 /* Check the illumination */
499 result = sci_read(dev, SCI_ILLUMINATION, &state);
500 sci_close(dev);
501 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
502 pr_err("ACPI call for illumination failed\n");
503 return LED_OFF;
504 } else if (result == TOS_NOT_SUPPORTED) {
505 pr_info("Illumination not supported\n");
506 return LED_OFF;
507 }
508
509 return state ? LED_FULL : LED_OFF;
510 }
511
512 /* KBD Illumination */
513 static int toshiba_kbd_illum_available(struct toshiba_acpi_dev *dev)
514 {
515 u32 in[TCI_WORDS] = { SCI_GET, SCI_KBD_ILLUM_STATUS, 0, 0, 0, 0 };
516 u32 out[TCI_WORDS];
517 acpi_status status;
518
519 if (!sci_open(dev))
520 return 0;
521
522 status = tci_raw(dev, in, out);
523 sci_close(dev);
524 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
525 pr_err("ACPI call to query kbd illumination support failed\n");
526 return 0;
527 } else if (out[0] == TOS_NOT_SUPPORTED) {
528 pr_info("Keyboard illumination not available\n");
529 return 0;
530 }
531
532 /*
533 * Check for keyboard backlight timeout max value,
534 * previous kbd backlight implementation set this to
535 * 0x3c0003, and now the new implementation set this
536 * to 0x3c001a, use this to distinguish between them.
537 */
538 if (out[3] == SCI_KBD_TIME_MAX)
539 dev->kbd_type = 2;
540 else
541 dev->kbd_type = 1;
542 /* Get the current keyboard backlight mode */
543 dev->kbd_mode = out[2] & SCI_KBD_MODE_MASK;
544 /* Get the current time (1-60 seconds) */
545 dev->kbd_time = out[2] >> HCI_MISC_SHIFT;
546
547 return 1;
548 }
549
550 static int toshiba_kbd_illum_status_set(struct toshiba_acpi_dev *dev, u32 time)
551 {
552 u32 result;
553
554 if (!sci_open(dev))
555 return -EIO;
556
557 result = sci_write(dev, SCI_KBD_ILLUM_STATUS, time);
558 sci_close(dev);
559 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
560 pr_err("ACPI call to set KBD backlight status failed\n");
561 return -EIO;
562 } else if (result == TOS_NOT_SUPPORTED) {
563 pr_info("Keyboard backlight status not supported\n");
564 return -ENODEV;
565 }
566
567 return 0;
568 }
569
570 static int toshiba_kbd_illum_status_get(struct toshiba_acpi_dev *dev, u32 *time)
571 {
572 u32 result;
573
574 if (!sci_open(dev))
575 return -EIO;
576
577 result = sci_read(dev, SCI_KBD_ILLUM_STATUS, time);
578 sci_close(dev);
579 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
580 pr_err("ACPI call to get KBD backlight status failed\n");
581 return -EIO;
582 } else if (result == TOS_NOT_SUPPORTED) {
583 pr_info("Keyboard backlight status not supported\n");
584 return -ENODEV;
585 }
586
587 return 0;
588 }
589
590 static enum led_brightness toshiba_kbd_backlight_get(struct led_classdev *cdev)
591 {
592 struct toshiba_acpi_dev *dev = container_of(cdev,
593 struct toshiba_acpi_dev, kbd_led);
594 u32 state, result;
595
596 /* Check the keyboard backlight state */
597 result = hci_read(dev, HCI_KBD_ILLUMINATION, &state);
598 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
599 pr_err("ACPI call to get the keyboard backlight failed\n");
600 return LED_OFF;
601 } else if (result == TOS_NOT_SUPPORTED) {
602 pr_info("Keyboard backlight not supported\n");
603 return LED_OFF;
604 }
605
606 return state ? LED_FULL : LED_OFF;
607 }
608
609 static void toshiba_kbd_backlight_set(struct led_classdev *cdev,
610 enum led_brightness brightness)
611 {
612 struct toshiba_acpi_dev *dev = container_of(cdev,
613 struct toshiba_acpi_dev, kbd_led);
614 u32 state, result;
615
616 /* Set the keyboard backlight state */
617 state = brightness ? 1 : 0;
618 result = hci_write(dev, HCI_KBD_ILLUMINATION, state);
619 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
620 pr_err("ACPI call to set KBD Illumination mode failed\n");
621 return;
622 } else if (result == TOS_NOT_SUPPORTED) {
623 pr_info("Keyboard backlight not supported\n");
624 return;
625 }
626 }
627
628 /* TouchPad support */
629 static int toshiba_touchpad_set(struct toshiba_acpi_dev *dev, u32 state)
630 {
631 u32 result;
632
633 if (!sci_open(dev))
634 return -EIO;
635
636 result = sci_write(dev, SCI_TOUCHPAD, state);
637 sci_close(dev);
638 if (result == TOS_FAILURE) {
639 pr_err("ACPI call to set the touchpad failed\n");
640 return -EIO;
641 } else if (result == TOS_NOT_SUPPORTED) {
642 return -ENODEV;
643 }
644
645 return 0;
646 }
647
648 static int toshiba_touchpad_get(struct toshiba_acpi_dev *dev, u32 *state)
649 {
650 u32 result;
651
652 if (!sci_open(dev))
653 return -EIO;
654
655 result = sci_read(dev, SCI_TOUCHPAD, state);
656 sci_close(dev);
657 if (result == TOS_FAILURE) {
658 pr_err("ACPI call to query the touchpad failed\n");
659 return -EIO;
660 } else if (result == TOS_NOT_SUPPORTED) {
661 return -ENODEV;
662 }
663
664 return 0;
665 }
666
667 /* Eco Mode support */
668 static int toshiba_eco_mode_available(struct toshiba_acpi_dev *dev)
669 {
670 acpi_status status;
671 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 0, 0, 0 };
672 u32 out[TCI_WORDS];
673
674 status = tci_raw(dev, in, out);
675 if (ACPI_FAILURE(status)) {
676 pr_err("ACPI call to get ECO led failed\n");
677 } else if (out[0] == TOS_NOT_INSTALLED) {
678 pr_info("ECO led not installed");
679 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
680 /*
681 * If we receive 0x8300 (Input Data Error), it means that the
682 * LED device is present, but that we just screwed the input
683 * parameters.
684 *
685 * Let's query the status of the LED to see if we really have a
686 * success response, indicating the actual presense of the LED,
687 * bail out otherwise.
688 */
689 in[3] = 1;
690 status = tci_raw(dev, in, out);
691 if (ACPI_FAILURE(status) || out[0] == TOS_FAILURE)
692 pr_err("ACPI call to get ECO led failed\n");
693 else if (out[0] == TOS_SUCCESS)
694 return 1;
695 }
696
697 return 0;
698 }
699
700 static enum led_brightness
701 toshiba_eco_mode_get_status(struct led_classdev *cdev)
702 {
703 struct toshiba_acpi_dev *dev = container_of(cdev,
704 struct toshiba_acpi_dev, eco_led);
705 u32 in[TCI_WORDS] = { HCI_GET, HCI_ECO_MODE, 0, 1, 0, 0 };
706 u32 out[TCI_WORDS];
707 acpi_status status;
708
709 status = tci_raw(dev, in, out);
710 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
711 pr_err("ACPI call to get ECO led failed\n");
712 return LED_OFF;
713 }
714
715 return out[2] ? LED_FULL : LED_OFF;
716 }
717
718 static void toshiba_eco_mode_set_status(struct led_classdev *cdev,
719 enum led_brightness brightness)
720 {
721 struct toshiba_acpi_dev *dev = container_of(cdev,
722 struct toshiba_acpi_dev, eco_led);
723 u32 in[TCI_WORDS] = { HCI_SET, HCI_ECO_MODE, 0, 1, 0, 0 };
724 u32 out[TCI_WORDS];
725 acpi_status status;
726
727 /* Switch the Eco Mode led on/off */
728 in[2] = (brightness) ? 1 : 0;
729 status = tci_raw(dev, in, out);
730 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
731 pr_err("ACPI call to set ECO led failed\n");
732 return;
733 }
734 }
735
736 /* Accelerometer support */
737 static int toshiba_accelerometer_supported(struct toshiba_acpi_dev *dev)
738 {
739 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER2, 0, 0, 0, 0 };
740 u32 out[TCI_WORDS];
741 acpi_status status;
742
743 /*
744 * Check if the accelerometer call exists,
745 * this call also serves as initialization
746 */
747 status = tci_raw(dev, in, out);
748 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
749 pr_err("ACPI call to query the accelerometer failed\n");
750 return -EIO;
751 } else if (out[0] == TOS_DATA_NOT_AVAILABLE ||
752 out[0] == TOS_NOT_INITIALIZED) {
753 pr_err("Accelerometer not initialized\n");
754 return -EIO;
755 } else if (out[0] == TOS_NOT_SUPPORTED) {
756 pr_info("Accelerometer not supported\n");
757 return -ENODEV;
758 }
759
760 return 0;
761 }
762
763 static int toshiba_accelerometer_get(struct toshiba_acpi_dev *dev,
764 u32 *xy, u32 *z)
765 {
766 u32 in[TCI_WORDS] = { HCI_GET, HCI_ACCELEROMETER, 0, 1, 0, 0 };
767 u32 out[TCI_WORDS];
768 acpi_status status;
769
770 /* Check the Accelerometer status */
771 status = tci_raw(dev, in, out);
772 if (ACPI_FAILURE(status) || out[0] == TOS_INPUT_DATA_ERROR) {
773 pr_err("ACPI call to query the accelerometer failed\n");
774 return -EIO;
775 }
776
777 *xy = out[2];
778 *z = out[4];
779
780 return 0;
781 }
782
783 /* Sleep (Charge and Music) utilities support */
784 static void toshiba_usb_sleep_charge_available(struct toshiba_acpi_dev *dev)
785 {
786 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
787 u32 out[TCI_WORDS];
788 acpi_status status;
789
790 /* Set the feature to "not supported" in case of error */
791 dev->usb_sleep_charge_supported = 0;
792
793 if (!sci_open(dev))
794 return;
795
796 status = tci_raw(dev, in, out);
797 if (ACPI_FAILURE(status)) {
798 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
799 sci_close(dev);
800 return;
801 } else if (out[0] == TOS_NOT_SUPPORTED) {
802 pr_info("USB Sleep and Charge not supported\n");
803 sci_close(dev);
804 return;
805 } else if (out[0] == TOS_SUCCESS) {
806 dev->usbsc_mode_base = out[4];
807 }
808
809 in[5] = SCI_USB_CHARGE_BAT_LVL;
810 status = tci_raw(dev, in, out);
811 if (ACPI_FAILURE(status)) {
812 pr_err("ACPI call to get USB Sleep and Charge mode failed\n");
813 sci_close(dev);
814 return;
815 } else if (out[0] == TOS_NOT_SUPPORTED) {
816 pr_info("USB Sleep and Charge not supported\n");
817 sci_close(dev);
818 return;
819 } else if (out[0] == TOS_SUCCESS) {
820 dev->usbsc_bat_level = out[2];
821 /*
822 * If we reach this point, it means that the laptop has support
823 * for this feature and all values are initialized.
824 * Set it as supported.
825 */
826 dev->usb_sleep_charge_supported = 1;
827 }
828
829 sci_close(dev);
830 }
831
832 static int toshiba_usb_sleep_charge_get(struct toshiba_acpi_dev *dev,
833 u32 *mode)
834 {
835 u32 result;
836
837 if (!sci_open(dev))
838 return -EIO;
839
840 result = sci_read(dev, SCI_USB_SLEEP_CHARGE, mode);
841 sci_close(dev);
842 if (result == TOS_FAILURE) {
843 pr_err("ACPI call to set USB S&C mode failed\n");
844 return -EIO;
845 } else if (result == TOS_NOT_SUPPORTED) {
846 pr_info("USB Sleep and Charge not supported\n");
847 return -ENODEV;
848 } else if (result == TOS_INPUT_DATA_ERROR) {
849 return -EIO;
850 }
851
852 return 0;
853 }
854
855 static int toshiba_usb_sleep_charge_set(struct toshiba_acpi_dev *dev,
856 u32 mode)
857 {
858 u32 result;
859
860 if (!sci_open(dev))
861 return -EIO;
862
863 result = sci_write(dev, SCI_USB_SLEEP_CHARGE, mode);
864 sci_close(dev);
865 if (result == TOS_FAILURE) {
866 pr_err("ACPI call to set USB S&C mode failed\n");
867 return -EIO;
868 } else if (result == TOS_NOT_SUPPORTED) {
869 pr_info("USB Sleep and Charge not supported\n");
870 return -ENODEV;
871 } else if (result == TOS_INPUT_DATA_ERROR) {
872 return -EIO;
873 }
874
875 return 0;
876 }
877
878 static int toshiba_sleep_functions_status_get(struct toshiba_acpi_dev *dev,
879 u32 *mode)
880 {
881 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
882 u32 out[TCI_WORDS];
883 acpi_status status;
884
885 if (!sci_open(dev))
886 return -EIO;
887
888 in[5] = SCI_USB_CHARGE_BAT_LVL;
889 status = tci_raw(dev, in, out);
890 sci_close(dev);
891 if (ACPI_FAILURE(status)) {
892 pr_err("ACPI call to get USB S&C battery level failed\n");
893 return -EIO;
894 } else if (out[0] == TOS_NOT_SUPPORTED) {
895 pr_info("USB Sleep and Charge not supported\n");
896 return -ENODEV;
897 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
898 return -EIO;
899 }
900
901 *mode = out[2];
902
903 return 0;
904 }
905
906 static int toshiba_sleep_functions_status_set(struct toshiba_acpi_dev *dev,
907 u32 mode)
908 {
909 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
910 u32 out[TCI_WORDS];
911 acpi_status status;
912
913 if (!sci_open(dev))
914 return -EIO;
915
916 in[2] = mode;
917 in[5] = SCI_USB_CHARGE_BAT_LVL;
918 status = tci_raw(dev, in, out);
919 sci_close(dev);
920 if (ACPI_FAILURE(status)) {
921 pr_err("ACPI call to set USB S&C battery level failed\n");
922 return -EIO;
923 } else if (out[0] == TOS_NOT_SUPPORTED) {
924 pr_info("USB Sleep and Charge not supported\n");
925 return -ENODEV;
926 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
927 return -EIO;
928 }
929
930 return 0;
931 }
932
933 static int toshiba_usb_rapid_charge_get(struct toshiba_acpi_dev *dev,
934 u32 *state)
935 {
936 u32 in[TCI_WORDS] = { SCI_GET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
937 u32 out[TCI_WORDS];
938 acpi_status status;
939
940 if (!sci_open(dev))
941 return -EIO;
942
943 in[5] = SCI_USB_CHARGE_RAPID_DSP;
944 status = tci_raw(dev, in, out);
945 sci_close(dev);
946 if (ACPI_FAILURE(status)) {
947 pr_err("ACPI call to get USB Rapid Charge failed\n");
948 return -EIO;
949 } else if (out[0] == TOS_NOT_SUPPORTED ||
950 out[0] == TOS_INPUT_DATA_ERROR) {
951 pr_info("USB Rapid Charge not supported\n");
952 return -ENODEV;
953 }
954
955 *state = out[2];
956
957 return 0;
958 }
959
960 static int toshiba_usb_rapid_charge_set(struct toshiba_acpi_dev *dev,
961 u32 state)
962 {
963 u32 in[TCI_WORDS] = { SCI_SET, SCI_USB_SLEEP_CHARGE, 0, 0, 0, 0 };
964 u32 out[TCI_WORDS];
965 acpi_status status;
966
967 if (!sci_open(dev))
968 return -EIO;
969
970 in[2] = state;
971 in[5] = SCI_USB_CHARGE_RAPID_DSP;
972 status = tci_raw(dev, in, out);
973 sci_close(dev);
974 if (ACPI_FAILURE(status)) {
975 pr_err("ACPI call to set USB Rapid Charge failed\n");
976 return -EIO;
977 } else if (out[0] == TOS_NOT_SUPPORTED) {
978 pr_info("USB Rapid Charge not supported\n");
979 return -ENODEV;
980 } else if (out[0] == TOS_INPUT_DATA_ERROR) {
981 return -EIO;
982 }
983
984 return 0;
985 }
986
987 static int toshiba_usb_sleep_music_get(struct toshiba_acpi_dev *dev, u32 *state)
988 {
989 u32 result;
990
991 if (!sci_open(dev))
992 return -EIO;
993
994 result = sci_read(dev, SCI_USB_SLEEP_MUSIC, state);
995 sci_close(dev);
996 if (result == TOS_FAILURE) {
997 pr_err("ACPI call to get Sleep and Music failed\n");
998 return -EIO;
999 } else if (result == TOS_NOT_SUPPORTED) {
1000 pr_info("Sleep and Music not supported\n");
1001 return -ENODEV;
1002 } else if (result == TOS_INPUT_DATA_ERROR) {
1003 return -EIO;
1004 }
1005
1006 return 0;
1007 }
1008
1009 static int toshiba_usb_sleep_music_set(struct toshiba_acpi_dev *dev, u32 state)
1010 {
1011 u32 result;
1012
1013 if (!sci_open(dev))
1014 return -EIO;
1015
1016 result = sci_write(dev, SCI_USB_SLEEP_MUSIC, state);
1017 sci_close(dev);
1018 if (result == TOS_FAILURE) {
1019 pr_err("ACPI call to set Sleep and Music failed\n");
1020 return -EIO;
1021 } else if (result == TOS_NOT_SUPPORTED) {
1022 pr_info("Sleep and Music not supported\n");
1023 return -ENODEV;
1024 } else if (result == TOS_INPUT_DATA_ERROR) {
1025 return -EIO;
1026 }
1027
1028 return 0;
1029 }
1030
1031 /* Keyboard function keys */
1032 static int toshiba_function_keys_get(struct toshiba_acpi_dev *dev, u32 *mode)
1033 {
1034 u32 result;
1035
1036 if (!sci_open(dev))
1037 return -EIO;
1038
1039 result = sci_read(dev, SCI_KBD_FUNCTION_KEYS, mode);
1040 sci_close(dev);
1041 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1042 pr_err("ACPI call to get KBD function keys failed\n");
1043 return -EIO;
1044 } else if (result == TOS_NOT_SUPPORTED) {
1045 pr_info("KBD function keys not supported\n");
1046 return -ENODEV;
1047 }
1048
1049 return 0;
1050 }
1051
1052 static int toshiba_function_keys_set(struct toshiba_acpi_dev *dev, u32 mode)
1053 {
1054 u32 result;
1055
1056 if (!sci_open(dev))
1057 return -EIO;
1058
1059 result = sci_write(dev, SCI_KBD_FUNCTION_KEYS, mode);
1060 sci_close(dev);
1061 if (result == TOS_FAILURE || result == TOS_INPUT_DATA_ERROR) {
1062 pr_err("ACPI call to set KBD function keys failed\n");
1063 return -EIO;
1064 } else if (result == TOS_NOT_SUPPORTED) {
1065 pr_info("KBD function keys not supported\n");
1066 return -ENODEV;
1067 }
1068
1069 return 0;
1070 }
1071
1072 /* Panel Power ON */
1073 static int toshiba_panel_power_on_get(struct toshiba_acpi_dev *dev, u32 *state)
1074 {
1075 u32 result;
1076
1077 if (!sci_open(dev))
1078 return -EIO;
1079
1080 result = sci_read(dev, SCI_PANEL_POWER_ON, state);
1081 sci_close(dev);
1082 if (result == TOS_FAILURE) {
1083 pr_err("ACPI call to get Panel Power ON failed\n");
1084 return -EIO;
1085 } else if (result == TOS_NOT_SUPPORTED) {
1086 pr_info("Panel Power on not supported\n");
1087 return -ENODEV;
1088 } else if (result == TOS_INPUT_DATA_ERROR) {
1089 return -EIO;
1090 }
1091
1092 return 0;
1093 }
1094
1095 static int toshiba_panel_power_on_set(struct toshiba_acpi_dev *dev, u32 state)
1096 {
1097 u32 result;
1098
1099 if (!sci_open(dev))
1100 return -EIO;
1101
1102 result = sci_write(dev, SCI_PANEL_POWER_ON, state);
1103 sci_close(dev);
1104 if (result == TOS_FAILURE) {
1105 pr_err("ACPI call to set Panel Power ON failed\n");
1106 return -EIO;
1107 } else if (result == TOS_NOT_SUPPORTED) {
1108 pr_info("Panel Power ON not supported\n");
1109 return -ENODEV;
1110 } else if (result == TOS_INPUT_DATA_ERROR) {
1111 return -EIO;
1112 }
1113
1114 return 0;
1115 }
1116
1117 /* USB Three */
1118 static int toshiba_usb_three_get(struct toshiba_acpi_dev *dev, u32 *state)
1119 {
1120 u32 result;
1121
1122 if (!sci_open(dev))
1123 return -EIO;
1124
1125 result = sci_read(dev, SCI_USB_THREE, state);
1126 sci_close(dev);
1127 if (result == TOS_FAILURE) {
1128 pr_err("ACPI call to get USB 3 failed\n");
1129 return -EIO;
1130 } else if (result == TOS_NOT_SUPPORTED) {
1131 pr_info("USB 3 not supported\n");
1132 return -ENODEV;
1133 } else if (result == TOS_INPUT_DATA_ERROR) {
1134 return -EIO;
1135 }
1136
1137 return 0;
1138 }
1139
1140 static int toshiba_usb_three_set(struct toshiba_acpi_dev *dev, u32 state)
1141 {
1142 u32 result;
1143
1144 if (!sci_open(dev))
1145 return -EIO;
1146
1147 result = sci_write(dev, SCI_USB_THREE, state);
1148 sci_close(dev);
1149 if (result == TOS_FAILURE) {
1150 pr_err("ACPI call to set USB 3 failed\n");
1151 return -EIO;
1152 } else if (result == TOS_NOT_SUPPORTED) {
1153 pr_info("USB 3 not supported\n");
1154 return -ENODEV;
1155 } else if (result == TOS_INPUT_DATA_ERROR) {
1156 return -EIO;
1157 }
1158
1159 return 0;
1160 }
1161
1162 /* Hotkey Event type */
1163 static int toshiba_hotkey_event_type_get(struct toshiba_acpi_dev *dev,
1164 u32 *type)
1165 {
1166 u32 in[TCI_WORDS] = { HCI_GET, HCI_SYSTEM_INFO, 0x03, 0, 0, 0 };
1167 u32 out[TCI_WORDS];
1168 acpi_status status;
1169
1170 status = tci_raw(dev, in, out);
1171 if (ACPI_FAILURE(status)) {
1172 pr_err("ACPI call to get System type failed\n");
1173 return -EIO;
1174 } else if (out[0] == TOS_NOT_SUPPORTED) {
1175 pr_info("System type not supported\n");
1176 return -ENODEV;
1177 }
1178
1179 *type = out[3];
1180
1181 return 0;
1182 }
1183
1184 /* Transflective Backlight */
1185 static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 *status)
1186 {
1187 u32 hci_result = hci_read(dev, HCI_TR_BACKLIGHT, status);
1188
1189 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1190 }
1191
1192 static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, u32 status)
1193 {
1194 u32 hci_result = hci_write(dev, HCI_TR_BACKLIGHT, !status);
1195
1196 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1197 }
1198
1199 static struct proc_dir_entry *toshiba_proc_dir;
1200
1201 /* LCD Brightness */
1202 static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
1203 {
1204 u32 hci_result;
1205 u32 value;
1206 int brightness = 0;
1207
1208 if (dev->tr_backlight_supported) {
1209 int ret = get_tr_backlight_status(dev, &value);
1210
1211 if (ret)
1212 return ret;
1213 if (value)
1214 return 0;
1215 brightness++;
1216 }
1217
1218 hci_result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
1219 if (hci_result == TOS_SUCCESS)
1220 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
1221
1222 return -EIO;
1223 }
1224
1225 static int get_lcd_brightness(struct backlight_device *bd)
1226 {
1227 struct toshiba_acpi_dev *dev = bl_get_data(bd);
1228
1229 return __get_lcd_brightness(dev);
1230 }
1231
1232 static int lcd_proc_show(struct seq_file *m, void *v)
1233 {
1234 struct toshiba_acpi_dev *dev = m->private;
1235 int value;
1236 int levels;
1237
1238 if (!dev->backlight_dev)
1239 return -ENODEV;
1240
1241 levels = dev->backlight_dev->props.max_brightness + 1;
1242 value = get_lcd_brightness(dev->backlight_dev);
1243 if (value >= 0) {
1244 seq_printf(m, "brightness: %d\n", value);
1245 seq_printf(m, "brightness_levels: %d\n", levels);
1246 return 0;
1247 }
1248
1249 pr_err("Error reading LCD brightness\n");
1250 return -EIO;
1251 }
1252
1253 static int lcd_proc_open(struct inode *inode, struct file *file)
1254 {
1255 return single_open(file, lcd_proc_show, PDE_DATA(inode));
1256 }
1257
1258 static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
1259 {
1260 u32 hci_result;
1261
1262 if (dev->tr_backlight_supported) {
1263 int ret = set_tr_backlight_status(dev, !value);
1264
1265 if (ret)
1266 return ret;
1267 if (value)
1268 value--;
1269 }
1270
1271 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1272 hci_result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
1273 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1274 }
1275
1276 static int set_lcd_status(struct backlight_device *bd)
1277 {
1278 struct toshiba_acpi_dev *dev = bl_get_data(bd);
1279
1280 return set_lcd_brightness(dev, bd->props.brightness);
1281 }
1282
1283 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1284 size_t count, loff_t *pos)
1285 {
1286 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1287 char cmd[42];
1288 size_t len;
1289 int value;
1290 int ret;
1291 int levels = dev->backlight_dev->props.max_brightness + 1;
1292
1293 len = min(count, sizeof(cmd) - 1);
1294 if (copy_from_user(cmd, buf, len))
1295 return -EFAULT;
1296 cmd[len] = '\0';
1297
1298 if (sscanf(cmd, " brightness : %i", &value) == 1 &&
1299 value >= 0 && value < levels) {
1300 ret = set_lcd_brightness(dev, value);
1301 if (ret == 0)
1302 ret = count;
1303 } else {
1304 ret = -EINVAL;
1305 }
1306 return ret;
1307 }
1308
1309 static const struct file_operations lcd_proc_fops = {
1310 .owner = THIS_MODULE,
1311 .open = lcd_proc_open,
1312 .read = seq_read,
1313 .llseek = seq_lseek,
1314 .release = single_release,
1315 .write = lcd_proc_write,
1316 };
1317
1318 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1319 {
1320 u32 hci_result;
1321
1322 hci_result = hci_read(dev, HCI_VIDEO_OUT, status);
1323 return hci_result == TOS_SUCCESS ? 0 : -EIO;
1324 }
1325
1326 static int video_proc_show(struct seq_file *m, void *v)
1327 {
1328 struct toshiba_acpi_dev *dev = m->private;
1329 u32 value;
1330 int ret;
1331
1332 ret = get_video_status(dev, &value);
1333 if (!ret) {
1334 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1335 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
1336 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
1337
1338 seq_printf(m, "lcd_out: %d\n", is_lcd);
1339 seq_printf(m, "crt_out: %d\n", is_crt);
1340 seq_printf(m, "tv_out: %d\n", is_tv);
1341 }
1342
1343 return ret;
1344 }
1345
1346 static int video_proc_open(struct inode *inode, struct file *file)
1347 {
1348 return single_open(file, video_proc_show, PDE_DATA(inode));
1349 }
1350
1351 static ssize_t video_proc_write(struct file *file, const char __user *buf,
1352 size_t count, loff_t *pos)
1353 {
1354 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1355 char *cmd, *buffer;
1356 int ret;
1357 int value;
1358 int remain = count;
1359 int lcd_out = -1;
1360 int crt_out = -1;
1361 int tv_out = -1;
1362 u32 video_out;
1363
1364 cmd = kmalloc(count + 1, GFP_KERNEL);
1365 if (!cmd)
1366 return -ENOMEM;
1367 if (copy_from_user(cmd, buf, count)) {
1368 kfree(cmd);
1369 return -EFAULT;
1370 }
1371 cmd[count] = '\0';
1372
1373 buffer = cmd;
1374
1375 /*
1376 * Scan expression. Multiple expressions may be delimited with ;
1377 * NOTE: To keep scanning simple, invalid fields are ignored.
1378 */
1379 while (remain) {
1380 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1381 lcd_out = value & 1;
1382 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1383 crt_out = value & 1;
1384 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1385 tv_out = value & 1;
1386 /* Advance to one character past the next ; */
1387 do {
1388 ++buffer;
1389 --remain;
1390 } while (remain && *(buffer - 1) != ';');
1391 }
1392
1393 kfree(cmd);
1394
1395 ret = get_video_status(dev, &video_out);
1396 if (!ret) {
1397 unsigned int new_video_out = video_out;
1398
1399 if (lcd_out != -1)
1400 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1401 if (crt_out != -1)
1402 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1403 if (tv_out != -1)
1404 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1405 /*
1406 * To avoid unnecessary video disruption, only write the new
1407 * video setting if something changed.
1408 */
1409 if (new_video_out != video_out)
1410 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1411 }
1412
1413 return ret ? ret : count;
1414 }
1415
1416 static const struct file_operations video_proc_fops = {
1417 .owner = THIS_MODULE,
1418 .open = video_proc_open,
1419 .read = seq_read,
1420 .llseek = seq_lseek,
1421 .release = single_release,
1422 .write = video_proc_write,
1423 };
1424
1425 /* Fan status */
1426 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1427 {
1428 u32 result = hci_read(dev, HCI_FAN, status);
1429
1430 if (result == TOS_FAILURE)
1431 pr_err("ACPI call to get Fan status failed\n");
1432 else if (result == TOS_NOT_SUPPORTED)
1433 return -ENODEV;
1434 else if (result == TOS_SUCCESS)
1435 return 0;
1436
1437 return -EIO;
1438 }
1439
1440 static int set_fan_status(struct toshiba_acpi_dev *dev, u32 status)
1441 {
1442 u32 result = hci_write(dev, HCI_FAN, status);
1443
1444 if (result == TOS_FAILURE)
1445 pr_err("ACPI call to set Fan status failed\n");
1446 else if (result == TOS_NOT_SUPPORTED)
1447 return -ENODEV;
1448 else if (result == TOS_SUCCESS)
1449 return 0;
1450
1451 return -EIO;
1452 }
1453
1454 static int fan_proc_show(struct seq_file *m, void *v)
1455 {
1456 struct toshiba_acpi_dev *dev = m->private;
1457 u32 value;
1458
1459 if (get_fan_status(dev, &value))
1460 return -EIO;
1461
1462 seq_printf(m, "running: %d\n", (value > 0));
1463 seq_printf(m, "force_on: %d\n", dev->force_fan);
1464
1465 return 0;
1466 }
1467
1468 static int fan_proc_open(struct inode *inode, struct file *file)
1469 {
1470 return single_open(file, fan_proc_show, PDE_DATA(inode));
1471 }
1472
1473 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1474 size_t count, loff_t *pos)
1475 {
1476 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1477 char cmd[42];
1478 size_t len;
1479 int value;
1480
1481 len = min(count, sizeof(cmd) - 1);
1482 if (copy_from_user(cmd, buf, len))
1483 return -EFAULT;
1484 cmd[len] = '\0';
1485
1486 if (sscanf(cmd, " force_on : %i", &value) != 1 &&
1487 value != 0 && value != 1)
1488 return -EINVAL;
1489
1490 if (set_fan_status(dev, value))
1491 return -EIO;
1492
1493 dev->force_fan = value;
1494
1495 return count;
1496 }
1497
1498 static const struct file_operations fan_proc_fops = {
1499 .owner = THIS_MODULE,
1500 .open = fan_proc_open,
1501 .read = seq_read,
1502 .llseek = seq_lseek,
1503 .release = single_release,
1504 .write = fan_proc_write,
1505 };
1506
1507 static int keys_proc_show(struct seq_file *m, void *v)
1508 {
1509 struct toshiba_acpi_dev *dev = m->private;
1510
1511 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid);
1512 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event);
1513
1514 return 0;
1515 }
1516
1517 static int keys_proc_open(struct inode *inode, struct file *file)
1518 {
1519 return single_open(file, keys_proc_show, PDE_DATA(inode));
1520 }
1521
1522 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1523 size_t count, loff_t *pos)
1524 {
1525 struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1526 char cmd[42];
1527 size_t len;
1528 int value;
1529
1530 len = min(count, sizeof(cmd) - 1);
1531 if (copy_from_user(cmd, buf, len))
1532 return -EFAULT;
1533 cmd[len] = '\0';
1534
1535 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
1536 dev->key_event_valid = 0;
1537 else
1538 return -EINVAL;
1539
1540 return count;
1541 }
1542
1543 static const struct file_operations keys_proc_fops = {
1544 .owner = THIS_MODULE,
1545 .open = keys_proc_open,
1546 .read = seq_read,
1547 .llseek = seq_lseek,
1548 .release = single_release,
1549 .write = keys_proc_write,
1550 };
1551
1552 static int version_proc_show(struct seq_file *m, void *v)
1553 {
1554 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION);
1555 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION);
1556 return 0;
1557 }
1558
1559 static int version_proc_open(struct inode *inode, struct file *file)
1560 {
1561 return single_open(file, version_proc_show, PDE_DATA(inode));
1562 }
1563
1564 static const struct file_operations version_proc_fops = {
1565 .owner = THIS_MODULE,
1566 .open = version_proc_open,
1567 .read = seq_read,
1568 .llseek = seq_lseek,
1569 .release = single_release,
1570 };
1571
1572 /*
1573 * Proc and module init
1574 */
1575
1576 #define PROC_TOSHIBA "toshiba"
1577
1578 static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1579 {
1580 if (dev->backlight_dev)
1581 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1582 &lcd_proc_fops, dev);
1583 if (dev->video_supported)
1584 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1585 &video_proc_fops, dev);
1586 if (dev->fan_supported)
1587 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1588 &fan_proc_fops, dev);
1589 if (dev->hotkey_dev)
1590 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1591 &keys_proc_fops, dev);
1592 proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1593 &version_proc_fops, dev);
1594 }
1595
1596 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1597 {
1598 if (dev->backlight_dev)
1599 remove_proc_entry("lcd", toshiba_proc_dir);
1600 if (dev->video_supported)
1601 remove_proc_entry("video", toshiba_proc_dir);
1602 if (dev->fan_supported)
1603 remove_proc_entry("fan", toshiba_proc_dir);
1604 if (dev->hotkey_dev)
1605 remove_proc_entry("keys", toshiba_proc_dir);
1606 remove_proc_entry("version", toshiba_proc_dir);
1607 }
1608
1609 static const struct backlight_ops toshiba_backlight_data = {
1610 .options = BL_CORE_SUSPENDRESUME,
1611 .get_brightness = get_lcd_brightness,
1612 .update_status = set_lcd_status,
1613 };
1614
1615 /*
1616 * Sysfs files
1617 */
1618 static ssize_t version_show(struct device *dev,
1619 struct device_attribute *attr, char *buf)
1620 {
1621 return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1622 }
1623 static DEVICE_ATTR_RO(version);
1624
1625 static ssize_t fan_store(struct device *dev,
1626 struct device_attribute *attr,
1627 const char *buf, size_t count)
1628 {
1629 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1630 int state;
1631 int ret;
1632
1633 ret = kstrtoint(buf, 0, &state);
1634 if (ret)
1635 return ret;
1636
1637 if (state != 0 && state != 1)
1638 return -EINVAL;
1639
1640 ret = set_fan_status(toshiba, state);
1641 if (ret)
1642 return ret;
1643
1644 return count;
1645 }
1646
1647 static ssize_t fan_show(struct device *dev,
1648 struct device_attribute *attr, char *buf)
1649 {
1650 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1651 u32 value;
1652 int ret;
1653
1654 ret = get_fan_status(toshiba, &value);
1655 if (ret)
1656 return ret;
1657
1658 return sprintf(buf, "%d\n", value);
1659 }
1660 static DEVICE_ATTR_RW(fan);
1661
1662 static ssize_t kbd_backlight_mode_store(struct device *dev,
1663 struct device_attribute *attr,
1664 const char *buf, size_t count)
1665 {
1666 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1667 int mode;
1668 int ret;
1669
1670
1671 ret = kstrtoint(buf, 0, &mode);
1672 if (ret)
1673 return ret;
1674
1675 /* Check for supported modes depending on keyboard backlight type */
1676 if (toshiba->kbd_type == 1) {
1677 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1678 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1679 return -EINVAL;
1680 } else if (toshiba->kbd_type == 2) {
1681 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1682 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1683 mode != SCI_KBD_MODE_OFF)
1684 return -EINVAL;
1685 }
1686
1687 /*
1688 * Set the Keyboard Backlight Mode where:
1689 * Auto - KBD backlight turns off automatically in given time
1690 * FN-Z - KBD backlight "toggles" when hotkey pressed
1691 * ON - KBD backlight is always on
1692 * OFF - KBD backlight is always off
1693 */
1694
1695 /* Only make a change if the actual mode has changed */
1696 if (toshiba->kbd_mode != mode) {
1697 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1698 int time = toshiba->kbd_time << HCI_MISC_SHIFT;
1699
1700 /* OR the "base time" to the actual method format */
1701 if (toshiba->kbd_type == 1) {
1702 /* Type 1 requires the current mode */
1703 time |= toshiba->kbd_mode;
1704 } else if (toshiba->kbd_type == 2) {
1705 /* Type 2 requires the desired mode */
1706 time |= mode;
1707 }
1708
1709 ret = toshiba_kbd_illum_status_set(toshiba, time);
1710 if (ret)
1711 return ret;
1712
1713 toshiba->kbd_mode = mode;
1714 }
1715
1716 return count;
1717 }
1718
1719 static ssize_t kbd_backlight_mode_show(struct device *dev,
1720 struct device_attribute *attr,
1721 char *buf)
1722 {
1723 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1724 u32 time;
1725
1726 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1727 return -EIO;
1728
1729 return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1730 }
1731 static DEVICE_ATTR_RW(kbd_backlight_mode);
1732
1733 static ssize_t kbd_type_show(struct device *dev,
1734 struct device_attribute *attr, char *buf)
1735 {
1736 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1737
1738 return sprintf(buf, "%d\n", toshiba->kbd_type);
1739 }
1740 static DEVICE_ATTR_RO(kbd_type);
1741
1742 static ssize_t available_kbd_modes_show(struct device *dev,
1743 struct device_attribute *attr,
1744 char *buf)
1745 {
1746 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1747
1748 if (toshiba->kbd_type == 1)
1749 return sprintf(buf, "%x %x\n",
1750 SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1751
1752 return sprintf(buf, "%x %x %x\n",
1753 SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
1754 }
1755 static DEVICE_ATTR_RO(available_kbd_modes);
1756
1757 static ssize_t kbd_backlight_timeout_store(struct device *dev,
1758 struct device_attribute *attr,
1759 const char *buf, size_t count)
1760 {
1761 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1762 int time;
1763 int ret;
1764
1765 ret = kstrtoint(buf, 0, &time);
1766 if (ret)
1767 return ret;
1768
1769 /* Check for supported values depending on kbd_type */
1770 if (toshiba->kbd_type == 1) {
1771 if (time < 0 || time > 60)
1772 return -EINVAL;
1773 } else if (toshiba->kbd_type == 2) {
1774 if (time < 1 || time > 60)
1775 return -EINVAL;
1776 }
1777
1778 /* Set the Keyboard Backlight Timeout */
1779
1780 /* Only make a change if the actual timeout has changed */
1781 if (toshiba->kbd_time != time) {
1782 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1783 time = time << HCI_MISC_SHIFT;
1784 /* OR the "base time" to the actual method format */
1785 if (toshiba->kbd_type == 1)
1786 time |= SCI_KBD_MODE_FNZ;
1787 else if (toshiba->kbd_type == 2)
1788 time |= SCI_KBD_MODE_AUTO;
1789
1790 ret = toshiba_kbd_illum_status_set(toshiba, time);
1791 if (ret)
1792 return ret;
1793
1794 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1795 }
1796
1797 return count;
1798 }
1799
1800 static ssize_t kbd_backlight_timeout_show(struct device *dev,
1801 struct device_attribute *attr,
1802 char *buf)
1803 {
1804 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1805 u32 time;
1806
1807 if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1808 return -EIO;
1809
1810 return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1811 }
1812 static DEVICE_ATTR_RW(kbd_backlight_timeout);
1813
1814 static ssize_t touchpad_store(struct device *dev,
1815 struct device_attribute *attr,
1816 const char *buf, size_t count)
1817 {
1818 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1819 int state;
1820 int ret;
1821
1822 /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1823 ret = kstrtoint(buf, 0, &state);
1824 if (ret)
1825 return ret;
1826 if (state != 0 && state != 1)
1827 return -EINVAL;
1828
1829 ret = toshiba_touchpad_set(toshiba, state);
1830 if (ret)
1831 return ret;
1832
1833 return count;
1834 }
1835
1836 static ssize_t touchpad_show(struct device *dev,
1837 struct device_attribute *attr, char *buf)
1838 {
1839 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1840 u32 state;
1841 int ret;
1842
1843 ret = toshiba_touchpad_get(toshiba, &state);
1844 if (ret < 0)
1845 return ret;
1846
1847 return sprintf(buf, "%i\n", state);
1848 }
1849 static DEVICE_ATTR_RW(touchpad);
1850
1851 static ssize_t position_show(struct device *dev,
1852 struct device_attribute *attr, char *buf)
1853 {
1854 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1855 u32 xyval, zval, tmp;
1856 u16 x, y, z;
1857 int ret;
1858
1859 xyval = zval = 0;
1860 ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1861 if (ret < 0)
1862 return ret;
1863
1864 x = xyval & HCI_ACCEL_MASK;
1865 tmp = xyval >> HCI_MISC_SHIFT;
1866 y = tmp & HCI_ACCEL_MASK;
1867 z = zval & HCI_ACCEL_MASK;
1868
1869 return sprintf(buf, "%d %d %d\n", x, y, z);
1870 }
1871 static DEVICE_ATTR_RO(position);
1872
1873 static ssize_t usb_sleep_charge_show(struct device *dev,
1874 struct device_attribute *attr, char *buf)
1875 {
1876 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1877 u32 mode;
1878 int ret;
1879
1880 ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1881 if (ret < 0)
1882 return ret;
1883
1884 return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1885 }
1886
1887 static ssize_t usb_sleep_charge_store(struct device *dev,
1888 struct device_attribute *attr,
1889 const char *buf, size_t count)
1890 {
1891 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1892 u32 mode;
1893 int state;
1894 int ret;
1895
1896 ret = kstrtoint(buf, 0, &state);
1897 if (ret)
1898 return ret;
1899 /*
1900 * Check for supported values, where:
1901 * 0 - Disabled
1902 * 1 - Alternate (Non USB conformant devices that require more power)
1903 * 2 - Auto (USB conformant devices)
1904 * 3 - Typical
1905 */
1906 if (state != 0 && state != 1 && state != 2 && state != 3)
1907 return -EINVAL;
1908
1909 /* Set the USB charging mode to internal value */
1910 mode = toshiba->usbsc_mode_base;
1911 if (state == 0)
1912 mode |= SCI_USB_CHARGE_DISABLED;
1913 else if (state == 1)
1914 mode |= SCI_USB_CHARGE_ALTERNATE;
1915 else if (state == 2)
1916 mode |= SCI_USB_CHARGE_AUTO;
1917 else if (state == 3)
1918 mode |= SCI_USB_CHARGE_TYPICAL;
1919
1920 ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1921 if (ret)
1922 return ret;
1923
1924 return count;
1925 }
1926 static DEVICE_ATTR_RW(usb_sleep_charge);
1927
1928 static ssize_t sleep_functions_on_battery_show(struct device *dev,
1929 struct device_attribute *attr,
1930 char *buf)
1931 {
1932 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1933 u32 state;
1934 int bat_lvl;
1935 int status;
1936 int ret;
1937 int tmp;
1938
1939 ret = toshiba_sleep_functions_status_get(toshiba, &state);
1940 if (ret < 0)
1941 return ret;
1942
1943 /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1944 tmp = state & SCI_USB_CHARGE_BAT_MASK;
1945 status = (tmp == 0x4) ? 1 : 0;
1946 /* Determine the battery level set */
1947 bat_lvl = state >> HCI_MISC_SHIFT;
1948
1949 return sprintf(buf, "%d %d\n", status, bat_lvl);
1950 }
1951
1952 static ssize_t sleep_functions_on_battery_store(struct device *dev,
1953 struct device_attribute *attr,
1954 const char *buf, size_t count)
1955 {
1956 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1957 u32 status;
1958 int value;
1959 int ret;
1960 int tmp;
1961
1962 ret = kstrtoint(buf, 0, &value);
1963 if (ret)
1964 return ret;
1965
1966 /*
1967 * Set the status of the function:
1968 * 0 - Disabled
1969 * 1-100 - Enabled
1970 */
1971 if (value < 0 || value > 100)
1972 return -EINVAL;
1973
1974 if (value == 0) {
1975 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
1976 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
1977 } else {
1978 tmp = value << HCI_MISC_SHIFT;
1979 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
1980 }
1981 ret = toshiba_sleep_functions_status_set(toshiba, status);
1982 if (ret < 0)
1983 return ret;
1984
1985 toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
1986
1987 return count;
1988 }
1989 static DEVICE_ATTR_RW(sleep_functions_on_battery);
1990
1991 static ssize_t usb_rapid_charge_show(struct device *dev,
1992 struct device_attribute *attr, char *buf)
1993 {
1994 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1995 u32 state;
1996 int ret;
1997
1998 ret = toshiba_usb_rapid_charge_get(toshiba, &state);
1999 if (ret < 0)
2000 return ret;
2001
2002 return sprintf(buf, "%d\n", state);
2003 }
2004
2005 static ssize_t usb_rapid_charge_store(struct device *dev,
2006 struct device_attribute *attr,
2007 const char *buf, size_t count)
2008 {
2009 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2010 int state;
2011 int ret;
2012
2013 ret = kstrtoint(buf, 0, &state);
2014 if (ret)
2015 return ret;
2016 if (state != 0 && state != 1)
2017 return -EINVAL;
2018
2019 ret = toshiba_usb_rapid_charge_set(toshiba, state);
2020 if (ret)
2021 return ret;
2022
2023 return count;
2024 }
2025 static DEVICE_ATTR_RW(usb_rapid_charge);
2026
2027 static ssize_t usb_sleep_music_show(struct device *dev,
2028 struct device_attribute *attr, char *buf)
2029 {
2030 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2031 u32 state;
2032 int ret;
2033
2034 ret = toshiba_usb_sleep_music_get(toshiba, &state);
2035 if (ret < 0)
2036 return ret;
2037
2038 return sprintf(buf, "%d\n", state);
2039 }
2040
2041 static ssize_t usb_sleep_music_store(struct device *dev,
2042 struct device_attribute *attr,
2043 const char *buf, size_t count)
2044 {
2045 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2046 int state;
2047 int ret;
2048
2049 ret = kstrtoint(buf, 0, &state);
2050 if (ret)
2051 return ret;
2052 if (state != 0 && state != 1)
2053 return -EINVAL;
2054
2055 ret = toshiba_usb_sleep_music_set(toshiba, state);
2056 if (ret)
2057 return ret;
2058
2059 return count;
2060 }
2061 static DEVICE_ATTR_RW(usb_sleep_music);
2062
2063 static ssize_t kbd_function_keys_show(struct device *dev,
2064 struct device_attribute *attr, char *buf)
2065 {
2066 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2067 int mode;
2068 int ret;
2069
2070 ret = toshiba_function_keys_get(toshiba, &mode);
2071 if (ret < 0)
2072 return ret;
2073
2074 return sprintf(buf, "%d\n", mode);
2075 }
2076
2077 static ssize_t kbd_function_keys_store(struct device *dev,
2078 struct device_attribute *attr,
2079 const char *buf, size_t count)
2080 {
2081 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2082 int mode;
2083 int ret;
2084
2085 ret = kstrtoint(buf, 0, &mode);
2086 if (ret)
2087 return ret;
2088 /*
2089 * Check for the function keys mode where:
2090 * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2091 * 1 - Special functions (Opposite of the above setting)
2092 */
2093 if (mode != 0 && mode != 1)
2094 return -EINVAL;
2095
2096 ret = toshiba_function_keys_set(toshiba, mode);
2097 if (ret)
2098 return ret;
2099
2100 pr_info("Reboot for changes to KBD Function Keys to take effect");
2101
2102 return count;
2103 }
2104 static DEVICE_ATTR_RW(kbd_function_keys);
2105
2106 static ssize_t panel_power_on_show(struct device *dev,
2107 struct device_attribute *attr, char *buf)
2108 {
2109 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2110 u32 state;
2111 int ret;
2112
2113 ret = toshiba_panel_power_on_get(toshiba, &state);
2114 if (ret < 0)
2115 return ret;
2116
2117 return sprintf(buf, "%d\n", state);
2118 }
2119
2120 static ssize_t panel_power_on_store(struct device *dev,
2121 struct device_attribute *attr,
2122 const char *buf, size_t count)
2123 {
2124 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2125 int state;
2126 int ret;
2127
2128 ret = kstrtoint(buf, 0, &state);
2129 if (ret)
2130 return ret;
2131 if (state != 0 && state != 1)
2132 return -EINVAL;
2133
2134 ret = toshiba_panel_power_on_set(toshiba, state);
2135 if (ret)
2136 return ret;
2137
2138 pr_info("Reboot for changes to Panel Power ON to take effect");
2139
2140 return count;
2141 }
2142 static DEVICE_ATTR_RW(panel_power_on);
2143
2144 static ssize_t usb_three_show(struct device *dev,
2145 struct device_attribute *attr, char *buf)
2146 {
2147 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2148 u32 state;
2149 int ret;
2150
2151 ret = toshiba_usb_three_get(toshiba, &state);
2152 if (ret < 0)
2153 return ret;
2154
2155 return sprintf(buf, "%d\n", state);
2156 }
2157
2158 static ssize_t usb_three_store(struct device *dev,
2159 struct device_attribute *attr,
2160 const char *buf, size_t count)
2161 {
2162 struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2163 int state;
2164 int ret;
2165
2166 ret = kstrtoint(buf, 0, &state);
2167 if (ret)
2168 return ret;
2169 /*
2170 * Check for USB 3 mode where:
2171 * 0 - Disabled (Acts like a USB 2 port, saving power)
2172 * 1 - Enabled
2173 */
2174 if (state != 0 && state != 1)
2175 return -EINVAL;
2176
2177 ret = toshiba_usb_three_set(toshiba, state);
2178 if (ret)
2179 return ret;
2180
2181 pr_info("Reboot for changes to USB 3 to take effect");
2182
2183 return count;
2184 }
2185 static DEVICE_ATTR_RW(usb_three);
2186
2187 static struct attribute *toshiba_attributes[] = {
2188 &dev_attr_version.attr,
2189 &dev_attr_fan.attr,
2190 &dev_attr_kbd_backlight_mode.attr,
2191 &dev_attr_kbd_type.attr,
2192 &dev_attr_available_kbd_modes.attr,
2193 &dev_attr_kbd_backlight_timeout.attr,
2194 &dev_attr_touchpad.attr,
2195 &dev_attr_position.attr,
2196 &dev_attr_usb_sleep_charge.attr,
2197 &dev_attr_sleep_functions_on_battery.attr,
2198 &dev_attr_usb_rapid_charge.attr,
2199 &dev_attr_usb_sleep_music.attr,
2200 &dev_attr_kbd_function_keys.attr,
2201 &dev_attr_panel_power_on.attr,
2202 &dev_attr_usb_three.attr,
2203 NULL,
2204 };
2205
2206 static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2207 struct attribute *attr, int idx)
2208 {
2209 struct device *dev = container_of(kobj, struct device, kobj);
2210 struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2211 bool exists = true;
2212
2213 if (attr == &dev_attr_fan.attr)
2214 exists = (drv->fan_supported) ? true : false;
2215 else if (attr == &dev_attr_kbd_backlight_mode.attr)
2216 exists = (drv->kbd_illum_supported) ? true : false;
2217 else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2218 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
2219 else if (attr == &dev_attr_touchpad.attr)
2220 exists = (drv->touchpad_supported) ? true : false;
2221 else if (attr == &dev_attr_position.attr)
2222 exists = (drv->accelerometer_supported) ? true : false;
2223 else if (attr == &dev_attr_usb_sleep_charge.attr)
2224 exists = (drv->usb_sleep_charge_supported) ? true : false;
2225 else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2226 exists = (drv->usb_sleep_charge_supported) ? true : false;
2227 else if (attr == &dev_attr_usb_rapid_charge.attr)
2228 exists = (drv->usb_rapid_charge_supported) ? true : false;
2229 else if (attr == &dev_attr_usb_sleep_music.attr)
2230 exists = (drv->usb_sleep_music_supported) ? true : false;
2231 else if (attr == &dev_attr_kbd_function_keys.attr)
2232 exists = (drv->kbd_function_keys_supported) ? true : false;
2233 else if (attr == &dev_attr_panel_power_on.attr)
2234 exists = (drv->panel_power_on_supported) ? true : false;
2235 else if (attr == &dev_attr_usb_three.attr)
2236 exists = (drv->usb_three_supported) ? true : false;
2237
2238 return exists ? attr->mode : 0;
2239 }
2240
2241 static struct attribute_group toshiba_attr_group = {
2242 .is_visible = toshiba_sysfs_is_visible,
2243 .attrs = toshiba_attributes,
2244 };
2245
2246 /*
2247 * Misc device
2248 */
2249 static int toshiba_acpi_smm_bridge(SMMRegisters *regs)
2250 {
2251 u32 in[TCI_WORDS] = { regs->eax, regs->ebx, regs->ecx,
2252 regs->edx, regs->esi, regs->edi };
2253 u32 out[TCI_WORDS];
2254 acpi_status status;
2255
2256 status = tci_raw(toshiba_acpi, in, out);
2257 if (ACPI_FAILURE(status)) {
2258 pr_err("ACPI call to query SMM registers failed\n");
2259 return -EIO;
2260 }
2261
2262 /* Fillout the SMM struct with the TCI call results */
2263 regs->eax = out[0];
2264 regs->ebx = out[1];
2265 regs->ecx = out[2];
2266 regs->edx = out[3];
2267 regs->esi = out[4];
2268 regs->edi = out[5];
2269
2270 return 0;
2271 }
2272
2273 static long toshiba_acpi_ioctl(struct file *fp, unsigned int cmd,
2274 unsigned long arg)
2275 {
2276 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
2277 SMMRegisters regs;
2278 int ret;
2279
2280 if (!argp)
2281 return -EINVAL;
2282
2283 switch (cmd) {
2284 case TOSH_SMM:
2285 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2286 return -EFAULT;
2287 ret = toshiba_acpi_smm_bridge(&regs);
2288 if (ret)
2289 return ret;
2290 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2291 return -EFAULT;
2292 break;
2293 case TOSHIBA_ACPI_SCI:
2294 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
2295 return -EFAULT;
2296 /* Ensure we are being called with a SCI_{GET, SET} register */
2297 if (regs.eax != SCI_GET && regs.eax != SCI_SET)
2298 return -EINVAL;
2299 if (!sci_open(toshiba_acpi))
2300 return -EIO;
2301 ret = toshiba_acpi_smm_bridge(&regs);
2302 sci_close(toshiba_acpi);
2303 if (ret)
2304 return ret;
2305 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
2306 return -EFAULT;
2307 break;
2308 default:
2309 return -EINVAL;
2310 }
2311
2312 return 0;
2313 }
2314
2315 static const struct file_operations toshiba_acpi_fops = {
2316 .owner = THIS_MODULE,
2317 .unlocked_ioctl = toshiba_acpi_ioctl,
2318 .llseek = noop_llseek,
2319 };
2320
2321 /*
2322 * Hotkeys
2323 */
2324 static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2325 {
2326 acpi_status status;
2327 u32 result;
2328
2329 status = acpi_evaluate_object(dev->acpi_dev->handle,
2330 "ENAB", NULL, NULL);
2331 if (ACPI_FAILURE(status))
2332 return -ENODEV;
2333
2334 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2335 if (result == TOS_FAILURE)
2336 return -EIO;
2337 else if (result == TOS_NOT_SUPPORTED)
2338 return -ENODEV;
2339
2340 return 0;
2341 }
2342
2343 static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2344 {
2345 u32 result;
2346
2347 /*
2348 * Re-activate the hotkeys, but this time, we are using the
2349 * "Special Functions" mode.
2350 */
2351 result = hci_write(dev, HCI_HOTKEY_EVENT,
2352 HCI_HOTKEY_SPECIAL_FUNCTIONS);
2353 if (result != TOS_SUCCESS)
2354 pr_err("Could not enable the Special Function mode\n");
2355 }
2356
2357 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2358 struct serio *port)
2359 {
2360 if (str & I8042_STR_AUXDATA)
2361 return false;
2362
2363 if (unlikely(data == 0xe0))
2364 return false;
2365
2366 if ((data & 0x7f) == TOS1900_FN_SCAN) {
2367 schedule_work(&toshiba_acpi->hotkey_work);
2368 return true;
2369 }
2370
2371 return false;
2372 }
2373
2374 static void toshiba_acpi_hotkey_work(struct work_struct *work)
2375 {
2376 acpi_handle ec_handle = ec_get_handle();
2377 acpi_status status;
2378
2379 if (!ec_handle)
2380 return;
2381
2382 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2383 if (ACPI_FAILURE(status))
2384 pr_err("ACPI NTFY method execution failed\n");
2385 }
2386
2387 /*
2388 * Returns hotkey scancode, or < 0 on failure.
2389 */
2390 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2391 {
2392 unsigned long long value;
2393 acpi_status status;
2394
2395 status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2396 NULL, &value);
2397 if (ACPI_FAILURE(status)) {
2398 pr_err("ACPI INFO method execution failed\n");
2399 return -EIO;
2400 }
2401
2402 return value;
2403 }
2404
2405 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2406 int scancode)
2407 {
2408 if (scancode == 0x100)
2409 return;
2410
2411 /* Act on key press; ignore key release */
2412 if (scancode & 0x80)
2413 return;
2414
2415 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2416 pr_info("Unknown key %x\n", scancode);
2417 }
2418
2419 static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2420 {
2421 if (dev->info_supported) {
2422 int scancode = toshiba_acpi_query_hotkey(dev);
2423
2424 if (scancode < 0) {
2425 pr_err("Failed to query hotkey event\n");
2426 } else if (scancode != 0) {
2427 toshiba_acpi_report_hotkey(dev, scancode);
2428 dev->key_event_valid = 1;
2429 dev->last_key_event = scancode;
2430 }
2431 } else if (dev->system_event_supported) {
2432 u32 result;
2433 u32 value;
2434 int retries = 3;
2435
2436 do {
2437 result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
2438 switch (result) {
2439 case TOS_SUCCESS:
2440 toshiba_acpi_report_hotkey(dev, (int)value);
2441 dev->key_event_valid = 1;
2442 dev->last_key_event = value;
2443 break;
2444 case TOS_NOT_SUPPORTED:
2445 /*
2446 * This is a workaround for an unresolved
2447 * issue on some machines where system events
2448 * sporadically become disabled.
2449 */
2450 result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
2451 if (result == TOS_SUCCESS)
2452 pr_notice("Re-enabled hotkeys\n");
2453 /* Fall through */
2454 default:
2455 retries--;
2456 break;
2457 }
2458 } while (retries && result != TOS_FIFO_EMPTY);
2459 }
2460 }
2461
2462 static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
2463 {
2464 const struct key_entry *keymap = toshiba_acpi_keymap;
2465 acpi_handle ec_handle;
2466 u32 events_type;
2467 u32 hci_result;
2468 int error;
2469
2470 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID)) {
2471 pr_info("WMI event detected, hotkeys will not be monitored\n");
2472 return 0;
2473 }
2474
2475 error = toshiba_acpi_enable_hotkeys(dev);
2476 if (error)
2477 return error;
2478
2479 error = toshiba_hotkey_event_type_get(dev, &events_type);
2480 if (error) {
2481 pr_err("Unable to query Hotkey Event Type\n");
2482 return error;
2483 }
2484 dev->hotkey_event_type = events_type;
2485
2486 dev->hotkey_dev = input_allocate_device();
2487 if (!dev->hotkey_dev)
2488 return -ENOMEM;
2489
2490 dev->hotkey_dev->name = "Toshiba input device";
2491 dev->hotkey_dev->phys = "toshiba_acpi/input0";
2492 dev->hotkey_dev->id.bustype = BUS_HOST;
2493
2494 if (events_type == HCI_SYSTEM_TYPE1 ||
2495 !dev->kbd_function_keys_supported)
2496 keymap = toshiba_acpi_keymap;
2497 else if (events_type == HCI_SYSTEM_TYPE2 ||
2498 dev->kbd_function_keys_supported)
2499 keymap = toshiba_acpi_alt_keymap;
2500 else
2501 pr_info("Unknown event type received %x\n", events_type);
2502 error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
2503 if (error)
2504 goto err_free_dev;
2505
2506 /*
2507 * For some machines the SCI responsible for providing hotkey
2508 * notification doesn't fire. We can trigger the notification
2509 * whenever the Fn key is pressed using the NTFY method, if
2510 * supported, so if it's present set up an i8042 key filter
2511 * for this purpose.
2512 */
2513 ec_handle = ec_get_handle();
2514 if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
2515 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2516
2517 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2518 if (error) {
2519 pr_err("Error installing key filter\n");
2520 goto err_free_keymap;
2521 }
2522
2523 dev->ntfy_supported = 1;
2524 }
2525
2526 /*
2527 * Determine hotkey query interface. Prefer using the INFO
2528 * method when it is available.
2529 */
2530 if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
2531 dev->info_supported = 1;
2532 else {
2533 hci_result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
2534 if (hci_result == TOS_SUCCESS)
2535 dev->system_event_supported = 1;
2536 }
2537
2538 if (!dev->info_supported && !dev->system_event_supported) {
2539 pr_warn("No hotkey query interface found\n");
2540 goto err_remove_filter;
2541 }
2542
2543 error = input_register_device(dev->hotkey_dev);
2544 if (error) {
2545 pr_info("Unable to register input device\n");
2546 goto err_remove_filter;
2547 }
2548
2549 return 0;
2550
2551 err_remove_filter:
2552 if (dev->ntfy_supported)
2553 i8042_remove_filter(toshiba_acpi_i8042_filter);
2554 err_free_keymap:
2555 sparse_keymap_free(dev->hotkey_dev);
2556 err_free_dev:
2557 input_free_device(dev->hotkey_dev);
2558 dev->hotkey_dev = NULL;
2559 return error;
2560 }
2561
2562 static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
2563 {
2564 struct backlight_properties props;
2565 int brightness;
2566 int ret;
2567
2568 /*
2569 * Some machines don't support the backlight methods at all, and
2570 * others support it read-only. Either of these is pretty useless,
2571 * so only register the backlight device if the backlight method
2572 * supports both reads and writes.
2573 */
2574 brightness = __get_lcd_brightness(dev);
2575 if (brightness < 0)
2576 return 0;
2577 ret = set_lcd_brightness(dev, brightness);
2578 if (ret) {
2579 pr_debug("Backlight method is read-only, disabling backlight support\n");
2580 return 0;
2581 }
2582
2583 /*
2584 * Tell acpi-video-detect code to prefer vendor backlight on all
2585 * systems with transflective backlight and on dmi matched systems.
2586 */
2587 if (dev->tr_backlight_supported ||
2588 dmi_check_system(toshiba_vendor_backlight_dmi))
2589 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2590
2591 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2592 return 0;
2593
2594 memset(&props, 0, sizeof(props));
2595 props.type = BACKLIGHT_PLATFORM;
2596 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
2597
2598 /* Adding an extra level and having 0 change to transflective mode */
2599 if (dev->tr_backlight_supported)
2600 props.max_brightness++;
2601
2602 dev->backlight_dev = backlight_device_register("toshiba",
2603 &dev->acpi_dev->dev,
2604 dev,
2605 &toshiba_backlight_data,
2606 &props);
2607 if (IS_ERR(dev->backlight_dev)) {
2608 ret = PTR_ERR(dev->backlight_dev);
2609 pr_err("Could not register toshiba backlight device\n");
2610 dev->backlight_dev = NULL;
2611 return ret;
2612 }
2613
2614 dev->backlight_dev->props.brightness = brightness;
2615 return 0;
2616 }
2617
2618 static int toshiba_acpi_remove(struct acpi_device *acpi_dev)
2619 {
2620 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2621
2622 misc_deregister(&dev->miscdev);
2623
2624 remove_toshiba_proc_entries(dev);
2625
2626 if (dev->sysfs_created)
2627 sysfs_remove_group(&dev->acpi_dev->dev.kobj,
2628 &toshiba_attr_group);
2629
2630 if (dev->ntfy_supported) {
2631 i8042_remove_filter(toshiba_acpi_i8042_filter);
2632 cancel_work_sync(&dev->hotkey_work);
2633 }
2634
2635 if (dev->hotkey_dev) {
2636 input_unregister_device(dev->hotkey_dev);
2637 sparse_keymap_free(dev->hotkey_dev);
2638 }
2639
2640 backlight_device_unregister(dev->backlight_dev);
2641
2642 if (dev->illumination_supported)
2643 led_classdev_unregister(&dev->led_dev);
2644
2645 if (dev->kbd_led_registered)
2646 led_classdev_unregister(&dev->kbd_led);
2647
2648 if (dev->eco_supported)
2649 led_classdev_unregister(&dev->eco_led);
2650
2651 if (toshiba_acpi)
2652 toshiba_acpi = NULL;
2653
2654 kfree(dev);
2655
2656 return 0;
2657 }
2658
2659 static const char *find_hci_method(acpi_handle handle)
2660 {
2661 if (acpi_has_method(handle, "GHCI"))
2662 return "GHCI";
2663
2664 if (acpi_has_method(handle, "SPFC"))
2665 return "SPFC";
2666
2667 return NULL;
2668 }
2669
2670 static int toshiba_acpi_add(struct acpi_device *acpi_dev)
2671 {
2672 struct toshiba_acpi_dev *dev;
2673 const char *hci_method;
2674 u32 special_functions;
2675 u32 dummy;
2676 int ret = 0;
2677
2678 if (toshiba_acpi)
2679 return -EBUSY;
2680
2681 pr_info("Toshiba Laptop ACPI Extras version %s\n",
2682 TOSHIBA_ACPI_VERSION);
2683
2684 hci_method = find_hci_method(acpi_dev->handle);
2685 if (!hci_method) {
2686 pr_err("HCI interface not found\n");
2687 return -ENODEV;
2688 }
2689
2690 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2691 if (!dev)
2692 return -ENOMEM;
2693 dev->acpi_dev = acpi_dev;
2694 dev->method_hci = hci_method;
2695 dev->miscdev.minor = MISC_DYNAMIC_MINOR;
2696 dev->miscdev.name = "toshiba_acpi";
2697 dev->miscdev.fops = &toshiba_acpi_fops;
2698
2699 ret = misc_register(&dev->miscdev);
2700 if (ret) {
2701 pr_err("Failed to register miscdevice\n");
2702 kfree(dev);
2703 return ret;
2704 }
2705
2706 acpi_dev->driver_data = dev;
2707 dev_set_drvdata(&acpi_dev->dev, dev);
2708
2709 /* Query the BIOS for supported features */
2710
2711 /*
2712 * The "Special Functions" are always supported by the laptops
2713 * with the new keyboard layout, query for its presence to help
2714 * determine the keymap layout to use.
2715 */
2716 ret = toshiba_function_keys_get(dev, &special_functions);
2717 dev->kbd_function_keys_supported = !ret;
2718
2719 if (toshiba_acpi_setup_keyboard(dev))
2720 pr_info("Unable to activate hotkeys\n");
2721
2722 /* Determine whether or not BIOS supports transflective backlight */
2723 ret = get_tr_backlight_status(dev, &dummy);
2724 dev->tr_backlight_supported = !ret;
2725
2726 ret = toshiba_acpi_setup_backlight(dev);
2727 if (ret)
2728 goto error;
2729
2730 if (toshiba_illumination_available(dev)) {
2731 dev->led_dev.name = "toshiba::illumination";
2732 dev->led_dev.max_brightness = 1;
2733 dev->led_dev.brightness_set = toshiba_illumination_set;
2734 dev->led_dev.brightness_get = toshiba_illumination_get;
2735 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev))
2736 dev->illumination_supported = 1;
2737 }
2738
2739 if (toshiba_eco_mode_available(dev)) {
2740 dev->eco_led.name = "toshiba::eco_mode";
2741 dev->eco_led.max_brightness = 1;
2742 dev->eco_led.brightness_set = toshiba_eco_mode_set_status;
2743 dev->eco_led.brightness_get = toshiba_eco_mode_get_status;
2744 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->eco_led))
2745 dev->eco_supported = 1;
2746 }
2747
2748 dev->kbd_illum_supported = toshiba_kbd_illum_available(dev);
2749 /*
2750 * Only register the LED if KBD illumination is supported
2751 * and the keyboard backlight operation mode is set to FN-Z
2752 */
2753 if (dev->kbd_illum_supported && dev->kbd_mode == SCI_KBD_MODE_FNZ) {
2754 dev->kbd_led.name = "toshiba::kbd_backlight";
2755 dev->kbd_led.max_brightness = 1;
2756 dev->kbd_led.brightness_set = toshiba_kbd_backlight_set;
2757 dev->kbd_led.brightness_get = toshiba_kbd_backlight_get;
2758 if (!led_classdev_register(&dev->acpi_dev->dev, &dev->kbd_led))
2759 dev->kbd_led_registered = 1;
2760 }
2761
2762 ret = toshiba_touchpad_get(dev, &dummy);
2763 dev->touchpad_supported = !ret;
2764
2765 ret = toshiba_accelerometer_supported(dev);
2766 dev->accelerometer_supported = !ret;
2767
2768 toshiba_usb_sleep_charge_available(dev);
2769
2770 ret = toshiba_usb_rapid_charge_get(dev, &dummy);
2771 dev->usb_rapid_charge_supported = !ret;
2772
2773 ret = toshiba_usb_sleep_music_get(dev, &dummy);
2774 dev->usb_sleep_music_supported = !ret;
2775
2776 ret = toshiba_panel_power_on_get(dev, &dummy);
2777 dev->panel_power_on_supported = !ret;
2778
2779 ret = toshiba_usb_three_get(dev, &dummy);
2780 dev->usb_three_supported = !ret;
2781
2782 ret = get_video_status(dev, &dummy);
2783 dev->video_supported = !ret;
2784
2785 ret = get_fan_status(dev, &dummy);
2786 dev->fan_supported = !ret;
2787
2788 /*
2789 * Enable the "Special Functions" mode only if they are
2790 * supported and if they are activated.
2791 */
2792 if (dev->kbd_function_keys_supported && special_functions)
2793 toshiba_acpi_enable_special_functions(dev);
2794
2795 ret = sysfs_create_group(&dev->acpi_dev->dev.kobj,
2796 &toshiba_attr_group);
2797 if (ret) {
2798 dev->sysfs_created = 0;
2799 goto error;
2800 }
2801 dev->sysfs_created = !ret;
2802
2803 create_toshiba_proc_entries(dev);
2804
2805 toshiba_acpi = dev;
2806
2807 return 0;
2808
2809 error:
2810 toshiba_acpi_remove(acpi_dev);
2811 return ret;
2812 }
2813
2814 static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event)
2815 {
2816 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev);
2817 int ret;
2818
2819 switch (event) {
2820 case 0x80: /* Hotkeys and some system events */
2821 /*
2822 * Machines with this WMI GUID aren't supported due to bugs in
2823 * their AML.
2824 *
2825 * Return silently to avoid triggering a netlink event.
2826 */
2827 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID))
2828 return;
2829 toshiba_acpi_process_hotkeys(dev);
2830 break;
2831 case 0x81: /* Dock events */
2832 case 0x82:
2833 case 0x83:
2834 pr_info("Dock event received %x\n", event);
2835 break;
2836 case 0x88: /* Thermal events */
2837 pr_info("Thermal event received\n");
2838 break;
2839 case 0x8f: /* LID closed */
2840 case 0x90: /* LID is closed and Dock has been ejected */
2841 break;
2842 case 0x8c: /* SATA power events */
2843 case 0x8b:
2844 pr_info("SATA power event received %x\n", event);
2845 break;
2846 case 0x92: /* Keyboard backlight mode changed */
2847 /* Update sysfs entries */
2848 ret = sysfs_update_group(&acpi_dev->dev.kobj,
2849 &toshiba_attr_group);
2850 if (ret)
2851 pr_err("Unable to update sysfs entries\n");
2852 break;
2853 case 0x85: /* Unknown */
2854 case 0x8d: /* Unknown */
2855 case 0x8e: /* Unknown */
2856 case 0x94: /* Unknown */
2857 case 0x95: /* Unknown */
2858 default:
2859 pr_info("Unknown event received %x\n", event);
2860 break;
2861 }
2862
2863 acpi_bus_generate_netlink_event(acpi_dev->pnp.device_class,
2864 dev_name(&acpi_dev->dev),
2865 event, 0);
2866 }
2867
2868 #ifdef CONFIG_PM_SLEEP
2869 static int toshiba_acpi_suspend(struct device *device)
2870 {
2871 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
2872
2873 if (dev->hotkey_dev) {
2874 u32 result;
2875
2876 result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE);
2877 if (result != TOS_SUCCESS)
2878 pr_info("Unable to disable hotkeys\n");
2879 }
2880
2881 return 0;
2882 }
2883
2884 static int toshiba_acpi_resume(struct device *device)
2885 {
2886 struct toshiba_acpi_dev *dev = acpi_driver_data(to_acpi_device(device));
2887
2888 if (dev->hotkey_dev) {
2889 int error = toshiba_acpi_enable_hotkeys(dev);
2890
2891 if (error)
2892 pr_info("Unable to re-enable hotkeys\n");
2893 }
2894
2895 return 0;
2896 }
2897 #endif
2898
2899 static SIMPLE_DEV_PM_OPS(toshiba_acpi_pm,
2900 toshiba_acpi_suspend, toshiba_acpi_resume);
2901
2902 static struct acpi_driver toshiba_acpi_driver = {
2903 .name = "Toshiba ACPI driver",
2904 .owner = THIS_MODULE,
2905 .ids = toshiba_device_ids,
2906 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
2907 .ops = {
2908 .add = toshiba_acpi_add,
2909 .remove = toshiba_acpi_remove,
2910 .notify = toshiba_acpi_notify,
2911 },
2912 .drv.pm = &toshiba_acpi_pm,
2913 };
2914
2915 static int __init toshiba_acpi_init(void)
2916 {
2917 int ret;
2918
2919 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
2920 if (!toshiba_proc_dir) {
2921 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n");
2922 return -ENODEV;
2923 }
2924
2925 ret = acpi_bus_register_driver(&toshiba_acpi_driver);
2926 if (ret) {
2927 pr_err("Failed to register ACPI driver: %d\n", ret);
2928 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
2929 }
2930
2931 return ret;
2932 }
2933
2934 static void __exit toshiba_acpi_exit(void)
2935 {
2936 acpi_bus_unregister_driver(&toshiba_acpi_driver);
2937 if (toshiba_proc_dir)
2938 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
2939 }
2940
2941 module_init(toshiba_acpi_init);
2942 module_exit(toshiba_acpi_exit);
This page took 0.152106 seconds and 5 git commands to generate.