videobuf-dvb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. int err;
  40. void *outp;
  41. dprintk("dvb thread started\n");
  42. set_freezable();
  43. videobuf_read_start(&dvb->dvbq);
  44. for (;;) {
  45. /* fetch next buffer */
  46. buf = list_entry(dvb->dvbq.stream.next,
  47. struct videobuf_buffer, stream);
  48. list_del(&buf->stream);
  49. err = videobuf_waiton(&dvb->dvbq, buf, 0, 1);
  50. /* no more feeds left or stop_feed() asked us to quit */
  51. if (0 == dvb->nfeeds)
  52. break;
  53. if (kthread_should_stop())
  54. break;
  55. try_to_freeze();
  56. /* feed buffer data to demux */
  57. outp = videobuf_queue_to_vaddr(&dvb->dvbq, buf);
  58. if (buf->state == VIDEOBUF_DONE)
  59. dvb_dmx_swfilter(&dvb->demux, outp,
  60. buf->size);
  61. /* requeue buffer */
  62. list_add_tail(&buf->stream,&dvb->dvbq.stream);
  63. spin_lock_irqsave(dvb->dvbq.irqlock,flags);
  64. dvb->dvbq.ops->buf_queue(&dvb->dvbq,buf);
  65. spin_unlock_irqrestore(dvb->dvbq.irqlock,flags);
  66. }
  67. videobuf_read_stop(&dvb->dvbq);
  68. dprintk("dvb thread stopped\n");
  69. /* Hmm, linux becomes *very* unhappy without this ... */
  70. while (!kthread_should_stop()) {
  71. set_current_state(TASK_INTERRUPTIBLE);
  72. schedule();
  73. }
  74. return 0;
  75. }
  76. static int videobuf_dvb_start_feed(struct dvb_demux_feed *feed)
  77. {
  78. struct dvb_demux *demux = feed->demux;
  79. struct videobuf_dvb *dvb = demux->priv;
  80. int rc;
  81. if (!demux->dmx.frontend)
  82. return -EINVAL;
  83. mutex_lock(&dvb->lock);
  84. dvb->nfeeds++;
  85. rc = dvb->nfeeds;
  86. if (NULL != dvb->thread)
  87. goto out;
  88. dvb->thread = kthread_run(videobuf_dvb_thread,
  89. dvb, "%s dvb", dvb->name);
  90. if (IS_ERR(dvb->thread)) {
  91. rc = PTR_ERR(dvb->thread);
  92. dvb->thread = NULL;
  93. }
  94. out:
  95. mutex_unlock(&dvb->lock);
  96. return rc;
  97. }
  98. static int videobuf_dvb_stop_feed(struct dvb_demux_feed *feed)
  99. {
  100. struct dvb_demux *demux = feed->demux;
  101. struct videobuf_dvb *dvb = demux->priv;
  102. int err = 0;
  103. mutex_lock(&dvb->lock);
  104. dvb->nfeeds--;
  105. if (0 == dvb->nfeeds && NULL != dvb->thread) {
  106. err = kthread_stop(dvb->thread);
  107. dvb->thread = NULL;
  108. }
  109. mutex_unlock(&dvb->lock);
  110. return err;
  111. }
  112. static int videobuf_dvb_register_adapter(struct videobuf_dvb_frontends *fe,
  113. struct module *module,
  114. void *adapter_priv,
  115. struct device *device,
  116. char *adapter_name,
  117. short *adapter_nr,
  118. int mfe_shared,
  119. int (*fe_ioctl_override)(struct dvb_frontend *,
  120. unsigned int, void *, unsigned int))
  121. {
  122. int result;
  123. mutex_init(&fe->lock);
  124. /* register adapter */
  125. result = dvb_register_adapter(&fe->adapter, adapter_name, module,
  126. device, adapter_nr);
  127. if (result < 0) {
  128. printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
  129. adapter_name, result);
  130. }
  131. fe->adapter.priv = adapter_priv;
  132. fe->adapter.mfe_shared = mfe_shared;
  133. fe->adapter.fe_ioctl_override = fe_ioctl_override;
  134. return result;
  135. }
  136. static int videobuf_dvb_register_frontend(struct dvb_adapter *adapter,
  137. struct videobuf_dvb *dvb)
  138. {
  139. int result;
  140. /* register frontend */
  141. result = dvb_register_frontend(adapter, dvb->frontend);
  142. if (result < 0) {
  143. printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
  144. dvb->name, result);
  145. goto fail_frontend;
  146. }
  147. /* register demux stuff */
  148. dvb->demux.dmx.capabilities =
  149. DMX_TS_FILTERING | DMX_SECTION_FILTERING |
  150. DMX_MEMORY_BASED_FILTERING;
  151. dvb->demux.priv = dvb;
  152. dvb->demux.filternum = 256;
  153. dvb->demux.feednum = 256;
  154. dvb->demux.start_feed = videobuf_dvb_start_feed;
  155. dvb->demux.stop_feed = videobuf_dvb_stop_feed;
  156. result = dvb_dmx_init(&dvb->demux);
  157. if (result < 0) {
  158. printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
  159. dvb->name, result);
  160. goto fail_dmx;
  161. }
  162. dvb->dmxdev.filternum = 256;
  163. dvb->dmxdev.demux = &dvb->demux.dmx;
  164. dvb->dmxdev.capabilities = 0;
  165. result = dvb_dmxdev_init(&dvb->dmxdev, adapter);
  166. if (result < 0) {
  167. printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
  168. dvb->name, result);
  169. goto fail_dmxdev;
  170. }
  171. dvb->fe_hw.source = DMX_FRONTEND_0;
  172. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  173. if (result < 0) {
  174. printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
  175. dvb->name, result);
  176. goto fail_fe_hw;
  177. }
  178. dvb->fe_mem.source = DMX_MEMORY_FE;
  179. result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  180. if (result < 0) {
  181. printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
  182. dvb->name, result);
  183. goto fail_fe_mem;
  184. }
  185. result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  186. if (result < 0) {
  187. printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
  188. dvb->name, result);
  189. goto fail_fe_conn;
  190. }
  191. /* register network adapter */
  192. result = dvb_net_init(adapter, &dvb->net, &dvb->demux.dmx);
  193. if (result < 0) {
  194. printk(KERN_WARNING "%s: dvb_net_init failed (errno = %d)\n",
  195. dvb->name, result);
  196. goto fail_fe_conn;
  197. }
  198. return 0;
  199. fail_fe_conn:
  200. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
  201. fail_fe_mem:
  202. dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
  203. fail_fe_hw:
  204. dvb_dmxdev_release(&dvb->dmxdev);
  205. fail_dmxdev:
  206. dvb_dmx_release(&dvb->demux);
  207. fail_dmx:
  208. dvb_unregister_frontend(dvb->frontend);
  209. fail_frontend:
  210. dvb_frontend_detach(dvb->frontend);
  211. dvb->frontend = NULL;
  212. return result;
  213. }
  214. /* ------------------------------------------------------------------ */
  215. /* Register a single adapter and one or more frontends */
  216. int videobuf_dvb_register_bus(struct videobuf_dvb_frontends *f,
  217. struct module *module,
  218. void *adapter_priv,
  219. struct device *device,
  220. short *adapter_nr,
  221. int mfe_shared,
  222. int (*fe_ioctl_override)(struct dvb_frontend *,
  223. unsigned int, void *, unsigned int))
  224. {
  225. struct list_head *list, *q;
  226. struct videobuf_dvb_frontend *fe;
  227. int res;
  228. fe = videobuf_dvb_get_frontend(f, 1);
  229. if (!fe) {
  230. printk(KERN_WARNING "Unable to register the adapter which has no frontends\n");
  231. return -EINVAL;
  232. }
  233. /* Bring up the adapter */
  234. res = videobuf_dvb_register_adapter(f, module, adapter_priv, device,
  235. fe->dvb.name, adapter_nr, mfe_shared, fe_ioctl_override);
  236. if (res < 0) {
  237. printk(KERN_WARNING "videobuf_dvb_register_adapter failed (errno = %d)\n", res);
  238. return res;
  239. }
  240. /* Attach all of the frontends to the adapter */
  241. mutex_lock(&f->lock);
  242. list_for_each_safe(list, q, &f->felist) {
  243. fe = list_entry(list, struct videobuf_dvb_frontend, felist);
  244. res = videobuf_dvb_register_frontend(&f->adapter, &fe->dvb);
  245. if (res < 0) {
  246. printk(KERN_WARNING "%s: videobuf_dvb_register_frontend failed (errno = %d)\n",
  247. fe->dvb.name, res);
  248. goto err;
  249. }
  250. }
  251. mutex_unlock(&f->lock);
  252. return 0;
  253. err:
  254. mutex_unlock(&f->lock);
  255. videobuf_dvb_unregister_bus(f);
  256. return res;
  257. }
  258. EXPORT_SYMBOL(videobuf_dvb_register_bus);
  259. void videobuf_dvb_unregister_bus(struct videobuf_dvb_frontends *f)
  260. {
  261. videobuf_dvb_dealloc_frontends(f);
  262. dvb_unregister_adapter(&f->adapter);
  263. }
  264. EXPORT_SYMBOL(videobuf_dvb_unregister_bus);
  265. struct videobuf_dvb_frontend *videobuf_dvb_get_frontend(
  266. struct videobuf_dvb_frontends *f, int id)
  267. {
  268. struct list_head *list, *q;
  269. struct videobuf_dvb_frontend *fe, *ret = NULL;
  270. mutex_lock(&f->lock);
  271. list_for_each_safe(list, q, &f->felist) {
  272. fe = list_entry(list, struct videobuf_dvb_frontend, felist);
  273. if (fe->id == id) {
  274. ret = fe;
  275. break;
  276. }
  277. }
  278. mutex_unlock(&f->lock);
  279. return ret;
  280. }
  281. EXPORT_SYMBOL(videobuf_dvb_get_frontend);
  282. int videobuf_dvb_find_frontend(struct videobuf_dvb_frontends *f,
  283. struct dvb_frontend *p)
  284. {
  285. struct list_head *list, *q;
  286. struct videobuf_dvb_frontend *fe = NULL;
  287. int ret = 0;
  288. mutex_lock(&f->lock);
  289. list_for_each_safe(list, q, &f->felist) {
  290. fe = list_entry(list, struct videobuf_dvb_frontend, felist);
  291. if (fe->dvb.frontend == p) {
  292. ret = fe->id;
  293. break;
  294. }
  295. }
  296. mutex_unlock(&f->lock);
  297. return ret;
  298. }
  299. EXPORT_SYMBOL(videobuf_dvb_find_frontend);
  300. struct videobuf_dvb_frontend *videobuf_dvb_alloc_frontend(
  301. struct videobuf_dvb_frontends *f, int id)
  302. {
  303. struct videobuf_dvb_frontend *fe;
  304. fe = kzalloc(sizeof(struct videobuf_dvb_frontend), GFP_KERNEL);
  305. if (fe == NULL)
  306. goto fail_alloc;
  307. fe->id = id;
  308. mutex_init(&fe->dvb.lock);
  309. mutex_lock(&f->lock);
  310. list_add_tail(&fe->felist, &f->felist);
  311. mutex_unlock(&f->lock);
  312. fail_alloc:
  313. return fe;
  314. }
  315. EXPORT_SYMBOL(videobuf_dvb_alloc_frontend);
  316. void videobuf_dvb_dealloc_frontends(struct videobuf_dvb_frontends *f)
  317. {
  318. struct list_head *list, *q;
  319. struct videobuf_dvb_frontend *fe;
  320. mutex_lock(&f->lock);
  321. list_for_each_safe(list, q, &f->felist) {
  322. fe = list_entry(list, struct videobuf_dvb_frontend, felist);
  323. if (fe->dvb.net.dvbdev) {
  324. dvb_net_release(&fe->dvb.net);
  325. fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx,
  326. &fe->dvb.fe_mem);
  327. fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx,
  328. &fe->dvb.fe_hw);
  329. dvb_dmxdev_release(&fe->dvb.dmxdev);
  330. dvb_dmx_release(&fe->dvb.demux);
  331. dvb_unregister_frontend(fe->dvb.frontend);
  332. }
  333. if (fe->dvb.frontend)
  334. /* always allocated, may have been reset */
  335. dvb_frontend_detach(fe->dvb.frontend);
  336. list_del(list); /* remove list entry */
  337. kfree(fe); /* free frontend allocation */
  338. }
  339. mutex_unlock(&f->lock);
  340. }
  341. EXPORT_SYMBOL(videobuf_dvb_dealloc_frontends);