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>
65 #ifdef CONFIG_SONY_LAPTOP_OLD
66 #include <linux/poll.h>
67 #include <linux/miscdevice.h>
70 #define DRV_PFX "sony-laptop: "
71 #define dprintk(msg...) do { \
72 if (debug) printk(KERN_WARNING DRV_PFX msg); \
75 #define SONY_LAPTOP_DRIVER_VERSION "0.5"
77 #define SONY_NC_CLASS "sony-nc"
78 #define SONY_NC_HID "SNY5001"
79 #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
81 #define SONY_PIC_CLASS "sony-pic"
82 #define SONY_PIC_HID "SNY6001"
83 #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
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);
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");
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");
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");
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)");
110 static int camera; /* = 0 */
111 module_param(camera, int, 0444);
112 MODULE_PARM_DESC(camera,
113 "set this to 1 to enable Motion Eye camera controls "
114 "(only use it if you have a C1VE or C1VN model)");
116 #ifdef CONFIG_SONY_LAPTOP_OLD
117 static int minor = -1;
118 module_param(minor, int, 0);
119 MODULE_PARM_DESC(minor,
120 "minor number of the misc device for the SPIC compatibility code, "
121 "default is -1 (automatic)");
124 /*********** Input Devices ***********/
126 #define SONY_LAPTOP_BUF_SIZE 128
127 struct sony_laptop_input_s {
129 struct input_dev *jog_dev;
130 struct input_dev *key_dev;
132 spinlock_t fifo_lock;
133 struct workqueue_struct *wq;
135 static struct sony_laptop_input_s sony_laptop_input = {
136 .users = ATOMIC_INIT(0),
139 struct sony_laptop_keypress {
140 struct input_dev *dev;
144 /* Correspondance table between sonypi events and input layer events */
148 } sony_laptop_inputkeys[] = {
149 { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
150 { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
151 { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
152 { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
153 { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
154 { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
155 { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
156 { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
157 { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
158 { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
159 { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
160 { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
161 { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
162 { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
163 { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
164 { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
165 { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
166 { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
167 { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
168 { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
169 { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
170 { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
171 { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
172 { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
173 { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
174 { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
175 { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
176 { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
177 { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
178 { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
179 { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
183 /* release buttons after a short delay if pressed */
184 static void do_sony_laptop_release_key(struct work_struct *work)
186 struct sony_laptop_keypress kp;
188 while (kfifo_get(sony_laptop_input.fifo, (unsigned char *)&kp,
189 sizeof(kp)) == sizeof(kp)) {
191 input_report_key(kp.dev, kp.key, 0);
195 static DECLARE_WORK(sony_laptop_release_key_work,
196 do_sony_laptop_release_key);
198 /* forward event to the input subsytem */
199 static void sony_laptop_report_input_event(u8 event)
201 struct input_dev *jog_dev = sony_laptop_input.jog_dev;
202 struct input_dev *key_dev = sony_laptop_input.key_dev;
203 struct sony_laptop_keypress kp = { NULL };
206 if (event == SONYPI_EVENT_FNKEY_RELEASED) {
207 /* Nothing, not all VAIOs generate this event */
214 case SONYPI_EVENT_JOGDIAL_UP:
215 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
216 input_report_rel(jog_dev, REL_WHEEL, 1);
220 case SONYPI_EVENT_JOGDIAL_DOWN:
221 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
222 input_report_rel(jog_dev, REL_WHEEL, -1);
227 case SONYPI_EVENT_JOGDIAL_PRESSED:
233 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
234 if (event == sony_laptop_inputkeys[i].sonypiev) {
236 kp.key = sony_laptop_inputkeys[i].inputev;
243 input_report_key(kp.dev, kp.key, 1);
245 kfifo_put(sony_laptop_input.fifo,
246 (unsigned char *)&kp, sizeof(kp));
248 if (!work_pending(&sony_laptop_release_key_work))
249 queue_work(sony_laptop_input.wq,
250 &sony_laptop_release_key_work);
252 dprintk("unknown input event %.2x\n", event);
255 static int sony_laptop_setup_input(void)
257 struct input_dev *jog_dev;
258 struct input_dev *key_dev;
262 /* don't run again if already initialized */
263 if (atomic_add_return(1, &sony_laptop_input.users) > 1)
267 spin_lock_init(&sony_laptop_input.fifo_lock);
268 sony_laptop_input.fifo =
269 kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
270 &sony_laptop_input.fifo_lock);
271 if (IS_ERR(sony_laptop_input.fifo)) {
272 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
273 error = PTR_ERR(sony_laptop_input.fifo);
278 sony_laptop_input.wq = create_singlethread_workqueue("sony-laptop");
279 if (!sony_laptop_input.wq) {
280 printk(KERN_ERR DRV_PFX
281 "Unabe to create workqueue.\n");
287 key_dev = input_allocate_device();
293 key_dev->name = "Sony Vaio Keys";
294 key_dev->id.bustype = BUS_ISA;
295 key_dev->id.vendor = PCI_VENDOR_ID_SONY;
297 /* Initialize the Input Drivers: special keys */
298 key_dev->evbit[0] = BIT(EV_KEY);
299 for (i = 0; sony_laptop_inputkeys[i].sonypiev; i++)
300 if (sony_laptop_inputkeys[i].inputev)
301 set_bit(sony_laptop_inputkeys[i].inputev,
304 error = input_register_device(key_dev);
306 goto err_free_keydev;
308 sony_laptop_input.key_dev = key_dev;
311 jog_dev = input_allocate_device();
314 goto err_unregister_keydev;
317 jog_dev->name = "Sony Vaio Jogdial";
318 jog_dev->id.bustype = BUS_ISA;
319 jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
321 jog_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
322 jog_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
323 jog_dev->relbit[0] = BIT(REL_WHEEL);
325 error = input_register_device(jog_dev);
327 goto err_free_jogdev;
329 sony_laptop_input.jog_dev = jog_dev;
334 input_free_device(jog_dev);
336 err_unregister_keydev:
337 input_unregister_device(key_dev);
338 /* to avoid kref underflow below at input_free_device */
342 input_free_device(key_dev);
345 destroy_workqueue(sony_laptop_input.wq);
348 kfifo_free(sony_laptop_input.fifo);
351 atomic_dec(&sony_laptop_input.users);
355 static void sony_laptop_remove_input(void)
357 /* cleanup only after the last user has gone */
358 if (!atomic_dec_and_test(&sony_laptop_input.users))
361 /* flush workqueue first */
362 flush_workqueue(sony_laptop_input.wq);
364 /* destroy input devs */
365 input_unregister_device(sony_laptop_input.key_dev);
366 sony_laptop_input.key_dev = NULL;
368 if (sony_laptop_input.jog_dev) {
369 input_unregister_device(sony_laptop_input.jog_dev);
370 sony_laptop_input.jog_dev = NULL;
373 destroy_workqueue(sony_laptop_input.wq);
374 kfifo_free(sony_laptop_input.fifo);
377 /*********** Platform Device ***********/
379 static atomic_t sony_pf_users = ATOMIC_INIT(0);
380 static struct platform_driver sony_pf_driver = {
382 .name = "sony-laptop",
383 .owner = THIS_MODULE,
386 static struct platform_device *sony_pf_device;
388 static int sony_pf_add(void)
392 /* don't run again if already initialized */
393 if (atomic_add_return(1, &sony_pf_users) > 1)
396 ret = platform_driver_register(&sony_pf_driver);
400 sony_pf_device = platform_device_alloc("sony-laptop", -1);
401 if (!sony_pf_device) {
403 goto out_platform_registered;
406 ret = platform_device_add(sony_pf_device);
408 goto out_platform_alloced;
412 out_platform_alloced:
413 platform_device_put(sony_pf_device);
414 sony_pf_device = NULL;
415 out_platform_registered:
416 platform_driver_unregister(&sony_pf_driver);
418 atomic_dec(&sony_pf_users);
422 static void sony_pf_remove(void)
424 /* deregister only after the last user has gone */
425 if (!atomic_dec_and_test(&sony_pf_users))
428 platform_device_del(sony_pf_device);
429 platform_device_put(sony_pf_device);
430 platform_driver_unregister(&sony_pf_driver);
433 /*********** SNC (SNY5001) Device ***********/
435 /* the device uses 1-based values, while the backlight subsystem uses
437 #define SONY_MAX_BRIGHTNESS 8
439 #define SNC_VALIDATE_IN 0
440 #define SNC_VALIDATE_OUT 1
442 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
444 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
445 const char *, size_t);
446 static int boolean_validate(const int, const int);
447 static int brightness_default_validate(const int, const int);
449 struct sony_nc_value {
450 char *name; /* name of the entry */
451 char **acpiget; /* names of the ACPI get function */
452 char **acpiset; /* names of the ACPI set function */
453 int (*validate)(const int, const int); /* input/output validation */
454 int value; /* current setting */
455 int valid; /* Has ever been set */
456 int debug; /* active only in debug mode ? */
457 struct device_attribute devattr; /* sysfs atribute */
460 #define SNC_HANDLE_NAMES(_name, _values...) \
461 static char *snc_##_name[] = { _values, NULL }
463 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
465 .name = __stringify(_name), \
466 .acpiget = _getters, \
467 .acpiset = _setters, \
468 .validate = _validate, \
470 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
473 #define SNC_HANDLE_NULL { .name = NULL }
475 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
477 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
478 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
480 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
481 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
483 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
484 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
486 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
487 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
489 SNC_HANDLE_NAMES(PID_get, "GPID");
491 SNC_HANDLE_NAMES(CTR_get, "GCTR");
492 SNC_HANDLE_NAMES(CTR_set, "SCTR");
494 SNC_HANDLE_NAMES(PCR_get, "GPCR");
495 SNC_HANDLE_NAMES(PCR_set, "SPCR");
497 SNC_HANDLE_NAMES(CMI_get, "GCMI");
498 SNC_HANDLE_NAMES(CMI_set, "SCMI");
500 static struct sony_nc_value sony_nc_values[] = {
501 SNC_HANDLE(brightness_default, snc_brightness_def_get,
502 snc_brightness_def_set, brightness_default_validate, 0),
503 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
504 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
505 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
506 boolean_validate, 0),
507 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
508 boolean_validate, 1),
509 /* unknown methods */
510 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
511 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
512 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
513 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
517 static acpi_handle sony_nc_acpi_handle;
518 static struct acpi_device *sony_nc_acpi_device = NULL;
521 * acpi_evaluate_object wrappers
523 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
525 struct acpi_buffer output;
526 union acpi_object out_obj;
529 output.length = sizeof(out_obj);
530 output.pointer = &out_obj;
532 status = acpi_evaluate_object(handle, name, NULL, &output);
533 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
534 *result = out_obj.integer.value;
538 printk(KERN_WARNING DRV_PFX "acpi_callreadfunc failed\n");
543 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
546 struct acpi_object_list params;
547 union acpi_object in_obj;
548 struct acpi_buffer output;
549 union acpi_object out_obj;
553 params.pointer = &in_obj;
554 in_obj.type = ACPI_TYPE_INTEGER;
555 in_obj.integer.value = value;
557 output.length = sizeof(out_obj);
558 output.pointer = &out_obj;
560 status = acpi_evaluate_object(handle, name, ¶ms, &output);
561 if (status == AE_OK) {
562 if (result != NULL) {
563 if (out_obj.type != ACPI_TYPE_INTEGER) {
564 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object bad "
568 *result = out_obj.integer.value;
573 printk(KERN_WARNING DRV_PFX "acpi_evaluate_object failed\n");
579 * sony_nc_values input/output validate functions
582 /* brightness_default_validate:
584 * manipulate input output values to keep consistency with the
585 * backlight framework for which brightness values are 0-based.
587 static int brightness_default_validate(const int direction, const int value)
590 case SNC_VALIDATE_OUT:
592 case SNC_VALIDATE_IN:
593 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
601 * on input validate boolean values 0/1, on output just pass the
604 static int boolean_validate(const int direction, const int value)
606 if (direction == SNC_VALIDATE_IN) {
607 if (value != 0 && value != 1)
614 * Sysfs show/store common to all sony_nc_values
616 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
620 struct sony_nc_value *item =
621 container_of(attr, struct sony_nc_value, devattr);
626 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
630 value = item->validate(SNC_VALIDATE_OUT, value);
632 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
635 static ssize_t sony_nc_sysfs_store(struct device *dev,
636 struct device_attribute *attr,
637 const char *buffer, size_t count)
640 struct sony_nc_value *item =
641 container_of(attr, struct sony_nc_value, devattr);
649 value = simple_strtoul(buffer, NULL, 10);
652 value = item->validate(SNC_VALIDATE_IN, value);
657 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
668 static int sony_backlight_update_status(struct backlight_device *bd)
670 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
671 bd->props.brightness + 1, NULL);
674 static int sony_backlight_get_brightness(struct backlight_device *bd)
678 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
680 /* brightness levels are 1-based, while backlight ones are 0-based */
684 static struct backlight_device *sony_backlight_device;
685 static struct backlight_ops sony_backlight_ops = {
686 .update_status = sony_backlight_update_status,
687 .get_brightness = sony_backlight_get_brightness,
693 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
695 dprintk("sony_acpi_notify, event: %d\n", event);
696 sony_laptop_report_input_event(event);
697 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
700 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
701 void *context, void **return_value)
703 struct acpi_namespace_node *node;
704 union acpi_operand_object *operand;
706 node = (struct acpi_namespace_node *)handle;
707 operand = (union acpi_operand_object *)node->object;
709 printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
710 (u32) operand->method.param_count);
718 static int sony_nc_resume(struct acpi_device *device)
720 struct sony_nc_value *item;
722 for (item = sony_nc_values; item->name; item++) {
727 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
730 printk("%s: %d\n", __FUNCTION__, ret);
737 static int sony_nc_add(struct acpi_device *device)
742 struct sony_nc_value *item;
744 printk(KERN_INFO DRV_PFX "%s v%s.\n",
745 SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
747 sony_nc_acpi_device = device;
748 strcpy(acpi_device_class(device), "sony/hotkey");
750 sony_nc_acpi_handle = device->handle;
753 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
754 1, sony_walk_callback, NULL, NULL);
755 if (ACPI_FAILURE(status)) {
756 printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
762 /* setup input devices and helper fifo */
763 result = sony_laptop_setup_input();
765 printk(KERN_ERR DRV_PFX
766 "Unabe to create input devices.\n");
770 status = acpi_install_notify_handler(sony_nc_acpi_handle,
772 sony_acpi_notify, NULL);
773 if (ACPI_FAILURE(status)) {
774 printk(KERN_WARNING DRV_PFX "unable to install notify handler\n");
779 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
780 sony_backlight_device = backlight_device_register("sony", NULL,
782 &sony_backlight_ops);
784 if (IS_ERR(sony_backlight_device)) {
785 printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
786 sony_backlight_device = NULL;
788 sony_backlight_device->props.brightness =
789 sony_backlight_get_brightness
790 (sony_backlight_device);
791 sony_backlight_device->props.max_brightness =
792 SONY_MAX_BRIGHTNESS - 1;
797 result = sony_pf_add();
801 /* create sony_pf sysfs attributes related to the SNC device */
802 for (item = sony_nc_values; item->name; ++item) {
804 if (!debug && item->debug)
807 /* find the available acpiget as described in the DSDT */
808 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
809 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
812 dprintk("Found %s getter: %s\n",
813 item->name, *item->acpiget);
814 item->devattr.attr.mode |= S_IRUGO;
819 /* find the available acpiset as described in the DSDT */
820 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
821 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
824 dprintk("Found %s setter: %s\n",
825 item->name, *item->acpiset);
826 item->devattr.attr.mode |= S_IWUSR;
831 if (item->devattr.attr.mode != 0) {
833 device_create_file(&sony_pf_device->dev,
843 for (item = sony_nc_values; item->name; ++item) {
844 device_remove_file(&sony_pf_device->dev, &item->devattr);
849 if (sony_backlight_device)
850 backlight_device_unregister(sony_backlight_device);
852 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
855 if (ACPI_FAILURE(status))
856 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
859 sony_laptop_remove_input();
865 static int sony_nc_remove(struct acpi_device *device, int type)
868 struct sony_nc_value *item;
870 if (sony_backlight_device)
871 backlight_device_unregister(sony_backlight_device);
873 sony_nc_acpi_device = NULL;
875 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
878 if (ACPI_FAILURE(status))
879 printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
881 for (item = sony_nc_values; item->name; ++item) {
882 device_remove_file(&sony_pf_device->dev, &item->devattr);
886 sony_laptop_remove_input();
887 dprintk(SONY_NC_DRIVER_NAME " removed.\n");
892 static struct acpi_driver sony_nc_driver = {
893 .name = SONY_NC_DRIVER_NAME,
894 .class = SONY_NC_CLASS,
896 .owner = THIS_MODULE,
899 .remove = sony_nc_remove,
900 .resume = sony_nc_resume,
904 /*********** SPIC (SNY6001) Device ***********/
906 #define SONYPI_DEVICE_TYPE1 0x00000001
907 #define SONYPI_DEVICE_TYPE2 0x00000002
908 #define SONYPI_DEVICE_TYPE3 0x00000004
910 #define SONY_PIC_EV_MASK 0xff
912 struct sony_pic_ioport {
913 struct acpi_resource_io io;
914 struct list_head list;
917 struct sony_pic_irq {
918 struct acpi_resource_irq irq;
919 struct list_head list;
922 struct sony_pic_dev {
926 struct acpi_device *acpi_dev;
927 struct sony_pic_irq *cur_irq;
928 struct sony_pic_ioport *cur_ioport;
929 struct list_head interrupts;
930 struct list_head ioports;
934 static struct sony_pic_dev spic_dev = {
935 .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
936 .ioports = LIST_HEAD_INIT(spic_dev.ioports),
940 #define SONYPI_JOGGER_MASK 0x00000001
941 #define SONYPI_CAPTURE_MASK 0x00000002
942 #define SONYPI_FNKEY_MASK 0x00000004
943 #define SONYPI_BLUETOOTH_MASK 0x00000008
944 #define SONYPI_PKEY_MASK 0x00000010
945 #define SONYPI_BACK_MASK 0x00000020
946 #define SONYPI_HELP_MASK 0x00000040
947 #define SONYPI_LID_MASK 0x00000080
948 #define SONYPI_ZOOM_MASK 0x00000100
949 #define SONYPI_THUMBPHRASE_MASK 0x00000200
950 #define SONYPI_MEYE_MASK 0x00000400
951 #define SONYPI_MEMORYSTICK_MASK 0x00000800
952 #define SONYPI_BATTERY_MASK 0x00001000
953 #define SONYPI_WIRELESS_MASK 0x00002000
955 struct sonypi_event {
960 /* The set of possible button release events */
961 static struct sonypi_event sonypi_releaseev[] = {
962 { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
966 /* The set of possible jogger events */
967 static struct sonypi_event sonypi_joggerev[] = {
968 { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
969 { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
970 { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
971 { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
972 { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
973 { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
974 { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
975 { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
976 { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
977 { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
978 { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
979 { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
980 { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
984 /* The set of possible capture button events */
985 static struct sonypi_event sonypi_captureev[] = {
986 { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
987 { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
988 { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
992 /* The set of possible fnkeys events */
993 static struct sonypi_event sonypi_fnkeyev[] = {
994 { 0x10, SONYPI_EVENT_FNKEY_ESC },
995 { 0x11, SONYPI_EVENT_FNKEY_F1 },
996 { 0x12, SONYPI_EVENT_FNKEY_F2 },
997 { 0x13, SONYPI_EVENT_FNKEY_F3 },
998 { 0x14, SONYPI_EVENT_FNKEY_F4 },
999 { 0x15, SONYPI_EVENT_FNKEY_F5 },
1000 { 0x16, SONYPI_EVENT_FNKEY_F6 },
1001 { 0x17, SONYPI_EVENT_FNKEY_F7 },
1002 { 0x18, SONYPI_EVENT_FNKEY_F8 },
1003 { 0x19, SONYPI_EVENT_FNKEY_F9 },
1004 { 0x1a, SONYPI_EVENT_FNKEY_F10 },
1005 { 0x1b, SONYPI_EVENT_FNKEY_F11 },
1006 { 0x1c, SONYPI_EVENT_FNKEY_F12 },
1007 { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
1008 { 0x21, SONYPI_EVENT_FNKEY_1 },
1009 { 0x22, SONYPI_EVENT_FNKEY_2 },
1010 { 0x31, SONYPI_EVENT_FNKEY_D },
1011 { 0x32, SONYPI_EVENT_FNKEY_E },
1012 { 0x33, SONYPI_EVENT_FNKEY_F },
1013 { 0x34, SONYPI_EVENT_FNKEY_S },
1014 { 0x35, SONYPI_EVENT_FNKEY_B },
1015 { 0x36, SONYPI_EVENT_FNKEY_ONLY },
1019 /* The set of possible program key events */
1020 static struct sonypi_event sonypi_pkeyev[] = {
1021 { 0x01, SONYPI_EVENT_PKEY_P1 },
1022 { 0x02, SONYPI_EVENT_PKEY_P2 },
1023 { 0x04, SONYPI_EVENT_PKEY_P3 },
1024 { 0x5c, SONYPI_EVENT_PKEY_P1 },
1028 /* The set of possible bluetooth events */
1029 static struct sonypi_event sonypi_blueev[] = {
1030 { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
1031 { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
1032 { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
1036 /* The set of possible wireless events */
1037 static struct sonypi_event sonypi_wlessev[] = {
1038 { 0x59, SONYPI_EVENT_WIRELESS_ON },
1039 { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
1043 /* The set of possible back button events */
1044 static struct sonypi_event sonypi_backev[] = {
1045 { 0x20, SONYPI_EVENT_BACK_PRESSED },
1049 /* The set of possible help button events */
1050 static struct sonypi_event sonypi_helpev[] = {
1051 { 0x3b, SONYPI_EVENT_HELP_PRESSED },
1056 /* The set of possible lid events */
1057 static struct sonypi_event sonypi_lidev[] = {
1058 { 0x51, SONYPI_EVENT_LID_CLOSED },
1059 { 0x50, SONYPI_EVENT_LID_OPENED },
1063 /* The set of possible zoom events */
1064 static struct sonypi_event sonypi_zoomev[] = {
1065 { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
1069 /* The set of possible thumbphrase events */
1070 static struct sonypi_event sonypi_thumbphraseev[] = {
1071 { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
1075 /* The set of possible motioneye camera events */
1076 static struct sonypi_event sonypi_meyeev[] = {
1077 { 0x00, SONYPI_EVENT_MEYE_FACE },
1078 { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
1082 /* The set of possible memorystick events */
1083 static struct sonypi_event sonypi_memorystickev[] = {
1084 { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
1085 { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
1089 /* The set of possible battery events */
1090 static struct sonypi_event sonypi_batteryev[] = {
1091 { 0x20, SONYPI_EVENT_BATTERY_INSERT },
1092 { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
1096 static struct sonypi_eventtypes {
1100 struct sonypi_event * events;
1101 } sony_pic_eventtypes[] = {
1102 { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
1103 { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
1104 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
1105 { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
1106 { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
1107 { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1108 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1109 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
1110 { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1111 { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
1113 { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
1114 { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
1115 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
1116 { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
1117 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1118 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
1119 { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
1120 { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
1121 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
1122 { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
1123 { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
1124 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1125 { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1126 { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1128 { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
1129 { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
1130 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
1131 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
1132 { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
1133 { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
1137 static int sony_pic_detect_device_type(void)
1139 struct pci_dev *pcidev;
1142 if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1143 PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
1144 model = SONYPI_DEVICE_TYPE1;
1146 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1147 PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
1148 model = SONYPI_DEVICE_TYPE3;
1150 else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
1151 PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
1152 model = SONYPI_DEVICE_TYPE3;
1155 model = SONYPI_DEVICE_TYPE2;
1158 pci_dev_put(pcidev);
1160 printk(KERN_INFO DRV_PFX "detected Type%d model\n",
1161 model == SONYPI_DEVICE_TYPE1 ? 1 :
1162 model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
1166 #define ITERATIONS_LONG 10000
1167 #define ITERATIONS_SHORT 10
1168 #define wait_on_command(command, iterations) { \
1169 unsigned int n = iterations; \
1170 while (--n && (command)) \
1173 dprintk("command failed at %s : %s (line %d)\n", \
1174 __FILE__, __FUNCTION__, __LINE__); \
1177 static u8 sony_pic_call1(u8 dev)
1181 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1183 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1184 v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
1185 v2 = inb_p(spic_dev.cur_ioport->io.minimum);
1186 dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
1190 static u8 sony_pic_call2(u8 dev, u8 fn)
1194 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1196 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1197 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
1199 outb(fn, spic_dev.cur_ioport->io.minimum);
1200 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1201 dprintk("sony_pic_call2: 0x%.4x\n", v1);
1205 static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
1209 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1210 outb(dev, spic_dev.cur_ioport->io.minimum + 4);
1211 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1212 outb(fn, spic_dev.cur_ioport->io.minimum);
1213 wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
1214 outb(v, spic_dev.cur_ioport->io.minimum);
1215 v1 = inb_p(spic_dev.cur_ioport->io.minimum);
1216 dprintk("sony_pic_call3: 0x%.4x\n", v1);
1220 /* camera tests and poweron/poweroff */
1221 #define SONYPI_CAMERA_PICTURE 5
1222 #define SONYPI_CAMERA_MUTE_MASK 0x40
1223 #define SONYPI_CAMERA_CONTROL 0x10
1224 #define SONYPI_CAMERA_STATUS 7
1225 #define SONYPI_CAMERA_STATUS_READY 0x2
1226 #define SONYPI_CAMERA_STATUS_POSITION 0x4
1228 static int __sony_pic_camera_ready(void)
1232 v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
1233 return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
1236 static int sony_pic_camera_off(void)
1239 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1243 mutex_lock(&spic_dev.lock);
1244 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
1245 SONYPI_CAMERA_MUTE_MASK),
1248 if (spic_dev.camera_power) {
1249 sony_pic_call2(0x91, 0);
1250 spic_dev.camera_power = 0;
1252 mutex_unlock(&spic_dev.lock);
1256 static int sony_pic_camera_on(void)
1262 printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
1266 mutex_lock(&spic_dev.lock);
1267 if (spic_dev.camera_power)
1270 for (j = 5; j > 0; j--) {
1272 for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
1274 sony_pic_call1(0x93);
1276 for (i = 400; i > 0; i--) {
1277 if (__sony_pic_camera_ready())
1286 printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
1291 wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
1295 spic_dev.camera_power = 1;
1298 mutex_unlock(&spic_dev.lock);
1302 static ssize_t sony_pic_camerapower_store(struct device *dev,
1303 struct device_attribute *attr,
1304 const char *buffer, size_t count)
1306 unsigned long value;
1311 value = simple_strtoul(buffer, NULL, 10);
1313 result = sony_pic_camera_on();
1315 result = sony_pic_camera_off();
1323 static ssize_t sony_pic_camerapower_show(struct device *dev,
1324 struct device_attribute *attr, char *buffer)
1327 mutex_lock(&spic_dev.lock);
1328 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.camera_power);
1329 mutex_unlock(&spic_dev.lock);
1333 /* bluetooth subsystem power state */
1334 static void __sony_pic_set_bluetoothpower(u8 state)
1337 if (spic_dev.bluetooth_power == state)
1339 sony_pic_call2(0x96, state);
1340 sony_pic_call1(0x82);
1341 spic_dev.bluetooth_power = state;
1344 static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
1345 struct device_attribute *attr,
1346 const char *buffer, size_t count)
1348 unsigned long value;
1352 value = simple_strtoul(buffer, NULL, 10);
1353 mutex_lock(&spic_dev.lock);
1354 __sony_pic_set_bluetoothpower(value);
1355 mutex_unlock(&spic_dev.lock);
1360 static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
1361 struct device_attribute *attr, char *buffer)
1364 mutex_lock(&spic_dev.lock);
1365 count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
1366 mutex_unlock(&spic_dev.lock);
1371 /* FAN0 information (reverse engineered from ACPI tables) */
1372 #define SONY_PIC_FAN0_STATUS 0x93
1373 static int sony_pic_set_fanspeed(unsigned long value)
1375 return ec_write(SONY_PIC_FAN0_STATUS, value);
1378 static int sony_pic_get_fanspeed(u8 *value)
1380 return ec_read(SONY_PIC_FAN0_STATUS, value);
1383 static ssize_t sony_pic_fanspeed_store(struct device *dev,
1384 struct device_attribute *attr,
1385 const char *buffer, size_t count)
1387 unsigned long value;
1391 value = simple_strtoul(buffer, NULL, 10);
1392 if (sony_pic_set_fanspeed(value))
1398 static ssize_t sony_pic_fanspeed_show(struct device *dev,
1399 struct device_attribute *attr, char *buffer)
1402 if (sony_pic_get_fanspeed(&value))
1405 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
1408 #define SPIC_ATTR(_name, _mode) \
1409 struct device_attribute spic_attr_##_name = __ATTR(_name, \
1410 _mode, sony_pic_## _name ##_show, \
1411 sony_pic_## _name ##_store)
1413 static SPIC_ATTR(camerapower, 0644);
1414 static SPIC_ATTR(bluetoothpower, 0644);
1415 static SPIC_ATTR(fanspeed, 0644);
1417 static struct attribute *spic_attributes[] = {
1418 &spic_attr_camerapower.attr,
1419 &spic_attr_bluetoothpower.attr,
1420 &spic_attr_fanspeed.attr,
1424 static struct attribute_group spic_attribute_group = {
1425 .attrs = spic_attributes
1428 /******** SONYPI compatibility **********/
1429 #ifdef CONFIG_SONY_LAPTOP_OLD
1431 /* battery / brightness / temperature addresses */
1432 #define SONYPI_BAT_FLAGS 0x81
1433 #define SONYPI_LCD_LIGHT 0x96
1434 #define SONYPI_BAT1_PCTRM 0xa0
1435 #define SONYPI_BAT1_LEFT 0xa2
1436 #define SONYPI_BAT1_MAXRT 0xa4
1437 #define SONYPI_BAT2_PCTRM 0xa8
1438 #define SONYPI_BAT2_LEFT 0xaa
1439 #define SONYPI_BAT2_MAXRT 0xac
1440 #define SONYPI_BAT1_MAXTK 0xb0
1441 #define SONYPI_BAT1_FULL 0xb2
1442 #define SONYPI_BAT2_MAXTK 0xb8
1443 #define SONYPI_BAT2_FULL 0xba
1444 #define SONYPI_TEMP_STATUS 0xC1
1446 struct sonypi_compat_s {
1447 struct fasync_struct *fifo_async;
1449 spinlock_t fifo_lock;
1450 wait_queue_head_t fifo_proc_list;
1451 atomic_t open_count;
1453 static struct sonypi_compat_s sonypi_compat = {
1454 .open_count = ATOMIC_INIT(0),
1457 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
1461 retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
1467 static int sonypi_misc_release(struct inode *inode, struct file *file)
1469 sonypi_misc_fasync(-1, file, 0);
1470 atomic_dec(&sonypi_compat.open_count);
1474 static int sonypi_misc_open(struct inode *inode, struct file *file)
1476 /* Flush input queue on first open */
1477 if (atomic_inc_return(&sonypi_compat.open_count) == 1)
1478 kfifo_reset(sonypi_compat.fifo);
1482 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
1483 size_t count, loff_t *pos)
1488 if ((kfifo_len(sonypi_compat.fifo) == 0) &&
1489 (file->f_flags & O_NONBLOCK))
1492 ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
1493 kfifo_len(sonypi_compat.fifo) != 0);
1497 while (ret < count &&
1498 (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
1499 if (put_user(c, buf++))
1505 struct inode *inode = file->f_path.dentry->d_inode;
1506 inode->i_atime = current_fs_time(inode->i_sb);
1512 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
1514 poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
1515 if (kfifo_len(sonypi_compat.fifo))
1516 return POLLIN | POLLRDNORM;
1520 static int ec_read16(u8 addr, u16 *value)
1523 if (ec_read(addr, &val_lb))
1525 if (ec_read(addr + 1, &val_hb))
1527 *value = val_lb | (val_hb << 8);
1531 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
1532 unsigned int cmd, unsigned long arg)
1535 void __user *argp = (void __user *)arg;
1540 mutex_lock(&spic_dev.lock);
1542 case SONYPI_IOCGBRT:
1543 if (sony_backlight_device == NULL) {
1547 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
1551 val8 = ((value & 0xff) - 1) << 5;
1552 if (copy_to_user(argp, &val8, sizeof(val8)))
1555 case SONYPI_IOCSBRT:
1556 if (sony_backlight_device == NULL) {
1560 if (copy_from_user(&val8, argp, sizeof(val8))) {
1564 if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
1565 (val8 >> 5) + 1, NULL)) {
1569 /* sync the backlight device status */
1570 sony_backlight_device->props.brightness =
1571 sony_backlight_get_brightness(sony_backlight_device);
1573 case SONYPI_IOCGBAT1CAP:
1574 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
1578 if (copy_to_user(argp, &val16, sizeof(val16)))
1581 case SONYPI_IOCGBAT1REM:
1582 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
1586 if (copy_to_user(argp, &val16, sizeof(val16)))
1589 case SONYPI_IOCGBAT2CAP:
1590 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
1594 if (copy_to_user(argp, &val16, sizeof(val16)))
1597 case SONYPI_IOCGBAT2REM:
1598 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
1602 if (copy_to_user(argp, &val16, sizeof(val16)))
1605 case SONYPI_IOCGBATFLAGS:
1606 if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
1611 if (copy_to_user(argp, &val8, sizeof(val8)))
1614 case SONYPI_IOCGBLUE:
1615 val8 = spic_dev.bluetooth_power;
1616 if (copy_to_user(argp, &val8, sizeof(val8)))
1619 case SONYPI_IOCSBLUE:
1620 if (copy_from_user(&val8, argp, sizeof(val8))) {
1624 __sony_pic_set_bluetoothpower(val8);
1627 case SONYPI_IOCGFAN:
1628 if (sony_pic_get_fanspeed(&val8)) {
1632 if (copy_to_user(argp, &val8, sizeof(val8)))
1635 case SONYPI_IOCSFAN:
1636 if (copy_from_user(&val8, argp, sizeof(val8))) {
1640 if (sony_pic_set_fanspeed(val8))
1643 /* GET Temperature (useful under APM) */
1644 case SONYPI_IOCGTEMP:
1645 if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
1649 if (copy_to_user(argp, &val8, sizeof(val8)))
1655 mutex_unlock(&spic_dev.lock);
1659 static const struct file_operations sonypi_misc_fops = {
1660 .owner = THIS_MODULE,
1661 .read = sonypi_misc_read,
1662 .poll = sonypi_misc_poll,
1663 .open = sonypi_misc_open,
1664 .release = sonypi_misc_release,
1665 .fasync = sonypi_misc_fasync,
1666 .ioctl = sonypi_misc_ioctl,
1669 static struct miscdevice sonypi_misc_device = {
1670 .minor = MISC_DYNAMIC_MINOR,
1672 .fops = &sonypi_misc_fops,
1675 static void sonypi_compat_report_event(u8 event)
1677 kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
1678 kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
1679 wake_up_interruptible(&sonypi_compat.fifo_proc_list);
1682 static int sonypi_compat_init(void)
1686 spin_lock_init(&sonypi_compat.fifo_lock);
1687 sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
1688 &sonypi_compat.fifo_lock);
1689 if (IS_ERR(sonypi_compat.fifo)) {
1690 printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
1691 return PTR_ERR(sonypi_compat.fifo);
1694 init_waitqueue_head(&sonypi_compat.fifo_proc_list);
1697 sonypi_misc_device.minor = minor;
1698 error = misc_register(&sonypi_misc_device);
1700 printk(KERN_ERR DRV_PFX "misc_register failed\n");
1701 goto err_free_kfifo;
1704 printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
1705 sonypi_misc_device.minor);
1710 kfifo_free(sonypi_compat.fifo);
1714 static void sonypi_compat_exit(void)
1716 misc_deregister(&sonypi_misc_device);
1717 kfifo_free(sonypi_compat.fifo);
1720 static int sonypi_compat_init(void) { return 0; }
1721 static void sonypi_compat_exit(void) { }
1722 static void sonypi_compat_report_event(u8 event) { }
1723 #endif /* CONFIG_SONY_LAPTOP_OLD */
1729 sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
1732 struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
1734 switch (resource->type) {
1735 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1736 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1739 case ACPI_RESOURCE_TYPE_IRQ:
1741 struct acpi_resource_irq *p = &resource->data.irq;
1742 struct sony_pic_irq *interrupt = NULL;
1743 if (!p || !p->interrupt_count) {
1745 * IRQ descriptors may have no IRQ# bits set,
1746 * particularly those those w/ _STA disabled
1748 dprintk("Blank IRQ resource\n");
1751 for (i = 0; i < p->interrupt_count; i++) {
1752 if (!p->interrupts[i]) {
1753 printk(KERN_WARNING DRV_PFX
1758 interrupt = kzalloc(sizeof(*interrupt),
1763 list_add(&interrupt->list, &dev->interrupts);
1764 interrupt->irq.triggering = p->triggering;
1765 interrupt->irq.polarity = p->polarity;
1766 interrupt->irq.sharable = p->sharable;
1767 interrupt->irq.interrupt_count = 1;
1768 interrupt->irq.interrupts[0] = p->interrupts[i];
1772 case ACPI_RESOURCE_TYPE_IO:
1774 struct acpi_resource_io *io = &resource->data.io;
1775 struct sony_pic_ioport *ioport = NULL;
1777 dprintk("Blank IO resource\n");
1781 ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
1785 list_add(&ioport->list, &dev->ioports);
1786 memcpy(&ioport->io, io, sizeof(*io));
1790 dprintk("Resource %d isn't an IRQ nor an IO port\n",
1793 case ACPI_RESOURCE_TYPE_END_TAG:
1796 return AE_CTRL_TERMINATE;
1799 static int sony_pic_possible_resources(struct acpi_device *device)
1802 acpi_status status = AE_OK;
1807 /* get device status */
1808 /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
1809 dprintk("Evaluating _STA\n");
1810 result = acpi_bus_get_status(device);
1812 printk(KERN_WARNING DRV_PFX "Unable to read status\n");
1816 if (!device->status.enabled)
1817 dprintk("Device disabled\n");
1819 dprintk("Device enabled\n");
1822 * Query and parse 'method'
1824 dprintk("Evaluating %s\n", METHOD_NAME__PRS);
1825 status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
1826 sony_pic_read_possible_resource, &spic_dev);
1827 if (ACPI_FAILURE(status)) {
1828 printk(KERN_WARNING DRV_PFX
1829 "Failure evaluating %s\n",
1838 * Disable the spic device by calling its _DIS method
1840 static int sony_pic_disable(struct acpi_device *device)
1842 if (ACPI_FAILURE(acpi_evaluate_object(device->handle, "_DIS", 0, NULL)))
1845 dprintk("Device disabled\n");
1851 * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
1853 * Call _SRS to set current resources
1855 static int sony_pic_enable(struct acpi_device *device,
1856 struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
1861 struct acpi_resource io_res;
1862 struct acpi_resource irq_res;
1863 struct acpi_resource end;
1865 struct acpi_buffer buffer = { 0, NULL };
1867 if (!ioport || !irq)
1870 /* init acpi_buffer */
1871 resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
1875 buffer.length = sizeof(*resource) + 1;
1876 buffer.pointer = resource;
1878 /* setup io resource */
1879 resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
1880 resource->io_res.length = sizeof(struct acpi_resource);
1881 memcpy(&resource->io_res.data.io, &ioport->io,
1882 sizeof(struct acpi_resource_io));
1884 /* setup irq resource */
1885 resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
1886 resource->irq_res.length = sizeof(struct acpi_resource);
1887 memcpy(&resource->irq_res.data.irq, &irq->irq,
1888 sizeof(struct acpi_resource_irq));
1889 /* we requested a shared irq */
1890 resource->irq_res.data.irq.sharable = ACPI_SHARED;
1892 resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
1894 /* Attempt to set the resource */
1895 dprintk("Evaluating _SRS\n");
1896 status = acpi_set_current_resources(device->handle, &buffer);
1898 /* check for total failure */
1899 if (ACPI_FAILURE(status)) {
1900 printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
1905 /* Necessary device initializations calls (from sonypi) */
1906 sony_pic_call1(0x82);
1907 sony_pic_call2(0x81, 0xff);
1908 sony_pic_call1(compat ? 0x92 : 0x82);
1917 * ISR: some event is available
1920 static irqreturn_t sony_pic_irq(int irq, void *dev_id)
1926 u8 device_event = 0;
1928 struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
1930 acpi_os_read_port(dev->cur_ioport->io.minimum, &port_val,
1931 dev->cur_ioport->io.address_length);
1932 ev = port_val & SONY_PIC_EV_MASK;
1933 data_mask = 0xff & (port_val >> (dev->cur_ioport->io.address_length - 8));
1935 dprintk("event (0x%.8x [%.2x] [%.2x]) at port 0x%.4x\n",
1936 port_val, ev, data_mask, dev->cur_ioport->io.minimum);
1938 if (ev == 0x00 || ev == 0xff)
1941 for (i = 0; sony_pic_eventtypes[i].model; i++) {
1943 if (spic_dev.model != sony_pic_eventtypes[i].model)
1946 if ((data_mask & sony_pic_eventtypes[i].data) !=
1947 sony_pic_eventtypes[i].data)
1950 if (!(mask & sony_pic_eventtypes[i].mask))
1953 for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
1954 if (ev == sony_pic_eventtypes[i].events[j].data) {
1956 sony_pic_eventtypes[i].events[j].event;
1964 sony_laptop_report_input_event(device_event);
1965 acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
1966 sonypi_compat_report_event(device_event);
1976 static int sony_pic_remove(struct acpi_device *device, int type)
1978 struct sony_pic_ioport *io, *tmp_io;
1979 struct sony_pic_irq *irq, *tmp_irq;
1981 sonypi_compat_exit();
1983 if (sony_pic_disable(device)) {
1984 printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
1988 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
1989 release_region(spic_dev.cur_ioport->io.minimum,
1990 spic_dev.cur_ioport->io.address_length);
1992 sony_laptop_remove_input();
1995 sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
1998 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
1999 list_del(&io->list);
2002 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2003 list_del(&irq->list);
2006 spic_dev.cur_ioport = NULL;
2007 spic_dev.cur_irq = NULL;
2009 dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
2013 static int sony_pic_add(struct acpi_device *device)
2016 struct sony_pic_ioport *io, *tmp_io;
2017 struct sony_pic_irq *irq, *tmp_irq;
2019 printk(KERN_INFO DRV_PFX "%s v%s.\n",
2020 SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
2022 spic_dev.acpi_dev = device;
2023 strcpy(acpi_device_class(device), "sony/hotkey");
2024 spic_dev.model = sony_pic_detect_device_type();
2025 mutex_init(&spic_dev.lock);
2027 /* read _PRS resources */
2028 result = sony_pic_possible_resources(device);
2030 printk(KERN_ERR DRV_PFX
2031 "Unabe to read possible resources.\n");
2032 goto err_free_resources;
2035 /* setup input devices and helper fifo */
2036 result = sony_laptop_setup_input();
2038 printk(KERN_ERR DRV_PFX
2039 "Unabe to create input devices.\n");
2040 goto err_free_resources;
2043 /* request io port */
2044 list_for_each_entry(io, &spic_dev.ioports, list) {
2045 if (request_region(io->io.minimum, io->io.address_length,
2046 "Sony Programable I/O Device")) {
2047 dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
2048 io->io.minimum, io->io.maximum,
2049 io->io.address_length);
2050 spic_dev.cur_ioport = io;
2054 if (!spic_dev.cur_ioport) {
2055 printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
2057 goto err_remove_input;
2061 list_for_each_entry(irq, &spic_dev.interrupts, list) {
2062 if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
2063 IRQF_SHARED, "sony-laptop", &spic_dev)) {
2064 dprintk("IRQ: %d - triggering: %d - "
2065 "polarity: %d - shr: %d\n",
2066 irq->irq.interrupts[0],
2067 irq->irq.triggering,
2070 spic_dev.cur_irq = irq;
2074 if (!spic_dev.cur_irq) {
2075 printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
2077 goto err_release_region;
2080 /* set resource status _SRS */
2081 result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2083 printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
2087 spic_dev.bluetooth_power = -1;
2088 /* create device attributes */
2089 result = sony_pf_add();
2091 goto err_disable_device;
2093 result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
2097 if (sonypi_compat_init())
2106 sony_pic_disable(device);
2109 free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
2112 release_region(spic_dev.cur_ioport->io.minimum,
2113 spic_dev.cur_ioport->io.address_length);
2116 sony_laptop_remove_input();
2119 list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
2120 list_del(&io->list);
2123 list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
2124 list_del(&irq->list);
2127 spic_dev.cur_ioport = NULL;
2128 spic_dev.cur_irq = NULL;
2133 static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
2135 if (sony_pic_disable(device))
2140 static int sony_pic_resume(struct acpi_device *device)
2142 sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
2146 static struct acpi_driver sony_pic_driver = {
2147 .name = SONY_PIC_DRIVER_NAME,
2148 .class = SONY_PIC_CLASS,
2149 .ids = SONY_PIC_HID,
2150 .owner = THIS_MODULE,
2152 .add = sony_pic_add,
2153 .remove = sony_pic_remove,
2154 .suspend = sony_pic_suspend,
2155 .resume = sony_pic_resume,
2159 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
2161 .ident = "Sony Vaio",
2163 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2164 DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
2168 .ident = "Sony Vaio",
2170 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
2171 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
2177 static int __init sony_laptop_init(void)
2181 if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
2182 result = acpi_bus_register_driver(&sony_pic_driver);
2184 printk(KERN_ERR DRV_PFX
2185 "Unable to register SPIC driver.");
2190 result = acpi_bus_register_driver(&sony_nc_driver);
2192 printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
2193 goto out_unregister_pic;
2200 acpi_bus_unregister_driver(&sony_pic_driver);
2205 static void __exit sony_laptop_exit(void)
2207 acpi_bus_unregister_driver(&sony_nc_driver);
2209 acpi_bus_unregister_driver(&sony_pic_driver);
2212 module_init(sony_laptop_init);
2213 module_exit(sony_laptop_exit);