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