pvrusb2-dvb.c 11 KB

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