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