usb-urb.c 6.9 KB

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