bttv-vbi.c 12 KB

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