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.
20 * That driver is somehow based of pwc driver:
21 * (C) 1999-2004 Nemosoft Unv.
22 * (C) 2004-2006 Luc Saillard (luc@saillard.org)
23 * (C) 2011 Hans de Goede <hdegoede@redhat.com>
25 * Development tree of that driver will be on:
26 * http://git.linuxtv.org/anttip/media_tree.git/shortlog/refs/heads/mirics
28 * GNU Radio plugin "gr-kernel" for device usage will be on:
29 * http://git.linuxtv.org/anttip/gr-kernel.git
32 * Help is very highly welcome for these + all the others you could imagine:
33 * - split USB ADC interface and RF tuner to own drivers (msi2500 and msi001)
34 * - move controls to V4L2 API
35 * - use libv4l2 for stream format conversions
36 * - gr-kernel: switch to v4l2_mmap (current read eats a lot of cpu)
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/gcd.h>
43 #include <asm/div64.h>
44 #include <media/v4l2-device.h>
45 #include <media/v4l2-ioctl.h>
46 #include <media/v4l2-ctrls.h>
47 #include <media/v4l2-event.h>
48 #include <linux/usb.h>
49 #include <media/videobuf2-vmalloc.h>
58 /* 60 – 120 MHz band, lna 24dB, mixer 19dB */
59 static const struct msi3101_gain msi3101_gain_lut_120[] = {
165 /* 120 – 245 MHz band, lna 24dB, mixer 19dB */
166 static const struct msi3101_gain msi3101_gain_lut_245[] = {
272 /* 420 – 1000 MHz band, lna 7dB, mixer 19dB */
273 static const struct msi3101_gain msi3101_gain_lut_1000[] = {
365 * bAlternateSetting 1
367 * bEndpointAddress 0x81 EP 1 IN
369 * Transfer Type Isochronous
370 * wMaxPacketSize 0x1400 3x 1024 bytes
373 #define MAX_ISO_BUFS (8)
374 #define ISO_FRAMES_PER_DESC (8)
375 #define ISO_MAX_FRAME_SIZE (3 * 1024)
376 #define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
377 #define MAX_ISOC_ERRORS 20
379 /* TODO: These should be moved to V4L2 API */
380 #define MSI3101_CID_SAMPLING_MODE ((V4L2_CID_USER_BASE | 0xf000) + 0)
381 #define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 1)
382 #define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 2)
383 #define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10)
384 #define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11)
385 #define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12)
386 #define MSI3101_CID_TUNER_GAIN ((V4L2_CID_USER_BASE | 0xf000) + 13)
388 /* intermediate buffers with raw data from the USB device */
389 struct msi3101_frame_buf {
390 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
391 struct list_head list;
394 struct msi3101_state {
395 struct video_device vdev;
396 struct v4l2_device v4l2_dev;
398 /* videobuf2 queue and queued buffers list */
399 struct vb2_queue vb_queue;
400 struct list_head queued_bufs;
401 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
403 /* Note if taking both locks v4l2_lock must always be locked first! */
404 struct mutex v4l2_lock; /* Protects everything else */
405 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
407 /* Pointer to our usb_device, will be NULL after unplug */
408 struct usb_device *udev; /* Both mutexes most be hold when setting! */
410 unsigned int isoc_errors; /* number of contiguous ISOC errors */
411 unsigned int vb_full; /* vb is full and packets dropped */
413 struct urb *urbs[MAX_ISO_BUFS];
414 int (*convert_stream) (struct msi3101_state *s, u32 *dst, u8 *src,
415 unsigned int src_len);
418 struct v4l2_ctrl_handler ctrl_handler;
419 struct v4l2_ctrl *ctrl_sampling_rate;
420 struct v4l2_ctrl *ctrl_tuner_rf;
421 struct v4l2_ctrl *ctrl_tuner_bw;
422 struct v4l2_ctrl *ctrl_tuner_if;
423 struct v4l2_ctrl *ctrl_tuner_gain;
425 u32 next_sample; /* for track lost packets */
426 u32 sample; /* for sample rate calc */
427 unsigned long jiffies;
428 unsigned int sample_ctrl_bit[4];
431 /* Private functions */
432 static struct msi3101_frame_buf *msi3101_get_next_fill_buf(
433 struct msi3101_state *s)
435 unsigned long flags = 0;
436 struct msi3101_frame_buf *buf = NULL;
438 spin_lock_irqsave(&s->queued_bufs_lock, flags);
439 if (list_empty(&s->queued_bufs))
442 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list);
443 list_del(&buf->list);
445 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
450 * +===========================================================================
451 * | 00-1023 | USB packet type '384'
452 * +===========================================================================
453 * | 00- 03 | sequence number of first sample in that USB packet
454 * +---------------------------------------------------------------------------
456 * +---------------------------------------------------------------------------
457 * | 16- 175 | samples
458 * +---------------------------------------------------------------------------
459 * | 176- 179 | control bits for previous samples
460 * +---------------------------------------------------------------------------
461 * | 180- 339 | samples
462 * +---------------------------------------------------------------------------
463 * | 340- 343 | control bits for previous samples
464 * +---------------------------------------------------------------------------
465 * | 344- 503 | samples
466 * +---------------------------------------------------------------------------
467 * | 504- 507 | control bits for previous samples
468 * +---------------------------------------------------------------------------
469 * | 508- 667 | samples
470 * +---------------------------------------------------------------------------
471 * | 668- 671 | control bits for previous samples
472 * +---------------------------------------------------------------------------
473 * | 672- 831 | samples
474 * +---------------------------------------------------------------------------
475 * | 832- 835 | control bits for previous samples
476 * +---------------------------------------------------------------------------
477 * | 836- 995 | samples
478 * +---------------------------------------------------------------------------
479 * | 996- 999 | control bits for previous samples
480 * +---------------------------------------------------------------------------
481 * | 1000-1023 | garbage
482 * +---------------------------------------------------------------------------
484 * Bytes 4 - 7 could have some meaning?
486 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
487 * numbers. This results one 2-bit number for 8 samples. It is likely used for
488 * for bit shifting sample by given bits, increasing actual sampling resolution.
489 * Number 2 (0b10) was never seen.
491 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
495 * Integer to 32-bit IEEE floating point representation routine is taken
496 * from Radeon R600 driver (drivers/gpu/drm/radeon/r600_blit_kms.c).
498 * TODO: Currently we do conversion here in Kernel, but in future that will
499 * be moved to the libv4l2 library as video format conversions are.
501 #define I2F_FRAC_BITS 23
502 #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
505 * Converts signed 8-bit integer into 32-bit IEEE floating point
508 static u32 msi3101_convert_sample_504(struct msi3101_state *s, u16 x)
510 u32 msb, exponent, fraction, sign;
512 /* Zero is special */
516 /* Negative / positive value */
519 x &= 0x7f; /* result is 7 bit ... + sign */
525 /* Get location of the most significant bit */
528 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
529 exponent = (127 + msb) << I2F_FRAC_BITS;
531 return (fraction + exponent) | sign;
534 static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst,
535 u8 *src, unsigned int src_len)
537 int i, j, i_max, dst_len = 0;
541 /* There could be 1-3 1024 bytes URB frames */
542 i_max = src_len / 1024;
544 for (i = 0; i < i_max; i++) {
545 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
546 if (i == 0 && s->next_sample != sample_num[0]) {
547 dev_dbg_ratelimited(&s->udev->dev,
548 "%d samples lost, %d %08x:%08x\n",
549 sample_num[0] - s->next_sample,
550 src_len, s->next_sample, sample_num[0]);
554 * Dump all unknown 'garbage' data - maybe we will discover
555 * someday if there is something rational...
557 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
560 for (j = 0; j < 1008; j += 2) {
561 sample[0] = src[j + 0];
562 sample[1] = src[j + 1];
564 *dst++ = msi3101_convert_sample_504(s, sample[0]);
565 *dst++ = msi3101_convert_sample_504(s, sample[1]);
567 /* 504 x I+Q 32bit float samples */
568 dst_len += 504 * 2 * 4;
572 /* calculate samping rate and output it in 10 seconds intervals */
573 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
574 unsigned long jiffies_now = jiffies;
575 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
576 unsigned int samples = sample_num[i_max - 1] - s->sample;
577 s->jiffies = jiffies_now;
578 s->sample = sample_num[i_max - 1];
579 dev_dbg(&s->udev->dev,
580 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
581 src_len, samples, msecs,
582 samples * 1000UL / msecs);
585 /* next sample (sample = sample + i * 504) */
586 s->next_sample = sample_num[i_max - 1] + 504;
592 * Converts signed ~10+2-bit integer into 32-bit IEEE floating point
595 static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift)
597 u32 msb, exponent, fraction, sign;
598 s->sample_ctrl_bit[shift]++;
600 /* Zero is special */
607 /* Convert 10-bit two's complement to 12-bit */
609 x |= ~0U << 10; /* set all the rest bits to one */
612 x &= 0x7ff; /* result is 11 bit ... + sign */
619 /* Get location of the most significant bit */
622 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
623 exponent = (127 + msb) << I2F_FRAC_BITS;
625 return (fraction + exponent) | sign;
628 static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
629 u8 *src, unsigned int src_len)
631 int i, j, k, l, i_max, dst_len = 0;
636 /* There could be 1-3 1024 bytes URB frames */
637 i_max = src_len / 1024;
638 for (i = 0; i < i_max; i++) {
639 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
640 if (i == 0 && s->next_sample != sample_num[0]) {
641 dev_dbg_ratelimited(&s->udev->dev,
642 "%d samples lost, %d %08x:%08x\n",
643 sample_num[0] - s->next_sample,
644 src_len, s->next_sample, sample_num[0]);
648 * Dump all unknown 'garbage' data - maybe we will discover
649 * someday if there is something rational...
651 dev_dbg_ratelimited(&s->udev->dev,
652 "%*ph %*ph\n", 12, &src[4], 24, &src[1000]);
655 for (j = 0; j < 6; j++) {
656 bits = src[160 + 3] << 24 | src[160 + 2] << 16 | src[160 + 1] << 8 | src[160 + 0] << 0;
657 for (k = 0; k < 16; k++) {
658 for (l = 0; l < 10; l += 5) {
659 sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8;
660 sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6;
661 sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4;
662 sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2;
664 *dst++ = msi3101_convert_sample_384(s, sample[0], (bits >> (2 * k)) & 0x3);
665 *dst++ = msi3101_convert_sample_384(s, sample[1], (bits >> (2 * k)) & 0x3);
666 *dst++ = msi3101_convert_sample_384(s, sample[2], (bits >> (2 * k)) & 0x3);
667 *dst++ = msi3101_convert_sample_384(s, sample[3], (bits >> (2 * k)) & 0x3);
671 dev_dbg_ratelimited(&s->udev->dev,
672 "sample control bits %08x\n", bits);
675 /* 384 x I+Q 32bit float samples */
676 dst_len += 384 * 2 * 4;
680 /* calculate samping rate and output it in 10 seconds intervals */
681 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
682 unsigned long jiffies_now = jiffies;
683 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
684 unsigned int samples = sample_num[i_max - 1] - s->sample;
685 s->jiffies = jiffies_now;
686 s->sample = sample_num[i_max - 1];
687 dev_dbg(&s->udev->dev,
688 "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n",
689 src_len, samples, msecs,
690 samples * 1000UL / msecs,
691 s->sample_ctrl_bit[0], s->sample_ctrl_bit[1],
692 s->sample_ctrl_bit[2], s->sample_ctrl_bit[3]);
695 /* next sample (sample = sample + i * 384) */
696 s->next_sample = sample_num[i_max - 1] + 384;
702 * Converts signed 12-bit integer into 32-bit IEEE floating point
705 static u32 msi3101_convert_sample_336(struct msi3101_state *s, u16 x)
707 u32 msb, exponent, fraction, sign;
709 /* Zero is special */
713 /* Negative / positive value */
716 x &= 0x7ff; /* result is 11 bit ... + sign */
722 /* Get location of the most significant bit */
725 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
726 exponent = (127 + msb) << I2F_FRAC_BITS;
728 return (fraction + exponent) | sign;
731 static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst,
732 u8 *src, unsigned int src_len)
734 int i, j, i_max, dst_len = 0;
738 /* There could be 1-3 1024 bytes URB frames */
739 i_max = src_len / 1024;
741 for (i = 0; i < i_max; i++) {
742 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
743 if (i == 0 && s->next_sample != sample_num[0]) {
744 dev_dbg_ratelimited(&s->udev->dev,
745 "%d samples lost, %d %08x:%08x\n",
746 sample_num[0] - s->next_sample,
747 src_len, s->next_sample, sample_num[0]);
751 * Dump all unknown 'garbage' data - maybe we will discover
752 * someday if there is something rational...
754 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
757 for (j = 0; j < 1008; j += 3) {
758 sample[0] = (src[j + 0] & 0xff) >> 0 | (src[j + 1] & 0x0f) << 8;
759 sample[1] = (src[j + 1] & 0xf0) >> 4 | (src[j + 2] & 0xff) << 4;
761 *dst++ = msi3101_convert_sample_336(s, sample[0]);
762 *dst++ = msi3101_convert_sample_336(s, sample[1]);
764 /* 336 x I+Q 32bit float samples */
765 dst_len += 336 * 2 * 4;
769 /* calculate samping rate and output it in 10 seconds intervals */
770 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
771 unsigned long jiffies_now = jiffies;
772 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
773 unsigned int samples = sample_num[i_max - 1] - s->sample;
774 s->jiffies = jiffies_now;
775 s->sample = sample_num[i_max - 1];
776 dev_dbg(&s->udev->dev,
777 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
778 src_len, samples, msecs,
779 samples * 1000UL / msecs);
782 /* next sample (sample = sample + i * 336) */
783 s->next_sample = sample_num[i_max - 1] + 336;
789 * Converts signed 14-bit integer into 32-bit IEEE floating point
792 static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x)
794 u32 msb, exponent, fraction, sign;
796 /* Zero is special */
800 /* Negative / positive value */
803 x &= 0x1fff; /* result is 13 bit ... + sign */
809 /* Get location of the most significant bit */
812 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
813 exponent = (127 + msb) << I2F_FRAC_BITS;
815 return (fraction + exponent) | sign;
818 static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst,
819 u8 *src, unsigned int src_len)
821 int i, j, i_max, dst_len = 0;
825 /* There could be 1-3 1024 bytes URB frames */
826 i_max = src_len / 1024;
828 for (i = 0; i < i_max; i++) {
829 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
830 if (i == 0 && s->next_sample != sample_num[0]) {
831 dev_dbg_ratelimited(&s->udev->dev,
832 "%d samples lost, %d %08x:%08x\n",
833 sample_num[0] - s->next_sample,
834 src_len, s->next_sample, sample_num[0]);
838 * Dump all unknown 'garbage' data - maybe we will discover
839 * someday if there is something rational...
841 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
844 for (j = 0; j < 1008; j += 4) {
845 sample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
846 sample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
848 *dst++ = msi3101_convert_sample_252(s, sample[0]);
849 *dst++ = msi3101_convert_sample_252(s, sample[1]);
851 /* 252 x I+Q 32bit float samples */
852 dst_len += 252 * 2 * 4;
856 /* calculate samping rate and output it in 10 seconds intervals */
857 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
858 unsigned long jiffies_now = jiffies;
859 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
860 unsigned int samples = sample_num[i_max - 1] - s->sample;
861 s->jiffies = jiffies_now;
862 s->sample = sample_num[i_max - 1];
863 dev_dbg(&s->udev->dev,
864 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
865 src_len, samples, msecs,
866 samples * 1000UL / msecs);
869 /* next sample (sample = sample + i * 252) */
870 s->next_sample = sample_num[i_max - 1] + 252;
876 * This gets called for the Isochronous pipe (stream). This is done in interrupt
877 * time, so it has to be fast, not crash, and not stall. Neat.
879 static void msi3101_isoc_handler(struct urb *urb)
881 struct msi3101_state *s = (struct msi3101_state *)urb->context;
882 int i, flen, fstatus;
883 unsigned char *iso_buf = NULL;
884 struct msi3101_frame_buf *fbuf;
886 if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
887 urb->status == -ESHUTDOWN) {
888 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
889 urb, urb->status == -ENOENT ? "" : "a");
893 if (urb->status != 0) {
894 dev_dbg(&s->udev->dev,
895 "msi3101_isoc_handler() called with status %d\n",
897 /* Give up after a number of contiguous errors */
898 if (++s->isoc_errors > MAX_ISOC_ERRORS)
899 dev_dbg(&s->udev->dev,
900 "Too many ISOC errors, bailing out\n");
903 /* Reset ISOC error counter. We did get here, after all. */
908 for (i = 0; i < urb->number_of_packets; i++) {
911 /* Check frame error */
912 fstatus = urb->iso_frame_desc[i].status;
914 dev_dbg_ratelimited(&s->udev->dev,
915 "frame=%d/%d has error %d skipping\n",
916 i, urb->number_of_packets, fstatus);
920 /* Check if that frame contains data */
921 flen = urb->iso_frame_desc[i].actual_length;
925 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
927 /* Get free framebuffer */
928 fbuf = msi3101_get_next_fill_buf(s);
931 dev_dbg_ratelimited(&s->udev->dev,
932 "videobuf is full, %d packets dropped\n",
937 /* fill framebuffer */
938 ptr = vb2_plane_vaddr(&fbuf->vb, 0);
939 flen = s->convert_stream(s, ptr, iso_buf, flen);
940 vb2_set_plane_payload(&fbuf->vb, 0, flen);
941 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
947 i = usb_submit_urb(urb, GFP_ATOMIC);
949 dev_dbg(&s->udev->dev,
950 "Error (%d) re-submitting urb in msi3101_isoc_handler\n",
954 static void msi3101_iso_stop(struct msi3101_state *s)
957 dev_dbg(&s->udev->dev, "%s:\n", __func__);
959 /* Unlinking ISOC buffers one by one */
960 for (i = 0; i < MAX_ISO_BUFS; i++) {
962 dev_dbg(&s->udev->dev, "Unlinking URB %p\n",
964 usb_kill_urb(s->urbs[i]);
969 static void msi3101_iso_free(struct msi3101_state *s)
972 dev_dbg(&s->udev->dev, "%s:\n", __func__);
974 /* Freeing ISOC buffers one by one */
975 for (i = 0; i < MAX_ISO_BUFS; i++) {
977 dev_dbg(&s->udev->dev, "Freeing URB\n");
978 if (s->urbs[i]->transfer_buffer) {
979 usb_free_coherent(s->udev,
980 s->urbs[i]->transfer_buffer_length,
981 s->urbs[i]->transfer_buffer,
982 s->urbs[i]->transfer_dma);
984 usb_free_urb(s->urbs[i]);
990 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
991 static void msi3101_isoc_cleanup(struct msi3101_state *s)
993 dev_dbg(&s->udev->dev, "%s:\n", __func__);
999 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
1000 static int msi3101_isoc_init(struct msi3101_state *s)
1002 struct usb_device *udev;
1005 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1010 ret = usb_set_interface(s->udev, 0, 1);
1014 /* Allocate and init Isochronuous urbs */
1015 for (i = 0; i < MAX_ISO_BUFS; i++) {
1016 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
1018 dev_err(&s->udev->dev,
1019 "Failed to allocate urb %d\n", i);
1020 msi3101_isoc_cleanup(s);
1024 dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb);
1028 urb->pipe = usb_rcvisocpipe(udev, 0x81);
1029 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1030 urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE,
1031 GFP_KERNEL, &urb->transfer_dma);
1032 if (urb->transfer_buffer == NULL) {
1033 dev_err(&s->udev->dev,
1034 "Failed to allocate urb buffer %d\n",
1036 msi3101_isoc_cleanup(s);
1039 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
1040 urb->complete = msi3101_isoc_handler;
1042 urb->start_frame = 0;
1043 urb->number_of_packets = ISO_FRAMES_PER_DESC;
1044 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
1045 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
1046 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
1051 for (i = 0; i < MAX_ISO_BUFS; i++) {
1052 ret = usb_submit_urb(s->urbs[i], GFP_KERNEL);
1054 dev_err(&s->udev->dev,
1055 "isoc_init() submit_urb %d failed with error %d\n",
1057 msi3101_isoc_cleanup(s);
1060 dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]);
1063 /* All is done... */
1067 /* Must be called with vb_queue_lock hold */
1068 static void msi3101_cleanup_queued_bufs(struct msi3101_state *s)
1070 unsigned long flags = 0;
1071 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1073 spin_lock_irqsave(&s->queued_bufs_lock, flags);
1074 while (!list_empty(&s->queued_bufs)) {
1075 struct msi3101_frame_buf *buf;
1077 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf,
1079 list_del(&buf->list);
1080 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1082 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
1085 /* The user yanked out the cable... */
1086 static void msi3101_disconnect(struct usb_interface *intf)
1088 struct v4l2_device *v = usb_get_intfdata(intf);
1089 struct msi3101_state *s =
1090 container_of(v, struct msi3101_state, v4l2_dev);
1091 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1093 mutex_lock(&s->vb_queue_lock);
1094 mutex_lock(&s->v4l2_lock);
1095 /* No need to keep the urbs around after disconnection */
1098 v4l2_device_disconnect(&s->v4l2_dev);
1099 video_unregister_device(&s->vdev);
1100 mutex_unlock(&s->v4l2_lock);
1101 mutex_unlock(&s->vb_queue_lock);
1103 v4l2_device_put(&s->v4l2_dev);
1106 static int msi3101_querycap(struct file *file, void *fh,
1107 struct v4l2_capability *cap)
1109 struct msi3101_state *s = video_drvdata(file);
1110 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1112 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
1113 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
1114 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
1115 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1117 cap->device_caps = V4L2_CAP_TUNER;
1118 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1123 /* Videobuf2 operations */
1124 static int msi3101_queue_setup(struct vb2_queue *vq,
1125 const struct v4l2_format *fmt, unsigned int *nbuffers,
1126 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
1128 struct msi3101_state *s = vb2_get_drv_priv(vq);
1129 dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers);
1131 /* Absolute min and max number of buffers available for mmap() */
1135 * 3, wMaxPacketSize 3x 1024 bytes
1136 * 504, max IQ sample pairs per 1024 frame
1137 * 2, two samples, I and Q
1140 sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 4); /* = 12096 */
1141 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
1142 __func__, *nbuffers, sizes[0]);
1146 static int msi3101_buf_prepare(struct vb2_buffer *vb)
1148 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
1150 /* Don't allow queing new buffers after device disconnection */
1157 static void msi3101_buf_queue(struct vb2_buffer *vb)
1159 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
1160 struct msi3101_frame_buf *buf =
1161 container_of(vb, struct msi3101_frame_buf, vb);
1162 unsigned long flags = 0;
1164 /* Check the device has not disconnected between prep and queuing */
1166 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1170 spin_lock_irqsave(&s->queued_bufs_lock, flags);
1171 list_add_tail(&buf->list, &s->queued_bufs);
1172 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
1175 #define CMD_WREG 0x41
1176 #define CMD_START_STREAMING 0x43
1177 #define CMD_STOP_STREAMING 0x45
1178 #define CMD_READ_UNKNOW 0x48
1180 #define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \
1182 if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
1183 direction = ">>>"; \
1185 direction = "<<<"; \
1186 dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
1187 "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \
1188 _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \
1191 static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data)
1195 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
1196 u16 value = (data >> 0) & 0xffff;
1197 u16 index = (data >> 16) & 0xffff;
1199 msi3101_dbg_usb_control_msg(s->udev,
1200 request, requesttype, value, index, NULL, 0);
1202 ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0),
1203 request, requesttype, value, index, NULL, 0, 2000);
1206 dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n",
1207 __func__, ret, cmd, data);
1212 static int msi3101_tuner_write(struct msi3101_state *s, u32 data)
1214 return msi3101_ctrl_msg(s, CMD_WREG, data << 8 | 0x09);
1217 #define F_REF 24000000
1219 static int msi3101_set_usb_adc(struct msi3101_state *s)
1221 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
1222 u32 reg3, reg4, reg7;
1224 f_sr = s->ctrl_sampling_rate->val64;
1226 /* select stream format */
1227 if (f_sr < 6000000) {
1228 s->convert_stream = msi3101_convert_stream_252;
1230 } else if (f_sr < 8000000) {
1231 s->convert_stream = msi3101_convert_stream_336;
1233 } else if (f_sr < 9000000) {
1234 s->convert_stream = msi3101_convert_stream_384;
1237 s->convert_stream = msi3101_convert_stream_504;
1242 * Synthesizer config is just a educated guess...
1244 * [7:0] 0x03, register address
1247 * [12:10] output divider
1250 * [15] fractional MSB, bit 20
1266 * VCO 202000000 - 720000000++
1271 /* XXX: Filters? AGC? */
1274 else if (f_sr < 7000000)
1276 else if (f_sr < 8500000)
1281 for (div_r_out = 4; div_r_out < 16; div_r_out += 2) {
1282 f_vco = f_sr * div_r_out * 12;
1283 dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n",
1284 __func__, div_r_out, f_vco);
1285 if (f_vco >= 202000000)
1289 div_n = f_vco / (F_REF * DIV_R_IN);
1290 div_m = f_vco % (F_REF * DIV_R_IN);
1291 fract = 0x200000ul * div_m / (F_REF * DIV_R_IN);
1293 reg3 |= div_n << 16;
1294 reg3 |= (div_r_out / 2 - 1) << 10;
1295 reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */
1296 reg4 |= ((fract >> 0) & 0x0fffff) << 8; /* [19:0] */
1298 dev_dbg(&s->udev->dev,
1299 "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n",
1300 __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg3, reg4);
1302 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008);
1306 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05);
1310 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000);
1314 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102);
1318 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008);
1322 ret = msi3101_ctrl_msg(s, CMD_WREG, reg7);
1326 ret = msi3101_ctrl_msg(s, CMD_WREG, reg4);
1330 ret = msi3101_ctrl_msg(s, CMD_WREG, reg3);
1337 static int msi3101_set_tuner(struct msi3101_state *s)
1340 unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;
1343 u8 mode, filter_mode, lo_div;
1344 const struct msi3101_gain *gain_lut;
1345 static const struct {
1350 { 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
1351 {108000000, 0x42, 32}, /* VHF_MODE */
1352 {330000000, 0x44, 16}, /* B3_MODE */
1353 {960000000, 0x48, 4}, /* B45_MODE */
1354 { ~0U, 0x50, 2}, /* BL_MODE */
1356 static const struct {
1360 { 0, 0x03}, /* Zero IF */
1361 { 450000, 0x02}, /* 450 kHz IF */
1362 {1620000, 0x01}, /* 1.62 MHz IF */
1363 {2048000, 0x00}, /* 2.048 MHz IF */
1365 static const struct {
1368 } bandwidth_lut[] = {
1369 { 200000, 0x00}, /* 200 kHz */
1370 { 300000, 0x01}, /* 300 kHz */
1371 { 600000, 0x02}, /* 600 kHz */
1372 {1536000, 0x03}, /* 1.536 MHz */
1373 {5000000, 0x04}, /* 5 MHz */
1374 {6000000, 0x05}, /* 6 MHz */
1375 {7000000, 0x06}, /* 7 MHz */
1376 {8000000, 0x07}, /* 8 MHz */
1379 unsigned int f_rf = s->ctrl_tuner_rf->val64;
1383 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
1385 unsigned int bandwidth = s->ctrl_tuner_bw->val;
1388 * intermediate frequency (Hz)
1389 * 0, 450000, 1620000, 2048000
1391 unsigned int f_if = s->ctrl_tuner_if->val;
1394 * gain reduction (dB)
1395 * 0 - 102 below 420 MHz
1396 * 0 - 85 above 420 MHz
1398 int gain = s->ctrl_tuner_gain->val;
1400 dev_dbg(&s->udev->dev,
1401 "%s: f_rf=%d bandwidth=%d f_if=%d gain=%d\n",
1402 __func__, f_rf, bandwidth, f_if, gain);
1406 for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
1407 if (f_rf <= band_lut[i].rf) {
1408 mode = band_lut[i].mode;
1409 lo_div = band_lut[i].lo_div;
1414 if (i == ARRAY_SIZE(band_lut))
1417 /* AM_MODE is upconverted */
1418 if ((mode >> 0) & 0x1)
1423 for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
1424 if (f_if == if_freq_lut[i].freq) {
1425 filter_mode = if_freq_lut[i].filter_mode;
1430 if (i == ARRAY_SIZE(if_freq_lut))
1433 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
1434 if (bandwidth == bandwidth_lut[i].freq) {
1435 bandwidth = bandwidth_lut[i].val;
1440 if (i == ARRAY_SIZE(bandwidth_lut))
1443 #define F_OUT_STEP 1
1445 f_vco = (f_rf + f_if + f_if1) * lo_div;
1448 m = do_div(tmp64, F_REF * R_REF);
1449 n = (unsigned int) tmp64;
1451 vco_step = F_OUT_STEP * lo_div;
1452 thresh = (F_REF * R_REF) / vco_step;
1453 frac = 1ul * thresh * m / (F_REF * R_REF);
1455 /* Find out greatest common divisor and divide to smaller. */
1456 tmp = gcd(thresh, frac);
1460 /* Force divide to reg max. Resolution will be reduced. */
1461 tmp = DIV_ROUND_UP(thresh, 4095);
1462 thresh = DIV_ROUND_CLOSEST(thresh, tmp);
1463 frac = DIV_ROUND_CLOSEST(frac, tmp);
1465 /* calc real RF set */
1466 tmp = 1ul * F_REF * R_REF * n;
1467 tmp += 1ul * F_REF * R_REF * frac / thresh;
1470 dev_dbg(&s->udev->dev,
1471 "%s: rf=%u:%u n=%d thresh=%d frac=%d\n",
1472 __func__, f_rf, tmp, n, thresh, frac);
1474 ret = msi3101_tuner_write(s, 0x00000e);
1478 ret = msi3101_tuner_write(s, 0x000003);
1484 reg |= filter_mode << 12;
1485 reg |= bandwidth << 14;
1488 ret = msi3101_tuner_write(s, reg);
1496 ret = msi3101_tuner_write(s, reg);
1503 ret = msi3101_tuner_write(s, reg);
1507 if (f_rf < 120000000) {
1508 gain_lut = msi3101_gain_lut_120;
1509 len = ARRAY_SIZE(msi3101_gain_lut_120);
1510 } else if (f_rf < 245000000) {
1511 gain_lut = msi3101_gain_lut_245;
1512 len = ARRAY_SIZE(msi3101_gain_lut_120);
1514 gain_lut = msi3101_gain_lut_1000;
1515 len = ARRAY_SIZE(msi3101_gain_lut_1000);
1518 for (i = 0; i < len; i++) {
1519 if (gain_lut[i].tot >= gain)
1526 dev_dbg(&s->udev->dev,
1527 "%s: gain tot=%d baseband=%d lna=%d mixer=%d\n",
1528 __func__, gain_lut[i].tot, gain_lut[i].baseband,
1529 gain_lut[i].lna, gain_lut[i].mixer);
1532 reg |= gain_lut[i].baseband << 4;
1534 reg |= gain_lut[i].mixer << 12;
1535 reg |= gain_lut[i].lna << 13;
1538 ret = msi3101_tuner_write(s, reg);
1545 ret = msi3101_tuner_write(s, reg);
1551 dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
1555 static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
1557 struct msi3101_state *s = vb2_get_drv_priv(vq);
1559 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1564 if (mutex_lock_interruptible(&s->v4l2_lock))
1565 return -ERESTARTSYS;
1567 ret = msi3101_set_usb_adc(s);
1569 ret = msi3101_isoc_init(s);
1571 msi3101_cleanup_queued_bufs(s);
1573 ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0);
1575 mutex_unlock(&s->v4l2_lock);
1580 static int msi3101_stop_streaming(struct vb2_queue *vq)
1582 struct msi3101_state *s = vb2_get_drv_priv(vq);
1583 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1585 if (mutex_lock_interruptible(&s->v4l2_lock))
1586 return -ERESTARTSYS;
1589 msi3101_isoc_cleanup(s);
1591 msi3101_cleanup_queued_bufs(s);
1593 /* according to tests, at least 700us delay is required */
1595 msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0);
1597 mutex_unlock(&s->v4l2_lock);
1602 static struct vb2_ops msi3101_vb2_ops = {
1603 .queue_setup = msi3101_queue_setup,
1604 .buf_prepare = msi3101_buf_prepare,
1605 .buf_queue = msi3101_buf_queue,
1606 .start_streaming = msi3101_start_streaming,
1607 .stop_streaming = msi3101_stop_streaming,
1608 .wait_prepare = vb2_ops_wait_prepare,
1609 .wait_finish = vb2_ops_wait_finish,
1612 static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i)
1617 strlcpy(i->name, "SDR data", sizeof(i->name));
1618 i->type = V4L2_INPUT_TYPE_CAMERA;
1623 static int msi3101_g_input(struct file *file, void *fh, unsigned int *i)
1630 static int msi3101_s_input(struct file *file, void *fh, unsigned int i)
1632 return i ? -EINVAL : 0;
1635 static int vidioc_s_tuner(struct file *file, void *priv,
1636 const struct v4l2_tuner *v)
1638 struct msi3101_state *s = video_drvdata(file);
1639 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1644 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
1646 struct msi3101_state *s = video_drvdata(file);
1647 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1649 strcpy(v->name, "SDR RX");
1650 v->capability = V4L2_TUNER_CAP_LOW;
1655 static int vidioc_s_frequency(struct file *file, void *priv,
1656 const struct v4l2_frequency *f)
1658 struct msi3101_state *s = video_drvdata(file);
1659 dev_dbg(&s->udev->dev, "%s: frequency=%lu Hz (%u)\n",
1660 __func__, f->frequency * 625UL / 10UL, f->frequency);
1662 return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf,
1663 f->frequency * 625UL / 10UL);
1666 static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
1667 .vidioc_querycap = msi3101_querycap,
1669 .vidioc_enum_input = msi3101_enum_input,
1670 .vidioc_g_input = msi3101_g_input,
1671 .vidioc_s_input = msi3101_s_input,
1673 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1674 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1675 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1676 .vidioc_querybuf = vb2_ioctl_querybuf,
1677 .vidioc_qbuf = vb2_ioctl_qbuf,
1678 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1680 .vidioc_streamon = vb2_ioctl_streamon,
1681 .vidioc_streamoff = vb2_ioctl_streamoff,
1683 .vidioc_g_tuner = vidioc_g_tuner,
1684 .vidioc_s_tuner = vidioc_s_tuner,
1685 .vidioc_s_frequency = vidioc_s_frequency,
1687 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1688 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1689 .vidioc_log_status = v4l2_ctrl_log_status,
1692 static const struct v4l2_file_operations msi3101_fops = {
1693 .owner = THIS_MODULE,
1694 .open = v4l2_fh_open,
1695 .release = vb2_fop_release,
1696 .read = vb2_fop_read,
1697 .poll = vb2_fop_poll,
1698 .mmap = vb2_fop_mmap,
1699 .unlocked_ioctl = video_ioctl2,
1702 static struct video_device msi3101_template = {
1703 .name = "Mirics MSi3101 SDR Dongle",
1704 .release = video_device_release_empty,
1705 .fops = &msi3101_fops,
1706 .ioctl_ops = &msi3101_ioctl_ops,
1709 static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
1711 struct msi3101_state *s =
1712 container_of(ctrl->handler, struct msi3101_state,
1715 dev_dbg(&s->udev->dev,
1716 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
1717 __func__, ctrl->id, ctrl->name, ctrl->val,
1718 ctrl->minimum, ctrl->maximum, ctrl->step);
1721 case MSI3101_CID_SAMPLING_MODE:
1722 case MSI3101_CID_SAMPLING_RATE:
1723 case MSI3101_CID_SAMPLING_RESOLUTION:
1726 case MSI3101_CID_TUNER_RF:
1727 case MSI3101_CID_TUNER_BW:
1728 case MSI3101_CID_TUNER_IF:
1729 case MSI3101_CID_TUNER_GAIN:
1730 ret = msi3101_set_tuner(s);
1739 static const struct v4l2_ctrl_ops msi3101_ctrl_ops = {
1740 .s_ctrl = msi3101_s_ctrl,
1743 static void msi3101_video_release(struct v4l2_device *v)
1745 struct msi3101_state *s =
1746 container_of(v, struct msi3101_state, v4l2_dev);
1748 v4l2_ctrl_handler_free(&s->ctrl_handler);
1749 v4l2_device_unregister(&s->v4l2_dev);
1753 static int msi3101_probe(struct usb_interface *intf,
1754 const struct usb_device_id *id)
1756 struct usb_device *udev = interface_to_usbdev(intf);
1757 struct msi3101_state *s = NULL;
1759 static const char * const ctrl_sampling_mode_qmenu_strings[] = {
1760 "Quadrature Sampling",
1763 static const struct v4l2_ctrl_config ctrl_sampling_mode = {
1764 .ops = &msi3101_ctrl_ops,
1765 .id = MSI3101_CID_SAMPLING_MODE,
1766 .type = V4L2_CTRL_TYPE_MENU,
1767 .flags = V4L2_CTRL_FLAG_INACTIVE,
1768 .name = "Sampling Mode",
1769 .qmenu = ctrl_sampling_mode_qmenu_strings,
1771 static const struct v4l2_ctrl_config ctrl_sampling_rate = {
1772 .ops = &msi3101_ctrl_ops,
1773 .id = MSI3101_CID_SAMPLING_RATE,
1774 .type = V4L2_CTRL_TYPE_INTEGER64,
1775 .name = "Sampling Rate",
1781 static const struct v4l2_ctrl_config ctrl_sampling_resolution = {
1782 .ops = &msi3101_ctrl_ops,
1783 .id = MSI3101_CID_SAMPLING_RESOLUTION,
1784 .type = V4L2_CTRL_TYPE_INTEGER,
1785 .flags = V4L2_CTRL_FLAG_INACTIVE,
1786 .name = "Sampling Resolution",
1792 static const struct v4l2_ctrl_config ctrl_tuner_rf = {
1793 .ops = &msi3101_ctrl_ops,
1794 .id = MSI3101_CID_TUNER_RF,
1795 .type = V4L2_CTRL_TYPE_INTEGER64,
1802 static const struct v4l2_ctrl_config ctrl_tuner_bw = {
1803 .ops = &msi3101_ctrl_ops,
1804 .id = MSI3101_CID_TUNER_BW,
1805 .type = V4L2_CTRL_TYPE_INTEGER,
1812 static const struct v4l2_ctrl_config ctrl_tuner_if = {
1813 .ops = &msi3101_ctrl_ops,
1814 .id = MSI3101_CID_TUNER_IF,
1815 .type = V4L2_CTRL_TYPE_INTEGER,
1816 .flags = V4L2_CTRL_FLAG_INACTIVE,
1823 static const struct v4l2_ctrl_config ctrl_tuner_gain = {
1824 .ops = &msi3101_ctrl_ops,
1825 .id = MSI3101_CID_TUNER_GAIN,
1826 .type = V4L2_CTRL_TYPE_INTEGER,
1827 .name = "Tuner Gain",
1834 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1836 pr_err("Could not allocate memory for msi3101_state\n");
1840 mutex_init(&s->v4l2_lock);
1841 mutex_init(&s->vb_queue_lock);
1842 spin_lock_init(&s->queued_bufs_lock);
1843 INIT_LIST_HEAD(&s->queued_bufs);
1847 /* Init videobuf2 queue structure */
1848 s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1849 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1850 s->vb_queue.drv_priv = s;
1851 s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
1852 s->vb_queue.ops = &msi3101_vb2_ops;
1853 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
1854 s->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1855 ret = vb2_queue_init(&s->vb_queue);
1857 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
1861 /* Init video_device structure */
1862 s->vdev = msi3101_template;
1863 s->vdev.queue = &s->vb_queue;
1864 s->vdev.queue->lock = &s->vb_queue_lock;
1865 set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags);
1866 video_set_drvdata(&s->vdev, s);
1868 /* Register controls */
1869 v4l2_ctrl_handler_init(&s->ctrl_handler, 7);
1870 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_mode, NULL);
1871 s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL);
1872 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL);
1873 s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL);
1874 s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL);
1875 s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL);
1876 s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL);
1877 if (s->ctrl_handler.error) {
1878 ret = s->ctrl_handler.error;
1879 dev_err(&s->udev->dev, "Could not initialize controls\n");
1880 goto err_free_controls;
1883 /* Register the v4l2_device structure */
1884 s->v4l2_dev.release = msi3101_video_release;
1885 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1887 dev_err(&s->udev->dev,
1888 "Failed to register v4l2-device (%d)\n", ret);
1889 goto err_free_controls;
1892 s->v4l2_dev.ctrl_handler = &s->ctrl_handler;
1893 s->vdev.v4l2_dev = &s->v4l2_dev;
1894 s->vdev.lock = &s->v4l2_lock;
1896 ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1);
1898 dev_err(&s->udev->dev,
1899 "Failed to register as video device (%d)\n",
1901 goto err_unregister_v4l2_dev;
1903 dev_info(&s->udev->dev, "Registered as %s\n",
1904 video_device_node_name(&s->vdev));
1908 err_unregister_v4l2_dev:
1909 v4l2_device_unregister(&s->v4l2_dev);
1911 v4l2_ctrl_handler_free(&s->ctrl_handler);
1917 /* USB device ID list */
1918 static struct usb_device_id msi3101_id_table[] = {
1919 { USB_DEVICE(0x1df7, 0x2500) }, /* Mirics MSi3101 SDR Dongle */
1920 { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */
1923 MODULE_DEVICE_TABLE(usb, msi3101_id_table);
1925 /* USB subsystem interface */
1926 static struct usb_driver msi3101_driver = {
1927 .name = KBUILD_MODNAME,
1928 .probe = msi3101_probe,
1929 .disconnect = msi3101_disconnect,
1930 .id_table = msi3101_id_table,
1933 module_usb_driver(msi3101_driver);
1935 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1936 MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1937 MODULE_LICENSE("GPL");