blob: b86134c0b2993c4fcadc7a63b61b4c8e85228eb7 [file] [log] [blame]
Peter Popoveccec87e32008-11-11 14:46:14 -05001/*
2 * Parallel port to Walkera WK-0701 TX joystick
3 *
4 * Copyright (c) 2008 Peter Popovec
5 *
6 * More about driver: <file:Documentation/input/walkera0701.txt>
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13*/
14
Dmitry Torokhove0dba552012-12-14 23:18:21 -080015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Peter Popoveccec87e32008-11-11 14:46:14 -050016
17#define RESERVE 20000
18#define SYNC_PULSE 1306000
19#define BIN0_PULSE 288000
20#define BIN1_PULSE 438000
21
22#define ANALOG_MIN_PULSE 318000
23#define ANALOG_MAX_PULSE 878000
24#define ANALOG_DELTA 80000
25
26#define BIN_SAMPLE ((BIN0_PULSE + BIN1_PULSE) / 2)
27
28#define NO_SYNC 25
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/parport.h>
33#include <linux/input.h>
34#include <linux/hrtimer.h>
35
36MODULE_AUTHOR("Peter Popovec <popovec@fei.tuke.sk>");
37MODULE_DESCRIPTION("Walkera WK-0701 TX as joystick");
38MODULE_LICENSE("GPL");
39
40static unsigned int walkera0701_pp_no;
41module_param_named(port, walkera0701_pp_no, int, 0);
42MODULE_PARM_DESC(port,
43 "Parallel port adapter for Walkera WK-0701 TX (default is 0)");
44
45/*
46 * For now, only one device is supported, if somebody need more devices, code
47 * can be expanded, one struct walkera_dev per device must be allocated and
48 * set up by walkera0701_connect (release of device by walkera0701_disconnect)
49 */
50
51struct walkera_dev {
52 unsigned char buf[25];
53 u64 irq_time, irq_lasttime;
54 int counter;
55 int ack;
56
57 struct input_dev *input_dev;
58 struct hrtimer timer;
59
60 struct parport *parport;
61 struct pardevice *pardevice;
62};
63
64static struct walkera_dev w_dev;
65
66static inline void walkera0701_parse_frame(struct walkera_dev *w)
67{
68 int i;
69 int val1, val2, val3, val4, val5, val6, val7, val8;
Dmitry Torokhove0dba552012-12-14 23:18:21 -080070 int magic, magic_bit;
Peter Popoveccec87e32008-11-11 14:46:14 -050071 int crc1, crc2;
72
73 for (crc1 = crc2 = i = 0; i < 10; i++) {
74 crc1 += w->buf[i] & 7;
75 crc2 += (w->buf[i] & 8) >> 3;
76 }
77 if ((w->buf[10] & 7) != (crc1 & 7))
78 return;
79 if (((w->buf[10] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
80 return;
81 for (crc1 = crc2 = 0, i = 11; i < 23; i++) {
82 crc1 += w->buf[i] & 7;
83 crc2 += (w->buf[i] & 8) >> 3;
84 }
85 if ((w->buf[23] & 7) != (crc1 & 7))
86 return;
87 if (((w->buf[23] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
88 return;
89 val1 = ((w->buf[0] & 7) * 256 + w->buf[1] * 16 + w->buf[2]) >> 2;
90 val1 *= ((w->buf[0] >> 2) & 2) - 1; /* sign */
91 val2 = (w->buf[2] & 1) << 8 | (w->buf[3] << 4) | w->buf[4];
92 val2 *= (w->buf[2] & 2) - 1; /* sign */
93 val3 = ((w->buf[5] & 7) * 256 + w->buf[6] * 16 + w->buf[7]) >> 2;
94 val3 *= ((w->buf[5] >> 2) & 2) - 1; /* sign */
95 val4 = (w->buf[7] & 1) << 8 | (w->buf[8] << 4) | w->buf[9];
96 val4 *= (w->buf[7] & 2) - 1; /* sign */
97 val5 = ((w->buf[11] & 7) * 256 + w->buf[12] * 16 + w->buf[13]) >> 2;
98 val5 *= ((w->buf[11] >> 2) & 2) - 1; /* sign */
99 val6 = (w->buf[13] & 1) << 8 | (w->buf[14] << 4) | w->buf[15];
100 val6 *= (w->buf[13] & 2) - 1; /* sign */
101 val7 = ((w->buf[16] & 7) * 256 + w->buf[17] * 16 + w->buf[18]) >> 2;
102 val7 *= ((w->buf[16] >> 2) & 2) - 1; /*sign */
103 val8 = (w->buf[18] & 1) << 8 | (w->buf[19] << 4) | w->buf[20];
104 val8 *= (w->buf[18] & 2) - 1; /*sign */
105
Dmitry Torokhove0dba552012-12-14 23:18:21 -0800106 magic = (w->buf[21] << 4) | w->buf[22];
107 magic_bit = (w->buf[24] & 8) >> 3;
108 pr_debug("%4d %4d %4d %4d %4d %4d %4d %4d (magic %2x %d)\n",
109 val1, val2, val3, val4, val5, val6, val7, val8,
110 magic, magic_bit);
111
Peter Popoveccec87e32008-11-11 14:46:14 -0500112 input_report_abs(w->input_dev, ABS_X, val2);
113 input_report_abs(w->input_dev, ABS_Y, val1);
114 input_report_abs(w->input_dev, ABS_Z, val6);
115 input_report_abs(w->input_dev, ABS_THROTTLE, val3);
116 input_report_abs(w->input_dev, ABS_RUDDER, val4);
117 input_report_abs(w->input_dev, ABS_MISC, val7);
118 input_report_key(w->input_dev, BTN_GEAR_DOWN, val5 > 0);
119}
120
121static inline int read_ack(struct pardevice *p)
122{
123 return parport_read_status(p->port) & 0x40;
124}
125
126/* falling edge, prepare to BIN value calculation */
127static void walkera0701_irq_handler(void *handler_data)
128{
129 u64 pulse_time;
130 struct walkera_dev *w = handler_data;
131
132 w->irq_time = ktime_to_ns(ktime_get());
133 pulse_time = w->irq_time - w->irq_lasttime;
134 w->irq_lasttime = w->irq_time;
135
136 /* cancel timer, if in handler or active do resync */
137 if (unlikely(0 != hrtimer_try_to_cancel(&w->timer))) {
138 w->counter = NO_SYNC;
139 return;
140 }
141
142 if (w->counter < NO_SYNC) {
143 if (w->ack) {
144 pulse_time -= BIN1_PULSE;
145 w->buf[w->counter] = 8;
146 } else {
147 pulse_time -= BIN0_PULSE;
148 w->buf[w->counter] = 0;
149 }
150 if (w->counter == 24) { /* full frame */
151 walkera0701_parse_frame(w);
152 w->counter = NO_SYNC;
153 if (abs(pulse_time - SYNC_PULSE) < RESERVE) /* new frame sync */
154 w->counter = 0;
155 } else {
156 if ((pulse_time > (ANALOG_MIN_PULSE - RESERVE)
157 && (pulse_time < (ANALOG_MAX_PULSE + RESERVE)))) {
158 pulse_time -= (ANALOG_MIN_PULSE - RESERVE);
159 pulse_time = (u32) pulse_time / ANALOG_DELTA; /* overtiping is safe, pulsetime < s32.. */
160 w->buf[w->counter++] |= (pulse_time & 7);
161 } else
162 w->counter = NO_SYNC;
163 }
164 } else if (abs(pulse_time - SYNC_PULSE - BIN0_PULSE) <
165 RESERVE + BIN1_PULSE - BIN0_PULSE) /* frame sync .. */
166 w->counter = 0;
167
168 hrtimer_start(&w->timer, ktime_set(0, BIN_SAMPLE), HRTIMER_MODE_REL);
169}
170
171static enum hrtimer_restart timer_handler(struct hrtimer
172 *handle)
173{
174 struct walkera_dev *w;
175
176 w = container_of(handle, struct walkera_dev, timer);
177 w->ack = read_ack(w->pardevice);
178
179 return HRTIMER_NORESTART;
180}
181
182static int walkera0701_open(struct input_dev *dev)
183{
184 struct walkera_dev *w = input_get_drvdata(dev);
185
186 parport_enable_irq(w->parport);
187 return 0;
188}
189
190static void walkera0701_close(struct input_dev *dev)
191{
192 struct walkera_dev *w = input_get_drvdata(dev);
193
194 parport_disable_irq(w->parport);
Peter Popoveca455e292012-12-14 22:57:25 -0800195 hrtimer_cancel(&w->timer);
Peter Popoveccec87e32008-11-11 14:46:14 -0500196}
197
198static int walkera0701_connect(struct walkera_dev *w, int parport)
199{
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800200 int error;
Peter Popoveccec87e32008-11-11 14:46:14 -0500201
202 w->parport = parport_find_number(parport);
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800203 if (!w->parport) {
204 pr_err("parport %d does not exist\n", parport);
Peter Popoveccec87e32008-11-11 14:46:14 -0500205 return -ENODEV;
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800206 }
Peter Popoveccec87e32008-11-11 14:46:14 -0500207
208 if (w->parport->irq == -1) {
Dmitry Torokhove0dba552012-12-14 23:18:21 -0800209 pr_err("parport %d does not have interrupt assigned\n",
210 parport);
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800211 error = -EINVAL;
212 goto err_put_parport;
Peter Popoveccec87e32008-11-11 14:46:14 -0500213 }
214
Peter Popoveccec87e32008-11-11 14:46:14 -0500215 w->pardevice = parport_register_device(w->parport, "walkera0701",
216 NULL, NULL, walkera0701_irq_handler,
217 PARPORT_DEV_EXCL, w);
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800218 if (!w->pardevice) {
219 pr_err("failed to register parport device\n");
220 error = -EIO;
221 goto err_put_parport;
222 }
Peter Popoveccec87e32008-11-11 14:46:14 -0500223
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800224 if (parport_negotiate(w->pardevice->port, IEEE1284_MODE_COMPAT)) {
225 pr_err("failed to negotiate parport mode\n");
226 error = -EIO;
227 goto err_unregister_device;
228 }
Peter Popoveccec87e32008-11-11 14:46:14 -0500229
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800230 if (parport_claim(w->pardevice)) {
231 pr_err("failed to claim parport\n");
232 error = -EBUSY;
233 goto err_unregister_device;
234 }
Peter Popoveccec87e32008-11-11 14:46:14 -0500235
Peter Popoveca455e292012-12-14 22:57:25 -0800236 hrtimer_init(&w->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
237 w->timer.function = timer_handler;
238
Peter Popoveccec87e32008-11-11 14:46:14 -0500239 w->input_dev = input_allocate_device();
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800240 if (!w->input_dev) {
241 pr_err("failed to allocate input device\n");
242 error = -ENOMEM;
243 goto err_release_parport;
244 }
Peter Popoveccec87e32008-11-11 14:46:14 -0500245
246 input_set_drvdata(w->input_dev, w);
247 w->input_dev->name = "Walkera WK-0701 TX";
248 w->input_dev->phys = w->parport->name;
249 w->input_dev->id.bustype = BUS_PARPORT;
250
251 /* TODO what id vendor/product/version ? */
252 w->input_dev->id.vendor = 0x0001;
253 w->input_dev->id.product = 0x0001;
254 w->input_dev->id.version = 0x0100;
Dmitry Torokhov1932c8a2012-12-14 23:13:24 -0800255 w->input_dev->dev.parent = w->parport->dev;
Peter Popoveccec87e32008-11-11 14:46:14 -0500256 w->input_dev->open = walkera0701_open;
257 w->input_dev->close = walkera0701_close;
258
259 w->input_dev->evbit[0] = BIT(EV_ABS) | BIT_MASK(EV_KEY);
260 w->input_dev->keybit[BIT_WORD(BTN_GEAR_DOWN)] = BIT_MASK(BTN_GEAR_DOWN);
261
262 input_set_abs_params(w->input_dev, ABS_X, -512, 512, 0, 0);
263 input_set_abs_params(w->input_dev, ABS_Y, -512, 512, 0, 0);
264 input_set_abs_params(w->input_dev, ABS_Z, -512, 512, 0, 0);
265 input_set_abs_params(w->input_dev, ABS_THROTTLE, -512, 512, 0, 0);
266 input_set_abs_params(w->input_dev, ABS_RUDDER, -512, 512, 0, 0);
267 input_set_abs_params(w->input_dev, ABS_MISC, -512, 512, 0, 0);
268
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800269 error = input_register_device(w->input_dev);
270 if (error) {
271 pr_err("failed to register input device\n");
272 goto err_free_input_dev;
273 }
Peter Popoveccec87e32008-11-11 14:46:14 -0500274
Peter Popoveccec87e32008-11-11 14:46:14 -0500275 return 0;
276
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800277err_free_input_dev:
Peter Popoveccec87e32008-11-11 14:46:14 -0500278 input_free_device(w->input_dev);
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800279err_release_parport:
Peter Popoveccec87e32008-11-11 14:46:14 -0500280 parport_release(w->pardevice);
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800281err_unregister_device:
Peter Popoveccec87e32008-11-11 14:46:14 -0500282 parport_unregister_device(w->pardevice);
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800283err_put_parport:
Peter Popoveccec87e32008-11-11 14:46:14 -0500284 parport_put_port(w->parport);
Dmitry Torokhovea05ae02012-12-16 22:38:45 -0800285 return error;
Peter Popoveccec87e32008-11-11 14:46:14 -0500286}
287
288static void walkera0701_disconnect(struct walkera_dev *w)
289{
Peter Popoveccec87e32008-11-11 14:46:14 -0500290 input_unregister_device(w->input_dev);
291 parport_release(w->pardevice);
292 parport_unregister_device(w->pardevice);
293 parport_put_port(w->parport);
294}
295
296static int __init walkera0701_init(void)
297{
298 return walkera0701_connect(&w_dev, walkera0701_pp_no);
299}
300
301static void __exit walkera0701_exit(void)
302{
303 walkera0701_disconnect(&w_dev);
304}
305
306module_init(walkera0701_init);
307module_exit(walkera0701_exit);