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