Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/kmemtrace.git] / drivers / hwmon / ams / ams-input.c
blobca7095d96ad0f0f1a786b095f414bb27138c8a00
1 /*
2 * Apple Motion Sensor driver (joystick emulation)
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include "ams.h"
22 static unsigned int joystick;
23 module_param(joystick, bool, 0644);
24 MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
26 static unsigned int invert;
27 module_param(invert, bool, 0644);
28 MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
30 static int ams_input_kthread(void *data)
32 s8 x, y, z;
34 while (!kthread_should_stop()) {
35 mutex_lock(&ams_info.lock);
37 ams_sensors(&x, &y, &z);
39 x -= ams_info.xcalib;
40 y -= ams_info.ycalib;
41 z -= ams_info.zcalib;
43 input_report_abs(ams_info.idev, ABS_X, invert ? -x : x);
44 input_report_abs(ams_info.idev, ABS_Y, invert ? -y : y);
45 input_report_abs(ams_info.idev, ABS_Z, z);
47 input_sync(ams_info.idev);
49 mutex_unlock(&ams_info.lock);
51 msleep(25);
54 return 0;
57 static int ams_input_open(struct input_dev *dev)
59 ams_info.kthread = kthread_run(ams_input_kthread, NULL, "kams");
60 return IS_ERR(ams_info.kthread) ? PTR_ERR(ams_info.kthread) : 0;
63 static void ams_input_close(struct input_dev *dev)
65 kthread_stop(ams_info.kthread);
68 /* Call with ams_info.lock held! */
69 static void ams_input_enable(void)
71 s8 x, y, z;
73 if (ams_info.idev)
74 return;
76 ams_sensors(&x, &y, &z);
77 ams_info.xcalib = x;
78 ams_info.ycalib = y;
79 ams_info.zcalib = z;
81 ams_info.idev = input_allocate_device();
82 if (!ams_info.idev)
83 return;
85 ams_info.idev->name = "Apple Motion Sensor";
86 ams_info.idev->id.bustype = ams_info.bustype;
87 ams_info.idev->id.vendor = 0;
88 ams_info.idev->open = ams_input_open;
89 ams_info.idev->close = ams_input_close;
90 ams_info.idev->dev.parent = &ams_info.of_dev->dev;
92 input_set_abs_params(ams_info.idev, ABS_X, -50, 50, 3, 0);
93 input_set_abs_params(ams_info.idev, ABS_Y, -50, 50, 3, 0);
94 input_set_abs_params(ams_info.idev, ABS_Z, -50, 50, 3, 0);
96 set_bit(EV_ABS, ams_info.idev->evbit);
97 set_bit(EV_KEY, ams_info.idev->evbit);
98 set_bit(BTN_TOUCH, ams_info.idev->keybit);
100 if (input_register_device(ams_info.idev)) {
101 input_free_device(ams_info.idev);
102 ams_info.idev = NULL;
103 return;
107 /* Call with ams_info.lock held! */
108 static void ams_input_disable(void)
110 if (ams_info.idev) {
111 input_unregister_device(ams_info.idev);
112 ams_info.idev = NULL;
116 static ssize_t ams_input_show_joystick(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 return sprintf(buf, "%d\n", joystick);
122 static ssize_t ams_input_store_joystick(struct device *dev,
123 struct device_attribute *attr, const char *buf, size_t count)
125 if (sscanf(buf, "%d\n", &joystick) != 1)
126 return -EINVAL;
128 mutex_lock(&ams_info.lock);
130 if (joystick)
131 ams_input_enable();
132 else
133 ams_input_disable();
135 mutex_unlock(&ams_info.lock);
137 return count;
140 static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
141 ams_input_show_joystick, ams_input_store_joystick);
143 /* Call with ams_info.lock held! */
144 int ams_input_init(void)
146 int result;
148 result = device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
150 if (!result && joystick)
151 ams_input_enable();
152 return result;
155 /* Call with ams_info.lock held! */
156 void ams_input_exit(void)
158 ams_input_disable();
159 device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);