blob: b36f4ce25d22dbe2bde1ada588e5b9a8c517d6bb [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 init/start/stop/exit stream functions
3 *
4 * Derived from ivtv-streams.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
Andy Walls6afdeaf2010-05-23 18:53:35 -03007 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030018 */
19
20#include "cx18-driver.h"
Andy Wallsb1526422008-08-30 16:03:44 -030021#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030022#include "cx18-fileops.h"
23#include "cx18-mailbox.h"
24#include "cx18-i2c.h"
25#include "cx18-queue.h"
26#include "cx18-ioctl.h"
27#include "cx18-streams.h"
28#include "cx18-cards.h"
29#include "cx18-scb.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030030#include "cx18-dvb.h"
31
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -050032#define CX18_DSP0_INTERRUPT_MASK 0xd0004C
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030033
Bhumika Goyale66fb182017-08-26 02:02:08 -040034static const struct v4l2_file_operations cx18_v4l2_enc_fops = {
Hans Verkuildaf20d92008-05-12 11:21:58 -030035 .owner = THIS_MODULE,
36 .read = cx18_v4l2_read,
37 .open = cx18_v4l2_open,
Hans Verkuildcaded72012-06-09 10:54:19 -030038 .unlocked_ioctl = video_ioctl2,
Hans Verkuildaf20d92008-05-12 11:21:58 -030039 .release = cx18_v4l2_close,
40 .poll = cx18_v4l2_enc_poll,
Steven Tothb7101de2011-04-06 08:32:56 -030041 .mmap = cx18_v4l2_mmap,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030042};
43
44/* offset from 0 to register ts v4l2 minors on */
45#define CX18_V4L2_ENC_TS_OFFSET 16
46/* offset from 0 to register pcm v4l2 minors on */
47#define CX18_V4L2_ENC_PCM_OFFSET 24
48/* offset from 0 to register yuv v4l2 minors on */
49#define CX18_V4L2_ENC_YUV_OFFSET 32
50
51static struct {
52 const char *name;
53 int vfl_type;
Hans Verkuildd896012008-10-04 08:36:54 -030054 int num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030055 int dma;
Hans Verkuildfdf7802014-11-24 06:37:21 -030056 u32 caps;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030057} cx18_stream_info[] = {
58 { /* CX18_ENC_STREAM_TYPE_MPG */
59 "encoder MPEG",
60 VFL_TYPE_GRABBER, 0,
Hans Verkuilff82b2112012-09-17 05:02:38 -030061 PCI_DMA_FROMDEVICE,
Hans Verkuildfdf7802014-11-24 06:37:21 -030062 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
63 V4L2_CAP_AUDIO | V4L2_CAP_TUNER
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030064 },
65 { /* CX18_ENC_STREAM_TYPE_TS */
66 "TS",
67 VFL_TYPE_GRABBER, -1,
Hans Verkuilff82b2112012-09-17 05:02:38 -030068 PCI_DMA_FROMDEVICE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030069 },
70 { /* CX18_ENC_STREAM_TYPE_YUV */
71 "encoder YUV",
72 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
Hans Verkuilff82b2112012-09-17 05:02:38 -030073 PCI_DMA_FROMDEVICE,
Hans Verkuildfdf7802014-11-24 06:37:21 -030074 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
75 V4L2_CAP_STREAMING | V4L2_CAP_AUDIO | V4L2_CAP_TUNER
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030076 },
77 { /* CX18_ENC_STREAM_TYPE_VBI */
78 "encoder VBI",
79 VFL_TYPE_VBI, 0,
Hans Verkuilff82b2112012-09-17 05:02:38 -030080 PCI_DMA_FROMDEVICE,
Hans Verkuildfdf7802014-11-24 06:37:21 -030081 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE |
82 V4L2_CAP_READWRITE | V4L2_CAP_TUNER
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030083 },
84 { /* CX18_ENC_STREAM_TYPE_PCM */
85 "encoder PCM audio",
86 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
Hans Verkuilff82b2112012-09-17 05:02:38 -030087 PCI_DMA_FROMDEVICE,
Hans Verkuil2b4fd3e2015-04-24 03:55:07 -030088 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030089 },
90 { /* CX18_ENC_STREAM_TYPE_IDX */
91 "encoder IDX",
92 VFL_TYPE_GRABBER, -1,
Hans Verkuilff82b2112012-09-17 05:02:38 -030093 PCI_DMA_FROMDEVICE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030094 },
95 { /* CX18_ENC_STREAM_TYPE_RAD */
96 "encoder radio",
97 VFL_TYPE_RADIO, 0,
Hans Verkuilff82b2112012-09-17 05:02:38 -030098 PCI_DMA_NONE,
Hans Verkuildfdf7802014-11-24 06:37:21 -030099 V4L2_CAP_RADIO | V4L2_CAP_TUNER
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300100 },
101};
102
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300103
Mauro Carvalho Chehab5e6e81b2012-10-27 11:28:50 -0300104static void cx18_dma_free(struct videobuf_queue *q,
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300105 struct cx18_stream *s, struct cx18_videobuf_buffer *buf)
106{
107 videobuf_waiton(q, &buf->vb, 0, 0);
108 videobuf_vmalloc_free(&buf->vb);
109 buf->vb.state = VIDEOBUF_NEEDS_INIT;
110}
111
112static int cx18_prepare_buffer(struct videobuf_queue *q,
113 struct cx18_stream *s,
114 struct cx18_videobuf_buffer *buf,
115 u32 pixelformat,
116 unsigned int width, unsigned int height,
117 enum v4l2_field field)
118{
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500119 struct cx18 *cx = s->cx;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300120 int rc = 0;
121
122 /* check settings */
123 buf->bytes_used = 0;
124
125 if ((width < 48) || (height < 32))
126 return -EINVAL;
127
128 buf->vb.size = (width * height * 2);
129 if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
130 return -EINVAL;
131
132 /* alloc + fill struct (if changed) */
133 if (buf->vb.width != width || buf->vb.height != height ||
134 buf->vb.field != field || s->pixelformat != pixelformat ||
135 buf->tvnorm != cx->std) {
136
137 buf->vb.width = width;
138 buf->vb.height = height;
139 buf->vb.field = field;
140 buf->tvnorm = cx->std;
141 s->pixelformat = pixelformat;
142
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300143 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
144 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
145 if (s->pixelformat == V4L2_PIX_FMT_HM12)
146 s->vb_bytes_per_frame = height * 720 * 3 / 2;
147 else
148 s->vb_bytes_per_frame = height * 720 * 2;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300149 cx18_dma_free(q, s, buf);
150 }
151
152 if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
153 return -EINVAL;
154
155 if (buf->vb.field == 0)
156 buf->vb.field = V4L2_FIELD_INTERLACED;
157
158 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
159 buf->vb.width = width;
160 buf->vb.height = height;
161 buf->vb.field = field;
162 buf->tvnorm = cx->std;
163 s->pixelformat = pixelformat;
164
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300165 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
166 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
167 if (s->pixelformat == V4L2_PIX_FMT_HM12)
168 s->vb_bytes_per_frame = height * 720 * 3 / 2;
169 else
170 s->vb_bytes_per_frame = height * 720 * 2;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300171 rc = videobuf_iolock(q, &buf->vb, NULL);
172 if (rc != 0)
173 goto fail;
174 }
175 buf->vb.state = VIDEOBUF_PREPARED;
176 return 0;
177
178fail:
179 cx18_dma_free(q, s, buf);
180 return rc;
181
182}
183
184/* VB_MIN_BUFSIZE is lcm(1440 * 480, 1440 * 576)
185 1440 is a single line of 4:2:2 YUV at 720 luma samples wide
186*/
187#define VB_MIN_BUFFERS 32
188#define VB_MIN_BUFSIZE 4147200
189
190static int buffer_setup(struct videobuf_queue *q,
191 unsigned int *count, unsigned int *size)
192{
193 struct cx18_stream *s = q->priv_data;
194 struct cx18 *cx = s->cx;
195
196 *size = 2 * cx->cxhdl.width * cx->cxhdl.height;
197 if (*count == 0)
198 *count = VB_MIN_BUFFERS;
199
200 while (*size * *count > VB_MIN_BUFFERS * VB_MIN_BUFSIZE)
201 (*count)--;
202
203 q->field = V4L2_FIELD_INTERLACED;
204 q->last = V4L2_FIELD_INTERLACED;
205
206 return 0;
207}
208
209static int buffer_prepare(struct videobuf_queue *q,
210 struct videobuf_buffer *vb,
211 enum v4l2_field field)
212{
213 struct cx18_videobuf_buffer *buf =
214 container_of(vb, struct cx18_videobuf_buffer, vb);
215 struct cx18_stream *s = q->priv_data;
216 struct cx18 *cx = s->cx;
217
218 return cx18_prepare_buffer(q, s, buf, s->pixelformat,
219 cx->cxhdl.width, cx->cxhdl.height, field);
220}
221
222static void buffer_release(struct videobuf_queue *q,
223 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 cx18_dma_free(q, s, buf);
230}
231
232static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
233{
234 struct cx18_videobuf_buffer *buf =
235 container_of(vb, struct cx18_videobuf_buffer, vb);
236 struct cx18_stream *s = q->priv_data;
237
238 buf->vb.state = VIDEOBUF_QUEUED;
239
240 list_add_tail(&buf->vb.queue, &s->vb_capture);
241}
242
Julia Lawall66883242017-08-04 08:09:44 -0400243static const struct videobuf_queue_ops cx18_videobuf_qops = {
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300244 .buf_setup = buffer_setup,
245 .buf_prepare = buffer_prepare,
246 .buf_queue = buffer_queue,
247 .buf_release = buffer_release,
248};
249
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300250static void cx18_stream_init(struct cx18 *cx, int type)
251{
252 struct cx18_stream *s = &cx->streams[type];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300253
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300254 memset(s, 0, sizeof(*s));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300255
256 /* initialize cx18_stream fields */
Andy Walls754f9962010-12-11 20:38:20 -0300257 s->dvb = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300258 s->cx = cx;
259 s->type = type;
260 s->name = cx18_stream_info[type].name;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300261 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300262
263 s->dma = cx18_stream_info[type].dma;
Hans Verkuildfdf7802014-11-24 06:37:21 -0300264 s->v4l2_dev_caps = cx18_stream_info[type].caps;
Andy Walls6ecd86dc2008-12-07 23:30:17 -0300265 s->buffers = cx->stream_buffers[type];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300266 s->buf_size = cx->stream_buf_size[type];
Andy Walls52fcb3e2009-11-08 23:45:24 -0300267 INIT_LIST_HEAD(&s->buf_pool);
268 s->bufs_per_mdl = 1;
269 s->mdl_size = s->buf_size * s->bufs_per_mdl;
Andy Walls6ecd86dc2008-12-07 23:30:17 -0300270
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300271 init_waitqueue_head(&s->waitq);
272 s->id = -1;
Andy Walls40c55202009-04-13 23:08:00 -0300273 spin_lock_init(&s->q_free.lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300274 cx18_queue_init(&s->q_free);
Andy Walls40c55202009-04-13 23:08:00 -0300275 spin_lock_init(&s->q_busy.lock);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300276 cx18_queue_init(&s->q_busy);
Andy Walls40c55202009-04-13 23:08:00 -0300277 spin_lock_init(&s->q_full.lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300278 cx18_queue_init(&s->q_full);
Andy Walls52fcb3e2009-11-08 23:45:24 -0300279 spin_lock_init(&s->q_idle.lock);
280 cx18_queue_init(&s->q_idle);
Andy Walls21a278b2009-04-15 20:45:10 -0300281
282 INIT_WORK(&s->out_work_order, cx18_out_work_handler);
Steven Tothb7101de2011-04-06 08:32:56 -0300283
284 INIT_LIST_HEAD(&s->vb_capture);
Kees Cook162e6372017-10-24 11:22:42 -0400285 timer_setup(&s->vb_timeout, cx18_vb_timeout, 0);
Steven Tothb7101de2011-04-06 08:32:56 -0300286 spin_lock_init(&s->vb_lock);
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300287 if (type == CX18_ENC_STREAM_TYPE_YUV) {
Simon Farnsworth612031c2011-05-10 10:49:50 -0300288 spin_lock_init(&s->vbuf_q_lock);
289
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300290 s->vb_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
291 videobuf_queue_vmalloc_init(&s->vbuf_q, &cx18_videobuf_qops,
292 &cx->pci_dev->dev, &s->vbuf_q_lock,
293 V4L2_BUF_TYPE_VIDEO_CAPTURE,
294 V4L2_FIELD_INTERLACED,
295 sizeof(struct cx18_videobuf_buffer),
296 s, &cx->serialize_lock);
Steven Tothb7101de2011-04-06 08:32:56 -0300297
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300298 /* Assume the previous pixel default */
299 s->pixelformat = V4L2_PIX_FMT_HM12;
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300300 s->vb_bytes_per_frame = cx->cxhdl.height * 720 * 3 / 2;
Hans Verkuil3a29a4f2015-04-02 08:34:30 -0300301 s->vb_bytes_per_line = 720;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300302 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300303}
304
305static int cx18_prep_dev(struct cx18 *cx, int type)
306{
307 struct cx18_stream *s = &cx->streams[type];
308 u32 cap = cx->v4l2_cap;
Hans Verkuildd896012008-10-04 08:36:54 -0300309 int num_offset = cx18_stream_info[type].num_offset;
Andy Walls5811cf92009-02-14 17:08:37 -0300310 int num = cx->instance + cx18_first_minor + num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300311
Andy Walls754f9962010-12-11 20:38:20 -0300312 /*
313 * These five fields are always initialized.
Hans Verkuil08569d62015-03-09 13:34:03 -0300314 * For analog capture related streams, if video_dev.v4l2_dev == NULL then the
Andy Walls754f9962010-12-11 20:38:20 -0300315 * stream is not in use.
316 * For the TS stream, if dvb == NULL then the stream is not in use.
317 * In those cases no other fields but these four can be used.
318 */
Hans Verkuil08569d62015-03-09 13:34:03 -0300319 s->video_dev.v4l2_dev = NULL;
Andy Walls754f9962010-12-11 20:38:20 -0300320 s->dvb = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300321 s->cx = cx;
322 s->type = type;
323 s->name = cx18_stream_info[type].name;
324
325 /* Check whether the radio is supported */
326 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
327 return 0;
328
329 /* Check whether VBI is supported */
330 if (type == CX18_ENC_STREAM_TYPE_VBI &&
331 !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
332 return 0;
333
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300334 /* User explicitly selected 0 buffers for these streams, so don't
335 create them. */
336 if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
Andy Walls6ecd86dc2008-12-07 23:30:17 -0300337 cx->stream_buffers[type] == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300338 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
339 return 0;
340 }
341
342 cx18_stream_init(cx, type);
343
Andy Walls754f9962010-12-11 20:38:20 -0300344 /* Allocate the cx18_dvb struct only for the TS on cards with DTV */
345 if (type == CX18_ENC_STREAM_TYPE_TS) {
346 if (cx->card->hw_all & CX18_HW_DVB) {
347 s->dvb = kzalloc(sizeof(struct cx18_dvb), GFP_KERNEL);
348 if (s->dvb == NULL) {
Mauro Carvalho Chehab6beb1382016-10-18 17:44:03 -0200349 CX18_ERR("Couldn't allocate cx18_dvb structure for %s\n",
350 s->name);
Andy Walls754f9962010-12-11 20:38:20 -0300351 return -ENOMEM;
352 }
353 } else {
354 /* Don't need buffers for the TS, if there is no DVB */
355 s->buffers = 0;
356 }
357 }
358
Hans Verkuildd896012008-10-04 08:36:54 -0300359 if (num_offset == -1)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300360 return 0;
361
Hans Verkuil08569d62015-03-09 13:34:03 -0300362 /* initialize the v4l2 video device structure */
363 snprintf(s->video_dev.name, sizeof(s->video_dev.name), "%s %s",
Andy Walls5811cf92009-02-14 17:08:37 -0300364 cx->v4l2_dev.name, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300365
Hans Verkuil08569d62015-03-09 13:34:03 -0300366 s->video_dev.num = num;
367 s->video_dev.v4l2_dev = &cx->v4l2_dev;
368 s->video_dev.fops = &cx18_v4l2_enc_fops;
369 s->video_dev.release = video_device_release_empty;
Hans Verkuil3a29a4f2015-04-02 08:34:30 -0300370 if (cx->card->video_inputs->video_type == CX18_CARD_INPUT_VID_TUNER)
371 s->video_dev.tvnorms = cx->tuner_std;
372 else
373 s->video_dev.tvnorms = V4L2_STD_ALL;
Hans Verkuil08569d62015-03-09 13:34:03 -0300374 s->video_dev.lock = &cx->serialize_lock;
375 cx18_set_funcs(&s->video_dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300376 return 0;
377}
378
379/* Initialize v4l2 variables and register v4l2 devices */
380int cx18_streams_setup(struct cx18 *cx)
381{
Andy Walls9b4a7c82008-10-18 10:20:25 -0300382 int type, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300383
384 /* Setup V4L2 Devices */
385 for (type = 0; type < CX18_MAX_STREAMS; type++) {
386 /* Prepare device */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300387 ret = cx18_prep_dev(cx, type);
388 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300389 break;
390
391 /* Allocate Stream */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300392 ret = cx18_stream_alloc(&cx->streams[type]);
393 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300394 break;
395 }
396 if (type == CX18_MAX_STREAMS)
397 return 0;
398
399 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300400 cx18_streams_cleanup(cx, 0);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300401 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300402}
403
404static int cx18_reg_dev(struct cx18 *cx, int type)
405{
406 struct cx18_stream *s = &cx->streams[type];
407 int vfl_type = cx18_stream_info[type].vfl_type;
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300408 const char *name;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300409 int num, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300410
Andy Walls754f9962010-12-11 20:38:20 -0300411 if (type == CX18_ENC_STREAM_TYPE_TS && s->dvb != NULL) {
Andy Walls9b4a7c82008-10-18 10:20:25 -0300412 ret = cx18_dvb_register(s);
413 if (ret < 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300414 CX18_ERR("DVB failed to register\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300415 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300416 }
417 }
418
Hans Verkuil08569d62015-03-09 13:34:03 -0300419 if (s->video_dev.v4l2_dev == NULL)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300420 return 0;
421
Hans Verkuil08569d62015-03-09 13:34:03 -0300422 num = s->video_dev.num;
Hans Verkuildd896012008-10-04 08:36:54 -0300423 /* card number + user defined offset + device offset */
424 if (type != CX18_ENC_STREAM_TYPE_MPG) {
425 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
426
Hans Verkuil08569d62015-03-09 13:34:03 -0300427 if (s_mpg->video_dev.v4l2_dev)
428 num = s_mpg->video_dev.num
Andy Walls3d05913d2009-01-10 21:54:39 -0300429 + cx18_stream_info[type].num_offset;
Hans Verkuildd896012008-10-04 08:36:54 -0300430 }
Hans Verkuil08569d62015-03-09 13:34:03 -0300431 video_set_drvdata(&s->video_dev, s);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300432
433 /* Register device. First try the desired minor, then any free one. */
Hans Verkuil08569d62015-03-09 13:34:03 -0300434 ret = video_register_device_no_warn(&s->video_dev, vfl_type, num);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300435 if (ret < 0) {
Hans Verkuil581644d2009-06-19 11:54:00 -0300436 CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300437 s->name, num);
Hans Verkuil08569d62015-03-09 13:34:03 -0300438 s->video_dev.v4l2_dev = NULL;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300439 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300440 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300441
Hans Verkuil08569d62015-03-09 13:34:03 -0300442 name = video_device_node_name(&s->video_dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300443
444 switch (vfl_type) {
445 case VFL_TYPE_GRABBER:
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300446 CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n",
447 name, s->name, cx->stream_buffers[type],
Andy Walls22dce182009-11-09 23:55:30 -0300448 cx->stream_buf_size[type] / 1024,
449 (cx->stream_buf_size[type] * 100 / 1024) % 100);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300450 break;
451
452 case VFL_TYPE_RADIO:
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300453 CX18_INFO("Registered device %s for %s\n", name, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300454 break;
455
456 case VFL_TYPE_VBI:
Andy Walls6ecd86dc2008-12-07 23:30:17 -0300457 if (cx->stream_buffers[type])
Mauro Carvalho Chehab6beb1382016-10-18 17:44:03 -0200458 CX18_INFO("Registered device %s for %s (%d x %d bytes)\n",
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300459 name, s->name, cx->stream_buffers[type],
Andy Walls6ecd86dc2008-12-07 23:30:17 -0300460 cx->stream_buf_size[type]);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300461 else
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300462 CX18_INFO("Registered device %s for %s\n",
463 name, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300464 break;
465 }
466
467 return 0;
468}
469
470/* Register v4l2 devices */
471int cx18_streams_register(struct cx18 *cx)
472{
473 int type;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300474 int err;
475 int ret = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300476
477 /* Register V4L2 devices */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300478 for (type = 0; type < CX18_MAX_STREAMS; type++) {
479 err = cx18_reg_dev(cx, type);
480 if (err && ret == 0)
481 ret = err;
482 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300483
Andy Walls9b4a7c82008-10-18 10:20:25 -0300484 if (ret == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300485 return 0;
486
487 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300488 cx18_streams_cleanup(cx, 1);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300489 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300490}
491
492/* Unregister v4l2 devices */
Hans Verkuil3f983872008-05-01 10:31:12 -0300493void cx18_streams_cleanup(struct cx18 *cx, int unregister)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300494{
495 struct video_device *vdev;
496 int type;
497
498 /* Teardown all streams */
499 for (type = 0; type < CX18_MAX_STREAMS; type++) {
Andy Walls7b1dde02009-12-31 01:35:08 -0300500
Andy Walls754f9962010-12-11 20:38:20 -0300501 /* The TS has a cx18_dvb structure, not a video_device */
Andy Walls7b1dde02009-12-31 01:35:08 -0300502 if (type == CX18_ENC_STREAM_TYPE_TS) {
Andy Walls754f9962010-12-11 20:38:20 -0300503 if (cx->streams[type].dvb != NULL) {
504 if (unregister)
505 cx18_dvb_unregister(&cx->streams[type]);
506 kfree(cx->streams[type].dvb);
507 cx->streams[type].dvb = NULL;
Andy Walls7b1dde02009-12-31 01:35:08 -0300508 cx18_stream_free(&cx->streams[type]);
509 }
510 continue;
Hans Verkuilfac36392008-07-18 10:07:10 -0300511 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300512
Andy Walls7b1dde02009-12-31 01:35:08 -0300513 /* No struct video_device, but can have buffers allocated */
514 if (type == CX18_ENC_STREAM_TYPE_IDX) {
Andy Walls0f890ab2011-03-27 20:19:15 -0300515 /* If the module params didn't inhibit IDX ... */
Andy Walls7b1dde02009-12-31 01:35:08 -0300516 if (cx->stream_buffers[type] != 0) {
517 cx->stream_buffers[type] = 0;
Andy Walls0f890ab2011-03-27 20:19:15 -0300518 /*
519 * Before calling cx18_stream_free(),
520 * check if the IDX stream was actually set up.
521 * Needed, since the cx18_probe() error path
522 * exits through here as well as normal clean up
523 */
524 if (cx->streams[type].buffers != 0)
525 cx18_stream_free(&cx->streams[type]);
Andy Walls7b1dde02009-12-31 01:35:08 -0300526 }
527 continue;
528 }
529
530 /* If struct video_device exists, can have buffers allocated */
Hans Verkuil08569d62015-03-09 13:34:03 -0300531 vdev = &cx->streams[type].video_dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300532
Hans Verkuil08569d62015-03-09 13:34:03 -0300533 if (vdev->v4l2_dev == NULL)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300534 continue;
535
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300536 if (type == CX18_ENC_STREAM_TYPE_YUV)
537 videobuf_mmap_free(&cx->streams[type].vbuf_q);
538
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300539 cx18_stream_free(&cx->streams[type]);
540
Hans Verkuil08569d62015-03-09 13:34:03 -0300541 video_unregister_device(vdev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300542 }
543}
544
545static void cx18_vbi_setup(struct cx18_stream *s)
546{
547 struct cx18 *cx = s->cx;
Andy Wallsdd073432008-12-12 16:24:04 -0300548 int raw = cx18_raw_vbi(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300549 u32 data[CX2341X_MBOX_MAX_DATA];
550 int lines;
551
552 if (cx->is_60hz) {
553 cx->vbi.count = 12;
554 cx->vbi.start[0] = 10;
555 cx->vbi.start[1] = 273;
556 } else { /* PAL/SECAM */
557 cx->vbi.count = 18;
558 cx->vbi.start[0] = 6;
559 cx->vbi.start[1] = 318;
560 }
561
562 /* setup VBI registers */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300563 if (raw)
564 v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &cx->vbi.in.fmt.vbi);
565 else
566 v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &cx->vbi.in.fmt.sliced);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300567
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300568 /*
569 * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
570 * VBI when the first analog capture channel starts, as once it starts
571 * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
572 * (i.e. for the VBI capture channels). We also send it for each
573 * analog capture channel anyway just to make sure we get the proper
574 * behavior
575 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300576 if (raw) {
577 lines = cx->vbi.count * 2;
578 } else {
Andy Walls812b1f92009-02-08 22:40:04 -0300579 /*
580 * For 525/60 systems, according to the VIP 2 & BT.656 std:
581 * The EAV RP code's Field bit toggles on line 4, a few lines
582 * after the Vertcal Blank bit has already toggled.
583 * Tell the encoder to capture 21-4+1=18 lines per field,
584 * since we want lines 10 through 21.
585 *
Andy Walls5ab74052009-05-10 22:14:29 -0300586 * For 625/50 systems, according to the VIP 2 & BT.656 std:
587 * The EAV RP code's Field bit toggles on line 1, a few lines
588 * after the Vertcal Blank bit has already toggled.
Andy Walls929a3ad2009-05-16 21:06:57 -0300589 * (We've actually set the digitizer so that the Field bit
590 * toggles on line 2.) Tell the encoder to capture 23-2+1=22
591 * lines per field, since we want lines 6 through 23.
Andy Walls812b1f92009-02-08 22:40:04 -0300592 */
Andy Walls929a3ad2009-05-16 21:06:57 -0300593 lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300594 }
595
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300596 data[0] = s->handle;
597 /* Lines per field */
598 data[1] = (lines / 2) | ((lines / 2) << 16);
599 /* bytes per line */
Mauro Carvalho Chehab318de792016-06-24 08:40:51 -0300600 data[2] = (raw ? VBI_ACTIVE_SAMPLES
601 : (cx->is_60hz ? VBI_HBLANK_SAMPLES_60HZ
602 : VBI_HBLANK_SAMPLES_50HZ));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300603 /* Every X number of frames a VBI interrupt arrives
604 (frames as in 25 or 30 fps) */
605 data[3] = 1;
Andy Walls302df972009-01-31 00:33:02 -0300606 /*
607 * Set the SAV/EAV RP codes to look for as start/stop points
608 * when in VIP-1.1 mode
609 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300610 if (raw) {
Andy Walls302df972009-01-31 00:33:02 -0300611 /*
612 * Start codes for beginning of "active" line in vertical blank
613 * 0x20 ( VerticalBlank )
614 * 0x60 ( EvenField VerticalBlank )
615 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300616 data[4] = 0x20602060;
Andy Walls302df972009-01-31 00:33:02 -0300617 /*
618 * End codes for end of "active" raw lines and regular lines
619 * 0x30 ( VerticalBlank HorizontalBlank)
620 * 0x70 ( EvenField VerticalBlank HorizontalBlank)
621 * 0x90 (Task HorizontalBlank)
622 * 0xd0 (Task EvenField HorizontalBlank)
623 */
Andy Wallsaf009cf2008-12-12 20:00:29 -0300624 data[5] = 0x307090d0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300625 } else {
Andy Walls302df972009-01-31 00:33:02 -0300626 /*
627 * End codes for active video, we want data in the hblank region
628 * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
629 * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
630 *
631 * Since the V bit is only allowed to toggle in the EAV RP code,
632 * just before the first active region line, these two
Andy Walls812b1f92009-02-08 22:40:04 -0300633 * are problematic:
Andy Walls302df972009-01-31 00:33:02 -0300634 * 0x90 (Task HorizontalBlank)
635 * 0xd0 (Task EvenField HorizontalBlank)
Andy Walls812b1f92009-02-08 22:40:04 -0300636 *
Andy Wallsaf7c58b2009-02-28 20:13:50 -0300637 * We have set the digitzer such that we don't have to worry
638 * about these problem codes.
Andy Walls302df972009-01-31 00:33:02 -0300639 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300640 data[4] = 0xB0F0B0F0;
Andy Walls302df972009-01-31 00:33:02 -0300641 /*
642 * Start codes for beginning of active line in vertical blank
643 * 0xa0 (Task VerticalBlank )
644 * 0xe0 (Task EvenField VerticalBlank )
645 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300646 data[5] = 0xA0E0A0E0;
647 }
648
649 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
650 data[0], data[1], data[2], data[3], data[4], data[5]);
651
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300652 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300653}
654
Andy Wallsef9917972009-12-31 18:27:13 -0300655void cx18_stream_rotate_idx_mdls(struct cx18 *cx)
656{
657 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
658 struct cx18_mdl *mdl;
659
660 if (!cx18_stream_enabled(s))
661 return;
662
663 /* Return if the firmware is not running low on MDLs */
664 if ((atomic_read(&s->q_free.depth) + atomic_read(&s->q_busy.depth)) >=
665 CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN)
666 return;
667
668 /* Return if there are no MDLs to rotate back to the firmware */
669 if (atomic_read(&s->q_full.depth) < 2)
670 return;
671
672 /*
673 * Take the oldest IDX MDL still holding data, and discard its index
674 * entries by scheduling the MDL to go back to the firmware
675 */
676 mdl = cx18_dequeue(s, &s->q_full);
677 if (mdl != NULL)
678 cx18_enqueue(s, mdl, &s->q_free);
679}
680
Andy Walls87116152009-04-13 22:42:43 -0300681static
Andy Walls52fcb3e2009-11-08 23:45:24 -0300682struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s,
683 struct cx18_mdl *mdl)
Andy Walls66c2a6b2008-12-08 23:02:45 -0300684{
685 struct cx18 *cx = s->cx;
686 struct cx18_queue *q;
687
688 /* Don't give it to the firmware, if we're not running a capture */
689 if (s->handle == CX18_INVALID_TASK_HANDLE ||
Andy Walls87116152009-04-13 22:42:43 -0300690 test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
Andy Walls66c2a6b2008-12-08 23:02:45 -0300691 !test_bit(CX18_F_S_STREAMING, &s->s_flags))
Andy Walls52fcb3e2009-11-08 23:45:24 -0300692 return cx18_enqueue(s, mdl, &s->q_free);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300693
Andy Walls52fcb3e2009-11-08 23:45:24 -0300694 q = cx18_enqueue(s, mdl, &s->q_busy);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300695 if (q != &s->q_busy)
Andy Walls52fcb3e2009-11-08 23:45:24 -0300696 return q; /* The firmware has the max MDLs it can handle */
Andy Walls66c2a6b2008-12-08 23:02:45 -0300697
Andy Walls52fcb3e2009-11-08 23:45:24 -0300698 cx18_mdl_sync_for_device(s, mdl);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300699 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
Andy Walls52fcb3e2009-11-08 23:45:24 -0300700 (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
701 s->bufs_per_mdl, mdl->id, s->mdl_size);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300702 return q;
703}
704
Andy Walls87116152009-04-13 22:42:43 -0300705static
706void _cx18_stream_load_fw_queue(struct cx18_stream *s)
Andy Walls66c2a6b2008-12-08 23:02:45 -0300707{
Andy Wallsabb096d2008-12-12 15:50:27 -0300708 struct cx18_queue *q;
Andy Walls52fcb3e2009-11-08 23:45:24 -0300709 struct cx18_mdl *mdl;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300710
Andy Wallsc37b11b2009-11-04 23:13:58 -0300711 if (atomic_read(&s->q_free.depth) == 0 ||
712 atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
Andy Wallsabb096d2008-12-12 15:50:27 -0300713 return;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300714
Andy Wallsabb096d2008-12-12 15:50:27 -0300715 /* Move from q_free to q_busy notifying the firmware, until the limit */
716 do {
Andy Walls52fcb3e2009-11-08 23:45:24 -0300717 mdl = cx18_dequeue(s, &s->q_free);
718 if (mdl == NULL)
Andy Wallsabb096d2008-12-12 15:50:27 -0300719 break;
Andy Walls52fcb3e2009-11-08 23:45:24 -0300720 q = _cx18_stream_put_mdl_fw(s, mdl);
Andy Wallsc37b11b2009-11-04 23:13:58 -0300721 } while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM
Andy Walls0ef02892008-12-14 18:52:12 -0300722 && q == &s->q_busy);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300723}
724
Andy Walls87116152009-04-13 22:42:43 -0300725void cx18_out_work_handler(struct work_struct *work)
726{
Andy Walls21a278b2009-04-15 20:45:10 -0300727 struct cx18_stream *s =
728 container_of(work, struct cx18_stream, out_work_order);
Andy Walls87116152009-04-13 22:42:43 -0300729
Andy Walls21a278b2009-04-15 20:45:10 -0300730 _cx18_stream_load_fw_queue(s);
Andy Walls87116152009-04-13 22:42:43 -0300731}
732
Andy Walls52fcb3e2009-11-08 23:45:24 -0300733static void cx18_stream_configure_mdls(struct cx18_stream *s)
734{
735 cx18_unload_queues(s);
736
Andy Walls22dce182009-11-09 23:55:30 -0300737 switch (s->type) {
738 case CX18_ENC_STREAM_TYPE_YUV:
739 /*
740 * Height should be a multiple of 32 lines.
741 * Set the MDL size to the exact size needed for one frame.
742 * Use enough buffers per MDL to cover the MDL size
743 */
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300744 if (s->pixelformat == V4L2_PIX_FMT_HM12)
745 s->mdl_size = 720 * s->cx->cxhdl.height * 3 / 2;
746 else
747 s->mdl_size = 720 * s->cx->cxhdl.height * 2;
Andy Walls22dce182009-11-09 23:55:30 -0300748 s->bufs_per_mdl = s->mdl_size / s->buf_size;
749 if (s->mdl_size % s->buf_size)
750 s->bufs_per_mdl++;
751 break;
Andy Walls127ce5f2009-11-11 00:22:57 -0300752 case CX18_ENC_STREAM_TYPE_VBI:
753 s->bufs_per_mdl = 1;
754 if (cx18_raw_vbi(s->cx)) {
755 s->mdl_size = (s->cx->is_60hz ? 12 : 18)
Mauro Carvalho Chehab318de792016-06-24 08:40:51 -0300756 * 2 * VBI_ACTIVE_SAMPLES;
Andy Walls127ce5f2009-11-11 00:22:57 -0300757 } else {
758 /*
759 * See comment in cx18_vbi_setup() below about the
760 * extra lines we capture in sliced VBI mode due to
761 * the lines on which EAV RP codes toggle.
762 */
763 s->mdl_size = s->cx->is_60hz
Mauro Carvalho Chehab318de792016-06-24 08:40:51 -0300764 ? (21 - 4 + 1) * 2 * VBI_HBLANK_SAMPLES_60HZ
765 : (23 - 2 + 1) * 2 * VBI_HBLANK_SAMPLES_50HZ;
Andy Walls127ce5f2009-11-11 00:22:57 -0300766 }
767 break;
Andy Walls22dce182009-11-09 23:55:30 -0300768 default:
769 s->bufs_per_mdl = 1;
770 s->mdl_size = s->buf_size * s->bufs_per_mdl;
771 break;
772 }
Andy Walls52fcb3e2009-11-08 23:45:24 -0300773
774 cx18_load_queues(s);
775}
776
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300777int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
778{
779 u32 data[MAX_MB_ARGUMENTS];
780 struct cx18 *cx = s->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300781 int captype = 0;
Andy Wallse46c54a2009-12-31 02:14:51 -0300782 struct cx18_stream *s_idx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300783
Andy Walls540bab92009-12-31 00:26:49 -0300784 if (!cx18_stream_enabled(s))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300785 return -EINVAL;
786
787 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
788
789 switch (s->type) {
790 case CX18_ENC_STREAM_TYPE_MPG:
791 captype = CAPTURE_CHANNEL_TYPE_MPEG;
792 cx->mpg_data_received = cx->vbi_data_inserted = 0;
793 cx->dualwatch_jiffies = jiffies;
Hans Verkuila75b9be2010-12-31 10:22:52 -0300794 cx->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300795 cx->search_pack_header = 0;
796 break;
797
Andy Wallse46c54a2009-12-31 02:14:51 -0300798 case CX18_ENC_STREAM_TYPE_IDX:
799 captype = CAPTURE_CHANNEL_TYPE_INDEX;
800 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300801 case CX18_ENC_STREAM_TYPE_TS:
802 captype = CAPTURE_CHANNEL_TYPE_TS;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300803 break;
804 case CX18_ENC_STREAM_TYPE_YUV:
805 captype = CAPTURE_CHANNEL_TYPE_YUV;
806 break;
807 case CX18_ENC_STREAM_TYPE_PCM:
808 captype = CAPTURE_CHANNEL_TYPE_PCM;
809 break;
810 case CX18_ENC_STREAM_TYPE_VBI:
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300811#ifdef CX18_ENCODER_PARSES_SLICED
Andy Wallsdd073432008-12-12 16:24:04 -0300812 captype = cx18_raw_vbi(cx) ?
813 CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300814#else
815 /*
816 * Currently we set things up so that Sliced VBI from the
817 * digitizer is handled as Raw VBI by the encoder
818 */
819 captype = CAPTURE_CHANNEL_TYPE_VBI;
820#endif
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300821 cx->vbi.frame = 0;
822 cx->vbi.inserted_frame = 0;
823 memset(cx->vbi.sliced_mpeg_size,
824 0, sizeof(cx->vbi.sliced_mpeg_size));
825 break;
826 default:
827 return -EINVAL;
828 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300829
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300830 /* Clear Streamoff flags in case left from last capture */
831 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
832
833 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
834 s->handle = data[0];
835 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
836
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300837 /*
838 * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
839 * set up all the parameters, as it is not obvious which parameters the
840 * firmware shares across capture channel types and which it does not.
841 *
842 * Some of the cx18_vapi() calls below apply to only certain capture
843 * channel types. We're hoping there's no harm in calling most of them
844 * anyway, as long as the values are all consistent. Setting some
845 * shared parameters will have no effect once an analog capture channel
846 * has started streaming.
847 */
848 if (captype != CAPTURE_CHANNEL_TYPE_TS) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300849 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
850 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
851 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
852 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300853
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300854 /*
855 * Audio related reset according to
Mauro Carvalho Chehab2ebe0bb2018-05-08 19:41:44 -0300856 * Documentation/media/v4l-drivers/cx2341x.rst
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300857 */
858 if (atomic_read(&cx->ana_capturing) == 0)
859 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
860 s->handle, 12);
861
862 /*
863 * Number of lines for Field 1 & Field 2 according to
Mauro Carvalho Chehab2ebe0bb2018-05-08 19:41:44 -0300864 * Documentation/media/v4l-drivers/cx2341x.rst
Andy Wallsf37aa512009-02-07 01:15:44 -0300865 * Field 1 is 312 for 625 line systems in BT.656
866 * Field 2 is 313 for 625 line systems in BT.656
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300867 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300868 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
Andy Wallsf37aa512009-02-07 01:15:44 -0300869 s->handle, 312, 313);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300870
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300871 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
872 cx18_vbi_setup(s);
873
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300874 /*
Andy Wallse46c54a2009-12-31 02:14:51 -0300875 * Select to receive I, P, and B frame index entries, if the
876 * index stream is enabled. Otherwise disable index entry
877 * generation.
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300878 */
Andy Wallse46c54a2009-12-31 02:14:51 -0300879 s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
Andy Walls5ada5772010-01-01 13:25:41 -0300880 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 2,
881 s->handle, cx18_stream_enabled(s_idx) ? 7 : 0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300882
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300883 /* Call out to the common CX2341x API setup for user controls */
Hans Verkuila75b9be2010-12-31 10:22:52 -0300884 cx->cxhdl.priv = s;
885 cx2341x_handler_setup(&cx->cxhdl);
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300886
887 /*
888 * When starting a capture and we're set for radio,
889 * ensure the video is muted, despite the user control.
890 */
Hans Verkuila75b9be2010-12-31 10:22:52 -0300891 if (!cx->cxhdl.video_mute &&
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300892 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
893 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
Hans Verkuila75b9be2010-12-31 10:22:52 -0300894 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8) | 1);
Steven Tothb7101de2011-04-06 08:32:56 -0300895
896 /* Enable the Video Format Converter for UYVY 4:2:2 support,
897 * rather than the default HM12 Macroblovk 4:2:0 support.
898 */
899 if (captype == CAPTURE_CHANNEL_TYPE_YUV) {
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300900 if (s->pixelformat == V4L2_PIX_FMT_UYVY)
Steven Tothb7101de2011-04-06 08:32:56 -0300901 cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
902 s->handle, 1);
903 else
904 /* If in doubt, default to HM12 */
905 cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
906 s->handle, 0);
907 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300908 }
909
Hans Verkuil31554ae2008-05-25 11:21:27 -0300910 if (atomic_read(&cx->tot_capturing) == 0) {
Hans Verkuila75b9be2010-12-31 10:22:52 -0300911 cx2341x_handler_set_busy(&cx->cxhdl, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300912 clear_bit(CX18_F_I_EOS, &cx->i_flags);
Andy Wallsb1526422008-08-30 16:03:44 -0300913 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300914 }
915
916 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300917 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
918 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300919
Andy Walls66c2a6b2008-12-08 23:02:45 -0300920 /* Init all the cpu_mdls for this stream */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300921 cx18_stream_configure_mdls(s);
Andy Walls87116152009-04-13 22:42:43 -0300922 _cx18_stream_load_fw_queue(s);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300923
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300924 /* begin_capture */
925 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
926 CX18_DEBUG_WARN("Error starting capture!\n");
Andy Walls3b5df8e2008-08-23 18:36:50 -0300927 /* Ensure we're really not capturing before releasing MDLs */
Andy Walls87116152009-04-13 22:42:43 -0300928 set_bit(CX18_F_S_STOPPING, &s->s_flags);
Andy Walls3b5df8e2008-08-23 18:36:50 -0300929 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
930 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
931 else
932 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300933 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
934 /* FIXME - CX18_F_S_STREAMOFF as well? */
Andy Walls3b5df8e2008-08-23 18:36:50 -0300935 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300936 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300937 s->handle = CX18_INVALID_TASK_HANDLE;
Andy Walls87116152009-04-13 22:42:43 -0300938 clear_bit(CX18_F_S_STOPPING, &s->s_flags);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300939 if (atomic_read(&cx->tot_capturing) == 0) {
940 set_bit(CX18_F_I_EOS, &cx->i_flags);
941 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
942 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300943 return -EINVAL;
944 }
945
946 /* you're live! sit back and await interrupts :) */
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300947 if (captype != CAPTURE_CHANNEL_TYPE_TS)
Hans Verkuil31554ae2008-05-25 11:21:27 -0300948 atomic_inc(&cx->ana_capturing);
949 atomic_inc(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300950 return 0;
951}
Devin Heitmueller0f4cf672009-11-19 23:23:57 -0300952EXPORT_SYMBOL(cx18_start_v4l2_encode_stream);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300953
954void cx18_stop_all_captures(struct cx18 *cx)
955{
956 int i;
957
958 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
959 struct cx18_stream *s = &cx->streams[i];
960
Andy Walls540bab92009-12-31 00:26:49 -0300961 if (!cx18_stream_enabled(s))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300962 continue;
963 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
964 cx18_stop_v4l2_encode_stream(s, 0);
965 }
966}
967
968int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
969{
970 struct cx18 *cx = s->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300971
Andy Walls540bab92009-12-31 00:26:49 -0300972 if (!cx18_stream_enabled(s))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300973 return -EINVAL;
974
975 /* This function assumes that you are allowed to stop the capture
976 and that we are actually capturing */
977
978 CX18_DEBUG_INFO("Stop Capture\n");
979
Hans Verkuil31554ae2008-05-25 11:21:27 -0300980 if (atomic_read(&cx->tot_capturing) == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300981 return 0;
982
Andy Walls87116152009-04-13 22:42:43 -0300983 set_bit(CX18_F_S_STOPPING, &s->s_flags);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300984 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
985 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
986 else
987 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
988
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300989 if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
990 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
991 }
992
Hans Verkuil31554ae2008-05-25 11:21:27 -0300993 if (s->type != CX18_ENC_STREAM_TYPE_TS)
994 atomic_dec(&cx->ana_capturing);
995 atomic_dec(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300996
997 /* Clear capture and no-read bits */
998 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
999
Andy Wallsf68d0cf2008-11-05 21:19:15 -03001000 /* Tell the CX23418 it can't use our buffers anymore */
1001 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
1002
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001003 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Wallsd3c5e702008-08-23 16:42:29 -03001004 s->handle = CX18_INVALID_TASK_HANDLE;
Andy Walls87116152009-04-13 22:42:43 -03001005 clear_bit(CX18_F_S_STOPPING, &s->s_flags);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001006
Hans Verkuil31554ae2008-05-25 11:21:27 -03001007 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001008 return 0;
1009
Hans Verkuila75b9be2010-12-31 10:22:52 -03001010 cx2341x_handler_set_busy(&cx->cxhdl, 0);
Andy Wallsb1526422008-08-30 16:03:44 -03001011 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001012 wake_up(&s->waitq);
1013
1014 return 0;
1015}
Devin Heitmueller0f4cf672009-11-19 23:23:57 -03001016EXPORT_SYMBOL(cx18_stop_v4l2_encode_stream);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001017
1018u32 cx18_find_handle(struct cx18 *cx)
1019{
1020 int i;
1021
1022 /* find first available handle to be used for global settings */
1023 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1024 struct cx18_stream *s = &cx->streams[i];
1025
Hans Verkuil08569d62015-03-09 13:34:03 -03001026 if (s->video_dev.v4l2_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001027 return s->handle;
1028 }
Andy Wallsd3c5e702008-08-23 16:42:29 -03001029 return CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001030}
Andy Wallsee2d64f2008-11-16 01:38:19 -03001031
1032struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
1033{
1034 int i;
1035 struct cx18_stream *s;
1036
1037 if (handle == CX18_INVALID_TASK_HANDLE)
1038 return NULL;
1039
1040 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1041 s = &cx->streams[i];
1042 if (s->handle != handle)
1043 continue;
Andy Walls540bab92009-12-31 00:26:49 -03001044 if (cx18_stream_enabled(s))
Andy Wallsee2d64f2008-11-16 01:38:19 -03001045 return s;
1046 }
1047 return NULL;
1048}