^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Parallel port to Walkera WK-0701 TX joystick
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2008 Peter Popovec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * More about driver: <file:Documentation/input/devices/walkera0701.rst>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define RESERVE 20000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define SYNC_PULSE 1306000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define BIN0_PULSE 288000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define BIN1_PULSE 438000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define ANALOG_MIN_PULSE 318000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define ANALOG_MAX_PULSE 878000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define ANALOG_DELTA 80000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define BIN_SAMPLE ((BIN0_PULSE + BIN1_PULSE) / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define NO_SYNC 25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/parport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MODULE_AUTHOR("Peter Popovec <popovec@fei.tuke.sk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MODULE_DESCRIPTION("Walkera WK-0701 TX as joystick");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static unsigned int walkera0701_pp_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) module_param_named(port, walkera0701_pp_no, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) MODULE_PARM_DESC(port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) "Parallel port adapter for Walkera WK-0701 TX (default is 0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * For now, only one device is supported, if somebody need more devices, code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * can be expanded, one struct walkera_dev per device must be allocated and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * set up by walkera0701_connect (release of device by walkera0701_disconnect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct walkera_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned char buf[25];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u64 irq_time, irq_lasttime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct hrtimer timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct parport *parport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct pardevice *pardevice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static struct walkera_dev w_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static inline void walkera0701_parse_frame(struct walkera_dev *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int val1, val2, val3, val4, val5, val6, val7, val8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int magic, magic_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int crc1, crc2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) for (crc1 = crc2 = i = 0; i < 10; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) crc1 += w->buf[i] & 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) crc2 += (w->buf[i] & 8) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if ((w->buf[10] & 7) != (crc1 & 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (((w->buf[10] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) for (crc1 = crc2 = 0, i = 11; i < 23; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) crc1 += w->buf[i] & 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) crc2 += (w->buf[i] & 8) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if ((w->buf[23] & 7) != (crc1 & 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (((w->buf[23] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) val1 = ((w->buf[0] & 7) * 256 + w->buf[1] * 16 + w->buf[2]) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) val1 *= ((w->buf[0] >> 2) & 2) - 1; /* sign */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) val2 = (w->buf[2] & 1) << 8 | (w->buf[3] << 4) | w->buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) val2 *= (w->buf[2] & 2) - 1; /* sign */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) val3 = ((w->buf[5] & 7) * 256 + w->buf[6] * 16 + w->buf[7]) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) val3 *= ((w->buf[5] >> 2) & 2) - 1; /* sign */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) val4 = (w->buf[7] & 1) << 8 | (w->buf[8] << 4) | w->buf[9];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) val4 *= (w->buf[7] & 2) - 1; /* sign */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) val5 = ((w->buf[11] & 7) * 256 + w->buf[12] * 16 + w->buf[13]) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) val5 *= ((w->buf[11] >> 2) & 2) - 1; /* sign */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) val6 = (w->buf[13] & 1) << 8 | (w->buf[14] << 4) | w->buf[15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) val6 *= (w->buf[13] & 2) - 1; /* sign */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) val7 = ((w->buf[16] & 7) * 256 + w->buf[17] * 16 + w->buf[18]) >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) val7 *= ((w->buf[16] >> 2) & 2) - 1; /*sign */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) val8 = (w->buf[18] & 1) << 8 | (w->buf[19] << 4) | w->buf[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) val8 *= (w->buf[18] & 2) - 1; /*sign */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) magic = (w->buf[21] << 4) | w->buf[22];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) magic_bit = (w->buf[24] & 8) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) pr_debug("%4d %4d %4d %4d %4d %4d %4d %4d (magic %2x %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) val1, val2, val3, val4, val5, val6, val7, val8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) magic, magic_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) input_report_abs(w->input_dev, ABS_X, val2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) input_report_abs(w->input_dev, ABS_Y, val1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) input_report_abs(w->input_dev, ABS_Z, val6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) input_report_abs(w->input_dev, ABS_THROTTLE, val3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) input_report_abs(w->input_dev, ABS_RUDDER, val4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) input_report_abs(w->input_dev, ABS_MISC, val7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) input_report_key(w->input_dev, BTN_GEAR_DOWN, val5 > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static inline int read_ack(struct pardevice *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return parport_read_status(p->port) & 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* falling edge, prepare to BIN value calculation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static void walkera0701_irq_handler(void *handler_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) u64 pulse_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct walkera_dev *w = handler_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) w->irq_time = ktime_to_ns(ktime_get());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) pulse_time = w->irq_time - w->irq_lasttime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) w->irq_lasttime = w->irq_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* cancel timer, if in handler or active do resync */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (unlikely(0 != hrtimer_try_to_cancel(&w->timer))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) w->counter = NO_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (w->counter < NO_SYNC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (w->ack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pulse_time -= BIN1_PULSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) w->buf[w->counter] = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) pulse_time -= BIN0_PULSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) w->buf[w->counter] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (w->counter == 24) { /* full frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) walkera0701_parse_frame(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) w->counter = NO_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (abs(pulse_time - SYNC_PULSE) < RESERVE) /* new frame sync */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) w->counter = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if ((pulse_time > (ANALOG_MIN_PULSE - RESERVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) && (pulse_time < (ANALOG_MAX_PULSE + RESERVE)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) pulse_time -= (ANALOG_MIN_PULSE - RESERVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pulse_time = (u32) pulse_time / ANALOG_DELTA; /* overtiping is safe, pulsetime < s32.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) w->buf[w->counter++] |= (pulse_time & 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) w->counter = NO_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) } else if (abs(pulse_time - SYNC_PULSE - BIN0_PULSE) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) RESERVE + BIN1_PULSE - BIN0_PULSE) /* frame sync .. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) w->counter = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) hrtimer_start(&w->timer, BIN_SAMPLE, HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static enum hrtimer_restart timer_handler(struct hrtimer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct walkera_dev *w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) w = container_of(handle, struct walkera_dev, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) w->ack = read_ack(w->pardevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int walkera0701_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct walkera_dev *w = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (parport_claim(w->pardevice))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) parport_enable_irq(w->parport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static void walkera0701_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct walkera_dev *w = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) parport_disable_irq(w->parport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) hrtimer_cancel(&w->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) parport_release(w->pardevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static void walkera0701_attach(struct parport *pp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct pardev_cb walkera0701_parport_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct walkera_dev *w = &w_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (pp->number != walkera0701_pp_no) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) pr_debug("Not using parport%d.\n", pp->number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (pp->irq == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) pr_err("parport %d does not have interrupt assigned\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) pp->number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) w->parport = pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) memset(&walkera0701_parport_cb, 0, sizeof(walkera0701_parport_cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) walkera0701_parport_cb.flags = PARPORT_FLAG_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) walkera0701_parport_cb.irq_func = walkera0701_irq_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) walkera0701_parport_cb.private = w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) w->pardevice = parport_register_dev_model(pp, "walkera0701",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) &walkera0701_parport_cb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!w->pardevice) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) pr_err("failed to register parport device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (parport_negotiate(w->pardevice->port, IEEE1284_MODE_COMPAT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) pr_err("failed to negotiate parport mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) goto err_unregister_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) hrtimer_init(&w->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) w->timer.function = timer_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) w->input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!w->input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) pr_err("failed to allocate input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto err_unregister_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) input_set_drvdata(w->input_dev, w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) w->input_dev->name = "Walkera WK-0701 TX";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) w->input_dev->phys = w->parport->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) w->input_dev->id.bustype = BUS_PARPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* TODO what id vendor/product/version ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) w->input_dev->id.vendor = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) w->input_dev->id.product = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) w->input_dev->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) w->input_dev->dev.parent = w->parport->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) w->input_dev->open = walkera0701_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) w->input_dev->close = walkera0701_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) w->input_dev->evbit[0] = BIT(EV_ABS) | BIT_MASK(EV_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) w->input_dev->keybit[BIT_WORD(BTN_GEAR_DOWN)] = BIT_MASK(BTN_GEAR_DOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) input_set_abs_params(w->input_dev, ABS_X, -512, 512, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) input_set_abs_params(w->input_dev, ABS_Y, -512, 512, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) input_set_abs_params(w->input_dev, ABS_Z, -512, 512, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) input_set_abs_params(w->input_dev, ABS_THROTTLE, -512, 512, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) input_set_abs_params(w->input_dev, ABS_RUDDER, -512, 512, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) input_set_abs_params(w->input_dev, ABS_MISC, -512, 512, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (input_register_device(w->input_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) pr_err("failed to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto err_free_input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) err_free_input_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) input_free_device(w->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) err_unregister_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) parport_unregister_device(w->pardevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static void walkera0701_detach(struct parport *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct walkera_dev *w = &w_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (!w->pardevice || w->parport->number != port->number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) input_unregister_device(w->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) parport_unregister_device(w->pardevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) w->parport = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static struct parport_driver walkera0701_parport_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .name = "walkera0701",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .match_port = walkera0701_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) .detach = walkera0701_detach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .devmodel = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int __init walkera0701_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return parport_register_driver(&walkera0701_parport_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static void __exit walkera0701_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) parport_unregister_driver(&walkera0701_parport_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) module_init(walkera0701_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) module_exit(walkera0701_exit);