xref: /linux/drivers/char/sonypi.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
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->i_atime = 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 	.llseek		= no_llseek,
1058 };
1059 
1060 static struct miscdevice sonypi_misc_device = {
1061 	.minor		= MISC_DYNAMIC_MINOR,
1062 	.name		= "sonypi",
1063 	.fops		= &sonypi_misc_fops,
1064 };
1065 
1066 static void sonypi_enable(unsigned int camera_on)
1067 {
1068 	switch (sonypi_device.model) {
1069 	case SONYPI_DEVICE_MODEL_TYPE1:
1070 		sonypi_type1_srs();
1071 		break;
1072 	case SONYPI_DEVICE_MODEL_TYPE2:
1073 		sonypi_type2_srs();
1074 		break;
1075 	case SONYPI_DEVICE_MODEL_TYPE3:
1076 		sonypi_type3_srs();
1077 		break;
1078 	}
1079 
1080 	sonypi_call1(0x82);
1081 	sonypi_call2(0x81, 0xff);
1082 	sonypi_call1(compat ? 0x92 : 0x82);
1083 
1084 	/* Enable ACPI mode to get Fn key events */
1085 	if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1086 		outb(0xf0, 0xb2);
1087 
1088 	if (camera && camera_on)
1089 		sonypi_camera_on();
1090 }
1091 
1092 static int sonypi_disable(void)
1093 {
1094 	sonypi_call2(0x81, 0);	/* make sure we don't get any more events */
1095 	if (camera)
1096 		sonypi_camera_off();
1097 
1098 	/* disable ACPI mode */
1099 	if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1100 		outb(0xf1, 0xb2);
1101 
1102 	switch (sonypi_device.model) {
1103 	case SONYPI_DEVICE_MODEL_TYPE1:
1104 		sonypi_type1_dis();
1105 		break;
1106 	case SONYPI_DEVICE_MODEL_TYPE2:
1107 		sonypi_type2_dis();
1108 		break;
1109 	case SONYPI_DEVICE_MODEL_TYPE3:
1110 		sonypi_type3_dis();
1111 		break;
1112 	}
1113 
1114 	return 0;
1115 }
1116 
1117 #ifdef CONFIG_ACPI
1118 static int sonypi_acpi_add(struct acpi_device *device)
1119 {
1120 	sonypi_acpi_device = device;
1121 	strcpy(acpi_device_name(device), "Sony laptop hotkeys");
1122 	strcpy(acpi_device_class(device), "sony/hotkey");
1123 	return 0;
1124 }
1125 
1126 static void sonypi_acpi_remove(struct acpi_device *device)
1127 {
1128 	sonypi_acpi_device = NULL;
1129 }
1130 
1131 static const struct acpi_device_id sonypi_device_ids[] = {
1132 	{"SNY6001", 0},
1133 	{"", 0},
1134 };
1135 
1136 static struct acpi_driver sonypi_acpi_driver = {
1137 	.name           = "sonypi",
1138 	.class          = "hkey",
1139 	.ids            = sonypi_device_ids,
1140 	.ops            = {
1141 		           .add = sonypi_acpi_add,
1142 			   .remove = sonypi_acpi_remove,
1143 	},
1144 };
1145 #endif
1146 
1147 static int sonypi_create_input_devices(struct platform_device *pdev)
1148 {
1149 	struct input_dev *jog_dev;
1150 	struct input_dev *key_dev;
1151 	int i;
1152 	int error;
1153 
1154 	sonypi_device.input_jog_dev = jog_dev = input_allocate_device();
1155 	if (!jog_dev)
1156 		return -ENOMEM;
1157 
1158 	jog_dev->name = "Sony Vaio Jogdial";
1159 	jog_dev->id.bustype = BUS_ISA;
1160 	jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
1161 	jog_dev->dev.parent = &pdev->dev;
1162 
1163 	jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
1164 	jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
1165 	jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
1166 
1167 	sonypi_device.input_key_dev = key_dev = input_allocate_device();
1168 	if (!key_dev) {
1169 		error = -ENOMEM;
1170 		goto err_free_jogdev;
1171 	}
1172 
1173 	key_dev->name = "Sony Vaio Keys";
1174 	key_dev->id.bustype = BUS_ISA;
1175 	key_dev->id.vendor = PCI_VENDOR_ID_SONY;
1176 	key_dev->dev.parent = &pdev->dev;
1177 
1178 	/* Initialize the Input Drivers: special keys */
1179 	key_dev->evbit[0] = BIT_MASK(EV_KEY);
1180 	for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
1181 		if (sonypi_inputkeys[i].inputev)
1182 			set_bit(sonypi_inputkeys[i].inputev, key_dev->keybit);
1183 
1184 	error = input_register_device(jog_dev);
1185 	if (error)
1186 		goto err_free_keydev;
1187 
1188 	error = input_register_device(key_dev);
1189 	if (error)
1190 		goto err_unregister_jogdev;
1191 
1192 	return 0;
1193 
1194  err_unregister_jogdev:
1195 	input_unregister_device(jog_dev);
1196 	/* Set to NULL so we don't free it again below */
1197 	jog_dev = NULL;
1198  err_free_keydev:
1199 	input_free_device(key_dev);
1200 	sonypi_device.input_key_dev = NULL;
1201  err_free_jogdev:
1202 	input_free_device(jog_dev);
1203 	sonypi_device.input_jog_dev = NULL;
1204 
1205 	return error;
1206 }
1207 
1208 static int sonypi_setup_ioports(struct sonypi_device *dev,
1209 				const struct sonypi_ioport_list *ioport_list)
1210 {
1211 	/* try to detect if sony-laptop is being used and thus
1212 	 * has already requested one of the known ioports.
1213 	 * As in the deprecated check_region this is racy has we have
1214 	 * multiple ioports available and one of them can be requested
1215 	 * between this check and the subsequent request. Anyway, as an
1216 	 * attempt to be some more user-friendly as we currently are,
1217 	 * this is enough.
1218 	 */
1219 	const struct sonypi_ioport_list *check = ioport_list;
1220 	while (check_ioport && check->port1) {
1221 		if (!request_region(check->port1,
1222 				   sonypi_device.region_size,
1223 				   "Sony Programmable I/O Device Check")) {
1224 			printk(KERN_ERR "sonypi: ioport 0x%.4x busy, using sony-laptop? "
1225 					"if not use check_ioport=0\n",
1226 					check->port1);
1227 			return -EBUSY;
1228 		}
1229 		release_region(check->port1, sonypi_device.region_size);
1230 		check++;
1231 	}
1232 
1233 	while (ioport_list->port1) {
1234 
1235 		if (request_region(ioport_list->port1,
1236 				   sonypi_device.region_size,
1237 				   "Sony Programmable I/O Device")) {
1238 			dev->ioport1 = ioport_list->port1;
1239 			dev->ioport2 = ioport_list->port2;
1240 			return 0;
1241 		}
1242 		ioport_list++;
1243 	}
1244 
1245 	return -EBUSY;
1246 }
1247 
1248 static int sonypi_setup_irq(struct sonypi_device *dev,
1249 				      const struct sonypi_irq_list *irq_list)
1250 {
1251 	while (irq_list->irq) {
1252 
1253 		if (!request_irq(irq_list->irq, sonypi_irq,
1254 				 IRQF_SHARED, "sonypi", sonypi_irq)) {
1255 			dev->irq = irq_list->irq;
1256 			dev->bits = irq_list->bits;
1257 			return 0;
1258 		}
1259 		irq_list++;
1260 	}
1261 
1262 	return -EBUSY;
1263 }
1264 
1265 static void sonypi_display_info(void)
1266 {
1267 	printk(KERN_INFO "sonypi: detected type%d model, "
1268 	       "verbose = %d, fnkeyinit = %s, camera = %s, "
1269 	       "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
1270 	       sonypi_device.model,
1271 	       verbose,
1272 	       fnkeyinit ? "on" : "off",
1273 	       camera ? "on" : "off",
1274 	       compat ? "on" : "off",
1275 	       mask,
1276 	       useinput ? "on" : "off",
1277 	       SONYPI_ACPI_ACTIVE ? "on" : "off");
1278 	printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
1279 	       sonypi_device.irq,
1280 	       sonypi_device.ioport1, sonypi_device.ioport2);
1281 
1282 	if (minor == -1)
1283 		printk(KERN_INFO "sonypi: device allocated minor is %d\n",
1284 		       sonypi_misc_device.minor);
1285 }
1286 
1287 static int sonypi_probe(struct platform_device *dev)
1288 {
1289 	const struct sonypi_ioport_list *ioport_list;
1290 	const struct sonypi_irq_list *irq_list;
1291 	struct pci_dev *pcidev;
1292 	int error;
1293 
1294 	printk(KERN_WARNING "sonypi: please try the sony-laptop module instead "
1295 			"and report failures, see also "
1296 			"http://www.linux.it/~malattia/wiki/index.php/Sony_drivers\n");
1297 
1298 	spin_lock_init(&sonypi_device.fifo_lock);
1299 	error = kfifo_alloc(&sonypi_device.fifo, SONYPI_BUF_SIZE, GFP_KERNEL);
1300 	if (error) {
1301 		printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1302 		return error;
1303 	}
1304 
1305 	init_waitqueue_head(&sonypi_device.fifo_proc_list);
1306 	mutex_init(&sonypi_device.lock);
1307 	sonypi_device.bluetooth_power = -1;
1308 
1309 	if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1310 				     PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1311 		sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
1312 	else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1313 					  PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1314 		sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
1315 	else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1316 					  PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1317 		sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
1318 	else
1319 		sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
1320 
1321 	if (pcidev && pci_enable_device(pcidev)) {
1322 		printk(KERN_ERR "sonypi: pci_enable_device failed\n");
1323 		error = -EIO;
1324 		goto err_put_pcidev;
1325 	}
1326 
1327 	sonypi_device.dev = pcidev;
1328 
1329 	if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
1330 		ioport_list = sonypi_type1_ioport_list;
1331 		sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
1332 		sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
1333 		irq_list = sonypi_type1_irq_list;
1334 	} else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
1335 		ioport_list = sonypi_type2_ioport_list;
1336 		sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
1337 		sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
1338 		irq_list = sonypi_type2_irq_list;
1339 	} else {
1340 		ioport_list = sonypi_type3_ioport_list;
1341 		sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
1342 		sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
1343 		irq_list = sonypi_type3_irq_list;
1344 	}
1345 
1346 	error = sonypi_setup_ioports(&sonypi_device, ioport_list);
1347 	if (error) {
1348 		printk(KERN_ERR "sonypi: failed to request ioports\n");
1349 		goto err_disable_pcidev;
1350 	}
1351 
1352 	error = sonypi_setup_irq(&sonypi_device, irq_list);
1353 	if (error) {
1354 		printk(KERN_ERR "sonypi: request_irq failed\n");
1355 		goto err_free_ioports;
1356 	}
1357 
1358 	if (minor != -1)
1359 		sonypi_misc_device.minor = minor;
1360 	error = misc_register(&sonypi_misc_device);
1361 	if (error) {
1362 		printk(KERN_ERR "sonypi: misc_register failed\n");
1363 		goto err_free_irq;
1364 	}
1365 
1366 	sonypi_display_info();
1367 
1368 	if (useinput) {
1369 
1370 		error = sonypi_create_input_devices(dev);
1371 		if (error) {
1372 			printk(KERN_ERR
1373 				"sonypi: failed to create input devices\n");
1374 			goto err_miscdev_unregister;
1375 		}
1376 
1377 		spin_lock_init(&sonypi_device.input_fifo_lock);
1378 		error = kfifo_alloc(&sonypi_device.input_fifo, SONYPI_BUF_SIZE,
1379 				GFP_KERNEL);
1380 		if (error) {
1381 			printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1382 			goto err_inpdev_unregister;
1383 		}
1384 
1385 		INIT_WORK(&sonypi_device.input_work, input_keyrelease);
1386 	}
1387 
1388 	sonypi_enable(0);
1389 
1390 	return 0;
1391 
1392  err_inpdev_unregister:
1393 	input_unregister_device(sonypi_device.input_key_dev);
1394 	input_unregister_device(sonypi_device.input_jog_dev);
1395  err_miscdev_unregister:
1396 	misc_deregister(&sonypi_misc_device);
1397  err_free_irq:
1398 	free_irq(sonypi_device.irq, sonypi_irq);
1399  err_free_ioports:
1400 	release_region(sonypi_device.ioport1, sonypi_device.region_size);
1401  err_disable_pcidev:
1402 	if (pcidev)
1403 		pci_disable_device(pcidev);
1404  err_put_pcidev:
1405 	pci_dev_put(pcidev);
1406 	kfifo_free(&sonypi_device.fifo);
1407 
1408 	return error;
1409 }
1410 
1411 static int sonypi_remove(struct platform_device *dev)
1412 {
1413 	sonypi_disable();
1414 
1415 	synchronize_irq(sonypi_device.irq);
1416 	flush_work(&sonypi_device.input_work);
1417 
1418 	if (useinput) {
1419 		input_unregister_device(sonypi_device.input_key_dev);
1420 		input_unregister_device(sonypi_device.input_jog_dev);
1421 		kfifo_free(&sonypi_device.input_fifo);
1422 	}
1423 
1424 	misc_deregister(&sonypi_misc_device);
1425 
1426 	free_irq(sonypi_device.irq, sonypi_irq);
1427 	release_region(sonypi_device.ioport1, sonypi_device.region_size);
1428 
1429 	if (sonypi_device.dev) {
1430 		pci_disable_device(sonypi_device.dev);
1431 		pci_dev_put(sonypi_device.dev);
1432 	}
1433 
1434 	kfifo_free(&sonypi_device.fifo);
1435 
1436 	return 0;
1437 }
1438 
1439 #ifdef CONFIG_PM_SLEEP
1440 static int old_camera_power;
1441 
1442 static int sonypi_suspend(struct device *dev)
1443 {
1444 	old_camera_power = sonypi_device.camera_power;
1445 	sonypi_disable();
1446 
1447 	return 0;
1448 }
1449 
1450 static int sonypi_resume(struct device *dev)
1451 {
1452 	sonypi_enable(old_camera_power);
1453 	return 0;
1454 }
1455 
1456 static SIMPLE_DEV_PM_OPS(sonypi_pm, sonypi_suspend, sonypi_resume);
1457 #define SONYPI_PM	(&sonypi_pm)
1458 #else
1459 #define SONYPI_PM	NULL
1460 #endif
1461 
1462 static void sonypi_shutdown(struct platform_device *dev)
1463 {
1464 	sonypi_disable();
1465 }
1466 
1467 static struct platform_driver sonypi_driver = {
1468 	.driver		= {
1469 		.name	= "sonypi",
1470 		.pm	= SONYPI_PM,
1471 	},
1472 	.probe		= sonypi_probe,
1473 	.remove		= sonypi_remove,
1474 	.shutdown	= sonypi_shutdown,
1475 };
1476 
1477 static struct platform_device *sonypi_platform_device;
1478 
1479 static const struct dmi_system_id sonypi_dmi_table[] __initconst = {
1480 	{
1481 		.ident = "Sony Vaio",
1482 		.matches = {
1483 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1484 			DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1485 		},
1486 	},
1487 	{
1488 		.ident = "Sony Vaio",
1489 		.matches = {
1490 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1491 			DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1492 		},
1493 	},
1494 	{ }
1495 };
1496 
1497 static int __init sonypi_init(void)
1498 {
1499 	int error;
1500 
1501 	printk(KERN_INFO
1502 		"sonypi: Sony Programmable I/O Controller Driver v%s.\n",
1503 		SONYPI_DRIVER_VERSION);
1504 
1505 	if (!dmi_check_system(sonypi_dmi_table))
1506 		return -ENODEV;
1507 
1508 	error = platform_driver_register(&sonypi_driver);
1509 	if (error)
1510 		return error;
1511 
1512 	sonypi_platform_device = platform_device_alloc("sonypi", -1);
1513 	if (!sonypi_platform_device) {
1514 		error = -ENOMEM;
1515 		goto err_driver_unregister;
1516 	}
1517 
1518 	error = platform_device_add(sonypi_platform_device);
1519 	if (error)
1520 		goto err_free_device;
1521 
1522 #ifdef CONFIG_ACPI
1523 	if (acpi_bus_register_driver(&sonypi_acpi_driver) >= 0)
1524 		acpi_driver_registered = 1;
1525 #endif
1526 
1527 	return 0;
1528 
1529  err_free_device:
1530 	platform_device_put(sonypi_platform_device);
1531  err_driver_unregister:
1532 	platform_driver_unregister(&sonypi_driver);
1533 	return error;
1534 }
1535 
1536 static void __exit sonypi_exit(void)
1537 {
1538 #ifdef CONFIG_ACPI
1539 	if (acpi_driver_registered)
1540 		acpi_bus_unregister_driver(&sonypi_acpi_driver);
1541 #endif
1542 	platform_device_unregister(sonypi_platform_device);
1543 	platform_driver_unregister(&sonypi_driver);
1544 	printk(KERN_INFO "sonypi: removed.\n");
1545 }
1546 
1547 module_init(sonypi_init);
1548 module_exit(sonypi_exit);
1549