Merge branch 'master' into for-davem
[sfrench/cifs-2.6.git] / drivers / platform / x86 / 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-2009 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 <linux/slab.h>
62 #include <acpi/acpi_drivers.h>
63 #include <acpi/acpi_bus.h>
64 #include <asm/uaccess.h>
65 #include <linux/sonypi.h>
66 #include <linux/sony-laptop.h>
67 #include <linux/rfkill.h>
68 #ifdef CONFIG_SONYPI_COMPAT
69 #include <linux/poll.h>
70 #include <linux/miscdevice.h>
71 #endif
72
73 #define DRV_PFX                 "sony-laptop: "
74 #define dprintk(msg...)         do {                    \
75         if (debug) printk(KERN_WARNING DRV_PFX  msg);   \
76 } while (0)
77
78 #define SONY_LAPTOP_DRIVER_VERSION      "0.6"
79
80 #define SONY_NC_CLASS           "sony-nc"
81 #define SONY_NC_HID             "SNY5001"
82 #define SONY_NC_DRIVER_NAME     "Sony Notebook Control Driver"
83
84 #define SONY_PIC_CLASS          "sony-pic"
85 #define SONY_PIC_HID            "SNY6001"
86 #define SONY_PIC_DRIVER_NAME    "Sony Programmable IO Control Driver"
87
88 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
89 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
90 MODULE_LICENSE("GPL");
91 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
92
93 static int debug;
94 module_param(debug, int, 0);
95 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
96                  "the development of this driver");
97
98 static int no_spic;             /* = 0 */
99 module_param(no_spic, int, 0444);
100 MODULE_PARM_DESC(no_spic,
101                  "set this if you don't want to enable the SPIC device");
102
103 static int compat;              /* = 0 */
104 module_param(compat, int, 0444);
105 MODULE_PARM_DESC(compat,
106                  "set this if you want to enable backward compatibility mode");
107
108 static unsigned long mask = 0xffffffff;
109 module_param(mask, ulong, 0644);
110 MODULE_PARM_DESC(mask,
111                  "set this to the mask of event you want to enable (see doc)");
112
113 static int camera;              /* = 0 */
114 module_param(camera, int, 0444);
115 MODULE_PARM_DESC(camera,
116                  "set this to 1 to enable Motion Eye camera controls "
117                  "(only use it if you have a C1VE or C1VN model)");
118
119 #ifdef CONFIG_SONYPI_COMPAT
120 static int minor = -1;
121 module_param(minor, int, 0);
122 MODULE_PARM_DESC(minor,
123                  "minor number of the misc device for the SPIC compatibility code, "
124                  "default is -1 (automatic)");
125 #endif
126
127 enum sony_nc_rfkill {
128         SONY_WIFI,
129         SONY_BLUETOOTH,
130         SONY_WWAN,
131         SONY_WIMAX,
132         N_SONY_RFKILL,
133 };
134
135 static int sony_rfkill_handle;
136 static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
137 static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
138 static void sony_nc_rfkill_update(void);
139
140 /*********** Input Devices ***********/
141
142 #define SONY_LAPTOP_BUF_SIZE    128
143 struct sony_laptop_input_s {
144         atomic_t                users;
145         struct input_dev        *jog_dev;
146         struct input_dev        *key_dev;
147         struct kfifo            fifo;
148         spinlock_t              fifo_lock;
149         struct timer_list       release_key_timer;
150 };
151
152 static struct sony_laptop_input_s sony_laptop_input = {
153         .users = ATOMIC_INIT(0),
154 };
155
156 struct sony_laptop_keypress {
157         struct input_dev *dev;
158         int key;
159 };
160
161 /* Correspondance table between sonypi events
162  * and input layer indexes in the keymap
163  */
164 static int sony_laptop_input_index[] = {
165         -1,     /*  0 no event */
166         -1,     /*  1 SONYPI_EVENT_JOGDIAL_DOWN */
167         -1,     /*  2 SONYPI_EVENT_JOGDIAL_UP */
168         -1,     /*  3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
169         -1,     /*  4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
170         -1,     /*  5 SONYPI_EVENT_JOGDIAL_PRESSED */
171         -1,     /*  6 SONYPI_EVENT_JOGDIAL_RELEASED */
172          0,     /*  7 SONYPI_EVENT_CAPTURE_PRESSED */
173          1,     /*  8 SONYPI_EVENT_CAPTURE_RELEASED */
174          2,     /*  9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
175          3,     /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
176          4,     /* 11 SONYPI_EVENT_FNKEY_ESC */
177          5,     /* 12 SONYPI_EVENT_FNKEY_F1 */
178          6,     /* 13 SONYPI_EVENT_FNKEY_F2 */
179          7,     /* 14 SONYPI_EVENT_FNKEY_F3 */
180          8,     /* 15 SONYPI_EVENT_FNKEY_F4 */
181          9,     /* 16 SONYPI_EVENT_FNKEY_F5 */
182         10,     /* 17 SONYPI_EVENT_FNKEY_F6 */
183         11,     /* 18 SONYPI_EVENT_FNKEY_F7 */
184         12,     /* 19 SONYPI_EVENT_FNKEY_F8 */
185         13,     /* 20 SONYPI_EVENT_FNKEY_F9 */
186         14,     /* 21 SONYPI_EVENT_FNKEY_F10 */
187         15,     /* 22 SONYPI_EVENT_FNKEY_F11 */
188         16,     /* 23 SONYPI_EVENT_FNKEY_F12 */
189         17,     /* 24 SONYPI_EVENT_FNKEY_1 */
190         18,     /* 25 SONYPI_EVENT_FNKEY_2 */
191         19,     /* 26 SONYPI_EVENT_FNKEY_D */
192         20,     /* 27 SONYPI_EVENT_FNKEY_E */
193         21,     /* 28 SONYPI_EVENT_FNKEY_F */
194         22,     /* 29 SONYPI_EVENT_FNKEY_S */
195         23,     /* 30 SONYPI_EVENT_FNKEY_B */
196         24,     /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
197         25,     /* 32 SONYPI_EVENT_PKEY_P1 */
198         26,     /* 33 SONYPI_EVENT_PKEY_P2 */
199         27,     /* 34 SONYPI_EVENT_PKEY_P3 */
200         28,     /* 35 SONYPI_EVENT_BACK_PRESSED */
201         -1,     /* 36 SONYPI_EVENT_LID_CLOSED */
202         -1,     /* 37 SONYPI_EVENT_LID_OPENED */
203         29,     /* 38 SONYPI_EVENT_BLUETOOTH_ON */
204         30,     /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
205         31,     /* 40 SONYPI_EVENT_HELP_PRESSED */
206         32,     /* 41 SONYPI_EVENT_FNKEY_ONLY */
207         33,     /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
208         34,     /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
209         35,     /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
210         36,     /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
211         37,     /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
212         38,     /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
213         39,     /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
214         40,     /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
215         41,     /* 50 SONYPI_EVENT_ZOOM_PRESSED */
216         42,     /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
217         43,     /* 52 SONYPI_EVENT_MEYE_FACE */
218         44,     /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
219         45,     /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
220         46,     /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
221         -1,     /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
222         -1,     /* 57 SONYPI_EVENT_BATTERY_INSERT */
223         -1,     /* 58 SONYPI_EVENT_BATTERY_REMOVE */
224         -1,     /* 59 SONYPI_EVENT_FNKEY_RELEASED */
225         47,     /* 60 SONYPI_EVENT_WIRELESS_ON */
226         48,     /* 61 SONYPI_EVENT_WIRELESS_OFF */
227         49,     /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
228         50,     /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
229         51,     /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
230         52,     /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
231         53,     /* 66 SONYPI_EVENT_PKEY_P4 */
232         54,     /* 67 SONYPI_EVENT_PKEY_P5 */
233         55,     /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
234         56,     /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
235         57,     /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
236         -1,     /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
237         58,     /* 72 SONYPI_EVENT_MEDIA_PRESSED */
238 };
239
240 static int sony_laptop_input_keycode_map[] = {
241         KEY_CAMERA,     /*  0 SONYPI_EVENT_CAPTURE_PRESSED */
242         KEY_RESERVED,   /*  1 SONYPI_EVENT_CAPTURE_RELEASED */
243         KEY_RESERVED,   /*  2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
244         KEY_RESERVED,   /*  3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
245         KEY_FN_ESC,     /*  4 SONYPI_EVENT_FNKEY_ESC */
246         KEY_FN_F1,      /*  5 SONYPI_EVENT_FNKEY_F1 */
247         KEY_FN_F2,      /*  6 SONYPI_EVENT_FNKEY_F2 */
248         KEY_FN_F3,      /*  7 SONYPI_EVENT_FNKEY_F3 */
249         KEY_FN_F4,      /*  8 SONYPI_EVENT_FNKEY_F4 */
250         KEY_FN_F5,      /*  9 SONYPI_EVENT_FNKEY_F5 */
251         KEY_FN_F6,      /* 10 SONYPI_EVENT_FNKEY_F6 */
252         KEY_FN_F7,      /* 11 SONYPI_EVENT_FNKEY_F7 */
253         KEY_FN_F8,      /* 12 SONYPI_EVENT_FNKEY_F8 */
254         KEY_FN_F9,      /* 13 SONYPI_EVENT_FNKEY_F9 */
255         KEY_FN_F10,     /* 14 SONYPI_EVENT_FNKEY_F10 */
256         KEY_FN_F11,     /* 15 SONYPI_EVENT_FNKEY_F11 */
257         KEY_FN_F12,     /* 16 SONYPI_EVENT_FNKEY_F12 */
258         KEY_FN_F1,      /* 17 SONYPI_EVENT_FNKEY_1 */
259         KEY_FN_F2,      /* 18 SONYPI_EVENT_FNKEY_2 */
260         KEY_FN_D,       /* 19 SONYPI_EVENT_FNKEY_D */
261         KEY_FN_E,       /* 20 SONYPI_EVENT_FNKEY_E */
262         KEY_FN_F,       /* 21 SONYPI_EVENT_FNKEY_F */
263         KEY_FN_S,       /* 22 SONYPI_EVENT_FNKEY_S */
264         KEY_FN_B,       /* 23 SONYPI_EVENT_FNKEY_B */
265         KEY_BLUETOOTH,  /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
266         KEY_PROG1,      /* 25 SONYPI_EVENT_PKEY_P1 */
267         KEY_PROG2,      /* 26 SONYPI_EVENT_PKEY_P2 */
268         KEY_PROG3,      /* 27 SONYPI_EVENT_PKEY_P3 */
269         KEY_BACK,       /* 28 SONYPI_EVENT_BACK_PRESSED */
270         KEY_BLUETOOTH,  /* 29 SONYPI_EVENT_BLUETOOTH_ON */
271         KEY_BLUETOOTH,  /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
272         KEY_HELP,       /* 31 SONYPI_EVENT_HELP_PRESSED */
273         KEY_FN,         /* 32 SONYPI_EVENT_FNKEY_ONLY */
274         KEY_RESERVED,   /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
275         KEY_RESERVED,   /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
276         KEY_RESERVED,   /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
277         KEY_RESERVED,   /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
278         KEY_RESERVED,   /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
279         KEY_RESERVED,   /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
280         KEY_RESERVED,   /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
281         KEY_RESERVED,   /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
282         KEY_ZOOM,       /* 41 SONYPI_EVENT_ZOOM_PRESSED */
283         BTN_THUMB,      /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
284         KEY_RESERVED,   /* 43 SONYPI_EVENT_MEYE_FACE */
285         KEY_RESERVED,   /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
286         KEY_RESERVED,   /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
287         KEY_RESERVED,   /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
288         KEY_WLAN,       /* 47 SONYPI_EVENT_WIRELESS_ON */
289         KEY_WLAN,       /* 48 SONYPI_EVENT_WIRELESS_OFF */
290         KEY_ZOOMIN,     /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
291         KEY_ZOOMOUT,    /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
292         KEY_EJECTCD,    /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
293         KEY_F13,        /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
294         KEY_PROG4,      /* 53 SONYPI_EVENT_PKEY_P4 */
295         KEY_F14,        /* 54 SONYPI_EVENT_PKEY_P5 */
296         KEY_F15,        /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
297         KEY_VOLUMEUP,   /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
298         KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
299         KEY_MEDIA,      /* 58 SONYPI_EVENT_MEDIA_PRESSED */
300 };
301
302 /* release buttons after a short delay if pressed */
303 static void do_sony_laptop_release_key(unsigned long unused)
304 {
305         struct sony_laptop_keypress kp;
306         unsigned long flags;
307
308         spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
309
310         if (kfifo_out(&sony_laptop_input.fifo,
311                       (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
312                 input_report_key(kp.dev, kp.key, 0);
313                 input_sync(kp.dev);
314         }
315
316         /* If there is something in the fifo schedule next release. */
317         if (kfifo_len(&sony_laptop_input.fifo) != 0)
318                 mod_timer(&sony_laptop_input.release_key_timer,
319                           jiffies + msecs_to_jiffies(10));
320
321         spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
322 }
323
324 /* forward event to the input subsystem */
325 static void sony_laptop_report_input_event(u8 event)
326 {
327         struct input_dev *jog_dev = sony_laptop_input.jog_dev;
328         struct input_dev *key_dev = sony_laptop_input.key_dev;
329         struct sony_laptop_keypress kp = { NULL };
330
331         if (event == SONYPI_EVENT_FNKEY_RELEASED ||
332                         event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
333                 /* Nothing, not all VAIOs generate this event */
334                 return;
335         }
336
337         /* report events */
338         switch (event) {
339         /* jog_dev events */
340         case SONYPI_EVENT_JOGDIAL_UP:
341         case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
342                 input_report_rel(jog_dev, REL_WHEEL, 1);
343                 input_sync(jog_dev);
344                 return;
345
346         case SONYPI_EVENT_JOGDIAL_DOWN:
347         case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
348                 input_report_rel(jog_dev, REL_WHEEL, -1);
349                 input_sync(jog_dev);
350                 return;
351
352         /* key_dev events */
353         case SONYPI_EVENT_JOGDIAL_PRESSED:
354                 kp.key = BTN_MIDDLE;
355                 kp.dev = jog_dev;
356                 break;
357
358         default:
359                 if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
360                         dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
361                         break;
362                 }
363                 if (sony_laptop_input_index[event] != -1) {
364                         kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
365                         if (kp.key != KEY_UNKNOWN)
366                                 kp.dev = key_dev;
367                 }
368                 break;
369         }
370
371         if (kp.dev) {
372                 input_report_key(kp.dev, kp.key, 1);
373                 /* we emit the scancode so we can always remap the key */
374                 input_event(kp.dev, EV_MSC, MSC_SCAN, event);
375                 input_sync(kp.dev);
376
377                 /* schedule key release */
378                 kfifo_in_locked(&sony_laptop_input.fifo,
379                                 (unsigned char *)&kp, sizeof(kp),
380                                 &sony_laptop_input.fifo_lock);
381                 mod_timer(&sony_laptop_input.release_key_timer,
382                           jiffies + msecs_to_jiffies(10));
383         } else
384                 dprintk("unknown input event %.2x\n", event);
385 }
386
387 static int sony_laptop_setup_input(struct acpi_device *acpi_device)
388 {
389         struct input_dev *jog_dev;
390         struct input_dev *key_dev;
391         int i;
392         int error;
393
394         /* don't run again if already initialized */
395         if (atomic_add_return(1, &sony_laptop_input.users) > 1)
396                 return 0;
397
398         /* kfifo */
399         spin_lock_init(&sony_laptop_input.fifo_lock);
400         error = kfifo_alloc(&sony_laptop_input.fifo,
401                             SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
402         if (error) {
403                 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
404                 goto err_dec_users;
405         }
406
407         setup_timer(&sony_laptop_input.release_key_timer,
408                     do_sony_laptop_release_key, 0);
409
410         /* input keys */
411         key_dev = input_allocate_device();
412         if (!key_dev) {
413                 error = -ENOMEM;
414                 goto err_free_kfifo;
415         }
416
417         key_dev->name = "Sony Vaio Keys";
418         key_dev->id.bustype = BUS_ISA;
419         key_dev->id.vendor = PCI_VENDOR_ID_SONY;
420         key_dev->dev.parent = &acpi_device->dev;
421
422         /* Initialize the Input Drivers: special keys */
423         input_set_capability(key_dev, EV_MSC, MSC_SCAN);
424
425         __set_bit(EV_KEY, key_dev->evbit);
426         key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
427         key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
428         key_dev->keycode = &sony_laptop_input_keycode_map;
429         for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
430                 __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
431         __clear_bit(KEY_RESERVED, key_dev->keybit);
432
433         error = input_register_device(key_dev);
434         if (error)
435                 goto err_free_keydev;
436
437         sony_laptop_input.key_dev = key_dev;
438
439         /* jogdial */
440         jog_dev = input_allocate_device();
441         if (!jog_dev) {
442                 error = -ENOMEM;
443                 goto err_unregister_keydev;
444         }
445
446         jog_dev->name = "Sony Vaio Jogdial";
447         jog_dev->id.bustype = BUS_ISA;
448         jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
449         key_dev->dev.parent = &acpi_device->dev;
450
451         input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
452         input_set_capability(jog_dev, EV_REL, REL_WHEEL);
453
454         error = input_register_device(jog_dev);
455         if (error)
456                 goto err_free_jogdev;
457
458         sony_laptop_input.jog_dev = jog_dev;
459
460         return 0;
461
462 err_free_jogdev:
463         input_free_device(jog_dev);
464
465 err_unregister_keydev:
466         input_unregister_device(key_dev);
467         /* to avoid kref underflow below at input_free_device */
468         key_dev = NULL;
469
470 err_free_keydev:
471         input_free_device(key_dev);
472
473 err_free_kfifo:
474         kfifo_free(&sony_laptop_input.fifo);
475
476 err_dec_users:
477         atomic_dec(&sony_laptop_input.users);
478         return error;
479 }
480
481 static void sony_laptop_remove_input(void)
482 {
483         struct sony_laptop_keypress kp = { NULL };
484
485         /* Cleanup only after the last user has gone */
486         if (!atomic_dec_and_test(&sony_laptop_input.users))
487                 return;
488
489         del_timer_sync(&sony_laptop_input.release_key_timer);
490
491         /*
492          * Generate key-up events for remaining keys. Note that we don't
493          * need locking since nobody is adding new events to the kfifo.
494          */
495         while (kfifo_out(&sony_laptop_input.fifo,
496                          (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
497                 input_report_key(kp.dev, kp.key, 0);
498                 input_sync(kp.dev);
499         }
500
501         /* destroy input devs */
502         input_unregister_device(sony_laptop_input.key_dev);
503         sony_laptop_input.key_dev = NULL;
504
505         if (sony_laptop_input.jog_dev) {
506                 input_unregister_device(sony_laptop_input.jog_dev);
507                 sony_laptop_input.jog_dev = NULL;
508         }
509
510         kfifo_free(&sony_laptop_input.fifo);
511 }
512
513 /*********** Platform Device ***********/
514
515 static atomic_t sony_pf_users = ATOMIC_INIT(0);
516 static struct platform_driver sony_pf_driver = {
517         .driver = {
518                    .name = "sony-laptop",
519                    .owner = THIS_MODULE,
520                    }
521 };
522 static struct platform_device *sony_pf_device;
523
524 static int sony_pf_add(void)
525 {
526         int ret = 0;
527
528         /* don't run again if already initialized */
529         if (atomic_add_return(1, &sony_pf_users) > 1)
530                 return 0;
531
532         ret = platform_driver_register(&sony_pf_driver);
533         if (ret)
534                 goto out;
535
536         sony_pf_device = platform_device_alloc("sony-laptop", -1);
537         if (!sony_pf_device) {
538                 ret = -ENOMEM;
539                 goto out_platform_registered;
540         }
541
542         ret = platform_device_add(sony_pf_device);
543         if (ret)
544                 goto out_platform_alloced;
545
546         return 0;
547
548       out_platform_alloced:
549         platform_device_put(sony_pf_device);
550         sony_pf_device = NULL;
551       out_platform_registered:
552         platform_driver_unregister(&sony_pf_driver);
553       out:
554         atomic_dec(&sony_pf_users);
555         return ret;
556 }
557
558 static void sony_pf_remove(void)
559 {
560         /* deregister only after the last user has gone */
561         if (!atomic_dec_and_test(&sony_pf_users))
562                 return;
563
564         platform_device_del(sony_pf_device);
565         platform_device_put(sony_pf_device);
566         platform_driver_unregister(&sony_pf_driver);
567 }
568
569 /*********** SNC (SNY5001) Device ***********/
570
571 /* the device uses 1-based values, while the backlight subsystem uses
572    0-based values */
573 #define SONY_MAX_BRIGHTNESS     8
574
575 #define SNC_VALIDATE_IN         0
576 #define SNC_VALIDATE_OUT        1
577
578 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
579                               char *);
580 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
581                                const char *, size_t);
582 static int boolean_validate(const int, const int);
583 static int brightness_default_validate(const int, const int);
584
585 struct sony_nc_value {
586         char *name;             /* name of the entry */
587         char **acpiget;         /* names of the ACPI get function */
588         char **acpiset;         /* names of the ACPI set function */
589         int (*validate)(const int, const int);  /* input/output validation */
590         int value;              /* current setting */
591         int valid;              /* Has ever been set */
592         int debug;              /* active only in debug mode ? */
593         struct device_attribute devattr;        /* sysfs atribute */
594 };
595
596 #define SNC_HANDLE_NAMES(_name, _values...) \
597         static char *snc_##_name[] = { _values, NULL }
598
599 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
600         { \
601                 .name           = __stringify(_name), \
602                 .acpiget        = _getters, \
603                 .acpiset        = _setters, \
604                 .validate       = _validate, \
605                 .debug          = _debug, \
606                 .devattr        = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
607         }
608
609 #define SNC_HANDLE_NULL { .name = NULL }
610
611 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
612
613 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
614 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
615
616 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
617 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
618
619 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
620 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
621
622 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
623 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
624
625 SNC_HANDLE_NAMES(lidstate_get, "GLID");
626
627 SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
628 SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
629
630 SNC_HANDLE_NAMES(gainbass_get, "GMGB");
631 SNC_HANDLE_NAMES(gainbass_set, "CMGB");
632
633 SNC_HANDLE_NAMES(PID_get, "GPID");
634
635 SNC_HANDLE_NAMES(CTR_get, "GCTR");
636 SNC_HANDLE_NAMES(CTR_set, "SCTR");
637
638 SNC_HANDLE_NAMES(PCR_get, "GPCR");
639 SNC_HANDLE_NAMES(PCR_set, "SPCR");
640
641 SNC_HANDLE_NAMES(CMI_get, "GCMI");
642 SNC_HANDLE_NAMES(CMI_set, "SCMI");
643
644 static struct sony_nc_value sony_nc_values[] = {
645         SNC_HANDLE(brightness_default, snc_brightness_def_get,
646                         snc_brightness_def_set, brightness_default_validate, 0),
647         SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
648         SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
649         SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
650                         boolean_validate, 0),
651         SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
652                         boolean_validate, 1),
653         SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
654                         boolean_validate, 0),
655         SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
656                         boolean_validate, 0),
657         SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
658                         boolean_validate, 0),
659         /* unknown methods */
660         SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
661         SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
662         SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
663         SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
664         SNC_HANDLE_NULL
665 };
666
667 static acpi_handle sony_nc_acpi_handle;
668 static struct acpi_device *sony_nc_acpi_device = NULL;
669
670 /*
671  * acpi_evaluate_object wrappers
672  */
673 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
674 {
675         struct acpi_buffer output;
676         union acpi_object out_obj;
677         acpi_status status;
678
679         output.length = sizeof(out_obj);
680         output.pointer = &out_obj;
681
682         status = acpi_evaluate_object(handle, name, NULL, &output);
683         if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
684                 *result = out_obj.integer.value;
685                 return 0;
686         }
687
688         printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
689
690         return -1;
691 }
692
693 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
694                             int *result)
695 {
696         struct acpi_object_list params;
697         union acpi_object in_obj;
698         struct acpi_buffer output;
699         union acpi_object out_obj;
700         acpi_status status;
701
702         params.count = 1;
703         params.pointer = &in_obj;
704         in_obj.type = ACPI_TYPE_INTEGER;
705         in_obj.integer.value = value;
706
707         output.length = sizeof(out_obj);
708         output.pointer = &out_obj;
709
710         status = acpi_evaluate_object(handle, name, &params, &output);
711         if (status == AE_OK) {
712                 if (result != NULL) {
713                         if (out_obj.type != ACPI_TYPE_INTEGER) {
714                                 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
715                                        "return type\n");
716                                 return -1;
717                         }
718                         *result = out_obj.integer.value;
719                 }
720                 return 0;
721         }
722
723         printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
724
725         return -1;
726 }
727
728 static int sony_find_snc_handle(int handle)
729 {
730         int i;
731         int result;
732
733         for (i = 0x20; i < 0x30; i++) {
734                 acpi_callsetfunc(sony_nc_acpi_handle, "SN00", i, &result);
735                 if (result == handle)
736                         return i-0x20;
737         }
738
739         return -1;
740 }
741
742 static int sony_call_snc_handle(int handle, int argument, int *result)
743 {
744         int offset = sony_find_snc_handle(handle);
745
746         if (offset < 0)
747                 return -1;
748
749         return acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
750                                 result);
751 }
752
753 /*
754  * sony_nc_values input/output validate functions
755  */
756
757 /* brightness_default_validate:
758  *
759  * manipulate input output values to keep consistency with the
760  * backlight framework for which brightness values are 0-based.
761  */
762 static int brightness_default_validate(const int direction, const int value)
763 {
764         switch (direction) {
765                 case SNC_VALIDATE_OUT:
766                         return value - 1;
767                 case SNC_VALIDATE_IN:
768                         if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
769                                 return value + 1;
770         }
771         return -EINVAL;
772 }
773
774 /* boolean_validate:
775  *
776  * on input validate boolean values 0/1, on output just pass the
777  * received value.
778  */
779 static int boolean_validate(const int direction, const int value)
780 {
781         if (direction == SNC_VALIDATE_IN) {
782                 if (value != 0 && value != 1)
783                         return -EINVAL;
784         }
785         return value;
786 }
787
788 /*
789  * Sysfs show/store common to all sony_nc_values
790  */
791 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
792                               char *buffer)
793 {
794         int value;
795         struct sony_nc_value *item =
796             container_of(attr, struct sony_nc_value, devattr);
797
798         if (!*item->acpiget)
799                 return -EIO;
800
801         if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
802                 return -EIO;
803
804         if (item->validate)
805                 value = item->validate(SNC_VALIDATE_OUT, value);
806
807         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
808 }
809
810 static ssize_t sony_nc_sysfs_store(struct device *dev,
811                                struct device_attribute *attr,
812                                const char *buffer, size_t count)
813 {
814         int value;
815         struct sony_nc_value *item =
816             container_of(attr, struct sony_nc_value, devattr);
817
818         if (!item->acpiset)
819                 return -EIO;
820
821         if (count > 31)
822                 return -EINVAL;
823
824         value = simple_strtoul(buffer, NULL, 10);
825
826         if (item->validate)
827                 value = item->validate(SNC_VALIDATE_IN, value);
828
829         if (value < 0)
830                 return value;
831
832         if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
833                 return -EIO;
834         item->value = value;
835         item->valid = 1;
836         return count;
837 }
838
839
840 /*
841  * Backlight device
842  */
843 static int sony_backlight_update_status(struct backlight_device *bd)
844 {
845         return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
846                                 bd->props.brightness + 1, NULL);
847 }
848
849 static int sony_backlight_get_brightness(struct backlight_device *bd)
850 {
851         int value;
852
853         if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
854                 return 0;
855         /* brightness levels are 1-based, while backlight ones are 0-based */
856         return value - 1;
857 }
858
859 static struct backlight_device *sony_backlight_device;
860 static struct backlight_ops sony_backlight_ops = {
861         .update_status = sony_backlight_update_status,
862         .get_brightness = sony_backlight_get_brightness,
863 };
864
865 /*
866  * New SNC-only Vaios event mapping to driver known keys
867  */
868 struct sony_nc_event {
869         u8      data;
870         u8      event;
871 };
872
873 static struct sony_nc_event sony_100_events[] = {
874         { 0x90, SONYPI_EVENT_PKEY_P1 },
875         { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
876         { 0x91, SONYPI_EVENT_PKEY_P2 },
877         { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
878         { 0x81, SONYPI_EVENT_FNKEY_F1 },
879         { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
880         { 0x82, SONYPI_EVENT_FNKEY_F2 },
881         { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
882         { 0x83, SONYPI_EVENT_FNKEY_F3 },
883         { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
884         { 0x84, SONYPI_EVENT_FNKEY_F4 },
885         { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
886         { 0x85, SONYPI_EVENT_FNKEY_F5 },
887         { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
888         { 0x86, SONYPI_EVENT_FNKEY_F6 },
889         { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
890         { 0x87, SONYPI_EVENT_FNKEY_F7 },
891         { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
892         { 0x89, SONYPI_EVENT_FNKEY_F9 },
893         { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
894         { 0x8A, SONYPI_EVENT_FNKEY_F10 },
895         { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
896         { 0x8C, SONYPI_EVENT_FNKEY_F12 },
897         { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
898         { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
899         { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
900         { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
901         { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
902         { 0, 0 },
903 };
904
905 static struct sony_nc_event sony_127_events[] = {
906         { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
907         { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
908         { 0x82, SONYPI_EVENT_PKEY_P1 },
909         { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
910         { 0x83, SONYPI_EVENT_PKEY_P2 },
911         { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
912         { 0x84, SONYPI_EVENT_PKEY_P3 },
913         { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
914         { 0x85, SONYPI_EVENT_PKEY_P4 },
915         { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
916         { 0x86, SONYPI_EVENT_PKEY_P5 },
917         { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
918         { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
919         { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
920         { 0, 0 },
921 };
922
923 /*
924  * ACPI callbacks
925  */
926 static void sony_nc_notify(struct acpi_device *device, u32 event)
927 {
928         u32 ev = event;
929
930         if (ev >= 0x90) {
931                 /* New-style event */
932                 int result;
933                 int key_handle = 0;
934                 ev -= 0x90;
935
936                 if (sony_find_snc_handle(0x100) == ev)
937                         key_handle = 0x100;
938                 if (sony_find_snc_handle(0x127) == ev)
939                         key_handle = 0x127;
940
941                 if (key_handle) {
942                         struct sony_nc_event *key_event;
943
944                         if (sony_call_snc_handle(key_handle, 0x200, &result)) {
945                                 dprintk("sony_nc_notify, unable to decode"
946                                         " event 0x%.2x 0x%.2x\n", key_handle,
947                                         ev);
948                                 /* restore the original event */
949                                 ev = event;
950                         } else {
951                                 ev = result & 0xFF;
952
953                                 if (key_handle == 0x100)
954                                         key_event = sony_100_events;
955                                 else
956                                         key_event = sony_127_events;
957
958                                 for (; key_event->data; key_event++) {
959                                         if (key_event->data == ev) {
960                                                 ev = key_event->event;
961                                                 break;
962                                         }
963                                 }
964
965                                 if (!key_event->data)
966                                         printk(KERN_INFO DRV_PFX
967                                                         "Unknown event: 0x%x 0x%x\n",
968                                                         key_handle,
969                                                         ev);
970                                 else
971                                         sony_laptop_report_input_event(ev);
972                         }
973                 } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
974                         sony_nc_rfkill_update();
975                         return;
976                 }
977         } else
978                 sony_laptop_report_input_event(ev);
979
980         dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
981         acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
982 }
983
984 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
985                                       void *context, void **return_value)
986 {
987         struct acpi_device_info *info;
988
989         if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
990                 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n",
991                         (char *)&info->name, info->param_count);
992
993                 kfree(info);
994         }
995
996         return AE_OK;
997 }
998
999 /*
1000  * ACPI device
1001  */
1002 static int sony_nc_function_setup(struct acpi_device *device)
1003 {
1004         int result;
1005
1006         /* Enable all events */
1007         acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
1008
1009         /* Setup hotkeys */
1010         sony_call_snc_handle(0x0100, 0, &result);
1011         sony_call_snc_handle(0x0101, 0, &result);
1012         sony_call_snc_handle(0x0102, 0x100, &result);
1013         sony_call_snc_handle(0x0127, 0, &result);
1014
1015         return 0;
1016 }
1017
1018 static int sony_nc_resume(struct acpi_device *device)
1019 {
1020         struct sony_nc_value *item;
1021         acpi_handle handle;
1022
1023         for (item = sony_nc_values; item->name; item++) {
1024                 int ret;
1025
1026                 if (!item->valid)
1027                         continue;
1028                 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
1029                                        item->value, NULL);
1030                 if (ret < 0) {
1031                         printk("%s: %d\n", __func__, ret);
1032                         break;
1033                 }
1034         }
1035
1036         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1037                                          &handle))) {
1038                 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1039                         dprintk("ECON Method failed\n");
1040         }
1041
1042         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1043                                          &handle))) {
1044                 dprintk("Doing SNC setup\n");
1045                 sony_nc_function_setup(device);
1046         }
1047
1048         /* set the last requested brightness level */
1049         if (sony_backlight_device &&
1050                         sony_backlight_update_status(sony_backlight_device) < 0)
1051                 printk(KERN_WARNING DRV_PFX "unable to restore brightness level\n");
1052
1053         /* re-read rfkill state */
1054         sony_nc_rfkill_update();
1055
1056         return 0;
1057 }
1058
1059 static void sony_nc_rfkill_cleanup(void)
1060 {
1061         int i;
1062
1063         for (i = 0; i < N_SONY_RFKILL; i++) {
1064                 if (sony_rfkill_devices[i]) {
1065                         rfkill_unregister(sony_rfkill_devices[i]);
1066                         rfkill_destroy(sony_rfkill_devices[i]);
1067                 }
1068         }
1069 }
1070
1071 static int sony_nc_rfkill_set(void *data, bool blocked)
1072 {
1073         int result;
1074         int argument = sony_rfkill_address[(long) data] + 0x100;
1075
1076         if (!blocked)
1077                 argument |= 0xff0000;
1078
1079         return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1080 }
1081
1082 static const struct rfkill_ops sony_rfkill_ops = {
1083         .set_block = sony_nc_rfkill_set,
1084 };
1085
1086 static int sony_nc_setup_rfkill(struct acpi_device *device,
1087                                 enum sony_nc_rfkill nc_type)
1088 {
1089         int err = 0;
1090         struct rfkill *rfk;
1091         enum rfkill_type type;
1092         const char *name;
1093         int result;
1094         bool hwblock;
1095
1096         switch (nc_type) {
1097         case SONY_WIFI:
1098                 type = RFKILL_TYPE_WLAN;
1099                 name = "sony-wifi";
1100                 break;
1101         case SONY_BLUETOOTH:
1102                 type = RFKILL_TYPE_BLUETOOTH;
1103                 name = "sony-bluetooth";
1104                 break;
1105         case SONY_WWAN:
1106                 type = RFKILL_TYPE_WWAN;
1107                 name = "sony-wwan";
1108                 break;
1109         case SONY_WIMAX:
1110                 type = RFKILL_TYPE_WIMAX;
1111                 name = "sony-wimax";
1112                 break;
1113         default:
1114                 return -EINVAL;
1115         }
1116
1117         rfk = rfkill_alloc(name, &device->dev, type,
1118                            &sony_rfkill_ops, (void *)nc_type);
1119         if (!rfk)
1120                 return -ENOMEM;
1121
1122         sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1123         hwblock = !(result & 0x1);
1124         rfkill_set_hw_state(rfk, hwblock);
1125
1126         err = rfkill_register(rfk);
1127         if (err) {
1128                 rfkill_destroy(rfk);
1129                 return err;
1130         }
1131         sony_rfkill_devices[nc_type] = rfk;
1132         return err;
1133 }
1134
1135 static void sony_nc_rfkill_update()
1136 {
1137         enum sony_nc_rfkill i;
1138         int result;
1139         bool hwblock;
1140
1141         sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
1142         hwblock = !(result & 0x1);
1143
1144         for (i = 0; i < N_SONY_RFKILL; i++) {
1145                 int argument = sony_rfkill_address[i];
1146
1147                 if (!sony_rfkill_devices[i])
1148                         continue;
1149
1150                 if (hwblock) {
1151                         if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
1152                                 /* we already know we're blocked */
1153                         }
1154                         continue;
1155                 }
1156
1157                 sony_call_snc_handle(sony_rfkill_handle, argument, &result);
1158                 rfkill_set_states(sony_rfkill_devices[i],
1159                                   !(result & 0xf), false);
1160         }
1161 }
1162
1163 static void sony_nc_rfkill_setup(struct acpi_device *device)
1164 {
1165         int offset;
1166         u8 dev_code, i;
1167         acpi_status status;
1168         struct acpi_object_list params;
1169         union acpi_object in_obj;
1170         union acpi_object *device_enum;
1171         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1172
1173         offset = sony_find_snc_handle(0x124);
1174         if (offset == -1) {
1175                 offset = sony_find_snc_handle(0x135);
1176                 if (offset == -1)
1177                         return;
1178                 else
1179                         sony_rfkill_handle = 0x135;
1180         } else
1181                 sony_rfkill_handle = 0x124;
1182         dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
1183
1184         /* need to read the whole buffer returned by the acpi call to SN06
1185          * here otherwise we may miss some features
1186          */
1187         params.count = 1;
1188         params.pointer = &in_obj;
1189         in_obj.type = ACPI_TYPE_INTEGER;
1190         in_obj.integer.value = offset;
1191         status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
1192                         &buffer);
1193         if (ACPI_FAILURE(status)) {
1194                 dprintk("Radio device enumeration failed\n");
1195                 return;
1196         }
1197
1198         device_enum = (union acpi_object *) buffer.pointer;
1199         if (!device_enum || device_enum->type != ACPI_TYPE_BUFFER) {
1200                 printk(KERN_ERR "Invalid SN06 return object 0x%.2x\n",
1201                                 device_enum->type);
1202                 goto out_no_enum;
1203         }
1204
1205         /* the buffer is filled with magic numbers describing the devices
1206          * available, 0xff terminates the enumeration
1207          */
1208         for (i = 0; i < device_enum->buffer.length; i++) {
1209
1210                 dev_code = *(device_enum->buffer.pointer + i);
1211                 if (dev_code == 0xff)
1212                         break;
1213
1214                 dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
1215
1216                 if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
1217                         sony_nc_setup_rfkill(device, SONY_WIFI);
1218
1219                 if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
1220                         sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
1221
1222                 if ((0xf0 & dev_code) == 0x20 &&
1223                                 !sony_rfkill_devices[SONY_WWAN])
1224                         sony_nc_setup_rfkill(device, SONY_WWAN);
1225
1226                 if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
1227                         sony_nc_setup_rfkill(device, SONY_WIMAX);
1228         }
1229
1230 out_no_enum:
1231         kfree(buffer.pointer);
1232         return;
1233 }
1234
1235 static int sony_nc_add(struct acpi_device *device)
1236 {
1237         acpi_status status;
1238         int result = 0;
1239         acpi_handle handle;
1240         struct sony_nc_value *item;
1241
1242         printk(KERN_INFO DRV_PFX "%s v%s.\n",
1243                 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
1244
1245         sony_nc_acpi_device = device;
1246         strcpy(acpi_device_class(device), "sony/hotkey");
1247
1248         sony_nc_acpi_handle = device->handle;
1249
1250         /* read device status */
1251         result = acpi_bus_get_status(device);
1252         /* bail IFF the above call was successful and the device is not present */
1253         if (!result && !device->status.present) {
1254                 dprintk("Device not present\n");
1255                 result = -ENODEV;
1256                 goto outwalk;
1257         }
1258
1259         if (debug) {
1260                 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
1261                                              1, sony_walk_callback, NULL, NULL, NULL);
1262                 if (ACPI_FAILURE(status)) {
1263                         printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
1264                         result = -ENODEV;
1265                         goto outwalk;
1266                 }
1267         }
1268
1269         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
1270                                          &handle))) {
1271                 if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
1272                         dprintk("ECON Method failed\n");
1273         }
1274
1275         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
1276                                          &handle))) {
1277                 dprintk("Doing SNC setup\n");
1278                 sony_nc_function_setup(device);
1279                 sony_nc_rfkill_setup(device);
1280         }
1281
1282         /* setup input devices and helper fifo */
1283         result = sony_laptop_setup_input(device);
1284         if (result) {
1285                 printk(KERN_ERR DRV_PFX
1286                                 "Unable to create input devices.\n");
1287                 goto outwalk;
1288         }
1289
1290         if (acpi_video_backlight_support()) {
1291                 printk(KERN_INFO DRV_PFX "brightness ignored, must be "
1292                        "controlled by ACPI video driver\n");
1293         } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
1294                                                 &handle))) {
1295                                                         struct backlight_properties props;
1296                 memset(&props, 0, sizeof(struct backlight_properties));
1297                 props.max_brightness = SONY_MAX_BRIGHTNESS - 1;
1298                 sony_backlight_device = backlight_device_register("sony", NULL,
1299                                                                   NULL,
1300                                                                   &sony_backlight_ops,
1301                                                                   &props);
1302
1303                 if (IS_ERR(sony_backlight_device)) {
1304                         printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
1305                         sony_backlight_device = NULL;
1306                 } else {
1307                         sony_backlight_device->props.brightness =
1308                             sony_backlight_get_brightness
1309                             (sony_backlight_device);
1310                 }
1311
1312         }
1313
1314         result = sony_pf_add();
1315         if (result)
1316                 goto outbacklight;
1317
1318         /* create sony_pf sysfs attributes related to the SNC device */
1319         for (item = sony_nc_values; item->name; ++item) {
1320
1321                 if (!debug && item->debug)
1322                         continue;
1323
1324                 /* find the available acpiget as described in the DSDT */
1325                 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
1326                         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1327                                                          *item->acpiget,
1328                                                          &handle))) {
1329                                 dprintk("Found %s getter: %s\n",
1330                                                 item->name, *item->acpiget);
1331                                 item->devattr.attr.mode |= S_IRUGO;
1332                                 break;
1333                         }
1334                 }
1335
1336                 /* find the available acpiset as described in the DSDT */
1337                 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
1338                         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
1339                                                          *item->acpiset,
1340                                                          &handle))) {
1341                                 dprintk("Found %s setter: %s\n",
1342                                                 item->name, *item->acpiset);
1343                                 item->devattr.attr.mode |= S_IWUSR;
1344                                 break;
1345                         }
1346                 }
1347
1348                 if (item->devattr.attr.mode != 0) {
1349                         result =
1350                             device_create_file(&sony_pf_device->dev,
1351                                                &item->devattr);
1352                         if (result)
1353                                 goto out_sysfs;
1354                 }
1355         }
1356
1357         return 0;
1358
1359       out_sysfs:
1360         for (item = sony_nc_values; item->name; ++item) {
1361                 device_remove_file(&sony_pf_device->dev, &item->devattr);
1362         }
1363         sony_pf_remove();
1364
1365       outbacklight:
1366         if (sony_backlight_device)
1367                 backlight_device_unregister(sony_backlight_device);
1368
1369         sony_laptop_remove_input();
1370
1371       outwalk:
1372         sony_nc_rfkill_cleanup();
1373         return result;
1374 }
1375
1376 static int sony_nc_remove(struct acpi_device *device, int type)
1377 {
1378         struct sony_nc_value *item;
1379
1380         if (sony_backlight_device)
1381                 backlight_device_unregister(sony_backlight_device);
1382
1383         sony_nc_acpi_device = NULL;
1384
1385         for (item = sony_nc_values; item->name; ++item) {
1386                 device_remove_file(&sony_pf_device->dev, &item->devattr);
1387         }
1388
1389         sony_pf_remove();
1390         sony_laptop_remove_input();
1391         sony_nc_rfkill_cleanup();
1392         dprintk(SONY_NC_DRIVER_NAME " removed.\n");
1393
1394         return 0;
1395 }
1396
1397 static const struct acpi_device_id sony_device_ids[] = {
1398         {SONY_NC_HID, 0},
1399         {SONY_PIC_HID, 0},
1400         {"", 0},
1401 };
1402 MODULE_DEVICE_TABLE(acpi, sony_device_ids);
1403
1404 static const struct acpi_device_id sony_nc_device_ids[] = {
1405         {SONY_NC_HID, 0},
1406         {"", 0},
1407 };
1408
1409 static struct acpi_driver sony_nc_driver = {
1410         .name = SONY_NC_DRIVER_NAME,
1411         .class = SONY_NC_CLASS,
1412         .ids = sony_nc_device_ids,
1413         .owner = THIS_MODULE,
1414         .ops = {
1415                 .add = sony_nc_add,
1416                 .remove = sony_nc_remove,
1417                 .resume = sony_nc_resume,
1418                 .notify = sony_nc_notify,
1419                 },
1420 };
1421
1422 /*********** SPIC (SNY6001) Device ***********/
1423
1424 #define SONYPI_DEVICE_TYPE1     0x00000001
1425 #define SONYPI_DEVICE_TYPE2     0x00000002
1426 #define SONYPI_DEVICE_TYPE3     0x00000004
1427 #define SONYPI_DEVICE_TYPE4     0x00000008
1428
1429 #define SONYPI_TYPE1_OFFSET     0x04
1430 #define SONYPI_TYPE2_OFFSET     0x12
1431 #define SONYPI_TYPE3_OFFSET     0x12
1432
1433 struct sony_pic_ioport {
1434         struct acpi_resource_io io1;
1435         struct acpi_resource_io io2;
1436         struct list_head        list;
1437 };
1438
1439 struct sony_pic_irq {
1440         struct acpi_resource_irq        irq;
1441         struct list_head                list;
1442 };
1443
1444 struct sonypi_eventtypes {
1445         u8                      data;
1446         unsigned long           mask;
1447         struct sonypi_event     *events;
1448 };
1449
1450 struct sony_pic_dev {
1451         struct acpi_device              *acpi_dev;
1452         struct sony_pic_irq             *cur_irq;
1453         struct sony_pic_ioport          *cur_ioport;
1454         struct list_head                interrupts;
1455         struct list_head                ioports;
1456         struct mutex                    lock;
1457         struct sonypi_eventtypes        *event_types;
1458         int                             (*handle_irq)(const u8, const u8);
1459         int                             model;
1460         u16                             evport_offset;
1461         u8                              camera_power;
1462         u8                              bluetooth_power;
1463         u8                              wwan_power;
1464 };
1465
1466 static struct sony_pic_dev spic_dev = {
1467         .interrupts     = LIST_HEAD_INIT(spic_dev.interrupts),
1468         .ioports        = LIST_HEAD_INIT(spic_dev.ioports),
1469 };
1470
1471 static int spic_drv_registered;
1472
1473 /* Event masks */
1474 #define SONYPI_JOGGER_MASK                      0x00000001
1475 #define SONYPI_CAPTURE_MASK                     0x00000002
1476 #define SONYPI_FNKEY_MASK                       0x00000004
1477 #define SONYPI_BLUETOOTH_MASK                   0x00000008
1478 #define SONYPI_PKEY_MASK                        0x00000010
1479 #define SONYPI_BACK_MASK                        0x00000020
1480 #define SONYPI_HELP_MASK                        0x00000040
1481 #define SONYPI_LID_MASK                         0x00000080
1482 #define SONYPI_ZOOM_MASK                        0x00000100
1483 #define SONYPI_THUMBPHRASE_MASK                 0x00000200
1484 #define SONYPI_MEYE_MASK                        0x00000400
1485 #define SONYPI_MEMORYSTICK_MASK                 0x00000800
1486 #define SONYPI_BATTERY_MASK                     0x00001000
1487 #define SONYPI_WIRELESS_MASK                    0x00002000
1488
1489 struct sonypi_event {
1490         u8      data;
1491         u8      event;
1492 };
1493
1494 /* The set of possible button release events */
1495 static struct sonypi_event sonypi_releaseev[] = {
1496         { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
1497         { 0, 0 }
1498 };
1499
1500 /* The set of possible jogger events  */
1501 static struct sonypi_event sonypi_joggerev[] = {
1502         { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
1503         { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
1504         { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
1505         { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
1506         { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
1507         { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
1508         { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
1509         { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
1510         { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
1511         { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
1512         { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
1513         { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
1514         { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
1515         { 0, 0 }
1516 };
1517
1518 /* The set of possible capture button events */
1519 static struct sonypi_event sonypi_captureev[] = {
1520         { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1521         { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1522         { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
1523         { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1524         { 0, 0 }
1525 };
1526
1527 /* The set of possible fnkeys events */
1528 static struct sonypi_event sonypi_fnkeyev[] = {
1529         { 0x10, SONYPI_EVENT_FNKEY_ESC },
1530         { 0x11, SONYPI_EVENT_FNKEY_F1 },
1531         { 0x12, SONYPI_EVENT_FNKEY_F2 },
1532         { 0x13, SONYPI_EVENT_FNKEY_F3 },
1533         { 0x14, SONYPI_EVENT_FNKEY_F4 },
1534         { 0x15, SONYPI_EVENT_FNKEY_F5 },
1535         { 0x16, SONYPI_EVENT_FNKEY_F6 },
1536         { 0x17, SONYPI_EVENT_FNKEY_F7 },
1537         { 0x18, SONYPI_EVENT_FNKEY_F8 },
1538         { 0x19, SONYPI_EVENT_FNKEY_F9 },
1539         { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1540         { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1541         { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1542         { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1543         { 0x21, SONYPI_EVENT_FNKEY_1 },
1544         { 0x22, SONYPI_EVENT_FNKEY_2 },
1545         { 0x31, SONYPI_EVENT_FNKEY_D },
1546         { 0x32, SONYPI_EVENT_FNKEY_E },
1547         { 0x33, SONYPI_EVENT_FNKEY_F },
1548         { 0x34, SONYPI_EVENT_FNKEY_S },
1549         { 0x35, SONYPI_EVENT_FNKEY_B },
1550         { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1551         { 0, 0 }
1552 };
1553
1554 /* The set of possible program key events */
1555 static struct sonypi_event sonypi_pkeyev[] = {
1556         { 0x01, SONYPI_EVENT_PKEY_P1 },
1557         { 0x02, SONYPI_EVENT_PKEY_P2 },
1558         { 0x04, SONYPI_EVENT_PKEY_P3 },
1559         { 0x20, SONYPI_EVENT_PKEY_P1 },
1560         { 0, 0 }
1561 };
1562
1563 /* The set of possible bluetooth events */
1564 static struct sonypi_event sonypi_blueev[] = {
1565         { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1566         { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1567         { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1568         { 0, 0 }
1569 };
1570
1571 /* The set of possible wireless events */
1572 static struct sonypi_event sonypi_wlessev[] = {
1573         { 0x59, SONYPI_EVENT_WIRELESS_ON },
1574         { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1575         { 0, 0 }
1576 };
1577
1578 /* The set of possible back button events */
1579 static struct sonypi_event sonypi_backev[] = {
1580         { 0x20, SONYPI_EVENT_BACK_PRESSED },
1581         { 0, 0 }
1582 };
1583
1584 /* The set of possible help button events */
1585 static struct sonypi_event sonypi_helpev[] = {
1586         { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1587         { 0, 0 }
1588 };
1589
1590
1591 /* The set of possible lid events */
1592 static struct sonypi_event sonypi_lidev[] = {
1593         { 0x51, SONYPI_EVENT_LID_CLOSED },
1594         { 0x50, SONYPI_EVENT_LID_OPENED },
1595         { 0, 0 }
1596 };
1597
1598 /* The set of possible zoom events */
1599 static struct sonypi_event sonypi_zoomev[] = {
1600         { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1601         { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
1602         { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
1603         { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
1604         { 0, 0 }
1605 };
1606
1607 /* The set of possible thumbphrase events */
1608 static struct sonypi_event sonypi_thumbphraseev[] = {
1609         { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1610         { 0, 0 }
1611 };
1612
1613 /* The set of possible motioneye camera events */
1614 static struct sonypi_event sonypi_meyeev[] = {
1615         { 0x00, SONYPI_EVENT_MEYE_FACE },
1616         { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1617         { 0, 0 }
1618 };
1619
1620 /* The set of possible memorystick events */
1621 static struct sonypi_event sonypi_memorystickev[] = {
1622         { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1623         { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1624         { 0, 0 }
1625 };
1626
1627 /* The set of possible battery events */
1628 static struct sonypi_event sonypi_batteryev[] = {
1629         { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1630         { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1631         { 0, 0 }
1632 };
1633
1634 /* The set of possible volume events */
1635 static struct sonypi_event sonypi_volumeev[] = {
1636         { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
1637         { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
1638         { 0, 0 }
1639 };
1640
1641 /* The set of possible brightness events */
1642 static struct sonypi_event sonypi_brightnessev[] = {
1643         { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
1644         { 0, 0 }
1645 };
1646
1647 static struct sonypi_eventtypes type1_events[] = {
1648         { 0, 0xffffffff, sonypi_releaseev },
1649         { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1650         { 0x30, SONYPI_LID_MASK, sonypi_lidev },
1651         { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1652         { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1653         { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1654         { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1655         { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1656         { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1657         { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1658         { 0 },
1659 };
1660 static struct sonypi_eventtypes type2_events[] = {
1661         { 0, 0xffffffff, sonypi_releaseev },
1662         { 0x38, SONYPI_LID_MASK, sonypi_lidev },
1663         { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1664         { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1665         { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1666         { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1667         { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1668         { 0x11, SONYPI_BACK_MASK, sonypi_backev },
1669         { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1670         { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1671         { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1672         { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1673         { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1674         { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1675         { 0 },
1676 };
1677 static struct sonypi_eventtypes type3_events[] = {
1678         { 0, 0xffffffff, sonypi_releaseev },
1679         { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1680         { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1681         { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1682         { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1683         { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1684         { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
1685         { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
1686         { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
1687         { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
1688         { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
1689         { 0 },
1690 };
1691
1692 /* low level spic calls */
1693 #define ITERATIONS_LONG         10000
1694 #define ITERATIONS_SHORT        10
1695 #define wait_on_command(command, iterations) {                          \
1696         unsigned int n = iterations;                                    \
1697         while (--n && (command))                                        \
1698                 udelay(1);                                              \
1699         if (!n)                                                         \
1700                 dprintk("command failed at %s : %s (line %d)\n",        \
1701                                 __FILE__, __func__, __LINE__);  \
1702 }
1703
1704 static u8 sony_pic_call1(u8 dev)
1705 {
1706         u8 v1, v2;
1707
1708         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1709                         ITERATIONS_LONG);
1710         outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1711         v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
1712         v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
1713         dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
1714         return v2;
1715 }
1716
1717 static u8 sony_pic_call2(u8 dev, u8 fn)
1718 {
1719         u8 v1;
1720
1721         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1722                         ITERATIONS_LONG);
1723         outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1724         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
1725                         ITERATIONS_LONG);
1726         outb(fn, spic_dev.cur_ioport->io1.minimum);
1727         v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1728         dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
1729         return v1;
1730 }
1731
1732 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1733 {
1734         u8 v1;
1735
1736         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1737         outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
1738         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1739         outb(fn, spic_dev.cur_ioport->io1.minimum);
1740         wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
1741         outb(v, spic_dev.cur_ioport->io1.minimum);
1742         v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
1743         dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
1744                         dev, fn, v, v1);
1745         return v1;
1746 }
1747
1748 /*
1749  * minidrivers for SPIC models
1750  */
1751 static int type3_handle_irq(const u8 data_mask, const u8 ev)
1752 {
1753         /*
1754          * 0x31 could mean we have to take some extra action and wait for
1755          * the next irq for some Type3 models, it will generate a new
1756          * irq and we can read new data from the device:
1757          *  - 0x5c and 0x5f requires 0xA0
1758          *  - 0x61 requires 0xB3
1759          */
1760         if (data_mask == 0x31) {
1761                 if (ev == 0x5c || ev == 0x5f)
1762                         sony_pic_call1(0xA0);
1763                 else if (ev == 0x61)
1764                         sony_pic_call1(0xB3);
1765                 return 0;
1766         }
1767         return 1;
1768 }
1769
1770 static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
1771 {
1772         struct pci_dev *pcidev;
1773
1774         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1775                         PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
1776         if (pcidev) {
1777                 dev->model = SONYPI_DEVICE_TYPE1;
1778                 dev->evport_offset = SONYPI_TYPE1_OFFSET;
1779                 dev->event_types = type1_events;
1780                 goto out;
1781         }
1782
1783         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1784                         PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
1785         if (pcidev) {
1786                 dev->model = SONYPI_DEVICE_TYPE2;
1787                 dev->evport_offset = SONYPI_TYPE2_OFFSET;
1788                 dev->event_types = type2_events;
1789                 goto out;
1790         }
1791
1792         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1793                         PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
1794         if (pcidev) {
1795                 dev->model = SONYPI_DEVICE_TYPE3;
1796                 dev->handle_irq = type3_handle_irq;
1797                 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1798                 dev->event_types = type3_events;
1799                 goto out;
1800         }
1801
1802         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1803                         PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
1804         if (pcidev) {
1805                 dev->model = SONYPI_DEVICE_TYPE3;
1806                 dev->handle_irq = type3_handle_irq;
1807                 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1808                 dev->event_types = type3_events;
1809                 goto out;
1810         }
1811
1812         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1813                         PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
1814         if (pcidev) {
1815                 dev->model = SONYPI_DEVICE_TYPE3;
1816                 dev->handle_irq = type3_handle_irq;
1817                 dev->evport_offset = SONYPI_TYPE3_OFFSET;
1818                 dev->event_types = type3_events;
1819                 goto out;
1820         }
1821
1822         /* default */
1823         dev->model = SONYPI_DEVICE_TYPE2;
1824         dev->evport_offset = SONYPI_TYPE2_OFFSET;
1825         dev->event_types = type2_events;
1826
1827 out:
1828         if (pcidev)
1829                 pci_dev_put(pcidev);
1830
1831         printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1832                         dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
1833                         dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1834 }
1835
1836 /* camera tests and poweron/poweroff */
1837 #define SONYPI_CAMERA_PICTURE           5
1838 #define SONYPI_CAMERA_CONTROL           0x10
1839
1840 #define SONYPI_CAMERA_BRIGHTNESS                0
1841 #define SONYPI_CAMERA_CONTRAST                  1
1842 #define SONYPI_CAMERA_HUE                       2
1843 #define SONYPI_CAMERA_COLOR                     3
1844 #define SONYPI_CAMERA_SHARPNESS                 4
1845
1846 #define SONYPI_CAMERA_EXPOSURE_MASK             0xC
1847 #define SONYPI_CAMERA_WHITE_BALANCE_MASK        0x3
1848 #define SONYPI_CAMERA_PICTURE_MODE_MASK         0x30
1849 #define SONYPI_CAMERA_MUTE_MASK                 0x40
1850
1851 /* the rest don't need a loop until not 0xff */
1852 #define SONYPI_CAMERA_AGC                       6
1853 #define SONYPI_CAMERA_AGC_MASK                  0x30
1854 #define SONYPI_CAMERA_SHUTTER_MASK              0x7
1855
1856 #define SONYPI_CAMERA_SHUTDOWN_REQUEST          7
1857 #define SONYPI_CAMERA_CONTROL                   0x10
1858
1859 #define SONYPI_CAMERA_STATUS                    7
1860 #define SONYPI_CAMERA_STATUS_READY              0x2
1861 #define SONYPI_CAMERA_STATUS_POSITION           0x4
1862
1863 #define SONYPI_DIRECTION_BACKWARDS              0x4
1864
1865 #define SONYPI_CAMERA_REVISION                  8
1866 #define SONYPI_CAMERA_ROMVERSION                9
1867
1868 static int __sony_pic_camera_ready(void)
1869 {
1870         u8 v;
1871
1872         v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1873         return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1874 }
1875
1876 static int __sony_pic_camera_off(void)
1877 {
1878         if (!camera) {
1879                 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1880                 return -ENODEV;
1881         }
1882
1883         wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1884                                 SONYPI_CAMERA_MUTE_MASK),
1885                         ITERATIONS_SHORT);
1886
1887         if (spic_dev.camera_power) {
1888                 sony_pic_call2(0x91, 0);
1889                 spic_dev.camera_power = 0;
1890         }
1891         return 0;
1892 }
1893
1894 static int __sony_pic_camera_on(void)
1895 {
1896         int i, j, x;
1897
1898         if (!camera) {
1899                 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1900                 return -ENODEV;
1901         }
1902
1903         if (spic_dev.camera_power)
1904                 return 0;
1905
1906         for (j = 5; j > 0; j--) {
1907
1908                 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1909                         msleep(10);
1910                 sony_pic_call1(0x93);
1911
1912                 for (i = 400; i > 0; i--) {
1913                         if (__sony_pic_camera_ready())
1914                                 break;
1915                         msleep(10);
1916                 }
1917                 if (i)
1918                         break;
1919         }
1920
1921         if (j == 0) {
1922                 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
1923                 return -ENODEV;
1924         }
1925
1926         wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1927                                 0x5a),
1928                         ITERATIONS_SHORT);
1929
1930         spic_dev.camera_power = 1;
1931         return 0;
1932 }
1933
1934 /* External camera command (exported to the motion eye v4l driver) */
1935 int sony_pic_camera_command(int command, u8 value)
1936 {
1937         if (!camera)
1938                 return -EIO;
1939
1940         mutex_lock(&spic_dev.lock);
1941
1942         switch (command) {
1943         case SONY_PIC_COMMAND_SETCAMERA:
1944                 if (value)
1945                         __sony_pic_camera_on();
1946                 else
1947                         __sony_pic_camera_off();
1948                 break;
1949         case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
1950                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1951                                 ITERATIONS_SHORT);
1952                 break;
1953         case SONY_PIC_COMMAND_SETCAMERACONTRAST:
1954                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1955                                 ITERATIONS_SHORT);
1956                 break;
1957         case SONY_PIC_COMMAND_SETCAMERAHUE:
1958                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1959                                 ITERATIONS_SHORT);
1960                 break;
1961         case SONY_PIC_COMMAND_SETCAMERACOLOR:
1962                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1963                                 ITERATIONS_SHORT);
1964                 break;
1965         case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
1966                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1967                                 ITERATIONS_SHORT);
1968                 break;
1969         case SONY_PIC_COMMAND_SETCAMERAPICTURE:
1970                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1971                                 ITERATIONS_SHORT);
1972                 break;
1973         case SONY_PIC_COMMAND_SETCAMERAAGC:
1974                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1975                                 ITERATIONS_SHORT);
1976                 break;
1977         default:
1978                 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1979                        command);
1980                 break;
1981         }
1982         mutex_unlock(&spic_dev.lock);
1983         return 0;
1984 }
1985 EXPORT_SYMBOL(sony_pic_camera_command);
1986
1987 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1988 static void __sony_pic_set_wwanpower(u8 state)
1989 {
1990         state = !!state;
1991         if (spic_dev.wwan_power == state)
1992                 return;
1993         sony_pic_call2(0xB0, state);
1994         sony_pic_call1(0x82);
1995         spic_dev.wwan_power = state;
1996 }
1997
1998 static ssize_t sony_pic_wwanpower_store(struct device *dev,
1999                 struct device_attribute *attr,
2000                 const char *buffer, size_t count)
2001 {
2002         unsigned long value;
2003         if (count > 31)
2004                 return -EINVAL;
2005
2006         value = simple_strtoul(buffer, NULL, 10);
2007         mutex_lock(&spic_dev.lock);
2008         __sony_pic_set_wwanpower(value);
2009         mutex_unlock(&spic_dev.lock);
2010
2011         return count;
2012 }
2013
2014 static ssize_t sony_pic_wwanpower_show(struct device *dev,
2015                 struct device_attribute *attr, char *buffer)
2016 {
2017         ssize_t count;
2018         mutex_lock(&spic_dev.lock);
2019         count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
2020         mutex_unlock(&spic_dev.lock);
2021         return count;
2022 }
2023
2024 /* bluetooth subsystem power state */
2025 static void __sony_pic_set_bluetoothpower(u8 state)
2026 {
2027         state = !!state;
2028         if (spic_dev.bluetooth_power == state)
2029                 return;
2030         sony_pic_call2(0x96, state);
2031         sony_pic_call1(0x82);
2032         spic_dev.bluetooth_power = state;
2033 }
2034
2035 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
2036                 struct device_attribute *attr,
2037                 const char *buffer, size_t count)
2038 {
2039         unsigned long value;
2040         if (count > 31)
2041                 return -EINVAL;
2042
2043         value = simple_strtoul(buffer, NULL, 10);
2044         mutex_lock(&spic_dev.lock);
2045         __sony_pic_set_bluetoothpower(value);
2046         mutex_unlock(&spic_dev.lock);
2047
2048         return count;
2049 }
2050
2051 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
2052                 struct device_attribute *attr, char *buffer)
2053 {
2054         ssize_t count = 0;
2055         mutex_lock(&spic_dev.lock);
2056         count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
2057         mutex_unlock(&spic_dev.lock);
2058         return count;
2059 }
2060
2061 /* fan speed */
2062 /* FAN0 information (reverse engineered from ACPI tables) */
2063 #define SONY_PIC_FAN0_STATUS    0x93
2064 static int sony_pic_set_fanspeed(unsigned long value)
2065 {
2066         return ec_write(SONY_PIC_FAN0_STATUS, value);
2067 }
2068
2069 static int sony_pic_get_fanspeed(u8 *value)
2070 {
2071         return ec_read(SONY_PIC_FAN0_STATUS, value);
2072 }
2073
2074 static ssize_t sony_pic_fanspeed_store(struct device *dev,
2075                 struct device_attribute *attr,
2076                 const char *buffer, size_t count)
2077 {
2078         unsigned long value;
2079         if (count > 31)
2080                 return -EINVAL;
2081
2082         value = simple_strtoul(buffer, NULL, 10);
2083         if (sony_pic_set_fanspeed(value))
2084                 return -EIO;
2085
2086         return count;
2087 }
2088
2089 static ssize_t sony_pic_fanspeed_show(struct device *dev,
2090                 struct device_attribute *attr, char *buffer)
2091 {
2092         u8 value = 0;
2093         if (sony_pic_get_fanspeed(&value))
2094                 return -EIO;
2095
2096         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
2097 }
2098
2099 #define SPIC_ATTR(_name, _mode)                                 \
2100 struct device_attribute spic_attr_##_name = __ATTR(_name,       \
2101                 _mode, sony_pic_## _name ##_show,               \
2102                 sony_pic_## _name ##_store)
2103
2104 static SPIC_ATTR(bluetoothpower, 0644);
2105 static SPIC_ATTR(wwanpower, 0644);
2106 static SPIC_ATTR(fanspeed, 0644);
2107
2108 static struct attribute *spic_attributes[] = {
2109         &spic_attr_bluetoothpower.attr,
2110         &spic_attr_wwanpower.attr,
2111         &spic_attr_fanspeed.attr,
2112         NULL
2113 };
2114
2115 static struct attribute_group spic_attribute_group = {
2116         .attrs = spic_attributes
2117 };
2118
2119 /******** SONYPI compatibility **********/
2120 #ifdef CONFIG_SONYPI_COMPAT
2121
2122 /* battery / brightness / temperature  addresses */
2123 #define SONYPI_BAT_FLAGS        0x81
2124 #define SONYPI_LCD_LIGHT        0x96
2125 #define SONYPI_BAT1_PCTRM       0xa0
2126 #define SONYPI_BAT1_LEFT        0xa2
2127 #define SONYPI_BAT1_MAXRT       0xa4
2128 #define SONYPI_BAT2_PCTRM       0xa8
2129 #define SONYPI_BAT2_LEFT        0xaa
2130 #define SONYPI_BAT2_MAXRT       0xac
2131 #define SONYPI_BAT1_MAXTK       0xb0
2132 #define SONYPI_BAT1_FULL        0xb2
2133 #define SONYPI_BAT2_MAXTK       0xb8
2134 #define SONYPI_BAT2_FULL        0xba
2135 #define SONYPI_TEMP_STATUS      0xC1
2136
2137 struct sonypi_compat_s {
2138         struct fasync_struct    *fifo_async;
2139         struct kfifo            fifo;
2140         spinlock_t              fifo_lock;
2141         wait_queue_head_t       fifo_proc_list;
2142         atomic_t                open_count;
2143 };
2144 static struct sonypi_compat_s sonypi_compat = {
2145         .open_count = ATOMIC_INIT(0),
2146 };
2147
2148 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
2149 {
2150         return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
2151 }
2152
2153 static int sonypi_misc_release(struct inode *inode, struct file *file)
2154 {
2155         atomic_dec(&sonypi_compat.open_count);
2156         return 0;
2157 }
2158
2159 static int sonypi_misc_open(struct inode *inode, struct file *file)
2160 {
2161         /* Flush input queue on first open */
2162         unsigned long flags;
2163
2164         spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
2165
2166         if (atomic_inc_return(&sonypi_compat.open_count) == 1)
2167                 kfifo_reset(&sonypi_compat.fifo);
2168
2169         spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
2170
2171         return 0;
2172 }
2173
2174 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
2175                                 size_t count, loff_t *pos)
2176 {
2177         ssize_t ret;
2178         unsigned char c;
2179
2180         if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
2181             (file->f_flags & O_NONBLOCK))
2182                 return -EAGAIN;
2183
2184         ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
2185                                        kfifo_len(&sonypi_compat.fifo) != 0);
2186         if (ret)
2187                 return ret;
2188
2189         while (ret < count &&
2190                (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
2191                           &sonypi_compat.fifo_lock) == sizeof(c))) {
2192                 if (put_user(c, buf++))
2193                         return -EFAULT;
2194                 ret++;
2195         }
2196
2197         if (ret > 0) {
2198                 struct inode *inode = file->f_path.dentry->d_inode;
2199                 inode->i_atime = current_fs_time(inode->i_sb);
2200         }
2201
2202         return ret;
2203 }
2204
2205 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
2206 {
2207         poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
2208         if (kfifo_len(&sonypi_compat.fifo))
2209                 return POLLIN | POLLRDNORM;
2210         return 0;
2211 }
2212
2213 static int ec_read16(u8 addr, u16 *value)
2214 {
2215         u8 val_lb, val_hb;
2216         if (ec_read(addr, &val_lb))
2217                 return -1;
2218         if (ec_read(addr + 1, &val_hb))
2219                 return -1;
2220         *value = val_lb | (val_hb << 8);
2221         return 0;
2222 }
2223
2224 static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
2225                                                         unsigned long arg)
2226 {
2227         int ret = 0;
2228         void __user *argp = (void __user *)arg;
2229         u8 val8;
2230         u16 val16;
2231         int value;
2232
2233         mutex_lock(&spic_dev.lock);
2234         switch (cmd) {
2235         case SONYPI_IOCGBRT:
2236                 if (sony_backlight_device == NULL) {
2237                         ret = -EIO;
2238                         break;
2239                 }
2240                 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
2241                         ret = -EIO;
2242                         break;
2243                 }
2244                 val8 = ((value & 0xff) - 1) << 5;
2245                 if (copy_to_user(argp, &val8, sizeof(val8)))
2246                                 ret = -EFAULT;
2247                 break;
2248         case SONYPI_IOCSBRT:
2249                 if (sony_backlight_device == NULL) {
2250                         ret = -EIO;
2251                         break;
2252                 }
2253                 if (copy_from_user(&val8, argp, sizeof(val8))) {
2254                         ret = -EFAULT;
2255                         break;
2256                 }
2257                 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
2258                                 (val8 >> 5) + 1, NULL)) {
2259                         ret = -EIO;
2260                         break;
2261                 }
2262                 /* sync the backlight device status */
2263                 sony_backlight_device->props.brightness =
2264                     sony_backlight_get_brightness(sony_backlight_device);
2265                 break;
2266         case SONYPI_IOCGBAT1CAP:
2267                 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
2268                         ret = -EIO;
2269                         break;
2270                 }
2271                 if (copy_to_user(argp, &val16, sizeof(val16)))
2272                         ret = -EFAULT;
2273                 break;
2274         case SONYPI_IOCGBAT1REM:
2275                 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
2276                         ret = -EIO;
2277                         break;
2278                 }
2279                 if (copy_to_user(argp, &val16, sizeof(val16)))
2280                         ret = -EFAULT;
2281                 break;
2282         case SONYPI_IOCGBAT2CAP:
2283                 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
2284                         ret = -EIO;
2285                         break;
2286                 }
2287                 if (copy_to_user(argp, &val16, sizeof(val16)))
2288                         ret = -EFAULT;
2289                 break;
2290         case SONYPI_IOCGBAT2REM:
2291                 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
2292                         ret = -EIO;
2293                         break;
2294                 }
2295                 if (copy_to_user(argp, &val16, sizeof(val16)))
2296                         ret = -EFAULT;
2297                 break;
2298         case SONYPI_IOCGBATFLAGS:
2299                 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
2300                         ret = -EIO;
2301                         break;
2302                 }
2303                 val8 &= 0x07;
2304                 if (copy_to_user(argp, &val8, sizeof(val8)))
2305                         ret = -EFAULT;
2306                 break;
2307         case SONYPI_IOCGBLUE:
2308                 val8 = spic_dev.bluetooth_power;
2309                 if (copy_to_user(argp, &val8, sizeof(val8)))
2310                         ret = -EFAULT;
2311                 break;
2312         case SONYPI_IOCSBLUE:
2313                 if (copy_from_user(&val8, argp, sizeof(val8))) {
2314                         ret = -EFAULT;
2315                         break;
2316                 }
2317                 __sony_pic_set_bluetoothpower(val8);
2318                 break;
2319         /* FAN Controls */
2320         case SONYPI_IOCGFAN:
2321                 if (sony_pic_get_fanspeed(&val8)) {
2322                         ret = -EIO;
2323                         break;
2324                 }
2325                 if (copy_to_user(argp, &val8, sizeof(val8)))
2326                         ret = -EFAULT;
2327                 break;
2328         case SONYPI_IOCSFAN:
2329                 if (copy_from_user(&val8, argp, sizeof(val8))) {
2330                         ret = -EFAULT;
2331                         break;
2332                 }
2333                 if (sony_pic_set_fanspeed(val8))
2334                         ret = -EIO;
2335                 break;
2336         /* GET Temperature (useful under APM) */
2337         case SONYPI_IOCGTEMP:
2338                 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
2339                         ret = -EIO;
2340                         break;
2341                 }
2342                 if (copy_to_user(argp, &val8, sizeof(val8)))
2343                         ret = -EFAULT;
2344                 break;
2345         default:
2346                 ret = -EINVAL;
2347         }
2348         mutex_unlock(&spic_dev.lock);
2349         return ret;
2350 }
2351
2352 static const struct file_operations sonypi_misc_fops = {
2353         .owner          = THIS_MODULE,
2354         .read           = sonypi_misc_read,
2355         .poll           = sonypi_misc_poll,
2356         .open           = sonypi_misc_open,
2357         .release        = sonypi_misc_release,
2358         .fasync         = sonypi_misc_fasync,
2359         .unlocked_ioctl = sonypi_misc_ioctl,
2360 };
2361
2362 static struct miscdevice sonypi_misc_device = {
2363         .minor          = MISC_DYNAMIC_MINOR,
2364         .name           = "sonypi",
2365         .fops           = &sonypi_misc_fops,
2366 };
2367
2368 static void sonypi_compat_report_event(u8 event)
2369 {
2370         kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
2371                         sizeof(event), &sonypi_compat.fifo_lock);
2372         kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
2373         wake_up_interruptible(&sonypi_compat.fifo_proc_list);
2374 }
2375
2376 static int sonypi_compat_init(void)
2377 {
2378         int error;
2379
2380         spin_lock_init(&sonypi_compat.fifo_lock);
2381         error =
2382          kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
2383         if (error) {
2384                 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
2385                 return error;
2386         }
2387
2388         init_waitqueue_head(&sonypi_compat.fifo_proc_list);
2389
2390         if (minor != -1)
2391                 sonypi_misc_device.minor = minor;
2392         error = misc_register(&sonypi_misc_device);
2393         if (error) {
2394                 printk(KERN_ERR DRV_PFX "misc_register failed\n");
2395                 goto err_free_kfifo;
2396         }
2397         if (minor == -1)
2398                 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
2399                        sonypi_misc_device.minor);
2400
2401         return 0;
2402
2403 err_free_kfifo:
2404         kfifo_free(&sonypi_compat.fifo);
2405         return error;
2406 }
2407
2408 static void sonypi_compat_exit(void)
2409 {
2410         misc_deregister(&sonypi_misc_device);
2411         kfifo_free(&sonypi_compat.fifo);
2412 }
2413 #else
2414 static int sonypi_compat_init(void) { return 0; }
2415 static void sonypi_compat_exit(void) { }
2416 static void sonypi_compat_report_event(u8 event) { }
2417 #endif /* CONFIG_SONYPI_COMPAT */
2418
2419 /*
2420  * ACPI callbacks
2421  */
2422 static acpi_status
2423 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
2424 {
2425         u32 i;
2426         struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
2427
2428         switch (resource->type) {
2429         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
2430                 {
2431                         /* start IO enumeration */
2432                         struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
2433                         if (!ioport)
2434                                 return AE_ERROR;
2435
2436                         list_add(&ioport->list, &dev->ioports);
2437                         return AE_OK;
2438                 }
2439
2440         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
2441                 /* end IO enumeration */
2442                 return AE_OK;
2443
2444         case ACPI_RESOURCE_TYPE_IRQ:
2445                 {
2446                         struct acpi_resource_irq *p = &resource->data.irq;
2447                         struct sony_pic_irq *interrupt = NULL;
2448                         if (!p || !p->interrupt_count) {
2449                                 /*
2450                                  * IRQ descriptors may have no IRQ# bits set,
2451                                  * particularly those those w/ _STA disabled
2452                                  */
2453                                 dprintk("Blank IRQ resource\n");
2454                                 return AE_OK;
2455                         }
2456                         for (i = 0; i < p->interrupt_count; i++) {
2457                                 if (!p->interrupts[i]) {
2458                                         printk(KERN_WARNING DRV_PFX
2459                                                         "Invalid IRQ %d\n",
2460                                                         p->interrupts[i]);
2461                                         continue;
2462                                 }
2463                                 interrupt = kzalloc(sizeof(*interrupt),
2464                                                 GFP_KERNEL);
2465                                 if (!interrupt)
2466                                         return AE_ERROR;
2467
2468                                 list_add(&interrupt->list, &dev->interrupts);
2469                                 interrupt->irq.triggering = p->triggering;
2470                                 interrupt->irq.polarity = p->polarity;
2471                                 interrupt->irq.sharable = p->sharable;
2472                                 interrupt->irq.interrupt_count = 1;
2473                                 interrupt->irq.interrupts[0] = p->interrupts[i];
2474                         }
2475                         return AE_OK;
2476                 }
2477         case ACPI_RESOURCE_TYPE_IO:
2478                 {
2479                         struct acpi_resource_io *io = &resource->data.io;
2480                         struct sony_pic_ioport *ioport =
2481                                 list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
2482                         if (!io) {
2483                                 dprintk("Blank IO resource\n");
2484                                 return AE_OK;
2485                         }
2486
2487                         if (!ioport->io1.minimum) {
2488                                 memcpy(&ioport->io1, io, sizeof(*io));
2489                                 dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
2490                                                 ioport->io1.address_length);
2491                         }
2492                         else if (!ioport->io2.minimum) {
2493                                 memcpy(&ioport->io2, io, sizeof(*io));
2494                                 dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
2495                                                 ioport->io2.address_length);
2496                         }
2497                         else {
2498                                 printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
2499                                 return AE_ERROR;
2500                         }
2501                         return AE_OK;
2502                 }
2503         default:
2504                 dprintk("Resource %d isn't an IRQ nor an IO port\n",
2505                                 resource->type);
2506
2507         case ACPI_RESOURCE_TYPE_END_TAG:
2508                 return AE_OK;
2509         }
2510         return AE_CTRL_TERMINATE;
2511 }
2512
2513 static int sony_pic_possible_resources(struct acpi_device *device)
2514 {
2515         int result = 0;
2516         acpi_status status = AE_OK;
2517
2518         if (!device)
2519                 return -EINVAL;
2520
2521         /* get device status */
2522         /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
2523         dprintk("Evaluating _STA\n");
2524         result = acpi_bus_get_status(device);
2525         if (result) {
2526                 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
2527                 goto end;
2528         }
2529
2530         if (!device->status.enabled)
2531                 dprintk("Device disabled\n");
2532         else
2533                 dprintk("Device enabled\n");
2534
2535         /*
2536          * Query and parse 'method'
2537          */
2538         dprintk("Evaluating %s\n", METHOD_NAME__PRS);
2539         status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
2540                         sony_pic_read_possible_resource, &spic_dev);
2541         if (ACPI_FAILURE(status)) {
2542                 printk(KERN_WARNING DRV_PFX
2543                                 "Failure evaluating %s\n",
2544                                 METHOD_NAME__PRS);
2545                 result = -ENODEV;
2546         }
2547 end:
2548         return result;
2549 }
2550
2551 /*
2552  *  Disable the spic device by calling its _DIS method
2553  */
2554 static int sony_pic_disable(struct acpi_device *device)
2555 {
2556         acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
2557                                                NULL);
2558
2559         if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
2560                 return -ENXIO;
2561
2562         dprintk("Device disabled\n");
2563         return 0;
2564 }
2565
2566
2567 /*
2568  *  Based on drivers/acpi/pci_link.c:acpi_pci_link_set
2569  *
2570  *  Call _SRS to set current resources
2571  */
2572 static int sony_pic_enable(struct acpi_device *device,
2573                 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
2574 {
2575         acpi_status status;
2576         int result = 0;
2577         /* Type 1 resource layout is:
2578          *    IO
2579          *    IO
2580          *    IRQNoFlags
2581          *    End
2582          *
2583          * Type 2 and 3 resource layout is:
2584          *    IO
2585          *    IRQNoFlags
2586          *    End
2587          */
2588         struct {
2589                 struct acpi_resource res1;
2590                 struct acpi_resource res2;
2591                 struct acpi_resource res3;
2592                 struct acpi_resource res4;
2593         } *resource;
2594         struct acpi_buffer buffer = { 0, NULL };
2595
2596         if (!ioport || !irq)
2597                 return -EINVAL;
2598
2599         /* init acpi_buffer */
2600         resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
2601         if (!resource)
2602                 return -ENOMEM;
2603
2604         buffer.length = sizeof(*resource) + 1;
2605         buffer.pointer = resource;
2606
2607         /* setup Type 1 resources */
2608         if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
2609
2610                 /* setup io resources */
2611                 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2612                 resource->res1.length = sizeof(struct acpi_resource);
2613                 memcpy(&resource->res1.data.io, &ioport->io1,
2614                                 sizeof(struct acpi_resource_io));
2615
2616                 resource->res2.type = ACPI_RESOURCE_TYPE_IO;
2617                 resource->res2.length = sizeof(struct acpi_resource);
2618                 memcpy(&resource->res2.data.io, &ioport->io2,
2619                                 sizeof(struct acpi_resource_io));
2620
2621                 /* setup irq resource */
2622                 resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
2623                 resource->res3.length = sizeof(struct acpi_resource);
2624                 memcpy(&resource->res3.data.irq, &irq->irq,
2625                                 sizeof(struct acpi_resource_irq));
2626                 /* we requested a shared irq */
2627                 resource->res3.data.irq.sharable = ACPI_SHARED;
2628
2629                 resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
2630
2631         }
2632         /* setup Type 2/3 resources */
2633         else {
2634                 /* setup io resource */
2635                 resource->res1.type = ACPI_RESOURCE_TYPE_IO;
2636                 resource->res1.length = sizeof(struct acpi_resource);
2637                 memcpy(&resource->res1.data.io, &ioport->io1,
2638                                 sizeof(struct acpi_resource_io));
2639
2640                 /* setup irq resource */
2641                 resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
2642                 resource->res2.length = sizeof(struct acpi_resource);
2643                 memcpy(&resource->res2.data.irq, &irq->irq,
2644                                 sizeof(struct acpi_resource_irq));
2645                 /* we requested a shared irq */
2646                 resource->res2.data.irq.sharable = ACPI_SHARED;
2647
2648                 resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
2649         }
2650
2651         /* Attempt to set the resource */
2652         dprintk("Evaluating _SRS\n");
2653         status = acpi_set_current_resources(device->handle, &buffer);
2654
2655         /* check for total failure */
2656         if (ACPI_FAILURE(status)) {
2657                 printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
2658                 result = -ENODEV;
2659                 goto end;
2660         }
2661
2662         /* Necessary device initializations calls (from sonypi) */
2663         sony_pic_call1(0x82);
2664         sony_pic_call2(0x81, 0xff);
2665         sony_pic_call1(compat ? 0x92 : 0x82);
2666
2667 end:
2668         kfree(resource);
2669         return result;
2670 }
2671
2672 /*****************
2673  *
2674  * ISR: some event is available
2675  *
2676  *****************/
2677 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2678 {
2679         int i, j;
2680         u8 ev = 0;
2681         u8 data_mask = 0;
2682         u8 device_event = 0;
2683
2684         struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2685
2686         ev = inb_p(dev->cur_ioport->io1.minimum);
2687         if (dev->cur_ioport->io2.minimum)
2688                 data_mask = inb_p(dev->cur_ioport->io2.minimum);
2689         else
2690                 data_mask = inb_p(dev->cur_ioport->io1.minimum +
2691                                 dev->evport_offset);
2692
2693         dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2694                         ev, data_mask, dev->cur_ioport->io1.minimum,
2695                         dev->evport_offset);
2696
2697         if (ev == 0x00 || ev == 0xff)
2698                 return IRQ_HANDLED;
2699
2700         for (i = 0; dev->event_types[i].mask; i++) {
2701
2702                 if ((data_mask & dev->event_types[i].data) !=
2703                     dev->event_types[i].data)
2704                         continue;
2705
2706                 if (!(mask & dev->event_types[i].mask))
2707                         continue;
2708
2709                 for (j = 0; dev->event_types[i].events[j].event; j++) {
2710                         if (ev == dev->event_types[i].events[j].data) {
2711                                 device_event =
2712                                         dev->event_types[i].events[j].event;
2713                                 goto found;
2714                         }
2715                 }
2716         }
2717         /* Still not able to decode the event try to pass
2718          * it over to the minidriver
2719          */
2720         if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
2721                 return IRQ_HANDLED;
2722
2723         dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
2724                         ev, data_mask, dev->cur_ioport->io1.minimum,
2725                         dev->evport_offset);
2726         return IRQ_HANDLED;
2727
2728 found:
2729         sony_laptop_report_input_event(device_event);
2730         acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
2731         sonypi_compat_report_event(device_event);
2732
2733         return IRQ_HANDLED;
2734 }
2735
2736 /*****************
2737  *
2738  *  ACPI driver
2739  *
2740  *****************/
2741 static int sony_pic_remove(struct acpi_device *device, int type)
2742 {
2743         struct sony_pic_ioport *io, *tmp_io;
2744         struct sony_pic_irq *irq, *tmp_irq;
2745
2746         if (sony_pic_disable(device)) {
2747                 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2748                 return -ENXIO;
2749         }
2750
2751         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2752         release_region(spic_dev.cur_ioport->io1.minimum,
2753                         spic_dev.cur_ioport->io1.address_length);
2754         if (spic_dev.cur_ioport->io2.minimum)
2755                 release_region(spic_dev.cur_ioport->io2.minimum,
2756                                 spic_dev.cur_ioport->io2.address_length);
2757
2758         sonypi_compat_exit();
2759
2760         sony_laptop_remove_input();
2761
2762         /* pf attrs */
2763         sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2764         sony_pf_remove();
2765
2766         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2767                 list_del(&io->list);
2768                 kfree(io);
2769         }
2770         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2771                 list_del(&irq->list);
2772                 kfree(irq);
2773         }
2774         spic_dev.cur_ioport = NULL;
2775         spic_dev.cur_irq = NULL;
2776
2777         dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2778         return 0;
2779 }
2780
2781 static int sony_pic_add(struct acpi_device *device)
2782 {
2783         int result;
2784         struct sony_pic_ioport *io, *tmp_io;
2785         struct sony_pic_irq *irq, *tmp_irq;
2786
2787         printk(KERN_INFO DRV_PFX "%s v%s.\n",
2788                 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2789
2790         spic_dev.acpi_dev = device;
2791         strcpy(acpi_device_class(device), "sony/hotkey");
2792         sony_pic_detect_device_type(&spic_dev);
2793         mutex_init(&spic_dev.lock);
2794
2795         /* read _PRS resources */
2796         result = sony_pic_possible_resources(device);
2797         if (result) {
2798                 printk(KERN_ERR DRV_PFX
2799                                 "Unable to read possible resources.\n");
2800                 goto err_free_resources;
2801         }
2802
2803         /* setup input devices and helper fifo */
2804         result = sony_laptop_setup_input(device);
2805         if (result) {
2806                 printk(KERN_ERR DRV_PFX
2807                                 "Unable to create input devices.\n");
2808                 goto err_free_resources;
2809         }
2810
2811         if (sonypi_compat_init())
2812                 goto err_remove_input;
2813
2814         /* request io port */
2815         list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
2816                 if (request_region(io->io1.minimum, io->io1.address_length,
2817                                         "Sony Programable I/O Device")) {
2818                         dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
2819                                         io->io1.minimum, io->io1.maximum,
2820                                         io->io1.address_length);
2821                         /* Type 1 have 2 ioports */
2822                         if (io->io2.minimum) {
2823                                 if (request_region(io->io2.minimum,
2824                                                 io->io2.address_length,
2825                                                 "Sony Programable I/O Device")) {
2826                                         dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
2827                                                         io->io2.minimum, io->io2.maximum,
2828                                                         io->io2.address_length);
2829                                         spic_dev.cur_ioport = io;
2830                                         break;
2831                                 }
2832                                 else {
2833                                         dprintk("Unable to get I/O port2: "
2834                                                         "0x%.4x (0x%.4x) + 0x%.2x\n",
2835                                                         io->io2.minimum, io->io2.maximum,
2836                                                         io->io2.address_length);
2837                                         release_region(io->io1.minimum,
2838                                                         io->io1.address_length);
2839                                 }
2840                         }
2841                         else {
2842                                 spic_dev.cur_ioport = io;
2843                                 break;
2844                         }
2845                 }
2846         }
2847         if (!spic_dev.cur_ioport) {
2848                 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2849                 result = -ENODEV;
2850                 goto err_remove_compat;
2851         }
2852
2853         /* request IRQ */
2854         list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
2855                 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2856                                         IRQF_DISABLED, "sony-laptop", &spic_dev)) {
2857                         dprintk("IRQ: %d - triggering: %d - "
2858                                         "polarity: %d - shr: %d\n",
2859                                         irq->irq.interrupts[0],
2860                                         irq->irq.triggering,
2861                                         irq->irq.polarity,
2862                                         irq->irq.sharable);
2863                         spic_dev.cur_irq = irq;
2864                         break;
2865                 }
2866         }
2867         if (!spic_dev.cur_irq) {
2868                 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2869                 result = -ENODEV;
2870                 goto err_release_region;
2871         }
2872
2873         /* set resource status _SRS */
2874         result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2875         if (result) {
2876                 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2877                 goto err_free_irq;
2878         }
2879
2880         spic_dev.bluetooth_power = -1;
2881         /* create device attributes */
2882         result = sony_pf_add();
2883         if (result)
2884                 goto err_disable_device;
2885
2886         result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2887         if (result)
2888                 goto err_remove_pf;
2889
2890         return 0;
2891
2892 err_remove_pf:
2893         sony_pf_remove();
2894
2895 err_disable_device:
2896         sony_pic_disable(device);
2897
2898 err_free_irq:
2899         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2900
2901 err_release_region:
2902         release_region(spic_dev.cur_ioport->io1.minimum,
2903                         spic_dev.cur_ioport->io1.address_length);
2904         if (spic_dev.cur_ioport->io2.minimum)
2905                 release_region(spic_dev.cur_ioport->io2.minimum,
2906                                 spic_dev.cur_ioport->io2.address_length);
2907
2908 err_remove_compat:
2909         sonypi_compat_exit();
2910
2911 err_remove_input:
2912         sony_laptop_remove_input();
2913
2914 err_free_resources:
2915         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2916                 list_del(&io->list);
2917                 kfree(io);
2918         }
2919         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2920                 list_del(&irq->list);
2921                 kfree(irq);
2922         }
2923         spic_dev.cur_ioport = NULL;
2924         spic_dev.cur_irq = NULL;
2925
2926         return result;
2927 }
2928
2929 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2930 {
2931         if (sony_pic_disable(device))
2932                 return -ENXIO;
2933         return 0;
2934 }
2935
2936 static int sony_pic_resume(struct acpi_device *device)
2937 {
2938         sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2939         return 0;
2940 }
2941
2942 static const struct acpi_device_id sony_pic_device_ids[] = {
2943         {SONY_PIC_HID, 0},
2944         {"", 0},
2945 };
2946
2947 static struct acpi_driver sony_pic_driver = {
2948         .name = SONY_PIC_DRIVER_NAME,
2949         .class = SONY_PIC_CLASS,
2950         .ids = sony_pic_device_ids,
2951         .owner = THIS_MODULE,
2952         .ops = {
2953                 .add = sony_pic_add,
2954                 .remove = sony_pic_remove,
2955                 .suspend = sony_pic_suspend,
2956                 .resume = sony_pic_resume,
2957                 },
2958 };
2959
2960 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2961         {
2962                 .ident = "Sony Vaio",
2963                 .matches = {
2964                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2965                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2966                 },
2967         },
2968         {
2969                 .ident = "Sony Vaio",
2970                 .matches = {
2971                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2972                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2973                 },
2974         },
2975         { }
2976 };
2977
2978 static int __init sony_laptop_init(void)
2979 {
2980         int result;
2981
2982         if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2983                 result = acpi_bus_register_driver(&sony_pic_driver);
2984                 if (result) {
2985                         printk(KERN_ERR DRV_PFX
2986                                         "Unable to register SPIC driver.");
2987                         goto out;
2988                 }
2989                 spic_drv_registered = 1;
2990         }
2991
2992         result = acpi_bus_register_driver(&sony_nc_driver);
2993         if (result) {
2994                 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2995                 goto out_unregister_pic;
2996         }
2997
2998         return 0;
2999
3000 out_unregister_pic:
3001         if (spic_drv_registered)
3002                 acpi_bus_unregister_driver(&sony_pic_driver);
3003 out:
3004         return result;
3005 }
3006
3007 static void __exit sony_laptop_exit(void)
3008 {
3009         acpi_bus_unregister_driver(&sony_nc_driver);
3010         if (spic_drv_registered)
3011                 acpi_bus_unregister_driver(&sony_pic_driver);
3012 }
3013
3014 module_init(sony_laptop_init);
3015 module_exit(sony_laptop_exit);