Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
0003 //
0004 // Copyright (c) 2006-2007 Mauro Carvalho Chehab <mchehab@kernel.org>
0005 //
0006 // Copyright (c) 2007 Michel Ludwig <michel.ludwig@gmail.com>
0007 //  - Fixed module load/unload
0008 
0009 #include <linux/module.h>
0010 #include <linux/delay.h>
0011 #include <linux/errno.h>
0012 #include <linux/fs.h>
0013 #include <linux/kernel.h>
0014 #include <linux/slab.h>
0015 #include <linux/mm.h>
0016 #include <linux/ioport.h>
0017 #include <linux/init.h>
0018 #include <linux/sched.h>
0019 #include <linux/random.h>
0020 #include <linux/usb.h>
0021 #include <linux/videodev2.h>
0022 #include <media/v4l2-ioctl.h>
0023 #include <media/v4l2-event.h>
0024 #include <media/tuner.h>
0025 #include <linux/interrupt.h>
0026 #include <linux/kthread.h>
0027 #include <linux/highmem.h>
0028 #include <linux/freezer.h>
0029 
0030 #include "tm6000-regs.h"
0031 #include "tm6000.h"
0032 
0033 #define BUFFER_TIMEOUT     msecs_to_jiffies(2000)  /* 2 seconds */
0034 
0035 /* Limits minimum and default number of buffers */
0036 #define TM6000_MIN_BUF 4
0037 #define TM6000_DEF_BUF 8
0038 
0039 #define TM6000_NUM_URB_BUF 8
0040 
0041 #define TM6000_MAX_ISO_PACKETS  46  /* Max number of ISO packets */
0042 
0043 /* Declare static vars that will be used as parameters */
0044 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
0045 static int video_nr = -1;       /* /dev/videoN, -1 for autodetect */
0046 static int radio_nr = -1;       /* /dev/radioN, -1 for autodetect */
0047 static bool keep_urb;           /* keep urb buffers allocated */
0048 
0049 /* Debug level */
0050 int tm6000_debug;
0051 EXPORT_SYMBOL_GPL(tm6000_debug);
0052 
0053 static struct tm6000_fmt format[] = {
0054     {
0055         .fourcc   = V4L2_PIX_FMT_YUYV,
0056         .depth    = 16,
0057     }, {
0058         .fourcc   = V4L2_PIX_FMT_UYVY,
0059         .depth    = 16,
0060     }, {
0061         .fourcc   = V4L2_PIX_FMT_TM6000,
0062         .depth    = 16,
0063     }
0064 };
0065 
0066 /* ------------------------------------------------------------------
0067  *  DMA and thread functions
0068  * ------------------------------------------------------------------
0069  */
0070 
0071 #define norm_maxw(a) 720
0072 #define norm_maxh(a) 576
0073 
0074 #define norm_minw(a) norm_maxw(a)
0075 #define norm_minh(a) norm_maxh(a)
0076 
0077 /*
0078  * video-buf generic routine to get the next available buffer
0079  */
0080 static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
0081                    struct tm6000_buffer   **buf)
0082 {
0083     struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
0084 
0085     if (list_empty(&dma_q->active)) {
0086         dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
0087         *buf = NULL;
0088         return;
0089     }
0090 
0091     *buf = list_entry(dma_q->active.next,
0092             struct tm6000_buffer, vb.queue);
0093 }
0094 
0095 /*
0096  * Announces that a buffer were filled and request the next
0097  */
0098 static inline void buffer_filled(struct tm6000_core *dev,
0099                  struct tm6000_dmaqueue *dma_q,
0100                  struct tm6000_buffer *buf)
0101 {
0102     /* Advice that buffer was filled */
0103     dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
0104     buf->vb.state = VIDEOBUF_DONE;
0105     buf->vb.field_count++;
0106     buf->vb.ts = ktime_get_ns();
0107 
0108     list_del(&buf->vb.queue);
0109     wake_up(&buf->vb.done);
0110 }
0111 
0112 /*
0113  * Identify the tm5600/6000 buffer header type and properly handles
0114  */
0115 static int copy_streams(u8 *data, unsigned long len,
0116             struct urb *urb)
0117 {
0118     struct tm6000_dmaqueue  *dma_q = urb->context;
0119     struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
0120     u8 *ptr = data, *endp = data+len;
0121     unsigned long header = 0;
0122     int rc = 0;
0123     unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
0124     struct tm6000_buffer *vbuf = NULL;
0125     char *voutp = NULL;
0126     unsigned int linewidth;
0127 
0128     if (!dev->radio) {
0129         /* get video buffer */
0130         get_next_buf(dma_q, &vbuf);
0131 
0132         if (!vbuf)
0133             return rc;
0134         voutp = videobuf_to_vmalloc(&vbuf->vb);
0135 
0136         if (!voutp)
0137             return 0;
0138     }
0139 
0140     for (ptr = data; ptr < endp;) {
0141         if (!dev->isoc_ctl.cmd) {
0142             /* Header */
0143             if (dev->isoc_ctl.tmp_buf_len > 0) {
0144                 /* from last urb or packet */
0145                 header = dev->isoc_ctl.tmp_buf;
0146                 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
0147                     memcpy((u8 *)&header +
0148                         dev->isoc_ctl.tmp_buf_len,
0149                         ptr,
0150                         4 - dev->isoc_ctl.tmp_buf_len);
0151                     ptr += 4 - dev->isoc_ctl.tmp_buf_len;
0152                 }
0153                 dev->isoc_ctl.tmp_buf_len = 0;
0154             } else {
0155                 if (ptr + 3 >= endp) {
0156                     /* have incomplete header */
0157                     dev->isoc_ctl.tmp_buf_len = endp - ptr;
0158                     memcpy(&dev->isoc_ctl.tmp_buf, ptr,
0159                         dev->isoc_ctl.tmp_buf_len);
0160                     return rc;
0161                 }
0162                 /* Seek for sync */
0163                 for (; ptr < endp - 3; ptr++) {
0164                     if (*(ptr + 3) == 0x47)
0165                         break;
0166                 }
0167                 /* Get message header */
0168                 header = *(unsigned long *)ptr;
0169                 ptr += 4;
0170             }
0171 
0172             /* split the header fields */
0173             size = ((header & 0x7e) << 1);
0174             if (size > 0)
0175                 size -= 4;
0176             block = (header >> 7) & 0xf;
0177             field = (header >> 11) & 0x1;
0178             line  = (header >> 12) & 0x1ff;
0179             cmd   = (header >> 21) & 0x7;
0180             /* Validates header fields */
0181             if (size > TM6000_URB_MSG_LEN)
0182                 size = TM6000_URB_MSG_LEN;
0183             pktsize = TM6000_URB_MSG_LEN;
0184             /*
0185              * calculate position in buffer and change the buffer
0186              */
0187             switch (cmd) {
0188             case TM6000_URB_MSG_VIDEO:
0189                 if (!dev->radio) {
0190                     if ((dev->isoc_ctl.vfield != field) &&
0191                         (field == 1)) {
0192                         /*
0193                          * Announces that a new buffer
0194                          * were filled
0195                          */
0196                         buffer_filled(dev, dma_q, vbuf);
0197                         dprintk(dev, V4L2_DEBUG_ISOC,
0198                             "new buffer filled\n");
0199                         get_next_buf(dma_q, &vbuf);
0200                         if (!vbuf)
0201                             return rc;
0202                         voutp = videobuf_to_vmalloc(&vbuf->vb);
0203                         if (!voutp)
0204                             return rc;
0205                         memset(voutp, 0, vbuf->vb.size);
0206                     }
0207                     linewidth = vbuf->vb.width << 1;
0208                     pos = ((line << 1) - field - 1) *
0209                     linewidth + block * TM6000_URB_MSG_LEN;
0210                     /* Don't allow to write out of the buffer */
0211                     if (pos + size > vbuf->vb.size)
0212                         cmd = TM6000_URB_MSG_ERR;
0213                     dev->isoc_ctl.vfield = field;
0214                 }
0215                 break;
0216             case TM6000_URB_MSG_VBI:
0217                 break;
0218             case TM6000_URB_MSG_AUDIO:
0219             case TM6000_URB_MSG_PTS:
0220                 size = pktsize; /* Size is always 180 bytes */
0221                 break;
0222             }
0223         } else {
0224             /* Continue the last copy */
0225             cmd = dev->isoc_ctl.cmd;
0226             size = dev->isoc_ctl.size;
0227             pos = dev->isoc_ctl.pos;
0228             pktsize = dev->isoc_ctl.pktsize;
0229             field = dev->isoc_ctl.field;
0230         }
0231         cpysize = (endp - ptr > size) ? size : endp - ptr;
0232         if (cpysize) {
0233             /* copy data in different buffers */
0234             switch (cmd) {
0235             case TM6000_URB_MSG_VIDEO:
0236                 /* Fills video buffer */
0237                 if (vbuf)
0238                     memcpy(&voutp[pos], ptr, cpysize);
0239                 break;
0240             case TM6000_URB_MSG_AUDIO: {
0241                 int i;
0242                 for (i = 0; i < cpysize; i += 2)
0243                     swab16s((u16 *)(ptr + i));
0244 
0245                 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
0246                 break;
0247             }
0248             case TM6000_URB_MSG_VBI:
0249                 /* Need some code to copy vbi buffer */
0250                 break;
0251             case TM6000_URB_MSG_PTS: {
0252                 /* Need some code to copy pts */
0253                 u32 pts;
0254                 pts = *(u32 *)ptr;
0255                 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
0256                     field, pts);
0257                 break;
0258             }
0259             }
0260         }
0261         if (ptr + pktsize > endp) {
0262             /*
0263              * End of URB packet, but cmd processing is not
0264              * complete. Preserve the state for a next packet
0265              */
0266             dev->isoc_ctl.pos = pos + cpysize;
0267             dev->isoc_ctl.size = size - cpysize;
0268             dev->isoc_ctl.cmd = cmd;
0269             dev->isoc_ctl.field = field;
0270             dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
0271             ptr += endp - ptr;
0272         } else {
0273             dev->isoc_ctl.cmd = 0;
0274             ptr += pktsize;
0275         }
0276     }
0277     return 0;
0278 }
0279 
0280 /*
0281  * Identify the tm5600/6000 buffer header type and properly handles
0282  */
0283 static int copy_multiplexed(u8 *ptr, unsigned long len,
0284             struct urb *urb)
0285 {
0286     struct tm6000_dmaqueue  *dma_q = urb->context;
0287     struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
0288     unsigned int pos = dev->isoc_ctl.pos, cpysize;
0289     int rc = 1;
0290     struct tm6000_buffer *buf;
0291     char *outp = NULL;
0292 
0293     get_next_buf(dma_q, &buf);
0294     if (buf)
0295         outp = videobuf_to_vmalloc(&buf->vb);
0296 
0297     if (!outp)
0298         return 0;
0299 
0300     while (len > 0) {
0301         cpysize = min(len, buf->vb.size-pos);
0302         memcpy(&outp[pos], ptr, cpysize);
0303         pos += cpysize;
0304         ptr += cpysize;
0305         len -= cpysize;
0306         if (pos >= buf->vb.size) {
0307             pos = 0;
0308             /* Announces that a new buffer were filled */
0309             buffer_filled(dev, dma_q, buf);
0310             dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
0311             get_next_buf(dma_q, &buf);
0312             if (!buf)
0313                 break;
0314             outp = videobuf_to_vmalloc(&(buf->vb));
0315             if (!outp)
0316                 return rc;
0317             pos = 0;
0318         }
0319     }
0320 
0321     dev->isoc_ctl.pos = pos;
0322     return rc;
0323 }
0324 
0325 static inline void print_err_status(struct tm6000_core *dev,
0326                      int packet, int status)
0327 {
0328     char *errmsg = "Unknown";
0329 
0330     switch (status) {
0331     case -ENOENT:
0332         errmsg = "unlinked synchronously";
0333         break;
0334     case -ECONNRESET:
0335         errmsg = "unlinked asynchronously";
0336         break;
0337     case -ENOSR:
0338         errmsg = "Buffer error (overrun)";
0339         break;
0340     case -EPIPE:
0341         errmsg = "Stalled (device not responding)";
0342         break;
0343     case -EOVERFLOW:
0344         errmsg = "Babble (bad cable?)";
0345         break;
0346     case -EPROTO:
0347         errmsg = "Bit-stuff error (bad cable?)";
0348         break;
0349     case -EILSEQ:
0350         errmsg = "CRC/Timeout (could be anything)";
0351         break;
0352     case -ETIME:
0353         errmsg = "Device does not respond";
0354         break;
0355     }
0356     if (packet < 0) {
0357         dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
0358             status, errmsg);
0359     } else {
0360         dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
0361             packet, status, errmsg);
0362     }
0363 }
0364 
0365 
0366 /*
0367  * Controls the isoc copy of each urb packet
0368  */
0369 static inline int tm6000_isoc_copy(struct urb *urb)
0370 {
0371     struct tm6000_dmaqueue  *dma_q = urb->context;
0372     struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
0373     int i, len = 0, rc = 1, status;
0374     char *p;
0375 
0376     if (urb->status < 0) {
0377         print_err_status(dev, -1, urb->status);
0378         return 0;
0379     }
0380 
0381     for (i = 0; i < urb->number_of_packets; i++) {
0382         status = urb->iso_frame_desc[i].status;
0383 
0384         if (status < 0) {
0385             print_err_status(dev, i, status);
0386             continue;
0387         }
0388 
0389         len = urb->iso_frame_desc[i].actual_length;
0390 
0391         if (len > 0) {
0392             p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
0393             if (!urb->iso_frame_desc[i].status) {
0394                 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
0395                     rc = copy_multiplexed(p, len, urb);
0396                     if (rc <= 0)
0397                         return rc;
0398                 } else {
0399                     copy_streams(p, len, urb);
0400                 }
0401             }
0402         }
0403     }
0404     return rc;
0405 }
0406 
0407 /* ------------------------------------------------------------------
0408  *  URB control
0409  * ------------------------------------------------------------------
0410  */
0411 
0412 /*
0413  * IRQ callback, called by URB callback
0414  */
0415 static void tm6000_irq_callback(struct urb *urb)
0416 {
0417     struct tm6000_dmaqueue  *dma_q = urb->context;
0418     struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
0419     unsigned long flags;
0420     int i;
0421 
0422     switch (urb->status) {
0423     case 0:
0424     case -ETIMEDOUT:
0425         break;
0426 
0427     case -ECONNRESET:
0428     case -ENOENT:
0429     case -ESHUTDOWN:
0430         return;
0431 
0432     default:
0433         tm6000_err("urb completion error %d.\n", urb->status);
0434         break;
0435     }
0436 
0437     spin_lock_irqsave(&dev->slock, flags);
0438     tm6000_isoc_copy(urb);
0439     spin_unlock_irqrestore(&dev->slock, flags);
0440 
0441     /* Reset urb buffers */
0442     for (i = 0; i < urb->number_of_packets; i++) {
0443         urb->iso_frame_desc[i].status = 0;
0444         urb->iso_frame_desc[i].actual_length = 0;
0445     }
0446 
0447     urb->status = usb_submit_urb(urb, GFP_ATOMIC);
0448     if (urb->status)
0449         tm6000_err("urb resubmit failed (error=%i)\n",
0450             urb->status);
0451 }
0452 
0453 /*
0454  * Allocate URB buffers
0455  */
0456 static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
0457 {
0458     int num_bufs = TM6000_NUM_URB_BUF;
0459     int i;
0460 
0461     if (dev->urb_buffer)
0462         return 0;
0463 
0464     dev->urb_buffer = kmalloc_array(num_bufs, sizeof(*dev->urb_buffer),
0465                     GFP_KERNEL);
0466     if (!dev->urb_buffer)
0467         return -ENOMEM;
0468 
0469     dev->urb_dma = kmalloc_array(num_bufs, sizeof(*dev->urb_dma),
0470                      GFP_KERNEL);
0471     if (!dev->urb_dma)
0472         return -ENOMEM;
0473 
0474     for (i = 0; i < num_bufs; i++) {
0475         dev->urb_buffer[i] = usb_alloc_coherent(
0476                     dev->udev, dev->urb_size,
0477                     GFP_KERNEL, &dev->urb_dma[i]);
0478         if (!dev->urb_buffer[i]) {
0479             tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
0480                     dev->urb_size, i);
0481             return -ENOMEM;
0482         }
0483         memset(dev->urb_buffer[i], 0, dev->urb_size);
0484     }
0485 
0486     return 0;
0487 }
0488 
0489 /*
0490  * Free URB buffers
0491  */
0492 static int tm6000_free_urb_buffers(struct tm6000_core *dev)
0493 {
0494     int i;
0495 
0496     if (!dev->urb_buffer)
0497         return 0;
0498 
0499     for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
0500         if (dev->urb_buffer[i]) {
0501             usb_free_coherent(dev->udev,
0502                     dev->urb_size,
0503                     dev->urb_buffer[i],
0504                     dev->urb_dma[i]);
0505             dev->urb_buffer[i] = NULL;
0506         }
0507     }
0508     kfree(dev->urb_buffer);
0509     kfree(dev->urb_dma);
0510     dev->urb_buffer = NULL;
0511     dev->urb_dma = NULL;
0512 
0513     return 0;
0514 }
0515 
0516 /*
0517  * Stop and Deallocate URBs
0518  */
0519 static void tm6000_uninit_isoc(struct tm6000_core *dev)
0520 {
0521     struct urb *urb;
0522     int i;
0523 
0524     dev->isoc_ctl.buf = NULL;
0525     for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
0526         urb = dev->isoc_ctl.urb[i];
0527         if (urb) {
0528             usb_kill_urb(urb);
0529             usb_unlink_urb(urb);
0530             usb_free_urb(urb);
0531             dev->isoc_ctl.urb[i] = NULL;
0532         }
0533         dev->isoc_ctl.transfer_buffer[i] = NULL;
0534     }
0535 
0536     if (!keep_urb)
0537         tm6000_free_urb_buffers(dev);
0538 
0539     kfree(dev->isoc_ctl.urb);
0540     kfree(dev->isoc_ctl.transfer_buffer);
0541 
0542     dev->isoc_ctl.urb = NULL;
0543     dev->isoc_ctl.transfer_buffer = NULL;
0544     dev->isoc_ctl.num_bufs = 0;
0545 }
0546 
0547 /*
0548  * Assign URBs and start IRQ
0549  */
0550 static int tm6000_prepare_isoc(struct tm6000_core *dev)
0551 {
0552     struct tm6000_dmaqueue *dma_q = &dev->vidq;
0553     int i, j, sb_size, pipe, size, max_packets;
0554     int num_bufs = TM6000_NUM_URB_BUF;
0555     struct urb *urb;
0556 
0557     /* De-allocates all pending stuff */
0558     tm6000_uninit_isoc(dev);
0559     /* Stop interrupt USB pipe */
0560     tm6000_ir_int_stop(dev);
0561 
0562     usb_set_interface(dev->udev,
0563               dev->isoc_in.bInterfaceNumber,
0564               dev->isoc_in.bAlternateSetting);
0565 
0566     /* Start interrupt USB pipe */
0567     tm6000_ir_int_start(dev);
0568 
0569     pipe = usb_rcvisocpipe(dev->udev,
0570                    dev->isoc_in.endp->desc.bEndpointAddress &
0571                    USB_ENDPOINT_NUMBER_MASK);
0572 
0573     size = usb_maxpacket(dev->udev, pipe);
0574 
0575     if (size > dev->isoc_in.maxsize)
0576         size = dev->isoc_in.maxsize;
0577 
0578     dev->isoc_ctl.max_pkt_size = size;
0579 
0580     max_packets = TM6000_MAX_ISO_PACKETS;
0581     sb_size = max_packets * size;
0582     dev->urb_size = sb_size;
0583 
0584     dev->isoc_ctl.num_bufs = num_bufs;
0585 
0586     dev->isoc_ctl.urb = kmalloc_array(num_bufs, sizeof(void *),
0587                       GFP_KERNEL);
0588     if (!dev->isoc_ctl.urb)
0589         return -ENOMEM;
0590 
0591     dev->isoc_ctl.transfer_buffer = kmalloc_array(num_bufs,
0592                               sizeof(void *),
0593                               GFP_KERNEL);
0594     if (!dev->isoc_ctl.transfer_buffer) {
0595         kfree(dev->isoc_ctl.urb);
0596         return -ENOMEM;
0597     }
0598 
0599     dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets (%d bytes) of %d bytes each to handle %u size\n",
0600             max_packets, num_bufs, sb_size,
0601             dev->isoc_in.maxsize, size);
0602 
0603 
0604     if (tm6000_alloc_urb_buffers(dev) < 0) {
0605         tm6000_err("cannot allocate memory for urb buffers\n");
0606 
0607         /* call free, as some buffers might have been allocated */
0608         tm6000_free_urb_buffers(dev);
0609         kfree(dev->isoc_ctl.urb);
0610         kfree(dev->isoc_ctl.transfer_buffer);
0611         return -ENOMEM;
0612     }
0613 
0614     /* allocate urbs and transfer buffers */
0615     for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
0616         urb = usb_alloc_urb(max_packets, GFP_KERNEL);
0617         if (!urb) {
0618             tm6000_uninit_isoc(dev);
0619             tm6000_free_urb_buffers(dev);
0620             return -ENOMEM;
0621         }
0622         dev->isoc_ctl.urb[i] = urb;
0623 
0624         urb->transfer_dma = dev->urb_dma[i];
0625         dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
0626 
0627         usb_fill_bulk_urb(urb, dev->udev, pipe,
0628                   dev->isoc_ctl.transfer_buffer[i], sb_size,
0629                   tm6000_irq_callback, dma_q);
0630         urb->interval = dev->isoc_in.endp->desc.bInterval;
0631         urb->number_of_packets = max_packets;
0632         urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
0633 
0634         for (j = 0; j < max_packets; j++) {
0635             urb->iso_frame_desc[j].offset = size * j;
0636             urb->iso_frame_desc[j].length = size;
0637         }
0638     }
0639 
0640     return 0;
0641 }
0642 
0643 static int tm6000_start_thread(struct tm6000_core *dev)
0644 {
0645     struct tm6000_dmaqueue *dma_q = &dev->vidq;
0646     int i;
0647 
0648     dma_q->frame = 0;
0649     dma_q->ini_jiffies = jiffies;
0650 
0651     init_waitqueue_head(&dma_q->wq);
0652 
0653     /* submit urbs and enables IRQ */
0654     for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
0655         int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
0656         if (rc) {
0657             tm6000_err("submit of urb %i failed (error=%i)\n", i,
0658                    rc);
0659             tm6000_uninit_isoc(dev);
0660             return rc;
0661         }
0662     }
0663 
0664     return 0;
0665 }
0666 
0667 /* ------------------------------------------------------------------
0668  *  Videobuf operations
0669  * ------------------------------------------------------------------
0670  */
0671 
0672 static int
0673 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
0674 {
0675     struct tm6000_fh *fh = vq->priv_data;
0676 
0677     *size = fh->fmt->depth * fh->width * fh->height >> 3;
0678     if (0 == *count)
0679         *count = TM6000_DEF_BUF;
0680 
0681     if (*count < TM6000_MIN_BUF)
0682         *count = TM6000_MIN_BUF;
0683 
0684     while (*size * *count > vid_limit * 1024 * 1024)
0685         (*count)--;
0686 
0687     return 0;
0688 }
0689 
0690 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
0691 {
0692     struct tm6000_fh *fh = vq->priv_data;
0693     struct tm6000_core   *dev = fh->dev;
0694     unsigned long flags;
0695 
0696     /* We used to wait for the buffer to finish here, but this didn't work
0697        because, as we were keeping the state as VIDEOBUF_QUEUED,
0698        videobuf_queue_cancel marked it as finished for us.
0699        (Also, it could wedge forever if the hardware was misconfigured.)
0700 
0701        This should be safe; by the time we get here, the buffer isn't
0702        queued anymore. If we ever start marking the buffers as
0703        VIDEOBUF_ACTIVE, it won't be, though.
0704     */
0705     spin_lock_irqsave(&dev->slock, flags);
0706     if (dev->isoc_ctl.buf == buf)
0707         dev->isoc_ctl.buf = NULL;
0708     spin_unlock_irqrestore(&dev->slock, flags);
0709 
0710     videobuf_vmalloc_free(&buf->vb);
0711     buf->vb.state = VIDEOBUF_NEEDS_INIT;
0712 }
0713 
0714 static int
0715 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
0716                         enum v4l2_field field)
0717 {
0718     struct tm6000_fh     *fh  = vq->priv_data;
0719     struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
0720     struct tm6000_core   *dev = fh->dev;
0721     int rc = 0;
0722 
0723     BUG_ON(NULL == fh->fmt);
0724 
0725 
0726     /* FIXME: It assumes depth=2 */
0727     /* The only currently supported format is 16 bits/pixel */
0728     buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
0729     if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
0730         return -EINVAL;
0731 
0732     if (buf->fmt       != fh->fmt    ||
0733         buf->vb.width  != fh->width  ||
0734         buf->vb.height != fh->height ||
0735         buf->vb.field  != field) {
0736         buf->fmt       = fh->fmt;
0737         buf->vb.width  = fh->width;
0738         buf->vb.height = fh->height;
0739         buf->vb.field  = field;
0740         buf->vb.state = VIDEOBUF_NEEDS_INIT;
0741     }
0742 
0743     if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
0744         rc = videobuf_iolock(vq, &buf->vb, NULL);
0745         if (rc != 0)
0746             goto fail;
0747     }
0748 
0749     if (!dev->isoc_ctl.num_bufs) {
0750         rc = tm6000_prepare_isoc(dev);
0751         if (rc < 0)
0752             goto fail;
0753 
0754         rc = tm6000_start_thread(dev);
0755         if (rc < 0)
0756             goto fail;
0757 
0758     }
0759 
0760     buf->vb.state = VIDEOBUF_PREPARED;
0761     return 0;
0762 
0763 fail:
0764     free_buffer(vq, buf);
0765     return rc;
0766 }
0767 
0768 static void
0769 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
0770 {
0771     struct tm6000_buffer    *buf     = container_of(vb, struct tm6000_buffer, vb);
0772     struct tm6000_fh        *fh      = vq->priv_data;
0773     struct tm6000_core      *dev     = fh->dev;
0774     struct tm6000_dmaqueue  *vidq    = &dev->vidq;
0775 
0776     buf->vb.state = VIDEOBUF_QUEUED;
0777     list_add_tail(&buf->vb.queue, &vidq->active);
0778 }
0779 
0780 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
0781 {
0782     struct tm6000_buffer   *buf  = container_of(vb, struct tm6000_buffer, vb);
0783 
0784     free_buffer(vq, buf);
0785 }
0786 
0787 static const struct videobuf_queue_ops tm6000_video_qops = {
0788     .buf_setup      = buffer_setup,
0789     .buf_prepare    = buffer_prepare,
0790     .buf_queue      = buffer_queue,
0791     .buf_release    = buffer_release,
0792 };
0793 
0794 /* ------------------------------------------------------------------
0795  *  IOCTL handling
0796  * ------------------------------------------------------------------
0797  */
0798 
0799 static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
0800 {
0801     /* Is the current fh handling it? if so, that's OK */
0802     if (dev->resources == fh && dev->is_res_read)
0803         return true;
0804 
0805     return false;
0806 }
0807 
0808 static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
0809 {
0810     /* Is the current fh handling it? if so, that's OK */
0811     if (dev->resources == fh)
0812         return true;
0813 
0814     return false;
0815 }
0816 
0817 static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
0818            bool is_res_read)
0819 {
0820     /* Is the current fh handling it? if so, that's OK */
0821     if (dev->resources == fh && dev->is_res_read == is_res_read)
0822         return true;
0823 
0824     /* is it free? */
0825     if (dev->resources)
0826         return false;
0827 
0828     /* grab it */
0829     dev->resources = fh;
0830     dev->is_res_read = is_res_read;
0831     dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
0832     return true;
0833 }
0834 
0835 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
0836 {
0837     /* Is the current fh handling it? if so, that's OK */
0838     if (dev->resources != fh)
0839         return;
0840 
0841     dev->resources = NULL;
0842     dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
0843 }
0844 
0845 /* ------------------------------------------------------------------
0846  *  IOCTL vidioc handling
0847  * ------------------------------------------------------------------
0848  */
0849 static int vidioc_querycap(struct file *file, void  *priv,
0850                     struct v4l2_capability *cap)
0851 {
0852     struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
0853 
0854     strscpy(cap->driver, "tm6000", sizeof(cap->driver));
0855     strscpy(cap->card, "Trident TM5600/6000/6010", sizeof(cap->card));
0856     usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
0857     cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
0858                 V4L2_CAP_DEVICE_CAPS;
0859     if (dev->tuner_type != TUNER_ABSENT)
0860         cap->capabilities |= V4L2_CAP_TUNER;
0861     if (dev->caps.has_radio)
0862         cap->capabilities |= V4L2_CAP_RADIO;
0863 
0864     return 0;
0865 }
0866 
0867 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
0868                     struct v4l2_fmtdesc *f)
0869 {
0870     if (f->index >= ARRAY_SIZE(format))
0871         return -EINVAL;
0872 
0873     f->pixelformat = format[f->index].fourcc;
0874     return 0;
0875 }
0876 
0877 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
0878                     struct v4l2_format *f)
0879 {
0880     struct tm6000_fh  *fh = priv;
0881 
0882     f->fmt.pix.width        = fh->width;
0883     f->fmt.pix.height       = fh->height;
0884     f->fmt.pix.field        = fh->vb_vidq.field;
0885     f->fmt.pix.pixelformat  = fh->fmt->fourcc;
0886     f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
0887     f->fmt.pix.bytesperline =
0888         (f->fmt.pix.width * fh->fmt->depth) >> 3;
0889     f->fmt.pix.sizeimage =
0890         f->fmt.pix.height * f->fmt.pix.bytesperline;
0891 
0892     return 0;
0893 }
0894 
0895 static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
0896 {
0897     unsigned int i;
0898 
0899     for (i = 0; i < ARRAY_SIZE(format); i++)
0900         if (format[i].fourcc == fourcc)
0901             return format+i;
0902     return NULL;
0903 }
0904 
0905 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
0906             struct v4l2_format *f)
0907 {
0908     struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
0909     struct tm6000_fmt *fmt;
0910     enum v4l2_field field;
0911 
0912     fmt = format_by_fourcc(f->fmt.pix.pixelformat);
0913     if (NULL == fmt) {
0914         dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
0915             f->fmt.pix.pixelformat);
0916         return -EINVAL;
0917     }
0918 
0919     field = f->fmt.pix.field;
0920 
0921     field = V4L2_FIELD_INTERLACED;
0922 
0923     tm6000_get_std_res(dev);
0924 
0925     f->fmt.pix.width  = dev->width;
0926     f->fmt.pix.height = dev->height;
0927 
0928     f->fmt.pix.width &= ~0x01;
0929 
0930     f->fmt.pix.field = field;
0931 
0932     f->fmt.pix.bytesperline =
0933         (f->fmt.pix.width * fmt->depth) >> 3;
0934     f->fmt.pix.sizeimage =
0935         f->fmt.pix.height * f->fmt.pix.bytesperline;
0936     f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
0937 
0938     return 0;
0939 }
0940 
0941 /*FIXME: This seems to be generic enough to be at videodev2 */
0942 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
0943                     struct v4l2_format *f)
0944 {
0945     struct tm6000_fh  *fh = priv;
0946     struct tm6000_core *dev = fh->dev;
0947     int ret = vidioc_try_fmt_vid_cap(file, fh, f);
0948     if (ret < 0)
0949         return ret;
0950 
0951     fh->fmt           = format_by_fourcc(f->fmt.pix.pixelformat);
0952     fh->width         = f->fmt.pix.width;
0953     fh->height        = f->fmt.pix.height;
0954     fh->vb_vidq.field = f->fmt.pix.field;
0955     fh->type          = f->type;
0956 
0957     dev->fourcc       = f->fmt.pix.pixelformat;
0958 
0959     tm6000_set_fourcc_format(dev);
0960 
0961     return 0;
0962 }
0963 
0964 static int vidioc_reqbufs(struct file *file, void *priv,
0965                struct v4l2_requestbuffers *p)
0966 {
0967     struct tm6000_fh  *fh = priv;
0968 
0969     return videobuf_reqbufs(&fh->vb_vidq, p);
0970 }
0971 
0972 static int vidioc_querybuf(struct file *file, void *priv,
0973                 struct v4l2_buffer *p)
0974 {
0975     struct tm6000_fh  *fh = priv;
0976 
0977     return videobuf_querybuf(&fh->vb_vidq, p);
0978 }
0979 
0980 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
0981 {
0982     struct tm6000_fh  *fh = priv;
0983 
0984     return videobuf_qbuf(&fh->vb_vidq, p);
0985 }
0986 
0987 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
0988 {
0989     struct tm6000_fh  *fh = priv;
0990 
0991     return videobuf_dqbuf(&fh->vb_vidq, p,
0992                 file->f_flags & O_NONBLOCK);
0993 }
0994 
0995 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
0996 {
0997     struct tm6000_fh *fh = priv;
0998     struct tm6000_core *dev = fh->dev;
0999 
1000     if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1001         return -EINVAL;
1002     if (i != fh->type)
1003         return -EINVAL;
1004 
1005     if (!res_get(dev, fh, false))
1006         return -EBUSY;
1007     return videobuf_streamon(&fh->vb_vidq);
1008 }
1009 
1010 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1011 {
1012     struct tm6000_fh *fh = priv;
1013     struct tm6000_core *dev = fh->dev;
1014 
1015     if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1016         return -EINVAL;
1017 
1018     if (i != fh->type)
1019         return -EINVAL;
1020 
1021     videobuf_streamoff(&fh->vb_vidq);
1022     res_free(dev, fh);
1023 
1024     return 0;
1025 }
1026 
1027 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1028 {
1029     int rc = 0;
1030     struct tm6000_fh *fh = priv;
1031     struct tm6000_core *dev = fh->dev;
1032 
1033     dev->norm = norm;
1034     rc = tm6000_init_analog_mode(dev);
1035 
1036     fh->width  = dev->width;
1037     fh->height = dev->height;
1038 
1039     if (rc < 0)
1040         return rc;
1041 
1042     v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
1043 
1044     return 0;
1045 }
1046 
1047 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1048 {
1049     struct tm6000_fh *fh = priv;
1050     struct tm6000_core *dev = fh->dev;
1051 
1052     *norm = dev->norm;
1053     return 0;
1054 }
1055 
1056 static const char *iname[] = {
1057     [TM6000_INPUT_TV] = "Television",
1058     [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1059     [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1060     [TM6000_INPUT_SVIDEO] = "S-Video",
1061 };
1062 
1063 static int vidioc_enum_input(struct file *file, void *priv,
1064                 struct v4l2_input *i)
1065 {
1066     struct tm6000_fh   *fh = priv;
1067     struct tm6000_core *dev = fh->dev;
1068     unsigned int n;
1069 
1070     n = i->index;
1071     if (n >= 3)
1072         return -EINVAL;
1073 
1074     if (!dev->vinput[n].type)
1075         return -EINVAL;
1076 
1077     i->index = n;
1078 
1079     if (dev->vinput[n].type == TM6000_INPUT_TV)
1080         i->type = V4L2_INPUT_TYPE_TUNER;
1081     else
1082         i->type = V4L2_INPUT_TYPE_CAMERA;
1083 
1084     strscpy(i->name, iname[dev->vinput[n].type], sizeof(i->name));
1085 
1086     i->std = TM6000_STD;
1087 
1088     return 0;
1089 }
1090 
1091 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1092 {
1093     struct tm6000_fh   *fh = priv;
1094     struct tm6000_core *dev = fh->dev;
1095 
1096     *i = dev->input;
1097 
1098     return 0;
1099 }
1100 
1101 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1102 {
1103     struct tm6000_fh   *fh = priv;
1104     struct tm6000_core *dev = fh->dev;
1105     int rc = 0;
1106 
1107     if (i >= 3)
1108         return -EINVAL;
1109     if (!dev->vinput[i].type)
1110         return -EINVAL;
1111 
1112     dev->input = i;
1113 
1114     rc = vidioc_s_std(file, priv, dev->norm);
1115 
1116     return rc;
1117 }
1118 
1119 /* --- controls ---------------------------------------------- */
1120 
1121 static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
1122 {
1123     struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
1124     u8  val = ctrl->val;
1125 
1126     switch (ctrl->id) {
1127     case V4L2_CID_CONTRAST:
1128         tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1129         return 0;
1130     case V4L2_CID_BRIGHTNESS:
1131         tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1132         return 0;
1133     case V4L2_CID_SATURATION:
1134         tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1135         return 0;
1136     case V4L2_CID_HUE:
1137         tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1138         return 0;
1139     }
1140     return -EINVAL;
1141 }
1142 
1143 static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
1144     .s_ctrl = tm6000_s_ctrl,
1145 };
1146 
1147 static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
1148 {
1149     struct tm6000_core *dev = container_of(ctrl->handler,
1150             struct tm6000_core, radio_ctrl_handler);
1151     u8  val = ctrl->val;
1152 
1153     switch (ctrl->id) {
1154     case V4L2_CID_AUDIO_MUTE:
1155         dev->ctl_mute = val;
1156         tm6000_tvaudio_set_mute(dev, val);
1157         return 0;
1158     case V4L2_CID_AUDIO_VOLUME:
1159         dev->ctl_volume = val;
1160         tm6000_set_volume(dev, val);
1161         return 0;
1162     }
1163     return -EINVAL;
1164 }
1165 
1166 static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
1167     .s_ctrl = tm6000_radio_s_ctrl,
1168 };
1169 
1170 static int vidioc_g_tuner(struct file *file, void *priv,
1171                 struct v4l2_tuner *t)
1172 {
1173     struct tm6000_fh   *fh  = priv;
1174     struct tm6000_core *dev = fh->dev;
1175 
1176     if (UNSET == dev->tuner_type)
1177         return -ENOTTY;
1178     if (0 != t->index)
1179         return -EINVAL;
1180 
1181     strscpy(t->name, "Television", sizeof(t->name));
1182     t->type       = V4L2_TUNER_ANALOG_TV;
1183     t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
1184     t->rangehigh  = 0xffffffffUL;
1185     t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1186 
1187     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1188 
1189     t->audmode = dev->amode;
1190 
1191     return 0;
1192 }
1193 
1194 static int vidioc_s_tuner(struct file *file, void *priv,
1195                 const struct v4l2_tuner *t)
1196 {
1197     struct tm6000_fh   *fh  = priv;
1198     struct tm6000_core *dev = fh->dev;
1199 
1200     if (UNSET == dev->tuner_type)
1201         return -ENOTTY;
1202     if (0 != t->index)
1203         return -EINVAL;
1204 
1205     if (t->audmode > V4L2_TUNER_MODE_STEREO)
1206         dev->amode = V4L2_TUNER_MODE_STEREO;
1207     else
1208         dev->amode = t->audmode;
1209     dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1210 
1211     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1212 
1213     return 0;
1214 }
1215 
1216 static int vidioc_g_frequency(struct file *file, void *priv,
1217                 struct v4l2_frequency *f)
1218 {
1219     struct tm6000_fh   *fh  = priv;
1220     struct tm6000_core *dev = fh->dev;
1221 
1222     if (UNSET == dev->tuner_type)
1223         return -ENOTTY;
1224     if (f->tuner)
1225         return -EINVAL;
1226 
1227     f->frequency = dev->freq;
1228 
1229     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1230 
1231     return 0;
1232 }
1233 
1234 static int vidioc_s_frequency(struct file *file, void *priv,
1235                 const struct v4l2_frequency *f)
1236 {
1237     struct tm6000_fh   *fh  = priv;
1238     struct tm6000_core *dev = fh->dev;
1239 
1240     if (UNSET == dev->tuner_type)
1241         return -ENOTTY;
1242     if (f->tuner != 0)
1243         return -EINVAL;
1244 
1245     dev->freq = f->frequency;
1246     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1247 
1248     return 0;
1249 }
1250 
1251 static int radio_g_tuner(struct file *file, void *priv,
1252                     struct v4l2_tuner *t)
1253 {
1254     struct tm6000_fh *fh = file->private_data;
1255     struct tm6000_core *dev = fh->dev;
1256 
1257     if (0 != t->index)
1258         return -EINVAL;
1259 
1260     memset(t, 0, sizeof(*t));
1261     strscpy(t->name, "Radio", sizeof(t->name));
1262     t->type = V4L2_TUNER_RADIO;
1263     t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1264     t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1265     t->audmode = V4L2_TUNER_MODE_STEREO;
1266 
1267     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1268 
1269     return 0;
1270 }
1271 
1272 static int radio_s_tuner(struct file *file, void *priv,
1273                     const struct v4l2_tuner *t)
1274 {
1275     struct tm6000_fh *fh = file->private_data;
1276     struct tm6000_core *dev = fh->dev;
1277 
1278     if (0 != t->index)
1279         return -EINVAL;
1280     v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1281     return 0;
1282 }
1283 
1284 /* ------------------------------------------------------------------
1285     File operations for the device
1286    ------------------------------------------------------------------*/
1287 
1288 static int __tm6000_open(struct file *file)
1289 {
1290     struct video_device *vdev = video_devdata(file);
1291     struct tm6000_core *dev = video_drvdata(file);
1292     struct tm6000_fh *fh;
1293     enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1294     int rc;
1295     int radio = 0;
1296 
1297     dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1298         video_device_node_name(vdev));
1299 
1300     switch (vdev->vfl_type) {
1301     case VFL_TYPE_VIDEO:
1302         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1303         break;
1304     case VFL_TYPE_VBI:
1305         type = V4L2_BUF_TYPE_VBI_CAPTURE;
1306         break;
1307     case VFL_TYPE_RADIO:
1308         radio = 1;
1309         break;
1310     default:
1311         return -EINVAL;
1312     }
1313 
1314     /* If more than one user, mutex should be added */
1315     dev->users++;
1316 
1317     dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1318         video_device_node_name(vdev), v4l2_type_names[type],
1319         dev->users);
1320 
1321     /* allocate + initialize per filehandle data */
1322     fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1323     if (NULL == fh) {
1324         dev->users--;
1325         return -ENOMEM;
1326     }
1327 
1328     v4l2_fh_init(&fh->fh, vdev);
1329     file->private_data = fh;
1330     fh->dev      = dev;
1331     fh->radio    = radio;
1332     dev->radio   = radio;
1333     fh->type     = type;
1334     dev->fourcc  = format[0].fourcc;
1335 
1336     fh->fmt      = format_by_fourcc(dev->fourcc);
1337 
1338     tm6000_get_std_res(dev);
1339 
1340     fh->width = dev->width;
1341     fh->height = dev->height;
1342 
1343     dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=%p, dev=%p, dev->vidq=%p\n",
1344         fh, dev, &dev->vidq);
1345     dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
1346         list_empty(&dev->vidq.queued));
1347     dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
1348         list_empty(&dev->vidq.active));
1349 
1350     /* initialize hardware on analog mode */
1351     rc = tm6000_init_analog_mode(dev);
1352     if (rc < 0) {
1353         v4l2_fh_exit(&fh->fh);
1354         kfree(fh);
1355         return rc;
1356     }
1357 
1358     dev->mode = TM6000_MODE_ANALOG;
1359 
1360     if (!fh->radio) {
1361         videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1362                 NULL, &dev->slock,
1363                 fh->type,
1364                 V4L2_FIELD_INTERLACED,
1365                 sizeof(struct tm6000_buffer), fh, &dev->lock);
1366     } else {
1367         dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
1368         tm6000_set_audio_rinput(dev);
1369         v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1370         tm6000_prepare_isoc(dev);
1371         tm6000_start_thread(dev);
1372     }
1373     v4l2_fh_add(&fh->fh);
1374 
1375     return 0;
1376 }
1377 
1378 static int tm6000_open(struct file *file)
1379 {
1380     struct video_device *vdev = video_devdata(file);
1381     int res;
1382 
1383     mutex_lock(vdev->lock);
1384     res = __tm6000_open(file);
1385     mutex_unlock(vdev->lock);
1386     return res;
1387 }
1388 
1389 static ssize_t
1390 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1391 {
1392     struct tm6000_fh *fh = file->private_data;
1393     struct tm6000_core *dev = fh->dev;
1394 
1395     if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1396         int res;
1397 
1398         if (!res_get(fh->dev, fh, true))
1399             return -EBUSY;
1400 
1401         if (mutex_lock_interruptible(&dev->lock))
1402             return -ERESTARTSYS;
1403         res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1404                     file->f_flags & O_NONBLOCK);
1405         mutex_unlock(&dev->lock);
1406         return res;
1407     }
1408     return 0;
1409 }
1410 
1411 static __poll_t
1412 __tm6000_poll(struct file *file, struct poll_table_struct *wait)
1413 {
1414     __poll_t req_events = poll_requested_events(wait);
1415     struct tm6000_fh        *fh = file->private_data;
1416     struct tm6000_buffer    *buf;
1417     __poll_t res = 0;
1418 
1419     if (v4l2_event_pending(&fh->fh))
1420         res = EPOLLPRI;
1421     else if (req_events & EPOLLPRI)
1422         poll_wait(file, &fh->fh.wait, wait);
1423     if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1424         return res | EPOLLERR;
1425 
1426     if (!!is_res_streaming(fh->dev, fh))
1427         return res | EPOLLERR;
1428 
1429     if (!is_res_read(fh->dev, fh)) {
1430         /* streaming capture */
1431         if (list_empty(&fh->vb_vidq.stream))
1432             return res | EPOLLERR;
1433         buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
1434         poll_wait(file, &buf->vb.done, wait);
1435         if (buf->vb.state == VIDEOBUF_DONE ||
1436             buf->vb.state == VIDEOBUF_ERROR)
1437             return res | EPOLLIN | EPOLLRDNORM;
1438     } else if (req_events & (EPOLLIN | EPOLLRDNORM)) {
1439         /* read() capture */
1440         return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
1441     }
1442     return res;
1443 }
1444 
1445 static __poll_t tm6000_poll(struct file *file, struct poll_table_struct *wait)
1446 {
1447     struct tm6000_fh *fh = file->private_data;
1448     struct tm6000_core *dev = fh->dev;
1449     __poll_t res;
1450 
1451     mutex_lock(&dev->lock);
1452     res = __tm6000_poll(file, wait);
1453     mutex_unlock(&dev->lock);
1454     return res;
1455 }
1456 
1457 static int tm6000_release(struct file *file)
1458 {
1459     struct tm6000_fh         *fh = file->private_data;
1460     struct tm6000_core      *dev = fh->dev;
1461     struct video_device    *vdev = video_devdata(file);
1462 
1463     dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1464         video_device_node_name(vdev), dev->users);
1465 
1466     mutex_lock(&dev->lock);
1467     dev->users--;
1468 
1469     res_free(dev, fh);
1470 
1471     if (!dev->users) {
1472         tm6000_uninit_isoc(dev);
1473 
1474         /* Stop interrupt USB pipe */
1475         tm6000_ir_int_stop(dev);
1476 
1477         usb_reset_configuration(dev->udev);
1478 
1479         if (dev->int_in.endp)
1480             usb_set_interface(dev->udev,
1481                     dev->isoc_in.bInterfaceNumber, 2);
1482         else
1483             usb_set_interface(dev->udev,
1484                     dev->isoc_in.bInterfaceNumber, 0);
1485 
1486         /* Start interrupt USB pipe */
1487         tm6000_ir_int_start(dev);
1488 
1489         if (!fh->radio)
1490             videobuf_mmap_free(&fh->vb_vidq);
1491     }
1492     v4l2_fh_del(&fh->fh);
1493     v4l2_fh_exit(&fh->fh);
1494     kfree(fh);
1495     mutex_unlock(&dev->lock);
1496 
1497     return 0;
1498 }
1499 
1500 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1501 {
1502     struct tm6000_fh *fh = file->private_data;
1503     struct tm6000_core *dev = fh->dev;
1504     int res;
1505 
1506     if (mutex_lock_interruptible(&dev->lock))
1507         return -ERESTARTSYS;
1508     res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1509     mutex_unlock(&dev->lock);
1510     return res;
1511 }
1512 
1513 static const struct v4l2_file_operations tm6000_fops = {
1514     .owner = THIS_MODULE,
1515     .open = tm6000_open,
1516     .release = tm6000_release,
1517     .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1518     .read = tm6000_read,
1519     .poll = tm6000_poll,
1520     .mmap = tm6000_mmap,
1521 };
1522 
1523 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1524     .vidioc_querycap          = vidioc_querycap,
1525     .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1526     .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1527     .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1528     .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1529     .vidioc_s_std             = vidioc_s_std,
1530     .vidioc_g_std             = vidioc_g_std,
1531     .vidioc_enum_input        = vidioc_enum_input,
1532     .vidioc_g_input           = vidioc_g_input,
1533     .vidioc_s_input           = vidioc_s_input,
1534     .vidioc_g_tuner           = vidioc_g_tuner,
1535     .vidioc_s_tuner           = vidioc_s_tuner,
1536     .vidioc_g_frequency       = vidioc_g_frequency,
1537     .vidioc_s_frequency       = vidioc_s_frequency,
1538     .vidioc_streamon          = vidioc_streamon,
1539     .vidioc_streamoff         = vidioc_streamoff,
1540     .vidioc_reqbufs           = vidioc_reqbufs,
1541     .vidioc_querybuf          = vidioc_querybuf,
1542     .vidioc_qbuf              = vidioc_qbuf,
1543     .vidioc_dqbuf             = vidioc_dqbuf,
1544     .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1545     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1546 };
1547 
1548 static struct video_device tm6000_template = {
1549     .name       = "tm6000",
1550     .fops           = &tm6000_fops,
1551     .ioctl_ops      = &video_ioctl_ops,
1552     .release    = video_device_release_empty,
1553     .tvnorms        = TM6000_STD,
1554 };
1555 
1556 static const struct v4l2_file_operations radio_fops = {
1557     .owner      = THIS_MODULE,
1558     .open       = tm6000_open,
1559     .poll       = v4l2_ctrl_poll,
1560     .release    = tm6000_release,
1561     .unlocked_ioctl = video_ioctl2,
1562 };
1563 
1564 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1565     .vidioc_querycap    = vidioc_querycap,
1566     .vidioc_g_tuner     = radio_g_tuner,
1567     .vidioc_s_tuner     = radio_s_tuner,
1568     .vidioc_g_frequency = vidioc_g_frequency,
1569     .vidioc_s_frequency = vidioc_s_frequency,
1570     .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1571     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1572 };
1573 
1574 static struct video_device tm6000_radio_template = {
1575     .name           = "tm6000",
1576     .fops           = &radio_fops,
1577     .ioctl_ops      = &radio_ioctl_ops,
1578 };
1579 
1580 /* -----------------------------------------------------------------
1581  *  Initialization and module stuff
1582  * ------------------------------------------------------------------
1583  */
1584 
1585 static void vdev_init(struct tm6000_core *dev,
1586         struct video_device *vfd,
1587         const struct video_device
1588         *template, const char *type_name)
1589 {
1590     *vfd = *template;
1591     vfd->v4l2_dev = &dev->v4l2_dev;
1592     vfd->release = video_device_release_empty;
1593     vfd->lock = &dev->lock;
1594 
1595     snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1596 
1597     video_set_drvdata(vfd, dev);
1598 }
1599 
1600 int tm6000_v4l2_register(struct tm6000_core *dev)
1601 {
1602     int ret = 0;
1603 
1604     v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1605     v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1606     v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1607             V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1608     v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1609             V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1610     v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1611             V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1612     v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1613             V4L2_CID_CONTRAST, 0, 255, 1, 119);
1614     v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1615             V4L2_CID_SATURATION, 0, 255, 1, 112);
1616     v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1617             V4L2_CID_HUE, -128, 127, 1, 0);
1618     v4l2_ctrl_add_handler(&dev->ctrl_handler,
1619             &dev->radio_ctrl_handler, NULL, false);
1620 
1621     if (dev->radio_ctrl_handler.error)
1622         ret = dev->radio_ctrl_handler.error;
1623     if (!ret && dev->ctrl_handler.error)
1624         ret = dev->ctrl_handler.error;
1625     if (ret)
1626         goto free_ctrl;
1627 
1628     vdev_init(dev, &dev->vfd, &tm6000_template, "video");
1629 
1630     dev->vfd.ctrl_handler = &dev->ctrl_handler;
1631     dev->vfd.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1632                    V4L2_CAP_READWRITE;
1633     if (dev->tuner_type != TUNER_ABSENT)
1634         dev->vfd.device_caps |= V4L2_CAP_TUNER;
1635 
1636     /* init video dma queues */
1637     INIT_LIST_HEAD(&dev->vidq.active);
1638     INIT_LIST_HEAD(&dev->vidq.queued);
1639 
1640     ret = video_register_device(&dev->vfd, VFL_TYPE_VIDEO, video_nr);
1641 
1642     if (ret < 0) {
1643         printk(KERN_INFO "%s: can't register video device\n",
1644                dev->name);
1645         goto free_ctrl;
1646     }
1647 
1648     printk(KERN_INFO "%s: registered device %s\n",
1649            dev->name, video_device_node_name(&dev->vfd));
1650 
1651     if (dev->caps.has_radio) {
1652         vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
1653                                "radio");
1654         dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1655         dev->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
1656         ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
1657                         radio_nr);
1658         if (ret < 0) {
1659             printk(KERN_INFO "%s: can't register radio device\n",
1660                    dev->name);
1661             goto unreg_video;
1662         }
1663 
1664         printk(KERN_INFO "%s: registered device %s\n",
1665                dev->name, video_device_node_name(&dev->radio_dev));
1666     }
1667 
1668     printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1669     return ret;
1670 
1671 unreg_video:
1672     video_unregister_device(&dev->vfd);
1673 free_ctrl:
1674     v4l2_ctrl_handler_free(&dev->ctrl_handler);
1675     v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1676     return ret;
1677 }
1678 
1679 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1680 {
1681     video_unregister_device(&dev->vfd);
1682 
1683     /* if URB buffers are still allocated free them now */
1684     tm6000_free_urb_buffers(dev);
1685 
1686     video_unregister_device(&dev->radio_dev);
1687     return 0;
1688 }
1689 
1690 int tm6000_v4l2_exit(void)
1691 {
1692     return 0;
1693 }
1694 
1695 module_param(video_nr, int, 0);
1696 MODULE_PARM_DESC(video_nr, "Allow changing video device number");
1697 
1698 module_param_named(debug, tm6000_debug, int, 0444);
1699 MODULE_PARM_DESC(debug, "activates debug info");
1700 
1701 module_param(vid_limit, int, 0644);
1702 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
1703 
1704 module_param(keep_urb, bool, 0);
1705 MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");