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