blob: 14ee06c8f12727ebd3d0a950b7e870d440616305 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002 * ACPI Sony Notebook Control Driver (SNC and SPIC)
Stelian Pop7f09c432007-01-13 23:04:31 +01003 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01005 * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
malattia@linux.it33a04452007-04-09 19:26:03 +020010 * The SNY6001 driver part is based on the sonypi driver which includes
11 * material from:
12 *
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
14 *
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
16 *
17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
18 *
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
20 *
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
22 *
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
24 *
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
26 *
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
28 *
Stelian Pop7f09c432007-01-13 23:04:31 +010029 * 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.
33 *
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.
38 *
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.
42 *
43 */
44
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>
Alessandro Guido50f62af2007-01-13 23:04:34 +010050#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010052#include <linux/err.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020053#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>
Stelian Pop7f09c432007-01-13 23:04:31 +010061#include <acpi/acpi_drivers.h>
62#include <acpi/acpi_bus.h>
63#include <asm/uaccess.h>
malattia@linux.it33a04452007-04-09 19:26:03 +020064#include <linux/sonypi.h>
malattia@linux.it1ce82c12007-04-28 23:34:36 +090065#include <linux/sony-laptop.h>
Mattia Dongilia64e62a2007-05-01 11:19:53 +090066#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +020067#include <linux/poll.h>
68#include <linux/miscdevice.h>
69#endif
Stelian Pop7f09c432007-01-13 23:04:31 +010070
malattia@linux.it33a04452007-04-09 19:26:03 +020071#define DRV_PFX "sony-laptop: "
malattia@linux.itb9a218b2007-04-09 10:19:06 +020072#define dprintk(msg...) do { \
malattia@linux.itf6119b02007-04-09 19:31:06 +020073 if (debug) printk(KERN_WARNING DRV_PFX msg); \
malattia@linux.itb9a218b2007-04-09 10:19:06 +020074} while (0)
75
malattia@linux.it33a04452007-04-09 19:26:03 +020076#define SONY_LAPTOP_DRIVER_VERSION "0.5"
Alessandro Guido50f62af2007-01-13 23:04:34 +010077
malattia@linux.it33a04452007-04-09 19:26:03 +020078#define SONY_NC_CLASS "sony-nc"
79#define SONY_NC_HID "SNY5001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020080#define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
malattia@linux.it33a04452007-04-09 19:26:03 +020081
82#define SONY_PIC_CLASS "sony-pic"
83#define SONY_PIC_HID "SNY6001"
malattia@linux.itf6119b02007-04-09 19:31:06 +020084#define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
Stelian Pop7f09c432007-01-13 23:04:31 +010085
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010086MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it33a04452007-04-09 19:26:03 +020087MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
Stelian Pop7f09c432007-01-13 23:04:31 +010088MODULE_LICENSE("GPL");
malattia@linux.it33a04452007-04-09 19:26:03 +020089MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
Stelian Pop7f09c432007-01-13 23:04:31 +010090
91static int debug;
92module_param(debug, int, 0);
93MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050094 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010095
malattia@linux.it33a04452007-04-09 19:26:03 +020096static int no_spic; /* = 0 */
97module_param(no_spic, int, 0444);
98MODULE_PARM_DESC(no_spic,
99 "set this if you don't want to enable the SPIC device");
100
101static int compat; /* = 0 */
102module_param(compat, int, 0444);
103MODULE_PARM_DESC(compat,
malattia@linux.it7b153f32007-04-09 19:31:25 +0200104 "set this if you want to enable backward compatibility mode");
malattia@linux.it33a04452007-04-09 19:26:03 +0200105
106static unsigned long mask = 0xffffffff;
107module_param(mask, ulong, 0644);
108MODULE_PARM_DESC(mask,
109 "set this to the mask of event you want to enable (see doc)");
110
malattia@linux.it5f3d2892007-04-28 23:18:45 +0900111static int camera; /* = 0 */
112module_param(camera, int, 0444);
113MODULE_PARM_DESC(camera,
114 "set this to 1 to enable Motion Eye camera controls "
115 "(only use it if you have a C1VE or C1VN model)");
116
Mattia Dongilia64e62a2007-05-01 11:19:53 +0900117#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +0200118static int minor = -1;
119module_param(minor, int, 0);
120MODULE_PARM_DESC(minor,
121 "minor number of the misc device for the SPIC compatibility code, "
122 "default is -1 (automatic)");
123#endif
124
malattia@linux.it1549ee62007-04-09 10:19:08 +0200125/*********** Input Devices ***********/
126
127#define SONY_LAPTOP_BUF_SIZE 128
128struct sony_laptop_input_s {
129 atomic_t users;
130 struct input_dev *jog_dev;
131 struct input_dev *key_dev;
132 struct kfifo *fifo;
133 spinlock_t fifo_lock;
134 struct workqueue_struct *wq;
135};
136static struct sony_laptop_input_s sony_laptop_input = {
137 .users = ATOMIC_INIT(0),
138};
139
140struct sony_laptop_keypress {
141 struct input_dev *dev;
142 int key;
143};
144
Mattia Dongilibc57f862007-07-20 02:01:57 +0900145/* Correspondance table between sonypi events
146 * and input layer indexes in the keymap
147 */
148static int sony_laptop_input_index[] = {
149 -1, /* no event */
150 -1, /* SONYPI_EVENT_JOGDIAL_DOWN */
151 -1, /* SONYPI_EVENT_JOGDIAL_UP */
152 -1, /* SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
153 -1, /* SONYPI_EVENT_JOGDIAL_UP_PRESSED */
154 -1, /* SONYPI_EVENT_JOGDIAL_PRESSED */
155 -1, /* SONYPI_EVENT_JOGDIAL_RELEASED */
156 0, /* SONYPI_EVENT_CAPTURE_PRESSED */
157 1, /* SONYPI_EVENT_CAPTURE_RELEASED */
158 2, /* SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
159 3, /* SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
160 4, /* SONYPI_EVENT_FNKEY_ESC */
161 5, /* SONYPI_EVENT_FNKEY_F1 */
162 6, /* SONYPI_EVENT_FNKEY_F2 */
163 7, /* SONYPI_EVENT_FNKEY_F3 */
164 8, /* SONYPI_EVENT_FNKEY_F4 */
165 9, /* SONYPI_EVENT_FNKEY_F5 */
166 10, /* SONYPI_EVENT_FNKEY_F6 */
167 11, /* SONYPI_EVENT_FNKEY_F7 */
168 12, /* SONYPI_EVENT_FNKEY_F8 */
169 13, /* SONYPI_EVENT_FNKEY_F9 */
170 14, /* SONYPI_EVENT_FNKEY_F10 */
171 15, /* SONYPI_EVENT_FNKEY_F11 */
172 16, /* SONYPI_EVENT_FNKEY_F12 */
173 17, /* SONYPI_EVENT_FNKEY_1 */
174 18, /* SONYPI_EVENT_FNKEY_2 */
175 19, /* SONYPI_EVENT_FNKEY_D */
176 20, /* SONYPI_EVENT_FNKEY_E */
177 21, /* SONYPI_EVENT_FNKEY_F */
178 22, /* SONYPI_EVENT_FNKEY_S */
179 23, /* SONYPI_EVENT_FNKEY_B */
180 24, /* SONYPI_EVENT_BLUETOOTH_PRESSED */
181 25, /* SONYPI_EVENT_PKEY_P1 */
182 26, /* SONYPI_EVENT_PKEY_P2 */
183 27, /* SONYPI_EVENT_PKEY_P3 */
184 28, /* SONYPI_EVENT_BACK_PRESSED */
185 -1, /* SONYPI_EVENT_LID_CLOSED */
186 -1, /* SONYPI_EVENT_LID_OPENED */
187 29, /* SONYPI_EVENT_BLUETOOTH_ON */
188 30, /* SONYPI_EVENT_BLUETOOTH_OFF */
189 31, /* SONYPI_EVENT_HELP_PRESSED */
190 32, /* SONYPI_EVENT_FNKEY_ONLY */
191 33, /* SONYPI_EVENT_JOGDIAL_FAST_DOWN */
192 34, /* SONYPI_EVENT_JOGDIAL_FAST_UP */
193 35, /* SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
194 36, /* SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
195 37, /* SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
196 38, /* SONYPI_EVENT_JOGDIAL_VFAST_UP */
197 39, /* SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
198 40, /* SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
199 41, /* SONYPI_EVENT_ZOOM_PRESSED */
200 42, /* SONYPI_EVENT_THUMBPHRASE_PRESSED */
201 43, /* SONYPI_EVENT_MEYE_FACE */
202 44, /* SONYPI_EVENT_MEYE_OPPOSITE */
203 45, /* SONYPI_EVENT_MEMORYSTICK_INSERT */
204 46, /* SONYPI_EVENT_MEMORYSTICK_EJECT */
205 -1, /* SONYPI_EVENT_ANYBUTTON_RELEASED */
206 -1, /* SONYPI_EVENT_BATTERY_INSERT */
207 -1, /* SONYPI_EVENT_BATTERY_REMOVE */
208 -1, /* SONYPI_EVENT_FNKEY_RELEASED */
209 47, /* SONYPI_EVENT_WIRELESS_ON */
210 48, /* SONYPI_EVENT_WIRELESS_OFF */
211};
212
213static int sony_laptop_input_keycode_map[] = {
214 KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
215 KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
216 KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
217 KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
218 KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
219 KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
220 KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
221 KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
222 KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
223 KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
224 KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
225 KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
226 KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
227 KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
228 KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
229 KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
230 KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
231 KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
232 KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
233 KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
234 KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
235 KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
236 KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
237 KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
238 KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
239 KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
240 KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
241 KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
242 KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
243 KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
244 KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
245 KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
246 KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
247 KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
248 KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
249 KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
250 KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
251 KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
252 KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
253 KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
254 KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
255 KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
256 BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
257 KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
258 KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
259 KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
260 KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
261 KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
262 KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
malattia@linux.it1549ee62007-04-09 10:19:08 +0200263};
264
265/* release buttons after a short delay if pressed */
266static void do_sony_laptop_release_key(struct work_struct *work)
267{
268 struct sony_laptop_keypress kp;
269
270 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
271 sizeof(kp)) == sizeof(kp)) {
272 msleep(10);
273 input_report_key(kp.dev, kp.key, 0);
274 input_sync(kp.dev);
275 }
276}
277static DECLARE_WORK(sony_laptop_release_key_work,
278 do_sony_laptop_release_key);
279
280/* forward event to the input subsytem */
281static void sony_laptop_report_input_event(u8 event)
282{
283 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
284 struct input_dev *key_dev = sony_laptop_input.key_dev;
285 struct sony_laptop_keypress kp = { NULL };
malattia@linux.it1549ee62007-04-09 10:19:08 +0200286
287 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
288 /* Nothing, not all VAIOs generate this event */
289 return;
290 }
291
292 /* report events */
293 switch (event) {
294 /* jog_dev events */
295 case SONYPI_EVENT_JOGDIAL_UP:
296 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
297 input_report_rel(jog_dev, REL_WHEEL, 1);
298 input_sync(jog_dev);
299 return;
300
301 case SONYPI_EVENT_JOGDIAL_DOWN:
302 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
303 input_report_rel(jog_dev, REL_WHEEL, -1);
304 input_sync(jog_dev);
305 return;
306
307 /* key_dev events */
308 case SONYPI_EVENT_JOGDIAL_PRESSED:
309 kp.key = BTN_MIDDLE;
310 kp.dev = jog_dev;
311 break;
312
313 default:
Mattia Dongilibc57f862007-07-20 02:01:57 +0900314 if (event > ARRAY_SIZE (sony_laptop_input_keycode_map)) {
315 dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
316 break;
317 }
318 if (sony_laptop_input_index[event] != -1) {
319 kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
320 if (kp.key != KEY_UNKNOWN)
malattia@linux.it1549ee62007-04-09 10:19:08 +0200321 kp.dev = key_dev;
Mattia Dongilibc57f862007-07-20 02:01:57 +0900322 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200323 break;
324 }
325
326 if (kp.dev) {
327 input_report_key(kp.dev, kp.key, 1);
Mattia Dongilibc57f862007-07-20 02:01:57 +0900328 /* we emit the scancode so we can always remap the key */
329 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
malattia@linux.it1549ee62007-04-09 10:19:08 +0200330 input_sync(kp.dev);
331 kfifo_put(sony_laptop_input.fifo,
332 (unsigned char *)&kp, sizeof(kp));
333
334 if (!work_pending(&sony_laptop_release_key_work))
335 queue_work(sony_laptop_input.wq,
336 &sony_laptop_release_key_work);
337 } else
338 dprintk("unknown input event %.2x\n", event);
339}
340
341static int sony_laptop_setup_input(void)
342{
343 struct input_dev *jog_dev;
344 struct input_dev *key_dev;
345 int i;
346 int error;
347
348 /* don't run again if already initialized */
349 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
350 return 0;
351
352 /* kfifo */
353 spin_lock_init(&sony_laptop_input.fifo_lock);
354 sony_laptop_input.fifo =
355 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
356 &sony_laptop_input.fifo_lock);
357 if (IS_ERR(sony_laptop_input.fifo)) {
358 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
359 error = PTR_ERR(sony_laptop_input.fifo);
360 goto err_dec_users;
361 }
362
363 /* init workqueue */
364 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
365 if (!sony_laptop_input.wq) {
366 printk(KERN_ERR DRV_PFX
367 "Unabe to create workqueue.\n");
368 error = -ENXIO;
369 goto err_free_kfifo;
370 }
371
372 /* input keys */
373 key_dev = input_allocate_device();
374 if (!key_dev) {
375 error = -ENOMEM;
376 goto err_destroy_wq;
377 }
378
379 key_dev->name = "Sony Vaio Keys";
380 key_dev->id.bustype = BUS_ISA;
381 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
382
383 /* Initialize the Input Drivers: special keys */
Mattia Dongilibc57f862007-07-20 02:01:57 +0900384 set_bit(EV_KEY, key_dev->evbit);
385 set_bit(EV_MSC, key_dev->evbit);
386 set_bit(MSC_SCAN, key_dev->mscbit);
387 key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
388 key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
389 key_dev->keycode = &sony_laptop_input_keycode_map;
390 for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++) {
391 if (sony_laptop_input_keycode_map[i] != KEY_RESERVED) {
392 set_bit(sony_laptop_input_keycode_map[i],
393 key_dev->keybit);
394 }
395 }
malattia@linux.it1549ee62007-04-09 10:19:08 +0200396
397 error = input_register_device(key_dev);
398 if (error)
399 goto err_free_keydev;
400
401 sony_laptop_input.key_dev = key_dev;
402
403 /* jogdial */
404 jog_dev = input_allocate_device();
405 if (!jog_dev) {
406 error = -ENOMEM;
407 goto err_unregister_keydev;
408 }
409
410 jog_dev->name = "Sony Vaio Jogdial";
411 jog_dev->id.bustype = BUS_ISA;
412 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
413
414 jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
415 jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
416 jog_dev->relbit[0] = BIT(REL_WHEEL);
417
418 error = input_register_device(jog_dev);
419 if (error)
420 goto err_free_jogdev;
421
422 sony_laptop_input.jog_dev = jog_dev;
423
424 return 0;
425
426err_free_jogdev:
427 input_free_device(jog_dev);
428
429err_unregister_keydev:
430 input_unregister_device(key_dev);
431 /* to avoid kref underflow below at input_free_device */
432 key_dev = NULL;
433
434err_free_keydev:
435 input_free_device(key_dev);
436
437err_destroy_wq:
438 destroy_workqueue(sony_laptop_input.wq);
439
440err_free_kfifo:
441 kfifo_free(sony_laptop_input.fifo);
442
443err_dec_users:
444 atomic_dec(&sony_laptop_input.users);
445 return error;
446}
447
448static void sony_laptop_remove_input(void)
449{
450 /* cleanup only after the last user has gone */
451 if (!atomic_dec_and_test(&sony_laptop_input.users))
452 return;
453
454 /* flush workqueue first */
455 flush_workqueue(sony_laptop_input.wq);
456
457 /* destroy input devs */
458 input_unregister_device(sony_laptop_input.key_dev);
459 sony_laptop_input.key_dev = NULL;
460
461 if (sony_laptop_input.jog_dev) {
462 input_unregister_device(sony_laptop_input.jog_dev);
463 sony_laptop_input.jog_dev = NULL;
464 }
465
466 destroy_workqueue(sony_laptop_input.wq);
467 kfifo_free(sony_laptop_input.fifo);
468}
469
malattia@linux.it56b87562007-04-09 10:19:05 +0200470/*********** Platform Device ***********/
471
472static atomic_t sony_pf_users = ATOMIC_INIT(0);
473static struct platform_driver sony_pf_driver = {
474 .driver = {
475 .name = "sony-laptop",
476 .owner = THIS_MODULE,
477 }
478};
479static struct platform_device *sony_pf_device;
480
481static int sony_pf_add(void)
482{
483 int ret = 0;
484
485 /* don't run again if already initialized */
486 if (atomic_add_return(1, &sony_pf_users) > 1)
487 return 0;
488
489 ret = platform_driver_register(&sony_pf_driver);
490 if (ret)
491 goto out;
492
493 sony_pf_device = platform_device_alloc("sony-laptop", -1);
494 if (!sony_pf_device) {
495 ret = -ENOMEM;
496 goto out_platform_registered;
497 }
498
499 ret = platform_device_add(sony_pf_device);
500 if (ret)
501 goto out_platform_alloced;
502
503 return 0;
504
505 out_platform_alloced:
506 platform_device_put(sony_pf_device);
507 sony_pf_device = NULL;
508 out_platform_registered:
509 platform_driver_unregister(&sony_pf_driver);
510 out:
511 atomic_dec(&sony_pf_users);
512 return ret;
513}
514
515static void sony_pf_remove(void)
516{
517 /* deregister only after the last user has gone */
518 if (!atomic_dec_and_test(&sony_pf_users))
519 return;
520
521 platform_device_del(sony_pf_device);
522 platform_device_put(sony_pf_device);
523 platform_driver_unregister(&sony_pf_driver);
524}
525
526/*********** SNC (SNY5001) Device ***********/
527
malattia@linux.it33a04452007-04-09 19:26:03 +0200528/* the device uses 1-based values, while the backlight subsystem uses
529 0-based values */
530#define SONY_MAX_BRIGHTNESS 8
531
532#define SNC_VALIDATE_IN 0
533#define SNC_VALIDATE_OUT 1
534
malattia@linux.it59b19102007-04-09 10:19:04 +0200535static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500536 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200537static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500538 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100539static int boolean_validate(const int, const int);
540static int brightness_default_validate(const int, const int);
541
malattia@linux.it59b19102007-04-09 10:19:04 +0200542struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500543 char *name; /* name of the entry */
544 char **acpiget; /* names of the ACPI get function */
545 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100546 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500547 int value; /* current setting */
548 int valid; /* Has ever been set */
549 int debug; /* active only in debug mode ? */
550 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100551};
552
malattia@linux.it59b19102007-04-09 10:19:04 +0200553#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100554 static char *snc_##_name[] = { _values, NULL }
555
malattia@linux.it59b19102007-04-09 10:19:04 +0200556#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100557 { \
558 .name = __stringify(_name), \
559 .acpiget = _getters, \
560 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100561 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100562 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200563 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100564 }
565
malattia@linux.it59b19102007-04-09 10:19:04 +0200566#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100567
malattia@linux.it59b19102007-04-09 10:19:04 +0200568SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100569
malattia@linux.it59b19102007-04-09 10:19:04 +0200570SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
571SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100572
malattia@linux.it59b19102007-04-09 10:19:04 +0200573SNC_HANDLE_NAMES(cdpower_get, "GCDP");
574SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100575
malattia@linux.it59b19102007-04-09 10:19:04 +0200576SNC_HANDLE_NAMES(audiopower_get, "GAZP");
577SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100578
malattia@linux.it59b19102007-04-09 10:19:04 +0200579SNC_HANDLE_NAMES(lanpower_get, "GLNP");
580SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100581
Mattia Dongili044847e2007-07-16 02:34:33 +0900582SNC_HANDLE_NAMES(lidstate_get, "GLID");
583
584SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
585SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
586
587SNC_HANDLE_NAMES(gainbass_get, "GMGB");
588SNC_HANDLE_NAMES(gainbass_set, "CMGB");
589
malattia@linux.it59b19102007-04-09 10:19:04 +0200590SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100591
malattia@linux.it59b19102007-04-09 10:19:04 +0200592SNC_HANDLE_NAMES(CTR_get, "GCTR");
593SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100594
malattia@linux.it59b19102007-04-09 10:19:04 +0200595SNC_HANDLE_NAMES(PCR_get, "GPCR");
596SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100597
malattia@linux.it59b19102007-04-09 10:19:04 +0200598SNC_HANDLE_NAMES(CMI_get, "GCMI");
599SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100600
malattia@linux.it59b19102007-04-09 10:19:04 +0200601static struct sony_nc_value sony_nc_values[] = {
602 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100603 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200604 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
605 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
606 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100607 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200608 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100609 boolean_validate, 1),
Mattia Dongili044847e2007-07-16 02:34:33 +0900610 SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
611 boolean_validate, 0),
612 SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
613 boolean_validate, 0),
614 SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
615 boolean_validate, 0),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100616 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200617 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
618 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
619 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
620 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
621 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100622};
623
malattia@linux.it59b19102007-04-09 10:19:04 +0200624static acpi_handle sony_nc_acpi_handle;
625static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100626
Mattia Dongilid78865c2007-02-07 20:01:56 +0100627/*
628 * acpi_evaluate_object wrappers
629 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100630static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
631{
632 struct acpi_buffer output;
633 union acpi_object out_obj;
634 acpi_status status;
635
636 output.length = sizeof(out_obj);
637 output.pointer = &out_obj;
638
639 status = acpi_evaluate_object(handle, name, NULL, &output);
640 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
641 *result = out_obj.integer.value;
642 return 0;
643 }
644
malattia@linux.itf6119b02007-04-09 19:31:06 +0200645 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100646
647 return -1;
648}
649
650static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
651 int *result)
652{
653 struct acpi_object_list params;
654 union acpi_object in_obj;
655 struct acpi_buffer output;
656 union acpi_object out_obj;
657 acpi_status status;
658
659 params.count = 1;
660 params.pointer = &in_obj;
661 in_obj.type = ACPI_TYPE_INTEGER;
662 in_obj.integer.value = value;
663
664 output.length = sizeof(out_obj);
665 output.pointer = &out_obj;
666
667 status = acpi_evaluate_object(handle, name, &params, &output);
668 if (status == AE_OK) {
669 if (result != NULL) {
670 if (out_obj.type != ACPI_TYPE_INTEGER) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200671 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
Stelian Pop7f09c432007-01-13 23:04:31 +0100672 "return type\n");
673 return -1;
674 }
675 *result = out_obj.integer.value;
676 }
677 return 0;
678 }
679
malattia@linux.itf6119b02007-04-09 19:31:06 +0200680 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100681
682 return -1;
683}
684
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100685/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200686 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100687 */
688
689/* brightness_default_validate:
690 *
691 * manipulate input output values to keep consistency with the
692 * backlight framework for which brightness values are 0-based.
693 */
694static int brightness_default_validate(const int direction, const int value)
695{
696 switch (direction) {
697 case SNC_VALIDATE_OUT:
698 return value - 1;
699 case SNC_VALIDATE_IN:
700 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
701 return value + 1;
702 }
703 return -EINVAL;
704}
705
706/* boolean_validate:
707 *
708 * on input validate boolean values 0/1, on output just pass the
709 * received value.
710 */
711static int boolean_validate(const int direction, const int value)
712{
713 if (direction == SNC_VALIDATE_IN) {
714 if (value != 0 && value != 1)
715 return -EINVAL;
716 }
717 return value;
718}
719
720/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200721 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100722 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200723static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500724 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100725{
Stelian Pop7f09c432007-01-13 23:04:31 +0100726 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200727 struct sony_nc_value *item =
728 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100729
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100730 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100731 return -EIO;
732
malattia@linux.it59b19102007-04-09 10:19:04 +0200733 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100734 return -EIO;
735
Mattia Dongili156c2212007-02-12 22:01:07 +0100736 if (item->validate)
737 value = item->validate(SNC_VALIDATE_OUT, value);
738
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100739 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100740}
741
malattia@linux.it59b19102007-04-09 10:19:04 +0200742static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500743 struct device_attribute *attr,
744 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100745{
Stelian Pop7f09c432007-01-13 23:04:31 +0100746 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200747 struct sony_nc_value *item =
748 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100749
750 if (!item->acpiset)
751 return -EIO;
752
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100753 if (count > 31)
754 return -EINVAL;
755
756 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100757
Mattia Dongili156c2212007-02-12 22:01:07 +0100758 if (item->validate)
759 value = item->validate(SNC_VALIDATE_IN, value);
760
761 if (value < 0)
762 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100763
malattia@linux.it59b19102007-04-09 10:19:04 +0200764 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100765 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100766 item->value = value;
767 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100768 return count;
769}
770
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100771
Mattia Dongilid78865c2007-02-07 20:01:56 +0100772/*
773 * Backlight device
774 */
775static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100776{
malattia@linux.it59b19102007-04-09 10:19:04 +0200777 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000778 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100779}
780
Mattia Dongilid78865c2007-02-07 20:01:56 +0100781static int sony_backlight_get_brightness(struct backlight_device *bd)
782{
783 int value;
784
malattia@linux.it59b19102007-04-09 10:19:04 +0200785 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100786 return 0;
787 /* brightness levels are 1-based, while backlight ones are 0-based */
788 return value - 1;
789}
790
791static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000792static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500793 .update_status = sony_backlight_update_status,
794 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100795};
796
797/*
Mattia Dongili6315fd12007-07-16 02:34:35 +0900798 * New SNC-only Vaios event mapping to driver known keys
799 */
800struct sony_nc_event {
801 u8 data;
802 u8 event;
803};
804
805static struct sony_nc_event *sony_nc_events;
806
807/* Vaio C* --maybe also FE*, N* and AR* ?-- special init sequence
808 * for Fn keys
809 */
810static int sony_nc_C_enable(struct dmi_system_id *id)
811{
812 int result = 0;
813
814 printk(KERN_NOTICE DRV_PFX "detected %s\n", id->ident);
815
816 sony_nc_events = id->driver_data;
817
818 if (acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x4, &result) < 0
819 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x2, &result) < 0
820 || acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0x10, &result) < 0
821 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x0, &result) < 0
822 || acpi_callsetfunc(sony_nc_acpi_handle, "SN03", 0x2, &result) < 0
823 || acpi_callsetfunc(sony_nc_acpi_handle, "SN07", 0x101, &result) < 0) {
824 printk(KERN_WARNING DRV_PFX "failed to initialize SNC, some "
825 "functionalities may be missing\n");
826 return 1;
827 }
828 return 0;
829}
830
831static struct sony_nc_event sony_C_events[] = {
832 { 0x81, SONYPI_EVENT_FNKEY_F1 },
833 { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
834 { 0x85, SONYPI_EVENT_FNKEY_F5 },
835 { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
836 { 0x86, SONYPI_EVENT_FNKEY_F6 },
837 { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
838 { 0x87, SONYPI_EVENT_FNKEY_F7 },
839 { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
840 { 0x8A, SONYPI_EVENT_FNKEY_F10 },
841 { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
842 { 0x8C, SONYPI_EVENT_FNKEY_F12 },
843 { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
844 { 0, 0 },
845};
846
847/* SNC-only model map */
848struct dmi_system_id sony_nc_ids[] = {
849 {
Mattia Dongili89892d12007-07-16 02:34:38 +0900850 .ident = "Sony Vaio FE Series",
851 .callback = sony_nc_C_enable,
852 .driver_data = sony_C_events,
853 .matches = {
854 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
855 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FE"),
856 },
857 },
858 {
Mattia Dongili6315fd12007-07-16 02:34:35 +0900859 .ident = "Sony Vaio C Series",
860 .callback = sony_nc_C_enable,
861 .driver_data = sony_C_events,
862 .matches = {
863 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
864 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-C"),
865 },
866 },
867 { }
868};
869
870/*
Mattia Dongilid78865c2007-02-07 20:01:56 +0100871 * ACPI callbacks
872 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100873static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
874{
Mattia Dongili6315fd12007-07-16 02:34:35 +0900875 struct sony_nc_event *evmap;
876 u32 ev = event;
877 int result;
878
879 if (ev == 0x92) {
880 /* read the key pressed from EC.GECR
881 * A call to SN07 with 0x0202 will do it as well respecting
882 * the current protocol on different OSes
883 *
884 * Note: the path for GECR may be
885 * \_SB.PCI0.LPCB.EC (C, FE, AR, N and friends)
886 * \_SB.PCI0.PIB.EC0 (VGN-FR notifications are sent directly, no GECR)
887 *
888 * TODO: we may want to do the same for the older GHKE -need
889 * dmi list- so this snippet may become one more callback.
890 */
891 if (acpi_callsetfunc(handle, "SN07", 0x0202, &result) < 0)
892 dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
893 else
894 ev = result & 0xFF;
895 }
896
897 if (sony_nc_events)
898 for (evmap = sony_nc_events; evmap->event; evmap++) {
899 if (evmap->data == ev) {
900 ev = evmap->event;
901 break;
902 }
903 }
904
905 dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
906 sony_laptop_report_input_event(ev);
907 acpi_bus_generate_event(sony_nc_acpi_device, 1, ev);
Stelian Pop7f09c432007-01-13 23:04:31 +0100908}
909
910static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
911 void *context, void **return_value)
912{
913 struct acpi_namespace_node *node;
914 union acpi_operand_object *operand;
915
Len Browna02d1c12007-02-07 15:34:02 -0500916 node = (struct acpi_namespace_node *)handle;
917 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100918
malattia@linux.itf6119b02007-04-09 19:31:06 +0200919 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
Stelian Pop7f09c432007-01-13 23:04:31 +0100920 (u32) operand->method.param_count);
921
922 return AE_OK;
923}
924
Mattia Dongilid78865c2007-02-07 20:01:56 +0100925/*
926 * ACPI device
927 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200928static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100929{
malattia@linux.it59b19102007-04-09 10:19:04 +0200930 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100931
malattia@linux.it59b19102007-04-09 10:19:04 +0200932 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100933 int ret;
934
935 if (!item->valid)
936 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200937 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500938 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100939 if (ret < 0) {
940 printk("%s: %d\n", __FUNCTION__, ret);
941 break;
942 }
943 }
Mattia Dongili6315fd12007-07-16 02:34:35 +0900944
945 /* re-initialize models with specific requirements */
946 dmi_check_system(sony_nc_ids);
947
Mattia Dongilid78865c2007-02-07 20:01:56 +0100948 return 0;
949}
950
malattia@linux.it59b19102007-04-09 10:19:04 +0200951static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100952{
953 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800954 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100955 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200956 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100957
malattia@linux.itf6119b02007-04-09 19:31:06 +0200958 printk(KERN_INFO DRV_PFX "%s v%s.\n",
959 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
960
malattia@linux.it59b19102007-04-09 10:19:04 +0200961 sony_nc_acpi_device = device;
malattia@linux.it33a04452007-04-09 19:26:03 +0200962 strcpy(acpi_device_class(device), "sony/hotkey");
Stelian Popc5611622007-01-13 23:04:37 +0100963
malattia@linux.it59b19102007-04-09 10:19:04 +0200964 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100965
Mattia Dongilib25b7322007-07-16 02:34:36 +0900966 /* read device status */
967 result = acpi_bus_get_status(device);
968 /* bail IFF the above call was successful and the device is not present */
969 if (!result && !device->status.present) {
970 dprintk("Device not present\n");
971 result = -ENODEV;
972 goto outwalk;
973 }
974
Stelian Pop7f09c432007-01-13 23:04:31 +0100975 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200976 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100977 1, sony_walk_callback, NULL, NULL);
978 if (ACPI_FAILURE(status)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +0200979 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100980 result = -ENODEV;
981 goto outwalk;
982 }
Stelian Popc5611622007-01-13 23:04:37 +0100983 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100984
Mattia Dongilib25b7322007-07-16 02:34:36 +0900985 /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
986 * should be respected as we already checked for the device presence above */
987 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
988 dprintk("Invoking _INI\n");
989 if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
990 NULL, NULL)))
991 dprintk("_INI Method failed\n");
992 }
993
malattia@linux.it1549ee62007-04-09 10:19:08 +0200994 /* setup input devices and helper fifo */
995 result = sony_laptop_setup_input();
996 if (result) {
997 printk(KERN_ERR DRV_PFX
998 "Unabe to create input devices.\n");
999 goto outwalk;
1000 }
1001
malattia@linux.it59b19102007-04-09 10:19:04 +02001002 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001003 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -05001004 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +01001005 if (ACPI_FAILURE(status)) {
Mattia Dongilib25b7322007-07-16 02:34:36 +09001006 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
Stelian Popc5611622007-01-13 23:04:37 +01001007 result = -ENODEV;
malattia@linux.it1549ee62007-04-09 10:19:08 +02001008 goto outinput;
Stelian Pop7f09c432007-01-13 23:04:31 +01001009 }
1010
malattia@linux.it59b19102007-04-09 10:19:04 +02001011 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +01001012 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -05001013 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +00001014 &sony_backlight_ops);
Mattia Dongili7df03b82007-01-13 23:04:41 +01001015
Len Browna02d1c12007-02-07 15:34:02 -05001016 if (IS_ERR(sony_backlight_device)) {
malattia@linux.itf6119b02007-04-09 19:31:06 +02001017 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
Mattia Dongili7df03b82007-01-13 23:04:41 +01001018 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +00001019 } else {
1020 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -05001021 sony_backlight_get_brightness
1022 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +00001023 sony_backlight_device->props.max_brightness =
1024 SONY_MAX_BRIGHTNESS - 1;
1025 }
1026
Alessandro Guido50f62af2007-01-13 23:04:34 +01001027 }
Stelian Pop7f09c432007-01-13 23:04:31 +01001028
Mattia Dongili6315fd12007-07-16 02:34:35 +09001029 /* initialize models with specific requirements */
1030 dmi_check_system(sony_nc_ids);
1031
malattia@linux.it49a11de2007-04-09 19:28:56 +02001032 result = sony_pf_add();
1033 if (result)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01001034 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +01001035
malattia@linux.it56b87562007-04-09 10:19:05 +02001036 /* create sony_pf sysfs attributes related to the SNC device */
1037 for (item = sony_nc_values; item->name; ++item) {
1038
1039 if (!debug && item->debug)
1040 continue;
1041
1042 /* find the available acpiget as described in the DSDT */
1043 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1044 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1045 *item->acpiget,
1046 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001047 dprintk("Found %s getter: %s\n",
1048 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +02001049 item->devattr.attr.mode |= S_IRUGO;
1050 break;
1051 }
1052 }
1053
1054 /* find the available acpiset as described in the DSDT */
1055 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1056 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1057 *item->acpiset,
1058 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +02001059 dprintk("Found %s setter: %s\n",
1060 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +02001061 item->devattr.attr.mode |= S_IWUSR;
1062 break;
1063 }
1064 }
1065
1066 if (item->devattr.attr.mode != 0) {
1067 result =
1068 device_create_file(&sony_pf_device->dev,
1069 &item->devattr);
1070 if (result)
1071 goto out_sysfs;
1072 }
1073 }
1074
Stelian Pop7f09c432007-01-13 23:04:31 +01001075 return 0;
1076
malattia@linux.it56b87562007-04-09 10:19:05 +02001077 out_sysfs:
1078 for (item = sony_nc_values; item->name; ++item) {
1079 device_remove_file(&sony_pf_device->dev, &item->devattr);
1080 }
1081 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001082
Len Browna02d1c12007-02-07 15:34:02 -05001083 outbacklight:
Mattia Dongili7df03b82007-01-13 23:04:41 +01001084 if (sony_backlight_device)
1085 backlight_device_unregister(sony_backlight_device);
1086
malattia@linux.it59b19102007-04-09 10:19:04 +02001087 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001088 ACPI_DEVICE_NOTIFY,
1089 sony_acpi_notify);
1090 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001091 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
malattia@linux.it1549ee62007-04-09 10:19:08 +02001092
1093 outinput:
1094 sony_laptop_remove_input();
1095
Len Browna02d1c12007-02-07 15:34:02 -05001096 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +01001097 return result;
1098}
1099
malattia@linux.it59b19102007-04-09 10:19:04 +02001100static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +01001101{
1102 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +02001103 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +01001104
Alessandro Guido50f62af2007-01-13 23:04:34 +01001105 if (sony_backlight_device)
1106 backlight_device_unregister(sony_backlight_device);
1107
malattia@linux.it59b19102007-04-09 10:19:04 +02001108 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +01001109
malattia@linux.it59b19102007-04-09 10:19:04 +02001110 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +01001111 ACPI_DEVICE_NOTIFY,
1112 sony_acpi_notify);
1113 if (ACPI_FAILURE(status))
malattia@linux.itf6119b02007-04-09 19:31:06 +02001114 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001115
malattia@linux.it56b87562007-04-09 10:19:05 +02001116 for (item = sony_nc_values; item->name; ++item) {
1117 device_remove_file(&sony_pf_device->dev, &item->devattr);
1118 }
1119
1120 sony_pf_remove();
malattia@linux.it1549ee62007-04-09 10:19:08 +02001121 sony_laptop_remove_input();
malattia@linux.itf6119b02007-04-09 19:31:06 +02001122 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
Stelian Pop7f09c432007-01-13 23:04:31 +01001123
1124 return 0;
1125}
1126
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001127static const struct acpi_device_id sony_device_ids[] = {
1128 {SONY_NC_HID, 0},
1129 {SONY_PIC_HID, 0},
1130 {"", 0},
1131};
1132MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1133
1134static const struct acpi_device_id sony_nc_device_ids[] = {
1135 {SONY_NC_HID, 0},
1136 {"", 0},
1137};
1138
malattia@linux.it59b19102007-04-09 10:19:04 +02001139static struct acpi_driver sony_nc_driver = {
1140 .name = SONY_NC_DRIVER_NAME,
1141 .class = SONY_NC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001142 .ids = sony_nc_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02001143 .owner = THIS_MODULE,
Len Browna02d1c12007-02-07 15:34:02 -05001144 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +02001145 .add = sony_nc_add,
1146 .remove = sony_nc_remove,
1147 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -05001148 },
Andrew Morton3f4f4612007-01-13 23:04:32 +01001149};
1150
malattia@linux.it33a04452007-04-09 19:26:03 +02001151/*********** SPIC (SNY6001) Device ***********/
1152
1153#define SONYPI_DEVICE_TYPE1 0x00000001
1154#define SONYPI_DEVICE_TYPE2 0x00000002
1155#define SONYPI_DEVICE_TYPE3 0x00000004
1156
Mattia Dongili22a17782007-07-16 02:34:39 +09001157#define SONYPI_TYPE1_OFFSET 0x04
1158#define SONYPI_TYPE2_OFFSET 0x12
1159#define SONYPI_TYPE3_OFFSET 0x12
malattia@linux.it33a04452007-04-09 19:26:03 +02001160
malattia@linux.it33a04452007-04-09 19:26:03 +02001161struct sony_pic_ioport {
1162 struct acpi_resource_io io;
1163 struct list_head list;
1164};
1165
1166struct sony_pic_irq {
1167 struct acpi_resource_irq irq;
1168 struct list_head list;
1169};
1170
1171struct sony_pic_dev {
1172 int model;
Mattia Dongili22a17782007-07-16 02:34:39 +09001173 u16 evport_offset;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001174 u8 camera_power;
1175 u8 bluetooth_power;
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001176 u8 wwan_power;
malattia@linux.it33a04452007-04-09 19:26:03 +02001177 struct acpi_device *acpi_dev;
1178 struct sony_pic_irq *cur_irq;
1179 struct sony_pic_ioport *cur_ioport;
1180 struct list_head interrupts;
1181 struct list_head ioports;
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001182 struct mutex lock;
malattia@linux.it33a04452007-04-09 19:26:03 +02001183};
1184
1185static struct sony_pic_dev spic_dev = {
1186 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
1187 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
1188};
1189
1190/* Event masks */
1191#define SONYPI_JOGGER_MASK 0x00000001
1192#define SONYPI_CAPTURE_MASK 0x00000002
1193#define SONYPI_FNKEY_MASK 0x00000004
1194#define SONYPI_BLUETOOTH_MASK 0x00000008
1195#define SONYPI_PKEY_MASK 0x00000010
1196#define SONYPI_BACK_MASK 0x00000020
1197#define SONYPI_HELP_MASK 0x00000040
1198#define SONYPI_LID_MASK 0x00000080
1199#define SONYPI_ZOOM_MASK 0x00000100
1200#define SONYPI_THUMBPHRASE_MASK 0x00000200
1201#define SONYPI_MEYE_MASK 0x00000400
1202#define SONYPI_MEMORYSTICK_MASK 0x00000800
1203#define SONYPI_BATTERY_MASK 0x00001000
1204#define SONYPI_WIRELESS_MASK 0x00002000
1205
1206struct sonypi_event {
1207 u8 data;
1208 u8 event;
1209};
1210
1211/* The set of possible button release events */
1212static struct sonypi_event sonypi_releaseev[] = {
1213 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1214 { 0, 0 }
1215};
1216
1217/* The set of possible jogger events */
1218static struct sonypi_event sonypi_joggerev[] = {
1219 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1220 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1221 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1222 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1223 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1224 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1225 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1226 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1227 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1228 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1229 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1230 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1231 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1232 { 0, 0 }
1233};
1234
1235/* The set of possible capture button events */
1236static struct sonypi_event sonypi_captureev[] = {
1237 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1238 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1239 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1240 { 0, 0 }
1241};
1242
1243/* The set of possible fnkeys events */
1244static struct sonypi_event sonypi_fnkeyev[] = {
1245 { 0x10, SONYPI_EVENT_FNKEY_ESC },
1246 { 0x11, SONYPI_EVENT_FNKEY_F1 },
1247 { 0x12, SONYPI_EVENT_FNKEY_F2 },
1248 { 0x13, SONYPI_EVENT_FNKEY_F3 },
1249 { 0x14, SONYPI_EVENT_FNKEY_F4 },
1250 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1251 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1252 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1253 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1254 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1255 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1256 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1257 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1258 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1259 { 0x21, SONYPI_EVENT_FNKEY_1 },
1260 { 0x22, SONYPI_EVENT_FNKEY_2 },
1261 { 0x31, SONYPI_EVENT_FNKEY_D },
1262 { 0x32, SONYPI_EVENT_FNKEY_E },
1263 { 0x33, SONYPI_EVENT_FNKEY_F },
1264 { 0x34, SONYPI_EVENT_FNKEY_S },
1265 { 0x35, SONYPI_EVENT_FNKEY_B },
1266 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1267 { 0, 0 }
1268};
1269
1270/* The set of possible program key events */
1271static struct sonypi_event sonypi_pkeyev[] = {
1272 { 0x01, SONYPI_EVENT_PKEY_P1 },
1273 { 0x02, SONYPI_EVENT_PKEY_P2 },
1274 { 0x04, SONYPI_EVENT_PKEY_P3 },
1275 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1276 { 0, 0 }
1277};
1278
1279/* The set of possible bluetooth events */
1280static struct sonypi_event sonypi_blueev[] = {
1281 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1282 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1283 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1284 { 0, 0 }
1285};
1286
1287/* The set of possible wireless events */
1288static struct sonypi_event sonypi_wlessev[] = {
1289 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1290 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1291 { 0, 0 }
1292};
1293
1294/* The set of possible back button events */
1295static struct sonypi_event sonypi_backev[] = {
1296 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1297 { 0, 0 }
1298};
1299
1300/* The set of possible help button events */
1301static struct sonypi_event sonypi_helpev[] = {
1302 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1303 { 0, 0 }
1304};
1305
1306
1307/* The set of possible lid events */
1308static struct sonypi_event sonypi_lidev[] = {
1309 { 0x51, SONYPI_EVENT_LID_CLOSED },
1310 { 0x50, SONYPI_EVENT_LID_OPENED },
1311 { 0, 0 }
1312};
1313
1314/* The set of possible zoom events */
1315static struct sonypi_event sonypi_zoomev[] = {
1316 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1317 { 0, 0 }
1318};
1319
1320/* The set of possible thumbphrase events */
1321static struct sonypi_event sonypi_thumbphraseev[] = {
1322 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1323 { 0, 0 }
1324};
1325
1326/* The set of possible motioneye camera events */
1327static struct sonypi_event sonypi_meyeev[] = {
1328 { 0x00, SONYPI_EVENT_MEYE_FACE },
1329 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1330 { 0, 0 }
1331};
1332
1333/* The set of possible memorystick events */
1334static struct sonypi_event sonypi_memorystickev[] = {
1335 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1336 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1337 { 0, 0 }
1338};
1339
1340/* The set of possible battery events */
1341static struct sonypi_event sonypi_batteryev[] = {
1342 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1343 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1344 { 0, 0 }
1345};
1346
1347static struct sonypi_eventtypes {
1348 int model;
1349 u8 data;
1350 unsigned long mask;
1351 struct sonypi_event * events;
1352} sony_pic_eventtypes[] = {
1353 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1354 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1355 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1356 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1357 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1358 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1359 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1360 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1361 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1362 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1363
1364 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1365 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1366 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1367 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1368 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1369 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1370 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1371 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1372 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1373 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1374 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1375 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1376 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1377 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1378
1379 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1380 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1381 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1382 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1383 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1384 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1385 { 0 }
1386};
1387
1388static int sony_pic_detect_device_type(void)
1389{
1390 struct pci_dev *pcidev;
1391 int model = 0;
1392
1393 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1394 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1395 model = SONYPI_DEVICE_TYPE1;
1396
1397 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1398 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1399 model = SONYPI_DEVICE_TYPE3;
1400
1401 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1402 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1403 model = SONYPI_DEVICE_TYPE3;
1404
1405 else
1406 model = SONYPI_DEVICE_TYPE2;
1407
1408 if (pcidev)
1409 pci_dev_put(pcidev);
1410
1411 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1412 model == SONYPI_DEVICE_TYPE1 ? 1 :
1413 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1414 return model;
1415}
1416
1417#define ITERATIONS_LONG 10000
1418#define ITERATIONS_SHORT 10
1419#define wait_on_command(command, iterations) { \
1420 unsigned int n = iterations; \
1421 while (--n && (command)) \
1422 udelay(1); \
1423 if (!n) \
1424 dprintk("command failed at %s : %s (line %d)\n", \
1425 __FILE__, __FUNCTION__, __LINE__); \
1426}
1427
1428static u8 sony_pic_call1(u8 dev)
1429{
1430 u8 v1, v2;
1431
1432 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1433 ITERATIONS_LONG);
1434 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1435 v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1436 v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1437 dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1438 return v2;
1439}
1440
1441static u8 sony_pic_call2(u8 dev, u8 fn)
1442{
1443 u8 v1;
1444
1445 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1446 ITERATIONS_LONG);
1447 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1448 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1449 ITERATIONS_LONG);
1450 outb(fn, spic_dev.cur_ioport->io.minimum);
1451 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1452 dprintk("sony_pic_call2: 0x%.4x\n", v1);
1453 return v1;
1454}
1455
malattia@linux.it49a11de2007-04-09 19:28:56 +02001456static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1457{
1458 u8 v1;
1459
1460 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1461 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1462 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1463 outb(fn, spic_dev.cur_ioport->io.minimum);
1464 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1465 outb(v, spic_dev.cur_ioport->io.minimum);
1466 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1467 dprintk("sony_pic_call3: 0x%.4x\n", v1);
1468 return v1;
1469}
1470
1471/* camera tests and poweron/poweroff */
1472#define SONYPI_CAMERA_PICTURE 5
malattia@linux.it49a11de2007-04-09 19:28:56 +02001473#define SONYPI_CAMERA_CONTROL 0x10
malattia@linux.ite3646322007-04-28 23:34:22 +09001474
1475#define SONYPI_CAMERA_BRIGHTNESS 0
1476#define SONYPI_CAMERA_CONTRAST 1
1477#define SONYPI_CAMERA_HUE 2
1478#define SONYPI_CAMERA_COLOR 3
1479#define SONYPI_CAMERA_SHARPNESS 4
1480
1481#define SONYPI_CAMERA_EXPOSURE_MASK 0xC
1482#define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
1483#define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
1484#define SONYPI_CAMERA_MUTE_MASK 0x40
1485
1486/* the rest don't need a loop until not 0xff */
1487#define SONYPI_CAMERA_AGC 6
1488#define SONYPI_CAMERA_AGC_MASK 0x30
1489#define SONYPI_CAMERA_SHUTTER_MASK 0x7
1490
1491#define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
1492#define SONYPI_CAMERA_CONTROL 0x10
1493
1494#define SONYPI_CAMERA_STATUS 7
1495#define SONYPI_CAMERA_STATUS_READY 0x2
1496#define SONYPI_CAMERA_STATUS_POSITION 0x4
1497
1498#define SONYPI_DIRECTION_BACKWARDS 0x4
1499
1500#define SONYPI_CAMERA_REVISION 8
1501#define SONYPI_CAMERA_ROMVERSION 9
malattia@linux.it49a11de2007-04-09 19:28:56 +02001502
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001503static int __sony_pic_camera_ready(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001504{
1505 u8 v;
1506
1507 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1508 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1509}
1510
malattia@linux.ite3646322007-04-28 23:34:22 +09001511static int __sony_pic_camera_off(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001512{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001513 if (!camera) {
1514 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1515 return -ENODEV;
1516 }
1517
malattia@linux.it49a11de2007-04-09 19:28:56 +02001518 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1519 SONYPI_CAMERA_MUTE_MASK),
1520 ITERATIONS_SHORT);
1521
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001522 if (spic_dev.camera_power) {
1523 sony_pic_call2(0x91, 0);
1524 spic_dev.camera_power = 0;
1525 }
1526 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001527}
1528
malattia@linux.ite3646322007-04-28 23:34:22 +09001529static int __sony_pic_camera_on(void)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001530{
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001531 int i, j, x;
1532
1533 if (!camera) {
1534 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1535 return -ENODEV;
1536 }
malattia@linux.it49a11de2007-04-09 19:28:56 +02001537
1538 if (spic_dev.camera_power)
malattia@linux.ite3646322007-04-28 23:34:22 +09001539 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001540
1541 for (j = 5; j > 0; j--) {
1542
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001543 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001544 msleep(10);
1545 sony_pic_call1(0x93);
1546
1547 for (i = 400; i > 0; i--) {
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001548 if (__sony_pic_camera_ready())
malattia@linux.it49a11de2007-04-09 19:28:56 +02001549 break;
1550 msleep(10);
1551 }
1552 if (i)
1553 break;
1554 }
1555
1556 if (j == 0) {
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001557 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
malattia@linux.ite3646322007-04-28 23:34:22 +09001558 return -ENODEV;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001559 }
1560
1561 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1562 0x5a),
1563 ITERATIONS_SHORT);
1564
1565 spic_dev.camera_power = 1;
malattia@linux.it5f3d2892007-04-28 23:18:45 +09001566 return 0;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001567}
1568
malattia@linux.ite3646322007-04-28 23:34:22 +09001569/* External camera command (exported to the motion eye v4l driver) */
1570int sony_pic_camera_command(int command, u8 value)
1571{
1572 if (!camera)
1573 return -EIO;
1574
1575 mutex_lock(&spic_dev.lock);
1576
1577 switch (command) {
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001578 case SONY_PIC_COMMAND_SETCAMERA:
malattia@linux.ite3646322007-04-28 23:34:22 +09001579 if (value)
1580 __sony_pic_camera_on();
1581 else
1582 __sony_pic_camera_off();
1583 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001584 case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001585 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1586 ITERATIONS_SHORT);
1587 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001588 case SONY_PIC_COMMAND_SETCAMERACONTRAST:
malattia@linux.ite3646322007-04-28 23:34:22 +09001589 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1590 ITERATIONS_SHORT);
1591 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001592 case SONY_PIC_COMMAND_SETCAMERAHUE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001593 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1594 ITERATIONS_SHORT);
1595 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001596 case SONY_PIC_COMMAND_SETCAMERACOLOR:
malattia@linux.ite3646322007-04-28 23:34:22 +09001597 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1598 ITERATIONS_SHORT);
1599 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001600 case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
malattia@linux.ite3646322007-04-28 23:34:22 +09001601 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1602 ITERATIONS_SHORT);
1603 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001604 case SONY_PIC_COMMAND_SETCAMERAPICTURE:
malattia@linux.ite3646322007-04-28 23:34:22 +09001605 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1606 ITERATIONS_SHORT);
1607 break;
malattia@linux.it1ce82c12007-04-28 23:34:36 +09001608 case SONY_PIC_COMMAND_SETCAMERAAGC:
malattia@linux.ite3646322007-04-28 23:34:22 +09001609 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1610 ITERATIONS_SHORT);
1611 break;
1612 default:
1613 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1614 command);
1615 break;
1616 }
1617 mutex_unlock(&spic_dev.lock);
1618 return 0;
1619}
1620EXPORT_SYMBOL(sony_pic_camera_command);
1621
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001622/* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1623static void sony_pic_set_wwanpower(u8 state)
1624{
1625 state = !!state;
1626 mutex_lock(&spic_dev.lock);
1627 if (spic_dev.wwan_power == state) {
1628 mutex_unlock(&spic_dev.lock);
1629 return;
1630 }
1631 sony_pic_call2(0xB0, state);
1632 spic_dev.wwan_power = state;
1633 mutex_unlock(&spic_dev.lock);
1634}
1635
1636static ssize_t sony_pic_wwanpower_store(struct device *dev,
1637 struct device_attribute *attr,
1638 const char *buffer, size_t count)
1639{
1640 unsigned long value;
1641 if (count > 31)
1642 return -EINVAL;
1643
1644 value = simple_strtoul(buffer, NULL, 10);
1645 sony_pic_set_wwanpower(value);
1646
1647 return count;
1648}
1649
1650static ssize_t sony_pic_wwanpower_show(struct device *dev,
1651 struct device_attribute *attr, char *buffer)
1652{
1653 ssize_t count;
1654 mutex_lock(&spic_dev.lock);
1655 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1656 mutex_unlock(&spic_dev.lock);
1657 return count;
1658}
1659
malattia@linux.it49a11de2007-04-09 19:28:56 +02001660/* bluetooth subsystem power state */
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001661static void __sony_pic_set_bluetoothpower(u8 state)
malattia@linux.it49a11de2007-04-09 19:28:56 +02001662{
1663 state = !!state;
1664 if (spic_dev.bluetooth_power == state)
1665 return;
1666 sony_pic_call2(0x96, state);
1667 sony_pic_call1(0x82);
1668 spic_dev.bluetooth_power = state;
1669}
1670
1671static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1672 struct device_attribute *attr,
1673 const char *buffer, size_t count)
1674{
1675 unsigned long value;
1676 if (count > 31)
1677 return -EINVAL;
1678
1679 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001680 mutex_lock(&spic_dev.lock);
1681 __sony_pic_set_bluetoothpower(value);
1682 mutex_unlock(&spic_dev.lock);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001683
1684 return count;
1685}
1686
1687static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1688 struct device_attribute *attr, char *buffer)
1689{
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001690 ssize_t count = 0;
1691 mutex_lock(&spic_dev.lock);
1692 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1693 mutex_unlock(&spic_dev.lock);
1694 return count;
malattia@linux.it49a11de2007-04-09 19:28:56 +02001695}
1696
1697/* fan speed */
1698/* FAN0 information (reverse engineered from ACPI tables) */
1699#define SONY_PIC_FAN0_STATUS 0x93
malattia@linux.it7b153f32007-04-09 19:31:25 +02001700static int sony_pic_set_fanspeed(unsigned long value)
1701{
1702 return ec_write(SONY_PIC_FAN0_STATUS, value);
1703}
1704
1705static int sony_pic_get_fanspeed(u8 *value)
1706{
1707 return ec_read(SONY_PIC_FAN0_STATUS, value);
1708}
1709
malattia@linux.it49a11de2007-04-09 19:28:56 +02001710static ssize_t sony_pic_fanspeed_store(struct device *dev,
1711 struct device_attribute *attr,
1712 const char *buffer, size_t count)
1713{
1714 unsigned long value;
1715 if (count > 31)
1716 return -EINVAL;
1717
1718 value = simple_strtoul(buffer, NULL, 10);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001719 if (sony_pic_set_fanspeed(value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001720 return -EIO;
1721
1722 return count;
1723}
1724
1725static ssize_t sony_pic_fanspeed_show(struct device *dev,
1726 struct device_attribute *attr, char *buffer)
1727{
1728 u8 value = 0;
malattia@linux.it7b153f32007-04-09 19:31:25 +02001729 if (sony_pic_get_fanspeed(&value))
malattia@linux.it49a11de2007-04-09 19:28:56 +02001730 return -EIO;
1731
1732 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1733}
1734
1735#define SPIC_ATTR(_name, _mode) \
1736struct device_attribute spic_attr_##_name = __ATTR(_name, \
1737 _mode, sony_pic_## _name ##_show, \
1738 sony_pic_## _name ##_store)
1739
malattia@linux.it49a11de2007-04-09 19:28:56 +02001740static SPIC_ATTR(bluetoothpower, 0644);
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001741static SPIC_ATTR(wwanpower, 0644);
malattia@linux.it49a11de2007-04-09 19:28:56 +02001742static SPIC_ATTR(fanspeed, 0644);
1743
1744static struct attribute *spic_attributes[] = {
malattia@linux.it49a11de2007-04-09 19:28:56 +02001745 &spic_attr_bluetoothpower.attr,
malattia@linux.it9476cdf2007-04-28 23:21:42 +09001746 &spic_attr_wwanpower.attr,
malattia@linux.it49a11de2007-04-09 19:28:56 +02001747 &spic_attr_fanspeed.attr,
1748 NULL
1749};
1750
1751static struct attribute_group spic_attribute_group = {
1752 .attrs = spic_attributes
1753};
1754
malattia@linux.it7b153f32007-04-09 19:31:25 +02001755/******** SONYPI compatibility **********/
Mattia Dongilia64e62a2007-05-01 11:19:53 +09001756#ifdef CONFIG_SONYPI_COMPAT
malattia@linux.it7b153f32007-04-09 19:31:25 +02001757
1758/* battery / brightness / temperature addresses */
1759#define SONYPI_BAT_FLAGS 0x81
1760#define SONYPI_LCD_LIGHT 0x96
1761#define SONYPI_BAT1_PCTRM 0xa0
1762#define SONYPI_BAT1_LEFT 0xa2
1763#define SONYPI_BAT1_MAXRT 0xa4
1764#define SONYPI_BAT2_PCTRM 0xa8
1765#define SONYPI_BAT2_LEFT 0xaa
1766#define SONYPI_BAT2_MAXRT 0xac
1767#define SONYPI_BAT1_MAXTK 0xb0
1768#define SONYPI_BAT1_FULL 0xb2
1769#define SONYPI_BAT2_MAXTK 0xb8
1770#define SONYPI_BAT2_FULL 0xba
1771#define SONYPI_TEMP_STATUS 0xC1
1772
1773struct sonypi_compat_s {
1774 struct fasync_struct *fifo_async;
1775 struct kfifo *fifo;
1776 spinlock_t fifo_lock;
1777 wait_queue_head_t fifo_proc_list;
1778 atomic_t open_count;
1779};
1780static struct sonypi_compat_s sonypi_compat = {
1781 .open_count = ATOMIC_INIT(0),
1782};
1783
1784static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1785{
1786 int retval;
1787
1788 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1789 if (retval < 0)
1790 return retval;
1791 return 0;
1792}
1793
1794static int sonypi_misc_release(struct inode *inode, struct file *file)
1795{
1796 sonypi_misc_fasync(-1, file, 0);
1797 atomic_dec(&sonypi_compat.open_count);
1798 return 0;
1799}
1800
1801static int sonypi_misc_open(struct inode *inode, struct file *file)
1802{
1803 /* Flush input queue on first open */
1804 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1805 kfifo_reset(sonypi_compat.fifo);
1806 return 0;
1807}
1808
1809static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1810 size_t count, loff_t *pos)
1811{
1812 ssize_t ret;
1813 unsigned char c;
1814
1815 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1816 (file->f_flags & O_NONBLOCK))
1817 return -EAGAIN;
1818
1819 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1820 kfifo_len(sonypi_compat.fifo) != 0);
1821 if (ret)
1822 return ret;
1823
1824 while (ret < count &&
1825 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1826 if (put_user(c, buf++))
1827 return -EFAULT;
1828 ret++;
1829 }
1830
1831 if (ret > 0) {
1832 struct inode *inode = file->f_path.dentry->d_inode;
1833 inode->i_atime = current_fs_time(inode->i_sb);
1834 }
1835
1836 return ret;
1837}
1838
1839static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1840{
1841 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1842 if (kfifo_len(sonypi_compat.fifo))
1843 return POLLIN | POLLRDNORM;
1844 return 0;
1845}
1846
1847static int ec_read16(u8 addr, u16 *value)
1848{
1849 u8 val_lb, val_hb;
1850 if (ec_read(addr, &val_lb))
1851 return -1;
1852 if (ec_read(addr + 1, &val_hb))
1853 return -1;
1854 *value = val_lb | (val_hb << 8);
1855 return 0;
1856}
1857
1858static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1859 unsigned int cmd, unsigned long arg)
1860{
1861 int ret = 0;
1862 void __user *argp = (void __user *)arg;
1863 u8 val8;
1864 u16 val16;
1865 int value;
1866
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001867 mutex_lock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001868 switch (cmd) {
1869 case SONYPI_IOCGBRT:
1870 if (sony_backlight_device == NULL) {
1871 ret = -EIO;
1872 break;
1873 }
1874 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1875 ret = -EIO;
1876 break;
1877 }
1878 val8 = ((value & 0xff) - 1) << 5;
1879 if (copy_to_user(argp, &val8, sizeof(val8)))
1880 ret = -EFAULT;
1881 break;
1882 case SONYPI_IOCSBRT:
1883 if (sony_backlight_device == NULL) {
1884 ret = -EIO;
1885 break;
1886 }
1887 if (copy_from_user(&val8, argp, sizeof(val8))) {
1888 ret = -EFAULT;
1889 break;
1890 }
1891 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1892 (val8 >> 5) + 1, NULL)) {
1893 ret = -EIO;
1894 break;
1895 }
1896 /* sync the backlight device status */
1897 sony_backlight_device->props.brightness =
1898 sony_backlight_get_brightness(sony_backlight_device);
1899 break;
1900 case SONYPI_IOCGBAT1CAP:
1901 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1902 ret = -EIO;
1903 break;
1904 }
1905 if (copy_to_user(argp, &val16, sizeof(val16)))
1906 ret = -EFAULT;
1907 break;
1908 case SONYPI_IOCGBAT1REM:
1909 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1910 ret = -EIO;
1911 break;
1912 }
1913 if (copy_to_user(argp, &val16, sizeof(val16)))
1914 ret = -EFAULT;
1915 break;
1916 case SONYPI_IOCGBAT2CAP:
1917 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1918 ret = -EIO;
1919 break;
1920 }
1921 if (copy_to_user(argp, &val16, sizeof(val16)))
1922 ret = -EFAULT;
1923 break;
1924 case SONYPI_IOCGBAT2REM:
1925 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1926 ret = -EIO;
1927 break;
1928 }
1929 if (copy_to_user(argp, &val16, sizeof(val16)))
1930 ret = -EFAULT;
1931 break;
1932 case SONYPI_IOCGBATFLAGS:
1933 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1934 ret = -EIO;
1935 break;
1936 }
1937 val8 &= 0x07;
1938 if (copy_to_user(argp, &val8, sizeof(val8)))
1939 ret = -EFAULT;
1940 break;
1941 case SONYPI_IOCGBLUE:
1942 val8 = spic_dev.bluetooth_power;
1943 if (copy_to_user(argp, &val8, sizeof(val8)))
1944 ret = -EFAULT;
1945 break;
1946 case SONYPI_IOCSBLUE:
1947 if (copy_from_user(&val8, argp, sizeof(val8))) {
1948 ret = -EFAULT;
1949 break;
1950 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001951 __sony_pic_set_bluetoothpower(val8);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001952 break;
1953 /* FAN Controls */
1954 case SONYPI_IOCGFAN:
1955 if (sony_pic_get_fanspeed(&val8)) {
1956 ret = -EIO;
1957 break;
1958 }
1959 if (copy_to_user(argp, &val8, sizeof(val8)))
1960 ret = -EFAULT;
1961 break;
1962 case SONYPI_IOCSFAN:
1963 if (copy_from_user(&val8, argp, sizeof(val8))) {
1964 ret = -EFAULT;
1965 break;
1966 }
1967 if (sony_pic_set_fanspeed(val8))
1968 ret = -EIO;
1969 break;
1970 /* GET Temperature (useful under APM) */
1971 case SONYPI_IOCGTEMP:
1972 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1973 ret = -EIO;
1974 break;
1975 }
1976 if (copy_to_user(argp, &val8, sizeof(val8)))
1977 ret = -EFAULT;
1978 break;
1979 default:
1980 ret = -EINVAL;
1981 }
malattia@linux.it9f9f0762007-04-28 23:19:36 +09001982 mutex_unlock(&spic_dev.lock);
malattia@linux.it7b153f32007-04-09 19:31:25 +02001983 return ret;
1984}
1985
1986static const struct file_operations sonypi_misc_fops = {
1987 .owner = THIS_MODULE,
1988 .read = sonypi_misc_read,
1989 .poll = sonypi_misc_poll,
1990 .open = sonypi_misc_open,
1991 .release = sonypi_misc_release,
1992 .fasync = sonypi_misc_fasync,
1993 .ioctl = sonypi_misc_ioctl,
1994};
1995
1996static struct miscdevice sonypi_misc_device = {
1997 .minor = MISC_DYNAMIC_MINOR,
1998 .name = "sonypi",
1999 .fops = &sonypi_misc_fops,
2000};
2001
2002static void sonypi_compat_report_event(u8 event)
2003{
2004 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
2005 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2006 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2007}
2008
2009static int sonypi_compat_init(void)
2010{
2011 int error;
2012
2013 spin_lock_init(&sonypi_compat.fifo_lock);
2014 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
2015 &sonypi_compat.fifo_lock);
2016 if (IS_ERR(sonypi_compat.fifo)) {
2017 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2018 return PTR_ERR(sonypi_compat.fifo);
2019 }
2020
2021 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002022
2023 if (minor != -1)
2024 sonypi_misc_device.minor = minor;
2025 error = misc_register(&sonypi_misc_device);
2026 if (error) {
2027 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2028 goto err_free_kfifo;
2029 }
2030 if (minor == -1)
malattia@linux.it5f3d2892007-04-28 23:18:45 +09002031 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
malattia@linux.it7b153f32007-04-09 19:31:25 +02002032 sonypi_misc_device.minor);
2033
2034 return 0;
2035
2036err_free_kfifo:
2037 kfifo_free(sonypi_compat.fifo);
2038 return error;
2039}
2040
2041static void sonypi_compat_exit(void)
2042{
2043 misc_deregister(&sonypi_misc_device);
2044 kfifo_free(sonypi_compat.fifo);
2045}
2046#else
2047static int sonypi_compat_init(void) { return 0; }
2048static void sonypi_compat_exit(void) { }
2049static void sonypi_compat_report_event(u8 event) { }
Mattia Dongilia64e62a2007-05-01 11:19:53 +09002050#endif /* CONFIG_SONYPI_COMPAT */
malattia@linux.it7b153f32007-04-09 19:31:25 +02002051
malattia@linux.it1549ee62007-04-09 10:19:08 +02002052/*
malattia@linux.it33a04452007-04-09 19:26:03 +02002053 * ACPI callbacks
malattia@linux.it1549ee62007-04-09 10:19:08 +02002054 */
malattia@linux.it33a04452007-04-09 19:26:03 +02002055static acpi_status
2056sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2057{
2058 u32 i;
2059 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2060
2061 switch (resource->type) {
2062 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
2063 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
2064 return AE_OK;
2065
2066 case ACPI_RESOURCE_TYPE_IRQ:
2067 {
2068 struct acpi_resource_irq *p = &resource->data.irq;
2069 struct sony_pic_irq *interrupt = NULL;
2070 if (!p || !p->interrupt_count) {
2071 /*
2072 * IRQ descriptors may have no IRQ# bits set,
2073 * particularly those those w/ _STA disabled
2074 */
2075 dprintk("Blank IRQ resource\n");
2076 return AE_OK;
2077 }
2078 for (i = 0; i < p->interrupt_count; i++) {
2079 if (!p->interrupts[i]) {
2080 printk(KERN_WARNING DRV_PFX
2081 "Invalid IRQ %d\n",
2082 p->interrupts[i]);
2083 continue;
2084 }
2085 interrupt = kzalloc(sizeof(*interrupt),
2086 GFP_KERNEL);
2087 if (!interrupt)
2088 return AE_ERROR;
2089
malattia@linux.it1a3e3232007-04-28 23:34:10 +09002090 list_add_tail(&interrupt->list, &dev->interrupts);
malattia@linux.it33a04452007-04-09 19:26:03 +02002091 interrupt->irq.triggering = p->triggering;
2092 interrupt->irq.polarity = p->polarity;
2093 interrupt->irq.sharable = p->sharable;
2094 interrupt->irq.interrupt_count = 1;
2095 interrupt->irq.interrupts[0] = p->interrupts[i];
2096 }
2097 return AE_OK;
2098 }
2099 case ACPI_RESOURCE_TYPE_IO:
2100 {
2101 struct acpi_resource_io *io = &resource->data.io;
2102 struct sony_pic_ioport *ioport = NULL;
2103 if (!io) {
2104 dprintk("Blank IO resource\n");
2105 return AE_OK;
2106 }
2107
2108 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2109 if (!ioport)
2110 return AE_ERROR;
2111
malattia@linux.it1a3e3232007-04-28 23:34:10 +09002112 list_add_tail(&ioport->list, &dev->ioports);
malattia@linux.it33a04452007-04-09 19:26:03 +02002113 memcpy(&ioport->io, io, sizeof(*io));
2114 return AE_OK;
2115 }
2116 default:
2117 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2118 resource->type);
2119
2120 case ACPI_RESOURCE_TYPE_END_TAG:
2121 return AE_OK;
2122 }
2123 return AE_CTRL_TERMINATE;
2124}
2125
2126static int sony_pic_possible_resources(struct acpi_device *device)
2127{
2128 int result = 0;
2129 acpi_status status = AE_OK;
2130
2131 if (!device)
2132 return -EINVAL;
2133
2134 /* get device status */
2135 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2136 dprintk("Evaluating _STA\n");
2137 result = acpi_bus_get_status(device);
2138 if (result) {
2139 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2140 goto end;
2141 }
2142
2143 if (!device->status.enabled)
2144 dprintk("Device disabled\n");
2145 else
2146 dprintk("Device enabled\n");
2147
2148 /*
2149 * Query and parse 'method'
2150 */
2151 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2152 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2153 sony_pic_read_possible_resource, &spic_dev);
2154 if (ACPI_FAILURE(status)) {
2155 printk(KERN_WARNING DRV_PFX
2156 "Failure evaluating %s\n",
2157 METHOD_NAME__PRS);
2158 result = -ENODEV;
2159 }
2160end:
2161 return result;
2162}
2163
2164/*
2165 * Disable the spic device by calling its _DIS method
2166 */
2167static int sony_pic_disable(struct acpi_device *device)
2168{
Randy Dunlapf3a740c2007-07-15 23:40:49 -07002169 if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
2170 "_DIS", NULL, NULL)))
malattia@linux.it33a04452007-04-09 19:26:03 +02002171 return -ENXIO;
2172
2173 dprintk("Device disabled\n");
2174 return 0;
2175}
2176
2177
2178/*
2179 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2180 *
2181 * Call _SRS to set current resources
2182 */
2183static int sony_pic_enable(struct acpi_device *device,
2184 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2185{
2186 acpi_status status;
2187 int result = 0;
2188 struct {
2189 struct acpi_resource io_res;
2190 struct acpi_resource irq_res;
2191 struct acpi_resource end;
2192 } *resource;
2193 struct acpi_buffer buffer = { 0, NULL };
2194
2195 if (!ioport || !irq)
2196 return -EINVAL;
2197
2198 /* init acpi_buffer */
2199 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2200 if (!resource)
2201 return -ENOMEM;
2202
2203 buffer.length = sizeof(*resource) + 1;
2204 buffer.pointer = resource;
2205
2206 /* setup io resource */
2207 resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
2208 resource->io_res.length = sizeof(struct acpi_resource);
2209 memcpy(&resource->io_res.data.io, &ioport->io,
2210 sizeof(struct acpi_resource_io));
2211
2212 /* setup irq resource */
2213 resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
2214 resource->irq_res.length = sizeof(struct acpi_resource);
2215 memcpy(&resource->irq_res.data.irq, &irq->irq,
2216 sizeof(struct acpi_resource_irq));
2217 /* we requested a shared irq */
2218 resource->irq_res.data.irq.sharable = ACPI_SHARED;
2219
2220 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
2221
2222 /* Attempt to set the resource */
2223 dprintk("Evaluating _SRS\n");
2224 status = acpi_set_current_resources(device->handle, &buffer);
2225
2226 /* check for total failure */
2227 if (ACPI_FAILURE(status)) {
2228 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
2229 result = -ENODEV;
2230 goto end;
2231 }
2232
2233 /* Necessary device initializations calls (from sonypi) */
2234 sony_pic_call1(0x82);
2235 sony_pic_call2(0x81, 0xff);
2236 sony_pic_call1(compat ? 0x92 : 0x82);
2237
2238end:
2239 kfree(resource);
2240 return result;
2241}
2242
2243/*****************
2244 *
2245 * ISR: some event is available
2246 *
2247 *****************/
2248static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2249{
2250 int i, j;
malattia@linux.it33a04452007-04-09 19:26:03 +02002251 u8 ev = 0;
2252 u8 data_mask = 0;
2253 u8 device_event = 0;
2254
2255 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2256
Mattia Dongili22a17782007-07-16 02:34:39 +09002257 ev = inb_p(dev->cur_ioport->io.minimum);
2258 data_mask = inb_p(dev->cur_ioport->io.minimum + dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002259
Mattia Dongili22a17782007-07-16 02:34:39 +09002260 dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2261 ev, data_mask, dev->cur_ioport->io.minimum, dev->evport_offset);
malattia@linux.it33a04452007-04-09 19:26:03 +02002262
2263 if (ev == 0x00 || ev == 0xff)
2264 return IRQ_HANDLED;
2265
2266 for (i = 0; sony_pic_eventtypes[i].model; i++) {
2267
2268 if (spic_dev.model != sony_pic_eventtypes[i].model)
2269 continue;
2270
2271 if ((data_mask & sony_pic_eventtypes[i].data) !=
2272 sony_pic_eventtypes[i].data)
2273 continue;
2274
2275 if (!(mask & sony_pic_eventtypes[i].mask))
2276 continue;
2277
2278 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2279 if (ev == sony_pic_eventtypes[i].events[j].data) {
2280 device_event =
2281 sony_pic_eventtypes[i].events[j].event;
2282 goto found;
2283 }
2284 }
2285 }
2286 return IRQ_HANDLED;
2287
2288found:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002289 sony_laptop_report_input_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002290 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
malattia@linux.it7b153f32007-04-09 19:31:25 +02002291 sonypi_compat_report_event(device_event);
malattia@linux.it33a04452007-04-09 19:26:03 +02002292
2293 return IRQ_HANDLED;
2294}
2295
2296/*****************
2297 *
2298 * ACPI driver
2299 *
2300 *****************/
2301static int sony_pic_remove(struct acpi_device *device, int type)
2302{
2303 struct sony_pic_ioport *io, *tmp_io;
2304 struct sony_pic_irq *irq, *tmp_irq;
2305
malattia@linux.it7b153f32007-04-09 19:31:25 +02002306 sonypi_compat_exit();
2307
malattia@linux.it33a04452007-04-09 19:26:03 +02002308 if (sony_pic_disable(device)) {
2309 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2310 return -ENXIO;
2311 }
2312
2313 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2314 release_region(spic_dev.cur_ioport->io.minimum,
2315 spic_dev.cur_ioport->io.address_length);
2316
malattia@linux.it1549ee62007-04-09 10:19:08 +02002317 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002318
malattia@linux.it49a11de2007-04-09 19:28:56 +02002319 /* pf attrs */
2320 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2321 sony_pf_remove();
2322
malattia@linux.it33a04452007-04-09 19:26:03 +02002323 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2324 list_del(&io->list);
2325 kfree(io);
2326 }
2327 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2328 list_del(&irq->list);
2329 kfree(irq);
2330 }
2331 spic_dev.cur_ioport = NULL;
2332 spic_dev.cur_irq = NULL;
2333
malattia@linux.itf6119b02007-04-09 19:31:06 +02002334 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
malattia@linux.it33a04452007-04-09 19:26:03 +02002335 return 0;
2336}
2337
2338static int sony_pic_add(struct acpi_device *device)
2339{
2340 int result;
2341 struct sony_pic_ioport *io, *tmp_io;
2342 struct sony_pic_irq *irq, *tmp_irq;
2343
malattia@linux.itf6119b02007-04-09 19:31:06 +02002344 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2345 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
malattia@linux.it33a04452007-04-09 19:26:03 +02002346
2347 spic_dev.acpi_dev = device;
2348 strcpy(acpi_device_class(device), "sony/hotkey");
2349 spic_dev.model = sony_pic_detect_device_type();
malattia@linux.it9f9f0762007-04-28 23:19:36 +09002350 mutex_init(&spic_dev.lock);
malattia@linux.it33a04452007-04-09 19:26:03 +02002351
Mattia Dongili22a17782007-07-16 02:34:39 +09002352 /* model specific characteristics */
2353 switch(spic_dev.model) {
2354 case SONYPI_DEVICE_TYPE1:
2355 spic_dev.evport_offset = SONYPI_TYPE1_OFFSET;
2356 break;
2357 case SONYPI_DEVICE_TYPE3:
2358 spic_dev.evport_offset = SONYPI_TYPE3_OFFSET;
2359 break;
2360 case SONYPI_DEVICE_TYPE2:
2361 default:
2362 spic_dev.evport_offset = SONYPI_TYPE2_OFFSET;
2363 break;
2364 }
2365
malattia@linux.it33a04452007-04-09 19:26:03 +02002366 /* read _PRS resources */
2367 result = sony_pic_possible_resources(device);
2368 if (result) {
2369 printk(KERN_ERR DRV_PFX
2370 "Unabe to read possible resources.\n");
2371 goto err_free_resources;
2372 }
2373
2374 /* setup input devices and helper fifo */
malattia@linux.it1549ee62007-04-09 10:19:08 +02002375 result = sony_laptop_setup_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002376 if (result) {
2377 printk(KERN_ERR DRV_PFX
2378 "Unabe to create input devices.\n");
2379 goto err_free_resources;
2380 }
2381
2382 /* request io port */
2383 list_for_each_entry(io, &spic_dev.ioports, list) {
2384 if (request_region(io->io.minimum, io->io.address_length,
2385 "Sony Programable I/O Device")) {
2386 dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
2387 io->io.minimum, io->io.maximum,
2388 io->io.address_length);
2389 spic_dev.cur_ioport = io;
2390 break;
2391 }
2392 }
2393 if (!spic_dev.cur_ioport) {
2394 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2395 result = -ENODEV;
2396 goto err_remove_input;
2397 }
2398
2399 /* request IRQ */
2400 list_for_each_entry(irq, &spic_dev.interrupts, list) {
2401 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2402 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2403 dprintk("IRQ: %d - triggering: %d - "
2404 "polarity: %d - shr: %d\n",
2405 irq->irq.interrupts[0],
2406 irq->irq.triggering,
2407 irq->irq.polarity,
2408 irq->irq.sharable);
2409 spic_dev.cur_irq = irq;
2410 break;
2411 }
2412 }
2413 if (!spic_dev.cur_irq) {
2414 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2415 result = -ENODEV;
2416 goto err_release_region;
2417 }
2418
2419 /* set resource status _SRS */
2420 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2421 if (result) {
2422 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2423 goto err_free_irq;
2424 }
2425
malattia@linux.it49a11de2007-04-09 19:28:56 +02002426 spic_dev.bluetooth_power = -1;
2427 /* create device attributes */
2428 result = sony_pf_add();
2429 if (result)
2430 goto err_disable_device;
2431
2432 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2433 if (result)
2434 goto err_remove_pf;
2435
malattia@linux.it7b153f32007-04-09 19:31:25 +02002436 if (sonypi_compat_init())
2437 goto err_remove_pf;
2438
malattia@linux.it33a04452007-04-09 19:26:03 +02002439 return 0;
2440
malattia@linux.it49a11de2007-04-09 19:28:56 +02002441err_remove_pf:
2442 sony_pf_remove();
2443
2444err_disable_device:
2445 sony_pic_disable(device);
2446
malattia@linux.it33a04452007-04-09 19:26:03 +02002447err_free_irq:
2448 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2449
2450err_release_region:
2451 release_region(spic_dev.cur_ioport->io.minimum,
2452 spic_dev.cur_ioport->io.address_length);
2453
2454err_remove_input:
malattia@linux.it1549ee62007-04-09 10:19:08 +02002455 sony_laptop_remove_input();
malattia@linux.it33a04452007-04-09 19:26:03 +02002456
2457err_free_resources:
2458 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2459 list_del(&io->list);
2460 kfree(io);
2461 }
2462 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2463 list_del(&irq->list);
2464 kfree(irq);
2465 }
2466 spic_dev.cur_ioport = NULL;
2467 spic_dev.cur_irq = NULL;
2468
2469 return result;
2470}
2471
2472static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2473{
2474 if (sony_pic_disable(device))
2475 return -ENXIO;
2476 return 0;
2477}
2478
2479static int sony_pic_resume(struct acpi_device *device)
2480{
2481 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2482 return 0;
2483}
2484
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002485static const struct acpi_device_id sony_pic_device_ids[] = {
2486 {SONY_PIC_HID, 0},
2487 {"", 0},
2488};
2489
malattia@linux.it33a04452007-04-09 19:26:03 +02002490static struct acpi_driver sony_pic_driver = {
2491 .name = SONY_PIC_DRIVER_NAME,
2492 .class = SONY_PIC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002493 .ids = sony_pic_device_ids,
malattia@linux.it33a04452007-04-09 19:26:03 +02002494 .owner = THIS_MODULE,
2495 .ops = {
2496 .add = sony_pic_add,
2497 .remove = sony_pic_remove,
2498 .suspend = sony_pic_suspend,
2499 .resume = sony_pic_resume,
2500 },
2501};
2502
2503static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2504 {
2505 .ident = "Sony Vaio",
2506 .matches = {
2507 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2508 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2509 },
2510 },
2511 {
2512 .ident = "Sony Vaio",
2513 .matches = {
2514 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2515 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2516 },
2517 },
2518 { }
2519};
2520
malattia@linux.it59b19102007-04-09 10:19:04 +02002521static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002522{
malattia@linux.it33a04452007-04-09 19:26:03 +02002523 int result;
2524
2525 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2526 result = acpi_bus_register_driver(&sony_pic_driver);
2527 if (result) {
2528 printk(KERN_ERR DRV_PFX
2529 "Unable to register SPIC driver.");
2530 goto out;
2531 }
2532 }
2533
2534 result = acpi_bus_register_driver(&sony_nc_driver);
2535 if (result) {
2536 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2537 goto out_unregister_pic;
2538 }
2539
2540 return 0;
2541
2542out_unregister_pic:
2543 if (!no_spic)
2544 acpi_bus_unregister_driver(&sony_pic_driver);
2545out:
2546 return result;
Stelian Pop7f09c432007-01-13 23:04:31 +01002547}
2548
malattia@linux.it59b19102007-04-09 10:19:04 +02002549static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +01002550{
malattia@linux.it59b19102007-04-09 10:19:04 +02002551 acpi_bus_unregister_driver(&sony_nc_driver);
malattia@linux.it33a04452007-04-09 19:26:03 +02002552 if (!no_spic)
2553 acpi_bus_unregister_driver(&sony_pic_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +01002554}
2555
malattia@linux.it59b19102007-04-09 10:19:04 +02002556module_init(sony_laptop_init);
2557module_exit(sony_laptop_exit);