blob: e1934e9cfdc82686fc2f38cbab21403104e1b074 [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 Walls1ed9dcc2008-11-22 01:37:34 -03007 * Copyright (C) 2008 Andy Walls <awalls@radix.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 Verkuildaf20d92008-05-12 11:21:58 -030044 .ioctl = cx18_v4l2_ioctl,
Hans Verkuildaf20d92008-05-12 11:21:58 -030045 .release = cx18_v4l2_close,
46 .poll = cx18_v4l2_enc_poll,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030047};
48
49/* offset from 0 to register ts v4l2 minors on */
50#define CX18_V4L2_ENC_TS_OFFSET 16
51/* offset from 0 to register pcm v4l2 minors on */
52#define CX18_V4L2_ENC_PCM_OFFSET 24
53/* offset from 0 to register yuv v4l2 minors on */
54#define CX18_V4L2_ENC_YUV_OFFSET 32
55
56static struct {
57 const char *name;
58 int vfl_type;
Hans Verkuildd896012008-10-04 08:36:54 -030059 int num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030060 int dma;
61 enum v4l2_buf_type buf_type;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030062} cx18_stream_info[] = {
63 { /* CX18_ENC_STREAM_TYPE_MPG */
64 "encoder MPEG",
65 VFL_TYPE_GRABBER, 0,
66 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030067 },
68 { /* CX18_ENC_STREAM_TYPE_TS */
69 "TS",
70 VFL_TYPE_GRABBER, -1,
71 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030072 },
73 { /* CX18_ENC_STREAM_TYPE_YUV */
74 "encoder YUV",
75 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
76 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030077 },
78 { /* CX18_ENC_STREAM_TYPE_VBI */
79 "encoder VBI",
80 VFL_TYPE_VBI, 0,
81 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030082 },
83 { /* CX18_ENC_STREAM_TYPE_PCM */
84 "encoder PCM audio",
85 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
86 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030087 },
88 { /* CX18_ENC_STREAM_TYPE_IDX */
89 "encoder IDX",
90 VFL_TYPE_GRABBER, -1,
91 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030092 },
93 { /* CX18_ENC_STREAM_TYPE_RAD */
94 "encoder radio",
95 VFL_TYPE_RADIO, 0,
96 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030097 },
98};
99
100static void cx18_stream_init(struct cx18 *cx, int type)
101{
102 struct cx18_stream *s = &cx->streams[type];
Andy Walls3d059132009-01-10 21:54:39 -0300103 struct video_device *video_dev = s->video_dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300104
Andy Walls3d059132009-01-10 21:54:39 -0300105 /* we need to keep video_dev, so restore it afterwards */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300106 memset(s, 0, sizeof(*s));
Andy Walls3d059132009-01-10 21:54:39 -0300107 s->video_dev = video_dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300108
109 /* initialize cx18_stream fields */
110 s->cx = cx;
111 s->type = type;
112 s->name = cx18_stream_info[type].name;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300113 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300114
115 s->dma = cx18_stream_info[type].dma;
Andy Walls6ecd86d2008-12-07 23:30:17 -0300116 s->buffers = cx->stream_buffers[type];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300117 s->buf_size = cx->stream_buf_size[type];
Andy Walls6ecd86d2008-12-07 23:30:17 -0300118
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300119 init_waitqueue_head(&s->waitq);
120 s->id = -1;
Andy Walls40c55202009-04-13 23:08:00 -0300121 spin_lock_init(&s->q_free.lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300122 cx18_queue_init(&s->q_free);
Andy Walls40c55202009-04-13 23:08:00 -0300123 spin_lock_init(&s->q_busy.lock);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300124 cx18_queue_init(&s->q_busy);
Andy Walls40c55202009-04-13 23:08:00 -0300125 spin_lock_init(&s->q_full.lock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300126 cx18_queue_init(&s->q_full);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300127}
128
129static int cx18_prep_dev(struct cx18 *cx, int type)
130{
131 struct cx18_stream *s = &cx->streams[type];
132 u32 cap = cx->v4l2_cap;
Hans Verkuildd896012008-10-04 08:36:54 -0300133 int num_offset = cx18_stream_info[type].num_offset;
Andy Walls5811cf92009-02-14 17:08:37 -0300134 int num = cx->instance + cx18_first_minor + num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300135
Andy Walls3d059132009-01-10 21:54:39 -0300136 /* These four fields are always initialized. If video_dev == NULL, then
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300137 this stream is not in use. In that case no other fields but these
138 four can be used. */
Andy Walls3d059132009-01-10 21:54:39 -0300139 s->video_dev = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300140 s->cx = cx;
141 s->type = type;
142 s->name = cx18_stream_info[type].name;
143
144 /* Check whether the radio is supported */
145 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
146 return 0;
147
148 /* Check whether VBI is supported */
149 if (type == CX18_ENC_STREAM_TYPE_VBI &&
150 !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
151 return 0;
152
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300153 /* User explicitly selected 0 buffers for these streams, so don't
154 create them. */
155 if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
Andy Walls6ecd86d2008-12-07 23:30:17 -0300156 cx->stream_buffers[type] == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300157 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
158 return 0;
159 }
160
161 cx18_stream_init(cx, type);
162
Hans Verkuildd896012008-10-04 08:36:54 -0300163 if (num_offset == -1)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300164 return 0;
165
166 /* allocate and initialize the v4l2 video device structure */
Andy Walls3d059132009-01-10 21:54:39 -0300167 s->video_dev = video_device_alloc();
168 if (s->video_dev == NULL) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300169 CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
170 s->name);
171 return -ENOMEM;
172 }
173
Andy Walls5811cf92009-02-14 17:08:37 -0300174 snprintf(s->video_dev->name, sizeof(s->video_dev->name), "%s %s",
175 cx->v4l2_dev.name, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300176
Andy Walls3d059132009-01-10 21:54:39 -0300177 s->video_dev->num = num;
Andy Walls5811cf92009-02-14 17:08:37 -0300178 s->video_dev->v4l2_dev = &cx->v4l2_dev;
Andy Walls3d059132009-01-10 21:54:39 -0300179 s->video_dev->fops = &cx18_v4l2_enc_fops;
180 s->video_dev->release = video_device_release;
181 s->video_dev->tvnorms = V4L2_STD_ALL;
182 cx18_set_funcs(s->video_dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300183 return 0;
184}
185
186/* Initialize v4l2 variables and register v4l2 devices */
187int cx18_streams_setup(struct cx18 *cx)
188{
Andy Walls9b4a7c82008-10-18 10:20:25 -0300189 int type, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300190
191 /* Setup V4L2 Devices */
192 for (type = 0; type < CX18_MAX_STREAMS; type++) {
193 /* Prepare device */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300194 ret = cx18_prep_dev(cx, type);
195 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300196 break;
197
198 /* Allocate Stream */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300199 ret = cx18_stream_alloc(&cx->streams[type]);
200 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300201 break;
202 }
203 if (type == CX18_MAX_STREAMS)
204 return 0;
205
206 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300207 cx18_streams_cleanup(cx, 0);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300208 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300209}
210
211static int cx18_reg_dev(struct cx18 *cx, int type)
212{
213 struct cx18_stream *s = &cx->streams[type];
214 int vfl_type = cx18_stream_info[type].vfl_type;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300215 int num, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300216
217 /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
218 * We need a VFL_TYPE_TS defined.
219 */
220 if (strcmp("TS", s->name) == 0) {
221 /* just return if no DVB is supported */
222 if ((cx->card->hw_all & CX18_HW_DVB) == 0)
223 return 0;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300224 ret = cx18_dvb_register(s);
225 if (ret < 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300226 CX18_ERR("DVB failed to register\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300227 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300228 }
229 }
230
Andy Walls3d059132009-01-10 21:54:39 -0300231 if (s->video_dev == NULL)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300232 return 0;
233
Andy Walls3d059132009-01-10 21:54:39 -0300234 num = s->video_dev->num;
Hans Verkuildd896012008-10-04 08:36:54 -0300235 /* card number + user defined offset + device offset */
236 if (type != CX18_ENC_STREAM_TYPE_MPG) {
237 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
238
Andy Walls3d059132009-01-10 21:54:39 -0300239 if (s_mpg->video_dev)
240 num = s_mpg->video_dev->num
241 + cx18_stream_info[type].num_offset;
Hans Verkuildd896012008-10-04 08:36:54 -0300242 }
Andy Walls5811cf92009-02-14 17:08:37 -0300243 video_set_drvdata(s->video_dev, s);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300244
245 /* Register device. First try the desired minor, then any free one. */
Andy Walls3d059132009-01-10 21:54:39 -0300246 ret = video_register_device(s->video_dev, vfl_type, num);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300247 if (ret < 0) {
Hans Verkuildd896012008-10-04 08:36:54 -0300248 CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
249 s->name, num);
Andy Walls3d059132009-01-10 21:54:39 -0300250 video_device_release(s->video_dev);
251 s->video_dev = NULL;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300252 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300253 }
Andy Walls3d059132009-01-10 21:54:39 -0300254 num = s->video_dev->num;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300255
256 switch (vfl_type) {
257 case VFL_TYPE_GRABBER:
Andy Walls6ecd86d2008-12-07 23:30:17 -0300258 CX18_INFO("Registered device video%d for %s (%d x %d kB)\n",
259 num, s->name, cx->stream_buffers[type],
260 cx->stream_buf_size[type]/1024);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300261 break;
262
263 case VFL_TYPE_RADIO:
264 CX18_INFO("Registered device radio%d for %s\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300265 num, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300266 break;
267
268 case VFL_TYPE_VBI:
Andy Walls6ecd86d2008-12-07 23:30:17 -0300269 if (cx->stream_buffers[type])
270 CX18_INFO("Registered device vbi%d for %s "
271 "(%d x %d bytes)\n",
272 num, s->name, cx->stream_buffers[type],
273 cx->stream_buf_size[type]);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300274 else
275 CX18_INFO("Registered device vbi%d for %s\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300276 num, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300277 break;
278 }
279
280 return 0;
281}
282
283/* Register v4l2 devices */
284int cx18_streams_register(struct cx18 *cx)
285{
286 int type;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300287 int err;
288 int ret = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300289
290 /* Register V4L2 devices */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300291 for (type = 0; type < CX18_MAX_STREAMS; type++) {
292 err = cx18_reg_dev(cx, type);
293 if (err && ret == 0)
294 ret = err;
295 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300296
Andy Walls9b4a7c82008-10-18 10:20:25 -0300297 if (ret == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300298 return 0;
299
300 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300301 cx18_streams_cleanup(cx, 1);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300302 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300303}
304
305/* Unregister v4l2 devices */
Hans Verkuil3f983872008-05-01 10:31:12 -0300306void cx18_streams_cleanup(struct cx18 *cx, int unregister)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300307{
308 struct video_device *vdev;
309 int type;
310
311 /* Teardown all streams */
312 for (type = 0; type < CX18_MAX_STREAMS; type++) {
Hans Verkuilfac36392008-07-18 10:07:10 -0300313 if (cx->streams[type].dvb.enabled) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300314 cx18_dvb_unregister(&cx->streams[type]);
Hans Verkuilfac36392008-07-18 10:07:10 -0300315 cx->streams[type].dvb.enabled = false;
316 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300317
Andy Walls3d059132009-01-10 21:54:39 -0300318 vdev = cx->streams[type].video_dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300319
Andy Walls3d059132009-01-10 21:54:39 -0300320 cx->streams[type].video_dev = NULL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300321 if (vdev == NULL)
322 continue;
323
324 cx18_stream_free(&cx->streams[type]);
325
Hans Verkuil3f983872008-05-01 10:31:12 -0300326 /* Unregister or release device */
327 if (unregister)
328 video_unregister_device(vdev);
329 else
330 video_device_release(vdev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300331 }
332}
333
334static void cx18_vbi_setup(struct cx18_stream *s)
335{
336 struct cx18 *cx = s->cx;
Andy Wallsdd073432008-12-12 16:24:04 -0300337 int raw = cx18_raw_vbi(cx);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300338 u32 data[CX2341X_MBOX_MAX_DATA];
339 int lines;
340
341 if (cx->is_60hz) {
342 cx->vbi.count = 12;
343 cx->vbi.start[0] = 10;
344 cx->vbi.start[1] = 273;
345 } else { /* PAL/SECAM */
346 cx->vbi.count = 18;
347 cx->vbi.start[0] = 6;
348 cx->vbi.start[1] = 318;
349 }
350
351 /* setup VBI registers */
Andy Wallsfa3e7032009-02-16 02:23:25 -0300352 v4l2_subdev_call(cx->sd_av, video, s_fmt, &cx->vbi.in);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300353
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300354 /*
355 * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
356 * VBI when the first analog capture channel starts, as once it starts
357 * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
358 * (i.e. for the VBI capture channels). We also send it for each
359 * analog capture channel anyway just to make sure we get the proper
360 * behavior
361 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300362 if (raw) {
363 lines = cx->vbi.count * 2;
364 } else {
Andy Walls812b1f92009-02-08 22:40:04 -0300365 /*
366 * For 525/60 systems, according to the VIP 2 & BT.656 std:
367 * The EAV RP code's Field bit toggles on line 4, a few lines
368 * after the Vertcal Blank bit has already toggled.
369 * Tell the encoder to capture 21-4+1=18 lines per field,
370 * since we want lines 10 through 21.
371 *
372 * FIXME - revisit for 625/50 systems
373 */
374 lines = cx->is_60hz ? (21 - 4 + 1) * 2 : 38;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300375 }
376
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300377 data[0] = s->handle;
378 /* Lines per field */
379 data[1] = (lines / 2) | ((lines / 2) << 16);
380 /* bytes per line */
Andy Walls302df972009-01-31 00:33:02 -0300381 data[2] = (raw ? vbi_active_samples
382 : (cx->is_60hz ? vbi_hblank_samples_60Hz
383 : vbi_hblank_samples_50Hz));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300384 /* Every X number of frames a VBI interrupt arrives
385 (frames as in 25 or 30 fps) */
386 data[3] = 1;
Andy Walls302df972009-01-31 00:33:02 -0300387 /*
388 * Set the SAV/EAV RP codes to look for as start/stop points
389 * when in VIP-1.1 mode
390 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300391 if (raw) {
Andy Walls302df972009-01-31 00:33:02 -0300392 /*
393 * Start codes for beginning of "active" line in vertical blank
394 * 0x20 ( VerticalBlank )
395 * 0x60 ( EvenField VerticalBlank )
396 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300397 data[4] = 0x20602060;
Andy Walls302df972009-01-31 00:33:02 -0300398 /*
399 * End codes for end of "active" raw lines and regular lines
400 * 0x30 ( VerticalBlank HorizontalBlank)
401 * 0x70 ( EvenField VerticalBlank HorizontalBlank)
402 * 0x90 (Task HorizontalBlank)
403 * 0xd0 (Task EvenField HorizontalBlank)
404 */
Andy Wallsaf009cf2008-12-12 20:00:29 -0300405 data[5] = 0x307090d0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300406 } else {
Andy Walls302df972009-01-31 00:33:02 -0300407 /*
408 * End codes for active video, we want data in the hblank region
409 * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
410 * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
411 *
412 * Since the V bit is only allowed to toggle in the EAV RP code,
413 * just before the first active region line, these two
Andy Walls812b1f92009-02-08 22:40:04 -0300414 * are problematic:
Andy Walls302df972009-01-31 00:33:02 -0300415 * 0x90 (Task HorizontalBlank)
416 * 0xd0 (Task EvenField HorizontalBlank)
Andy Walls812b1f92009-02-08 22:40:04 -0300417 *
Andy Wallsaf7c58b2009-02-28 20:13:50 -0300418 * We have set the digitzer such that we don't have to worry
419 * about these problem codes.
Andy Walls302df972009-01-31 00:33:02 -0300420 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300421 data[4] = 0xB0F0B0F0;
Andy Walls302df972009-01-31 00:33:02 -0300422 /*
423 * Start codes for beginning of active line in vertical blank
424 * 0xa0 (Task VerticalBlank )
425 * 0xe0 (Task EvenField VerticalBlank )
426 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300427 data[5] = 0xA0E0A0E0;
428 }
429
430 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
431 data[0], data[1], data[2], data[3], data[4], data[5]);
432
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300433 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300434}
435
Andy Walls87116152009-04-13 22:42:43 -0300436static
437struct cx18_queue *_cx18_stream_put_buf_fw(struct cx18_stream *s,
438 struct cx18_buffer *buf)
Andy Walls66c2a6b2008-12-08 23:02:45 -0300439{
440 struct cx18 *cx = s->cx;
441 struct cx18_queue *q;
442
443 /* Don't give it to the firmware, if we're not running a capture */
444 if (s->handle == CX18_INVALID_TASK_HANDLE ||
Andy Walls87116152009-04-13 22:42:43 -0300445 test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
Andy Walls66c2a6b2008-12-08 23:02:45 -0300446 !test_bit(CX18_F_S_STREAMING, &s->s_flags))
447 return cx18_enqueue(s, buf, &s->q_free);
448
449 q = cx18_enqueue(s, buf, &s->q_busy);
450 if (q != &s->q_busy)
451 return q; /* The firmware has the max buffers it can handle */
452
453 cx18_buf_sync_for_device(s, buf);
454 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
455 (void __iomem *) &cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
456 1, buf->id, s->buf_size);
457 return q;
458}
459
Andy Walls87116152009-04-13 22:42:43 -0300460static
461void _cx18_stream_load_fw_queue(struct cx18_stream *s)
Andy Walls66c2a6b2008-12-08 23:02:45 -0300462{
Andy Wallsabb096d2008-12-12 15:50:27 -0300463 struct cx18_queue *q;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300464 struct cx18_buffer *buf;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300465
Andy Wallsabb096d2008-12-12 15:50:27 -0300466 if (atomic_read(&s->q_free.buffers) == 0 ||
Andy Walls0ef02892008-12-14 18:52:12 -0300467 atomic_read(&s->q_busy.buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
Andy Wallsabb096d2008-12-12 15:50:27 -0300468 return;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300469
Andy Wallsabb096d2008-12-12 15:50:27 -0300470 /* Move from q_free to q_busy notifying the firmware, until the limit */
471 do {
472 buf = cx18_dequeue(s, &s->q_free);
473 if (buf == NULL)
474 break;
Andy Walls87116152009-04-13 22:42:43 -0300475 q = _cx18_stream_put_buf_fw(s, buf);
Andy Walls0ef02892008-12-14 18:52:12 -0300476 } while (atomic_read(&s->q_busy.buffers) < CX18_MAX_FW_MDLS_PER_STREAM
477 && q == &s->q_busy);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300478}
479
Andy Walls87116152009-04-13 22:42:43 -0300480static inline
481void free_out_work_order(struct cx18_out_work_order *order)
482{
483 atomic_set(&order->pending, 0);
484}
485
486void cx18_out_work_handler(struct work_struct *work)
487{
488 struct cx18_out_work_order *order =
489 container_of(work, struct cx18_out_work_order, work);
490 struct cx18_stream *s = order->s;
491 struct cx18_buffer *buf = order->buf;
492
493 free_out_work_order(order);
494
495 if (buf == NULL)
496 _cx18_stream_load_fw_queue(s);
497 else
498 _cx18_stream_put_buf_fw(s, buf);
499}
500
501static
502struct cx18_out_work_order *alloc_out_work_order(struct cx18 *cx)
503{
504 int i;
505 struct cx18_out_work_order *order = NULL;
506
507 for (i = 0; i < CX18_MAX_OUT_WORK_ORDERS; i++) {
508 /*
509 * We need "pending" to be atomic to inspect & set its contents
510 * 1. "pending" is only set to 1 here, but needs multiple access
511 * protection
512 * 2. work handler threads only clear "pending" and only
513 * on one, particular work order at a time, per handler thread.
514 */
515 if (atomic_add_unless(&cx->out_work_order[i].pending, 1, 1)) {
516 order = &cx->out_work_order[i];
517 break;
518 }
519 }
520 return order;
521}
522
523struct cx18_queue *cx18_stream_put_buf_fw(struct cx18_stream *s,
524 struct cx18_buffer *buf)
525{
526 struct cx18 *cx = s->cx;
527 struct cx18_out_work_order *order;
528
529 order = alloc_out_work_order(cx);
530 if (order == NULL) {
531 CX18_DEBUG_WARN("No blank, outgoing-mailbox, deferred-work, "
532 "order forms available; sending buffer %u back "
533 "to the firmware immediately for stream %s\n",
534 buf->id, s->name);
535 return _cx18_stream_put_buf_fw(s, buf);
536 }
537 order->s = s;
538 order->buf = buf;
539 queue_work(cx->out_work_queue, &order->work);
540 return NULL;
541}
542
543void cx18_stream_load_fw_queue(struct cx18_stream *s)
544{
545 struct cx18 *cx = s->cx;
546 struct cx18_out_work_order *order;
547
548 order = alloc_out_work_order(cx);
549 if (order == NULL) {
550 CX18_DEBUG_WARN("No blank, outgoing-mailbox, deferred-work, "
551 "order forms available; filling the firmware "
552 "buffer queue immediately for stream %s\n",
553 s->name);
554 _cx18_stream_load_fw_queue(s);
555 return;
556 }
557 order->s = s;
558 order->buf = NULL; /* Indicates to load the fw queue */
559 queue_work(cx->out_work_queue, &order->work);
560}
561
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300562int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
563{
564 u32 data[MAX_MB_ARGUMENTS];
565 struct cx18 *cx = s->cx;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300566 struct cx18_buffer *buf;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300567 int captype = 0;
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300568 struct cx18_api_func_private priv;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300569
Andy Walls3d059132009-01-10 21:54:39 -0300570 if (s->video_dev == NULL && s->dvb.enabled == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300571 return -EINVAL;
572
573 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
574
575 switch (s->type) {
576 case CX18_ENC_STREAM_TYPE_MPG:
577 captype = CAPTURE_CHANNEL_TYPE_MPEG;
578 cx->mpg_data_received = cx->vbi_data_inserted = 0;
579 cx->dualwatch_jiffies = jiffies;
580 cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
581 cx->search_pack_header = 0;
582 break;
583
584 case CX18_ENC_STREAM_TYPE_TS:
585 captype = CAPTURE_CHANNEL_TYPE_TS;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300586 break;
587 case CX18_ENC_STREAM_TYPE_YUV:
588 captype = CAPTURE_CHANNEL_TYPE_YUV;
589 break;
590 case CX18_ENC_STREAM_TYPE_PCM:
591 captype = CAPTURE_CHANNEL_TYPE_PCM;
592 break;
593 case CX18_ENC_STREAM_TYPE_VBI:
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300594#ifdef CX18_ENCODER_PARSES_SLICED
Andy Wallsdd073432008-12-12 16:24:04 -0300595 captype = cx18_raw_vbi(cx) ?
596 CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300597#else
598 /*
599 * Currently we set things up so that Sliced VBI from the
600 * digitizer is handled as Raw VBI by the encoder
601 */
602 captype = CAPTURE_CHANNEL_TYPE_VBI;
603#endif
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300604 cx->vbi.frame = 0;
605 cx->vbi.inserted_frame = 0;
606 memset(cx->vbi.sliced_mpeg_size,
607 0, sizeof(cx->vbi.sliced_mpeg_size));
608 break;
609 default:
610 return -EINVAL;
611 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300612
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300613 /* Clear Streamoff flags in case left from last capture */
614 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
615
616 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
617 s->handle = data[0];
618 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
619
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300620 /*
621 * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
622 * set up all the parameters, as it is not obvious which parameters the
623 * firmware shares across capture channel types and which it does not.
624 *
625 * Some of the cx18_vapi() calls below apply to only certain capture
626 * channel types. We're hoping there's no harm in calling most of them
627 * anyway, as long as the values are all consistent. Setting some
628 * shared parameters will have no effect once an analog capture channel
629 * has started streaming.
630 */
631 if (captype != CAPTURE_CHANNEL_TYPE_TS) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300632 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
633 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
634 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
635 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300636
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300637 /*
638 * Audio related reset according to
639 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
640 */
641 if (atomic_read(&cx->ana_capturing) == 0)
642 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
643 s->handle, 12);
644
645 /*
646 * Number of lines for Field 1 & Field 2 according to
647 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
Andy Wallsf37aa512009-02-07 01:15:44 -0300648 * Field 1 is 312 for 625 line systems in BT.656
649 * Field 2 is 313 for 625 line systems in BT.656
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300650 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300651 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
Andy Wallsf37aa512009-02-07 01:15:44 -0300652 s->handle, 312, 313);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300653
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300654 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
655 cx18_vbi_setup(s);
656
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300657 /*
658 * assign program index info.
659 * Mask 7: select I/P/B, Num_req: 400 max
660 * FIXME - currently we have this hardcoded as disabled
661 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300662 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
663
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300664 /* Call out to the common CX2341x API setup for user controls */
Andy Walls50b86ba2008-11-23 19:16:44 -0300665 priv.cx = cx;
666 priv.s = s;
667 cx2341x_update(&priv, cx18_api_func, NULL, &cx->params);
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300668
669 /*
670 * When starting a capture and we're set for radio,
671 * ensure the video is muted, despite the user control.
672 */
673 if (!cx->params.video_mute &&
674 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
675 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
676 (cx->params.video_mute_yuv << 8) | 1);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300677 }
678
Hans Verkuil31554ae2008-05-25 11:21:27 -0300679 if (atomic_read(&cx->tot_capturing) == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300680 clear_bit(CX18_F_I_EOS, &cx->i_flags);
Andy Wallsb1526422008-08-30 16:03:44 -0300681 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300682 }
683
684 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300685 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
686 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300687
Andy Walls66c2a6b2008-12-08 23:02:45 -0300688 /* Init all the cpu_mdls for this stream */
689 cx18_flush_queues(s);
Andy Walls40c55202009-04-13 23:08:00 -0300690 spin_lock(&s->q_free.lock);
Andy Wallsf6b181a2008-12-14 21:05:36 -0300691 list_for_each_entry(buf, &s->q_free.list, list) {
Andy Wallsb1526422008-08-30 16:03:44 -0300692 cx18_writel(cx, buf->dma_handle,
693 &cx->scb->cpu_mdl[buf->id].paddr);
694 cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300695 }
Andy Walls40c55202009-04-13 23:08:00 -0300696 spin_unlock(&s->q_free.lock);
Andy Walls87116152009-04-13 22:42:43 -0300697 _cx18_stream_load_fw_queue(s);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300698
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300699 /* begin_capture */
700 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
701 CX18_DEBUG_WARN("Error starting capture!\n");
Andy Walls3b5df8e2008-08-23 18:36:50 -0300702 /* Ensure we're really not capturing before releasing MDLs */
Andy Walls87116152009-04-13 22:42:43 -0300703 set_bit(CX18_F_S_STOPPING, &s->s_flags);
Andy Walls3b5df8e2008-08-23 18:36:50 -0300704 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
705 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
706 else
707 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300708 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
709 /* FIXME - CX18_F_S_STREAMOFF as well? */
Andy Walls3b5df8e2008-08-23 18:36:50 -0300710 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300711 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300712 s->handle = CX18_INVALID_TASK_HANDLE;
Andy Walls87116152009-04-13 22:42:43 -0300713 clear_bit(CX18_F_S_STOPPING, &s->s_flags);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300714 if (atomic_read(&cx->tot_capturing) == 0) {
715 set_bit(CX18_F_I_EOS, &cx->i_flags);
716 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
717 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300718 return -EINVAL;
719 }
720
721 /* you're live! sit back and await interrupts :) */
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300722 if (captype != CAPTURE_CHANNEL_TYPE_TS)
Hans Verkuil31554ae2008-05-25 11:21:27 -0300723 atomic_inc(&cx->ana_capturing);
724 atomic_inc(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300725 return 0;
726}
727
728void cx18_stop_all_captures(struct cx18 *cx)
729{
730 int i;
731
732 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
733 struct cx18_stream *s = &cx->streams[i];
734
Andy Walls3d059132009-01-10 21:54:39 -0300735 if (s->video_dev == NULL && s->dvb.enabled == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300736 continue;
737 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
738 cx18_stop_v4l2_encode_stream(s, 0);
739 }
740}
741
742int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
743{
744 struct cx18 *cx = s->cx;
745 unsigned long then;
746
Andy Walls3d059132009-01-10 21:54:39 -0300747 if (s->video_dev == NULL && s->dvb.enabled == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300748 return -EINVAL;
749
750 /* This function assumes that you are allowed to stop the capture
751 and that we are actually capturing */
752
753 CX18_DEBUG_INFO("Stop Capture\n");
754
Hans Verkuil31554ae2008-05-25 11:21:27 -0300755 if (atomic_read(&cx->tot_capturing) == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300756 return 0;
757
Andy Walls87116152009-04-13 22:42:43 -0300758 set_bit(CX18_F_S_STOPPING, &s->s_flags);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300759 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
760 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
761 else
762 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
763
764 then = jiffies;
765
766 if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
767 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
768 }
769
Hans Verkuil31554ae2008-05-25 11:21:27 -0300770 if (s->type != CX18_ENC_STREAM_TYPE_TS)
771 atomic_dec(&cx->ana_capturing);
772 atomic_dec(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300773
774 /* Clear capture and no-read bits */
775 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
776
Andy Wallsf68d0cf2008-11-05 21:19:15 -0300777 /* Tell the CX23418 it can't use our buffers anymore */
778 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
779
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300780 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Wallsd3c5e702008-08-23 16:42:29 -0300781 s->handle = CX18_INVALID_TASK_HANDLE;
Andy Walls87116152009-04-13 22:42:43 -0300782 clear_bit(CX18_F_S_STOPPING, &s->s_flags);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300783
Hans Verkuil31554ae2008-05-25 11:21:27 -0300784 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300785 return 0;
786
Andy Wallsb1526422008-08-30 16:03:44 -0300787 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300788 wake_up(&s->waitq);
789
790 return 0;
791}
792
793u32 cx18_find_handle(struct cx18 *cx)
794{
795 int i;
796
797 /* find first available handle to be used for global settings */
798 for (i = 0; i < CX18_MAX_STREAMS; i++) {
799 struct cx18_stream *s = &cx->streams[i];
800
Andy Walls3d059132009-01-10 21:54:39 -0300801 if (s->video_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300802 return s->handle;
803 }
Andy Wallsd3c5e702008-08-23 16:42:29 -0300804 return CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300805}
Andy Wallsee2d64f2008-11-16 01:38:19 -0300806
807struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
808{
809 int i;
810 struct cx18_stream *s;
811
812 if (handle == CX18_INVALID_TASK_HANDLE)
813 return NULL;
814
815 for (i = 0; i < CX18_MAX_STREAMS; i++) {
816 s = &cx->streams[i];
817 if (s->handle != handle)
818 continue;
Andy Walls3d059132009-01-10 21:54:39 -0300819 if (s->video_dev || s->dvb.enabled)
Andy Wallsee2d64f2008-11-16 01:38:19 -0300820 return s;
821 }
822 return NULL;
823}