23 #include <linux/module.h>
25 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <linux/random.h>
35 #include <linux/videodev2.h>
46 #define BUFFER_TIMEOUT msecs_to_jiffies(2000)
49 #define TM6000_MIN_BUF 4
50 #define TM6000_DEF_BUF 8
52 #define TM6000_MAX_ISO_PACKETS 46
56 static int video_nr = -1;
57 static int radio_nr = -1;
125 static const unsigned int CTRLS =
ARRAY_SIZE(tm6000_qctrl);
126 static int qctl_regs[
ARRAY_SIZE(tm6000_qctrl)];
130 .name =
"4:2:2, packed, YVY2",
134 .name =
"4:2:2, packed, UYVY",
138 .name =
"A/V + VBI mux packet",
148 for (i = 0; i < CTRLS; i++)
149 if (tm6000_qctrl[i].
id ==
id)
150 return tm6000_qctrl+
i;
159 #define norm_maxw(a) 720
160 #define norm_maxh(a) 576
162 #define norm_minw(a) norm_maxw(a)
163 #define norm_minh(a) norm_maxh(a)
173 if (list_empty(&dma_q->
active)) {
193 buf->
vb.field_count++;
203 static int copy_streams(
u8 *
data,
unsigned long len,
214 unsigned int linewidth;
218 get_next_buf(dma_q, &vbuf);
228 for (ptr = data; ptr < endp;) {
231 if (dev->
isoc_ctl.tmp_buf_len > 0) {
234 if (4 - dev->
isoc_ctl.tmp_buf_len > 0) {
239 ptr += 4 - dev->
isoc_ctl.tmp_buf_len;
243 if (ptr + 3 >= endp) {
251 for (; ptr < endp - 3; ptr++) {
252 if (*(ptr + 3) == 0x47)
256 header = *(
unsigned long *)ptr;
261 size = ((header & 0x7e) << 1);
264 block = (header >> 7) & 0xf;
265 field = (header >> 11) & 0x1;
266 line = (header >> 12) & 0x1ff;
267 cmd = (header >> 21) & 0x7;
278 if ((dev->
isoc_ctl.vfield != field) &&
284 buffer_filled(dev, dma_q, vbuf);
286 "new buffer filled\n");
287 get_next_buf(dma_q, &vbuf);
295 linewidth = vbuf->
vb.width << 1;
296 pos = ((line << 1) - field - 1) *
299 if (pos + size > vbuf->
vb.size)
319 cpysize = (endp - ptr >
size) ? size : endp - ptr;
326 memcpy(&voutp[pos], ptr, cpysize);
330 for (i = 0; i < cpysize; i += 2)
349 if (ptr + pktsize > endp) {
355 dev->
isoc_ctl.size = size - cpysize;
371 static int copy_multiplexed(
u8 *ptr,
unsigned long len,
376 unsigned int pos = dev->
isoc_ctl.pos, cpysize;
381 get_next_buf(dma_q, &buf);
389 cpysize =
min(len, buf->
vb.size-pos);
390 memcpy(&outp[pos], ptr, cpysize);
394 if (pos >= buf->
vb.size) {
397 buffer_filled(dev, dma_q, buf);
399 get_next_buf(dma_q, &buf);
413 static inline void print_err_status(
struct tm6000_core *dev,
416 char *errmsg =
"Unknown";
420 errmsg =
"unlinked synchronuously";
423 errmsg =
"unlinked asynchronuously";
426 errmsg =
"Buffer error (overrun)";
429 errmsg =
"Stalled (device not responding)";
432 errmsg =
"Babble (bad cable?)";
435 errmsg =
"Bit-stuff error (bad cable?)";
438 errmsg =
"CRC/Timeout (could be anything)";
441 errmsg =
"Device does not respond";
449 packet, status, errmsg);
457 static inline int tm6000_isoc_copy(
struct urb *urb)
461 int i, len = 0, rc = 1,
status;
464 if (urb->status < 0) {
465 print_err_status(dev, -1, urb->status);
469 for (i = 0; i < urb->number_of_packets; i++) {
470 status = urb->iso_frame_desc[
i].status;
473 print_err_status(dev, i, status);
477 len = urb->iso_frame_desc[
i].actual_length;
480 p = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
481 if (!urb->iso_frame_desc[i].status) {
483 rc = copy_multiplexed(p, len, urb);
487 copy_streams(p, len, urb);
503 static void tm6000_irq_callback(
struct urb *urb)
509 switch (urb->status) {
520 tm6000_err(
"urb completion error %d.\n", urb->status);
524 spin_lock(&dev->
slock);
525 tm6000_isoc_copy(urb);
526 spin_unlock(&dev->
slock);
529 for (i = 0; i < urb->number_of_packets; i++) {
530 urb->iso_frame_desc[
i].status = 0;
531 urb->iso_frame_desc[
i].actual_length = 0;
536 tm6000_err(
"urb resubmit failed (error=%i)\n",
543 static void tm6000_uninit_isoc(
struct tm6000_core *dev)
549 for (i = 0; i < dev->
isoc_ctl.num_bufs; i++) {
554 if (dev->
isoc_ctl.transfer_buffer[i]) {
556 urb->transfer_buffer_length,
577 static int tm6000_prepare_isoc(
struct tm6000_core *dev)
580 int i,
j, sb_size,
pipe,
size, max_packets, num_bufs = 8;
584 tm6000_uninit_isoc(dev);
589 dev->isoc_in.bInterfaceNumber,
590 dev->isoc_in.bAlternateSetting);
595 pipe = usb_rcvisocpipe(dev->
udev,
596 dev->isoc_in.endp->desc.bEndpointAddress &
599 size = usb_maxpacket(dev->
udev, pipe, usb_pipeout(pipe));
601 if (size > dev->isoc_in.maxsize)
602 size = dev->isoc_in.maxsize;
607 sb_size = max_packets *
size;
613 tm6000_err(
"cannot alloc memory for usb buffers\n");
619 if (!dev->
isoc_ctl.transfer_buffer) {
620 tm6000_err(
"cannot allocate memory for usbtransfer\n");
626 " (%d bytes) of %d bytes each to handle %u size\n",
627 max_packets, num_bufs, sb_size,
628 dev->isoc_in.maxsize, size);
631 for (i = 0; i < dev->
isoc_ctl.num_bufs; i++) {
634 tm6000_err(
"cannot alloc isoc_ctl.urb %i\n", i);
635 tm6000_uninit_isoc(dev);
643 if (!dev->
isoc_ctl.transfer_buffer[i]) {
644 tm6000_err(
"unable to allocate %i bytes for transfer"
648 tm6000_uninit_isoc(dev);
653 usb_fill_bulk_urb(urb, dev->
udev, pipe,
654 dev->
isoc_ctl.transfer_buffer[i], sb_size,
655 tm6000_irq_callback, dma_q);
656 urb->interval = dev->isoc_in.endp->desc.bInterval;
657 urb->number_of_packets = max_packets;
658 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
660 for (j = 0; j < max_packets; j++) {
661 urb->iso_frame_desc[
j].offset = size *
j;
662 urb->iso_frame_desc[
j].length =
size;
669 static int tm6000_start_thread(
struct tm6000_core *dev)
680 for (i = 0; i < dev->
isoc_ctl.num_bufs; i++) {
683 tm6000_err(
"submit of urb %i failed (error=%i)\n", i,
685 tm6000_uninit_isoc(dev);
710 while (*size * *count > vid_limit * 1024 * 1024)
737 spin_unlock_irqrestore(&dev->
slock, flags);
758 if (0 != buf->
vb.baddr && buf->
vb.bsize < buf->
vb.size)
761 if (buf->
fmt != fh->
fmt ||
764 buf->
vb.field != field) {
779 rc = tm6000_prepare_isoc(dev);
783 rc = tm6000_start_thread(dev);
817 .buf_setup = buffer_setup,
818 .buf_prepare = buffer_prepare,
819 .buf_queue = buffer_queue,
820 .buf_release = buffer_release,
878 static int vidioc_querycap(
struct file *
file,
void *
priv,
884 strlcpy(cap->
card,
"Trident TVMaster TM5600/6000/6010",
sizeof(cap->
card));
896 static int vidioc_enum_fmt_vid_cap(
struct file *file,
void *priv,
907 static int vidioc_g_fmt_vid_cap(
struct file *file,
void *priv,
915 f->
fmt.
pix.pixelformat = fh->
fmt->fourcc;
917 (f->
fmt.
pix.width * fh->
fmt->depth) >> 3;
929 if (format[i].fourcc == fourcc)
934 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *priv,
944 " invalid.\n", f->
fmt.
pix.pixelformat);
962 f->
fmt.
pix.width &= ~0x01;
975 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
980 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
997 static int vidioc_reqbufs(
struct file *file,
void *priv,
1005 static int vidioc_querybuf(
struct file *file,
void *priv,
1013 static int vidioc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *p)
1020 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *p)
1028 static int vidioc_streamon(
struct file *file,
void *priv,
enum v4l2_buf_type i)
1038 if (!res_get(dev, fh,
false))
1043 static int vidioc_streamoff(
struct file *file,
void *priv,
enum v4l2_buf_type i)
1060 static int vidioc_s_std(
struct file *file,
void *priv,
v4l2_std_id *
norm)
1080 static const char *iname[] = {
1087 static int vidioc_enum_input(
struct file *file,
void *priv,
1098 if (!dev->
vinput[n].type)
1115 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1125 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int i)
1133 if (!dev->
vinput[i].type)
1138 rc = vidioc_s_std(file, priv, &dev->
vfd->current_norm);
1144 static int vidioc_queryctrl(
struct file *file,
void *priv,
1149 for (i = 0; i <
ARRAY_SIZE(tm6000_qctrl); i++)
1150 if (qc->
id && qc->
id == tm6000_qctrl[i].
id) {
1151 memcpy(qc, &(tm6000_qctrl[i]),
1159 static int vidioc_g_ctrl(
struct file *file,
void *priv,
1197 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1229 static int vidioc_g_tuner(
struct file *file,
void *priv,
1253 static int vidioc_s_tuner(
struct file *file,
void *priv,
1272 static int vidioc_g_frequency(
struct file *file,
void *priv,
1289 static int vidioc_s_frequency(
struct file *file,
void *priv,
1310 static int radio_querycap(
struct file *file,
void *priv,
1331 static int radio_g_tuner(
struct file *file,
void *priv,
1340 memset(t, 0,
sizeof(*t));
1350 static int radio_s_tuner(
struct file *file,
void *priv,
1364 static int radio_enum_input(
struct file *file,
void *priv,
1382 static int radio_g_input(
struct file *filp,
void *priv,
unsigned int *i)
1387 if (dev->
input != 5)
1390 *i = dev->
input - 5;
1395 static int radio_g_audio(
struct file *file,
void *priv,
1398 memset(a, 0,
sizeof(*a));
1403 static int radio_s_audio(
struct file *file,
void *priv,
1409 static int radio_s_input(
struct file *filp,
void *priv,
unsigned int i)
1425 static int radio_s_std(
struct file *file,
void *fh,
v4l2_std_id *norm)
1430 static int radio_queryctrl(
struct file *file,
void *priv,
1439 ctrl = ctrl_by_id(c->
id);
1451 static int __tm6000_open(
struct file *file)
1461 video_device_node_name(vdev));
1504 "dev->vidq=0x%08lx\n",
1505 (
unsigned long)fh, (
unsigned long)dev,
1506 (
unsigned long)&dev->
vidq);
1508 "queued=%d\n", list_empty(&dev->vidq.queued));
1510 "active=%d\n", list_empty(&dev->vidq.active));
1519 for (i = 0; i <
ARRAY_SIZE(tm6000_qctrl); i++)
1536 tm6000_prepare_isoc(dev);
1537 tm6000_start_thread(dev);
1543 static int tm6000_open(
struct file *file)
1549 res = __tm6000_open(file);
1555 tm6000_read(
struct file *file,
char __user *data,
size_t count, loff_t *pos)
1563 if (!res_get(fh->
dev, fh,
true))
1585 if (!!is_res_streaming(fh->
dev, fh))
1588 if (!is_res_read(fh->
dev, fh)) {
1590 if (list_empty(&fh->
vb_vidq.stream))
1597 poll_wait(file, &buf->
vb.done, wait);
1604 static unsigned int tm6000_poll(
struct file *file,
struct poll_table_struct *wait)
1611 res = __tm6000_poll(file, wait);
1616 static int tm6000_release(
struct file *file)
1623 video_device_node_name(vdev), dev->
users);
1631 tm6000_uninit_isoc(dev);
1638 if (dev->int_in.endp)
1640 dev->isoc_in.bInterfaceNumber, 2);
1643 dev->isoc_in.bInterfaceNumber, 0);
1658 static int tm6000_mmap(
struct file *file,
struct vm_area_struct * vma)
1673 .open = tm6000_open,
1674 .release = tm6000_release,
1676 .read = tm6000_read,
1677 .poll = tm6000_poll,
1678 .mmap = tm6000_mmap,
1708 .fops = &tm6000_fops,
1709 .ioctl_ops = &video_ioctl_ops,
1717 .open = tm6000_open,
1718 .release = tm6000_release,
1723 .vidioc_querycap = radio_querycap,
1724 .vidioc_g_tuner = radio_g_tuner,
1725 .vidioc_enum_input = radio_enum_input,
1726 .vidioc_g_audio = radio_g_audio,
1727 .vidioc_s_tuner = radio_s_tuner,
1728 .vidioc_s_audio = radio_s_audio,
1729 .vidioc_s_input = radio_s_input,
1730 .vidioc_s_std = radio_s_std,
1731 .vidioc_queryctrl = radio_queryctrl,
1732 .vidioc_g_input = radio_g_input,
1741 .fops = &radio_fops,
1742 .ioctl_ops = &radio_ioctl_ops,
1752 *
template,
const char *type_name)
1768 video_set_drvdata(vfd, dev);
1785 INIT_LIST_HEAD(&dev->
vidq.active);
1786 INIT_LIST_HEAD(&dev->
vidq.queued);
1797 dev->
name, video_device_node_name(dev->
vfd));
1799 if (dev->
caps.has_radio) {
1820 printk(
KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1829 if (video_is_registered(dev->
radio_dev))