pvrusb2-dvb.c 11 KB

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