2 tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
6 Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 - Fixed module load/unload
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation version 2
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
29 #include <linux/ioport.h>
30 #include <linux/init.h>
31 #include <linux/sched.h>
32 #include <linux/random.h>
33 #include <linux/version.h>
34 #include <linux/usb.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-ioctl.h>
37 #include <linux/interrupt.h>
38 #include <linux/kthread.h>
39 #include <linux/highmem.h>
40 #include <linux/freezer.h>
42 #include "tm6000-regs.h"
45 #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
47 /* Limits minimum and default number of buffers */
48 #define TM6000_MIN_BUF 4
49 #define TM6000_DEF_BUF 8
51 #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
53 /* Declare static vars that will be used as parameters */
54 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
55 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
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);
213 block = (header >> 7) & 0xf;
214 field = (header >> 11) & 0x1;
215 line = (header >> 12) & 0x1ff;
216 cmd = (header >> 21) & 0x7;
218 /* Validates header fields */
219 if(size > TM6000_URB_MSG_LEN)
220 size = TM6000_URB_MSG_LEN;
222 if (cmd == TM6000_URB_MSG_VIDEO) {
223 if ((block+1)*TM6000_URB_MSG_LEN>linewidth)
224 cmd = TM6000_URB_MSG_ERR;
226 /* FIXME: Mounts the image as field0+field1
227 * It should, instead, check if the user selected
228 * entrelaced or non-entrelaced mode
230 pos = ((line << 1) - field - 1) * linewidth +
231 block * TM6000_URB_MSG_LEN;
233 /* Don't allow to write out of the buffer */
234 if (pos+TM6000_URB_MSG_LEN > (*buf)->vb.size) {
235 dprintk(dev, V4L2_DEBUG_ISOC,
236 "ERR: size=%d, num=%d, line=%d, "
238 size, block, line, field);
240 cmd = TM6000_URB_MSG_ERR;
246 /* Prints debug info */
247 dprintk(dev, V4L2_DEBUG_ISOC, "size=%d, num=%d, "
248 " line=%d, field=%d\n",
249 size, block, line, field);
251 if ((last_line!=line)&&(last_line+1!=line) &&
252 (cmd != TM6000_URB_MSG_ERR) ) {
253 if (cmd != TM6000_URB_MSG_VIDEO) {
254 dprintk(dev, V4L2_DEBUG_ISOC, "cmd=%d, "
255 "size=%d, num=%d, line=%d, field=%d\n",
256 cmd, size, block, line, field);
259 start_line=last_line;
260 /* Prints debug info */
261 dprintk(dev, V4L2_DEBUG_ISOC, "lines= %d-%d, "
263 start_line, last_line, field);
265 if ((start_line<6 && last_line>200) &&
266 (last_field != field) ) {
268 dev->isoc_ctl.nfields++;
269 if (dev->isoc_ctl.nfields>=2) {
270 dev->isoc_ctl.nfields=0;
272 /* Announces that a new buffer were filled */
273 buffer_filled (dev, dma_q, *buf);
274 dprintk(dev, V4L2_DEBUG_ISOC,
275 "new buffer filled\n");
276 get_next_buf (dma_q, buf);
279 out_p = videobuf_to_vmalloc(&((*buf)->vb));
283 pos = dev->isoc_ctl.pos = 0;
290 if (cmd == TM6000_URB_MSG_VIDEO)
293 pktsize = TM6000_URB_MSG_LEN;
295 /* Continue the last copy */
296 cmd = dev->isoc_ctl.cmd;
297 size= dev->isoc_ctl.size;
298 pos = dev->isoc_ctl.pos;
299 pktsize = dev->isoc_ctl.pktsize;
302 cpysize = (endp-(*ptr) > size) ? size : endp - *ptr;
305 /* handles each different URB message */
307 case TM6000_URB_MSG_VIDEO:
308 /* Fills video buffer */
309 memcpy(&out_p[pos], *ptr, cpysize);
311 case TM6000_URB_MSG_PTS:
313 case TM6000_URB_MSG_AUDIO:
314 /* Need some code to process audio */
315 printk ("%ld: cmd=%s, size=%d\n", jiffies,
316 tm6000_msg_type[cmd],size);
318 case TM6000_URB_MSG_VBI:
321 dprintk (dev, V4L2_DEBUG_ISOC, "cmd=%s, size=%d\n",
322 tm6000_msg_type[cmd],size);
326 /* End of URB packet, but cmd processing is not
327 * complete. Preserve the state for a next packet
329 dev->isoc_ctl.pos = pos+cpysize;
330 dev->isoc_ctl.size= size-cpysize;
331 dev->isoc_ctl.cmd = cmd;
332 dev->isoc_ctl.pktsize = pktsize-cpysize;
335 dev->isoc_ctl.cmd = 0;
342 static int copy_streams(u8 *data, unsigned long len,
345 struct tm6000_dmaqueue *dma_q = urb->context;
346 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
347 u8 *ptr=data, *endp=data+len;
348 unsigned long header=0;
350 struct tm6000_buffer *buf;
353 get_next_buf(dma_q, &buf);
355 outp = videobuf_to_vmalloc(&buf->vb);
360 for (ptr=data; ptr<endp;) {
361 if (!dev->isoc_ctl.cmd) {
362 u8 *p=(u8 *)&dev->isoc_ctl.tmp_buf;
363 /* FIXME: This seems very complex
364 * It just recovers up to 3 bytes of the header that
365 * might be at the previous packet
367 if (dev->isoc_ctl.tmp_buf_len) {
368 while (dev->isoc_ctl.tmp_buf_len) {
369 if ( *(ptr+3-dev->isoc_ctl.tmp_buf_len) == 0x47) {
373 dev->isoc_ctl.tmp_buf_len--;
375 if (dev->isoc_ctl.tmp_buf_len) {
377 dev->isoc_ctl.tmp_buf_len);
378 memcpy((u8 *)&header +
379 dev->isoc_ctl.tmp_buf_len,
381 4 - dev->isoc_ctl.tmp_buf_len);
382 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
387 for (;ptr<endp-3;ptr++) {
393 dev->isoc_ctl.tmp_buf_len=endp-ptr;
394 memcpy (&dev->isoc_ctl.tmp_buf,ptr,
395 dev->isoc_ctl.tmp_buf_len);
400 /* Get message header */
401 header=*(unsigned long *)ptr;
405 /* Copy or continue last copy */
406 rc=copy_packet(urb,header,&ptr,endp,outp,&buf);
409 printk(KERN_ERR "tm6000: buffer underrun at %ld\n",
420 * Identify the tm5600/6000 buffer header type and properly handles
422 static int copy_multiplexed(u8 *ptr, unsigned long len,
425 struct tm6000_dmaqueue *dma_q = urb->context;
426 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
427 unsigned int pos=dev->isoc_ctl.pos,cpysize;
429 struct tm6000_buffer *buf;
432 get_next_buf(dma_q, &buf);
434 outp = videobuf_to_vmalloc(&buf->vb);
440 cpysize=min(len,buf->vb.size-pos);
441 //printk("Copying %d bytes (max=%lu) from %p to %p[%u]\n",cpysize,(*buf)->vb.size,ptr,out_p,pos);
442 memcpy(&outp[pos], ptr, cpysize);
446 if (pos >= buf->vb.size) {
448 /* Announces that a new buffer were filled */
449 buffer_filled (dev, dma_q, buf);
450 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
451 get_next_buf (dma_q, &buf);
454 outp = videobuf_to_vmalloc(&(buf->vb));
461 dev->isoc_ctl.pos=pos;
465 static void inline print_err_status (struct tm6000_core *dev,
466 int packet, int status)
468 char *errmsg = "Unknown";
472 errmsg = "unlinked synchronuously";
475 errmsg = "unlinked asynchronuously";
478 errmsg = "Buffer error (overrun)";
481 errmsg = "Stalled (device not responding)";
484 errmsg = "Babble (bad cable?)";
487 errmsg = "Bit-stuff error (bad cable?)";
490 errmsg = "CRC/Timeout (could be anything)";
493 errmsg = "Device does not respond";
497 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
500 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
501 packet, status, errmsg);
507 * Controls the isoc copy of each urb packet
509 static inline int tm6000_isoc_copy(struct urb *urb)
511 struct tm6000_dmaqueue *dma_q = urb->context;
512 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
513 struct tm6000_buffer *buf;
514 int i, len=0, rc=1, status;
517 if (urb->status < 0) {
518 print_err_status (dev, -1, urb->status);
522 for (i = 0; i < urb->number_of_packets; i++) {
523 status = urb->iso_frame_desc[i].status;
526 print_err_status (dev,i,status);
530 len = urb->iso_frame_desc[i].actual_length;
533 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
534 if (!urb->iso_frame_desc[i].status) {
535 if ((dev->fourcc)==V4L2_PIX_FMT_TM6000) {
536 rc=copy_multiplexed(p, len, urb);
540 copy_streams(p, len, urb);
548 /* ------------------------------------------------------------------
550 ------------------------------------------------------------------*/
553 * IRQ callback, called by URB callback
555 static void tm6000_irq_callback(struct urb *urb)
557 struct tm6000_dmaqueue *dma_q = urb->context;
558 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
564 spin_lock(&dev->slock);
565 tm6000_isoc_copy(urb);
566 spin_unlock(&dev->slock);
568 /* Reset urb buffers */
569 for (i = 0; i < urb->number_of_packets; i++) {
570 urb->iso_frame_desc[i].status = 0;
571 urb->iso_frame_desc[i].actual_length = 0;
574 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
576 tm6000_err("urb resubmit failed (error=%i)\n",
581 * Stop and Deallocate URBs
583 static void tm6000_uninit_isoc(struct tm6000_core *dev)
588 dev->isoc_ctl.nfields = -1;
589 dev->isoc_ctl.buf = NULL;
590 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
591 urb=dev->isoc_ctl.urb[i];
595 if (dev->isoc_ctl.transfer_buffer[i]) {
596 usb_free_coherent(dev->udev,
597 urb->transfer_buffer_length,
598 dev->isoc_ctl.transfer_buffer[i],
602 dev->isoc_ctl.urb[i] = NULL;
604 dev->isoc_ctl.transfer_buffer[i] = NULL;
607 kfree (dev->isoc_ctl.urb);
608 kfree (dev->isoc_ctl.transfer_buffer);
610 dev->isoc_ctl.urb=NULL;
611 dev->isoc_ctl.transfer_buffer=NULL;
612 dev->isoc_ctl.num_bufs = 0;
614 dev->isoc_ctl.num_bufs=0;
618 * Allocate URBs and start IRQ
620 static int tm6000_prepare_isoc(struct tm6000_core *dev, unsigned int framesize)
622 struct tm6000_dmaqueue *dma_q = &dev->vidq;
623 int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
626 /* De-allocates all pending stuff */
627 tm6000_uninit_isoc(dev);
629 usb_set_interface(dev->udev,
630 dev->isoc_in.bInterfaceNumber,
631 dev->isoc_in.bAlternateSetting);
633 pipe = usb_rcvisocpipe(dev->udev,
634 dev->isoc_in.endp->desc.bEndpointAddress &
635 USB_ENDPOINT_NUMBER_MASK);
637 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
639 if (size > dev->isoc_in.maxsize)
640 size = dev->isoc_in.maxsize;
642 dev->isoc_ctl.max_pkt_size = size;
644 max_packets = ( framesize + size - 1) / size;
646 if (max_packets > TM6000_MAX_ISO_PACKETS)
647 max_packets = TM6000_MAX_ISO_PACKETS;
649 sb_size = max_packets * size;
651 dev->isoc_ctl.num_bufs = num_bufs;
653 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
654 if (!dev->isoc_ctl.urb) {
655 tm6000_err("cannot alloc memory for usb buffers\n");
659 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
661 if (!dev->isoc_ctl.transfer_buffer) {
662 tm6000_err("cannot allocate memory for usbtransfer\n");
663 kfree(dev->isoc_ctl.urb);
667 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
668 " (%d bytes) of %d bytes each to handle %u size\n",
669 max_packets, num_bufs, sb_size,
670 dev->isoc_in.maxsize, size);
672 /* allocate urbs and transfer buffers */
673 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
674 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
676 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
677 tm6000_uninit_isoc(dev);
681 dev->isoc_ctl.urb[i] = urb;
683 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
684 sb_size, GFP_KERNEL, &urb->transfer_dma);
685 if (!dev->isoc_ctl.transfer_buffer[i]) {
686 tm6000_err ("unable to allocate %i bytes for transfer"
689 in_interrupt()?" while in int":"");
690 tm6000_uninit_isoc(dev);
693 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
695 usb_fill_bulk_urb(urb, dev->udev, pipe,
696 dev->isoc_ctl.transfer_buffer[i], sb_size,
697 tm6000_irq_callback, dma_q);
698 urb->interval = dev->isoc_in.endp->desc.bInterval;
699 urb->number_of_packets = max_packets;
700 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
702 for (j = 0; j < max_packets; j++) {
703 urb->iso_frame_desc[j].offset = size * j;
704 urb->iso_frame_desc[j].length = size;
711 static int tm6000_start_thread( struct tm6000_core *dev)
713 struct tm6000_dmaqueue *dma_q = &dev->vidq;
717 dma_q->ini_jiffies=jiffies;
719 init_waitqueue_head(&dma_q->wq);
721 /* submit urbs and enables IRQ */
722 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
723 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
725 tm6000_err("submit of urb %i failed (error=%i)\n", i,
727 tm6000_uninit_isoc(dev);
735 /* ------------------------------------------------------------------
737 ------------------------------------------------------------------*/
740 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
742 struct tm6000_fh *fh = vq->priv_data;
744 *size = fh->fmt->depth * fh->width * fh->height >> 3;
746 *count = TM6000_DEF_BUF;
748 if (*count < TM6000_MIN_BUF) {
749 *count=TM6000_MIN_BUF;
752 while (*size * *count > vid_limit * 1024 * 1024)
758 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
760 struct tm6000_fh *fh = vq->priv_data;
761 struct tm6000_core *dev = fh->dev;
767 /* We used to wait for the buffer to finish here, but this didn't work
768 because, as we were keeping the state as VIDEOBUF_QUEUED,
769 videobuf_queue_cancel marked it as finished for us.
770 (Also, it could wedge forever if the hardware was misconfigured.)
772 This should be safe; by the time we get here, the buffer isn't
773 queued anymore. If we ever start marking the buffers as
774 VIDEOBUF_ACTIVE, it won't be, though.
776 spin_lock_irqsave(&dev->slock, flags);
777 if (dev->isoc_ctl.buf == buf)
778 dev->isoc_ctl.buf = NULL;
779 spin_unlock_irqrestore(&dev->slock, flags);
781 videobuf_vmalloc_free(&buf->vb);
782 buf->vb.state = VIDEOBUF_NEEDS_INIT;
786 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
787 enum v4l2_field field)
789 struct tm6000_fh *fh = vq->priv_data;
790 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
791 struct tm6000_core *dev = fh->dev;
792 int rc = 0, urb_init = 0;
794 BUG_ON(NULL == fh->fmt);
797 /* FIXME: It assumes depth=2 */
798 /* The only currently supported format is 16 bits/pixel */
799 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
800 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
803 if (buf->fmt != fh->fmt ||
804 buf->vb.width != fh->width ||
805 buf->vb.height != fh->height ||
806 buf->vb.field != field) {
808 buf->vb.width = fh->width;
809 buf->vb.height = fh->height;
810 buf->vb.field = field;
811 buf->vb.state = VIDEOBUF_NEEDS_INIT;
814 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
815 if (0 != (rc = videobuf_iolock(vq, &buf->vb, NULL)))
820 if (!dev->isoc_ctl.num_bufs)
824 rc = tm6000_prepare_isoc(dev, buf->vb.size);
828 rc = tm6000_start_thread(dev);
834 buf->vb.state = VIDEOBUF_PREPARED;
838 free_buffer(vq, buf);
843 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
845 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
846 struct tm6000_fh *fh = vq->priv_data;
847 struct tm6000_core *dev = fh->dev;
848 struct tm6000_dmaqueue *vidq = &dev->vidq;
850 buf->vb.state = VIDEOBUF_QUEUED;
851 list_add_tail(&buf->vb.queue, &vidq->active);
854 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
856 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
861 static struct videobuf_queue_ops tm6000_video_qops = {
862 .buf_setup = buffer_setup,
863 .buf_prepare = buffer_prepare,
864 .buf_queue = buffer_queue,
865 .buf_release = buffer_release,
868 /* ------------------------------------------------------------------
870 ------------------------------------------------------------------*/
872 static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh)
875 mutex_lock(&dev->lock);
876 if (dev->resources) {
877 /* no, someone else uses it */
878 mutex_unlock(&dev->lock);
881 /* it's free, grab it */
883 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
884 mutex_unlock(&dev->lock);
888 static int res_locked(struct tm6000_core *dev)
890 return (dev->resources);
893 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
895 mutex_lock(&dev->lock);
897 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
898 mutex_unlock(&dev->lock);
901 /* ------------------------------------------------------------------
902 IOCTL vidioc handling
903 ------------------------------------------------------------------*/
904 static int vidioc_querycap (struct file *file, void *priv,
905 struct v4l2_capability *cap)
907 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
909 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
910 strlcpy(cap->card,"Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
911 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
912 cap->version = TM6000_VERSION;
913 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
920 static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
921 struct v4l2_fmtdesc *f)
923 if (unlikely(f->index >= ARRAY_SIZE(format)))
926 strlcpy(f->description,format[f->index].name,sizeof(f->description));
927 f->pixelformat = format[f->index].fourcc;
931 static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
932 struct v4l2_format *f)
934 struct tm6000_fh *fh=priv;
936 f->fmt.pix.width = fh->width;
937 f->fmt.pix.height = fh->height;
938 f->fmt.pix.field = fh->vb_vidq.field;
939 f->fmt.pix.pixelformat = fh->fmt->fourcc;
940 f->fmt.pix.bytesperline =
941 (f->fmt.pix.width * fh->fmt->depth) >> 3;
942 f->fmt.pix.sizeimage =
943 f->fmt.pix.height * f->fmt.pix.bytesperline;
948 static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc)
952 for (i = 0; i < ARRAY_SIZE(format); i++)
953 if (format[i].fourcc == fourcc)
958 static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
959 struct v4l2_format *f)
961 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
962 struct tm6000_fmt *fmt;
963 enum v4l2_field field;
965 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
967 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
968 " invalid.\n", f->fmt.pix.pixelformat);
972 field = f->fmt.pix.field;
974 if (field == V4L2_FIELD_ANY) {
975 // field=V4L2_FIELD_INTERLACED;
976 field=V4L2_FIELD_SEQ_TB;
977 } else if (V4L2_FIELD_INTERLACED != field) {
978 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
982 tm6000_get_std_res (dev);
984 f->fmt.pix.width = dev->width;
985 f->fmt.pix.height = dev->height;
987 f->fmt.pix.width &= ~0x01;
989 f->fmt.pix.field = field;
991 f->fmt.pix.bytesperline =
992 (f->fmt.pix.width * fmt->depth) >> 3;
993 f->fmt.pix.sizeimage =
994 f->fmt.pix.height * f->fmt.pix.bytesperline;
999 /*FIXME: This seems to be generic enough to be at videodev2 */
1000 static int vidioc_s_fmt_vid_cap (struct file *file, void *priv,
1001 struct v4l2_format *f)
1003 struct tm6000_fh *fh=priv;
1004 struct tm6000_core *dev = fh->dev;
1005 int ret = vidioc_try_fmt_vid_cap(file,fh,f);
1009 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1010 fh->width = f->fmt.pix.width;
1011 fh->height = f->fmt.pix.height;
1012 fh->vb_vidq.field = f->fmt.pix.field;
1015 dev->fourcc = f->fmt.pix.pixelformat;
1017 tm6000_set_fourcc_format(dev);
1022 static int vidioc_reqbufs (struct file *file, void *priv,
1023 struct v4l2_requestbuffers *p)
1025 struct tm6000_fh *fh=priv;
1027 return (videobuf_reqbufs(&fh->vb_vidq, p));
1030 static int vidioc_querybuf (struct file *file, void *priv,
1031 struct v4l2_buffer *p)
1033 struct tm6000_fh *fh=priv;
1035 return (videobuf_querybuf(&fh->vb_vidq, p));
1038 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1040 struct tm6000_fh *fh=priv;
1042 return (videobuf_qbuf(&fh->vb_vidq, p));
1045 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1047 struct tm6000_fh *fh=priv;
1049 return (videobuf_dqbuf(&fh->vb_vidq, p,
1050 file->f_flags & O_NONBLOCK));
1053 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1054 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1056 struct tm6000_fh *fh=priv;
1058 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1062 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1064 struct tm6000_fh *fh=priv;
1065 struct tm6000_core *dev = fh->dev;
1067 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1072 if (!res_get(dev,fh))
1074 return (videobuf_streamon(&fh->vb_vidq));
1077 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1079 struct tm6000_fh *fh=priv;
1080 struct tm6000_core *dev = fh->dev;
1082 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1087 videobuf_streamoff(&fh->vb_vidq);
1093 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1096 struct tm6000_fh *fh=priv;
1097 struct tm6000_core *dev = fh->dev;
1099 rc=tm6000_set_standard (dev, norm);
1101 fh->width = dev->width;
1102 fh->height = dev->height;
1107 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1112 static int vidioc_enum_input (struct file *file, void *priv,
1113 struct v4l2_input *inp)
1115 switch (inp->index) {
1116 case TM6000_INPUT_TV:
1117 inp->type = V4L2_INPUT_TYPE_TUNER;
1118 strcpy(inp->name,"Television");
1120 case TM6000_INPUT_COMPOSITE:
1121 inp->type = V4L2_INPUT_TYPE_CAMERA;
1122 strcpy(inp->name,"Composite");
1124 case TM6000_INPUT_SVIDEO:
1125 inp->type = V4L2_INPUT_TYPE_CAMERA;
1126 strcpy(inp->name,"S-Video");
1131 inp->std = TM6000_STD;
1136 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1138 struct tm6000_fh *fh=priv;
1139 struct tm6000_core *dev = fh->dev;
1145 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1147 struct tm6000_fh *fh=priv;
1148 struct tm6000_core *dev = fh->dev;
1153 case TM6000_INPUT_TV:
1157 case TM6000_INPUT_COMPOSITE:
1158 case TM6000_INPUT_SVIDEO:
1165 rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1166 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1170 rc=vidioc_s_std (file, priv, &dev->vfd->current_norm);
1176 /* --- controls ---------------------------------------------- */
1177 static int vidioc_queryctrl (struct file *file, void *priv,
1178 struct v4l2_queryctrl *qc)
1182 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1183 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1184 memcpy(qc, &(tm6000_qctrl[i]),
1192 static int vidioc_g_ctrl (struct file *file, void *priv,
1193 struct v4l2_control *ctrl)
1195 struct tm6000_fh *fh=priv;
1196 struct tm6000_core *dev = fh->dev;
1199 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1201 case V4L2_CID_CONTRAST:
1202 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
1204 case V4L2_CID_BRIGHTNESS:
1205 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
1207 case V4L2_CID_SATURATION:
1208 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
1211 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
1224 static int vidioc_s_ctrl (struct file *file, void *priv,
1225 struct v4l2_control *ctrl)
1227 struct tm6000_fh *fh =priv;
1228 struct tm6000_core *dev = fh->dev;
1232 case V4L2_CID_CONTRAST:
1233 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1235 case V4L2_CID_BRIGHTNESS:
1236 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1238 case V4L2_CID_SATURATION:
1239 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1242 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1248 static int vidioc_g_tuner (struct file *file, void *priv,
1249 struct v4l2_tuner *t)
1251 struct tm6000_fh *fh =priv;
1252 struct tm6000_core *dev = fh->dev;
1254 if (unlikely(UNSET == dev->tuner_type))
1259 strcpy(t->name, "Television");
1260 t->type = V4L2_TUNER_ANALOG_TV;
1261 t->capability = V4L2_TUNER_CAP_NORM;
1262 t->rangehigh = 0xffffffffUL;
1263 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1268 static int vidioc_s_tuner (struct file *file, void *priv,
1269 struct v4l2_tuner *t)
1271 struct tm6000_fh *fh =priv;
1272 struct tm6000_core *dev = fh->dev;
1274 if (UNSET == dev->tuner_type)
1282 static int vidioc_g_frequency (struct file *file, void *priv,
1283 struct v4l2_frequency *f)
1285 struct tm6000_fh *fh =priv;
1286 struct tm6000_core *dev = fh->dev;
1288 if (unlikely(UNSET == dev->tuner_type))
1291 f->type = V4L2_TUNER_ANALOG_TV;
1292 f->frequency = dev->freq;
1294 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1299 static int vidioc_s_frequency (struct file *file, void *priv,
1300 struct v4l2_frequency *f)
1302 struct tm6000_fh *fh =priv;
1303 struct tm6000_core *dev = fh->dev;
1305 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1308 if (unlikely(UNSET == dev->tuner_type))
1310 if (unlikely(f->tuner != 0))
1313 // mutex_lock(&dev->lock);
1314 dev->freq = f->frequency;
1315 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1316 // mutex_unlock(&dev->lock);
1321 /* ------------------------------------------------------------------
1322 File operations for the device
1323 ------------------------------------------------------------------*/
1325 static int tm6000_open(struct file *file)
1327 struct video_device *vdev = video_devdata(file);
1328 struct tm6000_core *dev = video_drvdata(file);
1329 struct tm6000_fh *fh;
1330 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1333 printk(KERN_INFO "tm6000: open called (dev=%s)\n",
1334 video_device_node_name(vdev));
1336 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1337 video_device_node_name(vdev));
1340 /* If more than one user, mutex should be added */
1343 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1344 video_device_node_name(vdev), v4l2_type_names[type],
1347 /* allocate + initialize per filehandle data */
1348 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1354 file->private_data = fh;
1357 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1358 dev->fourcc = format[0].fourcc;
1360 fh->fmt = format_by_fourcc(dev->fourcc);
1362 tm6000_get_std_res (dev);
1364 fh->width = dev->width;
1365 fh->height = dev->height;
1367 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1368 "dev->vidq=0x%08lx\n",
1369 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1370 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1371 "queued=%d\n",list_empty(&dev->vidq.queued));
1372 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1373 "active=%d\n",list_empty(&dev->vidq.active));
1375 /* initialize hardware on analog mode */
1376 if (dev->mode!=TM6000_MODE_ANALOG) {
1377 rc=tm6000_init_analog_mode (dev);
1381 /* Put all controls at a sane state */
1382 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1383 qctl_regs[i] =tm6000_qctrl[i].default_value;
1385 dev->mode=TM6000_MODE_ANALOG;
1388 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1391 V4L2_FIELD_INTERLACED,
1392 sizeof(struct tm6000_buffer),fh);
1398 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1400 struct tm6000_fh *fh = file->private_data;
1402 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1403 if (res_locked(fh->dev))
1406 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1407 file->f_flags & O_NONBLOCK);
1413 tm6000_poll(struct file *file, struct poll_table_struct *wait)
1415 struct tm6000_fh *fh = file->private_data;
1416 struct tm6000_buffer *buf;
1418 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1421 if (res_get(fh->dev,fh)) {
1422 /* streaming capture */
1423 if (list_empty(&fh->vb_vidq.stream))
1425 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1427 /* read() capture */
1428 return videobuf_poll_stream(file, &fh->vb_vidq,
1431 poll_wait(file, &buf->vb.done, wait);
1432 if (buf->vb.state == VIDEOBUF_DONE ||
1433 buf->vb.state == VIDEOBUF_ERROR)
1434 return POLLIN|POLLRDNORM;
1438 static int tm6000_release(struct file *file)
1440 struct tm6000_fh *fh = file->private_data;
1441 struct tm6000_core *dev = fh->dev;
1442 struct video_device *vdev = video_devdata(file);
1444 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1445 video_device_node_name(vdev), dev->users);
1450 tm6000_uninit_isoc(dev);
1451 videobuf_mmap_free(&fh->vb_vidq);
1459 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1461 struct tm6000_fh *fh = file->private_data;
1464 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1469 static struct v4l2_file_operations tm6000_fops = {
1470 .owner = THIS_MODULE,
1471 .open = tm6000_open,
1472 .release = tm6000_release,
1473 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1474 .read = tm6000_read,
1475 .poll = tm6000_poll,
1476 .mmap = tm6000_mmap,
1479 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1480 .vidioc_querycap = vidioc_querycap,
1481 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1482 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1483 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1484 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1485 .vidioc_s_std = vidioc_s_std,
1486 .vidioc_enum_input = vidioc_enum_input,
1487 .vidioc_g_input = vidioc_g_input,
1488 .vidioc_s_input = vidioc_s_input,
1489 .vidioc_queryctrl = vidioc_queryctrl,
1490 .vidioc_g_ctrl = vidioc_g_ctrl,
1491 .vidioc_s_ctrl = vidioc_s_ctrl,
1492 .vidioc_g_tuner = vidioc_g_tuner,
1493 .vidioc_s_tuner = vidioc_s_tuner,
1494 .vidioc_g_frequency = vidioc_g_frequency,
1495 .vidioc_s_frequency = vidioc_s_frequency,
1496 .vidioc_streamon = vidioc_streamon,
1497 .vidioc_streamoff = vidioc_streamoff,
1498 .vidioc_reqbufs = vidioc_reqbufs,
1499 .vidioc_querybuf = vidioc_querybuf,
1500 .vidioc_qbuf = vidioc_qbuf,
1501 .vidioc_dqbuf = vidioc_dqbuf,
1502 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1503 .vidiocgmbuf = vidiocgmbuf,
1507 static struct video_device tm6000_template = {
1509 .fops = &tm6000_fops,
1510 .ioctl_ops = &video_ioctl_ops,
1511 .release = video_device_release,
1512 .tvnorms = TM6000_STD,
1513 .current_norm = V4L2_STD_NTSC_M,
1516 /* -----------------------------------------------------------------
1517 Initialization and module stuff
1518 ------------------------------------------------------------------*/
1520 int tm6000_v4l2_register(struct tm6000_core *dev)
1523 struct video_device *vfd;
1525 vfd = video_device_alloc();
1531 /* init video dma queues */
1532 INIT_LIST_HEAD(&dev->vidq.active);
1533 INIT_LIST_HEAD(&dev->vidq.queued);
1535 memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1536 dev->vfd->debug=tm6000_debug;
1537 vfd->v4l2_dev = &dev->v4l2_dev;
1538 video_set_drvdata(vfd, dev);
1540 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
1541 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1545 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1547 video_unregister_device(dev->vfd);
1552 int tm6000_v4l2_exit(void)
1557 module_param(video_nr, int, 0);
1558 MODULE_PARM_DESC(video_nr,"Allow changing video device number");
1560 module_param_named (debug, tm6000_debug, int, 0444);
1561 MODULE_PARM_DESC(debug,"activates debug info");
1563 module_param(vid_limit,int,0644);
1564 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");