[PATCH] powerpc: dont allow old RTC to be selected
[linux-2.6] / drivers / char / sonypi.c
1 /*
2  * Sony Programmable I/O Control Device driver for VAIO
3  *
4  * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
5  *
6  * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
7  *
8  * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
9  *
10  * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
11  *
12  * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
13  *
14  * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
15  *
16  * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
17  *
18  * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33  *
34  */
35
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/input.h>
39 #include <linux/pci.h>
40 #include <linux/sched.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/miscdevice.h>
44 #include <linux/poll.h>
45 #include <linux/delay.h>
46 #include <linux/wait.h>
47 #include <linux/acpi.h>
48 #include <linux/dmi.h>
49 #include <linux/err.h>
50 #include <linux/kfifo.h>
51 #include <linux/platform_device.h>
52
53 #include <asm/uaccess.h>
54 #include <asm/io.h>
55 #include <asm/system.h>
56
57 #include <linux/sonypi.h>
58
59 #define SONYPI_DRIVER_VERSION    "1.26"
60
61 MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
62 MODULE_DESCRIPTION("Sony Programmable I/O Control Device driver");
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION(SONYPI_DRIVER_VERSION);
65
66 static int minor = -1;
67 module_param(minor, int, 0);
68 MODULE_PARM_DESC(minor,
69                  "minor number of the misc device, default is -1 (automatic)");
70
71 static int verbose;             /* = 0 */
72 module_param(verbose, int, 0644);
73 MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
74
75 static int fnkeyinit;           /* = 0 */
76 module_param(fnkeyinit, int, 0444);
77 MODULE_PARM_DESC(fnkeyinit,
78                  "set this if your Fn keys do not generate any event");
79
80 static int camera;              /* = 0 */
81 module_param(camera, int, 0444);
82 MODULE_PARM_DESC(camera,
83                  "set this if you have a MotionEye camera (PictureBook series)");
84
85 static int compat;              /* = 0 */
86 module_param(compat, int, 0444);
87 MODULE_PARM_DESC(compat,
88                  "set this if you want to enable backward compatibility mode");
89
90 static unsigned long mask = 0xffffffff;
91 module_param(mask, ulong, 0644);
92 MODULE_PARM_DESC(mask,
93                  "set this to the mask of event you want to enable (see doc)");
94
95 static int useinput = 1;
96 module_param(useinput, int, 0444);
97 MODULE_PARM_DESC(useinput,
98                  "set this if you would like sonypi to feed events to the input subsystem");
99
100 #define SONYPI_DEVICE_MODEL_TYPE1       1
101 #define SONYPI_DEVICE_MODEL_TYPE2       2
102 #define SONYPI_DEVICE_MODEL_TYPE3       3
103
104 /* type1 models use those */
105 #define SONYPI_IRQ_PORT                 0x8034
106 #define SONYPI_IRQ_SHIFT                22
107 #define SONYPI_TYPE1_BASE               0x50
108 #define SONYPI_G10A                     (SONYPI_TYPE1_BASE+0x14)
109 #define SONYPI_TYPE1_REGION_SIZE        0x08
110 #define SONYPI_TYPE1_EVTYPE_OFFSET      0x04
111
112 /* type2 series specifics */
113 #define SONYPI_SIRQ                     0x9b
114 #define SONYPI_SLOB                     0x9c
115 #define SONYPI_SHIB                     0x9d
116 #define SONYPI_TYPE2_REGION_SIZE        0x20
117 #define SONYPI_TYPE2_EVTYPE_OFFSET      0x12
118
119 /* type3 series specifics */
120 #define SONYPI_TYPE3_BASE               0x40
121 #define SONYPI_TYPE3_GID2               (SONYPI_TYPE3_BASE+0x48) /* 16 bits */
122 #define SONYPI_TYPE3_MISC               (SONYPI_TYPE3_BASE+0x6d) /* 8 bits  */
123 #define SONYPI_TYPE3_REGION_SIZE        0x20
124 #define SONYPI_TYPE3_EVTYPE_OFFSET      0x12
125
126 /* battery / brightness addresses */
127 #define SONYPI_BAT_FLAGS        0x81
128 #define SONYPI_LCD_LIGHT        0x96
129 #define SONYPI_BAT1_PCTRM       0xa0
130 #define SONYPI_BAT1_LEFT        0xa2
131 #define SONYPI_BAT1_MAXRT       0xa4
132 #define SONYPI_BAT2_PCTRM       0xa8
133 #define SONYPI_BAT2_LEFT        0xaa
134 #define SONYPI_BAT2_MAXRT       0xac
135 #define SONYPI_BAT1_MAXTK       0xb0
136 #define SONYPI_BAT1_FULL        0xb2
137 #define SONYPI_BAT2_MAXTK       0xb8
138 #define SONYPI_BAT2_FULL        0xba
139
140 /* FAN0 information (reverse engineered from ACPI tables) */
141 #define SONYPI_FAN0_STATUS      0x93
142 #define SONYPI_TEMP_STATUS      0xC1
143
144 /* ioports used for brightness and type2 events */
145 #define SONYPI_DATA_IOPORT      0x62
146 #define SONYPI_CST_IOPORT       0x66
147
148 /* The set of possible ioports */
149 struct sonypi_ioport_list {
150         u16     port1;
151         u16     port2;
152 };
153
154 static struct sonypi_ioport_list sonypi_type1_ioport_list[] = {
155         { 0x10c0, 0x10c4 },     /* looks like the default on C1Vx */
156         { 0x1080, 0x1084 },
157         { 0x1090, 0x1094 },
158         { 0x10a0, 0x10a4 },
159         { 0x10b0, 0x10b4 },
160         { 0x0, 0x0 }
161 };
162
163 static struct sonypi_ioport_list sonypi_type2_ioport_list[] = {
164         { 0x1080, 0x1084 },
165         { 0x10a0, 0x10a4 },
166         { 0x10c0, 0x10c4 },
167         { 0x10e0, 0x10e4 },
168         { 0x0, 0x0 }
169 };
170
171 /* same as in type 2 models */
172 static struct sonypi_ioport_list *sonypi_type3_ioport_list =
173         sonypi_type2_ioport_list;
174
175 /* The set of possible interrupts */
176 struct sonypi_irq_list {
177         u16     irq;
178         u16     bits;
179 };
180
181 static struct sonypi_irq_list sonypi_type1_irq_list[] = {
182         { 11, 0x2 },    /* IRQ 11, GO22=0,GO23=1 in AML */
183         { 10, 0x1 },    /* IRQ 10, GO22=1,GO23=0 in AML */
184         {  5, 0x0 },    /* IRQ  5, GO22=0,GO23=0 in AML */
185         {  0, 0x3 }     /* no IRQ, GO22=1,GO23=1 in AML */
186 };
187
188 static struct sonypi_irq_list sonypi_type2_irq_list[] = {
189         { 11, 0x80 },   /* IRQ 11, 0x80 in SIRQ in AML */
190         { 10, 0x40 },   /* IRQ 10, 0x40 in SIRQ in AML */
191         {  9, 0x20 },   /* IRQ  9, 0x20 in SIRQ in AML */
192         {  6, 0x10 },   /* IRQ  6, 0x10 in SIRQ in AML */
193         {  0, 0x00 }    /* no IRQ, 0x00 in SIRQ in AML */
194 };
195
196 /* same as in type2 models */
197 static struct sonypi_irq_list *sonypi_type3_irq_list = sonypi_type2_irq_list;
198
199 #define SONYPI_CAMERA_BRIGHTNESS                0
200 #define SONYPI_CAMERA_CONTRAST                  1
201 #define SONYPI_CAMERA_HUE                       2
202 #define SONYPI_CAMERA_COLOR                     3
203 #define SONYPI_CAMERA_SHARPNESS                 4
204
205 #define SONYPI_CAMERA_PICTURE                   5
206 #define SONYPI_CAMERA_EXPOSURE_MASK             0xC
207 #define SONYPI_CAMERA_WHITE_BALANCE_MASK        0x3
208 #define SONYPI_CAMERA_PICTURE_MODE_MASK         0x30
209 #define SONYPI_CAMERA_MUTE_MASK                 0x40
210
211 /* the rest don't need a loop until not 0xff */
212 #define SONYPI_CAMERA_AGC                       6
213 #define SONYPI_CAMERA_AGC_MASK                  0x30
214 #define SONYPI_CAMERA_SHUTTER_MASK              0x7
215
216 #define SONYPI_CAMERA_SHUTDOWN_REQUEST          7
217 #define SONYPI_CAMERA_CONTROL                   0x10
218
219 #define SONYPI_CAMERA_STATUS                    7
220 #define SONYPI_CAMERA_STATUS_READY              0x2
221 #define SONYPI_CAMERA_STATUS_POSITION           0x4
222
223 #define SONYPI_DIRECTION_BACKWARDS              0x4
224
225 #define SONYPI_CAMERA_REVISION                  8
226 #define SONYPI_CAMERA_ROMVERSION                9
227
228 /* Event masks */
229 #define SONYPI_JOGGER_MASK                      0x00000001
230 #define SONYPI_CAPTURE_MASK                     0x00000002
231 #define SONYPI_FNKEY_MASK                       0x00000004
232 #define SONYPI_BLUETOOTH_MASK                   0x00000008
233 #define SONYPI_PKEY_MASK                        0x00000010
234 #define SONYPI_BACK_MASK                        0x00000020
235 #define SONYPI_HELP_MASK                        0x00000040
236 #define SONYPI_LID_MASK                         0x00000080
237 #define SONYPI_ZOOM_MASK                        0x00000100
238 #define SONYPI_THUMBPHRASE_MASK                 0x00000200
239 #define SONYPI_MEYE_MASK                        0x00000400
240 #define SONYPI_MEMORYSTICK_MASK                 0x00000800
241 #define SONYPI_BATTERY_MASK                     0x00001000
242 #define SONYPI_WIRELESS_MASK                    0x00002000
243
244 struct sonypi_event {
245         u8      data;
246         u8      event;
247 };
248
249 /* The set of possible button release events */
250 static struct sonypi_event sonypi_releaseev[] = {
251         { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
252         { 0, 0 }
253 };
254
255 /* The set of possible jogger events  */
256 static struct sonypi_event sonypi_joggerev[] = {
257         { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
258         { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
259         { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
260         { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
261         { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
262         { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
263         { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
264         { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
265         { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
266         { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
267         { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
268         { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
269         { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
270         { 0, 0 }
271 };
272
273 /* The set of possible capture button events */
274 static struct sonypi_event sonypi_captureev[] = {
275         { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
276         { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
277         { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
278         { 0, 0 }
279 };
280
281 /* The set of possible fnkeys events */
282 static struct sonypi_event sonypi_fnkeyev[] = {
283         { 0x10, SONYPI_EVENT_FNKEY_ESC },
284         { 0x11, SONYPI_EVENT_FNKEY_F1 },
285         { 0x12, SONYPI_EVENT_FNKEY_F2 },
286         { 0x13, SONYPI_EVENT_FNKEY_F3 },
287         { 0x14, SONYPI_EVENT_FNKEY_F4 },
288         { 0x15, SONYPI_EVENT_FNKEY_F5 },
289         { 0x16, SONYPI_EVENT_FNKEY_F6 },
290         { 0x17, SONYPI_EVENT_FNKEY_F7 },
291         { 0x18, SONYPI_EVENT_FNKEY_F8 },
292         { 0x19, SONYPI_EVENT_FNKEY_F9 },
293         { 0x1a, SONYPI_EVENT_FNKEY_F10 },
294         { 0x1b, SONYPI_EVENT_FNKEY_F11 },
295         { 0x1c, SONYPI_EVENT_FNKEY_F12 },
296         { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
297         { 0x21, SONYPI_EVENT_FNKEY_1 },
298         { 0x22, SONYPI_EVENT_FNKEY_2 },
299         { 0x31, SONYPI_EVENT_FNKEY_D },
300         { 0x32, SONYPI_EVENT_FNKEY_E },
301         { 0x33, SONYPI_EVENT_FNKEY_F },
302         { 0x34, SONYPI_EVENT_FNKEY_S },
303         { 0x35, SONYPI_EVENT_FNKEY_B },
304         { 0x36, SONYPI_EVENT_FNKEY_ONLY },
305         { 0, 0 }
306 };
307
308 /* The set of possible program key events */
309 static struct sonypi_event sonypi_pkeyev[] = {
310         { 0x01, SONYPI_EVENT_PKEY_P1 },
311         { 0x02, SONYPI_EVENT_PKEY_P2 },
312         { 0x04, SONYPI_EVENT_PKEY_P3 },
313         { 0x5c, SONYPI_EVENT_PKEY_P1 },
314         { 0, 0 }
315 };
316
317 /* The set of possible bluetooth events */
318 static struct sonypi_event sonypi_blueev[] = {
319         { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
320         { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
321         { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
322         { 0, 0 }
323 };
324
325 /* The set of possible wireless events */
326 static struct sonypi_event sonypi_wlessev[] = {
327         { 0x59, SONYPI_EVENT_WIRELESS_ON },
328         { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
329         { 0, 0 }
330 };
331
332 /* The set of possible back button events */
333 static struct sonypi_event sonypi_backev[] = {
334         { 0x20, SONYPI_EVENT_BACK_PRESSED },
335         { 0, 0 }
336 };
337
338 /* The set of possible help button events */
339 static struct sonypi_event sonypi_helpev[] = {
340         { 0x3b, SONYPI_EVENT_HELP_PRESSED },
341         { 0, 0 }
342 };
343
344
345 /* The set of possible lid events */
346 static struct sonypi_event sonypi_lidev[] = {
347         { 0x51, SONYPI_EVENT_LID_CLOSED },
348         { 0x50, SONYPI_EVENT_LID_OPENED },
349         { 0, 0 }
350 };
351
352 /* The set of possible zoom events */
353 static struct sonypi_event sonypi_zoomev[] = {
354         { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
355         { 0, 0 }
356 };
357
358 /* The set of possible thumbphrase events */
359 static struct sonypi_event sonypi_thumbphraseev[] = {
360         { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
361         { 0, 0 }
362 };
363
364 /* The set of possible motioneye camera events */
365 static struct sonypi_event sonypi_meyeev[] = {
366         { 0x00, SONYPI_EVENT_MEYE_FACE },
367         { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
368         { 0, 0 }
369 };
370
371 /* The set of possible memorystick events */
372 static struct sonypi_event sonypi_memorystickev[] = {
373         { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
374         { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
375         { 0, 0 }
376 };
377
378 /* The set of possible battery events */
379 static struct sonypi_event sonypi_batteryev[] = {
380         { 0x20, SONYPI_EVENT_BATTERY_INSERT },
381         { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
382         { 0, 0 }
383 };
384
385 static struct sonypi_eventtypes {
386         int                     model;
387         u8                      data;
388         unsigned long           mask;
389         struct sonypi_event *   events;
390 } sonypi_eventtypes[] = {
391         { SONYPI_DEVICE_MODEL_TYPE1, 0, 0xffffffff, sonypi_releaseev },
392         { SONYPI_DEVICE_MODEL_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
393         { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
394         { SONYPI_DEVICE_MODEL_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
395         { SONYPI_DEVICE_MODEL_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
396         { SONYPI_DEVICE_MODEL_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
397         { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
398         { SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
399         { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
400         { SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
401
402         { SONYPI_DEVICE_MODEL_TYPE2, 0, 0xffffffff, sonypi_releaseev },
403         { SONYPI_DEVICE_MODEL_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
404         { SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
405         { SONYPI_DEVICE_MODEL_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
406         { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
407         { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
408         { SONYPI_DEVICE_MODEL_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
409         { SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
410         { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
411         { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
412         { SONYPI_DEVICE_MODEL_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
413         { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
414         { SONYPI_DEVICE_MODEL_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
415         { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
416
417         { SONYPI_DEVICE_MODEL_TYPE3, 0, 0xffffffff, sonypi_releaseev },
418         { SONYPI_DEVICE_MODEL_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
419         { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
420         { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
421         { SONYPI_DEVICE_MODEL_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
422         { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
423         { 0 }
424 };
425
426 #define SONYPI_BUF_SIZE 128
427
428 /* Correspondance table between sonypi events and input layer events */
429 static struct {
430         int sonypiev;
431         int inputev;
432 } sonypi_inputkeys[] = {
433         { SONYPI_EVENT_CAPTURE_PRESSED,         KEY_CAMERA },
434         { SONYPI_EVENT_FNKEY_ONLY,              KEY_FN },
435         { SONYPI_EVENT_FNKEY_ESC,               KEY_FN_ESC },
436         { SONYPI_EVENT_FNKEY_F1,                KEY_FN_F1 },
437         { SONYPI_EVENT_FNKEY_F2,                KEY_FN_F2 },
438         { SONYPI_EVENT_FNKEY_F3,                KEY_FN_F3 },
439         { SONYPI_EVENT_FNKEY_F4,                KEY_FN_F4 },
440         { SONYPI_EVENT_FNKEY_F5,                KEY_FN_F5 },
441         { SONYPI_EVENT_FNKEY_F6,                KEY_FN_F6 },
442         { SONYPI_EVENT_FNKEY_F7,                KEY_FN_F7 },
443         { SONYPI_EVENT_FNKEY_F8,                KEY_FN_F8 },
444         { SONYPI_EVENT_FNKEY_F9,                KEY_FN_F9 },
445         { SONYPI_EVENT_FNKEY_F10,               KEY_FN_F10 },
446         { SONYPI_EVENT_FNKEY_F11,               KEY_FN_F11 },
447         { SONYPI_EVENT_FNKEY_F12,               KEY_FN_F12 },
448         { SONYPI_EVENT_FNKEY_1,                 KEY_FN_1 },
449         { SONYPI_EVENT_FNKEY_2,                 KEY_FN_2 },
450         { SONYPI_EVENT_FNKEY_D,                 KEY_FN_D },
451         { SONYPI_EVENT_FNKEY_E,                 KEY_FN_E },
452         { SONYPI_EVENT_FNKEY_F,                 KEY_FN_F },
453         { SONYPI_EVENT_FNKEY_S,                 KEY_FN_S },
454         { SONYPI_EVENT_FNKEY_B,                 KEY_FN_B },
455         { SONYPI_EVENT_BLUETOOTH_PRESSED,       KEY_BLUE },
456         { SONYPI_EVENT_BLUETOOTH_ON,            KEY_BLUE },
457         { SONYPI_EVENT_PKEY_P1,                 KEY_PROG1 },
458         { SONYPI_EVENT_PKEY_P2,                 KEY_PROG2 },
459         { SONYPI_EVENT_PKEY_P3,                 KEY_PROG3 },
460         { SONYPI_EVENT_BACK_PRESSED,            KEY_BACK },
461         { SONYPI_EVENT_HELP_PRESSED,            KEY_HELP },
462         { SONYPI_EVENT_ZOOM_PRESSED,            KEY_ZOOM },
463         { SONYPI_EVENT_THUMBPHRASE_PRESSED,     BTN_THUMB },
464         { 0, 0 },
465 };
466
467 struct sonypi_keypress {
468         struct input_dev *dev;
469         int key;
470 };
471
472 static struct sonypi_device {
473         struct pci_dev *dev;
474         u16 irq;
475         u16 bits;
476         u16 ioport1;
477         u16 ioport2;
478         u16 region_size;
479         u16 evtype_offset;
480         int camera_power;
481         int bluetooth_power;
482         struct semaphore lock;
483         struct kfifo *fifo;
484         spinlock_t fifo_lock;
485         wait_queue_head_t fifo_proc_list;
486         struct fasync_struct *fifo_async;
487         int open_count;
488         int model;
489         struct input_dev *input_jog_dev;
490         struct input_dev *input_key_dev;
491         struct work_struct input_work;
492         struct kfifo *input_fifo;
493         spinlock_t input_fifo_lock;
494 } sonypi_device;
495
496 #define ITERATIONS_LONG         10000
497 #define ITERATIONS_SHORT        10
498
499 #define wait_on_command(quiet, command, iterations) { \
500         unsigned int n = iterations; \
501         while (--n && (command)) \
502                 udelay(1); \
503         if (!n && (verbose || !quiet)) \
504                 printk(KERN_WARNING "sonypi command failed at %s : %s (line %d)\n", __FILE__, __FUNCTION__, __LINE__); \
505 }
506
507 #ifdef CONFIG_ACPI
508 #define SONYPI_ACPI_ACTIVE (!acpi_disabled)
509 #else
510 #define SONYPI_ACPI_ACTIVE 0
511 #endif                          /* CONFIG_ACPI */
512
513 #ifdef CONFIG_ACPI
514 static struct acpi_device *sonypi_acpi_device;
515 static int acpi_enabled;
516 #endif
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 #ifdef CONFIG_ACPI
872         if (acpi_enabled)
873                 acpi_bus_generate_event(sonypi_acpi_device, 1, event);
874 #endif
875
876         kfifo_put(sonypi_device.fifo, (unsigned char *)&event, sizeof(event));
877         kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
878         wake_up_interruptible(&sonypi_device.fifo_proc_list);
879
880         return IRQ_HANDLED;
881 }
882
883 /* External camera command (exported to the motion eye v4l driver) */
884 int sonypi_camera_command(int command, u8 value)
885 {
886         if (!camera)
887                 return -EIO;
888
889         down(&sonypi_device.lock);
890
891         switch (command) {
892         case SONYPI_COMMAND_SETCAMERA:
893                 if (value)
894                         sonypi_camera_on();
895                 else
896                         sonypi_camera_off();
897                 break;
898         case SONYPI_COMMAND_SETCAMERABRIGHTNESS:
899                 sonypi_set(SONYPI_CAMERA_BRIGHTNESS, value);
900                 break;
901         case SONYPI_COMMAND_SETCAMERACONTRAST:
902                 sonypi_set(SONYPI_CAMERA_CONTRAST, value);
903                 break;
904         case SONYPI_COMMAND_SETCAMERAHUE:
905                 sonypi_set(SONYPI_CAMERA_HUE, value);
906                 break;
907         case SONYPI_COMMAND_SETCAMERACOLOR:
908                 sonypi_set(SONYPI_CAMERA_COLOR, value);
909                 break;
910         case SONYPI_COMMAND_SETCAMERASHARPNESS:
911                 sonypi_set(SONYPI_CAMERA_SHARPNESS, value);
912                 break;
913         case SONYPI_COMMAND_SETCAMERAPICTURE:
914                 sonypi_set(SONYPI_CAMERA_PICTURE, value);
915                 break;
916         case SONYPI_COMMAND_SETCAMERAAGC:
917                 sonypi_set(SONYPI_CAMERA_AGC, value);
918                 break;
919         default:
920                 printk(KERN_ERR "sonypi: sonypi_camera_command invalid: %d\n",
921                        command);
922                 break;
923         }
924         up(&sonypi_device.lock);
925         return 0;
926 }
927
928 EXPORT_SYMBOL(sonypi_camera_command);
929
930 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
931 {
932         int retval;
933
934         retval = fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
935         if (retval < 0)
936                 return retval;
937         return 0;
938 }
939
940 static int sonypi_misc_release(struct inode *inode, struct file *file)
941 {
942         sonypi_misc_fasync(-1, file, 0);
943         down(&sonypi_device.lock);
944         sonypi_device.open_count--;
945         up(&sonypi_device.lock);
946         return 0;
947 }
948
949 static int sonypi_misc_open(struct inode *inode, struct file *file)
950 {
951         down(&sonypi_device.lock);
952         /* Flush input queue on first open */
953         if (!sonypi_device.open_count)
954                 kfifo_reset(sonypi_device.fifo);
955         sonypi_device.open_count++;
956         up(&sonypi_device.lock);
957         return 0;
958 }
959
960 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
961                                 size_t count, loff_t *pos)
962 {
963         ssize_t ret;
964         unsigned char c;
965
966         if ((kfifo_len(sonypi_device.fifo) == 0) &&
967             (file->f_flags & O_NONBLOCK))
968                 return -EAGAIN;
969
970         ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
971                                        kfifo_len(sonypi_device.fifo) != 0);
972         if (ret)
973                 return ret;
974
975         while (ret < count &&
976                (kfifo_get(sonypi_device.fifo, &c, sizeof(c)) == sizeof(c))) {
977                 if (put_user(c, buf++))
978                         return -EFAULT;
979                 ret++;
980         }
981
982         if (ret > 0) {
983                 struct inode *inode = file->f_dentry->d_inode;
984                 inode->i_atime = current_fs_time(inode->i_sb);
985         }
986
987         return ret;
988 }
989
990 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
991 {
992         poll_wait(file, &sonypi_device.fifo_proc_list, wait);
993         if (kfifo_len(sonypi_device.fifo))
994                 return POLLIN | POLLRDNORM;
995         return 0;
996 }
997
998 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
999                              unsigned int cmd, unsigned long arg)
1000 {
1001         int ret = 0;
1002         void __user *argp = (void __user *)arg;
1003         u8 val8;
1004         u16 val16;
1005
1006         down(&sonypi_device.lock);
1007         switch (cmd) {
1008         case SONYPI_IOCGBRT:
1009                 if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
1010                         ret = -EIO;
1011                         break;
1012                 }
1013                 if (copy_to_user(argp, &val8, sizeof(val8)))
1014                         ret = -EFAULT;
1015                 break;
1016         case SONYPI_IOCSBRT:
1017                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1018                         ret = -EFAULT;
1019                         break;
1020                 }
1021                 if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
1022                         ret = -EIO;
1023                 break;
1024         case SONYPI_IOCGBAT1CAP:
1025                 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1026                         ret = -EIO;
1027                         break;
1028                 }
1029                 if (copy_to_user(argp, &val16, sizeof(val16)))
1030                         ret = -EFAULT;
1031                 break;
1032         case SONYPI_IOCGBAT1REM:
1033                 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1034                         ret = -EIO;
1035                         break;
1036                 }
1037                 if (copy_to_user(argp, &val16, sizeof(val16)))
1038                         ret = -EFAULT;
1039                 break;
1040         case SONYPI_IOCGBAT2CAP:
1041                 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1042                         ret = -EIO;
1043                         break;
1044                 }
1045                 if (copy_to_user(argp, &val16, sizeof(val16)))
1046                         ret = -EFAULT;
1047                 break;
1048         case SONYPI_IOCGBAT2REM:
1049                 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1050                         ret = -EIO;
1051                         break;
1052                 }
1053                 if (copy_to_user(argp, &val16, sizeof(val16)))
1054                         ret = -EFAULT;
1055                 break;
1056         case SONYPI_IOCGBATFLAGS:
1057                 if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
1058                         ret = -EIO;
1059                         break;
1060                 }
1061                 val8 &= 0x07;
1062                 if (copy_to_user(argp, &val8, sizeof(val8)))
1063                         ret = -EFAULT;
1064                 break;
1065         case SONYPI_IOCGBLUE:
1066                 val8 = sonypi_device.bluetooth_power;
1067                 if (copy_to_user(argp, &val8, sizeof(val8)))
1068                         ret = -EFAULT;
1069                 break;
1070         case SONYPI_IOCSBLUE:
1071                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1072                         ret = -EFAULT;
1073                         break;
1074                 }
1075                 sonypi_setbluetoothpower(val8);
1076                 break;
1077         /* FAN Controls */
1078         case SONYPI_IOCGFAN:
1079                 if (sonypi_ec_read(SONYPI_FAN0_STATUS, &val8)) {
1080                         ret = -EIO;
1081                         break;
1082                 }
1083                 if (copy_to_user(argp, &val8, sizeof(val8)))
1084                         ret = -EFAULT;
1085                 break;
1086         case SONYPI_IOCSFAN:
1087                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1088                         ret = -EFAULT;
1089                         break;
1090                 }
1091                 if (sonypi_ec_write(SONYPI_FAN0_STATUS, val8))
1092                         ret = -EIO;
1093                 break;
1094         /* GET Temperature (useful under APM) */
1095         case SONYPI_IOCGTEMP:
1096                 if (sonypi_ec_read(SONYPI_TEMP_STATUS, &val8)) {
1097                         ret = -EIO;
1098                         break;
1099                 }
1100                 if (copy_to_user(argp, &val8, sizeof(val8)))
1101                         ret = -EFAULT;
1102                 break;
1103         default:
1104                 ret = -EINVAL;
1105         }
1106         up(&sonypi_device.lock);
1107         return ret;
1108 }
1109
1110 static struct file_operations sonypi_misc_fops = {
1111         .owner          = THIS_MODULE,
1112         .read           = sonypi_misc_read,
1113         .poll           = sonypi_misc_poll,
1114         .open           = sonypi_misc_open,
1115         .release        = sonypi_misc_release,
1116         .fasync         = sonypi_misc_fasync,
1117         .ioctl          = sonypi_misc_ioctl,
1118 };
1119
1120 static struct miscdevice sonypi_misc_device = {
1121         .minor          = MISC_DYNAMIC_MINOR,
1122         .name           = "sonypi",
1123         .fops           = &sonypi_misc_fops,
1124 };
1125
1126 static void sonypi_enable(unsigned int camera_on)
1127 {
1128         switch (sonypi_device.model) {
1129         case SONYPI_DEVICE_MODEL_TYPE1:
1130                 sonypi_type1_srs();
1131                 break;
1132         case SONYPI_DEVICE_MODEL_TYPE2:
1133                 sonypi_type2_srs();
1134                 break;
1135         case SONYPI_DEVICE_MODEL_TYPE3:
1136                 sonypi_type3_srs();
1137                 break;
1138         }
1139
1140         sonypi_call1(0x82);
1141         sonypi_call2(0x81, 0xff);
1142         sonypi_call1(compat ? 0x92 : 0x82);
1143
1144         /* Enable ACPI mode to get Fn key events */
1145         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1146                 outb(0xf0, 0xb2);
1147
1148         if (camera && camera_on)
1149                 sonypi_camera_on();
1150 }
1151
1152 static int sonypi_disable(void)
1153 {
1154         sonypi_call2(0x81, 0);  /* make sure we don't get any more events */
1155         if (camera)
1156                 sonypi_camera_off();
1157
1158         /* disable ACPI mode */
1159         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1160                 outb(0xf1, 0xb2);
1161
1162         switch (sonypi_device.model) {
1163         case SONYPI_DEVICE_MODEL_TYPE1:
1164                 sonypi_type1_dis();
1165                 break;
1166         case SONYPI_DEVICE_MODEL_TYPE2:
1167                 sonypi_type2_dis();
1168                 break;
1169         case SONYPI_DEVICE_MODEL_TYPE3:
1170                 sonypi_type3_dis();
1171                 break;
1172         }
1173
1174         return 0;
1175 }
1176
1177 #ifdef CONFIG_ACPI
1178 static int sonypi_acpi_add(struct acpi_device *device)
1179 {
1180         sonypi_acpi_device = device;
1181         strcpy(acpi_device_name(device), "Sony laptop hotkeys");
1182         strcpy(acpi_device_class(device), "sony/hotkey");
1183         return 0;
1184 }
1185
1186 static int sonypi_acpi_remove(struct acpi_device *device, int type)
1187 {
1188         sonypi_acpi_device = NULL;
1189         return 0;
1190 }
1191
1192 static struct acpi_driver sonypi_acpi_driver = {
1193         .name           = "sonypi",
1194         .class          = "hkey",
1195         .ids            = "SNY6001",
1196         .ops            = {
1197                            .add = sonypi_acpi_add,
1198                            .remove = sonypi_acpi_remove,
1199         },
1200 };
1201 #endif
1202
1203 static int __devinit sonypi_create_input_devices(void)
1204 {
1205         struct input_dev *jog_dev;
1206         struct input_dev *key_dev;
1207         int i;
1208         int error;
1209
1210         sonypi_device.input_jog_dev = jog_dev = input_allocate_device();
1211         if (!jog_dev)
1212                 return -ENOMEM;
1213
1214         jog_dev->name = "Sony Vaio Jogdial";
1215         jog_dev->id.bustype = BUS_ISA;
1216         jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
1217
1218         jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1219         jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
1220         jog_dev->relbit[0] = BIT(REL_WHEEL);
1221
1222         sonypi_device.input_key_dev = key_dev = input_allocate_device();
1223         if (!key_dev) {
1224                 error = -ENOMEM;
1225                 goto err_free_jogdev;
1226         }
1227
1228         key_dev->name = "Sony Vaio Keys";
1229         key_dev->id.bustype = BUS_ISA;
1230         key_dev->id.vendor = PCI_VENDOR_ID_SONY;
1231
1232         /* Initialize the Input Drivers: special keys */
1233         key_dev->evbit[0] = BIT(EV_KEY);
1234         for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
1235                 if (sonypi_inputkeys[i].inputev)
1236                         set_bit(sonypi_inputkeys[i].inputev, key_dev->keybit);
1237
1238         error = input_register_device(jog_dev);
1239         if (error)
1240                 goto err_free_keydev;
1241
1242         error = input_register_device(key_dev);
1243         if (error)
1244                 goto err_unregister_jogdev;
1245
1246         return 0;
1247
1248  err_unregister_jogdev:
1249         input_unregister_device(jog_dev);
1250         /* Set to NULL so we don't free it again below */
1251         jog_dev = NULL;
1252  err_free_keydev:
1253         input_free_device(key_dev);
1254         sonypi_device.input_key_dev = NULL;
1255  err_free_jogdev:
1256         input_free_device(jog_dev);
1257         sonypi_device.input_jog_dev = NULL;
1258
1259         return error;
1260 }
1261
1262 static int __devinit sonypi_setup_ioports(struct sonypi_device *dev,
1263                                 const struct sonypi_ioport_list *ioport_list)
1264 {
1265         while (ioport_list->port1) {
1266
1267                 if (request_region(ioport_list->port1,
1268                                    sonypi_device.region_size,
1269                                    "Sony Programable I/O Device")) {
1270                         dev->ioport1 = ioport_list->port1;
1271                         dev->ioport2 = ioport_list->port2;
1272                         return 0;
1273                 }
1274                 ioport_list++;
1275         }
1276
1277         return -EBUSY;
1278 }
1279
1280 static int __devinit sonypi_setup_irq(struct sonypi_device *dev,
1281                                       const struct sonypi_irq_list *irq_list)
1282 {
1283         while (irq_list->irq) {
1284
1285                 if (!request_irq(irq_list->irq, sonypi_irq,
1286                                  SA_SHIRQ, "sonypi", sonypi_irq)) {
1287                         dev->irq = irq_list->irq;
1288                         dev->bits = irq_list->bits;
1289                         return 0;
1290                 }
1291                 irq_list++;
1292         }
1293
1294         return -EBUSY;
1295 }
1296
1297 static void __devinit sonypi_display_info(void)
1298 {
1299         printk(KERN_INFO "sonypi: detected type%d model, "
1300                "verbose = %d, fnkeyinit = %s, camera = %s, "
1301                "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
1302                sonypi_device.model,
1303                verbose,
1304                fnkeyinit ? "on" : "off",
1305                camera ? "on" : "off",
1306                compat ? "on" : "off",
1307                mask,
1308                useinput ? "on" : "off",
1309                SONYPI_ACPI_ACTIVE ? "on" : "off");
1310         printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
1311                sonypi_device.irq,
1312                sonypi_device.ioport1, sonypi_device.ioport2);
1313
1314         if (minor == -1)
1315                 printk(KERN_INFO "sonypi: device allocated minor is %d\n",
1316                        sonypi_misc_device.minor);
1317 }
1318
1319 static int __devinit sonypi_probe(struct platform_device *dev)
1320 {
1321         const struct sonypi_ioport_list *ioport_list;
1322         const struct sonypi_irq_list *irq_list;
1323         struct pci_dev *pcidev;
1324         int error;
1325
1326         spin_lock_init(&sonypi_device.fifo_lock);
1327         sonypi_device.fifo = kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
1328                                          &sonypi_device.fifo_lock);
1329         if (IS_ERR(sonypi_device.fifo)) {
1330                 printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1331                 return PTR_ERR(sonypi_device.fifo);
1332         }
1333
1334         init_waitqueue_head(&sonypi_device.fifo_proc_list);
1335         init_MUTEX(&sonypi_device.lock);
1336         sonypi_device.bluetooth_power = -1;
1337
1338         if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1339                                      PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1340                 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
1341         else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1342                                           PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1343                 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
1344         else
1345                 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
1346
1347         if (pcidev && pci_enable_device(pcidev)) {
1348                 printk(KERN_ERR "sonypi: pci_enable_device failed\n");
1349                 error = -EIO;
1350                 goto err_put_pcidev;
1351         }
1352
1353         sonypi_device.dev = pcidev;
1354
1355         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
1356                 ioport_list = sonypi_type1_ioport_list;
1357                 sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
1358                 sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
1359                 irq_list = sonypi_type1_irq_list;
1360         } else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
1361                 ioport_list = sonypi_type2_ioport_list;
1362                 sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
1363                 sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
1364                 irq_list = sonypi_type2_irq_list;
1365         } else {
1366                 ioport_list = sonypi_type3_ioport_list;
1367                 sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
1368                 sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
1369                 irq_list = sonypi_type3_irq_list;
1370         }
1371
1372         error = sonypi_setup_ioports(&sonypi_device, ioport_list);
1373         if (error) {
1374                 printk(KERN_ERR "sonypi: failed to request ioports\n");
1375                 goto err_disable_pcidev;
1376         }
1377
1378         error = sonypi_setup_irq(&sonypi_device, irq_list);
1379         if (error) {
1380                 printk(KERN_ERR "sonypi: request_irq failed\n");
1381                 goto err_free_ioports;
1382         }
1383
1384         if (minor != -1)
1385                 sonypi_misc_device.minor = minor;
1386         error = misc_register(&sonypi_misc_device);
1387         if (error) {
1388                 printk(KERN_ERR "sonypi: misc_register failed\n");
1389                 goto err_free_irq;
1390         }
1391
1392         sonypi_display_info();
1393
1394         if (useinput) {
1395
1396                 error = sonypi_create_input_devices();
1397                 if (error) {
1398                         printk(KERN_ERR
1399                                 "sonypi: failed to create input devices\n");
1400                         goto err_miscdev_unregister;
1401                 }
1402
1403                 spin_lock_init(&sonypi_device.input_fifo_lock);
1404                 sonypi_device.input_fifo =
1405                         kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
1406                                     &sonypi_device.input_fifo_lock);
1407                 if (IS_ERR(sonypi_device.input_fifo)) {
1408                         printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1409                         error = PTR_ERR(sonypi_device.input_fifo);
1410                         goto err_inpdev_unregister;
1411                 }
1412
1413                 INIT_WORK(&sonypi_device.input_work, input_keyrelease, NULL);
1414         }
1415
1416         sonypi_enable(0);
1417
1418         return 0;
1419
1420  err_inpdev_unregister:
1421         input_unregister_device(sonypi_device.input_key_dev);
1422         input_unregister_device(sonypi_device.input_jog_dev);
1423  err_miscdev_unregister:
1424         misc_deregister(&sonypi_misc_device);
1425  err_free_irq:
1426         free_irq(sonypi_device.irq, sonypi_irq);
1427  err_free_ioports:
1428         release_region(sonypi_device.ioport1, sonypi_device.region_size);
1429  err_disable_pcidev:
1430         if (pcidev)
1431                 pci_disable_device(pcidev);
1432  err_put_pcidev:
1433         pci_dev_put(pcidev);
1434         kfifo_free(sonypi_device.fifo);
1435
1436         return error;
1437 }
1438
1439 static int __devexit sonypi_remove(struct platform_device *dev)
1440 {
1441         sonypi_disable();
1442
1443         synchronize_sched();  /* Allow sonypi interrupt to complete. */
1444         flush_scheduled_work();
1445
1446         if (useinput) {
1447                 input_unregister_device(sonypi_device.input_key_dev);
1448                 input_unregister_device(sonypi_device.input_jog_dev);
1449                 kfifo_free(sonypi_device.input_fifo);
1450         }
1451
1452         misc_deregister(&sonypi_misc_device);
1453
1454         free_irq(sonypi_device.irq, sonypi_irq);
1455         release_region(sonypi_device.ioport1, sonypi_device.region_size);
1456
1457         if (sonypi_device.dev) {
1458                 pci_disable_device(sonypi_device.dev);
1459                 pci_dev_put(sonypi_device.dev);
1460         }
1461
1462         kfifo_free(sonypi_device.fifo);
1463
1464         return 0;
1465 }
1466
1467 #ifdef CONFIG_PM
1468 static int old_camera_power;
1469
1470 static int sonypi_suspend(struct platform_device *dev, pm_message_t state)
1471 {
1472         old_camera_power = sonypi_device.camera_power;
1473         sonypi_disable();
1474
1475         return 0;
1476 }
1477
1478 static int sonypi_resume(struct platform_device *dev)
1479 {
1480         sonypi_enable(old_camera_power);
1481         return 0;
1482 }
1483 #else
1484 #define sonypi_suspend  NULL
1485 #define sonypi_resume   NULL
1486 #endif
1487
1488 static void sonypi_shutdown(struct platform_device *dev)
1489 {
1490         sonypi_disable();
1491 }
1492
1493 static struct platform_driver sonypi_driver = {
1494         .driver         = {
1495                 .name   = "sonypi",
1496                 .owner  = THIS_MODULE,
1497         },
1498         .probe          = sonypi_probe,
1499         .remove         = __devexit_p(sonypi_remove),
1500         .shutdown       = sonypi_shutdown,
1501         .suspend        = sonypi_suspend,
1502         .resume         = sonypi_resume,
1503 };
1504
1505 static struct platform_device *sonypi_platform_device;
1506
1507 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
1508         {
1509                 .ident = "Sony Vaio",
1510                 .matches = {
1511                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1512                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1513                 },
1514         },
1515         {
1516                 .ident = "Sony Vaio",
1517                 .matches = {
1518                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1519                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1520                 },
1521         },
1522         { }
1523 };
1524
1525 static int __init sonypi_init(void)
1526 {
1527         int error;
1528
1529         printk(KERN_INFO
1530                 "sonypi: Sony Programmable I/O Controller Driver v%s.\n",
1531                 SONYPI_DRIVER_VERSION);
1532
1533         if (!dmi_check_system(sonypi_dmi_table))
1534                 return -ENODEV;
1535
1536         error = platform_driver_register(&sonypi_driver);
1537         if (error)
1538                 return error;
1539
1540         sonypi_platform_device = platform_device_alloc("sonypi", -1);
1541         if (!sonypi_platform_device) {
1542                 error = -ENOMEM;
1543                 goto err_driver_unregister;
1544         }
1545
1546         error = platform_device_add(sonypi_platform_device);
1547         if (error)
1548                 goto err_free_device;
1549
1550 #ifdef CONFIG_ACPI
1551         if (acpi_bus_register_driver(&sonypi_acpi_driver) > 0)
1552                 acpi_enabled = 1;
1553 #endif
1554
1555         return 0;
1556
1557  err_free_device:
1558         platform_device_put(sonypi_platform_device);
1559  err_driver_unregister:
1560         platform_driver_unregister(&sonypi_driver);
1561         return error;
1562 }
1563
1564 static void __exit sonypi_exit(void)
1565 {
1566 #ifdef CONFIG_ACPI
1567         if (acpi_enabled)
1568                 acpi_bus_unregister_driver(&sonypi_acpi_driver);
1569 #endif
1570         platform_device_unregister(sonypi_platform_device);
1571         platform_driver_unregister(&sonypi_driver);
1572         printk(KERN_INFO "sonypi: removed.\n");
1573 }
1574
1575 module_init(sonypi_init);
1576 module_exit(sonypi_exit);