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