/drivers/media/video/bt8xx/bttv-vbi.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 454 lines · 281 code · 90 blank · 83 comment · 40 complexity · 4e9e22b8b7a3a0a252e6df71d49e8c32 MD5 · raw file

  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/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/kernel.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kdev_t.h>
  25. #include <media/v4l2-ioctl.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;
  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 (VIDEOBUF_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. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  125. bpl = 2044; /* max. vbipack */
  126. padding = VBI_BPL - bpl;
  127. if (fh->vbi_fmt.fmt.count[0] > 0) {
  128. rc = bttv_risc_packed(btv, &buf->top,
  129. dma->sglist,
  130. /* offset */ 0, bpl,
  131. padding, skip_lines0,
  132. fh->vbi_fmt.fmt.count[0]);
  133. if (0 != rc)
  134. goto fail;
  135. }
  136. if (fh->vbi_fmt.fmt.count[1] > 0) {
  137. offset = fh->vbi_fmt.fmt.count[0] * VBI_BPL;
  138. rc = bttv_risc_packed(btv, &buf->bottom,
  139. dma->sglist,
  140. offset, bpl,
  141. padding, skip_lines1,
  142. fh->vbi_fmt.fmt.count[1]);
  143. if (0 != rc)
  144. goto fail;
  145. }
  146. }
  147. /* VBI capturing ends at VDELAY, start of video capturing,
  148. no matter where the RISC program ends. VDELAY minimum is 2,
  149. bounds.top is the corresponding first field line number
  150. times two. VDELAY counts half field lines. */
  151. min_vdelay = MIN_VDELAY;
  152. if (fh->vbi_fmt.end >= tvnorm->cropcap.bounds.top)
  153. min_vdelay += fh->vbi_fmt.end - tvnorm->cropcap.bounds.top;
  154. /* For bttv_buffer_activate_vbi(). */
  155. buf->geo.vdelay = min_vdelay;
  156. buf->vb.state = VIDEOBUF_PREPARED;
  157. buf->vb.field = field;
  158. dprintk("buf prepare %p: top=%p bottom=%p field=%s\n",
  159. vb, &buf->top, &buf->bottom,
  160. v4l2_field_names[buf->vb.field]);
  161. return 0;
  162. fail:
  163. bttv_dma_free(q,btv,buf);
  164. return rc;
  165. }
  166. static void
  167. vbi_buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  168. {
  169. struct bttv_fh *fh = q->priv_data;
  170. struct bttv *btv = fh->btv;
  171. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  172. dprintk("queue %p\n",vb);
  173. buf->vb.state = VIDEOBUF_QUEUED;
  174. list_add_tail(&buf->vb.queue,&btv->vcapture);
  175. if (NULL == btv->cvbi) {
  176. fh->btv->loop_irq |= 4;
  177. bttv_set_dma(btv,0x0c);
  178. }
  179. }
  180. static void vbi_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  181. {
  182. struct bttv_fh *fh = q->priv_data;
  183. struct bttv *btv = fh->btv;
  184. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  185. dprintk("free %p\n",vb);
  186. bttv_dma_free(q,fh->btv,buf);
  187. }
  188. struct videobuf_queue_ops bttv_vbi_qops = {
  189. .buf_setup = vbi_buffer_setup,
  190. .buf_prepare = vbi_buffer_prepare,
  191. .buf_queue = vbi_buffer_queue,
  192. .buf_release = vbi_buffer_release,
  193. };
  194. /* ----------------------------------------------------------------------- */
  195. static int try_fmt(struct v4l2_vbi_format *f, const struct bttv_tvnorm *tvnorm,
  196. __s32 crop_start)
  197. {
  198. __s32 min_start, max_start, max_end, f2_offset;
  199. unsigned int i;
  200. /* For compatibility with earlier driver versions we must pretend
  201. the VBI and video capture window may overlap. In reality RISC
  202. magic aborts VBI capturing at the first line of video capturing,
  203. leaving the rest of the buffer unchanged, usually all zero.
  204. VBI capturing must always start before video capturing. >> 1
  205. because cropping counts field lines times two. */
  206. min_start = tvnorm->vbistart[0];
  207. max_start = (crop_start >> 1) - 1;
  208. max_end = (tvnorm->cropcap.bounds.top
  209. + tvnorm->cropcap.bounds.height) >> 1;
  210. if (min_start > max_start)
  211. return -EBUSY;
  212. BUG_ON(max_start >= max_end);
  213. f->sampling_rate = tvnorm->Fsc;
  214. f->samples_per_line = VBI_BPL;
  215. f->sample_format = V4L2_PIX_FMT_GREY;
  216. f->offset = VBI_OFFSET;
  217. f2_offset = tvnorm->vbistart[1] - tvnorm->vbistart[0];
  218. for (i = 0; i < 2; ++i) {
  219. if (0 == f->count[i]) {
  220. /* No data from this field. We leave f->start[i]
  221. alone because VIDIOCSVBIFMT is w/o and EINVALs
  222. when a driver does not support exactly the
  223. requested parameters. */
  224. } else {
  225. s64 start, count;
  226. start = clamp(f->start[i], min_start, max_start);
  227. /* s64 to prevent overflow. */
  228. count = (s64) f->start[i] + f->count[i] - start;
  229. f->start[i] = start;
  230. f->count[i] = clamp(count, (s64) 1,
  231. max_end - start);
  232. }
  233. min_start += f2_offset;
  234. max_start += f2_offset;
  235. max_end += f2_offset;
  236. }
  237. if (0 == (f->count[0] | f->count[1])) {
  238. /* As in earlier driver versions. */
  239. f->start[0] = tvnorm->vbistart[0];
  240. f->start[1] = tvnorm->vbistart[1];
  241. f->count[0] = 1;
  242. f->count[1] = 1;
  243. }
  244. f->flags = 0;
  245. f->reserved[0] = 0;
  246. f->reserved[1] = 0;
  247. return 0;
  248. }
  249. int bttv_try_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
  250. {
  251. struct bttv_fh *fh = f;
  252. struct bttv *btv = fh->btv;
  253. const struct bttv_tvnorm *tvnorm;
  254. __s32 crop_start;
  255. mutex_lock(&btv->lock);
  256. tvnorm = &bttv_tvnorms[btv->tvnorm];
  257. crop_start = btv->crop_start;
  258. mutex_unlock(&btv->lock);
  259. return try_fmt(&frt->fmt.vbi, tvnorm, crop_start);
  260. }
  261. int bttv_s_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
  262. {
  263. struct bttv_fh *fh = f;
  264. struct bttv *btv = fh->btv;
  265. const struct bttv_tvnorm *tvnorm;
  266. __s32 start1, end;
  267. int rc;
  268. mutex_lock(&btv->lock);
  269. rc = -EBUSY;
  270. if (fh->resources & RESOURCE_VBI)
  271. goto fail;
  272. tvnorm = &bttv_tvnorms[btv->tvnorm];
  273. rc = try_fmt(&frt->fmt.vbi, tvnorm, btv->crop_start);
  274. if (0 != rc)
  275. goto fail;
  276. start1 = frt->fmt.vbi.start[1] - tvnorm->vbistart[1] +
  277. tvnorm->vbistart[0];
  278. /* First possible line of video capturing. Should be
  279. max(f->start[0] + f->count[0], start1 + f->count[1]) * 2
  280. when capturing both fields. But for compatibility we must
  281. pretend the VBI and video capture window may overlap,
  282. so end = start + 1, the lowest possible value, times two
  283. because vbi_fmt.end counts field lines times two. */
  284. end = max(frt->fmt.vbi.start[0], start1) * 2 + 2;
  285. mutex_lock(&fh->vbi.vb_lock);
  286. fh->vbi_fmt.fmt = frt->fmt.vbi;
  287. fh->vbi_fmt.tvnorm = tvnorm;
  288. fh->vbi_fmt.end = end;
  289. mutex_unlock(&fh->vbi.vb_lock);
  290. rc = 0;
  291. fail:
  292. mutex_unlock(&btv->lock);
  293. return rc;
  294. }
  295. int bttv_g_fmt_vbi_cap(struct file *file, void *f, struct v4l2_format *frt)
  296. {
  297. struct bttv_fh *fh = f;
  298. const struct bttv_tvnorm *tvnorm;
  299. frt->fmt.vbi = fh->vbi_fmt.fmt;
  300. tvnorm = &bttv_tvnorms[fh->btv->tvnorm];
  301. if (tvnorm != fh->vbi_fmt.tvnorm) {
  302. __s32 max_end;
  303. unsigned int i;
  304. /* As in vbi_buffer_prepare() this imitates the
  305. behaviour of earlier driver versions after video
  306. standard changes, with default parameters anyway. */
  307. max_end = (tvnorm->cropcap.bounds.top
  308. + tvnorm->cropcap.bounds.height) >> 1;
  309. frt->fmt.vbi.sampling_rate = tvnorm->Fsc;
  310. for (i = 0; i < 2; ++i) {
  311. __s32 new_start;
  312. new_start = frt->fmt.vbi.start[i]
  313. + tvnorm->vbistart[i]
  314. - fh->vbi_fmt.tvnorm->vbistart[i];
  315. frt->fmt.vbi.start[i] = min(new_start, max_end - 1);
  316. frt->fmt.vbi.count[i] =
  317. min((__s32) frt->fmt.vbi.count[i],
  318. max_end - frt->fmt.vbi.start[i]);
  319. max_end += tvnorm->vbistart[1]
  320. - tvnorm->vbistart[0];
  321. }
  322. }
  323. return 0;
  324. }
  325. void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, unsigned int norm)
  326. {
  327. const struct bttv_tvnorm *tvnorm;
  328. unsigned int real_samples_per_line;
  329. unsigned int real_count;
  330. tvnorm = &bttv_tvnorms[norm];
  331. f->fmt.sampling_rate = tvnorm->Fsc;
  332. f->fmt.samples_per_line = VBI_BPL;
  333. f->fmt.sample_format = V4L2_PIX_FMT_GREY;
  334. f->fmt.offset = VBI_OFFSET;
  335. f->fmt.start[0] = tvnorm->vbistart[0];
  336. f->fmt.start[1] = tvnorm->vbistart[1];
  337. f->fmt.count[0] = VBI_DEFLINES;
  338. f->fmt.count[1] = VBI_DEFLINES;
  339. f->fmt.flags = 0;
  340. f->fmt.reserved[0] = 0;
  341. f->fmt.reserved[1] = 0;
  342. /* For compatibility the buffer size must be 2 * VBI_DEFLINES *
  343. VBI_BPL regardless of the current video standard. */
  344. real_samples_per_line = 1024 + tvnorm->vbipack * 4;
  345. real_count = ((tvnorm->cropcap.defrect.top >> 1)
  346. - tvnorm->vbistart[0]);
  347. BUG_ON(real_samples_per_line > VBI_BPL);
  348. BUG_ON(real_count > VBI_DEFLINES);
  349. f->tvnorm = tvnorm;
  350. /* See bttv_vbi_fmt_set(). */
  351. f->end = tvnorm->vbistart[0] * 2 + 2;
  352. }
  353. /* ----------------------------------------------------------------------- */
  354. /*
  355. * Local variables:
  356. * c-basic-offset: 8
  357. * End:
  358. */