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