blob: 5bafeb6486beb3c7da3459e63a74ca0de6994957 [file] [log] [blame]
Antti Palosaari12042b02012-06-20 23:09:41 -03001/*
2 * DVB USB framework
Antti Palosaaric79b3392012-05-23 10:06:09 -03003 *
Patrick Boettcher99e44da2016-01-24 12:56:58 -02004 * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@posteo.de>
Antti Palosaari12042b02012-06-20 23:09:41 -03005 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
Antti Palosaaric79b3392012-05-23 10:06:09 -03006 *
Antti Palosaari12042b02012-06-20 23:09:41 -03007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Antti Palosaaric79b3392012-05-23 10:06:09 -030020 */
Antti Palosaari12042b02012-06-20 23:09:41 -030021
Antti Palosaaric79b3392012-05-23 10:06:09 -030022#include "dvb_usb_common.h"
23
Wei Yongjun13828c62013-03-21 23:17:18 -030024static int dvb_usb_v2_generic_io(struct dvb_usb_device *d,
Antti Palosaariacb05492013-02-26 13:01:48 -030025 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
Antti Palosaaric79b3392012-05-23 10:06:09 -030026{
Antti Palosaari1162c7b2012-06-20 20:27:42 -030027 int ret, actual_length;
Antti Palosaaric79b3392012-05-23 10:06:09 -030028
Antti Palosaari206ace22014-08-21 11:19:13 -030029 if (!wbuf || !wlen || !d->props->generic_bulk_ctrl_endpoint ||
Antti Palosaari1162c7b2012-06-20 20:27:42 -030030 !d->props->generic_bulk_ctrl_endpoint_response) {
Antti Palosaarid10d1b92012-06-26 22:44:00 -030031 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, -EINVAL);
Antti Palosaaric79b3392012-05-23 10:06:09 -030032 return -EINVAL;
33 }
34
Antti Palosaari65d9bc92012-08-07 18:56:36 -030035 dev_dbg(&d->udev->dev, "%s: >>> %*ph\n", __func__, wlen, wbuf);
36
Antti Palosaari4e60d952012-05-24 14:44:21 -030037 ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev,
Antti Palosaarif093c382012-06-12 16:25:01 -030038 d->props->generic_bulk_ctrl_endpoint), wbuf, wlen,
Antti Palosaari1162c7b2012-06-20 20:27:42 -030039 &actual_length, 2000);
40 if (ret < 0)
Antti Palosaarid10d1b92012-06-26 22:44:00 -030041 dev_err(&d->udev->dev, "%s: usb_bulk_msg() failed=%d\n",
42 KBUILD_MODNAME, ret);
Antti Palosaaric79b3392012-05-23 10:06:09 -030043 else
Antti Palosaari1162c7b2012-06-20 20:27:42 -030044 ret = actual_length != wlen ? -EIO : 0;
Antti Palosaaric79b3392012-05-23 10:06:09 -030045
46 /* an answer is expected, and no error before */
47 if (!ret && rbuf && rlen) {
Antti Palosaari1162c7b2012-06-20 20:27:42 -030048 if (d->props->generic_bulk_ctrl_delay)
49 usleep_range(d->props->generic_bulk_ctrl_delay,
50 d->props->generic_bulk_ctrl_delay
51 + 20000);
Antti Palosaaric79b3392012-05-23 10:06:09 -030052
Antti Palosaari4e60d952012-05-24 14:44:21 -030053 ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
Antti Palosaari1162c7b2012-06-20 20:27:42 -030054 d->props->generic_bulk_ctrl_endpoint_response),
55 rbuf, rlen, &actual_length, 2000);
Antti Palosaaric79b3392012-05-23 10:06:09 -030056 if (ret)
Antti Palosaari6d7cfec2013-03-09 11:36:35 -030057 dev_err(&d->udev->dev,
58 "%s: 2nd usb_bulk_msg() failed=%d\n",
59 KBUILD_MODNAME, ret);
Antti Palosaari1162c7b2012-06-20 20:27:42 -030060
Antti Palosaari65d9bc92012-08-07 18:56:36 -030061 dev_dbg(&d->udev->dev, "%s: <<< %*ph\n", __func__,
62 actual_length, rbuf);
Antti Palosaaric79b3392012-05-23 10:06:09 -030063 }
64
Antti Palosaariacb05492013-02-26 13:01:48 -030065 return ret;
66}
67
68int dvb_usbv2_generic_rw(struct dvb_usb_device *d,
69 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
70{
71 int ret;
72
73 mutex_lock(&d->usb_mutex);
74 ret = dvb_usb_v2_generic_io(d, wbuf, wlen, rbuf, rlen);
Antti Palosaaric79b3392012-05-23 10:06:09 -030075 mutex_unlock(&d->usb_mutex);
Antti Palosaariacb05492013-02-26 13:01:48 -030076
Antti Palosaaric79b3392012-05-23 10:06:09 -030077 return ret;
78}
79EXPORT_SYMBOL(dvb_usbv2_generic_rw);
80
81int dvb_usbv2_generic_write(struct dvb_usb_device *d, u8 *buf, u16 len)
82{
Antti Palosaariacb05492013-02-26 13:01:48 -030083 int ret;
84
85 mutex_lock(&d->usb_mutex);
86 ret = dvb_usb_v2_generic_io(d, buf, len, NULL, 0);
87 mutex_unlock(&d->usb_mutex);
88
89 return ret;
Antti Palosaaric79b3392012-05-23 10:06:09 -030090}
91EXPORT_SYMBOL(dvb_usbv2_generic_write);
Antti Palosaariacb05492013-02-26 13:01:48 -030092
93int dvb_usbv2_generic_rw_locked(struct dvb_usb_device *d,
94 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
95{
96 return dvb_usb_v2_generic_io(d, wbuf, wlen, rbuf, rlen);
97}
98EXPORT_SYMBOL(dvb_usbv2_generic_rw_locked);
99
100int dvb_usbv2_generic_write_locked(struct dvb_usb_device *d, u8 *buf, u16 len)
101{
102 return dvb_usb_v2_generic_io(d, buf, len, NULL, 0);
103}
104EXPORT_SYMBOL(dvb_usbv2_generic_write_locked);