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/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>
72 #define DRV_PFX "sony-laptop: "
73 #define dprintk(msg...) do { \
74 if (debug) printk(KERN_WARNING DRV_PFX msg); \
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);
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)");
126 /*********** Input Devices ***********/
128 #define SONY_LAPTOP_BUF_SIZE 128
129 struct sony_laptop_input_s {
131 struct input_dev *jog_dev;
132 struct input_dev *key_dev;
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;
146 /* Correspondance table between sonypi events
147 * and input layer indexes in the keymap
149 static int sony_laptop_input_index[] = {
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 */
216 static int sony_laptop_input_keycode_map[] = {
217 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
218 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
219 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
220 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
221 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
222 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
223 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
224 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
225 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
226 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
227 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
228 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
229 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
230 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
231 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
232 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
233 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
234 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
235 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
236 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
237 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
238 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
239 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
240 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
241 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
242 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
243 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
244 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
245 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
246 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
247 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
248 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
249 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
250 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
251 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
252 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
253 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
254 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
255 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
256 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
257 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
258 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
259 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
260 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
261 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
262 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
263 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
264 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
265 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
266 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
267 KEY_ZOOMOUT /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
270 /* release buttons after a short delay if pressed */
271 static void do_sony_laptop_release_key(struct work_struct *work)
273 struct sony_laptop_keypress kp;
275 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
276 sizeof(kp)) == sizeof(kp)) {
278 input_report_key(kp.dev, kp.key, 0);
282 static DECLARE_WORK(sony_laptop_release_key_work,
283 do_sony_laptop_release_key);
285 /* forward event to the input subsystem */
286 static void sony_laptop_report_input_event(u8 event)
288 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
289 struct input_dev *key_dev = sony_laptop_input.key_dev;
290 struct sony_laptop_keypress kp = { NULL };
292 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
293 /* Nothing, not all VAIOs generate this event */
300 case SONYPI_EVENT_JOGDIAL_UP:
301 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
302 input_report_rel(jog_dev, REL_WHEEL, 1);
306 case SONYPI_EVENT_JOGDIAL_DOWN:
307 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
308 input_report_rel(jog_dev, REL_WHEEL, -1);
313 case SONYPI_EVENT_JOGDIAL_PRESSED:
319 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
320 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
323 if (sony_laptop_input_index[event] != -1) {
324 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
325 if (kp.key != KEY_UNKNOWN)
332 input_report_key(kp.dev, kp.key, 1);
333 /* we emit the scancode so we can always remap the key */
334 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
336 kfifo_put(sony_laptop_input.fifo,
337 (unsigned char *)&kp, sizeof(kp));
339 if (!work_pending(&sony_laptop_release_key_work))
340 queue_work(sony_laptop_input.wq,
341 &sony_laptop_release_key_work);
343 dprintk("unknown input event %.2x\n", event);
346 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
348 struct input_dev *jog_dev;
349 struct input_dev *key_dev;
353 /* don't run again if already initialized */
354 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
358 spin_lock_init(&sony_laptop_input.fifo_lock);
359 sony_laptop_input.fifo =
360 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
361 &sony_laptop_input.fifo_lock);
362 if (IS_ERR(sony_laptop_input.fifo)) {
363 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
364 error = PTR_ERR(sony_laptop_input.fifo);
369 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
370 if (!sony_laptop_input.wq) {
371 printk(KERN_ERR DRV_PFX
372 "Unabe to create workqueue.\n");
378 key_dev = input_allocate_device();
384 key_dev->name = "Sony Vaio Keys";
385 key_dev->id.bustype = BUS_ISA;
386 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
387 key_dev->dev.parent = &acpi_device->dev;
389 /* Initialize the Input Drivers: special keys */
390 set_bit(EV_KEY, key_dev->evbit);
391 set_bit(EV_MSC, key_dev->evbit);
392 set_bit(MSC_SCAN, key_dev->mscbit);
393 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
394 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
395 key_dev->keycode = &sony_laptop_input_keycode_map;
396 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++) {
397 if (sony_laptop_input_keycode_map[i] != KEY_RESERVED) {
398 set_bit(sony_laptop_input_keycode_map[i],
403 error = input_register_device(key_dev);
405 goto err_free_keydev;
407 sony_laptop_input.key_dev = key_dev;
410 jog_dev = input_allocate_device();
413 goto err_unregister_keydev;
416 jog_dev->name = "Sony Vaio Jogdial";
417 jog_dev->id.bustype = BUS_ISA;
418 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
419 key_dev->dev.parent = &acpi_device->dev;
421 jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
422 jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
423 jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
425 error = input_register_device(jog_dev);
427 goto err_free_jogdev;
429 sony_laptop_input.jog_dev = jog_dev;
434 input_free_device(jog_dev);
436 err_unregister_keydev:
437 input_unregister_device(key_dev);
438 /* to avoid kref underflow below at input_free_device */
442 input_free_device(key_dev);
445 destroy_workqueue(sony_laptop_input.wq);
448 kfifo_free(sony_laptop_input.fifo);
451 atomic_dec(&sony_laptop_input.users);
455 static void sony_laptop_remove_input(void)
457 /* cleanup only after the last user has gone */
458 if (!atomic_dec_and_test(&sony_laptop_input.users))
461 /* flush workqueue first */
462 flush_workqueue(sony_laptop_input.wq);
464 /* destroy input devs */
465 input_unregister_device(sony_laptop_input.key_dev);
466 sony_laptop_input.key_dev = NULL;
468 if (sony_laptop_input.jog_dev) {
469 input_unregister_device(sony_laptop_input.jog_dev);
470 sony_laptop_input.jog_dev = NULL;
473 destroy_workqueue(sony_laptop_input.wq);
474 kfifo_free(sony_laptop_input.fifo);
477 /*********** Platform Device ***********/
479 static atomic_t sony_pf_users = ATOMIC_INIT(0);
480 static struct platform_driver sony_pf_driver = {
482 .name = "sony-laptop",
483 .owner = THIS_MODULE,
486 static struct platform_device *sony_pf_device;
488 static int sony_pf_add(void)
492 /* don't run again if already initialized */
493 if (atomic_add_return(1, &sony_pf_users) > 1)
496 ret = platform_driver_register(&sony_pf_driver);
500 sony_pf_device = platform_device_alloc("sony-laptop", -1);
501 if (!sony_pf_device) {
503 goto out_platform_registered;
506 ret = platform_device_add(sony_pf_device);
508 goto out_platform_alloced;
512 out_platform_alloced:
513 platform_device_put(sony_pf_device);
514 sony_pf_device = NULL;
515 out_platform_registered:
516 platform_driver_unregister(&sony_pf_driver);
518 atomic_dec(&sony_pf_users);
522 static void sony_pf_remove(void)
524 /* deregister only after the last user has gone */
525 if (!atomic_dec_and_test(&sony_pf_users))
528 platform_device_del(sony_pf_device);
529 platform_device_put(sony_pf_device);
530 platform_driver_unregister(&sony_pf_driver);
533 /*********** SNC (SNY5001) Device ***********/
535 /* the device uses 1-based values, while the backlight subsystem uses
537 #define SONY_MAX_BRIGHTNESS 8
539 #define SNC_VALIDATE_IN 0
540 #define SNC_VALIDATE_OUT 1
542 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
544 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
545 const char *, size_t);
546 static int boolean_validate(const int, const int);
547 static int brightness_default_validate(const int, const int);
549 struct sony_nc_value {
550 char *name; /* name of the entry */
551 char **acpiget; /* names of the ACPI get function */
552 char **acpiset; /* names of the ACPI set function */
553 int (*validate)(const int, const int); /* input/output validation */
554 int value; /* current setting */
555 int valid; /* Has ever been set */
556 int debug; /* active only in debug mode ? */
557 struct device_attribute devattr; /* sysfs atribute */
560 #define SNC_HANDLE_NAMES(_name, _values...) \
561 static char *snc_##_name[] = { _values, NULL }
563 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
565 .name = __stringify(_name), \
566 .acpiget = _getters, \
567 .acpiset = _setters, \
568 .validate = _validate, \
570 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
573 #define SNC_HANDLE_NULL { .name = NULL }
575 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
577 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
578 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
580 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
581 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
583 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
584 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
586 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
587 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
589 SNC_HANDLE_NAMES(lidstate_get, "GLID");
591 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
592 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
594 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
595 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
597 SNC_HANDLE_NAMES(PID_get, "GPID");
599 SNC_HANDLE_NAMES(CTR_get, "GCTR");
600 SNC_HANDLE_NAMES(CTR_set, "SCTR");
602 SNC_HANDLE_NAMES(PCR_get, "GPCR");
603 SNC_HANDLE_NAMES(PCR_set, "SPCR");
605 SNC_HANDLE_NAMES(CMI_get, "GCMI");
606 SNC_HANDLE_NAMES(CMI_set, "SCMI");
608 static struct sony_nc_value sony_nc_values[] = {
609 SNC_HANDLE(brightness_default, snc_brightness_def_get,
610 snc_brightness_def_set, brightness_default_validate, 0),
611 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
612 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
613 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
614 boolean_validate, 0),
615 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
616 boolean_validate, 1),
617 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
618 boolean_validate, 0),
619 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
620 boolean_validate, 0),
621 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
622 boolean_validate, 0),
623 /* unknown methods */
624 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
625 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
626 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
627 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
631 static acpi_handle sony_nc_acpi_handle;
632 static struct acpi_device *sony_nc_acpi_device = NULL;
635 * acpi_evaluate_object wrappers
637 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
639 struct acpi_buffer output;
640 union acpi_object out_obj;
643 output.length = sizeof(out_obj);
644 output.pointer = &out_obj;
646 status = acpi_evaluate_object(handle, name, NULL, &output);
647 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
648 *result = out_obj.integer.value;
652 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
657 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
660 struct acpi_object_list params;
661 union acpi_object in_obj;
662 struct acpi_buffer output;
663 union acpi_object out_obj;
667 params.pointer = &in_obj;
668 in_obj.type = ACPI_TYPE_INTEGER;
669 in_obj.integer.value = value;
671 output.length = sizeof(out_obj);
672 output.pointer = &out_obj;
674 status = acpi_evaluate_object(handle, name, ¶ms, &output);
675 if (status == AE_OK) {
676 if (result != NULL) {
677 if (out_obj.type != ACPI_TYPE_INTEGER) {
678 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
682 *result = out_obj.integer.value;
687 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
693 * sony_nc_values input/output validate functions
696 /* brightness_default_validate:
698 * manipulate input output values to keep consistency with the
699 * backlight framework for which brightness values are 0-based.
701 static int brightness_default_validate(const int direction, const int value)
704 case SNC_VALIDATE_OUT:
706 case SNC_VALIDATE_IN:
707 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
715 * on input validate boolean values 0/1, on output just pass the
718 static int boolean_validate(const int direction, const int value)
720 if (direction == SNC_VALIDATE_IN) {
721 if (value != 0 && value != 1)
728 * Sysfs show/store common to all sony_nc_values
730 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
734 struct sony_nc_value *item =
735 container_of(attr, struct sony_nc_value, devattr);
740 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
744 value = item->validate(SNC_VALIDATE_OUT, value);
746 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
749 static ssize_t sony_nc_sysfs_store(struct device *dev,
750 struct device_attribute *attr,
751 const char *buffer, size_t count)
754 struct sony_nc_value *item =
755 container_of(attr, struct sony_nc_value, devattr);
763 value = simple_strtoul(buffer, NULL, 10);
766 value = item->validate(SNC_VALIDATE_IN, value);
771 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
782 static int sony_backlight_update_status(struct backlight_device *bd)
784 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
785 bd->props.brightness + 1, NULL);
788 static int sony_backlight_get_brightness(struct backlight_device *bd)
792 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
794 /* brightness levels are 1-based, while backlight ones are 0-based */
798 static struct backlight_device *sony_backlight_device;
799 static struct backlight_ops sony_backlight_ops = {
800 .update_status = sony_backlight_update_status,
801 .get_brightness = sony_backlight_get_brightness,
805 * New SNC-only Vaios event mapping to driver known keys
807 struct sony_nc_event {
812 static struct sony_nc_event *sony_nc_events;
814 /* Vaio C* --maybe also FE*, N* and AR* ?-- special init sequence
817 static int sony_nc_C_enable(const struct dmi_system_id *id)
821 printk(KERN_NOTICE DRV_PFX "detected %s\n", id->ident);
823 sony_nc_events = id->driver_data;
825 if (acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x4, &result) < 0
826 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x2, &result) < 0
827 || acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x10, &result) < 0
828 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x0, &result) < 0
829 || acpi_callsetfunc(sony_nc_acpi_handle, "SN03", 0x2, &result) < 0
830 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x101, &result) < 0) {
831 printk(KERN_WARNING DRV_PFX "failed to initialize SNC, some "
832 "functionalities may be missing\n");
838 static struct sony_nc_event sony_C_events[] = {
839 { 0x81, SONYPI_EVENT_FNKEY_F1 },
840 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
841 { 0x85, SONYPI_EVENT_FNKEY_F5 },
842 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
843 { 0x86, SONYPI_EVENT_FNKEY_F6 },
844 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
845 { 0x87, SONYPI_EVENT_FNKEY_F7 },
846 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
847 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
848 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
849 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
850 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
854 /* SNC-only model map */
855 static const struct dmi_system_id sony_nc_ids[] = {
857 .ident = "Sony Vaio FE Series",
858 .callback = sony_nc_C_enable,
859 .driver_data = sony_C_events,
861 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
862 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FE"),
866 .ident = "Sony Vaio FZ Series",
867 .callback = sony_nc_C_enable,
868 .driver_data = sony_C_events,
870 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
871 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FZ"),
875 .ident = "Sony Vaio C Series",
876 .callback = sony_nc_C_enable,
877 .driver_data = sony_C_events,
879 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
880 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-C"),
884 .ident = "Sony Vaio N Series",
885 .callback = sony_nc_C_enable,
886 .driver_data = sony_C_events,
888 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
889 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-N"),
898 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
900 struct sony_nc_event *evmap;
905 /* read the key pressed from EC.GECR
906 * A call to SN07 with 0x0202 will do it as well respecting
907 * the current protocol on different OSes
909 * Note: the path for GECR may be
910 * \_SB.PCI0.LPCB.EC (C, FE, AR, N and friends)
911 * \_SB.PCI0.PIB.EC0 (VGN-FR notifications are sent directly, no GECR)
913 * TODO: we may want to do the same for the older GHKE -need
914 * dmi list- so this snippet may become one more callback.
916 if (acpi_callsetfunc(handle, "SN07", 0x0202, &result) < 0)
917 dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
923 for (evmap = sony_nc_events; evmap->event; evmap++) {
924 if (evmap->data == ev) {
930 dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
931 sony_laptop_report_input_event(ev);
932 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
935 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
936 void *context, void **return_value)
938 struct acpi_device_info *info;
939 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
941 if (ACPI_SUCCESS(acpi_get_object_info(handle, &buffer))) {
942 info = buffer.pointer;
944 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n",
945 (char *)&info->name, info->param_count);
947 kfree(buffer.pointer);
956 static int sony_nc_resume(struct acpi_device *device)
958 struct sony_nc_value *item;
960 for (item = sony_nc_values; item->name; item++) {
965 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
968 printk("%s: %d\n", __func__, ret);
973 /* set the last requested brightness level */
974 if (sony_backlight_device &&
975 !sony_backlight_update_status(sony_backlight_device))
976 printk(KERN_WARNING DRV_PFX "unable to restore brightness level\n");
978 /* re-initialize models with specific requirements */
979 dmi_check_system(sony_nc_ids);
984 static int sony_nc_add(struct acpi_device *device)
989 struct sony_nc_value *item;
991 printk(KERN_INFO DRV_PFX "%s v%s.\n",
992 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
994 sony_nc_acpi_device = device;
995 strcpy(acpi_device_class(device), "sony/hotkey");
997 sony_nc_acpi_handle = device->handle;
999 /* read device status */
1000 result = acpi_bus_get_status(device);
1001 /* bail IFF the above call was successful and the device is not present */
1002 if (!result && !device->status.present) {
1003 dprintk("Device not present\n");
1009 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
1010 1, sony_walk_callback, NULL, NULL);
1011 if (ACPI_FAILURE(status)) {
1012 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
1018 /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
1019 * should be respected as we already checked for the device presence above */
1020 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
1021 dprintk("Invoking _INI\n");
1022 if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
1024 dprintk("_INI Method failed\n");
1027 /* setup input devices and helper fifo */
1028 result = sony_laptop_setup_input(device);
1030 printk(KERN_ERR DRV_PFX
1031 "Unabe to create input devices.\n");
1035 status = acpi_install_notify_handler(sony_nc_acpi_handle,
1037 sony_acpi_notify, NULL);
1038 if (ACPI_FAILURE(status)) {
1039 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
1044 if (acpi_video_backlight_support()) {
1045 printk(KERN_INFO DRV_PFX "brightness ignored, must be "
1046 "controlled by ACPI video driver\n");
1047 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1049 sony_backlight_device = backlight_device_register("sony", NULL,
1051 &sony_backlight_ops);
1053 if (IS_ERR(sony_backlight_device)) {
1054 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
1055 sony_backlight_device = NULL;
1057 sony_backlight_device->props.brightness =
1058 sony_backlight_get_brightness
1059 (sony_backlight_device);
1060 sony_backlight_device->props.max_brightness =
1061 SONY_MAX_BRIGHTNESS - 1;
1066 /* initialize models with specific requirements */
1067 dmi_check_system(sony_nc_ids);
1069 result = sony_pf_add();
1073 /* create sony_pf sysfs attributes related to the SNC device */
1074 for (item = sony_nc_values; item->name; ++item) {
1076 if (!debug && item->debug)
1079 /* find the available acpiget as described in the DSDT */
1080 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1081 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1084 dprintk("Found %s getter: %s\n",
1085 item->name, *item->acpiget);
1086 item->devattr.attr.mode |= S_IRUGO;
1091 /* find the available acpiset as described in the DSDT */
1092 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1093 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1096 dprintk("Found %s setter: %s\n",
1097 item->name, *item->acpiset);
1098 item->devattr.attr.mode |= S_IWUSR;
1103 if (item->devattr.attr.mode != 0) {
1105 device_create_file(&sony_pf_device->dev,
1115 for (item = sony_nc_values; item->name; ++item) {
1116 device_remove_file(&sony_pf_device->dev, &item->devattr);
1121 if (sony_backlight_device)
1122 backlight_device_unregister(sony_backlight_device);
1124 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
1127 if (ACPI_FAILURE(status))
1128 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
1131 sony_laptop_remove_input();
1137 static int sony_nc_remove(struct acpi_device *device, int type)
1140 struct sony_nc_value *item;
1142 if (sony_backlight_device)
1143 backlight_device_unregister(sony_backlight_device);
1145 sony_nc_acpi_device = NULL;
1147 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
1150 if (ACPI_FAILURE(status))
1151 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
1153 for (item = sony_nc_values; item->name; ++item) {
1154 device_remove_file(&sony_pf_device->dev, &item->devattr);
1158 sony_laptop_remove_input();
1159 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
1164 static const struct acpi_device_id sony_device_ids[] = {
1169 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1171 static const struct acpi_device_id sony_nc_device_ids[] = {
1176 static struct acpi_driver sony_nc_driver = {
1177 .name = SONY_NC_DRIVER_NAME,
1178 .class = SONY_NC_CLASS,
1179 .ids = sony_nc_device_ids,
1180 .owner = THIS_MODULE,
1183 .remove = sony_nc_remove,
1184 .resume = sony_nc_resume,
1188 /*********** SPIC (SNY6001) Device ***********/
1190 #define SONYPI_DEVICE_TYPE1 0x00000001
1191 #define SONYPI_DEVICE_TYPE2 0x00000002
1192 #define SONYPI_DEVICE_TYPE3 0x00000004
1193 #define SONYPI_DEVICE_TYPE4 0x00000008
1195 #define SONYPI_TYPE1_OFFSET 0x04
1196 #define SONYPI_TYPE2_OFFSET 0x12
1197 #define SONYPI_TYPE3_OFFSET 0x12
1198 #define SONYPI_TYPE4_OFFSET 0x12
1200 struct sony_pic_ioport {
1201 struct acpi_resource_io io1;
1202 struct acpi_resource_io io2;
1203 struct list_head list;
1206 struct sony_pic_irq {
1207 struct acpi_resource_irq irq;
1208 struct list_head list;
1211 struct sonypi_eventtypes {
1214 struct sonypi_event *events;
1217 struct device_ctrl {
1219 int (*handle_irq)(const u8, const u8);
1224 struct sonypi_eventtypes *event_types;
1227 struct sony_pic_dev {
1228 struct device_ctrl *control;
1229 struct acpi_device *acpi_dev;
1230 struct sony_pic_irq *cur_irq;
1231 struct sony_pic_ioport *cur_ioport;
1232 struct list_head interrupts;
1233 struct list_head ioports;
1240 static struct sony_pic_dev spic_dev = {
1241 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1242 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1246 #define SONYPI_JOGGER_MASK 0x00000001
1247 #define SONYPI_CAPTURE_MASK 0x00000002
1248 #define SONYPI_FNKEY_MASK 0x00000004
1249 #define SONYPI_BLUETOOTH_MASK 0x00000008
1250 #define SONYPI_PKEY_MASK 0x00000010
1251 #define SONYPI_BACK_MASK 0x00000020
1252 #define SONYPI_HELP_MASK 0x00000040
1253 #define SONYPI_LID_MASK 0x00000080
1254 #define SONYPI_ZOOM_MASK 0x00000100
1255 #define SONYPI_THUMBPHRASE_MASK 0x00000200
1256 #define SONYPI_MEYE_MASK 0x00000400
1257 #define SONYPI_MEMORYSTICK_MASK 0x00000800
1258 #define SONYPI_BATTERY_MASK 0x00001000
1259 #define SONYPI_WIRELESS_MASK 0x00002000
1261 struct sonypi_event {
1266 /* The set of possible button release events */
1267 static struct sonypi_event sonypi_releaseev[] = {
1268 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1272 /* The set of possible jogger events */
1273 static struct sonypi_event sonypi_joggerev[] = {
1274 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1275 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1276 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1277 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1278 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1279 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1280 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1281 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1282 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1283 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1284 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1285 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1286 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1290 /* The set of possible capture button events */
1291 static struct sonypi_event sonypi_captureev[] = {
1292 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1293 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1294 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
1295 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1299 /* The set of possible fnkeys events */
1300 static struct sonypi_event sonypi_fnkeyev[] = {
1301 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1302 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1303 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1304 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1305 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1306 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1307 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1308 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1309 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1310 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1311 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1312 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1313 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1314 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1315 { 0x21, SONYPI_EVENT_FNKEY_1 },
1316 { 0x22, SONYPI_EVENT_FNKEY_2 },
1317 { 0x31, SONYPI_EVENT_FNKEY_D },
1318 { 0x32, SONYPI_EVENT_FNKEY_E },
1319 { 0x33, SONYPI_EVENT_FNKEY_F },
1320 { 0x34, SONYPI_EVENT_FNKEY_S },
1321 { 0x35, SONYPI_EVENT_FNKEY_B },
1322 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1326 /* The set of possible program key events */
1327 static struct sonypi_event sonypi_pkeyev[] = {
1328 { 0x01, SONYPI_EVENT_PKEY_P1 },
1329 { 0x02, SONYPI_EVENT_PKEY_P2 },
1330 { 0x04, SONYPI_EVENT_PKEY_P3 },
1334 /* The set of possible bluetooth events */
1335 static struct sonypi_event sonypi_blueev[] = {
1336 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1337 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1338 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1342 /* The set of possible wireless events */
1343 static struct sonypi_event sonypi_wlessev[] = {
1344 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1345 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1349 /* The set of possible back button events */
1350 static struct sonypi_event sonypi_backev[] = {
1351 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1355 /* The set of possible help button events */
1356 static struct sonypi_event sonypi_helpev[] = {
1357 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1362 /* The set of possible lid events */
1363 static struct sonypi_event sonypi_lidev[] = {
1364 { 0x51, SONYPI_EVENT_LID_CLOSED },
1365 { 0x50, SONYPI_EVENT_LID_OPENED },
1369 /* The set of possible zoom events */
1370 static struct sonypi_event sonypi_zoomev[] = {
1371 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1372 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1373 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
1377 /* The set of possible thumbphrase events */
1378 static struct sonypi_event sonypi_thumbphraseev[] = {
1379 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1383 /* The set of possible motioneye camera events */
1384 static struct sonypi_event sonypi_meyeev[] = {
1385 { 0x00, SONYPI_EVENT_MEYE_FACE },
1386 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1390 /* The set of possible memorystick events */
1391 static struct sonypi_event sonypi_memorystickev[] = {
1392 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1393 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1397 /* The set of possible battery events */
1398 static struct sonypi_event sonypi_batteryev[] = {
1399 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1400 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1404 static struct sonypi_eventtypes type1_events[] = {
1405 { 0, 0xffffffff, sonypi_releaseev },
1406 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1407 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1408 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1409 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1410 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1411 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1412 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1413 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1414 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1417 static struct sonypi_eventtypes type2_events[] = {
1418 { 0, 0xffffffff, sonypi_releaseev },
1419 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1420 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1421 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1422 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1423 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1424 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1425 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1426 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1427 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1428 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1429 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1430 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1431 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1434 static struct sonypi_eventtypes type3_events[] = {
1435 { 0, 0xffffffff, sonypi_releaseev },
1436 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1437 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1438 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1439 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1440 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1443 static struct sonypi_eventtypes type4_events[] = {
1444 { 0, 0xffffffff, sonypi_releaseev },
1445 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1446 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1447 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1448 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1449 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1450 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1451 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
1455 /* low level spic calls */
1456 #define ITERATIONS_LONG 10000
1457 #define ITERATIONS_SHORT 10
1458 #define wait_on_command(command, iterations) { \
1459 unsigned int n = iterations; \
1460 while (--n && (command)) \
1463 dprintk("command failed at %s : %s (line %d)\n", \
1464 __FILE__, __func__, __LINE__); \
1467 static u8 sony_pic_call1(u8 dev)
1471 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1473 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1474 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1475 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
1476 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
1480 static u8 sony_pic_call2(u8 dev, u8 fn)
1484 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1486 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1487 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1489 outb(fn, spic_dev.cur_ioport->io1.minimum);
1490 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1491 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
1495 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1499 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1500 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1501 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1502 outb(fn, spic_dev.cur_ioport->io1.minimum);
1503 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1504 outb(v, spic_dev.cur_ioport->io1.minimum);
1505 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1506 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1512 * minidrivers for SPIC models
1514 static int type4_handle_irq(const u8 data_mask, const u8 ev)
1517 * 0x31 could mean we have to take some extra action and wait for
1518 * the next irq for some Type4 models, it will generate a new
1519 * irq and we can read new data from the device:
1520 * - 0x5c and 0x5f requires 0xA0
1521 * - 0x61 requires 0xB3
1523 if (data_mask == 0x31) {
1524 if (ev == 0x5c || ev == 0x5f)
1525 sony_pic_call1(0xA0);
1526 else if (ev == 0x61)
1527 sony_pic_call1(0xB3);
1533 static struct device_ctrl spic_types[] = {
1535 .model = SONYPI_DEVICE_TYPE1,
1537 .evport_offset = SONYPI_TYPE1_OFFSET,
1538 .event_types = type1_events,
1541 .model = SONYPI_DEVICE_TYPE2,
1543 .evport_offset = SONYPI_TYPE2_OFFSET,
1544 .event_types = type2_events,
1547 .model = SONYPI_DEVICE_TYPE3,
1549 .evport_offset = SONYPI_TYPE3_OFFSET,
1550 .event_types = type3_events,
1553 .model = SONYPI_DEVICE_TYPE4,
1554 .handle_irq = type4_handle_irq,
1555 .evport_offset = SONYPI_TYPE4_OFFSET,
1556 .event_types = type4_events,
1560 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1562 struct pci_dev *pcidev;
1564 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1565 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1567 dev->control = &spic_types[0];
1571 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1572 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1574 dev->control = &spic_types[2];
1578 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1579 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1581 dev->control = &spic_types[3];
1585 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1586 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1588 dev->control = &spic_types[3];
1593 dev->control = &spic_types[1];
1597 pci_dev_put(pcidev);
1599 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1600 dev->control->model == SONYPI_DEVICE_TYPE1 ? 1 :
1601 dev->control->model == SONYPI_DEVICE_TYPE2 ? 2 :
1602 dev->control->model == SONYPI_DEVICE_TYPE3 ? 3 : 4);
1605 /* camera tests and poweron/poweroff */
1606 #define SONYPI_CAMERA_PICTURE 5
1607 #define SONYPI_CAMERA_CONTROL 0x10
1609 #define SONYPI_CAMERA_BRIGHTNESS 0
1610 #define SONYPI_CAMERA_CONTRAST 1
1611 #define SONYPI_CAMERA_HUE 2
1612 #define SONYPI_CAMERA_COLOR 3
1613 #define SONYPI_CAMERA_SHARPNESS 4
1615 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1616 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1617 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1618 #define SONYPI_CAMERA_MUTE_MASK 0x40
1620 /* the rest don't need a loop until not 0xff */
1621 #define SONYPI_CAMERA_AGC 6
1622 #define SONYPI_CAMERA_AGC_MASK 0x30
1623 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
1625 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1626 #define SONYPI_CAMERA_CONTROL 0x10
1628 #define SONYPI_CAMERA_STATUS 7
1629 #define SONYPI_CAMERA_STATUS_READY 0x2
1630 #define SONYPI_CAMERA_STATUS_POSITION 0x4
1632 #define SONYPI_DIRECTION_BACKWARDS 0x4
1634 #define SONYPI_CAMERA_REVISION 8
1635 #define SONYPI_CAMERA_ROMVERSION 9
1637 static int __sony_pic_camera_ready(void)
1641 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1642 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1645 static int __sony_pic_camera_off(void)
1648 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1652 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1653 SONYPI_CAMERA_MUTE_MASK),
1656 if (spic_dev.camera_power) {
1657 sony_pic_call2(0x91, 0);
1658 spic_dev.camera_power = 0;
1663 static int __sony_pic_camera_on(void)
1668 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1672 if (spic_dev.camera_power)
1675 for (j = 5; j > 0; j--) {
1677 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1679 sony_pic_call1(0x93);
1681 for (i = 400; i > 0; i--) {
1682 if (__sony_pic_camera_ready())
1691 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
1695 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1699 spic_dev.camera_power = 1;
1703 /* External camera command (exported to the motion eye v4l driver) */
1704 int sony_pic_camera_command(int command, u8 value)
1709 mutex_lock(&spic_dev.lock);
1712 case SONY_PIC_COMMAND_SETCAMERA:
1714 __sony_pic_camera_on();
1716 __sony_pic_camera_off();
1718 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
1719 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1722 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
1723 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1726 case SONY_PIC_COMMAND_SETCAMERAHUE:
1727 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1730 case SONY_PIC_COMMAND_SETCAMERACOLOR:
1731 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1734 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
1735 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1738 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
1739 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1742 case SONY_PIC_COMMAND_SETCAMERAAGC:
1743 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1747 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1751 mutex_unlock(&spic_dev.lock);
1754 EXPORT_SYMBOL(sony_pic_camera_command);
1756 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1757 static void sony_pic_set_wwanpower(u8 state)
1760 mutex_lock(&spic_dev.lock);
1761 if (spic_dev.wwan_power == state) {
1762 mutex_unlock(&spic_dev.lock);
1765 sony_pic_call2(0xB0, state);
1766 spic_dev.wwan_power = state;
1767 mutex_unlock(&spic_dev.lock);
1770 static ssize_t sony_pic_wwanpower_store(struct device *dev,
1771 struct device_attribute *attr,
1772 const char *buffer, size_t count)
1774 unsigned long value;
1778 value = simple_strtoul(buffer, NULL, 10);
1779 sony_pic_set_wwanpower(value);
1784 static ssize_t sony_pic_wwanpower_show(struct device *dev,
1785 struct device_attribute *attr, char *buffer)
1788 mutex_lock(&spic_dev.lock);
1789 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1790 mutex_unlock(&spic_dev.lock);
1794 /* bluetooth subsystem power state */
1795 static void __sony_pic_set_bluetoothpower(u8 state)
1798 if (spic_dev.bluetooth_power == state)
1800 sony_pic_call2(0x96, state);
1801 sony_pic_call1(0x82);
1802 spic_dev.bluetooth_power = state;
1805 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1806 struct device_attribute *attr,
1807 const char *buffer, size_t count)
1809 unsigned long value;
1813 value = simple_strtoul(buffer, NULL, 10);
1814 mutex_lock(&spic_dev.lock);
1815 __sony_pic_set_bluetoothpower(value);
1816 mutex_unlock(&spic_dev.lock);
1821 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1822 struct device_attribute *attr, char *buffer)
1825 mutex_lock(&spic_dev.lock);
1826 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1827 mutex_unlock(&spic_dev.lock);
1832 /* FAN0 information (reverse engineered from ACPI tables) */
1833 #define SONY_PIC_FAN0_STATUS 0x93
1834 static int sony_pic_set_fanspeed(unsigned long value)
1836 return ec_write(SONY_PIC_FAN0_STATUS, value);
1839 static int sony_pic_get_fanspeed(u8 *value)
1841 return ec_read(SONY_PIC_FAN0_STATUS, value);
1844 static ssize_t sony_pic_fanspeed_store(struct device *dev,
1845 struct device_attribute *attr,
1846 const char *buffer, size_t count)
1848 unsigned long value;
1852 value = simple_strtoul(buffer, NULL, 10);
1853 if (sony_pic_set_fanspeed(value))
1859 static ssize_t sony_pic_fanspeed_show(struct device *dev,
1860 struct device_attribute *attr, char *buffer)
1863 if (sony_pic_get_fanspeed(&value))
1866 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1869 #define SPIC_ATTR(_name, _mode) \
1870 struct device_attribute spic_attr_##_name = __ATTR(_name, \
1871 _mode, sony_pic_## _name ##_show, \
1872 sony_pic_## _name ##_store)
1874 static SPIC_ATTR(bluetoothpower, 0644);
1875 static SPIC_ATTR(wwanpower, 0644);
1876 static SPIC_ATTR(fanspeed, 0644);
1878 static struct attribute *spic_attributes[] = {
1879 &spic_attr_bluetoothpower.attr,
1880 &spic_attr_wwanpower.attr,
1881 &spic_attr_fanspeed.attr,
1885 static struct attribute_group spic_attribute_group = {
1886 .attrs = spic_attributes
1889 /******** SONYPI compatibility **********/
1890 #ifdef CONFIG_SONYPI_COMPAT
1892 /* battery / brightness / temperature addresses */
1893 #define SONYPI_BAT_FLAGS 0x81
1894 #define SONYPI_LCD_LIGHT 0x96
1895 #define SONYPI_BAT1_PCTRM 0xa0
1896 #define SONYPI_BAT1_LEFT 0xa2
1897 #define SONYPI_BAT1_MAXRT 0xa4
1898 #define SONYPI_BAT2_PCTRM 0xa8
1899 #define SONYPI_BAT2_LEFT 0xaa
1900 #define SONYPI_BAT2_MAXRT 0xac
1901 #define SONYPI_BAT1_MAXTK 0xb0
1902 #define SONYPI_BAT1_FULL 0xb2
1903 #define SONYPI_BAT2_MAXTK 0xb8
1904 #define SONYPI_BAT2_FULL 0xba
1905 #define SONYPI_TEMP_STATUS 0xC1
1907 struct sonypi_compat_s {
1908 struct fasync_struct *fifo_async;
1910 spinlock_t fifo_lock;
1911 wait_queue_head_t fifo_proc_list;
1912 atomic_t open_count;
1914 static struct sonypi_compat_s sonypi_compat = {
1915 .open_count = ATOMIC_INIT(0),
1918 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1922 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1928 static int sonypi_misc_release(struct inode *inode, struct file *file)
1930 atomic_dec(&sonypi_compat.open_count);
1934 static int sonypi_misc_open(struct inode *inode, struct file *file)
1936 /* Flush input queue on first open */
1938 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1939 kfifo_reset(sonypi_compat.fifo);
1944 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1945 size_t count, loff_t *pos)
1950 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1951 (file->f_flags & O_NONBLOCK))
1954 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1955 kfifo_len(sonypi_compat.fifo) != 0);
1959 while (ret < count &&
1960 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1961 if (put_user(c, buf++))
1967 struct inode *inode = file->f_path.dentry->d_inode;
1968 inode->i_atime = current_fs_time(inode->i_sb);
1974 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1976 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1977 if (kfifo_len(sonypi_compat.fifo))
1978 return POLLIN | POLLRDNORM;
1982 static int ec_read16(u8 addr, u16 *value)
1985 if (ec_read(addr, &val_lb))
1987 if (ec_read(addr + 1, &val_hb))
1989 *value = val_lb | (val_hb << 8);
1993 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1994 unsigned int cmd, unsigned long arg)
1997 void __user *argp = (void __user *)arg;
2002 mutex_lock(&spic_dev.lock);
2004 case SONYPI_IOCGBRT:
2005 if (sony_backlight_device == NULL) {
2009 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2013 val8 = ((value & 0xff) - 1) << 5;
2014 if (copy_to_user(argp, &val8, sizeof(val8)))
2017 case SONYPI_IOCSBRT:
2018 if (sony_backlight_device == NULL) {
2022 if (copy_from_user(&val8, argp, sizeof(val8))) {
2026 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2027 (val8 >> 5) + 1, NULL)) {
2031 /* sync the backlight device status */
2032 sony_backlight_device->props.brightness =
2033 sony_backlight_get_brightness(sony_backlight_device);
2035 case SONYPI_IOCGBAT1CAP:
2036 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2040 if (copy_to_user(argp, &val16, sizeof(val16)))
2043 case SONYPI_IOCGBAT1REM:
2044 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2048 if (copy_to_user(argp, &val16, sizeof(val16)))
2051 case SONYPI_IOCGBAT2CAP:
2052 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2056 if (copy_to_user(argp, &val16, sizeof(val16)))
2059 case SONYPI_IOCGBAT2REM:
2060 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2064 if (copy_to_user(argp, &val16, sizeof(val16)))
2067 case SONYPI_IOCGBATFLAGS:
2068 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2073 if (copy_to_user(argp, &val8, sizeof(val8)))
2076 case SONYPI_IOCGBLUE:
2077 val8 = spic_dev.bluetooth_power;
2078 if (copy_to_user(argp, &val8, sizeof(val8)))
2081 case SONYPI_IOCSBLUE:
2082 if (copy_from_user(&val8, argp, sizeof(val8))) {
2086 __sony_pic_set_bluetoothpower(val8);
2089 case SONYPI_IOCGFAN:
2090 if (sony_pic_get_fanspeed(&val8)) {
2094 if (copy_to_user(argp, &val8, sizeof(val8)))
2097 case SONYPI_IOCSFAN:
2098 if (copy_from_user(&val8, argp, sizeof(val8))) {
2102 if (sony_pic_set_fanspeed(val8))
2105 /* GET Temperature (useful under APM) */
2106 case SONYPI_IOCGTEMP:
2107 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2111 if (copy_to_user(argp, &val8, sizeof(val8)))
2117 mutex_unlock(&spic_dev.lock);
2121 static const struct file_operations sonypi_misc_fops = {
2122 .owner = THIS_MODULE,
2123 .read = sonypi_misc_read,
2124 .poll = sonypi_misc_poll,
2125 .open = sonypi_misc_open,
2126 .release = sonypi_misc_release,
2127 .fasync = sonypi_misc_fasync,
2128 .ioctl = sonypi_misc_ioctl,
2131 static struct miscdevice sonypi_misc_device = {
2132 .minor = MISC_DYNAMIC_MINOR,
2134 .fops = &sonypi_misc_fops,
2137 static void sonypi_compat_report_event(u8 event)
2139 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
2140 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2141 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2144 static int sonypi_compat_init(void)
2148 spin_lock_init(&sonypi_compat.fifo_lock);
2149 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
2150 &sonypi_compat.fifo_lock);
2151 if (IS_ERR(sonypi_compat.fifo)) {
2152 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2153 return PTR_ERR(sonypi_compat.fifo);
2156 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
2159 sonypi_misc_device.minor = minor;
2160 error = misc_register(&sonypi_misc_device);
2162 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2163 goto err_free_kfifo;
2166 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
2167 sonypi_misc_device.minor);
2172 kfifo_free(sonypi_compat.fifo);
2176 static void sonypi_compat_exit(void)
2178 misc_deregister(&sonypi_misc_device);
2179 kfifo_free(sonypi_compat.fifo);
2182 static int sonypi_compat_init(void) { return 0; }
2183 static void sonypi_compat_exit(void) { }
2184 static void sonypi_compat_report_event(u8 event) { }
2185 #endif /* CONFIG_SONYPI_COMPAT */
2191 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2194 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2196 switch (resource->type) {
2197 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
2199 /* start IO enumeration */
2200 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2204 list_add(&ioport->list, &dev->ioports);
2208 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
2209 /* end IO enumeration */
2212 case ACPI_RESOURCE_TYPE_IRQ:
2214 struct acpi_resource_irq *p = &resource->data.irq;
2215 struct sony_pic_irq *interrupt = NULL;
2216 if (!p || !p->interrupt_count) {
2218 * IRQ descriptors may have no IRQ# bits set,
2219 * particularly those those w/ _STA disabled
2221 dprintk("Blank IRQ resource\n");
2224 for (i = 0; i < p->interrupt_count; i++) {
2225 if (!p->interrupts[i]) {
2226 printk(KERN_WARNING DRV_PFX
2231 interrupt = kzalloc(sizeof(*interrupt),
2236 list_add(&interrupt->list, &dev->interrupts);
2237 interrupt->irq.triggering = p->triggering;
2238 interrupt->irq.polarity = p->polarity;
2239 interrupt->irq.sharable = p->sharable;
2240 interrupt->irq.interrupt_count = 1;
2241 interrupt->irq.interrupts[0] = p->interrupts[i];
2245 case ACPI_RESOURCE_TYPE_IO:
2247 struct acpi_resource_io *io = &resource->data.io;
2248 struct sony_pic_ioport *ioport =
2249 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
2251 dprintk("Blank IO resource\n");
2255 if (!ioport->io1.minimum) {
2256 memcpy(&ioport->io1, io, sizeof(*io));
2257 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2258 ioport->io1.address_length);
2260 else if (!ioport->io2.minimum) {
2261 memcpy(&ioport->io2, io, sizeof(*io));
2262 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2263 ioport->io2.address_length);
2266 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
2272 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2275 case ACPI_RESOURCE_TYPE_END_TAG:
2278 return AE_CTRL_TERMINATE;
2281 static int sony_pic_possible_resources(struct acpi_device *device)
2284 acpi_status status = AE_OK;
2289 /* get device status */
2290 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2291 dprintk("Evaluating _STA\n");
2292 result = acpi_bus_get_status(device);
2294 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2298 if (!device->status.enabled)
2299 dprintk("Device disabled\n");
2301 dprintk("Device enabled\n");
2304 * Query and parse 'method'
2306 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2307 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2308 sony_pic_read_possible_resource, &spic_dev);
2309 if (ACPI_FAILURE(status)) {
2310 printk(KERN_WARNING DRV_PFX
2311 "Failure evaluating %s\n",
2320 * Disable the spic device by calling its _DIS method
2322 static int sony_pic_disable(struct acpi_device *device)
2324 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2327 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
2330 dprintk("Device disabled\n");
2336 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2338 * Call _SRS to set current resources
2340 static int sony_pic_enable(struct acpi_device *device,
2341 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2345 /* Type 1 resource layout is:
2351 * Type 2 and 3 resource layout is:
2357 struct acpi_resource res1;
2358 struct acpi_resource res2;
2359 struct acpi_resource res3;
2360 struct acpi_resource res4;
2362 struct acpi_buffer buffer = { 0, NULL };
2364 if (!ioport || !irq)
2367 /* init acpi_buffer */
2368 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2372 buffer.length = sizeof(*resource) + 1;
2373 buffer.pointer = resource;
2375 /* setup Type 1 resources */
2376 if (spic_dev.control->model == SONYPI_DEVICE_TYPE1) {
2378 /* setup io resources */
2379 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2380 resource->res1.length = sizeof(struct acpi_resource);
2381 memcpy(&resource->res1.data.io, &ioport->io1,
2382 sizeof(struct acpi_resource_io));
2384 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2385 resource->res2.length = sizeof(struct acpi_resource);
2386 memcpy(&resource->res2.data.io, &ioport->io2,
2387 sizeof(struct acpi_resource_io));
2389 /* setup irq resource */
2390 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2391 resource->res3.length = sizeof(struct acpi_resource);
2392 memcpy(&resource->res3.data.irq, &irq->irq,
2393 sizeof(struct acpi_resource_irq));
2394 /* we requested a shared irq */
2395 resource->res3.data.irq.sharable = ACPI_SHARED;
2397 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2400 /* setup Type 2/3 resources */
2402 /* setup io resource */
2403 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2404 resource->res1.length = sizeof(struct acpi_resource);
2405 memcpy(&resource->res1.data.io, &ioport->io1,
2406 sizeof(struct acpi_resource_io));
2408 /* setup irq resource */
2409 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2410 resource->res2.length = sizeof(struct acpi_resource);
2411 memcpy(&resource->res2.data.irq, &irq->irq,
2412 sizeof(struct acpi_resource_irq));
2413 /* we requested a shared irq */
2414 resource->res2.data.irq.sharable = ACPI_SHARED;
2416 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2419 /* Attempt to set the resource */
2420 dprintk("Evaluating _SRS\n");
2421 status = acpi_set_current_resources(device->handle, &buffer);
2423 /* check for total failure */
2424 if (ACPI_FAILURE(status)) {
2425 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
2430 /* Necessary device initializations calls (from sonypi) */
2431 sony_pic_call1(0x82);
2432 sony_pic_call2(0x81, 0xff);
2433 sony_pic_call1(compat ? 0x92 : 0x82);
2442 * ISR: some event is available
2445 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2450 u8 device_event = 0;
2452 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2454 ev = inb_p(dev->cur_ioport->io1.minimum);
2455 if (dev->cur_ioport->io2.minimum)
2456 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2458 data_mask = inb_p(dev->cur_ioport->io1.minimum +
2459 dev->control->evport_offset);
2461 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2462 ev, data_mask, dev->cur_ioport->io1.minimum,
2463 dev->control->evport_offset);
2465 if (ev == 0x00 || ev == 0xff)
2468 for (i = 0; dev->control->event_types[i].mask; i++) {
2470 if ((data_mask & dev->control->event_types[i].data) !=
2471 dev->control->event_types[i].data)
2474 if (!(mask & dev->control->event_types[i].mask))
2477 for (j = 0; dev->control->event_types[i].events[j].event; j++) {
2478 if (ev == dev->control->event_types[i].events[j].data) {
2481 event_types[i].events[j].event;
2486 /* Still not able to decode the event try to pass
2487 * it over to the minidriver
2489 if (dev->control->handle_irq &&
2490 dev->control->handle_irq(data_mask, ev) == 0)
2493 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2494 ev, data_mask, dev->cur_ioport->io1.minimum,
2495 dev->control->evport_offset);
2499 sony_laptop_report_input_event(device_event);
2500 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
2501 sonypi_compat_report_event(device_event);
2511 static int sony_pic_remove(struct acpi_device *device, int type)
2513 struct sony_pic_ioport *io, *tmp_io;
2514 struct sony_pic_irq *irq, *tmp_irq;
2516 if (sony_pic_disable(device)) {
2517 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2521 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2522 release_region(spic_dev.cur_ioport->io1.minimum,
2523 spic_dev.cur_ioport->io1.address_length);
2524 if (spic_dev.cur_ioport->io2.minimum)
2525 release_region(spic_dev.cur_ioport->io2.minimum,
2526 spic_dev.cur_ioport->io2.address_length);
2528 sonypi_compat_exit();
2530 sony_laptop_remove_input();
2533 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2536 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2537 list_del(&io->list);
2540 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2541 list_del(&irq->list);
2544 spic_dev.cur_ioport = NULL;
2545 spic_dev.cur_irq = NULL;
2547 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2551 static int sony_pic_add(struct acpi_device *device)
2554 struct sony_pic_ioport *io, *tmp_io;
2555 struct sony_pic_irq *irq, *tmp_irq;
2557 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2558 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2560 spic_dev.acpi_dev = device;
2561 strcpy(acpi_device_class(device), "sony/hotkey");
2562 sony_pic_detect_device_type(&spic_dev);
2563 mutex_init(&spic_dev.lock);
2565 /* read _PRS resources */
2566 result = sony_pic_possible_resources(device);
2568 printk(KERN_ERR DRV_PFX
2569 "Unabe to read possible resources.\n");
2570 goto err_free_resources;
2573 /* setup input devices and helper fifo */
2574 result = sony_laptop_setup_input(device);
2576 printk(KERN_ERR DRV_PFX
2577 "Unabe to create input devices.\n");
2578 goto err_free_resources;
2581 if (sonypi_compat_init())
2582 goto err_remove_input;
2584 /* request io port */
2585 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2586 if (request_region(io->io1.minimum, io->io1.address_length,
2587 "Sony Programable I/O Device")) {
2588 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2589 io->io1.minimum, io->io1.maximum,
2590 io->io1.address_length);
2591 /* Type 1 have 2 ioports */
2592 if (io->io2.minimum) {
2593 if (request_region(io->io2.minimum,
2594 io->io2.address_length,
2595 "Sony Programable I/O Device")) {
2596 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2597 io->io2.minimum, io->io2.maximum,
2598 io->io2.address_length);
2599 spic_dev.cur_ioport = io;
2603 dprintk("Unable to get I/O port2: "
2604 "0x%.4x (0x%.4x) + 0x%.2x\n",
2605 io->io2.minimum, io->io2.maximum,
2606 io->io2.address_length);
2607 release_region(io->io1.minimum,
2608 io->io1.address_length);
2612 spic_dev.cur_ioport = io;
2617 if (!spic_dev.cur_ioport) {
2618 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2620 goto err_remove_compat;
2624 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
2625 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2626 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2627 dprintk("IRQ: %d - triggering: %d - "
2628 "polarity: %d - shr: %d\n",
2629 irq->irq.interrupts[0],
2630 irq->irq.triggering,
2633 spic_dev.cur_irq = irq;
2637 if (!spic_dev.cur_irq) {
2638 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2640 goto err_release_region;
2643 /* set resource status _SRS */
2644 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2646 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2650 spic_dev.bluetooth_power = -1;
2651 /* create device attributes */
2652 result = sony_pf_add();
2654 goto err_disable_device;
2656 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2666 sony_pic_disable(device);
2669 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2672 release_region(spic_dev.cur_ioport->io1.minimum,
2673 spic_dev.cur_ioport->io1.address_length);
2674 if (spic_dev.cur_ioport->io2.minimum)
2675 release_region(spic_dev.cur_ioport->io2.minimum,
2676 spic_dev.cur_ioport->io2.address_length);
2679 sonypi_compat_exit();
2682 sony_laptop_remove_input();
2685 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2686 list_del(&io->list);
2689 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2690 list_del(&irq->list);
2693 spic_dev.cur_ioport = NULL;
2694 spic_dev.cur_irq = NULL;
2699 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2701 if (sony_pic_disable(device))
2706 static int sony_pic_resume(struct acpi_device *device)
2708 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2712 static const struct acpi_device_id sony_pic_device_ids[] = {
2717 static struct acpi_driver sony_pic_driver = {
2718 .name = SONY_PIC_DRIVER_NAME,
2719 .class = SONY_PIC_CLASS,
2720 .ids = sony_pic_device_ids,
2721 .owner = THIS_MODULE,
2723 .add = sony_pic_add,
2724 .remove = sony_pic_remove,
2725 .suspend = sony_pic_suspend,
2726 .resume = sony_pic_resume,
2730 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2732 .ident = "Sony Vaio",
2734 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2735 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2739 .ident = "Sony Vaio",
2741 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2742 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2748 static int __init sony_laptop_init(void)
2752 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2753 result = acpi_bus_register_driver(&sony_pic_driver);
2755 printk(KERN_ERR DRV_PFX
2756 "Unable to register SPIC driver.");
2761 result = acpi_bus_register_driver(&sony_nc_driver);
2763 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2764 goto out_unregister_pic;
2771 acpi_bus_unregister_driver(&sony_pic_driver);
2776 static void __exit sony_laptop_exit(void)
2778 acpi_bus_unregister_driver(&sony_nc_driver);
2780 acpi_bus_unregister_driver(&sony_pic_driver);
2783 module_init(sony_laptop_init);
2784 module_exit(sony_laptop_exit);