dvb-usb-dvb.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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,
  76. dvbdmxfeed->type);
  77. return dvb_usb_ctrl_feed(dvbdmxfeed, 1);
  78. }
  79. static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  80. {
  81. deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
  82. return dvb_usb_ctrl_feed(dvbdmxfeed, 0);
  83. }
  84. static void dvb_usb_media_device_register(struct dvb_usb_adapter *adap)
  85. {
  86. #ifdef CONFIG_MEDIA_CONTROLLER_DVB
  87. struct media_device *mdev;
  88. struct dvb_usb_device *d = adap->dev;
  89. struct usb_device *udev = d->udev;
  90. int ret;
  91. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  92. if (!mdev)
  93. return;
  94. mdev->dev = &udev->dev;
  95. strlcpy(mdev->model, d->desc->name, sizeof(mdev->model));
  96. if (udev->serial)
  97. strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
  98. strcpy(mdev->bus_info, udev->devpath);
  99. mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
  100. mdev->driver_version = LINUX_VERSION_CODE;
  101. ret = media_device_register(mdev);
  102. if (ret) {
  103. dev_err(&d->udev->dev,
  104. "Couldn't create a media device. Error: %d\n",
  105. ret);
  106. kfree(mdev);
  107. return;
  108. }
  109. dvb_register_media_controller(&adap->dvb_adap, mdev);
  110. dev_info(&d->udev->dev, "media controller created\n");
  111. #endif
  112. }
  113. static void dvb_usb_media_device_unregister(struct dvb_usb_adapter *adap)
  114. {
  115. #ifdef CONFIG_MEDIA_CONTROLLER_DVB
  116. if (!adap->dvb_adap.mdev)
  117. return;
  118. media_device_unregister(adap->dvb_adap.mdev);
  119. kfree(adap->dvb_adap.mdev);
  120. adap->dvb_adap.mdev = NULL;
  121. #endif
  122. }
  123. int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums)
  124. {
  125. int i;
  126. int ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->desc->name,
  127. adap->dev->owner, &adap->dev->udev->dev,
  128. adapter_nums);
  129. if (ret < 0) {
  130. deb_info("dvb_register_adapter failed: error %d", ret);
  131. goto err;
  132. }
  133. adap->dvb_adap.priv = adap;
  134. dvb_usb_media_device_register(adap);
  135. if (adap->dev->props.read_mac_address) {
  136. if (adap->dev->props.read_mac_address(adap->dev, adap->dvb_adap.proposed_mac) == 0)
  137. info("MAC address: %pM", adap->dvb_adap.proposed_mac);
  138. else
  139. err("MAC address reading failed.");
  140. }
  141. adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
  142. adap->demux.priv = adap;
  143. adap->demux.filternum = 0;
  144. for (i = 0; i < adap->props.num_frontends; i++) {
  145. if (adap->demux.filternum < adap->fe_adap[i].max_feed_count)
  146. adap->demux.filternum = adap->fe_adap[i].max_feed_count;
  147. }
  148. adap->demux.feednum = adap->demux.filternum;
  149. adap->demux.start_feed = dvb_usb_start_feed;
  150. adap->demux.stop_feed = dvb_usb_stop_feed;
  151. adap->demux.write_to_decoder = NULL;
  152. if ((ret = dvb_dmx_init(&adap->demux)) < 0) {
  153. err("dvb_dmx_init failed: error %d", ret);
  154. goto err_dmx;
  155. }
  156. adap->dmxdev.filternum = adap->demux.filternum;
  157. adap->dmxdev.demux = &adap->demux.dmx;
  158. adap->dmxdev.capabilities = 0;
  159. if ((ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap)) < 0) {
  160. err("dvb_dmxdev_init failed: error %d", ret);
  161. goto err_dmx_dev;
  162. }
  163. if ((ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net,
  164. &adap->demux.dmx)) < 0) {
  165. err("dvb_net_init failed: error %d", ret);
  166. goto err_net_init;
  167. }
  168. adap->state |= DVB_USB_ADAP_STATE_DVB;
  169. return 0;
  170. err_net_init:
  171. dvb_dmxdev_release(&adap->dmxdev);
  172. err_dmx_dev:
  173. dvb_dmx_release(&adap->demux);
  174. err_dmx:
  175. dvb_usb_media_device_unregister(adap);
  176. dvb_unregister_adapter(&adap->dvb_adap);
  177. err:
  178. return ret;
  179. }
  180. int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap)
  181. {
  182. if (adap->state & DVB_USB_ADAP_STATE_DVB) {
  183. deb_info("unregistering DVB part\n");
  184. dvb_net_release(&adap->dvb_net);
  185. adap->demux.dmx.close(&adap->demux.dmx);
  186. dvb_dmxdev_release(&adap->dmxdev);
  187. dvb_dmx_release(&adap->demux);
  188. dvb_usb_media_device_unregister(adap);
  189. dvb_unregister_adapter(&adap->dvb_adap);
  190. adap->state &= ~DVB_USB_ADAP_STATE_DVB;
  191. }
  192. return 0;
  193. }
  194. static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
  195. {
  196. struct dvb_usb_adapter *adap = fe->dvb->priv;
  197. int ret = (adap->props.frontend_ctrl) ?
  198. adap->props.frontend_ctrl(fe, onoff) : 0;
  199. if (ret < 0) {
  200. err("frontend_ctrl request failed");
  201. return ret;
  202. }
  203. if (onoff)
  204. adap->active_fe = fe->id;
  205. return 0;
  206. }
  207. static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
  208. {
  209. struct dvb_usb_adapter *adap = fe->dvb->priv;
  210. dvb_usb_device_power_ctrl(adap->dev, 1);
  211. dvb_usb_set_active_fe(fe, 1);
  212. if (adap->fe_adap[fe->id].fe_init)
  213. adap->fe_adap[fe->id].fe_init(fe);
  214. return 0;
  215. }
  216. static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
  217. {
  218. struct dvb_usb_adapter *adap = fe->dvb->priv;
  219. if (adap->fe_adap[fe->id].fe_sleep)
  220. adap->fe_adap[fe->id].fe_sleep(fe);
  221. dvb_usb_set_active_fe(fe, 0);
  222. return dvb_usb_device_power_ctrl(adap->dev, 0);
  223. }
  224. int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
  225. {
  226. int ret, i;
  227. /* register all given adapter frontends */
  228. for (i = 0; i < adap->props.num_frontends; i++) {
  229. if (adap->props.fe[i].frontend_attach == NULL) {
  230. err("strange: '%s' #%d,%d "
  231. "doesn't want to attach a frontend.",
  232. adap->dev->desc->name, adap->id, i);
  233. return 0;
  234. }
  235. ret = adap->props.fe[i].frontend_attach(adap);
  236. if (ret || adap->fe_adap[i].fe == NULL) {
  237. /* only print error when there is no FE at all */
  238. if (i == 0)
  239. err("no frontend was attached by '%s'",
  240. adap->dev->desc->name);
  241. return 0;
  242. }
  243. adap->fe_adap[i].fe->id = i;
  244. /* re-assign sleep and wakeup functions */
  245. adap->fe_adap[i].fe_init = adap->fe_adap[i].fe->ops.init;
  246. adap->fe_adap[i].fe->ops.init = dvb_usb_fe_wakeup;
  247. adap->fe_adap[i].fe_sleep = adap->fe_adap[i].fe->ops.sleep;
  248. adap->fe_adap[i].fe->ops.sleep = dvb_usb_fe_sleep;
  249. if (dvb_register_frontend(&adap->dvb_adap, adap->fe_adap[i].fe)) {
  250. err("Frontend %d registration failed.", i);
  251. dvb_frontend_detach(adap->fe_adap[i].fe);
  252. adap->fe_adap[i].fe = NULL;
  253. /* In error case, do not try register more FEs,
  254. * still leaving already registered FEs alive. */
  255. if (i == 0)
  256. return -ENODEV;
  257. else
  258. return 0;
  259. }
  260. /* only attach the tuner if the demod is there */
  261. if (adap->props.fe[i].tuner_attach != NULL)
  262. adap->props.fe[i].tuner_attach(adap);
  263. adap->num_frontends_initialized++;
  264. }
  265. dvb_create_media_graph(&adap->dvb_adap);
  266. return 0;
  267. }
  268. int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap)
  269. {
  270. int i = adap->num_frontends_initialized - 1;
  271. /* unregister all given adapter frontends */
  272. for (; i >= 0; i--) {
  273. if (adap->fe_adap[i].fe != NULL) {
  274. dvb_unregister_frontend(adap->fe_adap[i].fe);
  275. dvb_frontend_detach(adap->fe_adap[i].fe);
  276. }
  277. }
  278. adap->num_frontends_initialized = 0;
  279. return 0;
  280. }