35cf1edbc1795439d9e28478aa93edba91adc369
[sfrench/cifs-2.6.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
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, 0x08, SONYPI_HELP_MASK, sonypi_helpev },
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 /* The name of the devices for the input device drivers */
429 #define SONYPI_JOG_INPUTNAME    "Sony Vaio Jogdial"
430 #define SONYPI_KEY_INPUTNAME    "Sony Vaio Keys"
431
432 /* Correspondance table between sonypi events and input layer events */
433 static struct {
434         int sonypiev;
435         int inputev;
436 } sonypi_inputkeys[] = {
437         { SONYPI_EVENT_CAPTURE_PRESSED,         KEY_CAMERA },
438         { SONYPI_EVENT_FNKEY_ONLY,              KEY_FN },
439         { SONYPI_EVENT_FNKEY_ESC,               KEY_FN_ESC },
440         { SONYPI_EVENT_FNKEY_F1,                KEY_FN_F1 },
441         { SONYPI_EVENT_FNKEY_F2,                KEY_FN_F2 },
442         { SONYPI_EVENT_FNKEY_F3,                KEY_FN_F3 },
443         { SONYPI_EVENT_FNKEY_F4,                KEY_FN_F4 },
444         { SONYPI_EVENT_FNKEY_F5,                KEY_FN_F5 },
445         { SONYPI_EVENT_FNKEY_F6,                KEY_FN_F6 },
446         { SONYPI_EVENT_FNKEY_F7,                KEY_FN_F7 },
447         { SONYPI_EVENT_FNKEY_F8,                KEY_FN_F8 },
448         { SONYPI_EVENT_FNKEY_F9,                KEY_FN_F9 },
449         { SONYPI_EVENT_FNKEY_F10,               KEY_FN_F10 },
450         { SONYPI_EVENT_FNKEY_F11,               KEY_FN_F11 },
451         { SONYPI_EVENT_FNKEY_F12,               KEY_FN_F12 },
452         { SONYPI_EVENT_FNKEY_1,                 KEY_FN_1 },
453         { SONYPI_EVENT_FNKEY_2,                 KEY_FN_2 },
454         { SONYPI_EVENT_FNKEY_D,                 KEY_FN_D },
455         { SONYPI_EVENT_FNKEY_E,                 KEY_FN_E },
456         { SONYPI_EVENT_FNKEY_F,                 KEY_FN_F },
457         { SONYPI_EVENT_FNKEY_S,                 KEY_FN_S },
458         { SONYPI_EVENT_FNKEY_B,                 KEY_FN_B },
459         { SONYPI_EVENT_BLUETOOTH_PRESSED,       KEY_BLUE },
460         { SONYPI_EVENT_BLUETOOTH_ON,            KEY_BLUE },
461         { SONYPI_EVENT_PKEY_P1,                 KEY_PROG1 },
462         { SONYPI_EVENT_PKEY_P2,                 KEY_PROG2 },
463         { SONYPI_EVENT_PKEY_P3,                 KEY_PROG3 },
464         { SONYPI_EVENT_BACK_PRESSED,            KEY_BACK },
465         { SONYPI_EVENT_HELP_PRESSED,            KEY_HELP },
466         { SONYPI_EVENT_ZOOM_PRESSED,            KEY_ZOOM },
467         { SONYPI_EVENT_THUMBPHRASE_PRESSED,     BTN_THUMB },
468         { 0, 0 },
469 };
470
471 struct sonypi_keypress {
472         struct input_dev *dev;
473         int key;
474 };
475
476 static struct sonypi_device {
477         struct pci_dev *dev;
478         struct platform_device *pdev;
479         u16 irq;
480         u16 bits;
481         u16 ioport1;
482         u16 ioport2;
483         u16 region_size;
484         u16 evtype_offset;
485         int camera_power;
486         int bluetooth_power;
487         struct semaphore lock;
488         struct kfifo *fifo;
489         spinlock_t fifo_lock;
490         wait_queue_head_t fifo_proc_list;
491         struct fasync_struct *fifo_async;
492         int open_count;
493         int model;
494         struct input_dev input_jog_dev;
495         struct input_dev input_key_dev;
496         struct work_struct input_work;
497         struct kfifo *input_fifo;
498         spinlock_t input_fifo_lock;
499 } sonypi_device;
500
501 #define ITERATIONS_LONG         10000
502 #define ITERATIONS_SHORT        10
503
504 #define wait_on_command(quiet, command, iterations) { \
505         unsigned int n = iterations; \
506         while (--n && (command)) \
507                 udelay(1); \
508         if (!n && (verbose || !quiet)) \
509                 printk(KERN_WARNING "sonypi command failed at %s : %s (line %d)\n", __FILE__, __FUNCTION__, __LINE__); \
510 }
511
512 #ifdef CONFIG_ACPI
513 #define SONYPI_ACPI_ACTIVE (!acpi_disabled)
514 #else
515 #define SONYPI_ACPI_ACTIVE 0
516 #endif                          /* CONFIG_ACPI */
517
518 static int sonypi_ec_write(u8 addr, u8 value)
519 {
520 #ifdef CONFIG_ACPI_EC
521         if (SONYPI_ACPI_ACTIVE)
522                 return ec_write(addr, value);
523 #endif
524         wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
525         outb_p(0x81, SONYPI_CST_IOPORT);
526         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
527         outb_p(addr, SONYPI_DATA_IOPORT);
528         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
529         outb_p(value, SONYPI_DATA_IOPORT);
530         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
531         return 0;
532 }
533
534 static int sonypi_ec_read(u8 addr, u8 *value)
535 {
536 #ifdef CONFIG_ACPI_EC
537         if (SONYPI_ACPI_ACTIVE)
538                 return ec_read(addr, value);
539 #endif
540         wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
541         outb_p(0x80, SONYPI_CST_IOPORT);
542         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
543         outb_p(addr, SONYPI_DATA_IOPORT);
544         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
545         *value = inb_p(SONYPI_DATA_IOPORT);
546         return 0;
547 }
548
549 static int ec_read16(u8 addr, u16 *value)
550 {
551         u8 val_lb, val_hb;
552         if (sonypi_ec_read(addr, &val_lb))
553                 return -1;
554         if (sonypi_ec_read(addr + 1, &val_hb))
555                 return -1;
556         *value = val_lb | (val_hb << 8);
557         return 0;
558 }
559
560 /* Initializes the device - this comes from the AML code in the ACPI bios */
561 static void sonypi_type1_srs(void)
562 {
563         u32 v;
564
565         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
566         v = (v & 0xFFFF0000) | ((u32) sonypi_device.ioport1);
567         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
568
569         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
570         v = (v & 0xFFF0FFFF) |
571             (((u32) sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
572         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
573
574         v = inl(SONYPI_IRQ_PORT);
575         v &= ~(((u32) 0x3) << SONYPI_IRQ_SHIFT);
576         v |= (((u32) sonypi_device.bits) << SONYPI_IRQ_SHIFT);
577         outl(v, SONYPI_IRQ_PORT);
578
579         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
580         v = (v & 0xFF1FFFFF) | 0x00C00000;
581         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
582 }
583
584 static void sonypi_type2_srs(void)
585 {
586         if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
587                 printk(KERN_WARNING "ec_write failed\n");
588         if (sonypi_ec_write(SONYPI_SLOB, sonypi_device.ioport1 & 0x00FF))
589                 printk(KERN_WARNING "ec_write failed\n");
590         if (sonypi_ec_write(SONYPI_SIRQ, sonypi_device.bits))
591                 printk(KERN_WARNING "ec_write failed\n");
592         udelay(10);
593 }
594
595 static void sonypi_type3_srs(void)
596 {
597         u16 v16;
598         u8  v8;
599
600         /* This model type uses the same initialiazation of
601          * the embedded controller as the type2 models. */
602         sonypi_type2_srs();
603
604         /* Initialization of PCI config space of the LPC interface bridge. */
605         v16 = (sonypi_device.ioport1 & 0xFFF0) | 0x01;
606         pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, v16);
607         pci_read_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, &v8);
608         v8 = (v8 & 0xCF) | 0x10;
609         pci_write_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, v8);
610 }
611
612 /* Disables the device - this comes from the AML code in the ACPI bios */
613 static void sonypi_type1_dis(void)
614 {
615         u32 v;
616
617         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
618         v = v & 0xFF3FFFFF;
619         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
620
621         v = inl(SONYPI_IRQ_PORT);
622         v |= (0x3 << SONYPI_IRQ_SHIFT);
623         outl(v, SONYPI_IRQ_PORT);
624 }
625
626 static void sonypi_type2_dis(void)
627 {
628         if (sonypi_ec_write(SONYPI_SHIB, 0))
629                 printk(KERN_WARNING "ec_write failed\n");
630         if (sonypi_ec_write(SONYPI_SLOB, 0))
631                 printk(KERN_WARNING "ec_write failed\n");
632         if (sonypi_ec_write(SONYPI_SIRQ, 0))
633                 printk(KERN_WARNING "ec_write failed\n");
634 }
635
636 static void sonypi_type3_dis(void)
637 {
638         sonypi_type2_dis();
639         udelay(10);
640         pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, 0);
641 }
642
643 static u8 sonypi_call1(u8 dev)
644 {
645         u8 v1, v2;
646
647         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
648         outb(dev, sonypi_device.ioport2);
649         v1 = inb_p(sonypi_device.ioport2);
650         v2 = inb_p(sonypi_device.ioport1);
651         return v2;
652 }
653
654 static u8 sonypi_call2(u8 dev, u8 fn)
655 {
656         u8 v1;
657
658         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
659         outb(dev, sonypi_device.ioport2);
660         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
661         outb(fn, sonypi_device.ioport1);
662         v1 = inb_p(sonypi_device.ioport1);
663         return v1;
664 }
665
666 static u8 sonypi_call3(u8 dev, u8 fn, u8 v)
667 {
668         u8 v1;
669
670         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
671         outb(dev, sonypi_device.ioport2);
672         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
673         outb(fn, sonypi_device.ioport1);
674         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
675         outb(v, sonypi_device.ioport1);
676         v1 = inb_p(sonypi_device.ioport1);
677         return v1;
678 }
679
680 #if 0
681 /* Get brightness, hue etc. Unreliable... */
682 static u8 sonypi_read(u8 fn)
683 {
684         u8 v1, v2;
685         int n = 100;
686
687         while (n--) {
688                 v1 = sonypi_call2(0x8f, fn);
689                 v2 = sonypi_call2(0x8f, fn);
690                 if (v1 == v2 && v1 != 0xff)
691                         return v1;
692         }
693         return 0xff;
694 }
695 #endif
696
697 /* Set brightness, hue etc */
698 static void sonypi_set(u8 fn, u8 v)
699 {
700         wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
701 }
702
703 /* Tests if the camera is ready */
704 static int sonypi_camera_ready(void)
705 {
706         u8 v;
707
708         v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
709         return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
710 }
711
712 /* Turns the camera off */
713 static void sonypi_camera_off(void)
714 {
715         sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
716
717         if (!sonypi_device.camera_power)
718                 return;
719
720         sonypi_call2(0x91, 0);
721         sonypi_device.camera_power = 0;
722 }
723
724 /* Turns the camera on */
725 static void sonypi_camera_on(void)
726 {
727         int i, j;
728
729         if (sonypi_device.camera_power)
730                 return;
731
732         for (j = 5; j > 0; j--) {
733
734                 while (sonypi_call2(0x91, 0x1))
735                         msleep(10);
736                 sonypi_call1(0x93);
737
738                 for (i = 400; i > 0; i--) {
739                         if (sonypi_camera_ready())
740                                 break;
741                         msleep(10);
742                 }
743                 if (i)
744                         break;
745         }
746
747         if (j == 0) {
748                 printk(KERN_WARNING "sonypi: failed to power on camera\n");
749                 return;
750         }
751
752         sonypi_set(0x10, 0x5a);
753         sonypi_device.camera_power = 1;
754 }
755
756 /* sets the bluetooth subsystem power state */
757 static void sonypi_setbluetoothpower(u8 state)
758 {
759         state = !!state;
760
761         if (sonypi_device.bluetooth_power == state)
762                 return;
763
764         sonypi_call2(0x96, state);
765         sonypi_call1(0x82);
766         sonypi_device.bluetooth_power = state;
767 }
768
769 static void input_keyrelease(void *data)
770 {
771         struct sonypi_keypress kp;
772
773         while (kfifo_get(sonypi_device.input_fifo, (unsigned char *)&kp,
774                          sizeof(kp)) == sizeof(kp)) {
775                 msleep(10);
776                 input_report_key(kp.dev, kp.key, 0);
777                 input_sync(kp.dev);
778         }
779 }
780
781 static void sonypi_report_input_event(u8 event)
782 {
783         struct input_dev *jog_dev = &sonypi_device.input_jog_dev;
784         struct input_dev *key_dev = &sonypi_device.input_key_dev;
785         struct sonypi_keypress kp = { NULL };
786         int i;
787
788         switch (event) {
789         case SONYPI_EVENT_JOGDIAL_UP:
790         case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
791                 input_report_rel(jog_dev, REL_WHEEL, 1);
792                 input_sync(jog_dev);
793                 break;
794
795         case SONYPI_EVENT_JOGDIAL_DOWN:
796         case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
797                 input_report_rel(jog_dev, REL_WHEEL, -1);
798                 input_sync(jog_dev);
799                 break;
800
801         case SONYPI_EVENT_JOGDIAL_PRESSED:
802                 kp.key = BTN_MIDDLE;
803                 kp.dev = jog_dev;
804                 break;
805
806         case SONYPI_EVENT_FNKEY_RELEASED:
807                 /* Nothing, not all VAIOs generate this event */
808                 break;
809
810         default:
811                 for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
812                         if (event == sonypi_inputkeys[i].sonypiev) {
813                                 kp.dev = key_dev;
814                                 kp.key = sonypi_inputkeys[i].inputev;
815                                 break;
816                         }
817                 break;
818         }
819
820         if (kp.dev) {
821                 input_report_key(kp.dev, kp.key, 1);
822                 input_sync(kp.dev);
823                 kfifo_put(sonypi_device.input_fifo,
824                           (unsigned char *)&kp, sizeof(kp));
825                 schedule_work(&sonypi_device.input_work);
826         }
827 }
828
829 /* Interrupt handler: some event is available */
830 static irqreturn_t sonypi_irq(int irq, void *dev_id, struct pt_regs *regs)
831 {
832         u8 v1, v2, event = 0;
833         int i, j;
834
835         v1 = inb_p(sonypi_device.ioport1);
836         v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
837
838         for (i = 0; sonypi_eventtypes[i].model; i++) {
839                 if (sonypi_device.model != sonypi_eventtypes[i].model)
840                         continue;
841                 if ((v2 & sonypi_eventtypes[i].data) !=
842                     sonypi_eventtypes[i].data)
843                         continue;
844                 if (!(mask & sonypi_eventtypes[i].mask))
845                         continue;
846                 for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
847                         if (v1 == sonypi_eventtypes[i].events[j].data) {
848                                 event = sonypi_eventtypes[i].events[j].event;
849                                 goto found;
850                         }
851                 }
852         }
853
854         if (verbose)
855                 printk(KERN_WARNING
856                        "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",
857                        v1, v2);
858         /* We need to return IRQ_HANDLED here because there *are*
859          * events belonging to the sonypi device we don't know about,
860          * but we still don't want those to pollute the logs... */
861         return IRQ_HANDLED;
862
863 found:
864         if (verbose > 1)
865                 printk(KERN_INFO
866                        "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
867
868         if (useinput)
869                 sonypi_report_input_event(event);
870
871         kfifo_put(sonypi_device.fifo, (unsigned char *)&event, sizeof(event));
872         kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
873         wake_up_interruptible(&sonypi_device.fifo_proc_list);
874
875         return IRQ_HANDLED;
876 }
877
878 /* External camera command (exported to the motion eye v4l driver) */
879 int sonypi_camera_command(int command, u8 value)
880 {
881         if (!camera)
882                 return -EIO;
883
884         down(&sonypi_device.lock);
885
886         switch (command) {
887         case SONYPI_COMMAND_SETCAMERA:
888                 if (value)
889                         sonypi_camera_on();
890                 else
891                         sonypi_camera_off();
892                 break;
893         case SONYPI_COMMAND_SETCAMERABRIGHTNESS:
894                 sonypi_set(SONYPI_CAMERA_BRIGHTNESS, value);
895                 break;
896         case SONYPI_COMMAND_SETCAMERACONTRAST:
897                 sonypi_set(SONYPI_CAMERA_CONTRAST, value);
898                 break;
899         case SONYPI_COMMAND_SETCAMERAHUE:
900                 sonypi_set(SONYPI_CAMERA_HUE, value);
901                 break;
902         case SONYPI_COMMAND_SETCAMERACOLOR:
903                 sonypi_set(SONYPI_CAMERA_COLOR, value);
904                 break;
905         case SONYPI_COMMAND_SETCAMERASHARPNESS:
906                 sonypi_set(SONYPI_CAMERA_SHARPNESS, value);
907                 break;
908         case SONYPI_COMMAND_SETCAMERAPICTURE:
909                 sonypi_set(SONYPI_CAMERA_PICTURE, value);
910                 break;
911         case SONYPI_COMMAND_SETCAMERAAGC:
912                 sonypi_set(SONYPI_CAMERA_AGC, value);
913                 break;
914         default:
915                 printk(KERN_ERR "sonypi: sonypi_camera_command invalid: %d\n",
916                        command);
917                 break;
918         }
919         up(&sonypi_device.lock);
920         return 0;
921 }
922
923 EXPORT_SYMBOL(sonypi_camera_command);
924
925 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
926 {
927         int retval;
928
929         retval = fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
930         if (retval < 0)
931                 return retval;
932         return 0;
933 }
934
935 static int sonypi_misc_release(struct inode *inode, struct file *file)
936 {
937         sonypi_misc_fasync(-1, file, 0);
938         down(&sonypi_device.lock);
939         sonypi_device.open_count--;
940         up(&sonypi_device.lock);
941         return 0;
942 }
943
944 static int sonypi_misc_open(struct inode *inode, struct file *file)
945 {
946         down(&sonypi_device.lock);
947         /* Flush input queue on first open */
948         if (!sonypi_device.open_count)
949                 kfifo_reset(sonypi_device.fifo);
950         sonypi_device.open_count++;
951         up(&sonypi_device.lock);
952         return 0;
953 }
954
955 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
956                                 size_t count, loff_t *pos)
957 {
958         ssize_t ret;
959         unsigned char c;
960
961         if ((kfifo_len(sonypi_device.fifo) == 0) &&
962             (file->f_flags & O_NONBLOCK))
963                 return -EAGAIN;
964
965         ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
966                                        kfifo_len(sonypi_device.fifo) != 0);
967         if (ret)
968                 return ret;
969
970         while (ret < count &&
971                (kfifo_get(sonypi_device.fifo, &c, sizeof(c)) == sizeof(c))) {
972                 if (put_user(c, buf++))
973                         return -EFAULT;
974                 ret++;
975         }
976
977         if (ret > 0) {
978                 struct inode *inode = file->f_dentry->d_inode;
979                 inode->i_atime = current_fs_time(inode->i_sb);
980         }
981
982         return ret;
983 }
984
985 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
986 {
987         poll_wait(file, &sonypi_device.fifo_proc_list, wait);
988         if (kfifo_len(sonypi_device.fifo))
989                 return POLLIN | POLLRDNORM;
990         return 0;
991 }
992
993 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
994                              unsigned int cmd, unsigned long arg)
995 {
996         int ret = 0;
997         void __user *argp = (void __user *)arg;
998         u8 val8;
999         u16 val16;
1000
1001         down(&sonypi_device.lock);
1002         switch (cmd) {
1003         case SONYPI_IOCGBRT:
1004                 if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
1005                         ret = -EIO;
1006                         break;
1007                 }
1008                 if (copy_to_user(argp, &val8, sizeof(val8)))
1009                         ret = -EFAULT;
1010                 break;
1011         case SONYPI_IOCSBRT:
1012                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1013                         ret = -EFAULT;
1014                         break;
1015                 }
1016                 if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
1017                         ret = -EIO;
1018                 break;
1019         case SONYPI_IOCGBAT1CAP:
1020                 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1021                         ret = -EIO;
1022                         break;
1023                 }
1024                 if (copy_to_user(argp, &val16, sizeof(val16)))
1025                         ret = -EFAULT;
1026                 break;
1027         case SONYPI_IOCGBAT1REM:
1028                 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1029                         ret = -EIO;
1030                         break;
1031                 }
1032                 if (copy_to_user(argp, &val16, sizeof(val16)))
1033                         ret = -EFAULT;
1034                 break;
1035         case SONYPI_IOCGBAT2CAP:
1036                 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1037                         ret = -EIO;
1038                         break;
1039                 }
1040                 if (copy_to_user(argp, &val16, sizeof(val16)))
1041                         ret = -EFAULT;
1042                 break;
1043         case SONYPI_IOCGBAT2REM:
1044                 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1045                         ret = -EIO;
1046                         break;
1047                 }
1048                 if (copy_to_user(argp, &val16, sizeof(val16)))
1049                         ret = -EFAULT;
1050                 break;
1051         case SONYPI_IOCGBATFLAGS:
1052                 if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
1053                         ret = -EIO;
1054                         break;
1055                 }
1056                 val8 &= 0x07;
1057                 if (copy_to_user(argp, &val8, sizeof(val8)))
1058                         ret = -EFAULT;
1059                 break;
1060         case SONYPI_IOCGBLUE:
1061                 val8 = sonypi_device.bluetooth_power;
1062                 if (copy_to_user(argp, &val8, sizeof(val8)))
1063                         ret = -EFAULT;
1064                 break;
1065         case SONYPI_IOCSBLUE:
1066                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1067                         ret = -EFAULT;
1068                         break;
1069                 }
1070                 sonypi_setbluetoothpower(val8);
1071                 break;
1072         /* FAN Controls */
1073         case SONYPI_IOCGFAN:
1074                 if (sonypi_ec_read(SONYPI_FAN0_STATUS, &val8)) {
1075                         ret = -EIO;
1076                         break;
1077                 }
1078                 if (copy_to_user(argp, &val8, sizeof(val8)))
1079                         ret = -EFAULT;
1080                 break;
1081         case SONYPI_IOCSFAN:
1082                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1083                         ret = -EFAULT;
1084                         break;
1085                 }
1086                 if (sonypi_ec_write(SONYPI_FAN0_STATUS, val8))
1087                         ret = -EIO;
1088                 break;
1089         /* GET Temperature (useful under APM) */
1090         case SONYPI_IOCGTEMP:
1091                 if (sonypi_ec_read(SONYPI_TEMP_STATUS, &val8)) {
1092                         ret = -EIO;
1093                         break;
1094                 }
1095                 if (copy_to_user(argp, &val8, sizeof(val8)))
1096                         ret = -EFAULT;
1097                 break;
1098         default:
1099                 ret = -EINVAL;
1100         }
1101         up(&sonypi_device.lock);
1102         return ret;
1103 }
1104
1105 static struct file_operations sonypi_misc_fops = {
1106         .owner          = THIS_MODULE,
1107         .read           = sonypi_misc_read,
1108         .poll           = sonypi_misc_poll,
1109         .open           = sonypi_misc_open,
1110         .release        = sonypi_misc_release,
1111         .fasync         = sonypi_misc_fasync,
1112         .ioctl          = sonypi_misc_ioctl,
1113 };
1114
1115 static struct miscdevice sonypi_misc_device = {
1116         .minor          = MISC_DYNAMIC_MINOR,
1117         .name           = "sonypi",
1118         .fops           = &sonypi_misc_fops,
1119 };
1120
1121 static void sonypi_enable(unsigned int camera_on)
1122 {
1123         switch (sonypi_device.model) {
1124         case SONYPI_DEVICE_MODEL_TYPE1:
1125                 sonypi_type1_srs();
1126                 break;
1127         case SONYPI_DEVICE_MODEL_TYPE2:
1128                 sonypi_type2_srs();
1129                 break;
1130         case SONYPI_DEVICE_MODEL_TYPE3:
1131                 sonypi_type3_srs();
1132                 break;
1133         }
1134
1135         sonypi_call1(0x82);
1136         sonypi_call2(0x81, 0xff);
1137         sonypi_call1(compat ? 0x92 : 0x82);
1138
1139         /* Enable ACPI mode to get Fn key events */
1140         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1141                 outb(0xf0, 0xb2);
1142
1143         if (camera && camera_on)
1144                 sonypi_camera_on();
1145 }
1146
1147 static int sonypi_disable(void)
1148 {
1149         sonypi_call2(0x81, 0);  /* make sure we don't get any more events */
1150         if (camera)
1151                 sonypi_camera_off();
1152
1153         /* disable ACPI mode */
1154         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1155                 outb(0xf1, 0xb2);
1156
1157         switch (sonypi_device.model) {
1158         case SONYPI_DEVICE_MODEL_TYPE1:
1159                 sonypi_type1_dis();
1160                 break;
1161         case SONYPI_DEVICE_MODEL_TYPE2:
1162                 sonypi_type2_dis();
1163                 break;
1164         case SONYPI_DEVICE_MODEL_TYPE3:
1165                 sonypi_type3_dis();
1166                 break;
1167         }
1168
1169         return 0;
1170 }
1171
1172 #ifdef CONFIG_PM
1173 static int old_camera_power;
1174
1175 static int sonypi_suspend(struct device *dev, pm_message_t state, u32 level)
1176 {
1177         if (level == SUSPEND_DISABLE) {
1178                 old_camera_power = sonypi_device.camera_power;
1179                 sonypi_disable();
1180         }
1181         return 0;
1182 }
1183
1184 static int sonypi_resume(struct device *dev, u32 level)
1185 {
1186         if (level == RESUME_ENABLE)
1187                 sonypi_enable(old_camera_power);
1188         return 0;
1189 }
1190 #endif
1191
1192 static void sonypi_shutdown(struct device *dev)
1193 {
1194         sonypi_disable();
1195 }
1196
1197 static struct device_driver sonypi_driver = {
1198         .name           = "sonypi",
1199         .bus            = &platform_bus_type,
1200 #ifdef CONFIG_PM
1201         .suspend        = sonypi_suspend,
1202         .resume         = sonypi_resume,
1203 #endif
1204         .shutdown       = sonypi_shutdown,
1205 };
1206
1207 static int __devinit sonypi_probe(void)
1208 {
1209         int i, ret;
1210         struct sonypi_ioport_list *ioport_list;
1211         struct sonypi_irq_list *irq_list;
1212         struct pci_dev *pcidev;
1213
1214         if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1215                                      PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1216                 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
1217         else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1218                                           PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1219                 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
1220         else
1221                 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
1222
1223         sonypi_device.dev = pcidev;
1224
1225         spin_lock_init(&sonypi_device.fifo_lock);
1226         sonypi_device.fifo = kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
1227                                          &sonypi_device.fifo_lock);
1228         if (IS_ERR(sonypi_device.fifo)) {
1229                 printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1230                 ret = PTR_ERR(sonypi_device.fifo);
1231                 goto out_fifo;
1232         }
1233
1234         init_waitqueue_head(&sonypi_device.fifo_proc_list);
1235         init_MUTEX(&sonypi_device.lock);
1236         sonypi_device.bluetooth_power = -1;
1237
1238         if (pcidev && pci_enable_device(pcidev)) {
1239                 printk(KERN_ERR "sonypi: pci_enable_device failed\n");
1240                 ret = -EIO;
1241                 goto out_pcienable;
1242         }
1243
1244         if (minor != -1)
1245                 sonypi_misc_device.minor = minor;
1246         if ((ret = misc_register(&sonypi_misc_device))) {
1247                 printk(KERN_ERR "sonypi: misc_register failed\n");
1248                 goto out_miscreg;
1249         }
1250
1251
1252         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
1253                 ioport_list = sonypi_type1_ioport_list;
1254                 sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
1255                 sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
1256                 irq_list = sonypi_type1_irq_list;
1257         } else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
1258                 ioport_list = sonypi_type2_ioport_list;
1259                 sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
1260                 sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
1261                 irq_list = sonypi_type2_irq_list;
1262         } else {
1263                 ioport_list = sonypi_type3_ioport_list;
1264                 sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
1265                 sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
1266                 irq_list = sonypi_type3_irq_list;
1267         }
1268
1269         for (i = 0; ioport_list[i].port1; i++) {
1270                 if (request_region(ioport_list[i].port1,
1271                                    sonypi_device.region_size,
1272                                    "Sony Programable I/O Device")) {
1273                         /* get the ioport */
1274                         sonypi_device.ioport1 = ioport_list[i].port1;
1275                         sonypi_device.ioport2 = ioport_list[i].port2;
1276                         break;
1277                 }
1278         }
1279         if (!sonypi_device.ioport1) {
1280                 printk(KERN_ERR "sonypi: request_region failed\n");
1281                 ret = -ENODEV;
1282                 goto out_reqreg;
1283         }
1284
1285         for (i = 0; irq_list[i].irq; i++) {
1286
1287                 sonypi_device.irq = irq_list[i].irq;
1288                 sonypi_device.bits = irq_list[i].bits;
1289
1290                 if (!request_irq(sonypi_device.irq, sonypi_irq,
1291                                  SA_SHIRQ, "sonypi", sonypi_irq))
1292                         break;
1293         }
1294
1295         if (!irq_list[i].irq) {
1296                 printk(KERN_ERR "sonypi: request_irq failed\n");
1297                 ret = -ENODEV;
1298                 goto out_reqirq;
1299         }
1300
1301         if (useinput) {
1302                 /* Initialize the Input Drivers: jogdial */
1303                 int i;
1304                 sonypi_device.input_jog_dev.evbit[0] =
1305                         BIT(EV_KEY) | BIT(EV_REL);
1306                 sonypi_device.input_jog_dev.keybit[LONG(BTN_MOUSE)] =
1307                         BIT(BTN_MIDDLE);
1308                 sonypi_device.input_jog_dev.relbit[0] = BIT(REL_WHEEL);
1309                 sonypi_device.input_jog_dev.name = SONYPI_JOG_INPUTNAME;
1310                 sonypi_device.input_jog_dev.id.bustype = BUS_ISA;
1311                 sonypi_device.input_jog_dev.id.vendor = PCI_VENDOR_ID_SONY;
1312
1313                 input_register_device(&sonypi_device.input_jog_dev);
1314                 printk(KERN_INFO "%s input method installed.\n",
1315                        sonypi_device.input_jog_dev.name);
1316
1317                 /* Initialize the Input Drivers: special keys */
1318                 sonypi_device.input_key_dev.evbit[0] = BIT(EV_KEY);
1319                 for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
1320                         if (sonypi_inputkeys[i].inputev)
1321                                 set_bit(sonypi_inputkeys[i].inputev,
1322                                         sonypi_device.input_key_dev.keybit);
1323                 sonypi_device.input_key_dev.name = SONYPI_KEY_INPUTNAME;
1324                 sonypi_device.input_key_dev.id.bustype = BUS_ISA;
1325                 sonypi_device.input_key_dev.id.vendor = PCI_VENDOR_ID_SONY;
1326
1327                 input_register_device(&sonypi_device.input_key_dev);
1328                 printk(KERN_INFO "%s input method installed.\n",
1329                        sonypi_device.input_key_dev.name);
1330
1331                 spin_lock_init(&sonypi_device.input_fifo_lock);
1332                 sonypi_device.input_fifo =
1333                         kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
1334                                     &sonypi_device.input_fifo_lock);
1335                 if (IS_ERR(sonypi_device.input_fifo)) {
1336                         printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1337                         ret = PTR_ERR(sonypi_device.input_fifo);
1338                         goto out_infifo;
1339                 }
1340
1341                 INIT_WORK(&sonypi_device.input_work, input_keyrelease, NULL);
1342         }
1343
1344         sonypi_device.pdev = platform_device_register_simple("sonypi", -1,
1345                                                              NULL, 0);
1346         if (IS_ERR(sonypi_device.pdev)) {
1347                 ret = PTR_ERR(sonypi_device.pdev);
1348                 goto out_platformdev;
1349         }
1350
1351         sonypi_enable(0);
1352
1353         printk(KERN_INFO "sonypi: Sony Programmable I/O Controller Driver"
1354                "v%s.\n", SONYPI_DRIVER_VERSION);
1355         printk(KERN_INFO "sonypi: detected type%d model, "
1356                "verbose = %d, fnkeyinit = %s, camera = %s, "
1357                "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
1358                sonypi_device.model,
1359                verbose,
1360                fnkeyinit ? "on" : "off",
1361                camera ? "on" : "off",
1362                compat ? "on" : "off",
1363                mask,
1364                useinput ? "on" : "off",
1365                SONYPI_ACPI_ACTIVE ? "on" : "off");
1366         printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
1367                sonypi_device.irq,
1368                sonypi_device.ioport1, sonypi_device.ioport2);
1369
1370         if (minor == -1)
1371                 printk(KERN_INFO "sonypi: device allocated minor is %d\n",
1372                        sonypi_misc_device.minor);
1373
1374         return 0;
1375
1376 out_platformdev:
1377         kfifo_free(sonypi_device.input_fifo);
1378 out_infifo:
1379         input_unregister_device(&sonypi_device.input_key_dev);
1380         input_unregister_device(&sonypi_device.input_jog_dev);
1381         free_irq(sonypi_device.irq, sonypi_irq);
1382 out_reqirq:
1383         release_region(sonypi_device.ioport1, sonypi_device.region_size);
1384 out_reqreg:
1385         misc_deregister(&sonypi_misc_device);
1386 out_miscreg:
1387         if (pcidev)
1388                 pci_disable_device(pcidev);
1389 out_pcienable:
1390         kfifo_free(sonypi_device.fifo);
1391 out_fifo:
1392         pci_dev_put(sonypi_device.dev);
1393         return ret;
1394 }
1395
1396 static void __devexit sonypi_remove(void)
1397 {
1398         sonypi_disable();
1399
1400         synchronize_sched();  /* Allow sonypi interrupt to complete. */
1401         flush_scheduled_work();
1402
1403         platform_device_unregister(sonypi_device.pdev);
1404
1405         if (useinput) {
1406                 input_unregister_device(&sonypi_device.input_key_dev);
1407                 input_unregister_device(&sonypi_device.input_jog_dev);
1408                 kfifo_free(sonypi_device.input_fifo);
1409         }
1410
1411         free_irq(sonypi_device.irq, sonypi_irq);
1412         release_region(sonypi_device.ioport1, sonypi_device.region_size);
1413         misc_deregister(&sonypi_misc_device);
1414         if (sonypi_device.dev)
1415                 pci_disable_device(sonypi_device.dev);
1416         kfifo_free(sonypi_device.fifo);
1417         pci_dev_put(sonypi_device.dev);
1418         printk(KERN_INFO "sonypi: removed.\n");
1419 }
1420
1421 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
1422         {
1423                 .ident = "Sony Vaio",
1424                 .matches = {
1425                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1426                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1427                 },
1428         },
1429         {
1430                 .ident = "Sony Vaio",
1431                 .matches = {
1432                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1433                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1434                 },
1435         },
1436         { }
1437 };
1438
1439 static int __init sonypi_init(void)
1440 {
1441         int ret;
1442
1443         if (!dmi_check_system(sonypi_dmi_table))
1444                 return -ENODEV;
1445
1446         ret = driver_register(&sonypi_driver);
1447         if (ret)
1448                 return ret;
1449
1450         ret = sonypi_probe();
1451         if (ret)
1452                 driver_unregister(&sonypi_driver);
1453
1454         return ret;
1455 }
1456
1457 static void __exit sonypi_exit(void)
1458 {
1459         driver_unregister(&sonypi_driver);
1460         sonypi_remove();
1461 }
1462
1463 module_init(sonypi_init);
1464 module_exit(sonypi_exit);