usb_urb.c 9.5 KB

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