cx231xx-video.c 57.8 KB
Newer Older
1
/*
2 3
   cx231xx-video.c - driver for Conexant Cx23100/101/102
		     USB video capture devices
4 5

   Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6 7 8
	Based on em28xx driver
	Based on cx23885 driver
	Based on cx88 driver
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

   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.
 */

#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/bitmap.h>
#include <linux/usb.h>
#include <linux/i2c.h>
#include <linux/version.h>
#include <linux/mm.h>
#include <linux/mutex.h>

#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-chip-ident.h>
#include <media/msp3400.h>
#include <media/tuner.h>

#include "dvb_frontend.h"

#include "cx231xx.h"
#include "cx231xx-vbi.h"

47 48
#define CX231XX_VERSION_CODE            KERNEL_VERSION(0, 0, 1)

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#define DRIVER_AUTHOR   "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
#define DRIVER_DESC     "Conexant cx231xx based USB video device driver"

#define cx231xx_videodbg(fmt, arg...) do {\
	if (video_debug) \
		printk(KERN_INFO "%s %s :"fmt, \
			 dev->name, __func__ , ##arg); } while (0)

static unsigned int isoc_debug;
module_param(isoc_debug, int, 0644);
MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");

#define cx231xx_isocdbg(fmt, arg...) \
do {\
	if (isoc_debug) { \
		printk(KERN_INFO "%s %s :"fmt, \
			 dev->name, __func__ , ##arg); \
	} \
  } while (0)

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

static unsigned int card[]     = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
static unsigned int vbi_nr[]   = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };

78
module_param_array(card, int, NULL, 0444);
79 80 81 82
module_param_array(video_nr, int, NULL, 0444);
module_param_array(vbi_nr, int, NULL, 0444);
module_param_array(radio_nr, int, NULL, 0444);

83
MODULE_PARM_DESC(card, "card type");
84
MODULE_PARM_DESC(video_nr, "video device numbers");
85
MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
86 87 88 89 90 91 92 93 94
MODULE_PARM_DESC(radio_nr, "radio device numbers");

static unsigned int video_debug;
module_param(video_debug, int, 0644);
MODULE_PARM_DESC(video_debug, "enable debug messages [video]");

/* supported video standards */
static struct cx231xx_fmt format[] = {
	{
95 96 97 98 99
	 .name = "16bpp YUY2, 4:2:2, packed",
	 .fourcc = V4L2_PIX_FMT_YUYV,
	 .depth = 16,
	 .reg = 0,
	 },
100 101 102 103 104 105 106 107
};

/* supported controls */
/* Common to all boards */

/* ------------------------------------------------------------------- */

static const struct v4l2_queryctrl no_ctl = {
108
	.name = "42",
109 110 111 112 113 114
	.flags = V4L2_CTRL_FLAG_DISABLED,
};

static struct cx231xx_ctrl cx231xx_ctls[] = {
	/* --- video --- */
	{
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 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
		.v = {
			.id = V4L2_CID_BRIGHTNESS,
			.name = "Brightness",
			.minimum = 0x00,
			.maximum = 0xff,
			.step = 1,
			.default_value = 0x7f,
			.type = V4L2_CTRL_TYPE_INTEGER,
		},
		.off = 128,
		.reg = LUMA_CTRL,
		.mask = 0x00ff,
		.shift = 0,
	}, {
		.v = {
			.id = V4L2_CID_CONTRAST,
			.name = "Contrast",
			.minimum = 0,
			.maximum = 0xff,
			.step = 1,
			.default_value = 0x3f,
			.type = V4L2_CTRL_TYPE_INTEGER,
		},
		.off = 0,
		.reg = LUMA_CTRL,
		.mask = 0xff00,
		.shift = 8,
	}, {
		.v = {
			.id = V4L2_CID_HUE,
			.name = "Hue",
			.minimum = 0,
			.maximum = 0xff,
			.step = 1,
			.default_value = 0x7f,
			.type = V4L2_CTRL_TYPE_INTEGER,
		},
		.off = 128,
		.reg = CHROMA_CTRL,
		.mask = 0xff0000,
		.shift = 16,
	}, {
	/* strictly, this only describes only U saturation.
	* V saturation is handled specially through code.
	*/
		.v = {
			.id = V4L2_CID_SATURATION,
			.name = "Saturation",
			.minimum = 0,
			.maximum = 0xff,
			.step = 1,
			.default_value = 0x7f,
			.type = V4L2_CTRL_TYPE_INTEGER,
		},
		.off = 0,
		.reg = CHROMA_CTRL,
		.mask = 0x00ff,
		.shift = 0,
	}, {
		/* --- audio --- */
		.v = {
			.id = V4L2_CID_AUDIO_MUTE,
			.name = "Mute",
			.minimum = 0,
			.maximum = 1,
			.default_value = 1,
			.type = V4L2_CTRL_TYPE_BOOLEAN,
		},
		.reg = PATH1_CTL1,
		.mask = (0x1f << 24),
		.shift = 24,
	}, {
		.v = {
			.id = V4L2_CID_AUDIO_VOLUME,
			.name = "Volume",
			.minimum = 0,
			.maximum = 0x3f,
			.step = 1,
			.default_value = 0x3f,
			.type = V4L2_CTRL_TYPE_INTEGER,
		},
		.reg = PATH1_VOL_CTL,
		.mask = 0xff,
		.shift = 0,
	}
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
};
static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);

static const u32 cx231xx_user_ctrls[] = {
	V4L2_CID_USER_CLASS,
	V4L2_CID_BRIGHTNESS,
	V4L2_CID_CONTRAST,
	V4L2_CID_SATURATION,
	V4L2_CID_HUE,
	V4L2_CID_AUDIO_VOLUME,
#if 0
	V4L2_CID_AUDIO_BALANCE,
#endif
	V4L2_CID_AUDIO_MUTE,
	0
};

static const u32 *ctrl_classes[] = {
	cx231xx_user_ctrls,
	NULL
};

/* ------------------------------------------------------------------
	Video buffer and parser functions
   ------------------------------------------------------------------*/

/*
 * Announces that a buffer were filled and request the next
 */
static inline void buffer_filled(struct cx231xx *dev,
230 231
				 struct cx231xx_dmaqueue *dma_q,
				 struct cx231xx_buffer *buf)
232 233 234 235 236 237 238 239 240 241 242 243 244
{
	/* Advice that buffer was filled */
	cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
	buf->vb.state = VIDEOBUF_DONE;
	buf->vb.field_count++;
	do_gettimeofday(&buf->vb.ts);

	dev->video_mode.isoc_ctl.buf = NULL;

	list_del(&buf->vb.queue);
	wake_up(&buf->vb.done);
}

245
static inline void print_err_status(struct cx231xx *dev, int packet, int status)
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
{
	char *errmsg = "Unknown";

	switch (status) {
	case -ENOENT:
		errmsg = "unlinked synchronuously";
		break;
	case -ECONNRESET:
		errmsg = "unlinked asynchronuously";
		break;
	case -ENOSR:
		errmsg = "Buffer error (overrun)";
		break;
	case -EPIPE:
		errmsg = "Stalled (device not responding)";
		break;
	case -EOVERFLOW:
		errmsg = "Babble (bad cable?)";
		break;
	case -EPROTO:
		errmsg = "Bit-stuff error (bad cable?)";
		break;
	case -EILSEQ:
		errmsg = "CRC/Timeout (could be anything)";
		break;
	case -ETIME:
		errmsg = "Device does not respond";
		break;
	}
	if (packet < 0) {
276
		cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
277 278
	} else {
		cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
279
				packet, status, errmsg);
280 281 282 283 284 285 286
	}
}

/*
 * video-buf generic routine to get the next available buffer
 */
static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
287
				struct cx231xx_buffer **buf)
288
{
289 290 291
	struct cx231xx_video_mode *vmode =
	    container_of(dma_q, struct cx231xx_video_mode, vidq);
	struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

	char *outp;

	if (list_empty(&dma_q->active)) {
		cx231xx_isocdbg("No active queue to serve\n");
		dev->video_mode.isoc_ctl.buf = NULL;
		*buf = NULL;
		return;
	}

	/* Get the next buffer */
	*buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);

	/* Cleans up buffer - Usefull for testing for frame/URB loss */
	outp = videobuf_to_vmalloc(&(*buf)->vb);
	memset(outp, 0, (*buf)->vb.size);

	dev->video_mode.isoc_ctl.buf = *buf;

	return;
}

/*
 * Controls the isoc copy of each urb packet
 */
static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
{
319 320
	struct cx231xx_buffer *buf;
	struct cx231xx_dmaqueue *dma_q = urb->context;
321
	unsigned char *outp = NULL;
322
	int i, rc = 1;
323
	unsigned char *p_buffer;
324 325
	u32 bytes_parsed = 0, buffer_size = 0;
	u8 sav_eav = 0;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

	if (!dev)
		return 0;

	if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
		return 0;

	if (urb->status < 0) {
		print_err_status(dev, -1, urb->status);
		if (urb->status == -ENOENT)
			return 0;
	}

	buf = dev->video_mode.isoc_ctl.buf;
	if (buf != NULL)
		outp = videobuf_to_vmalloc(&buf->vb);

	for (i = 0; i < urb->number_of_packets; i++) {
		int status = urb->iso_frame_desc[i].status;

		if (status < 0) {
			print_err_status(dev, i, status);
			if (urb->iso_frame_desc[i].status != -EPROTO)
				continue;
		}

352
		if (urb->iso_frame_desc[i].actual_length <= 0) {
353 354 355 356
			/* cx231xx_isocdbg("packet %d is empty",i); - spammy */
			continue;
		}
		if (urb->iso_frame_desc[i].actual_length >
357
		    dev->video_mode.max_pkt_size) {
358 359 360 361
			cx231xx_isocdbg("packet bigger than packet size");
			continue;
		}

362
		/*  get buffer pointer and length */
363
		p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
364 365 366 367
		buffer_size = urb->iso_frame_desc[i].actual_length;
		bytes_parsed = 0;

		if (dma_q->is_partial_line) {
368
			/* Handle the case of a partial line */
369 370
			sav_eav = dma_q->last_sav;
		} else {
371 372
			/* Check for a SAV/EAV overlapping
				the buffer boundary */
373 374 375 376 377
			sav_eav =
			    cx231xx_find_boundary_SAV_EAV(p_buffer,
							  dma_q->partial_buf,
							  &bytes_parsed);
		}
378

379
		sav_eav &= 0xF0;
380 381
		/* Get the first line if we have some portion of an SAV/EAV from
		   the last buffer or a partial line  */
382
		if (sav_eav) {
383
			bytes_parsed += cx231xx_get_video_line(dev, dma_q,
384 385 386
				sav_eav,	/* SAV/EAV */
				p_buffer + bytes_parsed,	/* p_buffer */
				buffer_size - bytes_parsed);/* buf size */
387
		}
388

389 390
		/* Now parse data that is completely in this buffer */
		/* dma_q->is_partial_line = 0;  */
391

392 393
		while (bytes_parsed < buffer_size) {
			u32 bytes_used = 0;
394

395 396 397 398
			sav_eav = cx231xx_find_next_SAV_EAV(
				p_buffer + bytes_parsed,	/* p_buffer */
				buffer_size - bytes_parsed,	/* buf size */
				&bytes_used);/* bytes used to get SAV/EAV */
399

400
			bytes_parsed += bytes_used;
401

402 403
			sav_eav &= 0xF0;
			if (sav_eav && (bytes_parsed < buffer_size)) {
404
				bytes_parsed += cx231xx_get_video_line(dev,
405 406 407
					dma_q, sav_eav,	/* SAV/EAV */
					p_buffer + bytes_parsed,/* p_buffer */
					buffer_size - bytes_parsed);/*buf size*/
408 409
			}
		}
410

411 412
		/* Save the last four bytes of the buffer so we can check the
		   buffer boundary condition next time */
413 414
		memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
		bytes_parsed = 0;
415

416 417
	}
	return rc;
418 419
}

420 421
u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
				 u32 *p_bytes_used)
422
{
423 424 425 426 427
	u32 bytes_used;
	u8 boundary_bytes[8];
	u8 sav_eav = 0;

	*p_bytes_used = 0;
428

429 430
	/* Create an array of the last 4 bytes of the last buffer and the first
	   4 bytes of the current buffer. */
431

432 433
	memcpy(boundary_bytes, partial_buf, 4);
	memcpy(boundary_bytes + 4, p_buffer, 4);
434

435
	/* Check for the SAV/EAV in the boundary buffer */
436 437
	sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
					    &bytes_used);
438

439 440 441 442 443
	if (sav_eav) {
		/* found a boundary SAV/EAV.  Updates the bytes used to reflect
		   only those used in the new buffer */
		*p_bytes_used = bytes_used - 4;
	}
444

445
	return sav_eav;
446 447
}

448
u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
449 450 451
{
	u32 i;
	u8 sav_eav = 0;
452

453 454 455 456 457
	/*
	 * Don't search if the buffer size is less than 4.  It causes a page
	 * fault since buffer_size - 4 evaluates to a large number in that
	 * case.
	 */
458 459 460 461
	if (buffer_size < 4) {
		*p_bytes_used = buffer_size;
		return 0;
	}
462

463
	for (i = 0; i < (buffer_size - 3); i++) {
464

465 466
		if ((p_buffer[i] == 0xFF) &&
		    (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
467

468 469 470 471 472
			*p_bytes_used = i + 4;
			sav_eav = p_buffer[i + 3];
			return sav_eav;
		}
	}
473

474 475 476
	*p_bytes_used = buffer_size;
	return 0;
}
477

478 479 480
u32 cx231xx_get_video_line(struct cx231xx *dev,
			   struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
			   u8 *p_buffer, u32 buffer_size)
481 482 483 484 485 486
{
	u32 bytes_copied = 0;
	int current_field = -1;

	switch (sav_eav) {
	case SAV_ACTIVE_VIDEO_FIELD1:
487 488 489 490 491 492 493 494 495
		/* looking for skipped line which occurred in PAL 720x480 mode.
		   In this case, there will be no active data contained
		   between the SAV and EAV */
		if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
		    (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
		    ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
		     (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
		     (p_buffer[3] == EAV_VBLANK_FIELD1) ||
		     (p_buffer[3] == EAV_VBLANK_FIELD2)))
496 497 498
			return bytes_copied;
		current_field = 1;
		break;
499

500
	case SAV_ACTIVE_VIDEO_FIELD2:
501 502 503 504 505 506 507 508 509
		/* looking for skipped line which occurred in PAL 720x480 mode.
		   In this case, there will be no active data contained between
		   the SAV and EAV */
		if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
		    (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
		    ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
		     (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
		     (p_buffer[3] == EAV_VBLANK_FIELD1)       ||
		     (p_buffer[3] == EAV_VBLANK_FIELD2)))
510 511 512 513
			return bytes_copied;
		current_field = 2;
		break;
	}
514

515
	dma_q->last_sav = sav_eav;
516

517 518
	bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
					       buffer_size, current_field);
519 520

	return bytes_copied;
521 522
}

523 524
u32 cx231xx_copy_video_line(struct cx231xx *dev,
			    struct cx231xx_dmaqueue *dma_q, u8 *p_line,
525
			    u32 length, int field_number)
526
{
527 528 529
	u32 bytes_to_copy;
	struct cx231xx_buffer *buf;
	u32 _line_size = dev->width * 2;
530

531
	if (dma_q->current_field != field_number)
532
		cx231xx_reset_video_buffer(dev, dma_q);
533

534 535
	/* get the buffer pointer */
	buf = dev->video_mode.isoc_ctl.buf;
536

537 538
	/* Remember the field number for next time */
	dma_q->current_field = field_number;
539

540 541 542
	bytes_to_copy = dma_q->bytes_left_in_line;
	if (bytes_to_copy > length)
		bytes_to_copy = length;
543

544 545
	if (dma_q->lines_completed >= dma_q->lines_per_field) {
		dma_q->bytes_left_in_line -= bytes_to_copy;
546 547
		dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
					  0 : 1;
548 549
		return 0;
	}
550

551
	dma_q->is_partial_line = 1;
552

553 554 555 556
	/* If we don't have a buffer, just return the number of bytes we would
	   have copied if we had a buffer. */
	if (!buf) {
		dma_q->bytes_left_in_line -= bytes_to_copy;
557 558
		dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
					 ? 0 : 1;
559 560
		return bytes_to_copy;
	}
561

562 563
	/* copy the data to video buffer */
	cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
564

565 566
	dma_q->pos += bytes_to_copy;
	dma_q->bytes_left_in_line -= bytes_to_copy;
567

568 569 570 571
	if (dma_q->bytes_left_in_line == 0) {
		dma_q->bytes_left_in_line = _line_size;
		dma_q->lines_completed++;
		dma_q->is_partial_line = 0;
572

573 574
		if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
			buffer_filled(dev, dma_q, buf);
575

576 577 578 579 580
			dma_q->pos = 0;
			buf = NULL;
			dma_q->lines_completed = 0;
		}
	}
581

582
	return bytes_to_copy;
583 584
}

585 586
void cx231xx_reset_video_buffer(struct cx231xx *dev,
				struct cx231xx_dmaqueue *dma_q)
587
{
588
	struct cx231xx_buffer *buf;
589

590 591
	/* handle the switch from field 1 to field 2 */
	if (dma_q->current_field == 1) {
592
		if (dma_q->lines_completed >= dma_q->lines_per_field)
593
			dma_q->field1_done = 1;
594
		else
595 596
			dma_q->field1_done = 0;
	}
597

598
	buf = dev->video_mode.isoc_ctl.buf;
599

600 601 602 603
	if (buf == NULL) {
		u8 *outp = NULL;
		/* first try to get the buffer */
		get_next_buf(dma_q, &buf);
604

605 606
		if (buf)
			outp = videobuf_to_vmalloc(&buf->vb);
607

608 609 610 611
		dma_q->pos = 0;
		dma_q->field1_done = 0;
		dma_q->current_field = -1;
	}
612

613 614 615
	/* reset the counters */
	dma_q->bytes_left_in_line = dev->width << 1;
	dma_q->lines_completed = 0;
616 617 618
}

int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
619
		    u8 *p_buffer, u32 bytes_to_copy)
620
{
621 622 623 624 625 626
	u8 *p_out_buffer = NULL;
	u32 current_line_bytes_copied = 0;
	struct cx231xx_buffer *buf;
	u32 _line_size = dev->width << 1;
	void *startwrite;
	int offset, lencopy;
627

628
	buf = dev->video_mode.isoc_ctl.buf;
629

630 631
	if (buf == NULL)
		return -1;
632 633 634

	p_out_buffer = videobuf_to_vmalloc(&buf->vb);

635
	current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
636

637 638
	/* Offset field 2 one line from the top of the buffer */
	offset = (dma_q->current_field == 1) ? 0 : _line_size;
639

640 641
	/* Offset for field 2 */
	startwrite = p_out_buffer + offset;
642

643 644
	/* lines already completed in the current field */
	startwrite += (dma_q->lines_completed * _line_size * 2);
645

646 647
	/* bytes already completed in the current line */
	startwrite += current_line_bytes_copied;
648

649 650
	lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
		  bytes_to_copy : dma_q->bytes_left_in_line;
651

652
	if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
653
		return 0;
654

655 656
	/* The below copies the UYVY data straight into video buffer */
	cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
657

658
	return 0;
659 660
}

661
void cx231xx_swab(u16 *from, u16 *to, u16 len)
662
{
663
	u16 i;
664

665 666
	if (len <= 0)
		return;
667

668
	for (i = 0; i < len / 2; i++)
669
		to[i] = (from[i] << 8) | (from[i] >> 8);
670 671
}

672
u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
673
{
674
	u8 buffer_complete = 0;
675

676
	/* Dual field stream */
677 678 679
	buffer_complete = ((dma_q->current_field == 2) &&
			   (dma_q->lines_completed >= dma_q->lines_per_field) &&
			    dma_q->field1_done);
680

681
	return buffer_complete;
682 683 684 685 686 687 688 689 690 691
}

/* ------------------------------------------------------------------
	Videobuf operations
   ------------------------------------------------------------------*/

static int
buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
{
	struct cx231xx_fh *fh = vq->priv_data;
692
	struct cx231xx *dev = fh->dev;
693 694
	struct v4l2_frequency f;

695
	*size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
696 697 698 699 700 701 702 703 704
	if (0 == *count)
		*count = CX231XX_DEF_BUF;

	if (*count < CX231XX_MIN_BUF)
		*count = CX231XX_MIN_BUF;

	/* Ask tuner to go to analog mode */
	memset(&f, 0, sizeof(f));
	f.frequency = dev->ctl_freq;
705
	f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
706

707
	call_all(dev, tuner, s_frequency, &f);
708 709 710 711 712 713 714

	return 0;
}

/* This is called *without* dev->slock held; please keep it that way */
static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
{
715 716
	struct cx231xx_fh *fh = vq->priv_data;
	struct cx231xx *dev = fh->dev;
717
	unsigned long flags = 0;
718

719 720 721 722 723 724 725 726 727 728 729
	if (in_interrupt())
		BUG();

	/* We used to wait for the buffer to finish here, but this didn't work
	   because, as we were keeping the state as VIDEOBUF_QUEUED,
	   videobuf_queue_cancel marked it as finished for us.
	   (Also, it could wedge forever if the hardware was misconfigured.)

	   This should be safe; by the time we get here, the buffer isn't
	   queued anymore. If we ever start marking the buffers as
	   VIDEOBUF_ACTIVE, it won't be, though.
730
	 */
731 732 733 734 735 736 737 738 739 740 741
	spin_lock_irqsave(&dev->video_mode.slock, flags);
	if (dev->video_mode.isoc_ctl.buf == buf)
		dev->video_mode.isoc_ctl.buf = NULL;
	spin_unlock_irqrestore(&dev->video_mode.slock, flags);

	videobuf_vmalloc_free(&buf->vb);
	buf->vb.state = VIDEOBUF_NEEDS_INIT;
}

static int
buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
742
	       enum v4l2_field field)
743
{
744 745 746 747 748
	struct cx231xx_fh *fh = vq->priv_data;
	struct cx231xx_buffer *buf =
	    container_of(vb, struct cx231xx_buffer, vb);
	struct cx231xx *dev = fh->dev;
	int rc = 0, urb_init = 0;
749 750

	/* The only currently supported format is 16 bits/pixel */
751 752
	buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
			+ 7) >> 3;
753
	if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
754 755
		return -EINVAL;

756
	buf->vb.width = dev->width;
757
	buf->vb.height = dev->height;
758
	buf->vb.field = field;
759 760 761 762 763 764 765 766 767 768 769 770

	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
		rc = videobuf_iolock(vq, &buf->vb, NULL);
		if (rc < 0)
			goto fail;
	}

	if (!dev->video_mode.isoc_ctl.num_bufs)
		urb_init = 1;

	if (urb_init) {
		rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
771 772 773
				       CX231XX_NUM_BUFS,
				       dev->video_mode.max_pkt_size,
				       cx231xx_isoc_copy);
774 775 776 777 778 779 780
		if (rc < 0)
			goto fail;
	}

	buf->vb.state = VIDEOBUF_PREPARED;
	return 0;

781
fail:
782 783 784 785
	free_buffer(vq, buf);
	return rc;
}

786
static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
787
{
788 789 790 791 792
	struct cx231xx_buffer *buf =
	    container_of(vb, struct cx231xx_buffer, vb);
	struct cx231xx_fh *fh = vq->priv_data;
	struct cx231xx *dev = fh->dev;
	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
793 794 795 796 797 798 799

	buf->vb.state = VIDEOBUF_QUEUED;
	list_add_tail(&buf->vb.queue, &vidq->active);

}

static void buffer_release(struct videobuf_queue *vq,
800
			   struct videobuf_buffer *vb)
801
{
802 803 804 805
	struct cx231xx_buffer *buf =
	    container_of(vb, struct cx231xx_buffer, vb);
	struct cx231xx_fh *fh = vq->priv_data;
	struct cx231xx *dev = (struct cx231xx *)fh->dev;
806 807 808 809 810 811 812

	cx231xx_isocdbg("cx231xx: called buffer_release\n");

	free_buffer(vq, buf);
}

static struct videobuf_queue_ops cx231xx_video_qops = {
813 814 815 816
	.buf_setup = buffer_setup,
	.buf_prepare = buffer_prepare,
	.buf_queue = buffer_queue,
	.buf_release = buffer_release,
817 818 819 820 821 822 823 824 825 826 827 828 829 830
};

/*********************  v4l2 interface  **************************************/

void video_mux(struct cx231xx *dev, int index)
{

	struct v4l2_routing route;

	route.input = INPUT(index)->vmux;
	route.output = 0;
	dev->video_input = index;
	dev->ctl_ainput = INPUT(index)->amux;

831
	cx231xx_set_video_input_mux(dev, index);
832

833
	cx25840_call(dev, video, s_routing, &route);
834

835
	cx231xx_set_audio_input(dev, dev->ctl_ainput);
836

837
	cx231xx_info("video_mux : %d\n", index);
838

839 840
	/* do mode control overrides if required */
	cx231xx_do_mode_ctrl_overrides(dev);
841 842 843 844 845
}

/* Usage lock check functions */
static int res_get(struct cx231xx_fh *fh)
{
846 847
	struct cx231xx *dev = fh->dev;
	int rc = 0;
848 849 850 851 852

	/* This instance already has stream_on */
	if (fh->stream_on)
		return rc;

853 854 855 856 857 858 859 860 861 862
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
		if (dev->stream_on)
			return -EBUSY;
		dev->stream_on = 1;
	} else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
		if (dev->vbi_stream_on)
			return -EBUSY;
		dev->vbi_stream_on = 1;
	} else
		return -EINVAL;
863

864
	fh->stream_on = 1;
865 866 867 868 869 870

	return rc;
}

static int res_check(struct cx231xx_fh *fh)
{
871
	return fh->stream_on;
872 873 874 875
}

static void res_free(struct cx231xx_fh *fh)
{
876
	struct cx231xx *dev = fh->dev;
877 878 879

	fh->stream_on = 0;

880 881 882 883
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		dev->stream_on = 0;
	if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
		dev->vbi_stream_on = 0;
884 885 886 887 888 889 890 891 892 893 894
}

static int check_dev(struct cx231xx *dev)
{
	if (dev->state & DEV_DISCONNECTED) {
		cx231xx_errdev("v4l2 ioctl: device not present\n");
		return -ENODEV;
	}

	if (dev->state & DEV_MISCONFIGURED) {
		cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
895
			       "close and open it again\n");
896 897 898 899 900 901
		return -EIO;
	}
	return 0;
}

void get_scale(struct cx231xx *dev,
902 903
	       unsigned int width, unsigned int height,
	       unsigned int *hscale, unsigned int *vscale)
904
{
905 906
	unsigned int maxw = norm_maxw(dev);
	unsigned int maxh = norm_maxh(dev);
907 908 909 910 911 912 913 914 915

	*hscale = (((unsigned long)maxw) << 12) / width - 4096L;
	if (*hscale >= 0x4000)
		*hscale = 0x3fff;

	*vscale = (((unsigned long)maxh) << 12) / height - 4096L;
	if (*vscale >= 0x4000)
		*vscale = 0x3fff;

916 917
	dev->hscale = *hscale;
	dev->vscale = *vscale;
918 919 920 921 922 923 924 925

}

/* ------------------------------------------------------------------
	IOCTL vidioc handling
   ------------------------------------------------------------------*/

static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
926
				struct v4l2_format *f)
927
{
928 929
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
930 931 932 933 934 935 936

	mutex_lock(&dev->lock);

	f->fmt.pix.width = dev->width;
	f->fmt.pix.height = dev->height;
	f->fmt.pix.pixelformat = dev->format->fourcc;;
	f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
937
	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
938 939 940 941 942
	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;

	f->fmt.pix.field = V4L2_FIELD_INTERLACED;

	mutex_unlock(&dev->lock);
943

944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
	return 0;
}

static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(format); i++)
		if (format[i].fourcc == fourcc)
			return &format[i];

	return NULL;
}

static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
959
				  struct v4l2_format *f)
960
{
961 962 963 964 965 966 967 968
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int width = f->fmt.pix.width;
	int height = f->fmt.pix.height;
	unsigned int maxw = norm_maxw(dev);
	unsigned int maxh = norm_maxh(dev);
	unsigned int hscale, vscale;
	struct cx231xx_fmt *fmt;
969 970 971 972

	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
	if (!fmt) {
		cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
973
				 f->fmt.pix.pixelformat);
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
		return -EINVAL;
	}

	/* width must even because of the YUYV format
	   height must be even because of interlacing */
	height &= 0xfffe;
	width &= 0xfffe;

	if (unlikely(height < 32))
		height = 32;
	if (unlikely(height > maxh))
		height = maxh;
	if (unlikely(width < 48))
		width = 48;
	if (unlikely(width > maxw))
		width = maxw;

	get_scale(dev, width, height, &hscale, &vscale);

	width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
	height = (((unsigned long)maxh) << 12) / (vscale + 4096L);

	f->fmt.pix.width = width;
	f->fmt.pix.height = height;
	f->fmt.pix.pixelformat = fmt->fourcc;
	f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
	f->fmt.pix.field = V4L2_FIELD_INTERLACED;

	return 0;
}

static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1008
				struct v4l2_format *f)
1009
{
1010 1011 1012 1013
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
	struct cx231xx_fmt *fmt;
1014 1015 1016 1017 1018 1019 1020

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	mutex_lock(&dev->lock);

1021
	vidioc_try_fmt_vid_cap(file, priv, f);
1022

1023
	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	if (!fmt) {
		rc = -EINVAL;
		goto out;
	}

	if (videobuf_queue_is_busy(&fh->vb_vidq)) {
		cx231xx_errdev("%s queue busy\n", __func__);
		rc = -EBUSY;
		goto out;
	}

	if (dev->stream_on && !fh->stream_on) {
		cx231xx_errdev("%s device in use by another fh\n", __func__);
		rc = -EBUSY;
		goto out;
	}

	/* set new image size */
	dev->width = f->fmt.pix.width;
	dev->height = f->fmt.pix.height;
1044
	dev->format = fmt;
1045 1046
	get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);

1047
	call_all(dev, video, s_fmt, f);
1048

1049
	/* Set the correct alternate setting for this resolution */
1050 1051
	cx231xx_resolution_set(dev);

1052
out:
1053 1054 1055 1056
	mutex_unlock(&dev->lock);
	return rc;
}

1057
static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
1058
{
1059 1060
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1061 1062 1063 1064 1065

	*id = dev->norm;
	return 0;
}

1066
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1067
{
1068 1069
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1070
	struct v4l2_format f;
1071
	int rc;
1072 1073 1074 1075 1076

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1077
	cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

	mutex_lock(&dev->lock);
	dev->norm = *norm;

	/* Adjusts width/height, if needed */
	f.fmt.pix.width = dev->width;
	f.fmt.pix.height = dev->height;
	vidioc_try_fmt_vid_cap(file, priv, &f);

	/* set new image size */
	dev->width = f.fmt.pix.width;
	dev->height = f.fmt.pix.height;
	get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);

1092
	call_all(dev, tuner, s_std, dev->norm);
1093 1094 1095

	mutex_unlock(&dev->lock);

1096
	cx231xx_resolution_set(dev);
1097

1098 1099
	/* do mode control overrides */
	cx231xx_do_mode_ctrl_overrides(dev);
1100 1101 1102 1103 1104 1105

	return 0;
}

static const char *iname[] = {
	[CX231XX_VMUX_COMPOSITE1] = "Composite1",
1106
	[CX231XX_VMUX_SVIDEO]     = "S-Video",
1107
	[CX231XX_VMUX_TELEVISION] = "Television",
1108 1109 1110
	[CX231XX_VMUX_CABLE]      = "Cable TV",
	[CX231XX_VMUX_DVB]        = "DVB",
	[CX231XX_VMUX_DEBUG]      = "for debug only",
1111 1112 1113
};

static int vidioc_enum_input(struct file *file, void *priv,
1114
			     struct v4l2_input *i)
1115
{
1116 1117 1118
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	unsigned int n;
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131

	n = i->index;
	if (n >= MAX_CX231XX_INPUT)
		return -EINVAL;
	if (0 == INPUT(n)->type)
		return -EINVAL;

	i->index = n;
	i->type = V4L2_INPUT_TYPE_CAMERA;

	strcpy(i->name, iname[INPUT(n)->type]);

	if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1132
	    (CX231XX_VMUX_CABLE == INPUT(n)->type))
1133 1134 1135 1136 1137 1138 1139 1140 1141
		i->type = V4L2_INPUT_TYPE_TUNER;

	i->std = dev->vdev->tvnorms;

	return 0;
}

static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
1142 1143
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1144 1145 1146 1147 1148 1149 1150 1151

	*i = dev->video_input;

	return 0;
}

static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
{
1152 1153 1154
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	if (i >= MAX_CX231XX_INPUT)
		return -EINVAL;
	if (0 == INPUT(i)->type)
		return -EINVAL;

	mutex_lock(&dev->lock);

	video_mux(dev, i);

	mutex_unlock(&dev->lock);
	return 0;
}

static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
{
1175 1176
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

	switch (a->index) {
	case CX231XX_AMUX_VIDEO:
		strcpy(a->name, "Television");
		break;
	case CX231XX_AMUX_LINE_IN:
		strcpy(a->name, "Line In");
		break;
	default:
		return -EINVAL;
	}

	a->index = dev->ctl_ainput;
	a->capability = V4L2_AUDCAP_STEREO;

	return 0;
}

static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
{
1197 1198 1199
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int status = 0;
1200 1201 1202 1203 1204 1205

	/* Doesn't allow manual routing */
	if (a->index != dev->ctl_ainput)
		return -EINVAL;

	dev->ctl_ainput = INPUT(a->index)->amux;
1206
	status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
1207 1208 1209 1210 1211

	return status;
}

static int vidioc_queryctrl(struct file *file, void *priv,
1212
			    struct v4l2_queryctrl *qc)
1213
{
1214 1215 1216 1217 1218
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int id = qc->id;
	int i;
	int rc;
1219 1220 1221 1222 1223

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1224
	qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
1225 1226 1227 1228 1229 1230 1231
	if (unlikely(qc->id == 0))
		return -EINVAL;

	memset(qc, 0, sizeof(*qc));

	qc->id = id;

1232
	if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
		return -EINVAL;

	for (i = 0; i < CX231XX_CTLS; i++)
		if (cx231xx_ctls[i].v.id == qc->id)
			break;

	if (i == CX231XX_CTLS) {
		*qc = no_ctl;
		return 0;
	}
	*qc = cx231xx_ctls[i].v;

	mutex_lock(&dev->lock);
1246
	call_all(dev, core, queryctrl, qc);
1247 1248 1249 1250 1251 1252 1253 1254 1255
	mutex_unlock(&dev->lock);

	if (qc->type)
		return 0;
	else
		return -EINVAL;
}

static int vidioc_g_ctrl(struct file *file, void *priv,
1256
			 struct v4l2_control *ctrl)
1257
{
1258 1259 1260
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1261 1262 1263 1264 1265 1266

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	mutex_lock(&dev->lock);
1267
	call_all(dev, core, g_ctrl, ctrl);
1268 1269 1270 1271 1272
	mutex_unlock(&dev->lock);
	return rc;
}

static int vidioc_s_ctrl(struct file *file, void *priv,
1273
			 struct v4l2_control *ctrl)
1274
{
1275 1276 1277
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1278 1279 1280 1281 1282 1283

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	mutex_lock(&dev->lock);
1284
	call_all(dev, core, s_ctrl, ctrl);
1285 1286 1287 1288
	mutex_unlock(&dev->lock);
	return rc;
}

1289
static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1290
{
1291 1292 1293
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	if (0 != t->index)
		return -EINVAL;

	strcpy(t->name, "Tuner");

1304
	t->type = V4L2_TUNER_ANALOG_TV;
1305
	t->capability = V4L2_TUNER_CAP_NORM;
1306 1307
	t->rangehigh = 0xffffffffUL;
	t->signal = 0xffff;	/* LOCKED */
1308 1309 1310 1311

	return 0;
}

1312
static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1313
{
1314 1315 1316
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1317 1318 1319 1320 1321 1322 1323 1324 1325

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	if (0 != t->index)
		return -EINVAL;
#if 0
	mutex_lock(&dev->lock);
1326
	call_all(dev, tuner, s_tuner, t);
1327 1328 1329 1330 1331 1332
	mutex_unlock(&dev->lock);
#endif
	return 0;
}

static int vidioc_g_frequency(struct file *file, void *priv,
1333
			      struct v4l2_frequency *f)
1334
{
1335 1336
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1337

1338
	mutex_lock(&dev->lock);
1339 1340 1341
	f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
	f->frequency = dev->ctl_freq;

1342
	call_all(dev, tuner, g_frequency, f);
1343

1344
	mutex_unlock(&dev->lock);
1345 1346 1347 1348 1349

	return 0;
}

static int vidioc_s_frequency(struct file *file, void *priv,
1350
			      struct v4l2_frequency *f)
1351
{
1352 1353 1354
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	if (0 != f->tuner)
		return -EINVAL;

	if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
		return -EINVAL;
	if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
		return -EINVAL;

1368 1369
	/* set pre channel change settings in DIF first */
	rc = cx231xx_tuner_pre_channel_change(dev);
1370 1371 1372 1373 1374

	mutex_lock(&dev->lock);

	dev->ctl_freq = f->frequency;

1375
	if (dev->tuner_type == TUNER_XC5000) {
1376
		if (dev->cx231xx_set_analog_freq != NULL)
1377
			dev->cx231xx_set_analog_freq(dev, f->frequency);
1378 1379
	} else
		call_all(dev, tuner, s_frequency, f);
1380 1381 1382

	mutex_unlock(&dev->lock);

1383 1384
	/* set post channel change settings in DIF first */
	rc = cx231xx_tuner_post_channel_change(dev);
1385

1386
	cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
1387 1388 1389 1390 1391 1392 1393

	return rc;
}

#ifdef CONFIG_VIDEO_ADV_DEBUG

/*
1394 1395
  -R, --list-registers=type=<host/i2cdrv/i2caddr>,
				chip=<chip>[,min=<addr>,max=<addr>]
1396
		     dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1397 1398
  -r, --set-register=type=<host/i2cdrv/i2caddr>,
				chip=<chip>,reg=<addr>,val=<val>
1399
		     set the register [VIDIOC_DBG_S_REGISTER]
1400 1401 1402 1403 1404 1405 1406 1407 1408

  if type == host, then <chip> is the hosts chip ID (default 0)
  if type == i2cdrv (default), then <chip> is the I2C driver name or ID
  if type == i2caddr, then <chip> is the 7-bit I2C address
*/

static int vidioc_g_register(struct file *file, void *priv,
			     struct v4l2_dbg_register *reg)
{
1409 1410
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1411
	int ret = 0;
1412 1413 1414 1415 1416 1417 1418
	u8 value[4] = { 0, 0, 0, 0 };
	u32 data = 0;

	switch (reg->match.type) {
	case V4L2_CHIP_MATCH_HOST:
		switch (reg->match.addr) {
		case 0:	/* Cx231xx - internal registers */
1419 1420 1421 1422
			ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
						  (u16)reg->reg, value, 4);
			reg->val = value[0] | value[1] << 8 |
				   value[2] << 16 | value[3] << 24;
1423 1424
			break;
		case 1:	/* Colibri - read byte */
1425 1426
			ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
						  (u16)reg->reg, 2, &data, 1);
1427 1428 1429
			reg->val = le32_to_cpu(data & 0xff);
			break;
		case 14:	/* Colibri - read dword */
1430 1431
			ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
						  (u16)reg->reg, 2, &data, 4);
1432 1433 1434
			reg->val = le32_to_cpu(data);
			break;
		case 2:	/* Hammerhead - read byte */
1435 1436
			ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						  (u16)reg->reg, 2, &data, 1);
1437 1438 1439
			reg->val = le32_to_cpu(data & 0xff);
			break;
		case 24:	/* Hammerhead - read dword */
1440 1441
			ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
						  (u16)reg->reg, 2, &data, 4);
1442 1443 1444
			reg->val = le32_to_cpu(data);
			break;
		case 3:	/* flatiron - read byte */
1445 1446 1447 1448
			ret = cx231xx_read_i2c_data(dev,
						    Flatrion_DEVICE_ADDRESS,
						    (u16)reg->reg, 1,
						    &data, 1);
1449 1450 1451 1452 1453
			reg->val = le32_to_cpu(data & 0xff);
			break;
		case 34:	/* flatiron - read dword */
			ret =
			    cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
1454
						  (u16)reg->reg, 1, &data, 4);
1455 1456 1457 1458
			reg->val = le32_to_cpu(data);
			break;
		}
		return ret < 0 ? ret : 0;
1459

1460
	case V4L2_CHIP_MATCH_I2C_DRIVER:
1461
		call_all(dev, core, g_register, reg);
1462 1463 1464 1465 1466 1467 1468 1469
		return 0;
	case V4L2_CHIP_MATCH_I2C_ADDR:
		/* Not supported yet */
		return -EINVAL;
	default:
		if (!v4l2_chip_match_host(&reg->match))
			return -EINVAL;
	}
1470

1471
	mutex_lock(&dev->lock);
1472
	call_all(dev, core, g_register, reg);
1473
	mutex_unlock(&dev->lock);
1474 1475 1476 1477 1478 1479 1480

	return ret;
}

static int vidioc_s_register(struct file *file, void *priv,
			     struct v4l2_dbg_register *reg)
{
1481 1482 1483
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int ret = 0;
1484
	__le64 buf;
1485 1486
	u32 value;
	u8 data[4] = { 0, 0, 0, 0 };
1487 1488 1489

	buf = cpu_to_le64(reg->val);

1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
	switch (reg->match.type) {
	case V4L2_CHIP_MATCH_HOST:
		{
			value = (u32) buf & 0xffffffff;

			switch (reg->match.addr) {
			case 0:	/* cx231xx internal registers */
				data[0] = (u8) value;
				data[1] = (u8) (value >> 8);
				data[2] = (u8) (value >> 16);
				data[3] = (u8) (value >> 24);
1501
				ret = cx231xx_write_ctrl_reg(dev,
1502
							   VRT_SET_REGISTER,
1503
							   (u16)reg->reg, data,
1504 1505 1506
							   4);
				break;
			case 1:	/* Colibri - read byte */
1507 1508 1509 1510
				ret = cx231xx_write_i2c_data(dev,
							Colibri_DEVICE_ADDRESS,
							(u16)reg->reg, 2,
							value, 1);
1511 1512
				break;
			case 14:	/* Colibri - read dword */
1513 1514 1515 1516
				ret = cx231xx_write_i2c_data(dev,
							Colibri_DEVICE_ADDRESS,
							(u16)reg->reg, 2,
							value, 4);
1517 1518 1519 1520
				break;
			case 2:	/* Hammerhead - read byte */
				ret =
				    cx231xx_write_i2c_data(dev,
1521 1522 1523
							HAMMERHEAD_I2C_ADDRESS,
							(u16)reg->reg, 2,
							value, 1);
1524 1525 1526 1527
				break;
			case 24:	/* Hammerhead - read dword */
				ret =
				    cx231xx_write_i2c_data(dev,
1528 1529 1530
							HAMMERHEAD_I2C_ADDRESS,
							(u16)reg->reg, 2,
							value, 4);
1531 1532 1533 1534
				break;
			case 3:	/* flatiron - read byte */
				ret =
				    cx231xx_write_i2c_data(dev,
1535 1536 1537
							Flatrion_DEVICE_ADDRESS,
							(u16)reg->reg, 1,
							value, 1);
1538 1539 1540 1541
				break;
			case 34:	/* flatiron - read dword */
				ret =
				    cx231xx_write_i2c_data(dev,
1542 1543 1544
							Flatrion_DEVICE_ADDRESS,
							(u16)reg->reg, 1,
							value, 4);
1545 1546 1547 1548 1549 1550 1551 1552
				break;
			}
		}
		return ret < 0 ? ret : 0;

	default:
		break;
	}
1553 1554

	mutex_lock(&dev->lock);
1555
	call_all(dev, core, s_register, reg);
1556
	mutex_unlock(&dev->lock);
1557

1558
	return ret;
1559 1560 1561 1562
}
#endif

static int vidioc_cropcap(struct file *file, void *priv,
1563
			  struct v4l2_cropcap *cc)
1564
{
1565 1566
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582

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

	cc->bounds.left = 0;
	cc->bounds.top = 0;
	cc->bounds.width = dev->width;
	cc->bounds.height = dev->height;
	cc->defrect = cc->bounds;
	cc->pixelaspect.numerator = 54;	/* 4:3 FIXME: remove magic numbers */
	cc->pixelaspect.denominator = 59;

	return 0;
}

static int vidioc_streamon(struct file *file, void *priv,
1583
			   enum v4l2_buf_type type)
1584
{
1585 1586 1587
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1588 1589 1590 1591 1592

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1593
	mutex_lock(&dev->lock);
1594 1595 1596 1597 1598
	rc = res_get(fh);

	if (likely(rc >= 0))
		rc = videobuf_streamon(&fh->vb_vidq);

1599 1600
	call_all(dev, video, s_stream, 1);

1601 1602 1603 1604 1605 1606
	mutex_unlock(&dev->lock);

	return rc;
}

static int vidioc_streamoff(struct file *file, void *priv,
1607
			    enum v4l2_buf_type type)
1608
{
1609 1610 1611
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1612 1613 1614 1615 1616

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1617 1618
	if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
	    (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1619 1620 1621 1622
		return -EINVAL;
	if (type != fh->type)
		return -EINVAL;

1623
	mutex_lock(&dev->lock);
1624

1625 1626
	cx25840_call(dev, video, s_stream, 0);

1627 1628 1629
	videobuf_streamoff(&fh->vb_vidq);
	res_free(fh);

1630
	mutex_unlock(&dev->lock);
1631 1632 1633 1634

	return 0;
}

1635 1636
static int vidioc_querycap(struct file *file, void *priv,
			   struct v4l2_capability *cap)
1637
{
1638 1639
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1640 1641 1642

	strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
	strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1643
	strlcpy(cap->bus_info, dev->v4l2_dev.name,
1644
		sizeof(cap->bus_info));
1645 1646 1647

	cap->version = CX231XX_VERSION_CODE;

1648
	cap->capabilities = V4L2_CAP_VBI_CAPTURE |
1649
#if 0
1650
		V4L2_CAP_SLICED_VBI_CAPTURE |
1651
#endif
1652 1653 1654 1655
		V4L2_CAP_VIDEO_CAPTURE	|
		V4L2_CAP_AUDIO		|
		V4L2_CAP_READWRITE	|
		V4L2_CAP_STREAMING;
1656 1657 1658 1659 1660 1661 1662

	if (dev->tuner_type != TUNER_ABSENT)
		cap->capabilities |= V4L2_CAP_TUNER;

	return 0;
}

1663 1664
static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
				   struct v4l2_fmtdesc *f)
1665
{
1666
	if (unlikely(f->index >= ARRAY_SIZE(format)))
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
		return -EINVAL;

	strlcpy(f->description, format[f->index].name, sizeof(f->description));
	f->pixelformat = format[f->index].fourcc;

	return 0;
}

/* Sliced VBI ioctls */
static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1677
				       struct v4l2_format *f)
1678
{
1679 1680 1681
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1682 1683 1684 1685 1686 1687 1688 1689 1690

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	mutex_lock(&dev->lock);

	f->fmt.sliced.service_set = 0;

1691
	call_all(dev, video, g_fmt, f);
1692 1693 1694 1695 1696 1697 1698 1699 1700

	if (f->fmt.sliced.service_set == 0)
		rc = -EINVAL;

	mutex_unlock(&dev->lock);
	return rc;
}

static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1701
					 struct v4l2_format *f)
1702
{
1703 1704 1705
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1706 1707 1708 1709 1710 1711

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	mutex_lock(&dev->lock);
1712
	call_all(dev, video, g_fmt, f);
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
	mutex_unlock(&dev->lock);

	if (f->fmt.sliced.service_set == 0)
		return -EINVAL;

	return 0;
}

/* RAW VBI ioctls */

static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1724
				struct v4l2_format *f)
1725
{
1726 1727
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1728 1729

	f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1730
	    35468950 : 28636363;
1731 1732 1733 1734
	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
	f->fmt.vbi.offset = 64 * 4;
	f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1735
	    PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1736
	f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1737
	    PAL_VBI_LINES : NTSC_VBI_LINES;
1738
	f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1739
	    PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1740 1741 1742 1743 1744 1745 1746
	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];

	return 0;

}

static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1747
				  struct v4l2_format *f)
1748
{
1749 1750
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1751

1752
	if (dev->vbi_stream_on && !fh->stream_on) {
1753 1754 1755 1756 1757 1758
		cx231xx_errdev("%s device in use by another fh\n", __func__);
		return -EBUSY;
	}

	f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
	f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
1759
	    35468950 : 28636363;
1760 1761 1762 1763 1764
	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
	f->fmt.vbi.offset = 244;
	f->fmt.vbi.flags = 0;
	f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1765
	    PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1766
	f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1767
	    PAL_VBI_LINES : NTSC_VBI_LINES;
1768
	f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1769
	    PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1770 1771 1772 1773 1774 1775 1776 1777 1778
	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];

	return 0;

}

static int vidioc_reqbufs(struct file *file, void *priv,
			  struct v4l2_requestbuffers *rb)
{
1779 1780 1781
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1782 1783 1784 1785 1786

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1787
	return videobuf_reqbufs(&fh->vb_vidq, rb);
1788 1789
}

1790
static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
1791
{
1792 1793 1794
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1795 1796 1797 1798 1799

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1800
	return videobuf_querybuf(&fh->vb_vidq, b);
1801 1802 1803 1804
}

static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
{
1805 1806 1807
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1808 1809 1810 1811 1812

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1813
	return videobuf_qbuf(&fh->vb_vidq, b);
1814 1815 1816 1817
}

static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
{
1818 1819 1820
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1821 1822 1823 1824 1825

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1826
	return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1827 1828 1829 1830 1831
}

#ifdef CONFIG_VIDEO_V4L1_COMPAT
static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
{
1832
	struct cx231xx_fh *fh = priv;
1833 1834 1835 1836 1837 1838 1839 1840 1841

	return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
}
#endif

/* ----------------------------------------------------------- */
/* RADIO ESPECIFIC IOCTLS                                      */
/* ----------------------------------------------------------- */

1842
static int radio_querycap(struct file *file, void *priv,
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
			  struct v4l2_capability *cap)
{
	struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;

	strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
	strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));

	cap->version = CX231XX_VERSION_CODE;
	cap->capabilities = V4L2_CAP_TUNER;
	return 0;
}

1856
static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1857 1858 1859 1860 1861 1862 1863 1864 1865
{
	struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;

	if (unlikely(t->index > 0))
		return -EINVAL;

	strcpy(t->name, "Radio");
	t->type = V4L2_TUNER_RADIO;

1866
	mutex_lock(&dev->lock);
1867
	call_all(dev, tuner, s_tuner, t);
1868
	mutex_unlock(&dev->lock);
1869 1870 1871 1872

	return 0;
}

1873
static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
{
	if (i->index != 0)
		return -EINVAL;
	strcpy(i->name, "Radio");
	i->type = V4L2_INPUT_TYPE_TUNER;

	return 0;
}

static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
{
	if (unlikely(a->index))
		return -EINVAL;

	strcpy(a->name, "Radio");
	return 0;
}

1892
static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1893 1894 1895 1896 1897 1898
{
	struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;

	if (0 != t->index)
		return -EINVAL;

1899
	mutex_lock(&dev->lock);
1900
	call_all(dev, tuner, s_tuner, t);
1901
	mutex_unlock(&dev->lock);
1902 1903 1904 1905

	return 0;
}

1906
static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
{
	return 0;
}

static int radio_s_input(struct file *file, void *fh, unsigned int i)
{
	return 0;
}

static int radio_queryctrl(struct file *file, void *priv,
			   struct v4l2_queryctrl *c)
{
	int i;

1921
	if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
		return -EINVAL;
	if (c->id == V4L2_CID_AUDIO_MUTE) {
		for (i = 0; i < CX231XX_CTLS; i++)
			if (cx231xx_ctls[i].v.id == c->id)
				break;
		*c = cx231xx_ctls[i].v;
	} else
		*c = no_ctl;
	return 0;
}

/*
 * cx231xx_v4l2_open()
 * inits the device and starts isoc transfer
 */
static int cx231xx_v4l2_open(struct file *filp)
{
1939
	int minor = video_devdata(filp)->minor;
1940 1941 1942 1943 1944
	int errCode = 0, radio = 0;
	struct cx231xx *dev = NULL;
	struct cx231xx_fh *fh;
	enum v4l2_buf_type fh_type = 0;

1945
	dev = cx231xx_get_device(minor, &fh_type, &radio);
1946 1947 1948 1949 1950 1951
	if (NULL == dev)
		return -ENODEV;

	mutex_lock(&dev->lock);

	cx231xx_videodbg("open minor=%d type=%s users=%d\n",
1952
			 minor, v4l2_type_names[fh_type], dev->users);
1953 1954 1955 1956

#if 0
	errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
	if (errCode < 0) {
1957 1958
		cx231xx_errdev
		    ("Device locked on digital mode. Can't open analog\n");
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
		mutex_unlock(&dev->lock);
		return -EBUSY;
	}
#endif

	fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
	if (!fh) {
		cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
		mutex_unlock(&dev->lock);
		return -ENOMEM;
	}
	fh->dev = dev;
	fh->radio = radio;
	fh->type = fh_type;
	filp->private_data = fh;

	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
		dev->width = norm_maxw(dev);
		dev->height = norm_maxh(dev);
		dev->hscale = 0;
		dev->vscale = 0;

1981 1982
		/* Power up in Analog TV mode */
		cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1983 1984 1985 1986 1987 1988

#if 0
		cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
#endif
		cx231xx_resolution_set(dev);

1989 1990
		/* set video alternate setting */
		cx231xx_set_video_alternate(dev);
1991 1992 1993 1994 1995 1996

		/* Needed, since GPIO might have disabled power of
		   some i2c device */
		cx231xx_config_i2c(dev);

		/* device needs to be initialized before isoc transfer */
1997 1998
		dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
		video_mux(dev, dev->video_input);
1999 2000 2001 2002 2003 2004 2005

	}
	if (fh->radio) {
		cx231xx_videodbg("video_open: setting radio device\n");

		/* cx231xx_start_radio(dev); */

2006
		call_all(dev, tuner, s_radio);
2007 2008 2009 2010
	}

	dev->users++;

2011 2012 2013 2014
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
					    NULL, &dev->video_mode.slock,
					    fh->type, V4L2_FIELD_INTERLACED,
2015 2016
					    sizeof(struct cx231xx_buffer), fh);
	if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2017 2018
		/* Set the required alternate setting  VBI interface works in
		   Bulk mode only */
2019
		cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2020

2021 2022 2023
		videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
					    NULL, &dev->vbi_mode.slock,
					    fh->type, V4L2_FIELD_SEQ_TB,
2024 2025
					    sizeof(struct cx231xx_buffer), fh);
	}
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050

	mutex_unlock(&dev->lock);

	return errCode;
}

/*
 * cx231xx_realease_resources()
 * unregisters the v4l2,i2c and usb devices
 * called when the device gets disconected or at module unload
*/
void cx231xx_release_analog_resources(struct cx231xx *dev)
{

	/*FIXME: I2C IR should be disconnected */

	if (dev->radio_dev) {
		if (-1 != dev->radio_dev->minor)
			video_unregister_device(dev->radio_dev);
		else
			video_device_release(dev->radio_dev);
		dev->radio_dev = NULL;
	}
	if (dev->vbi_dev) {
		cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
2051
			     dev->vbi_dev->num);
2052 2053 2054 2055 2056 2057 2058 2059
		if (-1 != dev->vbi_dev->minor)
			video_unregister_device(dev->vbi_dev);
		else
			video_device_release(dev->vbi_dev);
		dev->vbi_dev = NULL;
	}
	if (dev->vdev) {
		cx231xx_info("V4L2 device /dev/video%d deregistered\n",
2060
			     dev->vdev->num);
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
		if (-1 != dev->vdev->minor)
			video_unregister_device(dev->vdev);
		else
			video_device_release(dev->vdev);
		dev->vdev = NULL;
	}
}

/*
 * cx231xx_v4l2_close()
 * stops streaming and deallocates all resources allocated by the v4l2
 * calls and ioctls
 */
static int cx231xx_v4l2_close(struct file *filp)
{
2076 2077
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
2078 2079 2080

	cx231xx_videodbg("users=%d\n", dev->users);

2081
	mutex_lock(&dev->lock);
2082 2083 2084 2085

	if (res_check(fh))
		res_free(fh);

2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
	if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
		videobuf_stop(&fh->vb_vidq);
		videobuf_mmap_free(&fh->vb_vidq);

		/* the device is already disconnect,
		   free the remaining resources */
		if (dev->state & DEV_DISCONNECTED) {
			cx231xx_release_resources(dev);
			mutex_unlock(&dev->lock);
			kfree(dev);
			return 0;
		}

		/* do this before setting alternate! */
		cx231xx_uninit_vbi_isoc(dev);

		/* set alternate 0 */
2103
		if (!dev->vbi_or_sliced_cc_mode)
2104
			cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2105
		else
2106 2107 2108 2109 2110 2111 2112
			cx231xx_set_alt_setting(dev, INDEX_HANC, 0);

		kfree(fh);
		dev->users--;
		wake_up_interruptible_nr(&dev->open, 1);
		mutex_unlock(&dev->lock);
		return 0;
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
	}

	if (dev->users == 1) {
		videobuf_stop(&fh->vb_vidq);
		videobuf_mmap_free(&fh->vb_vidq);

		/* the device is already disconnect,
		   free the remaining resources */
		if (dev->state & DEV_DISCONNECTED) {
			cx231xx_release_resources(dev);
			mutex_unlock(&dev->lock);
			kfree(dev);
			return 0;
		}

2128
		/* Save some power by putting tuner to sleep */
2129
		call_all(dev, core, s_standby, 0);
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149

		/* do this before setting alternate! */
		cx231xx_uninit_isoc(dev);
		cx231xx_set_mode(dev, CX231XX_SUSPEND);

		/* set alternate 0 */
		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
	}
	kfree(fh);
	dev->users--;
	wake_up_interruptible_nr(&dev->open, 1);
	mutex_unlock(&dev->lock);
	return 0;
}

/*
 * cx231xx_v4l2_read()
 * will allocate buffers when called for the first time
 */
static ssize_t
2150 2151
cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
		  loff_t *pos)
2152 2153 2154 2155 2156 2157 2158 2159 2160
{
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
	int rc;

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

2161 2162
	if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
	    (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
2163 2164 2165 2166 2167 2168 2169 2170
		mutex_lock(&dev->lock);
		rc = res_get(fh);
		mutex_unlock(&dev->lock);

		if (unlikely(rc < 0))
			return rc;

		return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2171
					    filp->f_flags & O_NONBLOCK);
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	}
	return 0;
}

/*
 * cx231xx_v4l2_poll()
 * will allocate buffers when called for the first time
 */
static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
{
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
	int rc;

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

	mutex_lock(&dev->lock);
	rc = res_get(fh);
	mutex_unlock(&dev->lock);

	if (unlikely(rc < 0))
		return POLLERR;

2197 2198 2199 2200
	if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
	    (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
		return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
	else
2201 2202 2203 2204 2205 2206 2207 2208
		return POLLERR;
}

/*
 * cx231xx_v4l2_mmap()
 */
static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
{
2209 2210 2211
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
	int rc;
2212 2213 2214 2215 2216

	rc = check_dev(dev);
	if (rc < 0)
		return rc;

2217
	mutex_lock(&dev->lock);
2218 2219 2220 2221 2222 2223 2224 2225 2226
	rc = res_get(fh);
	mutex_unlock(&dev->lock);

	if (unlikely(rc < 0))
		return rc;

	rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);

	cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2227 2228 2229
			 (unsigned long)vma->vm_start,
			 (unsigned long)vma->vm_end -
			 (unsigned long)vma->vm_start, rc);
2230 2231 2232 2233 2234

	return rc;
}

static const struct v4l2_file_operations cx231xx_v4l_fops = {
2235 2236
	.owner   = THIS_MODULE,
	.open    = cx231xx_v4l2_open,
2237
	.release = cx231xx_v4l2_close,
2238 2239 2240 2241
	.read    = cx231xx_v4l2_read,
	.poll    = cx231xx_v4l2_poll,
	.mmap    = cx231xx_v4l2_mmap,
	.ioctl   = video_ioctl2,
2242 2243 2244
};

static const struct v4l2_ioctl_ops video_ioctl_ops = {
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
	.vidioc_querycap               = vidioc_querycap,
	.vidioc_enum_fmt_vid_cap       = vidioc_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap          = vidioc_g_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap        = vidioc_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap          = vidioc_s_fmt_vid_cap,
	.vidioc_g_fmt_vbi_cap          = vidioc_g_fmt_vbi_cap,
	.vidioc_try_fmt_vbi_cap        = vidioc_try_fmt_vbi_cap,
	.vidioc_s_fmt_vbi_cap          = vidioc_try_fmt_vbi_cap,
	.vidioc_g_audio                =  vidioc_g_audio,
	.vidioc_s_audio                = vidioc_s_audio,
	.vidioc_cropcap                = vidioc_cropcap,
	.vidioc_g_fmt_sliced_vbi_cap   = vidioc_g_fmt_sliced_vbi_cap,
2257
	.vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
	.vidioc_reqbufs                = vidioc_reqbufs,
	.vidioc_querybuf               = vidioc_querybuf,
	.vidioc_qbuf                   = vidioc_qbuf,
	.vidioc_dqbuf                  = vidioc_dqbuf,
	.vidioc_s_std                  = vidioc_s_std,
	.vidioc_g_std                  = vidioc_g_std,
	.vidioc_enum_input             = vidioc_enum_input,
	.vidioc_g_input                = vidioc_g_input,
	.vidioc_s_input                = vidioc_s_input,
	.vidioc_queryctrl              = vidioc_queryctrl,
	.vidioc_g_ctrl                 = vidioc_g_ctrl,
	.vidioc_s_ctrl                 = vidioc_s_ctrl,
	.vidioc_streamon               = vidioc_streamon,
	.vidioc_streamoff              = vidioc_streamoff,
	.vidioc_g_tuner                = vidioc_g_tuner,
	.vidioc_s_tuner                = vidioc_s_tuner,
	.vidioc_g_frequency            = vidioc_g_frequency,
	.vidioc_s_frequency            = vidioc_s_frequency,
2276
#ifdef CONFIG_VIDEO_ADV_DEBUG
2277 2278
	.vidioc_g_register             = vidioc_g_register,
	.vidioc_s_register             = vidioc_s_register,
2279 2280
#endif
#ifdef CONFIG_VIDEO_V4L1_COMPAT
2281
	.vidiocgmbuf                   = vidiocgmbuf,
2282 2283 2284 2285 2286 2287
#endif
};

static struct video_device cx231xx_vbi_template;

static const struct video_device cx231xx_video_template = {
2288 2289 2290 2291 2292
	.fops         = &cx231xx_v4l_fops,
	.release      = video_device_release,
	.ioctl_ops    = &video_ioctl_ops,
	.minor        = -1,
	.tvnorms      = V4L2_STD_ALL,
2293
	.current_norm = V4L2_STD_PAL,
2294 2295 2296
};

static const struct v4l2_file_operations radio_fops = {
2297 2298
	.owner   = THIS_MODULE,
	.open   = cx231xx_v4l2_open,
2299
	.release = cx231xx_v4l2_close,
2300
	.ioctl   = video_ioctl2,
2301 2302 2303
};

static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
	.vidioc_querycap    = radio_querycap,
	.vidioc_g_tuner     = radio_g_tuner,
	.vidioc_enum_input  = radio_enum_input,
	.vidioc_g_audio     = radio_g_audio,
	.vidioc_s_tuner     = radio_s_tuner,
	.vidioc_s_audio     = radio_s_audio,
	.vidioc_s_input     = radio_s_input,
	.vidioc_queryctrl   = radio_queryctrl,
	.vidioc_g_ctrl      = vidioc_g_ctrl,
	.vidioc_s_ctrl      = vidioc_s_ctrl,
2314 2315
	.vidioc_g_frequency = vidioc_g_frequency,
	.vidioc_s_frequency = vidioc_s_frequency,
2316
#ifdef CONFIG_VIDEO_ADV_DEBUG
2317 2318
	.vidioc_g_register  = vidioc_g_register,
	.vidioc_s_register  = vidioc_s_register,
2319 2320 2321 2322
#endif
};

static struct video_device cx231xx_radio_template = {
2323 2324
	.name      = "cx231xx-radio",
	.fops      = &radio_fops,
2325
	.ioctl_ops = &radio_ioctl_ops,
2326
	.minor     = -1,
2327 2328 2329 2330
};

/******************************** usb interface ******************************/

2331 2332 2333
static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
		const struct video_device
		*template, const char *type_name)
2334 2335 2336 2337 2338 2339
{
	struct video_device *vfd;

	vfd = video_device_alloc();
	if (NULL == vfd)
		return NULL;
2340

2341
	*vfd = *template;
2342
	vfd->minor = -1;
2343
	vfd->v4l2_dev = &dev->v4l2_dev;
2344 2345 2346
	vfd->release = video_device_release;
	vfd->debug = video_debug;

2347
	snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2348 2349 2350 2351 2352 2353 2354 2355

	return vfd;
}

int cx231xx_register_analog_devices(struct cx231xx *dev)
{
	int ret;

2356
	cx231xx_info("%s()\n", __func__);
2357 2358

	cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
2359 2360 2361 2362
		     dev->name,
		     (CX231XX_VERSION_CODE >> 16) & 0xff,
		     (CX231XX_VERSION_CODE >> 8) & 0xff,
		     CX231XX_VERSION_CODE & 0xff);
2363 2364

	/* set default norm */
2365
	/*dev->norm = cx231xx_video_template.current_norm; */
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
	dev->width = norm_maxw(dev);
	dev->height = norm_maxh(dev);
	dev->interlaced = 0;
	dev->hscale = 0;
	dev->vscale = 0;

	/* Analog specific initialization */
	dev->format = &format[0];
	/* video_mux(dev, dev->video_input); */

	/* Audio defaults */
	dev->mute = 1;
	dev->volume = 0x1f;

	/* enable vbi capturing */
2381
	/* write code here...  */
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391

	/* allocate and fill video video_device struct */
	dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
	if (!dev->vdev) {
		cx231xx_errdev("cannot allocate video_device.\n");
		return -ENODEV;
	}

	/* register v4l2 video video_device */
	ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2392
				    video_nr[dev->devno]);
2393
	if (ret) {
2394 2395
		cx231xx_errdev("unable to register video device (error=%i).\n",
			       ret);
2396 2397 2398
		return ret;
	}

2399 2400
	cx231xx_info("%s/0: registered device video%d [v4l2]\n",
		     dev->name, dev->vdev->num);
2401

2402 2403 2404 2405
	/* Initialize VBI template */
	memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
	       sizeof(cx231xx_vbi_template));
	strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2406 2407 2408 2409 2410 2411

	/* Allocate and fill vbi video_device struct */
	dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");

	/* register v4l2 vbi video_device */
	ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2412
				    vbi_nr[dev->devno]);
2413 2414 2415 2416 2417
	if (ret < 0) {
		cx231xx_errdev("unable to register vbi device\n");
		return ret;
	}

2418 2419
	cx231xx_info("%s/0: registered device vbi%d\n",
		     dev->name, dev->vbi_dev->num);
2420 2421

	if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2422 2423
		dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
						   "radio");
2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
		if (!dev->radio_dev) {
			cx231xx_errdev("cannot allocate video_device.\n");
			return -ENODEV;
		}
		ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
					    radio_nr[dev->devno]);
		if (ret < 0) {
			cx231xx_errdev("can't register radio device\n");
			return ret;
		}
		cx231xx_info("Registered radio device as /dev/radio%d\n",
2435
			     dev->radio_dev->num);
2436 2437 2438
	}

	cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2439
		     dev->vdev->num, dev->vbi_dev->num);
2440 2441 2442

	return 0;
}