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 #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 40 /* 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 */
60 /* supported controls */
61 static struct v4l2_queryctrl tm6000_qctrl[] = {
63 .id = V4L2_CID_BRIGHTNESS,
64 .type = V4L2_CTRL_TYPE_INTEGER,
72 .id = V4L2_CID_CONTRAST,
73 .type = V4L2_CTRL_TYPE_INTEGER,
81 .id = V4L2_CID_SATURATION,
82 .type = V4L2_CTRL_TYPE_INTEGER,
91 .type = V4L2_CTRL_TYPE_INTEGER,
101 static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
103 static struct tm6000_fmt format[] = {
105 .name = "4:2:2, packed, YVY2",
106 .fourcc = V4L2_PIX_FMT_YUYV,
109 .name = "4:2:2, packed, UYVY",
110 .fourcc = V4L2_PIX_FMT_UYVY,
113 .name = "A/V + VBI mux packet",
114 .fourcc = V4L2_PIX_FMT_TM6000,
119 static LIST_HEAD(tm6000_corelist);
121 /* ------------------------------------------------------------------
122 DMA and thread functions
123 ------------------------------------------------------------------*/
125 #define norm_maxw(a) 720
126 #define norm_maxh(a) 576
128 #define norm_minw(a) norm_maxw(a)
129 #define norm_minh(a) norm_maxh(a)
132 * video-buf generic routine to get the next available buffer
134 static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
135 struct tm6000_buffer **buf)
137 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
140 if (list_empty(&dma_q->active)) {
141 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
146 *buf = list_entry(dma_q->active.next,
147 struct tm6000_buffer, vb.queue);
152 /* Cleans up buffer - Usefull for testing for frame/URB loss */
153 outp = videobuf_to_vmalloc(&(*buf)->vb);
154 memset(outp, 0, (*buf)->vb.size);
160 * Announces that a buffer were filled and request the next
162 static inline void buffer_filled(struct tm6000_core *dev,
163 struct tm6000_dmaqueue *dma_q,
164 struct tm6000_buffer *buf)
166 /* Advice that buffer was filled */
167 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
168 buf->vb.state = VIDEOBUF_DONE;
169 buf->vb.field_count++;
170 do_gettimeofday(&buf->vb.ts);
172 list_del(&buf->vb.queue);
173 wake_up(&buf->vb.done);
176 const char *tm6000_msg_type[] = {
177 "unknown(0)", /* 0 */
183 "unknown(6)", /* 6 */
184 "unknown(7)", /* 7 */
188 * Identify the tm5600/6000 buffer header type and properly handles
190 static int copy_packet(struct urb *urb, u32 header, u8 **ptr, u8 *endp,
191 u8 *out_p, struct tm6000_buffer **buf)
193 struct tm6000_dmaqueue *dma_q = urb->context;
194 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
196 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
198 /* FIXME: move to tm6000-isoc */
199 static int last_line = -2, start_line = -2, last_field = -2;
201 /* FIXME: this is the hardcoded window size
203 unsigned int linewidth = (*buf)->vb.width << 1;
205 if (!dev->isoc_ctl.cmd) {
206 c = (header >> 24) & 0xff;
208 /* split the header fields */
209 size = (((header & 0x7e) << 1) -1) *4;
210 block = (header >> 7) & 0xf;
211 field = (header >> 11) & 0x1;
212 line = (header >> 12) & 0x1ff;
213 cmd = (header >> 21) & 0x7;
215 /* Validates header fields */
216 if(size > TM6000_URB_MSG_LEN)
217 size = TM6000_URB_MSG_LEN;
219 if (cmd == TM6000_URB_MSG_VIDEO) {
220 if ((block+1)*TM6000_URB_MSG_LEN>linewidth)
221 cmd = TM6000_URB_MSG_ERR;
223 /* FIXME: Mounts the image as field0+field1
224 * It should, instead, check if the user selected
225 * entrelaced or non-entrelaced mode
227 pos= ((line<<1)+field)*linewidth +
228 block*TM6000_URB_MSG_LEN;
230 /* Don't allow to write out of the buffer */
231 if (pos+TM6000_URB_MSG_LEN > (*buf)->vb.size) {
232 dprintk(dev, V4L2_DEBUG_ISOC,
233 "ERR: size=%d, num=%d, line=%d, "
235 size, block, line, field);
237 cmd = TM6000_URB_MSG_ERR;
243 /* Prints debug info */
244 dprintk(dev, V4L2_DEBUG_ISOC, "size=%d, num=%d, "
245 " line=%d, field=%d\n",
246 size, block, line, field);
248 if ((last_line!=line)&&(last_line+1!=line) &&
249 (cmd != TM6000_URB_MSG_ERR) ) {
250 if (cmd != TM6000_URB_MSG_VIDEO) {
251 dprintk(dev, V4L2_DEBUG_ISOC, "cmd=%d, "
252 "size=%d, num=%d, line=%d, field=%d\n",
253 cmd, size, block, line, field);
256 start_line=last_line;
257 /* Prints debug info */
258 dprintk(dev, V4L2_DEBUG_ISOC, "lines= %d-%d, "
260 start_line, last_line, field);
262 if ((start_line<6 && last_line>200) &&
263 (last_field != field) ) {
265 dev->isoc_ctl.nfields++;
266 if (dev->isoc_ctl.nfields>=2) {
267 dev->isoc_ctl.nfields=0;
269 /* Announces that a new buffer were filled */
270 buffer_filled (dev, dma_q, *buf);
271 dprintk(dev, V4L2_DEBUG_ISOC,
272 "new buffer filled\n");
273 get_next_buf (dma_q, buf);
276 out_p = videobuf_to_vmalloc(&((*buf)->vb));
280 pos = dev->isoc_ctl.pos = 0;
289 pktsize = TM6000_URB_MSG_LEN;
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;
298 cpysize = (endp-(*ptr) > size) ? size : endp - *ptr;
301 /* handles each different URB message */
303 case TM6000_URB_MSG_VIDEO:
304 /* Fills video buffer */
305 memcpy(&out_p[pos], *ptr, cpysize);
307 case TM6000_URB_MSG_PTS:
309 case TM6000_URB_MSG_AUDIO:
310 /* Need some code to process audio */
311 printk ("%ld: cmd=%s, size=%d\n", jiffies,
312 tm6000_msg_type[cmd],size);
315 dprintk (dev, V4L2_DEBUG_ISOC, "cmd=%s, size=%d\n",
316 tm6000_msg_type[cmd],size);
320 /* End of URB packet, but cmd processing is not
321 * complete. Preserve the state for a next packet
323 dev->isoc_ctl.pos = pos+cpysize;
324 dev->isoc_ctl.size= size-cpysize;
325 dev->isoc_ctl.cmd = cmd;
326 dev->isoc_ctl.pktsize = pktsize-cpysize;
329 dev->isoc_ctl.cmd = 0;
336 static int copy_streams(u8 *data, u8 *out_p, unsigned long len,
337 struct urb *urb, struct tm6000_buffer **buf)
339 struct tm6000_dmaqueue *dma_q = urb->context;
340 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
341 u8 *ptr=data, *endp=data+len;
342 unsigned long header=0;
345 for (ptr=data; ptr<endp;) {
346 if (!dev->isoc_ctl.cmd) {
347 u8 *p=(u8 *)&dev->isoc_ctl.tmp_buf;
348 /* FIXME: This seems very complex
349 * It just recovers up to 3 bytes of the header that
350 * might be at the previous packet
352 if (dev->isoc_ctl.tmp_buf_len) {
353 while (dev->isoc_ctl.tmp_buf_len) {
354 if ( *(ptr+3-dev->isoc_ctl.tmp_buf_len) == 0x47) {
358 dev->isoc_ctl.tmp_buf_len--;
360 if (dev->isoc_ctl.tmp_buf_len) {
362 dev->isoc_ctl.tmp_buf_len);
363 memcpy (((u8 *)header)+
364 dev->isoc_ctl.tmp_buf,
366 4-dev->isoc_ctl.tmp_buf_len);
367 ptr+=4-dev->isoc_ctl.tmp_buf_len;
372 for (;ptr<endp-3;ptr++) {
378 dev->isoc_ctl.tmp_buf_len=endp-ptr;
379 memcpy (&dev->isoc_ctl.tmp_buf,ptr,
380 dev->isoc_ctl.tmp_buf_len);
385 /* Get message header */
386 header=*(unsigned long *)ptr;
390 /* Copy or continue last copy */
391 rc=copy_packet(urb,header,&ptr,endp,out_p,buf);
394 printk(KERN_ERR "tm6000: buffer underrun at %ld\n",
403 * Identify the tm5600/6000 buffer header type and properly handles
405 static int copy_multiplexed(u8 *ptr, u8 *out_p, unsigned long len,
406 struct urb *urb, struct tm6000_buffer **buf)
408 struct tm6000_dmaqueue *dma_q = urb->context;
409 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
410 unsigned int pos=dev->isoc_ctl.pos,cpysize;
414 cpysize=min(len,(*buf)->vb.size-pos);
415 //printk("Copying %d bytes (max=%lu) from %p to %p[%u]\n",cpysize,(*buf)->vb.size,ptr,out_p,pos);
416 memcpy(&out_p[pos], ptr, cpysize);
420 if (pos >= (*buf)->vb.size) {
422 /* Announces that a new buffer were filled */
423 buffer_filled (dev, dma_q, *buf);
424 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
425 get_next_buf (dma_q, buf);
428 out_p = videobuf_to_vmalloc(&((*buf)->vb));
435 dev->isoc_ctl.pos=pos;
439 static void inline print_err_status (struct tm6000_core *dev,
440 int packet, int status)
442 char *errmsg = "Unknown";
446 errmsg = "unlinked synchronuously";
449 errmsg = "unlinked asynchronuously";
452 errmsg = "Buffer error (overrun)";
455 errmsg = "Stalled (device not responding)";
458 errmsg = "Babble (bad cable?)";
461 errmsg = "Bit-stuff error (bad cable?)";
464 errmsg = "CRC/Timeout (could be anything)";
467 errmsg = "Device does not respond";
471 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
474 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
475 packet, status, errmsg);
481 * Controls the isoc copy of each urb packet
483 static inline int tm6000_isoc_copy(struct urb *urb)
485 struct tm6000_dmaqueue *dma_q = urb->context;
486 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
487 struct tm6000_buffer *buf;
490 char *outp = NULL, *p;
491 unsigned long copied;
493 get_next_buf(dma_q, &buf);
495 outp = videobuf_to_vmalloc(&buf->vb);
505 print_err_status (dev,-1,urb->status);
509 for (i = 0; i < urb->number_of_packets; i++) {
510 int status = urb->iso_frame_desc[i].status;
513 print_err_status (dev,i,status);
517 len=urb->iso_frame_desc[i].actual_length;
519 // if (len>=TM6000_URB_MSG_LEN) {
520 p=urb->transfer_buffer + urb->iso_frame_desc[i].offset;
521 if (!urb->iso_frame_desc[i].status) {
522 if ((buf->fmt->fourcc)==V4L2_PIX_FMT_TM6000) {
523 rc=copy_multiplexed(p, outp, len, urb, &buf);
527 copy_streams(p, outp, len, urb, &buf);
538 /* ------------------------------------------------------------------
540 ------------------------------------------------------------------*/
543 * IRQ callback, called by URB callback
545 static void tm6000_irq_callback(struct urb *urb)
547 struct tm6000_dmaqueue *dma_q = urb->context;
548 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
554 spin_lock(&dev->slock);
555 tm6000_isoc_copy(urb);
556 spin_unlock(&dev->slock);
558 /* Reset urb buffers */
559 for (i = 0; i < urb->number_of_packets; i++) {
560 urb->iso_frame_desc[i].status = 0;
561 urb->iso_frame_desc[i].actual_length = 0;
564 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
566 tm6000_err("urb resubmit failed (error=%i)\n",
571 * Stop and Deallocate URBs
573 static void tm6000_uninit_isoc(struct tm6000_core *dev)
578 dev->isoc_ctl.nfields = -1;
579 dev->isoc_ctl.buf = NULL;
580 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
581 urb=dev->isoc_ctl.urb[i];
585 if (dev->isoc_ctl.transfer_buffer[i]) {
586 usb_buffer_free(dev->udev,
587 urb->transfer_buffer_length,
588 dev->isoc_ctl.transfer_buffer[i],
592 dev->isoc_ctl.urb[i] = NULL;
594 dev->isoc_ctl.transfer_buffer[i] = NULL;
597 kfree (dev->isoc_ctl.urb);
598 kfree (dev->isoc_ctl.transfer_buffer);
600 dev->isoc_ctl.urb=NULL;
601 dev->isoc_ctl.transfer_buffer=NULL;
602 dev->isoc_ctl.num_bufs = 0;
604 dev->isoc_ctl.num_bufs=0;
608 * Allocate URBs and start IRQ
610 static int tm6000_prepare_isoc(struct tm6000_core *dev, unsigned int framesize)
612 struct tm6000_dmaqueue *dma_q = &dev->vidq;
613 int i, j, sb_size, pipe, size, max_packets, num_bufs = 5;
616 /* De-allocates all pending stuff */
617 tm6000_uninit_isoc(dev);
619 pipe = usb_rcvisocpipe(dev->udev,
620 dev->isoc_in->desc.bEndpointAddress &
621 USB_ENDPOINT_NUMBER_MASK);
623 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
625 if (size > dev->max_isoc_in)
626 size = dev->max_isoc_in;
628 dev->isoc_ctl.max_pkt_size = size;
630 max_packets = ( framesize + size - 1) / size;
632 if (max_packets > TM6000_MAX_ISO_PACKETS)
633 max_packets = TM6000_MAX_ISO_PACKETS;
635 sb_size = max_packets * size;
637 dev->isoc_ctl.num_bufs = num_bufs;
639 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
640 if (!dev->isoc_ctl.urb) {
641 tm6000_err("cannot alloc memory for usb buffers\n");
645 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
647 if (!dev->isoc_ctl.urb) {
648 tm6000_err("cannot allocate memory for usbtransfer\n");
649 kfree(dev->isoc_ctl.urb);
653 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
654 " (%d bytes) of %d bytes each to handle %u size\n",
655 max_packets, num_bufs, sb_size,
656 dev->max_isoc_in, size);
659 /* allocate urbs and transfer buffers */
660 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
661 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
663 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
664 tm6000_uninit_isoc(dev);
668 dev->isoc_ctl.urb[i] = urb;
670 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
671 sb_size, GFP_KERNEL, &urb->transfer_dma);
672 if (!dev->isoc_ctl.transfer_buffer[i]) {
673 tm6000_err ("unable to allocate %i bytes for transfer"
676 in_interrupt()?" while in int":"");
677 tm6000_uninit_isoc(dev);
680 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
682 usb_fill_bulk_urb(urb, dev->udev, pipe,
683 dev->isoc_ctl.transfer_buffer[i], sb_size,
684 tm6000_irq_callback, dma_q);
685 urb->interval = dev->isoc_in->desc.bInterval;
686 urb->number_of_packets = max_packets;
687 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
689 for (j = 0; j < max_packets; j++) {
690 urb->iso_frame_desc[j].offset = size * j;
691 urb->iso_frame_desc[j].length = size;
698 static int tm6000_start_thread( struct tm6000_core *dev)
700 struct tm6000_dmaqueue *dma_q = &dev->vidq;
704 dma_q->ini_jiffies=jiffies;
706 init_waitqueue_head(&dma_q->wq);
708 /* submit urbs and enables IRQ */
709 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
710 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
712 tm6000_err("submit of urb %i failed (error=%i)\n", i,
714 tm6000_uninit_isoc(dev);
722 /* ------------------------------------------------------------------
724 ------------------------------------------------------------------*/
727 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
729 struct tm6000_fh *fh = vq->priv_data;
731 *size = fh->fmt->depth * fh->width * fh->height >> 3;
733 *count = TM6000_DEF_BUF;
735 if (*count < TM6000_MIN_BUF) {
736 *count=TM6000_MIN_BUF;
739 while (*size * *count > vid_limit * 1024 * 1024)
745 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
747 struct tm6000_fh *fh = vq->priv_data;
748 struct tm6000_core *dev = fh->dev;
754 /* We used to wait for the buffer to finish here, but this didn't work
755 because, as we were keeping the state as VIDEOBUF_QUEUED,
756 videobuf_queue_cancel marked it as finished for us.
757 (Also, it could wedge forever if the hardware was misconfigured.)
759 This should be safe; by the time we get here, the buffer isn't
760 queued anymore. If we ever start marking the buffers as
761 VIDEOBUF_ACTIVE, it won't be, though.
763 spin_lock_irqsave(&dev->slock, flags);
764 if (dev->isoc_ctl.buf == buf)
765 dev->isoc_ctl.buf = NULL;
766 spin_unlock_irqrestore(&dev->slock, flags);
768 videobuf_vmalloc_free(&buf->vb);
769 buf->vb.state = VIDEOBUF_NEEDS_INIT;
773 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
774 enum v4l2_field field)
776 struct tm6000_fh *fh = vq->priv_data;
777 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
778 struct tm6000_core *dev = fh->dev;
779 int rc = 0, urb_init = 0;
781 BUG_ON(NULL == fh->fmt);
784 /* FIXME: It assumes depth=2 */
785 /* The only currently supported format is 16 bits/pixel */
786 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
787 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
790 if (buf->fmt != fh->fmt ||
791 buf->vb.width != fh->width ||
792 buf->vb.height != fh->height ||
793 buf->vb.field != field) {
795 buf->vb.width = fh->width;
796 buf->vb.height = fh->height;
797 buf->vb.field = field;
798 buf->vb.state = VIDEOBUF_NEEDS_INIT;
801 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
802 if (0 != (rc = videobuf_iolock(vq, &buf->vb, NULL)))
807 if (!dev->isoc_ctl.num_bufs)
811 rc = tm6000_prepare_isoc(dev, buf->vb.size);
815 rc = tm6000_start_thread(dev);
821 buf->vb.state = VIDEOBUF_PREPARED;
825 free_buffer(vq, buf);
830 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
832 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
833 struct tm6000_fh *fh = vq->priv_data;
834 struct tm6000_core *dev = fh->dev;
835 struct tm6000_dmaqueue *vidq = &dev->vidq;
837 buf->vb.state = VIDEOBUF_QUEUED;
838 list_add_tail(&buf->vb.queue, &vidq->active);
841 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
843 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
848 static struct videobuf_queue_ops tm6000_video_qops = {
849 .buf_setup = buffer_setup,
850 .buf_prepare = buffer_prepare,
851 .buf_queue = buffer_queue,
852 .buf_release = buffer_release,
855 /* ------------------------------------------------------------------
857 ------------------------------------------------------------------*/
859 static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh)
862 mutex_lock(&dev->lock);
863 if (dev->resources) {
864 /* no, someone else uses it */
865 mutex_unlock(&dev->lock);
868 /* it's free, grab it */
870 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
871 mutex_unlock(&dev->lock);
875 static int res_locked(struct tm6000_core *dev)
877 return (dev->resources);
880 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
882 mutex_lock(&dev->lock);
884 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
885 mutex_unlock(&dev->lock);
888 /* ------------------------------------------------------------------
889 IOCTL vidioc handling
890 ------------------------------------------------------------------*/
891 static int vidioc_querycap (struct file *file, void *priv,
892 struct v4l2_capability *cap)
894 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
896 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
897 strlcpy(cap->card,"Trident TVMaster TM5600/6000", sizeof(cap->card));
898 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
899 cap->version = TM6000_VERSION;
900 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
907 static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
908 struct v4l2_fmtdesc *f)
910 if (unlikely(f->index >= ARRAY_SIZE(format)))
913 strlcpy(f->description,format[f->index].name,sizeof(f->description));
914 f->pixelformat = format[f->index].fourcc;
918 static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
919 struct v4l2_format *f)
921 struct tm6000_fh *fh=priv;
923 f->fmt.pix.width = fh->width;
924 f->fmt.pix.height = fh->height;
925 f->fmt.pix.field = fh->vb_vidq.field;
926 f->fmt.pix.pixelformat = fh->fmt->fourcc;
927 f->fmt.pix.bytesperline =
928 (f->fmt.pix.width * fh->fmt->depth) >> 3;
929 f->fmt.pix.sizeimage =
930 f->fmt.pix.height * f->fmt.pix.bytesperline;
935 static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc)
939 for (i = 0; i < ARRAY_SIZE(format); i++)
940 if (format[i].fourcc == fourcc)
945 static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
946 struct v4l2_format *f)
948 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
949 struct tm6000_fmt *fmt;
950 enum v4l2_field field;
952 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
954 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
955 " invalid.\n", f->fmt.pix.pixelformat);
959 field = f->fmt.pix.field;
961 if (field == V4L2_FIELD_ANY) {
962 // field=V4L2_FIELD_INTERLACED;
963 field=V4L2_FIELD_SEQ_TB;
964 } else if (V4L2_FIELD_INTERLACED != field) {
965 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
969 tm6000_get_std_res (dev);
971 f->fmt.pix.width = dev->width;
972 f->fmt.pix.height = dev->height;
974 f->fmt.pix.width &= ~0x01;
976 f->fmt.pix.field = field;
978 f->fmt.pix.bytesperline =
979 (f->fmt.pix.width * fmt->depth) >> 3;
980 f->fmt.pix.sizeimage =
981 f->fmt.pix.height * f->fmt.pix.bytesperline;
986 /*FIXME: This seems to be generic enough to be at videodev2 */
987 static int vidioc_s_fmt_vid_cap (struct file *file, void *priv,
988 struct v4l2_format *f)
990 struct tm6000_fh *fh=priv;
991 struct tm6000_core *dev = fh->dev;
992 int ret = vidioc_try_fmt_vid_cap(file,fh,f);
996 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
997 fh->width = f->fmt.pix.width;
998 fh->height = f->fmt.pix.height;
999 fh->vb_vidq.field = f->fmt.pix.field;
1002 dev->fourcc = f->fmt.pix.pixelformat;
1004 tm6000_set_fourcc_format(dev);
1009 static int vidioc_reqbufs (struct file *file, void *priv,
1010 struct v4l2_requestbuffers *p)
1012 struct tm6000_fh *fh=priv;
1014 return (videobuf_reqbufs(&fh->vb_vidq, p));
1017 static int vidioc_querybuf (struct file *file, void *priv,
1018 struct v4l2_buffer *p)
1020 struct tm6000_fh *fh=priv;
1022 return (videobuf_querybuf(&fh->vb_vidq, p));
1025 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1027 struct tm6000_fh *fh=priv;
1029 return (videobuf_qbuf(&fh->vb_vidq, p));
1032 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1034 struct tm6000_fh *fh=priv;
1036 return (videobuf_dqbuf(&fh->vb_vidq, p,
1037 file->f_flags & O_NONBLOCK));
1040 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1041 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1043 struct tm6000_fh *fh=priv;
1045 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1049 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1051 struct tm6000_fh *fh=priv;
1052 struct tm6000_core *dev = fh->dev;
1054 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1059 if (!res_get(dev,fh))
1061 return (videobuf_streamon(&fh->vb_vidq));
1064 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1066 struct tm6000_fh *fh=priv;
1067 struct tm6000_core *dev = fh->dev;
1069 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1074 videobuf_streamoff(&fh->vb_vidq);
1080 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1083 struct tm6000_fh *fh=priv;
1084 struct tm6000_core *dev = fh->dev;
1086 rc=tm6000_set_standard (dev, norm);
1088 fh->width = dev->width;
1089 fh->height = dev->height;
1094 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1099 static int vidioc_enum_input (struct file *file, void *priv,
1100 struct v4l2_input *inp)
1102 switch (inp->index) {
1103 case TM6000_INPUT_TV:
1104 inp->type = V4L2_INPUT_TYPE_TUNER;
1105 strcpy(inp->name,"Television");
1107 case TM6000_INPUT_COMPOSITE:
1108 inp->type = V4L2_INPUT_TYPE_CAMERA;
1109 strcpy(inp->name,"Composite");
1111 case TM6000_INPUT_SVIDEO:
1112 inp->type = V4L2_INPUT_TYPE_CAMERA;
1113 strcpy(inp->name,"S-Video");
1118 inp->std = TM6000_STD;
1123 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1125 struct tm6000_fh *fh=priv;
1126 struct tm6000_core *dev = fh->dev;
1132 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1134 struct tm6000_fh *fh=priv;
1135 struct tm6000_core *dev = fh->dev;
1140 case TM6000_INPUT_TV:
1144 case TM6000_INPUT_COMPOSITE:
1145 case TM6000_INPUT_SVIDEO:
1152 rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1153 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1157 rc=vidioc_s_std (file, priv, &dev->vfd->current_norm);
1163 /* --- controls ---------------------------------------------- */
1164 static int vidioc_queryctrl (struct file *file, void *priv,
1165 struct v4l2_queryctrl *qc)
1169 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1170 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1171 memcpy(qc, &(tm6000_qctrl[i]),
1179 static int vidioc_g_ctrl (struct file *file, void *priv,
1180 struct v4l2_control *ctrl)
1182 struct tm6000_fh *fh=priv;
1183 struct tm6000_core *dev = fh->dev;
1186 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1188 case V4L2_CID_CONTRAST:
1189 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x08, 0);
1191 case V4L2_CID_BRIGHTNESS:
1192 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x09, 0);
1194 case V4L2_CID_SATURATION:
1195 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, 0);
1198 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, 0);
1211 static int vidioc_s_ctrl (struct file *file, void *priv,
1212 struct v4l2_control *ctrl)
1214 struct tm6000_fh *fh =priv;
1215 struct tm6000_core *dev = fh->dev;
1219 case V4L2_CID_CONTRAST:
1220 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x08, val);
1222 case V4L2_CID_BRIGHTNESS:
1223 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x09, val);
1225 case V4L2_CID_SATURATION:
1226 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, val);
1229 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, val);
1235 static int vidioc_g_tuner (struct file *file, void *priv,
1236 struct v4l2_tuner *t)
1238 struct tm6000_fh *fh =priv;
1239 struct tm6000_core *dev = fh->dev;
1241 if (unlikely(UNSET == dev->tuner_type))
1246 strcpy(t->name, "Television");
1247 t->type = V4L2_TUNER_ANALOG_TV;
1248 t->capability = V4L2_TUNER_CAP_NORM;
1249 t->rangehigh = 0xffffffffUL;
1250 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1255 static int vidioc_s_tuner (struct file *file, void *priv,
1256 struct v4l2_tuner *t)
1258 struct tm6000_fh *fh =priv;
1259 struct tm6000_core *dev = fh->dev;
1261 if (UNSET == dev->tuner_type)
1269 static int vidioc_g_frequency (struct file *file, void *priv,
1270 struct v4l2_frequency *f)
1272 struct tm6000_fh *fh =priv;
1273 struct tm6000_core *dev = fh->dev;
1275 if (unlikely(UNSET == dev->tuner_type))
1278 f->type = V4L2_TUNER_ANALOG_TV;
1279 f->frequency = dev->freq;
1281 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1286 static int vidioc_s_frequency (struct file *file, void *priv,
1287 struct v4l2_frequency *f)
1289 struct tm6000_fh *fh =priv;
1290 struct tm6000_core *dev = fh->dev;
1292 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1295 if (unlikely(UNSET == dev->tuner_type))
1297 if (unlikely(f->tuner != 0))
1300 // mutex_lock(&dev->lock);
1301 dev->freq = f->frequency;
1302 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1303 // mutex_unlock(&dev->lock);
1308 /* ------------------------------------------------------------------
1309 File operations for the device
1310 ------------------------------------------------------------------*/
1312 static int tm6000_open(struct file *file)
1314 struct video_device *vdev = video_devdata(file);
1315 struct tm6000_core *dev = video_drvdata(file);
1316 struct tm6000_fh *fh;
1317 struct list_head *list;
1318 enum v4l2_buf_type type = 0;
1321 printk(KERN_INFO "tm6000: open called (dev=%s)\n",
1322 video_device_node_name(vdev));
1324 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1325 video_device_node_name(vdev));
1327 list_for_each(list,&tm6000_corelist) {
1328 h = list_entry(list, struct tm6000_core, tm6000_corelist);
1329 if (h->vfd->minor == minor) {
1331 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1338 /* If more than one user, mutex should be added */
1341 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1342 video_device_node_name(vdev), v4l2_type_names[type],
1345 /* allocate + initialize per filehandle data */
1346 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1352 file->private_data = fh;
1355 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1356 dev->fourcc = format[0].fourcc;
1358 fh->fmt = format_by_fourcc(dev->fourcc);
1360 tm6000_get_std_res (dev);
1362 fh->width = dev->width;
1363 fh->height = dev->height;
1365 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1366 "dev->vidq=0x%08lx\n",
1367 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1368 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1369 "queued=%d\n",list_empty(&dev->vidq.queued));
1370 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1371 "active=%d\n",list_empty(&dev->vidq.active));
1373 /* initialize hardware on analog mode */
1374 if (dev->mode!=TM6000_MODE_ANALOG) {
1375 rc=tm6000_init_analog_mode (dev);
1379 /* Put all controls at a sane state */
1380 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1381 qctl_regs[i] =tm6000_qctrl[i].default_value;
1383 dev->mode=TM6000_MODE_ANALOG;
1386 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1389 V4L2_FIELD_INTERLACED,
1390 sizeof(struct tm6000_buffer),fh);
1396 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1398 struct tm6000_fh *fh = file->private_data;
1400 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1401 if (res_locked(fh->dev))
1404 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1405 file->f_flags & O_NONBLOCK);
1411 tm6000_poll(struct file *file, struct poll_table_struct *wait)
1413 struct tm6000_fh *fh = file->private_data;
1414 struct tm6000_buffer *buf;
1416 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1419 if (res_get(fh->dev,fh)) {
1420 /* streaming capture */
1421 if (list_empty(&fh->vb_vidq.stream))
1423 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1425 /* read() capture */
1426 return videobuf_poll_stream(file, &fh->vb_vidq,
1429 poll_wait(file, &buf->vb.done, wait);
1430 if (buf->vb.state == VIDEOBUF_DONE ||
1431 buf->vb.state == VIDEOBUF_ERROR)
1432 return POLLIN|POLLRDNORM;
1436 static int tm6000_release(struct file *file)
1438 struct tm6000_fh *fh = file->private_data;
1439 struct tm6000_core *dev = fh->dev;
1440 struct video_device *vdev = video_devdata(file);
1442 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1443 video_device_node_name(vdev), dev->users);
1448 tm6000_uninit_isoc(dev);
1449 videobuf_mmap_free(&fh->vb_vidq);
1457 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1459 struct tm6000_fh *fh = file->private_data;
1462 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1467 static struct v4l2_file_operations tm6000_fops = {
1468 .owner = THIS_MODULE,
1469 .open = tm6000_open,
1470 .release = tm6000_release,
1471 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1472 .read = tm6000_read,
1473 .poll = tm6000_poll,
1474 .mmap = tm6000_mmap,
1477 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1478 .vidioc_querycap = vidioc_querycap,
1479 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1480 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1481 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1482 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1483 .vidioc_s_std = vidioc_s_std,
1484 .vidioc_enum_input = vidioc_enum_input,
1485 .vidioc_g_input = vidioc_g_input,
1486 .vidioc_s_input = vidioc_s_input,
1487 .vidioc_queryctrl = vidioc_queryctrl,
1488 .vidioc_g_ctrl = vidioc_g_ctrl,
1489 .vidioc_s_ctrl = vidioc_s_ctrl,
1490 .vidioc_g_tuner = vidioc_g_tuner,
1491 .vidioc_s_tuner = vidioc_s_tuner,
1492 .vidioc_g_frequency = vidioc_g_frequency,
1493 .vidioc_s_frequency = vidioc_s_frequency,
1494 .vidioc_streamon = vidioc_streamon,
1495 .vidioc_streamoff = vidioc_streamoff,
1496 .vidioc_reqbufs = vidioc_reqbufs,
1497 .vidioc_querybuf = vidioc_querybuf,
1498 .vidioc_qbuf = vidioc_qbuf,
1499 .vidioc_dqbuf = vidioc_dqbuf,
1500 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1501 .vidiocgmbuf = vidiocgmbuf,
1505 static struct video_device tm6000_template = {
1507 .fops = &tm6000_fops,
1508 .ioctl_ops = &video_ioctl_ops,
1510 .release = video_device_release,
1511 .tvnorms = TM6000_STD,
1512 .current_norm = V4L2_STD_NTSC_M,
1515 /* -----------------------------------------------------------------
1516 Initialization and module stuff
1517 ------------------------------------------------------------------*/
1519 int tm6000_v4l2_register(struct tm6000_core *dev)
1522 struct video_device *vfd;
1524 vfd = video_device_alloc();
1530 list_add_tail(&dev->tm6000_corelist,&tm6000_corelist);
1532 /* init video dma queues */
1533 INIT_LIST_HEAD(&dev->vidq.active);
1534 INIT_LIST_HEAD(&dev->vidq.queued);
1536 memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1537 dev->vfd->debug=tm6000_debug;
1538 vfd->v4l2_dev = &dev->v4l2_dev;
1540 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
1541 printk(KERN_INFO "Trident TVMaster TM5600/TM6000 USB2 board (Load status: %d)\n", ret);
1545 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1547 struct tm6000_core *h;
1548 struct list_head *pos, *tmp;
1550 video_unregister_device(dev->vfd);
1552 list_for_each_safe(pos, tmp, &tm6000_corelist) {
1553 h = list_entry(pos, struct tm6000_core, tm6000_corelist);
1562 int tm6000_v4l2_exit(void)
1567 module_param(video_nr, int, 0);
1568 MODULE_PARM_DESC(video_nr,"Allow changing video device number");
1570 module_param_named (debug, tm6000_debug, int, 0444);
1571 MODULE_PARM_DESC(debug,"activates debug info");
1573 module_param(vid_limit,int,0644);
1574 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");