pvrusb2-dvb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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] = 0;
  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 = 0;
  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)) != 0) {
  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. /* TO DO: This function will call into the core and request for
  206. * input to be set to 'dtv' if (acquire) and if it isn't set already.
  207. *
  208. * If (!acquire) then we should do nothing -- don't switch inputs
  209. * again unless the analog side of the driver requests the bus.
  210. */
  211. return 0;
  212. }
  213. static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
  214. {
  215. int ret;
  216. ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
  217. THIS_MODULE/*&hdw->usb_dev->owner*/,
  218. &adap->channel.hdw->usb_dev->dev,
  219. adapter_nr);
  220. if (ret < 0) {
  221. err("dvb_register_adapter failed: error %d", ret);
  222. goto err;
  223. }
  224. adap->dvb_adap.priv = adap;
  225. adap->demux.dmx.capabilities = DMX_TS_FILTERING |
  226. DMX_SECTION_FILTERING |
  227. DMX_MEMORY_BASED_FILTERING;
  228. adap->demux.priv = adap;
  229. adap->demux.filternum = 256;
  230. adap->demux.feednum = 256;
  231. adap->demux.start_feed = pvr2_dvb_start_feed;
  232. adap->demux.stop_feed = pvr2_dvb_stop_feed;
  233. adap->demux.write_to_decoder = NULL;
  234. ret = dvb_dmx_init(&adap->demux);
  235. if (ret < 0) {
  236. err("dvb_dmx_init failed: error %d", ret);
  237. goto err_dmx;
  238. }
  239. adap->dmxdev.filternum = adap->demux.filternum;
  240. adap->dmxdev.demux = &adap->demux.dmx;
  241. adap->dmxdev.capabilities = 0;
  242. ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
  243. if (ret < 0) {
  244. err("dvb_dmxdev_init failed: error %d", ret);
  245. goto err_dmx_dev;
  246. }
  247. dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
  248. return 0;
  249. err_dmx_dev:
  250. dvb_dmx_release(&adap->demux);
  251. err_dmx:
  252. dvb_unregister_adapter(&adap->dvb_adap);
  253. err:
  254. return ret;
  255. }
  256. static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
  257. {
  258. printk(KERN_DEBUG "unregistering DVB devices\n");
  259. dvb_net_release(&adap->dvb_net);
  260. adap->demux.dmx.close(&adap->demux.dmx);
  261. dvb_dmxdev_release(&adap->dmxdev);
  262. dvb_dmx_release(&adap->demux);
  263. dvb_unregister_adapter(&adap->dvb_adap);
  264. return 0;
  265. }
  266. static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
  267. {
  268. struct pvr2_hdw *hdw = adap->channel.hdw;
  269. struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
  270. int ret;
  271. if (dvb_props == NULL) {
  272. err("fe_props not defined!");
  273. return -EINVAL;
  274. }
  275. /* FIXME: This code should be moved into the core,
  276. * and should only be called if we don't already have
  277. * control of the bus.
  278. *
  279. * We can't call "pvr2_dvb_bus_ctrl(adap->fe, 1)" from here,
  280. * because adap->fe isn't defined yet.
  281. */
  282. ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_by_id(hdw,
  283. PVR2_CID_INPUT),
  284. PVR2_CVAL_INPUT_DTV);
  285. if (ret != 0)
  286. return ret;
  287. pvr2_hdw_commit_ctl(hdw);
  288. if (dvb_props->frontend_attach == NULL) {
  289. err("frontend_attach not defined!");
  290. return -EINVAL;
  291. }
  292. if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
  293. if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
  294. err("frontend registration failed!");
  295. dvb_frontend_detach(adap->fe);
  296. adap->fe = NULL;
  297. return -ENODEV;
  298. }
  299. if (dvb_props->tuner_attach)
  300. dvb_props->tuner_attach(adap);
  301. if (adap->fe->ops.analog_ops.standby)
  302. adap->fe->ops.analog_ops.standby(adap->fe);
  303. /* Ensure all frontends negotiate bus access */
  304. adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
  305. } else {
  306. err("no frontend was attached!");
  307. return -ENODEV;
  308. }
  309. return 0;
  310. }
  311. static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
  312. {
  313. if (adap->fe != NULL) {
  314. dvb_unregister_frontend(adap->fe);
  315. dvb_frontend_detach(adap->fe);
  316. }
  317. return 0;
  318. }
  319. static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
  320. {
  321. pvr2_dvb_stream_end(adap);
  322. pvr2_dvb_frontend_exit(adap);
  323. pvr2_dvb_adapter_exit(adap);
  324. pvr2_channel_done(&adap->channel);
  325. kfree(adap);
  326. }
  327. static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
  328. {
  329. struct pvr2_dvb_adapter *adap;
  330. adap = container_of(chp, struct pvr2_dvb_adapter, channel);
  331. if (!adap->channel.mc_head->disconnect_flag) return;
  332. pvr2_dvb_destroy(adap);
  333. }
  334. struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
  335. {
  336. int ret = 0;
  337. struct pvr2_dvb_adapter *adap;
  338. if (!pvr->hdw->hdw_desc->dvb_props) {
  339. /* Device lacks a digital interface so don't set up
  340. the DVB side of the driver either. For now. */
  341. return NULL;
  342. }
  343. adap = kzalloc(sizeof(*adap), GFP_KERNEL);
  344. if (!adap) return adap;
  345. pvr2_channel_init(&adap->channel, pvr);
  346. adap->channel.check_func = pvr2_dvb_internal_check;
  347. init_waitqueue_head(&adap->buffer_wait_data);
  348. mutex_init(&adap->lock);
  349. ret = pvr2_dvb_adapter_init(adap);
  350. if (ret < 0) goto fail1;
  351. ret = pvr2_dvb_frontend_init(adap);
  352. if (ret < 0) goto fail2;
  353. return adap;
  354. fail2:
  355. pvr2_dvb_adapter_exit(adap);
  356. fail1:
  357. pvr2_channel_done(&adap->channel);
  358. return NULL;
  359. }