pvrusb2-dvb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/slab.h>
  23. #include <linux/mm.h>
  24. #include "dvbdev.h"
  25. #include "pvrusb2-debug.h"
  26. #include "pvrusb2-hdw-internal.h"
  27. #include "pvrusb2-hdw.h"
  28. #include "pvrusb2-io.h"
  29. #include "pvrusb2-dvb.h"
  30. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  31. static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
  32. {
  33. int ret;
  34. unsigned int count;
  35. struct pvr2_buffer *bp;
  36. struct pvr2_stream *stream;
  37. pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started");
  38. set_freezable();
  39. stream = adap->channel.stream->stream;
  40. for (;;) {
  41. if (kthread_should_stop()) break;
  42. /* Not sure about this... */
  43. try_to_freeze();
  44. bp = pvr2_stream_get_ready_buffer(stream);
  45. if (bp != NULL) {
  46. count = pvr2_buffer_get_count(bp);
  47. if (count) {
  48. dvb_dmx_swfilter(
  49. &adap->demux,
  50. adap->buffer_storage[
  51. pvr2_buffer_get_id(bp)],
  52. count);
  53. } else {
  54. ret = pvr2_buffer_get_status(bp);
  55. if (ret < 0) break;
  56. }
  57. ret = pvr2_buffer_queue(bp);
  58. if (ret < 0) break;
  59. /* Since we know we did something to a buffer,
  60. just go back and try again. No point in
  61. blocking unless we really ran out of
  62. buffers to process. */
  63. continue;
  64. }
  65. /* Wait until more buffers become available or we're
  66. told not to wait any longer. */
  67. ret = wait_event_interruptible(
  68. adap->buffer_wait_data,
  69. (pvr2_stream_get_ready_count(stream) > 0) ||
  70. kthread_should_stop());
  71. if (ret < 0) break;
  72. }
  73. /* If we get here and ret is < 0, then an error has occurred.
  74. Probably would be a good idea to communicate that to DVB core... */
  75. pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
  76. return 0;
  77. }
  78. static int pvr2_dvb_feed_thread(void *data)
  79. {
  80. int stat = pvr2_dvb_feed_func(data);
  81. /* from videobuf-dvb.c: */
  82. while (!kthread_should_stop()) {
  83. set_current_state(TASK_INTERRUPTIBLE);
  84. schedule();
  85. }
  86. return stat;
  87. }
  88. static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
  89. {
  90. wake_up(&adap->buffer_wait_data);
  91. }
  92. static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
  93. {
  94. unsigned int idx;
  95. struct pvr2_stream *stream;
  96. if (adap->thread) {
  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] = NULL;
  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 = NULL;
  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)) != NULL) {
  151. ret = pvr2_buffer_queue(bp);
  152. if (ret < 0) return ret;
  153. }
  154. adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
  155. if (IS_ERR(adap->thread)) {
  156. ret = PTR_ERR(adap->thread);
  157. adap->thread = NULL;
  158. return ret;
  159. }
  160. adap->stream_run = !0;
  161. return 0;
  162. }
  163. static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
  164. {
  165. int ret = pvr2_dvb_stream_do_start(adap);
  166. if (ret < 0) pvr2_dvb_stream_end(adap);
  167. return ret;
  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 ret = 0;
  173. if (adap == NULL) return -ENODEV;
  174. mutex_lock(&adap->lock);
  175. do {
  176. if (onoff) {
  177. if (!adap->feedcount) {
  178. pvr2_trace(PVR2_TRACE_DVB_FEED,
  179. "start feeding demux");
  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. pvr2_trace(PVR2_TRACE_DVB_FEED,
  188. "stop feeding demux");
  189. pvr2_dvb_stream_end(adap);
  190. }
  191. }
  192. } while (0);
  193. mutex_unlock(&adap->lock);
  194. return ret;
  195. }
  196. static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  197. {
  198. pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
  199. return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
  200. }
  201. static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  202. {
  203. pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
  204. return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
  205. }
  206. static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
  207. {
  208. struct pvr2_dvb_adapter *adap = fe->dvb->priv;
  209. return pvr2_channel_limit_inputs(
  210. &adap->channel,
  211. (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 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. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  222. "dvb_register_adapter failed: error %d", ret);
  223. goto err;
  224. }
  225. adap->dvb_adap.priv = adap;
  226. adap->demux.dmx.capabilities = DMX_TS_FILTERING |
  227. DMX_SECTION_FILTERING |
  228. DMX_MEMORY_BASED_FILTERING;
  229. adap->demux.priv = adap;
  230. adap->demux.filternum = 256;
  231. adap->demux.feednum = 256;
  232. adap->demux.start_feed = pvr2_dvb_start_feed;
  233. adap->demux.stop_feed = pvr2_dvb_stop_feed;
  234. adap->demux.write_to_decoder = NULL;
  235. ret = dvb_dmx_init(&adap->demux);
  236. if (ret < 0) {
  237. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  238. "dvb_dmx_init failed: error %d", ret);
  239. goto err_dmx;
  240. }
  241. adap->dmxdev.filternum = adap->demux.filternum;
  242. adap->dmxdev.demux = &adap->demux.dmx;
  243. adap->dmxdev.capabilities = 0;
  244. ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
  245. if (ret < 0) {
  246. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  247. "dvb_dmxdev_init failed: error %d", ret);
  248. goto err_dmx_dev;
  249. }
  250. dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
  251. return 0;
  252. err_dmx_dev:
  253. dvb_dmx_release(&adap->demux);
  254. err_dmx:
  255. dvb_unregister_adapter(&adap->dvb_adap);
  256. err:
  257. return ret;
  258. }
  259. static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
  260. {
  261. pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices");
  262. dvb_net_release(&adap->dvb_net);
  263. adap->demux.dmx.close(&adap->demux.dmx);
  264. dvb_dmxdev_release(&adap->dmxdev);
  265. dvb_dmx_release(&adap->demux);
  266. dvb_unregister_adapter(&adap->dvb_adap);
  267. return 0;
  268. }
  269. static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
  270. {
  271. struct pvr2_hdw *hdw = adap->channel.hdw;
  272. const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
  273. int ret = 0;
  274. if (dvb_props == NULL) {
  275. pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
  276. return -EINVAL;
  277. }
  278. ret = pvr2_channel_limit_inputs(
  279. &adap->channel,
  280. (1 << PVR2_CVAL_INPUT_DTV));
  281. if (ret) {
  282. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  283. "failed to grab control of dtv input (code=%d)",
  284. ret);
  285. return ret;
  286. }
  287. if (dvb_props->frontend_attach == NULL) {
  288. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  289. "frontend_attach not defined!");
  290. ret = -EINVAL;
  291. goto done;
  292. }
  293. if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
  294. if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
  295. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  296. "frontend registration failed!");
  297. dvb_frontend_detach(adap->fe);
  298. adap->fe = NULL;
  299. ret = -ENODEV;
  300. goto done;
  301. }
  302. if (dvb_props->tuner_attach)
  303. dvb_props->tuner_attach(adap);
  304. if (adap->fe->ops.analog_ops.standby)
  305. adap->fe->ops.analog_ops.standby(adap->fe);
  306. /* Ensure all frontends negotiate bus access */
  307. adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
  308. } else {
  309. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  310. "no frontend was attached!");
  311. ret = -ENODEV;
  312. return ret;
  313. }
  314. done:
  315. pvr2_channel_limit_inputs(&adap->channel, 0);
  316. return ret;
  317. }
  318. static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
  319. {
  320. if (adap->fe != NULL) {
  321. dvb_unregister_frontend(adap->fe);
  322. dvb_frontend_detach(adap->fe);
  323. }
  324. return 0;
  325. }
  326. static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
  327. {
  328. pvr2_dvb_stream_end(adap);
  329. pvr2_dvb_frontend_exit(adap);
  330. pvr2_dvb_adapter_exit(adap);
  331. pvr2_channel_done(&adap->channel);
  332. kfree(adap);
  333. }
  334. static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
  335. {
  336. struct pvr2_dvb_adapter *adap;
  337. adap = container_of(chp, struct pvr2_dvb_adapter, channel);
  338. if (!adap->channel.mc_head->disconnect_flag) return;
  339. pvr2_dvb_destroy(adap);
  340. }
  341. struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
  342. {
  343. int ret = 0;
  344. struct pvr2_dvb_adapter *adap;
  345. if (!pvr->hdw->hdw_desc->dvb_props) {
  346. /* Device lacks a digital interface so don't set up
  347. the DVB side of the driver either. For now. */
  348. return NULL;
  349. }
  350. adap = kzalloc(sizeof(*adap), GFP_KERNEL);
  351. if (!adap) return adap;
  352. pvr2_channel_init(&adap->channel, pvr);
  353. adap->channel.check_func = pvr2_dvb_internal_check;
  354. init_waitqueue_head(&adap->buffer_wait_data);
  355. mutex_init(&adap->lock);
  356. ret = pvr2_dvb_adapter_init(adap);
  357. if (ret < 0) goto fail1;
  358. ret = pvr2_dvb_frontend_init(adap);
  359. if (ret < 0) goto fail2;
  360. return adap;
  361. fail2:
  362. pvr2_dvb_adapter_exit(adap);
  363. fail1:
  364. pvr2_channel_done(&adap->channel);
  365. return NULL;
  366. }