Merge branch 'for-linus' of git://ftp.arm.linux.org.uk/~rmk/linux-arm
[sfrench/cifs-2.6.git] / drivers / platform / x86 / toshiba_acpi.c
1 /*
2  *  toshiba_acpi.c - Toshiba Laptop ACPI Extras
3  *
4  *  Copyright (C) 2002-2004 John Belmonte
5  *  Copyright (C) 2008 Philip Langdale
6  *  Copyright (C) 2010 Pierre Ducroquet
7  *  Copyright (C) 2014-2015 Azael Avalos
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  The full GNU General Public License is included in this distribution in
20  *  the file called "COPYING".
21  *
22  *  The devolpment page for this driver is located at
23  *  http://memebeam.org/toys/ToshibaAcpiDriver.
24  *
25  *  Credits:
26  *      Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27  *              engineering the Windows drivers
28  *      Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29  *      Rob Miller - TV out and hotkeys help
30  */
31
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
34 #define TOSHIBA_ACPI_VERSION    "0.22"
35 #define PROC_INTERFACE_VERSION  1
36
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/backlight.h>
44 #include <linux/input.h>
45 #include <linux/input/sparse-keymap.h>
46 #include <linux/leds.h>
47 #include <linux/slab.h>
48 #include <linux/workqueue.h>
49 #include <linux/i8042.h>
50 #include <linux/acpi.h>
51 #include <linux/dmi.h>
52 #include <linux/uaccess.h>
53 #include <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)) {
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)) {
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)) {
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)) {
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)) {
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)) {
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)) {
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)) {
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)) {
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)) {
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 /* Transflective Backlight */
1187 static int get_tr_backlight_status(struct toshiba_acpi_dev *dev, bool *enabled)
1188 {
1189         u32 hci_result;
1190         u32 status;
1191
1192         hci_result = hci_read(dev, HCI_TR_BACKLIGHT, &status);
1193         *enabled = !status;
1194         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1195 }
1196
1197 static int set_tr_backlight_status(struct toshiba_acpi_dev *dev, bool enable)
1198 {
1199         u32 hci_result;
1200         u32 value = !enable;
1201
1202         hci_result = hci_write(dev, HCI_TR_BACKLIGHT, value);
1203         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1204 }
1205
1206 static struct proc_dir_entry *toshiba_proc_dir;
1207
1208 /* LCD Brightness */
1209 static int __get_lcd_brightness(struct toshiba_acpi_dev *dev)
1210 {
1211         u32 hci_result;
1212         u32 value;
1213         int brightness = 0;
1214
1215         if (dev->tr_backlight_supported) {
1216                 bool enabled;
1217                 int ret = get_tr_backlight_status(dev, &enabled);
1218
1219                 if (ret)
1220                         return ret;
1221                 if (enabled)
1222                         return 0;
1223                 brightness++;
1224         }
1225
1226         hci_result = hci_read(dev, HCI_LCD_BRIGHTNESS, &value);
1227         if (hci_result == TOS_SUCCESS)
1228                 return brightness + (value >> HCI_LCD_BRIGHTNESS_SHIFT);
1229
1230         return -EIO;
1231 }
1232
1233 static int get_lcd_brightness(struct backlight_device *bd)
1234 {
1235         struct toshiba_acpi_dev *dev = bl_get_data(bd);
1236
1237         return __get_lcd_brightness(dev);
1238 }
1239
1240 static int lcd_proc_show(struct seq_file *m, void *v)
1241 {
1242         struct toshiba_acpi_dev *dev = m->private;
1243         int value;
1244         int levels;
1245
1246         if (!dev->backlight_dev)
1247                 return -ENODEV;
1248
1249         levels = dev->backlight_dev->props.max_brightness + 1;
1250         value = get_lcd_brightness(dev->backlight_dev);
1251         if (value >= 0) {
1252                 seq_printf(m, "brightness:              %d\n", value);
1253                 seq_printf(m, "brightness_levels:       %d\n", levels);
1254                 return 0;
1255         }
1256
1257         pr_err("Error reading LCD brightness\n");
1258         return -EIO;
1259 }
1260
1261 static int lcd_proc_open(struct inode *inode, struct file *file)
1262 {
1263         return single_open(file, lcd_proc_show, PDE_DATA(inode));
1264 }
1265
1266 static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
1267 {
1268         u32 hci_result;
1269
1270         if (dev->tr_backlight_supported) {
1271                 bool enable = !value;
1272                 int ret = set_tr_backlight_status(dev, enable);
1273
1274                 if (ret)
1275                         return ret;
1276                 if (value)
1277                         value--;
1278         }
1279
1280         value = value << HCI_LCD_BRIGHTNESS_SHIFT;
1281         hci_result = hci_write(dev, HCI_LCD_BRIGHTNESS, value);
1282         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1283 }
1284
1285 static int set_lcd_status(struct backlight_device *bd)
1286 {
1287         struct toshiba_acpi_dev *dev = bl_get_data(bd);
1288
1289         return set_lcd_brightness(dev, bd->props.brightness);
1290 }
1291
1292 static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
1293                               size_t count, loff_t *pos)
1294 {
1295         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1296         char cmd[42];
1297         size_t len;
1298         int value;
1299         int ret;
1300         int levels = dev->backlight_dev->props.max_brightness + 1;
1301
1302         len = min(count, sizeof(cmd) - 1);
1303         if (copy_from_user(cmd, buf, len))
1304                 return -EFAULT;
1305         cmd[len] = '\0';
1306
1307         if (sscanf(cmd, " brightness : %i", &value) == 1 &&
1308             value >= 0 && value < levels) {
1309                 ret = set_lcd_brightness(dev, value);
1310                 if (ret == 0)
1311                         ret = count;
1312         } else {
1313                 ret = -EINVAL;
1314         }
1315         return ret;
1316 }
1317
1318 static const struct file_operations lcd_proc_fops = {
1319         .owner          = THIS_MODULE,
1320         .open           = lcd_proc_open,
1321         .read           = seq_read,
1322         .llseek         = seq_lseek,
1323         .release        = single_release,
1324         .write          = lcd_proc_write,
1325 };
1326
1327 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status)
1328 {
1329         u32 hci_result;
1330
1331         hci_result = hci_read(dev, HCI_VIDEO_OUT, status);
1332         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1333 }
1334
1335 static int video_proc_show(struct seq_file *m, void *v)
1336 {
1337         struct toshiba_acpi_dev *dev = m->private;
1338         u32 value;
1339         int ret;
1340
1341         ret = get_video_status(dev, &value);
1342         if (!ret) {
1343                 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
1344                 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
1345                 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
1346
1347                 seq_printf(m, "lcd_out:                 %d\n", is_lcd);
1348                 seq_printf(m, "crt_out:                 %d\n", is_crt);
1349                 seq_printf(m, "tv_out:                  %d\n", is_tv);
1350         }
1351
1352         return ret;
1353 }
1354
1355 static int video_proc_open(struct inode *inode, struct file *file)
1356 {
1357         return single_open(file, video_proc_show, PDE_DATA(inode));
1358 }
1359
1360 static ssize_t video_proc_write(struct file *file, const char __user *buf,
1361                                 size_t count, loff_t *pos)
1362 {
1363         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1364         char *cmd, *buffer;
1365         int ret;
1366         int value;
1367         int remain = count;
1368         int lcd_out = -1;
1369         int crt_out = -1;
1370         int tv_out = -1;
1371         u32 video_out;
1372
1373         cmd = kmalloc(count + 1, GFP_KERNEL);
1374         if (!cmd)
1375                 return -ENOMEM;
1376         if (copy_from_user(cmd, buf, count)) {
1377                 kfree(cmd);
1378                 return -EFAULT;
1379         }
1380         cmd[count] = '\0';
1381
1382         buffer = cmd;
1383
1384         /*
1385          * Scan expression.  Multiple expressions may be delimited with ;
1386          * NOTE: To keep scanning simple, invalid fields are ignored.
1387          */
1388         while (remain) {
1389                 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
1390                         lcd_out = value & 1;
1391                 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
1392                         crt_out = value & 1;
1393                 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
1394                         tv_out = value & 1;
1395                 /* Advance to one character past the next ; */
1396                 do {
1397                         ++buffer;
1398                         --remain;
1399                 } while (remain && *(buffer - 1) != ';');
1400         }
1401
1402         kfree(cmd);
1403
1404         ret = get_video_status(dev, &video_out);
1405         if (!ret) {
1406                 unsigned int new_video_out = video_out;
1407
1408                 if (lcd_out != -1)
1409                         _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
1410                 if (crt_out != -1)
1411                         _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
1412                 if (tv_out != -1)
1413                         _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
1414                 /*
1415                  * To avoid unnecessary video disruption, only write the new
1416                  * video setting if something changed.
1417                  */
1418                 if (new_video_out != video_out)
1419                         ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
1420         }
1421
1422         return ret ? ret : count;
1423 }
1424
1425 static const struct file_operations video_proc_fops = {
1426         .owner          = THIS_MODULE,
1427         .open           = video_proc_open,
1428         .read           = seq_read,
1429         .llseek         = seq_lseek,
1430         .release        = single_release,
1431         .write          = video_proc_write,
1432 };
1433
1434 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status)
1435 {
1436         u32 hci_result;
1437
1438         hci_result = hci_read(dev, HCI_FAN, status);
1439         return hci_result == TOS_SUCCESS ? 0 : -EIO;
1440 }
1441
1442 static int fan_proc_show(struct seq_file *m, void *v)
1443 {
1444         struct toshiba_acpi_dev *dev = m->private;
1445         int ret;
1446         u32 value;
1447
1448         ret = get_fan_status(dev, &value);
1449         if (!ret) {
1450                 seq_printf(m, "running:                 %d\n", (value > 0));
1451                 seq_printf(m, "force_on:                %d\n", dev->force_fan);
1452         }
1453
1454         return ret;
1455 }
1456
1457 static int fan_proc_open(struct inode *inode, struct file *file)
1458 {
1459         return single_open(file, fan_proc_show, PDE_DATA(inode));
1460 }
1461
1462 static ssize_t fan_proc_write(struct file *file, const char __user *buf,
1463                               size_t count, loff_t *pos)
1464 {
1465         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1466         char cmd[42];
1467         size_t len;
1468         int value;
1469         u32 hci_result;
1470
1471         len = min(count, sizeof(cmd) - 1);
1472         if (copy_from_user(cmd, buf, len))
1473                 return -EFAULT;
1474         cmd[len] = '\0';
1475
1476         if (sscanf(cmd, " force_on : %i", &value) == 1 &&
1477             value >= 0 && value <= 1) {
1478                 hci_result = hci_write(dev, HCI_FAN, value);
1479                 if (hci_result == TOS_SUCCESS)
1480                         dev->force_fan = value;
1481                 else
1482                         return -EIO;
1483         } else {
1484                 return -EINVAL;
1485         }
1486
1487         return count;
1488 }
1489
1490 static const struct file_operations fan_proc_fops = {
1491         .owner          = THIS_MODULE,
1492         .open           = fan_proc_open,
1493         .read           = seq_read,
1494         .llseek         = seq_lseek,
1495         .release        = single_release,
1496         .write          = fan_proc_write,
1497 };
1498
1499 static int keys_proc_show(struct seq_file *m, void *v)
1500 {
1501         struct toshiba_acpi_dev *dev = m->private;
1502         u32 hci_result;
1503         u32 value;
1504
1505         if (!dev->key_event_valid && dev->system_event_supported) {
1506                 hci_result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
1507                 if (hci_result == TOS_SUCCESS) {
1508                         dev->key_event_valid = 1;
1509                         dev->last_key_event = value;
1510                 } else if (hci_result == TOS_FIFO_EMPTY) {
1511                         /* Better luck next time */
1512                 } else if (hci_result == TOS_NOT_SUPPORTED) {
1513                         /*
1514                          * This is a workaround for an unresolved issue on
1515                          * some machines where system events sporadically
1516                          * become disabled.
1517                          */
1518                         hci_result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
1519                         pr_notice("Re-enabled hotkeys\n");
1520                 } else {
1521                         pr_err("Error reading hotkey status\n");
1522                         return -EIO;
1523                 }
1524         }
1525
1526         seq_printf(m, "hotkey_ready:            %d\n", dev->key_event_valid);
1527         seq_printf(m, "hotkey:                  0x%04x\n", dev->last_key_event);
1528         return 0;
1529 }
1530
1531 static int keys_proc_open(struct inode *inode, struct file *file)
1532 {
1533         return single_open(file, keys_proc_show, PDE_DATA(inode));
1534 }
1535
1536 static ssize_t keys_proc_write(struct file *file, const char __user *buf,
1537                                size_t count, loff_t *pos)
1538 {
1539         struct toshiba_acpi_dev *dev = PDE_DATA(file_inode(file));
1540         char cmd[42];
1541         size_t len;
1542         int value;
1543
1544         len = min(count, sizeof(cmd) - 1);
1545         if (copy_from_user(cmd, buf, len))
1546                 return -EFAULT;
1547         cmd[len] = '\0';
1548
1549         if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0)
1550                 dev->key_event_valid = 0;
1551         else
1552                 return -EINVAL;
1553
1554         return count;
1555 }
1556
1557 static const struct file_operations keys_proc_fops = {
1558         .owner          = THIS_MODULE,
1559         .open           = keys_proc_open,
1560         .read           = seq_read,
1561         .llseek         = seq_lseek,
1562         .release        = single_release,
1563         .write          = keys_proc_write,
1564 };
1565
1566 static int version_proc_show(struct seq_file *m, void *v)
1567 {
1568         seq_printf(m, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
1569         seq_printf(m, "proc_interface:          %d\n", PROC_INTERFACE_VERSION);
1570         return 0;
1571 }
1572
1573 static int version_proc_open(struct inode *inode, struct file *file)
1574 {
1575         return single_open(file, version_proc_show, PDE_DATA(inode));
1576 }
1577
1578 static const struct file_operations version_proc_fops = {
1579         .owner          = THIS_MODULE,
1580         .open           = version_proc_open,
1581         .read           = seq_read,
1582         .llseek         = seq_lseek,
1583         .release        = single_release,
1584 };
1585
1586 /*
1587  * Proc and module init
1588  */
1589
1590 #define PROC_TOSHIBA            "toshiba"
1591
1592 static void create_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1593 {
1594         if (dev->backlight_dev)
1595                 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1596                                  &lcd_proc_fops, dev);
1597         if (dev->video_supported)
1598                 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1599                                  &video_proc_fops, dev);
1600         if (dev->fan_supported)
1601                 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1602                                  &fan_proc_fops, dev);
1603         if (dev->hotkey_dev)
1604                 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir,
1605                                  &keys_proc_fops, dev);
1606         proc_create_data("version", S_IRUGO, toshiba_proc_dir,
1607                          &version_proc_fops, dev);
1608 }
1609
1610 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev)
1611 {
1612         if (dev->backlight_dev)
1613                 remove_proc_entry("lcd", toshiba_proc_dir);
1614         if (dev->video_supported)
1615                 remove_proc_entry("video", toshiba_proc_dir);
1616         if (dev->fan_supported)
1617                 remove_proc_entry("fan", toshiba_proc_dir);
1618         if (dev->hotkey_dev)
1619                 remove_proc_entry("keys", toshiba_proc_dir);
1620         remove_proc_entry("version", toshiba_proc_dir);
1621 }
1622
1623 static const struct backlight_ops toshiba_backlight_data = {
1624         .options = BL_CORE_SUSPENDRESUME,
1625         .get_brightness = get_lcd_brightness,
1626         .update_status  = set_lcd_status,
1627 };
1628
1629 /*
1630  * Sysfs files
1631  */
1632 static ssize_t version_show(struct device *dev,
1633                             struct device_attribute *attr, char *buf)
1634 {
1635         return sprintf(buf, "%s\n", TOSHIBA_ACPI_VERSION);
1636 }
1637 static DEVICE_ATTR_RO(version);
1638
1639 static ssize_t fan_store(struct device *dev,
1640                          struct device_attribute *attr,
1641                          const char *buf, size_t count)
1642 {
1643         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1644         u32 result;
1645         int state;
1646         int ret;
1647
1648         ret = kstrtoint(buf, 0, &state);
1649         if (ret)
1650                 return ret;
1651
1652         if (state != 0 && state != 1)
1653                 return -EINVAL;
1654
1655         result = hci_write(toshiba, HCI_FAN, state);
1656         if (result == TOS_FAILURE)
1657                 return -EIO;
1658         else if (result == TOS_NOT_SUPPORTED)
1659                 return -ENODEV;
1660
1661         return count;
1662 }
1663
1664 static ssize_t fan_show(struct device *dev,
1665                         struct device_attribute *attr, char *buf)
1666 {
1667         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1668         u32 value;
1669         int ret;
1670
1671         ret = get_fan_status(toshiba, &value);
1672         if (ret)
1673                 return ret;
1674
1675         return sprintf(buf, "%d\n", value);
1676 }
1677 static DEVICE_ATTR_RW(fan);
1678
1679 static ssize_t kbd_backlight_mode_store(struct device *dev,
1680                                         struct device_attribute *attr,
1681                                         const char *buf, size_t count)
1682 {
1683         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1684         int mode;
1685         int time;
1686         int ret;
1687
1688
1689         ret = kstrtoint(buf, 0, &mode);
1690         if (ret)
1691                 return ret;
1692
1693         /* Check for supported modes depending on keyboard backlight type */
1694         if (toshiba->kbd_type == 1) {
1695                 /* Type 1 supports SCI_KBD_MODE_FNZ and SCI_KBD_MODE_AUTO */
1696                 if (mode != SCI_KBD_MODE_FNZ && mode != SCI_KBD_MODE_AUTO)
1697                         return -EINVAL;
1698         } else if (toshiba->kbd_type == 2) {
1699                 /* Type 2 doesn't support SCI_KBD_MODE_FNZ */
1700                 if (mode != SCI_KBD_MODE_AUTO && mode != SCI_KBD_MODE_ON &&
1701                     mode != SCI_KBD_MODE_OFF)
1702                         return -EINVAL;
1703         }
1704
1705         /*
1706          * Set the Keyboard Backlight Mode where:
1707          *      Auto - KBD backlight turns off automatically in given time
1708          *      FN-Z - KBD backlight "toggles" when hotkey pressed
1709          *      ON   - KBD backlight is always on
1710          *      OFF  - KBD backlight is always off
1711          */
1712
1713         /* Only make a change if the actual mode has changed */
1714         if (toshiba->kbd_mode != mode) {
1715                 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1716                 time = toshiba->kbd_time << HCI_MISC_SHIFT;
1717
1718                 /* OR the "base time" to the actual method format */
1719                 if (toshiba->kbd_type == 1) {
1720                         /* Type 1 requires the current mode */
1721                         time |= toshiba->kbd_mode;
1722                 } else if (toshiba->kbd_type == 2) {
1723                         /* Type 2 requires the desired mode */
1724                         time |= mode;
1725                 }
1726
1727                 ret = toshiba_kbd_illum_status_set(toshiba, time);
1728                 if (ret)
1729                         return ret;
1730
1731                 toshiba->kbd_mode = mode;
1732         }
1733
1734         return count;
1735 }
1736
1737 static ssize_t kbd_backlight_mode_show(struct device *dev,
1738                                        struct device_attribute *attr,
1739                                        char *buf)
1740 {
1741         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1742         u32 time;
1743
1744         if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1745                 return -EIO;
1746
1747         return sprintf(buf, "%i\n", time & SCI_KBD_MODE_MASK);
1748 }
1749 static DEVICE_ATTR_RW(kbd_backlight_mode);
1750
1751 static ssize_t kbd_type_show(struct device *dev,
1752                              struct device_attribute *attr, char *buf)
1753 {
1754         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1755
1756         return sprintf(buf, "%d\n", toshiba->kbd_type);
1757 }
1758 static DEVICE_ATTR_RO(kbd_type);
1759
1760 static ssize_t available_kbd_modes_show(struct device *dev,
1761                                         struct device_attribute *attr,
1762                                         char *buf)
1763 {
1764         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1765
1766         if (toshiba->kbd_type == 1)
1767                 return sprintf(buf, "%x %x\n",
1768                                SCI_KBD_MODE_FNZ, SCI_KBD_MODE_AUTO);
1769
1770         return sprintf(buf, "%x %x %x\n",
1771                        SCI_KBD_MODE_AUTO, SCI_KBD_MODE_ON, SCI_KBD_MODE_OFF);
1772 }
1773 static DEVICE_ATTR_RO(available_kbd_modes);
1774
1775 static ssize_t kbd_backlight_timeout_store(struct device *dev,
1776                                            struct device_attribute *attr,
1777                                            const char *buf, size_t count)
1778 {
1779         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1780         int time;
1781         int ret;
1782
1783         ret = kstrtoint(buf, 0, &time);
1784         if (ret)
1785                 return ret;
1786
1787         /* Check for supported values depending on kbd_type */
1788         if (toshiba->kbd_type == 1) {
1789                 if (time < 0 || time > 60)
1790                         return -EINVAL;
1791         } else if (toshiba->kbd_type == 2) {
1792                 if (time < 1 || time > 60)
1793                         return -EINVAL;
1794         }
1795
1796         /* Set the Keyboard Backlight Timeout */
1797
1798         /* Only make a change if the actual timeout has changed */
1799         if (toshiba->kbd_time != time) {
1800                 /* Shift the time to "base time" (0x3c0000 == 60 seconds) */
1801                 time = time << HCI_MISC_SHIFT;
1802                 /* OR the "base time" to the actual method format */
1803                 if (toshiba->kbd_type == 1)
1804                         time |= SCI_KBD_MODE_FNZ;
1805                 else if (toshiba->kbd_type == 2)
1806                         time |= SCI_KBD_MODE_AUTO;
1807
1808                 ret = toshiba_kbd_illum_status_set(toshiba, time);
1809                 if (ret)
1810                         return ret;
1811
1812                 toshiba->kbd_time = time >> HCI_MISC_SHIFT;
1813         }
1814
1815         return count;
1816 }
1817
1818 static ssize_t kbd_backlight_timeout_show(struct device *dev,
1819                                           struct device_attribute *attr,
1820                                           char *buf)
1821 {
1822         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1823         u32 time;
1824
1825         if (toshiba_kbd_illum_status_get(toshiba, &time) < 0)
1826                 return -EIO;
1827
1828         return sprintf(buf, "%i\n", time >> HCI_MISC_SHIFT);
1829 }
1830 static DEVICE_ATTR_RW(kbd_backlight_timeout);
1831
1832 static ssize_t touchpad_store(struct device *dev,
1833                               struct device_attribute *attr,
1834                               const char *buf, size_t count)
1835 {
1836         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1837         int state;
1838         int ret;
1839
1840         /* Set the TouchPad on/off, 0 - Disable | 1 - Enable */
1841         ret = kstrtoint(buf, 0, &state);
1842         if (ret)
1843                 return ret;
1844         if (state != 0 && state != 1)
1845                 return -EINVAL;
1846
1847         ret = toshiba_touchpad_set(toshiba, state);
1848         if (ret)
1849                 return ret;
1850
1851         return count;
1852 }
1853
1854 static ssize_t touchpad_show(struct device *dev,
1855                              struct device_attribute *attr, char *buf)
1856 {
1857         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1858         u32 state;
1859         int ret;
1860
1861         ret = toshiba_touchpad_get(toshiba, &state);
1862         if (ret < 0)
1863                 return ret;
1864
1865         return sprintf(buf, "%i\n", state);
1866 }
1867 static DEVICE_ATTR_RW(touchpad);
1868
1869 static ssize_t position_show(struct device *dev,
1870                              struct device_attribute *attr, char *buf)
1871 {
1872         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1873         u32 xyval, zval, tmp;
1874         u16 x, y, z;
1875         int ret;
1876
1877         xyval = zval = 0;
1878         ret = toshiba_accelerometer_get(toshiba, &xyval, &zval);
1879         if (ret < 0)
1880                 return ret;
1881
1882         x = xyval & HCI_ACCEL_MASK;
1883         tmp = xyval >> HCI_MISC_SHIFT;
1884         y = tmp & HCI_ACCEL_MASK;
1885         z = zval & HCI_ACCEL_MASK;
1886
1887         return sprintf(buf, "%d %d %d\n", x, y, z);
1888 }
1889 static DEVICE_ATTR_RO(position);
1890
1891 static ssize_t usb_sleep_charge_show(struct device *dev,
1892                                      struct device_attribute *attr, char *buf)
1893 {
1894         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1895         u32 mode;
1896         int ret;
1897
1898         ret = toshiba_usb_sleep_charge_get(toshiba, &mode);
1899         if (ret < 0)
1900                 return ret;
1901
1902         return sprintf(buf, "%x\n", mode & SCI_USB_CHARGE_MODE_MASK);
1903 }
1904
1905 static ssize_t usb_sleep_charge_store(struct device *dev,
1906                                       struct device_attribute *attr,
1907                                       const char *buf, size_t count)
1908 {
1909         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1910         u32 mode;
1911         int state;
1912         int ret;
1913
1914         ret = kstrtoint(buf, 0, &state);
1915         if (ret)
1916                 return ret;
1917         /*
1918          * Check for supported values, where:
1919          * 0 - Disabled
1920          * 1 - Alternate (Non USB conformant devices that require more power)
1921          * 2 - Auto (USB conformant devices)
1922          * 3 - Typical
1923          */
1924         if (state != 0 && state != 1 && state != 2 && state != 3)
1925                 return -EINVAL;
1926
1927         /* Set the USB charging mode to internal value */
1928         mode = toshiba->usbsc_mode_base;
1929         if (state == 0)
1930                 mode |= SCI_USB_CHARGE_DISABLED;
1931         else if (state == 1)
1932                 mode |= SCI_USB_CHARGE_ALTERNATE;
1933         else if (state == 2)
1934                 mode |= SCI_USB_CHARGE_AUTO;
1935         else if (state == 3)
1936                 mode |= SCI_USB_CHARGE_TYPICAL;
1937
1938         ret = toshiba_usb_sleep_charge_set(toshiba, mode);
1939         if (ret)
1940                 return ret;
1941
1942         return count;
1943 }
1944 static DEVICE_ATTR_RW(usb_sleep_charge);
1945
1946 static ssize_t sleep_functions_on_battery_show(struct device *dev,
1947                                                struct device_attribute *attr,
1948                                                char *buf)
1949 {
1950         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1951         u32 state;
1952         int bat_lvl;
1953         int status;
1954         int ret;
1955         int tmp;
1956
1957         ret = toshiba_sleep_functions_status_get(toshiba, &state);
1958         if (ret < 0)
1959                 return ret;
1960
1961         /* Determine the status: 0x4 - Enabled | 0x1 - Disabled */
1962         tmp = state & SCI_USB_CHARGE_BAT_MASK;
1963         status = (tmp == 0x4) ? 1 : 0;
1964         /* Determine the battery level set */
1965         bat_lvl = state >> HCI_MISC_SHIFT;
1966
1967         return sprintf(buf, "%d %d\n", status, bat_lvl);
1968 }
1969
1970 static ssize_t sleep_functions_on_battery_store(struct device *dev,
1971                                                 struct device_attribute *attr,
1972                                                 const char *buf, size_t count)
1973 {
1974         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
1975         u32 status;
1976         int value;
1977         int ret;
1978         int tmp;
1979
1980         ret = kstrtoint(buf, 0, &value);
1981         if (ret)
1982                 return ret;
1983
1984         /*
1985          * Set the status of the function:
1986          * 0 - Disabled
1987          * 1-100 - Enabled
1988          */
1989         if (value < 0 || value > 100)
1990                 return -EINVAL;
1991
1992         if (value == 0) {
1993                 tmp = toshiba->usbsc_bat_level << HCI_MISC_SHIFT;
1994                 status = tmp | SCI_USB_CHARGE_BAT_LVL_OFF;
1995         } else {
1996                 tmp = value << HCI_MISC_SHIFT;
1997                 status = tmp | SCI_USB_CHARGE_BAT_LVL_ON;
1998         }
1999         ret = toshiba_sleep_functions_status_set(toshiba, status);
2000         if (ret < 0)
2001                 return ret;
2002
2003         toshiba->usbsc_bat_level = status >> HCI_MISC_SHIFT;
2004
2005         return count;
2006 }
2007 static DEVICE_ATTR_RW(sleep_functions_on_battery);
2008
2009 static ssize_t usb_rapid_charge_show(struct device *dev,
2010                                      struct device_attribute *attr, char *buf)
2011 {
2012         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2013         u32 state;
2014         int ret;
2015
2016         ret = toshiba_usb_rapid_charge_get(toshiba, &state);
2017         if (ret < 0)
2018                 return ret;
2019
2020         return sprintf(buf, "%d\n", state);
2021 }
2022
2023 static ssize_t usb_rapid_charge_store(struct device *dev,
2024                                       struct device_attribute *attr,
2025                                       const char *buf, size_t count)
2026 {
2027         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2028         int state;
2029         int ret;
2030
2031         ret = kstrtoint(buf, 0, &state);
2032         if (ret)
2033                 return ret;
2034         if (state != 0 && state != 1)
2035                 return -EINVAL;
2036
2037         ret = toshiba_usb_rapid_charge_set(toshiba, state);
2038         if (ret)
2039                 return ret;
2040
2041         return count;
2042 }
2043 static DEVICE_ATTR_RW(usb_rapid_charge);
2044
2045 static ssize_t usb_sleep_music_show(struct device *dev,
2046                                     struct device_attribute *attr, char *buf)
2047 {
2048         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2049         u32 state;
2050         int ret;
2051
2052         ret = toshiba_usb_sleep_music_get(toshiba, &state);
2053         if (ret < 0)
2054                 return ret;
2055
2056         return sprintf(buf, "%d\n", state);
2057 }
2058
2059 static ssize_t usb_sleep_music_store(struct device *dev,
2060                                      struct device_attribute *attr,
2061                                      const char *buf, size_t count)
2062 {
2063         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2064         int state;
2065         int ret;
2066
2067         ret = kstrtoint(buf, 0, &state);
2068         if (ret)
2069                 return ret;
2070         if (state != 0 && state != 1)
2071                 return -EINVAL;
2072
2073         ret = toshiba_usb_sleep_music_set(toshiba, state);
2074         if (ret)
2075                 return ret;
2076
2077         return count;
2078 }
2079 static DEVICE_ATTR_RW(usb_sleep_music);
2080
2081 static ssize_t kbd_function_keys_show(struct device *dev,
2082                                       struct device_attribute *attr, char *buf)
2083 {
2084         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2085         int mode;
2086         int ret;
2087
2088         ret = toshiba_function_keys_get(toshiba, &mode);
2089         if (ret < 0)
2090                 return ret;
2091
2092         return sprintf(buf, "%d\n", mode);
2093 }
2094
2095 static ssize_t kbd_function_keys_store(struct device *dev,
2096                                        struct device_attribute *attr,
2097                                        const char *buf, size_t count)
2098 {
2099         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2100         int mode;
2101         int ret;
2102
2103         ret = kstrtoint(buf, 0, &mode);
2104         if (ret)
2105                 return ret;
2106         /*
2107          * Check for the function keys mode where:
2108          * 0 - Normal operation (F{1-12} as usual and hotkeys via FN-F{1-12})
2109          * 1 - Special functions (Opposite of the above setting)
2110          */
2111         if (mode != 0 && mode != 1)
2112                 return -EINVAL;
2113
2114         ret = toshiba_function_keys_set(toshiba, mode);
2115         if (ret)
2116                 return ret;
2117
2118         pr_info("Reboot for changes to KBD Function Keys to take effect");
2119
2120         return count;
2121 }
2122 static DEVICE_ATTR_RW(kbd_function_keys);
2123
2124 static ssize_t panel_power_on_show(struct device *dev,
2125                                    struct device_attribute *attr, char *buf)
2126 {
2127         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2128         u32 state;
2129         int ret;
2130
2131         ret = toshiba_panel_power_on_get(toshiba, &state);
2132         if (ret < 0)
2133                 return ret;
2134
2135         return sprintf(buf, "%d\n", state);
2136 }
2137
2138 static ssize_t panel_power_on_store(struct device *dev,
2139                                     struct device_attribute *attr,
2140                                     const char *buf, size_t count)
2141 {
2142         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2143         int state;
2144         int ret;
2145
2146         ret = kstrtoint(buf, 0, &state);
2147         if (ret)
2148                 return ret;
2149         if (state != 0 && state != 1)
2150                 return -EINVAL;
2151
2152         ret = toshiba_panel_power_on_set(toshiba, state);
2153         if (ret)
2154                 return ret;
2155
2156         pr_info("Reboot for changes to Panel Power ON to take effect");
2157
2158         return count;
2159 }
2160 static DEVICE_ATTR_RW(panel_power_on);
2161
2162 static ssize_t usb_three_show(struct device *dev,
2163                               struct device_attribute *attr, char *buf)
2164 {
2165         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2166         u32 state;
2167         int ret;
2168
2169         ret = toshiba_usb_three_get(toshiba, &state);
2170         if (ret < 0)
2171                 return ret;
2172
2173         return sprintf(buf, "%d\n", state);
2174 }
2175
2176 static ssize_t usb_three_store(struct device *dev,
2177                                struct device_attribute *attr,
2178                                const char *buf, size_t count)
2179 {
2180         struct toshiba_acpi_dev *toshiba = dev_get_drvdata(dev);
2181         int state;
2182         int ret;
2183
2184         ret = kstrtoint(buf, 0, &state);
2185         if (ret)
2186                 return ret;
2187         /*
2188          * Check for USB 3 mode where:
2189          * 0 - Disabled (Acts like a USB 2 port, saving power)
2190          * 1 - Enabled
2191          */
2192         if (state != 0 && state != 1)
2193                 return -EINVAL;
2194
2195         ret = toshiba_usb_three_set(toshiba, state);
2196         if (ret)
2197                 return ret;
2198
2199         pr_info("Reboot for changes to USB 3 to take effect");
2200
2201         return count;
2202 }
2203 static DEVICE_ATTR_RW(usb_three);
2204
2205 static struct attribute *toshiba_attributes[] = {
2206         &dev_attr_version.attr,
2207         &dev_attr_fan.attr,
2208         &dev_attr_kbd_backlight_mode.attr,
2209         &dev_attr_kbd_type.attr,
2210         &dev_attr_available_kbd_modes.attr,
2211         &dev_attr_kbd_backlight_timeout.attr,
2212         &dev_attr_touchpad.attr,
2213         &dev_attr_position.attr,
2214         &dev_attr_usb_sleep_charge.attr,
2215         &dev_attr_sleep_functions_on_battery.attr,
2216         &dev_attr_usb_rapid_charge.attr,
2217         &dev_attr_usb_sleep_music.attr,
2218         &dev_attr_kbd_function_keys.attr,
2219         &dev_attr_panel_power_on.attr,
2220         &dev_attr_usb_three.attr,
2221         NULL,
2222 };
2223
2224 static umode_t toshiba_sysfs_is_visible(struct kobject *kobj,
2225                                         struct attribute *attr, int idx)
2226 {
2227         struct device *dev = container_of(kobj, struct device, kobj);
2228         struct toshiba_acpi_dev *drv = dev_get_drvdata(dev);
2229         bool exists = true;
2230
2231         if (attr == &dev_attr_fan.attr)
2232                 exists = (drv->fan_supported) ? true : false;
2233         else if (attr == &dev_attr_kbd_backlight_mode.attr)
2234                 exists = (drv->kbd_illum_supported) ? true : false;
2235         else if (attr == &dev_attr_kbd_backlight_timeout.attr)
2236                 exists = (drv->kbd_mode == SCI_KBD_MODE_AUTO) ? true : false;
2237         else if (attr == &dev_attr_touchpad.attr)
2238                 exists = (drv->touchpad_supported) ? true : false;
2239         else if (attr == &dev_attr_position.attr)
2240                 exists = (drv->accelerometer_supported) ? true : false;
2241         else if (attr == &dev_attr_usb_sleep_charge.attr)
2242                 exists = (drv->usb_sleep_charge_supported) ? true : false;
2243         else if (attr == &dev_attr_sleep_functions_on_battery.attr)
2244                 exists = (drv->usb_sleep_charge_supported) ? true : false;
2245         else if (attr == &dev_attr_usb_rapid_charge.attr)
2246                 exists = (drv->usb_rapid_charge_supported) ? true : false;
2247         else if (attr == &dev_attr_usb_sleep_music.attr)
2248                 exists = (drv->usb_sleep_music_supported) ? true : false;
2249         else if (attr == &dev_attr_kbd_function_keys.attr)
2250                 exists = (drv->kbd_function_keys_supported) ? true : false;
2251         else if (attr == &dev_attr_panel_power_on.attr)
2252                 exists = (drv->panel_power_on_supported) ? true : false;
2253         else if (attr == &dev_attr_usb_three.attr)
2254                 exists = (drv->usb_three_supported) ? true : false;
2255
2256         return exists ? attr->mode : 0;
2257 }
2258
2259 static struct attribute_group toshiba_attr_group = {
2260         .is_visible = toshiba_sysfs_is_visible,
2261         .attrs = toshiba_attributes,
2262 };
2263
2264 /*
2265  * Hotkeys
2266  */
2267 static int toshiba_acpi_enable_hotkeys(struct toshiba_acpi_dev *dev)
2268 {
2269         acpi_status status;
2270         u32 result;
2271
2272         status = acpi_evaluate_object(dev->acpi_dev->handle,
2273                                       "ENAB", NULL, NULL);
2274         if (ACPI_FAILURE(status))
2275                 return -ENODEV;
2276
2277         result = hci_write(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE);
2278         if (result == TOS_FAILURE)
2279                 return -EIO;
2280         else if (result == TOS_NOT_SUPPORTED)
2281                 return -ENODEV;
2282
2283         return 0;
2284 }
2285
2286 static void toshiba_acpi_enable_special_functions(struct toshiba_acpi_dev *dev)
2287 {
2288         u32 result;
2289
2290         /*
2291          * Re-activate the hotkeys, but this time, we are using the
2292          * "Special Functions" mode.
2293          */
2294         result = hci_write(dev, HCI_HOTKEY_EVENT,
2295                            HCI_HOTKEY_SPECIAL_FUNCTIONS);
2296         if (result != TOS_SUCCESS)
2297                 pr_err("Could not enable the Special Function mode\n");
2298 }
2299
2300 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str,
2301                                       struct serio *port)
2302 {
2303         if (str & I8042_STR_AUXDATA)
2304                 return false;
2305
2306         if (unlikely(data == 0xe0))
2307                 return false;
2308
2309         if ((data & 0x7f) == TOS1900_FN_SCAN) {
2310                 schedule_work(&toshiba_acpi->hotkey_work);
2311                 return true;
2312         }
2313
2314         return false;
2315 }
2316
2317 static void toshiba_acpi_hotkey_work(struct work_struct *work)
2318 {
2319         acpi_handle ec_handle = ec_get_handle();
2320         acpi_status status;
2321
2322         if (!ec_handle)
2323                 return;
2324
2325         status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL);
2326         if (ACPI_FAILURE(status))
2327                 pr_err("ACPI NTFY method execution failed\n");
2328 }
2329
2330 /*
2331  * Returns hotkey scancode, or < 0 on failure.
2332  */
2333 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev)
2334 {
2335         unsigned long long value;
2336         acpi_status status;
2337
2338         status = acpi_evaluate_integer(dev->acpi_dev->handle, "INFO",
2339                                       NULL, &value);
2340         if (ACPI_FAILURE(status)) {
2341                 pr_err("ACPI INFO method execution failed\n");
2342                 return -EIO;
2343         }
2344
2345         return value;
2346 }
2347
2348 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev,
2349                                        int scancode)
2350 {
2351         if (scancode == 0x100)
2352                 return;
2353
2354         /* Act on key press; ignore key release */
2355         if (scancode & 0x80)
2356                 return;
2357
2358         if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true))
2359                 pr_info("Unknown key %x\n", scancode);
2360 }
2361
2362 static void toshiba_acpi_process_hotkeys(struct toshiba_acpi_dev *dev)
2363 {
2364         u32 hci_result, value;
2365         int retries = 3;
2366         int scancode;
2367
2368         if (dev->info_supported) {
2369                 scancode = toshiba_acpi_query_hotkey(dev);
2370                 if (scancode < 0)
2371                         pr_err("Failed to query hotkey event\n");
2372                 else if (scancode != 0)
2373                         toshiba_acpi_report_hotkey(dev, scancode);
2374         } else if (dev->system_event_supported) {
2375                 do {
2376                         hci_result = hci_read(dev, HCI_SYSTEM_EVENT, &value);
2377                         switch (hci_result) {
2378                         case TOS_SUCCESS:
2379                                 toshiba_acpi_report_hotkey(dev, (int)value);
2380                                 break;
2381                         case TOS_NOT_SUPPORTED:
2382                                 /*
2383                                  * This is a workaround for an unresolved
2384                                  * issue on some machines where system events
2385                                  * sporadically become disabled.
2386                                  */
2387                                 hci_result =
2388                                         hci_write(dev, HCI_SYSTEM_EVENT, 1);
2389                                 pr_notice("Re-enabled hotkeys\n");
2390                                 /* Fall through */
2391                         default:
2392                                 retries--;
2393                                 break;
2394                         }
2395                 } while (retries && hci_result != TOS_FIFO_EMPTY);
2396         }
2397 }
2398
2399 static int toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev)
2400 {
2401         const struct key_entry *keymap = toshiba_acpi_keymap;
2402         acpi_handle ec_handle;
2403         u32 events_type;
2404         u32 hci_result;
2405         int error;
2406
2407         error = toshiba_acpi_enable_hotkeys(dev);
2408         if (error)
2409                 return error;
2410
2411         error = toshiba_hotkey_event_type_get(dev, &events_type);
2412         if (error) {
2413                 pr_err("Unable to query Hotkey Event Type\n");
2414                 return error;
2415         }
2416         dev->hotkey_event_type = events_type;
2417
2418         dev->hotkey_dev = input_allocate_device();
2419         if (!dev->hotkey_dev)
2420                 return -ENOMEM;
2421
2422         dev->hotkey_dev->name = "Toshiba input device";
2423         dev->hotkey_dev->phys = "toshiba_acpi/input0";
2424         dev->hotkey_dev->id.bustype = BUS_HOST;
2425
2426         if (events_type == HCI_SYSTEM_TYPE1 ||
2427             !dev->kbd_function_keys_supported)
2428                 keymap = toshiba_acpi_keymap;
2429         else if (events_type == HCI_SYSTEM_TYPE2 ||
2430                  dev->kbd_function_keys_supported)
2431                 keymap = toshiba_acpi_alt_keymap;
2432         else
2433                 pr_info("Unknown event type received %x\n", events_type);
2434         error = sparse_keymap_setup(dev->hotkey_dev, keymap, NULL);
2435         if (error)
2436                 goto err_free_dev;
2437
2438         /*
2439          * For some machines the SCI responsible for providing hotkey
2440          * notification doesn't fire. We can trigger the notification
2441          * whenever the Fn key is pressed using the NTFY method, if
2442          * supported, so if it's present set up an i8042 key filter
2443          * for this purpose.
2444          */
2445         ec_handle = ec_get_handle();
2446         if (ec_handle && acpi_has_method(ec_handle, "NTFY")) {
2447                 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work);
2448
2449                 error = i8042_install_filter(toshiba_acpi_i8042_filter);
2450                 if (error) {
2451                         pr_err("Error installing key filter\n");
2452                         goto err_free_keymap;
2453                 }
2454
2455                 dev->ntfy_supported = 1;
2456         }
2457
2458         /*
2459          * Determine hotkey query interface. Prefer using the INFO
2460          * method when it is available.
2461          */
2462         if (acpi_has_method(dev->acpi_dev->handle, "INFO"))
2463                 dev->info_supported = 1;
2464         else {
2465                 hci_result = hci_write(dev, HCI_SYSTEM_EVENT, 1);
2466                 if (hci_result == TOS_SUCCESS)
2467                         dev->system_event_supported = 1;
2468         }
2469
2470         if (!dev->info_supported && !dev->system_event_supported) {
2471                 pr_warn("No hotkey query interface found\n");
2472                 goto err_remove_filter;
2473         }
2474
2475         error = input_register_device(dev->hotkey_dev);
2476         if (error) {
2477                 pr_info("Unable to register input device\n");
2478                 goto err_remove_filter;
2479         }
2480
2481         return 0;
2482
2483  err_remove_filter:
2484         if (dev->ntfy_supported)
2485                 i8042_remove_filter(toshiba_acpi_i8042_filter);
2486  err_free_keymap:
2487         sparse_keymap_free(dev->hotkey_dev);
2488  err_free_dev:
2489         input_free_device(dev->hotkey_dev);
2490         dev->hotkey_dev = NULL;
2491         return error;
2492 }
2493
2494 static int toshiba_acpi_setup_backlight(struct toshiba_acpi_dev *dev)
2495 {
2496         struct backlight_properties props;
2497         int brightness;
2498         int ret;
2499         bool enabled;
2500
2501         /*
2502          * Some machines don't support the backlight methods at all, and
2503          * others support it read-only. Either of these is pretty useless,
2504          * so only register the backlight device if the backlight method
2505          * supports both reads and writes.
2506          */
2507         brightness = __get_lcd_brightness(dev);
2508         if (brightness < 0)
2509                 return 0;
2510         ret = set_lcd_brightness(dev, brightness);
2511         if (ret) {
2512                 pr_debug("Backlight method is read-only, disabling backlight support\n");
2513                 return 0;
2514         }
2515
2516         /* Determine whether or not BIOS supports transflective backlight */
2517         ret = get_tr_backlight_status(dev, &enabled);
2518         dev->tr_backlight_supported = !ret;
2519
2520         /*
2521          * Tell acpi-video-detect code to prefer vendor backlight on all
2522          * systems with transflective backlight and on dmi matched systems.
2523          */
2524         if (dev->tr_backlight_supported ||
2525             dmi_check_system(toshiba_vendor_backlight_dmi))
2526                 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2527
2528         if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2529                 return 0;
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);