[PATCH] media: ir_toy: fix urb resubmission on transmit

From: YuanZhe Liu

Date: Fri Dec 04 2026 - 11:05:25 EST


The driver uses a single out urb for commands and tx data. A command is
completed by a response on the in endpoint, but that response does not
guarantee that the out urb has been given back by the host controller.
The in endpoint handler also submits tx chunks directly.

So when the device delays an out transfer, for example by NAKing it, the
driver can submit the out urb while it is still active. usb_submit_urb()
rejects this with a warning and -EBUSY; the transmission then fails and
the driver tries to reset an already confused device.

Check whether the out urb is still active before submitting a tx chunk,
and submit the chunk from the out completion if it is. Before starting a
command, drop any pending chunk and wait for the out urb to be released.

Fixes: 261463dbc34f ("media: rc: add support for Infrared Toy and IR Droid devices")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
This was found with syzkaller against a virtual USB IR Toy device. The
warning was observed on 7.3.5, and the code is unchanged in v7.4-rc5 and
media-committers next.

The change was compile-tested with CONFIG_IR_TOY=y only.

drivers/media/rc/ir_toy.c | 54 ++++++++++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 15 deletions(-)

diff --git a/drivers/media/rc/ir_toy.c b/drivers/media/rc/ir_toy.c
index 58b23d08a897..9633ae04317c 100644
--- a/drivers/media/rc/ir_toy.c
+++ b/drivers/media/rc/ir_toy.c
@@ -68,6 +68,7 @@ struct irtoy {

void *tx_buf;
uint tx_len;
+ uint tx_pending; /* chunk to send once urb_out is idle */

uint emitted;
uint hw_version;
@@ -77,6 +78,24 @@ struct irtoy {
char phys[64];
};

+static void irtoy_tx_submit(struct irtoy *irtoy, uint len)
+{
+ int err;
+
+ memcpy(irtoy->out, irtoy->tx_buf, len);
+ irtoy->urb_out->transfer_buffer_length = len;
+ err = usb_submit_urb(irtoy->urb_out, GFP_ATOMIC);
+ if (err) {
+ dev_err(irtoy->dev, "fail to submit tx buf urb: %d\n", err);
+ irtoy->state = STATE_IRDATA;
+ complete(&irtoy->command_done);
+ return;
+ }
+
+ irtoy->tx_buf += len;
+ irtoy->tx_len -= len;
+}
+
static void irtoy_response(struct irtoy *irtoy, u32 len)
{
switch (irtoy->state) {
@@ -165,7 +184,6 @@ static void irtoy_response(struct irtoy *irtoy, u32 len)
// send next part of tx buffer
uint space = irtoy->in[0];
uint buf_len;
- int err;

if (len != 1 || space > MAX_PACKET || space == 0) {
dev_dbg(irtoy->dev, "packet length expected: %*phN\n",
@@ -175,22 +193,13 @@ static void irtoy_response(struct irtoy *irtoy, u32 len)

buf_len = min(space, irtoy->tx_len);

- dev_dbg(irtoy->dev, "remaining:%u sending:%u\n",
- irtoy->tx_len, buf_len);
-
- 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);
- irtoy->state = STATE_IRDATA;
- complete(&irtoy->command_done);
+ /* urb_out may still be active; the out completion sends it */
+ if (irtoy->urb_out->hcpriv) {
+ irtoy->tx_pending = buf_len;
break;
}

- irtoy->tx_buf += buf_len;
- irtoy->tx_len -= buf_len;
+ irtoy_tx_submit(irtoy, buf_len);
}
break;
}
@@ -206,6 +215,13 @@ static void irtoy_out_callback(struct urb *urb)
} else {
dev_warn(irtoy->dev, "out urb status: %d\n", urb->status);
}
+
+ if (irtoy->tx_pending && irtoy->state == STATE_TX) {
+ uint len = irtoy->tx_pending;
+
+ irtoy->tx_pending = 0;
+ irtoy_tx_submit(irtoy, len);
+ }
}

static void irtoy_in_callback(struct urb *urb)
@@ -238,6 +254,11 @@ static int irtoy_command(struct irtoy *irtoy, const u8 *cmd, int cmd_len,
{
int err;

+ /* the in endpoint can complete the command before urb_out is idle */
+ irtoy->tx_pending = 0;
+ irtoy->state = STATE_IRDATA;
+ usb_kill_urb(irtoy->urb_out);
+
init_completion(&irtoy->command_done);

irtoy->state = state;
@@ -246,11 +267,14 @@ static int irtoy_command(struct irtoy *irtoy, const u8 *cmd, int cmd_len,
irtoy->urb_out->transfer_buffer_length = cmd_len;

err = usb_submit_urb(irtoy->urb_out, GFP_KERNEL);
- if (err != 0)
+ if (err != 0) {
+ irtoy->state = STATE_IRDATA;
return err;
+ }

if (!wait_for_completion_timeout(&irtoy->command_done,
msecs_to_jiffies(TIMEOUT))) {
+ irtoy->state = STATE_IRDATA;
usb_kill_urb(irtoy->urb_out);
return -ETIMEDOUT;
}
--
2.34.1