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