cx25821-video.c 45.6 KB
Newer Older
1 2 3
/*
 *  Driver for the Conexant CX25821 PCIe bridge
 *
4
 *  Copyright (C) 2009 Conexant Systems Inc.
5 6
 *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
 *  Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 8 9
 *  Parts adapted/taken from Eduardo Moscoso Rubino
 *  Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
 *
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

27 28
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

29 30 31
#include "cx25821-video.h"

MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
32
MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
33 34
MODULE_LICENSE("GPL");

35
static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
36 37 38 39 40

module_param_array(video_nr, int, NULL, 0444);

MODULE_PARM_DESC(video_nr, "video device numbers");

41
static unsigned int video_debug = VIDEO_DEBUG;
42 43 44 45 46 47 48 49 50 51 52
module_param(video_debug, int, 0644);
MODULE_PARM_DESC(video_debug, "enable debug messages [video]");

static unsigned int irq_debug;
module_param(irq_debug, int, 0644);
MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");

unsigned int vid_limit = 16;
module_param(vid_limit, int, 0644);
MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");

53 54
static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);

55 56 57
#define FORMAT_FLAGS_PACKED       0x01

struct cx25821_fmt formats[] = {
58
	{
59 60 61 62
		.name = "8 bpp, gray",
		.fourcc = V4L2_PIX_FMT_GREY,
		.depth = 8,
		.flags = FORMAT_FLAGS_PACKED,
63
	 }, {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		.name = "4:1:1, packed, Y41P",
		.fourcc = V4L2_PIX_FMT_Y41P,
		.depth = 12,
		.flags = FORMAT_FLAGS_PACKED,
	}, {
		.name = "4:2:2, packed, YUYV",
		.fourcc = V4L2_PIX_FMT_YUYV,
		.depth = 16,
		.flags = FORMAT_FLAGS_PACKED,
	}, {
		.name = "4:2:2, packed, UYVY",
		.fourcc = V4L2_PIX_FMT_UYVY,
		.depth = 16,
		.flags = FORMAT_FLAGS_PACKED,
	}, {
		.name = "4:2:0, YUV",
		.fourcc = V4L2_PIX_FMT_YUV420,
		.depth = 12,
		.flags = FORMAT_FLAGS_PACKED,
	},
84 85
};

86
int cx25821_get_format_size(void)
87
{
88
	return ARRAY_SIZE(formats);
89 90
}

91
struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
92
{
93
	unsigned int i;
94

95
	if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
96
		return formats + 1;
97

98 99 100
	for (i = 0; i < ARRAY_SIZE(formats); i++)
		if (formats[i].fourcc == fourcc)
			return formats + i;
101

102
	pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
103
	return NULL;
104 105
}

106 107
void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
			  u32 count)
108
{
109 110
	struct cx25821_buffer *buf;
	int bc;
111

112 113 114 115 116
	for (bc = 0;; bc++) {
		if (list_empty(&q->active)) {
			dprintk(1, "bc=%d (=0: active empty)\n", bc);
			break;
		}
117

118 119
		buf = list_entry(q->active.next, struct cx25821_buffer,
				vb.queue);
120

121 122 123
		/* count comes from the hw and it is 16bit wide --
		 * this trick handles wrap-arounds correctly for
		 * up to 32767 buffers in flight... */
124
		if ((s16) (count - buf->count) < 0)
125
			break;
126

127
		v4l2_get_timestamp(&buf->vb.ts);
128 129 130 131
		buf->vb.state = VIDEOBUF_DONE;
		list_del(&buf->vb.queue);
		wake_up(&buf->vb.done);
	}
132

133 134 135 136 137
	if (list_empty(&q->active))
		del_timer(&q->timeout);
	else
		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
	if (bc != 1)
138
		pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
139 140 141 142
}

int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
{
143 144
	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
		__func__, (unsigned int)norm, v4l2_norm_to_name(norm));
145

146
	dev->tvnorm = norm;
147

148 149
	/* Tell the internal A/V decoder */
	cx25821_call_all(dev, core, s_std, norm);
150

151
	return 0;
152 153 154
}

struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
155
				       struct pci_dev *pci,
156
				       const struct video_device *template,
157
				       char *type)
158
{
159 160 161 162 163 164 165 166 167 168 169
	struct video_device *vfd;
	dprintk(1, "%s()\n", __func__);

	vfd = video_device_alloc();
	if (NULL == vfd)
		return NULL;
	*vfd = *template;
	vfd->v4l2_dev = &dev->v4l2_dev;
	vfd->release = video_device_release;
	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
		 cx25821_boards[dev->board].name);
170
	video_set_drvdata(vfd, dev);
171
	return vfd;
172 173 174 175 176
}

/*
static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
{
177 178 179 180 181 182 183 184 185 186 187 188
	int i;

	if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
		return -EINVAL;
	for (i = 0; i < CX25821_CTLS; i++)
		if (cx25821_ctls[i].v.id == qctrl->id)
			break;
	if (i == CX25821_CTLS) {
		*qctrl = no_ctl;
		return 0;
	}
	*qctrl = cx25821_ctls[i].v;
189
	return 0;
190 191 192
}
*/

193
/* resource management */
194 195
int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
		    unsigned int bit)
196
{
197 198 199 200 201 202 203
	dprintk(1, "%s()\n", __func__);
	if (fh->resources & bit)
		/* have it already allocated */
		return 1;

	/* is it free? */
	mutex_lock(&dev->lock);
204
	if (dev->channels[fh->channel_id].resources & bit) {
205 206 207 208 209 210
		/* no, someone else uses it */
		mutex_unlock(&dev->lock);
		return 0;
	}
	/* it's free, grab it */
	fh->resources |= bit;
211
	dev->channels[fh->channel_id].resources |= bit;
212
	dprintk(1, "res: get %d\n", bit);
213
	mutex_unlock(&dev->lock);
214
	return 1;
215 216
}

217
int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
218
{
219
	return fh->resources & bit;
220 221
}

222
int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
223
{
224
	return fh->dev->channels[fh->channel_id].resources & bit;
225 226
}

227 228
void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
		      unsigned int bits)
229
{
230 231 232 233 234
	BUG_ON((fh->resources & bits) != bits);
	dprintk(1, "%s()\n", __func__);

	mutex_lock(&dev->lock);
	fh->resources &= ~bits;
235
	dev->channels[fh->channel_id].resources &= ~bits;
236 237
	dprintk(1, "res: put %d\n", bits);
	mutex_unlock(&dev->lock);
238 239 240 241
}

int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
{
242 243
	struct v4l2_routing route;
	memset(&route, 0, sizeof(route));
244

245
	dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
246 247 248
		__func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
		INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
	dev->input = input;
249

250
	route.input = INPUT(input)->vmux;
251

252 253
	/* Tell the internal A/V decoder */
	cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
254

255
	return 0;
256 257 258
}

int cx25821_start_video_dma(struct cx25821_dev *dev,
259 260 261
			    struct cx25821_dmaqueue *q,
			    struct cx25821_buffer *buf,
			    struct sram_channel *channel)
262
{
263
	int tmp = 0;
264

265 266
	/* setup fifo + format */
	cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
267

268 269 270
	/* reset counter */
	cx_write(channel->gpcnt_ctl, 3);
	q->count = 1;
271

272 273 274
	/* enable irq */
	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
	cx_set(channel->int_msk, 0x11);
275

276 277
	/* start dma */
	cx_write(channel->dma_ctl, 0x11);	/* FIFO and RISC enable */
278

279 280 281
	/* make sure upstream setting if any is reversed */
	tmp = cx_read(VID_CH_MODE_SEL);
	cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
282

283
	return 0;
284 285
}

286 287 288
static int cx25821_restart_video_queue(struct cx25821_dev *dev,
				       struct cx25821_dmaqueue *q,
				       struct sram_channel *channel)
289
{
290 291
	struct cx25821_buffer *buf, *prev;
	struct list_head *item;
292

293
	if (!list_empty(&q->active)) {
294 295
		buf = list_entry(q->active.next, struct cx25821_buffer,
				vb.queue);
296

297
		cx25821_start_video_dma(dev, q, buf, channel);
298

299 300 301 302
		list_for_each(item, &q->active) {
			buf = list_entry(item, struct cx25821_buffer, vb.queue);
			buf->count = q->count++;
		}
303

304 305 306
		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
		return 0;
	}
307

308 309 310 311 312
	prev = NULL;
	for (;;) {
		if (list_empty(&q->queued))
			return 0;

313 314
		buf = list_entry(q->queued.next, struct cx25821_buffer,
				vb.queue);
315 316 317 318 319 320 321 322 323 324 325 326 327 328

		if (NULL == prev) {
			list_move_tail(&buf->vb.queue, &q->active);
			cx25821_start_video_dma(dev, q, buf, channel);
			buf->vb.state = VIDEOBUF_ACTIVE;
			buf->count = q->count++;
			mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
		} else if (prev->vb.width == buf->vb.width &&
			   prev->vb.height == buf->vb.height &&
			   prev->fmt == buf->fmt) {
			list_move_tail(&buf->vb.queue, &q->active);
			buf->vb.state = VIDEOBUF_ACTIVE;
			buf->count = q->count++;
			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
329
			prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
330 331 332 333
		} else {
			return 0;
		}
		prev = buf;
334
	}
335 336
}

337
static void cx25821_vid_timeout(unsigned long data)
338
{
339 340 341
	struct cx25821_data *timeout_data = (struct cx25821_data *)data;
	struct cx25821_dev *dev = timeout_data->dev;
	struct sram_channel *channel = timeout_data->channel;
342
	struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
343 344 345
	struct cx25821_buffer *buf;
	unsigned long flags;

346
	/* cx25821_sram_channel_dump(dev, channel); */
347 348 349 350
	cx_clear(channel->dma_ctl, 0x11);

	spin_lock_irqsave(&dev->slock, flags);
	while (!list_empty(&q->active)) {
351 352
		buf = list_entry(q->active.next, struct cx25821_buffer,
				vb.queue);
353
		list_del(&buf->vb.queue);
354

355 356 357 358 359 360
		buf->vb.state = VIDEOBUF_ERROR;
		wake_up(&buf->vb.done);
	}

	cx25821_restart_video_queue(dev, q, channel);
	spin_unlock_irqrestore(&dev->slock, flags);
361 362 363 364
}

int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
{
365 366 367
	u32 count = 0;
	int handled = 0;
	u32 mask;
368
	struct sram_channel *channel = dev->channels[chan_num].sram_channels;
369 370 371 372 373 374 375 376 377

	mask = cx_read(channel->int_msk);
	if (0 == (status & mask))
		return handled;

	cx_write(channel->int_stat, status);

	/* risc op code error */
	if (status & (1 << 16)) {
378 379
		pr_warn("%s, %s: video risc op code error\n",
			dev->name, channel->name);
380 381 382
		cx_clear(channel->dma_ctl, 0x11);
		cx25821_sram_channel_dump(dev, channel);
	}
383

384 385 386 387
	/* risc1 y */
	if (status & FLD_VID_DST_RISC1) {
		spin_lock(&dev->slock);
		count = cx_read(channel->gpcnt);
388 389
		cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
				count);
390 391 392
		spin_unlock(&dev->slock);
		handled++;
	}
393

394 395 396 397
	/* risc2 y */
	if (status & 0x10) {
		dprintk(2, "stopper video\n");
		spin_lock(&dev->slock);
398 399
		cx25821_restart_video_queue(dev,
				&dev->channels[channel->i].vidq, channel);
400 401 402 403
		spin_unlock(&dev->slock);
		handled++;
	}
	return handled;
404 405
}

406
int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
407
		 unsigned int *size)
408
{
409
	struct cx25821_fh *fh = q->priv_data;
410

411
	*size = fh->fmt->depth * fh->width * fh->height >> 3;
412

413 414
	if (0 == *count)
		*count = 32;
415

416 417
	if (*size * *count > vid_limit * 1024 * 1024)
		*count = (vid_limit * 1024 * 1024) / *size;
418

419
	return 0;
420 421
}

422
int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
423
		   enum v4l2_field field)
424
{
425 426 427
	struct cx25821_fh *fh = q->priv_data;
	struct cx25821_dev *dev = fh->dev;
	struct cx25821_buffer *buf =
428
		container_of(vb, struct cx25821_buffer, vb);
429
	int rc, init_buffer = 0;
430
	u32 line0_offset;
431 432
	struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
	int bpl_local = LINE_SIZE_D1;
433
	int channel_opened = fh->channel_id;
434 435 436 437 438

	BUG_ON(NULL == fh->fmt);
	if (fh->width < 48 || fh->width > 720 ||
	    fh->height < 32 || fh->height > 576)
		return -EINVAL;
439

440
	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
441

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

445 446 447 448 449 450 451 452
	if (buf->fmt != fh->fmt ||
	    buf->vb.width != fh->width ||
	    buf->vb.height != fh->height || buf->vb.field != field) {
		buf->fmt = fh->fmt;
		buf->vb.width = fh->width;
		buf->vb.height = fh->height;
		buf->vb.field = field;
		init_buffer = 1;
453
	}
454 455 456 457 458

	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
		init_buffer = 1;
		rc = videobuf_iolock(q, &buf->vb, NULL);
		if (0 != rc) {
459
			printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
460
			goto fail;
461 462 463
		}
	}

464 465 466 467 468
	dprintk(1, "init_buffer=%d\n", init_buffer);

	if (init_buffer) {

		channel_opened = dev->channel_opened;
469 470
		if (channel_opened < 0 || channel_opened > 7)
			channel_opened = 7;
471

472 473
		if (dev->channels[channel_opened].pixel_formats ==
				PIXEL_FRMT_411)
474 475 476 477
			buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
		else
			buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);

478 479
		if (dev->channels[channel_opened].pixel_formats ==
				PIXEL_FRMT_411) {
480 481
			bpl_local = buf->bpl;
		} else {
482
			bpl_local = buf->bpl;   /* Default */
483 484

			if (channel_opened >= 0 && channel_opened <= 7) {
485 486
				if (dev->channels[channel_opened]
						.use_cif_resolution) {
487 488
					if (dev->tvnorm & V4L2_STD_PAL_BG ||
					    dev->tvnorm & V4L2_STD_PAL_DK)
489 490
						bpl_local = 352 << 1;
					else
491 492 493
						bpl_local = dev->channels[
							channel_opened].
							cif_width << 1;
494 495 496
				}
			}
		}
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		switch (buf->vb.field) {
		case V4L2_FIELD_TOP:
			cx25821_risc_buffer(dev->pci, &buf->risc,
					    dma->sglist, 0, UNSET,
					    buf->bpl, 0, buf->vb.height);
			break;
		case V4L2_FIELD_BOTTOM:
			cx25821_risc_buffer(dev->pci, &buf->risc,
					    dma->sglist, UNSET, 0,
					    buf->bpl, 0, buf->vb.height);
			break;
		case V4L2_FIELD_INTERLACED:
			/* All other formats are top field first */
			line0_offset = 0;
			dprintk(1, "top field first\n");

			cx25821_risc_buffer(dev->pci, &buf->risc,
					    dma->sglist, line0_offset,
					    bpl_local, bpl_local, bpl_local,
					    buf->vb.height >> 1);
			break;
		case V4L2_FIELD_SEQ_TB:
			cx25821_risc_buffer(dev->pci, &buf->risc,
					    dma->sglist,
					    0, buf->bpl * (buf->vb.height >> 1),
					    buf->bpl, 0, buf->vb.height >> 1);
			break;
		case V4L2_FIELD_SEQ_BT:
			cx25821_risc_buffer(dev->pci, &buf->risc,
					    dma->sglist,
					    buf->bpl * (buf->vb.height >> 1), 0,
					    buf->bpl, 0, buf->vb.height >> 1);
			break;
		default:
			BUG();
		}
534
	}
535

536 537 538
	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
		buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
		fh->fmt->name, (unsigned long)buf->risc.dma);
539

540
	buf->vb.state = VIDEOBUF_PREPARED;
541

542
	return 0;
543

544
fail:
545 546
	cx25821_free_buffer(q, buf);
	return rc;
547 548
}

549 550
void cx25821_buffer_release(struct videobuf_queue *q,
			    struct videobuf_buffer *vb)
551
{
552
	struct cx25821_buffer *buf =
553
		container_of(vb, struct cx25821_buffer, vb);
554

555
	cx25821_free_buffer(q, buf);
556 557 558 559
}

struct videobuf_queue *get_queue(struct cx25821_fh *fh)
{
560 561 562 563 564 565 566
	switch (fh->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		return &fh->vidq;
	default:
		BUG();
		return NULL;
	}
567 568
}

569
int cx25821_get_resource(struct cx25821_fh *fh, int resource)
570
{
571 572 573 574 575 576 577
	switch (fh->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		return resource;
	default:
		BUG();
		return 0;
	}
578 579
}

580
int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
581
{
582
	struct cx25821_fh *fh = file->private_data;
583

584
	return videobuf_mmap_mapper(get_queue(fh), vma);
585 586
}

587 588 589

static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
{
590
	struct cx25821_buffer *buf =
591
		container_of(vb, struct cx25821_buffer, vb);
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	struct cx25821_buffer *prev;
	struct cx25821_fh *fh = vq->priv_data;
	struct cx25821_dev *dev = fh->dev;
	struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;

	/* add jump to stopper */
	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
	buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */

	dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);

	if (!list_empty(&q->queued)) {
		list_add_tail(&buf->vb.queue, &q->queued);
		buf->vb.state = VIDEOBUF_QUEUED;
		dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
				buf->vb.i);

	} else if (list_empty(&q->active)) {
		list_add_tail(&buf->vb.queue, &q->active);
		cx25821_start_video_dma(dev, q, buf,
				dev->channels[fh->channel_id].sram_channels);
		buf->vb.state = VIDEOBUF_ACTIVE;
		buf->count = q->count++;
		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
		dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
				buf, buf->vb.i, buf->count, q->count);
	} else {
		prev = list_entry(q->active.prev, struct cx25821_buffer,
				vb.queue);
		if (prev->vb.width == buf->vb.width
623 624
		   && prev->vb.height == buf->vb.height
		   && prev->fmt == buf->fmt) {
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
			list_add_tail(&buf->vb.queue, &q->active);
			buf->vb.state = VIDEOBUF_ACTIVE;
			buf->count = q->count++;
			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);

			/* 64 bit bits 63-32 */
			prev->risc.jmp[2] = cpu_to_le32(0);
			dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
					buf, buf->vb.i, buf->count);

		} else {
			list_add_tail(&buf->vb.queue, &q->queued);
			buf->vb.state = VIDEOBUF_QUEUED;
			dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
					buf->vb.i);
		}
	}

	if (list_empty(&q->active))
		dprintk(2, "active queue empty!\n");
645 646 647
}

static struct videobuf_queue_ops cx25821_video_qops = {
648 649 650 651
	.buf_setup = cx25821_buffer_setup,
	.buf_prepare = cx25821_buffer_prepare,
	.buf_queue = buffer_queue,
	.buf_release = cx25821_buffer_release,
652 653 654 655
};

static int video_open(struct file *file)
{
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
	struct video_device *vdev = video_devdata(file);
	struct cx25821_dev *h, *dev = video_drvdata(file);
	struct cx25821_fh *fh;
	struct list_head *list;
	int minor = video_devdata(file)->minor;
	enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	u32 pix_format;
	int ch_id = 0;
	int i;

	dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
			v4l2_type_names[type]);

	/* allocate + initialize per filehandle data */
	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
	if (NULL == fh)
		return -ENOMEM;
673

674
	mutex_lock(&cx25821_devlist_mutex);
675

676 677 678
	list_for_each(list, &cx25821_devlist)
	{
		h = list_entry(list, struct cx25821_dev, devlist);
679

680
		for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; i++) {
681
			if (h->channels[i].video_dev &&
682
			    h->channels[i].video_dev->minor == minor) {
683 684 685 686 687 688 689 690
				dev = h;
				ch_id = i;
				type  = V4L2_BUF_TYPE_VIDEO_CAPTURE;
			}
		}
	}

	if (NULL == dev) {
691
		mutex_unlock(&cx25821_devlist_mutex);
692
		kfree(fh);
693
		return -ENODEV;
694
	}
695

696 697 698 699 700 701 702 703 704 705 706 707
	file->private_data = fh;
	fh->dev = dev;
	fh->type = type;
	fh->width = 720;
	fh->channel_id = ch_id;

	if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
		fh->height = 576;
	else
		fh->height = 480;

	dev->channel_opened = fh->channel_id;
708 709 710 711
	if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
		pix_format = V4L2_PIX_FMT_Y41P;
	else
		pix_format = V4L2_PIX_FMT_YUYV;
712 713 714 715
	fh->fmt = cx25821_format_by_fourcc(pix_format);

	v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);

716 717 718 719
	videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
			&dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
			V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
			fh, NULL);
720

721
	dprintk(1, "post videobuf_queue_init()\n");
722
	mutex_unlock(&cx25821_devlist_mutex);
723

724
	return 0;
725 726 727
}

static ssize_t video_read(struct file *file, char __user * data, size_t count,
728
			 loff_t *ppos)
729
{
730
	struct cx25821_fh *fh = file->private_data;
731

732 733 734 735
	switch (fh->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
			return -EBUSY;
736

737
		return videobuf_read_one(&fh->vidq, data, count, ppos,
738
					file->f_flags & O_NONBLOCK);
739

740 741 742 743
	default:
		BUG();
		return 0;
	}
744 745 746
}

static unsigned int video_poll(struct file *file,
747
			      struct poll_table_struct *wait)
748
{
749 750 751 752 753 754 755 756
	struct cx25821_fh *fh = file->private_data;
	struct cx25821_buffer *buf;

	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
		/* streaming capture */
		if (list_empty(&fh->vidq.stream))
			return POLLERR;
		buf = list_entry(fh->vidq.stream.next,
757
				struct cx25821_buffer, vb.stream);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	} else {
		/* read() capture */
		buf = (struct cx25821_buffer *)fh->vidq.read_buf;
		if (NULL == buf)
			return POLLERR;
	}

	poll_wait(file, &buf->vb.done, wait);
	if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
		if (buf->vb.state == VIDEOBUF_DONE) {
			struct cx25821_dev *dev = fh->dev;

			if (dev && dev->channels[fh->channel_id]
					.use_cif_resolution) {
				u8 cam_id = *((char *)buf->vb.baddr + 3);
				memcpy((char *)buf->vb.baddr,
774 775
				      (char *)buf->vb.baddr + (fh->width * 2),
				      (fh->width * 2));
776 777 778
				*((char *)buf->vb.baddr + 3) = cam_id;
			}
		}
779

780 781
		return POLLIN | POLLRDNORM;
	}
782

783
	return 0;
784 785 786 787
}

static int video_release(struct file *file)
{
788 789
	struct cx25821_fh *fh = file->private_data;
	struct cx25821_dev *dev = fh->dev;
790

791 792
	/* stop the risc engine and fifo */
	cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
793

794 795 796 797 798
	/* stop video capture */
	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
		videobuf_queue_cancel(&fh->vidq);
		cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
	}
799

800 801 802 803
	if (fh->vidq.read_buf) {
		cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
		kfree(fh->vidq.read_buf);
	}
804

805
	videobuf_mmap_free(&fh->vidq);
806

807 808 809
	v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
	file->private_data = NULL;
	kfree(fh);
810

811
	return 0;
812 813 814 815
}

static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
{
816 817
	struct cx25821_fh *fh = priv;
	struct cx25821_dev *dev = fh->dev;
818

819 820
	if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
		return -EINVAL;
821

822 823
	if (unlikely(i != fh->type))
		return -EINVAL;
824

825 826 827
	if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
						RESOURCE_VIDEO0))))
		return -EBUSY;
828

829
	return videobuf_streamon(get_queue(fh));
830 831 832 833
}

static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
{
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
	struct cx25821_fh *fh = priv;
	struct cx25821_dev *dev = fh->dev;
	int err, res;

	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
	if (i != fh->type)
		return -EINVAL;

	res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
	err = videobuf_streamoff(get_queue(fh));
	if (err < 0)
		return err;
	cx25821_res_free(dev, fh, res);
	return 0;
849 850 851
}

static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
852
				struct v4l2_format *f)
853
{
854 855
	struct cx25821_fh *fh = priv;
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
856
	struct v4l2_mbus_framefmt mbus_fmt;
857 858 859 860 861 862 863 864 865
	int err;
	int pix_format = PIXEL_FRMT_422;

	if (fh) {
		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
				      fh->prio);
		if (0 != err)
			return err;
	}
866

867 868
	dprintk(2, "%s()\n", __func__);
	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
869

870 871
	if (0 != err)
		return err;
872

873 874
	fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
	fh->vidq.field = f->fmt.pix.field;
875

876 877 878
	/* check if width and height is valid based on set standard */
	if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
		fh->width = f->fmt.pix.width;
879

880 881
	if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
		fh->height = f->fmt.pix.height;
882

883 884 885 886 887 888
	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
		pix_format = PIXEL_FRMT_411;
	else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
		pix_format = PIXEL_FRMT_422;
	else
		return -EINVAL;
889

890
	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
891

892 893 894 895 896
	/* check if cif resolution */
	if (fh->width == 320 || fh->width == 352)
		dev->channels[fh->channel_id].use_cif_resolution = 1;
	else
		dev->channels[fh->channel_id].use_cif_resolution = 0;
897

898 899
	dev->channels[fh->channel_id].cif_width = fh->width;
	medusa_set_resolution(dev, fh->width, SRAM_CH00);
900

901 902
	dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
		fh->height, fh->vidq.field);
903 904
	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
	cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
905

906
	return 0;
907 908 909 910
}

static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
{
911 912 913
	int ret_val = 0;
	struct cx25821_fh *fh = priv;
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
914

915
	ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
916

917
	p->sequence = dev->channels[fh->channel_id].vidq.count;
918

919
	return ret_val;
920 921 922 923
}

static int vidioc_log_status(struct file *file, void *priv)
{
924 925
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
	struct cx25821_fh *fh = priv;
926 927
	struct sram_channel *sram_ch =
		dev->channels[fh->channel_id].sram_channels;
928
	u32 tmp = 0;
929

930 931
	cx25821_call_all(dev, core, log_status);
	tmp = cx_read(sram_ch->dma_ctl);
932 933
	pr_info("Video input 0 is %s\n",
		(tmp & 0x11) ? "streaming" : "stopped");
934
	return 0;
935 936 937
}

static int vidioc_s_ctrl(struct file *file, void *priv,
938
			struct v4l2_control *ctl)
939
{
940 941 942 943 944 945 946 947 948 949 950 951
	struct cx25821_fh *fh = priv;
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
	int err;

	if (fh) {
		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
				      fh->prio);
		if (0 != err)
			return err;
	}

	return cx25821_set_control(dev, ctl, fh->channel_id);
952 953
}

954 955 956
/* VIDEO IOCTLS */
int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
				 struct v4l2_format *f)
957
{
958
	struct cx25821_fh *fh = priv;
959

960 961 962 963 964 965
	f->fmt.pix.width = fh->width;
	f->fmt.pix.height = fh->height;
	f->fmt.pix.field = fh->vidq.field;
	f->fmt.pix.pixelformat = fh->fmt->fourcc;
	f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
966

967
	return 0;
968 969
}

970 971
int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
				   struct v4l2_format *f)
972
{
973 974 975
	struct cx25821_fmt *fmt;
	enum v4l2_field field;
	unsigned int maxw, maxh;
976

977
	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
978 979
	if (NULL == fmt)
		return -EINVAL;
980

981 982 983
	field = f->fmt.pix.field;
	maxw = 720;
	maxh = 576;
984

985
	if (V4L2_FIELD_ANY == field) {
986 987 988 989
		if (f->fmt.pix.height > maxh / 2)
			field = V4L2_FIELD_INTERLACED;
		else
			field = V4L2_FIELD_TOP;
990
	}
991

992 993 994 995 996 997 998 999 1000 1001
	switch (field) {
	case V4L2_FIELD_TOP:
	case V4L2_FIELD_BOTTOM:
		maxh = maxh / 2;
		break;
	case V4L2_FIELD_INTERLACED:
		break;
	default:
		return -EINVAL;
	}
1002

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	f->fmt.pix.field = field;
	if (f->fmt.pix.height < 32)
		f->fmt.pix.height = 32;
	if (f->fmt.pix.height > maxh)
		f->fmt.pix.height = maxh;
	if (f->fmt.pix.width < 48)
		f->fmt.pix.width = 48;
	if (f->fmt.pix.width > maxw)
		f->fmt.pix.width = maxw;
	f->fmt.pix.width &= ~0x03;
	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1015

1016
	return 0;
1017 1018
}

1019 1020
int cx25821_vidioc_querycap(struct file *file, void *priv,
			    struct v4l2_capability *cap)
1021
{
1022
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1023 1024 1025 1026
	struct cx25821_fh *fh = priv;
	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
1027 1028 1029 1030

	strcpy(cap->driver, "cx25821");
	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1031 1032 1033 1034 1035
	if (fh->channel_id >= VID_CHANNEL_NUM)
		cap->device_caps = cap_output;
	else
		cap->device_caps = cap_input;
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1036
	return 0;
1037 1038
}

1039
int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1040
			    struct v4l2_fmtdesc *f)
1041
{
1042 1043
	if (unlikely(f->index >= ARRAY_SIZE(formats)))
		return -EINVAL;
1044

1045 1046
	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
	f->pixelformat = formats[f->index].fourcc;
1047

1048
	return 0;
1049 1050
}

1051 1052
int cx25821_vidioc_reqbufs(struct file *file, void *priv,
			   struct v4l2_requestbuffers *p)
1053
{
1054 1055
	struct cx25821_fh *fh = priv;
	return videobuf_reqbufs(get_queue(fh), p);
1056 1057
}

1058 1059
int cx25821_vidioc_querybuf(struct file *file, void *priv,
			    struct v4l2_buffer *p)
1060
{
1061 1062
	struct cx25821_fh *fh = priv;
	return videobuf_querybuf(get_queue(fh), p);
1063 1064
}

1065
int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1066
{
1067 1068
	struct cx25821_fh *fh = priv;
	return videobuf_qbuf(get_queue(fh), p);
1069 1070
}

1071
int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1072 1073
{
	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1074
	struct cx25821_fh *fh = f;
1075

1076
	*p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1077 1078 1079 1080

	return 0;
}

1081 1082
int cx25821_vidioc_s_priority(struct file *file, void *f,
			      enum v4l2_priority prio)
1083 1084 1085 1086
{
	struct cx25821_fh *fh = f;
	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;

1087 1088
	return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
			prio);
1089 1090
}

1091 1092 1093 1094 1095 1096 1097 1098
int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
{
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;

	*tvnorms = dev->tvnorm;
	return 0;
}

1099
int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1100
{
1101 1102 1103
	struct cx25821_fh *fh = priv;
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
	int err;
1104

1105
	dprintk(1, "%s()\n", __func__);
1106

1107
	if (fh) {
1108 1109
		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
				      fh->prio);
1110 1111 1112
		if (0 != err)
			return err;
	}
1113

1114
	if (dev->tvnorm == tvnorms)
1115
		return 0;
1116

1117
	mutex_lock(&dev->lock);
1118
	cx25821_set_tvnorm(dev, tvnorms);
1119
	mutex_unlock(&dev->lock);
1120

1121
	medusa_set_videostandard(dev);
1122

1123
	return 0;
1124 1125 1126 1127
}

int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
{
1128
	static const char * const iname[] = {
1129 1130 1131 1132 1133 1134
		[CX25821_VMUX_COMPOSITE] = "Composite",
		[CX25821_VMUX_SVIDEO] = "S-Video",
		[CX25821_VMUX_DEBUG] = "for debug only",
	};
	unsigned int n;
	dprintk(1, "%s()\n", __func__);
1135

1136
	n = i->index;
1137
	if (n >= 2)
1138 1139 1140 1141
		return -EINVAL;

	if (0 == INPUT(n)->type)
		return -EINVAL;
1142

1143 1144
	i->type = V4L2_INPUT_TYPE_CAMERA;
	strcpy(i->name, iname[INPUT(n)->type]);
1145

1146 1147
	i->std = CX25821_NORMS;
	return 0;
1148 1149
}

1150 1151
int cx25821_vidioc_enum_input(struct file *file, void *priv,
			      struct v4l2_input *i)
1152
{
1153 1154 1155
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
	dprintk(1, "%s()\n", __func__);
	return cx25821_enum_input(dev, i);
1156 1157
}

1158
int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1159
{
1160
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1161

1162
	*i = dev->input;
1163
	dprintk(1, "%s(): returns %d\n", __func__, *i);
1164
	return 0;
1165 1166
}

1167
int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1168
{
1169 1170 1171
	struct cx25821_fh *fh = priv;
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
	int err;
1172

1173
	dprintk(1, "%s(%d)\n", __func__, i);
1174

1175
	if (fh) {
1176 1177
		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
				      fh->prio);
1178 1179 1180
		if (0 != err)
			return err;
	}
1181

1182
	if (i >= CX25821_NR_INPUT) {
1183
		dprintk(1, "%s(): -EINVAL\n", __func__);
1184 1185
		return -EINVAL;
	}
1186

1187 1188 1189 1190
	mutex_lock(&dev->lock);
	cx25821_video_mux(dev, i);
	mutex_unlock(&dev->lock);
	return 0;
1191 1192 1193
}

#ifdef CONFIG_VIDEO_ADV_DEBUG
1194
int cx25821_vidioc_g_register(struct file *file, void *fh,
1195
		      struct v4l2_dbg_register *reg)
1196
{
1197
	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1198

1199 1200
	if (!v4l2_chip_match_host(&reg->match))
		return -EINVAL;
1201

1202
	cx25821_call_all(dev, core, g_register, reg);
1203

1204
	return 0;
1205 1206
}

1207
int cx25821_vidioc_s_register(struct file *file, void *fh,
1208
		      const struct v4l2_dbg_register *reg)
1209
{
1210
	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1211

1212 1213
	if (!v4l2_chip_match_host(&reg->match))
		return -EINVAL;
1214

1215
	cx25821_call_all(dev, core, s_register, reg);
1216

1217
	return 0;
1218 1219 1220 1221
}

#endif

1222
/*****************************************************************************/
1223
static const struct v4l2_queryctrl no_ctl = {
1224
	.name = "42",
1225 1226 1227 1228 1229 1230
	.flags = V4L2_CTRL_FLAG_DISABLED,
};

static struct v4l2_queryctrl cx25821_ctls[] = {
	/* --- video --- */
	{
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
		.id = V4L2_CID_BRIGHTNESS,
		.name = "Brightness",
		.minimum = 0,
		.maximum = 10000,
		.step = 1,
		.default_value = 6200,
		.type = V4L2_CTRL_TYPE_INTEGER,
	}, {
		.id = V4L2_CID_CONTRAST,
		.name = "Contrast",
		.minimum = 0,
		.maximum = 10000,
		.step = 1,
		.default_value = 5000,
		.type = V4L2_CTRL_TYPE_INTEGER,
	}, {
		.id = V4L2_CID_SATURATION,
		.name = "Saturation",
		.minimum = 0,
		.maximum = 10000,
		.step = 1,
		.default_value = 5000,
		.type = V4L2_CTRL_TYPE_INTEGER,
	}, {
		.id = V4L2_CID_HUE,
		.name = "Hue",
		.minimum = 0,
		.maximum = 10000,
		.step = 1,
		.default_value = 5000,
		.type = V4L2_CTRL_TYPE_INTEGER,
	}
1263 1264 1265 1266 1267 1268 1269
};
static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);

static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
{
	int i;

1270
	if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
		return -EINVAL;
	for (i = 0; i < CX25821_CTLS; i++)
		if (cx25821_ctls[i].id == qctrl->id)
			break;
	if (i == CX25821_CTLS) {
		*qctrl = no_ctl;
		return 0;
	}
	*qctrl = cx25821_ctls[i];
	return 0;
}

1283
int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1284
		     struct v4l2_queryctrl *qctrl)
1285
{
1286 1287 1288 1289 1290 1291
	return cx25821_ctrl_query(qctrl);
}

/* ------------------------------------------------------------------ */
/* VIDEO CTRL IOCTLS                                                  */

1292
static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1293 1294 1295 1296 1297
{
	unsigned int i;

	for (i = 0; i < CX25821_CTLS; i++)
		if (cx25821_ctls[i].id == id)
1298
			return cx25821_ctls + i;
1299 1300 1301
	return NULL;
}

1302 1303
int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
			  struct v4l2_control *ctl)
1304
{
1305
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1306
	struct cx25821_fh *fh = priv;
1307

1308
	const struct v4l2_queryctrl *ctrl;
1309 1310 1311 1312 1313

	ctrl = ctrl_by_id(ctl->id);

	if (NULL == ctrl)
		return -EINVAL;
1314 1315
	switch (ctl->id) {
	case V4L2_CID_BRIGHTNESS:
1316
		ctl->value = dev->channels[fh->channel_id].ctl_bright;
1317
		break;
1318
	case V4L2_CID_HUE:
1319
		ctl->value = dev->channels[fh->channel_id].ctl_hue;
1320
		break;
1321
	case V4L2_CID_CONTRAST:
1322
		ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1323
		break;
1324
	case V4L2_CID_SATURATION:
1325
		ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1326
		break;
1327
	}
1328 1329 1330 1331
	return 0;
}

int cx25821_set_control(struct cx25821_dev *dev,
1332
			struct v4l2_control *ctl, int chan_num)
1333
{
1334
	int err;
1335
	const struct v4l2_queryctrl *ctrl;
1336

1337
	err = -EINVAL;
1338 1339 1340

	ctrl = ctrl_by_id(ctl->id);

1341
	if (NULL == ctrl)
1342 1343
		return err;

1344 1345 1346 1347
	switch (ctrl->type) {
	case V4L2_CTRL_TYPE_BOOLEAN:
	case V4L2_CTRL_TYPE_MENU:
	case V4L2_CTRL_TYPE_INTEGER:
1348 1349 1350 1351 1352
		if (ctl->value < ctrl->minimum)
			ctl->value = ctrl->minimum;
		if (ctl->value > ctrl->maximum)
			ctl->value = ctrl->maximum;
		break;
1353 1354
	default:
		/* nothing */ ;
1355
	}
1356

1357 1358
	switch (ctl->id) {
	case V4L2_CID_BRIGHTNESS:
1359
		dev->channels[chan_num].ctl_bright = ctl->value;
1360 1361
		medusa_set_brightness(dev, ctl->value, chan_num);
		break;
1362
	case V4L2_CID_HUE:
1363
		dev->channels[chan_num].ctl_hue = ctl->value;
1364 1365
		medusa_set_hue(dev, ctl->value, chan_num);
		break;
1366
	case V4L2_CID_CONTRAST:
1367
		dev->channels[chan_num].ctl_contrast = ctl->value;
1368 1369
		medusa_set_contrast(dev, ctl->value, chan_num);
		break;
1370
	case V4L2_CID_SATURATION:
1371
		dev->channels[chan_num].ctl_saturation = ctl->value;
1372 1373
		medusa_set_saturation(dev, ctl->value, chan_num);
		break;
1374
	}
1375

1376
	err = 0;
1377 1378 1379 1380

	return err;
}

1381
static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
{
	struct v4l2_control ctrl;
	int i;
	for (i = 0; i < CX25821_CTLS; i++) {
		ctrl.id = cx25821_ctls[i].id;
		ctrl.value = cx25821_ctls[i].default_value;

		cx25821_set_control(dev, &ctrl, chan_num);
	}
}

1393 1394
int cx25821_vidioc_cropcap(struct file *file, void *priv,
			   struct v4l2_cropcap *cropcap)
1395 1396
{
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1397

1398 1399
	if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
1400 1401
	cropcap->bounds.top = 0;
	cropcap->bounds.left = 0;
1402 1403
	cropcap->bounds.width = 720;
	cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1404
	cropcap->pixelaspect.numerator =
1405
		dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1406
	cropcap->pixelaspect.denominator =
1407
		dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1408 1409 1410 1411
	cropcap->defrect = cropcap->bounds;
	return 0;
}

1412
int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
1413
{
1414 1415 1416 1417 1418
	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
	struct cx25821_fh *fh = priv;
	int err;

	if (fh) {
1419 1420
		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
				      fh->prio);
1421 1422 1423
		if (0 != err)
			return err;
	}
1424
	/* cx25821_vidioc_s_crop not supported */
1425
	return -EINVAL;
1426 1427
}

1428
int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1429
{
1430
	/* cx25821_vidioc_g_crop not supported */
1431
	return -EINVAL;
1432 1433
}

1434
int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1435
{
1436 1437 1438 1439 1440 1441
	if (tvnorm == V4L2_STD_PAL_BG) {
		if (width == 352 || width == 720)
			return 1;
		else
			return 0;
	}
1442

1443 1444 1445 1446 1447 1448 1449
	if (tvnorm == V4L2_STD_NTSC_M) {
		if (width == 320 || width == 352 || width == 720)
			return 1;
		else
			return 0;
	}
	return 0;
1450 1451
}

1452
int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1453
{
1454 1455 1456 1457 1458 1459
	if (tvnorm == V4L2_STD_PAL_BG) {
		if (height == 576 || height == 288)
			return 1;
		else
			return 0;
	}
1460

1461 1462 1463 1464 1465 1466
	if (tvnorm == V4L2_STD_NTSC_M) {
		if (height == 480 || height == 240)
			return 1;
		else
			return 0;
	}
1467

1468
	return 0;
1469
}
1470 1471

static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1472
				 unsigned long arg)
1473
{
1474 1475 1476 1477
	struct cx25821_fh *fh = file->private_data;
	struct cx25821_dev *dev = fh->dev;
	int command = 0;
	struct upstream_user_struct *data_from_user;
1478

1479
	data_from_user = (struct upstream_user_struct *)arg;
1480

1481 1482 1483 1484
	if (!data_from_user) {
		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
		return 0;
	}
1485

1486
	command = data_from_user->command;
1487

1488 1489
	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
		return 0;
1490

1491 1492 1493 1494 1495 1496
	dev->input_filename = data_from_user->input_filename;
	dev->input_audiofilename = data_from_user->input_filename;
	dev->vid_stdname = data_from_user->vid_stdname;
	dev->pixel_format = data_from_user->pixel_format;
	dev->channel_select = data_from_user->channel_select;
	dev->command = data_from_user->command;
1497

1498 1499 1500 1501
	switch (command) {
	case UPSTREAM_START_VIDEO:
		cx25821_start_upstream_video_ch1(dev, data_from_user);
		break;
1502

1503 1504 1505 1506
	case UPSTREAM_STOP_VIDEO:
		cx25821_stop_upstream_video_ch1(dev);
		break;
	}
1507

1508
	return 0;
1509 1510 1511
}

static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1512
				  unsigned long arg)
1513
{
1514 1515 1516 1517
	struct cx25821_fh *fh = file->private_data;
	struct cx25821_dev *dev = fh->dev;
	int command = 0;
	struct upstream_user_struct *data_from_user;
1518

1519
	data_from_user = (struct upstream_user_struct *)arg;
1520

1521 1522 1523 1524
	if (!data_from_user) {
		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
		return 0;
	}
1525

1526
	command = data_from_user->command;
1527

1528 1529
	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
		return 0;
1530

1531 1532 1533 1534 1535 1536
	dev->input_filename_ch2 = data_from_user->input_filename;
	dev->input_audiofilename = data_from_user->input_filename;
	dev->vid_stdname_ch2 = data_from_user->vid_stdname;
	dev->pixel_format_ch2 = data_from_user->pixel_format;
	dev->channel_select_ch2 = data_from_user->channel_select;
	dev->command_ch2 = data_from_user->command;
1537

1538 1539 1540 1541
	switch (command) {
	case UPSTREAM_START_VIDEO:
		cx25821_start_upstream_video_ch2(dev, data_from_user);
		break;
1542

1543 1544 1545 1546
	case UPSTREAM_STOP_VIDEO:
		cx25821_stop_upstream_video_ch2(dev);
		break;
	}
1547

1548
	return 0;
1549 1550 1551
}

static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1552
				  unsigned long arg)
1553
{
1554 1555 1556 1557
	struct cx25821_fh *fh = file->private_data;
	struct cx25821_dev *dev = fh->dev;
	int command = 0;
	struct upstream_user_struct *data_from_user;
1558

1559
	data_from_user = (struct upstream_user_struct *)arg;
1560

1561 1562 1563 1564
	if (!data_from_user) {
		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
		return 0;
	}
1565

1566
	command = data_from_user->command;
1567

1568 1569
	if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
		return 0;
1570

1571 1572 1573 1574 1575 1576
	dev->input_filename = data_from_user->input_filename;
	dev->input_audiofilename = data_from_user->input_filename;
	dev->vid_stdname = data_from_user->vid_stdname;
	dev->pixel_format = data_from_user->pixel_format;
	dev->channel_select = data_from_user->channel_select;
	dev->command = data_from_user->command;
1577

1578 1579 1580 1581
	switch (command) {
	case UPSTREAM_START_AUDIO:
		cx25821_start_upstream_audio(dev, data_from_user);
		break;
1582

1583 1584 1585 1586
	case UPSTREAM_STOP_AUDIO:
		cx25821_stop_upstream_audio(dev);
		break;
	}
1587

1588
	return 0;
1589 1590 1591
}

static long video_ioctl_set(struct file *file, unsigned int cmd,
1592
			   unsigned long arg)
1593
{
1594 1595 1596 1597 1598
	struct cx25821_fh *fh = file->private_data;
	struct cx25821_dev *dev = fh->dev;
	struct downstream_user_struct *data_from_user;
	int command;
	int width = 720;
1599 1600 1601 1602 1603
	int selected_channel = 0;
	int pix_format = 0;
	int i = 0;
	int cif_enable = 0;
	int cif_width = 0;
1604

1605
	data_from_user = (struct downstream_user_struct *)arg;
1606

1607 1608 1609 1610
	if (!data_from_user) {
		pr_err("%s(): User data is INVALID. Returning\n", __func__);
		return 0;
	}
1611

1612
	command = data_from_user->command;
1613

1614
	if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1615 1616 1617
	   && command != ENABLE_CIF_RESOLUTION && command != REG_READ
	   && command != REG_WRITE && command != MEDUSA_READ
	   && command != MEDUSA_WRITE) {
1618 1619
		return 0;
	}
1620

1621 1622
	switch (command) {
	case SET_VIDEO_STD:
1623 1624 1625 1626
		if (!strcmp(data_from_user->vid_stdname, "PAL"))
			dev->tvnorm = V4L2_STD_PAL_BG;
		else
			dev->tvnorm = V4L2_STD_NTSC_M;
1627 1628
		medusa_set_videostandard(dev);
		break;
1629

1630 1631 1632 1633 1634 1635 1636 1637
	case SET_PIXEL_FORMAT:
		selected_channel = data_from_user->decoder_select;
		pix_format = data_from_user->pixel_format;

		if (!(selected_channel <= 7 && selected_channel >= 0)) {
			selected_channel -= 4;
			selected_channel = selected_channel % 8;
		}
1638

1639 1640
		if (selected_channel >= 0)
			cx25821_set_pixel_format(dev, selected_channel,
1641
						pix_format);
1642

1643 1644 1645 1646 1647 1648 1649 1650 1651
		break;

	case ENABLE_CIF_RESOLUTION:
		selected_channel = data_from_user->decoder_select;
		cif_enable = data_from_user->cif_resolution_enable;
		cif_width = data_from_user->cif_width;

		if (cif_enable) {
			if (dev->tvnorm & V4L2_STD_PAL_BG
1652
			    || dev->tvnorm & V4L2_STD_PAL_DK) {
1653
				width = 352;
1654 1655 1656 1657 1658
			} else {
				width = cif_width;
				if (cif_width != 320 && cif_width != 352)
					width = 320;
			}
1659 1660 1661 1662 1663 1664 1665 1666
		}

		if (!(selected_channel <= 7 && selected_channel >= 0)) {
			selected_channel -= 4;
			selected_channel = selected_channel % 8;
		}

		if (selected_channel <= 7 && selected_channel >= 0) {
1667 1668
			dev->channels[selected_channel].use_cif_resolution =
				cif_enable;
1669 1670 1671 1672 1673 1674 1675 1676
			dev->channels[selected_channel].cif_width = width;
		} else {
			for (i = 0; i < VID_CHANNEL_NUM; i++) {
				dev->channels[i].use_cif_resolution =
					cif_enable;
				dev->channels[i].cif_width = width;
			}
		}
1677

1678 1679 1680 1681 1682 1683 1684 1685 1686
		medusa_set_resolution(dev, width, selected_channel);
		break;
	case REG_READ:
		data_from_user->reg_data = cx_read(data_from_user->reg_address);
		break;
	case REG_WRITE:
		cx_write(data_from_user->reg_address, data_from_user->reg_data);
		break;
	case MEDUSA_READ:
1687
		cx25821_i2c_read(&dev->i2c_bus[0],
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
					 (u16) data_from_user->reg_address,
					 &data_from_user->reg_data);
		break;
	case MEDUSA_WRITE:
		cx25821_i2c_write(&dev->i2c_bus[0],
				  (u16) data_from_user->reg_address,
				  data_from_user->reg_data);
		break;
	}

	return 0;
1699 1700 1701
}

static long cx25821_video_ioctl(struct file *file,
1702
				unsigned int cmd, unsigned long arg)
1703
{
1704
	int ret = 0;
1705

1706
	struct cx25821_fh *fh = file->private_data;
1707

1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
	/* check to see if it's the video upstream */
	if (fh->channel_id == SRAM_CH09) {
		ret = video_ioctl_upstream9(file, cmd, arg);
		return ret;
	} else if (fh->channel_id == SRAM_CH10) {
		ret = video_ioctl_upstream10(file, cmd, arg);
		return ret;
	} else if (fh->channel_id == SRAM_CH11) {
		ret = video_ioctl_upstream11(file, cmd, arg);
		ret = video_ioctl_set(file, cmd, arg);
		return ret;
	}

	return video_ioctl2(file, cmd, arg);
1722 1723 1724 1725
}

/* exported stuff */
static const struct v4l2_file_operations video_fops = {
1726 1727 1728 1729 1730 1731 1732
	.owner = THIS_MODULE,
	.open = video_open,
	.release = video_release,
	.read = video_read,
	.poll = video_poll,
	.mmap = cx25821_video_mmap,
	.ioctl = cx25821_video_ioctl,
1733 1734 1735
};

static const struct v4l2_ioctl_ops video_ioctl_ops = {
1736 1737 1738 1739 1740 1741 1742 1743 1744
	.vidioc_querycap = cx25821_vidioc_querycap,
	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
	.vidioc_reqbufs = cx25821_vidioc_reqbufs,
	.vidioc_querybuf = cx25821_vidioc_querybuf,
	.vidioc_qbuf = cx25821_vidioc_qbuf,
	.vidioc_dqbuf = vidioc_dqbuf,
1745
	.vidioc_g_std = cx25821_vidioc_g_std,
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
	.vidioc_s_std = cx25821_vidioc_s_std,
	.vidioc_cropcap = cx25821_vidioc_cropcap,
	.vidioc_s_crop = cx25821_vidioc_s_crop,
	.vidioc_g_crop = cx25821_vidioc_g_crop,
	.vidioc_enum_input = cx25821_vidioc_enum_input,
	.vidioc_g_input = cx25821_vidioc_g_input,
	.vidioc_s_input = cx25821_vidioc_s_input,
	.vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
	.vidioc_s_ctrl = vidioc_s_ctrl,
	.vidioc_queryctrl = cx25821_vidioc_queryctrl,
	.vidioc_streamon = vidioc_streamon,
	.vidioc_streamoff = vidioc_streamoff,
	.vidioc_log_status = vidioc_log_status,
	.vidioc_g_priority = cx25821_vidioc_g_priority,
	.vidioc_s_priority = cx25821_vidioc_s_priority,
1761
#ifdef CONFIG_VIDEO_ADV_DEBUG
1762 1763
	.vidioc_g_register = cx25821_vidioc_g_register,
	.vidioc_s_register = cx25821_vidioc_s_register,
1764 1765 1766
#endif
};

1767 1768
static const struct video_device cx25821_video_device = {
	.name = "cx25821-video",
1769
	.fops = &video_fops,
1770
	.minor = -1,
1771 1772
	.ioctl_ops = &video_ioctl_ops,
	.tvnorms = CX25821_NORMS,
1773
};
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846

void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
{
	cx_clear(PCI_INT_MSK, 1);

	if (dev->channels[chan_num].video_dev) {
		if (video_is_registered(dev->channels[chan_num].video_dev))
			video_unregister_device(
					dev->channels[chan_num].video_dev);
		else
			video_device_release(
					dev->channels[chan_num].video_dev);

		dev->channels[chan_num].video_dev = NULL;

		btcx_riscmem_free(dev->pci,
				&dev->channels[chan_num].vidq.stopper);

		pr_warn("device %d released!\n", chan_num);
	}

}

int cx25821_video_register(struct cx25821_dev *dev)
{
	int err;
	int i;

	spin_lock_init(&dev->slock);

	for (i = 0; i < VID_CHANNEL_NUM; ++i) {
		if (i == SRAM_CH08) /* audio channel */
			continue;

		cx25821_init_controls(dev, i);

		cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
			dev->channels[i].sram_channels->dma_ctl, 0x11, 0);

		dev->channels[i].sram_channels = &cx25821_sram_channels[i];
		dev->channels[i].video_dev = NULL;
		dev->channels[i].resources = 0;

		cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);

		INIT_LIST_HEAD(&dev->channels[i].vidq.active);
		INIT_LIST_HEAD(&dev->channels[i].vidq.queued);

		dev->channels[i].timeout_data.dev = dev;
		dev->channels[i].timeout_data.channel =
			&cx25821_sram_channels[i];
		dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
		dev->channels[i].vidq.timeout.data =
			(unsigned long)&dev->channels[i].timeout_data;
		init_timer(&dev->channels[i].vidq.timeout);

		/* register v4l devices */
		dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
				&cx25821_video_device, "video");

		err = video_register_device(dev->channels[i].video_dev,
				VFL_TYPE_GRABBER, video_nr[dev->nr]);

		if (err < 0)
			goto fail_unreg;

	}

	/* set PCI interrupt */
	cx_set(PCI_INT_MSK, 0xff);

	/* initial device configuration */
	mutex_lock(&dev->lock);
1847
	dev->tvnorm = V4L2_STD_NTSC_M,
1848 1849 1850 1851 1852 1853 1854 1855 1856
	cx25821_set_tvnorm(dev, dev->tvnorm);
	mutex_unlock(&dev->lock);

	return 0;

fail_unreg:
	cx25821_video_unregister(dev, i);
	return err;
}