dvb-usb-urb.c 3.3 KB

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