videobuf-dvb.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. *
  3. * some helper function for simple DVB cards which simply DMA the
  4. * complete transport stream and let the computer sort everything else
  5. * (i.e. we are using the software demux, ...). Also uses the
  6. * video-buf to manage DMA buffers.
  7. *
  8. * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/device.h>
  18. #include <linux/fs.h>
  19. #include <linux/kthread.h>
  20. #include <linux/file.h>
  21. #include <linux/slab.h>
  22. #include <linux/freezer.h>
  23. #include <media/videobuf-core.h>
  24. #include <media/videobuf-dvb.h>
  25. /* ------------------------------------------------------------------ */
  26. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  27. MODULE_LICENSE("GPL");
  28. static unsigned int debug;
  29. module_param(debug, int, 0644);
  30. MODULE_PARM_DESC(debug,"enable debug messages");
  31. #define dprintk(fmt, arg...) if (debug) \
  32. printk(KERN_DEBUG "%s/dvb: " fmt, dvb->name , ## arg)
  33. /* ------------------------------------------------------------------ */
  34. static int videobuf_dvb_thread(void *data)
  35. {
  36. struct videobuf_dvb *dvb = data;
  37. struct videobuf_buffer *buf;
  38. unsigned long flags;
  39. void *outp;
  40. dprintk("dvb thread started\n");
  41. set_freezable();
  42. videobuf_read_start(&dvb->dvbq);
  43. for (;;) {
  44. /* fetch next buffer */
  45. buf = list_entry(dvb->dvbq.stream.next,
  46. struct videobuf_buffer, stream);
  47. list_del(&buf->stream);
  48. videobuf_waiton(&dvb->dvbq, buf, 0, 1);
  49. /* no more feeds left or stop_feed() asked us to quit */
  50. if (0 == dvb->nfeeds)
  51. break;
  52. if (kthread_should_stop())
  53. break;
  54. try_to_freeze();
  55. /* feed buffer data to demux */
  56. outp = videobuf_queue_to_vaddr(&dvb->dvbq, buf);
  57. if (buf->state == VIDEOBUF_DONE)
  58. dvb_dmx_swfilter(&dvb->demux, outp,
  59. buf->size);
  60. /* requeue buffer */
  61. list_add_tail(&buf->stream,&dvb->dvbq.stream);
  62. spin_lock_irqsave(dvb->dvbq.irqlock,flags);
  63. dvb->dvbq.ops->buf_queue(&dvb->dvbq,buf);
  64. spin_unlock_irqrestore(dvb->dvbq.irqlock,flags);
  65. }
  66. videobuf_read_stop(&dvb->dvbq);
  67. dprintk("dvb thread stopped\n");
  68. /* Hmm, linux becomes *very* unhappy without this ... */
  69. while (!kthread_should_stop()) {
  70. set_current_state(TASK_INTERRUPTIBLE);
  71. schedule();
  72. }
  73. return 0;
  74. }
  75. static int videobuf_dvb_start_feed(struct dvb_demux_feed *feed)
  76. {
  77. struct dvb_demux *demux = feed->demux;
  78. struct videobuf_dvb *dvb = demux->priv;
  79. int rc;
  80. if (!demux->dmx.frontend)
  81. return -EINVAL;
  82. mutex_lock(&dvb->lock);
  83. dvb->nfeeds++;
  84. rc = dvb->nfeeds;
  85. if (NULL != dvb->thread)
  86. goto out;
  87. dvb->thread = kthread_run(videobuf_dvb_thread,
  88. dvb, "%s dvb", dvb->name);
  89. if (IS_ERR(dvb->thread)) {
  90. rc = PTR_ERR(dvb->thread);
  91. dvb->thread = NULL;
  92. }
  93. out:
  94. mutex_unlock(&dvb->lock);
  95. return rc;
  96. }
  97. static int videobuf_dvb_stop_feed(struct dvb_demux_feed *feed)
  98. {
  99. struct dvb_demux *demux = feed->demux;
  100. struct videobuf_dvb *dvb = demux->priv;
  101. int err = 0;
  102. mutex_lock(&dvb->lock);
  103. dvb->nfeeds--;
  104. if (0 == dvb->nfeeds && NULL != dvb->thread) {
  105. err = kthread_stop(dvb->thread);
  106. dvb->thread = NULL;
  107. }
  108. mutex_unlock(&dvb->lock);
  109. return err;
  110. }
  111. static int videobuf_dvb_register_adapter(struct videobuf_dvb_frontends *fe,
  112. struct module *module,
  113. void *adapter_priv,
  114. struct device *device,
  115. char *adapter_name,
  116. short *adapter_nr,
  117. int mfe_shared)
  118. {
  119. int result;
  120. mutex_init(&fe->lock);
  121. /* register adapter */
  122. result = dvb_register_adapter(&fe->adapter, adapter_name, module,
  123. device, adapter_nr);
  124. if (result < 0) {
  125. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  126. adapter_name, result);
  127. }
  128. fe->adapter.priv = adapter_priv;
  129. fe->adapter.mfe_shared = mfe_shared;
  130. return result;
  131. }
  132. static int videobuf_dvb_register_frontend(struct dvb_adapter *adapter,
  133. struct videobuf_dvb *dvb)
  134. {
  135. int result;
  136. /* register frontend */
  137. result = dvb_register_frontend(adapter, dvb->frontend);
  138. if (result < 0) {
  139. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  140. dvb->name, result);
  141. goto fail_frontend;
  142. }
  143. /* register demux stuff */
  144. dvb->demux.dmx.capabilities =
  145. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  146. DMX_MEMORY_BASED_FILTERING;
  147. dvb->demux.priv = dvb;
  148. dvb->demux.filternum = 256;
  149. dvb->demux.feednum = 256;
  150. dvb->demux.start_feed = videobuf_dvb_start_feed;
  151. dvb->demux.stop_feed = videobuf_dvb_stop_feed;
  152. result = dvb_dmx_init(&dvb->demux);
  153. if (result < 0) {
  154. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  155. dvb->name, result);
  156. goto fail_dmx;
  157. }
  158. dvb->dmxdev.filternum = 256;
  159. dvb->dmxdev.demux = &dvb->demux.dmx;
  160. dvb->dmxdev.capabilities = 0;
  161. result = dvb_dmxdev_init(&dvb->dmxdev, adapter);
  162. if (result < 0) {
  163. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  164. dvb->name, result);
  165. goto fail_dmxdev;
  166. }
  167. dvb->fe_hw.source = DMX_FRONTEND_0;
  168. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  169. if (result < 0) {
  170. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  171. dvb->name, result);
  172. goto fail_fe_hw;
  173. }
  174. dvb->fe_mem.source = DMX_MEMORY_FE;
  175. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  176. if (result < 0) {
  177. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  178. dvb->name, result);
  179. goto fail_fe_mem;
  180. }
  181. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  182. if (result < 0) {
  183. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  184. dvb->name, result);
  185. goto fail_fe_conn;
  186. }
  187. /* register network adapter */
  188. result = dvb_net_init(adapter, &dvb->net, &dvb->demux.dmx);
  189. if (result < 0) {
  190. printk(KERN_WARNING "%s: dvb_net_init failed (errno = %d)\n",
  191. dvb->name, result);
  192. goto fail_fe_conn;
  193. }
  194. return 0;
  195. fail_fe_conn:
  196. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  197. fail_fe_mem:
  198. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  199. fail_fe_hw:
  200. dvb_dmxdev_release(&dvb->dmxdev);
  201. fail_dmxdev:
  202. dvb_dmx_release(&dvb->demux);
  203. fail_dmx:
  204. dvb_unregister_frontend(dvb->frontend);
  205. fail_frontend:
  206. dvb_frontend_detach(dvb->frontend);
  207. dvb->frontend = NULL;
  208. return result;
  209. }
  210. /* ------------------------------------------------------------------ */
  211. /* Register a single adapter and one or more frontends */
  212. int videobuf_dvb_register_bus(struct videobuf_dvb_frontends *f,
  213. struct module *module,
  214. void *adapter_priv,
  215. struct device *device,
  216. short *adapter_nr,
  217. int mfe_shared)
  218. {
  219. struct list_head *list, *q;
  220. struct videobuf_dvb_frontend *fe;
  221. int res;
  222. fe = videobuf_dvb_get_frontend(f, 1);
  223. if (!fe) {
  224. printk(KERN_WARNING "Unable to register the adapter which has no frontends\n");
  225. return -EINVAL;
  226. }
  227. /* Bring up the adapter */
  228. res = videobuf_dvb_register_adapter(f, module, adapter_priv, device,
  229. fe->dvb.name, adapter_nr, mfe_shared);
  230. if (res < 0) {
  231. printk(KERN_WARNING "videobuf_dvb_register_adapter failed (errno = %d)\n", res);
  232. return res;
  233. }
  234. /* Attach all of the frontends to the adapter */
  235. mutex_lock(&f->lock);
  236. list_for_each_safe(list, q, &f->felist) {
  237. fe = list_entry(list, struct videobuf_dvb_frontend, felist);
  238. res = videobuf_dvb_register_frontend(&f->adapter, &fe->dvb);
  239. if (res < 0) {
  240. printk(KERN_WARNING "%s: videobuf_dvb_register_frontend failed (errno = %d)\n",
  241. fe->dvb.name, res);
  242. goto err;
  243. }
  244. }
  245. mutex_unlock(&f->lock);
  246. return 0;
  247. err:
  248. mutex_unlock(&f->lock);
  249. videobuf_dvb_unregister_bus(f);
  250. return res;
  251. }
  252. EXPORT_SYMBOL(videobuf_dvb_register_bus);
  253. void videobuf_dvb_unregister_bus(struct videobuf_dvb_frontends *f)
  254. {
  255. videobuf_dvb_dealloc_frontends(f);
  256. dvb_unregister_adapter(&f->adapter);
  257. }
  258. EXPORT_SYMBOL(videobuf_dvb_unregister_bus);
  259. struct videobuf_dvb_frontend *videobuf_dvb_get_frontend(
  260. struct videobuf_dvb_frontends *f, int id)
  261. {
  262. struct list_head *list, *q;
  263. struct videobuf_dvb_frontend *fe, *ret = NULL;
  264. mutex_lock(&f->lock);
  265. list_for_each_safe(list, q, &f->felist) {
  266. fe = list_entry(list, struct videobuf_dvb_frontend, felist);
  267. if (fe->id == id) {
  268. ret = fe;
  269. break;
  270. }
  271. }
  272. mutex_unlock(&f->lock);
  273. return ret;
  274. }
  275. EXPORT_SYMBOL(videobuf_dvb_get_frontend);
  276. int videobuf_dvb_find_frontend(struct videobuf_dvb_frontends *f,
  277. struct dvb_frontend *p)
  278. {
  279. struct list_head *list, *q;
  280. struct videobuf_dvb_frontend *fe = NULL;
  281. int ret = 0;
  282. mutex_lock(&f->lock);
  283. list_for_each_safe(list, q, &f->felist) {
  284. fe = list_entry(list, struct videobuf_dvb_frontend, felist);
  285. if (fe->dvb.frontend == p) {
  286. ret = fe->id;
  287. break;
  288. }
  289. }
  290. mutex_unlock(&f->lock);
  291. return ret;
  292. }
  293. EXPORT_SYMBOL(videobuf_dvb_find_frontend);
  294. struct videobuf_dvb_frontend *videobuf_dvb_alloc_frontend(
  295. struct videobuf_dvb_frontends *f, int id)
  296. {
  297. struct videobuf_dvb_frontend *fe;
  298. fe = kzalloc(sizeof(struct videobuf_dvb_frontend), GFP_KERNEL);
  299. if (fe == NULL)
  300. goto fail_alloc;
  301. fe->id = id;
  302. mutex_init(&fe->dvb.lock);
  303. mutex_lock(&f->lock);
  304. list_add_tail(&fe->felist, &f->felist);
  305. mutex_unlock(&f->lock);
  306. fail_alloc:
  307. return fe;
  308. }
  309. EXPORT_SYMBOL(videobuf_dvb_alloc_frontend);
  310. void videobuf_dvb_dealloc_frontends(struct videobuf_dvb_frontends *f)
  311. {
  312. struct list_head *list, *q;
  313. struct videobuf_dvb_frontend *fe;
  314. mutex_lock(&f->lock);
  315. list_for_each_safe(list, q, &f->felist) {
  316. fe = list_entry(list, struct videobuf_dvb_frontend, felist);
  317. if (fe->dvb.net.dvbdev) {
  318. dvb_net_release(&fe->dvb.net);
  319. fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx,
  320. &fe->dvb.fe_mem);
  321. fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx,
  322. &fe->dvb.fe_hw);
  323. dvb_dmxdev_release(&fe->dvb.dmxdev);
  324. dvb_dmx_release(&fe->dvb.demux);
  325. dvb_unregister_frontend(fe->dvb.frontend);
  326. }
  327. if (fe->dvb.frontend)
  328. /* always allocated, may have been reset */
  329. dvb_frontend_detach(fe->dvb.frontend);
  330. list_del(list); /* remove list entry */
  331. kfree(fe); /* free frontend allocation */
  332. }
  333. mutex_unlock(&f->lock);
  334. }
  335. EXPORT_SYMBOL(videobuf_dvb_dealloc_frontends);