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