pvrusb2-dvb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
  3. *
  4. * Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <linux/kthread.h>
  21. #include "dvbdev.h"
  22. #include "pvrusb2-hdw-internal.h"
  23. #include "pvrusb2-hdw.h"
  24. #include "pvrusb2-dvb.h"
  25. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  26. static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
  27. {
  28. struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
  29. int newfeedcount, ret = 0;
  30. if (adap == NULL)
  31. return -ENODEV;
  32. mutex_lock(&adap->lock);
  33. newfeedcount = adap->feedcount + (onoff ? 1 : -1);
  34. if (newfeedcount == 0) {
  35. printk(KERN_DEBUG "stop feeding\n");
  36. ret = kthread_stop(adap->thread);
  37. adap->thread = NULL;
  38. }
  39. adap->feedcount = newfeedcount;
  40. if (adap->feedcount == onoff && adap->feedcount > 0) {
  41. if (NULL != adap->thread)
  42. goto fail;
  43. printk(KERN_DEBUG "start feeding\n");
  44. if (IS_ERR(adap->thread)) {
  45. ret = PTR_ERR(adap->thread);
  46. adap->thread = NULL;
  47. }
  48. //ret = newfeedcount;
  49. }
  50. fail:
  51. mutex_unlock(&adap->lock);
  52. return ret;
  53. }
  54. static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  55. {
  56. printk(KERN_DEBUG "start pid: 0x%04x, feedtype: %d\n",
  57. dvbdmxfeed->pid, dvbdmxfeed->type);
  58. return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
  59. }
  60. static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  61. {
  62. printk(KERN_DEBUG "stop pid: 0x%04x, feedtype: %d\n",
  63. dvbdmxfeed->pid, dvbdmxfeed->type);
  64. return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
  65. }
  66. static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
  67. {
  68. /* TO DO: This function will call into the core and request for
  69. * input to be set to 'dtv' if (acquire) and if it isn't set already.
  70. *
  71. * If (!acquire) then we should do nothing -- don't switch inputs
  72. * again unless the analog side of the driver requests the bus.
  73. */
  74. return 0;
  75. }
  76. static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
  77. {
  78. int ret;
  79. ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
  80. THIS_MODULE/*&hdw->usb_dev->owner*/,
  81. &adap->pvr->hdw->usb_dev->dev,
  82. adapter_nr);
  83. if (ret < 0) {
  84. err("dvb_register_adapter failed: error %d", ret);
  85. goto err;
  86. }
  87. adap->dvb_adap.priv = adap;
  88. adap->demux.dmx.capabilities = DMX_TS_FILTERING |
  89. DMX_SECTION_FILTERING |
  90. DMX_MEMORY_BASED_FILTERING;
  91. adap->demux.priv = adap;
  92. adap->demux.filternum = 256;
  93. adap->demux.feednum = 256;
  94. adap->demux.start_feed = pvr2_dvb_start_feed;
  95. adap->demux.stop_feed = pvr2_dvb_stop_feed;
  96. adap->demux.write_to_decoder = NULL;
  97. ret = dvb_dmx_init(&adap->demux);
  98. if (ret < 0) {
  99. err("dvb_dmx_init failed: error %d", ret);
  100. goto err_dmx;
  101. }
  102. adap->dmxdev.filternum = adap->demux.filternum;
  103. adap->dmxdev.demux = &adap->demux.dmx;
  104. adap->dmxdev.capabilities = 0;
  105. ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
  106. if (ret < 0) {
  107. err("dvb_dmxdev_init failed: error %d", ret);
  108. goto err_dmx_dev;
  109. }
  110. dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
  111. adap->digital_up = 1;
  112. return 0;
  113. err_dmx_dev:
  114. dvb_dmx_release(&adap->demux);
  115. err_dmx:
  116. dvb_unregister_adapter(&adap->dvb_adap);
  117. err:
  118. return ret;
  119. }
  120. static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
  121. {
  122. if (adap->digital_up) {
  123. printk(KERN_DEBUG "unregistering DVB devices\n");
  124. dvb_net_release(&adap->dvb_net);
  125. adap->demux.dmx.close(&adap->demux.dmx);
  126. dvb_dmxdev_release(&adap->dmxdev);
  127. dvb_dmx_release(&adap->demux);
  128. dvb_unregister_adapter(&adap->dvb_adap);
  129. adap->digital_up = 0;
  130. }
  131. return 0;
  132. }
  133. static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
  134. {
  135. struct pvr2_dvb_props *dvb_props = adap->pvr->hdw->hdw_desc->dvb_props;
  136. if (dvb_props == NULL) {
  137. err("fe_props not defined!");
  138. return -EINVAL;
  139. }
  140. if (dvb_props->frontend_attach == NULL) {
  141. err("frontend_attach not defined!");
  142. return -EINVAL;
  143. }
  144. if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
  145. if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
  146. err("frontend registration failed!");
  147. dvb_frontend_detach(adap->fe);
  148. adap->fe = NULL;
  149. return -ENODEV;
  150. }
  151. if (dvb_props->tuner_attach)
  152. dvb_props->tuner_attach(adap);
  153. if (adap->fe->ops.analog_ops.standby)
  154. adap->fe->ops.analog_ops.standby(adap->fe);
  155. /* Ensure all frontends negotiate bus access */
  156. adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
  157. } else {
  158. err("no frontend was attached!");
  159. return -ENODEV;
  160. }
  161. return 0;
  162. }
  163. static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
  164. {
  165. if (adap->fe != NULL) {
  166. dvb_unregister_frontend(adap->fe);
  167. dvb_frontend_detach(adap->fe);
  168. }
  169. return 0;
  170. }
  171. int pvr2_dvb_init(struct pvr2_context *pvr)
  172. {
  173. int ret = 0;
  174. pvr->hdw->dvb.pvr = pvr;
  175. mutex_init(&pvr->hdw->dvb.lock);
  176. ret = pvr2_dvb_adapter_init(&pvr->hdw->dvb);
  177. if (ret < 0)
  178. goto fail;
  179. ret = pvr2_dvb_frontend_init(&pvr->hdw->dvb);
  180. fail:
  181. return ret;
  182. }
  183. int pvr2_dvb_exit(struct pvr2_context *pvr)
  184. {
  185. pvr2_dvb_frontend_exit(&pvr->hdw->dvb);
  186. pvr2_dvb_adapter_exit(&pvr->hdw->dvb);
  187. pvr->hdw->dvb.pvr = NULL;
  188. return 0;
  189. }