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