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