Re: [PATCH v2 1/3] usb: gadget: ccid: add support for USB CCID Gadget Device

2018-05-28 Thread Marcus Folkesson
Hi Andrzej,

Thank you for reviewing.

On Mon, May 28, 2018 at 11:12:27AM +0200, Andrzej Pietrasiewicz wrote:
> W dniu 28.05.2018 o 10:38, Marcus Folkesson pisze:
> > Hi Andrzej,
> > 
> > On Mon, May 28, 2018 at 09:04:51AM +0200, Andrzej Pietrasiewicz wrote:
> >> Mi Marcus,
> >>
> >> W dniu 26.05.2018 o 23:19, Marcus Folkesson pisze:
> >>> Chip Card Interface Device (CCID) protocol is a USB protocol that
> >>> allows a smartcard device to be connected to a computer via a card
> >>> reader using a standard USB interface, without the need for each 
> >>> manufacturer
> >>> of smartcards to provide its own reader or protocol.
> >>>
> >>> This gadget driver makes Linux show up as a CCID device to the host and 
> >>> let a
> >>> userspace daemon act as the smartcard.
> >>>
> >>> This is useful when the Linux gadget itself should act as a cryptographic
> >>> device or forward APDUs to an embedded smartcard device.
> >>>
> >>> Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
> >>> ---
> >>
> >>>
> >>> +config USB_CONFIGFS_CCID
> >>> + bool "Chip Card Interface Device (CCID)"
> >>> + depends on USB_CONFIGFS
> >>> + select USB_F_CCID
> >>> + help
> >>> +   The CCID function driver provides generic emulation of a
> >>> +   Chip Card Interface Device (CCID).
> >>> +
> >>> +   You will need a user space server talking to /dev/ccidg*,
> >>> +   since the kernel itself does not implement CCID/TPDU/APDU
> >>> +   protocol.
> >>
> >> Your function needs a userspace daemon to work.
> >> It seems you want to use FunctionFS for such a purpose
> >> instead of creating a new function.
> >>
> >> Andrzej
> > 
> >>> +   since the kernel itself does not implement CCID/TPDU/APDU
> > Oops, the driver does handle CCID.
> 
> Which parts of code do this handling?

My bad, I was thinking about the USB descriptors and endpoints setup.
That is of cause not part of the CCID protocol.

> 
> Is there any kind of state machine usual for protocols?
> If the protocol is stateless then isn't it just a data format then?

The protocol is stateless.

> 
> Which part of this handling must be done in kernel and why?
> 
> Does the said handling do anything other than forwarding the
> traffic between USB and a character device?

No, it forward the CCID messages to the character device to be handled
by the application.

> 
> What is the character device used for? I know: read, write and poll.
> But why? To do what?

It is used for the application to fetch, interpret and then perform actions 
depending on
commands.

> 
> > 
> > Well, yes, It needs an application that perform the "smartcard operations", 
> > such as
> > generate keys or sign data, as this depends on how it should be used.
> > 
> > The actual smartcard operations could for example be in software,
> > use a crypto engine in SoC or external HSM (Hardware Security Module).
> > 
> > Without the application, the gadget shows up as a smart card reader
> > with an unconnected smartcard.
> > 
> 
> Does showing up as anything require anything other than merely
> providing USB descriptors?

I guess.

> 
> Andrzej

Thank you,
Marcus
--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2 1/3] usb: gadget: ccid: add support for USB CCID Gadget Device

2018-05-28 Thread Marcus Folkesson
Hi Andrzej,

On Mon, May 28, 2018 at 09:04:51AM +0200, Andrzej Pietrasiewicz wrote:
> Mi Marcus,
> 
> W dniu 26.05.2018 o 23:19, Marcus Folkesson pisze:
> > Chip Card Interface Device (CCID) protocol is a USB protocol that
> > allows a smartcard device to be connected to a computer via a card
> > reader using a standard USB interface, without the need for each 
> > manufacturer
> > of smartcards to provide its own reader or protocol.
> > 
> > This gadget driver makes Linux show up as a CCID device to the host and let 
> > a
> > userspace daemon act as the smartcard.
> > 
> > This is useful when the Linux gadget itself should act as a cryptographic
> > device or forward APDUs to an embedded smartcard device.
> > 
> > Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
> > ---
> 
> >   
> > +config USB_CONFIGFS_CCID
> > +   bool "Chip Card Interface Device (CCID)"
> > +   depends on USB_CONFIGFS
> > +   select USB_F_CCID
> > +   help
> > + The CCID function driver provides generic emulation of a
> > + Chip Card Interface Device (CCID).
> > +
> > + You will need a user space server talking to /dev/ccidg*,
> > + since the kernel itself does not implement CCID/TPDU/APDU
> > + protocol.
> 
> Your function needs a userspace daemon to work.
> It seems you want to use FunctionFS for such a purpose
> instead of creating a new function.
> 
> Andrzej

> > + since the kernel itself does not implement CCID/TPDU/APDU
Oops, the driver does handle CCID.

Well, yes, It needs an application that perform the "smartcard operations", 
such as
generate keys or sign data, as this depends on how it should be used.

The actual smartcard operations could for example be in software,
use a crypto engine in SoC or external HSM (Hardware Security Module).

Without the application, the gadget shows up as a smart card reader
with an unconnected smartcard.

I guess it could be accomplished with FunctionFS as well.

Best regards
Marcus Folkesson

--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2 2/3] Documentation: usb: add documentation for USB CCID Gadget Device

2018-05-28 Thread Marcus Folkesson
Hi Randy,

On Sun, May 27, 2018 at 04:36:24PM -0700, Randy Dunlap wrote:
> Hi,
> 
> I have a few documentation comments below...
> 
> On 05/26/2018 02:19 PM, Marcus Folkesson wrote:
> > Add documentation to give a brief description on how to use the
> > CCID Gadget Device.
> > This includes a description for all attributes followed by an example on
> > how to setup the device with ConfigFS.
> > 
> > Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
> > ---
> >  Documentation/usb/gadget_ccid.rst | 267 
> > ++
> >  1 file changed, 267 insertions(+)
> >  create mode 100644 Documentation/usb/gadget_ccid.rst
> > 
> > diff --git a/Documentation/usb/gadget_ccid.rst 
> > b/Documentation/usb/gadget_ccid.rst
> > new file mode 100644
> > index ..5ac806b14604
> > --- /dev/null
> > +++ b/Documentation/usb/gadget_ccid.rst
> > @@ -0,0 +1,267 @@
> > +.. SPDX-License-Identifier: GPL-2.0
> > +
> > +
> > +CCID Gadget
> > +
> > +
> > +:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
> > +
> > +Introduction
> > +
> > +
> > +The CCID Gadget will present itself as a CCID device to the host system.
> > +The device supports two endpoints for now; BULK IN and BULK OUT.
> > +These endpoints is exposed to userspace via /dev/ccidg*.
> 
>are exposed
> 
> > +
> > +All CCID commands are sent on the BULK-OUT endpoint. Each command sent to 
> > the CCID
> > +has an associated ending response. Some commands can also have intermediate
> > +responses. The response is sent on the BULK-IN endpoint.
> > +See Figure 3-3 in the CCID Specification [1]_ for more details.
> > +
> > +The CCID commands must be handled in userspace since the driver is only 
> > working
> > +as a transport layer for the TPDUs.
> > +
> > +
> > +CCID Commands
> > +--
> > +
> > +All CCID commands begins with a 10 bytes header followed by an optional
> 
> with a 10-byte header
> (or maybe that's a locale difference)
> 
> > +data field depending on message type.
> > +
> > +++--+---+--+
> > +| Offset | Field| Size  | Description  |
> > +++==+===+==+
> > +| 0  | bMessageType | 1 | Type of message  |
> > +++--+---+--+
> > +| 1  | dwLength | 4 | Message specific data length |
> > +||  |   |  |
> > +++--+---+--+
> > +| 5  | bSlot| 1 | Identifies the slot number   |
> > +||  |   | for this command |
> > +++--+---+--+
> > +| 6  | bSeq | 1 | Sequence number for command  |
> > +++--+---+--+
> > +| 7  | ...  | 3 | Fields depends on message type   |
> > +++--+---+--+
> > +| 10 | abData   | array | Message specific data (OPTIONAL) |
> > +++--+---+--+
> > +
> > +
> > +Multiple CCID gadgets
> > +--
> > +
> > +It is possible to create multiple instances of the CCID gadget, however,
> > +a much more flexible way is to create one gadget and set the `nslots` 
> > attribute
> > +to the number of desired CCID devices.
> > +
> > +All CCID commands specifies which slot that is the receiver in the `bSlot` 
> > field
> 
>  specify which slot is the receiver
> 
> > +of the CCID header.
> > +
> > +Usage
> > +=
> > +
> > +Access from userspace
> > +--
> > +All communication is by read(2) and write(2) to the corresponding 
> > /dev/ccidg* device.
> > +Only one filedescriptor is allowed to be open to the device at a time.
> 
> file descriptor
> 
> > +
> > +The buffer size provided to read(2) **must be at least** 522 (10 bytes 
> > header + 512 bytes payload)
> > +bytes as we are working with whole commands.
> > +
> > +The buffer size provided to write(2) **may not exceed** 52

Re: [PATCH 1/3] usb: gadget: ccid: add support for USB CCID Gadget Device

2018-05-26 Thread Marcus Folkesson
On Sat, May 26, 2018 at 10:56:52PM +0200, Greg Kroah-Hartman wrote:
> On Sat, May 26, 2018 at 10:33:59PM +0200, Marcus Folkesson wrote:
> > Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
> 
> I can't take patches without any changelog text.  And why would you
> submit a patch over 1000 lines without any?

I'm sorry, obviously not much went well for me yesterday.
The changlelog was "fixup:ed" away and now I noticed that my
patch version history did not follow for v2.

Is it better to send a v3 with updated version history or wait for more
comments?

I'll start review my patches better before sending them.
Thank you for your time and comments.

> 
> Please fix.
> 
> thanks,
> 
> greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 2/3] Documentation: usb: add documentation for USB CCID Gadget Device

2018-05-26 Thread Marcus Folkesson
Add documentation to give a brief description on how to use the
CCID Gadget Device.
This includes a description for all attributes followed by an example on
how to setup the device with ConfigFS.

Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
---
 Documentation/usb/gadget_ccid.rst | 267 ++
 1 file changed, 267 insertions(+)
 create mode 100644 Documentation/usb/gadget_ccid.rst

diff --git a/Documentation/usb/gadget_ccid.rst 
b/Documentation/usb/gadget_ccid.rst
new file mode 100644
index ..5ac806b14604
--- /dev/null
+++ b/Documentation/usb/gadget_ccid.rst
@@ -0,0 +1,267 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+
+CCID Gadget
+
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+Introduction
+
+
+The CCID Gadget will present itself as a CCID device to the host system.
+The device supports two endpoints for now; BULK IN and BULK OUT.
+These endpoints is exposed to userspace via /dev/ccidg*.
+
+All CCID commands are sent on the BULK-OUT endpoint. Each command sent to the 
CCID
+has an associated ending response. Some commands can also have intermediate
+responses. The response is sent on the BULK-IN endpoint.
+See Figure 3-3 in the CCID Specification [1]_ for more details.
+
+The CCID commands must be handled in userspace since the driver is only working
+as a transport layer for the TPDUs.
+
+
+CCID Commands
+--
+
+All CCID commands begins with a 10 bytes header followed by an optional
+data field depending on message type.
+
+++--+---+--+
+| Offset | Field| Size  | Description  |
+++==+===+==+
+| 0  | bMessageType | 1 | Type of message  |
+++--+---+--+
+| 1  | dwLength | 4 | Message specific data length |
+||  |   |  |
+++--+---+--+
+| 5  | bSlot| 1 | Identifies the slot number   |
+||  |   | for this command |
+++--+---+--+
+| 6  | bSeq | 1 | Sequence number for command  |
+++--+---+--+
+| 7  | ...  | 3 | Fields depends on message type   |
+++--+---+--+
+| 10 | abData   | array | Message specific data (OPTIONAL) |
+++--+---+--+
+
+
+Multiple CCID gadgets
+--
+
+It is possible to create multiple instances of the CCID gadget, however,
+a much more flexible way is to create one gadget and set the `nslots` attribute
+to the number of desired CCID devices.
+
+All CCID commands specifies which slot that is the receiver in the `bSlot` 
field
+of the CCID header.
+
+Usage
+=
+
+Access from userspace
+--
+All communication is by read(2) and write(2) to the corresponding /dev/ccidg* 
device.
+Only one filedescriptor is allowed to be open to the device at a time.
+
+The buffer size provided to read(2) **must be at least** 522 (10 bytes header 
+ 512 bytes payload)
+bytes as we are working with whole commands.
+
+The buffer size provided to write(2) **may not exceed** 522 (10 bytes header + 
512 bytes payload)
+bytes as we are working with whole commands.
+
+
+Configuration with configfs
+
+
+ConfigFS is used to create and configure the CCID gadget.
+In order to get a device to work as intended, a few attributes must
+be considered.
+
+The attributes is described below followed by an example.
+
+features
+~
+
+The `feature` attribute writes to the dwFeatures field in the class descriptor.
+See Table 5.1-1 Smart Card Device Descriptors in the CCID Specification [1]_.
+
+The value indicates what intelligent features the CCID has.
+These values are available to user application as defines in ccid.h [2]_.
+The default value is 0x.
+
+The value is a bitwise OR operation performed on the following values:
+
++++
+| Value  | Description|
++++
+| 0x | No special characteristics |
++++
+| 0x0002 | Automatic parameter configuration based on ATR data|
++++
+| 0x0004 | Automatic activation of ICC 

[PATCH v2 1/3] usb: gadget: ccid: add support for USB CCID Gadget Device

2018-05-26 Thread Marcus Folkesson
Chip Card Interface Device (CCID) protocol is a USB protocol that
allows a smartcard device to be connected to a computer via a card
reader using a standard USB interface, without the need for each manufacturer
of smartcards to provide its own reader or protocol.

This gadget driver makes Linux show up as a CCID device to the host and let a
userspace daemon act as the smartcard.

This is useful when the Linux gadget itself should act as a cryptographic
device or forward APDUs to an embedded smartcard device.

Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
---
 drivers/usb/gadget/Kconfig   |  17 +
 drivers/usb/gadget/function/Makefile |   1 +
 drivers/usb/gadget/function/f_ccid.c | 988 +++
 drivers/usb/gadget/function/f_ccid.h |  91 
 include/uapi/linux/usb/ccid.h|  93 
 5 files changed, 1190 insertions(+)
 create mode 100644 drivers/usb/gadget/function/f_ccid.c
 create mode 100644 drivers/usb/gadget/function/f_ccid.h
 create mode 100644 include/uapi/linux/usb/ccid.h

diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 31cce7805eb2..bdebdf1ffa2b 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -149,6 +149,9 @@ config USB_LIBCOMPOSITE
 config USB_F_ACM
tristate
 
+config USB_F_CCID
+   tristate
+
 config USB_F_SS_LB
tristate
 
@@ -248,6 +251,20 @@ config USB_CONFIGFS_ACM
  ACM serial link.  This function can be used to interoperate with
  MS-Windows hosts or with the Linux-USB "cdc-acm" driver.
 
+config USB_CONFIGFS_CCID
+   bool "Chip Card Interface Device (CCID)"
+   depends on USB_CONFIGFS
+   select USB_F_CCID
+   help
+ The CCID function driver provides generic emulation of a
+ Chip Card Interface Device (CCID).
+
+ You will need a user space server talking to /dev/ccidg*,
+ since the kernel itself does not implement CCID/TPDU/APDU
+ protocol.
+
+ For more information, see Documentation/usb/gadget_ccid.rst.
+
 config USB_CONFIGFS_OBEX
bool "Object Exchange Model (CDC OBEX)"
depends on USB_CONFIGFS
diff --git a/drivers/usb/gadget/function/Makefile 
b/drivers/usb/gadget/function/Makefile
index 5d3a6cf02218..629851009e1a 100644
--- a/drivers/usb/gadget/function/Makefile
+++ b/drivers/usb/gadget/function/Makefile
@@ -9,6 +9,7 @@ ccflags-y   += 
-I$(srctree)/drivers/usb/gadget/udc/
 # USB Functions
 usb_f_acm-y:= f_acm.o
 obj-$(CONFIG_USB_F_ACM)+= usb_f_acm.o
+obj-$(CONFIG_USB_F_CCID)   += f_ccid.o
 usb_f_ss_lb-y  := f_loopback.o f_sourcesink.o
 obj-$(CONFIG_USB_F_SS_LB)  += usb_f_ss_lb.o
 obj-$(CONFIG_USB_U_SERIAL) += u_serial.o
diff --git a/drivers/usb/gadget/function/f_ccid.c 
b/drivers/usb/gadget/function/f_ccid.c
new file mode 100644
index ..9ff8615ca303
--- /dev/null
+++ b/drivers/usb/gadget/function/f_ccid.c
@@ -0,0 +1,988 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * f_ccid.c -- Chip Card Interface Device (CCID) function Driver
+ *
+ * Copyright (C) 2018 Marcus Folkesson <marcus.folkes...@gmail.com>
+ *
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "f_ccid.h"
+#include "u_f.h"
+
+/* Number of tx requests to allocate */
+#define N_TX_REQS 4
+
+/* Maximum number of devices */
+#define CCID_MINORS 4
+
+struct ccidg_bulk_dev {
+   atomic_t is_open;
+   atomic_t rx_req_busy;
+   wait_queue_head_t read_wq;
+   wait_queue_head_t write_wq;
+   struct usb_request *rx_req;
+   atomic_t rx_done;
+   struct list_head tx_idle;
+};
+
+struct f_ccidg {
+   struct usb_function_instancefunc_inst;
+   struct usb_function function;
+   spinlock_t lock;
+   atomic_t online;
+
+   /* Character device */
+   struct cdev cdev;
+   int minor;
+
+   /* Dynamic attributes */
+   u32 features;
+   u32 protocols;
+   u8 pinsupport;
+   u8 nslots;
+   u8 lcdlayout;
+
+   /* Endpoints */
+   struct usb_ep *in;
+   struct usb_ep *out;
+   struct ccidg_bulk_dev bulk_dev;
+};
+
+/* Interface Descriptor: */
+static struct usb_interface_descriptor ccid_interface_desc = {
+   .bLength =  USB_DT_INTERFACE_SIZE,
+   .bDescriptorType =  USB_DT_INTERFACE,
+   .bNumEndpoints =2,
+   .bInterfaceClass =  USB_CLASS_CSCID,
+   .bInterfaceSubClass =   0,
+   .bInterfaceProtocol =   0,
+};
+
+/* CCID Class Descriptor */
+static struct ccid_class_descriptor ccid_class_desc = {
+   .bLength =  sizeof(ccid_class_desc),
+   .bDescriptorType =  CCID_DECRIPTOR_TYPE,
+   .bcdCCID =  CCID1_10,
+   /* .bMaxSlotIndex = DYNAMIC */
+   .bVoltageSupport =  CCID_VOLTS_3_0,
+   

[PATCH v2 3/3] MAINTAINERS: add USB CCID Gadget Device

2018-05-26 Thread Marcus Folkesson
Add MAINTAINERS entry for USB CCID Gadget Device

Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
---
 MAINTAINERS | 8 
 1 file changed, 8 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 078fd80f664f..e77c3d2bec89 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14541,6 +14541,14 @@ L: linux-s...@vger.kernel.org
 S: Maintained
 F: drivers/usb/storage/uas.c
 
+USB CCID GADGET
+M: Marcus Folkesson <marcus.folkes...@gmail.com>
+L: linux-...@vger.kernel.org
+S: Maintained
+F: drivers/usb/gadget/function/f_ccid.*
+F: include/uapi/linux/usb/ccid.h
+F: Documentation/usb/gadget_ccid.rst
+
 USB CDC ETHERNET DRIVER
 M: Oliver Neukum <oli...@neukum.org>
 L: linux-...@vger.kernel.org
-- 
2.16.2

--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 3/3] MAINTAINERS: add USB CCID Gadget Device

2018-05-26 Thread Marcus Folkesson
Add MAINTAINERS entry for USB CCID Gadget Device

Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
---
 MAINTAINERS | 8 
 1 file changed, 8 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 078fd80f664f..e77c3d2bec89 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14541,6 +14541,14 @@ L: linux-s...@vger.kernel.org
 S: Maintained
 F: drivers/usb/storage/uas.c
 
+USB CCID GADGET
+M: Marcus Folkesson <marcus.folkes...@gmail.com>
+L: linux-...@vger.kernel.org
+S: Maintained
+F: drivers/usb/gadget/function/f_ccid.*
+F: include/uapi/linux/usb/ccid.h
+F: Documentation/usb/gadget_ccid.rst
+
 USB CDC ETHERNET DRIVER
 M: Oliver Neukum <oli...@neukum.org>
 L: linux-...@vger.kernel.org
-- 
2.16.2

--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 1/3] usb: gadget: ccid: add support for USB CCID Gadget Device

2018-05-26 Thread Marcus Folkesson
Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
---
 drivers/usb/gadget/Kconfig   |  17 +
 drivers/usb/gadget/function/Makefile |   1 +
 drivers/usb/gadget/function/f_ccid.c | 988 +++
 drivers/usb/gadget/function/f_ccid.h |  91 
 include/uapi/linux/usb/ccid.h|  93 
 5 files changed, 1190 insertions(+)
 create mode 100644 drivers/usb/gadget/function/f_ccid.c
 create mode 100644 drivers/usb/gadget/function/f_ccid.h
 create mode 100644 include/uapi/linux/usb/ccid.h

diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 31cce7805eb2..bdebdf1ffa2b 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -149,6 +149,9 @@ config USB_LIBCOMPOSITE
 config USB_F_ACM
tristate
 
+config USB_F_CCID
+   tristate
+
 config USB_F_SS_LB
tristate
 
@@ -248,6 +251,20 @@ config USB_CONFIGFS_ACM
  ACM serial link.  This function can be used to interoperate with
  MS-Windows hosts or with the Linux-USB "cdc-acm" driver.
 
+config USB_CONFIGFS_CCID
+   bool "Chip Card Interface Device (CCID)"
+   depends on USB_CONFIGFS
+   select USB_F_CCID
+   help
+ The CCID function driver provides generic emulation of a
+ Chip Card Interface Device (CCID).
+
+ You will need a user space server talking to /dev/ccidg*,
+ since the kernel itself does not implement CCID/TPDU/APDU
+ protocol.
+
+ For more information, see Documentation/usb/gadget_ccid.rst.
+
 config USB_CONFIGFS_OBEX
bool "Object Exchange Model (CDC OBEX)"
depends on USB_CONFIGFS
diff --git a/drivers/usb/gadget/function/Makefile 
b/drivers/usb/gadget/function/Makefile
index 5d3a6cf02218..629851009e1a 100644
--- a/drivers/usb/gadget/function/Makefile
+++ b/drivers/usb/gadget/function/Makefile
@@ -9,6 +9,7 @@ ccflags-y   += 
-I$(srctree)/drivers/usb/gadget/udc/
 # USB Functions
 usb_f_acm-y:= f_acm.o
 obj-$(CONFIG_USB_F_ACM)+= usb_f_acm.o
+obj-$(CONFIG_USB_F_CCID)   += f_ccid.o
 usb_f_ss_lb-y  := f_loopback.o f_sourcesink.o
 obj-$(CONFIG_USB_F_SS_LB)  += usb_f_ss_lb.o
 obj-$(CONFIG_USB_U_SERIAL) += u_serial.o
diff --git a/drivers/usb/gadget/function/f_ccid.c 
b/drivers/usb/gadget/function/f_ccid.c
new file mode 100644
index ..9ff8615ca303
--- /dev/null
+++ b/drivers/usb/gadget/function/f_ccid.c
@@ -0,0 +1,988 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * f_ccid.c -- Chip Card Interface Device (CCID) function Driver
+ *
+ * Copyright (C) 2018 Marcus Folkesson <marcus.folkes...@gmail.com>
+ *
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "f_ccid.h"
+#include "u_f.h"
+
+/* Number of tx requests to allocate */
+#define N_TX_REQS 4
+
+/* Maximum number of devices */
+#define CCID_MINORS 4
+
+struct ccidg_bulk_dev {
+   atomic_t is_open;
+   atomic_t rx_req_busy;
+   wait_queue_head_t read_wq;
+   wait_queue_head_t write_wq;
+   struct usb_request *rx_req;
+   atomic_t rx_done;
+   struct list_head tx_idle;
+};
+
+struct f_ccidg {
+   struct usb_function_instancefunc_inst;
+   struct usb_function function;
+   spinlock_t lock;
+   atomic_t online;
+
+   /* Character device */
+   struct cdev cdev;
+   int minor;
+
+   /* Dynamic attributes */
+   u32 features;
+   u32 protocols;
+   u8 pinsupport;
+   u8 nslots;
+   u8 lcdlayout;
+
+   /* Endpoints */
+   struct usb_ep *in;
+   struct usb_ep *out;
+   struct ccidg_bulk_dev bulk_dev;
+};
+
+/* Interface Descriptor: */
+static struct usb_interface_descriptor ccid_interface_desc = {
+   .bLength =  USB_DT_INTERFACE_SIZE,
+   .bDescriptorType =  USB_DT_INTERFACE,
+   .bNumEndpoints =2,
+   .bInterfaceClass =  USB_CLASS_CSCID,
+   .bInterfaceSubClass =   0,
+   .bInterfaceProtocol =   0,
+};
+
+/* CCID Class Descriptor */
+static struct ccid_class_descriptor ccid_class_desc = {
+   .bLength =  sizeof(ccid_class_desc),
+   .bDescriptorType =  CCID_DECRIPTOR_TYPE,
+   .bcdCCID =  CCID1_10,
+   /* .bMaxSlotIndex = DYNAMIC */
+   .bVoltageSupport =  CCID_VOLTS_3_0,
+   /* .dwProtocols =   DYNAMIC */
+   .dwDefaultClock =   3580,
+   .dwMaximumClock =   3580,
+   .bNumClockSupported =   0,
+   .dwDataRate =   9600,
+   .dwMaxDataRate =9600,
+   .bNumDataRatesSupported = 0,
+   .dwMaxIFSD =0,
+   .dwSynchProtocols = 0,
+   .dwMechanical = 0,
+   /* .dwFeatures =DYNAMIC */
+
+   /* extended APDU level Message Length */
+   .dwMaxCCIDMessageLength = 0x200,
+   .b

[PATCH 2/3] Documentation: usb: add documentation for USB CCID Gadget Device

2018-05-26 Thread Marcus Folkesson
Signed-off-by: Marcus Folkesson <marcus.folkes...@gmail.com>
---
 Documentation/usb/gadget_ccid.rst | 267 ++
 1 file changed, 267 insertions(+)
 create mode 100644 Documentation/usb/gadget_ccid.rst

diff --git a/Documentation/usb/gadget_ccid.rst 
b/Documentation/usb/gadget_ccid.rst
new file mode 100644
index ..5ac806b14604
--- /dev/null
+++ b/Documentation/usb/gadget_ccid.rst
@@ -0,0 +1,267 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+
+CCID Gadget
+
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+Introduction
+
+
+The CCID Gadget will present itself as a CCID device to the host system.
+The device supports two endpoints for now; BULK IN and BULK OUT.
+These endpoints is exposed to userspace via /dev/ccidg*.
+
+All CCID commands are sent on the BULK-OUT endpoint. Each command sent to the 
CCID
+has an associated ending response. Some commands can also have intermediate
+responses. The response is sent on the BULK-IN endpoint.
+See Figure 3-3 in the CCID Specification [1]_ for more details.
+
+The CCID commands must be handled in userspace since the driver is only working
+as a transport layer for the TPDUs.
+
+
+CCID Commands
+--
+
+All CCID commands begins with a 10 bytes header followed by an optional
+data field depending on message type.
+
+++--+---+--+
+| Offset | Field| Size  | Description  |
+++==+===+==+
+| 0  | bMessageType | 1 | Type of message  |
+++--+---+--+
+| 1  | dwLength | 4 | Message specific data length |
+||  |   |  |
+++--+---+--+
+| 5  | bSlot| 1 | Identifies the slot number   |
+||  |   | for this command |
+++--+---+--+
+| 6  | bSeq | 1 | Sequence number for command  |
+++--+---+--+
+| 7  | ...  | 3 | Fields depends on message type   |
+++--+---+--+
+| 10 | abData   | array | Message specific data (OPTIONAL) |
+++--+---+--+
+
+
+Multiple CCID gadgets
+--
+
+It is possible to create multiple instances of the CCID gadget, however,
+a much more flexible way is to create one gadget and set the `nslots` attribute
+to the number of desired CCID devices.
+
+All CCID commands specifies which slot that is the receiver in the `bSlot` 
field
+of the CCID header.
+
+Usage
+=
+
+Access from userspace
+--
+All communication is by read(2) and write(2) to the corresponding /dev/ccidg* 
device.
+Only one filedescriptor is allowed to be open to the device at a time.
+
+The buffer size provided to read(2) **must be at least** 522 (10 bytes header 
+ 512 bytes payload)
+bytes as we are working with whole commands.
+
+The buffer size provided to write(2) **may not exceed** 522 (10 bytes header + 
512 bytes payload)
+bytes as we are working with whole commands.
+
+
+Configuration with configfs
+
+
+ConfigFS is used to create and configure the CCID gadget.
+In order to get a device to work as intended, a few attributes must
+be considered.
+
+The attributes is described below followed by an example.
+
+features
+~
+
+The `feature` attribute writes to the dwFeatures field in the class descriptor.
+See Table 5.1-1 Smart Card Device Descriptors in the CCID Specification [1]_.
+
+The value indicates what intelligent features the CCID has.
+These values are available to user application as defines in ccid.h [2]_.
+The default value is 0x.
+
+The value is a bitwise OR operation performed on the following values:
+
++++
+| Value  | Description|
++++
+| 0x | No special characteristics |
++++
+| 0x0002 | Automatic parameter configuration based on ATR data|
++++
+| 0x0004 | Automatic activation of ICC on inserting   |
++++
+| 0x0008 | Automatic ICC volta

Re: [RESEND PATCH v5] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-03-16 Thread Marcus Folkesson
ping.

I do not want to nag, but would someone please have a look at this?

Thanks,
Marcus Folkesson

On Sun, Feb 18, 2018 at 05:17:46PM +0100, Marcus Folkesson wrote:
> 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 <marcus.folkes...@gmail.com>
> ---
> 
> v5:
>   - Drop autosuspend support
>   - Use pm_mutex instead of input_dev->mutex
>   - Use pxrc->is_open instead of input_dev->users
> v4:
>   - Add call to usb_mark_last_busy() in irq
>   - Move code from pxrc_resume() to pxrc_reset_resume()
> v3:
>   - Use RUDDER and MISC instead of TILT_X and TILT_Y
>   - Drop kref and anchor
>   - Rework URB handling
>   - Add PM support
> 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| 303 
> +++
>  4 files changed, 370 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 ..ca11f646bae8
> --- /dev/null
> +++ b/Documentation/input/devices/pxrc.rst
> @@ -0,0 +1,57 @@
> +=======
> +pxrc - PhoenixRC Flight Controller Adapter
> +===
> +
> +:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
> +
> +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_RUDDER|
> ++-++
> +| 6   |  ABS_THROTTLE  |
> ++-++
> +| 7   |  ABS_MISC  |
> ++-++
> +
> +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..332c0cc1b2ab 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
> + depends on 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 -

[RESEND PATCH v5] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-02-18 Thread Marcus Folkesson
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 <marcus.folkes...@gmail.com>
---

v5:
- Drop autosuspend support
- Use pm_mutex instead of input_dev->mutex
- Use pxrc->is_open instead of input_dev->users
v4:
- Add call to usb_mark_last_busy() in irq
- Move code from pxrc_resume() to pxrc_reset_resume()
v3:
- Use RUDDER and MISC instead of TILT_X and TILT_Y
- Drop kref and anchor
- Rework URB handling
- Add PM support
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| 303 +++
 4 files changed, 370 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 ..ca11f646bae8
--- /dev/null
+++ b/Documentation/input/devices/pxrc.rst
@@ -0,0 +1,57 @@
+===
+pxrc - PhoenixRC Flight Controller Adapter
+===
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+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_RUDDER|
++-++
+| 6   |  ABS_THROTTLE  |
++-++
+| 7   |  ABS_MISC  |
++-++
+
+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..332c0cc1b2ab 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
+   depends on 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 ..07a0dbd3ced2
--- /dev/null
+++ b/drivers/input/joystick/pxrc.c
@@ -0,0 +1,303 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Phoenix RC Flight Controller Adapter
+ *
+ * Copyright (C) 2018 Marcus Folkesson <marcus.folkes...@gmail.com>
+ *
+ */
+
+#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 pxrc {
+   str

Re: [PATCH v5] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-30 Thread Marcus Folkesson
Hello Dmitry,

Do you mind have a look at v5?

Thanks!


On Sat, Jan 20, 2018 at 09:58:40PM +0100, Marcus Folkesson wrote:
> 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 <marcus.folkes...@gmail.com>
> ---
> 
> v5:
>   - Drop autosuspend support
>   - Use pm_mutex instead of input_dev->mutex
>   - Use pxrc->is_open instead of input_dev->users
> v4:
>   - Add call to usb_mark_last_busy() in irq
>   - Move code from pxrc_resume() to pxrc_reset_resume()
> v3:
>   - Use RUDDER and MISC instead of TILT_X and TILT_Y
>   - Drop kref and anchor
>   - Rework URB handling
>   - Add PM support
> 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| 303 
> +++
>  4 files changed, 370 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 ..ca11f646bae8
> --- /dev/null
> +++ b/Documentation/input/devices/pxrc.rst
> @@ -0,0 +1,57 @@
> +=======
> +pxrc - PhoenixRC Flight Controller Adapter
> +===
> +
> +:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
> +
> +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_RUDDER|
> ++-++
> +| 6   |  ABS_THROTTLE  |
> ++-++
> +| 7   |  ABS_MISC  |
> ++-++
> +
> +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..332c0cc1b2ab 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
> + depends on 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/

Re: [PATCH v3] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-20 Thread Marcus Folkesson
Hello Dmitry,

On Fri, Jan 19, 2018 at 03:24:32PM -0800, Dmitry Torokhov wrote:
> On Wed, Jan 17, 2018 at 02:58:40PM +0100, Marcus Folkesson wrote:
> > Hello Dmitry,
> > 
> > On Tue, Jan 16, 2018 at 03:16:25PM -0800, Dmitry Torokhov wrote:
> > > Hi Marcus,
> > > 
> > > On Sat, Jan 13, 2018 at 09:15:32PM +0100, Marcus Folkesson wrote:
> > > > 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 <marcus.folkes...@gmail.com>
> > > > ---
> > > > v3:
> > > > - Use RUDDER and MISC instead of TILT_X and TILT_Y
> > > > - Drop kref and anchor
> > > > - Rework URB handling
> > > > - Add PM support
> > > 
> > > How did you test the PM support? By default the autopm is disabled on
> > > USB devices; you need to enable it by writing to sysfs (I believe you
> > > need to 'echo "auto" > /sys/bus/usb//power/control) and see if
> > > it gets autosuspended when not in use and resumed after you start
> > > interacting with it.
> > 
> > The test I've done is simply reading from the input device and then call
> > `pm-suspend`.
> > It works, suspend is called and reset_resume() will submit the URB
> > again. Without the PM code, the application did not read any events upon
> > resume.
> 
> We are talking about different things. You are testing system suspend,
> whereas I was talking about runtime suspend (that's what
> usb_autopm_get_interface() and friends does). It is disabled by default
> and you need to enable it by writing into sysfs as I mentioned above.
> Then, after a few seconds of not touching the device you should see the
> USB interface going into low power state and the device shoudl correctly
> implement remote wakeup signal to wake up the host controller/port when
> user touches it. If the device does not implement this correctly, then
> after suspending it will "die".
> 


Ok, I have read more about the autosuspend feature and I will drop
the support as the device does not seems to support remote wakeup
signals.

> > 
> > However, I found another tricky part.
> > If I enable autosuspend (as you suggest) it will suspend when noone is
> > using the device. Good.
> > 
> > But when someone is opening the device, input_dev->users is counted up
> > to 1 before resume() is called. 
> > Is this intended?
> > 
> > This code (from resume()) will therefor allways submit the URB:
> > 
> > if (input_dev->users && usb_submit_urb(pxrc->urb, GFP_NOIO) < 0)
> > 
> > 
> > Then open() is called and fails because the urb is allready submitted.
> > 
> > input_dev->users is only incremented in input.c:input_open_device() what
> > I can tell?
> 
> It is intended, but I guess we should not be using input_dev->users in
> resume(), but rather have a local flag in your driver structure trhat
> you update at the right time (i.e. after you submit USB in pxrc_open()).
> 
> I suppose we need the same fix in synaptics_usb.c...
> 

Will do.
I fix the synaptics_usb driver as well.

Also, I think we have a deadlock in the synaptics_usb driver.

When the device is suspended and someone is open the device, the input
subsystem will call input_open_device() which takes the
input_dev->mutex and then call input_dev->open().

synusb_open() has a call to usb_autopm_get_interface() which will
result in a call to the registered resume-function if the device is
suspended. (see Documentation/driver-api/usb/power-manaement.rst).

In the case of snaptics_usb, it will take the input_dev->mutex in the
resume function.

I have no synaptic mouse, but tested to put the same code into my
driver just to confirm, and got the following dump:

[ 9215.626476] INFO: task input-events:8590 blocked for more than 120 seconds.
[ 9215.626495]   Not tainted 4.15.0-rc8-ARCH+ #6
[ 9215.626500] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this 
message.
[ 9215.626507] input-eventsD0  8590   4394 0x0004
[ 9215.626520] Call Trace:
[ 9215.626546]  ? __schedule+0x236/0x850
[ 9215.626559]  schedule+0x2f/0x90
[ 9215.626569]  schedule_preempt_disabled+0x11/0x20
[ 9215.626579]  __mutex_lock.isra.0+0x1aa/0x520
[ 9215.626609]  ? usb_runtime_suspend+0x70/0x70 [usbcore]
[ 9215.626622]  ? pxrc_resume+0x37/0x70 [pxrc]
[ 9215.626632]  pxrc_resume+0x37/0x70 [pxrc]
[ 9215.626655]  usb_resume_interface.isra.2+0x39/0xe0 [usbcore]
[ 9215.626676]  usb_resume_both+0xd2/0x120 [usbcore]
[ 9215.626688]  __rpm_callback+0xb6/0x1f0
[

[PATCH v5] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-20 Thread Marcus Folkesson
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 <marcus.folkes...@gmail.com>
---

v5:
- Drop autosuspend support
- Use pm_mutex instead of input_dev->mutex
- Use pxrc->is_open instead of input_dev->users
v4:
- Add call to usb_mark_last_busy() in irq
- Move code from pxrc_resume() to pxrc_reset_resume()
v3:
- Use RUDDER and MISC instead of TILT_X and TILT_Y
- Drop kref and anchor
- Rework URB handling
- Add PM support
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| 303 +++
 4 files changed, 370 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 ..ca11f646bae8
--- /dev/null
+++ b/Documentation/input/devices/pxrc.rst
@@ -0,0 +1,57 @@
+===
+pxrc - PhoenixRC Flight Controller Adapter
+===
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+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_RUDDER|
++-++
+| 6   |  ABS_THROTTLE  |
++-++
+| 7   |  ABS_MISC  |
++-++
+
+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..332c0cc1b2ab 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
+   depends on 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 ..07a0dbd3ced2
--- /dev/null
+++ b/drivers/input/joystick/pxrc.c
@@ -0,0 +1,303 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Phoenix RC Flight Controller Adapter
+ *
+ * Copyright (C) 2018 Marcus Folkesson <marcus.folkes...@gmail.com>
+ *
+ */
+
+#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 pxrc {
+   str

Re: [PATCH v4] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-17 Thread Marcus Folkesson
Hi Randy!

On Wed, Jan 17, 2018 at 11:13:43AM -0800, Randy Dunlap wrote:
> On 01/17/2018 11:07 AM, Marcus Folkesson wrote:
> > 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 <marcus.folkes...@gmail.com>
> > ---
> 
> > 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
> 
> Drop the (first) depends and change "select USB" to "depends on USB".
> 
> Argh, there are lots of input/ drivers that do that.  That's bad/wrong.
> A driver should not select (enable) an entire subsystem.  If the user had
> the subsystem disabled, it should stay that way.
> 
> Dmitry, you can expect some patches...
> 

Hah, of course it should be "depends on" :-)
Other drivers fooled me...

Should I fix the other drivers too when I'm on it, or do you?


> > +   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
> 
> -- 
> ~Randy


Best regards
Marcus Folkesson


signature.asc
Description: PGP signature


[PATCH v4] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-17 Thread Marcus Folkesson
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 <marcus.folkes...@gmail.com>
---

v4:
- Add call to usb_mark_last_busy() in irq
- Move code from pxrc_resume() to pxrc_reset_resume()
v3:
- Use RUDDER and MISC instead of TILT_X and TILT_Y
- Drop kref and anchor
- Rework URB handling
- Add PM support
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| 318 +++
 4 files changed, 385 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 ..ca11f646bae8
--- /dev/null
+++ b/Documentation/input/devices/pxrc.rst
@@ -0,0 +1,57 @@
+===
+pxrc - PhoenixRC Flight Controller Adapter
+===
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+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_RUDDER|
++-++
+| 6   |  ABS_THROTTLE  |
++-++
+| 7   |  ABS_MISC  |
++-++
+
+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 ..216df4bf4484
--- /dev/null
+++ b/drivers/input/joystick/pxrc.c
@@ -0,0 +1,318 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Phoenix RC Flight Controller Adapter
+ *
+ * Copyright (C) 2018 Marcus Folkesson <marcus.folkes...@gmail.com>
+ *
+ */
+
+#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 pxrc {
+   struct input_dev*input;
+   struct usb_device   *udev;
+   struct usb_interface*intf;
+   struct urb  *urb;
+   __u8  

Re: [PATCH v3] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-17 Thread Marcus Folkesson
Hello Dmitry,

On Tue, Jan 16, 2018 at 03:16:25PM -0800, Dmitry Torokhov wrote:
> Hi Marcus,
> 
> On Sat, Jan 13, 2018 at 09:15:32PM +0100, Marcus Folkesson wrote:
> > 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 <marcus.folkes...@gmail.com>
> > ---
> > v3:
> > - Use RUDDER and MISC instead of TILT_X and TILT_Y
> > - Drop kref and anchor
> > - Rework URB handling
> > - Add PM support
> 
> How did you test the PM support? By default the autopm is disabled on
> USB devices; you need to enable it by writing to sysfs (I believe you
> need to 'echo "auto" > /sys/bus/usb//power/control) and see if
> it gets autosuspended when not in use and resumed after you start
> interacting with it.

The test I've done is simply reading from the input device and then call
`pm-suspend`.
It works, suspend is called and reset_resume() will submit the URB
again. Without the PM code, the application did not read any events upon
resume.

However, I found another tricky part.
If I enable autosuspend (as you suggest) it will suspend when noone is
using the device. Good.

But when someone is opening the device, input_dev->users is counted up
to 1 before resume() is called. 
Is this intended?

This code (from resume()) will therefor allways submit the URB:

if (input_dev->users && usb_submit_urb(pxrc->urb, GFP_NOIO) < 0)


Then open() is called and fails because the urb is allready submitted.

input_dev->users is only incremented in input.c:input_open_device() what
I can tell?

I will move the submitting code to reset_resume() instead.


> 
> > 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| 320 
> > +++
> >  4 files changed, 387 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 ..ca11f646bae8
> > --- /dev/null
> > +++ b/Documentation/input/devices/pxrc.rst
> > @@ -0,0 +1,57 @@
> > +===
> > +pxrc - PhoenixRC Flight Controller Adapter
> > +===
> > +
> > +:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
> > +
> > +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_RUDDER|
> > ++-++
> > +| 6   |  ABS_THROTTLE  |
> > ++-++
> > +| 7   |  ABS_MISC  |
> > ++-++
> > +
> > +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 

[PATCH v3] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-13 Thread Marcus Folkesson
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 <marcus.folkes...@gmail.com>
---
v3:
- Use RUDDER and MISC instead of TILT_X and TILT_Y
- Drop kref and anchor
- Rework URB handling
- Add PM support
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| 320 +++
 4 files changed, 387 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 ..ca11f646bae8
--- /dev/null
+++ b/Documentation/input/devices/pxrc.rst
@@ -0,0 +1,57 @@
+===
+pxrc - PhoenixRC Flight Controller Adapter
+===
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+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_RUDDER|
++-++
+| 6   |  ABS_THROTTLE  |
++-++
+| 7   |  ABS_MISC  |
++-++
+
+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 ..98d9b8184c46
--- /dev/null
+++ b/drivers/input/joystick/pxrc.c
@@ -0,0 +1,320 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for Phoenix RC Flight Controller Adapter
+ *
+ * Copyright (C) 2018 Marcus Folkesson <marcus.folkes...@gmail.com>
+ *
+ */
+
+#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 pxrc {
+   struct input_dev*input;
+   struct usb_device   *udev;
+   struct usb_interface*intf;
+   struct urb  *urb;
+   __u8epaddr;
+   charphys[64];
+   unsigned char   *data;
+   size_t

Re: [RESEND PATCH v2] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-11 Thread Marcus Folkesson
Hi Dmitry,

Thank you for your review!

On Wed, Jan 10, 2018 at 04:37:14PM -0800, Dmitry Torokhov wrote:
> Hi Marcus,
> 
> On Wed, Jan 10, 2018 at 02:11:39PM +0100, Marcus Folkesson wrote:
> > 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 <marcus.folkes...@gmail.com>
> > ---
> > 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 ..0ec466c58958
> > --- /dev/null
> > +++ b/Documentation/input/devices/pxrc.rst
> > @@ -0,0 +1,57 @@
> > +=======
> > +pxrc - PhoenixRC Flight Controller Adapter
> > +===
> > +
> > +:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
> > +
> > +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|
> > ++-++
> 
> TILT are normally reserved for stylus/pen. Do we have better event codes
> maybe? Is there a picture of the RC so I can make more sense of the
> proposed event assignment?
> 

Ok, maybe RUDDER and MISC?
The driver is actually for an "adapter" [1] that you connect your RC to.
The RC is then mapping its sticks/buttons to different channels. Unlike other
joysticks, this mapping is not fixed but something you setup in your RC.

For example, I'm using a Turnigy 9xr[2].

The RC is typically used with a RC Flight Simulator software (I'm using
Heli-X [3] when testing) which also map the channels to events (throttle,
rudder and so on).


> > +| 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 

[RESEND PATCH v2] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2018-01-10 Thread Marcus Folkesson
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 <marcus.folkes...@gmail.com>
---
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 ..0ec466c58958
--- /dev/null
+++ b/Documentation/input/devices/pxrc.rst
@@ -0,0 +1,57 @@
+===
+pxrc - PhoenixRC Flight Controller Adapter
+===
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+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 ..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 <marcus.folkes...@gmail.com>
+ *
+ */
+
+#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;
+   __u8epaddr;
+   charphys[64];
+   unsigned char   *data;
+   size_t  bsize;
+   struct kref kref;
+};
+
+#define to_pxrc_dev(d) container_of(d, struct usb_pxrc, kref)
+static 

[PATCH v2] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2017-12-31 Thread Marcus Folkesson
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 <marcus.folkes...@gmail.com>
---
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 ..0ec466c58958
--- /dev/null
+++ b/Documentation/input/devices/pxrc.rst
@@ -0,0 +1,57 @@
+===
+pxrc - PhoenixRC Flight Controller Adapter
+===
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+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 ..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 <marcus.folkes...@gmail.com>
+ *
+ */
+
+#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;
+   __u8epaddr;
+   charphys[64];
+   unsigned char   *data;
+   size_t  bsize;
+   struct kref kref;
+};
+
+#define to_pxrc_dev(d) container_of(d, struct usb_pxrc, kref)
+static 

[PATCH] input: pxrc: new driver for PhoenixRC Flight Controller Adapter

2017-12-31 Thread Marcus Folkesson
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 <marcus.folkes...@gmail.com>
---

Happy New Year!

 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 ..0ec466c58958
--- /dev/null
+++ b/Documentation/input/devices/pxrc.rst
@@ -0,0 +1,57 @@
+===
+pxrc - PhoenixRC Flight Controller Adapter
+===
+
+:Author: Marcus Folkesson <marcus.folkes...@gmail.com>
+
+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 ..87a9c09124b4
--- /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 <marcus.folkes...@gmail.com>
+ *
+ */
+
+#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;
+   __u8epaddr;
+   charphys[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