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