sony-laptop: Add support for extra keyboard events
[linux-2.6/libata-dev.git] / drivers / platform / x86 / sony-laptop.c
blobf6cdc8929fd7ee8e739f9135e512d30843424e5f
1 /*
2 * ACPI Sony Notebook Control Driver (SNC and SPIC)
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5 * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
10 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
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.
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.
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.
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/init.h>
49 #include <linux/smp_lock.h>
50 #include <linux/types.h>
51 #include <linux/backlight.h>
52 #include <linux/platform_device.h>
53 #include <linux/err.h>
54 #include <linux/dmi.h>
55 #include <linux/pci.h>
56 #include <linux/interrupt.h>
57 #include <linux/delay.h>
58 #include <linux/input.h>
59 #include <linux/kfifo.h>
60 #include <linux/workqueue.h>
61 #include <linux/acpi.h>
62 #include <acpi/acpi_drivers.h>
63 #include <acpi/acpi_bus.h>
64 #include <asm/uaccess.h>
65 #include <linux/sonypi.h>
66 #include <linux/sony-laptop.h>
67 #ifdef CONFIG_SONYPI_COMPAT
68 #include <linux/poll.h>
69 #include <linux/miscdevice.h>
70 #endif
72 #define DRV_PFX "sony-laptop: "
73 #define dprintk(msg...) do { \
74 if (debug) printk(KERN_WARNING DRV_PFX msg); \
75 } while (0)
77 #define SONY_LAPTOP_DRIVER_VERSION "0.6"
79 #define SONY_NC_CLASS "sony-nc"
80 #define SONY_NC_HID "SNY5001"
81 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
83 #define SONY_PIC_CLASS "sony-pic"
84 #define SONY_PIC_HID "SNY6001"
85 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
87 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
88 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
89 MODULE_LICENSE("GPL");
90 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
92 static int debug;
93 module_param(debug, int, 0);
94 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
95 "the development of this driver");
97 static int no_spic; /* = 0 */
98 module_param(no_spic, int, 0444);
99 MODULE_PARM_DESC(no_spic,
100 "set this if you don't want to enable the SPIC device");
102 static int compat; /* = 0 */
103 module_param(compat, int, 0444);
104 MODULE_PARM_DESC(compat,
105 "set this if you want to enable backward compatibility mode");
107 static unsigned long mask = 0xffffffff;
108 module_param(mask, ulong, 0644);
109 MODULE_PARM_DESC(mask,
110 "set this to the mask of event you want to enable (see doc)");
112 static int camera; /* = 0 */
113 module_param(camera, int, 0444);
114 MODULE_PARM_DESC(camera,
115 "set this to 1 to enable Motion Eye camera controls "
116 "(only use it if you have a C1VE or C1VN model)");
118 #ifdef CONFIG_SONYPI_COMPAT
119 static int minor = -1;
120 module_param(minor, int, 0);
121 MODULE_PARM_DESC(minor,
122 "minor number of the misc device for the SPIC compatibility code, "
123 "default is -1 (automatic)");
124 #endif
126 /*********** Input Devices ***********/
128 #define SONY_LAPTOP_BUF_SIZE 128
129 struct sony_laptop_input_s {
130 atomic_t users;
131 struct input_dev *jog_dev;
132 struct input_dev *key_dev;
133 struct kfifo *fifo;
134 spinlock_t fifo_lock;
135 struct workqueue_struct *wq;
137 static struct sony_laptop_input_s sony_laptop_input = {
138 .users = ATOMIC_INIT(0),
141 struct sony_laptop_keypress {
142 struct input_dev *dev;
143 int key;
146 /* Correspondance table between sonypi events
147 * and input layer indexes in the keymap
149 static int sony_laptop_input_index[] = {
150 -1, /* 0 no event */
151 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
152 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
153 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
154 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
155 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
156 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
157 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
158 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
159 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
160 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
161 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
162 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
163 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
164 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
165 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
166 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
167 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
168 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
169 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
170 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
171 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
172 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
173 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
174 17, /* 24 SONYPI_EVENT_FNKEY_1 */
175 18, /* 25 SONYPI_EVENT_FNKEY_2 */
176 19, /* 26 SONYPI_EVENT_FNKEY_D */
177 20, /* 27 SONYPI_EVENT_FNKEY_E */
178 21, /* 28 SONYPI_EVENT_FNKEY_F */
179 22, /* 29 SONYPI_EVENT_FNKEY_S */
180 23, /* 30 SONYPI_EVENT_FNKEY_B */
181 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
182 25, /* 32 SONYPI_EVENT_PKEY_P1 */
183 26, /* 33 SONYPI_EVENT_PKEY_P2 */
184 27, /* 34 SONYPI_EVENT_PKEY_P3 */
185 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
186 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
187 -1, /* 37 SONYPI_EVENT_LID_OPENED */
188 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
189 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
190 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
191 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
192 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
193 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
194 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
195 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
196 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
197 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
198 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
199 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
200 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
201 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
202 43, /* 52 SONYPI_EVENT_MEYE_FACE */
203 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
204 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
205 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
206 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
207 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
208 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
209 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
210 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
211 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
212 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
213 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
214 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
217 static int sony_laptop_input_keycode_map[] = {
218 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
219 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
220 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
221 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
222 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
223 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
224 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
225 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
226 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
227 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
228 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
229 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
230 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
231 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
232 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
233 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
234 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
235 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
236 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
237 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
238 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
239 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
240 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
241 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
242 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
243 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
244 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
245 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
246 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
247 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
248 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
249 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
250 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
251 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
252 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
253 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
254 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
255 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
256 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
257 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
258 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
259 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
260 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
261 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
262 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
263 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
264 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
265 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
266 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
267 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
268 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
269 KEY_EJECTCD /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
272 /* release buttons after a short delay if pressed */
273 static void do_sony_laptop_release_key(struct work_struct *work)
275 struct sony_laptop_keypress kp;
277 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
278 sizeof(kp)) == sizeof(kp)) {
279 msleep(10);
280 input_report_key(kp.dev, kp.key, 0);
281 input_sync(kp.dev);
284 static DECLARE_WORK(sony_laptop_release_key_work,
285 do_sony_laptop_release_key);
287 /* forward event to the input subsystem */
288 static void sony_laptop_report_input_event(u8 event)
290 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
291 struct input_dev *key_dev = sony_laptop_input.key_dev;
292 struct sony_laptop_keypress kp = { NULL };
294 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
295 /* Nothing, not all VAIOs generate this event */
296 return;
299 /* report events */
300 switch (event) {
301 /* jog_dev events */
302 case SONYPI_EVENT_JOGDIAL_UP:
303 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
304 input_report_rel(jog_dev, REL_WHEEL, 1);
305 input_sync(jog_dev);
306 return;
308 case SONYPI_EVENT_JOGDIAL_DOWN:
309 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
310 input_report_rel(jog_dev, REL_WHEEL, -1);
311 input_sync(jog_dev);
312 return;
314 /* key_dev events */
315 case SONYPI_EVENT_JOGDIAL_PRESSED:
316 kp.key = BTN_MIDDLE;
317 kp.dev = jog_dev;
318 break;
320 default:
321 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
322 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
323 break;
325 if (sony_laptop_input_index[event] != -1) {
326 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
327 if (kp.key != KEY_UNKNOWN)
328 kp.dev = key_dev;
330 break;
333 if (kp.dev) {
334 input_report_key(kp.dev, kp.key, 1);
335 /* we emit the scancode so we can always remap the key */
336 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
337 input_sync(kp.dev);
338 kfifo_put(sony_laptop_input.fifo,
339 (unsigned char *)&kp, sizeof(kp));
341 if (!work_pending(&sony_laptop_release_key_work))
342 queue_work(sony_laptop_input.wq,
343 &sony_laptop_release_key_work);
344 } else
345 dprintk("unknown input event %.2x\n", event);
348 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
350 struct input_dev *jog_dev;
351 struct input_dev *key_dev;
352 int i;
353 int error;
355 /* don't run again if already initialized */
356 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
357 return 0;
359 /* kfifo */
360 spin_lock_init(&sony_laptop_input.fifo_lock);
361 sony_laptop_input.fifo =
362 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
363 &sony_laptop_input.fifo_lock);
364 if (IS_ERR(sony_laptop_input.fifo)) {
365 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
366 error = PTR_ERR(sony_laptop_input.fifo);
367 goto err_dec_users;
370 /* init workqueue */
371 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
372 if (!sony_laptop_input.wq) {
373 printk(KERN_ERR DRV_PFX
374 "Unabe to create workqueue.\n");
375 error = -ENXIO;
376 goto err_free_kfifo;
379 /* input keys */
380 key_dev = input_allocate_device();
381 if (!key_dev) {
382 error = -ENOMEM;
383 goto err_destroy_wq;
386 key_dev->name = "Sony Vaio Keys";
387 key_dev->id.bustype = BUS_ISA;
388 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
389 key_dev->dev.parent = &acpi_device->dev;
391 /* Initialize the Input Drivers: special keys */
392 set_bit(EV_KEY, key_dev->evbit);
393 set_bit(EV_MSC, key_dev->evbit);
394 set_bit(MSC_SCAN, key_dev->mscbit);
395 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
396 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
397 key_dev->keycode = &sony_laptop_input_keycode_map;
398 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++) {
399 if (sony_laptop_input_keycode_map[i] != KEY_RESERVED) {
400 set_bit(sony_laptop_input_keycode_map[i],
401 key_dev->keybit);
405 error = input_register_device(key_dev);
406 if (error)
407 goto err_free_keydev;
409 sony_laptop_input.key_dev = key_dev;
411 /* jogdial */
412 jog_dev = input_allocate_device();
413 if (!jog_dev) {
414 error = -ENOMEM;
415 goto err_unregister_keydev;
418 jog_dev->name = "Sony Vaio Jogdial";
419 jog_dev->id.bustype = BUS_ISA;
420 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
421 key_dev->dev.parent = &acpi_device->dev;
423 jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
424 jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
425 jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
427 error = input_register_device(jog_dev);
428 if (error)
429 goto err_free_jogdev;
431 sony_laptop_input.jog_dev = jog_dev;
433 return 0;
435 err_free_jogdev:
436 input_free_device(jog_dev);
438 err_unregister_keydev:
439 input_unregister_device(key_dev);
440 /* to avoid kref underflow below at input_free_device */
441 key_dev = NULL;
443 err_free_keydev:
444 input_free_device(key_dev);
446 err_destroy_wq:
447 destroy_workqueue(sony_laptop_input.wq);
449 err_free_kfifo:
450 kfifo_free(sony_laptop_input.fifo);
452 err_dec_users:
453 atomic_dec(&sony_laptop_input.users);
454 return error;
457 static void sony_laptop_remove_input(void)
459 /* cleanup only after the last user has gone */
460 if (!atomic_dec_and_test(&sony_laptop_input.users))
461 return;
463 /* flush workqueue first */
464 flush_workqueue(sony_laptop_input.wq);
466 /* destroy input devs */
467 input_unregister_device(sony_laptop_input.key_dev);
468 sony_laptop_input.key_dev = NULL;
470 if (sony_laptop_input.jog_dev) {
471 input_unregister_device(sony_laptop_input.jog_dev);
472 sony_laptop_input.jog_dev = NULL;
475 destroy_workqueue(sony_laptop_input.wq);
476 kfifo_free(sony_laptop_input.fifo);
479 /*********** Platform Device ***********/
481 static atomic_t sony_pf_users = ATOMIC_INIT(0);
482 static struct platform_driver sony_pf_driver = {
483 .driver = {
484 .name = "sony-laptop",
485 .owner = THIS_MODULE,
488 static struct platform_device *sony_pf_device;
490 static int sony_pf_add(void)
492 int ret = 0;
494 /* don't run again if already initialized */
495 if (atomic_add_return(1, &sony_pf_users) > 1)
496 return 0;
498 ret = platform_driver_register(&sony_pf_driver);
499 if (ret)
500 goto out;
502 sony_pf_device = platform_device_alloc("sony-laptop", -1);
503 if (!sony_pf_device) {
504 ret = -ENOMEM;
505 goto out_platform_registered;
508 ret = platform_device_add(sony_pf_device);
509 if (ret)
510 goto out_platform_alloced;
512 return 0;
514 out_platform_alloced:
515 platform_device_put(sony_pf_device);
516 sony_pf_device = NULL;
517 out_platform_registered:
518 platform_driver_unregister(&sony_pf_driver);
519 out:
520 atomic_dec(&sony_pf_users);
521 return ret;
524 static void sony_pf_remove(void)
526 /* deregister only after the last user has gone */
527 if (!atomic_dec_and_test(&sony_pf_users))
528 return;
530 platform_device_del(sony_pf_device);
531 platform_device_put(sony_pf_device);
532 platform_driver_unregister(&sony_pf_driver);
535 /*********** SNC (SNY5001) Device ***********/
537 /* the device uses 1-based values, while the backlight subsystem uses
538 0-based values */
539 #define SONY_MAX_BRIGHTNESS 8
541 #define SNC_VALIDATE_IN 0
542 #define SNC_VALIDATE_OUT 1
544 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
545 char *);
546 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
547 const char *, size_t);
548 static int boolean_validate(const int, const int);
549 static int brightness_default_validate(const int, const int);
551 struct sony_nc_value {
552 char *name; /* name of the entry */
553 char **acpiget; /* names of the ACPI get function */
554 char **acpiset; /* names of the ACPI set function */
555 int (*validate)(const int, const int); /* input/output validation */
556 int value; /* current setting */
557 int valid; /* Has ever been set */
558 int debug; /* active only in debug mode ? */
559 struct device_attribute devattr; /* sysfs atribute */
562 #define SNC_HANDLE_NAMES(_name, _values...) \
563 static char *snc_##_name[] = { _values, NULL }
565 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
567 .name = __stringify(_name), \
568 .acpiget = _getters, \
569 .acpiset = _setters, \
570 .validate = _validate, \
571 .debug = _debug, \
572 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
575 #define SNC_HANDLE_NULL { .name = NULL }
577 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
579 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
580 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
582 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
583 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
585 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
586 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
588 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
589 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
591 SNC_HANDLE_NAMES(lidstate_get, "GLID");
593 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
594 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
596 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
597 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
599 SNC_HANDLE_NAMES(PID_get, "GPID");
601 SNC_HANDLE_NAMES(CTR_get, "GCTR");
602 SNC_HANDLE_NAMES(CTR_set, "SCTR");
604 SNC_HANDLE_NAMES(PCR_get, "GPCR");
605 SNC_HANDLE_NAMES(PCR_set, "SPCR");
607 SNC_HANDLE_NAMES(CMI_get, "GCMI");
608 SNC_HANDLE_NAMES(CMI_set, "SCMI");
610 static struct sony_nc_value sony_nc_values[] = {
611 SNC_HANDLE(brightness_default, snc_brightness_def_get,
612 snc_brightness_def_set, brightness_default_validate, 0),
613 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
614 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
615 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
616 boolean_validate, 0),
617 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
618 boolean_validate, 1),
619 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
620 boolean_validate, 0),
621 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
622 boolean_validate, 0),
623 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
624 boolean_validate, 0),
625 /* unknown methods */
626 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
627 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
628 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
629 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
630 SNC_HANDLE_NULL
633 static acpi_handle sony_nc_acpi_handle;
634 static struct acpi_device *sony_nc_acpi_device = NULL;
637 * acpi_evaluate_object wrappers
639 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
641 struct acpi_buffer output;
642 union acpi_object out_obj;
643 acpi_status status;
645 output.length = sizeof(out_obj);
646 output.pointer = &out_obj;
648 status = acpi_evaluate_object(handle, name, NULL, &output);
649 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
650 *result = out_obj.integer.value;
651 return 0;
654 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
656 return -1;
659 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
660 int *result)
662 struct acpi_object_list params;
663 union acpi_object in_obj;
664 struct acpi_buffer output;
665 union acpi_object out_obj;
666 acpi_status status;
668 params.count = 1;
669 params.pointer = &in_obj;
670 in_obj.type = ACPI_TYPE_INTEGER;
671 in_obj.integer.value = value;
673 output.length = sizeof(out_obj);
674 output.pointer = &out_obj;
676 status = acpi_evaluate_object(handle, name, &params, &output);
677 if (status == AE_OK) {
678 if (result != NULL) {
679 if (out_obj.type != ACPI_TYPE_INTEGER) {
680 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
681 "return type\n");
682 return -1;
684 *result = out_obj.integer.value;
686 return 0;
689 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
691 return -1;
694 static int sony_find_snc_handle(int handle)
696 int i;
697 int result;
699 for (i = 0x20; i < 0x30; i++) {
700 acpi_callsetfunc(sony_nc_acpi_handle, "SN00", i, &result);
701 if (result == handle)
702 return i-0x20;
705 return -1;
708 static int sony_call_snc_handle(int handle, int argument, int *result)
710 int offset = sony_find_snc_handle(handle);
712 if (offset < 0)
713 return -1;
715 return acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
716 result);
720 * sony_nc_values input/output validate functions
723 /* brightness_default_validate:
725 * manipulate input output values to keep consistency with the
726 * backlight framework for which brightness values are 0-based.
728 static int brightness_default_validate(const int direction, const int value)
730 switch (direction) {
731 case SNC_VALIDATE_OUT:
732 return value - 1;
733 case SNC_VALIDATE_IN:
734 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
735 return value + 1;
737 return -EINVAL;
740 /* boolean_validate:
742 * on input validate boolean values 0/1, on output just pass the
743 * received value.
745 static int boolean_validate(const int direction, const int value)
747 if (direction == SNC_VALIDATE_IN) {
748 if (value != 0 && value != 1)
749 return -EINVAL;
751 return value;
755 * Sysfs show/store common to all sony_nc_values
757 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
758 char *buffer)
760 int value;
761 struct sony_nc_value *item =
762 container_of(attr, struct sony_nc_value, devattr);
764 if (!*item->acpiget)
765 return -EIO;
767 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
768 return -EIO;
770 if (item->validate)
771 value = item->validate(SNC_VALIDATE_OUT, value);
773 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
776 static ssize_t sony_nc_sysfs_store(struct device *dev,
777 struct device_attribute *attr,
778 const char *buffer, size_t count)
780 int value;
781 struct sony_nc_value *item =
782 container_of(attr, struct sony_nc_value, devattr);
784 if (!item->acpiset)
785 return -EIO;
787 if (count > 31)
788 return -EINVAL;
790 value = simple_strtoul(buffer, NULL, 10);
792 if (item->validate)
793 value = item->validate(SNC_VALIDATE_IN, value);
795 if (value < 0)
796 return value;
798 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
799 return -EIO;
800 item->value = value;
801 item->valid = 1;
802 return count;
807 * Backlight device
809 static int sony_backlight_update_status(struct backlight_device *bd)
811 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
812 bd->props.brightness + 1, NULL);
815 static int sony_backlight_get_brightness(struct backlight_device *bd)
817 int value;
819 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
820 return 0;
821 /* brightness levels are 1-based, while backlight ones are 0-based */
822 return value - 1;
825 static struct backlight_device *sony_backlight_device;
826 static struct backlight_ops sony_backlight_ops = {
827 .update_status = sony_backlight_update_status,
828 .get_brightness = sony_backlight_get_brightness,
832 * New SNC-only Vaios event mapping to driver known keys
834 struct sony_nc_event {
835 u8 data;
836 u8 event;
839 static struct sony_nc_event sony_nc_events[] = {
840 { 0x90, SONYPI_EVENT_PKEY_P1 },
841 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
842 { 0x91, SONYPI_EVENT_PKEY_P1 },
843 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
844 { 0x81, SONYPI_EVENT_FNKEY_F1 },
845 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
846 { 0x85, SONYPI_EVENT_FNKEY_F5 },
847 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
848 { 0x86, SONYPI_EVENT_FNKEY_F6 },
849 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
850 { 0x87, SONYPI_EVENT_FNKEY_F7 },
851 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
852 { 0x89, SONYPI_EVENT_FNKEY_F9 },
853 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
854 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
855 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
856 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
857 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
858 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
859 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
860 { 0, 0 },
864 * ACPI callbacks
866 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
868 u32 ev = event;
869 int result;
871 if (ev >= 0x90) {
872 /* New-style event */
873 int origev = ev;
874 ev -= 0x90;
876 if (sony_find_snc_handle(0x100) == ev) {
877 int i;
879 if (sony_call_snc_handle(0x100, 0x200, &result))
880 dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
881 else
882 ev = result & 0xFF;
884 for (i = 0; sony_nc_events[i].event; i++) {
885 if (sony_nc_events[i].data == ev) {
886 ev = sony_nc_events[i].event;
887 break;
891 if (!sony_nc_events[i].data)
892 printk(KERN_INFO DRV_PFX
893 "Unknown event: %x %x\n", origev, ev);
897 dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
898 sony_laptop_report_input_event(ev);
899 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
902 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
903 void *context, void **return_value)
905 struct acpi_device_info *info;
906 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
908 if (ACPI_SUCCESS(acpi_get_object_info(handle, &buffer))) {
909 info = buffer.pointer;
911 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n",
912 (char *)&info->name, info->param_count);
914 kfree(buffer.pointer);
917 return AE_OK;
921 * ACPI device
923 static int sony_nc_function_setup(struct acpi_device *device)
925 int result;
927 /* Enable all events */
928 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
930 /* Setup hotkeys */
931 sony_call_snc_handle(0x0100, 0, &result);
932 sony_call_snc_handle(0x0101, 0, &result);
933 sony_call_snc_handle(0x0102, 0x100, &result);
935 return 0;
938 static int sony_nc_resume(struct acpi_device *device)
940 struct sony_nc_value *item;
941 acpi_handle handle;
943 for (item = sony_nc_values; item->name; item++) {
944 int ret;
946 if (!item->valid)
947 continue;
948 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
949 item->value, NULL);
950 if (ret < 0) {
951 printk("%s: %d\n", __func__, ret);
952 break;
956 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
957 &handle))) {
958 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
959 dprintk("ECON Method failed\n");
962 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
963 &handle))) {
964 dprintk("Doing SNC setup\n");
965 sony_nc_function_setup(device);
968 /* set the last requested brightness level */
969 if (sony_backlight_device &&
970 !sony_backlight_update_status(sony_backlight_device))
971 printk(KERN_WARNING DRV_PFX "unable to restore brightness level\n");
973 return 0;
976 static int sony_nc_add(struct acpi_device *device)
978 acpi_status status;
979 int result = 0;
980 acpi_handle handle;
981 struct sony_nc_value *item;
983 printk(KERN_INFO DRV_PFX "%s v%s.\n",
984 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
986 sony_nc_acpi_device = device;
987 strcpy(acpi_device_class(device), "sony/hotkey");
989 sony_nc_acpi_handle = device->handle;
991 /* read device status */
992 result = acpi_bus_get_status(device);
993 /* bail IFF the above call was successful and the device is not present */
994 if (!result && !device->status.present) {
995 dprintk("Device not present\n");
996 result = -ENODEV;
997 goto outwalk;
1000 if (debug) {
1001 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
1002 1, sony_walk_callback, NULL, NULL);
1003 if (ACPI_FAILURE(status)) {
1004 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
1005 result = -ENODEV;
1006 goto outwalk;
1010 /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
1011 * should be respected as we already checked for the device presence above */
1012 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
1013 dprintk("Invoking _INI\n");
1014 if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
1015 NULL, NULL)))
1016 dprintk("_INI Method failed\n");
1019 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1020 &handle))) {
1021 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1022 dprintk("ECON Method failed\n");
1025 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1026 &handle))) {
1027 dprintk("Doing SNC setup\n");
1028 sony_nc_function_setup(device);
1031 /* setup input devices and helper fifo */
1032 result = sony_laptop_setup_input(device);
1033 if (result) {
1034 printk(KERN_ERR DRV_PFX
1035 "Unabe to create input devices.\n");
1036 goto outwalk;
1039 status = acpi_install_notify_handler(sony_nc_acpi_handle,
1040 ACPI_DEVICE_NOTIFY,
1041 sony_acpi_notify, NULL);
1042 if (ACPI_FAILURE(status)) {
1043 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
1044 result = -ENODEV;
1045 goto outinput;
1048 if (acpi_video_backlight_support()) {
1049 printk(KERN_INFO DRV_PFX "brightness ignored, must be "
1050 "controlled by ACPI video driver\n");
1051 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1052 &handle))) {
1053 sony_backlight_device = backlight_device_register("sony", NULL,
1054 NULL,
1055 &sony_backlight_ops);
1057 if (IS_ERR(sony_backlight_device)) {
1058 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
1059 sony_backlight_device = NULL;
1060 } else {
1061 sony_backlight_device->props.brightness =
1062 sony_backlight_get_brightness
1063 (sony_backlight_device);
1064 sony_backlight_device->props.max_brightness =
1065 SONY_MAX_BRIGHTNESS - 1;
1070 result = sony_pf_add();
1071 if (result)
1072 goto outbacklight;
1074 /* create sony_pf sysfs attributes related to the SNC device */
1075 for (item = sony_nc_values; item->name; ++item) {
1077 if (!debug && item->debug)
1078 continue;
1080 /* find the available acpiget as described in the DSDT */
1081 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1082 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1083 *item->acpiget,
1084 &handle))) {
1085 dprintk("Found %s getter: %s\n",
1086 item->name, *item->acpiget);
1087 item->devattr.attr.mode |= S_IRUGO;
1088 break;
1092 /* find the available acpiset as described in the DSDT */
1093 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1094 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1095 *item->acpiset,
1096 &handle))) {
1097 dprintk("Found %s setter: %s\n",
1098 item->name, *item->acpiset);
1099 item->devattr.attr.mode |= S_IWUSR;
1100 break;
1104 if (item->devattr.attr.mode != 0) {
1105 result =
1106 device_create_file(&sony_pf_device->dev,
1107 &item->devattr);
1108 if (result)
1109 goto out_sysfs;
1113 return 0;
1115 out_sysfs:
1116 for (item = sony_nc_values; item->name; ++item) {
1117 device_remove_file(&sony_pf_device->dev, &item->devattr);
1119 sony_pf_remove();
1121 outbacklight:
1122 if (sony_backlight_device)
1123 backlight_device_unregister(sony_backlight_device);
1125 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
1126 ACPI_DEVICE_NOTIFY,
1127 sony_acpi_notify);
1128 if (ACPI_FAILURE(status))
1129 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
1131 outinput:
1132 sony_laptop_remove_input();
1134 outwalk:
1135 return result;
1138 static int sony_nc_remove(struct acpi_device *device, int type)
1140 acpi_status status;
1141 struct sony_nc_value *item;
1143 if (sony_backlight_device)
1144 backlight_device_unregister(sony_backlight_device);
1146 sony_nc_acpi_device = NULL;
1148 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
1149 ACPI_DEVICE_NOTIFY,
1150 sony_acpi_notify);
1151 if (ACPI_FAILURE(status))
1152 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
1154 for (item = sony_nc_values; item->name; ++item) {
1155 device_remove_file(&sony_pf_device->dev, &item->devattr);
1158 sony_pf_remove();
1159 sony_laptop_remove_input();
1160 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
1162 return 0;
1165 static const struct acpi_device_id sony_device_ids[] = {
1166 {SONY_NC_HID, 0},
1167 {SONY_PIC_HID, 0},
1168 {"", 0},
1170 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1172 static const struct acpi_device_id sony_nc_device_ids[] = {
1173 {SONY_NC_HID, 0},
1174 {"", 0},
1177 static struct acpi_driver sony_nc_driver = {
1178 .name = SONY_NC_DRIVER_NAME,
1179 .class = SONY_NC_CLASS,
1180 .ids = sony_nc_device_ids,
1181 .owner = THIS_MODULE,
1182 .ops = {
1183 .add = sony_nc_add,
1184 .remove = sony_nc_remove,
1185 .resume = sony_nc_resume,
1189 /*********** SPIC (SNY6001) Device ***********/
1191 #define SONYPI_DEVICE_TYPE1 0x00000001
1192 #define SONYPI_DEVICE_TYPE2 0x00000002
1193 #define SONYPI_DEVICE_TYPE3 0x00000004
1194 #define SONYPI_DEVICE_TYPE4 0x00000008
1196 #define SONYPI_TYPE1_OFFSET 0x04
1197 #define SONYPI_TYPE2_OFFSET 0x12
1198 #define SONYPI_TYPE3_OFFSET 0x12
1199 #define SONYPI_TYPE4_OFFSET 0x12
1201 struct sony_pic_ioport {
1202 struct acpi_resource_io io1;
1203 struct acpi_resource_io io2;
1204 struct list_head list;
1207 struct sony_pic_irq {
1208 struct acpi_resource_irq irq;
1209 struct list_head list;
1212 struct sonypi_eventtypes {
1213 u8 data;
1214 unsigned long mask;
1215 struct sonypi_event *events;
1218 struct device_ctrl {
1219 int model;
1220 int (*handle_irq)(const u8, const u8);
1221 u16 evport_offset;
1222 u8 has_camera;
1223 u8 has_bluetooth;
1224 u8 has_wwan;
1225 struct sonypi_eventtypes *event_types;
1228 struct sony_pic_dev {
1229 struct device_ctrl *control;
1230 struct acpi_device *acpi_dev;
1231 struct sony_pic_irq *cur_irq;
1232 struct sony_pic_ioport *cur_ioport;
1233 struct list_head interrupts;
1234 struct list_head ioports;
1235 struct mutex lock;
1236 u8 camera_power;
1237 u8 bluetooth_power;
1238 u8 wwan_power;
1241 static struct sony_pic_dev spic_dev = {
1242 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1243 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1246 /* Event masks */
1247 #define SONYPI_JOGGER_MASK 0x00000001
1248 #define SONYPI_CAPTURE_MASK 0x00000002
1249 #define SONYPI_FNKEY_MASK 0x00000004
1250 #define SONYPI_BLUETOOTH_MASK 0x00000008
1251 #define SONYPI_PKEY_MASK 0x00000010
1252 #define SONYPI_BACK_MASK 0x00000020
1253 #define SONYPI_HELP_MASK 0x00000040
1254 #define SONYPI_LID_MASK 0x00000080
1255 #define SONYPI_ZOOM_MASK 0x00000100
1256 #define SONYPI_THUMBPHRASE_MASK 0x00000200
1257 #define SONYPI_MEYE_MASK 0x00000400
1258 #define SONYPI_MEMORYSTICK_MASK 0x00000800
1259 #define SONYPI_BATTERY_MASK 0x00001000
1260 #define SONYPI_WIRELESS_MASK 0x00002000
1262 struct sonypi_event {
1263 u8 data;
1264 u8 event;
1267 /* The set of possible button release events */
1268 static struct sonypi_event sonypi_releaseev[] = {
1269 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1270 { 0, 0 }
1273 /* The set of possible jogger events */
1274 static struct sonypi_event sonypi_joggerev[] = {
1275 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1276 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1277 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1278 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1279 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1280 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1281 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1282 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1283 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1284 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1285 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1286 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1287 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1288 { 0, 0 }
1291 /* The set of possible capture button events */
1292 static struct sonypi_event sonypi_captureev[] = {
1293 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1294 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1295 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
1296 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1297 { 0, 0 }
1300 /* The set of possible fnkeys events */
1301 static struct sonypi_event sonypi_fnkeyev[] = {
1302 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1303 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1304 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1305 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1306 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1307 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1308 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1309 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1310 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1311 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1312 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1313 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1314 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1315 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1316 { 0x21, SONYPI_EVENT_FNKEY_1 },
1317 { 0x22, SONYPI_EVENT_FNKEY_2 },
1318 { 0x31, SONYPI_EVENT_FNKEY_D },
1319 { 0x32, SONYPI_EVENT_FNKEY_E },
1320 { 0x33, SONYPI_EVENT_FNKEY_F },
1321 { 0x34, SONYPI_EVENT_FNKEY_S },
1322 { 0x35, SONYPI_EVENT_FNKEY_B },
1323 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1324 { 0, 0 }
1327 /* The set of possible program key events */
1328 static struct sonypi_event sonypi_pkeyev[] = {
1329 { 0x01, SONYPI_EVENT_PKEY_P1 },
1330 { 0x02, SONYPI_EVENT_PKEY_P2 },
1331 { 0x04, SONYPI_EVENT_PKEY_P3 },
1332 { 0, 0 }
1335 /* The set of possible bluetooth events */
1336 static struct sonypi_event sonypi_blueev[] = {
1337 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1338 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1339 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1340 { 0, 0 }
1343 /* The set of possible wireless events */
1344 static struct sonypi_event sonypi_wlessev[] = {
1345 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1346 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1347 { 0, 0 }
1350 /* The set of possible back button events */
1351 static struct sonypi_event sonypi_backev[] = {
1352 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1353 { 0, 0 }
1356 /* The set of possible help button events */
1357 static struct sonypi_event sonypi_helpev[] = {
1358 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1359 { 0, 0 }
1363 /* The set of possible lid events */
1364 static struct sonypi_event sonypi_lidev[] = {
1365 { 0x51, SONYPI_EVENT_LID_CLOSED },
1366 { 0x50, SONYPI_EVENT_LID_OPENED },
1367 { 0, 0 }
1370 /* The set of possible zoom events */
1371 static struct sonypi_event sonypi_zoomev[] = {
1372 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1373 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1374 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
1375 { 0, 0 }
1378 /* The set of possible thumbphrase events */
1379 static struct sonypi_event sonypi_thumbphraseev[] = {
1380 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1381 { 0, 0 }
1384 /* The set of possible motioneye camera events */
1385 static struct sonypi_event sonypi_meyeev[] = {
1386 { 0x00, SONYPI_EVENT_MEYE_FACE },
1387 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1388 { 0, 0 }
1391 /* The set of possible memorystick events */
1392 static struct sonypi_event sonypi_memorystickev[] = {
1393 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1394 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1395 { 0, 0 }
1398 /* The set of possible battery events */
1399 static struct sonypi_event sonypi_batteryev[] = {
1400 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1401 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1402 { 0, 0 }
1405 static struct sonypi_eventtypes type1_events[] = {
1406 { 0, 0xffffffff, sonypi_releaseev },
1407 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1408 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1409 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1410 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1411 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1412 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1413 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1414 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1415 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1416 { 0 },
1418 static struct sonypi_eventtypes type2_events[] = {
1419 { 0, 0xffffffff, sonypi_releaseev },
1420 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1421 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1422 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1423 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1424 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1425 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1426 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1427 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1428 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1429 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1430 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1431 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1432 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1433 { 0 },
1435 static struct sonypi_eventtypes type3_events[] = {
1436 { 0, 0xffffffff, sonypi_releaseev },
1437 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1438 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1439 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1440 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1441 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1442 { 0 },
1444 static struct sonypi_eventtypes type4_events[] = {
1445 { 0, 0xffffffff, sonypi_releaseev },
1446 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1447 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1448 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1449 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1450 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1451 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1452 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
1453 { 0 },
1456 /* low level spic calls */
1457 #define ITERATIONS_LONG 10000
1458 #define ITERATIONS_SHORT 10
1459 #define wait_on_command(command, iterations) { \
1460 unsigned int n = iterations; \
1461 while (--n && (command)) \
1462 udelay(1); \
1463 if (!n) \
1464 dprintk("command failed at %s : %s (line %d)\n", \
1465 __FILE__, __func__, __LINE__); \
1468 static u8 sony_pic_call1(u8 dev)
1470 u8 v1, v2;
1472 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1473 ITERATIONS_LONG);
1474 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1475 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1476 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
1477 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
1478 return v2;
1481 static u8 sony_pic_call2(u8 dev, u8 fn)
1483 u8 v1;
1485 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1486 ITERATIONS_LONG);
1487 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1488 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1489 ITERATIONS_LONG);
1490 outb(fn, spic_dev.cur_ioport->io1.minimum);
1491 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1492 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
1493 return v1;
1496 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1498 u8 v1;
1500 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1501 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1502 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1503 outb(fn, spic_dev.cur_ioport->io1.minimum);
1504 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1505 outb(v, spic_dev.cur_ioport->io1.minimum);
1506 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1507 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1508 dev, fn, v, v1);
1509 return v1;
1513 * minidrivers for SPIC models
1515 static int type4_handle_irq(const u8 data_mask, const u8 ev)
1518 * 0x31 could mean we have to take some extra action and wait for
1519 * the next irq for some Type4 models, it will generate a new
1520 * irq and we can read new data from the device:
1521 * - 0x5c and 0x5f requires 0xA0
1522 * - 0x61 requires 0xB3
1524 if (data_mask == 0x31) {
1525 if (ev == 0x5c || ev == 0x5f)
1526 sony_pic_call1(0xA0);
1527 else if (ev == 0x61)
1528 sony_pic_call1(0xB3);
1529 return 0;
1531 return 1;
1534 static struct device_ctrl spic_types[] = {
1536 .model = SONYPI_DEVICE_TYPE1,
1537 .handle_irq = NULL,
1538 .evport_offset = SONYPI_TYPE1_OFFSET,
1539 .event_types = type1_events,
1542 .model = SONYPI_DEVICE_TYPE2,
1543 .handle_irq = NULL,
1544 .evport_offset = SONYPI_TYPE2_OFFSET,
1545 .event_types = type2_events,
1548 .model = SONYPI_DEVICE_TYPE3,
1549 .handle_irq = NULL,
1550 .evport_offset = SONYPI_TYPE3_OFFSET,
1551 .event_types = type3_events,
1554 .model = SONYPI_DEVICE_TYPE4,
1555 .handle_irq = type4_handle_irq,
1556 .evport_offset = SONYPI_TYPE4_OFFSET,
1557 .event_types = type4_events,
1561 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1563 struct pci_dev *pcidev;
1565 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1566 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1567 if (pcidev) {
1568 dev->control = &spic_types[0];
1569 goto out;
1572 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1573 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1574 if (pcidev) {
1575 dev->control = &spic_types[2];
1576 goto out;
1579 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1580 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1581 if (pcidev) {
1582 dev->control = &spic_types[3];
1583 goto out;
1586 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1587 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1588 if (pcidev) {
1589 dev->control = &spic_types[3];
1590 goto out;
1593 /* default */
1594 dev->control = &spic_types[1];
1596 out:
1597 if (pcidev)
1598 pci_dev_put(pcidev);
1600 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1601 dev->control->model == SONYPI_DEVICE_TYPE1 ? 1 :
1602 dev->control->model == SONYPI_DEVICE_TYPE2 ? 2 :
1603 dev->control->model == SONYPI_DEVICE_TYPE3 ? 3 : 4);
1606 /* camera tests and poweron/poweroff */
1607 #define SONYPI_CAMERA_PICTURE 5
1608 #define SONYPI_CAMERA_CONTROL 0x10
1610 #define SONYPI_CAMERA_BRIGHTNESS 0
1611 #define SONYPI_CAMERA_CONTRAST 1
1612 #define SONYPI_CAMERA_HUE 2
1613 #define SONYPI_CAMERA_COLOR 3
1614 #define SONYPI_CAMERA_SHARPNESS 4
1616 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1617 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1618 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1619 #define SONYPI_CAMERA_MUTE_MASK 0x40
1621 /* the rest don't need a loop until not 0xff */
1622 #define SONYPI_CAMERA_AGC 6
1623 #define SONYPI_CAMERA_AGC_MASK 0x30
1624 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
1626 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1627 #define SONYPI_CAMERA_CONTROL 0x10
1629 #define SONYPI_CAMERA_STATUS 7
1630 #define SONYPI_CAMERA_STATUS_READY 0x2
1631 #define SONYPI_CAMERA_STATUS_POSITION 0x4
1633 #define SONYPI_DIRECTION_BACKWARDS 0x4
1635 #define SONYPI_CAMERA_REVISION 8
1636 #define SONYPI_CAMERA_ROMVERSION 9
1638 static int __sony_pic_camera_ready(void)
1640 u8 v;
1642 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1643 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1646 static int __sony_pic_camera_off(void)
1648 if (!camera) {
1649 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1650 return -ENODEV;
1653 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1654 SONYPI_CAMERA_MUTE_MASK),
1655 ITERATIONS_SHORT);
1657 if (spic_dev.camera_power) {
1658 sony_pic_call2(0x91, 0);
1659 spic_dev.camera_power = 0;
1661 return 0;
1664 static int __sony_pic_camera_on(void)
1666 int i, j, x;
1668 if (!camera) {
1669 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1670 return -ENODEV;
1673 if (spic_dev.camera_power)
1674 return 0;
1676 for (j = 5; j > 0; j--) {
1678 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1679 msleep(10);
1680 sony_pic_call1(0x93);
1682 for (i = 400; i > 0; i--) {
1683 if (__sony_pic_camera_ready())
1684 break;
1685 msleep(10);
1687 if (i)
1688 break;
1691 if (j == 0) {
1692 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
1693 return -ENODEV;
1696 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1697 0x5a),
1698 ITERATIONS_SHORT);
1700 spic_dev.camera_power = 1;
1701 return 0;
1704 /* External camera command (exported to the motion eye v4l driver) */
1705 int sony_pic_camera_command(int command, u8 value)
1707 if (!camera)
1708 return -EIO;
1710 mutex_lock(&spic_dev.lock);
1712 switch (command) {
1713 case SONY_PIC_COMMAND_SETCAMERA:
1714 if (value)
1715 __sony_pic_camera_on();
1716 else
1717 __sony_pic_camera_off();
1718 break;
1719 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
1720 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1721 ITERATIONS_SHORT);
1722 break;
1723 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
1724 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1725 ITERATIONS_SHORT);
1726 break;
1727 case SONY_PIC_COMMAND_SETCAMERAHUE:
1728 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1729 ITERATIONS_SHORT);
1730 break;
1731 case SONY_PIC_COMMAND_SETCAMERACOLOR:
1732 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1733 ITERATIONS_SHORT);
1734 break;
1735 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
1736 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1737 ITERATIONS_SHORT);
1738 break;
1739 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
1740 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1741 ITERATIONS_SHORT);
1742 break;
1743 case SONY_PIC_COMMAND_SETCAMERAAGC:
1744 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1745 ITERATIONS_SHORT);
1746 break;
1747 default:
1748 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1749 command);
1750 break;
1752 mutex_unlock(&spic_dev.lock);
1753 return 0;
1755 EXPORT_SYMBOL(sony_pic_camera_command);
1757 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1758 static void sony_pic_set_wwanpower(u8 state)
1760 state = !!state;
1761 mutex_lock(&spic_dev.lock);
1762 if (spic_dev.wwan_power == state) {
1763 mutex_unlock(&spic_dev.lock);
1764 return;
1766 sony_pic_call2(0xB0, state);
1767 spic_dev.wwan_power = state;
1768 mutex_unlock(&spic_dev.lock);
1771 static ssize_t sony_pic_wwanpower_store(struct device *dev,
1772 struct device_attribute *attr,
1773 const char *buffer, size_t count)
1775 unsigned long value;
1776 if (count > 31)
1777 return -EINVAL;
1779 value = simple_strtoul(buffer, NULL, 10);
1780 sony_pic_set_wwanpower(value);
1782 return count;
1785 static ssize_t sony_pic_wwanpower_show(struct device *dev,
1786 struct device_attribute *attr, char *buffer)
1788 ssize_t count;
1789 mutex_lock(&spic_dev.lock);
1790 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1791 mutex_unlock(&spic_dev.lock);
1792 return count;
1795 /* bluetooth subsystem power state */
1796 static void __sony_pic_set_bluetoothpower(u8 state)
1798 state = !!state;
1799 if (spic_dev.bluetooth_power == state)
1800 return;
1801 sony_pic_call2(0x96, state);
1802 sony_pic_call1(0x82);
1803 spic_dev.bluetooth_power = state;
1806 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1807 struct device_attribute *attr,
1808 const char *buffer, size_t count)
1810 unsigned long value;
1811 if (count > 31)
1812 return -EINVAL;
1814 value = simple_strtoul(buffer, NULL, 10);
1815 mutex_lock(&spic_dev.lock);
1816 __sony_pic_set_bluetoothpower(value);
1817 mutex_unlock(&spic_dev.lock);
1819 return count;
1822 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1823 struct device_attribute *attr, char *buffer)
1825 ssize_t count = 0;
1826 mutex_lock(&spic_dev.lock);
1827 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1828 mutex_unlock(&spic_dev.lock);
1829 return count;
1832 /* fan speed */
1833 /* FAN0 information (reverse engineered from ACPI tables) */
1834 #define SONY_PIC_FAN0_STATUS 0x93
1835 static int sony_pic_set_fanspeed(unsigned long value)
1837 return ec_write(SONY_PIC_FAN0_STATUS, value);
1840 static int sony_pic_get_fanspeed(u8 *value)
1842 return ec_read(SONY_PIC_FAN0_STATUS, value);
1845 static ssize_t sony_pic_fanspeed_store(struct device *dev,
1846 struct device_attribute *attr,
1847 const char *buffer, size_t count)
1849 unsigned long value;
1850 if (count > 31)
1851 return -EINVAL;
1853 value = simple_strtoul(buffer, NULL, 10);
1854 if (sony_pic_set_fanspeed(value))
1855 return -EIO;
1857 return count;
1860 static ssize_t sony_pic_fanspeed_show(struct device *dev,
1861 struct device_attribute *attr, char *buffer)
1863 u8 value = 0;
1864 if (sony_pic_get_fanspeed(&value))
1865 return -EIO;
1867 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1870 #define SPIC_ATTR(_name, _mode) \
1871 struct device_attribute spic_attr_##_name = __ATTR(_name, \
1872 _mode, sony_pic_## _name ##_show, \
1873 sony_pic_## _name ##_store)
1875 static SPIC_ATTR(bluetoothpower, 0644);
1876 static SPIC_ATTR(wwanpower, 0644);
1877 static SPIC_ATTR(fanspeed, 0644);
1879 static struct attribute *spic_attributes[] = {
1880 &spic_attr_bluetoothpower.attr,
1881 &spic_attr_wwanpower.attr,
1882 &spic_attr_fanspeed.attr,
1883 NULL
1886 static struct attribute_group spic_attribute_group = {
1887 .attrs = spic_attributes
1890 /******** SONYPI compatibility **********/
1891 #ifdef CONFIG_SONYPI_COMPAT
1893 /* battery / brightness / temperature addresses */
1894 #define SONYPI_BAT_FLAGS 0x81
1895 #define SONYPI_LCD_LIGHT 0x96
1896 #define SONYPI_BAT1_PCTRM 0xa0
1897 #define SONYPI_BAT1_LEFT 0xa2
1898 #define SONYPI_BAT1_MAXRT 0xa4
1899 #define SONYPI_BAT2_PCTRM 0xa8
1900 #define SONYPI_BAT2_LEFT 0xaa
1901 #define SONYPI_BAT2_MAXRT 0xac
1902 #define SONYPI_BAT1_MAXTK 0xb0
1903 #define SONYPI_BAT1_FULL 0xb2
1904 #define SONYPI_BAT2_MAXTK 0xb8
1905 #define SONYPI_BAT2_FULL 0xba
1906 #define SONYPI_TEMP_STATUS 0xC1
1908 struct sonypi_compat_s {
1909 struct fasync_struct *fifo_async;
1910 struct kfifo *fifo;
1911 spinlock_t fifo_lock;
1912 wait_queue_head_t fifo_proc_list;
1913 atomic_t open_count;
1915 static struct sonypi_compat_s sonypi_compat = {
1916 .open_count = ATOMIC_INIT(0),
1919 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1921 int retval;
1923 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1924 if (retval < 0)
1925 return retval;
1926 return 0;
1929 static int sonypi_misc_release(struct inode *inode, struct file *file)
1931 atomic_dec(&sonypi_compat.open_count);
1932 return 0;
1935 static int sonypi_misc_open(struct inode *inode, struct file *file)
1937 /* Flush input queue on first open */
1938 lock_kernel();
1939 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1940 kfifo_reset(sonypi_compat.fifo);
1941 unlock_kernel();
1942 return 0;
1945 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1946 size_t count, loff_t *pos)
1948 ssize_t ret;
1949 unsigned char c;
1951 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1952 (file->f_flags & O_NONBLOCK))
1953 return -EAGAIN;
1955 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1956 kfifo_len(sonypi_compat.fifo) != 0);
1957 if (ret)
1958 return ret;
1960 while (ret < count &&
1961 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1962 if (put_user(c, buf++))
1963 return -EFAULT;
1964 ret++;
1967 if (ret > 0) {
1968 struct inode *inode = file->f_path.dentry->d_inode;
1969 inode->i_atime = current_fs_time(inode->i_sb);
1972 return ret;
1975 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1977 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1978 if (kfifo_len(sonypi_compat.fifo))
1979 return POLLIN | POLLRDNORM;
1980 return 0;
1983 static int ec_read16(u8 addr, u16 *value)
1985 u8 val_lb, val_hb;
1986 if (ec_read(addr, &val_lb))
1987 return -1;
1988 if (ec_read(addr + 1, &val_hb))
1989 return -1;
1990 *value = val_lb | (val_hb << 8);
1991 return 0;
1994 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1995 unsigned int cmd, unsigned long arg)
1997 int ret = 0;
1998 void __user *argp = (void __user *)arg;
1999 u8 val8;
2000 u16 val16;
2001 int value;
2003 mutex_lock(&spic_dev.lock);
2004 switch (cmd) {
2005 case SONYPI_IOCGBRT:
2006 if (sony_backlight_device == NULL) {
2007 ret = -EIO;
2008 break;
2010 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2011 ret = -EIO;
2012 break;
2014 val8 = ((value & 0xff) - 1) << 5;
2015 if (copy_to_user(argp, &val8, sizeof(val8)))
2016 ret = -EFAULT;
2017 break;
2018 case SONYPI_IOCSBRT:
2019 if (sony_backlight_device == NULL) {
2020 ret = -EIO;
2021 break;
2023 if (copy_from_user(&val8, argp, sizeof(val8))) {
2024 ret = -EFAULT;
2025 break;
2027 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2028 (val8 >> 5) + 1, NULL)) {
2029 ret = -EIO;
2030 break;
2032 /* sync the backlight device status */
2033 sony_backlight_device->props.brightness =
2034 sony_backlight_get_brightness(sony_backlight_device);
2035 break;
2036 case SONYPI_IOCGBAT1CAP:
2037 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2038 ret = -EIO;
2039 break;
2041 if (copy_to_user(argp, &val16, sizeof(val16)))
2042 ret = -EFAULT;
2043 break;
2044 case SONYPI_IOCGBAT1REM:
2045 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2046 ret = -EIO;
2047 break;
2049 if (copy_to_user(argp, &val16, sizeof(val16)))
2050 ret = -EFAULT;
2051 break;
2052 case SONYPI_IOCGBAT2CAP:
2053 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2054 ret = -EIO;
2055 break;
2057 if (copy_to_user(argp, &val16, sizeof(val16)))
2058 ret = -EFAULT;
2059 break;
2060 case SONYPI_IOCGBAT2REM:
2061 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2062 ret = -EIO;
2063 break;
2065 if (copy_to_user(argp, &val16, sizeof(val16)))
2066 ret = -EFAULT;
2067 break;
2068 case SONYPI_IOCGBATFLAGS:
2069 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2070 ret = -EIO;
2071 break;
2073 val8 &= 0x07;
2074 if (copy_to_user(argp, &val8, sizeof(val8)))
2075 ret = -EFAULT;
2076 break;
2077 case SONYPI_IOCGBLUE:
2078 val8 = spic_dev.bluetooth_power;
2079 if (copy_to_user(argp, &val8, sizeof(val8)))
2080 ret = -EFAULT;
2081 break;
2082 case SONYPI_IOCSBLUE:
2083 if (copy_from_user(&val8, argp, sizeof(val8))) {
2084 ret = -EFAULT;
2085 break;
2087 __sony_pic_set_bluetoothpower(val8);
2088 break;
2089 /* FAN Controls */
2090 case SONYPI_IOCGFAN:
2091 if (sony_pic_get_fanspeed(&val8)) {
2092 ret = -EIO;
2093 break;
2095 if (copy_to_user(argp, &val8, sizeof(val8)))
2096 ret = -EFAULT;
2097 break;
2098 case SONYPI_IOCSFAN:
2099 if (copy_from_user(&val8, argp, sizeof(val8))) {
2100 ret = -EFAULT;
2101 break;
2103 if (sony_pic_set_fanspeed(val8))
2104 ret = -EIO;
2105 break;
2106 /* GET Temperature (useful under APM) */
2107 case SONYPI_IOCGTEMP:
2108 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2109 ret = -EIO;
2110 break;
2112 if (copy_to_user(argp, &val8, sizeof(val8)))
2113 ret = -EFAULT;
2114 break;
2115 default:
2116 ret = -EINVAL;
2118 mutex_unlock(&spic_dev.lock);
2119 return ret;
2122 static const struct file_operations sonypi_misc_fops = {
2123 .owner = THIS_MODULE,
2124 .read = sonypi_misc_read,
2125 .poll = sonypi_misc_poll,
2126 .open = sonypi_misc_open,
2127 .release = sonypi_misc_release,
2128 .fasync = sonypi_misc_fasync,
2129 .ioctl = sonypi_misc_ioctl,
2132 static struct miscdevice sonypi_misc_device = {
2133 .minor = MISC_DYNAMIC_MINOR,
2134 .name = "sonypi",
2135 .fops = &sonypi_misc_fops,
2138 static void sonypi_compat_report_event(u8 event)
2140 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
2141 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2142 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2145 static int sonypi_compat_init(void)
2147 int error;
2149 spin_lock_init(&sonypi_compat.fifo_lock);
2150 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
2151 &sonypi_compat.fifo_lock);
2152 if (IS_ERR(sonypi_compat.fifo)) {
2153 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2154 return PTR_ERR(sonypi_compat.fifo);
2157 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
2159 if (minor != -1)
2160 sonypi_misc_device.minor = minor;
2161 error = misc_register(&sonypi_misc_device);
2162 if (error) {
2163 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2164 goto err_free_kfifo;
2166 if (minor == -1)
2167 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
2168 sonypi_misc_device.minor);
2170 return 0;
2172 err_free_kfifo:
2173 kfifo_free(sonypi_compat.fifo);
2174 return error;
2177 static void sonypi_compat_exit(void)
2179 misc_deregister(&sonypi_misc_device);
2180 kfifo_free(sonypi_compat.fifo);
2182 #else
2183 static int sonypi_compat_init(void) { return 0; }
2184 static void sonypi_compat_exit(void) { }
2185 static void sonypi_compat_report_event(u8 event) { }
2186 #endif /* CONFIG_SONYPI_COMPAT */
2189 * ACPI callbacks
2191 static acpi_status
2192 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2194 u32 i;
2195 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2197 switch (resource->type) {
2198 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
2200 /* start IO enumeration */
2201 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2202 if (!ioport)
2203 return AE_ERROR;
2205 list_add(&ioport->list, &dev->ioports);
2206 return AE_OK;
2209 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
2210 /* end IO enumeration */
2211 return AE_OK;
2213 case ACPI_RESOURCE_TYPE_IRQ:
2215 struct acpi_resource_irq *p = &resource->data.irq;
2216 struct sony_pic_irq *interrupt = NULL;
2217 if (!p || !p->interrupt_count) {
2219 * IRQ descriptors may have no IRQ# bits set,
2220 * particularly those those w/ _STA disabled
2222 dprintk("Blank IRQ resource\n");
2223 return AE_OK;
2225 for (i = 0; i < p->interrupt_count; i++) {
2226 if (!p->interrupts[i]) {
2227 printk(KERN_WARNING DRV_PFX
2228 "Invalid IRQ %d\n",
2229 p->interrupts[i]);
2230 continue;
2232 interrupt = kzalloc(sizeof(*interrupt),
2233 GFP_KERNEL);
2234 if (!interrupt)
2235 return AE_ERROR;
2237 list_add(&interrupt->list, &dev->interrupts);
2238 interrupt->irq.triggering = p->triggering;
2239 interrupt->irq.polarity = p->polarity;
2240 interrupt->irq.sharable = p->sharable;
2241 interrupt->irq.interrupt_count = 1;
2242 interrupt->irq.interrupts[0] = p->interrupts[i];
2244 return AE_OK;
2246 case ACPI_RESOURCE_TYPE_IO:
2248 struct acpi_resource_io *io = &resource->data.io;
2249 struct sony_pic_ioport *ioport =
2250 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
2251 if (!io) {
2252 dprintk("Blank IO resource\n");
2253 return AE_OK;
2256 if (!ioport->io1.minimum) {
2257 memcpy(&ioport->io1, io, sizeof(*io));
2258 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2259 ioport->io1.address_length);
2261 else if (!ioport->io2.minimum) {
2262 memcpy(&ioport->io2, io, sizeof(*io));
2263 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2264 ioport->io2.address_length);
2266 else {
2267 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
2268 return AE_ERROR;
2270 return AE_OK;
2272 default:
2273 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2274 resource->type);
2276 case ACPI_RESOURCE_TYPE_END_TAG:
2277 return AE_OK;
2279 return AE_CTRL_TERMINATE;
2282 static int sony_pic_possible_resources(struct acpi_device *device)
2284 int result = 0;
2285 acpi_status status = AE_OK;
2287 if (!device)
2288 return -EINVAL;
2290 /* get device status */
2291 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2292 dprintk("Evaluating _STA\n");
2293 result = acpi_bus_get_status(device);
2294 if (result) {
2295 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2296 goto end;
2299 if (!device->status.enabled)
2300 dprintk("Device disabled\n");
2301 else
2302 dprintk("Device enabled\n");
2305 * Query and parse 'method'
2307 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2308 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2309 sony_pic_read_possible_resource, &spic_dev);
2310 if (ACPI_FAILURE(status)) {
2311 printk(KERN_WARNING DRV_PFX
2312 "Failure evaluating %s\n",
2313 METHOD_NAME__PRS);
2314 result = -ENODEV;
2316 end:
2317 return result;
2321 * Disable the spic device by calling its _DIS method
2323 static int sony_pic_disable(struct acpi_device *device)
2325 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2326 NULL);
2328 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
2329 return -ENXIO;
2331 dprintk("Device disabled\n");
2332 return 0;
2337 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2339 * Call _SRS to set current resources
2341 static int sony_pic_enable(struct acpi_device *device,
2342 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2344 acpi_status status;
2345 int result = 0;
2346 /* Type 1 resource layout is:
2347 * IO
2348 * IO
2349 * IRQNoFlags
2350 * End
2352 * Type 2 and 3 resource layout is:
2353 * IO
2354 * IRQNoFlags
2355 * End
2357 struct {
2358 struct acpi_resource res1;
2359 struct acpi_resource res2;
2360 struct acpi_resource res3;
2361 struct acpi_resource res4;
2362 } *resource;
2363 struct acpi_buffer buffer = { 0, NULL };
2365 if (!ioport || !irq)
2366 return -EINVAL;
2368 /* init acpi_buffer */
2369 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2370 if (!resource)
2371 return -ENOMEM;
2373 buffer.length = sizeof(*resource) + 1;
2374 buffer.pointer = resource;
2376 /* setup Type 1 resources */
2377 if (spic_dev.control->model == SONYPI_DEVICE_TYPE1) {
2379 /* setup io resources */
2380 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2381 resource->res1.length = sizeof(struct acpi_resource);
2382 memcpy(&resource->res1.data.io, &ioport->io1,
2383 sizeof(struct acpi_resource_io));
2385 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2386 resource->res2.length = sizeof(struct acpi_resource);
2387 memcpy(&resource->res2.data.io, &ioport->io2,
2388 sizeof(struct acpi_resource_io));
2390 /* setup irq resource */
2391 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2392 resource->res3.length = sizeof(struct acpi_resource);
2393 memcpy(&resource->res3.data.irq, &irq->irq,
2394 sizeof(struct acpi_resource_irq));
2395 /* we requested a shared irq */
2396 resource->res3.data.irq.sharable = ACPI_SHARED;
2398 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2401 /* setup Type 2/3 resources */
2402 else {
2403 /* setup io resource */
2404 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2405 resource->res1.length = sizeof(struct acpi_resource);
2406 memcpy(&resource->res1.data.io, &ioport->io1,
2407 sizeof(struct acpi_resource_io));
2409 /* setup irq resource */
2410 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2411 resource->res2.length = sizeof(struct acpi_resource);
2412 memcpy(&resource->res2.data.irq, &irq->irq,
2413 sizeof(struct acpi_resource_irq));
2414 /* we requested a shared irq */
2415 resource->res2.data.irq.sharable = ACPI_SHARED;
2417 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2420 /* Attempt to set the resource */
2421 dprintk("Evaluating _SRS\n");
2422 status = acpi_set_current_resources(device->handle, &buffer);
2424 /* check for total failure */
2425 if (ACPI_FAILURE(status)) {
2426 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
2427 result = -ENODEV;
2428 goto end;
2431 /* Necessary device initializations calls (from sonypi) */
2432 sony_pic_call1(0x82);
2433 sony_pic_call2(0x81, 0xff);
2434 sony_pic_call1(compat ? 0x92 : 0x82);
2436 end:
2437 kfree(resource);
2438 return result;
2441 /*****************
2443 * ISR: some event is available
2445 *****************/
2446 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2448 int i, j;
2449 u8 ev = 0;
2450 u8 data_mask = 0;
2451 u8 device_event = 0;
2453 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2455 ev = inb_p(dev->cur_ioport->io1.minimum);
2456 if (dev->cur_ioport->io2.minimum)
2457 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2458 else
2459 data_mask = inb_p(dev->cur_ioport->io1.minimum +
2460 dev->control->evport_offset);
2462 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2463 ev, data_mask, dev->cur_ioport->io1.minimum,
2464 dev->control->evport_offset);
2466 if (ev == 0x00 || ev == 0xff)
2467 return IRQ_HANDLED;
2469 for (i = 0; dev->control->event_types[i].mask; i++) {
2471 if ((data_mask & dev->control->event_types[i].data) !=
2472 dev->control->event_types[i].data)
2473 continue;
2475 if (!(mask & dev->control->event_types[i].mask))
2476 continue;
2478 for (j = 0; dev->control->event_types[i].events[j].event; j++) {
2479 if (ev == dev->control->event_types[i].events[j].data) {
2480 device_event =
2481 dev->control->
2482 event_types[i].events[j].event;
2483 goto found;
2487 /* Still not able to decode the event try to pass
2488 * it over to the minidriver
2490 if (dev->control->handle_irq &&
2491 dev->control->handle_irq(data_mask, ev) == 0)
2492 return IRQ_HANDLED;
2494 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2495 ev, data_mask, dev->cur_ioport->io1.minimum,
2496 dev->control->evport_offset);
2497 return IRQ_HANDLED;
2499 found:
2500 sony_laptop_report_input_event(device_event);
2501 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
2502 sonypi_compat_report_event(device_event);
2504 return IRQ_HANDLED;
2507 /*****************
2509 * ACPI driver
2511 *****************/
2512 static int sony_pic_remove(struct acpi_device *device, int type)
2514 struct sony_pic_ioport *io, *tmp_io;
2515 struct sony_pic_irq *irq, *tmp_irq;
2517 if (sony_pic_disable(device)) {
2518 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2519 return -ENXIO;
2522 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2523 release_region(spic_dev.cur_ioport->io1.minimum,
2524 spic_dev.cur_ioport->io1.address_length);
2525 if (spic_dev.cur_ioport->io2.minimum)
2526 release_region(spic_dev.cur_ioport->io2.minimum,
2527 spic_dev.cur_ioport->io2.address_length);
2529 sonypi_compat_exit();
2531 sony_laptop_remove_input();
2533 /* pf attrs */
2534 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2535 sony_pf_remove();
2537 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2538 list_del(&io->list);
2539 kfree(io);
2541 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2542 list_del(&irq->list);
2543 kfree(irq);
2545 spic_dev.cur_ioport = NULL;
2546 spic_dev.cur_irq = NULL;
2548 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2549 return 0;
2552 static int sony_pic_add(struct acpi_device *device)
2554 int result;
2555 struct sony_pic_ioport *io, *tmp_io;
2556 struct sony_pic_irq *irq, *tmp_irq;
2558 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2559 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2561 spic_dev.acpi_dev = device;
2562 strcpy(acpi_device_class(device), "sony/hotkey");
2563 sony_pic_detect_device_type(&spic_dev);
2564 mutex_init(&spic_dev.lock);
2566 /* read _PRS resources */
2567 result = sony_pic_possible_resources(device);
2568 if (result) {
2569 printk(KERN_ERR DRV_PFX
2570 "Unabe to read possible resources.\n");
2571 goto err_free_resources;
2574 /* setup input devices and helper fifo */
2575 result = sony_laptop_setup_input(device);
2576 if (result) {
2577 printk(KERN_ERR DRV_PFX
2578 "Unabe to create input devices.\n");
2579 goto err_free_resources;
2582 if (sonypi_compat_init())
2583 goto err_remove_input;
2585 /* request io port */
2586 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2587 if (request_region(io->io1.minimum, io->io1.address_length,
2588 "Sony Programable I/O Device")) {
2589 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2590 io->io1.minimum, io->io1.maximum,
2591 io->io1.address_length);
2592 /* Type 1 have 2 ioports */
2593 if (io->io2.minimum) {
2594 if (request_region(io->io2.minimum,
2595 io->io2.address_length,
2596 "Sony Programable I/O Device")) {
2597 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2598 io->io2.minimum, io->io2.maximum,
2599 io->io2.address_length);
2600 spic_dev.cur_ioport = io;
2601 break;
2603 else {
2604 dprintk("Unable to get I/O port2: "
2605 "0x%.4x (0x%.4x) + 0x%.2x\n",
2606 io->io2.minimum, io->io2.maximum,
2607 io->io2.address_length);
2608 release_region(io->io1.minimum,
2609 io->io1.address_length);
2612 else {
2613 spic_dev.cur_ioport = io;
2614 break;
2618 if (!spic_dev.cur_ioport) {
2619 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2620 result = -ENODEV;
2621 goto err_remove_compat;
2624 /* request IRQ */
2625 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
2626 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2627 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2628 dprintk("IRQ: %d - triggering: %d - "
2629 "polarity: %d - shr: %d\n",
2630 irq->irq.interrupts[0],
2631 irq->irq.triggering,
2632 irq->irq.polarity,
2633 irq->irq.sharable);
2634 spic_dev.cur_irq = irq;
2635 break;
2638 if (!spic_dev.cur_irq) {
2639 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2640 result = -ENODEV;
2641 goto err_release_region;
2644 /* set resource status _SRS */
2645 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2646 if (result) {
2647 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2648 goto err_free_irq;
2651 spic_dev.bluetooth_power = -1;
2652 /* create device attributes */
2653 result = sony_pf_add();
2654 if (result)
2655 goto err_disable_device;
2657 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2658 if (result)
2659 goto err_remove_pf;
2661 return 0;
2663 err_remove_pf:
2664 sony_pf_remove();
2666 err_disable_device:
2667 sony_pic_disable(device);
2669 err_free_irq:
2670 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2672 err_release_region:
2673 release_region(spic_dev.cur_ioport->io1.minimum,
2674 spic_dev.cur_ioport->io1.address_length);
2675 if (spic_dev.cur_ioport->io2.minimum)
2676 release_region(spic_dev.cur_ioport->io2.minimum,
2677 spic_dev.cur_ioport->io2.address_length);
2679 err_remove_compat:
2680 sonypi_compat_exit();
2682 err_remove_input:
2683 sony_laptop_remove_input();
2685 err_free_resources:
2686 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2687 list_del(&io->list);
2688 kfree(io);
2690 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2691 list_del(&irq->list);
2692 kfree(irq);
2694 spic_dev.cur_ioport = NULL;
2695 spic_dev.cur_irq = NULL;
2697 return result;
2700 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2702 if (sony_pic_disable(device))
2703 return -ENXIO;
2704 return 0;
2707 static int sony_pic_resume(struct acpi_device *device)
2709 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2710 return 0;
2713 static const struct acpi_device_id sony_pic_device_ids[] = {
2714 {SONY_PIC_HID, 0},
2715 {"", 0},
2718 static struct acpi_driver sony_pic_driver = {
2719 .name = SONY_PIC_DRIVER_NAME,
2720 .class = SONY_PIC_CLASS,
2721 .ids = sony_pic_device_ids,
2722 .owner = THIS_MODULE,
2723 .ops = {
2724 .add = sony_pic_add,
2725 .remove = sony_pic_remove,
2726 .suspend = sony_pic_suspend,
2727 .resume = sony_pic_resume,
2731 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2733 .ident = "Sony Vaio",
2734 .matches = {
2735 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2736 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2740 .ident = "Sony Vaio",
2741 .matches = {
2742 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2743 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2749 static int __init sony_laptop_init(void)
2751 int result;
2753 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2754 result = acpi_bus_register_driver(&sony_pic_driver);
2755 if (result) {
2756 printk(KERN_ERR DRV_PFX
2757 "Unable to register SPIC driver.");
2758 goto out;
2762 result = acpi_bus_register_driver(&sony_nc_driver);
2763 if (result) {
2764 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2765 goto out_unregister_pic;
2768 return 0;
2770 out_unregister_pic:
2771 if (!no_spic)
2772 acpi_bus_unregister_driver(&sony_pic_driver);
2773 out:
2774 return result;
2777 static void __exit sony_laptop_exit(void)
2779 acpi_bus_unregister_driver(&sony_nc_driver);
2780 if (!no_spic)
2781 acpi_bus_unregister_driver(&sony_pic_driver);
2784 module_init(sony_laptop_init);
2785 module_exit(sony_laptop_exit);