Skip to contentSkip to conversation

IR Toy / IR Droid USB driver

PATCH Superseded Sean Young 3 participants 10 messages May 27, 2020

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
author maintainer
#7 lore raw

PATCH v2 2/3 USB: cdc-acm: blacklist IR Droid / IR Toy device

In reply to Sean Young

The IR Droid / IR Toy device used custom protocol over serial line, so it is presenting itself as CDC ACM device.

Now that we have proper in-kernel driver for it we need to black-list the device in cdc-acm driver.

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

---
 drivers/usb/class/cdc-acm.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/usb/class/cdc-acm.c +6 -0
index 84d6f7df09a4..1369e3be5019 100644
@@ -1864,6 +1864,12 @@ static const struct usb_device_id acm_ids[] = {
 	},
 #endif
 
+#if IS_ENABLED(CONFIG_IR_TOY)
+	{ USB_DEVICE(0x04d8, 0xfd08),
+	.driver_info = IGNORE_DEVICE,
+	},
+#endif
+
 	/*Samsung phone in firmware update mode */
 	{ USB_DEVICE(0x04e8, 0x685d),
 	.driver_info = IGNORE_DEVICE,
Signature · 1 line
2.26.2

Sean Young

author maintainer
#0 lore raw

v2 cover IR Toy / IR Droid USB driver

v1 was a serio driver, this has now been re-written to be a USB driver. This requires a blacklist in the usb cdc-acm driver.

This also allows us to feed IR data to the device from the urb callback handler, which reduces the chance of unwanted gaps in the transmission.

Sean Young (3):
media: rc: add support for Infrared Toy and IR Droid devices
USB: cdc-acm: blacklist IR Droid / IR Toy device
MAINTAINERS: rc core and lirc maintainership

 MAINTAINERS                 |  13 +
 drivers/media/rc/Kconfig    |  11 +
 drivers/media/rc/Makefile   |   1 +
 drivers/media/rc/ir_toy.c   | 525 ++++++++++++++++++++++++++++++++++++
 drivers/usb/class/cdc-acm.c |   6 +
 5 files changed, 556 insertions(+)
 create mode 100644 drivers/media/rc/ir_toy.c
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

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

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

---
 drivers/media/rc/Kconfig  |  11 +
 drivers/media/rc/Makefile |   1 +
 drivers/media/rc/ir_toy.c | 525 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 537 insertions(+)
 create mode 100644 drivers/media/rc/ir_toy.c
drivers/media/rc/Kconfig +11 -0
index c18dee648253..2c0ee2e5b446 100644
@@ -530,6 +530,17 @@ 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
+	depends on USB_ARCH_HAS_HCD
+	help
+	   Say Y here if you want to use the Infrared Toy or IR Droid, USB
+	   versions.
+
+	   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 48d23433b3c0..5bb2932ab119 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 +525 -0

525 lines not on this page · raw message

Signature · 1 line
2.26.2
author maintainer
#9 lore raw

PATCH v2 3/3 MAINTAINERS: rc core and lirc maintainership

In reply to Sean Young

This has been maintained for some time, update MAINTAINERS to reflect.

---
 MAINTAINERS | 13 +++++++++++++
 1 file changed, 13 insertions(+)
MAINTAINERS +13 -0
index 552e82f9adc0..b9677d250f2c 100644
@@ -14180,6 +14180,19 @@ L:	[email protected]
 S:	Orphan
 F:	drivers/net/wireless/ray*
 
+RC CORE / LIRC FRAMEWORK
+M:	Sean Young <[email protected]>
+L:	[email protected]
+S:	Supported
+W:	http://linuxtv.org
+T:	git git://linuxtv.org/media_tree.git
+F:	Documentation/driver-api/media/rc-core.rst
+F:	Documentation/userspace-api/media/rc/
+F:	drivers/media/rc/
+F:	include/media/rc-map.h
+F:	include/media/rc-core.h
+F:	include/uapi/linux/lirc.h
+
 RCMM REMOTE CONTROLS DECODER
 M:	Patrick Lerda <[email protected]>
 S:	Maintained
Signature · 1 line
2.26.2
maintainer Acked-by on v2 2/3
#8 lore raw

Re: [PATCH v2 2/3] USB: cdc-acm: blacklist IR Droid / IR Toy device

In reply to Sean Young
Quoted 34 lines · Sean Young

The IR Droid / IR Toy device used custom protocol over serial line, so it is presenting itself as CDC ACM device.

Now that we have proper in-kernel driver for it we need to black-list the device in cdc-acm driver.

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

---
 drivers/usb/class/cdc-acm.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/usb/class/cdc-acm.c +6 -0
index 84d6f7df09a4..1369e3be5019 100644
@@ -1864,6 +1864,12 @@ static const struct usb_device_id acm_ids[] = {
 	},
 #endif
 
+#if IS_ENABLED(CONFIG_IR_TOY)
+	{ USB_DEVICE(0x04d8, 0xfd08),
+	.driver_info = IGNORE_DEVICE,
+	},
+#endif
+
 	/*Samsung phone in firmware update mode */
 	{ USB_DEVICE(0x04e8, 0x685d),
 	.driver_info = IGNORE_DEVICE,
Signature · 1 line
2.26.2
maintainer
#3 lore raw

Re: [PATCH v2 1/3] media: rc: add support for Infrared Toy and IR Droid devices

In reply to Sean Young

Am Mittwoch, den 27.05.2020, 10:41 +0100 schrieb Sean Young:

Hi,

thank you for the driver. Much cleaner than routing this through CDC_ACM.

I am afraid there are a few issues though. Nothing major. I've added remarks directly to the code. Would you care to fix them up?

Regards
Oliver

drivers/media/rc/ir_toy.c line 26

Quoted 5 lines
drivers/media/rc/ir_toy.c new file +5 -0
+static const u8 COMMAND_VERSION[] = { 'v' };
+// End transmit and repeat reset command so we exit sump mode
+static const u8 COMMAND_RESET[] = { 0xff, 0xff, 0, 0, 0, 0, 0 };
+static const u8 COMMAND_SMODE_ENTER[] = { 's' };
+static const u8 COMMAND_TXSTART[] = { 0x26, 0x24, 0x25, 0x03 };

Using these directly as buffers is based on the assuption that the kernel code is accessable by DMA. On some architectures that is false. You need to use a bounce buffer.

Quoted 24 lines
drivers/media/rc/ir_toy.c new file +24 -0
+
+#define REPLY_XMITCOUNT 't'
+#define REPLY_XMITSUCCESS 'C'
+#define REPLY_VERSION 'V'
+#define REPLY_SAMPLEMODEPROTO 'S'
+
+#define TIMEOUT 500
+
+#define LEN_XMITRES 3
+#define LEN_VERSION 4
+#define LEN_SAMPLEMODEPROTO 3
+
+#define MIN_FW_VERSION 20
+#define UNIT_NS 21333
+#define MAX_TIMEOUT_NS (UNIT_NS * U16_MAX)
+
+#define MAX_PACKET 64
+
+enum state {
+	STATE_IRDATA,
+	STATE_RESET,
+	STATE_COMMAND,
+	STATE_TX,
+};

drivers/media/rc/ir_toy.c line 60

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+
+struct irtoy {
+	struct device *dev;
+	struct usb_device *usbdev;
+
+	struct rc_dev *rc;
+	struct urb *urb_in, *urb_out;
+
+	u8 in[MAX_PACKET];
+	u8 out[MAX_PACKET];

This violates the DMA coherency rules. The buffers must be allocated separately with kmalloc().

drivers/media/rc/ir_toy.c line 128

Quoted 7 lines
drivers/media/rc/ir_toy.c new file +7 -0
+	case STATE_IRDATA: {
+		struct ir_raw_event rawir = { .pulse = irtoy->pulse };
+		__be16 *in = (__be16 *)irtoy->in;
+		int i;
+
+		for (i = 0; i < len / sizeof(__be16); i++) {
+			u32 v = be16_to_cpup(in + i);

Is this 16 or 32 bit?

Quoted 11 lines
drivers/media/rc/ir_toy.c new file +11 -0
+
+			if (v == 0xffff) {
+				rawir.pulse = false;
+			} else {
+				rawir.duration = v * UNIT_NS;
+				ir_raw_event_store_with_timeout(irtoy->rc,
+								&rawir);
+			}
+
+			rawir.pulse = !rawir.pulse;
+		}

drivers/media/rc/ir_toy.c line 149

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+
+		irtoy->pulse = rawir.pulse;
+
+		ir_raw_event_handle(irtoy->rc);
+		break;
+	}
+	case STATE_TX:
+		if (irtoy->tx_len == 0) {
+			if (len == LEN_XMITRES &&
+			    irtoy->in[0] == REPLY_XMITCOUNT) {

Endianness?

drivers/media/rc/ir_toy.c line 152

Quoted 3 lines
drivers/media/rc/ir_toy.c new file +3 -0
+				__be16 *emitted = (__be16 *)(irtoy->in + 1);
+
+				irtoy->emitted = be16_to_cpup(emitted);

Reason you are using cpup versions?

drivers/media/rc/ir_toy.c line 156

Quoted 4 lines
drivers/media/rc/ir_toy.c new file +4 -0
+			} else if (len == 1 &&
+				   irtoy->in[0] == REPLY_XMITSUCCESS) {
+				complete(&irtoy->rx_done);
+				irtoy->state = STATE_IRDATA;

Race condition. Whoever you wake up with that complete could read the old state.

Quoted 8 lines
drivers/media/rc/ir_toy.c new file +8 -0
+			}
+		} else {
+			// send next part of tx buffer
+			uint max_send = irtoy->in[0];
+			uint buf_len = min(max_send, irtoy->tx_len);
+			int err;
+
+			dev_dbg(irtoy->dev, "ready to receive: 0x%02x\n",

drivers/media/rc/ir_toy.c line 174

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+				max_send);
+
+			memcpy(irtoy->out, irtoy->tx_buf, buf_len);
+			irtoy->urb_out->transfer_buffer_length = buf_len;
+			err = usb_submit_urb(irtoy->urb_out, GFP_ATOMIC);
+			if (err != 0) {
+				dev_err(irtoy->dev, "fail to submit tx buf urb: %d\n",
+					err);
+				complete(&irtoy->rx_done);
+				irtoy->state = STATE_IRDATA;

Same race condition as above.

Quoted 18 lines
drivers/media/rc/ir_toy.c new file +18 -0
+			}
+
+			irtoy->tx_buf += buf_len;
+			irtoy->tx_len -= buf_len;
+			break;
+		}
+		break;
+	case STATE_RESET:
+		dev_err(irtoy->dev, "unexpected response to reset: %*phN\n",
+			len, irtoy->in);
+	}
+}
+
+static void irtoy_out_callback(struct urb *urb)
+{
+	struct irtoy *irtoy = urb->context;
+
+	switch (urb->status) {

drivers/media/rc/ir_toy.c line 202

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+	case 0:
+		if (irtoy->state == STATE_RESET)
+			complete(&irtoy->rx_done);
+		break;
+
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+	case -EPROTO:
+		usb_unlink_urb(urb);

Redundant.

Quoted 12 lines
drivers/media/rc/ir_toy.c new file +12 -0
+		return;
+
+	default:
+		dev_warn(irtoy->dev, "out urb status: %d\n", urb->status);
+	}
+}
+
+static void irtoy_in_callback(struct urb *urb)
+{
+	struct irtoy *irtoy = urb->context;
+	int ret;
+

drivers/media/rc/ir_toy.c line 224

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+	switch (urb->status) {
+	case 0:
+		irtoy_response(irtoy, urb->actual_length);
+		break;
+
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+	case -EPROTO:
+		usb_unlink_urb(urb);

Redundant.

Quoted 17 lines
drivers/media/rc/ir_toy.c new file +17 -0
+		return;
+
+	default:
+		dev_warn(irtoy->dev, "in urb status: %d\n", urb->status);
+	}
+
+	ret = usb_submit_urb(urb, GFP_ATOMIC);
+	if (ret && ret != -ENODEV)
+		dev_warn(irtoy->dev, "failed to resubmit urb: %d\n", ret);
+}
+
+static int irtoy_command(struct irtoy *irtoy, const u8 *cmd, int cmd_len)
+{
+	int err;
+
+	init_completion(&irtoy->rx_done);
+

drivers/media/rc/ir_toy.c line 251

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+	memcpy(irtoy->out, cmd, cmd_len);
+	irtoy->urb_out->transfer_buffer_length = cmd_len;
+
+	err = usb_submit_urb(irtoy->urb_out, GFP_KERNEL);
+	if (err != 0)
+		return err;
+
+	if (!wait_for_completion_timeout(&irtoy->rx_done,
+					 msecs_to_jiffies(TIMEOUT)))
+		return -ETIMEDOUT;

Wrong error handling. The URB is still active. You cannot free the buffer. The caller has no idea when that is safe to do so. You must kill the URB in the timeout case.

Quoted 8 lines
drivers/media/rc/ir_toy.c new file +8 -0
+
+	return 0;
+}
+
+/*
+ * When sending IR, it is imperative that we send the IR data as quickly
+ * as possible to the device, so it does not run out of IR data and
+ * introduce gaps. So, we feed the data from the urb callback handler

drivers/media/rc/ir_toy.c line 269

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+ */
+static int irtoy_tx(struct rc_dev *rc, uint *txbuf, uint count)
+{
+	struct irtoy *irtoy = rc->priv;
+	unsigned int i, size;
+	__be16 *buf;
+	int err;
+
+	size = sizeof(u16) * (count + 1);
+	buf = kmalloc(size, GFP_KERNEL);

This is incompatible with the comment. If you are potentially in interrupt, you must use GFP_ATOMIC. Please clarify.

drivers/media/rc/ir_toy.c line 476

Quoted 7 lines
drivers/media/rc/ir_toy.c new file +6 -0
+static int irtoy_suspend(struct usb_interface *intf, pm_message_t message)
+{
+	struct irtoy *irtoy = usb_get_intfdata(intf);
+
+	usb_kill_urb(irtoy->urb_in);
+	usb_kill_urb(irtoy->urb_out);

That is brutal. It could fail commands. Do you really want to do that?

drivers/media/rc/ir_toy.c line 486

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+
+	return 0;
+}
+
+static int irtoy_resume(struct usb_interface *intf)
+{
+	struct irtoy *irtoy = usb_get_intfdata(intf);
+	int err;
+
+	err = usb_submit_urb(irtoy->urb_in, GFP_KERNEL);

That should technically be GFP_NOIO

Quoted 5 lines
drivers/media/rc/ir_toy.c new file +5 -0
+	if (err)
+		dev_warn(&intf->dev, "failed to submit urb: %d\n", err);
+
+	return err;
+}
author maintainer
#4 lore raw

Re: [PATCH v2 1/3] media: rc: add support for Infrared Toy and IR Droid devices

Hi Oliver,

Quoted 6 lines · Oliver Neukum

Am Mittwoch, den 27.05.2020, 10:41 +0100 schrieb Sean Young:

Hi,

thank you for the driver. Much cleaner than routing this through CDC_ACM.

Yes, it certainly is. It took some time for that insight to sink in (for me).

Quoted 3 lines

I am afraid there are a few issues though. Nothing major. I've added remarks directly to the code. Would you care to fix them up?

Of course, thank you for reviewing it. The driver is much better for it.

I'll send out a v3 shortly, response and a question below.

Thanks,

Sean

Quoted 4 lines

Regards
Oliver

Quoted 9 lines

drivers/media/rc/ir_toy.c line 26

Quoted 5 lines
drivers/media/rc/ir_toy.c new file +5 -0
+static const u8 COMMAND_VERSION[] = { 'v' };
+// End transmit and repeat reset command so we exit sump mode
+static const u8 COMMAND_RESET[] = { 0xff, 0xff, 0, 0, 0, 0, 0 };
+static const u8 COMMAND_SMODE_ENTER[] = { 's' };
+static const u8 COMMAND_TXSTART[] = { 0x26, 0x24, 0x25, 0x03 };

Using these directly as buffers is based on the assuption that the kernel code is accessable by DMA. On some architectures that is false. You need to use a bounce buffer.

This is true, however these arrays are not directly accessed, they're memcpy'ed into irtoy->out.

Quoted 30 lines
Quoted 30 lines
drivers/media/rc/ir_toy.c new file +29 -0
+
+#define REPLY_XMITCOUNT 't'
+#define REPLY_XMITSUCCESS 'C'
+#define REPLY_VERSION 'V'
+#define REPLY_SAMPLEMODEPROTO 'S'
+
+#define TIMEOUT 500
+
+#define LEN_XMITRES 3
+#define LEN_VERSION 4
+#define LEN_SAMPLEMODEPROTO 3
+
+#define MIN_FW_VERSION 20
+#define UNIT_NS 21333
+#define MAX_TIMEOUT_NS (UNIT_NS * U16_MAX)
+
+#define MAX_PACKET 64
+
+enum state {
+	STATE_IRDATA,
+	STATE_RESET,
+	STATE_COMMAND,
+	STATE_TX,
+};
+
+struct irtoy {
+	struct device *dev;
+	struct usb_device *usbdev;
+
Quoted 8 lines

drivers/media/rc/ir_toy.c line 60

Quoted 5 lines
drivers/media/rc/ir_toy.c new file +5 -0
+	struct rc_dev *rc;
+	struct urb *urb_in, *urb_out;
+
+	u8 in[MAX_PACKET];
+	u8 out[MAX_PACKET];

This violates the DMA coherency rules. The buffers must be allocated separately with kmalloc().

Right, I'll fix this and send out a v3. There are other usb drivers in drivers/media/rc/.. that break this rule too.

Quoted 9 lines

drivers/media/rc/ir_toy.c line 128

Quoted 7 lines
drivers/media/rc/ir_toy.c new file +7 -0
+	case STATE_IRDATA: {
+		struct ir_raw_event rawir = { .pulse = irtoy->pulse };
+		__be16 *in = (__be16 *)irtoy->in;
+		int i;
+
+		for (i = 0; i < len / sizeof(__be16); i++) {
+			u32 v = be16_to_cpup(in + i);

Is this 16 or 32 bit?

It's 16 bit but I would like it up-cast so that v * UNIT_NS is a 32 bit multiply. This could do with a comment. Also could be be16_to_cpu(in[i]).

Quoted 16 lines
Quoted 16 lines
drivers/media/rc/ir_toy.c new file +15 -0
+
+			if (v == 0xffff) {
+				rawir.pulse = false;
+			} else {
+				rawir.duration = v * UNIT_NS;
+				ir_raw_event_store_with_timeout(irtoy->rc,
+								&rawir);
+			}
+
+			rawir.pulse = !rawir.pulse;
+		}
+
+		irtoy->pulse = rawir.pulse;
+
+		ir_raw_event_handle(irtoy->rc);
Quoted 8 lines

drivers/media/rc/ir_toy.c line 149

Quoted 6 lines
drivers/media/rc/ir_toy.c new file +6 -0
+		break;
+	}
+	case STATE_TX:
+		if (irtoy->tx_len == 0) {
+			if (len == LEN_XMITRES &&
+			    irtoy->in[0] == REPLY_XMITCOUNT) {

Endianness?

Single byte.

Quoted 5 lines

drivers/media/rc/ir_toy.c line 152

Quoted 3 lines
drivers/media/rc/ir_toy.c new file +3 -0
+				__be16 *emitted = (__be16 *)(irtoy->in + 1);
+
+				irtoy->emitted = be16_to_cpup(emitted);

Reason you are using cpup versions?

emitted is not aligned. So, I'm wrong and this should be get_unaligned_be16().

Quoted 8 lines

drivers/media/rc/ir_toy.c line 156

Quoted 5 lines
drivers/media/rc/ir_toy.c new file +4 -0
+			} else if (len == 1 &&
+				   irtoy->in[0] == REPLY_XMITSUCCESS) {
+				complete(&irtoy->rx_done);
+				irtoy->state = STATE_IRDATA;

Race condition. Whoever you wake up with that complete could read the old state.

irtoy->state is only read in this function, the urb callback handler. However, as you noticed, this is looks wrong so it should be re-ordered.

Quoted 43 lines
Quoted 8 lines
drivers/media/rc/ir_toy.c new file +8 -0
+			}
+		} else {
+			// send next part of tx buffer
+			uint max_send = irtoy->in[0];
+			uint buf_len = min(max_send, irtoy->tx_len);
+			int err;
+
+			dev_dbg(irtoy->dev, "ready to receive: 0x%02x\n",

drivers/media/rc/ir_toy.c line 174

Quoted 10 lines
drivers/media/rc/ir_toy.c new file +10 -0
+				max_send);
+
+			memcpy(irtoy->out, irtoy->tx_buf, buf_len);
+			irtoy->urb_out->transfer_buffer_length = buf_len;
+			err = usb_submit_urb(irtoy->urb_out, GFP_ATOMIC);
+			if (err != 0) {
+				dev_err(irtoy->dev, "fail to submit tx buf urb: %d\n",
+					err);
+				complete(&irtoy->rx_done);
+				irtoy->state = STATE_IRDATA;

Same race condition as above.

Quoted 22 lines
drivers/media/rc/ir_toy.c new file +22 -0
+			}
+
+			irtoy->tx_buf += buf_len;
+			irtoy->tx_len -= buf_len;
+			break;
+		}
+		break;
+	case STATE_RESET:
+		dev_err(irtoy->dev, "unexpected response to reset: %*phN\n",
+			len, irtoy->in);
+	}
+}
+
+static void irtoy_out_callback(struct urb *urb)
+{
+	struct irtoy *irtoy = urb->context;
+
+	switch (urb->status) {
+	case 0:
+		if (irtoy->state == STATE_RESET)
+			complete(&irtoy->rx_done);
+		break;
Quoted 8 lines

drivers/media/rc/ir_toy.c line 202

Quoted 6 lines
drivers/media/rc/ir_toy.c new file +6 -0
+
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+	case -EPROTO:
+		usb_unlink_urb(urb);

Redundant.

Removed, thanks.

Quoted 16 lines
Quoted 16 lines
drivers/media/rc/ir_toy.c new file +16 -0
+		return;
+
+	default:
+		dev_warn(irtoy->dev, "out urb status: %d\n", urb->status);
+	}
+}
+
+static void irtoy_in_callback(struct urb *urb)
+{
+	struct irtoy *irtoy = urb->context;
+	int ret;
+
+	switch (urb->status) {
+	case 0:
+		irtoy_response(irtoy, urb->actual_length);
+		break;
Quoted 8 lines

drivers/media/rc/ir_toy.c line 202

Quoted 6 lines
drivers/media/rc/ir_toy.c new file +6 -0
+
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+	case -EPROTO:
+		usb_unlink_urb(urb);

Redundant.

Removed, thanks.

Quoted 23 lines
Quoted 23 lines
drivers/media/rc/ir_toy.c new file +23 -0
+		return;
+
+	default:
+		dev_warn(irtoy->dev, "in urb status: %d\n", urb->status);
+	}
+
+	ret = usb_submit_urb(urb, GFP_ATOMIC);
+	if (ret && ret != -ENODEV)
+		dev_warn(irtoy->dev, "failed to resubmit urb: %d\n", ret);
+}
+
+static int irtoy_command(struct irtoy *irtoy, const u8 *cmd, int cmd_len)
+{
+	int err;
+
+	init_completion(&irtoy->rx_done);
+
+	memcpy(irtoy->out, cmd, cmd_len);
+	irtoy->urb_out->transfer_buffer_length = cmd_len;
+
+	err = usb_submit_urb(irtoy->urb_out, GFP_KERNEL);
+	if (err != 0)
+		return err;
Quoted 8 lines

drivers/media/rc/ir_toy.c line 251

Quoted 4 lines
drivers/media/rc/ir_toy.c new file +4 -0
+
+	if (!wait_for_completion_timeout(&irtoy->rx_done,
+					 msecs_to_jiffies(TIMEOUT)))
+		return -ETIMEDOUT;

Wrong error handling. The URB is still active. You cannot free the buffer. The caller has no idea when that is safe to do so. You must kill the URB in the timeout case.

Good point, fixed.

Quoted 13 lines
Quoted 13 lines
drivers/media/rc/ir_toy.c new file +13 -0
+
+	return 0;
+}
+
+/*
+ * When sending IR, it is imperative that we send the IR data as quickly
+ * as possible to the device, so it does not run out of IR data and
+ * introduce gaps. So, we feed the data from the urb callback handler
+ */
+static int irtoy_tx(struct rc_dev *rc, uint *txbuf, uint count)
+{
+	struct irtoy *irtoy = rc->priv;
+	unsigned int i, size;
Quoted 8 lines

drivers/media/rc/ir_toy.c line 269

Quoted 5 lines
drivers/media/rc/ir_toy.c new file +5 -0
+	__be16 *buf;
+	int err;
+
+	size = sizeof(u16) * (count + 1);
+	buf = kmalloc(size, GFP_KERNEL);

This is incompatible with the comment. If you are potentially in interrupt, you must use GFP_ATOMIC. Please clarify.

This function is called in process context, from a write() on a lirc chardev. However, this buffer is stored in irtoy->tx_buf. It is then sent, in packets, to the device (memcpy'ed to irtoy->out). This handled in irtoy_response() in a response to the device sending a "you may send X bytes irdata now" message.

I'll update the comment.

Quoted 9 lines

drivers/media/rc/ir_toy.c line 476

Quoted 6 lines
drivers/media/rc/ir_toy.c new file +6 -0
+static int irtoy_suspend(struct usb_interface *intf, pm_message_t message)
+{
+	struct irtoy *irtoy = usb_get_intfdata(intf);
+
+	usb_kill_urb(irtoy->urb_in);
+	usb_kill_urb(irtoy->urb_out);

That is brutal. It could fail commands. Do you really want to do that?

Commands can only be sent during 1) device probe and 2) ir transmit. During ir transmit we are non-interruptable process context, so we should not end up here unless I'm mistaken.

When we're not issuing commands we're waiting for ir receive; should that urb be killed for the duration of suspend?

Quoted 13 lines

drivers/media/rc/ir_toy.c line 486

Quoted 11 lines
drivers/media/rc/ir_toy.c new file +10 -0
+
+	return 0;
+}
+
+static int irtoy_resume(struct usb_interface *intf)
+{
+	struct irtoy *irtoy = usb_get_intfdata(intf);
+	int err;
+
+	err = usb_submit_urb(irtoy->urb_in, GFP_KERNEL);

That should technically be GFP_NOIO

Fixed.

Quoted 6 lines
Quoted 6 lines
drivers/media/rc/ir_toy.c new file +5 -0
+	if (err)
+		dev_warn(&intf->dev, "failed to submit urb: %d\n", err);
+
+	return err;
+}
maintainer
#5 lore raw

Re: [PATCH v2 1/3] media: rc: add support for Infrared Toy and IR Droid devices

Am Mittwoch, den 27.05.2020, 13:28 +0100 schrieb Sean Young:

Hi,

Quoted 5 lines
Quoted 2 lines

This violates the DMA coherency rules. The buffers must be allocated separately with kmalloc().

Right, I'll fix this and send out a v3. There are other usb drivers in drivers/media/rc/.. that break this rule too.

Unfortunately.

Quoted 10 lines
Quoted 9 lines

drivers/media/rc/ir_toy.c line 128

Quoted 7 lines
drivers/media/rc/ir_toy.c new file +7 -0
+	case STATE_IRDATA: {
+		struct ir_raw_event rawir = { .pulse = irtoy->pulse };
+		__be16 *in = (__be16 *)irtoy->in;
+		int i;
+
+		for (i = 0; i < len / sizeof(__be16); i++) {
+			u32 v = be16_to_cpup(in + i);

Is this 16 or 32 bit?

Quoted 2 lines

It's 16 bit but I would like it up-cast so that v * UNIT_NS is a 32 bit multiply. This could do with a comment. Also could be be16_to_cpu(in[i]).

That is nasty. I'd say if you need a larger size, cast explicitly.

Quoted 10 lines
Quoted 9 lines

drivers/media/rc/ir_toy.c line 476

Quoted 6 lines
drivers/media/rc/ir_toy.c new file +6 -0
+static int irtoy_suspend(struct usb_interface *intf, pm_message_t message)
+{
+	struct irtoy *irtoy = usb_get_intfdata(intf);
+
+	usb_kill_urb(irtoy->urb_in);
+	usb_kill_urb(irtoy->urb_out);

That is brutal. It could fail commands. Do you really want to do that?

Quoted 3 lines

Commands can only be sent during 1) device probe and 2) ir transmit. During ir transmit we are non-interruptable process context, so we should not end up here unless I'm mistaken.

Well. then the kill is redundant. The freezer will save you if the system sleeps. If somebody switches on runtime PM. you are potentially in trouble.

Quoted 2 lines

When we're not issuing commands we're waiting for ir receive; should that urb be killed for the duration of suspend?

No IO to a suspended device.

Regards
Oliver

author maintainer
#6 lore raw

Re: [PATCH v2 1/3] media: rc: add support for Infrared Toy and IR Droid devices

Hi,

Quoted 8 lines · Oliver Neukum

Am Mittwoch, den 27.05.2020, 13:28 +0100 schrieb Sean Young:

Quoted 5 lines
Quoted 2 lines

This violates the DMA coherency rules. The buffers must be allocated separately with kmalloc().

Right, I'll fix this and send out a v3. There are other usb drivers in drivers/media/rc/.. that break this rule too.

Unfortunately.

Yes, I'll go through them in the near future and makes fixes based on this thread -- thanks!

Sean