blob: 3995af71b820a3ad4da36938655ae3c096a00787 [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.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
Andy Wallsb1526422008-08-30 16:03:44 -030026#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030027#include "cx18-fileops.h"
28#include "cx18-mailbox.h"
29#include "cx18-i2c.h"
30#include "cx18-queue.h"
31#include "cx18-ioctl.h"
32#include "cx18-streams.h"
33#include "cx18-cards.h"
34#include "cx18-scb.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030035#include "cx18-dvb.h"
36
37#define CX18_DSP0_INTERRUPT_MASK 0xd0004C
38
Hans Verkuilbec43662008-12-30 06:58:20 -030039static struct v4l2_file_operations cx18_v4l2_enc_fops = {
Hans Verkuildaf20d92008-05-12 11:21:58 -030040 .owner = THIS_MODULE,
41 .read = cx18_v4l2_read,
42 .open = cx18_v4l2_open,
Andy Walls3b6fe582008-06-21 08:36:31 -030043 /* FIXME change to video_ioctl2 if serialization lock can be removed */
Hans Verkuil78b055b2010-11-19 17:04:31 -030044 .unlocked_ioctl = cx18_v4l2_ioctl,
Hans Verkuildaf20d92008-05-12 11:21:58 -030045 .release = cx18_v4l2_close,
46 .poll = cx18_v4l2_enc_poll,
Steven Tothb7101de2011-04-06 08:32:56 -030047 .mmap = cx18_v4l2_mmap,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030048};
49
50/* offset from 0 to register ts v4l2 minors on */
51#define CX18_V4L2_ENC_TS_OFFSET 16
52/* offset from 0 to register pcm v4l2 minors on */
53#define CX18_V4L2_ENC_PCM_OFFSET 24
54/* offset from 0 to register yuv v4l2 minors on */
55#define CX18_V4L2_ENC_YUV_OFFSET 32
56
57static struct {
58 const char *name;
59 int vfl_type;
Hans Verkuildd896012008-10-04 08:36:54 -030060 int num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030061 int dma;
62 enum v4l2_buf_type buf_type;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030063} cx18_stream_info[] = {
64 { /* CX18_ENC_STREAM_TYPE_MPG */
65 "encoder MPEG",
66 VFL_TYPE_GRABBER, 0,
67 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030068 },
69 { /* CX18_ENC_STREAM_TYPE_TS */
70 "TS",
71 VFL_TYPE_GRABBER, -1,
72 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030073 },
74 { /* CX18_ENC_STREAM_TYPE_YUV */
75 "encoder YUV",
76 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
77 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030078 },
79 { /* CX18_ENC_STREAM_TYPE_VBI */
80 "encoder VBI",
81 VFL_TYPE_VBI, 0,
82 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
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,
87 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030088 },
89 { /* CX18_ENC_STREAM_TYPE_IDX */
90 "encoder IDX",
91 VFL_TYPE_GRABBER, -1,
92 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030093 },
94 { /* CX18_ENC_STREAM_TYPE_RAD */
95 "encoder radio",
96 VFL_TYPE_RADIO, 0,
97 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030098 },
99};
100
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300101
102void cx18_dma_free(struct videobuf_queue *q,
103 struct cx18_stream *s, struct cx18_videobuf_buffer *buf)
104{
105 videobuf_waiton(q, &buf->vb, 0, 0);
106 videobuf_vmalloc_free(&buf->vb);
107 buf->vb.state = VIDEOBUF_NEEDS_INIT;
108}
109
110static int cx18_prepare_buffer(struct videobuf_queue *q,
111 struct cx18_stream *s,
112 struct cx18_videobuf_buffer *buf,
113 u32 pixelformat,
114 unsigned int width, unsigned int height,
115 enum v4l2_field field)
116{
117 struct cx18 *cx = s->cx;
118 int rc = 0;
119
120 /* check settings */
121 buf->bytes_used = 0;
122
123 if ((width < 48) || (height < 32))
124 return -EINVAL;
125
126 buf->vb.size = (width * height * 2);
127 if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
128 return -EINVAL;
129
130 /* alloc + fill struct (if changed) */
131 if (buf->vb.width != width || buf->vb.height != height ||
132 buf->vb.field != field || s->pixelformat != pixelformat ||
133 buf->tvnorm != cx->std) {
134
135 buf->vb.width = width;
136 buf->vb.height = height;
137 buf->vb.field = field;
138 buf->tvnorm = cx->std;
139 s->pixelformat = pixelformat;
140
141 cx18_dma_free(q, s, buf);
142 }
143
144 if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
145 return -EINVAL;
146
147 if (buf->vb.field == 0)
148 buf->vb.field = V4L2_FIELD_INTERLACED;
149
150 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
151 buf->vb.width = width;
152 buf->vb.height = height;
153 buf->vb.field = field;
154 buf->tvnorm = cx->std;
155 s->pixelformat = pixelformat;
156
157 rc = videobuf_iolock(q, &buf->vb, NULL);
158 if (rc != 0)
159 goto fail;
160 }
161 buf->vb.state = VIDEOBUF_PREPARED;
162 return 0;
163
164fail:
165 cx18_dma_free(q, s, buf);
166 return rc;
167
168}
169
170/* VB_MIN_BUFSIZE is lcm(1440 * 480, 1440 * 576)
171 1440 is a single line of 4:2:2 YUV at 720 luma samples wide
172*/
173#define VB_MIN_BUFFERS 32
174#define VB_MIN_BUFSIZE 4147200
175
176static int buffer_setup(struct videobuf_queue *q,
177 unsigned int *count, unsigned int *size)
178{
179 struct cx18_stream *s = q->priv_data;
180 struct cx18 *cx = s->cx;
181
182 *size = 2 * cx->cxhdl.width * cx->cxhdl.height;
183 if (*count == 0)
184 *count = VB_MIN_BUFFERS;
185
186 while (*size * *count > VB_MIN_BUFFERS * VB_MIN_BUFSIZE)
187 (*count)--;
188
189 q->field = V4L2_FIELD_INTERLACED;
190 q->last = V4L2_FIELD_INTERLACED;
191
192 return 0;
193}
194
195static int buffer_prepare(struct videobuf_queue *q,
196 struct videobuf_buffer *vb,
197 enum v4l2_field field)
198{
199 struct cx18_videobuf_buffer *buf =
200 container_of(vb, struct cx18_videobuf_buffer, vb);
201 struct cx18_stream *s = q->priv_data;
202 struct cx18 *cx = s->cx;
203
204 return cx18_prepare_buffer(q, s, buf, s->pixelformat,
205 cx->cxhdl.width, cx->cxhdl.height, field);
206}
207
208static void buffer_release(struct videobuf_queue *q,
209 struct videobuf_buffer *vb)
210{
211 struct cx18_videobuf_buffer *buf =
212 container_of(vb, struct cx18_videobuf_buffer, vb);
213 struct cx18_stream *s = q->priv_data;
214
215 cx18_dma_free(q, s, buf);
216}
217
218static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
219{
220 struct cx18_videobuf_buffer *buf =
221 container_of(vb, struct cx18_videobuf_buffer, vb);
222 struct cx18_stream *s = q->priv_data;
223
224 buf->vb.state = VIDEOBUF_QUEUED;
225
226 list_add_tail(&buf->vb.queue, &s->vb_capture);
227}
228
229static struct videobuf_queue_ops cx18_videobuf_qops = {
230 .buf_setup = buffer_setup,
231 .buf_prepare = buffer_prepare,
232 .buf_queue = buffer_queue,
233 .buf_release = buffer_release,
234};
235
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300236static void cx18_stream_init(struct cx18 *cx, int type)
237{
238 struct cx18_stream *s = &cx->streams[type];
Andy Walls3d059132009-01-10 21:54:39 -0300239 struct video_device *video_dev = s->video_dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300240
Andy Walls3d059132009-01-10 21:54:39 -0300241 /* we need to keep video_dev, so restore it afterwards */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300242 memset(s, 0, sizeof(*s));
Andy Walls3d059132009-01-10 21:54:39 -0300243 s->video_dev = video_dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300244
245 /* initialize cx18_stream fields */
Andy Walls754f9962010-12-11 20:38:20 -0300246 s->dvb = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300247 s->cx = cx;
248 s->type = type;
249 s->name = cx18_stream_info[type].name;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300250 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300251
252 s->dma = cx18_stream_info[type].dma;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300253 s->buffers = cx->stream_buffers[type];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300254 s->buf_size = cx->stream_buf_size[type];
Andy Walls52fcb3e2009-11-08 23:45:24 -0300255 INIT_LIST_HEAD(&s->buf_pool);
256 s->bufs_per_mdl = 1;
257 s->mdl_size = s->buf_size * s->bufs_per_mdl;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300258
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300259 init_waitqueue_head(&s->waitq);
260 s->id = -1;
Andy Walls40c55202009-04-13 23:08:00 -0300261 spin_lock_init(&s->q_free.lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300262 cx18_queue_init(&s->q_free);
Andy Walls40c55202009-04-13 23:08:00 -0300263 spin_lock_init(&s->q_busy.lock);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300264 cx18_queue_init(&s->q_busy);
Andy Walls40c55202009-04-13 23:08:00 -0300265 spin_lock_init(&s->q_full.lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300266 cx18_queue_init(&s->q_full);
Andy Walls52fcb3e2009-11-08 23:45:24 -0300267 spin_lock_init(&s->q_idle.lock);
268 cx18_queue_init(&s->q_idle);
Andy Walls21a278b2009-04-15 20:45:10 -0300269
270 INIT_WORK(&s->out_work_order, cx18_out_work_handler);
Steven Tothb7101de2011-04-06 08:32:56 -0300271
272 INIT_LIST_HEAD(&s->vb_capture);
273 s->vb_timeout.function = cx18_vb_timeout;
274 s->vb_timeout.data = (unsigned long)s;
275 init_timer(&s->vb_timeout);
276 spin_lock_init(&s->vb_lock);
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300277 if (type == CX18_ENC_STREAM_TYPE_YUV) {
278 s->vb_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
279 videobuf_queue_vmalloc_init(&s->vbuf_q, &cx18_videobuf_qops,
280 &cx->pci_dev->dev, &s->vbuf_q_lock,
281 V4L2_BUF_TYPE_VIDEO_CAPTURE,
282 V4L2_FIELD_INTERLACED,
283 sizeof(struct cx18_videobuf_buffer),
284 s, &cx->serialize_lock);
Steven Tothb7101de2011-04-06 08:32:56 -0300285
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300286 /* Assume the previous pixel default */
287 s->pixelformat = V4L2_PIX_FMT_HM12;
288 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300289}
290
291static int cx18_prep_dev(struct cx18 *cx, int type)
292{
293 struct cx18_stream *s = &cx->streams[type];
294 u32 cap = cx->v4l2_cap;
Hans Verkuildd896012008-10-04 08:36:54 -0300295 int num_offset = cx18_stream_info[type].num_offset;
Andy Walls5811cf92009-02-14 17:08:37 -0300296 int num = cx->instance + cx18_first_minor + num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300297
Andy Walls754f9962010-12-11 20:38:20 -0300298 /*
299 * These five fields are always initialized.
300 * For analog capture related streams, if video_dev == NULL then the
301 * stream is not in use.
302 * For the TS stream, if dvb == NULL then the stream is not in use.
303 * In those cases no other fields but these four can be used.
304 */
Andy Walls3d059132009-01-10 21:54:39 -0300305 s->video_dev = NULL;
Andy Walls754f9962010-12-11 20:38:20 -0300306 s->dvb = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300307 s->cx = cx;
308 s->type = type;
309 s->name = cx18_stream_info[type].name;
310
311 /* Check whether the radio is supported */
312 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
313 return 0;
314
315 /* Check whether VBI is supported */
316 if (type == CX18_ENC_STREAM_TYPE_VBI &&
317 !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
318 return 0;
319
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300320 /* User explicitly selected 0 buffers for these streams, so don't
321 create them. */
322 if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
Andy Walls6ecd86d2008-12-07 23:30:17 -0300323 cx->stream_buffers[type] == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300324 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
325 return 0;
326 }
327
328 cx18_stream_init(cx, type);
329
Andy Walls754f9962010-12-11 20:38:20 -0300330 /* Allocate the cx18_dvb struct only for the TS on cards with DTV */
331 if (type == CX18_ENC_STREAM_TYPE_TS) {
332 if (cx->card->hw_all & CX18_HW_DVB) {
333 s->dvb = kzalloc(sizeof(struct cx18_dvb), GFP_KERNEL);
334 if (s->dvb == NULL) {
335 CX18_ERR("Couldn't allocate cx18_dvb structure"
336 " for %s\n", s->name);
337 return -ENOMEM;
338 }
339 } else {
340 /* Don't need buffers for the TS, if there is no DVB */
341 s->buffers = 0;
342 }
343 }
344
Hans Verkuildd896012008-10-04 08:36:54 -0300345 if (num_offset == -1)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300346 return 0;
347
348 /* allocate and initialize the v4l2 video device structure */
Andy Walls3d059132009-01-10 21:54:39 -0300349 s->video_dev = video_device_alloc();
350 if (s->video_dev == NULL) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300351 CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
352 s->name);
353 return -ENOMEM;
354 }
355
Andy Walls5811cf92009-02-14 17:08:37 -0300356 snprintf(s->video_dev->name, sizeof(s->video_dev->name), "%s %s",
357 cx->v4l2_dev.name, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300358
Andy Walls3d059132009-01-10 21:54:39 -0300359 s->video_dev->num = num;
Andy Walls5811cf92009-02-14 17:08:37 -0300360 s->video_dev->v4l2_dev = &cx->v4l2_dev;
Andy Walls3d059132009-01-10 21:54:39 -0300361 s->video_dev->fops = &cx18_v4l2_enc_fops;
362 s->video_dev->release = video_device_release;
363 s->video_dev->tvnorms = V4L2_STD_ALL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300364 set_bit(V4L2_FL_USE_FH_PRIO, &s->video_dev->flags);
Andy Walls3d059132009-01-10 21:54:39 -0300365 cx18_set_funcs(s->video_dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300366 return 0;
367}
368
369/* Initialize v4l2 variables and register v4l2 devices */
370int cx18_streams_setup(struct cx18 *cx)
371{
Andy Walls9b4a7c82008-10-18 10:20:25 -0300372 int type, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300373
374 /* Setup V4L2 Devices */
375 for (type = 0; type < CX18_MAX_STREAMS; type++) {
376 /* Prepare device */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300377 ret = cx18_prep_dev(cx, type);
378 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300379 break;
380
381 /* Allocate Stream */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300382 ret = cx18_stream_alloc(&cx->streams[type]);
383 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300384 break;
385 }
386 if (type == CX18_MAX_STREAMS)
387 return 0;
388
389 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300390 cx18_streams_cleanup(cx, 0);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300391 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300392}
393
394static int cx18_reg_dev(struct cx18 *cx, int type)
395{
396 struct cx18_stream *s = &cx->streams[type];
397 int vfl_type = cx18_stream_info[type].vfl_type;
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300398 const char *name;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300399 int num, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300400
Andy Walls754f9962010-12-11 20:38:20 -0300401 if (type == CX18_ENC_STREAM_TYPE_TS && s->dvb != NULL) {
Andy Walls9b4a7c82008-10-18 10:20:25 -0300402 ret = cx18_dvb_register(s);
403 if (ret < 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300404 CX18_ERR("DVB failed to register\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300405 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300406 }
407 }
408
Andy Walls3d059132009-01-10 21:54:39 -0300409 if (s->video_dev == NULL)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300410 return 0;
411
Andy Walls3d059132009-01-10 21:54:39 -0300412 num = s->video_dev->num;
Hans Verkuildd896012008-10-04 08:36:54 -0300413 /* card number + user defined offset + device offset */
414 if (type != CX18_ENC_STREAM_TYPE_MPG) {
415 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
416
Andy Walls3d059132009-01-10 21:54:39 -0300417 if (s_mpg->video_dev)
418 num = s_mpg->video_dev->num
419 + cx18_stream_info[type].num_offset;
Hans Verkuildd896012008-10-04 08:36:54 -0300420 }
Andy Walls5811cf92009-02-14 17:08:37 -0300421 video_set_drvdata(s->video_dev, s);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300422
423 /* Register device. First try the desired minor, then any free one. */
Hans Verkuil6b5270d2009-09-06 07:54:00 -0300424 ret = video_register_device_no_warn(s->video_dev, vfl_type, num);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300425 if (ret < 0) {
Hans Verkuil581644d2009-06-19 11:54:00 -0300426 CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300427 s->name, num);
Andy Walls3d059132009-01-10 21:54:39 -0300428 video_device_release(s->video_dev);
429 s->video_dev = NULL;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300430 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300431 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300432
433 name = video_device_node_name(s->video_dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300434
435 switch (vfl_type) {
436 case VFL_TYPE_GRABBER:
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300437 CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n",
438 name, s->name, cx->stream_buffers[type],
Andy Walls22dce182009-11-09 23:55:30 -0300439 cx->stream_buf_size[type] / 1024,
440 (cx->stream_buf_size[type] * 100 / 1024) % 100);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300441 break;
442
443 case VFL_TYPE_RADIO:
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300444 CX18_INFO("Registered device %s for %s\n", name, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300445 break;
446
447 case VFL_TYPE_VBI:
Andy Walls6ecd86d2008-12-07 23:30:17 -0300448 if (cx->stream_buffers[type])
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300449 CX18_INFO("Registered device %s for %s "
Andy Walls6ecd86d2008-12-07 23:30:17 -0300450 "(%d x %d bytes)\n",
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300451 name, s->name, cx->stream_buffers[type],
Andy Walls6ecd86d2008-12-07 23:30:17 -0300452 cx->stream_buf_size[type]);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300453 else
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300454 CX18_INFO("Registered device %s for %s\n",
455 name, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300456 break;
457 }
458
459 return 0;
460}
461
462/* Register v4l2 devices */
463int cx18_streams_register(struct cx18 *cx)
464{
465 int type;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300466 int err;
467 int ret = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300468
469 /* Register V4L2 devices */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300470 for (type = 0; type < CX18_MAX_STREAMS; type++) {
471 err = cx18_reg_dev(cx, type);
472 if (err && ret == 0)
473 ret = err;
474 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300475
Andy Walls9b4a7c82008-10-18 10:20:25 -0300476 if (ret == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300477 return 0;
478
479 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300480 cx18_streams_cleanup(cx, 1);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300481 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300482}
483
484/* Unregister v4l2 devices */
Hans Verkuil3f983872008-05-01 10:31:12 -0300485void cx18_streams_cleanup(struct cx18 *cx, int unregister)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300486{
487 struct video_device *vdev;
488 int type;
489
490 /* Teardown all streams */
491 for (type = 0; type < CX18_MAX_STREAMS; type++) {
Andy Walls7b1dde02009-12-31 01:35:08 -0300492
Andy Walls754f9962010-12-11 20:38:20 -0300493 /* The TS has a cx18_dvb structure, not a video_device */
Andy Walls7b1dde02009-12-31 01:35:08 -0300494 if (type == CX18_ENC_STREAM_TYPE_TS) {
Andy Walls754f9962010-12-11 20:38:20 -0300495 if (cx->streams[type].dvb != NULL) {
496 if (unregister)
497 cx18_dvb_unregister(&cx->streams[type]);
498 kfree(cx->streams[type].dvb);
499 cx->streams[type].dvb = NULL;
Andy Walls7b1dde02009-12-31 01:35:08 -0300500 cx18_stream_free(&cx->streams[type]);
501 }
502 continue;
Hans Verkuilfac36392008-07-18 10:07:10 -0300503 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300504
Andy Walls7b1dde02009-12-31 01:35:08 -0300505 /* No struct video_device, but can have buffers allocated */
506 if (type == CX18_ENC_STREAM_TYPE_IDX) {
Andy Walls0f890ab2011-03-27 20:19:15 -0300507 /* If the module params didn't inhibit IDX ... */
Andy Walls7b1dde02009-12-31 01:35:08 -0300508 if (cx->stream_buffers[type] != 0) {
509 cx->stream_buffers[type] = 0;
Andy Walls0f890ab2011-03-27 20:19:15 -0300510 /*
511 * Before calling cx18_stream_free(),
512 * check if the IDX stream was actually set up.
513 * Needed, since the cx18_probe() error path
514 * exits through here as well as normal clean up
515 */
516 if (cx->streams[type].buffers != 0)
517 cx18_stream_free(&cx->streams[type]);
Andy Walls7b1dde02009-12-31 01:35:08 -0300518 }
519 continue;
520 }
521
522 /* If struct video_device exists, can have buffers allocated */
Andy Walls3d059132009-01-10 21:54:39 -0300523 vdev = cx->streams[type].video_dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300524
Andy Walls3d059132009-01-10 21:54:39 -0300525 cx->streams[type].video_dev = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300526 if (vdev == NULL)
527 continue;
528
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300529 if (type == CX18_ENC_STREAM_TYPE_YUV)
530 videobuf_mmap_free(&cx->streams[type].vbuf_q);
531
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300532 cx18_stream_free(&cx->streams[type]);
533
Hans Verkuil3f983872008-05-01 10:31:12 -0300534 /* Unregister or release device */
535 if (unregister)
536 video_unregister_device(vdev);
537 else
538 video_device_release(vdev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300539 }
540}
541
542static void cx18_vbi_setup(struct cx18_stream *s)
543{
544 struct cx18 *cx = s->cx;
Andy Wallsdd073432008-12-12 16:24:04 -0300545 int raw = cx18_raw_vbi(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300546 u32 data[CX2341X_MBOX_MAX_DATA];
547 int lines;
548
549 if (cx->is_60hz) {
550 cx->vbi.count = 12;
551 cx->vbi.start[0] = 10;
552 cx->vbi.start[1] = 273;
553 } else { /* PAL/SECAM */
554 cx->vbi.count = 18;
555 cx->vbi.start[0] = 6;
556 cx->vbi.start[1] = 318;
557 }
558
559 /* setup VBI registers */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300560 if (raw)
561 v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &cx->vbi.in.fmt.vbi);
562 else
563 v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &cx->vbi.in.fmt.sliced);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300564
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300565 /*
566 * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
567 * VBI when the first analog capture channel starts, as once it starts
568 * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
569 * (i.e. for the VBI capture channels). We also send it for each
570 * analog capture channel anyway just to make sure we get the proper
571 * behavior
572 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300573 if (raw) {
574 lines = cx->vbi.count * 2;
575 } else {
Andy Walls812b1f92009-02-08 22:40:04 -0300576 /*
577 * For 525/60 systems, according to the VIP 2 & BT.656 std:
578 * The EAV RP code's Field bit toggles on line 4, a few lines
579 * after the Vertcal Blank bit has already toggled.
580 * Tell the encoder to capture 21-4+1=18 lines per field,
581 * since we want lines 10 through 21.
582 *
Andy Walls5ab74052009-05-10 22:14:29 -0300583 * For 625/50 systems, according to the VIP 2 & BT.656 std:
584 * The EAV RP code's Field bit toggles on line 1, a few lines
585 * after the Vertcal Blank bit has already toggled.
Andy Walls929a3ad2009-05-16 21:06:57 -0300586 * (We've actually set the digitizer so that the Field bit
587 * toggles on line 2.) Tell the encoder to capture 23-2+1=22
588 * lines per field, since we want lines 6 through 23.
Andy Walls812b1f92009-02-08 22:40:04 -0300589 */
Andy Walls929a3ad2009-05-16 21:06:57 -0300590 lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300591 }
592
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300593 data[0] = s->handle;
594 /* Lines per field */
595 data[1] = (lines / 2) | ((lines / 2) << 16);
596 /* bytes per line */
Andy Walls302df972009-01-31 00:33:02 -0300597 data[2] = (raw ? vbi_active_samples
598 : (cx->is_60hz ? vbi_hblank_samples_60Hz
599 : vbi_hblank_samples_50Hz));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300600 /* Every X number of frames a VBI interrupt arrives
601 (frames as in 25 or 30 fps) */
602 data[3] = 1;
Andy Walls302df972009-01-31 00:33:02 -0300603 /*
604 * Set the SAV/EAV RP codes to look for as start/stop points
605 * when in VIP-1.1 mode
606 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300607 if (raw) {
Andy Walls302df972009-01-31 00:33:02 -0300608 /*
609 * Start codes for beginning of "active" line in vertical blank
610 * 0x20 ( VerticalBlank )
611 * 0x60 ( EvenField VerticalBlank )
612 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300613 data[4] = 0x20602060;
Andy Walls302df972009-01-31 00:33:02 -0300614 /*
615 * End codes for end of "active" raw lines and regular lines
616 * 0x30 ( VerticalBlank HorizontalBlank)
617 * 0x70 ( EvenField VerticalBlank HorizontalBlank)
618 * 0x90 (Task HorizontalBlank)
619 * 0xd0 (Task EvenField HorizontalBlank)
620 */
Andy Wallsaf009cf2008-12-12 20:00:29 -0300621 data[5] = 0x307090d0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300622 } else {
Andy Walls302df972009-01-31 00:33:02 -0300623 /*
624 * End codes for active video, we want data in the hblank region
625 * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
626 * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
627 *
628 * Since the V bit is only allowed to toggle in the EAV RP code,
629 * just before the first active region line, these two
Andy Walls812b1f92009-02-08 22:40:04 -0300630 * are problematic:
Andy Walls302df972009-01-31 00:33:02 -0300631 * 0x90 (Task HorizontalBlank)
632 * 0xd0 (Task EvenField HorizontalBlank)
Andy Walls812b1f92009-02-08 22:40:04 -0300633 *
Andy Wallsaf7c58b2009-02-28 20:13:50 -0300634 * We have set the digitzer such that we don't have to worry
635 * about these problem codes.
Andy Walls302df972009-01-31 00:33:02 -0300636 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300637 data[4] = 0xB0F0B0F0;
Andy Walls302df972009-01-31 00:33:02 -0300638 /*
639 * Start codes for beginning of active line in vertical blank
640 * 0xa0 (Task VerticalBlank )
641 * 0xe0 (Task EvenField VerticalBlank )
642 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300643 data[5] = 0xA0E0A0E0;
644 }
645
646 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
647 data[0], data[1], data[2], data[3], data[4], data[5]);
648
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300649 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300650}
651
Andy Wallsef991792009-12-31 18:27:13 -0300652void cx18_stream_rotate_idx_mdls(struct cx18 *cx)
653{
654 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
655 struct cx18_mdl *mdl;
656
657 if (!cx18_stream_enabled(s))
658 return;
659
660 /* Return if the firmware is not running low on MDLs */
661 if ((atomic_read(&s->q_free.depth) + atomic_read(&s->q_busy.depth)) >=
662 CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN)
663 return;
664
665 /* Return if there are no MDLs to rotate back to the firmware */
666 if (atomic_read(&s->q_full.depth) < 2)
667 return;
668
669 /*
670 * Take the oldest IDX MDL still holding data, and discard its index
671 * entries by scheduling the MDL to go back to the firmware
672 */
673 mdl = cx18_dequeue(s, &s->q_full);
674 if (mdl != NULL)
675 cx18_enqueue(s, mdl, &s->q_free);
676}
677
Andy Walls87116152009-04-13 22:42:43 -0300678static
Andy Walls52fcb3e2009-11-08 23:45:24 -0300679struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s,
680 struct cx18_mdl *mdl)
Andy Walls66c2a6b2008-12-08 23:02:45 -0300681{
682 struct cx18 *cx = s->cx;
683 struct cx18_queue *q;
684
685 /* Don't give it to the firmware, if we're not running a capture */
686 if (s->handle == CX18_INVALID_TASK_HANDLE ||
Andy Walls87116152009-04-13 22:42:43 -0300687 test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
Andy Walls66c2a6b2008-12-08 23:02:45 -0300688 !test_bit(CX18_F_S_STREAMING, &s->s_flags))
Andy Walls52fcb3e2009-11-08 23:45:24 -0300689 return cx18_enqueue(s, mdl, &s->q_free);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300690
Andy Walls52fcb3e2009-11-08 23:45:24 -0300691 q = cx18_enqueue(s, mdl, &s->q_busy);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300692 if (q != &s->q_busy)
Andy Walls52fcb3e2009-11-08 23:45:24 -0300693 return q; /* The firmware has the max MDLs it can handle */
Andy Walls66c2a6b2008-12-08 23:02:45 -0300694
Andy Walls52fcb3e2009-11-08 23:45:24 -0300695 cx18_mdl_sync_for_device(s, mdl);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300696 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
Andy Walls52fcb3e2009-11-08 23:45:24 -0300697 (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
698 s->bufs_per_mdl, mdl->id, s->mdl_size);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300699 return q;
700}
701
Andy Walls87116152009-04-13 22:42:43 -0300702static
703void _cx18_stream_load_fw_queue(struct cx18_stream *s)
Andy Walls66c2a6b2008-12-08 23:02:45 -0300704{
Andy Wallsabb096d2008-12-12 15:50:27 -0300705 struct cx18_queue *q;
Andy Walls52fcb3e2009-11-08 23:45:24 -0300706 struct cx18_mdl *mdl;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300707
Andy Wallsc37b11b2009-11-04 23:13:58 -0300708 if (atomic_read(&s->q_free.depth) == 0 ||
709 atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
Andy Wallsabb096d2008-12-12 15:50:27 -0300710 return;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300711
Andy Wallsabb096d2008-12-12 15:50:27 -0300712 /* Move from q_free to q_busy notifying the firmware, until the limit */
713 do {
Andy Walls52fcb3e2009-11-08 23:45:24 -0300714 mdl = cx18_dequeue(s, &s->q_free);
715 if (mdl == NULL)
Andy Wallsabb096d2008-12-12 15:50:27 -0300716 break;
Andy Walls52fcb3e2009-11-08 23:45:24 -0300717 q = _cx18_stream_put_mdl_fw(s, mdl);
Andy Wallsc37b11b2009-11-04 23:13:58 -0300718 } while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM
Andy Walls0ef02892008-12-14 18:52:12 -0300719 && q == &s->q_busy);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300720}
721
Andy Walls87116152009-04-13 22:42:43 -0300722void cx18_out_work_handler(struct work_struct *work)
723{
Andy Walls21a278b2009-04-15 20:45:10 -0300724 struct cx18_stream *s =
725 container_of(work, struct cx18_stream, out_work_order);
Andy Walls87116152009-04-13 22:42:43 -0300726
Andy Walls21a278b2009-04-15 20:45:10 -0300727 _cx18_stream_load_fw_queue(s);
Andy Walls87116152009-04-13 22:42:43 -0300728}
729
Andy Walls52fcb3e2009-11-08 23:45:24 -0300730static void cx18_stream_configure_mdls(struct cx18_stream *s)
731{
732 cx18_unload_queues(s);
733
Andy Walls22dce182009-11-09 23:55:30 -0300734 switch (s->type) {
735 case CX18_ENC_STREAM_TYPE_YUV:
736 /*
737 * Height should be a multiple of 32 lines.
738 * Set the MDL size to the exact size needed for one frame.
739 * Use enough buffers per MDL to cover the MDL size
740 */
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300741 if (s->pixelformat == V4L2_PIX_FMT_HM12)
742 s->mdl_size = 720 * s->cx->cxhdl.height * 3 / 2;
743 else
744 s->mdl_size = 720 * s->cx->cxhdl.height * 2;
Andy Walls22dce182009-11-09 23:55:30 -0300745 s->bufs_per_mdl = s->mdl_size / s->buf_size;
746 if (s->mdl_size % s->buf_size)
747 s->bufs_per_mdl++;
748 break;
Andy Walls127ce5f2009-11-11 00:22:57 -0300749 case CX18_ENC_STREAM_TYPE_VBI:
750 s->bufs_per_mdl = 1;
751 if (cx18_raw_vbi(s->cx)) {
752 s->mdl_size = (s->cx->is_60hz ? 12 : 18)
753 * 2 * vbi_active_samples;
754 } else {
755 /*
756 * See comment in cx18_vbi_setup() below about the
757 * extra lines we capture in sliced VBI mode due to
758 * the lines on which EAV RP codes toggle.
759 */
760 s->mdl_size = s->cx->is_60hz
761 ? (21 - 4 + 1) * 2 * vbi_hblank_samples_60Hz
762 : (23 - 2 + 1) * 2 * vbi_hblank_samples_50Hz;
763 }
764 break;
Andy Walls22dce182009-11-09 23:55:30 -0300765 default:
766 s->bufs_per_mdl = 1;
767 s->mdl_size = s->buf_size * s->bufs_per_mdl;
768 break;
769 }
Andy Walls52fcb3e2009-11-08 23:45:24 -0300770
771 cx18_load_queues(s);
772}
773
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300774int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
775{
776 u32 data[MAX_MB_ARGUMENTS];
777 struct cx18 *cx = s->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300778 int captype = 0;
Andy Wallse46c54a2009-12-31 02:14:51 -0300779 struct cx18_stream *s_idx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300780
Andy Walls540bab92009-12-31 00:26:49 -0300781 if (!cx18_stream_enabled(s))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300782 return -EINVAL;
783
784 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
785
786 switch (s->type) {
787 case CX18_ENC_STREAM_TYPE_MPG:
788 captype = CAPTURE_CHANNEL_TYPE_MPEG;
789 cx->mpg_data_received = cx->vbi_data_inserted = 0;
790 cx->dualwatch_jiffies = jiffies;
Hans Verkuila75b9be2010-12-31 10:22:52 -0300791 cx->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300792 cx->search_pack_header = 0;
793 break;
794
Andy Wallse46c54a2009-12-31 02:14:51 -0300795 case CX18_ENC_STREAM_TYPE_IDX:
796 captype = CAPTURE_CHANNEL_TYPE_INDEX;
797 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300798 case CX18_ENC_STREAM_TYPE_TS:
799 captype = CAPTURE_CHANNEL_TYPE_TS;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300800 break;
801 case CX18_ENC_STREAM_TYPE_YUV:
802 captype = CAPTURE_CHANNEL_TYPE_YUV;
803 break;
804 case CX18_ENC_STREAM_TYPE_PCM:
805 captype = CAPTURE_CHANNEL_TYPE_PCM;
806 break;
807 case CX18_ENC_STREAM_TYPE_VBI:
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300808#ifdef CX18_ENCODER_PARSES_SLICED
Andy Wallsdd073432008-12-12 16:24:04 -0300809 captype = cx18_raw_vbi(cx) ?
810 CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300811#else
812 /*
813 * Currently we set things up so that Sliced VBI from the
814 * digitizer is handled as Raw VBI by the encoder
815 */
816 captype = CAPTURE_CHANNEL_TYPE_VBI;
817#endif
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300818 cx->vbi.frame = 0;
819 cx->vbi.inserted_frame = 0;
820 memset(cx->vbi.sliced_mpeg_size,
821 0, sizeof(cx->vbi.sliced_mpeg_size));
822 break;
823 default:
824 return -EINVAL;
825 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300826
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300827 /* Clear Streamoff flags in case left from last capture */
828 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
829
830 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
831 s->handle = data[0];
832 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
833
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300834 /*
835 * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
836 * set up all the parameters, as it is not obvious which parameters the
837 * firmware shares across capture channel types and which it does not.
838 *
839 * Some of the cx18_vapi() calls below apply to only certain capture
840 * channel types. We're hoping there's no harm in calling most of them
841 * anyway, as long as the values are all consistent. Setting some
842 * shared parameters will have no effect once an analog capture channel
843 * has started streaming.
844 */
845 if (captype != CAPTURE_CHANNEL_TYPE_TS) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300846 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
847 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
848 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
849 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300850
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300851 /*
852 * Audio related reset according to
853 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
854 */
855 if (atomic_read(&cx->ana_capturing) == 0)
856 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
857 s->handle, 12);
858
859 /*
860 * Number of lines for Field 1 & Field 2 according to
861 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
Andy Wallsf37aa512009-02-07 01:15:44 -0300862 * Field 1 is 312 for 625 line systems in BT.656
863 * Field 2 is 313 for 625 line systems in BT.656
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300864 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300865 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
Andy Wallsf37aa512009-02-07 01:15:44 -0300866 s->handle, 312, 313);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300867
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300868 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
869 cx18_vbi_setup(s);
870
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300871 /*
Andy Wallse46c54a2009-12-31 02:14:51 -0300872 * Select to receive I, P, and B frame index entries, if the
873 * index stream is enabled. Otherwise disable index entry
874 * generation.
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300875 */
Andy Wallse46c54a2009-12-31 02:14:51 -0300876 s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
Andy Walls5ada5772010-01-01 13:25:41 -0300877 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 2,
878 s->handle, cx18_stream_enabled(s_idx) ? 7 : 0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300879
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300880 /* Call out to the common CX2341x API setup for user controls */
Hans Verkuila75b9be2010-12-31 10:22:52 -0300881 cx->cxhdl.priv = s;
882 cx2341x_handler_setup(&cx->cxhdl);
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300883
884 /*
885 * When starting a capture and we're set for radio,
886 * ensure the video is muted, despite the user control.
887 */
Hans Verkuila75b9be2010-12-31 10:22:52 -0300888 if (!cx->cxhdl.video_mute &&
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300889 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
890 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
Hans Verkuila75b9be2010-12-31 10:22:52 -0300891 (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8) | 1);
Steven Tothb7101de2011-04-06 08:32:56 -0300892
893 /* Enable the Video Format Converter for UYVY 4:2:2 support,
894 * rather than the default HM12 Macroblovk 4:2:0 support.
895 */
896 if (captype == CAPTURE_CHANNEL_TYPE_YUV) {
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300897 if (s->pixelformat == V4L2_PIX_FMT_UYVY)
Steven Tothb7101de2011-04-06 08:32:56 -0300898 cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
899 s->handle, 1);
900 else
901 /* If in doubt, default to HM12 */
902 cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
903 s->handle, 0);
904 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300905 }
906
Hans Verkuil31554ae2008-05-25 11:21:27 -0300907 if (atomic_read(&cx->tot_capturing) == 0) {
Hans Verkuila75b9be2010-12-31 10:22:52 -0300908 cx2341x_handler_set_busy(&cx->cxhdl, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300909 clear_bit(CX18_F_I_EOS, &cx->i_flags);
Andy Wallsb1526422008-08-30 16:03:44 -0300910 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300911 }
912
913 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300914 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
915 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300916
Andy Walls66c2a6b2008-12-08 23:02:45 -0300917 /* Init all the cpu_mdls for this stream */
Andy Walls52fcb3e2009-11-08 23:45:24 -0300918 cx18_stream_configure_mdls(s);
Andy Walls87116152009-04-13 22:42:43 -0300919 _cx18_stream_load_fw_queue(s);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300920
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300921 /* begin_capture */
922 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
923 CX18_DEBUG_WARN("Error starting capture!\n");
Andy Walls3b5df8e2008-08-23 18:36:50 -0300924 /* Ensure we're really not capturing before releasing MDLs */
Andy Walls87116152009-04-13 22:42:43 -0300925 set_bit(CX18_F_S_STOPPING, &s->s_flags);
Andy Walls3b5df8e2008-08-23 18:36:50 -0300926 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
927 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
928 else
929 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300930 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
931 /* FIXME - CX18_F_S_STREAMOFF as well? */
Andy Walls3b5df8e2008-08-23 18:36:50 -0300932 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300933 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300934 s->handle = CX18_INVALID_TASK_HANDLE;
Andy Walls87116152009-04-13 22:42:43 -0300935 clear_bit(CX18_F_S_STOPPING, &s->s_flags);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300936 if (atomic_read(&cx->tot_capturing) == 0) {
937 set_bit(CX18_F_I_EOS, &cx->i_flags);
938 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
939 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300940 return -EINVAL;
941 }
942
943 /* you're live! sit back and await interrupts :) */
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300944 if (captype != CAPTURE_CHANNEL_TYPE_TS)
Hans Verkuil31554ae2008-05-25 11:21:27 -0300945 atomic_inc(&cx->ana_capturing);
946 atomic_inc(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300947 return 0;
948}
Devin Heitmueller0f4cf672009-11-19 23:23:57 -0300949EXPORT_SYMBOL(cx18_start_v4l2_encode_stream);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300950
951void cx18_stop_all_captures(struct cx18 *cx)
952{
953 int i;
954
955 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
956 struct cx18_stream *s = &cx->streams[i];
957
Andy Walls540bab92009-12-31 00:26:49 -0300958 if (!cx18_stream_enabled(s))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300959 continue;
960 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
961 cx18_stop_v4l2_encode_stream(s, 0);
962 }
963}
964
965int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
966{
967 struct cx18 *cx = s->cx;
968 unsigned long then;
969
Andy Walls540bab92009-12-31 00:26:49 -0300970 if (!cx18_stream_enabled(s))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300971 return -EINVAL;
972
973 /* This function assumes that you are allowed to stop the capture
974 and that we are actually capturing */
975
976 CX18_DEBUG_INFO("Stop Capture\n");
977
Hans Verkuil31554ae2008-05-25 11:21:27 -0300978 if (atomic_read(&cx->tot_capturing) == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300979 return 0;
980
Andy Walls87116152009-04-13 22:42:43 -0300981 set_bit(CX18_F_S_STOPPING, &s->s_flags);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300982 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
983 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
984 else
985 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
986
987 then = jiffies;
988
989 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
Andy Walls3d059132009-01-10 21:54:39 -03001026 if (s->video_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}