File Index
Files and directories
Sign in
FILE

au0828-video.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/usb/au0828/au0828-video.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Auvitek AU0828 USB Bridge (Analog video support)
   4 *
   5 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
   6 * Copyright (C) 2005-2008 Auvitek International, Ltd.
   7 */
   8
   9/* Developer Notes:
  10 *
  11 * The hardware scaler supported is unimplemented
  12 * AC97 audio support is unimplemented (only i2s audio mode)
  13 *
  14 */
  15
  16#include "au0828.h"
  17#include "au8522.h"
  18
  19#include <linux/module.h>
  20#include <linux/slab.h>
  21#include <linux/init.h>
  22#include <linux/device.h>
  23#include <media/v4l2-common.h>
  24#include <media/v4l2-mc.h>
  25#include <media/v4l2-ioctl.h>
  26#include <media/v4l2-event.h>
  27#include <media/tuner.h>
  28#include "au0828-reg.h"
  29
  30static DEFINE_MUTEX(au0828_sysfs_lock);
  31
  32/* ------------------------------------------------------------------
  33	Videobuf operations
  34   ------------------------------------------------------------------*/
  35
  36static unsigned int isoc_debug;
  37module_param(isoc_debug, int, 0644);
  38MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  39
  40#define au0828_isocdbg(fmt, arg...) \
  41do {\
  42	if (isoc_debug) { \
  43		pr_info("au0828 %s :"fmt, \
  44		       __func__ , ##arg);	   \
  45	} \
  46  } while (0)
  47
  48static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
  49{
  50	if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
  51		dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
  52}
  53
  54static inline void print_err_status(struct au0828_dev *dev,
  55				    int packet, int status)
  56{
  57	char *errmsg = "Unknown";
  58
  59	switch (status) {
  60	case -ENOENT:
  61		errmsg = "unlinked synchronously";
  62		break;
  63	case -ECONNRESET:
  64		errmsg = "unlinked asynchronously";
  65		break;
  66	case -ENOSR:
  67		errmsg = "Buffer error (overrun)";
  68		break;
  69	case -EPIPE:
  70		errmsg = "Stalled (device not responding)";
  71		break;
  72	case -EOVERFLOW:
  73		errmsg = "Babble (bad cable?)";
  74		break;
  75	case -EPROTO:
  76		errmsg = "Bit-stuff error (bad cable?)";
  77		break;
  78	case -EILSEQ:
  79		errmsg = "CRC/Timeout (could be anything)";
  80		break;
  81	case -ETIME:
  82		errmsg = "Device does not respond";
  83		break;
  84	}
  85	if (packet < 0) {
  86		au0828_isocdbg("URB status %d [%s].\n",	status, errmsg);
  87	} else {
  88		au0828_isocdbg("URB packet %d, status %d [%s].\n",
  89			       packet, status, errmsg);
  90	}
  91}
  92
  93static int check_dev(struct au0828_dev *dev)
  94{
  95	if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) {
  96		pr_info("v4l2 ioctl: device not present\n");
  97		return -ENODEV;
  98	}
  99
 100	if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) {
 101		pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n");
 102		return -EIO;
 103	}
 104	return 0;
 105}
 106
 107/*
 108 * IRQ callback, called by URB callback
 109 */
 110static void au0828_irq_callback(struct urb *urb)
 111{
 112	struct au0828_dmaqueue  *dma_q = urb->context;
 113	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
 114	unsigned long flags = 0;
 115	int i;
 116
 117	switch (urb->status) {
 118	case 0:             /* success */
 119	case -ETIMEDOUT:    /* NAK */
 120		break;
 121	case -ECONNRESET:   /* kill */
 122	case -ENOENT:
 123	case -ESHUTDOWN:
 124		au0828_isocdbg("au0828_irq_callback called: status kill\n");
 125		return;
 126	default:            /* unknown error */
 127		au0828_isocdbg("urb completion error %d.\n", urb->status);
 128		break;
 129	}
 130
 131	/* Copy data from URB */
 132	spin_lock_irqsave(&dev->slock, flags);
 133	dev->isoc_ctl.isoc_copy(dev, urb);
 134	spin_unlock_irqrestore(&dev->slock, flags);
 135
 136	/* Reset urb buffers */
 137	for (i = 0; i < urb->number_of_packets; i++) {
 138		urb->iso_frame_desc[i].status = 0;
 139		urb->iso_frame_desc[i].actual_length = 0;
 140	}
 141	urb->status = 0;
 142
 143	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
 144	if (urb->status) {
 145		au0828_isocdbg("urb resubmit failed (error=%i)\n",
 146			       urb->status);
 147	}
 148	dev->stream_state = STREAM_ON;
 149}
 150
 151/*
 152 * Stop and Deallocate URBs
 153 */
 154static void au0828_uninit_isoc(struct au0828_dev *dev)
 155{
 156	struct urb *urb;
 157	int i;
 158
 159	au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
 160
 161	dev->isoc_ctl.nfields = -1;
 162	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
 163		urb = dev->isoc_ctl.urb[i];
 164		if (urb) {
 165			if (!irqs_disabled())
 166				usb_kill_urb(urb);
 167			else
 168				usb_unlink_urb(urb);
 169
 170			if (dev->isoc_ctl.transfer_buffer[i]) {
 171				usb_free_coherent(dev->usbdev,
 172					urb->transfer_buffer_length,
 173					dev->isoc_ctl.transfer_buffer[i],
 174					urb->transfer_dma);
 175			}
 176			usb_free_urb(urb);
 177			dev->isoc_ctl.urb[i] = NULL;
 178		}
 179		dev->isoc_ctl.transfer_buffer[i] = NULL;
 180	}
 181
 182	kfree(dev->isoc_ctl.urb);
 183	kfree(dev->isoc_ctl.transfer_buffer);
 184
 185	dev->isoc_ctl.urb = NULL;
 186	dev->isoc_ctl.transfer_buffer = NULL;
 187	dev->isoc_ctl.num_bufs = 0;
 188
 189	dev->stream_state = STREAM_OFF;
 190}
 191
 192/*
 193 * Allocate URBs and start IRQ
 194 */
 195static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
 196			    int num_bufs, int max_pkt_size,
 197			    int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
 198{
 199	struct au0828_dmaqueue *dma_q = &dev->vidq;
 200	int i;
 201	int sb_size, pipe;
 202	struct urb *urb;
 203	int j, k;
 204	int rc;
 205
 206	au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
 207
 208	dev->isoc_ctl.isoc_copy = isoc_copy;
 209	dev->isoc_ctl.num_bufs = num_bufs;
 210
 211	dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(void *),  GFP_KERNEL);
 212	if (!dev->isoc_ctl.urb) {
 213		au0828_isocdbg("cannot alloc memory for usb buffers\n");
 214		return -ENOMEM;
 215	}
 216
 217	dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs, sizeof(void *),
 218						GFP_KERNEL);
 219	if (!dev->isoc_ctl.transfer_buffer) {
 220		au0828_isocdbg("cannot allocate memory for usb transfer\n");
 221		kfree(dev->isoc_ctl.urb);
 222		return -ENOMEM;
 223	}
 224
 225	dev->isoc_ctl.max_pkt_size = max_pkt_size;
 226	dev->isoc_ctl.buf = NULL;
 227
 228	sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
 229
 230	/* allocate urbs and transfer buffers */
 231	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
 232		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
 233		if (!urb) {
 234			au0828_isocdbg("cannot allocate URB\n");
 235			au0828_uninit_isoc(dev);
 236			return -ENOMEM;
 237		}
 238		dev->isoc_ctl.urb[i] = urb;
 239
 240		dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
 241			sb_size, GFP_KERNEL, &urb->transfer_dma);
 242		if (!dev->isoc_ctl.transfer_buffer[i]) {
 243			au0828_isocdbg("cannot allocate transfer buffer\n");
 244			au0828_uninit_isoc(dev);
 245			return -ENOMEM;
 246		}
 247		memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
 248
 249		pipe = usb_rcvisocpipe(dev->usbdev,
 250				       dev->isoc_in_endpointaddr);
 251
 252		usb_fill_int_urb(urb, dev->usbdev, pipe,
 253				 dev->isoc_ctl.transfer_buffer[i], sb_size,
 254				 au0828_irq_callback, dma_q, 1);
 255
 256		urb->number_of_packets = max_packets;
 257		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
 258
 259		k = 0;
 260		for (j = 0; j < max_packets; j++) {
 261			urb->iso_frame_desc[j].offset = k;
 262			urb->iso_frame_desc[j].length =
 263						dev->isoc_ctl.max_pkt_size;
 264			k += dev->isoc_ctl.max_pkt_size;
 265		}
 266	}
 267
 268	/* submit urbs and enables IRQ */
 269	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
 270		rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
 271		if (rc) {
 272			au0828_isocdbg("submit of urb %i failed (error=%i)\n",
 273				       i, rc);
 274			au0828_uninit_isoc(dev);
 275			return rc;
 276		}
 277	}
 278
 279	return 0;
 280}
 281
 282/*
 283 * Announces that a buffer were filled and request the next
 284 */
 285static inline void buffer_filled(struct au0828_dev *dev,
 286				 struct au0828_dmaqueue *dma_q,
 287				 struct au0828_buffer *buf)
 288{
 289	struct vb2_v4l2_buffer *vb = &buf->vb;
 290	struct vb2_queue *q = vb->vb2_buf.vb2_queue;
 291
 292	/* Advice that buffer was filled */
 293	au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
 294
 295	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
 296		vb->sequence = dev->frame_count++;
 297	else
 298		vb->sequence = dev->vbi_frame_count++;
 299
 300	vb->field = V4L2_FIELD_INTERLACED;
 301	vb->vb2_buf.timestamp = ktime_get_ns();
 302	vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
 303}
 304
 305/*
 306 * Identify the buffer header type and properly handles
 307 */
 308static void au0828_copy_video(struct au0828_dev *dev,
 309			      struct au0828_dmaqueue  *dma_q,
 310			      struct au0828_buffer *buf,
 311			      unsigned char *p,
 312			      unsigned char *outp, unsigned long len)
 313{
 314	void *fieldstart, *startwrite, *startread;
 315	int  linesdone, currlinedone, offset, lencopy, remain;
 316	int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
 317
 318	if (len == 0)
 319		return;
 320
 321	if (dma_q->pos + len > buf->length)
 322		len = buf->length - dma_q->pos;
 323
 324	startread = p;
 325	remain = len;
 326
 327	/* Interlaces frame */
 328	if (buf->top_field)
 329		fieldstart = outp;
 330	else
 331		fieldstart = outp + bytesperline;
 332
 333	linesdone = dma_q->pos / bytesperline;
 334	currlinedone = dma_q->pos % bytesperline;
 335	offset = linesdone * bytesperline * 2 + currlinedone;
 336	startwrite = fieldstart + offset;
 337	lencopy = bytesperline - currlinedone;
 338	lencopy = lencopy > remain ? remain : lencopy;
 339
 340	if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
 341		au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
 342			       ((char *)startwrite + lencopy) -
 343			       ((char *)outp + buf->length));
 344		remain = (char *)outp + buf->length - (char *)startwrite;
 345		lencopy = remain;
 346	}
 347	if (lencopy <= 0)
 348		return;
 349	memcpy(startwrite, startread, lencopy);
 350
 351	remain -= lencopy;
 352
 353	while (remain > 0) {
 354		startwrite += lencopy + bytesperline;
 355		startread += lencopy;
 356		if (bytesperline > remain)
 357			lencopy = remain;
 358		else
 359			lencopy = bytesperline;
 360
 361		if ((char *)startwrite + lencopy > (char *)outp +
 362		    buf->length) {
 363			au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
 364				       ((char *)startwrite + lencopy) -
 365				       ((char *)outp + buf->length));
 366			lencopy = remain = (char *)outp + buf->length -
 367					   (char *)startwrite;
 368		}
 369		if (lencopy <= 0)
 370			break;
 371
 372		memcpy(startwrite, startread, lencopy);
 373
 374		remain -= lencopy;
 375	}
 376
 377	if (offset > 1440) {
 378		/* We have enough data to check for greenscreen */
 379		if (outp[0] < 0x60 && outp[1440] < 0x60)
 380			dev->greenscreen_detected = 1;
 381	}
 382
 383	dma_q->pos += len;
 384}
 385
 386/*
 387 * generic routine to get the next available buffer
 388 */
 389static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
 390				struct au0828_buffer **buf)
 391{
 392	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
 393
 394	if (list_empty(&dma_q->active)) {
 395		au0828_isocdbg("No active queue to serve\n");
 396		dev->isoc_ctl.buf = NULL;
 397		*buf = NULL;
 398		return;
 399	}
 400
 401	/* Get the next buffer */
 402	*buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
 403	/* Cleans up buffer - Useful for testing for frame/URB loss */
 404	list_del(&(*buf)->list);
 405	dma_q->pos = 0;
 406	(*buf)->vb_buf = (*buf)->mem;
 407	dev->isoc_ctl.buf = *buf;
 408
 409	return;
 410}
 411
 412static void au0828_copy_vbi(struct au0828_dev *dev,
 413			      struct au0828_dmaqueue  *dma_q,
 414			      struct au0828_buffer *buf,
 415			      unsigned char *p,
 416			      unsigned char *outp, unsigned long len)
 417{
 418	unsigned char *startwrite, *startread;
 419	int bytesperline;
 420	int i, j = 0;
 421
 422	if (dev == NULL) {
 423		au0828_isocdbg("dev is null\n");
 424		return;
 425	}
 426
 427	if (dma_q == NULL) {
 428		au0828_isocdbg("dma_q is null\n");
 429		return;
 430	}
 431	if (buf == NULL)
 432		return;
 433	if (p == NULL) {
 434		au0828_isocdbg("p is null\n");
 435		return;
 436	}
 437	if (outp == NULL) {
 438		au0828_isocdbg("outp is null\n");
 439		return;
 440	}
 441
 442	bytesperline = dev->vbi_width;
 443
 444	if (dma_q->pos + len > buf->length)
 445		len = buf->length - dma_q->pos;
 446
 447	startread = p;
 448	startwrite = outp + (dma_q->pos / 2);
 449
 450	/* Make sure the bottom field populates the second half of the frame */
 451	if (buf->top_field == 0)
 452		startwrite += bytesperline * dev->vbi_height;
 453
 454	for (i = 0; i < len; i += 2)
 455		startwrite[j++] = startread[i+1];
 456
 457	dma_q->pos += len;
 458}
 459
 460
 461/*
 462 * generic routine to get the next available VBI buffer
 463 */
 464static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
 465				    struct au0828_buffer **buf)
 466{
 467	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
 468
 469	if (list_empty(&dma_q->active)) {
 470		au0828_isocdbg("No active queue to serve\n");
 471		dev->isoc_ctl.vbi_buf = NULL;
 472		*buf = NULL;
 473		return;
 474	}
 475
 476	/* Get the next buffer */
 477	*buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
 478	/* Cleans up buffer - Useful for testing for frame/URB loss */
 479	list_del(&(*buf)->list);
 480	dma_q->pos = 0;
 481	(*buf)->vb_buf = (*buf)->mem;
 482	dev->isoc_ctl.vbi_buf = *buf;
 483	return;
 484}
 485
 486/*
 487 * Controls the isoc copy of each urb packet
 488 */
 489static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
 490{
 491	struct au0828_buffer    *buf;
 492	struct au0828_buffer    *vbi_buf;
 493	struct au0828_dmaqueue  *dma_q = urb->context;
 494	struct au0828_dmaqueue  *vbi_dma_q = &dev->vbiq;
 495	unsigned char *outp = NULL;
 496	unsigned char *vbioutp = NULL;
 497	int i, len = 0, rc = 1;
 498	unsigned char *p;
 499	unsigned char fbyte;
 500	unsigned int vbi_field_size;
 501	unsigned int remain, lencopy;
 502
 503	if (!dev)
 504		return 0;
 505
 506	if (test_bit(DEV_DISCONNECTED, &dev->dev_state) ||
 507	    test_bit(DEV_MISCONFIGURED, &dev->dev_state))
 508		return 0;
 509
 510	if (urb->status < 0) {
 511		print_err_status(dev, -1, urb->status);
 512		if (urb->status == -ENOENT)
 513			return 0;
 514	}
 515
 516	buf = dev->isoc_ctl.buf;
 517	if (buf != NULL)
 518		outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
 519
 520	vbi_buf = dev->isoc_ctl.vbi_buf;
 521	if (vbi_buf != NULL)
 522		vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
 523
 524	for (i = 0; i < urb->number_of_packets; i++) {
 525		int status = urb->iso_frame_desc[i].status;
 526
 527		if (status < 0) {
 528			print_err_status(dev, i, status);
 529			if (urb->iso_frame_desc[i].status != -EPROTO)
 530				continue;
 531		}
 532
 533		if (urb->iso_frame_desc[i].actual_length <= 0)
 534			continue;
 535
 536		if (urb->iso_frame_desc[i].actual_length >
 537						dev->max_pkt_size) {
 538			au0828_isocdbg("packet bigger than packet size");
 539			continue;
 540		}
 541
 542		p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
 543		fbyte = p[0];
 544		len = urb->iso_frame_desc[i].actual_length - 4;
 545		p += 4;
 546
 547		if (fbyte & 0x80) {
 548			len -= 4;
 549			p += 4;
 550			au0828_isocdbg("Video frame %s\n",
 551				       (fbyte & 0x40) ? "odd" : "even");
 552			if (fbyte & 0x40) {
 553				/* VBI */
 554				if (vbi_buf != NULL)
 555					buffer_filled(dev, vbi_dma_q, vbi_buf);
 556				vbi_get_next_buf(vbi_dma_q, &vbi_buf);
 557				if (vbi_buf == NULL)
 558					vbioutp = NULL;
 559				else
 560					vbioutp = vb2_plane_vaddr(
 561						&vbi_buf->vb.vb2_buf, 0);
 562
 563				/* Video */
 564				if (buf != NULL)
 565					buffer_filled(dev, dma_q, buf);
 566				get_next_buf(dma_q, &buf);
 567				if (buf == NULL)
 568					outp = NULL;
 569				else
 570					outp = vb2_plane_vaddr(
 571						&buf->vb.vb2_buf, 0);
 572
 573				/* As long as isoc traffic is arriving, keep
 574				   resetting the timer */
 575				if (dev->vid_timeout_running)
 576					mod_timer(&dev->vid_timeout,
 577						  jiffies + (HZ / 10));
 578				if (dev->vbi_timeout_running)
 579					mod_timer(&dev->vbi_timeout,
 580						  jiffies + (HZ / 10));
 581			}
 582
 583			if (buf != NULL) {
 584				if (fbyte & 0x40)
 585					buf->top_field = 1;
 586				else
 587					buf->top_field = 0;
 588			}
 589
 590			if (vbi_buf != NULL) {
 591				if (fbyte & 0x40)
 592					vbi_buf->top_field = 1;
 593				else
 594					vbi_buf->top_field = 0;
 595			}
 596
 597			dev->vbi_read = 0;
 598			vbi_dma_q->pos = 0;
 599			dma_q->pos = 0;
 600		}
 601
 602		vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
 603		if (dev->vbi_read < vbi_field_size) {
 604			remain  = vbi_field_size - dev->vbi_read;
 605			lencopy = umin(len, remain);
 606
 607			if (vbi_buf != NULL)
 608				au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
 609						vbioutp, len);
 610
 611			len -= lencopy;
 612			p += lencopy;
 613			dev->vbi_read += lencopy;
 614		}
 615
 616		if (dev->vbi_read >= vbi_field_size && buf != NULL)
 617			au0828_copy_video(dev, dma_q, buf, p, outp, len);
 618	}
 619	return rc;
 620}
 621
 622void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
 623{
 624#ifdef CONFIG_MEDIA_CONTROLLER
 625	int i;
 626
 627	for (i = 0; i < AU0828_MAX_INPUT; i++) {
 628		if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
 629			return;
 630		media_device_unregister_entity(&dev->input_ent[i]);
 631	}
 632#endif
 633}
 634
 635static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
 636{
 637	struct au0828_dev *dev =
 638		container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
 639
 640	v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
 641	v4l2_device_unregister(&dev->v4l2_dev);
 642	au0828_usb_v4l2_media_release(dev);
 643	au0828_usb_release(dev);
 644}
 645
 646int au0828_v4l2_device_register(struct usb_interface *interface,
 647				struct au0828_dev *dev)
 648{
 649	int retval;
 650
 651	if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
 652		return 0;
 653
 654	/* Create the v4l2_device */
 655#ifdef CONFIG_MEDIA_CONTROLLER
 656	dev->v4l2_dev.mdev = dev->media_dev;
 657#endif
 658	retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
 659	if (retval) {
 660		pr_err("%s() v4l2_device_register failed\n",
 661		       __func__);
 662		return retval;
 663	}
 664
 665	dev->v4l2_dev.release = au0828_usb_v4l2_release;
 666
 667	/* This control handler will inherit the controls from au8522 */
 668	retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
 669	if (retval) {
 670		pr_err("%s() v4l2_ctrl_handler_init failed\n",
 671		       __func__);
 672		return retval;
 673	}
 674	dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
 675
 676	return 0;
 677}
 678
 679static int queue_setup(struct vb2_queue *vq,
 680		       unsigned int *nbuffers, unsigned int *nplanes,
 681		       unsigned int sizes[], struct device *alloc_devs[])
 682{
 683	struct au0828_dev *dev = vb2_get_drv_priv(vq);
 684	unsigned long size = dev->height * dev->bytesperline;
 685
 686	if (*nplanes)
 687		return sizes[0] < size ? -EINVAL : 0;
 688	*nplanes = 1;
 689	sizes[0] = size;
 690	return 0;
 691}
 692
 693static int
 694buffer_prepare(struct vb2_buffer *vb)
 695{
 696	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 697	struct au0828_buffer *buf = container_of(vbuf,
 698				struct au0828_buffer, vb);
 699	struct au0828_dev    *dev = vb2_get_drv_priv(vb->vb2_queue);
 700
 701	buf->length = dev->height * dev->bytesperline;
 702
 703	if (vb2_plane_size(vb, 0) < buf->length) {
 704		pr_err("%s data will not fit into plane (%lu < %lu)\n",
 705			__func__, vb2_plane_size(vb, 0), buf->length);
 706		return -EINVAL;
 707	}
 708	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
 709	return 0;
 710}
 711
 712static void
 713buffer_queue(struct vb2_buffer *vb)
 714{
 715	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 716	struct au0828_buffer    *buf     = container_of(vbuf,
 717							struct au0828_buffer,
 718							vb);
 719	struct au0828_dev       *dev     = vb2_get_drv_priv(vb->vb2_queue);
 720	struct au0828_dmaqueue  *vidq    = &dev->vidq;
 721	unsigned long flags = 0;
 722
 723	buf->mem = vb2_plane_vaddr(vb, 0);
 724	buf->length = vb2_plane_size(vb, 0);
 725
 726	spin_lock_irqsave(&dev->slock, flags);
 727	list_add_tail(&buf->list, &vidq->active);
 728	spin_unlock_irqrestore(&dev->slock, flags);
 729}
 730
 731static int au0828_i2s_init(struct au0828_dev *dev)
 732{
 733	/* Enable i2s mode */
 734	au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
 735	return 0;
 736}
 737
 738/*
 739 * Auvitek au0828 analog stream enable
 740 */
 741static int au0828_analog_stream_enable(struct au0828_dev *d)
 742{
 743	struct usb_interface *iface;
 744	int ret, h, w;
 745
 746	dprintk(1, "au0828_analog_stream_enable called\n");
 747
 748	if (test_bit(DEV_DISCONNECTED, &d->dev_state))
 749		return -ENODEV;
 750
 751	iface = usb_ifnum_to_if(d->usbdev, 0);
 752	if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
 753		dprintk(1, "Changing intf#0 to alt 5\n");
 754		/* set au0828 interface0 to AS5 here again */
 755		ret = usb_set_interface(d->usbdev, 0, 5);
 756		if (ret < 0) {
 757			pr_info("Au0828 can't set alt setting to 5!\n");
 758			return -EBUSY;
 759		}
 760	}
 761
 762	h = d->height / 2 + 2;
 763	w = d->width * 2;
 764
 765	au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
 766	au0828_writereg(d, 0x106, 0x00);
 767	/* set x position */
 768	au0828_writereg(d, 0x110, 0x00);
 769	au0828_writereg(d, 0x111, 0x00);
 770	au0828_writereg(d, 0x114, w & 0xff);
 771	au0828_writereg(d, 0x115, w >> 8);
 772	/* set y position */
 773	au0828_writereg(d, 0x112, 0x00);
 774	au0828_writereg(d, 0x113, 0x00);
 775	au0828_writereg(d, 0x116, h & 0xff);
 776	au0828_writereg(d, 0x117, h >> 8);
 777	au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
 778
 779	return 0;
 780}
 781
 782static int au0828_analog_stream_disable(struct au0828_dev *d)
 783{
 784	dprintk(1, "au0828_analog_stream_disable called\n");
 785	au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
 786	return 0;
 787}
 788
 789static void au0828_analog_stream_reset(struct au0828_dev *dev)
 790{
 791	dprintk(1, "au0828_analog_stream_reset called\n");
 792	au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
 793	mdelay(30);
 794	au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
 795}
 796
 797/*
 798 * Some operations needs to stop current streaming
 799 */
 800static int au0828_stream_interrupt(struct au0828_dev *dev)
 801{
 802	dev->stream_state = STREAM_INTERRUPT;
 803	if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
 804		return -ENODEV;
 805	return 0;
 806}
 807
 808int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
 809{
 810	struct au0828_dev *dev = vb2_get_drv_priv(vq);
 811	int rc = 0;
 812
 813	dprintk(1, "au0828_start_analog_streaming called %d\n",
 814		dev->streaming_users);
 815
 816	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
 817		dev->frame_count = 0;
 818	else
 819		dev->vbi_frame_count = 0;
 820
 821	if (dev->streaming_users == 0) {
 822		/* If we were doing ac97 instead of i2s, it would go here...*/
 823		au0828_i2s_init(dev);
 824		rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
 825				   AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
 826				   au0828_isoc_copy);
 827		if (rc < 0) {
 828			pr_info("au0828_init_isoc failed\n");
 829			return rc;
 830		}
 831
 832		v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
 833
 834		if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
 835			dev->vid_timeout_running = 1;
 836			mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
 837		} else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
 838			dev->vbi_timeout_running = 1;
 839			mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
 840		}
 841	}
 842	dev->streaming_users++;
 843	return rc;
 844}
 845
 846static void au0828_stop_streaming(struct vb2_queue *vq)
 847{
 848	struct au0828_dev *dev = vb2_get_drv_priv(vq);
 849	struct au0828_dmaqueue *vidq = &dev->vidq;
 850	unsigned long flags = 0;
 851
 852	dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
 853
 854	if (dev->streaming_users-- == 1) {
 855		au0828_uninit_isoc(dev);
 856		v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
 857	}
 858
 859	dev->vid_timeout_running = 0;
 860	timer_delete_sync(&dev->vid_timeout);
 861
 862	spin_lock_irqsave(&dev->slock, flags);
 863	if (dev->isoc_ctl.buf != NULL) {
 864		vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
 865				VB2_BUF_STATE_ERROR);
 866		dev->isoc_ctl.buf = NULL;
 867	}
 868	while (!list_empty(&vidq->active)) {
 869		struct au0828_buffer *buf;
 870
 871		buf = list_entry(vidq->active.next, struct au0828_buffer, list);
 872		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 873		list_del(&buf->list);
 874	}
 875	spin_unlock_irqrestore(&dev->slock, flags);
 876}
 877
 878void au0828_stop_vbi_streaming(struct vb2_queue *vq)
 879{
 880	struct au0828_dev *dev = vb2_get_drv_priv(vq);
 881	struct au0828_dmaqueue *vbiq = &dev->vbiq;
 882	unsigned long flags = 0;
 883
 884	dprintk(1, "au0828_stop_vbi_streaming called %d\n",
 885		dev->streaming_users);
 886
 887	if (dev->streaming_users-- == 1) {
 888		au0828_uninit_isoc(dev);
 889		v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
 890	}
 891
 892	spin_lock_irqsave(&dev->slock, flags);
 893	if (dev->isoc_ctl.vbi_buf != NULL) {
 894		vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
 895				VB2_BUF_STATE_ERROR);
 896		dev->isoc_ctl.vbi_buf = NULL;
 897	}
 898	while (!list_empty(&vbiq->active)) {
 899		struct au0828_buffer *buf;
 900
 901		buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
 902		list_del(&buf->list);
 903		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 904	}
 905	spin_unlock_irqrestore(&dev->slock, flags);
 906
 907	dev->vbi_timeout_running = 0;
 908	timer_delete_sync(&dev->vbi_timeout);
 909}
 910
 911static const struct vb2_ops au0828_video_qops = {
 912	.queue_setup     = queue_setup,
 913	.buf_prepare     = buffer_prepare,
 914	.buf_queue       = buffer_queue,
 915	.prepare_streaming = v4l_vb2q_enable_media_source,
 916	.start_streaming = au0828_start_analog_streaming,
 917	.stop_streaming  = au0828_stop_streaming,
 918};
 919
 920/* ------------------------------------------------------------------
 921   V4L2 interface
 922   ------------------------------------------------------------------*/
 923/*
 924 * au0828_analog_unregister
 925 * unregister v4l2 devices
 926 */
 927int au0828_analog_unregister(struct au0828_dev *dev)
 928{
 929	dprintk(1, "au0828_analog_unregister called\n");
 930
 931	/* No analog TV */
 932	if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
 933		return 0;
 934
 935	mutex_lock(&au0828_sysfs_lock);
 936	vb2_video_unregister_device(&dev->vdev);
 937	vb2_video_unregister_device(&dev->vbi_dev);
 938	mutex_unlock(&au0828_sysfs_lock);
 939
 940	v4l2_device_disconnect(&dev->v4l2_dev);
 941	v4l2_device_put(&dev->v4l2_dev);
 942
 943	return 1;
 944}
 945
 946/* This function ensures that video frames continue to be delivered even if
 947   the ITU-656 input isn't receiving any data (thereby preventing applications
 948   such as tvtime from hanging) */
 949static void au0828_vid_buffer_timeout(struct timer_list *t)
 950{
 951	struct au0828_dev *dev = timer_container_of(dev, t, vid_timeout);
 952	struct au0828_dmaqueue *dma_q = &dev->vidq;
 953	struct au0828_buffer *buf;
 954	unsigned char *vid_data;
 955	unsigned long flags = 0;
 956
 957	spin_lock_irqsave(&dev->slock, flags);
 958
 959	buf = dev->isoc_ctl.buf;
 960	if (buf != NULL) {
 961		vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
 962		memset(vid_data, 0x00, buf->length); /* Blank green frame */
 963		buffer_filled(dev, dma_q, buf);
 964	}
 965	get_next_buf(dma_q, &buf);
 966
 967	if (dev->vid_timeout_running == 1)
 968		mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
 969
 970	spin_unlock_irqrestore(&dev->slock, flags);
 971}
 972
 973static void au0828_vbi_buffer_timeout(struct timer_list *t)
 974{
 975	struct au0828_dev *dev = timer_container_of(dev, t, vbi_timeout);
 976	struct au0828_dmaqueue *dma_q = &dev->vbiq;
 977	struct au0828_buffer *buf;
 978	unsigned char *vbi_data;
 979	unsigned long flags = 0;
 980
 981	spin_lock_irqsave(&dev->slock, flags);
 982
 983	buf = dev->isoc_ctl.vbi_buf;
 984	if (buf != NULL) {
 985		vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
 986		memset(vbi_data, 0x00, buf->length);
 987		buffer_filled(dev, dma_q, buf);
 988	}
 989	vbi_get_next_buf(dma_q, &buf);
 990
 991	if (dev->vbi_timeout_running == 1)
 992		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
 993	spin_unlock_irqrestore(&dev->slock, flags);
 994}
 995
 996static int au0828_v4l2_open(struct file *filp)
 997{
 998	struct au0828_dev *dev = video_drvdata(filp);
 999	int ret;
1000
1001	dprintk(1,
1002		"%s called std_set %d dev_state %ld stream users %d users %d\n",
1003		__func__, dev->std_set_in_tuner_core, dev->dev_state,
1004		dev->streaming_users, dev->users);
1005
1006	if (mutex_lock_interruptible(&dev->lock))
1007		return -ERESTARTSYS;
1008
1009	ret = v4l2_fh_open(filp);
1010	if (ret) {
1011		au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1012				__func__, ret);
1013		mutex_unlock(&dev->lock);
1014		return ret;
1015	}
1016
1017	if (dev->users == 0) {
1018		au0828_analog_stream_enable(dev);
1019		au0828_analog_stream_reset(dev);
1020		dev->stream_state = STREAM_OFF;
1021		set_bit(DEV_INITIALIZED, &dev->dev_state);
1022	}
1023	dev->users++;
1024	mutex_unlock(&dev->lock);
1025	return ret;
1026}
1027
1028static int au0828_v4l2_close(struct file *filp)
1029{
1030	int ret;
1031	struct au0828_dev *dev = video_drvdata(filp);
1032	struct video_device *vdev = video_devdata(filp);
1033
1034	dprintk(1,
1035		"%s called std_set %d dev_state %ld stream users %d users %d\n",
1036		__func__, dev->std_set_in_tuner_core, dev->dev_state,
1037		dev->streaming_users, dev->users);
1038
1039	mutex_lock(&dev->lock);
1040	if (vdev->vfl_type == VFL_TYPE_VIDEO && dev->vid_timeout_running) {
1041		/* Cancel timeout thread in case they didn't call streamoff */
1042		dev->vid_timeout_running = 0;
1043		timer_delete_sync(&dev->vid_timeout);
1044	} else if (vdev->vfl_type == VFL_TYPE_VBI &&
1045			dev->vbi_timeout_running) {
1046		/* Cancel timeout thread in case they didn't call streamoff */
1047		dev->vbi_timeout_running = 0;
1048		timer_delete_sync(&dev->vbi_timeout);
1049	}
1050
1051	if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
1052		goto end;
1053
1054	if (dev->users == 1) {
1055		/*
1056		 * Avoid putting tuner in sleep if DVB or ALSA are
1057		 * streaming.
1058		 *
1059		 * On most USB devices  like au0828 the tuner can
1060		 * be safely put in sleep state here if ALSA isn't
1061		 * streaming. Exceptions are some very old USB tuner
1062		 * models such as em28xx-based WinTV USB2 which have
1063		 * a separate audio output jack. The devices that have
1064		 * a separate audio output jack have analog tuners,
1065		 * like Philips FM1236. Those devices are always on,
1066		 * so the s_power callback are silently ignored.
1067		 * So, the current logic here does the following:
1068		 * Disable (put tuner to sleep) when
1069		 * - ALSA and DVB aren't streaming.
1070		 * - the last V4L2 file handler is closed.
1071		 *
1072		 * FIXME:
1073		 *
1074		 * Additionally, this logic could be improved to
1075		 * disable the media source if the above conditions
1076		 * are met and if the device:
1077		 * - doesn't have a separate audio out plug (or
1078		 * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1079		 *
1080		 * Once this additional logic is in place, a callback
1081		 * is needed to enable the media source and power on
1082		 * the tuner, for radio to work.
1083		*/
1084		ret = v4l_enable_media_source(vdev);
1085		if (ret == 0)
1086			v4l2_device_call_all(&dev->v4l2_dev, 0, tuner,
1087					     standby);
1088		dev->std_set_in_tuner_core = 0;
1089
1090		/* When close the device, set the usb intf0 into alt0 to free
1091		   USB bandwidth */
1092		ret = usb_set_interface(dev->usbdev, 0, 0);
1093		if (ret < 0)
1094			pr_info("Au0828 can't set alternate to 0!\n");
1095	}
1096end:
1097	_vb2_fop_release(filp, NULL);
1098	dev->users--;
1099	mutex_unlock(&dev->lock);
1100	return 0;
1101}
1102
1103/* Must be called with dev->lock held */
1104static void au0828_init_tuner(struct au0828_dev *dev)
1105{
1106	struct v4l2_frequency f = {
1107		.frequency = dev->ctrl_freq,
1108		.type = V4L2_TUNER_ANALOG_TV,
1109	};
1110
1111	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1112		dev->std_set_in_tuner_core, dev->dev_state);
1113
1114	if (dev->std_set_in_tuner_core)
1115		return;
1116	dev->std_set_in_tuner_core = 1;
1117	i2c_gate_ctrl(dev, 1);
1118	/* If we've never sent the standard in tuner core, do so now.
1119	   We don't do this at device probe because we don't want to
1120	   incur the cost of a firmware load */
1121	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1122	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1123	i2c_gate_ctrl(dev, 0);
1124}
1125
1126static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1127			     struct v4l2_format *format)
1128{
1129	int ret;
1130	int width = format->fmt.pix.width;
1131	int height = format->fmt.pix.height;
1132
1133	/* If they are demanding a format other than the one we support,
1134	   bail out (tvtime asks for UYVY and then retries with YUYV) */
1135	if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1136		return -EINVAL;
1137
1138	/* format->fmt.pix.width only support 720 and height 480 */
1139	if (width != 720)
1140		width = 720;
1141	if (height != 480)
1142		height = 480;
1143
1144	format->fmt.pix.width = width;
1145	format->fmt.pix.height = height;
1146	format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1147	format->fmt.pix.bytesperline = width * 2;
1148	format->fmt.pix.sizeimage = width * height * 2;
1149	format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1150	format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1151
1152	if (cmd == VIDIOC_TRY_FMT)
1153		return 0;
1154
1155	/* maybe set new image format, driver current only support 720*480 */
1156	dev->width = width;
1157	dev->height = height;
1158	dev->frame_size = width * height * 2;
1159	dev->field_size = width * height;
1160	dev->bytesperline = width * 2;
1161
1162	if (dev->stream_state == STREAM_ON) {
1163		dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1164		ret = au0828_stream_interrupt(dev);
1165		if (ret != 0) {
1166			dprintk(1, "error interrupting video stream!\n");
1167			return ret;
1168		}
1169	}
1170
1171	au0828_analog_stream_enable(dev);
1172
1173	return 0;
1174}
1175
1176static int vidioc_querycap(struct file *file, void  *priv,
1177			   struct v4l2_capability *cap)
1178{
1179	struct au0828_dev *dev = video_drvdata(file);
1180
1181	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1182		dev->std_set_in_tuner_core, dev->dev_state);
1183
1184	strscpy(cap->driver, "au0828", sizeof(cap->driver));
1185	strscpy(cap->card, dev->board.name, sizeof(cap->card));
1186	usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1187
1188	/* set the device capabilities */
1189	cap->capabilities =
1190		V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1191		V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1192		V4L2_CAP_DEVICE_CAPS;
1193	return 0;
1194}
1195
1196static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1197					struct v4l2_fmtdesc *f)
1198{
1199	if (f->index)
1200		return -EINVAL;
1201
1202	dprintk(1, "%s called\n", __func__);
1203
1204	f->pixelformat = V4L2_PIX_FMT_UYVY;
1205
1206	return 0;
1207}
1208
1209static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1210					struct v4l2_format *f)
1211{
1212	struct au0828_dev *dev = video_drvdata(file);
1213
1214	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1215		dev->std_set_in_tuner_core, dev->dev_state);
1216
1217	f->fmt.pix.width = dev->width;
1218	f->fmt.pix.height = dev->height;
1219	f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1220	f->fmt.pix.bytesperline = dev->bytesperline;
1221	f->fmt.pix.sizeimage = dev->frame_size;
1222	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1223	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1224	return 0;
1225}
1226
1227static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1228				  struct v4l2_format *f)
1229{
1230	struct au0828_dev *dev = video_drvdata(file);
1231
1232	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1233		dev->std_set_in_tuner_core, dev->dev_state);
1234
1235	return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1236}
1237
1238static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1239				struct v4l2_format *f)
1240{
1241	struct au0828_dev *dev = video_drvdata(file);
1242	int rc;
1243
1244	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1245		dev->std_set_in_tuner_core, dev->dev_state);
1246
1247	rc = check_dev(dev);
1248	if (rc < 0)
1249		return rc;
1250
1251	if (vb2_is_busy(&dev->vb_vidq)) {
1252		pr_info("%s queue busy\n", __func__);
1253		rc = -EBUSY;
1254		goto out;
1255	}
1256
1257	rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1258out:
1259	return rc;
1260}
1261
1262static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1263{
1264	struct au0828_dev *dev = video_drvdata(file);
1265
1266	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1267		dev->std_set_in_tuner_core, dev->dev_state);
1268
1269	if (norm == dev->std)
1270		return 0;
1271
1272	if (dev->streaming_users > 0)
1273		return -EBUSY;
1274
1275	dev->std = norm;
1276
1277	au0828_init_tuner(dev);
1278
1279	i2c_gate_ctrl(dev, 1);
1280
1281	/*
1282	 * FIXME: when we support something other than 60Hz standards,
1283	 * we are going to have to make the au0828 bridge adjust the size
1284	 * of its capture buffer, which is currently hardcoded at 720x480
1285	 */
1286
1287	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1288
1289	i2c_gate_ctrl(dev, 0);
1290
1291	return 0;
1292}
1293
1294static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1295{
1296	struct au0828_dev *dev = video_drvdata(file);
1297
1298	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1299		dev->std_set_in_tuner_core, dev->dev_state);
1300
1301	*norm = dev->std;
1302	return 0;
1303}
1304
1305static int vidioc_enum_input(struct file *file, void *priv,
1306				struct v4l2_input *input)
1307{
1308	struct au0828_dev *dev = video_drvdata(file);
1309	unsigned int tmp;
1310
1311	static const char *inames[] = {
1312		[AU0828_VMUX_UNDEFINED] = "Undefined",
1313		[AU0828_VMUX_COMPOSITE] = "Composite",
1314		[AU0828_VMUX_SVIDEO] = "S-Video",
1315		[AU0828_VMUX_CABLE] = "Cable TV",
1316		[AU0828_VMUX_TELEVISION] = "Television",
1317		[AU0828_VMUX_DVB] = "DVB",
1318	};
1319
1320	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1321		dev->std_set_in_tuner_core, dev->dev_state);
1322
1323	tmp = input->index;
1324
1325	if (tmp >= AU0828_MAX_INPUT)
1326		return -EINVAL;
1327	if (AUVI_INPUT(tmp).type == 0)
1328		return -EINVAL;
1329
1330	input->index = tmp;
1331	strscpy(input->name, inames[AUVI_INPUT(tmp).type], sizeof(input->name));
1332	if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1333	    (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1334		input->type |= V4L2_INPUT_TYPE_TUNER;
1335		input->audioset = 1;
1336	} else {
1337		input->type |= V4L2_INPUT_TYPE_CAMERA;
1338		input->audioset = 2;
1339	}
1340
1341	input->std = dev->vdev.tvnorms;
1342
1343	return 0;
1344}
1345
1346static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1347{
1348	struct au0828_dev *dev = video_drvdata(file);
1349
1350	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1351		dev->std_set_in_tuner_core, dev->dev_state);
1352
1353	*i = dev->ctrl_input;
1354	return 0;
1355}
1356
1357static void au0828_s_input(struct au0828_dev *dev, int index)
1358{
1359	int i;
1360
1361	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1362		dev->std_set_in_tuner_core, dev->dev_state);
1363
1364	switch (AUVI_INPUT(index).type) {
1365	case AU0828_VMUX_SVIDEO:
1366		dev->input_type = AU0828_VMUX_SVIDEO;
1367		dev->ctrl_ainput = 1;
1368		break;
1369	case AU0828_VMUX_COMPOSITE:
1370		dev->input_type = AU0828_VMUX_COMPOSITE;
1371		dev->ctrl_ainput = 1;
1372		break;
1373	case AU0828_VMUX_TELEVISION:
1374		dev->input_type = AU0828_VMUX_TELEVISION;
1375		dev->ctrl_ainput = 0;
1376		break;
1377	default:
1378		dprintk(1, "unknown input type set [%d]\n",
1379			AUVI_INPUT(index).type);
1380		return;
1381	}
1382
1383	dev->ctrl_input = index;
1384
1385	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1386			AUVI_INPUT(index).vmux, 0, 0);
1387
1388	for (i = 0; i < AU0828_MAX_INPUT; i++) {
1389		int enable = 0;
1390		if (AUVI_INPUT(i).audio_setup == NULL)
1391			continue;
1392
1393		if (i == index)
1394			enable = 1;
1395		else
1396			enable = 0;
1397		if (enable) {
1398			(AUVI_INPUT(i).audio_setup)(dev, enable);
1399		} else {
1400			/* Make sure we leave it turned on if some
1401			   other input is routed to this callback */
1402			if ((AUVI_INPUT(i).audio_setup) !=
1403			    ((AUVI_INPUT(index).audio_setup))) {
1404				(AUVI_INPUT(i).audio_setup)(dev, enable);
1405			}
1406		}
1407	}
1408
1409	v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1410			AUVI_INPUT(index).amux, 0, 0);
1411}
1412
1413static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1414{
1415	struct au0828_dev *dev = video_drvdata(file);
1416	struct video_device *vfd = video_devdata(file);
1417
1418	dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1419		index);
1420	if (index >= AU0828_MAX_INPUT)
1421		return -EINVAL;
1422	if (AUVI_INPUT(index).type == 0)
1423		return -EINVAL;
1424
1425	if (dev->ctrl_input == index)
1426		return 0;
1427
1428	au0828_s_input(dev, index);
1429
1430	/*
1431	 * Input has been changed. Disable the media source
1432	 * associated with the old input and enable source
1433	 * for the newly set input
1434	 */
1435	v4l_disable_media_source(vfd);
1436	return v4l_enable_media_source(vfd);
1437}
1438
1439static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1440{
1441	if (a->index > 1)
1442		return -EINVAL;
1443
1444	dprintk(1, "%s called\n", __func__);
1445
1446	if (a->index == 0)
1447		strscpy(a->name, "Television", sizeof(a->name));
1448	else
1449		strscpy(a->name, "Line in", sizeof(a->name));
1450
1451	a->capability = V4L2_AUDCAP_STEREO;
1452	return 0;
1453}
1454
1455static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1456{
1457	struct au0828_dev *dev = video_drvdata(file);
1458
1459	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1460		dev->std_set_in_tuner_core, dev->dev_state);
1461
1462	a->index = dev->ctrl_ainput;
1463	if (a->index == 0)
1464		strscpy(a->name, "Television", sizeof(a->name));
1465	else
1466		strscpy(a->name, "Line in", sizeof(a->name));
1467
1468	a->capability = V4L2_AUDCAP_STEREO;
1469	return 0;
1470}
1471
1472static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1473{
1474	struct au0828_dev *dev = video_drvdata(file);
1475
1476	if (a->index != dev->ctrl_ainput)
1477		return -EINVAL;
1478
1479	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1480		dev->std_set_in_tuner_core, dev->dev_state);
1481	return 0;
1482}
1483
1484static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1485{
1486	struct au0828_dev *dev = video_drvdata(file);
1487	struct video_device *vfd = video_devdata(file);
1488	int ret;
1489
1490	if (t->index != 0)
1491		return -EINVAL;
1492
1493	ret = v4l_enable_media_source(vfd);
1494	if (ret)
1495		return ret;
1496
1497	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1498		dev->std_set_in_tuner_core, dev->dev_state);
1499
1500	strscpy(t->name, "Auvitek tuner", sizeof(t->name));
1501
1502	au0828_init_tuner(dev);
1503	i2c_gate_ctrl(dev, 1);
1504	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1505	i2c_gate_ctrl(dev, 0);
1506	return 0;
1507}
1508
1509static int vidioc_s_tuner(struct file *file, void *priv,
1510				const struct v4l2_tuner *t)
1511{
1512	struct au0828_dev *dev = video_drvdata(file);
1513
1514	if (t->index != 0)
1515		return -EINVAL;
1516
1517	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1518		dev->std_set_in_tuner_core, dev->dev_state);
1519
1520	au0828_init_tuner(dev);
1521	i2c_gate_ctrl(dev, 1);
1522	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1523	i2c_gate_ctrl(dev, 0);
1524
1525	dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1526		t->afc);
1527
1528	return 0;
1529
1530}
1531
1532static int vidioc_g_frequency(struct file *file, void *priv,
1533				struct v4l2_frequency *freq)
1534{
1535	struct au0828_dev *dev = video_drvdata(file);
1536
1537	if (freq->tuner != 0)
1538		return -EINVAL;
1539	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1540		dev->std_set_in_tuner_core, dev->dev_state);
1541	freq->frequency = dev->ctrl_freq;
1542	return 0;
1543}
1544
1545static int vidioc_s_frequency(struct file *file, void *priv,
1546				const struct v4l2_frequency *freq)
1547{
1548	struct au0828_dev *dev = video_drvdata(file);
1549	struct v4l2_frequency new_freq = *freq;
1550
1551	if (freq->tuner != 0)
1552		return -EINVAL;
1553
1554	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1555		dev->std_set_in_tuner_core, dev->dev_state);
1556
1557	au0828_init_tuner(dev);
1558	i2c_gate_ctrl(dev, 1);
1559
1560	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1561	/* Get the actual set (and possibly clamped) frequency */
1562	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1563	dev->ctrl_freq = new_freq.frequency;
1564
1565	i2c_gate_ctrl(dev, 0);
1566
1567	au0828_analog_stream_reset(dev);
1568
1569	return 0;
1570}
1571
1572
1573/* RAW VBI ioctls */
1574
1575static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1576				struct v4l2_format *format)
1577{
1578	struct au0828_dev *dev = video_drvdata(file);
1579
1580	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1581		dev->std_set_in_tuner_core, dev->dev_state);
1582
1583	format->fmt.vbi.samples_per_line = dev->vbi_width;
1584	format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1585	format->fmt.vbi.offset = 0;
1586	format->fmt.vbi.flags = 0;
1587	format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1588
1589	format->fmt.vbi.count[0] = dev->vbi_height;
1590	format->fmt.vbi.count[1] = dev->vbi_height;
1591	format->fmt.vbi.start[0] = 21;
1592	format->fmt.vbi.start[1] = 284;
1593	memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1594
1595	return 0;
1596}
1597
1598static int vidioc_g_pixelaspect(struct file *file, void *priv,
1599				int type, struct v4l2_fract *f)
1600{
1601	struct au0828_dev *dev = video_drvdata(file);
1602
1603	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1604		return -EINVAL;
1605
1606	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1607		dev->std_set_in_tuner_core, dev->dev_state);
1608
1609	f->numerator = 54;
1610	f->denominator = 59;
1611
1612	return 0;
1613}
1614
1615static int vidioc_g_selection(struct file *file, void *priv,
1616			      struct v4l2_selection *s)
1617{
1618	struct au0828_dev *dev = video_drvdata(file);
1619
1620	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1621		return -EINVAL;
1622
1623	switch (s->target) {
1624	case V4L2_SEL_TGT_CROP_BOUNDS:
1625	case V4L2_SEL_TGT_CROP_DEFAULT:
1626		s->r.left = 0;
1627		s->r.top = 0;
1628		s->r.width = dev->width;
1629		s->r.height = dev->height;
1630		break;
1631	default:
1632		return -EINVAL;
1633	}
1634	return 0;
1635}
1636
1637#ifdef CONFIG_VIDEO_ADV_DEBUG
1638static int vidioc_g_register(struct file *file, void *priv,
1639			     struct v4l2_dbg_register *reg)
1640{
1641	struct au0828_dev *dev = video_drvdata(file);
1642
1643	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1644		dev->std_set_in_tuner_core, dev->dev_state);
1645
1646	reg->val = au0828_read(dev, reg->reg);
1647	reg->size = 1;
1648	return 0;
1649}
1650
1651static int vidioc_s_register(struct file *file, void *priv,
1652			     const struct v4l2_dbg_register *reg)
1653{
1654	struct au0828_dev *dev = video_drvdata(file);
1655
1656	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1657		dev->std_set_in_tuner_core, dev->dev_state);
1658
1659	return au0828_writereg(dev, reg->reg, reg->val);
1660}
1661#endif
1662
1663static int vidioc_log_status(struct file *file, void *fh)
1664{
1665	struct video_device *vdev = video_devdata(file);
1666
1667	dprintk(1, "%s called\n", __func__);
1668
1669	v4l2_ctrl_log_status(file, fh);
1670	v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1671	return 0;
1672}
1673
1674void au0828_v4l2_suspend(struct au0828_dev *dev)
1675{
1676	struct urb *urb;
1677	int i;
1678
1679	pr_info("stopping V4L2\n");
1680
1681	if (dev->stream_state == STREAM_ON) {
1682		pr_info("stopping V4L2 active URBs\n");
1683		au0828_analog_stream_disable(dev);
1684		/* stop urbs */
1685		for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1686			urb = dev->isoc_ctl.urb[i];
1687			if (urb) {
1688				if (!irqs_disabled())
1689					usb_kill_urb(urb);
1690				else
1691					usb_unlink_urb(urb);
1692			}
1693		}
1694	}
1695
1696	if (dev->vid_timeout_running)
1697		timer_delete_sync(&dev->vid_timeout);
1698	if (dev->vbi_timeout_running)
1699		timer_delete_sync(&dev->vbi_timeout);
1700}
1701
1702void au0828_v4l2_resume(struct au0828_dev *dev)
1703{
1704	int i, rc;
1705
1706	pr_info("restarting V4L2\n");
1707
1708	if (dev->stream_state == STREAM_ON) {
1709		au0828_stream_interrupt(dev);
1710		au0828_init_tuner(dev);
1711	}
1712
1713	if (dev->vid_timeout_running)
1714		mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1715	if (dev->vbi_timeout_running)
1716		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1717
1718	/* If we were doing ac97 instead of i2s, it would go here...*/
1719	au0828_i2s_init(dev);
1720
1721	au0828_analog_stream_enable(dev);
1722
1723	if (!(dev->stream_state == STREAM_ON)) {
1724		au0828_analog_stream_reset(dev);
1725		/* submit urbs */
1726		for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1727			rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1728			if (rc) {
1729				au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1730					       i, rc);
1731				au0828_uninit_isoc(dev);
1732			}
1733		}
1734	}
1735}
1736
1737static const struct v4l2_file_operations au0828_v4l_fops = {
1738	.owner      = THIS_MODULE,
1739	.open       = au0828_v4l2_open,
1740	.release    = au0828_v4l2_close,
1741	.read       = vb2_fop_read,
1742	.poll       = vb2_fop_poll,
1743	.mmap       = vb2_fop_mmap,
1744	.unlocked_ioctl = video_ioctl2,
1745};
1746
1747static const struct v4l2_ioctl_ops video_ioctl_ops = {
1748	.vidioc_querycap            = vidioc_querycap,
1749	.vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
1750	.vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
1751	.vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
1752	.vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
1753	.vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1754	.vidioc_try_fmt_vbi_cap     = vidioc_g_fmt_vbi_cap,
1755	.vidioc_s_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1756	.vidioc_enumaudio           = vidioc_enumaudio,
1757	.vidioc_g_audio             = vidioc_g_audio,
1758	.vidioc_s_audio             = vidioc_s_audio,
1759	.vidioc_g_pixelaspect       = vidioc_g_pixelaspect,
1760	.vidioc_g_selection         = vidioc_g_selection,
1761
1762	.vidioc_reqbufs             = vb2_ioctl_reqbufs,
1763	.vidioc_create_bufs         = vb2_ioctl_create_bufs,
1764	.vidioc_prepare_buf         = vb2_ioctl_prepare_buf,
1765	.vidioc_querybuf            = vb2_ioctl_querybuf,
1766	.vidioc_qbuf                = vb2_ioctl_qbuf,
1767	.vidioc_dqbuf               = vb2_ioctl_dqbuf,
1768	.vidioc_expbuf               = vb2_ioctl_expbuf,
1769
1770	.vidioc_s_std               = vidioc_s_std,
1771	.vidioc_g_std               = vidioc_g_std,
1772	.vidioc_enum_input          = vidioc_enum_input,
1773	.vidioc_g_input             = vidioc_g_input,
1774	.vidioc_s_input             = vidioc_s_input,
1775
1776	.vidioc_streamon            = vb2_ioctl_streamon,
1777	.vidioc_streamoff           = vb2_ioctl_streamoff,
1778
1779	.vidioc_g_tuner             = vidioc_g_tuner,
1780	.vidioc_s_tuner             = vidioc_s_tuner,
1781	.vidioc_g_frequency         = vidioc_g_frequency,
1782	.vidioc_s_frequency         = vidioc_s_frequency,
1783#ifdef CONFIG_VIDEO_ADV_DEBUG
1784	.vidioc_g_register          = vidioc_g_register,
1785	.vidioc_s_register          = vidioc_s_register,
1786#endif
1787	.vidioc_log_status	    = vidioc_log_status,
1788	.vidioc_subscribe_event     = v4l2_ctrl_subscribe_event,
1789	.vidioc_unsubscribe_event   = v4l2_event_unsubscribe,
1790};
1791
1792static const struct video_device au0828_video_template = {
1793	.fops                       = &au0828_v4l_fops,
1794	.release                    = video_device_release_empty,
1795	.ioctl_ops		    = &video_ioctl_ops,
1796	.tvnorms                    = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1797};
1798
1799static int au0828_vb2_setup(struct au0828_dev *dev)
1800{
1801	int rc;
1802	struct vb2_queue *q;
1803
1804	/* Setup Videobuf2 for Video capture */
1805	q = &dev->vb_vidq;
1806	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1807	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1808	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1809	q->drv_priv = dev;
1810	q->buf_struct_size = sizeof(struct au0828_buffer);
1811	q->ops = &au0828_video_qops;
1812	q->mem_ops = &vb2_vmalloc_memops;
1813
1814	rc = vb2_queue_init(q);
1815	if (rc < 0)
1816		return rc;
1817
1818	/* Setup Videobuf2 for VBI capture */
1819	q = &dev->vb_vbiq;
1820	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1821	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1822	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1823	q->drv_priv = dev;
1824	q->buf_struct_size = sizeof(struct au0828_buffer);
1825	q->ops = &au0828_vbi_qops;
1826	q->mem_ops = &vb2_vmalloc_memops;
1827
1828	rc = vb2_queue_init(q);
1829	if (rc < 0)
1830		return rc;
1831
1832	return 0;
1833}
1834
1835static void au0828_analog_create_entities(struct au0828_dev *dev)
1836{
1837#if defined(CONFIG_MEDIA_CONTROLLER)
1838	static const char * const inames[] = {
1839		[AU0828_VMUX_COMPOSITE] = "Composite",
1840		[AU0828_VMUX_SVIDEO] = "S-Video",
1841		[AU0828_VMUX_CABLE] = "Cable TV",
1842		[AU0828_VMUX_TELEVISION] = "Television",
1843		[AU0828_VMUX_DVB] = "DVB",
1844	};
1845	int ret, i;
1846
1847	/* Initialize Video and VBI pads */
1848	dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1849	ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1850	if (ret < 0)
1851		pr_err("failed to initialize video media entity!\n");
1852
1853	dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1854	ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1855	if (ret < 0)
1856		pr_err("failed to initialize vbi media entity!\n");
1857
1858	/* Create entities for each input connector */
1859	for (i = 0; i < AU0828_MAX_INPUT; i++) {
1860		struct media_entity *ent = &dev->input_ent[i];
1861
1862		if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1863			break;
1864
1865		ent->name = inames[AUVI_INPUT(i).type];
1866		ent->flags = MEDIA_ENT_FL_CONNECTOR;
1867		dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1868
1869		switch (AUVI_INPUT(i).type) {
1870		case AU0828_VMUX_COMPOSITE:
1871			ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1872			break;
1873		case AU0828_VMUX_SVIDEO:
1874			ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1875			break;
1876		case AU0828_VMUX_CABLE:
1877		case AU0828_VMUX_TELEVISION:
1878		case AU0828_VMUX_DVB:
1879		default: /* Just to shut up a warning */
1880			ent->function = MEDIA_ENT_F_CONN_RF;
1881			break;
1882		}
1883
1884		ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1885		if (ret < 0)
1886			pr_err("failed to initialize input pad[%d]!\n", i);
1887
1888		ret = media_device_register_entity(dev->media_dev, ent);
1889		if (ret < 0)
1890			pr_err("failed to register input entity %d!\n", i);
1891	}
1892#endif
1893}
1894
1895/**************************************************************************/
1896
1897int au0828_analog_register(struct au0828_dev *dev,
1898			   struct usb_interface *interface)
1899{
1900	int retval = -ENOMEM;
1901	struct usb_host_interface *iface_desc;
1902	struct usb_endpoint_descriptor *endpoint;
1903	int i, ret;
1904
1905	dprintk(1, "au0828_analog_register called for intf#%d!\n",
1906		interface->cur_altsetting->desc.bInterfaceNumber);
1907
1908	/* No analog TV */
1909	if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1910		return 0;
1911
1912	/* set au0828 usb interface0 to as5 */
1913	retval = usb_set_interface(dev->usbdev,
1914			interface->cur_altsetting->desc.bInterfaceNumber, 5);
1915	if (retval != 0) {
1916		pr_info("Failure setting usb interface0 to as5\n");
1917		return retval;
1918	}
1919
1920	/* Figure out which endpoint has the isoc interface */
1921	iface_desc = interface->cur_altsetting;
1922	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1923		endpoint = &iface_desc->endpoint[i].desc;
1924		if (usb_endpoint_dir_in(endpoint) &&
1925		    (usb_endpoint_type(endpoint)
1926		     == USB_ENDPOINT_XFER_ISOC)) {
1927
1928			/* we find our isoc in endpoint */
1929			u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1930			dev->max_pkt_size = (tmp & 0x07ff) *
1931				(((tmp & 0x1800) >> 11) + 1);
1932			dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1933			dprintk(1,
1934				"Found isoc endpoint 0x%02x, max size = %d\n",
1935				dev->isoc_in_endpointaddr, dev->max_pkt_size);
1936		}
1937	}
1938	if (!(dev->isoc_in_endpointaddr)) {
1939		pr_info("Could not locate isoc endpoint\n");
1940		return -ENODEV;
1941	}
1942
1943	init_waitqueue_head(&dev->open);
1944	spin_lock_init(&dev->slock);
1945
1946	/* init video dma queues */
1947	INIT_LIST_HEAD(&dev->vidq.active);
1948	INIT_LIST_HEAD(&dev->vbiq.active);
1949
1950	timer_setup(&dev->vid_timeout, au0828_vid_buffer_timeout, 0);
1951	timer_setup(&dev->vbi_timeout, au0828_vbi_buffer_timeout, 0);
1952
1953	dev->width = NTSC_STD_W;
1954	dev->height = NTSC_STD_H;
1955	dev->field_size = dev->width * dev->height;
1956	dev->frame_size = dev->field_size << 1;
1957	dev->bytesperline = dev->width << 1;
1958	dev->vbi_width = 720;
1959	dev->vbi_height = 1;
1960	dev->ctrl_ainput = 0;
1961	dev->ctrl_freq = 960;
1962	dev->std = V4L2_STD_NTSC_M;
1963	/* Default input is TV Tuner */
1964	au0828_s_input(dev, 0);
1965
1966	mutex_init(&dev->vb_queue_lock);
1967	mutex_init(&dev->vb_vbi_queue_lock);
1968
1969	/* Fill the video capture device struct */
1970	dev->vdev = au0828_video_template;
1971	dev->vdev.v4l2_dev = &dev->v4l2_dev;
1972	dev->vdev.lock = &dev->lock;
1973	dev->vdev.queue = &dev->vb_vidq;
1974	dev->vdev.queue->lock = &dev->vb_queue_lock;
1975	dev->vdev.device_caps =
1976		V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1977		V4L2_CAP_TUNER | V4L2_CAP_VIDEO_CAPTURE;
1978	strscpy(dev->vdev.name, "au0828a video", sizeof(dev->vdev.name));
1979
1980	/* Setup the VBI device */
1981	dev->vbi_dev = au0828_video_template;
1982	dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1983	dev->vbi_dev.lock = &dev->lock;
1984	dev->vbi_dev.queue = &dev->vb_vbiq;
1985	dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1986	dev->vbi_dev.device_caps =
1987		V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1988		V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE;
1989	strscpy(dev->vbi_dev.name, "au0828a vbi", sizeof(dev->vbi_dev.name));
1990
1991	/* Init entities at the Media Controller */
1992	au0828_analog_create_entities(dev);
1993
1994	/* initialize videobuf2 stuff */
1995	retval = au0828_vb2_setup(dev);
1996	if (retval != 0) {
1997		dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
1998			retval);
1999		return -ENODEV;
2000	}
2001
2002	/* Register the v4l2 device */
2003	video_set_drvdata(&dev->vdev, dev);
2004	retval = video_register_device(&dev->vdev, VFL_TYPE_VIDEO, -1);
2005	if (retval != 0) {
2006		dprintk(1, "unable to register video device (error = %d).\n",
2007			retval);
2008		return -ENODEV;
2009	}
2010
2011	/* Register the vbi device */
2012	video_set_drvdata(&dev->vbi_dev, dev);
2013	retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
2014	if (retval != 0) {
2015		dprintk(1, "unable to register vbi device (error = %d).\n",
2016			retval);
2017		ret = -ENODEV;
2018		goto err_reg_vbi_dev;
2019	}
2020
2021#ifdef CONFIG_MEDIA_CONTROLLER
2022	retval = v4l2_mc_create_media_graph(dev->media_dev);
2023	if (retval) {
2024		pr_err("%s() au0282_dev_register failed to create graph\n",
2025			__func__);
2026		ret = -ENODEV;
2027		goto err_reg_vbi_dev;
2028	}
2029#endif
2030
2031	dprintk(1, "%s completed!\n", __func__);
2032
2033	return 0;
2034
2035err_reg_vbi_dev:
2036	vb2_video_unregister_device(&dev->vdev);
2037	return ret;
2038}
2039
~/ Read-only file preview.