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