usb_urb.c 9.5 KB

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