xref: /linux/drivers/media/usb/dvb-usb/dvb-usb-urb.c (revision 569bc8d6)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2786baecfSMauro Carvalho Chehab /* dvb-usb-urb.c is part of the DVB USB library.
3786baecfSMauro Carvalho Chehab  *
499e44da7SPatrick Boettcher  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
5786baecfSMauro Carvalho Chehab  * see dvb-usb-init.c for copyright information.
6786baecfSMauro Carvalho Chehab  *
7786baecfSMauro Carvalho Chehab  * This file keeps functions for initializing and handling the
8786baecfSMauro Carvalho Chehab  * USB and URB stuff.
9786baecfSMauro Carvalho Chehab  */
10786baecfSMauro Carvalho Chehab #include "dvb-usb-common.h"
11786baecfSMauro Carvalho Chehab 
dvb_usb_generic_rw(struct dvb_usb_device * d,u8 * wbuf,u16 wlen,u8 * rbuf,u16 rlen,int delay_ms)12786baecfSMauro Carvalho Chehab int dvb_usb_generic_rw(struct dvb_usb_device *d, u8 *wbuf, u16 wlen, u8 *rbuf,
13786baecfSMauro Carvalho Chehab 	u16 rlen, int delay_ms)
14786baecfSMauro Carvalho Chehab {
15*569bc8d6SHans Verkuil 	int actlen = 0, ret = -ENOMEM;
16786baecfSMauro Carvalho Chehab 
17786baecfSMauro Carvalho Chehab 	if (!d || wbuf == NULL || wlen == 0)
18786baecfSMauro Carvalho Chehab 		return -EINVAL;
19786baecfSMauro Carvalho Chehab 
20786baecfSMauro Carvalho Chehab 	if (d->props.generic_bulk_ctrl_endpoint == 0) {
21786baecfSMauro Carvalho Chehab 		err("endpoint for generic control not specified.");
22786baecfSMauro Carvalho Chehab 		return -EINVAL;
23786baecfSMauro Carvalho Chehab 	}
24786baecfSMauro Carvalho Chehab 
25786baecfSMauro Carvalho Chehab 	if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
26786baecfSMauro Carvalho Chehab 		return ret;
27786baecfSMauro Carvalho Chehab 
28786baecfSMauro Carvalho Chehab 	deb_xfer(">>> ");
29786baecfSMauro Carvalho Chehab 	debug_dump(wbuf,wlen,deb_xfer);
30786baecfSMauro Carvalho Chehab 
31786baecfSMauro Carvalho Chehab 	ret = usb_bulk_msg(d->udev,usb_sndbulkpipe(d->udev,
32786baecfSMauro Carvalho Chehab 			d->props.generic_bulk_ctrl_endpoint), wbuf,wlen,&actlen,
33786baecfSMauro Carvalho Chehab 			2000);
34786baecfSMauro Carvalho Chehab 
35786baecfSMauro Carvalho Chehab 	if (ret)
36786baecfSMauro Carvalho Chehab 		err("bulk message failed: %d (%d/%d)",ret,wlen,actlen);
37786baecfSMauro Carvalho Chehab 	else
38786baecfSMauro Carvalho Chehab 		ret = actlen != wlen ? -1 : 0;
39786baecfSMauro Carvalho Chehab 
40786baecfSMauro Carvalho Chehab 	/* an answer is expected, and no error before */
41786baecfSMauro Carvalho Chehab 	if (!ret && rbuf && rlen) {
42786baecfSMauro Carvalho Chehab 		if (delay_ms)
43786baecfSMauro Carvalho Chehab 			msleep(delay_ms);
44786baecfSMauro Carvalho Chehab 
45786baecfSMauro Carvalho Chehab 		ret = usb_bulk_msg(d->udev,usb_rcvbulkpipe(d->udev,
46786baecfSMauro Carvalho Chehab 				d->props.generic_bulk_ctrl_endpoint_response ?
47786baecfSMauro Carvalho Chehab 				d->props.generic_bulk_ctrl_endpoint_response :
48786baecfSMauro Carvalho Chehab 				d->props.generic_bulk_ctrl_endpoint),rbuf,rlen,&actlen,
49786baecfSMauro Carvalho Chehab 				2000);
50786baecfSMauro Carvalho Chehab 
51786baecfSMauro Carvalho Chehab 		if (ret)
52786baecfSMauro Carvalho Chehab 			err("recv bulk message failed: %d",ret);
53786baecfSMauro Carvalho Chehab 		else {
54786baecfSMauro Carvalho Chehab 			deb_xfer("<<< ");
55786baecfSMauro Carvalho Chehab 			debug_dump(rbuf,actlen,deb_xfer);
56786baecfSMauro Carvalho Chehab 		}
57786baecfSMauro Carvalho Chehab 	}
58786baecfSMauro Carvalho Chehab 
59786baecfSMauro Carvalho Chehab 	mutex_unlock(&d->usb_mutex);
60786baecfSMauro Carvalho Chehab 	return ret;
61786baecfSMauro Carvalho Chehab }
62786baecfSMauro Carvalho Chehab EXPORT_SYMBOL(dvb_usb_generic_rw);
63786baecfSMauro Carvalho Chehab 
dvb_usb_generic_write(struct dvb_usb_device * d,u8 * buf,u16 len)64786baecfSMauro Carvalho Chehab int dvb_usb_generic_write(struct dvb_usb_device *d, u8 *buf, u16 len)
65786baecfSMauro Carvalho Chehab {
66786baecfSMauro Carvalho Chehab 	return dvb_usb_generic_rw(d,buf,len,NULL,0,0);
67786baecfSMauro Carvalho Chehab }
68786baecfSMauro Carvalho Chehab EXPORT_SYMBOL(dvb_usb_generic_write);
69786baecfSMauro Carvalho Chehab 
dvb_usb_data_complete(struct usb_data_stream * stream,u8 * buffer,size_t length)70786baecfSMauro Carvalho Chehab static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buffer, size_t length)
71786baecfSMauro Carvalho Chehab {
72786baecfSMauro Carvalho Chehab 	struct dvb_usb_adapter *adap = stream->user_priv;
73786baecfSMauro Carvalho Chehab 	if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
74786baecfSMauro Carvalho Chehab 		dvb_dmx_swfilter(&adap->demux, buffer, length);
75786baecfSMauro Carvalho Chehab }
76786baecfSMauro Carvalho Chehab 
dvb_usb_data_complete_204(struct usb_data_stream * stream,u8 * buffer,size_t length)77786baecfSMauro Carvalho Chehab static void dvb_usb_data_complete_204(struct usb_data_stream *stream, u8 *buffer, size_t length)
78786baecfSMauro Carvalho Chehab {
79786baecfSMauro Carvalho Chehab 	struct dvb_usb_adapter *adap = stream->user_priv;
80786baecfSMauro Carvalho Chehab 	if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
81786baecfSMauro Carvalho Chehab 		dvb_dmx_swfilter_204(&adap->demux, buffer, length);
82786baecfSMauro Carvalho Chehab }
83786baecfSMauro Carvalho Chehab 
dvb_usb_data_complete_raw(struct usb_data_stream * stream,u8 * buffer,size_t length)84786baecfSMauro Carvalho Chehab static void dvb_usb_data_complete_raw(struct usb_data_stream *stream,
85786baecfSMauro Carvalho Chehab 				      u8 *buffer, size_t length)
86786baecfSMauro Carvalho Chehab {
87786baecfSMauro Carvalho Chehab 	struct dvb_usb_adapter *adap = stream->user_priv;
88786baecfSMauro Carvalho Chehab 	if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
89786baecfSMauro Carvalho Chehab 		dvb_dmx_swfilter_raw(&adap->demux, buffer, length);
90786baecfSMauro Carvalho Chehab }
91786baecfSMauro Carvalho Chehab 
dvb_usb_adapter_stream_init(struct dvb_usb_adapter * adap)92786baecfSMauro Carvalho Chehab int dvb_usb_adapter_stream_init(struct dvb_usb_adapter *adap)
93786baecfSMauro Carvalho Chehab {
94786baecfSMauro Carvalho Chehab 	int i, ret = 0;
95786baecfSMauro Carvalho Chehab 	for (i = 0; i < adap->props.num_frontends; i++) {
96786baecfSMauro Carvalho Chehab 
97786baecfSMauro Carvalho Chehab 		adap->fe_adap[i].stream.udev      = adap->dev->udev;
98786baecfSMauro Carvalho Chehab 		if (adap->props.fe[i].caps & DVB_USB_ADAP_RECEIVES_204_BYTE_TS)
99786baecfSMauro Carvalho Chehab 			adap->fe_adap[i].stream.complete =
100786baecfSMauro Carvalho Chehab 				dvb_usb_data_complete_204;
101786baecfSMauro Carvalho Chehab 		else
102786baecfSMauro Carvalho Chehab 		if (adap->props.fe[i].caps & DVB_USB_ADAP_RECEIVES_RAW_PAYLOAD)
103786baecfSMauro Carvalho Chehab 			adap->fe_adap[i].stream.complete =
104786baecfSMauro Carvalho Chehab 				dvb_usb_data_complete_raw;
105786baecfSMauro Carvalho Chehab 		else
106786baecfSMauro Carvalho Chehab 		adap->fe_adap[i].stream.complete  = dvb_usb_data_complete;
107786baecfSMauro Carvalho Chehab 		adap->fe_adap[i].stream.user_priv = adap;
108786baecfSMauro Carvalho Chehab 		ret = usb_urb_init(&adap->fe_adap[i].stream,
109786baecfSMauro Carvalho Chehab 				   &adap->props.fe[i].stream);
110786baecfSMauro Carvalho Chehab 		if (ret < 0)
111786baecfSMauro Carvalho Chehab 			break;
112786baecfSMauro Carvalho Chehab 	}
113786baecfSMauro Carvalho Chehab 	return ret;
114786baecfSMauro Carvalho Chehab }
115786baecfSMauro Carvalho Chehab 
dvb_usb_adapter_stream_exit(struct dvb_usb_adapter * adap)116786baecfSMauro Carvalho Chehab int dvb_usb_adapter_stream_exit(struct dvb_usb_adapter *adap)
117786baecfSMauro Carvalho Chehab {
118786baecfSMauro Carvalho Chehab 	int i;
119786baecfSMauro Carvalho Chehab 	for (i = 0; i < adap->props.num_frontends; i++)
120786baecfSMauro Carvalho Chehab 		usb_urb_exit(&adap->fe_adap[i].stream);
121786baecfSMauro Carvalho Chehab 	return 0;
122786baecfSMauro Carvalho Chehab }
123