xref: /linux/drivers/platform/x86/sony-laptop.c (revision 5b05bb3f6c5716fab6911e12d60dd1f43ad9806a)
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 */
do_sony_laptop_release_key(struct timer_list * unused)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 */
sony_laptop_report_input_event(u8 event)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 
sony_laptop_setup_input(struct device * parent)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 
sony_laptop_remove_input(void)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 
sony_pf_add(void)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 
sony_pf_remove(void)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  */
__call_snc_method(acpi_handle handle,char * method,u64 * value)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 
sony_nc_buffer_call(acpi_handle handle,char * name,u64 * value,void * buffer,size_t buflen)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 
sony_nc_int_call(acpi_handle handle,char * name,int * value,int * result)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 
sony_nc_handles_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_handles_setup(struct platform_device * pd)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 
sony_nc_handles_cleanup(struct platform_device * pd)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 
sony_find_snc_handle(int handle)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 
sony_call_snc_handle(int handle,int argument,int * result)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  */
brightness_default_validate(const int direction,const int value)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  */
boolean_validate(const int direction,const int value)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  */
sony_nc_sysfs_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_sysfs_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_backlight_update_status(struct backlight_device * bd)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 
sony_backlight_get_brightness(struct backlight_device * bd)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 
sony_nc_get_brightness_ng(struct backlight_device * bd)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 
sony_nc_update_status_ng(struct backlight_device * bd)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 
sony_nc_hotkeys_decode(u32 event,unsigned int handle)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 };
sony_nc_notify(acpi_handle ah,u32 event,void * data)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/hotkey",
1268 			dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
1269 }
1270 
sony_walk_callback(acpi_handle handle,u32 level,void * context,void ** return_value)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  */
sony_nc_function_setup(struct device * dev,struct platform_device * pf_device)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 
sony_nc_function_cleanup(struct platform_device * pd)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
sony_nc_function_resume(void)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 
sony_nc_resume(struct device * dev)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 
sony_nc_rfkill_cleanup(void)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 
sony_nc_rfkill_set(void * data,bool blocked)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 
sony_nc_setup_rfkill(struct device * parent,enum sony_nc_rfkill nc_type)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 
sony_nc_rfkill_update(void)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 
sony_nc_rfkill_setup(struct device * parent,unsigned int handle)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 
__sony_nc_kbd_backlight_mode_set(u8 value)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 
sony_nc_kbd_backlight_mode_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_kbd_backlight_mode_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
__sony_nc_kbd_backlight_timeout_set(u8 value)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 
sony_nc_kbd_backlight_timeout_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_kbd_backlight_timeout_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_kbd_backlight_setup(struct platform_device * pd,unsigned int handle)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 
sony_nc_kbd_backlight_cleanup(struct platform_device * pd,unsigned int handle)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 
sony_nc_battery_care_limit_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_battery_care_limit_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_battery_care_health_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_battery_care_setup(struct platform_device * pd,unsigned int handle)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 
sony_nc_battery_care_cleanup(struct platform_device * pd)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 
sony_nc_thermal_mode_set(unsigned short mode)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 
sony_nc_thermal_mode_get(void)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 
sony_nc_thermal_profiles_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_thermal_mode_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_thermal_mode_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_thermal_setup(struct platform_device * pd)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 
sony_nc_thermal_cleanup(struct platform_device * pd)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
sony_nc_thermal_resume(void)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 
sony_nc_lid_resume_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_lid_resume_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_lid_resume_setup(struct platform_device * pd,unsigned int handle)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 
sony_nc_lid_resume_cleanup(struct platform_device * pd)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 */
__sony_nc_gfx_switch_status_get(void)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 
sony_nc_gfx_switch_status_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_gfx_switch_setup(struct platform_device * pd,unsigned int handle)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 
sony_nc_gfx_switch_cleanup(struct platform_device * pd)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 
sony_nc_highspeed_charging_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_highspeed_charging_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_highspeed_charging_setup(struct platform_device * pd)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 
sony_nc_highspeed_charging_cleanup(struct platform_device * pd)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 
sony_nc_lowbatt_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_lowbatt_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_lowbatt_setup(struct platform_device * pd)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 
sony_nc_lowbatt_cleanup(struct platform_device * pd)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 
sony_nc_hsfan_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_hsfan_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_fanspeed_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_fanspeed_setup(struct platform_device * pd)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 
sony_nc_fanspeed_cleanup(struct platform_device * pd)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 
sony_nc_usb_charge_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_usb_charge_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_usb_charge_setup(struct platform_device * pd)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 
sony_nc_usb_charge_cleanup(struct platform_device * pd)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 
sony_nc_panelid_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_panelid_setup(struct platform_device * pd)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 
sony_nc_panelid_cleanup(struct platform_device * pd)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 
sony_nc_smart_conn_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_smart_conn_setup(struct platform_device * pd)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 
sony_nc_smart_conn_cleanup(struct platform_device * pd)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 
sony_nc_touchpad_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)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 
sony_nc_touchpad_show(struct device * dev,struct device_attribute * attr,char * buffer)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 
sony_nc_touchpad_setup(struct platform_device * pd,unsigned int handle)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 
sony_nc_touchpad_cleanup(struct platform_device * pd)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 
sony_nc_backlight_ng_read_limits(int handle,struct sony_backlight_props * props)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 
sony_nc_backlight_setup(void)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 
sony_nc_backlight_cleanup(void)3143 static void sony_nc_backlight_cleanup(void)
3144 {
3145 	backlight_device_unregister(sony_bl_props.dev);
3146 }
3147 
sony_nc_probe(struct platform_device * pdev)3148 static int sony_nc_probe(struct platform_device *pdev)
3149 {
3150 	struct acpi_device *device;
3151 	acpi_status status;
3152 	int result = 0;
3153 	struct sony_nc_value *item;
3154 
3155 	device = ACPI_COMPANION(&pdev->dev);
3156 	if (!device)
3157 		return -ENODEV;
3158 
3159 	sony_nc_acpi_device = device;
3160 	sony_nc_acpi_handle = device->handle;
3161 
3162 	/* read device status */
3163 	result = acpi_bus_get_status(device);
3164 	/* bail IFF the above call was successful and the device is not present */
3165 	if (!result && !device->status.present) {
3166 		dprintk("Device not present\n");
3167 		result = -ENODEV;
3168 		goto outwalk;
3169 	}
3170 
3171 	result = sony_pf_add();
3172 	if (result)
3173 		goto outpresent;
3174 
3175 	if (debug) {
3176 		status = acpi_walk_namespace(ACPI_TYPE_METHOD,
3177 				sony_nc_acpi_handle, 1, sony_walk_callback,
3178 				NULL, NULL, NULL);
3179 		if (ACPI_FAILURE(status)) {
3180 			pr_warn("unable to walk acpi resources\n");
3181 			result = -ENODEV;
3182 			goto outpresent;
3183 		}
3184 	}
3185 
3186 	result = sony_laptop_setup_input(&pdev->dev);
3187 	if (result) {
3188 		pr_err("Unable to create input devices\n");
3189 		goto outplatform;
3190 	}
3191 
3192 	if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
3193 		int arg = 1;
3194 		if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
3195 			dprintk("ECON Method failed\n");
3196 	}
3197 
3198 	if (acpi_has_method(sony_nc_acpi_handle, "SN00")) {
3199 		dprintk("Doing SNC setup\n");
3200 		/* retrieve the available handles */
3201 		result = sony_nc_handles_setup(sony_pf_device);
3202 		if (!result)
3203 			sony_nc_function_setup(&pdev->dev, sony_pf_device);
3204 	}
3205 
3206 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor)
3207 		sony_nc_backlight_setup();
3208 
3209 	/* create sony_pf sysfs attributes related to the SNC device */
3210 	for (item = sony_nc_values; item->name; ++item) {
3211 
3212 		if (!debug && item->debug)
3213 			continue;
3214 
3215 		/* find the available acpiget as described in the DSDT */
3216 		for (; item->acpiget && *item->acpiget; ++item->acpiget) {
3217 			if (acpi_has_method(sony_nc_acpi_handle,
3218 							*item->acpiget)) {
3219 				dprintk("Found %s getter: %s\n",
3220 						item->name, *item->acpiget);
3221 				item->devattr.attr.mode |= S_IRUGO;
3222 				break;
3223 			}
3224 		}
3225 
3226 		/* find the available acpiset as described in the DSDT */
3227 		for (; item->acpiset && *item->acpiset; ++item->acpiset) {
3228 			if (acpi_has_method(sony_nc_acpi_handle,
3229 							*item->acpiset)) {
3230 				dprintk("Found %s setter: %s\n",
3231 						item->name, *item->acpiset);
3232 				item->devattr.attr.mode |= S_IWUSR;
3233 				break;
3234 			}
3235 		}
3236 
3237 		if (item->devattr.attr.mode != 0) {
3238 			result =
3239 			    device_create_file(&sony_pf_device->dev,
3240 					       &item->devattr);
3241 			if (result)
3242 				goto out_sysfs;
3243 		}
3244 	}
3245 
3246 	result = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
3247 						 sony_nc_notify, NULL);
3248 	if (result)
3249 		goto out_sysfs;
3250 
3251 	pr_info("SNC setup done.\n");
3252 	return 0;
3253 
3254 out_sysfs:
3255 	for (item = sony_nc_values; item->name; ++item) {
3256 		device_remove_file(&sony_pf_device->dev, &item->devattr);
3257 	}
3258 	sony_nc_backlight_cleanup();
3259 	sony_nc_function_cleanup(sony_pf_device);
3260 	sony_nc_handles_cleanup(sony_pf_device);
3261 
3262 outplatform:
3263 	sony_laptop_remove_input();
3264 
3265 outpresent:
3266 	sony_pf_remove();
3267 
3268 outwalk:
3269 	sony_nc_rfkill_cleanup();
3270 	return result;
3271 }
3272 
sony_nc_remove(struct platform_device * pdev)3273 static void sony_nc_remove(struct platform_device *pdev)
3274 {
3275 	struct sony_nc_value *item;
3276 
3277 	acpi_dev_remove_notify_handler(ACPI_COMPANION(&pdev->dev),
3278 				       ACPI_DEVICE_NOTIFY, sony_nc_notify);
3279 
3280 	sony_nc_backlight_cleanup();
3281 
3282 	sony_nc_acpi_device = NULL;
3283 
3284 	for (item = sony_nc_values; item->name; ++item) {
3285 		device_remove_file(&sony_pf_device->dev, &item->devattr);
3286 	}
3287 
3288 	sony_nc_function_cleanup(sony_pf_device);
3289 	sony_nc_handles_cleanup(sony_pf_device);
3290 	sony_pf_remove();
3291 	sony_laptop_remove_input();
3292 	dprintk(SONY_NC_DRIVER_NAME " removed.\n");
3293 }
3294 
3295 static const struct acpi_device_id sony_device_ids[] __maybe_unused = {
3296 	{SONY_NC_HID, 0},
3297 	{SONY_PIC_HID, 0},
3298 	{"", 0},
3299 };
3300 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
3301 
3302 static const struct acpi_device_id sony_nc_device_ids[] = {
3303 	{SONY_NC_HID, 0},
3304 	{"", 0},
3305 };
3306 
3307 static struct platform_driver sony_nc_driver = {
3308 	.probe = sony_nc_probe,
3309 	.remove = sony_nc_remove,
3310 	.driver = {
3311 		.name = SONY_NC_DRIVER_NAME,
3312 		.acpi_match_table = sony_nc_device_ids,
3313 		.pm = &sony_nc_pm,
3314 	},
3315 };
3316 
3317 /*********** SPIC (SNY6001) Device ***********/
3318 
3319 #define SONYPI_DEVICE_TYPE1	0x00000001
3320 #define SONYPI_DEVICE_TYPE2	0x00000002
3321 #define SONYPI_DEVICE_TYPE3	0x00000004
3322 
3323 #define SONYPI_TYPE1_OFFSET	0x04
3324 #define SONYPI_TYPE2_OFFSET	0x12
3325 #define SONYPI_TYPE3_OFFSET	0x12
3326 
3327 struct sony_pic_ioport {
3328 	struct acpi_resource_io	io1;
3329 	struct acpi_resource_io	io2;
3330 	struct list_head	list;
3331 };
3332 
3333 struct sony_pic_irq {
3334 	struct list_head		list;
3335 
3336 	/* Must be last --ends in a flexible-array member. */
3337 	struct acpi_resource_irq	irq;
3338 };
3339 
3340 struct sonypi_eventtypes {
3341 	u8			data;
3342 	unsigned long		mask;
3343 	struct sonypi_event	*events;
3344 };
3345 
3346 struct sony_pic_dev {
3347 	struct acpi_device		*acpi_dev;
3348 	struct sony_pic_irq		*cur_irq;
3349 	struct sony_pic_ioport		*cur_ioport;
3350 	struct list_head		interrupts;
3351 	struct list_head		ioports;
3352 	struct mutex			lock;
3353 	struct sonypi_eventtypes	*event_types;
3354 	int                             (*handle_irq)(const u8, const u8);
3355 	int				model;
3356 	u16				evport_offset;
3357 	u8				camera_power;
3358 	u8				bluetooth_power;
3359 	u8				wwan_power;
3360 };
3361 
3362 static struct sony_pic_dev spic_dev = {
3363 	.interrupts	= LIST_HEAD_INIT(spic_dev.interrupts),
3364 	.ioports	= LIST_HEAD_INIT(spic_dev.ioports),
3365 };
3366 
3367 static int spic_drv_registered;
3368 
3369 /* Event masks */
3370 #define SONYPI_JOGGER_MASK			0x00000001
3371 #define SONYPI_CAPTURE_MASK			0x00000002
3372 #define SONYPI_FNKEY_MASK			0x00000004
3373 #define SONYPI_BLUETOOTH_MASK			0x00000008
3374 #define SONYPI_PKEY_MASK			0x00000010
3375 #define SONYPI_BACK_MASK			0x00000020
3376 #define SONYPI_HELP_MASK			0x00000040
3377 #define SONYPI_LID_MASK				0x00000080
3378 #define SONYPI_ZOOM_MASK			0x00000100
3379 #define SONYPI_THUMBPHRASE_MASK			0x00000200
3380 #define SONYPI_MEYE_MASK			0x00000400
3381 #define SONYPI_MEMORYSTICK_MASK			0x00000800
3382 #define SONYPI_BATTERY_MASK			0x00001000
3383 #define SONYPI_WIRELESS_MASK			0x00002000
3384 
3385 struct sonypi_event {
3386 	u8	data;
3387 	u8	event;
3388 };
3389 
3390 /* The set of possible button release events */
3391 static struct sonypi_event sonypi_releaseev[] = {
3392 	{ 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
3393 	{ 0, 0 }
3394 };
3395 
3396 /* The set of possible jogger events  */
3397 static struct sonypi_event sonypi_joggerev[] = {
3398 	{ 0x1f, SONYPI_EVENT_JOGDIAL_UP },
3399 	{ 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
3400 	{ 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
3401 	{ 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
3402 	{ 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
3403 	{ 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
3404 	{ 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
3405 	{ 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
3406 	{ 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
3407 	{ 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
3408 	{ 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
3409 	{ 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
3410 	{ 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
3411 	{ 0, 0 }
3412 };
3413 
3414 /* The set of possible capture button events */
3415 static struct sonypi_event sonypi_captureev[] = {
3416 	{ 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
3417 	{ 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
3418 	{ 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
3419 	{ 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
3420 	{ 0, 0 }
3421 };
3422 
3423 /* The set of possible fnkeys events */
3424 static struct sonypi_event sonypi_fnkeyev[] = {
3425 	{ 0x10, SONYPI_EVENT_FNKEY_ESC },
3426 	{ 0x11, SONYPI_EVENT_FNKEY_F1 },
3427 	{ 0x12, SONYPI_EVENT_FNKEY_F2 },
3428 	{ 0x13, SONYPI_EVENT_FNKEY_F3 },
3429 	{ 0x14, SONYPI_EVENT_FNKEY_F4 },
3430 	{ 0x15, SONYPI_EVENT_FNKEY_F5 },
3431 	{ 0x16, SONYPI_EVENT_FNKEY_F6 },
3432 	{ 0x17, SONYPI_EVENT_FNKEY_F7 },
3433 	{ 0x18, SONYPI_EVENT_FNKEY_F8 },
3434 	{ 0x19, SONYPI_EVENT_FNKEY_F9 },
3435 	{ 0x1a, SONYPI_EVENT_FNKEY_F10 },
3436 	{ 0x1b, SONYPI_EVENT_FNKEY_F11 },
3437 	{ 0x1c, SONYPI_EVENT_FNKEY_F12 },
3438 	{ 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
3439 	{ 0x21, SONYPI_EVENT_FNKEY_1 },
3440 	{ 0x22, SONYPI_EVENT_FNKEY_2 },
3441 	{ 0x31, SONYPI_EVENT_FNKEY_D },
3442 	{ 0x32, SONYPI_EVENT_FNKEY_E },
3443 	{ 0x33, SONYPI_EVENT_FNKEY_F },
3444 	{ 0x34, SONYPI_EVENT_FNKEY_S },
3445 	{ 0x35, SONYPI_EVENT_FNKEY_B },
3446 	{ 0x36, SONYPI_EVENT_FNKEY_ONLY },
3447 	{ 0, 0 }
3448 };
3449 
3450 /* The set of possible program key events */
3451 static struct sonypi_event sonypi_pkeyev[] = {
3452 	{ 0x01, SONYPI_EVENT_PKEY_P1 },
3453 	{ 0x02, SONYPI_EVENT_PKEY_P2 },
3454 	{ 0x04, SONYPI_EVENT_PKEY_P3 },
3455 	{ 0x20, SONYPI_EVENT_PKEY_P1 },
3456 	{ 0, 0 }
3457 };
3458 
3459 /* The set of possible bluetooth events */
3460 static struct sonypi_event sonypi_blueev[] = {
3461 	{ 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
3462 	{ 0x59, SONYPI_EVENT_BLUETOOTH_ON },
3463 	{ 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
3464 	{ 0, 0 }
3465 };
3466 
3467 /* The set of possible wireless events */
3468 static struct sonypi_event sonypi_wlessev[] = {
3469 	{ 0x59, SONYPI_EVENT_IGNORE },
3470 	{ 0x5a, SONYPI_EVENT_IGNORE },
3471 	{ 0, 0 }
3472 };
3473 
3474 /* The set of possible back button events */
3475 static struct sonypi_event sonypi_backev[] = {
3476 	{ 0x20, SONYPI_EVENT_BACK_PRESSED },
3477 	{ 0, 0 }
3478 };
3479 
3480 /* The set of possible help button events */
3481 static struct sonypi_event sonypi_helpev[] = {
3482 	{ 0x3b, SONYPI_EVENT_HELP_PRESSED },
3483 	{ 0, 0 }
3484 };
3485 
3486 
3487 /* The set of possible lid events */
3488 static struct sonypi_event sonypi_lidev[] = {
3489 	{ 0x51, SONYPI_EVENT_LID_CLOSED },
3490 	{ 0x50, SONYPI_EVENT_LID_OPENED },
3491 	{ 0, 0 }
3492 };
3493 
3494 /* The set of possible zoom events */
3495 static struct sonypi_event sonypi_zoomev[] = {
3496 	{ 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3497 	{ 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
3498 	{ 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
3499 	{ 0x04, SONYPI_EVENT_ZOOM_PRESSED },
3500 	{ 0, 0 }
3501 };
3502 
3503 /* The set of possible thumbphrase events */
3504 static struct sonypi_event sonypi_thumbphraseev[] = {
3505 	{ 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
3506 	{ 0, 0 }
3507 };
3508 
3509 /* The set of possible motioneye camera events */
3510 static struct sonypi_event sonypi_meyeev[] = {
3511 	{ 0x00, SONYPI_EVENT_MEYE_FACE },
3512 	{ 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
3513 	{ 0, 0 }
3514 };
3515 
3516 /* The set of possible memorystick events */
3517 static struct sonypi_event sonypi_memorystickev[] = {
3518 	{ 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
3519 	{ 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
3520 	{ 0, 0 }
3521 };
3522 
3523 /* The set of possible battery events */
3524 static struct sonypi_event sonypi_batteryev[] = {
3525 	{ 0x20, SONYPI_EVENT_BATTERY_INSERT },
3526 	{ 0x30, SONYPI_EVENT_BATTERY_REMOVE },
3527 	{ 0, 0 }
3528 };
3529 
3530 /* The set of possible volume events */
3531 static struct sonypi_event sonypi_volumeev[] = {
3532 	{ 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
3533 	{ 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
3534 	{ 0, 0 }
3535 };
3536 
3537 /* The set of possible brightness events */
3538 static struct sonypi_event sonypi_brightnessev[] = {
3539 	{ 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
3540 	{ 0, 0 }
3541 };
3542 
3543 static struct sonypi_eventtypes type1_events[] = {
3544 	{ 0, 0xffffffff, sonypi_releaseev },
3545 	{ 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
3546 	{ 0x30, SONYPI_LID_MASK, sonypi_lidev },
3547 	{ 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
3548 	{ 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
3549 	{ 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3550 	{ 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3551 	{ 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
3552 	{ 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3553 	{ 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
3554 	{ 0 },
3555 };
3556 static struct sonypi_eventtypes type2_events[] = {
3557 	{ 0, 0xffffffff, sonypi_releaseev },
3558 	{ 0x38, SONYPI_LID_MASK, sonypi_lidev },
3559 	{ 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
3560 	{ 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
3561 	{ 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3562 	{ 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
3563 	{ 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
3564 	{ 0x11, SONYPI_BACK_MASK, sonypi_backev },
3565 	{ 0x21, SONYPI_HELP_MASK, sonypi_helpev },
3566 	{ 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
3567 	{ 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
3568 	{ 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3569 	{ 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3570 	{ 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3571 	{ 0 },
3572 };
3573 static struct sonypi_eventtypes type3_events[] = {
3574 	{ 0, 0xffffffff, sonypi_releaseev },
3575 	{ 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
3576 	{ 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
3577 	{ 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
3578 	{ 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
3579 	{ 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3580 	{ 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
3581 	{ 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
3582 	{ 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
3583 	{ 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
3584 	{ 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3585 	{ 0 },
3586 };
3587 
3588 /* low level spic calls */
3589 #define ITERATIONS_LONG		10000
3590 #define ITERATIONS_SHORT	10
3591 #define wait_on_command(command, iterations) {				\
3592 	unsigned int n = iterations;					\
3593 	while (--n && (command))					\
3594 		udelay(1);						\
3595 	if (!n)								\
3596 		dprintk("command failed at %s : %s (line %d)\n",	\
3597 				__FILE__, __func__, __LINE__);	\
3598 }
3599 
sony_pic_call1(u8 dev)3600 static u8 sony_pic_call1(u8 dev)
3601 {
3602 	u8 v1, v2;
3603 
3604 	wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3605 			ITERATIONS_LONG);
3606 	outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3607 	v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
3608 	v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
3609 	dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
3610 	return v2;
3611 }
3612 
sony_pic_call2(u8 dev,u8 fn)3613 static u8 sony_pic_call2(u8 dev, u8 fn)
3614 {
3615 	u8 v1;
3616 
3617 	wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3618 			ITERATIONS_LONG);
3619 	outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
3620 	wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
3621 			ITERATIONS_LONG);
3622 	outb(fn, spic_dev.cur_ioport->io1.minimum);
3623 	v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
3624 	dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
3625 	return v1;
3626 }
3627 
3628 /*
3629  * minidrivers for SPIC models
3630  */
type3_handle_irq(const u8 data_mask,const u8 ev)3631 static int type3_handle_irq(const u8 data_mask, const u8 ev)
3632 {
3633 	/*
3634 	 * 0x31 could mean we have to take some extra action and wait for
3635 	 * the next irq for some Type3 models, it will generate a new
3636 	 * irq and we can read new data from the device:
3637 	 *  - 0x5c and 0x5f requires 0xA0
3638 	 *  - 0x61 requires 0xB3
3639 	 */
3640 	if (data_mask == 0x31) {
3641 		if (ev == 0x5c || ev == 0x5f)
3642 			sony_pic_call1(0xA0);
3643 		else if (ev == 0x61)
3644 			sony_pic_call1(0xB3);
3645 		return 0;
3646 	}
3647 	return 1;
3648 }
3649 
sony_pic_detect_device_type(struct sony_pic_dev * dev)3650 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
3651 {
3652 	struct pci_dev *pcidev;
3653 
3654 	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3655 			PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
3656 	if (pcidev) {
3657 		dev->model = SONYPI_DEVICE_TYPE1;
3658 		dev->evport_offset = SONYPI_TYPE1_OFFSET;
3659 		dev->event_types = type1_events;
3660 		goto out;
3661 	}
3662 
3663 	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3664 			PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
3665 	if (pcidev) {
3666 		dev->model = SONYPI_DEVICE_TYPE2;
3667 		dev->evport_offset = SONYPI_TYPE2_OFFSET;
3668 		dev->event_types = type2_events;
3669 		goto out;
3670 	}
3671 
3672 	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3673 			PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
3674 	if (pcidev) {
3675 		dev->model = SONYPI_DEVICE_TYPE3;
3676 		dev->handle_irq = type3_handle_irq;
3677 		dev->evport_offset = SONYPI_TYPE3_OFFSET;
3678 		dev->event_types = type3_events;
3679 		goto out;
3680 	}
3681 
3682 	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3683 			PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
3684 	if (pcidev) {
3685 		dev->model = SONYPI_DEVICE_TYPE3;
3686 		dev->handle_irq = type3_handle_irq;
3687 		dev->evport_offset = SONYPI_TYPE3_OFFSET;
3688 		dev->event_types = type3_events;
3689 		goto out;
3690 	}
3691 
3692 	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
3693 			PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
3694 	if (pcidev) {
3695 		dev->model = SONYPI_DEVICE_TYPE3;
3696 		dev->handle_irq = type3_handle_irq;
3697 		dev->evport_offset = SONYPI_TYPE3_OFFSET;
3698 		dev->event_types = type3_events;
3699 		goto out;
3700 	}
3701 
3702 	/* default */
3703 	dev->model = SONYPI_DEVICE_TYPE2;
3704 	dev->evport_offset = SONYPI_TYPE2_OFFSET;
3705 	dev->event_types = type2_events;
3706 
3707 out:
3708 	pci_dev_put(pcidev);
3709 
3710 	pr_info("detected Type%d model\n",
3711 		dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
3712 		dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3713 }
3714 
3715 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
__sony_pic_set_wwanpower(u8 state)3716 static void __sony_pic_set_wwanpower(u8 state)
3717 {
3718 	state = !!state;
3719 	if (spic_dev.wwan_power == state)
3720 		return;
3721 	sony_pic_call2(0xB0, state);
3722 	sony_pic_call1(0x82);
3723 	spic_dev.wwan_power = state;
3724 }
3725 
sony_pic_wwanpower_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)3726 static ssize_t sony_pic_wwanpower_store(struct device *dev,
3727 		struct device_attribute *attr,
3728 		const char *buffer, size_t count)
3729 {
3730 	unsigned long value;
3731 	if (count > 31)
3732 		return -EINVAL;
3733 
3734 	if (kstrtoul(buffer, 10, &value))
3735 		return -EINVAL;
3736 
3737 	mutex_lock(&spic_dev.lock);
3738 	__sony_pic_set_wwanpower(value);
3739 	mutex_unlock(&spic_dev.lock);
3740 
3741 	return count;
3742 }
3743 
sony_pic_wwanpower_show(struct device * dev,struct device_attribute * attr,char * buffer)3744 static ssize_t sony_pic_wwanpower_show(struct device *dev,
3745 		struct device_attribute *attr, char *buffer)
3746 {
3747 	ssize_t count;
3748 	mutex_lock(&spic_dev.lock);
3749 	count = sysfs_emit(buffer, "%d\n", spic_dev.wwan_power);
3750 	mutex_unlock(&spic_dev.lock);
3751 	return count;
3752 }
3753 
3754 /* bluetooth subsystem power state */
__sony_pic_set_bluetoothpower(u8 state)3755 static void __sony_pic_set_bluetoothpower(u8 state)
3756 {
3757 	state = !!state;
3758 	if (spic_dev.bluetooth_power == state)
3759 		return;
3760 	sony_pic_call2(0x96, state);
3761 	sony_pic_call1(0x82);
3762 	spic_dev.bluetooth_power = state;
3763 }
3764 
sony_pic_bluetoothpower_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)3765 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
3766 		struct device_attribute *attr,
3767 		const char *buffer, size_t count)
3768 {
3769 	unsigned long value;
3770 	if (count > 31)
3771 		return -EINVAL;
3772 
3773 	if (kstrtoul(buffer, 10, &value))
3774 		return -EINVAL;
3775 
3776 	mutex_lock(&spic_dev.lock);
3777 	__sony_pic_set_bluetoothpower(value);
3778 	mutex_unlock(&spic_dev.lock);
3779 
3780 	return count;
3781 }
3782 
sony_pic_bluetoothpower_show(struct device * dev,struct device_attribute * attr,char * buffer)3783 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
3784 		struct device_attribute *attr, char *buffer)
3785 {
3786 	ssize_t count = 0;
3787 	mutex_lock(&spic_dev.lock);
3788 	count = sysfs_emit(buffer, "%d\n", spic_dev.bluetooth_power);
3789 	mutex_unlock(&spic_dev.lock);
3790 	return count;
3791 }
3792 
3793 /* fan speed */
3794 /* FAN0 information (reverse engineered from ACPI tables) */
3795 #define SONY_PIC_FAN0_STATUS	0x93
sony_pic_set_fanspeed(unsigned long value)3796 static int sony_pic_set_fanspeed(unsigned long value)
3797 {
3798 	return ec_write(SONY_PIC_FAN0_STATUS, value);
3799 }
3800 
sony_pic_get_fanspeed(u8 * value)3801 static int sony_pic_get_fanspeed(u8 *value)
3802 {
3803 	return ec_read(SONY_PIC_FAN0_STATUS, value);
3804 }
3805 
sony_pic_fanspeed_store(struct device * dev,struct device_attribute * attr,const char * buffer,size_t count)3806 static ssize_t sony_pic_fanspeed_store(struct device *dev,
3807 		struct device_attribute *attr,
3808 		const char *buffer, size_t count)
3809 {
3810 	unsigned long value;
3811 	if (count > 31)
3812 		return -EINVAL;
3813 
3814 	if (kstrtoul(buffer, 10, &value))
3815 		return -EINVAL;
3816 
3817 	if (sony_pic_set_fanspeed(value))
3818 		return -EIO;
3819 
3820 	return count;
3821 }
3822 
sony_pic_fanspeed_show(struct device * dev,struct device_attribute * attr,char * buffer)3823 static ssize_t sony_pic_fanspeed_show(struct device *dev,
3824 		struct device_attribute *attr, char *buffer)
3825 {
3826 	u8 value = 0;
3827 	if (sony_pic_get_fanspeed(&value))
3828 		return -EIO;
3829 
3830 	return sysfs_emit(buffer, "%d\n", value);
3831 }
3832 
3833 #define SPIC_ATTR(_name, _mode)					\
3834 struct device_attribute spic_attr_##_name = __ATTR(_name,	\
3835 		_mode, sony_pic_## _name ##_show,		\
3836 		sony_pic_## _name ##_store)
3837 
3838 static SPIC_ATTR(bluetoothpower, 0644);
3839 static SPIC_ATTR(wwanpower, 0644);
3840 static SPIC_ATTR(fanspeed, 0644);
3841 
3842 static struct attribute *spic_attributes[] = {
3843 	&spic_attr_bluetoothpower.attr,
3844 	&spic_attr_wwanpower.attr,
3845 	&spic_attr_fanspeed.attr,
3846 	NULL
3847 };
3848 
3849 static const struct attribute_group spic_attribute_group = {
3850 	.attrs = spic_attributes
3851 };
3852 
3853 /******** SONYPI compatibility **********/
3854 #ifdef CONFIG_SONYPI_COMPAT
3855 
3856 /* battery / brightness / temperature  addresses */
3857 #define SONYPI_BAT_FLAGS	0x81
3858 #define SONYPI_LCD_LIGHT	0x96
3859 #define SONYPI_BAT1_PCTRM	0xa0
3860 #define SONYPI_BAT1_LEFT	0xa2
3861 #define SONYPI_BAT1_MAXRT	0xa4
3862 #define SONYPI_BAT2_PCTRM	0xa8
3863 #define SONYPI_BAT2_LEFT	0xaa
3864 #define SONYPI_BAT2_MAXRT	0xac
3865 #define SONYPI_BAT1_MAXTK	0xb0
3866 #define SONYPI_BAT1_FULL	0xb2
3867 #define SONYPI_BAT2_MAXTK	0xb8
3868 #define SONYPI_BAT2_FULL	0xba
3869 #define SONYPI_TEMP_STATUS	0xC1
3870 
3871 struct sonypi_compat_s {
3872 	struct fasync_struct	*fifo_async;
3873 	struct kfifo		fifo;
3874 	spinlock_t		fifo_lock;
3875 	wait_queue_head_t	fifo_proc_list;
3876 	atomic_t		open_count;
3877 };
3878 static struct sonypi_compat_s sonypi_compat = {
3879 	.open_count = ATOMIC_INIT(0),
3880 };
3881 
sonypi_misc_fasync(int fd,struct file * filp,int on)3882 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
3883 {
3884 	return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
3885 }
3886 
sonypi_misc_release(struct inode * inode,struct file * file)3887 static int sonypi_misc_release(struct inode *inode, struct file *file)
3888 {
3889 	atomic_dec(&sonypi_compat.open_count);
3890 	return 0;
3891 }
3892 
sonypi_misc_open(struct inode * inode,struct file * file)3893 static int sonypi_misc_open(struct inode *inode, struct file *file)
3894 {
3895 	/* Flush input queue on first open */
3896 	unsigned long flags;
3897 
3898 	spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
3899 
3900 	if (atomic_inc_return(&sonypi_compat.open_count) == 1)
3901 		kfifo_reset(&sonypi_compat.fifo);
3902 
3903 	spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
3904 
3905 	return 0;
3906 }
3907 
sonypi_misc_read(struct file * file,char __user * buf,size_t count,loff_t * pos)3908 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
3909 				size_t count, loff_t *pos)
3910 {
3911 	ssize_t ret;
3912 	unsigned char c;
3913 
3914 	if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
3915 	    (file->f_flags & O_NONBLOCK))
3916 		return -EAGAIN;
3917 
3918 	ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
3919 				       kfifo_len(&sonypi_compat.fifo) != 0);
3920 	if (ret)
3921 		return ret;
3922 
3923 	while (ret < count &&
3924 	       (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
3925 			  &sonypi_compat.fifo_lock) == sizeof(c))) {
3926 		if (put_user(c, buf++))
3927 			return -EFAULT;
3928 		ret++;
3929 	}
3930 
3931 	if (ret > 0) {
3932 		struct inode *inode = file_inode(file);
3933 		inode_set_atime_to_ts(inode, current_time(inode));
3934 	}
3935 
3936 	return ret;
3937 }
3938 
sonypi_misc_poll(struct file * file,poll_table * wait)3939 static __poll_t sonypi_misc_poll(struct file *file, poll_table *wait)
3940 {
3941 	poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
3942 	if (kfifo_len(&sonypi_compat.fifo))
3943 		return EPOLLIN | EPOLLRDNORM;
3944 	return 0;
3945 }
3946 
ec_read16(u8 addr,u16 * value)3947 static int ec_read16(u8 addr, u16 *value)
3948 {
3949 	u8 val_lb, val_hb;
3950 	if (ec_read(addr, &val_lb))
3951 		return -1;
3952 	if (ec_read(addr + 1, &val_hb))
3953 		return -1;
3954 	*value = val_lb | (val_hb << 8);
3955 	return 0;
3956 }
3957 
sonypi_misc_ioctl(struct file * fp,unsigned int cmd,unsigned long arg)3958 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
3959 							unsigned long arg)
3960 {
3961 	int ret = 0;
3962 	void __user *argp = (void __user *)arg;
3963 	u8 val8;
3964 	u16 val16;
3965 	int value;
3966 
3967 	mutex_lock(&spic_dev.lock);
3968 	switch (cmd) {
3969 	case SONYPI_IOCGBRT:
3970 		if (sony_bl_props.dev == NULL) {
3971 			ret = -EIO;
3972 			break;
3973 		}
3974 		if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
3975 					&value)) {
3976 			ret = -EIO;
3977 			break;
3978 		}
3979 		val8 = ((value & 0xff) - 1) << 5;
3980 		if (copy_to_user(argp, &val8, sizeof(val8)))
3981 				ret = -EFAULT;
3982 		break;
3983 	case SONYPI_IOCSBRT:
3984 		if (sony_bl_props.dev == NULL) {
3985 			ret = -EIO;
3986 			break;
3987 		}
3988 		if (copy_from_user(&val8, argp, sizeof(val8))) {
3989 			ret = -EFAULT;
3990 			break;
3991 		}
3992 		value = (val8 >> 5) + 1;
3993 		if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
3994 					NULL)) {
3995 			ret = -EIO;
3996 			break;
3997 		}
3998 		/* sync the backlight device status */
3999 		sony_bl_props.dev->props.brightness =
4000 		    sony_backlight_get_brightness(sony_bl_props.dev);
4001 		break;
4002 	case SONYPI_IOCGBAT1CAP:
4003 		if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
4004 			ret = -EIO;
4005 			break;
4006 		}
4007 		if (copy_to_user(argp, &val16, sizeof(val16)))
4008 			ret = -EFAULT;
4009 		break;
4010 	case SONYPI_IOCGBAT1REM:
4011 		if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
4012 			ret = -EIO;
4013 			break;
4014 		}
4015 		if (copy_to_user(argp, &val16, sizeof(val16)))
4016 			ret = -EFAULT;
4017 		break;
4018 	case SONYPI_IOCGBAT2CAP:
4019 		if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
4020 			ret = -EIO;
4021 			break;
4022 		}
4023 		if (copy_to_user(argp, &val16, sizeof(val16)))
4024 			ret = -EFAULT;
4025 		break;
4026 	case SONYPI_IOCGBAT2REM:
4027 		if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
4028 			ret = -EIO;
4029 			break;
4030 		}
4031 		if (copy_to_user(argp, &val16, sizeof(val16)))
4032 			ret = -EFAULT;
4033 		break;
4034 	case SONYPI_IOCGBATFLAGS:
4035 		if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
4036 			ret = -EIO;
4037 			break;
4038 		}
4039 		val8 &= 0x07;
4040 		if (copy_to_user(argp, &val8, sizeof(val8)))
4041 			ret = -EFAULT;
4042 		break;
4043 	case SONYPI_IOCGBLUE:
4044 		val8 = spic_dev.bluetooth_power;
4045 		if (copy_to_user(argp, &val8, sizeof(val8)))
4046 			ret = -EFAULT;
4047 		break;
4048 	case SONYPI_IOCSBLUE:
4049 		if (copy_from_user(&val8, argp, sizeof(val8))) {
4050 			ret = -EFAULT;
4051 			break;
4052 		}
4053 		__sony_pic_set_bluetoothpower(val8);
4054 		break;
4055 	/* FAN Controls */
4056 	case SONYPI_IOCGFAN:
4057 		if (sony_pic_get_fanspeed(&val8)) {
4058 			ret = -EIO;
4059 			break;
4060 		}
4061 		if (copy_to_user(argp, &val8, sizeof(val8)))
4062 			ret = -EFAULT;
4063 		break;
4064 	case SONYPI_IOCSFAN:
4065 		if (copy_from_user(&val8, argp, sizeof(val8))) {
4066 			ret = -EFAULT;
4067 			break;
4068 		}
4069 		if (sony_pic_set_fanspeed(val8))
4070 			ret = -EIO;
4071 		break;
4072 	/* GET Temperature (useful under APM) */
4073 	case SONYPI_IOCGTEMP:
4074 		if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
4075 			ret = -EIO;
4076 			break;
4077 		}
4078 		if (copy_to_user(argp, &val8, sizeof(val8)))
4079 			ret = -EFAULT;
4080 		break;
4081 	default:
4082 		ret = -EINVAL;
4083 	}
4084 	mutex_unlock(&spic_dev.lock);
4085 	return ret;
4086 }
4087 
4088 static const struct file_operations sonypi_misc_fops = {
4089 	.owner		= THIS_MODULE,
4090 	.read		= sonypi_misc_read,
4091 	.poll		= sonypi_misc_poll,
4092 	.open		= sonypi_misc_open,
4093 	.release	= sonypi_misc_release,
4094 	.fasync		= sonypi_misc_fasync,
4095 	.unlocked_ioctl	= sonypi_misc_ioctl,
4096 	.llseek		= noop_llseek,
4097 };
4098 
4099 static struct miscdevice sonypi_misc_device = {
4100 	.minor		= MISC_DYNAMIC_MINOR,
4101 	.name		= "sonypi",
4102 	.fops		= &sonypi_misc_fops,
4103 };
4104 
sonypi_compat_report_event(u8 event)4105 static void sonypi_compat_report_event(u8 event)
4106 {
4107 	kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
4108 			sizeof(event), &sonypi_compat.fifo_lock);
4109 	kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
4110 	wake_up_interruptible(&sonypi_compat.fifo_proc_list);
4111 }
4112 
sonypi_compat_init(void)4113 static int sonypi_compat_init(void)
4114 {
4115 	int error;
4116 
4117 	spin_lock_init(&sonypi_compat.fifo_lock);
4118 	error =
4119 	 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
4120 	if (error) {
4121 		pr_err("kfifo_alloc failed\n");
4122 		return error;
4123 	}
4124 
4125 	init_waitqueue_head(&sonypi_compat.fifo_proc_list);
4126 
4127 	if (minor != -1)
4128 		sonypi_misc_device.minor = minor;
4129 	error = misc_register(&sonypi_misc_device);
4130 	if (error) {
4131 		pr_err("misc_register failed\n");
4132 		goto err_free_kfifo;
4133 	}
4134 	if (minor == -1)
4135 		pr_info("device allocated minor is %d\n",
4136 			sonypi_misc_device.minor);
4137 
4138 	return 0;
4139 
4140 err_free_kfifo:
4141 	kfifo_free(&sonypi_compat.fifo);
4142 	return error;
4143 }
4144 
sonypi_compat_exit(void)4145 static void sonypi_compat_exit(void)
4146 {
4147 	misc_deregister(&sonypi_misc_device);
4148 	kfifo_free(&sonypi_compat.fifo);
4149 }
4150 #else
sonypi_compat_init(void)4151 static int sonypi_compat_init(void) { return 0; }
sonypi_compat_exit(void)4152 static void sonypi_compat_exit(void) { }
sonypi_compat_report_event(u8 event)4153 static void sonypi_compat_report_event(u8 event) { }
4154 #endif /* CONFIG_SONYPI_COMPAT */
4155 
4156 /*
4157  * ACPI callbacks
4158  */
4159 static acpi_status
sony_pic_read_possible_resource(struct acpi_resource * resource,void * context)4160 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
4161 {
4162 	u32 i;
4163 	struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
4164 
4165 	switch (resource->type) {
4166 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
4167 		{
4168 			/* start IO enumeration */
4169 			struct sony_pic_ioport *ioport = kzalloc_obj(*ioport);
4170 			if (!ioport)
4171 				return AE_ERROR;
4172 
4173 			list_add(&ioport->list, &dev->ioports);
4174 			return AE_OK;
4175 		}
4176 
4177 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
4178 		/* end IO enumeration */
4179 		return AE_OK;
4180 
4181 	case ACPI_RESOURCE_TYPE_IRQ:
4182 		{
4183 			struct acpi_resource_irq *p = &resource->data.irq;
4184 			struct sony_pic_irq *interrupt = NULL;
4185 			if (!p->interrupt_count) {
4186 				/*
4187 				 * IRQ descriptors may have no IRQ# bits set,
4188 				 * particularly those those w/ _STA disabled
4189 				 */
4190 				dprintk("Blank IRQ resource\n");
4191 				return AE_OK;
4192 			}
4193 			for (i = 0; i < p->interrupt_count; i++) {
4194 				if (!p->interrupts[i]) {
4195 					pr_warn("Invalid IRQ %d\n",
4196 						p->interrupts[i]);
4197 					continue;
4198 				}
4199 				interrupt = kzalloc_obj(*interrupt);
4200 				if (!interrupt)
4201 					return AE_ERROR;
4202 
4203 				list_add(&interrupt->list, &dev->interrupts);
4204 				interrupt->irq.triggering = p->triggering;
4205 				interrupt->irq.polarity = p->polarity;
4206 				interrupt->irq.shareable = p->shareable;
4207 				interrupt->irq.interrupt_count = 1;
4208 				interrupt->irq.interrupts[0] = p->interrupts[i];
4209 			}
4210 			return AE_OK;
4211 		}
4212 	case ACPI_RESOURCE_TYPE_IO:
4213 		{
4214 			struct acpi_resource_io *io = &resource->data.io;
4215 			struct sony_pic_ioport *ioport =
4216 				list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
4217 			if (!ioport->io1.minimum) {
4218 				memcpy(&ioport->io1, io, sizeof(*io));
4219 				dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
4220 						ioport->io1.address_length);
4221 			}
4222 			else if (!ioport->io2.minimum) {
4223 				memcpy(&ioport->io2, io, sizeof(*io));
4224 				dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
4225 						ioport->io2.address_length);
4226 			}
4227 			else {
4228 				pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
4229 				return AE_ERROR;
4230 			}
4231 			return AE_OK;
4232 		}
4233 
4234 	case ACPI_RESOURCE_TYPE_END_TAG:
4235 		return AE_OK;
4236 
4237 	default:
4238 		dprintk("Resource %d isn't an IRQ nor an IO port\n",
4239 			resource->type);
4240 		return AE_CTRL_TERMINATE;
4241 
4242 	}
4243 }
4244 
sony_pic_possible_resources(struct acpi_device * device)4245 static int sony_pic_possible_resources(struct acpi_device *device)
4246 {
4247 	int result = 0;
4248 	acpi_status status = AE_OK;
4249 
4250 	if (!device)
4251 		return -EINVAL;
4252 
4253 	/* get device status */
4254 	/* see acpi_pci_link_get_current acpi_pci_link_get_possible */
4255 	dprintk("Evaluating _STA\n");
4256 	result = acpi_bus_get_status(device);
4257 	if (result) {
4258 		pr_warn("Unable to read status\n");
4259 		goto end;
4260 	}
4261 
4262 	if (!device->status.enabled)
4263 		dprintk("Device disabled\n");
4264 	else
4265 		dprintk("Device enabled\n");
4266 
4267 	/*
4268 	 * Query and parse 'method'
4269 	 */
4270 	dprintk("Evaluating %s\n", METHOD_NAME__PRS);
4271 	status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
4272 			sony_pic_read_possible_resource, &spic_dev);
4273 	if (ACPI_FAILURE(status)) {
4274 		pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
4275 		result = -ENODEV;
4276 	}
4277 end:
4278 	return result;
4279 }
4280 
4281 /*
4282  *  Disable the spic device by calling its _DIS method
4283  */
sony_pic_disable(struct device * dev)4284 static int sony_pic_disable(struct device *dev)
4285 {
4286 	acpi_status ret = acpi_evaluate_object(ACPI_HANDLE(dev), "_DIS", NULL,
4287 					       NULL);
4288 
4289 	if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
4290 		return -ENXIO;
4291 
4292 	dprintk("Device disabled\n");
4293 	return 0;
4294 }
4295 
4296 
4297 /*
4298  *  Based on drivers/acpi/pci_link.c:acpi_pci_link_set
4299  *
4300  *  Call _SRS to set current resources
4301  */
sony_pic_enable(struct device * dev,struct sony_pic_ioport * ioport,struct sony_pic_irq * irq)4302 static int sony_pic_enable(struct device *dev,
4303 		struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
4304 {
4305 	acpi_status status;
4306 	int result = 0;
4307 	/* Type 1 resource layout is:
4308 	 *    IO
4309 	 *    IO
4310 	 *    IRQNoFlags
4311 	 *    End
4312 	 *
4313 	 * Type 2 and 3 resource layout is:
4314 	 *    IO
4315 	 *    IRQNoFlags
4316 	 *    End
4317 	 */
4318 	struct {
4319 		struct acpi_resource res1;
4320 		struct acpi_resource res2;
4321 		struct acpi_resource res3;
4322 		struct acpi_resource res4;
4323 	} *resource;
4324 	struct acpi_buffer buffer = { 0, NULL };
4325 
4326 	if (!ioport || !irq)
4327 		return -EINVAL;
4328 
4329 	/* init acpi_buffer */
4330 	resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
4331 	if (!resource)
4332 		return -ENOMEM;
4333 
4334 	buffer.length = sizeof(*resource) + 1;
4335 	buffer.pointer = resource;
4336 
4337 	/* setup Type 1 resources */
4338 	if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
4339 
4340 		/* setup io resources */
4341 		resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4342 		resource->res1.length = sizeof(struct acpi_resource);
4343 		memcpy(&resource->res1.data.io, &ioport->io1,
4344 				sizeof(struct acpi_resource_io));
4345 
4346 		resource->res2.type = ACPI_RESOURCE_TYPE_IO;
4347 		resource->res2.length = sizeof(struct acpi_resource);
4348 		memcpy(&resource->res2.data.io, &ioport->io2,
4349 				sizeof(struct acpi_resource_io));
4350 
4351 		/* setup irq resource */
4352 		resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
4353 		resource->res3.length = sizeof(struct acpi_resource);
4354 		memcpy(&resource->res3.data.irq, &irq->irq,
4355 				sizeof(struct acpi_resource_irq));
4356 		/* we requested a shared irq */
4357 		resource->res3.data.irq.shareable = ACPI_SHARED;
4358 
4359 		resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
4360 		resource->res4.length = sizeof(struct acpi_resource);
4361 	}
4362 	/* setup Type 2/3 resources */
4363 	else {
4364 		/* setup io resource */
4365 		resource->res1.type = ACPI_RESOURCE_TYPE_IO;
4366 		resource->res1.length = sizeof(struct acpi_resource);
4367 		memcpy(&resource->res1.data.io, &ioport->io1,
4368 				sizeof(struct acpi_resource_io));
4369 
4370 		/* setup irq resource */
4371 		resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
4372 		resource->res2.length = sizeof(struct acpi_resource);
4373 		memcpy(&resource->res2.data.irq, &irq->irq,
4374 				sizeof(struct acpi_resource_irq));
4375 		/* we requested a shared irq */
4376 		resource->res2.data.irq.shareable = ACPI_SHARED;
4377 
4378 		resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
4379 		resource->res3.length = sizeof(struct acpi_resource);
4380 	}
4381 
4382 	/* Attempt to set the resource */
4383 	dprintk("Evaluating _SRS\n");
4384 	status = acpi_set_current_resources(ACPI_HANDLE(dev), &buffer);
4385 
4386 	/* check for total failure */
4387 	if (ACPI_FAILURE(status)) {
4388 		pr_err("Error evaluating _SRS\n");
4389 		result = -ENODEV;
4390 		goto end;
4391 	}
4392 
4393 	/* Necessary device initializations calls (from sonypi) */
4394 	sony_pic_call1(0x82);
4395 	sony_pic_call2(0x81, 0xff);
4396 	sony_pic_call1(compat ? 0x92 : 0x82);
4397 
4398 end:
4399 	kfree(resource);
4400 	return result;
4401 }
4402 
4403 /*****************
4404  *
4405  * ISR: some event is available
4406  *
4407  *****************/
sony_pic_irq(int irq,void * dev_id)4408 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
4409 {
4410 	int i, j;
4411 	u8 ev = 0;
4412 	u8 data_mask = 0;
4413 	u8 device_event = 0;
4414 
4415 	struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
4416 
4417 	ev = inb_p(dev->cur_ioport->io1.minimum);
4418 	if (dev->cur_ioport->io2.minimum)
4419 		data_mask = inb_p(dev->cur_ioport->io2.minimum);
4420 	else
4421 		data_mask = inb_p(dev->cur_ioport->io1.minimum +
4422 				dev->evport_offset);
4423 
4424 	dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4425 			ev, data_mask, dev->cur_ioport->io1.minimum,
4426 			dev->evport_offset);
4427 
4428 	if (ev == 0x00 || ev == 0xff)
4429 		return IRQ_HANDLED;
4430 
4431 	for (i = 0; dev->event_types[i].mask; i++) {
4432 
4433 		if ((data_mask & dev->event_types[i].data) !=
4434 		    dev->event_types[i].data)
4435 			continue;
4436 
4437 		if (!(mask & dev->event_types[i].mask))
4438 			continue;
4439 
4440 		for (j = 0; dev->event_types[i].events[j].event; j++) {
4441 			if (ev == dev->event_types[i].events[j].data) {
4442 				device_event =
4443 					dev->event_types[i].events[j].event;
4444 				/* some events may require ignoring */
4445 				if (!device_event)
4446 					return IRQ_HANDLED;
4447 				goto found;
4448 			}
4449 		}
4450 	}
4451 	/* Still not able to decode the event try to pass
4452 	 * it over to the minidriver
4453 	 */
4454 	if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
4455 		return IRQ_HANDLED;
4456 
4457 	dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
4458 			ev, data_mask, dev->cur_ioport->io1.minimum,
4459 			dev->evport_offset);
4460 	return IRQ_HANDLED;
4461 
4462 found:
4463 	sony_laptop_report_input_event(device_event);
4464 	sonypi_compat_report_event(device_event);
4465 	return IRQ_HANDLED;
4466 }
4467 
4468 /*****************
4469  *
4470  *  ACPI driver
4471  *
4472  *****************/
sony_pic_remove(struct platform_device * pdev)4473 static void sony_pic_remove(struct platform_device *pdev)
4474 {
4475 	struct sony_pic_ioport *io, *tmp_io;
4476 	struct sony_pic_irq *irq, *tmp_irq;
4477 
4478 	if (sony_pic_disable(&pdev->dev)) {
4479 		pr_err("Couldn't disable device\n");
4480 		return;
4481 	}
4482 
4483 	free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4484 	release_region(spic_dev.cur_ioport->io1.minimum,
4485 			spic_dev.cur_ioport->io1.address_length);
4486 	if (spic_dev.cur_ioport->io2.minimum)
4487 		release_region(spic_dev.cur_ioport->io2.minimum,
4488 				spic_dev.cur_ioport->io2.address_length);
4489 
4490 	sonypi_compat_exit();
4491 
4492 	sony_laptop_remove_input();
4493 
4494 	/* pf attrs */
4495 	sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4496 	sony_pf_remove();
4497 
4498 	list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4499 		list_del(&io->list);
4500 		kfree(io);
4501 	}
4502 	list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4503 		list_del(&irq->list);
4504 		kfree(irq);
4505 	}
4506 	spic_dev.cur_ioport = NULL;
4507 	spic_dev.cur_irq = NULL;
4508 
4509 	dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
4510 }
4511 
sony_pic_probe(struct platform_device * pdev)4512 static int sony_pic_probe(struct platform_device *pdev)
4513 {
4514 	struct sony_pic_ioport *io, *tmp_io;
4515 	struct sony_pic_irq *irq, *tmp_irq;
4516 	struct acpi_device *device;
4517 	int result;
4518 
4519 	device = ACPI_COMPANION(&pdev->dev);
4520 	if (!device)
4521 		return -ENODEV;
4522 
4523 	spic_dev.acpi_dev = device;
4524 	sony_pic_detect_device_type(&spic_dev);
4525 	mutex_init(&spic_dev.lock);
4526 
4527 	/* read _PRS resources */
4528 	result = sony_pic_possible_resources(device);
4529 	if (result) {
4530 		pr_err("Unable to read possible resources\n");
4531 		goto err_free_resources;
4532 	}
4533 
4534 	/* setup input devices and helper fifo */
4535 	result = sony_laptop_setup_input(&pdev->dev);
4536 	if (result) {
4537 		pr_err("Unable to create input devices\n");
4538 		goto err_free_resources;
4539 	}
4540 
4541 	result = sonypi_compat_init();
4542 	if (result)
4543 		goto err_remove_input;
4544 
4545 	/* request io port */
4546 	list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
4547 		if (request_region(io->io1.minimum, io->io1.address_length,
4548 					"Sony Programmable I/O Device")) {
4549 			dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
4550 					io->io1.minimum, io->io1.maximum,
4551 					io->io1.address_length);
4552 			/* Type 1 have 2 ioports */
4553 			if (io->io2.minimum) {
4554 				if (request_region(io->io2.minimum,
4555 						io->io2.address_length,
4556 						"Sony Programmable I/O Device")) {
4557 					dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
4558 							io->io2.minimum, io->io2.maximum,
4559 							io->io2.address_length);
4560 					spic_dev.cur_ioport = io;
4561 					break;
4562 				}
4563 				else {
4564 					dprintk("Unable to get I/O port2: "
4565 							"0x%.4x (0x%.4x) + 0x%.2x\n",
4566 							io->io2.minimum, io->io2.maximum,
4567 							io->io2.address_length);
4568 					release_region(io->io1.minimum,
4569 							io->io1.address_length);
4570 				}
4571 			}
4572 			else {
4573 				spic_dev.cur_ioport = io;
4574 				break;
4575 			}
4576 		}
4577 	}
4578 	if (!spic_dev.cur_ioport) {
4579 		pr_err("Failed to request_region\n");
4580 		result = -ENODEV;
4581 		goto err_remove_compat;
4582 	}
4583 
4584 	/* request IRQ */
4585 	list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
4586 		if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
4587 					0, "sony-laptop", &spic_dev)) {
4588 			dprintk("IRQ: %d - triggering: %d - "
4589 					"polarity: %d - shr: %d\n",
4590 					irq->irq.interrupts[0],
4591 					irq->irq.triggering,
4592 					irq->irq.polarity,
4593 					irq->irq.shareable);
4594 			spic_dev.cur_irq = irq;
4595 			break;
4596 		}
4597 	}
4598 	if (!spic_dev.cur_irq) {
4599 		pr_err("Failed to request_irq\n");
4600 		result = -ENODEV;
4601 		goto err_release_region;
4602 	}
4603 
4604 	/* set resource status _SRS */
4605 	result = sony_pic_enable(&pdev->dev, spic_dev.cur_ioport, spic_dev.cur_irq);
4606 	if (result) {
4607 		pr_err("Couldn't enable device\n");
4608 		goto err_free_irq;
4609 	}
4610 
4611 	spic_dev.bluetooth_power = -1;
4612 	/* create device attributes */
4613 	result = sony_pf_add();
4614 	if (result)
4615 		goto err_disable_device;
4616 
4617 	result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
4618 	if (result)
4619 		goto err_remove_pf;
4620 
4621 	pr_info("SPIC setup done.\n");
4622 	return 0;
4623 
4624 err_remove_pf:
4625 	sony_pf_remove();
4626 
4627 err_disable_device:
4628 	sony_pic_disable(&pdev->dev);
4629 
4630 err_free_irq:
4631 	free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
4632 
4633 err_release_region:
4634 	release_region(spic_dev.cur_ioport->io1.minimum,
4635 			spic_dev.cur_ioport->io1.address_length);
4636 	if (spic_dev.cur_ioport->io2.minimum)
4637 		release_region(spic_dev.cur_ioport->io2.minimum,
4638 				spic_dev.cur_ioport->io2.address_length);
4639 
4640 err_remove_compat:
4641 	sonypi_compat_exit();
4642 
4643 err_remove_input:
4644 	sony_laptop_remove_input();
4645 
4646 err_free_resources:
4647 	list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
4648 		list_del(&io->list);
4649 		kfree(io);
4650 	}
4651 	list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
4652 		list_del(&irq->list);
4653 		kfree(irq);
4654 	}
4655 	spic_dev.cur_ioport = NULL;
4656 	spic_dev.cur_irq = NULL;
4657 
4658 	return result;
4659 }
4660 
4661 #ifdef CONFIG_PM_SLEEP
sony_pic_suspend(struct device * dev)4662 static int sony_pic_suspend(struct device *dev)
4663 {
4664 	if (sony_pic_disable(dev))
4665 		return -ENXIO;
4666 	return 0;
4667 }
4668 
sony_pic_resume(struct device * dev)4669 static int sony_pic_resume(struct device *dev)
4670 {
4671 	sony_pic_enable(dev, spic_dev.cur_ioport, spic_dev.cur_irq);
4672 	return 0;
4673 }
4674 #endif
4675 
4676 static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
4677 
4678 static const struct acpi_device_id sony_pic_device_ids[] = {
4679 	{SONY_PIC_HID, 0},
4680 	{"", 0},
4681 };
4682 
4683 static struct platform_driver sony_pic_driver = {
4684 	.probe = sony_pic_probe,
4685 	.remove = sony_pic_remove,
4686 	.driver = {
4687 		.name = SONY_PIC_DRIVER_NAME,
4688 		.acpi_match_table = sony_pic_device_ids,
4689 		.pm = &sony_pic_pm,
4690 	},
4691 };
4692 
4693 static const struct dmi_system_id sonypi_dmi_table[] __initconst = {
4694 	{
4695 		.ident = "Sony Vaio",
4696 		.matches = {
4697 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4698 			DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
4699 		},
4700 	},
4701 	{
4702 		.ident = "Sony Vaio",
4703 		.matches = {
4704 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
4705 			DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
4706 		},
4707 	},
4708 	{ }
4709 };
4710 
sony_laptop_init(void)4711 static int __init sony_laptop_init(void)
4712 {
4713 	int result;
4714 
4715 	if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
4716 		result = platform_driver_register(&sony_pic_driver);
4717 		if (result) {
4718 			pr_err("Unable to register SPIC driver\n");
4719 			goto out;
4720 		}
4721 		spic_drv_registered = 1;
4722 	}
4723 
4724 	result = platform_driver_register(&sony_nc_driver);
4725 	if (result) {
4726 		pr_err("Unable to register SNC driver\n");
4727 		goto out_unregister_pic;
4728 	}
4729 
4730 	return 0;
4731 
4732 out_unregister_pic:
4733 	if (spic_drv_registered)
4734 		platform_driver_unregister(&sony_pic_driver);
4735 out:
4736 	return result;
4737 }
4738 
sony_laptop_exit(void)4739 static void __exit sony_laptop_exit(void)
4740 {
4741 	platform_driver_unregister(&sony_nc_driver);
4742 	if (spic_drv_registered)
4743 		platform_driver_unregister(&sony_pic_driver);
4744 }
4745 
4746 module_init(sony_laptop_init);
4747 module_exit(sony_laptop_exit);
4748