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_v4l2_buffer *vb = &buf->vb;
305 	struct vb2_queue *q = vb->vb2_buf.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->sequence = dev->frame_count++;
312 	else
313 		vb->sequence = dev->vbi_frame_count++;
314 
315 	vb->field = V4L2_FIELD_INTERLACED;
316 	v4l2_get_timestamp(&vb->timestamp);
317 	vb2_buffer_done(&vb->vb2_buf, 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.vb2_buf, 0);
534 
535 	vbi_buf = dev->isoc_ctl.vbi_buf;
536 	if (vbi_buf != NULL)
537 		vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 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.vb2_buf, 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(
586 						&buf->vb.vb2_buf, 0);
587 
588 				/* As long as isoc traffic is arriving, keep
589 				   resetting the timer */
590 				if (dev->vid_timeout_running)
591 					mod_timer(&dev->vid_timeout,
592 						  jiffies + (HZ / 10));
593 				if (dev->vbi_timeout_running)
594 					mod_timer(&dev->vbi_timeout,
595 						  jiffies + (HZ / 10));
596 			}
597 
598 			if (buf != NULL) {
599 				if (fbyte & 0x40)
600 					buf->top_field = 1;
601 				else
602 					buf->top_field = 0;
603 			}
604 
605 			if (vbi_buf != NULL) {
606 				if (fbyte & 0x40)
607 					vbi_buf->top_field = 1;
608 				else
609 					vbi_buf->top_field = 0;
610 			}
611 
612 			dev->vbi_read = 0;
613 			vbi_dma_q->pos = 0;
614 			dma_q->pos = 0;
615 		}
616 
617 		vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
618 		if (dev->vbi_read < vbi_field_size) {
619 			remain  = vbi_field_size - dev->vbi_read;
620 			if (len < remain)
621 				lencopy = len;
622 			else
623 				lencopy = remain;
624 
625 			if (vbi_buf != NULL)
626 				au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
627 						vbioutp, len);
628 
629 			len -= lencopy;
630 			p += lencopy;
631 			dev->vbi_read += lencopy;
632 		}
633 
634 		if (dev->vbi_read >= vbi_field_size && buf != NULL)
635 			au0828_copy_video(dev, dma_q, buf, p, outp, len);
636 	}
637 	return rc;
638 }
639 
queue_setup(struct vb2_queue * vq,const void * parg,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],void * alloc_ctxs[])640 static int queue_setup(struct vb2_queue *vq, const void *parg,
641 		       unsigned int *nbuffers, unsigned int *nplanes,
642 		       unsigned int sizes[], void *alloc_ctxs[])
643 {
644 	const struct v4l2_format *fmt = parg;
645 	struct au0828_dev *dev = vb2_get_drv_priv(vq);
646 	unsigned long img_size = dev->height * dev->bytesperline;
647 	unsigned long size;
648 
649 	size = fmt ? fmt->fmt.pix.sizeimage : img_size;
650 	if (size < img_size)
651 		return -EINVAL;
652 
653 	*nplanes = 1;
654 	sizes[0] = size;
655 
656 	return 0;
657 }
658 
659 static int
buffer_prepare(struct vb2_buffer * vb)660 buffer_prepare(struct vb2_buffer *vb)
661 {
662 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
663 	struct au0828_buffer *buf = container_of(vbuf,
664 				struct au0828_buffer, vb);
665 	struct au0828_dev    *dev = vb2_get_drv_priv(vb->vb2_queue);
666 
667 	buf->length = dev->height * dev->bytesperline;
668 
669 	if (vb2_plane_size(vb, 0) < buf->length) {
670 		pr_err("%s data will not fit into plane (%lu < %lu)\n",
671 			__func__, vb2_plane_size(vb, 0), buf->length);
672 		return -EINVAL;
673 	}
674 	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
675 	return 0;
676 }
677 
678 static void
buffer_queue(struct vb2_buffer * vb)679 buffer_queue(struct vb2_buffer *vb)
680 {
681 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
682 	struct au0828_buffer    *buf     = container_of(vbuf,
683 							struct au0828_buffer,
684 							vb);
685 	struct au0828_dev       *dev     = vb2_get_drv_priv(vb->vb2_queue);
686 	struct au0828_dmaqueue  *vidq    = &dev->vidq;
687 	unsigned long flags = 0;
688 
689 	buf->mem = vb2_plane_vaddr(vb, 0);
690 	buf->length = vb2_plane_size(vb, 0);
691 
692 	spin_lock_irqsave(&dev->slock, flags);
693 	list_add_tail(&buf->list, &vidq->active);
694 	spin_unlock_irqrestore(&dev->slock, flags);
695 }
696 
au0828_i2s_init(struct au0828_dev * dev)697 static int au0828_i2s_init(struct au0828_dev *dev)
698 {
699 	/* Enable i2s mode */
700 	au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
701 	return 0;
702 }
703 
704 /*
705  * Auvitek au0828 analog stream enable
706  */
au0828_analog_stream_enable(struct au0828_dev * d)707 static int au0828_analog_stream_enable(struct au0828_dev *d)
708 {
709 	struct usb_interface *iface;
710 	int ret, h, w;
711 
712 	dprintk(1, "au0828_analog_stream_enable called\n");
713 
714 	iface = usb_ifnum_to_if(d->usbdev, 0);
715 	if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
716 		dprintk(1, "Changing intf#0 to alt 5\n");
717 		/* set au0828 interface0 to AS5 here again */
718 		ret = usb_set_interface(d->usbdev, 0, 5);
719 		if (ret < 0) {
720 			pr_info("Au0828 can't set alt setting to 5!\n");
721 			return -EBUSY;
722 		}
723 	}
724 
725 	h = d->height / 2 + 2;
726 	w = d->width * 2;
727 
728 	au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
729 	au0828_writereg(d, 0x106, 0x00);
730 	/* set x position */
731 	au0828_writereg(d, 0x110, 0x00);
732 	au0828_writereg(d, 0x111, 0x00);
733 	au0828_writereg(d, 0x114, w & 0xff);
734 	au0828_writereg(d, 0x115, w >> 8);
735 	/* set y position */
736 	au0828_writereg(d, 0x112, 0x00);
737 	au0828_writereg(d, 0x113, 0x00);
738 	au0828_writereg(d, 0x116, h & 0xff);
739 	au0828_writereg(d, 0x117, h >> 8);
740 	au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
741 
742 	return 0;
743 }
744 
au0828_analog_stream_disable(struct au0828_dev * d)745 static int au0828_analog_stream_disable(struct au0828_dev *d)
746 {
747 	dprintk(1, "au0828_analog_stream_disable called\n");
748 	au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
749 	return 0;
750 }
751 
au0828_analog_stream_reset(struct au0828_dev * dev)752 static void au0828_analog_stream_reset(struct au0828_dev *dev)
753 {
754 	dprintk(1, "au0828_analog_stream_reset called\n");
755 	au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
756 	mdelay(30);
757 	au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
758 }
759 
760 /*
761  * Some operations needs to stop current streaming
762  */
au0828_stream_interrupt(struct au0828_dev * dev)763 static int au0828_stream_interrupt(struct au0828_dev *dev)
764 {
765 	int ret = 0;
766 
767 	dev->stream_state = STREAM_INTERRUPT;
768 	if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
769 		return -ENODEV;
770 	else if (ret) {
771 		set_bit(DEV_MISCONFIGURED, &dev->dev_state);
772 		dprintk(1, "%s device is misconfigured!\n", __func__);
773 		return ret;
774 	}
775 	return 0;
776 }
777 
au0828_start_analog_streaming(struct vb2_queue * vq,unsigned int count)778 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
779 {
780 	struct au0828_dev *dev = vb2_get_drv_priv(vq);
781 	int rc = 0;
782 
783 	dprintk(1, "au0828_start_analog_streaming called %d\n",
784 		dev->streaming_users);
785 
786 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
787 		dev->frame_count = 0;
788 	else
789 		dev->vbi_frame_count = 0;
790 
791 	if (dev->streaming_users == 0) {
792 		/* If we were doing ac97 instead of i2s, it would go here...*/
793 		au0828_i2s_init(dev);
794 		rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
795 				   AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
796 				   au0828_isoc_copy);
797 		if (rc < 0) {
798 			pr_info("au0828_init_isoc failed\n");
799 			return rc;
800 		}
801 
802 		if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
803 			v4l2_device_call_all(&dev->v4l2_dev, 0, video,
804 						s_stream, 1);
805 			dev->vid_timeout_running = 1;
806 			mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
807 		} else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
808 			dev->vbi_timeout_running = 1;
809 			mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
810 		}
811 	}
812 	dev->streaming_users++;
813 	return rc;
814 }
815 
au0828_stop_streaming(struct vb2_queue * vq)816 static void au0828_stop_streaming(struct vb2_queue *vq)
817 {
818 	struct au0828_dev *dev = vb2_get_drv_priv(vq);
819 	struct au0828_dmaqueue *vidq = &dev->vidq;
820 	unsigned long flags = 0;
821 
822 	dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
823 
824 	if (dev->streaming_users-- == 1)
825 		au0828_uninit_isoc(dev);
826 
827 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
828 	dev->vid_timeout_running = 0;
829 	del_timer_sync(&dev->vid_timeout);
830 
831 	spin_lock_irqsave(&dev->slock, flags);
832 	if (dev->isoc_ctl.buf != NULL) {
833 		vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
834 				VB2_BUF_STATE_ERROR);
835 		dev->isoc_ctl.buf = NULL;
836 	}
837 	while (!list_empty(&vidq->active)) {
838 		struct au0828_buffer *buf;
839 
840 		buf = list_entry(vidq->active.next, struct au0828_buffer, list);
841 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
842 		list_del(&buf->list);
843 	}
844 	spin_unlock_irqrestore(&dev->slock, flags);
845 }
846 
au0828_stop_vbi_streaming(struct vb2_queue * vq)847 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
848 {
849 	struct au0828_dev *dev = vb2_get_drv_priv(vq);
850 	struct au0828_dmaqueue *vbiq = &dev->vbiq;
851 	unsigned long flags = 0;
852 
853 	dprintk(1, "au0828_stop_vbi_streaming called %d\n",
854 		dev->streaming_users);
855 
856 	if (dev->streaming_users-- == 1)
857 		au0828_uninit_isoc(dev);
858 
859 	spin_lock_irqsave(&dev->slock, flags);
860 	if (dev->isoc_ctl.vbi_buf != NULL) {
861 		vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
862 				VB2_BUF_STATE_ERROR);
863 		dev->isoc_ctl.vbi_buf = NULL;
864 	}
865 	while (!list_empty(&vbiq->active)) {
866 		struct au0828_buffer *buf;
867 
868 		buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
869 		list_del(&buf->list);
870 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
871 	}
872 	spin_unlock_irqrestore(&dev->slock, flags);
873 
874 	dev->vbi_timeout_running = 0;
875 	del_timer_sync(&dev->vbi_timeout);
876 }
877 
878 static struct vb2_ops au0828_video_qops = {
879 	.queue_setup     = queue_setup,
880 	.buf_prepare     = buffer_prepare,
881 	.buf_queue       = buffer_queue,
882 	.start_streaming = au0828_start_analog_streaming,
883 	.stop_streaming  = au0828_stop_streaming,
884 	.wait_prepare    = vb2_ops_wait_prepare,
885 	.wait_finish     = vb2_ops_wait_finish,
886 };
887 
888 /* ------------------------------------------------------------------
889    V4L2 interface
890    ------------------------------------------------------------------*/
891 /*
892  * au0828_analog_unregister
893  * unregister v4l2 devices
894  */
au0828_analog_unregister(struct au0828_dev * dev)895 void au0828_analog_unregister(struct au0828_dev *dev)
896 {
897 	dprintk(1, "au0828_analog_unregister called\n");
898 	mutex_lock(&au0828_sysfs_lock);
899 	video_unregister_device(&dev->vdev);
900 	video_unregister_device(&dev->vbi_dev);
901 	mutex_unlock(&au0828_sysfs_lock);
902 }
903 
904 /* This function ensures that video frames continue to be delivered even if
905    the ITU-656 input isn't receiving any data (thereby preventing applications
906    such as tvtime from hanging) */
au0828_vid_buffer_timeout(unsigned long data)907 static void au0828_vid_buffer_timeout(unsigned long data)
908 {
909 	struct au0828_dev *dev = (struct au0828_dev *) data;
910 	struct au0828_dmaqueue *dma_q = &dev->vidq;
911 	struct au0828_buffer *buf;
912 	unsigned char *vid_data;
913 	unsigned long flags = 0;
914 
915 	spin_lock_irqsave(&dev->slock, flags);
916 
917 	buf = dev->isoc_ctl.buf;
918 	if (buf != NULL) {
919 		vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
920 		memset(vid_data, 0x00, buf->length); /* Blank green frame */
921 		buffer_filled(dev, dma_q, buf);
922 	}
923 	get_next_buf(dma_q, &buf);
924 
925 	if (dev->vid_timeout_running == 1)
926 		mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
927 
928 	spin_unlock_irqrestore(&dev->slock, flags);
929 }
930 
au0828_vbi_buffer_timeout(unsigned long data)931 static void au0828_vbi_buffer_timeout(unsigned long data)
932 {
933 	struct au0828_dev *dev = (struct au0828_dev *) data;
934 	struct au0828_dmaqueue *dma_q = &dev->vbiq;
935 	struct au0828_buffer *buf;
936 	unsigned char *vbi_data;
937 	unsigned long flags = 0;
938 
939 	spin_lock_irqsave(&dev->slock, flags);
940 
941 	buf = dev->isoc_ctl.vbi_buf;
942 	if (buf != NULL) {
943 		vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
944 		memset(vbi_data, 0x00, buf->length);
945 		buffer_filled(dev, dma_q, buf);
946 	}
947 	vbi_get_next_buf(dma_q, &buf);
948 
949 	if (dev->vbi_timeout_running == 1)
950 		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
951 	spin_unlock_irqrestore(&dev->slock, flags);
952 }
953 
au0828_v4l2_open(struct file * filp)954 static int au0828_v4l2_open(struct file *filp)
955 {
956 	struct au0828_dev *dev = video_drvdata(filp);
957 	int ret;
958 
959 	dprintk(1,
960 		"%s called std_set %d dev_state %ld stream users %d users %d\n",
961 		__func__, dev->std_set_in_tuner_core, dev->dev_state,
962 		dev->streaming_users, dev->users);
963 
964 	if (mutex_lock_interruptible(&dev->lock))
965 		return -ERESTARTSYS;
966 
967 	ret = v4l2_fh_open(filp);
968 	if (ret) {
969 		au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
970 				__func__, ret);
971 		mutex_unlock(&dev->lock);
972 		return ret;
973 	}
974 
975 	if (dev->users == 0) {
976 		au0828_analog_stream_enable(dev);
977 		au0828_analog_stream_reset(dev);
978 		dev->stream_state = STREAM_OFF;
979 		set_bit(DEV_INITIALIZED, &dev->dev_state);
980 	}
981 	dev->users++;
982 	mutex_unlock(&dev->lock);
983 	return ret;
984 }
985 
au0828_v4l2_close(struct file * filp)986 static int au0828_v4l2_close(struct file *filp)
987 {
988 	int ret;
989 	struct au0828_dev *dev = video_drvdata(filp);
990 	struct video_device *vdev = video_devdata(filp);
991 
992 	dprintk(1,
993 		"%s called std_set %d dev_state %ld stream users %d users %d\n",
994 		__func__, dev->std_set_in_tuner_core, dev->dev_state,
995 		dev->streaming_users, dev->users);
996 
997 	mutex_lock(&dev->lock);
998 	if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
999 		/* Cancel timeout thread in case they didn't call streamoff */
1000 		dev->vid_timeout_running = 0;
1001 		del_timer_sync(&dev->vid_timeout);
1002 	} else if (vdev->vfl_type == VFL_TYPE_VBI &&
1003 			dev->vbi_timeout_running) {
1004 		/* Cancel timeout thread in case they didn't call streamoff */
1005 		dev->vbi_timeout_running = 0;
1006 		del_timer_sync(&dev->vbi_timeout);
1007 	}
1008 
1009 	if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
1010 		goto end;
1011 
1012 	if (dev->users == 1) {
1013 		/* Save some power by putting tuner to sleep */
1014 		v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1015 		dev->std_set_in_tuner_core = 0;
1016 
1017 		/* When close the device, set the usb intf0 into alt0 to free
1018 		   USB bandwidth */
1019 		ret = usb_set_interface(dev->usbdev, 0, 0);
1020 		if (ret < 0)
1021 			pr_info("Au0828 can't set alternate to 0!\n");
1022 	}
1023 end:
1024 	_vb2_fop_release(filp, NULL);
1025 	dev->users--;
1026 	mutex_unlock(&dev->lock);
1027 	return 0;
1028 }
1029 
1030 /* Must be called with dev->lock held */
au0828_init_tuner(struct au0828_dev * dev)1031 static void au0828_init_tuner(struct au0828_dev *dev)
1032 {
1033 	struct v4l2_frequency f = {
1034 		.frequency = dev->ctrl_freq,
1035 		.type = V4L2_TUNER_ANALOG_TV,
1036 	};
1037 
1038 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1039 		dev->std_set_in_tuner_core, dev->dev_state);
1040 
1041 	if (dev->std_set_in_tuner_core)
1042 		return;
1043 	dev->std_set_in_tuner_core = 1;
1044 	i2c_gate_ctrl(dev, 1);
1045 	/* If we've never sent the standard in tuner core, do so now.
1046 	   We don't do this at device probe because we don't want to
1047 	   incur the cost of a firmware load */
1048 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1049 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1050 	i2c_gate_ctrl(dev, 0);
1051 }
1052 
au0828_set_format(struct au0828_dev * dev,unsigned int cmd,struct v4l2_format * format)1053 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1054 			     struct v4l2_format *format)
1055 {
1056 	int ret;
1057 	int width = format->fmt.pix.width;
1058 	int height = format->fmt.pix.height;
1059 
1060 	/* If they are demanding a format other than the one we support,
1061 	   bail out (tvtime asks for UYVY and then retries with YUYV) */
1062 	if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1063 		return -EINVAL;
1064 
1065 	/* format->fmt.pix.width only support 720 and height 480 */
1066 	if (width != 720)
1067 		width = 720;
1068 	if (height != 480)
1069 		height = 480;
1070 
1071 	format->fmt.pix.width = width;
1072 	format->fmt.pix.height = height;
1073 	format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1074 	format->fmt.pix.bytesperline = width * 2;
1075 	format->fmt.pix.sizeimage = width * height * 2;
1076 	format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1077 	format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1078 	format->fmt.pix.priv = 0;
1079 
1080 	if (cmd == VIDIOC_TRY_FMT)
1081 		return 0;
1082 
1083 	/* maybe set new image format, driver current only support 720*480 */
1084 	dev->width = width;
1085 	dev->height = height;
1086 	dev->frame_size = width * height * 2;
1087 	dev->field_size = width * height;
1088 	dev->bytesperline = width * 2;
1089 
1090 	if (dev->stream_state == STREAM_ON) {
1091 		dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1092 		ret = au0828_stream_interrupt(dev);
1093 		if (ret != 0) {
1094 			dprintk(1, "error interrupting video stream!\n");
1095 			return ret;
1096 		}
1097 	}
1098 
1099 	au0828_analog_stream_enable(dev);
1100 
1101 	return 0;
1102 }
1103 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)1104 static int vidioc_querycap(struct file *file, void  *priv,
1105 			   struct v4l2_capability *cap)
1106 {
1107 	struct video_device *vdev = video_devdata(file);
1108 	struct au0828_dev *dev = video_drvdata(file);
1109 
1110 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1111 		dev->std_set_in_tuner_core, dev->dev_state);
1112 
1113 	strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1114 	strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1115 	usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1116 
1117 	/* set the device capabilities */
1118 	cap->device_caps = V4L2_CAP_AUDIO |
1119 		V4L2_CAP_READWRITE |
1120 		V4L2_CAP_STREAMING |
1121 		V4L2_CAP_TUNER;
1122 	if (vdev->vfl_type == VFL_TYPE_GRABBER)
1123 		cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1124 	else
1125 		cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1126 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1127 		V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
1128 	return 0;
1129 }
1130 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)1131 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1132 					struct v4l2_fmtdesc *f)
1133 {
1134 	if (f->index)
1135 		return -EINVAL;
1136 
1137 	dprintk(1, "%s called\n", __func__);
1138 
1139 	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1140 	strcpy(f->description, "Packed YUV2");
1141 
1142 	f->flags = 0;
1143 	f->pixelformat = V4L2_PIX_FMT_UYVY;
1144 
1145 	return 0;
1146 }
1147 
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1148 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1149 					struct v4l2_format *f)
1150 {
1151 	struct au0828_dev *dev = video_drvdata(file);
1152 
1153 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1154 		dev->std_set_in_tuner_core, dev->dev_state);
1155 
1156 	f->fmt.pix.width = dev->width;
1157 	f->fmt.pix.height = dev->height;
1158 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1159 	f->fmt.pix.bytesperline = dev->bytesperline;
1160 	f->fmt.pix.sizeimage = dev->frame_size;
1161 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1162 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1163 	f->fmt.pix.priv = 0;
1164 	return 0;
1165 }
1166 
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1167 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1168 				  struct v4l2_format *f)
1169 {
1170 	struct au0828_dev *dev = video_drvdata(file);
1171 
1172 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1173 		dev->std_set_in_tuner_core, dev->dev_state);
1174 
1175 	return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1176 }
1177 
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1178 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1179 				struct v4l2_format *f)
1180 {
1181 	struct au0828_dev *dev = video_drvdata(file);
1182 	int rc;
1183 
1184 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1185 		dev->std_set_in_tuner_core, dev->dev_state);
1186 
1187 	rc = check_dev(dev);
1188 	if (rc < 0)
1189 		return rc;
1190 
1191 	if (vb2_is_busy(&dev->vb_vidq)) {
1192 		pr_info("%s queue busy\n", __func__);
1193 		rc = -EBUSY;
1194 		goto out;
1195 	}
1196 
1197 	rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1198 out:
1199 	return rc;
1200 }
1201 
vidioc_s_std(struct file * file,void * priv,v4l2_std_id norm)1202 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1203 {
1204 	struct au0828_dev *dev = video_drvdata(file);
1205 
1206 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1207 		dev->std_set_in_tuner_core, dev->dev_state);
1208 
1209 	if (norm == dev->std)
1210 		return 0;
1211 
1212 	if (dev->streaming_users > 0)
1213 		return -EBUSY;
1214 
1215 	dev->std = norm;
1216 
1217 	au0828_init_tuner(dev);
1218 
1219 	i2c_gate_ctrl(dev, 1);
1220 
1221 	/*
1222 	 * FIXME: when we support something other than 60Hz standards,
1223 	 * we are going to have to make the au0828 bridge adjust the size
1224 	 * of its capture buffer, which is currently hardcoded at 720x480
1225 	 */
1226 
1227 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1228 
1229 	i2c_gate_ctrl(dev, 0);
1230 
1231 	return 0;
1232 }
1233 
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * norm)1234 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1235 {
1236 	struct au0828_dev *dev = video_drvdata(file);
1237 
1238 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1239 		dev->std_set_in_tuner_core, dev->dev_state);
1240 
1241 	*norm = dev->std;
1242 	return 0;
1243 }
1244 
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * input)1245 static int vidioc_enum_input(struct file *file, void *priv,
1246 				struct v4l2_input *input)
1247 {
1248 	struct au0828_dev *dev = video_drvdata(file);
1249 	unsigned int tmp;
1250 
1251 	static const char *inames[] = {
1252 		[AU0828_VMUX_UNDEFINED] = "Undefined",
1253 		[AU0828_VMUX_COMPOSITE] = "Composite",
1254 		[AU0828_VMUX_SVIDEO] = "S-Video",
1255 		[AU0828_VMUX_CABLE] = "Cable TV",
1256 		[AU0828_VMUX_TELEVISION] = "Television",
1257 		[AU0828_VMUX_DVB] = "DVB",
1258 		[AU0828_VMUX_DEBUG] = "tv debug"
1259 	};
1260 
1261 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1262 		dev->std_set_in_tuner_core, dev->dev_state);
1263 
1264 	tmp = input->index;
1265 
1266 	if (tmp >= AU0828_MAX_INPUT)
1267 		return -EINVAL;
1268 	if (AUVI_INPUT(tmp).type == 0)
1269 		return -EINVAL;
1270 
1271 	input->index = tmp;
1272 	strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1273 	if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1274 	    (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1275 		input->type |= V4L2_INPUT_TYPE_TUNER;
1276 		input->audioset = 1;
1277 	} else {
1278 		input->type |= V4L2_INPUT_TYPE_CAMERA;
1279 		input->audioset = 2;
1280 	}
1281 
1282 	input->std = dev->vdev.tvnorms;
1283 
1284 	return 0;
1285 }
1286 
vidioc_g_input(struct file * file,void * priv,unsigned int * i)1287 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1288 {
1289 	struct au0828_dev *dev = video_drvdata(file);
1290 
1291 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1292 		dev->std_set_in_tuner_core, dev->dev_state);
1293 
1294 	*i = dev->ctrl_input;
1295 	return 0;
1296 }
1297 
au0828_s_input(struct au0828_dev * dev,int index)1298 static void au0828_s_input(struct au0828_dev *dev, int index)
1299 {
1300 	int i;
1301 
1302 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1303 		dev->std_set_in_tuner_core, dev->dev_state);
1304 
1305 	switch (AUVI_INPUT(index).type) {
1306 	case AU0828_VMUX_SVIDEO:
1307 		dev->input_type = AU0828_VMUX_SVIDEO;
1308 		dev->ctrl_ainput = 1;
1309 		break;
1310 	case AU0828_VMUX_COMPOSITE:
1311 		dev->input_type = AU0828_VMUX_COMPOSITE;
1312 		dev->ctrl_ainput = 1;
1313 		break;
1314 	case AU0828_VMUX_TELEVISION:
1315 		dev->input_type = AU0828_VMUX_TELEVISION;
1316 		dev->ctrl_ainput = 0;
1317 		break;
1318 	default:
1319 		dprintk(1, "unknown input type set [%d]\n",
1320 			AUVI_INPUT(index).type);
1321 		break;
1322 	}
1323 
1324 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1325 			AUVI_INPUT(index).vmux, 0, 0);
1326 
1327 	for (i = 0; i < AU0828_MAX_INPUT; i++) {
1328 		int enable = 0;
1329 		if (AUVI_INPUT(i).audio_setup == NULL)
1330 			continue;
1331 
1332 		if (i == index)
1333 			enable = 1;
1334 		else
1335 			enable = 0;
1336 		if (enable) {
1337 			(AUVI_INPUT(i).audio_setup)(dev, enable);
1338 		} else {
1339 			/* Make sure we leave it turned on if some
1340 			   other input is routed to this callback */
1341 			if ((AUVI_INPUT(i).audio_setup) !=
1342 			    ((AUVI_INPUT(index).audio_setup))) {
1343 				(AUVI_INPUT(i).audio_setup)(dev, enable);
1344 			}
1345 		}
1346 	}
1347 
1348 	v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1349 			AUVI_INPUT(index).amux, 0, 0);
1350 }
1351 
vidioc_s_input(struct file * file,void * priv,unsigned int index)1352 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1353 {
1354 	struct au0828_dev *dev = video_drvdata(file);
1355 
1356 	dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1357 		index);
1358 	if (index >= AU0828_MAX_INPUT)
1359 		return -EINVAL;
1360 	if (AUVI_INPUT(index).type == 0)
1361 		return -EINVAL;
1362 	dev->ctrl_input = index;
1363 	au0828_s_input(dev, index);
1364 	return 0;
1365 }
1366 
vidioc_enumaudio(struct file * file,void * priv,struct v4l2_audio * a)1367 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1368 {
1369 	if (a->index > 1)
1370 		return -EINVAL;
1371 
1372 	dprintk(1, "%s called\n", __func__);
1373 
1374 	if (a->index == 0)
1375 		strcpy(a->name, "Television");
1376 	else
1377 		strcpy(a->name, "Line in");
1378 
1379 	a->capability = V4L2_AUDCAP_STEREO;
1380 	return 0;
1381 }
1382 
vidioc_g_audio(struct file * file,void * priv,struct v4l2_audio * a)1383 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1384 {
1385 	struct au0828_dev *dev = video_drvdata(file);
1386 
1387 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1388 		dev->std_set_in_tuner_core, dev->dev_state);
1389 
1390 	a->index = dev->ctrl_ainput;
1391 	if (a->index == 0)
1392 		strcpy(a->name, "Television");
1393 	else
1394 		strcpy(a->name, "Line in");
1395 
1396 	a->capability = V4L2_AUDCAP_STEREO;
1397 	return 0;
1398 }
1399 
vidioc_s_audio(struct file * file,void * priv,const struct v4l2_audio * a)1400 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1401 {
1402 	struct au0828_dev *dev = video_drvdata(file);
1403 
1404 	if (a->index != dev->ctrl_ainput)
1405 		return -EINVAL;
1406 
1407 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1408 		dev->std_set_in_tuner_core, dev->dev_state);
1409 	return 0;
1410 }
1411 
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)1412 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1413 {
1414 	struct au0828_dev *dev = video_drvdata(file);
1415 
1416 	if (t->index != 0)
1417 		return -EINVAL;
1418 
1419 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1420 		dev->std_set_in_tuner_core, dev->dev_state);
1421 
1422 	strcpy(t->name, "Auvitek tuner");
1423 
1424 	au0828_init_tuner(dev);
1425 	i2c_gate_ctrl(dev, 1);
1426 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1427 	i2c_gate_ctrl(dev, 0);
1428 	return 0;
1429 }
1430 
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)1431 static int vidioc_s_tuner(struct file *file, void *priv,
1432 				const struct v4l2_tuner *t)
1433 {
1434 	struct au0828_dev *dev = video_drvdata(file);
1435 
1436 	if (t->index != 0)
1437 		return -EINVAL;
1438 
1439 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1440 		dev->std_set_in_tuner_core, dev->dev_state);
1441 
1442 	au0828_init_tuner(dev);
1443 	i2c_gate_ctrl(dev, 1);
1444 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1445 	i2c_gate_ctrl(dev, 0);
1446 
1447 	dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1448 		t->afc);
1449 
1450 	return 0;
1451 
1452 }
1453 
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * freq)1454 static int vidioc_g_frequency(struct file *file, void *priv,
1455 				struct v4l2_frequency *freq)
1456 {
1457 	struct au0828_dev *dev = video_drvdata(file);
1458 
1459 	if (freq->tuner != 0)
1460 		return -EINVAL;
1461 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1462 		dev->std_set_in_tuner_core, dev->dev_state);
1463 	freq->frequency = dev->ctrl_freq;
1464 	return 0;
1465 }
1466 
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * freq)1467 static int vidioc_s_frequency(struct file *file, void *priv,
1468 				const struct v4l2_frequency *freq)
1469 {
1470 	struct au0828_dev *dev = video_drvdata(file);
1471 	struct v4l2_frequency new_freq = *freq;
1472 
1473 	if (freq->tuner != 0)
1474 		return -EINVAL;
1475 
1476 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1477 		dev->std_set_in_tuner_core, dev->dev_state);
1478 
1479 	au0828_init_tuner(dev);
1480 	i2c_gate_ctrl(dev, 1);
1481 
1482 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1483 	/* Get the actual set (and possibly clamped) frequency */
1484 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1485 	dev->ctrl_freq = new_freq.frequency;
1486 
1487 	i2c_gate_ctrl(dev, 0);
1488 
1489 	au0828_analog_stream_reset(dev);
1490 
1491 	return 0;
1492 }
1493 
1494 
1495 /* RAW VBI ioctls */
1496 
vidioc_g_fmt_vbi_cap(struct file * file,void * priv,struct v4l2_format * format)1497 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1498 				struct v4l2_format *format)
1499 {
1500 	struct au0828_dev *dev = video_drvdata(file);
1501 
1502 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1503 		dev->std_set_in_tuner_core, dev->dev_state);
1504 
1505 	format->fmt.vbi.samples_per_line = dev->vbi_width;
1506 	format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1507 	format->fmt.vbi.offset = 0;
1508 	format->fmt.vbi.flags = 0;
1509 	format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1510 
1511 	format->fmt.vbi.count[0] = dev->vbi_height;
1512 	format->fmt.vbi.count[1] = dev->vbi_height;
1513 	format->fmt.vbi.start[0] = 21;
1514 	format->fmt.vbi.start[1] = 284;
1515 	memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1516 
1517 	return 0;
1518 }
1519 
vidioc_cropcap(struct file * file,void * priv,struct v4l2_cropcap * cc)1520 static int vidioc_cropcap(struct file *file, void *priv,
1521 			  struct v4l2_cropcap *cc)
1522 {
1523 	struct au0828_dev *dev = video_drvdata(file);
1524 
1525 	if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1526 		return -EINVAL;
1527 
1528 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1529 		dev->std_set_in_tuner_core, dev->dev_state);
1530 
1531 	cc->bounds.left = 0;
1532 	cc->bounds.top = 0;
1533 	cc->bounds.width = dev->width;
1534 	cc->bounds.height = dev->height;
1535 
1536 	cc->defrect = cc->bounds;
1537 
1538 	cc->pixelaspect.numerator = 54;
1539 	cc->pixelaspect.denominator = 59;
1540 
1541 	return 0;
1542 }
1543 
1544 #ifdef CONFIG_VIDEO_ADV_DEBUG
vidioc_g_register(struct file * file,void * priv,struct v4l2_dbg_register * reg)1545 static int vidioc_g_register(struct file *file, void *priv,
1546 			     struct v4l2_dbg_register *reg)
1547 {
1548 	struct au0828_dev *dev = video_drvdata(file);
1549 
1550 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1551 		dev->std_set_in_tuner_core, dev->dev_state);
1552 
1553 	reg->val = au0828_read(dev, reg->reg);
1554 	reg->size = 1;
1555 	return 0;
1556 }
1557 
vidioc_s_register(struct file * file,void * priv,const struct v4l2_dbg_register * reg)1558 static int vidioc_s_register(struct file *file, void *priv,
1559 			     const struct v4l2_dbg_register *reg)
1560 {
1561 	struct au0828_dev *dev = video_drvdata(file);
1562 
1563 	dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1564 		dev->std_set_in_tuner_core, dev->dev_state);
1565 
1566 	return au0828_writereg(dev, reg->reg, reg->val);
1567 }
1568 #endif
1569 
vidioc_log_status(struct file * file,void * fh)1570 static int vidioc_log_status(struct file *file, void *fh)
1571 {
1572 	struct video_device *vdev = video_devdata(file);
1573 
1574 	dprintk(1, "%s called\n", __func__);
1575 
1576 	v4l2_ctrl_log_status(file, fh);
1577 	v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1578 	return 0;
1579 }
1580 
au0828_v4l2_suspend(struct au0828_dev * dev)1581 void au0828_v4l2_suspend(struct au0828_dev *dev)
1582 {
1583 	struct urb *urb;
1584 	int i;
1585 
1586 	pr_info("stopping V4L2\n");
1587 
1588 	if (dev->stream_state == STREAM_ON) {
1589 		pr_info("stopping V4L2 active URBs\n");
1590 		au0828_analog_stream_disable(dev);
1591 		/* stop urbs */
1592 		for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1593 			urb = dev->isoc_ctl.urb[i];
1594 			if (urb) {
1595 				if (!irqs_disabled())
1596 					usb_kill_urb(urb);
1597 				else
1598 					usb_unlink_urb(urb);
1599 			}
1600 		}
1601 	}
1602 
1603 	if (dev->vid_timeout_running)
1604 		del_timer_sync(&dev->vid_timeout);
1605 	if (dev->vbi_timeout_running)
1606 		del_timer_sync(&dev->vbi_timeout);
1607 }
1608 
au0828_v4l2_resume(struct au0828_dev * dev)1609 void au0828_v4l2_resume(struct au0828_dev *dev)
1610 {
1611 	int i, rc;
1612 
1613 	pr_info("restarting V4L2\n");
1614 
1615 	if (dev->stream_state == STREAM_ON) {
1616 		au0828_stream_interrupt(dev);
1617 		au0828_init_tuner(dev);
1618 	}
1619 
1620 	if (dev->vid_timeout_running)
1621 		mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1622 	if (dev->vbi_timeout_running)
1623 		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1624 
1625 	/* If we were doing ac97 instead of i2s, it would go here...*/
1626 	au0828_i2s_init(dev);
1627 
1628 	au0828_analog_stream_enable(dev);
1629 
1630 	if (!(dev->stream_state == STREAM_ON)) {
1631 		au0828_analog_stream_reset(dev);
1632 		/* submit urbs */
1633 		for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1634 			rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1635 			if (rc) {
1636 				au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1637 					       i, rc);
1638 				au0828_uninit_isoc(dev);
1639 			}
1640 		}
1641 	}
1642 }
1643 
1644 static struct v4l2_file_operations au0828_v4l_fops = {
1645 	.owner      = THIS_MODULE,
1646 	.open       = au0828_v4l2_open,
1647 	.release    = au0828_v4l2_close,
1648 	.read       = vb2_fop_read,
1649 	.poll       = vb2_fop_poll,
1650 	.mmap       = vb2_fop_mmap,
1651 	.unlocked_ioctl = video_ioctl2,
1652 };
1653 
1654 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1655 	.vidioc_querycap            = vidioc_querycap,
1656 	.vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
1657 	.vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
1658 	.vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
1659 	.vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
1660 	.vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1661 	.vidioc_try_fmt_vbi_cap     = vidioc_g_fmt_vbi_cap,
1662 	.vidioc_s_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1663 	.vidioc_enumaudio           = vidioc_enumaudio,
1664 	.vidioc_g_audio             = vidioc_g_audio,
1665 	.vidioc_s_audio             = vidioc_s_audio,
1666 	.vidioc_cropcap             = vidioc_cropcap,
1667 
1668 	.vidioc_reqbufs             = vb2_ioctl_reqbufs,
1669 	.vidioc_create_bufs         = vb2_ioctl_create_bufs,
1670 	.vidioc_prepare_buf         = vb2_ioctl_prepare_buf,
1671 	.vidioc_querybuf            = vb2_ioctl_querybuf,
1672 	.vidioc_qbuf                = vb2_ioctl_qbuf,
1673 	.vidioc_dqbuf               = vb2_ioctl_dqbuf,
1674 	.vidioc_expbuf               = vb2_ioctl_expbuf,
1675 
1676 	.vidioc_s_std               = vidioc_s_std,
1677 	.vidioc_g_std               = vidioc_g_std,
1678 	.vidioc_enum_input          = vidioc_enum_input,
1679 	.vidioc_g_input             = vidioc_g_input,
1680 	.vidioc_s_input             = vidioc_s_input,
1681 
1682 	.vidioc_streamon            = vb2_ioctl_streamon,
1683 	.vidioc_streamoff           = vb2_ioctl_streamoff,
1684 
1685 	.vidioc_g_tuner             = vidioc_g_tuner,
1686 	.vidioc_s_tuner             = vidioc_s_tuner,
1687 	.vidioc_g_frequency         = vidioc_g_frequency,
1688 	.vidioc_s_frequency         = vidioc_s_frequency,
1689 #ifdef CONFIG_VIDEO_ADV_DEBUG
1690 	.vidioc_g_register          = vidioc_g_register,
1691 	.vidioc_s_register          = vidioc_s_register,
1692 #endif
1693 	.vidioc_log_status	    = vidioc_log_status,
1694 	.vidioc_subscribe_event     = v4l2_ctrl_subscribe_event,
1695 	.vidioc_unsubscribe_event   = v4l2_event_unsubscribe,
1696 };
1697 
1698 static const struct video_device au0828_video_template = {
1699 	.fops                       = &au0828_v4l_fops,
1700 	.release                    = video_device_release_empty,
1701 	.ioctl_ops 		    = &video_ioctl_ops,
1702 	.tvnorms                    = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1703 };
1704 
au0828_vb2_setup(struct au0828_dev * dev)1705 static int au0828_vb2_setup(struct au0828_dev *dev)
1706 {
1707 	int rc;
1708 	struct vb2_queue *q;
1709 
1710 	/* Setup Videobuf2 for Video capture */
1711 	q = &dev->vb_vidq;
1712 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1713 	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1714 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1715 	q->drv_priv = dev;
1716 	q->buf_struct_size = sizeof(struct au0828_buffer);
1717 	q->ops = &au0828_video_qops;
1718 	q->mem_ops = &vb2_vmalloc_memops;
1719 
1720 	rc = vb2_queue_init(q);
1721 	if (rc < 0)
1722 		return rc;
1723 
1724 	/* Setup Videobuf2 for VBI capture */
1725 	q = &dev->vb_vbiq;
1726 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1727 	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1728 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1729 	q->drv_priv = dev;
1730 	q->buf_struct_size = sizeof(struct au0828_buffer);
1731 	q->ops = &au0828_vbi_qops;
1732 	q->mem_ops = &vb2_vmalloc_memops;
1733 
1734 	rc = vb2_queue_init(q);
1735 	if (rc < 0)
1736 		return rc;
1737 
1738 	return 0;
1739 }
1740 
1741 /**************************************************************************/
1742 
au0828_analog_register(struct au0828_dev * dev,struct usb_interface * interface)1743 int au0828_analog_register(struct au0828_dev *dev,
1744 			   struct usb_interface *interface)
1745 {
1746 	int retval = -ENOMEM;
1747 	struct usb_host_interface *iface_desc;
1748 	struct usb_endpoint_descriptor *endpoint;
1749 	int i, ret;
1750 
1751 	dprintk(1, "au0828_analog_register called for intf#%d!\n",
1752 		interface->cur_altsetting->desc.bInterfaceNumber);
1753 
1754 	/* set au0828 usb interface0 to as5 */
1755 	retval = usb_set_interface(dev->usbdev,
1756 			interface->cur_altsetting->desc.bInterfaceNumber, 5);
1757 	if (retval != 0) {
1758 		pr_info("Failure setting usb interface0 to as5\n");
1759 		return retval;
1760 	}
1761 
1762 	/* Figure out which endpoint has the isoc interface */
1763 	iface_desc = interface->cur_altsetting;
1764 	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1765 		endpoint = &iface_desc->endpoint[i].desc;
1766 		if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1767 		     == USB_DIR_IN) &&
1768 		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1769 		     == USB_ENDPOINT_XFER_ISOC)) {
1770 
1771 			/* we find our isoc in endpoint */
1772 			u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1773 			dev->max_pkt_size = (tmp & 0x07ff) *
1774 				(((tmp & 0x1800) >> 11) + 1);
1775 			dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1776 			dprintk(1,
1777 				"Found isoc endpoint 0x%02x, max size = %d\n",
1778 				dev->isoc_in_endpointaddr, dev->max_pkt_size);
1779 		}
1780 	}
1781 	if (!(dev->isoc_in_endpointaddr)) {
1782 		pr_info("Could not locate isoc endpoint\n");
1783 		return -ENODEV;
1784 	}
1785 
1786 	init_waitqueue_head(&dev->open);
1787 	spin_lock_init(&dev->slock);
1788 
1789 	/* init video dma queues */
1790 	INIT_LIST_HEAD(&dev->vidq.active);
1791 	INIT_LIST_HEAD(&dev->vbiq.active);
1792 
1793 	setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
1794 		    (unsigned long)dev);
1795 	setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
1796 		    (unsigned long)dev);
1797 
1798 	dev->width = NTSC_STD_W;
1799 	dev->height = NTSC_STD_H;
1800 	dev->field_size = dev->width * dev->height;
1801 	dev->frame_size = dev->field_size << 1;
1802 	dev->bytesperline = dev->width << 1;
1803 	dev->vbi_width = 720;
1804 	dev->vbi_height = 1;
1805 	dev->ctrl_ainput = 0;
1806 	dev->ctrl_freq = 960;
1807 	dev->std = V4L2_STD_NTSC_M;
1808 	au0828_s_input(dev, 0);
1809 
1810 	mutex_init(&dev->vb_queue_lock);
1811 	mutex_init(&dev->vb_vbi_queue_lock);
1812 
1813 	/* Fill the video capture device struct */
1814 	dev->vdev = au0828_video_template;
1815 	dev->vdev.v4l2_dev = &dev->v4l2_dev;
1816 	dev->vdev.lock = &dev->lock;
1817 	dev->vdev.queue = &dev->vb_vidq;
1818 	dev->vdev.queue->lock = &dev->vb_queue_lock;
1819 	strcpy(dev->vdev.name, "au0828a video");
1820 
1821 	/* Setup the VBI device */
1822 	dev->vbi_dev = au0828_video_template;
1823 	dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1824 	dev->vbi_dev.lock = &dev->lock;
1825 	dev->vbi_dev.queue = &dev->vb_vbiq;
1826 	dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1827 	strcpy(dev->vbi_dev.name, "au0828a vbi");
1828 
1829 	/* initialize videobuf2 stuff */
1830 	retval = au0828_vb2_setup(dev);
1831 	if (retval != 0) {
1832 		dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
1833 			retval);
1834 		return -ENODEV;
1835 	}
1836 
1837 	/* Register the v4l2 device */
1838 	video_set_drvdata(&dev->vdev, dev);
1839 	retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1840 	if (retval != 0) {
1841 		dprintk(1, "unable to register video device (error = %d).\n",
1842 			retval);
1843 		ret = -ENODEV;
1844 		goto err_reg_vdev;
1845 	}
1846 
1847 	/* Register the vbi device */
1848 	video_set_drvdata(&dev->vbi_dev, dev);
1849 	retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
1850 	if (retval != 0) {
1851 		dprintk(1, "unable to register vbi device (error = %d).\n",
1852 			retval);
1853 		ret = -ENODEV;
1854 		goto err_reg_vbi_dev;
1855 	}
1856 
1857 	dprintk(1, "%s completed!\n", __func__);
1858 
1859 	return 0;
1860 
1861 err_reg_vbi_dev:
1862 	video_unregister_device(&dev->vdev);
1863 err_reg_vdev:
1864 	vb2_queue_release(&dev->vb_vidq);
1865 	vb2_queue_release(&dev->vb_vbiq);
1866 	return ret;
1867 }
1868 
1869