blob: 73f66d03624dfd2b1e51a0aab8c236fc6ec4c7d6 [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>
Ingo Molnar86ae13b2009-10-12 16:22:46 +020039#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/input.h>
41#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/init.h>
43#include <linux/interrupt.h>
44#include <linux/miscdevice.h>
45#include <linux/poll.h>
46#include <linux/delay.h>
47#include <linux/wait.h>
48#include <linux/acpi.h>
49#include <linux/dmi.h>
50#include <linux/err.h>
51#include <linux/kfifo.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010052#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/uaccess.h>
56#include <asm/io.h>
57#include <asm/system.h>
58
59#include <linux/sonypi.h>
60
61#define SONYPI_DRIVER_VERSION "1.26"
62
63MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
64MODULE_DESCRIPTION("Sony Programmable I/O Control Device driver");
65MODULE_LICENSE("GPL");
66MODULE_VERSION(SONYPI_DRIVER_VERSION);
67
68static int minor = -1;
69module_param(minor, int, 0);
70MODULE_PARM_DESC(minor,
71 "minor number of the misc device, default is -1 (automatic)");
72
73static int verbose; /* = 0 */
74module_param(verbose, int, 0644);
75MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
76
77static int fnkeyinit; /* = 0 */
78module_param(fnkeyinit, int, 0444);
79MODULE_PARM_DESC(fnkeyinit,
80 "set this if your Fn keys do not generate any event");
81
82static int camera; /* = 0 */
83module_param(camera, int, 0444);
84MODULE_PARM_DESC(camera,
85 "set this if you have a MotionEye camera (PictureBook series)");
86
87static int compat; /* = 0 */
88module_param(compat, int, 0444);
89MODULE_PARM_DESC(compat,
90 "set this if you want to enable backward compatibility mode");
91
92static unsigned long mask = 0xffffffff;
93module_param(mask, ulong, 0644);
94MODULE_PARM_DESC(mask,
95 "set this to the mask of event you want to enable (see doc)");
96
97static int useinput = 1;
98module_param(useinput, int, 0444);
99MODULE_PARM_DESC(useinput,
100 "set this if you would like sonypi to feed events to the input subsystem");
101
malattia@linux.it1a3e3232007-04-28 23:34:10 +0900102static int check_ioport = 1;
103module_param(check_ioport, int, 0444);
104MODULE_PARM_DESC(check_ioport,
105 "set this to 0 if you think the automatic ioport check for sony-laptop is wrong");
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define SONYPI_DEVICE_MODEL_TYPE1 1
108#define SONYPI_DEVICE_MODEL_TYPE2 2
Erik Walingd2052c12005-09-06 15:17:02 -0700109#define SONYPI_DEVICE_MODEL_TYPE3 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/* type1 models use those */
112#define SONYPI_IRQ_PORT 0x8034
113#define SONYPI_IRQ_SHIFT 22
Erik Walingd2052c12005-09-06 15:17:02 -0700114#define SONYPI_TYPE1_BASE 0x50
115#define SONYPI_G10A (SONYPI_TYPE1_BASE+0x14)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define SONYPI_TYPE1_REGION_SIZE 0x08
117#define SONYPI_TYPE1_EVTYPE_OFFSET 0x04
118
119/* type2 series specifics */
120#define SONYPI_SIRQ 0x9b
121#define SONYPI_SLOB 0x9c
122#define SONYPI_SHIB 0x9d
123#define SONYPI_TYPE2_REGION_SIZE 0x20
124#define SONYPI_TYPE2_EVTYPE_OFFSET 0x12
125
Erik Walingd2052c12005-09-06 15:17:02 -0700126/* type3 series specifics */
127#define SONYPI_TYPE3_BASE 0x40
128#define SONYPI_TYPE3_GID2 (SONYPI_TYPE3_BASE+0x48) /* 16 bits */
129#define SONYPI_TYPE3_MISC (SONYPI_TYPE3_BASE+0x6d) /* 8 bits */
130#define SONYPI_TYPE3_REGION_SIZE 0x20
131#define SONYPI_TYPE3_EVTYPE_OFFSET 0x12
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* battery / brightness addresses */
134#define SONYPI_BAT_FLAGS 0x81
135#define SONYPI_LCD_LIGHT 0x96
136#define SONYPI_BAT1_PCTRM 0xa0
137#define SONYPI_BAT1_LEFT 0xa2
138#define SONYPI_BAT1_MAXRT 0xa4
139#define SONYPI_BAT2_PCTRM 0xa8
140#define SONYPI_BAT2_LEFT 0xaa
141#define SONYPI_BAT2_MAXRT 0xac
142#define SONYPI_BAT1_MAXTK 0xb0
143#define SONYPI_BAT1_FULL 0xb2
144#define SONYPI_BAT2_MAXTK 0xb8
145#define SONYPI_BAT2_FULL 0xba
146
147/* FAN0 information (reverse engineered from ACPI tables) */
148#define SONYPI_FAN0_STATUS 0x93
149#define SONYPI_TEMP_STATUS 0xC1
150
151/* ioports used for brightness and type2 events */
152#define SONYPI_DATA_IOPORT 0x62
153#define SONYPI_CST_IOPORT 0x66
154
155/* The set of possible ioports */
156struct sonypi_ioport_list {
157 u16 port1;
158 u16 port2;
159};
160
161static struct sonypi_ioport_list sonypi_type1_ioport_list[] = {
162 { 0x10c0, 0x10c4 }, /* looks like the default on C1Vx */
163 { 0x1080, 0x1084 },
164 { 0x1090, 0x1094 },
165 { 0x10a0, 0x10a4 },
166 { 0x10b0, 0x10b4 },
167 { 0x0, 0x0 }
168};
169
170static struct sonypi_ioport_list sonypi_type2_ioport_list[] = {
171 { 0x1080, 0x1084 },
172 { 0x10a0, 0x10a4 },
173 { 0x10c0, 0x10c4 },
174 { 0x10e0, 0x10e4 },
175 { 0x0, 0x0 }
176};
177
Erik Walingd2052c12005-09-06 15:17:02 -0700178/* same as in type 2 models */
179static struct sonypi_ioport_list *sonypi_type3_ioport_list =
180 sonypi_type2_ioport_list;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* The set of possible interrupts */
183struct sonypi_irq_list {
184 u16 irq;
185 u16 bits;
186};
187
188static struct sonypi_irq_list sonypi_type1_irq_list[] = {
189 { 11, 0x2 }, /* IRQ 11, GO22=0,GO23=1 in AML */
190 { 10, 0x1 }, /* IRQ 10, GO22=1,GO23=0 in AML */
191 { 5, 0x0 }, /* IRQ 5, GO22=0,GO23=0 in AML */
192 { 0, 0x3 } /* no IRQ, GO22=1,GO23=1 in AML */
193};
194
195static struct sonypi_irq_list sonypi_type2_irq_list[] = {
196 { 11, 0x80 }, /* IRQ 11, 0x80 in SIRQ in AML */
197 { 10, 0x40 }, /* IRQ 10, 0x40 in SIRQ in AML */
198 { 9, 0x20 }, /* IRQ 9, 0x20 in SIRQ in AML */
199 { 6, 0x10 }, /* IRQ 6, 0x10 in SIRQ in AML */
200 { 0, 0x00 } /* no IRQ, 0x00 in SIRQ in AML */
201};
202
Erik Walingd2052c12005-09-06 15:17:02 -0700203/* same as in type2 models */
204static struct sonypi_irq_list *sonypi_type3_irq_list = sonypi_type2_irq_list;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#define SONYPI_CAMERA_BRIGHTNESS 0
207#define SONYPI_CAMERA_CONTRAST 1
208#define SONYPI_CAMERA_HUE 2
209#define SONYPI_CAMERA_COLOR 3
210#define SONYPI_CAMERA_SHARPNESS 4
211
212#define SONYPI_CAMERA_PICTURE 5
213#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
214#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
215#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
216#define SONYPI_CAMERA_MUTE_MASK 0x40
217
218/* the rest don't need a loop until not 0xff */
219#define SONYPI_CAMERA_AGC 6
220#define SONYPI_CAMERA_AGC_MASK 0x30
221#define SONYPI_CAMERA_SHUTTER_MASK 0x7
222
223#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
224#define SONYPI_CAMERA_CONTROL 0x10
225
226#define SONYPI_CAMERA_STATUS 7
227#define SONYPI_CAMERA_STATUS_READY 0x2
228#define SONYPI_CAMERA_STATUS_POSITION 0x4
229
230#define SONYPI_DIRECTION_BACKWARDS 0x4
231
232#define SONYPI_CAMERA_REVISION 8
233#define SONYPI_CAMERA_ROMVERSION 9
234
235/* Event masks */
236#define SONYPI_JOGGER_MASK 0x00000001
237#define SONYPI_CAPTURE_MASK 0x00000002
238#define SONYPI_FNKEY_MASK 0x00000004
239#define SONYPI_BLUETOOTH_MASK 0x00000008
240#define SONYPI_PKEY_MASK 0x00000010
241#define SONYPI_BACK_MASK 0x00000020
242#define SONYPI_HELP_MASK 0x00000040
243#define SONYPI_LID_MASK 0x00000080
244#define SONYPI_ZOOM_MASK 0x00000100
245#define SONYPI_THUMBPHRASE_MASK 0x00000200
246#define SONYPI_MEYE_MASK 0x00000400
247#define SONYPI_MEMORYSTICK_MASK 0x00000800
248#define SONYPI_BATTERY_MASK 0x00001000
Erik Walingd2052c12005-09-06 15:17:02 -0700249#define SONYPI_WIRELESS_MASK 0x00002000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251struct sonypi_event {
252 u8 data;
253 u8 event;
254};
255
256/* The set of possible button release events */
257static struct sonypi_event sonypi_releaseev[] = {
258 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
259 { 0, 0 }
260};
261
262/* The set of possible jogger events */
263static struct sonypi_event sonypi_joggerev[] = {
264 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
265 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
266 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
267 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
268 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
269 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
270 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
271 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
272 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
273 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
274 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
275 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
276 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
277 { 0, 0 }
278};
279
280/* The set of possible capture button events */
281static struct sonypi_event sonypi_captureev[] = {
282 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
283 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
284 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
285 { 0, 0 }
286};
287
288/* The set of possible fnkeys events */
289static struct sonypi_event sonypi_fnkeyev[] = {
290 { 0x10, SONYPI_EVENT_FNKEY_ESC },
291 { 0x11, SONYPI_EVENT_FNKEY_F1 },
292 { 0x12, SONYPI_EVENT_FNKEY_F2 },
293 { 0x13, SONYPI_EVENT_FNKEY_F3 },
294 { 0x14, SONYPI_EVENT_FNKEY_F4 },
295 { 0x15, SONYPI_EVENT_FNKEY_F5 },
296 { 0x16, SONYPI_EVENT_FNKEY_F6 },
297 { 0x17, SONYPI_EVENT_FNKEY_F7 },
298 { 0x18, SONYPI_EVENT_FNKEY_F8 },
299 { 0x19, SONYPI_EVENT_FNKEY_F9 },
300 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
301 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
302 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
303 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
304 { 0x21, SONYPI_EVENT_FNKEY_1 },
305 { 0x22, SONYPI_EVENT_FNKEY_2 },
306 { 0x31, SONYPI_EVENT_FNKEY_D },
307 { 0x32, SONYPI_EVENT_FNKEY_E },
308 { 0x33, SONYPI_EVENT_FNKEY_F },
309 { 0x34, SONYPI_EVENT_FNKEY_S },
310 { 0x35, SONYPI_EVENT_FNKEY_B },
311 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
312 { 0, 0 }
313};
314
315/* The set of possible program key events */
316static struct sonypi_event sonypi_pkeyev[] = {
317 { 0x01, SONYPI_EVENT_PKEY_P1 },
318 { 0x02, SONYPI_EVENT_PKEY_P2 },
319 { 0x04, SONYPI_EVENT_PKEY_P3 },
320 { 0x5c, SONYPI_EVENT_PKEY_P1 },
321 { 0, 0 }
322};
323
324/* The set of possible bluetooth events */
325static struct sonypi_event sonypi_blueev[] = {
326 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
327 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
328 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
329 { 0, 0 }
330};
331
Erik Walingd2052c12005-09-06 15:17:02 -0700332/* The set of possible wireless events */
333static struct sonypi_event sonypi_wlessev[] = {
334 { 0x59, SONYPI_EVENT_WIRELESS_ON },
335 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
336 { 0, 0 }
337};
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/* The set of possible back button events */
340static struct sonypi_event sonypi_backev[] = {
341 { 0x20, SONYPI_EVENT_BACK_PRESSED },
342 { 0, 0 }
343};
344
345/* The set of possible help button events */
346static struct sonypi_event sonypi_helpev[] = {
347 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
348 { 0, 0 }
349};
350
351
352/* The set of possible lid events */
353static struct sonypi_event sonypi_lidev[] = {
354 { 0x51, SONYPI_EVENT_LID_CLOSED },
355 { 0x50, SONYPI_EVENT_LID_OPENED },
356 { 0, 0 }
357};
358
359/* The set of possible zoom events */
360static struct sonypi_event sonypi_zoomev[] = {
361 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
362 { 0, 0 }
363};
364
365/* The set of possible thumbphrase events */
366static struct sonypi_event sonypi_thumbphraseev[] = {
367 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
368 { 0, 0 }
369};
370
371/* The set of possible motioneye camera events */
372static struct sonypi_event sonypi_meyeev[] = {
373 { 0x00, SONYPI_EVENT_MEYE_FACE },
374 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
375 { 0, 0 }
376};
377
378/* The set of possible memorystick events */
379static struct sonypi_event sonypi_memorystickev[] = {
380 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
381 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
382 { 0, 0 }
383};
384
385/* The set of possible battery events */
386static struct sonypi_event sonypi_batteryev[] = {
387 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
388 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
389 { 0, 0 }
390};
391
392static struct sonypi_eventtypes {
393 int model;
394 u8 data;
395 unsigned long mask;
396 struct sonypi_event * events;
397} sonypi_eventtypes[] = {
398 { SONYPI_DEVICE_MODEL_TYPE1, 0, 0xffffffff, sonypi_releaseev },
399 { SONYPI_DEVICE_MODEL_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
400 { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
401 { SONYPI_DEVICE_MODEL_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
402 { SONYPI_DEVICE_MODEL_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
403 { SONYPI_DEVICE_MODEL_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
404 { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
405 { SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
406 { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
407 { SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
408
409 { SONYPI_DEVICE_MODEL_TYPE2, 0, 0xffffffff, sonypi_releaseev },
410 { SONYPI_DEVICE_MODEL_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
411 { SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
412 { SONYPI_DEVICE_MODEL_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
413 { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
414 { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
415 { SONYPI_DEVICE_MODEL_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
416 { SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
418 { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
419 { SONYPI_DEVICE_MODEL_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
420 { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
421 { SONYPI_DEVICE_MODEL_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
422 { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
423
Erik Walingd2052c12005-09-06 15:17:02 -0700424 { SONYPI_DEVICE_MODEL_TYPE3, 0, 0xffffffff, sonypi_releaseev },
425 { SONYPI_DEVICE_MODEL_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
426 { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
427 { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
428 { SONYPI_DEVICE_MODEL_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
429 { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 { 0 }
431};
432
433#define SONYPI_BUF_SIZE 128
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* Correspondance table between sonypi events and input layer events */
436static struct {
437 int sonypiev;
438 int inputev;
439} sonypi_inputkeys[] = {
440 { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
441 { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
442 { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
443 { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
444 { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
445 { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
446 { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
447 { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
448 { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
449 { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
450 { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
451 { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
452 { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
453 { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
454 { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
455 { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
456 { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
457 { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
458 { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
459 { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
460 { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
461 { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
462 { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
463 { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
464 { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
465 { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
466 { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
467 { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
468 { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
469 { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
470 { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
471 { 0, 0 },
472};
473
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500474struct sonypi_keypress {
475 struct input_dev *dev;
476 int key;
477};
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static struct sonypi_device {
480 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 u16 irq;
482 u16 bits;
483 u16 ioport1;
484 u16 ioport2;
485 u16 region_size;
486 u16 evtype_offset;
487 int camera_power;
488 int bluetooth_power;
Matthias Kaehlckec6c60102007-04-24 22:02:35 +0200489 struct mutex lock;
Stefani Seibold45465482009-12-21 14:37:26 -0800490 struct kfifo fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 spinlock_t fifo_lock;
492 wait_queue_head_t fifo_proc_list;
493 struct fasync_struct *fifo_async;
494 int open_count;
495 int model;
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -0500496 struct input_dev *input_jog_dev;
497 struct input_dev *input_key_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct work_struct input_work;
Stefani Seibold45465482009-12-21 14:37:26 -0800499 struct kfifo input_fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 spinlock_t input_fifo_lock;
501} sonypi_device;
502
503#define ITERATIONS_LONG 10000
504#define ITERATIONS_SHORT 10
505
506#define wait_on_command(quiet, command, iterations) { \
507 unsigned int n = iterations; \
508 while (--n && (command)) \
509 udelay(1); \
510 if (!n && (verbose || !quiet)) \
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700511 printk(KERN_WARNING "sonypi command failed at %s : %s (line %d)\n", __FILE__, __func__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514#ifdef CONFIG_ACPI
515#define SONYPI_ACPI_ACTIVE (!acpi_disabled)
516#else
517#define SONYPI_ACPI_ACTIVE 0
518#endif /* CONFIG_ACPI */
519
Ben Collinsb368fae2006-01-08 01:04:24 -0800520#ifdef CONFIG_ACPI
521static struct acpi_device *sonypi_acpi_device;
Bjorn Helgaase4513a52006-03-28 17:04:00 -0500522static int acpi_driver_registered;
Ben Collinsb368fae2006-01-08 01:04:24 -0800523#endif
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static int sonypi_ec_write(u8 addr, u8 value)
526{
Bjorn Helgaas8950d892008-11-05 16:18:03 -0700527#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (SONYPI_ACPI_ACTIVE)
529 return ec_write(addr, value);
530#endif
531 wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
532 outb_p(0x81, SONYPI_CST_IOPORT);
533 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
534 outb_p(addr, SONYPI_DATA_IOPORT);
535 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
536 outb_p(value, SONYPI_DATA_IOPORT);
537 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
538 return 0;
539}
540
541static int sonypi_ec_read(u8 addr, u8 *value)
542{
Bjorn Helgaas8950d892008-11-05 16:18:03 -0700543#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (SONYPI_ACPI_ACTIVE)
545 return ec_read(addr, value);
546#endif
547 wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
548 outb_p(0x80, SONYPI_CST_IOPORT);
549 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
550 outb_p(addr, SONYPI_DATA_IOPORT);
551 wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
552 *value = inb_p(SONYPI_DATA_IOPORT);
553 return 0;
554}
555
556static int ec_read16(u8 addr, u16 *value)
557{
558 u8 val_lb, val_hb;
559 if (sonypi_ec_read(addr, &val_lb))
560 return -1;
561 if (sonypi_ec_read(addr + 1, &val_hb))
562 return -1;
563 *value = val_lb | (val_hb << 8);
564 return 0;
565}
566
567/* Initializes the device - this comes from the AML code in the ACPI bios */
568static void sonypi_type1_srs(void)
569{
570 u32 v;
571
572 pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
573 v = (v & 0xFFFF0000) | ((u32) sonypi_device.ioport1);
574 pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
575
576 pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
577 v = (v & 0xFFF0FFFF) |
578 (((u32) sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
579 pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
580
581 v = inl(SONYPI_IRQ_PORT);
582 v &= ~(((u32) 0x3) << SONYPI_IRQ_SHIFT);
583 v |= (((u32) sonypi_device.bits) << SONYPI_IRQ_SHIFT);
584 outl(v, SONYPI_IRQ_PORT);
585
586 pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
587 v = (v & 0xFF1FFFFF) | 0x00C00000;
588 pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
589}
590
591static void sonypi_type2_srs(void)
592{
593 if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
594 printk(KERN_WARNING "ec_write failed\n");
595 if (sonypi_ec_write(SONYPI_SLOB, sonypi_device.ioport1 & 0x00FF))
596 printk(KERN_WARNING "ec_write failed\n");
597 if (sonypi_ec_write(SONYPI_SIRQ, sonypi_device.bits))
598 printk(KERN_WARNING "ec_write failed\n");
599 udelay(10);
600}
601
Erik Walingd2052c12005-09-06 15:17:02 -0700602static void sonypi_type3_srs(void)
603{
604 u16 v16;
605 u8 v8;
606
607 /* This model type uses the same initialiazation of
608 * the embedded controller as the type2 models. */
609 sonypi_type2_srs();
610
611 /* Initialization of PCI config space of the LPC interface bridge. */
612 v16 = (sonypi_device.ioport1 & 0xFFF0) | 0x01;
613 pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, v16);
614 pci_read_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, &v8);
615 v8 = (v8 & 0xCF) | 0x10;
616 pci_write_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, v8);
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/* Disables the device - this comes from the AML code in the ACPI bios */
620static void sonypi_type1_dis(void)
621{
622 u32 v;
623
624 pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
625 v = v & 0xFF3FFFFF;
626 pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
627
628 v = inl(SONYPI_IRQ_PORT);
629 v |= (0x3 << SONYPI_IRQ_SHIFT);
630 outl(v, SONYPI_IRQ_PORT);
631}
632
633static void sonypi_type2_dis(void)
634{
635 if (sonypi_ec_write(SONYPI_SHIB, 0))
636 printk(KERN_WARNING "ec_write failed\n");
637 if (sonypi_ec_write(SONYPI_SLOB, 0))
638 printk(KERN_WARNING "ec_write failed\n");
639 if (sonypi_ec_write(SONYPI_SIRQ, 0))
640 printk(KERN_WARNING "ec_write failed\n");
641}
642
Erik Walingd2052c12005-09-06 15:17:02 -0700643static void sonypi_type3_dis(void)
644{
645 sonypi_type2_dis();
646 udelay(10);
647 pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, 0);
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650static u8 sonypi_call1(u8 dev)
651{
652 u8 v1, v2;
653
654 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
655 outb(dev, sonypi_device.ioport2);
656 v1 = inb_p(sonypi_device.ioport2);
657 v2 = inb_p(sonypi_device.ioport1);
658 return v2;
659}
660
661static u8 sonypi_call2(u8 dev, u8 fn)
662{
663 u8 v1;
664
665 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
666 outb(dev, sonypi_device.ioport2);
667 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
668 outb(fn, sonypi_device.ioport1);
669 v1 = inb_p(sonypi_device.ioport1);
670 return v1;
671}
672
673static u8 sonypi_call3(u8 dev, u8 fn, u8 v)
674{
675 u8 v1;
676
677 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
678 outb(dev, sonypi_device.ioport2);
679 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
680 outb(fn, sonypi_device.ioport1);
681 wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
682 outb(v, sonypi_device.ioport1);
683 v1 = inb_p(sonypi_device.ioport1);
684 return v1;
685}
686
687#if 0
688/* Get brightness, hue etc. Unreliable... */
689static u8 sonypi_read(u8 fn)
690{
691 u8 v1, v2;
692 int n = 100;
693
694 while (n--) {
695 v1 = sonypi_call2(0x8f, fn);
696 v2 = sonypi_call2(0x8f, fn);
697 if (v1 == v2 && v1 != 0xff)
698 return v1;
699 }
700 return 0xff;
701}
702#endif
703
704/* Set brightness, hue etc */
705static void sonypi_set(u8 fn, u8 v)
706{
707 wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
708}
709
710/* Tests if the camera is ready */
711static int sonypi_camera_ready(void)
712{
713 u8 v;
714
715 v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
716 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
717}
718
719/* Turns the camera off */
720static void sonypi_camera_off(void)
721{
722 sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
723
724 if (!sonypi_device.camera_power)
725 return;
726
727 sonypi_call2(0x91, 0);
728 sonypi_device.camera_power = 0;
729}
730
731/* Turns the camera on */
732static void sonypi_camera_on(void)
733{
734 int i, j;
735
736 if (sonypi_device.camera_power)
737 return;
738
739 for (j = 5; j > 0; j--) {
740
741 while (sonypi_call2(0x91, 0x1))
742 msleep(10);
743 sonypi_call1(0x93);
744
745 for (i = 400; i > 0; i--) {
746 if (sonypi_camera_ready())
747 break;
748 msleep(10);
749 }
750 if (i)
751 break;
752 }
753
754 if (j == 0) {
755 printk(KERN_WARNING "sonypi: failed to power on camera\n");
756 return;
757 }
758
759 sonypi_set(0x10, 0x5a);
760 sonypi_device.camera_power = 1;
761}
762
763/* sets the bluetooth subsystem power state */
764static void sonypi_setbluetoothpower(u8 state)
765{
766 state = !!state;
767
768 if (sonypi_device.bluetooth_power == state)
769 return;
770
771 sonypi_call2(0x96, state);
772 sonypi_call1(0x82);
773 sonypi_device.bluetooth_power = state;
774}
775
David Howellsc4028952006-11-22 14:57:56 +0000776static void input_keyrelease(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500778 struct sonypi_keypress kp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800780 while (kfifo_out_locked(&sonypi_device.input_fifo, (unsigned char *)&kp,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800781 sizeof(kp), &sonypi_device.input_fifo_lock)
782 == sizeof(kp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 msleep(10);
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500784 input_report_key(kp.dev, kp.key, 0);
785 input_sync(kp.dev);
786 }
787}
788
789static void sonypi_report_input_event(u8 event)
790{
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -0500791 struct input_dev *jog_dev = sonypi_device.input_jog_dev;
792 struct input_dev *key_dev = sonypi_device.input_key_dev;
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500793 struct sonypi_keypress kp = { NULL };
794 int i;
795
796 switch (event) {
797 case SONYPI_EVENT_JOGDIAL_UP:
798 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
799 input_report_rel(jog_dev, REL_WHEEL, 1);
800 input_sync(jog_dev);
801 break;
802
803 case SONYPI_EVENT_JOGDIAL_DOWN:
804 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
805 input_report_rel(jog_dev, REL_WHEEL, -1);
806 input_sync(jog_dev);
807 break;
808
809 case SONYPI_EVENT_JOGDIAL_PRESSED:
810 kp.key = BTN_MIDDLE;
811 kp.dev = jog_dev;
812 break;
813
814 case SONYPI_EVENT_FNKEY_RELEASED:
815 /* Nothing, not all VAIOs generate this event */
816 break;
817
818 default:
819 for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
820 if (event == sonypi_inputkeys[i].sonypiev) {
821 kp.dev = key_dev;
822 kp.key = sonypi_inputkeys[i].inputev;
823 break;
824 }
825 break;
826 }
827
828 if (kp.dev) {
829 input_report_key(kp.dev, kp.key, 1);
830 input_sync(kp.dev);
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800831 kfifo_in_locked(&sonypi_device.input_fifo,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800832 (unsigned char *)&kp, sizeof(kp),
833 &sonypi_device.input_fifo_lock);
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500834 schedule_work(&sonypi_device.input_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836}
837
838/* Interrupt handler: some event is available */
David Howells7d12e782006-10-05 14:55:46 +0100839static irqreturn_t sonypi_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 u8 v1, v2, event = 0;
842 int i, j;
843
844 v1 = inb_p(sonypi_device.ioport1);
845 v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
846
847 for (i = 0; sonypi_eventtypes[i].model; i++) {
848 if (sonypi_device.model != sonypi_eventtypes[i].model)
849 continue;
850 if ((v2 & sonypi_eventtypes[i].data) !=
851 sonypi_eventtypes[i].data)
852 continue;
853 if (!(mask & sonypi_eventtypes[i].mask))
854 continue;
855 for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
856 if (v1 == sonypi_eventtypes[i].events[j].data) {
857 event = sonypi_eventtypes[i].events[j].event;
858 goto found;
859 }
860 }
861 }
862
863 if (verbose)
864 printk(KERN_WARNING
865 "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",
866 v1, v2);
867 /* We need to return IRQ_HANDLED here because there *are*
868 * events belonging to the sonypi device we don't know about,
869 * but we still don't want those to pollute the logs... */
870 return IRQ_HANDLED;
871
872found:
873 if (verbose > 1)
874 printk(KERN_INFO
875 "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
876
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -0500877 if (useinput)
878 sonypi_report_input_event(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Ben Collinsb368fae2006-01-08 01:04:24 -0800880#ifdef CONFIG_ACPI
Bjorn Helgaase4513a52006-03-28 17:04:00 -0500881 if (sonypi_acpi_device)
Len Brown14e04fb2007-08-23 15:20:26 -0400882 acpi_bus_generate_proc_event(sonypi_acpi_device, 1, event);
Ben Collinsb368fae2006-01-08 01:04:24 -0800883#endif
884
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800885 kfifo_in_locked(&sonypi_device.fifo, (unsigned char *)&event,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800886 sizeof(event), &sonypi_device.fifo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
888 wake_up_interruptible(&sonypi_device.fifo_proc_list);
889
890 return IRQ_HANDLED;
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static int sonypi_misc_fasync(int fd, struct file *filp, int on)
894{
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700895 return fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898static int sonypi_misc_release(struct inode *inode, struct file *file)
899{
Matthias Kaehlckec6c60102007-04-24 22:02:35 +0200900 mutex_lock(&sonypi_device.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 sonypi_device.open_count--;
Matthias Kaehlckec6c60102007-04-24 22:02:35 +0200902 mutex_unlock(&sonypi_device.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
904}
905
906static int sonypi_misc_open(struct inode *inode, struct file *file)
907{
Matthias Kaehlckec6c60102007-04-24 22:02:35 +0200908 mutex_lock(&sonypi_device.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* Flush input queue on first open */
910 if (!sonypi_device.open_count)
Stefani Seibold45465482009-12-21 14:37:26 -0800911 kfifo_reset(&sonypi_device.fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 sonypi_device.open_count++;
Matthias Kaehlckec6c60102007-04-24 22:02:35 +0200913 mutex_unlock(&sonypi_device.lock);
John Kacur6432e732009-10-18 23:49:49 +0200914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
917
918static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
919 size_t count, loff_t *pos)
920{
921 ssize_t ret;
922 unsigned char c;
923
Stefani Seibold45465482009-12-21 14:37:26 -0800924 if ((kfifo_len(&sonypi_device.fifo) == 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 (file->f_flags & O_NONBLOCK))
926 return -EAGAIN;
927
928 ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
Stefani Seibold45465482009-12-21 14:37:26 -0800929 kfifo_len(&sonypi_device.fifo) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (ret)
931 return ret;
932
933 while (ret < count &&
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800934 (kfifo_out_locked(&sonypi_device.fifo, &c, sizeof(c),
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800935 &sonypi_device.fifo_lock) == sizeof(c))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (put_user(c, buf++))
937 return -EFAULT;
938 ret++;
939 }
940
941 if (ret > 0) {
Josef Sipeka7113a92006-12-08 02:36:55 -0800942 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 inode->i_atime = current_fs_time(inode->i_sb);
944 }
945
946 return ret;
947}
948
949static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
950{
951 poll_wait(file, &sonypi_device.fifo_proc_list, wait);
Stefani Seibold45465482009-12-21 14:37:26 -0800952 if (kfifo_len(&sonypi_device.fifo))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return POLLIN | POLLRDNORM;
954 return 0;
955}
956
John Kacur6432e732009-10-18 23:49:49 +0200957static long sonypi_misc_ioctl(struct file *fp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 unsigned int cmd, unsigned long arg)
959{
John Kacur6432e732009-10-18 23:49:49 +0200960 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 void __user *argp = (void __user *)arg;
962 u8 val8;
963 u16 val16;
964
Matthias Kaehlckec6c60102007-04-24 22:02:35 +0200965 mutex_lock(&sonypi_device.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 switch (cmd) {
967 case SONYPI_IOCGBRT:
968 if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
969 ret = -EIO;
970 break;
971 }
972 if (copy_to_user(argp, &val8, sizeof(val8)))
973 ret = -EFAULT;
974 break;
975 case SONYPI_IOCSBRT:
976 if (copy_from_user(&val8, argp, sizeof(val8))) {
977 ret = -EFAULT;
978 break;
979 }
980 if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
981 ret = -EIO;
982 break;
983 case SONYPI_IOCGBAT1CAP:
984 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
985 ret = -EIO;
986 break;
987 }
988 if (copy_to_user(argp, &val16, sizeof(val16)))
989 ret = -EFAULT;
990 break;
991 case SONYPI_IOCGBAT1REM:
992 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
993 ret = -EIO;
994 break;
995 }
996 if (copy_to_user(argp, &val16, sizeof(val16)))
997 ret = -EFAULT;
998 break;
999 case SONYPI_IOCGBAT2CAP:
1000 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1001 ret = -EIO;
1002 break;
1003 }
1004 if (copy_to_user(argp, &val16, sizeof(val16)))
1005 ret = -EFAULT;
1006 break;
1007 case SONYPI_IOCGBAT2REM:
1008 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1009 ret = -EIO;
1010 break;
1011 }
1012 if (copy_to_user(argp, &val16, sizeof(val16)))
1013 ret = -EFAULT;
1014 break;
1015 case SONYPI_IOCGBATFLAGS:
1016 if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
1017 ret = -EIO;
1018 break;
1019 }
1020 val8 &= 0x07;
1021 if (copy_to_user(argp, &val8, sizeof(val8)))
1022 ret = -EFAULT;
1023 break;
1024 case SONYPI_IOCGBLUE:
1025 val8 = sonypi_device.bluetooth_power;
1026 if (copy_to_user(argp, &val8, sizeof(val8)))
1027 ret = -EFAULT;
1028 break;
1029 case SONYPI_IOCSBLUE:
1030 if (copy_from_user(&val8, argp, sizeof(val8))) {
1031 ret = -EFAULT;
1032 break;
1033 }
1034 sonypi_setbluetoothpower(val8);
1035 break;
1036 /* FAN Controls */
1037 case SONYPI_IOCGFAN:
1038 if (sonypi_ec_read(SONYPI_FAN0_STATUS, &val8)) {
1039 ret = -EIO;
1040 break;
1041 }
Al Viro05559852005-05-04 05:40:02 +01001042 if (copy_to_user(argp, &val8, sizeof(val8)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ret = -EFAULT;
1044 break;
1045 case SONYPI_IOCSFAN:
Al Viro05559852005-05-04 05:40:02 +01001046 if (copy_from_user(&val8, argp, sizeof(val8))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 ret = -EFAULT;
1048 break;
1049 }
1050 if (sonypi_ec_write(SONYPI_FAN0_STATUS, val8))
1051 ret = -EIO;
1052 break;
1053 /* GET Temperature (useful under APM) */
1054 case SONYPI_IOCGTEMP:
1055 if (sonypi_ec_read(SONYPI_TEMP_STATUS, &val8)) {
1056 ret = -EIO;
1057 break;
1058 }
Al Viro05559852005-05-04 05:40:02 +01001059 if (copy_to_user(argp, &val8, sizeof(val8)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 ret = -EFAULT;
1061 break;
1062 default:
1063 ret = -EINVAL;
1064 }
Matthias Kaehlckec6c60102007-04-24 22:02:35 +02001065 mutex_unlock(&sonypi_device.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return ret;
1067}
1068
Arjan van de Ven62322d22006-07-03 00:24:21 -07001069static const struct file_operations sonypi_misc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 .owner = THIS_MODULE,
1071 .read = sonypi_misc_read,
1072 .poll = sonypi_misc_poll,
1073 .open = sonypi_misc_open,
1074 .release = sonypi_misc_release,
1075 .fasync = sonypi_misc_fasync,
John Kacur6432e732009-10-18 23:49:49 +02001076 .unlocked_ioctl = sonypi_misc_ioctl,
1077 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078};
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);
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08001320 error = kfifo_alloc(&sonypi_device.fifo, SONYPI_BUF_SIZE, GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08001321 if (error) {
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001322 printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
Stefani Seibold45465482009-12-21 14:37:26 -08001323 return error;
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001324 }
1325
1326 init_waitqueue_head(&sonypi_device.fifo_proc_list);
Matthias Kaehlckec6c60102007-04-24 22:02:35 +02001327 mutex_init(&sonypi_device.lock);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001328 sonypi_device.bluetooth_power = -1;
1329
1330 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1331 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1332 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
1333 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1334 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1335 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
Arnaud MAZINbf104e62006-04-20 02:43:20 -07001336 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1337 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1338 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001339 else
1340 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
1341
1342 if (pcidev && pci_enable_device(pcidev)) {
1343 printk(KERN_ERR "sonypi: pci_enable_device failed\n");
1344 error = -EIO;
1345 goto err_put_pcidev;
1346 }
1347
1348 sonypi_device.dev = pcidev;
1349
1350 if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
1351 ioport_list = sonypi_type1_ioport_list;
1352 sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
1353 sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
1354 irq_list = sonypi_type1_irq_list;
1355 } else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
1356 ioport_list = sonypi_type2_ioport_list;
1357 sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
1358 sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
1359 irq_list = sonypi_type2_irq_list;
1360 } else {
1361 ioport_list = sonypi_type3_ioport_list;
1362 sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
1363 sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
1364 irq_list = sonypi_type3_irq_list;
1365 }
1366
1367 error = sonypi_setup_ioports(&sonypi_device, ioport_list);
1368 if (error) {
1369 printk(KERN_ERR "sonypi: failed to request ioports\n");
1370 goto err_disable_pcidev;
1371 }
1372
1373 error = sonypi_setup_irq(&sonypi_device, irq_list);
1374 if (error) {
1375 printk(KERN_ERR "sonypi: request_irq failed\n");
1376 goto err_free_ioports;
1377 }
1378
1379 if (minor != -1)
1380 sonypi_misc_device.minor = minor;
1381 error = misc_register(&sonypi_misc_device);
1382 if (error) {
1383 printk(KERN_ERR "sonypi: misc_register failed\n");
1384 goto err_free_irq;
1385 }
1386
1387 sonypi_display_info();
1388
1389 if (useinput) {
1390
Dmitry Torokhovdcf65cd2007-11-21 14:16:16 -05001391 error = sonypi_create_input_devices(dev);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001392 if (error) {
1393 printk(KERN_ERR
1394 "sonypi: failed to create input devices\n");
1395 goto err_miscdev_unregister;
1396 }
1397
1398 spin_lock_init(&sonypi_device.input_fifo_lock);
Stefani Seibold45465482009-12-21 14:37:26 -08001399 error = kfifo_alloc(&sonypi_device.input_fifo, SONYPI_BUF_SIZE,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08001400 GFP_KERNEL);
Stefani Seibold45465482009-12-21 14:37:26 -08001401 if (error) {
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001402 printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001403 goto err_inpdev_unregister;
1404 }
1405
David Howellsc4028952006-11-22 14:57:56 +00001406 INIT_WORK(&sonypi_device.input_work, input_keyrelease);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001407 }
1408
1409 sonypi_enable(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 return 0;
1412
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001413 err_inpdev_unregister:
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001414 input_unregister_device(sonypi_device.input_key_dev);
1415 input_unregister_device(sonypi_device.input_jog_dev);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001416 err_miscdev_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 misc_deregister(&sonypi_misc_device);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001418 err_free_irq:
1419 free_irq(sonypi_device.irq, sonypi_irq);
1420 err_free_ioports:
1421 release_region(sonypi_device.ioport1, sonypi_device.region_size);
1422 err_disable_pcidev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (pcidev)
1424 pci_disable_device(pcidev);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001425 err_put_pcidev:
1426 pci_dev_put(pcidev);
Stefani Seibold45465482009-12-21 14:37:26 -08001427 kfifo_free(&sonypi_device.fifo);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001428
1429 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001432static int __devexit sonypi_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 sonypi_disable();
1435
Dmitry Torokhov3cb93db2007-11-21 14:16:38 -05001436 synchronize_irq(sonypi_device.irq);
Dmitry Torokhovfb2ce3c2005-06-30 00:50:10 -05001437 flush_scheduled_work();
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (useinput) {
Dmitry Torokhovb416f2e2005-09-15 02:01:50 -05001440 input_unregister_device(sonypi_device.input_key_dev);
1441 input_unregister_device(sonypi_device.input_jog_dev);
Stefani Seibold45465482009-12-21 14:37:26 -08001442 kfifo_free(&sonypi_device.input_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001445 misc_deregister(&sonypi_misc_device);
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 free_irq(sonypi_device.irq, sonypi_irq);
1448 release_region(sonypi_device.ioport1, sonypi_device.region_size);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001449
1450 if (sonypi_device.dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 pci_disable_device(sonypi_device.dev);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001452 pci_dev_put(sonypi_device.dev);
1453 }
1454
Stefani Seibold45465482009-12-21 14:37:26 -08001455 kfifo_free(&sonypi_device.fifo);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001456
1457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001460#ifdef CONFIG_PM
1461static int old_camera_power;
1462
1463static int sonypi_suspend(struct platform_device *dev, pm_message_t state)
1464{
1465 old_camera_power = sonypi_device.camera_power;
1466 sonypi_disable();
1467
1468 return 0;
1469}
1470
1471static int sonypi_resume(struct platform_device *dev)
1472{
1473 sonypi_enable(old_camera_power);
1474 return 0;
1475}
1476#else
1477#define sonypi_suspend NULL
1478#define sonypi_resume NULL
1479#endif
1480
1481static void sonypi_shutdown(struct platform_device *dev)
1482{
1483 sonypi_disable();
1484}
1485
1486static struct platform_driver sonypi_driver = {
1487 .driver = {
1488 .name = "sonypi",
1489 .owner = THIS_MODULE,
1490 },
1491 .probe = sonypi_probe,
1492 .remove = __devexit_p(sonypi_remove),
1493 .shutdown = sonypi_shutdown,
1494 .suspend = sonypi_suspend,
1495 .resume = sonypi_resume,
1496};
1497
1498static struct platform_device *sonypi_platform_device;
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500static struct dmi_system_id __initdata sonypi_dmi_table[] = {
1501 {
1502 .ident = "Sony Vaio",
1503 .matches = {
1504 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1505 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1506 },
1507 },
1508 {
1509 .ident = "Sony Vaio",
1510 .matches = {
1511 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1512 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1513 },
1514 },
1515 { }
1516};
1517
1518static int __init sonypi_init(void)
1519{
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001520 int error;
1521
1522 printk(KERN_INFO
1523 "sonypi: Sony Programmable I/O Controller Driver v%s.\n",
1524 SONYPI_DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 if (!dmi_check_system(sonypi_dmi_table))
1527 return -ENODEV;
1528
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001529 error = platform_driver_register(&sonypi_driver);
1530 if (error)
1531 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001533 sonypi_platform_device = platform_device_alloc("sonypi", -1);
1534 if (!sonypi_platform_device) {
1535 error = -ENOMEM;
1536 goto err_driver_unregister;
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001539 error = platform_device_add(sonypi_platform_device);
1540 if (error)
1541 goto err_free_device;
1542
Ben Collinsb368fae2006-01-08 01:04:24 -08001543#ifdef CONFIG_ACPI
Bjorn Helgaase4513a52006-03-28 17:04:00 -05001544 if (acpi_bus_register_driver(&sonypi_acpi_driver) >= 0)
1545 acpi_driver_registered = 1;
Ben Collinsb368fae2006-01-08 01:04:24 -08001546#endif
1547
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001548 return 0;
1549
1550 err_free_device:
1551 platform_device_put(sonypi_platform_device);
1552 err_driver_unregister:
1553 platform_driver_unregister(&sonypi_driver);
1554 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557static void __exit sonypi_exit(void)
1558{
Ben Collinsb368fae2006-01-08 01:04:24 -08001559#ifdef CONFIG_ACPI
Bjorn Helgaase4513a52006-03-28 17:04:00 -05001560 if (acpi_driver_registered)
Ben Collinsb368fae2006-01-08 01:04:24 -08001561 acpi_bus_unregister_driver(&sonypi_acpi_driver);
1562#endif
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001563 platform_device_unregister(sonypi_platform_device);
Russell King3ae5eae2005-11-09 22:32:44 +00001564 platform_driver_unregister(&sonypi_driver);
Dmitry Torokhov44f06102006-01-08 01:04:22 -08001565 printk(KERN_INFO "sonypi: removed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568module_init(sonypi_init);
1569module_exit(sonypi_exit);