pvrusb2-dvb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 <linux/freezer.h>
  22. #include "dvbdev.h"
  23. #include "pvrusb2-hdw-internal.h"
  24. #include "pvrusb2-hdw.h"
  25. #include "pvrusb2-io.h"
  26. #include "pvrusb2-dvb.h"
  27. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  28. static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
  29. {
  30. int ret;
  31. unsigned int count;
  32. struct pvr2_buffer *bp;
  33. struct pvr2_stream *stream;
  34. printk(KERN_DEBUG "dvb thread started\n");
  35. set_freezable();
  36. stream = adap->channel.stream->stream;
  37. for (;;) {
  38. if (kthread_should_stop()) break;
  39. /* Not sure about this... */
  40. try_to_freeze();
  41. bp = pvr2_stream_get_ready_buffer(stream);
  42. if (bp != NULL) {
  43. count = pvr2_buffer_get_count(bp);
  44. if (count) {
  45. dvb_dmx_swfilter(
  46. &adap->demux,
  47. adap->buffer_storage[
  48. pvr2_buffer_get_id(bp)],
  49. count);
  50. } else {
  51. ret = pvr2_buffer_get_status(bp);
  52. if (ret < 0) break;
  53. }
  54. ret = pvr2_buffer_queue(bp);
  55. if (ret < 0) break;
  56. /* Since we know we did something to a buffer,
  57. just go back and try again. No point in
  58. blocking unless we really ran out of
  59. buffers to process. */
  60. continue;
  61. }
  62. /* Wait until more buffers become available or we're
  63. told not to wait any longer. */
  64. ret = wait_event_interruptible(
  65. adap->buffer_wait_data,
  66. (pvr2_stream_get_ready_count(stream) > 0) ||
  67. kthread_should_stop());
  68. if (ret < 0) break;
  69. }
  70. /* If we get here and ret is < 0, then an error has occurred.
  71. Probably would be a good idea to communicate that to DVB core... */
  72. printk(KERN_DEBUG "dvb thread stopped\n");
  73. return 0;
  74. }
  75. static int pvr2_dvb_feed_thread(void *data)
  76. {
  77. int stat = pvr2_dvb_feed_func(data);
  78. /* from videobuf-dvb.c: */
  79. while (!kthread_should_stop()) {
  80. set_current_state(TASK_INTERRUPTIBLE);
  81. schedule();
  82. }
  83. return stat;
  84. }
  85. static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
  86. {
  87. wake_up(&adap->buffer_wait_data);
  88. }
  89. static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
  90. {
  91. unsigned int idx;
  92. struct pvr2_stream *stream;
  93. if (adap->thread) {
  94. kthread_stop(adap->thread);
  95. adap->thread = NULL;
  96. }
  97. if (adap->channel.stream) {
  98. stream = adap->channel.stream->stream;
  99. } else {
  100. stream = NULL;
  101. }
  102. if (stream) {
  103. pvr2_hdw_set_streaming(adap->channel.hdw, 0);
  104. pvr2_stream_set_callback(stream, NULL, NULL);
  105. pvr2_stream_kill(stream);
  106. pvr2_stream_set_buffer_count(stream, 0);
  107. pvr2_channel_claim_stream(&adap->channel, NULL);
  108. }
  109. if (adap->stream_run) {
  110. for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
  111. if (!(adap->buffer_storage[idx])) continue;
  112. kfree(adap->buffer_storage[idx]);
  113. adap->buffer_storage[idx] = NULL;
  114. }
  115. adap->stream_run = 0;
  116. }
  117. }
  118. static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
  119. {
  120. struct pvr2_context *pvr = adap->channel.mc_head;
  121. unsigned int idx;
  122. int ret;
  123. struct pvr2_buffer *bp;
  124. struct pvr2_stream *stream = NULL;
  125. if (adap->stream_run) return -EIO;
  126. ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
  127. /* somebody else already has the stream */
  128. if (ret < 0) return ret;
  129. stream = adap->channel.stream->stream;
  130. for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
  131. adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
  132. GFP_KERNEL);
  133. if (!(adap->buffer_storage[idx])) return -ENOMEM;
  134. }
  135. pvr2_stream_set_callback(pvr->video_stream.stream,
  136. (pvr2_stream_callback) pvr2_dvb_notify, adap);
  137. ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
  138. if (ret < 0) return ret;
  139. for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
  140. bp = pvr2_stream_get_buffer(stream, idx);
  141. pvr2_buffer_set_buffer(bp,
  142. adap->buffer_storage[idx],
  143. PVR2_DVB_BUFFER_SIZE);
  144. }
  145. ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
  146. if (ret < 0) return ret;
  147. while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
  148. ret = pvr2_buffer_queue(bp);
  149. if (ret < 0) return ret;
  150. }
  151. adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
  152. if (IS_ERR(adap->thread)) {
  153. ret = PTR_ERR(adap->thread);
  154. adap->thread = NULL;
  155. return ret;
  156. }
  157. adap->stream_run = !0;
  158. return 0;
  159. }
  160. static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
  161. {
  162. int ret = pvr2_dvb_stream_do_start(adap);
  163. if (ret < 0) pvr2_dvb_stream_end(adap);
  164. return ret;
  165. }
  166. static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
  167. {
  168. struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
  169. int ret = 0;
  170. if (adap == NULL) return -ENODEV;
  171. mutex_lock(&adap->lock);
  172. do {
  173. if (onoff) {
  174. if (!adap->feedcount) {
  175. printk(KERN_DEBUG "start feeding\n");
  176. ret = pvr2_dvb_stream_start(adap);
  177. if (ret < 0) break;
  178. }
  179. (adap->feedcount)++;
  180. } else if (adap->feedcount > 0) {
  181. (adap->feedcount)--;
  182. if (!adap->feedcount) {
  183. printk(KERN_DEBUG "stop feeding\n");
  184. pvr2_dvb_stream_end(adap);
  185. }
  186. }
  187. } while (0);
  188. mutex_unlock(&adap->lock);
  189. return ret;
  190. }
  191. static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  192. {
  193. printk(KERN_DEBUG "start pid: 0x%04x, feedtype: %d\n",
  194. dvbdmxfeed->pid, dvbdmxfeed->type);
  195. return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
  196. }
  197. static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  198. {
  199. printk(KERN_DEBUG "stop pid: 0x%04x, feedtype: %d\n",
  200. dvbdmxfeed->pid, dvbdmxfeed->type);
  201. return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
  202. }
  203. static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
  204. {
  205. struct pvr2_dvb_adapter *adap = fe->dvb->priv;
  206. return pvr2_channel_limit_inputs(
  207. &adap->channel,
  208. (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
  209. }
  210. static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
  211. {
  212. int ret;
  213. ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
  214. THIS_MODULE/*&hdw->usb_dev->owner*/,
  215. &adap->channel.hdw->usb_dev->dev,
  216. adapter_nr);
  217. if (ret < 0) {
  218. err("dvb_register_adapter failed: error %d", ret);
  219. goto err;
  220. }
  221. adap->dvb_adap.priv = adap;
  222. adap->demux.dmx.capabilities = DMX_TS_FILTERING |
  223. DMX_SECTION_FILTERING |
  224. DMX_MEMORY_BASED_FILTERING;
  225. adap->demux.priv = adap;
  226. adap->demux.filternum = 256;
  227. adap->demux.feednum = 256;
  228. adap->demux.start_feed = pvr2_dvb_start_feed;
  229. adap->demux.stop_feed = pvr2_dvb_stop_feed;
  230. adap->demux.write_to_decoder = NULL;
  231. ret = dvb_dmx_init(&adap->demux);
  232. if (ret < 0) {
  233. err("dvb_dmx_init failed: error %d", ret);
  234. goto err_dmx;
  235. }
  236. adap->dmxdev.filternum = adap->demux.filternum;
  237. adap->dmxdev.demux = &adap->demux.dmx;
  238. adap->dmxdev.capabilities = 0;
  239. ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
  240. if (ret < 0) {
  241. err("dvb_dmxdev_init failed: error %d", ret);
  242. goto err_dmx_dev;
  243. }
  244. dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
  245. return 0;
  246. err_dmx_dev:
  247. dvb_dmx_release(&adap->demux);
  248. err_dmx:
  249. dvb_unregister_adapter(&adap->dvb_adap);
  250. err:
  251. return ret;
  252. }
  253. static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
  254. {
  255. printk(KERN_DEBUG "unregistering DVB devices\n");
  256. dvb_net_release(&adap->dvb_net);
  257. adap->demux.dmx.close(&adap->demux.dmx);
  258. dvb_dmxdev_release(&adap->dmxdev);
  259. dvb_dmx_release(&adap->demux);
  260. dvb_unregister_adapter(&adap->dvb_adap);
  261. return 0;
  262. }
  263. static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
  264. {
  265. struct pvr2_hdw *hdw = adap->channel.hdw;
  266. struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
  267. int ret = 0;
  268. if (dvb_props == NULL) {
  269. err("fe_props not defined!");
  270. return -EINVAL;
  271. }
  272. ret = pvr2_channel_limit_inputs(
  273. &adap->channel,
  274. (1 << PVR2_CVAL_INPUT_DTV));
  275. if (ret) {
  276. err("failed to grab control of dtv input (code=%d)",
  277. ret);
  278. return ret;
  279. }
  280. if (dvb_props->frontend_attach == NULL) {
  281. err("frontend_attach not defined!");
  282. ret = -EINVAL;
  283. goto done;
  284. }
  285. if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
  286. if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
  287. err("frontend registration failed!");
  288. dvb_frontend_detach(adap->fe);
  289. adap->fe = NULL;
  290. ret = -ENODEV;
  291. goto done;
  292. }
  293. if (dvb_props->tuner_attach)
  294. dvb_props->tuner_attach(adap);
  295. if (adap->fe->ops.analog_ops.standby)
  296. adap->fe->ops.analog_ops.standby(adap->fe);
  297. /* Ensure all frontends negotiate bus access */
  298. adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
  299. } else {
  300. err("no frontend was attached!");
  301. ret = -ENODEV;
  302. return ret;
  303. }
  304. done:
  305. pvr2_channel_limit_inputs(&adap->channel, 0);
  306. return ret;
  307. }
  308. static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
  309. {
  310. if (adap->fe != NULL) {
  311. dvb_unregister_frontend(adap->fe);
  312. dvb_frontend_detach(adap->fe);
  313. }
  314. return 0;
  315. }
  316. static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
  317. {
  318. pvr2_dvb_stream_end(adap);
  319. pvr2_dvb_frontend_exit(adap);
  320. pvr2_dvb_adapter_exit(adap);
  321. pvr2_channel_done(&adap->channel);
  322. kfree(adap);
  323. }
  324. static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
  325. {
  326. struct pvr2_dvb_adapter *adap;
  327. adap = container_of(chp, struct pvr2_dvb_adapter, channel);
  328. if (!adap->channel.mc_head->disconnect_flag) return;
  329. pvr2_dvb_destroy(adap);
  330. }
  331. struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
  332. {
  333. int ret = 0;
  334. struct pvr2_dvb_adapter *adap;
  335. if (!pvr->hdw->hdw_desc->dvb_props) {
  336. /* Device lacks a digital interface so don't set up
  337. the DVB side of the driver either. For now. */
  338. return NULL;
  339. }
  340. adap = kzalloc(sizeof(*adap), GFP_KERNEL);
  341. if (!adap) return adap;
  342. pvr2_channel_init(&adap->channel, pvr);
  343. adap->channel.check_func = pvr2_dvb_internal_check;
  344. init_waitqueue_head(&adap->buffer_wait_data);
  345. mutex_init(&adap->lock);
  346. ret = pvr2_dvb_adapter_init(adap);
  347. if (ret < 0) goto fail1;
  348. ret = pvr2_dvb_frontend_init(adap);
  349. if (ret < 0) goto fail2;
  350. return adap;
  351. fail2:
  352. pvr2_dvb_adapter_exit(adap);
  353. fail1:
  354. pvr2_channel_done(&adap->channel);
  355. return NULL;
  356. }