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
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/types.h>
50 #include <linux/backlight.h>
51 #include <linux/platform_device.h>
52 #include <linux/err.h>
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>
61 #include <acpi/acpi_drivers.h>
62 #include <acpi/acpi_bus.h>
63 #include <asm/uaccess.h>
64 #include <linux/sonypi.h>
65 #include <linux/sony-laptop.h>
66 #ifdef CONFIG_SONYPI_COMPAT
67 #include <linux/poll.h>
68 #include <linux/miscdevice.h>
71 #define DRV_PFX "sony-laptop: "
72 #define dprintk(msg...) do { \
73 if (debug) printk(KERN_WARNING DRV_PFX msg); \
76 #define SONY_LAPTOP_DRIVER_VERSION "0.5"
78 #define SONY_NC_CLASS "sony-nc"
79 #define SONY_NC_HID "SNY5001"
80 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
82 #define SONY_PIC_CLASS "sony-pic"
83 #define SONY_PIC_HID "SNY6001"
84 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
86 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
87 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
92 module_param(debug, int, 0);
93 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
94 "the development of this driver");
96 static int no_spic; /* = 0 */
97 module_param(no_spic, int, 0444);
98 MODULE_PARM_DESC(no_spic,
99 "set this if you don't want to enable the SPIC device");
101 static int compat; /* = 0 */
102 module_param(compat, int, 0444);
103 MODULE_PARM_DESC(compat,
104 "set this if you want to enable backward compatibility mode");
106 static unsigned long mask = 0xffffffff;
107 module_param(mask, ulong, 0644);
108 MODULE_PARM_DESC(mask,
109 "set this to the mask of event you want to enable (see doc)");
111 static int camera; /* = 0 */
112 module_param(camera, int, 0444);
113 MODULE_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)");
117 #ifdef CONFIG_SONYPI_COMPAT
118 static int minor = -1;
119 module_param(minor, int, 0);
120 MODULE_PARM_DESC(minor,
121 "minor number of the misc device for the SPIC compatibility code, "
122 "default is -1 (automatic)");
125 /*********** Input Devices ***********/
127 #define SONY_LAPTOP_BUF_SIZE 128
128 struct sony_laptop_input_s {
130 struct input_dev *jog_dev;
131 struct input_dev *key_dev;
133 spinlock_t fifo_lock;
134 struct workqueue_struct *wq;
136 static struct sony_laptop_input_s sony_laptop_input = {
137 .users = ATOMIC_INIT(0),
140 struct sony_laptop_keypress {
141 struct input_dev *dev;
145 /* Correspondance table between sonypi events
146 * and input layer indexes in the keymap
148 static int sony_laptop_input_index[] = {
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 */
213 static 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 */
265 /* release buttons after a short delay if pressed */
266 static void do_sony_laptop_release_key(struct work_struct *work)
268 struct sony_laptop_keypress kp;
270 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
271 sizeof(kp)) == sizeof(kp)) {
273 input_report_key(kp.dev, kp.key, 0);
277 static DECLARE_WORK(sony_laptop_release_key_work,
278 do_sony_laptop_release_key);
280 /* forward event to the input subsystem */
281 static void sony_laptop_report_input_event(u8 event)
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 };
287 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
288 /* Nothing, not all VAIOs generate this event */
295 case SONYPI_EVENT_JOGDIAL_UP:
296 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
297 input_report_rel(jog_dev, REL_WHEEL, 1);
301 case SONYPI_EVENT_JOGDIAL_DOWN:
302 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
303 input_report_rel(jog_dev, REL_WHEEL, -1);
308 case SONYPI_EVENT_JOGDIAL_PRESSED:
314 if (event > ARRAY_SIZE (sony_laptop_input_keycode_map)) {
315 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
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)
327 input_report_key(kp.dev, kp.key, 1);
328 /* we emit the scancode so we can always remap the key */
329 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
331 kfifo_put(sony_laptop_input.fifo,
332 (unsigned char *)&kp, sizeof(kp));
334 if (!work_pending(&sony_laptop_release_key_work))
335 queue_work(sony_laptop_input.wq,
336 &sony_laptop_release_key_work);
338 dprintk("unknown input event %.2x\n", event);
341 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
343 struct input_dev *jog_dev;
344 struct input_dev *key_dev;
348 /* don't run again if already initialized */
349 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
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);
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");
373 key_dev = input_allocate_device();
379 key_dev->name = "Sony Vaio Keys";
380 key_dev->id.bustype = BUS_ISA;
381 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
382 key_dev->dev.parent = &acpi_device->dev;
384 /* Initialize the Input Drivers: special keys */
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],
398 error = input_register_device(key_dev);
400 goto err_free_keydev;
402 sony_laptop_input.key_dev = key_dev;
405 jog_dev = input_allocate_device();
408 goto err_unregister_keydev;
411 jog_dev->name = "Sony Vaio Jogdial";
412 jog_dev->id.bustype = BUS_ISA;
413 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
414 key_dev->dev.parent = &acpi_device->dev;
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);
420 error = input_register_device(jog_dev);
422 goto err_free_jogdev;
424 sony_laptop_input.jog_dev = jog_dev;
429 input_free_device(jog_dev);
431 err_unregister_keydev:
432 input_unregister_device(key_dev);
433 /* to avoid kref underflow below at input_free_device */
437 input_free_device(key_dev);
440 destroy_workqueue(sony_laptop_input.wq);
443 kfifo_free(sony_laptop_input.fifo);
446 atomic_dec(&sony_laptop_input.users);
450 static void sony_laptop_remove_input(void)
452 /* cleanup only after the last user has gone */
453 if (!atomic_dec_and_test(&sony_laptop_input.users))
456 /* flush workqueue first */
457 flush_workqueue(sony_laptop_input.wq);
459 /* destroy input devs */
460 input_unregister_device(sony_laptop_input.key_dev);
461 sony_laptop_input.key_dev = NULL;
463 if (sony_laptop_input.jog_dev) {
464 input_unregister_device(sony_laptop_input.jog_dev);
465 sony_laptop_input.jog_dev = NULL;
468 destroy_workqueue(sony_laptop_input.wq);
469 kfifo_free(sony_laptop_input.fifo);
472 /*********** Platform Device ***********/
474 static atomic_t sony_pf_users = ATOMIC_INIT(0);
475 static struct platform_driver sony_pf_driver = {
477 .name = "sony-laptop",
478 .owner = THIS_MODULE,
481 static struct platform_device *sony_pf_device;
483 static int sony_pf_add(void)
487 /* don't run again if already initialized */
488 if (atomic_add_return(1, &sony_pf_users) > 1)
491 ret = platform_driver_register(&sony_pf_driver);
495 sony_pf_device = platform_device_alloc("sony-laptop", -1);
496 if (!sony_pf_device) {
498 goto out_platform_registered;
501 ret = platform_device_add(sony_pf_device);
503 goto out_platform_alloced;
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);
513 atomic_dec(&sony_pf_users);
517 static void sony_pf_remove(void)
519 /* deregister only after the last user has gone */
520 if (!atomic_dec_and_test(&sony_pf_users))
523 platform_device_del(sony_pf_device);
524 platform_device_put(sony_pf_device);
525 platform_driver_unregister(&sony_pf_driver);
528 /*********** SNC (SNY5001) Device ***********/
530 /* the device uses 1-based values, while the backlight subsystem uses
532 #define SONY_MAX_BRIGHTNESS 8
534 #define SNC_VALIDATE_IN 0
535 #define SNC_VALIDATE_OUT 1
537 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
539 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
540 const char *, size_t);
541 static int boolean_validate(const int, const int);
542 static int brightness_default_validate(const int, const int);
544 struct sony_nc_value {
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 */
548 int (*validate)(const int, const int); /* input/output validation */
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 */
555 #define SNC_HANDLE_NAMES(_name, _values...) \
556 static char *snc_##_name[] = { _values, NULL }
558 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
560 .name = __stringify(_name), \
561 .acpiget = _getters, \
562 .acpiset = _setters, \
563 .validate = _validate, \
565 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
568 #define SNC_HANDLE_NULL { .name = NULL }
570 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
572 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
573 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
575 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
576 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
578 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
579 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
581 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
582 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
584 SNC_HANDLE_NAMES(lidstate_get, "GLID");
586 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
587 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
589 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
590 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
592 SNC_HANDLE_NAMES(PID_get, "GPID");
594 SNC_HANDLE_NAMES(CTR_get, "GCTR");
595 SNC_HANDLE_NAMES(CTR_set, "SCTR");
597 SNC_HANDLE_NAMES(PCR_get, "GPCR");
598 SNC_HANDLE_NAMES(PCR_set, "SPCR");
600 SNC_HANDLE_NAMES(CMI_get, "GCMI");
601 SNC_HANDLE_NAMES(CMI_set, "SCMI");
603 static struct sony_nc_value sony_nc_values[] = {
604 SNC_HANDLE(brightness_default, snc_brightness_def_get,
605 snc_brightness_def_set, brightness_default_validate, 0),
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,
609 boolean_validate, 0),
610 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
611 boolean_validate, 1),
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),
618 /* unknown methods */
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),
626 static acpi_handle sony_nc_acpi_handle;
627 static struct acpi_device *sony_nc_acpi_device = NULL;
630 * acpi_evaluate_object wrappers
632 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
634 struct acpi_buffer output;
635 union acpi_object out_obj;
638 output.length = sizeof(out_obj);
639 output.pointer = &out_obj;
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;
647 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
652 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
655 struct acpi_object_list params;
656 union acpi_object in_obj;
657 struct acpi_buffer output;
658 union acpi_object out_obj;
662 params.pointer = &in_obj;
663 in_obj.type = ACPI_TYPE_INTEGER;
664 in_obj.integer.value = value;
666 output.length = sizeof(out_obj);
667 output.pointer = &out_obj;
669 status = acpi_evaluate_object(handle, name, ¶ms, &output);
670 if (status == AE_OK) {
671 if (result != NULL) {
672 if (out_obj.type != ACPI_TYPE_INTEGER) {
673 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
677 *result = out_obj.integer.value;
682 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
688 * sony_nc_values input/output validate functions
691 /* brightness_default_validate:
693 * manipulate input output values to keep consistency with the
694 * backlight framework for which brightness values are 0-based.
696 static int brightness_default_validate(const int direction, const int value)
699 case SNC_VALIDATE_OUT:
701 case SNC_VALIDATE_IN:
702 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
710 * on input validate boolean values 0/1, on output just pass the
713 static int boolean_validate(const int direction, const int value)
715 if (direction == SNC_VALIDATE_IN) {
716 if (value != 0 && value != 1)
723 * Sysfs show/store common to all sony_nc_values
725 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
729 struct sony_nc_value *item =
730 container_of(attr, struct sony_nc_value, devattr);
735 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
739 value = item->validate(SNC_VALIDATE_OUT, value);
741 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
744 static ssize_t sony_nc_sysfs_store(struct device *dev,
745 struct device_attribute *attr,
746 const char *buffer, size_t count)
749 struct sony_nc_value *item =
750 container_of(attr, struct sony_nc_value, devattr);
758 value = simple_strtoul(buffer, NULL, 10);
761 value = item->validate(SNC_VALIDATE_IN, value);
766 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
777 static int sony_backlight_update_status(struct backlight_device *bd)
779 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
780 bd->props.brightness + 1, NULL);
783 static int sony_backlight_get_brightness(struct backlight_device *bd)
787 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
789 /* brightness levels are 1-based, while backlight ones are 0-based */
793 static struct backlight_device *sony_backlight_device;
794 static struct backlight_ops sony_backlight_ops = {
795 .update_status = sony_backlight_update_status,
796 .get_brightness = sony_backlight_get_brightness,
800 * New SNC-only Vaios event mapping to driver known keys
802 struct sony_nc_event {
807 static struct sony_nc_event *sony_nc_events;
809 /* Vaio C* --maybe also FE*, N* and AR* ?-- special init sequence
812 static int sony_nc_C_enable(const struct dmi_system_id *id)
816 printk(KERN_NOTICE DRV_PFX "detected %s\n", id->ident);
818 sony_nc_events = id->driver_data;
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");
833 static 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 },
849 /* SNC-only model map */
850 static const struct dmi_system_id sony_nc_ids[] = {
852 .ident = "Sony Vaio FE Series",
853 .callback = sony_nc_C_enable,
854 .driver_data = sony_C_events,
856 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
857 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FE"),
861 .ident = "Sony Vaio FZ Series",
862 .callback = sony_nc_C_enable,
863 .driver_data = sony_C_events,
865 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
866 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FZ"),
870 .ident = "Sony Vaio C Series",
871 .callback = sony_nc_C_enable,
872 .driver_data = sony_C_events,
874 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
875 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-C"),
884 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
886 struct sony_nc_event *evmap;
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
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)
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.
902 if (acpi_callsetfunc(handle, "SN07", 0x0202, &result) < 0)
903 dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
909 for (evmap = sony_nc_events; evmap->event; evmap++) {
910 if (evmap->data == ev) {
916 dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
917 sony_laptop_report_input_event(ev);
918 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
921 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
922 void *context, void **return_value)
924 struct acpi_namespace_node *node;
925 union acpi_operand_object *operand;
927 node = (struct acpi_namespace_node *)handle;
928 operand = (union acpi_operand_object *)node->object;
930 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
931 (u32) operand->method.param_count);
939 static int sony_nc_resume(struct acpi_device *device)
941 struct sony_nc_value *item;
943 for (item = sony_nc_values; item->name; item++) {
948 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
951 printk("%s: %d\n", __FUNCTION__, ret);
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");
961 /* re-initialize models with specific requirements */
962 dmi_check_system(sony_nc_ids);
967 static int sony_nc_add(struct acpi_device *device)
972 struct sony_nc_value *item;
974 printk(KERN_INFO DRV_PFX "%s v%s.\n",
975 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
977 sony_nc_acpi_device = device;
978 strcpy(acpi_device_class(device), "sony/hotkey");
980 sony_nc_acpi_handle = device->handle;
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");
992 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
993 1, sony_walk_callback, NULL, NULL);
994 if (ACPI_FAILURE(status)) {
995 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
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,
1007 dprintk("_INI Method failed\n");
1010 /* setup input devices and helper fifo */
1011 result = sony_laptop_setup_input(device);
1013 printk(KERN_ERR DRV_PFX
1014 "Unabe to create input devices.\n");
1018 status = acpi_install_notify_handler(sony_nc_acpi_handle,
1020 sony_acpi_notify, NULL);
1021 if (ACPI_FAILURE(status)) {
1022 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
1027 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
1028 sony_backlight_device = backlight_device_register("sony", NULL,
1030 &sony_backlight_ops);
1032 if (IS_ERR(sony_backlight_device)) {
1033 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
1034 sony_backlight_device = NULL;
1036 sony_backlight_device->props.brightness =
1037 sony_backlight_get_brightness
1038 (sony_backlight_device);
1039 sony_backlight_device->props.max_brightness =
1040 SONY_MAX_BRIGHTNESS - 1;
1045 /* initialize models with specific requirements */
1046 dmi_check_system(sony_nc_ids);
1048 result = sony_pf_add();
1052 /* create sony_pf sysfs attributes related to the SNC device */
1053 for (item = sony_nc_values; item->name; ++item) {
1055 if (!debug && item->debug)
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,
1063 dprintk("Found %s getter: %s\n",
1064 item->name, *item->acpiget);
1065 item->devattr.attr.mode |= S_IRUGO;
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,
1075 dprintk("Found %s setter: %s\n",
1076 item->name, *item->acpiset);
1077 item->devattr.attr.mode |= S_IWUSR;
1082 if (item->devattr.attr.mode != 0) {
1084 device_create_file(&sony_pf_device->dev,
1094 for (item = sony_nc_values; item->name; ++item) {
1095 device_remove_file(&sony_pf_device->dev, &item->devattr);
1100 if (sony_backlight_device)
1101 backlight_device_unregister(sony_backlight_device);
1103 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
1106 if (ACPI_FAILURE(status))
1107 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
1110 sony_laptop_remove_input();
1116 static int sony_nc_remove(struct acpi_device *device, int type)
1119 struct sony_nc_value *item;
1121 if (sony_backlight_device)
1122 backlight_device_unregister(sony_backlight_device);
1124 sony_nc_acpi_device = NULL;
1126 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
1129 if (ACPI_FAILURE(status))
1130 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
1132 for (item = sony_nc_values; item->name; ++item) {
1133 device_remove_file(&sony_pf_device->dev, &item->devattr);
1137 sony_laptop_remove_input();
1138 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
1143 static const struct acpi_device_id sony_device_ids[] = {
1148 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1150 static const struct acpi_device_id sony_nc_device_ids[] = {
1155 static struct acpi_driver sony_nc_driver = {
1156 .name = SONY_NC_DRIVER_NAME,
1157 .class = SONY_NC_CLASS,
1158 .ids = sony_nc_device_ids,
1159 .owner = THIS_MODULE,
1162 .remove = sony_nc_remove,
1163 .resume = sony_nc_resume,
1167 /*********** SPIC (SNY6001) Device ***********/
1169 #define SONYPI_DEVICE_TYPE1 0x00000001
1170 #define SONYPI_DEVICE_TYPE2 0x00000002
1171 #define SONYPI_DEVICE_TYPE3 0x00000004
1173 #define SONYPI_TYPE1_OFFSET 0x04
1174 #define SONYPI_TYPE2_OFFSET 0x12
1175 #define SONYPI_TYPE3_OFFSET 0x12
1177 struct sony_pic_ioport {
1178 struct acpi_resource_io io1;
1179 struct acpi_resource_io io2;
1180 struct list_head list;
1183 struct sony_pic_irq {
1184 struct acpi_resource_irq irq;
1185 struct list_head list;
1188 struct sony_pic_dev {
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;
1202 static struct sony_pic_dev spic_dev = {
1203 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1204 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
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
1223 struct sonypi_event {
1228 /* The set of possible button release events */
1229 static struct sonypi_event sonypi_releaseev[] = {
1230 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1234 /* The set of possible jogger events */
1235 static 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 },
1252 /* The set of possible capture button events */
1253 static struct sonypi_event sonypi_captureev[] = {
1254 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1255 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1256 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1260 /* The set of possible fnkeys events */
1261 static 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 },
1287 /* The set of possible program key events */
1288 static 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 },
1296 /* The set of possible bluetooth events */
1297 static struct sonypi_event sonypi_blueev[] = {
1298 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1299 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1300 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1304 /* The set of possible wireless events */
1305 static struct sonypi_event sonypi_wlessev[] = {
1306 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1307 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1311 /* The set of possible back button events */
1312 static struct sonypi_event sonypi_backev[] = {
1313 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1317 /* The set of possible help button events */
1318 static struct sonypi_event sonypi_helpev[] = {
1319 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1324 /* The set of possible lid events */
1325 static struct sonypi_event sonypi_lidev[] = {
1326 { 0x51, SONYPI_EVENT_LID_CLOSED },
1327 { 0x50, SONYPI_EVENT_LID_OPENED },
1331 /* The set of possible zoom events */
1332 static struct sonypi_event sonypi_zoomev[] = {
1333 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1337 /* The set of possible thumbphrase events */
1338 static struct sonypi_event sonypi_thumbphraseev[] = {
1339 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1343 /* The set of possible motioneye camera events */
1344 static struct sonypi_event sonypi_meyeev[] = {
1345 { 0x00, SONYPI_EVENT_MEYE_FACE },
1346 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1350 /* The set of possible memorystick events */
1351 static struct sonypi_event sonypi_memorystickev[] = {
1352 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1353 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1357 /* The set of possible battery events */
1358 static struct sonypi_event sonypi_batteryev[] = {
1359 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1360 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1364 static struct sonypi_eventtypes {
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 },
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 },
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 },
1405 static int sony_pic_detect_device_type(void)
1407 struct pci_dev *pcidev;
1410 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1411 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1412 model = SONYPI_DEVICE_TYPE1;
1414 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1415 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1416 model = SONYPI_DEVICE_TYPE3;
1418 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1419 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1420 model = SONYPI_DEVICE_TYPE3;
1423 model = SONYPI_DEVICE_TYPE2;
1426 pci_dev_put(pcidev);
1428 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1429 model == SONYPI_DEVICE_TYPE1 ? 1 :
1430 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
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)) \
1441 dprintk("command failed at %s : %s (line %d)\n", \
1442 __FILE__, __FUNCTION__, __LINE__); \
1445 static u8 sony_pic_call1(u8 dev)
1449 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
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);
1454 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
1458 static u8 sony_pic_call2(u8 dev, u8 fn)
1462 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1464 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1465 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1467 outb(fn, spic_dev.cur_ioport->io1.minimum);
1468 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1469 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
1473 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
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);
1484 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1489 /* camera tests and poweron/poweroff */
1490 #define SONYPI_CAMERA_PICTURE 5
1491 #define SONYPI_CAMERA_CONTROL 0x10
1493 #define SONYPI_CAMERA_BRIGHTNESS 0
1494 #define SONYPI_CAMERA_CONTRAST 1
1495 #define SONYPI_CAMERA_HUE 2
1496 #define SONYPI_CAMERA_COLOR 3
1497 #define SONYPI_CAMERA_SHARPNESS 4
1499 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1500 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1501 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1502 #define SONYPI_CAMERA_MUTE_MASK 0x40
1504 /* the rest don't need a loop until not 0xff */
1505 #define SONYPI_CAMERA_AGC 6
1506 #define SONYPI_CAMERA_AGC_MASK 0x30
1507 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
1509 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1510 #define SONYPI_CAMERA_CONTROL 0x10
1512 #define SONYPI_CAMERA_STATUS 7
1513 #define SONYPI_CAMERA_STATUS_READY 0x2
1514 #define SONYPI_CAMERA_STATUS_POSITION 0x4
1516 #define SONYPI_DIRECTION_BACKWARDS 0x4
1518 #define SONYPI_CAMERA_REVISION 8
1519 #define SONYPI_CAMERA_ROMVERSION 9
1521 static int __sony_pic_camera_ready(void)
1525 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1526 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1529 static int __sony_pic_camera_off(void)
1532 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1536 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1537 SONYPI_CAMERA_MUTE_MASK),
1540 if (spic_dev.camera_power) {
1541 sony_pic_call2(0x91, 0);
1542 spic_dev.camera_power = 0;
1547 static int __sony_pic_camera_on(void)
1552 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1556 if (spic_dev.camera_power)
1559 for (j = 5; j > 0; j--) {
1561 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1563 sony_pic_call1(0x93);
1565 for (i = 400; i > 0; i--) {
1566 if (__sony_pic_camera_ready())
1575 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
1579 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1583 spic_dev.camera_power = 1;
1587 /* External camera command (exported to the motion eye v4l driver) */
1588 int sony_pic_camera_command(int command, u8 value)
1593 mutex_lock(&spic_dev.lock);
1596 case SONY_PIC_COMMAND_SETCAMERA:
1598 __sony_pic_camera_on();
1600 __sony_pic_camera_off();
1602 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
1603 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1606 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
1607 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1610 case SONY_PIC_COMMAND_SETCAMERAHUE:
1611 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1614 case SONY_PIC_COMMAND_SETCAMERACOLOR:
1615 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1618 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
1619 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1622 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
1623 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1626 case SONY_PIC_COMMAND_SETCAMERAAGC:
1627 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1631 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1635 mutex_unlock(&spic_dev.lock);
1638 EXPORT_SYMBOL(sony_pic_camera_command);
1640 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1641 static void sony_pic_set_wwanpower(u8 state)
1644 mutex_lock(&spic_dev.lock);
1645 if (spic_dev.wwan_power == state) {
1646 mutex_unlock(&spic_dev.lock);
1649 sony_pic_call2(0xB0, state);
1650 spic_dev.wwan_power = state;
1651 mutex_unlock(&spic_dev.lock);
1654 static ssize_t sony_pic_wwanpower_store(struct device *dev,
1655 struct device_attribute *attr,
1656 const char *buffer, size_t count)
1658 unsigned long value;
1662 value = simple_strtoul(buffer, NULL, 10);
1663 sony_pic_set_wwanpower(value);
1668 static ssize_t sony_pic_wwanpower_show(struct device *dev,
1669 struct device_attribute *attr, char *buffer)
1672 mutex_lock(&spic_dev.lock);
1673 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1674 mutex_unlock(&spic_dev.lock);
1678 /* bluetooth subsystem power state */
1679 static void __sony_pic_set_bluetoothpower(u8 state)
1682 if (spic_dev.bluetooth_power == state)
1684 sony_pic_call2(0x96, state);
1685 sony_pic_call1(0x82);
1686 spic_dev.bluetooth_power = state;
1689 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1690 struct device_attribute *attr,
1691 const char *buffer, size_t count)
1693 unsigned long value;
1697 value = simple_strtoul(buffer, NULL, 10);
1698 mutex_lock(&spic_dev.lock);
1699 __sony_pic_set_bluetoothpower(value);
1700 mutex_unlock(&spic_dev.lock);
1705 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1706 struct device_attribute *attr, char *buffer)
1709 mutex_lock(&spic_dev.lock);
1710 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1711 mutex_unlock(&spic_dev.lock);
1716 /* FAN0 information (reverse engineered from ACPI tables) */
1717 #define SONY_PIC_FAN0_STATUS 0x93
1718 static int sony_pic_set_fanspeed(unsigned long value)
1720 return ec_write(SONY_PIC_FAN0_STATUS, value);
1723 static int sony_pic_get_fanspeed(u8 *value)
1725 return ec_read(SONY_PIC_FAN0_STATUS, value);
1728 static ssize_t sony_pic_fanspeed_store(struct device *dev,
1729 struct device_attribute *attr,
1730 const char *buffer, size_t count)
1732 unsigned long value;
1736 value = simple_strtoul(buffer, NULL, 10);
1737 if (sony_pic_set_fanspeed(value))
1743 static ssize_t sony_pic_fanspeed_show(struct device *dev,
1744 struct device_attribute *attr, char *buffer)
1747 if (sony_pic_get_fanspeed(&value))
1750 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1753 #define SPIC_ATTR(_name, _mode) \
1754 struct device_attribute spic_attr_##_name = __ATTR(_name, \
1755 _mode, sony_pic_## _name ##_show, \
1756 sony_pic_## _name ##_store)
1758 static SPIC_ATTR(bluetoothpower, 0644);
1759 static SPIC_ATTR(wwanpower, 0644);
1760 static SPIC_ATTR(fanspeed, 0644);
1762 static struct attribute *spic_attributes[] = {
1763 &spic_attr_bluetoothpower.attr,
1764 &spic_attr_wwanpower.attr,
1765 &spic_attr_fanspeed.attr,
1769 static struct attribute_group spic_attribute_group = {
1770 .attrs = spic_attributes
1773 /******** SONYPI compatibility **********/
1774 #ifdef CONFIG_SONYPI_COMPAT
1776 /* battery / brightness / temperature addresses */
1777 #define SONYPI_BAT_FLAGS 0x81
1778 #define SONYPI_LCD_LIGHT 0x96
1779 #define SONYPI_BAT1_PCTRM 0xa0
1780 #define SONYPI_BAT1_LEFT 0xa2
1781 #define SONYPI_BAT1_MAXRT 0xa4
1782 #define SONYPI_BAT2_PCTRM 0xa8
1783 #define SONYPI_BAT2_LEFT 0xaa
1784 #define SONYPI_BAT2_MAXRT 0xac
1785 #define SONYPI_BAT1_MAXTK 0xb0
1786 #define SONYPI_BAT1_FULL 0xb2
1787 #define SONYPI_BAT2_MAXTK 0xb8
1788 #define SONYPI_BAT2_FULL 0xba
1789 #define SONYPI_TEMP_STATUS 0xC1
1791 struct sonypi_compat_s {
1792 struct fasync_struct *fifo_async;
1794 spinlock_t fifo_lock;
1795 wait_queue_head_t fifo_proc_list;
1796 atomic_t open_count;
1798 static struct sonypi_compat_s sonypi_compat = {
1799 .open_count = ATOMIC_INIT(0),
1802 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1806 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1812 static int sonypi_misc_release(struct inode *inode, struct file *file)
1814 sonypi_misc_fasync(-1, file, 0);
1815 atomic_dec(&sonypi_compat.open_count);
1819 static int sonypi_misc_open(struct inode *inode, struct file *file)
1821 /* Flush input queue on first open */
1822 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1823 kfifo_reset(sonypi_compat.fifo);
1827 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1828 size_t count, loff_t *pos)
1833 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1834 (file->f_flags & O_NONBLOCK))
1837 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1838 kfifo_len(sonypi_compat.fifo) != 0);
1842 while (ret < count &&
1843 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1844 if (put_user(c, buf++))
1850 struct inode *inode = file->f_path.dentry->d_inode;
1851 inode->i_atime = current_fs_time(inode->i_sb);
1857 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1859 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1860 if (kfifo_len(sonypi_compat.fifo))
1861 return POLLIN | POLLRDNORM;
1865 static int ec_read16(u8 addr, u16 *value)
1868 if (ec_read(addr, &val_lb))
1870 if (ec_read(addr + 1, &val_hb))
1872 *value = val_lb | (val_hb << 8);
1876 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1877 unsigned int cmd, unsigned long arg)
1880 void __user *argp = (void __user *)arg;
1885 mutex_lock(&spic_dev.lock);
1887 case SONYPI_IOCGBRT:
1888 if (sony_backlight_device == NULL) {
1892 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1896 val8 = ((value & 0xff) - 1) << 5;
1897 if (copy_to_user(argp, &val8, sizeof(val8)))
1900 case SONYPI_IOCSBRT:
1901 if (sony_backlight_device == NULL) {
1905 if (copy_from_user(&val8, argp, sizeof(val8))) {
1909 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1910 (val8 >> 5) + 1, NULL)) {
1914 /* sync the backlight device status */
1915 sony_backlight_device->props.brightness =
1916 sony_backlight_get_brightness(sony_backlight_device);
1918 case SONYPI_IOCGBAT1CAP:
1919 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1923 if (copy_to_user(argp, &val16, sizeof(val16)))
1926 case SONYPI_IOCGBAT1REM:
1927 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1931 if (copy_to_user(argp, &val16, sizeof(val16)))
1934 case SONYPI_IOCGBAT2CAP:
1935 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1939 if (copy_to_user(argp, &val16, sizeof(val16)))
1942 case SONYPI_IOCGBAT2REM:
1943 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1947 if (copy_to_user(argp, &val16, sizeof(val16)))
1950 case SONYPI_IOCGBATFLAGS:
1951 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1956 if (copy_to_user(argp, &val8, sizeof(val8)))
1959 case SONYPI_IOCGBLUE:
1960 val8 = spic_dev.bluetooth_power;
1961 if (copy_to_user(argp, &val8, sizeof(val8)))
1964 case SONYPI_IOCSBLUE:
1965 if (copy_from_user(&val8, argp, sizeof(val8))) {
1969 __sony_pic_set_bluetoothpower(val8);
1972 case SONYPI_IOCGFAN:
1973 if (sony_pic_get_fanspeed(&val8)) {
1977 if (copy_to_user(argp, &val8, sizeof(val8)))
1980 case SONYPI_IOCSFAN:
1981 if (copy_from_user(&val8, argp, sizeof(val8))) {
1985 if (sony_pic_set_fanspeed(val8))
1988 /* GET Temperature (useful under APM) */
1989 case SONYPI_IOCGTEMP:
1990 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1994 if (copy_to_user(argp, &val8, sizeof(val8)))
2000 mutex_unlock(&spic_dev.lock);
2004 static const struct file_operations sonypi_misc_fops = {
2005 .owner = THIS_MODULE,
2006 .read = sonypi_misc_read,
2007 .poll = sonypi_misc_poll,
2008 .open = sonypi_misc_open,
2009 .release = sonypi_misc_release,
2010 .fasync = sonypi_misc_fasync,
2011 .ioctl = sonypi_misc_ioctl,
2014 static struct miscdevice sonypi_misc_device = {
2015 .minor = MISC_DYNAMIC_MINOR,
2017 .fops = &sonypi_misc_fops,
2020 static void sonypi_compat_report_event(u8 event)
2022 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
2023 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2024 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2027 static int sonypi_compat_init(void)
2031 spin_lock_init(&sonypi_compat.fifo_lock);
2032 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
2033 &sonypi_compat.fifo_lock);
2034 if (IS_ERR(sonypi_compat.fifo)) {
2035 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2036 return PTR_ERR(sonypi_compat.fifo);
2039 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
2042 sonypi_misc_device.minor = minor;
2043 error = misc_register(&sonypi_misc_device);
2045 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2046 goto err_free_kfifo;
2049 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
2050 sonypi_misc_device.minor);
2055 kfifo_free(sonypi_compat.fifo);
2059 static void sonypi_compat_exit(void)
2061 misc_deregister(&sonypi_misc_device);
2062 kfifo_free(sonypi_compat.fifo);
2065 static int sonypi_compat_init(void) { return 0; }
2066 static void sonypi_compat_exit(void) { }
2067 static void sonypi_compat_report_event(u8 event) { }
2068 #endif /* CONFIG_SONYPI_COMPAT */
2074 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2077 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2079 switch (resource->type) {
2080 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
2082 /* start IO enumeration */
2083 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2087 list_add(&ioport->list, &dev->ioports);
2091 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
2092 /* end IO enumeration */
2095 case ACPI_RESOURCE_TYPE_IRQ:
2097 struct acpi_resource_irq *p = &resource->data.irq;
2098 struct sony_pic_irq *interrupt = NULL;
2099 if (!p || !p->interrupt_count) {
2101 * IRQ descriptors may have no IRQ# bits set,
2102 * particularly those those w/ _STA disabled
2104 dprintk("Blank IRQ resource\n");
2107 for (i = 0; i < p->interrupt_count; i++) {
2108 if (!p->interrupts[i]) {
2109 printk(KERN_WARNING DRV_PFX
2114 interrupt = kzalloc(sizeof(*interrupt),
2119 list_add(&interrupt->list, &dev->interrupts);
2120 interrupt->irq.triggering = p->triggering;
2121 interrupt->irq.polarity = p->polarity;
2122 interrupt->irq.sharable = p->sharable;
2123 interrupt->irq.interrupt_count = 1;
2124 interrupt->irq.interrupts[0] = p->interrupts[i];
2128 case ACPI_RESOURCE_TYPE_IO:
2130 struct acpi_resource_io *io = &resource->data.io;
2131 struct sony_pic_ioport *ioport =
2132 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
2134 dprintk("Blank IO resource\n");
2138 if (!ioport->io1.minimum) {
2139 memcpy(&ioport->io1, io, sizeof(*io));
2140 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2141 ioport->io1.address_length);
2143 else if (!ioport->io2.minimum) {
2144 memcpy(&ioport->io2, io, sizeof(*io));
2145 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2146 ioport->io2.address_length);
2149 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
2155 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2158 case ACPI_RESOURCE_TYPE_END_TAG:
2161 return AE_CTRL_TERMINATE;
2164 static int sony_pic_possible_resources(struct acpi_device *device)
2167 acpi_status status = AE_OK;
2172 /* get device status */
2173 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2174 dprintk("Evaluating _STA\n");
2175 result = acpi_bus_get_status(device);
2177 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2181 if (!device->status.enabled)
2182 dprintk("Device disabled\n");
2184 dprintk("Device enabled\n");
2187 * Query and parse 'method'
2189 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2190 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2191 sony_pic_read_possible_resource, &spic_dev);
2192 if (ACPI_FAILURE(status)) {
2193 printk(KERN_WARNING DRV_PFX
2194 "Failure evaluating %s\n",
2203 * Disable the spic device by calling its _DIS method
2205 static int sony_pic_disable(struct acpi_device *device)
2207 if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
2208 "_DIS", NULL, NULL)))
2211 dprintk("Device disabled\n");
2217 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2219 * Call _SRS to set current resources
2221 static int sony_pic_enable(struct acpi_device *device,
2222 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2226 /* Type 1 resource layout is:
2232 * Type 2 and 3 resource layout is:
2238 struct acpi_resource res1;
2239 struct acpi_resource res2;
2240 struct acpi_resource res3;
2241 struct acpi_resource res4;
2243 struct acpi_buffer buffer = { 0, NULL };
2245 if (!ioport || !irq)
2248 /* init acpi_buffer */
2249 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2253 buffer.length = sizeof(*resource) + 1;
2254 buffer.pointer = resource;
2256 /* setup Type 1 resources */
2257 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
2259 /* setup io resources */
2260 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2261 resource->res1.length = sizeof(struct acpi_resource);
2262 memcpy(&resource->res1.data.io, &ioport->io1,
2263 sizeof(struct acpi_resource_io));
2265 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2266 resource->res2.length = sizeof(struct acpi_resource);
2267 memcpy(&resource->res2.data.io, &ioport->io2,
2268 sizeof(struct acpi_resource_io));
2270 /* setup irq resource */
2271 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2272 resource->res3.length = sizeof(struct acpi_resource);
2273 memcpy(&resource->res3.data.irq, &irq->irq,
2274 sizeof(struct acpi_resource_irq));
2275 /* we requested a shared irq */
2276 resource->res3.data.irq.sharable = ACPI_SHARED;
2278 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2281 /* setup Type 2/3 resources */
2283 /* setup io resource */
2284 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2285 resource->res1.length = sizeof(struct acpi_resource);
2286 memcpy(&resource->res1.data.io, &ioport->io1,
2287 sizeof(struct acpi_resource_io));
2289 /* setup irq resource */
2290 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2291 resource->res2.length = sizeof(struct acpi_resource);
2292 memcpy(&resource->res2.data.irq, &irq->irq,
2293 sizeof(struct acpi_resource_irq));
2294 /* we requested a shared irq */
2295 resource->res2.data.irq.sharable = ACPI_SHARED;
2297 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2300 /* Attempt to set the resource */
2301 dprintk("Evaluating _SRS\n");
2302 status = acpi_set_current_resources(device->handle, &buffer);
2304 /* check for total failure */
2305 if (ACPI_FAILURE(status)) {
2306 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
2311 /* Necessary device initializations calls (from sonypi) */
2312 sony_pic_call1(0x82);
2313 sony_pic_call2(0x81, 0xff);
2314 sony_pic_call1(compat ? 0x92 : 0x82);
2323 * ISR: some event is available
2326 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2331 u8 device_event = 0;
2333 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2335 ev = inb_p(dev->cur_ioport->io1.minimum);
2336 if (dev->cur_ioport->io2.minimum)
2337 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2339 data_mask = inb_p(dev->cur_ioport->io1.minimum + dev->evport_offset);
2341 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2342 ev, data_mask, dev->cur_ioport->io1.minimum, dev->evport_offset);
2344 if (ev == 0x00 || ev == 0xff)
2347 for (i = 0; sony_pic_eventtypes[i].model; i++) {
2349 if (spic_dev.model != sony_pic_eventtypes[i].model)
2352 if ((data_mask & sony_pic_eventtypes[i].data) !=
2353 sony_pic_eventtypes[i].data)
2356 if (!(mask & sony_pic_eventtypes[i].mask))
2359 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2360 if (ev == sony_pic_eventtypes[i].events[j].data) {
2362 sony_pic_eventtypes[i].events[j].event;
2370 sony_laptop_report_input_event(device_event);
2371 acpi_bus_generate_proc_event(spic_dev.acpi_dev, 1, device_event);
2372 sonypi_compat_report_event(device_event);
2382 static int sony_pic_remove(struct acpi_device *device, int type)
2384 struct sony_pic_ioport *io, *tmp_io;
2385 struct sony_pic_irq *irq, *tmp_irq;
2387 if (sony_pic_disable(device)) {
2388 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2392 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2393 release_region(spic_dev.cur_ioport->io1.minimum,
2394 spic_dev.cur_ioport->io1.address_length);
2395 if (spic_dev.cur_ioport->io2.minimum)
2396 release_region(spic_dev.cur_ioport->io2.minimum,
2397 spic_dev.cur_ioport->io2.address_length);
2399 sonypi_compat_exit();
2401 sony_laptop_remove_input();
2404 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2407 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2408 list_del(&io->list);
2411 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2412 list_del(&irq->list);
2415 spic_dev.cur_ioport = NULL;
2416 spic_dev.cur_irq = NULL;
2418 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2422 static int sony_pic_add(struct acpi_device *device)
2425 struct sony_pic_ioport *io, *tmp_io;
2426 struct sony_pic_irq *irq, *tmp_irq;
2428 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2429 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2431 spic_dev.acpi_dev = device;
2432 strcpy(acpi_device_class(device), "sony/hotkey");
2433 spic_dev.model = sony_pic_detect_device_type();
2434 mutex_init(&spic_dev.lock);
2436 /* model specific characteristics */
2437 switch(spic_dev.model) {
2438 case SONYPI_DEVICE_TYPE1:
2439 spic_dev.evport_offset = SONYPI_TYPE1_OFFSET;
2441 case SONYPI_DEVICE_TYPE3:
2442 spic_dev.evport_offset = SONYPI_TYPE3_OFFSET;
2444 case SONYPI_DEVICE_TYPE2:
2446 spic_dev.evport_offset = SONYPI_TYPE2_OFFSET;
2450 /* read _PRS resources */
2451 result = sony_pic_possible_resources(device);
2453 printk(KERN_ERR DRV_PFX
2454 "Unabe to read possible resources.\n");
2455 goto err_free_resources;
2458 /* setup input devices and helper fifo */
2459 result = sony_laptop_setup_input(device);
2461 printk(KERN_ERR DRV_PFX
2462 "Unabe to create input devices.\n");
2463 goto err_free_resources;
2466 if (sonypi_compat_init())
2467 goto err_remove_input;
2469 /* request io port */
2470 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2471 if (request_region(io->io1.minimum, io->io1.address_length,
2472 "Sony Programable I/O Device")) {
2473 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2474 io->io1.minimum, io->io1.maximum,
2475 io->io1.address_length);
2476 /* Type 1 have 2 ioports */
2477 if (io->io2.minimum) {
2478 if (request_region(io->io2.minimum,
2479 io->io2.address_length,
2480 "Sony Programable I/O Device")) {
2481 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2482 io->io2.minimum, io->io2.maximum,
2483 io->io2.address_length);
2484 spic_dev.cur_ioport = io;
2488 dprintk("Unable to get I/O port2: "
2489 "0x%.4x (0x%.4x) + 0x%.2x\n",
2490 io->io2.minimum, io->io2.maximum,
2491 io->io2.address_length);
2492 release_region(io->io1.minimum,
2493 io->io1.address_length);
2497 spic_dev.cur_ioport = io;
2502 if (!spic_dev.cur_ioport) {
2503 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2505 goto err_remove_compat;
2509 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
2510 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2511 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2512 dprintk("IRQ: %d - triggering: %d - "
2513 "polarity: %d - shr: %d\n",
2514 irq->irq.interrupts[0],
2515 irq->irq.triggering,
2518 spic_dev.cur_irq = irq;
2522 if (!spic_dev.cur_irq) {
2523 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2525 goto err_release_region;
2528 /* set resource status _SRS */
2529 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2531 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2535 spic_dev.bluetooth_power = -1;
2536 /* create device attributes */
2537 result = sony_pf_add();
2539 goto err_disable_device;
2541 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2551 sony_pic_disable(device);
2554 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2557 release_region(spic_dev.cur_ioport->io1.minimum,
2558 spic_dev.cur_ioport->io1.address_length);
2559 if (spic_dev.cur_ioport->io2.minimum)
2560 release_region(spic_dev.cur_ioport->io2.minimum,
2561 spic_dev.cur_ioport->io2.address_length);
2564 sonypi_compat_exit();
2567 sony_laptop_remove_input();
2570 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2571 list_del(&io->list);
2574 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2575 list_del(&irq->list);
2578 spic_dev.cur_ioport = NULL;
2579 spic_dev.cur_irq = NULL;
2584 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2586 if (sony_pic_disable(device))
2591 static int sony_pic_resume(struct acpi_device *device)
2593 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2597 static const struct acpi_device_id sony_pic_device_ids[] = {
2602 static struct acpi_driver sony_pic_driver = {
2603 .name = SONY_PIC_DRIVER_NAME,
2604 .class = SONY_PIC_CLASS,
2605 .ids = sony_pic_device_ids,
2606 .owner = THIS_MODULE,
2608 .add = sony_pic_add,
2609 .remove = sony_pic_remove,
2610 .suspend = sony_pic_suspend,
2611 .resume = sony_pic_resume,
2615 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2617 .ident = "Sony Vaio",
2619 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2620 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2624 .ident = "Sony Vaio",
2626 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2627 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2633 static int __init sony_laptop_init(void)
2637 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2638 result = acpi_bus_register_driver(&sony_pic_driver);
2640 printk(KERN_ERR DRV_PFX
2641 "Unable to register SPIC driver.");
2646 result = acpi_bus_register_driver(&sony_nc_driver);
2648 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2649 goto out_unregister_pic;
2656 acpi_bus_unregister_driver(&sony_pic_driver);
2661 static void __exit sony_laptop_exit(void)
2663 acpi_bus_unregister_driver(&sony_nc_driver);
2665 acpi_bus_unregister_driver(&sony_pic_driver);
2668 module_init(sony_laptop_init);
2669 module_exit(sony_laptop_exit);