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