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_RATE ((V4L2_CID_USER_BASE | 0xf000) + 0)
365 #define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 1)
366 #define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10)
367 #define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11)
368 #define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12)
369 #define MSI3101_CID_TUNER_GAIN ((V4L2_CID_USER_BASE | 0xf000) + 13)
371 /* intermediate buffers with raw data from the USB device */
372 struct msi3101_frame_buf {
373 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
374 struct list_head list;
375 void *data; /* raw data from USB device */
376 int filled; /* number of bytes filled to *data */
379 struct msi3101_state {
380 struct video_device vdev;
381 struct v4l2_device v4l2_dev;
383 /* videobuf2 queue and queued buffers list */
384 struct vb2_queue vb_queue;
385 struct list_head queued_bufs;
386 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
388 /* Note if taking both locks v4l2_lock must always be locked first! */
389 struct mutex v4l2_lock; /* Protects everything else */
390 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
392 /* Pointer to our usb_device, will be NULL after unplug */
393 struct usb_device *udev; /* Both mutexes most be hold when setting! */
395 unsigned int isoc_errors; /* number of contiguous ISOC errors */
396 unsigned int vb_full; /* vb is full and packets dropped */
398 struct urb *urbs[MAX_ISO_BUFS];
401 struct v4l2_ctrl_handler ctrl_handler;
402 struct v4l2_ctrl *ctrl_sampling_rate;
403 struct v4l2_ctrl *ctrl_tuner_rf;
404 struct v4l2_ctrl *ctrl_tuner_bw;
405 struct v4l2_ctrl *ctrl_tuner_if;
406 struct v4l2_ctrl *ctrl_tuner_gain;
408 u32 next_sample; /* for track lost packets */
409 u32 sample; /* for sample rate calc */
410 unsigned long jiffies;
413 /* Private functions */
414 static struct msi3101_frame_buf *msi3101_get_next_fill_buf(
415 struct msi3101_state *s)
417 unsigned long flags = 0;
418 struct msi3101_frame_buf *buf = NULL;
420 spin_lock_irqsave(&s->queued_bufs_lock, flags);
421 if (list_empty(&s->queued_bufs))
424 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list);
425 list_del(&buf->list);
427 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
432 * Converts signed 10-bit integer into 32-bit IEEE floating point
434 * Will be exact from 0 to 2^24. Above that, we round towards zero
435 * as the fractional bits will not fit in a float. (It would be better to
436 * round towards even as the fpu does, but that is slower.)
438 #define I2F_FRAC_BITS 23
439 #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
440 static uint32_t msi3101_int2float(uint32_t x)
442 uint32_t msb, exponent, fraction, sign;
444 /* Zero is special */
448 /* Negative / positive value */
457 /* Get location of the most significant bit */
461 * Use a rotate instead of a shift because that works both leftwards
462 * and rightwards due to the mod(32) behaviour. This means we don't
463 * need to check to see if we are above 2^24 or not.
465 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
466 exponent = (127 + msb) << I2F_FRAC_BITS;
468 return (fraction + exponent) | sign;
471 #define MSI3101_CONVERT_IN_URB_HANDLER
472 #define MSI3101_EXTENSIVE_DEBUG
473 static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst,
474 const u8 *src, unsigned int src_len)
476 int i, j, k, l, i_max, dst_len = 0;
478 #ifdef MSI3101_EXTENSIVE_DEBUG
481 /* There could be 1-3 1024 bytes URB frames */
482 i_max = src_len / 1024;
483 for (i = 0; i < i_max; i++) {
484 #ifdef MSI3101_EXTENSIVE_DEBUG
485 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
486 if (i == 0 && s->next_sample != sample_num[0]) {
487 dev_dbg(&s->udev->dev,
488 "%d samples lost, %d %08x:%08x\n",
489 sample_num[0] - s->next_sample,
490 src_len, s->next_sample, sample_num[0]);
494 for (j = 0; j < 6; j++) {
495 for (k = 0; k < 16; k++) {
496 for (l = 0; l < 10; l += 5) {
497 sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8;
498 sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6;
499 sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4;
500 sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2;
502 *dst++ = msi3101_int2float(sample[0]);
503 *dst++ = msi3101_int2float(sample[1]);
504 *dst++ = msi3101_int2float(sample[2]);
505 *dst++ = msi3101_int2float(sample[3]);
507 /* 4 x 32bit float samples */
512 #ifdef MSI3101_EXTENSIVE_DEBUG
513 if (memcmp(src, "\xff\xff\xff\xff", 4) && memcmp(src, "\x00\x00\x00\x00", 4))
514 dev_dbg_ratelimited(&s->udev->dev,
515 "padding %*ph\n", 4, src);
522 #ifdef MSI3101_EXTENSIVE_DEBUG
523 /* calculate samping rate and output it in 10 seconds intervals */
524 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
525 unsigned long jiffies_now = jiffies;
526 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
527 unsigned int samples = sample_num[i_max - 1] - s->sample;
528 s->jiffies = jiffies_now;
529 s->sample = sample_num[i_max - 1];
530 dev_dbg(&s->udev->dev,
531 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
532 src_len, samples, msecs,
533 samples * 1000UL / msecs);
536 /* next sample (sample = sample + i * 384) */
537 s->next_sample = sample_num[i_max - 1] + 384;
543 * This gets called for the Isochronous pipe (stream). This is done in interrupt
544 * time, so it has to be fast, not crash, and not stall. Neat.
546 static void msi3101_isoc_handler(struct urb *urb)
548 struct msi3101_state *s = (struct msi3101_state *)urb->context;
549 int i, flen, fstatus;
550 unsigned char *iso_buf = NULL;
551 struct msi3101_frame_buf *fbuf;
553 if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
554 urb->status == -ESHUTDOWN) {
555 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
556 urb, urb->status == -ENOENT ? "" : "a");
560 if (urb->status != 0) {
561 dev_dbg(&s->udev->dev,
562 "msi3101_isoc_handler() called with status %d\n",
564 /* Give up after a number of contiguous errors */
565 if (++s->isoc_errors > MAX_ISOC_ERRORS)
566 dev_dbg(&s->udev->dev,
567 "Too many ISOC errors, bailing out\n");
570 /* Reset ISOC error counter. We did get here, after all. */
575 for (i = 0; i < urb->number_of_packets; i++) {
576 /* Check frame error */
577 fstatus = urb->iso_frame_desc[i].status;
579 dev_dbg(&s->udev->dev,
580 "frame=%d/%d has error %d skipping\n",
581 i, urb->number_of_packets, fstatus);
585 /* Check if that frame contains data */
586 flen = urb->iso_frame_desc[i].actual_length;
590 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
592 /* Get free framebuffer */
593 fbuf = msi3101_get_next_fill_buf(s);
596 dev_dbg_ratelimited(&s->udev->dev,
597 "videobuf is full, %d packets dropped\n",
602 /* fill framebuffer */
603 #ifdef MSI3101_CONVERT_IN_URB_HANDLER
604 vb2_set_plane_payload(&fbuf->vb, 0,
605 msi3101_convert_stream(s,
606 vb2_plane_vaddr(&fbuf->vb, 0), iso_buf, flen));
608 memcpy(fbuf->data, iso_buf, flen);
611 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
617 i = usb_submit_urb(urb, GFP_ATOMIC);
619 dev_dbg(&s->udev->dev,
620 "Error (%d) re-submitting urb in msi3101_isoc_handler\n",
624 static void msi3101_iso_stop(struct msi3101_state *s)
627 dev_dbg(&s->udev->dev, "%s:\n", __func__);
629 /* Unlinking ISOC buffers one by one */
630 for (i = 0; i < MAX_ISO_BUFS; i++) {
632 dev_dbg(&s->udev->dev, "Unlinking URB %p\n",
634 usb_kill_urb(s->urbs[i]);
639 static void msi3101_iso_free(struct msi3101_state *s)
642 dev_dbg(&s->udev->dev, "%s:\n", __func__);
644 /* Freeing ISOC buffers one by one */
645 for (i = 0; i < MAX_ISO_BUFS; i++) {
647 dev_dbg(&s->udev->dev, "Freeing URB\n");
648 if (s->urbs[i]->transfer_buffer) {
649 usb_free_coherent(s->udev,
650 s->urbs[i]->transfer_buffer_length,
651 s->urbs[i]->transfer_buffer,
652 s->urbs[i]->transfer_dma);
654 usb_free_urb(s->urbs[i]);
660 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
661 static void msi3101_isoc_cleanup(struct msi3101_state *s)
663 dev_dbg(&s->udev->dev, "%s:\n", __func__);
669 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
670 static int msi3101_isoc_init(struct msi3101_state *s)
672 struct usb_device *udev;
675 dev_dbg(&s->udev->dev, "%s:\n", __func__);
680 ret = usb_set_interface(s->udev, 0, 1);
684 /* Allocate and init Isochronuous urbs */
685 for (i = 0; i < MAX_ISO_BUFS; i++) {
686 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
688 dev_err(&s->udev->dev,
689 "Failed to allocate urb %d\n", i);
690 msi3101_isoc_cleanup(s);
694 dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb);
698 urb->pipe = usb_rcvisocpipe(udev, 0x81);
699 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
700 urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE,
701 GFP_KERNEL, &urb->transfer_dma);
702 if (urb->transfer_buffer == NULL) {
703 dev_err(&s->udev->dev,
704 "Failed to allocate urb buffer %d\n",
706 msi3101_isoc_cleanup(s);
709 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
710 urb->complete = msi3101_isoc_handler;
712 urb->start_frame = 0;
713 urb->number_of_packets = ISO_FRAMES_PER_DESC;
714 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
715 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
716 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
721 for (i = 0; i < MAX_ISO_BUFS; i++) {
722 ret = usb_submit_urb(s->urbs[i], GFP_KERNEL);
724 dev_err(&s->udev->dev,
725 "isoc_init() submit_urb %d failed with error %d\n",
727 msi3101_isoc_cleanup(s);
730 dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]);
737 /* Must be called with vb_queue_lock hold */
738 static void msi3101_cleanup_queued_bufs(struct msi3101_state *s)
740 unsigned long flags = 0;
741 dev_dbg(&s->udev->dev, "%s:\n", __func__);
743 spin_lock_irqsave(&s->queued_bufs_lock, flags);
744 while (!list_empty(&s->queued_bufs)) {
745 struct msi3101_frame_buf *buf;
747 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf,
749 list_del(&buf->list);
750 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
752 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
755 /* The user yanked out the cable... */
756 static void msi3101_disconnect(struct usb_interface *intf)
758 struct v4l2_device *v = usb_get_intfdata(intf);
759 struct msi3101_state *s =
760 container_of(v, struct msi3101_state, v4l2_dev);
761 dev_dbg(&s->udev->dev, "%s:\n", __func__);
763 mutex_lock(&s->vb_queue_lock);
764 mutex_lock(&s->v4l2_lock);
765 /* No need to keep the urbs around after disconnection */
768 v4l2_device_disconnect(&s->v4l2_dev);
769 video_unregister_device(&s->vdev);
770 mutex_unlock(&s->v4l2_lock);
771 mutex_unlock(&s->vb_queue_lock);
773 v4l2_device_put(&s->v4l2_dev);
776 static int msi3101_querycap(struct file *file, void *fh,
777 struct v4l2_capability *cap)
779 struct msi3101_state *s = video_drvdata(file);
780 dev_dbg(&s->udev->dev, "%s:\n", __func__);
782 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
783 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
784 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
785 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
787 cap->device_caps = V4L2_CAP_TUNER;
788 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
793 /* Videobuf2 operations */
794 static int msi3101_queue_setup(struct vb2_queue *vq,
795 const struct v4l2_format *fmt, unsigned int *nbuffers,
796 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
798 struct msi3101_state *s = vb2_get_drv_priv(vq);
799 dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers);
801 /* Absolute min and max number of buffers available for mmap() */
804 sizes[0] = PAGE_ALIGN(3 * 3072); /* 3 * 768 * 4 */
805 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
806 __func__, *nbuffers, sizes[0]);
810 static int msi3101_buf_init(struct vb2_buffer *vb)
812 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
813 struct msi3101_frame_buf *fbuf =
814 container_of(vb, struct msi3101_frame_buf, vb);
815 dev_dbg(&s->udev->dev, "%s:\n", __func__);
817 fbuf->data = vzalloc(ISO_MAX_FRAME_SIZE);
818 if (fbuf->data == NULL)
824 static int msi3101_buf_prepare(struct vb2_buffer *vb)
826 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
828 /* Don't allow queing new buffers after device disconnection */
836 * +===========================================================================
837 * | 00-1024 | USB packet
838 * +===========================================================================
839 * | 00- 03 | packet address
840 * +---------------------------------------------------------------------------
842 * +---------------------------------------------------------------------------
843 * | 16- 175 | samples
844 * +---------------------------------------------------------------------------
845 * | 176- 179 | padding
846 * +---------------------------------------------------------------------------
847 * | 180- 339 | samples
848 * +---------------------------------------------------------------------------
849 * | 340- 343 | padding
850 * +---------------------------------------------------------------------------
851 * | 344- 503 | samples
852 * +---------------------------------------------------------------------------
853 * | 504- 507 | padding
854 * +---------------------------------------------------------------------------
855 * | 508- 667 | samples
856 * +---------------------------------------------------------------------------
857 * | 668- 671 | padding
858 * +---------------------------------------------------------------------------
859 * | 672- 831 | samples
860 * +---------------------------------------------------------------------------
861 * | 832- 835 | padding
862 * +---------------------------------------------------------------------------
863 * | 836- 995 | samples
864 * +---------------------------------------------------------------------------
865 * | 996- 999 | padding
866 * +---------------------------------------------------------------------------
867 * | 1000-1024 | garbage
868 * +---------------------------------------------------------------------------
870 * bytes 4 - 7 could have some meaning?
871 * padding is "00 00 00 00" or "ff ff ff ff"
872 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
874 #ifdef MSI3101_CONVERT_IN_URB_HANDLER
875 static int msi3101_buf_finish(struct vb2_buffer *vb)
880 static int msi3101_buf_finish(struct vb2_buffer *vb)
882 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
883 struct msi3101_frame_buf *fbuf =
884 container_of(vb, struct msi3101_frame_buf, vb);
886 u32 *dst = vb2_plane_vaddr(&fbuf->vb, 0);
887 ret = msi3101_convert_stream(s, dst, fbuf->data, fbuf->filled);
888 vb2_set_plane_payload(&fbuf->vb, 0, ret);
893 static void msi3101_buf_cleanup(struct vb2_buffer *vb)
895 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
896 struct msi3101_frame_buf *buf =
897 container_of(vb, struct msi3101_frame_buf, vb);
898 dev_dbg(&s->udev->dev, "%s:\n", __func__);
902 static void msi3101_buf_queue(struct vb2_buffer *vb)
904 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
905 struct msi3101_frame_buf *buf =
906 container_of(vb, struct msi3101_frame_buf, vb);
907 unsigned long flags = 0;
909 /* Check the device has not disconnected between prep and queuing */
911 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
915 spin_lock_irqsave(&s->queued_bufs_lock, flags);
916 list_add_tail(&buf->list, &s->queued_bufs);
917 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
920 #define CMD_WREG 0x41
921 #define CMD_START_STREAMING 0x43
922 #define CMD_STOP_STREAMING 0x45
923 #define CMD_READ_UNKNOW 0x48
925 #define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \
927 if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
931 dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
932 "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \
933 _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \
936 static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data)
940 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
941 u16 value = (data >> 0) & 0xffff;
942 u16 index = (data >> 16) & 0xffff;
944 msi3101_dbg_usb_control_msg(s->udev,
945 request, requesttype, value, index, NULL, 0);
947 ret = usb_control_msg(s->udev, usb_rcvctrlpipe(s->udev, 0),
948 request, requesttype, value, index, NULL, 0, 2000);
951 dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n",
952 __func__, ret, cmd, data);
957 static int msi3101_tuner_write(struct msi3101_state *s, u32 data)
959 return msi3101_ctrl_msg(s, CMD_WREG, data << 8 | 0x09);
962 #define F_REF 24000000
963 static int msi3101_set_usb_adc(struct msi3101_state *s)
965 int ret, div_n, div_m, div_r_out, f_sr;
968 * FIXME: Synthesizer config is just a educated guess...
969 * It seems to give reasonable values when N is 5-12 and output
970 * divider R is 2, which means sampling rates 5-12 Msps in practise.
972 * reg 3 ADC synthesizer config
973 * [7:0] 0x03, register address
976 * [12:10] output divider
979 * [15] increase sr by max fract
996 f_sr = s->ctrl_sampling_rate->val64;
999 for (div_n = 12; div_n > 5; div_n--) {
1000 if (f_sr >= div_n * 1000000)
1004 reg3 |= div_n << 16;
1006 for (div_r_out = 2; div_r_out < 8; div_r_out++) {
1007 if (f_sr >= div_n * F_REF / div_r_out / 12)
1011 reg3 |= (div_r_out - 1) << 10;
1012 div_m = f_sr % (div_n * F_REF / div_r_out / 12);
1014 if (div_m >= 500000) {
1019 reg4 = ((div_m * 0x0ffffful / 500000) << 8) | 0x04;
1021 dev_dbg(&s->udev->dev, "%s: sr=%d n=%d m=%d r_out=%d reg4=%08x\n",
1022 __func__, f_sr, div_n, div_m, div_r_out, reg4);
1024 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008);
1028 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05);
1032 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000);
1036 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102);
1040 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008);
1044 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x0000a507);
1048 ret = msi3101_ctrl_msg(s, CMD_WREG, reg4);
1052 ret = msi3101_ctrl_msg(s, CMD_WREG, reg3);
1059 static int msi3101_set_tuner(struct msi3101_state *s)
1062 u32 reg, synthstep, thresh, n, frac;
1065 const struct msi3101_gain *gain_lut;
1066 static const struct {
1071 { 30000000, 0x01, 16}, /* AM_MODE1 */
1072 {108000000, 0x02, 32}, /* VHF_MODE */
1073 {240000000, 0x04, 16}, /* B3_MODE */
1074 {960000000, 0x08, 4}, /* B45_MODE */
1075 {167500000, 0x10, 2}, /* BL_MODE */
1077 static const struct {
1081 { 0, 0x03}, /* Zero IF */
1082 { 450000, 0x02}, /* 450 kHz IF */
1083 {1620000, 0x01}, /* 1.62 MHz IF */
1084 {2048000, 0x00}, /* 2.048 MHz IF */
1086 static const struct {
1089 } bandwidth_lut[] = {
1090 { 200000, 0x00}, /* 200 kHz */
1091 { 300000, 0x01}, /* 300 kHz */
1092 { 600000, 0x02}, /* 600 kHz */
1093 {1536000, 0x03}, /* 1.536 MHz */
1094 {5000000, 0x04}, /* 5 MHz */
1095 {6000000, 0x05}, /* 6 MHz */
1096 {7000000, 0x06}, /* 7 MHz */
1097 {8000000, 0x07}, /* 8 MHz */
1100 unsigned int rf_freq = s->ctrl_tuner_rf->val64;
1104 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
1106 int bandwidth = s->ctrl_tuner_bw->val;
1109 * intermediate frequency (Hz)
1110 * 0, 450000, 1620000, 2048000
1112 int if_freq = s->ctrl_tuner_if->val;
1115 * gain reduction (dB)
1116 * 0 - 102 below 420 MHz
1117 * 0 - 85 above 420 MHz
1119 int gain = s->ctrl_tuner_gain->val;
1121 dev_dbg(&s->udev->dev,
1122 "%s: rf_freq=%d bandwidth=%d if_freq=%d gain=%d\n",
1123 __func__, rf_freq, bandwidth, if_freq, gain);
1127 for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
1128 if (rf_freq <= band_lut[i].rf) {
1129 mode = band_lut[i].mode;
1130 lo_div = band_lut[i].lo_div;
1135 if (i == ARRAY_SIZE(band_lut))
1138 for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
1139 if (if_freq == if_freq_lut[i].freq) {
1140 if_freq = if_freq_lut[i].val;
1145 if (i == ARRAY_SIZE(if_freq_lut))
1148 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
1149 if (bandwidth == bandwidth_lut[i].freq) {
1150 bandwidth = bandwidth_lut[i].val;
1155 if (i == ARRAY_SIZE(bandwidth_lut))
1159 #define FREF1 24000000
1160 fsynth = (rf_freq + 0) * lo_div;
1161 synthstep = FSTEP * lo_div;
1162 thresh = (FREF1 * 4) / synthstep;
1163 n = fsynth / (FREF1 * 4);
1164 frac = thresh * (fsynth % (FREF1 * 4)) / (FREF1 * 4);
1166 if (thresh > 4095 || n > 63 || frac > 4095) {
1167 dev_dbg(&s->udev->dev,
1168 "%s: synth setup failed rf=%d thresh=%d n=%d frac=%d\n",
1169 __func__, rf_freq, thresh, n, frac);
1174 ret = msi3101_tuner_write(s, 0x00000e);
1175 ret = msi3101_tuner_write(s, 0x000003);
1180 reg |= if_freq << 12;
1181 reg |= bandwidth << 14;
1184 ret = msi3101_tuner_write(s, reg);
1192 ret = msi3101_tuner_write(s, reg);
1199 ret = msi3101_tuner_write(s, reg);
1203 if (rf_freq < 120000000) {
1204 gain_lut = msi3101_gain_lut_120;
1205 len = ARRAY_SIZE(msi3101_gain_lut_120);
1206 } else if (rf_freq < 245000000) {
1207 gain_lut = msi3101_gain_lut_245;
1208 len = ARRAY_SIZE(msi3101_gain_lut_120);
1210 gain_lut = msi3101_gain_lut_1000;
1211 len = ARRAY_SIZE(msi3101_gain_lut_1000);
1214 for (i = 0; i < len; i++) {
1215 if (gain_lut[i].tot >= gain)
1222 dev_dbg(&s->udev->dev,
1223 "%s: gain tot=%d baseband=%d lna=%d mixer=%d\n",
1224 __func__, gain_lut[i].tot, gain_lut[i].baseband,
1225 gain_lut[i].lna, gain_lut[i].mixer);
1228 reg |= gain_lut[i].baseband << 4;
1230 reg |= gain_lut[i].mixer << 12;
1231 reg |= gain_lut[i].lna << 13;
1234 ret = msi3101_tuner_write(s, reg);
1241 ret = msi3101_tuner_write(s, reg);
1247 dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
1251 static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
1253 struct msi3101_state *s = vb2_get_drv_priv(vq);
1255 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1260 if (mutex_lock_interruptible(&s->v4l2_lock))
1261 return -ERESTARTSYS;
1263 ret = msi3101_set_usb_adc(s);
1265 ret = msi3101_isoc_init(s);
1267 msi3101_cleanup_queued_bufs(s);
1269 ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0);
1271 mutex_unlock(&s->v4l2_lock);
1276 static int msi3101_stop_streaming(struct vb2_queue *vq)
1278 struct msi3101_state *s = vb2_get_drv_priv(vq);
1279 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1281 if (mutex_lock_interruptible(&s->v4l2_lock))
1282 return -ERESTARTSYS;
1284 msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0);
1287 msi3101_isoc_cleanup(s);
1289 msi3101_cleanup_queued_bufs(s);
1290 mutex_unlock(&s->v4l2_lock);
1295 static struct vb2_ops msi3101_vb2_ops = {
1296 .queue_setup = msi3101_queue_setup,
1297 .buf_init = msi3101_buf_init,
1298 .buf_prepare = msi3101_buf_prepare,
1299 .buf_finish = msi3101_buf_finish,
1300 .buf_cleanup = msi3101_buf_cleanup,
1301 .buf_queue = msi3101_buf_queue,
1302 .start_streaming = msi3101_start_streaming,
1303 .stop_streaming = msi3101_stop_streaming,
1304 .wait_prepare = vb2_ops_wait_prepare,
1305 .wait_finish = vb2_ops_wait_finish,
1308 static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i)
1313 strlcpy(i->name, "SDR data", sizeof(i->name));
1314 i->type = V4L2_INPUT_TYPE_CAMERA;
1319 static int msi3101_g_input(struct file *file, void *fh, unsigned int *i)
1326 static int msi3101_s_input(struct file *file, void *fh, unsigned int i)
1328 return i ? -EINVAL : 0;
1331 static int vidioc_s_tuner(struct file *file, void *priv,
1332 const struct v4l2_tuner *v)
1334 struct msi3101_state *s = video_drvdata(file);
1335 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1340 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
1342 struct msi3101_state *s = video_drvdata(file);
1343 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1345 strcpy(v->name, "SDR RX");
1346 v->capability = V4L2_TUNER_CAP_LOW;
1351 static int vidioc_s_frequency(struct file *file, void *priv,
1352 const struct v4l2_frequency *f)
1354 struct msi3101_state *s = video_drvdata(file);
1355 dev_dbg(&s->udev->dev, "%s: frequency=%u Hz (%d)\n",
1356 __func__, f->frequency * 625U / 10U, f->frequency);
1358 return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf,
1359 f->frequency * 625U / 10U);
1362 const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
1363 .vidioc_querycap = msi3101_querycap,
1365 .vidioc_enum_input = msi3101_enum_input,
1366 .vidioc_g_input = msi3101_g_input,
1367 .vidioc_s_input = msi3101_s_input,
1369 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1370 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1371 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1372 .vidioc_querybuf = vb2_ioctl_querybuf,
1373 .vidioc_qbuf = vb2_ioctl_qbuf,
1374 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1376 .vidioc_streamon = vb2_ioctl_streamon,
1377 .vidioc_streamoff = vb2_ioctl_streamoff,
1379 .vidioc_g_tuner = vidioc_g_tuner,
1380 .vidioc_s_tuner = vidioc_s_tuner,
1381 .vidioc_s_frequency = vidioc_s_frequency,
1383 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1384 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1385 .vidioc_log_status = v4l2_ctrl_log_status,
1388 static const struct v4l2_file_operations msi3101_fops = {
1389 .owner = THIS_MODULE,
1390 .open = v4l2_fh_open,
1391 .release = vb2_fop_release,
1392 .read = vb2_fop_read,
1393 .poll = vb2_fop_poll,
1394 .mmap = vb2_fop_mmap,
1395 .unlocked_ioctl = video_ioctl2,
1398 static struct video_device msi3101_template = {
1399 .name = "Mirics MSi3101 SDR Dongle",
1400 .release = video_device_release_empty,
1401 .fops = &msi3101_fops,
1402 .ioctl_ops = &msi3101_ioctl_ops,
1405 static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
1407 struct msi3101_state *s =
1408 container_of(ctrl->handler, struct msi3101_state,
1411 dev_dbg(&s->udev->dev,
1412 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
1413 __func__, ctrl->id, ctrl->name, ctrl->val,
1414 ctrl->minimum, ctrl->maximum, ctrl->step);
1417 case MSI3101_CID_SAMPLING_RATE:
1418 case MSI3101_CID_SAMPLING_RESOLUTION:
1421 case MSI3101_CID_TUNER_RF:
1422 case MSI3101_CID_TUNER_BW:
1423 case MSI3101_CID_TUNER_IF:
1424 case MSI3101_CID_TUNER_GAIN:
1425 ret = msi3101_set_tuner(s);
1434 static const struct v4l2_ctrl_ops msi3101_ctrl_ops = {
1435 .s_ctrl = msi3101_s_ctrl,
1438 static void msi3101_video_release(struct v4l2_device *v)
1440 struct msi3101_state *s =
1441 container_of(v, struct msi3101_state, v4l2_dev);
1443 v4l2_ctrl_handler_free(&s->ctrl_handler);
1444 v4l2_device_unregister(&s->v4l2_dev);
1448 static int msi3101_probe(struct usb_interface *intf,
1449 const struct usb_device_id *id)
1451 struct usb_device *udev = interface_to_usbdev(intf);
1452 struct msi3101_state *s = NULL;
1454 static const struct v4l2_ctrl_config ctrl_sampling_rate = {
1455 .ops = &msi3101_ctrl_ops,
1456 .id = MSI3101_CID_SAMPLING_RATE,
1457 .type = V4L2_CTRL_TYPE_INTEGER64,
1458 .name = "Sampling Rate",
1464 static const struct v4l2_ctrl_config ctrl_sampling_resolution = {
1465 .ops = &msi3101_ctrl_ops,
1466 .id = MSI3101_CID_SAMPLING_RESOLUTION,
1467 .type = V4L2_CTRL_TYPE_INTEGER,
1468 .flags = V4L2_CTRL_FLAG_INACTIVE,
1469 .name = "Sampling Resolution",
1475 static const struct v4l2_ctrl_config ctrl_tuner_rf = {
1476 .ops = &msi3101_ctrl_ops,
1477 .id = MSI3101_CID_TUNER_RF,
1478 .type = V4L2_CTRL_TYPE_INTEGER64,
1485 static const struct v4l2_ctrl_config ctrl_tuner_bw = {
1486 .ops = &msi3101_ctrl_ops,
1487 .id = MSI3101_CID_TUNER_BW,
1488 .type = V4L2_CTRL_TYPE_INTEGER,
1495 static const struct v4l2_ctrl_config ctrl_tuner_if = {
1496 .ops = &msi3101_ctrl_ops,
1497 .id = MSI3101_CID_TUNER_IF,
1498 .type = V4L2_CTRL_TYPE_INTEGER,
1499 .flags = V4L2_CTRL_FLAG_INACTIVE,
1506 static const struct v4l2_ctrl_config ctrl_tuner_gain = {
1507 .ops = &msi3101_ctrl_ops,
1508 .id = MSI3101_CID_TUNER_GAIN,
1509 .type = V4L2_CTRL_TYPE_INTEGER,
1510 .name = "Tuner Gain",
1517 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1519 pr_err("Could not allocate memory for msi3101_state\n");
1523 mutex_init(&s->v4l2_lock);
1524 mutex_init(&s->vb_queue_lock);
1525 spin_lock_init(&s->queued_bufs_lock);
1526 INIT_LIST_HEAD(&s->queued_bufs);
1530 /* Init videobuf2 queue structure */
1531 s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1532 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1533 s->vb_queue.drv_priv = s;
1534 s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
1535 s->vb_queue.ops = &msi3101_vb2_ops;
1536 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
1537 s->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1538 ret = vb2_queue_init(&s->vb_queue);
1540 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
1544 /* Init video_device structure */
1545 s->vdev = msi3101_template;
1546 s->vdev.queue = &s->vb_queue;
1547 s->vdev.queue->lock = &s->vb_queue_lock;
1548 set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags);
1549 video_set_drvdata(&s->vdev, s);
1551 /* Register controls */
1552 v4l2_ctrl_handler_init(&s->ctrl_handler, 6);
1553 s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL);
1554 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL);
1555 s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL);
1556 s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL);
1557 s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL);
1558 s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL);
1559 if (s->ctrl_handler.error) {
1560 ret = s->ctrl_handler.error;
1561 dev_err(&s->udev->dev, "Could not initialize controls\n");
1562 goto err_free_controls;
1565 /* Register the v4l2_device structure */
1566 s->v4l2_dev.release = msi3101_video_release;
1567 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1569 dev_err(&s->udev->dev,
1570 "Failed to register v4l2-device (%d)\n", ret);
1571 goto err_free_controls;
1574 s->v4l2_dev.ctrl_handler = &s->ctrl_handler;
1575 s->vdev.v4l2_dev = &s->v4l2_dev;
1576 s->vdev.lock = &s->v4l2_lock;
1578 ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1);
1580 dev_err(&s->udev->dev,
1581 "Failed to register as video device (%d)\n",
1583 goto err_unregister_v4l2_dev;
1585 dev_info(&s->udev->dev, "Registered as %s\n",
1586 video_device_node_name(&s->vdev));
1590 err_unregister_v4l2_dev:
1591 v4l2_device_unregister(&s->v4l2_dev);
1593 v4l2_ctrl_handler_free(&s->ctrl_handler);
1599 /* USB device ID list */
1600 static struct usb_device_id msi3101_id_table[] = {
1601 { USB_DEVICE(0x1df7, 0x2500) },
1604 MODULE_DEVICE_TABLE(usb, msi3101_id_table);
1606 /* USB subsystem interface */
1607 static struct usb_driver msi3101_driver = {
1608 .name = KBUILD_MODNAME,
1609 .probe = msi3101_probe,
1610 .disconnect = msi3101_disconnect,
1611 .id_table = msi3101_id_table,
1614 module_usb_driver(msi3101_driver);
1616 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1617 MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1618 MODULE_LICENSE("GPL");