pvrusb2-dvb.c 10.0 KB

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