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