ngene-dvb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * ngene-dvb.c: nGene PCIe bridge driver - DVB functions
  3. *
  4. * Copyright (C) 2005-2007 Micronas
  5. *
  6. * Copyright (C) 2008-2009 Ralph Metzler <rjkm@metzlerbros.de>
  7. * Modifications for new nGene firmware,
  8. * support for EEPROM-copying,
  9. * support for new dual DVB-S2 card prototype
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 only, as published by the Free Software Foundation.
  15. *
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  26. * 02110-1301, USA
  27. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/slab.h>
  33. #include <linux/poll.h>
  34. #include <linux/io.h>
  35. #include <asm/div64.h>
  36. #include <linux/pci.h>
  37. #include <linux/timer.h>
  38. #include <linux/byteorder/generic.h>
  39. #include <linux/firmware.h>
  40. #include <linux/vmalloc.h>
  41. #include "ngene.h"
  42. /****************************************************************************/
  43. /* COMMAND API interface ****************************************************/
  44. /****************************************************************************/
  45. static ssize_t ts_write(struct file *file, const char __user *buf,
  46. size_t count, loff_t *ppos)
  47. {
  48. struct dvb_device *dvbdev = file->private_data;
  49. struct ngene_channel *chan = dvbdev->priv;
  50. struct ngene *dev = chan->dev;
  51. if (wait_event_interruptible(dev->tsout_rbuf.queue,
  52. dvb_ringbuffer_free
  53. (&dev->tsout_rbuf) >= count) < 0)
  54. return 0;
  55. dvb_ringbuffer_write_user(&dev->tsout_rbuf, buf, count);
  56. return count;
  57. }
  58. static ssize_t ts_read(struct file *file, char __user *buf,
  59. size_t count, loff_t *ppos)
  60. {
  61. struct dvb_device *dvbdev = file->private_data;
  62. struct ngene_channel *chan = dvbdev->priv;
  63. struct ngene *dev = chan->dev;
  64. int left, avail;
  65. left = count;
  66. while (left) {
  67. if (wait_event_interruptible(
  68. dev->tsin_rbuf.queue,
  69. dvb_ringbuffer_avail(&dev->tsin_rbuf) > 0) < 0)
  70. return -EAGAIN;
  71. avail = dvb_ringbuffer_avail(&dev->tsin_rbuf);
  72. if (avail > left)
  73. avail = left;
  74. dvb_ringbuffer_read_user(&dev->tsin_rbuf, buf, avail);
  75. left -= avail;
  76. buf += avail;
  77. }
  78. return count;
  79. }
  80. static const struct file_operations ci_fops = {
  81. .owner = THIS_MODULE,
  82. .read = ts_read,
  83. .write = ts_write,
  84. .open = dvb_generic_open,
  85. .release = dvb_generic_release,
  86. };
  87. struct dvb_device ngene_dvbdev_ci = {
  88. .readers = -1,
  89. .writers = -1,
  90. .users = -1,
  91. .fops = &ci_fops,
  92. };
  93. /****************************************************************************/
  94. /* DVB functions and API interface ******************************************/
  95. /****************************************************************************/
  96. static void swap_buffer(u32 *p, u32 len)
  97. {
  98. while (len) {
  99. *p = swab32(*p);
  100. p++;
  101. len -= 4;
  102. }
  103. }
  104. /* start of filler packet */
  105. static u8 fill_ts[] = { 0x47, 0x1f, 0xff, 0x10, TS_FILLER };
  106. /* #define DEBUG_CI_XFER */
  107. #ifdef DEBUG_CI_XFER
  108. static u32 ok;
  109. static u32 overflow;
  110. static u32 stripped;
  111. #endif
  112. void *tsin_exchange(void *priv, void *buf, u32 len, u32 clock, u32 flags)
  113. {
  114. struct ngene_channel *chan = priv;
  115. struct ngene *dev = chan->dev;
  116. if (flags & DF_SWAP32)
  117. swap_buffer(buf, len);
  118. if (dev->ci.en && chan->number == 2) {
  119. while (len >= 188) {
  120. if (memcmp(buf, fill_ts, sizeof fill_ts) != 0) {
  121. if (dvb_ringbuffer_free(&dev->tsin_rbuf) >= 188) {
  122. dvb_ringbuffer_write(&dev->tsin_rbuf, buf, 188);
  123. wake_up(&dev->tsin_rbuf.queue);
  124. #ifdef DEBUG_CI_XFER
  125. ok++;
  126. #endif
  127. }
  128. #ifdef DEBUG_CI_XFER
  129. else
  130. overflow++;
  131. #endif
  132. }
  133. #ifdef DEBUG_CI_XFER
  134. else
  135. stripped++;
  136. if (ok % 100 == 0 && overflow)
  137. printk(KERN_WARNING "%s: ok %u overflow %u dropped %u\n", __func__, ok, overflow, stripped);
  138. #endif
  139. buf += 188;
  140. len -= 188;
  141. }
  142. return NULL;
  143. }
  144. if (chan->users > 0)
  145. dvb_dmx_swfilter(&chan->demux, buf, len);
  146. return NULL;
  147. }
  148. void *tsout_exchange(void *priv, void *buf, u32 len, u32 clock, u32 flags)
  149. {
  150. struct ngene_channel *chan = priv;
  151. struct ngene *dev = chan->dev;
  152. u32 alen;
  153. alen = dvb_ringbuffer_avail(&dev->tsout_rbuf);
  154. alen -= alen % 188;
  155. if (alen < len)
  156. FillTSBuffer(buf + alen, len - alen, flags);
  157. else
  158. alen = len;
  159. dvb_ringbuffer_read(&dev->tsout_rbuf, buf, alen);
  160. if (flags & DF_SWAP32)
  161. swap_buffer((u32 *)buf, alen);
  162. wake_up_interruptible(&dev->tsout_rbuf.queue);
  163. return buf;
  164. }
  165. int ngene_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  166. {
  167. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  168. struct ngene_channel *chan = dvbdmx->priv;
  169. if (chan->users == 0) {
  170. if (!chan->dev->cmd_timeout_workaround || !chan->running)
  171. set_transfer(chan, 1);
  172. }
  173. return ++chan->users;
  174. }
  175. int ngene_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  176. {
  177. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  178. struct ngene_channel *chan = dvbdmx->priv;
  179. if (--chan->users)
  180. return chan->users;
  181. if (!chan->dev->cmd_timeout_workaround)
  182. set_transfer(chan, 0);
  183. return 0;
  184. }
  185. int my_dvb_dmx_ts_card_init(struct dvb_demux *dvbdemux, char *id,
  186. int (*start_feed)(struct dvb_demux_feed *),
  187. int (*stop_feed)(struct dvb_demux_feed *),
  188. void *priv)
  189. {
  190. dvbdemux->priv = priv;
  191. dvbdemux->filternum = 256;
  192. dvbdemux->feednum = 256;
  193. dvbdemux->start_feed = start_feed;
  194. dvbdemux->stop_feed = stop_feed;
  195. dvbdemux->write_to_decoder = NULL;
  196. dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
  197. DMX_SECTION_FILTERING |
  198. DMX_MEMORY_BASED_FILTERING);
  199. return dvb_dmx_init(dvbdemux);
  200. }
  201. int my_dvb_dmxdev_ts_card_init(struct dmxdev *dmxdev,
  202. struct dvb_demux *dvbdemux,
  203. struct dmx_frontend *hw_frontend,
  204. struct dmx_frontend *mem_frontend,
  205. struct dvb_adapter *dvb_adapter)
  206. {
  207. int ret;
  208. dmxdev->filternum = 256;
  209. dmxdev->demux = &dvbdemux->dmx;
  210. dmxdev->capabilities = 0;
  211. ret = dvb_dmxdev_init(dmxdev, dvb_adapter);
  212. if (ret < 0)
  213. return ret;
  214. hw_frontend->source = DMX_FRONTEND_0;
  215. dvbdemux->dmx.add_frontend(&dvbdemux->dmx, hw_frontend);
  216. mem_frontend->source = DMX_MEMORY_FE;
  217. dvbdemux->dmx.add_frontend(&dvbdemux->dmx, mem_frontend);
  218. return dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, hw_frontend);
  219. }