From patchwork Sun Dec 31 20:48:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcus Folkesson X-Patchwork-Id: 10138317 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 672036037D for ; Sun, 31 Dec 2017 20:48:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4DEED288A2 for ; Sun, 31 Dec 2017 20:48:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 41B91288A1; Sun, 31 Dec 2017 20:48:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 01990288A1 for ; Sun, 31 Dec 2017 20:48:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751012AbdLaUsw (ORCPT ); Sun, 31 Dec 2017 15:48:52 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:41077 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750982AbdLaUsv (ORCPT ); Sun, 31 Dec 2017 15:48:51 -0500 Received: by mail-lf0-f66.google.com with SMTP id h137so8302658lfe.8; Sun, 31 Dec 2017 12:48:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=6rDfhOCOsTt75eb8JxeOgQssmW6JmM7jGmuLtcKMvBU=; b=tZ9ZY9SqcJYCq40S4SdsZFFMoZ0JZQACpSqYlIwP6IHOwA1vvOABfnIHcOLmrIHNNA hTvLY/hB/MJZQ/2GBC0Vay1kKM722qEFF+yJbeGsVtk+Ok1X8WR1s+8XvQ+1GE55bQ2o 6vqgOyI4cEwbLi6BmHiTDOOE1/e+Q0j4oS9WGM6a0PnxdO0UnianDXyicH8vUBrUxOj7 nQNSiDaNwlgrl6jykXWTK8QlRcCmGOr+IoQJfQSsKN3+VFGUHQPOb7o5R64iJudV4Cyw 3FXBB2HJJHUFc/BqkjaV9DXvJUxyMwbshJ7wVv1X6gjydV7rwqhYKyJi3eAyWJCOxoYc kubw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=6rDfhOCOsTt75eb8JxeOgQssmW6JmM7jGmuLtcKMvBU=; b=LPrmznaTDYscnooNyLwoZvXqkBjPaJG5kkXqTksjtrxRtGzPRKTrit6cshhJ6iSFaz s7JmZTfcXF/cf2hQTJuHyzI7WVGFFp1B0pertQ6vwo/X12oY4DdbEkqA0exQa5qMK7Bg XsGrnBzf2HfDmE0TP22Y7+tLdRc6OsRudcp/qSy36SH+mkK/hvkdfC/2CkgAxClUUOtd sS/MMqJSdDUGJMIkljiINRqxsrI5zfzjWZhW35YjDYXqW/HgjC+9KcpsP2xkBKuruNbB /CX3ZVHMBAzfiXdBhc38Zn5dQGSCwsPsy0FPNUbB9/M9A+qyi6X6kMoeYNlcq6ScSDGD f94A== X-Gm-Message-State: AKGB3mI3Z+cqmpbzKeTbdXITh2sjZmq990L7PJ5qA2NdSPO4+YDaWdOl VzEYbMJlNUDyIwnRSOKjqppNQxnr X-Google-Smtp-Source: ACJfBosbwFKsoYkvCBbKg5UVSoMrWjckE2y6hrlKS+hLy3rdK5Is6CSuLC1V+ZZyeT4/SY7y78Eseg== X-Received: by 10.25.181.221 with SMTP id g90mr19175262lfk.98.1514753329443; Sun, 31 Dec 2017 12:48:49 -0800 (PST) Received: from localhost.localdomain (c-2ec253d1-74736162.cust.telenor.se. [46.194.83.209]) by smtp.gmail.com with ESMTPSA id w62sm8267493lje.59.2017.12.31.12.48.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Dec 2017 12:48:48 -0800 (PST) From: Marcus Folkesson To: Dmitry Torokhov , Jonathan Corbet , Marcus Folkesson , Tomohiro Yoshidomi , David Herrmann , Philippe Ombredanne , Kate Stewart , Greg Kroah-Hartman Cc: linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] input: pxrc: new driver for PhoenixRC Flight Controller Adapter Date: Sun, 31 Dec 2017 21:48:46 +0100 Message-Id: <20171231204846.28151-1-marcus.folkesson@gmail.com> X-Mailer: git-send-email 2.15.1 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This driver let you plug in your RC controller to the adapter and use it as input device in various RC simulators. Signed-off-by: Marcus Folkesson --- v2: - Change module license to GPLv2 to match SPDX tag Documentation/input/devices/pxrc.rst | 57 ++++++++ drivers/input/joystick/Kconfig | 9 ++ drivers/input/joystick/Makefile | 1 + drivers/input/joystick/pxrc.c | 254 +++++++++++++++++++++++++++++++++++ 4 files changed, 321 insertions(+) create mode 100644 Documentation/input/devices/pxrc.rst create mode 100644 drivers/input/joystick/pxrc.c diff --git a/Documentation/input/devices/pxrc.rst b/Documentation/input/devices/pxrc.rst new file mode 100644 index 000000000000..0ec466c58958 --- /dev/null +++ b/Documentation/input/devices/pxrc.rst @@ -0,0 +1,57 @@ +======================================================= +pxrc - PhoenixRC Flight Controller Adapter +======================================================= + +:Author: Marcus Folkesson + +This driver let you use your own RC controller plugged into the +adapter that comes with PhoenixRC [1]_ or other compatible adapters. + +The adapter supports 7 analog channels and 1 digital input switch. + +Notes +===== + +Many RC controllers is able to configure which stick goes to which channel. +This is also configurable in most simulators, so a matching is not necessary. + +The driver is generating the following input event for analog channels: + ++---------+----------------+ +| Channel | Event | ++=========+================+ +| 1 | ABS_X | ++---------+----------------+ +| 2 | ABS_Y | ++---------+----------------+ +| 3 | ABS_RX | ++---------+----------------+ +| 4 | ABS_RY | ++---------+----------------+ +| 5 | ABS_TILT_X | ++---------+----------------+ +| 6 | ABS_TILT_Y | ++---------+----------------+ +| 7 | ABS_THROTTLE | ++---------+----------------+ + +The digital input switch is generated as an `BTN_A` event. + +Manual Testing +============== + +To test this driver's functionality you may use `input-event` which is part of +the `input layer utilities` suite [2]_. + +For example:: + + > modprobe pxrc + > input-events + +To print all input events from input `devnr`. + +References +========== + +.. [1] http://www.phoenix-sim.com/ +.. [2] https://www.kraxel.org/cgit/input/ diff --git a/drivers/input/joystick/Kconfig b/drivers/input/joystick/Kconfig index f3c2f6ea8b44..18ab6dafff41 100644 --- a/drivers/input/joystick/Kconfig +++ b/drivers/input/joystick/Kconfig @@ -351,4 +351,13 @@ config JOYSTICK_PSXPAD_SPI_FF To drive rumble motor a dedicated power supply is required. +config JOYSTICK_PXRC + tristate "PhoenixRC Flight Controller Adapter" + depends on USB_ARCH_HAS_HCD + select USB + help + Say Y here if you want to use the PhoenixRC Flight Controller Adapter. + + To compile this driver as a module, choose M here: the + module will be called pxrc. endif diff --git a/drivers/input/joystick/Makefile b/drivers/input/joystick/Makefile index 67651efda2e1..dd0492ebbed7 100644 --- a/drivers/input/joystick/Makefile +++ b/drivers/input/joystick/Makefile @@ -23,6 +23,7 @@ obj-$(CONFIG_JOYSTICK_JOYDUMP) += joydump.o obj-$(CONFIG_JOYSTICK_MAGELLAN) += magellan.o obj-$(CONFIG_JOYSTICK_MAPLE) += maplecontrol.o obj-$(CONFIG_JOYSTICK_PSXPAD_SPI) += psxpad-spi.o +obj-$(CONFIG_JOYSTICK_PXRC) += pxrc.o obj-$(CONFIG_JOYSTICK_SIDEWINDER) += sidewinder.o obj-$(CONFIG_JOYSTICK_SPACEBALL) += spaceball.o obj-$(CONFIG_JOYSTICK_SPACEORB) += spaceorb.o diff --git a/drivers/input/joystick/pxrc.c b/drivers/input/joystick/pxrc.c new file mode 100644 index 000000000000..2bec99df97e2 --- /dev/null +++ b/drivers/input/joystick/pxrc.c @@ -0,0 +1,254 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Driver for Phoenix RC Flight Controller Adapter + * + * Copyright (C) 2018 Marcus Folkesson + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PXRC_VENDOR_ID (0x1781) +#define PXRC_PRODUCT_ID (0x0898) + +static const struct usb_device_id pxrc_table[] = { + { USB_DEVICE(PXRC_VENDOR_ID, PXRC_PRODUCT_ID) }, + { } +}; +MODULE_DEVICE_TABLE(usb, pxrc_table); + +struct usb_pxrc { + struct input_dev *input_dev; + struct usb_device *udev; + struct usb_interface *interface; + struct usb_anchor anchor; + __u8 epaddr; + char phys[64]; + unsigned char *data; + size_t bsize; + struct kref kref; +}; + +#define to_pxrc_dev(d) container_of(d, struct usb_pxrc, kref) +static void pxrc_delete(struct kref *kref) +{ + struct usb_pxrc *pxrc = to_pxrc_dev(kref); + + usb_put_dev(pxrc->udev); +} + +static void pxrc_usb_irq(struct urb *urb) +{ + struct usb_pxrc *pxrc = urb->context; + + switch (urb->status) { + case 0: + /* success */ + break; + case -ETIME: + /* this urb is timing out */ + dev_dbg(&pxrc->interface->dev, + "%s - urb timed out - was the device unplugged?\n", + __func__); + return; + case -ECONNRESET: + case -ENOENT: + case -ESHUTDOWN: + case -EPIPE: + /* this urb is terminated, clean up */ + dev_dbg(&pxrc->interface->dev, "%s - urb shutting down with status: %d\n", + __func__, urb->status); + return; + default: + dev_dbg(&pxrc->interface->dev, "%s - nonzero urb status received: %d\n", + __func__, urb->status); + goto exit; + } + + if (urb->actual_length == 8) { + input_report_abs(pxrc->input_dev, ABS_X, pxrc->data[0]); + input_report_abs(pxrc->input_dev, ABS_Y, pxrc->data[2]); + input_report_abs(pxrc->input_dev, ABS_RX, pxrc->data[3]); + input_report_abs(pxrc->input_dev, ABS_RY, pxrc->data[4]); + input_report_abs(pxrc->input_dev, ABS_TILT_X, pxrc->data[5]); + input_report_abs(pxrc->input_dev, ABS_TILT_Y, pxrc->data[6]); + input_report_abs(pxrc->input_dev, ABS_THROTTLE, pxrc->data[7]); + + input_report_key(pxrc->input_dev, BTN_A, pxrc->data[1]); + } + +exit: + /* Resubmit to fetch new fresh URBs */ + usb_anchor_urb(urb, &pxrc->anchor); + if (usb_submit_urb(urb, GFP_ATOMIC) < 0) + usb_unanchor_urb(urb); +} + +static int pxrc_submit_intr_urb(struct usb_pxrc *pxrc) +{ + struct urb *urb; + unsigned int pipe; + int err; + + urb = usb_alloc_urb(0, GFP_KERNEL); + if (!urb) + return -ENOMEM; + + pipe = usb_rcvintpipe(pxrc->udev, pxrc->epaddr), + usb_fill_int_urb(urb, pxrc->udev, pipe, pxrc->data, pxrc->bsize, + pxrc_usb_irq, pxrc, 1); + usb_anchor_urb(urb, &pxrc->anchor); + err = usb_submit_urb(urb, GFP_KERNEL); + if (err < 0) + usb_unanchor_urb(urb); + + usb_free_urb(urb); + return err; +} + +static int pxrc_open(struct input_dev *input) +{ + struct usb_pxrc *pxrc = input_get_drvdata(input); + int err; + + err = pxrc_submit_intr_urb(pxrc); + if (err < 0) + goto error; + + kref_get(&pxrc->kref); + return 0; + +error: + usb_kill_anchored_urbs(&pxrc->anchor); + return err; +} + +static void pxrc_close(struct input_dev *input) +{ + struct usb_pxrc *pxrc = input_get_drvdata(input); + + usb_kill_anchored_urbs(&pxrc->anchor); + kref_put(&pxrc->kref, pxrc_delete); +} + +static int pxrc_input_init(struct usb_pxrc *pxrc) +{ + pxrc->input_dev = devm_input_allocate_device(&pxrc->interface->dev); + if (pxrc->input_dev == NULL) { + dev_err(&pxrc->interface->dev, "couldn't allocate input device\n"); + return -ENOMEM; + } + + pxrc->input_dev->name = "PXRC Flight Controller Adapter"; + pxrc->input_dev->phys = pxrc->phys; + pxrc->input_dev->id.bustype = BUS_USB; + pxrc->input_dev->id.vendor = PXRC_VENDOR_ID; + pxrc->input_dev->id.product = PXRC_PRODUCT_ID; + pxrc->input_dev->id.version = 0x01; + + pxrc->input_dev->open = pxrc_open; + pxrc->input_dev->close = pxrc_close; + + pxrc->input_dev->evbit[0] = BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY); + pxrc->input_dev->absbit[0] = BIT_MASK(ABS_X) | + BIT_MASK(ABS_Y) | + BIT_MASK(ABS_RY) | + BIT_MASK(ABS_RX) | + BIT_MASK(ABS_THROTTLE) | + BIT_MASK(ABS_TILT_X) | + BIT_MASK(ABS_TILT_Y); + + pxrc->input_dev->keybit[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_A); + + input_set_abs_params(pxrc->input_dev, ABS_X, 0, 255, 0, 0); + input_set_abs_params(pxrc->input_dev, ABS_Y, 0, 255, 0, 0); + input_set_abs_params(pxrc->input_dev, ABS_RX, 0, 255, 0, 0); + input_set_abs_params(pxrc->input_dev, ABS_RY, 0, 255, 0, 0); + input_set_abs_params(pxrc->input_dev, ABS_TILT_X, 0, 255, 0, 0); + input_set_abs_params(pxrc->input_dev, ABS_TILT_Y, 0, 255, 0, 0); + input_set_abs_params(pxrc->input_dev, ABS_THROTTLE, 0, 255, 0, 0); + + input_set_drvdata(pxrc->input_dev, pxrc); + + return input_register_device(pxrc->input_dev); +} + +static int pxrc_probe(struct usb_interface *interface, + const struct usb_device_id *id) +{ + struct usb_pxrc *pxrc; + struct usb_endpoint_descriptor *epirq; + int retval; + + pxrc = devm_kzalloc(&interface->dev, sizeof(*pxrc), GFP_KERNEL); + + if (!pxrc) + return -ENOMEM; + + kref_init(&pxrc->kref); + init_usb_anchor(&pxrc->anchor); + + pxrc->udev = usb_get_dev(interface_to_usbdev(interface)); + pxrc->interface = interface; + + /* Set up the endpoint information */ + /* This device only has an interrupt endpoint */ + retval = usb_find_common_endpoints(interface->cur_altsetting, + NULL, NULL, &epirq, NULL); + if (retval) { + dev_err(&interface->dev, + "Could not find endpoint\n"); + goto error; + } + + pxrc->bsize = usb_endpoint_maxp(epirq); + pxrc->epaddr = epirq->bEndpointAddress; + pxrc->data = devm_kmalloc(&interface->dev, pxrc->bsize, GFP_KERNEL); + if (!pxrc->data) { + retval = -ENOMEM; + goto error; + } + + usb_set_intfdata(interface, pxrc); + usb_make_path(pxrc->udev, pxrc->phys, sizeof(pxrc->phys)); + + retval = pxrc_input_init(pxrc); + if (retval) + goto error; + + return 0; + +error: + /* this frees allocated memory */ + kref_put(&pxrc->kref, pxrc_delete); + + return retval; +} + +static void pxrc_disconnect(struct usb_interface *interface) +{ + struct usb_pxrc *pxrc = usb_get_intfdata(interface); + + kref_put(&pxrc->kref, pxrc_delete); +} + +static struct usb_driver pxrc_driver = { + .name = "pxrc", + .probe = pxrc_probe, + .disconnect = pxrc_disconnect, + .id_table = pxrc_table, +}; + +module_usb_driver(pxrc_driver); + +MODULE_AUTHOR("Marcus Folkesson "); +MODULE_DESCRIPTION("PhoenixRC Flight Controller Adapter"); +MODULE_LICENSE("GPL v2");