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