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