2 * Mirics MSi3101 SDR Dongle driver
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/input.h>
26 #include <linux/videodev2.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-event.h>
31 #include <linux/usb.h>
32 #include <linux/mutex.h>
33 #include <media/videobuf2-vmalloc.h>
42 /* 60 – 120 MHz band, lna 24dB, mixer 19dB */
43 static const struct msi3101_gain msi3101_gain_lut_120[] = {
149 /* 120 – 245 MHz band, lna 24dB, mixer 19dB */
150 static const struct msi3101_gain msi3101_gain_lut_245[] = {
256 /* 420 – 1000 MHz band, lna 7dB, mixer 19dB */
257 static const struct msi3101_gain msi3101_gain_lut_1000[] = {
349 * bAlternateSetting 1
351 * bEndpointAddress 0x81 EP 1 IN
353 * Transfer Type Isochronous
354 * wMaxPacketSize 0x1400 3x 1024 bytes
357 #define MAX_ISO_BUFS (8)
358 #define ISO_FRAMES_PER_DESC (8)
359 #define ISO_MAX_FRAME_SIZE (3 * 1024)
360 #define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
362 #define MAX_ISOC_ERRORS 20
364 #define MSI3101_CID_SAMPLING_MODE ((V4L2_CID_USER_BASE | 0xf000) + 0)
365 #define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 1)
366 #define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 2)
367 #define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10)
368 #define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11)
369 #define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12)
370 #define MSI3101_CID_TUNER_GAIN ((V4L2_CID_USER_BASE | 0xf000) + 13)
372 /* intermediate buffers with raw data from the USB device */
373 struct msi3101_frame_buf {
374 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
375 struct list_head list;
376 void *data; /* raw data from USB device */
377 int filled; /* number of bytes filled to *data */
380 struct msi3101_state {
381 struct video_device vdev;
382 struct v4l2_device v4l2_dev;
384 /* videobuf2 queue and queued buffers list */
385 struct vb2_queue vb_queue;
386 struct list_head queued_bufs;
387 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
389 /* Note if taking both locks v4l2_lock must always be locked first! */
390 struct mutex v4l2_lock; /* Protects everything else */
391 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
393 /* Pointer to our usb_device, will be NULL after unplug */
394 struct usb_device *udev; /* Both mutexes most be hold when setting! */
396 unsigned int isoc_errors; /* number of contiguous ISOC errors */
397 unsigned int vb_full; /* vb is full and packets dropped */
399 struct urb *urbs[MAX_ISO_BUFS];
402 struct v4l2_ctrl_handler ctrl_handler;
403 struct v4l2_ctrl *ctrl_sampling_rate;
404 struct v4l2_ctrl *ctrl_tuner_rf;
405 struct v4l2_ctrl *ctrl_tuner_bw;
406 struct v4l2_ctrl *ctrl_tuner_if;
407 struct v4l2_ctrl *ctrl_tuner_gain;
409 u32 next_sample; /* for track lost packets */
410 u32 sample; /* for sample rate calc */
411 unsigned long jiffies;
414 /* Private functions */
415 static struct msi3101_frame_buf *msi3101_get_next_fill_buf(
416 struct msi3101_state *s)
418 unsigned long flags = 0;
419 struct msi3101_frame_buf *buf = NULL;
421 spin_lock_irqsave(&s->queued_bufs_lock, flags);
422 if (list_empty(&s->queued_bufs))
425 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list);
426 list_del(&buf->list);
428 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
433 * Converts signed 10-bit integer into 32-bit IEEE floating point
435 * Will be exact from 0 to 2^24. Above that, we round towards zero
436 * as the fractional bits will not fit in a float. (It would be better to
437 * round towards even as the fpu does, but that is slower.)
439 #define I2F_FRAC_BITS 23
440 #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
441 static uint32_t msi3101_int2float(uint32_t x)
443 uint32_t msb, exponent, fraction, sign;
445 /* Zero is special */
449 /* Negative / positive value */
458 /* Get location of the most significant bit */
462 * Use a rotate instead of a shift because that works both leftwards
463 * and rightwards due to the mod(32) behaviour. This means we don't
464 * need to check to see if we are above 2^24 or not.
466 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
467 exponent = (127 + msb) << I2F_FRAC_BITS;
469 return (fraction + exponent) | sign;
472 #define MSI3101_CONVERT_IN_URB_HANDLER
473 #define MSI3101_EXTENSIVE_DEBUG
474 static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst,
475 const u8 *src, unsigned int src_len)
477 int i, j, k, l, i_max, dst_len = 0;
479 #ifdef MSI3101_EXTENSIVE_DEBUG
482 /* There could be 1-3 1024 bytes URB frames */
483 i_max = src_len / 1024;
484 for (i = 0; i < i_max; i++) {
485 #ifdef MSI3101_EXTENSIVE_DEBUG
486 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
487 if (i == 0 && s->next_sample != sample_num[0]) {
488 dev_dbg(&s->udev->dev,
489 "%d samples lost, %d %08x:%08x\n",
490 sample_num[0] - s->next_sample,
491 src_len, s->next_sample, sample_num[0]);
495 for (j = 0; j < 6; j++) {
496 for (k = 0; k < 16; k++) {
497 for (l = 0; l < 10; l += 5) {
498 sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8;
499 sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6;
500 sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4;
501 sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2;
503 *dst++ = msi3101_int2float(sample[0]);
504 *dst++ = msi3101_int2float(sample[1]);
505 *dst++ = msi3101_int2float(sample[2]);
506 *dst++ = msi3101_int2float(sample[3]);
508 /* 4 x 32bit float samples */
513 #ifdef MSI3101_EXTENSIVE_DEBUG
514 if (memcmp(src, "\xff\xff\xff\xff", 4) && memcmp(src, "\x00\x00\x00\x00", 4))
515 dev_dbg_ratelimited(&s->udev->dev,
516 "padding %*ph\n", 4, src);
523 #ifdef MSI3101_EXTENSIVE_DEBUG
524 /* calculate samping rate and output it in 10 seconds intervals */
525 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
526 unsigned long jiffies_now = jiffies;
527 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
528 unsigned int samples = sample_num[i_max - 1] - s->sample;
529 s->jiffies = jiffies_now;
530 s->sample = sample_num[i_max - 1];
531 dev_dbg(&s->udev->dev,
532 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
533 src_len, samples, msecs,
534 samples * 1000UL / msecs);
537 /* next sample (sample = sample + i * 384) */
538 s->next_sample = sample_num[i_max - 1] + 384;
544 * This gets called for the Isochronous pipe (stream). This is done in interrupt
545 * time, so it has to be fast, not crash, and not stall. Neat.
547 static void msi3101_isoc_handler(struct urb *urb)
549 struct msi3101_state *s = (struct msi3101_state *)urb->context;
550 int i, flen, fstatus;
551 unsigned char *iso_buf = NULL;
552 struct msi3101_frame_buf *fbuf;
554 if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
555 urb->status == -ESHUTDOWN) {
556 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
557 urb, urb->status == -ENOENT ? "" : "a");
561 if (urb->status != 0) {
562 dev_dbg(&s->udev->dev,
563 "msi3101_isoc_handler() called with status %d\n",
565 /* Give up after a number of contiguous errors */
566 if (++s->isoc_errors > MAX_ISOC_ERRORS)
567 dev_dbg(&s->udev->dev,
568 "Too many ISOC errors, bailing out\n");
571 /* Reset ISOC error counter. We did get here, after all. */
576 for (i = 0; i < urb->number_of_packets; i++) {
577 /* Check frame error */
578 fstatus = urb->iso_frame_desc[i].status;
580 dev_dbg(&s->udev->dev,
581 "frame=%d/%d has error %d skipping\n",
582 i, urb->number_of_packets, fstatus);
586 /* Check if that frame contains data */
587 flen = urb->iso_frame_desc[i].actual_length;
591 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
593 /* Get free framebuffer */
594 fbuf = msi3101_get_next_fill_buf(s);
597 dev_dbg_ratelimited(&s->udev->dev,
598 "videobuf is full, %d packets dropped\n",
603 /* fill framebuffer */
604 #ifdef MSI3101_CONVERT_IN_URB_HANDLER
605 vb2_set_plane_payload(&fbuf->vb, 0,
606 msi3101_convert_stream(s,
607 vb2_plane_vaddr(&fbuf->vb, 0), iso_buf, flen));
609 memcpy(fbuf->data, iso_buf, flen);
612 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
618 i = usb_submit_urb(urb, GFP_ATOMIC);
620 dev_dbg(&s->udev->dev,
621 "Error (%d) re-submitting urb in msi3101_isoc_handler\n",
625 static void msi3101_iso_stop(struct msi3101_state *s)
628 dev_dbg(&s->udev->dev, "%s:\n", __func__);
630 /* Unlinking ISOC buffers one by one */
631 for (i = 0; i < MAX_ISO_BUFS; i++) {
633 dev_dbg(&s->udev->dev, "Unlinking URB %p\n",
635 usb_kill_urb(s->urbs[i]);
640 static void msi3101_iso_free(struct msi3101_state *s)
643 dev_dbg(&s->udev->dev, "%s:\n", __func__);
645 /* Freeing ISOC buffers one by one */
646 for (i = 0; i < MAX_ISO_BUFS; i++) {
648 dev_dbg(&s->udev->dev, "Freeing URB\n");
649 if (s->urbs[i]->transfer_buffer) {
650 usb_free_coherent(s->udev,
651 s->urbs[i]->transfer_buffer_length,
652 s->urbs[i]->transfer_buffer,
653 s->urbs[i]->transfer_dma);
655 usb_free_urb(s->urbs[i]);
661 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
662 static void msi3101_isoc_cleanup(struct msi3101_state *s)
664 dev_dbg(&s->udev->dev, "%s:\n", __func__);
670 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
671 static int msi3101_isoc_init(struct msi3101_state *s)
673 struct usb_device *udev;
676 dev_dbg(&s->udev->dev, "%s:\n", __func__);
681 ret = usb_set_interface(s->udev, 0, 1);
685 /* Allocate and init Isochronuous urbs */
686 for (i = 0; i < MAX_ISO_BUFS; i++) {
687 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
689 dev_err(&s->udev->dev,
690 "Failed to allocate urb %d\n", i);
691 msi3101_isoc_cleanup(s);
695 dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb);
699 urb->pipe = usb_rcvisocpipe(udev, 0x81);
700 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
701 urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE,
702 GFP_KERNEL, &urb->transfer_dma);
703 if (urb->transfer_buffer == NULL) {
704 dev_err(&s->udev->dev,
705 "Failed to allocate urb buffer %d\n",
707 msi3101_isoc_cleanup(s);
710 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
711 urb->complete = msi3101_isoc_handler;
713 urb->start_frame = 0;
714 urb->number_of_packets = ISO_FRAMES_PER_DESC;
715 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
716 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
717 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
722 for (i = 0; i < MAX_ISO_BUFS; i++) {
723 ret = usb_submit_urb(s->urbs[i], GFP_KERNEL);
725 dev_err(&s->udev->dev,
726 "isoc_init() submit_urb %d failed with error %d\n",
728 msi3101_isoc_cleanup(s);
731 dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]);
738 /* Must be called with vb_queue_lock hold */
739 static void msi3101_cleanup_queued_bufs(struct msi3101_state *s)
741 unsigned long flags = 0;
742 dev_dbg(&s->udev->dev, "%s:\n", __func__);
744 spin_lock_irqsave(&s->queued_bufs_lock, flags);
745 while (!list_empty(&s->queued_bufs)) {
746 struct msi3101_frame_buf *buf;
748 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf,
750 list_del(&buf->list);
751 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
753 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
756 /* The user yanked out the cable... */
757 static void msi3101_disconnect(struct usb_interface *intf)
759 struct v4l2_device *v = usb_get_intfdata(intf);
760 struct msi3101_state *s =
761 container_of(v, struct msi3101_state, v4l2_dev);
762 dev_dbg(&s->udev->dev, "%s:\n", __func__);
764 mutex_lock(&s->vb_queue_lock);
765 mutex_lock(&s->v4l2_lock);
766 /* No need to keep the urbs around after disconnection */
769 v4l2_device_disconnect(&s->v4l2_dev);
770 video_unregister_device(&s->vdev);
771 mutex_unlock(&s->v4l2_lock);
772 mutex_unlock(&s->vb_queue_lock);
774 v4l2_device_put(&s->v4l2_dev);
777 static int msi3101_querycap(struct file *file, void *fh,
778 struct v4l2_capability *cap)
780 struct msi3101_state *s = video_drvdata(file);
781 dev_dbg(&s->udev->dev, "%s:\n", __func__);
783 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
784 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
785 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
786 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
788 cap->device_caps = V4L2_CAP_TUNER;
789 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
794 /* Videobuf2 operations */
795 static int msi3101_queue_setup(struct vb2_queue *vq,
796 const struct v4l2_format *fmt, unsigned int *nbuffers,
797 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
799 struct msi3101_state *s = vb2_get_drv_priv(vq);
800 dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers);
802 /* Absolute min and max number of buffers available for mmap() */
805 sizes[0] = PAGE_ALIGN(3 * 3072); /* 3 * 768 * 4 */
806 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
807 __func__, *nbuffers, sizes[0]);
811 static int msi3101_buf_init(struct vb2_buffer *vb)
813 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
814 struct msi3101_frame_buf *fbuf =
815 container_of(vb, struct msi3101_frame_buf, vb);
816 dev_dbg(&s->udev->dev, "%s:\n", __func__);
818 fbuf->data = vzalloc(ISO_MAX_FRAME_SIZE);
819 if (fbuf->data == NULL)
825 static int msi3101_buf_prepare(struct vb2_buffer *vb)
827 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
829 /* Don't allow queing new buffers after device disconnection */
837 * +===========================================================================
838 * | 00-1024 | USB packet
839 * +===========================================================================
840 * | 00- 03 | packet address
841 * +---------------------------------------------------------------------------
843 * +---------------------------------------------------------------------------
844 * | 16- 175 | samples
845 * +---------------------------------------------------------------------------
846 * | 176- 179 | padding
847 * +---------------------------------------------------------------------------
848 * | 180- 339 | samples
849 * +---------------------------------------------------------------------------
850 * | 340- 343 | padding
851 * +---------------------------------------------------------------------------
852 * | 344- 503 | samples
853 * +---------------------------------------------------------------------------
854 * | 504- 507 | padding
855 * +---------------------------------------------------------------------------
856 * | 508- 667 | samples
857 * +---------------------------------------------------------------------------
858 * | 668- 671 | padding
859 * +---------------------------------------------------------------------------
860 * | 672- 831 | samples
861 * +---------------------------------------------------------------------------
862 * | 832- 835 | padding
863 * +---------------------------------------------------------------------------
864 * | 836- 995 | samples
865 * +---------------------------------------------------------------------------
866 * | 996- 999 | padding
867 * +---------------------------------------------------------------------------
868 * | 1000-1024 | garbage
869 * +---------------------------------------------------------------------------
871 * bytes 4 - 7 could have some meaning?
872 * padding is "00 00 00 00" or "ff ff ff ff"
873 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
875 #ifdef MSI3101_CONVERT_IN_URB_HANDLER
876 static int msi3101_buf_finish(struct vb2_buffer *vb)
881 static int msi3101_buf_finish(struct vb2_buffer *vb)
883 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
884 struct msi3101_frame_buf *fbuf =
885 container_of(vb, struct msi3101_frame_buf, vb);
887 u32 *dst = vb2_plane_vaddr(&fbuf->vb, 0);
888 ret = msi3101_convert_stream(s, dst, fbuf->data, fbuf->filled);
889 vb2_set_plane_payload(&fbuf->vb, 0, ret);
894 static void msi3101_buf_cleanup(struct vb2_buffer *vb)
896 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
897 struct msi3101_frame_buf *buf =
898 container_of(vb, struct msi3101_frame_buf, vb);
899 dev_dbg(&s->udev->dev, "%s:\n", __func__);
903 static void msi3101_buf_queue(struct vb2_buffer *vb)
905 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
906 struct msi3101_frame_buf *buf =
907 container_of(vb, struct msi3101_frame_buf, vb);
908 unsigned long flags = 0;
910 /* Check the device has not disconnected between prep and queuing */
912 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
916 spin_lock_irqsave(&s->queued_bufs_lock, flags);
917 list_add_tail(&buf->list, &s->queued_bufs);
918 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
921 #define CMD_WREG 0x41
922 #define CMD_START_STREAMING 0x43
923 #define CMD_STOP_STREAMING 0x45
924 #define CMD_READ_UNKNOW 0x48
926 #define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \
928 if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
932 dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
933 "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \
934 _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \
937 static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data)
941 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
942 u16 value = (data >> 0) & 0xffff;
943 u16 index = (data >> 16) & 0xffff;
945 msi3101_dbg_usb_control_msg(s->udev,
946 request, requesttype, value, index, NULL, 0);
948 ret = usb_control_msg(s->udev, usb_rcvctrlpipe(s->udev, 0),
949 request, requesttype, value, index, NULL, 0, 2000);
952 dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n",
953 __func__, ret, cmd, data);
958 static int msi3101_tuner_write(struct msi3101_state *s, u32 data)
960 return msi3101_ctrl_msg(s, CMD_WREG, data << 8 | 0x09);
963 #define F_REF 24000000
965 static int msi3101_set_usb_adc(struct msi3101_state *s)
967 int ret, div_n, div_m, div_r_out, f_sr, f_vco;
970 * Synthesizer config is just a educated guess...
972 * [7:0] 0x03, register address
975 * [12:10] output divider
978 * [15] increase sr by max fract
994 * VCO 202000000 - 720000000++
997 f_sr = s->ctrl_sampling_rate->val64;
1000 for (div_r_out = 4; div_r_out < 16; div_r_out += 2) {
1001 f_vco = f_sr * div_r_out * 12;
1002 dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n",
1003 __func__, div_r_out, f_vco);
1004 if (f_vco >= 202000000)
1008 div_n = f_vco / (F_REF * DIV_R_IN);
1009 div_m = f_vco % (F_REF * DIV_R_IN);
1011 reg3 |= div_n << 16;
1012 reg3 |= (div_r_out / 2 - 1) << 10;
1013 reg4 = 0x0ffffful * div_m / F_REF;
1015 if (reg4 >= 0x0ffffful) {
1016 dev_dbg(&s->udev->dev,
1017 "%s: extending fractional part value %08x\n",
1023 reg4 = (reg4 << 8) | 0x04;
1025 dev_dbg(&s->udev->dev,
1026 "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg4=%08x\n",
1027 __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg4);
1029 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008);
1033 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05);
1037 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000);
1041 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102);
1045 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008);
1049 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x0000a507);
1053 ret = msi3101_ctrl_msg(s, CMD_WREG, reg4);
1057 ret = msi3101_ctrl_msg(s, CMD_WREG, reg3);
1064 static int msi3101_set_tuner(struct msi3101_state *s)
1067 u32 reg, synthstep, thresh, n, frac;
1070 const struct msi3101_gain *gain_lut;
1071 static const struct {
1076 { 30000000, 0x01, 16}, /* AM_MODE1 */
1077 {108000000, 0x02, 32}, /* VHF_MODE */
1078 {240000000, 0x04, 16}, /* B3_MODE */
1079 {960000000, 0x08, 4}, /* B45_MODE */
1080 {167500000, 0x10, 2}, /* BL_MODE */
1082 static const struct {
1086 { 0, 0x03}, /* Zero IF */
1087 { 450000, 0x02}, /* 450 kHz IF */
1088 {1620000, 0x01}, /* 1.62 MHz IF */
1089 {2048000, 0x00}, /* 2.048 MHz IF */
1091 static const struct {
1094 } bandwidth_lut[] = {
1095 { 200000, 0x00}, /* 200 kHz */
1096 { 300000, 0x01}, /* 300 kHz */
1097 { 600000, 0x02}, /* 600 kHz */
1098 {1536000, 0x03}, /* 1.536 MHz */
1099 {5000000, 0x04}, /* 5 MHz */
1100 {6000000, 0x05}, /* 6 MHz */
1101 {7000000, 0x06}, /* 7 MHz */
1102 {8000000, 0x07}, /* 8 MHz */
1105 unsigned int rf_freq = s->ctrl_tuner_rf->val64;
1109 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
1111 int bandwidth = s->ctrl_tuner_bw->val;
1114 * intermediate frequency (Hz)
1115 * 0, 450000, 1620000, 2048000
1117 int if_freq = s->ctrl_tuner_if->val;
1120 * gain reduction (dB)
1121 * 0 - 102 below 420 MHz
1122 * 0 - 85 above 420 MHz
1124 int gain = s->ctrl_tuner_gain->val;
1126 dev_dbg(&s->udev->dev,
1127 "%s: rf_freq=%d bandwidth=%d if_freq=%d gain=%d\n",
1128 __func__, rf_freq, bandwidth, if_freq, gain);
1132 for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
1133 if (rf_freq <= band_lut[i].rf) {
1134 mode = band_lut[i].mode;
1135 lo_div = band_lut[i].lo_div;
1140 if (i == ARRAY_SIZE(band_lut))
1143 for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
1144 if (if_freq == if_freq_lut[i].freq) {
1145 if_freq = if_freq_lut[i].val;
1150 if (i == ARRAY_SIZE(if_freq_lut))
1153 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
1154 if (bandwidth == bandwidth_lut[i].freq) {
1155 bandwidth = bandwidth_lut[i].val;
1160 if (i == ARRAY_SIZE(bandwidth_lut))
1164 #define FREF1 24000000
1165 fsynth = (rf_freq + 0) * lo_div;
1166 synthstep = FSTEP * lo_div;
1167 thresh = (FREF1 * 4) / synthstep;
1168 n = fsynth / (FREF1 * 4);
1169 frac = thresh * (fsynth % (FREF1 * 4)) / (FREF1 * 4);
1171 if (thresh > 4095 || n > 63 || frac > 4095) {
1172 dev_dbg(&s->udev->dev,
1173 "%s: synth setup failed rf=%d thresh=%d n=%d frac=%d\n",
1174 __func__, rf_freq, thresh, n, frac);
1179 ret = msi3101_tuner_write(s, 0x00000e);
1180 ret = msi3101_tuner_write(s, 0x000003);
1185 reg |= if_freq << 12;
1186 reg |= bandwidth << 14;
1189 ret = msi3101_tuner_write(s, reg);
1197 ret = msi3101_tuner_write(s, reg);
1204 ret = msi3101_tuner_write(s, reg);
1208 if (rf_freq < 120000000) {
1209 gain_lut = msi3101_gain_lut_120;
1210 len = ARRAY_SIZE(msi3101_gain_lut_120);
1211 } else if (rf_freq < 245000000) {
1212 gain_lut = msi3101_gain_lut_245;
1213 len = ARRAY_SIZE(msi3101_gain_lut_120);
1215 gain_lut = msi3101_gain_lut_1000;
1216 len = ARRAY_SIZE(msi3101_gain_lut_1000);
1219 for (i = 0; i < len; i++) {
1220 if (gain_lut[i].tot >= gain)
1227 dev_dbg(&s->udev->dev,
1228 "%s: gain tot=%d baseband=%d lna=%d mixer=%d\n",
1229 __func__, gain_lut[i].tot, gain_lut[i].baseband,
1230 gain_lut[i].lna, gain_lut[i].mixer);
1233 reg |= gain_lut[i].baseband << 4;
1235 reg |= gain_lut[i].mixer << 12;
1236 reg |= gain_lut[i].lna << 13;
1239 ret = msi3101_tuner_write(s, reg);
1246 ret = msi3101_tuner_write(s, reg);
1252 dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
1256 static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
1258 struct msi3101_state *s = vb2_get_drv_priv(vq);
1260 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1265 if (mutex_lock_interruptible(&s->v4l2_lock))
1266 return -ERESTARTSYS;
1268 ret = msi3101_set_usb_adc(s);
1270 ret = msi3101_isoc_init(s);
1272 msi3101_cleanup_queued_bufs(s);
1274 ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0);
1276 mutex_unlock(&s->v4l2_lock);
1281 static int msi3101_stop_streaming(struct vb2_queue *vq)
1283 struct msi3101_state *s = vb2_get_drv_priv(vq);
1284 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1286 if (mutex_lock_interruptible(&s->v4l2_lock))
1287 return -ERESTARTSYS;
1289 msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0);
1292 msi3101_isoc_cleanup(s);
1294 msi3101_cleanup_queued_bufs(s);
1295 mutex_unlock(&s->v4l2_lock);
1300 static struct vb2_ops msi3101_vb2_ops = {
1301 .queue_setup = msi3101_queue_setup,
1302 .buf_init = msi3101_buf_init,
1303 .buf_prepare = msi3101_buf_prepare,
1304 .buf_finish = msi3101_buf_finish,
1305 .buf_cleanup = msi3101_buf_cleanup,
1306 .buf_queue = msi3101_buf_queue,
1307 .start_streaming = msi3101_start_streaming,
1308 .stop_streaming = msi3101_stop_streaming,
1309 .wait_prepare = vb2_ops_wait_prepare,
1310 .wait_finish = vb2_ops_wait_finish,
1313 static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i)
1318 strlcpy(i->name, "SDR data", sizeof(i->name));
1319 i->type = V4L2_INPUT_TYPE_CAMERA;
1324 static int msi3101_g_input(struct file *file, void *fh, unsigned int *i)
1331 static int msi3101_s_input(struct file *file, void *fh, unsigned int i)
1333 return i ? -EINVAL : 0;
1336 static int vidioc_s_tuner(struct file *file, void *priv,
1337 const struct v4l2_tuner *v)
1339 struct msi3101_state *s = video_drvdata(file);
1340 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1345 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
1347 struct msi3101_state *s = video_drvdata(file);
1348 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1350 strcpy(v->name, "SDR RX");
1351 v->capability = V4L2_TUNER_CAP_LOW;
1356 static int vidioc_s_frequency(struct file *file, void *priv,
1357 const struct v4l2_frequency *f)
1359 struct msi3101_state *s = video_drvdata(file);
1360 dev_dbg(&s->udev->dev, "%s: frequency=%u Hz (%d)\n",
1361 __func__, f->frequency * 625U / 10U, f->frequency);
1363 return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf,
1364 f->frequency * 625U / 10U);
1367 const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
1368 .vidioc_querycap = msi3101_querycap,
1370 .vidioc_enum_input = msi3101_enum_input,
1371 .vidioc_g_input = msi3101_g_input,
1372 .vidioc_s_input = msi3101_s_input,
1374 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1375 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1376 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1377 .vidioc_querybuf = vb2_ioctl_querybuf,
1378 .vidioc_qbuf = vb2_ioctl_qbuf,
1379 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1381 .vidioc_streamon = vb2_ioctl_streamon,
1382 .vidioc_streamoff = vb2_ioctl_streamoff,
1384 .vidioc_g_tuner = vidioc_g_tuner,
1385 .vidioc_s_tuner = vidioc_s_tuner,
1386 .vidioc_s_frequency = vidioc_s_frequency,
1388 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1389 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1390 .vidioc_log_status = v4l2_ctrl_log_status,
1393 static const struct v4l2_file_operations msi3101_fops = {
1394 .owner = THIS_MODULE,
1395 .open = v4l2_fh_open,
1396 .release = vb2_fop_release,
1397 .read = vb2_fop_read,
1398 .poll = vb2_fop_poll,
1399 .mmap = vb2_fop_mmap,
1400 .unlocked_ioctl = video_ioctl2,
1403 static struct video_device msi3101_template = {
1404 .name = "Mirics MSi3101 SDR Dongle",
1405 .release = video_device_release_empty,
1406 .fops = &msi3101_fops,
1407 .ioctl_ops = &msi3101_ioctl_ops,
1410 static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
1412 struct msi3101_state *s =
1413 container_of(ctrl->handler, struct msi3101_state,
1416 dev_dbg(&s->udev->dev,
1417 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
1418 __func__, ctrl->id, ctrl->name, ctrl->val,
1419 ctrl->minimum, ctrl->maximum, ctrl->step);
1422 case MSI3101_CID_SAMPLING_MODE:
1423 case MSI3101_CID_SAMPLING_RATE:
1424 case MSI3101_CID_SAMPLING_RESOLUTION:
1427 case MSI3101_CID_TUNER_RF:
1428 case MSI3101_CID_TUNER_BW:
1429 case MSI3101_CID_TUNER_IF:
1430 case MSI3101_CID_TUNER_GAIN:
1431 ret = msi3101_set_tuner(s);
1440 static const struct v4l2_ctrl_ops msi3101_ctrl_ops = {
1441 .s_ctrl = msi3101_s_ctrl,
1444 static void msi3101_video_release(struct v4l2_device *v)
1446 struct msi3101_state *s =
1447 container_of(v, struct msi3101_state, v4l2_dev);
1449 v4l2_ctrl_handler_free(&s->ctrl_handler);
1450 v4l2_device_unregister(&s->v4l2_dev);
1454 static int msi3101_probe(struct usb_interface *intf,
1455 const struct usb_device_id *id)
1457 struct usb_device *udev = interface_to_usbdev(intf);
1458 struct msi3101_state *s = NULL;
1460 static const char * const ctrl_sampling_mode_qmenu_strings[] = {
1461 "Quadrature Sampling",
1464 static const struct v4l2_ctrl_config ctrl_sampling_mode = {
1465 .ops = &msi3101_ctrl_ops,
1466 .id = MSI3101_CID_SAMPLING_MODE,
1467 .type = V4L2_CTRL_TYPE_MENU,
1468 .flags = V4L2_CTRL_FLAG_INACTIVE,
1469 .name = "Sampling Mode",
1470 .qmenu = ctrl_sampling_mode_qmenu_strings,
1472 static const struct v4l2_ctrl_config ctrl_sampling_rate = {
1473 .ops = &msi3101_ctrl_ops,
1474 .id = MSI3101_CID_SAMPLING_RATE,
1475 .type = V4L2_CTRL_TYPE_INTEGER64,
1476 .name = "Sampling Rate",
1482 static const struct v4l2_ctrl_config ctrl_sampling_resolution = {
1483 .ops = &msi3101_ctrl_ops,
1484 .id = MSI3101_CID_SAMPLING_RESOLUTION,
1485 .type = V4L2_CTRL_TYPE_INTEGER,
1486 .flags = V4L2_CTRL_FLAG_INACTIVE,
1487 .name = "Sampling Resolution",
1493 static const struct v4l2_ctrl_config ctrl_tuner_rf = {
1494 .ops = &msi3101_ctrl_ops,
1495 .id = MSI3101_CID_TUNER_RF,
1496 .type = V4L2_CTRL_TYPE_INTEGER64,
1503 static const struct v4l2_ctrl_config ctrl_tuner_bw = {
1504 .ops = &msi3101_ctrl_ops,
1505 .id = MSI3101_CID_TUNER_BW,
1506 .type = V4L2_CTRL_TYPE_INTEGER,
1513 static const struct v4l2_ctrl_config ctrl_tuner_if = {
1514 .ops = &msi3101_ctrl_ops,
1515 .id = MSI3101_CID_TUNER_IF,
1516 .type = V4L2_CTRL_TYPE_INTEGER,
1517 .flags = V4L2_CTRL_FLAG_INACTIVE,
1524 static const struct v4l2_ctrl_config ctrl_tuner_gain = {
1525 .ops = &msi3101_ctrl_ops,
1526 .id = MSI3101_CID_TUNER_GAIN,
1527 .type = V4L2_CTRL_TYPE_INTEGER,
1528 .name = "Tuner Gain",
1535 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1537 pr_err("Could not allocate memory for msi3101_state\n");
1541 mutex_init(&s->v4l2_lock);
1542 mutex_init(&s->vb_queue_lock);
1543 spin_lock_init(&s->queued_bufs_lock);
1544 INIT_LIST_HEAD(&s->queued_bufs);
1548 /* Init videobuf2 queue structure */
1549 s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1550 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1551 s->vb_queue.drv_priv = s;
1552 s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
1553 s->vb_queue.ops = &msi3101_vb2_ops;
1554 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
1555 s->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1556 ret = vb2_queue_init(&s->vb_queue);
1558 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
1562 /* Init video_device structure */
1563 s->vdev = msi3101_template;
1564 s->vdev.queue = &s->vb_queue;
1565 s->vdev.queue->lock = &s->vb_queue_lock;
1566 set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags);
1567 video_set_drvdata(&s->vdev, s);
1569 /* Register controls */
1570 v4l2_ctrl_handler_init(&s->ctrl_handler, 7);
1571 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_mode, NULL);
1572 s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL);
1573 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL);
1574 s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL);
1575 s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL);
1576 s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL);
1577 s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL);
1578 if (s->ctrl_handler.error) {
1579 ret = s->ctrl_handler.error;
1580 dev_err(&s->udev->dev, "Could not initialize controls\n");
1581 goto err_free_controls;
1584 /* Register the v4l2_device structure */
1585 s->v4l2_dev.release = msi3101_video_release;
1586 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1588 dev_err(&s->udev->dev,
1589 "Failed to register v4l2-device (%d)\n", ret);
1590 goto err_free_controls;
1593 s->v4l2_dev.ctrl_handler = &s->ctrl_handler;
1594 s->vdev.v4l2_dev = &s->v4l2_dev;
1595 s->vdev.lock = &s->v4l2_lock;
1597 ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1);
1599 dev_err(&s->udev->dev,
1600 "Failed to register as video device (%d)\n",
1602 goto err_unregister_v4l2_dev;
1604 dev_info(&s->udev->dev, "Registered as %s\n",
1605 video_device_node_name(&s->vdev));
1609 err_unregister_v4l2_dev:
1610 v4l2_device_unregister(&s->v4l2_dev);
1612 v4l2_ctrl_handler_free(&s->ctrl_handler);
1618 /* USB device ID list */
1619 static struct usb_device_id msi3101_id_table[] = {
1620 { USB_DEVICE(0x1df7, 0x2500) },
1623 MODULE_DEVICE_TABLE(usb, msi3101_id_table);
1625 /* USB subsystem interface */
1626 static struct usb_driver msi3101_driver = {
1627 .name = KBUILD_MODNAME,
1628 .probe = msi3101_probe,
1629 .disconnect = msi3101_disconnect,
1630 .id_table = msi3101_id_table,
1633 module_usb_driver(msi3101_driver);
1635 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1636 MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1637 MODULE_LICENSE("GPL");