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