Skip to content

Commit a6e47d5

Browse files
pelwellpopcornmix
authored andcommitted
USB: gadget: f_hid: avoid crashes and log spam
Disconnecting and reconnecting the USB cable can lead to crashes and a variety of kernel log spam. Try to fix or minimise both. See: raspberrypi#3870 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
1 parent 98637b2 commit a6e47d5

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

‎drivers/usb/gadget/function/f_hid.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
338338

339339
spin_lock_irqsave(&hidg->write_spinlock, flags);
340340

341+
if (!hidg->req) {
342+
spin_unlock_irqrestore(&hidg->write_spinlock, flags);
343+
return -ESHUTDOWN;
344+
}
345+
341346
#define WRITE_COND (!hidg->write_pending)
342347
try_again:
343348
/* write queue */
@@ -358,7 +363,13 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
358363
count = min_t(unsigned, count, hidg->report_length);
359364

360365
spin_unlock_irqrestore(&hidg->write_spinlock, flags);
361-
status = copy_from_user(req->buf, buffer, count);
366+
if (req) {
367+
status = copy_from_user(req->buf, buffer, count);
368+
} else {
369+
ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
370+
status = -ESHUTDOWN;
371+
goto release_write_pending;
372+
}
362373

363374
if (status != 0) {
364375
ERROR(hidg->func.config->cdev,
@@ -387,10 +398,13 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
387398

388399
spin_unlock_irqrestore(&hidg->write_spinlock, flags);
389400

401+
if (!hidg->in_ep->enabled) {
402+
ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
403+
status = -ESHUTDOWN;
404+
goto release_write_pending;
405+
}
390406
status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
391407
if (status < 0) {
392-
ERROR(hidg->func.config->cdev,
393-
"usb_ep_queue error on int endpoint %zd\n", status);
394408
goto release_write_pending;
395409
} else {
396410
status = count;

0 commit comments

Comments
 (0)