Skip to contentSkip to conversation

input: add support for the USB IR Toy and USB IR Droid

PATCH In review Sean Young 3 participants 11 messages May 7, 2020 – May 13, 2020

Subsystems (4)
lore
Share & tools
lore

Sean Young

author maintainer
#0 lore raw

PATCH 1/3 input: add support for the USB IR Toy and USB IR Droid

Add new serio ID for the USB IR Toy and IR Droid IR transceivers.

More information about the hardware:

http://dangerousprototypes.com/docs/USB_Infrared_Toy
https://www.irdroid.com/irdroid-usb-ir-transceiver/

---
 include/uapi/linux/serio.h | 1 +
 1 file changed, 1 insertion(+)
include/uapi/linux/serio.h +1 -0
index ed2a96f43ce4f..00c0be2546e93 100644
@@ -83,5 +83,6 @@
 #define SERIO_PULSE8_CEC	0x40
 #define SERIO_RAINSHADOW_CEC	0x41
 #define SERIO_FSIA6B	0x42
+#define SERIO_IRTOY	0x43
 
 #endif /* _UAPI_SERIO_H */
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH 2/3 input: serio: allow more than one byte to be sent at once

serio drivers can only send one byte at a time. If the underlying tty is a usb serial port, then each byte will be put into separate usb urbs, which is not efficient.

Additionally, the Infrared Toy device refuses to transmit IR if the IR data is sent one byte at a time. IR data is formatted in u16 values, and the firmware expects complete u16 values in the packet.

https://github.com/DangerousPrototypes/USB_IR_Toy/blob/master/Firmware-main/IRs.c#L240

---
 drivers/input/serio/serport.c |  9 +++++++++
 include/linux/serio.h         | 23 ++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 1 deletion(-)
drivers/input/serio/serport.c +9 -0
index 8ac970a423de6..887801691dddc 100644
@@ -47,6 +47,14 @@ static int serport_serio_write(struct serio *serio, unsigned char data)
 	return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
 }
 
+static int serport_serio_write_buf(struct serio *serio, unsigned char *data,
+				   uint count)
+{
+	struct serport *serport = serio->port_data;
+
+	return -(serport->tty->ops->write(serport->tty, data, count) != count);
+}
+
 static int serport_serio_open(struct serio *serio)
 {
 	struct serport *serport = serio->port_data;
@@ -173,6 +181,7 @@ static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, u
 	serio->id = serport->id;
 	serio->id.type = SERIO_RS232;
 	serio->write = serport_serio_write;
+	serio->write_buf = serport_serio_write_buf;
 	serio->open = serport_serio_open;
 	serio->close = serport_serio_close;
 	serio->port_data = serport;
include/linux/serio.h +22 -1
index 6c27d413da921..3918e56aec51c 100644
@@ -32,6 +32,7 @@ struct serio {
 	spinlock_t lock;
 
 	int (*write)(struct serio *, unsigned char);
+	int (*write_buf)(struct serio *serio, unsigned char *buf, uint size);
 	int (*open)(struct serio *);
 	void (*close)(struct serio *);
 	int (*start)(struct serio *);
@@ -121,12 +122,32 @@ void serio_unregister_driver(struct serio_driver *drv);
 
 static inline int serio_write(struct serio *serio, unsigned char data)
 {
-	if (serio->write)
+	if (serio->write_buf)
+		return serio->write_buf(serio, &data, 1);
+	else if (serio->write)
 		return serio->write(serio, data);
 	else
 		return -1;
 }
 
+static inline int serio_write_buf(struct serio *serio, unsigned char *data,
+				  uint size)
+{
+	if (serio->write_buf) {
+		return serio->write_buf(serio, data, size);
+	} else if (serio->write) {
+		int ret;
+
+		do {
+			ret = serio->write(serio, *data++);
+		} while (ret == 0 && --size);
+
+		return ret;
+	} else {
+		return -1;
+	}
+}
+
 static inline void serio_drv_write_wakeup(struct serio *serio)
 {
 	if (serio->drv && serio->drv->write_wakeup)
Signature · 1 line
2.26.2
maintainer
#2 lore raw

Re: [PATCH 2/3] input: serio: allow more than one byte to be sent at once

Quoted 9 lines · Sean Young

serio drivers can only send one byte at a time. If the underlying tty is a usb serial port, then each byte will be put into separate usb urbs, which is not efficient.

Additionally, the Infrared Toy device refuses to transmit IR if the IR data is sent one byte at a time. IR data is formatted in u16 values, and the firmware expects complete u16 values in the packet.

https://github.com/DangerousPrototypes/USB_IR_Toy/blob/master/Firmware-main/IRs.c#L240

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

serio is explicitly byte-oriented layer, of you need to exchange larger data I'd recommend using USB natively maybe?

Thanks.

Signature · 1 line
Dmitry
author maintainer
#3 lore raw

Re: [PATCH 2/3] input: serio: allow more than one byte to be sent at once

Quoted 5 lines · Dmitry Torokhov
Quoted 4 lines · Sean Young

serio drivers can only send one byte at a time. If the underlying tty is a usb serial port, then each byte will be put into separate usb urbs, which is not efficient.

Quoted 8 lines
Quoted 5 lines

Additionally, the Infrared Toy device refuses to transmit IR if the IR data is sent one byte at a time. IR data is formatted in u16 values, and the firmware expects complete u16 values in the packet.

https://github.com/DangerousPrototypes/USB_IR_Toy/blob/master/Firmware-main/IRs.c#L240

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

serio is a layer on top several serial protocols, including ttys. ttys allow more than one byte to be written at a time, see struct tty_operations:

int (*write)(struct tty_struct * tty,
const unsigned char *buf, int count);

ttys would be very inefficient if you could only write one byte at a time, and they are very serial.

This patch exposes the underlying tty write() functionality to serio. When the underlying tty is a usb serial port this makes for far fewer usb packets being used to send the same data, and fixes my driver problem, and it would reduce the number of calls in a few other cases too.

I'm happy to rework the patch if there are comments on the style or approach.

Thanks,

Sean

#4 lore raw

Re: [PATCH 2/3] input: serio: allow more than one byte to be sent at once

Quoted 24 lines · Sean Young
Quoted 5 lines · Dmitry Torokhov
Quoted 4 lines · Sean Young

serio drivers can only send one byte at a time. If the underlying tty is a usb serial port, then each byte will be put into separate usb urbs, which is not efficient.

Quoted 8 lines
Quoted 5 lines

Additionally, the Infrared Toy device refuses to transmit IR if the IR data is sent one byte at a time. IR data is formatted in u16 values, and the firmware expects complete u16 values in the packet.

https://github.com/DangerousPrototypes/USB_IR_Toy/blob/master/Firmware-main/IRs.c#L240

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

serio is a layer on top several serial protocols, including ttys. ttys allow more than one byte to be written at a time, see struct tty_operations:

int (*write)(struct tty_struct * tty,
const unsigned char *buf, int count);

ttys would be very inefficient if you could only write one byte at a time, and they are very serial.

Quoted 7 lines

This patch exposes the underlying tty write() functionality to serio. When the underlying tty is a usb serial port this makes for far fewer usb packets being used to send the same data, and fixes my driver problem, and it would reduce the number of calls in a few other cases too.

I'm happy to rework the patch if there are comments on the style or approach.

Why not just use the ir-usb.c driver for this device instead?

thanks,

greg k-h

author maintainer
#5 lore raw

Re: [PATCH 2/3] input: serio: allow more than one byte to be sent at once

Quoted 30 lines · Greg KH
Quoted 29 lines · Sean Young
Quoted 5 lines · Dmitry Torokhov
Quoted 4 lines · Sean Young

serio drivers can only send one byte at a time. If the underlying tty is a usb serial port, then each byte will be put into separate usb urbs, which is not efficient.

Quoted 8 lines
Quoted 5 lines

Additionally, the Infrared Toy device refuses to transmit IR if the IR data is sent one byte at a time. IR data is formatted in u16 values, and the firmware expects complete u16 values in the packet.

https://github.com/DangerousPrototypes/USB_IR_Toy/blob/master/Firmware-main/IRs.c#L240

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

serio is a layer on top several serial protocols, including ttys. ttys allow more than one byte to be written at a time, see struct tty_operations:

int (*write)(struct tty_struct * tty,
const unsigned char *buf, int count);

ttys would be very inefficient if you could only write one byte at a time, and they are very serial.

This patch exposes the underlying tty write() functionality to serio. When the underlying tty is a usb serial port this makes for far fewer usb packets being used to send the same data, and fixes my driver problem, and it would reduce the number of calls in a few other cases too.

Quoted 4 lines
Quoted 2 lines

I'm happy to rework the patch if there are comments on the style or approach.

Why not just use the ir-usb.c driver for this device instead?

So this device is the infrared kind which rc-core (in drivers/media/rc/) supports, remotes and such things (not for serial IR). So by using a rc-core driver, it can use kernel IR decoding, BPF decoding, lirc chardev and rc keymaps, etc.

This device is a PIC18F2550 type device, which is a usb serial port microcontroller type with some firmware and IR diodes:
http://dangerousprototypes.com/docs/USB_IR_Toy_v2

serio supports a whole bunch of usb serial devices which can be attached via inputattach(1). Not all of these are input devices, for example there are two cec devices too.

Now, in many of these drivers, multiple bytes need to be written to the device in order to send it a command, for example in drivers/input/touchscreen/elo.c:

        for (i = 0; i < ELO10_PACKET_LEN; i++) {
                csum += packet[i];
                if (serio_write(elo->serio, packet[i]))
                        goto out;
        }

So if serio had an interface for sending a buffer, that would be less call overhead. In fact, if the underlying serio is a serial usb port, that would much more efficient on the usb layer too (one usb roundtrips in stead of ELO10_PACKET_LEN roundtrips), like so:

	serio_write_buf(elo->serio, packet, ELO10_PACKET_LEN);

So what I'm suggesting is extending the serio interface to allow sending a buffer of bytes. Of course serio isn't just usb serial ports. There quite a few instances of serio_register_port() in the kernel. Many of them can be extended to support sending a buffer rather than a single byte, if this makes sense. For example the ps2 serio port takes a mutex for every byte, so this could be more efficient by reducing it to one mutex lock per buffer.

Now it would be nice to have a discussion about this rather than being dismissed with:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

Which is just a tad insulting.

Sean

maintainer
#6 lore raw

Re: [PATCH 2/3] input: serio: allow more than one byte to be sent at once

Quoted 74 lines · Sean Young
Quoted 30 lines · Greg KH
Quoted 29 lines · Sean Young
Quoted 5 lines · Dmitry Torokhov
Quoted 4 lines · Sean Young

serio drivers can only send one byte at a time. If the underlying tty is a usb serial port, then each byte will be put into separate usb urbs, which is not efficient.

Quoted 8 lines
Quoted 5 lines

Additionally, the Infrared Toy device refuses to transmit IR if the IR data is sent one byte at a time. IR data is formatted in u16 values, and the firmware expects complete u16 values in the packet.

https://github.com/DangerousPrototypes/USB_IR_Toy/blob/master/Firmware-main/IRs.c#L240

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

serio is a layer on top several serial protocols, including ttys. ttys allow more than one byte to be written at a time, see struct tty_operations:

int (*write)(struct tty_struct * tty,
const unsigned char *buf, int count);

ttys would be very inefficient if you could only write one byte at a time, and they are very serial.

This patch exposes the underlying tty write() functionality to serio. When the underlying tty is a usb serial port this makes for far fewer usb packets being used to send the same data, and fixes my driver problem, and it would reduce the number of calls in a few other cases too.

Quoted 4 lines
Quoted 2 lines

I'm happy to rework the patch if there are comments on the style or approach.

Why not just use the ir-usb.c driver for this device instead?

So this device is the infrared kind which rc-core (in drivers/media/rc/) supports, remotes and such things (not for serial IR). So by using a rc-core driver, it can use kernel IR decoding, BPF decoding, lirc chardev and rc keymaps, etc.

This device is a PIC18F2550 type device, which is a usb serial port microcontroller type with some firmware and IR diodes:
http://dangerousprototypes.com/docs/USB_IR_Toy_v2

serio supports a whole bunch of usb serial devices which can be attached via inputattach(1). Not all of these are input devices, for example there are two cec devices too.

Now, in many of these drivers, multiple bytes need to be written to the device in order to send it a command, for example in drivers/input/touchscreen/elo.c:

        for (i = 0; i < ELO10_PACKET_LEN; i++) {
                csum += packet[i];
                if (serio_write(elo->serio, packet[i]))
                        goto out;
        }

So if serio had an interface for sending a buffer, that would be less call overhead. In fact, if the underlying serio is a serial usb port, that would much more efficient on the usb layer too (one usb roundtrips in stead of ELO10_PACKET_LEN roundtrips), like so:

	serio_write_buf(elo->serio, packet, ELO10_PACKET_LEN);

So what I'm suggesting is extending the serio interface to allow sending a buffer of bytes. Of course serio isn't just usb serial ports. There quite a few instances of serio_register_port() in the kernel. Many of them can be extended to support sending a buffer rather than a single byte, if this makes sense. For example the ps2 serio port takes a mutex for every byte, so this could be more efficient by reducing it to one mutex lock per buffer.

Quoted 7 lines

Now it would be nice to have a discussion about this rather than being dismissed with:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

Which is just a tad insulting.

That was not meant to be insulting, however serial protocol defines that the data size is at most 9 bits, so expecting that one can transmit anything more than that _atomically_ is wrong. If your device/firmware requires 16 bits to be transferred as indivisible units, then serial port abstraction is wrong one to be used.

Now serio is layer "above" serial ports (but does not have to have an underlying serial port) that provides byte-oriented communication that is expected to mostly flow into host. Its does not expect heavy data flows coming from the host and into the device (if you look at all the touchscreens, psmouse, etc, they all send initialization sequences to the device, and then all the data flows into the host). Therefore there is little benefit in optimizing serio writes.

You are using performance clams as a clutch for the requirement of sending u16s, but as I mentioned it is wrong if you use serial ports abstraction layer. Greg mentioned ir-usb. You can maybe enhance it, or create a similar driver that connects USB to rc-core and ensures that you can communicate with the device with exact format it needs.

Thanks.

Signature · 1 line
Dmitry
author maintainer
#7 lore raw

Re: [PATCH 2/3] input: serio: allow more than one byte to be sent at once

Quoted 7 lines · Dmitry Torokhov
Quoted 6 lines · Sean Young

Now it would be nice to have a discussion about this rather than being dismissed with:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

Quoted 7 lines
Quoted 1 line

Which is just a tad insulting.

That was not meant to be insulting, however serial protocol defines that the data size is at most 9 bits, so expecting that one can transmit anything more than that _atomically_ is wrong. If your device/firmware requires 16 bits to be transferred as indivisible units, then serial port abstraction is wrong one to be used.

Honestly thank you for explaining that. I had no idea this was an abstract point about the demarcations of serial port-ness.

There is no physical rs-232 cabling involved at all in this case.

Quoted 7 lines

Now serio is layer "above" serial ports (but does not have to have an underlying serial port) that provides byte-oriented communication that is expected to mostly flow into host. Its does not expect heavy data flows coming from the host and into the device (if you look at all the touchscreens, psmouse, etc, they all send initialization sequences to the device, and then all the data flows into the host). Therefore there is little benefit in optimizing serio writes.

True, I didn't think this would make much of an measurable improvement, but still, some.

Quoted 5 lines

You are using performance clams as a clutch for the requirement of sending u16s, but as I mentioned it is wrong if you use serial ports abstraction layer. Greg mentioned ir-usb. You can maybe enhance it, or create a similar driver that connects USB to rc-core and ensures that you can communicate with the device with exact format it needs.

Yes, I'll go down this route.

Thank you for the discussion, it was very helpful.

Sean

#8 lore raw

Re: [PATCH 2/3] input: serio: allow more than one byte to be sent at once

In reply to Sean Young
Quoted 34 lines · Sean Young
Quoted 33 lines · Greg KH
Quoted 31 lines · Sean Young
Quoted 5 lines · Dmitry Torokhov
Quoted 4 lines · Sean Young

serio drivers can only send one byte at a time. If the underlying tty is a usb serial port, then each byte will be put into separate usb urbs, which is not efficient.

Quoted 8 lines
Quoted 5 lines

Additionally, the Infrared Toy device refuses to transmit IR if the IR data is sent one byte at a time. IR data is formatted in u16 values, and the firmware expects complete u16 values in the packet.

https://github.com/DangerousPrototypes/USB_IR_Toy/blob/master/Firmware-main/IRs.c#L240

Ummm, serial protocol data size is at most 9 bits so I have no earthly idea how they expect to get 16.

serio is a layer on top several serial protocols, including ttys. ttys allow more than one byte to be written at a time, see struct tty_operations:

int (*write)(struct tty_struct * tty,
const unsigned char *buf, int count);

ttys would be very inefficient if you could only write one byte at a time, and they are very serial.

This patch exposes the underlying tty write() functionality to serio. When the underlying tty is a usb serial port this makes for far fewer usb packets being used to send the same data, and fixes my driver problem, and it would reduce the number of calls in a few other cases too.

I'm happy to rework the patch if there are comments on the style or approach.

Quoted 6 lines
Quoted 1 line

Why not just use the ir-usb.c driver for this device instead?

So this device is the infrared kind which rc-core (in drivers/media/rc/) supports, remotes and such things (not for serial IR). So by using a rc-core driver, it can use kernel IR decoding, BPF decoding, lirc chardev and rc keymaps, etc.

So why do you want to user serio for this? serio should only be for input devices with a serial protocol.

Quoted 33 lines

This device is a PIC18F2550 type device, which is a usb serial port microcontroller type with some firmware and IR diodes:
http://dangerousprototypes.com/docs/USB_IR_Toy_v2

serio supports a whole bunch of usb serial devices which can be attached via inputattach(1). Not all of these are input devices, for example there are two cec devices too.

Now, in many of these drivers, multiple bytes need to be written to the device in order to send it a command, for example in drivers/input/touchscreen/elo.c:

        for (i = 0; i < ELO10_PACKET_LEN; i++) {
                csum += packet[i];
                if (serio_write(elo->serio, packet[i]))
                        goto out;
        }

So if serio had an interface for sending a buffer, that would be less call overhead. In fact, if the underlying serio is a serial usb port, that would much more efficient on the usb layer too (one usb roundtrips in stead of ELO10_PACKET_LEN roundtrips), like so:

	serio_write_buf(elo->serio, packet, ELO10_PACKET_LEN);

So what I'm suggesting is extending the serio interface to allow sending a buffer of bytes. Of course serio isn't just usb serial ports. There quite a few instances of serio_register_port() in the kernel. Many of them can be extended to support sending a buffer rather than a single byte, if this makes sense. For example the ps2 serio port takes a mutex for every byte, so this could be more efficient by reducing it to one mutex lock per buffer.

Quoted 2 lines

Now it would be nice to have a discussion about this rather than being dismissed with:

I think a custom usb driver that exposes the interfaces as input devices is going to be the simplest thing for you to do here as you will have full control over the packet size and format much easier. Odds are it will be less work overall for this.

thanks,

greg k-h

author maintainer
#9 lore raw

Re: [PATCH 2/3] input: serio: allow more than one byte to be sent at once

Quoted 8 lines · Greg KH
Quoted 4 lines · Sean Young

So this device is the infrared kind which rc-core (in drivers/media/rc/) supports, remotes and such things (not for serial IR). So by using a rc-core driver, it can use kernel IR decoding, BPF decoding, lirc chardev and rc keymaps, etc.

So why do you want to user serio for this? serio should only be for input devices with a serial protocol.

Admittedly this is a bit tenuous.

What I'm trying to do is write a kernel driver which uses the usb serial drivers, and not write a poor man's version of usb serial in the IR driver.

Quoted 4 lines

I think a custom usb driver that exposes the interfaces as input devices is going to be the simplest thing for you to do here as you will have full control over the packet size and format much easier. Odds are it will be less work overall for this.

Admittedly I don't think it will be much code, so maybe it won't be so ugly. It's just the code duplication I was trying to avoid.

So, I'll go ahead and as you suggest.

Thank you for your time and thoughts on this.

Sean

author maintainer
#10 lore raw

PATCH 3/3 media: rc: add support for Infrared Toy and IR Droid devices

In reply to Sean Young

These devices are connected via an usb serial port, and need to be attached using "inputattach -irtoy /dev/ttyACM[0-9]" (patches will be sent once this is merged).

http://dangerousprototypes.com/docs/USB_Infrared_Toy
https://www.irdroid.com/irdroid-usb-ir-transceiver/

---
 drivers/media/rc/Kconfig  |  15 ++
 drivers/media/rc/Makefile |   1 +
 drivers/media/rc/ir_toy.c | 448 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 464 insertions(+)
 create mode 100644 drivers/media/rc/ir_toy.c
drivers/media/rc/Kconfig +15 -0
index c18dee6482536..aa690ba6a981a 100644
@@ -530,6 +530,21 @@ config IR_ZX
 	   To compile this driver as a module, choose M here: the
 	   module will be called zx-irdec.
 
+config IR_TOY
+	tristate "Infrared Toy and IR Droid"
+	depends on RC_CORE
+	select USB
+	select USB_ACM
+	select SERIO
+	select SERIO_SERPORT
+	help
+	   Say Y here if you want to use the Infrared Toy or IR Droid. This
+	   is a serio driver which needs to be attached to the usb serial
+	   port using inputattach.
+
+	   To compile this driver as a module, choose M here: the module will be
+	   called ir_toy.
+
 endif #RC_DEVICES
 
 endif #RC_CORE
drivers/media/rc/Makefile +1 -0
index 48d23433b3c06..5bb2932ab1195 100644
@@ -50,3 +50,4 @@ obj-$(CONFIG_IR_MTK) += mtk-cir.o
 obj-$(CONFIG_IR_ZX) += zx-irdec.o
 obj-$(CONFIG_IR_TANGO) += tango-ir.o
 obj-$(CONFIG_RC_XBOX_DVD) += xbox_remote.o
+obj-$(CONFIG_IR_TOY) += ir_toy.o
drivers/media/rc/ir_toy.c new file +448 -0

448 lines not on this page · raw message

Signature · 1 line
2.26.2