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