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.
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
29 #include <linux/ioport.h>
30 #include <linux/init.h>
31 #include <linux/sched.h>
32 #include <linux/random.h>
33 #include <linux/version.h>
34 #include <linux/usb.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-ioctl.h>
37 #include <linux/interrupt.h>
38 #include <linux/kthread.h>
39 #include <linux/highmem.h>
40 #include <linux/freezer.h>
42 #include "tm6000-regs.h"
45 #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
47 /* Limits minimum and default number of buffers */
48 #define TM6000_MIN_BUF 4
49 #define TM6000_DEF_BUF 8
51 #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
53 /* Declare static vars that will be used as parameters */
54 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
55 static int video_nr
= -1; /* /dev/videoN, -1 for autodetect */
59 EXPORT_SYMBOL_GPL(tm6000_debug
);
61 /* supported controls */
62 static struct v4l2_queryctrl tm6000_qctrl
[] = {
64 .id
= V4L2_CID_BRIGHTNESS
,
65 .type
= V4L2_CTRL_TYPE_INTEGER
,
73 .id
= V4L2_CID_CONTRAST
,
74 .type
= V4L2_CTRL_TYPE_INTEGER
,
82 .id
= V4L2_CID_SATURATION
,
83 .type
= V4L2_CTRL_TYPE_INTEGER
,
92 .type
= V4L2_CTRL_TYPE_INTEGER
,
102 static int qctl_regs
[ARRAY_SIZE(tm6000_qctrl
)];
104 static struct tm6000_fmt format
[] = {
106 .name
= "4:2:2, packed, YVY2",
107 .fourcc
= V4L2_PIX_FMT_YUYV
,
110 .name
= "4:2:2, packed, UYVY",
111 .fourcc
= V4L2_PIX_FMT_UYVY
,
114 .name
= "A/V + VBI mux packet",
115 .fourcc
= V4L2_PIX_FMT_TM6000
,
120 /* ------------------------------------------------------------------
121 DMA and thread functions
122 ------------------------------------------------------------------*/
124 #define norm_maxw(a) 720
125 #define norm_maxh(a) 576
127 #define norm_minw(a) norm_maxw(a)
128 #define norm_minh(a) norm_maxh(a)
131 * video-buf generic routine to get the next available buffer
133 static inline void get_next_buf(struct tm6000_dmaqueue
*dma_q
,
134 struct tm6000_buffer
**buf
)
136 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
139 if (list_empty(&dma_q
->active
)) {
140 dprintk(dev
, V4L2_DEBUG_QUEUE
, "No active queue to serve\n");
145 *buf
= list_entry(dma_q
->active
.next
,
146 struct tm6000_buffer
, vb
.queue
);
151 /* Cleans up buffer - Usefull for testing for frame/URB loss */
152 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
158 * Announces that a buffer were filled and request the next
160 static inline void buffer_filled(struct tm6000_core
*dev
,
161 struct tm6000_dmaqueue
*dma_q
,
162 struct tm6000_buffer
*buf
)
164 /* Advice that buffer was filled */
165 dprintk(dev
, V4L2_DEBUG_ISOC
, "[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
166 buf
->vb
.state
= VIDEOBUF_DONE
;
167 buf
->vb
.field_count
++;
168 do_gettimeofday(&buf
->vb
.ts
);
170 list_del(&buf
->vb
.queue
);
171 wake_up(&buf
->vb
.done
);
174 const char *tm6000_msg_type
[] = {
175 "unknown(0)", /* 0 */
181 "unknown(6)", /* 6 */
182 "unknown(7)", /* 7 */
186 * Identify the tm5600/6000 buffer header type and properly handles
188 static int copy_streams(u8
*data
, unsigned long len
,
191 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
192 struct tm6000_core
*dev
= container_of(dma_q
,struct tm6000_core
,vidq
);
193 u8
*ptr
=data
, *endp
=data
+len
, c
;
194 unsigned long header
=0;
196 unsigned int cmd
, cpysize
, pktsize
, size
, field
, block
, line
, pos
= 0;
197 struct tm6000_buffer
*vbuf
;
199 unsigned int linewidth
;
201 /* get video buffer */
202 get_next_buf (dma_q
, &vbuf
);
205 voutp
= videobuf_to_vmalloc(&vbuf
->vb
);
209 for (ptr
= data
; ptr
< endp
;) {
210 if (!dev
->isoc_ctl
.cmd
) {
212 if (dev
->isoc_ctl
.tmp_buf_len
> 0) {
213 /* from last urb or packet */
214 header
= dev
->isoc_ctl
.tmp_buf
;
215 if (4 - dev
->isoc_ctl
.tmp_buf_len
> 0) {
216 memcpy ((u8
*)&header
+
217 dev
->isoc_ctl
.tmp_buf_len
,
219 4 - dev
->isoc_ctl
.tmp_buf_len
);
220 ptr
+= 4 - dev
->isoc_ctl
.tmp_buf_len
;
222 dev
->isoc_ctl
.tmp_buf_len
= 0;
224 if (ptr
+ 3 >= endp
) {
225 /* have incomplete header */
226 dev
->isoc_ctl
.tmp_buf_len
= endp
- ptr
;
227 memcpy (&dev
->isoc_ctl
.tmp_buf
, ptr
,
228 dev
->isoc_ctl
.tmp_buf_len
);
232 for (; ptr
< endp
- 3; ptr
++) {
233 if (*(ptr
+ 3) == 0x47)
236 /* Get message header */
237 header
= *(unsigned long *)ptr
;
241 /* split the header fields */
242 c
= (header
>> 24) & 0xff;
243 size
= ((header
& 0x7e) << 1);
246 block
= (header
>> 7) & 0xf;
247 field
= (header
>> 11) & 0x1;
248 line
= (header
>> 12) & 0x1ff;
249 cmd
= (header
>> 21) & 0x7;
250 /* Validates haeder fields */
251 if (size
> TM6000_URB_MSG_LEN
)
252 size
= TM6000_URB_MSG_LEN
;
253 pktsize
= TM6000_URB_MSG_LEN
;
254 /* calculate position in buffer
255 * and change the buffer
258 case TM6000_URB_MSG_VIDEO
:
259 if ((dev
->isoc_ctl
.vfield
!= field
) &&
261 /* Announces that a new buffer
264 buffer_filled (dev
, dma_q
, vbuf
);
265 dprintk (dev
, V4L2_DEBUG_ISOC
,
266 "new buffer filled\n");
267 get_next_buf (dma_q
, &vbuf
);
270 voutp
= videobuf_to_vmalloc (&vbuf
->vb
);
273 memset(voutp
, 0, vbuf
->vb
.size
);
275 linewidth
= vbuf
->vb
.width
<< 1;
276 pos
= ((line
<< 1) - field
- 1) * linewidth
+
277 block
* TM6000_URB_MSG_LEN
;
278 /* Don't allow to write out of the buffer */
279 if (pos
+ size
> vbuf
->vb
.size
)
280 cmd
= TM6000_URB_MSG_ERR
;
281 dev
->isoc_ctl
.vfield
= field
;
283 case TM6000_URB_MSG_VBI
:
285 case TM6000_URB_MSG_AUDIO
:
286 case TM6000_URB_MSG_PTS
:
287 size
= pktsize
; /* Size is always 180 bytes */
291 /* Continue the last copy */
292 cmd
= dev
->isoc_ctl
.cmd
;
293 size
= dev
->isoc_ctl
.size
;
294 pos
= dev
->isoc_ctl
.pos
;
295 pktsize
= dev
->isoc_ctl
.pktsize
;
297 cpysize
= (endp
- ptr
> size
) ? size
: endp
- ptr
;
299 /* copy data in different buffers */
301 case TM6000_URB_MSG_VIDEO
:
302 /* Fills video buffer */
304 memcpy (&voutp
[pos
], ptr
, cpysize
);
306 case TM6000_URB_MSG_AUDIO
:
307 tm6000_call_fillbuf(dev
, TM6000_AUDIO
, ptr
, cpysize
);
309 case TM6000_URB_MSG_VBI
:
310 /* Need some code to copy vbi buffer */
312 case TM6000_URB_MSG_PTS
:
313 /* Need some code to copy pts */
317 if (ptr
+ pktsize
> endp
) {
318 /* End of URB packet, but cmd processing is not
319 * complete. Preserve the state for a next packet
321 dev
->isoc_ctl
.pos
= pos
+ cpysize
;
322 dev
->isoc_ctl
.size
= size
- cpysize
;
323 dev
->isoc_ctl
.cmd
= cmd
;
324 dev
->isoc_ctl
.pktsize
= pktsize
- (endp
- ptr
);
327 dev
->isoc_ctl
.cmd
= 0;
335 * Identify the tm5600/6000 buffer header type and properly handles
337 static int copy_multiplexed(u8
*ptr
, unsigned long len
,
340 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
341 struct tm6000_core
*dev
= container_of(dma_q
,struct tm6000_core
,vidq
);
342 unsigned int pos
=dev
->isoc_ctl
.pos
,cpysize
;
344 struct tm6000_buffer
*buf
;
347 get_next_buf(dma_q
, &buf
);
349 outp
= videobuf_to_vmalloc(&buf
->vb
);
355 cpysize
=min(len
,buf
->vb
.size
-pos
);
356 //printk("Copying %d bytes (max=%lu) from %p to %p[%u]\n",cpysize,(*buf)->vb.size,ptr,out_p,pos);
357 memcpy(&outp
[pos
], ptr
, cpysize
);
361 if (pos
>= buf
->vb
.size
) {
363 /* Announces that a new buffer were filled */
364 buffer_filled (dev
, dma_q
, buf
);
365 dprintk(dev
, V4L2_DEBUG_ISOC
, "new buffer filled\n");
366 get_next_buf (dma_q
, &buf
);
369 outp
= videobuf_to_vmalloc(&(buf
->vb
));
376 dev
->isoc_ctl
.pos
=pos
;
380 static void inline print_err_status (struct tm6000_core
*dev
,
381 int packet
, int status
)
383 char *errmsg
= "Unknown";
387 errmsg
= "unlinked synchronuously";
390 errmsg
= "unlinked asynchronuously";
393 errmsg
= "Buffer error (overrun)";
396 errmsg
= "Stalled (device not responding)";
399 errmsg
= "Babble (bad cable?)";
402 errmsg
= "Bit-stuff error (bad cable?)";
405 errmsg
= "CRC/Timeout (could be anything)";
408 errmsg
= "Device does not respond";
412 dprintk(dev
, V4L2_DEBUG_QUEUE
, "URB status %d [%s].\n",
415 dprintk(dev
, V4L2_DEBUG_QUEUE
, "URB packet %d, status %d [%s].\n",
416 packet
, status
, errmsg
);
422 * Controls the isoc copy of each urb packet
424 static inline int tm6000_isoc_copy(struct urb
*urb
)
426 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
427 struct tm6000_core
*dev
= container_of(dma_q
,struct tm6000_core
,vidq
);
428 int i
, len
=0, rc
=1, status
;
431 if (urb
->status
< 0) {
432 print_err_status (dev
, -1, urb
->status
);
436 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
437 status
= urb
->iso_frame_desc
[i
].status
;
440 print_err_status (dev
,i
,status
);
444 len
= urb
->iso_frame_desc
[i
].actual_length
;
447 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
448 if (!urb
->iso_frame_desc
[i
].status
) {
449 if ((dev
->fourcc
)==V4L2_PIX_FMT_TM6000
) {
450 rc
=copy_multiplexed(p
, len
, urb
);
454 copy_streams(p
, len
, urb
);
462 /* ------------------------------------------------------------------
464 ------------------------------------------------------------------*/
467 * IRQ callback, called by URB callback
469 static void tm6000_irq_callback(struct urb
*urb
)
471 struct tm6000_dmaqueue
*dma_q
= urb
->context
;
472 struct tm6000_core
*dev
= container_of(dma_q
, struct tm6000_core
, vidq
);
478 spin_lock(&dev
->slock
);
479 tm6000_isoc_copy(urb
);
480 spin_unlock(&dev
->slock
);
482 /* Reset urb buffers */
483 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
484 urb
->iso_frame_desc
[i
].status
= 0;
485 urb
->iso_frame_desc
[i
].actual_length
= 0;
488 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
490 tm6000_err("urb resubmit failed (error=%i)\n",
495 * Stop and Deallocate URBs
497 static void tm6000_uninit_isoc(struct tm6000_core
*dev
)
502 dev
->isoc_ctl
.buf
= NULL
;
503 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
504 urb
=dev
->isoc_ctl
.urb
[i
];
508 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
509 usb_free_coherent(dev
->udev
,
510 urb
->transfer_buffer_length
,
511 dev
->isoc_ctl
.transfer_buffer
[i
],
515 dev
->isoc_ctl
.urb
[i
] = NULL
;
517 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
520 kfree (dev
->isoc_ctl
.urb
);
521 kfree (dev
->isoc_ctl
.transfer_buffer
);
523 dev
->isoc_ctl
.urb
=NULL
;
524 dev
->isoc_ctl
.transfer_buffer
=NULL
;
525 dev
->isoc_ctl
.num_bufs
= 0;
529 * Allocate URBs and start IRQ
531 static int tm6000_prepare_isoc(struct tm6000_core
*dev
, unsigned int framesize
)
533 struct tm6000_dmaqueue
*dma_q
= &dev
->vidq
;
534 int i
, j
, sb_size
, pipe
, size
, max_packets
, num_bufs
= 8;
537 /* De-allocates all pending stuff */
538 tm6000_uninit_isoc(dev
);
540 usb_set_interface(dev
->udev
,
541 dev
->isoc_in
.bInterfaceNumber
,
542 dev
->isoc_in
.bAlternateSetting
);
544 pipe
= usb_rcvisocpipe(dev
->udev
,
545 dev
->isoc_in
.endp
->desc
.bEndpointAddress
&
546 USB_ENDPOINT_NUMBER_MASK
);
548 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
550 if (size
> dev
->isoc_in
.maxsize
)
551 size
= dev
->isoc_in
.maxsize
;
553 dev
->isoc_ctl
.max_pkt_size
= size
;
555 max_packets
= ( framesize
+ size
- 1) / size
;
557 if (max_packets
> TM6000_MAX_ISO_PACKETS
)
558 max_packets
= TM6000_MAX_ISO_PACKETS
;
560 sb_size
= max_packets
* size
;
562 dev
->isoc_ctl
.num_bufs
= num_bufs
;
564 dev
->isoc_ctl
.urb
= kmalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
565 if (!dev
->isoc_ctl
.urb
) {
566 tm6000_err("cannot alloc memory for usb buffers\n");
570 dev
->isoc_ctl
.transfer_buffer
= kmalloc(sizeof(void *)*num_bufs
,
572 if (!dev
->isoc_ctl
.transfer_buffer
) {
573 tm6000_err("cannot allocate memory for usbtransfer\n");
574 kfree(dev
->isoc_ctl
.urb
);
578 dprintk(dev
, V4L2_DEBUG_QUEUE
, "Allocating %d x %d packets"
579 " (%d bytes) of %d bytes each to handle %u size\n",
580 max_packets
, num_bufs
, sb_size
,
581 dev
->isoc_in
.maxsize
, size
);
583 /* allocate urbs and transfer buffers */
584 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
585 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
587 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i
);
588 tm6000_uninit_isoc(dev
);
592 dev
->isoc_ctl
.urb
[i
] = urb
;
594 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->udev
,
595 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
596 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
597 tm6000_err ("unable to allocate %i bytes for transfer"
600 in_interrupt()?" while in int":"");
601 tm6000_uninit_isoc(dev
);
604 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
606 usb_fill_bulk_urb(urb
, dev
->udev
, pipe
,
607 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
608 tm6000_irq_callback
, dma_q
);
609 urb
->interval
= dev
->isoc_in
.endp
->desc
.bInterval
;
610 urb
->number_of_packets
= max_packets
;
611 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
613 for (j
= 0; j
< max_packets
; j
++) {
614 urb
->iso_frame_desc
[j
].offset
= size
* j
;
615 urb
->iso_frame_desc
[j
].length
= size
;
622 static int tm6000_start_thread( struct tm6000_core
*dev
)
624 struct tm6000_dmaqueue
*dma_q
= &dev
->vidq
;
628 dma_q
->ini_jiffies
=jiffies
;
630 init_waitqueue_head(&dma_q
->wq
);
632 /* submit urbs and enables IRQ */
633 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
634 int rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
636 tm6000_err("submit of urb %i failed (error=%i)\n", i
,
638 tm6000_uninit_isoc(dev
);
646 /* ------------------------------------------------------------------
648 ------------------------------------------------------------------*/
651 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
, unsigned int *size
)
653 struct tm6000_fh
*fh
= vq
->priv_data
;
655 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
657 *count
= TM6000_DEF_BUF
;
659 if (*count
< TM6000_MIN_BUF
) {
660 *count
=TM6000_MIN_BUF
;
663 while (*size
* *count
> vid_limit
* 1024 * 1024)
669 static void free_buffer(struct videobuf_queue
*vq
, struct tm6000_buffer
*buf
)
671 struct tm6000_fh
*fh
= vq
->priv_data
;
672 struct tm6000_core
*dev
= fh
->dev
;
678 /* We used to wait for the buffer to finish here, but this didn't work
679 because, as we were keeping the state as VIDEOBUF_QUEUED,
680 videobuf_queue_cancel marked it as finished for us.
681 (Also, it could wedge forever if the hardware was misconfigured.)
683 This should be safe; by the time we get here, the buffer isn't
684 queued anymore. If we ever start marking the buffers as
685 VIDEOBUF_ACTIVE, it won't be, though.
687 spin_lock_irqsave(&dev
->slock
, flags
);
688 if (dev
->isoc_ctl
.buf
== buf
)
689 dev
->isoc_ctl
.buf
= NULL
;
690 spin_unlock_irqrestore(&dev
->slock
, flags
);
692 videobuf_vmalloc_free(&buf
->vb
);
693 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
697 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
698 enum v4l2_field field
)
700 struct tm6000_fh
*fh
= vq
->priv_data
;
701 struct tm6000_buffer
*buf
= container_of(vb
,struct tm6000_buffer
,vb
);
702 struct tm6000_core
*dev
= fh
->dev
;
703 int rc
= 0, urb_init
= 0;
705 BUG_ON(NULL
== fh
->fmt
);
708 /* The only currently supported format is 16 bits/pixel */
709 buf
->vb
.size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
710 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
713 if (buf
->fmt
!= fh
->fmt
||
714 buf
->vb
.width
!= fh
->width
||
715 buf
->vb
.height
!= fh
->height
||
716 buf
->vb
.field
!= field
) {
718 buf
->vb
.width
= fh
->width
;
719 buf
->vb
.height
= fh
->height
;
720 buf
->vb
.field
= field
;
721 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
724 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
725 if (0 != (rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
)))
730 if (!dev
->isoc_ctl
.num_bufs
)
734 rc
= tm6000_prepare_isoc(dev
, buf
->vb
.size
);
738 rc
= tm6000_start_thread(dev
);
744 buf
->vb
.state
= VIDEOBUF_PREPARED
;
748 free_buffer(vq
, buf
);
753 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
755 struct tm6000_buffer
*buf
= container_of(vb
,struct tm6000_buffer
,vb
);
756 struct tm6000_fh
*fh
= vq
->priv_data
;
757 struct tm6000_core
*dev
= fh
->dev
;
758 struct tm6000_dmaqueue
*vidq
= &dev
->vidq
;
760 buf
->vb
.state
= VIDEOBUF_QUEUED
;
761 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
764 static void buffer_release(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
766 struct tm6000_buffer
*buf
= container_of(vb
,struct tm6000_buffer
,vb
);
771 static struct videobuf_queue_ops tm6000_video_qops
= {
772 .buf_setup
= buffer_setup
,
773 .buf_prepare
= buffer_prepare
,
774 .buf_queue
= buffer_queue
,
775 .buf_release
= buffer_release
,
778 /* ------------------------------------------------------------------
780 ------------------------------------------------------------------*/
782 static int res_get(struct tm6000_core
*dev
, struct tm6000_fh
*fh
)
785 mutex_lock(&dev
->lock
);
786 if (dev
->resources
) {
787 /* no, someone else uses it */
788 mutex_unlock(&dev
->lock
);
791 /* it's free, grab it */
793 dprintk(dev
, V4L2_DEBUG_RES_LOCK
, "res: get\n");
794 mutex_unlock(&dev
->lock
);
798 static int res_locked(struct tm6000_core
*dev
)
800 return (dev
->resources
);
803 static void res_free(struct tm6000_core
*dev
, struct tm6000_fh
*fh
)
805 mutex_lock(&dev
->lock
);
807 dprintk(dev
, V4L2_DEBUG_RES_LOCK
, "res: put\n");
808 mutex_unlock(&dev
->lock
);
811 /* ------------------------------------------------------------------
812 IOCTL vidioc handling
813 ------------------------------------------------------------------*/
814 static int vidioc_querycap (struct file
*file
, void *priv
,
815 struct v4l2_capability
*cap
)
817 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
819 strlcpy(cap
->driver
, "tm6000", sizeof(cap
->driver
));
820 strlcpy(cap
->card
,"Trident TVMaster TM5600/6000/6010", sizeof(cap
->card
));
821 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
822 cap
->version
= TM6000_VERSION
;
823 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
830 static int vidioc_enum_fmt_vid_cap (struct file
*file
, void *priv
,
831 struct v4l2_fmtdesc
*f
)
833 if (unlikely(f
->index
>= ARRAY_SIZE(format
)))
836 strlcpy(f
->description
,format
[f
->index
].name
,sizeof(f
->description
));
837 f
->pixelformat
= format
[f
->index
].fourcc
;
841 static int vidioc_g_fmt_vid_cap (struct file
*file
, void *priv
,
842 struct v4l2_format
*f
)
844 struct tm6000_fh
*fh
=priv
;
846 f
->fmt
.pix
.width
= fh
->width
;
847 f
->fmt
.pix
.height
= fh
->height
;
848 f
->fmt
.pix
.field
= fh
->vb_vidq
.field
;
849 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
850 f
->fmt
.pix
.bytesperline
=
851 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
852 f
->fmt
.pix
.sizeimage
=
853 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
858 static struct tm6000_fmt
* format_by_fourcc(unsigned int fourcc
)
862 for (i
= 0; i
< ARRAY_SIZE(format
); i
++)
863 if (format
[i
].fourcc
== fourcc
)
868 static int vidioc_try_fmt_vid_cap (struct file
*file
, void *priv
,
869 struct v4l2_format
*f
)
871 struct tm6000_core
*dev
= ((struct tm6000_fh
*)priv
)->dev
;
872 struct tm6000_fmt
*fmt
;
873 enum v4l2_field field
;
875 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
877 dprintk(dev
, V4L2_DEBUG_IOCTL_ARG
, "Fourcc format (0x%08x)"
878 " invalid.\n", f
->fmt
.pix
.pixelformat
);
882 field
= f
->fmt
.pix
.field
;
884 if (field
== V4L2_FIELD_ANY
) {
885 // field=V4L2_FIELD_INTERLACED;
886 field
=V4L2_FIELD_SEQ_TB
;
887 } else if (V4L2_FIELD_INTERLACED
!= field
) {
888 dprintk(dev
, V4L2_DEBUG_IOCTL_ARG
, "Field type invalid.\n");
892 tm6000_get_std_res (dev
);
894 f
->fmt
.pix
.width
= dev
->width
;
895 f
->fmt
.pix
.height
= dev
->height
;
897 f
->fmt
.pix
.width
&= ~0x01;
899 f
->fmt
.pix
.field
= field
;
901 f
->fmt
.pix
.bytesperline
=
902 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
903 f
->fmt
.pix
.sizeimage
=
904 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
909 static int vidioc_s_fmt_vid_cap (struct file
*file
, void *priv
,
910 struct v4l2_format
*f
)
912 struct tm6000_fh
*fh
=priv
;
913 struct tm6000_core
*dev
= fh
->dev
;
914 int ret
= vidioc_try_fmt_vid_cap(file
,fh
,f
);
918 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
919 fh
->width
= f
->fmt
.pix
.width
;
920 fh
->height
= f
->fmt
.pix
.height
;
921 fh
->vb_vidq
.field
= f
->fmt
.pix
.field
;
924 dev
->fourcc
= f
->fmt
.pix
.pixelformat
;
926 tm6000_set_fourcc_format(dev
);
931 static int vidioc_reqbufs (struct file
*file
, void *priv
,
932 struct v4l2_requestbuffers
*p
)
934 struct tm6000_fh
*fh
=priv
;
936 return (videobuf_reqbufs(&fh
->vb_vidq
, p
));
939 static int vidioc_querybuf (struct file
*file
, void *priv
,
940 struct v4l2_buffer
*p
)
942 struct tm6000_fh
*fh
=priv
;
944 return (videobuf_querybuf(&fh
->vb_vidq
, p
));
947 static int vidioc_qbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
949 struct tm6000_fh
*fh
=priv
;
951 return (videobuf_qbuf(&fh
->vb_vidq
, p
));
954 static int vidioc_dqbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
956 struct tm6000_fh
*fh
=priv
;
958 return (videobuf_dqbuf(&fh
->vb_vidq
, p
,
959 file
->f_flags
& O_NONBLOCK
));
962 #ifdef CONFIG_VIDEO_V4L1_COMPAT
963 static int vidiocgmbuf (struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
965 struct tm6000_fh
*fh
=priv
;
967 return videobuf_cgmbuf (&fh
->vb_vidq
, mbuf
, 8);
971 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
973 struct tm6000_fh
*fh
=priv
;
974 struct tm6000_core
*dev
= fh
->dev
;
976 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
981 if (!res_get(dev
,fh
))
983 return (videobuf_streamon(&fh
->vb_vidq
));
986 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
988 struct tm6000_fh
*fh
=priv
;
989 struct tm6000_core
*dev
= fh
->dev
;
991 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
996 videobuf_streamoff(&fh
->vb_vidq
);
1002 static int vidioc_s_std (struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1005 struct tm6000_fh
*fh
=priv
;
1006 struct tm6000_core
*dev
= fh
->dev
;
1008 rc
=tm6000_set_standard (dev
, norm
);
1010 fh
->width
= dev
->width
;
1011 fh
->height
= dev
->height
;
1016 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, dev
->norm
);
1021 static int vidioc_enum_input (struct file
*file
, void *priv
,
1022 struct v4l2_input
*inp
)
1024 switch (inp
->index
) {
1025 case TM6000_INPUT_TV
:
1026 inp
->type
= V4L2_INPUT_TYPE_TUNER
;
1027 strcpy(inp
->name
,"Television");
1029 case TM6000_INPUT_COMPOSITE
:
1030 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1031 strcpy(inp
->name
,"Composite");
1033 case TM6000_INPUT_SVIDEO
:
1034 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1035 strcpy(inp
->name
,"S-Video");
1040 inp
->std
= TM6000_STD
;
1045 static int vidioc_g_input (struct file
*file
, void *priv
, unsigned int *i
)
1047 struct tm6000_fh
*fh
=priv
;
1048 struct tm6000_core
*dev
= fh
->dev
;
1054 static int vidioc_s_input (struct file
*file
, void *priv
, unsigned int i
)
1056 struct tm6000_fh
*fh
=priv
;
1057 struct tm6000_core
*dev
= fh
->dev
;
1062 case TM6000_INPUT_TV
:
1066 case TM6000_INPUT_COMPOSITE
:
1067 case TM6000_INPUT_SVIDEO
:
1074 rc
=tm6000_read_write_usb (dev
, USB_DIR_OUT
| USB_TYPE_VENDOR
,
1075 REQ_03_SET_GET_MCU_PIN
, 0x03, 1, buf
, 1);
1079 rc
=vidioc_s_std (file
, priv
, &dev
->vfd
->current_norm
);
1085 /* --- controls ---------------------------------------------- */
1086 static int vidioc_queryctrl (struct file
*file
, void *priv
,
1087 struct v4l2_queryctrl
*qc
)
1091 for (i
= 0; i
< ARRAY_SIZE(tm6000_qctrl
); i
++)
1092 if (qc
->id
&& qc
->id
== tm6000_qctrl
[i
].id
) {
1093 memcpy(qc
, &(tm6000_qctrl
[i
]),
1101 static int vidioc_g_ctrl (struct file
*file
, void *priv
,
1102 struct v4l2_control
*ctrl
)
1104 struct tm6000_fh
*fh
=priv
;
1105 struct tm6000_core
*dev
= fh
->dev
;
1109 case V4L2_CID_CONTRAST
:
1110 val
= tm6000_get_reg(dev
, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ
, 0);
1112 case V4L2_CID_BRIGHTNESS
:
1113 val
= tm6000_get_reg(dev
, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ
, 0);
1115 case V4L2_CID_SATURATION
:
1116 val
= tm6000_get_reg(dev
, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ
, 0);
1119 val
= tm6000_get_reg(dev
, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ
, 0);
1132 static int vidioc_s_ctrl (struct file
*file
, void *priv
,
1133 struct v4l2_control
*ctrl
)
1135 struct tm6000_fh
*fh
=priv
;
1136 struct tm6000_core
*dev
= fh
->dev
;
1140 case V4L2_CID_CONTRAST
:
1141 tm6000_set_reg(dev
, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ
, val
);
1143 case V4L2_CID_BRIGHTNESS
:
1144 tm6000_set_reg(dev
, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ
, val
);
1146 case V4L2_CID_SATURATION
:
1147 tm6000_set_reg(dev
, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ
, val
);
1150 tm6000_set_reg(dev
, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ
, val
);
1156 static int vidioc_g_tuner (struct file
*file
, void *priv
,
1157 struct v4l2_tuner
*t
)
1159 struct tm6000_fh
*fh
=priv
;
1160 struct tm6000_core
*dev
= fh
->dev
;
1162 if (unlikely(UNSET
== dev
->tuner_type
))
1167 strcpy(t
->name
, "Television");
1168 t
->type
= V4L2_TUNER_ANALOG_TV
;
1169 t
->capability
= V4L2_TUNER_CAP_NORM
;
1170 t
->rangehigh
= 0xffffffffUL
;
1171 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1176 static int vidioc_s_tuner (struct file
*file
, void *priv
,
1177 struct v4l2_tuner
*t
)
1179 struct tm6000_fh
*fh
=priv
;
1180 struct tm6000_core
*dev
= fh
->dev
;
1182 if (UNSET
== dev
->tuner_type
)
1190 static int vidioc_g_frequency (struct file
*file
, void *priv
,
1191 struct v4l2_frequency
*f
)
1193 struct tm6000_fh
*fh
=priv
;
1194 struct tm6000_core
*dev
= fh
->dev
;
1196 if (unlikely(UNSET
== dev
->tuner_type
))
1199 f
->type
= V4L2_TUNER_ANALOG_TV
;
1200 f
->frequency
= dev
->freq
;
1202 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, f
);
1207 static int vidioc_s_frequency (struct file
*file
, void *priv
,
1208 struct v4l2_frequency
*f
)
1210 struct tm6000_fh
*fh
=priv
;
1211 struct tm6000_core
*dev
= fh
->dev
;
1213 if (unlikely(f
->type
!= V4L2_TUNER_ANALOG_TV
))
1216 if (unlikely(UNSET
== dev
->tuner_type
))
1218 if (unlikely(f
->tuner
!= 0))
1221 // mutex_lock(&dev->lock);
1222 dev
->freq
= f
->frequency
;
1223 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, f
);
1224 // mutex_unlock(&dev->lock);
1229 /* ------------------------------------------------------------------
1230 File operations for the device
1231 ------------------------------------------------------------------*/
1233 static int tm6000_open(struct file
*file
)
1235 struct video_device
*vdev
= video_devdata(file
);
1236 struct tm6000_core
*dev
= video_drvdata(file
);
1237 struct tm6000_fh
*fh
;
1238 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1241 printk(KERN_INFO
"tm6000: open called (dev=%s)\n",
1242 video_device_node_name(vdev
));
1244 dprintk(dev
, V4L2_DEBUG_OPEN
, "tm6000: open called (dev=%s)\n",
1245 video_device_node_name(vdev
));
1248 /* If more than one user, mutex should be added */
1251 dprintk(dev
, V4L2_DEBUG_OPEN
, "open dev=%s type=%s users=%d\n",
1252 video_device_node_name(vdev
), v4l2_type_names
[type
],
1255 /* allocate + initialize per filehandle data */
1256 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
1262 file
->private_data
= fh
;
1265 fh
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1266 dev
->fourcc
= format
[0].fourcc
;
1268 fh
->fmt
= format_by_fourcc(dev
->fourcc
);
1270 tm6000_get_std_res (dev
);
1272 fh
->width
= dev
->width
;
1273 fh
->height
= dev
->height
;
1275 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: fh=0x%08lx, dev=0x%08lx, "
1276 "dev->vidq=0x%08lx\n",
1277 (unsigned long)fh
,(unsigned long)dev
,(unsigned long)&dev
->vidq
);
1278 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: list_empty "
1279 "queued=%d\n",list_empty(&dev
->vidq
.queued
));
1280 dprintk(dev
, V4L2_DEBUG_OPEN
, "Open: list_empty "
1281 "active=%d\n",list_empty(&dev
->vidq
.active
));
1283 /* initialize hardware on analog mode */
1284 if (dev
->mode
!=TM6000_MODE_ANALOG
) {
1285 rc
=tm6000_init_analog_mode (dev
);
1289 /* Put all controls at a sane state */
1290 for (i
= 0; i
< ARRAY_SIZE(tm6000_qctrl
); i
++)
1291 qctl_regs
[i
] =tm6000_qctrl
[i
].default_value
;
1293 dev
->mode
=TM6000_MODE_ANALOG
;
1296 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &tm6000_video_qops
,
1299 V4L2_FIELD_INTERLACED
,
1300 sizeof(struct tm6000_buffer
),fh
);
1306 tm6000_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*pos
)
1308 struct tm6000_fh
*fh
= file
->private_data
;
1310 if (fh
->type
==V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1311 if (res_locked(fh
->dev
))
1314 return videobuf_read_stream(&fh
->vb_vidq
, data
, count
, pos
, 0,
1315 file
->f_flags
& O_NONBLOCK
);
1321 tm6000_poll(struct file
*file
, struct poll_table_struct
*wait
)
1323 struct tm6000_fh
*fh
= file
->private_data
;
1324 struct tm6000_buffer
*buf
;
1326 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= fh
->type
)
1329 if (res_get(fh
->dev
,fh
)) {
1330 /* streaming capture */
1331 if (list_empty(&fh
->vb_vidq
.stream
))
1333 buf
= list_entry(fh
->vb_vidq
.stream
.next
,struct tm6000_buffer
,vb
.stream
);
1335 /* read() capture */
1336 return videobuf_poll_stream(file
, &fh
->vb_vidq
,
1339 poll_wait(file
, &buf
->vb
.done
, wait
);
1340 if (buf
->vb
.state
== VIDEOBUF_DONE
||
1341 buf
->vb
.state
== VIDEOBUF_ERROR
)
1342 return POLLIN
|POLLRDNORM
;
1346 static int tm6000_release(struct file
*file
)
1348 struct tm6000_fh
*fh
= file
->private_data
;
1349 struct tm6000_core
*dev
= fh
->dev
;
1350 struct video_device
*vdev
= video_devdata(file
);
1352 dprintk(dev
, V4L2_DEBUG_OPEN
, "tm6000: close called (dev=%s, users=%d)\n",
1353 video_device_node_name(vdev
), dev
->users
);
1358 tm6000_uninit_isoc(dev
);
1359 videobuf_mmap_free(&fh
->vb_vidq
);
1367 static int tm6000_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1369 struct tm6000_fh
*fh
= file
->private_data
;
1372 ret
=videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1377 static struct v4l2_file_operations tm6000_fops
= {
1378 .owner
= THIS_MODULE
,
1379 .open
= tm6000_open
,
1380 .release
= tm6000_release
,
1381 .ioctl
= video_ioctl2
, /* V4L2 ioctl handler */
1382 .read
= tm6000_read
,
1383 .poll
= tm6000_poll
,
1384 .mmap
= tm6000_mmap
,
1387 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1388 .vidioc_querycap
= vidioc_querycap
,
1389 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1390 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1391 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1392 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1393 .vidioc_s_std
= vidioc_s_std
,
1394 .vidioc_enum_input
= vidioc_enum_input
,
1395 .vidioc_g_input
= vidioc_g_input
,
1396 .vidioc_s_input
= vidioc_s_input
,
1397 .vidioc_queryctrl
= vidioc_queryctrl
,
1398 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1399 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1400 .vidioc_g_tuner
= vidioc_g_tuner
,
1401 .vidioc_s_tuner
= vidioc_s_tuner
,
1402 .vidioc_g_frequency
= vidioc_g_frequency
,
1403 .vidioc_s_frequency
= vidioc_s_frequency
,
1404 .vidioc_streamon
= vidioc_streamon
,
1405 .vidioc_streamoff
= vidioc_streamoff
,
1406 .vidioc_reqbufs
= vidioc_reqbufs
,
1407 .vidioc_querybuf
= vidioc_querybuf
,
1408 .vidioc_qbuf
= vidioc_qbuf
,
1409 .vidioc_dqbuf
= vidioc_dqbuf
,
1410 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1411 .vidiocgmbuf
= vidiocgmbuf
,
1415 static struct video_device tm6000_template
= {
1417 .fops
= &tm6000_fops
,
1418 .ioctl_ops
= &video_ioctl_ops
,
1419 .release
= video_device_release
,
1420 .tvnorms
= TM6000_STD
,
1421 .current_norm
= V4L2_STD_NTSC_M
,
1424 /* -----------------------------------------------------------------
1425 Initialization and module stuff
1426 ------------------------------------------------------------------*/
1428 int tm6000_v4l2_register(struct tm6000_core
*dev
)
1431 struct video_device
*vfd
;
1433 vfd
= video_device_alloc();
1439 /* init video dma queues */
1440 INIT_LIST_HEAD(&dev
->vidq
.active
);
1441 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1443 memcpy (dev
->vfd
, &tm6000_template
, sizeof(*(dev
->vfd
)));
1444 dev
->vfd
->debug
=tm6000_debug
;
1445 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1446 video_set_drvdata(vfd
, dev
);
1448 ret
= video_register_device(dev
->vfd
, VFL_TYPE_GRABBER
, video_nr
);
1449 printk(KERN_INFO
"Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret
);
1453 int tm6000_v4l2_unregister(struct tm6000_core
*dev
)
1455 video_unregister_device(dev
->vfd
);
1460 int tm6000_v4l2_exit(void)
1465 module_param(video_nr
, int, 0);
1466 MODULE_PARM_DESC(video_nr
,"Allow changing video device number");
1468 module_param_named (debug
, tm6000_debug
, int, 0444);
1469 MODULE_PARM_DESC(debug
,"activates debug info");
1471 module_param(vid_limit
,int,0644);
1472 MODULE_PARM_DESC(vid_limit
,"capture memory limit in megabytes");