1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * DVB USB framework 4 * 5 * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.de> 6 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> 7 */ 8 9 #include "dvb_usb_common.h" 10 11 static int dvb_usb_v2_generic_io(struct dvb_usb_device *d, 12 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen) 13 { 14 int ret, actual_length; 15 16 if (!wbuf || !wlen || !d->props->generic_bulk_ctrl_endpoint || 17 !d->props->generic_bulk_ctrl_endpoint_response) { 18 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, -EINVAL); 19 return -EINVAL; 20 } 21 22 dev_dbg(&d->udev->dev, "%s: >>> %*ph\n", __func__, wlen, wbuf); 23 24 ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev, 25 d->props->generic_bulk_ctrl_endpoint), wbuf, wlen, 26 &actual_length, 2000); 27 if (ret < 0) 28 dev_err(&d->udev->dev, "%s: usb_bulk_msg() failed=%d\n", 29 KBUILD_MODNAME, ret); 30 else 31 ret = actual_length != wlen ? -EIO : 0; 32 33 /* an answer is expected, and no error before */ 34 if (!ret && rbuf && rlen) { 35 if (d->props->generic_bulk_ctrl_delay) 36 usleep_range(d->props->generic_bulk_ctrl_delay, 37 d->props->generic_bulk_ctrl_delay 38 + 20000); 39 40 ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev, 41 d->props->generic_bulk_ctrl_endpoint_response), 42 rbuf, rlen, &actual_length, 2000); 43 if (ret) 44 dev_err(&d->udev->dev, 45 "%s: 2nd usb_bulk_msg() failed=%d\n", 46 KBUILD_MODNAME, ret); 47 48 dev_dbg(&d->udev->dev, "%s: <<< %*ph\n", __func__, 49 actual_length, rbuf); 50 } 51 52 return ret; 53 } 54 55 int dvb_usbv2_generic_rw(struct dvb_usb_device *d, 56 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen) 57 { 58 int ret; 59 60 mutex_lock(&d->usb_mutex); 61 ret = dvb_usb_v2_generic_io(d, wbuf, wlen, rbuf, rlen); 62 mutex_unlock(&d->usb_mutex); 63 64 return ret; 65 } 66 EXPORT_SYMBOL(dvb_usbv2_generic_rw); 67 68 int dvb_usbv2_generic_write(struct dvb_usb_device *d, u8 *buf, u16 len) 69 { 70 int ret; 71 72 mutex_lock(&d->usb_mutex); 73 ret = dvb_usb_v2_generic_io(d, buf, len, NULL, 0); 74 mutex_unlock(&d->usb_mutex); 75 76 return ret; 77 } 78 EXPORT_SYMBOL(dvb_usbv2_generic_write); 79 80 int dvb_usbv2_generic_rw_locked(struct dvb_usb_device *d, 81 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen) 82 { 83 return dvb_usb_v2_generic_io(d, wbuf, wlen, rbuf, rlen); 84 } 85 EXPORT_SYMBOL(dvb_usbv2_generic_rw_locked); 86 87 int dvb_usbv2_generic_write_locked(struct dvb_usb_device *d, u8 *buf, u16 len) 88 { 89 return dvb_usb_v2_generic_io(d, buf, len, NULL, 0); 90 } 91 EXPORT_SYMBOL(dvb_usbv2_generic_write_locked); 92