1 /* dvb-usb-urb.c is part of the DVB USB library.
3 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
4 * see dvb-usb-init.c for copyright information.
6 * This file contains functions for initializing and handling the
9 #include "dvb-usb-common.h"
11 int dvb_usb_generic_rw(struct dvb_usb_device *d, u8 *wbuf, u16 wlen, u8 *rbuf,
12 u16 rlen, int delay_ms)
14 int actlen,ret = -ENOMEM;
16 if (d->props.generic_bulk_ctrl_endpoint == 0) {
17 err("endpoint for generic control not specified.");
21 if (wbuf == NULL || wlen == 0)
24 if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
28 debug_dump(wbuf,wlen,deb_xfer);
30 ret = usb_bulk_msg(d->udev,usb_sndbulkpipe(d->udev,
31 d->props.generic_bulk_ctrl_endpoint), wbuf,wlen,&actlen,
35 err("bulk message failed: %d (%d/%d)",ret,wlen,actlen);
37 ret = actlen != wlen ? -1 : 0;
39 /* an answer is expected, and no error before */
40 if (!ret && rbuf && rlen) {
44 ret = usb_bulk_msg(d->udev,usb_rcvbulkpipe(d->udev,
45 d->props.generic_bulk_ctrl_endpoint),rbuf,rlen,&actlen,
49 err("recv bulk message failed: %d",ret);
52 debug_dump(rbuf,actlen,deb_xfer);
56 mutex_unlock(&d->usb_mutex);
59 EXPORT_SYMBOL(dvb_usb_generic_rw);
61 int dvb_usb_generic_write(struct dvb_usb_device *d, u8 *buf, u16 len)
63 return dvb_usb_generic_rw(d,buf,len,NULL,0,0);
65 EXPORT_SYMBOL(dvb_usb_generic_write);
68 /* URB stuff for streaming */
69 static void dvb_usb_urb_complete(struct urb *urb, struct pt_regs *ptregs)
71 struct dvb_usb_device *d = urb->context;
72 int ptype = usb_pipetype(urb->pipe);
76 deb_ts("'%s' urb completed. feedcount: %d, status: %d, length: %d/%d, pack_num: %d, errors: %d\n",
77 ptype == PIPE_ISOCHRONOUS ? "isoc" : "bulk", d->feedcount,
78 urb->status,urb->actual_length,urb->transfer_buffer_length,
79 urb->number_of_packets,urb->error_count);
81 switch (urb->status) {
83 case -ETIMEDOUT: /* NAK */
85 case -ECONNRESET: /* kill */
90 deb_ts("urb completition error %d.", urb->status);
94 if (d->feedcount > 0) {
95 if (d->state & DVB_USB_STATE_DVB) {
97 case PIPE_ISOCHRONOUS:
98 b = (u8 *) urb->transfer_buffer;
99 for (i = 0; i < urb->number_of_packets; i++) {
100 if (urb->iso_frame_desc[i].status != 0)
101 deb_ts("iso frame descriptor has an error: %d\n",urb->iso_frame_desc[i].status);
102 else if (urb->iso_frame_desc[i].actual_length > 0) {
103 dvb_dmx_swfilter(&d->demux,b + urb->iso_frame_desc[i].offset,
104 urb->iso_frame_desc[i].actual_length);
106 urb->iso_frame_desc[i].status = 0;
107 urb->iso_frame_desc[i].actual_length = 0;
109 debug_dump(b,20,deb_ts);
112 if (urb->actual_length > 0)
113 dvb_dmx_swfilter(&d->demux, (u8 *) urb->transfer_buffer,urb->actual_length);
116 err("unkown endpoint type in completition handler.");
122 usb_submit_urb(urb,GFP_ATOMIC);
125 int dvb_usb_urb_kill(struct dvb_usb_device *d)
128 for (i = 0; i < d->urbs_submitted; i++) {
129 deb_ts("killing URB no. %d.\n",i);
132 usb_kill_urb(d->urb_list[i]);
134 d->urbs_submitted = 0;
138 int dvb_usb_urb_submit(struct dvb_usb_device *d)
141 for (i = 0; i < d->urbs_initialized; i++) {
142 deb_ts("submitting URB no. %d\n",i);
143 if ((ret = usb_submit_urb(d->urb_list[i],GFP_ATOMIC))) {
144 err("could not submit URB no. %d - get them all back",i);
153 static int dvb_usb_free_stream_buffers(struct dvb_usb_device *d)
155 if (d->state & DVB_USB_STATE_URB_BUF) {
158 deb_mem("freeing buffer %d\n",d->buf_num);
159 usb_buffer_free(d->udev, d->buf_size,
160 d->buf_list[d->buf_num], d->dma_addr[d->buf_num]);
166 d->state &= ~DVB_USB_STATE_URB_BUF;
171 static int dvb_usb_allocate_stream_buffers(struct dvb_usb_device *d, int num, unsigned long size)
176 deb_mem("all in all I will use %lu bytes for streaming\n",num*size);
178 if ((d->buf_list = kcalloc(num, sizeof(u8 *), GFP_ATOMIC)) == NULL)
181 if ((d->dma_addr = kcalloc(num, sizeof(dma_addr_t), GFP_ATOMIC)) == NULL) {
186 d->state |= DVB_USB_STATE_URB_BUF;
188 for (d->buf_num = 0; d->buf_num < num; d->buf_num++) {
189 deb_mem("allocating buffer %d\n",d->buf_num);
190 if (( d->buf_list[d->buf_num] =
191 usb_buffer_alloc(d->udev, size, SLAB_ATOMIC,
192 &d->dma_addr[d->buf_num]) ) == NULL) {
193 deb_mem("not enough memory for urb-buffer allocation.\n");
194 dvb_usb_free_stream_buffers(d);
197 deb_mem("buffer %d: %p (dma: %llu)\n",
198 d->buf_num, d->buf_list[d->buf_num],
199 (unsigned long long)d->dma_addr[d->buf_num]);
200 memset(d->buf_list[d->buf_num],0,size);
202 deb_mem("allocation successful\n");
207 static int dvb_usb_bulk_urb_init(struct dvb_usb_device *d)
211 if ((i = dvb_usb_allocate_stream_buffers(d,d->props.urb.count,
212 d->props.urb.u.bulk.buffersize)) < 0)
215 /* allocate the URBs */
216 for (i = 0; i < d->props.urb.count; i++) {
217 if ((d->urb_list[i] = usb_alloc_urb(0,GFP_ATOMIC)) == NULL)
220 usb_fill_bulk_urb( d->urb_list[i], d->udev,
221 usb_rcvbulkpipe(d->udev,d->props.urb.endpoint),
223 d->props.urb.u.bulk.buffersize,
224 dvb_usb_urb_complete, d);
226 d->urb_list[i]->transfer_flags = 0;
227 d->urbs_initialized++;
232 static int dvb_usb_isoc_urb_init(struct dvb_usb_device *d)
236 if ((i = dvb_usb_allocate_stream_buffers(d,d->props.urb.count,
237 d->props.urb.u.isoc.framesize*d->props.urb.u.isoc.framesperurb)) < 0)
240 /* allocate the URBs */
241 for (i = 0; i < d->props.urb.count; i++) {
243 int frame_offset = 0;
244 if ((d->urb_list[i] =
245 usb_alloc_urb(d->props.urb.u.isoc.framesperurb,GFP_ATOMIC)) == NULL)
248 urb = d->urb_list[i];
252 urb->complete = dvb_usb_urb_complete;
253 urb->pipe = usb_rcvisocpipe(d->udev,d->props.urb.endpoint);
254 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
255 urb->interval = d->props.urb.u.isoc.interval;
256 urb->number_of_packets = d->props.urb.u.isoc.framesperurb;
257 urb->transfer_buffer_length = d->buf_size;
258 urb->transfer_buffer = d->buf_list[i];
259 urb->transfer_dma = d->dma_addr[i];
261 for (j = 0; j < d->props.urb.u.isoc.framesperurb; j++) {
262 urb->iso_frame_desc[j].offset = frame_offset;
263 urb->iso_frame_desc[j].length = d->props.urb.u.isoc.framesize;
264 frame_offset += d->props.urb.u.isoc.framesize;
267 d->urbs_initialized++;
273 int dvb_usb_urb_init(struct dvb_usb_device *d)
276 * when reloading the driver w/o replugging the device
277 * sometimes a timeout occures, this helps
279 if (d->props.generic_bulk_ctrl_endpoint != 0) {
280 usb_clear_halt(d->udev,usb_sndbulkpipe(d->udev,d->props.generic_bulk_ctrl_endpoint));
281 usb_clear_halt(d->udev,usb_rcvbulkpipe(d->udev,d->props.generic_bulk_ctrl_endpoint));
283 usb_clear_halt(d->udev,usb_rcvbulkpipe(d->udev,d->props.urb.endpoint));
285 /* allocate the array for the data transfer URBs */
286 d->urb_list = kzalloc(d->props.urb.count * sizeof(struct urb *),GFP_KERNEL);
287 if (d->urb_list == NULL)
289 d->state |= DVB_USB_STATE_URB_LIST;
291 switch (d->props.urb.type) {
293 return dvb_usb_bulk_urb_init(d);
295 return dvb_usb_isoc_urb_init(d);
297 err("unkown URB-type for data transfer.");
302 int dvb_usb_urb_exit(struct dvb_usb_device *d)
308 if (d->state & DVB_USB_STATE_URB_LIST) {
309 for (i = 0; i < d->urbs_initialized; i++) {
310 if (d->urb_list[i] != NULL) {
311 deb_mem("freeing URB no. %d.\n",i);
313 usb_free_urb(d->urb_list[i]);
316 d->urbs_initialized = 0;
317 /* free the urb array */
319 d->state &= ~DVB_USB_STATE_URB_LIST;
322 dvb_usb_free_stream_buffers(d);