sony-laptop: Unify the input subsystem event forwarding
[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/types.h>
50 #include <linux/backlight.h>
51 #include <linux/platform_device.h>
52 #include <linux/err.h>
53 #include <linux/dmi.h>
54 #include <linux/pci.h>
55 #include <linux/interrupt.h>
56 #include <linux/delay.h>
57 #include <linux/input.h>
58 #include <linux/kfifo.h>
59 #include <linux/workqueue.h>
60 #include <linux/acpi.h>
61 #include <acpi/acpi_drivers.h>
62 #include <acpi/acpi_bus.h>
63 #include <asm/uaccess.h>
64 #include <linux/sonypi.h>
65
66 #define DRV_PFX                 "sony-laptop: "
67 #define LOG_PFX                 KERN_WARNING DRV_PFX
68 #define dprintk(msg...)         do {                    \
69         if (debug) printk(LOG_PFX  msg);                \
70 } while (0)
71
72 #define SONY_LAPTOP_DRIVER_VERSION      "0.5"
73
74 #define SONY_NC_CLASS           "sony-nc"
75 #define SONY_NC_HID             "SNY5001"
76 #define SONY_NC_DRIVER_NAME     "Sony Notebook Control"
77
78 #define SONY_PIC_CLASS          "sony-pic"
79 #define SONY_PIC_HID            "SNY6001"
80 #define SONY_PIC_DRIVER_NAME    "Sony Programmable IO Control"
81
82 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
83 MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
84 MODULE_LICENSE("GPL");
85 MODULE_VERSION(SONY_LAPTOP_DRIVER_VERSION);
86
87 static int debug;
88 module_param(debug, int, 0);
89 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
90                  "the development of this driver");
91
92 static int no_spic;             /* = 0 */
93 module_param(no_spic, int, 0444);
94 MODULE_PARM_DESC(no_spic,
95                  "set this if you don't want to enable the SPIC device");
96
97 static int compat;              /* = 0 */
98 module_param(compat, int, 0444);
99 MODULE_PARM_DESC(compat,
100                  "set this if you want to enable backward compatibility mode for SPIC");
101
102 static unsigned long mask = 0xffffffff;
103 module_param(mask, ulong, 0644);
104 MODULE_PARM_DESC(mask,
105                  "set this to the mask of event you want to enable (see doc)");
106
107 /*********** Input Devices ***********/
108
109 #define SONY_LAPTOP_BUF_SIZE    128
110 struct sony_laptop_input_s {
111         atomic_t                users;
112         struct input_dev        *jog_dev;
113         struct input_dev        *key_dev;
114         struct kfifo            *fifo;
115         spinlock_t              fifo_lock;
116         struct workqueue_struct *wq;
117 };
118 static struct sony_laptop_input_s sony_laptop_input = {
119         .users = ATOMIC_INIT(0),
120 };
121
122 struct sony_laptop_keypress {
123         struct input_dev *dev;
124         int key;
125 };
126
127 /* Correspondance table between sonypi events and input layer events */
128 static struct {
129         int sonypiev;
130         int inputev;
131 } sony_laptop_inputkeys[] = {
132         { SONYPI_EVENT_CAPTURE_PRESSED,         KEY_CAMERA },
133         { SONYPI_EVENT_FNKEY_ONLY,              KEY_FN },
134         { SONYPI_EVENT_FNKEY_ESC,               KEY_FN_ESC },
135         { SONYPI_EVENT_FNKEY_F1,                KEY_FN_F1 },
136         { SONYPI_EVENT_FNKEY_F2,                KEY_FN_F2 },
137         { SONYPI_EVENT_FNKEY_F3,                KEY_FN_F3 },
138         { SONYPI_EVENT_FNKEY_F4,                KEY_FN_F4 },
139         { SONYPI_EVENT_FNKEY_F5,                KEY_FN_F5 },
140         { SONYPI_EVENT_FNKEY_F6,                KEY_FN_F6 },
141         { SONYPI_EVENT_FNKEY_F7,                KEY_FN_F7 },
142         { SONYPI_EVENT_FNKEY_F8,                KEY_FN_F8 },
143         { SONYPI_EVENT_FNKEY_F9,                KEY_FN_F9 },
144         { SONYPI_EVENT_FNKEY_F10,               KEY_FN_F10 },
145         { SONYPI_EVENT_FNKEY_F11,               KEY_FN_F11 },
146         { SONYPI_EVENT_FNKEY_F12,               KEY_FN_F12 },
147         { SONYPI_EVENT_FNKEY_1,                 KEY_FN_1 },
148         { SONYPI_EVENT_FNKEY_2,                 KEY_FN_2 },
149         { SONYPI_EVENT_FNKEY_D,                 KEY_FN_D },
150         { SONYPI_EVENT_FNKEY_E,                 KEY_FN_E },
151         { SONYPI_EVENT_FNKEY_F,                 KEY_FN_F },
152         { SONYPI_EVENT_FNKEY_S,                 KEY_FN_S },
153         { SONYPI_EVENT_FNKEY_B,                 KEY_FN_B },
154         { SONYPI_EVENT_BLUETOOTH_PRESSED,       KEY_BLUE },
155         { SONYPI_EVENT_BLUETOOTH_ON,            KEY_BLUE },
156         { SONYPI_EVENT_PKEY_P1,                 KEY_PROG1 },
157         { SONYPI_EVENT_PKEY_P2,                 KEY_PROG2 },
158         { SONYPI_EVENT_PKEY_P3,                 KEY_PROG3 },
159         { SONYPI_EVENT_BACK_PRESSED,            KEY_BACK },
160         { SONYPI_EVENT_HELP_PRESSED,            KEY_HELP },
161         { SONYPI_EVENT_ZOOM_PRESSED,            KEY_ZOOM },
162         { SONYPI_EVENT_THUMBPHRASE_PRESSED,     BTN_THUMB },
163         { 0, 0 },
164 };
165
166 /* release buttons after a short delay if pressed */
167 static void do_sony_laptop_release_key(struct work_struct *work)
168 {
169         struct sony_laptop_keypress kp;
170
171         while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
172                          sizeof(kp)) == sizeof(kp)) {
173                 msleep(10);
174                 input_report_key(kp.dev, kp.key, 0);
175                 input_sync(kp.dev);
176         }
177 }
178 static DECLARE_WORK(sony_laptop_release_key_work,
179                 do_sony_laptop_release_key);
180
181 /* forward event to the input subsytem */
182 static void sony_laptop_report_input_event(u8 event)
183 {
184         struct input_dev *jog_dev = sony_laptop_input.jog_dev;
185         struct input_dev *key_dev = sony_laptop_input.key_dev;
186         struct sony_laptop_keypress kp = { NULL };
187         int i;
188
189         if (event == SONYPI_EVENT_FNKEY_RELEASED) {
190                 /* Nothing, not all VAIOs generate this event */
191                 return;
192         }
193
194         /* report events */
195         switch (event) {
196         /* jog_dev events */
197         case SONYPI_EVENT_JOGDIAL_UP:
198         case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
199                 input_report_rel(jog_dev, REL_WHEEL, 1);
200                 input_sync(jog_dev);
201                 return;
202
203         case SONYPI_EVENT_JOGDIAL_DOWN:
204         case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
205                 input_report_rel(jog_dev, REL_WHEEL, -1);
206                 input_sync(jog_dev);
207                 return;
208
209         /* key_dev events */
210         case SONYPI_EVENT_JOGDIAL_PRESSED:
211                 kp.key = BTN_MIDDLE;
212                 kp.dev = jog_dev;
213                 break;
214
215         default:
216                 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
217                         if (event == sony_laptop_inputkeys[i].sonypiev) {
218                                 kp.dev = key_dev;
219                                 kp.key = sony_laptop_inputkeys[i].inputev;
220                                 break;
221                         }
222                 break;
223         }
224
225         if (kp.dev) {
226                 input_report_key(kp.dev, kp.key, 1);
227                 input_sync(kp.dev);
228                 kfifo_put(sony_laptop_input.fifo,
229                           (unsigned char *)&kp, sizeof(kp));
230
231                 if (!work_pending(&sony_laptop_release_key_work))
232                         queue_work(sony_laptop_input.wq,
233                                         &sony_laptop_release_key_work);
234         } else
235                 dprintk("unknown input event %.2x\n", event);
236 }
237
238 static int sony_laptop_setup_input(void)
239 {
240         struct input_dev *jog_dev;
241         struct input_dev *key_dev;
242         int i;
243         int error;
244
245         /* don't run again if already initialized */
246         if (atomic_add_return(1, &sony_laptop_input.users) > 1)
247                 return 0;
248
249         /* kfifo */
250         spin_lock_init(&sony_laptop_input.fifo_lock);
251         sony_laptop_input.fifo =
252                 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
253                             &sony_laptop_input.fifo_lock);
254         if (IS_ERR(sony_laptop_input.fifo)) {
255                 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
256                 error = PTR_ERR(sony_laptop_input.fifo);
257                 goto err_dec_users;
258         }
259
260         /* init workqueue */
261         sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
262         if (!sony_laptop_input.wq) {
263                 printk(KERN_ERR DRV_PFX
264                                 "Unabe to create workqueue.\n");
265                 error = -ENXIO;
266                 goto err_free_kfifo;
267         }
268
269         /* input keys */
270         key_dev = input_allocate_device();
271         if (!key_dev) {
272                 error = -ENOMEM;
273                 goto err_destroy_wq;
274         }
275
276         key_dev->name = "Sony Vaio Keys";
277         key_dev->id.bustype = BUS_ISA;
278         key_dev->id.vendor = PCI_VENDOR_ID_SONY;
279
280         /* Initialize the Input Drivers: special keys */
281         key_dev->evbit[0] = BIT(EV_KEY);
282         for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
283                 if (sony_laptop_inputkeys[i].inputev)
284                         set_bit(sony_laptop_inputkeys[i].inputev,
285                                         key_dev->keybit);
286
287         error = input_register_device(key_dev);
288         if (error)
289                 goto err_free_keydev;
290
291         sony_laptop_input.key_dev = key_dev;
292
293         /* jogdial */
294         jog_dev = input_allocate_device();
295         if (!jog_dev) {
296                 error = -ENOMEM;
297                 goto err_unregister_keydev;
298         }
299
300         jog_dev->name = "Sony Vaio Jogdial";
301         jog_dev->id.bustype = BUS_ISA;
302         jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
303
304         jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
305         jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
306         jog_dev->relbit[0] = BIT(REL_WHEEL);
307
308         error = input_register_device(jog_dev);
309         if (error)
310                 goto err_free_jogdev;
311
312         sony_laptop_input.jog_dev = jog_dev;
313
314         return 0;
315
316 err_free_jogdev:
317         input_free_device(jog_dev);
318
319 err_unregister_keydev:
320         input_unregister_device(key_dev);
321         /* to avoid kref underflow below at input_free_device */
322         key_dev = NULL;
323
324 err_free_keydev:
325         input_free_device(key_dev);
326
327 err_destroy_wq:
328         destroy_workqueue(sony_laptop_input.wq);
329
330 err_free_kfifo:
331         kfifo_free(sony_laptop_input.fifo);
332
333 err_dec_users:
334         atomic_dec(&sony_laptop_input.users);
335         return error;
336 }
337
338 static void sony_laptop_remove_input(void)
339 {
340         /* cleanup only after the last user has gone */
341         if (!atomic_dec_and_test(&sony_laptop_input.users))
342                 return;
343
344         /* flush workqueue first */
345         flush_workqueue(sony_laptop_input.wq);
346
347         /* destroy input devs */
348         input_unregister_device(sony_laptop_input.key_dev);
349         sony_laptop_input.key_dev = NULL;
350
351         if (sony_laptop_input.jog_dev) {
352                 input_unregister_device(sony_laptop_input.jog_dev);
353                 sony_laptop_input.jog_dev = NULL;
354         }
355
356         destroy_workqueue(sony_laptop_input.wq);
357         kfifo_free(sony_laptop_input.fifo);
358 }
359
360 /*********** Platform Device ***********/
361
362 static atomic_t sony_pf_users = ATOMIC_INIT(0);
363 static struct platform_driver sony_pf_driver = {
364         .driver = {
365                    .name = "sony-laptop",
366                    .owner = THIS_MODULE,
367                    }
368 };
369 static struct platform_device *sony_pf_device;
370
371 static int sony_pf_add(void)
372 {
373         int ret = 0;
374
375         /* don't run again if already initialized */
376         if (atomic_add_return(1, &sony_pf_users) > 1)
377                 return 0;
378
379         ret = platform_driver_register(&sony_pf_driver);
380         if (ret)
381                 goto out;
382
383         sony_pf_device = platform_device_alloc("sony-laptop", -1);
384         if (!sony_pf_device) {
385                 ret = -ENOMEM;
386                 goto out_platform_registered;
387         }
388
389         ret = platform_device_add(sony_pf_device);
390         if (ret)
391                 goto out_platform_alloced;
392
393         return 0;
394
395       out_platform_alloced:
396         platform_device_put(sony_pf_device);
397         sony_pf_device = NULL;
398       out_platform_registered:
399         platform_driver_unregister(&sony_pf_driver);
400       out:
401         atomic_dec(&sony_pf_users);
402         return ret;
403 }
404
405 static void sony_pf_remove(void)
406 {
407         /* deregister only after the last user has gone */
408         if (!atomic_dec_and_test(&sony_pf_users))
409                 return;
410
411         platform_device_del(sony_pf_device);
412         platform_device_put(sony_pf_device);
413         platform_driver_unregister(&sony_pf_driver);
414 }
415
416 /*********** SNC (SNY5001) Device ***********/
417
418 /* the device uses 1-based values, while the backlight subsystem uses
419    0-based values */
420 #define SONY_MAX_BRIGHTNESS     8
421
422 #define SNC_VALIDATE_IN         0
423 #define SNC_VALIDATE_OUT        1
424
425 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
426                               char *);
427 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
428                                const char *, size_t);
429 static int boolean_validate(const int, const int);
430 static int brightness_default_validate(const int, const int);
431
432 struct sony_nc_value {
433         char *name;             /* name of the entry */
434         char **acpiget;         /* names of the ACPI get function */
435         char **acpiset;         /* names of the ACPI set function */
436         int (*validate)(const int, const int);  /* input/output validation */
437         int value;              /* current setting */
438         int valid;              /* Has ever been set */
439         int debug;              /* active only in debug mode ? */
440         struct device_attribute devattr;        /* sysfs atribute */
441 };
442
443 #define SNC_HANDLE_NAMES(_name, _values...) \
444         static char *snc_##_name[] = { _values, NULL }
445
446 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
447         { \
448                 .name           = __stringify(_name), \
449                 .acpiget        = _getters, \
450                 .acpiset        = _setters, \
451                 .validate       = _validate, \
452                 .debug          = _debug, \
453                 .devattr        = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
454         }
455
456 #define SNC_HANDLE_NULL { .name = NULL }
457
458 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
459
460 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
461 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
462
463 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
464 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
465
466 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
467 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
468
469 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
470 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
471
472 SNC_HANDLE_NAMES(PID_get, "GPID");
473
474 SNC_HANDLE_NAMES(CTR_get, "GCTR");
475 SNC_HANDLE_NAMES(CTR_set, "SCTR");
476
477 SNC_HANDLE_NAMES(PCR_get, "GPCR");
478 SNC_HANDLE_NAMES(PCR_set, "SPCR");
479
480 SNC_HANDLE_NAMES(CMI_get, "GCMI");
481 SNC_HANDLE_NAMES(CMI_set, "SCMI");
482
483 static struct sony_nc_value sony_nc_values[] = {
484         SNC_HANDLE(brightness_default, snc_brightness_def_get,
485                         snc_brightness_def_set, brightness_default_validate, 0),
486         SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
487         SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
488         SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
489                         boolean_validate, 0),
490         SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
491                         boolean_validate, 1),
492         /* unknown methods */
493         SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
494         SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
495         SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
496         SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
497         SNC_HANDLE_NULL
498 };
499
500 static acpi_handle sony_nc_acpi_handle;
501 static struct acpi_device *sony_nc_acpi_device = NULL;
502
503 /*
504  * acpi_evaluate_object wrappers
505  */
506 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
507 {
508         struct acpi_buffer output;
509         union acpi_object out_obj;
510         acpi_status status;
511
512         output.length = sizeof(out_obj);
513         output.pointer = &out_obj;
514
515         status = acpi_evaluate_object(handle, name, NULL, &output);
516         if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
517                 *result = out_obj.integer.value;
518                 return 0;
519         }
520
521         printk(LOG_PFX "acpi_callreadfunc failed\n");
522
523         return -1;
524 }
525
526 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
527                             int *result)
528 {
529         struct acpi_object_list params;
530         union acpi_object in_obj;
531         struct acpi_buffer output;
532         union acpi_object out_obj;
533         acpi_status status;
534
535         params.count = 1;
536         params.pointer = &in_obj;
537         in_obj.type = ACPI_TYPE_INTEGER;
538         in_obj.integer.value = value;
539
540         output.length = sizeof(out_obj);
541         output.pointer = &out_obj;
542
543         status = acpi_evaluate_object(handle, name, &params, &output);
544         if (status == AE_OK) {
545                 if (result != NULL) {
546                         if (out_obj.type != ACPI_TYPE_INTEGER) {
547                                 printk(LOG_PFX "acpi_evaluate_object bad "
548                                        "return type\n");
549                                 return -1;
550                         }
551                         *result = out_obj.integer.value;
552                 }
553                 return 0;
554         }
555
556         printk(LOG_PFX "acpi_evaluate_object failed\n");
557
558         return -1;
559 }
560
561 /*
562  * sony_nc_values input/output validate functions
563  */
564
565 /* brightness_default_validate:
566  *
567  * manipulate input output values to keep consistency with the
568  * backlight framework for which brightness values are 0-based.
569  */
570 static int brightness_default_validate(const int direction, const int value)
571 {
572         switch (direction) {
573                 case SNC_VALIDATE_OUT:
574                         return value - 1;
575                 case SNC_VALIDATE_IN:
576                         if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
577                                 return value + 1;
578         }
579         return -EINVAL;
580 }
581
582 /* boolean_validate:
583  *
584  * on input validate boolean values 0/1, on output just pass the
585  * received value.
586  */
587 static int boolean_validate(const int direction, const int value)
588 {
589         if (direction == SNC_VALIDATE_IN) {
590                 if (value != 0 && value != 1)
591                         return -EINVAL;
592         }
593         return value;
594 }
595
596 /*
597  * Sysfs show/store common to all sony_nc_values
598  */
599 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
600                               char *buffer)
601 {
602         int value;
603         struct sony_nc_value *item =
604             container_of(attr, struct sony_nc_value, devattr);
605
606         if (!*item->acpiget)
607                 return -EIO;
608
609         if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
610                 return -EIO;
611
612         if (item->validate)
613                 value = item->validate(SNC_VALIDATE_OUT, value);
614
615         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
616 }
617
618 static ssize_t sony_nc_sysfs_store(struct device *dev,
619                                struct device_attribute *attr,
620                                const char *buffer, size_t count)
621 {
622         int value;
623         struct sony_nc_value *item =
624             container_of(attr, struct sony_nc_value, devattr);
625
626         if (!item->acpiset)
627                 return -EIO;
628
629         if (count > 31)
630                 return -EINVAL;
631
632         value = simple_strtoul(buffer, NULL, 10);
633
634         if (item->validate)
635                 value = item->validate(SNC_VALIDATE_IN, value);
636
637         if (value < 0)
638                 return value;
639
640         if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
641                 return -EIO;
642         item->value = value;
643         item->valid = 1;
644         return count;
645 }
646
647
648 /*
649  * Backlight device
650  */
651 static int sony_backlight_update_status(struct backlight_device *bd)
652 {
653         return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
654                                 bd->props.brightness + 1, NULL);
655 }
656
657 static int sony_backlight_get_brightness(struct backlight_device *bd)
658 {
659         int value;
660
661         if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
662                 return 0;
663         /* brightness levels are 1-based, while backlight ones are 0-based */
664         return value - 1;
665 }
666
667 static struct backlight_device *sony_backlight_device;
668 static struct backlight_ops sony_backlight_ops = {
669         .update_status = sony_backlight_update_status,
670         .get_brightness = sony_backlight_get_brightness,
671 };
672
673 /*
674  * ACPI callbacks
675  */
676 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
677 {
678         dprintk("sony_acpi_notify, event: %d\n", event);
679         sony_laptop_report_input_event(event);
680         acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
681 }
682
683 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
684                                       void *context, void **return_value)
685 {
686         struct acpi_namespace_node *node;
687         union acpi_operand_object *operand;
688
689         node = (struct acpi_namespace_node *)handle;
690         operand = (union acpi_operand_object *)node->object;
691
692         printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
693                (u32) operand->method.param_count);
694
695         return AE_OK;
696 }
697
698 /*
699  * ACPI device
700  */
701 static int sony_nc_resume(struct acpi_device *device)
702 {
703         struct sony_nc_value *item;
704
705         for (item = sony_nc_values; item->name; item++) {
706                 int ret;
707
708                 if (!item->valid)
709                         continue;
710                 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
711                                        item->value, NULL);
712                 if (ret < 0) {
713                         printk("%s: %d\n", __FUNCTION__, ret);
714                         break;
715                 }
716         }
717         return 0;
718 }
719
720 static int sony_nc_add(struct acpi_device *device)
721 {
722         acpi_status status;
723         int result = 0;
724         acpi_handle handle;
725         struct sony_nc_value *item;
726
727         sony_nc_acpi_device = device;
728         strcpy(acpi_device_class(device), "sony/hotkey");
729
730         sony_nc_acpi_handle = device->handle;
731
732         if (debug) {
733                 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
734                                              1, sony_walk_callback, NULL, NULL);
735                 if (ACPI_FAILURE(status)) {
736                         printk(LOG_PFX "unable to walk acpi resources\n");
737                         result = -ENODEV;
738                         goto outwalk;
739                 }
740         }
741
742         /* setup input devices and helper fifo */
743         result = sony_laptop_setup_input();
744         if (result) {
745                 printk(KERN_ERR DRV_PFX
746                                 "Unabe to create input devices.\n");
747                 goto outwalk;
748         }
749
750         status = acpi_install_notify_handler(sony_nc_acpi_handle,
751                                              ACPI_DEVICE_NOTIFY,
752                                              sony_acpi_notify, NULL);
753         if (ACPI_FAILURE(status)) {
754                 printk(LOG_PFX "unable to install notify handler\n");
755                 result = -ENODEV;
756                 goto outinput;
757         }
758
759         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
760                 sony_backlight_device = backlight_device_register("sony", NULL,
761                                                                   NULL,
762                                                                   &sony_backlight_ops);
763
764                 if (IS_ERR(sony_backlight_device)) {
765                         printk(LOG_PFX "unable to register backlight device\n");
766                         sony_backlight_device = NULL;
767                 } else {
768                         sony_backlight_device->props.brightness =
769                             sony_backlight_get_brightness
770                             (sony_backlight_device);
771                         sony_backlight_device->props.max_brightness = 
772                             SONY_MAX_BRIGHTNESS - 1;
773                 }
774
775         }
776
777         if (sony_pf_add())
778                 goto outbacklight;
779
780         /* create sony_pf sysfs attributes related to the SNC device */
781         for (item = sony_nc_values; item->name; ++item) {
782
783                 if (!debug && item->debug)
784                         continue;
785
786                 /* find the available acpiget as described in the DSDT */
787                 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
788                         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
789                                                          *item->acpiget,
790                                                          &handle))) {
791                                 dprintk("Found %s getter: %s\n",
792                                                 item->name, *item->acpiget);
793                                 item->devattr.attr.mode |= S_IRUGO;
794                                 break;
795                         }
796                 }
797
798                 /* find the available acpiset as described in the DSDT */
799                 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
800                         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
801                                                          *item->acpiset,
802                                                          &handle))) {
803                                 dprintk("Found %s setter: %s\n",
804                                                 item->name, *item->acpiset);
805                                 item->devattr.attr.mode |= S_IWUSR;
806                                 break;
807                         }
808                 }
809
810                 if (item->devattr.attr.mode != 0) {
811                         result =
812                             device_create_file(&sony_pf_device->dev,
813                                                &item->devattr);
814                         if (result)
815                                 goto out_sysfs;
816                 }
817         }
818
819         printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
820
821         return 0;
822
823       out_sysfs:
824         for (item = sony_nc_values; item->name; ++item) {
825                 device_remove_file(&sony_pf_device->dev, &item->devattr);
826         }
827         sony_pf_remove();
828
829       outbacklight:
830         if (sony_backlight_device)
831                 backlight_device_unregister(sony_backlight_device);
832
833         status = acpi_remove_notify_handler(sony_nc_acpi_handle,
834                                             ACPI_DEVICE_NOTIFY,
835                                             sony_acpi_notify);
836         if (ACPI_FAILURE(status))
837                 printk(LOG_PFX "unable to remove notify handler\n");
838
839       outinput:
840         sony_laptop_remove_input();
841
842       outwalk:
843         return result;
844 }
845
846 static int sony_nc_remove(struct acpi_device *device, int type)
847 {
848         acpi_status status;
849         struct sony_nc_value *item;
850
851         if (sony_backlight_device)
852                 backlight_device_unregister(sony_backlight_device);
853
854         sony_nc_acpi_device = NULL;
855
856         status = acpi_remove_notify_handler(sony_nc_acpi_handle,
857                                             ACPI_DEVICE_NOTIFY,
858                                             sony_acpi_notify);
859         if (ACPI_FAILURE(status))
860                 printk(LOG_PFX "unable to remove notify handler\n");
861
862         for (item = sony_nc_values; item->name; ++item) {
863                 device_remove_file(&sony_pf_device->dev, &item->devattr);
864         }
865
866         sony_pf_remove();
867         sony_laptop_remove_input();
868
869         printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
870
871         return 0;
872 }
873
874 static struct acpi_driver sony_nc_driver = {
875         .name = SONY_NC_DRIVER_NAME,
876         .class = SONY_NC_CLASS,
877         .ids = SONY_NC_HID,
878         .owner = THIS_MODULE,
879         .ops = {
880                 .add = sony_nc_add,
881                 .remove = sony_nc_remove,
882                 .resume = sony_nc_resume,
883                 },
884 };
885
886 /*********** SPIC (SNY6001) Device ***********/
887
888 #define SONYPI_DEVICE_TYPE1     0x00000001
889 #define SONYPI_DEVICE_TYPE2     0x00000002
890 #define SONYPI_DEVICE_TYPE3     0x00000004
891
892 #define SONY_PIC_EV_MASK        0xff
893
894 struct sony_pic_ioport {
895         struct acpi_resource_io io;
896         struct list_head        list;
897 };
898
899 struct sony_pic_irq {
900         struct acpi_resource_irq        irq;
901         struct list_head                list;
902 };
903
904 struct sony_pic_dev {
905         int                     model;
906         struct acpi_device      *acpi_dev;
907         struct sony_pic_irq     *cur_irq;
908         struct sony_pic_ioport  *cur_ioport;
909         struct list_head        interrupts;
910         struct list_head        ioports;
911 };
912
913 static struct sony_pic_dev spic_dev = {
914         .interrupts     = LIST_HEAD_INIT(spic_dev.interrupts),
915         .ioports        = LIST_HEAD_INIT(spic_dev.ioports),
916 };
917
918 /* Event masks */
919 #define SONYPI_JOGGER_MASK                      0x00000001
920 #define SONYPI_CAPTURE_MASK                     0x00000002
921 #define SONYPI_FNKEY_MASK                       0x00000004
922 #define SONYPI_BLUETOOTH_MASK                   0x00000008
923 #define SONYPI_PKEY_MASK                        0x00000010
924 #define SONYPI_BACK_MASK                        0x00000020
925 #define SONYPI_HELP_MASK                        0x00000040
926 #define SONYPI_LID_MASK                         0x00000080
927 #define SONYPI_ZOOM_MASK                        0x00000100
928 #define SONYPI_THUMBPHRASE_MASK                 0x00000200
929 #define SONYPI_MEYE_MASK                        0x00000400
930 #define SONYPI_MEMORYSTICK_MASK                 0x00000800
931 #define SONYPI_BATTERY_MASK                     0x00001000
932 #define SONYPI_WIRELESS_MASK                    0x00002000
933
934 struct sonypi_event {
935         u8      data;
936         u8      event;
937 };
938
939 /* The set of possible button release events */
940 static struct sonypi_event sonypi_releaseev[] = {
941         { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
942         { 0, 0 }
943 };
944
945 /* The set of possible jogger events  */
946 static struct sonypi_event sonypi_joggerev[] = {
947         { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
948         { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
949         { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
950         { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
951         { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
952         { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
953         { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
954         { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
955         { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
956         { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
957         { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
958         { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
959         { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
960         { 0, 0 }
961 };
962
963 /* The set of possible capture button events */
964 static struct sonypi_event sonypi_captureev[] = {
965         { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
966         { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
967         { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
968         { 0, 0 }
969 };
970
971 /* The set of possible fnkeys events */
972 static struct sonypi_event sonypi_fnkeyev[] = {
973         { 0x10, SONYPI_EVENT_FNKEY_ESC },
974         { 0x11, SONYPI_EVENT_FNKEY_F1 },
975         { 0x12, SONYPI_EVENT_FNKEY_F2 },
976         { 0x13, SONYPI_EVENT_FNKEY_F3 },
977         { 0x14, SONYPI_EVENT_FNKEY_F4 },
978         { 0x15, SONYPI_EVENT_FNKEY_F5 },
979         { 0x16, SONYPI_EVENT_FNKEY_F6 },
980         { 0x17, SONYPI_EVENT_FNKEY_F7 },
981         { 0x18, SONYPI_EVENT_FNKEY_F8 },
982         { 0x19, SONYPI_EVENT_FNKEY_F9 },
983         { 0x1a, SONYPI_EVENT_FNKEY_F10 },
984         { 0x1b, SONYPI_EVENT_FNKEY_F11 },
985         { 0x1c, SONYPI_EVENT_FNKEY_F12 },
986         { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
987         { 0x21, SONYPI_EVENT_FNKEY_1 },
988         { 0x22, SONYPI_EVENT_FNKEY_2 },
989         { 0x31, SONYPI_EVENT_FNKEY_D },
990         { 0x32, SONYPI_EVENT_FNKEY_E },
991         { 0x33, SONYPI_EVENT_FNKEY_F },
992         { 0x34, SONYPI_EVENT_FNKEY_S },
993         { 0x35, SONYPI_EVENT_FNKEY_B },
994         { 0x36, SONYPI_EVENT_FNKEY_ONLY },
995         { 0, 0 }
996 };
997
998 /* The set of possible program key events */
999 static struct sonypi_event sonypi_pkeyev[] = {
1000         { 0x01, SONYPI_EVENT_PKEY_P1 },
1001         { 0x02, SONYPI_EVENT_PKEY_P2 },
1002         { 0x04, SONYPI_EVENT_PKEY_P3 },
1003         { 0x5c, SONYPI_EVENT_PKEY_P1 },
1004         { 0, 0 }
1005 };
1006
1007 /* The set of possible bluetooth events */
1008 static struct sonypi_event sonypi_blueev[] = {
1009         { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1010         { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1011         { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1012         { 0, 0 }
1013 };
1014
1015 /* The set of possible wireless events */
1016 static struct sonypi_event sonypi_wlessev[] = {
1017         { 0x59, SONYPI_EVENT_WIRELESS_ON },
1018         { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1019         { 0, 0 }
1020 };
1021
1022 /* The set of possible back button events */
1023 static struct sonypi_event sonypi_backev[] = {
1024         { 0x20, SONYPI_EVENT_BACK_PRESSED },
1025         { 0, 0 }
1026 };
1027
1028 /* The set of possible help button events */
1029 static struct sonypi_event sonypi_helpev[] = {
1030         { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1031         { 0, 0 }
1032 };
1033
1034
1035 /* The set of possible lid events */
1036 static struct sonypi_event sonypi_lidev[] = {
1037         { 0x51, SONYPI_EVENT_LID_CLOSED },
1038         { 0x50, SONYPI_EVENT_LID_OPENED },
1039         { 0, 0 }
1040 };
1041
1042 /* The set of possible zoom events */
1043 static struct sonypi_event sonypi_zoomev[] = {
1044         { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1045         { 0, 0 }
1046 };
1047
1048 /* The set of possible thumbphrase events */
1049 static struct sonypi_event sonypi_thumbphraseev[] = {
1050         { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1051         { 0, 0 }
1052 };
1053
1054 /* The set of possible motioneye camera events */
1055 static struct sonypi_event sonypi_meyeev[] = {
1056         { 0x00, SONYPI_EVENT_MEYE_FACE },
1057         { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1058         { 0, 0 }
1059 };
1060
1061 /* The set of possible memorystick events */
1062 static struct sonypi_event sonypi_memorystickev[] = {
1063         { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1064         { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1065         { 0, 0 }
1066 };
1067
1068 /* The set of possible battery events */
1069 static struct sonypi_event sonypi_batteryev[] = {
1070         { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1071         { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1072         { 0, 0 }
1073 };
1074
1075 static struct sonypi_eventtypes {
1076         int                     model;
1077         u8                      data;
1078         unsigned long           mask;
1079         struct sonypi_event *   events;
1080 } sony_pic_eventtypes[] = {
1081         { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1082         { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1083         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1084         { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1085         { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1086         { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1087         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1088         { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1089         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1090         { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1091
1092         { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1093         { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1094         { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1095         { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1096         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1097         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1098         { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1099         { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1100         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1101         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1102         { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1103         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1104         { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1105         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1106
1107         { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1108         { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1109         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1110         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1111         { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1112         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1113         { 0 }
1114 };
1115
1116 static int sony_pic_detect_device_type(void)
1117 {
1118         struct pci_dev *pcidev;
1119         int model = 0;
1120
1121         if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1122                                      PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1123                 model = SONYPI_DEVICE_TYPE1;
1124
1125         else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1126                                           PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1127                 model = SONYPI_DEVICE_TYPE3;
1128
1129         else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1130                                           PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1131                 model = SONYPI_DEVICE_TYPE3;
1132
1133         else
1134                 model = SONYPI_DEVICE_TYPE2;
1135
1136         if (pcidev)
1137                 pci_dev_put(pcidev);
1138
1139         printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1140                         model == SONYPI_DEVICE_TYPE1 ? 1 :
1141                         model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1142         return model;
1143 }
1144
1145 #define ITERATIONS_LONG         10000
1146 #define ITERATIONS_SHORT        10
1147 #define wait_on_command(command, iterations) {                          \
1148         unsigned int n = iterations;                                    \
1149         while (--n && (command))                                        \
1150                 udelay(1);                                              \
1151         if (!n)                                                         \
1152                 dprintk("command failed at %s : %s (line %d)\n",        \
1153                                 __FILE__, __FUNCTION__, __LINE__);      \
1154 }
1155
1156 static u8 sony_pic_call1(u8 dev)
1157 {
1158         u8 v1, v2;
1159
1160         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1161                         ITERATIONS_LONG);
1162         outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1163         v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1164         v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1165         dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1166         return v2;
1167 }
1168
1169 static u8 sony_pic_call2(u8 dev, u8 fn)
1170 {
1171         u8 v1;
1172
1173         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1174                         ITERATIONS_LONG);
1175         outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1176         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1177                         ITERATIONS_LONG);
1178         outb(fn, spic_dev.cur_ioport->io.minimum);
1179         v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1180         dprintk("sony_pic_call2: 0x%.4x\n", v1);
1181         return v1;
1182 }
1183
1184 /*
1185  * ACPI callbacks
1186  */
1187 static acpi_status
1188 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1189 {
1190         u32 i;
1191         struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1192
1193         switch (resource->type) {
1194         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1195         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1196                 return AE_OK;
1197
1198         case ACPI_RESOURCE_TYPE_IRQ:
1199                 {
1200                         struct acpi_resource_irq *p = &resource->data.irq;
1201                         struct sony_pic_irq *interrupt = NULL;
1202                         if (!p || !p->interrupt_count) {
1203                                 /*
1204                                  * IRQ descriptors may have no IRQ# bits set,
1205                                  * particularly those those w/ _STA disabled
1206                                  */
1207                                 dprintk("Blank IRQ resource\n");
1208                                 return AE_OK;
1209                         }
1210                         for (i = 0; i < p->interrupt_count; i++) {
1211                                 if (!p->interrupts[i]) {
1212                                         printk(KERN_WARNING DRV_PFX
1213                                                         "Invalid IRQ %d\n",
1214                                                         p->interrupts[i]);
1215                                         continue;
1216                                 }
1217                                 interrupt = kzalloc(sizeof(*interrupt),
1218                                                 GFP_KERNEL);
1219                                 if (!interrupt)
1220                                         return AE_ERROR;
1221
1222                                 list_add(&interrupt->list, &dev->interrupts);
1223                                 interrupt->irq.triggering = p->triggering;
1224                                 interrupt->irq.polarity = p->polarity;
1225                                 interrupt->irq.sharable = p->sharable;
1226                                 interrupt->irq.interrupt_count = 1;
1227                                 interrupt->irq.interrupts[0] = p->interrupts[i];
1228                         }
1229                         return AE_OK;
1230                 }
1231         case ACPI_RESOURCE_TYPE_IO:
1232                 {
1233                         struct acpi_resource_io *io = &resource->data.io;
1234                         struct sony_pic_ioport *ioport = NULL;
1235                         if (!io) {
1236                                 dprintk("Blank IO resource\n");
1237                                 return AE_OK;
1238                         }
1239
1240                         ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1241                         if (!ioport)
1242                                 return AE_ERROR;
1243
1244                         list_add(&ioport->list, &dev->ioports);
1245                         memcpy(&ioport->io, io, sizeof(*io));
1246                         return AE_OK;
1247                 }
1248         default:
1249                 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1250                                 resource->type);
1251
1252         case ACPI_RESOURCE_TYPE_END_TAG:
1253                 return AE_OK;
1254         }
1255         return AE_CTRL_TERMINATE;
1256 }
1257
1258 static int sony_pic_possible_resources(struct acpi_device *device)
1259 {
1260         int result = 0;
1261         acpi_status status = AE_OK;
1262
1263         if (!device)
1264                 return -EINVAL;
1265
1266         /* get device status */
1267         /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
1268         dprintk("Evaluating _STA\n");
1269         result = acpi_bus_get_status(device);
1270         if (result) {
1271                 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1272                 goto end;
1273         }
1274
1275         if (!device->status.enabled)
1276                 dprintk("Device disabled\n");
1277         else
1278                 dprintk("Device enabled\n");
1279
1280         /*
1281          * Query and parse 'method'
1282          */
1283         dprintk("Evaluating %s\n", METHOD_NAME__PRS);
1284         status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
1285                         sony_pic_read_possible_resource, &spic_dev);
1286         if (ACPI_FAILURE(status)) {
1287                 printk(KERN_WARNING DRV_PFX
1288                                 "Failure evaluating %s\n",
1289                                 METHOD_NAME__PRS);
1290                 result = -ENODEV;
1291         }
1292 end:
1293         return result;
1294 }
1295
1296 /*
1297  *  Disable the spic device by calling its _DIS method
1298  */
1299 static int sony_pic_disable(struct acpi_device *device)
1300 {
1301         if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
1302                 return -ENXIO;
1303
1304         dprintk("Device disabled\n");
1305         return 0;
1306 }
1307
1308
1309 /*
1310  *  Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1311  *
1312  *  Call _SRS to set current resources
1313  */
1314 static int sony_pic_enable(struct acpi_device *device,
1315                 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1316 {
1317         acpi_status status;
1318         int result = 0;
1319         struct {
1320                 struct acpi_resource io_res;
1321                 struct acpi_resource irq_res;
1322                 struct acpi_resource end;
1323         } *resource;
1324         struct acpi_buffer buffer = { 0, NULL };
1325
1326         if (!ioport || !irq)
1327                 return -EINVAL;
1328
1329         /* init acpi_buffer */
1330         resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1331         if (!resource)
1332                 return -ENOMEM;
1333
1334         buffer.length = sizeof(*resource) + 1;
1335         buffer.pointer = resource;
1336
1337         /* setup io resource */
1338         resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
1339         resource->io_res.length = sizeof(struct acpi_resource);
1340         memcpy(&resource->io_res.data.io, &ioport->io,
1341                         sizeof(struct acpi_resource_io));
1342
1343         /* setup irq resource */
1344         resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
1345         resource->irq_res.length = sizeof(struct acpi_resource);
1346         memcpy(&resource->irq_res.data.irq, &irq->irq,
1347                         sizeof(struct acpi_resource_irq));
1348         /* we requested a shared irq */
1349         resource->irq_res.data.irq.sharable = ACPI_SHARED;
1350
1351         resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1352
1353         /* Attempt to set the resource */
1354         dprintk("Evaluating _SRS\n");
1355         status = acpi_set_current_resources(device->handle, &buffer);
1356
1357         /* check for total failure */
1358         if (ACPI_FAILURE(status)) {
1359                 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
1360                 result = -ENODEV;
1361                 goto end;
1362         }
1363
1364         /* Necessary device initializations calls (from sonypi) */
1365         sony_pic_call1(0x82);
1366         sony_pic_call2(0x81, 0xff);
1367         sony_pic_call1(compat ? 0x92 : 0x82);
1368
1369 end:
1370         kfree(resource);
1371         return result;
1372 }
1373
1374 /*****************
1375  *
1376  * ISR: some event is available
1377  *
1378  *****************/
1379 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
1380 {
1381         int i, j;
1382         u32 port_val = 0;
1383         u8 ev = 0;
1384         u8 data_mask = 0;
1385         u8 device_event = 0;
1386
1387         struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
1388
1389         acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
1390                         dev->cur_ioport->io.address_length);
1391         ev = port_val & SONY_PIC_EV_MASK;
1392         data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
1393
1394         dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
1395                         port_val, ev, data_mask, dev->cur_ioport->io.minimum);
1396
1397         if (ev == 0x00 || ev == 0xff)
1398                 return IRQ_HANDLED;
1399
1400         for (i = 0; sony_pic_eventtypes[i].model; i++) {
1401
1402                 if (spic_dev.model != sony_pic_eventtypes[i].model)
1403                         continue;
1404
1405                 if ((data_mask & sony_pic_eventtypes[i].data) !=
1406                     sony_pic_eventtypes[i].data)
1407                         continue;
1408
1409                 if (!(mask & sony_pic_eventtypes[i].mask))
1410                         continue;
1411
1412                 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
1413                         if (ev == sony_pic_eventtypes[i].events[j].data) {
1414                                 device_event =
1415                                         sony_pic_eventtypes[i].events[j].event;
1416                                 goto found;
1417                         }
1418                 }
1419         }
1420         return IRQ_HANDLED;
1421
1422 found:
1423         sony_laptop_report_input_event(device_event);
1424         acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
1425
1426         return IRQ_HANDLED;
1427 }
1428
1429 /*****************
1430  *
1431  *  ACPI driver
1432  *
1433  *****************/
1434 static int sony_pic_remove(struct acpi_device *device, int type)
1435 {
1436         struct sony_pic_ioport *io, *tmp_io;
1437         struct sony_pic_irq *irq, *tmp_irq;
1438
1439         if (sony_pic_disable(device)) {
1440                 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
1441                 return -ENXIO;
1442         }
1443
1444         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
1445         release_region(spic_dev.cur_ioport->io.minimum,
1446                         spic_dev.cur_ioport->io.address_length);
1447
1448         sony_laptop_remove_input();
1449
1450         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1451                 list_del(&io->list);
1452                 kfree(io);
1453         }
1454         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
1455                 list_del(&irq->list);
1456                 kfree(irq);
1457         }
1458         spic_dev.cur_ioport = NULL;
1459         spic_dev.cur_irq = NULL;
1460
1461         dprintk("removed.\n");
1462         return 0;
1463 }
1464
1465 static int sony_pic_add(struct acpi_device *device)
1466 {
1467         int result;
1468         struct sony_pic_ioport *io, *tmp_io;
1469         struct sony_pic_irq *irq, *tmp_irq;
1470
1471         printk(KERN_INFO DRV_PFX
1472                 "Sony Programmable I/O Controller Driver v%s.\n",
1473                 SONY_LAPTOP_DRIVER_VERSION);
1474
1475         spic_dev.acpi_dev = device;
1476         strcpy(acpi_device_class(device), "sony/hotkey");
1477         spic_dev.model = sony_pic_detect_device_type();
1478
1479         /* read _PRS resources */
1480         result = sony_pic_possible_resources(device);
1481         if (result) {
1482                 printk(KERN_ERR DRV_PFX
1483                                 "Unabe to read possible resources.\n");
1484                 goto err_free_resources;
1485         }
1486
1487         /* setup input devices and helper fifo */
1488         result = sony_laptop_setup_input();
1489         if (result) {
1490                 printk(KERN_ERR DRV_PFX
1491                                 "Unabe to create input devices.\n");
1492                 goto err_free_resources;
1493         }
1494
1495         /* request io port */
1496         list_for_each_entry(io, &spic_dev.ioports, list) {
1497                 if (request_region(io->io.minimum, io->io.address_length,
1498                                         "Sony Programable I/O Device")) {
1499                         dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
1500                                         io->io.minimum, io->io.maximum,
1501                                         io->io.address_length);
1502                         spic_dev.cur_ioport = io;
1503                         break;
1504                 }
1505         }
1506         if (!spic_dev.cur_ioport) {
1507                 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
1508                 result = -ENODEV;
1509                 goto err_remove_input;
1510         }
1511
1512         /* request IRQ */
1513         list_for_each_entry(irq, &spic_dev.interrupts, list) {
1514                 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
1515                                         IRQF_SHARED, "sony-laptop", &spic_dev)) {
1516                         dprintk("IRQ: %d - triggering: %d - "
1517                                         "polarity: %d - shr: %d\n",
1518                                         irq->irq.interrupts[0],
1519                                         irq->irq.triggering,
1520                                         irq->irq.polarity,
1521                                         irq->irq.sharable);
1522                         spic_dev.cur_irq = irq;
1523                         break;
1524                 }
1525         }
1526         if (!spic_dev.cur_irq) {
1527                 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
1528                 result = -ENODEV;
1529                 goto err_release_region;
1530         }
1531
1532         /* set resource status _SRS */
1533         result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
1534         if (result) {
1535                 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
1536                 goto err_free_irq;
1537         }
1538
1539         return 0;
1540
1541 err_free_irq:
1542         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
1543
1544 err_release_region:
1545         release_region(spic_dev.cur_ioport->io.minimum,
1546                         spic_dev.cur_ioport->io.address_length);
1547
1548 err_remove_input:
1549         sony_laptop_remove_input();
1550
1551 err_free_resources:
1552         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1553                 list_del(&io->list);
1554                 kfree(io);
1555         }
1556         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
1557                 list_del(&irq->list);
1558                 kfree(irq);
1559         }
1560         spic_dev.cur_ioport = NULL;
1561         spic_dev.cur_irq = NULL;
1562
1563         return result;
1564 }
1565
1566 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
1567 {
1568         if (sony_pic_disable(device))
1569                 return -ENXIO;
1570         return 0;
1571 }
1572
1573 static int sony_pic_resume(struct acpi_device *device)
1574 {
1575         sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
1576         return 0;
1577 }
1578
1579 static struct acpi_driver sony_pic_driver = {
1580         .name = SONY_PIC_DRIVER_NAME,
1581         .class = SONY_PIC_CLASS,
1582         .ids = SONY_PIC_HID,
1583         .owner = THIS_MODULE,
1584         .ops = {
1585                 .add = sony_pic_add,
1586                 .remove = sony_pic_remove,
1587                 .suspend = sony_pic_suspend,
1588                 .resume = sony_pic_resume,
1589                 },
1590 };
1591
1592 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
1593         {
1594                 .ident = "Sony Vaio",
1595                 .matches = {
1596                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1597                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1598                 },
1599         },
1600         {
1601                 .ident = "Sony Vaio",
1602                 .matches = {
1603                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1604                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1605                 },
1606         },
1607         { }
1608 };
1609
1610 static int __init sony_laptop_init(void)
1611 {
1612         int result;
1613
1614         if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
1615                 result = acpi_bus_register_driver(&sony_pic_driver);
1616                 if (result) {
1617                         printk(KERN_ERR DRV_PFX
1618                                         "Unable to register SPIC driver.");
1619                         goto out;
1620                 }
1621         }
1622
1623         result = acpi_bus_register_driver(&sony_nc_driver);
1624         if (result) {
1625                 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
1626                 goto out_unregister_pic;
1627         }
1628
1629         return 0;
1630
1631 out_unregister_pic:
1632         if (!no_spic)
1633                 acpi_bus_unregister_driver(&sony_pic_driver);
1634 out:
1635         return result;
1636 }
1637
1638 static void __exit sony_laptop_exit(void)
1639 {
1640         acpi_bus_unregister_driver(&sony_nc_driver);
1641         if (!no_spic)
1642                 acpi_bus_unregister_driver(&sony_pic_driver);
1643 }
1644
1645 module_init(sony_laptop_init);
1646 module_exit(sony_laptop_exit);