diff --git a/drivers/media/usb/dvb-usb-v2/az6007.c b/drivers/media/usb/dvb-usb-v2/az6007.c index 65ef045b74ca..86ee62fd5f4f 100644 --- a/drivers/media/usb/dvb-usb-v2/az6007.c +++ b/drivers/media/usb/dvb-usb-v2/az6007.c @@ -97,19 +97,30 @@ static struct mt2063_config az6007_mt2063_config = { .refclock = 36125000, }; -static int __az6007_read(struct usb_device *udev, u8 req, u16 value, - u16 index, u8 *b, int blen) +static int __az6007_read(struct dvb_usb_device *d, struct az6007_device_state *st, + u8 req, u16 value, u16 index, u8 *b, int blen) { int ret; - ret = usb_control_msg(udev, - usb_rcvctrlpipe(udev, 0), + if (mutex_lock_interruptible(&d->usb_mutex) < 0) + return -EAGAIN; + + if (blen > sizeof(st->data)) { + pr_err("az6007: tried to read %d bytes, but I2C max size is %lu bytes\n", + blen, sizeof(st->data)); + ret = -EOPNOTSUPP; + goto end_unlock; + } + + ret = usb_control_msg(d->udev, + usb_rcvctrlpipe(d->udev, 0), req, USB_TYPE_VENDOR | USB_DIR_IN, value, index, b, blen, 5000); if (ret < 0) { pr_warn("usb read operation failed. (%d)\n", ret); - return -EIO; + ret = -EIO; + goto end_unlock; } if (az6007_xfer_debug) { @@ -119,30 +130,42 @@ static int __az6007_read(struct usb_device *udev, u8 req, u16 value, DUMP_PREFIX_NONE, b, blen); } +end_unlock: + mutex_unlock(&d->usb_mutex); return ret; } static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen) { - struct az6007_device_state *st = d->priv; + struct az6007_device_state *st = d_to_priv(d); int ret; if (mutex_lock_interruptible(&st->mutex) < 0) return -EAGAIN; - ret = __az6007_read(d->udev, req, value, index, b, blen); + ret = __az6007_read(d, st, req, value, index, b, blen); mutex_unlock(&st->mutex); return ret; } -static int __az6007_write(struct usb_device *udev, u8 req, u16 value, - u16 index, u8 *b, int blen) +static int __az6007_write(struct dvb_usb_device *d, struct az6007_device_state *st, + u8 req, u16 value, u16 index, u8 *b, int blen) { int ret; + if (mutex_lock_interruptible(&d->usb_mutex) < 0) + return -EAGAIN; + + if (blen > sizeof(st->data) - 1) { + pr_err("az6007: tried to write %d bytes, but I2C max size is %lu bytes\n", + blen, sizeof(st->data)); + ret = -EOPNOTSUPP; + goto end_unlock; + } + if (az6007_xfer_debug) { printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n", req, value, index); @@ -150,35 +173,33 @@ static int __az6007_write(struct usb_device *udev, u8 req, u16 value, DUMP_PREFIX_NONE, b, blen); } - if (blen > 64) { - pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n", - blen); - return -EOPNOTSUPP; - } - - ret = usb_control_msg(udev, - usb_sndctrlpipe(udev, 0), + ret = usb_control_msg(d->udev, + usb_sndctrlpipe(d->udev, 0), req, USB_TYPE_VENDOR | USB_DIR_OUT, value, index, b, blen, 5000); if (ret != blen) { pr_err("usb write operation failed. (%d)\n", ret); - return -EIO; + ret = -EIO; + goto end_unlock; } - return 0; + ret = 0; +end_unlock: + mutex_unlock(&d->usb_mutex); + return ret; } static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen) { - struct az6007_device_state *st = d->priv; + struct az6007_device_state *st = d_to_priv(d); int ret; if (mutex_lock_interruptible(&st->mutex) < 0) return -EAGAIN; - ret = __az6007_write(d->udev, req, value, index, b, blen); + ret = __az6007_write(d, st, req, value, index, b, blen); mutex_unlock(&st->mutex); @@ -752,8 +773,12 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int length; u8 req, addr; - if (mutex_lock_interruptible(&st->mutex) < 0) + if (mutex_lock_interruptible(&d->i2c_mutex) < 0) + return -EAGAIN; + if (mutex_lock_interruptible(&st->mutex) < 0) { + mutex_unlock(&d->i2c_mutex); return -EAGAIN; + } for (i = 0; i < num; i++) { addr = msgs[i].addr << 1; @@ -775,13 +800,12 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], value = addr | (1 << 8); length = 6 + msgs[i + 1].len; len = msgs[i + 1].len; - ret = __az6007_read(d->udev, req, value, index, + ret = __az6007_read(d, st, req, value, index, st->data, length); - if (ret >= len) { + if (ret >= len + 5) { for (j = 0; j < len; j++) msgs[i + 1].buf[j] = st->data[j + 5]; - } else - ret = -EIO; + } i++; } else if (!(msgs[i].flags & I2C_M_RD)) { /* write bytes */ @@ -797,10 +821,8 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], value = addr | (1 << 8); length = msgs[i].len - 1; len = msgs[i].len - 1; - for (j = 0; j < len; j++) - st->data[j] = msgs[i].buf[j + 1]; - ret = __az6007_write(d->udev, req, value, index, - st->data, length); + ret = __az6007_write(d, st, req, value, index, + &msgs[i].buf[1], length); } else { /* read bytes */ if (az6007_xfer_debug) @@ -815,16 +837,19 @@ static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], value = addr; length = msgs[i].len + 6; len = msgs[i].len; - ret = __az6007_read(d->udev, req, value, index, + ret = __az6007_read(d, st, req, value, index, st->data, length); - for (j = 0; j < len; j++) - msgs[i].buf[j] = st->data[j + 5]; + if (ret >= len + 5) { + for (j = 0; j < len; j++) + msgs[i].buf[j] = st->data[j + 5]; + } } if (ret < 0) goto err; } err: mutex_unlock(&st->mutex); + mutex_unlock(&d->i2c_mutex); if (ret < 0) { pr_info("%s ERROR: %i\n", __func__, ret); @@ -845,6 +870,7 @@ static const struct i2c_algorithm az6007_i2c_algo = { static int az6007_identify_state(struct dvb_usb_device *d, const char **name) { + struct az6007_device_state *state = d_to_priv(d); int ret; u8 *mac; @@ -855,7 +881,7 @@ static int az6007_identify_state(struct dvb_usb_device *d, const char **name) return -ENOMEM; /* Try to read the mac address */ - ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6); + ret = __az6007_read(d, state, AZ6007_READ_DATA, 6, 0, mac, 6); if (ret == 6) ret = WARM; else @@ -864,9 +890,9 @@ static int az6007_identify_state(struct dvb_usb_device *d, const char **name) kfree(mac); if (ret == COLD) { - __az6007_write(d->udev, 0x09, 1, 0, NULL, 0); - __az6007_write(d->udev, 0x00, 0, 0, NULL, 0); - __az6007_write(d->udev, 0x00, 0, 0, NULL, 0); + __az6007_write(d, state, 0x09, 1, 0, NULL, 0); + __az6007_write(d, state, 0x00, 0, 0, NULL, 0); + __az6007_write(d, state, 0x00, 0, 0, NULL, 0); } pr_debug("Device is on %s state\n",