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