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