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