cx231xx-video.c 54.4 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

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

25
#include "cx231xx.h"
26 27 28 29 30 31 32 33 34
#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/mm.h>
#include <linux/mutex.h>
35
#include <linux/slab.h>
36 37 38

#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
39
#include <media/v4l2-event.h>
40 41 42 43 44 45 46
#include <media/msp3400.h>
#include <media/tuner.h>

#include "dvb_frontend.h"

#include "cx231xx-vbi.h"

47
#define CX231XX_VERSION "0.0.2"
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#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");
72
MODULE_VERSION(CX231XX_VERSION);
73 74 75 76 77 78

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 };

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

84
MODULE_PARM_DESC(card, "card type");
85
MODULE_PARM_DESC(video_nr, "video device numbers");
86
MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87 88 89 90 91 92 93 94 95
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[] = {
	{
96 97 98 99 100
	 .name = "16bpp YUY2, 4:2:2, packed",
	 .fourcc = V4L2_PIX_FMT_YUYV,
	 .depth = 16,
	 .reg = 0,
	 },
101 102 103 104 105 106 107 108 109 110 111
};


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

/*
 * Announces that a buffer were filled and request the next
 */
static inline void buffer_filled(struct cx231xx *dev,
112 113
				 struct cx231xx_dmaqueue *dma_q,
				 struct cx231xx_buffer *buf)
114 115 116 117 118
{
	/* Advice that buffer was filled */
	cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
	buf->vb.state = VIDEOBUF_DONE;
	buf->vb.field_count++;
119
	v4l2_get_timestamp(&buf->vb.ts);
120

121 122 123 124
	if (dev->USE_ISO)
		dev->video_mode.isoc_ctl.buf = NULL;
	else
		dev->video_mode.bulk_ctl.buf = NULL;
125 126 127 128 129

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

130
static inline void print_err_status(struct cx231xx *dev, int packet, int status)
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
{
	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) {
161
		cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
162 163
	} else {
		cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
164
				packet, status, errmsg);
165 166 167 168 169 170 171
	}
}

/*
 * video-buf generic routine to get the next available buffer
 */
static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
172
				struct cx231xx_buffer **buf)
173
{
174 175 176
	struct cx231xx_video_mode *vmode =
	    container_of(dma_q, struct cx231xx_video_mode, vidq);
	struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
177 178 179 180 181

	char *outp;

	if (list_empty(&dma_q->active)) {
		cx231xx_isocdbg("No active queue to serve\n");
182 183 184 185
		if (dev->USE_ISO)
			dev->video_mode.isoc_ctl.buf = NULL;
		else
			dev->video_mode.bulk_ctl.buf = NULL;
186 187 188 189 190 191 192
		*buf = NULL;
		return;
	}

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

Lucas De Marchi's avatar
Lucas De Marchi committed
193
	/* Cleans up buffer - Useful for testing for frame/URB loss */
194 195 196
	outp = videobuf_to_vmalloc(&(*buf)->vb);
	memset(outp, 0, (*buf)->vb.size);

197 198 199 200
	if (dev->USE_ISO)
		dev->video_mode.isoc_ctl.buf = *buf;
	else
		dev->video_mode.bulk_ctl.buf = *buf;
201 202 203 204 205 206 207 208 209

	return;
}

/*
 * Controls the isoc copy of each urb packet
 */
static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
{
210
	struct cx231xx_dmaqueue *dma_q = urb->context;
211
	int i;
212
	unsigned char *p_buffer;
213 214
	u32 bytes_parsed = 0, buffer_size = 0;
	u8 sav_eav = 0;
215 216 217 218

	if (!dev)
		return 0;

219
	if (dev->state & DEV_DISCONNECTED)
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		return 0;

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

	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;
		}

237
		if (urb->iso_frame_desc[i].actual_length <= 0) {
238 239 240 241
			/* cx231xx_isocdbg("packet %d is empty",i); - spammy */
			continue;
		}
		if (urb->iso_frame_desc[i].actual_length >
242
		    dev->video_mode.max_pkt_size) {
243 244 245 246
			cx231xx_isocdbg("packet bigger than packet size");
			continue;
		}

247
		/*  get buffer pointer and length */
248
		p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
249 250 251 252
		buffer_size = urb->iso_frame_desc[i].actual_length;
		bytes_parsed = 0;

		if (dma_q->is_partial_line) {
253
			/* Handle the case of a partial line */
254 255
			sav_eav = dma_q->last_sav;
		} else {
256 257
			/* Check for a SAV/EAV overlapping
				the buffer boundary */
258 259 260 261 262
			sav_eav =
			    cx231xx_find_boundary_SAV_EAV(p_buffer,
							  dma_q->partial_buf,
							  &bytes_parsed);
		}
263

264
		sav_eav &= 0xF0;
265 266
		/* Get the first line if we have some portion of an SAV/EAV from
		   the last buffer or a partial line  */
267
		if (sav_eav) {
268
			bytes_parsed += cx231xx_get_video_line(dev, dma_q,
269 270 271
				sav_eav,	/* SAV/EAV */
				p_buffer + bytes_parsed,	/* p_buffer */
				buffer_size - bytes_parsed);/* buf size */
272
		}
273

274 275
		/* Now parse data that is completely in this buffer */
		/* dma_q->is_partial_line = 0;  */
276

277 278
		while (bytes_parsed < buffer_size) {
			u32 bytes_used = 0;
279

280 281 282 283
			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 */
284

285
			bytes_parsed += bytes_used;
286

287 288
			sav_eav &= 0xF0;
			if (sav_eav && (bytes_parsed < buffer_size)) {
289
				bytes_parsed += cx231xx_get_video_line(dev,
290 291 292
					dma_q, sav_eav,	/* SAV/EAV */
					p_buffer + bytes_parsed,/* p_buffer */
					buffer_size - bytes_parsed);/*buf size*/
293 294
			}
		}
295

296 297
		/* Save the last four bytes of the buffer so we can check the
		   buffer boundary condition next time */
298 299
		memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
		bytes_parsed = 0;
300

301
	}
302
	return 1;
303 304
}

305 306 307 308 309 310 311 312 313 314
static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
{
	struct cx231xx_dmaqueue *dma_q = urb->context;
	unsigned char *p_buffer;
	u32 bytes_parsed = 0, buffer_size = 0;
	u8 sav_eav = 0;

	if (!dev)
		return 0;

315
	if (dev->state & DEV_DISCONNECTED)
316 317 318 319 320 321 322 323 324 325 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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		return 0;

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

	if (1) {

		/*  get buffer pointer and length */
		p_buffer = urb->transfer_buffer;
		buffer_size = urb->actual_length;
		bytes_parsed = 0;

		if (dma_q->is_partial_line) {
			/* Handle the case of a partial line */
			sav_eav = dma_q->last_sav;
		} else {
			/* Check for a SAV/EAV overlapping
				the buffer boundary */
			sav_eav =
			    cx231xx_find_boundary_SAV_EAV(p_buffer,
							  dma_q->partial_buf,
							  &bytes_parsed);
		}

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

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

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

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

			bytes_parsed += bytes_used;

			sav_eav &= 0xF0;
			if (sav_eav && (bytes_parsed < buffer_size)) {
				bytes_parsed += cx231xx_get_video_line(dev,
					dma_q, sav_eav,	/* SAV/EAV */
					p_buffer + bytes_parsed,/* p_buffer */
					buffer_size - bytes_parsed);/*buf size*/
			}
		}

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

	}
381
	return 1;
382 383 384
}


385 386
u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
				 u32 *p_bytes_used)
387
{
388 389 390 391 392
	u32 bytes_used;
	u8 boundary_bytes[8];
	u8 sav_eav = 0;

	*p_bytes_used = 0;
393

394 395
	/* Create an array of the last 4 bytes of the last buffer and the first
	   4 bytes of the current buffer. */
396

397 398
	memcpy(boundary_bytes, partial_buf, 4);
	memcpy(boundary_bytes + 4, p_buffer, 4);
399

400
	/* Check for the SAV/EAV in the boundary buffer */
401 402
	sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
					    &bytes_used);
403

404 405 406 407 408
	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;
	}
409

410
	return sav_eav;
411 412
}

413
u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
414 415 416
{
	u32 i;
	u8 sav_eav = 0;
417

418 419 420 421 422
	/*
	 * 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.
	 */
423 424 425 426
	if (buffer_size < 4) {
		*p_bytes_used = buffer_size;
		return 0;
	}
427

428
	for (i = 0; i < (buffer_size - 3); i++) {
429

430 431
		if ((p_buffer[i] == 0xFF) &&
		    (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
432

433 434 435 436 437
			*p_bytes_used = i + 4;
			sav_eav = p_buffer[i + 3];
			return sav_eav;
		}
	}
438

439 440 441
	*p_bytes_used = buffer_size;
	return 0;
}
442

443 444 445
u32 cx231xx_get_video_line(struct cx231xx *dev,
			   struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
			   u8 *p_buffer, u32 buffer_size)
446 447 448 449 450 451
{
	u32 bytes_copied = 0;
	int current_field = -1;

	switch (sav_eav) {
	case SAV_ACTIVE_VIDEO_FIELD1:
452 453 454 455 456 457 458 459 460
		/* 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)))
461 462 463
			return bytes_copied;
		current_field = 1;
		break;
464

465
	case SAV_ACTIVE_VIDEO_FIELD2:
466 467 468 469 470 471 472 473 474
		/* 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)))
475 476 477 478
			return bytes_copied;
		current_field = 2;
		break;
	}
479

480
	dma_q->last_sav = sav_eav;
481

482 483
	bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
					       buffer_size, current_field);
484 485

	return bytes_copied;
486 487
}

488 489
u32 cx231xx_copy_video_line(struct cx231xx *dev,
			    struct cx231xx_dmaqueue *dma_q, u8 *p_line,
490
			    u32 length, int field_number)
491
{
492 493 494
	u32 bytes_to_copy;
	struct cx231xx_buffer *buf;
	u32 _line_size = dev->width * 2;
495

496
	if (dma_q->current_field != field_number)
497
		cx231xx_reset_video_buffer(dev, dma_q);
498

499
	/* get the buffer pointer */
500 501 502 503
	if (dev->USE_ISO)
		buf = dev->video_mode.isoc_ctl.buf;
	else
		buf = dev->video_mode.bulk_ctl.buf;
504

505 506
	/* Remember the field number for next time */
	dma_q->current_field = field_number;
507

508 509 510
	bytes_to_copy = dma_q->bytes_left_in_line;
	if (bytes_to_copy > length)
		bytes_to_copy = length;
511

512 513
	if (dma_q->lines_completed >= dma_q->lines_per_field) {
		dma_q->bytes_left_in_line -= bytes_to_copy;
514 515
		dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
					  0 : 1;
516 517
		return 0;
	}
518

519
	dma_q->is_partial_line = 1;
520

521 522 523 524
	/* 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;
525 526
		dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
					 ? 0 : 1;
527 528
		return bytes_to_copy;
	}
529

530 531
	/* copy the data to video buffer */
	cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
532

533 534
	dma_q->pos += bytes_to_copy;
	dma_q->bytes_left_in_line -= bytes_to_copy;
535

536 537 538 539
	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;
540

541 542
		if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
			buffer_filled(dev, dma_q, buf);
543

544 545 546 547 548
			dma_q->pos = 0;
			buf = NULL;
			dma_q->lines_completed = 0;
		}
	}
549

550
	return bytes_to_copy;
551 552
}

553 554
void cx231xx_reset_video_buffer(struct cx231xx *dev,
				struct cx231xx_dmaqueue *dma_q)
555
{
556
	struct cx231xx_buffer *buf;
557

558 559
	/* handle the switch from field 1 to field 2 */
	if (dma_q->current_field == 1) {
560
		if (dma_q->lines_completed >= dma_q->lines_per_field)
561
			dma_q->field1_done = 1;
562
		else
563 564
			dma_q->field1_done = 0;
	}
565

566 567 568 569
	if (dev->USE_ISO)
		buf = dev->video_mode.isoc_ctl.buf;
	else
		buf = dev->video_mode.bulk_ctl.buf;
570

571 572 573
	if (buf == NULL) {
		/* first try to get the buffer */
		get_next_buf(dma_q, &buf);
574

575 576 577 578
		dma_q->pos = 0;
		dma_q->field1_done = 0;
		dma_q->current_field = -1;
	}
579

580 581 582
	/* reset the counters */
	dma_q->bytes_left_in_line = dev->width << 1;
	dma_q->lines_completed = 0;
583 584 585
}

int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
586
		    u8 *p_buffer, u32 bytes_to_copy)
587
{
588 589 590 591 592 593
	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;
594

595 596 597 598
	if (dev->USE_ISO)
		buf = dev->video_mode.isoc_ctl.buf;
	else
		buf = dev->video_mode.bulk_ctl.buf;
599

600 601
	if (buf == NULL)
		return -1;
602 603 604

	p_out_buffer = videobuf_to_vmalloc(&buf->vb);

605
	current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
606

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

610 611
	/* Offset for field 2 */
	startwrite = p_out_buffer + offset;
612

613 614
	/* lines already completed in the current field */
	startwrite += (dma_q->lines_completed * _line_size * 2);
615

616 617
	/* bytes already completed in the current line */
	startwrite += current_line_bytes_copied;
618

619 620
	lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
		  bytes_to_copy : dma_q->bytes_left_in_line;
621

622
	if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
623
		return 0;
624

625 626
	/* The below copies the UYVY data straight into video buffer */
	cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
627

628
	return 0;
629 630
}

631
void cx231xx_swab(u16 *from, u16 *to, u16 len)
632
{
633
	u16 i;
634

635 636
	if (len <= 0)
		return;
637

638
	for (i = 0; i < len / 2; i++)
639
		to[i] = (from[i] << 8) | (from[i] >> 8);
640 641
}

642
u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
643
{
644
	u8 buffer_complete = 0;
645

646
	/* Dual field stream */
647 648 649
	buffer_complete = ((dma_q->current_field == 2) &&
			   (dma_q->lines_completed >= dma_q->lines_per_field) &&
			    dma_q->field1_done);
650

651
	return buffer_complete;
652 653 654 655 656 657 658 659 660 661
}

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

static int
buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
{
	struct cx231xx_fh *fh = vq->priv_data;
662
	struct cx231xx *dev = fh->dev;
663

664
	*size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
665 666 667 668 669 670 671 672 673 674 675 676
	if (0 == *count)
		*count = CX231XX_DEF_BUF;

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

	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)
{
677 678
	struct cx231xx_fh *fh = vq->priv_data;
	struct cx231xx *dev = fh->dev;
679
	unsigned long flags = 0;
680

681 682 683 684 685 686 687 688 689 690 691
	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.
692
	 */
693
	spin_lock_irqsave(&dev->video_mode.slock, flags);
694 695 696 697 698 699 700
	if (dev->USE_ISO) {
		if (dev->video_mode.isoc_ctl.buf == buf)
			dev->video_mode.isoc_ctl.buf = NULL;
	} else {
		if (dev->video_mode.bulk_ctl.buf == buf)
			dev->video_mode.bulk_ctl.buf = NULL;
	}
701 702 703 704 705 706 707 708
	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,
709
	       enum v4l2_field field)
710
{
711 712 713 714 715
	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;
716 717

	/* The only currently supported format is 16 bits/pixel */
718 719
	buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
			+ 7) >> 3;
720
	if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
721 722
		return -EINVAL;

723
	buf->vb.width = dev->width;
724
	buf->vb.height = dev->height;
725
	buf->vb.field = field;
726 727 728 729 730 731 732

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

733 734 735 736 737 738 739
	if (dev->USE_ISO) {
		if (!dev->video_mode.isoc_ctl.num_bufs)
			urb_init = 1;
	} else {
		if (!dev->video_mode.bulk_ctl.num_bufs)
			urb_init = 1;
	}
740
	/*pr_debug("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
741
		urb_init, dev->video_mode.max_pkt_size);*/
742
	if (urb_init) {
743 744 745
		dev->mode_tv = 0;
		if (dev->USE_ISO)
			rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
746 747 748
				       CX231XX_NUM_BUFS,
				       dev->video_mode.max_pkt_size,
				       cx231xx_isoc_copy);
749 750 751 752 753
		else
			rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
				       CX231XX_NUM_BUFS,
				       dev->video_mode.max_pkt_size,
				       cx231xx_bulk_copy);
754 755 756 757 758 759 760
		if (rc < 0)
			goto fail;
	}

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

761
fail:
762 763 764 765
	free_buffer(vq, buf);
	return rc;
}

766
static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
767
{
768 769 770 771 772
	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;
773 774 775 776 777 778 779

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

}

static void buffer_release(struct videobuf_queue *vq,
780
			   struct videobuf_buffer *vb)
781
{
782 783 784 785
	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;
786 787 788 789 790 791 792

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

	free_buffer(vq, buf);
}

static struct videobuf_queue_ops cx231xx_video_qops = {
793 794 795 796
	.buf_setup = buffer_setup,
	.buf_prepare = buffer_prepare,
	.buf_queue = buffer_queue,
	.buf_release = buffer_release,
797 798 799 800 801 802 803 804 805
};

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

void video_mux(struct cx231xx *dev, int index)
{
	dev->video_input = index;
	dev->ctl_ainput = INPUT(index)->amux;

806
	cx231xx_set_video_input_mux(dev, index);
807

808
	cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
809

810
	cx231xx_set_audio_input(dev, dev->ctl_ainput);
811

812
	pr_debug("video_mux : %d\n", index);
813

814 815
	/* do mode control overrides if required */
	cx231xx_do_mode_ctrl_overrides(dev);
816 817 818 819 820
}

/* Usage lock check functions */
static int res_get(struct cx231xx_fh *fh)
{
821 822
	struct cx231xx *dev = fh->dev;
	int rc = 0;
823 824 825 826 827

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

828 829 830 831 832 833 834 835 836 837
	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;
838

839
	fh->stream_on = 1;
840 841 842 843 844 845

	return rc;
}

static int res_check(struct cx231xx_fh *fh)
{
846
	return fh->stream_on;
847 848 849 850
}

static void res_free(struct cx231xx_fh *fh)
{
851
	struct cx231xx *dev = fh->dev;
852 853 854

	fh->stream_on = 0;

855 856 857 858
	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;
859 860 861 862 863
}

static int check_dev(struct cx231xx *dev)
{
	if (dev->state & DEV_DISCONNECTED) {
864
		pr_err("v4l2 ioctl: device not present\n");
865 866 867 868 869 870 871 872 873 874
		return -ENODEV;
	}
	return 0;
}

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

static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
875
				struct v4l2_format *f)
876
{
877 878
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
879 880 881

	f->fmt.pix.width = dev->width;
	f->fmt.pix.height = dev->height;
882 883
	f->fmt.pix.pixelformat = dev->format->fourcc;
	f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
884
	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;

	f->fmt.pix.field = V4L2_FIELD_INTERLACED;

	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,
904
				  struct v4l2_format *f)
905
{
906 907
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
908 909
	unsigned int width = f->fmt.pix.width;
	unsigned int height = f->fmt.pix.height;
910 911 912
	unsigned int maxw = norm_maxw(dev);
	unsigned int maxh = norm_maxh(dev);
	struct cx231xx_fmt *fmt;
913 914 915 916

	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
	if (!fmt) {
		cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
917
				 f->fmt.pix.pixelformat);
918 919 920 921 922
		return -EINVAL;
	}

	/* width must even because of the YUYV format
	   height must be even because of interlacing */
923
	v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
924 925 926 927

	f->fmt.pix.width = width;
	f->fmt.pix.height = height;
	f->fmt.pix.pixelformat = fmt->fourcc;
928
	f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
929 930 931 932 933 934 935 936
	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,
937
				struct v4l2_format *f)
938
{
939 940 941 942
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
	struct cx231xx_fmt *fmt;
943
	struct v4l2_mbus_framefmt mbus_fmt;
944 945 946 947 948

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

949
	vidioc_try_fmt_vid_cap(file, priv, f);
950

951
	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
952 953
	if (!fmt)
		return -EINVAL;
954 955

	if (videobuf_queue_is_busy(&fh->vb_vidq)) {
956
		pr_err("%s: queue busy\n", __func__);
957
		return -EBUSY;
958 959 960
	}

	if (dev->stream_on && !fh->stream_on) {
961
		pr_err("%s: device in use by another fh\n", __func__);
962
		return -EBUSY;
963 964 965 966 967
	}

	/* set new image size */
	dev->width = f->fmt.pix.width;
	dev->height = f->fmt.pix.height;
968
	dev->format = fmt;
969

970 971 972
	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
	call_all(dev, video, s_mbus_fmt, &mbus_fmt);
	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
973 974 975 976

	return rc;
}

977
static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
978
{
979 980
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
981 982 983 984 985

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

986
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
987
{
988 989
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
990
	struct v4l2_mbus_framefmt mbus_fmt;
991
	int rc;
992 993 994 995 996

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

997
	if (dev->norm == norm)
998 999 1000 1001
		return 0;

	if (videobuf_queue_is_busy(&fh->vb_vidq))
		return -EBUSY;
1002

1003
	dev->norm = norm;
1004 1005

	/* Adjusts width/height, if needed */
1006 1007
	dev->width = 720;
	dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
1008

1009
	call_all(dev, video, s_std, dev->norm);
1010 1011 1012 1013

	/* We need to reset basic properties in the decoder related to
	   resolution (since a standard change effects things like the number
	   of lines in VACT, etc) */
1014 1015 1016 1017
	memset(&mbus_fmt, 0, sizeof(mbus_fmt));
	mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
	mbus_fmt.width = dev->width;
	mbus_fmt.height = dev->height;
1018
	call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1019

1020 1021
	/* do mode control overrides */
	cx231xx_do_mode_ctrl_overrides(dev);
1022 1023 1024 1025 1026 1027

	return 0;
}

static const char *iname[] = {
	[CX231XX_VMUX_COMPOSITE1] = "Composite1",
1028
	[CX231XX_VMUX_SVIDEO]     = "S-Video",
1029
	[CX231XX_VMUX_TELEVISION] = "Television",
1030 1031 1032
	[CX231XX_VMUX_CABLE]      = "Cable TV",
	[CX231XX_VMUX_DVB]        = "DVB",
	[CX231XX_VMUX_DEBUG]      = "for debug only",
1033 1034
};

1035
int cx231xx_enum_input(struct file *file, void *priv,
1036
			     struct v4l2_input *i)
1037
{
1038 1039
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1040 1041
	u32 gen_stat;
	unsigned int ret, n;
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

	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) ||
1055
	    (CX231XX_VMUX_CABLE == INPUT(n)->type))
1056 1057 1058 1059
		i->type = V4L2_INPUT_TYPE_TUNER;

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

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	/* If they are asking about the active input, read signal status */
	if (n == dev->video_input) {
		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
					    GEN_STAT, 2, &gen_stat, 4);
		if (ret > 0) {
			if ((gen_stat & FLD_VPRES) == 0x00)
				i->status |= V4L2_IN_ST_NO_SIGNAL;
			if ((gen_stat & FLD_HLOCK) == 0x00)
				i->status |= V4L2_IN_ST_NO_H_LOCK;
		}
	}

1072 1073 1074
	return 0;
}

1075
int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
1076
{
1077 1078
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1079 1080 1081 1082 1083 1084

	*i = dev->video_input;

	return 0;
}

1085
int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
1086
{
1087 1088 1089
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1090

1091
	dev->mode_tv = 0;
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	rc = check_dev(dev);
	if (rc < 0)
		return rc;

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

	video_mux(dev, i);

1103 1104 1105 1106 1107
	if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
	    INPUT(i)->type == CX231XX_VMUX_CABLE) {
		/* There's a tuner, so reset the standard and put it on the
		   last known frequency (since it was probably powered down
		   until now */
1108
		call_all(dev, video, s_std, dev->norm);
1109 1110
	}

1111 1112 1113
	return 0;
}

1114
int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1115
{
1116 1117 1118
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

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

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

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

1129
	t->type = V4L2_TUNER_ANALOG_TV;
1130
	t->capability = V4L2_TUNER_CAP_NORM;
1131 1132
	t->rangehigh = 0xffffffffUL;
	t->signal = 0xffff;	/* LOCKED */
1133
	call_all(dev, tuner, g_tuner, t);
1134 1135 1136 1137

	return 0;
}

1138
int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1139
{
1140 1141 1142
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1143 1144 1145 1146 1147 1148 1149 1150

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

	if (0 != t->index)
		return -EINVAL;
#if 0
1151
	call_all(dev, tuner, s_tuner, t);
1152 1153 1154 1155
#endif
	return 0;
}

1156
int cx231xx_g_frequency(struct file *file, void *priv,
1157
			      struct v4l2_frequency *f)
1158
{
1159 1160
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1161

1162 1163 1164
	if (f->tuner)
		return -EINVAL;

1165 1166 1167 1168 1169
	f->frequency = dev->ctl_freq;

	return 0;
}

1170
int cx231xx_s_frequency(struct file *file, void *priv,
1171
			      const struct v4l2_frequency *f)
1172
{
1173 1174
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1175
	struct v4l2_frequency new_freq = *f;
1176
	int rc;
1177 1178
	u32 if_frequency = 5400000;

1179
	pr_debug("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1180
		 f->frequency, f->type);
1181 1182 1183 1184 1185 1186 1187 1188

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

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

1189 1190
	/* set pre channel change settings in DIF first */
	rc = cx231xx_tuner_pre_channel_change(dev);
1191

1192
	call_all(dev, tuner, s_frequency, f);
1193 1194
	call_all(dev, tuner, g_frequency, &new_freq);
	dev->ctl_freq = new_freq.frequency;
1195

1196 1197
	/* set post channel change settings in DIF first */
	rc = cx231xx_tuner_post_channel_change(dev);
1198

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	if (dev->tuner_type == TUNER_NXP_TDA18271) {
		if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
			if_frequency = 5400000;  /*5.4MHz	*/
		else if (dev->norm & V4L2_STD_B)
			if_frequency = 6000000;  /*6.0MHz	*/
		else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
			if_frequency = 6900000;  /*6.9MHz	*/
		else if (dev->norm & V4L2_STD_GH)
			if_frequency = 7100000;  /*7.1MHz	*/
		else if (dev->norm & V4L2_STD_PAL_I)
			if_frequency = 7250000;  /*7.25MHz	*/
		else if (dev->norm & V4L2_STD_SECAM_L)
			if_frequency = 6900000;  /*6.9MHz	*/
		else if (dev->norm & V4L2_STD_SECAM_LC)
			if_frequency = 1250000;  /*1.25MHz	*/

1215
		pr_debug("if_frequency is set to %d\n", if_frequency);
1216 1217 1218 1219 1220
		cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);

		update_HH_register_after_set_DIF(dev);
	}

1221
	pr_debug("Set New FREQUENCY to %d\n", f->frequency);
1222 1223 1224 1225

	return rc;
}

1226 1227 1228 1229
#ifdef CONFIG_VIDEO_ADV_DEBUG

int cx231xx_g_chip_info(struct file *file, void *fh,
			struct v4l2_dbg_chip_info *chip)
1230
{
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	switch (chip->match.addr) {
	case 0:	/* Cx231xx - internal registers */
		return 0;
	case 1:	/* AFE - read byte */
		strlcpy(chip->name, "AFE (byte)", sizeof(chip->name));
		return 0;
	case 2:	/* Video Block - read byte */
		strlcpy(chip->name, "Video (byte)", sizeof(chip->name));
		return 0;
	case 3:	/* I2S block - read byte */
		strlcpy(chip->name, "I2S (byte)", sizeof(chip->name));
		return 0;
	case 4: /* AFE - read dword */
		strlcpy(chip->name, "AFE (dword)", sizeof(chip->name));
		return 0;
	case 5: /* Video Block - read dword */
		strlcpy(chip->name, "Video (dword)", sizeof(chip->name));
		return 0;
	case 6: /* I2S Block - read dword */
		strlcpy(chip->name, "I2S (dword)", sizeof(chip->name));
1251 1252 1253 1254 1255
		return 0;
	}
	return -EINVAL;
}

1256
int cx231xx_g_register(struct file *file, void *priv,
1257 1258
			     struct v4l2_dbg_register *reg)
{
1259 1260
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1261
	int ret;
1262 1263 1264
	u8 value[4] = { 0, 0, 0, 0 };
	u32 data = 0;

1265 1266 1267 1268 1269 1270
	switch (reg->match.addr) {
	case 0:	/* Cx231xx - internal registers */
		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;
1271
		reg->size = 4;
1272 1273 1274 1275 1276
		break;
	case 1:	/* AFE - read byte */
		ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
				(u16)reg->reg, 2, &data, 1);
		reg->val = data;
1277
		reg->size = 1;
1278 1279 1280 1281 1282
		break;
	case 2:	/* Video Block - read byte */
		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
				(u16)reg->reg, 2, &data, 1);
		reg->val = data;
1283
		reg->size = 1;
1284 1285 1286 1287 1288
		break;
	case 3:	/* I2S block - read byte */
		ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
				(u16)reg->reg, 1, &data, 1);
		reg->val = data;
1289
		reg->size = 1;
1290 1291 1292 1293 1294
		break;
	case 4: /* AFE - read dword */
		ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
				(u16)reg->reg, 2, &data, 4);
		reg->val = data;
1295
		reg->size = 4;
1296 1297 1298 1299 1300
		break;
	case 5: /* Video Block - read dword */
		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
				(u16)reg->reg, 2, &data, 4);
		reg->val = data;
1301
		reg->size = 4;
1302 1303 1304 1305 1306
		break;
	case 6: /* I2S Block - read dword */
		ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
				(u16)reg->reg, 1, &data, 4);
		reg->val = data;
1307
		reg->size = 4;
1308
		break;
1309
	default:
1310
		return -EINVAL;
1311
	}
1312
	return ret < 0 ? ret : 0;
1313 1314
}

1315
int cx231xx_s_register(struct file *file, void *priv,
1316
			     const struct v4l2_dbg_register *reg)
1317
{
1318 1319
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1320
	int ret;
1321
	u8 data[4] = { 0, 0, 0, 0 };
1322

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	switch (reg->match.addr) {
	case 0:	/* cx231xx internal registers */
		data[0] = (u8) reg->val;
		data[1] = (u8) (reg->val >> 8);
		data[2] = (u8) (reg->val >> 16);
		data[3] = (u8) (reg->val >> 24);
		ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
				(u16)reg->reg, data, 4);
		break;
	case 1:	/* AFE - write byte */
		ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
				(u16)reg->reg, 2, reg->val, 1);
		break;
	case 2:	/* Video Block - write byte */
		ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
				(u16)reg->reg, 2, reg->val, 1);
		break;
	case 3:	/* I2S block - write byte */
		ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
				(u16)reg->reg, 1, reg->val, 1);
		break;
	case 4: /* AFE - write dword */
		ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
				(u16)reg->reg, 2, reg->val, 4);
		break;
	case 5: /* Video Block - write dword */
		ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
				(u16)reg->reg, 2, reg->val, 4);
1351
		break;
1352 1353 1354 1355 1356 1357
	case 6: /* I2S block - write dword */
		ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
				(u16)reg->reg, 1, reg->val, 4);
		break;
	default:
		return -EINVAL;
1358
	}
1359
	return ret < 0 ? ret : 0;
1360 1361 1362 1363
}
#endif

static int vidioc_cropcap(struct file *file, void *priv,
1364
			  struct v4l2_cropcap *cc)
1365
{
1366 1367
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

	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,
1384
			   enum v4l2_buf_type type)
1385
{
1386 1387 1388
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

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

	rc = res_get(fh);

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

1399 1400
	call_all(dev, video, s_stream, 1);

1401 1402 1403 1404
	return rc;
}

static int vidioc_streamoff(struct file *file, void *priv,
1405
			    enum v4l2_buf_type type)
1406
{
1407 1408 1409
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1410 1411 1412 1413 1414 1415 1416 1417

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

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

1418 1419
	cx25840_call(dev, video, s_stream, 0);

1420 1421 1422 1423 1424 1425
	videobuf_streamoff(&fh->vb_vidq);
	res_free(fh);

	return 0;
}

1426
int cx231xx_querycap(struct file *file, void *priv,
1427
			   struct v4l2_capability *cap)
1428
{
1429
	struct video_device *vdev = video_devdata(file);
1430 1431
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1432 1433 1434

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

1437 1438 1439
	if (vdev->vfl_type == VFL_TYPE_RADIO)
		cap->device_caps = V4L2_CAP_RADIO;
	else {
1440
		cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1441 1442 1443 1444 1445
		if (vdev->vfl_type == VFL_TYPE_VBI)
			cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
		else
			cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
	}
1446
	if (dev->tuner_type != TUNER_ABSENT)
1447
		cap->device_caps |= V4L2_CAP_TUNER;
1448
	cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
1449
		V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1450 1451 1452
		V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
	if (dev->radio_dev)
		cap->capabilities |= V4L2_CAP_RADIO;
1453 1454 1455 1456

	return 0;
}

1457 1458
static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
				   struct v4l2_fmtdesc *f)
1459
{
1460
	if (unlikely(f->index >= ARRAY_SIZE(format)))
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
		return -EINVAL;

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

	return 0;
}

/* RAW VBI ioctls */

static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1472
				struct v4l2_format *f)
1473
{
1474 1475
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1476

1477
	f->fmt.vbi.sampling_rate = 6750000 * 4;
1478 1479
	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1480
	f->fmt.vbi.offset = 0;
1481
	f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1482
	    PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1483
	f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1484
	    PAL_VBI_LINES : NTSC_VBI_LINES;
1485
	f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1486
	    PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1487
	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1488
	memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1489 1490 1491 1492 1493 1494

	return 0;

}

static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1495
				  struct v4l2_format *f)
1496
{
1497 1498
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
1499

1500
	f->fmt.vbi.sampling_rate = 6750000 * 4;
1501 1502
	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1503
	f->fmt.vbi.offset = 0;
1504 1505
	f->fmt.vbi.flags = 0;
	f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1506
	    PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1507
	f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1508
	    PAL_VBI_LINES : NTSC_VBI_LINES;
1509
	f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1510
	    PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1511
	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1512
	memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1513 1514 1515 1516 1517

	return 0;

}

1518 1519 1520 1521 1522 1523 1524
static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
				  struct v4l2_format *f)
{
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;

	if (dev->vbi_stream_on && !fh->stream_on) {
1525
		pr_err("%s device in use by another fh\n", __func__);
1526 1527 1528 1529 1530
		return -EBUSY;
	}
	return vidioc_try_fmt_vbi_cap(file, priv, f);
}

1531 1532 1533
static int vidioc_reqbufs(struct file *file, void *priv,
			  struct v4l2_requestbuffers *rb)
{
1534 1535 1536
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1537 1538 1539 1540 1541

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

1542
	return videobuf_reqbufs(&fh->vb_vidq, rb);
1543 1544
}

1545
static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
1546
{
1547 1548 1549
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1550 1551 1552 1553 1554

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

1555
	return videobuf_querybuf(&fh->vb_vidq, b);
1556 1557 1558 1559
}

static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
{
1560 1561 1562
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1563 1564 1565 1566 1567

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

1568
	return videobuf_qbuf(&fh->vb_vidq, b);
1569 1570 1571 1572
}

static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
{
1573 1574 1575
	struct cx231xx_fh *fh = priv;
	struct cx231xx *dev = fh->dev;
	int rc;
1576 1577 1578 1579 1580

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

1581
	return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1582 1583 1584 1585 1586 1587
}

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

1588
static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1589 1590 1591
{
	struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;

1592
	if (t->index)
1593 1594 1595 1596
		return -EINVAL;

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

1597
	call_all(dev, tuner, g_tuner, t);
1598 1599 1600

	return 0;
}
1601
static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1602 1603 1604
{
	struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;

1605
	if (t->index)
1606 1607
		return -EINVAL;

1608
	call_all(dev, tuner, s_tuner, t);
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618

	return 0;
}

/*
 * cx231xx_v4l2_open()
 * inits the device and starts isoc transfer
 */
static int cx231xx_v4l2_open(struct file *filp)
{
1619
	int radio = 0;
1620 1621
	struct video_device *vdev = video_devdata(filp);
	struct cx231xx *dev = video_drvdata(filp);
1622 1623 1624
	struct cx231xx_fh *fh;
	enum v4l2_buf_type fh_type = 0;

1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
	switch (vdev->vfl_type) {
	case VFL_TYPE_GRABBER:
		fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		break;
	case VFL_TYPE_VBI:
		fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
		break;
	case VFL_TYPE_RADIO:
		radio = 1;
		break;
	}
1636

1637 1638 1639
	cx231xx_videodbg("open dev=%s type=%s users=%d\n",
			 video_device_node_name(vdev), v4l2_type_names[fh_type],
			 dev->users);
1640 1641 1642 1643

#if 0
	errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
	if (errCode < 0) {
1644
		pr_err("Device locked on digital mode. Can't open analog\n");
1645 1646 1647 1648 1649 1650
		return -EBUSY;
	}
#endif

	fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
	if (!fh) {
1651
		pr_err("cx231xx-video.c: Out of memory?!\n");
1652 1653
		return -ENOMEM;
	}
1654 1655 1656 1657
	if (mutex_lock_interruptible(&dev->lock)) {
		kfree(fh);
		return -ERESTARTSYS;
	}
1658 1659 1660
	fh->dev = dev;
	fh->type = fh_type;
	filp->private_data = fh;
1661
	v4l2_fh_init(&fh->fh, vdev);
1662 1663

	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1664
		/* Power up in Analog TV mode */
1665
		if (dev->board.external_av)
1666 1667 1668 1669
			cx231xx_set_power_mode(dev,
				 POLARIS_AVMODE_ENXTERNAL_AV);
		else
			cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1670 1671 1672 1673 1674

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

1675 1676
		/* set video alternate setting */
		cx231xx_set_video_alternate(dev);
1677 1678 1679 1680 1681 1682

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

		/* device needs to be initialized before isoc transfer */
1683
		dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1684 1685

	}
1686
	if (radio) {
1687 1688 1689 1690
		cx231xx_videodbg("video_open: setting radio device\n");

		/* cx231xx_start_radio(dev); */

1691
		call_all(dev, tuner, s_radio);
1692 1693 1694 1695
	}

	dev->users++;

1696 1697 1698 1699
	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,
1700
					    sizeof(struct cx231xx_buffer),
1701
					    fh, &dev->lock);
1702
	if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1703 1704
		/* Set the required alternate setting  VBI interface works in
		   Bulk mode only */
1705
		cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1706

1707 1708 1709
		videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
					    NULL, &dev->vbi_mode.slock,
					    fh->type, V4L2_FIELD_SEQ_TB,
1710
					    sizeof(struct cx231xx_buffer),
1711
					    fh, &dev->lock);
1712
	}
1713
	mutex_unlock(&dev->lock);
1714
	v4l2_fh_add(&fh->fh);
1715

1716
	return 0;
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
}

/*
 * 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) {
1730
		if (video_is_registered(dev->radio_dev))
1731 1732 1733 1734 1735 1736
			video_unregister_device(dev->radio_dev);
		else
			video_device_release(dev->radio_dev);
		dev->radio_dev = NULL;
	}
	if (dev->vbi_dev) {
1737
		pr_info("V4L2 device %s deregistered\n",
1738
			video_device_node_name(dev->vbi_dev));
1739
		if (video_is_registered(dev->vbi_dev))
1740 1741 1742 1743 1744 1745
			video_unregister_device(dev->vbi_dev);
		else
			video_device_release(dev->vbi_dev);
		dev->vbi_dev = NULL;
	}
	if (dev->vdev) {
1746
		pr_info("V4L2 device %s deregistered\n",
1747
			video_device_node_name(dev->vdev));
1748

1749
		if (dev->board.has_417)
1750 1751
			cx231xx_417_unregister(dev);

1752
		if (video_is_registered(dev->vdev))
1753 1754 1755 1756 1757
			video_unregister_device(dev->vdev);
		else
			video_device_release(dev->vdev);
		dev->vdev = NULL;
	}
1758 1759
	v4l2_ctrl_handler_free(&dev->ctrl_handler);
	v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1760 1761 1762
}

/*
1763
 * cx231xx_close()
1764 1765 1766
 * stops streaming and deallocates all resources allocated by the v4l2
 * calls and ioctls
 */
1767
static int cx231xx_close(struct file *filp)
1768
{
1769 1770
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
1771 1772 1773

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

1774
	cx231xx_videodbg("users=%d\n", dev->users);
1775 1776 1777
	if (res_check(fh))
		res_free(fh);

1778 1779 1780 1781 1782 1783 1784
	/*
	 * To workaround error number=-71 on EP0 for VideoGrabber,
	 *	 need exclude following.
	 * FIXME: It is probably safe to remove most of these, as we're
	 * now avoiding the alternate setting for INDEX_VANC
	 */
	if (!dev->board.no_alt_vanc)
1785 1786 1787 1788 1789 1790 1791 1792 1793
		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) {
				if (atomic_read(&dev->devlist_count) > 0) {
					cx231xx_release_resources(dev);
1794
					fh->dev = NULL;
1795 1796 1797 1798
					return 0;
				}
				return 0;
			}
1799

1800 1801 1802 1803 1804 1805 1806 1807 1808
			/* do this before setting alternate! */
			cx231xx_uninit_vbi_isoc(dev);

			/* set alternate 0 */
			if (!dev->vbi_or_sliced_cc_mode)
				cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
			else
				cx231xx_set_alt_setting(dev, INDEX_HANC, 0);

1809 1810
			v4l2_fh_del(&fh->fh);
			v4l2_fh_exit(&fh->fh);
1811 1812 1813
			kfree(fh);
			dev->users--;
			wake_up_interruptible_nr(&dev->open, 1);
1814 1815 1816
			return 0;
		}

1817
	v4l2_fh_del(&fh->fh);
1818 1819
	dev->users--;
	if (!dev->users) {
1820 1821 1822 1823 1824 1825 1826
		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);
1827
			fh->dev = NULL;
1828 1829 1830
			return 0;
		}

1831
		/* Save some power by putting tuner to sleep */
1832
		call_all(dev, core, s_power, 0);
1833 1834

		/* do this before setting alternate! */
1835 1836 1837 1838
		if (dev->USE_ISO)
			cx231xx_uninit_isoc(dev);
		else
			cx231xx_uninit_bulk(dev);
1839 1840 1841 1842 1843
		cx231xx_set_mode(dev, CX231XX_SUSPEND);

		/* set alternate 0 */
		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
	}
1844
	v4l2_fh_exit(&fh->fh);
1845 1846 1847 1848 1849
	kfree(fh);
	wake_up_interruptible_nr(&dev->open, 1);
	return 0;
}

1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
static int cx231xx_v4l2_close(struct file *filp)
{
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
	int rc;

	mutex_lock(&dev->lock);
	rc = cx231xx_close(filp);
	mutex_unlock(&dev->lock);
	return rc;
}

1862 1863 1864 1865 1866
/*
 * cx231xx_v4l2_read()
 * will allocate buffers when called for the first time
 */
static ssize_t
1867 1868
cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
		  loff_t *pos)
1869 1870 1871 1872 1873 1874 1875 1876 1877
{
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
	int rc;

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

1878 1879
	if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
	    (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
1880 1881 1882 1883 1884
		rc = res_get(fh);

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

1885 1886 1887
		if (mutex_lock_interruptible(&dev->lock))
			return -ERESTARTSYS;
		rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1888
					    filp->f_flags & O_NONBLOCK);
1889 1890
		mutex_unlock(&dev->lock);
		return rc;
1891 1892 1893 1894 1895 1896 1897 1898
	}
	return 0;
}

/*
 * cx231xx_v4l2_poll()
 * will allocate buffers when called for the first time
 */
1899
static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
1900
{
1901
	unsigned long req_events = poll_requested_events(wait);
1902 1903
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
1904
	unsigned res = 0;
1905 1906 1907 1908
	int rc;

	rc = check_dev(dev);
	if (rc < 0)
1909
		return POLLERR;
1910 1911 1912 1913 1914 1915

	rc = res_get(fh);

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

1916 1917 1918 1919 1920 1921 1922 1923
	if (v4l2_event_pending(&fh->fh))
		res |= POLLPRI;
	else
		poll_wait(filp, &fh->fh.wait, wait);

	if (!(req_events & (POLLIN | POLLRDNORM)))
		return res;

1924
	if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
1925 1926
	    (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
		mutex_lock(&dev->lock);
1927
		res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1928 1929 1930
		mutex_unlock(&dev->lock);
		return res;
	}
1931
	return res | POLLERR;
1932 1933 1934 1935 1936 1937 1938
}

/*
 * cx231xx_v4l2_mmap()
 */
static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
{
1939 1940 1941
	struct cx231xx_fh *fh = filp->private_data;
	struct cx231xx *dev = fh->dev;
	int rc;
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951

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

	rc = res_get(fh);

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

1952 1953
	if (mutex_lock_interruptible(&dev->lock))
		return -ERESTARTSYS;
1954
	rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1955
	mutex_unlock(&dev->lock);
1956 1957

	cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1958 1959 1960
			 (unsigned long)vma->vm_start,
			 (unsigned long)vma->vm_end -
			 (unsigned long)vma->vm_start, rc);
1961 1962 1963 1964 1965

	return rc;
}

static const struct v4l2_file_operations cx231xx_v4l_fops = {
1966 1967
	.owner   = THIS_MODULE,
	.open    = cx231xx_v4l2_open,
1968
	.release = cx231xx_v4l2_close,
1969 1970 1971
	.read    = cx231xx_v4l2_read,
	.poll    = cx231xx_v4l2_poll,
	.mmap    = cx231xx_v4l2_mmap,
1972
	.unlocked_ioctl   = video_ioctl2,
1973 1974 1975
};

static const struct v4l2_ioctl_ops video_ioctl_ops = {
1976
	.vidioc_querycap               = cx231xx_querycap,
1977 1978 1979 1980 1981 1982
	.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,
1983
	.vidioc_s_fmt_vbi_cap          = vidioc_s_fmt_vbi_cap,
1984 1985 1986 1987 1988 1989 1990
	.vidioc_cropcap                = vidioc_cropcap,
	.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,
1991 1992 1993
	.vidioc_enum_input             = cx231xx_enum_input,
	.vidioc_g_input                = cx231xx_g_input,
	.vidioc_s_input                = cx231xx_s_input,
1994 1995
	.vidioc_streamon               = vidioc_streamon,
	.vidioc_streamoff              = vidioc_streamoff,
1996 1997 1998 1999
	.vidioc_g_tuner                = cx231xx_g_tuner,
	.vidioc_s_tuner                = cx231xx_s_tuner,
	.vidioc_g_frequency            = cx231xx_g_frequency,
	.vidioc_s_frequency            = cx231xx_s_frequency,
2000
#ifdef CONFIG_VIDEO_ADV_DEBUG
2001
	.vidioc_g_chip_info            = cx231xx_g_chip_info,
2002 2003
	.vidioc_g_register             = cx231xx_g_register,
	.vidioc_s_register             = cx231xx_s_register,
2004
#endif
2005 2006
	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2007 2008 2009 2010 2011
};

static struct video_device cx231xx_vbi_template;

static const struct video_device cx231xx_video_template = {
2012 2013 2014 2015
	.fops         = &cx231xx_v4l_fops,
	.release      = video_device_release,
	.ioctl_ops    = &video_ioctl_ops,
	.tvnorms      = V4L2_STD_ALL,
2016 2017 2018
};

static const struct v4l2_file_operations radio_fops = {
2019 2020
	.owner   = THIS_MODULE,
	.open   = cx231xx_v4l2_open,
2021
	.release = cx231xx_v4l2_close,
2022
	.poll = v4l2_ctrl_poll,
2023
	.unlocked_ioctl = video_ioctl2,
2024 2025 2026
};

static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2027
	.vidioc_querycap    = cx231xx_querycap,
2028 2029
	.vidioc_g_tuner     = radio_g_tuner,
	.vidioc_s_tuner     = radio_s_tuner,
2030 2031
	.vidioc_g_frequency = cx231xx_g_frequency,
	.vidioc_s_frequency = cx231xx_s_frequency,
2032
#ifdef CONFIG_VIDEO_ADV_DEBUG
2033
	.vidioc_g_chip_info = cx231xx_g_chip_info,
2034 2035
	.vidioc_g_register  = cx231xx_g_register,
	.vidioc_s_register  = cx231xx_s_register,
2036
#endif
2037 2038
	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2039 2040 2041
};

static struct video_device cx231xx_radio_template = {
2042 2043
	.name      = "cx231xx-radio",
	.fops      = &radio_fops,
2044
	.ioctl_ops = &radio_ioctl_ops,
2045 2046 2047 2048
};

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

2049 2050 2051
static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
		const struct video_device
		*template, const char *type_name)
2052 2053 2054 2055 2056 2057
{
	struct video_device *vfd;

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

2059
	*vfd = *template;
2060
	vfd->v4l2_dev = &dev->v4l2_dev;
2061 2062
	vfd->release = video_device_release;
	vfd->debug = video_debug;
2063
	vfd->lock = &dev->lock;
2064

2065
	snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2066

2067
	video_set_drvdata(vfd, dev);
2068 2069 2070 2071 2072 2073
	if (dev->tuner_type == TUNER_ABSENT) {
		v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
		v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
		v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
		v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
	}
2074 2075 2076 2077 2078 2079 2080
	return vfd;
}

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

2081
	pr_info("v4l2 driver version %s\n", CX231XX_VERSION);
2082 2083

	/* set default norm */
2084
	dev->norm = V4L2_STD_PAL;
2085 2086 2087 2088 2089 2090
	dev->width = norm_maxw(dev);
	dev->height = norm_maxh(dev);
	dev->interlaced = 0;

	/* Analog specific initialization */
	dev->format = &format[0];
2091 2092 2093

	/* Set the initial input */
	video_mux(dev, dev->video_input);
2094

2095
	call_all(dev, video, s_std, dev->norm);
2096

2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
	v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
	v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);

	if (dev->sd_cx25840) {
		v4l2_ctrl_add_handler(&dev->ctrl_handler,
				dev->sd_cx25840->ctrl_handler, NULL);
		v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
				dev->sd_cx25840->ctrl_handler,
				v4l2_ctrl_radio_filter);
	}

	if (dev->ctrl_handler.error)
		return dev->ctrl_handler.error;
	if (dev->radio_ctrl_handler.error)
		return dev->radio_ctrl_handler.error;
2112 2113

	/* enable vbi capturing */
2114
	/* write code here...  */
2115 2116 2117 2118

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

2123
	dev->vdev->ctrl_handler = &dev->ctrl_handler;
2124 2125
	/* register v4l2 video video_device */
	ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2126
				    video_nr[dev->devno]);
2127
	if (ret) {
2128
		pr_err("unable to register video device (error=%i).\n",
2129
			ret);
2130 2131 2132
		return ret;
	}

2133 2134
	pr_info("Registered video device %s [v4l2]\n",
		video_device_node_name(dev->vdev));
2135

2136
	/* Initialize VBI template */
2137
	cx231xx_vbi_template = cx231xx_video_template;
2138
	strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2139 2140 2141 2142

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

2143
	if (!dev->vbi_dev) {
2144
		pr_err("cannot allocate video_device.\n");
2145 2146 2147
		return -ENODEV;
	}
	dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
2148 2149
	/* register v4l2 vbi video_device */
	ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2150
				    vbi_nr[dev->devno]);
2151
	if (ret < 0) {
2152
		pr_err("unable to register vbi device\n");
2153 2154 2155
		return ret;
	}

2156 2157
	pr_info("Registered VBI device %s\n",
		video_device_node_name(dev->vbi_dev));
2158 2159

	if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2160 2161
		dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
						   "radio");
2162
		if (!dev->radio_dev) {
2163
			pr_err("cannot allocate video_device.\n");
2164 2165
			return -ENODEV;
		}
2166
		dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
2167 2168 2169
		ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
					    radio_nr[dev->devno]);
		if (ret < 0) {
2170
			pr_err("can't register radio device\n");
2171 2172
			return ret;
		}
2173
		pr_info("Registered radio device as %s\n",
2174
			video_device_node_name(dev->radio_dev));
2175 2176 2177 2178
	}

	return 0;
}