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