[media] msi3101: add support for stream format "252" I+Q per frame
[deliverable/linux.git] / drivers / staging / media / msi3101 / sdr-msi3101.c
CommitLineData
977e444f
AP
1/*
2 * Mirics MSi3101 SDR Dongle driver
3 *
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/input.h>
26#include <linux/videodev2.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-ioctl.h>
29#include <media/v4l2-ctrls.h>
30#include <media/v4l2-event.h>
31#include <linux/usb.h>
32#include <linux/mutex.h>
33#include <media/videobuf2-vmalloc.h>
34
35struct msi3101_gain {
36 u8 tot:7;
37 u8 baseband:6;
38 bool lna:1;
39 bool mixer:1;
40};
41
42/* 60 – 120 MHz band, lna 24dB, mixer 19dB */
43static const struct msi3101_gain msi3101_gain_lut_120[] = {
44 { 0, 0, 0, 0},
45 { 1, 1, 0, 0},
46 { 2, 2, 0, 0},
47 { 3, 3, 0, 0},
48 { 4, 4, 0, 0},
49 { 5, 5, 0, 0},
50 { 6, 6, 0, 0},
51 { 7, 7, 0, 0},
52 { 8, 8, 0, 0},
53 { 9, 9, 0, 0},
54 { 10, 10, 0, 0},
55 { 11, 11, 0, 0},
56 { 12, 12, 0, 0},
57 { 13, 13, 0, 0},
58 { 14, 14, 0, 0},
59 { 15, 15, 0, 0},
60 { 16, 16, 0, 0},
61 { 17, 17, 0, 0},
62 { 18, 18, 0, 0},
63 { 19, 19, 0, 0},
64 { 20, 20, 0, 0},
65 { 21, 21, 0, 0},
66 { 22, 22, 0, 0},
67 { 23, 23, 0, 0},
68 { 24, 24, 0, 0},
69 { 25, 25, 0, 0},
70 { 26, 26, 0, 0},
71 { 27, 27, 0, 0},
72 { 28, 28, 0, 0},
73 { 29, 5, 1, 0},
74 { 30, 6, 1, 0},
75 { 31, 7, 1, 0},
76 { 32, 8, 1, 0},
77 { 33, 9, 1, 0},
78 { 34, 10, 1, 0},
79 { 35, 11, 1, 0},
80 { 36, 12, 1, 0},
81 { 37, 13, 1, 0},
82 { 38, 14, 1, 0},
83 { 39, 15, 1, 0},
84 { 40, 16, 1, 0},
85 { 41, 17, 1, 0},
86 { 42, 18, 1, 0},
87 { 43, 19, 1, 0},
88 { 44, 20, 1, 0},
89 { 45, 21, 1, 0},
90 { 46, 22, 1, 0},
91 { 47, 23, 1, 0},
92 { 48, 24, 1, 0},
93 { 49, 25, 1, 0},
94 { 50, 26, 1, 0},
95 { 51, 27, 1, 0},
96 { 52, 28, 1, 0},
97 { 53, 29, 1, 0},
98 { 54, 30, 1, 0},
99 { 55, 31, 1, 0},
100 { 56, 32, 1, 0},
101 { 57, 33, 1, 0},
102 { 58, 34, 1, 0},
103 { 59, 35, 1, 0},
104 { 60, 36, 1, 0},
105 { 61, 37, 1, 0},
106 { 62, 38, 1, 0},
107 { 63, 39, 1, 0},
108 { 64, 40, 1, 0},
109 { 65, 41, 1, 0},
110 { 66, 42, 1, 0},
111 { 67, 43, 1, 0},
112 { 68, 44, 1, 0},
113 { 69, 45, 1, 0},
114 { 70, 46, 1, 0},
115 { 71, 47, 1, 0},
116 { 72, 48, 1, 0},
117 { 73, 49, 1, 0},
118 { 74, 50, 1, 0},
119 { 75, 51, 1, 0},
120 { 76, 52, 1, 0},
121 { 77, 53, 1, 0},
122 { 78, 54, 1, 0},
123 { 79, 55, 1, 0},
124 { 80, 56, 1, 0},
125 { 81, 57, 1, 0},
126 { 82, 58, 1, 0},
127 { 83, 40, 1, 1},
128 { 84, 41, 1, 1},
129 { 85, 42, 1, 1},
130 { 86, 43, 1, 1},
131 { 87, 44, 1, 1},
132 { 88, 45, 1, 1},
133 { 89, 46, 1, 1},
134 { 90, 47, 1, 1},
135 { 91, 48, 1, 1},
136 { 92, 49, 1, 1},
137 { 93, 50, 1, 1},
138 { 94, 51, 1, 1},
139 { 95, 52, 1, 1},
140 { 96, 53, 1, 1},
141 { 97, 54, 1, 1},
142 { 98, 55, 1, 1},
143 { 99, 56, 1, 1},
144 {100, 57, 1, 1},
145 {101, 58, 1, 1},
146 {102, 59, 1, 1},
147};
148
149/* 120 – 245 MHz band, lna 24dB, mixer 19dB */
150static const struct msi3101_gain msi3101_gain_lut_245[] = {
151 { 0, 0, 0, 0},
152 { 1, 1, 0, 0},
153 { 2, 2, 0, 0},
154 { 3, 3, 0, 0},
155 { 4, 4, 0, 0},
156 { 5, 5, 0, 0},
157 { 6, 6, 0, 0},
158 { 7, 7, 0, 0},
159 { 8, 8, 0, 0},
160 { 9, 9, 0, 0},
161 { 10, 10, 0, 0},
162 { 11, 11, 0, 0},
163 { 12, 12, 0, 0},
164 { 13, 13, 0, 0},
165 { 14, 14, 0, 0},
166 { 15, 15, 0, 0},
167 { 16, 16, 0, 0},
168 { 17, 17, 0, 0},
169 { 18, 18, 0, 0},
170 { 19, 19, 0, 0},
171 { 20, 20, 0, 0},
172 { 21, 21, 0, 0},
173 { 22, 22, 0, 0},
174 { 23, 23, 0, 0},
175 { 24, 24, 0, 0},
176 { 25, 25, 0, 0},
177 { 26, 26, 0, 0},
178 { 27, 27, 0, 0},
179 { 28, 28, 0, 0},
180 { 29, 5, 1, 0},
181 { 30, 6, 1, 0},
182 { 31, 7, 1, 0},
183 { 32, 8, 1, 0},
184 { 33, 9, 1, 0},
185 { 34, 10, 1, 0},
186 { 35, 11, 1, 0},
187 { 36, 12, 1, 0},
188 { 37, 13, 1, 0},
189 { 38, 14, 1, 0},
190 { 39, 15, 1, 0},
191 { 40, 16, 1, 0},
192 { 41, 17, 1, 0},
193 { 42, 18, 1, 0},
194 { 43, 19, 1, 0},
195 { 44, 20, 1, 0},
196 { 45, 21, 1, 0},
197 { 46, 22, 1, 0},
198 { 47, 23, 1, 0},
199 { 48, 24, 1, 0},
200 { 49, 25, 1, 0},
201 { 50, 26, 1, 0},
202 { 51, 27, 1, 0},
203 { 52, 28, 1, 0},
204 { 53, 29, 1, 0},
205 { 54, 30, 1, 0},
206 { 55, 31, 1, 0},
207 { 56, 32, 1, 0},
208 { 57, 33, 1, 0},
209 { 58, 34, 1, 0},
210 { 59, 35, 1, 0},
211 { 60, 36, 1, 0},
212 { 61, 37, 1, 0},
213 { 62, 38, 1, 0},
214 { 63, 39, 1, 0},
215 { 64, 40, 1, 0},
216 { 65, 41, 1, 0},
217 { 66, 42, 1, 0},
218 { 67, 43, 1, 0},
219 { 68, 44, 1, 0},
220 { 69, 45, 1, 0},
221 { 70, 46, 1, 0},
222 { 71, 47, 1, 0},
223 { 72, 48, 1, 0},
224 { 73, 49, 1, 0},
225 { 74, 50, 1, 0},
226 { 75, 51, 1, 0},
227 { 76, 52, 1, 0},
228 { 77, 53, 1, 0},
229 { 78, 54, 1, 0},
230 { 79, 55, 1, 0},
231 { 80, 56, 1, 0},
232 { 81, 57, 1, 0},
233 { 82, 58, 1, 0},
234 { 83, 40, 1, 1},
235 { 84, 41, 1, 1},
236 { 85, 42, 1, 1},
237 { 86, 43, 1, 1},
238 { 87, 44, 1, 1},
239 { 88, 45, 1, 1},
240 { 89, 46, 1, 1},
241 { 90, 47, 1, 1},
242 { 91, 48, 1, 1},
243 { 92, 49, 1, 1},
244 { 93, 50, 1, 1},
245 { 94, 51, 1, 1},
246 { 95, 52, 1, 1},
247 { 96, 53, 1, 1},
248 { 97, 54, 1, 1},
249 { 98, 55, 1, 1},
250 { 99, 56, 1, 1},
251 {100, 57, 1, 1},
252 {101, 58, 1, 1},
253 {102, 59, 1, 1},
254};
255
256/* 420 – 1000 MHz band, lna 7dB, mixer 19dB */
257static const struct msi3101_gain msi3101_gain_lut_1000[] = {
258 { 0, 0, 0, 0},
259 { 1, 1, 0, 0},
260 { 2, 2, 0, 0},
261 { 3, 3, 0, 0},
262 { 4, 4, 0, 0},
263 { 5, 5, 0, 0},
264 { 6, 6, 0, 0},
265 { 7, 7, 0, 0},
266 { 8, 8, 0, 0},
267 { 9, 9, 0, 0},
268 { 10, 10, 0, 0},
269 { 11, 11, 0, 0},
270 { 12, 5, 1, 0},
271 { 13, 6, 1, 0},
272 { 14, 7, 1, 0},
273 { 15, 8, 1, 0},
274 { 16, 9, 1, 0},
275 { 17, 10, 1, 0},
276 { 18, 11, 1, 0},
277 { 19, 12, 1, 0},
278 { 20, 13, 1, 0},
279 { 21, 14, 1, 0},
280 { 22, 15, 1, 0},
281 { 23, 16, 1, 0},
282 { 24, 17, 1, 0},
283 { 25, 18, 1, 0},
284 { 26, 19, 1, 0},
285 { 27, 20, 1, 0},
286 { 28, 21, 1, 0},
287 { 29, 22, 1, 0},
288 { 30, 23, 1, 0},
289 { 31, 24, 1, 0},
290 { 32, 25, 1, 0},
291 { 33, 26, 1, 0},
292 { 34, 27, 1, 0},
293 { 35, 28, 1, 0},
294 { 36, 29, 1, 0},
295 { 37, 30, 1, 0},
296 { 38, 31, 1, 0},
297 { 39, 32, 1, 0},
298 { 40, 33, 1, 0},
299 { 41, 34, 1, 0},
300 { 42, 35, 1, 0},
301 { 43, 36, 1, 0},
302 { 44, 37, 1, 0},
303 { 45, 38, 1, 0},
304 { 46, 39, 1, 0},
305 { 47, 40, 1, 0},
306 { 48, 41, 1, 0},
307 { 49, 42, 1, 0},
308 { 50, 43, 1, 0},
309 { 51, 44, 1, 0},
310 { 52, 45, 1, 0},
311 { 53, 46, 1, 0},
312 { 54, 47, 1, 0},
313 { 55, 48, 1, 0},
314 { 56, 49, 1, 0},
315 { 57, 50, 1, 0},
316 { 58, 51, 1, 0},
317 { 59, 52, 1, 0},
318 { 60, 53, 1, 0},
319 { 61, 54, 1, 0},
320 { 62, 55, 1, 0},
321 { 63, 56, 1, 0},
322 { 64, 57, 1, 0},
323 { 65, 58, 1, 0},
324 { 66, 40, 1, 1},
325 { 67, 41, 1, 1},
326 { 68, 42, 1, 1},
327 { 69, 43, 1, 1},
328 { 70, 44, 1, 1},
329 { 71, 45, 1, 1},
330 { 72, 46, 1, 1},
331 { 73, 47, 1, 1},
332 { 74, 48, 1, 1},
333 { 75, 49, 1, 1},
334 { 76, 50, 1, 1},
335 { 77, 51, 1, 1},
336 { 78, 52, 1, 1},
337 { 79, 53, 1, 1},
338 { 80, 54, 1, 1},
339 { 81, 55, 1, 1},
340 { 82, 56, 1, 1},
341 { 83, 57, 1, 1},
342 { 84, 58, 1, 1},
343 { 85, 59, 1, 1},
344};
345
346/*
347 * iConfiguration 0
348 * bInterfaceNumber 0
349 * bAlternateSetting 1
350 * bNumEndpoints 1
351 * bEndpointAddress 0x81 EP 1 IN
352 * bmAttributes 1
353 * Transfer Type Isochronous
354 * wMaxPacketSize 0x1400 3x 1024 bytes
355 * bInterval 1
356 */
357#define MAX_ISO_BUFS (8)
358#define ISO_FRAMES_PER_DESC (8)
359#define ISO_MAX_FRAME_SIZE (3 * 1024)
360#define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
361
362#define MAX_ISOC_ERRORS 20
363
3673b35c
AP
364#define MSI3101_CID_SAMPLING_MODE ((V4L2_CID_USER_BASE | 0xf000) + 0)
365#define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 1)
366#define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 2)
977e444f
AP
367#define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10)
368#define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11)
369#define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12)
370#define MSI3101_CID_TUNER_GAIN ((V4L2_CID_USER_BASE | 0xf000) + 13)
371
372/* intermediate buffers with raw data from the USB device */
373struct msi3101_frame_buf {
374 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
375 struct list_head list;
376 void *data; /* raw data from USB device */
377 int filled; /* number of bytes filled to *data */
378};
379
380struct msi3101_state {
381 struct video_device vdev;
382 struct v4l2_device v4l2_dev;
383
384 /* videobuf2 queue and queued buffers list */
385 struct vb2_queue vb_queue;
386 struct list_head queued_bufs;
387 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
388
389 /* Note if taking both locks v4l2_lock must always be locked first! */
390 struct mutex v4l2_lock; /* Protects everything else */
391 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
392
393 /* Pointer to our usb_device, will be NULL after unplug */
394 struct usb_device *udev; /* Both mutexes most be hold when setting! */
395
396 unsigned int isoc_errors; /* number of contiguous ISOC errors */
397 unsigned int vb_full; /* vb is full and packets dropped */
398
399 struct urb *urbs[MAX_ISO_BUFS];
554cbfbe
AP
400 int (*convert_stream) (struct msi3101_state *s, u32 *dst, u8 *src,
401 unsigned int src_len);
977e444f
AP
402
403 /* Controls */
404 struct v4l2_ctrl_handler ctrl_handler;
405 struct v4l2_ctrl *ctrl_sampling_rate;
406 struct v4l2_ctrl *ctrl_tuner_rf;
407 struct v4l2_ctrl *ctrl_tuner_bw;
408 struct v4l2_ctrl *ctrl_tuner_if;
409 struct v4l2_ctrl *ctrl_tuner_gain;
410
34599b9b
AP
411 u32 next_sample; /* for track lost packets */
412 u32 sample; /* for sample rate calc */
977e444f 413 unsigned long jiffies;
02004681 414 unsigned int sample_ctrl_bit[4];
977e444f
AP
415};
416
417/* Private functions */
418static struct msi3101_frame_buf *msi3101_get_next_fill_buf(
419 struct msi3101_state *s)
420{
421 unsigned long flags = 0;
422 struct msi3101_frame_buf *buf = NULL;
423
424 spin_lock_irqsave(&s->queued_bufs_lock, flags);
425 if (list_empty(&s->queued_bufs))
426 goto leave;
427
428 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list);
429 list_del(&buf->list);
430leave:
431 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
432 return buf;
433}
434
02004681
AP
435/*
436 * +===========================================================================
6450e509 437 * | 00-1023 | USB packet
02004681
AP
438 * +===========================================================================
439 * | 00- 03 | sequence number of first sample in that USB packet
440 * +---------------------------------------------------------------------------
441 * | 04- 15 | garbage
442 * +---------------------------------------------------------------------------
443 * | 16- 175 | samples
444 * +---------------------------------------------------------------------------
445 * | 176- 179 | control bits for previous samples
446 * +---------------------------------------------------------------------------
447 * | 180- 339 | samples
448 * +---------------------------------------------------------------------------
449 * | 340- 343 | control bits for previous samples
450 * +---------------------------------------------------------------------------
451 * | 344- 503 | samples
452 * +---------------------------------------------------------------------------
453 * | 504- 507 | control bits for previous samples
454 * +---------------------------------------------------------------------------
455 * | 508- 667 | samples
456 * +---------------------------------------------------------------------------
457 * | 668- 671 | control bits for previous samples
458 * +---------------------------------------------------------------------------
459 * | 672- 831 | samples
460 * +---------------------------------------------------------------------------
461 * | 832- 835 | control bits for previous samples
462 * +---------------------------------------------------------------------------
463 * | 836- 995 | samples
464 * +---------------------------------------------------------------------------
465 * | 996- 999 | control bits for previous samples
466 * +---------------------------------------------------------------------------
6450e509 467 * | 1000-1023 | garbage
02004681
AP
468 * +---------------------------------------------------------------------------
469 *
470 * Bytes 4 - 7 could have some meaning?
471 *
472 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
473 * numbers. This results one 2-bit number for 8 samples. It is likely used for
474 * for bit shifting sample by given bits, increasing actual sampling resolution.
475 * Number 2 (0b10) was never seen.
476 *
477 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
478 */
479
977e444f
AP
480/*
481 * Converts signed 10-bit integer into 32-bit IEEE floating point
482 * representation.
483 * Will be exact from 0 to 2^24. Above that, we round towards zero
484 * as the fractional bits will not fit in a float. (It would be better to
485 * round towards even as the fpu does, but that is slower.)
486 */
487#define I2F_FRAC_BITS 23
488#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
554cbfbe 489static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift)
977e444f 490{
02004681
AP
491 u32 msb, exponent, fraction, sign;
492 s->sample_ctrl_bit[shift]++;
977e444f
AP
493
494 /* Zero is special */
495 if (!x)
496 return 0;
497
02004681
AP
498 /* Convert 10-bit two's complement to 13-bit */
499 if (x & (1 << 9)) {
500 x |= ~0U << 10; /* set all the rest bits to one */
501 x <<= shift;
977e444f 502 x = -x;
02004681 503 x &= 0xfff; /* result is 12 bit ... + sign */
977e444f
AP
504 sign = 1 << 31;
505 } else {
02004681 506 x <<= shift;
977e444f
AP
507 sign = 0 << 31;
508 }
509
510 /* Get location of the most significant bit */
511 msb = __fls(x);
512
513 /*
514 * Use a rotate instead of a shift because that works both leftwards
515 * and rightwards due to the mod(32) behaviour. This means we don't
516 * need to check to see if we are above 2^24 or not.
517 */
518 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
519 exponent = (127 + msb) << I2F_FRAC_BITS;
520
521 return (fraction + exponent) | sign;
522}
523
524#define MSI3101_CONVERT_IN_URB_HANDLER
525#define MSI3101_EXTENSIVE_DEBUG
554cbfbe 526static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst,
6450e509 527 u8 *src, unsigned int src_len)
977e444f
AP
528{
529 int i, j, k, l, i_max, dst_len = 0;
530 u16 sample[4];
02004681 531 u32 bits;
977e444f 532#ifdef MSI3101_EXTENSIVE_DEBUG
34599b9b 533 u32 sample_num[3];
977e444f
AP
534#endif
535 /* There could be 1-3 1024 bytes URB frames */
536 i_max = src_len / 1024;
537 for (i = 0; i < i_max; i++) {
538#ifdef MSI3101_EXTENSIVE_DEBUG
34599b9b
AP
539 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
540 if (i == 0 && s->next_sample != sample_num[0]) {
977e444f 541 dev_dbg(&s->udev->dev,
34599b9b
AP
542 "%d samples lost, %d %08x:%08x\n",
543 sample_num[0] - s->next_sample,
544 src_len, s->next_sample, sample_num[0]);
977e444f 545 }
6450e509
AP
546
547 /*
548 * Dump all unknown 'garbage' data - maybe we will discover
549 * someday if there is something rational...
550 */
551 dev_dbg_ratelimited(&s->udev->dev,
552 "%*ph %*ph\n", 12, &src[4], 24, &src[1000]);
553 memset(&src[4], 0, 12);
554 memset(&src[1000], 0, 24);
977e444f
AP
555#endif
556 src += 16;
557 for (j = 0; j < 6; j++) {
02004681 558 bits = src[160 + 3] << 24 | src[160 + 2] << 16 | src[160 + 1] << 8 | src[160 + 0] << 0;
977e444f
AP
559 for (k = 0; k < 16; k++) {
560 for (l = 0; l < 10; l += 5) {
561 sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8;
562 sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6;
563 sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4;
564 sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2;
565
554cbfbe
AP
566 *dst++ = msi3101_convert_sample_384(s, sample[0], (bits >> (2 * k)) & 0x3);
567 *dst++ = msi3101_convert_sample_384(s, sample[1], (bits >> (2 * k)) & 0x3);
568 *dst++ = msi3101_convert_sample_384(s, sample[2], (bits >> (2 * k)) & 0x3);
569 *dst++ = msi3101_convert_sample_384(s, sample[3], (bits >> (2 * k)) & 0x3);
977e444f
AP
570
571 /* 4 x 32bit float samples */
572 dst_len += 4 * 4;
573 }
574 src += 10;
575 }
576#ifdef MSI3101_EXTENSIVE_DEBUG
02004681
AP
577 dev_dbg_ratelimited(&s->udev->dev,
578 "sample control bits %08x\n", bits);
977e444f
AP
579#endif
580 src += 4;
581 }
582 src += 24;
583 }
584
585#ifdef MSI3101_EXTENSIVE_DEBUG
34599b9b 586 /* calculate samping rate and output it in 10 seconds intervals */
977e444f
AP
587 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
588 unsigned long jiffies_now = jiffies;
589 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
34599b9b 590 unsigned int samples = sample_num[i_max - 1] - s->sample;
977e444f 591 s->jiffies = jiffies_now;
34599b9b 592 s->sample = sample_num[i_max - 1];
977e444f 593 dev_dbg(&s->udev->dev,
02004681 594 "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n",
34599b9b 595 src_len, samples, msecs,
02004681
AP
596 samples * 1000UL / msecs,
597 s->sample_ctrl_bit[0], s->sample_ctrl_bit[1],
598 s->sample_ctrl_bit[2], s->sample_ctrl_bit[3]);
977e444f
AP
599 }
600
34599b9b
AP
601 /* next sample (sample = sample + i * 384) */
602 s->next_sample = sample_num[i_max - 1] + 384;
977e444f
AP
603#endif
604 return dst_len;
605}
606
554cbfbe
AP
607/*
608 * Converts signed 14-bit integer into 32-bit IEEE floating point
609 * representation.
610 * Will be exact from 0 to 2^24. Above that, we round towards zero
611 * as the fractional bits will not fit in a float. (It would be better to
612 * round towards even as the fpu does, but that is slower.)
613 */
614#define I2F_FRAC_BITS 23
615#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
616static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x)
617{
618 u32 msb, exponent, fraction, sign;
619
620 /* Zero is special */
621 if (!x)
622 return 0;
623
624 /* Negative / positive value */
625 if (x & (1 << 13)) {
626 x = -x;
627 x &= 0x1fff; /* result is 13 bit ... + sign */
628 sign = 1 << 31;
629 } else {
630 sign = 0 << 31;
631 }
632
633 /* Get location of the most significant bit */
634 msb = __fls(x);
635
636 /*
637 * Use a rotate instead of a shift because that works both leftwards
638 * and rightwards due to the mod(32) behaviour. This means we don't
639 * need to check to see if we are above 2^24 or not.
640 */
641 fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK;
642 exponent = (127 + msb) << I2F_FRAC_BITS;
643
644 return (fraction + exponent) | sign;
645}
646
647static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst,
648 u8 *src, unsigned int src_len)
649{
650 int i, j, i_max, dst_len = 0;
651 u16 sample[2];
652 u32 sample_num[3];
653
654 /* There could be 1-3 1024 bytes URB frames */
655 i_max = src_len / 1024;
656
657 for (i = 0; i < i_max; i++) {
658 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
659 if (i == 0 && s->next_sample != sample_num[0]) {
660 dev_dbg_ratelimited(&s->udev->dev,
661 "%d samples lost, %d %08x:%08x\n",
662 sample_num[0] - s->next_sample,
663 src_len, s->next_sample, sample_num[0]);
664 }
665
666 /*
667 * Dump all unknown 'garbage' data - maybe we will discover
668 * someday if there is something rational...
669 */
670 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
671
672 src += 16;
673 for (j = 0; j < 1008; j += 4) {
674 sample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
675 sample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
676
677 *dst++ = msi3101_convert_sample_252(s, sample[0]);
678 *dst++ = msi3101_convert_sample_252(s, sample[1]);
679 }
680 /* 252 x I+Q 32bit float samples */
681 dst_len += 252 * 2 * 4;
682 src += 1008;
683 }
684
685 /* calculate samping rate and output it in 10 seconds intervals */
686 if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) {
687 unsigned long jiffies_now = jiffies;
688 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies);
689 unsigned int samples = sample_num[i_max - 1] - s->sample;
690 s->jiffies = jiffies_now;
691 s->sample = sample_num[i_max - 1];
692 dev_dbg(&s->udev->dev,
693 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
694 src_len, samples, msecs,
695 samples * 1000UL / msecs);
696 }
697
698 /* next sample (sample = sample + i * 252) */
699 s->next_sample = sample_num[i_max - 1] + 252;
700
701 return dst_len;
702}
703
977e444f
AP
704/*
705 * This gets called for the Isochronous pipe (stream). This is done in interrupt
706 * time, so it has to be fast, not crash, and not stall. Neat.
707 */
708static void msi3101_isoc_handler(struct urb *urb)
709{
710 struct msi3101_state *s = (struct msi3101_state *)urb->context;
711 int i, flen, fstatus;
712 unsigned char *iso_buf = NULL;
713 struct msi3101_frame_buf *fbuf;
714
715 if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
716 urb->status == -ESHUTDOWN) {
717 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
718 urb, urb->status == -ENOENT ? "" : "a");
719 return;
720 }
721
722 if (urb->status != 0) {
723 dev_dbg(&s->udev->dev,
724 "msi3101_isoc_handler() called with status %d\n",
725 urb->status);
726 /* Give up after a number of contiguous errors */
727 if (++s->isoc_errors > MAX_ISOC_ERRORS)
728 dev_dbg(&s->udev->dev,
729 "Too many ISOC errors, bailing out\n");
730 goto handler_end;
731 } else {
732 /* Reset ISOC error counter. We did get here, after all. */
733 s->isoc_errors = 0;
734 }
735
736 /* Compact data */
737 for (i = 0; i < urb->number_of_packets; i++) {
554cbfbe
AP
738 void *ptr;
739
977e444f
AP
740 /* Check frame error */
741 fstatus = urb->iso_frame_desc[i].status;
742 if (fstatus) {
743 dev_dbg(&s->udev->dev,
744 "frame=%d/%d has error %d skipping\n",
745 i, urb->number_of_packets, fstatus);
746 goto skip;
747 }
748
749 /* Check if that frame contains data */
750 flen = urb->iso_frame_desc[i].actual_length;
751 if (flen == 0)
752 goto skip;
753
754 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
755
756 /* Get free framebuffer */
757 fbuf = msi3101_get_next_fill_buf(s);
758 if (fbuf == NULL) {
759 s->vb_full++;
760 dev_dbg_ratelimited(&s->udev->dev,
761 "videobuf is full, %d packets dropped\n",
762 s->vb_full);
763 goto skip;
764 }
765
766 /* fill framebuffer */
767#ifdef MSI3101_CONVERT_IN_URB_HANDLER
554cbfbe
AP
768 ptr = vb2_plane_vaddr(&fbuf->vb, 0);
769 flen = s->convert_stream(s, ptr, iso_buf, flen);
770 vb2_set_plane_payload(&fbuf->vb, 0, flen);
977e444f
AP
771#else
772 memcpy(fbuf->data, iso_buf, flen);
773 fbuf->filled = flen;
774#endif
775 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
776skip:
777 ;
778 }
779
780handler_end:
781 i = usb_submit_urb(urb, GFP_ATOMIC);
782 if (i != 0)
783 dev_dbg(&s->udev->dev,
784 "Error (%d) re-submitting urb in msi3101_isoc_handler\n",
785 i);
786}
787
788static void msi3101_iso_stop(struct msi3101_state *s)
789{
790 int i;
791 dev_dbg(&s->udev->dev, "%s:\n", __func__);
792
793 /* Unlinking ISOC buffers one by one */
794 for (i = 0; i < MAX_ISO_BUFS; i++) {
795 if (s->urbs[i]) {
796 dev_dbg(&s->udev->dev, "Unlinking URB %p\n",
797 s->urbs[i]);
798 usb_kill_urb(s->urbs[i]);
799 }
800 }
801}
802
803static void msi3101_iso_free(struct msi3101_state *s)
804{
805 int i;
806 dev_dbg(&s->udev->dev, "%s:\n", __func__);
807
808 /* Freeing ISOC buffers one by one */
809 for (i = 0; i < MAX_ISO_BUFS; i++) {
810 if (s->urbs[i]) {
811 dev_dbg(&s->udev->dev, "Freeing URB\n");
812 if (s->urbs[i]->transfer_buffer) {
813 usb_free_coherent(s->udev,
814 s->urbs[i]->transfer_buffer_length,
815 s->urbs[i]->transfer_buffer,
816 s->urbs[i]->transfer_dma);
817 }
818 usb_free_urb(s->urbs[i]);
819 s->urbs[i] = NULL;
820 }
821 }
822}
823
824/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
825static void msi3101_isoc_cleanup(struct msi3101_state *s)
826{
827 dev_dbg(&s->udev->dev, "%s:\n", __func__);
828
829 msi3101_iso_stop(s);
830 msi3101_iso_free(s);
831}
832
833/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
834static int msi3101_isoc_init(struct msi3101_state *s)
835{
836 struct usb_device *udev;
837 struct urb *urb;
838 int i, j, ret;
839 dev_dbg(&s->udev->dev, "%s:\n", __func__);
840
841 s->isoc_errors = 0;
842 udev = s->udev;
843
844 ret = usb_set_interface(s->udev, 0, 1);
845 if (ret < 0)
846 return ret;
847
848 /* Allocate and init Isochronuous urbs */
849 for (i = 0; i < MAX_ISO_BUFS; i++) {
850 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
851 if (urb == NULL) {
852 dev_err(&s->udev->dev,
853 "Failed to allocate urb %d\n", i);
854 msi3101_isoc_cleanup(s);
855 return -ENOMEM;
856 }
857 s->urbs[i] = urb;
858 dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb);
859
860 urb->interval = 1;
861 urb->dev = udev;
862 urb->pipe = usb_rcvisocpipe(udev, 0x81);
863 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
864 urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE,
865 GFP_KERNEL, &urb->transfer_dma);
866 if (urb->transfer_buffer == NULL) {
867 dev_err(&s->udev->dev,
868 "Failed to allocate urb buffer %d\n",
869 i);
870 msi3101_isoc_cleanup(s);
871 return -ENOMEM;
872 }
873 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
874 urb->complete = msi3101_isoc_handler;
875 urb->context = s;
876 urb->start_frame = 0;
877 urb->number_of_packets = ISO_FRAMES_PER_DESC;
878 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
879 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
880 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
881 }
882 }
883
884 /* link */
885 for (i = 0; i < MAX_ISO_BUFS; i++) {
886 ret = usb_submit_urb(s->urbs[i], GFP_KERNEL);
887 if (ret) {
888 dev_err(&s->udev->dev,
889 "isoc_init() submit_urb %d failed with error %d\n",
890 i, ret);
891 msi3101_isoc_cleanup(s);
892 return ret;
893 }
894 dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]);
895 }
896
897 /* All is done... */
898 return 0;
899}
900
901/* Must be called with vb_queue_lock hold */
902static void msi3101_cleanup_queued_bufs(struct msi3101_state *s)
903{
904 unsigned long flags = 0;
905 dev_dbg(&s->udev->dev, "%s:\n", __func__);
906
907 spin_lock_irqsave(&s->queued_bufs_lock, flags);
908 while (!list_empty(&s->queued_bufs)) {
909 struct msi3101_frame_buf *buf;
910
911 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf,
912 list);
913 list_del(&buf->list);
914 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
915 }
916 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
917}
918
919/* The user yanked out the cable... */
920static void msi3101_disconnect(struct usb_interface *intf)
921{
922 struct v4l2_device *v = usb_get_intfdata(intf);
923 struct msi3101_state *s =
924 container_of(v, struct msi3101_state, v4l2_dev);
925 dev_dbg(&s->udev->dev, "%s:\n", __func__);
926
927 mutex_lock(&s->vb_queue_lock);
928 mutex_lock(&s->v4l2_lock);
929 /* No need to keep the urbs around after disconnection */
930 s->udev = NULL;
931
932 v4l2_device_disconnect(&s->v4l2_dev);
933 video_unregister_device(&s->vdev);
934 mutex_unlock(&s->v4l2_lock);
935 mutex_unlock(&s->vb_queue_lock);
936
937 v4l2_device_put(&s->v4l2_dev);
938}
939
940static int msi3101_querycap(struct file *file, void *fh,
941 struct v4l2_capability *cap)
942{
943 struct msi3101_state *s = video_drvdata(file);
944 dev_dbg(&s->udev->dev, "%s:\n", __func__);
945
946 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
947 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
948 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
949 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
950 V4L2_CAP_READWRITE;
951 cap->device_caps = V4L2_CAP_TUNER;
952 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
953 return 0;
954}
955
956
957/* Videobuf2 operations */
958static int msi3101_queue_setup(struct vb2_queue *vq,
959 const struct v4l2_format *fmt, unsigned int *nbuffers,
960 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
961{
962 struct msi3101_state *s = vb2_get_drv_priv(vq);
963 dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers);
964
965 /* Absolute min and max number of buffers available for mmap() */
966 *nbuffers = 32;
967 *nplanes = 1;
968 sizes[0] = PAGE_ALIGN(3 * 3072); /* 3 * 768 * 4 */
969 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
970 __func__, *nbuffers, sizes[0]);
971 return 0;
972}
973
974static int msi3101_buf_init(struct vb2_buffer *vb)
975{
976 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
977 struct msi3101_frame_buf *fbuf =
978 container_of(vb, struct msi3101_frame_buf, vb);
979 dev_dbg(&s->udev->dev, "%s:\n", __func__);
980
981 fbuf->data = vzalloc(ISO_MAX_FRAME_SIZE);
982 if (fbuf->data == NULL)
983 return -ENOMEM;
984
985 return 0;
986}
987
988static int msi3101_buf_prepare(struct vb2_buffer *vb)
989{
990 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
991
992 /* Don't allow queing new buffers after device disconnection */
993 if (!s->udev)
994 return -ENODEV;
995
996 return 0;
997}
998
977e444f
AP
999#ifdef MSI3101_CONVERT_IN_URB_HANDLER
1000static int msi3101_buf_finish(struct vb2_buffer *vb)
1001{
1002 return 0;
1003}
1004#else
1005static int msi3101_buf_finish(struct vb2_buffer *vb)
1006{
1007 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
1008 struct msi3101_frame_buf *fbuf =
1009 container_of(vb, struct msi3101_frame_buf, vb);
1010 int ret;
1011 u32 *dst = vb2_plane_vaddr(&fbuf->vb, 0);
554cbfbe 1012 ret = msi3101_convert_stream_384(s, dst, fbuf->data, fbuf->filled);
977e444f
AP
1013 vb2_set_plane_payload(&fbuf->vb, 0, ret);
1014 return 0;
1015}
1016#endif
1017
1018static void msi3101_buf_cleanup(struct vb2_buffer *vb)
1019{
1020 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
1021 struct msi3101_frame_buf *buf =
1022 container_of(vb, struct msi3101_frame_buf, vb);
1023 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1024
1025 vfree(buf->data);
1026}
1027static void msi3101_buf_queue(struct vb2_buffer *vb)
1028{
1029 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
1030 struct msi3101_frame_buf *buf =
1031 container_of(vb, struct msi3101_frame_buf, vb);
1032 unsigned long flags = 0;
1033
1034 /* Check the device has not disconnected between prep and queuing */
1035 if (!s->udev) {
1036 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1037 return;
1038 }
1039
1040 spin_lock_irqsave(&s->queued_bufs_lock, flags);
1041 list_add_tail(&buf->list, &s->queued_bufs);
1042 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
1043}
1044
1045#define CMD_WREG 0x41
1046#define CMD_START_STREAMING 0x43
1047#define CMD_STOP_STREAMING 0x45
1048#define CMD_READ_UNKNOW 0x48
1049
1050#define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \
1051 char *direction; \
1052 if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
1053 direction = ">>>"; \
1054 else \
1055 direction = "<<<"; \
1056 dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
1057 "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \
1058 _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \
1059}
1060
1061static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data)
1062{
1063 int ret;
1064 u8 request = cmd;
1065 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
1066 u16 value = (data >> 0) & 0xffff;
1067 u16 index = (data >> 16) & 0xffff;
1068
1069 msi3101_dbg_usb_control_msg(s->udev,
1070 request, requesttype, value, index, NULL, 0);
1071
d0fadf40 1072 ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0),
977e444f
AP
1073 request, requesttype, value, index, NULL, 0, 2000);
1074
1075 if (ret)
1076 dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n",
1077 __func__, ret, cmd, data);
1078
1079 return ret;
1080};
1081
1082static int msi3101_tuner_write(struct msi3101_state *s, u32 data)
1083{
1084 return msi3101_ctrl_msg(s, CMD_WREG, data << 8 | 0x09);
1085};
1086
1087#define F_REF 24000000
b1520857 1088#define DIV_R_IN 2
977e444f
AP
1089static int msi3101_set_usb_adc(struct msi3101_state *s)
1090{
22ca680e 1091 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
554cbfbe
AP
1092 u32 reg3, reg4, reg7;
1093
1094 f_sr = s->ctrl_sampling_rate->val64;
1095
1096 /* select stream format */
1097 if (f_sr < 6000000) {
1098 s->convert_stream = msi3101_convert_stream_252;
1099 reg7 = 0x00009407;
1100 } else {
1101 s->convert_stream = msi3101_convert_stream_384;
1102 reg7 = 0x0000a507;
1103 }
1104
977e444f 1105 /*
b1520857 1106 * Synthesizer config is just a educated guess...
977e444f 1107 *
977e444f
AP
1108 * [7:0] 0x03, register address
1109 * [8] 1, always
1110 * [9] ?
1111 * [12:10] output divider
1112 * [13] 0 ?
1113 * [14] 0 ?
22ca680e 1114 * [15] fractional MSB, bit 20
977e444f
AP
1115 * [16:19] N
1116 * [23:20] ?
1117 * [24:31] 0x01
1118 *
1119 * output divider
1120 * val div
1121 * 0 - (invalid)
b1520857
AP
1122 * 1 4
1123 * 2 6
1124 * 3 8
1125 * 4 10
1126 * 5 12
1127 * 6 14
1128 * 7 16
1129 *
1130 * VCO 202000000 - 720000000++
977e444f 1131 */
977e444f 1132 reg3 = 0x01c00303;
22ca680e 1133 reg4 = 0x00000004;
977e444f 1134
b1520857
AP
1135 for (div_r_out = 4; div_r_out < 16; div_r_out += 2) {
1136 f_vco = f_sr * div_r_out * 12;
1137 dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n",
1138 __func__, div_r_out, f_vco);
1139 if (f_vco >= 202000000)
977e444f
AP
1140 break;
1141 }
1142
b1520857
AP
1143 div_n = f_vco / (F_REF * DIV_R_IN);
1144 div_m = f_vco % (F_REF * DIV_R_IN);
22ca680e 1145 fract = 0x200000ul * div_m / (F_REF * DIV_R_IN);
977e444f 1146
b1520857
AP
1147 reg3 |= div_n << 16;
1148 reg3 |= (div_r_out / 2 - 1) << 10;
22ca680e
AP
1149 reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */
1150 reg4 |= ((fract >> 0) & 0x0fffff) << 8; /* [19:0] */
977e444f 1151
b1520857 1152 dev_dbg(&s->udev->dev,
22ca680e
AP
1153 "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n",
1154 __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg3, reg4);
977e444f
AP
1155
1156 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008);
1157 if (ret)
1158 goto err;
1159
1160 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05);
1161 if (ret)
1162 goto err;
1163
1164 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000);
1165 if (ret)
1166 goto err;
1167
1168 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102);
1169 if (ret)
1170 goto err;
1171
1172 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008);
1173 if (ret)
1174 goto err;
1175
554cbfbe 1176 ret = msi3101_ctrl_msg(s, CMD_WREG, reg7);
977e444f
AP
1177 if (ret)
1178 goto err;
1179
1180 ret = msi3101_ctrl_msg(s, CMD_WREG, reg4);
1181 if (ret)
1182 goto err;
1183
1184 ret = msi3101_ctrl_msg(s, CMD_WREG, reg3);
1185 if (ret)
1186 goto err;
1187err:
1188 return ret;
1189};
1190
1191static int msi3101_set_tuner(struct msi3101_state *s)
1192{
23df427e
AP
1193 int ret, i, len;
1194 unsigned int n, m, thresh, frac, vco_step, tmp;
1195 u32 reg;
1196 u64 f_vco;
977e444f
AP
1197 u8 mode, lo_div;
1198 const struct msi3101_gain *gain_lut;
1199 static const struct {
1200 u32 rf;
1201 u8 mode;
1202 u8 lo_div;
1203 } band_lut[] = {
1204 { 30000000, 0x01, 16}, /* AM_MODE1 */
1205 {108000000, 0x02, 32}, /* VHF_MODE */
1206 {240000000, 0x04, 16}, /* B3_MODE */
1207 {960000000, 0x08, 4}, /* B45_MODE */
1208 {167500000, 0x10, 2}, /* BL_MODE */
1209 };
1210 static const struct {
1211 u32 freq;
1212 u8 val;
1213 } if_freq_lut[] = {
1214 { 0, 0x03}, /* Zero IF */
1215 { 450000, 0x02}, /* 450 kHz IF */
1216 {1620000, 0x01}, /* 1.62 MHz IF */
1217 {2048000, 0x00}, /* 2.048 MHz IF */
1218 };
1219 static const struct {
1220 u32 freq;
1221 u8 val;
1222 } bandwidth_lut[] = {
1223 { 200000, 0x00}, /* 200 kHz */
1224 { 300000, 0x01}, /* 300 kHz */
1225 { 600000, 0x02}, /* 600 kHz */
1226 {1536000, 0x03}, /* 1.536 MHz */
1227 {5000000, 0x04}, /* 5 MHz */
1228 {6000000, 0x05}, /* 6 MHz */
1229 {7000000, 0x06}, /* 7 MHz */
1230 {8000000, 0x07}, /* 8 MHz */
1231 };
1232
1233 unsigned int rf_freq = s->ctrl_tuner_rf->val64;
1234
1235 /*
1236 * bandwidth (Hz)
1237 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
1238 */
1239 int bandwidth = s->ctrl_tuner_bw->val;
1240
1241 /*
1242 * intermediate frequency (Hz)
1243 * 0, 450000, 1620000, 2048000
1244 */
1245 int if_freq = s->ctrl_tuner_if->val;
1246
1247 /*
1248 * gain reduction (dB)
1249 * 0 - 102 below 420 MHz
1250 * 0 - 85 above 420 MHz
1251 */
1252 int gain = s->ctrl_tuner_gain->val;
1253
1254 dev_dbg(&s->udev->dev,
1255 "%s: rf_freq=%d bandwidth=%d if_freq=%d gain=%d\n",
1256 __func__, rf_freq, bandwidth, if_freq, gain);
1257
1258 ret = -EINVAL;
1259
1260 for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
1261 if (rf_freq <= band_lut[i].rf) {
1262 mode = band_lut[i].mode;
1263 lo_div = band_lut[i].lo_div;
1264 break;
1265 }
1266 }
1267
1268 if (i == ARRAY_SIZE(band_lut))
1269 goto err;
1270
1271 for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
1272 if (if_freq == if_freq_lut[i].freq) {
1273 if_freq = if_freq_lut[i].val;
1274 break;
1275 }
1276 }
1277
1278 if (i == ARRAY_SIZE(if_freq_lut))
1279 goto err;
1280
1281 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
1282 if (bandwidth == bandwidth_lut[i].freq) {
1283 bandwidth = bandwidth_lut[i].val;
1284 break;
1285 }
1286 }
1287
1288 if (i == ARRAY_SIZE(bandwidth_lut))
1289 goto err;
1290
23df427e
AP
1291#define F_OUT_STEP 1
1292#define R_REF 4
1293#define F_IF 0
1294 f_vco = (rf_freq + F_IF) * lo_div;
1295 n = f_vco / (F_REF * R_REF);
1296 m = f_vco % (F_REF * R_REF);
977e444f 1297
23df427e
AP
1298 vco_step = F_OUT_STEP * lo_div;
1299 thresh = (F_REF * R_REF) / vco_step;
1300 frac = 1ul * thresh * m / (F_REF * R_REF);
1301
1302 /* Divide to reg max. After that RF resolution will be +-500Hz. */
1303 tmp = DIV_ROUND_UP(thresh, 4095);
1304 thresh = DIV_ROUND_CLOSEST(thresh, tmp);
1305 frac = DIV_ROUND_CLOSEST(frac, tmp);
1306
1307 /* calc real RF set */
1308 tmp = 1ul * F_REF * R_REF * n;
1309 tmp += 1ul * F_REF * R_REF * frac / thresh;
1310 tmp /= lo_div;
1311
1312 dev_dbg(&s->udev->dev,
1313 "%s: rf=%u:%u n=%d thresh=%d frac=%d\n",
1314 __func__, rf_freq, tmp, n, thresh, frac);
977e444f
AP
1315
1316 ret = msi3101_tuner_write(s, 0x00000e);
1317 ret = msi3101_tuner_write(s, 0x000003);
1318
1319 reg = 0 << 0;
1320 reg |= mode << 4;
1321 reg |= 1 << 10;
1322 reg |= if_freq << 12;
1323 reg |= bandwidth << 14;
1324 reg |= 0x02 << 17;
1325 reg |= 0x00 << 20;
1326 ret = msi3101_tuner_write(s, reg);
1327 if (ret)
1328 goto err;
1329
1330 reg = 5 << 0;
1331 reg |= thresh << 4;
1332 reg |= 1 << 19;
1333 reg |= 1 << 21;
1334 ret = msi3101_tuner_write(s, reg);
1335 if (ret)
1336 goto err;
1337
1338 reg = 2 << 0;
1339 reg |= frac << 4;
1340 reg |= n << 16;
1341 ret = msi3101_tuner_write(s, reg);
1342 if (ret)
1343 goto err;
1344
1345 if (rf_freq < 120000000) {
1346 gain_lut = msi3101_gain_lut_120;
1347 len = ARRAY_SIZE(msi3101_gain_lut_120);
1348 } else if (rf_freq < 245000000) {
1349 gain_lut = msi3101_gain_lut_245;
1350 len = ARRAY_SIZE(msi3101_gain_lut_120);
1351 } else {
1352 gain_lut = msi3101_gain_lut_1000;
1353 len = ARRAY_SIZE(msi3101_gain_lut_1000);
1354 }
1355
1356 for (i = 0; i < len; i++) {
1357 if (gain_lut[i].tot >= gain)
1358 break;
1359 }
1360
1361 if (i == len)
1362 goto err;
1363
1364 dev_dbg(&s->udev->dev,
1365 "%s: gain tot=%d baseband=%d lna=%d mixer=%d\n",
1366 __func__, gain_lut[i].tot, gain_lut[i].baseband,
1367 gain_lut[i].lna, gain_lut[i].mixer);
1368
1369 reg = 1 << 0;
1370 reg |= gain_lut[i].baseband << 4;
1371 reg |= 0 << 10;
1372 reg |= gain_lut[i].mixer << 12;
1373 reg |= gain_lut[i].lna << 13;
1374 reg |= 4 << 14;
1375 reg |= 0 << 17;
1376 ret = msi3101_tuner_write(s, reg);
1377 if (ret)
1378 goto err;
1379
1380 reg = 6 << 0;
1381 reg |= 63 << 4;
1382 reg |= 4095 << 10;
1383 ret = msi3101_tuner_write(s, reg);
1384 if (ret)
1385 goto err;
1386
1387 return 0;
1388err:
1389 dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret);
1390 return ret;
1391};
1392
1393static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
1394{
1395 struct msi3101_state *s = vb2_get_drv_priv(vq);
1396 int ret;
1397 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1398
1399 if (!s->udev)
1400 return -ENODEV;
1401
1402 if (mutex_lock_interruptible(&s->v4l2_lock))
1403 return -ERESTARTSYS;
1404
1405 ret = msi3101_set_usb_adc(s);
1406
1407 ret = msi3101_isoc_init(s);
1408 if (ret)
1409 msi3101_cleanup_queued_bufs(s);
1410
1411 ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0);
1412
1413 mutex_unlock(&s->v4l2_lock);
1414
1415 return ret;
1416}
1417
1418static int msi3101_stop_streaming(struct vb2_queue *vq)
1419{
1420 struct msi3101_state *s = vb2_get_drv_priv(vq);
1421 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1422
1423 if (mutex_lock_interruptible(&s->v4l2_lock))
1424 return -ERESTARTSYS;
1425
977e444f
AP
1426 if (s->udev)
1427 msi3101_isoc_cleanup(s);
1428
1429 msi3101_cleanup_queued_bufs(s);
d0fadf40
AP
1430
1431 /* according to tests, at least 700us delay is required */
1432 msleep(20);
1433 msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0);
1434
977e444f
AP
1435 mutex_unlock(&s->v4l2_lock);
1436
1437 return 0;
1438}
1439
1440static struct vb2_ops msi3101_vb2_ops = {
1441 .queue_setup = msi3101_queue_setup,
1442 .buf_init = msi3101_buf_init,
1443 .buf_prepare = msi3101_buf_prepare,
1444 .buf_finish = msi3101_buf_finish,
1445 .buf_cleanup = msi3101_buf_cleanup,
1446 .buf_queue = msi3101_buf_queue,
1447 .start_streaming = msi3101_start_streaming,
1448 .stop_streaming = msi3101_stop_streaming,
1449 .wait_prepare = vb2_ops_wait_prepare,
1450 .wait_finish = vb2_ops_wait_finish,
1451};
1452
1453static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i)
1454{
1455 if (i->index != 0)
1456 return -EINVAL;
1457
1458 strlcpy(i->name, "SDR data", sizeof(i->name));
1459 i->type = V4L2_INPUT_TYPE_CAMERA;
1460
1461 return 0;
1462}
1463
1464static int msi3101_g_input(struct file *file, void *fh, unsigned int *i)
1465{
1466 *i = 0;
1467
1468 return 0;
1469}
1470
1471static int msi3101_s_input(struct file *file, void *fh, unsigned int i)
1472{
1473 return i ? -EINVAL : 0;
1474}
1475
1476static int vidioc_s_tuner(struct file *file, void *priv,
1477 const struct v4l2_tuner *v)
1478{
1479 struct msi3101_state *s = video_drvdata(file);
1480 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1481
1482 return 0;
1483}
1484
1485static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
1486{
1487 struct msi3101_state *s = video_drvdata(file);
1488 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1489
1490 strcpy(v->name, "SDR RX");
1491 v->capability = V4L2_TUNER_CAP_LOW;
1492
1493 return 0;
1494}
1495
1496static int vidioc_s_frequency(struct file *file, void *priv,
1497 const struct v4l2_frequency *f)
1498{
1499 struct msi3101_state *s = video_drvdata(file);
1500 dev_dbg(&s->udev->dev, "%s: frequency=%u Hz (%d)\n",
1501 __func__, f->frequency * 625U / 10U, f->frequency);
1502
1503 return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf,
1504 f->frequency * 625U / 10U);
1505}
1506
1507const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
1508 .vidioc_querycap = msi3101_querycap,
1509
1510 .vidioc_enum_input = msi3101_enum_input,
1511 .vidioc_g_input = msi3101_g_input,
1512 .vidioc_s_input = msi3101_s_input,
1513
1514 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1515 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1516 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1517 .vidioc_querybuf = vb2_ioctl_querybuf,
1518 .vidioc_qbuf = vb2_ioctl_qbuf,
1519 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1520
1521 .vidioc_streamon = vb2_ioctl_streamon,
1522 .vidioc_streamoff = vb2_ioctl_streamoff,
1523
1524 .vidioc_g_tuner = vidioc_g_tuner,
1525 .vidioc_s_tuner = vidioc_s_tuner,
1526 .vidioc_s_frequency = vidioc_s_frequency,
1527
1528 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1529 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1530 .vidioc_log_status = v4l2_ctrl_log_status,
1531};
1532
1533static const struct v4l2_file_operations msi3101_fops = {
1534 .owner = THIS_MODULE,
1535 .open = v4l2_fh_open,
1536 .release = vb2_fop_release,
1537 .read = vb2_fop_read,
1538 .poll = vb2_fop_poll,
1539 .mmap = vb2_fop_mmap,
1540 .unlocked_ioctl = video_ioctl2,
1541};
1542
1543static struct video_device msi3101_template = {
1544 .name = "Mirics MSi3101 SDR Dongle",
1545 .release = video_device_release_empty,
1546 .fops = &msi3101_fops,
1547 .ioctl_ops = &msi3101_ioctl_ops,
1548};
1549
1550static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl)
1551{
1552 struct msi3101_state *s =
1553 container_of(ctrl->handler, struct msi3101_state,
1554 ctrl_handler);
1555 int ret;
1556 dev_dbg(&s->udev->dev,
1557 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
1558 __func__, ctrl->id, ctrl->name, ctrl->val,
1559 ctrl->minimum, ctrl->maximum, ctrl->step);
1560
1561 switch (ctrl->id) {
3673b35c 1562 case MSI3101_CID_SAMPLING_MODE:
977e444f
AP
1563 case MSI3101_CID_SAMPLING_RATE:
1564 case MSI3101_CID_SAMPLING_RESOLUTION:
1565 ret = 0;
1566 break;
1567 case MSI3101_CID_TUNER_RF:
1568 case MSI3101_CID_TUNER_BW:
1569 case MSI3101_CID_TUNER_IF:
1570 case MSI3101_CID_TUNER_GAIN:
1571 ret = msi3101_set_tuner(s);
1572 break;
1573 default:
1574 ret = -EINVAL;
1575 }
1576
1577 return ret;
1578}
1579
1580static const struct v4l2_ctrl_ops msi3101_ctrl_ops = {
1581 .s_ctrl = msi3101_s_ctrl,
1582};
1583
1584static void msi3101_video_release(struct v4l2_device *v)
1585{
1586 struct msi3101_state *s =
1587 container_of(v, struct msi3101_state, v4l2_dev);
1588
1589 v4l2_ctrl_handler_free(&s->ctrl_handler);
1590 v4l2_device_unregister(&s->v4l2_dev);
1591 kfree(s);
1592}
1593
1594static int msi3101_probe(struct usb_interface *intf,
1595 const struct usb_device_id *id)
1596{
1597 struct usb_device *udev = interface_to_usbdev(intf);
1598 struct msi3101_state *s = NULL;
1599 int ret;
3673b35c
AP
1600 static const char * const ctrl_sampling_mode_qmenu_strings[] = {
1601 "Quadrature Sampling",
1602 NULL,
1603 };
1604 static const struct v4l2_ctrl_config ctrl_sampling_mode = {
1605 .ops = &msi3101_ctrl_ops,
1606 .id = MSI3101_CID_SAMPLING_MODE,
1607 .type = V4L2_CTRL_TYPE_MENU,
1608 .flags = V4L2_CTRL_FLAG_INACTIVE,
1609 .name = "Sampling Mode",
1610 .qmenu = ctrl_sampling_mode_qmenu_strings,
1611 };
977e444f
AP
1612 static const struct v4l2_ctrl_config ctrl_sampling_rate = {
1613 .ops = &msi3101_ctrl_ops,
1614 .id = MSI3101_CID_SAMPLING_RATE,
1615 .type = V4L2_CTRL_TYPE_INTEGER64,
1616 .name = "Sampling Rate",
1617 .min = 500000,
1618 .max = 12000000,
1619 .def = 2048000,
1620 .step = 1,
1621 };
1622 static const struct v4l2_ctrl_config ctrl_sampling_resolution = {
1623 .ops = &msi3101_ctrl_ops,
1624 .id = MSI3101_CID_SAMPLING_RESOLUTION,
1625 .type = V4L2_CTRL_TYPE_INTEGER,
1626 .flags = V4L2_CTRL_FLAG_INACTIVE,
1627 .name = "Sampling Resolution",
1628 .min = 10,
1629 .max = 10,
1630 .def = 10,
1631 .step = 1,
1632 };
1633 static const struct v4l2_ctrl_config ctrl_tuner_rf = {
1634 .ops = &msi3101_ctrl_ops,
1635 .id = MSI3101_CID_TUNER_RF,
1636 .type = V4L2_CTRL_TYPE_INTEGER64,
1637 .name = "Tuner RF",
1638 .min = 40000000,
1639 .max = 2000000000,
1640 .def = 100000000,
1641 .step = 1,
1642 };
1643 static const struct v4l2_ctrl_config ctrl_tuner_bw = {
1644 .ops = &msi3101_ctrl_ops,
1645 .id = MSI3101_CID_TUNER_BW,
1646 .type = V4L2_CTRL_TYPE_INTEGER,
1647 .name = "Tuner BW",
1648 .min = 200000,
1649 .max = 8000000,
1650 .def = 600000,
1651 .step = 1,
1652 };
1653 static const struct v4l2_ctrl_config ctrl_tuner_if = {
1654 .ops = &msi3101_ctrl_ops,
1655 .id = MSI3101_CID_TUNER_IF,
1656 .type = V4L2_CTRL_TYPE_INTEGER,
1657 .flags = V4L2_CTRL_FLAG_INACTIVE,
1658 .name = "Tuner IF",
1659 .min = 0,
1660 .max = 2048000,
1661 .def = 0,
1662 .step = 1,
1663 };
1664 static const struct v4l2_ctrl_config ctrl_tuner_gain = {
1665 .ops = &msi3101_ctrl_ops,
1666 .id = MSI3101_CID_TUNER_GAIN,
1667 .type = V4L2_CTRL_TYPE_INTEGER,
1668 .name = "Tuner Gain",
1669 .min = 0,
1670 .max = 102,
1671 .def = 0,
1672 .step = 1,
1673 };
1674
1675 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1676 if (s == NULL) {
1677 pr_err("Could not allocate memory for msi3101_state\n");
1678 return -ENOMEM;
1679 }
1680
1681 mutex_init(&s->v4l2_lock);
1682 mutex_init(&s->vb_queue_lock);
1683 spin_lock_init(&s->queued_bufs_lock);
1684 INIT_LIST_HEAD(&s->queued_bufs);
1685
1686 s->udev = udev;
1687
1688 /* Init videobuf2 queue structure */
1689 s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1690 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1691 s->vb_queue.drv_priv = s;
1692 s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
1693 s->vb_queue.ops = &msi3101_vb2_ops;
1694 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
1695 s->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1696 ret = vb2_queue_init(&s->vb_queue);
1697 if (ret < 0) {
1698 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
1699 goto err_free_mem;
1700 }
1701
1702 /* Init video_device structure */
1703 s->vdev = msi3101_template;
1704 s->vdev.queue = &s->vb_queue;
1705 s->vdev.queue->lock = &s->vb_queue_lock;
1706 set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags);
1707 video_set_drvdata(&s->vdev, s);
1708
1709 /* Register controls */
3673b35c
AP
1710 v4l2_ctrl_handler_init(&s->ctrl_handler, 7);
1711 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_mode, NULL);
977e444f
AP
1712 s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL);
1713 v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL);
1714 s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL);
1715 s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL);
1716 s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL);
1717 s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL);
1718 if (s->ctrl_handler.error) {
1719 ret = s->ctrl_handler.error;
1720 dev_err(&s->udev->dev, "Could not initialize controls\n");
1721 goto err_free_controls;
1722 }
1723
1724 /* Register the v4l2_device structure */
1725 s->v4l2_dev.release = msi3101_video_release;
1726 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1727 if (ret) {
1728 dev_err(&s->udev->dev,
1729 "Failed to register v4l2-device (%d)\n", ret);
1730 goto err_free_controls;
1731 }
1732
1733 s->v4l2_dev.ctrl_handler = &s->ctrl_handler;
1734 s->vdev.v4l2_dev = &s->v4l2_dev;
1735 s->vdev.lock = &s->v4l2_lock;
1736
1737 ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1);
1738 if (ret < 0) {
1739 dev_err(&s->udev->dev,
1740 "Failed to register as video device (%d)\n",
1741 ret);
1742 goto err_unregister_v4l2_dev;
1743 }
1744 dev_info(&s->udev->dev, "Registered as %s\n",
1745 video_device_node_name(&s->vdev));
1746
1747 return 0;
1748
1749err_unregister_v4l2_dev:
1750 v4l2_device_unregister(&s->v4l2_dev);
1751err_free_controls:
1752 v4l2_ctrl_handler_free(&s->ctrl_handler);
1753err_free_mem:
1754 kfree(s);
1755 return ret;
1756}
1757
1758/* USB device ID list */
1759static struct usb_device_id msi3101_id_table[] = {
1760 { USB_DEVICE(0x1df7, 0x2500) },
42fc5b42 1761 { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */
977e444f
AP
1762 { }
1763};
1764MODULE_DEVICE_TABLE(usb, msi3101_id_table);
1765
1766/* USB subsystem interface */
1767static struct usb_driver msi3101_driver = {
1768 .name = KBUILD_MODNAME,
1769 .probe = msi3101_probe,
1770 .disconnect = msi3101_disconnect,
1771 .id_table = msi3101_id_table,
1772};
1773
1774module_usb_driver(msi3101_driver);
1775
1776MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1777MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1778MODULE_LICENSE("GPL");
This page took 0.0971 seconds and 5 git commands to generate.