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 */
59 EXPORT_SYMBOL_GPL(tm6000_debug);
61 /* supported controls */
62 static struct v4l2_queryctrl tm6000_qctrl[] = {
64 .id = V4L2_CID_BRIGHTNESS,
65 .type = V4L2_CTRL_TYPE_INTEGER,
73 .id = V4L2_CID_CONTRAST,
74 .type = V4L2_CTRL_TYPE_INTEGER,
82 .id = V4L2_CID_SATURATION,
83 .type = V4L2_CTRL_TYPE_INTEGER,
92 .type = V4L2_CTRL_TYPE_INTEGER,
102 static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
104 static struct tm6000_fmt format[] = {
106 .name = "4:2:2, packed, YVY2",
107 .fourcc = V4L2_PIX_FMT_YUYV,
110 .name = "4:2:2, packed, UYVY",
111 .fourcc = V4L2_PIX_FMT_UYVY,
114 .name = "A/V + VBI mux packet",
115 .fourcc = V4L2_PIX_FMT_TM6000,
120 /* ------------------------------------------------------------------
121 * DMA and thread functions
122 * ------------------------------------------------------------------
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);
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_streams(u8 *data, unsigned long len,
192 struct tm6000_dmaqueue *dma_q = urb->context;
193 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
194 u8 *ptr = data, *endp = data+len, c;
195 unsigned long header = 0;
197 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
198 struct tm6000_buffer *vbuf;
200 unsigned int linewidth;
202 /* get video buffer */
203 get_next_buf(dma_q, &vbuf);
206 voutp = videobuf_to_vmalloc(&vbuf->vb);
210 for (ptr = data; ptr < endp;) {
211 if (!dev->isoc_ctl.cmd) {
213 if (dev->isoc_ctl.tmp_buf_len > 0) {
214 /* from last urb or packet */
215 header = dev->isoc_ctl.tmp_buf;
216 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
217 memcpy((u8 *)&header +
218 dev->isoc_ctl.tmp_buf_len,
220 4 - dev->isoc_ctl.tmp_buf_len);
221 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
223 dev->isoc_ctl.tmp_buf_len = 0;
225 if (ptr + 3 >= endp) {
226 /* have incomplete header */
227 dev->isoc_ctl.tmp_buf_len = endp - ptr;
228 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
229 dev->isoc_ctl.tmp_buf_len);
233 for (; ptr < endp - 3; ptr++) {
234 if (*(ptr + 3) == 0x47)
237 /* Get message header */
238 header = *(unsigned long *)ptr;
242 /* split the header fields */
243 c = (header >> 24) & 0xff;
244 size = ((header & 0x7e) << 1);
247 block = (header >> 7) & 0xf;
248 field = (header >> 11) & 0x1;
249 line = (header >> 12) & 0x1ff;
250 cmd = (header >> 21) & 0x7;
251 /* Validates haeder fields */
252 if (size > TM6000_URB_MSG_LEN)
253 size = TM6000_URB_MSG_LEN;
254 pktsize = TM6000_URB_MSG_LEN;
255 /* calculate position in buffer
256 * and change the buffer
259 case TM6000_URB_MSG_VIDEO:
260 if ((dev->isoc_ctl.vfield != field) &&
262 /* Announces that a new buffer
265 buffer_filled(dev, dma_q, vbuf);
266 dprintk(dev, V4L2_DEBUG_ISOC,
267 "new buffer filled\n");
268 get_next_buf(dma_q, &vbuf);
271 voutp = videobuf_to_vmalloc(&vbuf->vb);
274 memset(voutp, 0, vbuf->vb.size);
276 linewidth = vbuf->vb.width << 1;
277 pos = ((line << 1) - field - 1) * linewidth +
278 block * TM6000_URB_MSG_LEN;
279 /* Don't allow to write out of the buffer */
280 if (pos + size > vbuf->vb.size)
281 cmd = TM6000_URB_MSG_ERR;
282 dev->isoc_ctl.vfield = field;
284 case TM6000_URB_MSG_VBI:
286 case TM6000_URB_MSG_AUDIO:
287 case TM6000_URB_MSG_PTS:
288 size = pktsize; /* Size is always 180 bytes */
292 /* Continue the last copy */
293 cmd = dev->isoc_ctl.cmd;
294 size = dev->isoc_ctl.size;
295 pos = dev->isoc_ctl.pos;
296 pktsize = dev->isoc_ctl.pktsize;
298 cpysize = (endp - ptr > size) ? size : endp - ptr;
300 /* copy data in different buffers */
302 case TM6000_URB_MSG_VIDEO:
303 /* Fills video buffer */
305 memcpy(&voutp[pos], ptr, cpysize);
307 case TM6000_URB_MSG_AUDIO:
308 /* Need some code to copy audio buffer */
309 if (dev->fourcc == V4L2_PIX_FMT_YUYV) {
310 /* Swap word bytes */
313 for (i = 0; i < cpysize; i += 2)
314 swab16s((u16 *)(ptr + i));
316 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
318 case TM6000_URB_MSG_VBI:
319 /* Need some code to copy vbi buffer */
321 case TM6000_URB_MSG_PTS:
322 /* Need some code to copy pts */
326 if (ptr + pktsize > endp) {
327 /* End of URB packet, but cmd processing is not
328 * complete. Preserve the state for a next packet
330 dev->isoc_ctl.pos = pos + cpysize;
331 dev->isoc_ctl.size = size - cpysize;
332 dev->isoc_ctl.cmd = cmd;
333 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
336 dev->isoc_ctl.cmd = 0;
344 * Identify the tm5600/6000 buffer header type and properly handles
346 static int copy_multiplexed(u8 *ptr, unsigned long len,
349 struct tm6000_dmaqueue *dma_q = urb->context;
350 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
351 unsigned int pos = dev->isoc_ctl.pos, cpysize;
353 struct tm6000_buffer *buf;
356 get_next_buf(dma_q, &buf);
358 outp = videobuf_to_vmalloc(&buf->vb);
364 cpysize = min(len, buf->vb.size-pos);
365 memcpy(&outp[pos], ptr, cpysize);
369 if (pos >= buf->vb.size) {
371 /* Announces that a new buffer were filled */
372 buffer_filled(dev, dma_q, buf);
373 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
374 get_next_buf(dma_q, &buf);
377 outp = videobuf_to_vmalloc(&(buf->vb));
384 dev->isoc_ctl.pos = pos;
388 static inline void print_err_status(struct tm6000_core *dev,
389 int packet, int status)
391 char *errmsg = "Unknown";
395 errmsg = "unlinked synchronuously";
398 errmsg = "unlinked asynchronuously";
401 errmsg = "Buffer error (overrun)";
404 errmsg = "Stalled (device not responding)";
407 errmsg = "Babble (bad cable?)";
410 errmsg = "Bit-stuff error (bad cable?)";
413 errmsg = "CRC/Timeout (could be anything)";
416 errmsg = "Device does not respond";
420 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
423 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
424 packet, status, errmsg);
430 * Controls the isoc copy of each urb packet
432 static inline int tm6000_isoc_copy(struct urb *urb)
434 struct tm6000_dmaqueue *dma_q = urb->context;
435 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
436 int i, len = 0, rc = 1, status;
439 if (urb->status < 0) {
440 print_err_status(dev, -1, urb->status);
444 for (i = 0; i < urb->number_of_packets; i++) {
445 status = urb->iso_frame_desc[i].status;
448 print_err_status(dev, i, status);
452 len = urb->iso_frame_desc[i].actual_length;
455 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
456 if (!urb->iso_frame_desc[i].status) {
457 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
458 rc = copy_multiplexed(p, len, urb);
462 copy_streams(p, len, urb);
470 /* ------------------------------------------------------------------
472 * ------------------------------------------------------------------
476 * IRQ callback, called by URB callback
478 static void tm6000_irq_callback(struct urb *urb)
480 struct tm6000_dmaqueue *dma_q = urb->context;
481 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
487 spin_lock(&dev->slock);
488 tm6000_isoc_copy(urb);
489 spin_unlock(&dev->slock);
491 /* Reset urb buffers */
492 for (i = 0; i < urb->number_of_packets; i++) {
493 urb->iso_frame_desc[i].status = 0;
494 urb->iso_frame_desc[i].actual_length = 0;
497 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
499 tm6000_err("urb resubmit failed (error=%i)\n",
504 * Stop and Deallocate URBs
506 static void tm6000_uninit_isoc(struct tm6000_core *dev)
511 dev->isoc_ctl.buf = NULL;
512 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
513 urb = dev->isoc_ctl.urb[i];
517 if (dev->isoc_ctl.transfer_buffer[i]) {
518 usb_free_coherent(dev->udev,
519 urb->transfer_buffer_length,
520 dev->isoc_ctl.transfer_buffer[i],
524 dev->isoc_ctl.urb[i] = NULL;
526 dev->isoc_ctl.transfer_buffer[i] = NULL;
529 kfree(dev->isoc_ctl.urb);
530 kfree(dev->isoc_ctl.transfer_buffer);
532 dev->isoc_ctl.urb = NULL;
533 dev->isoc_ctl.transfer_buffer = NULL;
534 dev->isoc_ctl.num_bufs = 0;
538 * Allocate URBs and start IRQ
540 static int tm6000_prepare_isoc(struct tm6000_core *dev, unsigned int framesize)
542 struct tm6000_dmaqueue *dma_q = &dev->vidq;
543 int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
546 /* De-allocates all pending stuff */
547 tm6000_uninit_isoc(dev);
548 /* Stop interrupt USB pipe */
549 tm6000_ir_int_stop(dev);
551 usb_set_interface(dev->udev,
552 dev->isoc_in.bInterfaceNumber,
553 dev->isoc_in.bAlternateSetting);
555 /* Start interrupt USB pipe */
556 tm6000_ir_int_start(dev);
558 pipe = usb_rcvisocpipe(dev->udev,
559 dev->isoc_in.endp->desc.bEndpointAddress &
560 USB_ENDPOINT_NUMBER_MASK);
562 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
564 if (size > dev->isoc_in.maxsize)
565 size = dev->isoc_in.maxsize;
567 dev->isoc_ctl.max_pkt_size = size;
569 max_packets = (framesize + size - 1) / size;
571 if (max_packets > TM6000_MAX_ISO_PACKETS)
572 max_packets = TM6000_MAX_ISO_PACKETS;
574 sb_size = max_packets * size;
576 dev->isoc_ctl.num_bufs = num_bufs;
578 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
579 if (!dev->isoc_ctl.urb) {
580 tm6000_err("cannot alloc memory for usb buffers\n");
584 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
586 if (!dev->isoc_ctl.transfer_buffer) {
587 tm6000_err("cannot allocate memory for usbtransfer\n");
588 kfree(dev->isoc_ctl.urb);
592 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
593 " (%d bytes) of %d bytes each to handle %u size\n",
594 max_packets, num_bufs, sb_size,
595 dev->isoc_in.maxsize, size);
597 /* allocate urbs and transfer buffers */
598 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
599 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
601 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
602 tm6000_uninit_isoc(dev);
606 dev->isoc_ctl.urb[i] = urb;
608 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
609 sb_size, GFP_KERNEL, &urb->transfer_dma);
610 if (!dev->isoc_ctl.transfer_buffer[i]) {
611 tm6000_err("unable to allocate %i bytes for transfer"
614 in_interrupt() ? " while in int" : "");
615 tm6000_uninit_isoc(dev);
618 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
620 usb_fill_bulk_urb(urb, dev->udev, pipe,
621 dev->isoc_ctl.transfer_buffer[i], sb_size,
622 tm6000_irq_callback, dma_q);
623 urb->interval = dev->isoc_in.endp->desc.bInterval;
624 urb->number_of_packets = max_packets;
625 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
627 for (j = 0; j < max_packets; j++) {
628 urb->iso_frame_desc[j].offset = size * j;
629 urb->iso_frame_desc[j].length = size;
636 static int tm6000_start_thread(struct tm6000_core *dev)
638 struct tm6000_dmaqueue *dma_q = &dev->vidq;
642 dma_q->ini_jiffies = jiffies;
644 init_waitqueue_head(&dma_q->wq);
646 /* submit urbs and enables IRQ */
647 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
648 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
650 tm6000_err("submit of urb %i failed (error=%i)\n", i,
652 tm6000_uninit_isoc(dev);
660 /* ------------------------------------------------------------------
661 * Videobuf operations
662 * ------------------------------------------------------------------
666 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
668 struct tm6000_fh *fh = vq->priv_data;
670 *size = fh->fmt->depth * fh->width * fh->height >> 3;
672 *count = TM6000_DEF_BUF;
674 if (*count < TM6000_MIN_BUF)
675 *count = TM6000_MIN_BUF;
677 while (*size * *count > vid_limit * 1024 * 1024)
683 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
685 struct tm6000_fh *fh = vq->priv_data;
686 struct tm6000_core *dev = fh->dev;
692 /* We used to wait for the buffer to finish here, but this didn't work
693 because, as we were keeping the state as VIDEOBUF_QUEUED,
694 videobuf_queue_cancel marked it as finished for us.
695 (Also, it could wedge forever if the hardware was misconfigured.)
697 This should be safe; by the time we get here, the buffer isn't
698 queued anymore. If we ever start marking the buffers as
699 VIDEOBUF_ACTIVE, it won't be, though.
701 spin_lock_irqsave(&dev->slock, flags);
702 if (dev->isoc_ctl.buf == buf)
703 dev->isoc_ctl.buf = NULL;
704 spin_unlock_irqrestore(&dev->slock, flags);
706 videobuf_vmalloc_free(&buf->vb);
707 buf->vb.state = VIDEOBUF_NEEDS_INIT;
711 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
712 enum v4l2_field field)
714 struct tm6000_fh *fh = vq->priv_data;
715 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
716 struct tm6000_core *dev = fh->dev;
717 int rc = 0, urb_init = 0;
719 BUG_ON(NULL == fh->fmt);
722 /* FIXME: It assumes depth=2 */
723 /* The only currently supported format is 16 bits/pixel */
724 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
725 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
728 if (buf->fmt != fh->fmt ||
729 buf->vb.width != fh->width ||
730 buf->vb.height != fh->height ||
731 buf->vb.field != field) {
733 buf->vb.width = fh->width;
734 buf->vb.height = fh->height;
735 buf->vb.field = field;
736 buf->vb.state = VIDEOBUF_NEEDS_INIT;
739 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
740 if (0 != (rc = videobuf_iolock(vq, &buf->vb, NULL)))
745 if (!dev->isoc_ctl.num_bufs)
749 rc = tm6000_prepare_isoc(dev, buf->vb.size);
753 rc = tm6000_start_thread(dev);
759 buf->vb.state = VIDEOBUF_PREPARED;
763 free_buffer(vq, buf);
768 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
770 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
771 struct tm6000_fh *fh = vq->priv_data;
772 struct tm6000_core *dev = fh->dev;
773 struct tm6000_dmaqueue *vidq = &dev->vidq;
775 buf->vb.state = VIDEOBUF_QUEUED;
776 list_add_tail(&buf->vb.queue, &vidq->active);
779 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
781 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
783 free_buffer(vq, buf);
786 static struct videobuf_queue_ops tm6000_video_qops = {
787 .buf_setup = buffer_setup,
788 .buf_prepare = buffer_prepare,
789 .buf_queue = buffer_queue,
790 .buf_release = buffer_release,
793 /* ------------------------------------------------------------------
795 * ------------------------------------------------------------------
798 static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
800 /* Is the current fh handling it? if so, that's OK */
801 if (dev->resources == fh && dev->is_res_read)
807 static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
809 /* Is the current fh handling it? if so, that's OK */
810 if (dev->resources == fh)
816 static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
819 /* Is the current fh handling it? if so, that's OK */
820 if (dev->resources == fh && dev->is_res_read == is_res_read)
829 dev->is_res_read = is_res_read;
830 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
834 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
836 /* Is the current fh handling it? if so, that's OK */
837 if (dev->resources != fh)
840 dev->resources = NULL;
841 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
844 /* ------------------------------------------------------------------
845 * IOCTL vidioc handling
846 * ------------------------------------------------------------------
848 static int vidioc_querycap(struct file *file, void *priv,
849 struct v4l2_capability *cap)
852 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
853 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
854 cap->version = TM6000_VERSION;
855 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
862 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
863 struct v4l2_fmtdesc *f)
865 if (unlikely(f->index >= ARRAY_SIZE(format)))
868 strlcpy(f->description, format[f->index].name, sizeof(f->description));
869 f->pixelformat = format[f->index].fourcc;
873 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
874 struct v4l2_format *f)
876 struct tm6000_fh *fh = priv;
878 f->fmt.pix.width = fh->width;
879 f->fmt.pix.height = fh->height;
880 f->fmt.pix.field = fh->vb_vidq.field;
881 f->fmt.pix.pixelformat = fh->fmt->fourcc;
882 f->fmt.pix.bytesperline =
883 (f->fmt.pix.width * fh->fmt->depth) >> 3;
884 f->fmt.pix.sizeimage =
885 f->fmt.pix.height * f->fmt.pix.bytesperline;
890 static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
894 for (i = 0; i < ARRAY_SIZE(format); i++)
895 if (format[i].fourcc == fourcc)
900 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
901 struct v4l2_format *f)
903 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
904 struct tm6000_fmt *fmt;
905 enum v4l2_field field;
907 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
909 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
910 " invalid.\n", f->fmt.pix.pixelformat);
914 field = f->fmt.pix.field;
916 if (field == V4L2_FIELD_ANY)
917 field = V4L2_FIELD_SEQ_TB;
918 else if (V4L2_FIELD_INTERLACED != field) {
919 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
923 tm6000_get_std_res(dev);
925 f->fmt.pix.width = dev->width;
926 f->fmt.pix.height = dev->height;
928 f->fmt.pix.width &= ~0x01;
930 f->fmt.pix.field = field;
932 f->fmt.pix.bytesperline =
933 (f->fmt.pix.width * fmt->depth) >> 3;
934 f->fmt.pix.sizeimage =
935 f->fmt.pix.height * f->fmt.pix.bytesperline;
940 /*FIXME: This seems to be generic enough to be at videodev2 */
941 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
942 struct v4l2_format *f)
944 struct tm6000_fh *fh = priv;
945 struct tm6000_core *dev = fh->dev;
946 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
950 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
951 fh->width = f->fmt.pix.width;
952 fh->height = f->fmt.pix.height;
953 fh->vb_vidq.field = f->fmt.pix.field;
956 dev->fourcc = f->fmt.pix.pixelformat;
958 tm6000_set_fourcc_format(dev);
963 static int vidioc_reqbufs(struct file *file, void *priv,
964 struct v4l2_requestbuffers *p)
966 struct tm6000_fh *fh = priv;
968 return videobuf_reqbufs(&fh->vb_vidq, p);
971 static int vidioc_querybuf(struct file *file, void *priv,
972 struct v4l2_buffer *p)
974 struct tm6000_fh *fh = priv;
976 return videobuf_querybuf(&fh->vb_vidq, p);
979 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
981 struct tm6000_fh *fh = priv;
983 return videobuf_qbuf(&fh->vb_vidq, p);
986 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
988 struct tm6000_fh *fh = priv;
990 return videobuf_dqbuf(&fh->vb_vidq, p,
991 file->f_flags & O_NONBLOCK);
994 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
996 struct tm6000_fh *fh = priv;
997 struct tm6000_core *dev = fh->dev;
999 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1004 if (!res_get(dev, fh, false))
1006 return (videobuf_streamon(&fh->vb_vidq));
1009 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1011 struct tm6000_fh *fh=priv;
1012 struct tm6000_core *dev = fh->dev;
1014 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1019 videobuf_streamoff(&fh->vb_vidq);
1025 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1028 struct tm6000_fh *fh=priv;
1029 struct tm6000_core *dev = fh->dev;
1032 rc = tm6000_init_analog_mode(dev);
1034 fh->width = dev->width;
1035 fh->height = dev->height;
1040 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1045 static int vidioc_enum_input(struct file *file, void *priv,
1046 struct v4l2_input *inp)
1048 switch (inp->index) {
1049 case TM6000_INPUT_TV:
1050 inp->type = V4L2_INPUT_TYPE_TUNER;
1051 strcpy(inp->name, "Television");
1053 case TM6000_INPUT_COMPOSITE:
1054 inp->type = V4L2_INPUT_TYPE_CAMERA;
1055 strcpy(inp->name, "Composite");
1057 case TM6000_INPUT_SVIDEO:
1058 inp->type = V4L2_INPUT_TYPE_CAMERA;
1059 strcpy(inp->name, "S-Video");
1064 inp->std = TM6000_STD;
1069 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1071 struct tm6000_fh *fh = priv;
1072 struct tm6000_core *dev = fh->dev;
1078 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1080 struct tm6000_fh *fh = priv;
1081 struct tm6000_core *dev = fh->dev;
1086 case TM6000_INPUT_TV:
1090 case TM6000_INPUT_COMPOSITE:
1091 case TM6000_INPUT_SVIDEO:
1098 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1099 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1103 rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
1109 /* --- controls ---------------------------------------------- */
1110 static int vidioc_queryctrl(struct file *file, void *priv,
1111 struct v4l2_queryctrl *qc)
1115 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1116 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1117 memcpy(qc, &(tm6000_qctrl[i]),
1125 static int vidioc_g_ctrl(struct file *file, void *priv,
1126 struct v4l2_control *ctrl)
1128 struct tm6000_fh *fh = priv;
1129 struct tm6000_core *dev = fh->dev;
1132 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1134 case V4L2_CID_CONTRAST:
1135 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
1137 case V4L2_CID_BRIGHTNESS:
1138 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
1140 case V4L2_CID_SATURATION:
1141 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
1144 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
1157 static int vidioc_s_ctrl(struct file *file, void *priv,
1158 struct v4l2_control *ctrl)
1160 struct tm6000_fh *fh = priv;
1161 struct tm6000_core *dev = fh->dev;
1162 u8 val = ctrl->value;
1165 case V4L2_CID_CONTRAST:
1166 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1168 case V4L2_CID_BRIGHTNESS:
1169 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1171 case V4L2_CID_SATURATION:
1172 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1175 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1181 static int vidioc_g_tuner(struct file *file, void *priv,
1182 struct v4l2_tuner *t)
1184 struct tm6000_fh *fh = priv;
1185 struct tm6000_core *dev = fh->dev;
1187 if (unlikely(UNSET == dev->tuner_type))
1192 strcpy(t->name, "Television");
1193 t->type = V4L2_TUNER_ANALOG_TV;
1194 t->capability = V4L2_TUNER_CAP_NORM;
1195 t->rangehigh = 0xffffffffUL;
1196 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1201 static int vidioc_s_tuner(struct file *file, void *priv,
1202 struct v4l2_tuner *t)
1204 struct tm6000_fh *fh = priv;
1205 struct tm6000_core *dev = fh->dev;
1207 if (UNSET == dev->tuner_type)
1215 static int vidioc_g_frequency(struct file *file, void *priv,
1216 struct v4l2_frequency *f)
1218 struct tm6000_fh *fh = priv;
1219 struct tm6000_core *dev = fh->dev;
1221 if (unlikely(UNSET == dev->tuner_type))
1224 f->type = V4L2_TUNER_ANALOG_TV;
1225 f->frequency = dev->freq;
1227 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1232 static int vidioc_s_frequency(struct file *file, void *priv,
1233 struct v4l2_frequency *f)
1235 struct tm6000_fh *fh = priv;
1236 struct tm6000_core *dev = fh->dev;
1238 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1241 if (unlikely(UNSET == dev->tuner_type))
1243 if (unlikely(f->tuner != 0))
1246 dev->freq = f->frequency;
1247 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1252 /* ------------------------------------------------------------------
1253 File operations for the device
1254 ------------------------------------------------------------------*/
1256 static int tm6000_open(struct file *file)
1258 struct video_device *vdev = video_devdata(file);
1259 struct tm6000_core *dev = video_drvdata(file);
1260 struct tm6000_fh *fh;
1261 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1264 printk(KERN_INFO "tm6000: open called (dev=%s)\n",
1265 video_device_node_name(vdev));
1267 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1268 video_device_node_name(vdev));
1271 /* If more than one user, mutex should be added */
1274 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1275 video_device_node_name(vdev), v4l2_type_names[type],
1278 /* allocate + initialize per filehandle data */
1279 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1285 file->private_data = fh;
1288 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1289 dev->fourcc = format[0].fourcc;
1291 fh->fmt = format_by_fourcc(dev->fourcc);
1293 tm6000_get_std_res (dev);
1295 fh->width = dev->width;
1296 fh->height = dev->height;
1298 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1299 "dev->vidq=0x%08lx\n",
1300 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1301 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1302 "queued=%d\n",list_empty(&dev->vidq.queued));
1303 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1304 "active=%d\n",list_empty(&dev->vidq.active));
1306 /* initialize hardware on analog mode */
1307 rc = tm6000_init_analog_mode(dev);
1311 if (dev->mode != TM6000_MODE_ANALOG) {
1312 /* Put all controls at a sane state */
1313 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1314 qctl_regs[i] = tm6000_qctrl[i].default_value;
1316 dev->mode = TM6000_MODE_ANALOG;
1319 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1322 V4L2_FIELD_INTERLACED,
1323 sizeof(struct tm6000_buffer), fh, &dev->lock);
1329 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1331 struct tm6000_fh *fh = file->private_data;
1333 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1334 if (!res_get(fh->dev, fh, true))
1337 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1338 file->f_flags & O_NONBLOCK);
1344 tm6000_poll(struct file *file, struct poll_table_struct *wait)
1346 struct tm6000_fh *fh = file->private_data;
1347 struct tm6000_buffer *buf;
1349 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1352 if (!!is_res_streaming(fh->dev, fh))
1355 if (!is_res_read(fh->dev, fh)) {
1356 /* streaming capture */
1357 if (list_empty(&fh->vb_vidq.stream))
1359 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1361 /* read() capture */
1362 return videobuf_poll_stream(file, &fh->vb_vidq,
1365 poll_wait(file, &buf->vb.done, wait);
1366 if (buf->vb.state == VIDEOBUF_DONE ||
1367 buf->vb.state == VIDEOBUF_ERROR)
1368 return POLLIN | POLLRDNORM;
1372 static int tm6000_release(struct file *file)
1374 struct tm6000_fh *fh = file->private_data;
1375 struct tm6000_core *dev = fh->dev;
1376 struct video_device *vdev = video_devdata(file);
1378 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1379 video_device_node_name(vdev), dev->users);
1385 tm6000_uninit_isoc(dev);
1386 videobuf_mmap_free(&fh->vb_vidq);
1394 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1396 struct tm6000_fh *fh = file->private_data;
1399 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1404 static struct v4l2_file_operations tm6000_fops = {
1405 .owner = THIS_MODULE,
1406 .open = tm6000_open,
1407 .release = tm6000_release,
1408 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1409 .read = tm6000_read,
1410 .poll = tm6000_poll,
1411 .mmap = tm6000_mmap,
1414 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1415 .vidioc_querycap = vidioc_querycap,
1416 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1417 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1418 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1419 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1420 .vidioc_s_std = vidioc_s_std,
1421 .vidioc_enum_input = vidioc_enum_input,
1422 .vidioc_g_input = vidioc_g_input,
1423 .vidioc_s_input = vidioc_s_input,
1424 .vidioc_queryctrl = vidioc_queryctrl,
1425 .vidioc_g_ctrl = vidioc_g_ctrl,
1426 .vidioc_s_ctrl = vidioc_s_ctrl,
1427 .vidioc_g_tuner = vidioc_g_tuner,
1428 .vidioc_s_tuner = vidioc_s_tuner,
1429 .vidioc_g_frequency = vidioc_g_frequency,
1430 .vidioc_s_frequency = vidioc_s_frequency,
1431 .vidioc_streamon = vidioc_streamon,
1432 .vidioc_streamoff = vidioc_streamoff,
1433 .vidioc_reqbufs = vidioc_reqbufs,
1434 .vidioc_querybuf = vidioc_querybuf,
1435 .vidioc_qbuf = vidioc_qbuf,
1436 .vidioc_dqbuf = vidioc_dqbuf,
1439 static struct video_device tm6000_template = {
1441 .fops = &tm6000_fops,
1442 .ioctl_ops = &video_ioctl_ops,
1443 .release = video_device_release,
1444 .tvnorms = TM6000_STD,
1445 .current_norm = V4L2_STD_NTSC_M,
1448 /* -----------------------------------------------------------------
1449 * Initialization and module stuff
1450 * ------------------------------------------------------------------
1453 int tm6000_v4l2_register(struct tm6000_core *dev)
1456 struct video_device *vfd;
1458 vfd = video_device_alloc();
1464 /* init video dma queues */
1465 INIT_LIST_HEAD(&dev->vidq.active);
1466 INIT_LIST_HEAD(&dev->vidq.queued);
1468 memcpy(dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1469 dev->vfd->debug = tm6000_debug;
1470 dev->vfd->lock = &dev->lock;
1472 vfd->v4l2_dev = &dev->v4l2_dev;
1473 video_set_drvdata(vfd, dev);
1475 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
1476 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1480 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1482 video_unregister_device(dev->vfd);
1487 int tm6000_v4l2_exit(void)
1492 module_param(video_nr, int, 0);
1493 MODULE_PARM_DESC(video_nr, "Allow changing video device number");
1495 module_param_named(debug, tm6000_debug, int, 0444);
1496 MODULE_PARM_DESC(debug, "activates debug info");
1498 module_param(vid_limit, int, 0644);
1499 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");