1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24#include <linux/delay.h>
25#include <linux/errno.h>
26#include <linux/fs.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/ioport.h>
31#include <linux/init.h>
32#include <linux/sched.h>
33#include <linux/random.h>
34#include <linux/usb.h>
35#include <linux/videodev2.h>
36#include <media/v4l2-ioctl.h>
37#include <media/tuner.h>
38#include <linux/interrupt.h>
39#include <linux/kthread.h>
40#include <linux/highmem.h>
41#include <linux/freezer.h>
42
43#include "tm6000-regs.h"
44#include "tm6000.h"
45
46#define BUFFER_TIMEOUT msecs_to_jiffies(2000)
47
48
49#define TM6000_MIN_BUF 4
50#define TM6000_DEF_BUF 8
51
52#define TM6000_MAX_ISO_PACKETS 46
53
54
55static unsigned int vid_limit = 16;
56static int video_nr = -1;
57static int radio_nr = -1;
58
59
60int tm6000_debug;
61EXPORT_SYMBOL_GPL(tm6000_debug);
62
63static const struct v4l2_queryctrl no_ctrl = {
64 .name = "42",
65 .flags = V4L2_CTRL_FLAG_DISABLED,
66};
67
68
69static struct v4l2_queryctrl tm6000_qctrl[] = {
70 {
71 .id = V4L2_CID_BRIGHTNESS,
72 .type = V4L2_CTRL_TYPE_INTEGER,
73 .name = "Brightness",
74 .minimum = 0,
75 .maximum = 255,
76 .step = 1,
77 .default_value = 54,
78 .flags = 0,
79 }, {
80 .id = V4L2_CID_CONTRAST,
81 .type = V4L2_CTRL_TYPE_INTEGER,
82 .name = "Contrast",
83 .minimum = 0,
84 .maximum = 255,
85 .step = 0x1,
86 .default_value = 119,
87 .flags = 0,
88 }, {
89 .id = V4L2_CID_SATURATION,
90 .type = V4L2_CTRL_TYPE_INTEGER,
91 .name = "Saturation",
92 .minimum = 0,
93 .maximum = 255,
94 .step = 0x1,
95 .default_value = 112,
96 .flags = 0,
97 }, {
98 .id = V4L2_CID_HUE,
99 .type = V4L2_CTRL_TYPE_INTEGER,
100 .name = "Hue",
101 .minimum = -128,
102 .maximum = 127,
103 .step = 0x1,
104 .default_value = 0,
105 .flags = 0,
106 },
107
108 {
109 .id = V4L2_CID_AUDIO_MUTE,
110 .name = "Mute",
111 .minimum = 0,
112 .maximum = 1,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
114 }, {
115 .id = V4L2_CID_AUDIO_VOLUME,
116 .name = "Volume",
117 .minimum = -15,
118 .maximum = 15,
119 .step = 1,
120 .default_value = 0,
121 .type = V4L2_CTRL_TYPE_INTEGER,
122 }
123};
124
125static const unsigned int CTRLS = ARRAY_SIZE(tm6000_qctrl);
126static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
127
128static struct tm6000_fmt format[] = {
129 {
130 .name = "4:2:2, packed, YVY2",
131 .fourcc = V4L2_PIX_FMT_YUYV,
132 .depth = 16,
133 }, {
134 .name = "4:2:2, packed, UYVY",
135 .fourcc = V4L2_PIX_FMT_UYVY,
136 .depth = 16,
137 }, {
138 .name = "A/V + VBI mux packet",
139 .fourcc = V4L2_PIX_FMT_TM6000,
140 .depth = 16,
141 }
142};
143
144static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
145{
146 unsigned int i;
147
148 for (i = 0; i < CTRLS; i++)
149 if (tm6000_qctrl[i].id == id)
150 return tm6000_qctrl+i;
151 return NULL;
152}
153
154
155
156
157
158
159#define norm_maxw(a) 720
160#define norm_maxh(a) 576
161
162#define norm_minw(a) norm_maxw(a)
163#define norm_minh(a) norm_maxh(a)
164
165
166
167
168static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
169 struct tm6000_buffer **buf)
170{
171 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
172
173 if (list_empty(&dma_q->active)) {
174 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
175 *buf = NULL;
176 return;
177 }
178
179 *buf = list_entry(dma_q->active.next,
180 struct tm6000_buffer, vb.queue);
181}
182
183
184
185
186static inline void buffer_filled(struct tm6000_core *dev,
187 struct tm6000_dmaqueue *dma_q,
188 struct tm6000_buffer *buf)
189{
190
191 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
192 buf->vb.state = VIDEOBUF_DONE;
193 buf->vb.field_count++;
194 do_gettimeofday(&buf->vb.ts);
195
196 list_del(&buf->vb.queue);
197 wake_up(&buf->vb.done);
198}
199
200
201
202
203static int copy_streams(u8 *data, unsigned long len,
204 struct urb *urb)
205{
206 struct tm6000_dmaqueue *dma_q = urb->context;
207 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
208 u8 *ptr = data, *endp = data+len;
209 unsigned long header = 0;
210 int rc = 0;
211 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
212 struct tm6000_buffer *vbuf = NULL;
213 char *voutp = NULL;
214 unsigned int linewidth;
215
216 if (!dev->radio) {
217
218 get_next_buf(dma_q, &vbuf);
219
220 if (!vbuf)
221 return rc;
222 voutp = videobuf_to_vmalloc(&vbuf->vb);
223
224 if (!voutp)
225 return 0;
226 }
227
228 for (ptr = data; ptr < endp;) {
229 if (!dev->isoc_ctl.cmd) {
230
231 if (dev->isoc_ctl.tmp_buf_len > 0) {
232
233 header = dev->isoc_ctl.tmp_buf;
234 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
235 memcpy((u8 *)&header +
236 dev->isoc_ctl.tmp_buf_len,
237 ptr,
238 4 - dev->isoc_ctl.tmp_buf_len);
239 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
240 }
241 dev->isoc_ctl.tmp_buf_len = 0;
242 } else {
243 if (ptr + 3 >= endp) {
244
245 dev->isoc_ctl.tmp_buf_len = endp - ptr;
246 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
247 dev->isoc_ctl.tmp_buf_len);
248 return rc;
249 }
250
251 for (; ptr < endp - 3; ptr++) {
252 if (*(ptr + 3) == 0x47)
253 break;
254 }
255
256 header = *(unsigned long *)ptr;
257 ptr += 4;
258 }
259
260
261 size = ((header & 0x7e) << 1);
262 if (size > 0)
263 size -= 4;
264 block = (header >> 7) & 0xf;
265 field = (header >> 11) & 0x1;
266 line = (header >> 12) & 0x1ff;
267 cmd = (header >> 21) & 0x7;
268
269 if (size > TM6000_URB_MSG_LEN)
270 size = TM6000_URB_MSG_LEN;
271 pktsize = TM6000_URB_MSG_LEN;
272
273
274
275 switch (cmd) {
276 case TM6000_URB_MSG_VIDEO:
277 if (!dev->radio) {
278 if ((dev->isoc_ctl.vfield != field) &&
279 (field == 1)) {
280
281
282
283
284 buffer_filled(dev, dma_q, vbuf);
285 dprintk(dev, V4L2_DEBUG_ISOC,
286 "new buffer filled\n");
287 get_next_buf(dma_q, &vbuf);
288 if (!vbuf)
289 return rc;
290 voutp = videobuf_to_vmalloc(&vbuf->vb);
291 if (!voutp)
292 return rc;
293 memset(voutp, 0, vbuf->vb.size);
294 }
295 linewidth = vbuf->vb.width << 1;
296 pos = ((line << 1) - field - 1) *
297 linewidth + block * TM6000_URB_MSG_LEN;
298
299 if (pos + size > vbuf->vb.size)
300 cmd = TM6000_URB_MSG_ERR;
301 dev->isoc_ctl.vfield = field;
302 }
303 break;
304 case TM6000_URB_MSG_VBI:
305 break;
306 case TM6000_URB_MSG_AUDIO:
307 case TM6000_URB_MSG_PTS:
308 size = pktsize;
309 break;
310 }
311 } else {
312
313 cmd = dev->isoc_ctl.cmd;
314 size = dev->isoc_ctl.size;
315 pos = dev->isoc_ctl.pos;
316 pktsize = dev->isoc_ctl.pktsize;
317 field = dev->isoc_ctl.field;
318 }
319 cpysize = (endp - ptr > size) ? size : endp - ptr;
320 if (cpysize) {
321
322 switch (cmd) {
323 case TM6000_URB_MSG_VIDEO:
324
325 if (vbuf)
326 memcpy(&voutp[pos], ptr, cpysize);
327 break;
328 case TM6000_URB_MSG_AUDIO: {
329 int i;
330 for (i = 0; i < cpysize; i += 2)
331 swab16s((u16 *)(ptr + i));
332
333 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
334 break;
335 }
336 case TM6000_URB_MSG_VBI:
337
338 break;
339 case TM6000_URB_MSG_PTS: {
340
341 u32 pts;
342 pts = *(u32 *)ptr;
343 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
344 field, pts);
345 break;
346 }
347 }
348 }
349 if (ptr + pktsize > endp) {
350
351
352
353
354 dev->isoc_ctl.pos = pos + cpysize;
355 dev->isoc_ctl.size = size - cpysize;
356 dev->isoc_ctl.cmd = cmd;
357 dev->isoc_ctl.field = field;
358 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
359 ptr += endp - ptr;
360 } else {
361 dev->isoc_ctl.cmd = 0;
362 ptr += pktsize;
363 }
364 }
365 return 0;
366}
367
368
369
370
371static int copy_multiplexed(u8 *ptr, unsigned long len,
372 struct urb *urb)
373{
374 struct tm6000_dmaqueue *dma_q = urb->context;
375 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
376 unsigned int pos = dev->isoc_ctl.pos, cpysize;
377 int rc = 1;
378 struct tm6000_buffer *buf;
379 char *outp = NULL;
380
381 get_next_buf(dma_q, &buf);
382 if (buf)
383 outp = videobuf_to_vmalloc(&buf->vb);
384
385 if (!outp)
386 return 0;
387
388 while (len > 0) {
389 cpysize = min(len, buf->vb.size-pos);
390 memcpy(&outp[pos], ptr, cpysize);
391 pos += cpysize;
392 ptr += cpysize;
393 len -= cpysize;
394 if (pos >= buf->vb.size) {
395 pos = 0;
396
397 buffer_filled(dev, dma_q, buf);
398 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
399 get_next_buf(dma_q, &buf);
400 if (!buf)
401 break;
402 outp = videobuf_to_vmalloc(&(buf->vb));
403 if (!outp)
404 return rc;
405 pos = 0;
406 }
407 }
408
409 dev->isoc_ctl.pos = pos;
410 return rc;
411}
412
413static inline void print_err_status(struct tm6000_core *dev,
414 int packet, int status)
415{
416 char *errmsg = "Unknown";
417
418 switch (status) {
419 case -ENOENT:
420 errmsg = "unlinked synchronuously";
421 break;
422 case -ECONNRESET:
423 errmsg = "unlinked asynchronuously";
424 break;
425 case -ENOSR:
426 errmsg = "Buffer error (overrun)";
427 break;
428 case -EPIPE:
429 errmsg = "Stalled (device not responding)";
430 break;
431 case -EOVERFLOW:
432 errmsg = "Babble (bad cable?)";
433 break;
434 case -EPROTO:
435 errmsg = "Bit-stuff error (bad cable?)";
436 break;
437 case -EILSEQ:
438 errmsg = "CRC/Timeout (could be anything)";
439 break;
440 case -ETIME:
441 errmsg = "Device does not respond";
442 break;
443 }
444 if (packet < 0) {
445 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
446 status, errmsg);
447 } else {
448 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
449 packet, status, errmsg);
450 }
451}
452
453
454
455
456
457static inline int tm6000_isoc_copy(struct urb *urb)
458{
459 struct tm6000_dmaqueue *dma_q = urb->context;
460 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
461 int i, len = 0, rc = 1, status;
462 char *p;
463
464 if (urb->status < 0) {
465 print_err_status(dev, -1, urb->status);
466 return 0;
467 }
468
469 for (i = 0; i < urb->number_of_packets; i++) {
470 status = urb->iso_frame_desc[i].status;
471
472 if (status < 0) {
473 print_err_status(dev, i, status);
474 continue;
475 }
476
477 len = urb->iso_frame_desc[i].actual_length;
478
479 if (len > 0) {
480 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
481 if (!urb->iso_frame_desc[i].status) {
482 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
483 rc = copy_multiplexed(p, len, urb);
484 if (rc <= 0)
485 return rc;
486 } else {
487 copy_streams(p, len, urb);
488 }
489 }
490 }
491 }
492 return rc;
493}
494
495
496
497
498
499
500
501
502
503static void tm6000_irq_callback(struct urb *urb)
504{
505 struct tm6000_dmaqueue *dma_q = urb->context;
506 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
507 int i;
508
509 switch (urb->status) {
510 case 0:
511 case -ETIMEDOUT:
512 break;
513
514 case -ECONNRESET:
515 case -ENOENT:
516 case -ESHUTDOWN:
517 return;
518
519 default:
520 tm6000_err("urb completion error %d.\n", urb->status);
521 break;
522 }
523
524 spin_lock(&dev->slock);
525 tm6000_isoc_copy(urb);
526 spin_unlock(&dev->slock);
527
528
529 for (i = 0; i < urb->number_of_packets; i++) {
530 urb->iso_frame_desc[i].status = 0;
531 urb->iso_frame_desc[i].actual_length = 0;
532 }
533
534 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
535 if (urb->status)
536 tm6000_err("urb resubmit failed (error=%i)\n",
537 urb->status);
538}
539
540
541
542
543static void tm6000_uninit_isoc(struct tm6000_core *dev)
544{
545 struct urb *urb;
546 int i;
547
548 dev->isoc_ctl.buf = NULL;
549 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
550 urb = dev->isoc_ctl.urb[i];
551 if (urb) {
552 usb_kill_urb(urb);
553 usb_unlink_urb(urb);
554 if (dev->isoc_ctl.transfer_buffer[i]) {
555 usb_free_coherent(dev->udev,
556 urb->transfer_buffer_length,
557 dev->isoc_ctl.transfer_buffer[i],
558 urb->transfer_dma);
559 }
560 usb_free_urb(urb);
561 dev->isoc_ctl.urb[i] = NULL;
562 }
563 dev->isoc_ctl.transfer_buffer[i] = NULL;
564 }
565
566 kfree(dev->isoc_ctl.urb);
567 kfree(dev->isoc_ctl.transfer_buffer);
568
569 dev->isoc_ctl.urb = NULL;
570 dev->isoc_ctl.transfer_buffer = NULL;
571 dev->isoc_ctl.num_bufs = 0;
572}
573
574
575
576
577static int tm6000_prepare_isoc(struct tm6000_core *dev)
578{
579 struct tm6000_dmaqueue *dma_q = &dev->vidq;
580 int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
581 struct urb *urb;
582
583
584 tm6000_uninit_isoc(dev);
585
586 tm6000_ir_int_stop(dev);
587
588 usb_set_interface(dev->udev,
589 dev->isoc_in.bInterfaceNumber,
590 dev->isoc_in.bAlternateSetting);
591
592
593 tm6000_ir_int_start(dev);
594
595 pipe = usb_rcvisocpipe(dev->udev,
596 dev->isoc_in.endp->desc.bEndpointAddress &
597 USB_ENDPOINT_NUMBER_MASK);
598
599 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
600
601 if (size > dev->isoc_in.maxsize)
602 size = dev->isoc_in.maxsize;
603
604 dev->isoc_ctl.max_pkt_size = size;
605
606 max_packets = TM6000_MAX_ISO_PACKETS;
607 sb_size = max_packets * size;
608
609 dev->isoc_ctl.num_bufs = num_bufs;
610
611 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
612 if (!dev->isoc_ctl.urb) {
613 tm6000_err("cannot alloc memory for usb buffers\n");
614 return -ENOMEM;
615 }
616
617 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
618 GFP_KERNEL);
619 if (!dev->isoc_ctl.transfer_buffer) {
620 tm6000_err("cannot allocate memory for usbtransfer\n");
621 kfree(dev->isoc_ctl.urb);
622 return -ENOMEM;
623 }
624
625 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
626 " (%d bytes) of %d bytes each to handle %u size\n",
627 max_packets, num_bufs, sb_size,
628 dev->isoc_in.maxsize, size);
629
630
631 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
632 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
633 if (!urb) {
634 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
635 tm6000_uninit_isoc(dev);
636 usb_free_urb(urb);
637 return -ENOMEM;
638 }
639 dev->isoc_ctl.urb[i] = urb;
640
641 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
642 sb_size, GFP_KERNEL, &urb->transfer_dma);
643 if (!dev->isoc_ctl.transfer_buffer[i]) {
644 tm6000_err("unable to allocate %i bytes for transfer"
645 " buffer %i%s\n",
646 sb_size, i,
647 in_interrupt() ? " while in int" : "");
648 tm6000_uninit_isoc(dev);
649 return -ENOMEM;
650 }
651 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
652
653 usb_fill_bulk_urb(urb, dev->udev, pipe,
654 dev->isoc_ctl.transfer_buffer[i], sb_size,
655 tm6000_irq_callback, dma_q);
656 urb->interval = dev->isoc_in.endp->desc.bInterval;
657 urb->number_of_packets = max_packets;
658 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
659
660 for (j = 0; j < max_packets; j++) {
661 urb->iso_frame_desc[j].offset = size * j;
662 urb->iso_frame_desc[j].length = size;
663 }
664 }
665
666 return 0;
667}
668
669static int tm6000_start_thread(struct tm6000_core *dev)
670{
671 struct tm6000_dmaqueue *dma_q = &dev->vidq;
672 int i;
673
674 dma_q->frame = 0;
675 dma_q->ini_jiffies = jiffies;
676
677 init_waitqueue_head(&dma_q->wq);
678
679
680 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
681 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
682 if (rc) {
683 tm6000_err("submit of urb %i failed (error=%i)\n", i,
684 rc);
685 tm6000_uninit_isoc(dev);
686 return rc;
687 }
688 }
689
690 return 0;
691}
692
693
694
695
696
697
698static int
699buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
700{
701 struct tm6000_fh *fh = vq->priv_data;
702
703 *size = fh->fmt->depth * fh->width * fh->height >> 3;
704 if (0 == *count)
705 *count = TM6000_DEF_BUF;
706
707 if (*count < TM6000_MIN_BUF)
708 *count = TM6000_MIN_BUF;
709
710 while (*size * *count > vid_limit * 1024 * 1024)
711 (*count)--;
712
713 return 0;
714}
715
716static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
717{
718 struct tm6000_fh *fh = vq->priv_data;
719 struct tm6000_core *dev = fh->dev;
720 unsigned long flags;
721
722 if (in_interrupt())
723 BUG();
724
725
726
727
728
729
730
731
732
733
734 spin_lock_irqsave(&dev->slock, flags);
735 if (dev->isoc_ctl.buf == buf)
736 dev->isoc_ctl.buf = NULL;
737 spin_unlock_irqrestore(&dev->slock, flags);
738
739 videobuf_vmalloc_free(&buf->vb);
740 buf->vb.state = VIDEOBUF_NEEDS_INIT;
741}
742
743static int
744buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
745 enum v4l2_field field)
746{
747 struct tm6000_fh *fh = vq->priv_data;
748 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
749 struct tm6000_core *dev = fh->dev;
750 int rc = 0;
751
752 BUG_ON(NULL == fh->fmt);
753
754
755
756
757 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
758 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
759 return -EINVAL;
760
761 if (buf->fmt != fh->fmt ||
762 buf->vb.width != fh->width ||
763 buf->vb.height != fh->height ||
764 buf->vb.field != field) {
765 buf->fmt = fh->fmt;
766 buf->vb.width = fh->width;
767 buf->vb.height = fh->height;
768 buf->vb.field = field;
769 buf->vb.state = VIDEOBUF_NEEDS_INIT;
770 }
771
772 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
773 rc = videobuf_iolock(vq, &buf->vb, NULL);
774 if (rc != 0)
775 goto fail;
776 }
777
778 if (!dev->isoc_ctl.num_bufs) {
779 rc = tm6000_prepare_isoc(dev);
780 if (rc < 0)
781 goto fail;
782
783 rc = tm6000_start_thread(dev);
784 if (rc < 0)
785 goto fail;
786
787 }
788
789 buf->vb.state = VIDEOBUF_PREPARED;
790 return 0;
791
792fail:
793 free_buffer(vq, buf);
794 return rc;
795}
796
797static void
798buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
799{
800 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
801 struct tm6000_fh *fh = vq->priv_data;
802 struct tm6000_core *dev = fh->dev;
803 struct tm6000_dmaqueue *vidq = &dev->vidq;
804
805 buf->vb.state = VIDEOBUF_QUEUED;
806 list_add_tail(&buf->vb.queue, &vidq->active);
807}
808
809static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
810{
811 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
812
813 free_buffer(vq, buf);
814}
815
816static struct videobuf_queue_ops tm6000_video_qops = {
817 .buf_setup = buffer_setup,
818 .buf_prepare = buffer_prepare,
819 .buf_queue = buffer_queue,
820 .buf_release = buffer_release,
821};
822
823
824
825
826
827
828static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
829{
830
831 if (dev->resources == fh && dev->is_res_read)
832 return true;
833
834 return false;
835}
836
837static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
838{
839
840 if (dev->resources == fh)
841 return true;
842
843 return false;
844}
845
846static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
847 bool is_res_read)
848{
849
850 if (dev->resources == fh && dev->is_res_read == is_res_read)
851 return true;
852
853
854 if (dev->resources)
855 return false;
856
857
858 dev->resources = fh;
859 dev->is_res_read = is_res_read;
860 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
861 return true;
862}
863
864static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
865{
866
867 if (dev->resources != fh)
868 return;
869
870 dev->resources = NULL;
871 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
872}
873
874
875
876
877
878static int vidioc_querycap(struct file *file, void *priv,
879 struct v4l2_capability *cap)
880{
881 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
882
883 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
884 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
885 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
886 V4L2_CAP_STREAMING |
887 V4L2_CAP_AUDIO |
888 V4L2_CAP_READWRITE;
889
890 if (dev->tuner_type != TUNER_ABSENT)
891 cap->capabilities |= V4L2_CAP_TUNER;
892
893 return 0;
894}
895
896static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
897 struct v4l2_fmtdesc *f)
898{
899 if (unlikely(f->index >= ARRAY_SIZE(format)))
900 return -EINVAL;
901
902 strlcpy(f->description, format[f->index].name, sizeof(f->description));
903 f->pixelformat = format[f->index].fourcc;
904 return 0;
905}
906
907static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
908 struct v4l2_format *f)
909{
910 struct tm6000_fh *fh = priv;
911
912 f->fmt.pix.width = fh->width;
913 f->fmt.pix.height = fh->height;
914 f->fmt.pix.field = fh->vb_vidq.field;
915 f->fmt.pix.pixelformat = fh->fmt->fourcc;
916 f->fmt.pix.bytesperline =
917 (f->fmt.pix.width * fh->fmt->depth) >> 3;
918 f->fmt.pix.sizeimage =
919 f->fmt.pix.height * f->fmt.pix.bytesperline;
920
921 return 0;
922}
923
924static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
925{
926 unsigned int i;
927
928 for (i = 0; i < ARRAY_SIZE(format); i++)
929 if (format[i].fourcc == fourcc)
930 return format+i;
931 return NULL;
932}
933
934static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
935 struct v4l2_format *f)
936{
937 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
938 struct tm6000_fmt *fmt;
939 enum v4l2_field field;
940
941 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
942 if (NULL == fmt) {
943 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
944 " invalid.\n", f->fmt.pix.pixelformat);
945 return -EINVAL;
946 }
947
948 field = f->fmt.pix.field;
949
950 if (field == V4L2_FIELD_ANY)
951 field = V4L2_FIELD_SEQ_TB;
952 else if (V4L2_FIELD_INTERLACED != field) {
953 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
954 return -EINVAL;
955 }
956
957 tm6000_get_std_res(dev);
958
959 f->fmt.pix.width = dev->width;
960 f->fmt.pix.height = dev->height;
961
962 f->fmt.pix.width &= ~0x01;
963
964 f->fmt.pix.field = field;
965
966 f->fmt.pix.bytesperline =
967 (f->fmt.pix.width * fmt->depth) >> 3;
968 f->fmt.pix.sizeimage =
969 f->fmt.pix.height * f->fmt.pix.bytesperline;
970
971 return 0;
972}
973
974
975static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
976 struct v4l2_format *f)
977{
978 struct tm6000_fh *fh = priv;
979 struct tm6000_core *dev = fh->dev;
980 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
981 if (ret < 0)
982 return ret;
983
984 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
985 fh->width = f->fmt.pix.width;
986 fh->height = f->fmt.pix.height;
987 fh->vb_vidq.field = f->fmt.pix.field;
988 fh->type = f->type;
989
990 dev->fourcc = f->fmt.pix.pixelformat;
991
992 tm6000_set_fourcc_format(dev);
993
994 return 0;
995}
996
997static int vidioc_reqbufs(struct file *file, void *priv,
998 struct v4l2_requestbuffers *p)
999{
1000 struct tm6000_fh *fh = priv;
1001
1002 return videobuf_reqbufs(&fh->vb_vidq, p);
1003}
1004
1005static int vidioc_querybuf(struct file *file, void *priv,
1006 struct v4l2_buffer *p)
1007{
1008 struct tm6000_fh *fh = priv;
1009
1010 return videobuf_querybuf(&fh->vb_vidq, p);
1011}
1012
1013static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1014{
1015 struct tm6000_fh *fh = priv;
1016
1017 return videobuf_qbuf(&fh->vb_vidq, p);
1018}
1019
1020static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1021{
1022 struct tm6000_fh *fh = priv;
1023
1024 return videobuf_dqbuf(&fh->vb_vidq, p,
1025 file->f_flags & O_NONBLOCK);
1026}
1027
1028static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1029{
1030 struct tm6000_fh *fh = priv;
1031 struct tm6000_core *dev = fh->dev;
1032
1033 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1034 return -EINVAL;
1035 if (i != fh->type)
1036 return -EINVAL;
1037
1038 if (!res_get(dev, fh, false))
1039 return -EBUSY;
1040 return videobuf_streamon(&fh->vb_vidq);
1041}
1042
1043static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1044{
1045 struct tm6000_fh *fh = priv;
1046 struct tm6000_core *dev = fh->dev;
1047
1048 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1049 return -EINVAL;
1050
1051 if (i != fh->type)
1052 return -EINVAL;
1053
1054 videobuf_streamoff(&fh->vb_vidq);
1055 res_free(dev, fh);
1056
1057 return 0;
1058}
1059
1060static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1061{
1062 int rc = 0;
1063 struct tm6000_fh *fh = priv;
1064 struct tm6000_core *dev = fh->dev;
1065
1066 dev->norm = *norm;
1067 rc = tm6000_init_analog_mode(dev);
1068
1069 fh->width = dev->width;
1070 fh->height = dev->height;
1071
1072 if (rc < 0)
1073 return rc;
1074
1075 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1076
1077 return 0;
1078}
1079
1080static const char *iname[] = {
1081 [TM6000_INPUT_TV] = "Television",
1082 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1083 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1084 [TM6000_INPUT_SVIDEO] = "S-Video",
1085};
1086
1087static int vidioc_enum_input(struct file *file, void *priv,
1088 struct v4l2_input *i)
1089{
1090 struct tm6000_fh *fh = priv;
1091 struct tm6000_core *dev = fh->dev;
1092 unsigned int n;
1093
1094 n = i->index;
1095 if (n >= 3)
1096 return -EINVAL;
1097
1098 if (!dev->vinput[n].type)
1099 return -EINVAL;
1100
1101 i->index = n;
1102
1103 if (dev->vinput[n].type == TM6000_INPUT_TV)
1104 i->type = V4L2_INPUT_TYPE_TUNER;
1105 else
1106 i->type = V4L2_INPUT_TYPE_CAMERA;
1107
1108 strcpy(i->name, iname[dev->vinput[n].type]);
1109
1110 i->std = TM6000_STD;
1111
1112 return 0;
1113}
1114
1115static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1116{
1117 struct tm6000_fh *fh = priv;
1118 struct tm6000_core *dev = fh->dev;
1119
1120 *i = dev->input;
1121
1122 return 0;
1123}
1124
1125static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1126{
1127 struct tm6000_fh *fh = priv;
1128 struct tm6000_core *dev = fh->dev;
1129 int rc = 0;
1130
1131 if (i >= 3)
1132 return -EINVAL;
1133 if (!dev->vinput[i].type)
1134 return -EINVAL;
1135
1136 dev->input = i;
1137
1138 rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
1139
1140 return rc;
1141}
1142
1143
1144static int vidioc_queryctrl(struct file *file, void *priv,
1145 struct v4l2_queryctrl *qc)
1146{
1147 int i;
1148
1149 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1150 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1151 memcpy(qc, &(tm6000_qctrl[i]),
1152 sizeof(*qc));
1153 return 0;
1154 }
1155
1156 return -EINVAL;
1157}
1158
1159static int vidioc_g_ctrl(struct file *file, void *priv,
1160 struct v4l2_control *ctrl)
1161{
1162 struct tm6000_fh *fh = priv;
1163 struct tm6000_core *dev = fh->dev;
1164 int val;
1165
1166
1167 switch (ctrl->id) {
1168 case V4L2_CID_CONTRAST:
1169 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
1170 break;
1171 case V4L2_CID_BRIGHTNESS:
1172 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
1173 return 0;
1174 case V4L2_CID_SATURATION:
1175 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
1176 return 0;
1177 case V4L2_CID_HUE:
1178 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
1179 return 0;
1180 case V4L2_CID_AUDIO_MUTE:
1181 val = dev->ctl_mute;
1182 return 0;
1183 case V4L2_CID_AUDIO_VOLUME:
1184 val = dev->ctl_volume;
1185 return 0;
1186 default:
1187 return -EINVAL;
1188 }
1189
1190 if (val < 0)
1191 return val;
1192
1193 ctrl->value = val;
1194
1195 return 0;
1196}
1197static int vidioc_s_ctrl(struct file *file, void *priv,
1198 struct v4l2_control *ctrl)
1199{
1200 struct tm6000_fh *fh = priv;
1201 struct tm6000_core *dev = fh->dev;
1202 u8 val = ctrl->value;
1203
1204 switch (ctrl->id) {
1205 case V4L2_CID_CONTRAST:
1206 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1207 return 0;
1208 case V4L2_CID_BRIGHTNESS:
1209 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1210 return 0;
1211 case V4L2_CID_SATURATION:
1212 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1213 return 0;
1214 case V4L2_CID_HUE:
1215 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1216 return 0;
1217 case V4L2_CID_AUDIO_MUTE:
1218 dev->ctl_mute = val;
1219 tm6000_tvaudio_set_mute(dev, val);
1220 return 0;
1221 case V4L2_CID_AUDIO_VOLUME:
1222 dev->ctl_volume = val;
1223 tm6000_set_volume(dev, val);
1224 return 0;
1225 }
1226 return -EINVAL;
1227}
1228
1229static int vidioc_g_tuner(struct file *file, void *priv,
1230 struct v4l2_tuner *t)
1231{
1232 struct tm6000_fh *fh = priv;
1233 struct tm6000_core *dev = fh->dev;
1234
1235 if (unlikely(UNSET == dev->tuner_type))
1236 return -EINVAL;
1237 if (0 != t->index)
1238 return -EINVAL;
1239
1240 strcpy(t->name, "Television");
1241 t->type = V4L2_TUNER_ANALOG_TV;
1242 t->capability = V4L2_TUNER_CAP_NORM;
1243 t->rangehigh = 0xffffffffUL;
1244 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1245
1246 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1247
1248 t->audmode = dev->amode;
1249
1250 return 0;
1251}
1252
1253static int vidioc_s_tuner(struct file *file, void *priv,
1254 struct v4l2_tuner *t)
1255{
1256 struct tm6000_fh *fh = priv;
1257 struct tm6000_core *dev = fh->dev;
1258
1259 if (UNSET == dev->tuner_type)
1260 return -EINVAL;
1261 if (0 != t->index)
1262 return -EINVAL;
1263
1264 dev->amode = t->audmode;
1265 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1266
1267 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1268
1269 return 0;
1270}
1271
1272static int vidioc_g_frequency(struct file *file, void *priv,
1273 struct v4l2_frequency *f)
1274{
1275 struct tm6000_fh *fh = priv;
1276 struct tm6000_core *dev = fh->dev;
1277
1278 if (unlikely(UNSET == dev->tuner_type))
1279 return -EINVAL;
1280
1281 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1282 f->frequency = dev->freq;
1283
1284 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1285
1286 return 0;
1287}
1288
1289static int vidioc_s_frequency(struct file *file, void *priv,
1290 struct v4l2_frequency *f)
1291{
1292 struct tm6000_fh *fh = priv;
1293 struct tm6000_core *dev = fh->dev;
1294
1295 if (unlikely(UNSET == dev->tuner_type))
1296 return -EINVAL;
1297 if (unlikely(f->tuner != 0))
1298 return -EINVAL;
1299 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1300 return -EINVAL;
1301 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1302 return -EINVAL;
1303
1304 dev->freq = f->frequency;
1305 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1306
1307 return 0;
1308}
1309
1310static int radio_querycap(struct file *file, void *priv,
1311 struct v4l2_capability *cap)
1312{
1313 struct tm6000_fh *fh = file->private_data;
1314 struct tm6000_core *dev = fh->dev;
1315
1316 strcpy(cap->driver, "tm6000");
1317 strlcpy(cap->card, dev->name, sizeof(dev->name));
1318 sprintf(cap->bus_info, "USB%04x:%04x",
1319 le16_to_cpu(dev->udev->descriptor.idVendor),
1320 le16_to_cpu(dev->udev->descriptor.idProduct));
1321 cap->version = dev->dev_type;
1322 cap->capabilities = V4L2_CAP_TUNER |
1323 V4L2_CAP_AUDIO |
1324 V4L2_CAP_RADIO |
1325 V4L2_CAP_READWRITE |
1326 V4L2_CAP_STREAMING;
1327
1328 return 0;
1329}
1330
1331static int radio_g_tuner(struct file *file, void *priv,
1332 struct v4l2_tuner *t)
1333{
1334 struct tm6000_fh *fh = file->private_data;
1335 struct tm6000_core *dev = fh->dev;
1336
1337 if (0 != t->index)
1338 return -EINVAL;
1339
1340 memset(t, 0, sizeof(*t));
1341 strcpy(t->name, "Radio");
1342 t->type = V4L2_TUNER_RADIO;
1343 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1344
1345 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1346
1347 return 0;
1348}
1349
1350static int radio_s_tuner(struct file *file, void *priv,
1351 struct v4l2_tuner *t)
1352{
1353 struct tm6000_fh *fh = file->private_data;
1354 struct tm6000_core *dev = fh->dev;
1355
1356 if (0 != t->index)
1357 return -EINVAL;
1358
1359 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1360
1361 return 0;
1362}
1363
1364static int radio_enum_input(struct file *file, void *priv,
1365 struct v4l2_input *i)
1366{
1367 struct tm6000_fh *fh = priv;
1368 struct tm6000_core *dev = fh->dev;
1369
1370 if (i->index != 0)
1371 return -EINVAL;
1372
1373 if (!dev->rinput.type)
1374 return -EINVAL;
1375
1376 strcpy(i->name, "Radio");
1377 i->type = V4L2_INPUT_TYPE_TUNER;
1378
1379 return 0;
1380}
1381
1382static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
1383{
1384 struct tm6000_fh *fh = priv;
1385 struct tm6000_core *dev = fh->dev;
1386
1387 if (dev->input != 5)
1388 return -EINVAL;
1389
1390 *i = dev->input - 5;
1391
1392 return 0;
1393}
1394
1395static int radio_g_audio(struct file *file, void *priv,
1396 struct v4l2_audio *a)
1397{
1398 memset(a, 0, sizeof(*a));
1399 strcpy(a->name, "Radio");
1400 return 0;
1401}
1402
1403static int radio_s_audio(struct file *file, void *priv,
1404 struct v4l2_audio *a)
1405{
1406 return 0;
1407}
1408
1409static int radio_s_input(struct file *filp, void *priv, unsigned int i)
1410{
1411 struct tm6000_fh *fh = priv;
1412 struct tm6000_core *dev = fh->dev;
1413
1414 if (i)
1415 return -EINVAL;
1416
1417 if (!dev->rinput.type)
1418 return -EINVAL;
1419
1420 dev->input = i + 5;
1421
1422 return 0;
1423}
1424
1425static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
1426{
1427 return 0;
1428}
1429
1430static int radio_queryctrl(struct file *file, void *priv,
1431 struct v4l2_queryctrl *c)
1432{
1433 const struct v4l2_queryctrl *ctrl;
1434
1435 if (c->id < V4L2_CID_BASE ||
1436 c->id >= V4L2_CID_LASTP1)
1437 return -EINVAL;
1438 if (c->id == V4L2_CID_AUDIO_MUTE) {
1439 ctrl = ctrl_by_id(c->id);
1440 *c = *ctrl;
1441 } else
1442 *c = no_ctrl;
1443
1444 return 0;
1445}
1446
1447
1448
1449
1450
1451static int tm6000_open(struct file *file)
1452{
1453 struct video_device *vdev = video_devdata(file);
1454 struct tm6000_core *dev = video_drvdata(file);
1455 struct tm6000_fh *fh;
1456 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1457 int i, rc;
1458 int radio = 0;
1459
1460 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1461 video_device_node_name(vdev));
1462
1463 switch (vdev->vfl_type) {
1464 case VFL_TYPE_GRABBER:
1465 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1466 break;
1467 case VFL_TYPE_VBI:
1468 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1469 break;
1470 case VFL_TYPE_RADIO:
1471 radio = 1;
1472 break;
1473 }
1474
1475
1476 dev->users++;
1477
1478 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1479 video_device_node_name(vdev), v4l2_type_names[type],
1480 dev->users);
1481
1482
1483 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1484 if (NULL == fh) {
1485 dev->users--;
1486 return -ENOMEM;
1487 }
1488
1489 file->private_data = fh;
1490 fh->dev = dev;
1491 fh->radio = radio;
1492 dev->radio = radio;
1493 fh->type = type;
1494 dev->fourcc = format[0].fourcc;
1495
1496 fh->fmt = format_by_fourcc(dev->fourcc);
1497
1498 tm6000_get_std_res(dev);
1499
1500 fh->width = dev->width;
1501 fh->height = dev->height;
1502
1503 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1504 "dev->vidq=0x%08lx\n",
1505 (unsigned long)fh, (unsigned long)dev,
1506 (unsigned long)&dev->vidq);
1507 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1508 "queued=%d\n", list_empty(&dev->vidq.queued));
1509 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1510 "active=%d\n", list_empty(&dev->vidq.active));
1511
1512
1513 rc = tm6000_init_analog_mode(dev);
1514 if (rc < 0)
1515 return rc;
1516
1517 if (dev->mode != TM6000_MODE_ANALOG) {
1518
1519 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1520 qctl_regs[i] = tm6000_qctrl[i].default_value;
1521
1522 dev->mode = TM6000_MODE_ANALOG;
1523 }
1524
1525 if (!fh->radio) {
1526 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1527 NULL, &dev->slock,
1528 fh->type,
1529 V4L2_FIELD_INTERLACED,
1530 sizeof(struct tm6000_buffer), fh, &dev->lock);
1531 } else {
1532 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
1533 dev->input = 5;
1534 tm6000_set_audio_rinput(dev);
1535 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1536 tm6000_prepare_isoc(dev);
1537 tm6000_start_thread(dev);
1538 }
1539
1540 return 0;
1541}
1542
1543static ssize_t
1544tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1545{
1546 struct tm6000_fh *fh = file->private_data;
1547
1548 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1549 if (!res_get(fh->dev, fh, true))
1550 return -EBUSY;
1551
1552 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1553 file->f_flags & O_NONBLOCK);
1554 }
1555 return 0;
1556}
1557
1558static unsigned int
1559tm6000_poll(struct file *file, struct poll_table_struct *wait)
1560{
1561 struct tm6000_fh *fh = file->private_data;
1562 struct tm6000_buffer *buf;
1563
1564 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1565 return POLLERR;
1566
1567 if (!!is_res_streaming(fh->dev, fh))
1568 return POLLERR;
1569
1570 if (!is_res_read(fh->dev, fh)) {
1571
1572 if (list_empty(&fh->vb_vidq.stream))
1573 return POLLERR;
1574 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
1575 } else {
1576
1577 return videobuf_poll_stream(file, &fh->vb_vidq, wait);
1578 }
1579 poll_wait(file, &buf->vb.done, wait);
1580 if (buf->vb.state == VIDEOBUF_DONE ||
1581 buf->vb.state == VIDEOBUF_ERROR)
1582 return POLLIN | POLLRDNORM;
1583 return 0;
1584}
1585
1586static int tm6000_release(struct file *file)
1587{
1588 struct tm6000_fh *fh = file->private_data;
1589 struct tm6000_core *dev = fh->dev;
1590 struct video_device *vdev = video_devdata(file);
1591
1592 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1593 video_device_node_name(vdev), dev->users);
1594
1595 dev->users--;
1596
1597 res_free(dev, fh);
1598
1599 if (!dev->users) {
1600 tm6000_uninit_isoc(dev);
1601
1602
1603 tm6000_ir_int_stop(dev);
1604
1605 usb_reset_configuration(dev->udev);
1606
1607 if (dev->int_in.endp)
1608 usb_set_interface(dev->udev,
1609 dev->isoc_in.bInterfaceNumber, 2);
1610 else
1611 usb_set_interface(dev->udev,
1612 dev->isoc_in.bInterfaceNumber, 0);
1613
1614
1615 tm6000_ir_int_start(dev);
1616
1617 if (!fh->radio)
1618 videobuf_mmap_free(&fh->vb_vidq);
1619 }
1620
1621 kfree(fh);
1622
1623 return 0;
1624}
1625
1626static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1627{
1628 struct tm6000_fh *fh = file->private_data;
1629
1630 return videobuf_mmap_mapper(&fh->vb_vidq, vma);
1631}
1632
1633static struct v4l2_file_operations tm6000_fops = {
1634 .owner = THIS_MODULE,
1635 .open = tm6000_open,
1636 .release = tm6000_release,
1637 .unlocked_ioctl = video_ioctl2,
1638 .read = tm6000_read,
1639 .poll = tm6000_poll,
1640 .mmap = tm6000_mmap,
1641};
1642
1643static const struct v4l2_ioctl_ops video_ioctl_ops = {
1644 .vidioc_querycap = vidioc_querycap,
1645 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1646 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1647 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1648 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1649 .vidioc_s_std = vidioc_s_std,
1650 .vidioc_enum_input = vidioc_enum_input,
1651 .vidioc_g_input = vidioc_g_input,
1652 .vidioc_s_input = vidioc_s_input,
1653 .vidioc_queryctrl = vidioc_queryctrl,
1654 .vidioc_g_ctrl = vidioc_g_ctrl,
1655 .vidioc_s_ctrl = vidioc_s_ctrl,
1656 .vidioc_g_tuner = vidioc_g_tuner,
1657 .vidioc_s_tuner = vidioc_s_tuner,
1658 .vidioc_g_frequency = vidioc_g_frequency,
1659 .vidioc_s_frequency = vidioc_s_frequency,
1660 .vidioc_streamon = vidioc_streamon,
1661 .vidioc_streamoff = vidioc_streamoff,
1662 .vidioc_reqbufs = vidioc_reqbufs,
1663 .vidioc_querybuf = vidioc_querybuf,
1664 .vidioc_qbuf = vidioc_qbuf,
1665 .vidioc_dqbuf = vidioc_dqbuf,
1666};
1667
1668static struct video_device tm6000_template = {
1669 .name = "tm6000",
1670 .fops = &tm6000_fops,
1671 .ioctl_ops = &video_ioctl_ops,
1672 .release = video_device_release,
1673 .tvnorms = TM6000_STD,
1674 .current_norm = V4L2_STD_NTSC_M,
1675};
1676
1677static const struct v4l2_file_operations radio_fops = {
1678 .owner = THIS_MODULE,
1679 .open = tm6000_open,
1680 .release = tm6000_release,
1681 .unlocked_ioctl = video_ioctl2,
1682};
1683
1684static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1685 .vidioc_querycap = radio_querycap,
1686 .vidioc_g_tuner = radio_g_tuner,
1687 .vidioc_enum_input = radio_enum_input,
1688 .vidioc_g_audio = radio_g_audio,
1689 .vidioc_s_tuner = radio_s_tuner,
1690 .vidioc_s_audio = radio_s_audio,
1691 .vidioc_s_input = radio_s_input,
1692 .vidioc_s_std = radio_s_std,
1693 .vidioc_queryctrl = radio_queryctrl,
1694 .vidioc_g_input = radio_g_input,
1695 .vidioc_g_ctrl = vidioc_g_ctrl,
1696 .vidioc_s_ctrl = vidioc_s_ctrl,
1697 .vidioc_g_frequency = vidioc_g_frequency,
1698 .vidioc_s_frequency = vidioc_s_frequency,
1699};
1700
1701static struct video_device tm6000_radio_template = {
1702 .name = "tm6000",
1703 .fops = &radio_fops,
1704 .ioctl_ops = &radio_ioctl_ops,
1705};
1706
1707
1708
1709
1710
1711
1712static struct video_device *vdev_init(struct tm6000_core *dev,
1713 const struct video_device
1714 *template, const char *type_name)
1715{
1716 struct video_device *vfd;
1717
1718 vfd = video_device_alloc();
1719 if (NULL == vfd)
1720 return NULL;
1721
1722 *vfd = *template;
1723 vfd->v4l2_dev = &dev->v4l2_dev;
1724 vfd->release = video_device_release;
1725 vfd->debug = tm6000_debug;
1726 vfd->lock = &dev->lock;
1727
1728
1729
1730 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
1731
1732 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1733
1734 video_set_drvdata(vfd, dev);
1735 return vfd;
1736}
1737
1738int tm6000_v4l2_register(struct tm6000_core *dev)
1739{
1740 int ret = -1;
1741
1742 dev->vfd = vdev_init(dev, &tm6000_template, "video");
1743
1744 if (!dev->vfd) {
1745 printk(KERN_INFO "%s: can't register video device\n",
1746 dev->name);
1747 return -ENOMEM;
1748 }
1749
1750
1751 INIT_LIST_HEAD(&dev->vidq.active);
1752 INIT_LIST_HEAD(&dev->vidq.queued);
1753
1754 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
1755
1756 if (ret < 0) {
1757 printk(KERN_INFO "%s: can't register video device\n",
1758 dev->name);
1759 return ret;
1760 }
1761
1762 printk(KERN_INFO "%s: registered device %s\n",
1763 dev->name, video_device_node_name(dev->vfd));
1764
1765 if (dev->caps.has_radio) {
1766 dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
1767 "radio");
1768 if (!dev->radio_dev) {
1769 printk(KERN_INFO "%s: can't register radio device\n",
1770 dev->name);
1771 return ret;
1772 }
1773
1774 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1775 radio_nr);
1776 if (ret < 0) {
1777 printk(KERN_INFO "%s: can't register radio device\n",
1778 dev->name);
1779 return ret;
1780 }
1781
1782 printk(KERN_INFO "%s: registered device %s\n",
1783 dev->name, video_device_node_name(dev->radio_dev));
1784 }
1785
1786 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1787 return ret;
1788}
1789
1790int tm6000_v4l2_unregister(struct tm6000_core *dev)
1791{
1792 video_unregister_device(dev->vfd);
1793
1794 if (dev->radio_dev) {
1795 if (video_is_registered(dev->radio_dev))
1796 video_unregister_device(dev->radio_dev);
1797 else
1798 video_device_release(dev->radio_dev);
1799 dev->radio_dev = NULL;
1800 }
1801
1802 return 0;
1803}
1804
1805int tm6000_v4l2_exit(void)
1806{
1807 return 0;
1808}
1809
1810module_param(video_nr, int, 0);
1811MODULE_PARM_DESC(video_nr, "Allow changing video device number");
1812
1813module_param_named(debug, tm6000_debug, int, 0444);
1814MODULE_PARM_DESC(debug, "activates debug info");
1815
1816module_param(vid_limit, int, 0644);
1817MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
1818
1819