usb_urb.c 9.0 KB

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