pvrusb2-dvb.c 11 KB

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