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