2 * tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
6 * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 * - Fixed module load/unload
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation version 2
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.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>
43 #include "tm6000-regs.h"
46 #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
48 /* Limits minimum and default number of buffers */
49 #define TM6000_MIN_BUF 4
50 #define TM6000_DEF_BUF 8
52 #define TM6000_NUM_URB_BUF 8
54 #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
56 /* Declare static vars that will be used as parameters */
57 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
58 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
59 static int radio_nr
= -1; /* /dev/radioN, -1 for autodetect */
60 static int keep_urb
; /* keep urb buffers allocated */
64 EXPORT_SYMBOL_GPL(tm6000_debug
);
66 static const struct v4l2_queryctrl no_ctrl
= {
68 .flags
= V4L2_CTRL_FLAG_DISABLED
,
71 /* supported controls */
72 static struct v4l2_queryctrl tm6000_qctrl
[] = {
74 .id
= V4L2_CID_BRIGHTNESS
,
75 .type
= V4L2_CTRL_TYPE_INTEGER
,
83 .id
= V4L2_CID_CONTRAST
,
84 .type
= V4L2_CTRL_TYPE_INTEGER
,
92 .id
= V4L2_CID_SATURATION
,
93 .type
= V4L2_CTRL_TYPE_INTEGER
,
102 .type
= V4L2_CTRL_TYPE_INTEGER
,
112 .id
= V4L2_CID_AUDIO_MUTE
,
116 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
118 .id
= V4L2_CID_AUDIO_VOLUME
,
124 .type
= V4L2_CTRL_TYPE_INTEGER
,
128 static const unsigned int CTRLS
= ARRAY_SIZE(tm6000_qctrl
);
129 static int qctl_regs
[ARRAY_SIZE(tm6000_qctrl
)];
131 static struct tm6000_fmt format
[] = {
133 .name
= "4:2:2, packed, YVY2",
134 .fourcc
= V4L2_PIX_FMT_YUYV
,
137 .name
= "4:2:2, packed, UYVY",
138 .fourcc
= V4L2_PIX_FMT_UYVY
,
141 .name
= "A/V + VBI mux packet",
142 .fourcc
= V4L2_PIX_FMT_TM6000
,
147 static const struct v4l2_queryctrl
*ctrl_by_id(unsigned int id
)
151 for (i
= 0; i
< CTRLS
; i
++)
152 if (tm6000_qctrl
[i
].id
== id
)
153 return tm6000_qctrl
+i
;
157 /* ------------------------------------------------------------------
158 * DMA and thread functions
159 * ------------------------------------------------------------------
162 #define norm_maxw(a) 720
163 #define norm_maxh(a) 576
165 #define norm_minw(a) norm_maxw(a)
166 #define norm_minh(a) norm_maxh(a)
169 * video-buf generic routine to get the next available buffer
171 static inline void get_next_buf(struct tm6000_dmaqueue
*dma_q
,
172 struct tm6000_buffer
**buf
)
174 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
176 if (list_empty(&dma_q
->active
)) {
177 dprintk(dev
, V4L2_DEBUG_QUEUE
, "No active queue to serve\n");
182 *buf
= list_entry(dma_q
->active
.next
,
183 struct tm6000_buffer
, vb
.queue
);
187 * Announces that a buffer were filled and request the next
189 static inline void buffer_filled(struct tm6000_core
*dev
,
190 struct tm6000_dmaqueue
*dma_q
,
191 struct tm6000_buffer
*buf
)
193 /* Advice that buffer was filled */
194 dprintk(dev
, V4L2_DEBUG_ISOC
, "[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
195 buf
->vb
.state
= VIDEOBUF_DONE
;
196 buf
->vb
.field_count
++;
197 do_gettimeofday(&buf
->vb
.ts
);
199 list_del(&buf
->vb
.queue
);
200 wake_up(&buf
->vb
.done
);
204 * Identify the tm5600/6000 buffer header type and properly handles
206 static int copy_streams(u8
*data
, unsigned long len
,
209 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
210 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
211 u8
*ptr
= data
, *endp
= data
+len
;
212 unsigned long header
= 0;
214 unsigned int cmd
, cpysize
, pktsize
, size
, field
, block
, line
, pos
= 0;
215 struct tm6000_buffer
*vbuf
= NULL
;
217 unsigned int linewidth
;
220 /* get video buffer */
221 get_next_buf(dma_q
, &vbuf
);
225 voutp
= videobuf_to_vmalloc(&vbuf
->vb
);
231 for (ptr
= data
; ptr
< endp
;) {
232 if (!dev
->isoc_ctl
.cmd
) {
234 if (dev
->isoc_ctl
.tmp_buf_len
> 0) {
235 /* from last urb or packet */
236 header
= dev
->isoc_ctl
.tmp_buf
;
237 if (4 - dev
->isoc_ctl
.tmp_buf_len
> 0) {
238 memcpy((u8
*)&header
+
239 dev
->isoc_ctl
.tmp_buf_len
,
241 4 - dev
->isoc_ctl
.tmp_buf_len
);
242 ptr
+= 4 - dev
->isoc_ctl
.tmp_buf_len
;
244 dev
->isoc_ctl
.tmp_buf_len
= 0;
246 if (ptr
+ 3 >= endp
) {
247 /* have incomplete header */
248 dev
->isoc_ctl
.tmp_buf_len
= endp
- ptr
;
249 memcpy(&dev
->isoc_ctl
.tmp_buf
, ptr
,
250 dev
->isoc_ctl
.tmp_buf_len
);
254 for (; ptr
< endp
- 3; ptr
++) {
255 if (*(ptr
+ 3) == 0x47)
258 /* Get message header */
259 header
= *(unsigned long *)ptr
;
263 /* split the header fields */
264 size
= ((header
& 0x7e) << 1);
267 block
= (header
>> 7) & 0xf;
268 field
= (header
>> 11) & 0x1;
269 line
= (header
>> 12) & 0x1ff;
270 cmd
= (header
>> 21) & 0x7;
271 /* Validates haeder fields */
272 if (size
> TM6000_URB_MSG_LEN
)
273 size
= TM6000_URB_MSG_LEN
;
274 pktsize
= TM6000_URB_MSG_LEN
;
276 * calculate position in buffer and change the buffer
279 case TM6000_URB_MSG_VIDEO
:
281 if ((dev
->isoc_ctl
.vfield
!= field
) &&
284 * Announces that a new buffer
287 buffer_filled(dev
, dma_q
, vbuf
);
288 dprintk(dev
, V4L2_DEBUG_ISOC
,
289 "new buffer filled\n");
290 get_next_buf(dma_q
, &vbuf
);
293 voutp
= videobuf_to_vmalloc(&vbuf
->vb
);
296 memset(voutp
, 0, vbuf
->vb
.size
);
298 linewidth
= vbuf
->vb
.width
<< 1;
299 pos
= ((line
<< 1) - field
- 1) *
300 linewidth
+ block
* TM6000_URB_MSG_LEN
;
301 /* Don't allow to write out of the buffer */
302 if (pos
+ size
> vbuf
->vb
.size
)
303 cmd
= TM6000_URB_MSG_ERR
;
304 dev
->isoc_ctl
.vfield
= field
;
307 case TM6000_URB_MSG_VBI
:
309 case TM6000_URB_MSG_AUDIO
:
310 case TM6000_URB_MSG_PTS
:
311 size
= pktsize
; /* Size is always 180 bytes */
315 /* Continue the last copy */
316 cmd
= dev
->isoc_ctl
.cmd
;
317 size
= dev
->isoc_ctl
.size
;
318 pos
= dev
->isoc_ctl
.pos
;
319 pktsize
= dev
->isoc_ctl
.pktsize
;
320 field
= dev
->isoc_ctl
.field
;
322 cpysize
= (endp
- ptr
> size
) ? size
: endp
- ptr
;
324 /* copy data in different buffers */
326 case TM6000_URB_MSG_VIDEO
:
327 /* Fills video buffer */
329 memcpy(&voutp
[pos
], ptr
, cpysize
);
331 case TM6000_URB_MSG_AUDIO
: {
333 for (i
= 0; i
< cpysize
; i
+= 2)
334 swab16s((u16
*)(ptr
+ i
));
336 tm6000_call_fillbuf(dev
, TM6000_AUDIO
, ptr
, cpysize
);
339 case TM6000_URB_MSG_VBI
:
340 /* Need some code to copy vbi buffer */
342 case TM6000_URB_MSG_PTS
: {
343 /* Need some code to copy pts */
346 dprintk(dev
, V4L2_DEBUG_ISOC
, "field %d, PTS %x",
352 if (ptr
+ pktsize
> endp
) {
354 * End of URB packet, but cmd processing is not
355 * complete. Preserve the state for a next packet
357 dev
->isoc_ctl
.pos
= pos
+ cpysize
;
358 dev
->isoc_ctl
.size
= size
- cpysize
;
359 dev
->isoc_ctl
.cmd
= cmd
;
360 dev
->isoc_ctl
.field
= field
;
361 dev
->isoc_ctl
.pktsize
= pktsize
- (endp
- ptr
);
364 dev
->isoc_ctl
.cmd
= 0;
372 * Identify the tm5600/6000 buffer header type and properly handles
374 static int copy_multiplexed(u8
*ptr
, unsigned long len
,
377 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
378 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
379 unsigned int pos
= dev
->isoc_ctl
.pos
, cpysize
;
381 struct tm6000_buffer
*buf
;
384 get_next_buf(dma_q
, &buf
);
386 outp
= videobuf_to_vmalloc(&buf
->vb
);
392 cpysize
= min(len
, buf
->vb
.size
-pos
);
393 memcpy(&outp
[pos
], ptr
, cpysize
);
397 if (pos
>= buf
->vb
.size
) {
399 /* Announces that a new buffer were filled */
400 buffer_filled(dev
, dma_q
, buf
);
401 dprintk(dev
, V4L2_DEBUG_ISOC
, "new buffer filled\n");
402 get_next_buf(dma_q
, &buf
);
405 outp
= videobuf_to_vmalloc(&(buf
->vb
));
412 dev
->isoc_ctl
.pos
= pos
;
416 static inline void print_err_status(struct tm6000_core
*dev
,
417 int packet
, int status
)
419 char *errmsg
= "Unknown";
423 errmsg
= "unlinked synchronuously";
426 errmsg
= "unlinked asynchronuously";
429 errmsg
= "Buffer error (overrun)";
432 errmsg
= "Stalled (device not responding)";
435 errmsg
= "Babble (bad cable?)";
438 errmsg
= "Bit-stuff error (bad cable?)";
441 errmsg
= "CRC/Timeout (could be anything)";
444 errmsg
= "Device does not respond";
448 dprintk(dev
, V4L2_DEBUG_QUEUE
, "URB status %d [%s].\n",
451 dprintk(dev
, V4L2_DEBUG_QUEUE
, "URB packet %d, status %d [%s].\n",
452 packet
, status
, errmsg
);
458 * Controls the isoc copy of each urb packet
460 static inline int tm6000_isoc_copy(struct urb
*urb
)
462 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
463 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
464 int i
, len
= 0, rc
= 1, status
;
467 if (urb
->status
< 0) {
468 print_err_status(dev
, -1, urb
->status
);
472 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
473 status
= urb
->iso_frame_desc
[i
].status
;
476 print_err_status(dev
, i
, status
);
480 len
= urb
->iso_frame_desc
[i
].actual_length
;
483 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
484 if (!urb
->iso_frame_desc
[i
].status
) {
485 if ((dev
->fourcc
) == V4L2_PIX_FMT_TM6000
) {
486 rc
= copy_multiplexed(p
, len
, urb
);
490 copy_streams(p
, len
, urb
);
498 /* ------------------------------------------------------------------
500 * ------------------------------------------------------------------
504 * IRQ callback, called by URB callback
506 static void tm6000_irq_callback(struct urb
*urb
)
508 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
509 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
512 switch (urb
->status
) {
523 tm6000_err("urb completion error %d.\n", urb
->status
);
527 spin_lock(&dev
->slock
);
528 tm6000_isoc_copy(urb
);
529 spin_unlock(&dev
->slock
);
531 /* Reset urb buffers */
532 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
533 urb
->iso_frame_desc
[i
].status
= 0;
534 urb
->iso_frame_desc
[i
].actual_length
= 0;
537 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
539 tm6000_err("urb resubmit failed (error=%i)\n",
544 * Allocate URB buffers
546 static int tm6000_alloc_urb_buffers(struct tm6000_core
*dev
)
548 int num_bufs
= TM6000_NUM_URB_BUF
;
551 if (dev
->urb_buffer
!= NULL
)
554 dev
->urb_buffer
= kmalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
555 if (!dev
->urb_buffer
) {
556 tm6000_err("cannot allocate memory for urb buffers\n");
560 dev
->urb_dma
= kmalloc(sizeof(dma_addr_t
*)*num_bufs
, GFP_KERNEL
);
562 tm6000_err("cannot allocate memory for urb dma pointers\n");
566 for (i
= 0; i
< num_bufs
; i
++) {
567 dev
->urb_buffer
[i
] = usb_alloc_coherent(
568 dev
->udev
, dev
->urb_size
,
569 GFP_KERNEL
, &dev
->urb_dma
[i
]);
570 if (!dev
->urb_buffer
[i
]) {
571 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
575 memset(dev
->urb_buffer
[i
], 0, dev
->urb_size
);
584 static int tm6000_free_urb_buffers(struct tm6000_core
*dev
)
588 if (dev
->urb_buffer
== NULL
)
591 for (i
= 0; i
< TM6000_NUM_URB_BUF
; i
++) {
592 if (dev
->urb_buffer
[i
]) {
593 usb_free_coherent(dev
->udev
,
597 dev
->urb_buffer
[i
] = NULL
;
600 kfree(dev
->urb_buffer
);
602 dev
->urb_buffer
= NULL
;
609 * Stop and Deallocate URBs
611 static void tm6000_uninit_isoc(struct tm6000_core
*dev
)
616 dev
->isoc_ctl
.buf
= NULL
;
617 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
618 urb
= dev
->isoc_ctl
.urb
[i
];
623 dev
->isoc_ctl
.urb
[i
] = NULL
;
625 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
629 tm6000_free_urb_buffers(dev
);
631 kfree(dev
->isoc_ctl
.urb
);
632 kfree(dev
->isoc_ctl
.transfer_buffer
);
634 dev
->isoc_ctl
.urb
= NULL
;
635 dev
->isoc_ctl
.transfer_buffer
= NULL
;
636 dev
->isoc_ctl
.num_bufs
= 0;
640 * Assign URBs and start IRQ
642 static int tm6000_prepare_isoc(struct tm6000_core
*dev
)
644 struct tm6000_dmaqueue
*dma_q
= &dev
->vidq
;
645 int i
, j
, sb_size
, pipe
, size
, max_packets
;
646 int num_bufs
= TM6000_NUM_URB_BUF
;
649 /* De-allocates all pending stuff */
650 tm6000_uninit_isoc(dev
);
651 /* Stop interrupt USB pipe */
652 tm6000_ir_int_stop(dev
);
654 usb_set_interface(dev
->udev
,
655 dev
->isoc_in
.bInterfaceNumber
,
656 dev
->isoc_in
.bAlternateSetting
);
658 /* Start interrupt USB pipe */
659 tm6000_ir_int_start(dev
);
661 pipe
= usb_rcvisocpipe(dev
->udev
,
662 dev
->isoc_in
.endp
->desc
.bEndpointAddress
&
663 USB_ENDPOINT_NUMBER_MASK
);
665 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
667 if (size
> dev
->isoc_in
.maxsize
)
668 size
= dev
->isoc_in
.maxsize
;
670 dev
->isoc_ctl
.max_pkt_size
= size
;
672 max_packets
= TM6000_MAX_ISO_PACKETS
;
673 sb_size
= max_packets
* size
;
674 dev
->urb_size
= sb_size
;
676 dev
->isoc_ctl
.num_bufs
= num_bufs
;
678 dev
->isoc_ctl
.urb
= kmalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
679 if (!dev
->isoc_ctl
.urb
) {
680 tm6000_err("cannot alloc memory for usb buffers\n");
684 dev
->isoc_ctl
.transfer_buffer
= kmalloc(sizeof(void *)*num_bufs
,
686 if (!dev
->isoc_ctl
.transfer_buffer
) {
687 tm6000_err("cannot allocate memory for usbtransfer\n");
688 kfree(dev
->isoc_ctl
.urb
);
692 dprintk(dev
, V4L2_DEBUG_QUEUE
, "Allocating %d x %d packets"
693 " (%d bytes) of %d bytes each to handle %u size\n",
694 max_packets
, num_bufs
, sb_size
,
695 dev
->isoc_in
.maxsize
, size
);
698 if (!dev
->urb_buffer
&& tm6000_alloc_urb_buffers(dev
) < 0) {
699 tm6000_err("cannot allocate memory for urb buffers\n");
701 /* call free, as some buffers might have been allocated */
702 tm6000_free_urb_buffers(dev
);
703 kfree(dev
->isoc_ctl
.urb
);
704 kfree(dev
->isoc_ctl
.transfer_buffer
);
708 /* allocate urbs and transfer buffers */
709 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
710 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
712 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i
);
713 tm6000_uninit_isoc(dev
);
717 dev
->isoc_ctl
.urb
[i
] = urb
;
719 urb
->transfer_dma
= dev
->urb_dma
[i
];
720 dev
->isoc_ctl
.transfer_buffer
[i
] = dev
->urb_buffer
[i
];
722 usb_fill_bulk_urb(urb
, dev
->udev
, pipe
,
723 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
724 tm6000_irq_callback
, dma_q
);
725 urb
->interval
= dev
->isoc_in
.endp
->desc
.bInterval
;
726 urb
->number_of_packets
= max_packets
;
727 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
729 for (j
= 0; j
< max_packets
; j
++) {
730 urb
->iso_frame_desc
[j
].offset
= size
* j
;
731 urb
->iso_frame_desc
[j
].length
= size
;
738 static int tm6000_start_thread(struct tm6000_core
*dev
)
740 struct tm6000_dmaqueue
*dma_q
= &dev
->vidq
;
744 dma_q
->ini_jiffies
= jiffies
;
746 init_waitqueue_head(&dma_q
->wq
);
748 /* submit urbs and enables IRQ */
749 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
750 int rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
752 tm6000_err("submit of urb %i failed (error=%i)\n", i
,
754 tm6000_uninit_isoc(dev
);
762 /* ------------------------------------------------------------------
763 * Videobuf operations
764 * ------------------------------------------------------------------
768 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
770 struct tm6000_fh
*fh
= vq
->priv_data
;
772 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
774 *count
= TM6000_DEF_BUF
;
776 if (*count
< TM6000_MIN_BUF
)
777 *count
= TM6000_MIN_BUF
;
779 while (*size
* *count
> vid_limit
* 1024 * 1024)
785 static void free_buffer(struct videobuf_queue
*vq
, struct tm6000_buffer
*buf
)
787 struct tm6000_fh
*fh
= vq
->priv_data
;
788 struct tm6000_core
*dev
= fh
->dev
;
794 /* We used to wait for the buffer to finish here, but this didn't work
795 because, as we were keeping the state as VIDEOBUF_QUEUED,
796 videobuf_queue_cancel marked it as finished for us.
797 (Also, it could wedge forever if the hardware was misconfigured.)
799 This should be safe; by the time we get here, the buffer isn't
800 queued anymore. If we ever start marking the buffers as
801 VIDEOBUF_ACTIVE, it won't be, though.
803 spin_lock_irqsave(&dev
->slock
, flags
);
804 if (dev
->isoc_ctl
.buf
== buf
)
805 dev
->isoc_ctl
.buf
= NULL
;
806 spin_unlock_irqrestore(&dev
->slock
, flags
);
808 videobuf_vmalloc_free(&buf
->vb
);
809 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
813 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
814 enum v4l2_field field
)
816 struct tm6000_fh
*fh
= vq
->priv_data
;
817 struct tm6000_buffer
*buf
= container_of(vb
, struct tm6000_buffer
, vb
);
818 struct tm6000_core
*dev
= fh
->dev
;
821 BUG_ON(NULL
== fh
->fmt
);
824 /* FIXME: It assumes depth=2 */
825 /* The only currently supported format is 16 bits/pixel */
826 buf
->vb
.size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
827 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
830 if (buf
->fmt
!= fh
->fmt
||
831 buf
->vb
.width
!= fh
->width
||
832 buf
->vb
.height
!= fh
->height
||
833 buf
->vb
.field
!= field
) {
835 buf
->vb
.width
= fh
->width
;
836 buf
->vb
.height
= fh
->height
;
837 buf
->vb
.field
= field
;
838 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
841 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
842 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
847 if (!dev
->isoc_ctl
.num_bufs
) {
848 rc
= tm6000_prepare_isoc(dev
);
852 rc
= tm6000_start_thread(dev
);
858 buf
->vb
.state
= VIDEOBUF_PREPARED
;
862 free_buffer(vq
, buf
);
867 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
869 struct tm6000_buffer
*buf
= container_of(vb
, struct tm6000_buffer
, vb
);
870 struct tm6000_fh
*fh
= vq
->priv_data
;
871 struct tm6000_core
*dev
= fh
->dev
;
872 struct tm6000_dmaqueue
*vidq
= &dev
->vidq
;
874 buf
->vb
.state
= VIDEOBUF_QUEUED
;
875 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
878 static void buffer_release(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
880 struct tm6000_buffer
*buf
= container_of(vb
, struct tm6000_buffer
, vb
);
882 free_buffer(vq
, buf
);
885 static struct videobuf_queue_ops tm6000_video_qops
= {
886 .buf_setup
= buffer_setup
,
887 .buf_prepare
= buffer_prepare
,
888 .buf_queue
= buffer_queue
,
889 .buf_release
= buffer_release
,
892 /* ------------------------------------------------------------------
894 * ------------------------------------------------------------------
897 static bool is_res_read(struct tm6000_core
*dev
, struct tm6000_fh
*fh
)
899 /* Is the current fh handling it? if so, that's OK */
900 if (dev
->resources
== fh
&& dev
->is_res_read
)
906 static bool is_res_streaming(struct tm6000_core
*dev
, struct tm6000_fh
*fh
)
908 /* Is the current fh handling it? if so, that's OK */
909 if (dev
->resources
== fh
)
915 static bool res_get(struct tm6000_core
*dev
, struct tm6000_fh
*fh
,
918 /* Is the current fh handling it? if so, that's OK */
919 if (dev
->resources
== fh
&& dev
->is_res_read
== is_res_read
)
928 dev
->is_res_read
= is_res_read
;
929 dprintk(dev
, V4L2_DEBUG_RES_LOCK
, "res: get\n");
933 static void res_free(struct tm6000_core
*dev
, struct tm6000_fh
*fh
)
935 /* Is the current fh handling it? if so, that's OK */
936 if (dev
->resources
!= fh
)
939 dev
->resources
= NULL
;
940 dprintk(dev
, V4L2_DEBUG_RES_LOCK
, "res: put\n");
943 /* ------------------------------------------------------------------
944 * IOCTL vidioc handling
945 * ------------------------------------------------------------------
947 static int vidioc_querycap(struct file
*file
, void *priv
,
948 struct v4l2_capability
*cap
)
950 struct tm6000_core
*dev
= ((struct tm6000_fh
*)priv
)->dev
;
952 strlcpy(cap
->driver
, "tm6000", sizeof(cap
->driver
));
953 strlcpy(cap
->card
, "Trident TVMaster TM5600/6000/6010", sizeof(cap
->card
));
954 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
959 if (dev
->tuner_type
!= TUNER_ABSENT
)
960 cap
->capabilities
|= V4L2_CAP_TUNER
;
965 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
966 struct v4l2_fmtdesc
*f
)
968 if (unlikely(f
->index
>= ARRAY_SIZE(format
)))
971 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
972 f
->pixelformat
= format
[f
->index
].fourcc
;
976 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
977 struct v4l2_format
*f
)
979 struct tm6000_fh
*fh
= priv
;
981 f
->fmt
.pix
.width
= fh
->width
;
982 f
->fmt
.pix
.height
= fh
->height
;
983 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
984 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
985 f
->fmt
.pix
.bytesperline
=
986 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
987 f
->fmt
.pix
.sizeimage
=
988 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
993 static struct tm6000_fmt
*format_by_fourcc(unsigned int fourcc
)
997 for (i
= 0; i
< ARRAY_SIZE(format
); i
++)
998 if (format
[i
].fourcc
== fourcc
)
1003 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1004 struct v4l2_format
*f
)
1006 struct tm6000_core
*dev
= ((struct tm6000_fh
*)priv
)->dev
;
1007 struct tm6000_fmt
*fmt
;
1008 enum v4l2_field field
;
1010 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1012 dprintk(dev
, V4L2_DEBUG_IOCTL_ARG
, "Fourcc format (0x%08x)"
1013 " invalid.\n", f
->fmt
.pix
.pixelformat
);
1017 field
= f
->fmt
.pix
.field
;
1019 if (field
== V4L2_FIELD_ANY
)
1020 field
= V4L2_FIELD_SEQ_TB
;
1021 else if (V4L2_FIELD_INTERLACED
!= field
) {
1022 dprintk(dev
, V4L2_DEBUG_IOCTL_ARG
, "Field type invalid.\n");
1026 tm6000_get_std_res(dev
);
1028 f
->fmt
.pix
.width
= dev
->width
;
1029 f
->fmt
.pix
.height
= dev
->height
;
1031 f
->fmt
.pix
.width
&= ~0x01;
1033 f
->fmt
.pix
.field
= field
;
1035 f
->fmt
.pix
.bytesperline
=
1036 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1037 f
->fmt
.pix
.sizeimage
=
1038 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1043 /*FIXME: This seems to be generic enough to be at videodev2 */
1044 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1045 struct v4l2_format
*f
)
1047 struct tm6000_fh
*fh
= priv
;
1048 struct tm6000_core
*dev
= fh
->dev
;
1049 int ret
= vidioc_try_fmt_vid_cap(file
, fh
, f
);
1053 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1054 fh
->width
= f
->fmt
.pix
.width
;
1055 fh
->height
= f
->fmt
.pix
.height
;
1056 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
1059 dev
->fourcc
= f
->fmt
.pix
.pixelformat
;
1061 tm6000_set_fourcc_format(dev
);
1066 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1067 struct v4l2_requestbuffers
*p
)
1069 struct tm6000_fh
*fh
= priv
;
1071 return videobuf_reqbufs(&fh
->vb_vidq
, p
);
1074 static int vidioc_querybuf(struct file
*file
, void *priv
,
1075 struct v4l2_buffer
*p
)
1077 struct tm6000_fh
*fh
= priv
;
1079 return videobuf_querybuf(&fh
->vb_vidq
, p
);
1082 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1084 struct tm6000_fh
*fh
= priv
;
1086 return videobuf_qbuf(&fh
->vb_vidq
, p
);
1089 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1091 struct tm6000_fh
*fh
= priv
;
1093 return videobuf_dqbuf(&fh
->vb_vidq
, p
,
1094 file
->f_flags
& O_NONBLOCK
);
1097 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1099 struct tm6000_fh
*fh
= priv
;
1100 struct tm6000_core
*dev
= fh
->dev
;
1102 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1107 if (!res_get(dev
, fh
, false))
1109 return videobuf_streamon(&fh
->vb_vidq
);
1112 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1114 struct tm6000_fh
*fh
= priv
;
1115 struct tm6000_core
*dev
= fh
->dev
;
1117 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1123 videobuf_streamoff(&fh
->vb_vidq
);
1129 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1132 struct tm6000_fh
*fh
= priv
;
1133 struct tm6000_core
*dev
= fh
->dev
;
1136 rc
= tm6000_init_analog_mode(dev
);
1138 fh
->width
= dev
->width
;
1139 fh
->height
= dev
->height
;
1144 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, dev
->norm
);
1149 static const char *iname
[] = {
1150 [TM6000_INPUT_TV
] = "Television",
1151 [TM6000_INPUT_COMPOSITE1
] = "Composite 1",
1152 [TM6000_INPUT_COMPOSITE2
] = "Composite 2",
1153 [TM6000_INPUT_SVIDEO
] = "S-Video",
1156 static int vidioc_enum_input(struct file
*file
, void *priv
,
1157 struct v4l2_input
*i
)
1159 struct tm6000_fh
*fh
= priv
;
1160 struct tm6000_core
*dev
= fh
->dev
;
1167 if (!dev
->vinput
[n
].type
)
1172 if (dev
->vinput
[n
].type
== TM6000_INPUT_TV
)
1173 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1175 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1177 strcpy(i
->name
, iname
[dev
->vinput
[n
].type
]);
1179 i
->std
= TM6000_STD
;
1184 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1186 struct tm6000_fh
*fh
= priv
;
1187 struct tm6000_core
*dev
= fh
->dev
;
1194 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1196 struct tm6000_fh
*fh
= priv
;
1197 struct tm6000_core
*dev
= fh
->dev
;
1202 if (!dev
->vinput
[i
].type
)
1207 rc
= vidioc_s_std(file
, priv
, &dev
->vfd
->current_norm
);
1212 /* --- controls ---------------------------------------------- */
1213 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1214 struct v4l2_queryctrl
*qc
)
1218 for (i
= 0; i
< ARRAY_SIZE(tm6000_qctrl
); i
++)
1219 if (qc
->id
&& qc
->id
== tm6000_qctrl
[i
].id
) {
1220 memcpy(qc
, &(tm6000_qctrl
[i
]),
1228 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1229 struct v4l2_control
*ctrl
)
1231 struct tm6000_fh
*fh
= priv
;
1232 struct tm6000_core
*dev
= fh
->dev
;
1235 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1237 case V4L2_CID_CONTRAST
:
1238 val
= tm6000_get_reg(dev
, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ
, 0);
1240 case V4L2_CID_BRIGHTNESS
:
1241 val
= tm6000_get_reg(dev
, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ
, 0);
1243 case V4L2_CID_SATURATION
:
1244 val
= tm6000_get_reg(dev
, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ
, 0);
1247 val
= tm6000_get_reg(dev
, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ
, 0);
1249 case V4L2_CID_AUDIO_MUTE
:
1250 val
= dev
->ctl_mute
;
1252 case V4L2_CID_AUDIO_VOLUME
:
1253 val
= dev
->ctl_volume
;
1266 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1267 struct v4l2_control
*ctrl
)
1269 struct tm6000_fh
*fh
= priv
;
1270 struct tm6000_core
*dev
= fh
->dev
;
1271 u8 val
= ctrl
->value
;
1274 case V4L2_CID_CONTRAST
:
1275 tm6000_set_reg(dev
, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ
, val
);
1277 case V4L2_CID_BRIGHTNESS
:
1278 tm6000_set_reg(dev
, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ
, val
);
1280 case V4L2_CID_SATURATION
:
1281 tm6000_set_reg(dev
, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ
, val
);
1284 tm6000_set_reg(dev
, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ
, val
);
1286 case V4L2_CID_AUDIO_MUTE
:
1287 dev
->ctl_mute
= val
;
1288 tm6000_tvaudio_set_mute(dev
, val
);
1290 case V4L2_CID_AUDIO_VOLUME
:
1291 dev
->ctl_volume
= val
;
1292 tm6000_set_volume(dev
, val
);
1298 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1299 struct v4l2_tuner
*t
)
1301 struct tm6000_fh
*fh
= priv
;
1302 struct tm6000_core
*dev
= fh
->dev
;
1304 if (unlikely(UNSET
== dev
->tuner_type
))
1309 strcpy(t
->name
, "Television");
1310 t
->type
= V4L2_TUNER_ANALOG_TV
;
1311 t
->capability
= V4L2_TUNER_CAP_NORM
;
1312 t
->rangehigh
= 0xffffffffUL
;
1313 t
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1315 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1317 t
->audmode
= dev
->amode
;
1322 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1323 struct v4l2_tuner
*t
)
1325 struct tm6000_fh
*fh
= priv
;
1326 struct tm6000_core
*dev
= fh
->dev
;
1328 if (UNSET
== dev
->tuner_type
)
1333 dev
->amode
= t
->audmode
;
1334 dprintk(dev
, 3, "audio mode: %x\n", t
->audmode
);
1336 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1341 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1342 struct v4l2_frequency
*f
)
1344 struct tm6000_fh
*fh
= priv
;
1345 struct tm6000_core
*dev
= fh
->dev
;
1347 if (unlikely(UNSET
== dev
->tuner_type
))
1350 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1351 f
->frequency
= dev
->freq
;
1353 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, f
);
1358 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1359 struct v4l2_frequency
*f
)
1361 struct tm6000_fh
*fh
= priv
;
1362 struct tm6000_core
*dev
= fh
->dev
;
1364 if (unlikely(UNSET
== dev
->tuner_type
))
1366 if (unlikely(f
->tuner
!= 0))
1368 if (0 == fh
->radio
&& V4L2_TUNER_ANALOG_TV
!= f
->type
)
1370 if (1 == fh
->radio
&& V4L2_TUNER_RADIO
!= f
->type
)
1373 dev
->freq
= f
->frequency
;
1374 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, f
);
1379 static int radio_querycap(struct file
*file
, void *priv
,
1380 struct v4l2_capability
*cap
)
1382 struct tm6000_fh
*fh
= file
->private_data
;
1383 struct tm6000_core
*dev
= fh
->dev
;
1385 strcpy(cap
->driver
, "tm6000");
1386 strlcpy(cap
->card
, dev
->name
, sizeof(dev
->name
));
1387 sprintf(cap
->bus_info
, "USB%04x:%04x",
1388 le16_to_cpu(dev
->udev
->descriptor
.idVendor
),
1389 le16_to_cpu(dev
->udev
->descriptor
.idProduct
));
1390 cap
->version
= dev
->dev_type
;
1391 cap
->capabilities
= V4L2_CAP_TUNER
|
1394 V4L2_CAP_READWRITE
|
1400 static int radio_g_tuner(struct file
*file
, void *priv
,
1401 struct v4l2_tuner
*t
)
1403 struct tm6000_fh
*fh
= file
->private_data
;
1404 struct tm6000_core
*dev
= fh
->dev
;
1409 memset(t
, 0, sizeof(*t
));
1410 strcpy(t
->name
, "Radio");
1411 t
->type
= V4L2_TUNER_RADIO
;
1412 t
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1414 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1419 static int radio_s_tuner(struct file
*file
, void *priv
,
1420 struct v4l2_tuner
*t
)
1422 struct tm6000_fh
*fh
= file
->private_data
;
1423 struct tm6000_core
*dev
= fh
->dev
;
1428 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1433 static int radio_enum_input(struct file
*file
, void *priv
,
1434 struct v4l2_input
*i
)
1436 struct tm6000_fh
*fh
= priv
;
1437 struct tm6000_core
*dev
= fh
->dev
;
1442 if (!dev
->rinput
.type
)
1445 strcpy(i
->name
, "Radio");
1446 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1451 static int radio_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
1453 struct tm6000_fh
*fh
= priv
;
1454 struct tm6000_core
*dev
= fh
->dev
;
1456 if (dev
->input
!= 5)
1459 *i
= dev
->input
- 5;
1464 static int radio_g_audio(struct file
*file
, void *priv
,
1465 struct v4l2_audio
*a
)
1467 memset(a
, 0, sizeof(*a
));
1468 strcpy(a
->name
, "Radio");
1472 static int radio_s_audio(struct file
*file
, void *priv
,
1473 const struct v4l2_audio
*a
)
1478 static int radio_s_input(struct file
*filp
, void *priv
, unsigned int i
)
1480 struct tm6000_fh
*fh
= priv
;
1481 struct tm6000_core
*dev
= fh
->dev
;
1486 if (!dev
->rinput
.type
)
1494 static int radio_s_std(struct file
*file
, void *fh
, v4l2_std_id
*norm
)
1499 static int radio_queryctrl(struct file
*file
, void *priv
,
1500 struct v4l2_queryctrl
*c
)
1502 const struct v4l2_queryctrl
*ctrl
;
1504 if (c
->id
< V4L2_CID_BASE
||
1505 c
->id
>= V4L2_CID_LASTP1
)
1507 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
1508 ctrl
= ctrl_by_id(c
->id
);
1516 /* ------------------------------------------------------------------
1517 File operations for the device
1518 ------------------------------------------------------------------*/
1520 static int __tm6000_open(struct file
*file
)
1522 struct video_device
*vdev
= video_devdata(file
);
1523 struct tm6000_core
*dev
= video_drvdata(file
);
1524 struct tm6000_fh
*fh
;
1525 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1529 dprintk(dev
, V4L2_DEBUG_OPEN
, "tm6000: open called (dev=%s)\n",
1530 video_device_node_name(vdev
));
1532 switch (vdev
->vfl_type
) {
1533 case VFL_TYPE_GRABBER
:
1534 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1537 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1539 case VFL_TYPE_RADIO
:
1544 /* If more than one user, mutex should be added */
1547 dprintk(dev
, V4L2_DEBUG_OPEN
, "open dev=%s type=%s users=%d\n",
1548 video_device_node_name(vdev
), v4l2_type_names
[type
],
1551 /* allocate + initialize per filehandle data */
1552 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1558 file
->private_data
= fh
;
1563 dev
->fourcc
= format
[0].fourcc
;
1565 fh
->fmt
= format_by_fourcc(dev
->fourcc
);
1567 tm6000_get_std_res(dev
);
1569 fh
->width
= dev
->width
;
1570 fh
->height
= dev
->height
;
1572 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: fh=0x%08lx, dev=0x%08lx, "
1573 "dev->vidq=0x%08lx\n",
1574 (unsigned long)fh
, (unsigned long)dev
,
1575 (unsigned long)&dev
->vidq
);
1576 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: list_empty "
1577 "queued=%d\n", list_empty(&dev
->vidq
.queued
));
1578 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: list_empty "
1579 "active=%d\n", list_empty(&dev
->vidq
.active
));
1581 /* initialize hardware on analog mode */
1582 rc
= tm6000_init_analog_mode(dev
);
1586 if (dev
->mode
!= TM6000_MODE_ANALOG
) {
1587 /* Put all controls at a sane state */
1588 for (i
= 0; i
< ARRAY_SIZE(tm6000_qctrl
); i
++)
1589 qctl_regs
[i
] = tm6000_qctrl
[i
].default_value
;
1591 dev
->mode
= TM6000_MODE_ANALOG
;
1595 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &tm6000_video_qops
,
1598 V4L2_FIELD_INTERLACED
,
1599 sizeof(struct tm6000_buffer
), fh
, &dev
->lock
);
1601 dprintk(dev
, V4L2_DEBUG_OPEN
, "video_open: setting radio device\n");
1603 tm6000_set_audio_rinput(dev
);
1604 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_radio
);
1605 tm6000_prepare_isoc(dev
);
1606 tm6000_start_thread(dev
);
1612 static int tm6000_open(struct file
*file
)
1614 struct video_device
*vdev
= video_devdata(file
);
1617 mutex_lock(vdev
->lock
);
1618 res
= __tm6000_open(file
);
1619 mutex_unlock(vdev
->lock
);
1624 tm6000_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*pos
)
1626 struct tm6000_fh
*fh
= file
->private_data
;
1627 struct tm6000_core
*dev
= fh
->dev
;
1629 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1632 if (!res_get(fh
->dev
, fh
, true))
1635 if (mutex_lock_interruptible(&dev
->lock
))
1636 return -ERESTARTSYS
;
1637 res
= videobuf_read_stream(&fh
->vb_vidq
, data
, count
, pos
, 0,
1638 file
->f_flags
& O_NONBLOCK
);
1639 mutex_unlock(&dev
->lock
);
1646 __tm6000_poll(struct file
*file
, struct poll_table_struct
*wait
)
1648 struct tm6000_fh
*fh
= file
->private_data
;
1649 struct tm6000_buffer
*buf
;
1651 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1654 if (!!is_res_streaming(fh
->dev
, fh
))
1657 if (!is_res_read(fh
->dev
, fh
)) {
1658 /* streaming capture */
1659 if (list_empty(&fh
->vb_vidq
.stream
))
1661 buf
= list_entry(fh
->vb_vidq
.stream
.next
, struct tm6000_buffer
, vb
.stream
);
1663 /* read() capture */
1664 return videobuf_poll_stream(file
, &fh
->vb_vidq
, wait
);
1666 poll_wait(file
, &buf
->vb
.done
, wait
);
1667 if (buf
->vb
.state
== VIDEOBUF_DONE
||
1668 buf
->vb
.state
== VIDEOBUF_ERROR
)
1669 return POLLIN
| POLLRDNORM
;
1673 static unsigned int tm6000_poll(struct file
*file
, struct poll_table_struct
*wait
)
1675 struct tm6000_fh
*fh
= file
->private_data
;
1676 struct tm6000_core
*dev
= fh
->dev
;
1679 mutex_lock(&dev
->lock
);
1680 res
= __tm6000_poll(file
, wait
);
1681 mutex_unlock(&dev
->lock
);
1685 static int tm6000_release(struct file
*file
)
1687 struct tm6000_fh
*fh
= file
->private_data
;
1688 struct tm6000_core
*dev
= fh
->dev
;
1689 struct video_device
*vdev
= video_devdata(file
);
1691 dprintk(dev
, V4L2_DEBUG_OPEN
, "tm6000: close called (dev=%s, users=%d)\n",
1692 video_device_node_name(vdev
), dev
->users
);
1694 mutex_lock(&dev
->lock
);
1700 tm6000_uninit_isoc(dev
);
1702 /* Stop interrupt USB pipe */
1703 tm6000_ir_int_stop(dev
);
1705 usb_reset_configuration(dev
->udev
);
1707 if (dev
->int_in
.endp
)
1708 usb_set_interface(dev
->udev
,
1709 dev
->isoc_in
.bInterfaceNumber
, 2);
1711 usb_set_interface(dev
->udev
,
1712 dev
->isoc_in
.bInterfaceNumber
, 0);
1714 /* Start interrupt USB pipe */
1715 tm6000_ir_int_start(dev
);
1718 videobuf_mmap_free(&fh
->vb_vidq
);
1722 mutex_unlock(&dev
->lock
);
1727 static int tm6000_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1729 struct tm6000_fh
*fh
= file
->private_data
;
1730 struct tm6000_core
*dev
= fh
->dev
;
1733 if (mutex_lock_interruptible(&dev
->lock
))
1734 return -ERESTARTSYS
;
1735 res
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1736 mutex_unlock(&dev
->lock
);
1740 static struct v4l2_file_operations tm6000_fops
= {
1741 .owner
= THIS_MODULE
,
1742 .open
= tm6000_open
,
1743 .release
= tm6000_release
,
1744 .unlocked_ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1745 .read
= tm6000_read
,
1746 .poll
= tm6000_poll
,
1747 .mmap
= tm6000_mmap
,
1750 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1751 .vidioc_querycap
= vidioc_querycap
,
1752 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1753 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1754 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1755 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1756 .vidioc_s_std
= vidioc_s_std
,
1757 .vidioc_enum_input
= vidioc_enum_input
,
1758 .vidioc_g_input
= vidioc_g_input
,
1759 .vidioc_s_input
= vidioc_s_input
,
1760 .vidioc_queryctrl
= vidioc_queryctrl
,
1761 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1762 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1763 .vidioc_g_tuner
= vidioc_g_tuner
,
1764 .vidioc_s_tuner
= vidioc_s_tuner
,
1765 .vidioc_g_frequency
= vidioc_g_frequency
,
1766 .vidioc_s_frequency
= vidioc_s_frequency
,
1767 .vidioc_streamon
= vidioc_streamon
,
1768 .vidioc_streamoff
= vidioc_streamoff
,
1769 .vidioc_reqbufs
= vidioc_reqbufs
,
1770 .vidioc_querybuf
= vidioc_querybuf
,
1771 .vidioc_qbuf
= vidioc_qbuf
,
1772 .vidioc_dqbuf
= vidioc_dqbuf
,
1775 static struct video_device tm6000_template
= {
1777 .fops
= &tm6000_fops
,
1778 .ioctl_ops
= &video_ioctl_ops
,
1779 .release
= video_device_release
,
1780 .tvnorms
= TM6000_STD
,
1781 .current_norm
= V4L2_STD_NTSC_M
,
1784 static const struct v4l2_file_operations radio_fops
= {
1785 .owner
= THIS_MODULE
,
1786 .open
= tm6000_open
,
1787 .release
= tm6000_release
,
1788 .unlocked_ioctl
= video_ioctl2
,
1791 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
1792 .vidioc_querycap
= radio_querycap
,
1793 .vidioc_g_tuner
= radio_g_tuner
,
1794 .vidioc_enum_input
= radio_enum_input
,
1795 .vidioc_g_audio
= radio_g_audio
,
1796 .vidioc_s_tuner
= radio_s_tuner
,
1797 .vidioc_s_audio
= radio_s_audio
,
1798 .vidioc_s_input
= radio_s_input
,
1799 .vidioc_s_std
= radio_s_std
,
1800 .vidioc_queryctrl
= radio_queryctrl
,
1801 .vidioc_g_input
= radio_g_input
,
1802 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1803 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1804 .vidioc_g_frequency
= vidioc_g_frequency
,
1805 .vidioc_s_frequency
= vidioc_s_frequency
,
1808 static struct video_device tm6000_radio_template
= {
1810 .fops
= &radio_fops
,
1811 .ioctl_ops
= &radio_ioctl_ops
,
1814 /* -----------------------------------------------------------------
1815 * Initialization and module stuff
1816 * ------------------------------------------------------------------
1819 static struct video_device
*vdev_init(struct tm6000_core
*dev
,
1820 const struct video_device
1821 *template, const char *type_name
)
1823 struct video_device
*vfd
;
1825 vfd
= video_device_alloc();
1830 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1831 vfd
->release
= video_device_release
;
1832 vfd
->debug
= tm6000_debug
;
1833 vfd
->lock
= &dev
->lock
;
1835 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s", dev
->name
, type_name
);
1837 video_set_drvdata(vfd
, dev
);
1841 int tm6000_v4l2_register(struct tm6000_core
*dev
)
1845 dev
->vfd
= vdev_init(dev
, &tm6000_template
, "video");
1848 printk(KERN_INFO
"%s: can't register video device\n",
1853 /* init video dma queues */
1854 INIT_LIST_HEAD(&dev
->vidq
.active
);
1855 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1857 ret
= video_register_device(dev
->vfd
, VFL_TYPE_GRABBER
, video_nr
);
1860 printk(KERN_INFO
"%s: can't register video device\n",
1865 printk(KERN_INFO
"%s: registered device %s\n",
1866 dev
->name
, video_device_node_name(dev
->vfd
));
1868 if (dev
->caps
.has_radio
) {
1869 dev
->radio_dev
= vdev_init(dev
, &tm6000_radio_template
,
1871 if (!dev
->radio_dev
) {
1872 printk(KERN_INFO
"%s: can't register radio device\n",
1875 return ret
; /* FIXME release resource */
1878 ret
= video_register_device(dev
->radio_dev
, VFL_TYPE_RADIO
,
1881 printk(KERN_INFO
"%s: can't register radio device\n",
1883 return ret
; /* FIXME release resource */
1886 printk(KERN_INFO
"%s: registered device %s\n",
1887 dev
->name
, video_device_node_name(dev
->radio_dev
));
1890 printk(KERN_INFO
"Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret
);
1894 int tm6000_v4l2_unregister(struct tm6000_core
*dev
)
1896 video_unregister_device(dev
->vfd
);
1898 /* if URB buffers are still allocated free them now */
1899 tm6000_free_urb_buffers(dev
);
1901 if (dev
->radio_dev
) {
1902 if (video_is_registered(dev
->radio_dev
))
1903 video_unregister_device(dev
->radio_dev
);
1905 video_device_release(dev
->radio_dev
);
1906 dev
->radio_dev
= NULL
;
1912 int tm6000_v4l2_exit(void)
1917 module_param(video_nr
, int, 0);
1918 MODULE_PARM_DESC(video_nr
, "Allow changing video device number");
1920 module_param_named(debug
, tm6000_debug
, int, 0444);
1921 MODULE_PARM_DESC(debug
, "activates debug info");
1923 module_param(vid_limit
, int, 0644);
1924 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
1926 module_param(keep_urb
, bool, 0);
1927 MODULE_PARM_DESC(keep_urb
, "Keep urb buffers allocated even when the device is closed by the user");