Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[linux-2.6] / drivers / misc / sony-laptop.c
1 /*
2  * ACPI Sony Notebook Control Driver (SNC and SPIC)
3  *
4  * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5  * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
6  *
7  * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8  * which are copyrighted by their respective authors.
9  *
10  * 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  *
29  * This program is free software; you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation; either version 2 of the License, or
32  * (at your option) any later version.
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>
50 #include <linux/backlight.h>
51 #include <linux/platform_device.h>
52 #include <linux/err.h>
53 #include <linux/dmi.h>
54 #include <linux/pci.h>
55 #include <linux/interrupt.h>
56 #include <linux/delay.h>
57 #include <linux/input.h>
58 #include <linux/kfifo.h>
59 #include <linux/workqueue.h>
60 #include <linux/acpi.h>
61 #include <acpi/acpi_drivers.h>
62 #include <acpi/acpi_bus.h>
63 #include <asm/uaccess.h>
64 #include <linux/sonypi.h>
65 #include <linux/sony-laptop.h>
66 #ifdef CONFIG_SONYPI_COMPAT
67 #include <linux/poll.h>
68 #include <linux/miscdevice.h>
69 #endif
70
71 #define DRV_PFX                 "sony-laptop: "
72 #define dprintk(msg...)         do {                    \
73         if (debug) printk(KERN_WARNING DRV_PFX  msg);   \
74 } while (0)
75
76 #define SONY_LAPTOP_DRIVER_VERSION      "0.5"
77
78 #define SONY_NC_CLASS           "sony-nc"
79 #define SONY_NC_HID             "SNY5001"
80 #define SONY_NC_DRIVER_NAME     "Sony Notebook Control Driver"
81
82 #define SONY_PIC_CLASS          "sony-pic"
83 #define SONY_PIC_HID            "SNY6001"
84 #define SONY_PIC_DRIVER_NAME    "Sony Programmable IO Control Driver"
85
86 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
87 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
90
91 static int debug;
92 module_param(debug, int, 0);
93 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
94                  "the development of this driver");
95
96 static int no_spic;             /* = 0 */
97 module_param(no_spic, int, 0444);
98 MODULE_PARM_DESC(no_spic,
99                  "set this if you don't want to enable the SPIC device");
100
101 static int compat;              /* = 0 */
102 module_param(compat, int, 0444);
103 MODULE_PARM_DESC(compat,
104                  "set this if you want to enable backward compatibility mode");
105
106 static unsigned long mask = 0xffffffff;
107 module_param(mask, ulong, 0644);
108 MODULE_PARM_DESC(mask,
109                  "set this to the mask of event you want to enable (see doc)");
110
111 static int camera;              /* = 0 */
112 module_param(camera, int, 0444);
113 MODULE_PARM_DESC(camera,
114                  "set this to 1 to enable Motion Eye camera controls "
115                  "(only use it if you have a C1VE or C1VN model)");
116
117 #ifdef CONFIG_SONYPI_COMPAT
118 static int minor = -1;
119 module_param(minor, int, 0);
120 MODULE_PARM_DESC(minor,
121                  "minor number of the misc device for the SPIC compatibility code, "
122                  "default is -1 (automatic)");
123 #endif
124
125 /*********** Input Devices ***********/
126
127 #define SONY_LAPTOP_BUF_SIZE    128
128 struct 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 };
136 static struct sony_laptop_input_s sony_laptop_input = {
137         .users = ATOMIC_INIT(0),
138 };
139
140 struct sony_laptop_keypress {
141         struct input_dev *dev;
142         int key;
143 };
144
145 /* Correspondance table between sonypi events
146  * and input layer indexes in the keymap
147  */
148 static 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
213 static int sony_laptop_input_keycode_map[] = {
214         KEY_CAMERA,     /*  0 SONYPI_EVENT_CAPTURE_PRESSED */
215         KEY_RESERVED,   /*  1 SONYPI_EVENT_CAPTURE_RELEASED */
216         KEY_RESERVED,   /*  2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
217         KEY_RESERVED,   /*  3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
218         KEY_FN_ESC,     /*  4 SONYPI_EVENT_FNKEY_ESC */
219         KEY_FN_F1,      /*  5 SONYPI_EVENT_FNKEY_F1 */
220         KEY_FN_F2,      /*  6 SONYPI_EVENT_FNKEY_F2 */
221         KEY_FN_F3,      /*  7 SONYPI_EVENT_FNKEY_F3 */
222         KEY_FN_F4,      /*  8 SONYPI_EVENT_FNKEY_F4 */
223         KEY_FN_F5,      /*  9 SONYPI_EVENT_FNKEY_F5 */
224         KEY_FN_F6,      /* 10 SONYPI_EVENT_FNKEY_F6 */
225         KEY_FN_F7,      /* 11 SONYPI_EVENT_FNKEY_F7 */
226         KEY_FN_F8,      /* 12 SONYPI_EVENT_FNKEY_F8 */
227         KEY_FN_F9,      /* 13 SONYPI_EVENT_FNKEY_F9 */
228         KEY_FN_F10,     /* 14 SONYPI_EVENT_FNKEY_F10 */
229         KEY_FN_F11,     /* 15 SONYPI_EVENT_FNKEY_F11 */
230         KEY_FN_F12,     /* 16 SONYPI_EVENT_FNKEY_F12 */
231         KEY_FN_F1,      /* 17 SONYPI_EVENT_FNKEY_1 */
232         KEY_FN_F2,      /* 18 SONYPI_EVENT_FNKEY_2 */
233         KEY_FN_D,       /* 19 SONYPI_EVENT_FNKEY_D */
234         KEY_FN_E,       /* 20 SONYPI_EVENT_FNKEY_E */
235         KEY_FN_F,       /* 21 SONYPI_EVENT_FNKEY_F */
236         KEY_FN_S,       /* 22 SONYPI_EVENT_FNKEY_S */
237         KEY_FN_B,       /* 23 SONYPI_EVENT_FNKEY_B */
238         KEY_BLUETOOTH,  /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
239         KEY_PROG1,      /* 25 SONYPI_EVENT_PKEY_P1 */
240         KEY_PROG2,      /* 26 SONYPI_EVENT_PKEY_P2 */
241         KEY_PROG3,      /* 27 SONYPI_EVENT_PKEY_P3 */
242         KEY_BACK,       /* 28 SONYPI_EVENT_BACK_PRESSED */
243         KEY_BLUETOOTH,  /* 29 SONYPI_EVENT_BLUETOOTH_ON */
244         KEY_BLUETOOTH,  /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
245         KEY_HELP,       /* 31 SONYPI_EVENT_HELP_PRESSED */
246         KEY_FN,         /* 32 SONYPI_EVENT_FNKEY_ONLY */
247         KEY_RESERVED,   /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
248         KEY_RESERVED,   /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
249         KEY_RESERVED,   /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
250         KEY_RESERVED,   /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
251         KEY_RESERVED,   /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
252         KEY_RESERVED,   /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
253         KEY_RESERVED,   /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
254         KEY_RESERVED,   /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
255         KEY_ZOOM,       /* 41 SONYPI_EVENT_ZOOM_PRESSED */
256         BTN_THUMB,      /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
257         KEY_RESERVED,   /* 43 SONYPI_EVENT_MEYE_FACE */
258         KEY_RESERVED,   /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
259         KEY_RESERVED,   /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
260         KEY_RESERVED,   /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
261         KEY_WLAN,       /* 47 SONYPI_EVENT_WIRELESS_ON */
262         KEY_WLAN,       /* 48 SONYPI_EVENT_WIRELESS_OFF */
263 };
264
265 /* release buttons after a short delay if pressed */
266 static 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 }
277 static DECLARE_WORK(sony_laptop_release_key_work,
278                 do_sony_laptop_release_key);
279
280 /* forward event to the input subsytem */
281 static 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 };
286
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:
314                 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)
321                                 kp.dev = key_dev;
322                 }
323                 break;
324         }
325
326         if (kp.dev) {
327                 input_report_key(kp.dev, kp.key, 1);
328                 /* we emit the scancode so we can always remap the key */
329                 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
330                 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
341 static 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 */
384         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         }
396
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
426 err_free_jogdev:
427         input_free_device(jog_dev);
428
429 err_unregister_keydev:
430         input_unregister_device(key_dev);
431         /* to avoid kref underflow below at input_free_device */
432         key_dev = NULL;
433
434 err_free_keydev:
435         input_free_device(key_dev);
436
437 err_destroy_wq:
438         destroy_workqueue(sony_laptop_input.wq);
439
440 err_free_kfifo:
441         kfifo_free(sony_laptop_input.fifo);
442
443 err_dec_users:
444         atomic_dec(&sony_laptop_input.users);
445         return error;
446 }
447
448 static 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
470 /*********** Platform Device ***********/
471
472 static atomic_t sony_pf_users = ATOMIC_INIT(0);
473 static struct platform_driver sony_pf_driver = {
474         .driver = {
475                    .name = "sony-laptop",
476                    .owner = THIS_MODULE,
477                    }
478 };
479 static struct platform_device *sony_pf_device;
480
481 static 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
515 static 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
528 /* 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
535 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
536                               char *);
537 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
538                                const char *, size_t);
539 static int boolean_validate(const int, const int);
540 static int brightness_default_validate(const int, const int);
541
542 struct sony_nc_value {
543         char *name;             /* name of the entry */
544         char **acpiget;         /* names of the ACPI get function */
545         char **acpiset;         /* names of the ACPI set function */
546         int (*validate)(const int, const int);  /* input/output validation */
547         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 */
551 };
552
553 #define SNC_HANDLE_NAMES(_name, _values...) \
554         static char *snc_##_name[] = { _values, NULL }
555
556 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
557         { \
558                 .name           = __stringify(_name), \
559                 .acpiget        = _getters, \
560                 .acpiset        = _setters, \
561                 .validate       = _validate, \
562                 .debug          = _debug, \
563                 .devattr        = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
564         }
565
566 #define SNC_HANDLE_NULL { .name = NULL }
567
568 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
569
570 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
571 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
572
573 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
574 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
575
576 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
577 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
578
579 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
580 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
581
582 SNC_HANDLE_NAMES(lidstate_get, "GLID");
583
584 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
585 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
586
587 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
588 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
589
590 SNC_HANDLE_NAMES(PID_get, "GPID");
591
592 SNC_HANDLE_NAMES(CTR_get, "GCTR");
593 SNC_HANDLE_NAMES(CTR_set, "SCTR");
594
595 SNC_HANDLE_NAMES(PCR_get, "GPCR");
596 SNC_HANDLE_NAMES(PCR_set, "SPCR");
597
598 SNC_HANDLE_NAMES(CMI_get, "GCMI");
599 SNC_HANDLE_NAMES(CMI_set, "SCMI");
600
601 static struct sony_nc_value sony_nc_values[] = {
602         SNC_HANDLE(brightness_default, snc_brightness_def_get,
603                         snc_brightness_def_set, brightness_default_validate, 0),
604         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,
607                         boolean_validate, 0),
608         SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
609                         boolean_validate, 1),
610         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),
616         /* unknown methods */
617         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
622 };
623
624 static acpi_handle sony_nc_acpi_handle;
625 static struct acpi_device *sony_nc_acpi_device = NULL;
626
627 /*
628  * acpi_evaluate_object wrappers
629  */
630 static 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
645         printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
646
647         return -1;
648 }
649
650 static 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) {
671                                 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
672                                        "return type\n");
673                                 return -1;
674                         }
675                         *result = out_obj.integer.value;
676                 }
677                 return 0;
678         }
679
680         printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
681
682         return -1;
683 }
684
685 /*
686  * sony_nc_values input/output validate functions
687  */
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  */
694 static 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  */
711 static 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 /*
721  * Sysfs show/store common to all sony_nc_values
722  */
723 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
724                               char *buffer)
725 {
726         int value;
727         struct sony_nc_value *item =
728             container_of(attr, struct sony_nc_value, devattr);
729
730         if (!*item->acpiget)
731                 return -EIO;
732
733         if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
734                 return -EIO;
735
736         if (item->validate)
737                 value = item->validate(SNC_VALIDATE_OUT, value);
738
739         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
740 }
741
742 static ssize_t sony_nc_sysfs_store(struct device *dev,
743                                struct device_attribute *attr,
744                                const char *buffer, size_t count)
745 {
746         int value;
747         struct sony_nc_value *item =
748             container_of(attr, struct sony_nc_value, devattr);
749
750         if (!item->acpiset)
751                 return -EIO;
752
753         if (count > 31)
754                 return -EINVAL;
755
756         value = simple_strtoul(buffer, NULL, 10);
757
758         if (item->validate)
759                 value = item->validate(SNC_VALIDATE_IN, value);
760
761         if (value < 0)
762                 return value;
763
764         if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
765                 return -EIO;
766         item->value = value;
767         item->valid = 1;
768         return count;
769 }
770
771
772 /*
773  * Backlight device
774  */
775 static int sony_backlight_update_status(struct backlight_device *bd)
776 {
777         return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
778                                 bd->props.brightness + 1, NULL);
779 }
780
781 static int sony_backlight_get_brightness(struct backlight_device *bd)
782 {
783         int value;
784
785         if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
786                 return 0;
787         /* brightness levels are 1-based, while backlight ones are 0-based */
788         return value - 1;
789 }
790
791 static struct backlight_device *sony_backlight_device;
792 static struct backlight_ops sony_backlight_ops = {
793         .update_status = sony_backlight_update_status,
794         .get_brightness = sony_backlight_get_brightness,
795 };
796
797 /*
798  * New SNC-only Vaios event mapping to driver known keys
799  */
800 struct sony_nc_event {
801         u8      data;
802         u8      event;
803 };
804
805 static 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  */
810 static 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
831 static 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 */
848 struct dmi_system_id sony_nc_ids[] = {
849                 {
850                         .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                 {
859                         .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 /*
871  * ACPI callbacks
872  */
873 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
874 {
875         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);
908 }
909
910 static 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
916         node = (struct acpi_namespace_node *)handle;
917         operand = (union acpi_operand_object *)node->object;
918
919         printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
920                (u32) operand->method.param_count);
921
922         return AE_OK;
923 }
924
925 /*
926  * ACPI device
927  */
928 static int sony_nc_resume(struct acpi_device *device)
929 {
930         struct sony_nc_value *item;
931
932         for (item = sony_nc_values; item->name; item++) {
933                 int ret;
934
935                 if (!item->valid)
936                         continue;
937                 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
938                                        item->value, NULL);
939                 if (ret < 0) {
940                         printk("%s: %d\n", __FUNCTION__, ret);
941                         break;
942                 }
943         }
944
945         /* re-initialize models with specific requirements */
946         dmi_check_system(sony_nc_ids);
947
948         return 0;
949 }
950
951 static int sony_nc_add(struct acpi_device *device)
952 {
953         acpi_status status;
954         int result = 0;
955         acpi_handle handle;
956         struct sony_nc_value *item;
957
958         printk(KERN_INFO DRV_PFX "%s v%s.\n",
959                 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
960
961         sony_nc_acpi_device = device;
962         strcpy(acpi_device_class(device), "sony/hotkey");
963
964         sony_nc_acpi_handle = device->handle;
965
966         /* 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
975         if (debug) {
976                 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
977                                              1, sony_walk_callback, NULL, NULL);
978                 if (ACPI_FAILURE(status)) {
979                         printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
980                         result = -ENODEV;
981                         goto outwalk;
982                 }
983         }
984
985         /* 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
994         /* 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
1002         status = acpi_install_notify_handler(sony_nc_acpi_handle,
1003                                              ACPI_DEVICE_NOTIFY,
1004                                              sony_acpi_notify, NULL);
1005         if (ACPI_FAILURE(status)) {
1006                 printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
1007                 result = -ENODEV;
1008                 goto outinput;
1009         }
1010
1011         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
1012                 sony_backlight_device = backlight_device_register("sony", NULL,
1013                                                                   NULL,
1014                                                                   &sony_backlight_ops);
1015
1016                 if (IS_ERR(sony_backlight_device)) {
1017                         printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
1018                         sony_backlight_device = NULL;
1019                 } else {
1020                         sony_backlight_device->props.brightness =
1021                             sony_backlight_get_brightness
1022                             (sony_backlight_device);
1023                         sony_backlight_device->props.max_brightness = 
1024                             SONY_MAX_BRIGHTNESS - 1;
1025                 }
1026
1027         }
1028
1029         /* initialize models with specific requirements */
1030         dmi_check_system(sony_nc_ids);
1031
1032         result = sony_pf_add();
1033         if (result)
1034                 goto outbacklight;
1035
1036         /* 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))) {
1047                                 dprintk("Found %s getter: %s\n",
1048                                                 item->name, *item->acpiget);
1049                                 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))) {
1059                                 dprintk("Found %s setter: %s\n",
1060                                                 item->name, *item->acpiset);
1061                                 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
1075         return 0;
1076
1077       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();
1082
1083       outbacklight:
1084         if (sony_backlight_device)
1085                 backlight_device_unregister(sony_backlight_device);
1086
1087         status = acpi_remove_notify_handler(sony_nc_acpi_handle,
1088                                             ACPI_DEVICE_NOTIFY,
1089                                             sony_acpi_notify);
1090         if (ACPI_FAILURE(status))
1091                 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
1092
1093       outinput:
1094         sony_laptop_remove_input();
1095
1096       outwalk:
1097         return result;
1098 }
1099
1100 static int sony_nc_remove(struct acpi_device *device, int type)
1101 {
1102         acpi_status status;
1103         struct sony_nc_value *item;
1104
1105         if (sony_backlight_device)
1106                 backlight_device_unregister(sony_backlight_device);
1107
1108         sony_nc_acpi_device = NULL;
1109
1110         status = acpi_remove_notify_handler(sony_nc_acpi_handle,
1111                                             ACPI_DEVICE_NOTIFY,
1112                                             sony_acpi_notify);
1113         if (ACPI_FAILURE(status))
1114                 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
1115
1116         for (item = sony_nc_values; item->name; ++item) {
1117                 device_remove_file(&sony_pf_device->dev, &item->devattr);
1118         }
1119
1120         sony_pf_remove();
1121         sony_laptop_remove_input();
1122         dprintk(SONY_NC_DRIVER_NAME " removed.\n");
1123
1124         return 0;
1125 }
1126
1127 static struct acpi_driver sony_nc_driver = {
1128         .name = SONY_NC_DRIVER_NAME,
1129         .class = SONY_NC_CLASS,
1130         .ids = SONY_NC_HID,
1131         .owner = THIS_MODULE,
1132         .ops = {
1133                 .add = sony_nc_add,
1134                 .remove = sony_nc_remove,
1135                 .resume = sony_nc_resume,
1136                 },
1137 };
1138
1139 /*********** SPIC (SNY6001) Device ***********/
1140
1141 #define SONYPI_DEVICE_TYPE1     0x00000001
1142 #define SONYPI_DEVICE_TYPE2     0x00000002
1143 #define SONYPI_DEVICE_TYPE3     0x00000004
1144
1145 #define SONYPI_TYPE1_OFFSET     0x04
1146 #define SONYPI_TYPE2_OFFSET     0x12
1147 #define SONYPI_TYPE3_OFFSET     0x12
1148
1149 struct sony_pic_ioport {
1150         struct acpi_resource_io io;
1151         struct list_head        list;
1152 };
1153
1154 struct sony_pic_irq {
1155         struct acpi_resource_irq        irq;
1156         struct list_head                list;
1157 };
1158
1159 struct sony_pic_dev {
1160         int                     model;
1161         u16                     evport_offset;
1162         u8                      camera_power;
1163         u8                      bluetooth_power;
1164         u8                      wwan_power;
1165         struct acpi_device      *acpi_dev;
1166         struct sony_pic_irq     *cur_irq;
1167         struct sony_pic_ioport  *cur_ioport;
1168         struct list_head        interrupts;
1169         struct list_head        ioports;
1170         struct mutex            lock;
1171 };
1172
1173 static struct sony_pic_dev spic_dev = {
1174         .interrupts     = LIST_HEAD_INIT(spic_dev.interrupts),
1175         .ioports        = LIST_HEAD_INIT(spic_dev.ioports),
1176 };
1177
1178 /* Event masks */
1179 #define SONYPI_JOGGER_MASK                      0x00000001
1180 #define SONYPI_CAPTURE_MASK                     0x00000002
1181 #define SONYPI_FNKEY_MASK                       0x00000004
1182 #define SONYPI_BLUETOOTH_MASK                   0x00000008
1183 #define SONYPI_PKEY_MASK                        0x00000010
1184 #define SONYPI_BACK_MASK                        0x00000020
1185 #define SONYPI_HELP_MASK                        0x00000040
1186 #define SONYPI_LID_MASK                         0x00000080
1187 #define SONYPI_ZOOM_MASK                        0x00000100
1188 #define SONYPI_THUMBPHRASE_MASK                 0x00000200
1189 #define SONYPI_MEYE_MASK                        0x00000400
1190 #define SONYPI_MEMORYSTICK_MASK                 0x00000800
1191 #define SONYPI_BATTERY_MASK                     0x00001000
1192 #define SONYPI_WIRELESS_MASK                    0x00002000
1193
1194 struct sonypi_event {
1195         u8      data;
1196         u8      event;
1197 };
1198
1199 /* The set of possible button release events */
1200 static struct sonypi_event sonypi_releaseev[] = {
1201         { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1202         { 0, 0 }
1203 };
1204
1205 /* The set of possible jogger events  */
1206 static struct sonypi_event sonypi_joggerev[] = {
1207         { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1208         { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1209         { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1210         { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1211         { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1212         { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1213         { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1214         { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1215         { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1216         { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1217         { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1218         { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1219         { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1220         { 0, 0 }
1221 };
1222
1223 /* The set of possible capture button events */
1224 static struct sonypi_event sonypi_captureev[] = {
1225         { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1226         { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1227         { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1228         { 0, 0 }
1229 };
1230
1231 /* The set of possible fnkeys events */
1232 static struct sonypi_event sonypi_fnkeyev[] = {
1233         { 0x10, SONYPI_EVENT_FNKEY_ESC },
1234         { 0x11, SONYPI_EVENT_FNKEY_F1 },
1235         { 0x12, SONYPI_EVENT_FNKEY_F2 },
1236         { 0x13, SONYPI_EVENT_FNKEY_F3 },
1237         { 0x14, SONYPI_EVENT_FNKEY_F4 },
1238         { 0x15, SONYPI_EVENT_FNKEY_F5 },
1239         { 0x16, SONYPI_EVENT_FNKEY_F6 },
1240         { 0x17, SONYPI_EVENT_FNKEY_F7 },
1241         { 0x18, SONYPI_EVENT_FNKEY_F8 },
1242         { 0x19, SONYPI_EVENT_FNKEY_F9 },
1243         { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1244         { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1245         { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1246         { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1247         { 0x21, SONYPI_EVENT_FNKEY_1 },
1248         { 0x22, SONYPI_EVENT_FNKEY_2 },
1249         { 0x31, SONYPI_EVENT_FNKEY_D },
1250         { 0x32, SONYPI_EVENT_FNKEY_E },
1251         { 0x33, SONYPI_EVENT_FNKEY_F },
1252         { 0x34, SONYPI_EVENT_FNKEY_S },
1253         { 0x35, SONYPI_EVENT_FNKEY_B },
1254         { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1255         { 0, 0 }
1256 };
1257
1258 /* The set of possible program key events */
1259 static struct sonypi_event sonypi_pkeyev[] = {
1260         { 0x01, SONYPI_EVENT_PKEY_P1 },
1261         { 0x02, SONYPI_EVENT_PKEY_P2 },
1262         { 0x04, SONYPI_EVENT_PKEY_P3 },
1263         { 0x5c, SONYPI_EVENT_PKEY_P1 },
1264         { 0, 0 }
1265 };
1266
1267 /* The set of possible bluetooth events */
1268 static struct sonypi_event sonypi_blueev[] = {
1269         { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1270         { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1271         { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1272         { 0, 0 }
1273 };
1274
1275 /* The set of possible wireless events */
1276 static struct sonypi_event sonypi_wlessev[] = {
1277         { 0x59, SONYPI_EVENT_WIRELESS_ON },
1278         { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1279         { 0, 0 }
1280 };
1281
1282 /* The set of possible back button events */
1283 static struct sonypi_event sonypi_backev[] = {
1284         { 0x20, SONYPI_EVENT_BACK_PRESSED },
1285         { 0, 0 }
1286 };
1287
1288 /* The set of possible help button events */
1289 static struct sonypi_event sonypi_helpev[] = {
1290         { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1291         { 0, 0 }
1292 };
1293
1294
1295 /* The set of possible lid events */
1296 static struct sonypi_event sonypi_lidev[] = {
1297         { 0x51, SONYPI_EVENT_LID_CLOSED },
1298         { 0x50, SONYPI_EVENT_LID_OPENED },
1299         { 0, 0 }
1300 };
1301
1302 /* The set of possible zoom events */
1303 static struct sonypi_event sonypi_zoomev[] = {
1304         { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1305         { 0, 0 }
1306 };
1307
1308 /* The set of possible thumbphrase events */
1309 static struct sonypi_event sonypi_thumbphraseev[] = {
1310         { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1311         { 0, 0 }
1312 };
1313
1314 /* The set of possible motioneye camera events */
1315 static struct sonypi_event sonypi_meyeev[] = {
1316         { 0x00, SONYPI_EVENT_MEYE_FACE },
1317         { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1318         { 0, 0 }
1319 };
1320
1321 /* The set of possible memorystick events */
1322 static struct sonypi_event sonypi_memorystickev[] = {
1323         { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1324         { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1325         { 0, 0 }
1326 };
1327
1328 /* The set of possible battery events */
1329 static struct sonypi_event sonypi_batteryev[] = {
1330         { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1331         { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1332         { 0, 0 }
1333 };
1334
1335 static struct sonypi_eventtypes {
1336         int                     model;
1337         u8                      data;
1338         unsigned long           mask;
1339         struct sonypi_event *   events;
1340 } sony_pic_eventtypes[] = {
1341         { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1342         { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1343         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1344         { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1345         { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1346         { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1347         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1348         { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1349         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1350         { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1351
1352         { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1353         { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1354         { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1355         { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1356         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1357         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1358         { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1359         { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1360         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1361         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1362         { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1363         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1364         { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1365         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1366
1367         { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1368         { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1369         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1370         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1371         { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1372         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1373         { 0 }
1374 };
1375
1376 static int sony_pic_detect_device_type(void)
1377 {
1378         struct pci_dev *pcidev;
1379         int model = 0;
1380
1381         if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1382                                      PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1383                 model = SONYPI_DEVICE_TYPE1;
1384
1385         else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1386                                           PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1387                 model = SONYPI_DEVICE_TYPE3;
1388
1389         else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1390                                           PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1391                 model = SONYPI_DEVICE_TYPE3;
1392
1393         else
1394                 model = SONYPI_DEVICE_TYPE2;
1395
1396         if (pcidev)
1397                 pci_dev_put(pcidev);
1398
1399         printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1400                         model == SONYPI_DEVICE_TYPE1 ? 1 :
1401                         model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1402         return model;
1403 }
1404
1405 #define ITERATIONS_LONG         10000
1406 #define ITERATIONS_SHORT        10
1407 #define wait_on_command(command, iterations) {                          \
1408         unsigned int n = iterations;                                    \
1409         while (--n && (command))                                        \
1410                 udelay(1);                                              \
1411         if (!n)                                                         \
1412                 dprintk("command failed at %s : %s (line %d)\n",        \
1413                                 __FILE__, __FUNCTION__, __LINE__);      \
1414 }
1415
1416 static u8 sony_pic_call1(u8 dev)
1417 {
1418         u8 v1, v2;
1419
1420         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1421                         ITERATIONS_LONG);
1422         outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1423         v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1424         v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1425         dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1426         return v2;
1427 }
1428
1429 static u8 sony_pic_call2(u8 dev, u8 fn)
1430 {
1431         u8 v1;
1432
1433         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1434                         ITERATIONS_LONG);
1435         outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1436         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1437                         ITERATIONS_LONG);
1438         outb(fn, spic_dev.cur_ioport->io.minimum);
1439         v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1440         dprintk("sony_pic_call2: 0x%.4x\n", v1);
1441         return v1;
1442 }
1443
1444 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1445 {
1446         u8 v1;
1447
1448         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1449         outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1450         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1451         outb(fn, spic_dev.cur_ioport->io.minimum);
1452         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1453         outb(v, spic_dev.cur_ioport->io.minimum);
1454         v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1455         dprintk("sony_pic_call3: 0x%.4x\n", v1);
1456         return v1;
1457 }
1458
1459 /* camera tests and poweron/poweroff */
1460 #define SONYPI_CAMERA_PICTURE           5
1461 #define SONYPI_CAMERA_CONTROL           0x10
1462
1463 #define SONYPI_CAMERA_BRIGHTNESS                0
1464 #define SONYPI_CAMERA_CONTRAST                  1
1465 #define SONYPI_CAMERA_HUE                       2
1466 #define SONYPI_CAMERA_COLOR                     3
1467 #define SONYPI_CAMERA_SHARPNESS                 4
1468
1469 #define SONYPI_CAMERA_EXPOSURE_MASK             0xC
1470 #define SONYPI_CAMERA_WHITE_BALANCE_MASK        0x3
1471 #define SONYPI_CAMERA_PICTURE_MODE_MASK         0x30
1472 #define SONYPI_CAMERA_MUTE_MASK                 0x40
1473
1474 /* the rest don't need a loop until not 0xff */
1475 #define SONYPI_CAMERA_AGC                       6
1476 #define SONYPI_CAMERA_AGC_MASK                  0x30
1477 #define SONYPI_CAMERA_SHUTTER_MASK              0x7
1478
1479 #define SONYPI_CAMERA_SHUTDOWN_REQUEST          7
1480 #define SONYPI_CAMERA_CONTROL                   0x10
1481
1482 #define SONYPI_CAMERA_STATUS                    7
1483 #define SONYPI_CAMERA_STATUS_READY              0x2
1484 #define SONYPI_CAMERA_STATUS_POSITION           0x4
1485
1486 #define SONYPI_DIRECTION_BACKWARDS              0x4
1487
1488 #define SONYPI_CAMERA_REVISION                  8
1489 #define SONYPI_CAMERA_ROMVERSION                9
1490
1491 static int __sony_pic_camera_ready(void)
1492 {
1493         u8 v;
1494
1495         v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1496         return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1497 }
1498
1499 static int __sony_pic_camera_off(void)
1500 {
1501         if (!camera) {
1502                 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1503                 return -ENODEV;
1504         }
1505
1506         wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1507                                 SONYPI_CAMERA_MUTE_MASK),
1508                         ITERATIONS_SHORT);
1509
1510         if (spic_dev.camera_power) {
1511                 sony_pic_call2(0x91, 0);
1512                 spic_dev.camera_power = 0;
1513         }
1514         return 0;
1515 }
1516
1517 static int __sony_pic_camera_on(void)
1518 {
1519         int i, j, x;
1520
1521         if (!camera) {
1522                 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1523                 return -ENODEV;
1524         }
1525
1526         if (spic_dev.camera_power)
1527                 return 0;
1528
1529         for (j = 5; j > 0; j--) {
1530
1531                 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1532                         msleep(10);
1533                 sony_pic_call1(0x93);
1534
1535                 for (i = 400; i > 0; i--) {
1536                         if (__sony_pic_camera_ready())
1537                                 break;
1538                         msleep(10);
1539                 }
1540                 if (i)
1541                         break;
1542         }
1543
1544         if (j == 0) {
1545                 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
1546                 return -ENODEV;
1547         }
1548
1549         wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1550                                 0x5a),
1551                         ITERATIONS_SHORT);
1552
1553         spic_dev.camera_power = 1;
1554         return 0;
1555 }
1556
1557 /* External camera command (exported to the motion eye v4l driver) */
1558 int sony_pic_camera_command(int command, u8 value)
1559 {
1560         if (!camera)
1561                 return -EIO;
1562
1563         mutex_lock(&spic_dev.lock);
1564
1565         switch (command) {
1566         case SONY_PIC_COMMAND_SETCAMERA:
1567                 if (value)
1568                         __sony_pic_camera_on();
1569                 else
1570                         __sony_pic_camera_off();
1571                 break;
1572         case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
1573                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1574                                 ITERATIONS_SHORT);
1575                 break;
1576         case SONY_PIC_COMMAND_SETCAMERACONTRAST:
1577                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1578                                 ITERATIONS_SHORT);
1579                 break;
1580         case SONY_PIC_COMMAND_SETCAMERAHUE:
1581                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1582                                 ITERATIONS_SHORT);
1583                 break;
1584         case SONY_PIC_COMMAND_SETCAMERACOLOR:
1585                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1586                                 ITERATIONS_SHORT);
1587                 break;
1588         case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
1589                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1590                                 ITERATIONS_SHORT);
1591                 break;
1592         case SONY_PIC_COMMAND_SETCAMERAPICTURE:
1593                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1594                                 ITERATIONS_SHORT);
1595                 break;
1596         case SONY_PIC_COMMAND_SETCAMERAAGC:
1597                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1598                                 ITERATIONS_SHORT);
1599                 break;
1600         default:
1601                 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1602                        command);
1603                 break;
1604         }
1605         mutex_unlock(&spic_dev.lock);
1606         return 0;
1607 }
1608 EXPORT_SYMBOL(sony_pic_camera_command);
1609
1610 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1611 static void sony_pic_set_wwanpower(u8 state)
1612 {
1613         state = !!state;
1614         mutex_lock(&spic_dev.lock);
1615         if (spic_dev.wwan_power == state) {
1616                 mutex_unlock(&spic_dev.lock);
1617                 return;
1618         }
1619         sony_pic_call2(0xB0, state);
1620         spic_dev.wwan_power = state;
1621         mutex_unlock(&spic_dev.lock);
1622 }
1623
1624 static ssize_t sony_pic_wwanpower_store(struct device *dev,
1625                 struct device_attribute *attr,
1626                 const char *buffer, size_t count)
1627 {
1628         unsigned long value;
1629         if (count > 31)
1630                 return -EINVAL;
1631
1632         value = simple_strtoul(buffer, NULL, 10);
1633         sony_pic_set_wwanpower(value);
1634
1635         return count;
1636 }
1637
1638 static ssize_t sony_pic_wwanpower_show(struct device *dev,
1639                 struct device_attribute *attr, char *buffer)
1640 {
1641         ssize_t count;
1642         mutex_lock(&spic_dev.lock);
1643         count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1644         mutex_unlock(&spic_dev.lock);
1645         return count;
1646 }
1647
1648 /* bluetooth subsystem power state */
1649 static void __sony_pic_set_bluetoothpower(u8 state)
1650 {
1651         state = !!state;
1652         if (spic_dev.bluetooth_power == state)
1653                 return;
1654         sony_pic_call2(0x96, state);
1655         sony_pic_call1(0x82);
1656         spic_dev.bluetooth_power = state;
1657 }
1658
1659 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1660                 struct device_attribute *attr,
1661                 const char *buffer, size_t count)
1662 {
1663         unsigned long value;
1664         if (count > 31)
1665                 return -EINVAL;
1666
1667         value = simple_strtoul(buffer, NULL, 10);
1668         mutex_lock(&spic_dev.lock);
1669         __sony_pic_set_bluetoothpower(value);
1670         mutex_unlock(&spic_dev.lock);
1671
1672         return count;
1673 }
1674
1675 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1676                 struct device_attribute *attr, char *buffer)
1677 {
1678         ssize_t count = 0;
1679         mutex_lock(&spic_dev.lock);
1680         count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1681         mutex_unlock(&spic_dev.lock);
1682         return count;
1683 }
1684
1685 /* fan speed */
1686 /* FAN0 information (reverse engineered from ACPI tables) */
1687 #define SONY_PIC_FAN0_STATUS    0x93
1688 static int sony_pic_set_fanspeed(unsigned long value)
1689 {
1690         return ec_write(SONY_PIC_FAN0_STATUS, value);
1691 }
1692
1693 static int sony_pic_get_fanspeed(u8 *value)
1694 {
1695         return ec_read(SONY_PIC_FAN0_STATUS, value);
1696 }
1697
1698 static ssize_t sony_pic_fanspeed_store(struct device *dev,
1699                 struct device_attribute *attr,
1700                 const char *buffer, size_t count)
1701 {
1702         unsigned long value;
1703         if (count > 31)
1704                 return -EINVAL;
1705
1706         value = simple_strtoul(buffer, NULL, 10);
1707         if (sony_pic_set_fanspeed(value))
1708                 return -EIO;
1709
1710         return count;
1711 }
1712
1713 static ssize_t sony_pic_fanspeed_show(struct device *dev,
1714                 struct device_attribute *attr, char *buffer)
1715 {
1716         u8 value = 0;
1717         if (sony_pic_get_fanspeed(&value))
1718                 return -EIO;
1719
1720         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1721 }
1722
1723 #define SPIC_ATTR(_name, _mode)                                 \
1724 struct device_attribute spic_attr_##_name = __ATTR(_name,       \
1725                 _mode, sony_pic_## _name ##_show,               \
1726                 sony_pic_## _name ##_store)
1727
1728 static SPIC_ATTR(bluetoothpower, 0644);
1729 static SPIC_ATTR(wwanpower, 0644);
1730 static SPIC_ATTR(fanspeed, 0644);
1731
1732 static struct attribute *spic_attributes[] = {
1733         &spic_attr_bluetoothpower.attr,
1734         &spic_attr_wwanpower.attr,
1735         &spic_attr_fanspeed.attr,
1736         NULL
1737 };
1738
1739 static struct attribute_group spic_attribute_group = {
1740         .attrs = spic_attributes
1741 };
1742
1743 /******** SONYPI compatibility **********/
1744 #ifdef CONFIG_SONYPI_COMPAT
1745
1746 /* battery / brightness / temperature  addresses */
1747 #define SONYPI_BAT_FLAGS        0x81
1748 #define SONYPI_LCD_LIGHT        0x96
1749 #define SONYPI_BAT1_PCTRM       0xa0
1750 #define SONYPI_BAT1_LEFT        0xa2
1751 #define SONYPI_BAT1_MAXRT       0xa4
1752 #define SONYPI_BAT2_PCTRM       0xa8
1753 #define SONYPI_BAT2_LEFT        0xaa
1754 #define SONYPI_BAT2_MAXRT       0xac
1755 #define SONYPI_BAT1_MAXTK       0xb0
1756 #define SONYPI_BAT1_FULL        0xb2
1757 #define SONYPI_BAT2_MAXTK       0xb8
1758 #define SONYPI_BAT2_FULL        0xba
1759 #define SONYPI_TEMP_STATUS      0xC1
1760
1761 struct sonypi_compat_s {
1762         struct fasync_struct    *fifo_async;
1763         struct kfifo            *fifo;
1764         spinlock_t              fifo_lock;
1765         wait_queue_head_t       fifo_proc_list;
1766         atomic_t                open_count;
1767 };
1768 static struct sonypi_compat_s sonypi_compat = {
1769         .open_count = ATOMIC_INIT(0),
1770 };
1771
1772 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1773 {
1774         int retval;
1775
1776         retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1777         if (retval < 0)
1778                 return retval;
1779         return 0;
1780 }
1781
1782 static int sonypi_misc_release(struct inode *inode, struct file *file)
1783 {
1784         sonypi_misc_fasync(-1, file, 0);
1785         atomic_dec(&sonypi_compat.open_count);
1786         return 0;
1787 }
1788
1789 static int sonypi_misc_open(struct inode *inode, struct file *file)
1790 {
1791         /* Flush input queue on first open */
1792         if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1793                 kfifo_reset(sonypi_compat.fifo);
1794         return 0;
1795 }
1796
1797 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1798                                 size_t count, loff_t *pos)
1799 {
1800         ssize_t ret;
1801         unsigned char c;
1802
1803         if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1804             (file->f_flags & O_NONBLOCK))
1805                 return -EAGAIN;
1806
1807         ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1808                                        kfifo_len(sonypi_compat.fifo) != 0);
1809         if (ret)
1810                 return ret;
1811
1812         while (ret < count &&
1813                (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1814                 if (put_user(c, buf++))
1815                         return -EFAULT;
1816                 ret++;
1817         }
1818
1819         if (ret > 0) {
1820                 struct inode *inode = file->f_path.dentry->d_inode;
1821                 inode->i_atime = current_fs_time(inode->i_sb);
1822         }
1823
1824         return ret;
1825 }
1826
1827 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1828 {
1829         poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1830         if (kfifo_len(sonypi_compat.fifo))
1831                 return POLLIN | POLLRDNORM;
1832         return 0;
1833 }
1834
1835 static int ec_read16(u8 addr, u16 *value)
1836 {
1837         u8 val_lb, val_hb;
1838         if (ec_read(addr, &val_lb))
1839                 return -1;
1840         if (ec_read(addr + 1, &val_hb))
1841                 return -1;
1842         *value = val_lb | (val_hb << 8);
1843         return 0;
1844 }
1845
1846 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1847                              unsigned int cmd, unsigned long arg)
1848 {
1849         int ret = 0;
1850         void __user *argp = (void __user *)arg;
1851         u8 val8;
1852         u16 val16;
1853         int value;
1854
1855         mutex_lock(&spic_dev.lock);
1856         switch (cmd) {
1857         case SONYPI_IOCGBRT:
1858                 if (sony_backlight_device == NULL) {
1859                         ret = -EIO;
1860                         break;
1861                 }
1862                 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1863                         ret = -EIO;
1864                         break;
1865                 }
1866                 val8 = ((value & 0xff) - 1) << 5;
1867                 if (copy_to_user(argp, &val8, sizeof(val8)))
1868                                 ret = -EFAULT;
1869                 break;
1870         case SONYPI_IOCSBRT:
1871                 if (sony_backlight_device == NULL) {
1872                         ret = -EIO;
1873                         break;
1874                 }
1875                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1876                         ret = -EFAULT;
1877                         break;
1878                 }
1879                 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1880                                 (val8 >> 5) + 1, NULL)) {
1881                         ret = -EIO;
1882                         break;
1883                 }
1884                 /* sync the backlight device status */
1885                 sony_backlight_device->props.brightness =
1886                     sony_backlight_get_brightness(sony_backlight_device);
1887                 break;
1888         case SONYPI_IOCGBAT1CAP:
1889                 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1890                         ret = -EIO;
1891                         break;
1892                 }
1893                 if (copy_to_user(argp, &val16, sizeof(val16)))
1894                         ret = -EFAULT;
1895                 break;
1896         case SONYPI_IOCGBAT1REM:
1897                 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1898                         ret = -EIO;
1899                         break;
1900                 }
1901                 if (copy_to_user(argp, &val16, sizeof(val16)))
1902                         ret = -EFAULT;
1903                 break;
1904         case SONYPI_IOCGBAT2CAP:
1905                 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1906                         ret = -EIO;
1907                         break;
1908                 }
1909                 if (copy_to_user(argp, &val16, sizeof(val16)))
1910                         ret = -EFAULT;
1911                 break;
1912         case SONYPI_IOCGBAT2REM:
1913                 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1914                         ret = -EIO;
1915                         break;
1916                 }
1917                 if (copy_to_user(argp, &val16, sizeof(val16)))
1918                         ret = -EFAULT;
1919                 break;
1920         case SONYPI_IOCGBATFLAGS:
1921                 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1922                         ret = -EIO;
1923                         break;
1924                 }
1925                 val8 &= 0x07;
1926                 if (copy_to_user(argp, &val8, sizeof(val8)))
1927                         ret = -EFAULT;
1928                 break;
1929         case SONYPI_IOCGBLUE:
1930                 val8 = spic_dev.bluetooth_power;
1931                 if (copy_to_user(argp, &val8, sizeof(val8)))
1932                         ret = -EFAULT;
1933                 break;
1934         case SONYPI_IOCSBLUE:
1935                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1936                         ret = -EFAULT;
1937                         break;
1938                 }
1939                 __sony_pic_set_bluetoothpower(val8);
1940                 break;
1941         /* FAN Controls */
1942         case SONYPI_IOCGFAN:
1943                 if (sony_pic_get_fanspeed(&val8)) {
1944                         ret = -EIO;
1945                         break;
1946                 }
1947                 if (copy_to_user(argp, &val8, sizeof(val8)))
1948                         ret = -EFAULT;
1949                 break;
1950         case SONYPI_IOCSFAN:
1951                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1952                         ret = -EFAULT;
1953                         break;
1954                 }
1955                 if (sony_pic_set_fanspeed(val8))
1956                         ret = -EIO;
1957                 break;
1958         /* GET Temperature (useful under APM) */
1959         case SONYPI_IOCGTEMP:
1960                 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1961                         ret = -EIO;
1962                         break;
1963                 }
1964                 if (copy_to_user(argp, &val8, sizeof(val8)))
1965                         ret = -EFAULT;
1966                 break;
1967         default:
1968                 ret = -EINVAL;
1969         }
1970         mutex_unlock(&spic_dev.lock);
1971         return ret;
1972 }
1973
1974 static const struct file_operations sonypi_misc_fops = {
1975         .owner          = THIS_MODULE,
1976         .read           = sonypi_misc_read,
1977         .poll           = sonypi_misc_poll,
1978         .open           = sonypi_misc_open,
1979         .release        = sonypi_misc_release,
1980         .fasync         = sonypi_misc_fasync,
1981         .ioctl          = sonypi_misc_ioctl,
1982 };
1983
1984 static struct miscdevice sonypi_misc_device = {
1985         .minor          = MISC_DYNAMIC_MINOR,
1986         .name           = "sonypi",
1987         .fops           = &sonypi_misc_fops,
1988 };
1989
1990 static void sonypi_compat_report_event(u8 event)
1991 {
1992         kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
1993         kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
1994         wake_up_interruptible(&sonypi_compat.fifo_proc_list);
1995 }
1996
1997 static int sonypi_compat_init(void)
1998 {
1999         int error;
2000
2001         spin_lock_init(&sonypi_compat.fifo_lock);
2002         sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
2003                                          &sonypi_compat.fifo_lock);
2004         if (IS_ERR(sonypi_compat.fifo)) {
2005                 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2006                 return PTR_ERR(sonypi_compat.fifo);
2007         }
2008
2009         init_waitqueue_head(&sonypi_compat.fifo_proc_list);
2010
2011         if (minor != -1)
2012                 sonypi_misc_device.minor = minor;
2013         error = misc_register(&sonypi_misc_device);
2014         if (error) {
2015                 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2016                 goto err_free_kfifo;
2017         }
2018         if (minor == -1)
2019                 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
2020                        sonypi_misc_device.minor);
2021
2022         return 0;
2023
2024 err_free_kfifo:
2025         kfifo_free(sonypi_compat.fifo);
2026         return error;
2027 }
2028
2029 static void sonypi_compat_exit(void)
2030 {
2031         misc_deregister(&sonypi_misc_device);
2032         kfifo_free(sonypi_compat.fifo);
2033 }
2034 #else
2035 static int sonypi_compat_init(void) { return 0; }
2036 static void sonypi_compat_exit(void) { }
2037 static void sonypi_compat_report_event(u8 event) { }
2038 #endif /* CONFIG_SONYPI_COMPAT */
2039
2040 /*
2041  * ACPI callbacks
2042  */
2043 static acpi_status
2044 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2045 {
2046         u32 i;
2047         struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2048
2049         switch (resource->type) {
2050         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
2051         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
2052                 return AE_OK;
2053
2054         case ACPI_RESOURCE_TYPE_IRQ:
2055                 {
2056                         struct acpi_resource_irq *p = &resource->data.irq;
2057                         struct sony_pic_irq *interrupt = NULL;
2058                         if (!p || !p->interrupt_count) {
2059                                 /*
2060                                  * IRQ descriptors may have no IRQ# bits set,
2061                                  * particularly those those w/ _STA disabled
2062                                  */
2063                                 dprintk("Blank IRQ resource\n");
2064                                 return AE_OK;
2065                         }
2066                         for (i = 0; i < p->interrupt_count; i++) {
2067                                 if (!p->interrupts[i]) {
2068                                         printk(KERN_WARNING DRV_PFX
2069                                                         "Invalid IRQ %d\n",
2070                                                         p->interrupts[i]);
2071                                         continue;
2072                                 }
2073                                 interrupt = kzalloc(sizeof(*interrupt),
2074                                                 GFP_KERNEL);
2075                                 if (!interrupt)
2076                                         return AE_ERROR;
2077
2078                                 list_add_tail(&interrupt->list, &dev->interrupts);
2079                                 interrupt->irq.triggering = p->triggering;
2080                                 interrupt->irq.polarity = p->polarity;
2081                                 interrupt->irq.sharable = p->sharable;
2082                                 interrupt->irq.interrupt_count = 1;
2083                                 interrupt->irq.interrupts[0] = p->interrupts[i];
2084                         }
2085                         return AE_OK;
2086                 }
2087         case ACPI_RESOURCE_TYPE_IO:
2088                 {
2089                         struct acpi_resource_io *io = &resource->data.io;
2090                         struct sony_pic_ioport *ioport = NULL;
2091                         if (!io) {
2092                                 dprintk("Blank IO resource\n");
2093                                 return AE_OK;
2094                         }
2095
2096                         ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2097                         if (!ioport)
2098                                 return AE_ERROR;
2099
2100                         list_add_tail(&ioport->list, &dev->ioports);
2101                         memcpy(&ioport->io, io, sizeof(*io));
2102                         return AE_OK;
2103                 }
2104         default:
2105                 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2106                                 resource->type);
2107
2108         case ACPI_RESOURCE_TYPE_END_TAG:
2109                 return AE_OK;
2110         }
2111         return AE_CTRL_TERMINATE;
2112 }
2113
2114 static int sony_pic_possible_resources(struct acpi_device *device)
2115 {
2116         int result = 0;
2117         acpi_status status = AE_OK;
2118
2119         if (!device)
2120                 return -EINVAL;
2121
2122         /* get device status */
2123         /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2124         dprintk("Evaluating _STA\n");
2125         result = acpi_bus_get_status(device);
2126         if (result) {
2127                 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2128                 goto end;
2129         }
2130
2131         if (!device->status.enabled)
2132                 dprintk("Device disabled\n");
2133         else
2134                 dprintk("Device enabled\n");
2135
2136         /*
2137          * Query and parse 'method'
2138          */
2139         dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2140         status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2141                         sony_pic_read_possible_resource, &spic_dev);
2142         if (ACPI_FAILURE(status)) {
2143                 printk(KERN_WARNING DRV_PFX
2144                                 "Failure evaluating %s\n",
2145                                 METHOD_NAME__PRS);
2146                 result = -ENODEV;
2147         }
2148 end:
2149         return result;
2150 }
2151
2152 /*
2153  *  Disable the spic device by calling its _DIS method
2154  */
2155 static int sony_pic_disable(struct acpi_device *device)
2156 {
2157         if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
2158                         "_DIS", NULL, NULL)))
2159                 return -ENXIO;
2160
2161         dprintk("Device disabled\n");
2162         return 0;
2163 }
2164
2165
2166 /*
2167  *  Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2168  *
2169  *  Call _SRS to set current resources
2170  */
2171 static int sony_pic_enable(struct acpi_device *device,
2172                 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2173 {
2174         acpi_status status;
2175         int result = 0;
2176         struct {
2177                 struct acpi_resource io_res;
2178                 struct acpi_resource irq_res;
2179                 struct acpi_resource end;
2180         } *resource;
2181         struct acpi_buffer buffer = { 0, NULL };
2182
2183         if (!ioport || !irq)
2184                 return -EINVAL;
2185
2186         /* init acpi_buffer */
2187         resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2188         if (!resource)
2189                 return -ENOMEM;
2190
2191         buffer.length = sizeof(*resource) + 1;
2192         buffer.pointer = resource;
2193
2194         /* setup io resource */
2195         resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
2196         resource->io_res.length = sizeof(struct acpi_resource);
2197         memcpy(&resource->io_res.data.io, &ioport->io,
2198                         sizeof(struct acpi_resource_io));
2199
2200         /* setup irq resource */
2201         resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
2202         resource->irq_res.length = sizeof(struct acpi_resource);
2203         memcpy(&resource->irq_res.data.irq, &irq->irq,
2204                         sizeof(struct acpi_resource_irq));
2205         /* we requested a shared irq */
2206         resource->irq_res.data.irq.sharable = ACPI_SHARED;
2207
2208         resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
2209
2210         /* Attempt to set the resource */
2211         dprintk("Evaluating _SRS\n");
2212         status = acpi_set_current_resources(device->handle, &buffer);
2213
2214         /* check for total failure */
2215         if (ACPI_FAILURE(status)) {
2216                 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
2217                 result = -ENODEV;
2218                 goto end;
2219         }
2220
2221         /* Necessary device initializations calls (from sonypi) */
2222         sony_pic_call1(0x82);
2223         sony_pic_call2(0x81, 0xff);
2224         sony_pic_call1(compat ? 0x92 : 0x82);
2225
2226 end:
2227         kfree(resource);
2228         return result;
2229 }
2230
2231 /*****************
2232  *
2233  * ISR: some event is available
2234  *
2235  *****************/
2236 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2237 {
2238         int i, j;
2239         u8 ev = 0;
2240         u8 data_mask = 0;
2241         u8 device_event = 0;
2242
2243         struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2244
2245         ev = inb_p(dev->cur_ioport->io.minimum);
2246         data_mask = inb_p(dev->cur_ioport->io.minimum + dev->evport_offset);
2247
2248         dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2249                         ev, data_mask, dev->cur_ioport->io.minimum, dev->evport_offset);
2250
2251         if (ev == 0x00 || ev == 0xff)
2252                 return IRQ_HANDLED;
2253
2254         for (i = 0; sony_pic_eventtypes[i].model; i++) {
2255
2256                 if (spic_dev.model != sony_pic_eventtypes[i].model)
2257                         continue;
2258
2259                 if ((data_mask & sony_pic_eventtypes[i].data) !=
2260                     sony_pic_eventtypes[i].data)
2261                         continue;
2262
2263                 if (!(mask & sony_pic_eventtypes[i].mask))
2264                         continue;
2265
2266                 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2267                         if (ev == sony_pic_eventtypes[i].events[j].data) {
2268                                 device_event =
2269                                         sony_pic_eventtypes[i].events[j].event;
2270                                 goto found;
2271                         }
2272                 }
2273         }
2274         return IRQ_HANDLED;
2275
2276 found:
2277         sony_laptop_report_input_event(device_event);
2278         acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
2279         sonypi_compat_report_event(device_event);
2280
2281         return IRQ_HANDLED;
2282 }
2283
2284 /*****************
2285  *
2286  *  ACPI driver
2287  *
2288  *****************/
2289 static int sony_pic_remove(struct acpi_device *device, int type)
2290 {
2291         struct sony_pic_ioport *io, *tmp_io;
2292         struct sony_pic_irq *irq, *tmp_irq;
2293
2294         sonypi_compat_exit();
2295
2296         if (sony_pic_disable(device)) {
2297                 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2298                 return -ENXIO;
2299         }
2300
2301         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2302         release_region(spic_dev.cur_ioport->io.minimum,
2303                         spic_dev.cur_ioport->io.address_length);
2304
2305         sony_laptop_remove_input();
2306
2307         /* pf attrs */
2308         sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2309         sony_pf_remove();
2310
2311         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2312                 list_del(&io->list);
2313                 kfree(io);
2314         }
2315         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2316                 list_del(&irq->list);
2317                 kfree(irq);
2318         }
2319         spic_dev.cur_ioport = NULL;
2320         spic_dev.cur_irq = NULL;
2321
2322         dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2323         return 0;
2324 }
2325
2326 static int sony_pic_add(struct acpi_device *device)
2327 {
2328         int result;
2329         struct sony_pic_ioport *io, *tmp_io;
2330         struct sony_pic_irq *irq, *tmp_irq;
2331
2332         printk(KERN_INFO DRV_PFX "%s v%s.\n",
2333                 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2334
2335         spic_dev.acpi_dev = device;
2336         strcpy(acpi_device_class(device), "sony/hotkey");
2337         spic_dev.model = sony_pic_detect_device_type();
2338         mutex_init(&spic_dev.lock);
2339
2340         /* model specific characteristics */
2341         switch(spic_dev.model) {
2342                 case SONYPI_DEVICE_TYPE1:
2343                         spic_dev.evport_offset = SONYPI_TYPE1_OFFSET;
2344                         break;
2345                 case SONYPI_DEVICE_TYPE3:
2346                         spic_dev.evport_offset = SONYPI_TYPE3_OFFSET;
2347                         break;
2348                 case SONYPI_DEVICE_TYPE2:
2349                 default:
2350                         spic_dev.evport_offset = SONYPI_TYPE2_OFFSET;
2351                         break;
2352         }
2353
2354         /* read _PRS resources */
2355         result = sony_pic_possible_resources(device);
2356         if (result) {
2357                 printk(KERN_ERR DRV_PFX
2358                                 "Unabe to read possible resources.\n");
2359                 goto err_free_resources;
2360         }
2361
2362         /* setup input devices and helper fifo */
2363         result = sony_laptop_setup_input();
2364         if (result) {
2365                 printk(KERN_ERR DRV_PFX
2366                                 "Unabe to create input devices.\n");
2367                 goto err_free_resources;
2368         }
2369
2370         /* request io port */
2371         list_for_each_entry(io, &spic_dev.ioports, list) {
2372                 if (request_region(io->io.minimum, io->io.address_length,
2373                                         "Sony Programable I/O Device")) {
2374                         dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
2375                                         io->io.minimum, io->io.maximum,
2376                                         io->io.address_length);
2377                         spic_dev.cur_ioport = io;
2378                         break;
2379                 }
2380         }
2381         if (!spic_dev.cur_ioport) {
2382                 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2383                 result = -ENODEV;
2384                 goto err_remove_input;
2385         }
2386
2387         /* request IRQ */
2388         list_for_each_entry(irq, &spic_dev.interrupts, list) {
2389                 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2390                                         IRQF_SHARED, "sony-laptop", &spic_dev)) {
2391                         dprintk("IRQ: %d - triggering: %d - "
2392                                         "polarity: %d - shr: %d\n",
2393                                         irq->irq.interrupts[0],
2394                                         irq->irq.triggering,
2395                                         irq->irq.polarity,
2396                                         irq->irq.sharable);
2397                         spic_dev.cur_irq = irq;
2398                         break;
2399                 }
2400         }
2401         if (!spic_dev.cur_irq) {
2402                 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2403                 result = -ENODEV;
2404                 goto err_release_region;
2405         }
2406
2407         /* set resource status _SRS */
2408         result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2409         if (result) {
2410                 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2411                 goto err_free_irq;
2412         }
2413
2414         spic_dev.bluetooth_power = -1;
2415         /* create device attributes */
2416         result = sony_pf_add();
2417         if (result)
2418                 goto err_disable_device;
2419
2420         result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2421         if (result)
2422                 goto err_remove_pf;
2423
2424         if (sonypi_compat_init())
2425                 goto err_remove_pf;
2426
2427         return 0;
2428
2429 err_remove_pf:
2430         sony_pf_remove();
2431
2432 err_disable_device:
2433         sony_pic_disable(device);
2434
2435 err_free_irq:
2436         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2437
2438 err_release_region:
2439         release_region(spic_dev.cur_ioport->io.minimum,
2440                         spic_dev.cur_ioport->io.address_length);
2441
2442 err_remove_input:
2443         sony_laptop_remove_input();
2444
2445 err_free_resources:
2446         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2447                 list_del(&io->list);
2448                 kfree(io);
2449         }
2450         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2451                 list_del(&irq->list);
2452                 kfree(irq);
2453         }
2454         spic_dev.cur_ioport = NULL;
2455         spic_dev.cur_irq = NULL;
2456
2457         return result;
2458 }
2459
2460 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2461 {
2462         if (sony_pic_disable(device))
2463                 return -ENXIO;
2464         return 0;
2465 }
2466
2467 static int sony_pic_resume(struct acpi_device *device)
2468 {
2469         sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2470         return 0;
2471 }
2472
2473 static struct acpi_driver sony_pic_driver = {
2474         .name = SONY_PIC_DRIVER_NAME,
2475         .class = SONY_PIC_CLASS,
2476         .ids = SONY_PIC_HID,
2477         .owner = THIS_MODULE,
2478         .ops = {
2479                 .add = sony_pic_add,
2480                 .remove = sony_pic_remove,
2481                 .suspend = sony_pic_suspend,
2482                 .resume = sony_pic_resume,
2483                 },
2484 };
2485
2486 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2487         {
2488                 .ident = "Sony Vaio",
2489                 .matches = {
2490                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2491                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2492                 },
2493         },
2494         {
2495                 .ident = "Sony Vaio",
2496                 .matches = {
2497                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2498                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2499                 },
2500         },
2501         { }
2502 };
2503
2504 static int __init sony_laptop_init(void)
2505 {
2506         int result;
2507
2508         if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2509                 result = acpi_bus_register_driver(&sony_pic_driver);
2510                 if (result) {
2511                         printk(KERN_ERR DRV_PFX
2512                                         "Unable to register SPIC driver.");
2513                         goto out;
2514                 }
2515         }
2516
2517         result = acpi_bus_register_driver(&sony_nc_driver);
2518         if (result) {
2519                 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2520                 goto out_unregister_pic;
2521         }
2522
2523         return 0;
2524
2525 out_unregister_pic:
2526         if (!no_spic)
2527                 acpi_bus_unregister_driver(&sony_pic_driver);
2528 out:
2529         return result;
2530 }
2531
2532 static void __exit sony_laptop_exit(void)
2533 {
2534         acpi_bus_unregister_driver(&sony_nc_driver);
2535         if (!no_spic)
2536                 acpi_bus_unregister_driver(&sony_pic_driver);
2537 }
2538
2539 module_init(sony_laptop_init);
2540 module_exit(sony_laptop_exit);