1 /* usb-urb.c is part of the DVB USB library.
3 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
4 * see dvb-usb-init.c for copyright information.
6 * This file keeps functions for initializing and handling the
7 * BULK and ISOC USB data transfers in a generic way.
8 * Can be used for DVB-only and also, that's the plan, for
9 * Hybrid USB devices (analog and DVB).
11 #include "dvb_usb_common.h"
13 /* URB stuff for streaming */
15 int usb_urb_reconfig(struct usb_data_stream *stream,
16 struct usb_data_stream_properties *props);
18 static void usb_urb_complete(struct urb *urb)
20 struct usb_data_stream *stream = urb->context;
21 int ptype = usb_pipetype(urb->pipe);
25 dev_dbg_ratelimited(&stream->udev->dev, "%s: %s urb completed " \
26 "status=%d length=%d/%d pack_num=%d errors=%d\n",
27 __func__, ptype == PIPE_ISOCHRONOUS ? "isoc" : "bulk",
28 urb->status, urb->actual_length,
29 urb->transfer_buffer_length,
30 urb->number_of_packets, urb->error_count);
32 switch (urb->status) {
34 case -ETIMEDOUT: /* NAK */
36 case -ECONNRESET: /* kill */
41 dev_dbg_ratelimited(&stream->udev->dev,
42 "%s: urb completition failed=%d\n",
43 __func__, urb->status);
47 b = (u8 *) urb->transfer_buffer;
49 case PIPE_ISOCHRONOUS:
50 for (i = 0; i < urb->number_of_packets; i++) {
51 if (urb->iso_frame_desc[i].status != 0)
52 dev_dbg(&stream->udev->dev, "%s: iso frame " \
53 "descriptor has an error=%d\n",
55 urb->iso_frame_desc[i].status);
56 else if (urb->iso_frame_desc[i].actual_length > 0)
57 stream->complete(stream,
58 b + urb->iso_frame_desc[i].offset,
59 urb->iso_frame_desc[i].actual_length);
61 urb->iso_frame_desc[i].status = 0;
62 urb->iso_frame_desc[i].actual_length = 0;
66 if (urb->actual_length > 0)
67 stream->complete(stream, b, urb->actual_length);
70 dev_err(&stream->udev->dev, "%s: unknown endpoint type in " \
71 "completition handler\n", KBUILD_MODNAME);
74 usb_submit_urb(urb, GFP_ATOMIC);
77 int usb_urb_killv2(struct usb_data_stream *stream)
80 for (i = 0; i < stream->urbs_submitted; i++) {
81 dev_dbg(&stream->udev->dev, "%s: kill urb=%d\n", __func__, i);
83 usb_kill_urb(stream->urb_list[i]);
85 stream->urbs_submitted = 0;
89 int usb_urb_submitv2(struct usb_data_stream *stream,
90 struct usb_data_stream_properties *props)
95 ret = usb_urb_reconfig(stream, props);
100 for (i = 0; i < stream->urbs_initialized; i++) {
101 dev_dbg(&stream->udev->dev, "%s: submit urb=%d\n", __func__, i);
102 ret = usb_submit_urb(stream->urb_list[i], GFP_ATOMIC);
104 dev_err(&stream->udev->dev, "%s: could not submit " \
105 "urb no. %d - get them all back\n",
107 usb_urb_killv2(stream);
110 stream->urbs_submitted++;
115 static int usb_urb_free_urbs(struct usb_data_stream *stream)
119 usb_urb_killv2(stream);
121 for (i = stream->urbs_initialized - 1; i >= 0; i--) {
122 if (stream->urb_list[i]) {
123 dev_dbg(&stream->udev->dev, "%s: free urb=%d\n",
126 usb_free_urb(stream->urb_list[i]);
129 stream->urbs_initialized = 0;
134 static int usb_urb_alloc_bulk_urbs(struct usb_data_stream *stream)
138 /* allocate the URBs */
139 for (i = 0; i < stream->props.count; i++) {
140 dev_dbg(&stream->udev->dev, "%s: alloc urb=%d\n", __func__, i);
141 stream->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
142 if (!stream->urb_list[i]) {
143 dev_dbg(&stream->udev->dev, "%s: failed\n", __func__);
144 for (j = 0; j < i; j++)
145 usb_free_urb(stream->urb_list[j]);
148 usb_fill_bulk_urb(stream->urb_list[i],
150 usb_rcvbulkpipe(stream->udev,
151 stream->props.endpoint),
153 stream->props.u.bulk.buffersize,
154 usb_urb_complete, stream);
156 stream->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
157 stream->urb_list[i]->transfer_dma = stream->dma_addr[i];
158 stream->urbs_initialized++;
163 static int usb_urb_alloc_isoc_urbs(struct usb_data_stream *stream)
167 /* allocate the URBs */
168 for (i = 0; i < stream->props.count; i++) {
170 int frame_offset = 0;
171 dev_dbg(&stream->udev->dev, "%s: alloc urb=%d\n", __func__, i);
172 stream->urb_list[i] = usb_alloc_urb(
173 stream->props.u.isoc.framesperurb, GFP_ATOMIC);
174 if (!stream->urb_list[i]) {
175 dev_dbg(&stream->udev->dev, "%s: failed\n", __func__);
176 for (j = 0; j < i; j++)
177 usb_free_urb(stream->urb_list[j]);
181 urb = stream->urb_list[i];
183 urb->dev = stream->udev;
184 urb->context = stream;
185 urb->complete = usb_urb_complete;
186 urb->pipe = usb_rcvisocpipe(stream->udev,
187 stream->props.endpoint);
188 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
189 urb->interval = stream->props.u.isoc.interval;
190 urb->number_of_packets = stream->props.u.isoc.framesperurb;
191 urb->transfer_buffer_length = stream->props.u.isoc.framesize *
192 stream->props.u.isoc.framesperurb;
193 urb->transfer_buffer = stream->buf_list[i];
194 urb->transfer_dma = stream->dma_addr[i];
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;
203 stream->urbs_initialized++;
208 static int usb_free_stream_buffers(struct usb_data_stream *stream)
210 if (stream->state & USB_STATE_URB_BUF) {
211 while (stream->buf_num) {
213 dev_dbg(&stream->udev->dev, "%s: free buf=%d\n",
214 __func__, stream->buf_num);
215 usb_free_coherent(stream->udev, stream->buf_size,
216 stream->buf_list[stream->buf_num],
217 stream->dma_addr[stream->buf_num]);
221 stream->state &= ~USB_STATE_URB_BUF;
226 static int usb_alloc_stream_buffers(struct usb_data_stream *stream, int num,
230 stream->buf_size = size;
232 dev_dbg(&stream->udev->dev, "%s: all in all I will use %lu bytes for " \
233 "streaming\n", __func__, num * size);
235 for (stream->buf_num = 0; stream->buf_num < num; stream->buf_num++) {
236 stream->buf_list[stream->buf_num] = usb_alloc_coherent(
237 stream->udev, size, GFP_ATOMIC,
238 &stream->dma_addr[stream->buf_num]);
239 if (!stream->buf_list[stream->buf_num]) {
240 dev_dbg(&stream->udev->dev, "%s: alloc buf=%d failed\n",
241 __func__, stream->buf_num);
242 usb_free_stream_buffers(stream);
246 dev_dbg(&stream->udev->dev, "%s: alloc buf=%d %p (dma %llu)\n",
247 __func__, stream->buf_num,
248 stream->buf_list[stream->buf_num],
249 (long long)stream->dma_addr[stream->buf_num]);
250 memset(stream->buf_list[stream->buf_num], 0, size);
251 stream->state |= USB_STATE_URB_BUF;
257 int usb_urb_reconfig(struct usb_data_stream *stream,
258 struct usb_data_stream_properties *props)
265 /* check allocated buffers are large enough for the request */
266 if (props->type == USB_BULK) {
267 buf_size = stream->props.u.bulk.buffersize;
268 } else if (props->type == USB_ISOC) {
269 buf_size = props->u.isoc.framesize * props->u.isoc.framesperurb;
271 dev_err(&stream->udev->dev, "%s: invalid endpoint type=%d\n",
272 KBUILD_MODNAME, props->type);
276 if (stream->buf_num < props->count || stream->buf_size < buf_size) {
277 dev_err(&stream->udev->dev, "%s: cannot reconfigure as " \
278 "allocated buffers are too small\n",
283 /* check if all fields are same */
284 if (stream->props.type == props->type &&
285 stream->props.count == props->count &&
286 stream->props.endpoint == props->endpoint) {
287 if (props->type == USB_BULK &&
288 props->u.bulk.buffersize ==
289 stream->props.u.bulk.buffersize)
291 else if (props->type == USB_ISOC &&
292 props->u.isoc.framesperurb ==
293 stream->props.u.isoc.framesperurb &&
294 props->u.isoc.framesize ==
295 stream->props.u.isoc.framesize &&
296 props->u.isoc.interval ==
297 stream->props.u.isoc.interval)
301 dev_dbg(&stream->udev->dev, "%s: re-alloc urbs\n", __func__);
303 usb_urb_free_urbs(stream);
304 memcpy(&stream->props, props, sizeof(*props));
305 if (props->type == USB_BULK)
306 return usb_urb_alloc_bulk_urbs(stream);
307 else if (props->type == USB_ISOC)
308 return usb_urb_alloc_isoc_urbs(stream);
313 int usb_urb_initv2(struct usb_data_stream *stream,
314 const struct usb_data_stream_properties *props)
318 if (!stream || !props)
321 memcpy(&stream->props, props, sizeof(*props));
323 if (!stream->complete) {
324 dev_err(&stream->udev->dev, "%s: there is no data callback - " \
325 "this doesn't make sense\n", KBUILD_MODNAME);
329 switch (stream->props.type) {
331 ret = usb_alloc_stream_buffers(stream, stream->props.count,
332 stream->props.u.bulk.buffersize);
336 return usb_urb_alloc_bulk_urbs(stream);
338 ret = usb_alloc_stream_buffers(stream, stream->props.count,
339 stream->props.u.isoc.framesize *
340 stream->props.u.isoc.framesperurb);
344 return usb_urb_alloc_isoc_urbs(stream);
346 dev_err(&stream->udev->dev, "%s: unknown urb-type for data " \
347 "transfer\n", KBUILD_MODNAME);
352 int usb_urb_exitv2(struct usb_data_stream *stream)
354 usb_urb_free_urbs(stream);
355 usb_free_stream_buffers(stream);