blob: e4f3f4e655d2896c8e312a7ace0ecb2c5c4b6daf [file] [log] [blame]
Antti Palosaari977e4442013-07-05 00:24:08 -03001/*
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.
Antti Palosaari00e049b2013-08-07 15:06:29 -030019 *
20 * That driver is somehow based of pwc driver:
21 * (C) 1999-2004 Nemosoft Unv.
22 * (C) 2004-2006 Luc Saillard (luc@saillard.org)
23 * (C) 2011 Hans de Goede <hdegoede@redhat.com>
Antti Palosaari977e4442013-07-05 00:24:08 -030024 */
25
Antti Palosaari977e4442013-07-05 00:24:08 -030026#include <linux/module.h>
Antti Palosaari977e4442013-07-05 00:24:08 -030027#include <linux/slab.h>
Mauro Carvalho Chehab04e40bd2013-08-24 13:35:14 -030028#include <asm/div64.h>
Antti Palosaari977e4442013-07-05 00:24:08 -030029#include <media/v4l2-device.h>
30#include <media/v4l2-ioctl.h>
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-event.h>
33#include <linux/usb.h>
Antti Palosaari977e4442013-07-05 00:24:08 -030034#include <media/videobuf2-vmalloc.h>
Antti Palosaari2e68f842014-02-02 23:42:00 -030035#include <linux/spi/spi.h>
Antti Palosaari977e4442013-07-05 00:24:08 -030036
Antti Palosaari3912eb62014-07-13 22:27:21 -030037static bool msi3101_emulated_fmt;
38module_param_named(emulated_formats, msi3101_emulated_fmt, bool, 0644);
39MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
40
Antti Palosaari977e4442013-07-05 00:24:08 -030041/*
42 * iConfiguration 0
43 * bInterfaceNumber 0
44 * bAlternateSetting 1
45 * bNumEndpoints 1
46 * bEndpointAddress 0x81 EP 1 IN
47 * bmAttributes 1
48 * Transfer Type Isochronous
49 * wMaxPacketSize 0x1400 3x 1024 bytes
50 * bInterval 1
51 */
52#define MAX_ISO_BUFS (8)
53#define ISO_FRAMES_PER_DESC (8)
54#define ISO_MAX_FRAME_SIZE (3 * 1024)
55#define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
Antti Palosaari977e4442013-07-05 00:24:08 -030056#define MAX_ISOC_ERRORS 20
57
Antti Palosaari00e049b2013-08-07 15:06:29 -030058/* TODO: These should be moved to V4L2 API */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030059#define V4L2_PIX_FMT_SDR_S12 v4l2_fourcc('D', 'S', '1', '2') /* signed 12-bit */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030060#define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */
61
Antti Palosaari2e68f842014-02-02 23:42:00 -030062static const struct v4l2_frequency_band bands[] = {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030063 {
64 .tuner = 0,
65 .type = V4L2_TUNER_ADC,
66 .index = 0,
67 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
68 .rangelow = 1200000,
69 .rangehigh = 15000000,
70 },
71};
72
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030073/* stream formats */
74struct msi3101_format {
75 char *name;
76 u32 pixelformat;
77};
78
79/* format descriptions for capture and preview */
80static struct msi3101_format formats[] = {
81 {
Antti Palosaari3912eb62014-07-13 22:27:21 -030082 .name = "Complex S8",
83 .pixelformat = V4L2_SDR_FMT_CS8,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030084#if 0
85 }, {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030086 .name = "10+2-bit signed",
87 .pixelformat = V4L2_PIX_FMT_SDR_MSI2500_384,
88 }, {
89 .name = "12-bit signed",
90 .pixelformat = V4L2_PIX_FMT_SDR_S12,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030091#endif
Antti Palosaari3912eb62014-07-13 22:27:21 -030092 }, {
93 .name = "Complex S14LE",
94 .pixelformat = V4L2_SDR_FMT_CS14LE,
95 }, {
96 .name = "Complex U8 (emulated)",
97 .pixelformat = V4L2_SDR_FMT_CU8,
98 }, {
99 .name = "Complex U16LE (emulated)",
100 .pixelformat = V4L2_SDR_FMT_CU16LE,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300101 },
102};
103
104static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
Antti Palosaari977e4442013-07-05 00:24:08 -0300105
106/* intermediate buffers with raw data from the USB device */
107struct msi3101_frame_buf {
108 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
109 struct list_head list;
Antti Palosaari977e4442013-07-05 00:24:08 -0300110};
111
112struct msi3101_state {
113 struct video_device vdev;
114 struct v4l2_device v4l2_dev;
Antti Palosaari2e68f842014-02-02 23:42:00 -0300115 struct v4l2_subdev *v4l2_subdev;
116 struct spi_master *master;
Antti Palosaari977e4442013-07-05 00:24:08 -0300117
118 /* videobuf2 queue and queued buffers list */
119 struct vb2_queue vb_queue;
120 struct list_head queued_bufs;
121 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
122
123 /* Note if taking both locks v4l2_lock must always be locked first! */
124 struct mutex v4l2_lock; /* Protects everything else */
125 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
126
127 /* Pointer to our usb_device, will be NULL after unplug */
128 struct usb_device *udev; /* Both mutexes most be hold when setting! */
129
Antti Palosaari2e68f842014-02-02 23:42:00 -0300130 unsigned int f_adc;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300131 u32 pixelformat;
Antti Palosaari3912eb62014-07-13 22:27:21 -0300132 unsigned int num_formats;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300133
Antti Palosaari977e4442013-07-05 00:24:08 -0300134 unsigned int isoc_errors; /* number of contiguous ISOC errors */
135 unsigned int vb_full; /* vb is full and packets dropped */
136
137 struct urb *urbs[MAX_ISO_BUFS];
Linus Torvalds3c83e612014-04-04 09:50:07 -0700138 int (*convert_stream)(struct msi3101_state *s, u8 *dst, u8 *src,
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300139 unsigned int src_len);
Antti Palosaari977e4442013-07-05 00:24:08 -0300140
141 /* Controls */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300142 struct v4l2_ctrl_handler hdl;
Antti Palosaari977e4442013-07-05 00:24:08 -0300143
Antti Palosaari34599b92013-07-08 22:31:47 -0300144 u32 next_sample; /* for track lost packets */
145 u32 sample; /* for sample rate calc */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300146 unsigned long jiffies_next;
Antti Palosaari02004682013-07-10 21:59:58 -0300147 unsigned int sample_ctrl_bit[4];
Antti Palosaari977e4442013-07-05 00:24:08 -0300148};
149
150/* Private functions */
151static struct msi3101_frame_buf *msi3101_get_next_fill_buf(
152 struct msi3101_state *s)
153{
154 unsigned long flags = 0;
155 struct msi3101_frame_buf *buf = NULL;
156
157 spin_lock_irqsave(&s->queued_bufs_lock, flags);
158 if (list_empty(&s->queued_bufs))
159 goto leave;
160
161 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list);
162 list_del(&buf->list);
163leave:
164 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
165 return buf;
166}
167
168/*
Antti Palosaari02004682013-07-10 21:59:58 -0300169 * +===========================================================================
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300170 * | 00-1023 | USB packet type '504'
171 * +===========================================================================
172 * | 00- 03 | sequence number of first sample in that USB packet
173 * +---------------------------------------------------------------------------
174 * | 04- 15 | garbage
175 * +---------------------------------------------------------------------------
176 * | 16-1023 | samples
177 * +---------------------------------------------------------------------------
178 * signed 8-bit sample
179 * 504 * 2 = 1008 samples
180 */
181static int msi3101_convert_stream_504(struct msi3101_state *s, u8 *dst,
182 u8 *src, unsigned int src_len)
183{
184 int i, i_max, dst_len = 0;
185 u32 sample_num[3];
186
187 /* There could be 1-3 1024 bytes URB frames */
188 i_max = src_len / 1024;
189
190 for (i = 0; i < i_max; i++) {
191 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
192 if (i == 0 && s->next_sample != sample_num[0]) {
193 dev_dbg_ratelimited(&s->udev->dev,
194 "%d samples lost, %d %08x:%08x\n",
195 sample_num[0] - s->next_sample,
196 src_len, s->next_sample, sample_num[0]);
197 }
198
199 /*
200 * Dump all unknown 'garbage' data - maybe we will discover
201 * someday if there is something rational...
202 */
203 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
204
205 /* 504 x I+Q samples */
206 src += 16;
207 memcpy(dst, src, 1008);
208 src += 1008;
209 dst += 1008;
210 dst_len += 1008;
211 }
212
213 /* calculate samping rate and output it in 10 seconds intervals */
214 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
215 unsigned long jiffies_now = jiffies;
216 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
217 unsigned int samples = sample_num[i_max - 1] - s->sample;
218 s->jiffies_next = jiffies_now;
219 s->sample = sample_num[i_max - 1];
220 dev_dbg(&s->udev->dev,
221 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
222 src_len, samples, msecs,
223 samples * 1000UL / msecs);
224 }
225
226 /* next sample (sample = sample + i * 504) */
227 s->next_sample = sample_num[i_max - 1] + 504;
228
229 return dst_len;
230}
231
232static int msi3101_convert_stream_504_u8(struct msi3101_state *s, u8 *dst,
233 u8 *src, unsigned int src_len)
234{
235 int i, j, i_max, dst_len = 0;
236 u32 sample_num[3];
237 s8 *s8src;
238 u8 *u8dst;
239
240 /* There could be 1-3 1024 bytes URB frames */
241 i_max = src_len / 1024;
242 u8dst = (u8 *) dst;
243
244 for (i = 0; i < i_max; i++) {
245 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
246 if (i == 0 && s->next_sample != sample_num[0]) {
247 dev_dbg_ratelimited(&s->udev->dev,
248 "%d samples lost, %d %08x:%08x\n",
249 sample_num[0] - s->next_sample,
250 src_len, s->next_sample, sample_num[0]);
251 }
252
253 /*
254 * Dump all unknown 'garbage' data - maybe we will discover
255 * someday if there is something rational...
256 */
257 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
258
259 /* 504 x I+Q samples */
260 src += 16;
261
262 s8src = (s8 *) src;
263 for (j = 0; j < 1008; j++)
264 *u8dst++ = *s8src++ + 128;
265
266 src += 1008;
267 dst += 1008;
268 dst_len += 1008;
269 }
270
271 /* calculate samping rate and output it in 10 seconds intervals */
272 if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
273#define MSECS 10000UL
274 unsigned int samples = sample_num[i_max - 1] - s->sample;
275 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
276 s->sample = sample_num[i_max - 1];
277 dev_dbg(&s->udev->dev,
278 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
279 src_len, samples, MSECS,
280 samples * 1000UL / MSECS);
281 }
282
283 /* next sample (sample = sample + i * 504) */
284 s->next_sample = sample_num[i_max - 1] + 504;
285
286 return dst_len;
287}
288
289/*
290 * +===========================================================================
Antti Palosaari61198df2013-08-08 15:39:33 -0300291 * | 00-1023 | USB packet type '384'
Antti Palosaari02004682013-07-10 21:59:58 -0300292 * +===========================================================================
293 * | 00- 03 | sequence number of first sample in that USB packet
294 * +---------------------------------------------------------------------------
295 * | 04- 15 | garbage
296 * +---------------------------------------------------------------------------
297 * | 16- 175 | samples
298 * +---------------------------------------------------------------------------
299 * | 176- 179 | control bits for previous samples
300 * +---------------------------------------------------------------------------
301 * | 180- 339 | samples
302 * +---------------------------------------------------------------------------
303 * | 340- 343 | control bits for previous samples
304 * +---------------------------------------------------------------------------
305 * | 344- 503 | samples
306 * +---------------------------------------------------------------------------
307 * | 504- 507 | control bits for previous samples
308 * +---------------------------------------------------------------------------
309 * | 508- 667 | samples
310 * +---------------------------------------------------------------------------
311 * | 668- 671 | control bits for previous samples
312 * +---------------------------------------------------------------------------
313 * | 672- 831 | samples
314 * +---------------------------------------------------------------------------
315 * | 832- 835 | control bits for previous samples
316 * +---------------------------------------------------------------------------
317 * | 836- 995 | samples
318 * +---------------------------------------------------------------------------
319 * | 996- 999 | control bits for previous samples
320 * +---------------------------------------------------------------------------
Antti Palosaari6450e502013-07-31 19:58:12 -0300321 * | 1000-1023 | garbage
Antti Palosaari02004682013-07-10 21:59:58 -0300322 * +---------------------------------------------------------------------------
323 *
324 * Bytes 4 - 7 could have some meaning?
325 *
326 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
327 * numbers. This results one 2-bit number for 8 samples. It is likely used for
328 * for bit shifting sample by given bits, increasing actual sampling resolution.
329 * Number 2 (0b10) was never seen.
330 *
331 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
332 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300333static int msi3101_convert_stream_384(struct msi3101_state *s, u8 *dst,
Antti Palosaari61198df2013-08-08 15:39:33 -0300334 u8 *src, unsigned int src_len)
335{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300336 int i, i_max, dst_len = 0;
Antti Palosaari34599b92013-07-08 22:31:47 -0300337 u32 sample_num[3];
Antti Palosaari00e049b2013-08-07 15:06:29 -0300338
Antti Palosaari977e4442013-07-05 00:24:08 -0300339 /* There could be 1-3 1024 bytes URB frames */
340 i_max = src_len / 1024;
341 for (i = 0; i < i_max; i++) {
Antti Palosaari34599b92013-07-08 22:31:47 -0300342 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
343 if (i == 0 && s->next_sample != sample_num[0]) {
Antti Palosaari00e049b2013-08-07 15:06:29 -0300344 dev_dbg_ratelimited(&s->udev->dev,
Antti Palosaari34599b92013-07-08 22:31:47 -0300345 "%d samples lost, %d %08x:%08x\n",
346 sample_num[0] - s->next_sample,
347 src_len, s->next_sample, sample_num[0]);
Antti Palosaari977e4442013-07-05 00:24:08 -0300348 }
Antti Palosaari6450e502013-07-31 19:58:12 -0300349
350 /*
351 * Dump all unknown 'garbage' data - maybe we will discover
352 * someday if there is something rational...
353 */
354 dev_dbg_ratelimited(&s->udev->dev,
355 "%*ph %*ph\n", 12, &src[4], 24, &src[1000]);
Antti Palosaari00e049b2013-08-07 15:06:29 -0300356
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300357 /* 384 x I+Q samples */
Antti Palosaari977e4442013-07-05 00:24:08 -0300358 src += 16;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300359 memcpy(dst, src, 984);
360 src += 984 + 24;
361 dst += 984;
362 dst_len += 984;
Antti Palosaari977e4442013-07-05 00:24:08 -0300363 }
364
Antti Palosaari34599b92013-07-08 22:31:47 -0300365 /* calculate samping rate and output it in 10 seconds intervals */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300366 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300367 unsigned long jiffies_now = jiffies;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300368 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
Antti Palosaari34599b92013-07-08 22:31:47 -0300369 unsigned int samples = sample_num[i_max - 1] - s->sample;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300370 s->jiffies_next = jiffies_now;
Antti Palosaari34599b92013-07-08 22:31:47 -0300371 s->sample = sample_num[i_max - 1];
Antti Palosaari977e4442013-07-05 00:24:08 -0300372 dev_dbg(&s->udev->dev,
Antti Palosaari02004682013-07-10 21:59:58 -0300373 "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n",
Antti Palosaari34599b92013-07-08 22:31:47 -0300374 src_len, samples, msecs,
Antti Palosaari02004682013-07-10 21:59:58 -0300375 samples * 1000UL / msecs,
376 s->sample_ctrl_bit[0], s->sample_ctrl_bit[1],
377 s->sample_ctrl_bit[2], s->sample_ctrl_bit[3]);
Antti Palosaari977e4442013-07-05 00:24:08 -0300378 }
379
Antti Palosaari34599b92013-07-08 22:31:47 -0300380 /* next sample (sample = sample + i * 384) */
381 s->next_sample = sample_num[i_max - 1] + 384;
Antti Palosaari00e049b2013-08-07 15:06:29 -0300382
Antti Palosaari977e4442013-07-05 00:24:08 -0300383 return dst_len;
384}
385
386/*
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300387 * +===========================================================================
388 * | 00-1023 | USB packet type '336'
389 * +===========================================================================
390 * | 00- 03 | sequence number of first sample in that USB packet
391 * +---------------------------------------------------------------------------
392 * | 04- 15 | garbage
393 * +---------------------------------------------------------------------------
394 * | 16-1023 | samples
395 * +---------------------------------------------------------------------------
396 * signed 12-bit sample
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300397 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300398static int msi3101_convert_stream_336(struct msi3101_state *s, u8 *dst,
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300399 u8 *src, unsigned int src_len)
400{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300401 int i, i_max, dst_len = 0;
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300402 u32 sample_num[3];
403
404 /* There could be 1-3 1024 bytes URB frames */
405 i_max = src_len / 1024;
406
407 for (i = 0; i < i_max; i++) {
408 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
409 if (i == 0 && s->next_sample != sample_num[0]) {
410 dev_dbg_ratelimited(&s->udev->dev,
411 "%d samples lost, %d %08x:%08x\n",
412 sample_num[0] - s->next_sample,
413 src_len, s->next_sample, sample_num[0]);
414 }
415
416 /*
417 * Dump all unknown 'garbage' data - maybe we will discover
418 * someday if there is something rational...
419 */
420 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
421
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300422 /* 336 x I+Q samples */
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300423 src += 16;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300424 memcpy(dst, src, 1008);
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300425 src += 1008;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300426 dst += 1008;
427 dst_len += 1008;
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300428 }
429
430 /* calculate samping rate and output it in 10 seconds intervals */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300431 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300432 unsigned long jiffies_now = jiffies;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300433 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300434 unsigned int samples = sample_num[i_max - 1] - s->sample;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300435 s->jiffies_next = jiffies_now;
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300436 s->sample = sample_num[i_max - 1];
437 dev_dbg(&s->udev->dev,
438 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
439 src_len, samples, msecs,
440 samples * 1000UL / msecs);
441 }
442
443 /* next sample (sample = sample + i * 336) */
444 s->next_sample = sample_num[i_max - 1] + 336;
445
446 return dst_len;
447}
448
449/*
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300450 * +===========================================================================
451 * | 00-1023 | USB packet type '252'
452 * +===========================================================================
453 * | 00- 03 | sequence number of first sample in that USB packet
454 * +---------------------------------------------------------------------------
455 * | 04- 15 | garbage
456 * +---------------------------------------------------------------------------
457 * | 16-1023 | samples
458 * +---------------------------------------------------------------------------
459 * signed 14-bit sample
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300460 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300461static int msi3101_convert_stream_252(struct msi3101_state *s, u8 *dst,
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300462 u8 *src, unsigned int src_len)
463{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300464 int i, i_max, dst_len = 0;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300465 u32 sample_num[3];
466
467 /* There could be 1-3 1024 bytes URB frames */
468 i_max = src_len / 1024;
469
470 for (i = 0; i < i_max; i++) {
471 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
472 if (i == 0 && s->next_sample != sample_num[0]) {
473 dev_dbg_ratelimited(&s->udev->dev,
474 "%d samples lost, %d %08x:%08x\n",
475 sample_num[0] - s->next_sample,
476 src_len, s->next_sample, sample_num[0]);
477 }
478
479 /*
480 * Dump all unknown 'garbage' data - maybe we will discover
481 * someday if there is something rational...
482 */
483 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
484
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300485 /* 252 x I+Q samples */
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300486 src += 16;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300487 memcpy(dst, src, 1008);
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300488 src += 1008;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300489 dst += 1008;
490 dst_len += 1008;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300491 }
492
493 /* calculate samping rate and output it in 10 seconds intervals */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300494 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300495 unsigned long jiffies_now = jiffies;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300496 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300497 unsigned int samples = sample_num[i_max - 1] - s->sample;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300498 s->jiffies_next = jiffies_now;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300499 s->sample = sample_num[i_max - 1];
500 dev_dbg(&s->udev->dev,
501 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
502 src_len, samples, msecs,
503 samples * 1000UL / msecs);
504 }
505
506 /* next sample (sample = sample + i * 252) */
507 s->next_sample = sample_num[i_max - 1] + 252;
508
509 return dst_len;
510}
511
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300512static int msi3101_convert_stream_252_u16(struct msi3101_state *s, u8 *dst,
513 u8 *src, unsigned int src_len)
514{
515 int i, j, i_max, dst_len = 0;
516 u32 sample_num[3];
517 u16 *u16dst = (u16 *) dst;
518 struct {signed int x:14;} se;
519
520 /* There could be 1-3 1024 bytes URB frames */
521 i_max = src_len / 1024;
522
523 for (i = 0; i < i_max; i++) {
524 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
525 if (i == 0 && s->next_sample != sample_num[0]) {
526 dev_dbg_ratelimited(&s->udev->dev,
527 "%d samples lost, %d %08x:%08x\n",
528 sample_num[0] - s->next_sample,
529 src_len, s->next_sample, sample_num[0]);
530 }
531
532 /*
533 * Dump all unknown 'garbage' data - maybe we will discover
534 * someday if there is something rational...
535 */
536 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
537
538 /* 252 x I+Q samples */
539 src += 16;
540
541 for (j = 0; j < 1008; j += 4) {
542 unsigned int usample[2];
543 int ssample[2];
544
545 usample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
546 usample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
547
548 /* sign extension from 14-bit to signed int */
549 ssample[0] = se.x = usample[0];
550 ssample[1] = se.x = usample[1];
551
552 /* from signed to unsigned */
553 usample[0] = ssample[0] + 8192;
554 usample[1] = ssample[1] + 8192;
555
556 /* from 14-bit to 16-bit */
557 *u16dst++ = (usample[0] << 2) | (usample[0] >> 12);
558 *u16dst++ = (usample[1] << 2) | (usample[1] >> 12);
559 }
560
561 src += 1008;
562 dst += 1008;
563 dst_len += 1008;
564 }
565
566 /* calculate samping rate and output it in 10 seconds intervals */
567 if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
568#define MSECS 10000UL
569 unsigned int samples = sample_num[i_max - 1] - s->sample;
570 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
571 s->sample = sample_num[i_max - 1];
572 dev_dbg(&s->udev->dev,
573 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
574 src_len, samples, MSECS,
575 samples * 1000UL / MSECS);
576 }
577
578 /* next sample (sample = sample + i * 252) */
579 s->next_sample = sample_num[i_max - 1] + 252;
580
581 return dst_len;
582}
583
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300584/*
Antti Palosaari977e4442013-07-05 00:24:08 -0300585 * This gets called for the Isochronous pipe (stream). This is done in interrupt
586 * time, so it has to be fast, not crash, and not stall. Neat.
587 */
588static void msi3101_isoc_handler(struct urb *urb)
589{
590 struct msi3101_state *s = (struct msi3101_state *)urb->context;
591 int i, flen, fstatus;
592 unsigned char *iso_buf = NULL;
593 struct msi3101_frame_buf *fbuf;
594
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300595 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
596 urb->status == -ESHUTDOWN)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300597 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
598 urb, urb->status == -ENOENT ? "" : "a");
599 return;
600 }
601
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300602 if (unlikely(urb->status != 0)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300603 dev_dbg(&s->udev->dev,
604 "msi3101_isoc_handler() called with status %d\n",
605 urb->status);
606 /* Give up after a number of contiguous errors */
607 if (++s->isoc_errors > MAX_ISOC_ERRORS)
608 dev_dbg(&s->udev->dev,
609 "Too many ISOC errors, bailing out\n");
610 goto handler_end;
611 } else {
612 /* Reset ISOC error counter. We did get here, after all. */
613 s->isoc_errors = 0;
614 }
615
616 /* Compact data */
617 for (i = 0; i < urb->number_of_packets; i++) {
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300618 void *ptr;
619
Antti Palosaari977e4442013-07-05 00:24:08 -0300620 /* Check frame error */
621 fstatus = urb->iso_frame_desc[i].status;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300622 if (unlikely(fstatus)) {
Antti Palosaari00e049b2013-08-07 15:06:29 -0300623 dev_dbg_ratelimited(&s->udev->dev,
Antti Palosaari977e4442013-07-05 00:24:08 -0300624 "frame=%d/%d has error %d skipping\n",
625 i, urb->number_of_packets, fstatus);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300626 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300627 }
628
629 /* Check if that frame contains data */
630 flen = urb->iso_frame_desc[i].actual_length;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300631 if (unlikely(flen == 0))
632 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300633
634 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
635
636 /* Get free framebuffer */
637 fbuf = msi3101_get_next_fill_buf(s);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300638 if (unlikely(fbuf == NULL)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300639 s->vb_full++;
640 dev_dbg_ratelimited(&s->udev->dev,
641 "videobuf is full, %d packets dropped\n",
642 s->vb_full);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300643 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300644 }
645
646 /* fill framebuffer */
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300647 ptr = vb2_plane_vaddr(&fbuf->vb, 0);
648 flen = s->convert_stream(s, ptr, iso_buf, flen);
649 vb2_set_plane_payload(&fbuf->vb, 0, flen);
Antti Palosaari977e4442013-07-05 00:24:08 -0300650 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
Antti Palosaari977e4442013-07-05 00:24:08 -0300651 }
652
653handler_end:
654 i = usb_submit_urb(urb, GFP_ATOMIC);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300655 if (unlikely(i != 0))
Antti Palosaari977e4442013-07-05 00:24:08 -0300656 dev_dbg(&s->udev->dev,
657 "Error (%d) re-submitting urb in msi3101_isoc_handler\n",
658 i);
659}
660
661static void msi3101_iso_stop(struct msi3101_state *s)
662{
663 int i;
664 dev_dbg(&s->udev->dev, "%s:\n", __func__);
665
666 /* Unlinking ISOC buffers one by one */
667 for (i = 0; i < MAX_ISO_BUFS; i++) {
668 if (s->urbs[i]) {
669 dev_dbg(&s->udev->dev, "Unlinking URB %p\n",
670 s->urbs[i]);
671 usb_kill_urb(s->urbs[i]);
672 }
673 }
674}
675
676static void msi3101_iso_free(struct msi3101_state *s)
677{
678 int i;
679 dev_dbg(&s->udev->dev, "%s:\n", __func__);
680
681 /* Freeing ISOC buffers one by one */
682 for (i = 0; i < MAX_ISO_BUFS; i++) {
683 if (s->urbs[i]) {
684 dev_dbg(&s->udev->dev, "Freeing URB\n");
685 if (s->urbs[i]->transfer_buffer) {
686 usb_free_coherent(s->udev,
687 s->urbs[i]->transfer_buffer_length,
688 s->urbs[i]->transfer_buffer,
689 s->urbs[i]->transfer_dma);
690 }
691 usb_free_urb(s->urbs[i]);
692 s->urbs[i] = NULL;
693 }
694 }
695}
696
697/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
698static void msi3101_isoc_cleanup(struct msi3101_state *s)
699{
700 dev_dbg(&s->udev->dev, "%s:\n", __func__);
701
702 msi3101_iso_stop(s);
703 msi3101_iso_free(s);
704}
705
706/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
707static int msi3101_isoc_init(struct msi3101_state *s)
708{
709 struct usb_device *udev;
710 struct urb *urb;
711 int i, j, ret;
712 dev_dbg(&s->udev->dev, "%s:\n", __func__);
713
714 s->isoc_errors = 0;
715 udev = s->udev;
716
717 ret = usb_set_interface(s->udev, 0, 1);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300718 if (ret)
Antti Palosaari977e4442013-07-05 00:24:08 -0300719 return ret;
720
721 /* Allocate and init Isochronuous urbs */
722 for (i = 0; i < MAX_ISO_BUFS; i++) {
723 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
724 if (urb == NULL) {
725 dev_err(&s->udev->dev,
726 "Failed to allocate urb %d\n", i);
727 msi3101_isoc_cleanup(s);
728 return -ENOMEM;
729 }
730 s->urbs[i] = urb;
731 dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb);
732
733 urb->interval = 1;
734 urb->dev = udev;
735 urb->pipe = usb_rcvisocpipe(udev, 0x81);
736 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
737 urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE,
738 GFP_KERNEL, &urb->transfer_dma);
739 if (urb->transfer_buffer == NULL) {
740 dev_err(&s->udev->dev,
741 "Failed to allocate urb buffer %d\n",
742 i);
743 msi3101_isoc_cleanup(s);
744 return -ENOMEM;
745 }
746 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
747 urb->complete = msi3101_isoc_handler;
748 urb->context = s;
749 urb->start_frame = 0;
750 urb->number_of_packets = ISO_FRAMES_PER_DESC;
751 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
752 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
753 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
754 }
755 }
756
757 /* link */
758 for (i = 0; i < MAX_ISO_BUFS; i++) {
759 ret = usb_submit_urb(s->urbs[i], GFP_KERNEL);
760 if (ret) {
761 dev_err(&s->udev->dev,
762 "isoc_init() submit_urb %d failed with error %d\n",
763 i, ret);
764 msi3101_isoc_cleanup(s);
765 return ret;
766 }
767 dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]);
768 }
769
770 /* All is done... */
771 return 0;
772}
773
774/* Must be called with vb_queue_lock hold */
775static void msi3101_cleanup_queued_bufs(struct msi3101_state *s)
776{
777 unsigned long flags = 0;
778 dev_dbg(&s->udev->dev, "%s:\n", __func__);
779
780 spin_lock_irqsave(&s->queued_bufs_lock, flags);
781 while (!list_empty(&s->queued_bufs)) {
782 struct msi3101_frame_buf *buf;
783
784 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf,
785 list);
786 list_del(&buf->list);
787 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
788 }
789 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
790}
791
792/* The user yanked out the cable... */
793static void msi3101_disconnect(struct usb_interface *intf)
794{
795 struct v4l2_device *v = usb_get_intfdata(intf);
796 struct msi3101_state *s =
797 container_of(v, struct msi3101_state, v4l2_dev);
798 dev_dbg(&s->udev->dev, "%s:\n", __func__);
799
800 mutex_lock(&s->vb_queue_lock);
801 mutex_lock(&s->v4l2_lock);
802 /* No need to keep the urbs around after disconnection */
803 s->udev = NULL;
Antti Palosaari977e4442013-07-05 00:24:08 -0300804 v4l2_device_disconnect(&s->v4l2_dev);
805 video_unregister_device(&s->vdev);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300806 spi_unregister_master(s->master);
Antti Palosaari977e4442013-07-05 00:24:08 -0300807 mutex_unlock(&s->v4l2_lock);
808 mutex_unlock(&s->vb_queue_lock);
809
810 v4l2_device_put(&s->v4l2_dev);
811}
812
813static int msi3101_querycap(struct file *file, void *fh,
814 struct v4l2_capability *cap)
815{
816 struct msi3101_state *s = video_drvdata(file);
817 dev_dbg(&s->udev->dev, "%s:\n", __func__);
818
819 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
820 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
821 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300822 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
823 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
Antti Palosaari977e4442013-07-05 00:24:08 -0300824 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
825 return 0;
826}
827
Antti Palosaari977e4442013-07-05 00:24:08 -0300828/* Videobuf2 operations */
829static int msi3101_queue_setup(struct vb2_queue *vq,
830 const struct v4l2_format *fmt, unsigned int *nbuffers,
831 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
832{
833 struct msi3101_state *s = vb2_get_drv_priv(vq);
834 dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers);
835
836 /* Absolute min and max number of buffers available for mmap() */
Antti Palosaarid287e4e2014-02-26 20:48:23 -0300837 *nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32);
Antti Palosaari977e4442013-07-05 00:24:08 -0300838 *nplanes = 1;
Antti Palosaaric0c72032013-09-15 13:21:20 -0300839 /*
840 * 3, wMaxPacketSize 3x 1024 bytes
841 * 504, max IQ sample pairs per 1024 frame
842 * 2, two samples, I and Q
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300843 * 2, 16-bit is enough for single sample
Antti Palosaaric0c72032013-09-15 13:21:20 -0300844 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300845 sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 2);
Antti Palosaari977e4442013-07-05 00:24:08 -0300846 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
847 __func__, *nbuffers, sizes[0]);
848 return 0;
849}
850
Antti Palosaari977e4442013-07-05 00:24:08 -0300851static void msi3101_buf_queue(struct vb2_buffer *vb)
852{
853 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
854 struct msi3101_frame_buf *buf =
855 container_of(vb, struct msi3101_frame_buf, vb);
856 unsigned long flags = 0;
857
858 /* Check the device has not disconnected between prep and queuing */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300859 if (unlikely(!s->udev)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300860 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
861 return;
862 }
863
864 spin_lock_irqsave(&s->queued_bufs_lock, flags);
865 list_add_tail(&buf->list, &s->queued_bufs);
866 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
867}
868
869#define CMD_WREG 0x41
870#define CMD_START_STREAMING 0x43
871#define CMD_STOP_STREAMING 0x45
872#define CMD_READ_UNKNOW 0x48
873
874#define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \
875 char *direction; \
876 if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
877 direction = ">>>"; \
878 else \
879 direction = "<<<"; \
880 dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
881 "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \
882 _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \
883}
884
885static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data)
886{
887 int ret;
888 u8 request = cmd;
889 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
890 u16 value = (data >> 0) & 0xffff;
891 u16 index = (data >> 16) & 0xffff;
892
893 msi3101_dbg_usb_control_msg(s->udev,
894 request, requesttype, value, index, NULL, 0);
895
Antti Palosaarid0fadf42013-07-11 12:19:32 -0300896 ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0),
Antti Palosaari977e4442013-07-05 00:24:08 -0300897 request, requesttype, value, index, NULL, 0, 2000);
898
899 if (ret)
900 dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n",
901 __func__, ret, cmd, data);
902
903 return ret;
904};
905
Antti Palosaari977e4442013-07-05 00:24:08 -0300906#define F_REF 24000000
Antti Palosaarib1520852013-07-09 02:44:56 -0300907#define DIV_R_IN 2
Antti Palosaari977e4442013-07-05 00:24:08 -0300908static int msi3101_set_usb_adc(struct msi3101_state *s)
909{
Antti Palosaari22ca6802013-07-31 21:48:51 -0300910 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300911 u32 reg3, reg4, reg7;
Antti Palosaari2e68f842014-02-02 23:42:00 -0300912 struct v4l2_ctrl *bandwidth_auto;
913 struct v4l2_ctrl *bandwidth;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300914
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300915 f_sr = s->f_adc;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300916
Antti Palosaari2e68f842014-02-02 23:42:00 -0300917 /* set tuner, subdev, filters according to sampling rate */
918 bandwidth_auto = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300919 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
920 bandwidth = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
921 v4l2_ctrl_s_ctrl(bandwidth, s->f_adc);
922 }
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300923
924 /* select stream format */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300925 switch (s->pixelformat) {
926 case V4L2_SDR_FMT_CU8:
927 s->convert_stream = msi3101_convert_stream_504_u8;
928 reg7 = 0x000c9407;
929 break;
930 case V4L2_SDR_FMT_CU16LE:
931 s->convert_stream = msi3101_convert_stream_252_u16;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300932 reg7 = 0x00009407;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300933 break;
Antti Palosaari3912eb62014-07-13 22:27:21 -0300934 case V4L2_SDR_FMT_CS8:
Antti Palosaari61198df2013-08-08 15:39:33 -0300935 s->convert_stream = msi3101_convert_stream_504;
936 reg7 = 0x000c9407;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300937 break;
938 case V4L2_PIX_FMT_SDR_MSI2500_384:
939 s->convert_stream = msi3101_convert_stream_384;
940 reg7 = 0x0000a507;
941 break;
942 case V4L2_PIX_FMT_SDR_S12:
943 s->convert_stream = msi3101_convert_stream_336;
944 reg7 = 0x00008507;
945 break;
Antti Palosaari3912eb62014-07-13 22:27:21 -0300946 case V4L2_SDR_FMT_CS14LE:
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300947 s->convert_stream = msi3101_convert_stream_252;
948 reg7 = 0x00009407;
949 break;
950 default:
951 s->convert_stream = msi3101_convert_stream_504_u8;
952 reg7 = 0x000c9407;
953 break;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300954 }
955
Antti Palosaari977e4442013-07-05 00:24:08 -0300956 /*
Antti Palosaarib1520852013-07-09 02:44:56 -0300957 * Synthesizer config is just a educated guess...
Antti Palosaari977e4442013-07-05 00:24:08 -0300958 *
Antti Palosaari977e4442013-07-05 00:24:08 -0300959 * [7:0] 0x03, register address
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300960 * [8] 1, power control
961 * [9] ?, power control
Antti Palosaari977e4442013-07-05 00:24:08 -0300962 * [12:10] output divider
963 * [13] 0 ?
964 * [14] 0 ?
Antti Palosaari22ca6802013-07-31 21:48:51 -0300965 * [15] fractional MSB, bit 20
Antti Palosaari977e4442013-07-05 00:24:08 -0300966 * [16:19] N
967 * [23:20] ?
968 * [24:31] 0x01
969 *
970 * output divider
971 * val div
972 * 0 - (invalid)
Antti Palosaarib1520852013-07-09 02:44:56 -0300973 * 1 4
974 * 2 6
975 * 3 8
976 * 4 10
977 * 5 12
978 * 6 14
979 * 7 16
980 *
981 * VCO 202000000 - 720000000++
Antti Palosaari977e4442013-07-05 00:24:08 -0300982 */
Antti Palosaaric5a431d2013-08-05 21:33:31 -0300983 reg3 = 0x01000303;
Antti Palosaari22ca6802013-07-31 21:48:51 -0300984 reg4 = 0x00000004;
Antti Palosaari977e4442013-07-05 00:24:08 -0300985
Antti Palosaaric5a431d2013-08-05 21:33:31 -0300986 /* XXX: Filters? AGC? */
987 if (f_sr < 6000000)
988 reg3 |= 0x1 << 20;
989 else if (f_sr < 7000000)
990 reg3 |= 0x5 << 20;
991 else if (f_sr < 8500000)
992 reg3 |= 0x9 << 20;
993 else
994 reg3 |= 0xd << 20;
995
Antti Palosaarib1520852013-07-09 02:44:56 -0300996 for (div_r_out = 4; div_r_out < 16; div_r_out += 2) {
997 f_vco = f_sr * div_r_out * 12;
998 dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n",
999 __func__, div_r_out, f_vco);
1000 if (f_vco >= 202000000)
Antti Palosaari977e4442013-07-05 00:24:08 -03001001 break;
1002 }
1003
Antti Palosaarib1520852013-07-09 02:44:56 -03001004 div_n = f_vco / (F_REF * DIV_R_IN);
1005 div_m = f_vco % (F_REF * DIV_R_IN);
Antti Palosaari22ca6802013-07-31 21:48:51 -03001006 fract = 0x200000ul * div_m / (F_REF * DIV_R_IN);
Antti Palosaarib1520852013-07-09 02:44:56 -03001007
Antti Palosaari977e4442013-07-05 00:24:08 -03001008 reg3 |= div_n << 16;
Antti Palosaarib1520852013-07-09 02:44:56 -03001009 reg3 |= (div_r_out / 2 - 1) << 10;
Antti Palosaari22ca6802013-07-31 21:48:51 -03001010 reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */
1011 reg4 |= ((fract >> 0) & 0x0fffff) << 8; /* [19:0] */
Antti Palosaari977e4442013-07-05 00:24:08 -03001012
Antti Palosaarib1520852013-07-09 02:44:56 -03001013 dev_dbg(&s->udev->dev,
Antti Palosaari22ca6802013-07-31 21:48:51 -03001014 "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n",
1015 __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg3, reg4);
Antti Palosaari977e4442013-07-05 00:24:08 -03001016
1017 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008);
1018 if (ret)
1019 goto err;
1020
1021 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05);
1022 if (ret)
1023 goto err;
1024
1025 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000);
1026 if (ret)
1027 goto err;
1028
1029 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102);
1030 if (ret)
1031 goto err;
1032
1033 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008);
1034 if (ret)
1035 goto err;
1036
Antti Palosaari554cbfb2013-08-05 20:43:36 -03001037 ret = msi3101_ctrl_msg(s, CMD_WREG, reg7);
Antti Palosaari977e4442013-07-05 00:24:08 -03001038 if (ret)
1039 goto err;
1040
1041 ret = msi3101_ctrl_msg(s, CMD_WREG, reg4);
1042 if (ret)
1043 goto err;
1044
1045 ret = msi3101_ctrl_msg(s, CMD_WREG, reg3);
1046 if (ret)
1047 goto err;
1048err:
1049 return ret;
1050};
1051
Antti Palosaari977e4442013-07-05 00:24:08 -03001052static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
1053{
1054 struct msi3101_state *s = vb2_get_drv_priv(vq);
1055 int ret;
1056 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1057
1058 if (!s->udev)
1059 return -ENODEV;
1060
1061 if (mutex_lock_interruptible(&s->v4l2_lock))
1062 return -ERESTARTSYS;
1063
Antti Palosaari2e68f842014-02-02 23:42:00 -03001064 /* wake-up tuner */
1065 v4l2_subdev_call(s->v4l2_subdev, core, s_power, 1);
1066
Antti Palosaari977e4442013-07-05 00:24:08 -03001067 ret = msi3101_set_usb_adc(s);
1068
1069 ret = msi3101_isoc_init(s);
1070 if (ret)
1071 msi3101_cleanup_queued_bufs(s);
1072
1073 ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0);
1074
1075 mutex_unlock(&s->v4l2_lock);
1076
1077 return ret;
1078}
1079
Hans Verkuile37559b2014-04-17 02:47:21 -03001080static void msi3101_stop_streaming(struct vb2_queue *vq)
Antti Palosaari977e4442013-07-05 00:24:08 -03001081{
1082 struct msi3101_state *s = vb2_get_drv_priv(vq);
Hans Verkuile37559b2014-04-17 02:47:21 -03001083
Antti Palosaari977e4442013-07-05 00:24:08 -03001084 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1085
Hans Verkuile37559b2014-04-17 02:47:21 -03001086 mutex_lock(&s->v4l2_lock);
Antti Palosaari977e4442013-07-05 00:24:08 -03001087
Antti Palosaari977e4442013-07-05 00:24:08 -03001088 if (s->udev)
1089 msi3101_isoc_cleanup(s);
1090
1091 msi3101_cleanup_queued_bufs(s);
Antti Palosaarid0fadf42013-07-11 12:19:32 -03001092
1093 /* according to tests, at least 700us delay is required */
1094 msleep(20);
Hans Verkuile37559b2014-04-17 02:47:21 -03001095 if (!msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0)) {
1096 /* sleep USB IF / ADC */
1097 msi3101_ctrl_msg(s, CMD_WREG, 0x01000003);
1098 }
Antti Palosaarid0fadf42013-07-11 12:19:32 -03001099
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001100 /* sleep tuner */
Hans Verkuile37559b2014-04-17 02:47:21 -03001101 v4l2_subdev_call(s->v4l2_subdev, core, s_power, 0);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001102
Antti Palosaari977e4442013-07-05 00:24:08 -03001103 mutex_unlock(&s->v4l2_lock);
Antti Palosaari977e4442013-07-05 00:24:08 -03001104}
1105
1106static struct vb2_ops msi3101_vb2_ops = {
1107 .queue_setup = msi3101_queue_setup,
Antti Palosaari977e4442013-07-05 00:24:08 -03001108 .buf_queue = msi3101_buf_queue,
1109 .start_streaming = msi3101_start_streaming,
1110 .stop_streaming = msi3101_stop_streaming,
1111 .wait_prepare = vb2_ops_wait_prepare,
1112 .wait_finish = vb2_ops_wait_finish,
1113};
1114
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001115static int msi3101_enum_fmt_sdr_cap(struct file *file, void *priv,
1116 struct v4l2_fmtdesc *f)
Antti Palosaari977e4442013-07-05 00:24:08 -03001117{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001118 struct msi3101_state *s = video_drvdata(file);
1119 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, f->index);
1120
Antti Palosaari3912eb62014-07-13 22:27:21 -03001121 if (f->index >= s->num_formats)
Antti Palosaari977e4442013-07-05 00:24:08 -03001122 return -EINVAL;
1123
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001124 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1125 f->pixelformat = formats[f->index].pixelformat;
Antti Palosaari977e4442013-07-05 00:24:08 -03001126
1127 return 0;
1128}
1129
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001130static int msi3101_g_fmt_sdr_cap(struct file *file, void *priv,
1131 struct v4l2_format *f)
Antti Palosaari977e4442013-07-05 00:24:08 -03001132{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001133 struct msi3101_state *s = video_drvdata(file);
1134 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1135 (char *)&s->pixelformat);
1136
Antti Palosaaric3509122014-03-10 16:30:44 -03001137 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001138 f->fmt.sdr.pixelformat = s->pixelformat;
Antti Palosaari977e4442013-07-05 00:24:08 -03001139
1140 return 0;
1141}
1142
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001143static int msi3101_s_fmt_sdr_cap(struct file *file, void *priv,
1144 struct v4l2_format *f)
Antti Palosaari977e4442013-07-05 00:24:08 -03001145{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001146 struct msi3101_state *s = video_drvdata(file);
1147 struct vb2_queue *q = &s->vb_queue;
1148 int i;
1149 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1150 (char *)&f->fmt.sdr.pixelformat);
1151
1152 if (vb2_is_busy(q))
1153 return -EBUSY;
1154
Antti Palosaaric3509122014-03-10 16:30:44 -03001155 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari3912eb62014-07-13 22:27:21 -03001156 for (i = 0; i < s->num_formats; i++) {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001157 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1158 s->pixelformat = f->fmt.sdr.pixelformat;
1159 return 0;
1160 }
1161 }
1162
1163 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1164 s->pixelformat = formats[0].pixelformat;
1165
1166 return 0;
Antti Palosaari977e4442013-07-05 00:24:08 -03001167}
1168
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001169static int msi3101_try_fmt_sdr_cap(struct file *file, void *priv,
1170 struct v4l2_format *f)
1171{
1172 struct msi3101_state *s = video_drvdata(file);
1173 int i;
1174 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1175 (char *)&f->fmt.sdr.pixelformat);
1176
Antti Palosaaric3509122014-03-10 16:30:44 -03001177 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari3912eb62014-07-13 22:27:21 -03001178 for (i = 0; i < s->num_formats; i++) {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001179 if (formats[i].pixelformat == f->fmt.sdr.pixelformat)
1180 return 0;
1181 }
1182
1183 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1184
1185 return 0;
1186}
1187
1188static int msi3101_s_tuner(struct file *file, void *priv,
Antti Palosaari977e4442013-07-05 00:24:08 -03001189 const struct v4l2_tuner *v)
1190{
1191 struct msi3101_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001192 int ret;
1193 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
Antti Palosaari977e4442013-07-05 00:24:08 -03001194
Antti Palosaari2e68f842014-02-02 23:42:00 -03001195 if (v->index == 0)
1196 ret = 0;
1197 else if (v->index == 1)
1198 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_tuner, v);
1199 else
1200 ret = -EINVAL;
1201
1202 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -03001203}
1204
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001205static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Antti Palosaari977e4442013-07-05 00:24:08 -03001206{
1207 struct msi3101_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001208 int ret;
1209 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
Antti Palosaari977e4442013-07-05 00:24:08 -03001210
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001211 if (v->index == 0) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001212 strlcpy(v->name, "Mirics MSi2500", sizeof(v->name));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001213 v->type = V4L2_TUNER_ADC;
1214 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1215 v->rangelow = 1200000;
1216 v->rangehigh = 15000000;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001217 ret = 0;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001218 } else if (v->index == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001219 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_tuner, v);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001220 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001221 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001222 }
Antti Palosaari977e4442013-07-05 00:24:08 -03001223
Antti Palosaari2e68f842014-02-02 23:42:00 -03001224 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -03001225}
1226
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001227static int msi3101_g_frequency(struct file *file, void *priv,
1228 struct v4l2_frequency *f)
1229{
1230 struct msi3101_state *s = video_drvdata(file);
1231 int ret = 0;
1232 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n",
1233 __func__, f->tuner, f->type);
1234
Antti Palosaari2e68f842014-02-02 23:42:00 -03001235 if (f->tuner == 0) {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001236 f->frequency = s->f_adc;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001237 ret = 0;
1238 } else if (f->tuner == 1) {
Antti Palosaaric3509122014-03-10 16:30:44 -03001239 f->type = V4L2_TUNER_RF;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001240 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_frequency, f);
1241 } else {
1242 ret = -EINVAL;
1243 }
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001244
1245 return ret;
1246}
1247
1248static int msi3101_s_frequency(struct file *file, void *priv,
Antti Palosaari977e4442013-07-05 00:24:08 -03001249 const struct v4l2_frequency *f)
1250{
1251 struct msi3101_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001252 int ret;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001253 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n",
1254 __func__, f->tuner, f->type, f->frequency);
Antti Palosaari977e4442013-07-05 00:24:08 -03001255
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001256 if (f->tuner == 0) {
1257 s->f_adc = clamp_t(unsigned int, f->frequency,
Antti Palosaari2e68f842014-02-02 23:42:00 -03001258 bands[0].rangelow,
1259 bands[0].rangehigh);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001260 dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n",
1261 __func__, s->f_adc);
1262 ret = msi3101_set_usb_adc(s);
1263 } else if (f->tuner == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001264 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_frequency, f);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001265 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001266 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001267 }
1268
1269 return ret;
1270}
1271
1272static int msi3101_enum_freq_bands(struct file *file, void *priv,
1273 struct v4l2_frequency_band *band)
1274{
1275 struct msi3101_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001276 int ret;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001277 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n",
1278 __func__, band->tuner, band->type, band->index);
1279
1280 if (band->tuner == 0) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001281 if (band->index >= ARRAY_SIZE(bands)) {
1282 ret = -EINVAL;
1283 } else {
1284 *band = bands[band->index];
1285 ret = 0;
1286 }
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001287 } else if (band->tuner == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001288 ret = v4l2_subdev_call(s->v4l2_subdev, tuner,
1289 enum_freq_bands, band);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001290 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001291 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001292 }
1293
Antti Palosaari2e68f842014-02-02 23:42:00 -03001294 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -03001295}
1296
Fengguang Wu6648ef12013-09-02 19:09:53 -03001297static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
Antti Palosaari977e4442013-07-05 00:24:08 -03001298 .vidioc_querycap = msi3101_querycap,
1299
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001300 .vidioc_enum_fmt_sdr_cap = msi3101_enum_fmt_sdr_cap,
1301 .vidioc_g_fmt_sdr_cap = msi3101_g_fmt_sdr_cap,
1302 .vidioc_s_fmt_sdr_cap = msi3101_s_fmt_sdr_cap,
1303 .vidioc_try_fmt_sdr_cap = msi3101_try_fmt_sdr_cap,
Antti Palosaari977e4442013-07-05 00:24:08 -03001304
1305 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1306 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1307 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1308 .vidioc_querybuf = vb2_ioctl_querybuf,
1309 .vidioc_qbuf = vb2_ioctl_qbuf,
1310 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1311
1312 .vidioc_streamon = vb2_ioctl_streamon,
1313 .vidioc_streamoff = vb2_ioctl_streamoff,
1314
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001315 .vidioc_g_tuner = msi3101_g_tuner,
1316 .vidioc_s_tuner = msi3101_s_tuner,
1317
1318 .vidioc_g_frequency = msi3101_g_frequency,
1319 .vidioc_s_frequency = msi3101_s_frequency,
1320 .vidioc_enum_freq_bands = msi3101_enum_freq_bands,
Antti Palosaari977e4442013-07-05 00:24:08 -03001321
1322 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1323 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1324 .vidioc_log_status = v4l2_ctrl_log_status,
1325};
1326
1327static const struct v4l2_file_operations msi3101_fops = {
1328 .owner = THIS_MODULE,
1329 .open = v4l2_fh_open,
1330 .release = vb2_fop_release,
1331 .read = vb2_fop_read,
1332 .poll = vb2_fop_poll,
1333 .mmap = vb2_fop_mmap,
1334 .unlocked_ioctl = video_ioctl2,
1335};
1336
1337static struct video_device msi3101_template = {
1338 .name = "Mirics MSi3101 SDR Dongle",
1339 .release = video_device_release_empty,
1340 .fops = &msi3101_fops,
1341 .ioctl_ops = &msi3101_ioctl_ops,
1342};
1343
Antti Palosaari977e4442013-07-05 00:24:08 -03001344static void msi3101_video_release(struct v4l2_device *v)
1345{
1346 struct msi3101_state *s =
1347 container_of(v, struct msi3101_state, v4l2_dev);
1348
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001349 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaari977e4442013-07-05 00:24:08 -03001350 v4l2_device_unregister(&s->v4l2_dev);
1351 kfree(s);
1352}
1353
Antti Palosaari2e68f842014-02-02 23:42:00 -03001354static int msi3101_transfer_one_message(struct spi_master *master,
1355 struct spi_message *m)
1356{
1357 struct msi3101_state *s = spi_master_get_devdata(master);
1358 struct spi_transfer *t;
1359 int ret = 0;
1360 u32 data;
1361
1362 list_for_each_entry(t, &m->transfers, transfer_list) {
1363 dev_dbg(&s->udev->dev, "%s: msg=%*ph\n",
1364 __func__, t->len, t->tx_buf);
1365 data = 0x09; /* reg 9 is SPI adapter */
1366 data |= ((u8 *)t->tx_buf)[0] << 8;
1367 data |= ((u8 *)t->tx_buf)[1] << 16;
1368 data |= ((u8 *)t->tx_buf)[2] << 24;
1369 ret = msi3101_ctrl_msg(s, CMD_WREG, data);
1370 }
1371
1372 m->status = ret;
1373 spi_finalize_current_message(master);
1374 return ret;
1375}
1376
Antti Palosaari977e4442013-07-05 00:24:08 -03001377static int msi3101_probe(struct usb_interface *intf,
1378 const struct usb_device_id *id)
1379{
1380 struct usb_device *udev = interface_to_usbdev(intf);
1381 struct msi3101_state *s = NULL;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001382 struct v4l2_subdev *sd;
1383 struct spi_master *master;
Antti Palosaari977e4442013-07-05 00:24:08 -03001384 int ret;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001385 static struct spi_board_info board_info = {
1386 .modalias = "msi001",
1387 .bus_num = 0,
1388 .chip_select = 0,
1389 .max_speed_hz = 12000000,
Antti Palosaari977e4442013-07-05 00:24:08 -03001390 };
1391
1392 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1393 if (s == NULL) {
1394 pr_err("Could not allocate memory for msi3101_state\n");
1395 return -ENOMEM;
1396 }
1397
1398 mutex_init(&s->v4l2_lock);
1399 mutex_init(&s->vb_queue_lock);
1400 spin_lock_init(&s->queued_bufs_lock);
1401 INIT_LIST_HEAD(&s->queued_bufs);
Antti Palosaari977e4442013-07-05 00:24:08 -03001402 s->udev = udev;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001403 s->f_adc = bands[0].rangelow;
Antti Palosaari3912eb62014-07-13 22:27:21 -03001404 s->pixelformat = formats[0].pixelformat;
1405 s->num_formats = NUM_FORMATS;
1406 if (msi3101_emulated_fmt == false)
1407 s->num_formats -= 2;
Antti Palosaari977e4442013-07-05 00:24:08 -03001408
1409 /* Init videobuf2 queue structure */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001410 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
Antti Palosaari977e4442013-07-05 00:24:08 -03001411 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1412 s->vb_queue.drv_priv = s;
1413 s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
1414 s->vb_queue.ops = &msi3101_vb2_ops;
1415 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03001416 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Antti Palosaari977e4442013-07-05 00:24:08 -03001417 ret = vb2_queue_init(&s->vb_queue);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001418 if (ret) {
Antti Palosaari977e4442013-07-05 00:24:08 -03001419 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
1420 goto err_free_mem;
1421 }
1422
1423 /* Init video_device structure */
1424 s->vdev = msi3101_template;
1425 s->vdev.queue = &s->vb_queue;
1426 s->vdev.queue->lock = &s->vb_queue_lock;
Antti Palosaari977e4442013-07-05 00:24:08 -03001427 video_set_drvdata(&s->vdev, s);
1428
Antti Palosaari977e4442013-07-05 00:24:08 -03001429 /* Register the v4l2_device structure */
1430 s->v4l2_dev.release = msi3101_video_release;
1431 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1432 if (ret) {
1433 dev_err(&s->udev->dev,
1434 "Failed to register v4l2-device (%d)\n", ret);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001435 goto err_free_mem;
1436 }
1437
1438 /* SPI master adapter */
1439 master = spi_alloc_master(&s->udev->dev, 0);
1440 if (master == NULL) {
1441 ret = -ENOMEM;
1442 goto err_unregister_v4l2_dev;
1443 }
1444
1445 s->master = master;
1446 master->bus_num = 0;
1447 master->num_chipselect = 1;
1448 master->transfer_one_message = msi3101_transfer_one_message;
1449 spi_master_set_devdata(master, s);
1450 ret = spi_register_master(master);
1451 if (ret) {
1452 spi_master_put(master);
1453 goto err_unregister_v4l2_dev;
1454 }
1455
1456 /* load v4l2 subdevice */
1457 sd = v4l2_spi_new_subdev(&s->v4l2_dev, master, &board_info);
1458 s->v4l2_subdev = sd;
1459 if (sd == NULL) {
1460 dev_err(&s->udev->dev, "cannot get v4l2 subdevice\n");
1461 ret = -ENODEV;
1462 goto err_unregister_master;
1463 }
1464
1465 /* Register controls */
1466 v4l2_ctrl_handler_init(&s->hdl, 0);
1467 if (s->hdl.error) {
1468 ret = s->hdl.error;
1469 dev_err(&s->udev->dev, "Could not initialize controls\n");
Antti Palosaari977e4442013-07-05 00:24:08 -03001470 goto err_free_controls;
1471 }
1472
Antti Palosaari2e68f842014-02-02 23:42:00 -03001473 /* currently all controls are from subdev */
1474 v4l2_ctrl_add_handler(&s->hdl, sd->ctrl_handler, NULL);
1475
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001476 s->v4l2_dev.ctrl_handler = &s->hdl;
Antti Palosaari977e4442013-07-05 00:24:08 -03001477 s->vdev.v4l2_dev = &s->v4l2_dev;
1478 s->vdev.lock = &s->v4l2_lock;
1479
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001480 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
1481 if (ret) {
Antti Palosaari977e4442013-07-05 00:24:08 -03001482 dev_err(&s->udev->dev,
1483 "Failed to register as video device (%d)\n",
1484 ret);
1485 goto err_unregister_v4l2_dev;
1486 }
1487 dev_info(&s->udev->dev, "Registered as %s\n",
1488 video_device_node_name(&s->vdev));
1489
1490 return 0;
1491
Antti Palosaari977e4442013-07-05 00:24:08 -03001492err_free_controls:
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001493 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001494err_unregister_master:
1495 spi_unregister_master(s->master);
Antti Palosaari977e4442013-07-05 00:24:08 -03001496err_unregister_v4l2_dev:
1497 v4l2_device_unregister(&s->v4l2_dev);
Antti Palosaari977e4442013-07-05 00:24:08 -03001498err_free_mem:
1499 kfree(s);
1500 return ret;
1501}
1502
1503/* USB device ID list */
1504static struct usb_device_id msi3101_id_table[] = {
Antti Palosaari00e049b2013-08-07 15:06:29 -03001505 { USB_DEVICE(0x1df7, 0x2500) }, /* Mirics MSi3101 SDR Dongle */
Antti Palosaari42fc5b42013-07-17 15:05:27 -03001506 { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */
Antti Palosaari977e4442013-07-05 00:24:08 -03001507 { }
1508};
1509MODULE_DEVICE_TABLE(usb, msi3101_id_table);
1510
1511/* USB subsystem interface */
1512static struct usb_driver msi3101_driver = {
1513 .name = KBUILD_MODNAME,
1514 .probe = msi3101_probe,
1515 .disconnect = msi3101_disconnect,
1516 .id_table = msi3101_id_table,
1517};
1518
1519module_usb_driver(msi3101_driver);
1520
1521MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1522MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1523MODULE_LICENSE("GPL");