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