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