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