au0828-video.c 50.9 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
/*
 * 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:
 *
 * The hardware scaler supported is unimplemented
 * AC97 audio support is unimplemented (only i2s audio mode)
 *
 */

30 31
#include "au0828.h"

32
#include <linux/module.h>
33
#include <linux/slab.h>
34 35 36 37
#include <linux/init.h>
#include <linux/device.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
38
#include <media/v4l2-event.h>
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <media/tuner.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) { \
55
		pr_info("au0828 %s :"fmt, \
56 57 58 59
		       __func__ , ##arg);	   \
	} \
  } while (0)

60 61 62 63 64 65
static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
{
	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, val);
}

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 103 104 105 106 107
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) {
108
		pr_info("v4l2 ioctl: device not present\n");
109 110 111 112
		return -ENODEV;
	}

	if (dev->dev_state & DEV_MISCONFIGURED) {
113
		pr_info("v4l2 ioctl: device is misconfigured; "
114 115 116 117 118 119 120 121 122 123 124 125 126
		       "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);
127
	unsigned long flags = 0;
128
	int i;
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

	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 */
145
	spin_lock_irqsave(&dev->slock, flags);
146
	dev->isoc_ctl.isoc_copy(dev, urb);
147
	spin_unlock_irqrestore(&dev->slock, flags);
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);
	}
161
	dev->stream_state = STREAM_ON;
162 163 164 165 166
}

/*
 * Stop and Deallocate URBs
 */
167
static void au0828_uninit_isoc(struct au0828_dev *dev)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
	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]) {
184
				usb_free_coherent(dev->usbdev,
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
					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;
201 202

	dev->stream_state = STREAM_OFF;
203 204 205 206 207
}

/*
 * Allocate URBs and start IRQ
 */
208 209 210
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))
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 248 249 250 251 252
{
	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");

	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;

253
		dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
			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;
273
		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
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

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

	/* 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,
302 303
				 struct au0828_dmaqueue *dma_q,
				 struct au0828_buffer *buf)
304
{
305 306
	struct vb2_v4l2_buffer *vb = &buf->vb;
	struct vb2_queue *q = vb->vb2_buf.vb2_queue;
307

308
	/* Advice that buffer was filled */
309
	au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
310

311
	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
312
		vb->sequence = dev->frame_count++;
313
	else
314
		vb->sequence = dev->vbi_frame_count++;
315

316
	vb->field = V4L2_FIELD_INTERLACED;
317
	vb->vb2_buf.timestamp = ktime_get_ns();
318
	vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
319 320
}

321 322 323 324 325 326 327 328 329 330 331 332 333
/*
 * 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 @@@@ */

334 335 336
	if (len == 0)
		return;

337 338
	if (dma_q->pos + len > buf->length)
		len = buf->length - dma_q->pos;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

	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;

356
	if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
357 358
		au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
			       ((char *)startwrite + lencopy) -
359 360
			       ((char *)outp + buf->length));
		remain = (char *)outp + buf->length - (char *)startwrite;
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
		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 +
378
		    buf->length) {
379
			au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
380
				       ((char *)startwrite + lencopy) -
381 382
				       ((char *)outp + buf->length));
			lencopy = remain = (char *)outp + buf->length -
383 384 385 386 387 388 389 390 391 392 393 394
					   (char *)startwrite;
		}
		if (lencopy <= 0)
			break;

		memcpy(startwrite, startread, lencopy);

		remain -= lencopy;
	}

	if (offset > 1440) {
		/* We have enough data to check for greenscreen */
395
		if (outp[0] < 0x60 && outp[1440] < 0x60)
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
			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 */
418 419 420 421 422
	*buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
	/* Cleans up buffer - Useful for testing for frame/URB loss */
	list_del(&(*buf)->list);
	dma_q->pos = 0;
	(*buf)->vb_buf = (*buf)->mem;
423 424 425 426 427
	dev->isoc_ctl.buf = *buf;

	return;
}

428 429 430 431 432 433 434
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;
435
	int bytesperline;
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	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;
	}

458 459
	bytesperline = dev->vbi_width;

460 461
	if (dma_q->pos + len > buf->length)
		len = buf->length - dma_q->pos;
462 463 464 465 466 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

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

	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 */
493
	*buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
L
Lucas De Marchi 已提交
494
	/* Cleans up buffer - Useful for testing for frame/URB loss */
495 496 497
	list_del(&(*buf)->list);
	dma_q->pos = 0;
	(*buf)->vb_buf = (*buf)->mem;
498 499 500 501
	dev->isoc_ctl.vbi_buf = *buf;
	return;
}

502 503 504 505 506 507
/*
 * 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;
508
	struct au0828_buffer    *vbi_buf;
509
	struct au0828_dmaqueue  *dma_q = urb->context;
510
	struct au0828_dmaqueue  *vbi_dma_q = &dev->vbiq;
511
	unsigned char *outp = NULL;
512
	unsigned char *vbioutp = NULL;
513 514 515
	int i, len = 0, rc = 1;
	unsigned char *p;
	unsigned char fbyte;
516 517
	unsigned int vbi_field_size;
	unsigned int remain, lencopy;
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

	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)
534
		outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
535

536 537
	vbi_buf = dev->isoc_ctl.vbi_buf;
	if (vbi_buf != NULL)
538
		vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
539

540 541 542 543 544 545 546 547 548
	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;
		}

549
		if (urb->iso_frame_desc[i].actual_length <= 0)
550
			continue;
551

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
		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");
568
			if (fbyte & 0x40) {
569 570
				/* VBI */
				if (vbi_buf != NULL)
571
					buffer_filled(dev, vbi_dma_q, vbi_buf);
572 573 574 575
				vbi_get_next_buf(vbi_dma_q, &vbi_buf);
				if (vbi_buf == NULL)
					vbioutp = NULL;
				else
576
					vbioutp = vb2_plane_vaddr(
577
						&vbi_buf->vb.vb2_buf, 0);
578 579

				/* Video */
580 581 582
				if (buf != NULL)
					buffer_filled(dev, dma_q, buf);
				get_next_buf(dma_q, &buf);
583
				if (buf == NULL)
584
					outp = NULL;
585
				else
586 587
					outp = vb2_plane_vaddr(
						&buf->vb.vb2_buf, 0);
588 589 590 591 592 593 594 595 596

				/* 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));
597 598 599
			}

			if (buf != NULL) {
600
				if (fbyte & 0x40)
601
					buf->top_field = 1;
602
				else
603 604 605
					buf->top_field = 0;
			}

606 607 608 609 610 611 612 613 614
			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;
615 616
			dma_q->pos = 0;
		}
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

		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)
636 637 638 639 640
			au0828_copy_video(dev, dma_q, buf, p, outp, len);
	}
	return rc;
}

641 642 643 644
static int au0828_enable_analog_tuner(struct au0828_dev *dev)
{
#ifdef CONFIG_MEDIA_CONTROLLER
	struct media_device *mdev = dev->media_dev;
645
	struct media_entity *source;
646
	struct media_link *link, *found_link = NULL;
647
	int ret, active_links = 0;
648

649
	if (!mdev || !dev->decoder)
650 651 652 653 654 655 656 657 658
		return 0;

	/*
	 * This will find the tuner that is connected into the decoder.
	 * Technically, this is not 100% correct, as the device may be
	 * using an analog input instead of the tuner. However, as we can't
	 * do DVB streaming while the DMA engine is being used for V4L2,
	 * this should be enough for the actual needs.
	 */
659
	list_for_each_entry(link, &dev->decoder->links, list) {
660
		if (link->sink->entity == dev->decoder) {
661 662 663 664 665 666 667 668 669 670 671
			found_link = link;
			if (link->flags & MEDIA_LNK_FL_ENABLED)
				active_links++;
			break;
		}
	}

	if (active_links == 1 || !found_link)
		return 0;

	source = found_link->source->entity;
672
	list_for_each_entry(link, &source->links, list) {
673 674 675 676 677
		struct media_entity *sink;
		int flags = 0;

		sink = link->sink->entity;

678
		if (sink == dev->decoder)
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
			flags = MEDIA_LNK_FL_ENABLED;

		ret = media_entity_setup_link(link, flags);
		if (ret) {
			pr_err(
				"Couldn't change link %s->%s to %s. Error %d\n",
				source->name, sink->name,
				flags ? "enabled" : "disabled",
				ret);
			return ret;
		} else
			au0828_isocdbg(
				"link %s->%s was %s\n",
				source->name, sink->name,
				flags ? "ENABLED" : "disabled");
	}
#endif
	return 0;
}

699
static int queue_setup(struct vb2_queue *vq,
700 701
		       unsigned int *nbuffers, unsigned int *nplanes,
		       unsigned int sizes[], void *alloc_ctxs[])
702
{
703
	struct au0828_dev *dev = vb2_get_drv_priv(vq);
704
	unsigned long size = dev->height * dev->bytesperline;
705

706 707
	if (*nplanes)
		return sizes[0] < size ? -EINVAL : 0;
708 709
	*nplanes = 1;
	sizes[0] = size;
710

711 712
	au0828_enable_analog_tuner(dev);

713
	return 0;
714 715 716
}

static int
717
buffer_prepare(struct vb2_buffer *vb)
718
{
719 720 721
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct au0828_buffer *buf = container_of(vbuf,
				struct au0828_buffer, vb);
722
	struct au0828_dev    *dev = vb2_get_drv_priv(vb->vb2_queue);
723

724
	buf->length = dev->height * dev->bytesperline;
725

726 727 728
	if (vb2_plane_size(vb, 0) < buf->length) {
		pr_err("%s data will not fit into plane (%lu < %lu)\n",
			__func__, vb2_plane_size(vb, 0), buf->length);
729 730
		return -EINVAL;
	}
731
	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
732 733 734 735
	return 0;
}

static void
736
buffer_queue(struct vb2_buffer *vb)
737
{
738 739
	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
	struct au0828_buffer    *buf     = container_of(vbuf,
740 741
							struct au0828_buffer,
							vb);
742
	struct au0828_dev       *dev     = vb2_get_drv_priv(vb->vb2_queue);
743
	struct au0828_dmaqueue  *vidq    = &dev->vidq;
744
	unsigned long flags = 0;
745

746 747
	buf->mem = vb2_plane_vaddr(vb, 0);
	buf->length = vb2_plane_size(vb, 0);
748

749 750 751
	spin_lock_irqsave(&dev->slock, flags);
	list_add_tail(&buf->list, &vidq->active);
	spin_unlock_irqrestore(&dev->slock, flags);
752 753 754 755 756 757 758 759 760 761 762 763
}

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
 */
764
static int au0828_analog_stream_enable(struct au0828_dev *d)
765
{
766
	struct usb_interface *iface;
767
	int ret, h, w;
768

769
	dprintk(1, "au0828_analog_stream_enable called\n");
770 771 772 773 774 775 776

	iface = usb_ifnum_to_if(d->usbdev, 0);
	if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
		dprintk(1, "Changing intf#0 to alt 5\n");
		/* set au0828 interface0 to AS5 here again */
		ret = usb_set_interface(d->usbdev, 0, 5);
		if (ret < 0) {
777
			pr_info("Au0828 can't set alt setting to 5!\n");
778 779 780 781
			return -EBUSY;
		}
	}

782 783
	h = d->height / 2 + 2;
	w = d->width * 2;
784

785 786 787 788 789
	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);
790 791
	au0828_writereg(d, 0x114, w & 0xff);
	au0828_writereg(d, 0x115, w >> 8);
792
	/* set y position */
793
	au0828_writereg(d, 0x112, 0x00);
794
	au0828_writereg(d, 0x113, 0x00);
795 796
	au0828_writereg(d, 0x116, h & 0xff);
	au0828_writereg(d, 0x117, h >> 8);
797 798 799 800 801
	au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);

	return 0;
}

802
static int au0828_analog_stream_disable(struct au0828_dev *d)
803 804 805 806 807 808
{
	dprintk(1, "au0828_analog_stream_disable called\n");
	au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
	return 0;
}

809
static void au0828_analog_stream_reset(struct au0828_dev *dev)
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
{
	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;
825
	if (dev->dev_state == DEV_DISCONNECTED)
826
		return -ENODEV;
827
	else if (ret) {
828
		dev->dev_state = DEV_MISCONFIGURED;
829
		dprintk(1, "%s device is misconfigured!\n", __func__);
830 831 832 833 834
		return ret;
	}
	return 0;
}

835
int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
836
{
837 838
	struct au0828_dev *dev = vb2_get_drv_priv(vq);
	int rc = 0;
839

840 841
	dprintk(1, "au0828_start_analog_streaming called %d\n",
		dev->streaming_users);
842

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
		dev->frame_count = 0;
	else
		dev->vbi_frame_count = 0;

	if (dev->streaming_users == 0) {
		/* If we were doing ac97 instead of i2s, it would go here...*/
		au0828_i2s_init(dev);
		rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
				   AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
				   au0828_isoc_copy);
		if (rc < 0) {
			pr_info("au0828_init_isoc failed\n");
			return rc;
		}
858

859 860 861 862 863 864 865 866 867 868 869 870 871
		if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
			v4l2_device_call_all(&dev->v4l2_dev, 0, video,
						s_stream, 1);
			dev->vid_timeout_running = 1;
			mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
		} else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
			dev->vbi_timeout_running = 1;
			mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
		}
	}
	dev->streaming_users++;
	return rc;
}
872

873
static void au0828_stop_streaming(struct vb2_queue *vq)
874
{
875 876 877
	struct au0828_dev *dev = vb2_get_drv_priv(vq);
	struct au0828_dmaqueue *vidq = &dev->vidq;
	unsigned long flags = 0;
878

879
	dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
880

881 882 883 884 885 886 887 888 889
	if (dev->streaming_users-- == 1)
		au0828_uninit_isoc(dev);

	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
	dev->vid_timeout_running = 0;
	del_timer_sync(&dev->vid_timeout);

	spin_lock_irqsave(&dev->slock, flags);
	if (dev->isoc_ctl.buf != NULL) {
890 891
		vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
				VB2_BUF_STATE_ERROR);
892
		dev->isoc_ctl.buf = NULL;
893
	}
894 895
	while (!list_empty(&vidq->active)) {
		struct au0828_buffer *buf;
896

897
		buf = list_entry(vidq->active.next, struct au0828_buffer, list);
898
		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
899 900 901
		list_del(&buf->list);
	}
	spin_unlock_irqrestore(&dev->slock, flags);
902
}
903

904
void au0828_stop_vbi_streaming(struct vb2_queue *vq)
905
{
906 907 908
	struct au0828_dev *dev = vb2_get_drv_priv(vq);
	struct au0828_dmaqueue *vbiq = &dev->vbiq;
	unsigned long flags = 0;
909

910 911
	dprintk(1, "au0828_stop_vbi_streaming called %d\n",
		dev->streaming_users);
912

913 914
	if (dev->streaming_users-- == 1)
		au0828_uninit_isoc(dev);
915

916 917
	spin_lock_irqsave(&dev->slock, flags);
	if (dev->isoc_ctl.vbi_buf != NULL) {
918
		vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
919 920 921 922 923 924 925 926
				VB2_BUF_STATE_ERROR);
		dev->isoc_ctl.vbi_buf = NULL;
	}
	while (!list_empty(&vbiq->active)) {
		struct au0828_buffer *buf;

		buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
		list_del(&buf->list);
927
		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
928 929
	}
	spin_unlock_irqrestore(&dev->slock, flags);
930

931 932
	dev->vbi_timeout_running = 0;
	del_timer_sync(&dev->vbi_timeout);
933 934
}

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
static struct vb2_ops au0828_video_qops = {
	.queue_setup     = queue_setup,
	.buf_prepare     = buffer_prepare,
	.buf_queue       = buffer_queue,
	.start_streaming = au0828_start_analog_streaming,
	.stop_streaming  = au0828_stop_streaming,
	.wait_prepare    = vb2_ops_wait_prepare,
	.wait_finish     = vb2_ops_wait_finish,
};

/* ------------------------------------------------------------------
   V4L2 interface
   ------------------------------------------------------------------*/
/*
 * au0828_analog_unregister
 * unregister v4l2 devices
 */
void au0828_analog_unregister(struct au0828_dev *dev)
953
{
954 955
	dprintk(1, "au0828_analog_unregister called\n");
	mutex_lock(&au0828_sysfs_lock);
956 957
	video_unregister_device(&dev->vdev);
	video_unregister_device(&dev->vbi_dev);
958
	mutex_unlock(&au0828_sysfs_lock);
959 960
}

961 962 963
/* 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) */
964
static void au0828_vid_buffer_timeout(unsigned long data)
965 966 967 968 969
{
	struct au0828_dev *dev = (struct au0828_dev *) data;
	struct au0828_dmaqueue *dma_q = &dev->vidq;
	struct au0828_buffer *buf;
	unsigned char *vid_data;
970
	unsigned long flags = 0;
971

972
	spin_lock_irqsave(&dev->slock, flags);
973 974 975

	buf = dev->isoc_ctl.buf;
	if (buf != NULL) {
976
		vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
977
		memset(vid_data, 0x00, buf->length); /* Blank green frame */
978 979
		buffer_filled(dev, dma_q, buf);
	}
980 981 982 983
	get_next_buf(dma_q, &buf);

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

985
	spin_unlock_irqrestore(&dev->slock, flags);
986 987
}

988
static void au0828_vbi_buffer_timeout(unsigned long data)
989 990 991 992 993
{
	struct au0828_dev *dev = (struct au0828_dev *) data;
	struct au0828_dmaqueue *dma_q = &dev->vbiq;
	struct au0828_buffer *buf;
	unsigned char *vbi_data;
994
	unsigned long flags = 0;
995

996
	spin_lock_irqsave(&dev->slock, flags);
997 998 999

	buf = dev->isoc_ctl.vbi_buf;
	if (buf != NULL) {
1000
		vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
1001
		memset(vbi_data, 0x00, buf->length);
1002
		buffer_filled(dev, dma_q, buf);
1003
	}
1004
	vbi_get_next_buf(dma_q, &buf);
1005

1006 1007 1008
	if (dev->vbi_timeout_running == 1)
		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
	spin_unlock_irqrestore(&dev->slock, flags);
1009 1010
}

1011 1012
static int au0828_v4l2_open(struct file *filp)
{
1013
	struct au0828_dev *dev = video_drvdata(filp);
1014
	int ret;
1015

1016 1017 1018 1019
	dprintk(1,
		"%s called std_set %d dev_state %d stream users %d users %d\n",
		__func__, dev->std_set_in_tuner_core, dev->dev_state,
		dev->streaming_users, dev->users);
1020

1021
	if (mutex_lock_interruptible(&dev->lock))
1022
		return -ERESTARTSYS;
1023 1024 1025 1026 1027 1028 1029

	ret = v4l2_fh_open(filp);
	if (ret) {
		au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
				__func__, ret);
		mutex_unlock(&dev->lock);
		return ret;
1030
	}
1031

1032
	if (dev->users == 0) {
1033 1034 1035 1036 1037 1038
		au0828_analog_stream_enable(dev);
		au0828_analog_stream_reset(dev);
		dev->stream_state = STREAM_OFF;
		dev->dev_state |= DEV_INITIALIZED;
	}
	dev->users++;
1039
	mutex_unlock(&dev->lock);
1040 1041 1042 1043 1044 1045
	return ret;
}

static int au0828_v4l2_close(struct file *filp)
{
	int ret;
1046 1047 1048 1049 1050 1051 1052
	struct au0828_dev *dev = video_drvdata(filp);
	struct video_device *vdev = video_devdata(filp);

	dprintk(1,
		"%s called std_set %d dev_state %d stream users %d users %d\n",
		__func__, dev->std_set_in_tuner_core, dev->dev_state,
		dev->streaming_users, dev->users);
1053

1054
	mutex_lock(&dev->lock);
1055
	if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
1056 1057 1058
		/* Cancel timeout thread in case they didn't call streamoff */
		dev->vid_timeout_running = 0;
		del_timer_sync(&dev->vid_timeout);
1059 1060
	} else if (vdev->vfl_type == VFL_TYPE_VBI &&
			dev->vbi_timeout_running) {
1061 1062 1063
		/* Cancel timeout thread in case they didn't call streamoff */
		dev->vbi_timeout_running = 0;
		del_timer_sync(&dev->vbi_timeout);
1064 1065
	}

1066 1067
	if (dev->dev_state == DEV_DISCONNECTED)
		goto end;
1068

1069
	if (dev->users == 1) {
1070
		/* Save some power by putting tuner to sleep */
1071
		v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1072
		dev->std_set_in_tuner_core = 0;
1073

1074 1075 1076
		/* When close the device, set the usb intf0 into alt0 to free
		   USB bandwidth */
		ret = usb_set_interface(dev->usbdev, 0, 0);
1077
		if (ret < 0)
1078
			pr_info("Au0828 can't set alternate to 0!\n");
1079
	}
1080 1081
end:
	_vb2_fop_release(filp, NULL);
1082
	dev->users--;
1083
	mutex_unlock(&dev->lock);
1084 1085 1086
	return 0;
}

1087 1088 1089 1090 1091 1092 1093 1094
/* Must be called with dev->lock held */
static void au0828_init_tuner(struct au0828_dev *dev)
{
	struct v4l2_frequency f = {
		.frequency = dev->ctrl_freq,
		.type = V4L2_TUNER_ANALOG_TV,
	};

1095 1096 1097
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1098 1099 1100 1101 1102 1103 1104
	if (dev->std_set_in_tuner_core)
		return;
	dev->std_set_in_tuner_core = 1;
	i2c_gate_ctrl(dev, 1);
	/* 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 */
1105
	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1106 1107 1108 1109
	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
	i2c_gate_ctrl(dev, 0);
}

1110 1111 1112 1113 1114 1115 1116 1117 1118
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;

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

	/* format->fmt.pix.width only support 720 and height 480 */
1123
	if (width != 720)
1124
		width = 720;
1125
	if (height != 480)
1126 1127 1128 1129 1130 1131 1132 1133 1134
		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;
1135
	format->fmt.pix.priv = 0;
1136

1137
	if (cmd == VIDIOC_TRY_FMT)
1138 1139 1140 1141 1142 1143 1144 1145 1146
		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;

1147
	if (dev->stream_state == STREAM_ON) {
1148
		dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1149 1150
		ret = au0828_stream_interrupt(dev);
		if (ret != 0) {
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
			dprintk(1, "error interrupting video stream!\n");
			return ret;
		}
	}

	au0828_analog_stream_enable(dev);

	return 0;
}

static int vidioc_querycap(struct file *file, void  *priv,
			   struct v4l2_capability *cap)
{
H
Hans Verkuil 已提交
1164
	struct video_device *vdev = video_devdata(file);
1165 1166 1167 1168
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1169 1170

	strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1171
	strlcpy(cap->card, dev->board.name, sizeof(cap->card));
H
Hans Verkuil 已提交
1172
	usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1173

H
Hans Verkuil 已提交
1174 1175
	/* set the device capabilities */
	cap->device_caps = V4L2_CAP_AUDIO |
1176 1177 1178
		V4L2_CAP_READWRITE |
		V4L2_CAP_STREAMING |
		V4L2_CAP_TUNER;
H
Hans Verkuil 已提交
1179 1180 1181 1182 1183 1184
	if (vdev->vfl_type == VFL_TYPE_GRABBER)
		cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
	else
		cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
		V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
1185 1186 1187 1188 1189 1190
	return 0;
}

static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
					struct v4l2_fmtdesc *f)
{
1191
	if (f->index)
1192 1193
		return -EINVAL;

1194 1195
	dprintk(1, "%s called\n", __func__);

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	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)
{
1208 1209 1210 1211
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1212 1213 1214 1215 1216 1217 1218 1219

	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;
1220
	f->fmt.pix.priv = 0;
1221 1222 1223 1224 1225 1226
	return 0;
}

static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
				  struct v4l2_format *f)
{
1227 1228 1229 1230
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1231 1232 1233 1234 1235 1236 1237

	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)
{
1238
	struct au0828_dev *dev = video_drvdata(file);
1239 1240
	int rc;

1241 1242 1243
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1244 1245 1246 1247
	rc = check_dev(dev);
	if (rc < 0)
		return rc;

1248
	if (vb2_is_busy(&dev->vb_vidq)) {
1249
		pr_info("%s queue busy\n", __func__);
1250 1251 1252 1253
		rc = -EBUSY;
		goto out;
	}

1254
	rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1255 1256 1257 1258
out:
	return rc;
}

1259
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1260
{
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

	if (norm == dev->std)
		return 0;

	if (dev->streaming_users > 0)
		return -EBUSY;
1271

1272 1273 1274 1275
	dev->std = norm;

	au0828_init_tuner(dev);

1276
	i2c_gate_ctrl(dev, 1);
1277

1278 1279 1280 1281 1282
	/*
	 * FIXME: when we support something other than 60Hz standards,
	 * we are going to have to make the au0828 bridge adjust the size
	 * of its capture buffer, which is currently hardcoded at 720x480
	 */
1283

1284
	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1285

1286
	i2c_gate_ctrl(dev, 0);
1287

1288 1289 1290
	return 0;
}

1291 1292
static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
{
1293 1294 1295 1296
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1297 1298 1299 1300 1301

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

1302 1303 1304
static int vidioc_enum_input(struct file *file, void *priv,
				struct v4l2_input *input)
{
1305
	struct au0828_dev *dev = video_drvdata(file);
1306 1307 1308
	unsigned int tmp;

	static const char *inames[] = {
1309
		[AU0828_VMUX_UNDEFINED] = "Undefined",
1310 1311 1312 1313 1314 1315 1316 1317
		[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"
	};

1318 1319 1320
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1321 1322
	tmp = input->index;

1323
	if (tmp >= AU0828_MAX_INPUT)
1324
		return -EINVAL;
1325
	if (AUVI_INPUT(tmp).type == 0)
1326 1327 1328
		return -EINVAL;

	input->index = tmp;
1329
	strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1330
	if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1331
	    (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1332
		input->type |= V4L2_INPUT_TYPE_TUNER;
1333 1334
		input->audioset = 1;
	} else {
1335
		input->type |= V4L2_INPUT_TYPE_CAMERA;
1336 1337
		input->audioset = 2;
	}
1338

1339
	input->std = dev->vdev.tvnorms;
1340 1341 1342 1343 1344 1345

	return 0;
}

static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
1346 1347 1348 1349 1350
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1351 1352 1353 1354
	*i = dev->ctrl_input;
	return 0;
}

1355
static void au0828_s_input(struct au0828_dev *dev, int index)
1356 1357 1358
{
	int i;

1359 1360 1361
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1362
	switch (AUVI_INPUT(index).type) {
1363 1364
	case AU0828_VMUX_SVIDEO:
		dev->input_type = AU0828_VMUX_SVIDEO;
1365
		dev->ctrl_ainput = 1;
1366 1367 1368
		break;
	case AU0828_VMUX_COMPOSITE:
		dev->input_type = AU0828_VMUX_COMPOSITE;
1369
		dev->ctrl_ainput = 1;
1370 1371 1372
		break;
	case AU0828_VMUX_TELEVISION:
		dev->input_type = AU0828_VMUX_TELEVISION;
1373
		dev->ctrl_ainput = 0;
1374 1375
		break;
	default:
1376
		dprintk(1, "unknown input type set [%d]\n",
1377 1378
			AUVI_INPUT(index).type);
		break;
1379 1380
	}

1381 1382
	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
			AUVI_INPUT(index).vmux, 0, 0);
1383 1384 1385

	for (i = 0; i < AU0828_MAX_INPUT; i++) {
		int enable = 0;
1386
		if (AUVI_INPUT(i).audio_setup == NULL)
1387 1388 1389 1390 1391 1392 1393
			continue;

		if (i == index)
			enable = 1;
		else
			enable = 0;
		if (enable) {
1394
			(AUVI_INPUT(i).audio_setup)(dev, enable);
1395 1396 1397
		} else {
			/* Make sure we leave it turned on if some
			   other input is routed to this callback */
1398 1399 1400
			if ((AUVI_INPUT(i).audio_setup) !=
			    ((AUVI_INPUT(index).audio_setup))) {
				(AUVI_INPUT(i).audio_setup)(dev, enable);
1401 1402 1403 1404
			}
		}
	}

1405 1406
	v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
			AUVI_INPUT(index).amux, 0, 0);
1407 1408 1409 1410
}

static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
{
1411
	struct au0828_dev *dev = video_drvdata(file);
1412 1413 1414 1415 1416 1417 1418 1419 1420

	dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
		index);
	if (index >= AU0828_MAX_INPUT)
		return -EINVAL;
	if (AUVI_INPUT(index).type == 0)
		return -EINVAL;
	dev->ctrl_input = index;
	au0828_s_input(dev, index);
1421 1422 1423
	return 0;
}

1424 1425 1426 1427 1428
static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
{
	if (a->index > 1)
		return -EINVAL;

1429 1430
	dprintk(1, "%s called\n", __func__);

1431 1432 1433 1434 1435 1436 1437 1438 1439
	if (a->index == 0)
		strcpy(a->name, "Television");
	else
		strcpy(a->name, "Line in");

	a->capability = V4L2_AUDCAP_STEREO;
	return 0;
}

1440 1441
static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
{
1442 1443 1444 1445
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1446

1447 1448
	a->index = dev->ctrl_ainput;
	if (a->index == 0)
1449 1450 1451 1452 1453 1454 1455 1456
		strcpy(a->name, "Television");
	else
		strcpy(a->name, "Line in");

	a->capability = V4L2_AUDCAP_STEREO;
	return 0;
}

1457
static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1458
{
1459
	struct au0828_dev *dev = video_drvdata(file);
1460

1461
	if (a->index != dev->ctrl_ainput)
1462
		return -EINVAL;
1463 1464 1465

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1466 1467 1468 1469 1470
	return 0;
}

static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
{
1471
	struct au0828_dev *dev = video_drvdata(file);
1472

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

1476 1477 1478
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1479
	strcpy(t->name, "Auvitek tuner");
1480 1481 1482

	au0828_init_tuner(dev);
	i2c_gate_ctrl(dev, 1);
1483
	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1484
	i2c_gate_ctrl(dev, 0);
1485 1486 1487 1488
	return 0;
}

static int vidioc_s_tuner(struct file *file, void *priv,
1489
				const struct v4l2_tuner *t)
1490
{
1491
	struct au0828_dev *dev = video_drvdata(file);
1492

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

1496 1497 1498
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1499
	au0828_init_tuner(dev);
1500
	i2c_gate_ctrl(dev, 1);
1501
	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1502
	i2c_gate_ctrl(dev, 0);
1503

1504 1505
	dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
		t->afc);
1506

1507 1508 1509 1510 1511 1512 1513
	return 0;

}

static int vidioc_g_frequency(struct file *file, void *priv,
				struct v4l2_frequency *freq)
{
1514
	struct au0828_dev *dev = video_drvdata(file);
1515

1516 1517
	if (freq->tuner != 0)
		return -EINVAL;
1518 1519
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1520 1521 1522 1523 1524
	freq->frequency = dev->ctrl_freq;
	return 0;
}

static int vidioc_s_frequency(struct file *file, void *priv,
1525
				const struct v4l2_frequency *freq)
1526
{
1527
	struct au0828_dev *dev = video_drvdata(file);
1528
	struct v4l2_frequency new_freq = *freq;
1529

1530
	if (freq->tuner != 0)
1531 1532
		return -EINVAL;

1533 1534 1535
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1536
	au0828_init_tuner(dev);
1537
	i2c_gate_ctrl(dev, 1);
1538

1539
	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1540 1541 1542
	/* Get the actual set (and possibly clamped) frequency */
	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
	dev->ctrl_freq = new_freq.frequency;
1543

1544
	i2c_gate_ctrl(dev, 0);
1545

1546 1547 1548 1549 1550
	au0828_analog_stream_reset(dev);

	return 0;
}

1551 1552 1553 1554 1555 1556

/* RAW VBI ioctls */

static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
				struct v4l2_format *format)
{
1557 1558 1559 1560
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571

	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;
1572
	memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1573 1574 1575 1576

	return 0;
}

1577 1578 1579
static int vidioc_cropcap(struct file *file, void *priv,
			  struct v4l2_cropcap *cc)
{
1580
	struct au0828_dev *dev = video_drvdata(file);
1581

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

1585 1586 1587
	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);

1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
	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;
}

1601
#ifdef CONFIG_VIDEO_ADV_DEBUG
1602 1603 1604
static int vidioc_g_register(struct file *file, void *priv,
			     struct v4l2_dbg_register *reg)
{
1605 1606 1607 1608
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1609

1610
	reg->val = au0828_read(dev, reg->reg);
H
Hans Verkuil 已提交
1611
	reg->size = 1;
1612
	return 0;
1613 1614 1615
}

static int vidioc_s_register(struct file *file, void *priv,
1616
			     const struct v4l2_dbg_register *reg)
1617
{
1618 1619 1620 1621
	struct au0828_dev *dev = video_drvdata(file);

	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
		dev->std_set_in_tuner_core, dev->dev_state);
1622

1623
	return au0828_writereg(dev, reg->reg, reg->val);
1624
}
1625
#endif
1626

1627 1628 1629 1630
static int vidioc_log_status(struct file *file, void *fh)
{
	struct video_device *vdev = video_devdata(file);

1631 1632
	dprintk(1, "%s called\n", __func__);

1633 1634 1635 1636 1637
	v4l2_ctrl_log_status(file, fh);
	v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
	return 0;
}

1638 1639 1640 1641 1642
void au0828_v4l2_suspend(struct au0828_dev *dev)
{
	struct urb *urb;
	int i;

1643 1644
	pr_info("stopping V4L2\n");

1645
	if (dev->stream_state == STREAM_ON) {
1646
		pr_info("stopping V4L2 active URBs\n");
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
		au0828_analog_stream_disable(dev);
		/* stop urbs */
		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->vid_timeout_running)
		del_timer_sync(&dev->vid_timeout);
	if (dev->vbi_timeout_running)
		del_timer_sync(&dev->vbi_timeout);
}

void au0828_v4l2_resume(struct au0828_dev *dev)
{
	int i, rc;

1670 1671
	pr_info("restarting V4L2\n");

1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
	if (dev->stream_state == STREAM_ON) {
		au0828_stream_interrupt(dev);
		au0828_init_tuner(dev);
	}

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

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

	au0828_analog_stream_enable(dev);

	if (!(dev->stream_state == STREAM_ON)) {
		au0828_analog_stream_reset(dev);
		/* submit urbs */
		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);
			}
		}
	}
}

1701 1702 1703 1704
static struct v4l2_file_operations au0828_v4l_fops = {
	.owner      = THIS_MODULE,
	.open       = au0828_v4l2_open,
	.release    = au0828_v4l2_close,
1705 1706 1707
	.read       = vb2_fop_read,
	.poll       = vb2_fop_poll,
	.mmap       = vb2_fop_mmap,
1708
	.unlocked_ioctl = video_ioctl2,
1709 1710 1711 1712 1713 1714 1715 1716
};

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,
1717
	.vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1718
	.vidioc_try_fmt_vbi_cap     = vidioc_g_fmt_vbi_cap,
1719
	.vidioc_s_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1720
	.vidioc_enumaudio           = vidioc_enumaudio,
1721 1722 1723
	.vidioc_g_audio             = vidioc_g_audio,
	.vidioc_s_audio             = vidioc_s_audio,
	.vidioc_cropcap             = vidioc_cropcap,
1724 1725 1726 1727 1728 1729 1730 1731 1732

	.vidioc_reqbufs             = vb2_ioctl_reqbufs,
	.vidioc_create_bufs         = vb2_ioctl_create_bufs,
	.vidioc_prepare_buf         = vb2_ioctl_prepare_buf,
	.vidioc_querybuf            = vb2_ioctl_querybuf,
	.vidioc_qbuf                = vb2_ioctl_qbuf,
	.vidioc_dqbuf               = vb2_ioctl_dqbuf,
	.vidioc_expbuf               = vb2_ioctl_expbuf,

1733
	.vidioc_s_std               = vidioc_s_std,
1734
	.vidioc_g_std               = vidioc_g_std,
1735 1736 1737
	.vidioc_enum_input          = vidioc_enum_input,
	.vidioc_g_input             = vidioc_g_input,
	.vidioc_s_input             = vidioc_s_input,
1738 1739 1740 1741

	.vidioc_streamon            = vb2_ioctl_streamon,
	.vidioc_streamoff           = vb2_ioctl_streamoff,

1742 1743 1744 1745 1746 1747 1748 1749
	.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
1750 1751 1752
	.vidioc_log_status	    = vidioc_log_status,
	.vidioc_subscribe_event     = v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event   = v4l2_event_unsubscribe,
1753 1754 1755 1756
};

static const struct video_device au0828_video_template = {
	.fops                       = &au0828_v4l_fops,
1757
	.release                    = video_device_release_empty,
1758
	.ioctl_ops 		    = &video_ioctl_ops,
1759
	.tvnorms                    = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1760 1761
};

1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
static int au0828_vb2_setup(struct au0828_dev *dev)
{
	int rc;
	struct vb2_queue *q;

	/* Setup Videobuf2 for Video capture */
	q = &dev->vb_vidq;
	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
	q->drv_priv = dev;
	q->buf_struct_size = sizeof(struct au0828_buffer);
	q->ops = &au0828_video_qops;
	q->mem_ops = &vb2_vmalloc_memops;

	rc = vb2_queue_init(q);
	if (rc < 0)
		return rc;

	/* Setup Videobuf2 for VBI capture */
	q = &dev->vb_vbiq;
	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
	q->drv_priv = dev;
	q->buf_struct_size = sizeof(struct au0828_buffer);
	q->ops = &au0828_vbi_qops;
	q->mem_ops = &vb2_vmalloc_memops;

	rc = vb2_queue_init(q);
	if (rc < 0)
		return rc;

	return 0;
}

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
static void au0828_analog_create_entities(struct au0828_dev *dev)
{
#if defined(CONFIG_MEDIA_CONTROLLER)
	static const char * const inames[] = {
		[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"
	};
	int ret, i;

	/* Initialize Video and VBI pads */
	dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1813
	ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1814 1815 1816 1817
	if (ret < 0)
		pr_err("failed to initialize video media entity!\n");

	dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1818
	ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
	if (ret < 0)
		pr_err("failed to initialize vbi media entity!\n");

	/* Create entities for each input connector */
	for (i = 0; i < AU0828_MAX_INPUT; i++) {
		struct media_entity *ent = &dev->input_ent[i];

		if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
			break;

		ent->name = inames[AUVI_INPUT(i).type];
		ent->flags = MEDIA_ENT_FL_CONNECTOR;
		dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;

		switch (AUVI_INPUT(i).type) {
		case AU0828_VMUX_COMPOSITE:
1835
			ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1836 1837
			break;
		case AU0828_VMUX_SVIDEO:
1838
			ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1839 1840 1841 1842
			break;
		case AU0828_VMUX_CABLE:
		case AU0828_VMUX_TELEVISION:
		case AU0828_VMUX_DVB:
1843
			ent->function = MEDIA_ENT_F_CONN_RF;
1844 1845
			break;
		default: /* AU0828_VMUX_DEBUG */
1846
			continue;
1847 1848
		}

1849
		ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
		if (ret < 0)
			pr_err("failed to initialize input pad[%d]!\n", i);

		ret = media_device_register_entity(dev->media_dev, ent);
		if (ret < 0)
			pr_err("failed to register input entity %d!\n", i);
	}
#endif
}

1860 1861
/**************************************************************************/

1862 1863
int au0828_analog_register(struct au0828_dev *dev,
			   struct usb_interface *interface)
1864 1865
{
	int retval = -ENOMEM;
1866 1867
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *endpoint;
1868
	int i, ret;
1869

1870 1871
	dprintk(1, "au0828_analog_register called for intf#%d!\n",
		interface->cur_altsetting->desc.bInterfaceNumber);
1872

1873 1874
	/* set au0828 usb interface0 to as5 */
	retval = usb_set_interface(dev->usbdev,
1875
			interface->cur_altsetting->desc.bInterfaceNumber, 5);
1876
	if (retval != 0) {
1877
		pr_info("Failure setting usb interface0 to as5\n");
1878 1879 1880 1881 1882
		return retval;
	}

	/* Figure out which endpoint has the isoc interface */
	iface_desc = interface->cur_altsetting;
1883
	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1884
		endpoint = &iface_desc->endpoint[i].desc;
1885 1886 1887 1888
		if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
		     == USB_DIR_IN) &&
		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
		     == USB_ENDPOINT_XFER_ISOC)) {
1889 1890 1891

			/* we find our isoc in endpoint */
			u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1892 1893
			dev->max_pkt_size = (tmp & 0x07ff) *
				(((tmp & 0x1800) >> 11) + 1);
1894
			dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1895 1896 1897
			dprintk(1,
				"Found isoc endpoint 0x%02x, max size = %d\n",
				dev->isoc_in_endpointaddr, dev->max_pkt_size);
1898 1899
		}
	}
1900
	if (!(dev->isoc_in_endpointaddr)) {
1901
		pr_info("Could not locate isoc endpoint\n");
1902 1903 1904
		return -ENODEV;
	}

1905 1906 1907
	init_waitqueue_head(&dev->open);
	spin_lock_init(&dev->slock);

1908
	/* init video dma queues */
1909
	INIT_LIST_HEAD(&dev->vidq.active);
1910
	INIT_LIST_HEAD(&dev->vbiq.active);
1911

J
Julia Lawall 已提交
1912 1913 1914 1915
	setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
		    (unsigned long)dev);
	setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
		    (unsigned long)dev);
1916

1917 1918 1919 1920 1921
	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;
1922 1923
	dev->vbi_width = 720;
	dev->vbi_height = 1;
1924
	dev->ctrl_ainput = 0;
1925
	dev->ctrl_freq = 960;
1926
	dev->std = V4L2_STD_NTSC_M;
1927
	au0828_s_input(dev, 0);
1928

1929 1930 1931
	mutex_init(&dev->vb_queue_lock);
	mutex_init(&dev->vb_vbi_queue_lock);

1932
	/* Fill the video capture device struct */
1933 1934 1935 1936 1937 1938
	dev->vdev = au0828_video_template;
	dev->vdev.v4l2_dev = &dev->v4l2_dev;
	dev->vdev.lock = &dev->lock;
	dev->vdev.queue = &dev->vb_vidq;
	dev->vdev.queue->lock = &dev->vb_queue_lock;
	strcpy(dev->vdev.name, "au0828a video");
1939 1940

	/* Setup the VBI device */
1941 1942 1943 1944 1945 1946
	dev->vbi_dev = au0828_video_template;
	dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
	dev->vbi_dev.lock = &dev->lock;
	dev->vbi_dev.queue = &dev->vb_vbiq;
	dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
	strcpy(dev->vbi_dev.name, "au0828a vbi");
1947

1948 1949
	/* Init entities at the Media Controller */
	au0828_analog_create_entities(dev);
1950

1951 1952 1953 1954 1955
	/* initialize videobuf2 stuff */
	retval = au0828_vb2_setup(dev);
	if (retval != 0) {
		dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
			retval);
1956
		return -ENODEV;
1957 1958
	}

1959
	/* Register the v4l2 device */
1960 1961
	video_set_drvdata(&dev->vdev, dev);
	retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1962 1963 1964
	if (retval != 0) {
		dprintk(1, "unable to register video device (error = %d).\n",
			retval);
1965
		ret = -ENODEV;
1966
		goto err_reg_vdev;
1967 1968 1969
	}

	/* Register the vbi device */
1970 1971
	video_set_drvdata(&dev->vbi_dev, dev);
	retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
1972 1973 1974
	if (retval != 0) {
		dprintk(1, "unable to register vbi device (error = %d).\n",
			retval);
1975
		ret = -ENODEV;
1976
		goto err_reg_vbi_dev;
1977 1978
	}

1979
	dprintk(1, "%s completed!\n", __func__);
1980 1981

	return 0;
1982

1983
err_reg_vbi_dev:
1984
	video_unregister_device(&dev->vdev);
1985 1986 1987
err_reg_vdev:
	vb2_queue_release(&dev->vb_vidq);
	vb2_queue_release(&dev->vb_vbiq);
1988
	return ret;
1989 1990
}