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