aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntti Palosaari <crope@iki.fi>2013-11-17 01:04:51 -0500
committerMauro Carvalho Chehab <m.chehab@samsung.com>2014-03-13 09:26:49 -0400
commit3d0c8fa3c5a0f9ffc4c3e8b4625ddeb875aee50b (patch)
tree7f540edbeae8e2d1c9c0278aed4014d30bed04bd
parent9aa4357e9b10b92acb85e30834f8eb4aa7b94554 (diff)
[media] msi3101: convert to SDR API
Massive rewrite. Use SDR API. Fix bugs. Signed-off-by: Antti Palosaari <crope@iki.fi> Acked-by: Hans Verkuil <hans.verkuil@cisco.com> Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
-rw-r--r--drivers/staging/media/msi3101/sdr-msi3101.c1351
1 files changed, 573 insertions, 778 deletions
diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c
index 04ff29e597b8..cf71e7e6c317 100644
--- a/drivers/staging/media/msi3101/sdr-msi3101.c
+++ b/drivers/staging/media/msi3101/sdr-msi3101.c
@@ -21,20 +21,6 @@
21 * (C) 1999-2004 Nemosoft Unv. 21 * (C) 1999-2004 Nemosoft Unv.
22 * (C) 2004-2006 Luc Saillard (luc@saillard.org) 22 * (C) 2004-2006 Luc Saillard (luc@saillard.org)
23 * (C) 2011 Hans de Goede <hdegoede@redhat.com> 23 * (C) 2011 Hans de Goede <hdegoede@redhat.com>
24 *
25 * Development tree of that driver will be on:
26 * http://git.linuxtv.org/anttip/media_tree.git/shortlog/refs/heads/mirics
27 *
28 * GNU Radio plugin "gr-kernel" for device usage will be on:
29 * http://git.linuxtv.org/anttip/gr-kernel.git
30 *
31 * TODO:
32 * Help is very highly welcome for these + all the others you could imagine:
33 * - split USB ADC interface and RF tuner to own drivers (msi2500 and msi001)
34 * - move controls to V4L2 API
35 * - use libv4l2 for stream format conversions
36 * - gr-kernel: switch to v4l2_mmap (current read eats a lot of cpu)
37 * - SDRSharp support
38 */ 24 */
39 25
40#include <linux/module.h> 26#include <linux/module.h>
@@ -48,317 +34,6 @@
48#include <linux/usb.h> 34#include <linux/usb.h>
49#include <media/videobuf2-vmalloc.h> 35#include <media/videobuf2-vmalloc.h>
50 36
51struct msi3101_gain {
52 u8 tot:7;
53 u8 baseband:6;
54 bool lna:1;
55 bool mixer:1;
56};
57
58/* 60 – 120 MHz band, lna 24dB, mixer 19dB */
59static const struct msi3101_gain msi3101_gain_lut_120[] = {
60 { 0, 0, 0, 0},
61 { 1, 1, 0, 0},
62 { 2, 2, 0, 0},
63 { 3, 3, 0, 0},
64 { 4, 4, 0, 0},
65 { 5, 5, 0, 0},
66 { 6, 6, 0, 0},
67 { 7, 7, 0, 0},
68 { 8, 8, 0, 0},
69 { 9, 9, 0, 0},
70 { 10, 10, 0, 0},
71 { 11, 11, 0, 0},
72 { 12, 12, 0, 0},
73 { 13, 13, 0, 0},
74 { 14, 14, 0, 0},
75 { 15, 15, 0, 0},
76 { 16, 16, 0, 0},
77 { 17, 17, 0, 0},
78 { 18, 18, 0, 0},
79 { 19, 19, 0, 0},
80 { 20, 20, 0, 0},
81 { 21, 21, 0, 0},
82 { 22, 22, 0, 0},
83 { 23, 23, 0, 0},
84 { 24, 24, 0, 0},
85 { 25, 25, 0, 0},
86 { 26, 26, 0, 0},
87 { 27, 27, 0, 0},
88 { 28, 28, 0, 0},
89 { 29, 5, 1, 0},
90 { 30, 6, 1, 0},
91 { 31, 7, 1, 0},
92 { 32, 8, 1, 0},
93 { 33, 9, 1, 0},
94 { 34, 10, 1, 0},
95 { 35, 11, 1, 0},
96 { 36, 12, 1, 0},
97 { 37, 13, 1, 0},
98 { 38, 14, 1, 0},
99 { 39, 15, 1, 0},
100 { 40, 16, 1, 0},
101 { 41, 17, 1, 0},
102 { 42, 18, 1, 0},
103 { 43, 19, 1, 0},
104 { 44, 20, 1, 0},
105 { 45, 21, 1, 0},
106 { 46, 22, 1, 0},
107 { 47, 23, 1, 0},
108 { 48, 24, 1, 0},
109 { 49, 25, 1, 0},
110 { 50, 26, 1, 0},
111 { 51, 27, 1, 0},
112 { 52, 28, 1, 0},
113 { 53, 29, 1, 0},
114 { 54, 30, 1, 0},
115 { 55, 31, 1, 0},
116 { 56, 32, 1, 0},
117 { 57, 33, 1, 0},
118 { 58, 34, 1, 0},
119 { 59, 35, 1, 0},
120 { 60, 36, 1, 0},
121 { 61, 37, 1, 0},
122 { 62, 38, 1, 0},
123 { 63, 39, 1, 0},
124 { 64, 40, 1, 0},
125 { 65, 41, 1, 0},
126 { 66, 42, 1, 0},
127 { 67, 43, 1, 0},
128 { 68, 44, 1, 0},
129 { 69, 45, 1, 0},
130 { 70, 46, 1, 0},
131 { 71, 47, 1, 0},
132 { 72, 48, 1, 0},
133 { 73, 49, 1, 0},
134 { 74, 50, 1, 0},
135 { 75, 51, 1, 0},
136 { 76, 52, 1, 0},
137 { 77, 53, 1, 0},
138 { 78, 54, 1, 0},
139 { 79, 55, 1, 0},
140 { 80, 56, 1, 0},
141 { 81, 57, 1, 0},
142 { 82, 58, 1, 0},
143 { 83, 40, 1, 1},
144 { 84, 41, 1, 1},
145 { 85, 42, 1, 1},
146 { 86, 43, 1, 1},
147 { 87, 44, 1, 1},
148 { 88, 45, 1, 1},
149 { 89, 46, 1, 1},
150 { 90, 47, 1, 1},
151 { 91, 48, 1, 1},
152 { 92, 49, 1, 1},
153 { 93, 50, 1, 1},
154 { 94, 51, 1, 1},
155 { 95, 52, 1, 1},
156 { 96, 53, 1, 1},
157 { 97, 54, 1, 1},
158 { 98, 55, 1, 1},
159 { 99, 56, 1, 1},
160 {100, 57, 1, 1},
161 {101, 58, 1, 1},
162 {102, 59, 1, 1},
163};
164
165/* 120 – 245 MHz band, lna 24dB, mixer 19dB */
166static const struct msi3101_gain msi3101_gain_lut_245[] = {
167 { 0, 0, 0, 0},
168 { 1, 1, 0, 0},
169 { 2, 2, 0, 0},
170 { 3, 3, 0, 0},
171 { 4, 4, 0, 0},
172 { 5, 5, 0, 0},
173 { 6, 6, 0, 0},
174 { 7, 7, 0, 0},
175 { 8, 8, 0, 0},
176 { 9, 9, 0, 0},
177 { 10, 10, 0, 0},
178 { 11, 11, 0, 0},
179 { 12, 12, 0, 0},
180 { 13, 13, 0, 0},
181 { 14, 14, 0, 0},
182 { 15, 15, 0, 0},
183 { 16, 16, 0, 0},
184 { 17, 17, 0, 0},
185 { 18, 18, 0, 0},
186 { 19, 19, 0, 0},
187 { 20, 20, 0, 0},
188 { 21, 21, 0, 0},
189 { 22, 22, 0, 0},
190 { 23, 23, 0, 0},
191 { 24, 24, 0, 0},
192 { 25, 25, 0, 0},
193 { 26, 26, 0, 0},
194 { 27, 27, 0, 0},
195 { 28, 28, 0, 0},
196 { 29, 5, 1, 0},
197 { 30, 6, 1, 0},
198 { 31, 7, 1, 0},
199 { 32, 8, 1, 0},
200 { 33, 9, 1, 0},
201 { 34, 10, 1, 0},
202 { 35, 11, 1, 0},
203 { 36, 12, 1, 0},
204 { 37, 13, 1, 0},
205 { 38, 14, 1, 0},
206 { 39, 15, 1, 0},
207 { 40, 16, 1, 0},
208 { 41, 17, 1, 0},
209 { 42, 18, 1, 0},
210 { 43, 19, 1, 0},
211 { 44, 20, 1, 0},
212 { 45, 21, 1, 0},
213 { 46, 22, 1, 0},
214 { 47, 23, 1, 0},
215 { 48, 24, 1, 0},
216 { 49, 25, 1, 0},
217 { 50, 26, 1, 0},
218 { 51, 27, 1, 0},
219 { 52, 28, 1, 0},
220 { 53, 29, 1, 0},
221 { 54, 30, 1, 0},
222 { 55, 31, 1, 0},
223 { 56, 32, 1, 0},
224 { 57, 33, 1, 0},
225 { 58, 34, 1, 0},
226 { 59, 35, 1, 0},
227 { 60, 36, 1, 0},
228 { 61, 37, 1, 0},
229 { 62, 38, 1, 0},
230 { 63, 39, 1, 0},
231 { 64, 40, 1, 0},
232 { 65, 41, 1, 0},
233 { 66, 42, 1, 0},
234 { 67, 43, 1, 0},
235 { 68, 44, 1, 0},
236 { 69, 45, 1, 0},
237 { 70, 46, 1, 0},
238 { 71, 47, 1, 0},
239 { 72, 48, 1, 0},
240 { 73, 49, 1, 0},
241 { 74, 50, 1, 0},
242 { 75, 51, 1, 0},
243 { 76, 52, 1, 0},
244 { 77, 53, 1, 0},
245 { 78, 54, 1, 0},
246 { 79, 55, 1, 0},
247 { 80, 56, 1, 0},
248 { 81, 57, 1, 0},
249 { 82, 58, 1, 0},
250 { 83, 40, 1, 1},
251 { 84, 41, 1, 1},
252 { 85, 42, 1, 1},
253 { 86, 43, 1, 1},
254 { 87, 44, 1, 1},
255 { 88, 45, 1, 1},
256 { 89, 46, 1, 1},
257 { 90, 47, 1, 1},
258 { 91, 48, 1, 1},
259 { 92, 49, 1, 1},
260 { 93, 50, 1, 1},
261 { 94, 51, 1, 1},
262 { 95, 52, 1, 1},
263 { 96, 53, 1, 1},
264 { 97, 54, 1, 1},
265 { 98, 55, 1, 1},
266 { 99, 56, 1, 1},
267 {100, 57, 1, 1},
268 {101, 58, 1, 1},
269 {102, 59, 1, 1},
270};
271
272/* 420 – 1000 MHz band, lna 7dB, mixer 19dB */
273static const struct msi3101_gain msi3101_gain_lut_1000[] = {
274 { 0, 0, 0, 0},
275 { 1, 1, 0, 0},
276 { 2, 2, 0, 0},
277 { 3, 3, 0, 0},
278 { 4, 4, 0, 0},
279 { 5, 5, 0, 0},
280 { 6, 6, 0, 0},
281 { 7, 7, 0, 0},
282 { 8, 8, 0, 0},
283 { 9, 9, 0, 0},
284 { 10, 10, 0, 0},
285 { 11, 11, 0, 0},
286 { 12, 5, 1, 0},
287 { 13, 6, 1, 0},
288 { 14, 7, 1, 0},
289 { 15, 8, 1, 0},
290 { 16, 9, 1, 0},
291 { 17, 10, 1, 0},
292 { 18, 11, 1, 0},
293 { 19, 12, 1, 0},
294 { 20, 13, 1, 0},
295 { 21, 14, 1, 0},
296 { 22, 15, 1, 0},
297 { 23, 16, 1, 0},
298 { 24, 17, 1, 0},
299 { 25, 18, 1, 0},
300 { 26, 19, 1, 0},
301 { 27, 20, 1, 0},
302 { 28, 21, 1, 0},
303 { 29, 22, 1, 0},
304 { 30, 23, 1, 0},
305 { 31, 24, 1, 0},
306 { 32, 25, 1, 0},
307 { 33, 26, 1, 0},
308 { 34, 27, 1, 0},
309 { 35, 28, 1, 0},
310 { 36, 29, 1, 0},
311 { 37, 30, 1, 0},
312 { 38, 31, 1, 0},
313 { 39, 32, 1, 0},
314 { 40, 33, 1, 0},
315 { 41, 34, 1, 0},
316 { 42, 35, 1, 0},
317 { 43, 36, 1, 0},
318 { 44, 37, 1, 0},
319 { 45, 38, 1, 0},
320 { 46, 39, 1, 0},
321 { 47, 40, 1, 0},
322 { 48, 41, 1, 0},
323 { 49, 42, 1, 0},
324 { 50, 43, 1, 0},
325 { 51, 44, 1, 0},
326 { 52, 45, 1, 0},
327 { 53, 46, 1, 0},
328 { 54, 47, 1, 0},
329 { 55, 48, 1, 0},
330 { 56, 49, 1, 0},
331 { 57, 50, 1, 0},
332 { 58, 51, 1, 0},
333 { 59, 52, 1, 0},
334 { 60, 53, 1, 0},
335 { 61, 54, 1, 0},
336 { 62, 55, 1, 0},
337 { 63, 56, 1, 0},
338 { 64, 57, 1, 0},
339 { 65, 58, 1, 0},
340 { 66, 40, 1, 1},
341 { 67, 41, 1, 1},
342 { 68, 42, 1, 1},
343 { 69, 43, 1, 1},
344 { 70, 44, 1, 1},
345 { 71, 45, 1, 1},
346 { 72, 46, 1, 1},
347 { 73, 47, 1, 1},
348 { 74, 48, 1, 1},
349 { 75, 49, 1, 1},
350 { 76, 50, 1, 1},
351 { 77, 51, 1, 1},
352 { 78, 52, 1, 1},
353 { 79, 53, 1, 1},
354 { 80, 54, 1, 1},
355 { 81, 55, 1, 1},
356 { 82, 56, 1, 1},
357 { 83, 57, 1, 1},
358 { 84, 58, 1, 1},
359 { 85, 59, 1, 1},
360};
361
362/* 37/*
363 * iConfiguration 0 38 * iConfiguration 0
364 * bInterfaceNumber 0 39 * bInterfaceNumber 0
@@ -377,13 +52,72 @@ static const struct msi3101_gain msi3101_gain_lut_1000[] = {
377#define MAX_ISOC_ERRORS 20 52#define MAX_ISOC_ERRORS 20
378 53
379/* TODO: These should be moved to V4L2 API */ 54/* TODO: These should be moved to V4L2 API */
380#define MSI3101_CID_SAMPLING_MODE ((V4L2_CID_USER_BASE | 0xf000) + 0) 55#define V4L2_PIX_FMT_SDR_S8 v4l2_fourcc('D', 'S', '0', '8') /* signed 8-bit */
381#define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 1) 56#define V4L2_PIX_FMT_SDR_S12 v4l2_fourcc('D', 'S', '1', '2') /* signed 12-bit */
382#define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 2) 57#define V4L2_PIX_FMT_SDR_S14 v4l2_fourcc('D', 'S', '1', '4') /* signed 14-bit */
383#define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10) 58#define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */
384#define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11) 59
385#define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12) 60static const struct v4l2_frequency_band bands_adc[] = {
386#define MSI3101_CID_TUNER_GAIN ((V4L2_CID_USER_BASE | 0xf000) + 13) 61 {
62 .tuner = 0,
63 .type = V4L2_TUNER_ADC,
64 .index = 0,
65 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
66 .rangelow = 1200000,
67 .rangehigh = 15000000,
68 },
69};
70
71static const struct v4l2_frequency_band bands_rf[] = {
72 {
73 .tuner = 1,
74 .type = V4L2_TUNER_RF,
75 .index = 0,
76 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
77 .rangelow = 49000000,
78 .rangehigh = 263000000,
79 }, {
80 .tuner = 1,
81 .type = V4L2_TUNER_RF,
82 .index = 1,
83 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
84 .rangelow = 390000000,
85 .rangehigh = 960000000,
86 },
87};
88
89/* stream formats */
90struct msi3101_format {
91 char *name;
92 u32 pixelformat;
93};
94
95/* format descriptions for capture and preview */
96static struct msi3101_format formats[] = {
97 {
98 .name = "IQ U8",
99 .pixelformat = V4L2_SDR_FMT_CU8,
100 }, {
101 .name = "IQ U16LE",
102 .pixelformat = V4L2_SDR_FMT_CU16LE,
103#if 0
104 }, {
105 .name = "8-bit signed",
106 .pixelformat = V4L2_PIX_FMT_SDR_S8,
107 }, {
108 .name = "10+2-bit signed",
109 .pixelformat = V4L2_PIX_FMT_SDR_MSI2500_384,
110 }, {
111 .name = "12-bit signed",
112 .pixelformat = V4L2_PIX_FMT_SDR_S12,
113 }, {
114 .name = "14-bit signed",
115 .pixelformat = V4L2_PIX_FMT_SDR_S14,
116#endif
117 },
118};
119
120static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
387 121
388/* intermediate buffers with raw data from the USB device */ 122/* intermediate buffers with raw data from the USB device */
389struct msi3101_frame_buf { 123struct msi3101_frame_buf {
@@ -407,24 +141,30 @@ struct msi3101_state {
407 /* Pointer to our usb_device, will be NULL after unplug */ 141 /* Pointer to our usb_device, will be NULL after unplug */
408 struct usb_device *udev; /* Both mutexes most be hold when setting! */ 142 struct usb_device *udev; /* Both mutexes most be hold when setting! */
409 143
144 unsigned int f_adc, f_tuner;
145 u32 pixelformat;
146
410 unsigned int isoc_errors; /* number of contiguous ISOC errors */ 147 unsigned int isoc_errors; /* number of contiguous ISOC errors */
411 unsigned int vb_full; /* vb is full and packets dropped */ 148 unsigned int vb_full; /* vb is full and packets dropped */
412 149
413 struct urb *urbs[MAX_ISO_BUFS]; 150 struct urb *urbs[MAX_ISO_BUFS];
414 int (*convert_stream) (struct msi3101_state *s, u32 *dst, u8 *src, 151 int (*convert_stream) (struct msi3101_state *s, u8 *dst, u8 *src,
415 unsigned int src_len); 152 unsigned int src_len);
416 153
417 /* Controls */ 154 /* Controls */
418 struct v4l2_ctrl_handler ctrl_handler; 155 struct v4l2_ctrl_handler hdl;
419 struct v4l2_ctrl *ctrl_sampling_rate; 156 struct v4l2_ctrl *bandwidth_auto;
420 struct v4l2_ctrl *ctrl_tuner_rf; 157 struct v4l2_ctrl *bandwidth;
421 struct v4l2_ctrl *ctrl_tuner_bw; 158 struct v4l2_ctrl *lna_gain_auto;
422 struct v4l2_ctrl *ctrl_tuner_if; 159 struct v4l2_ctrl *lna_gain;
423 struct v4l2_ctrl *ctrl_tuner_gain; 160 struct v4l2_ctrl *mixer_gain_auto;
161 struct v4l2_ctrl *mixer_gain;
162 struct v4l2_ctrl *if_gain_auto;
163 struct v4l2_ctrl *if_gain;
424 164
425 u32 next_sample; /* for track lost packets */ 165 u32 next_sample; /* for track lost packets */
426 u32 sample; /* for sample rate calc */ 166 u32 sample; /* for sample rate calc */
427 unsigned long jiffies; 167 unsigned long jiffies_next;
428 unsigned int sample_ctrl_bit[4]; 168 unsigned int sample_ctrl_bit[4];
429}; 169};
430 170
@@ -448,98 +188,79 @@ leave:
448 188
449/* 189/*
450 * +=========================================================================== 190 * +===========================================================================
451 * | 00-1023 | USB packet type '384' 191 * | 00-1023 | USB packet type '504'
452 * +=========================================================================== 192 * +===========================================================================
453 * | 00- 03 | sequence number of first sample in that USB packet 193 * | 00- 03 | sequence number of first sample in that USB packet
454 * +--------------------------------------------------------------------------- 194 * +---------------------------------------------------------------------------
455 * | 04- 15 | garbage 195 * | 04- 15 | garbage
456 * +--------------------------------------------------------------------------- 196 * +---------------------------------------------------------------------------
457 * | 16- 175 | samples 197 * | 16-1023 | samples
458 * +---------------------------------------------------------------------------
459 * | 176- 179 | control bits for previous samples
460 * +---------------------------------------------------------------------------
461 * | 180- 339 | samples
462 * +---------------------------------------------------------------------------
463 * | 340- 343 | control bits for previous samples
464 * +---------------------------------------------------------------------------
465 * | 344- 503 | samples
466 * +---------------------------------------------------------------------------
467 * | 504- 507 | control bits for previous samples
468 * +---------------------------------------------------------------------------
469 * | 508- 667 | samples
470 * +---------------------------------------------------------------------------
471 * | 668- 671 | control bits for previous samples
472 * +---------------------------------------------------------------------------
473 * | 672- 831 | samples
474 * +---------------------------------------------------------------------------
475 * | 832- 835 | control bits for previous samples
476 * +---------------------------------------------------------------------------
477 * | 836- 995 | samples
478 * +---------------------------------------------------------------------------
479 * | 996- 999 | control bits for previous samples
480 * +---------------------------------------------------------------------------
481 * | 1000-1023 | garbage
482 * +--------------------------------------------------------------------------- 198 * +---------------------------------------------------------------------------
483 * 199 * signed 8-bit sample
484 * Bytes 4 - 7 could have some meaning? 200 * 504 * 2 = 1008 samples
485 *
486 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
487 * numbers. This results one 2-bit number for 8 samples. It is likely used for
488 * for bit shifting sample by given bits, increasing actual sampling resolution.
489 * Number 2 (0b10) was never seen.
490 *
491 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
492 */ 201 */
202static int msi3101_convert_stream_504(struct msi3101_state *s, u8 *dst,
203 u8 *src, unsigned int src_len)
204{
205 int i, i_max, dst_len = 0;
206 u32 sample_num[3];
493 207
494/* 208 /* There could be 1-3 1024 bytes URB frames */
495 * Integer to 32-bit IEEE floating point representation routine is taken 209 i_max = src_len / 1024;
496 * from Radeon R600 driver (drivers/gpu/drm/radeon/r600_blit_kms.c).
497 *
498 * TODO: Currently we do conversion here in Kernel, but in future that will
499 * be moved to the libv4l2 library as video format conversions are.
500 */
501#define I2F_FRAC_BITS 23
502#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
503 210
504/* 211 for (i = 0; i < i_max; i++) {
505 * Converts signed 8-bit integer into 32-bit IEEE floating point 212 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
506 * representation. 213 if (i == 0 && s->next_sample != sample_num[0]) {
507 */ 214 dev_dbg_ratelimited(&s->udev->dev,
508static u32 msi3101_convert_sample_504(struct msi3101_state *s, u16 x) 215 "%d samples lost, %d %08x:%08x\n",
509{ 216 sample_num[0] - s->next_sample,
510 u32 msb, exponent, fraction, sign; 217 src_len, s->next_sample, sample_num[0]);
218 }
511 219
512 /* Zero is special */ 220 /*
513 if (!x) 221 * Dump all unknown 'garbage' data - maybe we will discover
514 return 0; 222 * someday if there is something rational...
223 */
224 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
515 225
516 /* Negative / positive value */ 226 /* 504 x I+Q samples */
517 if (x & (1 << 7)) { 227 src += 16;
518 x = -x; 228 memcpy(dst, src, 1008);
519 x &= 0x7f; /* result is 7 bit ... + sign */ 229 src += 1008;
520 sign = 1 << 31; 230 dst += 1008;
521 } else { 231 dst_len += 1008;
522 sign = 0 << 31;
523 } 232 }
524 233
525 /* Get location of the most significant bit */ 234 /* calculate samping rate and output it in 10 seconds intervals */
526 msb = __fls(x); 235 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
236 unsigned long jiffies_now = jiffies;
237 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
238 unsigned int samples = sample_num[i_max - 1] - s->sample;
239 s->jiffies_next = jiffies_now;
240 s->sample = sample_num[i_max - 1];
241 dev_dbg(&s->udev->dev,
242 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
243 src_len, samples, msecs,
244 samples * 1000UL / msecs);
245 }
527 246
528 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; 247 /* next sample (sample = sample + i * 504) */
529 exponent = (127 + msb) << I2F_FRAC_BITS; 248 s->next_sample = sample_num[i_max - 1] + 504;
530 249
531 return (fraction + exponent) | sign; 250 return dst_len;
532} 251}
533 252
534static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst, 253static int msi3101_convert_stream_504_u8(struct msi3101_state *s, u8 *dst,
535 u8 *src, unsigned int src_len) 254 u8 *src, unsigned int src_len)
536{ 255{
537 int i, j, i_max, dst_len = 0; 256 int i, j, i_max, dst_len = 0;
538 u16 sample[2];
539 u32 sample_num[3]; 257 u32 sample_num[3];
258 s8 *s8src;
259 u8 *u8dst;
540 260
541 /* There could be 1-3 1024 bytes URB frames */ 261 /* There could be 1-3 1024 bytes URB frames */
542 i_max = src_len / 1024; 262 i_max = src_len / 1024;
263 u8dst = (u8 *) dst;
543 264
544 for (i = 0; i < i_max; i++) { 265 for (i = 0; i < i_max; i++) {
545 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; 266 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,
556 */ 277 */
557 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); 278 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
558 279
280 /* 504 x I+Q samples */
559 src += 16; 281 src += 16;
560 for (j = 0; j < 1008; j += 2) {
561 sample[0] = src[j + 0];
562 sample[1] = src[j + 1];
563 282
564 *dst++ = msi3101_convert_sample_504(s, sample[0]); 283 s8src = (s8 *) src;
565 *dst++ = msi3101_convert_sample_504(s, sample[1]); 284 for (j = 0; j < 1008; j++)
566 } 285 *u8dst++ = *s8src++ + 128;
567 /* 504 x I+Q 32bit float samples */ 286
568 dst_len += 504 * 2 * 4;
569 src += 1008; 287 src += 1008;
288 dst += 1008;
289 dst_len += 1008;
570 } 290 }
571 291
572 /* calculate samping rate and output it in 10 seconds intervals */ 292 /* calculate samping rate and output it in 10 seconds intervals */
573 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { 293 if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
574 unsigned long jiffies_now = jiffies; 294#define MSECS 10000UL
575 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
576 unsigned int samples = sample_num[i_max - 1] - s->sample; 295 unsigned int samples = sample_num[i_max - 1] - s->sample;
577 s->jiffies = jiffies_now; 296 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
578 s->sample = sample_num[i_max - 1]; 297 s->sample = sample_num[i_max - 1];
579 dev_dbg(&s->udev->dev, 298 dev_dbg(&s->udev->dev,
580 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", 299 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
581 src_len, samples, msecs, 300 src_len, samples, MSECS,
582 samples * 1000UL / msecs); 301 samples * 1000UL / MSECS);
583 } 302 }
584 303
585 /* next sample (sample = sample + i * 504) */ 304 /* next sample (sample = sample + i * 504) */
@@ -589,48 +308,53 @@ static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst,
589} 308}
590 309
591/* 310/*
592 * Converts signed ~10+2-bit integer into 32-bit IEEE floating point 311 * +===========================================================================
593 * representation. 312 * | 00-1023 | USB packet type '384'
313 * +===========================================================================
314 * | 00- 03 | sequence number of first sample in that USB packet
315 * +---------------------------------------------------------------------------
316 * | 04- 15 | garbage
317 * +---------------------------------------------------------------------------
318 * | 16- 175 | samples
319 * +---------------------------------------------------------------------------
320 * | 176- 179 | control bits for previous samples
321 * +---------------------------------------------------------------------------
322 * | 180- 339 | samples
323 * +---------------------------------------------------------------------------
324 * | 340- 343 | control bits for previous samples
325 * +---------------------------------------------------------------------------
326 * | 344- 503 | samples
327 * +---------------------------------------------------------------------------
328 * | 504- 507 | control bits for previous samples
329 * +---------------------------------------------------------------------------
330 * | 508- 667 | samples
331 * +---------------------------------------------------------------------------
332 * | 668- 671 | control bits for previous samples
333 * +---------------------------------------------------------------------------
334 * | 672- 831 | samples
335 * +---------------------------------------------------------------------------
336 * | 832- 835 | control bits for previous samples
337 * +---------------------------------------------------------------------------
338 * | 836- 995 | samples
339 * +---------------------------------------------------------------------------
340 * | 996- 999 | control bits for previous samples
341 * +---------------------------------------------------------------------------
342 * | 1000-1023 | garbage
343 * +---------------------------------------------------------------------------
344 *
345 * Bytes 4 - 7 could have some meaning?
346 *
347 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
348 * numbers. This results one 2-bit number for 8 samples. It is likely used for
349 * for bit shifting sample by given bits, increasing actual sampling resolution.
350 * Number 2 (0b10) was never seen.
351 *
352 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
594 */ 353 */
595static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift) 354static int msi3101_convert_stream_384(struct msi3101_state *s, u8 *dst,
596{
597 u32 msb, exponent, fraction, sign;
598 s->sample_ctrl_bit[shift]++;
599
600 /* Zero is special */
601 if (!x)
602 return 0;
603
604 if (shift == 3)
605 shift = 2;
606
607 /* Convert 10-bit two's complement to 12-bit */
608 if (x & (1 << 9)) {
609 x |= ~0U << 10; /* set all the rest bits to one */
610 x <<= shift;
611 x = -x;
612 x &= 0x7ff; /* result is 11 bit ... + sign */
613 sign = 1 << 31;
614 } else {
615 x <<= shift;
616 sign = 0 << 31;
617 }
618
619 /* Get location of the most significant bit */
620 msb = __fls(x);
621
622 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
623 exponent = (127 + msb) << I2F_FRAC_BITS;
624
625 return (fraction + exponent) | sign;
626}
627
628static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
629 u8 *src, unsigned int src_len) 355 u8 *src, unsigned int src_len)
630{ 356{
631 int i, j, k, l, i_max, dst_len = 0; 357 int i, i_max, dst_len = 0;
632 u16 sample[4];
633 u32 bits;
634 u32 sample_num[3]; 358 u32 sample_num[3];
635 359
636 /* There could be 1-3 1024 bytes URB frames */ 360 /* 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,
651 dev_dbg_ratelimited(&s->udev->dev, 375 dev_dbg_ratelimited(&s->udev->dev,
652 "%*ph %*ph\n", 12, &src[4], 24, &src[1000]); 376 "%*ph %*ph\n", 12, &src[4], 24, &src[1000]);
653 377
378 /* 384 x I+Q samples */
654 src += 16; 379 src += 16;
655 for (j = 0; j < 6; j++) { 380 memcpy(dst, src, 984);
656 bits = src[160 + 3] << 24 | src[160 + 2] << 16 | src[160 + 1] << 8 | src[160 + 0] << 0; 381 src += 984 + 24;
657 for (k = 0; k < 16; k++) { 382 dst += 984;
658 for (l = 0; l < 10; l += 5) { 383 dst_len += 984;
659 sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8;
660 sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6;
661 sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4;
662 sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2;
663
664 *dst++ = msi3101_convert_sample_384(s, sample[0], (bits >> (2 * k)) & 0x3);
665 *dst++ = msi3101_convert_sample_384(s, sample[1], (bits >> (2 * k)) & 0x3);
666 *dst++ = msi3101_convert_sample_384(s, sample[2], (bits >> (2 * k)) & 0x3);
667 *dst++ = msi3101_convert_sample_384(s, sample[3], (bits >> (2 * k)) & 0x3);
668 }
669 src += 10;
670 }
671 dev_dbg_ratelimited(&s->udev->dev,
672 "sample control bits %08x\n", bits);
673 src += 4;
674 }
675 /* 384 x I+Q 32bit float samples */
676 dst_len += 384 * 2 * 4;
677 src += 24;
678 } 384 }
679 385
680 /* calculate samping rate and output it in 10 seconds intervals */ 386 /* calculate samping rate and output it in 10 seconds intervals */
681 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { 387 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
682 unsigned long jiffies_now = jiffies; 388 unsigned long jiffies_now = jiffies;
683 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); 389 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
684 unsigned int samples = sample_num[i_max - 1] - s->sample; 390 unsigned int samples = sample_num[i_max - 1] - s->sample;
685 s->jiffies = jiffies_now; 391 s->jiffies_next = jiffies_now;
686 s->sample = sample_num[i_max - 1]; 392 s->sample = sample_num[i_max - 1];
687 dev_dbg(&s->udev->dev, 393 dev_dbg(&s->udev->dev,
688 "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n", 394 "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,
699} 405}
700 406
701/* 407/*
702 * Converts signed 12-bit integer into 32-bit IEEE floating point 408 * +===========================================================================
703 * representation. 409 * | 00-1023 | USB packet type '336'
410 * +===========================================================================
411 * | 00- 03 | sequence number of first sample in that USB packet
412 * +---------------------------------------------------------------------------
413 * | 04- 15 | garbage
414 * +---------------------------------------------------------------------------
415 * | 16-1023 | samples
416 * +---------------------------------------------------------------------------
417 * signed 12-bit sample
704 */ 418 */
705static u32 msi3101_convert_sample_336(struct msi3101_state *s, u16 x) 419static int msi3101_convert_stream_336(struct msi3101_state *s, u8 *dst,
706{
707 u32 msb, exponent, fraction, sign;
708
709 /* Zero is special */
710 if (!x)
711 return 0;
712
713 /* Negative / positive value */
714 if (x & (1 << 11)) {
715 x = -x;
716 x &= 0x7ff; /* result is 11 bit ... + sign */
717 sign = 1 << 31;
718 } else {
719 sign = 0 << 31;
720 }
721
722 /* Get location of the most significant bit */
723 msb = __fls(x);
724
725 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
726 exponent = (127 + msb) << I2F_FRAC_BITS;
727
728 return (fraction + exponent) | sign;
729}
730
731static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst,
732 u8 *src, unsigned int src_len) 420 u8 *src, unsigned int src_len)
733{ 421{
734 int i, j, i_max, dst_len = 0; 422 int i, i_max, dst_len = 0;
735 u16 sample[2];
736 u32 sample_num[3]; 423 u32 sample_num[3];
737 424
738 /* There could be 1-3 1024 bytes URB frames */ 425 /* 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,
753 */ 440 */
754 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); 441 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
755 442
443 /* 336 x I+Q samples */
756 src += 16; 444 src += 16;
757 for (j = 0; j < 1008; j += 3) { 445 memcpy(dst, src, 1008);
758 sample[0] = (src[j + 0] & 0xff) >> 0 | (src[j + 1] & 0x0f) << 8;
759 sample[1] = (src[j + 1] & 0xf0) >> 4 | (src[j + 2] & 0xff) << 4;
760
761 *dst++ = msi3101_convert_sample_336(s, sample[0]);
762 *dst++ = msi3101_convert_sample_336(s, sample[1]);
763 }
764 /* 336 x I+Q 32bit float samples */
765 dst_len += 336 * 2 * 4;
766 src += 1008; 446 src += 1008;
447 dst += 1008;
448 dst_len += 1008;
767 } 449 }
768 450
769 /* calculate samping rate and output it in 10 seconds intervals */ 451 /* calculate samping rate and output it in 10 seconds intervals */
770 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { 452 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
771 unsigned long jiffies_now = jiffies; 453 unsigned long jiffies_now = jiffies;
772 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); 454 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
773 unsigned int samples = sample_num[i_max - 1] - s->sample; 455 unsigned int samples = sample_num[i_max - 1] - s->sample;
774 s->jiffies = jiffies_now; 456 s->jiffies_next = jiffies_now;
775 s->sample = sample_num[i_max - 1]; 457 s->sample = sample_num[i_max - 1];
776 dev_dbg(&s->udev->dev, 458 dev_dbg(&s->udev->dev,
777 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", 459 "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,
786} 468}
787 469
788/* 470/*
789 * Converts signed 14-bit integer into 32-bit IEEE floating point 471 * +===========================================================================
790 * representation. 472 * | 00-1023 | USB packet type '252'
473 * +===========================================================================
474 * | 00- 03 | sequence number of first sample in that USB packet
475 * +---------------------------------------------------------------------------
476 * | 04- 15 | garbage
477 * +---------------------------------------------------------------------------
478 * | 16-1023 | samples
479 * +---------------------------------------------------------------------------
480 * signed 14-bit sample
791 */ 481 */
792static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x) 482static int msi3101_convert_stream_252(struct msi3101_state *s, u8 *dst,
483 u8 *src, unsigned int src_len)
793{ 484{
794 u32 msb, exponent, fraction, sign; 485 int i, i_max, dst_len = 0;
486 u32 sample_num[3];
795 487
796 /* Zero is special */ 488 /* There could be 1-3 1024 bytes URB frames */
797 if (!x) 489 i_max = src_len / 1024;
798 return 0;
799 490
800 /* Negative / positive value */ 491 for (i = 0; i < i_max; i++) {
801 if (x & (1 << 13)) { 492 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
802 x = -x; 493 if (i == 0 && s->next_sample != sample_num[0]) {
803 x &= 0x1fff; /* result is 13 bit ... + sign */ 494 dev_dbg_ratelimited(&s->udev->dev,
804 sign = 1 << 31; 495 "%d samples lost, %d %08x:%08x\n",
805 } else { 496 sample_num[0] - s->next_sample,
806 sign = 0 << 31; 497 src_len, s->next_sample, sample_num[0]);
498 }
499
500 /*
501 * Dump all unknown 'garbage' data - maybe we will discover
502 * someday if there is something rational...
503 */
504 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
505
506 /* 252 x I+Q samples */
507 src += 16;
508 memcpy(dst, src, 1008);
509 src += 1008;
510 dst += 1008;
511 dst_len += 1008;
807 } 512 }
808 513
809 /* Get location of the most significant bit */ 514 /* calculate samping rate and output it in 10 seconds intervals */
810 msb = __fls(x); 515 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
516 unsigned long jiffies_now = jiffies;
517 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
518 unsigned int samples = sample_num[i_max - 1] - s->sample;
519 s->jiffies_next = jiffies_now;
520 s->sample = sample_num[i_max - 1];
521 dev_dbg(&s->udev->dev,
522 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
523 src_len, samples, msecs,
524 samples * 1000UL / msecs);
525 }
811 526
812 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; 527 /* next sample (sample = sample + i * 252) */
813 exponent = (127 + msb) << I2F_FRAC_BITS; 528 s->next_sample = sample_num[i_max - 1] + 252;
814 529
815 return (fraction + exponent) | sign; 530 return dst_len;
816} 531}
817 532
818static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst, 533static int msi3101_convert_stream_252_u16(struct msi3101_state *s, u8 *dst,
819 u8 *src, unsigned int src_len) 534 u8 *src, unsigned int src_len)
820{ 535{
821 int i, j, i_max, dst_len = 0; 536 int i, j, i_max, dst_len = 0;
822 u16 sample[2];
823 u32 sample_num[3]; 537 u32 sample_num[3];
538 u16 *u16dst = (u16 *) dst;
539 struct {signed int x:14;} se;
824 540
825 /* There could be 1-3 1024 bytes URB frames */ 541 /* There could be 1-3 1024 bytes URB frames */
826 i_max = src_len / 1024; 542 i_max = src_len / 1024;
@@ -840,30 +556,44 @@ static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst,
840 */ 556 */
841 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); 557 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
842 558
559 /* 252 x I+Q samples */
843 src += 16; 560 src += 16;
561
844 for (j = 0; j < 1008; j += 4) { 562 for (j = 0; j < 1008; j += 4) {
845 sample[0] = src[j + 0] >> 0 | src[j + 1] << 8; 563 unsigned int usample[2];
846 sample[1] = src[j + 2] >> 0 | src[j + 3] << 8; 564 int ssample[2];
847 565
848 *dst++ = msi3101_convert_sample_252(s, sample[0]); 566 usample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
849 *dst++ = msi3101_convert_sample_252(s, sample[1]); 567 usample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
568
569 /* sign extension from 14-bit to signed int */
570 ssample[0] = se.x = usample[0];
571 ssample[1] = se.x = usample[1];
572
573 /* from signed to unsigned */
574 usample[0] = ssample[0] + 8192;
575 usample[1] = ssample[1] + 8192;
576
577 /* from 14-bit to 16-bit */
578 *u16dst++ = (usample[0] << 2) | (usample[0] >> 12);
579 *u16dst++ = (usample[1] << 2) | (usample[1] >> 12);
850 } 580 }
851 /* 252 x I+Q 32bit float samples */ 581
852 dst_len += 252 * 2 * 4;
853 src += 1008; 582 src += 1008;
583 dst += 1008;
584 dst_len += 1008;
854 } 585 }
855 586
856 /* calculate samping rate and output it in 10 seconds intervals */ 587 /* calculate samping rate and output it in 10 seconds intervals */
857 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { 588 if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
858 unsigned long jiffies_now = jiffies; 589#define MSECS 10000UL
859 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
860 unsigned int samples = sample_num[i_max - 1] - s->sample; 590 unsigned int samples = sample_num[i_max - 1] - s->sample;
861 s->jiffies = jiffies_now; 591 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
862 s->sample = sample_num[i_max - 1]; 592 s->sample = sample_num[i_max - 1];
863 dev_dbg(&s->udev->dev, 593 dev_dbg(&s->udev->dev,
864 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", 594 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
865 src_len, samples, msecs, 595 src_len, samples, MSECS,
866 samples * 1000UL / msecs); 596 samples * 1000UL / MSECS);
867 } 597 }
868 598
869 /* next sample (sample = sample + i * 252) */ 599 /* next sample (sample = sample + i * 252) */
@@ -883,14 +613,14 @@ static void msi3101_isoc_handler(struct urb *urb)
883 unsigned char *iso_buf = NULL; 613 unsigned char *iso_buf = NULL;
884 struct msi3101_frame_buf *fbuf; 614 struct msi3101_frame_buf *fbuf;
885 615
886 if (urb->status == -ENOENT || urb->status == -ECONNRESET || 616 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
887 urb->status == -ESHUTDOWN) { 617 urb->status == -ESHUTDOWN)) {
888 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n", 618 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
889 urb, urb->status == -ENOENT ? "" : "a"); 619 urb, urb->status == -ENOENT ? "" : "a");
890 return; 620 return;
891 } 621 }
892 622
893 if (urb->status != 0) { 623 if (unlikely(urb->status != 0)) {
894 dev_dbg(&s->udev->dev, 624 dev_dbg(&s->udev->dev,
895 "msi3101_isoc_handler() called with status %d\n", 625 "msi3101_isoc_handler() called with status %d\n",
896 urb->status); 626 urb->status);
@@ -910,28 +640,28 @@ static void msi3101_isoc_handler(struct urb *urb)
910 640
911 /* Check frame error */ 641 /* Check frame error */
912 fstatus = urb->iso_frame_desc[i].status; 642 fstatus = urb->iso_frame_desc[i].status;
913 if (fstatus) { 643 if (unlikely(fstatus)) {
914 dev_dbg_ratelimited(&s->udev->dev, 644 dev_dbg_ratelimited(&s->udev->dev,
915 "frame=%d/%d has error %d skipping\n", 645 "frame=%d/%d has error %d skipping\n",
916 i, urb->number_of_packets, fstatus); 646 i, urb->number_of_packets, fstatus);
917 goto skip; 647 continue;
918 } 648 }
919 649
920 /* Check if that frame contains data */ 650 /* Check if that frame contains data */
921 flen = urb->iso_frame_desc[i].actual_length; 651 flen = urb->iso_frame_desc[i].actual_length;
922 if (flen == 0) 652 if (unlikely(flen == 0))
923 goto skip; 653 continue;
924 654
925 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 655 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
926 656
927 /* Get free framebuffer */ 657 /* Get free framebuffer */
928 fbuf = msi3101_get_next_fill_buf(s); 658 fbuf = msi3101_get_next_fill_buf(s);
929 if (fbuf == NULL) { 659 if (unlikely(fbuf == NULL)) {
930 s->vb_full++; 660 s->vb_full++;
931 dev_dbg_ratelimited(&s->udev->dev, 661 dev_dbg_ratelimited(&s->udev->dev,
932 "videobuf is full, %d packets dropped\n", 662 "videobuf is full, %d packets dropped\n",
933 s->vb_full); 663 s->vb_full);
934 goto skip; 664 continue;
935 } 665 }
936 666
937 /* fill framebuffer */ 667 /* fill framebuffer */
@@ -939,13 +669,11 @@ static void msi3101_isoc_handler(struct urb *urb)
939 flen = s->convert_stream(s, ptr, iso_buf, flen); 669 flen = s->convert_stream(s, ptr, iso_buf, flen);
940 vb2_set_plane_payload(&fbuf->vb, 0, flen); 670 vb2_set_plane_payload(&fbuf->vb, 0, flen);
941 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE); 671 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
942skip:
943 ;
944 } 672 }
945 673
946handler_end: 674handler_end:
947 i = usb_submit_urb(urb, GFP_ATOMIC); 675 i = usb_submit_urb(urb, GFP_ATOMIC);
948 if (i != 0) 676 if (unlikely(i != 0))
949 dev_dbg(&s->udev->dev, 677 dev_dbg(&s->udev->dev,
950 "Error (%d) re-submitting urb in msi3101_isoc_handler\n", 678 "Error (%d) re-submitting urb in msi3101_isoc_handler\n",
951 i); 679 i);
@@ -1008,7 +736,7 @@ static int msi3101_isoc_init(struct msi3101_state *s)
1008 udev = s->udev; 736 udev = s->udev;
1009 737
1010 ret = usb_set_interface(s->udev, 0, 1); 738 ret = usb_set_interface(s->udev, 0, 1);
1011 if (ret < 0) 739 if (ret)
1012 return ret; 740 return ret;
1013 741
1014 /* Allocate and init Isochronuous urbs */ 742 /* Allocate and init Isochronuous urbs */
@@ -1112,14 +840,12 @@ static int msi3101_querycap(struct file *file, void *fh,
1112 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); 840 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
1113 strlcpy(cap->card, s->vdev.name, sizeof(cap->card)); 841 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
1114 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); 842 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
1115 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | 843 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
1116 V4L2_CAP_READWRITE; 844 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
1117 cap->device_caps = V4L2_CAP_TUNER;
1118 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 845 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1119 return 0; 846 return 0;
1120} 847}
1121 848
1122
1123/* Videobuf2 operations */ 849/* Videobuf2 operations */
1124static int msi3101_queue_setup(struct vb2_queue *vq, 850static int msi3101_queue_setup(struct vb2_queue *vq,
1125 const struct v4l2_format *fmt, unsigned int *nbuffers, 851 const struct v4l2_format *fmt, unsigned int *nbuffers,
@@ -1135,25 +861,14 @@ static int msi3101_queue_setup(struct vb2_queue *vq,
1135 * 3, wMaxPacketSize 3x 1024 bytes 861 * 3, wMaxPacketSize 3x 1024 bytes
1136 * 504, max IQ sample pairs per 1024 frame 862 * 504, max IQ sample pairs per 1024 frame
1137 * 2, two samples, I and Q 863 * 2, two samples, I and Q
1138 * 4, 32-bit float 864 * 2, 16-bit is enough for single sample
1139 */ 865 */
1140 sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 4); /* = 12096 */ 866 sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 2);
1141 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n", 867 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
1142 __func__, *nbuffers, sizes[0]); 868 __func__, *nbuffers, sizes[0]);
1143 return 0; 869 return 0;
1144} 870}
1145 871
1146static int msi3101_buf_prepare(struct vb2_buffer *vb)
1147{
1148 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
1149
1150 /* Don't allow queing new buffers after device disconnection */
1151 if (!s->udev)
1152 return -ENODEV;
1153
1154 return 0;
1155}
1156
1157static void msi3101_buf_queue(struct vb2_buffer *vb) 872static void msi3101_buf_queue(struct vb2_buffer *vb)
1158{ 873{
1159 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); 874 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
@@ -1162,7 +877,7 @@ static void msi3101_buf_queue(struct vb2_buffer *vb)
1162 unsigned long flags = 0; 877 unsigned long flags = 0;
1163 878
1164 /* Check the device has not disconnected between prep and queuing */ 879 /* Check the device has not disconnected between prep and queuing */
1165 if (!s->udev) { 880 if (unlikely(!s->udev)) {
1166 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); 881 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1167 return; 882 return;
1168 } 883 }
@@ -1221,29 +936,46 @@ static int msi3101_set_usb_adc(struct msi3101_state *s)
1221 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract; 936 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
1222 u32 reg3, reg4, reg7; 937 u32 reg3, reg4, reg7;
1223 938
1224 f_sr = s->ctrl_sampling_rate->val64; 939 f_sr = s->f_adc;
1225 940
1226 /* select stream format */ 941 /* select stream format */
1227 if (f_sr < 6000000) { 942 switch (s->pixelformat) {
1228 s->convert_stream = msi3101_convert_stream_252; 943 case V4L2_SDR_FMT_CU8:
944 s->convert_stream = msi3101_convert_stream_504_u8;
945 reg7 = 0x000c9407;
946 break;
947 case V4L2_SDR_FMT_CU16LE:
948 s->convert_stream = msi3101_convert_stream_252_u16;
1229 reg7 = 0x00009407; 949 reg7 = 0x00009407;
1230 } else if (f_sr < 8000000) { 950 break;
1231 s->convert_stream = msi3101_convert_stream_336; 951 case V4L2_PIX_FMT_SDR_S8:
1232 reg7 = 0x00008507; 952 s->convert_stream = msi3101_convert_stream_504;
1233 } else if (f_sr < 9000000) { 953 reg7 = 0x000c9407;
954 break;
955 case V4L2_PIX_FMT_SDR_MSI2500_384:
1234 s->convert_stream = msi3101_convert_stream_384; 956 s->convert_stream = msi3101_convert_stream_384;
1235 reg7 = 0x0000a507; 957 reg7 = 0x0000a507;
1236 } else { 958 break;
1237 s->convert_stream = msi3101_convert_stream_504; 959 case V4L2_PIX_FMT_SDR_S12:
960 s->convert_stream = msi3101_convert_stream_336;
961 reg7 = 0x00008507;
962 break;
963 case V4L2_PIX_FMT_SDR_S14:
964 s->convert_stream = msi3101_convert_stream_252;
965 reg7 = 0x00009407;
966 break;
967 default:
968 s->convert_stream = msi3101_convert_stream_504_u8;
1238 reg7 = 0x000c9407; 969 reg7 = 0x000c9407;
970 break;
1239 } 971 }
1240 972
1241 /* 973 /*
1242 * Synthesizer config is just a educated guess... 974 * Synthesizer config is just a educated guess...
1243 * 975 *
1244 * [7:0] 0x03, register address 976 * [7:0] 0x03, register address
1245 * [8] 1, always 977 * [8] 1, power control
1246 * [9] ? 978 * [9] ?, power control
1247 * [12:10] output divider 979 * [12:10] output divider
1248 * [13] 0 ? 980 * [13] 0 ?
1249 * [14] 0 ? 981 * [14] 0 ?
@@ -1334,14 +1066,37 @@ err:
1334 return ret; 1066 return ret;
1335}; 1067};
1336 1068
1069static int msi3101_set_gain(struct msi3101_state *s)
1070{
1071 int ret;
1072 u32 reg;
1073 dev_dbg(&s->udev->dev, "%s: lna=%d mixer=%d if=%d\n", __func__,
1074 s->lna_gain->val, s->mixer_gain->val, s->if_gain->val);
1075
1076 reg = 1 << 0;
1077 reg |= (59 - s->if_gain->val) << 4;
1078 reg |= 0 << 10;
1079 reg |= (1 - s->mixer_gain->val) << 12;
1080 reg |= (1 - s->lna_gain->val) << 13;
1081 reg |= 4 << 14;
1082 reg |= 0 << 17;
1083 ret = msi3101_tuner_write(s, reg);
1084 if (ret)
1085 goto err;
1086
1087 return 0;
1088err:
1089 dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
1090 return ret;
1091};
1092
1337static int msi3101_set_tuner(struct msi3101_state *s) 1093static int msi3101_set_tuner(struct msi3101_state *s)
1338{ 1094{
1339 int ret, i, len; 1095 int ret, i;
1340 unsigned int n, m, thresh, frac, vco_step, tmp, f_if1; 1096 unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;
1341 u32 reg; 1097 u32 reg;
1342 u64 f_vco, tmp64; 1098 u64 f_vco, tmp64;
1343 u8 mode, filter_mode, lo_div; 1099 u8 mode, filter_mode, lo_div;
1344 const struct msi3101_gain *gain_lut;
1345 static const struct { 1100 static const struct {
1346 u32 rf; 1101 u32 rf;
1347 u8 mode; 1102 u8 mode;
@@ -1376,30 +1131,23 @@ static int msi3101_set_tuner(struct msi3101_state *s)
1376 {8000000, 0x07}, /* 8 MHz */ 1131 {8000000, 0x07}, /* 8 MHz */
1377 }; 1132 };
1378 1133
1379 unsigned int f_rf = s->ctrl_tuner_rf->val64; 1134 unsigned int f_rf = s->f_tuner;
1380 1135
1381 /* 1136 /*
1382 * bandwidth (Hz) 1137 * bandwidth (Hz)
1383 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000 1138 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
1384 */ 1139 */
1385 unsigned int bandwidth = s->ctrl_tuner_bw->val; 1140 unsigned int bandwidth;
1386 1141
1387 /* 1142 /*
1388 * intermediate frequency (Hz) 1143 * intermediate frequency (Hz)
1389 * 0, 450000, 1620000, 2048000 1144 * 0, 450000, 1620000, 2048000
1390 */ 1145 */
1391 unsigned int f_if = s->ctrl_tuner_if->val; 1146 unsigned int f_if = 0;
1392
1393 /*
1394 * gain reduction (dB)
1395 * 0 - 102 below 420 MHz
1396 * 0 - 85 above 420 MHz
1397 */
1398 int gain = s->ctrl_tuner_gain->val;
1399 1147
1400 dev_dbg(&s->udev->dev, 1148 dev_dbg(&s->udev->dev,
1401 "%s: f_rf=%d bandwidth=%d f_if=%d gain=%d\n", 1149 "%s: f_rf=%d f_if=%d\n",
1402 __func__, f_rf, bandwidth, f_if, gain); 1150 __func__, f_rf, f_if);
1403 1151
1404 ret = -EINVAL; 1152 ret = -EINVAL;
1405 1153
@@ -1430,8 +1178,16 @@ static int msi3101_set_tuner(struct msi3101_state *s)
1430 if (i == ARRAY_SIZE(if_freq_lut)) 1178 if (i == ARRAY_SIZE(if_freq_lut))
1431 goto err; 1179 goto err;
1432 1180
1181 /* filters */
1182 if (s->bandwidth_auto->val)
1183 bandwidth = s->f_adc;
1184 else
1185 bandwidth = s->bandwidth->val;
1186
1187 bandwidth = clamp(bandwidth, 200000U, 8000000U);
1188
1433 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) { 1189 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
1434 if (bandwidth == bandwidth_lut[i].freq) { 1190 if (bandwidth <= bandwidth_lut[i].freq) {
1435 bandwidth = bandwidth_lut[i].val; 1191 bandwidth = bandwidth_lut[i].val;
1436 break; 1192 break;
1437 } 1193 }
@@ -1440,6 +1196,11 @@ static int msi3101_set_tuner(struct msi3101_state *s)
1440 if (i == ARRAY_SIZE(bandwidth_lut)) 1196 if (i == ARRAY_SIZE(bandwidth_lut))
1441 goto err; 1197 goto err;
1442 1198
1199 s->bandwidth->val = bandwidth_lut[i].freq;
1200
1201 dev_dbg(&s->udev->dev, "%s: bandwidth selected=%d\n",
1202 __func__, bandwidth_lut[i].freq);
1203
1443#define F_OUT_STEP 1 1204#define F_OUT_STEP 1
1444#define R_REF 4 1205#define R_REF 4
1445 f_vco = (f_rf + f_if + f_if1) * lo_div; 1206 f_vco = (f_rf + f_if + f_if1) * lo_div;
@@ -1504,38 +1265,7 @@ static int msi3101_set_tuner(struct msi3101_state *s)
1504 if (ret) 1265 if (ret)
1505 goto err; 1266 goto err;
1506 1267
1507 if (f_rf < 120000000) { 1268 ret = msi3101_set_gain(s);
1508 gain_lut = msi3101_gain_lut_120;
1509 len = ARRAY_SIZE(msi3101_gain_lut_120);
1510 } else if (f_rf < 245000000) {
1511 gain_lut = msi3101_gain_lut_245;
1512 len = ARRAY_SIZE(msi3101_gain_lut_120);
1513 } else {
1514 gain_lut = msi3101_gain_lut_1000;
1515 len = ARRAY_SIZE(msi3101_gain_lut_1000);
1516 }
1517
1518 for (i = 0; i < len; i++) {
1519 if (gain_lut[i].tot >= gain)
1520 break;
1521 }
1522
1523 if (i == len)
1524 goto err;
1525
1526 dev_dbg(&s->udev->dev,
1527 "%s: gain tot=%d baseband=%d lna=%d mixer=%d\n",
1528 __func__, gain_lut[i].tot, gain_lut[i].baseband,
1529 gain_lut[i].lna, gain_lut[i].mixer);
1530
1531 reg = 1 << 0;
1532 reg |= gain_lut[i].baseband << 4;
1533 reg |= 0 << 10;
1534 reg |= gain_lut[i].mixer << 12;
1535 reg |= gain_lut[i].lna << 13;
1536 reg |= 4 << 14;
1537 reg |= 0 << 17;
1538 ret = msi3101_tuner_write(s, reg);
1539 if (ret) 1269 if (ret)
1540 goto err; 1270 goto err;
1541 1271
@@ -1594,6 +1324,12 @@ static int msi3101_stop_streaming(struct vb2_queue *vq)
1594 msleep(20); 1324 msleep(20);
1595 msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0); 1325 msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0);
1596 1326
1327 /* sleep USB IF / ADC */
1328 msi3101_ctrl_msg(s, CMD_WREG, 0x01000003);
1329
1330 /* sleep tuner */
1331 msi3101_tuner_write(s, 0x000000);
1332
1597 mutex_unlock(&s->v4l2_lock); 1333 mutex_unlock(&s->v4l2_lock);
1598 1334
1599 return 0; 1335 return 0;
@@ -1601,7 +1337,6 @@ static int msi3101_stop_streaming(struct vb2_queue *vq)
1601 1337
1602static struct vb2_ops msi3101_vb2_ops = { 1338static struct vb2_ops msi3101_vb2_ops = {
1603 .queue_setup = msi3101_queue_setup, 1339 .queue_setup = msi3101_queue_setup,
1604 .buf_prepare = msi3101_buf_prepare,
1605 .buf_queue = msi3101_buf_queue, 1340 .buf_queue = msi3101_buf_queue,
1606 .start_streaming = msi3101_start_streaming, 1341 .start_streaming = msi3101_start_streaming,
1607 .stop_streaming = msi3101_stop_streaming, 1342 .stop_streaming = msi3101_stop_streaming,
@@ -1609,30 +1344,77 @@ static struct vb2_ops msi3101_vb2_ops = {
1609 .wait_finish = vb2_ops_wait_finish, 1344 .wait_finish = vb2_ops_wait_finish,
1610}; 1345};
1611 1346
1612static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i) 1347static int msi3101_enum_fmt_sdr_cap(struct file *file, void *priv,
1348 struct v4l2_fmtdesc *f)
1613{ 1349{
1614 if (i->index != 0) 1350 struct msi3101_state *s = video_drvdata(file);
1351 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, f->index);
1352
1353 if (f->index >= NUM_FORMATS)
1615 return -EINVAL; 1354 return -EINVAL;
1616 1355
1617 strlcpy(i->name, "SDR data", sizeof(i->name)); 1356 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1618 i->type = V4L2_INPUT_TYPE_CAMERA; 1357 f->pixelformat = formats[f->index].pixelformat;
1358
1359 return 0;
1360}
1361
1362static int msi3101_g_fmt_sdr_cap(struct file *file, void *priv,
1363 struct v4l2_format *f)
1364{
1365 struct msi3101_state *s = video_drvdata(file);
1366 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1367 (char *)&s->pixelformat);
1368
1369 f->fmt.sdr.pixelformat = s->pixelformat;
1619 1370
1620 return 0; 1371 return 0;
1621} 1372}
1622 1373
1623static int msi3101_g_input(struct file *file, void *fh, unsigned int *i) 1374static int msi3101_s_fmt_sdr_cap(struct file *file, void *priv,
1375 struct v4l2_format *f)
1624{ 1376{
1625 *i = 0; 1377 struct msi3101_state *s = video_drvdata(file);
1378 struct vb2_queue *q = &s->vb_queue;
1379 int i;
1380 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1381 (char *)&f->fmt.sdr.pixelformat);
1382
1383 if (vb2_is_busy(q))
1384 return -EBUSY;
1385
1386 for (i = 0; i < NUM_FORMATS; i++) {
1387 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1388 s->pixelformat = f->fmt.sdr.pixelformat;
1389 return 0;
1390 }
1391 }
1392
1393 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1394 s->pixelformat = formats[0].pixelformat;
1626 1395
1627 return 0; 1396 return 0;
1628} 1397}
1629 1398
1630static int msi3101_s_input(struct file *file, void *fh, unsigned int i) 1399static int msi3101_try_fmt_sdr_cap(struct file *file, void *priv,
1400 struct v4l2_format *f)
1631{ 1401{
1632 return i ? -EINVAL : 0; 1402 struct msi3101_state *s = video_drvdata(file);
1403 int i;
1404 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1405 (char *)&f->fmt.sdr.pixelformat);
1406
1407 for (i = 0; i < NUM_FORMATS; i++) {
1408 if (formats[i].pixelformat == f->fmt.sdr.pixelformat)
1409 return 0;
1410 }
1411
1412 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1413
1414 return 0;
1633} 1415}
1634 1416
1635static int vidioc_s_tuner(struct file *file, void *priv, 1417static int msi3101_s_tuner(struct file *file, void *priv,
1636 const struct v4l2_tuner *v) 1418 const struct v4l2_tuner *v)
1637{ 1419{
1638 struct msi3101_state *s = video_drvdata(file); 1420 struct msi3101_state *s = video_drvdata(file);
@@ -1641,34 +1423,113 @@ static int vidioc_s_tuner(struct file *file, void *priv,
1641 return 0; 1423 return 0;
1642} 1424}
1643 1425
1644static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v) 1426static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
1645{ 1427{
1646 struct msi3101_state *s = video_drvdata(file); 1428 struct msi3101_state *s = video_drvdata(file);
1647 dev_dbg(&s->udev->dev, "%s:\n", __func__); 1429 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1648 1430
1649 strcpy(v->name, "SDR RX"); 1431 if (v->index == 0) {
1650 v->capability = V4L2_TUNER_CAP_LOW; 1432 strlcpy(v->name, "ADC: Mirics MSi2500", sizeof(v->name));
1433 v->type = V4L2_TUNER_ADC;
1434 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1435 v->rangelow = 1200000;
1436 v->rangehigh = 15000000;
1437 } else if (v->index == 1) {
1438 strlcpy(v->name, "RF: Mirics MSi001", sizeof(v->name));
1439 v->type = V4L2_TUNER_RF;
1440 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1441 v->rangelow = 49000000;
1442 v->rangehigh = 960000000;
1443 } else {
1444 return -EINVAL;
1445 }
1651 1446
1652 return 0; 1447 return 0;
1653} 1448}
1654 1449
1655static int vidioc_s_frequency(struct file *file, void *priv, 1450static int msi3101_g_frequency(struct file *file, void *priv,
1451 struct v4l2_frequency *f)
1452{
1453 struct msi3101_state *s = video_drvdata(file);
1454 int ret = 0;
1455 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n",
1456 __func__, f->tuner, f->type);
1457
1458 if (f->tuner == 0)
1459 f->frequency = s->f_adc;
1460 else if (f->tuner == 1)
1461 f->frequency = s->f_tuner;
1462 else
1463 return -EINVAL;
1464
1465 return ret;
1466}
1467
1468static int msi3101_s_frequency(struct file *file, void *priv,
1656 const struct v4l2_frequency *f) 1469 const struct v4l2_frequency *f)
1657{ 1470{
1658 struct msi3101_state *s = video_drvdata(file); 1471 struct msi3101_state *s = video_drvdata(file);
1659 dev_dbg(&s->udev->dev, "%s: frequency=%lu Hz (%u)\n", 1472 int ret, band;
1660 __func__, f->frequency * 625UL / 10UL, f->frequency); 1473 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n",
1474 __func__, f->tuner, f->type, f->frequency);
1475
1476 if (f->tuner == 0) {
1477 s->f_adc = clamp_t(unsigned int, f->frequency,
1478 bands_adc[0].rangelow,
1479 bands_adc[0].rangehigh);
1480 dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n",
1481 __func__, s->f_adc);
1482 ret = msi3101_set_usb_adc(s);
1483 } else if (f->tuner == 1) {
1484 #define BAND_RF_0 ((bands_rf[0].rangehigh + bands_rf[1].rangelow) / 2)
1485 if (f->frequency < BAND_RF_0)
1486 band = 0;
1487 else
1488 band = 1;
1489 s->f_tuner = clamp_t(unsigned int, f->frequency,
1490 bands_rf[band].rangelow,
1491 bands_rf[band].rangehigh);
1492 dev_dbg(&s->udev->dev, "%s: RF frequency=%u Hz\n",
1493 __func__, f->frequency);
1494 ret = msi3101_set_tuner(s);
1495 } else {
1496 return -EINVAL;
1497 }
1661 1498
1662 return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf, 1499 return ret;
1663 f->frequency * 625UL / 10UL); 1500}
1501
1502static int msi3101_enum_freq_bands(struct file *file, void *priv,
1503 struct v4l2_frequency_band *band)
1504{
1505 struct msi3101_state *s = video_drvdata(file);
1506 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n",
1507 __func__, band->tuner, band->type, band->index);
1508
1509 if (band->tuner == 0) {
1510 if (band->index >= ARRAY_SIZE(bands_adc))
1511 return -EINVAL;
1512
1513 *band = bands_adc[band->index];
1514 } else if (band->tuner == 1) {
1515 if (band->index >= ARRAY_SIZE(bands_rf))
1516 return -EINVAL;
1517
1518 *band = bands_rf[band->index];
1519 } else {
1520 return -EINVAL;
1521 }
1522
1523 return 0;
1664} 1524}
1665 1525
1666static const struct v4l2_ioctl_ops msi3101_ioctl_ops = { 1526static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
1667 .vidioc_querycap = msi3101_querycap, 1527 .vidioc_querycap = msi3101_querycap,
1668 1528
1669 .vidioc_enum_input = msi3101_enum_input, 1529 .vidioc_enum_fmt_sdr_cap = msi3101_enum_fmt_sdr_cap,
1670 .vidioc_g_input = msi3101_g_input, 1530 .vidioc_g_fmt_sdr_cap = msi3101_g_fmt_sdr_cap,
1671 .vidioc_s_input = msi3101_s_input, 1531 .vidioc_s_fmt_sdr_cap = msi3101_s_fmt_sdr_cap,
1532 .vidioc_try_fmt_sdr_cap = msi3101_try_fmt_sdr_cap,
1672 1533
1673 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1534 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1674 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1535 .vidioc_create_bufs = vb2_ioctl_create_bufs,
@@ -1680,9 +1541,12 @@ static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
1680 .vidioc_streamon = vb2_ioctl_streamon, 1541 .vidioc_streamon = vb2_ioctl_streamon,
1681 .vidioc_streamoff = vb2_ioctl_streamoff, 1542 .vidioc_streamoff = vb2_ioctl_streamoff,
1682 1543
1683 .vidioc_g_tuner = vidioc_g_tuner, 1544 .vidioc_g_tuner = msi3101_g_tuner,
1684 .vidioc_s_tuner = vidioc_s_tuner, 1545 .vidioc_s_tuner = msi3101_s_tuner,
1685 .vidioc_s_frequency = vidioc_s_frequency, 1546
1547 .vidioc_g_frequency = msi3101_g_frequency,
1548 .vidioc_s_frequency = msi3101_s_frequency,
1549 .vidioc_enum_freq_bands = msi3101_enum_freq_bands,
1686 1550
1687 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1551 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1688 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1552 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
@@ -1710,7 +1574,7 @@ static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
1710{ 1574{
1711 struct msi3101_state *s = 1575 struct msi3101_state *s =
1712 container_of(ctrl->handler, struct msi3101_state, 1576 container_of(ctrl->handler, struct msi3101_state,
1713 ctrl_handler); 1577 hdl);
1714 int ret; 1578 int ret;
1715 dev_dbg(&s->udev->dev, 1579 dev_dbg(&s->udev->dev,
1716 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n", 1580 "%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)
1718 ctrl->minimum, ctrl->maximum, ctrl->step); 1582 ctrl->minimum, ctrl->maximum, ctrl->step);
1719 1583
1720 switch (ctrl->id) { 1584 switch (ctrl->id) {
1721 case MSI3101_CID_SAMPLING_MODE: 1585 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1722 case MSI3101_CID_SAMPLING_RATE: 1586 case V4L2_CID_RF_TUNER_BANDWIDTH:
1723 case MSI3101_CID_SAMPLING_RESOLUTION:
1724 ret = 0;
1725 break;
1726 case MSI3101_CID_TUNER_RF:
1727 case MSI3101_CID_TUNER_BW:
1728 case MSI3101_CID_TUNER_IF:
1729 case MSI3101_CID_TUNER_GAIN:
1730 ret = msi3101_set_tuner(s); 1587 ret = msi3101_set_tuner(s);
1731 break; 1588 break;
1589 case V4L2_CID_RF_TUNER_LNA_GAIN:
1590 case V4L2_CID_RF_TUNER_MIXER_GAIN:
1591 case V4L2_CID_RF_TUNER_IF_GAIN:
1592 ret = msi3101_set_gain(s);
1593 break;
1732 default: 1594 default:
1595 dev_dbg(&s->udev->dev, "%s: EINVAL\n", __func__);
1733 ret = -EINVAL; 1596 ret = -EINVAL;
1734 } 1597 }
1735 1598
@@ -1745,7 +1608,7 @@ static void msi3101_video_release(struct v4l2_device *v)
1745 struct msi3101_state *s = 1608 struct msi3101_state *s =
1746 container_of(v, struct msi3101_state, v4l2_dev); 1609 container_of(v, struct msi3101_state, v4l2_dev);
1747 1610
1748 v4l2_ctrl_handler_free(&s->ctrl_handler); 1611 v4l2_ctrl_handler_free(&s->hdl);
1749 v4l2_device_unregister(&s->v4l2_dev); 1612 v4l2_device_unregister(&s->v4l2_dev);
1750 kfree(s); 1613 kfree(s);
1751} 1614}
@@ -1755,81 +1618,8 @@ static int msi3101_probe(struct usb_interface *intf,
1755{ 1618{
1756 struct usb_device *udev = interface_to_usbdev(intf); 1619 struct usb_device *udev = interface_to_usbdev(intf);
1757 struct msi3101_state *s = NULL; 1620 struct msi3101_state *s = NULL;
1621 const struct v4l2_ctrl_ops *ops = &msi3101_ctrl_ops;
1758 int ret; 1622 int ret;
1759 static const char * const ctrl_sampling_mode_qmenu_strings[] = {
1760 "Quadrature Sampling",
1761 NULL,
1762 };
1763 static const struct v4l2_ctrl_config ctrl_sampling_mode = {
1764 .ops = &msi3101_ctrl_ops,
1765 .id = MSI3101_CID_SAMPLING_MODE,
1766 .type = V4L2_CTRL_TYPE_MENU,
1767 .flags = V4L2_CTRL_FLAG_INACTIVE,
1768 .name = "Sampling Mode",
1769 .qmenu = ctrl_sampling_mode_qmenu_strings,
1770 };
1771 static const struct v4l2_ctrl_config ctrl_sampling_rate = {
1772 .ops = &msi3101_ctrl_ops,
1773 .id = MSI3101_CID_SAMPLING_RATE,
1774 .type = V4L2_CTRL_TYPE_INTEGER64,
1775 .name = "Sampling Rate",
1776 .min = 500000,
1777 .max = 12000000,
1778 .def = 2048000,
1779 .step = 1,
1780 };
1781 static const struct v4l2_ctrl_config ctrl_sampling_resolution = {
1782 .ops = &msi3101_ctrl_ops,
1783 .id = MSI3101_CID_SAMPLING_RESOLUTION,
1784 .type = V4L2_CTRL_TYPE_INTEGER,
1785 .flags = V4L2_CTRL_FLAG_INACTIVE,
1786 .name = "Sampling Resolution",
1787 .min = 10,
1788 .max = 10,
1789 .def = 10,
1790 .step = 1,
1791 };
1792 static const struct v4l2_ctrl_config ctrl_tuner_rf = {
1793 .ops = &msi3101_ctrl_ops,
1794 .id = MSI3101_CID_TUNER_RF,
1795 .type = V4L2_CTRL_TYPE_INTEGER64,
1796 .name = "Tuner RF",
1797 .min = 40000000,
1798 .max = 2000000000,
1799 .def = 100000000,
1800 .step = 1,
1801 };
1802 static const struct v4l2_ctrl_config ctrl_tuner_bw = {
1803 .ops = &msi3101_ctrl_ops,
1804 .id = MSI3101_CID_TUNER_BW,
1805 .type = V4L2_CTRL_TYPE_INTEGER,
1806 .name = "Tuner BW",
1807 .min = 200000,
1808 .max = 8000000,
1809 .def = 600000,
1810 .step = 1,
1811 };
1812 static const struct v4l2_ctrl_config ctrl_tuner_if = {
1813 .ops = &msi3101_ctrl_ops,
1814 .id = MSI3101_CID_TUNER_IF,
1815 .type = V4L2_CTRL_TYPE_INTEGER,
1816 .flags = V4L2_CTRL_FLAG_INACTIVE,
1817 .name = "Tuner IF",
1818 .min = 0,
1819 .max = 2048000,
1820 .def = 0,
1821 .step = 1,
1822 };
1823 static const struct v4l2_ctrl_config ctrl_tuner_gain = {
1824 .ops = &msi3101_ctrl_ops,
1825 .id = MSI3101_CID_TUNER_GAIN,
1826 .type = V4L2_CTRL_TYPE_INTEGER,
1827 .name = "Tuner Gain",
1828 .min = 0,
1829 .max = 102,
1830 .def = 0,
1831 .step = 1,
1832 };
1833 1623
1834 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL); 1624 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1835 if (s == NULL) { 1625 if (s == NULL) {
@@ -1841,11 +1631,12 @@ static int msi3101_probe(struct usb_interface *intf,
1841 mutex_init(&s->vb_queue_lock); 1631 mutex_init(&s->vb_queue_lock);
1842 spin_lock_init(&s->queued_bufs_lock); 1632 spin_lock_init(&s->queued_bufs_lock);
1843 INIT_LIST_HEAD(&s->queued_bufs); 1633 INIT_LIST_HEAD(&s->queued_bufs);
1844
1845 s->udev = udev; 1634 s->udev = udev;
1635 s->f_adc = bands_adc[0].rangelow;
1636 s->pixelformat = V4L2_SDR_FMT_CU8;
1846 1637
1847 /* Init videobuf2 queue structure */ 1638 /* Init videobuf2 queue structure */
1848 s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1639 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1849 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; 1640 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1850 s->vb_queue.drv_priv = s; 1641 s->vb_queue.drv_priv = s;
1851 s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf); 1642 s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
@@ -1853,7 +1644,7 @@ static int msi3101_probe(struct usb_interface *intf,
1853 s->vb_queue.mem_ops = &vb2_vmalloc_memops; 1644 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
1854 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1645 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1855 ret = vb2_queue_init(&s->vb_queue); 1646 ret = vb2_queue_init(&s->vb_queue);
1856 if (ret < 0) { 1647 if (ret) {
1857 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n"); 1648 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
1858 goto err_free_mem; 1649 goto err_free_mem;
1859 } 1650 }
@@ -1866,16 +1657,20 @@ static int msi3101_probe(struct usb_interface *intf,
1866 video_set_drvdata(&s->vdev, s); 1657 video_set_drvdata(&s->vdev, s);
1867 1658
1868 /* Register controls */ 1659 /* Register controls */
1869 v4l2_ctrl_handler_init(&s->ctrl_handler, 7); 1660 v4l2_ctrl_handler_init(&s->hdl, 5);
1870 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_mode, NULL); 1661 s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, ops,
1871 s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL); 1662 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
1872 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL); 1663 s->bandwidth = v4l2_ctrl_new_std(&s->hdl, ops,
1873 s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL); 1664 V4L2_CID_RF_TUNER_BANDWIDTH, 0, 8000000, 1, 0);
1874 s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL); 1665 v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
1875 s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL); 1666 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, ops,
1876 s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL); 1667 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
1877 if (s->ctrl_handler.error) { 1668 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, ops,
1878 ret = s->ctrl_handler.error; 1669 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
1670 s->if_gain = v4l2_ctrl_new_std(&s->hdl, ops,
1671 V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
1672 if (s->hdl.error) {
1673 ret = s->hdl.error;
1879 dev_err(&s->udev->dev, "Could not initialize controls\n"); 1674 dev_err(&s->udev->dev, "Could not initialize controls\n");
1880 goto err_free_controls; 1675 goto err_free_controls;
1881 } 1676 }
@@ -1889,12 +1684,12 @@ static int msi3101_probe(struct usb_interface *intf,
1889 goto err_free_controls; 1684 goto err_free_controls;
1890 } 1685 }
1891 1686
1892 s->v4l2_dev.ctrl_handler = &s->ctrl_handler; 1687 s->v4l2_dev.ctrl_handler = &s->hdl;
1893 s->vdev.v4l2_dev = &s->v4l2_dev; 1688 s->vdev.v4l2_dev = &s->v4l2_dev;
1894 s->vdev.lock = &s->v4l2_lock; 1689 s->vdev.lock = &s->v4l2_lock;
1895 1690
1896 ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1); 1691 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
1897 if (ret < 0) { 1692 if (ret) {
1898 dev_err(&s->udev->dev, 1693 dev_err(&s->udev->dev,
1899 "Failed to register as video device (%d)\n", 1694 "Failed to register as video device (%d)\n",
1900 ret); 1695 ret);
@@ -1908,7 +1703,7 @@ static int msi3101_probe(struct usb_interface *intf,
1908err_unregister_v4l2_dev: 1703err_unregister_v4l2_dev:
1909 v4l2_device_unregister(&s->v4l2_dev); 1704 v4l2_device_unregister(&s->v4l2_dev);
1910err_free_controls: 1705err_free_controls:
1911 v4l2_ctrl_handler_free(&s->ctrl_handler); 1706 v4l2_ctrl_handler_free(&s->hdl);
1912err_free_mem: 1707err_free_mem:
1913 kfree(s); 1708 kfree(s);
1914 return ret; 1709 return ret;