xref: /linux/drivers/input/joystick/walkera0701.c (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
1 /*
2  *  Parallel port to Walkera WK-0701 TX joystick
3  *
4  *  Copyright (c) 2008 Peter Popovec
5  *
6  *  More about driver:  <file:Documentation/input/devices/walkera0701.rst>
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 
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
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 
36 MODULE_AUTHOR("Peter Popovec <popovec@fei.tuke.sk>");
37 MODULE_DESCRIPTION("Walkera WK-0701 TX as joystick");
38 MODULE_LICENSE("GPL");
39 
40 static unsigned int walkera0701_pp_no;
41 module_param_named(port, walkera0701_pp_no, int, 0);
42 MODULE_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 
51 struct 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 
64 static struct walkera_dev w_dev;
65 
66 static inline void walkera0701_parse_frame(struct walkera_dev *w)
67 {
68 	int i;
69 	int val1, val2, val3, val4, val5, val6, val7, val8;
70 	int magic, magic_bit;
71 	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 
106 	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 
112 	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 
121 static 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 */
127 static 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, BIN_SAMPLE, HRTIMER_MODE_REL);
169 }
170 
171 static 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 
182 static int walkera0701_open(struct input_dev *dev)
183 {
184 	struct walkera_dev *w = input_get_drvdata(dev);
185 
186 	if (parport_claim(w->pardevice))
187 		return -EBUSY;
188 
189 	parport_enable_irq(w->parport);
190 	return 0;
191 }
192 
193 static void walkera0701_close(struct input_dev *dev)
194 {
195 	struct walkera_dev *w = input_get_drvdata(dev);
196 
197 	parport_disable_irq(w->parport);
198 	hrtimer_cancel(&w->timer);
199 
200 	parport_release(w->pardevice);
201 }
202 
203 static void walkera0701_attach(struct parport *pp)
204 {
205 	struct pardev_cb walkera0701_parport_cb;
206 	struct walkera_dev *w = &w_dev;
207 
208 	if (pp->number != walkera0701_pp_no) {
209 		pr_debug("Not using parport%d.\n", pp->number);
210 		return;
211 	}
212 
213 	if (pp->irq == -1) {
214 		pr_err("parport %d does not have interrupt assigned\n",
215 			pp->number);
216 		return;
217 	}
218 
219 	w->parport = pp;
220 
221 	memset(&walkera0701_parport_cb, 0, sizeof(walkera0701_parport_cb));
222 	walkera0701_parport_cb.flags = PARPORT_FLAG_EXCL;
223 	walkera0701_parport_cb.irq_func = walkera0701_irq_handler;
224 	walkera0701_parport_cb.private = w;
225 
226 	w->pardevice = parport_register_dev_model(pp, "walkera0701",
227 						  &walkera0701_parport_cb, 0);
228 
229 	if (!w->pardevice) {
230 		pr_err("failed to register parport device\n");
231 		return;
232 	}
233 
234 	if (parport_negotiate(w->pardevice->port, IEEE1284_MODE_COMPAT)) {
235 		pr_err("failed to negotiate parport mode\n");
236 		goto err_unregister_device;
237 	}
238 
239 	hrtimer_init(&w->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
240 	w->timer.function = timer_handler;
241 
242 	w->input_dev = input_allocate_device();
243 	if (!w->input_dev) {
244 		pr_err("failed to allocate input device\n");
245 		goto err_unregister_device;
246 	}
247 
248 	input_set_drvdata(w->input_dev, w);
249 	w->input_dev->name = "Walkera WK-0701 TX";
250 	w->input_dev->phys = w->parport->name;
251 	w->input_dev->id.bustype = BUS_PARPORT;
252 
253 	/* TODO what id vendor/product/version ? */
254 	w->input_dev->id.vendor = 0x0001;
255 	w->input_dev->id.product = 0x0001;
256 	w->input_dev->id.version = 0x0100;
257 	w->input_dev->dev.parent = w->parport->dev;
258 	w->input_dev->open = walkera0701_open;
259 	w->input_dev->close = walkera0701_close;
260 
261 	w->input_dev->evbit[0] = BIT(EV_ABS) | BIT_MASK(EV_KEY);
262 	w->input_dev->keybit[BIT_WORD(BTN_GEAR_DOWN)] = BIT_MASK(BTN_GEAR_DOWN);
263 
264 	input_set_abs_params(w->input_dev, ABS_X, -512, 512, 0, 0);
265 	input_set_abs_params(w->input_dev, ABS_Y, -512, 512, 0, 0);
266 	input_set_abs_params(w->input_dev, ABS_Z, -512, 512, 0, 0);
267 	input_set_abs_params(w->input_dev, ABS_THROTTLE, -512, 512, 0, 0);
268 	input_set_abs_params(w->input_dev, ABS_RUDDER, -512, 512, 0, 0);
269 	input_set_abs_params(w->input_dev, ABS_MISC, -512, 512, 0, 0);
270 
271 	if (input_register_device(w->input_dev)) {
272 		pr_err("failed to register input device\n");
273 		goto err_free_input_dev;
274 	}
275 
276 	return;
277 
278 err_free_input_dev:
279 	input_free_device(w->input_dev);
280 err_unregister_device:
281 	parport_unregister_device(w->pardevice);
282 }
283 
284 static void walkera0701_detach(struct parport *port)
285 {
286 	struct walkera_dev *w = &w_dev;
287 
288 	if (!w->pardevice || w->parport->number != port->number)
289 		return;
290 
291 	input_unregister_device(w->input_dev);
292 	parport_unregister_device(w->pardevice);
293 	w->parport = NULL;
294 }
295 
296 static struct parport_driver walkera0701_parport_driver = {
297 	.name = "walkera0701",
298 	.match_port = walkera0701_attach,
299 	.detach = walkera0701_detach,
300 	.devmodel = true,
301 };
302 
303 static int __init walkera0701_init(void)
304 {
305 	return parport_register_driver(&walkera0701_parport_driver);
306 }
307 
308 static void __exit walkera0701_exit(void)
309 {
310 	parport_unregister_driver(&walkera0701_parport_driver);
311 }
312 
313 module_init(walkera0701_init);
314 module_exit(walkera0701_exit);
315