18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * DVB USB framework
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.de>
68c2ecf20Sopenharmony_ci * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "dvb_usb_common.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic int dvb_usb_v2_generic_io(struct dvb_usb_device *d,
128c2ecf20Sopenharmony_ci		u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	int ret, actual_length;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	if (!wbuf || !wlen || !d->props->generic_bulk_ctrl_endpoint ||
178c2ecf20Sopenharmony_ci			!d->props->generic_bulk_ctrl_endpoint_response) {
188c2ecf20Sopenharmony_ci		dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, -EINVAL);
198c2ecf20Sopenharmony_ci		return -EINVAL;
208c2ecf20Sopenharmony_ci	}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	dev_dbg(&d->udev->dev, "%s: >>> %*ph\n", __func__, wlen, wbuf);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev,
258c2ecf20Sopenharmony_ci			d->props->generic_bulk_ctrl_endpoint), wbuf, wlen,
268c2ecf20Sopenharmony_ci			&actual_length, 2000);
278c2ecf20Sopenharmony_ci	if (ret) {
288c2ecf20Sopenharmony_ci		dev_err(&d->udev->dev, "%s: usb_bulk_msg() failed=%d\n",
298c2ecf20Sopenharmony_ci				KBUILD_MODNAME, ret);
308c2ecf20Sopenharmony_ci		return ret;
318c2ecf20Sopenharmony_ci	}
328c2ecf20Sopenharmony_ci	if (actual_length != wlen) {
338c2ecf20Sopenharmony_ci		dev_err(&d->udev->dev, "%s: usb_bulk_msg() write length=%d, actual=%d\n",
348c2ecf20Sopenharmony_ci			KBUILD_MODNAME, wlen, actual_length);
358c2ecf20Sopenharmony_ci		return -EIO;
368c2ecf20Sopenharmony_ci	}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* an answer is expected */
398c2ecf20Sopenharmony_ci	if (rbuf && rlen) {
408c2ecf20Sopenharmony_ci		if (d->props->generic_bulk_ctrl_delay)
418c2ecf20Sopenharmony_ci			usleep_range(d->props->generic_bulk_ctrl_delay,
428c2ecf20Sopenharmony_ci					d->props->generic_bulk_ctrl_delay
438c2ecf20Sopenharmony_ci					+ 20000);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci		ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
468c2ecf20Sopenharmony_ci				d->props->generic_bulk_ctrl_endpoint_response),
478c2ecf20Sopenharmony_ci				rbuf, rlen, &actual_length, 2000);
488c2ecf20Sopenharmony_ci		if (ret)
498c2ecf20Sopenharmony_ci			dev_err(&d->udev->dev,
508c2ecf20Sopenharmony_ci					"%s: 2nd usb_bulk_msg() failed=%d\n",
518c2ecf20Sopenharmony_ci					KBUILD_MODNAME, ret);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci		dev_dbg(&d->udev->dev, "%s: <<< %*ph\n", __func__,
548c2ecf20Sopenharmony_ci				actual_length, rbuf);
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return ret;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciint dvb_usbv2_generic_rw(struct dvb_usb_device *d,
618c2ecf20Sopenharmony_ci		u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	int ret;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	mutex_lock(&d->usb_mutex);
668c2ecf20Sopenharmony_ci	ret = dvb_usb_v2_generic_io(d, wbuf, wlen, rbuf, rlen);
678c2ecf20Sopenharmony_ci	mutex_unlock(&d->usb_mutex);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	return ret;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dvb_usbv2_generic_rw);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciint dvb_usbv2_generic_write(struct dvb_usb_device *d, u8 *buf, u16 len)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	int ret;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	mutex_lock(&d->usb_mutex);
788c2ecf20Sopenharmony_ci	ret = dvb_usb_v2_generic_io(d, buf, len, NULL, 0);
798c2ecf20Sopenharmony_ci	mutex_unlock(&d->usb_mutex);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return ret;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dvb_usbv2_generic_write);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciint dvb_usbv2_generic_rw_locked(struct dvb_usb_device *d,
868c2ecf20Sopenharmony_ci		u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return dvb_usb_v2_generic_io(d, wbuf, wlen, rbuf, rlen);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dvb_usbv2_generic_rw_locked);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciint dvb_usbv2_generic_write_locked(struct dvb_usb_device *d, u8 *buf, u16 len)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	return dvb_usb_v2_generic_io(d, buf, len, NULL, 0);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dvb_usbv2_generic_write_locked);
97