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