include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6-block.git] / drivers / platform / x86 / sony-laptop.c
CommitLineData
7f09c432 1/*
33a04454 2 * ACPI Sony Notebook Control Driver (SNC and SPIC)
7f09c432
SP
3 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
37418347 5 * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
7f09c432
SP
6 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
33a04454 10 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
db955170 17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
33a04454 18 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
7f09c432
SP
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 *
43 */
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/init.h>
49#include <linux/types.h>
50f62afb 50#include <linux/backlight.h>
ed3aa4b7 51#include <linux/platform_device.h>
50f62afb 52#include <linux/err.h>
33a04454 53#include <linux/dmi.h>
54#include <linux/pci.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57#include <linux/input.h>
58#include <linux/kfifo.h>
59#include <linux/workqueue.h>
60#include <linux/acpi.h>
5a0e3ad6 61#include <linux/slab.h>
7f09c432
SP
62#include <acpi/acpi_drivers.h>
63#include <acpi/acpi_bus.h>
64#include <asm/uaccess.h>
33a04454 65#include <linux/sonypi.h>
1ce82c14 66#include <linux/sony-laptop.h>
6cc056bc 67#include <linux/rfkill.h>
a64e62a0 68#ifdef CONFIG_SONYPI_COMPAT
7b153f36 69#include <linux/poll.h>
70#include <linux/miscdevice.h>
71#endif
7f09c432 72
33a04454 73#define DRV_PFX "sony-laptop: "
b9a218b7 74#define dprintk(msg...) do { \
f6119b02 75 if (debug) printk(KERN_WARNING DRV_PFX msg); \
b9a218b7 76} while (0)
77
425ef5d7 78#define SONY_LAPTOP_DRIVER_VERSION "0.6"
33a04454 79
80#define SONY_NC_CLASS "sony-nc"
59b19106 81#define SONY_NC_HID "SNY5001"
f6119b02 82#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
50f62afb 83
33a04454 84#define SONY_PIC_CLASS "sony-pic"
85#define SONY_PIC_HID "SNY6001"
f6119b02 86#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
7f09c432 87
ed3aa4b7 88MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
33a04454 89MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
7f09c432 90MODULE_LICENSE("GPL");
33a04454 91MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
7f09c432
SP
92
93static int debug;
94module_param(debug, int, 0);
95MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
a02d1c1d 96 "the development of this driver");
7f09c432 97
33a04454 98static int no_spic; /* = 0 */
99module_param(no_spic, int, 0444);
100MODULE_PARM_DESC(no_spic,
101 "set this if you don't want to enable the SPIC device");
102
103static int compat; /* = 0 */
104module_param(compat, int, 0444);
105MODULE_PARM_DESC(compat,
7b153f36 106 "set this if you want to enable backward compatibility mode");
33a04454 107
108static unsigned long mask = 0xffffffff;
109module_param(mask, ulong, 0644);
110MODULE_PARM_DESC(mask,
111 "set this to the mask of event you want to enable (see doc)");
112
5f3d2898 113static int camera; /* = 0 */
114module_param(camera, int, 0444);
115MODULE_PARM_DESC(camera,
116 "set this to 1 to enable Motion Eye camera controls "
117 "(only use it if you have a C1VE or C1VN model)");
118
a64e62a0 119#ifdef CONFIG_SONYPI_COMPAT
7b153f36 120static int minor = -1;
121module_param(minor, int, 0);
122MODULE_PARM_DESC(minor,
123 "minor number of the misc device for the SPIC compatibility code, "
124 "default is -1 (automatic)");
125#endif
126
6cc056bc
MG
127enum sony_nc_rfkill {
128 SONY_WIFI,
129 SONY_BLUETOOTH,
130 SONY_WWAN,
131 SONY_WIMAX,
19d337df 132 N_SONY_RFKILL,
6cc056bc
MG
133};
134
d5a664a3 135static int sony_rfkill_handle;
19d337df
JB
136static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
137static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
6cc056bc
MG
138static void sony_nc_rfkill_update(void);
139
1549ee6f 140/*********** Input Devices ***********/
141
142#define SONY_LAPTOP_BUF_SIZE 128
143struct sony_laptop_input_s {
144 atomic_t users;
145 struct input_dev *jog_dev;
146 struct input_dev *key_dev;
45465487 147 struct kfifo fifo;
1549ee6f 148 spinlock_t fifo_lock;
cffdde99 149 struct timer_list release_key_timer;
1549ee6f 150};
6cc056bc 151
1549ee6f 152static struct sony_laptop_input_s sony_laptop_input = {
153 .users = ATOMIC_INIT(0),
154};
155
156struct sony_laptop_keypress {
157 struct input_dev *dev;
158 int key;
159};
160
bc57f865
MD
161/* Correspondance table between sonypi events
162 * and input layer indexes in the keymap
163 */
164static int sony_laptop_input_index[] = {
3eb8749a
MD
165 -1, /* 0 no event */
166 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
167 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
168 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
169 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
170 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
171 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
172 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
173 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
174 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
175 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
176 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
177 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
178 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
179 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
180 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
181 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
182 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
183 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
184 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
185 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
186 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
187 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
188 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
189 17, /* 24 SONYPI_EVENT_FNKEY_1 */
190 18, /* 25 SONYPI_EVENT_FNKEY_2 */
191 19, /* 26 SONYPI_EVENT_FNKEY_D */
192 20, /* 27 SONYPI_EVENT_FNKEY_E */
193 21, /* 28 SONYPI_EVENT_FNKEY_F */
194 22, /* 29 SONYPI_EVENT_FNKEY_S */
195 23, /* 30 SONYPI_EVENT_FNKEY_B */
196 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
197 25, /* 32 SONYPI_EVENT_PKEY_P1 */
198 26, /* 33 SONYPI_EVENT_PKEY_P2 */
199 27, /* 34 SONYPI_EVENT_PKEY_P3 */
200 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
201 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
202 -1, /* 37 SONYPI_EVENT_LID_OPENED */
203 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
204 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
205 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
206 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
207 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
208 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
209 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
210 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
211 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
212 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
213 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
214 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
215 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
216 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
217 43, /* 52 SONYPI_EVENT_MEYE_FACE */
218 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
219 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
220 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
221 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
222 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
223 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
224 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
225 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
226 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
227 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
228 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
9b57896e 229 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
45c7942b
MG
230 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
231 53, /* 66 SONYPI_EVENT_PKEY_P4 */
232 54, /* 67 SONYPI_EVENT_PKEY_P5 */
233 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
1cae7103
HJ
234 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
235 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
236 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
4f924ba5 237 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
bc57f865
MD
238};
239
240static int sony_laptop_input_keycode_map[] = {
241 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
242 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
243 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
244 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
245 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
246 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
247 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
248 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
249 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
250 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
251 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
252 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
253 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
254 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
255 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
256 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
257 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
258 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
259 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
260 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
261 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
262 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
263 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
264 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
265 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
266 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
267 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
268 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
269 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
270 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
271 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
272 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
273 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
274 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
275 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
276 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
277 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
278 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
279 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
280 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
281 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
282 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
283 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
284 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
285 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
286 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
287 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
288 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
289 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
3eb8749a 290 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
9b57896e 291 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
45c7942b
MG
292 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
293 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
294 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
295 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
296 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
1cae7103
HJ
297 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
298 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
4f924ba5 299 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
1549ee6f 300};
301
302/* release buttons after a short delay if pressed */
cffdde99 303static void do_sony_laptop_release_key(unsigned long unused)
1549ee6f 304{
305 struct sony_laptop_keypress kp;
9593bd07 306 unsigned long flags;
1549ee6f 307
9593bd07
DT
308 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
309
310 if (kfifo_out(&sony_laptop_input.fifo,
311 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
1549ee6f 312 input_report_key(kp.dev, kp.key, 0);
313 input_sync(kp.dev);
314 }
9593bd07
DT
315
316 /* If there is something in the fifo schedule next release. */
317 if (kfifo_len(&sony_laptop_input.fifo) != 0)
cffdde99
DT
318 mod_timer(&sony_laptop_input.release_key_timer,
319 jiffies + msecs_to_jiffies(10));
9593bd07
DT
320
321 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
1549ee6f 322}
1549ee6f 323
3a4fa0a2 324/* forward event to the input subsystem */
1549ee6f 325static void sony_laptop_report_input_event(u8 event)
326{
327 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
328 struct input_dev *key_dev = sony_laptop_input.key_dev;
329 struct sony_laptop_keypress kp = { NULL };
1549ee6f 330
a83021a2
AT
331 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
332 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
1549ee6f 333 /* Nothing, not all VAIOs generate this event */
334 return;
335 }
336
337 /* report events */
338 switch (event) {
339 /* jog_dev events */
340 case SONYPI_EVENT_JOGDIAL_UP:
341 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
342 input_report_rel(jog_dev, REL_WHEEL, 1);
343 input_sync(jog_dev);
344 return;
345
346 case SONYPI_EVENT_JOGDIAL_DOWN:
347 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
348 input_report_rel(jog_dev, REL_WHEEL, -1);
349 input_sync(jog_dev);
350 return;
351
352 /* key_dev events */
353 case SONYPI_EVENT_JOGDIAL_PRESSED:
354 kp.key = BTN_MIDDLE;
355 kp.dev = jog_dev;
356 break;
357
358 default:
d399d130 359 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
bc57f865
MD
360 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
361 break;
362 }
363 if (sony_laptop_input_index[event] != -1) {
364 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
365 if (kp.key != KEY_UNKNOWN)
1549ee6f 366 kp.dev = key_dev;
bc57f865 367 }
1549ee6f 368 break;
369 }
370
371 if (kp.dev) {
372 input_report_key(kp.dev, kp.key, 1);
bc57f865
MD
373 /* we emit the scancode so we can always remap the key */
374 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
1549ee6f 375 input_sync(kp.dev);
1549ee6f 376
9593bd07
DT
377 /* schedule key release */
378 kfifo_in_locked(&sony_laptop_input.fifo,
379 (unsigned char *)&kp, sizeof(kp),
380 &sony_laptop_input.fifo_lock);
cffdde99
DT
381 mod_timer(&sony_laptop_input.release_key_timer,
382 jiffies + msecs_to_jiffies(10));
1549ee6f 383 } else
384 dprintk("unknown input event %.2x\n", event);
385}
386
2e4d242c 387static int sony_laptop_setup_input(struct acpi_device *acpi_device)
1549ee6f 388{
389 struct input_dev *jog_dev;
390 struct input_dev *key_dev;
391 int i;
392 int error;
393
394 /* don't run again if already initialized */
395 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
396 return 0;
397
398 /* kfifo */
399 spin_lock_init(&sony_laptop_input.fifo_lock);
9593bd07
DT
400 error = kfifo_alloc(&sony_laptop_input.fifo,
401 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
45465487 402 if (error) {
1549ee6f 403 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
1549ee6f 404 goto err_dec_users;
405 }
406
cffdde99
DT
407 setup_timer(&sony_laptop_input.release_key_timer,
408 do_sony_laptop_release_key, 0);
1549ee6f 409
410 /* input keys */
411 key_dev = input_allocate_device();
412 if (!key_dev) {
413 error = -ENOMEM;
9593bd07 414 goto err_free_kfifo;
1549ee6f 415 }
416
417 key_dev->name = "Sony Vaio Keys";
418 key_dev->id.bustype = BUS_ISA;
419 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
2e4d242c 420 key_dev->dev.parent = &acpi_device->dev;
1549ee6f 421
422 /* Initialize the Input Drivers: special keys */
c45bc9d6
DT
423 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
424
425 __set_bit(EV_KEY, key_dev->evbit);
bc57f865
MD
426 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
427 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
428 key_dev->keycode = &sony_laptop_input_keycode_map;
c45bc9d6
DT
429 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
430 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
431 __clear_bit(KEY_RESERVED, key_dev->keybit);
1549ee6f 432
433 error = input_register_device(key_dev);
434 if (error)
435 goto err_free_keydev;
436
437 sony_laptop_input.key_dev = key_dev;
438
439 /* jogdial */
440 jog_dev = input_allocate_device();
441 if (!jog_dev) {
442 error = -ENOMEM;
443 goto err_unregister_keydev;
444 }
445
446 jog_dev->name = "Sony Vaio Jogdial";
447 jog_dev->id.bustype = BUS_ISA;
448 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
2e4d242c 449 key_dev->dev.parent = &acpi_device->dev;
1549ee6f 450
c45bc9d6
DT
451 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
452 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
1549ee6f 453
454 error = input_register_device(jog_dev);
455 if (error)
456 goto err_free_jogdev;
457
458 sony_laptop_input.jog_dev = jog_dev;
459
460 return 0;
461
462err_free_jogdev:
463 input_free_device(jog_dev);
464
465err_unregister_keydev:
466 input_unregister_device(key_dev);
467 /* to avoid kref underflow below at input_free_device */
468 key_dev = NULL;
469
470err_free_keydev:
471 input_free_device(key_dev);
472
1549ee6f 473err_free_kfifo:
45465487 474 kfifo_free(&sony_laptop_input.fifo);
1549ee6f 475
476err_dec_users:
477 atomic_dec(&sony_laptop_input.users);
478 return error;
479}
480
481static void sony_laptop_remove_input(void)
482{
9593bd07
DT
483 struct sony_laptop_keypress kp = { NULL };
484
485 /* Cleanup only after the last user has gone */
1549ee6f 486 if (!atomic_dec_and_test(&sony_laptop_input.users))
487 return;
488
cffdde99 489 del_timer_sync(&sony_laptop_input.release_key_timer);
9593bd07
DT
490
491 /*
492 * Generate key-up events for remaining keys. Note that we don't
493 * need locking since nobody is adding new events to the kfifo.
494 */
495 while (kfifo_out(&sony_laptop_input.fifo,
496 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
497 input_report_key(kp.dev, kp.key, 0);
498 input_sync(kp.dev);
499 }
1549ee6f 500
501 /* destroy input devs */
502 input_unregister_device(sony_laptop_input.key_dev);
503 sony_laptop_input.key_dev = NULL;
504
505 if (sony_laptop_input.jog_dev) {
506 input_unregister_device(sony_laptop_input.jog_dev);
507 sony_laptop_input.jog_dev = NULL;
508 }
509
45465487 510 kfifo_free(&sony_laptop_input.fifo);
1549ee6f 511}
512
56b8756b 513/*********** Platform Device ***********/
514
515static atomic_t sony_pf_users = ATOMIC_INIT(0);
516static struct platform_driver sony_pf_driver = {
517 .driver = {
518 .name = "sony-laptop",
519 .owner = THIS_MODULE,
520 }
521};
522static struct platform_device *sony_pf_device;
523
524static int sony_pf_add(void)
525{
526 int ret = 0;
527
528 /* don't run again if already initialized */
529 if (atomic_add_return(1, &sony_pf_users) > 1)
530 return 0;
531
532 ret = platform_driver_register(&sony_pf_driver);
533 if (ret)
534 goto out;
535
536 sony_pf_device = platform_device_alloc("sony-laptop", -1);
537 if (!sony_pf_device) {
538 ret = -ENOMEM;
539 goto out_platform_registered;
540 }
541
542 ret = platform_device_add(sony_pf_device);
543 if (ret)
544 goto out_platform_alloced;
545
546 return 0;
547
548 out_platform_alloced:
549 platform_device_put(sony_pf_device);
550 sony_pf_device = NULL;
551 out_platform_registered:
552 platform_driver_unregister(&sony_pf_driver);
553 out:
554 atomic_dec(&sony_pf_users);
555 return ret;
556}
557
558static void sony_pf_remove(void)
559{
560 /* deregister only after the last user has gone */
561 if (!atomic_dec_and_test(&sony_pf_users))
562 return;
563
564 platform_device_del(sony_pf_device);
565 platform_device_put(sony_pf_device);
566 platform_driver_unregister(&sony_pf_driver);
567}
568
569/*********** SNC (SNY5001) Device ***********/
570
33a04454 571/* the device uses 1-based values, while the backlight subsystem uses
572 0-based values */
573#define SONY_MAX_BRIGHTNESS 8
574
575#define SNC_VALIDATE_IN 0
576#define SNC_VALIDATE_OUT 1
577
59b19106 578static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
a02d1c1d 579 char *);
59b19106 580static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
a02d1c1d 581 const char *, size_t);
156c221b
MD
582static int boolean_validate(const int, const int);
583static int brightness_default_validate(const int, const int);
584
59b19106 585struct sony_nc_value {
a02d1c1d
LB
586 char *name; /* name of the entry */
587 char **acpiget; /* names of the ACPI get function */
588 char **acpiset; /* names of the ACPI set function */
156c221b 589 int (*validate)(const int, const int); /* input/output validation */
a02d1c1d
LB
590 int value; /* current setting */
591 int valid; /* Has ever been set */
592 int debug; /* active only in debug mode ? */
593 struct device_attribute devattr; /* sysfs atribute */
ed3aa4b7
MD
594};
595
59b19106 596#define SNC_HANDLE_NAMES(_name, _values...) \
ed3aa4b7
MD
597 static char *snc_##_name[] = { _values, NULL }
598
59b19106 599#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
ed3aa4b7
MD
600 { \
601 .name = __stringify(_name), \
602 .acpiget = _getters, \
603 .acpiset = _setters, \
156c221b 604 .validate = _validate, \
ed3aa4b7 605 .debug = _debug, \
59b19106 606 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
7f09c432 607 }
ed3aa4b7 608
59b19106 609#define SNC_HANDLE_NULL { .name = NULL }
ed3aa4b7 610
59b19106 611SNC_HANDLE_NAMES(fnkey_get, "GHKE");
ed3aa4b7 612
59b19106 613SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
614SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
ed3aa4b7 615
59b19106 616SNC_HANDLE_NAMES(cdpower_get, "GCDP");
617SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
ed3aa4b7 618
59b19106 619SNC_HANDLE_NAMES(audiopower_get, "GAZP");
620SNC_HANDLE_NAMES(audiopower_set, "AZPW");
ed3aa4b7 621
59b19106 622SNC_HANDLE_NAMES(lanpower_get, "GLNP");
623SNC_HANDLE_NAMES(lanpower_set, "LNPW");
ed3aa4b7 624
044847e0
MD
625SNC_HANDLE_NAMES(lidstate_get, "GLID");
626
627SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
628SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
629
630SNC_HANDLE_NAMES(gainbass_get, "GMGB");
631SNC_HANDLE_NAMES(gainbass_set, "CMGB");
632
59b19106 633SNC_HANDLE_NAMES(PID_get, "GPID");
ed3aa4b7 634
59b19106 635SNC_HANDLE_NAMES(CTR_get, "GCTR");
636SNC_HANDLE_NAMES(CTR_set, "SCTR");
ed3aa4b7 637
59b19106 638SNC_HANDLE_NAMES(PCR_get, "GPCR");
639SNC_HANDLE_NAMES(PCR_set, "SPCR");
ed3aa4b7 640
59b19106 641SNC_HANDLE_NAMES(CMI_get, "GCMI");
642SNC_HANDLE_NAMES(CMI_set, "SCMI");
ed3aa4b7 643
59b19106 644static struct sony_nc_value sony_nc_values[] = {
645 SNC_HANDLE(brightness_default, snc_brightness_def_get,
156c221b 646 snc_brightness_def_set, brightness_default_validate, 0),
59b19106 647 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
648 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
649 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
156c221b 650 boolean_validate, 0),
59b19106 651 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
156c221b 652 boolean_validate, 1),
044847e0
MD
653 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
654 boolean_validate, 0),
655 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
656 boolean_validate, 0),
657 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
658 boolean_validate, 0),
ed3aa4b7 659 /* unknown methods */
59b19106 660 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
661 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
662 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
663 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
664 SNC_HANDLE_NULL
7f09c432
SP
665};
666
59b19106 667static acpi_handle sony_nc_acpi_handle;
668static struct acpi_device *sony_nc_acpi_device = NULL;
ed3aa4b7 669
d78865cd
MD
670/*
671 * acpi_evaluate_object wrappers
672 */
7f09c432
SP
673static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
674{
675 struct acpi_buffer output;
676 union acpi_object out_obj;
677 acpi_status status;
678
679 output.length = sizeof(out_obj);
680 output.pointer = &out_obj;
681
682 status = acpi_evaluate_object(handle, name, NULL, &output);
683 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
684 *result = out_obj.integer.value;
685 return 0;
686 }
687
f6119b02 688 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
7f09c432
SP
689
690 return -1;
691}
692
693static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
694 int *result)
695{
696 struct acpi_object_list params;
697 union acpi_object in_obj;
698 struct acpi_buffer output;
699 union acpi_object out_obj;
700 acpi_status status;
701
702 params.count = 1;
703 params.pointer = &in_obj;
704 in_obj.type = ACPI_TYPE_INTEGER;
705 in_obj.integer.value = value;
706
707 output.length = sizeof(out_obj);
708 output.pointer = &out_obj;
709
710 status = acpi_evaluate_object(handle, name, &params, &output);
711 if (status == AE_OK) {
712 if (result != NULL) {
713 if (out_obj.type != ACPI_TYPE_INTEGER) {
f6119b02 714 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
7f09c432
SP
715 "return type\n");
716 return -1;
717 }
718 *result = out_obj.integer.value;
719 }
720 return 0;
721 }
722
f6119b02 723 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
7f09c432
SP
724
725 return -1;
726}
727
badf26f0
MG
728static int sony_find_snc_handle(int handle)
729{
730 int i;
731 int result;
732
733 for (i = 0x20; i < 0x30; i++) {
734 acpi_callsetfunc(sony_nc_acpi_handle, "SN00", i, &result);
735 if (result == handle)
736 return i-0x20;
737 }
738
739 return -1;
740}
741
742static int sony_call_snc_handle(int handle, int argument, int *result)
743{
744 int offset = sony_find_snc_handle(handle);
745
746 if (offset < 0)
747 return -1;
748
749 return acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
750 result);
751}
752
156c221b 753/*
59b19106 754 * sony_nc_values input/output validate functions
156c221b
MD
755 */
756
757/* brightness_default_validate:
758 *
759 * manipulate input output values to keep consistency with the
760 * backlight framework for which brightness values are 0-based.
761 */
762static int brightness_default_validate(const int direction, const int value)
763{
764 switch (direction) {
765 case SNC_VALIDATE_OUT:
766 return value - 1;
767 case SNC_VALIDATE_IN:
768 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
769 return value + 1;
770 }
771 return -EINVAL;
772}
773
774/* boolean_validate:
775 *
776 * on input validate boolean values 0/1, on output just pass the
777 * received value.
778 */
779static int boolean_validate(const int direction, const int value)
780{
781 if (direction == SNC_VALIDATE_IN) {
782 if (value != 0 && value != 1)
783 return -EINVAL;
784 }
785 return value;
786}
787
ed3aa4b7 788/*
59b19106 789 * Sysfs show/store common to all sony_nc_values
ed3aa4b7 790 */
59b19106 791static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
a02d1c1d 792 char *buffer)
7f09c432 793{
7f09c432 794 int value;
59b19106 795 struct sony_nc_value *item =
796 container_of(attr, struct sony_nc_value, devattr);
7f09c432 797
ed3aa4b7 798 if (!*item->acpiget)
7f09c432
SP
799 return -EIO;
800
59b19106 801 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
7f09c432
SP
802 return -EIO;
803
156c221b
MD
804 if (item->validate)
805 value = item->validate(SNC_VALIDATE_OUT, value);
806
ed3aa4b7 807 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
7f09c432
SP
808}
809
59b19106 810static ssize_t sony_nc_sysfs_store(struct device *dev,
a02d1c1d
LB
811 struct device_attribute *attr,
812 const char *buffer, size_t count)
7f09c432 813{
7f09c432 814 int value;
59b19106 815 struct sony_nc_value *item =
816 container_of(attr, struct sony_nc_value, devattr);
7f09c432
SP
817
818 if (!item->acpiset)
819 return -EIO;
820
ed3aa4b7
MD
821 if (count > 31)
822 return -EINVAL;
823
824 value = simple_strtoul(buffer, NULL, 10);
7f09c432 825
156c221b
MD
826 if (item->validate)
827 value = item->validate(SNC_VALIDATE_IN, value);
828
829 if (value < 0)
830 return value;
7f09c432 831
59b19106 832 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
7f09c432 833 return -EIO;
3f4f461f
AM
834 item->value = value;
835 item->valid = 1;
7f09c432
SP
836 return count;
837}
838
ed3aa4b7 839
d78865cd
MD
840/*
841 * Backlight device
842 */
843static int sony_backlight_update_status(struct backlight_device *bd)
3f4f461f 844{
59b19106 845 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
321709c5 846 bd->props.brightness + 1, NULL);
d78865cd 847}
3f4f461f 848
d78865cd
MD
849static int sony_backlight_get_brightness(struct backlight_device *bd)
850{
851 int value;
3f4f461f 852
59b19106 853 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
d78865cd
MD
854 return 0;
855 /* brightness levels are 1-based, while backlight ones are 0-based */
856 return value - 1;
3f4f461f
AM
857}
858
d78865cd 859static struct backlight_device *sony_backlight_device;
321709c5 860static struct backlight_ops sony_backlight_ops = {
a02d1c1d
LB
861 .update_status = sony_backlight_update_status,
862 .get_brightness = sony_backlight_get_brightness,
d78865cd
MD
863};
864
6315fd1c
MD
865/*
866 * New SNC-only Vaios event mapping to driver known keys
867 */
868struct sony_nc_event {
869 u8 data;
870 u8 event;
871};
872
45c7942b 873static struct sony_nc_event sony_100_events[] = {
9b57896e
MG
874 { 0x90, SONYPI_EVENT_PKEY_P1 },
875 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
6479efb6 876 { 0x91, SONYPI_EVENT_PKEY_P2 },
9b57896e 877 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
6315fd1c
MD
878 { 0x81, SONYPI_EVENT_FNKEY_F1 },
879 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
f5acf5e8
AV
880 { 0x82, SONYPI_EVENT_FNKEY_F2 },
881 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
882 { 0x83, SONYPI_EVENT_FNKEY_F3 },
883 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
884 { 0x84, SONYPI_EVENT_FNKEY_F4 },
885 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
6315fd1c
MD
886 { 0x85, SONYPI_EVENT_FNKEY_F5 },
887 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
888 { 0x86, SONYPI_EVENT_FNKEY_F6 },
889 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
890 { 0x87, SONYPI_EVENT_FNKEY_F7 },
891 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
9b57896e
MG
892 { 0x89, SONYPI_EVENT_FNKEY_F9 },
893 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
6315fd1c
MD
894 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
895 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
896 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
897 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
9b57896e
MG
898 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
899 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
4f924ba5
MD
900 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
901 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
6315fd1c
MD
902 { 0, 0 },
903};
904
45c7942b
MG
905static struct sony_nc_event sony_127_events[] = {
906 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
907 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
908 { 0x82, SONYPI_EVENT_PKEY_P1 },
909 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
910 { 0x83, SONYPI_EVENT_PKEY_P2 },
911 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
912 { 0x84, SONYPI_EVENT_PKEY_P3 },
913 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
914 { 0x85, SONYPI_EVENT_PKEY_P4 },
915 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
916 { 0x86, SONYPI_EVENT_PKEY_P5 },
917 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
45c7942b
MG
918 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
919 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
920 { 0, 0 },
921};
922
d78865cd
MD
923/*
924 * ACPI callbacks
925 */
8037d6e6 926static void sony_nc_notify(struct acpi_device *device, u32 event)
7f09c432 927{
6315fd1c 928 u32 ev = event;
6315fd1c 929
9b57896e
MG
930 if (ev >= 0x90) {
931 /* New-style event */
16dd55f3 932 int result;
45c7942b 933 int key_handle = 0;
9b57896e
MG
934 ev -= 0x90;
935
45c7942b
MG
936 if (sony_find_snc_handle(0x100) == ev)
937 key_handle = 0x100;
938 if (sony_find_snc_handle(0x127) == ev)
939 key_handle = 0x127;
940
6479efb6 941 if (key_handle) {
45c7942b 942 struct sony_nc_event *key_event;
9b57896e 943
16dd55f3 944 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
8037d6e6 945 dprintk("sony_nc_notify, unable to decode"
45c7942b
MG
946 " event 0x%.2x 0x%.2x\n", key_handle,
947 ev);
16dd55f3
MW
948 /* restore the original event */
949 ev = event;
950 } else {
9b57896e
MG
951 ev = result & 0xFF;
952
16dd55f3
MW
953 if (key_handle == 0x100)
954 key_event = sony_100_events;
955 else
956 key_event = sony_127_events;
45c7942b 957
16dd55f3
MW
958 for (; key_event->data; key_event++) {
959 if (key_event->data == ev) {
960 ev = key_event->event;
961 break;
962 }
9b57896e 963 }
6315fd1c 964
16dd55f3
MW
965 if (!key_event->data)
966 printk(KERN_INFO DRV_PFX
967 "Unknown event: 0x%x 0x%x\n",
968 key_handle,
969 ev);
970 else
971 sony_laptop_report_input_event(ev);
45c7942b 972 }
d5a664a3 973 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
6cc056bc
MG
974 sony_nc_rfkill_update();
975 return;
9b57896e 976 }
16dd55f3
MW
977 } else
978 sony_laptop_report_input_event(ev);
badf26f0 979
8037d6e6 980 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
14e04fb3 981 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
7f09c432
SP
982}
983
984static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
985 void *context, void **return_value)
986{
30823736 987 struct acpi_device_info *info;
7f09c432 988
15b8dd53 989 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
30823736
LM
990 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n",
991 (char *)&info->name, info->param_count);
992
15b8dd53 993 kfree(info);
30823736 994 }
7f09c432
SP
995
996 return AE_OK;
997}
998
d78865cd
MD
999/*
1000 * ACPI device
1001 */
badf26f0
MG
1002static int sony_nc_function_setup(struct acpi_device *device)
1003{
1004 int result;
1005
1006 /* Enable all events */
1007 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1008
1009 /* Setup hotkeys */
1010 sony_call_snc_handle(0x0100, 0, &result);
1011 sony_call_snc_handle(0x0101, 0, &result);
1012 sony_call_snc_handle(0x0102, 0x100, &result);
560e84ac 1013 sony_call_snc_handle(0x0127, 0, &result);
badf26f0
MG
1014
1015 return 0;
1016}
1017
59b19106 1018static int sony_nc_resume(struct acpi_device *device)
d78865cd 1019{
59b19106 1020 struct sony_nc_value *item;
badf26f0 1021 acpi_handle handle;
d78865cd 1022
59b19106 1023 for (item = sony_nc_values; item->name; item++) {
d78865cd
MD
1024 int ret;
1025
1026 if (!item->valid)
1027 continue;
59b19106 1028 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
a02d1c1d 1029 item->value, NULL);
d78865cd 1030 if (ret < 0) {
6e574195 1031 printk("%s: %d\n", __func__, ret);
d78865cd
MD
1032 break;
1033 }
1034 }
6315fd1c 1035
82734bfc
MG
1036 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1037 &handle))) {
1038 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1039 dprintk("ECON Method failed\n");
1040 }
1041
badf26f0
MG
1042 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1043 &handle))) {
1044 dprintk("Doing SNC setup\n");
1045 sony_nc_function_setup(device);
1046 }
1047
e84a02ba
MD
1048 /* set the last requested brightness level */
1049 if (sony_backlight_device &&
c35d4b35 1050 sony_backlight_update_status(sony_backlight_device) < 0)
0ef9cff1 1051 printk(KERN_WARNING DRV_PFX "unable to restore brightness level\n");
e84a02ba 1052
a0d97d6c
AJ
1053 /* re-read rfkill state */
1054 sony_nc_rfkill_update();
1055
d78865cd
MD
1056 return 0;
1057}
1058
6cc056bc
MG
1059static void sony_nc_rfkill_cleanup(void)
1060{
1061 int i;
1062
19d337df
JB
1063 for (i = 0; i < N_SONY_RFKILL; i++) {
1064 if (sony_rfkill_devices[i]) {
6cc056bc 1065 rfkill_unregister(sony_rfkill_devices[i]);
19d337df
JB
1066 rfkill_destroy(sony_rfkill_devices[i]);
1067 }
6cc056bc
MG
1068 }
1069}
1070
19d337df 1071static int sony_nc_rfkill_set(void *data, bool blocked)
6cc056bc
MG
1072{
1073 int result;
1074 int argument = sony_rfkill_address[(long) data] + 0x100;
1075
19d337df 1076 if (!blocked)
6cc056bc
MG
1077 argument |= 0xff0000;
1078
d5a664a3 1079 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
6cc056bc
MG
1080}
1081
19d337df
JB
1082static const struct rfkill_ops sony_rfkill_ops = {
1083 .set_block = sony_nc_rfkill_set,
1084};
6cc056bc 1085
19d337df
JB
1086static int sony_nc_setup_rfkill(struct acpi_device *device,
1087 enum sony_nc_rfkill nc_type)
6cc056bc
MG
1088{
1089 int err = 0;
19d337df
JB
1090 struct rfkill *rfk;
1091 enum rfkill_type type;
1092 const char *name;
50fab076
AJ
1093 int result;
1094 bool hwblock;
19d337df
JB
1095
1096 switch (nc_type) {
1097 case SONY_WIFI:
1098 type = RFKILL_TYPE_WLAN;
1099 name = "sony-wifi";
1100 break;
1101 case SONY_BLUETOOTH:
1102 type = RFKILL_TYPE_BLUETOOTH;
1103 name = "sony-bluetooth";
1104 break;
1105 case SONY_WWAN:
1106 type = RFKILL_TYPE_WWAN;
1107 name = "sony-wwan";
1108 break;
1109 case SONY_WIMAX:
1110 type = RFKILL_TYPE_WIMAX;
1111 name = "sony-wimax";
1112 break;
1113 default:
1114 return -EINVAL;
53005a0a 1115 }
6cc056bc 1116
19d337df
JB
1117 rfk = rfkill_alloc(name, &device->dev, type,
1118 &sony_rfkill_ops, (void *)nc_type);
1119 if (!rfk)
1120 return -ENOMEM;
6cc056bc 1121
d5a664a3 1122 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
50fab076
AJ
1123 hwblock = !(result & 0x1);
1124 rfkill_set_hw_state(rfk, hwblock);
1125
19d337df
JB
1126 err = rfkill_register(rfk);
1127 if (err) {
1128 rfkill_destroy(rfk);
1129 return err;
53005a0a 1130 }
19d337df 1131 sony_rfkill_devices[nc_type] = rfk;
6cc056bc
MG
1132 return err;
1133}
1134
19d337df 1135static void sony_nc_rfkill_update()
6cc056bc 1136{
19d337df
JB
1137 enum sony_nc_rfkill i;
1138 int result;
1139 bool hwblock;
6cc056bc 1140
d5a664a3 1141 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
19d337df 1142 hwblock = !(result & 0x1);
6cc056bc 1143
19d337df
JB
1144 for (i = 0; i < N_SONY_RFKILL; i++) {
1145 int argument = sony_rfkill_address[i];
6cc056bc 1146
19d337df
JB
1147 if (!sony_rfkill_devices[i])
1148 continue;
1149
1150 if (hwblock) {
e1f8a19e
JB
1151 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1152 /* we already know we're blocked */
1153 }
19d337df 1154 continue;
6cc056bc 1155 }
19d337df 1156
d5a664a3 1157 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
19d337df
JB
1158 rfkill_set_states(sony_rfkill_devices[i],
1159 !(result & 0xf), false);
6cc056bc
MG
1160 }
1161}
1162
528809c3 1163static void sony_nc_rfkill_setup(struct acpi_device *device)
6cc056bc 1164{
528809c3
MD
1165 int offset;
1166 u8 dev_code, i;
1167 acpi_status status;
1168 struct acpi_object_list params;
1169 union acpi_object in_obj;
1170 union acpi_object *device_enum;
1171 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6cc056bc 1172
528809c3
MD
1173 offset = sony_find_snc_handle(0x124);
1174 if (offset == -1) {
1175 offset = sony_find_snc_handle(0x135);
1176 if (offset == -1)
1177 return;
d5a664a3
MD
1178 else
1179 sony_rfkill_handle = 0x135;
1180 } else
1181 sony_rfkill_handle = 0x124;
528809c3
MD
1182 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
1183
1184 /* need to read the whole buffer returned by the acpi call to SN06
1185 * here otherwise we may miss some features
1186 */
1187 params.count = 1;
1188 params.pointer = &in_obj;
1189 in_obj.type = ACPI_TYPE_INTEGER;
1190 in_obj.integer.value = offset;
1191 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1192 &buffer);
1193 if (ACPI_FAILURE(status)) {
1194 dprintk("Radio device enumeration failed\n");
1195 return;
1196 }
6cc056bc 1197
528809c3
MD
1198 device_enum = (union acpi_object *) buffer.pointer;
1199 if (!device_enum || device_enum->type != ACPI_TYPE_BUFFER) {
1200 printk(KERN_ERR "Invalid SN06 return object 0x%.2x\n",
1201 device_enum->type);
1202 goto out_no_enum;
6cc056bc
MG
1203 }
1204
528809c3
MD
1205 /* the buffer is filled with magic numbers describing the devices
1206 * available, 0xff terminates the enumeration
1207 */
c14973f9
DT
1208 for (i = 0; i < device_enum->buffer.length; i++) {
1209
1210 dev_code = *(device_enum->buffer.pointer + i);
1211 if (dev_code == 0xff)
1212 break;
1213
528809c3 1214 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
6cc056bc 1215
528809c3
MD
1216 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1217 sony_nc_setup_rfkill(device, SONY_WIFI);
1218
1219 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1220 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1221
1222 if ((0xf0 & dev_code) == 0x20 &&
1223 !sony_rfkill_devices[SONY_WWAN])
1224 sony_nc_setup_rfkill(device, SONY_WWAN);
1225
1226 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1227 sony_nc_setup_rfkill(device, SONY_WIMAX);
1228 }
1229
1230out_no_enum:
1231 kfree(buffer.pointer);
1232 return;
6cc056bc
MG
1233}
1234
59b19106 1235static int sony_nc_add(struct acpi_device *device)
7f09c432
SP
1236{
1237 acpi_status status;
8607c673 1238 int result = 0;
50f62afb 1239 acpi_handle handle;
56b8756b 1240 struct sony_nc_value *item;
7f09c432 1241
f6119b02 1242 printk(KERN_INFO DRV_PFX "%s v%s.\n",
1243 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
1244
59b19106 1245 sony_nc_acpi_device = device;
33a04454 1246 strcpy(acpi_device_class(device), "sony/hotkey");
c561162f 1247
59b19106 1248 sony_nc_acpi_handle = device->handle;
7f09c432 1249
b25b732a
MD
1250 /* read device status */
1251 result = acpi_bus_get_status(device);
1252 /* bail IFF the above call was successful and the device is not present */
1253 if (!result && !device->status.present) {
1254 dprintk("Device not present\n");
1255 result = -ENODEV;
1256 goto outwalk;
1257 }
1258
7f09c432 1259 if (debug) {
59b19106 1260 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
2263576c 1261 1, sony_walk_callback, NULL, NULL, NULL);
7f09c432 1262 if (ACPI_FAILURE(status)) {
f6119b02 1263 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
7f09c432
SP
1264 result = -ENODEV;
1265 goto outwalk;
1266 }
c561162f 1267 }
7f09c432 1268
82734bfc
MG
1269 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1270 &handle))) {
1271 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1272 dprintk("ECON Method failed\n");
1273 }
1274
badf26f0
MG
1275 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1276 &handle))) {
1277 dprintk("Doing SNC setup\n");
1278 sony_nc_function_setup(device);
6cc056bc 1279 sony_nc_rfkill_setup(device);
badf26f0
MG
1280 }
1281
1549ee6f 1282 /* setup input devices and helper fifo */
2e4d242c 1283 result = sony_laptop_setup_input(device);
1549ee6f 1284 if (result) {
1285 printk(KERN_ERR DRV_PFX
ff07a53a 1286 "Unable to create input devices.\n");
1549ee6f 1287 goto outwalk;
1288 }
1289
38cfc148 1290 if (acpi_video_backlight_support()) {
3fedd90f 1291 printk(KERN_INFO DRV_PFX "brightness ignored, must be "
540b8bb9
TR
1292 "controlled by ACPI video driver\n");
1293 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1294 &handle))) {
a19a6ee6
MG
1295 struct backlight_properties props;
1296 memset(&props, 0, sizeof(struct backlight_properties));
1297 props.max_brightness = SONY_MAX_BRIGHTNESS - 1;
50f62afb 1298 sony_backlight_device = backlight_device_register("sony", NULL,
a02d1c1d 1299 NULL,
a19a6ee6
MG
1300 &sony_backlight_ops,
1301 &props);
7df03b82 1302
a02d1c1d 1303 if (IS_ERR(sony_backlight_device)) {
f6119b02 1304 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
7df03b82 1305 sony_backlight_device = NULL;
321709c5
RP
1306 } else {
1307 sony_backlight_device->props.brightness =
a02d1c1d
LB
1308 sony_backlight_get_brightness
1309 (sony_backlight_device);
321709c5
RP
1310 }
1311
50f62afb 1312 }
7f09c432 1313
49a11dea 1314 result = sony_pf_add();
1315 if (result)
ed3aa4b7 1316 goto outbacklight;
7f09c432 1317
56b8756b 1318 /* create sony_pf sysfs attributes related to the SNC device */
1319 for (item = sony_nc_values; item->name; ++item) {
1320
1321 if (!debug && item->debug)
1322 continue;
1323
1324 /* find the available acpiget as described in the DSDT */
1325 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1326 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1327 *item->acpiget,
1328 &handle))) {
b9a218b7 1329 dprintk("Found %s getter: %s\n",
1330 item->name, *item->acpiget);
56b8756b 1331 item->devattr.attr.mode |= S_IRUGO;
1332 break;
1333 }
1334 }
1335
1336 /* find the available acpiset as described in the DSDT */
1337 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1338 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1339 *item->acpiset,
1340 &handle))) {
b9a218b7 1341 dprintk("Found %s setter: %s\n",
1342 item->name, *item->acpiset);
56b8756b 1343 item->devattr.attr.mode |= S_IWUSR;
1344 break;
1345 }
1346 }
1347
1348 if (item->devattr.attr.mode != 0) {
1349 result =
1350 device_create_file(&sony_pf_device->dev,
1351 &item->devattr);
1352 if (result)
1353 goto out_sysfs;
1354 }
1355 }
1356
7f09c432
SP
1357 return 0;
1358
56b8756b 1359 out_sysfs:
1360 for (item = sony_nc_values; item->name; ++item) {
1361 device_remove_file(&sony_pf_device->dev, &item->devattr);
1362 }
1363 sony_pf_remove();
1549ee6f 1364
a02d1c1d 1365 outbacklight:
7df03b82
MD
1366 if (sony_backlight_device)
1367 backlight_device_unregister(sony_backlight_device);
1368
1549ee6f 1369 sony_laptop_remove_input();
1370
a02d1c1d 1371 outwalk:
6cc056bc 1372 sony_nc_rfkill_cleanup();
7f09c432
SP
1373 return result;
1374}
1375
59b19106 1376static int sony_nc_remove(struct acpi_device *device, int type)
7f09c432 1377{
56b8756b 1378 struct sony_nc_value *item;
7f09c432 1379
50f62afb
AG
1380 if (sony_backlight_device)
1381 backlight_device_unregister(sony_backlight_device);
1382
59b19106 1383 sony_nc_acpi_device = NULL;
c561162f 1384
56b8756b 1385 for (item = sony_nc_values; item->name; ++item) {
1386 device_remove_file(&sony_pf_device->dev, &item->devattr);
1387 }
1388
1389 sony_pf_remove();
1549ee6f 1390 sony_laptop_remove_input();
6cc056bc 1391 sony_nc_rfkill_cleanup();
f6119b02 1392 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
7f09c432
SP
1393
1394 return 0;
1395}
1396
1ba90e3a
TR
1397static const struct acpi_device_id sony_device_ids[] = {
1398 {SONY_NC_HID, 0},
1399 {SONY_PIC_HID, 0},
1400 {"", 0},
1401};
1402MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1403
1404static const struct acpi_device_id sony_nc_device_ids[] = {
1405 {SONY_NC_HID, 0},
1406 {"", 0},
1407};
1408
59b19106 1409static struct acpi_driver sony_nc_driver = {
1410 .name = SONY_NC_DRIVER_NAME,
1411 .class = SONY_NC_CLASS,
1ba90e3a 1412 .ids = sony_nc_device_ids,
33a04454 1413 .owner = THIS_MODULE,
a02d1c1d 1414 .ops = {
59b19106 1415 .add = sony_nc_add,
1416 .remove = sony_nc_remove,
1417 .resume = sony_nc_resume,
8037d6e6 1418 .notify = sony_nc_notify,
a02d1c1d 1419 },
3f4f461f
AM
1420};
1421
33a04454 1422/*********** SPIC (SNY6001) Device ***********/
1423
1424#define SONYPI_DEVICE_TYPE1 0x00000001
1425#define SONYPI_DEVICE_TYPE2 0x00000002
1426#define SONYPI_DEVICE_TYPE3 0x00000004
3eb8749a 1427#define SONYPI_DEVICE_TYPE4 0x00000008
33a04454 1428
22a17780
MD
1429#define SONYPI_TYPE1_OFFSET 0x04
1430#define SONYPI_TYPE2_OFFSET 0x12
1431#define SONYPI_TYPE3_OFFSET 0x12
33a04454 1432
33a04454 1433struct sony_pic_ioport {
fd1caaed
MD
1434 struct acpi_resource_io io1;
1435 struct acpi_resource_io io2;
33a04454 1436 struct list_head list;
1437};
1438
1439struct sony_pic_irq {
1440 struct acpi_resource_irq irq;
1441 struct list_head list;
1442};
1443
de920430
MD
1444struct sonypi_eventtypes {
1445 u8 data;
1446 unsigned long mask;
1447 struct sonypi_event *events;
1448};
1449
31df7144
MD
1450struct sony_pic_dev {
1451 struct acpi_device *acpi_dev;
1452 struct sony_pic_irq *cur_irq;
1453 struct sony_pic_ioport *cur_ioport;
1454 struct list_head interrupts;
1455 struct list_head ioports;
1456 struct mutex lock;
1457 struct sonypi_eventtypes *event_types;
1458 int (*handle_irq)(const u8, const u8);
de920430 1459 int model;
de920430 1460 u16 evport_offset;
31df7144
MD
1461 u8 camera_power;
1462 u8 bluetooth_power;
1463 u8 wwan_power;
33a04454 1464};
1465
1466static struct sony_pic_dev spic_dev = {
1467 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1468 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1469};
1470
5e6f9725
AJ
1471static int spic_drv_registered;
1472
33a04454 1473/* Event masks */
1474#define SONYPI_JOGGER_MASK 0x00000001
1475#define SONYPI_CAPTURE_MASK 0x00000002
1476#define SONYPI_FNKEY_MASK 0x00000004
1477#define SONYPI_BLUETOOTH_MASK 0x00000008
1478#define SONYPI_PKEY_MASK 0x00000010
1479#define SONYPI_BACK_MASK 0x00000020
1480#define SONYPI_HELP_MASK 0x00000040
1481#define SONYPI_LID_MASK 0x00000080
1482#define SONYPI_ZOOM_MASK 0x00000100
1483#define SONYPI_THUMBPHRASE_MASK 0x00000200
1484#define SONYPI_MEYE_MASK 0x00000400
1485#define SONYPI_MEMORYSTICK_MASK 0x00000800
1486#define SONYPI_BATTERY_MASK 0x00001000
1487#define SONYPI_WIRELESS_MASK 0x00002000
1488
1489struct sonypi_event {
1490 u8 data;
1491 u8 event;
1492};
1493
1494/* The set of possible button release events */
1495static struct sonypi_event sonypi_releaseev[] = {
1496 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1497 { 0, 0 }
1498};
1499
1500/* The set of possible jogger events */
1501static struct sonypi_event sonypi_joggerev[] = {
1502 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1503 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1504 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1505 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1506 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1507 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1508 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1509 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1510 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1511 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1512 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1513 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1514 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1515 { 0, 0 }
1516};
1517
1518/* The set of possible capture button events */
1519static struct sonypi_event sonypi_captureev[] = {
1520 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1521 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
3eb8749a 1522 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
33a04454 1523 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1524 { 0, 0 }
1525};
1526
1527/* The set of possible fnkeys events */
1528static struct sonypi_event sonypi_fnkeyev[] = {
1529 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1530 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1531 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1532 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1533 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1534 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1535 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1536 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1537 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1538 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1539 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1540 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1541 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1542 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1543 { 0x21, SONYPI_EVENT_FNKEY_1 },
1544 { 0x22, SONYPI_EVENT_FNKEY_2 },
1545 { 0x31, SONYPI_EVENT_FNKEY_D },
1546 { 0x32, SONYPI_EVENT_FNKEY_E },
1547 { 0x33, SONYPI_EVENT_FNKEY_F },
1548 { 0x34, SONYPI_EVENT_FNKEY_S },
1549 { 0x35, SONYPI_EVENT_FNKEY_B },
1550 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1551 { 0, 0 }
1552};
1553
1554/* The set of possible program key events */
1555static struct sonypi_event sonypi_pkeyev[] = {
1556 { 0x01, SONYPI_EVENT_PKEY_P1 },
1557 { 0x02, SONYPI_EVENT_PKEY_P2 },
1558 { 0x04, SONYPI_EVENT_PKEY_P3 },
1cae7103 1559 { 0x20, SONYPI_EVENT_PKEY_P1 },
33a04454 1560 { 0, 0 }
1561};
1562
1563/* The set of possible bluetooth events */
1564static struct sonypi_event sonypi_blueev[] = {
1565 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1566 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1567 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1568 { 0, 0 }
1569};
1570
1571/* The set of possible wireless events */
1572static struct sonypi_event sonypi_wlessev[] = {
1573 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1574 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1575 { 0, 0 }
1576};
1577
1578/* The set of possible back button events */
1579static struct sonypi_event sonypi_backev[] = {
1580 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1581 { 0, 0 }
1582};
1583
1584/* The set of possible help button events */
1585static struct sonypi_event sonypi_helpev[] = {
1586 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1587 { 0, 0 }
1588};
1589
1590
1591/* The set of possible lid events */
1592static struct sonypi_event sonypi_lidev[] = {
1593 { 0x51, SONYPI_EVENT_LID_CLOSED },
1594 { 0x50, SONYPI_EVENT_LID_OPENED },
1595 { 0, 0 }
1596};
1597
1598/* The set of possible zoom events */
1599static struct sonypi_event sonypi_zoomev[] = {
1600 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
3eb8749a
MD
1601 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1602 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
1cae7103 1603 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
33a04454 1604 { 0, 0 }
1605};
1606
1607/* The set of possible thumbphrase events */
1608static struct sonypi_event sonypi_thumbphraseev[] = {
1609 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1610 { 0, 0 }
1611};
1612
1613/* The set of possible motioneye camera events */
1614static struct sonypi_event sonypi_meyeev[] = {
1615 { 0x00, SONYPI_EVENT_MEYE_FACE },
1616 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1617 { 0, 0 }
1618};
1619
1620/* The set of possible memorystick events */
1621static struct sonypi_event sonypi_memorystickev[] = {
1622 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1623 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1624 { 0, 0 }
1625};
1626
1627/* The set of possible battery events */
1628static struct sonypi_event sonypi_batteryev[] = {
1629 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1630 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1631 { 0, 0 }
1632};
1633
1cae7103
HJ
1634/* The set of possible volume events */
1635static struct sonypi_event sonypi_volumeev[] = {
1636 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1637 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1638 { 0, 0 }
1639};
1640
1641/* The set of possible brightness events */
1642static struct sonypi_event sonypi_brightnessev[] = {
1643 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1644 { 0, 0 }
1645};
1646
de920430
MD
1647static struct sonypi_eventtypes type1_events[] = {
1648 { 0, 0xffffffff, sonypi_releaseev },
1649 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1650 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1651 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1652 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1653 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1654 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1655 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1656 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1657 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1658 { 0 },
1659};
1660static struct sonypi_eventtypes type2_events[] = {
1661 { 0, 0xffffffff, sonypi_releaseev },
1662 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1663 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1664 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1665 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1666 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1667 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1668 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1669 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1670 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1671 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1672 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1673 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1674 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1675 { 0 },
1676};
1677static struct sonypi_eventtypes type3_events[] = {
1678 { 0, 0xffffffff, sonypi_releaseev },
1679 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1680 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1681 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1682 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1683 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
3eb8749a
MD
1684 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1685 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1686 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
1cae7103
HJ
1687 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
1688 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
3eb8749a 1689 { 0 },
de920430
MD
1690};
1691
3eb8749a 1692/* low level spic calls */
33a04454 1693#define ITERATIONS_LONG 10000
1694#define ITERATIONS_SHORT 10
1695#define wait_on_command(command, iterations) { \
1696 unsigned int n = iterations; \
1697 while (--n && (command)) \
1698 udelay(1); \
1699 if (!n) \
1700 dprintk("command failed at %s : %s (line %d)\n", \
6e574195 1701 __FILE__, __func__, __LINE__); \
33a04454 1702}
1703
1704static u8 sony_pic_call1(u8 dev)
1705{
1706 u8 v1, v2;
1707
fd1caaed 1708 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
33a04454 1709 ITERATIONS_LONG);
fd1caaed
MD
1710 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1711 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1712 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
75a1f9ce 1713 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
33a04454 1714 return v2;
1715}
1716
1717static u8 sony_pic_call2(u8 dev, u8 fn)
1718{
1719 u8 v1;
1720
fd1caaed 1721 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
33a04454 1722 ITERATIONS_LONG);
fd1caaed
MD
1723 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1724 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
33a04454 1725 ITERATIONS_LONG);
fd1caaed
MD
1726 outb(fn, spic_dev.cur_ioport->io1.minimum);
1727 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
75a1f9ce 1728 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
33a04454 1729 return v1;
1730}
1731
49a11dea 1732static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1733{
1734 u8 v1;
1735
fd1caaed
MD
1736 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1737 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1738 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1739 outb(fn, spic_dev.cur_ioport->io1.minimum);
1740 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1741 outb(v, spic_dev.cur_ioport->io1.minimum);
1742 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
75a1f9ce
MD
1743 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1744 dev, fn, v, v1);
49a11dea 1745 return v1;
1746}
1747
3eb8749a
MD
1748/*
1749 * minidrivers for SPIC models
1750 */
e93c8a68 1751static int type3_handle_irq(const u8 data_mask, const u8 ev)
3eb8749a
MD
1752{
1753 /*
1754 * 0x31 could mean we have to take some extra action and wait for
e93c8a68 1755 * the next irq for some Type3 models, it will generate a new
3eb8749a
MD
1756 * irq and we can read new data from the device:
1757 * - 0x5c and 0x5f requires 0xA0
1758 * - 0x61 requires 0xB3
1759 */
1760 if (data_mask == 0x31) {
1761 if (ev == 0x5c || ev == 0x5f)
1762 sony_pic_call1(0xA0);
1763 else if (ev == 0x61)
1764 sony_pic_call1(0xB3);
1765 return 0;
1766 }
1767 return 1;
1768}
1769
3eb8749a
MD
1770static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1771{
1772 struct pci_dev *pcidev;
1773
1774 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1775 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1776 if (pcidev) {
31df7144
MD
1777 dev->model = SONYPI_DEVICE_TYPE1;
1778 dev->evport_offset = SONYPI_TYPE1_OFFSET;
1779 dev->event_types = type1_events;
3eb8749a
MD
1780 goto out;
1781 }
1782
1783 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1784 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1785 if (pcidev) {
31df7144
MD
1786 dev->model = SONYPI_DEVICE_TYPE2;
1787 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1788 dev->event_types = type2_events;
3eb8749a
MD
1789 goto out;
1790 }
1791
1792 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1793 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1794 if (pcidev) {
31df7144
MD
1795 dev->model = SONYPI_DEVICE_TYPE3;
1796 dev->handle_irq = type3_handle_irq;
1797 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1798 dev->event_types = type3_events;
3eb8749a
MD
1799 goto out;
1800 }
1801
1802 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1803 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1804 if (pcidev) {
31df7144
MD
1805 dev->model = SONYPI_DEVICE_TYPE3;
1806 dev->handle_irq = type3_handle_irq;
1807 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1808 dev->event_types = type3_events;
3eb8749a
MD
1809 goto out;
1810 }
1811
d5b02695
IM
1812 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1813 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
1814 if (pcidev) {
31df7144
MD
1815 dev->model = SONYPI_DEVICE_TYPE3;
1816 dev->handle_irq = type3_handle_irq;
1817 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1818 dev->event_types = type3_events;
d5b02695
IM
1819 goto out;
1820 }
1821
3eb8749a 1822 /* default */
31df7144
MD
1823 dev->model = SONYPI_DEVICE_TYPE2;
1824 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1825 dev->event_types = type2_events;
3eb8749a
MD
1826
1827out:
1828 if (pcidev)
1829 pci_dev_put(pcidev);
1830
1831 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
31df7144
MD
1832 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
1833 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
3eb8749a
MD
1834}
1835
49a11dea 1836/* camera tests and poweron/poweroff */
1837#define SONYPI_CAMERA_PICTURE 5
49a11dea 1838#define SONYPI_CAMERA_CONTROL 0x10
e364632e 1839
1840#define SONYPI_CAMERA_BRIGHTNESS 0
1841#define SONYPI_CAMERA_CONTRAST 1
1842#define SONYPI_CAMERA_HUE 2
1843#define SONYPI_CAMERA_COLOR 3
1844#define SONYPI_CAMERA_SHARPNESS 4
1845
1846#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1847#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1848#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1849#define SONYPI_CAMERA_MUTE_MASK 0x40
1850
1851/* the rest don't need a loop until not 0xff */
1852#define SONYPI_CAMERA_AGC 6
1853#define SONYPI_CAMERA_AGC_MASK 0x30
1854#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1855
1856#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1857#define SONYPI_CAMERA_CONTROL 0x10
1858
1859#define SONYPI_CAMERA_STATUS 7
1860#define SONYPI_CAMERA_STATUS_READY 0x2
1861#define SONYPI_CAMERA_STATUS_POSITION 0x4
1862
1863#define SONYPI_DIRECTION_BACKWARDS 0x4
1864
1865#define SONYPI_CAMERA_REVISION 8
1866#define SONYPI_CAMERA_ROMVERSION 9
49a11dea 1867
9f9f0761 1868static int __sony_pic_camera_ready(void)
49a11dea 1869{
1870 u8 v;
1871
1872 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1873 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1874}
1875
e364632e 1876static int __sony_pic_camera_off(void)
49a11dea 1877{
5f3d2898 1878 if (!camera) {
1879 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1880 return -ENODEV;
1881 }
1882
49a11dea 1883 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1884 SONYPI_CAMERA_MUTE_MASK),
1885 ITERATIONS_SHORT);
1886
5f3d2898 1887 if (spic_dev.camera_power) {
1888 sony_pic_call2(0x91, 0);
1889 spic_dev.camera_power = 0;
1890 }
1891 return 0;
49a11dea 1892}
1893
e364632e 1894static int __sony_pic_camera_on(void)
49a11dea 1895{
5f3d2898 1896 int i, j, x;
1897
1898 if (!camera) {
1899 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1900 return -ENODEV;
1901 }
49a11dea 1902
1903 if (spic_dev.camera_power)
e364632e 1904 return 0;
49a11dea 1905
1906 for (j = 5; j > 0; j--) {
1907
5f3d2898 1908 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
49a11dea 1909 msleep(10);
1910 sony_pic_call1(0x93);
1911
1912 for (i = 400; i > 0; i--) {
9f9f0761 1913 if (__sony_pic_camera_ready())
49a11dea 1914 break;
1915 msleep(10);
1916 }
1917 if (i)
1918 break;
1919 }
1920
1921 if (j == 0) {
5f3d2898 1922 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
e364632e 1923 return -ENODEV;
49a11dea 1924 }
1925
1926 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1927 0x5a),
1928 ITERATIONS_SHORT);
1929
1930 spic_dev.camera_power = 1;
5f3d2898 1931 return 0;
49a11dea 1932}
1933
e364632e 1934/* External camera command (exported to the motion eye v4l driver) */
1935int sony_pic_camera_command(int command, u8 value)
1936{
1937 if (!camera)
1938 return -EIO;
1939
1940 mutex_lock(&spic_dev.lock);
1941
1942 switch (command) {
1ce82c14 1943 case SONY_PIC_COMMAND_SETCAMERA:
e364632e 1944 if (value)
1945 __sony_pic_camera_on();
1946 else
1947 __sony_pic_camera_off();
1948 break;
1ce82c14 1949 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
e364632e 1950 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1951 ITERATIONS_SHORT);
1952 break;
1ce82c14 1953 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
e364632e 1954 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1955 ITERATIONS_SHORT);
1956 break;
1ce82c14 1957 case SONY_PIC_COMMAND_SETCAMERAHUE:
e364632e 1958 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1959 ITERATIONS_SHORT);
1960 break;
1ce82c14 1961 case SONY_PIC_COMMAND_SETCAMERACOLOR:
e364632e 1962 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1963 ITERATIONS_SHORT);
1964 break;
1ce82c14 1965 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
e364632e 1966 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1967 ITERATIONS_SHORT);
1968 break;
1ce82c14 1969 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
e364632e 1970 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1971 ITERATIONS_SHORT);
1972 break;
1ce82c14 1973 case SONY_PIC_COMMAND_SETCAMERAAGC:
e364632e 1974 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1975 ITERATIONS_SHORT);
1976 break;
1977 default:
1978 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1979 command);
1980 break;
1981 }
1982 mutex_unlock(&spic_dev.lock);
1983 return 0;
1984}
1985EXPORT_SYMBOL(sony_pic_camera_command);
1986
9476cdfa 1987/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
c9f1e6f6 1988static void __sony_pic_set_wwanpower(u8 state)
9476cdfa 1989{
1990 state = !!state;
c9f1e6f6 1991 if (spic_dev.wwan_power == state)
9476cdfa 1992 return;
9476cdfa 1993 sony_pic_call2(0xB0, state);
3ad1b761 1994 sony_pic_call1(0x82);
9476cdfa 1995 spic_dev.wwan_power = state;
9476cdfa 1996}
1997
1998static ssize_t sony_pic_wwanpower_store(struct device *dev,
1999 struct device_attribute *attr,
2000 const char *buffer, size_t count)
2001{
2002 unsigned long value;
2003 if (count > 31)
2004 return -EINVAL;
2005
2006 value = simple_strtoul(buffer, NULL, 10);
c9f1e6f6
MD
2007 mutex_lock(&spic_dev.lock);
2008 __sony_pic_set_wwanpower(value);
2009 mutex_unlock(&spic_dev.lock);
9476cdfa 2010
2011 return count;
2012}
2013
2014static ssize_t sony_pic_wwanpower_show(struct device *dev,
2015 struct device_attribute *attr, char *buffer)
2016{
2017 ssize_t count;
2018 mutex_lock(&spic_dev.lock);
2019 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2020 mutex_unlock(&spic_dev.lock);
2021 return count;
2022}
2023
49a11dea 2024/* bluetooth subsystem power state */
9f9f0761 2025static void __sony_pic_set_bluetoothpower(u8 state)
49a11dea 2026{
2027 state = !!state;
2028 if (spic_dev.bluetooth_power == state)
2029 return;
2030 sony_pic_call2(0x96, state);
2031 sony_pic_call1(0x82);
2032 spic_dev.bluetooth_power = state;
2033}
2034
2035static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2036 struct device_attribute *attr,
2037 const char *buffer, size_t count)
2038{
2039 unsigned long value;
2040 if (count > 31)
2041 return -EINVAL;
2042
2043 value = simple_strtoul(buffer, NULL, 10);
9f9f0761 2044 mutex_lock(&spic_dev.lock);
2045 __sony_pic_set_bluetoothpower(value);
2046 mutex_unlock(&spic_dev.lock);
49a11dea 2047
2048 return count;
2049}
2050
2051static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2052 struct device_attribute *attr, char *buffer)
2053{
9f9f0761 2054 ssize_t count = 0;
2055 mutex_lock(&spic_dev.lock);
2056 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2057 mutex_unlock(&spic_dev.lock);
2058 return count;
49a11dea 2059}
2060
2061/* fan speed */
2062/* FAN0 information (reverse engineered from ACPI tables) */
2063#define SONY_PIC_FAN0_STATUS 0x93
7b153f36 2064static int sony_pic_set_fanspeed(unsigned long value)
2065{
2066 return ec_write(SONY_PIC_FAN0_STATUS, value);
2067}
2068
2069static int sony_pic_get_fanspeed(u8 *value)
2070{
2071 return ec_read(SONY_PIC_FAN0_STATUS, value);
2072}
2073
49a11dea 2074static ssize_t sony_pic_fanspeed_store(struct device *dev,
2075 struct device_attribute *attr,
2076 const char *buffer, size_t count)
2077{
2078 unsigned long value;
2079 if (count > 31)
2080 return -EINVAL;
2081
2082 value = simple_strtoul(buffer, NULL, 10);
7b153f36 2083 if (sony_pic_set_fanspeed(value))
49a11dea 2084 return -EIO;
2085
2086 return count;
2087}
2088
2089static ssize_t sony_pic_fanspeed_show(struct device *dev,
2090 struct device_attribute *attr, char *buffer)
2091{
2092 u8 value = 0;
7b153f36 2093 if (sony_pic_get_fanspeed(&value))
49a11dea 2094 return -EIO;
2095
2096 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2097}
2098
2099#define SPIC_ATTR(_name, _mode) \
2100struct device_attribute spic_attr_##_name = __ATTR(_name, \
2101 _mode, sony_pic_## _name ##_show, \
2102 sony_pic_## _name ##_store)
2103
49a11dea 2104static SPIC_ATTR(bluetoothpower, 0644);
9476cdfa 2105static SPIC_ATTR(wwanpower, 0644);
49a11dea 2106static SPIC_ATTR(fanspeed, 0644);
2107
2108static struct attribute *spic_attributes[] = {
49a11dea 2109 &spic_attr_bluetoothpower.attr,
9476cdfa 2110 &spic_attr_wwanpower.attr,
49a11dea 2111 &spic_attr_fanspeed.attr,
2112 NULL
2113};
2114
2115static struct attribute_group spic_attribute_group = {
2116 .attrs = spic_attributes
2117};
2118
7b153f36 2119/******** SONYPI compatibility **********/
a64e62a0 2120#ifdef CONFIG_SONYPI_COMPAT
7b153f36 2121
2122/* battery / brightness / temperature addresses */
2123#define SONYPI_BAT_FLAGS 0x81
2124#define SONYPI_LCD_LIGHT 0x96
2125#define SONYPI_BAT1_PCTRM 0xa0
2126#define SONYPI_BAT1_LEFT 0xa2
2127#define SONYPI_BAT1_MAXRT 0xa4
2128#define SONYPI_BAT2_PCTRM 0xa8
2129#define SONYPI_BAT2_LEFT 0xaa
2130#define SONYPI_BAT2_MAXRT 0xac
2131#define SONYPI_BAT1_MAXTK 0xb0
2132#define SONYPI_BAT1_FULL 0xb2
2133#define SONYPI_BAT2_MAXTK 0xb8
2134#define SONYPI_BAT2_FULL 0xba
2135#define SONYPI_TEMP_STATUS 0xC1
2136
2137struct sonypi_compat_s {
2138 struct fasync_struct *fifo_async;
45465487 2139 struct kfifo fifo;
7b153f36 2140 spinlock_t fifo_lock;
2141 wait_queue_head_t fifo_proc_list;
2142 atomic_t open_count;
2143};
2144static struct sonypi_compat_s sonypi_compat = {
2145 .open_count = ATOMIC_INIT(0),
2146};
2147
2148static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2149{
60aa4924 2150 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
7b153f36 2151}
2152
2153static int sonypi_misc_release(struct inode *inode, struct file *file)
2154{
7b153f36 2155 atomic_dec(&sonypi_compat.open_count);
2156 return 0;
2157}
2158
2159static int sonypi_misc_open(struct inode *inode, struct file *file)
2160{
2161 /* Flush input queue on first open */
4ef4cbb3
AIB
2162 unsigned long flags;
2163
45465487 2164 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
4ef4cbb3 2165
7b153f36 2166 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
e64c026d 2167 kfifo_reset(&sonypi_compat.fifo);
4ef4cbb3 2168
45465487 2169 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
4ef4cbb3 2170
7b153f36 2171 return 0;
2172}
2173
2174static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2175 size_t count, loff_t *pos)
2176{
2177 ssize_t ret;
2178 unsigned char c;
2179
45465487 2180 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
7b153f36 2181 (file->f_flags & O_NONBLOCK))
2182 return -EAGAIN;
2183
2184 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
45465487 2185 kfifo_len(&sonypi_compat.fifo) != 0);
7b153f36 2186 if (ret)
2187 return ret;
2188
2189 while (ret < count &&
7acd72eb 2190 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
c1e13f25 2191 &sonypi_compat.fifo_lock) == sizeof(c))) {
7b153f36 2192 if (put_user(c, buf++))
2193 return -EFAULT;
2194 ret++;
2195 }
2196
2197 if (ret > 0) {
2198 struct inode *inode = file->f_path.dentry->d_inode;
2199 inode->i_atime = current_fs_time(inode->i_sb);
2200 }
2201
2202 return ret;
2203}
2204
2205static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2206{
2207 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
45465487 2208 if (kfifo_len(&sonypi_compat.fifo))
7b153f36 2209 return POLLIN | POLLRDNORM;
2210 return 0;
2211}
2212
2213static int ec_read16(u8 addr, u16 *value)
2214{
2215 u8 val_lb, val_hb;
2216 if (ec_read(addr, &val_lb))
2217 return -1;
2218 if (ec_read(addr + 1, &val_hb))
2219 return -1;
2220 *value = val_lb | (val_hb << 8);
2221 return 0;
2222}
2223
2b24ef09
AC
2224static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2225 unsigned long arg)
7b153f36 2226{
2227 int ret = 0;
2228 void __user *argp = (void __user *)arg;
2229 u8 val8;
2230 u16 val16;
2231 int value;
2232
9f9f0761 2233 mutex_lock(&spic_dev.lock);
7b153f36 2234 switch (cmd) {
2235 case SONYPI_IOCGBRT:
2236 if (sony_backlight_device == NULL) {
2237 ret = -EIO;
2238 break;
2239 }
2240 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2241 ret = -EIO;
2242 break;
2243 }
2244 val8 = ((value & 0xff) - 1) << 5;
2245 if (copy_to_user(argp, &val8, sizeof(val8)))
2246 ret = -EFAULT;
2247 break;
2248 case SONYPI_IOCSBRT:
2249 if (sony_backlight_device == NULL) {
2250 ret = -EIO;
2251 break;
2252 }
2253 if (copy_from_user(&val8, argp, sizeof(val8))) {
2254 ret = -EFAULT;
2255 break;
2256 }
2257 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2258 (val8 >> 5) + 1, NULL)) {
2259 ret = -EIO;
2260 break;
2261 }
2262 /* sync the backlight device status */
2263 sony_backlight_device->props.brightness =
2264 sony_backlight_get_brightness(sony_backlight_device);
2265 break;
2266 case SONYPI_IOCGBAT1CAP:
2267 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2268 ret = -EIO;
2269 break;
2270 }
2271 if (copy_to_user(argp, &val16, sizeof(val16)))
2272 ret = -EFAULT;
2273 break;
2274 case SONYPI_IOCGBAT1REM:
2275 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2276 ret = -EIO;
2277 break;
2278 }
2279 if (copy_to_user(argp, &val16, sizeof(val16)))
2280 ret = -EFAULT;
2281 break;
2282 case SONYPI_IOCGBAT2CAP:
2283 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2284 ret = -EIO;
2285 break;
2286 }
2287 if (copy_to_user(argp, &val16, sizeof(val16)))
2288 ret = -EFAULT;
2289 break;
2290 case SONYPI_IOCGBAT2REM:
2291 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2292 ret = -EIO;
2293 break;
2294 }
2295 if (copy_to_user(argp, &val16, sizeof(val16)))
2296 ret = -EFAULT;
2297 break;
2298 case SONYPI_IOCGBATFLAGS:
2299 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2300 ret = -EIO;
2301 break;
2302 }
2303 val8 &= 0x07;
2304 if (copy_to_user(argp, &val8, sizeof(val8)))
2305 ret = -EFAULT;
2306 break;
2307 case SONYPI_IOCGBLUE:
2308 val8 = spic_dev.bluetooth_power;
2309 if (copy_to_user(argp, &val8, sizeof(val8)))
2310 ret = -EFAULT;
2311 break;
2312 case SONYPI_IOCSBLUE:
2313 if (copy_from_user(&val8, argp, sizeof(val8))) {
2314 ret = -EFAULT;
2315 break;
2316 }
9f9f0761 2317 __sony_pic_set_bluetoothpower(val8);
7b153f36 2318 break;
2319 /* FAN Controls */
2320 case SONYPI_IOCGFAN:
2321 if (sony_pic_get_fanspeed(&val8)) {
2322 ret = -EIO;
2323 break;
2324 }
2325 if (copy_to_user(argp, &val8, sizeof(val8)))
2326 ret = -EFAULT;
2327 break;
2328 case SONYPI_IOCSFAN:
2329 if (copy_from_user(&val8, argp, sizeof(val8))) {
2330 ret = -EFAULT;
2331 break;
2332 }
2333 if (sony_pic_set_fanspeed(val8))
2334 ret = -EIO;
2335 break;
2336 /* GET Temperature (useful under APM) */
2337 case SONYPI_IOCGTEMP:
2338 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2339 ret = -EIO;
2340 break;
2341 }
2342 if (copy_to_user(argp, &val8, sizeof(val8)))
2343 ret = -EFAULT;
2344 break;
2345 default:
2346 ret = -EINVAL;
2347 }
9f9f0761 2348 mutex_unlock(&spic_dev.lock);
7b153f36 2349 return ret;
2350}
2351
2352static const struct file_operations sonypi_misc_fops = {
2353 .owner = THIS_MODULE,
2354 .read = sonypi_misc_read,
2355 .poll = sonypi_misc_poll,
2356 .open = sonypi_misc_open,
2357 .release = sonypi_misc_release,
2358 .fasync = sonypi_misc_fasync,
2b24ef09 2359 .unlocked_ioctl = sonypi_misc_ioctl,
7b153f36 2360};
2361
2362static struct miscdevice sonypi_misc_device = {
2363 .minor = MISC_DYNAMIC_MINOR,
2364 .name = "sonypi",
2365 .fops = &sonypi_misc_fops,
2366};
2367
2368static void sonypi_compat_report_event(u8 event)
2369{
7acd72eb 2370 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
c1e13f25 2371 sizeof(event), &sonypi_compat.fifo_lock);
7b153f36 2372 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2373 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2374}
2375
2376static int sonypi_compat_init(void)
2377{
2378 int error;
2379
2380 spin_lock_init(&sonypi_compat.fifo_lock);
45465487 2381 error =
c1e13f25 2382 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
45465487 2383 if (error) {
7b153f36 2384 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
45465487 2385 return error;
7b153f36 2386 }
2387
2388 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
7b153f36 2389
2390 if (minor != -1)
2391 sonypi_misc_device.minor = minor;
2392 error = misc_register(&sonypi_misc_device);
2393 if (error) {
2394 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2395 goto err_free_kfifo;
2396 }
2397 if (minor == -1)
5f3d2898 2398 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
7b153f36 2399 sonypi_misc_device.minor);
2400
2401 return 0;
2402
2403err_free_kfifo:
45465487 2404 kfifo_free(&sonypi_compat.fifo);
7b153f36 2405 return error;
2406}
2407
2408static void sonypi_compat_exit(void)
2409{
2410 misc_deregister(&sonypi_misc_device);
45465487 2411 kfifo_free(&sonypi_compat.fifo);
7b153f36 2412}
2413#else
2414static int sonypi_compat_init(void) { return 0; }
2415static void sonypi_compat_exit(void) { }
2416static void sonypi_compat_report_event(u8 event) { }
a64e62a0 2417#endif /* CONFIG_SONYPI_COMPAT */
7b153f36 2418
1549ee6f 2419/*
33a04454 2420 * ACPI callbacks
1549ee6f 2421 */
33a04454 2422static acpi_status
2423sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2424{
2425 u32 i;
2426 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2427
2428 switch (resource->type) {
2429 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
fd1caaed
MD
2430 {
2431 /* start IO enumeration */
2432 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2433 if (!ioport)
2434 return AE_ERROR;
2435
2436 list_add(&ioport->list, &dev->ioports);
2437 return AE_OK;
2438 }
2439
33a04454 2440 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
fd1caaed 2441 /* end IO enumeration */
33a04454 2442 return AE_OK;
2443
2444 case ACPI_RESOURCE_TYPE_IRQ:
2445 {
2446 struct acpi_resource_irq *p = &resource->data.irq;
2447 struct sony_pic_irq *interrupt = NULL;
2448 if (!p || !p->interrupt_count) {
2449 /*
2450 * IRQ descriptors may have no IRQ# bits set,
2451 * particularly those those w/ _STA disabled
2452 */
2453 dprintk("Blank IRQ resource\n");
2454 return AE_OK;
2455 }
2456 for (i = 0; i < p->interrupt_count; i++) {
2457 if (!p->interrupts[i]) {
2458 printk(KERN_WARNING DRV_PFX
2459 "Invalid IRQ %d\n",
2460 p->interrupts[i]);
2461 continue;
2462 }
2463 interrupt = kzalloc(sizeof(*interrupt),
2464 GFP_KERNEL);
2465 if (!interrupt)
2466 return AE_ERROR;
2467
fd1caaed 2468 list_add(&interrupt->list, &dev->interrupts);
33a04454 2469 interrupt->irq.triggering = p->triggering;
2470 interrupt->irq.polarity = p->polarity;
2471 interrupt->irq.sharable = p->sharable;
2472 interrupt->irq.interrupt_count = 1;
2473 interrupt->irq.interrupts[0] = p->interrupts[i];
2474 }
2475 return AE_OK;
2476 }
2477 case ACPI_RESOURCE_TYPE_IO:
2478 {
2479 struct acpi_resource_io *io = &resource->data.io;
fd1caaed
MD
2480 struct sony_pic_ioport *ioport =
2481 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
33a04454 2482 if (!io) {
2483 dprintk("Blank IO resource\n");
2484 return AE_OK;
2485 }
2486
fd1caaed
MD
2487 if (!ioport->io1.minimum) {
2488 memcpy(&ioport->io1, io, sizeof(*io));
2489 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2490 ioport->io1.address_length);
2491 }
2492 else if (!ioport->io2.minimum) {
2493 memcpy(&ioport->io2, io, sizeof(*io));
2494 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2495 ioport->io2.address_length);
2496 }
2497 else {
2498 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
33a04454 2499 return AE_ERROR;
fd1caaed 2500 }
33a04454 2501 return AE_OK;
2502 }
2503 default:
2504 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2505 resource->type);
2506
2507 case ACPI_RESOURCE_TYPE_END_TAG:
2508 return AE_OK;
2509 }
2510 return AE_CTRL_TERMINATE;
2511}
2512
2513static int sony_pic_possible_resources(struct acpi_device *device)
2514{
2515 int result = 0;
2516 acpi_status status = AE_OK;
2517
2518 if (!device)
2519 return -EINVAL;
2520
2521 /* get device status */
2522 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2523 dprintk("Evaluating _STA\n");
2524 result = acpi_bus_get_status(device);
2525 if (result) {
2526 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2527 goto end;
2528 }
2529
2530 if (!device->status.enabled)
2531 dprintk("Device disabled\n");
2532 else
2533 dprintk("Device enabled\n");
2534
2535 /*
2536 * Query and parse 'method'
2537 */
2538 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2539 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2540 sony_pic_read_possible_resource, &spic_dev);
2541 if (ACPI_FAILURE(status)) {
2542 printk(KERN_WARNING DRV_PFX
2543 "Failure evaluating %s\n",
2544 METHOD_NAME__PRS);
2545 result = -ENODEV;
2546 }
2547end:
2548 return result;
2549}
2550
2551/*
2552 * Disable the spic device by calling its _DIS method
2553 */
2554static int sony_pic_disable(struct acpi_device *device)
2555{
6158d3a2
MG
2556 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2557 NULL);
2558
2559 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
33a04454 2560 return -ENXIO;
2561
2562 dprintk("Device disabled\n");
2563 return 0;
2564}
2565
2566
2567/*
2568 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2569 *
2570 * Call _SRS to set current resources
2571 */
2572static int sony_pic_enable(struct acpi_device *device,
2573 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2574{
2575 acpi_status status;
2576 int result = 0;
fd1caaed
MD
2577 /* Type 1 resource layout is:
2578 * IO
2579 * IO
2580 * IRQNoFlags
2581 * End
2582 *
2583 * Type 2 and 3 resource layout is:
2584 * IO
2585 * IRQNoFlags
2586 * End
2587 */
33a04454 2588 struct {
fd1caaed
MD
2589 struct acpi_resource res1;
2590 struct acpi_resource res2;
2591 struct acpi_resource res3;
2592 struct acpi_resource res4;
33a04454 2593 } *resource;
2594 struct acpi_buffer buffer = { 0, NULL };
2595
2596 if (!ioport || !irq)
2597 return -EINVAL;
2598
2599 /* init acpi_buffer */
2600 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2601 if (!resource)
2602 return -ENOMEM;
2603
2604 buffer.length = sizeof(*resource) + 1;
2605 buffer.pointer = resource;
2606
fd1caaed 2607 /* setup Type 1 resources */
31df7144 2608 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
33a04454 2609
fd1caaed
MD
2610 /* setup io resources */
2611 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2612 resource->res1.length = sizeof(struct acpi_resource);
2613 memcpy(&resource->res1.data.io, &ioport->io1,
2614 sizeof(struct acpi_resource_io));
33a04454 2615
fd1caaed
MD
2616 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2617 resource->res2.length = sizeof(struct acpi_resource);
2618 memcpy(&resource->res2.data.io, &ioport->io2,
2619 sizeof(struct acpi_resource_io));
2620
2621 /* setup irq resource */
2622 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2623 resource->res3.length = sizeof(struct acpi_resource);
2624 memcpy(&resource->res3.data.irq, &irq->irq,
2625 sizeof(struct acpi_resource_irq));
2626 /* we requested a shared irq */
2627 resource->res3.data.irq.sharable = ACPI_SHARED;
2628
2629 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2630
2631 }
2632 /* setup Type 2/3 resources */
2633 else {
2634 /* setup io resource */
2635 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2636 resource->res1.length = sizeof(struct acpi_resource);
2637 memcpy(&resource->res1.data.io, &ioport->io1,
2638 sizeof(struct acpi_resource_io));
2639
2640 /* setup irq resource */
2641 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2642 resource->res2.length = sizeof(struct acpi_resource);
2643 memcpy(&resource->res2.data.irq, &irq->irq,
2644 sizeof(struct acpi_resource_irq));
2645 /* we requested a shared irq */
2646 resource->res2.data.irq.sharable = ACPI_SHARED;
2647
2648 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2649 }
33a04454 2650
2651 /* Attempt to set the resource */
2652 dprintk("Evaluating _SRS\n");
2653 status = acpi_set_current_resources(device->handle, &buffer);
2654
2655 /* check for total failure */
2656 if (ACPI_FAILURE(status)) {
fd1caaed 2657 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
33a04454 2658 result = -ENODEV;
2659 goto end;
2660 }
2661
2662 /* Necessary device initializations calls (from sonypi) */
2663 sony_pic_call1(0x82);
2664 sony_pic_call2(0x81, 0xff);
2665 sony_pic_call1(compat ? 0x92 : 0x82);
2666
2667end:
2668 kfree(resource);
2669 return result;
2670}
2671
2672/*****************
2673 *
2674 * ISR: some event is available
2675 *
2676 *****************/
2677static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2678{
2679 int i, j;
33a04454 2680 u8 ev = 0;
2681 u8 data_mask = 0;
2682 u8 device_event = 0;
2683
2684 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2685
fd1caaed
MD
2686 ev = inb_p(dev->cur_ioport->io1.minimum);
2687 if (dev->cur_ioport->io2.minimum)
2688 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2689 else
de920430 2690 data_mask = inb_p(dev->cur_ioport->io1.minimum +
31df7144 2691 dev->evport_offset);
33a04454 2692
22a17780 2693 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
de920430 2694 ev, data_mask, dev->cur_ioport->io1.minimum,
31df7144 2695 dev->evport_offset);
33a04454 2696
2697 if (ev == 0x00 || ev == 0xff)
2698 return IRQ_HANDLED;
2699
31df7144 2700 for (i = 0; dev->event_types[i].mask; i++) {
33a04454 2701
31df7144
MD
2702 if ((data_mask & dev->event_types[i].data) !=
2703 dev->event_types[i].data)
33a04454 2704 continue;
2705
31df7144 2706 if (!(mask & dev->event_types[i].mask))
33a04454 2707 continue;
2708
31df7144
MD
2709 for (j = 0; dev->event_types[i].events[j].event; j++) {
2710 if (ev == dev->event_types[i].events[j].data) {
33a04454 2711 device_event =
31df7144 2712 dev->event_types[i].events[j].event;
33a04454 2713 goto found;
2714 }
2715 }
2716 }
3eb8749a
MD
2717 /* Still not able to decode the event try to pass
2718 * it over to the minidriver
2719 */
31df7144 2720 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
3eb8749a
MD
2721 return IRQ_HANDLED;
2722
de920430
MD
2723 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2724 ev, data_mask, dev->cur_ioport->io1.minimum,
31df7144 2725 dev->evport_offset);
33a04454 2726 return IRQ_HANDLED;
2727
2728found:
1549ee6f 2729 sony_laptop_report_input_event(device_event);
de920430 2730 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
7b153f36 2731 sonypi_compat_report_event(device_event);
33a04454 2732
2733 return IRQ_HANDLED;
2734}
2735
2736/*****************
2737 *
2738 * ACPI driver
2739 *
2740 *****************/
2741static int sony_pic_remove(struct acpi_device *device, int type)
2742{
2743 struct sony_pic_ioport *io, *tmp_io;
2744 struct sony_pic_irq *irq, *tmp_irq;
2745
2746 if (sony_pic_disable(device)) {
2747 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2748 return -ENXIO;
2749 }
2750
2751 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
fd1caaed
MD
2752 release_region(spic_dev.cur_ioport->io1.minimum,
2753 spic_dev.cur_ioport->io1.address_length);
2754 if (spic_dev.cur_ioport->io2.minimum)
2755 release_region(spic_dev.cur_ioport->io2.minimum,
2756 spic_dev.cur_ioport->io2.address_length);
33a04454 2757
015a916f
MD
2758 sonypi_compat_exit();
2759
1549ee6f 2760 sony_laptop_remove_input();
33a04454 2761
49a11dea 2762 /* pf attrs */
2763 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2764 sony_pf_remove();
2765
33a04454 2766 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2767 list_del(&io->list);
2768 kfree(io);
2769 }
2770 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2771 list_del(&irq->list);
2772 kfree(irq);
2773 }
2774 spic_dev.cur_ioport = NULL;
2775 spic_dev.cur_irq = NULL;
2776
f6119b02 2777 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
33a04454 2778 return 0;
2779}
2780
2781static int sony_pic_add(struct acpi_device *device)
2782{
2783 int result;
2784 struct sony_pic_ioport *io, *tmp_io;
2785 struct sony_pic_irq *irq, *tmp_irq;
2786
f6119b02 2787 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2788 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
33a04454 2789
2790 spic_dev.acpi_dev = device;
2791 strcpy(acpi_device_class(device), "sony/hotkey");
de920430 2792 sony_pic_detect_device_type(&spic_dev);
9f9f0761 2793 mutex_init(&spic_dev.lock);
33a04454 2794
2795 /* read _PRS resources */
2796 result = sony_pic_possible_resources(device);
2797 if (result) {
2798 printk(KERN_ERR DRV_PFX
ff07a53a 2799 "Unable to read possible resources.\n");
33a04454 2800 goto err_free_resources;
2801 }
2802
2803 /* setup input devices and helper fifo */
2e4d242c 2804 result = sony_laptop_setup_input(device);
33a04454 2805 if (result) {
2806 printk(KERN_ERR DRV_PFX
ff07a53a 2807 "Unable to create input devices.\n");
33a04454 2808 goto err_free_resources;
2809 }
2810
015a916f
MD
2811 if (sonypi_compat_init())
2812 goto err_remove_input;
2813
33a04454 2814 /* request io port */
fd1caaed
MD
2815 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2816 if (request_region(io->io1.minimum, io->io1.address_length,
33a04454 2817 "Sony Programable I/O Device")) {
fd1caaed
MD
2818 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2819 io->io1.minimum, io->io1.maximum,
2820 io->io1.address_length);
2821 /* Type 1 have 2 ioports */
2822 if (io->io2.minimum) {
2823 if (request_region(io->io2.minimum,
2824 io->io2.address_length,
2825 "Sony Programable I/O Device")) {
2826 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2827 io->io2.minimum, io->io2.maximum,
2828 io->io2.address_length);
2829 spic_dev.cur_ioport = io;
2830 break;
2831 }
2832 else {
2833 dprintk("Unable to get I/O port2: "
2834 "0x%.4x (0x%.4x) + 0x%.2x\n",
2835 io->io2.minimum, io->io2.maximum,
2836 io->io2.address_length);
2837 release_region(io->io1.minimum,
2838 io->io1.address_length);
2839 }
2840 }
2841 else {
2842 spic_dev.cur_ioport = io;
2843 break;
2844 }
33a04454 2845 }
2846 }
2847 if (!spic_dev.cur_ioport) {
2848 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2849 result = -ENODEV;
015a916f 2850 goto err_remove_compat;
33a04454 2851 }
2852
2853 /* request IRQ */
fd1caaed 2854 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
33a04454 2855 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
d1e0de92 2856 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
33a04454 2857 dprintk("IRQ: %d - triggering: %d - "
2858 "polarity: %d - shr: %d\n",
2859 irq->irq.interrupts[0],
2860 irq->irq.triggering,
2861 irq->irq.polarity,
2862 irq->irq.sharable);
2863 spic_dev.cur_irq = irq;
2864 break;
2865 }
2866 }
2867 if (!spic_dev.cur_irq) {
2868 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2869 result = -ENODEV;
2870 goto err_release_region;
2871 }
2872
2873 /* set resource status _SRS */
2874 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2875 if (result) {
2876 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2877 goto err_free_irq;
2878 }
2879
49a11dea 2880 spic_dev.bluetooth_power = -1;
2881 /* create device attributes */
2882 result = sony_pf_add();
2883 if (result)
2884 goto err_disable_device;
2885
2886 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2887 if (result)
2888 goto err_remove_pf;
2889
33a04454 2890 return 0;
2891
49a11dea 2892err_remove_pf:
2893 sony_pf_remove();
2894
2895err_disable_device:
2896 sony_pic_disable(device);
2897
33a04454 2898err_free_irq:
2899 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2900
2901err_release_region:
fd1caaed
MD
2902 release_region(spic_dev.cur_ioport->io1.minimum,
2903 spic_dev.cur_ioport->io1.address_length);
2904 if (spic_dev.cur_ioport->io2.minimum)
2905 release_region(spic_dev.cur_ioport->io2.minimum,
2906 spic_dev.cur_ioport->io2.address_length);
33a04454 2907
015a916f
MD
2908err_remove_compat:
2909 sonypi_compat_exit();
2910
33a04454 2911err_remove_input:
1549ee6f 2912 sony_laptop_remove_input();
33a04454 2913
2914err_free_resources:
2915 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2916 list_del(&io->list);
2917 kfree(io);
2918 }
2919 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2920 list_del(&irq->list);
2921 kfree(irq);
2922 }
2923 spic_dev.cur_ioport = NULL;
2924 spic_dev.cur_irq = NULL;
2925
2926 return result;
2927}
2928
2929static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2930{
2931 if (sony_pic_disable(device))
2932 return -ENXIO;
2933 return 0;
2934}
2935
2936static int sony_pic_resume(struct acpi_device *device)
2937{
2938 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2939 return 0;
2940}
2941
1ba90e3a
TR
2942static const struct acpi_device_id sony_pic_device_ids[] = {
2943 {SONY_PIC_HID, 0},
2944 {"", 0},
2945};
2946
33a04454 2947static struct acpi_driver sony_pic_driver = {
2948 .name = SONY_PIC_DRIVER_NAME,
2949 .class = SONY_PIC_CLASS,
1ba90e3a 2950 .ids = sony_pic_device_ids,
33a04454 2951 .owner = THIS_MODULE,
2952 .ops = {
2953 .add = sony_pic_add,
2954 .remove = sony_pic_remove,
2955 .suspend = sony_pic_suspend,
2956 .resume = sony_pic_resume,
2957 },
2958};
2959
2960static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2961 {
2962 .ident = "Sony Vaio",
2963 .matches = {
2964 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2965 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2966 },
2967 },
2968 {
2969 .ident = "Sony Vaio",
2970 .matches = {
2971 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2972 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2973 },
2974 },
2975 { }
2976};
2977
59b19106 2978static int __init sony_laptop_init(void)
7f09c432 2979{
33a04454 2980 int result;
2981
2982 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2983 result = acpi_bus_register_driver(&sony_pic_driver);
2984 if (result) {
2985 printk(KERN_ERR DRV_PFX
2986 "Unable to register SPIC driver.");
2987 goto out;
2988 }
5e6f9725 2989 spic_drv_registered = 1;
33a04454 2990 }
2991
2992 result = acpi_bus_register_driver(&sony_nc_driver);
2993 if (result) {
2994 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2995 goto out_unregister_pic;
2996 }
2997
2998 return 0;
2999
3000out_unregister_pic:
5e6f9725 3001 if (spic_drv_registered)
33a04454 3002 acpi_bus_unregister_driver(&sony_pic_driver);
3003out:
3004 return result;
7f09c432
SP
3005}
3006
59b19106 3007static void __exit sony_laptop_exit(void)
7f09c432 3008{
59b19106 3009 acpi_bus_unregister_driver(&sony_nc_driver);
5e6f9725 3010 if (spic_drv_registered)
33a04454 3011 acpi_bus_unregister_driver(&sony_pic_driver);
7f09c432
SP
3012}
3013
59b19106 3014module_init(sony_laptop_init);
3015module_exit(sony_laptop_exit);