usb-urb.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_free_coherent(stream->udev, stream->buf_size,
  90. stream->buf_list[stream->buf_num],
  91. stream->dma_addr[stream->buf_num]);
  92. }
  93. }
  94. stream->state &= ~USB_STATE_URB_BUF;
  95. return 0;
  96. }
  97. static int usb_allocate_stream_buffers(struct usb_data_stream *stream, int num, unsigned long size)
  98. {
  99. stream->buf_num = 0;
  100. stream->buf_size = size;
  101. deb_mem("all in all I will use %lu bytes for streaming\n",num*size);
  102. for (stream->buf_num = 0; stream->buf_num < num; stream->buf_num++) {
  103. deb_mem("allocating buffer %d\n",stream->buf_num);
  104. if (( stream->buf_list[stream->buf_num] =
  105. usb_alloc_coherent(stream->udev, size, GFP_ATOMIC,
  106. &stream->dma_addr[stream->buf_num]) ) == NULL) {
  107. deb_mem("not enough memory for urb-buffer allocation.\n");
  108. usb_free_stream_buffers(stream);
  109. return -ENOMEM;
  110. }
  111. deb_mem("buffer %d: %p (dma: %Lu)\n",
  112. stream->buf_num,
  113. stream->buf_list[stream->buf_num], (long long)stream->dma_addr[stream->buf_num]);
  114. memset(stream->buf_list[stream->buf_num],0,size);
  115. stream->state |= USB_STATE_URB_BUF;
  116. }
  117. deb_mem("allocation successful\n");
  118. return 0;
  119. }
  120. static int usb_bulk_urb_init(struct usb_data_stream *stream)
  121. {
  122. int i, j;
  123. if ((i = usb_allocate_stream_buffers(stream,stream->props.count,
  124. stream->props.u.bulk.buffersize)) < 0)
  125. return i;
  126. /* allocate the URBs */
  127. for (i = 0; i < stream->props.count; i++) {
  128. stream->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
  129. if (!stream->urb_list[i]) {
  130. deb_mem("not enough memory for urb_alloc_urb!.\n");
  131. for (j = 0; j < i; j++)
  132. usb_free_urb(stream->urb_list[i]);
  133. return -ENOMEM;
  134. }
  135. usb_fill_bulk_urb( stream->urb_list[i], stream->udev,
  136. usb_rcvbulkpipe(stream->udev,stream->props.endpoint),
  137. stream->buf_list[i],
  138. stream->props.u.bulk.buffersize,
  139. usb_urb_complete, stream);
  140. stream->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  141. stream->urb_list[i]->transfer_dma = stream->dma_addr[i];
  142. stream->urbs_initialized++;
  143. }
  144. return 0;
  145. }
  146. static int usb_isoc_urb_init(struct usb_data_stream *stream)
  147. {
  148. int i,j;
  149. if ((i = usb_allocate_stream_buffers(stream,stream->props.count,
  150. stream->props.u.isoc.framesize*stream->props.u.isoc.framesperurb)) < 0)
  151. return i;
  152. /* allocate the URBs */
  153. for (i = 0; i < stream->props.count; i++) {
  154. struct urb *urb;
  155. int frame_offset = 0;
  156. stream->urb_list[i] = usb_alloc_urb(stream->props.u.isoc.framesperurb, GFP_ATOMIC);
  157. if (!stream->urb_list[i]) {
  158. deb_mem("not enough memory for urb_alloc_urb!\n");
  159. for (j = 0; j < i; j++)
  160. usb_free_urb(stream->urb_list[i]);
  161. return -ENOMEM;
  162. }
  163. urb = stream->urb_list[i];
  164. urb->dev = stream->udev;
  165. urb->context = stream;
  166. urb->complete = usb_urb_complete;
  167. urb->pipe = usb_rcvisocpipe(stream->udev,stream->props.endpoint);
  168. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  169. urb->interval = stream->props.u.isoc.interval;
  170. urb->number_of_packets = stream->props.u.isoc.framesperurb;
  171. urb->transfer_buffer_length = stream->buf_size;
  172. urb->transfer_buffer = stream->buf_list[i];
  173. urb->transfer_dma = stream->dma_addr[i];
  174. for (j = 0; j < stream->props.u.isoc.framesperurb; j++) {
  175. urb->iso_frame_desc[j].offset = frame_offset;
  176. urb->iso_frame_desc[j].length = stream->props.u.isoc.framesize;
  177. frame_offset += stream->props.u.isoc.framesize;
  178. }
  179. stream->urbs_initialized++;
  180. }
  181. return 0;
  182. }
  183. int usb_urb_init(struct usb_data_stream *stream, struct usb_data_stream_properties *props)
  184. {
  185. if (stream == NULL || props == NULL)
  186. return -EINVAL;
  187. memcpy(&stream->props, props, sizeof(*props));
  188. usb_clear_halt(stream->udev,usb_rcvbulkpipe(stream->udev,stream->props.endpoint));
  189. if (stream->complete == NULL) {
  190. err("there is no data callback - this doesn't make sense.");
  191. return -EINVAL;
  192. }
  193. switch (stream->props.type) {
  194. case USB_BULK:
  195. return usb_bulk_urb_init(stream);
  196. case USB_ISOC:
  197. return usb_isoc_urb_init(stream);
  198. default:
  199. err("unknown URB-type for data transfer.");
  200. return -EINVAL;
  201. }
  202. }
  203. int usb_urb_exit(struct usb_data_stream *stream)
  204. {
  205. int i;
  206. usb_urb_kill(stream);
  207. for (i = 0; i < stream->urbs_initialized; i++) {
  208. if (stream->urb_list[i] != NULL) {
  209. deb_mem("freeing URB no. %d.\n",i);
  210. /* free the URBs */
  211. usb_free_urb(stream->urb_list[i]);
  212. }
  213. }
  214. stream->urbs_initialized = 0;
  215. usb_free_stream_buffers(stream);
  216. return 0;
  217. }