sony-laptop: use pr_<level> for messages
[linux-2.6/btrfs-unstable.git] / drivers / platform / x86 / sony-laptop.c
blob89d66c70d8280c4f998f75902c0d15a790ca3513
1 /*
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
11 * material from:
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/init.h>
49 #include <linux/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>
71 #endif
73 #define DRV_PFX "sony-laptop: "
74 #define dprintk(msg...) do { \
75 if (debug) \
76 pr_warn(DRV_PFX msg); \
77 } while (0)
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);
94 static int debug;
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)");
126 #endif
128 enum sony_nc_rfkill {
129 SONY_WIFI,
130 SONY_BLUETOOTH,
131 SONY_WWAN,
132 SONY_WIMAX,
133 N_SONY_RFKILL,
136 static int sony_rfkill_handle;
137 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
138 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
139 static void sony_nc_rfkill_update(void);
141 /*********** Input Devices ***********/
143 #define SONY_LAPTOP_BUF_SIZE 128
144 struct sony_laptop_input_s {
145 atomic_t users;
146 struct input_dev *jog_dev;
147 struct input_dev *key_dev;
148 struct kfifo fifo;
149 spinlock_t fifo_lock;
150 struct timer_list release_key_timer;
153 static struct sony_laptop_input_s sony_laptop_input = {
154 .users = ATOMIC_INIT(0),
157 struct sony_laptop_keypress {
158 struct input_dev *dev;
159 int key;
162 /* Correspondance table between sonypi events
163 * and input layer indexes in the keymap
165 static int sony_laptop_input_index[] = {
166 -1, /* 0 no event */
167 -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
168 -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
169 -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
170 -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
171 -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
172 -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
173 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
174 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
175 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
176 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
177 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
178 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
179 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
180 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
181 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
182 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
183 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
184 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
185 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
186 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
187 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
188 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
189 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
190 17, /* 24 SONYPI_EVENT_FNKEY_1 */
191 18, /* 25 SONYPI_EVENT_FNKEY_2 */
192 19, /* 26 SONYPI_EVENT_FNKEY_D */
193 20, /* 27 SONYPI_EVENT_FNKEY_E */
194 21, /* 28 SONYPI_EVENT_FNKEY_F */
195 22, /* 29 SONYPI_EVENT_FNKEY_S */
196 23, /* 30 SONYPI_EVENT_FNKEY_B */
197 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
198 25, /* 32 SONYPI_EVENT_PKEY_P1 */
199 26, /* 33 SONYPI_EVENT_PKEY_P2 */
200 27, /* 34 SONYPI_EVENT_PKEY_P3 */
201 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
202 -1, /* 36 SONYPI_EVENT_LID_CLOSED */
203 -1, /* 37 SONYPI_EVENT_LID_OPENED */
204 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
205 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
206 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
207 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
208 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
209 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
210 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
211 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
212 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
213 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
214 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
215 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
216 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
217 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
218 43, /* 52 SONYPI_EVENT_MEYE_FACE */
219 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
220 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
221 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
222 -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
223 -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
224 -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
225 -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
226 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
227 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
228 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
229 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
230 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
231 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
232 53, /* 66 SONYPI_EVENT_PKEY_P4 */
233 54, /* 67 SONYPI_EVENT_PKEY_P5 */
234 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
235 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
236 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
237 -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
238 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
239 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
242 static int sony_laptop_input_keycode_map[] = {
243 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
244 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
245 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
246 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
247 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
248 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
249 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
250 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
251 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
252 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
253 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
254 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
255 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
256 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
257 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
258 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
259 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
260 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
261 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
262 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
263 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
264 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
265 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
266 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
267 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
268 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
269 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
270 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
271 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
272 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
273 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
274 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
275 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
276 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
277 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
278 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
279 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
280 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
281 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
282 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
283 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
284 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
285 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
286 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
287 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
288 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
289 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
290 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
291 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
292 KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
293 KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
294 KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
295 KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
296 KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
297 KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
298 KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
299 KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
300 KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
301 KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
302 KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
305 /* release buttons after a short delay if pressed */
306 static void do_sony_laptop_release_key(unsigned long unused)
308 struct sony_laptop_keypress kp;
309 unsigned long flags;
311 spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
313 if (kfifo_out(&sony_laptop_input.fifo,
314 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
315 input_report_key(kp.dev, kp.key, 0);
316 input_sync(kp.dev);
319 /* If there is something in the fifo schedule next release. */
320 if (kfifo_len(&sony_laptop_input.fifo) != 0)
321 mod_timer(&sony_laptop_input.release_key_timer,
322 jiffies + msecs_to_jiffies(10));
324 spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
327 /* forward event to the input subsystem */
328 static void sony_laptop_report_input_event(u8 event)
330 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
331 struct input_dev *key_dev = sony_laptop_input.key_dev;
332 struct sony_laptop_keypress kp = { NULL };
334 if (event == SONYPI_EVENT_FNKEY_RELEASED ||
335 event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
336 /* Nothing, not all VAIOs generate this event */
337 return;
340 /* report events */
341 switch (event) {
342 /* jog_dev events */
343 case SONYPI_EVENT_JOGDIAL_UP:
344 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
345 input_report_rel(jog_dev, REL_WHEEL, 1);
346 input_sync(jog_dev);
347 return;
349 case SONYPI_EVENT_JOGDIAL_DOWN:
350 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
351 input_report_rel(jog_dev, REL_WHEEL, -1);
352 input_sync(jog_dev);
353 return;
355 /* key_dev events */
356 case SONYPI_EVENT_JOGDIAL_PRESSED:
357 kp.key = BTN_MIDDLE;
358 kp.dev = jog_dev;
359 break;
361 default:
362 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
363 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
364 break;
366 if (sony_laptop_input_index[event] != -1) {
367 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
368 if (kp.key != KEY_UNKNOWN)
369 kp.dev = key_dev;
371 break;
374 if (kp.dev) {
375 input_report_key(kp.dev, kp.key, 1);
376 /* we emit the scancode so we can always remap the key */
377 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
378 input_sync(kp.dev);
380 /* schedule key release */
381 kfifo_in_locked(&sony_laptop_input.fifo,
382 (unsigned char *)&kp, sizeof(kp),
383 &sony_laptop_input.fifo_lock);
384 mod_timer(&sony_laptop_input.release_key_timer,
385 jiffies + msecs_to_jiffies(10));
386 } else
387 dprintk("unknown input event %.2x\n", event);
390 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
392 struct input_dev *jog_dev;
393 struct input_dev *key_dev;
394 int i;
395 int error;
397 /* don't run again if already initialized */
398 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
399 return 0;
401 /* kfifo */
402 spin_lock_init(&sony_laptop_input.fifo_lock);
403 error = kfifo_alloc(&sony_laptop_input.fifo,
404 SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
405 if (error) {
406 pr_err(DRV_PFX "kfifo_alloc failed\n");
407 goto err_dec_users;
410 setup_timer(&sony_laptop_input.release_key_timer,
411 do_sony_laptop_release_key, 0);
413 /* input keys */
414 key_dev = input_allocate_device();
415 if (!key_dev) {
416 error = -ENOMEM;
417 goto err_free_kfifo;
420 key_dev->name = "Sony Vaio Keys";
421 key_dev->id.bustype = BUS_ISA;
422 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
423 key_dev->dev.parent = &acpi_device->dev;
425 /* Initialize the Input Drivers: special keys */
426 input_set_capability(key_dev, EV_MSC, MSC_SCAN);
428 __set_bit(EV_KEY, key_dev->evbit);
429 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
430 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
431 key_dev->keycode = &sony_laptop_input_keycode_map;
432 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
433 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
434 __clear_bit(KEY_RESERVED, key_dev->keybit);
436 error = input_register_device(key_dev);
437 if (error)
438 goto err_free_keydev;
440 sony_laptop_input.key_dev = key_dev;
442 /* jogdial */
443 jog_dev = input_allocate_device();
444 if (!jog_dev) {
445 error = -ENOMEM;
446 goto err_unregister_keydev;
449 jog_dev->name = "Sony Vaio Jogdial";
450 jog_dev->id.bustype = BUS_ISA;
451 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
452 key_dev->dev.parent = &acpi_device->dev;
454 input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
455 input_set_capability(jog_dev, EV_REL, REL_WHEEL);
457 error = input_register_device(jog_dev);
458 if (error)
459 goto err_free_jogdev;
461 sony_laptop_input.jog_dev = jog_dev;
463 return 0;
465 err_free_jogdev:
466 input_free_device(jog_dev);
468 err_unregister_keydev:
469 input_unregister_device(key_dev);
470 /* to avoid kref underflow below at input_free_device */
471 key_dev = NULL;
473 err_free_keydev:
474 input_free_device(key_dev);
476 err_free_kfifo:
477 kfifo_free(&sony_laptop_input.fifo);
479 err_dec_users:
480 atomic_dec(&sony_laptop_input.users);
481 return error;
484 static void sony_laptop_remove_input(void)
486 struct sony_laptop_keypress kp = { NULL };
488 /* Cleanup only after the last user has gone */
489 if (!atomic_dec_and_test(&sony_laptop_input.users))
490 return;
492 del_timer_sync(&sony_laptop_input.release_key_timer);
495 * Generate key-up events for remaining keys. Note that we don't
496 * need locking since nobody is adding new events to the kfifo.
498 while (kfifo_out(&sony_laptop_input.fifo,
499 (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
500 input_report_key(kp.dev, kp.key, 0);
501 input_sync(kp.dev);
504 /* destroy input devs */
505 input_unregister_device(sony_laptop_input.key_dev);
506 sony_laptop_input.key_dev = NULL;
508 if (sony_laptop_input.jog_dev) {
509 input_unregister_device(sony_laptop_input.jog_dev);
510 sony_laptop_input.jog_dev = NULL;
513 kfifo_free(&sony_laptop_input.fifo);
516 /*********** Platform Device ***********/
518 static atomic_t sony_pf_users = ATOMIC_INIT(0);
519 static struct platform_driver sony_pf_driver = {
520 .driver = {
521 .name = "sony-laptop",
522 .owner = THIS_MODULE,
525 static struct platform_device *sony_pf_device;
527 static int sony_pf_add(void)
529 int ret = 0;
531 /* don't run again if already initialized */
532 if (atomic_add_return(1, &sony_pf_users) > 1)
533 return 0;
535 ret = platform_driver_register(&sony_pf_driver);
536 if (ret)
537 goto out;
539 sony_pf_device = platform_device_alloc("sony-laptop", -1);
540 if (!sony_pf_device) {
541 ret = -ENOMEM;
542 goto out_platform_registered;
545 ret = platform_device_add(sony_pf_device);
546 if (ret)
547 goto out_platform_alloced;
549 return 0;
551 out_platform_alloced:
552 platform_device_put(sony_pf_device);
553 sony_pf_device = NULL;
554 out_platform_registered:
555 platform_driver_unregister(&sony_pf_driver);
556 out:
557 atomic_dec(&sony_pf_users);
558 return ret;
561 static void sony_pf_remove(void)
563 /* deregister only after the last user has gone */
564 if (!atomic_dec_and_test(&sony_pf_users))
565 return;
567 platform_device_unregister(sony_pf_device);
568 platform_driver_unregister(&sony_pf_driver);
571 /*********** SNC (SNY5001) Device ***********/
573 /* the device uses 1-based values, while the backlight subsystem uses
574 0-based values */
575 #define SONY_MAX_BRIGHTNESS 8
577 #define SNC_VALIDATE_IN 0
578 #define SNC_VALIDATE_OUT 1
580 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
581 char *);
582 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
583 const char *, size_t);
584 static int boolean_validate(const int, const int);
585 static int brightness_default_validate(const int, const int);
587 struct sony_nc_value {
588 char *name; /* name of the entry */
589 char **acpiget; /* names of the ACPI get function */
590 char **acpiset; /* names of the ACPI set function */
591 int (*validate)(const int, const int); /* input/output validation */
592 int value; /* current setting */
593 int valid; /* Has ever been set */
594 int debug; /* active only in debug mode ? */
595 struct device_attribute devattr; /* sysfs atribute */
598 #define SNC_HANDLE_NAMES(_name, _values...) \
599 static char *snc_##_name[] = { _values, NULL }
601 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
603 .name = __stringify(_name), \
604 .acpiget = _getters, \
605 .acpiset = _setters, \
606 .validate = _validate, \
607 .debug = _debug, \
608 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
611 #define SNC_HANDLE_NULL { .name = NULL }
613 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
615 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
616 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
618 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
619 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
621 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
622 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
624 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
625 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
627 SNC_HANDLE_NAMES(lidstate_get, "GLID");
629 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
630 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
632 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
633 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
635 SNC_HANDLE_NAMES(PID_get, "GPID");
637 SNC_HANDLE_NAMES(CTR_get, "GCTR");
638 SNC_HANDLE_NAMES(CTR_set, "SCTR");
640 SNC_HANDLE_NAMES(PCR_get, "GPCR");
641 SNC_HANDLE_NAMES(PCR_set, "SPCR");
643 SNC_HANDLE_NAMES(CMI_get, "GCMI");
644 SNC_HANDLE_NAMES(CMI_set, "SCMI");
646 static struct sony_nc_value sony_nc_values[] = {
647 SNC_HANDLE(brightness_default, snc_brightness_def_get,
648 snc_brightness_def_set, brightness_default_validate, 0),
649 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
650 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
651 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
652 boolean_validate, 0),
653 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
654 boolean_validate, 1),
655 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
656 boolean_validate, 0),
657 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
658 boolean_validate, 0),
659 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
660 boolean_validate, 0),
661 /* unknown methods */
662 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
663 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
664 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
665 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
666 SNC_HANDLE_NULL
669 static acpi_handle sony_nc_acpi_handle;
670 static struct acpi_device *sony_nc_acpi_device = NULL;
673 * acpi_evaluate_object wrappers
675 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
677 struct acpi_buffer output;
678 union acpi_object out_obj;
679 acpi_status status;
681 output.length = sizeof(out_obj);
682 output.pointer = &out_obj;
684 status = acpi_evaluate_object(handle, name, NULL, &output);
685 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
686 *result = out_obj.integer.value;
687 return 0;
690 pr_warn(DRV_PFX "acpi_callreadfunc failed\n");
692 return -1;
695 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
696 int *result)
698 struct acpi_object_list params;
699 union acpi_object in_obj;
700 struct acpi_buffer output;
701 union acpi_object out_obj;
702 acpi_status status;
704 params.count = 1;
705 params.pointer = &in_obj;
706 in_obj.type = ACPI_TYPE_INTEGER;
707 in_obj.integer.value = value;
709 output.length = sizeof(out_obj);
710 output.pointer = &out_obj;
712 status = acpi_evaluate_object(handle, name, &params, &output);
713 if (status == AE_OK) {
714 if (result != NULL) {
715 if (out_obj.type != ACPI_TYPE_INTEGER) {
716 pr_warn(DRV_PFX "acpi_evaluate_object bad "
717 "return type\n");
718 return -1;
720 *result = out_obj.integer.value;
722 return 0;
725 pr_warn(DRV_PFX "acpi_evaluate_object failed\n");
727 return -1;
730 static int sony_find_snc_handle(int handle)
732 int i;
733 int result;
735 for (i = 0x20; i < 0x30; i++) {
736 acpi_callsetfunc(sony_nc_acpi_handle, "SN00", i, &result);
737 if (result == handle) {
738 dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
739 handle, i - 0x20);
740 return i-0x20;
744 dprintk("handle 0x%.4x not found\n", handle);
745 return -1;
748 static int sony_call_snc_handle(int handle, int argument, int *result)
750 int ret = 0;
751 int offset = sony_find_snc_handle(handle);
753 if (offset < 0)
754 return -1;
756 ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
757 result);
758 dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
759 *result);
760 return ret;
764 * sony_nc_values input/output validate functions
767 /* brightness_default_validate:
769 * manipulate input output values to keep consistency with the
770 * backlight framework for which brightness values are 0-based.
772 static int brightness_default_validate(const int direction, const int value)
774 switch (direction) {
775 case SNC_VALIDATE_OUT:
776 return value - 1;
777 case SNC_VALIDATE_IN:
778 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
779 return value + 1;
781 return -EINVAL;
784 /* boolean_validate:
786 * on input validate boolean values 0/1, on output just pass the
787 * received value.
789 static int boolean_validate(const int direction, const int value)
791 if (direction == SNC_VALIDATE_IN) {
792 if (value != 0 && value != 1)
793 return -EINVAL;
795 return value;
799 * Sysfs show/store common to all sony_nc_values
801 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
802 char *buffer)
804 int value;
805 struct sony_nc_value *item =
806 container_of(attr, struct sony_nc_value, devattr);
808 if (!*item->acpiget)
809 return -EIO;
811 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
812 return -EIO;
814 if (item->validate)
815 value = item->validate(SNC_VALIDATE_OUT, value);
817 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
820 static ssize_t sony_nc_sysfs_store(struct device *dev,
821 struct device_attribute *attr,
822 const char *buffer, size_t count)
824 int value;
825 struct sony_nc_value *item =
826 container_of(attr, struct sony_nc_value, devattr);
828 if (!item->acpiset)
829 return -EIO;
831 if (count > 31)
832 return -EINVAL;
834 value = simple_strtoul(buffer, NULL, 10);
836 if (item->validate)
837 value = item->validate(SNC_VALIDATE_IN, value);
839 if (value < 0)
840 return value;
842 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
843 return -EIO;
844 item->value = value;
845 item->valid = 1;
846 return count;
851 * Backlight device
853 static int sony_backlight_update_status(struct backlight_device *bd)
855 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
856 bd->props.brightness + 1, NULL);
859 static int sony_backlight_get_brightness(struct backlight_device *bd)
861 int value;
863 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
864 return 0;
865 /* brightness levels are 1-based, while backlight ones are 0-based */
866 return value - 1;
869 static struct backlight_device *sony_backlight_device;
870 static const struct backlight_ops sony_backlight_ops = {
871 .update_status = sony_backlight_update_status,
872 .get_brightness = sony_backlight_get_brightness,
876 * New SNC-only Vaios event mapping to driver known keys
878 struct sony_nc_event {
879 u8 data;
880 u8 event;
883 static struct sony_nc_event sony_100_events[] = {
884 { 0x90, SONYPI_EVENT_PKEY_P1 },
885 { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
886 { 0x91, SONYPI_EVENT_PKEY_P2 },
887 { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
888 { 0x81, SONYPI_EVENT_FNKEY_F1 },
889 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
890 { 0x82, SONYPI_EVENT_FNKEY_F2 },
891 { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
892 { 0x83, SONYPI_EVENT_FNKEY_F3 },
893 { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
894 { 0x84, SONYPI_EVENT_FNKEY_F4 },
895 { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
896 { 0x85, SONYPI_EVENT_FNKEY_F5 },
897 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
898 { 0x86, SONYPI_EVENT_FNKEY_F6 },
899 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
900 { 0x87, SONYPI_EVENT_FNKEY_F7 },
901 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
902 { 0x89, SONYPI_EVENT_FNKEY_F9 },
903 { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
904 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
905 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
906 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
907 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
908 { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
909 { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
910 { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
911 { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
912 { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
913 { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
914 { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
915 { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
916 { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
917 { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
918 { 0xa6, SONYPI_EVENT_HELP_PRESSED },
919 { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
920 { 0, 0 },
923 static struct sony_nc_event sony_127_events[] = {
924 { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
925 { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
926 { 0x82, SONYPI_EVENT_PKEY_P1 },
927 { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
928 { 0x83, SONYPI_EVENT_PKEY_P2 },
929 { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
930 { 0x84, SONYPI_EVENT_PKEY_P3 },
931 { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
932 { 0x85, SONYPI_EVENT_PKEY_P4 },
933 { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
934 { 0x86, SONYPI_EVENT_PKEY_P5 },
935 { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
936 { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
937 { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
938 { 0, 0 },
942 * ACPI callbacks
944 static void sony_nc_notify(struct acpi_device *device, u32 event)
946 u32 ev = event;
948 if (ev >= 0x90) {
949 /* New-style event */
950 int result;
951 int key_handle = 0;
952 ev -= 0x90;
954 if (sony_find_snc_handle(0x100) == ev)
955 key_handle = 0x100;
956 if (sony_find_snc_handle(0x127) == ev)
957 key_handle = 0x127;
959 if (key_handle) {
960 struct sony_nc_event *key_event;
962 if (sony_call_snc_handle(key_handle, 0x200, &result)) {
963 dprintk("sony_nc_notify, unable to decode"
964 " event 0x%.2x 0x%.2x\n", key_handle,
965 ev);
966 /* restore the original event */
967 ev = event;
968 } else {
969 ev = result & 0xFF;
971 if (key_handle == 0x100)
972 key_event = sony_100_events;
973 else
974 key_event = sony_127_events;
976 for (; key_event->data; key_event++) {
977 if (key_event->data == ev) {
978 ev = key_event->event;
979 break;
983 if (!key_event->data)
984 pr_info(DRV_PFX
985 "Unknown event: 0x%x 0x%x\n",
986 key_handle,
987 ev);
988 else
989 sony_laptop_report_input_event(ev);
991 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
992 sony_nc_rfkill_update();
993 return;
995 } else
996 sony_laptop_report_input_event(ev);
998 dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
999 acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
1002 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
1003 void *context, void **return_value)
1005 struct acpi_device_info *info;
1007 if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
1008 pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
1009 (char *)&info->name, info->param_count);
1011 kfree(info);
1014 return AE_OK;
1018 * ACPI device
1020 static int sony_nc_function_setup(struct acpi_device *device)
1022 int result;
1024 /* Enable all events */
1025 acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1027 /* Setup hotkeys */
1028 sony_call_snc_handle(0x0100, 0, &result);
1029 sony_call_snc_handle(0x0101, 0, &result);
1030 sony_call_snc_handle(0x0102, 0x100, &result);
1031 sony_call_snc_handle(0x0127, 0, &result);
1033 return 0;
1036 static int sony_nc_resume(struct acpi_device *device)
1038 struct sony_nc_value *item;
1039 acpi_handle handle;
1041 for (item = sony_nc_values; item->name; item++) {
1042 int ret;
1044 if (!item->valid)
1045 continue;
1046 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
1047 item->value, NULL);
1048 if (ret < 0) {
1049 pr_err(DRV_PFX "%s: %d\n", __func__, ret);
1050 break;
1054 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1055 &handle))) {
1056 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1057 dprintk("ECON Method failed\n");
1060 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1061 &handle))) {
1062 dprintk("Doing SNC setup\n");
1063 sony_nc_function_setup(device);
1066 /* set the last requested brightness level */
1067 if (sony_backlight_device &&
1068 sony_backlight_update_status(sony_backlight_device) < 0)
1069 pr_warn(DRV_PFX "unable to restore brightness level\n");
1071 /* re-read rfkill state */
1072 sony_nc_rfkill_update();
1074 return 0;
1077 static void sony_nc_rfkill_cleanup(void)
1079 int i;
1081 for (i = 0; i < N_SONY_RFKILL; i++) {
1082 if (sony_rfkill_devices[i]) {
1083 rfkill_unregister(sony_rfkill_devices[i]);
1084 rfkill_destroy(sony_rfkill_devices[i]);
1089 static int sony_nc_rfkill_set(void *data, bool blocked)
1091 int result;
1092 int argument = sony_rfkill_address[(long) data] + 0x100;
1094 if (!blocked)
1095 argument |= 0xff0000;
1097 return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1100 static const struct rfkill_ops sony_rfkill_ops = {
1101 .set_block = sony_nc_rfkill_set,
1104 static int sony_nc_setup_rfkill(struct acpi_device *device,
1105 enum sony_nc_rfkill nc_type)
1107 int err = 0;
1108 struct rfkill *rfk;
1109 enum rfkill_type type;
1110 const char *name;
1111 int result;
1112 bool hwblock;
1114 switch (nc_type) {
1115 case SONY_WIFI:
1116 type = RFKILL_TYPE_WLAN;
1117 name = "sony-wifi";
1118 break;
1119 case SONY_BLUETOOTH:
1120 type = RFKILL_TYPE_BLUETOOTH;
1121 name = "sony-bluetooth";
1122 break;
1123 case SONY_WWAN:
1124 type = RFKILL_TYPE_WWAN;
1125 name = "sony-wwan";
1126 break;
1127 case SONY_WIMAX:
1128 type = RFKILL_TYPE_WIMAX;
1129 name = "sony-wimax";
1130 break;
1131 default:
1132 return -EINVAL;
1135 rfk = rfkill_alloc(name, &device->dev, type,
1136 &sony_rfkill_ops, (void *)nc_type);
1137 if (!rfk)
1138 return -ENOMEM;
1140 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1141 hwblock = !(result & 0x1);
1142 rfkill_set_hw_state(rfk, hwblock);
1144 err = rfkill_register(rfk);
1145 if (err) {
1146 rfkill_destroy(rfk);
1147 return err;
1149 sony_rfkill_devices[nc_type] = rfk;
1150 return err;
1153 static void sony_nc_rfkill_update(void)
1155 enum sony_nc_rfkill i;
1156 int result;
1157 bool hwblock;
1159 sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1160 hwblock = !(result & 0x1);
1162 for (i = 0; i < N_SONY_RFKILL; i++) {
1163 int argument = sony_rfkill_address[i];
1165 if (!sony_rfkill_devices[i])
1166 continue;
1168 if (hwblock) {
1169 if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1170 /* we already know we're blocked */
1172 continue;
1175 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1176 rfkill_set_states(sony_rfkill_devices[i],
1177 !(result & 0xf), false);
1181 static void sony_nc_rfkill_setup(struct acpi_device *device)
1183 int offset;
1184 u8 dev_code, i;
1185 acpi_status status;
1186 struct acpi_object_list params;
1187 union acpi_object in_obj;
1188 union acpi_object *device_enum;
1189 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1191 offset = sony_find_snc_handle(0x124);
1192 if (offset == -1) {
1193 offset = sony_find_snc_handle(0x135);
1194 if (offset == -1)
1195 return;
1196 else
1197 sony_rfkill_handle = 0x135;
1198 } else
1199 sony_rfkill_handle = 0x124;
1200 dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
1202 /* need to read the whole buffer returned by the acpi call to SN06
1203 * here otherwise we may miss some features
1205 params.count = 1;
1206 params.pointer = &in_obj;
1207 in_obj.type = ACPI_TYPE_INTEGER;
1208 in_obj.integer.value = offset;
1209 status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1210 &buffer);
1211 if (ACPI_FAILURE(status)) {
1212 dprintk("Radio device enumeration failed\n");
1213 return;
1216 device_enum = (union acpi_object *) buffer.pointer;
1217 if (!device_enum || device_enum->type != ACPI_TYPE_BUFFER) {
1218 pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
1219 device_enum->type);
1220 goto out_no_enum;
1223 /* the buffer is filled with magic numbers describing the devices
1224 * available, 0xff terminates the enumeration
1226 for (i = 0; i < device_enum->buffer.length; i++) {
1228 dev_code = *(device_enum->buffer.pointer + i);
1229 if (dev_code == 0xff)
1230 break;
1232 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1234 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1235 sony_nc_setup_rfkill(device, SONY_WIFI);
1237 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1238 sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1240 if ((0xf0 & dev_code) == 0x20 &&
1241 !sony_rfkill_devices[SONY_WWAN])
1242 sony_nc_setup_rfkill(device, SONY_WWAN);
1244 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1245 sony_nc_setup_rfkill(device, SONY_WIMAX);
1248 out_no_enum:
1249 kfree(buffer.pointer);
1250 return;
1253 static int sony_nc_add(struct acpi_device *device)
1255 acpi_status status;
1256 int result = 0;
1257 acpi_handle handle;
1258 struct sony_nc_value *item;
1260 pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
1261 SONY_LAPTOP_DRIVER_VERSION);
1263 sony_nc_acpi_device = device;
1264 strcpy(acpi_device_class(device), "sony/hotkey");
1266 sony_nc_acpi_handle = device->handle;
1268 /* read device status */
1269 result = acpi_bus_get_status(device);
1270 /* bail IFF the above call was successful and the device is not present */
1271 if (!result && !device->status.present) {
1272 dprintk("Device not present\n");
1273 result = -ENODEV;
1274 goto outwalk;
1277 if (debug) {
1278 status = acpi_walk_namespace(ACPI_TYPE_METHOD,
1279 sony_nc_acpi_handle, 1, sony_walk_callback,
1280 NULL, NULL, NULL);
1281 if (ACPI_FAILURE(status)) {
1282 pr_warn(DRV_PFX "unable to walk acpi resources\n");
1283 result = -ENODEV;
1284 goto outwalk;
1288 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1289 &handle))) {
1290 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1291 dprintk("ECON Method failed\n");
1294 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1295 &handle))) {
1296 dprintk("Doing SNC setup\n");
1297 sony_nc_function_setup(device);
1298 sony_nc_rfkill_setup(device);
1301 /* setup input devices and helper fifo */
1302 result = sony_laptop_setup_input(device);
1303 if (result) {
1304 pr_err(DRV_PFX "Unable to create input devices.\n");
1305 goto outwalk;
1308 if (acpi_video_backlight_support()) {
1309 pr_info(DRV_PFX "brightness ignored, must be "
1310 "controlled by ACPI video driver\n");
1311 } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1312 &handle))) {
1313 struct backlight_properties props;
1314 memset(&props, 0, sizeof(struct backlight_properties));
1315 props.type = BACKLIGHT_PLATFORM;
1316 props.max_brightness = SONY_MAX_BRIGHTNESS - 1;
1317 sony_backlight_device = backlight_device_register("sony", NULL,
1318 NULL,
1319 &sony_backlight_ops,
1320 &props);
1322 if (IS_ERR(sony_backlight_device)) {
1323 pr_warning(DRV_PFX "unable to register backlight device\n");
1324 sony_backlight_device = NULL;
1325 } else {
1326 sony_backlight_device->props.brightness =
1327 sony_backlight_get_brightness
1328 (sony_backlight_device);
1333 result = sony_pf_add();
1334 if (result)
1335 goto outbacklight;
1337 /* create sony_pf sysfs attributes related to the SNC device */
1338 for (item = sony_nc_values; item->name; ++item) {
1340 if (!debug && item->debug)
1341 continue;
1343 /* find the available acpiget as described in the DSDT */
1344 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1345 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1346 *item->acpiget,
1347 &handle))) {
1348 dprintk("Found %s getter: %s\n",
1349 item->name, *item->acpiget);
1350 item->devattr.attr.mode |= S_IRUGO;
1351 break;
1355 /* find the available acpiset as described in the DSDT */
1356 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1357 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1358 *item->acpiset,
1359 &handle))) {
1360 dprintk("Found %s setter: %s\n",
1361 item->name, *item->acpiset);
1362 item->devattr.attr.mode |= S_IWUSR;
1363 break;
1367 if (item->devattr.attr.mode != 0) {
1368 result =
1369 device_create_file(&sony_pf_device->dev,
1370 &item->devattr);
1371 if (result)
1372 goto out_sysfs;
1376 return 0;
1378 out_sysfs:
1379 for (item = sony_nc_values; item->name; ++item) {
1380 device_remove_file(&sony_pf_device->dev, &item->devattr);
1382 sony_pf_remove();
1384 outbacklight:
1385 if (sony_backlight_device)
1386 backlight_device_unregister(sony_backlight_device);
1388 sony_laptop_remove_input();
1390 outwalk:
1391 sony_nc_rfkill_cleanup();
1392 return result;
1395 static int sony_nc_remove(struct acpi_device *device, int type)
1397 struct sony_nc_value *item;
1399 if (sony_backlight_device)
1400 backlight_device_unregister(sony_backlight_device);
1402 sony_nc_acpi_device = NULL;
1404 for (item = sony_nc_values; item->name; ++item) {
1405 device_remove_file(&sony_pf_device->dev, &item->devattr);
1408 sony_pf_remove();
1409 sony_laptop_remove_input();
1410 sony_nc_rfkill_cleanup();
1411 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
1413 return 0;
1416 static const struct acpi_device_id sony_device_ids[] = {
1417 {SONY_NC_HID, 0},
1418 {SONY_PIC_HID, 0},
1419 {"", 0},
1421 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1423 static const struct acpi_device_id sony_nc_device_ids[] = {
1424 {SONY_NC_HID, 0},
1425 {"", 0},
1428 static struct acpi_driver sony_nc_driver = {
1429 .name = SONY_NC_DRIVER_NAME,
1430 .class = SONY_NC_CLASS,
1431 .ids = sony_nc_device_ids,
1432 .owner = THIS_MODULE,
1433 .ops = {
1434 .add = sony_nc_add,
1435 .remove = sony_nc_remove,
1436 .resume = sony_nc_resume,
1437 .notify = sony_nc_notify,
1441 /*********** SPIC (SNY6001) Device ***********/
1443 #define SONYPI_DEVICE_TYPE1 0x00000001
1444 #define SONYPI_DEVICE_TYPE2 0x00000002
1445 #define SONYPI_DEVICE_TYPE3 0x00000004
1446 #define SONYPI_DEVICE_TYPE4 0x00000008
1448 #define SONYPI_TYPE1_OFFSET 0x04
1449 #define SONYPI_TYPE2_OFFSET 0x12
1450 #define SONYPI_TYPE3_OFFSET 0x12
1452 struct sony_pic_ioport {
1453 struct acpi_resource_io io1;
1454 struct acpi_resource_io io2;
1455 struct list_head list;
1458 struct sony_pic_irq {
1459 struct acpi_resource_irq irq;
1460 struct list_head list;
1463 struct sonypi_eventtypes {
1464 u8 data;
1465 unsigned long mask;
1466 struct sonypi_event *events;
1469 struct sony_pic_dev {
1470 struct acpi_device *acpi_dev;
1471 struct sony_pic_irq *cur_irq;
1472 struct sony_pic_ioport *cur_ioport;
1473 struct list_head interrupts;
1474 struct list_head ioports;
1475 struct mutex lock;
1476 struct sonypi_eventtypes *event_types;
1477 int (*handle_irq)(const u8, const u8);
1478 int model;
1479 u16 evport_offset;
1480 u8 camera_power;
1481 u8 bluetooth_power;
1482 u8 wwan_power;
1485 static struct sony_pic_dev spic_dev = {
1486 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1487 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1490 static int spic_drv_registered;
1492 /* Event masks */
1493 #define SONYPI_JOGGER_MASK 0x00000001
1494 #define SONYPI_CAPTURE_MASK 0x00000002
1495 #define SONYPI_FNKEY_MASK 0x00000004
1496 #define SONYPI_BLUETOOTH_MASK 0x00000008
1497 #define SONYPI_PKEY_MASK 0x00000010
1498 #define SONYPI_BACK_MASK 0x00000020
1499 #define SONYPI_HELP_MASK 0x00000040
1500 #define SONYPI_LID_MASK 0x00000080
1501 #define SONYPI_ZOOM_MASK 0x00000100
1502 #define SONYPI_THUMBPHRASE_MASK 0x00000200
1503 #define SONYPI_MEYE_MASK 0x00000400
1504 #define SONYPI_MEMORYSTICK_MASK 0x00000800
1505 #define SONYPI_BATTERY_MASK 0x00001000
1506 #define SONYPI_WIRELESS_MASK 0x00002000
1508 struct sonypi_event {
1509 u8 data;
1510 u8 event;
1513 /* The set of possible button release events */
1514 static struct sonypi_event sonypi_releaseev[] = {
1515 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1516 { 0, 0 }
1519 /* The set of possible jogger events */
1520 static struct sonypi_event sonypi_joggerev[] = {
1521 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1522 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1523 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1524 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1525 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1526 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1527 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1528 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1529 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1530 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1531 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1532 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1533 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1534 { 0, 0 }
1537 /* The set of possible capture button events */
1538 static struct sonypi_event sonypi_captureev[] = {
1539 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1540 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1541 { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
1542 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1543 { 0, 0 }
1546 /* The set of possible fnkeys events */
1547 static struct sonypi_event sonypi_fnkeyev[] = {
1548 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1549 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1550 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1551 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1552 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1553 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1554 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1555 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1556 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1557 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1558 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1559 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1560 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1561 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1562 { 0x21, SONYPI_EVENT_FNKEY_1 },
1563 { 0x22, SONYPI_EVENT_FNKEY_2 },
1564 { 0x31, SONYPI_EVENT_FNKEY_D },
1565 { 0x32, SONYPI_EVENT_FNKEY_E },
1566 { 0x33, SONYPI_EVENT_FNKEY_F },
1567 { 0x34, SONYPI_EVENT_FNKEY_S },
1568 { 0x35, SONYPI_EVENT_FNKEY_B },
1569 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1570 { 0, 0 }
1573 /* The set of possible program key events */
1574 static struct sonypi_event sonypi_pkeyev[] = {
1575 { 0x01, SONYPI_EVENT_PKEY_P1 },
1576 { 0x02, SONYPI_EVENT_PKEY_P2 },
1577 { 0x04, SONYPI_EVENT_PKEY_P3 },
1578 { 0x20, SONYPI_EVENT_PKEY_P1 },
1579 { 0, 0 }
1582 /* The set of possible bluetooth events */
1583 static struct sonypi_event sonypi_blueev[] = {
1584 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1585 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1586 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1587 { 0, 0 }
1590 /* The set of possible wireless events */
1591 static struct sonypi_event sonypi_wlessev[] = {
1592 { 0x59, SONYPI_EVENT_IGNORE },
1593 { 0x5a, SONYPI_EVENT_IGNORE },
1594 { 0, 0 }
1597 /* The set of possible back button events */
1598 static struct sonypi_event sonypi_backev[] = {
1599 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1600 { 0, 0 }
1603 /* The set of possible help button events */
1604 static struct sonypi_event sonypi_helpev[] = {
1605 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1606 { 0, 0 }
1610 /* The set of possible lid events */
1611 static struct sonypi_event sonypi_lidev[] = {
1612 { 0x51, SONYPI_EVENT_LID_CLOSED },
1613 { 0x50, SONYPI_EVENT_LID_OPENED },
1614 { 0, 0 }
1617 /* The set of possible zoom events */
1618 static struct sonypi_event sonypi_zoomev[] = {
1619 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1620 { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1621 { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
1622 { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
1623 { 0, 0 }
1626 /* The set of possible thumbphrase events */
1627 static struct sonypi_event sonypi_thumbphraseev[] = {
1628 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1629 { 0, 0 }
1632 /* The set of possible motioneye camera events */
1633 static struct sonypi_event sonypi_meyeev[] = {
1634 { 0x00, SONYPI_EVENT_MEYE_FACE },
1635 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1636 { 0, 0 }
1639 /* The set of possible memorystick events */
1640 static struct sonypi_event sonypi_memorystickev[] = {
1641 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1642 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1643 { 0, 0 }
1646 /* The set of possible battery events */
1647 static struct sonypi_event sonypi_batteryev[] = {
1648 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1649 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1650 { 0, 0 }
1653 /* The set of possible volume events */
1654 static struct sonypi_event sonypi_volumeev[] = {
1655 { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1656 { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1657 { 0, 0 }
1660 /* The set of possible brightness events */
1661 static struct sonypi_event sonypi_brightnessev[] = {
1662 { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1663 { 0, 0 }
1666 static struct sonypi_eventtypes type1_events[] = {
1667 { 0, 0xffffffff, sonypi_releaseev },
1668 { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1669 { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1670 { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1671 { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1672 { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1673 { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1674 { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1675 { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1676 { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1677 { 0 },
1679 static struct sonypi_eventtypes type2_events[] = {
1680 { 0, 0xffffffff, sonypi_releaseev },
1681 { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1682 { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1683 { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1684 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1685 { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1686 { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1687 { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1688 { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1689 { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1690 { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1691 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1692 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1693 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1694 { 0 },
1696 static struct sonypi_eventtypes type3_events[] = {
1697 { 0, 0xffffffff, sonypi_releaseev },
1698 { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1699 { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1700 { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1701 { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1702 { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1703 { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1704 { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1705 { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
1706 { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
1707 { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
1708 { 0 },
1711 /* low level spic calls */
1712 #define ITERATIONS_LONG 10000
1713 #define ITERATIONS_SHORT 10
1714 #define wait_on_command(command, iterations) { \
1715 unsigned int n = iterations; \
1716 while (--n && (command)) \
1717 udelay(1); \
1718 if (!n) \
1719 dprintk("command failed at %s : %s (line %d)\n", \
1720 __FILE__, __func__, __LINE__); \
1723 static u8 sony_pic_call1(u8 dev)
1725 u8 v1, v2;
1727 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1728 ITERATIONS_LONG);
1729 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1730 v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1731 v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
1732 dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
1733 return v2;
1736 static u8 sony_pic_call2(u8 dev, u8 fn)
1738 u8 v1;
1740 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1741 ITERATIONS_LONG);
1742 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1743 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1744 ITERATIONS_LONG);
1745 outb(fn, spic_dev.cur_ioport->io1.minimum);
1746 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1747 dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
1748 return v1;
1751 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1753 u8 v1;
1755 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1756 outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1757 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1758 outb(fn, spic_dev.cur_ioport->io1.minimum);
1759 wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1760 outb(v, spic_dev.cur_ioport->io1.minimum);
1761 v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1762 dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1763 dev, fn, v, v1);
1764 return v1;
1768 * minidrivers for SPIC models
1770 static int type3_handle_irq(const u8 data_mask, const u8 ev)
1773 * 0x31 could mean we have to take some extra action and wait for
1774 * the next irq for some Type3 models, it will generate a new
1775 * irq and we can read new data from the device:
1776 * - 0x5c and 0x5f requires 0xA0
1777 * - 0x61 requires 0xB3
1779 if (data_mask == 0x31) {
1780 if (ev == 0x5c || ev == 0x5f)
1781 sony_pic_call1(0xA0);
1782 else if (ev == 0x61)
1783 sony_pic_call1(0xB3);
1784 return 0;
1786 return 1;
1789 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1791 struct pci_dev *pcidev;
1793 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1794 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1795 if (pcidev) {
1796 dev->model = SONYPI_DEVICE_TYPE1;
1797 dev->evport_offset = SONYPI_TYPE1_OFFSET;
1798 dev->event_types = type1_events;
1799 goto out;
1802 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1803 PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1804 if (pcidev) {
1805 dev->model = SONYPI_DEVICE_TYPE2;
1806 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1807 dev->event_types = type2_events;
1808 goto out;
1811 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1812 PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1813 if (pcidev) {
1814 dev->model = SONYPI_DEVICE_TYPE3;
1815 dev->handle_irq = type3_handle_irq;
1816 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1817 dev->event_types = type3_events;
1818 goto out;
1821 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1822 PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1823 if (pcidev) {
1824 dev->model = SONYPI_DEVICE_TYPE3;
1825 dev->handle_irq = type3_handle_irq;
1826 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1827 dev->event_types = type3_events;
1828 goto out;
1831 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1832 PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
1833 if (pcidev) {
1834 dev->model = SONYPI_DEVICE_TYPE3;
1835 dev->handle_irq = type3_handle_irq;
1836 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1837 dev->event_types = type3_events;
1838 goto out;
1841 /* default */
1842 dev->model = SONYPI_DEVICE_TYPE2;
1843 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1844 dev->event_types = type2_events;
1846 out:
1847 if (pcidev)
1848 pci_dev_put(pcidev);
1850 pr_info(DRV_PFX "detected Type%d model\n",
1851 dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
1852 dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1855 /* camera tests and poweron/poweroff */
1856 #define SONYPI_CAMERA_PICTURE 5
1857 #define SONYPI_CAMERA_CONTROL 0x10
1859 #define SONYPI_CAMERA_BRIGHTNESS 0
1860 #define SONYPI_CAMERA_CONTRAST 1
1861 #define SONYPI_CAMERA_HUE 2
1862 #define SONYPI_CAMERA_COLOR 3
1863 #define SONYPI_CAMERA_SHARPNESS 4
1865 #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1866 #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1867 #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1868 #define SONYPI_CAMERA_MUTE_MASK 0x40
1870 /* the rest don't need a loop until not 0xff */
1871 #define SONYPI_CAMERA_AGC 6
1872 #define SONYPI_CAMERA_AGC_MASK 0x30
1873 #define SONYPI_CAMERA_SHUTTER_MASK 0x7
1875 #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1876 #define SONYPI_CAMERA_CONTROL 0x10
1878 #define SONYPI_CAMERA_STATUS 7
1879 #define SONYPI_CAMERA_STATUS_READY 0x2
1880 #define SONYPI_CAMERA_STATUS_POSITION 0x4
1882 #define SONYPI_DIRECTION_BACKWARDS 0x4
1884 #define SONYPI_CAMERA_REVISION 8
1885 #define SONYPI_CAMERA_ROMVERSION 9
1887 static int __sony_pic_camera_ready(void)
1889 u8 v;
1891 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1892 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1895 static int __sony_pic_camera_off(void)
1897 if (!camera) {
1898 pr_warn(DRV_PFX "camera control not enabled\n");
1899 return -ENODEV;
1902 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1903 SONYPI_CAMERA_MUTE_MASK),
1904 ITERATIONS_SHORT);
1906 if (spic_dev.camera_power) {
1907 sony_pic_call2(0x91, 0);
1908 spic_dev.camera_power = 0;
1910 return 0;
1913 static int __sony_pic_camera_on(void)
1915 int i, j, x;
1917 if (!camera) {
1918 pr_warn(DRV_PFX "camera control not enabled\n");
1919 return -ENODEV;
1922 if (spic_dev.camera_power)
1923 return 0;
1925 for (j = 5; j > 0; j--) {
1927 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1928 msleep(10);
1929 sony_pic_call1(0x93);
1931 for (i = 400; i > 0; i--) {
1932 if (__sony_pic_camera_ready())
1933 break;
1934 msleep(10);
1936 if (i)
1937 break;
1940 if (j == 0) {
1941 pr_warn(DRV_PFX "failed to power on camera\n");
1942 return -ENODEV;
1945 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1946 0x5a),
1947 ITERATIONS_SHORT);
1949 spic_dev.camera_power = 1;
1950 return 0;
1953 /* External camera command (exported to the motion eye v4l driver) */
1954 int sony_pic_camera_command(int command, u8 value)
1956 if (!camera)
1957 return -EIO;
1959 mutex_lock(&spic_dev.lock);
1961 switch (command) {
1962 case SONY_PIC_COMMAND_SETCAMERA:
1963 if (value)
1964 __sony_pic_camera_on();
1965 else
1966 __sony_pic_camera_off();
1967 break;
1968 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
1969 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1970 ITERATIONS_SHORT);
1971 break;
1972 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
1973 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1974 ITERATIONS_SHORT);
1975 break;
1976 case SONY_PIC_COMMAND_SETCAMERAHUE:
1977 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1978 ITERATIONS_SHORT);
1979 break;
1980 case SONY_PIC_COMMAND_SETCAMERACOLOR:
1981 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1982 ITERATIONS_SHORT);
1983 break;
1984 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
1985 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1986 ITERATIONS_SHORT);
1987 break;
1988 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
1989 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1990 ITERATIONS_SHORT);
1991 break;
1992 case SONY_PIC_COMMAND_SETCAMERAAGC:
1993 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1994 ITERATIONS_SHORT);
1995 break;
1996 default:
1997 pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
1998 command);
1999 break;
2001 mutex_unlock(&spic_dev.lock);
2002 return 0;
2004 EXPORT_SYMBOL(sony_pic_camera_command);
2006 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
2007 static void __sony_pic_set_wwanpower(u8 state)
2009 state = !!state;
2010 if (spic_dev.wwan_power == state)
2011 return;
2012 sony_pic_call2(0xB0, state);
2013 sony_pic_call1(0x82);
2014 spic_dev.wwan_power = state;
2017 static ssize_t sony_pic_wwanpower_store(struct device *dev,
2018 struct device_attribute *attr,
2019 const char *buffer, size_t count)
2021 unsigned long value;
2022 if (count > 31)
2023 return -EINVAL;
2025 value = simple_strtoul(buffer, NULL, 10);
2026 mutex_lock(&spic_dev.lock);
2027 __sony_pic_set_wwanpower(value);
2028 mutex_unlock(&spic_dev.lock);
2030 return count;
2033 static ssize_t sony_pic_wwanpower_show(struct device *dev,
2034 struct device_attribute *attr, char *buffer)
2036 ssize_t count;
2037 mutex_lock(&spic_dev.lock);
2038 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2039 mutex_unlock(&spic_dev.lock);
2040 return count;
2043 /* bluetooth subsystem power state */
2044 static void __sony_pic_set_bluetoothpower(u8 state)
2046 state = !!state;
2047 if (spic_dev.bluetooth_power == state)
2048 return;
2049 sony_pic_call2(0x96, state);
2050 sony_pic_call1(0x82);
2051 spic_dev.bluetooth_power = state;
2054 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2055 struct device_attribute *attr,
2056 const char *buffer, size_t count)
2058 unsigned long value;
2059 if (count > 31)
2060 return -EINVAL;
2062 value = simple_strtoul(buffer, NULL, 10);
2063 mutex_lock(&spic_dev.lock);
2064 __sony_pic_set_bluetoothpower(value);
2065 mutex_unlock(&spic_dev.lock);
2067 return count;
2070 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2071 struct device_attribute *attr, char *buffer)
2073 ssize_t count = 0;
2074 mutex_lock(&spic_dev.lock);
2075 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2076 mutex_unlock(&spic_dev.lock);
2077 return count;
2080 /* fan speed */
2081 /* FAN0 information (reverse engineered from ACPI tables) */
2082 #define SONY_PIC_FAN0_STATUS 0x93
2083 static int sony_pic_set_fanspeed(unsigned long value)
2085 return ec_write(SONY_PIC_FAN0_STATUS, value);
2088 static int sony_pic_get_fanspeed(u8 *value)
2090 return ec_read(SONY_PIC_FAN0_STATUS, value);
2093 static ssize_t sony_pic_fanspeed_store(struct device *dev,
2094 struct device_attribute *attr,
2095 const char *buffer, size_t count)
2097 unsigned long value;
2098 if (count > 31)
2099 return -EINVAL;
2101 value = simple_strtoul(buffer, NULL, 10);
2102 if (sony_pic_set_fanspeed(value))
2103 return -EIO;
2105 return count;
2108 static ssize_t sony_pic_fanspeed_show(struct device *dev,
2109 struct device_attribute *attr, char *buffer)
2111 u8 value = 0;
2112 if (sony_pic_get_fanspeed(&value))
2113 return -EIO;
2115 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2118 #define SPIC_ATTR(_name, _mode) \
2119 struct device_attribute spic_attr_##_name = __ATTR(_name, \
2120 _mode, sony_pic_## _name ##_show, \
2121 sony_pic_## _name ##_store)
2123 static SPIC_ATTR(bluetoothpower, 0644);
2124 static SPIC_ATTR(wwanpower, 0644);
2125 static SPIC_ATTR(fanspeed, 0644);
2127 static struct attribute *spic_attributes[] = {
2128 &spic_attr_bluetoothpower.attr,
2129 &spic_attr_wwanpower.attr,
2130 &spic_attr_fanspeed.attr,
2131 NULL
2134 static struct attribute_group spic_attribute_group = {
2135 .attrs = spic_attributes
2138 /******** SONYPI compatibility **********/
2139 #ifdef CONFIG_SONYPI_COMPAT
2141 /* battery / brightness / temperature addresses */
2142 #define SONYPI_BAT_FLAGS 0x81
2143 #define SONYPI_LCD_LIGHT 0x96
2144 #define SONYPI_BAT1_PCTRM 0xa0
2145 #define SONYPI_BAT1_LEFT 0xa2
2146 #define SONYPI_BAT1_MAXRT 0xa4
2147 #define SONYPI_BAT2_PCTRM 0xa8
2148 #define SONYPI_BAT2_LEFT 0xaa
2149 #define SONYPI_BAT2_MAXRT 0xac
2150 #define SONYPI_BAT1_MAXTK 0xb0
2151 #define SONYPI_BAT1_FULL 0xb2
2152 #define SONYPI_BAT2_MAXTK 0xb8
2153 #define SONYPI_BAT2_FULL 0xba
2154 #define SONYPI_TEMP_STATUS 0xC1
2156 struct sonypi_compat_s {
2157 struct fasync_struct *fifo_async;
2158 struct kfifo fifo;
2159 spinlock_t fifo_lock;
2160 wait_queue_head_t fifo_proc_list;
2161 atomic_t open_count;
2163 static struct sonypi_compat_s sonypi_compat = {
2164 .open_count = ATOMIC_INIT(0),
2167 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2169 return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
2172 static int sonypi_misc_release(struct inode *inode, struct file *file)
2174 atomic_dec(&sonypi_compat.open_count);
2175 return 0;
2178 static int sonypi_misc_open(struct inode *inode, struct file *file)
2180 /* Flush input queue on first open */
2181 unsigned long flags;
2183 spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
2185 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
2186 kfifo_reset(&sonypi_compat.fifo);
2188 spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
2190 return 0;
2193 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2194 size_t count, loff_t *pos)
2196 ssize_t ret;
2197 unsigned char c;
2199 if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
2200 (file->f_flags & O_NONBLOCK))
2201 return -EAGAIN;
2203 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
2204 kfifo_len(&sonypi_compat.fifo) != 0);
2205 if (ret)
2206 return ret;
2208 while (ret < count &&
2209 (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
2210 &sonypi_compat.fifo_lock) == sizeof(c))) {
2211 if (put_user(c, buf++))
2212 return -EFAULT;
2213 ret++;
2216 if (ret > 0) {
2217 struct inode *inode = file->f_path.dentry->d_inode;
2218 inode->i_atime = current_fs_time(inode->i_sb);
2221 return ret;
2224 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2226 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
2227 if (kfifo_len(&sonypi_compat.fifo))
2228 return POLLIN | POLLRDNORM;
2229 return 0;
2232 static int ec_read16(u8 addr, u16 *value)
2234 u8 val_lb, val_hb;
2235 if (ec_read(addr, &val_lb))
2236 return -1;
2237 if (ec_read(addr + 1, &val_hb))
2238 return -1;
2239 *value = val_lb | (val_hb << 8);
2240 return 0;
2243 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2244 unsigned long arg)
2246 int ret = 0;
2247 void __user *argp = (void __user *)arg;
2248 u8 val8;
2249 u16 val16;
2250 int value;
2252 mutex_lock(&spic_dev.lock);
2253 switch (cmd) {
2254 case SONYPI_IOCGBRT:
2255 if (sony_backlight_device == NULL) {
2256 ret = -EIO;
2257 break;
2259 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2260 ret = -EIO;
2261 break;
2263 val8 = ((value & 0xff) - 1) << 5;
2264 if (copy_to_user(argp, &val8, sizeof(val8)))
2265 ret = -EFAULT;
2266 break;
2267 case SONYPI_IOCSBRT:
2268 if (sony_backlight_device == NULL) {
2269 ret = -EIO;
2270 break;
2272 if (copy_from_user(&val8, argp, sizeof(val8))) {
2273 ret = -EFAULT;
2274 break;
2276 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2277 (val8 >> 5) + 1, NULL)) {
2278 ret = -EIO;
2279 break;
2281 /* sync the backlight device status */
2282 sony_backlight_device->props.brightness =
2283 sony_backlight_get_brightness(sony_backlight_device);
2284 break;
2285 case SONYPI_IOCGBAT1CAP:
2286 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2287 ret = -EIO;
2288 break;
2290 if (copy_to_user(argp, &val16, sizeof(val16)))
2291 ret = -EFAULT;
2292 break;
2293 case SONYPI_IOCGBAT1REM:
2294 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2295 ret = -EIO;
2296 break;
2298 if (copy_to_user(argp, &val16, sizeof(val16)))
2299 ret = -EFAULT;
2300 break;
2301 case SONYPI_IOCGBAT2CAP:
2302 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2303 ret = -EIO;
2304 break;
2306 if (copy_to_user(argp, &val16, sizeof(val16)))
2307 ret = -EFAULT;
2308 break;
2309 case SONYPI_IOCGBAT2REM:
2310 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2311 ret = -EIO;
2312 break;
2314 if (copy_to_user(argp, &val16, sizeof(val16)))
2315 ret = -EFAULT;
2316 break;
2317 case SONYPI_IOCGBATFLAGS:
2318 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2319 ret = -EIO;
2320 break;
2322 val8 &= 0x07;
2323 if (copy_to_user(argp, &val8, sizeof(val8)))
2324 ret = -EFAULT;
2325 break;
2326 case SONYPI_IOCGBLUE:
2327 val8 = spic_dev.bluetooth_power;
2328 if (copy_to_user(argp, &val8, sizeof(val8)))
2329 ret = -EFAULT;
2330 break;
2331 case SONYPI_IOCSBLUE:
2332 if (copy_from_user(&val8, argp, sizeof(val8))) {
2333 ret = -EFAULT;
2334 break;
2336 __sony_pic_set_bluetoothpower(val8);
2337 break;
2338 /* FAN Controls */
2339 case SONYPI_IOCGFAN:
2340 if (sony_pic_get_fanspeed(&val8)) {
2341 ret = -EIO;
2342 break;
2344 if (copy_to_user(argp, &val8, sizeof(val8)))
2345 ret = -EFAULT;
2346 break;
2347 case SONYPI_IOCSFAN:
2348 if (copy_from_user(&val8, argp, sizeof(val8))) {
2349 ret = -EFAULT;
2350 break;
2352 if (sony_pic_set_fanspeed(val8))
2353 ret = -EIO;
2354 break;
2355 /* GET Temperature (useful under APM) */
2356 case SONYPI_IOCGTEMP:
2357 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2358 ret = -EIO;
2359 break;
2361 if (copy_to_user(argp, &val8, sizeof(val8)))
2362 ret = -EFAULT;
2363 break;
2364 default:
2365 ret = -EINVAL;
2367 mutex_unlock(&spic_dev.lock);
2368 return ret;
2371 static const struct file_operations sonypi_misc_fops = {
2372 .owner = THIS_MODULE,
2373 .read = sonypi_misc_read,
2374 .poll = sonypi_misc_poll,
2375 .open = sonypi_misc_open,
2376 .release = sonypi_misc_release,
2377 .fasync = sonypi_misc_fasync,
2378 .unlocked_ioctl = sonypi_misc_ioctl,
2379 .llseek = noop_llseek,
2382 static struct miscdevice sonypi_misc_device = {
2383 .minor = MISC_DYNAMIC_MINOR,
2384 .name = "sonypi",
2385 .fops = &sonypi_misc_fops,
2388 static void sonypi_compat_report_event(u8 event)
2390 kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
2391 sizeof(event), &sonypi_compat.fifo_lock);
2392 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2393 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2396 static int sonypi_compat_init(void)
2398 int error;
2400 spin_lock_init(&sonypi_compat.fifo_lock);
2401 error =
2402 kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
2403 if (error) {
2404 pr_err(DRV_PFX "kfifo_alloc failed\n");
2405 return error;
2408 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
2410 if (minor != -1)
2411 sonypi_misc_device.minor = minor;
2412 error = misc_register(&sonypi_misc_device);
2413 if (error) {
2414 pr_err(DRV_PFX "misc_register failed\n");
2415 goto err_free_kfifo;
2417 if (minor == -1)
2418 pr_info(DRV_PFX "device allocated minor is %d\n",
2419 sonypi_misc_device.minor);
2421 return 0;
2423 err_free_kfifo:
2424 kfifo_free(&sonypi_compat.fifo);
2425 return error;
2428 static void sonypi_compat_exit(void)
2430 misc_deregister(&sonypi_misc_device);
2431 kfifo_free(&sonypi_compat.fifo);
2433 #else
2434 static int sonypi_compat_init(void) { return 0; }
2435 static void sonypi_compat_exit(void) { }
2436 static void sonypi_compat_report_event(u8 event) { }
2437 #endif /* CONFIG_SONYPI_COMPAT */
2440 * ACPI callbacks
2442 static acpi_status
2443 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2445 u32 i;
2446 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2448 switch (resource->type) {
2449 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
2451 /* start IO enumeration */
2452 struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2453 if (!ioport)
2454 return AE_ERROR;
2456 list_add(&ioport->list, &dev->ioports);
2457 return AE_OK;
2460 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
2461 /* end IO enumeration */
2462 return AE_OK;
2464 case ACPI_RESOURCE_TYPE_IRQ:
2466 struct acpi_resource_irq *p = &resource->data.irq;
2467 struct sony_pic_irq *interrupt = NULL;
2468 if (!p || !p->interrupt_count) {
2470 * IRQ descriptors may have no IRQ# bits set,
2471 * particularly those those w/ _STA disabled
2473 dprintk("Blank IRQ resource\n");
2474 return AE_OK;
2476 for (i = 0; i < p->interrupt_count; i++) {
2477 if (!p->interrupts[i]) {
2478 pr_warn(DRV_PFX "Invalid IRQ %d\n",
2479 p->interrupts[i]);
2480 continue;
2482 interrupt = kzalloc(sizeof(*interrupt),
2483 GFP_KERNEL);
2484 if (!interrupt)
2485 return AE_ERROR;
2487 list_add(&interrupt->list, &dev->interrupts);
2488 interrupt->irq.triggering = p->triggering;
2489 interrupt->irq.polarity = p->polarity;
2490 interrupt->irq.sharable = p->sharable;
2491 interrupt->irq.interrupt_count = 1;
2492 interrupt->irq.interrupts[0] = p->interrupts[i];
2494 return AE_OK;
2496 case ACPI_RESOURCE_TYPE_IO:
2498 struct acpi_resource_io *io = &resource->data.io;
2499 struct sony_pic_ioport *ioport =
2500 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
2501 if (!io) {
2502 dprintk("Blank IO resource\n");
2503 return AE_OK;
2506 if (!ioport->io1.minimum) {
2507 memcpy(&ioport->io1, io, sizeof(*io));
2508 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2509 ioport->io1.address_length);
2511 else if (!ioport->io2.minimum) {
2512 memcpy(&ioport->io2, io, sizeof(*io));
2513 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2514 ioport->io2.address_length);
2516 else {
2517 pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
2518 return AE_ERROR;
2520 return AE_OK;
2522 default:
2523 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2524 resource->type);
2526 case ACPI_RESOURCE_TYPE_END_TAG:
2527 return AE_OK;
2529 return AE_CTRL_TERMINATE;
2532 static int sony_pic_possible_resources(struct acpi_device *device)
2534 int result = 0;
2535 acpi_status status = AE_OK;
2537 if (!device)
2538 return -EINVAL;
2540 /* get device status */
2541 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2542 dprintk("Evaluating _STA\n");
2543 result = acpi_bus_get_status(device);
2544 if (result) {
2545 pr_warn(DRV_PFX "Unable to read status\n");
2546 goto end;
2549 if (!device->status.enabled)
2550 dprintk("Device disabled\n");
2551 else
2552 dprintk("Device enabled\n");
2555 * Query and parse 'method'
2557 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2558 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2559 sony_pic_read_possible_resource, &spic_dev);
2560 if (ACPI_FAILURE(status)) {
2561 pr_warn(DRV_PFX "Failure evaluating %s\n",
2562 METHOD_NAME__PRS);
2563 result = -ENODEV;
2565 end:
2566 return result;
2570 * Disable the spic device by calling its _DIS method
2572 static int sony_pic_disable(struct acpi_device *device)
2574 acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2575 NULL);
2577 if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
2578 return -ENXIO;
2580 dprintk("Device disabled\n");
2581 return 0;
2586 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2588 * Call _SRS to set current resources
2590 static int sony_pic_enable(struct acpi_device *device,
2591 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2593 acpi_status status;
2594 int result = 0;
2595 /* Type 1 resource layout is:
2596 * IO
2597 * IO
2598 * IRQNoFlags
2599 * End
2601 * Type 2 and 3 resource layout is:
2602 * IO
2603 * IRQNoFlags
2604 * End
2606 struct {
2607 struct acpi_resource res1;
2608 struct acpi_resource res2;
2609 struct acpi_resource res3;
2610 struct acpi_resource res4;
2611 } *resource;
2612 struct acpi_buffer buffer = { 0, NULL };
2614 if (!ioport || !irq)
2615 return -EINVAL;
2617 /* init acpi_buffer */
2618 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2619 if (!resource)
2620 return -ENOMEM;
2622 buffer.length = sizeof(*resource) + 1;
2623 buffer.pointer = resource;
2625 /* setup Type 1 resources */
2626 if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
2628 /* setup io resources */
2629 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2630 resource->res1.length = sizeof(struct acpi_resource);
2631 memcpy(&resource->res1.data.io, &ioport->io1,
2632 sizeof(struct acpi_resource_io));
2634 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2635 resource->res2.length = sizeof(struct acpi_resource);
2636 memcpy(&resource->res2.data.io, &ioport->io2,
2637 sizeof(struct acpi_resource_io));
2639 /* setup irq resource */
2640 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2641 resource->res3.length = sizeof(struct acpi_resource);
2642 memcpy(&resource->res3.data.irq, &irq->irq,
2643 sizeof(struct acpi_resource_irq));
2644 /* we requested a shared irq */
2645 resource->res3.data.irq.sharable = ACPI_SHARED;
2647 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2650 /* setup Type 2/3 resources */
2651 else {
2652 /* setup io resource */
2653 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2654 resource->res1.length = sizeof(struct acpi_resource);
2655 memcpy(&resource->res1.data.io, &ioport->io1,
2656 sizeof(struct acpi_resource_io));
2658 /* setup irq resource */
2659 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2660 resource->res2.length = sizeof(struct acpi_resource);
2661 memcpy(&resource->res2.data.irq, &irq->irq,
2662 sizeof(struct acpi_resource_irq));
2663 /* we requested a shared irq */
2664 resource->res2.data.irq.sharable = ACPI_SHARED;
2666 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2669 /* Attempt to set the resource */
2670 dprintk("Evaluating _SRS\n");
2671 status = acpi_set_current_resources(device->handle, &buffer);
2673 /* check for total failure */
2674 if (ACPI_FAILURE(status)) {
2675 pr_err(DRV_PFX "Error evaluating _SRS\n");
2676 result = -ENODEV;
2677 goto end;
2680 /* Necessary device initializations calls (from sonypi) */
2681 sony_pic_call1(0x82);
2682 sony_pic_call2(0x81, 0xff);
2683 sony_pic_call1(compat ? 0x92 : 0x82);
2685 end:
2686 kfree(resource);
2687 return result;
2690 /*****************
2692 * ISR: some event is available
2694 *****************/
2695 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2697 int i, j;
2698 u8 ev = 0;
2699 u8 data_mask = 0;
2700 u8 device_event = 0;
2702 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2704 ev = inb_p(dev->cur_ioport->io1.minimum);
2705 if (dev->cur_ioport->io2.minimum)
2706 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2707 else
2708 data_mask = inb_p(dev->cur_ioport->io1.minimum +
2709 dev->evport_offset);
2711 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2712 ev, data_mask, dev->cur_ioport->io1.minimum,
2713 dev->evport_offset);
2715 if (ev == 0x00 || ev == 0xff)
2716 return IRQ_HANDLED;
2718 for (i = 0; dev->event_types[i].mask; i++) {
2720 if ((data_mask & dev->event_types[i].data) !=
2721 dev->event_types[i].data)
2722 continue;
2724 if (!(mask & dev->event_types[i].mask))
2725 continue;
2727 for (j = 0; dev->event_types[i].events[j].event; j++) {
2728 if (ev == dev->event_types[i].events[j].data) {
2729 device_event =
2730 dev->event_types[i].events[j].event;
2731 /* some events may require ignoring */
2732 if (!device_event)
2733 return IRQ_HANDLED;
2734 goto found;
2738 /* Still not able to decode the event try to pass
2739 * it over to the minidriver
2741 if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
2742 return IRQ_HANDLED;
2744 dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2745 ev, data_mask, dev->cur_ioport->io1.minimum,
2746 dev->evport_offset);
2747 return IRQ_HANDLED;
2749 found:
2750 sony_laptop_report_input_event(device_event);
2751 acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
2752 sonypi_compat_report_event(device_event);
2753 return IRQ_HANDLED;
2756 /*****************
2758 * ACPI driver
2760 *****************/
2761 static int sony_pic_remove(struct acpi_device *device, int type)
2763 struct sony_pic_ioport *io, *tmp_io;
2764 struct sony_pic_irq *irq, *tmp_irq;
2766 if (sony_pic_disable(device)) {
2767 pr_err(DRV_PFX "Couldn't disable device.\n");
2768 return -ENXIO;
2771 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2772 release_region(spic_dev.cur_ioport->io1.minimum,
2773 spic_dev.cur_ioport->io1.address_length);
2774 if (spic_dev.cur_ioport->io2.minimum)
2775 release_region(spic_dev.cur_ioport->io2.minimum,
2776 spic_dev.cur_ioport->io2.address_length);
2778 sonypi_compat_exit();
2780 sony_laptop_remove_input();
2782 /* pf attrs */
2783 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2784 sony_pf_remove();
2786 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2787 list_del(&io->list);
2788 kfree(io);
2790 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2791 list_del(&irq->list);
2792 kfree(irq);
2794 spic_dev.cur_ioport = NULL;
2795 spic_dev.cur_irq = NULL;
2797 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2798 return 0;
2801 static int sony_pic_add(struct acpi_device *device)
2803 int result;
2804 struct sony_pic_ioport *io, *tmp_io;
2805 struct sony_pic_irq *irq, *tmp_irq;
2807 pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
2808 SONY_LAPTOP_DRIVER_VERSION);
2810 spic_dev.acpi_dev = device;
2811 strcpy(acpi_device_class(device), "sony/hotkey");
2812 sony_pic_detect_device_type(&spic_dev);
2813 mutex_init(&spic_dev.lock);
2815 /* read _PRS resources */
2816 result = sony_pic_possible_resources(device);
2817 if (result) {
2818 pr_err(DRV_PFX "Unable to read possible resources.\n");
2819 goto err_free_resources;
2822 /* setup input devices and helper fifo */
2823 result = sony_laptop_setup_input(device);
2824 if (result) {
2825 pr_err(DRV_PFX "Unable to create input devices.\n");
2826 goto err_free_resources;
2829 if (sonypi_compat_init())
2830 goto err_remove_input;
2832 /* request io port */
2833 list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2834 if (request_region(io->io1.minimum, io->io1.address_length,
2835 "Sony Programable I/O Device")) {
2836 dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2837 io->io1.minimum, io->io1.maximum,
2838 io->io1.address_length);
2839 /* Type 1 have 2 ioports */
2840 if (io->io2.minimum) {
2841 if (request_region(io->io2.minimum,
2842 io->io2.address_length,
2843 "Sony Programable I/O Device")) {
2844 dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2845 io->io2.minimum, io->io2.maximum,
2846 io->io2.address_length);
2847 spic_dev.cur_ioport = io;
2848 break;
2850 else {
2851 dprintk("Unable to get I/O port2: "
2852 "0x%.4x (0x%.4x) + 0x%.2x\n",
2853 io->io2.minimum, io->io2.maximum,
2854 io->io2.address_length);
2855 release_region(io->io1.minimum,
2856 io->io1.address_length);
2859 else {
2860 spic_dev.cur_ioport = io;
2861 break;
2865 if (!spic_dev.cur_ioport) {
2866 pr_err(DRV_PFX "Failed to request_region.\n");
2867 result = -ENODEV;
2868 goto err_remove_compat;
2871 /* request IRQ */
2872 list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
2873 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2874 IRQF_DISABLED, "sony-laptop", &spic_dev)) {
2875 dprintk("IRQ: %d - triggering: %d - "
2876 "polarity: %d - shr: %d\n",
2877 irq->irq.interrupts[0],
2878 irq->irq.triggering,
2879 irq->irq.polarity,
2880 irq->irq.sharable);
2881 spic_dev.cur_irq = irq;
2882 break;
2885 if (!spic_dev.cur_irq) {
2886 pr_err(DRV_PFX "Failed to request_irq.\n");
2887 result = -ENODEV;
2888 goto err_release_region;
2891 /* set resource status _SRS */
2892 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2893 if (result) {
2894 pr_err(DRV_PFX "Couldn't enable device.\n");
2895 goto err_free_irq;
2898 spic_dev.bluetooth_power = -1;
2899 /* create device attributes */
2900 result = sony_pf_add();
2901 if (result)
2902 goto err_disable_device;
2904 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2905 if (result)
2906 goto err_remove_pf;
2908 return 0;
2910 err_remove_pf:
2911 sony_pf_remove();
2913 err_disable_device:
2914 sony_pic_disable(device);
2916 err_free_irq:
2917 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2919 err_release_region:
2920 release_region(spic_dev.cur_ioport->io1.minimum,
2921 spic_dev.cur_ioport->io1.address_length);
2922 if (spic_dev.cur_ioport->io2.minimum)
2923 release_region(spic_dev.cur_ioport->io2.minimum,
2924 spic_dev.cur_ioport->io2.address_length);
2926 err_remove_compat:
2927 sonypi_compat_exit();
2929 err_remove_input:
2930 sony_laptop_remove_input();
2932 err_free_resources:
2933 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2934 list_del(&io->list);
2935 kfree(io);
2937 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2938 list_del(&irq->list);
2939 kfree(irq);
2941 spic_dev.cur_ioport = NULL;
2942 spic_dev.cur_irq = NULL;
2944 return result;
2947 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2949 if (sony_pic_disable(device))
2950 return -ENXIO;
2951 return 0;
2954 static int sony_pic_resume(struct acpi_device *device)
2956 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2957 return 0;
2960 static const struct acpi_device_id sony_pic_device_ids[] = {
2961 {SONY_PIC_HID, 0},
2962 {"", 0},
2965 static struct acpi_driver sony_pic_driver = {
2966 .name = SONY_PIC_DRIVER_NAME,
2967 .class = SONY_PIC_CLASS,
2968 .ids = sony_pic_device_ids,
2969 .owner = THIS_MODULE,
2970 .ops = {
2971 .add = sony_pic_add,
2972 .remove = sony_pic_remove,
2973 .suspend = sony_pic_suspend,
2974 .resume = sony_pic_resume,
2978 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2980 .ident = "Sony Vaio",
2981 .matches = {
2982 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2983 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2987 .ident = "Sony Vaio",
2988 .matches = {
2989 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2990 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2996 static int __init sony_laptop_init(void)
2998 int result;
3000 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
3001 result = acpi_bus_register_driver(&sony_pic_driver);
3002 if (result) {
3003 pr_err(DRV_PFX "Unable to register SPIC driver.");
3004 goto out;
3006 spic_drv_registered = 1;
3009 result = acpi_bus_register_driver(&sony_nc_driver);
3010 if (result) {
3011 pr_err(DRV_PFX "Unable to register SNC driver.");
3012 goto out_unregister_pic;
3015 return 0;
3017 out_unregister_pic:
3018 if (spic_drv_registered)
3019 acpi_bus_unregister_driver(&sony_pic_driver);
3020 out:
3021 return result;
3024 static void __exit sony_laptop_exit(void)
3026 acpi_bus_unregister_driver(&sony_nc_driver);
3027 if (spic_drv_registered)
3028 acpi_bus_unregister_driver(&sony_pic_driver);
3031 module_init(sony_laptop_init);
3032 module_exit(sony_laptop_exit);