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 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);
155 memset(outp, 0, (*buf)->vb.size);
161 * Announces that a buffer were filled and request the next
163 static inline void buffer_filled(struct tm6000_core *dev,
164 struct tm6000_dmaqueue *dma_q,
165 struct tm6000_buffer *buf)
167 /* Advice that buffer was filled */
168 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
169 buf->vb.state = VIDEOBUF_DONE;
170 buf->vb.field_count++;
171 do_gettimeofday(&buf->vb.ts);
173 list_del(&buf->vb.queue);
174 wake_up(&buf->vb.done);
177 const char *tm6000_msg_type[] = {
178 "unknown(0)", /* 0 */
184 "unknown(6)", /* 6 */
185 "unknown(7)", /* 7 */
189 * Identify the tm5600/6000 buffer header type and properly handles
191 static int copy_packet(struct urb *urb, u32 header, u8 **ptr, u8 *endp,
192 u8 *out_p, struct tm6000_buffer **buf)
194 struct tm6000_dmaqueue *dma_q = urb->context;
195 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
197 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
199 /* FIXME: move to tm6000-isoc */
200 static int last_line = -2, start_line = -2, last_field = -2;
202 /* FIXME: this is the hardcoded window size
204 unsigned int linewidth = (*buf)->vb.width << 1;
206 if (!dev->isoc_ctl.cmd) {
207 c = (header >> 24) & 0xff;
209 /* split the header fields */
210 size = (((header & 0x7e) << 1) -1) *4;
211 block = (header >> 7) & 0xf;
212 field = (header >> 11) & 0x1;
213 line = (header >> 12) & 0x1ff;
214 cmd = (header >> 21) & 0x7;
216 /* Validates header fields */
217 if(size > TM6000_URB_MSG_LEN)
218 size = TM6000_URB_MSG_LEN;
220 if (cmd == TM6000_URB_MSG_VIDEO) {
221 if ((block+1)*TM6000_URB_MSG_LEN>linewidth)
222 cmd = TM6000_URB_MSG_ERR;
224 /* FIXME: Mounts the image as field0+field1
225 * It should, instead, check if the user selected
226 * entrelaced or non-entrelaced mode
228 pos= ((line<<1)+field)*linewidth +
229 block*TM6000_URB_MSG_LEN;
231 /* Don't allow to write out of the buffer */
232 if (pos+TM6000_URB_MSG_LEN > (*buf)->vb.size) {
233 dprintk(dev, V4L2_DEBUG_ISOC,
234 "ERR: size=%d, num=%d, line=%d, "
236 size, block, line, field);
238 cmd = TM6000_URB_MSG_ERR;
244 /* Prints debug info */
245 dprintk(dev, V4L2_DEBUG_ISOC, "size=%d, num=%d, "
246 " line=%d, field=%d\n",
247 size, block, line, field);
249 if ((last_line!=line)&&(last_line+1!=line) &&
250 (cmd != TM6000_URB_MSG_ERR) ) {
251 if (cmd != TM6000_URB_MSG_VIDEO) {
252 dprintk(dev, V4L2_DEBUG_ISOC, "cmd=%d, "
253 "size=%d, num=%d, line=%d, field=%d\n",
254 cmd, size, block, line, field);
257 start_line=last_line;
258 /* Prints debug info */
259 dprintk(dev, V4L2_DEBUG_ISOC, "lines= %d-%d, "
261 start_line, last_line, field);
263 if ((start_line<6 && last_line>200) &&
264 (last_field != field) ) {
266 dev->isoc_ctl.nfields++;
267 if (dev->isoc_ctl.nfields>=2) {
268 dev->isoc_ctl.nfields=0;
270 /* Announces that a new buffer were filled */
271 buffer_filled (dev, dma_q, *buf);
272 dprintk(dev, V4L2_DEBUG_ISOC,
273 "new buffer filled\n");
274 get_next_buf (dma_q, buf);
277 out_p = videobuf_to_vmalloc(&((*buf)->vb));
281 pos = dev->isoc_ctl.pos = 0;
288 if (cmd == TM6000_URB_MSG_VIDEO)
291 pktsize = TM6000_URB_MSG_LEN;
293 /* Continue the last copy */
294 cmd = dev->isoc_ctl.cmd;
295 size= dev->isoc_ctl.size;
296 pos = dev->isoc_ctl.pos;
297 pktsize = dev->isoc_ctl.pktsize;
300 cpysize = (endp-(*ptr) > size) ? size : endp - *ptr;
303 /* handles each different URB message */
305 case TM6000_URB_MSG_VIDEO:
306 /* Fills video buffer */
307 memcpy(&out_p[pos], *ptr, cpysize);
309 case TM6000_URB_MSG_PTS:
311 case TM6000_URB_MSG_AUDIO:
312 /* Need some code to process audio */
313 printk ("%ld: cmd=%s, size=%d\n", jiffies,
314 tm6000_msg_type[cmd],size);
317 dprintk (dev, V4L2_DEBUG_ISOC, "cmd=%s, size=%d\n",
318 tm6000_msg_type[cmd],size);
322 /* End of URB packet, but cmd processing is not
323 * complete. Preserve the state for a next packet
325 dev->isoc_ctl.pos = pos+cpysize;
326 dev->isoc_ctl.size= size-cpysize;
327 dev->isoc_ctl.cmd = cmd;
328 dev->isoc_ctl.pktsize = pktsize-cpysize;
331 dev->isoc_ctl.cmd = 0;
338 static int copy_streams(u8 *data, u8 *out_p, unsigned long len,
339 struct urb *urb, struct tm6000_buffer **buf)
341 struct tm6000_dmaqueue *dma_q = urb->context;
342 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
343 u8 *ptr=data, *endp=data+len;
344 unsigned long header=0;
347 for (ptr=data; ptr<endp;) {
348 if (!dev->isoc_ctl.cmd) {
349 u8 *p=(u8 *)&dev->isoc_ctl.tmp_buf;
350 /* FIXME: This seems very complex
351 * It just recovers up to 3 bytes of the header that
352 * might be at the previous packet
354 if (dev->isoc_ctl.tmp_buf_len) {
355 while (dev->isoc_ctl.tmp_buf_len) {
356 if ( *(ptr+3-dev->isoc_ctl.tmp_buf_len) == 0x47) {
360 dev->isoc_ctl.tmp_buf_len--;
362 if (dev->isoc_ctl.tmp_buf_len) {
364 dev->isoc_ctl.tmp_buf_len);
365 memcpy((u8 *)&header +
366 dev->isoc_ctl.tmp_buf_len,
368 4 - dev->isoc_ctl.tmp_buf_len);
369 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
374 for (;ptr<endp-3;ptr++) {
380 dev->isoc_ctl.tmp_buf_len=endp-ptr;
381 memcpy (&dev->isoc_ctl.tmp_buf,ptr,
382 dev->isoc_ctl.tmp_buf_len);
387 /* Get message header */
388 header=*(unsigned long *)ptr;
392 /* Copy or continue last copy */
393 rc=copy_packet(urb,header,&ptr,endp,out_p,buf);
396 printk(KERN_ERR "tm6000: buffer underrun at %ld\n",
407 * Identify the tm5600/6000 buffer header type and properly handles
409 static int copy_multiplexed(u8 *ptr, u8 *out_p, unsigned long len,
410 struct urb *urb, struct tm6000_buffer **buf)
412 struct tm6000_dmaqueue *dma_q = urb->context;
413 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
414 unsigned int pos=dev->isoc_ctl.pos,cpysize;
418 cpysize=min(len,(*buf)->vb.size-pos);
419 //printk("Copying %d bytes (max=%lu) from %p to %p[%u]\n",cpysize,(*buf)->vb.size,ptr,out_p,pos);
420 memcpy(&out_p[pos], ptr, cpysize);
424 if (pos >= (*buf)->vb.size) {
426 /* Announces that a new buffer were filled */
427 buffer_filled (dev, dma_q, *buf);
428 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
429 get_next_buf (dma_q, buf);
432 out_p = videobuf_to_vmalloc(&((*buf)->vb));
439 dev->isoc_ctl.pos=pos;
443 static void inline print_err_status (struct tm6000_core *dev,
444 int packet, int status)
446 char *errmsg = "Unknown";
450 errmsg = "unlinked synchronuously";
453 errmsg = "unlinked asynchronuously";
456 errmsg = "Buffer error (overrun)";
459 errmsg = "Stalled (device not responding)";
462 errmsg = "Babble (bad cable?)";
465 errmsg = "Bit-stuff error (bad cable?)";
468 errmsg = "CRC/Timeout (could be anything)";
471 errmsg = "Device does not respond";
475 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
478 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
479 packet, status, errmsg);
485 * Controls the isoc copy of each urb packet
487 static inline int tm6000_isoc_copy(struct urb *urb)
489 struct tm6000_dmaqueue *dma_q = urb->context;
490 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
491 struct tm6000_buffer *buf;
494 char *outp = NULL, *p;
495 unsigned long copied;
497 get_next_buf(dma_q, &buf);
499 outp = videobuf_to_vmalloc(&buf->vb);
509 print_err_status (dev,-1,urb->status);
513 for (i = 0; i < urb->number_of_packets; i++) {
514 int status = urb->iso_frame_desc[i].status;
517 print_err_status (dev,i,status);
521 len=urb->iso_frame_desc[i].actual_length;
523 // if (len>=TM6000_URB_MSG_LEN) {
524 p=urb->transfer_buffer + urb->iso_frame_desc[i].offset;
525 if (!urb->iso_frame_desc[i].status) {
526 if ((buf->fmt->fourcc)==V4L2_PIX_FMT_TM6000) {
527 rc=copy_multiplexed(p, outp, len, urb, &buf);
531 copy_streams(p, outp, len, urb, &buf);
535 if (copied >= size || !buf)
542 /* ------------------------------------------------------------------
544 ------------------------------------------------------------------*/
547 * IRQ callback, called by URB callback
549 static void tm6000_irq_callback(struct urb *urb)
551 struct tm6000_dmaqueue *dma_q = urb->context;
552 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
558 spin_lock(&dev->slock);
559 tm6000_isoc_copy(urb);
560 spin_unlock(&dev->slock);
562 /* Reset urb buffers */
563 for (i = 0; i < urb->number_of_packets; i++) {
564 urb->iso_frame_desc[i].status = 0;
565 urb->iso_frame_desc[i].actual_length = 0;
568 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
570 tm6000_err("urb resubmit failed (error=%i)\n",
575 * Stop and Deallocate URBs
577 static void tm6000_uninit_isoc(struct tm6000_core *dev)
582 dev->isoc_ctl.nfields = -1;
583 dev->isoc_ctl.buf = NULL;
584 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
585 urb=dev->isoc_ctl.urb[i];
589 if (dev->isoc_ctl.transfer_buffer[i]) {
590 usb_buffer_free(dev->udev,
591 urb->transfer_buffer_length,
592 dev->isoc_ctl.transfer_buffer[i],
596 dev->isoc_ctl.urb[i] = NULL;
598 dev->isoc_ctl.transfer_buffer[i] = NULL;
601 kfree (dev->isoc_ctl.urb);
602 kfree (dev->isoc_ctl.transfer_buffer);
604 dev->isoc_ctl.urb=NULL;
605 dev->isoc_ctl.transfer_buffer=NULL;
606 dev->isoc_ctl.num_bufs = 0;
608 dev->isoc_ctl.num_bufs=0;
612 * Allocate URBs and start IRQ
614 static int tm6000_prepare_isoc(struct tm6000_core *dev, unsigned int framesize)
616 struct tm6000_dmaqueue *dma_q = &dev->vidq;
617 int i, j, sb_size, pipe, size, max_packets, num_bufs = 5;
620 /* De-allocates all pending stuff */
621 tm6000_uninit_isoc(dev);
623 usb_set_interface(dev->udev,
624 dev->isoc_in.bInterfaceNumber,
625 dev->isoc_in.bAlternateSetting);
627 pipe = usb_rcvisocpipe(dev->udev,
628 dev->isoc_in.endp->desc.bEndpointAddress &
629 USB_ENDPOINT_NUMBER_MASK);
631 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
633 if (size > dev->isoc_in.maxsize)
634 size = dev->isoc_in.maxsize;
636 dev->isoc_ctl.max_pkt_size = size;
638 max_packets = ( framesize + size - 1) / size;
640 if (max_packets > TM6000_MAX_ISO_PACKETS)
641 max_packets = TM6000_MAX_ISO_PACKETS;
643 sb_size = max_packets * size;
645 dev->isoc_ctl.num_bufs = num_bufs;
647 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
648 if (!dev->isoc_ctl.urb) {
649 tm6000_err("cannot alloc memory for usb buffers\n");
653 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
655 if (!dev->isoc_ctl.transfer_buffer) {
656 tm6000_err("cannot allocate memory for usbtransfer\n");
657 kfree(dev->isoc_ctl.urb);
661 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
662 " (%d bytes) of %d bytes each to handle %u size\n",
663 max_packets, num_bufs, sb_size,
664 dev->isoc_in.maxsize, size);
666 /* allocate urbs and transfer buffers */
667 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
668 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
670 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
671 tm6000_uninit_isoc(dev);
675 dev->isoc_ctl.urb[i] = urb;
677 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
678 sb_size, GFP_KERNEL, &urb->transfer_dma);
679 if (!dev->isoc_ctl.transfer_buffer[i]) {
680 tm6000_err ("unable to allocate %i bytes for transfer"
683 in_interrupt()?" while in int":"");
684 tm6000_uninit_isoc(dev);
687 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
689 usb_fill_bulk_urb(urb, dev->udev, pipe,
690 dev->isoc_ctl.transfer_buffer[i], sb_size,
691 tm6000_irq_callback, dma_q);
692 urb->interval = dev->isoc_in.endp->desc.bInterval;
693 urb->number_of_packets = max_packets;
694 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
696 for (j = 0; j < max_packets; j++) {
697 urb->iso_frame_desc[j].offset = size * j;
698 urb->iso_frame_desc[j].length = size;
705 static int tm6000_start_thread( struct tm6000_core *dev)
707 struct tm6000_dmaqueue *dma_q = &dev->vidq;
711 dma_q->ini_jiffies=jiffies;
713 init_waitqueue_head(&dma_q->wq);
715 /* submit urbs and enables IRQ */
716 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
717 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
719 tm6000_err("submit of urb %i failed (error=%i)\n", i,
721 tm6000_uninit_isoc(dev);
729 /* ------------------------------------------------------------------
731 ------------------------------------------------------------------*/
734 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
736 struct tm6000_fh *fh = vq->priv_data;
738 *size = fh->fmt->depth * fh->width * fh->height >> 3;
740 *count = TM6000_DEF_BUF;
742 if (*count < TM6000_MIN_BUF) {
743 *count=TM6000_MIN_BUF;
746 while (*size * *count > vid_limit * 1024 * 1024)
752 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
754 struct tm6000_fh *fh = vq->priv_data;
755 struct tm6000_core *dev = fh->dev;
761 /* We used to wait for the buffer to finish here, but this didn't work
762 because, as we were keeping the state as VIDEOBUF_QUEUED,
763 videobuf_queue_cancel marked it as finished for us.
764 (Also, it could wedge forever if the hardware was misconfigured.)
766 This should be safe; by the time we get here, the buffer isn't
767 queued anymore. If we ever start marking the buffers as
768 VIDEOBUF_ACTIVE, it won't be, though.
770 spin_lock_irqsave(&dev->slock, flags);
771 if (dev->isoc_ctl.buf == buf)
772 dev->isoc_ctl.buf = NULL;
773 spin_unlock_irqrestore(&dev->slock, flags);
775 videobuf_vmalloc_free(&buf->vb);
776 buf->vb.state = VIDEOBUF_NEEDS_INIT;
780 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
781 enum v4l2_field field)
783 struct tm6000_fh *fh = vq->priv_data;
784 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
785 struct tm6000_core *dev = fh->dev;
786 int rc = 0, urb_init = 0;
788 BUG_ON(NULL == fh->fmt);
791 /* FIXME: It assumes depth=2 */
792 /* The only currently supported format is 16 bits/pixel */
793 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
794 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
797 if (buf->fmt != fh->fmt ||
798 buf->vb.width != fh->width ||
799 buf->vb.height != fh->height ||
800 buf->vb.field != field) {
802 buf->vb.width = fh->width;
803 buf->vb.height = fh->height;
804 buf->vb.field = field;
805 buf->vb.state = VIDEOBUF_NEEDS_INIT;
808 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
809 if (0 != (rc = videobuf_iolock(vq, &buf->vb, NULL)))
814 if (!dev->isoc_ctl.num_bufs)
818 rc = tm6000_prepare_isoc(dev, buf->vb.size);
822 rc = tm6000_start_thread(dev);
828 buf->vb.state = VIDEOBUF_PREPARED;
832 free_buffer(vq, buf);
837 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
839 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
840 struct tm6000_fh *fh = vq->priv_data;
841 struct tm6000_core *dev = fh->dev;
842 struct tm6000_dmaqueue *vidq = &dev->vidq;
844 buf->vb.state = VIDEOBUF_QUEUED;
845 list_add_tail(&buf->vb.queue, &vidq->active);
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);
855 static struct videobuf_queue_ops tm6000_video_qops = {
856 .buf_setup = buffer_setup,
857 .buf_prepare = buffer_prepare,
858 .buf_queue = buffer_queue,
859 .buf_release = buffer_release,
862 /* ------------------------------------------------------------------
864 ------------------------------------------------------------------*/
866 static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh)
869 mutex_lock(&dev->lock);
870 if (dev->resources) {
871 /* no, someone else uses it */
872 mutex_unlock(&dev->lock);
875 /* it's free, grab it */
877 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
878 mutex_unlock(&dev->lock);
882 static int res_locked(struct tm6000_core *dev)
884 return (dev->resources);
887 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
889 mutex_lock(&dev->lock);
891 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
892 mutex_unlock(&dev->lock);
895 /* ------------------------------------------------------------------
896 IOCTL vidioc handling
897 ------------------------------------------------------------------*/
898 static int vidioc_querycap (struct file *file, void *priv,
899 struct v4l2_capability *cap)
901 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
903 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
904 strlcpy(cap->card,"Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
905 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
906 cap->version = TM6000_VERSION;
907 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
914 static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
915 struct v4l2_fmtdesc *f)
917 if (unlikely(f->index >= ARRAY_SIZE(format)))
920 strlcpy(f->description,format[f->index].name,sizeof(f->description));
921 f->pixelformat = format[f->index].fourcc;
925 static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
926 struct v4l2_format *f)
928 struct tm6000_fh *fh=priv;
930 f->fmt.pix.width = fh->width;
931 f->fmt.pix.height = fh->height;
932 f->fmt.pix.field = fh->vb_vidq.field;
933 f->fmt.pix.pixelformat = fh->fmt->fourcc;
934 f->fmt.pix.bytesperline =
935 (f->fmt.pix.width * fh->fmt->depth) >> 3;
936 f->fmt.pix.sizeimage =
937 f->fmt.pix.height * f->fmt.pix.bytesperline;
942 static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc)
946 for (i = 0; i < ARRAY_SIZE(format); i++)
947 if (format[i].fourcc == fourcc)
952 static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
953 struct v4l2_format *f)
955 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
956 struct tm6000_fmt *fmt;
957 enum v4l2_field field;
959 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
961 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
962 " invalid.\n", f->fmt.pix.pixelformat);
966 field = f->fmt.pix.field;
968 if (field == V4L2_FIELD_ANY) {
969 // field=V4L2_FIELD_INTERLACED;
970 field=V4L2_FIELD_SEQ_TB;
971 } else if (V4L2_FIELD_INTERLACED != field) {
972 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
976 tm6000_get_std_res (dev);
978 f->fmt.pix.width = dev->width;
979 f->fmt.pix.height = dev->height;
981 f->fmt.pix.width &= ~0x01;
983 f->fmt.pix.field = field;
985 f->fmt.pix.bytesperline =
986 (f->fmt.pix.width * fmt->depth) >> 3;
987 f->fmt.pix.sizeimage =
988 f->fmt.pix.height * f->fmt.pix.bytesperline;
993 /*FIXME: This seems to be generic enough to be at videodev2 */
994 static int vidioc_s_fmt_vid_cap (struct file *file, void *priv,
995 struct v4l2_format *f)
997 struct tm6000_fh *fh=priv;
998 struct tm6000_core *dev = fh->dev;
999 int ret = vidioc_try_fmt_vid_cap(file,fh,f);
1003 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1004 fh->width = f->fmt.pix.width;
1005 fh->height = f->fmt.pix.height;
1006 fh->vb_vidq.field = f->fmt.pix.field;
1009 dev->fourcc = f->fmt.pix.pixelformat;
1011 tm6000_set_fourcc_format(dev);
1016 static int vidioc_reqbufs (struct file *file, void *priv,
1017 struct v4l2_requestbuffers *p)
1019 struct tm6000_fh *fh=priv;
1021 return (videobuf_reqbufs(&fh->vb_vidq, p));
1024 static int vidioc_querybuf (struct file *file, void *priv,
1025 struct v4l2_buffer *p)
1027 struct tm6000_fh *fh=priv;
1029 return (videobuf_querybuf(&fh->vb_vidq, p));
1032 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1034 struct tm6000_fh *fh=priv;
1036 return (videobuf_qbuf(&fh->vb_vidq, p));
1039 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1041 struct tm6000_fh *fh=priv;
1043 return (videobuf_dqbuf(&fh->vb_vidq, p,
1044 file->f_flags & O_NONBLOCK));
1047 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1048 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1050 struct tm6000_fh *fh=priv;
1052 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1056 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1058 struct tm6000_fh *fh=priv;
1059 struct tm6000_core *dev = fh->dev;
1061 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1066 if (!res_get(dev,fh))
1068 return (videobuf_streamon(&fh->vb_vidq));
1071 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1073 struct tm6000_fh *fh=priv;
1074 struct tm6000_core *dev = fh->dev;
1076 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1081 videobuf_streamoff(&fh->vb_vidq);
1087 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1090 struct tm6000_fh *fh=priv;
1091 struct tm6000_core *dev = fh->dev;
1093 rc=tm6000_set_standard (dev, norm);
1095 fh->width = dev->width;
1096 fh->height = dev->height;
1101 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1106 static int vidioc_enum_input (struct file *file, void *priv,
1107 struct v4l2_input *inp)
1109 switch (inp->index) {
1110 case TM6000_INPUT_TV:
1111 inp->type = V4L2_INPUT_TYPE_TUNER;
1112 strcpy(inp->name,"Television");
1114 case TM6000_INPUT_COMPOSITE:
1115 inp->type = V4L2_INPUT_TYPE_CAMERA;
1116 strcpy(inp->name,"Composite");
1118 case TM6000_INPUT_SVIDEO:
1119 inp->type = V4L2_INPUT_TYPE_CAMERA;
1120 strcpy(inp->name,"S-Video");
1125 inp->std = TM6000_STD;
1130 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1132 struct tm6000_fh *fh=priv;
1133 struct tm6000_core *dev = fh->dev;
1139 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1141 struct tm6000_fh *fh=priv;
1142 struct tm6000_core *dev = fh->dev;
1147 case TM6000_INPUT_TV:
1151 case TM6000_INPUT_COMPOSITE:
1152 case TM6000_INPUT_SVIDEO:
1159 rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1160 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1164 rc=vidioc_s_std (file, priv, &dev->vfd->current_norm);
1170 /* --- controls ---------------------------------------------- */
1171 static int vidioc_queryctrl (struct file *file, void *priv,
1172 struct v4l2_queryctrl *qc)
1176 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1177 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1178 memcpy(qc, &(tm6000_qctrl[i]),
1186 static int vidioc_g_ctrl (struct file *file, void *priv,
1187 struct v4l2_control *ctrl)
1189 struct tm6000_fh *fh=priv;
1190 struct tm6000_core *dev = fh->dev;
1193 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1195 case V4L2_CID_CONTRAST:
1196 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
1198 case V4L2_CID_BRIGHTNESS:
1199 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
1201 case V4L2_CID_SATURATION:
1202 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
1205 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
1218 static int vidioc_s_ctrl (struct file *file, void *priv,
1219 struct v4l2_control *ctrl)
1221 struct tm6000_fh *fh =priv;
1222 struct tm6000_core *dev = fh->dev;
1226 case V4L2_CID_CONTRAST:
1227 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1229 case V4L2_CID_BRIGHTNESS:
1230 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1232 case V4L2_CID_SATURATION:
1233 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1236 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1242 static int vidioc_g_tuner (struct file *file, void *priv,
1243 struct v4l2_tuner *t)
1245 struct tm6000_fh *fh =priv;
1246 struct tm6000_core *dev = fh->dev;
1248 if (unlikely(UNSET == dev->tuner_type))
1253 strcpy(t->name, "Television");
1254 t->type = V4L2_TUNER_ANALOG_TV;
1255 t->capability = V4L2_TUNER_CAP_NORM;
1256 t->rangehigh = 0xffffffffUL;
1257 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1262 static int vidioc_s_tuner (struct file *file, void *priv,
1263 struct v4l2_tuner *t)
1265 struct tm6000_fh *fh =priv;
1266 struct tm6000_core *dev = fh->dev;
1268 if (UNSET == dev->tuner_type)
1276 static int vidioc_g_frequency (struct file *file, void *priv,
1277 struct v4l2_frequency *f)
1279 struct tm6000_fh *fh =priv;
1280 struct tm6000_core *dev = fh->dev;
1282 if (unlikely(UNSET == dev->tuner_type))
1285 f->type = V4L2_TUNER_ANALOG_TV;
1286 f->frequency = dev->freq;
1288 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1293 static int vidioc_s_frequency (struct file *file, void *priv,
1294 struct v4l2_frequency *f)
1296 struct tm6000_fh *fh =priv;
1297 struct tm6000_core *dev = fh->dev;
1299 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1302 if (unlikely(UNSET == dev->tuner_type))
1304 if (unlikely(f->tuner != 0))
1307 // mutex_lock(&dev->lock);
1308 dev->freq = f->frequency;
1309 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1310 // mutex_unlock(&dev->lock);
1315 /* ------------------------------------------------------------------
1316 File operations for the device
1317 ------------------------------------------------------------------*/
1319 static int tm6000_open(struct file *file)
1321 struct video_device *vdev = video_devdata(file);
1322 struct tm6000_core *dev = video_drvdata(file);
1323 struct tm6000_fh *fh;
1324 struct list_head *list;
1325 enum v4l2_buf_type type = 0;
1328 printk(KERN_INFO "tm6000: open called (dev=%s)\n",
1329 video_device_node_name(vdev));
1331 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1332 video_device_node_name(vdev));
1334 list_for_each(list,&tm6000_corelist) {
1335 h = list_entry(list, struct tm6000_core, tm6000_corelist);
1336 if (h->vfd->minor == minor) {
1338 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1345 /* If more than one user, mutex should be added */
1348 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1349 video_device_node_name(vdev), v4l2_type_names[type],
1352 /* allocate + initialize per filehandle data */
1353 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1359 file->private_data = fh;
1362 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1363 dev->fourcc = format[0].fourcc;
1365 fh->fmt = format_by_fourcc(dev->fourcc);
1367 tm6000_get_std_res (dev);
1369 fh->width = dev->width;
1370 fh->height = dev->height;
1372 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1373 "dev->vidq=0x%08lx\n",
1374 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1375 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1376 "queued=%d\n",list_empty(&dev->vidq.queued));
1377 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1378 "active=%d\n",list_empty(&dev->vidq.active));
1380 /* initialize hardware on analog mode */
1381 if (dev->mode!=TM6000_MODE_ANALOG) {
1382 rc=tm6000_init_analog_mode (dev);
1386 /* Put all controls at a sane state */
1387 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1388 qctl_regs[i] =tm6000_qctrl[i].default_value;
1390 dev->mode=TM6000_MODE_ANALOG;
1393 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1396 V4L2_FIELD_INTERLACED,
1397 sizeof(struct tm6000_buffer),fh);
1403 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1405 struct tm6000_fh *fh = file->private_data;
1407 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1408 if (res_locked(fh->dev))
1411 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1412 file->f_flags & O_NONBLOCK);
1418 tm6000_poll(struct file *file, struct poll_table_struct *wait)
1420 struct tm6000_fh *fh = file->private_data;
1421 struct tm6000_buffer *buf;
1423 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1426 if (res_get(fh->dev,fh)) {
1427 /* streaming capture */
1428 if (list_empty(&fh->vb_vidq.stream))
1430 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1432 /* read() capture */
1433 return videobuf_poll_stream(file, &fh->vb_vidq,
1436 poll_wait(file, &buf->vb.done, wait);
1437 if (buf->vb.state == VIDEOBUF_DONE ||
1438 buf->vb.state == VIDEOBUF_ERROR)
1439 return POLLIN|POLLRDNORM;
1443 static int tm6000_release(struct file *file)
1445 struct tm6000_fh *fh = file->private_data;
1446 struct tm6000_core *dev = fh->dev;
1447 struct video_device *vdev = video_devdata(file);
1449 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1450 video_device_node_name(vdev), dev->users);
1455 tm6000_uninit_isoc(dev);
1456 videobuf_mmap_free(&fh->vb_vidq);
1464 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1466 struct tm6000_fh *fh = file->private_data;
1469 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1474 static struct v4l2_file_operations tm6000_fops = {
1475 .owner = THIS_MODULE,
1476 .open = tm6000_open,
1477 .release = tm6000_release,
1478 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1479 .read = tm6000_read,
1480 .poll = tm6000_poll,
1481 .mmap = tm6000_mmap,
1484 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1485 .vidioc_querycap = vidioc_querycap,
1486 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1487 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1488 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1489 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1490 .vidioc_s_std = vidioc_s_std,
1491 .vidioc_enum_input = vidioc_enum_input,
1492 .vidioc_g_input = vidioc_g_input,
1493 .vidioc_s_input = vidioc_s_input,
1494 .vidioc_queryctrl = vidioc_queryctrl,
1495 .vidioc_g_ctrl = vidioc_g_ctrl,
1496 .vidioc_s_ctrl = vidioc_s_ctrl,
1497 .vidioc_g_tuner = vidioc_g_tuner,
1498 .vidioc_s_tuner = vidioc_s_tuner,
1499 .vidioc_g_frequency = vidioc_g_frequency,
1500 .vidioc_s_frequency = vidioc_s_frequency,
1501 .vidioc_streamon = vidioc_streamon,
1502 .vidioc_streamoff = vidioc_streamoff,
1503 .vidioc_reqbufs = vidioc_reqbufs,
1504 .vidioc_querybuf = vidioc_querybuf,
1505 .vidioc_qbuf = vidioc_qbuf,
1506 .vidioc_dqbuf = vidioc_dqbuf,
1507 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1508 .vidiocgmbuf = vidiocgmbuf,
1512 static struct video_device tm6000_template = {
1514 .fops = &tm6000_fops,
1515 .ioctl_ops = &video_ioctl_ops,
1516 .release = video_device_release,
1517 .tvnorms = TM6000_STD,
1518 .current_norm = V4L2_STD_NTSC_M,
1521 /* -----------------------------------------------------------------
1522 Initialization and module stuff
1523 ------------------------------------------------------------------*/
1525 int tm6000_v4l2_register(struct tm6000_core *dev)
1528 struct video_device *vfd;
1530 vfd = video_device_alloc();
1536 list_add_tail(&dev->tm6000_corelist,&tm6000_corelist);
1538 /* init video dma queues */
1539 INIT_LIST_HEAD(&dev->vidq.active);
1540 INIT_LIST_HEAD(&dev->vidq.queued);
1542 memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1543 dev->vfd->debug=tm6000_debug;
1544 vfd->v4l2_dev = &dev->v4l2_dev;
1546 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
1547 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1551 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1553 struct tm6000_core *h;
1554 struct list_head *pos, *tmp;
1556 video_unregister_device(dev->vfd);
1558 list_for_each_safe(pos, tmp, &tm6000_corelist) {
1559 h = list_entry(pos, struct tm6000_core, tm6000_corelist);
1568 int tm6000_v4l2_exit(void)
1573 module_param(video_nr, int, 0);
1574 MODULE_PARM_DESC(video_nr,"Allow changing video device number");
1576 module_param_named (debug, tm6000_debug, int, 0444);
1577 MODULE_PARM_DESC(debug,"activates debug info");
1579 module_param(vid_limit,int,0644);
1580 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");