dvb-dibusb-dvb.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * dvb-dibusb-dvb.c is part of the driver for mobile USB Budget DVB-T devices
  3. * based on reference design made by DiBcom (http://www.dibcom.fr/)
  4. *
  5. * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
  6. *
  7. * see dvb-dibusb-core.c for more copyright details.
  8. *
  9. * This file contains functions for initializing and handling the
  10. * linux-dvb API.
  11. */
  12. #include "dvb-dibusb.h"
  13. #include <linux/usb.h>
  14. #include <linux/version.h>
  15. static u32 urb_compl_count;
  16. /*
  17. * MPEG2 TS DVB stuff
  18. */
  19. void dibusb_urb_complete(struct urb *urb, struct pt_regs *ptregs)
  20. {
  21. struct usb_dibusb *dib = urb->context;
  22. deb_ts("urb complete feedcount: %d, status: %d, length: %d\n",dib->feedcount,urb->status,
  23. urb->actual_length);
  24. urb_compl_count++;
  25. if (urb_compl_count % 1000 == 0)
  26. deb_info("%d urbs completed so far.\n",urb_compl_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. deb_ts("urb completition error %d.", urb->status);
  37. break;
  38. }
  39. if (dib->feedcount > 0 && urb->actual_length > 0) {
  40. if (dib->init_state & DIBUSB_STATE_DVB)
  41. dvb_dmx_swfilter(&dib->demux, (u8*) urb->transfer_buffer,urb->actual_length);
  42. } else
  43. deb_ts("URB dropped because of feedcount.\n");
  44. usb_submit_urb(urb,GFP_ATOMIC);
  45. }
  46. static int dibusb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
  47. {
  48. struct usb_dibusb *dib = dvbdmxfeed->demux->priv;
  49. int newfeedcount;
  50. if (dib == NULL)
  51. return -ENODEV;
  52. newfeedcount = dib->feedcount + (onoff ? 1 : -1);
  53. /*
  54. * stop feed before setting a new pid if there will be no pid anymore
  55. */
  56. if (newfeedcount == 0) {
  57. deb_ts("stop feeding\n");
  58. if (dib->xfer_ops.fifo_ctrl != NULL) {
  59. if (dib->xfer_ops.fifo_ctrl(dib->fe,0)) {
  60. err("error while inhibiting fifo.");
  61. return -ENODEV;
  62. }
  63. }
  64. dibusb_streaming(dib,0);
  65. }
  66. dib->feedcount = newfeedcount;
  67. /* activate the pid on the device specific pid_filter */
  68. deb_ts("setting pid: %5d %04x at index %d '%s'\n",dvbdmxfeed->pid,dvbdmxfeed->pid,dvbdmxfeed->index,onoff ? "on" : "off");
  69. if (dib->pid_parse && dib->xfer_ops.pid_ctrl != NULL)
  70. dib->xfer_ops.pid_ctrl(dib->fe,dvbdmxfeed->index,dvbdmxfeed->pid,onoff);
  71. /*
  72. * start the feed if this was the first pid to set and there is still a pid
  73. * for reception.
  74. */
  75. if (dib->feedcount == onoff && dib->feedcount > 0) {
  76. deb_ts("controlling pid parser\n");
  77. if (dib->xfer_ops.pid_parse != NULL) {
  78. if (dib->xfer_ops.pid_parse(dib->fe,dib->pid_parse) < 0) {
  79. err("could not handle pid_parser");
  80. }
  81. }
  82. deb_ts("start feeding\n");
  83. if (dib->xfer_ops.fifo_ctrl != NULL) {
  84. if (dib->xfer_ops.fifo_ctrl(dib->fe,1)) {
  85. err("error while enabling fifo.");
  86. return -ENODEV;
  87. }
  88. }
  89. dibusb_streaming(dib,1);
  90. }
  91. return 0;
  92. }
  93. static int dibusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  94. {
  95. deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type);
  96. return dibusb_ctrl_feed(dvbdmxfeed,1);
  97. }
  98. static int dibusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  99. {
  100. deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
  101. return dibusb_ctrl_feed(dvbdmxfeed,0);
  102. }
  103. int dibusb_dvb_init(struct usb_dibusb *dib)
  104. {
  105. int ret;
  106. urb_compl_count = 0;
  107. if ((ret = dvb_register_adapter(&dib->adapter, DRIVER_DESC,
  108. THIS_MODULE)) < 0) {
  109. deb_info("dvb_register_adapter failed: error %d", ret);
  110. goto err;
  111. }
  112. dib->adapter.priv = dib;
  113. /* i2c is done in dibusb_i2c_init */
  114. dib->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
  115. dib->demux.priv = (void *)dib;
  116. /* get pidcount from demod */
  117. dib->demux.feednum = dib->demux.filternum = 255;
  118. dib->demux.start_feed = dibusb_start_feed;
  119. dib->demux.stop_feed = dibusb_stop_feed;
  120. dib->demux.write_to_decoder = NULL;
  121. if ((ret = dvb_dmx_init(&dib->demux)) < 0) {
  122. err("dvb_dmx_init failed: error %d",ret);
  123. goto err_dmx;
  124. }
  125. dib->dmxdev.filternum = dib->demux.filternum;
  126. dib->dmxdev.demux = &dib->demux.dmx;
  127. dib->dmxdev.capabilities = 0;
  128. if ((ret = dvb_dmxdev_init(&dib->dmxdev, &dib->adapter)) < 0) {
  129. err("dvb_dmxdev_init failed: error %d",ret);
  130. goto err_dmx_dev;
  131. }
  132. dvb_net_init(&dib->adapter, &dib->dvb_net, &dib->demux.dmx);
  133. goto success;
  134. err_dmx_dev:
  135. dvb_dmx_release(&dib->demux);
  136. err_dmx:
  137. dvb_unregister_adapter(&dib->adapter);
  138. err:
  139. return ret;
  140. success:
  141. dib->init_state |= DIBUSB_STATE_DVB;
  142. return 0;
  143. }
  144. int dibusb_dvb_exit(struct usb_dibusb *dib)
  145. {
  146. if (dib->init_state & DIBUSB_STATE_DVB) {
  147. dib->init_state &= ~DIBUSB_STATE_DVB;
  148. deb_info("unregistering DVB part\n");
  149. dvb_net_release(&dib->dvb_net);
  150. dib->demux.dmx.close(&dib->demux.dmx);
  151. dvb_dmxdev_release(&dib->dmxdev);
  152. dvb_dmx_release(&dib->demux);
  153. dvb_unregister_adapter(&dib->adapter);
  154. }
  155. return 0;
  156. }