1 // SPDX-License-Identifier: GPL-2.0
2 /* usb-urb.c is part of the DVB USB library.
3  *
4  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
5  * see dvb-usb-init.c for copyright information.
6  *
7  * This file keeps functions for initializing and handling the
8  * BULK and ISOC USB data transfers in a generic way.
9  * Can be used for DVB-only and also, that's the plan, for
10  * Hybrid USB devices (analog and DVB).
11  */
12 #include "dvb_usb_common.h"
13 
14 /* URB stuff for streaming */
15 
16 int usb_urb_reconfig(struct usb_data_stream *stream,
17 		struct usb_data_stream_properties *props);
18 
19 static void usb_urb_complete(struct urb *urb)
20 {
21 	struct usb_data_stream *stream = urb->context;
22 	int ptype = usb_pipetype(urb->pipe);
23 	int i;
24 	u8 *b;
25 
26 	dev_dbg_ratelimited(&stream->udev->dev,
27 			"%s: %s urb completed status=%d length=%d/%d pack_num=%d errors=%d\n",
28 			__func__, ptype == PIPE_ISOCHRONOUS ? "isoc" : "bulk",
29 			urb->status, urb->actual_length,
30 			urb->transfer_buffer_length,
31 			urb->number_of_packets, urb->error_count);
32 
33 	switch (urb->status) {
34 	case 0:         /* success */
35 	case -ETIMEDOUT:    /* NAK */
36 		break;
37 	case -ECONNRESET:   /* kill */
38 	case -ENOENT:
39 	case -ESHUTDOWN:
40 		return;
41 	default:        /* error */
42 		dev_dbg_ratelimited(&stream->udev->dev,
43 				"%s: urb completion failed=%d\n",
44 				__func__, urb->status);
45 		break;
46 	}
47 
48 	b = (u8 *) urb->transfer_buffer;
49 	switch (ptype) {
50 	case PIPE_ISOCHRONOUS:
51 		for (i = 0; i < urb->number_of_packets; i++) {
52 			if (urb->iso_frame_desc[i].status != 0)
53 				dev_dbg(&stream->udev->dev,
54 						"%s: iso frame descriptor has an error=%d\n",
55 						__func__,
56 						urb->iso_frame_desc[i].status);
57 			else if (urb->iso_frame_desc[i].actual_length > 0)
58 				stream->complete(stream,
59 					b + urb->iso_frame_desc[i].offset,
60 					urb->iso_frame_desc[i].actual_length);
61 
62 			urb->iso_frame_desc[i].status = 0;
63 			urb->iso_frame_desc[i].actual_length = 0;
64 		}
65 		break;
66 	case PIPE_BULK:
67 		if (urb->actual_length > 0)
68 			stream->complete(stream, b, urb->actual_length);
69 		break;
70 	default:
71 		dev_err(&stream->udev->dev,
72 				"%s: unknown endpoint type in completion handler\n",
73 				KBUILD_MODNAME);
74 		return;
75 	}
76 	usb_submit_urb(urb, GFP_ATOMIC);
77 }
78 
79 int usb_urb_killv2(struct usb_data_stream *stream)
80 {
81 	int i;
82 	for (i = 0; i < stream->urbs_submitted; i++) {
83 		dev_dbg(&stream->udev->dev, "%s: kill urb=%d\n", __func__, i);
84 		/* stop the URB */
85 		usb_kill_urb(stream->urb_list[i]);
86 	}
87 	stream->urbs_submitted = 0;
88 	return 0;
89 }
90 
91 int usb_urb_submitv2(struct usb_data_stream *stream,
92 		struct usb_data_stream_properties *props)
93 {
94 	int i, ret;
95 
96 	if (props) {
97 		ret = usb_urb_reconfig(stream, props);
98 		if (ret < 0)
99 			return ret;
100 	}
101 
102 	for (i = 0; i < stream->urbs_initialized; i++) {
103 		dev_dbg(&stream->udev->dev, "%s: submit urb=%d\n", __func__, i);
104 		ret = usb_submit_urb(stream->urb_list[i], GFP_ATOMIC);
105 		if (ret) {
106 			dev_err(&stream->udev->dev,
107 					"%s: could not submit urb no. %d - get them all back\n",
108 					KBUILD_MODNAME, i);
109 			usb_urb_killv2(stream);
110 			return ret;
111 		}
112 		stream->urbs_submitted++;
113 	}
114 	return 0;
115 }
116 
117 static int usb_urb_free_urbs(struct usb_data_stream *stream)
118 {
119 	int i;
120 
121 	usb_urb_killv2(stream);
122 
123 	for (i = stream->urbs_initialized - 1; i >= 0; i--) {
124 		if (stream->urb_list[i]) {
125 			dev_dbg(&stream->udev->dev, "%s: free urb=%d\n",
126 					__func__, i);
127 			/* free the URBs */
128 			usb_free_urb(stream->urb_list[i]);
129 		}
130 	}
131 	stream->urbs_initialized = 0;
132 
133 	return 0;
134 }
135 
136 static int usb_urb_alloc_bulk_urbs(struct usb_data_stream *stream)
137 {
138 	int i, j;
139 
140 	/* allocate the URBs */
141 	for (i = 0; i < stream->props.count; i++) {
142 		dev_dbg(&stream->udev->dev, "%s: alloc urb=%d\n", __func__, i);
143 		stream->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
144 		if (!stream->urb_list[i]) {
145 			dev_dbg(&stream->udev->dev, "%s: failed\n", __func__);
146 			for (j = 0; j < i; j++)
147 				usb_free_urb(stream->urb_list[j]);
148 			return -ENOMEM;
149 		}
150 		usb_fill_bulk_urb(stream->urb_list[i],
151 				stream->udev,
152 				usb_rcvbulkpipe(stream->udev,
153 						stream->props.endpoint),
154 				stream->buf_list[i],
155 				stream->props.u.bulk.buffersize,
156 				usb_urb_complete, stream);
157 
158 		stream->urb_list[i]->transfer_flags = URB_FREE_BUFFER;
159 		stream->urbs_initialized++;
160 	}
161 	return 0;
162 }
163 
164 static int usb_urb_alloc_isoc_urbs(struct usb_data_stream *stream)
165 {
166 	int i, j;
167 
168 	/* allocate the URBs */
169 	for (i = 0; i < stream->props.count; i++) {
170 		struct urb *urb;
171 		int frame_offset = 0;
172 		dev_dbg(&stream->udev->dev, "%s: alloc urb=%d\n", __func__, i);
173 		stream->urb_list[i] = usb_alloc_urb(
174 				stream->props.u.isoc.framesperurb, GFP_ATOMIC);
175 		if (!stream->urb_list[i]) {
176 			dev_dbg(&stream->udev->dev, "%s: failed\n", __func__);
177 			for (j = 0; j < i; j++)
178 				usb_free_urb(stream->urb_list[j]);
179 			return -ENOMEM;
180 		}
181 
182 		urb = stream->urb_list[i];
183 
184 		urb->dev = stream->udev;
185 		urb->context = stream;
186 		urb->complete = usb_urb_complete;
187 		urb->pipe = usb_rcvisocpipe(stream->udev,
188 				stream->props.endpoint);
189 		urb->transfer_flags = URB_ISO_ASAP | URB_FREE_BUFFER;
190 		urb->interval = stream->props.u.isoc.interval;
191 		urb->number_of_packets = stream->props.u.isoc.framesperurb;
192 		urb->transfer_buffer_length = stream->props.u.isoc.framesize *
193 				stream->props.u.isoc.framesperurb;
194 		urb->transfer_buffer = stream->buf_list[i];
195 
196 		for (j = 0; j < stream->props.u.isoc.framesperurb; j++) {
197 			urb->iso_frame_desc[j].offset = frame_offset;
198 			urb->iso_frame_desc[j].length =
199 					stream->props.u.isoc.framesize;
200 			frame_offset += stream->props.u.isoc.framesize;
201 		}
202 
203 		stream->urbs_initialized++;
204 	}
205 	return 0;
206 }
207 
208 static int usb_free_stream_buffers(struct usb_data_stream *stream)
209 {
210 	if (stream->state & USB_STATE_URB_BUF) {
211 		while (stream->buf_num) {
212 			stream->buf_num--;
213 			stream->buf_list[stream->buf_num] = NULL;
214 		}
215 	}
216 
217 	stream->state &= ~USB_STATE_URB_BUF;
218 
219 	return 0;
220 }
221 
222 static int usb_alloc_stream_buffers(struct usb_data_stream *stream, int num,
223 				    unsigned long size)
224 {
225 	stream->buf_num = 0;
226 	stream->buf_size = size;
227 
228 	dev_dbg(&stream->udev->dev,
229 			"%s: all in all I will use %lu bytes for streaming\n",
230 			__func__,  num * size);
231 
232 	for (stream->buf_num = 0; stream->buf_num < num; stream->buf_num++) {
233 		stream->buf_list[stream->buf_num] = kzalloc(size, GFP_ATOMIC);
234 		if (!stream->buf_list[stream->buf_num]) {
235 			dev_dbg(&stream->udev->dev, "%s: alloc buf=%d failed\n",
236 					__func__, stream->buf_num);
237 			usb_free_stream_buffers(stream);
238 			return -ENOMEM;
239 		}
240 
241 		dev_dbg(&stream->udev->dev, "%s: alloc buf=%d %p (dma %llu)\n",
242 				__func__, stream->buf_num,
243 				stream->buf_list[stream->buf_num],
244 				(long long)stream->dma_addr[stream->buf_num]);
245 		stream->state |= USB_STATE_URB_BUF;
246 	}
247 
248 	return 0;
249 }
250 
251 int usb_urb_reconfig(struct usb_data_stream *stream,
252 		struct usb_data_stream_properties *props)
253 {
254 	int buf_size;
255 
256 	if (!props)
257 		return 0;
258 
259 	/* check allocated buffers are large enough for the request */
260 	if (props->type == USB_BULK) {
261 		buf_size = stream->props.u.bulk.buffersize;
262 	} else if (props->type == USB_ISOC) {
263 		buf_size = props->u.isoc.framesize * props->u.isoc.framesperurb;
264 	} else {
265 		dev_err(&stream->udev->dev, "%s: invalid endpoint type=%d\n",
266 				KBUILD_MODNAME, props->type);
267 		return -EINVAL;
268 	}
269 
270 	if (stream->buf_num < props->count || stream->buf_size < buf_size) {
271 		dev_err(&stream->udev->dev,
272 				"%s: cannot reconfigure as allocated buffers are too small\n",
273 				KBUILD_MODNAME);
274 		return -EINVAL;
275 	}
276 
277 	/* check if all fields are same */
278 	if (stream->props.type == props->type &&
279 			stream->props.count == props->count &&
280 			stream->props.endpoint == props->endpoint) {
281 		if (props->type == USB_BULK &&
282 				props->u.bulk.buffersize ==
283 				stream->props.u.bulk.buffersize)
284 			return 0;
285 		else if (props->type == USB_ISOC &&
286 				props->u.isoc.framesperurb ==
287 				stream->props.u.isoc.framesperurb &&
288 				props->u.isoc.framesize ==
289 				stream->props.u.isoc.framesize &&
290 				props->u.isoc.interval ==
291 				stream->props.u.isoc.interval)
292 			return 0;
293 	}
294 
295 	dev_dbg(&stream->udev->dev, "%s: re-alloc urbs\n", __func__);
296 
297 	usb_urb_free_urbs(stream);
298 	memcpy(&stream->props, props, sizeof(*props));
299 	if (props->type == USB_BULK)
300 		return usb_urb_alloc_bulk_urbs(stream);
301 	else if (props->type == USB_ISOC)
302 		return usb_urb_alloc_isoc_urbs(stream);
303 
304 	return 0;
305 }
306 
307 int usb_urb_initv2(struct usb_data_stream *stream,
308 		const struct usb_data_stream_properties *props)
309 {
310 	int ret;
311 
312 	if (!stream || !props)
313 		return -EINVAL;
314 
315 	memcpy(&stream->props, props, sizeof(*props));
316 
317 	if (!stream->complete) {
318 		dev_err(&stream->udev->dev,
319 				"%s: there is no data callback - this doesn't make sense\n",
320 				KBUILD_MODNAME);
321 		return -EINVAL;
322 	}
323 
324 	switch (stream->props.type) {
325 	case USB_BULK:
326 		ret = usb_alloc_stream_buffers(stream, stream->props.count,
327 				stream->props.u.bulk.buffersize);
328 		if (ret < 0)
329 			return ret;
330 
331 		return usb_urb_alloc_bulk_urbs(stream);
332 	case USB_ISOC:
333 		ret = usb_alloc_stream_buffers(stream, stream->props.count,
334 				stream->props.u.isoc.framesize *
335 				stream->props.u.isoc.framesperurb);
336 		if (ret < 0)
337 			return ret;
338 
339 		return usb_urb_alloc_isoc_urbs(stream);
340 	default:
341 		dev_err(&stream->udev->dev,
342 				"%s: unknown urb-type for data transfer\n",
343 				KBUILD_MODNAME);
344 		return -EINVAL;
345 	}
346 }
347 
348 int usb_urb_exitv2(struct usb_data_stream *stream)
349 {
350 	usb_urb_free_urbs(stream);
351 	usb_free_stream_buffers(stream);
352 
353 	return 0;
354 }
355