1 // SPDX-License-Identifier: GPL-2.0
2 // tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 // Copyright (c) 2006-2007 Mauro Carvalho Chehab <mchehab@kernel.org>
6 // Copyright (c) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 // - Fixed module load/unload
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/random.h>
20 #include <linux/usb.h>
21 #include <linux/videodev2.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-event.h>
24 #include <media/tuner.h>
25 #include <linux/interrupt.h>
26 #include <linux/kthread.h>
27 #include <linux/highmem.h>
28 #include <linux/freezer.h>
30 #include "tm6000-regs.h"
33 #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
35 /* Limits minimum and default number of buffers */
36 #define TM6000_MIN_BUF 4
37 #define TM6000_DEF_BUF 8
39 #define TM6000_NUM_URB_BUF 8
41 #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
43 /* Declare static vars that will be used as parameters */
44 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
45 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
46 static int radio_nr
= -1; /* /dev/radioN, -1 for autodetect */
47 static bool keep_urb
; /* keep urb buffers allocated */
51 EXPORT_SYMBOL_GPL(tm6000_debug
);
53 static struct tm6000_fmt format
[] = {
55 .name
= "4:2:2, packed, YVY2",
56 .fourcc
= V4L2_PIX_FMT_YUYV
,
59 .name
= "4:2:2, packed, UYVY",
60 .fourcc
= V4L2_PIX_FMT_UYVY
,
63 .name
= "A/V + VBI mux packet",
64 .fourcc
= V4L2_PIX_FMT_TM6000
,
69 /* ------------------------------------------------------------------
70 * DMA and thread functions
71 * ------------------------------------------------------------------
74 #define norm_maxw(a) 720
75 #define norm_maxh(a) 576
77 #define norm_minw(a) norm_maxw(a)
78 #define norm_minh(a) norm_maxh(a)
81 * video-buf generic routine to get the next available buffer
83 static inline void get_next_buf(struct tm6000_dmaqueue
*dma_q
,
84 struct tm6000_buffer
**buf
)
86 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
88 if (list_empty(&dma_q
->active
)) {
89 dprintk(dev
, V4L2_DEBUG_QUEUE
, "No active queue to serve\n");
94 *buf
= list_entry(dma_q
->active
.next
,
95 struct tm6000_buffer
, vb
.queue
);
99 * Announces that a buffer were filled and request the next
101 static inline void buffer_filled(struct tm6000_core
*dev
,
102 struct tm6000_dmaqueue
*dma_q
,
103 struct tm6000_buffer
*buf
)
105 /* Advice that buffer was filled */
106 dprintk(dev
, V4L2_DEBUG_ISOC
, "[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
107 buf
->vb
.state
= VIDEOBUF_DONE
;
108 buf
->vb
.field_count
++;
109 v4l2_get_timestamp(&buf
->vb
.ts
);
111 list_del(&buf
->vb
.queue
);
112 wake_up(&buf
->vb
.done
);
116 * Identify the tm5600/6000 buffer header type and properly handles
118 static int copy_streams(u8
*data
, unsigned long len
,
121 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
122 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
123 u8
*ptr
= data
, *endp
= data
+len
;
124 unsigned long header
= 0;
126 unsigned int cmd
, cpysize
, pktsize
, size
, field
, block
, line
, pos
= 0;
127 struct tm6000_buffer
*vbuf
= NULL
;
129 unsigned int linewidth
;
132 /* get video buffer */
133 get_next_buf(dma_q
, &vbuf
);
137 voutp
= videobuf_to_vmalloc(&vbuf
->vb
);
143 for (ptr
= data
; ptr
< endp
;) {
144 if (!dev
->isoc_ctl
.cmd
) {
146 if (dev
->isoc_ctl
.tmp_buf_len
> 0) {
147 /* from last urb or packet */
148 header
= dev
->isoc_ctl
.tmp_buf
;
149 if (4 - dev
->isoc_ctl
.tmp_buf_len
> 0) {
150 memcpy((u8
*)&header
+
151 dev
->isoc_ctl
.tmp_buf_len
,
153 4 - dev
->isoc_ctl
.tmp_buf_len
);
154 ptr
+= 4 - dev
->isoc_ctl
.tmp_buf_len
;
156 dev
->isoc_ctl
.tmp_buf_len
= 0;
158 if (ptr
+ 3 >= endp
) {
159 /* have incomplete header */
160 dev
->isoc_ctl
.tmp_buf_len
= endp
- ptr
;
161 memcpy(&dev
->isoc_ctl
.tmp_buf
, ptr
,
162 dev
->isoc_ctl
.tmp_buf_len
);
166 for (; ptr
< endp
- 3; ptr
++) {
167 if (*(ptr
+ 3) == 0x47)
170 /* Get message header */
171 header
= *(unsigned long *)ptr
;
175 /* split the header fields */
176 size
= ((header
& 0x7e) << 1);
179 block
= (header
>> 7) & 0xf;
180 field
= (header
>> 11) & 0x1;
181 line
= (header
>> 12) & 0x1ff;
182 cmd
= (header
>> 21) & 0x7;
183 /* Validates haeder fields */
184 if (size
> TM6000_URB_MSG_LEN
)
185 size
= TM6000_URB_MSG_LEN
;
186 pktsize
= TM6000_URB_MSG_LEN
;
188 * calculate position in buffer and change the buffer
191 case TM6000_URB_MSG_VIDEO
:
193 if ((dev
->isoc_ctl
.vfield
!= field
) &&
196 * Announces that a new buffer
199 buffer_filled(dev
, dma_q
, vbuf
);
200 dprintk(dev
, V4L2_DEBUG_ISOC
,
201 "new buffer filled\n");
202 get_next_buf(dma_q
, &vbuf
);
205 voutp
= videobuf_to_vmalloc(&vbuf
->vb
);
208 memset(voutp
, 0, vbuf
->vb
.size
);
210 linewidth
= vbuf
->vb
.width
<< 1;
211 pos
= ((line
<< 1) - field
- 1) *
212 linewidth
+ block
* TM6000_URB_MSG_LEN
;
213 /* Don't allow to write out of the buffer */
214 if (pos
+ size
> vbuf
->vb
.size
)
215 cmd
= TM6000_URB_MSG_ERR
;
216 dev
->isoc_ctl
.vfield
= field
;
219 case TM6000_URB_MSG_VBI
:
221 case TM6000_URB_MSG_AUDIO
:
222 case TM6000_URB_MSG_PTS
:
223 size
= pktsize
; /* Size is always 180 bytes */
227 /* Continue the last copy */
228 cmd
= dev
->isoc_ctl
.cmd
;
229 size
= dev
->isoc_ctl
.size
;
230 pos
= dev
->isoc_ctl
.pos
;
231 pktsize
= dev
->isoc_ctl
.pktsize
;
232 field
= dev
->isoc_ctl
.field
;
234 cpysize
= (endp
- ptr
> size
) ? size
: endp
- ptr
;
236 /* copy data in different buffers */
238 case TM6000_URB_MSG_VIDEO
:
239 /* Fills video buffer */
241 memcpy(&voutp
[pos
], ptr
, cpysize
);
243 case TM6000_URB_MSG_AUDIO
: {
245 for (i
= 0; i
< cpysize
; i
+= 2)
246 swab16s((u16
*)(ptr
+ i
));
248 tm6000_call_fillbuf(dev
, TM6000_AUDIO
, ptr
, cpysize
);
251 case TM6000_URB_MSG_VBI
:
252 /* Need some code to copy vbi buffer */
254 case TM6000_URB_MSG_PTS
: {
255 /* Need some code to copy pts */
258 dprintk(dev
, V4L2_DEBUG_ISOC
, "field %d, PTS %x",
264 if (ptr
+ pktsize
> endp
) {
266 * End of URB packet, but cmd processing is not
267 * complete. Preserve the state for a next packet
269 dev
->isoc_ctl
.pos
= pos
+ cpysize
;
270 dev
->isoc_ctl
.size
= size
- cpysize
;
271 dev
->isoc_ctl
.cmd
= cmd
;
272 dev
->isoc_ctl
.field
= field
;
273 dev
->isoc_ctl
.pktsize
= pktsize
- (endp
- ptr
);
276 dev
->isoc_ctl
.cmd
= 0;
284 * Identify the tm5600/6000 buffer header type and properly handles
286 static int copy_multiplexed(u8
*ptr
, unsigned long len
,
289 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
290 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
291 unsigned int pos
= dev
->isoc_ctl
.pos
, cpysize
;
293 struct tm6000_buffer
*buf
;
296 get_next_buf(dma_q
, &buf
);
298 outp
= videobuf_to_vmalloc(&buf
->vb
);
304 cpysize
= min(len
, buf
->vb
.size
-pos
);
305 memcpy(&outp
[pos
], ptr
, cpysize
);
309 if (pos
>= buf
->vb
.size
) {
311 /* Announces that a new buffer were filled */
312 buffer_filled(dev
, dma_q
, buf
);
313 dprintk(dev
, V4L2_DEBUG_ISOC
, "new buffer filled\n");
314 get_next_buf(dma_q
, &buf
);
317 outp
= videobuf_to_vmalloc(&(buf
->vb
));
324 dev
->isoc_ctl
.pos
= pos
;
328 static inline void print_err_status(struct tm6000_core
*dev
,
329 int packet
, int status
)
331 char *errmsg
= "Unknown";
335 errmsg
= "unlinked synchronously";
338 errmsg
= "unlinked asynchronously";
341 errmsg
= "Buffer error (overrun)";
344 errmsg
= "Stalled (device not responding)";
347 errmsg
= "Babble (bad cable?)";
350 errmsg
= "Bit-stuff error (bad cable?)";
353 errmsg
= "CRC/Timeout (could be anything)";
356 errmsg
= "Device does not respond";
360 dprintk(dev
, V4L2_DEBUG_QUEUE
, "URB status %d [%s].\n",
363 dprintk(dev
, V4L2_DEBUG_QUEUE
, "URB packet %d, status %d [%s].\n",
364 packet
, status
, errmsg
);
370 * Controls the isoc copy of each urb packet
372 static inline int tm6000_isoc_copy(struct urb
*urb
)
374 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
375 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
376 int i
, len
= 0, rc
= 1, status
;
379 if (urb
->status
< 0) {
380 print_err_status(dev
, -1, urb
->status
);
384 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
385 status
= urb
->iso_frame_desc
[i
].status
;
388 print_err_status(dev
, i
, status
);
392 len
= urb
->iso_frame_desc
[i
].actual_length
;
395 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
396 if (!urb
->iso_frame_desc
[i
].status
) {
397 if ((dev
->fourcc
) == V4L2_PIX_FMT_TM6000
) {
398 rc
= copy_multiplexed(p
, len
, urb
);
402 copy_streams(p
, len
, urb
);
410 /* ------------------------------------------------------------------
412 * ------------------------------------------------------------------
416 * IRQ callback, called by URB callback
418 static void tm6000_irq_callback(struct urb
*urb
)
420 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
421 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
425 switch (urb
->status
) {
436 tm6000_err("urb completion error %d.\n", urb
->status
);
440 spin_lock_irqsave(&dev
->slock
, flags
);
441 tm6000_isoc_copy(urb
);
442 spin_unlock_irqrestore(&dev
->slock
, flags
);
444 /* Reset urb buffers */
445 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
446 urb
->iso_frame_desc
[i
].status
= 0;
447 urb
->iso_frame_desc
[i
].actual_length
= 0;
450 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
452 tm6000_err("urb resubmit failed (error=%i)\n",
457 * Allocate URB buffers
459 static int tm6000_alloc_urb_buffers(struct tm6000_core
*dev
)
461 int num_bufs
= TM6000_NUM_URB_BUF
;
467 dev
->urb_buffer
= kmalloc_array(num_bufs
, sizeof(void *), GFP_KERNEL
);
468 if (!dev
->urb_buffer
)
471 dev
->urb_dma
= kmalloc_array(num_bufs
, sizeof(dma_addr_t
*),
476 for (i
= 0; i
< num_bufs
; i
++) {
477 dev
->urb_buffer
[i
] = usb_alloc_coherent(
478 dev
->udev
, dev
->urb_size
,
479 GFP_KERNEL
, &dev
->urb_dma
[i
]);
480 if (!dev
->urb_buffer
[i
]) {
481 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
485 memset(dev
->urb_buffer
[i
], 0, dev
->urb_size
);
494 static int tm6000_free_urb_buffers(struct tm6000_core
*dev
)
498 if (!dev
->urb_buffer
)
501 for (i
= 0; i
< TM6000_NUM_URB_BUF
; i
++) {
502 if (dev
->urb_buffer
[i
]) {
503 usb_free_coherent(dev
->udev
,
507 dev
->urb_buffer
[i
] = NULL
;
510 kfree(dev
->urb_buffer
);
512 dev
->urb_buffer
= NULL
;
519 * Stop and Deallocate URBs
521 static void tm6000_uninit_isoc(struct tm6000_core
*dev
)
526 dev
->isoc_ctl
.buf
= NULL
;
527 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
528 urb
= dev
->isoc_ctl
.urb
[i
];
533 dev
->isoc_ctl
.urb
[i
] = NULL
;
535 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
539 tm6000_free_urb_buffers(dev
);
541 kfree(dev
->isoc_ctl
.urb
);
542 kfree(dev
->isoc_ctl
.transfer_buffer
);
544 dev
->isoc_ctl
.urb
= NULL
;
545 dev
->isoc_ctl
.transfer_buffer
= NULL
;
546 dev
->isoc_ctl
.num_bufs
= 0;
550 * Assign URBs and start IRQ
552 static int tm6000_prepare_isoc(struct tm6000_core
*dev
)
554 struct tm6000_dmaqueue
*dma_q
= &dev
->vidq
;
555 int i
, j
, sb_size
, pipe
, size
, max_packets
;
556 int num_bufs
= TM6000_NUM_URB_BUF
;
559 /* De-allocates all pending stuff */
560 tm6000_uninit_isoc(dev
);
561 /* Stop interrupt USB pipe */
562 tm6000_ir_int_stop(dev
);
564 usb_set_interface(dev
->udev
,
565 dev
->isoc_in
.bInterfaceNumber
,
566 dev
->isoc_in
.bAlternateSetting
);
568 /* Start interrupt USB pipe */
569 tm6000_ir_int_start(dev
);
571 pipe
= usb_rcvisocpipe(dev
->udev
,
572 dev
->isoc_in
.endp
->desc
.bEndpointAddress
&
573 USB_ENDPOINT_NUMBER_MASK
);
575 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
577 if (size
> dev
->isoc_in
.maxsize
)
578 size
= dev
->isoc_in
.maxsize
;
580 dev
->isoc_ctl
.max_pkt_size
= size
;
582 max_packets
= TM6000_MAX_ISO_PACKETS
;
583 sb_size
= max_packets
* size
;
584 dev
->urb_size
= sb_size
;
586 dev
->isoc_ctl
.num_bufs
= num_bufs
;
588 dev
->isoc_ctl
.urb
= kmalloc_array(num_bufs
, sizeof(void *),
590 if (!dev
->isoc_ctl
.urb
)
593 dev
->isoc_ctl
.transfer_buffer
= kmalloc_array(num_bufs
,
596 if (!dev
->isoc_ctl
.transfer_buffer
) {
597 kfree(dev
->isoc_ctl
.urb
);
601 dprintk(dev
, V4L2_DEBUG_QUEUE
, "Allocating %d x %d packets (%d bytes) of %d bytes each to handle %u size\n",
602 max_packets
, num_bufs
, sb_size
,
603 dev
->isoc_in
.maxsize
, size
);
606 if (tm6000_alloc_urb_buffers(dev
) < 0) {
607 tm6000_err("cannot allocate memory for urb buffers\n");
609 /* call free, as some buffers might have been allocated */
610 tm6000_free_urb_buffers(dev
);
611 kfree(dev
->isoc_ctl
.urb
);
612 kfree(dev
->isoc_ctl
.transfer_buffer
);
616 /* allocate urbs and transfer buffers */
617 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
618 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
620 tm6000_uninit_isoc(dev
);
621 tm6000_free_urb_buffers(dev
);
624 dev
->isoc_ctl
.urb
[i
] = urb
;
626 urb
->transfer_dma
= dev
->urb_dma
[i
];
627 dev
->isoc_ctl
.transfer_buffer
[i
] = dev
->urb_buffer
[i
];
629 usb_fill_bulk_urb(urb
, dev
->udev
, pipe
,
630 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
631 tm6000_irq_callback
, dma_q
);
632 urb
->interval
= dev
->isoc_in
.endp
->desc
.bInterval
;
633 urb
->number_of_packets
= max_packets
;
634 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
636 for (j
= 0; j
< max_packets
; j
++) {
637 urb
->iso_frame_desc
[j
].offset
= size
* j
;
638 urb
->iso_frame_desc
[j
].length
= size
;
645 static int tm6000_start_thread(struct tm6000_core
*dev
)
647 struct tm6000_dmaqueue
*dma_q
= &dev
->vidq
;
651 dma_q
->ini_jiffies
= jiffies
;
653 init_waitqueue_head(&dma_q
->wq
);
655 /* submit urbs and enables IRQ */
656 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
657 int rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
659 tm6000_err("submit of urb %i failed (error=%i)\n", i
,
661 tm6000_uninit_isoc(dev
);
669 /* ------------------------------------------------------------------
670 * Videobuf operations
671 * ------------------------------------------------------------------
675 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
677 struct tm6000_fh
*fh
= vq
->priv_data
;
679 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
681 *count
= TM6000_DEF_BUF
;
683 if (*count
< TM6000_MIN_BUF
)
684 *count
= TM6000_MIN_BUF
;
686 while (*size
* *count
> vid_limit
* 1024 * 1024)
692 static void free_buffer(struct videobuf_queue
*vq
, struct tm6000_buffer
*buf
)
694 struct tm6000_fh
*fh
= vq
->priv_data
;
695 struct tm6000_core
*dev
= fh
->dev
;
698 BUG_ON(in_interrupt());
700 /* We used to wait for the buffer to finish here, but this didn't work
701 because, as we were keeping the state as VIDEOBUF_QUEUED,
702 videobuf_queue_cancel marked it as finished for us.
703 (Also, it could wedge forever if the hardware was misconfigured.)
705 This should be safe; by the time we get here, the buffer isn't
706 queued anymore. If we ever start marking the buffers as
707 VIDEOBUF_ACTIVE, it won't be, though.
709 spin_lock_irqsave(&dev
->slock
, flags
);
710 if (dev
->isoc_ctl
.buf
== buf
)
711 dev
->isoc_ctl
.buf
= NULL
;
712 spin_unlock_irqrestore(&dev
->slock
, flags
);
714 videobuf_vmalloc_free(&buf
->vb
);
715 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
719 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
720 enum v4l2_field field
)
722 struct tm6000_fh
*fh
= vq
->priv_data
;
723 struct tm6000_buffer
*buf
= container_of(vb
, struct tm6000_buffer
, vb
);
724 struct tm6000_core
*dev
= fh
->dev
;
727 BUG_ON(NULL
== fh
->fmt
);
730 /* FIXME: It assumes depth=2 */
731 /* The only currently supported format is 16 bits/pixel */
732 buf
->vb
.size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
733 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
736 if (buf
->fmt
!= fh
->fmt
||
737 buf
->vb
.width
!= fh
->width
||
738 buf
->vb
.height
!= fh
->height
||
739 buf
->vb
.field
!= field
) {
741 buf
->vb
.width
= fh
->width
;
742 buf
->vb
.height
= fh
->height
;
743 buf
->vb
.field
= field
;
744 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
747 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
748 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
753 if (!dev
->isoc_ctl
.num_bufs
) {
754 rc
= tm6000_prepare_isoc(dev
);
758 rc
= tm6000_start_thread(dev
);
764 buf
->vb
.state
= VIDEOBUF_PREPARED
;
768 free_buffer(vq
, buf
);
773 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
775 struct tm6000_buffer
*buf
= container_of(vb
, struct tm6000_buffer
, vb
);
776 struct tm6000_fh
*fh
= vq
->priv_data
;
777 struct tm6000_core
*dev
= fh
->dev
;
778 struct tm6000_dmaqueue
*vidq
= &dev
->vidq
;
780 buf
->vb
.state
= VIDEOBUF_QUEUED
;
781 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
784 static void buffer_release(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
786 struct tm6000_buffer
*buf
= container_of(vb
, struct tm6000_buffer
, vb
);
788 free_buffer(vq
, buf
);
791 static const struct videobuf_queue_ops tm6000_video_qops
= {
792 .buf_setup
= buffer_setup
,
793 .buf_prepare
= buffer_prepare
,
794 .buf_queue
= buffer_queue
,
795 .buf_release
= buffer_release
,
798 /* ------------------------------------------------------------------
800 * ------------------------------------------------------------------
803 static bool is_res_read(struct tm6000_core
*dev
, struct tm6000_fh
*fh
)
805 /* Is the current fh handling it? if so, that's OK */
806 if (dev
->resources
== fh
&& dev
->is_res_read
)
812 static bool is_res_streaming(struct tm6000_core
*dev
, struct tm6000_fh
*fh
)
814 /* Is the current fh handling it? if so, that's OK */
815 if (dev
->resources
== fh
)
821 static bool res_get(struct tm6000_core
*dev
, struct tm6000_fh
*fh
,
824 /* Is the current fh handling it? if so, that's OK */
825 if (dev
->resources
== fh
&& dev
->is_res_read
== is_res_read
)
834 dev
->is_res_read
= is_res_read
;
835 dprintk(dev
, V4L2_DEBUG_RES_LOCK
, "res: get\n");
839 static void res_free(struct tm6000_core
*dev
, struct tm6000_fh
*fh
)
841 /* Is the current fh handling it? if so, that's OK */
842 if (dev
->resources
!= fh
)
845 dev
->resources
= NULL
;
846 dprintk(dev
, V4L2_DEBUG_RES_LOCK
, "res: put\n");
849 /* ------------------------------------------------------------------
850 * IOCTL vidioc handling
851 * ------------------------------------------------------------------
853 static int vidioc_querycap(struct file
*file
, void *priv
,
854 struct v4l2_capability
*cap
)
856 struct tm6000_core
*dev
= ((struct tm6000_fh
*)priv
)->dev
;
857 struct video_device
*vdev
= video_devdata(file
);
859 strlcpy(cap
->driver
, "tm6000", sizeof(cap
->driver
));
860 strlcpy(cap
->card
, "Trident TVMaster TM5600/6000/6010", sizeof(cap
->card
));
861 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
862 if (dev
->tuner_type
!= TUNER_ABSENT
)
863 cap
->device_caps
|= V4L2_CAP_TUNER
;
864 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
865 cap
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
|
869 cap
->device_caps
|= V4L2_CAP_RADIO
;
870 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
|
871 V4L2_CAP_RADIO
| V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
;
876 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
877 struct v4l2_fmtdesc
*f
)
879 if (f
->index
>= ARRAY_SIZE(format
))
882 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
883 f
->pixelformat
= format
[f
->index
].fourcc
;
887 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
888 struct v4l2_format
*f
)
890 struct tm6000_fh
*fh
= priv
;
892 f
->fmt
.pix
.width
= fh
->width
;
893 f
->fmt
.pix
.height
= fh
->height
;
894 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
895 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
896 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
897 f
->fmt
.pix
.bytesperline
=
898 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
899 f
->fmt
.pix
.sizeimage
=
900 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
905 static struct tm6000_fmt
*format_by_fourcc(unsigned int fourcc
)
909 for (i
= 0; i
< ARRAY_SIZE(format
); i
++)
910 if (format
[i
].fourcc
== fourcc
)
915 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
916 struct v4l2_format
*f
)
918 struct tm6000_core
*dev
= ((struct tm6000_fh
*)priv
)->dev
;
919 struct tm6000_fmt
*fmt
;
920 enum v4l2_field field
;
922 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
924 dprintk(dev
, 2, "Fourcc format (0x%08x) invalid.\n",
925 f
->fmt
.pix
.pixelformat
);
929 field
= f
->fmt
.pix
.field
;
931 field
= V4L2_FIELD_INTERLACED
;
933 tm6000_get_std_res(dev
);
935 f
->fmt
.pix
.width
= dev
->width
;
936 f
->fmt
.pix
.height
= dev
->height
;
938 f
->fmt
.pix
.width
&= ~0x01;
940 f
->fmt
.pix
.field
= field
;
942 f
->fmt
.pix
.bytesperline
=
943 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
944 f
->fmt
.pix
.sizeimage
=
945 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
946 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
951 /*FIXME: This seems to be generic enough to be at videodev2 */
952 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
953 struct v4l2_format
*f
)
955 struct tm6000_fh
*fh
= priv
;
956 struct tm6000_core
*dev
= fh
->dev
;
957 int ret
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
961 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
962 fh
->width
= f
->fmt
.pix
.width
;
963 fh
->height
= f
->fmt
.pix
.height
;
964 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
967 dev
->fourcc
= f
->fmt
.pix
.pixelformat
;
969 tm6000_set_fourcc_format(dev
);
974 static int vidioc_reqbufs(struct file
*file
, void *priv
,
975 struct v4l2_requestbuffers
*p
)
977 struct tm6000_fh
*fh
= priv
;
979 return videobuf_reqbufs(&fh
->vb_vidq
, p
);
982 static int vidioc_querybuf(struct file
*file
, void *priv
,
983 struct v4l2_buffer
*p
)
985 struct tm6000_fh
*fh
= priv
;
987 return videobuf_querybuf(&fh
->vb_vidq
, p
);
990 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
992 struct tm6000_fh
*fh
= priv
;
994 return videobuf_qbuf(&fh
->vb_vidq
, p
);
997 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
999 struct tm6000_fh
*fh
= priv
;
1001 return videobuf_dqbuf(&fh
->vb_vidq
, p
,
1002 file
->f_flags
& O_NONBLOCK
);
1005 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1007 struct tm6000_fh
*fh
= priv
;
1008 struct tm6000_core
*dev
= fh
->dev
;
1010 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1015 if (!res_get(dev
, fh
, false))
1017 return videobuf_streamon(&fh
->vb_vidq
);
1020 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1022 struct tm6000_fh
*fh
= priv
;
1023 struct tm6000_core
*dev
= fh
->dev
;
1025 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1031 videobuf_streamoff(&fh
->vb_vidq
);
1037 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
1040 struct tm6000_fh
*fh
= priv
;
1041 struct tm6000_core
*dev
= fh
->dev
;
1044 rc
= tm6000_init_analog_mode(dev
);
1046 fh
->width
= dev
->width
;
1047 fh
->height
= dev
->height
;
1052 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_std
, dev
->norm
);
1057 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1059 struct tm6000_fh
*fh
= priv
;
1060 struct tm6000_core
*dev
= fh
->dev
;
1066 static const char *iname
[] = {
1067 [TM6000_INPUT_TV
] = "Television",
1068 [TM6000_INPUT_COMPOSITE1
] = "Composite 1",
1069 [TM6000_INPUT_COMPOSITE2
] = "Composite 2",
1070 [TM6000_INPUT_SVIDEO
] = "S-Video",
1073 static int vidioc_enum_input(struct file
*file
, void *priv
,
1074 struct v4l2_input
*i
)
1076 struct tm6000_fh
*fh
= priv
;
1077 struct tm6000_core
*dev
= fh
->dev
;
1084 if (!dev
->vinput
[n
].type
)
1089 if (dev
->vinput
[n
].type
== TM6000_INPUT_TV
)
1090 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1092 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1094 strcpy(i
->name
, iname
[dev
->vinput
[n
].type
]);
1096 i
->std
= TM6000_STD
;
1101 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1103 struct tm6000_fh
*fh
= priv
;
1104 struct tm6000_core
*dev
= fh
->dev
;
1111 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1113 struct tm6000_fh
*fh
= priv
;
1114 struct tm6000_core
*dev
= fh
->dev
;
1119 if (!dev
->vinput
[i
].type
)
1124 rc
= vidioc_s_std(file
, priv
, dev
->norm
);
1129 /* --- controls ---------------------------------------------- */
1131 static int tm6000_s_ctrl(struct v4l2_ctrl
*ctrl
)
1133 struct tm6000_core
*dev
= container_of(ctrl
->handler
, struct tm6000_core
, ctrl_handler
);
1137 case V4L2_CID_CONTRAST
:
1138 tm6000_set_reg(dev
, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ
, val
);
1140 case V4L2_CID_BRIGHTNESS
:
1141 tm6000_set_reg(dev
, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ
, val
);
1143 case V4L2_CID_SATURATION
:
1144 tm6000_set_reg(dev
, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ
, val
);
1147 tm6000_set_reg(dev
, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ
, val
);
1153 static const struct v4l2_ctrl_ops tm6000_ctrl_ops
= {
1154 .s_ctrl
= tm6000_s_ctrl
,
1157 static int tm6000_radio_s_ctrl(struct v4l2_ctrl
*ctrl
)
1159 struct tm6000_core
*dev
= container_of(ctrl
->handler
,
1160 struct tm6000_core
, radio_ctrl_handler
);
1164 case V4L2_CID_AUDIO_MUTE
:
1165 dev
->ctl_mute
= val
;
1166 tm6000_tvaudio_set_mute(dev
, val
);
1168 case V4L2_CID_AUDIO_VOLUME
:
1169 dev
->ctl_volume
= val
;
1170 tm6000_set_volume(dev
, val
);
1176 static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops
= {
1177 .s_ctrl
= tm6000_radio_s_ctrl
,
1180 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1181 struct v4l2_tuner
*t
)
1183 struct tm6000_fh
*fh
= priv
;
1184 struct tm6000_core
*dev
= fh
->dev
;
1186 if (UNSET
== dev
->tuner_type
)
1191 strcpy(t
->name
, "Television");
1192 t
->type
= V4L2_TUNER_ANALOG_TV
;
1193 t
->capability
= V4L2_TUNER_CAP_NORM
| V4L2_TUNER_CAP_STEREO
;
1194 t
->rangehigh
= 0xffffffffUL
;
1195 t
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1197 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1199 t
->audmode
= dev
->amode
;
1204 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1205 const struct v4l2_tuner
*t
)
1207 struct tm6000_fh
*fh
= priv
;
1208 struct tm6000_core
*dev
= fh
->dev
;
1210 if (UNSET
== dev
->tuner_type
)
1215 if (t
->audmode
> V4L2_TUNER_MODE_STEREO
)
1216 dev
->amode
= V4L2_TUNER_MODE_STEREO
;
1218 dev
->amode
= t
->audmode
;
1219 dprintk(dev
, 3, "audio mode: %x\n", t
->audmode
);
1221 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1226 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1227 struct v4l2_frequency
*f
)
1229 struct tm6000_fh
*fh
= priv
;
1230 struct tm6000_core
*dev
= fh
->dev
;
1232 if (UNSET
== dev
->tuner_type
)
1237 f
->frequency
= dev
->freq
;
1239 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, f
);
1244 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1245 const struct v4l2_frequency
*f
)
1247 struct tm6000_fh
*fh
= priv
;
1248 struct tm6000_core
*dev
= fh
->dev
;
1250 if (UNSET
== dev
->tuner_type
)
1255 dev
->freq
= f
->frequency
;
1256 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, f
);
1261 static int radio_g_tuner(struct file
*file
, void *priv
,
1262 struct v4l2_tuner
*t
)
1264 struct tm6000_fh
*fh
= file
->private_data
;
1265 struct tm6000_core
*dev
= fh
->dev
;
1270 memset(t
, 0, sizeof(*t
));
1271 strcpy(t
->name
, "Radio");
1272 t
->type
= V4L2_TUNER_RADIO
;
1273 t
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
;
1274 t
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1275 t
->audmode
= V4L2_TUNER_MODE_STEREO
;
1277 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1282 static int radio_s_tuner(struct file
*file
, void *priv
,
1283 const struct v4l2_tuner
*t
)
1285 struct tm6000_fh
*fh
= file
->private_data
;
1286 struct tm6000_core
*dev
= fh
->dev
;
1290 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1294 /* ------------------------------------------------------------------
1295 File operations for the device
1296 ------------------------------------------------------------------*/
1298 static int __tm6000_open(struct file
*file
)
1300 struct video_device
*vdev
= video_devdata(file
);
1301 struct tm6000_core
*dev
= video_drvdata(file
);
1302 struct tm6000_fh
*fh
;
1303 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1307 dprintk(dev
, V4L2_DEBUG_OPEN
, "tm6000: open called (dev=%s)\n",
1308 video_device_node_name(vdev
));
1310 switch (vdev
->vfl_type
) {
1311 case VFL_TYPE_GRABBER
:
1312 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1315 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1317 case VFL_TYPE_RADIO
:
1324 /* If more than one user, mutex should be added */
1327 dprintk(dev
, V4L2_DEBUG_OPEN
, "open dev=%s type=%s users=%d\n",
1328 video_device_node_name(vdev
), v4l2_type_names
[type
],
1331 /* allocate + initialize per filehandle data */
1332 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1338 v4l2_fh_init(&fh
->fh
, vdev
);
1339 file
->private_data
= fh
;
1344 dev
->fourcc
= format
[0].fourcc
;
1346 fh
->fmt
= format_by_fourcc(dev
->fourcc
);
1348 tm6000_get_std_res(dev
);
1350 fh
->width
= dev
->width
;
1351 fh
->height
= dev
->height
;
1353 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: fh=%p, dev=%p, dev->vidq=%p\n",
1354 fh
, dev
, &dev
->vidq
);
1355 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: list_empty queued=%d\n",
1356 list_empty(&dev
->vidq
.queued
));
1357 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: list_empty active=%d\n",
1358 list_empty(&dev
->vidq
.active
));
1360 /* initialize hardware on analog mode */
1361 rc
= tm6000_init_analog_mode(dev
);
1363 v4l2_fh_exit(&fh
->fh
);
1368 dev
->mode
= TM6000_MODE_ANALOG
;
1371 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &tm6000_video_qops
,
1374 V4L2_FIELD_INTERLACED
,
1375 sizeof(struct tm6000_buffer
), fh
, &dev
->lock
);
1377 dprintk(dev
, V4L2_DEBUG_OPEN
, "video_open: setting radio device\n");
1378 tm6000_set_audio_rinput(dev
);
1379 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_radio
);
1380 tm6000_prepare_isoc(dev
);
1381 tm6000_start_thread(dev
);
1383 v4l2_fh_add(&fh
->fh
);
1388 static int tm6000_open(struct file
*file
)
1390 struct video_device
*vdev
= video_devdata(file
);
1393 mutex_lock(vdev
->lock
);
1394 res
= __tm6000_open(file
);
1395 mutex_unlock(vdev
->lock
);
1400 tm6000_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*pos
)
1402 struct tm6000_fh
*fh
= file
->private_data
;
1403 struct tm6000_core
*dev
= fh
->dev
;
1405 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1408 if (!res_get(fh
->dev
, fh
, true))
1411 if (mutex_lock_interruptible(&dev
->lock
))
1412 return -ERESTARTSYS
;
1413 res
= videobuf_read_stream(&fh
->vb_vidq
, data
, count
, pos
, 0,
1414 file
->f_flags
& O_NONBLOCK
);
1415 mutex_unlock(&dev
->lock
);
1422 __tm6000_poll(struct file
*file
, struct poll_table_struct
*wait
)
1424 __poll_t req_events
= poll_requested_events(wait
);
1425 struct tm6000_fh
*fh
= file
->private_data
;
1426 struct tm6000_buffer
*buf
;
1429 if (v4l2_event_pending(&fh
->fh
))
1431 else if (req_events
& EPOLLPRI
)
1432 poll_wait(file
, &fh
->fh
.wait
, wait
);
1433 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1434 return res
| EPOLLERR
;
1436 if (!!is_res_streaming(fh
->dev
, fh
))
1437 return res
| EPOLLERR
;
1439 if (!is_res_read(fh
->dev
, fh
)) {
1440 /* streaming capture */
1441 if (list_empty(&fh
->vb_vidq
.stream
))
1442 return res
| EPOLLERR
;
1443 buf
= list_entry(fh
->vb_vidq
.stream
.next
, struct tm6000_buffer
, vb
.stream
);
1444 poll_wait(file
, &buf
->vb
.done
, wait
);
1445 if (buf
->vb
.state
== VIDEOBUF_DONE
||
1446 buf
->vb
.state
== VIDEOBUF_ERROR
)
1447 return res
| EPOLLIN
| EPOLLRDNORM
;
1448 } else if (req_events
& (EPOLLIN
| EPOLLRDNORM
)) {
1449 /* read() capture */
1450 return res
| videobuf_poll_stream(file
, &fh
->vb_vidq
, wait
);
1455 static __poll_t
tm6000_poll(struct file
*file
, struct poll_table_struct
*wait
)
1457 struct tm6000_fh
*fh
= file
->private_data
;
1458 struct tm6000_core
*dev
= fh
->dev
;
1461 mutex_lock(&dev
->lock
);
1462 res
= __tm6000_poll(file
, wait
);
1463 mutex_unlock(&dev
->lock
);
1467 static int tm6000_release(struct file
*file
)
1469 struct tm6000_fh
*fh
= file
->private_data
;
1470 struct tm6000_core
*dev
= fh
->dev
;
1471 struct video_device
*vdev
= video_devdata(file
);
1473 dprintk(dev
, V4L2_DEBUG_OPEN
, "tm6000: close called (dev=%s, users=%d)\n",
1474 video_device_node_name(vdev
), dev
->users
);
1476 mutex_lock(&dev
->lock
);
1482 tm6000_uninit_isoc(dev
);
1484 /* Stop interrupt USB pipe */
1485 tm6000_ir_int_stop(dev
);
1487 usb_reset_configuration(dev
->udev
);
1489 if (dev
->int_in
.endp
)
1490 usb_set_interface(dev
->udev
,
1491 dev
->isoc_in
.bInterfaceNumber
, 2);
1493 usb_set_interface(dev
->udev
,
1494 dev
->isoc_in
.bInterfaceNumber
, 0);
1496 /* Start interrupt USB pipe */
1497 tm6000_ir_int_start(dev
);
1500 videobuf_mmap_free(&fh
->vb_vidq
);
1502 v4l2_fh_del(&fh
->fh
);
1503 v4l2_fh_exit(&fh
->fh
);
1505 mutex_unlock(&dev
->lock
);
1510 static int tm6000_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1512 struct tm6000_fh
*fh
= file
->private_data
;
1513 struct tm6000_core
*dev
= fh
->dev
;
1516 if (mutex_lock_interruptible(&dev
->lock
))
1517 return -ERESTARTSYS
;
1518 res
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1519 mutex_unlock(&dev
->lock
);
1523 static const struct v4l2_file_operations tm6000_fops
= {
1524 .owner
= THIS_MODULE
,
1525 .open
= tm6000_open
,
1526 .release
= tm6000_release
,
1527 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1528 .read
= tm6000_read
,
1529 .poll
= tm6000_poll
,
1530 .mmap
= tm6000_mmap
,
1533 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1534 .vidioc_querycap
= vidioc_querycap
,
1535 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1536 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1537 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1538 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1539 .vidioc_s_std
= vidioc_s_std
,
1540 .vidioc_g_std
= vidioc_g_std
,
1541 .vidioc_enum_input
= vidioc_enum_input
,
1542 .vidioc_g_input
= vidioc_g_input
,
1543 .vidioc_s_input
= vidioc_s_input
,
1544 .vidioc_g_tuner
= vidioc_g_tuner
,
1545 .vidioc_s_tuner
= vidioc_s_tuner
,
1546 .vidioc_g_frequency
= vidioc_g_frequency
,
1547 .vidioc_s_frequency
= vidioc_s_frequency
,
1548 .vidioc_streamon
= vidioc_streamon
,
1549 .vidioc_streamoff
= vidioc_streamoff
,
1550 .vidioc_reqbufs
= vidioc_reqbufs
,
1551 .vidioc_querybuf
= vidioc_querybuf
,
1552 .vidioc_qbuf
= vidioc_qbuf
,
1553 .vidioc_dqbuf
= vidioc_dqbuf
,
1554 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1555 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1558 static struct video_device tm6000_template
= {
1560 .fops
= &tm6000_fops
,
1561 .ioctl_ops
= &video_ioctl_ops
,
1562 .release
= video_device_release_empty
,
1563 .tvnorms
= TM6000_STD
,
1566 static const struct v4l2_file_operations radio_fops
= {
1567 .owner
= THIS_MODULE
,
1568 .open
= tm6000_open
,
1569 .poll
= v4l2_ctrl_poll
,
1570 .release
= tm6000_release
,
1571 .unlocked_ioctl
= video_ioctl2
,
1574 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
1575 .vidioc_querycap
= vidioc_querycap
,
1576 .vidioc_g_tuner
= radio_g_tuner
,
1577 .vidioc_s_tuner
= radio_s_tuner
,
1578 .vidioc_g_frequency
= vidioc_g_frequency
,
1579 .vidioc_s_frequency
= vidioc_s_frequency
,
1580 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1581 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1584 static struct video_device tm6000_radio_template
= {
1586 .fops
= &radio_fops
,
1587 .ioctl_ops
= &radio_ioctl_ops
,
1590 /* -----------------------------------------------------------------
1591 * Initialization and module stuff
1592 * ------------------------------------------------------------------
1595 static void vdev_init(struct tm6000_core
*dev
,
1596 struct video_device
*vfd
,
1597 const struct video_device
1598 *template, const char *type_name
)
1601 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1602 vfd
->release
= video_device_release_empty
;
1603 vfd
->lock
= &dev
->lock
;
1605 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s", dev
->name
, type_name
);
1607 video_set_drvdata(vfd
, dev
);
1610 int tm6000_v4l2_register(struct tm6000_core
*dev
)
1614 v4l2_ctrl_handler_init(&dev
->ctrl_handler
, 6);
1615 v4l2_ctrl_handler_init(&dev
->radio_ctrl_handler
, 2);
1616 v4l2_ctrl_new_std(&dev
->radio_ctrl_handler
, &tm6000_radio_ctrl_ops
,
1617 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
1618 v4l2_ctrl_new_std(&dev
->radio_ctrl_handler
, &tm6000_radio_ctrl_ops
,
1619 V4L2_CID_AUDIO_VOLUME
, -15, 15, 1, 0);
1620 v4l2_ctrl_new_std(&dev
->ctrl_handler
, &tm6000_ctrl_ops
,
1621 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 54);
1622 v4l2_ctrl_new_std(&dev
->ctrl_handler
, &tm6000_ctrl_ops
,
1623 V4L2_CID_CONTRAST
, 0, 255, 1, 119);
1624 v4l2_ctrl_new_std(&dev
->ctrl_handler
, &tm6000_ctrl_ops
,
1625 V4L2_CID_SATURATION
, 0, 255, 1, 112);
1626 v4l2_ctrl_new_std(&dev
->ctrl_handler
, &tm6000_ctrl_ops
,
1627 V4L2_CID_HUE
, -128, 127, 1, 0);
1628 v4l2_ctrl_add_handler(&dev
->ctrl_handler
,
1629 &dev
->radio_ctrl_handler
, NULL
);
1631 if (dev
->radio_ctrl_handler
.error
)
1632 ret
= dev
->radio_ctrl_handler
.error
;
1633 if (!ret
&& dev
->ctrl_handler
.error
)
1634 ret
= dev
->ctrl_handler
.error
;
1638 vdev_init(dev
, &dev
->vfd
, &tm6000_template
, "video");
1640 dev
->vfd
.ctrl_handler
= &dev
->ctrl_handler
;
1642 /* init video dma queues */
1643 INIT_LIST_HEAD(&dev
->vidq
.active
);
1644 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1646 ret
= video_register_device(&dev
->vfd
, VFL_TYPE_GRABBER
, video_nr
);
1649 printk(KERN_INFO
"%s: can't register video device\n",
1654 printk(KERN_INFO
"%s: registered device %s\n",
1655 dev
->name
, video_device_node_name(&dev
->vfd
));
1657 if (dev
->caps
.has_radio
) {
1658 vdev_init(dev
, &dev
->radio_dev
, &tm6000_radio_template
,
1660 dev
->radio_dev
.ctrl_handler
= &dev
->radio_ctrl_handler
;
1661 ret
= video_register_device(&dev
->radio_dev
, VFL_TYPE_RADIO
,
1664 printk(KERN_INFO
"%s: can't register radio device\n",
1669 printk(KERN_INFO
"%s: registered device %s\n",
1670 dev
->name
, video_device_node_name(&dev
->radio_dev
));
1673 printk(KERN_INFO
"Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret
);
1677 video_unregister_device(&dev
->vfd
);
1679 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1680 v4l2_ctrl_handler_free(&dev
->radio_ctrl_handler
);
1684 int tm6000_v4l2_unregister(struct tm6000_core
*dev
)
1686 video_unregister_device(&dev
->vfd
);
1688 /* if URB buffers are still allocated free them now */
1689 tm6000_free_urb_buffers(dev
);
1691 video_unregister_device(&dev
->radio_dev
);
1695 int tm6000_v4l2_exit(void)
1700 module_param(video_nr
, int, 0);
1701 MODULE_PARM_DESC(video_nr
, "Allow changing video device number");
1703 module_param_named(debug
, tm6000_debug
, int, 0444);
1704 MODULE_PARM_DESC(debug
, "activates debug info");
1706 module_param(vid_limit
, int, 0644);
1707 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
1709 module_param(keep_urb
, bool, 0);
1710 MODULE_PARM_DESC(keep_urb
, "Keep urb buffers allocated even when the device is closed by the user");