dvb-usb-urb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* dvb-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. * USB and URB stuff.
  9. */
  10. #include "dvb-usb-common.h"
  11. int dvb_usb_generic_rw(struct dvb_usb_device *d, u8 *wbuf, u16 wlen, u8 *rbuf,
  12. u16 rlen, int delay_ms)
  13. {
  14. int actlen = 0, ret = -ENOMEM;
  15. if (!d || wbuf == NULL || wlen == 0)
  16. return -EINVAL;
  17. if (d->props.generic_bulk_ctrl_endpoint == 0) {
  18. err("endpoint for generic control not specified.");
  19. return -EINVAL;
  20. }
  21. if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
  22. return ret;
  23. deb_xfer(">>> ");
  24. debug_dump(wbuf,wlen,deb_xfer);
  25. ret = usb_bulk_msg(d->udev,usb_sndbulkpipe(d->udev,
  26. d->props.generic_bulk_ctrl_endpoint), wbuf,wlen,&actlen,
  27. 2000);
  28. if (ret)
  29. err("bulk message failed: %d (%d/%d)",ret,wlen,actlen);
  30. else
  31. ret = actlen != wlen ? -1 : 0;
  32. /* an answer is expected, and no error before */
  33. if (!ret && rbuf && rlen) {
  34. if (delay_ms)
  35. msleep(delay_ms);
  36. ret = usb_bulk_msg(d->udev,usb_rcvbulkpipe(d->udev,
  37. d->props.generic_bulk_ctrl_endpoint_response ?
  38. d->props.generic_bulk_ctrl_endpoint_response :
  39. d->props.generic_bulk_ctrl_endpoint),rbuf,rlen,&actlen,
  40. 2000);
  41. if (ret)
  42. err("recv bulk message failed: %d",ret);
  43. else {
  44. deb_xfer("<<< ");
  45. debug_dump(rbuf,actlen,deb_xfer);
  46. }
  47. }
  48. mutex_unlock(&d->usb_mutex);
  49. return ret;
  50. }
  51. EXPORT_SYMBOL(dvb_usb_generic_rw);
  52. int dvb_usb_generic_write(struct dvb_usb_device *d, u8 *buf, u16 len)
  53. {
  54. return dvb_usb_generic_rw(d,buf,len,NULL,0,0);
  55. }
  56. EXPORT_SYMBOL(dvb_usb_generic_write);
  57. static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buffer, size_t length)
  58. {
  59. struct dvb_usb_adapter *adap = stream->user_priv;
  60. if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
  61. dvb_dmx_swfilter(&adap->demux, buffer, length);
  62. }
  63. static void dvb_usb_data_complete_204(struct usb_data_stream *stream, u8 *buffer, size_t length)
  64. {
  65. struct dvb_usb_adapter *adap = stream->user_priv;
  66. if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
  67. dvb_dmx_swfilter_204(&adap->demux, buffer, length);
  68. }
  69. static void dvb_usb_data_complete_raw(struct usb_data_stream *stream,
  70. u8 *buffer, size_t length)
  71. {
  72. struct dvb_usb_adapter *adap = stream->user_priv;
  73. if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
  74. dvb_dmx_swfilter_raw(&adap->demux, buffer, length);
  75. }
  76. int dvb_usb_adapter_stream_init(struct dvb_usb_adapter *adap)
  77. {
  78. int i, ret = 0;
  79. for (i = 0; i < adap->props.num_frontends; i++) {
  80. adap->fe_adap[i].stream.udev = adap->dev->udev;
  81. if (adap->props.fe[i].caps & DVB_USB_ADAP_RECEIVES_204_BYTE_TS)
  82. adap->fe_adap[i].stream.complete =
  83. dvb_usb_data_complete_204;
  84. else
  85. if (adap->props.fe[i].caps & DVB_USB_ADAP_RECEIVES_RAW_PAYLOAD)
  86. adap->fe_adap[i].stream.complete =
  87. dvb_usb_data_complete_raw;
  88. else
  89. adap->fe_adap[i].stream.complete = dvb_usb_data_complete;
  90. adap->fe_adap[i].stream.user_priv = adap;
  91. ret = usb_urb_init(&adap->fe_adap[i].stream,
  92. &adap->props.fe[i].stream);
  93. if (ret < 0)
  94. break;
  95. }
  96. return ret;
  97. }
  98. int dvb_usb_adapter_stream_exit(struct dvb_usb_adapter *adap)
  99. {
  100. int i;
  101. for (i = 0; i < adap->props.num_frontends; i++)
  102. usb_urb_exit(&adap->fe_adap[i].stream);
  103. return 0;
  104. }