Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Mirics MSi2500 driver
0004  * Mirics MSi3101 SDR Dongle driver
0005  *
0006  * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
0007  *
0008  * That driver is somehow based of pwc driver:
0009  *  (C) 1999-2004 Nemosoft Unv.
0010  *  (C) 2004-2006 Luc Saillard (luc@saillard.org)
0011  *  (C) 2011 Hans de Goede <hdegoede@redhat.com>
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/slab.h>
0016 #include <asm/div64.h>
0017 #include <media/v4l2-device.h>
0018 #include <media/v4l2-ioctl.h>
0019 #include <media/v4l2-ctrls.h>
0020 #include <media/v4l2-event.h>
0021 #include <linux/usb.h>
0022 #include <media/videobuf2-v4l2.h>
0023 #include <media/videobuf2-vmalloc.h>
0024 #include <linux/spi/spi.h>
0025 
0026 static bool msi2500_emulated_fmt;
0027 module_param_named(emulated_formats, msi2500_emulated_fmt, bool, 0644);
0028 MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
0029 
0030 /*
0031  *   iConfiguration          0
0032  *     bInterfaceNumber        0
0033  *     bAlternateSetting       1
0034  *     bNumEndpoints           1
0035  *       bEndpointAddress     0x81  EP 1 IN
0036  *       bmAttributes            1
0037  *         Transfer Type            Isochronous
0038  *       wMaxPacketSize     0x1400  3x 1024 bytes
0039  *       bInterval               1
0040  */
0041 #define MAX_ISO_BUFS            (8)
0042 #define ISO_FRAMES_PER_DESC     (8)
0043 #define ISO_MAX_FRAME_SIZE      (3 * 1024)
0044 #define ISO_BUFFER_SIZE         (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
0045 #define MAX_ISOC_ERRORS         20
0046 
0047 /*
0048  * TODO: These formats should be moved to V4L2 API. Formats are currently
0049  * disabled from formats[] table, not visible to userspace.
0050  */
0051  /* signed 12-bit */
0052 #define MSI2500_PIX_FMT_SDR_S12         v4l2_fourcc('D', 'S', '1', '2')
0053 /* Mirics MSi2500 format 384 */
0054 #define MSI2500_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4')
0055 
0056 static const struct v4l2_frequency_band bands[] = {
0057     {
0058         .tuner = 0,
0059         .type = V4L2_TUNER_ADC,
0060         .index = 0,
0061         .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
0062         .rangelow   =  1200000,
0063         .rangehigh  = 15000000,
0064     },
0065 };
0066 
0067 /* stream formats */
0068 struct msi2500_format {
0069     u32 pixelformat;
0070     u32 buffersize;
0071 };
0072 
0073 /* format descriptions for capture and preview */
0074 static struct msi2500_format formats[] = {
0075     {
0076         .pixelformat    = V4L2_SDR_FMT_CS8,
0077         .buffersize = 3 * 1008,
0078 #if 0
0079     }, {
0080         .pixelformat    = MSI2500_PIX_FMT_SDR_MSI2500_384,
0081     }, {
0082         .pixelformat    = MSI2500_PIX_FMT_SDR_S12,
0083 #endif
0084     }, {
0085         .pixelformat    = V4L2_SDR_FMT_CS14LE,
0086         .buffersize = 3 * 1008,
0087     }, {
0088         .pixelformat    = V4L2_SDR_FMT_CU8,
0089         .buffersize = 3 * 1008,
0090     }, {
0091         .pixelformat    =  V4L2_SDR_FMT_CU16LE,
0092         .buffersize = 3 * 1008,
0093     },
0094 };
0095 
0096 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
0097 
0098 /* intermediate buffers with raw data from the USB device */
0099 struct msi2500_frame_buf {
0100     /* common v4l buffer stuff -- must be first */
0101     struct vb2_v4l2_buffer vb;
0102     struct list_head list;
0103 };
0104 
0105 struct msi2500_dev {
0106     struct device *dev;
0107     struct video_device vdev;
0108     struct v4l2_device v4l2_dev;
0109     struct v4l2_subdev *v4l2_subdev;
0110     struct spi_master *master;
0111 
0112     /* videobuf2 queue and queued buffers list */
0113     struct vb2_queue vb_queue;
0114     struct list_head queued_bufs;
0115     spinlock_t queued_bufs_lock; /* Protects queued_bufs */
0116 
0117     /* Note if taking both locks v4l2_lock must always be locked first! */
0118     struct mutex v4l2_lock;      /* Protects everything else */
0119     struct mutex vb_queue_lock;  /* Protects vb_queue and capt_file */
0120 
0121     /* Pointer to our usb_device, will be NULL after unplug */
0122     struct usb_device *udev; /* Both mutexes most be hold when setting! */
0123 
0124     unsigned int f_adc;
0125     u32 pixelformat;
0126     u32 buffersize;
0127     unsigned int num_formats;
0128 
0129     unsigned int isoc_errors; /* number of contiguous ISOC errors */
0130     unsigned int vb_full; /* vb is full and packets dropped */
0131 
0132     struct urb *urbs[MAX_ISO_BUFS];
0133 
0134     /* Controls */
0135     struct v4l2_ctrl_handler hdl;
0136 
0137     u32 next_sample; /* for track lost packets */
0138     u32 sample; /* for sample rate calc */
0139     unsigned long jiffies_next;
0140 };
0141 
0142 /* Private functions */
0143 static struct msi2500_frame_buf *msi2500_get_next_fill_buf(
0144                             struct msi2500_dev *dev)
0145 {
0146     unsigned long flags;
0147     struct msi2500_frame_buf *buf = NULL;
0148 
0149     spin_lock_irqsave(&dev->queued_bufs_lock, flags);
0150     if (list_empty(&dev->queued_bufs))
0151         goto leave;
0152 
0153     buf = list_entry(dev->queued_bufs.next, struct msi2500_frame_buf, list);
0154     list_del(&buf->list);
0155 leave:
0156     spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
0157     return buf;
0158 }
0159 
0160 /*
0161  * +===========================================================================
0162  * |   00-1023 | USB packet type '504'
0163  * +===========================================================================
0164  * |   00-  03 | sequence number of first sample in that USB packet
0165  * +---------------------------------------------------------------------------
0166  * |   04-  15 | garbage
0167  * +---------------------------------------------------------------------------
0168  * |   16-1023 | samples
0169  * +---------------------------------------------------------------------------
0170  * signed 8-bit sample
0171  * 504 * 2 = 1008 samples
0172  *
0173  *
0174  * +===========================================================================
0175  * |   00-1023 | USB packet type '384'
0176  * +===========================================================================
0177  * |   00-  03 | sequence number of first sample in that USB packet
0178  * +---------------------------------------------------------------------------
0179  * |   04-  15 | garbage
0180  * +---------------------------------------------------------------------------
0181  * |   16- 175 | samples
0182  * +---------------------------------------------------------------------------
0183  * |  176- 179 | control bits for previous samples
0184  * +---------------------------------------------------------------------------
0185  * |  180- 339 | samples
0186  * +---------------------------------------------------------------------------
0187  * |  340- 343 | control bits for previous samples
0188  * +---------------------------------------------------------------------------
0189  * |  344- 503 | samples
0190  * +---------------------------------------------------------------------------
0191  * |  504- 507 | control bits for previous samples
0192  * +---------------------------------------------------------------------------
0193  * |  508- 667 | samples
0194  * +---------------------------------------------------------------------------
0195  * |  668- 671 | control bits for previous samples
0196  * +---------------------------------------------------------------------------
0197  * |  672- 831 | samples
0198  * +---------------------------------------------------------------------------
0199  * |  832- 835 | control bits for previous samples
0200  * +---------------------------------------------------------------------------
0201  * |  836- 995 | samples
0202  * +---------------------------------------------------------------------------
0203  * |  996- 999 | control bits for previous samples
0204  * +---------------------------------------------------------------------------
0205  * | 1000-1023 | garbage
0206  * +---------------------------------------------------------------------------
0207  *
0208  * Bytes 4 - 7 could have some meaning?
0209  *
0210  * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
0211  * numbers. This results one 2-bit number for 8 samples. It is likely used for
0212  * for bit shifting sample by given bits, increasing actual sampling resolution.
0213  * Number 2 (0b10) was never seen.
0214  *
0215  * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
0216  *
0217  *
0218  * +===========================================================================
0219  * |   00-1023 | USB packet type '336'
0220  * +===========================================================================
0221  * |   00-  03 | sequence number of first sample in that USB packet
0222  * +---------------------------------------------------------------------------
0223  * |   04-  15 | garbage
0224  * +---------------------------------------------------------------------------
0225  * |   16-1023 | samples
0226  * +---------------------------------------------------------------------------
0227  * signed 12-bit sample
0228  *
0229  *
0230  * +===========================================================================
0231  * |   00-1023 | USB packet type '252'
0232  * +===========================================================================
0233  * |   00-  03 | sequence number of first sample in that USB packet
0234  * +---------------------------------------------------------------------------
0235  * |   04-  15 | garbage
0236  * +---------------------------------------------------------------------------
0237  * |   16-1023 | samples
0238  * +---------------------------------------------------------------------------
0239  * signed 14-bit sample
0240  */
0241 
0242 static int msi2500_convert_stream(struct msi2500_dev *dev, u8 *dst, u8 *src,
0243                   unsigned int src_len)
0244 {
0245     unsigned int i, j, transactions, dst_len = 0;
0246     u32 sample[3];
0247 
0248     /* There could be 1-3 1024 byte transactions per packet */
0249     transactions = src_len / 1024;
0250 
0251     for (i = 0; i < transactions; i++) {
0252         sample[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 |
0253                 src[0] << 0;
0254         if (i == 0 && dev->next_sample != sample[0]) {
0255             dev_dbg_ratelimited(dev->dev,
0256                         "%d samples lost, %d %08x:%08x\n",
0257                         sample[0] - dev->next_sample,
0258                         src_len, dev->next_sample,
0259                         sample[0]);
0260         }
0261 
0262         /*
0263          * Dump all unknown 'garbage' data - maybe we will discover
0264          * someday if there is something rational...
0265          */
0266         dev_dbg_ratelimited(dev->dev, "%*ph\n", 12, &src[4]);
0267 
0268         src += 16; /* skip header */
0269 
0270         switch (dev->pixelformat) {
0271         case V4L2_SDR_FMT_CU8: /* 504 x IQ samples */
0272         {
0273             s8 *s8src = (s8 *)src;
0274             u8 *u8dst = (u8 *)dst;
0275 
0276             for (j = 0; j < 1008; j++)
0277                 *u8dst++ = *s8src++ + 128;
0278 
0279             src += 1008;
0280             dst += 1008;
0281             dst_len += 1008;
0282             dev->next_sample = sample[i] + 504;
0283             break;
0284         }
0285         case  V4L2_SDR_FMT_CU16LE: /* 252 x IQ samples */
0286         {
0287             s16 *s16src = (s16 *)src;
0288             u16 *u16dst = (u16 *)dst;
0289             struct {signed int x:14; } se; /* sign extension */
0290             unsigned int utmp;
0291 
0292             for (j = 0; j < 1008; j += 2) {
0293                 /* sign extension from 14-bit to signed int */
0294                 se.x = *s16src++;
0295                 /* from signed int to unsigned int */
0296                 utmp = se.x + 8192;
0297                 /* from 14-bit to 16-bit */
0298                 *u16dst++ = utmp << 2 | utmp >> 12;
0299             }
0300 
0301             src += 1008;
0302             dst += 1008;
0303             dst_len += 1008;
0304             dev->next_sample = sample[i] + 252;
0305             break;
0306         }
0307         case MSI2500_PIX_FMT_SDR_MSI2500_384: /* 384 x IQ samples */
0308             /* Dump unknown 'garbage' data */
0309             dev_dbg_ratelimited(dev->dev, "%*ph\n", 24, &src[1000]);
0310             memcpy(dst, src, 984);
0311             src += 984 + 24;
0312             dst += 984;
0313             dst_len += 984;
0314             dev->next_sample = sample[i] + 384;
0315             break;
0316         case V4L2_SDR_FMT_CS8:         /* 504 x IQ samples */
0317             memcpy(dst, src, 1008);
0318             src += 1008;
0319             dst += 1008;
0320             dst_len += 1008;
0321             dev->next_sample = sample[i] + 504;
0322             break;
0323         case MSI2500_PIX_FMT_SDR_S12:  /* 336 x IQ samples */
0324             memcpy(dst, src, 1008);
0325             src += 1008;
0326             dst += 1008;
0327             dst_len += 1008;
0328             dev->next_sample = sample[i] + 336;
0329             break;
0330         case V4L2_SDR_FMT_CS14LE:      /* 252 x IQ samples */
0331             memcpy(dst, src, 1008);
0332             src += 1008;
0333             dst += 1008;
0334             dst_len += 1008;
0335             dev->next_sample = sample[i] + 252;
0336             break;
0337         default:
0338             break;
0339         }
0340     }
0341 
0342     /* calculate sample rate and output it in 10 seconds intervals */
0343     if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
0344         #define MSECS 10000UL
0345         unsigned int msecs = jiffies_to_msecs(jiffies -
0346                 dev->jiffies_next + msecs_to_jiffies(MSECS));
0347         unsigned int samples = dev->next_sample - dev->sample;
0348 
0349         dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
0350         dev->sample = dev->next_sample;
0351         dev_dbg(dev->dev, "size=%u samples=%u msecs=%u sample rate=%lu\n",
0352             src_len, samples, msecs,
0353             samples * 1000UL / msecs);
0354     }
0355 
0356     return dst_len;
0357 }
0358 
0359 /*
0360  * This gets called for the Isochronous pipe (stream). This is done in interrupt
0361  * time, so it has to be fast, not crash, and not stall. Neat.
0362  */
0363 static void msi2500_isoc_handler(struct urb *urb)
0364 {
0365     struct msi2500_dev *dev = (struct msi2500_dev *)urb->context;
0366     int i, flen, fstatus;
0367     unsigned char *iso_buf = NULL;
0368     struct msi2500_frame_buf *fbuf;
0369 
0370     if (unlikely(urb->status == -ENOENT ||
0371              urb->status == -ECONNRESET ||
0372              urb->status == -ESHUTDOWN)) {
0373         dev_dbg(dev->dev, "URB (%p) unlinked %ssynchronously\n",
0374             urb, urb->status == -ENOENT ? "" : "a");
0375         return;
0376     }
0377 
0378     if (unlikely(urb->status != 0)) {
0379         dev_dbg(dev->dev, "called with status %d\n", urb->status);
0380         /* Give up after a number of contiguous errors */
0381         if (++dev->isoc_errors > MAX_ISOC_ERRORS)
0382             dev_dbg(dev->dev, "Too many ISOC errors, bailing out\n");
0383         goto handler_end;
0384     } else {
0385         /* Reset ISOC error counter. We did get here, after all. */
0386         dev->isoc_errors = 0;
0387     }
0388 
0389     /* Compact data */
0390     for (i = 0; i < urb->number_of_packets; i++) {
0391         void *ptr;
0392 
0393         /* Check frame error */
0394         fstatus = urb->iso_frame_desc[i].status;
0395         if (unlikely(fstatus)) {
0396             dev_dbg_ratelimited(dev->dev,
0397                         "frame=%d/%d has error %d skipping\n",
0398                         i, urb->number_of_packets, fstatus);
0399             continue;
0400         }
0401 
0402         /* Check if that frame contains data */
0403         flen = urb->iso_frame_desc[i].actual_length;
0404         if (unlikely(flen == 0))
0405             continue;
0406 
0407         iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
0408 
0409         /* Get free framebuffer */
0410         fbuf = msi2500_get_next_fill_buf(dev);
0411         if (unlikely(fbuf == NULL)) {
0412             dev->vb_full++;
0413             dev_dbg_ratelimited(dev->dev,
0414                         "videobuf is full, %d packets dropped\n",
0415                         dev->vb_full);
0416             continue;
0417         }
0418 
0419         /* fill framebuffer */
0420         ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
0421         flen = msi2500_convert_stream(dev, ptr, iso_buf, flen);
0422         vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, flen);
0423         vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
0424     }
0425 
0426 handler_end:
0427     i = usb_submit_urb(urb, GFP_ATOMIC);
0428     if (unlikely(i != 0))
0429         dev_dbg(dev->dev, "Error (%d) re-submitting urb\n", i);
0430 }
0431 
0432 static void msi2500_iso_stop(struct msi2500_dev *dev)
0433 {
0434     int i;
0435 
0436     dev_dbg(dev->dev, "\n");
0437 
0438     /* Unlinking ISOC buffers one by one */
0439     for (i = 0; i < MAX_ISO_BUFS; i++) {
0440         if (dev->urbs[i]) {
0441             dev_dbg(dev->dev, "Unlinking URB %p\n", dev->urbs[i]);
0442             usb_kill_urb(dev->urbs[i]);
0443         }
0444     }
0445 }
0446 
0447 static void msi2500_iso_free(struct msi2500_dev *dev)
0448 {
0449     int i;
0450 
0451     dev_dbg(dev->dev, "\n");
0452 
0453     /* Freeing ISOC buffers one by one */
0454     for (i = 0; i < MAX_ISO_BUFS; i++) {
0455         if (dev->urbs[i]) {
0456             dev_dbg(dev->dev, "Freeing URB\n");
0457             if (dev->urbs[i]->transfer_buffer) {
0458                 usb_free_coherent(dev->udev,
0459                     dev->urbs[i]->transfer_buffer_length,
0460                     dev->urbs[i]->transfer_buffer,
0461                     dev->urbs[i]->transfer_dma);
0462             }
0463             usb_free_urb(dev->urbs[i]);
0464             dev->urbs[i] = NULL;
0465         }
0466     }
0467 }
0468 
0469 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
0470 static void msi2500_isoc_cleanup(struct msi2500_dev *dev)
0471 {
0472     dev_dbg(dev->dev, "\n");
0473 
0474     msi2500_iso_stop(dev);
0475     msi2500_iso_free(dev);
0476 }
0477 
0478 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
0479 static int msi2500_isoc_init(struct msi2500_dev *dev)
0480 {
0481     struct urb *urb;
0482     int i, j, ret;
0483 
0484     dev_dbg(dev->dev, "\n");
0485 
0486     dev->isoc_errors = 0;
0487 
0488     ret = usb_set_interface(dev->udev, 0, 1);
0489     if (ret)
0490         return ret;
0491 
0492     /* Allocate and init Isochronuous urbs */
0493     for (i = 0; i < MAX_ISO_BUFS; i++) {
0494         urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
0495         if (urb == NULL) {
0496             msi2500_isoc_cleanup(dev);
0497             return -ENOMEM;
0498         }
0499         dev->urbs[i] = urb;
0500         dev_dbg(dev->dev, "Allocated URB at 0x%p\n", urb);
0501 
0502         urb->interval = 1;
0503         urb->dev = dev->udev;
0504         urb->pipe = usb_rcvisocpipe(dev->udev, 0x81);
0505         urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
0506         urb->transfer_buffer = usb_alloc_coherent(dev->udev,
0507                 ISO_BUFFER_SIZE,
0508                 GFP_KERNEL, &urb->transfer_dma);
0509         if (urb->transfer_buffer == NULL) {
0510             dev_err(dev->dev,
0511                 "Failed to allocate urb buffer %d\n", i);
0512             msi2500_isoc_cleanup(dev);
0513             return -ENOMEM;
0514         }
0515         urb->transfer_buffer_length = ISO_BUFFER_SIZE;
0516         urb->complete = msi2500_isoc_handler;
0517         urb->context = dev;
0518         urb->start_frame = 0;
0519         urb->number_of_packets = ISO_FRAMES_PER_DESC;
0520         for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
0521             urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
0522             urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
0523         }
0524     }
0525 
0526     /* link */
0527     for (i = 0; i < MAX_ISO_BUFS; i++) {
0528         ret = usb_submit_urb(dev->urbs[i], GFP_KERNEL);
0529         if (ret) {
0530             dev_err(dev->dev,
0531                 "usb_submit_urb %d failed with error %d\n",
0532                 i, ret);
0533             msi2500_isoc_cleanup(dev);
0534             return ret;
0535         }
0536         dev_dbg(dev->dev, "URB 0x%p submitted.\n", dev->urbs[i]);
0537     }
0538 
0539     /* All is done... */
0540     return 0;
0541 }
0542 
0543 /* Must be called with vb_queue_lock hold */
0544 static void msi2500_cleanup_queued_bufs(struct msi2500_dev *dev)
0545 {
0546     unsigned long flags;
0547 
0548     dev_dbg(dev->dev, "\n");
0549 
0550     spin_lock_irqsave(&dev->queued_bufs_lock, flags);
0551     while (!list_empty(&dev->queued_bufs)) {
0552         struct msi2500_frame_buf *buf;
0553 
0554         buf = list_entry(dev->queued_bufs.next,
0555                  struct msi2500_frame_buf, list);
0556         list_del(&buf->list);
0557         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0558     }
0559     spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
0560 }
0561 
0562 /* The user yanked out the cable... */
0563 static void msi2500_disconnect(struct usb_interface *intf)
0564 {
0565     struct v4l2_device *v = usb_get_intfdata(intf);
0566     struct msi2500_dev *dev =
0567             container_of(v, struct msi2500_dev, v4l2_dev);
0568 
0569     dev_dbg(dev->dev, "\n");
0570 
0571     mutex_lock(&dev->vb_queue_lock);
0572     mutex_lock(&dev->v4l2_lock);
0573     /* No need to keep the urbs around after disconnection */
0574     dev->udev = NULL;
0575     v4l2_device_disconnect(&dev->v4l2_dev);
0576     video_unregister_device(&dev->vdev);
0577     spi_unregister_master(dev->master);
0578     mutex_unlock(&dev->v4l2_lock);
0579     mutex_unlock(&dev->vb_queue_lock);
0580 
0581     v4l2_device_put(&dev->v4l2_dev);
0582 }
0583 
0584 static int msi2500_querycap(struct file *file, void *fh,
0585                 struct v4l2_capability *cap)
0586 {
0587     struct msi2500_dev *dev = video_drvdata(file);
0588 
0589     dev_dbg(dev->dev, "\n");
0590 
0591     strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
0592     strscpy(cap->card, dev->vdev.name, sizeof(cap->card));
0593     usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
0594     return 0;
0595 }
0596 
0597 /* Videobuf2 operations */
0598 static int msi2500_queue_setup(struct vb2_queue *vq,
0599                    unsigned int *nbuffers,
0600                    unsigned int *nplanes, unsigned int sizes[],
0601                    struct device *alloc_devs[])
0602 {
0603     struct msi2500_dev *dev = vb2_get_drv_priv(vq);
0604 
0605     dev_dbg(dev->dev, "nbuffers=%d\n", *nbuffers);
0606 
0607     /* Absolute min and max number of buffers available for mmap() */
0608     *nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32);
0609     *nplanes = 1;
0610     sizes[0] = PAGE_ALIGN(dev->buffersize);
0611     dev_dbg(dev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
0612     return 0;
0613 }
0614 
0615 static void msi2500_buf_queue(struct vb2_buffer *vb)
0616 {
0617     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0618     struct msi2500_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
0619     struct msi2500_frame_buf *buf = container_of(vbuf,
0620                              struct msi2500_frame_buf,
0621                              vb);
0622     unsigned long flags;
0623 
0624     /* Check the device has not disconnected between prep and queuing */
0625     if (unlikely(!dev->udev)) {
0626         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0627         return;
0628     }
0629 
0630     spin_lock_irqsave(&dev->queued_bufs_lock, flags);
0631     list_add_tail(&buf->list, &dev->queued_bufs);
0632     spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
0633 }
0634 
0635 #define CMD_WREG               0x41
0636 #define CMD_START_STREAMING    0x43
0637 #define CMD_STOP_STREAMING     0x45
0638 #define CMD_READ_UNKNOWN       0x48
0639 
0640 #define msi2500_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
0641     char *_direction; \
0642     if (_t & USB_DIR_IN) \
0643         _direction = "<<<"; \
0644     else \
0645         _direction = ">>>"; \
0646     dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
0647             _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \
0648             _l & 0xff, _l >> 8, _direction, _l, _b); \
0649 }
0650 
0651 static int msi2500_ctrl_msg(struct msi2500_dev *dev, u8 cmd, u32 data)
0652 {
0653     int ret;
0654     u8 request = cmd;
0655     u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
0656     u16 value = (data >> 0) & 0xffff;
0657     u16 index = (data >> 16) & 0xffff;
0658 
0659     msi2500_dbg_usb_control_msg(dev->dev, request, requesttype,
0660                     value, index, NULL, 0);
0661     ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), request,
0662                   requesttype, value, index, NULL, 0, 2000);
0663     if (ret)
0664         dev_err(dev->dev, "failed %d, cmd %02x, data %04x\n",
0665             ret, cmd, data);
0666 
0667     return ret;
0668 }
0669 
0670 static int msi2500_set_usb_adc(struct msi2500_dev *dev)
0671 {
0672     int ret;
0673     unsigned int f_vco, f_sr, div_n, k, k_cw, div_out;
0674     u32 reg3, reg4, reg7;
0675     struct v4l2_ctrl *bandwidth_auto;
0676     struct v4l2_ctrl *bandwidth;
0677 
0678     f_sr = dev->f_adc;
0679 
0680     /* set tuner, subdev, filters according to sampling rate */
0681     bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
0682             V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
0683     if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
0684         bandwidth = v4l2_ctrl_find(&dev->hdl,
0685                 V4L2_CID_RF_TUNER_BANDWIDTH);
0686         v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
0687     }
0688 
0689     /* select stream format */
0690     switch (dev->pixelformat) {
0691     case V4L2_SDR_FMT_CU8:
0692         reg7 = 0x000c9407; /* 504 */
0693         break;
0694     case  V4L2_SDR_FMT_CU16LE:
0695         reg7 = 0x00009407; /* 252 */
0696         break;
0697     case V4L2_SDR_FMT_CS8:
0698         reg7 = 0x000c9407; /* 504 */
0699         break;
0700     case MSI2500_PIX_FMT_SDR_MSI2500_384:
0701         reg7 = 0x0000a507; /* 384 */
0702         break;
0703     case MSI2500_PIX_FMT_SDR_S12:
0704         reg7 = 0x00008507; /* 336 */
0705         break;
0706     case V4L2_SDR_FMT_CS14LE:
0707         reg7 = 0x00009407; /* 252 */
0708         break;
0709     default:
0710         reg7 = 0x000c9407; /* 504 */
0711         break;
0712     }
0713 
0714     /*
0715      * Fractional-N synthesizer
0716      *
0717      *           +----------------------------------------+
0718      *           v                                        |
0719      *  Fref   +----+     +-------+     +-----+         +------+     +---+
0720      * ------> | PD | --> |  VCO  | --> | /2  | ------> | /N.F | <-- | K |
0721      *         +----+     +-------+     +-----+         +------+     +---+
0722      *                      |
0723      *                      |
0724      *                      v
0725      *                    +-------+     +-----+  Fout
0726      *                    | /Rout | --> | /12 | ------>
0727      *                    +-------+     +-----+
0728      */
0729     /*
0730      * Synthesizer config is just a educated guess...
0731      *
0732      * [7:0]   0x03, register address
0733      * [8]     1, power control
0734      * [9]     ?, power control
0735      * [12:10] output divider
0736      * [13]    0 ?
0737      * [14]    0 ?
0738      * [15]    fractional MSB, bit 20
0739      * [16:19] N
0740      * [23:20] ?
0741      * [24:31] 0x01
0742      *
0743      * output divider
0744      * val   div
0745      *   0     - (invalid)
0746      *   1     4
0747      *   2     6
0748      *   3     8
0749      *   4    10
0750      *   5    12
0751      *   6    14
0752      *   7    16
0753      *
0754      * VCO 202000000 - 720000000++
0755      */
0756 
0757     #define F_REF 24000000
0758     #define DIV_PRE_N 2
0759     #define DIV_LO_OUT 12
0760     reg3 = 0x01000303;
0761     reg4 = 0x00000004;
0762 
0763     /* XXX: Filters? AGC? VCO band? */
0764     if (f_sr < 6000000)
0765         reg3 |= 0x1 << 20;
0766     else if (f_sr < 7000000)
0767         reg3 |= 0x5 << 20;
0768     else if (f_sr < 8500000)
0769         reg3 |= 0x9 << 20;
0770     else
0771         reg3 |= 0xd << 20;
0772 
0773     for (div_out = 4; div_out < 16; div_out += 2) {
0774         f_vco = f_sr * div_out * DIV_LO_OUT;
0775         dev_dbg(dev->dev, "div_out=%u f_vco=%u\n", div_out, f_vco);
0776         if (f_vco >= 202000000)
0777             break;
0778     }
0779 
0780     /* Calculate PLL integer and fractional control word. */
0781     div_n = div_u64_rem(f_vco, DIV_PRE_N * F_REF, &k);
0782     k_cw = div_u64((u64) k * 0x200000, DIV_PRE_N * F_REF);
0783 
0784     reg3 |= div_n << 16;
0785     reg3 |= (div_out / 2 - 1) << 10;
0786     reg3 |= ((k_cw >> 20) & 0x000001) << 15; /* [20] */
0787     reg4 |= ((k_cw >>  0) & 0x0fffff) <<  8; /* [19:0] */
0788 
0789     dev_dbg(dev->dev,
0790         "f_sr=%u f_vco=%u div_n=%u k=%u div_out=%u reg3=%08x reg4=%08x\n",
0791         f_sr, f_vco, div_n, k, div_out, reg3, reg4);
0792 
0793     ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00608008);
0794     if (ret)
0795         goto err;
0796 
0797     ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00000c05);
0798     if (ret)
0799         goto err;
0800 
0801     ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00020000);
0802     if (ret)
0803         goto err;
0804 
0805     ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00480102);
0806     if (ret)
0807         goto err;
0808 
0809     ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00f38008);
0810     if (ret)
0811         goto err;
0812 
0813     ret = msi2500_ctrl_msg(dev, CMD_WREG, reg7);
0814     if (ret)
0815         goto err;
0816 
0817     ret = msi2500_ctrl_msg(dev, CMD_WREG, reg4);
0818     if (ret)
0819         goto err;
0820 
0821     ret = msi2500_ctrl_msg(dev, CMD_WREG, reg3);
0822 err:
0823     return ret;
0824 }
0825 
0826 static int msi2500_start_streaming(struct vb2_queue *vq, unsigned int count)
0827 {
0828     struct msi2500_dev *dev = vb2_get_drv_priv(vq);
0829     int ret;
0830 
0831     dev_dbg(dev->dev, "\n");
0832 
0833     if (!dev->udev)
0834         return -ENODEV;
0835 
0836     if (mutex_lock_interruptible(&dev->v4l2_lock))
0837         return -ERESTARTSYS;
0838 
0839     /* wake-up tuner */
0840     v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
0841 
0842     ret = msi2500_set_usb_adc(dev);
0843 
0844     ret = msi2500_isoc_init(dev);
0845     if (ret)
0846         msi2500_cleanup_queued_bufs(dev);
0847 
0848     ret = msi2500_ctrl_msg(dev, CMD_START_STREAMING, 0);
0849 
0850     mutex_unlock(&dev->v4l2_lock);
0851 
0852     return ret;
0853 }
0854 
0855 static void msi2500_stop_streaming(struct vb2_queue *vq)
0856 {
0857     struct msi2500_dev *dev = vb2_get_drv_priv(vq);
0858 
0859     dev_dbg(dev->dev, "\n");
0860 
0861     mutex_lock(&dev->v4l2_lock);
0862 
0863     if (dev->udev)
0864         msi2500_isoc_cleanup(dev);
0865 
0866     msi2500_cleanup_queued_bufs(dev);
0867 
0868     /* according to tests, at least 700us delay is required  */
0869     msleep(20);
0870     if (dev->udev && !msi2500_ctrl_msg(dev, CMD_STOP_STREAMING, 0)) {
0871         /* sleep USB IF / ADC */
0872         msi2500_ctrl_msg(dev, CMD_WREG, 0x01000003);
0873     }
0874 
0875     /* sleep tuner */
0876     v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
0877 
0878     mutex_unlock(&dev->v4l2_lock);
0879 }
0880 
0881 static const struct vb2_ops msi2500_vb2_ops = {
0882     .queue_setup            = msi2500_queue_setup,
0883     .buf_queue              = msi2500_buf_queue,
0884     .start_streaming        = msi2500_start_streaming,
0885     .stop_streaming         = msi2500_stop_streaming,
0886     .wait_prepare           = vb2_ops_wait_prepare,
0887     .wait_finish            = vb2_ops_wait_finish,
0888 };
0889 
0890 static int msi2500_enum_fmt_sdr_cap(struct file *file, void *priv,
0891                     struct v4l2_fmtdesc *f)
0892 {
0893     struct msi2500_dev *dev = video_drvdata(file);
0894 
0895     dev_dbg(dev->dev, "index=%d\n", f->index);
0896 
0897     if (f->index >= dev->num_formats)
0898         return -EINVAL;
0899 
0900     f->pixelformat = formats[f->index].pixelformat;
0901 
0902     return 0;
0903 }
0904 
0905 static int msi2500_g_fmt_sdr_cap(struct file *file, void *priv,
0906                  struct v4l2_format *f)
0907 {
0908     struct msi2500_dev *dev = video_drvdata(file);
0909 
0910     dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
0911         (char *)&dev->pixelformat);
0912 
0913     f->fmt.sdr.pixelformat = dev->pixelformat;
0914     f->fmt.sdr.buffersize = dev->buffersize;
0915 
0916     return 0;
0917 }
0918 
0919 static int msi2500_s_fmt_sdr_cap(struct file *file, void *priv,
0920                  struct v4l2_format *f)
0921 {
0922     struct msi2500_dev *dev = video_drvdata(file);
0923     struct vb2_queue *q = &dev->vb_queue;
0924     int i;
0925 
0926     dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
0927         (char *)&f->fmt.sdr.pixelformat);
0928 
0929     if (vb2_is_busy(q))
0930         return -EBUSY;
0931 
0932     for (i = 0; i < dev->num_formats; i++) {
0933         if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
0934             dev->pixelformat = formats[i].pixelformat;
0935             dev->buffersize = formats[i].buffersize;
0936             f->fmt.sdr.buffersize = formats[i].buffersize;
0937             return 0;
0938         }
0939     }
0940 
0941     dev->pixelformat = formats[0].pixelformat;
0942     dev->buffersize = formats[0].buffersize;
0943     f->fmt.sdr.pixelformat = formats[0].pixelformat;
0944     f->fmt.sdr.buffersize = formats[0].buffersize;
0945 
0946     return 0;
0947 }
0948 
0949 static int msi2500_try_fmt_sdr_cap(struct file *file, void *priv,
0950                    struct v4l2_format *f)
0951 {
0952     struct msi2500_dev *dev = video_drvdata(file);
0953     int i;
0954 
0955     dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
0956         (char *)&f->fmt.sdr.pixelformat);
0957 
0958     for (i = 0; i < dev->num_formats; i++) {
0959         if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
0960             f->fmt.sdr.buffersize = formats[i].buffersize;
0961             return 0;
0962         }
0963     }
0964 
0965     f->fmt.sdr.pixelformat = formats[0].pixelformat;
0966     f->fmt.sdr.buffersize = formats[0].buffersize;
0967 
0968     return 0;
0969 }
0970 
0971 static int msi2500_s_tuner(struct file *file, void *priv,
0972                const struct v4l2_tuner *v)
0973 {
0974     struct msi2500_dev *dev = video_drvdata(file);
0975     int ret;
0976 
0977     dev_dbg(dev->dev, "index=%d\n", v->index);
0978 
0979     if (v->index == 0)
0980         ret = 0;
0981     else if (v->index == 1)
0982         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
0983     else
0984         ret = -EINVAL;
0985 
0986     return ret;
0987 }
0988 
0989 static int msi2500_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
0990 {
0991     struct msi2500_dev *dev = video_drvdata(file);
0992     int ret;
0993 
0994     dev_dbg(dev->dev, "index=%d\n", v->index);
0995 
0996     if (v->index == 0) {
0997         strscpy(v->name, "Mirics MSi2500", sizeof(v->name));
0998         v->type = V4L2_TUNER_ADC;
0999         v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1000         v->rangelow =   1200000;
1001         v->rangehigh = 15000000;
1002         ret = 0;
1003     } else if (v->index == 1) {
1004         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
1005     } else {
1006         ret = -EINVAL;
1007     }
1008 
1009     return ret;
1010 }
1011 
1012 static int msi2500_g_frequency(struct file *file, void *priv,
1013                    struct v4l2_frequency *f)
1014 {
1015     struct msi2500_dev *dev = video_drvdata(file);
1016     int ret  = 0;
1017 
1018     dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
1019 
1020     if (f->tuner == 0) {
1021         f->frequency = dev->f_adc;
1022         ret = 0;
1023     } else if (f->tuner == 1) {
1024         f->type = V4L2_TUNER_RF;
1025         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
1026     } else {
1027         ret = -EINVAL;
1028     }
1029 
1030     return ret;
1031 }
1032 
1033 static int msi2500_s_frequency(struct file *file, void *priv,
1034                    const struct v4l2_frequency *f)
1035 {
1036     struct msi2500_dev *dev = video_drvdata(file);
1037     int ret;
1038 
1039     dev_dbg(dev->dev, "tuner=%d type=%d frequency=%u\n",
1040         f->tuner, f->type, f->frequency);
1041 
1042     if (f->tuner == 0) {
1043         dev->f_adc = clamp_t(unsigned int, f->frequency,
1044                      bands[0].rangelow,
1045                      bands[0].rangehigh);
1046         dev_dbg(dev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
1047         ret = msi2500_set_usb_adc(dev);
1048     } else if (f->tuner == 1) {
1049         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
1050     } else {
1051         ret = -EINVAL;
1052     }
1053 
1054     return ret;
1055 }
1056 
1057 static int msi2500_enum_freq_bands(struct file *file, void *priv,
1058                    struct v4l2_frequency_band *band)
1059 {
1060     struct msi2500_dev *dev = video_drvdata(file);
1061     int ret;
1062 
1063     dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n",
1064         band->tuner, band->type, band->index);
1065 
1066     if (band->tuner == 0) {
1067         if (band->index >= ARRAY_SIZE(bands)) {
1068             ret = -EINVAL;
1069         } else {
1070             *band = bands[band->index];
1071             ret = 0;
1072         }
1073     } else if (band->tuner == 1) {
1074         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner,
1075                        enum_freq_bands, band);
1076     } else {
1077         ret = -EINVAL;
1078     }
1079 
1080     return ret;
1081 }
1082 
1083 static const struct v4l2_ioctl_ops msi2500_ioctl_ops = {
1084     .vidioc_querycap          = msi2500_querycap,
1085 
1086     .vidioc_enum_fmt_sdr_cap  = msi2500_enum_fmt_sdr_cap,
1087     .vidioc_g_fmt_sdr_cap     = msi2500_g_fmt_sdr_cap,
1088     .vidioc_s_fmt_sdr_cap     = msi2500_s_fmt_sdr_cap,
1089     .vidioc_try_fmt_sdr_cap   = msi2500_try_fmt_sdr_cap,
1090 
1091     .vidioc_reqbufs           = vb2_ioctl_reqbufs,
1092     .vidioc_create_bufs       = vb2_ioctl_create_bufs,
1093     .vidioc_prepare_buf       = vb2_ioctl_prepare_buf,
1094     .vidioc_querybuf          = vb2_ioctl_querybuf,
1095     .vidioc_qbuf              = vb2_ioctl_qbuf,
1096     .vidioc_dqbuf             = vb2_ioctl_dqbuf,
1097 
1098     .vidioc_streamon          = vb2_ioctl_streamon,
1099     .vidioc_streamoff         = vb2_ioctl_streamoff,
1100 
1101     .vidioc_g_tuner           = msi2500_g_tuner,
1102     .vidioc_s_tuner           = msi2500_s_tuner,
1103 
1104     .vidioc_g_frequency       = msi2500_g_frequency,
1105     .vidioc_s_frequency       = msi2500_s_frequency,
1106     .vidioc_enum_freq_bands   = msi2500_enum_freq_bands,
1107 
1108     .vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
1109     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1110     .vidioc_log_status        = v4l2_ctrl_log_status,
1111 };
1112 
1113 static const struct v4l2_file_operations msi2500_fops = {
1114     .owner                    = THIS_MODULE,
1115     .open                     = v4l2_fh_open,
1116     .release                  = vb2_fop_release,
1117     .read                     = vb2_fop_read,
1118     .poll                     = vb2_fop_poll,
1119     .mmap                     = vb2_fop_mmap,
1120     .unlocked_ioctl           = video_ioctl2,
1121 };
1122 
1123 static const struct video_device msi2500_template = {
1124     .name                     = "Mirics MSi3101 SDR Dongle",
1125     .release                  = video_device_release_empty,
1126     .fops                     = &msi2500_fops,
1127     .ioctl_ops                = &msi2500_ioctl_ops,
1128 };
1129 
1130 static void msi2500_video_release(struct v4l2_device *v)
1131 {
1132     struct msi2500_dev *dev = container_of(v, struct msi2500_dev, v4l2_dev);
1133 
1134     v4l2_ctrl_handler_free(&dev->hdl);
1135     v4l2_device_unregister(&dev->v4l2_dev);
1136     kfree(dev);
1137 }
1138 
1139 static int msi2500_transfer_one_message(struct spi_master *master,
1140                     struct spi_message *m)
1141 {
1142     struct msi2500_dev *dev = spi_master_get_devdata(master);
1143     struct spi_transfer *t;
1144     int ret = 0;
1145     u32 data;
1146 
1147     list_for_each_entry(t, &m->transfers, transfer_list) {
1148         dev_dbg(dev->dev, "msg=%*ph\n", t->len, t->tx_buf);
1149         data = 0x09; /* reg 9 is SPI adapter */
1150         data |= ((u8 *)t->tx_buf)[0] << 8;
1151         data |= ((u8 *)t->tx_buf)[1] << 16;
1152         data |= ((u8 *)t->tx_buf)[2] << 24;
1153         ret = msi2500_ctrl_msg(dev, CMD_WREG, data);
1154     }
1155 
1156     m->status = ret;
1157     spi_finalize_current_message(master);
1158     return ret;
1159 }
1160 
1161 static int msi2500_probe(struct usb_interface *intf,
1162              const struct usb_device_id *id)
1163 {
1164     struct msi2500_dev *dev;
1165     struct v4l2_subdev *sd;
1166     struct spi_master *master;
1167     int ret;
1168     static struct spi_board_info board_info = {
1169         .modalias       = "msi001",
1170         .bus_num        = 0,
1171         .chip_select        = 0,
1172         .max_speed_hz       = 12000000,
1173     };
1174 
1175     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1176     if (!dev) {
1177         ret = -ENOMEM;
1178         goto err;
1179     }
1180 
1181     mutex_init(&dev->v4l2_lock);
1182     mutex_init(&dev->vb_queue_lock);
1183     spin_lock_init(&dev->queued_bufs_lock);
1184     INIT_LIST_HEAD(&dev->queued_bufs);
1185     dev->dev = &intf->dev;
1186     dev->udev = interface_to_usbdev(intf);
1187     dev->f_adc = bands[0].rangelow;
1188     dev->pixelformat = formats[0].pixelformat;
1189     dev->buffersize = formats[0].buffersize;
1190     dev->num_formats = NUM_FORMATS;
1191     if (!msi2500_emulated_fmt)
1192         dev->num_formats -= 2;
1193 
1194     /* Init videobuf2 queue structure */
1195     dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1196     dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1197     dev->vb_queue.drv_priv = dev;
1198     dev->vb_queue.buf_struct_size = sizeof(struct msi2500_frame_buf);
1199     dev->vb_queue.ops = &msi2500_vb2_ops;
1200     dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1201     dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1202     ret = vb2_queue_init(&dev->vb_queue);
1203     if (ret) {
1204         dev_err(dev->dev, "Could not initialize vb2 queue\n");
1205         goto err_free_mem;
1206     }
1207 
1208     /* Init video_device structure */
1209     dev->vdev = msi2500_template;
1210     dev->vdev.queue = &dev->vb_queue;
1211     dev->vdev.queue->lock = &dev->vb_queue_lock;
1212     video_set_drvdata(&dev->vdev, dev);
1213 
1214     /* Register the v4l2_device structure */
1215     dev->v4l2_dev.release = msi2500_video_release;
1216     ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev);
1217     if (ret) {
1218         dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret);
1219         goto err_free_mem;
1220     }
1221 
1222     /* SPI master adapter */
1223     master = spi_alloc_master(dev->dev, 0);
1224     if (master == NULL) {
1225         ret = -ENOMEM;
1226         goto err_unregister_v4l2_dev;
1227     }
1228 
1229     dev->master = master;
1230     master->bus_num = -1;
1231     master->num_chipselect = 1;
1232     master->transfer_one_message = msi2500_transfer_one_message;
1233     spi_master_set_devdata(master, dev);
1234     ret = spi_register_master(master);
1235     if (ret) {
1236         spi_master_put(master);
1237         goto err_unregister_v4l2_dev;
1238     }
1239 
1240     /* load v4l2 subdevice */
1241     sd = v4l2_spi_new_subdev(&dev->v4l2_dev, master, &board_info);
1242     dev->v4l2_subdev = sd;
1243     if (sd == NULL) {
1244         dev_err(dev->dev, "cannot get v4l2 subdevice\n");
1245         ret = -ENODEV;
1246         goto err_unregister_master;
1247     }
1248 
1249     /* Register controls */
1250     v4l2_ctrl_handler_init(&dev->hdl, 0);
1251     if (dev->hdl.error) {
1252         ret = dev->hdl.error;
1253         dev_err(dev->dev, "Could not initialize controls\n");
1254         goto err_free_controls;
1255     }
1256 
1257     /* currently all controls are from subdev */
1258     v4l2_ctrl_add_handler(&dev->hdl, sd->ctrl_handler, NULL, true);
1259 
1260     dev->v4l2_dev.ctrl_handler = &dev->hdl;
1261     dev->vdev.v4l2_dev = &dev->v4l2_dev;
1262     dev->vdev.lock = &dev->v4l2_lock;
1263     dev->vdev.device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
1264                 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
1265 
1266     ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1267     if (ret) {
1268         dev_err(dev->dev,
1269             "Failed to register as video device (%d)\n", ret);
1270         goto err_unregister_v4l2_dev;
1271     }
1272     dev_info(dev->dev, "Registered as %s\n",
1273          video_device_node_name(&dev->vdev));
1274     dev_notice(dev->dev,
1275            "SDR API is still slightly experimental and functionality changes may follow\n");
1276     return 0;
1277 err_free_controls:
1278     v4l2_ctrl_handler_free(&dev->hdl);
1279 err_unregister_master:
1280     spi_unregister_master(dev->master);
1281 err_unregister_v4l2_dev:
1282     v4l2_device_unregister(&dev->v4l2_dev);
1283 err_free_mem:
1284     kfree(dev);
1285 err:
1286     return ret;
1287 }
1288 
1289 /* USB device ID list */
1290 static const struct usb_device_id msi2500_id_table[] = {
1291     {USB_DEVICE(0x1df7, 0x2500)}, /* Mirics MSi3101 SDR Dongle */
1292     {USB_DEVICE(0x2040, 0xd300)}, /* Hauppauge WinTV 133559 LF */
1293     {}
1294 };
1295 MODULE_DEVICE_TABLE(usb, msi2500_id_table);
1296 
1297 /* USB subsystem interface */
1298 static struct usb_driver msi2500_driver = {
1299     .name                     = KBUILD_MODNAME,
1300     .probe                    = msi2500_probe,
1301     .disconnect               = msi2500_disconnect,
1302     .id_table                 = msi2500_id_table,
1303 };
1304 
1305 module_usb_driver(msi2500_driver);
1306 
1307 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1308 MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1309 MODULE_LICENSE("GPL");