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