usb_urb.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. int usb_urb_reconfig(struct usb_data_stream *stream,
  14. struct usb_data_stream_properties *props);
  15. static void usb_urb_complete(struct urb *urb)
  16. {
  17. struct usb_data_stream *stream = urb->context;
  18. int ptype = usb_pipetype(urb->pipe);
  19. int i;
  20. u8 *b;
  21. dev_dbg(&stream->udev->dev, "%s: %s urb completed status=%d " \
  22. "length=%d/%d pack_num=%d errors=%d\n", __func__,
  23. ptype == PIPE_ISOCHRONOUS ? "isoc" : "bulk",
  24. urb->status, urb->actual_length,
  25. urb->transfer_buffer_length,
  26. urb->number_of_packets, urb->error_count);
  27. switch (urb->status) {
  28. case 0: /* success */
  29. case -ETIMEDOUT: /* NAK */
  30. break;
  31. case -ECONNRESET: /* kill */
  32. case -ENOENT:
  33. case -ESHUTDOWN:
  34. return;
  35. default: /* error */
  36. dev_dbg(&stream->udev->dev, "%s: urb completition failed=%d\n",
  37. __func__, urb->status);
  38. break;
  39. }
  40. b = (u8 *) urb->transfer_buffer;
  41. switch (ptype) {
  42. case PIPE_ISOCHRONOUS:
  43. for (i = 0; i < urb->number_of_packets; i++) {
  44. if (urb->iso_frame_desc[i].status != 0)
  45. dev_dbg(&stream->udev->dev, "%s: iso frame " \
  46. "descriptor has an error=%d\n",
  47. __func__,
  48. urb->iso_frame_desc[i].status);
  49. else if (urb->iso_frame_desc[i].actual_length > 0)
  50. stream->complete(stream,
  51. b + urb->iso_frame_desc[i].offset,
  52. urb->iso_frame_desc[i].actual_length);
  53. urb->iso_frame_desc[i].status = 0;
  54. urb->iso_frame_desc[i].actual_length = 0;
  55. }
  56. break;
  57. case PIPE_BULK:
  58. if (urb->actual_length > 0)
  59. stream->complete(stream, b, urb->actual_length);
  60. break;
  61. default:
  62. dev_err(&stream->udev->dev, "%s: unknown endpoint type in " \
  63. "completition handler\n", KBUILD_MODNAME);
  64. return;
  65. }
  66. usb_submit_urb(urb, GFP_ATOMIC);
  67. }
  68. int usb_urb_killv2(struct usb_data_stream *stream)
  69. {
  70. int i;
  71. for (i = 0; i < stream->urbs_submitted; i++) {
  72. dev_dbg(&stream->udev->dev, "%s: kill urb=%d\n", __func__, i);
  73. /* stop the URB */
  74. usb_kill_urb(stream->urb_list[i]);
  75. }
  76. stream->urbs_submitted = 0;
  77. return 0;
  78. }
  79. int usb_urb_submitv2(struct usb_data_stream *stream,
  80. struct usb_data_stream_properties *props)
  81. {
  82. int i, ret;
  83. if (props) {
  84. ret = usb_urb_reconfig(stream, props);
  85. if (ret < 0)
  86. return ret;
  87. }
  88. for (i = 0; i < stream->urbs_initialized; i++) {
  89. dev_dbg(&stream->udev->dev, "%s: submit urb=%d\n", __func__, i);
  90. ret = usb_submit_urb(stream->urb_list[i], GFP_ATOMIC);
  91. if (ret) {
  92. dev_err(&stream->udev->dev, "%s: could not submit " \
  93. "urb no. %d - get them all back\n",
  94. KBUILD_MODNAME, i);
  95. usb_urb_killv2(stream);
  96. return ret;
  97. }
  98. stream->urbs_submitted++;
  99. }
  100. return 0;
  101. }
  102. int usb_urb_free_urbs(struct usb_data_stream *stream)
  103. {
  104. int i;
  105. usb_urb_killv2(stream);
  106. for (i = stream->urbs_initialized - 1; i >= 0; i--) {
  107. if (stream->urb_list[i]) {
  108. dev_dbg(&stream->udev->dev, "%s: free urb=%d\n",
  109. __func__, i);
  110. /* free the URBs */
  111. usb_free_urb(stream->urb_list[i]);
  112. }
  113. }
  114. stream->urbs_initialized = 0;
  115. return 0;
  116. }
  117. static int usb_urb_alloc_bulk_urbs(struct usb_data_stream *stream)
  118. {
  119. int i, j;
  120. /* allocate the URBs */
  121. for (i = 0; i < stream->props.count; i++) {
  122. dev_dbg(&stream->udev->dev, "%s: alloc urb=%d\n", __func__, i);
  123. stream->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
  124. if (!stream->urb_list[i]) {
  125. dev_dbg(&stream->udev->dev, "%s: failed\n", __func__);
  126. for (j = 0; j < i; j++)
  127. usb_free_urb(stream->urb_list[j]);
  128. return -ENOMEM;
  129. }
  130. usb_fill_bulk_urb(stream->urb_list[i],
  131. stream->udev,
  132. usb_rcvbulkpipe(stream->udev,
  133. stream->props.endpoint),
  134. stream->buf_list[i],
  135. stream->props.u.bulk.buffersize,
  136. usb_urb_complete, stream);
  137. stream->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  138. stream->urb_list[i]->transfer_dma = stream->dma_addr[i];
  139. stream->urbs_initialized++;
  140. }
  141. return 0;
  142. }
  143. static int usb_urb_alloc_isoc_urbs(struct usb_data_stream *stream)
  144. {
  145. int i, j;
  146. /* allocate the URBs */
  147. for (i = 0; i < stream->props.count; i++) {
  148. struct urb *urb;
  149. int frame_offset = 0;
  150. dev_dbg(&stream->udev->dev, "%s: alloc urb=%d\n", __func__, i);
  151. stream->urb_list[i] = usb_alloc_urb(
  152. stream->props.u.isoc.framesperurb, GFP_ATOMIC);
  153. if (!stream->urb_list[i]) {
  154. dev_dbg(&stream->udev->dev, "%s: failed\n", __func__);
  155. for (j = 0; j < i; j++)
  156. usb_free_urb(stream->urb_list[j]);
  157. return -ENOMEM;
  158. }
  159. urb = stream->urb_list[i];
  160. urb->dev = stream->udev;
  161. urb->context = stream;
  162. urb->complete = usb_urb_complete;
  163. urb->pipe = usb_rcvisocpipe(stream->udev,
  164. stream->props.endpoint);
  165. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  166. urb->interval = stream->props.u.isoc.interval;
  167. urb->number_of_packets = stream->props.u.isoc.framesperurb;
  168. urb->transfer_buffer_length = stream->props.u.isoc.framesize *
  169. stream->props.u.isoc.framesperurb;
  170. urb->transfer_buffer = stream->buf_list[i];
  171. urb->transfer_dma = stream->dma_addr[i];
  172. for (j = 0; j < stream->props.u.isoc.framesperurb; j++) {
  173. urb->iso_frame_desc[j].offset = frame_offset;
  174. urb->iso_frame_desc[j].length =
  175. 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_free_stream_buffers(struct usb_data_stream *stream)
  183. {
  184. if (stream->state & USB_STATE_URB_BUF) {
  185. while (stream->buf_num) {
  186. stream->buf_num--;
  187. dev_dbg(&stream->udev->dev, "%s: free buf=%d\n",
  188. __func__, stream->buf_num);
  189. usb_free_coherent(stream->udev, stream->buf_size,
  190. stream->buf_list[stream->buf_num],
  191. stream->dma_addr[stream->buf_num]);
  192. }
  193. }
  194. stream->state &= ~USB_STATE_URB_BUF;
  195. return 0;
  196. }
  197. int usb_alloc_stream_buffers(struct usb_data_stream *stream, int num,
  198. unsigned long size)
  199. {
  200. stream->buf_num = 0;
  201. stream->buf_size = size;
  202. dev_dbg(&stream->udev->dev, "%s: all in all I will use %lu bytes for " \
  203. "streaming\n", __func__, num * size);
  204. for (stream->buf_num = 0; stream->buf_num < num; stream->buf_num++) {
  205. stream->buf_list[stream->buf_num] = usb_alloc_coherent(
  206. stream->udev, size, GFP_ATOMIC,
  207. &stream->dma_addr[stream->buf_num]);
  208. if (!stream->buf_list[stream->buf_num]) {
  209. dev_dbg(&stream->udev->dev, "%s: alloc buf=%d failed\n",
  210. __func__, stream->buf_num);
  211. usb_free_stream_buffers(stream);
  212. return -ENOMEM;
  213. }
  214. dev_dbg(&stream->udev->dev, "%s: alloc buf=%d %p (dma %llu)\n",
  215. __func__, stream->buf_num,
  216. stream->buf_list[stream->buf_num],
  217. (long long)stream->dma_addr[stream->buf_num]);
  218. memset(stream->buf_list[stream->buf_num], 0, size);
  219. stream->state |= USB_STATE_URB_BUF;
  220. }
  221. return 0;
  222. }
  223. int usb_urb_reconfig(struct usb_data_stream *stream,
  224. struct usb_data_stream_properties *props)
  225. {
  226. int buf_size;
  227. if (!props)
  228. return 0;
  229. /* check allocated buffers are large enough for the request */
  230. if (props->type == USB_BULK) {
  231. buf_size = stream->props.u.bulk.buffersize;
  232. } else if (props->type == USB_ISOC) {
  233. buf_size = props->u.isoc.framesize * props->u.isoc.framesperurb;
  234. } else {
  235. dev_err(&stream->udev->dev, "%s: invalid endpoint type=%d\n",
  236. KBUILD_MODNAME, props->type);
  237. return -EINVAL;
  238. }
  239. if (stream->buf_num < props->count || stream->buf_size < buf_size) {
  240. dev_err(&stream->udev->dev, "%s: cannot reconfigure as " \
  241. "allocated buffers are too small\n",
  242. KBUILD_MODNAME);
  243. return -EINVAL;
  244. }
  245. /* check if all fields are same */
  246. if (stream->props.type == props->type &&
  247. stream->props.count == props->count &&
  248. stream->props.endpoint == props->endpoint) {
  249. if (props->type == USB_BULK &&
  250. props->u.bulk.buffersize ==
  251. stream->props.u.bulk.buffersize)
  252. return 0;
  253. else if (props->type == USB_ISOC &&
  254. props->u.isoc.framesperurb ==
  255. stream->props.u.isoc.framesperurb &&
  256. props->u.isoc.framesize ==
  257. stream->props.u.isoc.framesize &&
  258. props->u.isoc.interval ==
  259. stream->props.u.isoc.interval)
  260. return 0;
  261. }
  262. dev_dbg(&stream->udev->dev, "%s: re-alloc urbs\n", __func__);
  263. usb_urb_free_urbs(stream);
  264. memcpy(&stream->props, props, sizeof(*props));
  265. if (props->type == USB_BULK)
  266. return usb_urb_alloc_bulk_urbs(stream);
  267. else if (props->type == USB_ISOC)
  268. return usb_urb_alloc_isoc_urbs(stream);
  269. return 0;
  270. }
  271. int usb_urb_initv2(struct usb_data_stream *stream,
  272. const struct usb_data_stream_properties *props)
  273. {
  274. int ret;
  275. if (!stream || !props)
  276. return -EINVAL;
  277. memcpy(&stream->props, props, sizeof(*props));
  278. if (!stream->complete) {
  279. dev_err(&stream->udev->dev, "%s: there is no data callback - " \
  280. "this doesn't make sense\n", KBUILD_MODNAME);
  281. return -EINVAL;
  282. }
  283. switch (stream->props.type) {
  284. case USB_BULK:
  285. ret = usb_alloc_stream_buffers(stream, stream->props.count,
  286. stream->props.u.bulk.buffersize);
  287. if (ret < 0)
  288. return ret;
  289. return usb_urb_alloc_bulk_urbs(stream);
  290. case USB_ISOC:
  291. ret = usb_alloc_stream_buffers(stream, stream->props.count,
  292. stream->props.u.isoc.framesize *
  293. stream->props.u.isoc.framesperurb);
  294. if (ret < 0)
  295. return ret;
  296. return usb_urb_alloc_isoc_urbs(stream);
  297. default:
  298. dev_err(&stream->udev->dev, "%s: unknown urb-type for data " \
  299. "transfer\n", KBUILD_MODNAME);
  300. return -EINVAL;
  301. }
  302. }
  303. int usb_urb_exitv2(struct usb_data_stream *stream)
  304. {
  305. usb_urb_free_urbs(stream);
  306. usb_free_stream_buffers(stream);
  307. return 0;
  308. }