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