blob: 0431beed0ef44dcfcfc7043d85fe514f602f1d10 [file] [log] [blame]
Antti Palosaari12042b02012-06-20 23:09:41 -03001/*
2 * DVB USB framework
Antti Palosaaric79b3392012-05-23 10:06:09 -03003 *
Antti Palosaari12042b02012-06-20 23:09:41 -03004 * Copyright (C) 2004-6 Patrick Boettcher <patrick.boettcher@desy.de>
5 * 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
24int dvb_usbv2_generic_rw(struct dvb_usb_device *d, u8 *wbuf, u16 wlen, u8 *rbuf,
Antti Palosaari1162c7b2012-06-20 20:27:42 -030025 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 Palosaari1162c7b2012-06-20 20:27:42 -030029 if (!d || !wbuf || !wlen || !d->props->generic_bulk_ctrl_endpoint ||
30 !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 Palosaari4e60d952012-05-24 14:44:21 -030035 ret = mutex_lock_interruptible(&d->usb_mutex);
Antti Palosaari1162c7b2012-06-20 20:27:42 -030036 if (ret < 0)
Antti Palosaaric79b3392012-05-23 10:06:09 -030037 return ret;
38
Antti Palosaari65d9bc92012-08-07 18:56:36 -030039 dev_dbg(&d->udev->dev, "%s: >>> %*ph\n", __func__, wlen, wbuf);
40
Antti Palosaari4e60d952012-05-24 14:44:21 -030041 ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev,
Antti Palosaarif093c382012-06-12 16:25:01 -030042 d->props->generic_bulk_ctrl_endpoint), wbuf, wlen,
Antti Palosaari1162c7b2012-06-20 20:27:42 -030043 &actual_length, 2000);
44 if (ret < 0)
Antti Palosaarid10d1b92012-06-26 22:44:00 -030045 dev_err(&d->udev->dev, "%s: usb_bulk_msg() failed=%d\n",
46 KBUILD_MODNAME, ret);
Antti Palosaaric79b3392012-05-23 10:06:09 -030047 else
Antti Palosaari1162c7b2012-06-20 20:27:42 -030048 ret = actual_length != wlen ? -EIO : 0;
Antti Palosaaric79b3392012-05-23 10:06:09 -030049
50 /* an answer is expected, and no error before */
51 if (!ret && rbuf && rlen) {
Antti Palosaari1162c7b2012-06-20 20:27:42 -030052 if (d->props->generic_bulk_ctrl_delay)
53 usleep_range(d->props->generic_bulk_ctrl_delay,
54 d->props->generic_bulk_ctrl_delay
55 + 20000);
Antti Palosaaric79b3392012-05-23 10:06:09 -030056
Antti Palosaari4e60d952012-05-24 14:44:21 -030057 ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
Antti Palosaari1162c7b2012-06-20 20:27:42 -030058 d->props->generic_bulk_ctrl_endpoint_response),
59 rbuf, rlen, &actual_length, 2000);
Antti Palosaaric79b3392012-05-23 10:06:09 -030060 if (ret)
Antti Palosaarid10d1b92012-06-26 22:44:00 -030061 dev_err(&d->udev->dev, "%s: 2nd usb_bulk_msg() " \
62 "failed=%d\n", KBUILD_MODNAME, ret);
Antti Palosaari1162c7b2012-06-20 20:27:42 -030063
Antti Palosaari65d9bc92012-08-07 18:56:36 -030064 dev_dbg(&d->udev->dev, "%s: <<< %*ph\n", __func__,
65 actual_length, rbuf);
Antti Palosaaric79b3392012-05-23 10:06:09 -030066 }
67
68 mutex_unlock(&d->usb_mutex);
69 return ret;
70}
71EXPORT_SYMBOL(dvb_usbv2_generic_rw);
72
73int dvb_usbv2_generic_write(struct dvb_usb_device *d, u8 *buf, u16 len)
74{
Antti Palosaari1162c7b2012-06-20 20:27:42 -030075 return dvb_usbv2_generic_rw(d, buf, len, NULL, 0);
Antti Palosaaric79b3392012-05-23 10:06:09 -030076}
77EXPORT_SYMBOL(dvb_usbv2_generic_write);