8a7a54750aeed2b5ae139d480b393a747cae1769
[deliverable/linux.git] / drivers / media / usb / au0828 / au0828-video.c
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
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
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
105 static 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 * IRQ callback, called by URB callback
122 */
123 static 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: /* success */
132 case -ETIMEDOUT: /* NAK */
133 break;
134 case -ECONNRESET: /* kill */
135 case -ENOENT:
136 case -ESHUTDOWN:
137 au0828_isocdbg("au0828_irq_callback called: status kill\n");
138 return;
139 default: /* unknown error */
140 au0828_isocdbg("urb completition error %d.\n", urb->status);
141 break;
142 }
143
144 /* Copy data from URB */
145 spin_lock_irqsave(&dev->slock, flags);
146 dev->isoc_ctl.isoc_copy(dev, urb);
147 spin_unlock_irqrestore(&dev->slock, flags);
148
149 /* Reset urb buffers */
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 * Stop and Deallocate URBs
166 */
167 static 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 * Allocate URBs and start IRQ
207 */
208 static 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 /* De-allocates all pending stuff */
222 au0828_uninit_isoc(dev);
223
224 dev->isoc_ctl.isoc_copy = isoc_copy;
225 dev->isoc_ctl.num_bufs = num_bufs;
226
227 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
228 if (!dev->isoc_ctl.urb) {
229 au0828_isocdbg("cannot alloc memory for usb buffers\n");
230 return -ENOMEM;
231 }
232
233 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
234 GFP_KERNEL);
235 if (!dev->isoc_ctl.transfer_buffer) {
236 au0828_isocdbg("cannot allocate memory for usb transfer\n");
237 kfree(dev->isoc_ctl.urb);
238 return -ENOMEM;
239 }
240
241 dev->isoc_ctl.max_pkt_size = max_pkt_size;
242 dev->isoc_ctl.buf = NULL;
243
244 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
245
246 /* allocate urbs and transfer buffers */
247 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
248 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
249 if (!urb) {
250 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
251 au0828_uninit_isoc(dev);
252 return -ENOMEM;
253 }
254 dev->isoc_ctl.urb[i] = urb;
255
256 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
257 sb_size, GFP_KERNEL, &urb->transfer_dma);
258 if (!dev->isoc_ctl.transfer_buffer[i]) {
259 printk("unable to allocate %i bytes for transfer"
260 " buffer %i%s\n",
261 sb_size, i,
262 in_interrupt() ? " while in int" : "");
263 au0828_uninit_isoc(dev);
264 return -ENOMEM;
265 }
266 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
267
268 pipe = usb_rcvisocpipe(dev->usbdev,
269 dev->isoc_in_endpointaddr),
270
271 usb_fill_int_urb(urb, dev->usbdev, pipe,
272 dev->isoc_ctl.transfer_buffer[i], sb_size,
273 au0828_irq_callback, dma_q, 1);
274
275 urb->number_of_packets = max_packets;
276 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
277
278 k = 0;
279 for (j = 0; j < max_packets; j++) {
280 urb->iso_frame_desc[j].offset = k;
281 urb->iso_frame_desc[j].length =
282 dev->isoc_ctl.max_pkt_size;
283 k += dev->isoc_ctl.max_pkt_size;
284 }
285 }
286
287 init_waitqueue_head(&dma_q->wq);
288
289 /* submit urbs and enables IRQ */
290 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
291 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
292 if (rc) {
293 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
294 i, rc);
295 au0828_uninit_isoc(dev);
296 return rc;
297 }
298 }
299
300 return 0;
301 }
302
303 /*
304 * Announces that a buffer were filled and request the next
305 */
306 static inline void buffer_filled(struct au0828_dev *dev,
307 struct au0828_dmaqueue *dma_q,
308 struct au0828_buffer *buf)
309 {
310 /* Advice that buffer was filled */
311 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
312
313 buf->vb.state = VIDEOBUF_DONE;
314 buf->vb.field_count++;
315 v4l2_get_timestamp(&buf->vb.ts);
316
317 dev->isoc_ctl.buf = NULL;
318
319 list_del(&buf->vb.queue);
320 wake_up(&buf->vb.done);
321 }
322
323 static inline void vbi_buffer_filled(struct au0828_dev *dev,
324 struct au0828_dmaqueue *dma_q,
325 struct au0828_buffer *buf)
326 {
327 /* Advice that buffer was filled */
328 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
329
330 buf->vb.state = VIDEOBUF_DONE;
331 buf->vb.field_count++;
332 v4l2_get_timestamp(&buf->vb.ts);
333
334 dev->isoc_ctl.vbi_buf = NULL;
335
336 list_del(&buf->vb.queue);
337 wake_up(&buf->vb.done);
338 }
339
340 /*
341 * Identify the buffer header type and properly handles
342 */
343 static void au0828_copy_video(struct au0828_dev *dev,
344 struct au0828_dmaqueue *dma_q,
345 struct au0828_buffer *buf,
346 unsigned char *p,
347 unsigned char *outp, unsigned long len)
348 {
349 void *fieldstart, *startwrite, *startread;
350 int linesdone, currlinedone, offset, lencopy, remain;
351 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
352
353 if (len == 0)
354 return;
355
356 if (dma_q->pos + len > buf->vb.size)
357 len = buf->vb.size - dma_q->pos;
358
359 startread = p;
360 remain = len;
361
362 /* Interlaces frame */
363 if (buf->top_field)
364 fieldstart = outp;
365 else
366 fieldstart = outp + bytesperline;
367
368 linesdone = dma_q->pos / bytesperline;
369 currlinedone = dma_q->pos % bytesperline;
370 offset = linesdone * bytesperline * 2 + currlinedone;
371 startwrite = fieldstart + offset;
372 lencopy = bytesperline - currlinedone;
373 lencopy = lencopy > remain ? remain : lencopy;
374
375 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
376 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
377 ((char *)startwrite + lencopy) -
378 ((char *)outp + buf->vb.size));
379 remain = (char *)outp + buf->vb.size - (char *)startwrite;
380 lencopy = remain;
381 }
382 if (lencopy <= 0)
383 return;
384 memcpy(startwrite, startread, lencopy);
385
386 remain -= lencopy;
387
388 while (remain > 0) {
389 startwrite += lencopy + bytesperline;
390 startread += lencopy;
391 if (bytesperline > remain)
392 lencopy = remain;
393 else
394 lencopy = bytesperline;
395
396 if ((char *)startwrite + lencopy > (char *)outp +
397 buf->vb.size) {
398 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
399 ((char *)startwrite + lencopy) -
400 ((char *)outp + buf->vb.size));
401 lencopy = remain = (char *)outp + buf->vb.size -
402 (char *)startwrite;
403 }
404 if (lencopy <= 0)
405 break;
406
407 memcpy(startwrite, startread, lencopy);
408
409 remain -= lencopy;
410 }
411
412 if (offset > 1440) {
413 /* We have enough data to check for greenscreen */
414 if (outp[0] < 0x60 && outp[1440] < 0x60)
415 dev->greenscreen_detected = 1;
416 }
417
418 dma_q->pos += len;
419 }
420
421 /*
422 * video-buf generic routine to get the next available buffer
423 */
424 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
425 struct au0828_buffer **buf)
426 {
427 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
428
429 if (list_empty(&dma_q->active)) {
430 au0828_isocdbg("No active queue to serve\n");
431 dev->isoc_ctl.buf = NULL;
432 *buf = NULL;
433 return;
434 }
435
436 /* Get the next buffer */
437 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
438 dev->isoc_ctl.buf = *buf;
439
440 return;
441 }
442
443 static void au0828_copy_vbi(struct au0828_dev *dev,
444 struct au0828_dmaqueue *dma_q,
445 struct au0828_buffer *buf,
446 unsigned char *p,
447 unsigned char *outp, unsigned long len)
448 {
449 unsigned char *startwrite, *startread;
450 int bytesperline;
451 int i, j = 0;
452
453 if (dev == NULL) {
454 au0828_isocdbg("dev is null\n");
455 return;
456 }
457
458 if (dma_q == NULL) {
459 au0828_isocdbg("dma_q is null\n");
460 return;
461 }
462 if (buf == NULL)
463 return;
464 if (p == NULL) {
465 au0828_isocdbg("p is null\n");
466 return;
467 }
468 if (outp == NULL) {
469 au0828_isocdbg("outp is null\n");
470 return;
471 }
472
473 bytesperline = dev->vbi_width;
474
475 if (dma_q->pos + len > buf->vb.size)
476 len = buf->vb.size - dma_q->pos;
477
478 startread = p;
479 startwrite = outp + (dma_q->pos / 2);
480
481 /* Make sure the bottom field populates the second half of the frame */
482 if (buf->top_field == 0)
483 startwrite += bytesperline * dev->vbi_height;
484
485 for (i = 0; i < len; i += 2)
486 startwrite[j++] = startread[i+1];
487
488 dma_q->pos += len;
489 }
490
491
492 /*
493 * video-buf generic routine to get the next available VBI buffer
494 */
495 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
496 struct au0828_buffer **buf)
497 {
498 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
499 char *outp;
500
501 if (list_empty(&dma_q->active)) {
502 au0828_isocdbg("No active queue to serve\n");
503 dev->isoc_ctl.vbi_buf = NULL;
504 *buf = NULL;
505 return;
506 }
507
508 /* Get the next buffer */
509 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
510 /* Cleans up buffer - Useful for testing for frame/URB loss */
511 outp = videobuf_to_vmalloc(&(*buf)->vb);
512 memset(outp, 0x00, (*buf)->vb.size);
513
514 dev->isoc_ctl.vbi_buf = *buf;
515
516 return;
517 }
518
519 /*
520 * Controls the isoc copy of each urb packet
521 */
522 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
523 {
524 struct au0828_buffer *buf;
525 struct au0828_buffer *vbi_buf;
526 struct au0828_dmaqueue *dma_q = urb->context;
527 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
528 unsigned char *outp = NULL;
529 unsigned char *vbioutp = NULL;
530 int i, len = 0, rc = 1;
531 unsigned char *p;
532 unsigned char fbyte;
533 unsigned int vbi_field_size;
534 unsigned int remain, lencopy;
535
536 if (!dev)
537 return 0;
538
539 if ((dev->dev_state & DEV_DISCONNECTED) ||
540 (dev->dev_state & DEV_MISCONFIGURED))
541 return 0;
542
543 if (urb->status < 0) {
544 print_err_status(dev, -1, urb->status);
545 if (urb->status == -ENOENT)
546 return 0;
547 }
548
549 buf = dev->isoc_ctl.buf;
550 if (buf != NULL)
551 outp = videobuf_to_vmalloc(&buf->vb);
552
553 vbi_buf = dev->isoc_ctl.vbi_buf;
554 if (vbi_buf != NULL)
555 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
556
557 for (i = 0; i < urb->number_of_packets; i++) {
558 int status = urb->iso_frame_desc[i].status;
559
560 if (status < 0) {
561 print_err_status(dev, i, status);
562 if (urb->iso_frame_desc[i].status != -EPROTO)
563 continue;
564 }
565
566 if (urb->iso_frame_desc[i].actual_length <= 0)
567 continue;
568
569 if (urb->iso_frame_desc[i].actual_length >
570 dev->max_pkt_size) {
571 au0828_isocdbg("packet bigger than packet size");
572 continue;
573 }
574
575 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
576 fbyte = p[0];
577 len = urb->iso_frame_desc[i].actual_length - 4;
578 p += 4;
579
580 if (fbyte & 0x80) {
581 len -= 4;
582 p += 4;
583 au0828_isocdbg("Video frame %s\n",
584 (fbyte & 0x40) ? "odd" : "even");
585 if (fbyte & 0x40) {
586 /* VBI */
587 if (vbi_buf != NULL)
588 vbi_buffer_filled(dev,
589 vbi_dma_q,
590 vbi_buf);
591 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
592 if (vbi_buf == NULL)
593 vbioutp = NULL;
594 else
595 vbioutp = videobuf_to_vmalloc(
596 &vbi_buf->vb);
597
598 /* Video */
599 if (buf != NULL)
600 buffer_filled(dev, dma_q, buf);
601 get_next_buf(dma_q, &buf);
602 if (buf == NULL)
603 outp = NULL;
604 else
605 outp = videobuf_to_vmalloc(&buf->vb);
606
607 /* As long as isoc traffic is arriving, keep
608 resetting the timer */
609 if (dev->vid_timeout_running)
610 mod_timer(&dev->vid_timeout,
611 jiffies + (HZ / 10));
612 if (dev->vbi_timeout_running)
613 mod_timer(&dev->vbi_timeout,
614 jiffies + (HZ / 10));
615 }
616
617 if (buf != NULL) {
618 if (fbyte & 0x40)
619 buf->top_field = 1;
620 else
621 buf->top_field = 0;
622 }
623
624 if (vbi_buf != NULL) {
625 if (fbyte & 0x40)
626 vbi_buf->top_field = 1;
627 else
628 vbi_buf->top_field = 0;
629 }
630
631 dev->vbi_read = 0;
632 vbi_dma_q->pos = 0;
633 dma_q->pos = 0;
634 }
635
636 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
637 if (dev->vbi_read < vbi_field_size) {
638 remain = vbi_field_size - dev->vbi_read;
639 if (len < remain)
640 lencopy = len;
641 else
642 lencopy = remain;
643
644 if (vbi_buf != NULL)
645 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
646 vbioutp, len);
647
648 len -= lencopy;
649 p += lencopy;
650 dev->vbi_read += lencopy;
651 }
652
653 if (dev->vbi_read >= vbi_field_size && buf != NULL)
654 au0828_copy_video(dev, dma_q, buf, p, outp, len);
655 }
656 return rc;
657 }
658
659 static int
660 buffer_setup(struct videobuf_queue *vq, unsigned int *count,
661 unsigned int *size)
662 {
663 struct au0828_fh *fh = vq->priv_data;
664 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
665
666 if (0 == *count)
667 *count = AU0828_DEF_BUF;
668
669 if (*count < AU0828_MIN_BUF)
670 *count = AU0828_MIN_BUF;
671 return 0;
672 }
673
674 /* This is called *without* dev->slock held; please keep it that way */
675 static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
676 {
677 struct au0828_fh *fh = vq->priv_data;
678 struct au0828_dev *dev = fh->dev;
679 unsigned long flags = 0;
680 if (in_interrupt())
681 BUG();
682
683 /* We used to wait for the buffer to finish here, but this didn't work
684 because, as we were keeping the state as VIDEOBUF_QUEUED,
685 videobuf_queue_cancel marked it as finished for us.
686 (Also, it could wedge forever if the hardware was misconfigured.)
687
688 This should be safe; by the time we get here, the buffer isn't
689 queued anymore. If we ever start marking the buffers as
690 VIDEOBUF_ACTIVE, it won't be, though.
691 */
692 spin_lock_irqsave(&dev->slock, flags);
693 if (dev->isoc_ctl.buf == buf)
694 dev->isoc_ctl.buf = NULL;
695 spin_unlock_irqrestore(&dev->slock, flags);
696
697 videobuf_vmalloc_free(&buf->vb);
698 buf->vb.state = VIDEOBUF_NEEDS_INIT;
699 }
700
701 static int
702 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
703 enum v4l2_field field)
704 {
705 struct au0828_fh *fh = vq->priv_data;
706 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
707 struct au0828_dev *dev = fh->dev;
708 int rc = 0, urb_init = 0;
709
710 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
711
712 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
713 return -EINVAL;
714
715 buf->vb.width = dev->width;
716 buf->vb.height = dev->height;
717 buf->vb.field = field;
718
719 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
720 rc = videobuf_iolock(vq, &buf->vb, NULL);
721 if (rc < 0) {
722 pr_info("videobuf_iolock failed\n");
723 goto fail;
724 }
725 }
726
727 if (!dev->isoc_ctl.num_bufs)
728 urb_init = 1;
729
730 if (urb_init) {
731 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
732 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
733 au0828_isoc_copy);
734 if (rc < 0) {
735 pr_info("au0828_init_isoc failed\n");
736 goto fail;
737 }
738 }
739
740 buf->vb.state = VIDEOBUF_PREPARED;
741 return 0;
742
743 fail:
744 free_buffer(vq, buf);
745 return rc;
746 }
747
748 static void
749 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
750 {
751 struct au0828_buffer *buf = container_of(vb,
752 struct au0828_buffer,
753 vb);
754 struct au0828_fh *fh = vq->priv_data;
755 struct au0828_dev *dev = fh->dev;
756 struct au0828_dmaqueue *vidq = &dev->vidq;
757
758 buf->vb.state = VIDEOBUF_QUEUED;
759 list_add_tail(&buf->vb.queue, &vidq->active);
760 }
761
762 static void buffer_release(struct videobuf_queue *vq,
763 struct videobuf_buffer *vb)
764 {
765 struct au0828_buffer *buf = container_of(vb,
766 struct au0828_buffer,
767 vb);
768
769 free_buffer(vq, buf);
770 }
771
772 static struct videobuf_queue_ops au0828_video_qops = {
773 .buf_setup = buffer_setup,
774 .buf_prepare = buffer_prepare,
775 .buf_queue = buffer_queue,
776 .buf_release = buffer_release,
777 };
778
779 /* ------------------------------------------------------------------
780 V4L2 interface
781 ------------------------------------------------------------------*/
782
783 static int au0828_i2s_init(struct au0828_dev *dev)
784 {
785 /* Enable i2s mode */
786 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
787 return 0;
788 }
789
790 /*
791 * Auvitek au0828 analog stream enable
792 */
793 static int au0828_analog_stream_enable(struct au0828_dev *d)
794 {
795 struct usb_interface *iface;
796 int ret, h, w;
797
798 dprintk(1, "au0828_analog_stream_enable called\n");
799
800 iface = usb_ifnum_to_if(d->usbdev, 0);
801 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
802 dprintk(1, "Changing intf#0 to alt 5\n");
803 /* set au0828 interface0 to AS5 here again */
804 ret = usb_set_interface(d->usbdev, 0, 5);
805 if (ret < 0) {
806 pr_info("Au0828 can't set alt setting to 5!\n");
807 return -EBUSY;
808 }
809 }
810
811 h = d->height / 2 + 2;
812 w = d->width * 2;
813
814 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
815 au0828_writereg(d, 0x106, 0x00);
816 /* set x position */
817 au0828_writereg(d, 0x110, 0x00);
818 au0828_writereg(d, 0x111, 0x00);
819 au0828_writereg(d, 0x114, w & 0xff);
820 au0828_writereg(d, 0x115, w >> 8);
821 /* set y position */
822 au0828_writereg(d, 0x112, 0x00);
823 au0828_writereg(d, 0x113, 0x00);
824 au0828_writereg(d, 0x116, h & 0xff);
825 au0828_writereg(d, 0x117, h >> 8);
826 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
827
828 return 0;
829 }
830
831 int au0828_analog_stream_disable(struct au0828_dev *d)
832 {
833 dprintk(1, "au0828_analog_stream_disable called\n");
834 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
835 return 0;
836 }
837
838 static void au0828_analog_stream_reset(struct au0828_dev *dev)
839 {
840 dprintk(1, "au0828_analog_stream_reset called\n");
841 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
842 mdelay(30);
843 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
844 }
845
846 /*
847 * Some operations needs to stop current streaming
848 */
849 static int au0828_stream_interrupt(struct au0828_dev *dev)
850 {
851 int ret = 0;
852
853 dev->stream_state = STREAM_INTERRUPT;
854 if (dev->dev_state == DEV_DISCONNECTED)
855 return -ENODEV;
856 else if (ret) {
857 dev->dev_state = DEV_MISCONFIGURED;
858 dprintk(1, "%s device is misconfigured!\n", __func__);
859 return ret;
860 }
861 return 0;
862 }
863
864 /*
865 * au0828_release_resources
866 * unregister v4l2 devices
867 */
868 void au0828_analog_unregister(struct au0828_dev *dev)
869 {
870 dprintk(1, "au0828_release_resources called\n");
871 mutex_lock(&au0828_sysfs_lock);
872
873 if (dev->vdev)
874 video_unregister_device(dev->vdev);
875 if (dev->vbi_dev)
876 video_unregister_device(dev->vbi_dev);
877
878 mutex_unlock(&au0828_sysfs_lock);
879 }
880
881
882 /* Usage lock check functions */
883 static int res_get(struct au0828_fh *fh, unsigned int bit)
884 {
885 struct au0828_dev *dev = fh->dev;
886
887 if (fh->resources & bit)
888 /* have it already allocated */
889 return 1;
890
891 /* is it free? */
892 if (dev->resources & bit) {
893 /* no, someone else uses it */
894 return 0;
895 }
896 /* it's free, grab it */
897 fh->resources |= bit;
898 dev->resources |= bit;
899 dprintk(1, "res: get %d\n", bit);
900
901 return 1;
902 }
903
904 static int res_check(struct au0828_fh *fh, unsigned int bit)
905 {
906 return fh->resources & bit;
907 }
908
909 static int res_locked(struct au0828_dev *dev, unsigned int bit)
910 {
911 return dev->resources & bit;
912 }
913
914 static void res_free(struct au0828_fh *fh, unsigned int bits)
915 {
916 struct au0828_dev *dev = fh->dev;
917
918 BUG_ON((fh->resources & bits) != bits);
919
920 fh->resources &= ~bits;
921 dev->resources &= ~bits;
922 dprintk(1, "res: put %d\n", bits);
923 }
924
925 static int get_ressource(struct au0828_fh *fh)
926 {
927 switch (fh->type) {
928 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
929 return AU0828_RESOURCE_VIDEO;
930 case V4L2_BUF_TYPE_VBI_CAPTURE:
931 return AU0828_RESOURCE_VBI;
932 default:
933 BUG();
934 return 0;
935 }
936 }
937
938 /* This function ensures that video frames continue to be delivered even if
939 the ITU-656 input isn't receiving any data (thereby preventing applications
940 such as tvtime from hanging) */
941 static void au0828_vid_buffer_timeout(unsigned long data)
942 {
943 struct au0828_dev *dev = (struct au0828_dev *) data;
944 struct au0828_dmaqueue *dma_q = &dev->vidq;
945 struct au0828_buffer *buf;
946 unsigned char *vid_data;
947 unsigned long flags = 0;
948
949 spin_lock_irqsave(&dev->slock, flags);
950
951 buf = dev->isoc_ctl.buf;
952 if (buf != NULL) {
953 vid_data = videobuf_to_vmalloc(&buf->vb);
954 memset(vid_data, 0x00, buf->vb.size); /* Blank green frame */
955 buffer_filled(dev, dma_q, buf);
956 }
957 get_next_buf(dma_q, &buf);
958
959 if (dev->vid_timeout_running == 1)
960 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
961
962 spin_unlock_irqrestore(&dev->slock, flags);
963 }
964
965 static void au0828_vbi_buffer_timeout(unsigned long data)
966 {
967 struct au0828_dev *dev = (struct au0828_dev *) data;
968 struct au0828_dmaqueue *dma_q = &dev->vbiq;
969 struct au0828_buffer *buf;
970 unsigned char *vbi_data;
971 unsigned long flags = 0;
972
973 spin_lock_irqsave(&dev->slock, flags);
974
975 buf = dev->isoc_ctl.vbi_buf;
976 if (buf != NULL) {
977 vbi_data = videobuf_to_vmalloc(&buf->vb);
978 memset(vbi_data, 0x00, buf->vb.size);
979 vbi_buffer_filled(dev, dma_q, buf);
980 }
981 vbi_get_next_buf(dma_q, &buf);
982
983 if (dev->vbi_timeout_running == 1)
984 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
985 spin_unlock_irqrestore(&dev->slock, flags);
986 }
987
988
989 static int au0828_v4l2_open(struct file *filp)
990 {
991 int ret = 0;
992 struct video_device *vdev = video_devdata(filp);
993 struct au0828_dev *dev = video_drvdata(filp);
994 struct au0828_fh *fh;
995 int type;
996
997 switch (vdev->vfl_type) {
998 case VFL_TYPE_GRABBER:
999 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1000 break;
1001 case VFL_TYPE_VBI:
1002 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1003 break;
1004 default:
1005 return -EINVAL;
1006 }
1007
1008 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
1009 if (NULL == fh) {
1010 dprintk(1, "Failed allocate au0828_fh struct!\n");
1011 return -ENOMEM;
1012 }
1013
1014 fh->type = type;
1015 fh->dev = dev;
1016 v4l2_fh_init(&fh->fh, vdev);
1017 filp->private_data = fh;
1018
1019 if (mutex_lock_interruptible(&dev->lock)) {
1020 kfree(fh);
1021 return -ERESTARTSYS;
1022 }
1023 if (dev->users == 0) {
1024 au0828_analog_stream_enable(dev);
1025 au0828_analog_stream_reset(dev);
1026
1027 /* If we were doing ac97 instead of i2s, it would go here...*/
1028 au0828_i2s_init(dev);
1029
1030 dev->stream_state = STREAM_OFF;
1031 dev->dev_state |= DEV_INITIALIZED;
1032 }
1033
1034 dev->users++;
1035 mutex_unlock(&dev->lock);
1036
1037 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
1038 NULL, &dev->slock,
1039 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1040 V4L2_FIELD_INTERLACED,
1041 sizeof(struct au0828_buffer), fh,
1042 &dev->lock);
1043
1044 /* VBI Setup */
1045 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
1046 NULL, &dev->slock,
1047 V4L2_BUF_TYPE_VBI_CAPTURE,
1048 V4L2_FIELD_SEQ_TB,
1049 sizeof(struct au0828_buffer), fh,
1050 &dev->lock);
1051 v4l2_fh_add(&fh->fh);
1052 return ret;
1053 }
1054
1055 static int au0828_v4l2_close(struct file *filp)
1056 {
1057 int ret;
1058 struct au0828_fh *fh = filp->private_data;
1059 struct au0828_dev *dev = fh->dev;
1060
1061 v4l2_fh_del(&fh->fh);
1062 v4l2_fh_exit(&fh->fh);
1063 mutex_lock(&dev->lock);
1064 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1065 /* Cancel timeout thread in case they didn't call streamoff */
1066 dev->vid_timeout_running = 0;
1067 del_timer_sync(&dev->vid_timeout);
1068
1069 videobuf_stop(&fh->vb_vidq);
1070 res_free(fh, AU0828_RESOURCE_VIDEO);
1071 }
1072
1073 if (res_check(fh, AU0828_RESOURCE_VBI)) {
1074 /* Cancel timeout thread in case they didn't call streamoff */
1075 dev->vbi_timeout_running = 0;
1076 del_timer_sync(&dev->vbi_timeout);
1077
1078 videobuf_stop(&fh->vb_vbiq);
1079 res_free(fh, AU0828_RESOURCE_VBI);
1080 }
1081
1082 if (dev->users == 1 && video_is_registered(video_devdata(filp))) {
1083 au0828_analog_stream_disable(dev);
1084
1085 au0828_uninit_isoc(dev);
1086
1087 /* Save some power by putting tuner to sleep */
1088 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1089 dev->std_set_in_tuner_core = 0;
1090
1091 /* When close the device, set the usb intf0 into alt0 to free
1092 USB bandwidth */
1093 ret = usb_set_interface(dev->usbdev, 0, 0);
1094 if (ret < 0)
1095 pr_info("Au0828 can't set alternate to 0!\n");
1096 }
1097 mutex_unlock(&dev->lock);
1098
1099 videobuf_mmap_free(&fh->vb_vidq);
1100 videobuf_mmap_free(&fh->vb_vbiq);
1101 kfree(fh);
1102 dev->users--;
1103 wake_up_interruptible_nr(&dev->open, 1);
1104 return 0;
1105 }
1106
1107 /* Must be called with dev->lock held */
1108 static void au0828_init_tuner(struct au0828_dev *dev)
1109 {
1110 struct v4l2_frequency f = {
1111 .frequency = dev->ctrl_freq,
1112 .type = V4L2_TUNER_ANALOG_TV,
1113 };
1114
1115 if (dev->std_set_in_tuner_core)
1116 return;
1117 dev->std_set_in_tuner_core = 1;
1118 i2c_gate_ctrl(dev, 1);
1119 /* If we've never sent the standard in tuner core, do so now.
1120 We don't do this at device probe because we don't want to
1121 incur the cost of a firmware load */
1122 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1123 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1124 i2c_gate_ctrl(dev, 0);
1125 }
1126
1127 static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
1128 size_t count, loff_t *pos)
1129 {
1130 struct au0828_fh *fh = filp->private_data;
1131 struct au0828_dev *dev = fh->dev;
1132 int rc;
1133
1134 rc = check_dev(dev);
1135 if (rc < 0)
1136 return rc;
1137
1138 if (mutex_lock_interruptible(&dev->lock))
1139 return -ERESTARTSYS;
1140 au0828_init_tuner(dev);
1141 mutex_unlock(&dev->lock);
1142
1143 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1144 if (res_locked(dev, AU0828_RESOURCE_VIDEO))
1145 return -EBUSY;
1146
1147 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1148 filp->f_flags & O_NONBLOCK);
1149 }
1150
1151 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1152 if (!res_get(fh, AU0828_RESOURCE_VBI))
1153 return -EBUSY;
1154
1155 if (dev->vbi_timeout_running == 0) {
1156 /* Handle case where caller tries to read without
1157 calling streamon first */
1158 dev->vbi_timeout_running = 1;
1159 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1160 }
1161
1162 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1163 filp->f_flags & O_NONBLOCK);
1164 }
1165
1166 return 0;
1167 }
1168
1169 static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
1170 {
1171 struct au0828_fh *fh = filp->private_data;
1172 struct au0828_dev *dev = fh->dev;
1173 unsigned long req_events = poll_requested_events(wait);
1174 unsigned int res;
1175
1176 if (check_dev(dev) < 0)
1177 return POLLERR;
1178
1179 res = v4l2_ctrl_poll(filp, wait);
1180 if (!(req_events & (POLLIN | POLLRDNORM)))
1181 return res;
1182
1183 if (mutex_lock_interruptible(&dev->lock))
1184 return -ERESTARTSYS;
1185 au0828_init_tuner(dev);
1186 mutex_unlock(&dev->lock);
1187
1188 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1189 if (!res_get(fh, AU0828_RESOURCE_VIDEO))
1190 return POLLERR;
1191 return res | videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1192 }
1193 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1194 if (!res_get(fh, AU0828_RESOURCE_VBI))
1195 return POLLERR;
1196 return res | videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1197 }
1198 return POLLERR;
1199 }
1200
1201 static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1202 {
1203 struct au0828_fh *fh = filp->private_data;
1204 struct au0828_dev *dev = fh->dev;
1205 int rc;
1206
1207 rc = check_dev(dev);
1208 if (rc < 0)
1209 return rc;
1210
1211 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1212 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1213 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1214 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
1215
1216 return rc;
1217 }
1218
1219 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1220 struct v4l2_format *format)
1221 {
1222 int ret;
1223 int width = format->fmt.pix.width;
1224 int height = format->fmt.pix.height;
1225
1226 /* If they are demanding a format other than the one we support,
1227 bail out (tvtime asks for UYVY and then retries with YUYV) */
1228 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1229 return -EINVAL;
1230
1231 /* format->fmt.pix.width only support 720 and height 480 */
1232 if (width != 720)
1233 width = 720;
1234 if (height != 480)
1235 height = 480;
1236
1237 format->fmt.pix.width = width;
1238 format->fmt.pix.height = height;
1239 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1240 format->fmt.pix.bytesperline = width * 2;
1241 format->fmt.pix.sizeimage = width * height * 2;
1242 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1243 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1244 format->fmt.pix.priv = 0;
1245
1246 if (cmd == VIDIOC_TRY_FMT)
1247 return 0;
1248
1249 /* maybe set new image format, driver current only support 720*480 */
1250 dev->width = width;
1251 dev->height = height;
1252 dev->frame_size = width * height * 2;
1253 dev->field_size = width * height;
1254 dev->bytesperline = width * 2;
1255
1256 if (dev->stream_state == STREAM_ON) {
1257 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1258 ret = au0828_stream_interrupt(dev);
1259 if (ret != 0) {
1260 dprintk(1, "error interrupting video stream!\n");
1261 return ret;
1262 }
1263 }
1264
1265 au0828_analog_stream_enable(dev);
1266
1267 return 0;
1268 }
1269
1270
1271 static int vidioc_querycap(struct file *file, void *priv,
1272 struct v4l2_capability *cap)
1273 {
1274 struct video_device *vdev = video_devdata(file);
1275 struct au0828_fh *fh = priv;
1276 struct au0828_dev *dev = fh->dev;
1277
1278 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1279 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1280 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1281
1282 /* set the device capabilities */
1283 cap->device_caps = V4L2_CAP_AUDIO |
1284 V4L2_CAP_READWRITE |
1285 V4L2_CAP_STREAMING |
1286 V4L2_CAP_TUNER;
1287 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1288 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1289 else
1290 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1291 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1292 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
1293 return 0;
1294 }
1295
1296 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1297 struct v4l2_fmtdesc *f)
1298 {
1299 if (f->index)
1300 return -EINVAL;
1301
1302 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1303 strcpy(f->description, "Packed YUV2");
1304
1305 f->flags = 0;
1306 f->pixelformat = V4L2_PIX_FMT_UYVY;
1307
1308 return 0;
1309 }
1310
1311 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1312 struct v4l2_format *f)
1313 {
1314 struct au0828_fh *fh = priv;
1315 struct au0828_dev *dev = fh->dev;
1316
1317 f->fmt.pix.width = dev->width;
1318 f->fmt.pix.height = dev->height;
1319 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1320 f->fmt.pix.bytesperline = dev->bytesperline;
1321 f->fmt.pix.sizeimage = dev->frame_size;
1322 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1323 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1324 f->fmt.pix.priv = 0;
1325 return 0;
1326 }
1327
1328 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1329 struct v4l2_format *f)
1330 {
1331 struct au0828_fh *fh = priv;
1332 struct au0828_dev *dev = fh->dev;
1333
1334 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1335 }
1336
1337 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1338 struct v4l2_format *f)
1339 {
1340 struct au0828_fh *fh = priv;
1341 struct au0828_dev *dev = fh->dev;
1342 int rc;
1343
1344 rc = check_dev(dev);
1345 if (rc < 0)
1346 return rc;
1347
1348 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1349 pr_info("%s queue busy\n", __func__);
1350 rc = -EBUSY;
1351 goto out;
1352 }
1353
1354 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1355 out:
1356 return rc;
1357 }
1358
1359 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1360 {
1361 struct au0828_fh *fh = priv;
1362 struct au0828_dev *dev = fh->dev;
1363
1364 dev->std = norm;
1365
1366 au0828_init_tuner(dev);
1367
1368 i2c_gate_ctrl(dev, 1);
1369
1370 /*
1371 * FIXME: when we support something other than 60Hz standards,
1372 * we are going to have to make the au0828 bridge adjust the size
1373 * of its capture buffer, which is currently hardcoded at 720x480
1374 */
1375
1376 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1377
1378 i2c_gate_ctrl(dev, 0);
1379
1380 return 0;
1381 }
1382
1383 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1384 {
1385 struct au0828_fh *fh = priv;
1386 struct au0828_dev *dev = fh->dev;
1387
1388 *norm = dev->std;
1389 return 0;
1390 }
1391
1392 static int vidioc_enum_input(struct file *file, void *priv,
1393 struct v4l2_input *input)
1394 {
1395 struct au0828_fh *fh = priv;
1396 struct au0828_dev *dev = fh->dev;
1397 unsigned int tmp;
1398
1399 static const char *inames[] = {
1400 [AU0828_VMUX_UNDEFINED] = "Undefined",
1401 [AU0828_VMUX_COMPOSITE] = "Composite",
1402 [AU0828_VMUX_SVIDEO] = "S-Video",
1403 [AU0828_VMUX_CABLE] = "Cable TV",
1404 [AU0828_VMUX_TELEVISION] = "Television",
1405 [AU0828_VMUX_DVB] = "DVB",
1406 [AU0828_VMUX_DEBUG] = "tv debug"
1407 };
1408
1409 tmp = input->index;
1410
1411 if (tmp >= AU0828_MAX_INPUT)
1412 return -EINVAL;
1413 if (AUVI_INPUT(tmp).type == 0)
1414 return -EINVAL;
1415
1416 input->index = tmp;
1417 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1418 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1419 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1420 input->type |= V4L2_INPUT_TYPE_TUNER;
1421 input->audioset = 1;
1422 } else {
1423 input->type |= V4L2_INPUT_TYPE_CAMERA;
1424 input->audioset = 2;
1425 }
1426
1427 input->std = dev->vdev->tvnorms;
1428
1429 return 0;
1430 }
1431
1432 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1433 {
1434 struct au0828_fh *fh = priv;
1435 struct au0828_dev *dev = fh->dev;
1436 *i = dev->ctrl_input;
1437 return 0;
1438 }
1439
1440 static void au0828_s_input(struct au0828_dev *dev, int index)
1441 {
1442 int i;
1443
1444 switch (AUVI_INPUT(index).type) {
1445 case AU0828_VMUX_SVIDEO:
1446 dev->input_type = AU0828_VMUX_SVIDEO;
1447 dev->ctrl_ainput = 1;
1448 break;
1449 case AU0828_VMUX_COMPOSITE:
1450 dev->input_type = AU0828_VMUX_COMPOSITE;
1451 dev->ctrl_ainput = 1;
1452 break;
1453 case AU0828_VMUX_TELEVISION:
1454 dev->input_type = AU0828_VMUX_TELEVISION;
1455 dev->ctrl_ainput = 0;
1456 break;
1457 default:
1458 dprintk(1, "unknown input type set [%d]\n",
1459 AUVI_INPUT(index).type);
1460 break;
1461 }
1462
1463 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1464 AUVI_INPUT(index).vmux, 0, 0);
1465
1466 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1467 int enable = 0;
1468 if (AUVI_INPUT(i).audio_setup == NULL)
1469 continue;
1470
1471 if (i == index)
1472 enable = 1;
1473 else
1474 enable = 0;
1475 if (enable) {
1476 (AUVI_INPUT(i).audio_setup)(dev, enable);
1477 } else {
1478 /* Make sure we leave it turned on if some
1479 other input is routed to this callback */
1480 if ((AUVI_INPUT(i).audio_setup) !=
1481 ((AUVI_INPUT(index).audio_setup))) {
1482 (AUVI_INPUT(i).audio_setup)(dev, enable);
1483 }
1484 }
1485 }
1486
1487 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1488 AUVI_INPUT(index).amux, 0, 0);
1489 }
1490
1491 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1492 {
1493 struct au0828_fh *fh = priv;
1494 struct au0828_dev *dev = fh->dev;
1495
1496 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1497 index);
1498 if (index >= AU0828_MAX_INPUT)
1499 return -EINVAL;
1500 if (AUVI_INPUT(index).type == 0)
1501 return -EINVAL;
1502 dev->ctrl_input = index;
1503 au0828_s_input(dev, index);
1504 return 0;
1505 }
1506
1507 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1508 {
1509 if (a->index > 1)
1510 return -EINVAL;
1511
1512 if (a->index == 0)
1513 strcpy(a->name, "Television");
1514 else
1515 strcpy(a->name, "Line in");
1516
1517 a->capability = V4L2_AUDCAP_STEREO;
1518 return 0;
1519 }
1520
1521 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1522 {
1523 struct au0828_fh *fh = priv;
1524 struct au0828_dev *dev = fh->dev;
1525
1526 a->index = dev->ctrl_ainput;
1527 if (a->index == 0)
1528 strcpy(a->name, "Television");
1529 else
1530 strcpy(a->name, "Line in");
1531
1532 a->capability = V4L2_AUDCAP_STEREO;
1533 return 0;
1534 }
1535
1536 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1537 {
1538 struct au0828_fh *fh = priv;
1539 struct au0828_dev *dev = fh->dev;
1540
1541 if (a->index != dev->ctrl_ainput)
1542 return -EINVAL;
1543 return 0;
1544 }
1545
1546 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1547 {
1548 struct au0828_fh *fh = priv;
1549 struct au0828_dev *dev = fh->dev;
1550
1551 if (t->index != 0)
1552 return -EINVAL;
1553
1554 strcpy(t->name, "Auvitek tuner");
1555
1556 au0828_init_tuner(dev);
1557 i2c_gate_ctrl(dev, 1);
1558 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1559 i2c_gate_ctrl(dev, 0);
1560 return 0;
1561 }
1562
1563 static int vidioc_s_tuner(struct file *file, void *priv,
1564 const struct v4l2_tuner *t)
1565 {
1566 struct au0828_fh *fh = priv;
1567 struct au0828_dev *dev = fh->dev;
1568
1569 if (t->index != 0)
1570 return -EINVAL;
1571
1572 au0828_init_tuner(dev);
1573 i2c_gate_ctrl(dev, 1);
1574 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1575 i2c_gate_ctrl(dev, 0);
1576
1577 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1578 t->afc);
1579
1580 return 0;
1581
1582 }
1583
1584 static int vidioc_g_frequency(struct file *file, void *priv,
1585 struct v4l2_frequency *freq)
1586 {
1587 struct au0828_fh *fh = priv;
1588 struct au0828_dev *dev = fh->dev;
1589
1590 if (freq->tuner != 0)
1591 return -EINVAL;
1592 freq->frequency = dev->ctrl_freq;
1593 return 0;
1594 }
1595
1596 static int vidioc_s_frequency(struct file *file, void *priv,
1597 const struct v4l2_frequency *freq)
1598 {
1599 struct au0828_fh *fh = priv;
1600 struct au0828_dev *dev = fh->dev;
1601 struct v4l2_frequency new_freq = *freq;
1602
1603 if (freq->tuner != 0)
1604 return -EINVAL;
1605
1606 au0828_init_tuner(dev);
1607 i2c_gate_ctrl(dev, 1);
1608
1609 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1610 /* Get the actual set (and possibly clamped) frequency */
1611 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1612 dev->ctrl_freq = new_freq.frequency;
1613
1614 i2c_gate_ctrl(dev, 0);
1615
1616 au0828_analog_stream_reset(dev);
1617
1618 return 0;
1619 }
1620
1621
1622 /* RAW VBI ioctls */
1623
1624 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1625 struct v4l2_format *format)
1626 {
1627 struct au0828_fh *fh = priv;
1628 struct au0828_dev *dev = fh->dev;
1629
1630 format->fmt.vbi.samples_per_line = dev->vbi_width;
1631 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1632 format->fmt.vbi.offset = 0;
1633 format->fmt.vbi.flags = 0;
1634 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1635
1636 format->fmt.vbi.count[0] = dev->vbi_height;
1637 format->fmt.vbi.count[1] = dev->vbi_height;
1638 format->fmt.vbi.start[0] = 21;
1639 format->fmt.vbi.start[1] = 284;
1640 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1641
1642 return 0;
1643 }
1644
1645 static int vidioc_cropcap(struct file *file, void *priv,
1646 struct v4l2_cropcap *cc)
1647 {
1648 struct au0828_fh *fh = priv;
1649 struct au0828_dev *dev = fh->dev;
1650
1651 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1652 return -EINVAL;
1653
1654 cc->bounds.left = 0;
1655 cc->bounds.top = 0;
1656 cc->bounds.width = dev->width;
1657 cc->bounds.height = dev->height;
1658
1659 cc->defrect = cc->bounds;
1660
1661 cc->pixelaspect.numerator = 54;
1662 cc->pixelaspect.denominator = 59;
1663
1664 return 0;
1665 }
1666
1667 static int vidioc_streamon(struct file *file, void *priv,
1668 enum v4l2_buf_type type)
1669 {
1670 struct au0828_fh *fh = priv;
1671 struct au0828_dev *dev = fh->dev;
1672 int rc = -EINVAL;
1673
1674 rc = check_dev(dev);
1675 if (rc < 0)
1676 return rc;
1677
1678 if (unlikely(type != fh->type))
1679 return -EINVAL;
1680
1681 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1682 fh, type, fh->resources, dev->resources);
1683
1684 if (unlikely(!res_get(fh, get_ressource(fh))))
1685 return -EBUSY;
1686
1687 au0828_init_tuner(dev);
1688 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1689 au0828_analog_stream_enable(dev);
1690 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
1691 }
1692
1693 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1694 rc = videobuf_streamon(&fh->vb_vidq);
1695 dev->vid_timeout_running = 1;
1696 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1697 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1698 rc = videobuf_streamon(&fh->vb_vbiq);
1699 dev->vbi_timeout_running = 1;
1700 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1701 }
1702
1703 return rc;
1704 }
1705
1706 static int vidioc_streamoff(struct file *file, void *priv,
1707 enum v4l2_buf_type type)
1708 {
1709 struct au0828_fh *fh = priv;
1710 struct au0828_dev *dev = fh->dev;
1711 int rc;
1712 int i;
1713
1714 rc = check_dev(dev);
1715 if (rc < 0)
1716 return rc;
1717
1718 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1719 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1720 return -EINVAL;
1721 if (type != fh->type)
1722 return -EINVAL;
1723
1724 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1725 fh, type, fh->resources, dev->resources);
1726
1727 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1728 dev->vid_timeout_running = 0;
1729 del_timer_sync(&dev->vid_timeout);
1730
1731 au0828_analog_stream_disable(dev);
1732 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1733 rc = au0828_stream_interrupt(dev);
1734 if (rc != 0)
1735 return rc;
1736
1737 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1738 if (AUVI_INPUT(i).audio_setup == NULL)
1739 continue;
1740 (AUVI_INPUT(i).audio_setup)(dev, 0);
1741 }
1742
1743 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1744 videobuf_streamoff(&fh->vb_vidq);
1745 res_free(fh, AU0828_RESOURCE_VIDEO);
1746 }
1747 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1748 dev->vbi_timeout_running = 0;
1749 del_timer_sync(&dev->vbi_timeout);
1750
1751 if (res_check(fh, AU0828_RESOURCE_VBI)) {
1752 videobuf_streamoff(&fh->vb_vbiq);
1753 res_free(fh, AU0828_RESOURCE_VBI);
1754 }
1755 }
1756
1757 return 0;
1758 }
1759
1760 #ifdef CONFIG_VIDEO_ADV_DEBUG
1761 static int vidioc_g_register(struct file *file, void *priv,
1762 struct v4l2_dbg_register *reg)
1763 {
1764 struct au0828_fh *fh = priv;
1765 struct au0828_dev *dev = fh->dev;
1766
1767 reg->val = au0828_read(dev, reg->reg);
1768 reg->size = 1;
1769 return 0;
1770 }
1771
1772 static int vidioc_s_register(struct file *file, void *priv,
1773 const struct v4l2_dbg_register *reg)
1774 {
1775 struct au0828_fh *fh = priv;
1776 struct au0828_dev *dev = fh->dev;
1777
1778 return au0828_writereg(dev, reg->reg, reg->val);
1779 }
1780 #endif
1781
1782 static int vidioc_log_status(struct file *file, void *fh)
1783 {
1784 struct video_device *vdev = video_devdata(file);
1785
1786 v4l2_ctrl_log_status(file, fh);
1787 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1788 return 0;
1789 }
1790
1791 static int vidioc_reqbufs(struct file *file, void *priv,
1792 struct v4l2_requestbuffers *rb)
1793 {
1794 struct au0828_fh *fh = priv;
1795 struct au0828_dev *dev = fh->dev;
1796 int rc;
1797
1798 rc = check_dev(dev);
1799 if (rc < 0)
1800 return rc;
1801
1802 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1803 rc = videobuf_reqbufs(&fh->vb_vidq, rb);
1804 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1805 rc = videobuf_reqbufs(&fh->vb_vbiq, rb);
1806
1807 return rc;
1808 }
1809
1810 static int vidioc_querybuf(struct file *file, void *priv,
1811 struct v4l2_buffer *b)
1812 {
1813 struct au0828_fh *fh = priv;
1814 struct au0828_dev *dev = fh->dev;
1815 int rc;
1816
1817 rc = check_dev(dev);
1818 if (rc < 0)
1819 return rc;
1820
1821 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1822 rc = videobuf_querybuf(&fh->vb_vidq, b);
1823 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1824 rc = videobuf_querybuf(&fh->vb_vbiq, b);
1825
1826 return rc;
1827 }
1828
1829 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1830 {
1831 struct au0828_fh *fh = priv;
1832 struct au0828_dev *dev = fh->dev;
1833 int rc;
1834
1835 rc = check_dev(dev);
1836 if (rc < 0)
1837 return rc;
1838
1839 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1840 rc = videobuf_qbuf(&fh->vb_vidq, b);
1841 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1842 rc = videobuf_qbuf(&fh->vb_vbiq, b);
1843
1844 return rc;
1845 }
1846
1847 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1848 {
1849 struct au0828_fh *fh = priv;
1850 struct au0828_dev *dev = fh->dev;
1851 int rc;
1852
1853 rc = check_dev(dev);
1854 if (rc < 0)
1855 return rc;
1856
1857 /* Workaround for a bug in the au0828 hardware design that sometimes
1858 results in the colorspace being inverted */
1859 if (dev->greenscreen_detected == 1) {
1860 dprintk(1, "Detected green frame. Resetting stream...\n");
1861 au0828_analog_stream_reset(dev);
1862 dev->greenscreen_detected = 0;
1863 }
1864
1865 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1866 rc = videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1867 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1868 rc = videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags & O_NONBLOCK);
1869
1870 return rc;
1871 }
1872
1873 void au0828_v4l2_suspend(struct au0828_dev *dev)
1874 {
1875 struct urb *urb;
1876 int i;
1877
1878 pr_info("stopping V4L2\n");
1879
1880 if (dev->stream_state == STREAM_ON) {
1881 pr_info("stopping V4L2 active URBs\n");
1882 au0828_analog_stream_disable(dev);
1883 /* stop urbs */
1884 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1885 urb = dev->isoc_ctl.urb[i];
1886 if (urb) {
1887 if (!irqs_disabled())
1888 usb_kill_urb(urb);
1889 else
1890 usb_unlink_urb(urb);
1891 }
1892 }
1893 }
1894
1895 if (dev->vid_timeout_running)
1896 del_timer_sync(&dev->vid_timeout);
1897 if (dev->vbi_timeout_running)
1898 del_timer_sync(&dev->vbi_timeout);
1899 }
1900
1901 void au0828_v4l2_resume(struct au0828_dev *dev)
1902 {
1903 int i, rc;
1904
1905 pr_info("restarting V4L2\n");
1906
1907 if (dev->stream_state == STREAM_ON) {
1908 au0828_stream_interrupt(dev);
1909 au0828_init_tuner(dev);
1910 }
1911
1912 if (dev->vid_timeout_running)
1913 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1914 if (dev->vbi_timeout_running)
1915 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1916
1917 /* If we were doing ac97 instead of i2s, it would go here...*/
1918 au0828_i2s_init(dev);
1919
1920 au0828_analog_stream_enable(dev);
1921
1922 if (!(dev->stream_state == STREAM_ON)) {
1923 au0828_analog_stream_reset(dev);
1924 /* submit urbs */
1925 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1926 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1927 if (rc) {
1928 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1929 i, rc);
1930 au0828_uninit_isoc(dev);
1931 }
1932 }
1933 }
1934 }
1935
1936 static struct v4l2_file_operations au0828_v4l_fops = {
1937 .owner = THIS_MODULE,
1938 .open = au0828_v4l2_open,
1939 .release = au0828_v4l2_close,
1940 .read = au0828_v4l2_read,
1941 .poll = au0828_v4l2_poll,
1942 .mmap = au0828_v4l2_mmap,
1943 .unlocked_ioctl = video_ioctl2,
1944 };
1945
1946 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1947 .vidioc_querycap = vidioc_querycap,
1948 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1949 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1950 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1951 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1952 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1953 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1954 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1955 .vidioc_enumaudio = vidioc_enumaudio,
1956 .vidioc_g_audio = vidioc_g_audio,
1957 .vidioc_s_audio = vidioc_s_audio,
1958 .vidioc_cropcap = vidioc_cropcap,
1959 .vidioc_reqbufs = vidioc_reqbufs,
1960 .vidioc_querybuf = vidioc_querybuf,
1961 .vidioc_qbuf = vidioc_qbuf,
1962 .vidioc_dqbuf = vidioc_dqbuf,
1963 .vidioc_s_std = vidioc_s_std,
1964 .vidioc_g_std = vidioc_g_std,
1965 .vidioc_enum_input = vidioc_enum_input,
1966 .vidioc_g_input = vidioc_g_input,
1967 .vidioc_s_input = vidioc_s_input,
1968 .vidioc_streamon = vidioc_streamon,
1969 .vidioc_streamoff = vidioc_streamoff,
1970 .vidioc_g_tuner = vidioc_g_tuner,
1971 .vidioc_s_tuner = vidioc_s_tuner,
1972 .vidioc_g_frequency = vidioc_g_frequency,
1973 .vidioc_s_frequency = vidioc_s_frequency,
1974 #ifdef CONFIG_VIDEO_ADV_DEBUG
1975 .vidioc_g_register = vidioc_g_register,
1976 .vidioc_s_register = vidioc_s_register,
1977 #endif
1978 .vidioc_log_status = vidioc_log_status,
1979 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1980 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1981 };
1982
1983 static const struct video_device au0828_video_template = {
1984 .fops = &au0828_v4l_fops,
1985 .release = video_device_release,
1986 .ioctl_ops = &video_ioctl_ops,
1987 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1988 };
1989
1990 /**************************************************************************/
1991
1992 int au0828_analog_register(struct au0828_dev *dev,
1993 struct usb_interface *interface)
1994 {
1995 int retval = -ENOMEM;
1996 struct usb_host_interface *iface_desc;
1997 struct usb_endpoint_descriptor *endpoint;
1998 int i, ret;
1999
2000 dprintk(1, "au0828_analog_register called for intf#%d!\n",
2001 interface->cur_altsetting->desc.bInterfaceNumber);
2002
2003 /* set au0828 usb interface0 to as5 */
2004 retval = usb_set_interface(dev->usbdev,
2005 interface->cur_altsetting->desc.bInterfaceNumber, 5);
2006 if (retval != 0) {
2007 pr_info("Failure setting usb interface0 to as5\n");
2008 return retval;
2009 }
2010
2011 /* Figure out which endpoint has the isoc interface */
2012 iface_desc = interface->cur_altsetting;
2013 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2014 endpoint = &iface_desc->endpoint[i].desc;
2015 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2016 == USB_DIR_IN) &&
2017 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2018 == USB_ENDPOINT_XFER_ISOC)) {
2019
2020 /* we find our isoc in endpoint */
2021 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
2022 dev->max_pkt_size = (tmp & 0x07ff) *
2023 (((tmp & 0x1800) >> 11) + 1);
2024 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
2025 dprintk(1,
2026 "Found isoc endpoint 0x%02x, max size = %d\n",
2027 dev->isoc_in_endpointaddr, dev->max_pkt_size);
2028 }
2029 }
2030 if (!(dev->isoc_in_endpointaddr)) {
2031 pr_info("Could not locate isoc endpoint\n");
2032 kfree(dev);
2033 return -ENODEV;
2034 }
2035
2036 init_waitqueue_head(&dev->open);
2037 spin_lock_init(&dev->slock);
2038
2039 /* init video dma queues */
2040 INIT_LIST_HEAD(&dev->vidq.active);
2041 INIT_LIST_HEAD(&dev->vidq.queued);
2042 INIT_LIST_HEAD(&dev->vbiq.active);
2043 INIT_LIST_HEAD(&dev->vbiq.queued);
2044
2045 dev->vid_timeout.function = au0828_vid_buffer_timeout;
2046 dev->vid_timeout.data = (unsigned long) dev;
2047 init_timer(&dev->vid_timeout);
2048
2049 dev->vbi_timeout.function = au0828_vbi_buffer_timeout;
2050 dev->vbi_timeout.data = (unsigned long) dev;
2051 init_timer(&dev->vbi_timeout);
2052
2053 dev->width = NTSC_STD_W;
2054 dev->height = NTSC_STD_H;
2055 dev->field_size = dev->width * dev->height;
2056 dev->frame_size = dev->field_size << 1;
2057 dev->bytesperline = dev->width << 1;
2058 dev->vbi_width = 720;
2059 dev->vbi_height = 1;
2060 dev->ctrl_ainput = 0;
2061 dev->ctrl_freq = 960;
2062 dev->std = V4L2_STD_NTSC_M;
2063 au0828_s_input(dev, 0);
2064
2065 /* allocate and fill v4l2 video struct */
2066 dev->vdev = video_device_alloc();
2067 if (NULL == dev->vdev) {
2068 dprintk(1, "Can't allocate video_device.\n");
2069 return -ENOMEM;
2070 }
2071
2072 /* allocate the VBI struct */
2073 dev->vbi_dev = video_device_alloc();
2074 if (NULL == dev->vbi_dev) {
2075 dprintk(1, "Can't allocate vbi_device.\n");
2076 ret = -ENOMEM;
2077 goto err_vdev;
2078 }
2079
2080 /* Fill the video capture device struct */
2081 *dev->vdev = au0828_video_template;
2082 dev->vdev->v4l2_dev = &dev->v4l2_dev;
2083 dev->vdev->lock = &dev->lock;
2084 strcpy(dev->vdev->name, "au0828a video");
2085
2086 /* Setup the VBI device */
2087 *dev->vbi_dev = au0828_video_template;
2088 dev->vbi_dev->v4l2_dev = &dev->v4l2_dev;
2089 dev->vbi_dev->lock = &dev->lock;
2090 strcpy(dev->vbi_dev->name, "au0828a vbi");
2091
2092 /* Register the v4l2 device */
2093 video_set_drvdata(dev->vdev, dev);
2094 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
2095 if (retval != 0) {
2096 dprintk(1, "unable to register video device (error = %d).\n",
2097 retval);
2098 ret = -ENODEV;
2099 goto err_vbi_dev;
2100 }
2101
2102 /* Register the vbi device */
2103 video_set_drvdata(dev->vbi_dev, dev);
2104 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
2105 if (retval != 0) {
2106 dprintk(1, "unable to register vbi device (error = %d).\n",
2107 retval);
2108 ret = -ENODEV;
2109 goto err_vbi_dev;
2110 }
2111
2112 dprintk(1, "%s completed!\n", __func__);
2113
2114 return 0;
2115
2116 err_vbi_dev:
2117 video_device_release(dev->vbi_dev);
2118 err_vdev:
2119 video_device_release(dev->vdev);
2120 return ret;
2121 }
2122
This page took 0.092833 seconds and 4 git commands to generate.