sony-laptop: add hibernate on low battery function
[firefly-linux-kernel-4.4.55.git] / drivers / platform / x86 / sony-laptop.c
1 /*
2  * ACPI Sony Notebook Control Driver (SNC and SPIC)
3  *
4  * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5  * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
6  *
7  * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8  * which are copyrighted by their respective authors.
9  *
10  * The SNY6001 driver part is based on the sonypi driver which includes
11  * material from:
12  *
13  * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14  *
15  * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16  *
17  * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
18  *
19  * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20  *
21  * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22  *
23  * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24  *
25  * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26  *
27  * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28  *
29  * This program is free software; you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation; either version 2 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42  *
43  */
44
45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
47 #include <linux/kernel.h>
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/init.h>
51 #include <linux/types.h>
52 #include <linux/backlight.h>
53 #include <linux/platform_device.h>
54 #include <linux/err.h>
55 #include <linux/dmi.h>
56 #include <linux/pci.h>
57 #include <linux/interrupt.h>
58 #include <linux/delay.h>
59 #include <linux/input.h>
60 #include <linux/kfifo.h>
61 #include <linux/workqueue.h>
62 #include <linux/acpi.h>
63 #include <linux/slab.h>
64 #include <linux/sonypi.h>
65 #include <linux/sony-laptop.h>
66 #include <linux/rfkill.h>
67 #ifdef CONFIG_SONYPI_COMPAT
68 #include <linux/poll.h>
69 #include <linux/miscdevice.h>
70 #endif
71 #include <asm/uaccess.h>
72
73 #define dprintk(fmt, ...)                       \
74 do {                                            \
75         if (debug)                              \
76                 pr_warn(fmt, ##__VA_ARGS__);    \
77 } while (0)
78
79 #define SONY_LAPTOP_DRIVER_VERSION      "0.6"
80
81 #define SONY_NC_CLASS           "sony-nc"
82 #define SONY_NC_HID             "SNY5001"
83 #define SONY_NC_DRIVER_NAME     "Sony Notebook Control Driver"
84
85 #define SONY_PIC_CLASS          "sony-pic"
86 #define SONY_PIC_HID            "SNY6001"
87 #define SONY_PIC_DRIVER_NAME    "Sony Programmable IO Control Driver"
88
89 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
90 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
91 MODULE_LICENSE("GPL");
92 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
93
94 static int debug;
95 module_param(debug, int, 0);
96 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
97                  "the development of this driver");
98
99 static int no_spic;             /* = 0 */
100 module_param(no_spic, int, 0444);
101 MODULE_PARM_DESC(no_spic,
102                  "set this if you don't want to enable the SPIC device");
103
104 static int compat;              /* = 0 */
105 module_param(compat, int, 0444);
106 MODULE_PARM_DESC(compat,
107                  "set this if you want to enable backward compatibility mode");
108
109 static unsigned long mask = 0xffffffff;
110 module_param(mask, ulong, 0644);
111 MODULE_PARM_DESC(mask,
112                  "set this to the mask of event you want to enable (see doc)");
113
114 static int camera;              /* = 0 */
115 module_param(camera, int, 0444);
116 MODULE_PARM_DESC(camera,
117                  "set this to 1 to enable Motion Eye camera controls "
118                  "(only use it if you have a C1VE or C1VN model)");
119
120 #ifdef CONFIG_SONYPI_COMPAT
121 static int minor = -1;
122 module_param(minor, int, 0);
123 MODULE_PARM_DESC(minor,
124                  "minor number of the misc device for the SPIC compatibility code, "
125                  "default is -1 (automatic)");
126 #endif
127
128 static int kbd_backlight = -1;
129 module_param(kbd_backlight, int, 0444);
130 MODULE_PARM_DESC(kbd_backlight,
131                  "set this to 0 to disable keyboard backlight, "
132                  "1 to enable it (default: no change from current value)");
133
134 static int kbd_backlight_timeout = -1;
135 module_param(kbd_backlight_timeout, int, 0444);
136 MODULE_PARM_DESC(kbd_backlight_timeout,
137                  "meaningful values vary from 0 to 3 and their meaning depends "
138                  "on the model (default: no change from current value)");
139
140 #ifdef CONFIG_PM_SLEEP
141 static void sony_nc_thermal_resume(void);
142 #endif
143 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
144                 unsigned int handle);
145 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
146                 unsigned int handle);
147
148 static int sony_nc_battery_care_setup(struct platform_device *pd,
149                 unsigned int handle);
150 static void sony_nc_battery_care_cleanup(struct platform_device *pd);
151
152 static int sony_nc_thermal_setup(struct platform_device *pd);
153 static void sony_nc_thermal_cleanup(struct platform_device *pd);
154
155 static int sony_nc_lid_resume_setup(struct platform_device *pd,
156                                     unsigned int handle);
157 static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
158
159 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
160                 unsigned int handle);
161 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd);
162 static int __sony_nc_gfx_switch_status_get(void);
163
164 static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
165 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
166
167 static int sony_nc_lowbatt_setup(struct platform_device *pd);
168 static void sony_nc_lowbatt_cleanup(struct platform_device *pd);
169
170 static int sony_nc_fanspeed_setup(struct platform_device *pd);
171 static void sony_nc_fanspeed_cleanup(struct platform_device *pd);
172
173 static int sony_nc_usb_charge_setup(struct platform_device *pd);
174 static void sony_nc_usb_charge_cleanup(struct platform_device *pd);
175
176 static int sony_nc_panelid_setup(struct platform_device *pd);
177 static void sony_nc_panelid_cleanup(struct platform_device *pd);
178
179 static int sony_nc_touchpad_setup(struct platform_device *pd,
180                                   unsigned int handle);
181 static void sony_nc_touchpad_cleanup(struct platform_device *pd);
182
183 enum sony_nc_rfkill {
184         SONY_WIFI,
185         SONY_BLUETOOTH,
186         SONY_WWAN,
187         SONY_WIMAX,
188         N_SONY_RFKILL,
189 };
190
191 static int sony_rfkill_handle;
192 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
193 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
194 static int sony_nc_rfkill_setup(struct acpi_device *device,
195                 unsigned int handle);
196 static void sony_nc_rfkill_cleanup(void);
197 static void sony_nc_rfkill_update(void);
198
199 /*********** Input Devices ***********/
200
201 #define SONY_LAPTOP_BUF_SIZE    128
202 struct sony_laptop_input_s {
203         atomic_t                users;
204         struct input_dev        *jog_dev;
205         struct input_dev        *key_dev;
206         struct kfifo            fifo;
207         spinlock_t              fifo_lock;
208         struct timer_list       release_key_timer;
209 };
210
211 static struct sony_laptop_input_s sony_laptop_input = {
212         .users = ATOMIC_INIT(0),
213 };
214
215 struct sony_laptop_keypress {
216         struct input_dev *dev;
217         int key;
218 };
219
220 /* Correspondance table between sonypi events
221  * and input layer indexes in the keymap
222  */
223 static int sony_laptop_input_index[] = {
224         -1,     /*  0 no event */
225         -1,     /*  1 SONYPI_EVENT_JOGDIAL_DOWN */
226         -1,     /*  2 SONYPI_EVENT_JOGDIAL_UP */
227         -1,     /*  3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
228         -1,     /*  4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
229         -1,     /*  5 SONYPI_EVENT_JOGDIAL_PRESSED */
230         -1,     /*  6 SONYPI_EVENT_JOGDIAL_RELEASED */
231          0,     /*  7 SONYPI_EVENT_CAPTURE_PRESSED */
232          1,     /*  8 SONYPI_EVENT_CAPTURE_RELEASED */
233          2,     /*  9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
234          3,     /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
235          4,     /* 11 SONYPI_EVENT_FNKEY_ESC */
236          5,     /* 12 SONYPI_EVENT_FNKEY_F1 */
237          6,     /* 13 SONYPI_EVENT_FNKEY_F2 */
238          7,     /* 14 SONYPI_EVENT_FNKEY_F3 */
239          8,     /* 15 SONYPI_EVENT_FNKEY_F4 */
240          9,     /* 16 SONYPI_EVENT_FNKEY_F5 */
241         10,     /* 17 SONYPI_EVENT_FNKEY_F6 */
242         11,     /* 18 SONYPI_EVENT_FNKEY_F7 */
243         12,     /* 19 SONYPI_EVENT_FNKEY_F8 */
244         13,     /* 20 SONYPI_EVENT_FNKEY_F9 */
245         14,     /* 21 SONYPI_EVENT_FNKEY_F10 */
246         15,     /* 22 SONYPI_EVENT_FNKEY_F11 */
247         16,     /* 23 SONYPI_EVENT_FNKEY_F12 */
248         17,     /* 24 SONYPI_EVENT_FNKEY_1 */
249         18,     /* 25 SONYPI_EVENT_FNKEY_2 */
250         19,     /* 26 SONYPI_EVENT_FNKEY_D */
251         20,     /* 27 SONYPI_EVENT_FNKEY_E */
252         21,     /* 28 SONYPI_EVENT_FNKEY_F */
253         22,     /* 29 SONYPI_EVENT_FNKEY_S */
254         23,     /* 30 SONYPI_EVENT_FNKEY_B */
255         24,     /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
256         25,     /* 32 SONYPI_EVENT_PKEY_P1 */
257         26,     /* 33 SONYPI_EVENT_PKEY_P2 */
258         27,     /* 34 SONYPI_EVENT_PKEY_P3 */
259         28,     /* 35 SONYPI_EVENT_BACK_PRESSED */
260         -1,     /* 36 SONYPI_EVENT_LID_CLOSED */
261         -1,     /* 37 SONYPI_EVENT_LID_OPENED */
262         29,     /* 38 SONYPI_EVENT_BLUETOOTH_ON */
263         30,     /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
264         31,     /* 40 SONYPI_EVENT_HELP_PRESSED */
265         32,     /* 41 SONYPI_EVENT_FNKEY_ONLY */
266         33,     /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
267         34,     /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
268         35,     /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
269         36,     /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
270         37,     /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
271         38,     /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
272         39,     /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
273         40,     /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
274         41,     /* 50 SONYPI_EVENT_ZOOM_PRESSED */
275         42,     /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
276         43,     /* 52 SONYPI_EVENT_MEYE_FACE */
277         44,     /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
278         45,     /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
279         46,     /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
280         -1,     /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
281         -1,     /* 57 SONYPI_EVENT_BATTERY_INSERT */
282         -1,     /* 58 SONYPI_EVENT_BATTERY_REMOVE */
283         -1,     /* 59 SONYPI_EVENT_FNKEY_RELEASED */
284         47,     /* 60 SONYPI_EVENT_WIRELESS_ON */
285         48,     /* 61 SONYPI_EVENT_WIRELESS_OFF */
286         49,     /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
287         50,     /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
288         51,     /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
289         52,     /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
290         53,     /* 66 SONYPI_EVENT_PKEY_P4 */
291         54,     /* 67 SONYPI_EVENT_PKEY_P5 */
292         55,     /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
293         56,     /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
294         57,     /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
295         -1,     /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
296         58,     /* 72 SONYPI_EVENT_MEDIA_PRESSED */
297         59,     /* 72 SONYPI_EVENT_VENDOR_PRESSED */
298 };
299
300 static int sony_laptop_input_keycode_map[] = {
301         KEY_CAMERA,     /*  0 SONYPI_EVENT_CAPTURE_PRESSED */
302         KEY_RESERVED,   /*  1 SONYPI_EVENT_CAPTURE_RELEASED */
303         KEY_RESERVED,   /*  2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
304         KEY_RESERVED,   /*  3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
305         KEY_FN_ESC,     /*  4 SONYPI_EVENT_FNKEY_ESC */
306         KEY_FN_F1,      /*  5 SONYPI_EVENT_FNKEY_F1 */
307         KEY_FN_F2,      /*  6 SONYPI_EVENT_FNKEY_F2 */
308         KEY_FN_F3,      /*  7 SONYPI_EVENT_FNKEY_F3 */
309         KEY_FN_F4,      /*  8 SONYPI_EVENT_FNKEY_F4 */
310         KEY_FN_F5,      /*  9 SONYPI_EVENT_FNKEY_F5 */
311         KEY_FN_F6,      /* 10 SONYPI_EVENT_FNKEY_F6 */
312         KEY_FN_F7,      /* 11 SONYPI_EVENT_FNKEY_F7 */
313         KEY_FN_F8,      /* 12 SONYPI_EVENT_FNKEY_F8 */
314         KEY_FN_F9,      /* 13 SONYPI_EVENT_FNKEY_F9 */
315         KEY_FN_F10,     /* 14 SONYPI_EVENT_FNKEY_F10 */
316         KEY_FN_F11,     /* 15 SONYPI_EVENT_FNKEY_F11 */
317         KEY_FN_F12,     /* 16 SONYPI_EVENT_FNKEY_F12 */
318         KEY_FN_1,       /* 17 SONYPI_EVENT_FNKEY_1 */
319         KEY_FN_2,       /* 18 SONYPI_EVENT_FNKEY_2 */
320         KEY_FN_D,       /* 19 SONYPI_EVENT_FNKEY_D */
321         KEY_FN_E,       /* 20 SONYPI_EVENT_FNKEY_E */
322         KEY_FN_F,       /* 21 SONYPI_EVENT_FNKEY_F */
323         KEY_FN_S,       /* 22 SONYPI_EVENT_FNKEY_S */
324         KEY_FN_B,       /* 23 SONYPI_EVENT_FNKEY_B */
325         KEY_BLUETOOTH,  /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
326         KEY_PROG1,      /* 25 SONYPI_EVENT_PKEY_P1 */
327         KEY_PROG2,      /* 26 SONYPI_EVENT_PKEY_P2 */
328         KEY_PROG3,      /* 27 SONYPI_EVENT_PKEY_P3 */
329         KEY_BACK,       /* 28 SONYPI_EVENT_BACK_PRESSED */
330         KEY_BLUETOOTH,  /* 29 SONYPI_EVENT_BLUETOOTH_ON */
331         KEY_BLUETOOTH,  /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
332         KEY_HELP,       /* 31 SONYPI_EVENT_HELP_PRESSED */
333         KEY_FN,         /* 32 SONYPI_EVENT_FNKEY_ONLY */
334         KEY_RESERVED,   /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
335         KEY_RESERVED,   /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
336         KEY_RESERVED,   /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
337         KEY_RESERVED,   /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
338         KEY_RESERVED,   /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
339         KEY_RESERVED,   /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
340         KEY_RESERVED,   /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
341         KEY_RESERVED,   /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
342         KEY_ZOOM,       /* 41 SONYPI_EVENT_ZOOM_PRESSED */
343         BTN_THUMB,      /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
344         KEY_RESERVED,   /* 43 SONYPI_EVENT_MEYE_FACE */
345         KEY_RESERVED,   /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
346         KEY_RESERVED,   /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
347         KEY_RESERVED,   /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
348         KEY_WLAN,       /* 47 SONYPI_EVENT_WIRELESS_ON */
349         KEY_WLAN,       /* 48 SONYPI_EVENT_WIRELESS_OFF */
350         KEY_ZOOMIN,     /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
351         KEY_ZOOMOUT,    /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
352         KEY_EJECTCD,    /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
353         KEY_F13,        /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
354         KEY_PROG4,      /* 53 SONYPI_EVENT_PKEY_P4 */
355         KEY_F14,        /* 54 SONYPI_EVENT_PKEY_P5 */
356         KEY_F15,        /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
357         KEY_VOLUMEUP,   /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
358         KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
359         KEY_MEDIA,      /* 58 SONYPI_EVENT_MEDIA_PRESSED */
360         KEY_VENDOR,     /* 59 SONYPI_EVENT_VENDOR_PRESSED */
361 };
362
363 /* release buttons after a short delay if pressed */
364 static void do_sony_laptop_release_key(unsigned long unused)
365 {
366         struct sony_laptop_keypress kp;
367         unsigned long flags;
368
369         spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
370
371         if (kfifo_out(&sony_laptop_input.fifo,
372                       (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
373                 input_report_key(kp.dev, kp.key, 0);
374                 input_sync(kp.dev);
375         }
376
377         /* If there is something in the fifo schedule next release. */
378         if (kfifo_len(&sony_laptop_input.fifo) != 0)
379                 mod_timer(&sony_laptop_input.release_key_timer,
380                           jiffies + msecs_to_jiffies(10));
381
382         spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
383 }
384
385 /* forward event to the input subsystem */
386 static void sony_laptop_report_input_event(u8 event)
387 {
388         struct input_dev *jog_dev = sony_laptop_input.jog_dev;
389         struct input_dev *key_dev = sony_laptop_input.key_dev;
390         struct sony_laptop_keypress kp = { NULL };
391         int scancode = -1;
392
393         if (event == SONYPI_EVENT_FNKEY_RELEASED ||
394                         event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
395                 /* Nothing, not all VAIOs generate this event */
396                 return;
397         }
398
399         /* report events */
400         switch (event) {
401         /* jog_dev events */
402         case SONYPI_EVENT_JOGDIAL_UP:
403         case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
404                 input_report_rel(jog_dev, REL_WHEEL, 1);
405                 input_sync(jog_dev);
406                 return;
407
408         case SONYPI_EVENT_JOGDIAL_DOWN:
409         case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
410                 input_report_rel(jog_dev, REL_WHEEL, -1);
411                 input_sync(jog_dev);
412                 return;
413
414         /* key_dev events */
415         case SONYPI_EVENT_JOGDIAL_PRESSED:
416                 kp.key = BTN_MIDDLE;
417                 kp.dev = jog_dev;
418                 break;
419
420         default:
421                 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
422                         dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
423                         break;
424                 }
425                 if ((scancode = sony_laptop_input_index[event]) != -1) {
426                         kp.key = sony_laptop_input_keycode_map[scancode];
427                         if (kp.key != KEY_UNKNOWN)
428                                 kp.dev = key_dev;
429                 }
430                 break;
431         }
432
433         if (kp.dev) {
434                 /* if we have a scancode we emit it so we can always
435                     remap the key */
436                 if (scancode != -1)
437                         input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
438                 input_report_key(kp.dev, kp.key, 1);
439                 input_sync(kp.dev);
440
441                 /* schedule key release */
442                 kfifo_in_locked(&sony_laptop_input.fifo,
443                                 (unsigned char *)&kp, sizeof(kp),
444                                 &sony_laptop_input.fifo_lock);
445                 mod_timer(&sony_laptop_input.release_key_timer,
446                           jiffies + msecs_to_jiffies(10));
447         } else
448                 dprintk("unknown input event %.2x\n", event);
449 }
450
451 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
452 {
453         struct input_dev *jog_dev;
454         struct input_dev *key_dev;
455         int i;
456         int error;
457
458         /* don't run again if already initialized */
459         if (atomic_add_return(1, &sony_laptop_input.users) > 1)
460                 return 0;
461
462         /* kfifo */
463         spin_lock_init(&sony_laptop_input.fifo_lock);
464         error = kfifo_alloc(&sony_laptop_input.fifo,
465                             SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
466         if (error) {
467                 pr_err("kfifo_alloc failed\n");
468                 goto err_dec_users;
469         }
470
471         setup_timer(&sony_laptop_input.release_key_timer,
472                     do_sony_laptop_release_key, 0);
473
474         /* input keys */
475         key_dev = input_allocate_device();
476         if (!key_dev) {
477                 error = -ENOMEM;
478                 goto err_free_kfifo;
479         }
480
481         key_dev->name = "Sony Vaio Keys";
482         key_dev->id.bustype = BUS_ISA;
483         key_dev->id.vendor = PCI_VENDOR_ID_SONY;
484         key_dev->dev.parent = &acpi_device->dev;
485
486         /* Initialize the Input Drivers: special keys */
487         input_set_capability(key_dev, EV_MSC, MSC_SCAN);
488
489         __set_bit(EV_KEY, key_dev->evbit);
490         key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
491         key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
492         key_dev->keycode = &sony_laptop_input_keycode_map;
493         for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
494                 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
495         __clear_bit(KEY_RESERVED, key_dev->keybit);
496
497         error = input_register_device(key_dev);
498         if (error)
499                 goto err_free_keydev;
500
501         sony_laptop_input.key_dev = key_dev;
502
503         /* jogdial */
504         jog_dev = input_allocate_device();
505         if (!jog_dev) {
506                 error = -ENOMEM;
507                 goto err_unregister_keydev;
508         }
509
510         jog_dev->name = "Sony Vaio Jogdial";
511         jog_dev->id.bustype = BUS_ISA;
512         jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
513         jog_dev->dev.parent = &acpi_device->dev;
514
515         input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
516         input_set_capability(jog_dev, EV_REL, REL_WHEEL);
517
518         error = input_register_device(jog_dev);
519         if (error)
520                 goto err_free_jogdev;
521
522         sony_laptop_input.jog_dev = jog_dev;
523
524         return 0;
525
526 err_free_jogdev:
527         input_free_device(jog_dev);
528
529 err_unregister_keydev:
530         input_unregister_device(key_dev);
531         /* to avoid kref underflow below at input_free_device */
532         key_dev = NULL;
533
534 err_free_keydev:
535         input_free_device(key_dev);
536
537 err_free_kfifo:
538         kfifo_free(&sony_laptop_input.fifo);
539
540 err_dec_users:
541         atomic_dec(&sony_laptop_input.users);
542         return error;
543 }
544
545 static void sony_laptop_remove_input(void)
546 {
547         struct sony_laptop_keypress kp = { NULL };
548
549         /* Cleanup only after the last user has gone */
550         if (!atomic_dec_and_test(&sony_laptop_input.users))
551                 return;
552
553         del_timer_sync(&sony_laptop_input.release_key_timer);
554
555         /*
556          * Generate key-up events for remaining keys. Note that we don't
557          * need locking since nobody is adding new events to the kfifo.
558          */
559         while (kfifo_out(&sony_laptop_input.fifo,
560                          (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
561                 input_report_key(kp.dev, kp.key, 0);
562                 input_sync(kp.dev);
563         }
564
565         /* destroy input devs */
566         input_unregister_device(sony_laptop_input.key_dev);
567         sony_laptop_input.key_dev = NULL;
568
569         if (sony_laptop_input.jog_dev) {
570                 input_unregister_device(sony_laptop_input.jog_dev);
571                 sony_laptop_input.jog_dev = NULL;
572         }
573
574         kfifo_free(&sony_laptop_input.fifo);
575 }
576
577 /*********** Platform Device ***********/
578
579 static atomic_t sony_pf_users = ATOMIC_INIT(0);
580 static struct platform_driver sony_pf_driver = {
581         .driver = {
582                    .name = "sony-laptop",
583                    .owner = THIS_MODULE,
584                    }
585 };
586 static struct platform_device *sony_pf_device;
587
588 static int sony_pf_add(void)
589 {
590         int ret = 0;
591
592         /* don't run again if already initialized */
593         if (atomic_add_return(1, &sony_pf_users) > 1)
594                 return 0;
595
596         ret = platform_driver_register(&sony_pf_driver);
597         if (ret)
598                 goto out;
599
600         sony_pf_device = platform_device_alloc("sony-laptop", -1);
601         if (!sony_pf_device) {
602                 ret = -ENOMEM;
603                 goto out_platform_registered;
604         }
605
606         ret = platform_device_add(sony_pf_device);
607         if (ret)
608                 goto out_platform_alloced;
609
610         return 0;
611
612       out_platform_alloced:
613         platform_device_put(sony_pf_device);
614         sony_pf_device = NULL;
615       out_platform_registered:
616         platform_driver_unregister(&sony_pf_driver);
617       out:
618         atomic_dec(&sony_pf_users);
619         return ret;
620 }
621
622 static void sony_pf_remove(void)
623 {
624         /* deregister only after the last user has gone */
625         if (!atomic_dec_and_test(&sony_pf_users))
626                 return;
627
628         platform_device_unregister(sony_pf_device);
629         platform_driver_unregister(&sony_pf_driver);
630 }
631
632 /*********** SNC (SNY5001) Device ***********/
633
634 /* the device uses 1-based values, while the backlight subsystem uses
635    0-based values */
636 #define SONY_MAX_BRIGHTNESS     8
637
638 #define SNC_VALIDATE_IN         0
639 #define SNC_VALIDATE_OUT        1
640
641 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
642                               char *);
643 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
644                                const char *, size_t);
645 static int boolean_validate(const int, const int);
646 static int brightness_default_validate(const int, const int);
647
648 struct sony_nc_value {
649         char *name;             /* name of the entry */
650         char **acpiget;         /* names of the ACPI get function */
651         char **acpiset;         /* names of the ACPI set function */
652         int (*validate)(const int, const int);  /* input/output validation */
653         int value;              /* current setting */
654         int valid;              /* Has ever been set */
655         int debug;              /* active only in debug mode ? */
656         struct device_attribute devattr;        /* sysfs attribute */
657 };
658
659 #define SNC_HANDLE_NAMES(_name, _values...) \
660         static char *snc_##_name[] = { _values, NULL }
661
662 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
663         { \
664                 .name           = __stringify(_name), \
665                 .acpiget        = _getters, \
666                 .acpiset        = _setters, \
667                 .validate       = _validate, \
668                 .debug          = _debug, \
669                 .devattr        = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
670         }
671
672 #define SNC_HANDLE_NULL { .name = NULL }
673
674 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
675
676 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
677 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
678
679 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
680 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
681
682 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
683 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
684
685 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
686 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
687
688 SNC_HANDLE_NAMES(lidstate_get, "GLID");
689
690 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
691 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
692
693 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
694 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
695
696 SNC_HANDLE_NAMES(PID_get, "GPID");
697
698 SNC_HANDLE_NAMES(CTR_get, "GCTR");
699 SNC_HANDLE_NAMES(CTR_set, "SCTR");
700
701 SNC_HANDLE_NAMES(PCR_get, "GPCR");
702 SNC_HANDLE_NAMES(PCR_set, "SPCR");
703
704 SNC_HANDLE_NAMES(CMI_get, "GCMI");
705 SNC_HANDLE_NAMES(CMI_set, "SCMI");
706
707 static struct sony_nc_value sony_nc_values[] = {
708         SNC_HANDLE(brightness_default, snc_brightness_def_get,
709                         snc_brightness_def_set, brightness_default_validate, 0),
710         SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
711         SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
712         SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
713                         boolean_validate, 0),
714         SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
715                         boolean_validate, 1),
716         SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
717                         boolean_validate, 0),
718         SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
719                         boolean_validate, 0),
720         SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
721                         boolean_validate, 0),
722         /* unknown methods */
723         SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
724         SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
725         SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
726         SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
727         SNC_HANDLE_NULL
728 };
729
730 static acpi_handle sony_nc_acpi_handle;
731 static struct acpi_device *sony_nc_acpi_device = NULL;
732
733 /*
734  * acpi_evaluate_object wrappers
735  * all useful calls into SNC methods take one or zero parameters and return
736  * integers or arrays.
737  */
738 static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
739                 u64 *value)
740 {
741         union acpi_object *result = NULL;
742         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
743         acpi_status status;
744
745         if (value) {
746                 struct acpi_object_list params;
747                 union acpi_object in;
748                 in.type = ACPI_TYPE_INTEGER;
749                 in.integer.value = *value;
750                 params.count = 1;
751                 params.pointer = &in;
752                 status = acpi_evaluate_object(handle, method, &params, &output);
753                 dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
754                                 (unsigned int)(*value >> 32),
755                                 (unsigned int)*value & 0xffffffff);
756         } else {
757                 status = acpi_evaluate_object(handle, method, NULL, &output);
758                 dprintk("__call_snc_method: [%s]\n", method);
759         }
760
761         if (ACPI_FAILURE(status)) {
762                 pr_err("Failed to evaluate [%s]\n", method);
763                 return NULL;
764         }
765
766         result = (union acpi_object *) output.pointer;
767         if (!result)
768                 dprintk("No return object [%s]\n", method);
769
770         return result;
771 }
772
773 static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
774                 int *result)
775 {
776         union acpi_object *object = NULL;
777         if (value) {
778                 u64 v = *value;
779                 object = __call_snc_method(handle, name, &v);
780         } else
781                 object = __call_snc_method(handle, name, NULL);
782
783         if (!object)
784                 return -EINVAL;
785
786         if (object->type != ACPI_TYPE_INTEGER) {
787                 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
788                                 ACPI_TYPE_INTEGER, object->type);
789                 kfree(object);
790                 return -EINVAL;
791         }
792
793         if (result)
794                 *result = object->integer.value;
795
796         kfree(object);
797         return 0;
798 }
799
800 #define MIN(a, b)       (a > b ? b : a)
801 static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
802                 void *buffer, size_t buflen)
803 {
804         int ret = 0;
805         size_t len;
806         union acpi_object *object = __call_snc_method(handle, name, value);
807
808         if (!object)
809                 return -EINVAL;
810
811         if (object->type == ACPI_TYPE_BUFFER) {
812                 len = MIN(buflen, object->buffer.length);
813                 memcpy(buffer, object->buffer.pointer, len);
814
815         } else if (object->type == ACPI_TYPE_INTEGER) {
816                 len = MIN(buflen, sizeof(object->integer.value));
817                 memcpy(buffer, &object->integer.value, len);
818
819         } else {
820                 pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
821                                 ACPI_TYPE_BUFFER, object->type);
822                 ret = -EINVAL;
823         }
824
825         kfree(object);
826         return ret;
827 }
828
829 struct sony_nc_handles {
830         u16 cap[0x10];
831         struct device_attribute devattr;
832 };
833
834 static struct sony_nc_handles *handles;
835
836 static ssize_t sony_nc_handles_show(struct device *dev,
837                 struct device_attribute *attr, char *buffer)
838 {
839         ssize_t len = 0;
840         int i;
841
842         for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
843                 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
844                                 handles->cap[i]);
845         }
846         len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
847
848         return len;
849 }
850
851 static int sony_nc_handles_setup(struct platform_device *pd)
852 {
853         int i, r, result, arg;
854
855         handles = kzalloc(sizeof(*handles), GFP_KERNEL);
856         if (!handles)
857                 return -ENOMEM;
858
859         for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
860                 arg = i + 0x20;
861                 r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
862                                         &result);
863                 if (!r) {
864                         dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
865                                         result, i);
866                         handles->cap[i] = result;
867                 }
868         }
869
870         if (debug) {
871                 sysfs_attr_init(&handles->devattr.attr);
872                 handles->devattr.attr.name = "handles";
873                 handles->devattr.attr.mode = S_IRUGO;
874                 handles->devattr.show = sony_nc_handles_show;
875
876                 /* allow reading capabilities via sysfs */
877                 if (device_create_file(&pd->dev, &handles->devattr)) {
878                         kfree(handles);
879                         handles = NULL;
880                         return -1;
881                 }
882         }
883
884         return 0;
885 }
886
887 static int sony_nc_handles_cleanup(struct platform_device *pd)
888 {
889         if (handles) {
890                 if (debug)
891                         device_remove_file(&pd->dev, &handles->devattr);
892                 kfree(handles);
893                 handles = NULL;
894         }
895         return 0;
896 }
897
898 static int sony_find_snc_handle(int handle)
899 {
900         int i;
901
902         /* not initialized yet, return early */
903         if (!handles || !handle)
904                 return -EINVAL;
905
906         for (i = 0; i < 0x10; i++) {
907                 if (handles->cap[i] == handle) {
908                         dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
909                                         handle, i);
910                         return i;
911                 }
912         }
913         dprintk("handle 0x%.4x not found\n", handle);
914         return -EINVAL;
915 }
916
917 static int sony_call_snc_handle(int handle, int argument, int *result)
918 {
919         int arg, ret = 0;
920         int offset = sony_find_snc_handle(handle);
921
922         if (offset < 0)
923                 return offset;
924
925         arg = offset | argument;
926         ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
927         dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
928         return ret;
929 }
930
931 /*
932  * sony_nc_values input/output validate functions
933  */
934
935 /* brightness_default_validate:
936  *
937  * manipulate input output values to keep consistency with the
938  * backlight framework for which brightness values are 0-based.
939  */
940 static int brightness_default_validate(const int direction, const int value)
941 {
942         switch (direction) {
943                 case SNC_VALIDATE_OUT:
944                         return value - 1;
945                 case SNC_VALIDATE_IN:
946                         if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
947                                 return value + 1;
948         }
949         return -EINVAL;
950 }
951
952 /* boolean_validate:
953  *
954  * on input validate boolean values 0/1, on output just pass the
955  * received value.
956  */
957 static int boolean_validate(const int direction, const int value)
958 {
959         if (direction == SNC_VALIDATE_IN) {
960                 if (value != 0 && value != 1)
961                         return -EINVAL;
962         }
963         return value;
964 }
965
966 /*
967  * Sysfs show/store common to all sony_nc_values
968  */
969 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
970                               char *buffer)
971 {
972         int value, ret = 0;
973         struct sony_nc_value *item =
974             container_of(attr, struct sony_nc_value, devattr);
975
976         if (!*item->acpiget)
977                 return -EIO;
978
979         ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
980                                 &value);
981         if (ret < 0)
982                 return -EIO;
983
984         if (item->validate)
985                 value = item->validate(SNC_VALIDATE_OUT, value);
986
987         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
988 }
989
990 static ssize_t sony_nc_sysfs_store(struct device *dev,
991                                struct device_attribute *attr,
992                                const char *buffer, size_t count)
993 {
994         int value;
995         int ret = 0;
996         struct sony_nc_value *item =
997             container_of(attr, struct sony_nc_value, devattr);
998
999         if (!item->acpiset)
1000                 return -EIO;
1001
1002         if (count > 31)
1003                 return -EINVAL;
1004
1005         if (kstrtoint(buffer, 10, &value))
1006                 return -EINVAL;
1007
1008         if (item->validate)
1009                 value = item->validate(SNC_VALIDATE_IN, value);
1010
1011         if (value < 0)
1012                 return value;
1013
1014         ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1015                                &value, NULL);
1016         if (ret < 0)
1017                 return -EIO;
1018
1019         item->value = value;
1020         item->valid = 1;
1021         return count;
1022 }
1023
1024
1025 /*
1026  * Backlight device
1027  */
1028 struct sony_backlight_props {
1029         struct backlight_device *dev;
1030         int                     handle;
1031         int                     cmd_base;
1032         u8                      offset;
1033         u8                      maxlvl;
1034 };
1035 struct sony_backlight_props sony_bl_props;
1036
1037 static int sony_backlight_update_status(struct backlight_device *bd)
1038 {
1039         int arg = bd->props.brightness + 1;
1040         return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
1041 }
1042
1043 static int sony_backlight_get_brightness(struct backlight_device *bd)
1044 {
1045         int value;
1046
1047         if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
1048                 return 0;
1049         /* brightness levels are 1-based, while backlight ones are 0-based */
1050         return value - 1;
1051 }
1052
1053 static int sony_nc_get_brightness_ng(struct backlight_device *bd)
1054 {
1055         int result;
1056         struct sony_backlight_props *sdev =
1057                 (struct sony_backlight_props *)bl_get_data(bd);
1058
1059         sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
1060
1061         return (result & 0xff) - sdev->offset;
1062 }
1063
1064 static int sony_nc_update_status_ng(struct backlight_device *bd)
1065 {
1066         int value, result;
1067         struct sony_backlight_props *sdev =
1068                 (struct sony_backlight_props *)bl_get_data(bd);
1069
1070         value = bd->props.brightness + sdev->offset;
1071         if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
1072                                 &result))
1073                 return -EIO;
1074
1075         return value;
1076 }
1077
1078 static const struct backlight_ops sony_backlight_ops = {
1079         .options = BL_CORE_SUSPENDRESUME,
1080         .update_status = sony_backlight_update_status,
1081         .get_brightness = sony_backlight_get_brightness,
1082 };
1083 static const struct backlight_ops sony_backlight_ng_ops = {
1084         .options = BL_CORE_SUSPENDRESUME,
1085         .update_status = sony_nc_update_status_ng,
1086         .get_brightness = sony_nc_get_brightness_ng,
1087 };
1088
1089 /*
1090  * New SNC-only Vaios event mapping to driver known keys
1091  */
1092 struct sony_nc_event {
1093         u8      data;
1094         u8      event;
1095 };
1096
1097 static struct sony_nc_event sony_100_events[] = {
1098         { 0x90, SONYPI_EVENT_PKEY_P1 },
1099         { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
1100         { 0x91, SONYPI_EVENT_PKEY_P2 },
1101         { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
1102         { 0x81, SONYPI_EVENT_FNKEY_F1 },
1103         { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
1104         { 0x82, SONYPI_EVENT_FNKEY_F2 },
1105         { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1106         { 0x83, SONYPI_EVENT_FNKEY_F3 },
1107         { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1108         { 0x84, SONYPI_EVENT_FNKEY_F4 },
1109         { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
1110         { 0x85, SONYPI_EVENT_FNKEY_F5 },
1111         { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1112         { 0x86, SONYPI_EVENT_FNKEY_F6 },
1113         { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1114         { 0x87, SONYPI_EVENT_FNKEY_F7 },
1115         { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
1116         { 0x88, SONYPI_EVENT_FNKEY_F8 },
1117         { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
1118         { 0x89, SONYPI_EVENT_FNKEY_F9 },
1119         { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
1120         { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1121         { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1122         { 0x8B, SONYPI_EVENT_FNKEY_F11 },
1123         { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
1124         { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1125         { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
1126         { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1127         { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
1128         { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1129         { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
1130         { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1131         { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
1132         { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1133         { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1134         { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1135         { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1136         { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1137         { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
1138         { 0xa8, SONYPI_EVENT_FNKEY_1 },
1139         { 0x28, SONYPI_EVENT_ANYBUTTON_RELEASED },
1140         { 0, 0 },
1141 };
1142
1143 static struct sony_nc_event sony_127_events[] = {
1144         { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1145         { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1146         { 0x82, SONYPI_EVENT_PKEY_P1 },
1147         { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1148         { 0x83, SONYPI_EVENT_PKEY_P2 },
1149         { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1150         { 0x84, SONYPI_EVENT_PKEY_P3 },
1151         { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1152         { 0x85, SONYPI_EVENT_PKEY_P4 },
1153         { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1154         { 0x86, SONYPI_EVENT_PKEY_P5 },
1155         { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
1156         { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1157         { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1158         { 0, 0 },
1159 };
1160
1161 static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
1162 {
1163         int ret = -EINVAL;
1164         unsigned int result = 0;
1165         struct sony_nc_event *key_event;
1166
1167         if (sony_call_snc_handle(handle, 0x200, &result)) {
1168                 dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
1169                                 event);
1170                 return -EINVAL;
1171         }
1172
1173         result &= 0xFF;
1174
1175         if (handle == 0x0100)
1176                 key_event = sony_100_events;
1177         else
1178                 key_event = sony_127_events;
1179
1180         for (; key_event->data; key_event++) {
1181                 if (key_event->data == result) {
1182                         ret = key_event->event;
1183                         break;
1184                 }
1185         }
1186
1187         if (!key_event->data)
1188                 pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
1189                                 event, result, handle);
1190
1191         return ret;
1192 }
1193
1194 /*
1195  * ACPI callbacks
1196  */
1197 enum event_types {
1198         HOTKEY = 1,
1199         KILLSWITCH,
1200         GFX_SWITCH
1201 };
1202 static void sony_nc_notify(struct acpi_device *device, u32 event)
1203 {
1204         u32 real_ev = event;
1205         u8 ev_type = 0;
1206         dprintk("sony_nc_notify, event: 0x%.2x\n", event);
1207
1208         if (event >= 0x90) {
1209                 unsigned int result = 0;
1210                 unsigned int arg = 0;
1211                 unsigned int handle = 0;
1212                 unsigned int offset = event - 0x90;
1213
1214                 if (offset >= ARRAY_SIZE(handles->cap)) {
1215                         pr_err("Event 0x%x outside of capabilities list\n",
1216                                         event);
1217                         return;
1218                 }
1219                 handle = handles->cap[offset];
1220
1221                 /* list of handles known for generating events */
1222                 switch (handle) {
1223                 /* hotkey event */
1224                 case 0x0100:
1225                 case 0x0127:
1226                         ev_type = HOTKEY;
1227                         real_ev = sony_nc_hotkeys_decode(event, handle);
1228
1229                         if (real_ev > 0)
1230                                 sony_laptop_report_input_event(real_ev);
1231                         else
1232                                 /* restore the original event for reporting */
1233                                 real_ev = event;
1234
1235                         break;
1236
1237                 /* wlan switch */
1238                 case 0x0124:
1239                 case 0x0135:
1240                         /* events on this handle are reported when the
1241                          * switch changes position or for battery
1242                          * events. We'll notify both of them but only
1243                          * update the rfkill device status when the
1244                          * switch is moved.
1245                          */
1246                         ev_type = KILLSWITCH;
1247                         sony_call_snc_handle(handle, 0x0100, &result);
1248                         real_ev = result & 0x03;
1249
1250                         /* hw switch event */
1251                         if (real_ev == 1)
1252                                 sony_nc_rfkill_update();
1253
1254                         break;
1255
1256                 case 0x0128:
1257                 case 0x0146:
1258                         /* Hybrid GFX switching */
1259                         sony_call_snc_handle(handle, 0x0000, &result);
1260                         dprintk("GFX switch event received (reason: %s)\n",
1261                                         (result == 0x1) ? "switch change" :
1262                                         (result == 0x2) ? "output switch" :
1263                                         (result == 0x3) ? "output switch" :
1264                                         "");
1265
1266                         ev_type = GFX_SWITCH;
1267                         real_ev = __sony_nc_gfx_switch_status_get();
1268                         break;
1269
1270                 case 0x015B:
1271                         /* Hybrid GFX switching SVS151290S */
1272                         ev_type = GFX_SWITCH;
1273                         real_ev = __sony_nc_gfx_switch_status_get();
1274                         break;
1275                 default:
1276                         dprintk("Unknown event 0x%x for handle 0x%x\n",
1277                                         event, handle);
1278                         break;
1279                 }
1280
1281                 /* clear the event (and the event reason when present) */
1282                 arg = 1 << offset;
1283                 sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
1284
1285         } else {
1286                 /* old style event */
1287                 ev_type = HOTKEY;
1288                 sony_laptop_report_input_event(real_ev);
1289         }
1290         acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
1291                         dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
1292 }
1293
1294 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1295                                       void *context, void **return_value)
1296 {
1297         struct acpi_device_info *info;
1298
1299         if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
1300                 pr_warn("method: name: %4.4s, args %X\n",
1301                         (char *)&info->name, info->param_count);
1302
1303                 kfree(info);
1304         }
1305
1306         return AE_OK;
1307 }
1308
1309 /*
1310  * ACPI device
1311  */
1312 static void sony_nc_function_setup(struct acpi_device *device,
1313                 struct platform_device *pf_device)
1314 {
1315         unsigned int i, result, bitmask, arg;
1316
1317         if (!handles)
1318                 return;
1319
1320         /* setup found handles here */
1321         for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1322                 unsigned int handle = handles->cap[i];
1323
1324                 if (!handle)
1325                         continue;
1326
1327                 dprintk("setting up handle 0x%.4x\n", handle);
1328
1329                 switch (handle) {
1330                 case 0x0100:
1331                 case 0x0101:
1332                 case 0x0127:
1333                         /* setup hotkeys */
1334                         sony_call_snc_handle(handle, 0, &result);
1335                         break;
1336                 case 0x0102:
1337                         /* setup hotkeys */
1338                         sony_call_snc_handle(handle, 0x100, &result);
1339                         break;
1340                 case 0x0105:
1341                 case 0x0148:
1342                         /* touchpad enable/disable */
1343                         result = sony_nc_touchpad_setup(pf_device, handle);
1344                         if (result)
1345                                 pr_err("couldn't set up touchpad control function (%d)\n",
1346                                                 result);
1347                         break;
1348                 case 0x0115:
1349                 case 0x0136:
1350                 case 0x013f:
1351                         result = sony_nc_battery_care_setup(pf_device, handle);
1352                         if (result)
1353                                 pr_err("couldn't set up battery care function (%d)\n",
1354                                                 result);
1355                         break;
1356                 case 0x0119:
1357                 case 0x015D:
1358                         result = sony_nc_lid_resume_setup(pf_device, handle);
1359                         if (result)
1360                                 pr_err("couldn't set up lid resume function (%d)\n",
1361                                                 result);
1362                         break;
1363                 case 0x0122:
1364                         result = sony_nc_thermal_setup(pf_device);
1365                         if (result)
1366                                 pr_err("couldn't set up thermal profile function (%d)\n",
1367                                                 result);
1368                         break;
1369                 case 0x0128:
1370                 case 0x0146:
1371                 case 0x015B:
1372                         result = sony_nc_gfx_switch_setup(pf_device, handle);
1373                         if (result)
1374                                 pr_err("couldn't set up GFX Switch status (%d)\n",
1375                                                 result);
1376                         break;
1377                 case 0x0131:
1378                         result = sony_nc_highspeed_charging_setup(pf_device);
1379                         if (result)
1380                                 pr_err("couldn't set up high speed charging function (%d)\n",
1381                                        result);
1382                         break;
1383                 case 0x0124:
1384                 case 0x0135:
1385                         result = sony_nc_rfkill_setup(device, handle);
1386                         if (result)
1387                                 pr_err("couldn't set up rfkill support (%d)\n",
1388                                                 result);
1389                         break;
1390                 case 0x0137:
1391                 case 0x0143:
1392                 case 0x014b:
1393                 case 0x014c:
1394                 case 0x0163:
1395                         result = sony_nc_kbd_backlight_setup(pf_device, handle);
1396                         if (result)
1397                                 pr_err("couldn't set up keyboard backlight function (%d)\n",
1398                                                 result);
1399                         break;
1400                 case 0x0121:
1401                         result = sony_nc_lowbatt_setup(pf_device);
1402                         if (result)
1403                                 pr_err("couldn't set up low battery function (%d)\n",
1404                                        result);
1405                         break;
1406                 case 0x0149:
1407                         result = sony_nc_fanspeed_setup(pf_device);
1408                         if (result)
1409                                 pr_err("couldn't set up fan speed function (%d)\n",
1410                                        result);
1411                         break;
1412                 case 0x0155:
1413                         result = sony_nc_usb_charge_setup(pf_device);
1414                         if (result)
1415                                 pr_err("couldn't set up USB charge support (%d)\n",
1416                                                 result);
1417                         break;
1418                 case 0x011D:
1419                         result = sony_nc_panelid_setup(pf_device);
1420                         if (result)
1421                                 pr_err("couldn't set up panel ID function (%d)\n",
1422                                        result);
1423                         break;
1424                 default:
1425                         continue;
1426                 }
1427         }
1428
1429         /* Enable all events */
1430         arg = 0x10;
1431         if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1432                 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1433                                 &result);
1434 }
1435
1436 static void sony_nc_function_cleanup(struct platform_device *pd)
1437 {
1438         unsigned int i, result, bitmask, handle;
1439
1440         /* get enabled events and disable them */
1441         sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
1442         sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
1443
1444         /* cleanup handles here */
1445         for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1446
1447                 handle = handles->cap[i];
1448
1449                 if (!handle)
1450                         continue;
1451
1452                 switch (handle) {
1453                 case 0x0105:
1454                 case 0x0148:
1455                         sony_nc_touchpad_cleanup(pd);
1456                         break;
1457                 case 0x0115:
1458                 case 0x0136:
1459                 case 0x013f:
1460                         sony_nc_battery_care_cleanup(pd);
1461                         break;
1462                 case 0x0119:
1463                 case 0x015D:
1464                         sony_nc_lid_resume_cleanup(pd);
1465                         break;
1466                 case 0x0122:
1467                         sony_nc_thermal_cleanup(pd);
1468                         break;
1469                 case 0x0128:
1470                 case 0x0146:
1471                 case 0x015B:
1472                         sony_nc_gfx_switch_cleanup(pd);
1473                         break;
1474                 case 0x0131:
1475                         sony_nc_highspeed_charging_cleanup(pd);
1476                         break;
1477                 case 0x0124:
1478                 case 0x0135:
1479                         sony_nc_rfkill_cleanup();
1480                         break;
1481                 case 0x0137:
1482                 case 0x0143:
1483                 case 0x014b:
1484                 case 0x014c:
1485                 case 0x0163:
1486                         sony_nc_kbd_backlight_cleanup(pd, handle);
1487                         break;
1488                 case 0x0121:
1489                         sony_nc_lowbatt_cleanup(pd);
1490                         break;
1491                 case 0x0149:
1492                         sony_nc_fanspeed_cleanup(pd);
1493                         break;
1494                 case 0x0155:
1495                         sony_nc_usb_charge_cleanup(pd);
1496                         break;
1497                 case 0x011D:
1498                         sony_nc_panelid_cleanup(pd);
1499                         break;
1500                 default:
1501                         continue;
1502                 }
1503         }
1504
1505         /* finally cleanup the handles list */
1506         sony_nc_handles_cleanup(pd);
1507 }
1508
1509 #ifdef CONFIG_PM_SLEEP
1510 static void sony_nc_function_resume(void)
1511 {
1512         unsigned int i, result, bitmask, arg;
1513
1514         dprintk("Resuming SNC device\n");
1515
1516         for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
1517                 unsigned int handle = handles->cap[i];
1518
1519                 if (!handle)
1520                         continue;
1521
1522                 switch (handle) {
1523                 case 0x0100:
1524                 case 0x0101:
1525                 case 0x0127:
1526                         /* re-enable hotkeys */
1527                         sony_call_snc_handle(handle, 0, &result);
1528                         break;
1529                 case 0x0102:
1530                         /* re-enable hotkeys */
1531                         sony_call_snc_handle(handle, 0x100, &result);
1532                         break;
1533                 case 0x0122:
1534                         sony_nc_thermal_resume();
1535                         break;
1536                 case 0x0124:
1537                 case 0x0135:
1538                         sony_nc_rfkill_update();
1539                         break;
1540                 default:
1541                         continue;
1542                 }
1543         }
1544
1545         /* Enable all events */
1546         arg = 0x10;
1547         if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
1548                 sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
1549                                 &result);
1550 }
1551
1552 static int sony_nc_resume(struct device *dev)
1553 {
1554         struct sony_nc_value *item;
1555
1556         for (item = sony_nc_values; item->name; item++) {
1557                 int ret;
1558
1559                 if (!item->valid)
1560                         continue;
1561                 ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
1562                                        &item->value, NULL);
1563                 if (ret < 0) {
1564                         pr_err("%s: %d\n", __func__, ret);
1565                         break;
1566                 }
1567         }
1568
1569         if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
1570                 int arg = 1;
1571                 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
1572                         dprintk("ECON Method failed\n");
1573         }
1574
1575         if (acpi_has_method(sony_nc_acpi_handle, "SN00"))
1576                 sony_nc_function_resume();
1577
1578         return 0;
1579 }
1580 #endif
1581
1582 static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
1583
1584 static void sony_nc_rfkill_cleanup(void)
1585 {
1586         int i;
1587
1588         for (i = 0; i < N_SONY_RFKILL; i++) {
1589                 if (sony_rfkill_devices[i]) {
1590                         rfkill_unregister(sony_rfkill_devices[i]);
1591                         rfkill_destroy(sony_rfkill_devices[i]);
1592                 }
1593         }
1594 }
1595
1596 static int sony_nc_rfkill_set(void *data, bool blocked)
1597 {
1598         int result;
1599         int argument = sony_rfkill_address[(long) data] + 0x100;
1600
1601         if (!blocked)
1602                 argument |= 0x070000;
1603
1604         return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1605 }
1606
1607 static const struct rfkill_ops sony_rfkill_ops = {
1608         .set_block = sony_nc_rfkill_set,
1609 };
1610
1611 static int sony_nc_setup_rfkill(struct acpi_device *device,
1612                                 enum sony_nc_rfkill nc_type)
1613 {
1614         int err = 0;
1615         struct rfkill *rfk;
1616         enum rfkill_type type;
1617         const char *name;
1618         int result;
1619         bool hwblock, swblock;
1620
1621         switch (nc_type) {
1622         case SONY_WIFI:
1623                 type = RFKILL_TYPE_WLAN;
1624                 name = "sony-wifi";
1625                 break;
1626         case SONY_BLUETOOTH:
1627                 type = RFKILL_TYPE_BLUETOOTH;
1628                 name = "sony-bluetooth";
1629                 break;
1630         case SONY_WWAN:
1631                 type = RFKILL_TYPE_WWAN;
1632                 name = "sony-wwan";
1633                 break;
1634         case SONY_WIMAX:
1635                 type = RFKILL_TYPE_WIMAX;
1636                 name = "sony-wimax";
1637                 break;
1638         default:
1639                 return -EINVAL;
1640         }
1641
1642         rfk = rfkill_alloc(name, &device->dev, type,
1643                            &sony_rfkill_ops, (void *)nc_type);
1644         if (!rfk)
1645                 return -ENOMEM;
1646
1647         if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
1648                 rfkill_destroy(rfk);
1649                 return -1;
1650         }
1651         hwblock = !(result & 0x1);
1652
1653         if (sony_call_snc_handle(sony_rfkill_handle,
1654                                 sony_rfkill_address[nc_type],
1655                                 &result) < 0) {
1656                 rfkill_destroy(rfk);
1657                 return -1;
1658         }
1659         swblock = !(result & 0x2);
1660
1661         rfkill_init_sw_state(rfk, swblock);
1662         rfkill_set_hw_state(rfk, hwblock);
1663
1664         err = rfkill_register(rfk);
1665         if (err) {
1666                 rfkill_destroy(rfk);
1667                 return err;
1668         }
1669         sony_rfkill_devices[nc_type] = rfk;
1670         return err;
1671 }
1672
1673 static void sony_nc_rfkill_update(void)
1674 {
1675         enum sony_nc_rfkill i;
1676         int result;
1677         bool hwblock;
1678
1679         sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1680         hwblock = !(result & 0x1);
1681
1682         for (i = 0; i < N_SONY_RFKILL; i++) {
1683                 int argument = sony_rfkill_address[i];
1684
1685                 if (!sony_rfkill_devices[i])
1686                         continue;
1687
1688                 if (hwblock) {
1689                         if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1690                                 /* we already know we're blocked */
1691                         }
1692                         continue;
1693                 }
1694
1695                 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1696                 rfkill_set_states(sony_rfkill_devices[i],
1697                                   !(result & 0x2), false);
1698         }
1699 }
1700
1701 static int sony_nc_rfkill_setup(struct acpi_device *device,
1702                 unsigned int handle)
1703 {
1704         u64 offset;
1705         int i;
1706         unsigned char buffer[32] = { 0 };
1707
1708         offset = sony_find_snc_handle(handle);
1709         sony_rfkill_handle = handle;
1710
1711         i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
1712                         32);
1713         if (i < 0)
1714                 return i;
1715
1716         /* The buffer is filled with magic numbers describing the devices
1717          * available, 0xff terminates the enumeration.
1718          * Known codes:
1719          *      0x00 WLAN
1720          *      0x10 BLUETOOTH
1721          *      0x20 WWAN GPRS-EDGE
1722          *      0x21 WWAN HSDPA
1723          *      0x22 WWAN EV-DO
1724          *      0x23 WWAN GPS
1725          *      0x25 Gobi WWAN no GPS
1726          *      0x26 Gobi WWAN + GPS
1727          *      0x28 Gobi WWAN no GPS
1728          *      0x29 Gobi WWAN + GPS
1729          *      0x30 WIMAX
1730          *      0x50 Gobi WWAN no GPS
1731          *      0x51 Gobi WWAN + GPS
1732          *      0x70 no SIM card slot
1733          *      0x71 SIM card slot
1734          */
1735         for (i = 0; i < ARRAY_SIZE(buffer); i++) {
1736
1737                 if (buffer[i] == 0xff)
1738                         break;
1739
1740                 dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
1741
1742                 if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
1743                         sony_nc_setup_rfkill(device, SONY_WIFI);
1744
1745                 if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1746                         sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1747
1748                 if (((0xf0 & buffer[i]) == 0x20 ||
1749                                         (0xf0 & buffer[i]) == 0x50) &&
1750                                 !sony_rfkill_devices[SONY_WWAN])
1751                         sony_nc_setup_rfkill(device, SONY_WWAN);
1752
1753                 if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1754                         sony_nc_setup_rfkill(device, SONY_WIMAX);
1755         }
1756         return 0;
1757 }
1758
1759 /* Keyboard backlight feature */
1760 struct kbd_backlight {
1761         unsigned int handle;
1762         unsigned int base;
1763         unsigned int mode;
1764         unsigned int timeout;
1765         struct device_attribute mode_attr;
1766         struct device_attribute timeout_attr;
1767 };
1768
1769 static struct kbd_backlight *kbdbl_ctl;
1770
1771 static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1772 {
1773         int result;
1774
1775         if (value > 1)
1776                 return -EINVAL;
1777
1778         if (sony_call_snc_handle(kbdbl_ctl->handle,
1779                                 (value << 0x10) | (kbdbl_ctl->base), &result))
1780                 return -EIO;
1781
1782         /* Try to turn the light on/off immediately */
1783         sony_call_snc_handle(kbdbl_ctl->handle,
1784                         (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
1785
1786         kbdbl_ctl->mode = value;
1787
1788         return 0;
1789 }
1790
1791 static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1792                 struct device_attribute *attr,
1793                 const char *buffer, size_t count)
1794 {
1795         int ret = 0;
1796         unsigned long value;
1797
1798         if (count > 31)
1799                 return -EINVAL;
1800
1801         if (kstrtoul(buffer, 10, &value))
1802                 return -EINVAL;
1803
1804         ret = __sony_nc_kbd_backlight_mode_set(value);
1805         if (ret < 0)
1806                 return ret;
1807
1808         return count;
1809 }
1810
1811 static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1812                 struct device_attribute *attr, char *buffer)
1813 {
1814         ssize_t count = 0;
1815         count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
1816         return count;
1817 }
1818
1819 static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1820 {
1821         int result;
1822
1823         if (value > 3)
1824                 return -EINVAL;
1825
1826         if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
1827                                 (kbdbl_ctl->base + 0x200), &result))
1828                 return -EIO;
1829
1830         kbdbl_ctl->timeout = value;
1831
1832         return 0;
1833 }
1834
1835 static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1836                 struct device_attribute *attr,
1837                 const char *buffer, size_t count)
1838 {
1839         int ret = 0;
1840         unsigned long value;
1841
1842         if (count > 31)
1843                 return -EINVAL;
1844
1845         if (kstrtoul(buffer, 10, &value))
1846                 return -EINVAL;
1847
1848         ret = __sony_nc_kbd_backlight_timeout_set(value);
1849         if (ret < 0)
1850                 return ret;
1851
1852         return count;
1853 }
1854
1855 static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1856                 struct device_attribute *attr, char *buffer)
1857 {
1858         ssize_t count = 0;
1859         count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
1860         return count;
1861 }
1862
1863 static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
1864                 unsigned int handle)
1865 {
1866         int result;
1867         int ret = 0;
1868
1869         if (kbdbl_ctl) {
1870                 pr_warn("handle 0x%.4x: keyboard backlight setup already done for 0x%.4x\n",
1871                                 handle, kbdbl_ctl->handle);
1872                 return -EBUSY;
1873         }
1874
1875         /* verify the kbd backlight presence, these handles are not used for
1876          * keyboard backlight only
1877          */
1878         ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
1879                         &result);
1880         if (ret)
1881                 return ret;
1882
1883         if ((handle == 0x0137 && !(result & 0x02)) ||
1884                         !(result & 0x01)) {
1885                 dprintk("no backlight keyboard found\n");
1886                 return 0;
1887         }
1888
1889         kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
1890         if (!kbdbl_ctl)
1891                 return -ENOMEM;
1892
1893         kbdbl_ctl->mode = kbd_backlight;
1894         kbdbl_ctl->timeout = kbd_backlight_timeout;
1895         kbdbl_ctl->handle = handle;
1896         if (handle == 0x0137)
1897                 kbdbl_ctl->base = 0x0C00;
1898         else
1899                 kbdbl_ctl->base = 0x4000;
1900
1901         sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
1902         kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
1903         kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1904         kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1905         kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1906
1907         sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
1908         kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
1909         kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1910         kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1911         kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1912
1913         ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
1914         if (ret)
1915                 goto outkzalloc;
1916
1917         ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1918         if (ret)
1919                 goto outmode;
1920
1921         __sony_nc_kbd_backlight_mode_set(kbdbl_ctl->mode);
1922         __sony_nc_kbd_backlight_timeout_set(kbdbl_ctl->timeout);
1923
1924         return 0;
1925
1926 outmode:
1927         device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1928 outkzalloc:
1929         kfree(kbdbl_ctl);
1930         kbdbl_ctl = NULL;
1931         return ret;
1932 }
1933
1934 static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
1935                 unsigned int handle)
1936 {
1937         if (kbdbl_ctl && handle == kbdbl_ctl->handle) {
1938                 device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
1939                 device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
1940                 kfree(kbdbl_ctl);
1941                 kbdbl_ctl = NULL;
1942         }
1943 }
1944
1945 struct battery_care_control {
1946         struct device_attribute attrs[2];
1947         unsigned int handle;
1948 };
1949 static struct battery_care_control *bcare_ctl;
1950
1951 static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
1952                 struct device_attribute *attr,
1953                 const char *buffer, size_t count)
1954 {
1955         unsigned int result, cmd;
1956         unsigned long value;
1957
1958         if (count > 31)
1959                 return -EINVAL;
1960
1961         if (kstrtoul(buffer, 10, &value))
1962                 return -EINVAL;
1963
1964         /*  limit values (2 bits):
1965          *  00 - none
1966          *  01 - 80%
1967          *  10 - 50%
1968          *  11 - 100%
1969          *
1970          *  bit 0: 0 disable BCL, 1 enable BCL
1971          *  bit 1: 1 tell to store the battery limit (see bits 6,7) too
1972          *  bits 2,3: reserved
1973          *  bits 4,5: store the limit into the EC
1974          *  bits 6,7: store the limit into the battery
1975          */
1976         cmd = 0;
1977
1978         if (value > 0) {
1979                 if (value <= 50)
1980                         cmd = 0x20;
1981
1982                 else if (value <= 80)
1983                         cmd = 0x10;
1984
1985                 else if (value <= 100)
1986                         cmd = 0x30;
1987
1988                 else
1989                         return -EINVAL;
1990
1991                 /*
1992                  * handle 0x0115 should allow storing on battery too;
1993                  * handle 0x0136 same as 0x0115 + health status;
1994                  * handle 0x013f, same as 0x0136 but no storing on the battery
1995                  */
1996                 if (bcare_ctl->handle != 0x013f)
1997                         cmd = cmd | (cmd << 2);
1998
1999                 cmd = (cmd | 0x1) << 0x10;
2000         }
2001
2002         if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
2003                 return -EIO;
2004
2005         return count;
2006 }
2007
2008 static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
2009                 struct device_attribute *attr, char *buffer)
2010 {
2011         unsigned int result, status;
2012
2013         if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
2014                 return -EIO;
2015
2016         status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
2017         switch (status) {
2018         case 1:
2019                 status = 80;
2020                 break;
2021         case 2:
2022                 status = 50;
2023                 break;
2024         case 3:
2025                 status = 100;
2026                 break;
2027         default:
2028                 status = 0;
2029                 break;
2030         }
2031
2032         return snprintf(buffer, PAGE_SIZE, "%d\n", status);
2033 }
2034
2035 static ssize_t sony_nc_battery_care_health_show(struct device *dev,
2036                 struct device_attribute *attr, char *buffer)
2037 {
2038         ssize_t count = 0;
2039         unsigned int health;
2040
2041         if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
2042                 return -EIO;
2043
2044         count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
2045
2046         return count;
2047 }
2048
2049 static int sony_nc_battery_care_setup(struct platform_device *pd,
2050                 unsigned int handle)
2051 {
2052         int ret = 0;
2053
2054         bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
2055         if (!bcare_ctl)
2056                 return -ENOMEM;
2057
2058         bcare_ctl->handle = handle;
2059
2060         sysfs_attr_init(&bcare_ctl->attrs[0].attr);
2061         bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
2062         bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
2063         bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
2064         bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
2065
2066         ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
2067         if (ret)
2068                 goto outkzalloc;
2069
2070         /* 0x0115 is for models with no health reporting capability */
2071         if (handle == 0x0115)
2072                 return 0;
2073
2074         sysfs_attr_init(&bcare_ctl->attrs[1].attr);
2075         bcare_ctl->attrs[1].attr.name = "battery_care_health";
2076         bcare_ctl->attrs[1].attr.mode = S_IRUGO;
2077         bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
2078
2079         ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
2080         if (ret)
2081                 goto outlimiter;
2082
2083         return 0;
2084
2085 outlimiter:
2086         device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2087
2088 outkzalloc:
2089         kfree(bcare_ctl);
2090         bcare_ctl = NULL;
2091
2092         return ret;
2093 }
2094
2095 static void sony_nc_battery_care_cleanup(struct platform_device *pd)
2096 {
2097         if (bcare_ctl) {
2098                 device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
2099                 if (bcare_ctl->handle != 0x0115)
2100                         device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
2101
2102                 kfree(bcare_ctl);
2103                 bcare_ctl = NULL;
2104         }
2105 }
2106
2107 struct snc_thermal_ctrl {
2108         unsigned int mode;
2109         unsigned int profiles;
2110         struct device_attribute mode_attr;
2111         struct device_attribute profiles_attr;
2112 };
2113 static struct snc_thermal_ctrl *th_handle;
2114
2115 #define THM_PROFILE_MAX 3
2116 static const char * const snc_thermal_profiles[] = {
2117         "balanced",
2118         "silent",
2119         "performance"
2120 };
2121
2122 static int sony_nc_thermal_mode_set(unsigned short mode)
2123 {
2124         unsigned int result;
2125
2126         /* the thermal profile seems to be a two bit bitmask:
2127          * lsb -> silent
2128          * msb -> performance
2129          * no bit set is the normal operation and is always valid
2130          * Some vaio models only have "balanced" and "performance"
2131          */
2132         if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
2133                 return -EINVAL;
2134
2135         if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
2136                 return -EIO;
2137
2138         th_handle->mode = mode;
2139
2140         return 0;
2141 }
2142
2143 static int sony_nc_thermal_mode_get(void)
2144 {
2145         unsigned int result;
2146
2147         if (sony_call_snc_handle(0x0122, 0x0100, &result))
2148                 return -EIO;
2149
2150         return result & 0xff;
2151 }
2152
2153 static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
2154                 struct device_attribute *attr, char *buffer)
2155 {
2156         short cnt;
2157         size_t idx = 0;
2158
2159         for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
2160                 if (!cnt || (th_handle->profiles & cnt))
2161                         idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
2162                                         snc_thermal_profiles[cnt]);
2163         }
2164         idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
2165
2166         return idx;
2167 }
2168
2169 static ssize_t sony_nc_thermal_mode_store(struct device *dev,
2170                 struct device_attribute *attr,
2171                 const char *buffer, size_t count)
2172 {
2173         unsigned short cmd;
2174         size_t len = count;
2175
2176         if (count == 0)
2177                 return -EINVAL;
2178
2179         /* skip the newline if present */
2180         if (buffer[len - 1] == '\n')
2181                 len--;
2182
2183         for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
2184                 if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
2185                         break;
2186
2187         if (sony_nc_thermal_mode_set(cmd))
2188                 return -EIO;
2189
2190         return count;
2191 }
2192
2193 static ssize_t sony_nc_thermal_mode_show(struct device *dev,
2194                 struct device_attribute *attr, char *buffer)
2195 {
2196         ssize_t count = 0;
2197         int mode = sony_nc_thermal_mode_get();
2198
2199         if (mode < 0)
2200                 return mode;
2201
2202         count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
2203
2204         return count;
2205 }
2206
2207 static int sony_nc_thermal_setup(struct platform_device *pd)
2208 {
2209         int ret = 0;
2210         th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
2211         if (!th_handle)
2212                 return -ENOMEM;
2213
2214         ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
2215         if (ret) {
2216                 pr_warn("couldn't to read the thermal profiles\n");
2217                 goto outkzalloc;
2218         }
2219
2220         ret = sony_nc_thermal_mode_get();
2221         if (ret < 0) {
2222                 pr_warn("couldn't to read the current thermal profile");
2223                 goto outkzalloc;
2224         }
2225         th_handle->mode = ret;
2226
2227         sysfs_attr_init(&th_handle->profiles_attr.attr);
2228         th_handle->profiles_attr.attr.name = "thermal_profiles";
2229         th_handle->profiles_attr.attr.mode = S_IRUGO;
2230         th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
2231
2232         sysfs_attr_init(&th_handle->mode_attr.attr);
2233         th_handle->mode_attr.attr.name = "thermal_control";
2234         th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
2235         th_handle->mode_attr.show = sony_nc_thermal_mode_show;
2236         th_handle->mode_attr.store = sony_nc_thermal_mode_store;
2237
2238         ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
2239         if (ret)
2240                 goto outkzalloc;
2241
2242         ret = device_create_file(&pd->dev, &th_handle->mode_attr);
2243         if (ret)
2244                 goto outprofiles;
2245
2246         return 0;
2247
2248 outprofiles:
2249         device_remove_file(&pd->dev, &th_handle->profiles_attr);
2250 outkzalloc:
2251         kfree(th_handle);
2252         th_handle = NULL;
2253         return ret;
2254 }
2255
2256 static void sony_nc_thermal_cleanup(struct platform_device *pd)
2257 {
2258         if (th_handle) {
2259                 device_remove_file(&pd->dev, &th_handle->profiles_attr);
2260                 device_remove_file(&pd->dev, &th_handle->mode_attr);
2261                 kfree(th_handle);
2262                 th_handle = NULL;
2263         }
2264 }
2265
2266 #ifdef CONFIG_PM_SLEEP
2267 static void sony_nc_thermal_resume(void)
2268 {
2269         unsigned int status = sony_nc_thermal_mode_get();
2270
2271         if (status != th_handle->mode)
2272                 sony_nc_thermal_mode_set(th_handle->mode);
2273 }
2274 #endif
2275
2276 /* resume on LID open */
2277 #define LID_RESUME_S5   0
2278 #define LID_RESUME_S4   1
2279 #define LID_RESUME_S3   2
2280 #define LID_RESUME_MAX  3
2281 struct snc_lid_resume_control {
2282         struct device_attribute attrs[LID_RESUME_MAX];
2283         unsigned int status;
2284         int handle;
2285 };
2286 static struct snc_lid_resume_control *lid_ctl;
2287
2288 static ssize_t sony_nc_lid_resume_store(struct device *dev,
2289                                         struct device_attribute *attr,
2290                                         const char *buffer, size_t count)
2291 {
2292         unsigned int result;
2293         unsigned long value;
2294         unsigned int pos = LID_RESUME_S5;
2295         if (count > 31)
2296                 return -EINVAL;
2297
2298         if (kstrtoul(buffer, 10, &value) || value > 1)
2299                 return -EINVAL;
2300
2301         /* the value we have to write to SNC is a bitmask:
2302          * +--------------+
2303          * | S3 | S4 | S5 |
2304          * +--------------+
2305          *   2    1    0
2306          */
2307         while (pos < LID_RESUME_MAX) {
2308                 if (&lid_ctl->attrs[pos].attr == &attr->attr)
2309                         break;
2310                 pos++;
2311         }
2312         if (pos == LID_RESUME_MAX)
2313                 return -EINVAL;
2314
2315         if (value)
2316                 value = lid_ctl->status | (1 << pos);
2317         else
2318                 value = lid_ctl->status & ~(1 << pos);
2319
2320         if (sony_call_snc_handle(lid_ctl->handle, value << 0x10 | 0x0100,
2321                                 &result))
2322                 return -EIO;
2323
2324         lid_ctl->status = value;
2325
2326         return count;
2327 }
2328
2329 static ssize_t sony_nc_lid_resume_show(struct device *dev,
2330                                         struct device_attribute *attr,
2331                                         char *buffer)
2332 {
2333         unsigned int pos = LID_RESUME_S5;
2334
2335         while (pos < LID_RESUME_MAX) {
2336                 if (&lid_ctl->attrs[pos].attr == &attr->attr)
2337                         return snprintf(buffer, PAGE_SIZE, "%d\n",
2338                                         (lid_ctl->status >> pos) & 0x01);
2339                 pos++;
2340         }
2341         return -EINVAL;
2342 }
2343
2344 static int sony_nc_lid_resume_setup(struct platform_device *pd,
2345                                         unsigned int handle)
2346 {
2347         unsigned int result;
2348         int i;
2349
2350         if (sony_call_snc_handle(handle, 0x0000, &result))
2351                 return -EIO;
2352
2353         lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
2354         if (!lid_ctl)
2355                 return -ENOMEM;
2356
2357         lid_ctl->status = result & 0x7;
2358         lid_ctl->handle = handle;
2359
2360         sysfs_attr_init(&lid_ctl->attrs[0].attr);
2361         lid_ctl->attrs[LID_RESUME_S5].attr.name = "lid_resume_S5";
2362         lid_ctl->attrs[LID_RESUME_S5].attr.mode = S_IRUGO | S_IWUSR;
2363         lid_ctl->attrs[LID_RESUME_S5].show = sony_nc_lid_resume_show;
2364         lid_ctl->attrs[LID_RESUME_S5].store = sony_nc_lid_resume_store;
2365
2366         if (handle == 0x0119) {
2367                 sysfs_attr_init(&lid_ctl->attrs[1].attr);
2368                 lid_ctl->attrs[LID_RESUME_S4].attr.name = "lid_resume_S4";
2369                 lid_ctl->attrs[LID_RESUME_S4].attr.mode = S_IRUGO | S_IWUSR;
2370                 lid_ctl->attrs[LID_RESUME_S4].show = sony_nc_lid_resume_show;
2371                 lid_ctl->attrs[LID_RESUME_S4].store = sony_nc_lid_resume_store;
2372
2373                 sysfs_attr_init(&lid_ctl->attrs[2].attr);
2374                 lid_ctl->attrs[LID_RESUME_S3].attr.name = "lid_resume_S3";
2375                 lid_ctl->attrs[LID_RESUME_S3].attr.mode = S_IRUGO | S_IWUSR;
2376                 lid_ctl->attrs[LID_RESUME_S3].show = sony_nc_lid_resume_show;
2377                 lid_ctl->attrs[LID_RESUME_S3].store = sony_nc_lid_resume_store;
2378         }
2379         for (i = 0; i < LID_RESUME_MAX &&
2380                         lid_ctl->attrs[LID_RESUME_S3].attr.name; i++) {
2381                 result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
2382                 if (result)
2383                         goto liderror;
2384         }
2385
2386         return 0;
2387
2388 liderror:
2389         for (i--; i >= 0; i--)
2390                 device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2391
2392         kfree(lid_ctl);
2393         lid_ctl = NULL;
2394
2395         return result;
2396 }
2397
2398 static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
2399 {
2400         int i;
2401
2402         if (lid_ctl) {
2403                 for (i = 0; i < LID_RESUME_MAX; i++) {
2404                         if (!lid_ctl->attrs[i].attr.name)
2405                                 break;
2406
2407                         device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
2408                 }
2409
2410                 kfree(lid_ctl);
2411                 lid_ctl = NULL;
2412         }
2413 }
2414
2415 /* GFX Switch position */
2416 enum gfx_switch {
2417         SPEED,
2418         STAMINA,
2419         AUTO
2420 };
2421 struct snc_gfx_switch_control {
2422         struct device_attribute attr;
2423         unsigned int handle;
2424 };
2425 static struct snc_gfx_switch_control *gfxs_ctl;
2426
2427 /* returns 0 for speed, 1 for stamina */
2428 static int __sony_nc_gfx_switch_status_get(void)
2429 {
2430         unsigned int result;
2431
2432         if (sony_call_snc_handle(gfxs_ctl->handle,
2433                                 gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
2434                                 &result))
2435                 return -EIO;
2436
2437         switch (gfxs_ctl->handle) {
2438         case 0x0146:
2439                 /* 1: discrete GFX (speed)
2440                  * 0: integrated GFX (stamina)
2441                  */
2442                 return result & 0x1 ? SPEED : STAMINA;
2443                 break;
2444         case 0x015B:
2445                 /* 0: discrete GFX (speed)
2446                  * 1: integrated GFX (stamina)
2447                  */
2448                 return result & 0x1 ? STAMINA : SPEED;
2449                 break;
2450         case 0x0128:
2451                 /* it's a more elaborated bitmask, for now:
2452                  * 2: integrated GFX (stamina)
2453                  * 0: discrete GFX (speed)
2454                  */
2455                 dprintk("GFX Status: 0x%x\n", result);
2456                 return result & 0x80 ? AUTO :
2457                         result & 0x02 ? STAMINA : SPEED;
2458                 break;
2459         }
2460         return -EINVAL;
2461 }
2462
2463 static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
2464                                        struct device_attribute *attr,
2465                                        char *buffer)
2466 {
2467         int pos = __sony_nc_gfx_switch_status_get();
2468
2469         if (pos < 0)
2470                 return pos;
2471
2472         return snprintf(buffer, PAGE_SIZE, "%s\n",
2473                                         pos == SPEED ? "speed" :
2474                                         pos == STAMINA ? "stamina" :
2475                                         pos == AUTO ? "auto" : "unknown");
2476 }
2477
2478 static int sony_nc_gfx_switch_setup(struct platform_device *pd,
2479                 unsigned int handle)
2480 {
2481         unsigned int result;
2482
2483         gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
2484         if (!gfxs_ctl)
2485                 return -ENOMEM;
2486
2487         gfxs_ctl->handle = handle;
2488
2489         sysfs_attr_init(&gfxs_ctl->attr.attr);
2490         gfxs_ctl->attr.attr.name = "gfx_switch_status";
2491         gfxs_ctl->attr.attr.mode = S_IRUGO;
2492         gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
2493
2494         result = device_create_file(&pd->dev, &gfxs_ctl->attr);
2495         if (result)
2496                 goto gfxerror;
2497
2498         return 0;
2499
2500 gfxerror:
2501         kfree(gfxs_ctl);
2502         gfxs_ctl = NULL;
2503
2504         return result;
2505 }
2506
2507 static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
2508 {
2509         if (gfxs_ctl) {
2510                 device_remove_file(&pd->dev, &gfxs_ctl->attr);
2511
2512                 kfree(gfxs_ctl);
2513                 gfxs_ctl = NULL;
2514         }
2515 }
2516
2517 /* High speed charging function */
2518 static struct device_attribute *hsc_handle;
2519
2520 static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
2521                 struct device_attribute *attr,
2522                 const char *buffer, size_t count)
2523 {
2524         unsigned int result;
2525         unsigned long value;
2526
2527         if (count > 31)
2528                 return -EINVAL;
2529
2530         if (kstrtoul(buffer, 10, &value) || value > 1)
2531                 return -EINVAL;
2532
2533         if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
2534                 return -EIO;
2535
2536         return count;
2537 }
2538
2539 static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
2540                 struct device_attribute *attr, char *buffer)
2541 {
2542         unsigned int result;
2543
2544         if (sony_call_snc_handle(0x0131, 0x0100, &result))
2545                 return -EIO;
2546
2547         return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2548 }
2549
2550 static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
2551 {
2552         unsigned int result;
2553
2554         if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
2555                 /* some models advertise the handle but have no implementation
2556                  * for it
2557                  */
2558                 pr_info("No High Speed Charging capability found\n");
2559                 return 0;
2560         }
2561
2562         hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2563         if (!hsc_handle)
2564                 return -ENOMEM;
2565
2566         sysfs_attr_init(&hsc_handle->attr);
2567         hsc_handle->attr.name = "battery_highspeed_charging";
2568         hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
2569         hsc_handle->show = sony_nc_highspeed_charging_show;
2570         hsc_handle->store = sony_nc_highspeed_charging_store;
2571
2572         result = device_create_file(&pd->dev, hsc_handle);
2573         if (result) {
2574                 kfree(hsc_handle);
2575                 hsc_handle = NULL;
2576                 return result;
2577         }
2578
2579         return 0;
2580 }
2581
2582 static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
2583 {
2584         if (hsc_handle) {
2585                 device_remove_file(&pd->dev, hsc_handle);
2586                 kfree(hsc_handle);
2587                 hsc_handle = NULL;
2588         }
2589 }
2590
2591 /* low battery function */
2592 static struct device_attribute *lowbatt_handle;
2593
2594 static ssize_t sony_nc_lowbatt_store(struct device *dev,
2595                 struct device_attribute *attr,
2596                 const char *buffer, size_t count)
2597 {
2598         unsigned int result;
2599         unsigned long value;
2600
2601         if (count > 31)
2602                 return -EINVAL;
2603
2604         if (kstrtoul(buffer, 10, &value) || value > 1)
2605                 return -EINVAL;
2606
2607         if (sony_call_snc_handle(0x0121, value << 8, &result))
2608                 return -EIO;
2609
2610         return count;
2611 }
2612
2613 static ssize_t sony_nc_lowbatt_show(struct device *dev,
2614                 struct device_attribute *attr, char *buffer)
2615 {
2616         unsigned int result;
2617
2618         if (sony_call_snc_handle(0x0121, 0x0200, &result))
2619                 return -EIO;
2620
2621         return snprintf(buffer, PAGE_SIZE, "%d\n", result & 1);
2622 }
2623
2624 static int sony_nc_lowbatt_setup(struct platform_device *pd)
2625 {
2626         unsigned int result;
2627
2628         lowbatt_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2629         if (!lowbatt_handle)
2630                 return -ENOMEM;
2631
2632         sysfs_attr_init(&lowbatt_handle->attr);
2633         lowbatt_handle->attr.name = "lowbatt_hibernate";
2634         lowbatt_handle->attr.mode = S_IRUGO | S_IWUSR;
2635         lowbatt_handle->show = sony_nc_lowbatt_show;
2636         lowbatt_handle->store = sony_nc_lowbatt_store;
2637
2638         result = device_create_file(&pd->dev, lowbatt_handle);
2639         if (result) {
2640                 kfree(lowbatt_handle);
2641                 lowbatt_handle = NULL;
2642                 return result;
2643         }
2644
2645         return 0;
2646 }
2647
2648 static void sony_nc_lowbatt_cleanup(struct platform_device *pd)
2649 {
2650         if (lowbatt_handle) {
2651                 device_remove_file(&pd->dev, lowbatt_handle);
2652                 kfree(lowbatt_handle);
2653                 lowbatt_handle = NULL;
2654         }
2655 }
2656
2657 /* fan speed function */
2658 static struct device_attribute *fan_handle, *hsf_handle;
2659
2660 static ssize_t sony_nc_hsfan_store(struct device *dev,
2661                 struct device_attribute *attr,
2662                 const char *buffer, size_t count)
2663 {
2664         unsigned int result;
2665         unsigned long value;
2666
2667         if (count > 31)
2668                 return -EINVAL;
2669
2670         if (kstrtoul(buffer, 10, &value) || value > 1)
2671                 return -EINVAL;
2672
2673         if (sony_call_snc_handle(0x0149, value << 0x10 | 0x0200, &result))
2674                 return -EIO;
2675
2676         return count;
2677 }
2678
2679 static ssize_t sony_nc_hsfan_show(struct device *dev,
2680                 struct device_attribute *attr, char *buffer)
2681 {
2682         unsigned int result;
2683
2684         if (sony_call_snc_handle(0x0149, 0x0100, &result))
2685                 return -EIO;
2686
2687         return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2688 }
2689
2690 static ssize_t sony_nc_fanspeed_show(struct device *dev,
2691                 struct device_attribute *attr, char *buffer)
2692 {
2693         unsigned int result;
2694
2695         if (sony_call_snc_handle(0x0149, 0x0300, &result))
2696                 return -EIO;
2697
2698         return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0xff);
2699 }
2700
2701 static int sony_nc_fanspeed_setup(struct platform_device *pd)
2702 {
2703         unsigned int result;
2704
2705         fan_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2706         if (!fan_handle)
2707                 return -ENOMEM;
2708
2709         hsf_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2710         if (!hsf_handle) {
2711                 result = -ENOMEM;
2712                 goto out_hsf_handle_alloc;
2713         }
2714
2715         sysfs_attr_init(&fan_handle->attr);
2716         fan_handle->attr.name = "fanspeed";
2717         fan_handle->attr.mode = S_IRUGO;
2718         fan_handle->show = sony_nc_fanspeed_show;
2719         fan_handle->store = NULL;
2720
2721         sysfs_attr_init(&hsf_handle->attr);
2722         hsf_handle->attr.name = "fan_forced";
2723         hsf_handle->attr.mode = S_IRUGO | S_IWUSR;
2724         hsf_handle->show = sony_nc_hsfan_show;
2725         hsf_handle->store = sony_nc_hsfan_store;
2726
2727         result = device_create_file(&pd->dev, fan_handle);
2728         if (result)
2729                 goto out_fan_handle;
2730
2731         result = device_create_file(&pd->dev, hsf_handle);
2732         if (result)
2733                 goto out_hsf_handle;
2734
2735         return 0;
2736
2737 out_hsf_handle:
2738         device_remove_file(&pd->dev, fan_handle);
2739
2740 out_fan_handle:
2741         kfree(hsf_handle);
2742         hsf_handle = NULL;
2743
2744 out_hsf_handle_alloc:
2745         kfree(fan_handle);
2746         fan_handle = NULL;
2747         return result;
2748 }
2749
2750 static void sony_nc_fanspeed_cleanup(struct platform_device *pd)
2751 {
2752         if (fan_handle) {
2753                 device_remove_file(&pd->dev, fan_handle);
2754                 kfree(fan_handle);
2755                 fan_handle = NULL;
2756         }
2757         if (hsf_handle) {
2758                 device_remove_file(&pd->dev, hsf_handle);
2759                 kfree(hsf_handle);
2760                 hsf_handle = NULL;
2761         }
2762 }
2763
2764 /* USB charge function */
2765 static struct device_attribute *uc_handle;
2766
2767 static ssize_t sony_nc_usb_charge_store(struct device *dev,
2768                 struct device_attribute *attr,
2769                 const char *buffer, size_t count)
2770 {
2771         unsigned int result;
2772         unsigned long value;
2773
2774         if (count > 31)
2775                 return -EINVAL;
2776
2777         if (kstrtoul(buffer, 10, &value) || value > 1)
2778                 return -EINVAL;
2779
2780         if (sony_call_snc_handle(0x0155, value << 0x10 | 0x0100, &result))
2781                 return -EIO;
2782
2783         return count;
2784 }
2785
2786 static ssize_t sony_nc_usb_charge_show(struct device *dev,
2787                 struct device_attribute *attr, char *buffer)
2788 {
2789         unsigned int result;
2790
2791         if (sony_call_snc_handle(0x0155, 0x0000, &result))
2792                 return -EIO;
2793
2794         return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
2795 }
2796
2797 static int sony_nc_usb_charge_setup(struct platform_device *pd)
2798 {
2799         unsigned int result;
2800
2801         if (sony_call_snc_handle(0x0155, 0x0000, &result) || !(result & 0x01)) {
2802                 /* some models advertise the handle but have no implementation
2803                  * for it
2804                  */
2805                 pr_info("No USB Charge capability found\n");
2806                 return 0;
2807         }
2808
2809         uc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2810         if (!uc_handle)
2811                 return -ENOMEM;
2812
2813         sysfs_attr_init(&uc_handle->attr);
2814         uc_handle->attr.name = "usb_charge";
2815         uc_handle->attr.mode = S_IRUGO | S_IWUSR;
2816         uc_handle->show = sony_nc_usb_charge_show;
2817         uc_handle->store = sony_nc_usb_charge_store;
2818
2819         result = device_create_file(&pd->dev, uc_handle);
2820         if (result) {
2821                 kfree(uc_handle);
2822                 uc_handle = NULL;
2823                 return result;
2824         }
2825
2826         return 0;
2827 }
2828
2829 static void sony_nc_usb_charge_cleanup(struct platform_device *pd)
2830 {
2831         if (uc_handle) {
2832                 device_remove_file(&pd->dev, uc_handle);
2833                 kfree(uc_handle);
2834                 uc_handle = NULL;
2835         }
2836 }
2837
2838 /* Panel ID function */
2839 static struct device_attribute *panel_handle;
2840
2841 static ssize_t sony_nc_panelid_show(struct device *dev,
2842                 struct device_attribute *attr, char *buffer)
2843 {
2844         unsigned int result;
2845
2846         if (sony_call_snc_handle(0x011D, 0x0000, &result))
2847                 return -EIO;
2848
2849         return snprintf(buffer, PAGE_SIZE, "%d\n", result);
2850 }
2851
2852 static int sony_nc_panelid_setup(struct platform_device *pd)
2853 {
2854         unsigned int result;
2855
2856         panel_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
2857         if (!panel_handle)
2858                 return -ENOMEM;
2859
2860         sysfs_attr_init(&panel_handle->attr);
2861         panel_handle->attr.name = "panel_id";
2862         panel_handle->attr.mode = S_IRUGO;
2863         panel_handle->show = sony_nc_panelid_show;
2864         panel_handle->store = NULL;
2865
2866         result = device_create_file(&pd->dev, panel_handle);
2867         if (result) {
2868                 kfree(panel_handle);
2869                 panel_handle = NULL;
2870                 return result;
2871         }
2872
2873         return 0;
2874 }
2875
2876 static void sony_nc_panelid_cleanup(struct platform_device *pd)
2877 {
2878         if (panel_handle) {
2879                 device_remove_file(&pd->dev, panel_handle);
2880                 kfree(panel_handle);
2881                 panel_handle = NULL;
2882         }
2883 }
2884
2885 /* Touchpad enable/disable */
2886 struct touchpad_control {
2887         struct device_attribute attr;
2888         int handle;
2889 };
2890 static struct touchpad_control *tp_ctl;
2891
2892 static ssize_t sony_nc_touchpad_store(struct device *dev,
2893                 struct device_attribute *attr, const char *buffer, size_t count)
2894 {
2895         unsigned int result;
2896         unsigned long value;
2897
2898         if (count > 31)
2899                 return -EINVAL;
2900
2901         if (kstrtoul(buffer, 10, &value) || value > 1)
2902                 return -EINVAL;
2903
2904         /* sysfs: 0 disabled, 1 enabled
2905          * EC: 0 enabled, 1 disabled
2906          */
2907         if (sony_call_snc_handle(tp_ctl->handle,
2908                                 (!value << 0x10) | 0x100, &result))
2909                 return -EIO;
2910
2911         return count;
2912 }
2913
2914 static ssize_t sony_nc_touchpad_show(struct device *dev,
2915                 struct device_attribute *attr, char *buffer)
2916 {
2917         unsigned int result;
2918
2919         if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
2920                 return -EINVAL;
2921
2922         return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
2923 }
2924
2925 static int sony_nc_touchpad_setup(struct platform_device *pd,
2926                 unsigned int handle)
2927 {
2928         int ret = 0;
2929
2930         tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
2931         if (!tp_ctl)
2932                 return -ENOMEM;
2933
2934         tp_ctl->handle = handle;
2935
2936         sysfs_attr_init(&tp_ctl->attr.attr);
2937         tp_ctl->attr.attr.name = "touchpad";
2938         tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
2939         tp_ctl->attr.show = sony_nc_touchpad_show;
2940         tp_ctl->attr.store = sony_nc_touchpad_store;
2941
2942         ret = device_create_file(&pd->dev, &tp_ctl->attr);
2943         if (ret) {
2944                 kfree(tp_ctl);
2945                 tp_ctl = NULL;
2946         }
2947
2948         return ret;
2949 }
2950
2951 static void sony_nc_touchpad_cleanup(struct platform_device *pd)
2952 {
2953         if (tp_ctl) {
2954                 device_remove_file(&pd->dev, &tp_ctl->attr);
2955                 kfree(tp_ctl);
2956                 tp_ctl = NULL;
2957         }
2958 }
2959
2960 static void sony_nc_backlight_ng_read_limits(int handle,
2961                 struct sony_backlight_props *props)
2962 {
2963         u64 offset;
2964         int i;
2965         int lvl_table_len = 0;
2966         u8 min = 0xff, max = 0x00;
2967         unsigned char buffer[32] = { 0 };
2968
2969         props->handle = handle;
2970         props->offset = 0;
2971         props->maxlvl = 0xff;
2972
2973         offset = sony_find_snc_handle(handle);
2974
2975         /* try to read the boundaries from ACPI tables, if we fail the above
2976          * defaults should be reasonable
2977          */
2978         i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
2979                         32);
2980         if (i < 0)
2981                 return;
2982
2983         switch (handle) {
2984         case 0x012f:
2985         case 0x0137:
2986                 lvl_table_len = 9;
2987                 break;
2988         case 0x143:
2989         case 0x14b:
2990         case 0x14c:
2991                 lvl_table_len = 16;
2992                 break;
2993         }
2994
2995         /* the buffer lists brightness levels available, brightness levels are
2996          * from position 0 to 8 in the array, other values are used by ALS
2997          * control.
2998          */
2999         for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
3000
3001                 dprintk("Brightness level: %d\n", buffer[i]);
3002
3003                 if (!buffer[i])
3004                         break;
3005
3006                 if (buffer[i] > max)
3007                         max = buffer[i];
3008                 if (buffer[i] < min)
3009                         min = buffer[i];
3010         }
3011         props->offset = min;
3012         props->maxlvl = max;
3013         dprintk("Brightness levels: min=%d max=%d\n", props->offset,
3014                         props->maxlvl);
3015 }
3016
3017 static void sony_nc_backlight_setup(void)
3018 {
3019         int max_brightness = 0;
3020         const struct backlight_ops *ops = NULL;
3021         struct backlight_properties props;
3022
3023         if (sony_find_snc_handle(0x12f) >= 0) {
3024                 ops = &sony_backlight_ng_ops;
3025                 sony_bl_props.cmd_base = 0x0100;
3026                 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
3027                 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3028
3029         } else if (sony_find_snc_handle(0x137) >= 0) {
3030                 ops = &sony_backlight_ng_ops;
3031                 sony_bl_props.cmd_base = 0x0100;
3032                 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
3033                 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3034
3035         } else if (sony_find_snc_handle(0x143) >= 0) {
3036                 ops = &sony_backlight_ng_ops;
3037                 sony_bl_props.cmd_base = 0x3000;
3038                 sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
3039                 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3040
3041         } else if (sony_find_snc_handle(0x14b) >= 0) {
3042                 ops = &sony_backlight_ng_ops;
3043                 sony_bl_props.cmd_base = 0x3000;
3044                 sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
3045                 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3046
3047         } else if (sony_find_snc_handle(0x14c) >= 0) {
3048                 ops = &sony_backlight_ng_ops;
3049                 sony_bl_props.cmd_base = 0x3000;
3050                 sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
3051                 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
3052
3053         } else if (acpi_has_method(sony_nc_acpi_handle, "GBRT")) {
3054                 ops = &sony_backlight_ops;
3055                 max_brightness = SONY_MAX_BRIGHTNESS - 1;
3056
3057         } else
3058                 return;
3059
3060         memset(&props, 0, sizeof(struct backlight_properties));
3061         props.type = BACKLIGHT_PLATFORM;
3062         props.max_brightness = max_brightness;
3063         sony_bl_props.dev = backlight_device_register("sony", NULL,
3064                                                       &sony_bl_props,
3065                                                       ops, &props);
3066
3067         if (IS_ERR(sony_bl_props.dev)) {
3068                 pr_warn("unable to register backlight device\n");
3069                 sony_bl_props.dev = NULL;
3070         } else
3071                 sony_bl_props.dev->props.brightness =
3072                         ops->get_brightness(sony_bl_props.dev);
3073 }
3074
3075 static void sony_nc_backlight_cleanup(void)
3076 {
3077         if (sony_bl_props.dev)
3078                 backlight_device_unregister(sony_bl_props.dev);
3079 }
3080
3081 static int sony_nc_add(struct acpi_device *device)
3082 {
3083         acpi_status status;
3084         int result = 0;
3085         struct sony_nc_value *item;
3086
3087         pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
3088
3089         sony_nc_acpi_device = device;
3090         strcpy(acpi_device_class(device), "sony/hotkey");
3091
3092         sony_nc_acpi_handle = device->handle;
3093
3094         /* read device status */
3095         result = acpi_bus_get_status(device);
3096         /* bail IFF the above call was successful and the device is not present */
3097         if (!result && !device->status.present) {
3098                 dprintk("Device not present\n");
3099                 result = -ENODEV;
3100                 goto outwalk;
3101         }
3102
3103         result = sony_pf_add();
3104         if (result)
3105                 goto outpresent;
3106
3107         if (debug) {
3108                 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
3109                                 sony_nc_acpi_handle, 1, sony_walk_callback,
3110                                 NULL, NULL, NULL);
3111                 if (ACPI_FAILURE(status)) {
3112                         pr_warn("unable to walk acpi resources\n");
3113                         result = -ENODEV;
3114                         goto outpresent;
3115                 }
3116         }
3117
3118         result = sony_laptop_setup_input(device);
3119         if (result) {
3120                 pr_err("Unable to create input devices\n");
3121                 goto outplatform;
3122         }
3123
3124         if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
3125                 int arg = 1;
3126                 if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
3127                         dprintk("ECON Method failed\n");
3128         }
3129
3130         if (acpi_has_method(sony_nc_acpi_handle, "SN00")) {
3131                 dprintk("Doing SNC setup\n");
3132                 /* retrieve the available handles */
3133                 result = sony_nc_handles_setup(sony_pf_device);
3134                 if (!result)
3135                         sony_nc_function_setup(device, sony_pf_device);
3136         }
3137
3138         /* setup input devices and helper fifo */
3139         if (acpi_video_backlight_support()) {
3140                 pr_info("brightness ignored, must be controlled by ACPI video driver\n");
3141         } else {
3142                 sony_nc_backlight_setup();
3143         }
3144
3145         /* create sony_pf sysfs attributes related to the SNC device */
3146         for (item = sony_nc_values; item->name; ++item) {
3147
3148                 if (!debug && item->debug)
3149                         continue;
3150
3151                 /* find the available acpiget as described in the DSDT */
3152                 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
3153                         if (acpi_has_method(sony_nc_acpi_handle,
3154                                                         *item->acpiget)) {
3155                                 dprintk("Found %s getter: %s\n",
3156                                                 item->name, *item->acpiget);
3157                                 item->devattr.attr.mode |= S_IRUGO;
3158                                 break;
3159                         }
3160                 }
3161
3162                 /* find the available acpiset as described in the DSDT */
3163                 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
3164                         if (acpi_has_method(sony_nc_acpi_handle,
3165                                                         *item->acpiset)) {
3166                                 dprintk("Found %s setter: %s\n",
3167                                                 item->name, *item->acpiset);
3168                                 item->devattr.attr.mode |= S_IWUSR;
3169                                 break;
3170                         }
3171                 }
3172
3173                 if (item->devattr.attr.mode != 0) {
3174                         result =
3175                             device_create_file(&sony_pf_device->dev,
3176                                                &item->devattr);
3177                         if (result)
3178                                 goto out_sysfs;
3179                 }
3180         }
3181
3182         return 0;
3183
3184 out_sysfs:
3185         for (item = sony_nc_values; item->name; ++item) {
3186                 device_remove_file(&sony_pf_device->dev, &item->devattr);
3187         }
3188         sony_nc_backlight_cleanup();
3189         sony_nc_function_cleanup(sony_pf_device);
3190         sony_nc_handles_cleanup(sony_pf_device);
3191
3192 outplatform:
3193         sony_laptop_remove_input();
3194
3195 outpresent:
3196         sony_pf_remove();
3197
3198 outwalk:
3199         sony_nc_rfkill_cleanup();
3200         return result;
3201 }
3202
3203 static int sony_nc_remove(struct acpi_device *device)
3204 {
3205         struct sony_nc_value *item;
3206
3207         sony_nc_backlight_cleanup();
3208
3209         sony_nc_acpi_device = NULL;
3210
3211         for (item = sony_nc_values; item->name; ++item) {
3212                 device_remove_file(&sony_pf_device->dev, &item->devattr);
3213         }
3214
3215         sony_nc_function_cleanup(sony_pf_device);
3216         sony_nc_handles_cleanup(sony_pf_device);
3217         sony_pf_remove();
3218         sony_laptop_remove_input();
3219         dprintk(SONY_NC_DRIVER_NAME " removed.\n");
3220
3221         return 0;
3222 }
3223
3224 static const struct acpi_device_id sony_device_ids[] = {
3225         {SONY_NC_HID, 0},
3226         {SONY_PIC_HID, 0},
3227         {"", 0},
3228 };
3229 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
3230
3231 static const struct acpi_device_id sony_nc_device_ids[] = {
3232         {SONY_NC_HID, 0},
3233         {"", 0},
3234 };
3235
3236 static struct acpi_driver sony_nc_driver = {
3237         .name = SONY_NC_DRIVER_NAME,
3238         .class = SONY_NC_CLASS,
3239         .ids = sony_nc_device_ids,
3240         .owner = THIS_MODULE,
3241         .ops = {
3242                 .add = sony_nc_add,
3243                 .remove = sony_nc_remove,
3244                 .notify = sony_nc_notify,
3245                 },
3246         .drv.pm = &sony_nc_pm,
3247 };
3248
3249 /*********** SPIC (SNY6001) Device ***********/
3250
3251 #define SONYPI_DEVICE_TYPE1     0x00000001
3252 #define SONYPI_DEVICE_TYPE2     0x00000002
3253 #define SONYPI_DEVICE_TYPE3     0x00000004
3254
3255 #define SONYPI_TYPE1_OFFSET     0x04
3256 #define SONYPI_TYPE2_OFFSET     0x12
3257 #define SONYPI_TYPE3_OFFSET     0x12
3258
3259 struct sony_pic_ioport {
3260         struct acpi_resource_io io1;
3261         struct acpi_resource_io io2;
3262         struct list_head        list;
3263 };
3264
3265 struct sony_pic_irq {
3266         struct acpi_resource_irq        irq;
3267         struct list_head                list;
3268 };
3269
3270 struct sonypi_eventtypes {
3271         u8                      data;
3272         unsigned long           mask;
3273         struct sonypi_event     *events;
3274 };
3275
3276 struct sony_pic_dev {
3277         struct acpi_device              *acpi_dev;
3278         struct sony_pic_irq             *cur_irq;
3279         struct sony_pic_ioport          *cur_ioport;
3280         struct list_head                interrupts;
3281         struct list_head                ioports;
3282         struct mutex                    lock;
3283         struct sonypi_eventtypes        *event_types;
3284         int                             (*handle_irq)(const u8, const u8);
3285         int                             model;
3286         u16                             evport_offset;
3287         u8                              camera_power;
3288         u8                              bluetooth_power;
3289         u8                              wwan_power;
3290 };
3291
3292 static struct sony_pic_dev spic_dev = {
3293         .interrupts     = LIST_HEAD_INIT(spic_dev.interrupts),
3294         .ioports        = LIST_HEAD_INIT(spic_dev.ioports),
3295 };
3296
3297 static int spic_drv_registered;
3298
3299 /* Event masks */
3300 #define SONYPI_JOGGER_MASK                      0x00000001
3301 #define SONYPI_CAPTURE_MASK                     0x00000002
3302 #define SONYPI_FNKEY_MASK                       0x00000004
3303 #define SONYPI_BLUETOOTH_MASK                   0x00000008
3304 #define SONYPI_PKEY_MASK                        0x00000010
3305 #define SONYPI_BACK_MASK                        0x00000020
3306 #define SONYPI_HELP_MASK                        0x00000040
3307 #define SONYPI_LID_MASK                         0x00000080
3308 #define SONYPI_ZOOM_MASK                        0x00000100
3309 #define SONYPI_THUMBPHRASE_MASK                 0x00000200
3310 #define SONYPI_MEYE_MASK                        0x00000400
3311 #define SONYPI_MEMORYSTICK_MASK                 0x00000800
3312 #define SONYPI_BATTERY_MASK                     0x00001000
3313 #define SONYPI_WIRELESS_MASK                    0x00002000
3314
3315 struct sonypi_event {
3316         u8      data;
3317         u8      event;
3318 };
3319
3320 /* The set of possible button release events */
3321 static struct sonypi_event sonypi_releaseev[] = {
3322         { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
3323         { 0, 0 }
3324 };
3325
3326 /* The set of possible jogger events  */
3327 static struct sonypi_event sonypi_joggerev[] = {
3328         { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
3329         { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
3330         { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
3331         { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
3332         { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
3333         { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
3334         { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
3335         { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
3336         { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
3337         { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
3338         { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
3339         { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
3340         { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
3341         { 0, 0 }
3342 };
3343
3344 /* The set of possible capture button events */
3345 static struct sonypi_event sonypi_captureev[] = {
3346         { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
3347         { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
3348         { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
3349         { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
3350         { 0, 0 }
3351 };
3352
3353 /* The set of possible fnkeys events */
3354 static struct sonypi_event sonypi_fnkeyev[] = {
3355         { 0x10, SONYPI_EVENT_FNKEY_ESC },
3356         { 0x11, SONYPI_EVENT_FNKEY_F1 },
3357         { 0x12, SONYPI_EVENT_FNKEY_F2 },
3358         { 0x13, SONYPI_EVENT_FNKEY_F3 },
3359         { 0x14, SONYPI_EVENT_FNKEY_F4 },
3360         { 0x15, SONYPI_EVENT_FNKEY_F5 },
3361         { 0x16, SONYPI_EVENT_FNKEY_F6 },
3362         { 0x17, SONYPI_EVENT_FNKEY_F7 },
3363         { 0x18, SONYPI_EVENT_FNKEY_F8 },
3364         { 0x19, SONYPI_EVENT_FNKEY_F9 },
3365         { 0x1a, SONYPI_EVENT_FNKEY_F10 },
3366         { 0x1b, SONYPI_EVENT_FNKEY_F11 },
3367         { 0x1c, SONYPI_EVENT_FNKEY_F12 },
3368         { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3369         { 0x21, SONYPI_EVENT_FNKEY_1 },
3370         { 0x22, SONYPI_EVENT_FNKEY_2 },
3371         { 0x31, SONYPI_EVENT_FNKEY_D },
3372         { 0x32, SONYPI_EVENT_FNKEY_E },
3373         { 0x33, SONYPI_EVENT_FNKEY_F },
3374         { 0x34, SONYPI_EVENT_FNKEY_S },
3375         { 0x35, SONYPI_EVENT_FNKEY_B },
3376         { 0x36, SONYPI_EVENT_FNKEY_ONLY },
3377         { 0, 0 }
3378 };
3379
3380 /* The set of possible program key events */
3381 static struct sonypi_event sonypi_pkeyev[] = {
3382         { 0x01, SONYPI_EVENT_PKEY_P1 },
3383         { 0x02, SONYPI_EVENT_PKEY_P2 },
3384         { 0x04, SONYPI_EVENT_PKEY_P3 },
3385         { 0x20, SONYPI_EVENT_PKEY_P1 },
3386         { 0, 0 }
3387 };
3388
3389 /* The set of possible bluetooth events */
3390 static struct sonypi_event sonypi_blueev[] = {
3391         { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3392         { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3393         { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3394         { 0, 0 }
3395 };
3396
3397 /* The set of possible wireless events */
3398 static struct sonypi_event sonypi_wlessev[] = {
3399         { 0x59, SONYPI_EVENT_IGNORE },
3400         { 0x5a, SONYPI_EVENT_IGNORE },
3401         { 0, 0 }
3402 };
3403
3404 /* The set of possible back button events */
3405 static struct sonypi_event sonypi_backev[] = {
3406         { 0x20, SONYPI_EVENT_BACK_PRESSED },
3407         { 0, 0 }
3408 };
3409
3410 /* The set of possible help button events */
3411 static struct sonypi_event sonypi_helpev[] = {
3412         { 0x3b, SONYPI_EVENT_HELP_PRESSED },
3413         { 0, 0 }
3414 };
3415
3416
3417 /* The set of possible lid events */
3418 static struct sonypi_event sonypi_lidev[] = {
3419         { 0x51, SONYPI_EVENT_LID_CLOSED },
3420         { 0x50, SONYPI_EVENT_LID_OPENED },
3421         { 0, 0 }
3422 };
3423
3424 /* The set of possible zoom events */
3425 static struct sonypi_event sonypi_zoomev[] = {
3426         { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3427         { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3428         { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
3429         { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
3430         { 0, 0 }
3431 };
3432
3433 /* The set of possible thumbphrase events */
3434 static struct sonypi_event sonypi_thumbphraseev[] = {
3435         { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3436         { 0, 0 }
3437 };
3438
3439 /* The set of possible motioneye camera events */
3440 static struct sonypi_event sonypi_meyeev[] = {
3441         { 0x00, SONYPI_EVENT_MEYE_FACE },
3442         { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3443         { 0, 0 }
3444 };
3445
3446 /* The set of possible memorystick events */
3447 static struct sonypi_event sonypi_memorystickev[] = {
3448         { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3449         { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3450         { 0, 0 }
3451 };
3452
3453 /* The set of possible battery events */
3454 static struct sonypi_event sonypi_batteryev[] = {
3455         { 0x20, SONYPI_EVENT_BATTERY_INSERT },
3456         { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3457         { 0, 0 }
3458 };
3459
3460 /* The set of possible volume events */
3461 static struct sonypi_event sonypi_volumeev[] = {
3462         { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3463         { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3464         { 0, 0 }
3465 };
3466
3467 /* The set of possible brightness events */
3468 static struct sonypi_event sonypi_brightnessev[] = {
3469         { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3470         { 0, 0 }
3471 };
3472
3473 static struct sonypi_eventtypes type1_events[] = {
3474         { 0, 0xffffffff, sonypi_releaseev },
3475         { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3476         { 0x30, SONYPI_LID_MASK, sonypi_lidev },
3477         { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3478         { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3479         { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3480         { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3481         { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3482         { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3483         { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3484         { 0 },
3485 };
3486 static struct sonypi_eventtypes type2_events[] = {
3487         { 0, 0xffffffff, sonypi_releaseev },
3488         { 0x38, SONYPI_LID_MASK, sonypi_lidev },
3489         { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3490         { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3491         { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3492         { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3493         { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3494         { 0x11, SONYPI_BACK_MASK, sonypi_backev },
3495         { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3496         { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3497         { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3498         { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3499         { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3500         { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3501         { 0 },
3502 };
3503 static struct sonypi_eventtypes type3_events[] = {
3504         { 0, 0xffffffff, sonypi_releaseev },
3505         { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3506         { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3507         { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3508         { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3509         { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3510         { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3511         { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3512         { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
3513         { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3514         { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3515         { 0 },
3516 };
3517
3518 /* low level spic calls */
3519 #define ITERATIONS_LONG         10000
3520 #define ITERATIONS_SHORT        10
3521 #define wait_on_command(command, iterations) {                          \
3522         unsigned int n = iterations;                                    \
3523         while (--n && (command))                                        \
3524                 udelay(1);                                              \
3525         if (!n)                                                         \
3526                 dprintk("command failed at %s : %s (line %d)\n",        \
3527                                 __FILE__, __func__, __LINE__);  \
3528 }
3529
3530 static u8 sony_pic_call1(u8 dev)
3531 {
3532         u8 v1, v2;
3533
3534         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3535                         ITERATIONS_LONG);
3536         outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3537         v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3538         v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
3539         dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
3540         return v2;
3541 }
3542
3543 static u8 sony_pic_call2(u8 dev, u8 fn)
3544 {
3545         u8 v1;
3546
3547         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3548                         ITERATIONS_LONG);
3549         outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3550         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3551                         ITERATIONS_LONG);
3552         outb(fn, spic_dev.cur_ioport->io1.minimum);
3553         v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3554         dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
3555         return v1;
3556 }
3557
3558 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
3559 {
3560         u8 v1;
3561
3562         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3563         outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3564         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3565         outb(fn, spic_dev.cur_ioport->io1.minimum);
3566         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
3567         outb(v, spic_dev.cur_ioport->io1.minimum);
3568         v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3569         dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
3570                         dev, fn, v, v1);
3571         return v1;
3572 }
3573
3574 /*
3575  * minidrivers for SPIC models
3576  */
3577 static int type3_handle_irq(const u8 data_mask, const u8 ev)
3578 {
3579         /*
3580          * 0x31 could mean we have to take some extra action and wait for
3581          * the next irq for some Type3 models, it will generate a new
3582          * irq and we can read new data from the device:
3583          *  - 0x5c and 0x5f requires 0xA0
3584          *  - 0x61 requires 0xB3
3585          */
3586         if (data_mask == 0x31) {
3587                 if (ev == 0x5c || ev == 0x5f)
3588                         sony_pic_call1(0xA0);
3589                 else if (ev == 0x61)
3590                         sony_pic_call1(0xB3);
3591                 return 0;
3592         }
3593         return 1;
3594 }
3595
3596 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3597 {
3598         struct pci_dev *pcidev;
3599
3600         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3601                         PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3602         if (pcidev) {
3603                 dev->model = SONYPI_DEVICE_TYPE1;
3604                 dev->evport_offset = SONYPI_TYPE1_OFFSET;
3605                 dev->event_types = type1_events;
3606                 goto out;
3607         }
3608
3609         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3610                         PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3611         if (pcidev) {
3612                 dev->model = SONYPI_DEVICE_TYPE2;
3613                 dev->evport_offset = SONYPI_TYPE2_OFFSET;
3614                 dev->event_types = type2_events;
3615                 goto out;
3616         }
3617
3618         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3619                         PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3620         if (pcidev) {
3621                 dev->model = SONYPI_DEVICE_TYPE3;
3622                 dev->handle_irq = type3_handle_irq;
3623                 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3624                 dev->event_types = type3_events;
3625                 goto out;
3626         }
3627
3628         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3629                         PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3630         if (pcidev) {
3631                 dev->model = SONYPI_DEVICE_TYPE3;
3632                 dev->handle_irq = type3_handle_irq;
3633                 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3634                 dev->event_types = type3_events;
3635                 goto out;
3636         }
3637
3638         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3639                         PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3640         if (pcidev) {
3641                 dev->model = SONYPI_DEVICE_TYPE3;
3642                 dev->handle_irq = type3_handle_irq;
3643                 dev->evport_offset = SONYPI_TYPE3_OFFSET;
3644                 dev->event_types = type3_events;
3645                 goto out;
3646         }
3647
3648         /* default */
3649         dev->model = SONYPI_DEVICE_TYPE2;
3650         dev->evport_offset = SONYPI_TYPE2_OFFSET;
3651         dev->event_types = type2_events;
3652
3653 out:
3654         if (pcidev)
3655                 pci_dev_put(pcidev);
3656
3657         pr_info("detected Type%d model\n",
3658                 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3659                 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3660 }
3661
3662 /* camera tests and poweron/poweroff */
3663 #define SONYPI_CAMERA_PICTURE           5
3664 #define SONYPI_CAMERA_CONTROL           0x10
3665
3666 #define SONYPI_CAMERA_BRIGHTNESS                0
3667 #define SONYPI_CAMERA_CONTRAST                  1
3668 #define SONYPI_CAMERA_HUE                       2
3669 #define SONYPI_CAMERA_COLOR                     3
3670 #define SONYPI_CAMERA_SHARPNESS                 4
3671
3672 #define SONYPI_CAMERA_EXPOSURE_MASK             0xC
3673 #define SONYPI_CAMERA_WHITE_BALANCE_MASK        0x3
3674 #define SONYPI_CAMERA_PICTURE_MODE_MASK         0x30
3675 #define SONYPI_CAMERA_MUTE_MASK                 0x40
3676
3677 /* the rest don't need a loop until not 0xff */
3678 #define SONYPI_CAMERA_AGC                       6
3679 #define SONYPI_CAMERA_AGC_MASK                  0x30
3680 #define SONYPI_CAMERA_SHUTTER_MASK              0x7
3681
3682 #define SONYPI_CAMERA_SHUTDOWN_REQUEST          7
3683 #define SONYPI_CAMERA_CONTROL                   0x10
3684
3685 #define SONYPI_CAMERA_STATUS                    7
3686 #define SONYPI_CAMERA_STATUS_READY              0x2
3687 #define SONYPI_CAMERA_STATUS_POSITION           0x4
3688
3689 #define SONYPI_DIRECTION_BACKWARDS              0x4
3690
3691 #define SONYPI_CAMERA_REVISION                  8
3692 #define SONYPI_CAMERA_ROMVERSION                9
3693
3694 static int __sony_pic_camera_ready(void)
3695 {
3696         u8 v;
3697
3698         v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
3699         return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
3700 }
3701
3702 static int __sony_pic_camera_off(void)
3703 {
3704         if (!camera) {
3705                 pr_warn("camera control not enabled\n");
3706                 return -ENODEV;
3707         }
3708
3709         wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
3710                                 SONYPI_CAMERA_MUTE_MASK),
3711                         ITERATIONS_SHORT);
3712
3713         if (spic_dev.camera_power) {
3714                 sony_pic_call2(0x91, 0);
3715                 spic_dev.camera_power = 0;
3716         }
3717         return 0;
3718 }
3719
3720 static int __sony_pic_camera_on(void)
3721 {
3722         int i, j, x;
3723
3724         if (!camera) {
3725                 pr_warn("camera control not enabled\n");
3726                 return -ENODEV;
3727         }
3728
3729         if (spic_dev.camera_power)
3730                 return 0;
3731
3732         for (j = 5; j > 0; j--) {
3733
3734                 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
3735                         msleep(10);
3736                 sony_pic_call1(0x93);
3737
3738                 for (i = 400; i > 0; i--) {
3739                         if (__sony_pic_camera_ready())
3740                                 break;
3741                         msleep(10);
3742                 }
3743                 if (i)
3744                         break;
3745         }
3746
3747         if (j == 0) {
3748                 pr_warn("failed to power on camera\n");
3749                 return -ENODEV;
3750         }
3751
3752         wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
3753                                 0x5a),
3754                         ITERATIONS_SHORT);
3755
3756         spic_dev.camera_power = 1;
3757         return 0;
3758 }
3759
3760 /* External camera command (exported to the motion eye v4l driver) */
3761 int sony_pic_camera_command(int command, u8 value)
3762 {
3763         if (!camera)
3764                 return -EIO;
3765
3766         mutex_lock(&spic_dev.lock);
3767
3768         switch (command) {
3769         case SONY_PIC_COMMAND_SETCAMERA:
3770                 if (value)
3771                         __sony_pic_camera_on();
3772                 else
3773                         __sony_pic_camera_off();
3774                 break;
3775         case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
3776                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
3777                                 ITERATIONS_SHORT);
3778                 break;
3779         case SONY_PIC_COMMAND_SETCAMERACONTRAST:
3780                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
3781                                 ITERATIONS_SHORT);
3782                 break;
3783         case SONY_PIC_COMMAND_SETCAMERAHUE:
3784                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
3785                                 ITERATIONS_SHORT);
3786                 break;
3787         case SONY_PIC_COMMAND_SETCAMERACOLOR:
3788                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
3789                                 ITERATIONS_SHORT);
3790                 break;
3791         case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
3792                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
3793                                 ITERATIONS_SHORT);
3794                 break;
3795         case SONY_PIC_COMMAND_SETCAMERAPICTURE:
3796                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
3797                                 ITERATIONS_SHORT);
3798                 break;
3799         case SONY_PIC_COMMAND_SETCAMERAAGC:
3800                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
3801                                 ITERATIONS_SHORT);
3802                 break;
3803         default:
3804                 pr_err("sony_pic_camera_command invalid: %d\n", command);
3805                 break;
3806         }
3807         mutex_unlock(&spic_dev.lock);
3808         return 0;
3809 }
3810 EXPORT_SYMBOL(sony_pic_camera_command);
3811
3812 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
3813 static void __sony_pic_set_wwanpower(u8 state)
3814 {
3815         state = !!state;
3816         if (spic_dev.wwan_power == state)
3817                 return;
3818         sony_pic_call2(0xB0, state);
3819         sony_pic_call1(0x82);
3820         spic_dev.wwan_power = state;
3821 }
3822
3823 static ssize_t sony_pic_wwanpower_store(struct device *dev,
3824                 struct device_attribute *attr,
3825                 const char *buffer, size_t count)
3826 {
3827         unsigned long value;
3828         if (count > 31)
3829                 return -EINVAL;
3830
3831         if (kstrtoul(buffer, 10, &value))
3832                 return -EINVAL;
3833
3834         mutex_lock(&spic_dev.lock);
3835         __sony_pic_set_wwanpower(value);
3836         mutex_unlock(&spic_dev.lock);
3837
3838         return count;
3839 }
3840
3841 static ssize_t sony_pic_wwanpower_show(struct device *dev,
3842                 struct device_attribute *attr, char *buffer)
3843 {
3844         ssize_t count;
3845         mutex_lock(&spic_dev.lock);
3846         count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
3847         mutex_unlock(&spic_dev.lock);
3848         return count;
3849 }
3850
3851 /* bluetooth subsystem power state */
3852 static void __sony_pic_set_bluetoothpower(u8 state)
3853 {
3854         state = !!state;
3855         if (spic_dev.bluetooth_power == state)
3856                 return;
3857         sony_pic_call2(0x96, state);
3858         sony_pic_call1(0x82);
3859         spic_dev.bluetooth_power = state;
3860 }
3861
3862 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3863                 struct device_attribute *attr,
3864                 const char *buffer, size_t count)
3865 {
3866         unsigned long value;
3867         if (count > 31)
3868                 return -EINVAL;
3869
3870         if (kstrtoul(buffer, 10, &value))
3871                 return -EINVAL;
3872
3873         mutex_lock(&spic_dev.lock);
3874         __sony_pic_set_bluetoothpower(value);
3875         mutex_unlock(&spic_dev.lock);
3876
3877         return count;
3878 }
3879
3880 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3881                 struct device_attribute *attr, char *buffer)
3882 {
3883         ssize_t count = 0;
3884         mutex_lock(&spic_dev.lock);
3885         count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
3886         mutex_unlock(&spic_dev.lock);
3887         return count;
3888 }
3889
3890 /* fan speed */
3891 /* FAN0 information (reverse engineered from ACPI tables) */
3892 #define SONY_PIC_FAN0_STATUS    0x93
3893 static int sony_pic_set_fanspeed(unsigned long value)
3894 {
3895         return ec_write(SONY_PIC_FAN0_STATUS, value);
3896 }
3897
3898 static int sony_pic_get_fanspeed(u8 *value)
3899 {
3900         return ec_read(SONY_PIC_FAN0_STATUS, value);
3901 }
3902
3903 static ssize_t sony_pic_fanspeed_store(struct device *dev,
3904                 struct device_attribute *attr,
3905                 const char *buffer, size_t count)
3906 {
3907         unsigned long value;
3908         if (count > 31)
3909                 return -EINVAL;
3910
3911         if (kstrtoul(buffer, 10, &value))
3912                 return -EINVAL;
3913
3914         if (sony_pic_set_fanspeed(value))
3915                 return -EIO;
3916
3917         return count;
3918 }
3919
3920 static ssize_t sony_pic_fanspeed_show(struct device *dev,
3921                 struct device_attribute *attr, char *buffer)
3922 {
3923         u8 value = 0;
3924         if (sony_pic_get_fanspeed(&value))
3925                 return -EIO;
3926
3927         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
3928 }
3929
3930 #define SPIC_ATTR(_name, _mode)                                 \
3931 struct device_attribute spic_attr_##_name = __ATTR(_name,       \
3932                 _mode, sony_pic_## _name ##_show,               \
3933                 sony_pic_## _name ##_store)
3934
3935 static SPIC_ATTR(bluetoothpower, 0644);
3936 static SPIC_ATTR(wwanpower, 0644);
3937 static SPIC_ATTR(fanspeed, 0644);
3938
3939 static struct attribute *spic_attributes[] = {
3940         &spic_attr_bluetoothpower.attr,
3941         &spic_attr_wwanpower.attr,
3942         &spic_attr_fanspeed.attr,
3943         NULL
3944 };
3945
3946 static struct attribute_group spic_attribute_group = {
3947         .attrs = spic_attributes
3948 };
3949
3950 /******** SONYPI compatibility **********/
3951 #ifdef CONFIG_SONYPI_COMPAT
3952
3953 /* battery / brightness / temperature  addresses */
3954 #define SONYPI_BAT_FLAGS        0x81
3955 #define SONYPI_LCD_LIGHT        0x96
3956 #define SONYPI_BAT1_PCTRM       0xa0
3957 #define SONYPI_BAT1_LEFT        0xa2
3958 #define SONYPI_BAT1_MAXRT       0xa4
3959 #define SONYPI_BAT2_PCTRM       0xa8
3960 #define SONYPI_BAT2_LEFT        0xaa
3961 #define SONYPI_BAT2_MAXRT       0xac
3962 #define SONYPI_BAT1_MAXTK       0xb0
3963 #define SONYPI_BAT1_FULL        0xb2
3964 #define SONYPI_BAT2_MAXTK       0xb8
3965 #define SONYPI_BAT2_FULL        0xba
3966 #define SONYPI_TEMP_STATUS      0xC1
3967
3968 struct sonypi_compat_s {
3969         struct fasync_struct    *fifo_async;
3970         struct kfifo            fifo;
3971         spinlock_t              fifo_lock;
3972         wait_queue_head_t       fifo_proc_list;
3973         atomic_t                open_count;
3974 };
3975 static struct sonypi_compat_s sonypi_compat = {
3976         .open_count = ATOMIC_INIT(0),
3977 };
3978
3979 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3980 {
3981         return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
3982 }
3983
3984 static int sonypi_misc_release(struct inode *inode, struct file *file)
3985 {
3986         atomic_dec(&sonypi_compat.open_count);
3987         return 0;
3988 }
3989
3990 static int sonypi_misc_open(struct inode *inode, struct file *file)
3991 {
3992         /* Flush input queue on first open */
3993         unsigned long flags;
3994
3995         spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
3996
3997         if (atomic_inc_return(&sonypi_compat.open_count) == 1)
3998                 kfifo_reset(&sonypi_compat.fifo);
3999
4000         spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
4001
4002         return 0;
4003 }
4004
4005 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
4006                                 size_t count, loff_t *pos)
4007 {
4008         ssize_t ret;
4009         unsigned char c;
4010
4011         if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
4012             (file->f_flags & O_NONBLOCK))
4013                 return -EAGAIN;
4014
4015         ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
4016                                        kfifo_len(&sonypi_compat.fifo) != 0);
4017         if (ret)
4018                 return ret;
4019
4020         while (ret < count &&
4021                (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
4022                           &sonypi_compat.fifo_lock) == sizeof(c))) {
4023                 if (put_user(c, buf++))
4024                         return -EFAULT;
4025                 ret++;
4026         }
4027
4028         if (ret > 0) {
4029                 struct inode *inode = file_inode(file);
4030                 inode->i_atime = current_fs_time(inode->i_sb);
4031         }
4032
4033         return ret;
4034 }
4035
4036 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
4037 {
4038         poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
4039         if (kfifo_len(&sonypi_compat.fifo))
4040                 return POLLIN | POLLRDNORM;
4041         return 0;
4042 }
4043
4044 static int ec_read16(u8 addr, u16 *value)
4045 {
4046         u8 val_lb, val_hb;
4047         if (ec_read(addr, &val_lb))
4048                 return -1;
4049         if (ec_read(addr + 1, &val_hb))
4050                 return -1;
4051         *value = val_lb | (val_hb << 8);
4052         return 0;
4053 }
4054
4055 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
4056                                                         unsigned long arg)
4057 {
4058         int ret = 0;
4059         void __user *argp = (void __user *)arg;
4060         u8 val8;
4061         u16 val16;
4062         int value;
4063
4064         mutex_lock(&spic_dev.lock);
4065         switch (cmd) {
4066         case SONYPI_IOCGBRT:
4067                 if (sony_bl_props.dev == NULL) {
4068                         ret = -EIO;
4069                         break;
4070                 }
4071                 if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
4072                                         &value)) {
4073                         ret = -EIO;
4074                         break;
4075                 }
4076                 val8 = ((value & 0xff) - 1) << 5;
4077                 if (copy_to_user(argp, &val8, sizeof(val8)))
4078                                 ret = -EFAULT;
4079                 break;
4080         case SONYPI_IOCSBRT:
4081                 if (sony_bl_props.dev == NULL) {
4082                         ret = -EIO;
4083                         break;
4084                 }
4085                 if (copy_from_user(&val8, argp, sizeof(val8))) {
4086                         ret = -EFAULT;
4087                         break;
4088                 }
4089                 value = (val8 >> 5) + 1;
4090                 if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
4091                                         NULL)) {
4092                         ret = -EIO;
4093                         break;
4094                 }
4095                 /* sync the backlight device status */
4096                 sony_bl_props.dev->props.brightness =
4097                     sony_backlight_get_brightness(sony_bl_props.dev);
4098                 break;
4099         case SONYPI_IOCGBAT1CAP:
4100                 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
4101                         ret = -EIO;
4102                         break;
4103                 }
4104                 if (copy_to_user(argp, &val16, sizeof(val16)))
4105                         ret = -EFAULT;
4106                 break;
4107         case SONYPI_IOCGBAT1REM:
4108                 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
4109                         ret = -EIO;
4110                         break;
4111                 }
4112                 if (copy_to_user(argp, &val16, sizeof(val16)))
4113                         ret = -EFAULT;
4114                 break;
4115         case SONYPI_IOCGBAT2CAP:
4116                 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
4117                         ret = -EIO;
4118                         break;
4119                 }
4120                 if (copy_to_user(argp, &val16, sizeof(val16)))
4121                         ret = -EFAULT;
4122                 break;
4123         case SONYPI_IOCGBAT2REM:
4124                 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
4125                         ret = -EIO;
4126                         break;
4127                 }
4128                 if (copy_to_user(argp, &val16, sizeof(val16)))
4129                         ret = -EFAULT;
4130                 break;
4131         case SONYPI_IOCGBATFLAGS:
4132                 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
4133                         ret = -EIO;
4134                         break;
4135                 }
4136                 val8 &= 0x07;
4137                 if (copy_to_user(argp, &val8, sizeof(val8)))
4138                         ret = -EFAULT;
4139                 break;
4140         case SONYPI_IOCGBLUE:
4141                 val8 = spic_dev.bluetooth_power;
4142                 if (copy_to_user(argp, &val8, sizeof(val8)))
4143                         ret = -EFAULT;
4144                 break;
4145         case SONYPI_IOCSBLUE:
4146                 if (copy_from_user(&val8, argp, sizeof(val8))) {
4147                         ret = -EFAULT;
4148                         break;
4149                 }
4150                 __sony_pic_set_bluetoothpower(val8);
4151                 break;
4152         /* FAN Controls */
4153         case SONYPI_IOCGFAN:
4154                 if (sony_pic_get_fanspeed(&val8)) {
4155                         ret = -EIO;
4156                         break;
4157                 }
4158                 if (copy_to_user(argp, &val8, sizeof(val8)))
4159                         ret = -EFAULT;
4160                 break;
4161         case SONYPI_IOCSFAN:
4162                 if (copy_from_user(&val8, argp, sizeof(val8))) {
4163                         ret = -EFAULT;
4164                         break;
4165                 }
4166                 if (sony_pic_set_fanspeed(val8))
4167                         ret = -EIO;
4168                 break;
4169         /* GET Temperature (useful under APM) */
4170         case SONYPI_IOCGTEMP:
4171                 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
4172                         ret = -EIO;
4173                         break;
4174                 }
4175                 if (copy_to_user(argp, &val8, sizeof(val8)))
4176                         ret = -EFAULT;
4177                 break;
4178         default:
4179                 ret = -EINVAL;
4180         }
4181         mutex_unlock(&spic_dev.lock);
4182         return ret;
4183 }
4184
4185 static const struct file_operations sonypi_misc_fops = {
4186         .owner          = THIS_MODULE,
4187         .read           = sonypi_misc_read,
4188         .poll           = sonypi_misc_poll,
4189         .open           = sonypi_misc_open,
4190         .release        = sonypi_misc_release,
4191         .fasync         = sonypi_misc_fasync,
4192         .unlocked_ioctl = sonypi_misc_ioctl,
4193         .llseek         = noop_llseek,
4194 };
4195
4196 static struct miscdevice sonypi_misc_device = {
4197         .minor          = MISC_DYNAMIC_MINOR,
4198         .name           = "sonypi",
4199         .fops           = &sonypi_misc_fops,
4200 };
4201
4202 static void sonypi_compat_report_event(u8 event)
4203 {
4204         kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
4205                         sizeof(event), &sonypi_compat.fifo_lock);
4206         kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
4207         wake_up_interruptible(&sonypi_compat.fifo_proc_list);
4208 }
4209
4210 static int sonypi_compat_init(void)
4211 {
4212         int error;
4213
4214         spin_lock_init(&sonypi_compat.fifo_lock);
4215         error =
4216          kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
4217         if (error) {
4218                 pr_err("kfifo_alloc failed\n");
4219                 return error;
4220         }
4221
4222         init_waitqueue_head(&sonypi_compat.fifo_proc_list);
4223
4224         if (minor != -1)
4225                 sonypi_misc_device.minor = minor;
4226         error = misc_register(&sonypi_misc_device);
4227         if (error) {
4228                 pr_err("misc_register failed\n");
4229                 goto err_free_kfifo;
4230         }
4231         if (minor == -1)
4232                 pr_info("device allocated minor is %d\n",
4233                         sonypi_misc_device.minor);
4234
4235         return 0;
4236
4237 err_free_kfifo:
4238         kfifo_free(&sonypi_compat.fifo);
4239         return error;
4240 }
4241
4242 static void sonypi_compat_exit(void)
4243 {
4244         misc_deregister(&sonypi_misc_device);
4245         kfifo_free(&sonypi_compat.fifo);
4246 }
4247 #else
4248 static int sonypi_compat_init(void) { return 0; }
4249 static void sonypi_compat_exit(void) { }
4250 static void sonypi_compat_report_event(u8 event) { }
4251 #endif /* CONFIG_SONYPI_COMPAT */
4252
4253 /*
4254  * ACPI callbacks
4255  */
4256 static acpi_status
4257 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
4258 {
4259         u32 i;
4260         struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
4261
4262         switch (resource->type) {
4263         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
4264                 {
4265                         /* start IO enumeration */
4266                         struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
4267                         if (!ioport)
4268                                 return AE_ERROR;
4269
4270                         list_add(&ioport->list, &dev->ioports);
4271                         return AE_OK;
4272                 }
4273
4274         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
4275                 /* end IO enumeration */
4276                 return AE_OK;
4277
4278         case ACPI_RESOURCE_TYPE_IRQ:
4279                 {
4280                         struct acpi_resource_irq *p = &resource->data.irq;
4281                         struct sony_pic_irq *interrupt = NULL;
4282                         if (!p || !p->interrupt_count) {
4283                                 /*
4284                                  * IRQ descriptors may have no IRQ# bits set,
4285                                  * particularly those those w/ _STA disabled
4286                                  */
4287                                 dprintk("Blank IRQ resource\n");
4288                                 return AE_OK;
4289                         }
4290                         for (i = 0; i < p->interrupt_count; i++) {
4291                                 if (!p->interrupts[i]) {
4292                                         pr_warn("Invalid IRQ %d\n",
4293                                                 p->interrupts[i]);
4294                                         continue;
4295                                 }
4296                                 interrupt = kzalloc(sizeof(*interrupt),
4297                                                 GFP_KERNEL);
4298                                 if (!interrupt)
4299                                         return AE_ERROR;
4300
4301                                 list_add(&interrupt->list, &dev->interrupts);
4302                                 interrupt->irq.triggering = p->triggering;
4303                                 interrupt->irq.polarity = p->polarity;
4304                                 interrupt->irq.sharable = p->sharable;
4305                                 interrupt->irq.interrupt_count = 1;
4306                                 interrupt->irq.interrupts[0] = p->interrupts[i];
4307                         }
4308                         return AE_OK;
4309                 }
4310         case ACPI_RESOURCE_TYPE_IO:
4311                 {
4312                         struct acpi_resource_io *io = &resource->data.io;
4313                         struct sony_pic_ioport *ioport =
4314                                 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
4315                         if (!io) {
4316                                 dprintk("Blank IO resource\n");
4317                                 return AE_OK;
4318                         }
4319
4320                         if (!ioport->io1.minimum) {
4321                                 memcpy(&ioport->io1, io, sizeof(*io));
4322                                 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
4323                                                 ioport->io1.address_length);
4324                         }
4325                         else if (!ioport->io2.minimum) {
4326                                 memcpy(&ioport->io2, io, sizeof(*io));
4327                                 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
4328                                                 ioport->io2.address_length);
4329                         }
4330                         else {
4331                                 pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
4332                                 return AE_ERROR;
4333                         }
4334                         return AE_OK;
4335                 }
4336         default:
4337                 dprintk("Resource %d isn't an IRQ nor an IO port\n",
4338                         resource->type);
4339
4340         case ACPI_RESOURCE_TYPE_END_TAG:
4341                 return AE_OK;
4342         }
4343         return AE_CTRL_TERMINATE;
4344 }
4345
4346 static int sony_pic_possible_resources(struct acpi_device *device)
4347 {
4348         int result = 0;
4349         acpi_status status = AE_OK;
4350
4351         if (!device)
4352                 return -EINVAL;
4353
4354         /* get device status */
4355         /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4356         dprintk("Evaluating _STA\n");
4357         result = acpi_bus_get_status(device);
4358         if (result) {
4359                 pr_warn("Unable to read status\n");
4360                 goto end;
4361         }
4362
4363         if (!device->status.enabled)
4364                 dprintk("Device disabled\n");
4365         else
4366                 dprintk("Device enabled\n");
4367
4368         /*
4369          * Query and parse 'method'
4370          */
4371         dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4372         status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4373                         sony_pic_read_possible_resource, &spic_dev);
4374         if (ACPI_FAILURE(status)) {
4375                 pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
4376                 result = -ENODEV;
4377         }
4378 end:
4379         return result;
4380 }
4381
4382 /*
4383  *  Disable the spic device by calling its _DIS method
4384  */
4385 static int sony_pic_disable(struct acpi_device *device)
4386 {
4387         acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
4388                                                NULL);
4389
4390         if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
4391                 return -ENXIO;
4392
4393         dprintk("Device disabled\n");
4394         return 0;
4395 }
4396
4397
4398 /*
4399  *  Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4400  *
4401  *  Call _SRS to set current resources
4402  */
4403 static int sony_pic_enable(struct acpi_device *device,
4404                 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4405 {
4406         acpi_status status;
4407         int result = 0;
4408         /* Type 1 resource layout is:
4409          *    IO
4410          *    IO
4411          *    IRQNoFlags
4412          *    End
4413          *
4414          * Type 2 and 3 resource layout is:
4415          *    IO
4416          *    IRQNoFlags
4417          *    End
4418          */
4419         struct {
4420                 struct acpi_resource res1;
4421                 struct acpi_resource res2;
4422                 struct acpi_resource res3;
4423                 struct acpi_resource res4;
4424         } *resource;
4425         struct acpi_buffer buffer = { 0, NULL };
4426
4427         if (!ioport || !irq)
4428                 return -EINVAL;
4429
4430         /* init acpi_buffer */
4431         resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4432         if (!resource)
4433                 return -ENOMEM;
4434
4435         buffer.length = sizeof(*resource) + 1;
4436         buffer.pointer = resource;
4437
4438         /* setup Type 1 resources */
4439         if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
4440
4441                 /* setup io resources */
4442                 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4443                 resource->res1.length = sizeof(struct acpi_resource);
4444                 memcpy(&resource->res1.data.io, &ioport->io1,
4445                                 sizeof(struct acpi_resource_io));
4446
4447                 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4448                 resource->res2.length = sizeof(struct acpi_resource);
4449                 memcpy(&resource->res2.data.io, &ioport->io2,
4450                                 sizeof(struct acpi_resource_io));
4451
4452                 /* setup irq resource */
4453                 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4454                 resource->res3.length = sizeof(struct acpi_resource);
4455                 memcpy(&resource->res3.data.irq, &irq->irq,
4456                                 sizeof(struct acpi_resource_irq));
4457                 /* we requested a shared irq */
4458                 resource->res3.data.irq.sharable = ACPI_SHARED;
4459
4460                 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
4461                 resource->res4.length = sizeof(struct acpi_resource);
4462         }
4463         /* setup Type 2/3 resources */
4464         else {
4465                 /* setup io resource */
4466                 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4467                 resource->res1.length = sizeof(struct acpi_resource);
4468                 memcpy(&resource->res1.data.io, &ioport->io1,
4469                                 sizeof(struct acpi_resource_io));
4470
4471                 /* setup irq resource */
4472                 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4473                 resource->res2.length = sizeof(struct acpi_resource);
4474                 memcpy(&resource->res2.data.irq, &irq->irq,
4475                                 sizeof(struct acpi_resource_irq));
4476                 /* we requested a shared irq */
4477                 resource->res2.data.irq.sharable = ACPI_SHARED;
4478
4479                 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
4480                 resource->res3.length = sizeof(struct acpi_resource);
4481         }
4482
4483         /* Attempt to set the resource */
4484         dprintk("Evaluating _SRS\n");
4485         status = acpi_set_current_resources(device->handle, &buffer);
4486
4487         /* check for total failure */
4488         if (ACPI_FAILURE(status)) {
4489                 pr_err("Error evaluating _SRS\n");
4490                 result = -ENODEV;
4491                 goto end;
4492         }
4493
4494         /* Necessary device initializations calls (from sonypi) */
4495         sony_pic_call1(0x82);
4496         sony_pic_call2(0x81, 0xff);
4497         sony_pic_call1(compat ? 0x92 : 0x82);
4498
4499 end:
4500         kfree(resource);
4501         return result;
4502 }
4503
4504 /*****************
4505  *
4506  * ISR: some event is available
4507  *
4508  *****************/
4509 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4510 {
4511         int i, j;
4512         u8 ev = 0;
4513         u8 data_mask = 0;
4514         u8 device_event = 0;
4515
4516         struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4517
4518         ev = inb_p(dev->cur_ioport->io1.minimum);
4519         if (dev->cur_ioport->io2.minimum)
4520                 data_mask = inb_p(dev->cur_ioport->io2.minimum);
4521         else
4522                 data_mask = inb_p(dev->cur_ioport->io1.minimum +
4523                                 dev->evport_offset);
4524
4525         dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4526                         ev, data_mask, dev->cur_ioport->io1.minimum,
4527                         dev->evport_offset);
4528
4529         if (ev == 0x00 || ev == 0xff)
4530                 return IRQ_HANDLED;
4531
4532         for (i = 0; dev->event_types[i].mask; i++) {
4533
4534                 if ((data_mask & dev->event_types[i].data) !=
4535                     dev->event_types[i].data)
4536                         continue;
4537
4538                 if (!(mask & dev->event_types[i].mask))
4539                         continue;
4540
4541                 for (j = 0; dev->event_types[i].events[j].event; j++) {
4542                         if (ev == dev->event_types[i].events[j].data) {
4543                                 device_event =
4544                                         dev->event_types[i].events[j].event;
4545                                 /* some events may require ignoring */
4546                                 if (!device_event)
4547                                         return IRQ_HANDLED;
4548                                 goto found;
4549                         }
4550                 }
4551         }
4552         /* Still not able to decode the event try to pass
4553          * it over to the minidriver
4554          */
4555         if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
4556                 return IRQ_HANDLED;
4557
4558         dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4559                         ev, data_mask, dev->cur_ioport->io1.minimum,
4560                         dev->evport_offset);
4561         return IRQ_HANDLED;
4562
4563 found:
4564         sony_laptop_report_input_event(device_event);
4565         sonypi_compat_report_event(device_event);
4566         return IRQ_HANDLED;
4567 }
4568
4569 /*****************
4570  *
4571  *  ACPI driver
4572  *
4573  *****************/
4574 static int sony_pic_remove(struct acpi_device *device)
4575 {
4576         struct sony_pic_ioport *io, *tmp_io;
4577         struct sony_pic_irq *irq, *tmp_irq;
4578
4579         if (sony_pic_disable(device)) {
4580                 pr_err("Couldn't disable device\n");
4581                 return -ENXIO;
4582         }
4583
4584         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4585         release_region(spic_dev.cur_ioport->io1.minimum,
4586                         spic_dev.cur_ioport->io1.address_length);
4587         if (spic_dev.cur_ioport->io2.minimum)
4588                 release_region(spic_dev.cur_ioport->io2.minimum,
4589                                 spic_dev.cur_ioport->io2.address_length);
4590
4591         sonypi_compat_exit();
4592
4593         sony_laptop_remove_input();
4594
4595         /* pf attrs */
4596         sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4597         sony_pf_remove();
4598
4599         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4600                 list_del(&io->list);
4601                 kfree(io);
4602         }
4603         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4604                 list_del(&irq->list);
4605                 kfree(irq);
4606         }
4607         spic_dev.cur_ioport = NULL;
4608         spic_dev.cur_irq = NULL;
4609
4610         dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
4611         return 0;
4612 }
4613
4614 static int sony_pic_add(struct acpi_device *device)
4615 {
4616         int result;
4617         struct sony_pic_ioport *io, *tmp_io;
4618         struct sony_pic_irq *irq, *tmp_irq;
4619
4620         pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
4621
4622         spic_dev.acpi_dev = device;
4623         strcpy(acpi_device_class(device), "sony/hotkey");
4624         sony_pic_detect_device_type(&spic_dev);
4625         mutex_init(&spic_dev.lock);
4626
4627         /* read _PRS resources */
4628         result = sony_pic_possible_resources(device);
4629         if (result) {
4630                 pr_err("Unable to read possible resources\n");
4631                 goto err_free_resources;
4632         }
4633
4634         /* setup input devices and helper fifo */
4635         result = sony_laptop_setup_input(device);
4636         if (result) {
4637                 pr_err("Unable to create input devices\n");
4638                 goto err_free_resources;
4639         }
4640
4641         result = sonypi_compat_init();
4642         if (result)
4643                 goto err_remove_input;
4644
4645         /* request io port */
4646         list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4647                 if (request_region(io->io1.minimum, io->io1.address_length,
4648                                         "Sony Programmable I/O Device")) {
4649                         dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4650                                         io->io1.minimum, io->io1.maximum,
4651                                         io->io1.address_length);
4652                         /* Type 1 have 2 ioports */
4653                         if (io->io2.minimum) {
4654                                 if (request_region(io->io2.minimum,
4655                                                 io->io2.address_length,
4656                                                 "Sony Programmable I/O Device")) {
4657                                         dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4658                                                         io->io2.minimum, io->io2.maximum,
4659                                                         io->io2.address_length);
4660                                         spic_dev.cur_ioport = io;
4661                                         break;
4662                                 }
4663                                 else {
4664                                         dprintk("Unable to get I/O port2: "
4665                                                         "0x%.4x (0x%.4x) + 0x%.2x\n",
4666                                                         io->io2.minimum, io->io2.maximum,
4667                                                         io->io2.address_length);
4668                                         release_region(io->io1.minimum,
4669                                                         io->io1.address_length);
4670                                 }
4671                         }
4672                         else {
4673                                 spic_dev.cur_ioport = io;
4674                                 break;
4675                         }
4676                 }
4677         }
4678         if (!spic_dev.cur_ioport) {
4679                 pr_err("Failed to request_region\n");
4680                 result = -ENODEV;
4681                 goto err_remove_compat;
4682         }
4683
4684         /* request IRQ */
4685         list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
4686                 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
4687                                         0, "sony-laptop", &spic_dev)) {
4688                         dprintk("IRQ: %d - triggering: %d - "
4689                                         "polarity: %d - shr: %d\n",
4690                                         irq->irq.interrupts[0],
4691                                         irq->irq.triggering,
4692                                         irq->irq.polarity,
4693                                         irq->irq.sharable);
4694                         spic_dev.cur_irq = irq;
4695                         break;
4696                 }
4697         }
4698         if (!spic_dev.cur_irq) {
4699                 pr_err("Failed to request_irq\n");
4700                 result = -ENODEV;
4701                 goto err_release_region;
4702         }
4703
4704         /* set resource status _SRS */
4705         result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
4706         if (result) {
4707                 pr_err("Couldn't enable device\n");
4708                 goto err_free_irq;
4709         }
4710
4711         spic_dev.bluetooth_power = -1;
4712         /* create device attributes */
4713         result = sony_pf_add();
4714         if (result)
4715                 goto err_disable_device;
4716
4717         result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4718         if (result)
4719                 goto err_remove_pf;
4720
4721         return 0;
4722
4723 err_remove_pf:
4724         sony_pf_remove();
4725
4726 err_disable_device:
4727         sony_pic_disable(device);
4728
4729 err_free_irq:
4730         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4731
4732 err_release_region:
4733         release_region(spic_dev.cur_ioport->io1.minimum,
4734                         spic_dev.cur_ioport->io1.address_length);
4735         if (spic_dev.cur_ioport->io2.minimum)
4736                 release_region(spic_dev.cur_ioport->io2.minimum,
4737                                 spic_dev.cur_ioport->io2.address_length);
4738
4739 err_remove_compat:
4740         sonypi_compat_exit();
4741
4742 err_remove_input:
4743         sony_laptop_remove_input();
4744
4745 err_free_resources:
4746         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4747                 list_del(&io->list);
4748                 kfree(io);
4749         }
4750         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4751                 list_del(&irq->list);
4752                 kfree(irq);
4753         }
4754         spic_dev.cur_ioport = NULL;
4755         spic_dev.cur_irq = NULL;
4756
4757         return result;
4758 }
4759
4760 #ifdef CONFIG_PM_SLEEP
4761 static int sony_pic_suspend(struct device *dev)
4762 {
4763         if (sony_pic_disable(to_acpi_device(dev)))
4764                 return -ENXIO;
4765         return 0;
4766 }
4767
4768 static int sony_pic_resume(struct device *dev)
4769 {
4770         sony_pic_enable(to_acpi_device(dev),
4771                         spic_dev.cur_ioport, spic_dev.cur_irq);
4772         return 0;
4773 }
4774 #endif
4775
4776 static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4777
4778 static const struct acpi_device_id sony_pic_device_ids[] = {
4779         {SONY_PIC_HID, 0},
4780         {"", 0},
4781 };
4782
4783 static struct acpi_driver sony_pic_driver = {
4784         .name = SONY_PIC_DRIVER_NAME,
4785         .class = SONY_PIC_CLASS,
4786         .ids = sony_pic_device_ids,
4787         .owner = THIS_MODULE,
4788         .ops = {
4789                 .add = sony_pic_add,
4790                 .remove = sony_pic_remove,
4791                 },
4792         .drv.pm = &sony_pic_pm,
4793 };
4794
4795 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
4796         {
4797                 .ident = "Sony Vaio",
4798                 .matches = {
4799                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4800                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4801                 },
4802         },
4803         {
4804                 .ident = "Sony Vaio",
4805                 .matches = {
4806                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4807                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4808                 },
4809         },
4810         { }
4811 };
4812
4813 static int __init sony_laptop_init(void)
4814 {
4815         int result;
4816
4817         if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4818                 result = acpi_bus_register_driver(&sony_pic_driver);
4819                 if (result) {
4820                         pr_err("Unable to register SPIC driver\n");
4821                         goto out;
4822                 }
4823                 spic_drv_registered = 1;
4824         }
4825
4826         result = acpi_bus_register_driver(&sony_nc_driver);
4827         if (result) {
4828                 pr_err("Unable to register SNC driver\n");
4829                 goto out_unregister_pic;
4830         }
4831
4832         return 0;
4833
4834 out_unregister_pic:
4835         if (spic_drv_registered)
4836                 acpi_bus_unregister_driver(&sony_pic_driver);
4837 out:
4838         return result;
4839 }
4840
4841 static void __exit sony_laptop_exit(void)
4842 {
4843         acpi_bus_unregister_driver(&sony_nc_driver);
4844         if (spic_drv_registered)
4845                 acpi_bus_unregister_driver(&sony_pic_driver);
4846 }
4847
4848 module_init(sony_laptop_init);
4849 module_exit(sony_laptop_exit);