All of lore.kernel.org
 help / color / mirror / Atom feed
* [REVIEW PATCH 0/6] SDR API - Mirics MSi3101 driver
@ 2014-02-27  0:25 Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 1/6] msi3101: convert to SDR API Antti Palosaari
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Antti Palosaari @ 2014-02-27  0:25 UTC (permalink / raw)
  To: linux-media; +Cc: Hans Verkuil, Antti Palosaari

Mirics MSi3101 driver.

Antti

Antti Palosaari (6):
  msi3101: convert to SDR API
  msi001: Mirics MSi001 silicon tuner driver
  msi3101: use msi001 tuner driver
  MAINTAINERS: add msi001 driver
  MAINTAINERS: add msi3101 driver
  msi3101: clamp mmap buffers to reasonable level

 MAINTAINERS                                 |   20 +
 drivers/staging/media/msi3101/Kconfig       |    7 +-
 drivers/staging/media/msi3101/Makefile      |    1 +
 drivers/staging/media/msi3101/msi001.c      |  499 +++++++++
 drivers/staging/media/msi3101/sdr-msi3101.c | 1560 ++++++++++-----------------
 5 files changed, 1096 insertions(+), 991 deletions(-)
 create mode 100644 drivers/staging/media/msi3101/msi001.c

-- 
1.8.5.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [REVIEW PATCH 1/6] msi3101: convert to SDR API
  2014-02-27  0:25 [REVIEW PATCH 0/6] SDR API - Mirics MSi3101 driver Antti Palosaari
@ 2014-02-27  0:25 ` Antti Palosaari
  2014-03-13 13:21   ` Mauro Carvalho Chehab
  2014-02-27  0:25 ` [REVIEW PATCH 2/6] msi001: Mirics MSi001 silicon tuner driver Antti Palosaari
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Antti Palosaari @ 2014-02-27  0:25 UTC (permalink / raw)
  To: linux-media; +Cc: Hans Verkuil, Antti Palosaari

Massive rewrite. Use SDR API. Fix bugs.

Signed-off-by: Antti Palosaari <crope@iki.fi>
Acked-by: Hans Verkuil <hans.verkuil@cisco.com>
---
 drivers/staging/media/msi3101/sdr-msi3101.c | 1351 ++++++++++++---------------
 1 file changed, 573 insertions(+), 778 deletions(-)

diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c
index 4c3bf77..5da0ad1 100644
--- a/drivers/staging/media/msi3101/sdr-msi3101.c
+++ b/drivers/staging/media/msi3101/sdr-msi3101.c
@@ -21,20 +21,6 @@
  *  (C) 1999-2004 Nemosoft Unv.
  *  (C) 2004-2006 Luc Saillard (luc@saillard.org)
  *  (C) 2011 Hans de Goede <hdegoede@redhat.com>
- *
- * Development tree of that driver will be on:
- * http://git.linuxtv.org/anttip/media_tree.git/shortlog/refs/heads/mirics
- *
- * GNU Radio plugin "gr-kernel" for device usage will be on:
- * http://git.linuxtv.org/anttip/gr-kernel.git
- *
- * TODO:
- * Help is very highly welcome for these + all the others you could imagine:
- * - split USB ADC interface and RF tuner to own drivers (msi2500 and msi001)
- * - move controls to V4L2 API
- * - use libv4l2 for stream format conversions
- * - gr-kernel: switch to v4l2_mmap (current read eats a lot of cpu)
- * - SDRSharp support
  */
 
 #include <linux/module.h>
@@ -48,317 +34,6 @@
 #include <linux/usb.h>
 #include <media/videobuf2-vmalloc.h>
 
-struct msi3101_gain {
-	u8 tot:7;
-	u8 baseband:6;
-	bool lna:1;
-	bool mixer:1;
-};
-
-/* 60 – 120 MHz band, lna 24dB, mixer 19dB */
-static const struct msi3101_gain msi3101_gain_lut_120[] = {
-	{  0,  0,  0,  0},
-	{  1,  1,  0,  0},
-	{  2,  2,  0,  0},
-	{  3,  3,  0,  0},
-	{  4,  4,  0,  0},
-	{  5,  5,  0,  0},
-	{  6,  6,  0,  0},
-	{  7,  7,  0,  0},
-	{  8,  8,  0,  0},
-	{  9,  9,  0,  0},
-	{ 10, 10,  0,  0},
-	{ 11, 11,  0,  0},
-	{ 12, 12,  0,  0},
-	{ 13, 13,  0,  0},
-	{ 14, 14,  0,  0},
-	{ 15, 15,  0,  0},
-	{ 16, 16,  0,  0},
-	{ 17, 17,  0,  0},
-	{ 18, 18,  0,  0},
-	{ 19, 19,  0,  0},
-	{ 20, 20,  0,  0},
-	{ 21, 21,  0,  0},
-	{ 22, 22,  0,  0},
-	{ 23, 23,  0,  0},
-	{ 24, 24,  0,  0},
-	{ 25, 25,  0,  0},
-	{ 26, 26,  0,  0},
-	{ 27, 27,  0,  0},
-	{ 28, 28,  0,  0},
-	{ 29,  5,  1,  0},
-	{ 30,  6,  1,  0},
-	{ 31,  7,  1,  0},
-	{ 32,  8,  1,  0},
-	{ 33,  9,  1,  0},
-	{ 34, 10,  1,  0},
-	{ 35, 11,  1,  0},
-	{ 36, 12,  1,  0},
-	{ 37, 13,  1,  0},
-	{ 38, 14,  1,  0},
-	{ 39, 15,  1,  0},
-	{ 40, 16,  1,  0},
-	{ 41, 17,  1,  0},
-	{ 42, 18,  1,  0},
-	{ 43, 19,  1,  0},
-	{ 44, 20,  1,  0},
-	{ 45, 21,  1,  0},
-	{ 46, 22,  1,  0},
-	{ 47, 23,  1,  0},
-	{ 48, 24,  1,  0},
-	{ 49, 25,  1,  0},
-	{ 50, 26,  1,  0},
-	{ 51, 27,  1,  0},
-	{ 52, 28,  1,  0},
-	{ 53, 29,  1,  0},
-	{ 54, 30,  1,  0},
-	{ 55, 31,  1,  0},
-	{ 56, 32,  1,  0},
-	{ 57, 33,  1,  0},
-	{ 58, 34,  1,  0},
-	{ 59, 35,  1,  0},
-	{ 60, 36,  1,  0},
-	{ 61, 37,  1,  0},
-	{ 62, 38,  1,  0},
-	{ 63, 39,  1,  0},
-	{ 64, 40,  1,  0},
-	{ 65, 41,  1,  0},
-	{ 66, 42,  1,  0},
-	{ 67, 43,  1,  0},
-	{ 68, 44,  1,  0},
-	{ 69, 45,  1,  0},
-	{ 70, 46,  1,  0},
-	{ 71, 47,  1,  0},
-	{ 72, 48,  1,  0},
-	{ 73, 49,  1,  0},
-	{ 74, 50,  1,  0},
-	{ 75, 51,  1,  0},
-	{ 76, 52,  1,  0},
-	{ 77, 53,  1,  0},
-	{ 78, 54,  1,  0},
-	{ 79, 55,  1,  0},
-	{ 80, 56,  1,  0},
-	{ 81, 57,  1,  0},
-	{ 82, 58,  1,  0},
-	{ 83, 40,  1,  1},
-	{ 84, 41,  1,  1},
-	{ 85, 42,  1,  1},
-	{ 86, 43,  1,  1},
-	{ 87, 44,  1,  1},
-	{ 88, 45,  1,  1},
-	{ 89, 46,  1,  1},
-	{ 90, 47,  1,  1},
-	{ 91, 48,  1,  1},
-	{ 92, 49,  1,  1},
-	{ 93, 50,  1,  1},
-	{ 94, 51,  1,  1},
-	{ 95, 52,  1,  1},
-	{ 96, 53,  1,  1},
-	{ 97, 54,  1,  1},
-	{ 98, 55,  1,  1},
-	{ 99, 56,  1,  1},
-	{100, 57,  1,  1},
-	{101, 58,  1,  1},
-	{102, 59,  1,  1},
-};
-
-/* 120 – 245 MHz band, lna 24dB, mixer 19dB */
-static const struct msi3101_gain msi3101_gain_lut_245[] = {
-	{  0,  0,  0,  0},
-	{  1,  1,  0,  0},
-	{  2,  2,  0,  0},
-	{  3,  3,  0,  0},
-	{  4,  4,  0,  0},
-	{  5,  5,  0,  0},
-	{  6,  6,  0,  0},
-	{  7,  7,  0,  0},
-	{  8,  8,  0,  0},
-	{  9,  9,  0,  0},
-	{ 10, 10,  0,  0},
-	{ 11, 11,  0,  0},
-	{ 12, 12,  0,  0},
-	{ 13, 13,  0,  0},
-	{ 14, 14,  0,  0},
-	{ 15, 15,  0,  0},
-	{ 16, 16,  0,  0},
-	{ 17, 17,  0,  0},
-	{ 18, 18,  0,  0},
-	{ 19, 19,  0,  0},
-	{ 20, 20,  0,  0},
-	{ 21, 21,  0,  0},
-	{ 22, 22,  0,  0},
-	{ 23, 23,  0,  0},
-	{ 24, 24,  0,  0},
-	{ 25, 25,  0,  0},
-	{ 26, 26,  0,  0},
-	{ 27, 27,  0,  0},
-	{ 28, 28,  0,  0},
-	{ 29,  5,  1,  0},
-	{ 30,  6,  1,  0},
-	{ 31,  7,  1,  0},
-	{ 32,  8,  1,  0},
-	{ 33,  9,  1,  0},
-	{ 34, 10,  1,  0},
-	{ 35, 11,  1,  0},
-	{ 36, 12,  1,  0},
-	{ 37, 13,  1,  0},
-	{ 38, 14,  1,  0},
-	{ 39, 15,  1,  0},
-	{ 40, 16,  1,  0},
-	{ 41, 17,  1,  0},
-	{ 42, 18,  1,  0},
-	{ 43, 19,  1,  0},
-	{ 44, 20,  1,  0},
-	{ 45, 21,  1,  0},
-	{ 46, 22,  1,  0},
-	{ 47, 23,  1,  0},
-	{ 48, 24,  1,  0},
-	{ 49, 25,  1,  0},
-	{ 50, 26,  1,  0},
-	{ 51, 27,  1,  0},
-	{ 52, 28,  1,  0},
-	{ 53, 29,  1,  0},
-	{ 54, 30,  1,  0},
-	{ 55, 31,  1,  0},
-	{ 56, 32,  1,  0},
-	{ 57, 33,  1,  0},
-	{ 58, 34,  1,  0},
-	{ 59, 35,  1,  0},
-	{ 60, 36,  1,  0},
-	{ 61, 37,  1,  0},
-	{ 62, 38,  1,  0},
-	{ 63, 39,  1,  0},
-	{ 64, 40,  1,  0},
-	{ 65, 41,  1,  0},
-	{ 66, 42,  1,  0},
-	{ 67, 43,  1,  0},
-	{ 68, 44,  1,  0},
-	{ 69, 45,  1,  0},
-	{ 70, 46,  1,  0},
-	{ 71, 47,  1,  0},
-	{ 72, 48,  1,  0},
-	{ 73, 49,  1,  0},
-	{ 74, 50,  1,  0},
-	{ 75, 51,  1,  0},
-	{ 76, 52,  1,  0},
-	{ 77, 53,  1,  0},
-	{ 78, 54,  1,  0},
-	{ 79, 55,  1,  0},
-	{ 80, 56,  1,  0},
-	{ 81, 57,  1,  0},
-	{ 82, 58,  1,  0},
-	{ 83, 40,  1,  1},
-	{ 84, 41,  1,  1},
-	{ 85, 42,  1,  1},
-	{ 86, 43,  1,  1},
-	{ 87, 44,  1,  1},
-	{ 88, 45,  1,  1},
-	{ 89, 46,  1,  1},
-	{ 90, 47,  1,  1},
-	{ 91, 48,  1,  1},
-	{ 92, 49,  1,  1},
-	{ 93, 50,  1,  1},
-	{ 94, 51,  1,  1},
-	{ 95, 52,  1,  1},
-	{ 96, 53,  1,  1},
-	{ 97, 54,  1,  1},
-	{ 98, 55,  1,  1},
-	{ 99, 56,  1,  1},
-	{100, 57,  1,  1},
-	{101, 58,  1,  1},
-	{102, 59,  1,  1},
-};
-
-/* 420 – 1000 MHz band, lna 7dB, mixer 19dB */
-static const struct msi3101_gain msi3101_gain_lut_1000[] = {
-	{  0,  0, 0,  0},
-	{  1,  1, 0,  0},
-	{  2,  2, 0,  0},
-	{  3,  3, 0,  0},
-	{  4,  4, 0,  0},
-	{  5,  5, 0,  0},
-	{  6,  6, 0,  0},
-	{  7,  7, 0,  0},
-	{  8,  8, 0,  0},
-	{  9,  9, 0,  0},
-	{ 10, 10, 0,  0},
-	{ 11, 11, 0,  0},
-	{ 12,  5, 1,  0},
-	{ 13,  6, 1,  0},
-	{ 14,  7, 1,  0},
-	{ 15,  8, 1,  0},
-	{ 16,  9, 1,  0},
-	{ 17, 10, 1,  0},
-	{ 18, 11, 1,  0},
-	{ 19, 12, 1,  0},
-	{ 20, 13, 1,  0},
-	{ 21, 14, 1,  0},
-	{ 22, 15, 1,  0},
-	{ 23, 16, 1,  0},
-	{ 24, 17, 1,  0},
-	{ 25, 18, 1,  0},
-	{ 26, 19, 1,  0},
-	{ 27, 20, 1,  0},
-	{ 28, 21, 1,  0},
-	{ 29, 22, 1,  0},
-	{ 30, 23, 1,  0},
-	{ 31, 24, 1,  0},
-	{ 32, 25, 1,  0},
-	{ 33, 26, 1,  0},
-	{ 34, 27, 1,  0},
-	{ 35, 28, 1,  0},
-	{ 36, 29, 1,  0},
-	{ 37, 30, 1,  0},
-	{ 38, 31, 1,  0},
-	{ 39, 32, 1,  0},
-	{ 40, 33, 1,  0},
-	{ 41, 34, 1,  0},
-	{ 42, 35, 1,  0},
-	{ 43, 36, 1,  0},
-	{ 44, 37, 1,  0},
-	{ 45, 38, 1,  0},
-	{ 46, 39, 1,  0},
-	{ 47, 40, 1,  0},
-	{ 48, 41, 1,  0},
-	{ 49, 42, 1,  0},
-	{ 50, 43, 1,  0},
-	{ 51, 44, 1,  0},
-	{ 52, 45, 1,  0},
-	{ 53, 46, 1,  0},
-	{ 54, 47, 1,  0},
-	{ 55, 48, 1,  0},
-	{ 56, 49, 1,  0},
-	{ 57, 50, 1,  0},
-	{ 58, 51, 1,  0},
-	{ 59, 52, 1,  0},
-	{ 60, 53, 1,  0},
-	{ 61, 54, 1,  0},
-	{ 62, 55, 1,  0},
-	{ 63, 56, 1,  0},
-	{ 64, 57, 1,  0},
-	{ 65, 58, 1,  0},
-	{ 66, 40, 1,  1},
-	{ 67, 41, 1,  1},
-	{ 68, 42, 1,  1},
-	{ 69, 43, 1,  1},
-	{ 70, 44, 1,  1},
-	{ 71, 45, 1,  1},
-	{ 72, 46, 1,  1},
-	{ 73, 47, 1,  1},
-	{ 74, 48, 1,  1},
-	{ 75, 49, 1,  1},
-	{ 76, 50, 1,  1},
-	{ 77, 51, 1,  1},
-	{ 78, 52, 1,  1},
-	{ 79, 53, 1,  1},
-	{ 80, 54, 1,  1},
-	{ 81, 55, 1,  1},
-	{ 82, 56, 1,  1},
-	{ 83, 57, 1,  1},
-	{ 84, 58, 1,  1},
-	{ 85, 59, 1,  1},
-};
-
 /*
  *   iConfiguration          0
  *     bInterfaceNumber        0
@@ -377,13 +52,72 @@ static const struct msi3101_gain msi3101_gain_lut_1000[] = {
 #define MAX_ISOC_ERRORS         20
 
 /* TODO: These should be moved to V4L2 API */
-#define MSI3101_CID_SAMPLING_MODE         ((V4L2_CID_USER_BASE | 0xf000) + 0)
-#define MSI3101_CID_SAMPLING_RATE         ((V4L2_CID_USER_BASE | 0xf000) + 1)
-#define MSI3101_CID_SAMPLING_RESOLUTION   ((V4L2_CID_USER_BASE | 0xf000) + 2)
-#define MSI3101_CID_TUNER_RF              ((V4L2_CID_USER_BASE | 0xf000) + 10)
-#define MSI3101_CID_TUNER_BW              ((V4L2_CID_USER_BASE | 0xf000) + 11)
-#define MSI3101_CID_TUNER_IF              ((V4L2_CID_USER_BASE | 0xf000) + 12)
-#define MSI3101_CID_TUNER_GAIN            ((V4L2_CID_USER_BASE | 0xf000) + 13)
+#define V4L2_PIX_FMT_SDR_S8     v4l2_fourcc('D', 'S', '0', '8') /* signed 8-bit */
+#define V4L2_PIX_FMT_SDR_S12    v4l2_fourcc('D', 'S', '1', '2') /* signed 12-bit */
+#define V4L2_PIX_FMT_SDR_S14    v4l2_fourcc('D', 'S', '1', '4') /* signed 14-bit */
+#define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */
+
+static const struct v4l2_frequency_band bands_adc[] = {
+	{
+		.tuner = 0,
+		.type = V4L2_TUNER_ADC,
+		.index = 0,
+		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
+		.rangelow   =  1200000,
+		.rangehigh  = 15000000,
+	},
+};
+
+static const struct v4l2_frequency_band bands_rf[] = {
+	{
+		.tuner = 1,
+		.type = V4L2_TUNER_RF,
+		.index = 0,
+		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
+		.rangelow   =   49000000,
+		.rangehigh  =  263000000,
+	}, {
+		.tuner = 1,
+		.type = V4L2_TUNER_RF,
+		.index = 1,
+		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
+		.rangelow   =  390000000,
+		.rangehigh  =  960000000,
+	},
+};
+
+/* stream formats */
+struct msi3101_format {
+	char	*name;
+	u32	pixelformat;
+};
+
+/* format descriptions for capture and preview */
+static struct msi3101_format formats[] = {
+	{
+		.name		= "IQ U8",
+		.pixelformat	= V4L2_SDR_FMT_CU8,
+	}, {
+		.name		= "IQ U16LE",
+		.pixelformat	=  V4L2_SDR_FMT_CU16LE,
+#if 0
+	}, {
+		.name		= "8-bit signed",
+		.pixelformat	= V4L2_PIX_FMT_SDR_S8,
+	}, {
+		.name		= "10+2-bit signed",
+		.pixelformat	= V4L2_PIX_FMT_SDR_MSI2500_384,
+	}, {
+		.name		= "12-bit signed",
+		.pixelformat	= V4L2_PIX_FMT_SDR_S12,
+	}, {
+		.name		= "14-bit signed",
+		.pixelformat	= V4L2_PIX_FMT_SDR_S14,
+#endif
+	},
+};
+
+static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
 
 /* intermediate buffers with raw data from the USB device */
 struct msi3101_frame_buf {
@@ -407,24 +141,30 @@ struct msi3101_state {
 	/* Pointer to our usb_device, will be NULL after unplug */
 	struct usb_device *udev; /* Both mutexes most be hold when setting! */
 
+	unsigned int f_adc, f_tuner;
+	u32 pixelformat;
+
 	unsigned int isoc_errors; /* number of contiguous ISOC errors */
 	unsigned int vb_full; /* vb is full and packets dropped */
 
 	struct urb *urbs[MAX_ISO_BUFS];
-	int (*convert_stream) (struct msi3101_state *s, u32 *dst, u8 *src,
+	int (*convert_stream) (struct msi3101_state *s, u8 *dst, u8 *src,
 			unsigned int src_len);
 
 	/* Controls */
-	struct v4l2_ctrl_handler ctrl_handler;
-	struct v4l2_ctrl *ctrl_sampling_rate;
-	struct v4l2_ctrl *ctrl_tuner_rf;
-	struct v4l2_ctrl *ctrl_tuner_bw;
-	struct v4l2_ctrl *ctrl_tuner_if;
-	struct v4l2_ctrl *ctrl_tuner_gain;
+	struct v4l2_ctrl_handler hdl;
+	struct v4l2_ctrl *bandwidth_auto;
+	struct v4l2_ctrl *bandwidth;
+	struct v4l2_ctrl *lna_gain_auto;
+	struct v4l2_ctrl *lna_gain;
+	struct v4l2_ctrl *mixer_gain_auto;
+	struct v4l2_ctrl *mixer_gain;
+	struct v4l2_ctrl *if_gain_auto;
+	struct v4l2_ctrl *if_gain;
 
 	u32 next_sample; /* for track lost packets */
 	u32 sample; /* for sample rate calc */
-	unsigned long jiffies;
+	unsigned long jiffies_next;
 	unsigned int sample_ctrl_bit[4];
 };
 
@@ -448,98 +188,79 @@ leave:
 
 /*
  * +===========================================================================
- * |   00-1023 | USB packet type '384'
+ * |   00-1023 | USB packet type '504'
  * +===========================================================================
  * |   00-  03 | sequence number of first sample in that USB packet
  * +---------------------------------------------------------------------------
  * |   04-  15 | garbage
  * +---------------------------------------------------------------------------
- * |   16- 175 | samples
- * +---------------------------------------------------------------------------
- * |  176- 179 | control bits for previous samples
- * +---------------------------------------------------------------------------
- * |  180- 339 | samples
- * +---------------------------------------------------------------------------
- * |  340- 343 | control bits for previous samples
- * +---------------------------------------------------------------------------
- * |  344- 503 | samples
- * +---------------------------------------------------------------------------
- * |  504- 507 | control bits for previous samples
- * +---------------------------------------------------------------------------
- * |  508- 667 | samples
- * +---------------------------------------------------------------------------
- * |  668- 671 | control bits for previous samples
- * +---------------------------------------------------------------------------
- * |  672- 831 | samples
- * +---------------------------------------------------------------------------
- * |  832- 835 | control bits for previous samples
- * +---------------------------------------------------------------------------
- * |  836- 995 | samples
- * +---------------------------------------------------------------------------
- * |  996- 999 | control bits for previous samples
- * +---------------------------------------------------------------------------
- * | 1000-1023 | garbage
+ * |   16-1023 | samples
  * +---------------------------------------------------------------------------
- *
- * Bytes 4 - 7 could have some meaning?
- *
- * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
- * numbers. This results one 2-bit number for 8 samples. It is likely used for
- * for bit shifting sample by given bits, increasing actual sampling resolution.
- * Number 2 (0b10) was never seen.
- *
- * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
+ * signed 8-bit sample
+ * 504 * 2 = 1008 samples
  */
+static int msi3101_convert_stream_504(struct msi3101_state *s, u8 *dst,
+		u8 *src, unsigned int src_len)
+{
+	int i, i_max, dst_len = 0;
+	u32 sample_num[3];
 
-/*
- * Integer to 32-bit IEEE floating point representation routine is taken
- * from Radeon R600 driver (drivers/gpu/drm/radeon/r600_blit_kms.c).
- *
- * TODO: Currently we do conversion here in Kernel, but in future that will
- * be moved to the libv4l2 library as video format conversions are.
- */
-#define I2F_FRAC_BITS  23
-#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
+	/* There could be 1-3 1024 bytes URB frames */
+	i_max = src_len / 1024;
 
-/*
- * Converts signed 8-bit integer into 32-bit IEEE floating point
- * representation.
- */
-static u32 msi3101_convert_sample_504(struct msi3101_state *s, u16 x)
-{
-	u32 msb, exponent, fraction, sign;
+	for (i = 0; i < i_max; i++) {
+		sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
+		if (i == 0 && s->next_sample != sample_num[0]) {
+			dev_dbg_ratelimited(&s->udev->dev,
+					"%d samples lost, %d %08x:%08x\n",
+					sample_num[0] - s->next_sample,
+					src_len, s->next_sample, sample_num[0]);
+		}
 
-	/* Zero is special */
-	if (!x)
-		return 0;
+		/*
+		 * Dump all unknown 'garbage' data - maybe we will discover
+		 * someday if there is something rational...
+		 */
+		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
 
-	/* Negative / positive value */
-	if (x & (1 << 7)) {
-		x = -x;
-		x &= 0x7f; /* result is 7 bit ... + sign */
-		sign = 1 << 31;
-	} else {
-		sign = 0 << 31;
+		/* 504 x I+Q samples */
+		src += 16;
+		memcpy(dst, src, 1008);
+		src += 1008;
+		dst += 1008;
+		dst_len += 1008;
 	}
 
-	/* Get location of the most significant bit */
-	msb = __fls(x);
+	/* calculate samping rate and output it in 10 seconds intervals */
+	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
+		unsigned long jiffies_now = jiffies;
+		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
+		unsigned int samples = sample_num[i_max - 1] - s->sample;
+		s->jiffies_next = jiffies_now;
+		s->sample = sample_num[i_max - 1];
+		dev_dbg(&s->udev->dev,
+				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
+				src_len, samples, msecs,
+				samples * 1000UL / msecs);
+	}
 
-	fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
-	exponent = (127 + msb) << I2F_FRAC_BITS;
+	/* next sample (sample = sample + i * 504) */
+	s->next_sample = sample_num[i_max - 1] + 504;
 
-	return (fraction + exponent) | sign;
+	return dst_len;
 }
 
-static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst,
+static int msi3101_convert_stream_504_u8(struct msi3101_state *s, u8 *dst,
 		u8 *src, unsigned int src_len)
 {
 	int i, j, i_max, dst_len = 0;
-	u16 sample[2];
 	u32 sample_num[3];
+	s8 *s8src;
+	u8 *u8dst;
 
 	/* There could be 1-3 1024 bytes URB frames */
 	i_max = src_len / 1024;
+	u8dst = (u8 *) dst;
 
 	for (i = 0; i < i_max; i++) {
 		sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
@@ -556,30 +277,28 @@ static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst,
 		 */
 		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
 
+		/* 504 x I+Q samples */
 		src += 16;
-		for (j = 0; j < 1008; j += 2) {
-			sample[0] = src[j + 0];
-			sample[1] = src[j + 1];
 
-			*dst++ = msi3101_convert_sample_504(s, sample[0]);
-			*dst++ = msi3101_convert_sample_504(s, sample[1]);
-		}
-		/* 504 x I+Q 32bit float samples */
-		dst_len += 504 * 2 * 4;
+		s8src = (s8 *) src;
+		for (j = 0; j < 1008; j++)
+			*u8dst++ = *s8src++ + 128;
+
 		src += 1008;
+		dst += 1008;
+		dst_len += 1008;
 	}
 
 	/* calculate samping rate and output it in 10 seconds intervals */
-	if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
-		unsigned long jiffies_now = jiffies;
-		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
+	if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
+#define MSECS 10000UL
 		unsigned int samples = sample_num[i_max - 1] - s->sample;
-		s->jiffies = jiffies_now;
+		s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
 		s->sample = sample_num[i_max - 1];
 		dev_dbg(&s->udev->dev,
 				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
-				src_len, samples, msecs,
-				samples * 1000UL / msecs);
+				src_len, samples, MSECS,
+				samples * 1000UL / MSECS);
 	}
 
 	/* next sample (sample = sample + i * 504) */
@@ -589,48 +308,53 @@ static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst,
 }
 
 /*
- * Converts signed ~10+2-bit integer into 32-bit IEEE floating point
- * representation.
+ * +===========================================================================
+ * |   00-1023 | USB packet type '384'
+ * +===========================================================================
+ * |   00-  03 | sequence number of first sample in that USB packet
+ * +---------------------------------------------------------------------------
+ * |   04-  15 | garbage
+ * +---------------------------------------------------------------------------
+ * |   16- 175 | samples
+ * +---------------------------------------------------------------------------
+ * |  176- 179 | control bits for previous samples
+ * +---------------------------------------------------------------------------
+ * |  180- 339 | samples
+ * +---------------------------------------------------------------------------
+ * |  340- 343 | control bits for previous samples
+ * +---------------------------------------------------------------------------
+ * |  344- 503 | samples
+ * +---------------------------------------------------------------------------
+ * |  504- 507 | control bits for previous samples
+ * +---------------------------------------------------------------------------
+ * |  508- 667 | samples
+ * +---------------------------------------------------------------------------
+ * |  668- 671 | control bits for previous samples
+ * +---------------------------------------------------------------------------
+ * |  672- 831 | samples
+ * +---------------------------------------------------------------------------
+ * |  832- 835 | control bits for previous samples
+ * +---------------------------------------------------------------------------
+ * |  836- 995 | samples
+ * +---------------------------------------------------------------------------
+ * |  996- 999 | control bits for previous samples
+ * +---------------------------------------------------------------------------
+ * | 1000-1023 | garbage
+ * +---------------------------------------------------------------------------
+ *
+ * Bytes 4 - 7 could have some meaning?
+ *
+ * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
+ * numbers. This results one 2-bit number for 8 samples. It is likely used for
+ * for bit shifting sample by given bits, increasing actual sampling resolution.
+ * Number 2 (0b10) was never seen.
+ *
+ * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
  */
-static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift)
-{
-	u32 msb, exponent, fraction, sign;
-	s->sample_ctrl_bit[shift]++;
-
-	/* Zero is special */
-	if (!x)
-		return 0;
-
-	if (shift == 3)
-		shift =	2;
-
-	/* Convert 10-bit two's complement to 12-bit */
-	if (x & (1 << 9)) {
-		x |= ~0U << 10; /* set all the rest bits to one */
-		x <<= shift;
-		x = -x;
-		x &= 0x7ff; /* result is 11 bit ... + sign */
-		sign = 1 << 31;
-	} else {
-		x <<= shift;
-		sign = 0 << 31;
-	}
-
-	/* Get location of the most significant bit */
-	msb = __fls(x);
-
-	fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
-	exponent = (127 + msb) << I2F_FRAC_BITS;
-
-	return (fraction + exponent) | sign;
-}
-
-static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
+static int msi3101_convert_stream_384(struct msi3101_state *s, u8 *dst,
 		u8 *src, unsigned int src_len)
 {
-	int i, j, k, l, i_max, dst_len = 0;
-	u16 sample[4];
-	u32 bits;
+	int i, i_max, dst_len = 0;
 	u32 sample_num[3];
 
 	/* There could be 1-3 1024 bytes URB frames */
@@ -651,38 +375,20 @@ static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
 		dev_dbg_ratelimited(&s->udev->dev,
 				"%*ph  %*ph\n", 12, &src[4], 24, &src[1000]);
 
+		/* 384 x I+Q samples */
 		src += 16;
-		for (j = 0; j < 6; j++) {
-			bits = src[160 + 3] << 24 | src[160 + 2] << 16 | src[160 + 1] << 8 | src[160 + 0] << 0;
-			for (k = 0; k < 16; k++) {
-				for (l = 0; l < 10; l += 5) {
-					sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8;
-					sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6;
-					sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4;
-					sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2;
-
-					*dst++ = msi3101_convert_sample_384(s, sample[0], (bits >> (2 * k)) & 0x3);
-					*dst++ = msi3101_convert_sample_384(s, sample[1], (bits >> (2 * k)) & 0x3);
-					*dst++ = msi3101_convert_sample_384(s, sample[2], (bits >> (2 * k)) & 0x3);
-					*dst++ = msi3101_convert_sample_384(s, sample[3], (bits >> (2 * k)) & 0x3);
-				}
-				src += 10;
-			}
-			dev_dbg_ratelimited(&s->udev->dev,
-					"sample control bits %08x\n", bits);
-			src += 4;
-		}
-		/* 384 x I+Q 32bit float samples */
-		dst_len += 384 * 2 * 4;
-		src += 24;
+		memcpy(dst, src, 984);
+		src += 984 + 24;
+		dst += 984;
+		dst_len += 984;
 	}
 
 	/* calculate samping rate and output it in 10 seconds intervals */
-	if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
+	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
 		unsigned long jiffies_now = jiffies;
-		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
+		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
 		unsigned int samples = sample_num[i_max - 1] - s->sample;
-		s->jiffies = jiffies_now;
+		s->jiffies_next = jiffies_now;
 		s->sample = sample_num[i_max - 1];
 		dev_dbg(&s->udev->dev,
 				"slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n",
@@ -699,40 +405,21 @@ static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
 }
 
 /*
- * Converts signed 12-bit integer into 32-bit IEEE floating point
- * representation.
+ * +===========================================================================
+ * |   00-1023 | USB packet type '336'
+ * +===========================================================================
+ * |   00-  03 | sequence number of first sample in that USB packet
+ * +---------------------------------------------------------------------------
+ * |   04-  15 | garbage
+ * +---------------------------------------------------------------------------
+ * |   16-1023 | samples
+ * +---------------------------------------------------------------------------
+ * signed 12-bit sample
  */
-static u32 msi3101_convert_sample_336(struct msi3101_state *s, u16 x)
-{
-	u32 msb, exponent, fraction, sign;
-
-	/* Zero is special */
-	if (!x)
-		return 0;
-
-	/* Negative / positive value */
-	if (x & (1 << 11)) {
-		x = -x;
-		x &= 0x7ff; /* result is 11 bit ... + sign */
-		sign = 1 << 31;
-	} else {
-		sign = 0 << 31;
-	}
-
-	/* Get location of the most significant bit */
-	msb = __fls(x);
-
-	fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
-	exponent = (127 + msb) << I2F_FRAC_BITS;
-
-	return (fraction + exponent) | sign;
-}
-
-static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst,
+static int msi3101_convert_stream_336(struct msi3101_state *s, u8 *dst,
 		u8 *src, unsigned int src_len)
 {
-	int i, j, i_max, dst_len = 0;
-	u16 sample[2];
+	int i, i_max, dst_len = 0;
 	u32 sample_num[3];
 
 	/* There could be 1-3 1024 bytes URB frames */
@@ -753,25 +440,20 @@ static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst,
 		 */
 		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
 
+		/* 336 x I+Q samples */
 		src += 16;
-		for (j = 0; j < 1008; j += 3) {
-			sample[0] = (src[j + 0] & 0xff) >> 0 | (src[j + 1] & 0x0f) << 8;
-			sample[1] = (src[j + 1] & 0xf0) >> 4 | (src[j + 2] & 0xff) << 4;
-
-			*dst++ = msi3101_convert_sample_336(s, sample[0]);
-			*dst++ = msi3101_convert_sample_336(s, sample[1]);
-		}
-		/* 336 x I+Q 32bit float samples */
-		dst_len += 336 * 2 * 4;
+		memcpy(dst, src, 1008);
 		src += 1008;
+		dst += 1008;
+		dst_len += 1008;
 	}
 
 	/* calculate samping rate and output it in 10 seconds intervals */
-	if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
+	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
 		unsigned long jiffies_now = jiffies;
-		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
+		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
 		unsigned int samples = sample_num[i_max - 1] - s->sample;
-		s->jiffies = jiffies_now;
+		s->jiffies_next = jiffies_now;
 		s->sample = sample_num[i_max - 1];
 		dev_dbg(&s->udev->dev,
 				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
@@ -786,41 +468,75 @@ static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst,
 }
 
 /*
- * Converts signed 14-bit integer into 32-bit IEEE floating point
- * representation.
+ * +===========================================================================
+ * |   00-1023 | USB packet type '252'
+ * +===========================================================================
+ * |   00-  03 | sequence number of first sample in that USB packet
+ * +---------------------------------------------------------------------------
+ * |   04-  15 | garbage
+ * +---------------------------------------------------------------------------
+ * |   16-1023 | samples
+ * +---------------------------------------------------------------------------
+ * signed 14-bit sample
  */
-static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x)
+static int msi3101_convert_stream_252(struct msi3101_state *s, u8 *dst,
+		u8 *src, unsigned int src_len)
 {
-	u32 msb, exponent, fraction, sign;
+	int i, i_max, dst_len = 0;
+	u32 sample_num[3];
 
-	/* Zero is special */
-	if (!x)
-		return 0;
+	/* There could be 1-3 1024 bytes URB frames */
+	i_max = src_len / 1024;
 
-	/* Negative / positive value */
-	if (x & (1 << 13)) {
-		x = -x;
-		x &= 0x1fff; /* result is 13 bit ... + sign */
-		sign = 1 << 31;
-	} else {
-		sign = 0 << 31;
+	for (i = 0; i < i_max; i++) {
+		sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
+		if (i == 0 && s->next_sample != sample_num[0]) {
+			dev_dbg_ratelimited(&s->udev->dev,
+					"%d samples lost, %d %08x:%08x\n",
+					sample_num[0] - s->next_sample,
+					src_len, s->next_sample, sample_num[0]);
+		}
+
+		/*
+		 * Dump all unknown 'garbage' data - maybe we will discover
+		 * someday if there is something rational...
+		 */
+		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
+
+		/* 252 x I+Q samples */
+		src += 16;
+		memcpy(dst, src, 1008);
+		src += 1008;
+		dst += 1008;
+		dst_len += 1008;
 	}
 
-	/* Get location of the most significant bit */
-	msb = __fls(x);
+	/* calculate samping rate and output it in 10 seconds intervals */
+	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
+		unsigned long jiffies_now = jiffies;
+		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
+		unsigned int samples = sample_num[i_max - 1] - s->sample;
+		s->jiffies_next = jiffies_now;
+		s->sample = sample_num[i_max - 1];
+		dev_dbg(&s->udev->dev,
+				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
+				src_len, samples, msecs,
+				samples * 1000UL / msecs);
+	}
 
-	fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
-	exponent = (127 + msb) << I2F_FRAC_BITS;
+	/* next sample (sample = sample + i * 252) */
+	s->next_sample = sample_num[i_max - 1] + 252;
 
-	return (fraction + exponent) | sign;
+	return dst_len;
 }
 
-static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst,
+static int msi3101_convert_stream_252_u16(struct msi3101_state *s, u8 *dst,
 		u8 *src, unsigned int src_len)
 {
 	int i, j, i_max, dst_len = 0;
-	u16 sample[2];
 	u32 sample_num[3];
+	u16 *u16dst = (u16 *) dst;
+	struct {signed int x:14;} se;
 
 	/* There could be 1-3 1024 bytes URB frames */
 	i_max = src_len / 1024;
@@ -840,30 +556,44 @@ static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst,
 		 */
 		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
 
+		/* 252 x I+Q samples */
 		src += 16;
+
 		for (j = 0; j < 1008; j += 4) {
-			sample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
-			sample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
+			unsigned int usample[2];
+			int ssample[2];
 
-			*dst++ = msi3101_convert_sample_252(s, sample[0]);
-			*dst++ = msi3101_convert_sample_252(s, sample[1]);
+			usample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
+			usample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
+
+			/* sign extension from 14-bit to signed int */
+			ssample[0] = se.x = usample[0];
+			ssample[1] = se.x = usample[1];
+
+			/* from signed to unsigned */
+			usample[0] = ssample[0] + 8192;
+			usample[1] = ssample[1] + 8192;
+
+			/* from 14-bit to 16-bit */
+			*u16dst++ = (usample[0] << 2) | (usample[0] >> 12);
+			*u16dst++ = (usample[1] << 2) | (usample[1] >> 12);
 		}
-		/* 252 x I+Q 32bit float samples */
-		dst_len += 252 * 2 * 4;
+
 		src += 1008;
+		dst += 1008;
+		dst_len += 1008;
 	}
 
 	/* calculate samping rate and output it in 10 seconds intervals */
-	if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
-		unsigned long jiffies_now = jiffies;
-		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
+	if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
+#define MSECS 10000UL
 		unsigned int samples = sample_num[i_max - 1] - s->sample;
-		s->jiffies = jiffies_now;
+		s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
 		s->sample = sample_num[i_max - 1];
 		dev_dbg(&s->udev->dev,
 				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
-				src_len, samples, msecs,
-				samples * 1000UL / msecs);
+				src_len, samples, MSECS,
+				samples * 1000UL / MSECS);
 	}
 
 	/* next sample (sample = sample + i * 252) */
@@ -883,14 +613,14 @@ static void msi3101_isoc_handler(struct urb *urb)
 	unsigned char *iso_buf = NULL;
 	struct msi3101_frame_buf *fbuf;
 
-	if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
-			urb->status == -ESHUTDOWN) {
+	if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
+			urb->status == -ESHUTDOWN)) {
 		dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
 				urb, urb->status == -ENOENT ? "" : "a");
 		return;
 	}
 
-	if (urb->status != 0) {
+	if (unlikely(urb->status != 0)) {
 		dev_dbg(&s->udev->dev,
 				"msi3101_isoc_handler() called with status %d\n",
 				urb->status);
@@ -910,28 +640,28 @@ static void msi3101_isoc_handler(struct urb *urb)
 
 		/* Check frame error */
 		fstatus = urb->iso_frame_desc[i].status;
-		if (fstatus) {
+		if (unlikely(fstatus)) {
 			dev_dbg_ratelimited(&s->udev->dev,
 					"frame=%d/%d has error %d skipping\n",
 					i, urb->number_of_packets, fstatus);
-			goto skip;
+			continue;
 		}
 
 		/* Check if that frame contains data */
 		flen = urb->iso_frame_desc[i].actual_length;
-		if (flen == 0)
-			goto skip;
+		if (unlikely(flen == 0))
+			continue;
 
 		iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
 
 		/* Get free framebuffer */
 		fbuf = msi3101_get_next_fill_buf(s);
-		if (fbuf == NULL) {
+		if (unlikely(fbuf == NULL)) {
 			s->vb_full++;
 			dev_dbg_ratelimited(&s->udev->dev,
 					"videobuf is full, %d packets dropped\n",
 					s->vb_full);
-			goto skip;
+			continue;
 		}
 
 		/* fill framebuffer */
@@ -939,13 +669,11 @@ static void msi3101_isoc_handler(struct urb *urb)
 		flen = s->convert_stream(s, ptr, iso_buf, flen);
 		vb2_set_plane_payload(&fbuf->vb, 0, flen);
 		vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
-skip:
-		;
 	}
 
 handler_end:
 	i = usb_submit_urb(urb, GFP_ATOMIC);
-	if (i != 0)
+	if (unlikely(i != 0))
 		dev_dbg(&s->udev->dev,
 				"Error (%d) re-submitting urb in msi3101_isoc_handler\n",
 				i);
@@ -1008,7 +736,7 @@ static int msi3101_isoc_init(struct msi3101_state *s)
 	udev = s->udev;
 
 	ret = usb_set_interface(s->udev, 0, 1);
-	if (ret < 0)
+	if (ret)
 		return ret;
 
 	/* Allocate and init Isochronuous urbs */
@@ -1112,14 +840,12 @@ static int msi3101_querycap(struct file *file, void *fh,
 	strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
 	strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
 	usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
-	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
-			V4L2_CAP_READWRITE;
-	cap->device_caps = V4L2_CAP_TUNER;
+	cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
+			V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 	return 0;
 }
 
-
 /* Videobuf2 operations */
 static int msi3101_queue_setup(struct vb2_queue *vq,
 		const struct v4l2_format *fmt, unsigned int *nbuffers,
@@ -1135,25 +861,14 @@ static int msi3101_queue_setup(struct vb2_queue *vq,
 	 *   3, wMaxPacketSize 3x 1024 bytes
 	 * 504, max IQ sample pairs per 1024 frame
 	 *   2, two samples, I and Q
-	 *   4, 32-bit float
+	 *   2, 16-bit is enough for single sample
 	 */
-	sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 4); /* = 12096 */
+	sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 2);
 	dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
 			__func__, *nbuffers, sizes[0]);
 	return 0;
 }
 
-static int msi3101_buf_prepare(struct vb2_buffer *vb)
-{
-	struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
-
-	/* Don't allow queing new buffers after device disconnection */
-	if (!s->udev)
-		return -ENODEV;
-
-	return 0;
-}
-
 static void msi3101_buf_queue(struct vb2_buffer *vb)
 {
 	struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
@@ -1162,7 +877,7 @@ static void msi3101_buf_queue(struct vb2_buffer *vb)
 	unsigned long flags = 0;
 
 	/* Check the device has not disconnected between prep and queuing */
-	if (!s->udev) {
+	if (unlikely(!s->udev)) {
 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
 		return;
 	}
@@ -1221,29 +936,46 @@ static int msi3101_set_usb_adc(struct msi3101_state *s)
 	int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
 	u32 reg3, reg4, reg7;
 
-	f_sr = s->ctrl_sampling_rate->val64;
+	f_sr = s->f_adc;
 
 	/* select stream format */
-	if (f_sr < 6000000) {
-		s->convert_stream = msi3101_convert_stream_252;
+	switch (s->pixelformat) {
+	case V4L2_SDR_FMT_CU8:
+		s->convert_stream = msi3101_convert_stream_504_u8;
+		reg7 = 0x000c9407;
+		break;
+	case  V4L2_SDR_FMT_CU16LE:
+		s->convert_stream = msi3101_convert_stream_252_u16;
 		reg7 = 0x00009407;
-	} else if (f_sr < 8000000) {
-		s->convert_stream = msi3101_convert_stream_336;
-		reg7 = 0x00008507;
-	} else if (f_sr < 9000000) {
+		break;
+	case V4L2_PIX_FMT_SDR_S8:
+		s->convert_stream = msi3101_convert_stream_504;
+		reg7 = 0x000c9407;
+		break;
+	case V4L2_PIX_FMT_SDR_MSI2500_384:
 		s->convert_stream = msi3101_convert_stream_384;
 		reg7 = 0x0000a507;
-	} else {
-		s->convert_stream = msi3101_convert_stream_504;
+		break;
+	case V4L2_PIX_FMT_SDR_S12:
+		s->convert_stream = msi3101_convert_stream_336;
+		reg7 = 0x00008507;
+		break;
+	case V4L2_PIX_FMT_SDR_S14:
+		s->convert_stream = msi3101_convert_stream_252;
+		reg7 = 0x00009407;
+		break;
+	default:
+		s->convert_stream = msi3101_convert_stream_504_u8;
 		reg7 = 0x000c9407;
+		break;
 	}
 
 	/*
 	 * Synthesizer config is just a educated guess...
 	 *
 	 * [7:0]   0x03, register address
-	 * [8]     1, always
-	 * [9]     ?
+	 * [8]     1, power control
+	 * [9]     ?, power control
 	 * [12:10] output divider
 	 * [13]    0 ?
 	 * [14]    0 ?
@@ -1334,14 +1066,37 @@ err:
 	return ret;
 };
 
+static int msi3101_set_gain(struct msi3101_state *s)
+{
+	int ret;
+	u32 reg;
+	dev_dbg(&s->udev->dev, "%s: lna=%d mixer=%d if=%d\n", __func__,
+			s->lna_gain->val, s->mixer_gain->val, s->if_gain->val);
+
+	reg = 1 << 0;
+	reg |= (59 - s->if_gain->val) << 4;
+	reg |= 0 << 10;
+	reg |= (1 - s->mixer_gain->val) << 12;
+	reg |= (1 - s->lna_gain->val) << 13;
+	reg |= 4 << 14;
+	reg |= 0 << 17;
+	ret = msi3101_tuner_write(s, reg);
+	if (ret)
+		goto err;
+
+	return 0;
+err:
+	dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
+	return ret;
+};
+
 static int msi3101_set_tuner(struct msi3101_state *s)
 {
-	int ret, i, len;
+	int ret, i;
 	unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;
 	u32 reg;
 	u64 f_vco, tmp64;
 	u8 mode, filter_mode, lo_div;
-	const struct msi3101_gain *gain_lut;
 	static const struct {
 		u32 rf;
 		u8 mode;
@@ -1376,30 +1131,23 @@ static int msi3101_set_tuner(struct msi3101_state *s)
 		{8000000, 0x07}, /* 8 MHz */
 	};
 
-	unsigned int f_rf = s->ctrl_tuner_rf->val64;
+	unsigned int f_rf = s->f_tuner;
 
 	/*
 	 * bandwidth (Hz)
 	 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
 	 */
-	unsigned int bandwidth = s->ctrl_tuner_bw->val;
+	unsigned int bandwidth;
 
 	/*
 	 * intermediate frequency (Hz)
 	 * 0, 450000, 1620000, 2048000
 	 */
-	unsigned int f_if = s->ctrl_tuner_if->val;
-
-	/*
-	 * gain reduction (dB)
-	 * 0 - 102 below 420 MHz
-	 * 0 - 85 above 420 MHz
-	 */
-	int gain = s->ctrl_tuner_gain->val;
+	unsigned int f_if = 0;
 
 	dev_dbg(&s->udev->dev,
-			"%s: f_rf=%d bandwidth=%d f_if=%d gain=%d\n",
-			__func__, f_rf, bandwidth, f_if, gain);
+			"%s: f_rf=%d f_if=%d\n",
+			__func__, f_rf, f_if);
 
 	ret = -EINVAL;
 
@@ -1430,8 +1178,16 @@ static int msi3101_set_tuner(struct msi3101_state *s)
 	if (i == ARRAY_SIZE(if_freq_lut))
 		goto err;
 
+	/* filters */
+	if (s->bandwidth_auto->val)
+		bandwidth = s->f_adc;
+	else
+		bandwidth = s->bandwidth->val;
+
+	bandwidth = clamp(bandwidth, 200000U, 8000000U);
+
 	for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
-		if (bandwidth == bandwidth_lut[i].freq) {
+		if (bandwidth <= bandwidth_lut[i].freq) {
 			bandwidth = bandwidth_lut[i].val;
 			break;
 		}
@@ -1440,6 +1196,11 @@ static int msi3101_set_tuner(struct msi3101_state *s)
 	if (i == ARRAY_SIZE(bandwidth_lut))
 		goto err;
 
+	s->bandwidth->val = bandwidth_lut[i].freq;
+
+	dev_dbg(&s->udev->dev, "%s: bandwidth selected=%d\n",
+			__func__, bandwidth_lut[i].freq);
+
 #define F_OUT_STEP 1
 #define R_REF 4
 	f_vco = (f_rf + f_if + f_if1) * lo_div;
@@ -1504,38 +1265,7 @@ static int msi3101_set_tuner(struct msi3101_state *s)
 	if (ret)
 		goto err;
 
-	if (f_rf < 120000000) {
-		gain_lut = msi3101_gain_lut_120;
-		len = ARRAY_SIZE(msi3101_gain_lut_120);
-	} else if (f_rf < 245000000) {
-		gain_lut = msi3101_gain_lut_245;
-		len = ARRAY_SIZE(msi3101_gain_lut_120);
-	} else {
-		gain_lut = msi3101_gain_lut_1000;
-		len = ARRAY_SIZE(msi3101_gain_lut_1000);
-	}
-
-	for (i = 0; i < len; i++) {
-		if (gain_lut[i].tot >= gain)
-			break;
-	}
-
-	if (i == len)
-		goto err;
-
-	dev_dbg(&s->udev->dev,
-			"%s: gain tot=%d baseband=%d lna=%d mixer=%d\n",
-			__func__, gain_lut[i].tot, gain_lut[i].baseband,
-			gain_lut[i].lna, gain_lut[i].mixer);
-
-	reg = 1 << 0;
-	reg |= gain_lut[i].baseband << 4;
-	reg |= 0 << 10;
-	reg |= gain_lut[i].mixer << 12;
-	reg |= gain_lut[i].lna << 13;
-	reg |= 4 << 14;
-	reg |= 0 << 17;
-	ret = msi3101_tuner_write(s, reg);
+	ret = msi3101_set_gain(s);
 	if (ret)
 		goto err;
 
@@ -1594,6 +1324,12 @@ static int msi3101_stop_streaming(struct vb2_queue *vq)
 	msleep(20);
 	msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0);
 
+	/* sleep USB IF / ADC */
+	msi3101_ctrl_msg(s, CMD_WREG, 0x01000003);
+
+	/* sleep tuner */
+	msi3101_tuner_write(s, 0x000000);
+
 	mutex_unlock(&s->v4l2_lock);
 
 	return 0;
@@ -1601,7 +1337,6 @@ static int msi3101_stop_streaming(struct vb2_queue *vq)
 
 static struct vb2_ops msi3101_vb2_ops = {
 	.queue_setup            = msi3101_queue_setup,
-	.buf_prepare            = msi3101_buf_prepare,
 	.buf_queue              = msi3101_buf_queue,
 	.start_streaming        = msi3101_start_streaming,
 	.stop_streaming         = msi3101_stop_streaming,
@@ -1609,30 +1344,77 @@ static struct vb2_ops msi3101_vb2_ops = {
 	.wait_finish            = vb2_ops_wait_finish,
 };
 
-static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i)
+static int msi3101_enum_fmt_sdr_cap(struct file *file, void *priv,
+		struct v4l2_fmtdesc *f)
 {
-	if (i->index != 0)
+	struct msi3101_state *s = video_drvdata(file);
+	dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, f->index);
+
+	if (f->index >= NUM_FORMATS)
 		return -EINVAL;
 
-	strlcpy(i->name, "SDR data", sizeof(i->name));
-	i->type = V4L2_INPUT_TYPE_CAMERA;
+	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
+	f->pixelformat = formats[f->index].pixelformat;
+
+	return 0;
+}
+
+static int msi3101_g_fmt_sdr_cap(struct file *file, void *priv,
+		struct v4l2_format *f)
+{
+	struct msi3101_state *s = video_drvdata(file);
+	dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
+			(char *)&s->pixelformat);
+
+	f->fmt.sdr.pixelformat = s->pixelformat;
 
 	return 0;
 }
 
-static int msi3101_g_input(struct file *file, void *fh, unsigned int *i)
+static int msi3101_s_fmt_sdr_cap(struct file *file, void *priv,
+		struct v4l2_format *f)
 {
-	*i = 0;
+	struct msi3101_state *s = video_drvdata(file);
+	struct vb2_queue *q = &s->vb_queue;
+	int i;
+	dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
+			(char *)&f->fmt.sdr.pixelformat);
+
+	if (vb2_is_busy(q))
+		return -EBUSY;
+
+	for (i = 0; i < NUM_FORMATS; i++) {
+		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
+			s->pixelformat = f->fmt.sdr.pixelformat;
+			return 0;
+		}
+	}
+
+	f->fmt.sdr.pixelformat = formats[0].pixelformat;
+	s->pixelformat = formats[0].pixelformat;
 
 	return 0;
 }
 
-static int msi3101_s_input(struct file *file, void *fh, unsigned int i)
+static int msi3101_try_fmt_sdr_cap(struct file *file, void *priv,
+		struct v4l2_format *f)
 {
-	return i ? -EINVAL : 0;
+	struct msi3101_state *s = video_drvdata(file);
+	int i;
+	dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
+			(char *)&f->fmt.sdr.pixelformat);
+
+	for (i = 0; i < NUM_FORMATS; i++) {
+		if (formats[i].pixelformat == f->fmt.sdr.pixelformat)
+			return 0;
+	}
+
+	f->fmt.sdr.pixelformat = formats[0].pixelformat;
+
+	return 0;
 }
 
-static int vidioc_s_tuner(struct file *file, void *priv,
+static int msi3101_s_tuner(struct file *file, void *priv,
 		const struct v4l2_tuner *v)
 {
 	struct msi3101_state *s = video_drvdata(file);
@@ -1641,34 +1423,113 @@ static int vidioc_s_tuner(struct file *file, void *priv,
 	return 0;
 }
 
-static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
+static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
 {
 	struct msi3101_state *s = video_drvdata(file);
 	dev_dbg(&s->udev->dev, "%s:\n", __func__);
 
-	strcpy(v->name, "SDR RX");
-	v->capability = V4L2_TUNER_CAP_LOW;
+	if (v->index == 0) {
+		strlcpy(v->name, "ADC: Mirics MSi2500", sizeof(v->name));
+		v->type = V4L2_TUNER_ADC;
+		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
+		v->rangelow =   1200000;
+		v->rangehigh = 15000000;
+	} else if (v->index == 1) {
+		strlcpy(v->name, "RF: Mirics MSi001", sizeof(v->name));
+		v->type = V4L2_TUNER_RF;
+		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
+		v->rangelow =    49000000;
+		v->rangehigh =  960000000;
+	} else {
+		return -EINVAL;
+	}
 
 	return 0;
 }
 
-static int vidioc_s_frequency(struct file *file, void *priv,
+static int msi3101_g_frequency(struct file *file, void *priv,
+		struct v4l2_frequency *f)
+{
+	struct msi3101_state *s = video_drvdata(file);
+	int ret  = 0;
+	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n",
+			__func__, f->tuner, f->type);
+
+	if (f->tuner == 0)
+		f->frequency = s->f_adc;
+	else if (f->tuner == 1)
+		f->frequency = s->f_tuner;
+	else
+		return -EINVAL;
+
+	return ret;
+}
+
+static int msi3101_s_frequency(struct file *file, void *priv,
 		const struct v4l2_frequency *f)
 {
 	struct msi3101_state *s = video_drvdata(file);
-	dev_dbg(&s->udev->dev, "%s: frequency=%lu Hz (%u)\n",
-			__func__, f->frequency * 625UL / 10UL, f->frequency);
+	int ret, band;
+	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n",
+			__func__, f->tuner, f->type, f->frequency);
+
+	if (f->tuner == 0) {
+		s->f_adc = clamp_t(unsigned int, f->frequency,
+				bands_adc[0].rangelow,
+				bands_adc[0].rangehigh);
+		dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n",
+				__func__, s->f_adc);
+		ret = msi3101_set_usb_adc(s);
+	} else if (f->tuner == 1) {
+		#define BAND_RF_0 ((bands_rf[0].rangehigh + bands_rf[1].rangelow) / 2)
+		if (f->frequency < BAND_RF_0)
+			band = 0;
+		else
+			band = 1;
+		s->f_tuner = clamp_t(unsigned int, f->frequency,
+				bands_rf[band].rangelow,
+				bands_rf[band].rangehigh);
+		dev_dbg(&s->udev->dev, "%s: RF frequency=%u Hz\n",
+				__func__, f->frequency);
+		ret = msi3101_set_tuner(s);
+	} else {
+		return -EINVAL;
+	}
 
-	return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf,
-			f->frequency * 625UL / 10UL);
+	return ret;
+}
+
+static int msi3101_enum_freq_bands(struct file *file, void *priv,
+		struct v4l2_frequency_band *band)
+{
+	struct msi3101_state *s = video_drvdata(file);
+	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n",
+			__func__, band->tuner, band->type, band->index);
+
+	if (band->tuner == 0) {
+		if (band->index >= ARRAY_SIZE(bands_adc))
+			return -EINVAL;
+
+		*band = bands_adc[band->index];
+	} else if (band->tuner == 1) {
+		if (band->index >= ARRAY_SIZE(bands_rf))
+			return -EINVAL;
+
+		*band = bands_rf[band->index];
+	} else {
+		return -EINVAL;
+	}
+
+	return 0;
 }
 
 static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
 	.vidioc_querycap          = msi3101_querycap,
 
-	.vidioc_enum_input        = msi3101_enum_input,
-	.vidioc_g_input           = msi3101_g_input,
-	.vidioc_s_input           = msi3101_s_input,
+	.vidioc_enum_fmt_sdr_cap  = msi3101_enum_fmt_sdr_cap,
+	.vidioc_g_fmt_sdr_cap     = msi3101_g_fmt_sdr_cap,
+	.vidioc_s_fmt_sdr_cap     = msi3101_s_fmt_sdr_cap,
+	.vidioc_try_fmt_sdr_cap   = msi3101_try_fmt_sdr_cap,
 
 	.vidioc_reqbufs           = vb2_ioctl_reqbufs,
 	.vidioc_create_bufs       = vb2_ioctl_create_bufs,
@@ -1680,9 +1541,12 @@ static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
 	.vidioc_streamon          = vb2_ioctl_streamon,
 	.vidioc_streamoff         = vb2_ioctl_streamoff,
 
-	.vidioc_g_tuner           = vidioc_g_tuner,
-	.vidioc_s_tuner           = vidioc_s_tuner,
-	.vidioc_s_frequency       = vidioc_s_frequency,
+	.vidioc_g_tuner           = msi3101_g_tuner,
+	.vidioc_s_tuner           = msi3101_s_tuner,
+
+	.vidioc_g_frequency       = msi3101_g_frequency,
+	.vidioc_s_frequency       = msi3101_s_frequency,
+	.vidioc_enum_freq_bands   = msi3101_enum_freq_bands,
 
 	.vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
@@ -1710,7 +1574,7 @@ static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
 {
 	struct msi3101_state *s =
 			container_of(ctrl->handler, struct msi3101_state,
-					ctrl_handler);
+					hdl);
 	int ret;
 	dev_dbg(&s->udev->dev,
 			"%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
@@ -1718,18 +1582,17 @@ static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
 			ctrl->minimum, ctrl->maximum, ctrl->step);
 
 	switch (ctrl->id) {
-	case MSI3101_CID_SAMPLING_MODE:
-	case MSI3101_CID_SAMPLING_RATE:
-	case MSI3101_CID_SAMPLING_RESOLUTION:
-		ret = 0;
-		break;
-	case MSI3101_CID_TUNER_RF:
-	case MSI3101_CID_TUNER_BW:
-	case MSI3101_CID_TUNER_IF:
-	case MSI3101_CID_TUNER_GAIN:
+	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
+	case V4L2_CID_RF_TUNER_BANDWIDTH:
 		ret = msi3101_set_tuner(s);
 		break;
+	case  V4L2_CID_RF_TUNER_LNA_GAIN:
+	case  V4L2_CID_RF_TUNER_MIXER_GAIN:
+	case  V4L2_CID_RF_TUNER_IF_GAIN:
+		ret = msi3101_set_gain(s);
+		break;
 	default:
+		dev_dbg(&s->udev->dev, "%s: EINVAL\n", __func__);
 		ret = -EINVAL;
 	}
 
@@ -1745,7 +1608,7 @@ static void msi3101_video_release(struct v4l2_device *v)
 	struct msi3101_state *s =
 			container_of(v, struct msi3101_state, v4l2_dev);
 
-	v4l2_ctrl_handler_free(&s->ctrl_handler);
+	v4l2_ctrl_handler_free(&s->hdl);
 	v4l2_device_unregister(&s->v4l2_dev);
 	kfree(s);
 }
@@ -1755,81 +1618,8 @@ static int msi3101_probe(struct usb_interface *intf,
 {
 	struct usb_device *udev = interface_to_usbdev(intf);
 	struct msi3101_state *s = NULL;
+	const struct v4l2_ctrl_ops *ops = &msi3101_ctrl_ops;
 	int ret;
-	static const char * const ctrl_sampling_mode_qmenu_strings[] = {
-		"Quadrature Sampling",
-		NULL,
-	};
-	static const struct v4l2_ctrl_config ctrl_sampling_mode = {
-		.ops	= &msi3101_ctrl_ops,
-		.id	= MSI3101_CID_SAMPLING_MODE,
-		.type   = V4L2_CTRL_TYPE_MENU,
-		.flags  = V4L2_CTRL_FLAG_INACTIVE,
-		.name	= "Sampling Mode",
-		.qmenu  = ctrl_sampling_mode_qmenu_strings,
-	};
-	static const struct v4l2_ctrl_config ctrl_sampling_rate = {
-		.ops	= &msi3101_ctrl_ops,
-		.id	= MSI3101_CID_SAMPLING_RATE,
-		.type	= V4L2_CTRL_TYPE_INTEGER64,
-		.name	= "Sampling Rate",
-		.min	= 500000,
-		.max	= 12000000,
-		.def    = 2048000,
-		.step	= 1,
-	};
-	static const struct v4l2_ctrl_config ctrl_sampling_resolution = {
-		.ops	= &msi3101_ctrl_ops,
-		.id	= MSI3101_CID_SAMPLING_RESOLUTION,
-		.type	= V4L2_CTRL_TYPE_INTEGER,
-		.flags  = V4L2_CTRL_FLAG_INACTIVE,
-		.name	= "Sampling Resolution",
-		.min	= 10,
-		.max	= 10,
-		.def    = 10,
-		.step	= 1,
-	};
-	static const struct v4l2_ctrl_config ctrl_tuner_rf = {
-		.ops	= &msi3101_ctrl_ops,
-		.id	= MSI3101_CID_TUNER_RF,
-		.type   = V4L2_CTRL_TYPE_INTEGER64,
-		.name	= "Tuner RF",
-		.min	= 40000000,
-		.max	= 2000000000,
-		.def    = 100000000,
-		.step	= 1,
-	};
-	static const struct v4l2_ctrl_config ctrl_tuner_bw = {
-		.ops	= &msi3101_ctrl_ops,
-		.id	= MSI3101_CID_TUNER_BW,
-		.type	= V4L2_CTRL_TYPE_INTEGER,
-		.name	= "Tuner BW",
-		.min	= 200000,
-		.max	= 8000000,
-		.def    = 600000,
-		.step	= 1,
-	};
-	static const struct v4l2_ctrl_config ctrl_tuner_if = {
-		.ops	= &msi3101_ctrl_ops,
-		.id	= MSI3101_CID_TUNER_IF,
-		.type	= V4L2_CTRL_TYPE_INTEGER,
-		.flags  = V4L2_CTRL_FLAG_INACTIVE,
-		.name	= "Tuner IF",
-		.min	= 0,
-		.max	= 2048000,
-		.def    = 0,
-		.step	= 1,
-	};
-	static const struct v4l2_ctrl_config ctrl_tuner_gain = {
-		.ops	= &msi3101_ctrl_ops,
-		.id	= MSI3101_CID_TUNER_GAIN,
-		.type	= V4L2_CTRL_TYPE_INTEGER,
-		.name	= "Tuner Gain",
-		.min	= 0,
-		.max	= 102,
-		.def    = 0,
-		.step	= 1,
-	};
 
 	s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
 	if (s == NULL) {
@@ -1841,11 +1631,12 @@ static int msi3101_probe(struct usb_interface *intf,
 	mutex_init(&s->vb_queue_lock);
 	spin_lock_init(&s->queued_bufs_lock);
 	INIT_LIST_HEAD(&s->queued_bufs);
-
 	s->udev = udev;
+	s->f_adc = bands_adc[0].rangelow;
+	s->pixelformat = V4L2_SDR_FMT_CU8;
 
 	/* Init videobuf2 queue structure */
-	s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+	s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
 	s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
 	s->vb_queue.drv_priv = s;
 	s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
@@ -1853,7 +1644,7 @@ static int msi3101_probe(struct usb_interface *intf,
 	s->vb_queue.mem_ops = &vb2_vmalloc_memops;
 	s->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 	ret = vb2_queue_init(&s->vb_queue);
-	if (ret < 0) {
+	if (ret) {
 		dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
 		goto err_free_mem;
 	}
@@ -1866,16 +1657,20 @@ static int msi3101_probe(struct usb_interface *intf,
 	video_set_drvdata(&s->vdev, s);
 
 	/* Register controls */
-	v4l2_ctrl_handler_init(&s->ctrl_handler, 7);
-	v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_mode, NULL);
-	s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL);
-	v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL);
-	s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL);
-	s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL);
-	s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL);
-	s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL);
-	if (s->ctrl_handler.error) {
-		ret = s->ctrl_handler.error;
+	v4l2_ctrl_handler_init(&s->hdl, 5);
+	s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, ops,
+			V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
+	s->bandwidth = v4l2_ctrl_new_std(&s->hdl, ops,
+			V4L2_CID_RF_TUNER_BANDWIDTH, 0, 8000000, 1, 0);
+	v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
+	s->lna_gain = v4l2_ctrl_new_std(&s->hdl, ops,
+			V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
+	s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, ops,
+			V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
+	s->if_gain = v4l2_ctrl_new_std(&s->hdl, ops,
+			V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
+	if (s->hdl.error) {
+		ret = s->hdl.error;
 		dev_err(&s->udev->dev, "Could not initialize controls\n");
 		goto err_free_controls;
 	}
@@ -1889,12 +1684,12 @@ static int msi3101_probe(struct usb_interface *intf,
 		goto err_free_controls;
 	}
 
-	s->v4l2_dev.ctrl_handler = &s->ctrl_handler;
+	s->v4l2_dev.ctrl_handler = &s->hdl;
 	s->vdev.v4l2_dev = &s->v4l2_dev;
 	s->vdev.lock = &s->v4l2_lock;
 
-	ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1);
-	if (ret < 0) {
+	ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
+	if (ret) {
 		dev_err(&s->udev->dev,
 				"Failed to register as video device (%d)\n",
 				ret);
@@ -1908,7 +1703,7 @@ static int msi3101_probe(struct usb_interface *intf,
 err_unregister_v4l2_dev:
 	v4l2_device_unregister(&s->v4l2_dev);
 err_free_controls:
-	v4l2_ctrl_handler_free(&s->ctrl_handler);
+	v4l2_ctrl_handler_free(&s->hdl);
 err_free_mem:
 	kfree(s);
 	return ret;
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [REVIEW PATCH 2/6] msi001: Mirics MSi001 silicon tuner driver
  2014-02-27  0:25 [REVIEW PATCH 0/6] SDR API - Mirics MSi3101 driver Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 1/6] msi3101: convert to SDR API Antti Palosaari
@ 2014-02-27  0:25 ` Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 3/6] msi3101: use msi001 " Antti Palosaari
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Antti Palosaari @ 2014-02-27  0:25 UTC (permalink / raw)
  To: linux-media; +Cc: Hans Verkuil, Antti Palosaari

That RF tuner driver is bound via SPI bus model and it implements V4L
subdev API. I split it out from MSi3101 SDR driver.
MSi3101 = MSi2500 + MSi001.

Signed-off-by: Antti Palosaari <crope@iki.fi>
Acked-by: Hans Verkuil <hans.verkuil@cisco.com>
---
 drivers/staging/media/msi3101/Kconfig  |   4 +
 drivers/staging/media/msi3101/Makefile |   1 +
 drivers/staging/media/msi3101/msi001.c | 499 +++++++++++++++++++++++++++++++++
 3 files changed, 504 insertions(+)
 create mode 100644 drivers/staging/media/msi3101/msi001.c

diff --git a/drivers/staging/media/msi3101/Kconfig b/drivers/staging/media/msi3101/Kconfig
index 0c349c8..97d5210 100644
--- a/drivers/staging/media/msi3101/Kconfig
+++ b/drivers/staging/media/msi3101/Kconfig
@@ -3,3 +3,7 @@ config USB_MSI3101
 	depends on USB && VIDEO_DEV && VIDEO_V4L2
 	select VIDEOBUF2_CORE
 	select VIDEOBUF2_VMALLOC
+
+config MEDIA_TUNER_MSI001
+	tristate "Mirics MSi001"
+	depends on VIDEO_V4L2 && SPI
diff --git a/drivers/staging/media/msi3101/Makefile b/drivers/staging/media/msi3101/Makefile
index 3730654..daf4f58 100644
--- a/drivers/staging/media/msi3101/Makefile
+++ b/drivers/staging/media/msi3101/Makefile
@@ -1 +1,2 @@
 obj-$(CONFIG_USB_MSI3101)             += sdr-msi3101.o
+obj-$(CONFIG_MEDIA_TUNER_MSI001)      += msi001.o
diff --git a/drivers/staging/media/msi3101/msi001.c b/drivers/staging/media/msi3101/msi001.c
new file mode 100644
index 0000000..25feece
--- /dev/null
+++ b/drivers/staging/media/msi3101/msi001.c
@@ -0,0 +1,499 @@
+/*
+ * Mirics MSi001 silicon tuner driver
+ *
+ * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
+ * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
+ *
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/gcd.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-ctrls.h>
+
+static const struct v4l2_frequency_band bands[] = {
+	{
+		.type = V4L2_TUNER_RF,
+		.index = 0,
+		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
+		.rangelow   =   49000000,
+		.rangehigh  =  263000000,
+	}, {
+		.type = V4L2_TUNER_RF,
+		.index = 1,
+		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
+		.rangelow   =  390000000,
+		.rangehigh  =  960000000,
+	},
+};
+
+struct msi001 {
+	struct spi_device *spi;
+	struct v4l2_subdev sd;
+
+	/* Controls */
+	struct v4l2_ctrl_handler hdl;
+	struct v4l2_ctrl *bandwidth_auto;
+	struct v4l2_ctrl *bandwidth;
+	struct v4l2_ctrl *lna_gain;
+	struct v4l2_ctrl *mixer_gain;
+	struct v4l2_ctrl *if_gain;
+
+	unsigned int f_tuner;
+};
+
+static inline struct msi001 *sd_to_msi001(struct v4l2_subdev *sd)
+{
+	return container_of(sd, struct msi001, sd);
+}
+
+static int msi001_wreg(struct msi001 *s, u32 data)
+{
+	/* Register format: 4 bits addr + 20 bits value */
+	return spi_write(s->spi, &data, 3);
+};
+
+static int msi001_set_gain(struct msi001 *s, int lna_gain, int mixer_gain,
+		int if_gain)
+{
+	int ret;
+	u32 reg;
+	dev_dbg(&s->spi->dev, "%s: lna=%d mixer=%d if=%d\n", __func__,
+			lna_gain, mixer_gain, if_gain);
+
+	reg = 1 << 0;
+	reg |= (59 - if_gain) << 4;
+	reg |= 0 << 10;
+	reg |= (1 - mixer_gain) << 12;
+	reg |= (1 - lna_gain) << 13;
+	reg |= 4 << 14;
+	reg |= 0 << 17;
+	ret = msi001_wreg(s, reg);
+	if (ret)
+		goto err;
+
+	return 0;
+err:
+	dev_dbg(&s->spi->dev, "%s: failed %d\n", __func__, ret);
+	return ret;
+};
+
+static int msi001_set_tuner(struct msi001 *s)
+{
+	int ret, i;
+	unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;
+	u32 reg;
+	u64 f_vco, tmp64;
+	u8 mode, filter_mode, lo_div;
+	static const struct {
+		u32 rf;
+		u8 mode;
+		u8 lo_div;
+	} band_lut[] = {
+		{ 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
+		{108000000, 0x42, 32}, /* VHF_MODE */
+		{330000000, 0x44, 16}, /* B3_MODE */
+		{960000000, 0x48,  4}, /* B45_MODE */
+		{      ~0U, 0x50,  2}, /* BL_MODE */
+	};
+	static const struct {
+		u32 freq;
+		u8 filter_mode;
+	} if_freq_lut[] = {
+		{      0, 0x03}, /* Zero IF */
+		{ 450000, 0x02}, /* 450 kHz IF */
+		{1620000, 0x01}, /* 1.62 MHz IF */
+		{2048000, 0x00}, /* 2.048 MHz IF */
+	};
+	static const struct {
+		u32 freq;
+		u8 val;
+	} bandwidth_lut[] = {
+		{ 200000, 0x00}, /* 200 kHz */
+		{ 300000, 0x01}, /* 300 kHz */
+		{ 600000, 0x02}, /* 600 kHz */
+		{1536000, 0x03}, /* 1.536 MHz */
+		{5000000, 0x04}, /* 5 MHz */
+		{6000000, 0x05}, /* 6 MHz */
+		{7000000, 0x06}, /* 7 MHz */
+		{8000000, 0x07}, /* 8 MHz */
+	};
+
+	unsigned int f_rf = s->f_tuner;
+
+	/*
+	 * bandwidth (Hz)
+	 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
+	 */
+	unsigned int bandwidth;
+
+	/*
+	 * intermediate frequency (Hz)
+	 * 0, 450000, 1620000, 2048000
+	 */
+	unsigned int f_if = 0;
+	#define F_REF 24000000
+	#define R_REF 4
+	#define F_OUT_STEP 1
+
+	dev_dbg(&s->spi->dev,
+			"%s: f_rf=%d f_if=%d\n",
+			__func__, f_rf, f_if);
+
+	for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
+		if (f_rf <= band_lut[i].rf) {
+			mode = band_lut[i].mode;
+			lo_div = band_lut[i].lo_div;
+			break;
+		}
+	}
+
+	if (i == ARRAY_SIZE(band_lut)) {
+		ret = -EINVAL;
+		goto err;
+	}
+
+	/* AM_MODE is upconverted */
+	if ((mode >> 0) & 0x1)
+		f_if1 =  5 * F_REF;
+	else
+		f_if1 =  0;
+
+	for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
+		if (f_if == if_freq_lut[i].freq) {
+			filter_mode = if_freq_lut[i].filter_mode;
+			break;
+		}
+	}
+
+	if (i == ARRAY_SIZE(if_freq_lut)) {
+		ret = -EINVAL;
+		goto err;
+	}
+
+	/* filters */
+	bandwidth = s->bandwidth->val;
+	bandwidth = clamp(bandwidth, 200000U, 8000000U);
+
+	for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
+		if (bandwidth <= bandwidth_lut[i].freq) {
+			bandwidth = bandwidth_lut[i].val;
+			break;
+		}
+	}
+
+	if (i == ARRAY_SIZE(bandwidth_lut)) {
+		ret = -EINVAL;
+		goto err;
+	}
+
+	s->bandwidth->val = bandwidth_lut[i].freq;
+
+	dev_dbg(&s->spi->dev, "%s: bandwidth selected=%d\n",
+			__func__, bandwidth_lut[i].freq);
+
+	f_vco = (f_rf + f_if + f_if1) * lo_div;
+	tmp64 = f_vco;
+	m = do_div(tmp64, F_REF * R_REF);
+	n = (unsigned int) tmp64;
+
+	vco_step = F_OUT_STEP * lo_div;
+	thresh = (F_REF * R_REF) / vco_step;
+	frac = 1ul * thresh * m / (F_REF * R_REF);
+
+	/* Find out greatest common divisor and divide to smaller. */
+	tmp = gcd(thresh, frac);
+	thresh /= tmp;
+	frac /= tmp;
+
+	/* Force divide to reg max. Resolution will be reduced. */
+	tmp = DIV_ROUND_UP(thresh, 4095);
+	thresh = DIV_ROUND_CLOSEST(thresh, tmp);
+	frac = DIV_ROUND_CLOSEST(frac, tmp);
+
+	/* calc real RF set */
+	tmp = 1ul * F_REF * R_REF * n;
+	tmp += 1ul * F_REF * R_REF * frac / thresh;
+	tmp /= lo_div;
+
+	dev_dbg(&s->spi->dev,
+			"%s: rf=%u:%u n=%d thresh=%d frac=%d\n",
+				__func__, f_rf, tmp, n, thresh, frac);
+
+	ret = msi001_wreg(s, 0x00000e);
+	if (ret)
+		goto err;
+
+	ret = msi001_wreg(s, 0x000003);
+	if (ret)
+		goto err;
+
+	reg = 0 << 0;
+	reg |= mode << 4;
+	reg |= filter_mode << 12;
+	reg |= bandwidth << 14;
+	reg |= 0x02 << 17;
+	reg |= 0x00 << 20;
+	ret = msi001_wreg(s, reg);
+	if (ret)
+		goto err;
+
+	reg = 5 << 0;
+	reg |= thresh << 4;
+	reg |= 1 << 19;
+	reg |= 1 << 21;
+	ret = msi001_wreg(s, reg);
+	if (ret)
+		goto err;
+
+	reg = 2 << 0;
+	reg |= frac << 4;
+	reg |= n << 16;
+	ret = msi001_wreg(s, reg);
+	if (ret)
+		goto err;
+
+	ret = msi001_set_gain(s, s->lna_gain->cur.val, s->mixer_gain->cur.val,
+			s->if_gain->cur.val);
+	if (ret)
+		goto err;
+
+	reg = 6 << 0;
+	reg |= 63 << 4;
+	reg |= 4095 << 10;
+	ret = msi001_wreg(s, reg);
+	if (ret)
+		goto err;
+
+	return 0;
+err:
+	dev_dbg(&s->spi->dev, "%s: failed %d\n", __func__, ret);
+	return ret;
+};
+
+static int msi001_s_power(struct v4l2_subdev *sd, int on)
+{
+	struct msi001 *s = sd_to_msi001(sd);
+	int ret;
+	dev_dbg(&s->spi->dev, "%s: on=%d\n", __func__, on);
+
+	if (on)
+		ret = 0;
+	else
+		ret = msi001_wreg(s, 0x000000);
+
+	return ret;
+}
+
+static const struct v4l2_subdev_core_ops msi001_core_ops = {
+	.s_power                  = msi001_s_power,
+};
+
+static int msi001_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
+{
+	struct msi001 *s = sd_to_msi001(sd);
+	dev_dbg(&s->spi->dev, "%s: index=%d\n", __func__, v->index);
+
+	strlcpy(v->name, "Mirics MSi001", sizeof(v->name));
+	v->type = V4L2_TUNER_RF;
+	v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
+	v->rangelow =    49000000;
+	v->rangehigh =  960000000;
+
+	return 0;
+}
+
+static int msi001_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
+{
+	struct msi001 *s = sd_to_msi001(sd);
+	dev_dbg(&s->spi->dev, "%s: index=%d\n", __func__, v->index);
+	return 0;
+}
+
+static int msi001_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
+{
+	struct msi001 *s = sd_to_msi001(sd);
+	dev_dbg(&s->spi->dev, "%s: tuner=%d\n", __func__, f->tuner);
+	f->frequency = s->f_tuner;
+	return 0;
+}
+
+static int msi001_s_frequency(struct v4l2_subdev *sd,
+		const struct v4l2_frequency *f)
+{
+	struct msi001 *s = sd_to_msi001(sd);
+	unsigned int band;
+	dev_dbg(&s->spi->dev, "%s: tuner=%d type=%d frequency=%u\n",
+			__func__, f->tuner, f->type, f->frequency);
+
+	if (f->frequency < ((bands[0].rangehigh + bands[1].rangelow) / 2))
+		band = 0;
+	else
+		band = 1;
+	s->f_tuner = clamp_t(unsigned int, f->frequency,
+			bands[band].rangelow, bands[band].rangehigh);
+
+	return msi001_set_tuner(s);
+}
+
+static int msi001_enum_freq_bands(struct v4l2_subdev *sd,
+		struct v4l2_frequency_band *band)
+{
+	struct msi001 *s = sd_to_msi001(sd);
+	dev_dbg(&s->spi->dev, "%s: tuner=%d type=%d index=%d\n",
+			__func__, band->tuner, band->type, band->index);
+
+	if (band->index >= ARRAY_SIZE(bands))
+		return -EINVAL;
+
+	band->capability = bands[band->index].capability;
+	band->rangelow = bands[band->index].rangelow;
+	band->rangehigh = bands[band->index].rangehigh;
+
+	return 0;
+}
+
+static const struct v4l2_subdev_tuner_ops msi001_tuner_ops = {
+	.g_tuner                  = msi001_g_tuner,
+	.s_tuner                  = msi001_s_tuner,
+	.g_frequency              = msi001_g_frequency,
+	.s_frequency              = msi001_s_frequency,
+	.enum_freq_bands          = msi001_enum_freq_bands,
+};
+
+static const struct v4l2_subdev_ops msi001_ops = {
+	.core                     = &msi001_core_ops,
+	.tuner                    = &msi001_tuner_ops,
+};
+
+static int msi001_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct msi001 *s = container_of(ctrl->handler, struct msi001, hdl);
+
+	int ret;
+	dev_dbg(&s->spi->dev,
+			"%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
+			__func__, ctrl->id, ctrl->name, ctrl->val,
+			ctrl->minimum, ctrl->maximum, ctrl->step);
+
+	switch (ctrl->id) {
+	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
+	case V4L2_CID_RF_TUNER_BANDWIDTH:
+		ret = msi001_set_tuner(s);
+		break;
+	case  V4L2_CID_RF_TUNER_LNA_GAIN:
+		ret = msi001_set_gain(s, s->lna_gain->val,
+				s->mixer_gain->cur.val, s->if_gain->cur.val);
+		break;
+	case  V4L2_CID_RF_TUNER_MIXER_GAIN:
+		ret = msi001_set_gain(s, s->lna_gain->cur.val,
+				s->mixer_gain->val, s->if_gain->cur.val);
+		break;
+	case  V4L2_CID_RF_TUNER_IF_GAIN:
+		ret = msi001_set_gain(s, s->lna_gain->cur.val,
+				s->mixer_gain->cur.val, s->if_gain->val);
+		break;
+	default:
+		dev_dbg(&s->spi->dev, "%s: unkown control %d\n",
+				__func__, ctrl->id);
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static const struct v4l2_ctrl_ops msi001_ctrl_ops = {
+	.s_ctrl                   = msi001_s_ctrl,
+};
+
+static int msi001_probe(struct spi_device *spi)
+{
+	struct msi001 *s;
+	int ret;
+	dev_dbg(&spi->dev, "%s:\n", __func__);
+
+	s = kzalloc(sizeof(struct msi001), GFP_KERNEL);
+	if (s == NULL) {
+		ret = -ENOMEM;
+		dev_dbg(&spi->dev, "Could not allocate memory for msi001\n");
+		goto err_kfree;
+	}
+
+	s->spi = spi;
+	v4l2_spi_subdev_init(&s->sd, spi, &msi001_ops);
+
+	/* Register controls */
+	v4l2_ctrl_handler_init(&s->hdl, 5);
+	s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
+			V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
+	s->bandwidth = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
+			V4L2_CID_RF_TUNER_BANDWIDTH, 200000, 8000000, 1, 200000);
+	v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
+	s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
+			V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
+	s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
+			V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
+	s->if_gain = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
+			V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
+	if (s->hdl.error) {
+		ret = s->hdl.error;
+		dev_err(&s->spi->dev, "Could not initialize controls\n");
+		/* control init failed, free handler */
+		goto err_ctrl_handler_free;
+	}
+
+	s->sd.ctrl_handler = &s->hdl;
+	return 0;
+
+err_ctrl_handler_free:
+	v4l2_ctrl_handler_free(&s->hdl);
+err_kfree:
+	kfree(s);
+	return ret;
+}
+
+static int msi001_remove(struct spi_device *spi)
+{
+	struct v4l2_subdev *sd = spi_get_drvdata(spi);
+	struct msi001 *s = sd_to_msi001(sd);
+	dev_dbg(&spi->dev, "%s:\n", __func__);
+
+	/*
+	 * Registered by v4l2_spi_new_subdev() from master driver, but we must
+	 * unregister it from here. Weird.
+	 */
+	v4l2_device_unregister_subdev(&s->sd);
+	v4l2_ctrl_handler_free(&s->hdl);
+	kfree(s);
+	return 0;
+}
+
+static const struct spi_device_id msi001_id[] = {
+	{"msi001", 0},
+	{}
+};
+MODULE_DEVICE_TABLE(spi, msi001_id);
+
+static struct spi_driver msi001_driver = {
+	.driver = {
+		.name	= "msi001",
+		.owner	= THIS_MODULE,
+	},
+	.probe		= msi001_probe,
+	.remove		= msi001_remove,
+	.id_table	= msi001_id,
+};
+module_spi_driver(msi001_driver);
+
+MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
+MODULE_DESCRIPTION("Mirics MSi001");
+MODULE_LICENSE("GPL");
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [REVIEW PATCH 3/6] msi3101: use msi001 tuner driver
  2014-02-27  0:25 [REVIEW PATCH 0/6] SDR API - Mirics MSi3101 driver Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 1/6] msi3101: convert to SDR API Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 2/6] msi001: Mirics MSi001 silicon tuner driver Antti Palosaari
@ 2014-02-27  0:25 ` Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 4/6] MAINTAINERS: add msi001 driver Antti Palosaari
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Antti Palosaari @ 2014-02-27  0:25 UTC (permalink / raw)
  To: linux-media; +Cc: Hans Verkuil, Antti Palosaari

Remove MSi001 RF tuner related code as MSi001 functionality is moved
to own driver.

Implement SPI master adapter.

Attach MSi001 driver via SPI / V4L subdev framework.

Signed-off-by: Antti Palosaari <crope@iki.fi>
Acked-by: Hans Verkuil <hans.verkuil@cisco.com>
---
 drivers/staging/media/msi3101/Kconfig       |   3 +-
 drivers/staging/media/msi3101/sdr-msi3101.c | 483 ++++++++--------------------
 2 files changed, 136 insertions(+), 350 deletions(-)

diff --git a/drivers/staging/media/msi3101/Kconfig b/drivers/staging/media/msi3101/Kconfig
index 97d5210..de0b3bb 100644
--- a/drivers/staging/media/msi3101/Kconfig
+++ b/drivers/staging/media/msi3101/Kconfig
@@ -1,8 +1,9 @@
 config USB_MSI3101
 	tristate "Mirics MSi3101 SDR Dongle"
-	depends on USB && VIDEO_DEV && VIDEO_V4L2
+	depends on USB && VIDEO_DEV && VIDEO_V4L2 && SPI
 	select VIDEOBUF2_CORE
 	select VIDEOBUF2_VMALLOC
+	select MEDIA_TUNER_MSI001
 
 config MEDIA_TUNER_MSI001
 	tristate "Mirics MSi001"
diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c
index 5da0ad1..c111e9a 100644
--- a/drivers/staging/media/msi3101/sdr-msi3101.c
+++ b/drivers/staging/media/msi3101/sdr-msi3101.c
@@ -25,7 +25,6 @@
 
 #include <linux/module.h>
 #include <linux/slab.h>
-#include <linux/gcd.h>
 #include <asm/div64.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ioctl.h>
@@ -33,6 +32,7 @@
 #include <media/v4l2-event.h>
 #include <linux/usb.h>
 #include <media/videobuf2-vmalloc.h>
+#include <linux/spi/spi.h>
 
 /*
  *   iConfiguration          0
@@ -57,7 +57,7 @@
 #define V4L2_PIX_FMT_SDR_S14    v4l2_fourcc('D', 'S', '1', '4') /* signed 14-bit */
 #define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */
 
-static const struct v4l2_frequency_band bands_adc[] = {
+static const struct v4l2_frequency_band bands[] = {
 	{
 		.tuner = 0,
 		.type = V4L2_TUNER_ADC,
@@ -68,24 +68,6 @@ static const struct v4l2_frequency_band bands_adc[] = {
 	},
 };
 
-static const struct v4l2_frequency_band bands_rf[] = {
-	{
-		.tuner = 1,
-		.type = V4L2_TUNER_RF,
-		.index = 0,
-		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
-		.rangelow   =   49000000,
-		.rangehigh  =  263000000,
-	}, {
-		.tuner = 1,
-		.type = V4L2_TUNER_RF,
-		.index = 1,
-		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
-		.rangelow   =  390000000,
-		.rangehigh  =  960000000,
-	},
-};
-
 /* stream formats */
 struct msi3101_format {
 	char	*name;
@@ -128,6 +110,8 @@ struct msi3101_frame_buf {
 struct msi3101_state {
 	struct video_device vdev;
 	struct v4l2_device v4l2_dev;
+	struct v4l2_subdev *v4l2_subdev;
+	struct spi_master *master;
 
 	/* videobuf2 queue and queued buffers list */
 	struct vb2_queue vb_queue;
@@ -141,7 +125,7 @@ struct msi3101_state {
 	/* Pointer to our usb_device, will be NULL after unplug */
 	struct usb_device *udev; /* Both mutexes most be hold when setting! */
 
-	unsigned int f_adc, f_tuner;
+	unsigned int f_adc;
 	u32 pixelformat;
 
 	unsigned int isoc_errors; /* number of contiguous ISOC errors */
@@ -153,14 +137,6 @@ struct msi3101_state {
 
 	/* Controls */
 	struct v4l2_ctrl_handler hdl;
-	struct v4l2_ctrl *bandwidth_auto;
-	struct v4l2_ctrl *bandwidth;
-	struct v4l2_ctrl *lna_gain_auto;
-	struct v4l2_ctrl *lna_gain;
-	struct v4l2_ctrl *mixer_gain_auto;
-	struct v4l2_ctrl *mixer_gain;
-	struct v4l2_ctrl *if_gain_auto;
-	struct v4l2_ctrl *if_gain;
 
 	u32 next_sample; /* for track lost packets */
 	u32 sample; /* for sample rate calc */
@@ -822,9 +798,9 @@ static void msi3101_disconnect(struct usb_interface *intf)
 	mutex_lock(&s->v4l2_lock);
 	/* No need to keep the urbs around after disconnection */
 	s->udev = NULL;
-
 	v4l2_device_disconnect(&s->v4l2_dev);
 	video_unregister_device(&s->vdev);
+	spi_unregister_master(s->master);
 	mutex_unlock(&s->v4l2_lock);
 	mutex_unlock(&s->vb_queue_lock);
 
@@ -924,20 +900,25 @@ static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data)
 	return ret;
 };
 
-static int msi3101_tuner_write(struct msi3101_state *s, u32 data)
-{
-	return msi3101_ctrl_msg(s, CMD_WREG, data << 8 | 0x09);
-};
-
 #define F_REF 24000000
 #define DIV_R_IN 2
 static int msi3101_set_usb_adc(struct msi3101_state *s)
 {
 	int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
 	u32 reg3, reg4, reg7;
+	struct v4l2_ctrl *bandwidth_auto;
+	struct v4l2_ctrl *bandwidth;
 
 	f_sr = s->f_adc;
 
+	/* set tuner, subdev, filters according to sampling rate */
+	bandwidth_auto = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
+	bandwidth = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
+	if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
+		bandwidth = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
+		v4l2_ctrl_s_ctrl(bandwidth, s->f_adc);
+	}
+
 	/* select stream format */
 	switch (s->pixelformat) {
 	case V4L2_SDR_FMT_CU8:
@@ -1066,222 +1047,6 @@ err:
 	return ret;
 };
 
-static int msi3101_set_gain(struct msi3101_state *s)
-{
-	int ret;
-	u32 reg;
-	dev_dbg(&s->udev->dev, "%s: lna=%d mixer=%d if=%d\n", __func__,
-			s->lna_gain->val, s->mixer_gain->val, s->if_gain->val);
-
-	reg = 1 << 0;
-	reg |= (59 - s->if_gain->val) << 4;
-	reg |= 0 << 10;
-	reg |= (1 - s->mixer_gain->val) << 12;
-	reg |= (1 - s->lna_gain->val) << 13;
-	reg |= 4 << 14;
-	reg |= 0 << 17;
-	ret = msi3101_tuner_write(s, reg);
-	if (ret)
-		goto err;
-
-	return 0;
-err:
-	dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
-	return ret;
-};
-
-static int msi3101_set_tuner(struct msi3101_state *s)
-{
-	int ret, i;
-	unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;
-	u32 reg;
-	u64 f_vco, tmp64;
-	u8 mode, filter_mode, lo_div;
-	static const struct {
-		u32 rf;
-		u8 mode;
-		u8 lo_div;
-	} band_lut[] = {
-		{ 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
-		{108000000, 0x42, 32}, /* VHF_MODE */
-		{330000000, 0x44, 16}, /* B3_MODE */
-		{960000000, 0x48,  4}, /* B45_MODE */
-		{      ~0U, 0x50,  2}, /* BL_MODE */
-	};
-	static const struct {
-		u32 freq;
-		u8 filter_mode;
-	} if_freq_lut[] = {
-		{      0, 0x03}, /* Zero IF */
-		{ 450000, 0x02}, /* 450 kHz IF */
-		{1620000, 0x01}, /* 1.62 MHz IF */
-		{2048000, 0x00}, /* 2.048 MHz IF */
-	};
-	static const struct {
-		u32 freq;
-		u8 val;
-	} bandwidth_lut[] = {
-		{ 200000, 0x00}, /* 200 kHz */
-		{ 300000, 0x01}, /* 300 kHz */
-		{ 600000, 0x02}, /* 600 kHz */
-		{1536000, 0x03}, /* 1.536 MHz */
-		{5000000, 0x04}, /* 5 MHz */
-		{6000000, 0x05}, /* 6 MHz */
-		{7000000, 0x06}, /* 7 MHz */
-		{8000000, 0x07}, /* 8 MHz */
-	};
-
-	unsigned int f_rf = s->f_tuner;
-
-	/*
-	 * bandwidth (Hz)
-	 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
-	 */
-	unsigned int bandwidth;
-
-	/*
-	 * intermediate frequency (Hz)
-	 * 0, 450000, 1620000, 2048000
-	 */
-	unsigned int f_if = 0;
-
-	dev_dbg(&s->udev->dev,
-			"%s: f_rf=%d f_if=%d\n",
-			__func__, f_rf, f_if);
-
-	ret = -EINVAL;
-
-	for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
-		if (f_rf <= band_lut[i].rf) {
-			mode = band_lut[i].mode;
-			lo_div = band_lut[i].lo_div;
-			break;
-		}
-	}
-
-	if (i == ARRAY_SIZE(band_lut))
-		goto err;
-
-	/* AM_MODE is upconverted */
-	if ((mode >> 0) & 0x1)
-		f_if1 =  5 * F_REF;
-	else
-		f_if1 =  0;
-
-	for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
-		if (f_if == if_freq_lut[i].freq) {
-			filter_mode = if_freq_lut[i].filter_mode;
-			break;
-		}
-	}
-
-	if (i == ARRAY_SIZE(if_freq_lut))
-		goto err;
-
-	/* filters */
-	if (s->bandwidth_auto->val)
-		bandwidth = s->f_adc;
-	else
-		bandwidth = s->bandwidth->val;
-
-	bandwidth = clamp(bandwidth, 200000U, 8000000U);
-
-	for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
-		if (bandwidth <= bandwidth_lut[i].freq) {
-			bandwidth = bandwidth_lut[i].val;
-			break;
-		}
-	}
-
-	if (i == ARRAY_SIZE(bandwidth_lut))
-		goto err;
-
-	s->bandwidth->val = bandwidth_lut[i].freq;
-
-	dev_dbg(&s->udev->dev, "%s: bandwidth selected=%d\n",
-			__func__, bandwidth_lut[i].freq);
-
-#define F_OUT_STEP 1
-#define R_REF 4
-	f_vco = (f_rf + f_if + f_if1) * lo_div;
-
-	tmp64 = f_vco;
-	m = do_div(tmp64, F_REF * R_REF);
-	n = (unsigned int) tmp64;
-
-	vco_step = F_OUT_STEP * lo_div;
-	thresh = (F_REF * R_REF) / vco_step;
-	frac = 1ul * thresh * m / (F_REF * R_REF);
-
-	/* Find out greatest common divisor and divide to smaller. */
-	tmp = gcd(thresh, frac);
-	thresh /= tmp;
-	frac /= tmp;
-
-	/* Force divide to reg max. Resolution will be reduced. */
-	tmp = DIV_ROUND_UP(thresh, 4095);
-	thresh = DIV_ROUND_CLOSEST(thresh, tmp);
-	frac = DIV_ROUND_CLOSEST(frac, tmp);
-
-	/* calc real RF set */
-	tmp = 1ul * F_REF * R_REF * n;
-	tmp += 1ul * F_REF * R_REF * frac / thresh;
-	tmp /= lo_div;
-
-	dev_dbg(&s->udev->dev,
-			"%s: rf=%u:%u n=%d thresh=%d frac=%d\n",
-				__func__, f_rf, tmp, n, thresh, frac);
-
-	ret = msi3101_tuner_write(s, 0x00000e);
-	if (ret)
-		goto err;
-
-	ret = msi3101_tuner_write(s, 0x000003);
-	if (ret)
-		goto err;
-
-	reg = 0 << 0;
-	reg |= mode << 4;
-	reg |= filter_mode << 12;
-	reg |= bandwidth << 14;
-	reg |= 0x02 << 17;
-	reg |= 0x00 << 20;
-	ret = msi3101_tuner_write(s, reg);
-	if (ret)
-		goto err;
-
-	reg = 5 << 0;
-	reg |= thresh << 4;
-	reg |= 1 << 19;
-	reg |= 1 << 21;
-	ret = msi3101_tuner_write(s, reg);
-	if (ret)
-		goto err;
-
-	reg = 2 << 0;
-	reg |= frac << 4;
-	reg |= n << 16;
-	ret = msi3101_tuner_write(s, reg);
-	if (ret)
-		goto err;
-
-	ret = msi3101_set_gain(s);
-	if (ret)
-		goto err;
-
-	reg = 6 << 0;
-	reg |= 63 << 4;
-	reg |= 4095 << 10;
-	ret = msi3101_tuner_write(s, reg);
-	if (ret)
-		goto err;
-
-	return 0;
-err:
-	dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
-	return ret;
-};
-
 static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
 {
 	struct msi3101_state *s = vb2_get_drv_priv(vq);
@@ -1294,6 +1059,9 @@ static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
 	if (mutex_lock_interruptible(&s->v4l2_lock))
 		return -ERESTARTSYS;
 
+	/* wake-up tuner */
+	v4l2_subdev_call(s->v4l2_subdev, core, s_power, 1);
+
 	ret = msi3101_set_usb_adc(s);
 
 	ret = msi3101_isoc_init(s);
@@ -1328,7 +1096,7 @@ static int msi3101_stop_streaming(struct vb2_queue *vq)
 	msi3101_ctrl_msg(s, CMD_WREG, 0x01000003);
 
 	/* sleep tuner */
-	msi3101_tuner_write(s, 0x000000);
+	v4l2_subdev_call(s->v4l2_subdev, core, s_power, 0);
 
 	mutex_unlock(&s->v4l2_lock);
 
@@ -1418,33 +1186,39 @@ static int msi3101_s_tuner(struct file *file, void *priv,
 		const struct v4l2_tuner *v)
 {
 	struct msi3101_state *s = video_drvdata(file);
-	dev_dbg(&s->udev->dev, "%s:\n", __func__);
+	int ret;
+	dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
 
-	return 0;
+	if (v->index == 0)
+		ret = 0;
+	else if (v->index == 1)
+		ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_tuner, v);
+	else
+		ret = -EINVAL;
+
+	return ret;
 }
 
 static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
 {
 	struct msi3101_state *s = video_drvdata(file);
-	dev_dbg(&s->udev->dev, "%s:\n", __func__);
+	int ret;
+	dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
 
 	if (v->index == 0) {
-		strlcpy(v->name, "ADC: Mirics MSi2500", sizeof(v->name));
+		strlcpy(v->name, "Mirics MSi2500", sizeof(v->name));
 		v->type = V4L2_TUNER_ADC;
 		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
 		v->rangelow =   1200000;
 		v->rangehigh = 15000000;
+		ret = 0;
 	} else if (v->index == 1) {
-		strlcpy(v->name, "RF: Mirics MSi001", sizeof(v->name));
-		v->type = V4L2_TUNER_RF;
-		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
-		v->rangelow =    49000000;
-		v->rangehigh =  960000000;
+		ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_tuner, v);
 	} else {
-		return -EINVAL;
+		ret = -EINVAL;
 	}
 
-	return 0;
+	return ret;
 }
 
 static int msi3101_g_frequency(struct file *file, void *priv,
@@ -1455,12 +1229,14 @@ static int msi3101_g_frequency(struct file *file, void *priv,
 	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n",
 			__func__, f->tuner, f->type);
 
-	if (f->tuner == 0)
+	if (f->tuner == 0) {
 		f->frequency = s->f_adc;
-	else if (f->tuner == 1)
-		f->frequency = s->f_tuner;
-	else
-		return -EINVAL;
+		ret = 0;
+	} else if (f->tuner == 1) {
+		ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_frequency, f);
+	} else {
+		ret = -EINVAL;
+	}
 
 	return ret;
 }
@@ -1469,31 +1245,21 @@ static int msi3101_s_frequency(struct file *file, void *priv,
 		const struct v4l2_frequency *f)
 {
 	struct msi3101_state *s = video_drvdata(file);
-	int ret, band;
+	int ret;
 	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n",
 			__func__, f->tuner, f->type, f->frequency);
 
 	if (f->tuner == 0) {
 		s->f_adc = clamp_t(unsigned int, f->frequency,
-				bands_adc[0].rangelow,
-				bands_adc[0].rangehigh);
+				bands[0].rangelow,
+				bands[0].rangehigh);
 		dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n",
 				__func__, s->f_adc);
 		ret = msi3101_set_usb_adc(s);
 	} else if (f->tuner == 1) {
-		#define BAND_RF_0 ((bands_rf[0].rangehigh + bands_rf[1].rangelow) / 2)
-		if (f->frequency < BAND_RF_0)
-			band = 0;
-		else
-			band = 1;
-		s->f_tuner = clamp_t(unsigned int, f->frequency,
-				bands_rf[band].rangelow,
-				bands_rf[band].rangehigh);
-		dev_dbg(&s->udev->dev, "%s: RF frequency=%u Hz\n",
-				__func__, f->frequency);
-		ret = msi3101_set_tuner(s);
+		ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_frequency, f);
 	} else {
-		return -EINVAL;
+		ret = -EINVAL;
 	}
 
 	return ret;
@@ -1503,24 +1269,25 @@ static int msi3101_enum_freq_bands(struct file *file, void *priv,
 		struct v4l2_frequency_band *band)
 {
 	struct msi3101_state *s = video_drvdata(file);
+	int ret;
 	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n",
 			__func__, band->tuner, band->type, band->index);
 
 	if (band->tuner == 0) {
-		if (band->index >= ARRAY_SIZE(bands_adc))
-			return -EINVAL;
-
-		*band = bands_adc[band->index];
+		if (band->index >= ARRAY_SIZE(bands)) {
+			ret = -EINVAL;
+		} else {
+			*band = bands[band->index];
+			ret = 0;
+		}
 	} else if (band->tuner == 1) {
-		if (band->index >= ARRAY_SIZE(bands_rf))
-			return -EINVAL;
-
-		*band = bands_rf[band->index];
+		ret = v4l2_subdev_call(s->v4l2_subdev, tuner,
+				enum_freq_bands, band);
 	} else {
-		return -EINVAL;
+		ret = -EINVAL;
 	}
 
-	return 0;
+	return ret;
 }
 
 static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
@@ -1570,39 +1337,6 @@ static struct video_device msi3101_template = {
 	.ioctl_ops                = &msi3101_ioctl_ops,
 };
 
-static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
-{
-	struct msi3101_state *s =
-			container_of(ctrl->handler, struct msi3101_state,
-					hdl);
-	int ret;
-	dev_dbg(&s->udev->dev,
-			"%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
-			__func__, ctrl->id, ctrl->name, ctrl->val,
-			ctrl->minimum, ctrl->maximum, ctrl->step);
-
-	switch (ctrl->id) {
-	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
-	case V4L2_CID_RF_TUNER_BANDWIDTH:
-		ret = msi3101_set_tuner(s);
-		break;
-	case  V4L2_CID_RF_TUNER_LNA_GAIN:
-	case  V4L2_CID_RF_TUNER_MIXER_GAIN:
-	case  V4L2_CID_RF_TUNER_IF_GAIN:
-		ret = msi3101_set_gain(s);
-		break;
-	default:
-		dev_dbg(&s->udev->dev, "%s: EINVAL\n", __func__);
-		ret = -EINVAL;
-	}
-
-	return ret;
-}
-
-static const struct v4l2_ctrl_ops msi3101_ctrl_ops = {
-	.s_ctrl = msi3101_s_ctrl,
-};
-
 static void msi3101_video_release(struct v4l2_device *v)
 {
 	struct msi3101_state *s =
@@ -1613,13 +1347,43 @@ static void msi3101_video_release(struct v4l2_device *v)
 	kfree(s);
 }
 
+static int msi3101_transfer_one_message(struct spi_master *master,
+		struct spi_message *m)
+{
+	struct msi3101_state *s = spi_master_get_devdata(master);
+	struct spi_transfer *t;
+	int ret = 0;
+	u32 data;
+
+	list_for_each_entry(t, &m->transfers, transfer_list) {
+		dev_dbg(&s->udev->dev, "%s: msg=%*ph\n",
+				__func__, t->len, t->tx_buf);
+		data = 0x09; /* reg 9 is SPI adapter */
+		data |= ((u8 *)t->tx_buf)[0] << 8;
+		data |= ((u8 *)t->tx_buf)[1] << 16;
+		data |= ((u8 *)t->tx_buf)[2] << 24;
+		ret = msi3101_ctrl_msg(s, CMD_WREG, data);
+	}
+
+	m->status = ret;
+	spi_finalize_current_message(master);
+	return ret;
+}
+
 static int msi3101_probe(struct usb_interface *intf,
 		const struct usb_device_id *id)
 {
 	struct usb_device *udev = interface_to_usbdev(intf);
 	struct msi3101_state *s = NULL;
-	const struct v4l2_ctrl_ops *ops = &msi3101_ctrl_ops;
+	struct v4l2_subdev *sd;
+	struct spi_master *master;
 	int ret;
+	static struct spi_board_info board_info = {
+		.modalias		= "msi001",
+		.bus_num		= 0,
+		.chip_select		= 0,
+		.max_speed_hz		= 12000000,
+	};
 
 	s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
 	if (s == NULL) {
@@ -1632,7 +1396,7 @@ static int msi3101_probe(struct usb_interface *intf,
 	spin_lock_init(&s->queued_bufs_lock);
 	INIT_LIST_HEAD(&s->queued_bufs);
 	s->udev = udev;
-	s->f_adc = bands_adc[0].rangelow;
+	s->f_adc = bands[0].rangelow;
 	s->pixelformat = V4L2_SDR_FMT_CU8;
 
 	/* Init videobuf2 queue structure */
@@ -1656,34 +1420,53 @@ static int msi3101_probe(struct usb_interface *intf,
 	set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags);
 	video_set_drvdata(&s->vdev, s);
 
-	/* Register controls */
-	v4l2_ctrl_handler_init(&s->hdl, 5);
-	s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, ops,
-			V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
-	s->bandwidth = v4l2_ctrl_new_std(&s->hdl, ops,
-			V4L2_CID_RF_TUNER_BANDWIDTH, 0, 8000000, 1, 0);
-	v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
-	s->lna_gain = v4l2_ctrl_new_std(&s->hdl, ops,
-			V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
-	s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, ops,
-			V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
-	s->if_gain = v4l2_ctrl_new_std(&s->hdl, ops,
-			V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
-	if (s->hdl.error) {
-		ret = s->hdl.error;
-		dev_err(&s->udev->dev, "Could not initialize controls\n");
-		goto err_free_controls;
-	}
-
 	/* Register the v4l2_device structure */
 	s->v4l2_dev.release = msi3101_video_release;
 	ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
 	if (ret) {
 		dev_err(&s->udev->dev,
 				"Failed to register v4l2-device (%d)\n", ret);
+		goto err_free_mem;
+	}
+
+	/* SPI master adapter */
+	master = spi_alloc_master(&s->udev->dev, 0);
+	if (master == NULL) {
+		ret = -ENOMEM;
+		goto err_unregister_v4l2_dev;
+	}
+
+	s->master = master;
+	master->bus_num = 0;
+	master->num_chipselect = 1;
+	master->transfer_one_message = msi3101_transfer_one_message;
+	spi_master_set_devdata(master, s);
+	ret = spi_register_master(master);
+	if (ret) {
+		spi_master_put(master);
+		goto err_unregister_v4l2_dev;
+	}
+
+	/* load v4l2 subdevice */
+	sd = v4l2_spi_new_subdev(&s->v4l2_dev, master, &board_info);
+	s->v4l2_subdev = sd;
+	if (sd == NULL) {
+		dev_err(&s->udev->dev, "cannot get v4l2 subdevice\n");
+		ret = -ENODEV;
+		goto err_unregister_master;
+	}
+
+	/* Register controls */
+	v4l2_ctrl_handler_init(&s->hdl, 0);
+	if (s->hdl.error) {
+		ret = s->hdl.error;
+		dev_err(&s->udev->dev, "Could not initialize controls\n");
 		goto err_free_controls;
 	}
 
+	/* currently all controls are from subdev */
+	v4l2_ctrl_add_handler(&s->hdl, sd->ctrl_handler, NULL);
+
 	s->v4l2_dev.ctrl_handler = &s->hdl;
 	s->vdev.v4l2_dev = &s->v4l2_dev;
 	s->vdev.lock = &s->v4l2_lock;
@@ -1700,10 +1483,12 @@ static int msi3101_probe(struct usb_interface *intf,
 
 	return 0;
 
-err_unregister_v4l2_dev:
-	v4l2_device_unregister(&s->v4l2_dev);
 err_free_controls:
 	v4l2_ctrl_handler_free(&s->hdl);
+err_unregister_master:
+	spi_unregister_master(s->master);
+err_unregister_v4l2_dev:
+	v4l2_device_unregister(&s->v4l2_dev);
 err_free_mem:
 	kfree(s);
 	return ret;
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [REVIEW PATCH 4/6] MAINTAINERS: add msi001 driver
  2014-02-27  0:25 [REVIEW PATCH 0/6] SDR API - Mirics MSi3101 driver Antti Palosaari
                   ` (2 preceding siblings ...)
  2014-02-27  0:25 ` [REVIEW PATCH 3/6] msi3101: use msi001 " Antti Palosaari
@ 2014-02-27  0:25 ` Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 5/6] MAINTAINERS: add msi3101 driver Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 6/6] msi3101: clamp mmap buffers to reasonable level Antti Palosaari
  5 siblings, 0 replies; 8+ messages in thread
From: Antti Palosaari @ 2014-02-27  0:25 UTC (permalink / raw)
  To: linux-media; +Cc: Hans Verkuil, Antti Palosaari

Mirics MSi001 silicon tuner driver. Currently in staging as SDR API
is not ready.

Signed-off-by: Antti Palosaari <crope@iki.fi>
Acked-by: Hans Verkuil <hans.verkuil@cisco.com>
---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index b2cf5cf..15ebabb 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5757,6 +5757,16 @@ L:	platform-driver-x86@vger.kernel.org
 S:	Supported
 F:	drivers/platform/x86/msi-wmi.c
 
+MSI001 MEDIA DRIVER
+M:	Antti Palosaari <crope@iki.fi>
+L:	linux-media@vger.kernel.org
+W:	http://linuxtv.org/
+W:	http://palosaari.fi/linux/
+Q:	http://patchwork.linuxtv.org/project/linux-media/list/
+T:	git git://linuxtv.org/anttip/media_tree.git
+S:	Maintained
+F:	drivers/staging/media/msi3101/msi001*
+
 MT9M032 APTINA SENSOR DRIVER
 M:	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 L:	linux-media@vger.kernel.org
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [REVIEW PATCH 5/6] MAINTAINERS: add msi3101 driver
  2014-02-27  0:25 [REVIEW PATCH 0/6] SDR API - Mirics MSi3101 driver Antti Palosaari
                   ` (3 preceding siblings ...)
  2014-02-27  0:25 ` [REVIEW PATCH 4/6] MAINTAINERS: add msi001 driver Antti Palosaari
@ 2014-02-27  0:25 ` Antti Palosaari
  2014-02-27  0:25 ` [REVIEW PATCH 6/6] msi3101: clamp mmap buffers to reasonable level Antti Palosaari
  5 siblings, 0 replies; 8+ messages in thread
From: Antti Palosaari @ 2014-02-27  0:25 UTC (permalink / raw)
  To: linux-media; +Cc: Hans Verkuil, Antti Palosaari

Mirics MSi2500 (MSi3101) SDR ADC + USB interface driver. Currently
in staging as SDR API is not ready.

Signed-off-by: Antti Palosaari <crope@iki.fi>
Acked-by: Hans Verkuil <hans.verkuil@cisco.com>
---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 15ebabb..f03772a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5767,6 +5767,16 @@ T:	git git://linuxtv.org/anttip/media_tree.git
 S:	Maintained
 F:	drivers/staging/media/msi3101/msi001*
 
+MSI3101 MEDIA DRIVER
+M:	Antti Palosaari <crope@iki.fi>
+L:	linux-media@vger.kernel.org
+W:	http://linuxtv.org/
+W:	http://palosaari.fi/linux/
+Q:	http://patchwork.linuxtv.org/project/linux-media/list/
+T:	git git://linuxtv.org/anttip/media_tree.git
+S:	Maintained
+F:	drivers/staging/media/msi3101/sdr-msi3101*
+
 MT9M032 APTINA SENSOR DRIVER
 M:	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 L:	linux-media@vger.kernel.org
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [REVIEW PATCH 6/6] msi3101: clamp mmap buffers to reasonable level
  2014-02-27  0:25 [REVIEW PATCH 0/6] SDR API - Mirics MSi3101 driver Antti Palosaari
                   ` (4 preceding siblings ...)
  2014-02-27  0:25 ` [REVIEW PATCH 5/6] MAINTAINERS: add msi3101 driver Antti Palosaari
@ 2014-02-27  0:25 ` Antti Palosaari
  5 siblings, 0 replies; 8+ messages in thread
From: Antti Palosaari @ 2014-02-27  0:25 UTC (permalink / raw)
  To: linux-media; +Cc: Hans Verkuil, Antti Palosaari

That value is coming from the user and we need only ensure it is
reasonable. That was pointed by Hans when reviewing rtl2832_sdr driver.

Signed-off-by: Antti Palosaari <crope@iki.fi>
Cc: Hans Verkuil <hverkuil@xs4all.nl>
---
 drivers/staging/media/msi3101/sdr-msi3101.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c
index c111e9a..000395a 100644
--- a/drivers/staging/media/msi3101/sdr-msi3101.c
+++ b/drivers/staging/media/msi3101/sdr-msi3101.c
@@ -831,7 +831,7 @@ static int msi3101_queue_setup(struct vb2_queue *vq,
 	dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers);
 
 	/* Absolute min and max number of buffers available for mmap() */
-	*nbuffers = 32;
+	*nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32);
 	*nplanes = 1;
 	/*
 	 *   3, wMaxPacketSize 3x 1024 bytes
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [REVIEW PATCH 1/6] msi3101: convert to SDR API
  2014-02-27  0:25 ` [REVIEW PATCH 1/6] msi3101: convert to SDR API Antti Palosaari
@ 2014-03-13 13:21   ` Mauro Carvalho Chehab
  0 siblings, 0 replies; 8+ messages in thread
From: Mauro Carvalho Chehab @ 2014-03-13 13:21 UTC (permalink / raw)
  To: Antti Palosaari; +Cc: linux-media, Hans Verkuil

Em Thu, 27 Feb 2014 02:25:17 +0200
Antti Palosaari <crope@iki.fi> escreveu:

> Massive rewrite. Use SDR API. Fix bugs.

Please always check your patches with checkpatch.pl. This one has some
issues:

WARNING: Comparing jiffies is almost always wrong; prefer time_after, time_before and friends
#589: FILE: drivers/staging/media/msi3101/sdr-msi3101.c:235:
+	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {

WARNING: Comparing jiffies is almost always wrong; prefer time_after, time_before and friends
#799: FILE: drivers/staging/media/msi3101/sdr-msi3101.c:387:
+	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {

WARNING: Comparing jiffies is almost always wrong; prefer time_after, time_before and friends
#885: FILE: drivers/staging/media/msi3101/sdr-msi3101.c:452:
+	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {

WARNING: Comparing jiffies is almost always wrong; prefer time_after, time_before and friends
#959: FILE: drivers/staging/media/msi3101/sdr-msi3101.c:515:
+	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {

For all the above, you should use the time_is_after_jiffies() macros.

ERROR: space required after that ';' (ctx:VxV)
#988: FILE: drivers/staging/media/msi3101/sdr-msi3101.c:539:
+	struct {signed int x:14;} se;
 	                       ^

For this one, please don't put multiple statements on a single line.

Ok, this is a staging driver, so I'll apply it, but please fix it
latter, to make easier for us to move it out of staging when done.

Regards,
Mauro

> 
> Signed-off-by: Antti Palosaari <crope@iki.fi>
> Acked-by: Hans Verkuil <hans.verkuil@cisco.com>
> ---
>  drivers/staging/media/msi3101/sdr-msi3101.c | 1351 ++++++++++++---------------
>  1 file changed, 573 insertions(+), 778 deletions(-)
> 
> diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c
> index 4c3bf77..5da0ad1 100644
> --- a/drivers/staging/media/msi3101/sdr-msi3101.c
> +++ b/drivers/staging/media/msi3101/sdr-msi3101.c
> @@ -21,20 +21,6 @@
>   *  (C) 1999-2004 Nemosoft Unv.
>   *  (C) 2004-2006 Luc Saillard (luc@saillard.org)
>   *  (C) 2011 Hans de Goede <hdegoede@redhat.com>
> - *
> - * Development tree of that driver will be on:
> - * http://git.linuxtv.org/anttip/media_tree.git/shortlog/refs/heads/mirics
> - *
> - * GNU Radio plugin "gr-kernel" for device usage will be on:
> - * http://git.linuxtv.org/anttip/gr-kernel.git
> - *
> - * TODO:
> - * Help is very highly welcome for these + all the others you could imagine:
> - * - split USB ADC interface and RF tuner to own drivers (msi2500 and msi001)
> - * - move controls to V4L2 API
> - * - use libv4l2 for stream format conversions
> - * - gr-kernel: switch to v4l2_mmap (current read eats a lot of cpu)
> - * - SDRSharp support
>   */
>  
>  #include <linux/module.h>
> @@ -48,317 +34,6 @@
>  #include <linux/usb.h>
>  #include <media/videobuf2-vmalloc.h>
>  
> -struct msi3101_gain {
> -	u8 tot:7;
> -	u8 baseband:6;
> -	bool lna:1;
> -	bool mixer:1;
> -};
> -
> -/* 60 – 120 MHz band, lna 24dB, mixer 19dB */
> -static const struct msi3101_gain msi3101_gain_lut_120[] = {
> -	{  0,  0,  0,  0},
> -	{  1,  1,  0,  0},
> -	{  2,  2,  0,  0},
> -	{  3,  3,  0,  0},
> -	{  4,  4,  0,  0},
> -	{  5,  5,  0,  0},
> -	{  6,  6,  0,  0},
> -	{  7,  7,  0,  0},
> -	{  8,  8,  0,  0},
> -	{  9,  9,  0,  0},
> -	{ 10, 10,  0,  0},
> -	{ 11, 11,  0,  0},
> -	{ 12, 12,  0,  0},
> -	{ 13, 13,  0,  0},
> -	{ 14, 14,  0,  0},
> -	{ 15, 15,  0,  0},
> -	{ 16, 16,  0,  0},
> -	{ 17, 17,  0,  0},
> -	{ 18, 18,  0,  0},
> -	{ 19, 19,  0,  0},
> -	{ 20, 20,  0,  0},
> -	{ 21, 21,  0,  0},
> -	{ 22, 22,  0,  0},
> -	{ 23, 23,  0,  0},
> -	{ 24, 24,  0,  0},
> -	{ 25, 25,  0,  0},
> -	{ 26, 26,  0,  0},
> -	{ 27, 27,  0,  0},
> -	{ 28, 28,  0,  0},
> -	{ 29,  5,  1,  0},
> -	{ 30,  6,  1,  0},
> -	{ 31,  7,  1,  0},
> -	{ 32,  8,  1,  0},
> -	{ 33,  9,  1,  0},
> -	{ 34, 10,  1,  0},
> -	{ 35, 11,  1,  0},
> -	{ 36, 12,  1,  0},
> -	{ 37, 13,  1,  0},
> -	{ 38, 14,  1,  0},
> -	{ 39, 15,  1,  0},
> -	{ 40, 16,  1,  0},
> -	{ 41, 17,  1,  0},
> -	{ 42, 18,  1,  0},
> -	{ 43, 19,  1,  0},
> -	{ 44, 20,  1,  0},
> -	{ 45, 21,  1,  0},
> -	{ 46, 22,  1,  0},
> -	{ 47, 23,  1,  0},
> -	{ 48, 24,  1,  0},
> -	{ 49, 25,  1,  0},
> -	{ 50, 26,  1,  0},
> -	{ 51, 27,  1,  0},
> -	{ 52, 28,  1,  0},
> -	{ 53, 29,  1,  0},
> -	{ 54, 30,  1,  0},
> -	{ 55, 31,  1,  0},
> -	{ 56, 32,  1,  0},
> -	{ 57, 33,  1,  0},
> -	{ 58, 34,  1,  0},
> -	{ 59, 35,  1,  0},
> -	{ 60, 36,  1,  0},
> -	{ 61, 37,  1,  0},
> -	{ 62, 38,  1,  0},
> -	{ 63, 39,  1,  0},
> -	{ 64, 40,  1,  0},
> -	{ 65, 41,  1,  0},
> -	{ 66, 42,  1,  0},
> -	{ 67, 43,  1,  0},
> -	{ 68, 44,  1,  0},
> -	{ 69, 45,  1,  0},
> -	{ 70, 46,  1,  0},
> -	{ 71, 47,  1,  0},
> -	{ 72, 48,  1,  0},
> -	{ 73, 49,  1,  0},
> -	{ 74, 50,  1,  0},
> -	{ 75, 51,  1,  0},
> -	{ 76, 52,  1,  0},
> -	{ 77, 53,  1,  0},
> -	{ 78, 54,  1,  0},
> -	{ 79, 55,  1,  0},
> -	{ 80, 56,  1,  0},
> -	{ 81, 57,  1,  0},
> -	{ 82, 58,  1,  0},
> -	{ 83, 40,  1,  1},
> -	{ 84, 41,  1,  1},
> -	{ 85, 42,  1,  1},
> -	{ 86, 43,  1,  1},
> -	{ 87, 44,  1,  1},
> -	{ 88, 45,  1,  1},
> -	{ 89, 46,  1,  1},
> -	{ 90, 47,  1,  1},
> -	{ 91, 48,  1,  1},
> -	{ 92, 49,  1,  1},
> -	{ 93, 50,  1,  1},
> -	{ 94, 51,  1,  1},
> -	{ 95, 52,  1,  1},
> -	{ 96, 53,  1,  1},
> -	{ 97, 54,  1,  1},
> -	{ 98, 55,  1,  1},
> -	{ 99, 56,  1,  1},
> -	{100, 57,  1,  1},
> -	{101, 58,  1,  1},
> -	{102, 59,  1,  1},
> -};
> -
> -/* 120 – 245 MHz band, lna 24dB, mixer 19dB */
> -static const struct msi3101_gain msi3101_gain_lut_245[] = {
> -	{  0,  0,  0,  0},
> -	{  1,  1,  0,  0},
> -	{  2,  2,  0,  0},
> -	{  3,  3,  0,  0},
> -	{  4,  4,  0,  0},
> -	{  5,  5,  0,  0},
> -	{  6,  6,  0,  0},
> -	{  7,  7,  0,  0},
> -	{  8,  8,  0,  0},
> -	{  9,  9,  0,  0},
> -	{ 10, 10,  0,  0},
> -	{ 11, 11,  0,  0},
> -	{ 12, 12,  0,  0},
> -	{ 13, 13,  0,  0},
> -	{ 14, 14,  0,  0},
> -	{ 15, 15,  0,  0},
> -	{ 16, 16,  0,  0},
> -	{ 17, 17,  0,  0},
> -	{ 18, 18,  0,  0},
> -	{ 19, 19,  0,  0},
> -	{ 20, 20,  0,  0},
> -	{ 21, 21,  0,  0},
> -	{ 22, 22,  0,  0},
> -	{ 23, 23,  0,  0},
> -	{ 24, 24,  0,  0},
> -	{ 25, 25,  0,  0},
> -	{ 26, 26,  0,  0},
> -	{ 27, 27,  0,  0},
> -	{ 28, 28,  0,  0},
> -	{ 29,  5,  1,  0},
> -	{ 30,  6,  1,  0},
> -	{ 31,  7,  1,  0},
> -	{ 32,  8,  1,  0},
> -	{ 33,  9,  1,  0},
> -	{ 34, 10,  1,  0},
> -	{ 35, 11,  1,  0},
> -	{ 36, 12,  1,  0},
> -	{ 37, 13,  1,  0},
> -	{ 38, 14,  1,  0},
> -	{ 39, 15,  1,  0},
> -	{ 40, 16,  1,  0},
> -	{ 41, 17,  1,  0},
> -	{ 42, 18,  1,  0},
> -	{ 43, 19,  1,  0},
> -	{ 44, 20,  1,  0},
> -	{ 45, 21,  1,  0},
> -	{ 46, 22,  1,  0},
> -	{ 47, 23,  1,  0},
> -	{ 48, 24,  1,  0},
> -	{ 49, 25,  1,  0},
> -	{ 50, 26,  1,  0},
> -	{ 51, 27,  1,  0},
> -	{ 52, 28,  1,  0},
> -	{ 53, 29,  1,  0},
> -	{ 54, 30,  1,  0},
> -	{ 55, 31,  1,  0},
> -	{ 56, 32,  1,  0},
> -	{ 57, 33,  1,  0},
> -	{ 58, 34,  1,  0},
> -	{ 59, 35,  1,  0},
> -	{ 60, 36,  1,  0},
> -	{ 61, 37,  1,  0},
> -	{ 62, 38,  1,  0},
> -	{ 63, 39,  1,  0},
> -	{ 64, 40,  1,  0},
> -	{ 65, 41,  1,  0},
> -	{ 66, 42,  1,  0},
> -	{ 67, 43,  1,  0},
> -	{ 68, 44,  1,  0},
> -	{ 69, 45,  1,  0},
> -	{ 70, 46,  1,  0},
> -	{ 71, 47,  1,  0},
> -	{ 72, 48,  1,  0},
> -	{ 73, 49,  1,  0},
> -	{ 74, 50,  1,  0},
> -	{ 75, 51,  1,  0},
> -	{ 76, 52,  1,  0},
> -	{ 77, 53,  1,  0},
> -	{ 78, 54,  1,  0},
> -	{ 79, 55,  1,  0},
> -	{ 80, 56,  1,  0},
> -	{ 81, 57,  1,  0},
> -	{ 82, 58,  1,  0},
> -	{ 83, 40,  1,  1},
> -	{ 84, 41,  1,  1},
> -	{ 85, 42,  1,  1},
> -	{ 86, 43,  1,  1},
> -	{ 87, 44,  1,  1},
> -	{ 88, 45,  1,  1},
> -	{ 89, 46,  1,  1},
> -	{ 90, 47,  1,  1},
> -	{ 91, 48,  1,  1},
> -	{ 92, 49,  1,  1},
> -	{ 93, 50,  1,  1},
> -	{ 94, 51,  1,  1},
> -	{ 95, 52,  1,  1},
> -	{ 96, 53,  1,  1},
> -	{ 97, 54,  1,  1},
> -	{ 98, 55,  1,  1},
> -	{ 99, 56,  1,  1},
> -	{100, 57,  1,  1},
> -	{101, 58,  1,  1},
> -	{102, 59,  1,  1},
> -};
> -
> -/* 420 – 1000 MHz band, lna 7dB, mixer 19dB */
> -static const struct msi3101_gain msi3101_gain_lut_1000[] = {
> -	{  0,  0, 0,  0},
> -	{  1,  1, 0,  0},
> -	{  2,  2, 0,  0},
> -	{  3,  3, 0,  0},
> -	{  4,  4, 0,  0},
> -	{  5,  5, 0,  0},
> -	{  6,  6, 0,  0},
> -	{  7,  7, 0,  0},
> -	{  8,  8, 0,  0},
> -	{  9,  9, 0,  0},
> -	{ 10, 10, 0,  0},
> -	{ 11, 11, 0,  0},
> -	{ 12,  5, 1,  0},
> -	{ 13,  6, 1,  0},
> -	{ 14,  7, 1,  0},
> -	{ 15,  8, 1,  0},
> -	{ 16,  9, 1,  0},
> -	{ 17, 10, 1,  0},
> -	{ 18, 11, 1,  0},
> -	{ 19, 12, 1,  0},
> -	{ 20, 13, 1,  0},
> -	{ 21, 14, 1,  0},
> -	{ 22, 15, 1,  0},
> -	{ 23, 16, 1,  0},
> -	{ 24, 17, 1,  0},
> -	{ 25, 18, 1,  0},
> -	{ 26, 19, 1,  0},
> -	{ 27, 20, 1,  0},
> -	{ 28, 21, 1,  0},
> -	{ 29, 22, 1,  0},
> -	{ 30, 23, 1,  0},
> -	{ 31, 24, 1,  0},
> -	{ 32, 25, 1,  0},
> -	{ 33, 26, 1,  0},
> -	{ 34, 27, 1,  0},
> -	{ 35, 28, 1,  0},
> -	{ 36, 29, 1,  0},
> -	{ 37, 30, 1,  0},
> -	{ 38, 31, 1,  0},
> -	{ 39, 32, 1,  0},
> -	{ 40, 33, 1,  0},
> -	{ 41, 34, 1,  0},
> -	{ 42, 35, 1,  0},
> -	{ 43, 36, 1,  0},
> -	{ 44, 37, 1,  0},
> -	{ 45, 38, 1,  0},
> -	{ 46, 39, 1,  0},
> -	{ 47, 40, 1,  0},
> -	{ 48, 41, 1,  0},
> -	{ 49, 42, 1,  0},
> -	{ 50, 43, 1,  0},
> -	{ 51, 44, 1,  0},
> -	{ 52, 45, 1,  0},
> -	{ 53, 46, 1,  0},
> -	{ 54, 47, 1,  0},
> -	{ 55, 48, 1,  0},
> -	{ 56, 49, 1,  0},
> -	{ 57, 50, 1,  0},
> -	{ 58, 51, 1,  0},
> -	{ 59, 52, 1,  0},
> -	{ 60, 53, 1,  0},
> -	{ 61, 54, 1,  0},
> -	{ 62, 55, 1,  0},
> -	{ 63, 56, 1,  0},
> -	{ 64, 57, 1,  0},
> -	{ 65, 58, 1,  0},
> -	{ 66, 40, 1,  1},
> -	{ 67, 41, 1,  1},
> -	{ 68, 42, 1,  1},
> -	{ 69, 43, 1,  1},
> -	{ 70, 44, 1,  1},
> -	{ 71, 45, 1,  1},
> -	{ 72, 46, 1,  1},
> -	{ 73, 47, 1,  1},
> -	{ 74, 48, 1,  1},
> -	{ 75, 49, 1,  1},
> -	{ 76, 50, 1,  1},
> -	{ 77, 51, 1,  1},
> -	{ 78, 52, 1,  1},
> -	{ 79, 53, 1,  1},
> -	{ 80, 54, 1,  1},
> -	{ 81, 55, 1,  1},
> -	{ 82, 56, 1,  1},
> -	{ 83, 57, 1,  1},
> -	{ 84, 58, 1,  1},
> -	{ 85, 59, 1,  1},
> -};
> -
>  /*
>   *   iConfiguration          0
>   *     bInterfaceNumber        0
> @@ -377,13 +52,72 @@ static const struct msi3101_gain msi3101_gain_lut_1000[] = {
>  #define MAX_ISOC_ERRORS         20
>  
>  /* TODO: These should be moved to V4L2 API */
> -#define MSI3101_CID_SAMPLING_MODE         ((V4L2_CID_USER_BASE | 0xf000) + 0)
> -#define MSI3101_CID_SAMPLING_RATE         ((V4L2_CID_USER_BASE | 0xf000) + 1)
> -#define MSI3101_CID_SAMPLING_RESOLUTION   ((V4L2_CID_USER_BASE | 0xf000) + 2)
> -#define MSI3101_CID_TUNER_RF              ((V4L2_CID_USER_BASE | 0xf000) + 10)
> -#define MSI3101_CID_TUNER_BW              ((V4L2_CID_USER_BASE | 0xf000) + 11)
> -#define MSI3101_CID_TUNER_IF              ((V4L2_CID_USER_BASE | 0xf000) + 12)
> -#define MSI3101_CID_TUNER_GAIN            ((V4L2_CID_USER_BASE | 0xf000) + 13)
> +#define V4L2_PIX_FMT_SDR_S8     v4l2_fourcc('D', 'S', '0', '8') /* signed 8-bit */
> +#define V4L2_PIX_FMT_SDR_S12    v4l2_fourcc('D', 'S', '1', '2') /* signed 12-bit */
> +#define V4L2_PIX_FMT_SDR_S14    v4l2_fourcc('D', 'S', '1', '4') /* signed 14-bit */
> +#define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */


Why here? This should be at videodev2.h, and documented at DocBook.

> +
> +static const struct v4l2_frequency_band bands_adc[] = {
> +	{
> +		.tuner = 0,
> +		.type = V4L2_TUNER_ADC,
> +		.index = 0,
> +		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
> +		.rangelow   =  1200000,
> +		.rangehigh  = 15000000,
> +	},
> +};
> +
> +static const struct v4l2_frequency_band bands_rf[] = {
> +	{
> +		.tuner = 1,
> +		.type = V4L2_TUNER_RF,
> +		.index = 0,
> +		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
> +		.rangelow   =   49000000,
> +		.rangehigh  =  263000000,
> +	}, {
> +		.tuner = 1,
> +		.type = V4L2_TUNER_RF,
> +		.index = 1,
> +		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
> +		.rangelow   =  390000000,
> +		.rangehigh  =  960000000,
> +	},
> +};
> +
> +/* stream formats */
> +struct msi3101_format {
> +	char	*name;
> +	u32	pixelformat;
> +};
> +
> +/* format descriptions for capture and preview */
> +static struct msi3101_format formats[] = {
> +	{
> +		.name		= "IQ U8",
> +		.pixelformat	= V4L2_SDR_FMT_CU8,
> +	}, {
> +		.name		= "IQ U16LE",
> +		.pixelformat	=  V4L2_SDR_FMT_CU16LE,
> +#if 0
> +	}, {
> +		.name		= "8-bit signed",
> +		.pixelformat	= V4L2_PIX_FMT_SDR_S8,
> +	}, {
> +		.name		= "10+2-bit signed",
> +		.pixelformat	= V4L2_PIX_FMT_SDR_MSI2500_384,
> +	}, {
> +		.name		= "12-bit signed",
> +		.pixelformat	= V4L2_PIX_FMT_SDR_S12,
> +	}, {
> +		.name		= "14-bit signed",
> +		.pixelformat	= V4L2_PIX_FMT_SDR_S14,
> +#endif
> +	},
> +};
> +
> +static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
>  
>  /* intermediate buffers with raw data from the USB device */
>  struct msi3101_frame_buf {
> @@ -407,24 +141,30 @@ struct msi3101_state {
>  	/* Pointer to our usb_device, will be NULL after unplug */
>  	struct usb_device *udev; /* Both mutexes most be hold when setting! */
>  
> +	unsigned int f_adc, f_tuner;
> +	u32 pixelformat;
> +
>  	unsigned int isoc_errors; /* number of contiguous ISOC errors */
>  	unsigned int vb_full; /* vb is full and packets dropped */
>  
>  	struct urb *urbs[MAX_ISO_BUFS];
> -	int (*convert_stream) (struct msi3101_state *s, u32 *dst, u8 *src,
> +	int (*convert_stream) (struct msi3101_state *s, u8 *dst, u8 *src,
>  			unsigned int src_len);
>  
>  	/* Controls */
> -	struct v4l2_ctrl_handler ctrl_handler;
> -	struct v4l2_ctrl *ctrl_sampling_rate;
> -	struct v4l2_ctrl *ctrl_tuner_rf;
> -	struct v4l2_ctrl *ctrl_tuner_bw;
> -	struct v4l2_ctrl *ctrl_tuner_if;
> -	struct v4l2_ctrl *ctrl_tuner_gain;
> +	struct v4l2_ctrl_handler hdl;
> +	struct v4l2_ctrl *bandwidth_auto;
> +	struct v4l2_ctrl *bandwidth;
> +	struct v4l2_ctrl *lna_gain_auto;
> +	struct v4l2_ctrl *lna_gain;
> +	struct v4l2_ctrl *mixer_gain_auto;
> +	struct v4l2_ctrl *mixer_gain;
> +	struct v4l2_ctrl *if_gain_auto;
> +	struct v4l2_ctrl *if_gain;
>  
>  	u32 next_sample; /* for track lost packets */
>  	u32 sample; /* for sample rate calc */
> -	unsigned long jiffies;
> +	unsigned long jiffies_next;
>  	unsigned int sample_ctrl_bit[4];
>  };
>  
> @@ -448,98 +188,79 @@ leave:
>  
>  /*
>   * +===========================================================================
> - * |   00-1023 | USB packet type '384'
> + * |   00-1023 | USB packet type '504'
>   * +===========================================================================
>   * |   00-  03 | sequence number of first sample in that USB packet
>   * +---------------------------------------------------------------------------
>   * |   04-  15 | garbage
>   * +---------------------------------------------------------------------------
> - * |   16- 175 | samples
> - * +---------------------------------------------------------------------------
> - * |  176- 179 | control bits for previous samples
> - * +---------------------------------------------------------------------------
> - * |  180- 339 | samples
> - * +---------------------------------------------------------------------------
> - * |  340- 343 | control bits for previous samples
> - * +---------------------------------------------------------------------------
> - * |  344- 503 | samples
> - * +---------------------------------------------------------------------------
> - * |  504- 507 | control bits for previous samples
> - * +---------------------------------------------------------------------------
> - * |  508- 667 | samples
> - * +---------------------------------------------------------------------------
> - * |  668- 671 | control bits for previous samples
> - * +---------------------------------------------------------------------------
> - * |  672- 831 | samples
> - * +---------------------------------------------------------------------------
> - * |  832- 835 | control bits for previous samples
> - * +---------------------------------------------------------------------------
> - * |  836- 995 | samples
> - * +---------------------------------------------------------------------------
> - * |  996- 999 | control bits for previous samples
> - * +---------------------------------------------------------------------------
> - * | 1000-1023 | garbage
> + * |   16-1023 | samples
>   * +---------------------------------------------------------------------------
> - *
> - * Bytes 4 - 7 could have some meaning?
> - *
> - * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
> - * numbers. This results one 2-bit number for 8 samples. It is likely used for
> - * for bit shifting sample by given bits, increasing actual sampling resolution.
> - * Number 2 (0b10) was never seen.
> - *
> - * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
> + * signed 8-bit sample
> + * 504 * 2 = 1008 samples
>   */
> +static int msi3101_convert_stream_504(struct msi3101_state *s, u8 *dst,
> +		u8 *src, unsigned int src_len)
> +{
> +	int i, i_max, dst_len = 0;
> +	u32 sample_num[3];
>  
> -/*
> - * Integer to 32-bit IEEE floating point representation routine is taken
> - * from Radeon R600 driver (drivers/gpu/drm/radeon/r600_blit_kms.c).
> - *
> - * TODO: Currently we do conversion here in Kernel, but in future that will
> - * be moved to the libv4l2 library as video format conversions are.
> - */
> -#define I2F_FRAC_BITS  23
> -#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
> +	/* There could be 1-3 1024 bytes URB frames */
> +	i_max = src_len / 1024;
>  
> -/*
> - * Converts signed 8-bit integer into 32-bit IEEE floating point
> - * representation.
> - */
> -static u32 msi3101_convert_sample_504(struct msi3101_state *s, u16 x)
> -{
> -	u32 msb, exponent, fraction, sign;
> +	for (i = 0; i < i_max; i++) {
> +		sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
> +		if (i == 0 && s->next_sample != sample_num[0]) {
> +			dev_dbg_ratelimited(&s->udev->dev,
> +					"%d samples lost, %d %08x:%08x\n",
> +					sample_num[0] - s->next_sample,
> +					src_len, s->next_sample, sample_num[0]);
> +		}
>  
> -	/* Zero is special */
> -	if (!x)
> -		return 0;
> +		/*
> +		 * Dump all unknown 'garbage' data - maybe we will discover
> +		 * someday if there is something rational...
> +		 */
> +		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
>  
> -	/* Negative / positive value */
> -	if (x & (1 << 7)) {
> -		x = -x;
> -		x &= 0x7f; /* result is 7 bit ... + sign */
> -		sign = 1 << 31;
> -	} else {
> -		sign = 0 << 31;
> +		/* 504 x I+Q samples */
> +		src += 16;
> +		memcpy(dst, src, 1008);
> +		src += 1008;
> +		dst += 1008;
> +		dst_len += 1008;
>  	}
>  
> -	/* Get location of the most significant bit */
> -	msb = __fls(x);
> +	/* calculate samping rate and output it in 10 seconds intervals */
> +	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
> +		unsigned long jiffies_now = jiffies;
> +		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
> +		unsigned int samples = sample_num[i_max - 1] - s->sample;
> +		s->jiffies_next = jiffies_now;
> +		s->sample = sample_num[i_max - 1];
> +		dev_dbg(&s->udev->dev,
> +				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
> +				src_len, samples, msecs,
> +				samples * 1000UL / msecs);
> +	}
>  
> -	fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
> -	exponent = (127 + msb) << I2F_FRAC_BITS;
> +	/* next sample (sample = sample + i * 504) */
> +	s->next_sample = sample_num[i_max - 1] + 504;
>  
> -	return (fraction + exponent) | sign;
> +	return dst_len;
>  }
>  
> -static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst,
> +static int msi3101_convert_stream_504_u8(struct msi3101_state *s, u8 *dst,
>  		u8 *src, unsigned int src_len)
>  {
>  	int i, j, i_max, dst_len = 0;
> -	u16 sample[2];
>  	u32 sample_num[3];
> +	s8 *s8src;
> +	u8 *u8dst;
>  
>  	/* There could be 1-3 1024 bytes URB frames */
>  	i_max = src_len / 1024;
> +	u8dst = (u8 *) dst;
>  
>  	for (i = 0; i < i_max; i++) {
>  		sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
> @@ -556,30 +277,28 @@ static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst,
>  		 */
>  		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
>  
> +		/* 504 x I+Q samples */
>  		src += 16;
> -		for (j = 0; j < 1008; j += 2) {
> -			sample[0] = src[j + 0];
> -			sample[1] = src[j + 1];
>  
> -			*dst++ = msi3101_convert_sample_504(s, sample[0]);
> -			*dst++ = msi3101_convert_sample_504(s, sample[1]);
> -		}
> -		/* 504 x I+Q 32bit float samples */
> -		dst_len += 504 * 2 * 4;
> +		s8src = (s8 *) src;
> +		for (j = 0; j < 1008; j++)
> +			*u8dst++ = *s8src++ + 128;
> +
>  		src += 1008;
> +		dst += 1008;
> +		dst_len += 1008;
>  	}
>  
>  	/* calculate samping rate and output it in 10 seconds intervals */
> -	if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
> -		unsigned long jiffies_now = jiffies;
> -		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
> +	if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
> +#define MSECS 10000UL
>  		unsigned int samples = sample_num[i_max - 1] - s->sample;
> -		s->jiffies = jiffies_now;
> +		s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
>  		s->sample = sample_num[i_max - 1];
>  		dev_dbg(&s->udev->dev,
>  				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
> -				src_len, samples, msecs,
> -				samples * 1000UL / msecs);
> +				src_len, samples, MSECS,
> +				samples * 1000UL / MSECS);
>  	}
>  
>  	/* next sample (sample = sample + i * 504) */
> @@ -589,48 +308,53 @@ static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst,
>  }
>  
>  /*
> - * Converts signed ~10+2-bit integer into 32-bit IEEE floating point
> - * representation.
> + * +===========================================================================
> + * |   00-1023 | USB packet type '384'
> + * +===========================================================================
> + * |   00-  03 | sequence number of first sample in that USB packet
> + * +---------------------------------------------------------------------------
> + * |   04-  15 | garbage
> + * +---------------------------------------------------------------------------
> + * |   16- 175 | samples
> + * +---------------------------------------------------------------------------
> + * |  176- 179 | control bits for previous samples
> + * +---------------------------------------------------------------------------
> + * |  180- 339 | samples
> + * +---------------------------------------------------------------------------
> + * |  340- 343 | control bits for previous samples
> + * +---------------------------------------------------------------------------
> + * |  344- 503 | samples
> + * +---------------------------------------------------------------------------
> + * |  504- 507 | control bits for previous samples
> + * +---------------------------------------------------------------------------
> + * |  508- 667 | samples
> + * +---------------------------------------------------------------------------
> + * |  668- 671 | control bits for previous samples
> + * +---------------------------------------------------------------------------
> + * |  672- 831 | samples
> + * +---------------------------------------------------------------------------
> + * |  832- 835 | control bits for previous samples
> + * +---------------------------------------------------------------------------
> + * |  836- 995 | samples
> + * +---------------------------------------------------------------------------
> + * |  996- 999 | control bits for previous samples
> + * +---------------------------------------------------------------------------
> + * | 1000-1023 | garbage
> + * +---------------------------------------------------------------------------
> + *
> + * Bytes 4 - 7 could have some meaning?
> + *
> + * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
> + * numbers. This results one 2-bit number for 8 samples. It is likely used for
> + * for bit shifting sample by given bits, increasing actual sampling resolution.
> + * Number 2 (0b10) was never seen.
> + *
> + * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
>   */
> -static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift)
> -{
> -	u32 msb, exponent, fraction, sign;
> -	s->sample_ctrl_bit[shift]++;
> -
> -	/* Zero is special */
> -	if (!x)
> -		return 0;
> -
> -	if (shift == 3)
> -		shift =	2;
> -
> -	/* Convert 10-bit two's complement to 12-bit */
> -	if (x & (1 << 9)) {
> -		x |= ~0U << 10; /* set all the rest bits to one */
> -		x <<= shift;
> -		x = -x;
> -		x &= 0x7ff; /* result is 11 bit ... + sign */
> -		sign = 1 << 31;
> -	} else {
> -		x <<= shift;
> -		sign = 0 << 31;
> -	}
> -
> -	/* Get location of the most significant bit */
> -	msb = __fls(x);
> -
> -	fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
> -	exponent = (127 + msb) << I2F_FRAC_BITS;
> -
> -	return (fraction + exponent) | sign;
> -}
> -
> -static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
> +static int msi3101_convert_stream_384(struct msi3101_state *s, u8 *dst,
>  		u8 *src, unsigned int src_len)
>  {
> -	int i, j, k, l, i_max, dst_len = 0;
> -	u16 sample[4];
> -	u32 bits;
> +	int i, i_max, dst_len = 0;
>  	u32 sample_num[3];
>  
>  	/* There could be 1-3 1024 bytes URB frames */
> @@ -651,38 +375,20 @@ static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
>  		dev_dbg_ratelimited(&s->udev->dev,
>  				"%*ph  %*ph\n", 12, &src[4], 24, &src[1000]);
>  
> +		/* 384 x I+Q samples */
>  		src += 16;
> -		for (j = 0; j < 6; j++) {
> -			bits = src[160 + 3] << 24 | src[160 + 2] << 16 | src[160 + 1] << 8 | src[160 + 0] << 0;
> -			for (k = 0; k < 16; k++) {
> -				for (l = 0; l < 10; l += 5) {
> -					sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8;
> -					sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6;
> -					sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4;
> -					sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2;
> -
> -					*dst++ = msi3101_convert_sample_384(s, sample[0], (bits >> (2 * k)) & 0x3);
> -					*dst++ = msi3101_convert_sample_384(s, sample[1], (bits >> (2 * k)) & 0x3);
> -					*dst++ = msi3101_convert_sample_384(s, sample[2], (bits >> (2 * k)) & 0x3);
> -					*dst++ = msi3101_convert_sample_384(s, sample[3], (bits >> (2 * k)) & 0x3);
> -				}
> -				src += 10;
> -			}
> -			dev_dbg_ratelimited(&s->udev->dev,
> -					"sample control bits %08x\n", bits);
> -			src += 4;
> -		}
> -		/* 384 x I+Q 32bit float samples */
> -		dst_len += 384 * 2 * 4;
> -		src += 24;
> +		memcpy(dst, src, 984);
> +		src += 984 + 24;
> +		dst += 984;
> +		dst_len += 984;
>  	}
>  
>  	/* calculate samping rate and output it in 10 seconds intervals */
> -	if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
> +	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
>  		unsigned long jiffies_now = jiffies;
> -		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
> +		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
>  		unsigned int samples = sample_num[i_max - 1] - s->sample;
> -		s->jiffies = jiffies_now;
> +		s->jiffies_next = jiffies_now;
>  		s->sample = sample_num[i_max - 1];
>  		dev_dbg(&s->udev->dev,
>  				"slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n",
> @@ -699,40 +405,21 @@ static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
>  }
>  
>  /*
> - * Converts signed 12-bit integer into 32-bit IEEE floating point
> - * representation.
> + * +===========================================================================
> + * |   00-1023 | USB packet type '336'
> + * +===========================================================================
> + * |   00-  03 | sequence number of first sample in that USB packet
> + * +---------------------------------------------------------------------------
> + * |   04-  15 | garbage
> + * +---------------------------------------------------------------------------
> + * |   16-1023 | samples
> + * +---------------------------------------------------------------------------
> + * signed 12-bit sample
>   */
> -static u32 msi3101_convert_sample_336(struct msi3101_state *s, u16 x)
> -{
> -	u32 msb, exponent, fraction, sign;
> -
> -	/* Zero is special */
> -	if (!x)
> -		return 0;
> -
> -	/* Negative / positive value */
> -	if (x & (1 << 11)) {
> -		x = -x;
> -		x &= 0x7ff; /* result is 11 bit ... + sign */
> -		sign = 1 << 31;
> -	} else {
> -		sign = 0 << 31;
> -	}
> -
> -	/* Get location of the most significant bit */
> -	msb = __fls(x);
> -
> -	fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
> -	exponent = (127 + msb) << I2F_FRAC_BITS;
> -
> -	return (fraction + exponent) | sign;
> -}
> -
> -static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst,
> +static int msi3101_convert_stream_336(struct msi3101_state *s, u8 *dst,
>  		u8 *src, unsigned int src_len)
>  {
> -	int i, j, i_max, dst_len = 0;
> -	u16 sample[2];
> +	int i, i_max, dst_len = 0;
>  	u32 sample_num[3];
>  
>  	/* There could be 1-3 1024 bytes URB frames */
> @@ -753,25 +440,20 @@ static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst,
>  		 */
>  		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
>  
> +		/* 336 x I+Q samples */
>  		src += 16;
> -		for (j = 0; j < 1008; j += 3) {
> -			sample[0] = (src[j + 0] & 0xff) >> 0 | (src[j + 1] & 0x0f) << 8;
> -			sample[1] = (src[j + 1] & 0xf0) >> 4 | (src[j + 2] & 0xff) << 4;
> -
> -			*dst++ = msi3101_convert_sample_336(s, sample[0]);
> -			*dst++ = msi3101_convert_sample_336(s, sample[1]);
> -		}
> -		/* 336 x I+Q 32bit float samples */
> -		dst_len += 336 * 2 * 4;
> +		memcpy(dst, src, 1008);
>  		src += 1008;
> +		dst += 1008;
> +		dst_len += 1008;
>  	}
>  
>  	/* calculate samping rate and output it in 10 seconds intervals */
> -	if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
> +	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
>  		unsigned long jiffies_now = jiffies;
> -		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
> +		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
>  		unsigned int samples = sample_num[i_max - 1] - s->sample;
> -		s->jiffies = jiffies_now;
> +		s->jiffies_next = jiffies_now;
>  		s->sample = sample_num[i_max - 1];
>  		dev_dbg(&s->udev->dev,
>  				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
> @@ -786,41 +468,75 @@ static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst,
>  }
>  
>  /*
> - * Converts signed 14-bit integer into 32-bit IEEE floating point
> - * representation.
> + * +===========================================================================
> + * |   00-1023 | USB packet type '252'
> + * +===========================================================================
> + * |   00-  03 | sequence number of first sample in that USB packet
> + * +---------------------------------------------------------------------------
> + * |   04-  15 | garbage
> + * +---------------------------------------------------------------------------
> + * |   16-1023 | samples
> + * +---------------------------------------------------------------------------
> + * signed 14-bit sample
>   */
> -static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x)
> +static int msi3101_convert_stream_252(struct msi3101_state *s, u8 *dst,
> +		u8 *src, unsigned int src_len)
>  {
> -	u32 msb, exponent, fraction, sign;
> +	int i, i_max, dst_len = 0;
> +	u32 sample_num[3];
>  
> -	/* Zero is special */
> -	if (!x)
> -		return 0;
> +	/* There could be 1-3 1024 bytes URB frames */
> +	i_max = src_len / 1024;
>  
> -	/* Negative / positive value */
> -	if (x & (1 << 13)) {
> -		x = -x;
> -		x &= 0x1fff; /* result is 13 bit ... + sign */
> -		sign = 1 << 31;
> -	} else {
> -		sign = 0 << 31;
> +	for (i = 0; i < i_max; i++) {
> +		sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
> +		if (i == 0 && s->next_sample != sample_num[0]) {
> +			dev_dbg_ratelimited(&s->udev->dev,
> +					"%d samples lost, %d %08x:%08x\n",
> +					sample_num[0] - s->next_sample,
> +					src_len, s->next_sample, sample_num[0]);
> +		}
> +
> +		/*
> +		 * Dump all unknown 'garbage' data - maybe we will discover
> +		 * someday if there is something rational...
> +		 */
> +		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
> +
> +		/* 252 x I+Q samples */
> +		src += 16;
> +		memcpy(dst, src, 1008);
> +		src += 1008;
> +		dst += 1008;
> +		dst_len += 1008;
>  	}
>  
> -	/* Get location of the most significant bit */
> -	msb = __fls(x);
> +	/* calculate samping rate and output it in 10 seconds intervals */
> +	if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
> +		unsigned long jiffies_now = jiffies;
> +		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
> +		unsigned int samples = sample_num[i_max - 1] - s->sample;
> +		s->jiffies_next = jiffies_now;
> +		s->sample = sample_num[i_max - 1];
> +		dev_dbg(&s->udev->dev,
> +				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
> +				src_len, samples, msecs,
> +				samples * 1000UL / msecs);
> +	}
>  
> -	fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
> -	exponent = (127 + msb) << I2F_FRAC_BITS;
> +	/* next sample (sample = sample + i * 252) */
> +	s->next_sample = sample_num[i_max - 1] + 252;
>  
> -	return (fraction + exponent) | sign;
> +	return dst_len;
>  }
>  
> -static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst,
> +static int msi3101_convert_stream_252_u16(struct msi3101_state *s, u8 *dst,
>  		u8 *src, unsigned int src_len)
>  {
>  	int i, j, i_max, dst_len = 0;
> -	u16 sample[2];
>  	u32 sample_num[3];
> +	u16 *u16dst = (u16 *) dst;
> +	struct {signed int x:14;} se;
>  
>  	/* There could be 1-3 1024 bytes URB frames */
>  	i_max = src_len / 1024;
> @@ -840,30 +556,44 @@ static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst,
>  		 */
>  		dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
>  
> +		/* 252 x I+Q samples */
>  		src += 16;
> +
>  		for (j = 0; j < 1008; j += 4) {
> -			sample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
> -			sample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
> +			unsigned int usample[2];
> +			int ssample[2];
>  
> -			*dst++ = msi3101_convert_sample_252(s, sample[0]);
> -			*dst++ = msi3101_convert_sample_252(s, sample[1]);
> +			usample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
> +			usample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
> +
> +			/* sign extension from 14-bit to signed int */
> +			ssample[0] = se.x = usample[0];
> +			ssample[1] = se.x = usample[1];
> +
> +			/* from signed to unsigned */
> +			usample[0] = ssample[0] + 8192;
> +			usample[1] = ssample[1] + 8192;
> +
> +			/* from 14-bit to 16-bit */
> +			*u16dst++ = (usample[0] << 2) | (usample[0] >> 12);
> +			*u16dst++ = (usample[1] << 2) | (usample[1] >> 12);
>  		}
> -		/* 252 x I+Q 32bit float samples */
> -		dst_len += 252 * 2 * 4;
> +
>  		src += 1008;
> +		dst += 1008;
> +		dst_len += 1008;
>  	}
>  
>  	/* calculate samping rate and output it in 10 seconds intervals */
> -	if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
> -		unsigned long jiffies_now = jiffies;
> -		unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
> +	if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
> +#define MSECS 10000UL
>  		unsigned int samples = sample_num[i_max - 1] - s->sample;
> -		s->jiffies = jiffies_now;
> +		s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
>  		s->sample = sample_num[i_max - 1];
>  		dev_dbg(&s->udev->dev,
>  				"slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
> -				src_len, samples, msecs,
> -				samples * 1000UL / msecs);
> +				src_len, samples, MSECS,
> +				samples * 1000UL / MSECS);
>  	}
>  
>  	/* next sample (sample = sample + i * 252) */
> @@ -883,14 +613,14 @@ static void msi3101_isoc_handler(struct urb *urb)
>  	unsigned char *iso_buf = NULL;
>  	struct msi3101_frame_buf *fbuf;
>  
> -	if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
> -			urb->status == -ESHUTDOWN) {
> +	if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
> +			urb->status == -ESHUTDOWN)) {
>  		dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
>  				urb, urb->status == -ENOENT ? "" : "a");
>  		return;
>  	}
>  
> -	if (urb->status != 0) {
> +	if (unlikely(urb->status != 0)) {
>  		dev_dbg(&s->udev->dev,
>  				"msi3101_isoc_handler() called with status %d\n",
>  				urb->status);
> @@ -910,28 +640,28 @@ static void msi3101_isoc_handler(struct urb *urb)
>  
>  		/* Check frame error */
>  		fstatus = urb->iso_frame_desc[i].status;
> -		if (fstatus) {
> +		if (unlikely(fstatus)) {
>  			dev_dbg_ratelimited(&s->udev->dev,
>  					"frame=%d/%d has error %d skipping\n",
>  					i, urb->number_of_packets, fstatus);
> -			goto skip;
> +			continue;
>  		}
>  
>  		/* Check if that frame contains data */
>  		flen = urb->iso_frame_desc[i].actual_length;
> -		if (flen == 0)
> -			goto skip;
> +		if (unlikely(flen == 0))
> +			continue;
>  
>  		iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
>  
>  		/* Get free framebuffer */
>  		fbuf = msi3101_get_next_fill_buf(s);
> -		if (fbuf == NULL) {
> +		if (unlikely(fbuf == NULL)) {
>  			s->vb_full++;
>  			dev_dbg_ratelimited(&s->udev->dev,
>  					"videobuf is full, %d packets dropped\n",
>  					s->vb_full);
> -			goto skip;
> +			continue;
>  		}
>  
>  		/* fill framebuffer */
> @@ -939,13 +669,11 @@ static void msi3101_isoc_handler(struct urb *urb)
>  		flen = s->convert_stream(s, ptr, iso_buf, flen);
>  		vb2_set_plane_payload(&fbuf->vb, 0, flen);
>  		vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
> -skip:
> -		;
>  	}
>  
>  handler_end:
>  	i = usb_submit_urb(urb, GFP_ATOMIC);
> -	if (i != 0)
> +	if (unlikely(i != 0))
>  		dev_dbg(&s->udev->dev,
>  				"Error (%d) re-submitting urb in msi3101_isoc_handler\n",
>  				i);
> @@ -1008,7 +736,7 @@ static int msi3101_isoc_init(struct msi3101_state *s)
>  	udev = s->udev;
>  
>  	ret = usb_set_interface(s->udev, 0, 1);
> -	if (ret < 0)
> +	if (ret)
>  		return ret;
>  
>  	/* Allocate and init Isochronuous urbs */
> @@ -1112,14 +840,12 @@ static int msi3101_querycap(struct file *file, void *fh,
>  	strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
>  	strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
>  	usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
> -	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
> -			V4L2_CAP_READWRITE;
> -	cap->device_caps = V4L2_CAP_TUNER;
> +	cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
> +			V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
>  	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
>  	return 0;
>  }
>  
> -
>  /* Videobuf2 operations */
>  static int msi3101_queue_setup(struct vb2_queue *vq,
>  		const struct v4l2_format *fmt, unsigned int *nbuffers,
> @@ -1135,25 +861,14 @@ static int msi3101_queue_setup(struct vb2_queue *vq,
>  	 *   3, wMaxPacketSize 3x 1024 bytes
>  	 * 504, max IQ sample pairs per 1024 frame
>  	 *   2, two samples, I and Q
> -	 *   4, 32-bit float
> +	 *   2, 16-bit is enough for single sample
>  	 */
> -	sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 4); /* = 12096 */
> +	sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 2);
>  	dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
>  			__func__, *nbuffers, sizes[0]);
>  	return 0;
>  }
>  
> -static int msi3101_buf_prepare(struct vb2_buffer *vb)
> -{
> -	struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
> -
> -	/* Don't allow queing new buffers after device disconnection */
> -	if (!s->udev)
> -		return -ENODEV;
> -
> -	return 0;
> -}
> -
>  static void msi3101_buf_queue(struct vb2_buffer *vb)
>  {
>  	struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
> @@ -1162,7 +877,7 @@ static void msi3101_buf_queue(struct vb2_buffer *vb)
>  	unsigned long flags = 0;
>  
>  	/* Check the device has not disconnected between prep and queuing */
> -	if (!s->udev) {
> +	if (unlikely(!s->udev)) {
>  		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
>  		return;
>  	}
> @@ -1221,29 +936,46 @@ static int msi3101_set_usb_adc(struct msi3101_state *s)
>  	int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
>  	u32 reg3, reg4, reg7;
>  
> -	f_sr = s->ctrl_sampling_rate->val64;
> +	f_sr = s->f_adc;
>  
>  	/* select stream format */
> -	if (f_sr < 6000000) {
> -		s->convert_stream = msi3101_convert_stream_252;
> +	switch (s->pixelformat) {
> +	case V4L2_SDR_FMT_CU8:
> +		s->convert_stream = msi3101_convert_stream_504_u8;
> +		reg7 = 0x000c9407;
> +		break;
> +	case  V4L2_SDR_FMT_CU16LE:
> +		s->convert_stream = msi3101_convert_stream_252_u16;
>  		reg7 = 0x00009407;
> -	} else if (f_sr < 8000000) {
> -		s->convert_stream = msi3101_convert_stream_336;
> -		reg7 = 0x00008507;
> -	} else if (f_sr < 9000000) {
> +		break;
> +	case V4L2_PIX_FMT_SDR_S8:
> +		s->convert_stream = msi3101_convert_stream_504;
> +		reg7 = 0x000c9407;
> +		break;
> +	case V4L2_PIX_FMT_SDR_MSI2500_384:
>  		s->convert_stream = msi3101_convert_stream_384;
>  		reg7 = 0x0000a507;
> -	} else {
> -		s->convert_stream = msi3101_convert_stream_504;
> +		break;
> +	case V4L2_PIX_FMT_SDR_S12:
> +		s->convert_stream = msi3101_convert_stream_336;
> +		reg7 = 0x00008507;
> +		break;
> +	case V4L2_PIX_FMT_SDR_S14:
> +		s->convert_stream = msi3101_convert_stream_252;
> +		reg7 = 0x00009407;
> +		break;
> +	default:
> +		s->convert_stream = msi3101_convert_stream_504_u8;
>  		reg7 = 0x000c9407;
> +		break;
>  	}
>  
>  	/*
>  	 * Synthesizer config is just a educated guess...
>  	 *
>  	 * [7:0]   0x03, register address
> -	 * [8]     1, always
> -	 * [9]     ?
> +	 * [8]     1, power control
> +	 * [9]     ?, power control
>  	 * [12:10] output divider
>  	 * [13]    0 ?
>  	 * [14]    0 ?
> @@ -1334,14 +1066,37 @@ err:
>  	return ret;
>  };
>  
> +static int msi3101_set_gain(struct msi3101_state *s)
> +{
> +	int ret;
> +	u32 reg;
> +	dev_dbg(&s->udev->dev, "%s: lna=%d mixer=%d if=%d\n", __func__,
> +			s->lna_gain->val, s->mixer_gain->val, s->if_gain->val);
> +
> +	reg = 1 << 0;
> +	reg |= (59 - s->if_gain->val) << 4;
> +	reg |= 0 << 10;
> +	reg |= (1 - s->mixer_gain->val) << 12;
> +	reg |= (1 - s->lna_gain->val) << 13;
> +	reg |= 4 << 14;
> +	reg |= 0 << 17;
> +	ret = msi3101_tuner_write(s, reg);
> +	if (ret)
> +		goto err;
> +
> +	return 0;
> +err:
> +	dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
> +	return ret;
> +};
> +
>  static int msi3101_set_tuner(struct msi3101_state *s)
>  {
> -	int ret, i, len;
> +	int ret, i;
>  	unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;
>  	u32 reg;
>  	u64 f_vco, tmp64;
>  	u8 mode, filter_mode, lo_div;
> -	const struct msi3101_gain *gain_lut;
>  	static const struct {
>  		u32 rf;
>  		u8 mode;
> @@ -1376,30 +1131,23 @@ static int msi3101_set_tuner(struct msi3101_state *s)
>  		{8000000, 0x07}, /* 8 MHz */
>  	};
>  
> -	unsigned int f_rf = s->ctrl_tuner_rf->val64;
> +	unsigned int f_rf = s->f_tuner;
>  
>  	/*
>  	 * bandwidth (Hz)
>  	 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
>  	 */
> -	unsigned int bandwidth = s->ctrl_tuner_bw->val;
> +	unsigned int bandwidth;
>  
>  	/*
>  	 * intermediate frequency (Hz)
>  	 * 0, 450000, 1620000, 2048000
>  	 */
> -	unsigned int f_if = s->ctrl_tuner_if->val;
> -
> -	/*
> -	 * gain reduction (dB)
> -	 * 0 - 102 below 420 MHz
> -	 * 0 - 85 above 420 MHz
> -	 */
> -	int gain = s->ctrl_tuner_gain->val;
> +	unsigned int f_if = 0;
>  
>  	dev_dbg(&s->udev->dev,
> -			"%s: f_rf=%d bandwidth=%d f_if=%d gain=%d\n",
> -			__func__, f_rf, bandwidth, f_if, gain);
> +			"%s: f_rf=%d f_if=%d\n",
> +			__func__, f_rf, f_if);
>  
>  	ret = -EINVAL;
>  
> @@ -1430,8 +1178,16 @@ static int msi3101_set_tuner(struct msi3101_state *s)
>  	if (i == ARRAY_SIZE(if_freq_lut))
>  		goto err;
>  
> +	/* filters */
> +	if (s->bandwidth_auto->val)
> +		bandwidth = s->f_adc;
> +	else
> +		bandwidth = s->bandwidth->val;
> +
> +	bandwidth = clamp(bandwidth, 200000U, 8000000U);
> +
>  	for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
> -		if (bandwidth == bandwidth_lut[i].freq) {
> +		if (bandwidth <= bandwidth_lut[i].freq) {
>  			bandwidth = bandwidth_lut[i].val;
>  			break;
>  		}
> @@ -1440,6 +1196,11 @@ static int msi3101_set_tuner(struct msi3101_state *s)
>  	if (i == ARRAY_SIZE(bandwidth_lut))
>  		goto err;
>  
> +	s->bandwidth->val = bandwidth_lut[i].freq;
> +
> +	dev_dbg(&s->udev->dev, "%s: bandwidth selected=%d\n",
> +			__func__, bandwidth_lut[i].freq);
> +
>  #define F_OUT_STEP 1
>  #define R_REF 4
>  	f_vco = (f_rf + f_if + f_if1) * lo_div;
> @@ -1504,38 +1265,7 @@ static int msi3101_set_tuner(struct msi3101_state *s)
>  	if (ret)
>  		goto err;
>  
> -	if (f_rf < 120000000) {
> -		gain_lut = msi3101_gain_lut_120;
> -		len = ARRAY_SIZE(msi3101_gain_lut_120);
> -	} else if (f_rf < 245000000) {
> -		gain_lut = msi3101_gain_lut_245;
> -		len = ARRAY_SIZE(msi3101_gain_lut_120);
> -	} else {
> -		gain_lut = msi3101_gain_lut_1000;
> -		len = ARRAY_SIZE(msi3101_gain_lut_1000);
> -	}
> -
> -	for (i = 0; i < len; i++) {
> -		if (gain_lut[i].tot >= gain)
> -			break;
> -	}
> -
> -	if (i == len)
> -		goto err;
> -
> -	dev_dbg(&s->udev->dev,
> -			"%s: gain tot=%d baseband=%d lna=%d mixer=%d\n",
> -			__func__, gain_lut[i].tot, gain_lut[i].baseband,
> -			gain_lut[i].lna, gain_lut[i].mixer);
> -
> -	reg = 1 << 0;
> -	reg |= gain_lut[i].baseband << 4;
> -	reg |= 0 << 10;
> -	reg |= gain_lut[i].mixer << 12;
> -	reg |= gain_lut[i].lna << 13;
> -	reg |= 4 << 14;
> -	reg |= 0 << 17;
> -	ret = msi3101_tuner_write(s, reg);
> +	ret = msi3101_set_gain(s);
>  	if (ret)
>  		goto err;
>  
> @@ -1594,6 +1324,12 @@ static int msi3101_stop_streaming(struct vb2_queue *vq)
>  	msleep(20);
>  	msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0);
>  
> +	/* sleep USB IF / ADC */
> +	msi3101_ctrl_msg(s, CMD_WREG, 0x01000003);
> +
> +	/* sleep tuner */
> +	msi3101_tuner_write(s, 0x000000);
> +
>  	mutex_unlock(&s->v4l2_lock);
>  
>  	return 0;
> @@ -1601,7 +1337,6 @@ static int msi3101_stop_streaming(struct vb2_queue *vq)
>  
>  static struct vb2_ops msi3101_vb2_ops = {
>  	.queue_setup            = msi3101_queue_setup,
> -	.buf_prepare            = msi3101_buf_prepare,
>  	.buf_queue              = msi3101_buf_queue,
>  	.start_streaming        = msi3101_start_streaming,
>  	.stop_streaming         = msi3101_stop_streaming,
> @@ -1609,30 +1344,77 @@ static struct vb2_ops msi3101_vb2_ops = {
>  	.wait_finish            = vb2_ops_wait_finish,
>  };
>  
> -static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i)
> +static int msi3101_enum_fmt_sdr_cap(struct file *file, void *priv,
> +		struct v4l2_fmtdesc *f)
>  {
> -	if (i->index != 0)
> +	struct msi3101_state *s = video_drvdata(file);
> +	dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, f->index);
> +
> +	if (f->index >= NUM_FORMATS)
>  		return -EINVAL;
>  
> -	strlcpy(i->name, "SDR data", sizeof(i->name));
> -	i->type = V4L2_INPUT_TYPE_CAMERA;
> +	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
> +	f->pixelformat = formats[f->index].pixelformat;
> +
> +	return 0;
> +}
> +
> +static int msi3101_g_fmt_sdr_cap(struct file *file, void *priv,
> +		struct v4l2_format *f)
> +{
> +	struct msi3101_state *s = video_drvdata(file);
> +	dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
> +			(char *)&s->pixelformat);
> +
> +	f->fmt.sdr.pixelformat = s->pixelformat;
>  
>  	return 0;
>  }
>  
> -static int msi3101_g_input(struct file *file, void *fh, unsigned int *i)
> +static int msi3101_s_fmt_sdr_cap(struct file *file, void *priv,
> +		struct v4l2_format *f)
>  {
> -	*i = 0;
> +	struct msi3101_state *s = video_drvdata(file);
> +	struct vb2_queue *q = &s->vb_queue;
> +	int i;
> +	dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
> +			(char *)&f->fmt.sdr.pixelformat);
> +
> +	if (vb2_is_busy(q))
> +		return -EBUSY;
> +
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
> +			s->pixelformat = f->fmt.sdr.pixelformat;
> +			return 0;
> +		}
> +	}
> +
> +	f->fmt.sdr.pixelformat = formats[0].pixelformat;
> +	s->pixelformat = formats[0].pixelformat;
>  
>  	return 0;
>  }
>  
> -static int msi3101_s_input(struct file *file, void *fh, unsigned int i)
> +static int msi3101_try_fmt_sdr_cap(struct file *file, void *priv,
> +		struct v4l2_format *f)
>  {
> -	return i ? -EINVAL : 0;
> +	struct msi3101_state *s = video_drvdata(file);
> +	int i;
> +	dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
> +			(char *)&f->fmt.sdr.pixelformat);
> +
> +	for (i = 0; i < NUM_FORMATS; i++) {
> +		if (formats[i].pixelformat == f->fmt.sdr.pixelformat)
> +			return 0;
> +	}
> +
> +	f->fmt.sdr.pixelformat = formats[0].pixelformat;
> +
> +	return 0;
>  }
>  
> -static int vidioc_s_tuner(struct file *file, void *priv,
> +static int msi3101_s_tuner(struct file *file, void *priv,
>  		const struct v4l2_tuner *v)
>  {
>  	struct msi3101_state *s = video_drvdata(file);
> @@ -1641,34 +1423,113 @@ static int vidioc_s_tuner(struct file *file, void *priv,
>  	return 0;
>  }
>  
> -static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
> +static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
>  {
>  	struct msi3101_state *s = video_drvdata(file);
>  	dev_dbg(&s->udev->dev, "%s:\n", __func__);
>  
> -	strcpy(v->name, "SDR RX");
> -	v->capability = V4L2_TUNER_CAP_LOW;
> +	if (v->index == 0) {
> +		strlcpy(v->name, "ADC: Mirics MSi2500", sizeof(v->name));
> +		v->type = V4L2_TUNER_ADC;
> +		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
> +		v->rangelow =   1200000;
> +		v->rangehigh = 15000000;
> +	} else if (v->index == 1) {
> +		strlcpy(v->name, "RF: Mirics MSi001", sizeof(v->name));
> +		v->type = V4L2_TUNER_RF;
> +		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
> +		v->rangelow =    49000000;
> +		v->rangehigh =  960000000;
> +	} else {
> +		return -EINVAL;
> +	}
>  
>  	return 0;
>  }
>  
> -static int vidioc_s_frequency(struct file *file, void *priv,
> +static int msi3101_g_frequency(struct file *file, void *priv,
> +		struct v4l2_frequency *f)
> +{
> +	struct msi3101_state *s = video_drvdata(file);
> +	int ret  = 0;
> +	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n",
> +			__func__, f->tuner, f->type);
> +
> +	if (f->tuner == 0)
> +		f->frequency = s->f_adc;
> +	else if (f->tuner == 1)
> +		f->frequency = s->f_tuner;
> +	else
> +		return -EINVAL;
> +
> +	return ret;
> +}
> +
> +static int msi3101_s_frequency(struct file *file, void *priv,
>  		const struct v4l2_frequency *f)
>  {
>  	struct msi3101_state *s = video_drvdata(file);
> -	dev_dbg(&s->udev->dev, "%s: frequency=%lu Hz (%u)\n",
> -			__func__, f->frequency * 625UL / 10UL, f->frequency);
> +	int ret, band;
> +	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n",
> +			__func__, f->tuner, f->type, f->frequency);
> +
> +	if (f->tuner == 0) {
> +		s->f_adc = clamp_t(unsigned int, f->frequency,
> +				bands_adc[0].rangelow,
> +				bands_adc[0].rangehigh);
> +		dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n",
> +				__func__, s->f_adc);
> +		ret = msi3101_set_usb_adc(s);
> +	} else if (f->tuner == 1) {
> +		#define BAND_RF_0 ((bands_rf[0].rangehigh + bands_rf[1].rangelow) / 2)
> +		if (f->frequency < BAND_RF_0)
> +			band = 0;
> +		else
> +			band = 1;
> +		s->f_tuner = clamp_t(unsigned int, f->frequency,
> +				bands_rf[band].rangelow,
> +				bands_rf[band].rangehigh);
> +		dev_dbg(&s->udev->dev, "%s: RF frequency=%u Hz\n",
> +				__func__, f->frequency);
> +		ret = msi3101_set_tuner(s);
> +	} else {
> +		return -EINVAL;
> +	}
>  
> -	return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf,
> -			f->frequency * 625UL / 10UL);
> +	return ret;
> +}
> +
> +static int msi3101_enum_freq_bands(struct file *file, void *priv,
> +		struct v4l2_frequency_band *band)
> +{
> +	struct msi3101_state *s = video_drvdata(file);
> +	dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n",
> +			__func__, band->tuner, band->type, band->index);
> +
> +	if (band->tuner == 0) {
> +		if (band->index >= ARRAY_SIZE(bands_adc))
> +			return -EINVAL;
> +
> +		*band = bands_adc[band->index];
> +	} else if (band->tuner == 1) {
> +		if (band->index >= ARRAY_SIZE(bands_rf))
> +			return -EINVAL;
> +
> +		*band = bands_rf[band->index];
> +	} else {
> +		return -EINVAL;
> +	}
> +
> +	return 0;
>  }
>  
>  static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
>  	.vidioc_querycap          = msi3101_querycap,
>  
> -	.vidioc_enum_input        = msi3101_enum_input,
> -	.vidioc_g_input           = msi3101_g_input,
> -	.vidioc_s_input           = msi3101_s_input,
> +	.vidioc_enum_fmt_sdr_cap  = msi3101_enum_fmt_sdr_cap,
> +	.vidioc_g_fmt_sdr_cap     = msi3101_g_fmt_sdr_cap,
> +	.vidioc_s_fmt_sdr_cap     = msi3101_s_fmt_sdr_cap,
> +	.vidioc_try_fmt_sdr_cap   = msi3101_try_fmt_sdr_cap,
>  
>  	.vidioc_reqbufs           = vb2_ioctl_reqbufs,
>  	.vidioc_create_bufs       = vb2_ioctl_create_bufs,
> @@ -1680,9 +1541,12 @@ static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
>  	.vidioc_streamon          = vb2_ioctl_streamon,
>  	.vidioc_streamoff         = vb2_ioctl_streamoff,
>  
> -	.vidioc_g_tuner           = vidioc_g_tuner,
> -	.vidioc_s_tuner           = vidioc_s_tuner,
> -	.vidioc_s_frequency       = vidioc_s_frequency,
> +	.vidioc_g_tuner           = msi3101_g_tuner,
> +	.vidioc_s_tuner           = msi3101_s_tuner,
> +
> +	.vidioc_g_frequency       = msi3101_g_frequency,
> +	.vidioc_s_frequency       = msi3101_s_frequency,
> +	.vidioc_enum_freq_bands   = msi3101_enum_freq_bands,
>  
>  	.vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
>  	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
> @@ -1710,7 +1574,7 @@ static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
>  {
>  	struct msi3101_state *s =
>  			container_of(ctrl->handler, struct msi3101_state,
> -					ctrl_handler);
> +					hdl);
>  	int ret;
>  	dev_dbg(&s->udev->dev,
>  			"%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
> @@ -1718,18 +1582,17 @@ static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
>  			ctrl->minimum, ctrl->maximum, ctrl->step);
>  
>  	switch (ctrl->id) {
> -	case MSI3101_CID_SAMPLING_MODE:
> -	case MSI3101_CID_SAMPLING_RATE:
> -	case MSI3101_CID_SAMPLING_RESOLUTION:
> -		ret = 0;
> -		break;
> -	case MSI3101_CID_TUNER_RF:
> -	case MSI3101_CID_TUNER_BW:
> -	case MSI3101_CID_TUNER_IF:
> -	case MSI3101_CID_TUNER_GAIN:
> +	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
> +	case V4L2_CID_RF_TUNER_BANDWIDTH:
>  		ret = msi3101_set_tuner(s);
>  		break;
> +	case  V4L2_CID_RF_TUNER_LNA_GAIN:
> +	case  V4L2_CID_RF_TUNER_MIXER_GAIN:
> +	case  V4L2_CID_RF_TUNER_IF_GAIN:
> +		ret = msi3101_set_gain(s);
> +		break;
>  	default:
> +		dev_dbg(&s->udev->dev, "%s: EINVAL\n", __func__);
>  		ret = -EINVAL;
>  	}
>  
> @@ -1745,7 +1608,7 @@ static void msi3101_video_release(struct v4l2_device *v)
>  	struct msi3101_state *s =
>  			container_of(v, struct msi3101_state, v4l2_dev);
>  
> -	v4l2_ctrl_handler_free(&s->ctrl_handler);
> +	v4l2_ctrl_handler_free(&s->hdl);
>  	v4l2_device_unregister(&s->v4l2_dev);
>  	kfree(s);
>  }
> @@ -1755,81 +1618,8 @@ static int msi3101_probe(struct usb_interface *intf,
>  {
>  	struct usb_device *udev = interface_to_usbdev(intf);
>  	struct msi3101_state *s = NULL;
> +	const struct v4l2_ctrl_ops *ops = &msi3101_ctrl_ops;
>  	int ret;
> -	static const char * const ctrl_sampling_mode_qmenu_strings[] = {
> -		"Quadrature Sampling",
> -		NULL,
> -	};
> -	static const struct v4l2_ctrl_config ctrl_sampling_mode = {
> -		.ops	= &msi3101_ctrl_ops,
> -		.id	= MSI3101_CID_SAMPLING_MODE,
> -		.type   = V4L2_CTRL_TYPE_MENU,
> -		.flags  = V4L2_CTRL_FLAG_INACTIVE,
> -		.name	= "Sampling Mode",
> -		.qmenu  = ctrl_sampling_mode_qmenu_strings,
> -	};
> -	static const struct v4l2_ctrl_config ctrl_sampling_rate = {
> -		.ops	= &msi3101_ctrl_ops,
> -		.id	= MSI3101_CID_SAMPLING_RATE,
> -		.type	= V4L2_CTRL_TYPE_INTEGER64,
> -		.name	= "Sampling Rate",
> -		.min	= 500000,
> -		.max	= 12000000,
> -		.def    = 2048000,
> -		.step	= 1,
> -	};
> -	static const struct v4l2_ctrl_config ctrl_sampling_resolution = {
> -		.ops	= &msi3101_ctrl_ops,
> -		.id	= MSI3101_CID_SAMPLING_RESOLUTION,
> -		.type	= V4L2_CTRL_TYPE_INTEGER,
> -		.flags  = V4L2_CTRL_FLAG_INACTIVE,
> -		.name	= "Sampling Resolution",
> -		.min	= 10,
> -		.max	= 10,
> -		.def    = 10,
> -		.step	= 1,
> -	};
> -	static const struct v4l2_ctrl_config ctrl_tuner_rf = {
> -		.ops	= &msi3101_ctrl_ops,
> -		.id	= MSI3101_CID_TUNER_RF,
> -		.type   = V4L2_CTRL_TYPE_INTEGER64,
> -		.name	= "Tuner RF",
> -		.min	= 40000000,
> -		.max	= 2000000000,
> -		.def    = 100000000,
> -		.step	= 1,
> -	};
> -	static const struct v4l2_ctrl_config ctrl_tuner_bw = {
> -		.ops	= &msi3101_ctrl_ops,
> -		.id	= MSI3101_CID_TUNER_BW,
> -		.type	= V4L2_CTRL_TYPE_INTEGER,
> -		.name	= "Tuner BW",
> -		.min	= 200000,
> -		.max	= 8000000,
> -		.def    = 600000,
> -		.step	= 1,
> -	};
> -	static const struct v4l2_ctrl_config ctrl_tuner_if = {
> -		.ops	= &msi3101_ctrl_ops,
> -		.id	= MSI3101_CID_TUNER_IF,
> -		.type	= V4L2_CTRL_TYPE_INTEGER,
> -		.flags  = V4L2_CTRL_FLAG_INACTIVE,
> -		.name	= "Tuner IF",
> -		.min	= 0,
> -		.max	= 2048000,
> -		.def    = 0,
> -		.step	= 1,
> -	};
> -	static const struct v4l2_ctrl_config ctrl_tuner_gain = {
> -		.ops	= &msi3101_ctrl_ops,
> -		.id	= MSI3101_CID_TUNER_GAIN,
> -		.type	= V4L2_CTRL_TYPE_INTEGER,
> -		.name	= "Tuner Gain",
> -		.min	= 0,
> -		.max	= 102,
> -		.def    = 0,
> -		.step	= 1,
> -	};
>  
>  	s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
>  	if (s == NULL) {
> @@ -1841,11 +1631,12 @@ static int msi3101_probe(struct usb_interface *intf,
>  	mutex_init(&s->vb_queue_lock);
>  	spin_lock_init(&s->queued_bufs_lock);
>  	INIT_LIST_HEAD(&s->queued_bufs);
> -
>  	s->udev = udev;
> +	s->f_adc = bands_adc[0].rangelow;
> +	s->pixelformat = V4L2_SDR_FMT_CU8;
>  
>  	/* Init videobuf2 queue structure */
> -	s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> +	s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
>  	s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
>  	s->vb_queue.drv_priv = s;
>  	s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
> @@ -1853,7 +1644,7 @@ static int msi3101_probe(struct usb_interface *intf,
>  	s->vb_queue.mem_ops = &vb2_vmalloc_memops;
>  	s->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
>  	ret = vb2_queue_init(&s->vb_queue);
> -	if (ret < 0) {
> +	if (ret) {
>  		dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
>  		goto err_free_mem;
>  	}
> @@ -1866,16 +1657,20 @@ static int msi3101_probe(struct usb_interface *intf,
>  	video_set_drvdata(&s->vdev, s);
>  
>  	/* Register controls */
> -	v4l2_ctrl_handler_init(&s->ctrl_handler, 7);
> -	v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_mode, NULL);
> -	s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL);
> -	v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL);
> -	s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL);
> -	s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL);
> -	s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL);
> -	s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL);
> -	if (s->ctrl_handler.error) {
> -		ret = s->ctrl_handler.error;
> +	v4l2_ctrl_handler_init(&s->hdl, 5);
> +	s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, ops,
> +			V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
> +	s->bandwidth = v4l2_ctrl_new_std(&s->hdl, ops,
> +			V4L2_CID_RF_TUNER_BANDWIDTH, 0, 8000000, 1, 0);
> +	v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
> +	s->lna_gain = v4l2_ctrl_new_std(&s->hdl, ops,
> +			V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
> +	s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, ops,
> +			V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
> +	s->if_gain = v4l2_ctrl_new_std(&s->hdl, ops,
> +			V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
> +	if (s->hdl.error) {
> +		ret = s->hdl.error;
>  		dev_err(&s->udev->dev, "Could not initialize controls\n");
>  		goto err_free_controls;
>  	}
> @@ -1889,12 +1684,12 @@ static int msi3101_probe(struct usb_interface *intf,
>  		goto err_free_controls;
>  	}
>  
> -	s->v4l2_dev.ctrl_handler = &s->ctrl_handler;
> +	s->v4l2_dev.ctrl_handler = &s->hdl;
>  	s->vdev.v4l2_dev = &s->v4l2_dev;
>  	s->vdev.lock = &s->v4l2_lock;
>  
> -	ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1);
> -	if (ret < 0) {
> +	ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
> +	if (ret) {
>  		dev_err(&s->udev->dev,
>  				"Failed to register as video device (%d)\n",
>  				ret);
> @@ -1908,7 +1703,7 @@ static int msi3101_probe(struct usb_interface *intf,
>  err_unregister_v4l2_dev:
>  	v4l2_device_unregister(&s->v4l2_dev);
>  err_free_controls:
> -	v4l2_ctrl_handler_free(&s->ctrl_handler);
> +	v4l2_ctrl_handler_free(&s->hdl);
>  err_free_mem:
>  	kfree(s);
>  	return ret;


-- 

Regards,
Mauro

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-03-13 13:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-27  0:25 [REVIEW PATCH 0/6] SDR API - Mirics MSi3101 driver Antti Palosaari
2014-02-27  0:25 ` [REVIEW PATCH 1/6] msi3101: convert to SDR API Antti Palosaari
2014-03-13 13:21   ` Mauro Carvalho Chehab
2014-02-27  0:25 ` [REVIEW PATCH 2/6] msi001: Mirics MSi001 silicon tuner driver Antti Palosaari
2014-02-27  0:25 ` [REVIEW PATCH 3/6] msi3101: use msi001 " Antti Palosaari
2014-02-27  0:25 ` [REVIEW PATCH 4/6] MAINTAINERS: add msi001 driver Antti Palosaari
2014-02-27  0:25 ` [REVIEW PATCH 5/6] MAINTAINERS: add msi3101 driver Antti Palosaari
2014-02-27  0:25 ` [REVIEW PATCH 6/6] msi3101: clamp mmap buffers to reasonable level Antti Palosaari

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.