cx18-streams.c 29.0 KB
Newer Older
1 2 3 4 5 6
/*
 *  cx18 init/start/stop/exit stream functions
 *
 *  Derived from ivtv-streams.c
 *
 *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
7
 *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA
 */

#include "cx18-driver.h"
26
#include "cx18-io.h"
27 28 29 30 31 32 33 34 35 36 37 38
#include "cx18-fileops.h"
#include "cx18-mailbox.h"
#include "cx18-i2c.h"
#include "cx18-queue.h"
#include "cx18-ioctl.h"
#include "cx18-streams.h"
#include "cx18-cards.h"
#include "cx18-scb.h"
#include "cx18-dvb.h"

#define CX18_DSP0_INTERRUPT_MASK     	0xd0004C

39
static struct v4l2_file_operations cx18_v4l2_enc_fops = {
40 41 42
	.owner = THIS_MODULE,
	.read = cx18_v4l2_read,
	.open = cx18_v4l2_open,
43
	.unlocked_ioctl = video_ioctl2,
44 45
	.release = cx18_v4l2_close,
	.poll = cx18_v4l2_enc_poll,
46
	.mmap = cx18_v4l2_mmap,
47 48 49 50 51 52 53 54 55 56 57 58
};

/* offset from 0 to register ts v4l2 minors on */
#define CX18_V4L2_ENC_TS_OFFSET   16
/* offset from 0 to register pcm v4l2 minors on */
#define CX18_V4L2_ENC_PCM_OFFSET  24
/* offset from 0 to register yuv v4l2 minors on */
#define CX18_V4L2_ENC_YUV_OFFSET  32

static struct {
	const char *name;
	int vfl_type;
59
	int num_offset;
60 61 62 63 64
	int dma;
} cx18_stream_info[] = {
	{	/* CX18_ENC_STREAM_TYPE_MPG */
		"encoder MPEG",
		VFL_TYPE_GRABBER, 0,
65
		PCI_DMA_FROMDEVICE,
66 67 68 69
	},
	{	/* CX18_ENC_STREAM_TYPE_TS */
		"TS",
		VFL_TYPE_GRABBER, -1,
70
		PCI_DMA_FROMDEVICE,
71 72 73 74
	},
	{	/* CX18_ENC_STREAM_TYPE_YUV */
		"encoder YUV",
		VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
75
		PCI_DMA_FROMDEVICE,
76 77 78 79
	},
	{	/* CX18_ENC_STREAM_TYPE_VBI */
		"encoder VBI",
		VFL_TYPE_VBI, 0,
80
		PCI_DMA_FROMDEVICE,
81 82 83 84
	},
	{	/* CX18_ENC_STREAM_TYPE_PCM */
		"encoder PCM audio",
		VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
85
		PCI_DMA_FROMDEVICE,
86 87 88 89
	},
	{	/* CX18_ENC_STREAM_TYPE_IDX */
		"encoder IDX",
		VFL_TYPE_GRABBER, -1,
90
		PCI_DMA_FROMDEVICE,
91 92 93 94
	},
	{	/* CX18_ENC_STREAM_TYPE_RAD */
		"encoder radio",
		VFL_TYPE_RADIO, 0,
95
		PCI_DMA_NONE,
96 97 98
	},
};

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

void cx18_dma_free(struct videobuf_queue *q,
	struct cx18_stream *s, struct cx18_videobuf_buffer *buf)
{
	videobuf_waiton(q, &buf->vb, 0, 0);
	videobuf_vmalloc_free(&buf->vb);
	buf->vb.state = VIDEOBUF_NEEDS_INIT;
}

static int cx18_prepare_buffer(struct videobuf_queue *q,
	struct cx18_stream *s,
	struct cx18_videobuf_buffer *buf,
	u32 pixelformat,
	unsigned int width, unsigned int height,
	enum v4l2_field field)
{
        struct cx18 *cx = s->cx;
	int rc = 0;

	/* check settings */
	buf->bytes_used = 0;

	if ((width  < 48) || (height < 32))
		return -EINVAL;

	buf->vb.size = (width * height * 2);
	if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
		return -EINVAL;

	/* alloc + fill struct (if changed) */
	if (buf->vb.width != width || buf->vb.height != height ||
	    buf->vb.field != field || s->pixelformat != pixelformat ||
	    buf->tvnorm != cx->std) {

		buf->vb.width  = width;
		buf->vb.height = height;
		buf->vb.field  = field;
		buf->tvnorm    = cx->std;
		s->pixelformat = pixelformat;

139 140 141 142 143 144
		/* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
		   UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
		if (s->pixelformat == V4L2_PIX_FMT_HM12)
			s->vb_bytes_per_frame = height * 720 * 3 / 2;
		else
			s->vb_bytes_per_frame = height * 720 * 2;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
		cx18_dma_free(q, s, buf);
	}

	if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
		return -EINVAL;

	if (buf->vb.field == 0)
		buf->vb.field = V4L2_FIELD_INTERLACED;

	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
		buf->vb.width  = width;
		buf->vb.height = height;
		buf->vb.field  = field;
		buf->tvnorm    = cx->std;
		s->pixelformat = pixelformat;

161 162 163 164 165 166
		/* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
		   UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
		if (s->pixelformat == V4L2_PIX_FMT_HM12)
			s->vb_bytes_per_frame = height * 720 * 3 / 2;
		else
			s->vb_bytes_per_frame = height * 720 * 2;
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		rc = videobuf_iolock(q, &buf->vb, NULL);
		if (rc != 0)
			goto fail;
	}
	buf->vb.state = VIDEOBUF_PREPARED;
	return 0;

fail:
	cx18_dma_free(q, s, buf);
	return rc;

}

/* VB_MIN_BUFSIZE is lcm(1440 * 480, 1440 * 576)
   1440 is a single line of 4:2:2 YUV at 720 luma samples wide
*/
#define VB_MIN_BUFFERS 32
#define VB_MIN_BUFSIZE 4147200

static int buffer_setup(struct videobuf_queue *q,
	unsigned int *count, unsigned int *size)
{
	struct cx18_stream *s = q->priv_data;
	struct cx18 *cx = s->cx;

	*size = 2 * cx->cxhdl.width * cx->cxhdl.height;
	if (*count == 0)
		*count = VB_MIN_BUFFERS;

	while (*size * *count > VB_MIN_BUFFERS * VB_MIN_BUFSIZE)
		(*count)--;

	q->field = V4L2_FIELD_INTERLACED;
	q->last = V4L2_FIELD_INTERLACED;

	return 0;
}

static int buffer_prepare(struct videobuf_queue *q,
	struct videobuf_buffer *vb,
	enum v4l2_field field)
{
	struct cx18_videobuf_buffer *buf =
		container_of(vb, struct cx18_videobuf_buffer, vb);
	struct cx18_stream *s = q->priv_data;
	struct cx18 *cx = s->cx;

	return cx18_prepare_buffer(q, s, buf, s->pixelformat,
		cx->cxhdl.width, cx->cxhdl.height, field);
}

static void buffer_release(struct videobuf_queue *q,
	struct videobuf_buffer *vb)
{
	struct cx18_videobuf_buffer *buf =
		container_of(vb, struct cx18_videobuf_buffer, vb);
	struct cx18_stream *s = q->priv_data;

	cx18_dma_free(q, s, buf);
}

static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
{
	struct cx18_videobuf_buffer *buf =
		container_of(vb, struct cx18_videobuf_buffer, vb);
	struct cx18_stream *s = q->priv_data;

	buf->vb.state = VIDEOBUF_QUEUED;

	list_add_tail(&buf->vb.queue, &s->vb_capture);
}

static struct videobuf_queue_ops cx18_videobuf_qops = {
	.buf_setup    = buffer_setup,
	.buf_prepare  = buffer_prepare,
	.buf_queue    = buffer_queue,
	.buf_release  = buffer_release,
};

246 247 248
static void cx18_stream_init(struct cx18 *cx, int type)
{
	struct cx18_stream *s = &cx->streams[type];
249
	struct video_device *video_dev = s->video_dev;
250

251
	/* we need to keep video_dev, so restore it afterwards */
252
	memset(s, 0, sizeof(*s));
253
	s->video_dev = video_dev;
254 255

	/* initialize cx18_stream fields */
256
	s->dvb = NULL;
257 258 259
	s->cx = cx;
	s->type = type;
	s->name = cx18_stream_info[type].name;
260
	s->handle = CX18_INVALID_TASK_HANDLE;
261 262

	s->dma = cx18_stream_info[type].dma;
263
	s->buffers = cx->stream_buffers[type];
264
	s->buf_size = cx->stream_buf_size[type];
265 266 267
	INIT_LIST_HEAD(&s->buf_pool);
	s->bufs_per_mdl = 1;
	s->mdl_size = s->buf_size * s->bufs_per_mdl;
268

269 270
	init_waitqueue_head(&s->waitq);
	s->id = -1;
271
	spin_lock_init(&s->q_free.lock);
272
	cx18_queue_init(&s->q_free);
273
	spin_lock_init(&s->q_busy.lock);
274
	cx18_queue_init(&s->q_busy);
275
	spin_lock_init(&s->q_full.lock);
276
	cx18_queue_init(&s->q_full);
277 278
	spin_lock_init(&s->q_idle.lock);
	cx18_queue_init(&s->q_idle);
279 280

	INIT_WORK(&s->out_work_order, cx18_out_work_handler);
281 282 283 284 285 286

	INIT_LIST_HEAD(&s->vb_capture);
	s->vb_timeout.function = cx18_vb_timeout;
	s->vb_timeout.data = (unsigned long)s;
	init_timer(&s->vb_timeout);
	spin_lock_init(&s->vb_lock);
287
	if (type == CX18_ENC_STREAM_TYPE_YUV) {
288 289
		spin_lock_init(&s->vbuf_q_lock);

290 291 292 293 294 295 296 297 298 299
		s->vb_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		videobuf_queue_vmalloc_init(&s->vbuf_q, &cx18_videobuf_qops,
			&cx->pci_dev->dev, &s->vbuf_q_lock,
			V4L2_BUF_TYPE_VIDEO_CAPTURE,
			V4L2_FIELD_INTERLACED,
			sizeof(struct cx18_videobuf_buffer),
			s, &cx->serialize_lock);

		/* Assume the previous pixel default */
		s->pixelformat = V4L2_PIX_FMT_HM12;
300
		s->vb_bytes_per_frame = cx->cxhdl.height * 720 * 3 / 2;
301
	}
302 303 304 305 306 307
}

static int cx18_prep_dev(struct cx18 *cx, int type)
{
	struct cx18_stream *s = &cx->streams[type];
	u32 cap = cx->v4l2_cap;
308
	int num_offset = cx18_stream_info[type].num_offset;
309
	int num = cx->instance + cx18_first_minor + num_offset;
310

311 312 313 314 315 316 317
	/*
	 * These five fields are always initialized.
	 * For analog capture related streams, if video_dev == NULL then the
	 * stream is not in use.
	 * For the TS stream, if dvb == NULL then the stream is not in use.
	 * In those cases no other fields but these four can be used.
	 */
318
	s->video_dev = NULL;
319
	s->dvb = NULL;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	s->cx = cx;
	s->type = type;
	s->name = cx18_stream_info[type].name;

	/* Check whether the radio is supported */
	if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
		return 0;

	/* Check whether VBI is supported */
	if (type == CX18_ENC_STREAM_TYPE_VBI &&
	    !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
		return 0;

	/* User explicitly selected 0 buffers for these streams, so don't
	   create them. */
	if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
336
	    cx->stream_buffers[type] == 0) {
337 338 339 340 341 342
		CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
		return 0;
	}

	cx18_stream_init(cx, type);

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	/* Allocate the cx18_dvb struct only for the TS on cards with DTV */
	if (type == CX18_ENC_STREAM_TYPE_TS) {
		if (cx->card->hw_all & CX18_HW_DVB) {
			s->dvb = kzalloc(sizeof(struct cx18_dvb), GFP_KERNEL);
			if (s->dvb == NULL) {
				CX18_ERR("Couldn't allocate cx18_dvb structure"
					 " for %s\n", s->name);
				return -ENOMEM;
			}
		} else {
			/* Don't need buffers for the TS, if there is no DVB */
			s->buffers = 0;
		}
	}

358
	if (num_offset == -1)
359 360 361
		return 0;

	/* allocate and initialize the v4l2 video device structure */
362 363
	s->video_dev = video_device_alloc();
	if (s->video_dev == NULL) {
364 365 366 367 368
		CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
				s->name);
		return -ENOMEM;
	}

369 370
	snprintf(s->video_dev->name, sizeof(s->video_dev->name), "%s %s",
		 cx->v4l2_dev.name, s->name);
371

372
	s->video_dev->num = num;
373
	s->video_dev->v4l2_dev = &cx->v4l2_dev;
374 375 376
	s->video_dev->fops = &cx18_v4l2_enc_fops;
	s->video_dev->release = video_device_release;
	s->video_dev->tvnorms = V4L2_STD_ALL;
377
	s->video_dev->lock = &cx->serialize_lock;
378
	set_bit(V4L2_FL_USE_FH_PRIO, &s->video_dev->flags);
379
	cx18_set_funcs(s->video_dev);
380 381 382 383 384 385
	return 0;
}

/* Initialize v4l2 variables and register v4l2 devices */
int cx18_streams_setup(struct cx18 *cx)
{
386
	int type, ret;
387 388 389 390

	/* Setup V4L2 Devices */
	for (type = 0; type < CX18_MAX_STREAMS; type++) {
		/* Prepare device */
391 392
		ret = cx18_prep_dev(cx, type);
		if (ret < 0)
393 394 395
			break;

		/* Allocate Stream */
396 397
		ret = cx18_stream_alloc(&cx->streams[type]);
		if (ret < 0)
398 399 400 401 402 403
			break;
	}
	if (type == CX18_MAX_STREAMS)
		return 0;

	/* One or more streams could not be initialized. Clean 'em all up. */
404
	cx18_streams_cleanup(cx, 0);
405
	return ret;
406 407 408 409 410 411
}

static int cx18_reg_dev(struct cx18 *cx, int type)
{
	struct cx18_stream *s = &cx->streams[type];
	int vfl_type = cx18_stream_info[type].vfl_type;
412
	const char *name;
413
	int num, ret;
414

415
	if (type == CX18_ENC_STREAM_TYPE_TS && s->dvb != NULL) {
416 417
		ret = cx18_dvb_register(s);
		if (ret < 0) {
418
			CX18_ERR("DVB failed to register\n");
419
			return ret;
420 421 422
		}
	}

423
	if (s->video_dev == NULL)
424 425
		return 0;

426
	num = s->video_dev->num;
427 428 429 430
	/* card number + user defined offset + device offset */
	if (type != CX18_ENC_STREAM_TYPE_MPG) {
		struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];

431 432 433
		if (s_mpg->video_dev)
			num = s_mpg->video_dev->num
			    + cx18_stream_info[type].num_offset;
434
	}
435
	video_set_drvdata(s->video_dev, s);
436 437

	/* Register device. First try the desired minor, then any free one. */
438
	ret = video_register_device_no_warn(s->video_dev, vfl_type, num);
439
	if (ret < 0) {
440
		CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
441
			s->name, num);
442 443
		video_device_release(s->video_dev);
		s->video_dev = NULL;
444
		return ret;
445
	}
446 447

	name = video_device_node_name(s->video_dev);
448 449 450

	switch (vfl_type) {
	case VFL_TYPE_GRABBER:
451 452
		CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n",
			  name, s->name, cx->stream_buffers[type],
453 454
			  cx->stream_buf_size[type] / 1024,
			  (cx->stream_buf_size[type] * 100 / 1024) % 100);
455 456 457
		break;

	case VFL_TYPE_RADIO:
458
		CX18_INFO("Registered device %s for %s\n", name, s->name);
459 460 461
		break;

	case VFL_TYPE_VBI:
462
		if (cx->stream_buffers[type])
463
			CX18_INFO("Registered device %s for %s "
464
				  "(%d x %d bytes)\n",
465
				  name, s->name, cx->stream_buffers[type],
466
				  cx->stream_buf_size[type]);
467
		else
468 469
			CX18_INFO("Registered device %s for %s\n",
				name, s->name);
470 471 472 473 474 475 476 477 478 479
		break;
	}

	return 0;
}

/* Register v4l2 devices */
int cx18_streams_register(struct cx18 *cx)
{
	int type;
480 481
	int err;
	int ret = 0;
482 483

	/* Register V4L2 devices */
484 485 486 487 488
	for (type = 0; type < CX18_MAX_STREAMS; type++) {
		err = cx18_reg_dev(cx, type);
		if (err && ret == 0)
			ret = err;
	}
489

490
	if (ret == 0)
491 492 493
		return 0;

	/* One or more streams could not be initialized. Clean 'em all up. */
494
	cx18_streams_cleanup(cx, 1);
495
	return ret;
496 497 498
}

/* Unregister v4l2 devices */
499
void cx18_streams_cleanup(struct cx18 *cx, int unregister)
500 501 502 503 504 505
{
	struct video_device *vdev;
	int type;

	/* Teardown all streams */
	for (type = 0; type < CX18_MAX_STREAMS; type++) {
506

507
		/* The TS has a cx18_dvb structure, not a video_device */
508
		if (type == CX18_ENC_STREAM_TYPE_TS) {
509 510 511 512 513
			if (cx->streams[type].dvb != NULL) {
				if (unregister)
					cx18_dvb_unregister(&cx->streams[type]);
				kfree(cx->streams[type].dvb);
				cx->streams[type].dvb = NULL;
514 515 516 517 518 519 520
				cx18_stream_free(&cx->streams[type]);
			}
			continue;
		}

		/* No struct video_device, but can have buffers allocated */
		if (type == CX18_ENC_STREAM_TYPE_IDX) {
521
			/* If the module params didn't inhibit IDX ... */
522 523
			if (cx->stream_buffers[type] != 0) {
				cx->stream_buffers[type] = 0;
524 525 526 527 528 529 530 531
				/*
				 * Before calling cx18_stream_free(),
				 * check if the IDX stream was actually set up.
				 * Needed, since the cx18_probe() error path
				 * exits through here as well as normal clean up
				 */
				if (cx->streams[type].buffers != 0)
					cx18_stream_free(&cx->streams[type]);
532 533
			}
			continue;
534
		}
535

536
		/* If struct video_device exists, can have buffers allocated */
537
		vdev = cx->streams[type].video_dev;
538

539
		cx->streams[type].video_dev = NULL;
540 541 542
		if (vdev == NULL)
			continue;

543 544 545
		if (type == CX18_ENC_STREAM_TYPE_YUV)
			videobuf_mmap_free(&cx->streams[type].vbuf_q);

546 547
		cx18_stream_free(&cx->streams[type]);

548 549 550 551 552
		/* Unregister or release device */
		if (unregister)
			video_unregister_device(vdev);
		else
			video_device_release(vdev);
553 554 555 556 557 558
	}
}

static void cx18_vbi_setup(struct cx18_stream *s)
{
	struct cx18 *cx = s->cx;
559
	int raw = cx18_raw_vbi(cx);
560 561 562 563 564 565 566 567 568 569 570 571 572 573
	u32 data[CX2341X_MBOX_MAX_DATA];
	int lines;

	if (cx->is_60hz) {
		cx->vbi.count = 12;
		cx->vbi.start[0] = 10;
		cx->vbi.start[1] = 273;
	} else {        /* PAL/SECAM */
		cx->vbi.count = 18;
		cx->vbi.start[0] = 6;
		cx->vbi.start[1] = 318;
	}

	/* setup VBI registers */
574 575 576 577
	if (raw)
		v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &cx->vbi.in.fmt.vbi);
	else
		v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &cx->vbi.in.fmt.sliced);
578

579 580 581 582 583 584 585 586
	/*
	 * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
	 * VBI when the first analog capture channel starts, as once it starts
	 * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
	 * (i.e. for the VBI capture channels).  We also send it for each
	 * analog capture channel anyway just to make sure we get the proper
	 * behavior
	 */
587 588 589
	if (raw) {
		lines = cx->vbi.count * 2;
	} else {
590 591 592 593 594 595 596
		/*
		 * For 525/60 systems, according to the VIP 2 & BT.656 std:
		 * The EAV RP code's Field bit toggles on line 4, a few lines
		 * after the Vertcal Blank bit has already toggled.
		 * Tell the encoder to capture 21-4+1=18 lines per field,
		 * since we want lines 10 through 21.
		 *
597 598 599
		 * For 625/50 systems, according to the VIP 2 & BT.656 std:
		 * The EAV RP code's Field bit toggles on line 1, a few lines
		 * after the Vertcal Blank bit has already toggled.
600 601 602
		 * (We've actually set the digitizer so that the Field bit
		 * toggles on line 2.) Tell the encoder to capture 23-2+1=22
		 * lines per field, since we want lines 6 through 23.
603
		 */
604
		lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
605 606 607 608 609 610
	}

	data[0] = s->handle;
	/* Lines per field */
	data[1] = (lines / 2) | ((lines / 2) << 16);
	/* bytes per line */
611 612 613
	data[2] = (raw ? vbi_active_samples
		       : (cx->is_60hz ? vbi_hblank_samples_60Hz
				      : vbi_hblank_samples_50Hz));
614 615 616
	/* Every X number of frames a VBI interrupt arrives
	   (frames as in 25 or 30 fps) */
	data[3] = 1;
617 618 619 620
	/*
	 * Set the SAV/EAV RP codes to look for as start/stop points
	 * when in VIP-1.1 mode
	 */
621
	if (raw) {
622 623 624 625 626
		/*
		 * Start codes for beginning of "active" line in vertical blank
		 * 0x20 (               VerticalBlank                )
		 * 0x60 (     EvenField VerticalBlank                )
		 */
627
		data[4] = 0x20602060;
628 629 630 631 632 633 634
		/*
		 * End codes for end of "active" raw lines and regular lines
		 * 0x30 (               VerticalBlank HorizontalBlank)
		 * 0x70 (     EvenField VerticalBlank HorizontalBlank)
		 * 0x90 (Task                         HorizontalBlank)
		 * 0xd0 (Task EvenField               HorizontalBlank)
		 */
635
		data[5] = 0x307090d0;
636
	} else {
637 638 639 640 641 642 643
		/*
		 * End codes for active video, we want data in the hblank region
		 * 0xb0 (Task         0 VerticalBlank HorizontalBlank)
		 * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
		 *
		 * Since the V bit is only allowed to toggle in the EAV RP code,
		 * just before the first active region line, these two
644
		 * are problematic:
645 646
		 * 0x90 (Task                         HorizontalBlank)
		 * 0xd0 (Task EvenField               HorizontalBlank)
647
		 *
648 649
		 * We have set the digitzer such that we don't have to worry
		 * about these problem codes.
650
		 */
651
		data[4] = 0xB0F0B0F0;
652 653 654 655 656
		/*
		 * Start codes for beginning of active line in vertical blank
		 * 0xa0 (Task           VerticalBlank                )
		 * 0xe0 (Task EvenField VerticalBlank                )
		 */
657 658 659 660 661 662
		data[5] = 0xA0E0A0E0;
	}

	CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
			data[0], data[1], data[2], data[3], data[4], data[5]);

663
	cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
664 665
}

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
void cx18_stream_rotate_idx_mdls(struct cx18 *cx)
{
	struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
	struct cx18_mdl *mdl;

	if (!cx18_stream_enabled(s))
		return;

	/* Return if the firmware is not running low on MDLs */
	if ((atomic_read(&s->q_free.depth) + atomic_read(&s->q_busy.depth)) >=
					    CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN)
		return;

	/* Return if there are no MDLs to rotate back to the firmware */
	if (atomic_read(&s->q_full.depth) < 2)
		return;

	/*
	 * Take the oldest IDX MDL still holding data, and discard its index
	 * entries by scheduling the MDL to go back to the firmware
	 */
	mdl = cx18_dequeue(s, &s->q_full);
	if (mdl != NULL)
		cx18_enqueue(s, mdl, &s->q_free);
}

692
static
693 694
struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s,
					   struct cx18_mdl *mdl)
695 696 697 698 699 700
{
	struct cx18 *cx = s->cx;
	struct cx18_queue *q;

	/* Don't give it to the firmware, if we're not running a capture */
	if (s->handle == CX18_INVALID_TASK_HANDLE ||
701
	    test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
702
	    !test_bit(CX18_F_S_STREAMING, &s->s_flags))
703
		return cx18_enqueue(s, mdl, &s->q_free);
704

705
	q = cx18_enqueue(s, mdl, &s->q_busy);
706
	if (q != &s->q_busy)
707
		return q; /* The firmware has the max MDLs it can handle */
708

709
	cx18_mdl_sync_for_device(s, mdl);
710
	cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
711 712
		  (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
		  s->bufs_per_mdl, mdl->id, s->mdl_size);
713 714 715
	return q;
}

716 717
static
void _cx18_stream_load_fw_queue(struct cx18_stream *s)
718
{
719
	struct cx18_queue *q;
720
	struct cx18_mdl *mdl;
721

722 723
	if (atomic_read(&s->q_free.depth) == 0 ||
	    atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
724 725 726 727
		return;

	/* Move from q_free to q_busy notifying the firmware, until the limit */
	do {
728 729
		mdl = cx18_dequeue(s, &s->q_free);
		if (mdl == NULL)
730
			break;
731
		q = _cx18_stream_put_mdl_fw(s, mdl);
732
	} while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM
733
		 && q == &s->q_busy);
734 735
}

736 737
void cx18_out_work_handler(struct work_struct *work)
{
738 739
	struct cx18_stream *s =
			 container_of(work, struct cx18_stream, out_work_order);
740

741
	_cx18_stream_load_fw_queue(s);
742 743
}

744 745 746 747
static void cx18_stream_configure_mdls(struct cx18_stream *s)
{
	cx18_unload_queues(s);

748 749 750 751 752 753 754
	switch (s->type) {
	case CX18_ENC_STREAM_TYPE_YUV:
		/*
		 * Height should be a multiple of 32 lines.
		 * Set the MDL size to the exact size needed for one frame.
		 * Use enough buffers per MDL to cover the MDL size
		 */
755 756 757 758
		if (s->pixelformat == V4L2_PIX_FMT_HM12)
			s->mdl_size = 720 * s->cx->cxhdl.height * 3 / 2;
		else
			s->mdl_size = 720 * s->cx->cxhdl.height * 2;
759 760 761 762
		s->bufs_per_mdl = s->mdl_size / s->buf_size;
		if (s->mdl_size % s->buf_size)
			s->bufs_per_mdl++;
		break;
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	case CX18_ENC_STREAM_TYPE_VBI:
		s->bufs_per_mdl = 1;
		if  (cx18_raw_vbi(s->cx)) {
			s->mdl_size = (s->cx->is_60hz ? 12 : 18)
						       * 2 * vbi_active_samples;
		} else {
			/*
			 * See comment in cx18_vbi_setup() below about the
			 * extra lines we capture in sliced VBI mode due to
			 * the lines on which EAV RP codes toggle.
			*/
			s->mdl_size = s->cx->is_60hz
				   ? (21 - 4 + 1) * 2 * vbi_hblank_samples_60Hz
				   : (23 - 2 + 1) * 2 * vbi_hblank_samples_50Hz;
		}
		break;
779 780 781 782 783
	default:
		s->bufs_per_mdl = 1;
		s->mdl_size = s->buf_size * s->bufs_per_mdl;
		break;
	}
784 785 786 787

	cx18_load_queues(s);
}

788 789 790 791 792
int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
{
	u32 data[MAX_MB_ARGUMENTS];
	struct cx18 *cx = s->cx;
	int captype = 0;
793
	struct cx18_stream *s_idx;
794

795
	if (!cx18_stream_enabled(s))
796 797 798 799 800 801 802 803 804
		return -EINVAL;

	CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);

	switch (s->type) {
	case CX18_ENC_STREAM_TYPE_MPG:
		captype = CAPTURE_CHANNEL_TYPE_MPEG;
		cx->mpg_data_received = cx->vbi_data_inserted = 0;
		cx->dualwatch_jiffies = jiffies;
805
		cx->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
806 807 808
		cx->search_pack_header = 0;
		break;

809 810 811
	case CX18_ENC_STREAM_TYPE_IDX:
		captype = CAPTURE_CHANNEL_TYPE_INDEX;
		break;
812 813 814 815 816 817 818 819 820 821
	case CX18_ENC_STREAM_TYPE_TS:
		captype = CAPTURE_CHANNEL_TYPE_TS;
		break;
	case CX18_ENC_STREAM_TYPE_YUV:
		captype = CAPTURE_CHANNEL_TYPE_YUV;
		break;
	case CX18_ENC_STREAM_TYPE_PCM:
		captype = CAPTURE_CHANNEL_TYPE_PCM;
		break;
	case CX18_ENC_STREAM_TYPE_VBI:
822
#ifdef CX18_ENCODER_PARSES_SLICED
823 824
		captype = cx18_raw_vbi(cx) ?
		     CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
825 826 827 828 829 830 831
#else
		/*
		 * Currently we set things up so that Sliced VBI from the
		 * digitizer is handled as Raw VBI by the encoder
		 */
		captype = CAPTURE_CHANNEL_TYPE_VBI;
#endif
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
		cx->vbi.frame = 0;
		cx->vbi.inserted_frame = 0;
		memset(cx->vbi.sliced_mpeg_size,
			0, sizeof(cx->vbi.sliced_mpeg_size));
		break;
	default:
		return -EINVAL;
	}

	/* Clear Streamoff flags in case left from last capture */
	clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);

	cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
	s->handle = data[0];
	cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);

848 849 850 851 852 853 854 855 856 857 858 859
	/*
	 * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
	 * set up all the parameters, as it is not obvious which parameters the
	 * firmware shares across capture channel types and which it does not.
	 *
	 * Some of the cx18_vapi() calls below apply to only certain capture
	 * channel types.  We're hoping there's no harm in calling most of them
	 * anyway, as long as the values are all consistent.  Setting some
	 * shared parameters will have no effect once an analog capture channel
	 * has started streaming.
	 */
	if (captype != CAPTURE_CHANNEL_TYPE_TS) {
860 861 862 863 864
		cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
		cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
		cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
		cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);

865 866 867 868 869 870 871 872 873 874 875
		/*
		 * Audio related reset according to
		 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
		 */
		if (atomic_read(&cx->ana_capturing) == 0)
			cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
				  s->handle, 12);

		/*
		 * Number of lines for Field 1 & Field 2 according to
		 * Documentation/video4linux/cx2341x/fw-encoder-api.txt
876 877
		 * Field 1 is 312 for 625 line systems in BT.656
		 * Field 2 is 313 for 625 line systems in BT.656
878
		 */
879
		cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
880
			  s->handle, 312, 313);
881 882 883 884

		if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
			cx18_vbi_setup(s);

885
		/*
886 887 888
		 * Select to receive I, P, and B frame index entries, if the
		 * index stream is enabled.  Otherwise disable index entry
		 * generation.
889
		 */
890
		s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
891 892
		cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 2,
				 s->handle, cx18_stream_enabled(s_idx) ? 7 : 0);
893

894
		/* Call out to the common CX2341x API setup for user controls */
895 896
		cx->cxhdl.priv = s;
		cx2341x_handler_setup(&cx->cxhdl);
897 898 899 900 901

		/*
		 * When starting a capture and we're set for radio,
		 * ensure the video is muted, despite the user control.
		 */
902
		if (!cx->cxhdl.video_mute &&
903 904
		    test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
			cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
905
			  (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8) | 1);
906 907 908 909 910

		/* Enable the Video Format Converter for UYVY 4:2:2 support,
		 * rather than the default HM12 Macroblovk 4:2:0 support.
		 */
		if (captype == CAPTURE_CHANNEL_TYPE_YUV) {
911
			if (s->pixelformat == V4L2_PIX_FMT_UYVY)
912 913 914 915 916 917 918
				cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
					s->handle, 1);
			else
				/* If in doubt, default to HM12 */
				cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
					s->handle, 0);
		}
919 920
	}

921
	if (atomic_read(&cx->tot_capturing) == 0) {
922
		cx2341x_handler_set_busy(&cx->cxhdl, 1);
923
		clear_bit(CX18_F_I_EOS, &cx->i_flags);
924
		cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
925 926 927
	}

	cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
A
Al Viro 已提交
928 929
		(void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
		(void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
930

931
	/* Init all the cpu_mdls for this stream */
932
	cx18_stream_configure_mdls(s);
933
	_cx18_stream_load_fw_queue(s);
934

935 936 937
	/* begin_capture */
	if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
		CX18_DEBUG_WARN("Error starting capture!\n");
938
		/* Ensure we're really not capturing before releasing MDLs */
939
		set_bit(CX18_F_S_STOPPING, &s->s_flags);
940 941 942 943
		if (s->type == CX18_ENC_STREAM_TYPE_MPG)
			cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
		else
			cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
944 945
		clear_bit(CX18_F_S_STREAMING, &s->s_flags);
		/* FIXME - CX18_F_S_STREAMOFF as well? */
946
		cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
947
		cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
948
		s->handle = CX18_INVALID_TASK_HANDLE;
949
		clear_bit(CX18_F_S_STOPPING, &s->s_flags);
950 951 952 953
		if (atomic_read(&cx->tot_capturing) == 0) {
			set_bit(CX18_F_I_EOS, &cx->i_flags);
			cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
		}
954 955 956 957
		return -EINVAL;
	}

	/* you're live! sit back and await interrupts :) */
958
	if (captype != CAPTURE_CHANNEL_TYPE_TS)
959 960
		atomic_inc(&cx->ana_capturing);
	atomic_inc(&cx->tot_capturing);
961 962
	return 0;
}
963
EXPORT_SYMBOL(cx18_start_v4l2_encode_stream);
964 965 966 967 968 969 970 971

void cx18_stop_all_captures(struct cx18 *cx)
{
	int i;

	for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
		struct cx18_stream *s = &cx->streams[i];

972
		if (!cx18_stream_enabled(s))
973 974 975 976 977 978 979 980 981 982
			continue;
		if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
			cx18_stop_v4l2_encode_stream(s, 0);
	}
}

int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
{
	struct cx18 *cx = s->cx;

983
	if (!cx18_stream_enabled(s))
984 985 986 987 988 989 990
		return -EINVAL;

	/* This function assumes that you are allowed to stop the capture
	   and that we are actually capturing */

	CX18_DEBUG_INFO("Stop Capture\n");

991
	if (atomic_read(&cx->tot_capturing) == 0)
992 993
		return 0;

994
	set_bit(CX18_F_S_STOPPING, &s->s_flags);
995 996 997 998 999 1000 1001 1002 1003
	if (s->type == CX18_ENC_STREAM_TYPE_MPG)
		cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
	else
		cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);

	if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
		CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
	}

1004 1005 1006
	if (s->type != CX18_ENC_STREAM_TYPE_TS)
		atomic_dec(&cx->ana_capturing);
	atomic_dec(&cx->tot_capturing);
1007 1008 1009 1010

	/* Clear capture and no-read bits */
	clear_bit(CX18_F_S_STREAMING, &s->s_flags);

1011 1012 1013
	/* Tell the CX23418 it can't use our buffers anymore */
	cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);

1014
	cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
1015
	s->handle = CX18_INVALID_TASK_HANDLE;
1016
	clear_bit(CX18_F_S_STOPPING, &s->s_flags);
1017

1018
	if (atomic_read(&cx->tot_capturing) > 0)
1019 1020
		return 0;

1021
	cx2341x_handler_set_busy(&cx->cxhdl, 0);
1022
	cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
1023 1024 1025 1026
	wake_up(&s->waitq);

	return 0;
}
1027
EXPORT_SYMBOL(cx18_stop_v4l2_encode_stream);
1028 1029 1030 1031 1032 1033 1034 1035 1036

u32 cx18_find_handle(struct cx18 *cx)
{
	int i;

	/* find first available handle to be used for global settings */
	for (i = 0; i < CX18_MAX_STREAMS; i++) {
		struct cx18_stream *s = &cx->streams[i];

1037
		if (s->video_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
1038 1039
			return s->handle;
	}
1040
	return CX18_INVALID_TASK_HANDLE;
1041
}
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
{
	int i;
	struct cx18_stream *s;

	if (handle == CX18_INVALID_TASK_HANDLE)
		return NULL;

	for (i = 0; i < CX18_MAX_STREAMS; i++) {
		s = &cx->streams[i];
		if (s->handle != handle)
			continue;
1055
		if (cx18_stream_enabled(s))
1056 1057 1058 1059
			return s;
	}
	return NULL;
}