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