sony-laptop: map wireless switch events to KEY_WLAN
[sfrench/cifs-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  * ACPI callbacks
709  */
710 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
711 {
712         dprintk("sony_acpi_notify, event: %d\n", event);
713         sony_laptop_report_input_event(event);
714         acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
715 }
716
717 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
718                                       void *context, void **return_value)
719 {
720         struct acpi_namespace_node *node;
721         union acpi_operand_object *operand;
722
723         node = (struct acpi_namespace_node *)handle;
724         operand = (union acpi_operand_object *)node->object;
725
726         printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
727                (u32) operand->method.param_count);
728
729         return AE_OK;
730 }
731
732 /*
733  * ACPI device
734  */
735 static int sony_nc_resume(struct acpi_device *device)
736 {
737         struct sony_nc_value *item;
738
739         for (item = sony_nc_values; item->name; item++) {
740                 int ret;
741
742                 if (!item->valid)
743                         continue;
744                 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
745                                        item->value, NULL);
746                 if (ret < 0) {
747                         printk("%s: %d\n", __FUNCTION__, ret);
748                         break;
749                 }
750         }
751         return 0;
752 }
753
754 static int sony_nc_add(struct acpi_device *device)
755 {
756         acpi_status status;
757         int result = 0;
758         acpi_handle handle;
759         struct sony_nc_value *item;
760
761         printk(KERN_INFO DRV_PFX "%s v%s.\n",
762                 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
763
764         sony_nc_acpi_device = device;
765         strcpy(acpi_device_class(device), "sony/hotkey");
766
767         sony_nc_acpi_handle = device->handle;
768
769         if (debug) {
770                 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
771                                              1, sony_walk_callback, NULL, NULL);
772                 if (ACPI_FAILURE(status)) {
773                         printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
774                         result = -ENODEV;
775                         goto outwalk;
776                 }
777         }
778
779         /* setup input devices and helper fifo */
780         result = sony_laptop_setup_input();
781         if (result) {
782                 printk(KERN_ERR DRV_PFX
783                                 "Unabe to create input devices.\n");
784                 goto outwalk;
785         }
786
787         status = acpi_install_notify_handler(sony_nc_acpi_handle,
788                                              ACPI_DEVICE_NOTIFY,
789                                              sony_acpi_notify, NULL);
790         if (ACPI_FAILURE(status)) {
791                 printk(KERN_WARNING DRV_PFX "unable to install notify handler\n");
792                 result = -ENODEV;
793                 goto outinput;
794         }
795
796         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
797                 sony_backlight_device = backlight_device_register("sony", NULL,
798                                                                   NULL,
799                                                                   &sony_backlight_ops);
800
801                 if (IS_ERR(sony_backlight_device)) {
802                         printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
803                         sony_backlight_device = NULL;
804                 } else {
805                         sony_backlight_device->props.brightness =
806                             sony_backlight_get_brightness
807                             (sony_backlight_device);
808                         sony_backlight_device->props.max_brightness = 
809                             SONY_MAX_BRIGHTNESS - 1;
810                 }
811
812         }
813
814         result = sony_pf_add();
815         if (result)
816                 goto outbacklight;
817
818         /* create sony_pf sysfs attributes related to the SNC device */
819         for (item = sony_nc_values; item->name; ++item) {
820
821                 if (!debug && item->debug)
822                         continue;
823
824                 /* find the available acpiget as described in the DSDT */
825                 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
826                         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
827                                                          *item->acpiget,
828                                                          &handle))) {
829                                 dprintk("Found %s getter: %s\n",
830                                                 item->name, *item->acpiget);
831                                 item->devattr.attr.mode |= S_IRUGO;
832                                 break;
833                         }
834                 }
835
836                 /* find the available acpiset as described in the DSDT */
837                 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
838                         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
839                                                          *item->acpiset,
840                                                          &handle))) {
841                                 dprintk("Found %s setter: %s\n",
842                                                 item->name, *item->acpiset);
843                                 item->devattr.attr.mode |= S_IWUSR;
844                                 break;
845                         }
846                 }
847
848                 if (item->devattr.attr.mode != 0) {
849                         result =
850                             device_create_file(&sony_pf_device->dev,
851                                                &item->devattr);
852                         if (result)
853                                 goto out_sysfs;
854                 }
855         }
856
857         return 0;
858
859       out_sysfs:
860         for (item = sony_nc_values; item->name; ++item) {
861                 device_remove_file(&sony_pf_device->dev, &item->devattr);
862         }
863         sony_pf_remove();
864
865       outbacklight:
866         if (sony_backlight_device)
867                 backlight_device_unregister(sony_backlight_device);
868
869         status = acpi_remove_notify_handler(sony_nc_acpi_handle,
870                                             ACPI_DEVICE_NOTIFY,
871                                             sony_acpi_notify);
872         if (ACPI_FAILURE(status))
873                 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
874
875       outinput:
876         sony_laptop_remove_input();
877
878       outwalk:
879         return result;
880 }
881
882 static int sony_nc_remove(struct acpi_device *device, int type)
883 {
884         acpi_status status;
885         struct sony_nc_value *item;
886
887         if (sony_backlight_device)
888                 backlight_device_unregister(sony_backlight_device);
889
890         sony_nc_acpi_device = NULL;
891
892         status = acpi_remove_notify_handler(sony_nc_acpi_handle,
893                                             ACPI_DEVICE_NOTIFY,
894                                             sony_acpi_notify);
895         if (ACPI_FAILURE(status))
896                 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
897
898         for (item = sony_nc_values; item->name; ++item) {
899                 device_remove_file(&sony_pf_device->dev, &item->devattr);
900         }
901
902         sony_pf_remove();
903         sony_laptop_remove_input();
904         dprintk(SONY_NC_DRIVER_NAME " removed.\n");
905
906         return 0;
907 }
908
909 static struct acpi_driver sony_nc_driver = {
910         .name = SONY_NC_DRIVER_NAME,
911         .class = SONY_NC_CLASS,
912         .ids = SONY_NC_HID,
913         .owner = THIS_MODULE,
914         .ops = {
915                 .add = sony_nc_add,
916                 .remove = sony_nc_remove,
917                 .resume = sony_nc_resume,
918                 },
919 };
920
921 /*********** SPIC (SNY6001) Device ***********/
922
923 #define SONYPI_DEVICE_TYPE1     0x00000001
924 #define SONYPI_DEVICE_TYPE2     0x00000002
925 #define SONYPI_DEVICE_TYPE3     0x00000004
926
927 #define SONY_PIC_EV_MASK        0xff
928
929 struct sony_pic_ioport {
930         struct acpi_resource_io io;
931         struct list_head        list;
932 };
933
934 struct sony_pic_irq {
935         struct acpi_resource_irq        irq;
936         struct list_head                list;
937 };
938
939 struct sony_pic_dev {
940         int                     model;
941         u8                      camera_power;
942         u8                      bluetooth_power;
943         u8                      wwan_power;
944         struct acpi_device      *acpi_dev;
945         struct sony_pic_irq     *cur_irq;
946         struct sony_pic_ioport  *cur_ioport;
947         struct list_head        interrupts;
948         struct list_head        ioports;
949         struct mutex            lock;
950 };
951
952 static struct sony_pic_dev spic_dev = {
953         .interrupts     = LIST_HEAD_INIT(spic_dev.interrupts),
954         .ioports        = LIST_HEAD_INIT(spic_dev.ioports),
955 };
956
957 /* Event masks */
958 #define SONYPI_JOGGER_MASK                      0x00000001
959 #define SONYPI_CAPTURE_MASK                     0x00000002
960 #define SONYPI_FNKEY_MASK                       0x00000004
961 #define SONYPI_BLUETOOTH_MASK                   0x00000008
962 #define SONYPI_PKEY_MASK                        0x00000010
963 #define SONYPI_BACK_MASK                        0x00000020
964 #define SONYPI_HELP_MASK                        0x00000040
965 #define SONYPI_LID_MASK                         0x00000080
966 #define SONYPI_ZOOM_MASK                        0x00000100
967 #define SONYPI_THUMBPHRASE_MASK                 0x00000200
968 #define SONYPI_MEYE_MASK                        0x00000400
969 #define SONYPI_MEMORYSTICK_MASK                 0x00000800
970 #define SONYPI_BATTERY_MASK                     0x00001000
971 #define SONYPI_WIRELESS_MASK                    0x00002000
972
973 struct sonypi_event {
974         u8      data;
975         u8      event;
976 };
977
978 /* The set of possible button release events */
979 static struct sonypi_event sonypi_releaseev[] = {
980         { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
981         { 0, 0 }
982 };
983
984 /* The set of possible jogger events  */
985 static struct sonypi_event sonypi_joggerev[] = {
986         { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
987         { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
988         { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
989         { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
990         { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
991         { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
992         { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
993         { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
994         { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
995         { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
996         { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
997         { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
998         { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
999         { 0, 0 }
1000 };
1001
1002 /* The set of possible capture button events */
1003 static struct sonypi_event sonypi_captureev[] = {
1004         { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
1005         { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
1006         { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
1007         { 0, 0 }
1008 };
1009
1010 /* The set of possible fnkeys events */
1011 static struct sonypi_event sonypi_fnkeyev[] = {
1012         { 0x10, SONYPI_EVENT_FNKEY_ESC },
1013         { 0x11, SONYPI_EVENT_FNKEY_F1 },
1014         { 0x12, SONYPI_EVENT_FNKEY_F2 },
1015         { 0x13, SONYPI_EVENT_FNKEY_F3 },
1016         { 0x14, SONYPI_EVENT_FNKEY_F4 },
1017         { 0x15, SONYPI_EVENT_FNKEY_F5 },
1018         { 0x16, SONYPI_EVENT_FNKEY_F6 },
1019         { 0x17, SONYPI_EVENT_FNKEY_F7 },
1020         { 0x18, SONYPI_EVENT_FNKEY_F8 },
1021         { 0x19, SONYPI_EVENT_FNKEY_F9 },
1022         { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1023         { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1024         { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1025         { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1026         { 0x21, SONYPI_EVENT_FNKEY_1 },
1027         { 0x22, SONYPI_EVENT_FNKEY_2 },
1028         { 0x31, SONYPI_EVENT_FNKEY_D },
1029         { 0x32, SONYPI_EVENT_FNKEY_E },
1030         { 0x33, SONYPI_EVENT_FNKEY_F },
1031         { 0x34, SONYPI_EVENT_FNKEY_S },
1032         { 0x35, SONYPI_EVENT_FNKEY_B },
1033         { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1034         { 0, 0 }
1035 };
1036
1037 /* The set of possible program key events */
1038 static struct sonypi_event sonypi_pkeyev[] = {
1039         { 0x01, SONYPI_EVENT_PKEY_P1 },
1040         { 0x02, SONYPI_EVENT_PKEY_P2 },
1041         { 0x04, SONYPI_EVENT_PKEY_P3 },
1042         { 0x5c, SONYPI_EVENT_PKEY_P1 },
1043         { 0, 0 }
1044 };
1045
1046 /* The set of possible bluetooth events */
1047 static struct sonypi_event sonypi_blueev[] = {
1048         { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1049         { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1050         { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1051         { 0, 0 }
1052 };
1053
1054 /* The set of possible wireless events */
1055 static struct sonypi_event sonypi_wlessev[] = {
1056         { 0x59, SONYPI_EVENT_WIRELESS_ON },
1057         { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1058         { 0, 0 }
1059 };
1060
1061 /* The set of possible back button events */
1062 static struct sonypi_event sonypi_backev[] = {
1063         { 0x20, SONYPI_EVENT_BACK_PRESSED },
1064         { 0, 0 }
1065 };
1066
1067 /* The set of possible help button events */
1068 static struct sonypi_event sonypi_helpev[] = {
1069         { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1070         { 0, 0 }
1071 };
1072
1073
1074 /* The set of possible lid events */
1075 static struct sonypi_event sonypi_lidev[] = {
1076         { 0x51, SONYPI_EVENT_LID_CLOSED },
1077         { 0x50, SONYPI_EVENT_LID_OPENED },
1078         { 0, 0 }
1079 };
1080
1081 /* The set of possible zoom events */
1082 static struct sonypi_event sonypi_zoomev[] = {
1083         { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1084         { 0, 0 }
1085 };
1086
1087 /* The set of possible thumbphrase events */
1088 static struct sonypi_event sonypi_thumbphraseev[] = {
1089         { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1090         { 0, 0 }
1091 };
1092
1093 /* The set of possible motioneye camera events */
1094 static struct sonypi_event sonypi_meyeev[] = {
1095         { 0x00, SONYPI_EVENT_MEYE_FACE },
1096         { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1097         { 0, 0 }
1098 };
1099
1100 /* The set of possible memorystick events */
1101 static struct sonypi_event sonypi_memorystickev[] = {
1102         { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1103         { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1104         { 0, 0 }
1105 };
1106
1107 /* The set of possible battery events */
1108 static struct sonypi_event sonypi_batteryev[] = {
1109         { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1110         { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1111         { 0, 0 }
1112 };
1113
1114 static struct sonypi_eventtypes {
1115         int                     model;
1116         u8                      data;
1117         unsigned long           mask;
1118         struct sonypi_event *   events;
1119 } sony_pic_eventtypes[] = {
1120         { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1121         { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1122         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1123         { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1124         { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1125         { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1126         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1127         { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1128         { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1129         { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1130
1131         { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1132         { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1133         { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1134         { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1135         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1136         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1137         { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1138         { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1139         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1140         { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1141         { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1142         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1143         { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1144         { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1145
1146         { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1147         { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1148         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1149         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1150         { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1151         { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1152         { 0 }
1153 };
1154
1155 static int sony_pic_detect_device_type(void)
1156 {
1157         struct pci_dev *pcidev;
1158         int model = 0;
1159
1160         if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1161                                      PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1162                 model = SONYPI_DEVICE_TYPE1;
1163
1164         else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1165                                           PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1166                 model = SONYPI_DEVICE_TYPE3;
1167
1168         else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1169                                           PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1170                 model = SONYPI_DEVICE_TYPE3;
1171
1172         else
1173                 model = SONYPI_DEVICE_TYPE2;
1174
1175         if (pcidev)
1176                 pci_dev_put(pcidev);
1177
1178         printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1179                         model == SONYPI_DEVICE_TYPE1 ? 1 :
1180                         model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1181         return model;
1182 }
1183
1184 #define ITERATIONS_LONG         10000
1185 #define ITERATIONS_SHORT        10
1186 #define wait_on_command(command, iterations) {                          \
1187         unsigned int n = iterations;                                    \
1188         while (--n && (command))                                        \
1189                 udelay(1);                                              \
1190         if (!n)                                                         \
1191                 dprintk("command failed at %s : %s (line %d)\n",        \
1192                                 __FILE__, __FUNCTION__, __LINE__);      \
1193 }
1194
1195 static u8 sony_pic_call1(u8 dev)
1196 {
1197         u8 v1, v2;
1198
1199         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1200                         ITERATIONS_LONG);
1201         outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1202         v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1203         v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1204         dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1205         return v2;
1206 }
1207
1208 static u8 sony_pic_call2(u8 dev, u8 fn)
1209 {
1210         u8 v1;
1211
1212         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1213                         ITERATIONS_LONG);
1214         outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1215         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1216                         ITERATIONS_LONG);
1217         outb(fn, spic_dev.cur_ioport->io.minimum);
1218         v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1219         dprintk("sony_pic_call2: 0x%.4x\n", v1);
1220         return v1;
1221 }
1222
1223 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1224 {
1225         u8 v1;
1226
1227         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1228         outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1229         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1230         outb(fn, spic_dev.cur_ioport->io.minimum);
1231         wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1232         outb(v, spic_dev.cur_ioport->io.minimum);
1233         v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1234         dprintk("sony_pic_call3: 0x%.4x\n", v1);
1235         return v1;
1236 }
1237
1238 /* camera tests and poweron/poweroff */
1239 #define SONYPI_CAMERA_PICTURE           5
1240 #define SONYPI_CAMERA_CONTROL           0x10
1241
1242 #define SONYPI_CAMERA_BRIGHTNESS                0
1243 #define SONYPI_CAMERA_CONTRAST                  1
1244 #define SONYPI_CAMERA_HUE                       2
1245 #define SONYPI_CAMERA_COLOR                     3
1246 #define SONYPI_CAMERA_SHARPNESS                 4
1247
1248 #define SONYPI_CAMERA_EXPOSURE_MASK             0xC
1249 #define SONYPI_CAMERA_WHITE_BALANCE_MASK        0x3
1250 #define SONYPI_CAMERA_PICTURE_MODE_MASK         0x30
1251 #define SONYPI_CAMERA_MUTE_MASK                 0x40
1252
1253 /* the rest don't need a loop until not 0xff */
1254 #define SONYPI_CAMERA_AGC                       6
1255 #define SONYPI_CAMERA_AGC_MASK                  0x30
1256 #define SONYPI_CAMERA_SHUTTER_MASK              0x7
1257
1258 #define SONYPI_CAMERA_SHUTDOWN_REQUEST          7
1259 #define SONYPI_CAMERA_CONTROL                   0x10
1260
1261 #define SONYPI_CAMERA_STATUS                    7
1262 #define SONYPI_CAMERA_STATUS_READY              0x2
1263 #define SONYPI_CAMERA_STATUS_POSITION           0x4
1264
1265 #define SONYPI_DIRECTION_BACKWARDS              0x4
1266
1267 #define SONYPI_CAMERA_REVISION                  8
1268 #define SONYPI_CAMERA_ROMVERSION                9
1269
1270 static int __sony_pic_camera_ready(void)
1271 {
1272         u8 v;
1273
1274         v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1275         return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1276 }
1277
1278 static int __sony_pic_camera_off(void)
1279 {
1280         if (!camera) {
1281                 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1282                 return -ENODEV;
1283         }
1284
1285         wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1286                                 SONYPI_CAMERA_MUTE_MASK),
1287                         ITERATIONS_SHORT);
1288
1289         if (spic_dev.camera_power) {
1290                 sony_pic_call2(0x91, 0);
1291                 spic_dev.camera_power = 0;
1292         }
1293         return 0;
1294 }
1295
1296 static int __sony_pic_camera_on(void)
1297 {
1298         int i, j, x;
1299
1300         if (!camera) {
1301                 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1302                 return -ENODEV;
1303         }
1304
1305         if (spic_dev.camera_power)
1306                 return 0;
1307
1308         for (j = 5; j > 0; j--) {
1309
1310                 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1311                         msleep(10);
1312                 sony_pic_call1(0x93);
1313
1314                 for (i = 400; i > 0; i--) {
1315                         if (__sony_pic_camera_ready())
1316                                 break;
1317                         msleep(10);
1318                 }
1319                 if (i)
1320                         break;
1321         }
1322
1323         if (j == 0) {
1324                 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
1325                 return -ENODEV;
1326         }
1327
1328         wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1329                                 0x5a),
1330                         ITERATIONS_SHORT);
1331
1332         spic_dev.camera_power = 1;
1333         return 0;
1334 }
1335
1336 /* External camera command (exported to the motion eye v4l driver) */
1337 int sony_pic_camera_command(int command, u8 value)
1338 {
1339         if (!camera)
1340                 return -EIO;
1341
1342         mutex_lock(&spic_dev.lock);
1343
1344         switch (command) {
1345         case SONY_PIC_COMMAND_SETCAMERA:
1346                 if (value)
1347                         __sony_pic_camera_on();
1348                 else
1349                         __sony_pic_camera_off();
1350                 break;
1351         case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
1352                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
1353                                 ITERATIONS_SHORT);
1354                 break;
1355         case SONY_PIC_COMMAND_SETCAMERACONTRAST:
1356                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
1357                                 ITERATIONS_SHORT);
1358                 break;
1359         case SONY_PIC_COMMAND_SETCAMERAHUE:
1360                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
1361                                 ITERATIONS_SHORT);
1362                 break;
1363         case SONY_PIC_COMMAND_SETCAMERACOLOR:
1364                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
1365                                 ITERATIONS_SHORT);
1366                 break;
1367         case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
1368                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
1369                                 ITERATIONS_SHORT);
1370                 break;
1371         case SONY_PIC_COMMAND_SETCAMERAPICTURE:
1372                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
1373                                 ITERATIONS_SHORT);
1374                 break;
1375         case SONY_PIC_COMMAND_SETCAMERAAGC:
1376                 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
1377                                 ITERATIONS_SHORT);
1378                 break;
1379         default:
1380                 printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
1381                        command);
1382                 break;
1383         }
1384         mutex_unlock(&spic_dev.lock);
1385         return 0;
1386 }
1387 EXPORT_SYMBOL(sony_pic_camera_command);
1388
1389 /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
1390 static void sony_pic_set_wwanpower(u8 state)
1391 {
1392         state = !!state;
1393         mutex_lock(&spic_dev.lock);
1394         if (spic_dev.wwan_power == state) {
1395                 mutex_unlock(&spic_dev.lock);
1396                 return;
1397         }
1398         sony_pic_call2(0xB0, state);
1399         spic_dev.wwan_power = state;
1400         mutex_unlock(&spic_dev.lock);
1401 }
1402
1403 static ssize_t sony_pic_wwanpower_store(struct device *dev,
1404                 struct device_attribute *attr,
1405                 const char *buffer, size_t count)
1406 {
1407         unsigned long value;
1408         if (count > 31)
1409                 return -EINVAL;
1410
1411         value = simple_strtoul(buffer, NULL, 10);
1412         sony_pic_set_wwanpower(value);
1413
1414         return count;
1415 }
1416
1417 static ssize_t sony_pic_wwanpower_show(struct device *dev,
1418                 struct device_attribute *attr, char *buffer)
1419 {
1420         ssize_t count;
1421         mutex_lock(&spic_dev.lock);
1422         count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
1423         mutex_unlock(&spic_dev.lock);
1424         return count;
1425 }
1426
1427 /* bluetooth subsystem power state */
1428 static void __sony_pic_set_bluetoothpower(u8 state)
1429 {
1430         state = !!state;
1431         if (spic_dev.bluetooth_power == state)
1432                 return;
1433         sony_pic_call2(0x96, state);
1434         sony_pic_call1(0x82);
1435         spic_dev.bluetooth_power = state;
1436 }
1437
1438 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1439                 struct device_attribute *attr,
1440                 const char *buffer, size_t count)
1441 {
1442         unsigned long value;
1443         if (count > 31)
1444                 return -EINVAL;
1445
1446         value = simple_strtoul(buffer, NULL, 10);
1447         mutex_lock(&spic_dev.lock);
1448         __sony_pic_set_bluetoothpower(value);
1449         mutex_unlock(&spic_dev.lock);
1450
1451         return count;
1452 }
1453
1454 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1455                 struct device_attribute *attr, char *buffer)
1456 {
1457         ssize_t count = 0;
1458         mutex_lock(&spic_dev.lock);
1459         count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1460         mutex_unlock(&spic_dev.lock);
1461         return count;
1462 }
1463
1464 /* fan speed */
1465 /* FAN0 information (reverse engineered from ACPI tables) */
1466 #define SONY_PIC_FAN0_STATUS    0x93
1467 static int sony_pic_set_fanspeed(unsigned long value)
1468 {
1469         return ec_write(SONY_PIC_FAN0_STATUS, value);
1470 }
1471
1472 static int sony_pic_get_fanspeed(u8 *value)
1473 {
1474         return ec_read(SONY_PIC_FAN0_STATUS, value);
1475 }
1476
1477 static ssize_t sony_pic_fanspeed_store(struct device *dev,
1478                 struct device_attribute *attr,
1479                 const char *buffer, size_t count)
1480 {
1481         unsigned long value;
1482         if (count > 31)
1483                 return -EINVAL;
1484
1485         value = simple_strtoul(buffer, NULL, 10);
1486         if (sony_pic_set_fanspeed(value))
1487                 return -EIO;
1488
1489         return count;
1490 }
1491
1492 static ssize_t sony_pic_fanspeed_show(struct device *dev,
1493                 struct device_attribute *attr, char *buffer)
1494 {
1495         u8 value = 0;
1496         if (sony_pic_get_fanspeed(&value))
1497                 return -EIO;
1498
1499         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1500 }
1501
1502 #define SPIC_ATTR(_name, _mode)                                 \
1503 struct device_attribute spic_attr_##_name = __ATTR(_name,       \
1504                 _mode, sony_pic_## _name ##_show,               \
1505                 sony_pic_## _name ##_store)
1506
1507 static SPIC_ATTR(bluetoothpower, 0644);
1508 static SPIC_ATTR(wwanpower, 0644);
1509 static SPIC_ATTR(fanspeed, 0644);
1510
1511 static struct attribute *spic_attributes[] = {
1512         &spic_attr_bluetoothpower.attr,
1513         &spic_attr_wwanpower.attr,
1514         &spic_attr_fanspeed.attr,
1515         NULL
1516 };
1517
1518 static struct attribute_group spic_attribute_group = {
1519         .attrs = spic_attributes
1520 };
1521
1522 /******** SONYPI compatibility **********/
1523 #ifdef CONFIG_SONYPI_COMPAT
1524
1525 /* battery / brightness / temperature  addresses */
1526 #define SONYPI_BAT_FLAGS        0x81
1527 #define SONYPI_LCD_LIGHT        0x96
1528 #define SONYPI_BAT1_PCTRM       0xa0
1529 #define SONYPI_BAT1_LEFT        0xa2
1530 #define SONYPI_BAT1_MAXRT       0xa4
1531 #define SONYPI_BAT2_PCTRM       0xa8
1532 #define SONYPI_BAT2_LEFT        0xaa
1533 #define SONYPI_BAT2_MAXRT       0xac
1534 #define SONYPI_BAT1_MAXTK       0xb0
1535 #define SONYPI_BAT1_FULL        0xb2
1536 #define SONYPI_BAT2_MAXTK       0xb8
1537 #define SONYPI_BAT2_FULL        0xba
1538 #define SONYPI_TEMP_STATUS      0xC1
1539
1540 struct sonypi_compat_s {
1541         struct fasync_struct    *fifo_async;
1542         struct kfifo            *fifo;
1543         spinlock_t              fifo_lock;
1544         wait_queue_head_t       fifo_proc_list;
1545         atomic_t                open_count;
1546 };
1547 static struct sonypi_compat_s sonypi_compat = {
1548         .open_count = ATOMIC_INIT(0),
1549 };
1550
1551 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1552 {
1553         int retval;
1554
1555         retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1556         if (retval < 0)
1557                 return retval;
1558         return 0;
1559 }
1560
1561 static int sonypi_misc_release(struct inode *inode, struct file *file)
1562 {
1563         sonypi_misc_fasync(-1, file, 0);
1564         atomic_dec(&sonypi_compat.open_count);
1565         return 0;
1566 }
1567
1568 static int sonypi_misc_open(struct inode *inode, struct file *file)
1569 {
1570         /* Flush input queue on first open */
1571         if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1572                 kfifo_reset(sonypi_compat.fifo);
1573         return 0;
1574 }
1575
1576 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1577                                 size_t count, loff_t *pos)
1578 {
1579         ssize_t ret;
1580         unsigned char c;
1581
1582         if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1583             (file->f_flags & O_NONBLOCK))
1584                 return -EAGAIN;
1585
1586         ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1587                                        kfifo_len(sonypi_compat.fifo) != 0);
1588         if (ret)
1589                 return ret;
1590
1591         while (ret < count &&
1592                (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1593                 if (put_user(c, buf++))
1594                         return -EFAULT;
1595                 ret++;
1596         }
1597
1598         if (ret > 0) {
1599                 struct inode *inode = file->f_path.dentry->d_inode;
1600                 inode->i_atime = current_fs_time(inode->i_sb);
1601         }
1602
1603         return ret;
1604 }
1605
1606 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1607 {
1608         poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1609         if (kfifo_len(sonypi_compat.fifo))
1610                 return POLLIN | POLLRDNORM;
1611         return 0;
1612 }
1613
1614 static int ec_read16(u8 addr, u16 *value)
1615 {
1616         u8 val_lb, val_hb;
1617         if (ec_read(addr, &val_lb))
1618                 return -1;
1619         if (ec_read(addr + 1, &val_hb))
1620                 return -1;
1621         *value = val_lb | (val_hb << 8);
1622         return 0;
1623 }
1624
1625 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1626                              unsigned int cmd, unsigned long arg)
1627 {
1628         int ret = 0;
1629         void __user *argp = (void __user *)arg;
1630         u8 val8;
1631         u16 val16;
1632         int value;
1633
1634         mutex_lock(&spic_dev.lock);
1635         switch (cmd) {
1636         case SONYPI_IOCGBRT:
1637                 if (sony_backlight_device == NULL) {
1638                         ret = -EIO;
1639                         break;
1640                 }
1641                 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1642                         ret = -EIO;
1643                         break;
1644                 }
1645                 val8 = ((value & 0xff) - 1) << 5;
1646                 if (copy_to_user(argp, &val8, sizeof(val8)))
1647                                 ret = -EFAULT;
1648                 break;
1649         case SONYPI_IOCSBRT:
1650                 if (sony_backlight_device == NULL) {
1651                         ret = -EIO;
1652                         break;
1653                 }
1654                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1655                         ret = -EFAULT;
1656                         break;
1657                 }
1658                 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1659                                 (val8 >> 5) + 1, NULL)) {
1660                         ret = -EIO;
1661                         break;
1662                 }
1663                 /* sync the backlight device status */
1664                 sony_backlight_device->props.brightness =
1665                     sony_backlight_get_brightness(sony_backlight_device);
1666                 break;
1667         case SONYPI_IOCGBAT1CAP:
1668                 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1669                         ret = -EIO;
1670                         break;
1671                 }
1672                 if (copy_to_user(argp, &val16, sizeof(val16)))
1673                         ret = -EFAULT;
1674                 break;
1675         case SONYPI_IOCGBAT1REM:
1676                 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1677                         ret = -EIO;
1678                         break;
1679                 }
1680                 if (copy_to_user(argp, &val16, sizeof(val16)))
1681                         ret = -EFAULT;
1682                 break;
1683         case SONYPI_IOCGBAT2CAP:
1684                 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1685                         ret = -EIO;
1686                         break;
1687                 }
1688                 if (copy_to_user(argp, &val16, sizeof(val16)))
1689                         ret = -EFAULT;
1690                 break;
1691         case SONYPI_IOCGBAT2REM:
1692                 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1693                         ret = -EIO;
1694                         break;
1695                 }
1696                 if (copy_to_user(argp, &val16, sizeof(val16)))
1697                         ret = -EFAULT;
1698                 break;
1699         case SONYPI_IOCGBATFLAGS:
1700                 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1701                         ret = -EIO;
1702                         break;
1703                 }
1704                 val8 &= 0x07;
1705                 if (copy_to_user(argp, &val8, sizeof(val8)))
1706                         ret = -EFAULT;
1707                 break;
1708         case SONYPI_IOCGBLUE:
1709                 val8 = spic_dev.bluetooth_power;
1710                 if (copy_to_user(argp, &val8, sizeof(val8)))
1711                         ret = -EFAULT;
1712                 break;
1713         case SONYPI_IOCSBLUE:
1714                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1715                         ret = -EFAULT;
1716                         break;
1717                 }
1718                 __sony_pic_set_bluetoothpower(val8);
1719                 break;
1720         /* FAN Controls */
1721         case SONYPI_IOCGFAN:
1722                 if (sony_pic_get_fanspeed(&val8)) {
1723                         ret = -EIO;
1724                         break;
1725                 }
1726                 if (copy_to_user(argp, &val8, sizeof(val8)))
1727                         ret = -EFAULT;
1728                 break;
1729         case SONYPI_IOCSFAN:
1730                 if (copy_from_user(&val8, argp, sizeof(val8))) {
1731                         ret = -EFAULT;
1732                         break;
1733                 }
1734                 if (sony_pic_set_fanspeed(val8))
1735                         ret = -EIO;
1736                 break;
1737         /* GET Temperature (useful under APM) */
1738         case SONYPI_IOCGTEMP:
1739                 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1740                         ret = -EIO;
1741                         break;
1742                 }
1743                 if (copy_to_user(argp, &val8, sizeof(val8)))
1744                         ret = -EFAULT;
1745                 break;
1746         default:
1747                 ret = -EINVAL;
1748         }
1749         mutex_unlock(&spic_dev.lock);
1750         return ret;
1751 }
1752
1753 static const struct file_operations sonypi_misc_fops = {
1754         .owner          = THIS_MODULE,
1755         .read           = sonypi_misc_read,
1756         .poll           = sonypi_misc_poll,
1757         .open           = sonypi_misc_open,
1758         .release        = sonypi_misc_release,
1759         .fasync         = sonypi_misc_fasync,
1760         .ioctl          = sonypi_misc_ioctl,
1761 };
1762
1763 static struct miscdevice sonypi_misc_device = {
1764         .minor          = MISC_DYNAMIC_MINOR,
1765         .name           = "sonypi",
1766         .fops           = &sonypi_misc_fops,
1767 };
1768
1769 static void sonypi_compat_report_event(u8 event)
1770 {
1771         kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
1772         kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
1773         wake_up_interruptible(&sonypi_compat.fifo_proc_list);
1774 }
1775
1776 static int sonypi_compat_init(void)
1777 {
1778         int error;
1779
1780         spin_lock_init(&sonypi_compat.fifo_lock);
1781         sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
1782                                          &sonypi_compat.fifo_lock);
1783         if (IS_ERR(sonypi_compat.fifo)) {
1784                 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
1785                 return PTR_ERR(sonypi_compat.fifo);
1786         }
1787
1788         init_waitqueue_head(&sonypi_compat.fifo_proc_list);
1789
1790         if (minor != -1)
1791                 sonypi_misc_device.minor = minor;
1792         error = misc_register(&sonypi_misc_device);
1793         if (error) {
1794                 printk(KERN_ERR DRV_PFX "misc_register failed\n");
1795                 goto err_free_kfifo;
1796         }
1797         if (minor == -1)
1798                 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
1799                        sonypi_misc_device.minor);
1800
1801         return 0;
1802
1803 err_free_kfifo:
1804         kfifo_free(sonypi_compat.fifo);
1805         return error;
1806 }
1807
1808 static void sonypi_compat_exit(void)
1809 {
1810         misc_deregister(&sonypi_misc_device);
1811         kfifo_free(sonypi_compat.fifo);
1812 }
1813 #else
1814 static int sonypi_compat_init(void) { return 0; }
1815 static void sonypi_compat_exit(void) { }
1816 static void sonypi_compat_report_event(u8 event) { }
1817 #endif /* CONFIG_SONYPI_COMPAT */
1818
1819 /*
1820  * ACPI callbacks
1821  */
1822 static acpi_status
1823 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1824 {
1825         u32 i;
1826         struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1827
1828         switch (resource->type) {
1829         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1830         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1831                 return AE_OK;
1832
1833         case ACPI_RESOURCE_TYPE_IRQ:
1834                 {
1835                         struct acpi_resource_irq *p = &resource->data.irq;
1836                         struct sony_pic_irq *interrupt = NULL;
1837                         if (!p || !p->interrupt_count) {
1838                                 /*
1839                                  * IRQ descriptors may have no IRQ# bits set,
1840                                  * particularly those those w/ _STA disabled
1841                                  */
1842                                 dprintk("Blank IRQ resource\n");
1843                                 return AE_OK;
1844                         }
1845                         for (i = 0; i < p->interrupt_count; i++) {
1846                                 if (!p->interrupts[i]) {
1847                                         printk(KERN_WARNING DRV_PFX
1848                                                         "Invalid IRQ %d\n",
1849                                                         p->interrupts[i]);
1850                                         continue;
1851                                 }
1852                                 interrupt = kzalloc(sizeof(*interrupt),
1853                                                 GFP_KERNEL);
1854                                 if (!interrupt)
1855                                         return AE_ERROR;
1856
1857                                 list_add_tail(&interrupt->list, &dev->interrupts);
1858                                 interrupt->irq.triggering = p->triggering;
1859                                 interrupt->irq.polarity = p->polarity;
1860                                 interrupt->irq.sharable = p->sharable;
1861                                 interrupt->irq.interrupt_count = 1;
1862                                 interrupt->irq.interrupts[0] = p->interrupts[i];
1863                         }
1864                         return AE_OK;
1865                 }
1866         case ACPI_RESOURCE_TYPE_IO:
1867                 {
1868                         struct acpi_resource_io *io = &resource->data.io;
1869                         struct sony_pic_ioport *ioport = NULL;
1870                         if (!io) {
1871                                 dprintk("Blank IO resource\n");
1872                                 return AE_OK;
1873                         }
1874
1875                         ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1876                         if (!ioport)
1877                                 return AE_ERROR;
1878
1879                         list_add_tail(&ioport->list, &dev->ioports);
1880                         memcpy(&ioport->io, io, sizeof(*io));
1881                         return AE_OK;
1882                 }
1883         default:
1884                 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1885                                 resource->type);
1886
1887         case ACPI_RESOURCE_TYPE_END_TAG:
1888                 return AE_OK;
1889         }
1890         return AE_CTRL_TERMINATE;
1891 }
1892
1893 static int sony_pic_possible_resources(struct acpi_device *device)
1894 {
1895         int result = 0;
1896         acpi_status status = AE_OK;
1897
1898         if (!device)
1899                 return -EINVAL;
1900
1901         /* get device status */
1902         /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
1903         dprintk("Evaluating _STA\n");
1904         result = acpi_bus_get_status(device);
1905         if (result) {
1906                 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1907                 goto end;
1908         }
1909
1910         if (!device->status.enabled)
1911                 dprintk("Device disabled\n");
1912         else
1913                 dprintk("Device enabled\n");
1914
1915         /*
1916          * Query and parse 'method'
1917          */
1918         dprintk("Evaluating %s\n", METHOD_NAME__PRS);
1919         status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
1920                         sony_pic_read_possible_resource, &spic_dev);
1921         if (ACPI_FAILURE(status)) {
1922                 printk(KERN_WARNING DRV_PFX
1923                                 "Failure evaluating %s\n",
1924                                 METHOD_NAME__PRS);
1925                 result = -ENODEV;
1926         }
1927 end:
1928         return result;
1929 }
1930
1931 /*
1932  *  Disable the spic device by calling its _DIS method
1933  */
1934 static int sony_pic_disable(struct acpi_device *device)
1935 {
1936         if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
1937                         "_DIS", NULL, NULL)))
1938                 return -ENXIO;
1939
1940         dprintk("Device disabled\n");
1941         return 0;
1942 }
1943
1944
1945 /*
1946  *  Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1947  *
1948  *  Call _SRS to set current resources
1949  */
1950 static int sony_pic_enable(struct acpi_device *device,
1951                 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1952 {
1953         acpi_status status;
1954         int result = 0;
1955         struct {
1956                 struct acpi_resource io_res;
1957                 struct acpi_resource irq_res;
1958                 struct acpi_resource end;
1959         } *resource;
1960         struct acpi_buffer buffer = { 0, NULL };
1961
1962         if (!ioport || !irq)
1963                 return -EINVAL;
1964
1965         /* init acpi_buffer */
1966         resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1967         if (!resource)
1968                 return -ENOMEM;
1969
1970         buffer.length = sizeof(*resource) + 1;
1971         buffer.pointer = resource;
1972
1973         /* setup io resource */
1974         resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
1975         resource->io_res.length = sizeof(struct acpi_resource);
1976         memcpy(&resource->io_res.data.io, &ioport->io,
1977                         sizeof(struct acpi_resource_io));
1978
1979         /* setup irq resource */
1980         resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
1981         resource->irq_res.length = sizeof(struct acpi_resource);
1982         memcpy(&resource->irq_res.data.irq, &irq->irq,
1983                         sizeof(struct acpi_resource_irq));
1984         /* we requested a shared irq */
1985         resource->irq_res.data.irq.sharable = ACPI_SHARED;
1986
1987         resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1988
1989         /* Attempt to set the resource */
1990         dprintk("Evaluating _SRS\n");
1991         status = acpi_set_current_resources(device->handle, &buffer);
1992
1993         /* check for total failure */
1994         if (ACPI_FAILURE(status)) {
1995                 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
1996                 result = -ENODEV;
1997                 goto end;
1998         }
1999
2000         /* Necessary device initializations calls (from sonypi) */
2001         sony_pic_call1(0x82);
2002         sony_pic_call2(0x81, 0xff);
2003         sony_pic_call1(compat ? 0x92 : 0x82);
2004
2005 end:
2006         kfree(resource);
2007         return result;
2008 }
2009
2010 /*****************
2011  *
2012  * ISR: some event is available
2013  *
2014  *****************/
2015 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
2016 {
2017         int i, j;
2018         u32 port_val = 0;
2019         u8 ev = 0;
2020         u8 data_mask = 0;
2021         u8 device_event = 0;
2022
2023         struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
2024
2025         acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
2026                         dev->cur_ioport->io.address_length);
2027         ev = port_val & SONY_PIC_EV_MASK;
2028         data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
2029
2030         dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
2031                         port_val, ev, data_mask, dev->cur_ioport->io.minimum);
2032
2033         if (ev == 0x00 || ev == 0xff)
2034                 return IRQ_HANDLED;
2035
2036         for (i = 0; sony_pic_eventtypes[i].model; i++) {
2037
2038                 if (spic_dev.model != sony_pic_eventtypes[i].model)
2039                         continue;
2040
2041                 if ((data_mask & sony_pic_eventtypes[i].data) !=
2042                     sony_pic_eventtypes[i].data)
2043                         continue;
2044
2045                 if (!(mask & sony_pic_eventtypes[i].mask))
2046                         continue;
2047
2048                 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
2049                         if (ev == sony_pic_eventtypes[i].events[j].data) {
2050                                 device_event =
2051                                         sony_pic_eventtypes[i].events[j].event;
2052                                 goto found;
2053                         }
2054                 }
2055         }
2056         return IRQ_HANDLED;
2057
2058 found:
2059         sony_laptop_report_input_event(device_event);
2060         acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
2061         sonypi_compat_report_event(device_event);
2062
2063         return IRQ_HANDLED;
2064 }
2065
2066 /*****************
2067  *
2068  *  ACPI driver
2069  *
2070  *****************/
2071 static int sony_pic_remove(struct acpi_device *device, int type)
2072 {
2073         struct sony_pic_ioport *io, *tmp_io;
2074         struct sony_pic_irq *irq, *tmp_irq;
2075
2076         sonypi_compat_exit();
2077
2078         if (sony_pic_disable(device)) {
2079                 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
2080                 return -ENXIO;
2081         }
2082
2083         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2084         release_region(spic_dev.cur_ioport->io.minimum,
2085                         spic_dev.cur_ioport->io.address_length);
2086
2087         sony_laptop_remove_input();
2088
2089         /* pf attrs */
2090         sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2091         sony_pf_remove();
2092
2093         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2094                 list_del(&io->list);
2095                 kfree(io);
2096         }
2097         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2098                 list_del(&irq->list);
2099                 kfree(irq);
2100         }
2101         spic_dev.cur_ioport = NULL;
2102         spic_dev.cur_irq = NULL;
2103
2104         dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2105         return 0;
2106 }
2107
2108 static int sony_pic_add(struct acpi_device *device)
2109 {
2110         int result;
2111         struct sony_pic_ioport *io, *tmp_io;
2112         struct sony_pic_irq *irq, *tmp_irq;
2113
2114         printk(KERN_INFO DRV_PFX "%s v%s.\n",
2115                 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2116
2117         spic_dev.acpi_dev = device;
2118         strcpy(acpi_device_class(device), "sony/hotkey");
2119         spic_dev.model = sony_pic_detect_device_type();
2120         mutex_init(&spic_dev.lock);
2121
2122         /* read _PRS resources */
2123         result = sony_pic_possible_resources(device);
2124         if (result) {
2125                 printk(KERN_ERR DRV_PFX
2126                                 "Unabe to read possible resources.\n");
2127                 goto err_free_resources;
2128         }
2129
2130         /* setup input devices and helper fifo */
2131         result = sony_laptop_setup_input();
2132         if (result) {
2133                 printk(KERN_ERR DRV_PFX
2134                                 "Unabe to create input devices.\n");
2135                 goto err_free_resources;
2136         }
2137
2138         /* request io port */
2139         list_for_each_entry(io, &spic_dev.ioports, list) {
2140                 if (request_region(io->io.minimum, io->io.address_length,
2141                                         "Sony Programable I/O Device")) {
2142                         dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
2143                                         io->io.minimum, io->io.maximum,
2144                                         io->io.address_length);
2145                         spic_dev.cur_ioport = io;
2146                         break;
2147                 }
2148         }
2149         if (!spic_dev.cur_ioport) {
2150                 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2151                 result = -ENODEV;
2152                 goto err_remove_input;
2153         }
2154
2155         /* request IRQ */
2156         list_for_each_entry(irq, &spic_dev.interrupts, list) {
2157                 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2158                                         IRQF_SHARED, "sony-laptop", &spic_dev)) {
2159                         dprintk("IRQ: %d - triggering: %d - "
2160                                         "polarity: %d - shr: %d\n",
2161                                         irq->irq.interrupts[0],
2162                                         irq->irq.triggering,
2163                                         irq->irq.polarity,
2164                                         irq->irq.sharable);
2165                         spic_dev.cur_irq = irq;
2166                         break;
2167                 }
2168         }
2169         if (!spic_dev.cur_irq) {
2170                 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2171                 result = -ENODEV;
2172                 goto err_release_region;
2173         }
2174
2175         /* set resource status _SRS */
2176         result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2177         if (result) {
2178                 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2179                 goto err_free_irq;
2180         }
2181
2182         spic_dev.bluetooth_power = -1;
2183         /* create device attributes */
2184         result = sony_pf_add();
2185         if (result)
2186                 goto err_disable_device;
2187
2188         result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2189         if (result)
2190                 goto err_remove_pf;
2191
2192         if (sonypi_compat_init())
2193                 goto err_remove_pf;
2194
2195         return 0;
2196
2197 err_remove_pf:
2198         sony_pf_remove();
2199
2200 err_disable_device:
2201         sony_pic_disable(device);
2202
2203 err_free_irq:
2204         free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2205
2206 err_release_region:
2207         release_region(spic_dev.cur_ioport->io.minimum,
2208                         spic_dev.cur_ioport->io.address_length);
2209
2210 err_remove_input:
2211         sony_laptop_remove_input();
2212
2213 err_free_resources:
2214         list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2215                 list_del(&io->list);
2216                 kfree(io);
2217         }
2218         list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2219                 list_del(&irq->list);
2220                 kfree(irq);
2221         }
2222         spic_dev.cur_ioport = NULL;
2223         spic_dev.cur_irq = NULL;
2224
2225         return result;
2226 }
2227
2228 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2229 {
2230         if (sony_pic_disable(device))
2231                 return -ENXIO;
2232         return 0;
2233 }
2234
2235 static int sony_pic_resume(struct acpi_device *device)
2236 {
2237         sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2238         return 0;
2239 }
2240
2241 static struct acpi_driver sony_pic_driver = {
2242         .name = SONY_PIC_DRIVER_NAME,
2243         .class = SONY_PIC_CLASS,
2244         .ids = SONY_PIC_HID,
2245         .owner = THIS_MODULE,
2246         .ops = {
2247                 .add = sony_pic_add,
2248                 .remove = sony_pic_remove,
2249                 .suspend = sony_pic_suspend,
2250                 .resume = sony_pic_resume,
2251                 },
2252 };
2253
2254 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2255         {
2256                 .ident = "Sony Vaio",
2257                 .matches = {
2258                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2259                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2260                 },
2261         },
2262         {
2263                 .ident = "Sony Vaio",
2264                 .matches = {
2265                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2266                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2267                 },
2268         },
2269         { }
2270 };
2271
2272 static int __init sony_laptop_init(void)
2273 {
2274         int result;
2275
2276         if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2277                 result = acpi_bus_register_driver(&sony_pic_driver);
2278                 if (result) {
2279                         printk(KERN_ERR DRV_PFX
2280                                         "Unable to register SPIC driver.");
2281                         goto out;
2282                 }
2283         }
2284
2285         result = acpi_bus_register_driver(&sony_nc_driver);
2286         if (result) {
2287                 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2288                 goto out_unregister_pic;
2289         }
2290
2291         return 0;
2292
2293 out_unregister_pic:
2294         if (!no_spic)
2295                 acpi_bus_unregister_driver(&sony_pic_driver);
2296 out:
2297         return result;
2298 }
2299
2300 static void __exit sony_laptop_exit(void)
2301 {
2302         acpi_bus_unregister_driver(&sony_nc_driver);
2303         if (!no_spic)
2304                 acpi_bus_unregister_driver(&sony_pic_driver);
2305 }
2306
2307 module_init(sony_laptop_init);
2308 module_exit(sony_laptop_exit);