blob: 2edae7dfcab25407c4c677bcfad6e4f3bdf4e57f [file] [log] [blame]
Stelian Popdcb69dd2006-12-12 18:18:30 +01001/*
2 * Apple Motion Sensor driver (joystick emulation)
3 *
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
6 *
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.
11 */
12
13#include <linux/module.h>
14
15#include <linux/types.h>
16#include <linux/errno.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19
20#include "ams.h"
21
Rusty Russell90ab5ee2012-01-13 09:32:20 +103022static bool joystick;
Dmitry Torokhov0a020022008-10-17 17:51:12 +020023module_param(joystick, bool, S_IRUGO);
Stelian Popdcb69dd2006-12-12 18:18:30 +010024MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
25
Rusty Russell90ab5ee2012-01-13 09:32:20 +103026static bool invert;
Dmitry Torokhov0a020022008-10-17 17:51:12 +020027module_param(invert, bool, S_IWUSR | S_IRUGO);
Stelian Popdcb69dd2006-12-12 18:18:30 +010028MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
29
Dmitry Torokhovee4cd322008-10-17 17:51:12 +020030static DEFINE_MUTEX(ams_input_mutex);
31
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040032static void ams_idev_poll(struct input_polled_dev *dev)
Stelian Popdcb69dd2006-12-12 18:18:30 +010033{
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040034 struct input_dev *idev = dev->input;
Stelian Popdcb69dd2006-12-12 18:18:30 +010035 s8 x, y, z;
36
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040037 mutex_lock(&ams_info.lock);
Stelian Popdcb69dd2006-12-12 18:18:30 +010038
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040039 ams_sensors(&x, &y, &z);
Stelian Popdcb69dd2006-12-12 18:18:30 +010040
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040041 x -= ams_info.xcalib;
42 y -= ams_info.ycalib;
43 z -= ams_info.zcalib;
Stelian Popdcb69dd2006-12-12 18:18:30 +010044
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040045 input_report_abs(idev, ABS_X, invert ? -x : x);
46 input_report_abs(idev, ABS_Y, invert ? -y : y);
47 input_report_abs(idev, ABS_Z, z);
Stelian Popdcb69dd2006-12-12 18:18:30 +010048
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040049 input_sync(idev);
Stelian Popdcb69dd2006-12-12 18:18:30 +010050
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040051 mutex_unlock(&ams_info.lock);
Stelian Popdcb69dd2006-12-12 18:18:30 +010052}
53
54/* Call with ams_info.lock held! */
Dmitry Torokhovee4cd322008-10-17 17:51:12 +020055static int ams_input_enable(void)
Stelian Popdcb69dd2006-12-12 18:18:30 +010056{
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040057 struct input_dev *input;
Stelian Popdcb69dd2006-12-12 18:18:30 +010058 s8 x, y, z;
Dmitry Torokhovee4cd322008-10-17 17:51:12 +020059 int error;
Stelian Popdcb69dd2006-12-12 18:18:30 +010060
61 ams_sensors(&x, &y, &z);
62 ams_info.xcalib = x;
63 ams_info.ycalib = y;
64 ams_info.zcalib = z;
65
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040066 ams_info.idev = input_allocate_polled_device();
Stelian Popdcb69dd2006-12-12 18:18:30 +010067 if (!ams_info.idev)
Dmitry Torokhovee4cd322008-10-17 17:51:12 +020068 return -ENOMEM;
Stelian Popdcb69dd2006-12-12 18:18:30 +010069
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040070 ams_info.idev->poll = ams_idev_poll;
71 ams_info.idev->poll_interval = 25;
Stelian Popdcb69dd2006-12-12 18:18:30 +010072
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040073 input = ams_info.idev->input;
74 input->name = "Apple Motion Sensor";
75 input->id.bustype = ams_info.bustype;
76 input->id.vendor = 0;
77 input->dev.parent = &ams_info.of_dev->dev;
Stelian Popdcb69dd2006-12-12 18:18:30 +010078
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040079 input_set_abs_params(input, ABS_X, -50, 50, 3, 0);
80 input_set_abs_params(input, ABS_Y, -50, 50, 3, 0);
81 input_set_abs_params(input, ABS_Z, -50, 50, 3, 0);
Stelian Popdcb69dd2006-12-12 18:18:30 +010082
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040083 set_bit(EV_ABS, input->evbit);
84 set_bit(EV_KEY, input->evbit);
85 set_bit(BTN_TOUCH, input->keybit);
86
Dmitry Torokhovee4cd322008-10-17 17:51:12 +020087 error = input_register_polled_device(ams_info.idev);
88 if (error) {
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -040089 input_free_polled_device(ams_info.idev);
Stelian Popdcb69dd2006-12-12 18:18:30 +010090 ams_info.idev = NULL;
Dmitry Torokhovee4cd322008-10-17 17:51:12 +020091 return error;
Stelian Popdcb69dd2006-12-12 18:18:30 +010092 }
Dmitry Torokhovee4cd322008-10-17 17:51:12 +020093
94 joystick = 1;
95
96 return 0;
Stelian Popdcb69dd2006-12-12 18:18:30 +010097}
98
Stelian Popdcb69dd2006-12-12 18:18:30 +010099static void ams_input_disable(void)
100{
101 if (ams_info.idev) {
Dmitry Torokhov3fdbc342007-09-26 00:01:41 -0400102 input_unregister_polled_device(ams_info.idev);
103 input_free_polled_device(ams_info.idev);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100104 ams_info.idev = NULL;
105 }
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200106
107 joystick = 0;
Stelian Popdcb69dd2006-12-12 18:18:30 +0100108}
109
110static ssize_t ams_input_show_joystick(struct device *dev,
111 struct device_attribute *attr, char *buf)
112{
113 return sprintf(buf, "%d\n", joystick);
114}
115
116static ssize_t ams_input_store_joystick(struct device *dev,
117 struct device_attribute *attr, const char *buf, size_t count)
118{
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200119 unsigned long enable;
120 int error = 0;
Jingoo Han7033b642013-07-19 16:16:56 +0900121 int ret;
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200122
Jingoo Han7033b642013-07-19 16:16:56 +0900123 ret = kstrtoul(buf, 0, &enable);
124 if (ret)
125 return ret;
126 if (enable > 1)
Stelian Popdcb69dd2006-12-12 18:18:30 +0100127 return -EINVAL;
128
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200129 mutex_lock(&ams_input_mutex);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100130
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200131 if (enable != joystick) {
132 if (enable)
133 error = ams_input_enable();
134 else
135 ams_input_disable();
136 }
Stelian Popdcb69dd2006-12-12 18:18:30 +0100137
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200138 mutex_unlock(&ams_input_mutex);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100139
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200140 return error ? error : count;
Stelian Popdcb69dd2006-12-12 18:18:30 +0100141}
142
143static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
144 ams_input_show_joystick, ams_input_store_joystick);
145
Stelian Popdcb69dd2006-12-12 18:18:30 +0100146int ams_input_init(void)
147{
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200148 if (joystick)
Stelian Popdcb69dd2006-12-12 18:18:30 +0100149 ams_input_enable();
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200150
151 return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100152}
153
Al Virod7bde2f2007-02-09 16:38:50 +0000154void ams_input_exit(void)
Stelian Popdcb69dd2006-12-12 18:18:30 +0100155{
Stelian Popdcb69dd2006-12-12 18:18:30 +0100156 device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200157
158 mutex_lock(&ams_input_mutex);
159 ams_input_disable();
160 mutex_unlock(&ams_input_mutex);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100161}