dvb-usb-dvb.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* dvb-usb-dvb.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 contains functions for initializing and handling the
  7. * linux-dvb API.
  8. */
  9. #include "dvb-usb-common.h"
  10. /* does the complete input transfer handling */
  11. static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
  12. {
  13. struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
  14. int newfeedcount, ret;
  15. if (adap == NULL)
  16. return -ENODEV;
  17. if ((adap->active_fe < 0) ||
  18. (adap->active_fe >= adap->num_frontends_initialized)) {
  19. return -EINVAL;
  20. }
  21. newfeedcount = adap->feedcount + (onoff ? 1 : -1);
  22. /* stop feed before setting a new pid if there will be no pid anymore */
  23. if (newfeedcount == 0) {
  24. deb_ts("stop feeding\n");
  25. usb_urb_kill(&adap->fe_adap[adap->active_fe].stream);
  26. if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
  27. ret = adap->props.fe[adap->active_fe].streaming_ctrl(adap, 0);
  28. if (ret < 0) {
  29. err("error while stopping stream.");
  30. return ret;
  31. }
  32. }
  33. }
  34. adap->feedcount = newfeedcount;
  35. /* activate the pid on the device specific pid_filter */
  36. deb_ts("setting pid (%s): %5d %04x at index %d '%s'\n",
  37. adap->fe_adap[adap->active_fe].pid_filtering ?
  38. "yes" : "no", dvbdmxfeed->pid, dvbdmxfeed->pid,
  39. dvbdmxfeed->index, onoff ? "on" : "off");
  40. if (adap->props.fe[adap->active_fe].caps & DVB_USB_ADAP_HAS_PID_FILTER &&
  41. adap->fe_adap[adap->active_fe].pid_filtering &&
  42. adap->props.fe[adap->active_fe].pid_filter != NULL)
  43. adap->props.fe[adap->active_fe].pid_filter(adap, dvbdmxfeed->index, dvbdmxfeed->pid, onoff);
  44. /* start the feed if this was the first feed and there is still a feed
  45. * for reception.
  46. */
  47. if (adap->feedcount == onoff && adap->feedcount > 0) {
  48. deb_ts("submitting all URBs\n");
  49. usb_urb_submit(&adap->fe_adap[adap->active_fe].stream);
  50. deb_ts("controlling pid parser\n");
  51. if (adap->props.fe[adap->active_fe].caps & DVB_USB_ADAP_HAS_PID_FILTER &&
  52. adap->props.fe[adap->active_fe].caps &
  53. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF &&
  54. adap->props.fe[adap->active_fe].pid_filter_ctrl != NULL) {
  55. ret = adap->props.fe[adap->active_fe].pid_filter_ctrl(adap,
  56. adap->fe_adap[adap->active_fe].pid_filtering);
  57. if (ret < 0) {
  58. err("could not handle pid_parser");
  59. return ret;
  60. }
  61. }
  62. deb_ts("start feeding\n");
  63. if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
  64. ret = adap->props.fe[adap->active_fe].streaming_ctrl(adap, 1);
  65. if (ret < 0) {
  66. err("error while enabling fifo.");
  67. return ret;
  68. }
  69. }
  70. }
  71. return 0;
  72. }
  73. static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  74. {
  75. deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type);
  76. return dvb_usb_ctrl_feed(dvbdmxfeed,1);
  77. }
  78. static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  79. {
  80. deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
  81. return dvb_usb_ctrl_feed(dvbdmxfeed,0);
  82. }
  83. int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums)
  84. {
  85. int i;
  86. int ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->desc->name,
  87. adap->dev->owner, &adap->dev->udev->dev,
  88. adapter_nums);
  89. if (ret < 0) {
  90. deb_info("dvb_register_adapter failed: error %d", ret);
  91. goto err;
  92. }
  93. adap->dvb_adap.priv = adap;
  94. adap->dvb_adap.fe_ioctl_override = adap->props.fe_ioctl_override;
  95. if (adap->dev->props.read_mac_address) {
  96. if (adap->dev->props.read_mac_address(adap->dev,adap->dvb_adap.proposed_mac) == 0)
  97. info("MAC address: %pM",adap->dvb_adap.proposed_mac);
  98. else
  99. err("MAC address reading failed.");
  100. }
  101. adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
  102. adap->demux.priv = adap;
  103. adap->demux.filternum = 0;
  104. for (i = 0; i < adap->props.num_frontends; i++) {
  105. if (adap->demux.filternum < adap->fe_adap[i].max_feed_count)
  106. adap->demux.filternum = adap->fe_adap[i].max_feed_count;
  107. }
  108. adap->demux.feednum = adap->demux.filternum;
  109. adap->demux.start_feed = dvb_usb_start_feed;
  110. adap->demux.stop_feed = dvb_usb_stop_feed;
  111. adap->demux.write_to_decoder = NULL;
  112. if ((ret = dvb_dmx_init(&adap->demux)) < 0) {
  113. err("dvb_dmx_init failed: error %d",ret);
  114. goto err_dmx;
  115. }
  116. adap->dmxdev.filternum = adap->demux.filternum;
  117. adap->dmxdev.demux = &adap->demux.dmx;
  118. adap->dmxdev.capabilities = 0;
  119. if ((ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap)) < 0) {
  120. err("dvb_dmxdev_init failed: error %d",ret);
  121. goto err_dmx_dev;
  122. }
  123. if ((ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net,
  124. &adap->demux.dmx)) < 0) {
  125. err("dvb_net_init failed: error %d",ret);
  126. goto err_net_init;
  127. }
  128. adap->state |= DVB_USB_ADAP_STATE_DVB;
  129. return 0;
  130. err_net_init:
  131. dvb_dmxdev_release(&adap->dmxdev);
  132. err_dmx_dev:
  133. dvb_dmx_release(&adap->demux);
  134. err_dmx:
  135. dvb_unregister_adapter(&adap->dvb_adap);
  136. err:
  137. return ret;
  138. }
  139. int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap)
  140. {
  141. if (adap->state & DVB_USB_ADAP_STATE_DVB) {
  142. deb_info("unregistering DVB part\n");
  143. dvb_net_release(&adap->dvb_net);
  144. adap->demux.dmx.close(&adap->demux.dmx);
  145. dvb_dmxdev_release(&adap->dmxdev);
  146. dvb_dmx_release(&adap->demux);
  147. dvb_unregister_adapter(&adap->dvb_adap);
  148. adap->state &= ~DVB_USB_ADAP_STATE_DVB;
  149. }
  150. return 0;
  151. }
  152. static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
  153. {
  154. struct dvb_usb_adapter *adap = fe->dvb->priv;
  155. int ret = (adap->props.frontend_ctrl) ?
  156. adap->props.frontend_ctrl(fe, onoff) : 0;
  157. if (ret < 0) {
  158. err("frontend_ctrl request failed");
  159. return ret;
  160. }
  161. if (onoff)
  162. adap->active_fe = fe->id;
  163. return 0;
  164. }
  165. static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
  166. {
  167. struct dvb_usb_adapter *adap = fe->dvb->priv;
  168. dvb_usb_device_power_ctrl(adap->dev, 1);
  169. dvb_usb_set_active_fe(fe, 1);
  170. if (adap->fe_adap[fe->id].fe_init)
  171. adap->fe_adap[fe->id].fe_init(fe);
  172. return 0;
  173. }
  174. static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
  175. {
  176. struct dvb_usb_adapter *adap = fe->dvb->priv;
  177. if (adap->fe_adap[fe->id].fe_sleep)
  178. adap->fe_adap[fe->id].fe_sleep(fe);
  179. dvb_usb_set_active_fe(fe, 0);
  180. return dvb_usb_device_power_ctrl(adap->dev, 0);
  181. }
  182. int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
  183. {
  184. int ret, i;
  185. /* register all given adapter frontends */
  186. for (i = 0; i < adap->props.num_frontends; i++) {
  187. if (adap->props.fe[i].frontend_attach == NULL) {
  188. err("strange: '%s' #%d,%d "
  189. "doesn't want to attach a frontend.",
  190. adap->dev->desc->name, adap->id, i);
  191. return 0;
  192. }
  193. ret = adap->props.fe[i].frontend_attach(adap);
  194. if (ret || adap->fe_adap[i].fe == NULL) {
  195. /* only print error when there is no FE at all */
  196. if (i == 0)
  197. err("no frontend was attached by '%s'",
  198. adap->dev->desc->name);
  199. return 0;
  200. }
  201. adap->fe_adap[i].fe->id = i;
  202. /* re-assign sleep and wakeup functions */
  203. adap->fe_adap[i].fe_init = adap->fe_adap[i].fe->ops.init;
  204. adap->fe_adap[i].fe->ops.init = dvb_usb_fe_wakeup;
  205. adap->fe_adap[i].fe_sleep = adap->fe_adap[i].fe->ops.sleep;
  206. adap->fe_adap[i].fe->ops.sleep = dvb_usb_fe_sleep;
  207. if (dvb_register_frontend(&adap->dvb_adap, adap->fe_adap[i].fe)) {
  208. err("Frontend %d registration failed.", i);
  209. dvb_frontend_detach(adap->fe_adap[i].fe);
  210. adap->fe_adap[i].fe = NULL;
  211. /* In error case, do not try register more FEs,
  212. * still leaving already registered FEs alive. */
  213. if (i == 0)
  214. return -ENODEV;
  215. else
  216. return 0;
  217. }
  218. /* only attach the tuner if the demod is there */
  219. if (adap->props.fe[i].tuner_attach != NULL)
  220. adap->props.fe[i].tuner_attach(adap);
  221. adap->num_frontends_initialized++;
  222. }
  223. return 0;
  224. }
  225. int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap)
  226. {
  227. int i = adap->num_frontends_initialized - 1;
  228. /* unregister all given adapter frontends */
  229. for (; i >= 0; i--) {
  230. if (adap->fe_adap[i].fe != NULL) {
  231. dvb_unregister_frontend(adap->fe_adap[i].fe);
  232. dvb_frontend_detach(adap->fe_adap[i].fe);
  233. }
  234. }
  235. adap->num_frontends_initialized = 0;
  236. return 0;
  237. }