2 * ACPI Sony Notebook Control Driver (SNC and SPIC)
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5 * Copyright (C) 2007-2009 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 <linux/slab.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 #include <linux/rfkill.h>
68 #ifdef CONFIG_SONYPI_COMPAT
69 #include <linux/poll.h>
70 #include <linux/miscdevice.h>
73 #define DRV_PFX "sony-laptop: "
74 #define dprintk(msg...) do { \
76 pr_warn(DRV_PFX msg); \
79 #define SONY_LAPTOP_DRIVER_VERSION "0.6"
81 #define SONY_NC_CLASS "sony-nc"
82 #define SONY_NC_HID "SNY5001"
83 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
85 #define SONY_PIC_CLASS "sony-pic"
86 #define SONY_PIC_HID "SNY6001"
87 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
89 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
90 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
91 MODULE_LICENSE("GPL");
92 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
95 module_param(debug, int, 0);
96 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
97 "the development of this driver");
99 static int no_spic; /* = 0 */
100 module_param(no_spic, int, 0444);
101 MODULE_PARM_DESC(no_spic,
102 "set this if you don't want to enable the SPIC device");
104 static int compat; /* = 0 */
105 module_param(compat, int, 0444);
106 MODULE_PARM_DESC(compat,
107 "set this if you want to enable backward compatibility mode");
109 static unsigned long mask = 0xffffffff;
110 module_param(mask, ulong, 0644);
111 MODULE_PARM_DESC(mask,
112 "set this to the mask of event you want to enable (see doc)");
114 static int camera; /* = 0 */
115 module_param(camera, int, 0444);
116 MODULE_PARM_DESC(camera,
117 "set this to 1 to enable Motion Eye camera controls "
118 "(only use it if you have a C1VE or C1VN model)");
120 #ifdef CONFIG_SONYPI_COMPAT
121 static int minor = -1;
122 module_param(minor, int, 0);
123 MODULE_PARM_DESC(minor,
124 "minor number of the misc device for the SPIC compatibility code, "
125 "default is -1 (automatic)");
128 static int kbd_backlight; /* = 1 */
129 module_param(kbd_backlight, int, 0444);
130 MODULE_PARM_DESC(kbd_backlight,
131 "set this to 0 to disable keyboard backlight, "
132 "1 to enable it (default: 0)");
134 static int kbd_backlight_timeout; /* = 0 */
135 module_param(kbd_backlight_timeout, int, 0444);
136 MODULE_PARM_DESC(kbd_backlight_timeout,
137 "set this to 0 to set the default 10 seconds timeout, "
138 "1 for 30 seconds, 2 for 60 seconds and 3 to disable timeout "
141 static void sony_nc_kbd_backlight_resume(void);
143 enum sony_nc_rfkill {
151 static int sony_rfkill_handle;
152 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
153 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
154 static void sony_nc_rfkill_update(void);
156 /*********** Input Devices ***********/
158 #define SONY_LAPTOP_BUF_SIZE 128
159 struct sony_laptop_input_s {
161 struct input_dev *jog_dev;
162 struct input_dev *key_dev;
164 spinlock_t fifo_lock;
165 struct timer_list release_key_timer;
168 static struct sony_laptop_input_s sony_laptop_input = {
169 .users = ATOMIC_INIT(0),
172 struct sony_laptop_keypress {
173 struct input_dev *dev;
177 /* Correspondance table between sonypi events
178 * and input layer indexes in the keymap
180 static int sony_laptop_input_index[] = {
182 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
183 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
184 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
185 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
186 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
187 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
188 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
189 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
190 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
191 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
192 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
193 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
194 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
195 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
196 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
197 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
198 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
199 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
200 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
201 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
202 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
203 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
204 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
205 17, /* 24 SONYPI_EVENT_FNKEY_1 */
206 18, /* 25 SONYPI_EVENT_FNKEY_2 */
207 19, /* 26 SONYPI_EVENT_FNKEY_D */
208 20, /* 27 SONYPI_EVENT_FNKEY_E */
209 21, /* 28 SONYPI_EVENT_FNKEY_F */
210 22, /* 29 SONYPI_EVENT_FNKEY_S */
211 23, /* 30 SONYPI_EVENT_FNKEY_B */
212 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
213 25, /* 32 SONYPI_EVENT_PKEY_P1 */
214 26, /* 33 SONYPI_EVENT_PKEY_P2 */
215 27, /* 34 SONYPI_EVENT_PKEY_P3 */
216 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
217 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
218 -1, /* 37 SONYPI_EVENT_LID_OPENED */
219 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
220 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
221 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
222 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
223 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
224 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
225 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
226 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
227 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
228 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
229 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
230 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
231 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
232 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
233 43, /* 52 SONYPI_EVENT_MEYE_FACE */
234 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
235 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
236 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
237 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
238 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
239 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
240 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
241 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
242 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
243 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
244 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
245 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
246 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
247 53, /* 66 SONYPI_EVENT_PKEY_P4 */
248 54, /* 67 SONYPI_EVENT_PKEY_P5 */
249 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
250 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
251 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
252 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
253 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
254 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
257 static int sony_laptop_input_keycode_map[] = {
258 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
259 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
260 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
261 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
262 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
263 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
264 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
265 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
266 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
267 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
268 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
269 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
270 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
271 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
272 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
273 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
274 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
275 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
276 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
277 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
278 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
279 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
280 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
281 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
282 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
283 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
284 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
285 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
286 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
287 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
288 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
289 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
290 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
291 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
292 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
293 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
294 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
295 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
296 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
297 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
298 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
299 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
300 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
301 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
302 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
303 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
304 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
305 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
306 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
307 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
308 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
309 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
310 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
311 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
312 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
313 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
314 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
315 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
316 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
317 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
320 /* release buttons after a short delay if pressed */
321 static void do_sony_laptop_release_key(unsigned long unused)
323 struct sony_laptop_keypress kp;
326 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
328 if (kfifo_out(&sony_laptop_input.fifo,
329 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
330 input_report_key(kp.dev, kp.key, 0);
334 /* If there is something in the fifo schedule next release. */
335 if (kfifo_len(&sony_laptop_input.fifo) != 0)
336 mod_timer(&sony_laptop_input.release_key_timer,
337 jiffies + msecs_to_jiffies(10));
339 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
342 /* forward event to the input subsystem */
343 static void sony_laptop_report_input_event(u8 event)
345 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
346 struct input_dev *key_dev = sony_laptop_input.key_dev;
347 struct sony_laptop_keypress kp = { NULL };
349 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
350 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
351 /* Nothing, not all VAIOs generate this event */
358 case SONYPI_EVENT_JOGDIAL_UP:
359 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
360 input_report_rel(jog_dev, REL_WHEEL, 1);
364 case SONYPI_EVENT_JOGDIAL_DOWN:
365 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
366 input_report_rel(jog_dev, REL_WHEEL, -1);
371 case SONYPI_EVENT_JOGDIAL_PRESSED:
377 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
378 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
381 if (sony_laptop_input_index[event] != -1) {
382 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
383 if (kp.key != KEY_UNKNOWN)
390 input_report_key(kp.dev, kp.key, 1);
391 /* we emit the scancode so we can always remap the key */
392 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
395 /* schedule key release */
396 kfifo_in_locked(&sony_laptop_input.fifo,
397 (unsigned char *)&kp, sizeof(kp),
398 &sony_laptop_input.fifo_lock);
399 mod_timer(&sony_laptop_input.release_key_timer,
400 jiffies + msecs_to_jiffies(10));
402 dprintk("unknown input event %.2x\n", event);
405 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
407 struct input_dev *jog_dev;
408 struct input_dev *key_dev;
412 /* don't run again if already initialized */
413 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
417 spin_lock_init(&sony_laptop_input.fifo_lock);
418 error = kfifo_alloc(&sony_laptop_input.fifo,
419 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
421 pr_err(DRV_PFX "kfifo_alloc failed\n");
425 setup_timer(&sony_laptop_input.release_key_timer,
426 do_sony_laptop_release_key, 0);
429 key_dev = input_allocate_device();
435 key_dev->name = "Sony Vaio Keys";
436 key_dev->id.bustype = BUS_ISA;
437 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
438 key_dev->dev.parent = &acpi_device->dev;
440 /* Initialize the Input Drivers: special keys */
441 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
443 __set_bit(EV_KEY, key_dev->evbit);
444 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
445 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
446 key_dev->keycode = &sony_laptop_input_keycode_map;
447 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
448 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
449 __clear_bit(KEY_RESERVED, key_dev->keybit);
451 error = input_register_device(key_dev);
453 goto err_free_keydev;
455 sony_laptop_input.key_dev = key_dev;
458 jog_dev = input_allocate_device();
461 goto err_unregister_keydev;
464 jog_dev->name = "Sony Vaio Jogdial";
465 jog_dev->id.bustype = BUS_ISA;
466 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
467 key_dev->dev.parent = &acpi_device->dev;
469 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
470 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
472 error = input_register_device(jog_dev);
474 goto err_free_jogdev;
476 sony_laptop_input.jog_dev = jog_dev;
481 input_free_device(jog_dev);
483 err_unregister_keydev:
484 input_unregister_device(key_dev);
485 /* to avoid kref underflow below at input_free_device */
489 input_free_device(key_dev);
492 kfifo_free(&sony_laptop_input.fifo);
495 atomic_dec(&sony_laptop_input.users);
499 static void sony_laptop_remove_input(void)
501 struct sony_laptop_keypress kp = { NULL };
503 /* Cleanup only after the last user has gone */
504 if (!atomic_dec_and_test(&sony_laptop_input.users))
507 del_timer_sync(&sony_laptop_input.release_key_timer);
510 * Generate key-up events for remaining keys. Note that we don't
511 * need locking since nobody is adding new events to the kfifo.
513 while (kfifo_out(&sony_laptop_input.fifo,
514 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
515 input_report_key(kp.dev, kp.key, 0);
519 /* destroy input devs */
520 input_unregister_device(sony_laptop_input.key_dev);
521 sony_laptop_input.key_dev = NULL;
523 if (sony_laptop_input.jog_dev) {
524 input_unregister_device(sony_laptop_input.jog_dev);
525 sony_laptop_input.jog_dev = NULL;
528 kfifo_free(&sony_laptop_input.fifo);
531 /*********** Platform Device ***********/
533 static atomic_t sony_pf_users = ATOMIC_INIT(0);
534 static struct platform_driver sony_pf_driver = {
536 .name = "sony-laptop",
537 .owner = THIS_MODULE,
540 static struct platform_device *sony_pf_device;
542 static int sony_pf_add(void)
546 /* don't run again if already initialized */
547 if (atomic_add_return(1, &sony_pf_users) > 1)
550 ret = platform_driver_register(&sony_pf_driver);
554 sony_pf_device = platform_device_alloc("sony-laptop", -1);
555 if (!sony_pf_device) {
557 goto out_platform_registered;
560 ret = platform_device_add(sony_pf_device);
562 goto out_platform_alloced;
566 out_platform_alloced:
567 platform_device_put(sony_pf_device);
568 sony_pf_device = NULL;
569 out_platform_registered:
570 platform_driver_unregister(&sony_pf_driver);
572 atomic_dec(&sony_pf_users);
576 static void sony_pf_remove(void)
578 /* deregister only after the last user has gone */
579 if (!atomic_dec_and_test(&sony_pf_users))
582 platform_device_unregister(sony_pf_device);
583 platform_driver_unregister(&sony_pf_driver);
586 /*********** SNC (SNY5001) Device ***********/
588 /* the device uses 1-based values, while the backlight subsystem uses
590 #define SONY_MAX_BRIGHTNESS 8
592 #define SNC_VALIDATE_IN 0
593 #define SNC_VALIDATE_OUT 1
595 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
597 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
598 const char *, size_t);
599 static int boolean_validate(const int, const int);
600 static int brightness_default_validate(const int, const int);
602 struct sony_nc_value {
603 char *name; /* name of the entry */
604 char **acpiget; /* names of the ACPI get function */
605 char **acpiset; /* names of the ACPI set function */
606 int (*validate)(const int, const int); /* input/output validation */
607 int value; /* current setting */
608 int valid; /* Has ever been set */
609 int debug; /* active only in debug mode ? */
610 struct device_attribute devattr; /* sysfs attribute */
613 #define SNC_HANDLE_NAMES(_name, _values...) \
614 static char *snc_##_name[] = { _values, NULL }
616 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
618 .name = __stringify(_name), \
619 .acpiget = _getters, \
620 .acpiset = _setters, \
621 .validate = _validate, \
623 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
626 #define SNC_HANDLE_NULL { .name = NULL }
628 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
630 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
631 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
633 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
634 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
636 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
637 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
639 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
640 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
642 SNC_HANDLE_NAMES(lidstate_get, "GLID");
644 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
645 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
647 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
648 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
650 SNC_HANDLE_NAMES(PID_get, "GPID");
652 SNC_HANDLE_NAMES(CTR_get, "GCTR");
653 SNC_HANDLE_NAMES(CTR_set, "SCTR");
655 SNC_HANDLE_NAMES(PCR_get, "GPCR");
656 SNC_HANDLE_NAMES(PCR_set, "SPCR");
658 SNC_HANDLE_NAMES(CMI_get, "GCMI");
659 SNC_HANDLE_NAMES(CMI_set, "SCMI");
661 static struct sony_nc_value sony_nc_values[] = {
662 SNC_HANDLE(brightness_default, snc_brightness_def_get,
663 snc_brightness_def_set, brightness_default_validate, 0),
664 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
665 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
666 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
667 boolean_validate, 0),
668 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
669 boolean_validate, 1),
670 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
671 boolean_validate, 0),
672 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
673 boolean_validate, 0),
674 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
675 boolean_validate, 0),
676 /* unknown methods */
677 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
678 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
679 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
680 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
684 static acpi_handle sony_nc_acpi_handle;
685 static struct acpi_device *sony_nc_acpi_device = NULL;
688 * acpi_evaluate_object wrappers
690 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
692 struct acpi_buffer output;
693 union acpi_object out_obj;
696 output.length = sizeof(out_obj);
697 output.pointer = &out_obj;
699 status = acpi_evaluate_object(handle, name, NULL, &output);
700 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
701 *result = out_obj.integer.value;
705 pr_warn(DRV_PFX "acpi_callreadfunc failed\n");
710 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
713 struct acpi_object_list params;
714 union acpi_object in_obj;
715 struct acpi_buffer output;
716 union acpi_object out_obj;
720 params.pointer = &in_obj;
721 in_obj.type = ACPI_TYPE_INTEGER;
722 in_obj.integer.value = value;
724 output.length = sizeof(out_obj);
725 output.pointer = &out_obj;
727 status = acpi_evaluate_object(handle, name, ¶ms, &output);
728 if (status == AE_OK) {
729 if (result != NULL) {
730 if (out_obj.type != ACPI_TYPE_INTEGER) {
731 pr_warn(DRV_PFX "acpi_evaluate_object bad "
735 *result = out_obj.integer.value;
740 pr_warn(DRV_PFX "acpi_evaluate_object failed\n");
745 struct sony_nc_handles {
747 struct device_attribute devattr;
750 static struct sony_nc_handles *handles;
752 static ssize_t sony_nc_handles_show(struct device *dev,
753 struct device_attribute *attr, char *buffer)
758 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
759 len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
762 len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
767 static int sony_nc_handles_setup(struct platform_device *pd)
772 handles = kzalloc(sizeof(*handles), GFP_KERNEL);
776 for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
777 if (!acpi_callsetfunc(sony_nc_acpi_handle,
778 "SN00", i + 0x20, &result)) {
779 dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
781 handles->cap[i] = result;
786 sysfs_attr_init(&handles->devattr.attr);
787 handles->devattr.attr.name = "handles";
788 handles->devattr.attr.mode = S_IRUGO;
789 handles->devattr.show = sony_nc_handles_show;
791 /* allow reading capabilities via sysfs */
792 if (device_create_file(&pd->dev, &handles->devattr)) {
802 static int sony_nc_handles_cleanup(struct platform_device *pd)
806 device_remove_file(&pd->dev, &handles->devattr);
813 static int sony_find_snc_handle(int handle)
817 /* not initialized yet, return early */
821 for (i = 0; i < 0x10; i++) {
822 if (handles->cap[i] == handle) {
823 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
828 dprintk("handle 0x%.4x not found\n", handle);
832 static int sony_call_snc_handle(int handle, int argument, int *result)
835 int offset = sony_find_snc_handle(handle);
840 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
842 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
848 * sony_nc_values input/output validate functions
851 /* brightness_default_validate:
853 * manipulate input output values to keep consistency with the
854 * backlight framework for which brightness values are 0-based.
856 static int brightness_default_validate(const int direction, const int value)
859 case SNC_VALIDATE_OUT:
861 case SNC_VALIDATE_IN:
862 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
870 * on input validate boolean values 0/1, on output just pass the
873 static int boolean_validate(const int direction, const int value)
875 if (direction == SNC_VALIDATE_IN) {
876 if (value != 0 && value != 1)
883 * Sysfs show/store common to all sony_nc_values
885 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
889 struct sony_nc_value *item =
890 container_of(attr, struct sony_nc_value, devattr);
895 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
899 value = item->validate(SNC_VALIDATE_OUT, value);
901 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
904 static ssize_t sony_nc_sysfs_store(struct device *dev,
905 struct device_attribute *attr,
906 const char *buffer, size_t count)
909 struct sony_nc_value *item =
910 container_of(attr, struct sony_nc_value, devattr);
918 value = simple_strtoul(buffer, NULL, 10);
921 value = item->validate(SNC_VALIDATE_IN, value);
926 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
937 struct sony_backlight_props {
938 struct backlight_device *dev;
943 struct sony_backlight_props sony_bl_props;
945 static int sony_backlight_update_status(struct backlight_device *bd)
947 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
948 bd->props.brightness + 1, NULL);
951 static int sony_backlight_get_brightness(struct backlight_device *bd)
955 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
957 /* brightness levels are 1-based, while backlight ones are 0-based */
961 static int sony_nc_get_brightness_ng(struct backlight_device *bd)
964 int *handle = (int *)bl_get_data(bd);
965 struct sony_backlight_props *sdev =
966 (struct sony_backlight_props *)bl_get_data(bd);
968 sony_call_snc_handle(sdev->handle, 0x0200, &result);
970 return (result & 0xff) - sdev->offset;
973 static int sony_nc_update_status_ng(struct backlight_device *bd)
976 int *handle = (int *)bl_get_data(bd);
977 struct sony_backlight_props *sdev =
978 (struct sony_backlight_props *)bl_get_data(bd);
980 value = bd->props.brightness + sdev->offset;
981 if (sony_call_snc_handle(sdev->handle, 0x0100 | (value << 16), &result))
987 static const struct backlight_ops sony_backlight_ops = {
988 .options = BL_CORE_SUSPENDRESUME,
989 .update_status = sony_backlight_update_status,
990 .get_brightness = sony_backlight_get_brightness,
992 static const struct backlight_ops sony_backlight_ng_ops = {
993 .options = BL_CORE_SUSPENDRESUME,
994 .update_status = sony_nc_update_status_ng,
995 .get_brightness = sony_nc_get_brightness_ng,
999 * New SNC-only Vaios event mapping to driver known keys
1001 struct sony_nc_event {
1006 static struct sony_nc_event sony_100_events[] = {
1007 { 0x90, SONYPI_EVENT_PKEY_P1 },
1008 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
1009 { 0x91, SONYPI_EVENT_PKEY_P2 },
1010 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
1011 { 0x81, SONYPI_EVENT_FNKEY_F1 },
1012 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
1013 { 0x82, SONYPI_EVENT_FNKEY_F2 },
1014 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
1015 { 0x83, SONYPI_EVENT_FNKEY_F3 },
1016 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
1017 { 0x84, SONYPI_EVENT_FNKEY_F4 },
1018 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
1019 { 0x85, SONYPI_EVENT_FNKEY_F5 },
1020 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
1021 { 0x86, SONYPI_EVENT_FNKEY_F6 },
1022 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
1023 { 0x87, SONYPI_EVENT_FNKEY_F7 },
1024 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
1025 { 0x89, SONYPI_EVENT_FNKEY_F9 },
1026 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
1027 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
1028 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
1029 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
1030 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
1031 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
1032 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
1033 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
1034 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
1035 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
1036 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
1037 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
1038 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
1039 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
1040 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
1041 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
1042 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
1046 static struct sony_nc_event sony_127_events[] = {
1047 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
1048 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
1049 { 0x82, SONYPI_EVENT_PKEY_P1 },
1050 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
1051 { 0x83, SONYPI_EVENT_PKEY_P2 },
1052 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
1053 { 0x84, SONYPI_EVENT_PKEY_P3 },
1054 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
1055 { 0x85, SONYPI_EVENT_PKEY_P4 },
1056 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
1057 { 0x86, SONYPI_EVENT_PKEY_P5 },
1058 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
1059 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
1060 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
1067 static void sony_nc_notify(struct acpi_device *device, u32 event)
1072 /* New-style event */
1077 if (sony_find_snc_handle(0x100) == ev)
1079 if (sony_find_snc_handle(0x127) == ev)
1083 struct sony_nc_event *key_event;
1085 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
1086 dprintk("sony_nc_notify, unable to decode"
1087 " event 0x%.2x 0x%.2x\n", key_handle,
1089 /* restore the original event */
1094 if (key_handle == 0x100)
1095 key_event = sony_100_events;
1097 key_event = sony_127_events;
1099 for (; key_event->data; key_event++) {
1100 if (key_event->data == ev) {
1101 ev = key_event->event;
1106 if (!key_event->data)
1108 "Unknown event: 0x%x 0x%x\n",
1112 sony_laptop_report_input_event(ev);
1114 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
1115 sony_nc_rfkill_update();
1119 sony_laptop_report_input_event(ev);
1121 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
1122 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
1125 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1126 void *context, void **return_value)
1128 struct acpi_device_info *info;
1130 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
1131 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
1132 (char *)&info->name, info->param_count);
1143 static int sony_nc_function_setup(struct acpi_device *device)
1147 /* Enable all events */
1148 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1151 sony_call_snc_handle(0x0100, 0, &result);
1152 sony_call_snc_handle(0x0101, 0, &result);
1153 sony_call_snc_handle(0x0102, 0x100, &result);
1154 sony_call_snc_handle(0x0127, 0, &result);
1159 static int sony_nc_resume(struct acpi_device *device)
1161 struct sony_nc_value *item;
1164 for (item = sony_nc_values; item->name; item++) {
1169 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
1172 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
1177 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1179 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1180 dprintk("ECON Method failed\n");
1183 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1185 dprintk("Doing SNC setup\n");
1186 sony_nc_function_setup(device);
1189 /* re-read rfkill state */
1190 sony_nc_rfkill_update();
1192 /* restore kbd backlight states */
1193 sony_nc_kbd_backlight_resume();
1198 static void sony_nc_rfkill_cleanup(void)
1202 for (i = 0; i < N_SONY_RFKILL; i++) {
1203 if (sony_rfkill_devices[i]) {
1204 rfkill_unregister(sony_rfkill_devices[i]);
1205 rfkill_destroy(sony_rfkill_devices[i]);
1210 static int sony_nc_rfkill_set(void *data, bool blocked)
1213 int argument = sony_rfkill_address[(long) data] + 0x100;
1216 argument |= 0xff0000;
1218 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1221 static const struct rfkill_ops sony_rfkill_ops = {
1222 .set_block = sony_nc_rfkill_set,
1225 static int sony_nc_setup_rfkill(struct acpi_device *device,
1226 enum sony_nc_rfkill nc_type)
1230 enum rfkill_type type;
1237 type = RFKILL_TYPE_WLAN;
1240 case SONY_BLUETOOTH:
1241 type = RFKILL_TYPE_BLUETOOTH;
1242 name = "sony-bluetooth";
1245 type = RFKILL_TYPE_WWAN;
1249 type = RFKILL_TYPE_WIMAX;
1250 name = "sony-wimax";
1256 rfk = rfkill_alloc(name, &device->dev, type,
1257 &sony_rfkill_ops, (void *)nc_type);
1261 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1262 hwblock = !(result & 0x1);
1263 rfkill_set_hw_state(rfk, hwblock);
1265 err = rfkill_register(rfk);
1267 rfkill_destroy(rfk);
1270 sony_rfkill_devices[nc_type] = rfk;
1274 static void sony_nc_rfkill_update(void)
1276 enum sony_nc_rfkill i;
1280 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1281 hwblock = !(result & 0x1);
1283 for (i = 0; i < N_SONY_RFKILL; i++) {
1284 int argument = sony_rfkill_address[i];
1286 if (!sony_rfkill_devices[i])
1290 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1291 /* we already know we're blocked */
1296 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1297 rfkill_set_states(sony_rfkill_devices[i],
1298 !(result & 0xf), false);
1302 static void sony_nc_rfkill_setup(struct acpi_device *device)
1307 struct acpi_object_list params;
1308 union acpi_object in_obj;
1309 union acpi_object *device_enum;
1310 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1312 offset = sony_find_snc_handle(0x124);
1314 offset = sony_find_snc_handle(0x135);
1318 sony_rfkill_handle = 0x135;
1320 sony_rfkill_handle = 0x124;
1321 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
1323 /* need to read the whole buffer returned by the acpi call to SN06
1324 * here otherwise we may miss some features
1327 params.pointer = &in_obj;
1328 in_obj.type = ACPI_TYPE_INTEGER;
1329 in_obj.integer.value = offset;
1330 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", ¶ms,
1332 if (ACPI_FAILURE(status)) {
1333 dprintk("Radio device enumeration failed\n");
1337 device_enum = (union acpi_object *) buffer.pointer;
1339 pr_err(DRV_PFX "No SN06 return object.");
1342 if (device_enum->type != ACPI_TYPE_BUFFER) {
1343 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1348 /* the buffer is filled with magic numbers describing the devices
1349 * available, 0xff terminates the enumeration
1351 for (i = 0; i < device_enum->buffer.length; i++) {
1353 dev_code = *(device_enum->buffer.pointer + i);
1354 if (dev_code == 0xff)
1357 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1359 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1360 sony_nc_setup_rfkill(device, SONY_WIFI);
1362 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1363 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1365 if ((0xf0 & dev_code) == 0x20 &&
1366 !sony_rfkill_devices[SONY_WWAN])
1367 sony_nc_setup_rfkill(device, SONY_WWAN);
1369 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1370 sony_nc_setup_rfkill(device, SONY_WIMAX);
1374 kfree(buffer.pointer);
1378 /* Keyboard backlight feature */
1379 #define KBDBL_HANDLER 0x137
1380 #define KBDBL_PRESENT 0xB00
1381 #define SET_MODE 0xC00
1382 #define SET_STATE 0xD00
1383 #define SET_TIMEOUT 0xE00
1385 struct kbd_backlight {
1388 struct device_attribute mode_attr;
1389 struct device_attribute timeout_attr;
1392 static struct kbd_backlight *kbdbl_handle;
1394 static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
1401 if (sony_call_snc_handle(KBDBL_HANDLER,
1402 (value << 0x10) | SET_MODE, &result))
1405 /* Try to turn the light on/off immediately */
1406 sony_call_snc_handle(KBDBL_HANDLER, (value << 0x10) | SET_STATE,
1409 kbdbl_handle->mode = value;
1414 static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
1415 struct device_attribute *attr,
1416 const char *buffer, size_t count)
1419 unsigned long value;
1424 if (strict_strtoul(buffer, 10, &value))
1427 ret = __sony_nc_kbd_backlight_mode_set(value);
1434 static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
1435 struct device_attribute *attr, char *buffer)
1438 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
1442 static int __sony_nc_kbd_backlight_timeout_set(u8 value)
1449 if (sony_call_snc_handle(KBDBL_HANDLER,
1450 (value << 0x10) | SET_TIMEOUT, &result))
1453 kbdbl_handle->timeout = value;
1458 static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
1459 struct device_attribute *attr,
1460 const char *buffer, size_t count)
1463 unsigned long value;
1468 if (strict_strtoul(buffer, 10, &value))
1471 ret = __sony_nc_kbd_backlight_timeout_set(value);
1478 static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
1479 struct device_attribute *attr, char *buffer)
1482 count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
1486 static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
1490 if (sony_call_snc_handle(KBDBL_HANDLER, KBDBL_PRESENT, &result))
1492 if (!(result & 0x02))
1495 kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
1499 sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
1500 kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
1501 kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
1502 kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
1503 kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
1505 sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
1506 kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
1507 kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
1508 kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
1509 kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
1511 if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
1514 if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
1517 __sony_nc_kbd_backlight_mode_set(kbd_backlight);
1518 __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
1523 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1525 kfree(kbdbl_handle);
1526 kbdbl_handle = NULL;
1530 static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
1535 device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
1536 device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
1538 /* restore the default hw behaviour */
1539 sony_call_snc_handle(KBDBL_HANDLER, 0x1000 | SET_MODE, &result);
1540 sony_call_snc_handle(KBDBL_HANDLER, SET_TIMEOUT, &result);
1542 kfree(kbdbl_handle);
1547 static void sony_nc_kbd_backlight_resume(void)
1554 if (kbdbl_handle->mode == 0)
1555 sony_call_snc_handle(KBDBL_HANDLER, SET_MODE, &ignore);
1557 if (kbdbl_handle->timeout != 0)
1558 sony_call_snc_handle(KBDBL_HANDLER,
1559 (kbdbl_handle->timeout << 0x10) | SET_TIMEOUT,
1563 static void sony_nc_backlight_ng_read_limits(int handle,
1564 struct sony_backlight_props *props)
1569 u8 min = 0xff, max = 0x00;
1570 struct acpi_object_list params;
1571 union acpi_object in_obj;
1572 union acpi_object *lvl_enum;
1573 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1575 props->handle = handle;
1577 props->maxlvl = 0xff;
1579 offset = sony_find_snc_handle(handle);
1583 /* try to read the boundaries from ACPI tables, if we fail the above
1584 * defaults should be reasonable
1587 params.pointer = &in_obj;
1588 in_obj.type = ACPI_TYPE_INTEGER;
1589 in_obj.integer.value = offset;
1590 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", ¶ms,
1592 if (ACPI_FAILURE(status))
1595 lvl_enum = (union acpi_object *) buffer.pointer;
1597 pr_err("No SN06 return object.");
1600 if (lvl_enum->type != ACPI_TYPE_BUFFER) {
1601 pr_err("Invalid SN06 return object 0x%.2x\n",
1606 /* the buffer lists brightness levels available, brightness levels are
1607 * from 0 to 8 in the array, other values are used by ALS control.
1609 for (i = 0; i < 9 && i < lvl_enum->buffer.length; i++) {
1611 brlvl = *(lvl_enum->buffer.pointer + i);
1612 dprintk("Brightness level: %d\n", brlvl);
1622 props->offset = min;
1623 props->maxlvl = max;
1624 dprintk("Brightness levels: min=%d max=%d\n", props->offset,
1628 kfree(buffer.pointer);
1632 static void sony_nc_backlight_setup(void)
1635 int max_brightness = 0;
1636 const struct backlight_ops *ops = NULL;
1637 struct backlight_properties props;
1639 if (sony_find_snc_handle(0x12f) != -1) {
1640 ops = &sony_backlight_ng_ops;
1641 sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
1642 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
1644 } else if (sony_find_snc_handle(0x137) != -1) {
1645 ops = &sony_backlight_ng_ops;
1646 sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
1647 max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
1649 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1651 ops = &sony_backlight_ops;
1652 max_brightness = SONY_MAX_BRIGHTNESS - 1;
1657 memset(&props, 0, sizeof(struct backlight_properties));
1658 props.type = BACKLIGHT_PLATFORM;
1659 props.max_brightness = max_brightness;
1660 sony_bl_props.dev = backlight_device_register("sony", NULL,
1664 if (IS_ERR(sony_bl_props.dev)) {
1665 pr_warn(DRV_PFX "unable to register backlight device\n");
1666 sony_bl_props.dev = NULL;
1668 sony_bl_props.dev->props.brightness =
1669 ops->get_brightness(sony_bl_props.dev);
1672 static void sony_nc_backlight_cleanup(void)
1674 if (sony_bl_props.dev)
1675 backlight_device_unregister(sony_bl_props.dev);
1678 static int sony_nc_add(struct acpi_device *device)
1683 struct sony_nc_value *item;
1685 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1686 SONY_LAPTOP_DRIVER_VERSION);
1688 sony_nc_acpi_device = device;
1689 strcpy(acpi_device_class(device), "sony/hotkey");
1691 sony_nc_acpi_handle = device->handle;
1693 /* read device status */
1694 result = acpi_bus_get_status(device);
1695 /* bail IFF the above call was successful and the device is not present */
1696 if (!result && !device->status.present) {
1697 dprintk("Device not present\n");
1702 result = sony_pf_add();
1707 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1708 sony_nc_acpi_handle, 1, sony_walk_callback,
1710 if (ACPI_FAILURE(status)) {
1711 pr_warn(DRV_PFX "unable to walk acpi resources\n");
1717 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1719 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1720 dprintk("ECON Method failed\n");
1723 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1725 dprintk("Doing SNC setup\n");
1726 result = sony_nc_handles_setup(sony_pf_device);
1729 result = sony_nc_kbd_backlight_setup(sony_pf_device);
1732 sony_nc_function_setup(device);
1733 sony_nc_rfkill_setup(device);
1736 /* setup input devices and helper fifo */
1737 result = sony_laptop_setup_input(device);
1739 pr_err(DRV_PFX "Unable to create input devices.\n");
1740 goto outkbdbacklight;
1743 if (acpi_video_backlight_support()) {
1744 pr_info(DRV_PFX "brightness ignored, must be "
1745 "controlled by ACPI video driver\n");
1747 sony_nc_backlight_setup();
1750 /* create sony_pf sysfs attributes related to the SNC device */
1751 for (item = sony_nc_values; item->name; ++item) {
1753 if (!debug && item->debug)
1756 /* find the available acpiget as described in the DSDT */
1757 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1758 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1761 dprintk("Found %s getter: %s\n",
1762 item->name, *item->acpiget);
1763 item->devattr.attr.mode |= S_IRUGO;
1768 /* find the available acpiset as described in the DSDT */
1769 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1770 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1773 dprintk("Found %s setter: %s\n",
1774 item->name, *item->acpiset);
1775 item->devattr.attr.mode |= S_IWUSR;
1780 if (item->devattr.attr.mode != 0) {
1782 device_create_file(&sony_pf_device->dev,
1792 for (item = sony_nc_values; item->name; ++item) {
1793 device_remove_file(&sony_pf_device->dev, &item->devattr);
1795 sony_nc_backlight_cleanup();
1797 sony_laptop_remove_input();
1800 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1803 sony_nc_handles_cleanup(sony_pf_device);
1809 sony_nc_rfkill_cleanup();
1813 static int sony_nc_remove(struct acpi_device *device, int type)
1815 struct sony_nc_value *item;
1817 sony_nc_backlight_cleanup();
1819 sony_nc_acpi_device = NULL;
1821 for (item = sony_nc_values; item->name; ++item) {
1822 device_remove_file(&sony_pf_device->dev, &item->devattr);
1825 sony_nc_kbd_backlight_cleanup(sony_pf_device);
1826 sony_nc_handles_cleanup(sony_pf_device);
1828 sony_laptop_remove_input();
1829 sony_nc_rfkill_cleanup();
1830 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
1835 static const struct acpi_device_id sony_device_ids[] = {
1840 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1842 static const struct acpi_device_id sony_nc_device_ids[] = {
1847 static struct acpi_driver sony_nc_driver = {
1848 .name = SONY_NC_DRIVER_NAME,
1849 .class = SONY_NC_CLASS,
1850 .ids = sony_nc_device_ids,
1851 .owner = THIS_MODULE,
1854 .remove = sony_nc_remove,
1855 .resume = sony_nc_resume,
1856 .notify = sony_nc_notify,
1860 /*********** SPIC (SNY6001) Device ***********/
1862 #define SONYPI_DEVICE_TYPE1 0x00000001
1863 #define SONYPI_DEVICE_TYPE2 0x00000002
1864 #define SONYPI_DEVICE_TYPE3 0x00000004
1866 #define SONYPI_TYPE1_OFFSET 0x04
1867 #define SONYPI_TYPE2_OFFSET 0x12
1868 #define SONYPI_TYPE3_OFFSET 0x12
1870 struct sony_pic_ioport {
1871 struct acpi_resource_io io1;
1872 struct acpi_resource_io io2;
1873 struct list_head list;
1876 struct sony_pic_irq {
1877 struct acpi_resource_irq irq;
1878 struct list_head list;
1881 struct sonypi_eventtypes {
1884 struct sonypi_event *events;
1887 struct sony_pic_dev {
1888 struct acpi_device *acpi_dev;
1889 struct sony_pic_irq *cur_irq;
1890 struct sony_pic_ioport *cur_ioport;
1891 struct list_head interrupts;
1892 struct list_head ioports;
1894 struct sonypi_eventtypes *event_types;
1895 int (*handle_irq)(const u8, const u8);
1903 static struct sony_pic_dev spic_dev = {
1904 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1905 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1908 static int spic_drv_registered;
1911 #define SONYPI_JOGGER_MASK 0x00000001
1912 #define SONYPI_CAPTURE_MASK 0x00000002
1913 #define SONYPI_FNKEY_MASK 0x00000004
1914 #define SONYPI_BLUETOOTH_MASK 0x00000008
1915 #define SONYPI_PKEY_MASK 0x00000010
1916 #define SONYPI_BACK_MASK 0x00000020
1917 #define SONYPI_HELP_MASK 0x00000040
1918 #define SONYPI_LID_MASK 0x00000080
1919 #define SONYPI_ZOOM_MASK 0x00000100
1920 #define SONYPI_THUMBPHRASE_MASK 0x00000200
1921 #define SONYPI_MEYE_MASK 0x00000400
1922 #define SONYPI_MEMORYSTICK_MASK 0x00000800
1923 #define SONYPI_BATTERY_MASK 0x00001000
1924 #define SONYPI_WIRELESS_MASK 0x00002000
1926 struct sonypi_event {
1931 /* The set of possible button release events */
1932 static struct sonypi_event sonypi_releaseev[] = {
1933 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1937 /* The set of possible jogger events */
1938 static struct sonypi_event sonypi_joggerev[] = {
1939 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1940 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1941 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1942 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1943 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1944 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1945 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1946 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1947 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1948 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1949 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1950 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1951 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1955 /* The set of possible capture button events */
1956 static struct sonypi_event sonypi_captureev[] = {
1957 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1958 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1959 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
1960 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1964 /* The set of possible fnkeys events */
1965 static struct sonypi_event sonypi_fnkeyev[] = {
1966 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1967 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1968 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1969 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1970 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1971 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1972 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1973 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1974 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1975 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1976 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1977 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1978 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1979 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1980 { 0x21, SONYPI_EVENT_FNKEY_1 },
1981 { 0x22, SONYPI_EVENT_FNKEY_2 },
1982 { 0x31, SONYPI_EVENT_FNKEY_D },
1983 { 0x32, SONYPI_EVENT_FNKEY_E },
1984 { 0x33, SONYPI_EVENT_FNKEY_F },
1985 { 0x34, SONYPI_EVENT_FNKEY_S },
1986 { 0x35, SONYPI_EVENT_FNKEY_B },
1987 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1991 /* The set of possible program key events */
1992 static struct sonypi_event sonypi_pkeyev[] = {
1993 { 0x01, SONYPI_EVENT_PKEY_P1 },
1994 { 0x02, SONYPI_EVENT_PKEY_P2 },
1995 { 0x04, SONYPI_EVENT_PKEY_P3 },
1996 { 0x20, SONYPI_EVENT_PKEY_P1 },
2000 /* The set of possible bluetooth events */
2001 static struct sonypi_event sonypi_blueev[] = {
2002 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
2003 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
2004 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
2008 /* The set of possible wireless events */
2009 static struct sonypi_event sonypi_wlessev[] = {
2010 { 0x59, SONYPI_EVENT_IGNORE },
2011 { 0x5a, SONYPI_EVENT_IGNORE },
2015 /* The set of possible back button events */
2016 static struct sonypi_event sonypi_backev[] = {
2017 { 0x20, SONYPI_EVENT_BACK_PRESSED },
2021 /* The set of possible help button events */
2022 static struct sonypi_event sonypi_helpev[] = {
2023 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
2028 /* The set of possible lid events */
2029 static struct sonypi_event sonypi_lidev[] = {
2030 { 0x51, SONYPI_EVENT_LID_CLOSED },
2031 { 0x50, SONYPI_EVENT_LID_OPENED },
2035 /* The set of possible zoom events */
2036 static struct sonypi_event sonypi_zoomev[] = {
2037 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
2038 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
2039 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
2040 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
2044 /* The set of possible thumbphrase events */
2045 static struct sonypi_event sonypi_thumbphraseev[] = {
2046 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
2050 /* The set of possible motioneye camera events */
2051 static struct sonypi_event sonypi_meyeev[] = {
2052 { 0x00, SONYPI_EVENT_MEYE_FACE },
2053 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
2057 /* The set of possible memorystick events */
2058 static struct sonypi_event sonypi_memorystickev[] = {
2059 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
2060 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
2064 /* The set of possible battery events */
2065 static struct sonypi_event sonypi_batteryev[] = {
2066 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
2067 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
2071 /* The set of possible volume events */
2072 static struct sonypi_event sonypi_volumeev[] = {
2073 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
2074 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
2078 /* The set of possible brightness events */
2079 static struct sonypi_event sonypi_brightnessev[] = {
2080 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
2084 static struct sonypi_eventtypes type1_events[] = {
2085 { 0, 0xffffffff, sonypi_releaseev },
2086 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
2087 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
2088 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
2089 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
2090 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2091 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2092 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
2093 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2094 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
2097 static struct sonypi_eventtypes type2_events[] = {
2098 { 0, 0xffffffff, sonypi_releaseev },
2099 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
2100 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
2101 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
2102 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2103 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
2104 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
2105 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
2106 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
2107 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
2108 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
2109 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2110 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2111 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2114 static struct sonypi_eventtypes type3_events[] = {
2115 { 0, 0xffffffff, sonypi_releaseev },
2116 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
2117 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
2118 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
2119 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
2120 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
2121 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
2122 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
2123 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
2124 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
2125 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
2129 /* low level spic calls */
2130 #define ITERATIONS_LONG 10000
2131 #define ITERATIONS_SHORT 10
2132 #define wait_on_command(command, iterations) { \
2133 unsigned int n = iterations; \
2134 while (--n && (command)) \
2137 dprintk("command failed at %s : %s (line %d)\n", \
2138 __FILE__, __func__, __LINE__); \
2141 static u8 sony_pic_call1(u8 dev)
2145 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
2147 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2148 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
2149 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
2150 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
2154 static u8 sony_pic_call2(u8 dev, u8 fn)
2158 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
2160 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2161 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
2163 outb(fn, spic_dev.cur_ioport->io1.minimum);
2164 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
2165 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
2169 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
2173 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2174 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
2175 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2176 outb(fn, spic_dev.cur_ioport->io1.minimum);
2177 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
2178 outb(v, spic_dev.cur_ioport->io1.minimum);
2179 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
2180 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
2186 * minidrivers for SPIC models
2188 static int type3_handle_irq(const u8 data_mask, const u8 ev)
2191 * 0x31 could mean we have to take some extra action and wait for
2192 * the next irq for some Type3 models, it will generate a new
2193 * irq and we can read new data from the device:
2194 * - 0x5c and 0x5f requires 0xA0
2195 * - 0x61 requires 0xB3
2197 if (data_mask == 0x31) {
2198 if (ev == 0x5c || ev == 0x5f)
2199 sony_pic_call1(0xA0);
2200 else if (ev == 0x61)
2201 sony_pic_call1(0xB3);
2207 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
2209 struct pci_dev *pcidev;
2211 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2212 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
2214 dev->model = SONYPI_DEVICE_TYPE1;
2215 dev->evport_offset = SONYPI_TYPE1_OFFSET;
2216 dev->event_types = type1_events;
2220 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2221 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
2223 dev->model = SONYPI_DEVICE_TYPE2;
2224 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2225 dev->event_types = type2_events;
2229 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2230 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
2232 dev->model = SONYPI_DEVICE_TYPE3;
2233 dev->handle_irq = type3_handle_irq;
2234 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2235 dev->event_types = type3_events;
2239 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2240 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
2242 dev->model = SONYPI_DEVICE_TYPE3;
2243 dev->handle_irq = type3_handle_irq;
2244 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2245 dev->event_types = type3_events;
2249 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
2250 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
2252 dev->model = SONYPI_DEVICE_TYPE3;
2253 dev->handle_irq = type3_handle_irq;
2254 dev->evport_offset = SONYPI_TYPE3_OFFSET;
2255 dev->event_types = type3_events;
2260 dev->model = SONYPI_DEVICE_TYPE2;
2261 dev->evport_offset = SONYPI_TYPE2_OFFSET;
2262 dev->event_types = type2_events;
2266 pci_dev_put(pcidev);
2268 pr_info(DRV_PFX "detected Type%d model\n",
2269 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
2270 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
2273 /* camera tests and poweron/poweroff */
2274 #define SONYPI_CAMERA_PICTURE 5
2275 #define SONYPI_CAMERA_CONTROL 0x10
2277 #define SONYPI_CAMERA_BRIGHTNESS 0
2278 #define SONYPI_CAMERA_CONTRAST 1
2279 #define SONYPI_CAMERA_HUE 2
2280 #define SONYPI_CAMERA_COLOR 3
2281 #define SONYPI_CAMERA_SHARPNESS 4
2283 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
2284 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
2285 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
2286 #define SONYPI_CAMERA_MUTE_MASK 0x40
2288 /* the rest don't need a loop until not 0xff */
2289 #define SONYPI_CAMERA_AGC 6
2290 #define SONYPI_CAMERA_AGC_MASK 0x30
2291 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
2293 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
2294 #define SONYPI_CAMERA_CONTROL 0x10
2296 #define SONYPI_CAMERA_STATUS 7
2297 #define SONYPI_CAMERA_STATUS_READY 0x2
2298 #define SONYPI_CAMERA_STATUS_POSITION 0x4
2300 #define SONYPI_DIRECTION_BACKWARDS 0x4
2302 #define SONYPI_CAMERA_REVISION 8
2303 #define SONYPI_CAMERA_ROMVERSION 9
2305 static int __sony_pic_camera_ready(void)
2309 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
2310 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
2313 static int __sony_pic_camera_off(void)
2316 pr_warn(DRV_PFX "camera control not enabled\n");
2320 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
2321 SONYPI_CAMERA_MUTE_MASK),
2324 if (spic_dev.camera_power) {
2325 sony_pic_call2(0x91, 0);
2326 spic_dev.camera_power = 0;
2331 static int __sony_pic_camera_on(void)
2336 pr_warn(DRV_PFX "camera control not enabled\n");
2340 if (spic_dev.camera_power)
2343 for (j = 5; j > 0; j--) {
2345 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
2347 sony_pic_call1(0x93);
2349 for (i = 400; i > 0; i--) {
2350 if (__sony_pic_camera_ready())
2359 pr_warn(DRV_PFX "failed to power on camera\n");
2363 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
2367 spic_dev.camera_power = 1;
2371 /* External camera command (exported to the motion eye v4l driver) */
2372 int sony_pic_camera_command(int command, u8 value)
2377 mutex_lock(&spic_dev.lock);
2380 case SONY_PIC_COMMAND_SETCAMERA:
2382 __sony_pic_camera_on();
2384 __sony_pic_camera_off();
2386 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
2387 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
2390 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
2391 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
2394 case SONY_PIC_COMMAND_SETCAMERAHUE:
2395 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
2398 case SONY_PIC_COMMAND_SETCAMERACOLOR:
2399 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
2402 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
2403 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
2406 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
2407 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
2410 case SONY_PIC_COMMAND_SETCAMERAAGC:
2411 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
2415 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
2419 mutex_unlock(&spic_dev.lock);
2422 EXPORT_SYMBOL(sony_pic_camera_command);
2424 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
2425 static void __sony_pic_set_wwanpower(u8 state)
2428 if (spic_dev.wwan_power == state)
2430 sony_pic_call2(0xB0, state);
2431 sony_pic_call1(0x82);
2432 spic_dev.wwan_power = state;
2435 static ssize_t sony_pic_wwanpower_store(struct device *dev,
2436 struct device_attribute *attr,
2437 const char *buffer, size_t count)
2439 unsigned long value;
2443 value = simple_strtoul(buffer, NULL, 10);
2444 mutex_lock(&spic_dev.lock);
2445 __sony_pic_set_wwanpower(value);
2446 mutex_unlock(&spic_dev.lock);
2451 static ssize_t sony_pic_wwanpower_show(struct device *dev,
2452 struct device_attribute *attr, char *buffer)
2455 mutex_lock(&spic_dev.lock);
2456 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2457 mutex_unlock(&spic_dev.lock);
2461 /* bluetooth subsystem power state */
2462 static void __sony_pic_set_bluetoothpower(u8 state)
2465 if (spic_dev.bluetooth_power == state)
2467 sony_pic_call2(0x96, state);
2468 sony_pic_call1(0x82);
2469 spic_dev.bluetooth_power = state;
2472 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2473 struct device_attribute *attr,
2474 const char *buffer, size_t count)
2476 unsigned long value;
2480 value = simple_strtoul(buffer, NULL, 10);
2481 mutex_lock(&spic_dev.lock);
2482 __sony_pic_set_bluetoothpower(value);
2483 mutex_unlock(&spic_dev.lock);
2488 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2489 struct device_attribute *attr, char *buffer)
2492 mutex_lock(&spic_dev.lock);
2493 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2494 mutex_unlock(&spic_dev.lock);
2499 /* FAN0 information (reverse engineered from ACPI tables) */
2500 #define SONY_PIC_FAN0_STATUS 0x93
2501 static int sony_pic_set_fanspeed(unsigned long value)
2503 return ec_write(SONY_PIC_FAN0_STATUS, value);
2506 static int sony_pic_get_fanspeed(u8 *value)
2508 return ec_read(SONY_PIC_FAN0_STATUS, value);
2511 static ssize_t sony_pic_fanspeed_store(struct device *dev,
2512 struct device_attribute *attr,
2513 const char *buffer, size_t count)
2515 unsigned long value;
2519 value = simple_strtoul(buffer, NULL, 10);
2520 if (sony_pic_set_fanspeed(value))
2526 static ssize_t sony_pic_fanspeed_show(struct device *dev,
2527 struct device_attribute *attr, char *buffer)
2530 if (sony_pic_get_fanspeed(&value))
2533 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2536 #define SPIC_ATTR(_name, _mode) \
2537 struct device_attribute spic_attr_##_name = __ATTR(_name, \
2538 _mode, sony_pic_## _name ##_show, \
2539 sony_pic_## _name ##_store)
2541 static SPIC_ATTR(bluetoothpower, 0644);
2542 static SPIC_ATTR(wwanpower, 0644);
2543 static SPIC_ATTR(fanspeed, 0644);
2545 static struct attribute *spic_attributes[] = {
2546 &spic_attr_bluetoothpower.attr,
2547 &spic_attr_wwanpower.attr,
2548 &spic_attr_fanspeed.attr,
2552 static struct attribute_group spic_attribute_group = {
2553 .attrs = spic_attributes
2556 /******** SONYPI compatibility **********/
2557 #ifdef CONFIG_SONYPI_COMPAT
2559 /* battery / brightness / temperature addresses */
2560 #define SONYPI_BAT_FLAGS 0x81
2561 #define SONYPI_LCD_LIGHT 0x96
2562 #define SONYPI_BAT1_PCTRM 0xa0
2563 #define SONYPI_BAT1_LEFT 0xa2
2564 #define SONYPI_BAT1_MAXRT 0xa4
2565 #define SONYPI_BAT2_PCTRM 0xa8
2566 #define SONYPI_BAT2_LEFT 0xaa
2567 #define SONYPI_BAT2_MAXRT 0xac
2568 #define SONYPI_BAT1_MAXTK 0xb0
2569 #define SONYPI_BAT1_FULL 0xb2
2570 #define SONYPI_BAT2_MAXTK 0xb8
2571 #define SONYPI_BAT2_FULL 0xba
2572 #define SONYPI_TEMP_STATUS 0xC1
2574 struct sonypi_compat_s {
2575 struct fasync_struct *fifo_async;
2577 spinlock_t fifo_lock;
2578 wait_queue_head_t fifo_proc_list;
2579 atomic_t open_count;
2581 static struct sonypi_compat_s sonypi_compat = {
2582 .open_count = ATOMIC_INIT(0),
2585 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2587 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
2590 static int sonypi_misc_release(struct inode *inode, struct file *file)
2592 atomic_dec(&sonypi_compat.open_count);
2596 static int sonypi_misc_open(struct inode *inode, struct file *file)
2598 /* Flush input queue on first open */
2599 unsigned long flags;
2601 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
2603 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
2604 kfifo_reset(&sonypi_compat.fifo);
2606 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
2611 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2612 size_t count, loff_t *pos)
2617 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
2618 (file->f_flags & O_NONBLOCK))
2621 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
2622 kfifo_len(&sonypi_compat.fifo) != 0);
2626 while (ret < count &&
2627 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
2628 &sonypi_compat.fifo_lock) == sizeof(c))) {
2629 if (put_user(c, buf++))
2635 struct inode *inode = file->f_path.dentry->d_inode;
2636 inode->i_atime = current_fs_time(inode->i_sb);
2642 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2644 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
2645 if (kfifo_len(&sonypi_compat.fifo))
2646 return POLLIN | POLLRDNORM;
2650 static int ec_read16(u8 addr, u16 *value)
2653 if (ec_read(addr, &val_lb))
2655 if (ec_read(addr + 1, &val_hb))
2657 *value = val_lb | (val_hb << 8);
2661 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2665 void __user *argp = (void __user *)arg;
2670 mutex_lock(&spic_dev.lock);
2672 case SONYPI_IOCGBRT:
2673 if (sony_bl_props.dev == NULL) {
2677 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2681 val8 = ((value & 0xff) - 1) << 5;
2682 if (copy_to_user(argp, &val8, sizeof(val8)))
2685 case SONYPI_IOCSBRT:
2686 if (sony_bl_props.dev == NULL) {
2690 if (copy_from_user(&val8, argp, sizeof(val8))) {
2694 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2695 (val8 >> 5) + 1, NULL)) {
2699 /* sync the backlight device status */
2700 sony_bl_props.dev->props.brightness =
2701 sony_backlight_get_brightness(sony_bl_props.dev);
2703 case SONYPI_IOCGBAT1CAP:
2704 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2708 if (copy_to_user(argp, &val16, sizeof(val16)))
2711 case SONYPI_IOCGBAT1REM:
2712 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2716 if (copy_to_user(argp, &val16, sizeof(val16)))
2719 case SONYPI_IOCGBAT2CAP:
2720 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2724 if (copy_to_user(argp, &val16, sizeof(val16)))
2727 case SONYPI_IOCGBAT2REM:
2728 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2732 if (copy_to_user(argp, &val16, sizeof(val16)))
2735 case SONYPI_IOCGBATFLAGS:
2736 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2741 if (copy_to_user(argp, &val8, sizeof(val8)))
2744 case SONYPI_IOCGBLUE:
2745 val8 = spic_dev.bluetooth_power;
2746 if (copy_to_user(argp, &val8, sizeof(val8)))
2749 case SONYPI_IOCSBLUE:
2750 if (copy_from_user(&val8, argp, sizeof(val8))) {
2754 __sony_pic_set_bluetoothpower(val8);
2757 case SONYPI_IOCGFAN:
2758 if (sony_pic_get_fanspeed(&val8)) {
2762 if (copy_to_user(argp, &val8, sizeof(val8)))
2765 case SONYPI_IOCSFAN:
2766 if (copy_from_user(&val8, argp, sizeof(val8))) {
2770 if (sony_pic_set_fanspeed(val8))
2773 /* GET Temperature (useful under APM) */
2774 case SONYPI_IOCGTEMP:
2775 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2779 if (copy_to_user(argp, &val8, sizeof(val8)))
2785 mutex_unlock(&spic_dev.lock);
2789 static const struct file_operations sonypi_misc_fops = {
2790 .owner = THIS_MODULE,
2791 .read = sonypi_misc_read,
2792 .poll = sonypi_misc_poll,
2793 .open = sonypi_misc_open,
2794 .release = sonypi_misc_release,
2795 .fasync = sonypi_misc_fasync,
2796 .unlocked_ioctl = sonypi_misc_ioctl,
2797 .llseek = noop_llseek,
2800 static struct miscdevice sonypi_misc_device = {
2801 .minor = MISC_DYNAMIC_MINOR,
2803 .fops = &sonypi_misc_fops,
2806 static void sonypi_compat_report_event(u8 event)
2808 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
2809 sizeof(event), &sonypi_compat.fifo_lock);
2810 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2811 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2814 static int sonypi_compat_init(void)
2818 spin_lock_init(&sonypi_compat.fifo_lock);
2820 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
2822 pr_err(DRV_PFX "kfifo_alloc failed\n");
2826 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
2829 sonypi_misc_device.minor = minor;
2830 error = misc_register(&sonypi_misc_device);
2832 pr_err(DRV_PFX "misc_register failed\n");
2833 goto err_free_kfifo;
2836 pr_info(DRV_PFX "device allocated minor is %d\n",
2837 sonypi_misc_device.minor);
2842 kfifo_free(&sonypi_compat.fifo);
2846 static void sonypi_compat_exit(void)
2848 misc_deregister(&sonypi_misc_device);
2849 kfifo_free(&sonypi_compat.fifo);
2852 static int sonypi_compat_init(void) { return 0; }
2853 static void sonypi_compat_exit(void) { }
2854 static void sonypi_compat_report_event(u8 event) { }
2855 #endif /* CONFIG_SONYPI_COMPAT */
2861 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2864 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2866 switch (resource->type) {
2867 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
2869 /* start IO enumeration */
2870 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2874 list_add(&ioport->list, &dev->ioports);
2878 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
2879 /* end IO enumeration */
2882 case ACPI_RESOURCE_TYPE_IRQ:
2884 struct acpi_resource_irq *p = &resource->data.irq;
2885 struct sony_pic_irq *interrupt = NULL;
2886 if (!p || !p->interrupt_count) {
2888 * IRQ descriptors may have no IRQ# bits set,
2889 * particularly those those w/ _STA disabled
2891 dprintk("Blank IRQ resource\n");
2894 for (i = 0; i < p->interrupt_count; i++) {
2895 if (!p->interrupts[i]) {
2896 pr_warn(DRV_PFX "Invalid IRQ %d\n",
2900 interrupt = kzalloc(sizeof(*interrupt),
2905 list_add(&interrupt->list, &dev->interrupts);
2906 interrupt->irq.triggering = p->triggering;
2907 interrupt->irq.polarity = p->polarity;
2908 interrupt->irq.sharable = p->sharable;
2909 interrupt->irq.interrupt_count = 1;
2910 interrupt->irq.interrupts[0] = p->interrupts[i];
2914 case ACPI_RESOURCE_TYPE_IO:
2916 struct acpi_resource_io *io = &resource->data.io;
2917 struct sony_pic_ioport *ioport =
2918 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
2920 dprintk("Blank IO resource\n");
2924 if (!ioport->io1.minimum) {
2925 memcpy(&ioport->io1, io, sizeof(*io));
2926 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2927 ioport->io1.address_length);
2929 else if (!ioport->io2.minimum) {
2930 memcpy(&ioport->io2, io, sizeof(*io));
2931 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2932 ioport->io2.address_length);
2935 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
2941 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2944 case ACPI_RESOURCE_TYPE_END_TAG:
2947 return AE_CTRL_TERMINATE;
2950 static int sony_pic_possible_resources(struct acpi_device *device)
2953 acpi_status status = AE_OK;
2958 /* get device status */
2959 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2960 dprintk("Evaluating _STA\n");
2961 result = acpi_bus_get_status(device);
2963 pr_warn(DRV_PFX "Unable to read status\n");
2967 if (!device->status.enabled)
2968 dprintk("Device disabled\n");
2970 dprintk("Device enabled\n");
2973 * Query and parse 'method'
2975 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2976 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2977 sony_pic_read_possible_resource, &spic_dev);
2978 if (ACPI_FAILURE(status)) {
2979 pr_warn(DRV_PFX "Failure evaluating %s\n",
2988 * Disable the spic device by calling its _DIS method
2990 static int sony_pic_disable(struct acpi_device *device)
2992 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2995 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
2998 dprintk("Device disabled\n");
3004 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
3006 * Call _SRS to set current resources
3008 static int sony_pic_enable(struct acpi_device *device,
3009 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
3013 /* Type 1 resource layout is:
3019 * Type 2 and 3 resource layout is:
3025 struct acpi_resource res1;
3026 struct acpi_resource res2;
3027 struct acpi_resource res3;
3028 struct acpi_resource res4;
3030 struct acpi_buffer buffer = { 0, NULL };
3032 if (!ioport || !irq)
3035 /* init acpi_buffer */
3036 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
3040 buffer.length = sizeof(*resource) + 1;
3041 buffer.pointer = resource;
3043 /* setup Type 1 resources */
3044 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
3046 /* setup io resources */
3047 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3048 resource->res1.length = sizeof(struct acpi_resource);
3049 memcpy(&resource->res1.data.io, &ioport->io1,
3050 sizeof(struct acpi_resource_io));
3052 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
3053 resource->res2.length = sizeof(struct acpi_resource);
3054 memcpy(&resource->res2.data.io, &ioport->io2,
3055 sizeof(struct acpi_resource_io));
3057 /* setup irq resource */
3058 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
3059 resource->res3.length = sizeof(struct acpi_resource);
3060 memcpy(&resource->res3.data.irq, &irq->irq,
3061 sizeof(struct acpi_resource_irq));
3062 /* we requested a shared irq */
3063 resource->res3.data.irq.sharable = ACPI_SHARED;
3065 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
3068 /* setup Type 2/3 resources */
3070 /* setup io resource */
3071 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
3072 resource->res1.length = sizeof(struct acpi_resource);
3073 memcpy(&resource->res1.data.io, &ioport->io1,
3074 sizeof(struct acpi_resource_io));
3076 /* setup irq resource */
3077 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
3078 resource->res2.length = sizeof(struct acpi_resource);
3079 memcpy(&resource->res2.data.irq, &irq->irq,
3080 sizeof(struct acpi_resource_irq));
3081 /* we requested a shared irq */
3082 resource->res2.data.irq.sharable = ACPI_SHARED;
3084 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
3087 /* Attempt to set the resource */
3088 dprintk("Evaluating _SRS\n");
3089 status = acpi_set_current_resources(device->handle, &buffer);
3091 /* check for total failure */
3092 if (ACPI_FAILURE(status)) {
3093 pr_err(DRV_PFX "Error evaluating _SRS\n");
3098 /* Necessary device initializations calls (from sonypi) */
3099 sony_pic_call1(0x82);
3100 sony_pic_call2(0x81, 0xff);
3101 sony_pic_call1(compat ? 0x92 : 0x82);
3110 * ISR: some event is available
3113 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
3118 u8 device_event = 0;
3120 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
3122 ev = inb_p(dev->cur_ioport->io1.minimum);
3123 if (dev->cur_ioport->io2.minimum)
3124 data_mask = inb_p(dev->cur_ioport->io2.minimum);
3126 data_mask = inb_p(dev->cur_ioport->io1.minimum +
3127 dev->evport_offset);
3129 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3130 ev, data_mask, dev->cur_ioport->io1.minimum,
3131 dev->evport_offset);
3133 if (ev == 0x00 || ev == 0xff)
3136 for (i = 0; dev->event_types[i].mask; i++) {
3138 if ((data_mask & dev->event_types[i].data) !=
3139 dev->event_types[i].data)
3142 if (!(mask & dev->event_types[i].mask))
3145 for (j = 0; dev->event_types[i].events[j].event; j++) {
3146 if (ev == dev->event_types[i].events[j].data) {
3148 dev->event_types[i].events[j].event;
3149 /* some events may require ignoring */
3156 /* Still not able to decode the event try to pass
3157 * it over to the minidriver
3159 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
3162 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
3163 ev, data_mask, dev->cur_ioport->io1.minimum,
3164 dev->evport_offset);
3168 sony_laptop_report_input_event(device_event);
3169 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
3170 sonypi_compat_report_event(device_event);
3179 static int sony_pic_remove(struct acpi_device *device, int type)
3181 struct sony_pic_ioport *io, *tmp_io;
3182 struct sony_pic_irq *irq, *tmp_irq;
3184 if (sony_pic_disable(device)) {
3185 pr_err(DRV_PFX "Couldn't disable device.\n");
3189 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3190 release_region(spic_dev.cur_ioport->io1.minimum,
3191 spic_dev.cur_ioport->io1.address_length);
3192 if (spic_dev.cur_ioport->io2.minimum)
3193 release_region(spic_dev.cur_ioport->io2.minimum,
3194 spic_dev.cur_ioport->io2.address_length);
3196 sonypi_compat_exit();
3198 sony_laptop_remove_input();
3201 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3204 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3205 list_del(&io->list);
3208 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3209 list_del(&irq->list);
3212 spic_dev.cur_ioport = NULL;
3213 spic_dev.cur_irq = NULL;
3215 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
3219 static int sony_pic_add(struct acpi_device *device)
3222 struct sony_pic_ioport *io, *tmp_io;
3223 struct sony_pic_irq *irq, *tmp_irq;
3225 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
3226 SONY_LAPTOP_DRIVER_VERSION);
3228 spic_dev.acpi_dev = device;
3229 strcpy(acpi_device_class(device), "sony/hotkey");
3230 sony_pic_detect_device_type(&spic_dev);
3231 mutex_init(&spic_dev.lock);
3233 /* read _PRS resources */
3234 result = sony_pic_possible_resources(device);
3236 pr_err(DRV_PFX "Unable to read possible resources.\n");
3237 goto err_free_resources;
3240 /* setup input devices and helper fifo */
3241 result = sony_laptop_setup_input(device);
3243 pr_err(DRV_PFX "Unable to create input devices.\n");
3244 goto err_free_resources;
3247 if (sonypi_compat_init())
3248 goto err_remove_input;
3250 /* request io port */
3251 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
3252 if (request_region(io->io1.minimum, io->io1.address_length,
3253 "Sony Programmable I/O Device")) {
3254 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
3255 io->io1.minimum, io->io1.maximum,
3256 io->io1.address_length);
3257 /* Type 1 have 2 ioports */
3258 if (io->io2.minimum) {
3259 if (request_region(io->io2.minimum,
3260 io->io2.address_length,
3261 "Sony Programmable I/O Device")) {
3262 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
3263 io->io2.minimum, io->io2.maximum,
3264 io->io2.address_length);
3265 spic_dev.cur_ioport = io;
3269 dprintk("Unable to get I/O port2: "
3270 "0x%.4x (0x%.4x) + 0x%.2x\n",
3271 io->io2.minimum, io->io2.maximum,
3272 io->io2.address_length);
3273 release_region(io->io1.minimum,
3274 io->io1.address_length);
3278 spic_dev.cur_ioport = io;
3283 if (!spic_dev.cur_ioport) {
3284 pr_err(DRV_PFX "Failed to request_region.\n");
3286 goto err_remove_compat;
3290 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
3291 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
3292 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
3293 dprintk("IRQ: %d - triggering: %d - "
3294 "polarity: %d - shr: %d\n",
3295 irq->irq.interrupts[0],
3296 irq->irq.triggering,
3299 spic_dev.cur_irq = irq;
3303 if (!spic_dev.cur_irq) {
3304 pr_err(DRV_PFX "Failed to request_irq.\n");
3306 goto err_release_region;
3309 /* set resource status _SRS */
3310 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3312 pr_err(DRV_PFX "Couldn't enable device.\n");
3316 spic_dev.bluetooth_power = -1;
3317 /* create device attributes */
3318 result = sony_pf_add();
3320 goto err_disable_device;
3322 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
3332 sony_pic_disable(device);
3335 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
3338 release_region(spic_dev.cur_ioport->io1.minimum,
3339 spic_dev.cur_ioport->io1.address_length);
3340 if (spic_dev.cur_ioport->io2.minimum)
3341 release_region(spic_dev.cur_ioport->io2.minimum,
3342 spic_dev.cur_ioport->io2.address_length);
3345 sonypi_compat_exit();
3348 sony_laptop_remove_input();
3351 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
3352 list_del(&io->list);
3355 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
3356 list_del(&irq->list);
3359 spic_dev.cur_ioport = NULL;
3360 spic_dev.cur_irq = NULL;
3365 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
3367 if (sony_pic_disable(device))
3372 static int sony_pic_resume(struct acpi_device *device)
3374 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
3378 static const struct acpi_device_id sony_pic_device_ids[] = {
3383 static struct acpi_driver sony_pic_driver = {
3384 .name = SONY_PIC_DRIVER_NAME,
3385 .class = SONY_PIC_CLASS,
3386 .ids = sony_pic_device_ids,
3387 .owner = THIS_MODULE,
3389 .add = sony_pic_add,
3390 .remove = sony_pic_remove,
3391 .suspend = sony_pic_suspend,
3392 .resume = sony_pic_resume,
3396 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
3398 .ident = "Sony Vaio",
3400 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3401 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
3405 .ident = "Sony Vaio",
3407 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
3408 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
3414 static int __init sony_laptop_init(void)
3418 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3419 result = acpi_bus_register_driver(&sony_pic_driver);
3421 pr_err(DRV_PFX "Unable to register SPIC driver.");
3424 spic_drv_registered = 1;
3427 result = acpi_bus_register_driver(&sony_nc_driver);
3429 pr_err(DRV_PFX "Unable to register SNC driver.");
3430 goto out_unregister_pic;
3436 if (spic_drv_registered)
3437 acpi_bus_unregister_driver(&sony_pic_driver);
3442 static void __exit sony_laptop_exit(void)
3444 acpi_bus_unregister_driver(&sony_nc_driver);
3445 if (spic_drv_registered)
3446 acpi_bus_unregister_driver(&sony_pic_driver);
3449 module_init(sony_laptop_init);
3450 module_exit(sony_laptop_exit);