usb-urb.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* usb-urb.c is part of the DVB USB library.
  2. *
  3. * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
  4. * see dvb-usb-init.c for copyright information.
  5. *
  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).
  10. */
  11. #include "dvb-usb-common.h"
  12. /* URB stuff for streaming */
  13. static void usb_urb_complete(struct urb *urb)
  14. {
  15. struct usb_data_stream *stream = urb->context;
  16. int ptype = usb_pipetype(urb->pipe);
  17. int i;
  18. u8 *b;
  19. deb_uxfer("'%s' urb completed. status: %d, length: %d/%d, pack_num: %d, errors: %d\n",
  20. ptype == PIPE_ISOCHRONOUS ? "isoc" : "bulk",
  21. urb->status,urb->actual_length,urb->transfer_buffer_length,
  22. urb->number_of_packets,urb->error_count);
  23. switch (urb->status) {
  24. case 0: /* success */
  25. case -ETIMEDOUT: /* NAK */
  26. break;
  27. case -ECONNRESET: /* kill */
  28. case -ENOENT:
  29. case -ESHUTDOWN:
  30. return;
  31. default: /* error */
  32. deb_ts("urb completition error %d.\n", urb->status);
  33. break;
  34. }
  35. b = (u8 *) urb->transfer_buffer;
  36. switch (ptype) {
  37. case PIPE_ISOCHRONOUS:
  38. for (i = 0; i < urb->number_of_packets; i++) {
  39. if (urb->iso_frame_desc[i].status != 0)
  40. deb_ts("iso frame descriptor has an error: %d\n",urb->iso_frame_desc[i].status);
  41. else if (urb->iso_frame_desc[i].actual_length > 0)
  42. stream->complete(stream, b + urb->iso_frame_desc[i].offset, urb->iso_frame_desc[i].actual_length);
  43. urb->iso_frame_desc[i].status = 0;
  44. urb->iso_frame_desc[i].actual_length = 0;
  45. }
  46. debug_dump(b,20,deb_uxfer);
  47. break;
  48. case PIPE_BULK:
  49. if (urb->actual_length > 0)
  50. stream->complete(stream, b, urb->actual_length);
  51. break;
  52. default:
  53. err("unknown endpoint type in completition handler.");
  54. return;
  55. }
  56. usb_submit_urb(urb,GFP_ATOMIC);
  57. }
  58. int usb_urb_kill(struct usb_data_stream *stream)
  59. {
  60. int i;
  61. for (i = 0; i < stream->urbs_submitted; i++) {
  62. deb_ts("killing URB no. %d.\n",i);
  63. /* stop the URB */
  64. usb_kill_urb(stream->urb_list[i]);
  65. }
  66. stream->urbs_submitted = 0;
  67. return 0;
  68. }
  69. int usb_urb_submit(struct usb_data_stream *stream)
  70. {
  71. int i,ret;
  72. for (i = 0; i < stream->urbs_initialized; i++) {
  73. deb_ts("submitting URB no. %d\n",i);
  74. if ((ret = usb_submit_urb(stream->urb_list[i],GFP_ATOMIC))) {
  75. err("could not submit URB no. %d - get them all back",i);
  76. usb_urb_kill(stream);
  77. return ret;
  78. }
  79. stream->urbs_submitted++;
  80. }
  81. return 0;
  82. }
  83. static int usb_free_stream_buffers(struct usb_data_stream *stream)
  84. {
  85. if (stream->state & USB_STATE_URB_BUF) {
  86. while (stream->buf_num) {
  87. stream->buf_num--;
  88. deb_mem("freeing buffer %d\n",stream->buf_num);
  89. usb_buffer_free(stream->udev, stream->buf_size,
  90. stream->buf_list[stream->buf_num], stream->dma_addr[stream->buf_num]);
  91. }
  92. }
  93. stream->state &= ~USB_STATE_URB_BUF;
  94. return 0;
  95. }
  96. static int usb_allocate_stream_buffers(struct usb_data_stream *stream, int num, unsigned long size)
  97. {
  98. stream->buf_num = 0;
  99. stream->buf_size = size;
  100. deb_mem("all in all I will use %lu bytes for streaming\n",num*size);
  101. for (stream->buf_num = 0; stream->buf_num < num; stream->buf_num++) {
  102. deb_mem("allocating buffer %d\n",stream->buf_num);
  103. if (( stream->buf_list[stream->buf_num] =
  104. usb_buffer_alloc(stream->udev, size, GFP_ATOMIC,
  105. &stream->dma_addr[stream->buf_num]) ) == NULL) {
  106. deb_mem("not enough memory for urb-buffer allocation.\n");
  107. usb_free_stream_buffers(stream);
  108. return -ENOMEM;
  109. }
  110. deb_mem("buffer %d: %p (dma: %Lu)\n",
  111. stream->buf_num,
  112. stream->buf_list[stream->buf_num], (long long)stream->dma_addr[stream->buf_num]);
  113. memset(stream->buf_list[stream->buf_num],0,size);
  114. stream->state |= USB_STATE_URB_BUF;
  115. }
  116. deb_mem("allocation successful\n");
  117. return 0;
  118. }
  119. static int usb_bulk_urb_init(struct usb_data_stream *stream)
  120. {
  121. int i, j;
  122. if ((i = usb_allocate_stream_buffers(stream,stream->props.count,
  123. stream->props.u.bulk.buffersize)) < 0)
  124. return i;
  125. /* allocate the URBs */
  126. for (i = 0; i < stream->props.count; i++) {
  127. stream->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
  128. if (!stream->urb_list[i]) {
  129. deb_mem("not enough memory for urb_alloc_urb!.\n");
  130. for (j = 0; j < i; j++)
  131. usb_free_urb(stream->urb_list[i]);
  132. return -ENOMEM;
  133. }
  134. usb_fill_bulk_urb( stream->urb_list[i], stream->udev,
  135. usb_rcvbulkpipe(stream->udev,stream->props.endpoint),
  136. stream->buf_list[i],
  137. stream->props.u.bulk.buffersize,
  138. usb_urb_complete, stream);
  139. stream->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  140. stream->urb_list[i]->transfer_dma = stream->dma_addr[i];
  141. stream->urbs_initialized++;
  142. }
  143. return 0;
  144. }
  145. static int usb_isoc_urb_init(struct usb_data_stream *stream)
  146. {
  147. int i,j;
  148. if ((i = usb_allocate_stream_buffers(stream,stream->props.count,
  149. stream->props.u.isoc.framesize*stream->props.u.isoc.framesperurb)) < 0)
  150. return i;
  151. /* allocate the URBs */
  152. for (i = 0; i < stream->props.count; i++) {
  153. struct urb *urb;
  154. int frame_offset = 0;
  155. stream->urb_list[i] = usb_alloc_urb(stream->props.u.isoc.framesperurb, GFP_ATOMIC);
  156. if (!stream->urb_list[i]) {
  157. deb_mem("not enough memory for urb_alloc_urb!\n");
  158. for (j = 0; j < i; j++)
  159. usb_free_urb(stream->urb_list[i]);
  160. return -ENOMEM;
  161. }
  162. urb = stream->urb_list[i];
  163. urb->dev = stream->udev;
  164. urb->context = stream;
  165. urb->complete = usb_urb_complete;
  166. urb->pipe = usb_rcvisocpipe(stream->udev,stream->props.endpoint);
  167. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  168. urb->interval = stream->props.u.isoc.interval;
  169. urb->number_of_packets = stream->props.u.isoc.framesperurb;
  170. urb->transfer_buffer_length = stream->buf_size;
  171. urb->transfer_buffer = stream->buf_list[i];
  172. urb->transfer_dma = stream->dma_addr[i];
  173. for (j = 0; j < stream->props.u.isoc.framesperurb; j++) {
  174. urb->iso_frame_desc[j].offset = frame_offset;
  175. urb->iso_frame_desc[j].length = stream->props.u.isoc.framesize;
  176. frame_offset += stream->props.u.isoc.framesize;
  177. }
  178. stream->urbs_initialized++;
  179. }
  180. return 0;
  181. }
  182. int usb_urb_init(struct usb_data_stream *stream, struct usb_data_stream_properties *props)
  183. {
  184. if (stream == NULL || props == NULL)
  185. return -EINVAL;
  186. memcpy(&stream->props, props, sizeof(*props));
  187. usb_clear_halt(stream->udev,usb_rcvbulkpipe(stream->udev,stream->props.endpoint));
  188. if (stream->complete == NULL) {
  189. err("there is no data callback - this doesn't make sense.");
  190. return -EINVAL;
  191. }
  192. switch (stream->props.type) {
  193. case USB_BULK:
  194. return usb_bulk_urb_init(stream);
  195. case USB_ISOC:
  196. return usb_isoc_urb_init(stream);
  197. default:
  198. err("unknown URB-type for data transfer.");
  199. return -EINVAL;
  200. }
  201. }
  202. int usb_urb_exit(struct usb_data_stream *stream)
  203. {
  204. int i;
  205. usb_urb_kill(stream);
  206. for (i = 0; i < stream->urbs_initialized; i++) {
  207. if (stream->urb_list[i] != NULL) {
  208. deb_mem("freeing URB no. %d.\n",i);
  209. /* free the URBs */
  210. usb_free_urb(stream->urb_list[i]);
  211. }
  212. }
  213. stream->urbs_initialized = 0;
  214. usb_free_stream_buffers(stream);
  215. return 0;
  216. }