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