Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 2 | /* |
| 3 | * cx18 init/start/stop/exit stream functions |
| 4 | * |
| 5 | * Derived from ivtv-streams.c |
| 6 | * |
| 7 | * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl> |
Andy Walls | 6afdeaf | 2010-05-23 18:53:35 -0300 | [diff] [blame] | 8 | * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net> |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "cx18-driver.h" |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 12 | #include "cx18-io.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 13 | #include "cx18-fileops.h" |
| 14 | #include "cx18-mailbox.h" |
| 15 | #include "cx18-i2c.h" |
| 16 | #include "cx18-queue.h" |
| 17 | #include "cx18-ioctl.h" |
| 18 | #include "cx18-streams.h" |
| 19 | #include "cx18-cards.h" |
| 20 | #include "cx18-scb.h" |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 21 | #include "cx18-dvb.h" |
| 22 | |
Mauro Carvalho Chehab | 6e6a8b5 | 2018-01-04 13:08:56 -0500 | [diff] [blame] | 23 | #define CX18_DSP0_INTERRUPT_MASK 0xd0004C |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 24 | |
Bhumika Goyal | e66fb18 | 2017-08-26 02:02:08 -0400 | [diff] [blame] | 25 | static const struct v4l2_file_operations cx18_v4l2_enc_fops = { |
Hans Verkuil | daf20d9 | 2008-05-12 11:21:58 -0300 | [diff] [blame] | 26 | .owner = THIS_MODULE, |
| 27 | .read = cx18_v4l2_read, |
| 28 | .open = cx18_v4l2_open, |
Hans Verkuil | dcaded7 | 2012-06-09 10:54:19 -0300 | [diff] [blame] | 29 | .unlocked_ioctl = video_ioctl2, |
Hans Verkuil | daf20d9 | 2008-05-12 11:21:58 -0300 | [diff] [blame] | 30 | .release = cx18_v4l2_close, |
| 31 | .poll = cx18_v4l2_enc_poll, |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 32 | .mmap = cx18_v4l2_mmap, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /* offset from 0 to register ts v4l2 minors on */ |
| 36 | #define CX18_V4L2_ENC_TS_OFFSET 16 |
| 37 | /* offset from 0 to register pcm v4l2 minors on */ |
| 38 | #define CX18_V4L2_ENC_PCM_OFFSET 24 |
| 39 | /* offset from 0 to register yuv v4l2 minors on */ |
| 40 | #define CX18_V4L2_ENC_YUV_OFFSET 32 |
| 41 | |
| 42 | static struct { |
| 43 | const char *name; |
| 44 | int vfl_type; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 45 | int num_offset; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 46 | int dma; |
Hans Verkuil | dfdf780 | 2014-11-24 06:37:21 -0300 | [diff] [blame] | 47 | u32 caps; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 48 | } cx18_stream_info[] = { |
| 49 | { /* CX18_ENC_STREAM_TYPE_MPG */ |
| 50 | "encoder MPEG", |
| 51 | VFL_TYPE_GRABBER, 0, |
Hans Verkuil | ff82b211 | 2012-09-17 05:02:38 -0300 | [diff] [blame] | 52 | PCI_DMA_FROMDEVICE, |
Hans Verkuil | dfdf780 | 2014-11-24 06:37:21 -0300 | [diff] [blame] | 53 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | |
| 54 | V4L2_CAP_AUDIO | V4L2_CAP_TUNER |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 55 | }, |
| 56 | { /* CX18_ENC_STREAM_TYPE_TS */ |
| 57 | "TS", |
| 58 | VFL_TYPE_GRABBER, -1, |
Hans Verkuil | ff82b211 | 2012-09-17 05:02:38 -0300 | [diff] [blame] | 59 | PCI_DMA_FROMDEVICE, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 60 | }, |
| 61 | { /* CX18_ENC_STREAM_TYPE_YUV */ |
| 62 | "encoder YUV", |
| 63 | VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET, |
Hans Verkuil | ff82b211 | 2012-09-17 05:02:38 -0300 | [diff] [blame] | 64 | PCI_DMA_FROMDEVICE, |
Hans Verkuil | dfdf780 | 2014-11-24 06:37:21 -0300 | [diff] [blame] | 65 | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | |
| 66 | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO | V4L2_CAP_TUNER |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 67 | }, |
| 68 | { /* CX18_ENC_STREAM_TYPE_VBI */ |
| 69 | "encoder VBI", |
| 70 | VFL_TYPE_VBI, 0, |
Hans Verkuil | ff82b211 | 2012-09-17 05:02:38 -0300 | [diff] [blame] | 71 | PCI_DMA_FROMDEVICE, |
Hans Verkuil | dfdf780 | 2014-11-24 06:37:21 -0300 | [diff] [blame] | 72 | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE | |
| 73 | V4L2_CAP_READWRITE | V4L2_CAP_TUNER |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 74 | }, |
| 75 | { /* CX18_ENC_STREAM_TYPE_PCM */ |
| 76 | "encoder PCM audio", |
| 77 | VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET, |
Hans Verkuil | ff82b211 | 2012-09-17 05:02:38 -0300 | [diff] [blame] | 78 | PCI_DMA_FROMDEVICE, |
Hans Verkuil | 2b4fd3e | 2015-04-24 03:55:07 -0300 | [diff] [blame] | 79 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 80 | }, |
| 81 | { /* CX18_ENC_STREAM_TYPE_IDX */ |
| 82 | "encoder IDX", |
| 83 | VFL_TYPE_GRABBER, -1, |
Hans Verkuil | ff82b211 | 2012-09-17 05:02:38 -0300 | [diff] [blame] | 84 | PCI_DMA_FROMDEVICE, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 85 | }, |
| 86 | { /* CX18_ENC_STREAM_TYPE_RAD */ |
| 87 | "encoder radio", |
| 88 | VFL_TYPE_RADIO, 0, |
Hans Verkuil | ff82b211 | 2012-09-17 05:02:38 -0300 | [diff] [blame] | 89 | PCI_DMA_NONE, |
Hans Verkuil | dfdf780 | 2014-11-24 06:37:21 -0300 | [diff] [blame] | 90 | V4L2_CAP_RADIO | V4L2_CAP_TUNER |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 91 | }, |
| 92 | }; |
| 93 | |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 94 | |
Mauro Carvalho Chehab | 5e6e81b | 2012-10-27 11:28:50 -0300 | [diff] [blame] | 95 | static void cx18_dma_free(struct videobuf_queue *q, |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 96 | struct cx18_stream *s, struct cx18_videobuf_buffer *buf) |
| 97 | { |
| 98 | videobuf_waiton(q, &buf->vb, 0, 0); |
| 99 | videobuf_vmalloc_free(&buf->vb); |
| 100 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
| 101 | } |
| 102 | |
| 103 | static int cx18_prepare_buffer(struct videobuf_queue *q, |
| 104 | struct cx18_stream *s, |
| 105 | struct cx18_videobuf_buffer *buf, |
| 106 | u32 pixelformat, |
| 107 | unsigned int width, unsigned int height, |
| 108 | enum v4l2_field field) |
| 109 | { |
Mauro Carvalho Chehab | 4a3fad7 | 2018-01-04 06:47:28 -0500 | [diff] [blame] | 110 | struct cx18 *cx = s->cx; |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 111 | int rc = 0; |
| 112 | |
| 113 | /* check settings */ |
| 114 | buf->bytes_used = 0; |
| 115 | |
| 116 | if ((width < 48) || (height < 32)) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | buf->vb.size = (width * height * 2); |
| 120 | if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size)) |
| 121 | return -EINVAL; |
| 122 | |
| 123 | /* alloc + fill struct (if changed) */ |
| 124 | if (buf->vb.width != width || buf->vb.height != height || |
| 125 | buf->vb.field != field || s->pixelformat != pixelformat || |
| 126 | buf->tvnorm != cx->std) { |
| 127 | |
| 128 | buf->vb.width = width; |
| 129 | buf->vb.height = height; |
| 130 | buf->vb.field = field; |
| 131 | buf->tvnorm = cx->std; |
| 132 | s->pixelformat = pixelformat; |
| 133 | |
Simon Farnsworth | 09fc980 | 2011-09-05 12:23:12 -0300 | [diff] [blame] | 134 | /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2))) |
| 135 | UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */ |
| 136 | if (s->pixelformat == V4L2_PIX_FMT_HM12) |
| 137 | s->vb_bytes_per_frame = height * 720 * 3 / 2; |
| 138 | else |
| 139 | s->vb_bytes_per_frame = height * 720 * 2; |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 140 | cx18_dma_free(q, s, buf); |
| 141 | } |
| 142 | |
| 143 | if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size)) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | if (buf->vb.field == 0) |
| 147 | buf->vb.field = V4L2_FIELD_INTERLACED; |
| 148 | |
| 149 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
| 150 | buf->vb.width = width; |
| 151 | buf->vb.height = height; |
| 152 | buf->vb.field = field; |
| 153 | buf->tvnorm = cx->std; |
| 154 | s->pixelformat = pixelformat; |
| 155 | |
Simon Farnsworth | 09fc980 | 2011-09-05 12:23:12 -0300 | [diff] [blame] | 156 | /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2))) |
| 157 | UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */ |
| 158 | if (s->pixelformat == V4L2_PIX_FMT_HM12) |
| 159 | s->vb_bytes_per_frame = height * 720 * 3 / 2; |
| 160 | else |
| 161 | s->vb_bytes_per_frame = height * 720 * 2; |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 162 | rc = videobuf_iolock(q, &buf->vb, NULL); |
| 163 | if (rc != 0) |
| 164 | goto fail; |
| 165 | } |
| 166 | buf->vb.state = VIDEOBUF_PREPARED; |
| 167 | return 0; |
| 168 | |
| 169 | fail: |
| 170 | cx18_dma_free(q, s, buf); |
| 171 | return rc; |
| 172 | |
| 173 | } |
| 174 | |
| 175 | /* VB_MIN_BUFSIZE is lcm(1440 * 480, 1440 * 576) |
| 176 | 1440 is a single line of 4:2:2 YUV at 720 luma samples wide |
| 177 | */ |
| 178 | #define VB_MIN_BUFFERS 32 |
| 179 | #define VB_MIN_BUFSIZE 4147200 |
| 180 | |
| 181 | static int buffer_setup(struct videobuf_queue *q, |
| 182 | unsigned int *count, unsigned int *size) |
| 183 | { |
| 184 | struct cx18_stream *s = q->priv_data; |
| 185 | struct cx18 *cx = s->cx; |
| 186 | |
| 187 | *size = 2 * cx->cxhdl.width * cx->cxhdl.height; |
| 188 | if (*count == 0) |
| 189 | *count = VB_MIN_BUFFERS; |
| 190 | |
| 191 | while (*size * *count > VB_MIN_BUFFERS * VB_MIN_BUFSIZE) |
| 192 | (*count)--; |
| 193 | |
| 194 | q->field = V4L2_FIELD_INTERLACED; |
| 195 | q->last = V4L2_FIELD_INTERLACED; |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int buffer_prepare(struct videobuf_queue *q, |
| 201 | struct videobuf_buffer *vb, |
| 202 | enum v4l2_field field) |
| 203 | { |
| 204 | struct cx18_videobuf_buffer *buf = |
| 205 | container_of(vb, struct cx18_videobuf_buffer, vb); |
| 206 | struct cx18_stream *s = q->priv_data; |
| 207 | struct cx18 *cx = s->cx; |
| 208 | |
| 209 | return cx18_prepare_buffer(q, s, buf, s->pixelformat, |
| 210 | cx->cxhdl.width, cx->cxhdl.height, field); |
| 211 | } |
| 212 | |
| 213 | static void buffer_release(struct videobuf_queue *q, |
| 214 | struct videobuf_buffer *vb) |
| 215 | { |
| 216 | struct cx18_videobuf_buffer *buf = |
| 217 | container_of(vb, struct cx18_videobuf_buffer, vb); |
| 218 | struct cx18_stream *s = q->priv_data; |
| 219 | |
| 220 | cx18_dma_free(q, s, buf); |
| 221 | } |
| 222 | |
| 223 | static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) |
| 224 | { |
| 225 | struct cx18_videobuf_buffer *buf = |
| 226 | container_of(vb, struct cx18_videobuf_buffer, vb); |
| 227 | struct cx18_stream *s = q->priv_data; |
| 228 | |
| 229 | buf->vb.state = VIDEOBUF_QUEUED; |
| 230 | |
| 231 | list_add_tail(&buf->vb.queue, &s->vb_capture); |
| 232 | } |
| 233 | |
Julia Lawall | 6688324 | 2017-08-04 08:09:44 -0400 | [diff] [blame] | 234 | static const struct videobuf_queue_ops cx18_videobuf_qops = { |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 235 | .buf_setup = buffer_setup, |
| 236 | .buf_prepare = buffer_prepare, |
| 237 | .buf_queue = buffer_queue, |
| 238 | .buf_release = buffer_release, |
| 239 | }; |
| 240 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 241 | static void cx18_stream_init(struct cx18 *cx, int type) |
| 242 | { |
| 243 | struct cx18_stream *s = &cx->streams[type]; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 244 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 245 | memset(s, 0, sizeof(*s)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 246 | |
| 247 | /* initialize cx18_stream fields */ |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 248 | s->dvb = NULL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 249 | s->cx = cx; |
| 250 | s->type = type; |
| 251 | s->name = cx18_stream_info[type].name; |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame] | 252 | s->handle = CX18_INVALID_TASK_HANDLE; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 253 | |
| 254 | s->dma = cx18_stream_info[type].dma; |
Hans Verkuil | dfdf780 | 2014-11-24 06:37:21 -0300 | [diff] [blame] | 255 | s->v4l2_dev_caps = cx18_stream_info[type].caps; |
Andy Walls | 6ecd86dc | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 256 | s->buffers = cx->stream_buffers[type]; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 257 | s->buf_size = cx->stream_buf_size[type]; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 258 | INIT_LIST_HEAD(&s->buf_pool); |
| 259 | s->bufs_per_mdl = 1; |
| 260 | s->mdl_size = s->buf_size * s->bufs_per_mdl; |
Andy Walls | 6ecd86dc | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 261 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 262 | init_waitqueue_head(&s->waitq); |
| 263 | s->id = -1; |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 264 | spin_lock_init(&s->q_free.lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 265 | cx18_queue_init(&s->q_free); |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 266 | spin_lock_init(&s->q_busy.lock); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 267 | cx18_queue_init(&s->q_busy); |
Andy Walls | 40c5520 | 2009-04-13 23:08:00 -0300 | [diff] [blame] | 268 | spin_lock_init(&s->q_full.lock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 269 | cx18_queue_init(&s->q_full); |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 270 | spin_lock_init(&s->q_idle.lock); |
| 271 | cx18_queue_init(&s->q_idle); |
Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 272 | |
| 273 | INIT_WORK(&s->out_work_order, cx18_out_work_handler); |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 274 | |
| 275 | INIT_LIST_HEAD(&s->vb_capture); |
Kees Cook | 162e637 | 2017-10-24 11:22:42 -0400 | [diff] [blame] | 276 | timer_setup(&s->vb_timeout, cx18_vb_timeout, 0); |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 277 | spin_lock_init(&s->vb_lock); |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 278 | if (type == CX18_ENC_STREAM_TYPE_YUV) { |
Simon Farnsworth | 612031c | 2011-05-10 10:49:50 -0300 | [diff] [blame] | 279 | spin_lock_init(&s->vbuf_q_lock); |
| 280 | |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 281 | s->vb_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 282 | videobuf_queue_vmalloc_init(&s->vbuf_q, &cx18_videobuf_qops, |
| 283 | &cx->pci_dev->dev, &s->vbuf_q_lock, |
| 284 | V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 285 | V4L2_FIELD_INTERLACED, |
| 286 | sizeof(struct cx18_videobuf_buffer), |
| 287 | s, &cx->serialize_lock); |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 288 | |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 289 | /* Assume the previous pixel default */ |
| 290 | s->pixelformat = V4L2_PIX_FMT_HM12; |
Simon Farnsworth | 09fc980 | 2011-09-05 12:23:12 -0300 | [diff] [blame] | 291 | s->vb_bytes_per_frame = cx->cxhdl.height * 720 * 3 / 2; |
Hans Verkuil | 3a29a4f | 2015-04-02 08:34:30 -0300 | [diff] [blame] | 292 | s->vb_bytes_per_line = 720; |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 293 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int cx18_prep_dev(struct cx18 *cx, int type) |
| 297 | { |
| 298 | struct cx18_stream *s = &cx->streams[type]; |
| 299 | u32 cap = cx->v4l2_cap; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 300 | int num_offset = cx18_stream_info[type].num_offset; |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 301 | int num = cx->instance + cx18_first_minor + num_offset; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 302 | |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 303 | /* |
| 304 | * These five fields are always initialized. |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 305 | * For analog capture related streams, if video_dev.v4l2_dev == NULL then the |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 306 | * stream is not in use. |
| 307 | * For the TS stream, if dvb == NULL then the stream is not in use. |
| 308 | * In those cases no other fields but these four can be used. |
| 309 | */ |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 310 | s->video_dev.v4l2_dev = NULL; |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 311 | s->dvb = NULL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 312 | s->cx = cx; |
| 313 | s->type = type; |
| 314 | s->name = cx18_stream_info[type].name; |
| 315 | |
| 316 | /* Check whether the radio is supported */ |
| 317 | if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO)) |
| 318 | return 0; |
| 319 | |
| 320 | /* Check whether VBI is supported */ |
| 321 | if (type == CX18_ENC_STREAM_TYPE_VBI && |
| 322 | !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE))) |
| 323 | return 0; |
| 324 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 325 | /* User explicitly selected 0 buffers for these streams, so don't |
| 326 | create them. */ |
| 327 | if (cx18_stream_info[type].dma != PCI_DMA_NONE && |
Andy Walls | 6ecd86dc | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 328 | cx->stream_buffers[type] == 0) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 329 | CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | cx18_stream_init(cx, type); |
| 334 | |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 335 | /* Allocate the cx18_dvb struct only for the TS on cards with DTV */ |
| 336 | if (type == CX18_ENC_STREAM_TYPE_TS) { |
| 337 | if (cx->card->hw_all & CX18_HW_DVB) { |
| 338 | s->dvb = kzalloc(sizeof(struct cx18_dvb), GFP_KERNEL); |
| 339 | if (s->dvb == NULL) { |
Mauro Carvalho Chehab | 6beb138 | 2016-10-18 17:44:03 -0200 | [diff] [blame] | 340 | CX18_ERR("Couldn't allocate cx18_dvb structure for %s\n", |
| 341 | s->name); |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 342 | return -ENOMEM; |
| 343 | } |
| 344 | } else { |
| 345 | /* Don't need buffers for the TS, if there is no DVB */ |
| 346 | s->buffers = 0; |
| 347 | } |
| 348 | } |
| 349 | |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 350 | if (num_offset == -1) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 351 | return 0; |
| 352 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 353 | /* initialize the v4l2 video device structure */ |
| 354 | snprintf(s->video_dev.name, sizeof(s->video_dev.name), "%s %s", |
Andy Walls | 5811cf9 | 2009-02-14 17:08:37 -0300 | [diff] [blame] | 355 | cx->v4l2_dev.name, s->name); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 356 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 357 | s->video_dev.num = num; |
| 358 | s->video_dev.v4l2_dev = &cx->v4l2_dev; |
| 359 | s->video_dev.fops = &cx18_v4l2_enc_fops; |
| 360 | s->video_dev.release = video_device_release_empty; |
Hans Verkuil | 3a29a4f | 2015-04-02 08:34:30 -0300 | [diff] [blame] | 361 | if (cx->card->video_inputs->video_type == CX18_CARD_INPUT_VID_TUNER) |
| 362 | s->video_dev.tvnorms = cx->tuner_std; |
| 363 | else |
| 364 | s->video_dev.tvnorms = V4L2_STD_ALL; |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 365 | s->video_dev.lock = &cx->serialize_lock; |
| 366 | cx18_set_funcs(&s->video_dev); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /* Initialize v4l2 variables and register v4l2 devices */ |
| 371 | int cx18_streams_setup(struct cx18 *cx) |
| 372 | { |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 373 | int type, ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 374 | |
| 375 | /* Setup V4L2 Devices */ |
| 376 | for (type = 0; type < CX18_MAX_STREAMS; type++) { |
| 377 | /* Prepare device */ |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 378 | ret = cx18_prep_dev(cx, type); |
| 379 | if (ret < 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 380 | break; |
| 381 | |
| 382 | /* Allocate Stream */ |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 383 | ret = cx18_stream_alloc(&cx->streams[type]); |
| 384 | if (ret < 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 385 | break; |
| 386 | } |
| 387 | if (type == CX18_MAX_STREAMS) |
| 388 | return 0; |
| 389 | |
| 390 | /* One or more streams could not be initialized. Clean 'em all up. */ |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 391 | cx18_streams_cleanup(cx, 0); |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 392 | return ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static int cx18_reg_dev(struct cx18 *cx, int type) |
| 396 | { |
| 397 | struct cx18_stream *s = &cx->streams[type]; |
| 398 | int vfl_type = cx18_stream_info[type].vfl_type; |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 399 | const char *name; |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 400 | int num, ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 401 | |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 402 | if (type == CX18_ENC_STREAM_TYPE_TS && s->dvb != NULL) { |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 403 | ret = cx18_dvb_register(s); |
| 404 | if (ret < 0) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 405 | CX18_ERR("DVB failed to register\n"); |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 406 | return ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 410 | if (s->video_dev.v4l2_dev == NULL) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 411 | return 0; |
| 412 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 413 | num = s->video_dev.num; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 414 | /* card number + user defined offset + device offset */ |
| 415 | if (type != CX18_ENC_STREAM_TYPE_MPG) { |
| 416 | struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG]; |
| 417 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 418 | if (s_mpg->video_dev.v4l2_dev) |
| 419 | num = s_mpg->video_dev.num |
Andy Walls | 3d05913d | 2009-01-10 21:54:39 -0300 | [diff] [blame] | 420 | + cx18_stream_info[type].num_offset; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 421 | } |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 422 | video_set_drvdata(&s->video_dev, s); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 423 | |
| 424 | /* Register device. First try the desired minor, then any free one. */ |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 425 | ret = video_register_device_no_warn(&s->video_dev, vfl_type, num); |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 426 | if (ret < 0) { |
Hans Verkuil | 581644d | 2009-06-19 11:54:00 -0300 | [diff] [blame] | 427 | CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n", |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 428 | s->name, num); |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 429 | s->video_dev.v4l2_dev = NULL; |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 430 | return ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 431 | } |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 432 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 433 | name = video_device_node_name(&s->video_dev); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 434 | |
| 435 | switch (vfl_type) { |
| 436 | case VFL_TYPE_GRABBER: |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 437 | CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n", |
| 438 | name, s->name, cx->stream_buffers[type], |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 439 | cx->stream_buf_size[type] / 1024, |
| 440 | (cx->stream_buf_size[type] * 100 / 1024) % 100); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 441 | break; |
| 442 | |
| 443 | case VFL_TYPE_RADIO: |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 444 | CX18_INFO("Registered device %s for %s\n", name, s->name); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 445 | break; |
| 446 | |
| 447 | case VFL_TYPE_VBI: |
Andy Walls | 6ecd86dc | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 448 | if (cx->stream_buffers[type]) |
Mauro Carvalho Chehab | 6beb138 | 2016-10-18 17:44:03 -0200 | [diff] [blame] | 449 | CX18_INFO("Registered device %s for %s (%d x %d bytes)\n", |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 450 | name, s->name, cx->stream_buffers[type], |
Andy Walls | 6ecd86dc | 2008-12-07 23:30:17 -0300 | [diff] [blame] | 451 | cx->stream_buf_size[type]); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 452 | else |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 453 | CX18_INFO("Registered device %s for %s\n", |
| 454 | name, s->name); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | /* Register v4l2 devices */ |
| 462 | int cx18_streams_register(struct cx18 *cx) |
| 463 | { |
| 464 | int type; |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 465 | int err; |
| 466 | int ret = 0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 467 | |
| 468 | /* Register V4L2 devices */ |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 469 | for (type = 0; type < CX18_MAX_STREAMS; type++) { |
| 470 | err = cx18_reg_dev(cx, type); |
| 471 | if (err && ret == 0) |
| 472 | ret = err; |
| 473 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 474 | |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 475 | if (ret == 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 476 | return 0; |
| 477 | |
| 478 | /* One or more streams could not be initialized. Clean 'em all up. */ |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 479 | cx18_streams_cleanup(cx, 1); |
Andy Walls | 9b4a7c8 | 2008-10-18 10:20:25 -0300 | [diff] [blame] | 480 | return ret; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* Unregister v4l2 devices */ |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 484 | void cx18_streams_cleanup(struct cx18 *cx, int unregister) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 485 | { |
| 486 | struct video_device *vdev; |
| 487 | int type; |
| 488 | |
| 489 | /* Teardown all streams */ |
| 490 | for (type = 0; type < CX18_MAX_STREAMS; type++) { |
Andy Walls | 7b1dde0 | 2009-12-31 01:35:08 -0300 | [diff] [blame] | 491 | |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 492 | /* The TS has a cx18_dvb structure, not a video_device */ |
Andy Walls | 7b1dde0 | 2009-12-31 01:35:08 -0300 | [diff] [blame] | 493 | if (type == CX18_ENC_STREAM_TYPE_TS) { |
Andy Walls | 754f996 | 2010-12-11 20:38:20 -0300 | [diff] [blame] | 494 | if (cx->streams[type].dvb != NULL) { |
| 495 | if (unregister) |
| 496 | cx18_dvb_unregister(&cx->streams[type]); |
| 497 | kfree(cx->streams[type].dvb); |
| 498 | cx->streams[type].dvb = NULL; |
Andy Walls | 7b1dde0 | 2009-12-31 01:35:08 -0300 | [diff] [blame] | 499 | cx18_stream_free(&cx->streams[type]); |
| 500 | } |
| 501 | continue; |
Hans Verkuil | fac3639 | 2008-07-18 10:07:10 -0300 | [diff] [blame] | 502 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 503 | |
Andy Walls | 7b1dde0 | 2009-12-31 01:35:08 -0300 | [diff] [blame] | 504 | /* No struct video_device, but can have buffers allocated */ |
| 505 | if (type == CX18_ENC_STREAM_TYPE_IDX) { |
Andy Walls | 0f890ab | 2011-03-27 20:19:15 -0300 | [diff] [blame] | 506 | /* If the module params didn't inhibit IDX ... */ |
Andy Walls | 7b1dde0 | 2009-12-31 01:35:08 -0300 | [diff] [blame] | 507 | if (cx->stream_buffers[type] != 0) { |
| 508 | cx->stream_buffers[type] = 0; |
Andy Walls | 0f890ab | 2011-03-27 20:19:15 -0300 | [diff] [blame] | 509 | /* |
| 510 | * Before calling cx18_stream_free(), |
| 511 | * check if the IDX stream was actually set up. |
| 512 | * Needed, since the cx18_probe() error path |
| 513 | * exits through here as well as normal clean up |
| 514 | */ |
| 515 | if (cx->streams[type].buffers != 0) |
| 516 | cx18_stream_free(&cx->streams[type]); |
Andy Walls | 7b1dde0 | 2009-12-31 01:35:08 -0300 | [diff] [blame] | 517 | } |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | /* If struct video_device exists, can have buffers allocated */ |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 522 | vdev = &cx->streams[type].video_dev; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 523 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 524 | if (vdev->v4l2_dev == NULL) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 525 | continue; |
| 526 | |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 527 | if (type == CX18_ENC_STREAM_TYPE_YUV) |
| 528 | videobuf_mmap_free(&cx->streams[type].vbuf_q); |
| 529 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 530 | cx18_stream_free(&cx->streams[type]); |
| 531 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 532 | video_unregister_device(vdev); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | static void cx18_vbi_setup(struct cx18_stream *s) |
| 537 | { |
| 538 | struct cx18 *cx = s->cx; |
Andy Walls | dd07343 | 2008-12-12 16:24:04 -0300 | [diff] [blame] | 539 | int raw = cx18_raw_vbi(cx); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 540 | u32 data[CX2341X_MBOX_MAX_DATA]; |
| 541 | int lines; |
| 542 | |
| 543 | if (cx->is_60hz) { |
| 544 | cx->vbi.count = 12; |
| 545 | cx->vbi.start[0] = 10; |
| 546 | cx->vbi.start[1] = 273; |
| 547 | } else { /* PAL/SECAM */ |
| 548 | cx->vbi.count = 18; |
| 549 | cx->vbi.start[0] = 6; |
| 550 | cx->vbi.start[1] = 318; |
| 551 | } |
| 552 | |
| 553 | /* setup VBI registers */ |
Hans Verkuil | add632c | 2010-03-14 12:24:15 -0300 | [diff] [blame] | 554 | if (raw) |
| 555 | v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &cx->vbi.in.fmt.vbi); |
| 556 | else |
| 557 | v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &cx->vbi.in.fmt.sliced); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 558 | |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 559 | /* |
| 560 | * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw |
| 561 | * VBI when the first analog capture channel starts, as once it starts |
| 562 | * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup |
| 563 | * (i.e. for the VBI capture channels). We also send it for each |
| 564 | * analog capture channel anyway just to make sure we get the proper |
| 565 | * behavior |
| 566 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 567 | if (raw) { |
| 568 | lines = cx->vbi.count * 2; |
| 569 | } else { |
Andy Walls | 812b1f9 | 2009-02-08 22:40:04 -0300 | [diff] [blame] | 570 | /* |
| 571 | * For 525/60 systems, according to the VIP 2 & BT.656 std: |
| 572 | * The EAV RP code's Field bit toggles on line 4, a few lines |
| 573 | * after the Vertcal Blank bit has already toggled. |
| 574 | * Tell the encoder to capture 21-4+1=18 lines per field, |
| 575 | * since we want lines 10 through 21. |
| 576 | * |
Andy Walls | 5ab7405 | 2009-05-10 22:14:29 -0300 | [diff] [blame] | 577 | * For 625/50 systems, according to the VIP 2 & BT.656 std: |
| 578 | * The EAV RP code's Field bit toggles on line 1, a few lines |
| 579 | * after the Vertcal Blank bit has already toggled. |
Andy Walls | 929a3ad | 2009-05-16 21:06:57 -0300 | [diff] [blame] | 580 | * (We've actually set the digitizer so that the Field bit |
| 581 | * toggles on line 2.) Tell the encoder to capture 23-2+1=22 |
| 582 | * lines per field, since we want lines 6 through 23. |
Andy Walls | 812b1f9 | 2009-02-08 22:40:04 -0300 | [diff] [blame] | 583 | */ |
Andy Walls | 929a3ad | 2009-05-16 21:06:57 -0300 | [diff] [blame] | 584 | lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 585 | } |
| 586 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 587 | data[0] = s->handle; |
| 588 | /* Lines per field */ |
| 589 | data[1] = (lines / 2) | ((lines / 2) << 16); |
| 590 | /* bytes per line */ |
Mauro Carvalho Chehab | 318de79 | 2016-06-24 08:40:51 -0300 | [diff] [blame] | 591 | data[2] = (raw ? VBI_ACTIVE_SAMPLES |
| 592 | : (cx->is_60hz ? VBI_HBLANK_SAMPLES_60HZ |
| 593 | : VBI_HBLANK_SAMPLES_50HZ)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 594 | /* Every X number of frames a VBI interrupt arrives |
| 595 | (frames as in 25 or 30 fps) */ |
| 596 | data[3] = 1; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 597 | /* |
| 598 | * Set the SAV/EAV RP codes to look for as start/stop points |
| 599 | * when in VIP-1.1 mode |
| 600 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 601 | if (raw) { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 602 | /* |
| 603 | * Start codes for beginning of "active" line in vertical blank |
| 604 | * 0x20 ( VerticalBlank ) |
| 605 | * 0x60 ( EvenField VerticalBlank ) |
| 606 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 607 | data[4] = 0x20602060; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 608 | /* |
| 609 | * End codes for end of "active" raw lines and regular lines |
| 610 | * 0x30 ( VerticalBlank HorizontalBlank) |
| 611 | * 0x70 ( EvenField VerticalBlank HorizontalBlank) |
| 612 | * 0x90 (Task HorizontalBlank) |
| 613 | * 0xd0 (Task EvenField HorizontalBlank) |
| 614 | */ |
Andy Walls | af009cf | 2008-12-12 20:00:29 -0300 | [diff] [blame] | 615 | data[5] = 0x307090d0; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 616 | } else { |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 617 | /* |
| 618 | * End codes for active video, we want data in the hblank region |
| 619 | * 0xb0 (Task 0 VerticalBlank HorizontalBlank) |
| 620 | * 0xf0 (Task EvenField VerticalBlank HorizontalBlank) |
| 621 | * |
| 622 | * Since the V bit is only allowed to toggle in the EAV RP code, |
| 623 | * just before the first active region line, these two |
Andy Walls | 812b1f9 | 2009-02-08 22:40:04 -0300 | [diff] [blame] | 624 | * are problematic: |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 625 | * 0x90 (Task HorizontalBlank) |
| 626 | * 0xd0 (Task EvenField HorizontalBlank) |
Andy Walls | 812b1f9 | 2009-02-08 22:40:04 -0300 | [diff] [blame] | 627 | * |
Andy Walls | af7c58b | 2009-02-28 20:13:50 -0300 | [diff] [blame] | 628 | * We have set the digitzer such that we don't have to worry |
| 629 | * about these problem codes. |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 630 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 631 | data[4] = 0xB0F0B0F0; |
Andy Walls | 302df97 | 2009-01-31 00:33:02 -0300 | [diff] [blame] | 632 | /* |
| 633 | * Start codes for beginning of active line in vertical blank |
| 634 | * 0xa0 (Task VerticalBlank ) |
| 635 | * 0xe0 (Task EvenField VerticalBlank ) |
| 636 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 637 | data[5] = 0xA0E0A0E0; |
| 638 | } |
| 639 | |
| 640 | CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n", |
| 641 | data[0], data[1], data[2], data[3], data[4], data[5]); |
| 642 | |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 643 | cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 644 | } |
| 645 | |
Andy Walls | ef991797 | 2009-12-31 18:27:13 -0300 | [diff] [blame] | 646 | void cx18_stream_rotate_idx_mdls(struct cx18 *cx) |
| 647 | { |
| 648 | struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX]; |
| 649 | struct cx18_mdl *mdl; |
| 650 | |
| 651 | if (!cx18_stream_enabled(s)) |
| 652 | return; |
| 653 | |
| 654 | /* Return if the firmware is not running low on MDLs */ |
| 655 | if ((atomic_read(&s->q_free.depth) + atomic_read(&s->q_busy.depth)) >= |
| 656 | CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN) |
| 657 | return; |
| 658 | |
| 659 | /* Return if there are no MDLs to rotate back to the firmware */ |
| 660 | if (atomic_read(&s->q_full.depth) < 2) |
| 661 | return; |
| 662 | |
| 663 | /* |
| 664 | * Take the oldest IDX MDL still holding data, and discard its index |
| 665 | * entries by scheduling the MDL to go back to the firmware |
| 666 | */ |
| 667 | mdl = cx18_dequeue(s, &s->q_full); |
| 668 | if (mdl != NULL) |
| 669 | cx18_enqueue(s, mdl, &s->q_free); |
| 670 | } |
| 671 | |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 672 | static |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 673 | struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s, |
| 674 | struct cx18_mdl *mdl) |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 675 | { |
| 676 | struct cx18 *cx = s->cx; |
| 677 | struct cx18_queue *q; |
| 678 | |
| 679 | /* Don't give it to the firmware, if we're not running a capture */ |
| 680 | if (s->handle == CX18_INVALID_TASK_HANDLE || |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 681 | test_bit(CX18_F_S_STOPPING, &s->s_flags) || |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 682 | !test_bit(CX18_F_S_STREAMING, &s->s_flags)) |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 683 | return cx18_enqueue(s, mdl, &s->q_free); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 684 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 685 | q = cx18_enqueue(s, mdl, &s->q_busy); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 686 | if (q != &s->q_busy) |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 687 | return q; /* The firmware has the max MDLs it can handle */ |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 688 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 689 | cx18_mdl_sync_for_device(s, mdl); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 690 | cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle, |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 691 | (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem, |
| 692 | s->bufs_per_mdl, mdl->id, s->mdl_size); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 693 | return q; |
| 694 | } |
| 695 | |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 696 | static |
| 697 | void _cx18_stream_load_fw_queue(struct cx18_stream *s) |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 698 | { |
Andy Walls | abb096d | 2008-12-12 15:50:27 -0300 | [diff] [blame] | 699 | struct cx18_queue *q; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 700 | struct cx18_mdl *mdl; |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 701 | |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 702 | if (atomic_read(&s->q_free.depth) == 0 || |
| 703 | atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM) |
Andy Walls | abb096d | 2008-12-12 15:50:27 -0300 | [diff] [blame] | 704 | return; |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 705 | |
Andy Walls | abb096d | 2008-12-12 15:50:27 -0300 | [diff] [blame] | 706 | /* Move from q_free to q_busy notifying the firmware, until the limit */ |
| 707 | do { |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 708 | mdl = cx18_dequeue(s, &s->q_free); |
| 709 | if (mdl == NULL) |
Andy Walls | abb096d | 2008-12-12 15:50:27 -0300 | [diff] [blame] | 710 | break; |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 711 | q = _cx18_stream_put_mdl_fw(s, mdl); |
Andy Walls | c37b11b | 2009-11-04 23:13:58 -0300 | [diff] [blame] | 712 | } while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM |
Andy Walls | 0ef0289 | 2008-12-14 18:52:12 -0300 | [diff] [blame] | 713 | && q == &s->q_busy); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 714 | } |
| 715 | |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 716 | void cx18_out_work_handler(struct work_struct *work) |
| 717 | { |
Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 718 | struct cx18_stream *s = |
| 719 | container_of(work, struct cx18_stream, out_work_order); |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 720 | |
Andy Walls | 21a278b | 2009-04-15 20:45:10 -0300 | [diff] [blame] | 721 | _cx18_stream_load_fw_queue(s); |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 722 | } |
| 723 | |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 724 | static void cx18_stream_configure_mdls(struct cx18_stream *s) |
| 725 | { |
| 726 | cx18_unload_queues(s); |
| 727 | |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 728 | switch (s->type) { |
| 729 | case CX18_ENC_STREAM_TYPE_YUV: |
| 730 | /* |
| 731 | * Height should be a multiple of 32 lines. |
| 732 | * Set the MDL size to the exact size needed for one frame. |
| 733 | * Use enough buffers per MDL to cover the MDL size |
| 734 | */ |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 735 | if (s->pixelformat == V4L2_PIX_FMT_HM12) |
| 736 | s->mdl_size = 720 * s->cx->cxhdl.height * 3 / 2; |
| 737 | else |
| 738 | s->mdl_size = 720 * s->cx->cxhdl.height * 2; |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 739 | s->bufs_per_mdl = s->mdl_size / s->buf_size; |
| 740 | if (s->mdl_size % s->buf_size) |
| 741 | s->bufs_per_mdl++; |
| 742 | break; |
Andy Walls | 127ce5f | 2009-11-11 00:22:57 -0300 | [diff] [blame] | 743 | case CX18_ENC_STREAM_TYPE_VBI: |
| 744 | s->bufs_per_mdl = 1; |
| 745 | if (cx18_raw_vbi(s->cx)) { |
| 746 | s->mdl_size = (s->cx->is_60hz ? 12 : 18) |
Mauro Carvalho Chehab | 318de79 | 2016-06-24 08:40:51 -0300 | [diff] [blame] | 747 | * 2 * VBI_ACTIVE_SAMPLES; |
Andy Walls | 127ce5f | 2009-11-11 00:22:57 -0300 | [diff] [blame] | 748 | } else { |
| 749 | /* |
| 750 | * See comment in cx18_vbi_setup() below about the |
| 751 | * extra lines we capture in sliced VBI mode due to |
| 752 | * the lines on which EAV RP codes toggle. |
| 753 | */ |
| 754 | s->mdl_size = s->cx->is_60hz |
Mauro Carvalho Chehab | 318de79 | 2016-06-24 08:40:51 -0300 | [diff] [blame] | 755 | ? (21 - 4 + 1) * 2 * VBI_HBLANK_SAMPLES_60HZ |
| 756 | : (23 - 2 + 1) * 2 * VBI_HBLANK_SAMPLES_50HZ; |
Andy Walls | 127ce5f | 2009-11-11 00:22:57 -0300 | [diff] [blame] | 757 | } |
| 758 | break; |
Andy Walls | 22dce18 | 2009-11-09 23:55:30 -0300 | [diff] [blame] | 759 | default: |
| 760 | s->bufs_per_mdl = 1; |
| 761 | s->mdl_size = s->buf_size * s->bufs_per_mdl; |
| 762 | break; |
| 763 | } |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 764 | |
| 765 | cx18_load_queues(s); |
| 766 | } |
| 767 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 768 | int cx18_start_v4l2_encode_stream(struct cx18_stream *s) |
| 769 | { |
| 770 | u32 data[MAX_MB_ARGUMENTS]; |
| 771 | struct cx18 *cx = s->cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 772 | int captype = 0; |
Andy Walls | e46c54a | 2009-12-31 02:14:51 -0300 | [diff] [blame] | 773 | struct cx18_stream *s_idx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 774 | |
Andy Walls | 540bab9 | 2009-12-31 00:26:49 -0300 | [diff] [blame] | 775 | if (!cx18_stream_enabled(s)) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 776 | return -EINVAL; |
| 777 | |
| 778 | CX18_DEBUG_INFO("Start encoder stream %s\n", s->name); |
| 779 | |
| 780 | switch (s->type) { |
| 781 | case CX18_ENC_STREAM_TYPE_MPG: |
| 782 | captype = CAPTURE_CHANNEL_TYPE_MPEG; |
| 783 | cx->mpg_data_received = cx->vbi_data_inserted = 0; |
| 784 | cx->dualwatch_jiffies = jiffies; |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 785 | cx->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 786 | cx->search_pack_header = 0; |
| 787 | break; |
| 788 | |
Andy Walls | e46c54a | 2009-12-31 02:14:51 -0300 | [diff] [blame] | 789 | case CX18_ENC_STREAM_TYPE_IDX: |
| 790 | captype = CAPTURE_CHANNEL_TYPE_INDEX; |
| 791 | break; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 792 | case CX18_ENC_STREAM_TYPE_TS: |
| 793 | captype = CAPTURE_CHANNEL_TYPE_TS; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 794 | break; |
| 795 | case CX18_ENC_STREAM_TYPE_YUV: |
| 796 | captype = CAPTURE_CHANNEL_TYPE_YUV; |
| 797 | break; |
| 798 | case CX18_ENC_STREAM_TYPE_PCM: |
| 799 | captype = CAPTURE_CHANNEL_TYPE_PCM; |
| 800 | break; |
| 801 | case CX18_ENC_STREAM_TYPE_VBI: |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 802 | #ifdef CX18_ENCODER_PARSES_SLICED |
Andy Walls | dd07343 | 2008-12-12 16:24:04 -0300 | [diff] [blame] | 803 | captype = cx18_raw_vbi(cx) ? |
| 804 | CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI; |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 805 | #else |
| 806 | /* |
| 807 | * Currently we set things up so that Sliced VBI from the |
| 808 | * digitizer is handled as Raw VBI by the encoder |
| 809 | */ |
| 810 | captype = CAPTURE_CHANNEL_TYPE_VBI; |
| 811 | #endif |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 812 | cx->vbi.frame = 0; |
| 813 | cx->vbi.inserted_frame = 0; |
| 814 | memset(cx->vbi.sliced_mpeg_size, |
| 815 | 0, sizeof(cx->vbi.sliced_mpeg_size)); |
| 816 | break; |
| 817 | default: |
| 818 | return -EINVAL; |
| 819 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 820 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 821 | /* Clear Streamoff flags in case left from last capture */ |
| 822 | clear_bit(CX18_F_S_STREAMOFF, &s->s_flags); |
| 823 | |
| 824 | cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE); |
| 825 | s->handle = data[0]; |
| 826 | cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype); |
| 827 | |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 828 | /* |
| 829 | * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and |
| 830 | * set up all the parameters, as it is not obvious which parameters the |
| 831 | * firmware shares across capture channel types and which it does not. |
| 832 | * |
| 833 | * Some of the cx18_vapi() calls below apply to only certain capture |
| 834 | * channel types. We're hoping there's no harm in calling most of them |
| 835 | * anyway, as long as the values are all consistent. Setting some |
| 836 | * shared parameters will have no effect once an analog capture channel |
| 837 | * has started streaming. |
| 838 | */ |
| 839 | if (captype != CAPTURE_CHANNEL_TYPE_TS) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 840 | cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0); |
| 841 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1); |
| 842 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0); |
| 843 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 844 | |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 845 | /* |
| 846 | * Audio related reset according to |
Mauro Carvalho Chehab | 2ebe0bb | 2018-05-08 19:41:44 -0300 | [diff] [blame] | 847 | * Documentation/media/v4l-drivers/cx2341x.rst |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 848 | */ |
| 849 | if (atomic_read(&cx->ana_capturing) == 0) |
| 850 | cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, |
| 851 | s->handle, 12); |
| 852 | |
| 853 | /* |
| 854 | * Number of lines for Field 1 & Field 2 according to |
Mauro Carvalho Chehab | 2ebe0bb | 2018-05-08 19:41:44 -0300 | [diff] [blame] | 855 | * Documentation/media/v4l-drivers/cx2341x.rst |
Andy Walls | f37aa51 | 2009-02-07 01:15:44 -0300 | [diff] [blame] | 856 | * Field 1 is 312 for 625 line systems in BT.656 |
| 857 | * Field 2 is 313 for 625 line systems in BT.656 |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 858 | */ |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 859 | cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3, |
Andy Walls | f37aa51 | 2009-02-07 01:15:44 -0300 | [diff] [blame] | 860 | s->handle, 312, 313); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 861 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 862 | if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE) |
| 863 | cx18_vbi_setup(s); |
| 864 | |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 865 | /* |
Andy Walls | e46c54a | 2009-12-31 02:14:51 -0300 | [diff] [blame] | 866 | * Select to receive I, P, and B frame index entries, if the |
| 867 | * index stream is enabled. Otherwise disable index entry |
| 868 | * generation. |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 869 | */ |
Andy Walls | e46c54a | 2009-12-31 02:14:51 -0300 | [diff] [blame] | 870 | s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX]; |
Andy Walls | 5ada577 | 2010-01-01 13:25:41 -0300 | [diff] [blame] | 871 | cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 2, |
| 872 | s->handle, cx18_stream_enabled(s_idx) ? 7 : 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 873 | |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 874 | /* Call out to the common CX2341x API setup for user controls */ |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 875 | cx->cxhdl.priv = s; |
| 876 | cx2341x_handler_setup(&cx->cxhdl); |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 877 | |
| 878 | /* |
| 879 | * When starting a capture and we're set for radio, |
| 880 | * ensure the video is muted, despite the user control. |
| 881 | */ |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 882 | if (!cx->cxhdl.video_mute && |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 883 | test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) |
| 884 | cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle, |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 885 | (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8) | 1); |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 886 | |
| 887 | /* Enable the Video Format Converter for UYVY 4:2:2 support, |
| 888 | * rather than the default HM12 Macroblovk 4:2:0 support. |
| 889 | */ |
| 890 | if (captype == CAPTURE_CHANNEL_TYPE_YUV) { |
Simon Farnsworth | 1bf5842 | 2011-05-03 08:57:40 -0300 | [diff] [blame] | 891 | if (s->pixelformat == V4L2_PIX_FMT_UYVY) |
Steven Toth | b7101de | 2011-04-06 08:32:56 -0300 | [diff] [blame] | 892 | cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2, |
| 893 | s->handle, 1); |
| 894 | else |
| 895 | /* If in doubt, default to HM12 */ |
| 896 | cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2, |
| 897 | s->handle, 0); |
| 898 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 899 | } |
| 900 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 901 | if (atomic_read(&cx->tot_capturing) == 0) { |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 902 | cx2341x_handler_set_busy(&cx->cxhdl, 1); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 903 | clear_bit(CX18_F_I_EOS, &cx->i_flags); |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 904 | cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle, |
Al Viro | 990c81c | 2008-05-21 00:32:01 -0300 | [diff] [blame] | 908 | (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem, |
| 909 | (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 910 | |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 911 | /* Init all the cpu_mdls for this stream */ |
Andy Walls | 52fcb3e | 2009-11-08 23:45:24 -0300 | [diff] [blame] | 912 | cx18_stream_configure_mdls(s); |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 913 | _cx18_stream_load_fw_queue(s); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 914 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 915 | /* begin_capture */ |
| 916 | if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) { |
| 917 | CX18_DEBUG_WARN("Error starting capture!\n"); |
Andy Walls | 3b5df8e | 2008-08-23 18:36:50 -0300 | [diff] [blame] | 918 | /* Ensure we're really not capturing before releasing MDLs */ |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 919 | set_bit(CX18_F_S_STOPPING, &s->s_flags); |
Andy Walls | 3b5df8e | 2008-08-23 18:36:50 -0300 | [diff] [blame] | 920 | if (s->type == CX18_ENC_STREAM_TYPE_MPG) |
| 921 | cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1); |
| 922 | else |
| 923 | cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 924 | clear_bit(CX18_F_S_STREAMING, &s->s_flags); |
| 925 | /* FIXME - CX18_F_S_STREAMOFF as well? */ |
Andy Walls | 3b5df8e | 2008-08-23 18:36:50 -0300 | [diff] [blame] | 926 | cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 927 | cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 928 | s->handle = CX18_INVALID_TASK_HANDLE; |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 929 | clear_bit(CX18_F_S_STOPPING, &s->s_flags); |
Andy Walls | 66c2a6b | 2008-12-08 23:02:45 -0300 | [diff] [blame] | 930 | if (atomic_read(&cx->tot_capturing) == 0) { |
| 931 | set_bit(CX18_F_I_EOS, &cx->i_flags); |
| 932 | cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK); |
| 933 | } |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 934 | return -EINVAL; |
| 935 | } |
| 936 | |
| 937 | /* you're live! sit back and await interrupts :) */ |
Andy Walls | dcc0ef8 | 2009-02-06 18:33:43 -0300 | [diff] [blame] | 938 | if (captype != CAPTURE_CHANNEL_TYPE_TS) |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 939 | atomic_inc(&cx->ana_capturing); |
| 940 | atomic_inc(&cx->tot_capturing); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 941 | return 0; |
| 942 | } |
Devin Heitmueller | 0f4cf67 | 2009-11-19 23:23:57 -0300 | [diff] [blame] | 943 | EXPORT_SYMBOL(cx18_start_v4l2_encode_stream); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 944 | |
| 945 | void cx18_stop_all_captures(struct cx18 *cx) |
| 946 | { |
| 947 | int i; |
| 948 | |
| 949 | for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) { |
| 950 | struct cx18_stream *s = &cx->streams[i]; |
| 951 | |
Andy Walls | 540bab9 | 2009-12-31 00:26:49 -0300 | [diff] [blame] | 952 | if (!cx18_stream_enabled(s)) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 953 | continue; |
| 954 | if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) |
| 955 | cx18_stop_v4l2_encode_stream(s, 0); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end) |
| 960 | { |
| 961 | struct cx18 *cx = s->cx; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 962 | |
Andy Walls | 540bab9 | 2009-12-31 00:26:49 -0300 | [diff] [blame] | 963 | if (!cx18_stream_enabled(s)) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 964 | return -EINVAL; |
| 965 | |
| 966 | /* This function assumes that you are allowed to stop the capture |
| 967 | and that we are actually capturing */ |
| 968 | |
| 969 | CX18_DEBUG_INFO("Stop Capture\n"); |
| 970 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 971 | if (atomic_read(&cx->tot_capturing) == 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 972 | return 0; |
| 973 | |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 974 | set_bit(CX18_F_S_STOPPING, &s->s_flags); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 975 | if (s->type == CX18_ENC_STREAM_TYPE_MPG) |
| 976 | cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end); |
| 977 | else |
| 978 | cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle); |
| 979 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 980 | if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) { |
| 981 | CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n"); |
| 982 | } |
| 983 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 984 | if (s->type != CX18_ENC_STREAM_TYPE_TS) |
| 985 | atomic_dec(&cx->ana_capturing); |
| 986 | atomic_dec(&cx->tot_capturing); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 987 | |
| 988 | /* Clear capture and no-read bits */ |
| 989 | clear_bit(CX18_F_S_STREAMING, &s->s_flags); |
| 990 | |
Andy Walls | f68d0cf | 2008-11-05 21:19:15 -0300 | [diff] [blame] | 991 | /* Tell the CX23418 it can't use our buffers anymore */ |
| 992 | cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle); |
| 993 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 994 | cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle); |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame] | 995 | s->handle = CX18_INVALID_TASK_HANDLE; |
Andy Walls | 8711615 | 2009-04-13 22:42:43 -0300 | [diff] [blame] | 996 | clear_bit(CX18_F_S_STOPPING, &s->s_flags); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 997 | |
Hans Verkuil | 31554ae | 2008-05-25 11:21:27 -0300 | [diff] [blame] | 998 | if (atomic_read(&cx->tot_capturing) > 0) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 999 | return 0; |
| 1000 | |
Hans Verkuil | a75b9be | 2010-12-31 10:22:52 -0300 | [diff] [blame] | 1001 | cx2341x_handler_set_busy(&cx->cxhdl, 0); |
Andy Walls | b152642 | 2008-08-30 16:03:44 -0300 | [diff] [blame] | 1002 | cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1003 | wake_up(&s->waitq); |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
Devin Heitmueller | 0f4cf67 | 2009-11-19 23:23:57 -0300 | [diff] [blame] | 1007 | EXPORT_SYMBOL(cx18_stop_v4l2_encode_stream); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1008 | |
| 1009 | u32 cx18_find_handle(struct cx18 *cx) |
| 1010 | { |
| 1011 | int i; |
| 1012 | |
| 1013 | /* find first available handle to be used for global settings */ |
| 1014 | for (i = 0; i < CX18_MAX_STREAMS; i++) { |
| 1015 | struct cx18_stream *s = &cx->streams[i]; |
| 1016 | |
Hans Verkuil | 08569d6 | 2015-03-09 13:34:03 -0300 | [diff] [blame] | 1017 | if (s->video_dev.v4l2_dev && (s->handle != CX18_INVALID_TASK_HANDLE)) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1018 | return s->handle; |
| 1019 | } |
Andy Walls | d3c5e70 | 2008-08-23 16:42:29 -0300 | [diff] [blame] | 1020 | return CX18_INVALID_TASK_HANDLE; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 1021 | } |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 1022 | |
| 1023 | struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle) |
| 1024 | { |
| 1025 | int i; |
| 1026 | struct cx18_stream *s; |
| 1027 | |
| 1028 | if (handle == CX18_INVALID_TASK_HANDLE) |
| 1029 | return NULL; |
| 1030 | |
| 1031 | for (i = 0; i < CX18_MAX_STREAMS; i++) { |
| 1032 | s = &cx->streams[i]; |
| 1033 | if (s->handle != handle) |
| 1034 | continue; |
Andy Walls | 540bab9 | 2009-12-31 00:26:49 -0300 | [diff] [blame] | 1035 | if (cx18_stream_enabled(s)) |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 1036 | return s; |
| 1037 | } |
| 1038 | return NULL; |
| 1039 | } |