bttv-vbi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. bttv - Bt848 frame grabber driver
  3. vbi interface
  4. (c) 2002 Gerd Knorr <kraxel@bytesex.org>
  5. Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
  6. Sponsored by OPQ Systems AB
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/kernel.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kdev_t.h>
  26. #include <asm/io.h>
  27. #include "bttvp.h"
  28. /* Offset from line sync pulse leading edge (0H) to start of VBI capture,
  29. in fCLKx2 pixels. According to the datasheet, VBI capture starts
  30. VBI_HDELAY fCLKx1 pixels from the tailing edgeof /HRESET, and /HRESET
  31. is 64 fCLKx1 pixels wide. VBI_HDELAY is set to 0, so this should be
  32. (64 + 0) * 2 = 128 fCLKx2 pixels. But it's not! The datasheet is
  33. Just Plain Wrong. The real value appears to be different for
  34. different revisions of the bt8x8 chips, and to be affected by the
  35. horizontal scaling factor. Experimentally, the value is measured
  36. to be about 244. */
  37. #define VBI_OFFSET 244
  38. /* 2048 for compatibility with earlier driver versions. The driver
  39. really stores 1024 + tvnorm->vbipack * 4 samples per line in the
  40. buffer. Note tvnorm->vbipack is <= 0xFF (limit of VBIPACK_LO + HI
  41. is 0x1FF DWORDs) and VBI read()s store a frame counter in the last
  42. four bytes of the VBI image. */
  43. #define VBI_BPL 2048
  44. /* Compatibility. */
  45. #define VBI_DEFLINES 16
  46. static unsigned int vbibufs = 4;
  47. static unsigned int vbi_debug = 0;
  48. module_param(vbibufs, int, 0444);
  49. module_param(vbi_debug, int, 0644);
  50. MODULE_PARM_DESC(vbibufs,"number of vbi buffers, range 2-32, default 4");
  51. MODULE_PARM_DESC(vbi_debug,"vbi code debug messages, default is 0 (no)");
  52. #ifdef dprintk
  53. # undef dprintk
  54. #endif
  55. #define dprintk(fmt, arg...) if (vbi_debug) \
  56. printk(KERN_DEBUG "bttv%d/vbi: " fmt, btv->c.nr , ## arg)
  57. #define IMAGE_SIZE(fmt) \
  58. (((fmt)->count[0] + (fmt)->count[1]) * (fmt)->samples_per_line)
  59. /* ----------------------------------------------------------------------- */
  60. /* vbi risc code + mm */
  61. static int vbi_buffer_setup(struct videobuf_queue *q,
  62. unsigned int *count, unsigned int *size)
  63. {
  64. struct bttv_fh *fh = q->priv_data;
  65. struct bttv *btv = fh->btv;
  66. if (0 == *count)
  67. *count = vbibufs;
  68. *size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
  69. dprintk("setup: samples=%u start=%d,%d count=%u,%u\n",
  70. fh->vbi_fmt.fmt.samples_per_line,
  71. fh->vbi_fmt.fmt.start[0],
  72. fh->vbi_fmt.fmt.start[1],
  73. fh->vbi_fmt.fmt.count[0],
  74. fh->vbi_fmt.fmt.count[1]);
  75. return 0;
  76. }
  77. static int vbi_buffer_prepare(struct videobuf_queue *q,
  78. struct videobuf_buffer *vb,
  79. enum v4l2_field field)
  80. {
  81. struct bttv_fh *fh = q->priv_data;
  82. struct bttv *btv = fh->btv;
  83. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  84. const struct bttv_tvnorm *tvnorm;
  85. unsigned int skip_lines0, skip_lines1, min_vdelay;
  86. int redo_dma_risc;
  87. int rc;
  88. buf->vb.size = IMAGE_SIZE(&fh->vbi_fmt.fmt);
  89. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  90. return -EINVAL;
  91. tvnorm = fh->vbi_fmt.tvnorm;
  92. /* There's no VBI_VDELAY register, RISC must skip the lines
  93. we don't want. With default parameters we skip zero lines
  94. as earlier driver versions did. The driver permits video
  95. standard changes while capturing, so we use vbi_fmt.tvnorm
  96. instead of btv->tvnorm to skip zero lines after video
  97. standard changes as well. */
  98. skip_lines0 = 0;
  99. skip_lines1 = 0;
  100. if (fh->vbi_fmt.fmt.count[0] > 0)
  101. skip_lines0 = max(0, (fh->vbi_fmt.fmt.start[0]
  102. - tvnorm->vbistart[0]));
  103. if (fh->vbi_fmt.fmt.count[1] > 0)
  104. skip_lines1 = max(0, (fh->vbi_fmt.fmt.start[1]
  105. - tvnorm->vbistart[1]));
  106. redo_dma_risc = 0;
  107. if (buf->vbi_skip[0] != skip_lines0 ||
  108. buf->vbi_skip[1] != skip_lines1 ||
  109. buf->vbi_count[0] != fh->vbi_fmt.fmt.count[0] ||
  110. buf->vbi_count[1] != fh->vbi_fmt.fmt.count[1]) {
  111. buf->vbi_skip[0] = skip_lines0;
  112. buf->vbi_skip[1] = skip_lines1;
  113. buf->vbi_count[0] = fh->vbi_fmt.fmt.count[0];
  114. buf->vbi_count[1] = fh->vbi_fmt.fmt.count[1];
  115. redo_dma_risc = 1;
  116. }
  117. if (STATE_NEEDS_INIT == buf->vb.state) {
  118. redo_dma_risc = 1;
  119. if (0 != (rc = videobuf_iolock(q, &buf->vb, NULL)))
  120. goto fail;
  121. }
  122. if (redo_dma_risc) {
  123. unsigned int bpl, padding, offset;
  124. bpl = 2044; /* max. vbipack */
  125. padding = VBI_BPL - bpl;
  126. if (fh->vbi_fmt.fmt.count[0] > 0) {
  127. rc = bttv_risc_packed(btv, &buf->top,
  128. buf->vb.dma.sglist,
  129. /* offset */ 0, bpl,
  130. padding, skip_lines0,
  131. fh->vbi_fmt.fmt.count[0]);
  132. if (0 != rc)
  133. goto fail;
  134. }
  135. if (fh->vbi_fmt.fmt.count[1] > 0) {
  136. offset = fh->vbi_fmt.fmt.count[0] * VBI_BPL;
  137. rc = bttv_risc_packed(btv, &buf->bottom,
  138. buf->vb.dma.sglist,
  139. offset, bpl,
  140. padding, skip_lines1,
  141. fh->vbi_fmt.fmt.count[1]);
  142. if (0 != rc)
  143. goto fail;
  144. }
  145. }
  146. /* VBI capturing ends at VDELAY, start of video capturing,
  147. no matter where the RISC program ends. VDELAY minimum is 2,
  148. bounds.top is the corresponding first field line number
  149. times two. VDELAY counts half field lines. */
  150. min_vdelay = MIN_VDELAY;
  151. if (fh->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
  152. min_vdelay += fh->vbi_fmt.end - tvnorm->cropcap.bounds.top;
  153. /* For bttv_buffer_activate_vbi(). */
  154. buf->geo.vdelay = min_vdelay;
  155. buf->vb.state = STATE_PREPARED;
  156. buf->vb.field = field;
  157. dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
  158. vb, &buf->top, &buf->bottom,
  159. v4l2_field_names[buf->vb.field]);
  160. return 0;
  161. fail:
  162. bttv_dma_free(q,btv,buf);
  163. return rc;
  164. }
  165. static void
  166. vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  167. {
  168. struct bttv_fh *fh = q->priv_data;
  169. struct bttv *btv = fh->btv;
  170. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  171. dprintk("queue %p\n",vb);
  172. buf->vb.state = STATE_QUEUED;
  173. list_add_tail(&buf->vb.queue,&btv->vcapture);
  174. if (NULL == btv->cvbi) {
  175. fh->btv->loop_irq |= 4;
  176. bttv_set_dma(btv,0x0c);
  177. }
  178. }
  179. static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  180. {
  181. struct bttv_fh *fh = q->priv_data;
  182. struct bttv *btv = fh->btv;
  183. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  184. dprintk("free %p\n",vb);
  185. bttv_dma_free(&fh->cap,fh->btv,buf);
  186. }
  187. struct videobuf_queue_ops bttv_vbi_qops = {
  188. .buf_setup = vbi_buffer_setup,
  189. .buf_prepare = vbi_buffer_prepare,
  190. .buf_queue = vbi_buffer_queue,
  191. .buf_release = vbi_buffer_release,
  192. };
  193. /* ----------------------------------------------------------------------- */
  194. static int
  195. try_fmt (struct v4l2_vbi_format * f,
  196. const struct bttv_tvnorm * tvnorm,
  197. __s32 crop_start)
  198. {
  199. __s32 min_start, max_start, max_end, f2_offset;
  200. unsigned int i;
  201. /* For compatibility with earlier driver versions we must pretend
  202. the VBI and video capture window may overlap. In reality RISC
  203. magic aborts VBI capturing at the first line of video capturing,
  204. leaving the rest of the buffer unchanged, usually all zero.
  205. VBI capturing must always start before video capturing. >> 1
  206. because cropping counts field lines times two. */
  207. min_start = tvnorm->vbistart[0];
  208. max_start = (crop_start >> 1) - 1;
  209. max_end = (tvnorm->cropcap.bounds.top
  210. + tvnorm->cropcap.bounds.height) >> 1;
  211. if (min_start > max_start)
  212. return -EBUSY;
  213. BUG_ON(max_start >= max_end);
  214. f->sampling_rate = tvnorm->Fsc;
  215. f->samples_per_line = VBI_BPL;
  216. f->sample_format = V4L2_PIX_FMT_GREY;
  217. f->offset = VBI_OFFSET;
  218. f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
  219. for (i = 0; i < 2; ++i) {
  220. if (0 == f->count[i]) {
  221. /* No data from this field. We leave f->start[i]
  222. alone because VIDIOCSVBIFMT is w/o and EINVALs
  223. when a driver does not support exactly the
  224. requested parameters. */
  225. } else {
  226. s64 start, count;
  227. start = clamp(f->start[i], min_start, max_start);
  228. /* s64 to prevent overflow. */
  229. count = (s64) f->start[i] + f->count[i] - start;
  230. f->start[i] = start;
  231. f->count[i] = clamp(count, (s64) 1,
  232. max_end - start);
  233. }
  234. min_start += f2_offset;
  235. max_start += f2_offset;
  236. max_end += f2_offset;
  237. }
  238. if (0 == (f->count[0] | f->count[1])) {
  239. /* As in earlier driver versions. */
  240. f->start[0] = tvnorm->vbistart[0];
  241. f->start[1] = tvnorm->vbistart[1];
  242. f->count[0] = 1;
  243. f->count[1] = 1;
  244. }
  245. f->flags = 0;
  246. f->reserved[0] = 0;
  247. f->reserved[1] = 0;
  248. return 0;
  249. }
  250. int
  251. bttv_vbi_try_fmt (struct bttv_fh * fh,
  252. struct v4l2_vbi_format * f)
  253. {
  254. struct bttv *btv = fh->btv;
  255. const struct bttv_tvnorm *tvnorm;
  256. __s32 crop_start;
  257. mutex_lock(&btv->lock);
  258. tvnorm = &bttv_tvnorms[btv->tvnorm];
  259. crop_start = btv->crop_start;
  260. mutex_unlock(&btv->lock);
  261. return try_fmt(f, tvnorm, crop_start);
  262. }
  263. int
  264. bttv_vbi_set_fmt (struct bttv_fh * fh,
  265. struct v4l2_vbi_format * f)
  266. {
  267. struct bttv *btv = fh->btv;
  268. const struct bttv_tvnorm *tvnorm;
  269. __s32 start1, end;
  270. int rc;
  271. mutex_lock(&btv->lock);
  272. rc = -EBUSY;
  273. if (fh->resources & RESOURCE_VBI)
  274. goto fail;
  275. tvnorm = &bttv_tvnorms[btv->tvnorm];
  276. rc = try_fmt(f, tvnorm, btv->crop_start);
  277. if (0 != rc)
  278. goto fail;
  279. start1 = f->start[1] - tvnorm->vbistart[1] + tvnorm->vbistart[0];
  280. /* First possible line of video capturing. Should be
  281. max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
  282. when capturing both fields. But for compatibility we must
  283. pretend the VBI and video capture window may overlap,
  284. so end = start + 1, the lowest possible value, times two
  285. because vbi_fmt.end counts field lines times two. */
  286. end = max(f->start[0], start1) * 2 + 2;
  287. mutex_lock(&fh->vbi.lock);
  288. fh->vbi_fmt.fmt = *f;
  289. fh->vbi_fmt.tvnorm = tvnorm;
  290. fh->vbi_fmt.end = end;
  291. mutex_unlock(&fh->vbi.lock);
  292. rc = 0;
  293. fail:
  294. mutex_unlock(&btv->lock);
  295. return rc;
  296. }
  297. void
  298. bttv_vbi_get_fmt (struct bttv_fh * fh,
  299. struct v4l2_vbi_format * f)
  300. {
  301. const struct bttv_tvnorm *tvnorm;
  302. *f = fh->vbi_fmt.fmt;
  303. tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
  304. if (tvnorm != fh->vbi_fmt.tvnorm) {
  305. __s32 max_end;
  306. unsigned int i;
  307. /* As in vbi_buffer_prepare() this imitates the
  308. behaviour of earlier driver versions after video
  309. standard changes, with default parameters anyway. */
  310. max_end = (tvnorm->cropcap.bounds.top
  311. + tvnorm->cropcap.bounds.height) >> 1;
  312. f->sampling_rate = tvnorm->Fsc;
  313. for (i = 0; i < 2; ++i) {
  314. __s32 new_start;
  315. new_start = f->start[i]
  316. + tvnorm->vbistart[i]
  317. - fh->vbi_fmt.tvnorm->vbistart[i];
  318. f->start[i] = min(new_start, max_end - 1);
  319. f->count[i] = min((__s32) f->count[i],
  320. max_end - f->start[i]);
  321. max_end += tvnorm->vbistart[1]
  322. - tvnorm->vbistart[0];
  323. }
  324. }
  325. }
  326. void
  327. bttv_vbi_fmt_reset (struct bttv_vbi_fmt * f,
  328. int norm)
  329. {
  330. const struct bttv_tvnorm *tvnorm;
  331. unsigned int real_samples_per_line;
  332. unsigned int real_count;
  333. tvnorm = &bttv_tvnorms[norm];
  334. f->fmt.sampling_rate = tvnorm->Fsc;
  335. f->fmt.samples_per_line = VBI_BPL;
  336. f->fmt.sample_format = V4L2_PIX_FMT_GREY;
  337. f->fmt.offset = VBI_OFFSET;
  338. f->fmt.start[0] = tvnorm->vbistart[0];
  339. f->fmt.start[1] = tvnorm->vbistart[1];
  340. f->fmt.count[0] = VBI_DEFLINES;
  341. f->fmt.count[1] = VBI_DEFLINES;
  342. f->fmt.flags = 0;
  343. f->fmt.reserved[0] = 0;
  344. f->fmt.reserved[1] = 0;
  345. /* For compatibility the buffer size must be 2 * VBI_DEFLINES *
  346. VBI_BPL regardless of the current video standard. */
  347. real_samples_per_line = 1024 + tvnorm->vbipack * 4;
  348. real_count = ((tvnorm->cropcap.defrect.top >> 1)
  349. - tvnorm->vbistart[0]);
  350. BUG_ON(real_samples_per_line > VBI_BPL);
  351. BUG_ON(real_count > VBI_DEFLINES);
  352. f->tvnorm = tvnorm;
  353. /* See bttv_vbi_fmt_set(). */
  354. f->end = tvnorm->vbistart[0] * 2 + 2;
  355. }
  356. /* ----------------------------------------------------------------------- */
  357. /*
  358. * Local variables:
  359. * c-basic-offset: 8
  360. * End:
  361. */