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 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 /* ------------------------------------------------------------------
120 DMA and thread functions
121 ------------------------------------------------------------------*/
123 #define norm_maxw(a) 720
124 #define norm_maxh(a) 576
126 #define norm_minw(a) norm_maxw(a)
127 #define norm_minh(a) norm_maxh(a)
130 * video-buf generic routine to get the next available buffer
132 static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
133 struct tm6000_buffer **buf)
135 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
138 if (list_empty(&dma_q->active)) {
139 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
144 *buf = list_entry(dma_q->active.next,
145 struct tm6000_buffer, vb.queue);
150 /* Cleans up buffer - Usefull for testing for frame/URB loss */
151 outp = videobuf_to_vmalloc(&(*buf)->vb);
153 // memset(outp, 0, (*buf)->vb.size);
159 * Announces that a buffer were filled and request the next
161 static inline void buffer_filled(struct tm6000_core *dev,
162 struct tm6000_dmaqueue *dma_q,
163 struct tm6000_buffer *buf)
165 /* Advice that buffer was filled */
166 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
167 buf->vb.state = VIDEOBUF_DONE;
168 buf->vb.field_count++;
169 do_gettimeofday(&buf->vb.ts);
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
175 const char *tm6000_msg_type[] = {
176 "unknown(0)", /* 0 */
182 "unknown(6)", /* 6 */
183 "unknown(7)", /* 7 */
187 * Identify the tm5600/6000 buffer header type and properly handles
189 static int copy_packet(struct urb *urb, u32 header, u8 **ptr, u8 *endp,
190 u8 *out_p, struct tm6000_buffer **buf)
192 struct tm6000_dmaqueue *dma_q = urb->context;
193 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
195 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
197 /* FIXME: move to tm6000-isoc */
198 static int last_line = -2, start_line = -2, last_field = -2;
200 /* FIXME: this is the hardcoded window size
202 unsigned int linewidth = (*buf)->vb.width << 1;
204 if (!dev->isoc_ctl.cmd) {
205 c = (header >> 24) & 0xff;
207 /* split the header fields */
208 size = (((header & 0x7e) << 1) -1) *4;
209 block = (header >> 7) & 0xf;
210 field = (header >> 11) & 0x1;
211 line = (header >> 12) & 0x1ff;
212 cmd = (header >> 21) & 0x7;
214 /* Validates header fields */
215 if(size > TM6000_URB_MSG_LEN)
216 size = TM6000_URB_MSG_LEN;
218 if (cmd == TM6000_URB_MSG_VIDEO) {
219 if ((block+1)*TM6000_URB_MSG_LEN>linewidth)
220 cmd = TM6000_URB_MSG_ERR;
222 /* FIXME: Mounts the image as field0+field1
223 * It should, instead, check if the user selected
224 * entrelaced or non-entrelaced mode
226 pos = ((line << 1) - field - 1) * linewidth +
227 block * TM6000_URB_MSG_LEN;
229 /* Don't allow to write out of the buffer */
230 if (pos+TM6000_URB_MSG_LEN > (*buf)->vb.size) {
231 dprintk(dev, V4L2_DEBUG_ISOC,
232 "ERR: size=%d, num=%d, line=%d, "
234 size, block, line, field);
236 cmd = TM6000_URB_MSG_ERR;
242 /* Prints debug info */
243 dprintk(dev, V4L2_DEBUG_ISOC, "size=%d, num=%d, "
244 " line=%d, field=%d\n",
245 size, block, line, field);
247 if ((last_line!=line)&&(last_line+1!=line) &&
248 (cmd != TM6000_URB_MSG_ERR) ) {
249 if (cmd != TM6000_URB_MSG_VIDEO) {
250 dprintk(dev, V4L2_DEBUG_ISOC, "cmd=%d, "
251 "size=%d, num=%d, line=%d, field=%d\n",
252 cmd, size, block, line, field);
255 start_line=last_line;
256 /* Prints debug info */
257 dprintk(dev, V4L2_DEBUG_ISOC, "lines= %d-%d, "
259 start_line, last_line, field);
261 if ((start_line<6 && last_line>200) &&
262 (last_field != field) ) {
264 dev->isoc_ctl.nfields++;
265 if (dev->isoc_ctl.nfields>=2) {
266 dev->isoc_ctl.nfields=0;
268 /* Announces that a new buffer were filled */
269 buffer_filled (dev, dma_q, *buf);
270 dprintk(dev, V4L2_DEBUG_ISOC,
271 "new buffer filled\n");
272 get_next_buf (dma_q, buf);
275 out_p = videobuf_to_vmalloc(&((*buf)->vb));
279 pos = dev->isoc_ctl.pos = 0;
286 if (cmd == TM6000_URB_MSG_VIDEO)
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_len,
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",
405 * Identify the tm5600/6000 buffer header type and properly handles
407 static int copy_multiplexed(u8 *ptr, u8 *out_p, unsigned long len,
408 struct urb *urb, struct tm6000_buffer **buf)
410 struct tm6000_dmaqueue *dma_q = urb->context;
411 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
412 unsigned int pos=dev->isoc_ctl.pos,cpysize;
416 cpysize=min(len,(*buf)->vb.size-pos);
417 //printk("Copying %d bytes (max=%lu) from %p to %p[%u]\n",cpysize,(*buf)->vb.size,ptr,out_p,pos);
418 memcpy(&out_p[pos], ptr, cpysize);
422 if (pos >= (*buf)->vb.size) {
424 /* Announces that a new buffer were filled */
425 buffer_filled (dev, dma_q, *buf);
426 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
427 get_next_buf (dma_q, buf);
430 out_p = videobuf_to_vmalloc(&((*buf)->vb));
437 dev->isoc_ctl.pos=pos;
441 static void inline print_err_status (struct tm6000_core *dev,
442 int packet, int status)
444 char *errmsg = "Unknown";
448 errmsg = "unlinked synchronuously";
451 errmsg = "unlinked asynchronuously";
454 errmsg = "Buffer error (overrun)";
457 errmsg = "Stalled (device not responding)";
460 errmsg = "Babble (bad cable?)";
463 errmsg = "Bit-stuff error (bad cable?)";
466 errmsg = "CRC/Timeout (could be anything)";
469 errmsg = "Device does not respond";
473 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
476 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
477 packet, status, errmsg);
483 * Controls the isoc copy of each urb packet
485 static inline int tm6000_isoc_copy(struct urb *urb)
487 struct tm6000_dmaqueue *dma_q = urb->context;
488 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
489 struct tm6000_buffer *buf;
492 char *outp = NULL, *p;
493 unsigned long copied;
495 get_next_buf(dma_q, &buf);
497 outp = videobuf_to_vmalloc(&buf->vb);
507 print_err_status (dev,-1,urb->status);
511 for (i = 0; i < urb->number_of_packets; i++) {
512 int status = urb->iso_frame_desc[i].status;
515 print_err_status (dev,i,status);
519 len=urb->iso_frame_desc[i].actual_length;
521 // if (len>=TM6000_URB_MSG_LEN) {
522 p=urb->transfer_buffer + urb->iso_frame_desc[i].offset;
523 if (!urb->iso_frame_desc[i].status) {
524 if ((buf->fmt->fourcc)==V4L2_PIX_FMT_TM6000) {
525 rc=copy_multiplexed(p, outp, len, urb, &buf);
529 copy_streams(p, outp, len, urb, &buf);
533 if (copied >= size || !buf)
540 /* ------------------------------------------------------------------
542 ------------------------------------------------------------------*/
545 * IRQ callback, called by URB callback
547 static void tm6000_irq_callback(struct urb *urb)
549 struct tm6000_dmaqueue *dma_q = urb->context;
550 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
556 spin_lock(&dev->slock);
557 tm6000_isoc_copy(urb);
558 spin_unlock(&dev->slock);
560 /* Reset urb buffers */
561 for (i = 0; i < urb->number_of_packets; i++) {
562 urb->iso_frame_desc[i].status = 0;
563 urb->iso_frame_desc[i].actual_length = 0;
566 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
568 tm6000_err("urb resubmit failed (error=%i)\n",
573 * Stop and Deallocate URBs
575 static void tm6000_uninit_isoc(struct tm6000_core *dev)
580 dev->isoc_ctl.nfields = -1;
581 dev->isoc_ctl.buf = NULL;
582 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
583 urb=dev->isoc_ctl.urb[i];
587 if (dev->isoc_ctl.transfer_buffer[i]) {
588 usb_free_coherent(dev->udev,
589 urb->transfer_buffer_length,
590 dev->isoc_ctl.transfer_buffer[i],
594 dev->isoc_ctl.urb[i] = NULL;
596 dev->isoc_ctl.transfer_buffer[i] = NULL;
599 kfree (dev->isoc_ctl.urb);
600 kfree (dev->isoc_ctl.transfer_buffer);
602 dev->isoc_ctl.urb=NULL;
603 dev->isoc_ctl.transfer_buffer=NULL;
604 dev->isoc_ctl.num_bufs = 0;
606 dev->isoc_ctl.num_bufs=0;
610 * Allocate URBs and start IRQ
612 static int tm6000_prepare_isoc(struct tm6000_core *dev, unsigned int framesize)
614 struct tm6000_dmaqueue *dma_q = &dev->vidq;
615 int i, j, sb_size, pipe, size, max_packets, num_bufs = 5;
618 /* De-allocates all pending stuff */
619 tm6000_uninit_isoc(dev);
621 usb_set_interface(dev->udev,
622 dev->isoc_in.bInterfaceNumber,
623 dev->isoc_in.bAlternateSetting);
625 pipe = usb_rcvisocpipe(dev->udev,
626 dev->isoc_in.endp->desc.bEndpointAddress &
627 USB_ENDPOINT_NUMBER_MASK);
629 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
631 if (size > dev->isoc_in.maxsize)
632 size = dev->isoc_in.maxsize;
634 dev->isoc_ctl.max_pkt_size = size;
636 max_packets = ( framesize + size - 1) / size;
638 if (max_packets > TM6000_MAX_ISO_PACKETS)
639 max_packets = TM6000_MAX_ISO_PACKETS;
641 sb_size = max_packets * size;
643 dev->isoc_ctl.num_bufs = num_bufs;
645 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
646 if (!dev->isoc_ctl.urb) {
647 tm6000_err("cannot alloc memory for usb buffers\n");
651 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
653 if (!dev->isoc_ctl.transfer_buffer) {
654 tm6000_err("cannot allocate memory for usbtransfer\n");
655 kfree(dev->isoc_ctl.urb);
659 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
660 " (%d bytes) of %d bytes each to handle %u size\n",
661 max_packets, num_bufs, sb_size,
662 dev->isoc_in.maxsize, size);
664 /* allocate urbs and transfer buffers */
665 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
666 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
668 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
669 tm6000_uninit_isoc(dev);
673 dev->isoc_ctl.urb[i] = urb;
675 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
676 sb_size, GFP_KERNEL, &urb->transfer_dma);
677 if (!dev->isoc_ctl.transfer_buffer[i]) {
678 tm6000_err ("unable to allocate %i bytes for transfer"
681 in_interrupt()?" while in int":"");
682 tm6000_uninit_isoc(dev);
685 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
687 usb_fill_bulk_urb(urb, dev->udev, pipe,
688 dev->isoc_ctl.transfer_buffer[i], sb_size,
689 tm6000_irq_callback, dma_q);
690 urb->interval = dev->isoc_in.endp->desc.bInterval;
691 urb->number_of_packets = max_packets;
692 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
694 for (j = 0; j < max_packets; j++) {
695 urb->iso_frame_desc[j].offset = size * j;
696 urb->iso_frame_desc[j].length = size;
703 static int tm6000_start_thread( struct tm6000_core *dev)
705 struct tm6000_dmaqueue *dma_q = &dev->vidq;
709 dma_q->ini_jiffies=jiffies;
711 init_waitqueue_head(&dma_q->wq);
713 /* submit urbs and enables IRQ */
714 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
715 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
717 tm6000_err("submit of urb %i failed (error=%i)\n", i,
719 tm6000_uninit_isoc(dev);
727 /* ------------------------------------------------------------------
729 ------------------------------------------------------------------*/
732 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
734 struct tm6000_fh *fh = vq->priv_data;
736 *size = fh->fmt->depth * fh->width * fh->height >> 3;
738 *count = TM6000_DEF_BUF;
740 if (*count < TM6000_MIN_BUF) {
741 *count=TM6000_MIN_BUF;
744 while (*size * *count > vid_limit * 1024 * 1024)
750 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
752 struct tm6000_fh *fh = vq->priv_data;
753 struct tm6000_core *dev = fh->dev;
759 /* We used to wait for the buffer to finish here, but this didn't work
760 because, as we were keeping the state as VIDEOBUF_QUEUED,
761 videobuf_queue_cancel marked it as finished for us.
762 (Also, it could wedge forever if the hardware was misconfigured.)
764 This should be safe; by the time we get here, the buffer isn't
765 queued anymore. If we ever start marking the buffers as
766 VIDEOBUF_ACTIVE, it won't be, though.
768 spin_lock_irqsave(&dev->slock, flags);
769 if (dev->isoc_ctl.buf == buf)
770 dev->isoc_ctl.buf = NULL;
771 spin_unlock_irqrestore(&dev->slock, flags);
773 videobuf_vmalloc_free(&buf->vb);
774 buf->vb.state = VIDEOBUF_NEEDS_INIT;
778 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
779 enum v4l2_field field)
781 struct tm6000_fh *fh = vq->priv_data;
782 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
783 struct tm6000_core *dev = fh->dev;
784 int rc = 0, urb_init = 0;
786 BUG_ON(NULL == fh->fmt);
789 /* FIXME: It assumes depth=2 */
790 /* The only currently supported format is 16 bits/pixel */
791 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
792 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
795 if (buf->fmt != fh->fmt ||
796 buf->vb.width != fh->width ||
797 buf->vb.height != fh->height ||
798 buf->vb.field != field) {
800 buf->vb.width = fh->width;
801 buf->vb.height = fh->height;
802 buf->vb.field = field;
803 buf->vb.state = VIDEOBUF_NEEDS_INIT;
806 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
807 if (0 != (rc = videobuf_iolock(vq, &buf->vb, NULL)))
812 if (!dev->isoc_ctl.num_bufs)
816 rc = tm6000_prepare_isoc(dev, buf->vb.size);
820 rc = tm6000_start_thread(dev);
826 buf->vb.state = VIDEOBUF_PREPARED;
830 free_buffer(vq, buf);
835 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
837 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
838 struct tm6000_fh *fh = vq->priv_data;
839 struct tm6000_core *dev = fh->dev;
840 struct tm6000_dmaqueue *vidq = &dev->vidq;
842 buf->vb.state = VIDEOBUF_QUEUED;
843 list_add_tail(&buf->vb.queue, &vidq->active);
846 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
848 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
853 static struct videobuf_queue_ops tm6000_video_qops = {
854 .buf_setup = buffer_setup,
855 .buf_prepare = buffer_prepare,
856 .buf_queue = buffer_queue,
857 .buf_release = buffer_release,
860 /* ------------------------------------------------------------------
862 ------------------------------------------------------------------*/
864 static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh)
867 mutex_lock(&dev->lock);
868 if (dev->resources) {
869 /* no, someone else uses it */
870 mutex_unlock(&dev->lock);
873 /* it's free, grab it */
875 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
876 mutex_unlock(&dev->lock);
880 static int res_locked(struct tm6000_core *dev)
882 return (dev->resources);
885 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
887 mutex_lock(&dev->lock);
889 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
890 mutex_unlock(&dev->lock);
893 /* ------------------------------------------------------------------
894 IOCTL vidioc handling
895 ------------------------------------------------------------------*/
896 static int vidioc_querycap (struct file *file, void *priv,
897 struct v4l2_capability *cap)
899 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
901 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
902 strlcpy(cap->card,"Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
903 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
904 cap->version = TM6000_VERSION;
905 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
912 static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
913 struct v4l2_fmtdesc *f)
915 if (unlikely(f->index >= ARRAY_SIZE(format)))
918 strlcpy(f->description,format[f->index].name,sizeof(f->description));
919 f->pixelformat = format[f->index].fourcc;
923 static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
924 struct v4l2_format *f)
926 struct tm6000_fh *fh=priv;
928 f->fmt.pix.width = fh->width;
929 f->fmt.pix.height = fh->height;
930 f->fmt.pix.field = fh->vb_vidq.field;
931 f->fmt.pix.pixelformat = fh->fmt->fourcc;
932 f->fmt.pix.bytesperline =
933 (f->fmt.pix.width * fh->fmt->depth) >> 3;
934 f->fmt.pix.sizeimage =
935 f->fmt.pix.height * f->fmt.pix.bytesperline;
940 static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc)
944 for (i = 0; i < ARRAY_SIZE(format); i++)
945 if (format[i].fourcc == fourcc)
950 static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
951 struct v4l2_format *f)
953 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
954 struct tm6000_fmt *fmt;
955 enum v4l2_field field;
957 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
959 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
960 " invalid.\n", f->fmt.pix.pixelformat);
964 field = f->fmt.pix.field;
966 if (field == V4L2_FIELD_ANY) {
967 // field=V4L2_FIELD_INTERLACED;
968 field=V4L2_FIELD_SEQ_TB;
969 } else if (V4L2_FIELD_INTERLACED != field) {
970 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
974 tm6000_get_std_res (dev);
976 f->fmt.pix.width = dev->width;
977 f->fmt.pix.height = dev->height;
979 f->fmt.pix.width &= ~0x01;
981 f->fmt.pix.field = field;
983 f->fmt.pix.bytesperline =
984 (f->fmt.pix.width * fmt->depth) >> 3;
985 f->fmt.pix.sizeimage =
986 f->fmt.pix.height * f->fmt.pix.bytesperline;
991 /*FIXME: This seems to be generic enough to be at videodev2 */
992 static int vidioc_s_fmt_vid_cap (struct file *file, void *priv,
993 struct v4l2_format *f)
995 struct tm6000_fh *fh=priv;
996 struct tm6000_core *dev = fh->dev;
997 int ret = vidioc_try_fmt_vid_cap(file,fh,f);
1001 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1002 fh->width = f->fmt.pix.width;
1003 fh->height = f->fmt.pix.height;
1004 fh->vb_vidq.field = f->fmt.pix.field;
1007 dev->fourcc = f->fmt.pix.pixelformat;
1009 tm6000_set_fourcc_format(dev);
1014 static int vidioc_reqbufs (struct file *file, void *priv,
1015 struct v4l2_requestbuffers *p)
1017 struct tm6000_fh *fh=priv;
1019 return (videobuf_reqbufs(&fh->vb_vidq, p));
1022 static int vidioc_querybuf (struct file *file, void *priv,
1023 struct v4l2_buffer *p)
1025 struct tm6000_fh *fh=priv;
1027 return (videobuf_querybuf(&fh->vb_vidq, p));
1030 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1032 struct tm6000_fh *fh=priv;
1034 return (videobuf_qbuf(&fh->vb_vidq, p));
1037 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1039 struct tm6000_fh *fh=priv;
1041 return (videobuf_dqbuf(&fh->vb_vidq, p,
1042 file->f_flags & O_NONBLOCK));
1045 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1046 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1048 struct tm6000_fh *fh=priv;
1050 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1054 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1056 struct tm6000_fh *fh=priv;
1057 struct tm6000_core *dev = fh->dev;
1059 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1064 if (!res_get(dev,fh))
1066 return (videobuf_streamon(&fh->vb_vidq));
1069 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1071 struct tm6000_fh *fh=priv;
1072 struct tm6000_core *dev = fh->dev;
1074 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1079 videobuf_streamoff(&fh->vb_vidq);
1085 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1088 struct tm6000_fh *fh=priv;
1089 struct tm6000_core *dev = fh->dev;
1091 rc=tm6000_set_standard (dev, norm);
1093 fh->width = dev->width;
1094 fh->height = dev->height;
1099 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1104 static int vidioc_enum_input (struct file *file, void *priv,
1105 struct v4l2_input *inp)
1107 switch (inp->index) {
1108 case TM6000_INPUT_TV:
1109 inp->type = V4L2_INPUT_TYPE_TUNER;
1110 strcpy(inp->name,"Television");
1112 case TM6000_INPUT_COMPOSITE:
1113 inp->type = V4L2_INPUT_TYPE_CAMERA;
1114 strcpy(inp->name,"Composite");
1116 case TM6000_INPUT_SVIDEO:
1117 inp->type = V4L2_INPUT_TYPE_CAMERA;
1118 strcpy(inp->name,"S-Video");
1123 inp->std = TM6000_STD;
1128 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1130 struct tm6000_fh *fh=priv;
1131 struct tm6000_core *dev = fh->dev;
1137 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1139 struct tm6000_fh *fh=priv;
1140 struct tm6000_core *dev = fh->dev;
1145 case TM6000_INPUT_TV:
1149 case TM6000_INPUT_COMPOSITE:
1150 case TM6000_INPUT_SVIDEO:
1157 rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1158 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1162 rc=vidioc_s_std (file, priv, &dev->vfd->current_norm);
1168 /* --- controls ---------------------------------------------- */
1169 static int vidioc_queryctrl (struct file *file, void *priv,
1170 struct v4l2_queryctrl *qc)
1174 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1175 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1176 memcpy(qc, &(tm6000_qctrl[i]),
1184 static int vidioc_g_ctrl (struct file *file, void *priv,
1185 struct v4l2_control *ctrl)
1187 struct tm6000_fh *fh=priv;
1188 struct tm6000_core *dev = fh->dev;
1191 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1193 case V4L2_CID_CONTRAST:
1194 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
1196 case V4L2_CID_BRIGHTNESS:
1197 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
1199 case V4L2_CID_SATURATION:
1200 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
1203 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
1216 static int vidioc_s_ctrl (struct file *file, void *priv,
1217 struct v4l2_control *ctrl)
1219 struct tm6000_fh *fh =priv;
1220 struct tm6000_core *dev = fh->dev;
1224 case V4L2_CID_CONTRAST:
1225 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1227 case V4L2_CID_BRIGHTNESS:
1228 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1230 case V4L2_CID_SATURATION:
1231 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1234 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1240 static int vidioc_g_tuner (struct file *file, void *priv,
1241 struct v4l2_tuner *t)
1243 struct tm6000_fh *fh =priv;
1244 struct tm6000_core *dev = fh->dev;
1246 if (unlikely(UNSET == dev->tuner_type))
1251 strcpy(t->name, "Television");
1252 t->type = V4L2_TUNER_ANALOG_TV;
1253 t->capability = V4L2_TUNER_CAP_NORM;
1254 t->rangehigh = 0xffffffffUL;
1255 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1260 static int vidioc_s_tuner (struct file *file, void *priv,
1261 struct v4l2_tuner *t)
1263 struct tm6000_fh *fh =priv;
1264 struct tm6000_core *dev = fh->dev;
1266 if (UNSET == dev->tuner_type)
1274 static int vidioc_g_frequency (struct file *file, void *priv,
1275 struct v4l2_frequency *f)
1277 struct tm6000_fh *fh =priv;
1278 struct tm6000_core *dev = fh->dev;
1280 if (unlikely(UNSET == dev->tuner_type))
1283 f->type = V4L2_TUNER_ANALOG_TV;
1284 f->frequency = dev->freq;
1286 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1291 static int vidioc_s_frequency (struct file *file, void *priv,
1292 struct v4l2_frequency *f)
1294 struct tm6000_fh *fh =priv;
1295 struct tm6000_core *dev = fh->dev;
1297 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1300 if (unlikely(UNSET == dev->tuner_type))
1302 if (unlikely(f->tuner != 0))
1305 // mutex_lock(&dev->lock);
1306 dev->freq = f->frequency;
1307 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1308 // mutex_unlock(&dev->lock);
1313 /* ------------------------------------------------------------------
1314 File operations for the device
1315 ------------------------------------------------------------------*/
1317 static int tm6000_open(struct file *file)
1319 struct video_device *vdev = video_devdata(file);
1320 struct tm6000_core *dev = video_drvdata(file);
1321 struct tm6000_fh *fh;
1322 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1325 printk(KERN_INFO "tm6000: open called (dev=%s)\n",
1326 video_device_node_name(vdev));
1328 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1329 video_device_node_name(vdev));
1332 /* If more than one user, mutex should be added */
1335 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1336 video_device_node_name(vdev), v4l2_type_names[type],
1339 /* allocate + initialize per filehandle data */
1340 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1346 file->private_data = fh;
1349 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1350 dev->fourcc = format[0].fourcc;
1352 fh->fmt = format_by_fourcc(dev->fourcc);
1354 tm6000_get_std_res (dev);
1356 fh->width = dev->width;
1357 fh->height = dev->height;
1359 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1360 "dev->vidq=0x%08lx\n",
1361 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1362 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1363 "queued=%d\n",list_empty(&dev->vidq.queued));
1364 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1365 "active=%d\n",list_empty(&dev->vidq.active));
1367 /* initialize hardware on analog mode */
1368 if (dev->mode!=TM6000_MODE_ANALOG) {
1369 rc=tm6000_init_analog_mode (dev);
1373 /* Put all controls at a sane state */
1374 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1375 qctl_regs[i] =tm6000_qctrl[i].default_value;
1377 dev->mode=TM6000_MODE_ANALOG;
1380 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1383 V4L2_FIELD_INTERLACED,
1384 sizeof(struct tm6000_buffer),fh);
1390 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1392 struct tm6000_fh *fh = file->private_data;
1394 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1395 if (res_locked(fh->dev))
1398 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1399 file->f_flags & O_NONBLOCK);
1405 tm6000_poll(struct file *file, struct poll_table_struct *wait)
1407 struct tm6000_fh *fh = file->private_data;
1408 struct tm6000_buffer *buf;
1410 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1413 if (res_get(fh->dev,fh)) {
1414 /* streaming capture */
1415 if (list_empty(&fh->vb_vidq.stream))
1417 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1419 /* read() capture */
1420 return videobuf_poll_stream(file, &fh->vb_vidq,
1423 poll_wait(file, &buf->vb.done, wait);
1424 if (buf->vb.state == VIDEOBUF_DONE ||
1425 buf->vb.state == VIDEOBUF_ERROR)
1426 return POLLIN|POLLRDNORM;
1430 static int tm6000_release(struct file *file)
1432 struct tm6000_fh *fh = file->private_data;
1433 struct tm6000_core *dev = fh->dev;
1434 struct video_device *vdev = video_devdata(file);
1436 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1437 video_device_node_name(vdev), dev->users);
1442 tm6000_uninit_isoc(dev);
1443 videobuf_mmap_free(&fh->vb_vidq);
1451 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1453 struct tm6000_fh *fh = file->private_data;
1456 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1461 static struct v4l2_file_operations tm6000_fops = {
1462 .owner = THIS_MODULE,
1463 .open = tm6000_open,
1464 .release = tm6000_release,
1465 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1466 .read = tm6000_read,
1467 .poll = tm6000_poll,
1468 .mmap = tm6000_mmap,
1471 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1472 .vidioc_querycap = vidioc_querycap,
1473 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1474 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1475 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1476 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1477 .vidioc_s_std = vidioc_s_std,
1478 .vidioc_enum_input = vidioc_enum_input,
1479 .vidioc_g_input = vidioc_g_input,
1480 .vidioc_s_input = vidioc_s_input,
1481 .vidioc_queryctrl = vidioc_queryctrl,
1482 .vidioc_g_ctrl = vidioc_g_ctrl,
1483 .vidioc_s_ctrl = vidioc_s_ctrl,
1484 .vidioc_g_tuner = vidioc_g_tuner,
1485 .vidioc_s_tuner = vidioc_s_tuner,
1486 .vidioc_g_frequency = vidioc_g_frequency,
1487 .vidioc_s_frequency = vidioc_s_frequency,
1488 .vidioc_streamon = vidioc_streamon,
1489 .vidioc_streamoff = vidioc_streamoff,
1490 .vidioc_reqbufs = vidioc_reqbufs,
1491 .vidioc_querybuf = vidioc_querybuf,
1492 .vidioc_qbuf = vidioc_qbuf,
1493 .vidioc_dqbuf = vidioc_dqbuf,
1494 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1495 .vidiocgmbuf = vidiocgmbuf,
1499 static struct video_device tm6000_template = {
1501 .fops = &tm6000_fops,
1502 .ioctl_ops = &video_ioctl_ops,
1503 .release = video_device_release,
1504 .tvnorms = TM6000_STD,
1505 .current_norm = V4L2_STD_NTSC_M,
1508 /* -----------------------------------------------------------------
1509 Initialization and module stuff
1510 ------------------------------------------------------------------*/
1512 int tm6000_v4l2_register(struct tm6000_core *dev)
1515 struct video_device *vfd;
1517 vfd = video_device_alloc();
1523 /* init video dma queues */
1524 INIT_LIST_HEAD(&dev->vidq.active);
1525 INIT_LIST_HEAD(&dev->vidq.queued);
1527 memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1528 dev->vfd->debug=tm6000_debug;
1529 vfd->v4l2_dev = &dev->v4l2_dev;
1530 video_set_drvdata(vfd, dev);
1532 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
1533 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1537 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1539 video_unregister_device(dev->vfd);
1544 int tm6000_v4l2_exit(void)
1549 module_param(video_nr, int, 0);
1550 MODULE_PARM_DESC(video_nr,"Allow changing video device number");
1552 module_param_named (debug, tm6000_debug, int, 0444);
1553 MODULE_PARM_DESC(debug,"activates debug info");
1555 module_param(vid_limit,int,0644);
1556 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");