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