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 = down_interruptible(&d->usb_sem)))
27 debug_dump(wbuf,wlen,deb_xfer);
29 ret = usb_bulk_msg(d->udev,usb_sndbulkpipe(d->udev,
30 d->props.generic_bulk_ctrl_endpoint), wbuf,wlen,&actlen,
34 err("bulk message failed: %d (%d/%d)",ret,wlen,actlen);
36 ret = actlen != wlen ? -1 : 0;
38 /* an answer is expected, and no error before */
39 if (!ret && rbuf && rlen) {
43 ret = usb_bulk_msg(d->udev,usb_rcvbulkpipe(d->udev,
44 d->props.generic_bulk_ctrl_endpoint),rbuf,rlen,&actlen,
48 err("recv bulk message failed: %d",ret);
50 debug_dump(rbuf,actlen,deb_xfer);
56 EXPORT_SYMBOL(dvb_usb_generic_rw);
58 int dvb_usb_generic_write(struct dvb_usb_device *d, u8 *buf, u16 len)
60 return dvb_usb_generic_rw(d,buf,len,NULL,0,0);
62 EXPORT_SYMBOL(dvb_usb_generic_write);
64 static void dvb_usb_bulk_urb_complete(struct urb *urb, struct pt_regs *ptregs)
66 struct dvb_usb_device *d = urb->context;
68 deb_ts("bulk urb completed. feedcount: %d, status: %d, length: %d\n",d->feedcount,urb->status,
71 switch (urb->status) {
73 case -ETIMEDOUT: /* NAK */
75 case -ECONNRESET: /* kill */
80 deb_ts("urb completition error %d.", urb->status);
84 if (d->feedcount > 0 && urb->actual_length > 0) {
85 if (d->state & DVB_USB_STATE_DVB)
86 dvb_dmx_swfilter(&d->demux, (u8*) urb->transfer_buffer,urb->actual_length);
88 deb_ts("URB dropped because of feedcount.\n");
90 usb_submit_urb(urb,GFP_ATOMIC);
93 int dvb_usb_urb_kill(struct dvb_usb_device *d)
96 for (i = 0; i < d->urbs_submitted; i++) {
97 deb_info("killing URB no. %d.\n",i);
100 usb_kill_urb(d->urb_list[i]);
102 d->urbs_submitted = 0;
106 int dvb_usb_urb_submit(struct dvb_usb_device *d)
109 for (i = 0; i < d->urbs_initialized; i++) {
110 deb_info("submitting URB no. %d\n",i);
111 if ((ret = usb_submit_urb(d->urb_list[i],GFP_ATOMIC))) {
112 err("could not submit URB no. %d - get them all back\n",i);
121 static int dvb_usb_bulk_urb_init(struct dvb_usb_device *d)
123 int i,bufsize = d->props.urb.count * d->props.urb.u.bulk.buffersize;
125 deb_info("allocate %d bytes as buffersize for all URBs\n",bufsize);
126 /* allocate the actual buffer for the URBs */
127 if ((d->buffer = usb_buffer_alloc(d->udev, bufsize, SLAB_ATOMIC, &d->dma_handle)) == NULL) {
128 deb_info("not enough memory for urb-buffer allocation.\n");
131 deb_info("allocation successful\n");
132 memset(d->buffer,0,bufsize);
134 d->state |= DVB_USB_STATE_URB_BUF;
136 /* allocate the URBs */
137 for (i = 0; i < d->props.urb.count; i++) {
138 if (!(d->urb_list[i] = usb_alloc_urb(0,GFP_ATOMIC))) {
142 usb_fill_bulk_urb( d->urb_list[i], d->udev,
143 usb_rcvbulkpipe(d->udev,d->props.urb.endpoint),
144 &d->buffer[i*d->props.urb.u.bulk.buffersize],
145 d->props.urb.u.bulk.buffersize,
146 dvb_usb_bulk_urb_complete, d);
148 d->urb_list[i]->transfer_flags = 0;
149 d->urbs_initialized++;
154 int dvb_usb_urb_init(struct dvb_usb_device *d)
157 * when reloading the driver w/o replugging the device
158 * sometimes a timeout occures, this helps
160 if (d->props.generic_bulk_ctrl_endpoint != 0) {
161 usb_clear_halt(d->udev,usb_sndbulkpipe(d->udev,d->props.generic_bulk_ctrl_endpoint));
162 usb_clear_halt(d->udev,usb_rcvbulkpipe(d->udev,d->props.generic_bulk_ctrl_endpoint));
164 usb_clear_halt(d->udev,usb_rcvbulkpipe(d->udev,d->props.urb.endpoint));
166 /* allocate the array for the data transfer URBs */
167 d->urb_list = kmalloc(d->props.urb.count * sizeof(struct urb *),GFP_KERNEL);
168 if (d->urb_list == NULL)
170 memset(d->urb_list,0,d->props.urb.count * sizeof(struct urb *));
171 d->state |= DVB_USB_STATE_URB_LIST;
173 switch (d->props.urb.type) {
175 return dvb_usb_bulk_urb_init(d);
177 err("isochronous transfer not yet implemented in dvb-usb.");
180 err("unkown URB-type for data transfer.");
185 int dvb_usb_urb_exit(struct dvb_usb_device *d)
191 if (d->state & DVB_USB_STATE_URB_LIST) {
192 for (i = 0; i < d->urbs_initialized; i++) {
193 if (d->urb_list[i] != NULL) {
194 deb_info("freeing URB no. %d.\n",i);
196 usb_free_urb(d->urb_list[i]);
199 d->urbs_initialized = 0;
200 /* free the urb array */
202 d->state &= ~DVB_USB_STATE_URB_LIST;
205 if (d->state & DVB_USB_STATE_URB_BUF)
206 usb_buffer_free(d->udev, d->props.urb.u.bulk.buffersize * d->props.urb.count,
207 d->buffer, d->dma_handle);
209 d->state &= ~DVB_USB_STATE_URB_BUF;