Skip to content

Commit

Permalink
USB: gadget: f_hid: avoid crashes and log spam
Browse files Browse the repository at this point in the history
Disconnecting and reconnecting the USB cable can lead to crashes and a
variety of kernel log spam. Try to fix or minimise both.

See: #3870

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
  • Loading branch information
pelwell authored and popcornmix committed Jan 27, 2021
1 parent 98637b2 commit a6e47d5
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions drivers/usb/gadget/function/f_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,

spin_lock_irqsave(&hidg->write_spinlock, flags);

if (!hidg->req) {
spin_unlock_irqrestore(&hidg->write_spinlock, flags);
return -ESHUTDOWN;
}

#define WRITE_COND (!hidg->write_pending)
try_again:
/* write queue */
Expand All @@ -358,7 +363,13 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
count = min_t(unsigned, count, hidg->report_length);

spin_unlock_irqrestore(&hidg->write_spinlock, flags);
status = copy_from_user(req->buf, buffer, count);
if (req) {
status = copy_from_user(req->buf, buffer, count);
} else {
ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
status = -ESHUTDOWN;
goto release_write_pending;
}

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

spin_unlock_irqrestore(&hidg->write_spinlock, flags);

if (!hidg->in_ep->enabled) {
ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
status = -ESHUTDOWN;
goto release_write_pending;
}
status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
if (status < 0) {
ERROR(hidg->func.config->cdev,
"usb_ep_queue error on int endpoint %zd\n", status);
goto release_write_pending;
} else {
status = count;
Expand Down

0 comments on commit a6e47d5

Please sign in to comment.