usb_urb.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. 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->urb_list[i]->transfer_flags = URB_FREE_BUFFER;
  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_FREE_BUFFER;
  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. 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. stream->buf_list[stream->buf_num] = NULL;
  189. }
  190. }
  191. stream->state &= ~USB_STATE_URB_BUF;
  192. return 0;
  193. }
  194. static int usb_alloc_stream_buffers(struct usb_data_stream *stream, int num,
  195. unsigned long size)
  196. {
  197. stream->buf_num = 0;
  198. stream->buf_size = size;
  199. dev_dbg(&stream->udev->dev,
  200. "%s: all in all I will use %lu bytes for streaming\n",
  201. __func__, num * size);
  202. for (stream->buf_num = 0; stream->buf_num < num; stream->buf_num++) {
  203. stream->buf_list[stream->buf_num] = kzalloc(size, GFP_ATOMIC);
  204. if (!stream->buf_list[stream->buf_num]) {
  205. dev_dbg(&stream->udev->dev, "%s: alloc buf=%d failed\n",
  206. __func__, stream->buf_num);
  207. usb_free_stream_buffers(stream);
  208. return -ENOMEM;
  209. }
  210. dev_dbg(&stream->udev->dev, "%s: alloc buf=%d %p (dma %llu)\n",
  211. __func__, stream->buf_num,
  212. stream->buf_list[stream->buf_num],
  213. (long long)stream->dma_addr[stream->buf_num]);
  214. stream->state |= USB_STATE_URB_BUF;
  215. }
  216. return 0;
  217. }
  218. int usb_urb_reconfig(struct usb_data_stream *stream,
  219. struct usb_data_stream_properties *props)
  220. {
  221. int buf_size;
  222. if (!props)
  223. return 0;
  224. /* check allocated buffers are large enough for the request */
  225. if (props->type == USB_BULK) {
  226. buf_size = stream->props.u.bulk.buffersize;
  227. } else if (props->type == USB_ISOC) {
  228. buf_size = props->u.isoc.framesize * props->u.isoc.framesperurb;
  229. } else {
  230. dev_err(&stream->udev->dev, "%s: invalid endpoint type=%d\n",
  231. KBUILD_MODNAME, props->type);
  232. return -EINVAL;
  233. }
  234. if (stream->buf_num < props->count || stream->buf_size < buf_size) {
  235. dev_err(&stream->udev->dev,
  236. "%s: cannot reconfigure as allocated buffers are too small\n",
  237. KBUILD_MODNAME);
  238. return -EINVAL;
  239. }
  240. /* check if all fields are same */
  241. if (stream->props.type == props->type &&
  242. stream->props.count == props->count &&
  243. stream->props.endpoint == props->endpoint) {
  244. if (props->type == USB_BULK &&
  245. props->u.bulk.buffersize ==
  246. stream->props.u.bulk.buffersize)
  247. return 0;
  248. else if (props->type == USB_ISOC &&
  249. props->u.isoc.framesperurb ==
  250. stream->props.u.isoc.framesperurb &&
  251. props->u.isoc.framesize ==
  252. stream->props.u.isoc.framesize &&
  253. props->u.isoc.interval ==
  254. stream->props.u.isoc.interval)
  255. return 0;
  256. }
  257. dev_dbg(&stream->udev->dev, "%s: re-alloc urbs\n", __func__);
  258. usb_urb_free_urbs(stream);
  259. memcpy(&stream->props, props, sizeof(*props));
  260. if (props->type == USB_BULK)
  261. return usb_urb_alloc_bulk_urbs(stream);
  262. else if (props->type == USB_ISOC)
  263. return usb_urb_alloc_isoc_urbs(stream);
  264. return 0;
  265. }
  266. int usb_urb_initv2(struct usb_data_stream *stream,
  267. const struct usb_data_stream_properties *props)
  268. {
  269. int ret;
  270. if (!stream || !props)
  271. return -EINVAL;
  272. memcpy(&stream->props, props, sizeof(*props));
  273. if (!stream->complete) {
  274. dev_err(&stream->udev->dev,
  275. "%s: there is no data callback - this doesn't make sense\n",
  276. KBUILD_MODNAME);
  277. return -EINVAL;
  278. }
  279. switch (stream->props.type) {
  280. case USB_BULK:
  281. ret = usb_alloc_stream_buffers(stream, stream->props.count,
  282. stream->props.u.bulk.buffersize);
  283. if (ret < 0)
  284. return ret;
  285. return usb_urb_alloc_bulk_urbs(stream);
  286. case USB_ISOC:
  287. ret = usb_alloc_stream_buffers(stream, stream->props.count,
  288. stream->props.u.isoc.framesize *
  289. stream->props.u.isoc.framesperurb);
  290. if (ret < 0)
  291. return ret;
  292. return usb_urb_alloc_isoc_urbs(stream);
  293. default:
  294. dev_err(&stream->udev->dev,
  295. "%s: unknown urb-type for data transfer\n",
  296. KBUILD_MODNAME);
  297. return -EINVAL;
  298. }
  299. }
  300. int usb_urb_exitv2(struct usb_data_stream *stream)
  301. {
  302. usb_urb_free_urbs(stream);
  303. usb_free_stream_buffers(stream);
  304. return 0;
  305. }