au0828-video.c 50.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Auvitek AU0828 USB Bridge (Analog video support)
 *
 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
 * Copyright (C) 2005-2008 Auvitek International, Ltd.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/* Developer Notes:
 *
 * VBI support is not yet working
 * The hardware scaler supported is unimplemented
 * AC97 audio support is unimplemented (only i2s audio mode)
 *
 */

#include <linux/module.h>
32
#include <linux/slab.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#include <linux/init.h>
#include <linux/device.h>
#include <linux/suspend.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-chip-ident.h>
#include <media/tuner.h>
#include "au0828.h"
#include "au0828-reg.h"

static DEFINE_MUTEX(au0828_sysfs_lock);

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

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

#define au0828_isocdbg(fmt, arg...) \
do {\
	if (isoc_debug) { \
		printk(KERN_INFO "au0828 %s :"fmt, \
		       __func__ , ##arg);	   \
	} \
  } while (0)

static inline void print_err_status(struct au0828_dev *dev,
				    int packet, int status)
{
	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) {
		au0828_isocdbg("URB status %d [%s].\n",	status, errmsg);
	} else {
		au0828_isocdbg("URB packet %d, status %d [%s].\n",
			       packet, status, errmsg);
	}
}

static int check_dev(struct au0828_dev *dev)
{
	if (dev->dev_state & DEV_DISCONNECTED) {
103
		printk(KERN_INFO "v4l2 ioctl: device not present\n");
104 105 106 107
		return -ENODEV;
	}

	if (dev->dev_state & DEV_MISCONFIGURED) {
108
		printk(KERN_INFO "v4l2 ioctl: device is misconfigured; "
109 110 111 112 113 114 115 116 117 118 119 120 121
		       "close and open it again\n");
		return -EIO;
	}
	return 0;
}

/*
 * IRQ callback, called by URB callback
 */
static void au0828_irq_callback(struct urb *urb)
{
	struct au0828_dmaqueue  *dma_q = urb->context;
	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
122
	unsigned long flags = 0;
123
	int i;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

	switch (urb->status) {
	case 0:             /* success */
	case -ETIMEDOUT:    /* NAK */
		break;
	case -ECONNRESET:   /* kill */
	case -ENOENT:
	case -ESHUTDOWN:
		au0828_isocdbg("au0828_irq_callback called: status kill\n");
		return;
	default:            /* unknown error */
		au0828_isocdbg("urb completition error %d.\n", urb->status);
		break;
	}

	/* Copy data from URB */
140
	spin_lock_irqsave(&dev->slock, flags);
141
	dev->isoc_ctl.isoc_copy(dev, urb);
142
	spin_unlock_irqrestore(&dev->slock, flags);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	/* Reset urb buffers */
	for (i = 0; i < urb->number_of_packets; i++) {
		urb->iso_frame_desc[i].status = 0;
		urb->iso_frame_desc[i].actual_length = 0;
	}
	urb->status = 0;

	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
	if (urb->status) {
		au0828_isocdbg("urb resubmit failed (error=%i)\n",
			       urb->status);
	}
}

/*
 * Stop and Deallocate URBs
 */
161
static void au0828_uninit_isoc(struct au0828_dev *dev)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
{
	struct urb *urb;
	int i;

	au0828_isocdbg("au0828: called au0828_uninit_isoc\n");

	dev->isoc_ctl.nfields = -1;
	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
		urb = dev->isoc_ctl.urb[i];
		if (urb) {
			if (!irqs_disabled())
				usb_kill_urb(urb);
			else
				usb_unlink_urb(urb);

			if (dev->isoc_ctl.transfer_buffer[i]) {
178
				usb_free_coherent(dev->usbdev,
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
					urb->transfer_buffer_length,
					dev->isoc_ctl.transfer_buffer[i],
					urb->transfer_dma);
			}
			usb_free_urb(urb);
			dev->isoc_ctl.urb[i] = NULL;
		}
		dev->isoc_ctl.transfer_buffer[i] = NULL;
	}

	kfree(dev->isoc_ctl.urb);
	kfree(dev->isoc_ctl.transfer_buffer);

	dev->isoc_ctl.urb = NULL;
	dev->isoc_ctl.transfer_buffer = NULL;
	dev->isoc_ctl.num_bufs = 0;
}

/*
 * Allocate URBs and start IRQ
 */
200 201 202
static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
			    int num_bufs, int max_pkt_size,
			    int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
{
	struct au0828_dmaqueue *dma_q = &dev->vidq;
	int i;
	int sb_size, pipe;
	struct urb *urb;
	int j, k;
	int rc;

	au0828_isocdbg("au0828: called au0828_prepare_isoc\n");

	/* De-allocates all pending stuff */
	au0828_uninit_isoc(dev);

	dev->isoc_ctl.isoc_copy = isoc_copy;
	dev->isoc_ctl.num_bufs = num_bufs;

	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
	if (!dev->isoc_ctl.urb) {
		au0828_isocdbg("cannot alloc memory for usb buffers\n");
		return -ENOMEM;
	}

	dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
					      GFP_KERNEL);
	if (!dev->isoc_ctl.transfer_buffer) {
		au0828_isocdbg("cannot allocate memory for usb transfer\n");
		kfree(dev->isoc_ctl.urb);
		return -ENOMEM;
	}

	dev->isoc_ctl.max_pkt_size = max_pkt_size;
	dev->isoc_ctl.buf = NULL;

	sb_size = max_packets * dev->isoc_ctl.max_pkt_size;

	/* allocate urbs and transfer buffers */
	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
		if (!urb) {
			au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
			au0828_uninit_isoc(dev);
			return -ENOMEM;
		}
		dev->isoc_ctl.urb[i] = urb;

248
		dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
			sb_size, GFP_KERNEL, &urb->transfer_dma);
		if (!dev->isoc_ctl.transfer_buffer[i]) {
			printk("unable to allocate %i bytes for transfer"
					" buffer %i%s\n",
					sb_size, i,
					in_interrupt() ? " while in int" : "");
			au0828_uninit_isoc(dev);
			return -ENOMEM;
		}
		memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);

		pipe = usb_rcvisocpipe(dev->usbdev,
				       dev->isoc_in_endpointaddr),

		usb_fill_int_urb(urb, dev->usbdev, pipe,
				 dev->isoc_ctl.transfer_buffer[i], sb_size,
				 au0828_irq_callback, dma_q, 1);

		urb->number_of_packets = max_packets;
268
		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

		k = 0;
		for (j = 0; j < max_packets; j++) {
			urb->iso_frame_desc[j].offset = k;
			urb->iso_frame_desc[j].length =
						dev->isoc_ctl.max_pkt_size;
			k += dev->isoc_ctl.max_pkt_size;
		}
	}

	init_waitqueue_head(&dma_q->wq);

	/* submit urbs and enables IRQ */
	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
		rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
		if (rc) {
			au0828_isocdbg("submit of urb %i failed (error=%i)\n",
				       i, rc);
			au0828_uninit_isoc(dev);
			return rc;
		}
	}

	return 0;
}

/*
 * Announces that a buffer were filled and request the next
 */
static inline void buffer_filled(struct au0828_dev *dev,
				  struct au0828_dmaqueue *dma_q,
				  struct au0828_buffer *buf)
{
	/* Advice that buffer was filled */
	au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);

	buf->vb.state = VIDEOBUF_DONE;
	buf->vb.field_count++;
307
	v4l2_get_timestamp(&buf->vb.ts);
308 309 310 311 312 313 314

	dev->isoc_ctl.buf = NULL;

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

315 316 317 318 319 320 321 322 323
static inline void vbi_buffer_filled(struct au0828_dev *dev,
				     struct au0828_dmaqueue *dma_q,
				     struct au0828_buffer *buf)
{
	/* Advice that buffer was filled */
	au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);

	buf->vb.state = VIDEOBUF_DONE;
	buf->vb.field_count++;
324
	v4l2_get_timestamp(&buf->vb.ts);
325 326 327 328 329 330 331

	dev->isoc_ctl.vbi_buf = NULL;

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

332 333 334 335 336 337 338 339 340 341 342 343 344
/*
 * Identify the buffer header type and properly handles
 */
static void au0828_copy_video(struct au0828_dev *dev,
			      struct au0828_dmaqueue  *dma_q,
			      struct au0828_buffer *buf,
			      unsigned char *p,
			      unsigned char *outp, unsigned long len)
{
	void *fieldstart, *startwrite, *startread;
	int  linesdone, currlinedone, offset, lencopy, remain;
	int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */

345 346 347
	if (len == 0)
		return;

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 381 382 383 384 385 386 387 388 389
	if (dma_q->pos + len > buf->vb.size)
		len = buf->vb.size - dma_q->pos;

	startread = p;
	remain = len;

	/* Interlaces frame */
	if (buf->top_field)
		fieldstart = outp;
	else
		fieldstart = outp + bytesperline;

	linesdone = dma_q->pos / bytesperline;
	currlinedone = dma_q->pos % bytesperline;
	offset = linesdone * bytesperline * 2 + currlinedone;
	startwrite = fieldstart + offset;
	lencopy = bytesperline - currlinedone;
	lencopy = lencopy > remain ? remain : lencopy;

	if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
		au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
			       ((char *)startwrite + lencopy) -
			       ((char *)outp + buf->vb.size));
		remain = (char *)outp + buf->vb.size - (char *)startwrite;
		lencopy = remain;
	}
	if (lencopy <= 0)
		return;
	memcpy(startwrite, startread, lencopy);

	remain -= lencopy;

	while (remain > 0) {
		startwrite += lencopy + bytesperline;
		startread += lencopy;
		if (bytesperline > remain)
			lencopy = remain;
		else
			lencopy = bytesperline;

		if ((char *)startwrite + lencopy > (char *)outp +
		    buf->vb.size) {
390
			au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
				       ((char *)startwrite + lencopy) -
				       ((char *)outp + buf->vb.size));
			lencopy = remain = (char *)outp + buf->vb.size -
					   (char *)startwrite;
		}
		if (lencopy <= 0)
			break;

		memcpy(startwrite, startread, lencopy);

		remain -= lencopy;
	}

	if (offset > 1440) {
		/* We have enough data to check for greenscreen */
406
		if (outp[0] < 0x60 && outp[1440] < 0x60)
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
			dev->greenscreen_detected = 1;
	}

	dma_q->pos += len;
}

/*
 * video-buf generic routine to get the next available buffer
 */
static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
				struct au0828_buffer **buf)
{
	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);

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

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

	return;
}

435 436 437 438 439 440 441
static void au0828_copy_vbi(struct au0828_dev *dev,
			      struct au0828_dmaqueue  *dma_q,
			      struct au0828_buffer *buf,
			      unsigned char *p,
			      unsigned char *outp, unsigned long len)
{
	unsigned char *startwrite, *startread;
442
	int bytesperline;
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	int i, j = 0;

	if (dev == NULL) {
		au0828_isocdbg("dev is null\n");
		return;
	}

	if (dma_q == NULL) {
		au0828_isocdbg("dma_q is null\n");
		return;
	}
	if (buf == NULL)
		return;
	if (p == NULL) {
		au0828_isocdbg("p is null\n");
		return;
	}
	if (outp == NULL) {
		au0828_isocdbg("outp is null\n");
		return;
	}

465 466
	bytesperline = dev->vbi_width;

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	if (dma_q->pos + len > buf->vb.size)
		len = buf->vb.size - dma_q->pos;

	startread = p;
	startwrite = outp + (dma_q->pos / 2);

	/* Make sure the bottom field populates the second half of the frame */
	if (buf->top_field == 0)
		startwrite += bytesperline * dev->vbi_height;

	for (i = 0; i < len; i += 2)
		startwrite[j++] = startread[i+1];

	dma_q->pos += len;
}


/*
 * video-buf generic routine to get the next available VBI buffer
 */
static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
				    struct au0828_buffer **buf)
{
	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
	char *outp;

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

	/* Get the next buffer */
	*buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
L
Lucas De Marchi 已提交
502
	/* Cleans up buffer - Useful for testing for frame/URB loss */
503 504 505 506 507 508 509 510
	outp = videobuf_to_vmalloc(&(*buf)->vb);
	memset(outp, 0x00, (*buf)->vb.size);

	dev->isoc_ctl.vbi_buf = *buf;

	return;
}

511 512 513 514 515 516
/*
 * Controls the isoc copy of each urb packet
 */
static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
{
	struct au0828_buffer    *buf;
517
	struct au0828_buffer    *vbi_buf;
518
	struct au0828_dmaqueue  *dma_q = urb->context;
519
	struct au0828_dmaqueue  *vbi_dma_q = &dev->vbiq;
520
	unsigned char *outp = NULL;
521
	unsigned char *vbioutp = NULL;
522 523 524
	int i, len = 0, rc = 1;
	unsigned char *p;
	unsigned char fbyte;
525 526
	unsigned int vbi_field_size;
	unsigned int remain, lencopy;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

	if (!dev)
		return 0;

	if ((dev->dev_state & DEV_DISCONNECTED) ||
	    (dev->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->isoc_ctl.buf;
	if (buf != NULL)
		outp = videobuf_to_vmalloc(&buf->vb);

545 546 547 548
	vbi_buf = dev->isoc_ctl.vbi_buf;
	if (vbi_buf != NULL)
		vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);

549 550 551 552 553 554 555 556 557
	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;
		}

558
		if (urb->iso_frame_desc[i].actual_length <= 0)
559
			continue;
560

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
		if (urb->iso_frame_desc[i].actual_length >
						dev->max_pkt_size) {
			au0828_isocdbg("packet bigger than packet size");
			continue;
		}

		p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
		fbyte = p[0];
		len = urb->iso_frame_desc[i].actual_length - 4;
		p += 4;

		if (fbyte & 0x80) {
			len -= 4;
			p += 4;
			au0828_isocdbg("Video frame %s\n",
				       (fbyte & 0x40) ? "odd" : "even");
577
			if (fbyte & 0x40) {
578 579 580 581 582 583 584 585 586 587 588 589 590
				/* VBI */
				if (vbi_buf != NULL)
					vbi_buffer_filled(dev,
							  vbi_dma_q,
							  vbi_buf);
				vbi_get_next_buf(vbi_dma_q, &vbi_buf);
				if (vbi_buf == NULL)
					vbioutp = NULL;
				else
					vbioutp = videobuf_to_vmalloc(
						&vbi_buf->vb);

				/* Video */
591 592 593
				if (buf != NULL)
					buffer_filled(dev, dma_q, buf);
				get_next_buf(dma_q, &buf);
594
				if (buf == NULL)
595
					outp = NULL;
596
				else
597
					outp = videobuf_to_vmalloc(&buf->vb);
598 599 600 601 602 603 604 605 606

				/* As long as isoc traffic is arriving, keep
				   resetting the timer */
				if (dev->vid_timeout_running)
					mod_timer(&dev->vid_timeout,
						  jiffies + (HZ / 10));
				if (dev->vbi_timeout_running)
					mod_timer(&dev->vbi_timeout,
						  jiffies + (HZ / 10));
607 608 609
			}

			if (buf != NULL) {
610
				if (fbyte & 0x40)
611
					buf->top_field = 1;
612
				else
613 614 615
					buf->top_field = 0;
			}

616 617 618 619 620 621 622 623 624
			if (vbi_buf != NULL) {
				if (fbyte & 0x40)
					vbi_buf->top_field = 1;
				else
					vbi_buf->top_field = 0;
			}

			dev->vbi_read = 0;
			vbi_dma_q->pos = 0;
625 626
			dma_q->pos = 0;
		}
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

		vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
		if (dev->vbi_read < vbi_field_size) {
			remain  = vbi_field_size - dev->vbi_read;
			if (len < remain)
				lencopy = len;
			else
				lencopy = remain;

			if (vbi_buf != NULL)
				au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
						vbioutp, len);

			len -= lencopy;
			p += lencopy;
			dev->vbi_read += lencopy;
		}

		if (dev->vbi_read >= vbi_field_size && buf != NULL)
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
			au0828_copy_video(dev, dma_q, buf, p, outp, len);
	}
	return rc;
}

static int
buffer_setup(struct videobuf_queue *vq, unsigned int *count,
	     unsigned int *size)
{
	struct au0828_fh *fh = vq->priv_data;
	*size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;

	if (0 == *count)
		*count = AU0828_DEF_BUF;

	if (*count < AU0828_MIN_BUF)
		*count = AU0828_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 au0828_buffer *buf)
{
	struct au0828_fh     *fh  = vq->priv_data;
	struct au0828_dev    *dev = fh->dev;
	unsigned long flags = 0;
	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.
	*/
	spin_lock_irqsave(&dev->slock, flags);
	if (dev->isoc_ctl.buf == buf)
		dev->isoc_ctl.buf = NULL;
	spin_unlock_irqrestore(&dev->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,
						enum v4l2_field field)
{
	struct au0828_fh     *fh  = vq->priv_data;
	struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
	struct au0828_dev    *dev = fh->dev;
	int                  rc = 0, urb_init = 0;

	buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;

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

	buf->vb.width  = dev->width;
	buf->vb.height = dev->height;
	buf->vb.field  = field;

	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
		rc = videobuf_iolock(vq, &buf->vb, NULL);
		if (rc < 0) {
714
			printk(KERN_INFO "videobuf_iolock failed\n");
715 716 717 718 719 720 721 722 723 724 725 726
			goto fail;
		}
	}

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

	if (urb_init) {
		rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
				      AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
				      au0828_isoc_copy);
		if (rc < 0) {
727
			printk(KERN_INFO "au0828_init_isoc failed\n");
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
			goto fail;
		}
	}

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

fail:
	free_buffer(vq, buf);
	return rc;
}

static void
buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
{
	struct au0828_buffer    *buf     = container_of(vb,
							struct au0828_buffer,
							vb);
	struct au0828_fh        *fh      = vq->priv_data;
	struct au0828_dev       *dev     = fh->dev;
	struct au0828_dmaqueue  *vidq    = &dev->vidq;

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

static void buffer_release(struct videobuf_queue *vq,
				struct videobuf_buffer *vb)
{
	struct au0828_buffer   *buf  = container_of(vb,
						    struct au0828_buffer,
						    vb);

	free_buffer(vq, buf);
}

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

/* ------------------------------------------------------------------
   V4L2 interface
   ------------------------------------------------------------------*/

static int au0828_i2s_init(struct au0828_dev *dev)
{
	/* Enable i2s mode */
	au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
	return 0;
}

/*
 * Auvitek au0828 analog stream enable
 * Please set interface0 to AS5 before enable the stream
 */
786
static int au0828_analog_stream_enable(struct au0828_dev *d)
787 788 789 790 791 792 793 794 795 796
{
	dprintk(1, "au0828_analog_stream_enable called\n");
	au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
	au0828_writereg(d, 0x106, 0x00);
	/* set x position */
	au0828_writereg(d, 0x110, 0x00);
	au0828_writereg(d, 0x111, 0x00);
	au0828_writereg(d, 0x114, 0xa0);
	au0828_writereg(d, 0x115, 0x05);
	/* set y position */
797
	au0828_writereg(d, 0x112, 0x00);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
	au0828_writereg(d, 0x113, 0x00);
	au0828_writereg(d, 0x116, 0xf2);
	au0828_writereg(d, 0x117, 0x00);
	au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);

	return 0;
}

int au0828_analog_stream_disable(struct au0828_dev *d)
{
	dprintk(1, "au0828_analog_stream_disable called\n");
	au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
	return 0;
}

813
static void au0828_analog_stream_reset(struct au0828_dev *dev)
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
{
	dprintk(1, "au0828_analog_stream_reset called\n");
	au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
	mdelay(30);
	au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
}

/*
 * Some operations needs to stop current streaming
 */
static int au0828_stream_interrupt(struct au0828_dev *dev)
{
	int ret = 0;

	dev->stream_state = STREAM_INTERRUPT;
829
	if (dev->dev_state == DEV_DISCONNECTED)
830
		return -ENODEV;
831
	else if (ret) {
832
		dev->dev_state = DEV_MISCONFIGURED;
833
		dprintk(1, "%s device is misconfigured!\n", __func__);
834 835 836 837 838 839 840 841 842 843 844 845 846 847
		return ret;
	}
	return 0;
}

/*
 * au0828_release_resources
 * unregister v4l2 devices
 */
void au0828_analog_unregister(struct au0828_dev *dev)
{
	dprintk(1, "au0828_release_resources called\n");
	mutex_lock(&au0828_sysfs_lock);

848
	if (dev->vdev)
849 850 851
		video_unregister_device(dev->vdev);
	if (dev->vbi_dev)
		video_unregister_device(dev->vbi_dev);
852 853 854 855 856 857

	mutex_unlock(&au0828_sysfs_lock);
}


/* Usage lock check functions */
858
static int res_get(struct au0828_fh *fh, unsigned int bit)
859
{
860
	struct au0828_dev    *dev = fh->dev;
861

862 863 864
	if (fh->resources & bit)
		/* have it already allocated */
		return 1;
865

866 867 868 869 870 871 872 873 874
	/* is it free? */
	if (dev->resources & bit) {
		/* no, someone else uses it */
		return 0;
	}
	/* it's free, grab it */
	fh->resources  |= bit;
	dev->resources |= bit;
	dprintk(1, "res: get %d\n", bit);
875

876 877
	return 1;
}
878

879 880 881
static int res_check(struct au0828_fh *fh, unsigned int bit)
{
	return fh->resources & bit;
882 883
}

884
static int res_locked(struct au0828_dev *dev, unsigned int bit)
885
{
886
	return dev->resources & bit;
887 888
}

889
static void res_free(struct au0828_fh *fh, unsigned int bits)
890
{
891 892 893
	struct au0828_dev    *dev = fh->dev;

	BUG_ON((fh->resources & bits) != bits);
894

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
	fh->resources  &= ~bits;
	dev->resources &= ~bits;
	dprintk(1, "res: put %d\n", bits);
}

static int get_ressource(struct au0828_fh *fh)
{
	switch (fh->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		return AU0828_RESOURCE_VIDEO;
	case V4L2_BUF_TYPE_VBI_CAPTURE:
		return AU0828_RESOURCE_VBI;
	default:
		BUG();
		return 0;
	}
911 912
}

913 914 915
/* This function ensures that video frames continue to be delivered even if
   the ITU-656 input isn't receiving any data (thereby preventing applications
   such as tvtime from hanging) */
916
static void au0828_vid_buffer_timeout(unsigned long data)
917 918 919 920 921
{
	struct au0828_dev *dev = (struct au0828_dev *) data;
	struct au0828_dmaqueue *dma_q = &dev->vidq;
	struct au0828_buffer *buf;
	unsigned char *vid_data;
922
	unsigned long flags = 0;
923

924
	spin_lock_irqsave(&dev->slock, flags);
925 926 927 928 929 930 931

	buf = dev->isoc_ctl.buf;
	if (buf != NULL) {
		vid_data = videobuf_to_vmalloc(&buf->vb);
		memset(vid_data, 0x00, buf->vb.size); /* Blank green frame */
		buffer_filled(dev, dma_q, buf);
	}
932 933 934 935
	get_next_buf(dma_q, &buf);

	if (dev->vid_timeout_running == 1)
		mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
936

937
	spin_unlock_irqrestore(&dev->slock, flags);
938 939
}

940
static void au0828_vbi_buffer_timeout(unsigned long data)
941 942 943 944 945
{
	struct au0828_dev *dev = (struct au0828_dev *) data;
	struct au0828_dmaqueue *dma_q = &dev->vbiq;
	struct au0828_buffer *buf;
	unsigned char *vbi_data;
946
	unsigned long flags = 0;
947

948
	spin_lock_irqsave(&dev->slock, flags);
949 950 951 952 953 954 955

	buf = dev->isoc_ctl.vbi_buf;
	if (buf != NULL) {
		vbi_data = videobuf_to_vmalloc(&buf->vb);
		memset(vbi_data, 0x00, buf->vb.size);
		vbi_buffer_filled(dev, dma_q, buf);
	}
956
	vbi_get_next_buf(dma_q, &buf);
957

958 959 960
	if (dev->vbi_timeout_running == 1)
		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
	spin_unlock_irqrestore(&dev->slock, flags);
961 962 963
}


964 965 966
static int au0828_v4l2_open(struct file *filp)
{
	int ret = 0;
967
	struct video_device *vdev = video_devdata(filp);
968
	struct au0828_dev *dev = video_drvdata(filp);
969
	struct au0828_fh *fh;
970
	int type;
971

972 973 974 975 976
	switch (vdev->vfl_type) {
	case VFL_TYPE_GRABBER:
		type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		break;
	case VFL_TYPE_VBI:
977
		type = V4L2_BUF_TYPE_VBI_CAPTURE;
978 979 980 981
		break;
	default:
		return -EINVAL;
	}
982 983

	fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
984
	if (NULL == fh) {
985 986 987 988 989 990 991 992
		dprintk(1, "Failed allocate au0828_fh struct!\n");
		return -ENOMEM;
	}

	fh->type = type;
	fh->dev = dev;
	filp->private_data = fh;

993
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
994 995
		/* set au0828 interface0 to AS5 here again */
		ret = usb_set_interface(dev->usbdev, 0, 5);
996 997
		if (ret < 0) {
			printk(KERN_INFO "Au0828 can't set alternate to 5!\n");
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
			return -EBUSY;
		}
		dev->width = NTSC_STD_W;
		dev->height = NTSC_STD_H;
		dev->frame_size = dev->width * dev->height * 2;
		dev->field_size = dev->width * dev->height;
		dev->bytesperline = dev->width * 2;

		au0828_analog_stream_enable(dev);
		au0828_analog_stream_reset(dev);

		/* If we were doing ac97 instead of i2s, it would go here...*/
		au0828_i2s_init(dev);

		dev->stream_state = STREAM_OFF;
		dev->dev_state |= DEV_INITIALIZED;
	}

	dev->users++;

	videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
1019 1020
				    NULL, &dev->slock,
				    V4L2_BUF_TYPE_VIDEO_CAPTURE,
1021
				    V4L2_FIELD_INTERLACED,
1022 1023
				    sizeof(struct au0828_buffer), fh,
				    &dev->lock);
1024

1025 1026 1027 1028 1029 1030 1031
	/* VBI Setup */
	dev->vbi_width = 720;
	dev->vbi_height = 1;
	videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
				    NULL, &dev->slock,
				    V4L2_BUF_TYPE_VBI_CAPTURE,
				    V4L2_FIELD_SEQ_TB,
1032 1033
				    sizeof(struct au0828_buffer), fh,
				    &dev->lock);
1034 1035 1036 1037 1038 1039 1040 1041 1042
	return ret;
}

static int au0828_v4l2_close(struct file *filp)
{
	int ret;
	struct au0828_fh *fh = filp->private_data;
	struct au0828_dev *dev = fh->dev;

1043
	if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1044 1045 1046 1047
		/* Cancel timeout thread in case they didn't call streamoff */
		dev->vid_timeout_running = 0;
		del_timer_sync(&dev->vid_timeout);

1048
		videobuf_stop(&fh->vb_vidq);
1049 1050
		res_free(fh, AU0828_RESOURCE_VIDEO);
	}
1051

1052
	if (res_check(fh, AU0828_RESOURCE_VBI)) {
1053 1054 1055 1056
		/* Cancel timeout thread in case they didn't call streamoff */
		dev->vbi_timeout_running = 0;
		del_timer_sync(&dev->vbi_timeout);

1057 1058 1059 1060 1061
		videobuf_stop(&fh->vb_vbiq);
		res_free(fh, AU0828_RESOURCE_VBI);
	}

	if (dev->users == 1) {
1062
		if (dev->dev_state & DEV_DISCONNECTED) {
1063 1064 1065 1066 1067 1068 1069 1070 1071
			au0828_analog_unregister(dev);
			kfree(dev);
			return 0;
		}

		au0828_analog_stream_disable(dev);

		au0828_uninit_isoc(dev);

1072
		/* Save some power by putting tuner to sleep */
1073
		v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1074

1075 1076 1077
		/* When close the device, set the usb intf0 into alt0 to free
		   USB bandwidth */
		ret = usb_set_interface(dev->usbdev, 0, 0);
1078 1079
		if (ret < 0)
			printk(KERN_INFO "Au0828 can't set alternate to 0!\n");
1080 1081
	}

1082 1083
	videobuf_mmap_free(&fh->vb_vidq);
	videobuf_mmap_free(&fh->vb_vbiq);
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	kfree(fh);
	dev->users--;
	wake_up_interruptible_nr(&dev->open, 1);
	return 0;
}

static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
				size_t count, loff_t *pos)
{
	struct au0828_fh *fh = filp->private_data;
	struct au0828_dev *dev = fh->dev;
	int rc;

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

1101
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1102 1103
		if (res_locked(dev, AU0828_RESOURCE_VIDEO))
			return -EBUSY;
1104 1105 1106 1107

		return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
					filp->f_flags & O_NONBLOCK);
	}
1108 1109 1110 1111 1112

	if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
		if (!res_get(fh, AU0828_RESOURCE_VBI))
			return -EBUSY;

1113 1114 1115 1116 1117 1118 1119
		if (dev->vbi_timeout_running == 0) {
			/* Handle case where caller tries to read without
			   calling streamon first */
			dev->vbi_timeout_running = 1;
			mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
		}

1120 1121 1122 1123
		return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
					    filp->f_flags & O_NONBLOCK);
	}

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
	return 0;
}

static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
{
	struct au0828_fh *fh = filp->private_data;
	struct au0828_dev *dev = fh->dev;
	int rc;

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

1137 1138 1139 1140 1141 1142 1143 1144 1145
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
		if (!res_get(fh, AU0828_RESOURCE_VIDEO))
			return POLLERR;
		return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
	} else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
		if (!res_get(fh, AU0828_RESOURCE_VBI))
			return POLLERR;
		return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
	} else {
1146
		return POLLERR;
1147
	}
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
}

static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
{
	struct au0828_fh *fh    = filp->private_data;
	struct au0828_dev *dev   = fh->dev;
	int		 rc;

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

1160 1161 1162 1163
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
	else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
		rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

	return rc;
}

static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
			     struct v4l2_format *format)
{
	int ret;
	int width = format->fmt.pix.width;
	int height = format->fmt.pix.height;

1175
	if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1176 1177 1178 1179
		return -EINVAL;

	/* If they are demanding a format other than the one we support,
	   bail out (tvtime asks for UYVY and then retries with YUYV) */
1180
	if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1181 1182 1183
		return -EINVAL;

	/* format->fmt.pix.width only support 720 and height 480 */
1184
	if (width != 720)
1185
		width = 720;
1186
	if (height != 480)
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
		height = 480;

	format->fmt.pix.width = width;
	format->fmt.pix.height = height;
	format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
	format->fmt.pix.bytesperline = width * 2;
	format->fmt.pix.sizeimage = width * height * 2;
	format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
	format->fmt.pix.field = V4L2_FIELD_INTERLACED;

1197
	if (cmd == VIDIOC_TRY_FMT)
1198 1199 1200 1201 1202 1203 1204 1205 1206
		return 0;

	/* maybe set new image format, driver current only support 720*480 */
	dev->width = width;
	dev->height = height;
	dev->frame_size = width * height * 2;
	dev->field_size = width * height;
	dev->bytesperline = width * 2;

1207
	if (dev->stream_state == STREAM_ON) {
1208
		dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1209 1210
		ret = au0828_stream_interrupt(dev);
		if (ret != 0) {
1211 1212 1213 1214 1215 1216 1217
			dprintk(1, "error interrupting video stream!\n");
			return ret;
		}
	}

	/* set au0828 interface0 to AS5 here again */
	ret = usb_set_interface(dev->usbdev, 0, 5);
1218 1219
	if (ret < 0) {
		printk(KERN_INFO "Au0828 can't set alt setting to 5!\n");
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
		return -EBUSY;
	}

	au0828_analog_stream_enable(dev);

	return 0;
}


static int vidioc_queryctrl(struct file *file, void *priv,
			    struct v4l2_queryctrl *qc)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
1234
	v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
	if (qc->type)
		return 0;
	else
		return -EINVAL;
}

static int vidioc_querycap(struct file *file, void  *priv,
			   struct v4l2_capability *cap)
{
	struct au0828_fh *fh  = priv;
	struct au0828_dev *dev = fh->dev;

	strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1248
	strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1249
	strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
1250 1251

	/*set the device capabilities */
1252 1253
	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
		V4L2_CAP_VBI_CAPTURE |
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
		V4L2_CAP_AUDIO |
		V4L2_CAP_READWRITE |
		V4L2_CAP_STREAMING |
		V4L2_CAP_TUNER;
	return 0;
}

static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
					struct v4l2_fmtdesc *f)
{
1264
	if (f->index)
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
		return -EINVAL;

	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	strcpy(f->description, "Packed YUV2");

	f->flags = 0;
	f->pixelformat = V4L2_PIX_FMT_UYVY;

	return 0;
}

static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
					struct v4l2_format *f)
{
	struct au0828_fh *fh  = priv;
	struct au0828_dev *dev = fh->dev;

	f->fmt.pix.width = dev->width;
	f->fmt.pix.height = dev->height;
	f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
	f->fmt.pix.bytesperline = dev->bytesperline;
	f->fmt.pix.sizeimage = dev->frame_size;
	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
	return 0;
}

static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
				  struct v4l2_format *f)
{
	struct au0828_fh *fh  = priv;
	struct au0828_dev *dev = fh->dev;

	return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
}

static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
{
	struct au0828_fh *fh  = priv;
	struct au0828_dev *dev = fh->dev;
	int rc;

1308 1309 1310 1311
	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1312
	if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1313
		printk(KERN_INFO "%s queue busy\n", __func__);
1314 1315 1316 1317
		rc = -EBUSY;
		goto out;
	}

1318
	rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1319 1320 1321 1322
out:
	return rc;
}

1323
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1324 1325 1326 1327
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;

1328 1329 1330
	if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
		dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);

1331 1332 1333 1334
	/* FIXME: when we support something other than NTSC, we are going to
	   have to make the au0828 bridge adjust the size of its capture
	   buffer, which is currently hardcoded at 720x480 */

1335
	v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, norm);
1336
	dev->std_set_in_tuner_core = 1;
1337 1338 1339 1340

	if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
		dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
	return 0;
}

static int vidioc_enum_input(struct file *file, void *priv,
				struct v4l2_input *input)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	unsigned int tmp;

	static const char *inames[] = {
1352
		[AU0828_VMUX_UNDEFINED] = "Undefined",
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
		[AU0828_VMUX_COMPOSITE] = "Composite",
		[AU0828_VMUX_SVIDEO] = "S-Video",
		[AU0828_VMUX_CABLE] = "Cable TV",
		[AU0828_VMUX_TELEVISION] = "Television",
		[AU0828_VMUX_DVB] = "DVB",
		[AU0828_VMUX_DEBUG] = "tv debug"
	};

	tmp = input->index;

1363
	if (tmp >= AU0828_MAX_INPUT)
1364
		return -EINVAL;
1365
	if (AUVI_INPUT(tmp).type == 0)
1366 1367 1368
		return -EINVAL;

	input->index = tmp;
1369
	strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1370 1371
	if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
	    (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE))
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
		input->type |= V4L2_INPUT_TYPE_TUNER;
	else
		input->type |= V4L2_INPUT_TYPE_CAMERA;

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

	return 0;
}

static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	*i = dev->ctrl_input;
	return 0;
}

static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	int i;

1395
	dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1396
		index);
1397
	if (index >= AU0828_MAX_INPUT)
1398
		return -EINVAL;
1399
	if (AUVI_INPUT(index).type == 0)
1400 1401 1402
		return -EINVAL;
	dev->ctrl_input = index;

1403
	switch (AUVI_INPUT(index).type) {
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	case AU0828_VMUX_SVIDEO:
		dev->input_type = AU0828_VMUX_SVIDEO;
		break;
	case AU0828_VMUX_COMPOSITE:
		dev->input_type = AU0828_VMUX_COMPOSITE;
		break;
	case AU0828_VMUX_TELEVISION:
		dev->input_type = AU0828_VMUX_TELEVISION;
		break;
	default:
1414 1415 1416
		dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
			AUVI_INPUT(index).type);
		break;
1417 1418
	}

1419 1420
	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
			AUVI_INPUT(index).vmux, 0, 0);
1421 1422 1423

	for (i = 0; i < AU0828_MAX_INPUT; i++) {
		int enable = 0;
1424
		if (AUVI_INPUT(i).audio_setup == NULL)
1425 1426 1427 1428 1429 1430 1431
			continue;

		if (i == index)
			enable = 1;
		else
			enable = 0;
		if (enable) {
1432
			(AUVI_INPUT(i).audio_setup)(dev, enable);
1433 1434 1435
		} else {
			/* Make sure we leave it turned on if some
			   other input is routed to this callback */
1436 1437 1438
			if ((AUVI_INPUT(i).audio_setup) !=
			    ((AUVI_INPUT(index).audio_setup))) {
				(AUVI_INPUT(i).audio_setup)(dev, enable);
1439 1440 1441 1442
			}
		}
	}

1443 1444
	v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
			AUVI_INPUT(index).amux, 0, 0);
1445 1446 1447 1448 1449 1450 1451 1452 1453
	return 0;
}

static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	unsigned int index = a->index;

1454
	if (a->index > 1)
1455 1456 1457
		return -EINVAL;

	index = dev->ctrl_ainput;
1458
	if (index == 0)
1459 1460 1461 1462 1463 1464 1465 1466 1467
		strcpy(a->name, "Television");
	else
		strcpy(a->name, "Line in");

	a->capability = V4L2_AUDCAP_STEREO;
	a->index = index;
	return 0;
}

1468
static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1469 1470 1471
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
1472
	if (a->index != dev->ctrl_ainput)
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
		return -EINVAL;
	return 0;
}

static int vidioc_g_ctrl(struct file *file, void *priv,
			 struct v4l2_control *ctrl)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;

1483
	v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1484 1485 1486 1487 1488 1489 1490 1491 1492
	return 0;

}

static int vidioc_s_ctrl(struct file *file, void *priv,
				struct v4l2_control *ctrl)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
1493
	v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1494 1495 1496 1497 1498 1499 1500 1501
	return 0;
}

static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;

1502
	if (t->index != 0)
1503 1504 1505
		return -EINVAL;

	strcpy(t->name, "Auvitek tuner");
1506
	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1507 1508 1509 1510
	return 0;
}

static int vidioc_s_tuner(struct file *file, void *priv,
1511
				const struct v4l2_tuner *t)
1512 1513 1514 1515
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;

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

1519 1520 1521
	if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
		dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);

1522
	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1523 1524 1525 1526

	if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
		dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);

1527 1528
	dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
		t->afc);
1529

1530 1531 1532 1533 1534 1535 1536 1537 1538
	return 0;

}

static int vidioc_g_frequency(struct file *file, void *priv,
				struct v4l2_frequency *freq)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
1539

1540 1541 1542 1543 1544 1545
	freq->type = V4L2_TUNER_ANALOG_TV;
	freq->frequency = dev->ctrl_freq;
	return 0;
}

static int vidioc_s_frequency(struct file *file, void *priv,
1546
				const struct v4l2_frequency *freq)
1547 1548 1549 1550
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;

1551
	if (freq->tuner != 0)
1552
		return -EINVAL;
1553
	if (freq->type != V4L2_TUNER_ANALOG_TV)
1554 1555 1556 1557
		return -EINVAL;

	dev->ctrl_freq = freq->frequency;

1558 1559 1560
	if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
		dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 1);

1561 1562 1563 1564 1565 1566 1567 1568 1569
	if (dev->std_set_in_tuner_core == 0) {
	  /* If we've never sent the standard in tuner core, do so now.  We
	     don't do this at device probe because we don't want to incur
	     the cost of a firmware load */
	  v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std,
			       dev->vdev->tvnorms);
	  dev->std_set_in_tuner_core = 1;
	}

1570
	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1571

1572 1573 1574
	if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
		dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, 0);

1575 1576 1577 1578 1579
	au0828_analog_stream_reset(dev);

	return 0;
}

1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602

/* RAW VBI ioctls */

static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
				struct v4l2_format *format)
{
	struct au0828_fh      *fh  = priv;
	struct au0828_dev     *dev = fh->dev;

	format->fmt.vbi.samples_per_line = dev->vbi_width;
	format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
	format->fmt.vbi.offset = 0;
	format->fmt.vbi.flags = 0;
	format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;

	format->fmt.vbi.count[0] = dev->vbi_height;
	format->fmt.vbi.count[1] = dev->vbi_height;
	format->fmt.vbi.start[0] = 21;
	format->fmt.vbi.start[1] = 284;

	return 0;
}

1603 1604 1605 1606 1607 1608 1609 1610
static int vidioc_g_chip_ident(struct file *file, void *priv,
	       struct v4l2_dbg_chip_ident *chip)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	chip->ident = V4L2_IDENT_NONE;
	chip->revision = 0;

1611 1612 1613 1614 1615
	if (v4l2_chip_match_host(&chip->match)) {
		chip->ident = V4L2_IDENT_AU0828;
		return 0;
	}

1616
	v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1617 1618 1619
	if (chip->ident == V4L2_IDENT_NONE)
		return -EINVAL;

1620 1621 1622 1623 1624 1625 1626 1627 1628
	return 0;
}

static int vidioc_cropcap(struct file *file, void *priv,
			  struct v4l2_cropcap *cc)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;

1629
	if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
		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;
	cc->pixelaspect.denominator = 59;

	return 0;
}

static int vidioc_streamon(struct file *file, void *priv,
			   enum v4l2_buf_type type)
{
1648 1649 1650
	struct au0828_fh      *fh  = priv;
	struct au0828_dev     *dev = fh->dev;
	int                   rc = -EINVAL;
1651 1652 1653 1654 1655

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

1656 1657 1658 1659 1660 1661 1662 1663 1664
	if (unlikely(type != fh->type))
		return -EINVAL;

	dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
		fh, type, fh->resources, dev->resources);

	if (unlikely(!res_get(fh, get_ressource(fh))))
		return -EBUSY;

1665 1666
	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
		au0828_analog_stream_enable(dev);
1667
		v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
1668 1669
	}

1670
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1671
		rc = videobuf_streamon(&fh->vb_vidq);
1672 1673 1674
		dev->vid_timeout_running = 1;
		mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
	} else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1675
		rc = videobuf_streamon(&fh->vb_vbiq);
1676 1677 1678
		dev->vbi_timeout_running = 1;
		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
	}
1679 1680 1681 1682 1683 1684 1685

	return rc;
}

static int vidioc_streamoff(struct file *file, void *priv,
			    enum v4l2_buf_type type)
{
1686 1687 1688 1689
	struct au0828_fh      *fh  = priv;
	struct au0828_dev     *dev = fh->dev;
	int                   rc;
	int                   i;
1690 1691 1692 1693 1694

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

1695 1696
	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
	    fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1697 1698 1699 1700
		return -EINVAL;
	if (type != fh->type)
		return -EINVAL;

1701 1702 1703 1704
	dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
		fh, type, fh->resources, dev->resources);

	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1705 1706 1707
		dev->vid_timeout_running = 0;
		del_timer_sync(&dev->vid_timeout);

1708
		v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1709 1710 1711
		rc = au0828_stream_interrupt(dev);
		if (rc != 0)
			return rc;
1712

1713 1714 1715 1716 1717
		for (i = 0; i < AU0828_MAX_INPUT; i++) {
			if (AUVI_INPUT(i).audio_setup == NULL)
				continue;
			(AUVI_INPUT(i).audio_setup)(dev, 0);
		}
1718

1719 1720 1721 1722
		if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
			videobuf_streamoff(&fh->vb_vidq);
			res_free(fh, AU0828_RESOURCE_VIDEO);
		}
1723
	} else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1724 1725 1726
		dev->vbi_timeout_running = 0;
		del_timer_sync(&dev->vbi_timeout);

1727 1728 1729 1730
		if (res_check(fh, AU0828_RESOURCE_VBI)) {
			videobuf_streamoff(&fh->vb_vbiq);
			res_free(fh, AU0828_RESOURCE_VBI);
		}
1731
	}
1732 1733 1734 1735

	return 0;
}

1736
#ifdef CONFIG_VIDEO_ADV_DEBUG
1737 1738 1739 1740 1741 1742 1743 1744
static int vidioc_g_register(struct file *file, void *priv,
			     struct v4l2_dbg_register *reg)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;

	switch (reg->match.type) {
	case V4L2_CHIP_MATCH_I2C_DRIVER:
1745
		v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1746 1747
		return 0;
	default:
1748 1749
		if (!v4l2_chip_match_host(&reg->match))
			return -EINVAL;
1750
	}
1751 1752 1753

	reg->val = au0828_read(dev, reg->reg);
	return 0;
1754 1755 1756
}

static int vidioc_s_register(struct file *file, void *priv,
1757
			     const struct v4l2_dbg_register *reg)
1758 1759 1760 1761 1762 1763
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;

	switch (reg->match.type) {
	case V4L2_CHIP_MATCH_I2C_DRIVER:
1764
		v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1765 1766
		return 0;
	default:
1767 1768
		if (!v4l2_chip_match_host(&reg->match))
			return -EINVAL;
1769
	}
1770
	return au0828_writereg(dev, reg->reg, reg->val);
1771
}
1772
#endif
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784

static int vidioc_reqbufs(struct file *file, void *priv,
			  struct v4l2_requestbuffers *rb)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	int rc;

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

1785 1786 1787 1788 1789 1790
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		rc = videobuf_reqbufs(&fh->vb_vidq, rb);
	else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
		rc = videobuf_reqbufs(&fh->vb_vbiq, rb);

	return rc;
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
}

static int vidioc_querybuf(struct file *file, void *priv,
			   struct v4l2_buffer *b)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	int rc;

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

1804 1805 1806 1807 1808 1809
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		rc = videobuf_querybuf(&fh->vb_vidq, b);
	else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
		rc = videobuf_querybuf(&fh->vb_vbiq, b);

	return rc;
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
}

static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	int rc;

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

1822 1823 1824 1825 1826 1827
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		rc = videobuf_qbuf(&fh->vb_vidq, b);
	else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
		rc = videobuf_qbuf(&fh->vb_vbiq, b);

	return rc;
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
}

static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
{
	struct au0828_fh *fh = priv;
	struct au0828_dev *dev = fh->dev;
	int rc;

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

	/* Workaround for a bug in the au0828 hardware design that sometimes
	   results in the colorspace being inverted */
	if (dev->greenscreen_detected == 1) {
		dprintk(1, "Detected green frame.  Resetting stream...\n");
		au0828_analog_stream_reset(dev);
		dev->greenscreen_detected = 0;
	}

1848 1849 1850 1851 1852 1853
	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		rc = videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
	else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
		rc = videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags & O_NONBLOCK);

	return rc;
1854 1855 1856 1857 1858 1859 1860 1861 1862
}

static struct v4l2_file_operations au0828_v4l_fops = {
	.owner      = THIS_MODULE,
	.open       = au0828_v4l2_open,
	.release    = au0828_v4l2_close,
	.read       = au0828_v4l2_read,
	.poll       = au0828_v4l2_poll,
	.mmap       = au0828_v4l2_mmap,
1863
	.unlocked_ioctl = video_ioctl2,
1864 1865 1866 1867 1868 1869 1870 1871
};

static const struct v4l2_ioctl_ops video_ioctl_ops = {
	.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,
1872
	.vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1873
	.vidioc_s_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
	.vidioc_g_audio             = vidioc_g_audio,
	.vidioc_s_audio             = vidioc_s_audio,
	.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_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,
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.vidioc_g_register          = vidioc_g_register,
	.vidioc_s_register          = vidioc_s_register,
#endif
1898
	.vidioc_g_chip_ident        = vidioc_g_chip_ident,
1899 1900 1901 1902 1903 1904
};

static const struct video_device au0828_video_template = {
	.fops                       = &au0828_v4l_fops,
	.release                    = video_device_release,
	.ioctl_ops 		    = &video_ioctl_ops,
1905 1906
	.tvnorms                    = V4L2_STD_NTSC_M,
	.current_norm               = V4L2_STD_NTSC_M,
1907 1908 1909 1910
};

/**************************************************************************/

1911 1912
int au0828_analog_register(struct au0828_dev *dev,
			   struct usb_interface *interface)
1913 1914
{
	int retval = -ENOMEM;
1915 1916
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *endpoint;
1917
	int i, ret;
1918 1919 1920

	dprintk(1, "au0828_analog_register called!\n");

1921 1922
	/* set au0828 usb interface0 to as5 */
	retval = usb_set_interface(dev->usbdev,
1923
			interface->cur_altsetting->desc.bInterfaceNumber, 5);
1924
	if (retval != 0) {
1925
		printk(KERN_INFO "Failure setting usb interface0 to as5\n");
1926 1927 1928 1929 1930
		return retval;
	}

	/* Figure out which endpoint has the isoc interface */
	iface_desc = interface->cur_altsetting;
1931
	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1932
		endpoint = &iface_desc->endpoint[i].desc;
1933 1934 1935 1936
		if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
		     == USB_DIR_IN) &&
		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
		     == USB_ENDPOINT_XFER_ISOC)) {
1937 1938 1939

			/* we find our isoc in endpoint */
			u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1940 1941
			dev->max_pkt_size = (tmp & 0x07ff) *
				(((tmp & 0x1800) >> 11) + 1);
1942 1943 1944
			dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
		}
	}
1945 1946
	if (!(dev->isoc_in_endpointaddr)) {
		printk(KERN_INFO "Could not locate isoc endpoint\n");
1947 1948 1949 1950
		kfree(dev);
		return -ENODEV;
	}

1951 1952 1953
	init_waitqueue_head(&dev->open);
	spin_lock_init(&dev->slock);

1954
	/* init video dma queues */
1955 1956
	INIT_LIST_HEAD(&dev->vidq.active);
	INIT_LIST_HEAD(&dev->vidq.queued);
1957 1958
	INIT_LIST_HEAD(&dev->vbiq.active);
	INIT_LIST_HEAD(&dev->vbiq.queued);
1959

1960 1961 1962 1963 1964 1965 1966 1967
	dev->vid_timeout.function = au0828_vid_buffer_timeout;
	dev->vid_timeout.data = (unsigned long) dev;
	init_timer(&dev->vid_timeout);

	dev->vbi_timeout.function = au0828_vbi_buffer_timeout;
	dev->vbi_timeout.data = (unsigned long) dev;
	init_timer(&dev->vbi_timeout);

1968 1969 1970 1971 1972 1973 1974 1975 1976
	dev->width = NTSC_STD_W;
	dev->height = NTSC_STD_H;
	dev->field_size = dev->width * dev->height;
	dev->frame_size = dev->field_size << 1;
	dev->bytesperline = dev->width << 1;
	dev->ctrl_ainput = 0;

	/* allocate and fill v4l2 video struct */
	dev->vdev = video_device_alloc();
1977
	if (NULL == dev->vdev) {
1978 1979 1980 1981
		dprintk(1, "Can't allocate video_device.\n");
		return -ENOMEM;
	}

1982
	/* allocate the VBI struct */
1983
	dev->vbi_dev = video_device_alloc();
1984
	if (NULL == dev->vbi_dev) {
1985
		dprintk(1, "Can't allocate vbi_device.\n");
1986 1987
		ret = -ENOMEM;
		goto err_vdev;
1988 1989 1990 1991 1992
	}

	/* Fill the video capture device struct */
	*dev->vdev = au0828_video_template;
	dev->vdev->parent = &dev->usbdev->dev;
1993
	dev->vdev->lock = &dev->lock;
1994 1995 1996 1997 1998
	strcpy(dev->vdev->name, "au0828a video");

	/* Setup the VBI device */
	*dev->vbi_dev = au0828_video_template;
	dev->vbi_dev->parent = &dev->usbdev->dev;
1999
	dev->vbi_dev->lock = &dev->lock;
2000 2001 2002
	strcpy(dev->vbi_dev->name, "au0828a vbi");

	/* Register the v4l2 device */
2003
	video_set_drvdata(dev->vdev, dev);
2004 2005 2006 2007
	retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
	if (retval != 0) {
		dprintk(1, "unable to register video device (error = %d).\n",
			retval);
2008 2009
		ret = -ENODEV;
		goto err_vbi_dev;
2010 2011 2012
	}

	/* Register the vbi device */
2013
	video_set_drvdata(dev->vbi_dev, dev);
2014 2015 2016 2017
	retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
	if (retval != 0) {
		dprintk(1, "unable to register vbi device (error = %d).\n",
			retval);
2018 2019
		ret = -ENODEV;
		goto err_vbi_dev;
2020 2021
	}

2022
	dprintk(1, "%s completed!\n", __func__);
2023 2024

	return 0;
2025 2026 2027 2028 2029 2030

err_vbi_dev:
	video_device_release(dev->vbi_dev);
err_vdev:
	video_device_release(dev->vdev);
	return ret;
2031 2032
}