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