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