2 tm6000-video.c - driver for TM5600/TM6000 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 #ifdef CONFIG_VIDEO_V4L1_COMPAT
37 #include <linux/videodev.h>
39 #include <linux/interrupt.h>
40 #include <linux/kthread.h>
41 #include <linux/highmem.h>
42 #include <linux/freezer.h>
44 #include "tm6000-regs.h"
47 #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
49 /* Limits minimum and default number of buffers */
50 #define TM6000_MIN_BUF 4
51 #define TM6000_DEF_BUF 8
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 */
57 unsigned long tm6000_devused;
62 /* supported controls */
63 static struct v4l2_queryctrl tm6000_qctrl[] = {
65 .id = V4L2_CID_BRIGHTNESS,
66 .type = V4L2_CTRL_TYPE_INTEGER,
74 .id = V4L2_CID_CONTRAST,
75 .type = V4L2_CTRL_TYPE_INTEGER,
83 .id = V4L2_CID_SATURATION,
84 .type = V4L2_CTRL_TYPE_INTEGER,
93 .type = V4L2_CTRL_TYPE_INTEGER,
98 .default_value = 0, //4 ?
103 static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
105 static struct tm6000_fmt format[] = {
107 .name = "4:2:2, packed, YVY2",
108 .fourcc = V4L2_PIX_FMT_YUYV,
111 .name = "4:2:2, packed, UYVY",
112 .fourcc = V4L2_PIX_FMT_UYVY,
115 .name = "A/V + VBI mux packet",
116 .fourcc = V4L2_PIX_FMT_TM6000,
121 static LIST_HEAD(tm6000_corelist);
123 /* ------------------------------------------------------------------
124 DMA and thread functions
125 ------------------------------------------------------------------*/
127 #define norm_maxw(a) 720
128 #define norm_maxh(a) 480
130 //#define norm_minw(a) norm_maxw(a)
131 #define norm_minw(a) norm_maxw(a)
132 #define norm_minh(a) norm_maxh(a)
135 * video-buf generic routine to get the next available buffer
137 static int inline get_next_buf (struct tm6000_dmaqueue *dma_q,
138 struct tm6000_buffer **buf)
140 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
142 if (list_empty(&dma_q->active)) {
143 dprintk(dev, V4L2_DEBUG_QUEUE,"No active queue to serve\n");
147 *buf = list_entry(dma_q->active.next,
148 struct tm6000_buffer, vb.queue);
150 /* Nobody is waiting something to be done, just return */
151 if (!waitqueue_active(&(*buf)->vb.done)) {
152 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
160 * Announces that a buffer were filled and request the next
162 static void inline buffer_filled (struct tm6000_core *dev,
163 struct tm6000_buffer *buf)
165 /* Advice that buffer was filled */
166 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] wakeup\n",buf,buf->vb.i);
167 buf->vb.state = STATE_DONE;
168 buf->vb.field_count++;
169 do_gettimeofday(&buf->vb.ts);
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
176 * Macro to allow copying data into the proper memory type
179 #define bufcpy(buf,out_ptr,in_ptr,size) \
181 if (__copy_to_user(out_ptr,in_ptr,size)!=0) \
182 tm6000_err("copy_to_user failed.\n"); \
186 * Identify the tm5600/6000 buffer header type and properly handles
188 static int copy_streams(u8 *data, u8 *out_p, unsigned long len,
189 struct urb *urb, struct tm6000_buffer **buf)
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;
195 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos=0;
196 unsigned long header;
199 /* FIXME: this is the hardcoded window size
201 unsigned int linesize=720*2;
203 //static int last_line=-2;
205 for (ptr=data; ptr<endp;) {
206 if (!dev->isoc_ctl.cmd) {
208 for (ptr+=3;ptr<endp;ptr++) {
217 /* Get message header */
218 header=*(unsigned long *)ptr;
220 c=(header>>24) & 0xff;
222 /* split the header fields */
223 size = (((header & 0x7e)<<1) -1) *4;
224 block = (header>>7) & 0xf;
225 field = (header>>11) & 0x1;
226 line = (header>>12) & 0x1ff;
227 cmd = (header>>21) & 0x7;
229 /* FIXME: Maximum possible line is 511.
230 * This doesn't seem to be enough for PAL standards
233 /* Validates header fields */
234 if(size>TM6000_URB_MSG_LEN)
235 size=TM6000_URB_MSG_LEN;
237 cmd = TM6000_URB_MSG_ERR;
239 /* FIXME: Mounts the image as field0+field1
240 * It should, instead, check if the user selected
241 * entrelaced or non-entrelaced mode
243 pos=((line<<1)+field)*linesize+
244 block*TM6000_URB_MSG_LEN;
248 /* Don't allow to write out of the buffer */
249 if (pos+TM6000_URB_MSG_LEN > (*buf)->vb.size)
250 cmd = TM6000_URB_MSG_ERR;
252 /* Prints debug info */
253 dprintk(dev, V4L2_DEBUG_ISOC, "size=%d, num=%d, "
254 " line=%d, field=%d\n",
255 size, block, line, field);
257 dev->isoc_ctl.cmd = cmd;
258 dev->isoc_ctl.size = size;
259 dev->isoc_ctl.pos = pos;
260 dev->isoc_ctl.pktsize = pktsize = TM6000_URB_MSG_LEN;
262 cmd = dev->isoc_ctl.cmd;
263 size= dev->isoc_ctl.size;
264 pos = dev->isoc_ctl.pos;
265 pktsize = dev->isoc_ctl.pktsize;
267 cpysize=(endp-ptr>size)?size:endp-ptr;
270 /* handles each different URB message */
272 case TM6000_URB_MSG_VIDEO:
273 /* Fills video buffer */
274 bufcpy(*buf,&out_p[pos],ptr,cpysize);
279 /* End of URB packet, but cmd processing is not
280 * complete. Preserve the state for a next packet
282 dev->isoc_ctl.pos = pos+cpysize;
283 dev->isoc_ctl.size= size-cpysize;
284 dev->isoc_ctl.cmd = cmd;
285 dev->isoc_ctl.pktsize = pktsize-cpysize;
288 dev->isoc_ctl.cmd = 0;
296 * Identify the tm5600/6000 buffer header type and properly handles
298 static int copy_multiplexed(u8 *ptr, u8 *out_p, unsigned long len,
299 struct urb *urb, struct tm6000_buffer **buf)
301 struct tm6000_dmaqueue *dma_q = urb->context;
302 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
303 unsigned int pos=dev->isoc_ctl.pos,cpysize;
307 cpysize=min(len,(*buf)->vb.size-pos);
308 //printk("Copying %d bytes (max=%lu) from %p to %p[%u]\n",cpysize,(*buf)->vb.size,ptr,out_p,pos);
309 bufcpy(*buf,&out_p[pos],ptr,cpysize);
313 if (pos >= (*buf)->vb.size) {
315 /* Announces that a new buffer were filled */
316 buffer_filled (dev, *buf);
317 dprintk(dev, V4L2_DEBUG_QUEUE, "new buffer filled\n");
319 rc=get_next_buf (dma_q, buf);
322 printk(KERN_ERR "tm6000: buffer underrun\n");
328 dev->isoc_ctl.pos=pos;
333 * Controls the isoc copy of each urb packet
335 static inline int tm6000_isoc_copy(struct urb *urb, struct tm6000_buffer **buf)
337 struct tm6000_dmaqueue *dma_q = urb->context;
338 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
339 void *outp=videobuf_to_vmalloc (&((*buf)->vb));
341 int size=(*buf)->vb.size;
343 unsigned long copied;
348 for (i = 0; i < urb->number_of_packets; i++) {
349 int status = urb->iso_frame_desc[i].status;
350 char *errmsg = "Unknown";
354 errmsg = "unlinked synchronuously";
357 errmsg = "unlinked asynchronuously";
360 errmsg = "Buffer error (overrun)";
363 errmsg = "Stalled (device not responding)";
366 errmsg = "Babble (bad cable?)";
369 errmsg = "Bit-stuff error (bad cable?)";
372 errmsg = "CRC/Timeout (could be anything)";
375 errmsg = "Device does not respond";
378 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
384 len=urb->iso_frame_desc[i].actual_length;
386 if (len>=TM6000_URB_MSG_LEN) {
387 p=urb->transfer_buffer + urb->iso_frame_desc[i].offset;
388 if (!urb->iso_frame_desc[i].status) {
389 if (((*buf)->fmt->fourcc)==V4L2_PIX_FMT_TM6000) {
390 rc=copy_multiplexed(p,outp,len,urb,buf);
394 rc=copy_streams(p,outp,len,urb,buf);
403 if (((*buf)->fmt->fourcc)!=V4L2_PIX_FMT_TM6000) {
404 buffer_filled (dev, *buf);
405 dprintk(dev, V4L2_DEBUG_QUEUE, "new buffer filled\n");
411 /* ------------------------------------------------------------------
413 ------------------------------------------------------------------*/
416 * IRQ callback, called by URB callback
418 static void tm6000_irq_callback(struct urb *urb)
420 struct tm6000_buffer *buf;
421 struct tm6000_dmaqueue *dma_q = urb->context;
422 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
426 spin_lock_irqsave(&dev->slock,flags);
428 rc=get_next_buf (dma_q, &buf);
432 /* Copy data from URB */
433 rc=tm6000_isoc_copy(urb, &buf);
436 /* Reset urb buffers */
437 for (i = 0; i < urb->number_of_packets; i++) {
438 urb->iso_frame_desc[i].status = 0;
439 urb->iso_frame_desc[i].actual_length = 0;
443 if ((urb->status = usb_submit_urb(urb, GFP_ATOMIC))) {
444 tm6000_err("urb resubmit failed (error=%i)\n",
450 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
451 del_timer(&dma_q->timeout);
453 /* Data filled, reset watchdog */
454 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
457 spin_unlock_irqrestore(&dev->slock,flags);
461 * Stop and Deallocate URBs
463 static void tm6000_uninit_isoc(struct tm6000_core *dev)
468 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
469 urb=dev->isoc_ctl.urb[i];
473 if (dev->isoc_ctl.transfer_buffer[i]) {
474 usb_buffer_free(dev->udev,
475 urb->transfer_buffer_length,
476 dev->isoc_ctl.transfer_buffer[i],
480 dev->isoc_ctl.urb[i] = NULL;
482 dev->isoc_ctl.transfer_buffer[i] = NULL;
485 kfree (dev->isoc_ctl.urb);
486 kfree (dev->isoc_ctl.transfer_buffer);
487 dev->isoc_ctl.urb=NULL;
488 dev->isoc_ctl.transfer_buffer=NULL;
490 dev->isoc_ctl.num_bufs=0;
494 * Stop video thread - FIXME: Can be easily removed
496 static void tm6000_stop_thread(struct tm6000_dmaqueue *dma_q)
498 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
500 tm6000_uninit_isoc(dev);
505 * Allocate URBs and start IRQ
507 static int tm6000_prepare_isoc(struct tm6000_core *dev,
508 int max_packets, int num_bufs)
510 struct tm6000_dmaqueue *dma_q = &dev->vidq;
516 /* De-allocates all pending stuff */
517 tm6000_uninit_isoc(dev);
519 dev->isoc_ctl.num_bufs=num_bufs;
521 dev->isoc_ctl.urb=kmalloc(sizeof(void *)*num_bufs,
523 if (!dev->isoc_ctl.urb) {
524 tm6000_err("cannot alloc memory for usb buffers\n");
528 dev->isoc_ctl.transfer_buffer=kmalloc(sizeof(void *)*num_bufs,
530 if (!dev->isoc_ctl.urb) {
531 tm6000_err("cannot allocate memory for usbtransfer\n");
532 kfree(dev->isoc_ctl.urb);
536 dev->isoc_ctl.max_pkt_size=dev->max_isoc_in;
538 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
541 /* allocate urbs and transfer buffers */
542 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
543 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
545 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
546 tm6000_uninit_isoc(dev);
549 dev->isoc_ctl.urb[i] = urb;
551 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
553 &dev->isoc_ctl.urb[i]->transfer_dma);
554 if (!dev->isoc_ctl.transfer_buffer[i]) {
555 tm6000_err ("unable to allocate %i bytes for transfer"
556 " buffer %i\n", sb_size, i);
557 tm6000_uninit_isoc(dev);
560 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
562 pipe=usb_rcvisocpipe(dev->udev,
563 dev->isoc_in->desc.bEndpointAddress &
564 USB_ENDPOINT_NUMBER_MASK);
565 usb_fill_int_urb(urb, dev->udev, pipe,
566 dev->isoc_ctl.transfer_buffer[i],sb_size,
567 tm6000_irq_callback, dma_q,
568 dev->isoc_in->desc.bInterval);
570 urb->number_of_packets = max_packets;
571 urb->transfer_flags = URB_ISO_ASAP;
574 for (j = 0; j < max_packets; j++) {
575 urb->iso_frame_desc[j].offset = k;
576 urb->iso_frame_desc[j].length =
577 dev->isoc_ctl.max_pkt_size;
578 k += dev->isoc_ctl.max_pkt_size;
585 static int tm6000_start_thread( struct tm6000_dmaqueue *dma_q,
586 struct tm6000_buffer *buf)
588 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
592 dma_q->ini_jiffies=jiffies;
594 init_waitqueue_head(&dma_q->wq);
596 /* submit urbs and enables IRQ */
597 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
598 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
600 tm6000_err("submit of urb %i failed (error=%i)\n", i,
602 tm6000_uninit_isoc(dev);
613 static int restart_video_queue(struct tm6000_dmaqueue *dma_q)
615 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
617 struct tm6000_buffer *buf, *prev;
618 struct list_head *item;
620 dprintk(dev, V4L2_DEBUG_QUEUE, "%s dma_q=0x%08lx\n",
621 __FUNCTION__,(unsigned long)dma_q);
623 if (!list_empty(&dma_q->active)) {
624 buf = list_entry(dma_q->active.next, struct tm6000_buffer, vb.queue);
625 dprintk(dev, V4L2_DEBUG_QUEUE,
626 "restart_queue [%p/%d]: restart dma\n", buf, buf->vb.i);
628 dprintk(dev, V4L2_DEBUG_QUEUE, "Restarting video dma\n");
629 tm6000_stop_thread(dma_q);
630 tm6000_start_thread(dma_q, buf);
632 /* cancel all outstanding capture / vbi requests */
633 list_for_each(item,&dma_q->active) {
634 buf = list_entry(item, struct tm6000_buffer, vb.queue);
636 list_del(&buf->vb.queue);
637 buf->vb.state = STATE_ERROR;
638 wake_up(&buf->vb.done);
640 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
647 if (list_empty(&dma_q->queued))
649 buf = list_entry(dma_q->queued.next, struct tm6000_buffer, vb.queue);
651 list_del(&buf->vb.queue);
652 list_add_tail(&buf->vb.queue,&dma_q->active);
654 dprintk(dev, V4L2_DEBUG_QUEUE, "Restarting video dma\n");
655 tm6000_stop_thread(dma_q);
656 tm6000_start_thread(dma_q, buf);
658 buf->vb.state = STATE_ACTIVE;
659 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
660 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] restart_queue -"
661 " first active\n", buf, buf->vb.i);
663 } else if (prev->vb.width == buf->vb.width &&
664 prev->vb.height == buf->vb.height &&
665 prev->fmt == buf->fmt) {
666 list_del(&buf->vb.queue);
667 list_add_tail(&buf->vb.queue,&dma_q->active);
668 buf->vb.state = STATE_ACTIVE;
669 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] restart_queue -"
670 " move to active\n",buf,buf->vb.i);
678 static void tm6000_vid_timeout(unsigned long data)
680 struct tm6000_core *dev = (struct tm6000_core*)data;
681 struct tm6000_dmaqueue *vidq = &dev->vidq;
682 struct tm6000_buffer *buf;
685 spin_lock_irqsave(&dev->slock,flags);
686 while (!list_empty(&vidq->active)) {
687 buf = list_entry(vidq->active.next, struct tm6000_buffer,
689 list_del(&buf->vb.queue);
690 buf->vb.state = STATE_ERROR;
691 wake_up(&buf->vb.done);
692 dprintk(dev, V4L2_DEBUG_QUEUE, "tm6000/0: [%p/%d] timeout\n",
696 restart_video_queue(vidq);
697 spin_unlock_irqrestore(&dev->slock,flags);
700 /* ------------------------------------------------------------------
702 ------------------------------------------------------------------*/
705 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
707 struct tm6000_fh *fh = vq->priv_data;
709 *size = fh->fmt->depth * fh->width * fh->height >> 3;
711 *count = TM6000_DEF_BUF;
713 if (*count < TM6000_MIN_BUF) {
714 *count=TM6000_MIN_BUF;
717 printk("Requesting %d buffers\n",*count);
719 while (*size * *count > vid_limit * 1024 * 1024)
725 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
730 videobuf_waiton(&buf->vb,0,0);
731 videobuf_vmalloc_free(&buf->vb);
732 buf->vb.state = STATE_NEEDS_INIT;
736 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
737 enum v4l2_field field)
739 struct tm6000_fh *fh = vq->priv_data;
740 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
741 struct tm6000_core *dev = fh->dev;
742 int rc=0, urbsize, urb_init=0;
744 BUG_ON(NULL == fh->fmt);
746 if (fh->width < norm_minw(core) || fh->width > norm_maxw(core) ||
747 fh->height < norm_minh(core) || fh->height > norm_maxh(core)) {
748 dprintk(dev, V4L2_DEBUG_QUEUE, "Window size (%dx%d) is out of "
749 "supported range\n", fh->width, fh->height);
750 dprintk(dev, V4L2_DEBUG_QUEUE, "Valid range is from (%dx%d) to "
751 "(%dx%d)\n", norm_minw(core), norm_minh(core),
752 norm_maxw(core),norm_maxh(core));
756 /* FIXME: It assumes depth=2 */
757 /* The only currently supported format is 16 bits/pixel */
758 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
759 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
762 if (buf->fmt != fh->fmt ||
763 buf->vb.width != fh->width ||
764 buf->vb.height != fh->height ||
765 buf->vb.field != field) {
767 buf->vb.width = fh->width;
768 buf->vb.height = fh->height;
769 buf->vb.field = field;
770 buf->vb.state = STATE_NEEDS_INIT;
773 if (STATE_NEEDS_INIT == buf->vb.state) {
774 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
780 if (!dev->isoc_ctl.num_bufs)
784 /* Should allocate/request at least h
785 res x v res x 2 bytes/pixel */
786 urbsize=(buf->vb.size+dev->max_isoc_in-1)/dev->max_isoc_in;
788 /* Hack to allocate memory for Video + Audio */
789 /* FIXME: should also consider header ovehead of
791 urbsize+=((48000*4+24)/25+dev->max_isoc_in-1)/dev->max_isoc_in;
793 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d packets to handle "
794 "%lu size\n", urbsize,buf->vb.size);
795 rc = tm6000_prepare_isoc(dev, urbsize, 2);
801 buf->vb.state = STATE_PREPARED;
810 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
812 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
813 struct tm6000_fh *fh = vq->priv_data;
814 struct tm6000_core *dev = fh->dev;
815 struct tm6000_dmaqueue *vidq = &dev->vidq;
816 struct tm6000_buffer *prev;
818 if (!list_empty(&vidq->queued)) {
819 list_add_tail(&buf->vb.queue,&vidq->queued);
820 buf->vb.state = STATE_QUEUED;
821 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue - "
822 "append to queued\n", buf, buf->vb.i);
823 } else if (list_empty(&vidq->active)) {
824 list_add_tail(&buf->vb.queue,&vidq->active);
825 buf->vb.state = STATE_ACTIVE;
826 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
827 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue - "
828 "first active\n", buf, buf->vb.i);
829 tm6000_start_thread(vidq, buf);
831 prev = list_entry(vidq->active.prev, struct tm6000_buffer, vb.queue);
832 if (prev->vb.width == buf->vb.width &&
833 prev->vb.height == buf->vb.height &&
834 prev->fmt == buf->fmt) {
835 list_add_tail(&buf->vb.queue,&vidq->active);
836 buf->vb.state = STATE_ACTIVE;
837 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue -"
838 " append to active\n", buf, buf->vb.i);
840 list_add_tail(&buf->vb.queue,&vidq->queued);
841 buf->vb.state = STATE_QUEUED;
842 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue -"
843 " first queued\n", buf, buf->vb.i);
848 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
850 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
851 struct tm6000_fh *fh = vq->priv_data;
852 struct tm6000_core *dev = (struct tm6000_core*)fh->dev;
853 struct tm6000_dmaqueue *vidq = &dev->vidq;
855 tm6000_stop_thread(vidq);
860 static struct videobuf_queue_ops tm6000_video_qops = {
861 .buf_setup = buffer_setup,
862 .buf_prepare = buffer_prepare,
863 .buf_queue = buffer_queue,
864 .buf_release = buffer_release,
867 /* ------------------------------------------------------------------
869 ------------------------------------------------------------------*/
871 static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh)
874 mutex_lock(&dev->lock);
875 if (dev->resources) {
876 /* no, someone else uses it */
877 mutex_unlock(&dev->lock);
880 /* it's free, grab it */
882 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
883 mutex_unlock(&dev->lock);
887 static int res_locked(struct tm6000_core *dev)
889 return (dev->resources);
892 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
894 mutex_lock(&dev->lock);
896 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
897 mutex_unlock(&dev->lock);
900 /* ------------------------------------------------------------------
901 IOCTL vidioc handling
902 ------------------------------------------------------------------*/
903 static int vidioc_querycap (struct file *file, void *priv,
904 struct v4l2_capability *cap)
906 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
908 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
909 strlcpy(cap->card,"Trident TVMaster TM5600/6000", sizeof(cap->card));
910 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
911 cap->version = TM6000_VERSION;
912 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
919 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
920 struct v4l2_fmtdesc *f)
922 if (unlikely(f->index >= ARRAY_SIZE(format)))
925 strlcpy(f->description,format[f->index].name,sizeof(f->description));
926 f->pixelformat = format[f->index].fourcc;
930 static int vidioc_g_fmt_cap (struct file *file, void *priv,
931 struct v4l2_format *f)
933 struct tm6000_fh *fh=priv;
935 f->fmt.pix.width = fh->width;
936 f->fmt.pix.height = fh->height;
937 f->fmt.pix.field = fh->vb_vidq.field;
938 f->fmt.pix.pixelformat = fh->fmt->fourcc;
939 f->fmt.pix.bytesperline =
940 (f->fmt.pix.width * fh->fmt->depth) >> 3;
941 f->fmt.pix.sizeimage =
942 f->fmt.pix.height * f->fmt.pix.bytesperline;
947 static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc)
951 for (i = 0; i < ARRAY_SIZE(format); i++)
952 if (format[i].fourcc == fourcc)
957 static int vidioc_try_fmt_cap (struct file *file, void *priv,
958 struct v4l2_format *f)
960 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
961 struct tm6000_fmt *fmt;
962 enum v4l2_field field;
964 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
966 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
967 " invalid.\n", f->fmt.pix.pixelformat);
971 field = f->fmt.pix.field;
973 if (field == V4L2_FIELD_ANY) {
974 // field=V4L2_FIELD_INTERLACED;
975 field=V4L2_FIELD_SEQ_TB;
976 } else if (V4L2_FIELD_INTERLACED != field) {
977 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
981 if (f->fmt.pix.width < norm_minw(core))
982 f->fmt.pix.width = norm_minw(core);
984 if (f->fmt.pix.width > norm_maxw(core))
985 f->fmt.pix.width = norm_maxw(core);
987 if (f->fmt.pix.height < norm_minh(core))
988 f->fmt.pix.height = norm_minh(core);
990 if (f->fmt.pix.height > norm_maxh(core))
991 f->fmt.pix.height = norm_maxh(core);
993 f->fmt.pix.width &= ~0x01;
995 f->fmt.pix.field = field;
997 f->fmt.pix.bytesperline =
998 (f->fmt.pix.width * fmt->depth) >> 3;
999 f->fmt.pix.sizeimage =
1000 f->fmt.pix.height * f->fmt.pix.bytesperline;
1005 /*FIXME: This seems to be generic enough to be at videodev2 */
1006 static int vidioc_s_fmt_cap (struct file *file, void *priv,
1007 struct v4l2_format *f)
1009 struct tm6000_fh *fh=priv;
1010 struct tm6000_core *dev = fh->dev;
1011 int ret = vidioc_try_fmt_cap(file,fh,f);
1015 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1016 fh->width = f->fmt.pix.width;
1017 fh->height = f->fmt.pix.height;
1018 fh->vb_vidq.field = f->fmt.pix.field;
1021 dev->fourcc = f->fmt.pix.pixelformat;
1023 tm6000_set_fourcc_format(dev);
1028 static int vidioc_reqbufs (struct file *file, void *priv,
1029 struct v4l2_requestbuffers *p)
1031 struct tm6000_fh *fh=priv;
1033 return (videobuf_reqbufs(&fh->vb_vidq, p));
1036 static int vidioc_querybuf (struct file *file, void *priv,
1037 struct v4l2_buffer *p)
1039 struct tm6000_fh *fh=priv;
1041 return (videobuf_querybuf(&fh->vb_vidq, p));
1044 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1046 struct tm6000_fh *fh=priv;
1048 return (videobuf_qbuf(&fh->vb_vidq, p));
1051 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1053 struct tm6000_fh *fh=priv;
1055 return (videobuf_dqbuf(&fh->vb_vidq, p,
1056 file->f_flags & O_NONBLOCK));
1059 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1060 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1062 struct tm6000_fh *fh=priv;
1064 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1068 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1070 struct tm6000_fh *fh=priv;
1071 struct tm6000_core *dev = fh->dev;
1073 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1078 if (!res_get(dev,fh))
1080 return (videobuf_streamon(&fh->vb_vidq));
1083 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1085 struct tm6000_fh *fh=priv;
1086 struct tm6000_core *dev = fh->dev;
1088 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1093 videobuf_streamoff(&fh->vb_vidq);
1099 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1102 struct tm6000_fh *fh=priv;
1103 struct tm6000_core *dev = fh->dev;
1105 rc=tm6000_set_standard (dev, norm);
1109 tm6000_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
1114 static int vidioc_enum_input (struct file *file, void *priv,
1115 struct v4l2_input *inp)
1117 switch (inp->index) {
1118 case TM6000_INPUT_TV:
1119 inp->type = V4L2_INPUT_TYPE_TUNER;
1120 strcpy(inp->name,"Television");
1122 case TM6000_INPUT_COMPOSITE:
1123 inp->type = V4L2_INPUT_TYPE_CAMERA;
1124 strcpy(inp->name,"Composite");
1126 case TM6000_INPUT_SVIDEO:
1127 inp->type = V4L2_INPUT_TYPE_CAMERA;
1128 strcpy(inp->name,"S-Video");
1133 inp->std = TM6000_STD;
1138 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1140 struct tm6000_fh *fh=priv;
1141 struct tm6000_core *dev = fh->dev;
1147 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1149 struct tm6000_fh *fh=priv;
1150 struct tm6000_core *dev = fh->dev;
1155 case TM6000_INPUT_TV:
1159 case TM6000_INPUT_COMPOSITE:
1160 case TM6000_INPUT_SVIDEO:
1167 rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1168 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1172 rc=vidioc_s_std (file, priv, &dev->vfd->current_norm);
1178 /* --- controls ---------------------------------------------- */
1179 static int vidioc_queryctrl (struct file *file, void *priv,
1180 struct v4l2_queryctrl *qc)
1184 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1185 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1186 memcpy(qc, &(tm6000_qctrl[i]),
1194 static int vidioc_g_ctrl (struct file *file, void *priv,
1195 struct v4l2_control *ctrl)
1197 struct tm6000_fh *fh=priv;
1198 struct tm6000_core *dev = fh->dev;
1201 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1203 case V4L2_CID_CONTRAST:
1204 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x08, 0);
1206 case V4L2_CID_BRIGHTNESS:
1207 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x09, 0);
1209 case V4L2_CID_SATURATION:
1210 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, 0);
1213 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, 0);
1226 static int vidioc_s_ctrl (struct file *file, void *priv,
1227 struct v4l2_control *ctrl)
1229 struct tm6000_fh *fh =priv;
1230 struct tm6000_core *dev = fh->dev;
1234 case V4L2_CID_CONTRAST:
1235 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x08, val);
1237 case V4L2_CID_BRIGHTNESS:
1238 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x09, val);
1240 case V4L2_CID_SATURATION:
1241 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, val);
1244 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, val);
1250 static int vidioc_g_tuner (struct file *file, void *priv,
1251 struct v4l2_tuner *t)
1253 struct tm6000_fh *fh =priv;
1254 struct tm6000_core *dev = fh->dev;
1256 if (unlikely(UNSET == dev->tuner_type))
1261 strcpy(t->name, "Television");
1262 t->type = V4L2_TUNER_ANALOG_TV;
1263 t->capability = V4L2_TUNER_CAP_NORM;
1264 t->rangehigh = 0xffffffffUL;
1265 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1270 static int vidioc_s_tuner (struct file *file, void *priv,
1271 struct v4l2_tuner *t)
1273 struct tm6000_fh *fh =priv;
1274 struct tm6000_core *dev = fh->dev;
1276 if (UNSET == dev->tuner_type)
1284 static int vidioc_g_frequency (struct file *file, void *priv,
1285 struct v4l2_frequency *f)
1287 struct tm6000_fh *fh =priv;
1288 struct tm6000_core *dev = fh->dev;
1290 if (unlikely(UNSET == dev->tuner_type))
1293 f->type = V4L2_TUNER_ANALOG_TV;
1294 f->frequency = dev->freq;
1296 tm6000_i2c_call_clients(dev,VIDIOC_G_FREQUENCY,f);
1301 static int vidioc_s_frequency (struct file *file, void *priv,
1302 struct v4l2_frequency *f)
1304 struct tm6000_fh *fh =priv;
1305 struct tm6000_core *dev = fh->dev;
1307 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1310 if (unlikely(UNSET == dev->tuner_type))
1312 if (unlikely(f->tuner != 0))
1315 // mutex_lock(&dev->lock);
1316 dev->freq = f->frequency;
1317 tm6000_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1318 // mutex_unlock(&dev->lock);
1323 /* ------------------------------------------------------------------
1324 File operations for the device
1325 ------------------------------------------------------------------*/
1327 static int tm6000_open(struct inode *inode, struct file *file)
1329 int minor = iminor(inode);
1330 struct tm6000_core *h,*dev = NULL;
1331 struct tm6000_fh *fh;
1332 struct list_head *list;
1333 enum v4l2_buf_type type = 0;
1336 printk(KERN_INFO "tm6000: open called (minor=%d)\n",minor);
1339 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called "
1340 "(minor=%d)\n",minor);
1342 list_for_each(list,&tm6000_corelist) {
1343 h = list_entry(list, struct tm6000_core, tm6000_corelist);
1344 if (h->vfd->minor == minor) {
1346 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1353 /* If more than one user, mutex should be added */
1356 dprintk(dev, V4L2_DEBUG_OPEN, "open minor=%d type=%s users=%d\n",
1357 minor,v4l2_type_names[type],dev->users);
1359 /* allocate + initialize per filehandle data */
1360 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1366 file->private_data = fh;
1369 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1370 dev->fourcc = format[0].fourcc;
1372 fh->fmt = format_by_fourcc(dev->fourcc);
1373 fh->width = norm_maxw();
1374 fh->height = norm_maxh();
1376 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1377 "dev->vidq=0x%08lx\n",
1378 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1379 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1380 "queued=%d\n",list_empty(&dev->vidq.queued));
1381 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1382 "active=%d\n",list_empty(&dev->vidq.active));
1384 /* initialize hardware on analog mode */
1385 if (dev->mode!=TM6000_MODE_ANALOG) {
1386 rc=tm6000_init_analog_mode (dev);
1390 /* Put all controls at a sane state */
1391 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1392 qctl_regs[i] =tm6000_qctrl[i].default_value;
1394 dev->mode=TM6000_MODE_ANALOG;
1397 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1400 V4L2_FIELD_INTERLACED,
1401 sizeof(struct tm6000_buffer),fh);
1407 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1409 struct tm6000_fh *fh = file->private_data;
1411 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1412 if (res_locked(fh->dev))
1415 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1416 file->f_flags & O_NONBLOCK);
1422 tm6000_poll(struct file *file, struct poll_table_struct *wait)
1424 struct tm6000_fh *fh = file->private_data;
1425 struct tm6000_buffer *buf;
1427 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1430 if (res_get(fh->dev,fh)) {
1431 /* streaming capture */
1432 if (list_empty(&fh->vb_vidq.stream))
1434 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1436 /* read() capture */
1437 buf = (struct tm6000_buffer*)fh->vb_vidq.read_buf;
1441 poll_wait(file, &buf->vb.done, wait);
1442 if (buf->vb.state == STATE_DONE ||
1443 buf->vb.state == STATE_ERROR)
1444 return POLLIN|POLLRDNORM;
1448 static int tm6000_release(struct inode *inode, struct file *file)
1450 struct tm6000_fh *fh = file->private_data;
1451 struct tm6000_core *dev = fh->dev;
1452 struct tm6000_dmaqueue *vidq = &dev->vidq;
1453 int minor = iminor(inode);
1455 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (minor=%d, users=%d)\n",minor,dev->users);
1460 tm6000_stop_thread(vidq);
1461 videobuf_mmap_free(&fh->vb_vidq);
1469 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1471 struct tm6000_fh *fh = file->private_data;
1474 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1479 static struct file_operations tm6000_fops = {
1480 .owner = THIS_MODULE,
1481 .open = tm6000_open,
1482 .release = tm6000_release,
1483 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1484 .read = tm6000_read,
1485 .poll = tm6000_poll,
1486 .mmap = tm6000_mmap,
1487 .llseek = no_llseek,
1490 static struct video_device tm6000_template = {
1492 .type = VID_TYPE_CAPTURE,
1493 .fops = &tm6000_fops,
1495 .release = video_device_release,
1497 .vidioc_querycap = vidioc_querycap,
1498 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1499 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1500 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1501 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1502 .vidioc_s_std = vidioc_s_std,
1503 .vidioc_enum_input = vidioc_enum_input,
1504 .vidioc_g_input = vidioc_g_input,
1505 .vidioc_s_input = vidioc_s_input,
1506 .vidioc_queryctrl = vidioc_queryctrl,
1507 .vidioc_g_ctrl = vidioc_g_ctrl,
1508 .vidioc_s_ctrl = vidioc_s_ctrl,
1509 .vidioc_g_tuner = vidioc_g_tuner,
1510 .vidioc_s_tuner = vidioc_s_tuner,
1511 .vidioc_g_frequency = vidioc_g_frequency,
1512 .vidioc_s_frequency = vidioc_s_frequency,
1513 .vidioc_streamon = vidioc_streamon,
1514 .vidioc_streamoff = vidioc_streamoff,
1515 .vidioc_reqbufs = vidioc_reqbufs,
1516 .vidioc_querybuf = vidioc_querybuf,
1517 .vidioc_qbuf = vidioc_qbuf,
1518 .vidioc_dqbuf = vidioc_dqbuf,
1519 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1520 .vidiocgmbuf = vidiocgmbuf,
1522 .tvnorms = TM6000_STD,
1523 .current_norm = V4L2_STD_NTSC_M,
1525 /* -----------------------------------------------------------------
1526 Initialization and module stuff
1527 ------------------------------------------------------------------*/
1529 int tm6000_v4l2_register(struct tm6000_core *dev)
1532 struct video_device *vfd;
1534 vfd = video_device_alloc();
1540 list_add_tail(&dev->tm6000_corelist,&tm6000_corelist);
1542 /* init video dma queues */
1543 INIT_LIST_HEAD(&dev->vidq.active);
1544 INIT_LIST_HEAD(&dev->vidq.queued);
1546 dev->vidq.timeout.function = tm6000_vid_timeout;
1547 dev->vidq.timeout.data = (unsigned long)dev;
1548 init_timer(&dev->vidq.timeout);
1550 memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1551 dev->vfd->debug=tm6000_debug;
1553 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
1554 printk(KERN_INFO "Trident TVMaster TM5600/TM6000 USB2 board (Load status: %d)\n", ret);
1558 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1560 struct tm6000_core *h;
1561 struct list_head *pos, *tmp;
1563 video_unregister_device(dev->vfd);
1565 list_for_each_safe(pos, tmp, &tm6000_corelist) {
1566 h = list_entry(pos, struct tm6000_core, tm6000_corelist);
1575 int tm6000_v4l2_exit(void)
1580 module_param(video_nr, int, 0);
1581 MODULE_PARM_DESC(video_nr,"Allow changing video device number");
1583 module_param_named (debug, tm6000_debug, int, 0444);
1584 MODULE_PARM_DESC(debug,"activates debug info");
1586 module_param(vid_limit,int,0644);
1587 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");