xref: /linux/drivers/char/sonypi.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Sony Programmable I/O Control Device driver for VAIO
4  *
5  * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
6  *
7  * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
8  *
9  * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
10  *
11  * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
12  *
13  * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
14  *
15  * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
16  *
17  * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
18  *
19  * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
20  *
21  * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
22  */
23 
24 #include <linux/module.h>
25 #include <linux/sched.h>
26 #include <linux/input.h>
27 #include <linux/pci.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/miscdevice.h>
31 #include <linux/poll.h>
32 #include <linux/delay.h>
33 #include <linux/wait.h>
34 #include <linux/acpi.h>
35 #include <linux/dmi.h>
36 #include <linux/err.h>
37 #include <linux/kfifo.h>
38 #include <linux/platform_device.h>
39 #include <linux/gfp.h>
40 
41 #include <linux/uaccess.h>
42 #include <asm/io.h>
43 
44 #include <linux/sonypi.h>
45 
46 #define SONYPI_DRIVER_VERSION	 "1.26"
47 
48 MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
49 MODULE_DESCRIPTION("Sony Programmable I/O Control Device driver");
50 MODULE_LICENSE("GPL");
51 MODULE_VERSION(SONYPI_DRIVER_VERSION);
52 
53 static int minor = -1;
54 module_param(minor, int, 0);
55 MODULE_PARM_DESC(minor,
56 		 "minor number of the misc device, default is -1 (automatic)");
57 
58 static int verbose;		/* = 0 */
59 module_param(verbose, int, 0644);
60 MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
61 
62 static int fnkeyinit;		/* = 0 */
63 module_param(fnkeyinit, int, 0444);
64 MODULE_PARM_DESC(fnkeyinit,
65 		 "set this if your Fn keys do not generate any event");
66 
67 static int camera;		/* = 0 */
68 module_param(camera, int, 0444);
69 MODULE_PARM_DESC(camera,
70 		 "set this if you have a MotionEye camera (PictureBook series)");
71 
72 static int compat;		/* = 0 */
73 module_param(compat, int, 0444);
74 MODULE_PARM_DESC(compat,
75 		 "set this if you want to enable backward compatibility mode");
76 
77 static unsigned long mask = 0xffffffff;
78 module_param(mask, ulong, 0644);
79 MODULE_PARM_DESC(mask,
80 		 "set this to the mask of event you want to enable (see doc)");
81 
82 static int useinput = 1;
83 module_param(useinput, int, 0444);
84 MODULE_PARM_DESC(useinput,
85 		 "set this if you would like sonypi to feed events to the input subsystem");
86 
87 static int check_ioport = 1;
88 module_param(check_ioport, int, 0444);
89 MODULE_PARM_DESC(check_ioport,
90 		 "set this to 0 if you think the automatic ioport check for sony-laptop is wrong");
91 
92 #define SONYPI_DEVICE_MODEL_TYPE1	1
93 #define SONYPI_DEVICE_MODEL_TYPE2	2
94 #define SONYPI_DEVICE_MODEL_TYPE3	3
95 
96 /* type1 models use those */
97 #define SONYPI_IRQ_PORT			0x8034
98 #define SONYPI_IRQ_SHIFT		22
99 #define SONYPI_TYPE1_BASE		0x50
100 #define SONYPI_G10A			(SONYPI_TYPE1_BASE+0x14)
101 #define SONYPI_TYPE1_REGION_SIZE	0x08
102 #define SONYPI_TYPE1_EVTYPE_OFFSET	0x04
103 
104 /* type2 series specifics */
105 #define SONYPI_SIRQ			0x9b
106 #define SONYPI_SLOB			0x9c
107 #define SONYPI_SHIB			0x9d
108 #define SONYPI_TYPE2_REGION_SIZE	0x20
109 #define SONYPI_TYPE2_EVTYPE_OFFSET	0x12
110 
111 /* type3 series specifics */
112 #define SONYPI_TYPE3_BASE		0x40
113 #define SONYPI_TYPE3_GID2		(SONYPI_TYPE3_BASE+0x48) /* 16 bits */
114 #define SONYPI_TYPE3_MISC		(SONYPI_TYPE3_BASE+0x6d) /* 8 bits  */
115 #define SONYPI_TYPE3_REGION_SIZE	0x20
116 #define SONYPI_TYPE3_EVTYPE_OFFSET	0x12
117 
118 /* battery / brightness addresses */
119 #define SONYPI_BAT_FLAGS	0x81
120 #define SONYPI_LCD_LIGHT	0x96
121 #define SONYPI_BAT1_PCTRM	0xa0
122 #define SONYPI_BAT1_LEFT	0xa2
123 #define SONYPI_BAT1_MAXRT	0xa4
124 #define SONYPI_BAT2_PCTRM	0xa8
125 #define SONYPI_BAT2_LEFT	0xaa
126 #define SONYPI_BAT2_MAXRT	0xac
127 #define SONYPI_BAT1_MAXTK	0xb0
128 #define SONYPI_BAT1_FULL	0xb2
129 #define SONYPI_BAT2_MAXTK	0xb8
130 #define SONYPI_BAT2_FULL	0xba
131 
132 /* FAN0 information (reverse engineered from ACPI tables) */
133 #define SONYPI_FAN0_STATUS	0x93
134 #define SONYPI_TEMP_STATUS	0xC1
135 
136 /* ioports used for brightness and type2 events */
137 #define SONYPI_DATA_IOPORT	0x62
138 #define SONYPI_CST_IOPORT	0x66
139 
140 /* The set of possible ioports */
141 struct sonypi_ioport_list {
142 	u16	port1;
143 	u16	port2;
144 };
145 
146 static struct sonypi_ioport_list sonypi_type1_ioport_list[] = {
147 	{ 0x10c0, 0x10c4 },	/* looks like the default on C1Vx */
148 	{ 0x1080, 0x1084 },
149 	{ 0x1090, 0x1094 },
150 	{ 0x10a0, 0x10a4 },
151 	{ 0x10b0, 0x10b4 },
152 	{ 0x0, 0x0 }
153 };
154 
155 static struct sonypi_ioport_list sonypi_type2_ioport_list[] = {
156 	{ 0x1080, 0x1084 },
157 	{ 0x10a0, 0x10a4 },
158 	{ 0x10c0, 0x10c4 },
159 	{ 0x10e0, 0x10e4 },
160 	{ 0x0, 0x0 }
161 };
162 
163 /* same as in type 2 models */
164 static struct sonypi_ioport_list *sonypi_type3_ioport_list =
165 	sonypi_type2_ioport_list;
166 
167 /* The set of possible interrupts */
168 struct sonypi_irq_list {
169 	u16	irq;
170 	u16	bits;
171 };
172 
173 static struct sonypi_irq_list sonypi_type1_irq_list[] = {
174 	{ 11, 0x2 },	/* IRQ 11, GO22=0,GO23=1 in AML */
175 	{ 10, 0x1 },	/* IRQ 10, GO22=1,GO23=0 in AML */
176 	{  5, 0x0 },	/* IRQ  5, GO22=0,GO23=0 in AML */
177 	{  0, 0x3 }	/* no IRQ, GO22=1,GO23=1 in AML */
178 };
179 
180 static struct sonypi_irq_list sonypi_type2_irq_list[] = {
181 	{ 11, 0x80 },	/* IRQ 11, 0x80 in SIRQ in AML */
182 	{ 10, 0x40 },	/* IRQ 10, 0x40 in SIRQ in AML */
183 	{  9, 0x20 },	/* IRQ  9, 0x20 in SIRQ in AML */
184 	{  6, 0x10 },	/* IRQ  6, 0x10 in SIRQ in AML */
185 	{  0, 0x00 }	/* no IRQ, 0x00 in SIRQ in AML */
186 };
187 
188 /* same as in type2 models */
189 static struct sonypi_irq_list *sonypi_type3_irq_list = sonypi_type2_irq_list;
190 
191 #define SONYPI_CAMERA_BRIGHTNESS		0
192 #define SONYPI_CAMERA_CONTRAST			1
193 #define SONYPI_CAMERA_HUE			2
194 #define SONYPI_CAMERA_COLOR			3
195 #define SONYPI_CAMERA_SHARPNESS			4
196 
197 #define SONYPI_CAMERA_PICTURE			5
198 #define SONYPI_CAMERA_EXPOSURE_MASK		0xC
199 #define SONYPI_CAMERA_WHITE_BALANCE_MASK	0x3
200 #define SONYPI_CAMERA_PICTURE_MODE_MASK		0x30
201 #define SONYPI_CAMERA_MUTE_MASK			0x40
202 
203 /* the rest don't need a loop until not 0xff */
204 #define SONYPI_CAMERA_AGC			6
205 #define SONYPI_CAMERA_AGC_MASK			0x30
206 #define SONYPI_CAMERA_SHUTTER_MASK 		0x7
207 
208 #define SONYPI_CAMERA_SHUTDOWN_REQUEST		7
209 #define SONYPI_CAMERA_CONTROL			0x10
210 
211 #define SONYPI_CAMERA_STATUS 			7
212 #define SONYPI_CAMERA_STATUS_READY 		0x2
213 #define SONYPI_CAMERA_STATUS_POSITION		0x4
214 
215 #define SONYPI_DIRECTION_BACKWARDS 		0x4
216 
217 #define SONYPI_CAMERA_REVISION 			8
218 #define SONYPI_CAMERA_ROMVERSION 		9
219 
220 /* Event masks */
221 #define SONYPI_JOGGER_MASK			0x00000001
222 #define SONYPI_CAPTURE_MASK			0x00000002
223 #define SONYPI_FNKEY_MASK			0x00000004
224 #define SONYPI_BLUETOOTH_MASK			0x00000008
225 #define SONYPI_PKEY_MASK			0x00000010
226 #define SONYPI_BACK_MASK			0x00000020
227 #define SONYPI_HELP_MASK			0x00000040
228 #define SONYPI_LID_MASK				0x00000080
229 #define SONYPI_ZOOM_MASK			0x00000100
230 #define SONYPI_THUMBPHRASE_MASK			0x00000200
231 #define SONYPI_MEYE_MASK			0x00000400
232 #define SONYPI_MEMORYSTICK_MASK			0x00000800
233 #define SONYPI_BATTERY_MASK			0x00001000
234 #define SONYPI_WIRELESS_MASK			0x00002000
235 
236 struct sonypi_event {
237 	u8	data;
238 	u8	event;
239 };
240 
241 /* The set of possible button release events */
242 static struct sonypi_event sonypi_releaseev[] = {
243 	{ 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
244 	{ 0, 0 }
245 };
246 
247 /* The set of possible jogger events  */
248 static struct sonypi_event sonypi_joggerev[] = {
249 	{ 0x1f, SONYPI_EVENT_JOGDIAL_UP },
250 	{ 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
251 	{ 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
252 	{ 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
253 	{ 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
254 	{ 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
255 	{ 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
256 	{ 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
257 	{ 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
258 	{ 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
259 	{ 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
260 	{ 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
261 	{ 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
262 	{ 0, 0 }
263 };
264 
265 /* The set of possible capture button events */
266 static struct sonypi_event sonypi_captureev[] = {
267 	{ 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
268 	{ 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
269 	{ 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
270 	{ 0, 0 }
271 };
272 
273 /* The set of possible fnkeys events */
274 static struct sonypi_event sonypi_fnkeyev[] = {
275 	{ 0x10, SONYPI_EVENT_FNKEY_ESC },
276 	{ 0x11, SONYPI_EVENT_FNKEY_F1 },
277 	{ 0x12, SONYPI_EVENT_FNKEY_F2 },
278 	{ 0x13, SONYPI_EVENT_FNKEY_F3 },
279 	{ 0x14, SONYPI_EVENT_FNKEY_F4 },
280 	{ 0x15, SONYPI_EVENT_FNKEY_F5 },
281 	{ 0x16, SONYPI_EVENT_FNKEY_F6 },
282 	{ 0x17, SONYPI_EVENT_FNKEY_F7 },
283 	{ 0x18, SONYPI_EVENT_FNKEY_F8 },
284 	{ 0x19, SONYPI_EVENT_FNKEY_F9 },
285 	{ 0x1a, SONYPI_EVENT_FNKEY_F10 },
286 	{ 0x1b, SONYPI_EVENT_FNKEY_F11 },
287 	{ 0x1c, SONYPI_EVENT_FNKEY_F12 },
288 	{ 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
289 	{ 0x21, SONYPI_EVENT_FNKEY_1 },
290 	{ 0x22, SONYPI_EVENT_FNKEY_2 },
291 	{ 0x31, SONYPI_EVENT_FNKEY_D },
292 	{ 0x32, SONYPI_EVENT_FNKEY_E },
293 	{ 0x33, SONYPI_EVENT_FNKEY_F },
294 	{ 0x34, SONYPI_EVENT_FNKEY_S },
295 	{ 0x35, SONYPI_EVENT_FNKEY_B },
296 	{ 0x36, SONYPI_EVENT_FNKEY_ONLY },
297 	{ 0, 0 }
298 };
299 
300 /* The set of possible program key events */
301 static struct sonypi_event sonypi_pkeyev[] = {
302 	{ 0x01, SONYPI_EVENT_PKEY_P1 },
303 	{ 0x02, SONYPI_EVENT_PKEY_P2 },
304 	{ 0x04, SONYPI_EVENT_PKEY_P3 },
305 	{ 0x5c, SONYPI_EVENT_PKEY_P1 },
306 	{ 0, 0 }
307 };
308 
309 /* The set of possible bluetooth events */
310 static struct sonypi_event sonypi_blueev[] = {
311 	{ 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
312 	{ 0x59, SONYPI_EVENT_BLUETOOTH_ON },
313 	{ 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
314 	{ 0, 0 }
315 };
316 
317 /* The set of possible wireless events */
318 static struct sonypi_event sonypi_wlessev[] = {
319 	{ 0x59, SONYPI_EVENT_WIRELESS_ON },
320 	{ 0x5a, SONYPI_EVENT_WIRELESS_OFF },
321 	{ 0, 0 }
322 };
323 
324 /* The set of possible back button events */
325 static struct sonypi_event sonypi_backev[] = {
326 	{ 0x20, SONYPI_EVENT_BACK_PRESSED },
327 	{ 0, 0 }
328 };
329 
330 /* The set of possible help button events */
331 static struct sonypi_event sonypi_helpev[] = {
332 	{ 0x3b, SONYPI_EVENT_HELP_PRESSED },
333 	{ 0, 0 }
334 };
335 
336 
337 /* The set of possible lid events */
338 static struct sonypi_event sonypi_lidev[] = {
339 	{ 0x51, SONYPI_EVENT_LID_CLOSED },
340 	{ 0x50, SONYPI_EVENT_LID_OPENED },
341 	{ 0, 0 }
342 };
343 
344 /* The set of possible zoom events */
345 static struct sonypi_event sonypi_zoomev[] = {
346 	{ 0x39, SONYPI_EVENT_ZOOM_PRESSED },
347 	{ 0, 0 }
348 };
349 
350 /* The set of possible thumbphrase events */
351 static struct sonypi_event sonypi_thumbphraseev[] = {
352 	{ 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
353 	{ 0, 0 }
354 };
355 
356 /* The set of possible motioneye camera events */
357 static struct sonypi_event sonypi_meyeev[] = {
358 	{ 0x00, SONYPI_EVENT_MEYE_FACE },
359 	{ 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
360 	{ 0, 0 }
361 };
362 
363 /* The set of possible memorystick events */
364 static struct sonypi_event sonypi_memorystickev[] = {
365 	{ 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
366 	{ 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
367 	{ 0, 0 }
368 };
369 
370 /* The set of possible battery events */
371 static struct sonypi_event sonypi_batteryev[] = {
372 	{ 0x20, SONYPI_EVENT_BATTERY_INSERT },
373 	{ 0x30, SONYPI_EVENT_BATTERY_REMOVE },
374 	{ 0, 0 }
375 };
376 
377 static struct sonypi_eventtypes {
378 	int			model;
379 	u8			data;
380 	unsigned long		mask;
381 	struct sonypi_event *	events;
382 } sonypi_eventtypes[] = {
383 	{ SONYPI_DEVICE_MODEL_TYPE1, 0, 0xffffffff, sonypi_releaseev },
384 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
385 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
386 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
387 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
388 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
389 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
390 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
391 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
392 	{ SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
393 
394 	{ SONYPI_DEVICE_MODEL_TYPE2, 0, 0xffffffff, sonypi_releaseev },
395 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
396 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
397 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
398 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
399 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
400 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
401 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
402 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
403 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
404 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
405 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
406 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
407 	{ SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
408 
409 	{ SONYPI_DEVICE_MODEL_TYPE3, 0, 0xffffffff, sonypi_releaseev },
410 	{ SONYPI_DEVICE_MODEL_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
411 	{ SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
412 	{ SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
413 	{ SONYPI_DEVICE_MODEL_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
414 	{ SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
415 	{ 0 }
416 };
417 
418 #define SONYPI_BUF_SIZE	128
419 
420 /* Correspondance table between sonypi events and input layer events */
421 static struct {
422 	int sonypiev;
423 	int inputev;
424 } sonypi_inputkeys[] = {
425 	{ SONYPI_EVENT_CAPTURE_PRESSED,	 	KEY_CAMERA },
426 	{ SONYPI_EVENT_FNKEY_ONLY, 		KEY_FN },
427 	{ SONYPI_EVENT_FNKEY_ESC, 		KEY_FN_ESC },
428 	{ SONYPI_EVENT_FNKEY_F1, 		KEY_FN_F1 },
429 	{ SONYPI_EVENT_FNKEY_F2, 		KEY_FN_F2 },
430 	{ SONYPI_EVENT_FNKEY_F3, 		KEY_FN_F3 },
431 	{ SONYPI_EVENT_FNKEY_F4, 		KEY_FN_F4 },
432 	{ SONYPI_EVENT_FNKEY_F5, 		KEY_FN_F5 },
433 	{ SONYPI_EVENT_FNKEY_F6, 		KEY_FN_F6 },
434 	{ SONYPI_EVENT_FNKEY_F7, 		KEY_FN_F7 },
435 	{ SONYPI_EVENT_FNKEY_F8, 		KEY_FN_F8 },
436 	{ SONYPI_EVENT_FNKEY_F9,		KEY_FN_F9 },
437 	{ SONYPI_EVENT_FNKEY_F10,		KEY_FN_F10 },
438 	{ SONYPI_EVENT_FNKEY_F11, 		KEY_FN_F11 },
439 	{ SONYPI_EVENT_FNKEY_F12,		KEY_FN_F12 },
440 	{ SONYPI_EVENT_FNKEY_1, 		KEY_FN_1 },
441 	{ SONYPI_EVENT_FNKEY_2, 		KEY_FN_2 },
442 	{ SONYPI_EVENT_FNKEY_D,			KEY_FN_D },
443 	{ SONYPI_EVENT_FNKEY_E,			KEY_FN_E },
444 	{ SONYPI_EVENT_FNKEY_F,			KEY_FN_F },
445 	{ SONYPI_EVENT_FNKEY_S,			KEY_FN_S },
446 	{ SONYPI_EVENT_FNKEY_B,			KEY_FN_B },
447 	{ SONYPI_EVENT_BLUETOOTH_PRESSED, 	KEY_BLUE },
448 	{ SONYPI_EVENT_BLUETOOTH_ON, 		KEY_BLUE },
449 	{ SONYPI_EVENT_PKEY_P1, 		KEY_PROG1 },
450 	{ SONYPI_EVENT_PKEY_P2, 		KEY_PROG2 },
451 	{ SONYPI_EVENT_PKEY_P3, 		KEY_PROG3 },
452 	{ SONYPI_EVENT_BACK_PRESSED, 		KEY_BACK },
453 	{ SONYPI_EVENT_HELP_PRESSED, 		KEY_HELP },
454 	{ SONYPI_EVENT_ZOOM_PRESSED, 		KEY_ZOOM },
455 	{ SONYPI_EVENT_THUMBPHRASE_PRESSED, 	BTN_THUMB },
456 	{ 0, 0 },
457 };
458 
459 struct sonypi_keypress {
460 	struct input_dev *dev;
461 	int key;
462 };
463 
464 static struct sonypi_device {
465 	struct pci_dev *dev;
466 	u16 irq;
467 	u16 bits;
468 	u16 ioport1;
469 	u16 ioport2;
470 	u16 region_size;
471 	u16 evtype_offset;
472 	int camera_power;
473 	int bluetooth_power;
474 	struct mutex lock;
475 	struct kfifo fifo;
476 	spinlock_t fifo_lock;
477 	wait_queue_head_t fifo_proc_list;
478 	struct fasync_struct *fifo_async;
479 	int open_count;
480 	int model;
481 	struct input_dev *input_jog_dev;
482 	struct input_dev *input_key_dev;
483 	struct work_struct input_work;
484 	struct kfifo input_fifo;
485 	spinlock_t input_fifo_lock;
486 } sonypi_device;
487 
488 #define ITERATIONS_LONG		10000
489 #define ITERATIONS_SHORT	10
490 
491 #define wait_on_command(quiet, command, iterations) { \
492 	unsigned int n = iterations; \
493 	while (--n && (command)) \
494 		udelay(1); \
495 	if (!n && (verbose || !quiet)) \
496 		printk(KERN_WARNING "sonypi command failed at %s : %s (line %d)\n", __FILE__, __func__, __LINE__); \
497 }
498 
499 #ifdef CONFIG_ACPI
500 #define SONYPI_ACPI_ACTIVE (!acpi_disabled)
501 #else
502 #define SONYPI_ACPI_ACTIVE 0
503 #endif				/* CONFIG_ACPI */
504 
505 #ifdef CONFIG_ACPI
506 static struct acpi_device *sonypi_acpi_device;
507 static int acpi_driver_registered;
508 #endif
509 
510 static int sonypi_ec_write(u8 addr, u8 value)
511 {
512 #ifdef CONFIG_ACPI
513 	if (SONYPI_ACPI_ACTIVE)
514 		return ec_write(addr, value);
515 #endif
516 	wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
517 	outb_p(0x81, SONYPI_CST_IOPORT);
518 	wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
519 	outb_p(addr, SONYPI_DATA_IOPORT);
520 	wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
521 	outb_p(value, SONYPI_DATA_IOPORT);
522 	wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
523 	return 0;
524 }
525 
526 static int sonypi_ec_read(u8 addr, u8 *value)
527 {
528 #ifdef CONFIG_ACPI
529 	if (SONYPI_ACPI_ACTIVE)
530 		return ec_read(addr, value);
531 #endif
532 	wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
533 	outb_p(0x80, SONYPI_CST_IOPORT);
534 	wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
535 	outb_p(addr, SONYPI_DATA_IOPORT);
536 	wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
537 	*value = inb_p(SONYPI_DATA_IOPORT);
538 	return 0;
539 }
540 
541 static int ec_read16(u8 addr, u16 *value)
542 {
543 	u8 val_lb, val_hb;
544 	if (sonypi_ec_read(addr, &val_lb))
545 		return -1;
546 	if (sonypi_ec_read(addr + 1, &val_hb))
547 		return -1;
548 	*value = val_lb | (val_hb << 8);
549 	return 0;
550 }
551 
552 /* Initializes the device - this comes from the AML code in the ACPI bios */
553 static void sonypi_type1_srs(void)
554 {
555 	u32 v;
556 
557 	pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
558 	v = (v & 0xFFFF0000) | ((u32) sonypi_device.ioport1);
559 	pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
560 
561 	pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
562 	v = (v & 0xFFF0FFFF) |
563 	    (((u32) sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
564 	pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
565 
566 	v = inl(SONYPI_IRQ_PORT);
567 	v &= ~(((u32) 0x3) << SONYPI_IRQ_SHIFT);
568 	v |= (((u32) sonypi_device.bits) << SONYPI_IRQ_SHIFT);
569 	outl(v, SONYPI_IRQ_PORT);
570 
571 	pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
572 	v = (v & 0xFF1FFFFF) | 0x00C00000;
573 	pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
574 }
575 
576 static void sonypi_type2_srs(void)
577 {
578 	if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
579 		printk(KERN_WARNING "ec_write failed\n");
580 	if (sonypi_ec_write(SONYPI_SLOB, sonypi_device.ioport1 & 0x00FF))
581 		printk(KERN_WARNING "ec_write failed\n");
582 	if (sonypi_ec_write(SONYPI_SIRQ, sonypi_device.bits))
583 		printk(KERN_WARNING "ec_write failed\n");
584 	udelay(10);
585 }
586 
587 static void sonypi_type3_srs(void)
588 {
589 	u16 v16;
590 	u8  v8;
591 
592 	/* This model type uses the same initialization of
593 	 * the embedded controller as the type2 models. */
594 	sonypi_type2_srs();
595 
596 	/* Initialization of PCI config space of the LPC interface bridge. */
597 	v16 = (sonypi_device.ioport1 & 0xFFF0) | 0x01;
598 	pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, v16);
599 	pci_read_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, &v8);
600 	v8 = (v8 & 0xCF) | 0x10;
601 	pci_write_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, v8);
602 }
603 
604 /* Disables the device - this comes from the AML code in the ACPI bios */
605 static void sonypi_type1_dis(void)
606 {
607 	u32 v;
608 
609 	pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
610 	v = v & 0xFF3FFFFF;
611 	pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
612 
613 	v = inl(SONYPI_IRQ_PORT);
614 	v |= (0x3 << SONYPI_IRQ_SHIFT);
615 	outl(v, SONYPI_IRQ_PORT);
616 }
617 
618 static void sonypi_type2_dis(void)
619 {
620 	if (sonypi_ec_write(SONYPI_SHIB, 0))
621 		printk(KERN_WARNING "ec_write failed\n");
622 	if (sonypi_ec_write(SONYPI_SLOB, 0))
623 		printk(KERN_WARNING "ec_write failed\n");
624 	if (sonypi_ec_write(SONYPI_SIRQ, 0))
625 		printk(KERN_WARNING "ec_write failed\n");
626 }
627 
628 static void sonypi_type3_dis(void)
629 {
630 	sonypi_type2_dis();
631 	udelay(10);
632 	pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, 0);
633 }
634 
635 static u8 sonypi_call1(u8 dev)
636 {
637 	u8 v1, v2;
638 
639 	wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
640 	outb(dev, sonypi_device.ioport2);
641 	v1 = inb_p(sonypi_device.ioport2);
642 	v2 = inb_p(sonypi_device.ioport1);
643 	return v2;
644 }
645 
646 static u8 sonypi_call2(u8 dev, u8 fn)
647 {
648 	u8 v1;
649 
650 	wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
651 	outb(dev, sonypi_device.ioport2);
652 	wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
653 	outb(fn, sonypi_device.ioport1);
654 	v1 = inb_p(sonypi_device.ioport1);
655 	return v1;
656 }
657 
658 static u8 sonypi_call3(u8 dev, u8 fn, u8 v)
659 {
660 	u8 v1;
661 
662 	wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
663 	outb(dev, sonypi_device.ioport2);
664 	wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
665 	outb(fn, sonypi_device.ioport1);
666 	wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
667 	outb(v, sonypi_device.ioport1);
668 	v1 = inb_p(sonypi_device.ioport1);
669 	return v1;
670 }
671 
672 #if 0
673 /* Get brightness, hue etc. Unreliable... */
674 static u8 sonypi_read(u8 fn)
675 {
676 	u8 v1, v2;
677 	int n = 100;
678 
679 	while (n--) {
680 		v1 = sonypi_call2(0x8f, fn);
681 		v2 = sonypi_call2(0x8f, fn);
682 		if (v1 == v2 && v1 != 0xff)
683 			return v1;
684 	}
685 	return 0xff;
686 }
687 #endif
688 
689 /* Set brightness, hue etc */
690 static void sonypi_set(u8 fn, u8 v)
691 {
692 	wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
693 }
694 
695 /* Tests if the camera is ready */
696 static int sonypi_camera_ready(void)
697 {
698 	u8 v;
699 
700 	v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
701 	return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
702 }
703 
704 /* Turns the camera off */
705 static void sonypi_camera_off(void)
706 {
707 	sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
708 
709 	if (!sonypi_device.camera_power)
710 		return;
711 
712 	sonypi_call2(0x91, 0);
713 	sonypi_device.camera_power = 0;
714 }
715 
716 /* Turns the camera on */
717 static void sonypi_camera_on(void)
718 {
719 	int i, j;
720 
721 	if (sonypi_device.camera_power)
722 		return;
723 
724 	for (j = 5; j > 0; j--) {
725 
726 		while (sonypi_call2(0x91, 0x1))
727 			msleep(10);
728 		sonypi_call1(0x93);
729 
730 		for (i = 400; i > 0; i--) {
731 			if (sonypi_camera_ready())
732 				break;
733 			msleep(10);
734 		}
735 		if (i)
736 			break;
737 	}
738 
739 	if (j == 0) {
740 		printk(KERN_WARNING "sonypi: failed to power on camera\n");
741 		return;
742 	}
743 
744 	sonypi_set(0x10, 0x5a);
745 	sonypi_device.camera_power = 1;
746 }
747 
748 /* sets the bluetooth subsystem power state */
749 static void sonypi_setbluetoothpower(u8 state)
750 {
751 	state = !!state;
752 
753 	if (sonypi_device.bluetooth_power == state)
754 		return;
755 
756 	sonypi_call2(0x96, state);
757 	sonypi_call1(0x82);
758 	sonypi_device.bluetooth_power = state;
759 }
760 
761 static void input_keyrelease(struct work_struct *work)
762 {
763 	struct sonypi_keypress kp;
764 
765 	while (kfifo_out_locked(&sonypi_device.input_fifo, (unsigned char *)&kp,
766 			 sizeof(kp), &sonypi_device.input_fifo_lock)
767 			== sizeof(kp)) {
768 		msleep(10);
769 		input_report_key(kp.dev, kp.key, 0);
770 		input_sync(kp.dev);
771 	}
772 }
773 
774 static void sonypi_report_input_event(u8 event)
775 {
776 	struct input_dev *jog_dev = sonypi_device.input_jog_dev;
777 	struct input_dev *key_dev = sonypi_device.input_key_dev;
778 	struct sonypi_keypress kp = { NULL };
779 	int i;
780 
781 	switch (event) {
782 	case SONYPI_EVENT_JOGDIAL_UP:
783 	case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
784 		input_report_rel(jog_dev, REL_WHEEL, 1);
785 		input_sync(jog_dev);
786 		break;
787 
788 	case SONYPI_EVENT_JOGDIAL_DOWN:
789 	case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
790 		input_report_rel(jog_dev, REL_WHEEL, -1);
791 		input_sync(jog_dev);
792 		break;
793 
794 	case SONYPI_EVENT_JOGDIAL_PRESSED:
795 		kp.key = BTN_MIDDLE;
796 		kp.dev = jog_dev;
797 		break;
798 
799 	case SONYPI_EVENT_FNKEY_RELEASED:
800 		/* Nothing, not all VAIOs generate this event */
801 		break;
802 
803 	default:
804 		for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
805 			if (event == sonypi_inputkeys[i].sonypiev) {
806 				kp.dev = key_dev;
807 				kp.key = sonypi_inputkeys[i].inputev;
808 				break;
809 			}
810 		break;
811 	}
812 
813 	if (kp.dev) {
814 		input_report_key(kp.dev, kp.key, 1);
815 		input_sync(kp.dev);
816 		kfifo_in_locked(&sonypi_device.input_fifo,
817 			(unsigned char *)&kp, sizeof(kp),
818 			&sonypi_device.input_fifo_lock);
819 		schedule_work(&sonypi_device.input_work);
820 	}
821 }
822 
823 /* Interrupt handler: some event is available */
824 static irqreturn_t sonypi_irq(int irq, void *dev_id)
825 {
826 	u8 v1, v2, event = 0;
827 	int i, j;
828 
829 	v1 = inb_p(sonypi_device.ioport1);
830 	v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
831 
832 	for (i = 0; sonypi_eventtypes[i].model; i++) {
833 		if (sonypi_device.model != sonypi_eventtypes[i].model)
834 			continue;
835 		if ((v2 & sonypi_eventtypes[i].data) !=
836 		    sonypi_eventtypes[i].data)
837 			continue;
838 		if (!(mask & sonypi_eventtypes[i].mask))
839 			continue;
840 		for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
841 			if (v1 == sonypi_eventtypes[i].events[j].data) {
842 				event = sonypi_eventtypes[i].events[j].event;
843 				goto found;
844 			}
845 		}
846 	}
847 
848 	if (verbose)
849 		printk(KERN_WARNING
850 		       "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",
851 		       v1, v2);
852 	/* We need to return IRQ_HANDLED here because there *are*
853 	 * events belonging to the sonypi device we don't know about,
854 	 * but we still don't want those to pollute the logs... */
855 	return IRQ_HANDLED;
856 
857 found:
858 	if (verbose > 1)
859 		printk(KERN_INFO
860 		       "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
861 
862 	if (useinput)
863 		sonypi_report_input_event(event);
864 
865 	kfifo_in_locked(&sonypi_device.fifo, (unsigned char *)&event,
866 			sizeof(event), &sonypi_device.fifo_lock);
867 	kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
868 	wake_up_interruptible(&sonypi_device.fifo_proc_list);
869 
870 	return IRQ_HANDLED;
871 }
872 
873 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
874 {
875 	return fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
876 }
877 
878 static int sonypi_misc_release(struct inode *inode, struct file *file)
879 {
880 	mutex_lock(&sonypi_device.lock);
881 	sonypi_device.open_count--;
882 	mutex_unlock(&sonypi_device.lock);
883 	return 0;
884 }
885 
886 static int sonypi_misc_open(struct inode *inode, struct file *file)
887 {
888 	mutex_lock(&sonypi_device.lock);
889 	/* Flush input queue on first open */
890 	if (!sonypi_device.open_count)
891 		kfifo_reset(&sonypi_device.fifo);
892 	sonypi_device.open_count++;
893 	mutex_unlock(&sonypi_device.lock);
894 
895 	return 0;
896 }
897 
898 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
899 				size_t count, loff_t *pos)
900 {
901 	ssize_t ret;
902 	unsigned char c;
903 
904 	if ((kfifo_len(&sonypi_device.fifo) == 0) &&
905 	    (file->f_flags & O_NONBLOCK))
906 		return -EAGAIN;
907 
908 	ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
909 				       kfifo_len(&sonypi_device.fifo) != 0);
910 	if (ret)
911 		return ret;
912 
913 	while (ret < count &&
914 	       (kfifo_out_locked(&sonypi_device.fifo, &c, sizeof(c),
915 				 &sonypi_device.fifo_lock) == sizeof(c))) {
916 		if (put_user(c, buf++))
917 			return -EFAULT;
918 		ret++;
919 	}
920 
921 	if (ret > 0) {
922 		struct inode *inode = file_inode(file);
923 		inode_set_atime_to_ts(inode, current_time(inode));
924 	}
925 
926 	return ret;
927 }
928 
929 static __poll_t sonypi_misc_poll(struct file *file, poll_table *wait)
930 {
931 	poll_wait(file, &sonypi_device.fifo_proc_list, wait);
932 	if (kfifo_len(&sonypi_device.fifo))
933 		return EPOLLIN | EPOLLRDNORM;
934 	return 0;
935 }
936 
937 static long sonypi_misc_ioctl(struct file *fp,
938 			     unsigned int cmd, unsigned long arg)
939 {
940 	long ret = 0;
941 	void __user *argp = (void __user *)arg;
942 	u8 val8;
943 	u16 val16;
944 
945 	mutex_lock(&sonypi_device.lock);
946 	switch (cmd) {
947 	case SONYPI_IOCGBRT:
948 		if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
949 			ret = -EIO;
950 			break;
951 		}
952 		if (copy_to_user(argp, &val8, sizeof(val8)))
953 			ret = -EFAULT;
954 		break;
955 	case SONYPI_IOCSBRT:
956 		if (copy_from_user(&val8, argp, sizeof(val8))) {
957 			ret = -EFAULT;
958 			break;
959 		}
960 		if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
961 			ret = -EIO;
962 		break;
963 	case SONYPI_IOCGBAT1CAP:
964 		if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
965 			ret = -EIO;
966 			break;
967 		}
968 		if (copy_to_user(argp, &val16, sizeof(val16)))
969 			ret = -EFAULT;
970 		break;
971 	case SONYPI_IOCGBAT1REM:
972 		if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
973 			ret = -EIO;
974 			break;
975 		}
976 		if (copy_to_user(argp, &val16, sizeof(val16)))
977 			ret = -EFAULT;
978 		break;
979 	case SONYPI_IOCGBAT2CAP:
980 		if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
981 			ret = -EIO;
982 			break;
983 		}
984 		if (copy_to_user(argp, &val16, sizeof(val16)))
985 			ret = -EFAULT;
986 		break;
987 	case SONYPI_IOCGBAT2REM:
988 		if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
989 			ret = -EIO;
990 			break;
991 		}
992 		if (copy_to_user(argp, &val16, sizeof(val16)))
993 			ret = -EFAULT;
994 		break;
995 	case SONYPI_IOCGBATFLAGS:
996 		if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
997 			ret = -EIO;
998 			break;
999 		}
1000 		val8 &= 0x07;
1001 		if (copy_to_user(argp, &val8, sizeof(val8)))
1002 			ret = -EFAULT;
1003 		break;
1004 	case SONYPI_IOCGBLUE:
1005 		val8 = sonypi_device.bluetooth_power;
1006 		if (copy_to_user(argp, &val8, sizeof(val8)))
1007 			ret = -EFAULT;
1008 		break;
1009 	case SONYPI_IOCSBLUE:
1010 		if (copy_from_user(&val8, argp, sizeof(val8))) {
1011 			ret = -EFAULT;
1012 			break;
1013 		}
1014 		sonypi_setbluetoothpower(val8);
1015 		break;
1016 	/* FAN Controls */
1017 	case SONYPI_IOCGFAN:
1018 		if (sonypi_ec_read(SONYPI_FAN0_STATUS, &val8)) {
1019 			ret = -EIO;
1020 			break;
1021 		}
1022 		if (copy_to_user(argp, &val8, sizeof(val8)))
1023 			ret = -EFAULT;
1024 		break;
1025 	case SONYPI_IOCSFAN:
1026 		if (copy_from_user(&val8, argp, sizeof(val8))) {
1027 			ret = -EFAULT;
1028 			break;
1029 		}
1030 		if (sonypi_ec_write(SONYPI_FAN0_STATUS, val8))
1031 			ret = -EIO;
1032 		break;
1033 	/* GET Temperature (useful under APM) */
1034 	case SONYPI_IOCGTEMP:
1035 		if (sonypi_ec_read(SONYPI_TEMP_STATUS, &val8)) {
1036 			ret = -EIO;
1037 			break;
1038 		}
1039 		if (copy_to_user(argp, &val8, sizeof(val8)))
1040 			ret = -EFAULT;
1041 		break;
1042 	default:
1043 		ret = -EINVAL;
1044 	}
1045 	mutex_unlock(&sonypi_device.lock);
1046 	return ret;
1047 }
1048 
1049 static const struct file_operations sonypi_misc_fops = {
1050 	.owner		= THIS_MODULE,
1051 	.read		= sonypi_misc_read,
1052 	.poll		= sonypi_misc_poll,
1053 	.open		= sonypi_misc_open,
1054 	.release	= sonypi_misc_release,
1055 	.fasync		= sonypi_misc_fasync,
1056 	.unlocked_ioctl	= sonypi_misc_ioctl,
1057 };
1058 
1059 static struct miscdevice sonypi_misc_device = {
1060 	.minor		= MISC_DYNAMIC_MINOR,
1061 	.name		= "sonypi",
1062 	.fops		= &sonypi_misc_fops,
1063 };
1064 
1065 static void sonypi_enable(unsigned int camera_on)
1066 {
1067 	switch (sonypi_device.model) {
1068 	case SONYPI_DEVICE_MODEL_TYPE1:
1069 		sonypi_type1_srs();
1070 		break;
1071 	case SONYPI_DEVICE_MODEL_TYPE2:
1072 		sonypi_type2_srs();
1073 		break;
1074 	case SONYPI_DEVICE_MODEL_TYPE3:
1075 		sonypi_type3_srs();
1076 		break;
1077 	}
1078 
1079 	sonypi_call1(0x82);
1080 	sonypi_call2(0x81, 0xff);
1081 	sonypi_call1(compat ? 0x92 : 0x82);
1082 
1083 	/* Enable ACPI mode to get Fn key events */
1084 	if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1085 		outb(0xf0, 0xb2);
1086 
1087 	if (camera && camera_on)
1088 		sonypi_camera_on();
1089 }
1090 
1091 static int sonypi_disable(void)
1092 {
1093 	sonypi_call2(0x81, 0);	/* make sure we don't get any more events */
1094 	if (camera)
1095 		sonypi_camera_off();
1096 
1097 	/* disable ACPI mode */
1098 	if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1099 		outb(0xf1, 0xb2);
1100 
1101 	switch (sonypi_device.model) {
1102 	case SONYPI_DEVICE_MODEL_TYPE1:
1103 		sonypi_type1_dis();
1104 		break;
1105 	case SONYPI_DEVICE_MODEL_TYPE2:
1106 		sonypi_type2_dis();
1107 		break;
1108 	case SONYPI_DEVICE_MODEL_TYPE3:
1109 		sonypi_type3_dis();
1110 		break;
1111 	}
1112 
1113 	return 0;
1114 }
1115 
1116 #ifdef CONFIG_ACPI
1117 static int sonypi_acpi_add(struct acpi_device *device)
1118 {
1119 	sonypi_acpi_device = device;
1120 	strcpy(acpi_device_name(device), "Sony laptop hotkeys");
1121 	strcpy(acpi_device_class(device), "sony/hotkey");
1122 	return 0;
1123 }
1124 
1125 static void sonypi_acpi_remove(struct acpi_device *device)
1126 {
1127 	sonypi_acpi_device = NULL;
1128 }
1129 
1130 static const struct acpi_device_id sonypi_device_ids[] = {
1131 	{"SNY6001", 0},
1132 	{"", 0},
1133 };
1134 
1135 static struct acpi_driver sonypi_acpi_driver = {
1136 	.name           = "sonypi",
1137 	.class          = "hkey",
1138 	.ids            = sonypi_device_ids,
1139 	.ops            = {
1140 		           .add = sonypi_acpi_add,
1141 			   .remove = sonypi_acpi_remove,
1142 	},
1143 };
1144 #endif
1145 
1146 static int sonypi_create_input_devices(struct platform_device *pdev)
1147 {
1148 	struct input_dev *jog_dev;
1149 	struct input_dev *key_dev;
1150 	int i;
1151 	int error;
1152 
1153 	sonypi_device.input_jog_dev = jog_dev = input_allocate_device();
1154 	if (!jog_dev)
1155 		return -ENOMEM;
1156 
1157 	jog_dev->name = "Sony Vaio Jogdial";
1158 	jog_dev->id.bustype = BUS_ISA;
1159 	jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
1160 	jog_dev->dev.parent = &pdev->dev;
1161 
1162 	jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1163 	jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
1164 	jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
1165 
1166 	sonypi_device.input_key_dev = key_dev = input_allocate_device();
1167 	if (!key_dev) {
1168 		error = -ENOMEM;
1169 		goto err_free_jogdev;
1170 	}
1171 
1172 	key_dev->name = "Sony Vaio Keys";
1173 	key_dev->id.bustype = BUS_ISA;
1174 	key_dev->id.vendor = PCI_VENDOR_ID_SONY;
1175 	key_dev->dev.parent = &pdev->dev;
1176 
1177 	/* Initialize the Input Drivers: special keys */
1178 	key_dev->evbit[0] = BIT_MASK(EV_KEY);
1179 	for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
1180 		if (sonypi_inputkeys[i].inputev)
1181 			set_bit(sonypi_inputkeys[i].inputev, key_dev->keybit);
1182 
1183 	error = input_register_device(jog_dev);
1184 	if (error)
1185 		goto err_free_keydev;
1186 
1187 	error = input_register_device(key_dev);
1188 	if (error)
1189 		goto err_unregister_jogdev;
1190 
1191 	return 0;
1192 
1193  err_unregister_jogdev:
1194 	input_unregister_device(jog_dev);
1195 	/* Set to NULL so we don't free it again below */
1196 	jog_dev = NULL;
1197  err_free_keydev:
1198 	input_free_device(key_dev);
1199 	sonypi_device.input_key_dev = NULL;
1200  err_free_jogdev:
1201 	input_free_device(jog_dev);
1202 	sonypi_device.input_jog_dev = NULL;
1203 
1204 	return error;
1205 }
1206 
1207 static int sonypi_setup_ioports(struct sonypi_device *dev,
1208 				const struct sonypi_ioport_list *ioport_list)
1209 {
1210 	/* try to detect if sony-laptop is being used and thus
1211 	 * has already requested one of the known ioports.
1212 	 * As in the deprecated check_region this is racy has we have
1213 	 * multiple ioports available and one of them can be requested
1214 	 * between this check and the subsequent request. Anyway, as an
1215 	 * attempt to be some more user-friendly as we currently are,
1216 	 * this is enough.
1217 	 */
1218 	const struct sonypi_ioport_list *check = ioport_list;
1219 	while (check_ioport && check->port1) {
1220 		if (!request_region(check->port1,
1221 				   sonypi_device.region_size,
1222 				   "Sony Programmable I/O Device Check")) {
1223 			printk(KERN_ERR "sonypi: ioport 0x%.4x busy, using sony-laptop? "
1224 					"if not use check_ioport=0\n",
1225 					check->port1);
1226 			return -EBUSY;
1227 		}
1228 		release_region(check->port1, sonypi_device.region_size);
1229 		check++;
1230 	}
1231 
1232 	while (ioport_list->port1) {
1233 
1234 		if (request_region(ioport_list->port1,
1235 				   sonypi_device.region_size,
1236 				   "Sony Programmable I/O Device")) {
1237 			dev->ioport1 = ioport_list->port1;
1238 			dev->ioport2 = ioport_list->port2;
1239 			return 0;
1240 		}
1241 		ioport_list++;
1242 	}
1243 
1244 	return -EBUSY;
1245 }
1246 
1247 static int sonypi_setup_irq(struct sonypi_device *dev,
1248 				      const struct sonypi_irq_list *irq_list)
1249 {
1250 	while (irq_list->irq) {
1251 
1252 		if (!request_irq(irq_list->irq, sonypi_irq,
1253 				 IRQF_SHARED, "sonypi", sonypi_irq)) {
1254 			dev->irq = irq_list->irq;
1255 			dev->bits = irq_list->bits;
1256 			return 0;
1257 		}
1258 		irq_list++;
1259 	}
1260 
1261 	return -EBUSY;
1262 }
1263 
1264 static void sonypi_display_info(void)
1265 {
1266 	printk(KERN_INFO "sonypi: detected type%d model, "
1267 	       "verbose = %d, fnkeyinit = %s, camera = %s, "
1268 	       "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
1269 	       sonypi_device.model,
1270 	       verbose,
1271 	       fnkeyinit ? "on" : "off",
1272 	       camera ? "on" : "off",
1273 	       compat ? "on" : "off",
1274 	       mask,
1275 	       useinput ? "on" : "off",
1276 	       SONYPI_ACPI_ACTIVE ? "on" : "off");
1277 	printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
1278 	       sonypi_device.irq,
1279 	       sonypi_device.ioport1, sonypi_device.ioport2);
1280 
1281 	if (minor == -1)
1282 		printk(KERN_INFO "sonypi: device allocated minor is %d\n",
1283 		       sonypi_misc_device.minor);
1284 }
1285 
1286 static int sonypi_probe(struct platform_device *dev)
1287 {
1288 	const struct sonypi_ioport_list *ioport_list;
1289 	const struct sonypi_irq_list *irq_list;
1290 	struct pci_dev *pcidev;
1291 	int error;
1292 
1293 	printk(KERN_WARNING "sonypi: please try the sony-laptop module instead "
1294 			"and report failures, see also "
1295 			"http://www.linux.it/~malattia/wiki/index.php/Sony_drivers\n");
1296 
1297 	spin_lock_init(&sonypi_device.fifo_lock);
1298 	error = kfifo_alloc(&sonypi_device.fifo, SONYPI_BUF_SIZE, GFP_KERNEL);
1299 	if (error) {
1300 		printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1301 		return error;
1302 	}
1303 
1304 	init_waitqueue_head(&sonypi_device.fifo_proc_list);
1305 	mutex_init(&sonypi_device.lock);
1306 	sonypi_device.bluetooth_power = -1;
1307 
1308 	if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1309 				     PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1310 		sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
1311 	else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1312 					  PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1313 		sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
1314 	else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1315 					  PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1316 		sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
1317 	else
1318 		sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
1319 
1320 	if (pcidev && pci_enable_device(pcidev)) {
1321 		printk(KERN_ERR "sonypi: pci_enable_device failed\n");
1322 		error = -EIO;
1323 		goto err_put_pcidev;
1324 	}
1325 
1326 	sonypi_device.dev = pcidev;
1327 
1328 	if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
1329 		ioport_list = sonypi_type1_ioport_list;
1330 		sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
1331 		sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
1332 		irq_list = sonypi_type1_irq_list;
1333 	} else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
1334 		ioport_list = sonypi_type2_ioport_list;
1335 		sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
1336 		sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
1337 		irq_list = sonypi_type2_irq_list;
1338 	} else {
1339 		ioport_list = sonypi_type3_ioport_list;
1340 		sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
1341 		sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
1342 		irq_list = sonypi_type3_irq_list;
1343 	}
1344 
1345 	error = sonypi_setup_ioports(&sonypi_device, ioport_list);
1346 	if (error) {
1347 		printk(KERN_ERR "sonypi: failed to request ioports\n");
1348 		goto err_disable_pcidev;
1349 	}
1350 
1351 	error = sonypi_setup_irq(&sonypi_device, irq_list);
1352 	if (error) {
1353 		printk(KERN_ERR "sonypi: request_irq failed\n");
1354 		goto err_free_ioports;
1355 	}
1356 
1357 	if (minor != -1)
1358 		sonypi_misc_device.minor = minor;
1359 	error = misc_register(&sonypi_misc_device);
1360 	if (error) {
1361 		printk(KERN_ERR "sonypi: misc_register failed\n");
1362 		goto err_free_irq;
1363 	}
1364 
1365 	sonypi_display_info();
1366 
1367 	if (useinput) {
1368 
1369 		error = sonypi_create_input_devices(dev);
1370 		if (error) {
1371 			printk(KERN_ERR
1372 				"sonypi: failed to create input devices\n");
1373 			goto err_miscdev_unregister;
1374 		}
1375 
1376 		spin_lock_init(&sonypi_device.input_fifo_lock);
1377 		error = kfifo_alloc(&sonypi_device.input_fifo, SONYPI_BUF_SIZE,
1378 				GFP_KERNEL);
1379 		if (error) {
1380 			printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1381 			goto err_inpdev_unregister;
1382 		}
1383 
1384 		INIT_WORK(&sonypi_device.input_work, input_keyrelease);
1385 	}
1386 
1387 	sonypi_enable(0);
1388 
1389 	return 0;
1390 
1391  err_inpdev_unregister:
1392 	input_unregister_device(sonypi_device.input_key_dev);
1393 	input_unregister_device(sonypi_device.input_jog_dev);
1394  err_miscdev_unregister:
1395 	misc_deregister(&sonypi_misc_device);
1396  err_free_irq:
1397 	free_irq(sonypi_device.irq, sonypi_irq);
1398  err_free_ioports:
1399 	release_region(sonypi_device.ioport1, sonypi_device.region_size);
1400  err_disable_pcidev:
1401 	if (pcidev)
1402 		pci_disable_device(pcidev);
1403  err_put_pcidev:
1404 	pci_dev_put(pcidev);
1405 	kfifo_free(&sonypi_device.fifo);
1406 
1407 	return error;
1408 }
1409 
1410 static void sonypi_remove(struct platform_device *dev)
1411 {
1412 	sonypi_disable();
1413 
1414 	synchronize_irq(sonypi_device.irq);
1415 	flush_work(&sonypi_device.input_work);
1416 
1417 	if (useinput) {
1418 		input_unregister_device(sonypi_device.input_key_dev);
1419 		input_unregister_device(sonypi_device.input_jog_dev);
1420 		kfifo_free(&sonypi_device.input_fifo);
1421 	}
1422 
1423 	misc_deregister(&sonypi_misc_device);
1424 
1425 	free_irq(sonypi_device.irq, sonypi_irq);
1426 	release_region(sonypi_device.ioport1, sonypi_device.region_size);
1427 
1428 	if (sonypi_device.dev) {
1429 		pci_disable_device(sonypi_device.dev);
1430 		pci_dev_put(sonypi_device.dev);
1431 	}
1432 
1433 	kfifo_free(&sonypi_device.fifo);
1434 }
1435 
1436 #ifdef CONFIG_PM_SLEEP
1437 static int old_camera_power;
1438 
1439 static int sonypi_suspend(struct device *dev)
1440 {
1441 	old_camera_power = sonypi_device.camera_power;
1442 	sonypi_disable();
1443 
1444 	return 0;
1445 }
1446 
1447 static int sonypi_resume(struct device *dev)
1448 {
1449 	sonypi_enable(old_camera_power);
1450 	return 0;
1451 }
1452 
1453 static SIMPLE_DEV_PM_OPS(sonypi_pm, sonypi_suspend, sonypi_resume);
1454 #define SONYPI_PM	(&sonypi_pm)
1455 #else
1456 #define SONYPI_PM	NULL
1457 #endif
1458 
1459 static void sonypi_shutdown(struct platform_device *dev)
1460 {
1461 	sonypi_disable();
1462 }
1463 
1464 static struct platform_driver sonypi_driver = {
1465 	.driver		= {
1466 		.name	= "sonypi",
1467 		.pm	= SONYPI_PM,
1468 	},
1469 	.probe		= sonypi_probe,
1470 	.remove		= sonypi_remove,
1471 	.shutdown	= sonypi_shutdown,
1472 };
1473 
1474 static struct platform_device *sonypi_platform_device;
1475 
1476 static const struct dmi_system_id sonypi_dmi_table[] __initconst = {
1477 	{
1478 		.ident = "Sony Vaio",
1479 		.matches = {
1480 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1481 			DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1482 		},
1483 	},
1484 	{
1485 		.ident = "Sony Vaio",
1486 		.matches = {
1487 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1488 			DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1489 		},
1490 	},
1491 	{ }
1492 };
1493 
1494 static int __init sonypi_init(void)
1495 {
1496 	int error;
1497 
1498 	printk(KERN_INFO
1499 		"sonypi: Sony Programmable I/O Controller Driver v%s.\n",
1500 		SONYPI_DRIVER_VERSION);
1501 
1502 	if (!dmi_check_system(sonypi_dmi_table))
1503 		return -ENODEV;
1504 
1505 	error = platform_driver_register(&sonypi_driver);
1506 	if (error)
1507 		return error;
1508 
1509 	sonypi_platform_device = platform_device_alloc("sonypi", -1);
1510 	if (!sonypi_platform_device) {
1511 		error = -ENOMEM;
1512 		goto err_driver_unregister;
1513 	}
1514 
1515 	error = platform_device_add(sonypi_platform_device);
1516 	if (error)
1517 		goto err_free_device;
1518 
1519 #ifdef CONFIG_ACPI
1520 	if (acpi_bus_register_driver(&sonypi_acpi_driver) >= 0)
1521 		acpi_driver_registered = 1;
1522 #endif
1523 
1524 	return 0;
1525 
1526  err_free_device:
1527 	platform_device_put(sonypi_platform_device);
1528  err_driver_unregister:
1529 	platform_driver_unregister(&sonypi_driver);
1530 	return error;
1531 }
1532 
1533 static void __exit sonypi_exit(void)
1534 {
1535 #ifdef CONFIG_ACPI
1536 	if (acpi_driver_registered)
1537 		acpi_bus_unregister_driver(&sonypi_acpi_driver);
1538 #endif
1539 	platform_device_unregister(sonypi_platform_device);
1540 	platform_driver_unregister(&sonypi_driver);
1541 	printk(KERN_INFO "sonypi: removed.\n");
1542 }
1543 
1544 module_init(sonypi_init);
1545 module_exit(sonypi_exit);
1546