{
struct iguanair *ir = dev->priv;
uint8_t space, *payload;
- unsigned i, size, rc;
+ unsigned i, size, rc, bytes;
struct send_packet *packet;
mutex_lock(&ir->lock);
/* convert from us to carrier periods */
for (i = size = 0; i < count; i++) {
txbuf[i] = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
- size += (txbuf[i] + 126) / 127;
+ bytes = (txbuf[i] + 126) / 127;
+ if (size + bytes > ir->bufsize) {
+ count = i;
+ break;
+ }
+ size += bytes;
}
- packet = kmalloc(sizeof(*packet) + size, GFP_KERNEL);
- if (!packet) {
- rc = -ENOMEM;
+ if (count == 0) {
+ rc = -EINVAL;
goto out;
}
- if (size > ir->bufsize) {
- rc = -E2BIG;
+ packet = kmalloc(sizeof(*packet) + size, GFP_KERNEL);
+ if (!packet) {
+ rc = -ENOMEM;
goto out;
}
rc = iguanair_receiver(ir, false);
if (rc) {
dev_warn(ir->dev, "disable receiver before transmit failed\n");
- goto out;
+ goto out_kfree;
}
}
dev_warn(ir->dev, "re-enable receiver after transmit failed\n");
}
+out_kfree:
+ kfree(packet);
out:
mutex_unlock(&ir->lock);
- kfree(packet);
- return rc;
+ return rc ? rc : count;
}
static int iguanair_open(struct rc_dev *rdev)
ir = kzalloc(sizeof(*ir), GFP_KERNEL);
rc = rc_allocate_device();
if (!ir || !rc) {
- ret = ENOMEM;
+ ret = -ENOMEM;
goto out;
}
ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
if (!ir->buf_in || !ir->urb_in) {
- ret = ENOMEM;
+ ret = -ENOMEM;
goto out;
}