2 * ACPI Sony Notebook Control Driver (SNC and SPIC)
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5 * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
10 * The SNY6001 driver part is based on the sonypi driver which includes
13 * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
15 * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
17 * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
19 * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
21 * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
23 * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
25 * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
27 * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
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.
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.
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.
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>
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); \
72 #define SONY_LAPTOP_DRIVER_VERSION "0.5"
74 #define SONY_NC_CLASS "sony-nc"
75 #define SONY_NC_HID "SNY5001"
76 #define SONY_NC_DRIVER_NAME "Sony Notebook Control"
78 #define SONY_PIC_CLASS "sony-pic"
79 #define SONY_PIC_HID "SNY6001"
80 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control"
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);
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");
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");
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");
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)");
107 /*********** Input Devices ***********/
109 #define SONY_LAPTOP_BUF_SIZE 128
110 struct sony_laptop_input_s {
112 struct input_dev *jog_dev;
113 struct input_dev *key_dev;
115 spinlock_t fifo_lock;
116 struct workqueue_struct *wq;
118 static struct sony_laptop_input_s sony_laptop_input = {
119 .users = ATOMIC_INIT(0),
122 struct sony_laptop_keypress {
123 struct input_dev *dev;
127 /* Correspondance table between sonypi events and input layer events */
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 },
166 /* release buttons after a short delay if pressed */
167 static void do_sony_laptop_release_key(struct work_struct *work)
169 struct sony_laptop_keypress kp;
171 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
172 sizeof(kp)) == sizeof(kp)) {
174 input_report_key(kp.dev, kp.key, 0);
178 static DECLARE_WORK(sony_laptop_release_key_work,
179 do_sony_laptop_release_key);
181 /* forward event to the input subsytem */
182 static void sony_laptop_report_input_event(u8 event)
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 };
189 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
190 /* Nothing, not all VAIOs generate this event */
197 case SONYPI_EVENT_JOGDIAL_UP:
198 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
199 input_report_rel(jog_dev, REL_WHEEL, 1);
203 case SONYPI_EVENT_JOGDIAL_DOWN:
204 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
205 input_report_rel(jog_dev, REL_WHEEL, -1);
210 case SONYPI_EVENT_JOGDIAL_PRESSED:
216 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
217 if (event == sony_laptop_inputkeys[i].sonypiev) {
219 kp.key = sony_laptop_inputkeys[i].inputev;
226 input_report_key(kp.dev, kp.key, 1);
228 kfifo_put(sony_laptop_input.fifo,
229 (unsigned char *)&kp, sizeof(kp));
231 if (!work_pending(&sony_laptop_release_key_work))
232 queue_work(sony_laptop_input.wq,
233 &sony_laptop_release_key_work);
235 dprintk("unknown input event %.2x\n", event);
238 static int sony_laptop_setup_input(void)
240 struct input_dev *jog_dev;
241 struct input_dev *key_dev;
245 /* don't run again if already initialized */
246 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
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);
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");
270 key_dev = input_allocate_device();
276 key_dev->name = "Sony Vaio Keys";
277 key_dev->id.bustype = BUS_ISA;
278 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
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,
287 error = input_register_device(key_dev);
289 goto err_free_keydev;
291 sony_laptop_input.key_dev = key_dev;
294 jog_dev = input_allocate_device();
297 goto err_unregister_keydev;
300 jog_dev->name = "Sony Vaio Jogdial";
301 jog_dev->id.bustype = BUS_ISA;
302 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
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);
308 error = input_register_device(jog_dev);
310 goto err_free_jogdev;
312 sony_laptop_input.jog_dev = jog_dev;
317 input_free_device(jog_dev);
319 err_unregister_keydev:
320 input_unregister_device(key_dev);
321 /* to avoid kref underflow below at input_free_device */
325 input_free_device(key_dev);
328 destroy_workqueue(sony_laptop_input.wq);
331 kfifo_free(sony_laptop_input.fifo);
334 atomic_dec(&sony_laptop_input.users);
338 static void sony_laptop_remove_input(void)
340 /* cleanup only after the last user has gone */
341 if (!atomic_dec_and_test(&sony_laptop_input.users))
344 /* flush workqueue first */
345 flush_workqueue(sony_laptop_input.wq);
347 /* destroy input devs */
348 input_unregister_device(sony_laptop_input.key_dev);
349 sony_laptop_input.key_dev = NULL;
351 if (sony_laptop_input.jog_dev) {
352 input_unregister_device(sony_laptop_input.jog_dev);
353 sony_laptop_input.jog_dev = NULL;
356 destroy_workqueue(sony_laptop_input.wq);
357 kfifo_free(sony_laptop_input.fifo);
360 /*********** Platform Device ***********/
362 static atomic_t sony_pf_users = ATOMIC_INIT(0);
363 static struct platform_driver sony_pf_driver = {
365 .name = "sony-laptop",
366 .owner = THIS_MODULE,
369 static struct platform_device *sony_pf_device;
371 static int sony_pf_add(void)
375 /* don't run again if already initialized */
376 if (atomic_add_return(1, &sony_pf_users) > 1)
379 ret = platform_driver_register(&sony_pf_driver);
383 sony_pf_device = platform_device_alloc("sony-laptop", -1);
384 if (!sony_pf_device) {
386 goto out_platform_registered;
389 ret = platform_device_add(sony_pf_device);
391 goto out_platform_alloced;
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);
401 atomic_dec(&sony_pf_users);
405 static void sony_pf_remove(void)
407 /* deregister only after the last user has gone */
408 if (!atomic_dec_and_test(&sony_pf_users))
411 platform_device_del(sony_pf_device);
412 platform_device_put(sony_pf_device);
413 platform_driver_unregister(&sony_pf_driver);
416 /*********** SNC (SNY5001) Device ***********/
418 /* the device uses 1-based values, while the backlight subsystem uses
420 #define SONY_MAX_BRIGHTNESS 8
422 #define SNC_VALIDATE_IN 0
423 #define SNC_VALIDATE_OUT 1
425 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
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);
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 */
443 #define SNC_HANDLE_NAMES(_name, _values...) \
444 static char *snc_##_name[] = { _values, NULL }
446 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
448 .name = __stringify(_name), \
449 .acpiget = _getters, \
450 .acpiset = _setters, \
451 .validate = _validate, \
453 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
456 #define SNC_HANDLE_NULL { .name = NULL }
458 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
460 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
461 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
463 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
464 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
466 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
467 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
469 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
470 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
472 SNC_HANDLE_NAMES(PID_get, "GPID");
474 SNC_HANDLE_NAMES(CTR_get, "GCTR");
475 SNC_HANDLE_NAMES(CTR_set, "SCTR");
477 SNC_HANDLE_NAMES(PCR_get, "GPCR");
478 SNC_HANDLE_NAMES(PCR_set, "SPCR");
480 SNC_HANDLE_NAMES(CMI_get, "GCMI");
481 SNC_HANDLE_NAMES(CMI_set, "SCMI");
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),
500 static acpi_handle sony_nc_acpi_handle;
501 static struct acpi_device *sony_nc_acpi_device = NULL;
504 * acpi_evaluate_object wrappers
506 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
508 struct acpi_buffer output;
509 union acpi_object out_obj;
512 output.length = sizeof(out_obj);
513 output.pointer = &out_obj;
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;
521 printk(LOG_PFX "acpi_callreadfunc failed\n");
526 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
529 struct acpi_object_list params;
530 union acpi_object in_obj;
531 struct acpi_buffer output;
532 union acpi_object out_obj;
536 params.pointer = &in_obj;
537 in_obj.type = ACPI_TYPE_INTEGER;
538 in_obj.integer.value = value;
540 output.length = sizeof(out_obj);
541 output.pointer = &out_obj;
543 status = acpi_evaluate_object(handle, name, ¶ms, &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 "
551 *result = out_obj.integer.value;
556 printk(LOG_PFX "acpi_evaluate_object failed\n");
562 * sony_nc_values input/output validate functions
565 /* brightness_default_validate:
567 * manipulate input output values to keep consistency with the
568 * backlight framework for which brightness values are 0-based.
570 static int brightness_default_validate(const int direction, const int value)
573 case SNC_VALIDATE_OUT:
575 case SNC_VALIDATE_IN:
576 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
584 * on input validate boolean values 0/1, on output just pass the
587 static int boolean_validate(const int direction, const int value)
589 if (direction == SNC_VALIDATE_IN) {
590 if (value != 0 && value != 1)
597 * Sysfs show/store common to all sony_nc_values
599 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
603 struct sony_nc_value *item =
604 container_of(attr, struct sony_nc_value, devattr);
609 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
613 value = item->validate(SNC_VALIDATE_OUT, value);
615 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
618 static ssize_t sony_nc_sysfs_store(struct device *dev,
619 struct device_attribute *attr,
620 const char *buffer, size_t count)
623 struct sony_nc_value *item =
624 container_of(attr, struct sony_nc_value, devattr);
632 value = simple_strtoul(buffer, NULL, 10);
635 value = item->validate(SNC_VALIDATE_IN, value);
640 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
651 static int sony_backlight_update_status(struct backlight_device *bd)
653 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
654 bd->props.brightness + 1, NULL);
657 static int sony_backlight_get_brightness(struct backlight_device *bd)
661 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
663 /* brightness levels are 1-based, while backlight ones are 0-based */
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,
676 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
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);
683 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
684 void *context, void **return_value)
686 struct acpi_namespace_node *node;
687 union acpi_operand_object *operand;
689 node = (struct acpi_namespace_node *)handle;
690 operand = (union acpi_operand_object *)node->object;
692 printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
693 (u32) operand->method.param_count);
701 static int sony_nc_resume(struct acpi_device *device)
703 struct sony_nc_value *item;
705 for (item = sony_nc_values; item->name; item++) {
710 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
713 printk("%s: %d\n", __FUNCTION__, ret);
720 static int sony_nc_add(struct acpi_device *device)
725 struct sony_nc_value *item;
727 sony_nc_acpi_device = device;
728 strcpy(acpi_device_class(device), "sony/hotkey");
730 sony_nc_acpi_handle = device->handle;
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");
742 /* setup input devices and helper fifo */
743 result = sony_laptop_setup_input();
745 printk(KERN_ERR DRV_PFX
746 "Unabe to create input devices.\n");
750 status = acpi_install_notify_handler(sony_nc_acpi_handle,
752 sony_acpi_notify, NULL);
753 if (ACPI_FAILURE(status)) {
754 printk(LOG_PFX "unable to install notify handler\n");
759 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
760 sony_backlight_device = backlight_device_register("sony", NULL,
762 &sony_backlight_ops);
764 if (IS_ERR(sony_backlight_device)) {
765 printk(LOG_PFX "unable to register backlight device\n");
766 sony_backlight_device = NULL;
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;
777 result = sony_pf_add();
781 /* create sony_pf sysfs attributes related to the SNC device */
782 for (item = sony_nc_values; item->name; ++item) {
784 if (!debug && item->debug)
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,
792 dprintk("Found %s getter: %s\n",
793 item->name, *item->acpiget);
794 item->devattr.attr.mode |= S_IRUGO;
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,
804 dprintk("Found %s setter: %s\n",
805 item->name, *item->acpiset);
806 item->devattr.attr.mode |= S_IWUSR;
811 if (item->devattr.attr.mode != 0) {
813 device_create_file(&sony_pf_device->dev,
820 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
825 for (item = sony_nc_values; item->name; ++item) {
826 device_remove_file(&sony_pf_device->dev, &item->devattr);
831 if (sony_backlight_device)
832 backlight_device_unregister(sony_backlight_device);
834 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
837 if (ACPI_FAILURE(status))
838 printk(LOG_PFX "unable to remove notify handler\n");
841 sony_laptop_remove_input();
847 static int sony_nc_remove(struct acpi_device *device, int type)
850 struct sony_nc_value *item;
852 if (sony_backlight_device)
853 backlight_device_unregister(sony_backlight_device);
855 sony_nc_acpi_device = NULL;
857 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
860 if (ACPI_FAILURE(status))
861 printk(LOG_PFX "unable to remove notify handler\n");
863 for (item = sony_nc_values; item->name; ++item) {
864 device_remove_file(&sony_pf_device->dev, &item->devattr);
868 sony_laptop_remove_input();
870 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
875 static struct acpi_driver sony_nc_driver = {
876 .name = SONY_NC_DRIVER_NAME,
877 .class = SONY_NC_CLASS,
879 .owner = THIS_MODULE,
882 .remove = sony_nc_remove,
883 .resume = sony_nc_resume,
887 /*********** SPIC (SNY6001) Device ***********/
889 #define SONYPI_DEVICE_TYPE1 0x00000001
890 #define SONYPI_DEVICE_TYPE2 0x00000002
891 #define SONYPI_DEVICE_TYPE3 0x00000004
893 #define SONY_PIC_EV_MASK 0xff
895 struct sony_pic_ioport {
896 struct acpi_resource_io io;
897 struct list_head list;
900 struct sony_pic_irq {
901 struct acpi_resource_irq irq;
902 struct list_head list;
905 struct sony_pic_dev {
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;
916 static struct sony_pic_dev spic_dev = {
917 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
918 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
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
937 struct sonypi_event {
942 /* The set of possible button release events */
943 static struct sonypi_event sonypi_releaseev[] = {
944 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
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 },
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 },
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 },
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 },
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 },
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 },
1025 /* The set of possible back button events */
1026 static struct sonypi_event sonypi_backev[] = {
1027 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1031 /* The set of possible help button events */
1032 static struct sonypi_event sonypi_helpev[] = {
1033 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
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 },
1045 /* The set of possible zoom events */
1046 static struct sonypi_event sonypi_zoomev[] = {
1047 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1051 /* The set of possible thumbphrase events */
1052 static struct sonypi_event sonypi_thumbphraseev[] = {
1053 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
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 },
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 },
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 },
1078 static struct sonypi_eventtypes {
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 },
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 },
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 },
1119 static int sony_pic_detect_device_type(void)
1121 struct pci_dev *pcidev;
1124 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1125 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1126 model = SONYPI_DEVICE_TYPE1;
1128 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1129 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1130 model = SONYPI_DEVICE_TYPE3;
1132 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1133 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1134 model = SONYPI_DEVICE_TYPE3;
1137 model = SONYPI_DEVICE_TYPE2;
1140 pci_dev_put(pcidev);
1142 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1143 model == SONYPI_DEVICE_TYPE1 ? 1 :
1144 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
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)) \
1155 dprintk("command failed at %s : %s (line %d)\n", \
1156 __FILE__, __FUNCTION__, __LINE__); \
1159 static u8 sony_pic_call1(u8 dev)
1163 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
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);
1172 static u8 sony_pic_call2(u8 dev, u8 fn)
1176 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1178 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1179 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
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);
1187 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
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);
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
1210 static int sony_pic_camera_ready(void)
1214 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1215 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1218 static void sony_pic_camera_off(void)
1220 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1221 SONYPI_CAMERA_MUTE_MASK),
1224 if (!spic_dev.camera_power)
1227 sony_pic_call2(0x91, 0);
1228 spic_dev.camera_power = 0;
1231 static void sony_pic_camera_on(void)
1235 if (spic_dev.camera_power)
1238 for (j = 5; j > 0; j--) {
1240 while (sony_pic_call2(0x91, 0x1))
1242 sony_pic_call1(0x93);
1244 for (i = 400; i > 0; i--) {
1245 if (sony_pic_camera_ready())
1254 printk(KERN_WARNING "sonypi: failed to power on camera\n");
1258 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1262 spic_dev.camera_power = 1;
1265 static ssize_t sony_pic_camerapower_store(struct device *dev,
1266 struct device_attribute *attr,
1267 const char *buffer, size_t count)
1269 unsigned long value;
1273 value = simple_strtoul(buffer, NULL, 10);
1275 sony_pic_camera_on();
1277 sony_pic_camera_off();
1282 static ssize_t sony_pic_camerapower_show(struct device *dev,
1283 struct device_attribute *attr, char *buffer)
1285 return snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.camera_power);
1288 /* bluetooth subsystem power state */
1289 static void sony_pic_set_bluetoothpower(u8 state)
1292 if (spic_dev.bluetooth_power == state)
1294 sony_pic_call2(0x96, state);
1295 sony_pic_call1(0x82);
1296 spic_dev.bluetooth_power = state;
1299 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1300 struct device_attribute *attr,
1301 const char *buffer, size_t count)
1303 unsigned long value;
1307 value = simple_strtoul(buffer, NULL, 10);
1308 sony_pic_set_bluetoothpower(value);
1313 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1314 struct device_attribute *attr, char *buffer)
1316 return snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
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)
1326 unsigned long value;
1330 value = simple_strtoul(buffer, NULL, 10);
1331 if (ec_write(SONY_PIC_FAN0_STATUS, value))
1337 static ssize_t sony_pic_fanspeed_show(struct device *dev,
1338 struct device_attribute *attr, char *buffer)
1341 if (ec_read(SONY_PIC_FAN0_STATUS, &value))
1344 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
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)
1352 static SPIC_ATTR(camerapower, 0644);
1353 static SPIC_ATTR(bluetoothpower, 0644);
1354 static SPIC_ATTR(fanspeed, 0644);
1356 static struct attribute *spic_attributes[] = {
1357 &spic_attr_camerapower.attr,
1358 &spic_attr_bluetoothpower.attr,
1359 &spic_attr_fanspeed.attr,
1363 static struct attribute_group spic_attribute_group = {
1364 .attrs = spic_attributes
1371 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1374 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1376 switch (resource->type) {
1377 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1378 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1381 case ACPI_RESOURCE_TYPE_IRQ:
1383 struct acpi_resource_irq *p = &resource->data.irq;
1384 struct sony_pic_irq *interrupt = NULL;
1385 if (!p || !p->interrupt_count) {
1387 * IRQ descriptors may have no IRQ# bits set,
1388 * particularly those those w/ _STA disabled
1390 dprintk("Blank IRQ resource\n");
1393 for (i = 0; i < p->interrupt_count; i++) {
1394 if (!p->interrupts[i]) {
1395 printk(KERN_WARNING DRV_PFX
1400 interrupt = kzalloc(sizeof(*interrupt),
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];
1414 case ACPI_RESOURCE_TYPE_IO:
1416 struct acpi_resource_io *io = &resource->data.io;
1417 struct sony_pic_ioport *ioport = NULL;
1419 dprintk("Blank IO resource\n");
1423 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1427 list_add(&ioport->list, &dev->ioports);
1428 memcpy(&ioport->io, io, sizeof(*io));
1432 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1435 case ACPI_RESOURCE_TYPE_END_TAG:
1438 return AE_CTRL_TERMINATE;
1441 static int sony_pic_possible_resources(struct acpi_device *device)
1444 acpi_status status = AE_OK;
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);
1454 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1458 if (!device->status.enabled)
1459 dprintk("Device disabled\n");
1461 dprintk("Device enabled\n");
1464 * Query and parse 'method'
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",
1480 * Disable the spic device by calling its _DIS method
1482 static int sony_pic_disable(struct acpi_device *device)
1484 if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
1487 dprintk("Device disabled\n");
1493 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1495 * Call _SRS to set current resources
1497 static int sony_pic_enable(struct acpi_device *device,
1498 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1503 struct acpi_resource io_res;
1504 struct acpi_resource irq_res;
1505 struct acpi_resource end;
1507 struct acpi_buffer buffer = { 0, NULL };
1509 if (!ioport || !irq)
1512 /* init acpi_buffer */
1513 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1517 buffer.length = sizeof(*resource) + 1;
1518 buffer.pointer = resource;
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));
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;
1534 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1536 /* Attempt to set the resource */
1537 dprintk("Evaluating _SRS\n");
1538 status = acpi_set_current_resources(device->handle, &buffer);
1540 /* check for total failure */
1541 if (ACPI_FAILURE(status)) {
1542 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
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);
1559 * ISR: some event is available
1562 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
1568 u8 device_event = 0;
1570 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
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));
1577 dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
1578 port_val, ev, data_mask, dev->cur_ioport->io.minimum);
1580 if (ev == 0x00 || ev == 0xff)
1583 for (i = 0; sony_pic_eventtypes[i].model; i++) {
1585 if (spic_dev.model != sony_pic_eventtypes[i].model)
1588 if ((data_mask & sony_pic_eventtypes[i].data) !=
1589 sony_pic_eventtypes[i].data)
1592 if (!(mask & sony_pic_eventtypes[i].mask))
1595 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
1596 if (ev == sony_pic_eventtypes[i].events[j].data) {
1598 sony_pic_eventtypes[i].events[j].event;
1606 sony_laptop_report_input_event(device_event);
1607 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
1617 static int sony_pic_remove(struct acpi_device *device, int type)
1619 struct sony_pic_ioport *io, *tmp_io;
1620 struct sony_pic_irq *irq, *tmp_irq;
1622 if (sony_pic_disable(device)) {
1623 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
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);
1631 sony_laptop_remove_input();
1634 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
1637 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1638 list_del(&io->list);
1641 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
1642 list_del(&irq->list);
1645 spic_dev.cur_ioport = NULL;
1646 spic_dev.cur_irq = NULL;
1648 dprintk("removed.\n");
1652 static int sony_pic_add(struct acpi_device *device)
1655 struct sony_pic_ioport *io, *tmp_io;
1656 struct sony_pic_irq *irq, *tmp_irq;
1658 printk(KERN_INFO DRV_PFX
1659 "Sony Programmable I/O Controller Driver v%s.\n",
1660 SONY_LAPTOP_DRIVER_VERSION);
1662 spic_dev.acpi_dev = device;
1663 strcpy(acpi_device_class(device), "sony/hotkey");
1664 spic_dev.model = sony_pic_detect_device_type();
1666 /* read _PRS resources */
1667 result = sony_pic_possible_resources(device);
1669 printk(KERN_ERR DRV_PFX
1670 "Unabe to read possible resources.\n");
1671 goto err_free_resources;
1674 /* setup input devices and helper fifo */
1675 result = sony_laptop_setup_input();
1677 printk(KERN_ERR DRV_PFX
1678 "Unabe to create input devices.\n");
1679 goto err_free_resources;
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;
1693 if (!spic_dev.cur_ioport) {
1694 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
1696 goto err_remove_input;
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,
1709 spic_dev.cur_irq = irq;
1713 if (!spic_dev.cur_irq) {
1714 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
1716 goto err_release_region;
1719 /* set resource status _SRS */
1720 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
1722 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
1726 spic_dev.bluetooth_power = -1;
1727 /* create device attributes */
1728 result = sony_pf_add();
1730 goto err_disable_device;
1732 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
1742 sony_pic_disable(device);
1745 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
1748 release_region(spic_dev.cur_ioport->io.minimum,
1749 spic_dev.cur_ioport->io.address_length);
1752 sony_laptop_remove_input();
1755 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1756 list_del(&io->list);
1759 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
1760 list_del(&irq->list);
1763 spic_dev.cur_ioport = NULL;
1764 spic_dev.cur_irq = NULL;
1769 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
1771 if (sony_pic_disable(device))
1776 static int sony_pic_resume(struct acpi_device *device)
1778 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
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,
1788 .add = sony_pic_add,
1789 .remove = sony_pic_remove,
1790 .suspend = sony_pic_suspend,
1791 .resume = sony_pic_resume,
1795 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
1797 .ident = "Sony Vaio",
1799 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1800 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
1804 .ident = "Sony Vaio",
1806 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
1807 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
1813 static int __init sony_laptop_init(void)
1817 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
1818 result = acpi_bus_register_driver(&sony_pic_driver);
1820 printk(KERN_ERR DRV_PFX
1821 "Unable to register SPIC driver.");
1826 result = acpi_bus_register_driver(&sony_nc_driver);
1828 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
1829 goto out_unregister_pic;
1836 acpi_bus_unregister_driver(&sony_pic_driver);
1841 static void __exit sony_laptop_exit(void)
1843 acpi_bus_unregister_driver(&sony_nc_driver);
1845 acpi_bus_unregister_driver(&sony_pic_driver);
1848 module_init(sony_laptop_init);
1849 module_exit(sony_laptop_exit);