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