blob: 6a9e23dc4897e3915508366b65d73969a63f5a51 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Russell Kingd052d1b2005-10-29 19:07:23 +010051#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
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
61MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
62MODULE_DESCRIPTION("Sony Programmable I/O Control Device driver");
63MODULE_LICENSE("GPL");
64MODULE_VERSION(SONYPI_DRIVER_VERSION);
65
66static int minor = -1;
67module_param(minor, int, 0);
68MODULE_PARM_DESC(minor,
69 "minor number of the misc device, default is -1 (automatic)");
70
71static int verbose; /* = 0 */
72module_param(verbose, int, 0644);
73MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
74
75static int fnkeyinit; /* = 0 */
76module_param(fnkeyinit, int, 0444);
77MODULE_PARM_DESC(fnkeyinit,
78 "set this if your Fn keys do not generate any event");
79
80static int camera; /* = 0 */
81module_param(camera, int, 0444);
82MODULE_PARM_DESC(camera,
83 "set this if you have a MotionEye camera (PictureBook series)");
84
85static int compat; /* = 0 */
86module_param(compat, int, 0444);
87MODULE_PARM_DESC(compat,
88 "set this if you want to enable backward compatibility mode");
89
90static unsigned long mask = 0xffffffff;
91module_param(mask, ulong, 0644);
92MODULE_PARM_DESC(mask,
93 "set this to the mask of event you want to enable (see doc)");
94
95static int useinput = 1;
96module_param(useinput, int, 0444);
97MODULE_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
Erik Walingd2052c12005-09-06 15:17:02 -0700102#define SONYPI_DEVICE_MODEL_TYPE3 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/* type1 models use those */
105#define SONYPI_IRQ_PORT 0x8034
106#define SONYPI_IRQ_SHIFT 22
Erik Walingd2052c12005-09-06 15:17:02 -0700107#define SONYPI_TYPE1_BASE 0x50
108#define SONYPI_G10A (SONYPI_TYPE1_BASE+0x14)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#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
Erik Walingd2052c12005-09-06 15:17:02 -0700119/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* 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 */
149struct sonypi_ioport_list {
150 u16 port1;
151 u16 port2;
152};
153
154static 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
163static 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
Erik Walingd2052c12005-09-06 15:17:02 -0700171/* same as in type 2 models */
172static struct sonypi_ioport_list *sonypi_type3_ioport_list =
173 sonypi_type2_ioport_list;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* The set of possible interrupts */
176struct sonypi_irq_list {
177 u16 irq;
178 u16 bits;
179};
180
181static 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
188static 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
Erik Walingd2052c12005-09-06 15:17:02 -0700196/* same as in type2 models */
197static struct sonypi_irq_list *sonypi_type3_irq_list = sonypi_type2_irq_list;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#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
Erik Walingd2052c12005-09-06 15:17:02 -0700242#define SONYPI_WIRELESS_MASK 0x00002000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244struct sonypi_event {
245 u8 data;
246 u8 event;
247};
248
249/* The set of possible button release events */
250static struct sonypi_event sonypi_releaseev[] = {
251 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
252 { 0, 0 }
253};
254
255/* The set of possible jogger events */
256static 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 */
274static 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 */
282static 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 */
309static 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 */
318static 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
Erik Walingd2052c12005-09-06 15:17:02 -0700325/* The set of possible wireless events */
326static struct sonypi_event sonypi_wlessev[] = {
327 { 0x59, SONYPI_EVENT_WIRELESS_ON },
328 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
329 { 0, 0 }
330};
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/* The set of possible back button events */
333static struct sonypi_event sonypi_backev[] = {
334 { 0x20, SONYPI_EVENT_BACK_PRESSED },
335 { 0, 0 }
336};
337
338/* The set of possible help button events */
339static struct sonypi_event sonypi_helpev[] = {
340 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
341 { 0, 0 }
342};
343
344
345/* The set of possible lid events */
346static 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 */
353static struct sonypi_event sonypi_zoomev[] = {
354 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
355 { 0, 0 }
356};
357
358/* The set of possible thumbphrase events */
359static struct sonypi_event sonypi_thumbphraseev[] = {
360 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
361 { 0, 0 }
362};
363
364/* The set of possible motioneye camera events */
365static 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 */
372static 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 */
379static struct sonypi_event sonypi_batteryev[] = {
380 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
381 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
382 { 0, 0 }
383};
384
385static 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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 { 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
Erik Walingd2052c12005-09-06 15:17:02 -0700417 { 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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 { 0 }
424};
425
426#define SONYPI_BUF_SIZE 128
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/* Correspondance table between sonypi events and input layer events */
429static 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
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500467struct sonypi_keypress {
468 struct input_dev *dev;
469 int key;
470};
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static struct sonypi_device {
473 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 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;
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -0500489 struct input_dev *input_jog_dev;
490 struct input_dev *input_key_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 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
513static int sonypi_ec_write(u8 addr, u8 value)
514{
515#ifdef CONFIG_ACPI_EC
516 if (SONYPI_ACPI_ACTIVE)
517 return ec_write(addr, value);
518#endif
519 wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
520 outb_p(0x81, SONYPI_CST_IOPORT);
521 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
522 outb_p(addr, SONYPI_DATA_IOPORT);
523 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
524 outb_p(value, SONYPI_DATA_IOPORT);
525 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
526 return 0;
527}
528
529static int sonypi_ec_read(u8 addr, u8 *value)
530{
531#ifdef CONFIG_ACPI_EC
532 if (SONYPI_ACPI_ACTIVE)
533 return ec_read(addr, value);
534#endif
535 wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
536 outb_p(0x80, SONYPI_CST_IOPORT);
537 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
538 outb_p(addr, SONYPI_DATA_IOPORT);
539 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
540 *value = inb_p(SONYPI_DATA_IOPORT);
541 return 0;
542}
543
544static int ec_read16(u8 addr, u16 *value)
545{
546 u8 val_lb, val_hb;
547 if (sonypi_ec_read(addr, &val_lb))
548 return -1;
549 if (sonypi_ec_read(addr + 1, &val_hb))
550 return -1;
551 *value = val_lb | (val_hb << 8);
552 return 0;
553}
554
555/* Initializes the device - this comes from the AML code in the ACPI bios */
556static void sonypi_type1_srs(void)
557{
558 u32 v;
559
560 pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
561 v = (v & 0xFFFF0000) | ((u32) sonypi_device.ioport1);
562 pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
563
564 pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
565 v = (v & 0xFFF0FFFF) |
566 (((u32) sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
567 pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
568
569 v = inl(SONYPI_IRQ_PORT);
570 v &= ~(((u32) 0x3) << SONYPI_IRQ_SHIFT);
571 v |= (((u32) sonypi_device.bits) << SONYPI_IRQ_SHIFT);
572 outl(v, SONYPI_IRQ_PORT);
573
574 pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
575 v = (v & 0xFF1FFFFF) | 0x00C00000;
576 pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
577}
578
579static void sonypi_type2_srs(void)
580{
581 if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
582 printk(KERN_WARNING "ec_write failed\n");
583 if (sonypi_ec_write(SONYPI_SLOB, sonypi_device.ioport1 & 0x00FF))
584 printk(KERN_WARNING "ec_write failed\n");
585 if (sonypi_ec_write(SONYPI_SIRQ, sonypi_device.bits))
586 printk(KERN_WARNING "ec_write failed\n");
587 udelay(10);
588}
589
Erik Walingd2052c12005-09-06 15:17:02 -0700590static void sonypi_type3_srs(void)
591{
592 u16 v16;
593 u8 v8;
594
595 /* This model type uses the same initialiazation of
596 * the embedded controller as the type2 models. */
597 sonypi_type2_srs();
598
599 /* Initialization of PCI config space of the LPC interface bridge. */
600 v16 = (sonypi_device.ioport1 & 0xFFF0) | 0x01;
601 pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, v16);
602 pci_read_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, &v8);
603 v8 = (v8 & 0xCF) | 0x10;
604 pci_write_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, v8);
605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/* Disables the device - this comes from the AML code in the ACPI bios */
608static void sonypi_type1_dis(void)
609{
610 u32 v;
611
612 pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
613 v = v & 0xFF3FFFFF;
614 pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
615
616 v = inl(SONYPI_IRQ_PORT);
617 v |= (0x3 << SONYPI_IRQ_SHIFT);
618 outl(v, SONYPI_IRQ_PORT);
619}
620
621static void sonypi_type2_dis(void)
622{
623 if (sonypi_ec_write(SONYPI_SHIB, 0))
624 printk(KERN_WARNING "ec_write failed\n");
625 if (sonypi_ec_write(SONYPI_SLOB, 0))
626 printk(KERN_WARNING "ec_write failed\n");
627 if (sonypi_ec_write(SONYPI_SIRQ, 0))
628 printk(KERN_WARNING "ec_write failed\n");
629}
630
Erik Walingd2052c12005-09-06 15:17:02 -0700631static void sonypi_type3_dis(void)
632{
633 sonypi_type2_dis();
634 udelay(10);
635 pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, 0);
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638static u8 sonypi_call1(u8 dev)
639{
640 u8 v1, v2;
641
642 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
643 outb(dev, sonypi_device.ioport2);
644 v1 = inb_p(sonypi_device.ioport2);
645 v2 = inb_p(sonypi_device.ioport1);
646 return v2;
647}
648
649static u8 sonypi_call2(u8 dev, u8 fn)
650{
651 u8 v1;
652
653 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
654 outb(dev, sonypi_device.ioport2);
655 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
656 outb(fn, sonypi_device.ioport1);
657 v1 = inb_p(sonypi_device.ioport1);
658 return v1;
659}
660
661static u8 sonypi_call3(u8 dev, u8 fn, u8 v)
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 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
670 outb(v, sonypi_device.ioport1);
671 v1 = inb_p(sonypi_device.ioport1);
672 return v1;
673}
674
675#if 0
676/* Get brightness, hue etc. Unreliable... */
677static u8 sonypi_read(u8 fn)
678{
679 u8 v1, v2;
680 int n = 100;
681
682 while (n--) {
683 v1 = sonypi_call2(0x8f, fn);
684 v2 = sonypi_call2(0x8f, fn);
685 if (v1 == v2 && v1 != 0xff)
686 return v1;
687 }
688 return 0xff;
689}
690#endif
691
692/* Set brightness, hue etc */
693static void sonypi_set(u8 fn, u8 v)
694{
695 wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
696}
697
698/* Tests if the camera is ready */
699static int sonypi_camera_ready(void)
700{
701 u8 v;
702
703 v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
704 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
705}
706
707/* Turns the camera off */
708static void sonypi_camera_off(void)
709{
710 sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
711
712 if (!sonypi_device.camera_power)
713 return;
714
715 sonypi_call2(0x91, 0);
716 sonypi_device.camera_power = 0;
717}
718
719/* Turns the camera on */
720static void sonypi_camera_on(void)
721{
722 int i, j;
723
724 if (sonypi_device.camera_power)
725 return;
726
727 for (j = 5; j > 0; j--) {
728
729 while (sonypi_call2(0x91, 0x1))
730 msleep(10);
731 sonypi_call1(0x93);
732
733 for (i = 400; i > 0; i--) {
734 if (sonypi_camera_ready())
735 break;
736 msleep(10);
737 }
738 if (i)
739 break;
740 }
741
742 if (j == 0) {
743 printk(KERN_WARNING "sonypi: failed to power on camera\n");
744 return;
745 }
746
747 sonypi_set(0x10, 0x5a);
748 sonypi_device.camera_power = 1;
749}
750
751/* sets the bluetooth subsystem power state */
752static void sonypi_setbluetoothpower(u8 state)
753{
754 state = !!state;
755
756 if (sonypi_device.bluetooth_power == state)
757 return;
758
759 sonypi_call2(0x96, state);
760 sonypi_call1(0x82);
761 sonypi_device.bluetooth_power = state;
762}
763
764static void input_keyrelease(void *data)
765{
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500766 struct sonypi_keypress kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500768 while (kfifo_get(sonypi_device.input_fifo, (unsigned char *)&kp,
769 sizeof(kp)) == sizeof(kp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 msleep(10);
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500771 input_report_key(kp.dev, kp.key, 0);
772 input_sync(kp.dev);
773 }
774}
775
776static void sonypi_report_input_event(u8 event)
777{
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -0500778 struct input_dev *jog_dev = sonypi_device.input_jog_dev;
779 struct input_dev *key_dev = sonypi_device.input_key_dev;
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500780 struct sonypi_keypress kp = { NULL };
781 int i;
782
783 switch (event) {
784 case SONYPI_EVENT_JOGDIAL_UP:
785 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
786 input_report_rel(jog_dev, REL_WHEEL, 1);
787 input_sync(jog_dev);
788 break;
789
790 case SONYPI_EVENT_JOGDIAL_DOWN:
791 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
792 input_report_rel(jog_dev, REL_WHEEL, -1);
793 input_sync(jog_dev);
794 break;
795
796 case SONYPI_EVENT_JOGDIAL_PRESSED:
797 kp.key = BTN_MIDDLE;
798 kp.dev = jog_dev;
799 break;
800
801 case SONYPI_EVENT_FNKEY_RELEASED:
802 /* Nothing, not all VAIOs generate this event */
803 break;
804
805 default:
806 for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
807 if (event == sonypi_inputkeys[i].sonypiev) {
808 kp.dev = key_dev;
809 kp.key = sonypi_inputkeys[i].inputev;
810 break;
811 }
812 break;
813 }
814
815 if (kp.dev) {
816 input_report_key(kp.dev, kp.key, 1);
817 input_sync(kp.dev);
818 kfifo_put(sonypi_device.input_fifo,
819 (unsigned char *)&kp, sizeof(kp));
820 schedule_work(&sonypi_device.input_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822}
823
824/* Interrupt handler: some event is available */
825static irqreturn_t sonypi_irq(int irq, void *dev_id, struct pt_regs *regs)
826{
827 u8 v1, v2, event = 0;
828 int i, j;
829
830 v1 = inb_p(sonypi_device.ioport1);
831 v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
832
833 for (i = 0; sonypi_eventtypes[i].model; i++) {
834 if (sonypi_device.model != sonypi_eventtypes[i].model)
835 continue;
836 if ((v2 & sonypi_eventtypes[i].data) !=
837 sonypi_eventtypes[i].data)
838 continue;
839 if (!(mask & sonypi_eventtypes[i].mask))
840 continue;
841 for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
842 if (v1 == sonypi_eventtypes[i].events[j].data) {
843 event = sonypi_eventtypes[i].events[j].event;
844 goto found;
845 }
846 }
847 }
848
849 if (verbose)
850 printk(KERN_WARNING
851 "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",
852 v1, v2);
853 /* We need to return IRQ_HANDLED here because there *are*
854 * events belonging to the sonypi device we don't know about,
855 * but we still don't want those to pollute the logs... */
856 return IRQ_HANDLED;
857
858found:
859 if (verbose > 1)
860 printk(KERN_INFO
861 "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
862
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500863 if (useinput)
864 sonypi_report_input_event(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 kfifo_put(sonypi_device.fifo, (unsigned char *)&event, sizeof(event));
867 kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
868 wake_up_interruptible(&sonypi_device.fifo_proc_list);
869
870 return IRQ_HANDLED;
871}
872
873/* External camera command (exported to the motion eye v4l driver) */
874int sonypi_camera_command(int command, u8 value)
875{
876 if (!camera)
877 return -EIO;
878
879 down(&sonypi_device.lock);
880
881 switch (command) {
882 case SONYPI_COMMAND_SETCAMERA:
883 if (value)
884 sonypi_camera_on();
885 else
886 sonypi_camera_off();
887 break;
888 case SONYPI_COMMAND_SETCAMERABRIGHTNESS:
889 sonypi_set(SONYPI_CAMERA_BRIGHTNESS, value);
890 break;
891 case SONYPI_COMMAND_SETCAMERACONTRAST:
892 sonypi_set(SONYPI_CAMERA_CONTRAST, value);
893 break;
894 case SONYPI_COMMAND_SETCAMERAHUE:
895 sonypi_set(SONYPI_CAMERA_HUE, value);
896 break;
897 case SONYPI_COMMAND_SETCAMERACOLOR:
898 sonypi_set(SONYPI_CAMERA_COLOR, value);
899 break;
900 case SONYPI_COMMAND_SETCAMERASHARPNESS:
901 sonypi_set(SONYPI_CAMERA_SHARPNESS, value);
902 break;
903 case SONYPI_COMMAND_SETCAMERAPICTURE:
904 sonypi_set(SONYPI_CAMERA_PICTURE, value);
905 break;
906 case SONYPI_COMMAND_SETCAMERAAGC:
907 sonypi_set(SONYPI_CAMERA_AGC, value);
908 break;
909 default:
910 printk(KERN_ERR "sonypi: sonypi_camera_command invalid: %d\n",
911 command);
912 break;
913 }
914 up(&sonypi_device.lock);
915 return 0;
916}
917
918EXPORT_SYMBOL(sonypi_camera_command);
919
920static int sonypi_misc_fasync(int fd, struct file *filp, int on)
921{
922 int retval;
923
924 retval = fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
925 if (retval < 0)
926 return retval;
927 return 0;
928}
929
930static int sonypi_misc_release(struct inode *inode, struct file *file)
931{
932 sonypi_misc_fasync(-1, file, 0);
933 down(&sonypi_device.lock);
934 sonypi_device.open_count--;
935 up(&sonypi_device.lock);
936 return 0;
937}
938
939static int sonypi_misc_open(struct inode *inode, struct file *file)
940{
941 down(&sonypi_device.lock);
942 /* Flush input queue on first open */
943 if (!sonypi_device.open_count)
944 kfifo_reset(sonypi_device.fifo);
945 sonypi_device.open_count++;
946 up(&sonypi_device.lock);
947 return 0;
948}
949
950static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
951 size_t count, loff_t *pos)
952{
953 ssize_t ret;
954 unsigned char c;
955
956 if ((kfifo_len(sonypi_device.fifo) == 0) &&
957 (file->f_flags & O_NONBLOCK))
958 return -EAGAIN;
959
960 ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
961 kfifo_len(sonypi_device.fifo) != 0);
962 if (ret)
963 return ret;
964
965 while (ret < count &&
966 (kfifo_get(sonypi_device.fifo, &c, sizeof(c)) == sizeof(c))) {
967 if (put_user(c, buf++))
968 return -EFAULT;
969 ret++;
970 }
971
972 if (ret > 0) {
973 struct inode *inode = file->f_dentry->d_inode;
974 inode->i_atime = current_fs_time(inode->i_sb);
975 }
976
977 return ret;
978}
979
980static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
981{
982 poll_wait(file, &sonypi_device.fifo_proc_list, wait);
983 if (kfifo_len(sonypi_device.fifo))
984 return POLLIN | POLLRDNORM;
985 return 0;
986}
987
988static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
989 unsigned int cmd, unsigned long arg)
990{
991 int ret = 0;
992 void __user *argp = (void __user *)arg;
993 u8 val8;
994 u16 val16;
995
996 down(&sonypi_device.lock);
997 switch (cmd) {
998 case SONYPI_IOCGBRT:
999 if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
1000 ret = -EIO;
1001 break;
1002 }
1003 if (copy_to_user(argp, &val8, sizeof(val8)))
1004 ret = -EFAULT;
1005 break;
1006 case SONYPI_IOCSBRT:
1007 if (copy_from_user(&val8, argp, sizeof(val8))) {
1008 ret = -EFAULT;
1009 break;
1010 }
1011 if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
1012 ret = -EIO;
1013 break;
1014 case SONYPI_IOCGBAT1CAP:
1015 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1016 ret = -EIO;
1017 break;
1018 }
1019 if (copy_to_user(argp, &val16, sizeof(val16)))
1020 ret = -EFAULT;
1021 break;
1022 case SONYPI_IOCGBAT1REM:
1023 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1024 ret = -EIO;
1025 break;
1026 }
1027 if (copy_to_user(argp, &val16, sizeof(val16)))
1028 ret = -EFAULT;
1029 break;
1030 case SONYPI_IOCGBAT2CAP:
1031 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1032 ret = -EIO;
1033 break;
1034 }
1035 if (copy_to_user(argp, &val16, sizeof(val16)))
1036 ret = -EFAULT;
1037 break;
1038 case SONYPI_IOCGBAT2REM:
1039 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1040 ret = -EIO;
1041 break;
1042 }
1043 if (copy_to_user(argp, &val16, sizeof(val16)))
1044 ret = -EFAULT;
1045 break;
1046 case SONYPI_IOCGBATFLAGS:
1047 if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
1048 ret = -EIO;
1049 break;
1050 }
1051 val8 &= 0x07;
1052 if (copy_to_user(argp, &val8, sizeof(val8)))
1053 ret = -EFAULT;
1054 break;
1055 case SONYPI_IOCGBLUE:
1056 val8 = sonypi_device.bluetooth_power;
1057 if (copy_to_user(argp, &val8, sizeof(val8)))
1058 ret = -EFAULT;
1059 break;
1060 case SONYPI_IOCSBLUE:
1061 if (copy_from_user(&val8, argp, sizeof(val8))) {
1062 ret = -EFAULT;
1063 break;
1064 }
1065 sonypi_setbluetoothpower(val8);
1066 break;
1067 /* FAN Controls */
1068 case SONYPI_IOCGFAN:
1069 if (sonypi_ec_read(SONYPI_FAN0_STATUS, &val8)) {
1070 ret = -EIO;
1071 break;
1072 }
Al Viro05559852005-05-04 05:40:02 +01001073 if (copy_to_user(argp, &val8, sizeof(val8)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ret = -EFAULT;
1075 break;
1076 case SONYPI_IOCSFAN:
Al Viro05559852005-05-04 05:40:02 +01001077 if (copy_from_user(&val8, argp, sizeof(val8))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ret = -EFAULT;
1079 break;
1080 }
1081 if (sonypi_ec_write(SONYPI_FAN0_STATUS, val8))
1082 ret = -EIO;
1083 break;
1084 /* GET Temperature (useful under APM) */
1085 case SONYPI_IOCGTEMP:
1086 if (sonypi_ec_read(SONYPI_TEMP_STATUS, &val8)) {
1087 ret = -EIO;
1088 break;
1089 }
Al Viro05559852005-05-04 05:40:02 +01001090 if (copy_to_user(argp, &val8, sizeof(val8)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 ret = -EFAULT;
1092 break;
1093 default:
1094 ret = -EINVAL;
1095 }
1096 up(&sonypi_device.lock);
1097 return ret;
1098}
1099
1100static struct file_operations sonypi_misc_fops = {
1101 .owner = THIS_MODULE,
1102 .read = sonypi_misc_read,
1103 .poll = sonypi_misc_poll,
1104 .open = sonypi_misc_open,
1105 .release = sonypi_misc_release,
1106 .fasync = sonypi_misc_fasync,
1107 .ioctl = sonypi_misc_ioctl,
1108};
1109
1110static struct miscdevice sonypi_misc_device = {
1111 .minor = MISC_DYNAMIC_MINOR,
1112 .name = "sonypi",
1113 .fops = &sonypi_misc_fops,
1114};
1115
1116static void sonypi_enable(unsigned int camera_on)
1117{
Erik Walingd2052c12005-09-06 15:17:02 -07001118 switch (sonypi_device.model) {
1119 case SONYPI_DEVICE_MODEL_TYPE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 sonypi_type1_srs();
Erik Walingd2052c12005-09-06 15:17:02 -07001121 break;
1122 case SONYPI_DEVICE_MODEL_TYPE2:
1123 sonypi_type2_srs();
1124 break;
1125 case SONYPI_DEVICE_MODEL_TYPE3:
1126 sonypi_type3_srs();
1127 break;
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 sonypi_call1(0x82);
1131 sonypi_call2(0x81, 0xff);
1132 sonypi_call1(compat ? 0x92 : 0x82);
1133
1134 /* Enable ACPI mode to get Fn key events */
1135 if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1136 outb(0xf0, 0xb2);
1137
1138 if (camera && camera_on)
1139 sonypi_camera_on();
1140}
1141
1142static int sonypi_disable(void)
1143{
1144 sonypi_call2(0x81, 0); /* make sure we don't get any more events */
1145 if (camera)
1146 sonypi_camera_off();
1147
1148 /* disable ACPI mode */
1149 if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
1150 outb(0xf1, 0xb2);
1151
Erik Walingd2052c12005-09-06 15:17:02 -07001152 switch (sonypi_device.model) {
1153 case SONYPI_DEVICE_MODEL_TYPE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sonypi_type1_dis();
Erik Walingd2052c12005-09-06 15:17:02 -07001155 break;
1156 case SONYPI_DEVICE_MODEL_TYPE2:
1157 sonypi_type2_dis();
1158 break;
1159 case SONYPI_DEVICE_MODEL_TYPE3:
1160 sonypi_type3_dis();
1161 break;
1162 }
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return 0;
1165}
1166
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001167static int __devinit sonypi_create_input_devices(void)
1168{
1169 struct input_dev *jog_dev;
1170 struct input_dev *key_dev;
1171 int i;
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001172 int error;
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001173
1174 sonypi_device.input_jog_dev = jog_dev = input_allocate_device();
1175 if (!jog_dev)
1176 return -ENOMEM;
1177
1178 jog_dev->name = "Sony Vaio Jogdial";
1179 jog_dev->id.bustype = BUS_ISA;
1180 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
1181
1182 jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
1183 jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
1184 jog_dev->relbit[0] = BIT(REL_WHEEL);
1185
1186 sonypi_device.input_key_dev = key_dev = input_allocate_device();
1187 if (!key_dev) {
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001188 error = -ENOMEM;
1189 goto err_free_jogdev;
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001190 }
1191
1192 key_dev->name = "Sony Vaio Keys";
1193 key_dev->id.bustype = BUS_ISA;
1194 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
1195
1196 /* Initialize the Input Drivers: special keys */
1197 key_dev->evbit[0] = BIT(EV_KEY);
1198 for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
1199 if (sonypi_inputkeys[i].inputev)
1200 set_bit(sonypi_inputkeys[i].inputev, key_dev->keybit);
1201
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001202 error = input_register_device(jog_dev);
1203 if (error)
1204 goto err_free_keydev;
1205
1206 error = input_register_device(key_dev);
1207 if (error)
1208 goto err_unregister_jogdev;
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001209
1210 return 0;
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001211
1212 err_unregister_jogdev:
1213 input_unregister_device(jog_dev);
1214 /* Set to NULL so we don't free it again below */
1215 jog_dev = NULL;
1216 err_free_keydev:
1217 input_free_device(key_dev);
1218 sonypi_device.input_key_dev = NULL;
1219 err_free_jogdev:
1220 input_free_device(jog_dev);
1221 sonypi_device.input_jog_dev = NULL;
1222
1223 return error;
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001224}
1225
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001226static int __devinit sonypi_setup_ioports(struct sonypi_device *dev,
1227 const struct sonypi_ioport_list *ioport_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001229 while (ioport_list->port1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001231 if (request_region(ioport_list->port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 sonypi_device.region_size,
1233 "Sony Programable I/O Device")) {
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001234 dev->ioport1 = ioport_list->port1;
1235 dev->ioport2 = ioport_list->port2;
1236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001238 ioport_list++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001241 return -EBUSY;
1242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001244static int __devinit sonypi_setup_irq(struct sonypi_device *dev,
1245 const struct sonypi_irq_list *irq_list)
1246{
1247 while (irq_list->irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001249 if (!request_irq(irq_list->irq, sonypi_irq,
1250 SA_SHIRQ, "sonypi", sonypi_irq)) {
1251 dev->irq = irq_list->irq;
1252 dev->bits = irq_list->bits;
1253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001255 irq_list++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001258 return -EBUSY;
1259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001261static void __devinit sonypi_display_info(void)
1262{
Erik Walingd2052c12005-09-06 15:17:02 -07001263 printk(KERN_INFO "sonypi: detected type%d model, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 "verbose = %d, fnkeyinit = %s, camera = %s, "
1265 "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
Erik Walingd2052c12005-09-06 15:17:02 -07001266 sonypi_device.model,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 verbose,
1268 fnkeyinit ? "on" : "off",
1269 camera ? "on" : "off",
1270 compat ? "on" : "off",
1271 mask,
1272 useinput ? "on" : "off",
1273 SONYPI_ACPI_ACTIVE ? "on" : "off");
1274 printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
1275 sonypi_device.irq,
1276 sonypi_device.ioport1, sonypi_device.ioport2);
1277
1278 if (minor == -1)
1279 printk(KERN_INFO "sonypi: device allocated minor is %d\n",
1280 sonypi_misc_device.minor);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001281}
1282
1283static int __devinit sonypi_probe(struct platform_device *dev)
1284{
1285 const struct sonypi_ioport_list *ioport_list;
1286 const struct sonypi_irq_list *irq_list;
1287 struct pci_dev *pcidev;
1288 int error;
1289
1290 spin_lock_init(&sonypi_device.fifo_lock);
1291 sonypi_device.fifo = kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
1292 &sonypi_device.fifo_lock);
1293 if (IS_ERR(sonypi_device.fifo)) {
1294 printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1295 return PTR_ERR(sonypi_device.fifo);
1296 }
1297
1298 init_waitqueue_head(&sonypi_device.fifo_proc_list);
1299 init_MUTEX(&sonypi_device.lock);
1300 sonypi_device.bluetooth_power = -1;
1301
1302 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1303 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1304 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
1305 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1306 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1307 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
1308 else
1309 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
1310
1311 if (pcidev && pci_enable_device(pcidev)) {
1312 printk(KERN_ERR "sonypi: pci_enable_device failed\n");
1313 error = -EIO;
1314 goto err_put_pcidev;
1315 }
1316
1317 sonypi_device.dev = pcidev;
1318
1319 if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
1320 ioport_list = sonypi_type1_ioport_list;
1321 sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
1322 sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
1323 irq_list = sonypi_type1_irq_list;
1324 } else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
1325 ioport_list = sonypi_type2_ioport_list;
1326 sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
1327 sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
1328 irq_list = sonypi_type2_irq_list;
1329 } else {
1330 ioport_list = sonypi_type3_ioport_list;
1331 sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
1332 sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
1333 irq_list = sonypi_type3_irq_list;
1334 }
1335
1336 error = sonypi_setup_ioports(&sonypi_device, ioport_list);
1337 if (error) {
1338 printk(KERN_ERR "sonypi: failed to request ioports\n");
1339 goto err_disable_pcidev;
1340 }
1341
1342 error = sonypi_setup_irq(&sonypi_device, irq_list);
1343 if (error) {
1344 printk(KERN_ERR "sonypi: request_irq failed\n");
1345 goto err_free_ioports;
1346 }
1347
1348 if (minor != -1)
1349 sonypi_misc_device.minor = minor;
1350 error = misc_register(&sonypi_misc_device);
1351 if (error) {
1352 printk(KERN_ERR "sonypi: misc_register failed\n");
1353 goto err_free_irq;
1354 }
1355
1356 sonypi_display_info();
1357
1358 if (useinput) {
1359
1360 error = sonypi_create_input_devices();
1361 if (error) {
1362 printk(KERN_ERR
1363 "sonypi: failed to create input devices\n");
1364 goto err_miscdev_unregister;
1365 }
1366
1367 spin_lock_init(&sonypi_device.input_fifo_lock);
1368 sonypi_device.input_fifo =
1369 kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
1370 &sonypi_device.input_fifo_lock);
1371 if (IS_ERR(sonypi_device.input_fifo)) {
1372 printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
1373 error = PTR_ERR(sonypi_device.input_fifo);
1374 goto err_inpdev_unregister;
1375 }
1376
1377 INIT_WORK(&sonypi_device.input_work, input_keyrelease, NULL);
1378 }
1379
1380 sonypi_enable(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 return 0;
1383
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001384 err_inpdev_unregister:
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001385 input_unregister_device(sonypi_device.input_key_dev);
1386 input_unregister_device(sonypi_device.input_jog_dev);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001387 err_miscdev_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 misc_deregister(&sonypi_misc_device);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001389 err_free_irq:
1390 free_irq(sonypi_device.irq, sonypi_irq);
1391 err_free_ioports:
1392 release_region(sonypi_device.ioport1, sonypi_device.region_size);
1393 err_disable_pcidev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (pcidev)
1395 pci_disable_device(pcidev);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001396 err_put_pcidev:
1397 pci_dev_put(pcidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 kfifo_free(sonypi_device.fifo);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001399
1400 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001403static int __devexit sonypi_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 sonypi_disable();
1406
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -05001407 synchronize_sched(); /* Allow sonypi interrupt to complete. */
1408 flush_scheduled_work();
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (useinput) {
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001411 input_unregister_device(sonypi_device.input_key_dev);
1412 input_unregister_device(sonypi_device.input_jog_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 kfifo_free(sonypi_device.input_fifo);
1414 }
1415
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001416 misc_deregister(&sonypi_misc_device);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 free_irq(sonypi_device.irq, sonypi_irq);
1419 release_region(sonypi_device.ioport1, sonypi_device.region_size);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001420
1421 if (sonypi_device.dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 pci_disable_device(sonypi_device.dev);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001423 pci_dev_put(sonypi_device.dev);
1424 }
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 kfifo_free(sonypi_device.fifo);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001427
1428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001431#ifdef CONFIG_PM
1432static int old_camera_power;
1433
1434static int sonypi_suspend(struct platform_device *dev, pm_message_t state)
1435{
1436 old_camera_power = sonypi_device.camera_power;
1437 sonypi_disable();
1438
1439 return 0;
1440}
1441
1442static int sonypi_resume(struct platform_device *dev)
1443{
1444 sonypi_enable(old_camera_power);
1445 return 0;
1446}
1447#else
1448#define sonypi_suspend NULL
1449#define sonypi_resume NULL
1450#endif
1451
1452static void sonypi_shutdown(struct platform_device *dev)
1453{
1454 sonypi_disable();
1455}
1456
1457static struct platform_driver sonypi_driver = {
1458 .driver = {
1459 .name = "sonypi",
1460 .owner = THIS_MODULE,
1461 },
1462 .probe = sonypi_probe,
1463 .remove = __devexit_p(sonypi_remove),
1464 .shutdown = sonypi_shutdown,
1465 .suspend = sonypi_suspend,
1466 .resume = sonypi_resume,
1467};
1468
1469static struct platform_device *sonypi_platform_device;
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471static struct dmi_system_id __initdata sonypi_dmi_table[] = {
1472 {
1473 .ident = "Sony Vaio",
1474 .matches = {
1475 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1476 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1477 },
1478 },
1479 {
1480 .ident = "Sony Vaio",
1481 .matches = {
1482 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1483 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1484 },
1485 },
1486 { }
1487};
1488
1489static int __init sonypi_init(void)
1490{
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001491 int error;
1492
1493 printk(KERN_INFO
1494 "sonypi: Sony Programmable I/O Controller Driver v%s.\n",
1495 SONYPI_DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if (!dmi_check_system(sonypi_dmi_table))
1498 return -ENODEV;
1499
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001500 error = platform_driver_register(&sonypi_driver);
1501 if (error)
1502 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001504 sonypi_platform_device = platform_device_alloc("sonypi", -1);
1505 if (!sonypi_platform_device) {
1506 error = -ENOMEM;
1507 goto err_driver_unregister;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001510 error = platform_device_add(sonypi_platform_device);
1511 if (error)
1512 goto err_free_device;
1513
1514 return 0;
1515
1516 err_free_device:
1517 platform_device_put(sonypi_platform_device);
1518 err_driver_unregister:
1519 platform_driver_unregister(&sonypi_driver);
1520 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523static void __exit sonypi_exit(void)
1524{
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001525 platform_device_unregister(sonypi_platform_device);
Russell King3ae5eae2005-11-09 22:32:44 +00001526 platform_driver_unregister(&sonypi_driver);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001527 printk(KERN_INFO "sonypi: removed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
1530module_init(sonypi_init);
1531module_exit(sonypi_exit);