blob: 6b0b7f751f2014d6270fca703b043009e6058358 [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"
35#include "cx18-av-core.h"
36#include "cx18-dvb.h"
37
38#define CX18_DSP0_INTERRUPT_MASK 0xd0004C
39
40static struct file_operations cx18_v4l2_enc_fops = {
Hans Verkuildaf20d92008-05-12 11:21:58 -030041 .owner = THIS_MODULE,
42 .read = cx18_v4l2_read,
43 .open = cx18_v4l2_open,
Andy Walls3b6fe582008-06-21 08:36:31 -030044 /* FIXME change to video_ioctl2 if serialization lock can be removed */
Hans Verkuildaf20d92008-05-12 11:21:58 -030045 .ioctl = cx18_v4l2_ioctl,
46 .compat_ioctl = v4l_compat_ioctl32,
47 .release = cx18_v4l2_close,
48 .poll = cx18_v4l2_enc_poll,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030049};
50
51/* offset from 0 to register ts v4l2 minors on */
52#define CX18_V4L2_ENC_TS_OFFSET 16
53/* offset from 0 to register pcm v4l2 minors on */
54#define CX18_V4L2_ENC_PCM_OFFSET 24
55/* offset from 0 to register yuv v4l2 minors on */
56#define CX18_V4L2_ENC_YUV_OFFSET 32
57
58static struct {
59 const char *name;
60 int vfl_type;
Hans Verkuildd896012008-10-04 08:36:54 -030061 int num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030062 int dma;
63 enum v4l2_buf_type buf_type;
64 struct file_operations *fops;
65} cx18_stream_info[] = {
66 { /* CX18_ENC_STREAM_TYPE_MPG */
67 "encoder MPEG",
68 VFL_TYPE_GRABBER, 0,
69 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
70 &cx18_v4l2_enc_fops
71 },
72 { /* CX18_ENC_STREAM_TYPE_TS */
73 "TS",
74 VFL_TYPE_GRABBER, -1,
75 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
76 &cx18_v4l2_enc_fops
77 },
78 { /* CX18_ENC_STREAM_TYPE_YUV */
79 "encoder YUV",
80 VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
81 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
82 &cx18_v4l2_enc_fops
83 },
84 { /* CX18_ENC_STREAM_TYPE_VBI */
85 "encoder VBI",
86 VFL_TYPE_VBI, 0,
87 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
88 &cx18_v4l2_enc_fops
89 },
90 { /* CX18_ENC_STREAM_TYPE_PCM */
91 "encoder PCM audio",
92 VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
93 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
94 &cx18_v4l2_enc_fops
95 },
96 { /* CX18_ENC_STREAM_TYPE_IDX */
97 "encoder IDX",
98 VFL_TYPE_GRABBER, -1,
99 PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
100 &cx18_v4l2_enc_fops
101 },
102 { /* CX18_ENC_STREAM_TYPE_RAD */
103 "encoder radio",
104 VFL_TYPE_RADIO, 0,
105 PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
106 &cx18_v4l2_enc_fops
107 },
108};
109
110static void cx18_stream_init(struct cx18 *cx, int type)
111{
112 struct cx18_stream *s = &cx->streams[type];
113 struct video_device *dev = s->v4l2dev;
114 u32 max_size = cx->options.megabytes[type] * 1024 * 1024;
115
116 /* we need to keep v4l2dev, so restore it afterwards */
117 memset(s, 0, sizeof(*s));
118 s->v4l2dev = dev;
119
120 /* initialize cx18_stream fields */
121 s->cx = cx;
122 s->type = type;
123 s->name = cx18_stream_info[type].name;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300124 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300125
126 s->dma = cx18_stream_info[type].dma;
127 s->buf_size = cx->stream_buf_size[type];
128 if (s->buf_size)
129 s->buffers = max_size / s->buf_size;
Andy Wallsf576cee2008-11-16 20:18:05 -0300130 mutex_init(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300131 init_waitqueue_head(&s->waitq);
132 s->id = -1;
133 cx18_queue_init(&s->q_free);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300134 cx18_queue_init(&s->q_busy);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300135 cx18_queue_init(&s->q_full);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300136}
137
138static int cx18_prep_dev(struct cx18 *cx, int type)
139{
140 struct cx18_stream *s = &cx->streams[type];
141 u32 cap = cx->v4l2_cap;
Hans Verkuildd896012008-10-04 08:36:54 -0300142 int num_offset = cx18_stream_info[type].num_offset;
143 int num = cx->num + cx18_first_minor + num_offset;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300144
145 /* These four fields are always initialized. If v4l2dev == NULL, then
146 this stream is not in use. In that case no other fields but these
147 four can be used. */
148 s->v4l2dev = NULL;
149 s->cx = cx;
150 s->type = type;
151 s->name = cx18_stream_info[type].name;
152
153 /* Check whether the radio is supported */
154 if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
155 return 0;
156
157 /* Check whether VBI is supported */
158 if (type == CX18_ENC_STREAM_TYPE_VBI &&
159 !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
160 return 0;
161
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300162 /* User explicitly selected 0 buffers for these streams, so don't
163 create them. */
164 if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
165 cx->options.megabytes[type] == 0) {
166 CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
167 return 0;
168 }
169
170 cx18_stream_init(cx, type);
171
Hans Verkuildd896012008-10-04 08:36:54 -0300172 if (num_offset == -1)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300173 return 0;
174
175 /* allocate and initialize the v4l2 video device structure */
176 s->v4l2dev = video_device_alloc();
177 if (s->v4l2dev == NULL) {
178 CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
179 s->name);
180 return -ENOMEM;
181 }
182
Hans Verkuil37f89f92008-06-22 11:57:31 -0300183 snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
184 cx->num);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300185
Hans Verkuildd896012008-10-04 08:36:54 -0300186 s->v4l2dev->num = num;
Hans Verkuil5e85e732008-07-20 06:31:39 -0300187 s->v4l2dev->parent = &cx->dev->dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300188 s->v4l2dev->fops = cx18_stream_info[type].fops;
189 s->v4l2dev->release = video_device_release;
Andy Walls3b6fe582008-06-21 08:36:31 -0300190 s->v4l2dev->tvnorms = V4L2_STD_ALL;
191 cx18_set_funcs(s->v4l2dev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300192 return 0;
193}
194
195/* Initialize v4l2 variables and register v4l2 devices */
196int cx18_streams_setup(struct cx18 *cx)
197{
Andy Walls9b4a7c82008-10-18 10:20:25 -0300198 int type, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300199
200 /* Setup V4L2 Devices */
201 for (type = 0; type < CX18_MAX_STREAMS; type++) {
202 /* Prepare device */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300203 ret = cx18_prep_dev(cx, type);
204 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300205 break;
206
207 /* Allocate Stream */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300208 ret = cx18_stream_alloc(&cx->streams[type]);
209 if (ret < 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300210 break;
211 }
212 if (type == CX18_MAX_STREAMS)
213 return 0;
214
215 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300216 cx18_streams_cleanup(cx, 0);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300217 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300218}
219
220static int cx18_reg_dev(struct cx18 *cx, int type)
221{
222 struct cx18_stream *s = &cx->streams[type];
223 int vfl_type = cx18_stream_info[type].vfl_type;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300224 int num, ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300225
226 /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
227 * We need a VFL_TYPE_TS defined.
228 */
229 if (strcmp("TS", s->name) == 0) {
230 /* just return if no DVB is supported */
231 if ((cx->card->hw_all & CX18_HW_DVB) == 0)
232 return 0;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300233 ret = cx18_dvb_register(s);
234 if (ret < 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300235 CX18_ERR("DVB failed to register\n");
Andy Walls9b4a7c82008-10-18 10:20:25 -0300236 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300237 }
238 }
239
240 if (s->v4l2dev == NULL)
241 return 0;
242
Hans Verkuildd896012008-10-04 08:36:54 -0300243 num = s->v4l2dev->num;
244 /* card number + user defined offset + device offset */
245 if (type != CX18_ENC_STREAM_TYPE_MPG) {
246 struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
247
248 if (s_mpg->v4l2dev)
249 num = s_mpg->v4l2dev->num + cx18_stream_info[type].num_offset;
250 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300251
252 /* Register device. First try the desired minor, then any free one. */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300253 ret = video_register_device(s->v4l2dev, vfl_type, num);
254 if (ret < 0) {
Hans Verkuildd896012008-10-04 08:36:54 -0300255 CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
256 s->name, num);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300257 video_device_release(s->v4l2dev);
258 s->v4l2dev = NULL;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300259 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300260 }
Hans Verkuildd896012008-10-04 08:36:54 -0300261 num = s->v4l2dev->num;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300262
263 switch (vfl_type) {
264 case VFL_TYPE_GRABBER:
265 CX18_INFO("Registered device video%d for %s (%d MB)\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300266 num, s->name, cx->options.megabytes[type]);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300267 break;
268
269 case VFL_TYPE_RADIO:
270 CX18_INFO("Registered device radio%d for %s\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300271 num, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300272 break;
273
274 case VFL_TYPE_VBI:
275 if (cx->options.megabytes[type])
276 CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300277 num,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300278 s->name, cx->options.megabytes[type]);
279 else
280 CX18_INFO("Registered device vbi%d for %s\n",
Hans Verkuildd896012008-10-04 08:36:54 -0300281 num, s->name);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300282 break;
283 }
284
285 return 0;
286}
287
288/* Register v4l2 devices */
289int cx18_streams_register(struct cx18 *cx)
290{
291 int type;
Andy Walls9b4a7c82008-10-18 10:20:25 -0300292 int err;
293 int ret = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300294
295 /* Register V4L2 devices */
Andy Walls9b4a7c82008-10-18 10:20:25 -0300296 for (type = 0; type < CX18_MAX_STREAMS; type++) {
297 err = cx18_reg_dev(cx, type);
298 if (err && ret == 0)
299 ret = err;
300 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300301
Andy Walls9b4a7c82008-10-18 10:20:25 -0300302 if (ret == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300303 return 0;
304
305 /* One or more streams could not be initialized. Clean 'em all up. */
Hans Verkuil3f983872008-05-01 10:31:12 -0300306 cx18_streams_cleanup(cx, 1);
Andy Walls9b4a7c82008-10-18 10:20:25 -0300307 return ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300308}
309
310/* Unregister v4l2 devices */
Hans Verkuil3f983872008-05-01 10:31:12 -0300311void cx18_streams_cleanup(struct cx18 *cx, int unregister)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300312{
313 struct video_device *vdev;
314 int type;
315
316 /* Teardown all streams */
317 for (type = 0; type < CX18_MAX_STREAMS; type++) {
Hans Verkuilfac36392008-07-18 10:07:10 -0300318 if (cx->streams[type].dvb.enabled) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300319 cx18_dvb_unregister(&cx->streams[type]);
Hans Verkuilfac36392008-07-18 10:07:10 -0300320 cx->streams[type].dvb.enabled = false;
321 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300322
323 vdev = cx->streams[type].v4l2dev;
324
325 cx->streams[type].v4l2dev = NULL;
326 if (vdev == NULL)
327 continue;
328
329 cx18_stream_free(&cx->streams[type]);
330
Hans Verkuil3f983872008-05-01 10:31:12 -0300331 /* Unregister or release device */
332 if (unregister)
333 video_unregister_device(vdev);
334 else
335 video_device_release(vdev);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300336 }
337}
338
339static void cx18_vbi_setup(struct cx18_stream *s)
340{
341 struct cx18 *cx = s->cx;
342 int raw = cx->vbi.sliced_in->service_set == 0;
343 u32 data[CX2341X_MBOX_MAX_DATA];
344 int lines;
345
346 if (cx->is_60hz) {
347 cx->vbi.count = 12;
348 cx->vbi.start[0] = 10;
349 cx->vbi.start[1] = 273;
350 } else { /* PAL/SECAM */
351 cx->vbi.count = 18;
352 cx->vbi.start[0] = 6;
353 cx->vbi.start[1] = 318;
354 }
355
356 /* setup VBI registers */
357 cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
358
359 /* determine number of lines and total number of VBI bytes.
360 A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
361 The '- 1' byte is probably an unused U or V byte. Or something...
362 A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
363 header, 42 data bytes + checksum (to be confirmed) */
364 if (raw) {
365 lines = cx->vbi.count * 2;
366 } else {
367 lines = cx->is_60hz ? 24 : 38;
368 if (cx->is_60hz)
369 lines += 2;
370 }
371
372 cx->vbi.enc_size = lines *
373 (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
374
375 data[0] = s->handle;
376 /* Lines per field */
377 data[1] = (lines / 2) | ((lines / 2) << 16);
378 /* bytes per line */
379 data[2] = (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
380 /* Every X number of frames a VBI interrupt arrives
381 (frames as in 25 or 30 fps) */
382 data[3] = 1;
383 /* Setup VBI for the cx25840 digitizer */
384 if (raw) {
385 data[4] = 0x20602060;
386 data[5] = 0x30703070;
387 } else {
388 data[4] = 0xB0F0B0F0;
389 data[5] = 0xA0E0A0E0;
390 }
391
392 CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
393 data[0], data[1], data[2], data[3], data[4], data[5]);
394
395 if (s->type == CX18_ENC_STREAM_TYPE_VBI)
396 cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
397}
398
Andy Walls66c2a6b2008-12-08 23:02:45 -0300399struct cx18_queue *cx18_stream_put_buf_fw(struct cx18_stream *s,
400 struct cx18_buffer *buf)
401{
402 struct cx18 *cx = s->cx;
403 struct cx18_queue *q;
404
405 /* Don't give it to the firmware, if we're not running a capture */
406 if (s->handle == CX18_INVALID_TASK_HANDLE ||
407 !test_bit(CX18_F_S_STREAMING, &s->s_flags))
408 return cx18_enqueue(s, buf, &s->q_free);
409
410 q = cx18_enqueue(s, buf, &s->q_busy);
411 if (q != &s->q_busy)
412 return q; /* The firmware has the max buffers it can handle */
413
414 cx18_buf_sync_for_device(s, buf);
415 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
416 (void __iomem *) &cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
417 1, buf->id, s->buf_size);
418 return q;
419}
420
421/* Must hold s->qlock when calling */
422void cx18_stream_load_fw_queue_nolock(struct cx18_stream *s)
423{
424 struct cx18_buffer *buf;
425 struct cx18 *cx = s->cx;
426
427 /* Move from q_free to q_busy notifying the firmware: 63 buf limit */
428 while (s->handle != CX18_INVALID_TASK_HANDLE &&
429 test_bit(CX18_F_S_STREAMING, &s->s_flags) &&
430 atomic_read(&s->q_busy.buffers) < 63 &&
431 !list_empty(&s->q_free.list)) {
432
433 /* Move from q_free to q_busy */
434 buf = list_entry(s->q_free.list.next, struct cx18_buffer, list);
435 list_move_tail(&buf->list, &s->q_busy.list);
436 buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
437 atomic_dec(&s->q_free.buffers);
438 atomic_inc(&s->q_busy.buffers);
439
440 /* Notify firmware */
441 cx18_buf_sync_for_device(s, buf);
442 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
443 (void __iomem *) &cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
444 1, buf->id, s->buf_size);
445 }
446}
447
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300448int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
449{
450 u32 data[MAX_MB_ARGUMENTS];
451 struct cx18 *cx = s->cx;
452 struct list_head *p;
Andy Walls66c2a6b2008-12-08 23:02:45 -0300453 struct cx18_buffer *buf;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300454 int ts = 0;
455 int captype = 0;
456
457 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
458 return -EINVAL;
459
460 CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
461
462 switch (s->type) {
463 case CX18_ENC_STREAM_TYPE_MPG:
464 captype = CAPTURE_CHANNEL_TYPE_MPEG;
465 cx->mpg_data_received = cx->vbi_data_inserted = 0;
466 cx->dualwatch_jiffies = jiffies;
467 cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
468 cx->search_pack_header = 0;
469 break;
470
471 case CX18_ENC_STREAM_TYPE_TS:
472 captype = CAPTURE_CHANNEL_TYPE_TS;
473 ts = 1;
474 break;
475 case CX18_ENC_STREAM_TYPE_YUV:
476 captype = CAPTURE_CHANNEL_TYPE_YUV;
477 break;
478 case CX18_ENC_STREAM_TYPE_PCM:
479 captype = CAPTURE_CHANNEL_TYPE_PCM;
480 break;
481 case CX18_ENC_STREAM_TYPE_VBI:
482 captype = cx->vbi.sliced_in->service_set ?
483 CAPTURE_CHANNEL_TYPE_SLICED_VBI : CAPTURE_CHANNEL_TYPE_VBI;
484 cx->vbi.frame = 0;
485 cx->vbi.inserted_frame = 0;
486 memset(cx->vbi.sliced_mpeg_size,
487 0, sizeof(cx->vbi.sliced_mpeg_size));
488 break;
489 default:
490 return -EINVAL;
491 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300492
493 /* mute/unmute video */
494 cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
495 s->handle, !!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags));
496
497 /* Clear Streamoff flags in case left from last capture */
498 clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
499
500 cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
501 s->handle = data[0];
502 cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
503
Hans Verkuil31554ae2008-05-25 11:21:27 -0300504 if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300505 /* Stuff from Windows, we don't know what it is */
506 cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
507 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
508 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
509 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
510 cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, s->handle, 12);
511
512 cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
513 s->handle, cx->digitizer, cx->digitizer);
514
515 /* Setup VBI */
516 if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
517 cx18_vbi_setup(s);
518
519 /* assign program index info.
520 Mask 7: select I/P/B, Num_req: 400 max */
521 cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
522
523 /* Setup API for Stream */
524 cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
525 }
526
Hans Verkuil31554ae2008-05-25 11:21:27 -0300527 if (atomic_read(&cx->tot_capturing) == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300528 clear_bit(CX18_F_I_EOS, &cx->i_flags);
Andy Wallsb1526422008-08-30 16:03:44 -0300529 cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300530 }
531
532 cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
Al Viro990c81c2008-05-21 00:32:01 -0300533 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
534 (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300535
Andy Walls66c2a6b2008-12-08 23:02:45 -0300536 /* Init all the cpu_mdls for this stream */
537 cx18_flush_queues(s);
538 mutex_lock(&s->qlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300539 list_for_each(p, &s->q_free.list) {
Andy Walls66c2a6b2008-12-08 23:02:45 -0300540 buf = list_entry(p, struct cx18_buffer, list);
Andy Wallsb1526422008-08-30 16:03:44 -0300541 cx18_writel(cx, buf->dma_handle,
542 &cx->scb->cpu_mdl[buf->id].paddr);
543 cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300544 }
Andy Walls66c2a6b2008-12-08 23:02:45 -0300545 cx18_stream_load_fw_queue_nolock(s);
546 mutex_unlock(&s->qlock);
547
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300548 /* begin_capture */
549 if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
550 CX18_DEBUG_WARN("Error starting capture!\n");
Andy Walls3b5df8e2008-08-23 18:36:50 -0300551 /* Ensure we're really not capturing before releasing MDLs */
552 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
553 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
554 else
555 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300556 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
557 /* FIXME - CX18_F_S_STREAMOFF as well? */
Andy Walls3b5df8e2008-08-23 18:36:50 -0300558 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300559 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Walls66c2a6b2008-12-08 23:02:45 -0300560 s->handle = CX18_INVALID_TASK_HANDLE;
561 if (atomic_read(&cx->tot_capturing) == 0) {
562 set_bit(CX18_F_I_EOS, &cx->i_flags);
563 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
564 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300565 return -EINVAL;
566 }
567
568 /* you're live! sit back and await interrupts :) */
Hans Verkuil31554ae2008-05-25 11:21:27 -0300569 if (!ts)
570 atomic_inc(&cx->ana_capturing);
571 atomic_inc(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300572 return 0;
573}
574
575void cx18_stop_all_captures(struct cx18 *cx)
576{
577 int i;
578
579 for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
580 struct cx18_stream *s = &cx->streams[i];
581
582 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
583 continue;
584 if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
585 cx18_stop_v4l2_encode_stream(s, 0);
586 }
587}
588
589int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
590{
591 struct cx18 *cx = s->cx;
592 unsigned long then;
593
594 if (s->v4l2dev == NULL && s->dvb.enabled == 0)
595 return -EINVAL;
596
597 /* This function assumes that you are allowed to stop the capture
598 and that we are actually capturing */
599
600 CX18_DEBUG_INFO("Stop Capture\n");
601
Hans Verkuil31554ae2008-05-25 11:21:27 -0300602 if (atomic_read(&cx->tot_capturing) == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300603 return 0;
604
605 if (s->type == CX18_ENC_STREAM_TYPE_MPG)
606 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
607 else
608 cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
609
610 then = jiffies;
611
612 if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
613 CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
614 }
615
Hans Verkuil31554ae2008-05-25 11:21:27 -0300616 if (s->type != CX18_ENC_STREAM_TYPE_TS)
617 atomic_dec(&cx->ana_capturing);
618 atomic_dec(&cx->tot_capturing);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300619
620 /* Clear capture and no-read bits */
621 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
622
Andy Wallsf68d0cf2008-11-05 21:19:15 -0300623 /* Tell the CX23418 it can't use our buffers anymore */
624 cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
625
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300626 cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
Andy Wallsd3c5e702008-08-23 16:42:29 -0300627 s->handle = CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300628
Hans Verkuil31554ae2008-05-25 11:21:27 -0300629 if (atomic_read(&cx->tot_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300630 return 0;
631
Andy Wallsb1526422008-08-30 16:03:44 -0300632 cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300633 wake_up(&s->waitq);
634
635 return 0;
636}
637
638u32 cx18_find_handle(struct cx18 *cx)
639{
640 int i;
641
642 /* find first available handle to be used for global settings */
643 for (i = 0; i < CX18_MAX_STREAMS; i++) {
644 struct cx18_stream *s = &cx->streams[i];
645
Andy Wallsd3c5e702008-08-23 16:42:29 -0300646 if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300647 return s->handle;
648 }
Andy Wallsd3c5e702008-08-23 16:42:29 -0300649 return CX18_INVALID_TASK_HANDLE;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300650}
Andy Wallsee2d64f2008-11-16 01:38:19 -0300651
652struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
653{
654 int i;
655 struct cx18_stream *s;
656
657 if (handle == CX18_INVALID_TASK_HANDLE)
658 return NULL;
659
660 for (i = 0; i < CX18_MAX_STREAMS; i++) {
661 s = &cx->streams[i];
662 if (s->handle != handle)
663 continue;
664 if (s->v4l2dev || s->dvb.enabled)
665 return s;
666 }
667 return NULL;
668}