blob: 53aca380b28854c85c54b38aaee7654fa92f33d5 [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
37/*
38 * iConfiguration 0
39 * bInterfaceNumber 0
40 * bAlternateSetting 1
41 * bNumEndpoints 1
42 * bEndpointAddress 0x81 EP 1 IN
43 * bmAttributes 1
44 * Transfer Type Isochronous
45 * wMaxPacketSize 0x1400 3x 1024 bytes
46 * bInterval 1
47 */
48#define MAX_ISO_BUFS (8)
49#define ISO_FRAMES_PER_DESC (8)
50#define ISO_MAX_FRAME_SIZE (3 * 1024)
51#define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
Antti Palosaari977e4442013-07-05 00:24:08 -030052#define MAX_ISOC_ERRORS 20
53
Antti Palosaari00e049b2013-08-07 15:06:29 -030054/* TODO: These should be moved to V4L2 API */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030055#define V4L2_PIX_FMT_SDR_S8 v4l2_fourcc('D', 'S', '0', '8') /* signed 8-bit */
56#define V4L2_PIX_FMT_SDR_S12 v4l2_fourcc('D', 'S', '1', '2') /* signed 12-bit */
57#define V4L2_PIX_FMT_SDR_S14 v4l2_fourcc('D', 'S', '1', '4') /* signed 14-bit */
58#define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */
59
Antti Palosaari2e68f842014-02-02 23:42:00 -030060static const struct v4l2_frequency_band bands[] = {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030061 {
62 .tuner = 0,
63 .type = V4L2_TUNER_ADC,
64 .index = 0,
65 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
66 .rangelow = 1200000,
67 .rangehigh = 15000000,
68 },
69};
70
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030071/* stream formats */
72struct msi3101_format {
73 char *name;
74 u32 pixelformat;
75};
76
77/* format descriptions for capture and preview */
78static struct msi3101_format formats[] = {
79 {
80 .name = "IQ U8",
81 .pixelformat = V4L2_SDR_FMT_CU8,
82 }, {
83 .name = "IQ U16LE",
84 .pixelformat = V4L2_SDR_FMT_CU16LE,
85#if 0
86 }, {
87 .name = "8-bit signed",
88 .pixelformat = V4L2_PIX_FMT_SDR_S8,
89 }, {
90 .name = "10+2-bit signed",
91 .pixelformat = V4L2_PIX_FMT_SDR_MSI2500_384,
92 }, {
93 .name = "12-bit signed",
94 .pixelformat = V4L2_PIX_FMT_SDR_S12,
95 }, {
96 .name = "14-bit signed",
97 .pixelformat = V4L2_PIX_FMT_SDR_S14,
98#endif
99 },
100};
101
102static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
Antti Palosaari977e4442013-07-05 00:24:08 -0300103
104/* intermediate buffers with raw data from the USB device */
105struct msi3101_frame_buf {
106 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
107 struct list_head list;
Antti Palosaari977e4442013-07-05 00:24:08 -0300108};
109
110struct msi3101_state {
111 struct video_device vdev;
112 struct v4l2_device v4l2_dev;
Antti Palosaari2e68f842014-02-02 23:42:00 -0300113 struct v4l2_subdev *v4l2_subdev;
114 struct spi_master *master;
Antti Palosaari977e4442013-07-05 00:24:08 -0300115
116 /* videobuf2 queue and queued buffers list */
117 struct vb2_queue vb_queue;
118 struct list_head queued_bufs;
119 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
120
121 /* Note if taking both locks v4l2_lock must always be locked first! */
122 struct mutex v4l2_lock; /* Protects everything else */
123 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
124
125 /* Pointer to our usb_device, will be NULL after unplug */
126 struct usb_device *udev; /* Both mutexes most be hold when setting! */
127
Antti Palosaari2e68f842014-02-02 23:42:00 -0300128 unsigned int f_adc;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300129 u32 pixelformat;
130
Antti Palosaari977e4442013-07-05 00:24:08 -0300131 unsigned int isoc_errors; /* number of contiguous ISOC errors */
132 unsigned int vb_full; /* vb is full and packets dropped */
133
134 struct urb *urbs[MAX_ISO_BUFS];
Linus Torvalds3c83e612014-04-04 09:50:07 -0700135 int (*convert_stream)(struct msi3101_state *s, u8 *dst, u8 *src,
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300136 unsigned int src_len);
Antti Palosaari977e4442013-07-05 00:24:08 -0300137
138 /* Controls */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300139 struct v4l2_ctrl_handler hdl;
Antti Palosaari977e4442013-07-05 00:24:08 -0300140
Antti Palosaari34599b92013-07-08 22:31:47 -0300141 u32 next_sample; /* for track lost packets */
142 u32 sample; /* for sample rate calc */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300143 unsigned long jiffies_next;
Antti Palosaari02004682013-07-10 21:59:58 -0300144 unsigned int sample_ctrl_bit[4];
Antti Palosaari977e4442013-07-05 00:24:08 -0300145};
146
147/* Private functions */
148static struct msi3101_frame_buf *msi3101_get_next_fill_buf(
149 struct msi3101_state *s)
150{
151 unsigned long flags = 0;
152 struct msi3101_frame_buf *buf = NULL;
153
154 spin_lock_irqsave(&s->queued_bufs_lock, flags);
155 if (list_empty(&s->queued_bufs))
156 goto leave;
157
158 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list);
159 list_del(&buf->list);
160leave:
161 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
162 return buf;
163}
164
165/*
Antti Palosaari02004682013-07-10 21:59:58 -0300166 * +===========================================================================
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300167 * | 00-1023 | USB packet type '504'
168 * +===========================================================================
169 * | 00- 03 | sequence number of first sample in that USB packet
170 * +---------------------------------------------------------------------------
171 * | 04- 15 | garbage
172 * +---------------------------------------------------------------------------
173 * | 16-1023 | samples
174 * +---------------------------------------------------------------------------
175 * signed 8-bit sample
176 * 504 * 2 = 1008 samples
177 */
178static int msi3101_convert_stream_504(struct msi3101_state *s, u8 *dst,
179 u8 *src, unsigned int src_len)
180{
181 int i, i_max, dst_len = 0;
182 u32 sample_num[3];
183
184 /* There could be 1-3 1024 bytes URB frames */
185 i_max = src_len / 1024;
186
187 for (i = 0; i < i_max; i++) {
188 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
189 if (i == 0 && s->next_sample != sample_num[0]) {
190 dev_dbg_ratelimited(&s->udev->dev,
191 "%d samples lost, %d %08x:%08x\n",
192 sample_num[0] - s->next_sample,
193 src_len, s->next_sample, sample_num[0]);
194 }
195
196 /*
197 * Dump all unknown 'garbage' data - maybe we will discover
198 * someday if there is something rational...
199 */
200 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
201
202 /* 504 x I+Q samples */
203 src += 16;
204 memcpy(dst, src, 1008);
205 src += 1008;
206 dst += 1008;
207 dst_len += 1008;
208 }
209
210 /* calculate samping rate and output it in 10 seconds intervals */
211 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
212 unsigned long jiffies_now = jiffies;
213 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
214 unsigned int samples = sample_num[i_max - 1] - s->sample;
215 s->jiffies_next = jiffies_now;
216 s->sample = sample_num[i_max - 1];
217 dev_dbg(&s->udev->dev,
218 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
219 src_len, samples, msecs,
220 samples * 1000UL / msecs);
221 }
222
223 /* next sample (sample = sample + i * 504) */
224 s->next_sample = sample_num[i_max - 1] + 504;
225
226 return dst_len;
227}
228
229static int msi3101_convert_stream_504_u8(struct msi3101_state *s, u8 *dst,
230 u8 *src, unsigned int src_len)
231{
232 int i, j, i_max, dst_len = 0;
233 u32 sample_num[3];
234 s8 *s8src;
235 u8 *u8dst;
236
237 /* There could be 1-3 1024 bytes URB frames */
238 i_max = src_len / 1024;
239 u8dst = (u8 *) dst;
240
241 for (i = 0; i < i_max; i++) {
242 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
243 if (i == 0 && s->next_sample != sample_num[0]) {
244 dev_dbg_ratelimited(&s->udev->dev,
245 "%d samples lost, %d %08x:%08x\n",
246 sample_num[0] - s->next_sample,
247 src_len, s->next_sample, sample_num[0]);
248 }
249
250 /*
251 * Dump all unknown 'garbage' data - maybe we will discover
252 * someday if there is something rational...
253 */
254 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
255
256 /* 504 x I+Q samples */
257 src += 16;
258
259 s8src = (s8 *) src;
260 for (j = 0; j < 1008; j++)
261 *u8dst++ = *s8src++ + 128;
262
263 src += 1008;
264 dst += 1008;
265 dst_len += 1008;
266 }
267
268 /* calculate samping rate and output it in 10 seconds intervals */
269 if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
270#define MSECS 10000UL
271 unsigned int samples = sample_num[i_max - 1] - s->sample;
272 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
273 s->sample = sample_num[i_max - 1];
274 dev_dbg(&s->udev->dev,
275 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
276 src_len, samples, MSECS,
277 samples * 1000UL / MSECS);
278 }
279
280 /* next sample (sample = sample + i * 504) */
281 s->next_sample = sample_num[i_max - 1] + 504;
282
283 return dst_len;
284}
285
286/*
287 * +===========================================================================
Antti Palosaari61198df2013-08-08 15:39:33 -0300288 * | 00-1023 | USB packet type '384'
Antti Palosaari02004682013-07-10 21:59:58 -0300289 * +===========================================================================
290 * | 00- 03 | sequence number of first sample in that USB packet
291 * +---------------------------------------------------------------------------
292 * | 04- 15 | garbage
293 * +---------------------------------------------------------------------------
294 * | 16- 175 | samples
295 * +---------------------------------------------------------------------------
296 * | 176- 179 | control bits for previous samples
297 * +---------------------------------------------------------------------------
298 * | 180- 339 | samples
299 * +---------------------------------------------------------------------------
300 * | 340- 343 | control bits for previous samples
301 * +---------------------------------------------------------------------------
302 * | 344- 503 | samples
303 * +---------------------------------------------------------------------------
304 * | 504- 507 | control bits for previous samples
305 * +---------------------------------------------------------------------------
306 * | 508- 667 | samples
307 * +---------------------------------------------------------------------------
308 * | 668- 671 | control bits for previous samples
309 * +---------------------------------------------------------------------------
310 * | 672- 831 | samples
311 * +---------------------------------------------------------------------------
312 * | 832- 835 | control bits for previous samples
313 * +---------------------------------------------------------------------------
314 * | 836- 995 | samples
315 * +---------------------------------------------------------------------------
316 * | 996- 999 | control bits for previous samples
317 * +---------------------------------------------------------------------------
Antti Palosaari6450e502013-07-31 19:58:12 -0300318 * | 1000-1023 | garbage
Antti Palosaari02004682013-07-10 21:59:58 -0300319 * +---------------------------------------------------------------------------
320 *
321 * Bytes 4 - 7 could have some meaning?
322 *
323 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
324 * numbers. This results one 2-bit number for 8 samples. It is likely used for
325 * for bit shifting sample by given bits, increasing actual sampling resolution.
326 * Number 2 (0b10) was never seen.
327 *
328 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
329 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300330static int msi3101_convert_stream_384(struct msi3101_state *s, u8 *dst,
Antti Palosaari61198df2013-08-08 15:39:33 -0300331 u8 *src, unsigned int src_len)
332{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300333 int i, i_max, dst_len = 0;
Antti Palosaari34599b92013-07-08 22:31:47 -0300334 u32 sample_num[3];
Antti Palosaari00e049b2013-08-07 15:06:29 -0300335
Antti Palosaari977e4442013-07-05 00:24:08 -0300336 /* There could be 1-3 1024 bytes URB frames */
337 i_max = src_len / 1024;
338 for (i = 0; i < i_max; i++) {
Antti Palosaari34599b92013-07-08 22:31:47 -0300339 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
340 if (i == 0 && s->next_sample != sample_num[0]) {
Antti Palosaari00e049b2013-08-07 15:06:29 -0300341 dev_dbg_ratelimited(&s->udev->dev,
Antti Palosaari34599b92013-07-08 22:31:47 -0300342 "%d samples lost, %d %08x:%08x\n",
343 sample_num[0] - s->next_sample,
344 src_len, s->next_sample, sample_num[0]);
Antti Palosaari977e4442013-07-05 00:24:08 -0300345 }
Antti Palosaari6450e502013-07-31 19:58:12 -0300346
347 /*
348 * Dump all unknown 'garbage' data - maybe we will discover
349 * someday if there is something rational...
350 */
351 dev_dbg_ratelimited(&s->udev->dev,
352 "%*ph %*ph\n", 12, &src[4], 24, &src[1000]);
Antti Palosaari00e049b2013-08-07 15:06:29 -0300353
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300354 /* 384 x I+Q samples */
Antti Palosaari977e4442013-07-05 00:24:08 -0300355 src += 16;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300356 memcpy(dst, src, 984);
357 src += 984 + 24;
358 dst += 984;
359 dst_len += 984;
Antti Palosaari977e4442013-07-05 00:24:08 -0300360 }
361
Antti Palosaari34599b92013-07-08 22:31:47 -0300362 /* calculate samping rate and output it in 10 seconds intervals */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300363 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300364 unsigned long jiffies_now = jiffies;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300365 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
Antti Palosaari34599b92013-07-08 22:31:47 -0300366 unsigned int samples = sample_num[i_max - 1] - s->sample;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300367 s->jiffies_next = jiffies_now;
Antti Palosaari34599b92013-07-08 22:31:47 -0300368 s->sample = sample_num[i_max - 1];
Antti Palosaari977e4442013-07-05 00:24:08 -0300369 dev_dbg(&s->udev->dev,
Antti Palosaari02004682013-07-10 21:59:58 -0300370 "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n",
Antti Palosaari34599b92013-07-08 22:31:47 -0300371 src_len, samples, msecs,
Antti Palosaari02004682013-07-10 21:59:58 -0300372 samples * 1000UL / msecs,
373 s->sample_ctrl_bit[0], s->sample_ctrl_bit[1],
374 s->sample_ctrl_bit[2], s->sample_ctrl_bit[3]);
Antti Palosaari977e4442013-07-05 00:24:08 -0300375 }
376
Antti Palosaari34599b92013-07-08 22:31:47 -0300377 /* next sample (sample = sample + i * 384) */
378 s->next_sample = sample_num[i_max - 1] + 384;
Antti Palosaari00e049b2013-08-07 15:06:29 -0300379
Antti Palosaari977e4442013-07-05 00:24:08 -0300380 return dst_len;
381}
382
383/*
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300384 * +===========================================================================
385 * | 00-1023 | USB packet type '336'
386 * +===========================================================================
387 * | 00- 03 | sequence number of first sample in that USB packet
388 * +---------------------------------------------------------------------------
389 * | 04- 15 | garbage
390 * +---------------------------------------------------------------------------
391 * | 16-1023 | samples
392 * +---------------------------------------------------------------------------
393 * signed 12-bit sample
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300394 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300395static int msi3101_convert_stream_336(struct msi3101_state *s, u8 *dst,
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300396 u8 *src, unsigned int src_len)
397{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300398 int i, i_max, dst_len = 0;
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300399 u32 sample_num[3];
400
401 /* There could be 1-3 1024 bytes URB frames */
402 i_max = src_len / 1024;
403
404 for (i = 0; i < i_max; i++) {
405 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
406 if (i == 0 && s->next_sample != sample_num[0]) {
407 dev_dbg_ratelimited(&s->udev->dev,
408 "%d samples lost, %d %08x:%08x\n",
409 sample_num[0] - s->next_sample,
410 src_len, s->next_sample, sample_num[0]);
411 }
412
413 /*
414 * Dump all unknown 'garbage' data - maybe we will discover
415 * someday if there is something rational...
416 */
417 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
418
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300419 /* 336 x I+Q samples */
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300420 src += 16;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300421 memcpy(dst, src, 1008);
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300422 src += 1008;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300423 dst += 1008;
424 dst_len += 1008;
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300425 }
426
427 /* calculate samping rate and output it in 10 seconds intervals */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300428 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300429 unsigned long jiffies_now = jiffies;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300430 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300431 unsigned int samples = sample_num[i_max - 1] - s->sample;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300432 s->jiffies_next = jiffies_now;
Antti Palosaari8e2efac2013-08-07 11:27:21 -0300433 s->sample = sample_num[i_max - 1];
434 dev_dbg(&s->udev->dev,
435 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
436 src_len, samples, msecs,
437 samples * 1000UL / msecs);
438 }
439
440 /* next sample (sample = sample + i * 336) */
441 s->next_sample = sample_num[i_max - 1] + 336;
442
443 return dst_len;
444}
445
446/*
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300447 * +===========================================================================
448 * | 00-1023 | USB packet type '252'
449 * +===========================================================================
450 * | 00- 03 | sequence number of first sample in that USB packet
451 * +---------------------------------------------------------------------------
452 * | 04- 15 | garbage
453 * +---------------------------------------------------------------------------
454 * | 16-1023 | samples
455 * +---------------------------------------------------------------------------
456 * signed 14-bit sample
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300457 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300458static int msi3101_convert_stream_252(struct msi3101_state *s, u8 *dst,
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300459 u8 *src, unsigned int src_len)
460{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300461 int i, i_max, dst_len = 0;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300462 u32 sample_num[3];
463
464 /* There could be 1-3 1024 bytes URB frames */
465 i_max = src_len / 1024;
466
467 for (i = 0; i < i_max; i++) {
468 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
469 if (i == 0 && s->next_sample != sample_num[0]) {
470 dev_dbg_ratelimited(&s->udev->dev,
471 "%d samples lost, %d %08x:%08x\n",
472 sample_num[0] - s->next_sample,
473 src_len, s->next_sample, sample_num[0]);
474 }
475
476 /*
477 * Dump all unknown 'garbage' data - maybe we will discover
478 * someday if there is something rational...
479 */
480 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
481
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300482 /* 252 x I+Q samples */
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300483 src += 16;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300484 memcpy(dst, src, 1008);
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300485 src += 1008;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300486 dst += 1008;
487 dst_len += 1008;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300488 }
489
490 /* calculate samping rate and output it in 10 seconds intervals */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300491 if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300492 unsigned long jiffies_now = jiffies;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300493 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300494 unsigned int samples = sample_num[i_max - 1] - s->sample;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300495 s->jiffies_next = jiffies_now;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300496 s->sample = sample_num[i_max - 1];
497 dev_dbg(&s->udev->dev,
498 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
499 src_len, samples, msecs,
500 samples * 1000UL / msecs);
501 }
502
503 /* next sample (sample = sample + i * 252) */
504 s->next_sample = sample_num[i_max - 1] + 252;
505
506 return dst_len;
507}
508
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300509static int msi3101_convert_stream_252_u16(struct msi3101_state *s, u8 *dst,
510 u8 *src, unsigned int src_len)
511{
512 int i, j, i_max, dst_len = 0;
513 u32 sample_num[3];
514 u16 *u16dst = (u16 *) dst;
515 struct {signed int x:14;} se;
516
517 /* There could be 1-3 1024 bytes URB frames */
518 i_max = src_len / 1024;
519
520 for (i = 0; i < i_max; i++) {
521 sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0;
522 if (i == 0 && s->next_sample != sample_num[0]) {
523 dev_dbg_ratelimited(&s->udev->dev,
524 "%d samples lost, %d %08x:%08x\n",
525 sample_num[0] - s->next_sample,
526 src_len, s->next_sample, sample_num[0]);
527 }
528
529 /*
530 * Dump all unknown 'garbage' data - maybe we will discover
531 * someday if there is something rational...
532 */
533 dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]);
534
535 /* 252 x I+Q samples */
536 src += 16;
537
538 for (j = 0; j < 1008; j += 4) {
539 unsigned int usample[2];
540 int ssample[2];
541
542 usample[0] = src[j + 0] >> 0 | src[j + 1] << 8;
543 usample[1] = src[j + 2] >> 0 | src[j + 3] << 8;
544
545 /* sign extension from 14-bit to signed int */
546 ssample[0] = se.x = usample[0];
547 ssample[1] = se.x = usample[1];
548
549 /* from signed to unsigned */
550 usample[0] = ssample[0] + 8192;
551 usample[1] = ssample[1] + 8192;
552
553 /* from 14-bit to 16-bit */
554 *u16dst++ = (usample[0] << 2) | (usample[0] >> 12);
555 *u16dst++ = (usample[1] << 2) | (usample[1] >> 12);
556 }
557
558 src += 1008;
559 dst += 1008;
560 dst_len += 1008;
561 }
562
563 /* calculate samping rate and output it in 10 seconds intervals */
564 if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
565#define MSECS 10000UL
566 unsigned int samples = sample_num[i_max - 1] - s->sample;
567 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
568 s->sample = sample_num[i_max - 1];
569 dev_dbg(&s->udev->dev,
570 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
571 src_len, samples, MSECS,
572 samples * 1000UL / MSECS);
573 }
574
575 /* next sample (sample = sample + i * 252) */
576 s->next_sample = sample_num[i_max - 1] + 252;
577
578 return dst_len;
579}
580
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300581/*
Antti Palosaari977e4442013-07-05 00:24:08 -0300582 * This gets called for the Isochronous pipe (stream). This is done in interrupt
583 * time, so it has to be fast, not crash, and not stall. Neat.
584 */
585static void msi3101_isoc_handler(struct urb *urb)
586{
587 struct msi3101_state *s = (struct msi3101_state *)urb->context;
588 int i, flen, fstatus;
589 unsigned char *iso_buf = NULL;
590 struct msi3101_frame_buf *fbuf;
591
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300592 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
593 urb->status == -ESHUTDOWN)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300594 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
595 urb, urb->status == -ENOENT ? "" : "a");
596 return;
597 }
598
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300599 if (unlikely(urb->status != 0)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300600 dev_dbg(&s->udev->dev,
601 "msi3101_isoc_handler() called with status %d\n",
602 urb->status);
603 /* Give up after a number of contiguous errors */
604 if (++s->isoc_errors > MAX_ISOC_ERRORS)
605 dev_dbg(&s->udev->dev,
606 "Too many ISOC errors, bailing out\n");
607 goto handler_end;
608 } else {
609 /* Reset ISOC error counter. We did get here, after all. */
610 s->isoc_errors = 0;
611 }
612
613 /* Compact data */
614 for (i = 0; i < urb->number_of_packets; i++) {
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300615 void *ptr;
616
Antti Palosaari977e4442013-07-05 00:24:08 -0300617 /* Check frame error */
618 fstatus = urb->iso_frame_desc[i].status;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300619 if (unlikely(fstatus)) {
Antti Palosaari00e049b2013-08-07 15:06:29 -0300620 dev_dbg_ratelimited(&s->udev->dev,
Antti Palosaari977e4442013-07-05 00:24:08 -0300621 "frame=%d/%d has error %d skipping\n",
622 i, urb->number_of_packets, fstatus);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300623 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300624 }
625
626 /* Check if that frame contains data */
627 flen = urb->iso_frame_desc[i].actual_length;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300628 if (unlikely(flen == 0))
629 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300630
631 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
632
633 /* Get free framebuffer */
634 fbuf = msi3101_get_next_fill_buf(s);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300635 if (unlikely(fbuf == NULL)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300636 s->vb_full++;
637 dev_dbg_ratelimited(&s->udev->dev,
638 "videobuf is full, %d packets dropped\n",
639 s->vb_full);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300640 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300641 }
642
643 /* fill framebuffer */
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300644 ptr = vb2_plane_vaddr(&fbuf->vb, 0);
645 flen = s->convert_stream(s, ptr, iso_buf, flen);
646 vb2_set_plane_payload(&fbuf->vb, 0, flen);
Antti Palosaari977e4442013-07-05 00:24:08 -0300647 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
Antti Palosaari977e4442013-07-05 00:24:08 -0300648 }
649
650handler_end:
651 i = usb_submit_urb(urb, GFP_ATOMIC);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300652 if (unlikely(i != 0))
Antti Palosaari977e4442013-07-05 00:24:08 -0300653 dev_dbg(&s->udev->dev,
654 "Error (%d) re-submitting urb in msi3101_isoc_handler\n",
655 i);
656}
657
658static void msi3101_iso_stop(struct msi3101_state *s)
659{
660 int i;
661 dev_dbg(&s->udev->dev, "%s:\n", __func__);
662
663 /* Unlinking ISOC buffers one by one */
664 for (i = 0; i < MAX_ISO_BUFS; i++) {
665 if (s->urbs[i]) {
666 dev_dbg(&s->udev->dev, "Unlinking URB %p\n",
667 s->urbs[i]);
668 usb_kill_urb(s->urbs[i]);
669 }
670 }
671}
672
673static void msi3101_iso_free(struct msi3101_state *s)
674{
675 int i;
676 dev_dbg(&s->udev->dev, "%s:\n", __func__);
677
678 /* Freeing ISOC buffers one by one */
679 for (i = 0; i < MAX_ISO_BUFS; i++) {
680 if (s->urbs[i]) {
681 dev_dbg(&s->udev->dev, "Freeing URB\n");
682 if (s->urbs[i]->transfer_buffer) {
683 usb_free_coherent(s->udev,
684 s->urbs[i]->transfer_buffer_length,
685 s->urbs[i]->transfer_buffer,
686 s->urbs[i]->transfer_dma);
687 }
688 usb_free_urb(s->urbs[i]);
689 s->urbs[i] = NULL;
690 }
691 }
692}
693
694/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
695static void msi3101_isoc_cleanup(struct msi3101_state *s)
696{
697 dev_dbg(&s->udev->dev, "%s:\n", __func__);
698
699 msi3101_iso_stop(s);
700 msi3101_iso_free(s);
701}
702
703/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
704static int msi3101_isoc_init(struct msi3101_state *s)
705{
706 struct usb_device *udev;
707 struct urb *urb;
708 int i, j, ret;
709 dev_dbg(&s->udev->dev, "%s:\n", __func__);
710
711 s->isoc_errors = 0;
712 udev = s->udev;
713
714 ret = usb_set_interface(s->udev, 0, 1);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300715 if (ret)
Antti Palosaari977e4442013-07-05 00:24:08 -0300716 return ret;
717
718 /* Allocate and init Isochronuous urbs */
719 for (i = 0; i < MAX_ISO_BUFS; i++) {
720 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
721 if (urb == NULL) {
722 dev_err(&s->udev->dev,
723 "Failed to allocate urb %d\n", i);
724 msi3101_isoc_cleanup(s);
725 return -ENOMEM;
726 }
727 s->urbs[i] = urb;
728 dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb);
729
730 urb->interval = 1;
731 urb->dev = udev;
732 urb->pipe = usb_rcvisocpipe(udev, 0x81);
733 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
734 urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE,
735 GFP_KERNEL, &urb->transfer_dma);
736 if (urb->transfer_buffer == NULL) {
737 dev_err(&s->udev->dev,
738 "Failed to allocate urb buffer %d\n",
739 i);
740 msi3101_isoc_cleanup(s);
741 return -ENOMEM;
742 }
743 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
744 urb->complete = msi3101_isoc_handler;
745 urb->context = s;
746 urb->start_frame = 0;
747 urb->number_of_packets = ISO_FRAMES_PER_DESC;
748 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
749 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
750 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
751 }
752 }
753
754 /* link */
755 for (i = 0; i < MAX_ISO_BUFS; i++) {
756 ret = usb_submit_urb(s->urbs[i], GFP_KERNEL);
757 if (ret) {
758 dev_err(&s->udev->dev,
759 "isoc_init() submit_urb %d failed with error %d\n",
760 i, ret);
761 msi3101_isoc_cleanup(s);
762 return ret;
763 }
764 dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]);
765 }
766
767 /* All is done... */
768 return 0;
769}
770
771/* Must be called with vb_queue_lock hold */
772static void msi3101_cleanup_queued_bufs(struct msi3101_state *s)
773{
774 unsigned long flags = 0;
775 dev_dbg(&s->udev->dev, "%s:\n", __func__);
776
777 spin_lock_irqsave(&s->queued_bufs_lock, flags);
778 while (!list_empty(&s->queued_bufs)) {
779 struct msi3101_frame_buf *buf;
780
781 buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf,
782 list);
783 list_del(&buf->list);
784 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
785 }
786 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
787}
788
789/* The user yanked out the cable... */
790static void msi3101_disconnect(struct usb_interface *intf)
791{
792 struct v4l2_device *v = usb_get_intfdata(intf);
793 struct msi3101_state *s =
794 container_of(v, struct msi3101_state, v4l2_dev);
795 dev_dbg(&s->udev->dev, "%s:\n", __func__);
796
797 mutex_lock(&s->vb_queue_lock);
798 mutex_lock(&s->v4l2_lock);
799 /* No need to keep the urbs around after disconnection */
800 s->udev = NULL;
Antti Palosaari977e4442013-07-05 00:24:08 -0300801 v4l2_device_disconnect(&s->v4l2_dev);
802 video_unregister_device(&s->vdev);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300803 spi_unregister_master(s->master);
Antti Palosaari977e4442013-07-05 00:24:08 -0300804 mutex_unlock(&s->v4l2_lock);
805 mutex_unlock(&s->vb_queue_lock);
806
807 v4l2_device_put(&s->v4l2_dev);
808}
809
810static int msi3101_querycap(struct file *file, void *fh,
811 struct v4l2_capability *cap)
812{
813 struct msi3101_state *s = video_drvdata(file);
814 dev_dbg(&s->udev->dev, "%s:\n", __func__);
815
816 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
817 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
818 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300819 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
820 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
Antti Palosaari977e4442013-07-05 00:24:08 -0300821 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
822 return 0;
823}
824
Antti Palosaari977e4442013-07-05 00:24:08 -0300825/* Videobuf2 operations */
826static int msi3101_queue_setup(struct vb2_queue *vq,
827 const struct v4l2_format *fmt, unsigned int *nbuffers,
828 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
829{
830 struct msi3101_state *s = vb2_get_drv_priv(vq);
831 dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers);
832
833 /* Absolute min and max number of buffers available for mmap() */
Antti Palosaarid287e4e2014-02-26 20:48:23 -0300834 *nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32);
Antti Palosaari977e4442013-07-05 00:24:08 -0300835 *nplanes = 1;
Antti Palosaaric0c72032013-09-15 13:21:20 -0300836 /*
837 * 3, wMaxPacketSize 3x 1024 bytes
838 * 504, max IQ sample pairs per 1024 frame
839 * 2, two samples, I and Q
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300840 * 2, 16-bit is enough for single sample
Antti Palosaaric0c72032013-09-15 13:21:20 -0300841 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300842 sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 2);
Antti Palosaari977e4442013-07-05 00:24:08 -0300843 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
844 __func__, *nbuffers, sizes[0]);
845 return 0;
846}
847
Antti Palosaari977e4442013-07-05 00:24:08 -0300848static void msi3101_buf_queue(struct vb2_buffer *vb)
849{
850 struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue);
851 struct msi3101_frame_buf *buf =
852 container_of(vb, struct msi3101_frame_buf, vb);
853 unsigned long flags = 0;
854
855 /* Check the device has not disconnected between prep and queuing */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300856 if (unlikely(!s->udev)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300857 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
858 return;
859 }
860
861 spin_lock_irqsave(&s->queued_bufs_lock, flags);
862 list_add_tail(&buf->list, &s->queued_bufs);
863 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
864}
865
866#define CMD_WREG 0x41
867#define CMD_START_STREAMING 0x43
868#define CMD_STOP_STREAMING 0x45
869#define CMD_READ_UNKNOW 0x48
870
871#define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \
872 char *direction; \
873 if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
874 direction = ">>>"; \
875 else \
876 direction = "<<<"; \
877 dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
878 "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \
879 _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \
880}
881
882static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data)
883{
884 int ret;
885 u8 request = cmd;
886 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
887 u16 value = (data >> 0) & 0xffff;
888 u16 index = (data >> 16) & 0xffff;
889
890 msi3101_dbg_usb_control_msg(s->udev,
891 request, requesttype, value, index, NULL, 0);
892
Antti Palosaarid0fadf42013-07-11 12:19:32 -0300893 ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0),
Antti Palosaari977e4442013-07-05 00:24:08 -0300894 request, requesttype, value, index, NULL, 0, 2000);
895
896 if (ret)
897 dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n",
898 __func__, ret, cmd, data);
899
900 return ret;
901};
902
Antti Palosaari977e4442013-07-05 00:24:08 -0300903#define F_REF 24000000
Antti Palosaarib1520852013-07-09 02:44:56 -0300904#define DIV_R_IN 2
Antti Palosaari977e4442013-07-05 00:24:08 -0300905static int msi3101_set_usb_adc(struct msi3101_state *s)
906{
Antti Palosaari22ca6802013-07-31 21:48:51 -0300907 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300908 u32 reg3, reg4, reg7;
Antti Palosaari2e68f842014-02-02 23:42:00 -0300909 struct v4l2_ctrl *bandwidth_auto;
910 struct v4l2_ctrl *bandwidth;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300911
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300912 f_sr = s->f_adc;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300913
Antti Palosaari2e68f842014-02-02 23:42:00 -0300914 /* set tuner, subdev, filters according to sampling rate */
915 bandwidth_auto = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300916 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
917 bandwidth = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
918 v4l2_ctrl_s_ctrl(bandwidth, s->f_adc);
919 }
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300920
921 /* select stream format */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300922 switch (s->pixelformat) {
923 case V4L2_SDR_FMT_CU8:
924 s->convert_stream = msi3101_convert_stream_504_u8;
925 reg7 = 0x000c9407;
926 break;
927 case V4L2_SDR_FMT_CU16LE:
928 s->convert_stream = msi3101_convert_stream_252_u16;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300929 reg7 = 0x00009407;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300930 break;
931 case V4L2_PIX_FMT_SDR_S8:
Antti Palosaari61198df2013-08-08 15:39:33 -0300932 s->convert_stream = msi3101_convert_stream_504;
933 reg7 = 0x000c9407;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300934 break;
935 case V4L2_PIX_FMT_SDR_MSI2500_384:
936 s->convert_stream = msi3101_convert_stream_384;
937 reg7 = 0x0000a507;
938 break;
939 case V4L2_PIX_FMT_SDR_S12:
940 s->convert_stream = msi3101_convert_stream_336;
941 reg7 = 0x00008507;
942 break;
943 case V4L2_PIX_FMT_SDR_S14:
944 s->convert_stream = msi3101_convert_stream_252;
945 reg7 = 0x00009407;
946 break;
947 default:
948 s->convert_stream = msi3101_convert_stream_504_u8;
949 reg7 = 0x000c9407;
950 break;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300951 }
952
Antti Palosaari977e4442013-07-05 00:24:08 -0300953 /*
Antti Palosaarib1520852013-07-09 02:44:56 -0300954 * Synthesizer config is just a educated guess...
Antti Palosaari977e4442013-07-05 00:24:08 -0300955 *
Antti Palosaari977e4442013-07-05 00:24:08 -0300956 * [7:0] 0x03, register address
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300957 * [8] 1, power control
958 * [9] ?, power control
Antti Palosaari977e4442013-07-05 00:24:08 -0300959 * [12:10] output divider
960 * [13] 0 ?
961 * [14] 0 ?
Antti Palosaari22ca6802013-07-31 21:48:51 -0300962 * [15] fractional MSB, bit 20
Antti Palosaari977e4442013-07-05 00:24:08 -0300963 * [16:19] N
964 * [23:20] ?
965 * [24:31] 0x01
966 *
967 * output divider
968 * val div
969 * 0 - (invalid)
Antti Palosaarib1520852013-07-09 02:44:56 -0300970 * 1 4
971 * 2 6
972 * 3 8
973 * 4 10
974 * 5 12
975 * 6 14
976 * 7 16
977 *
978 * VCO 202000000 - 720000000++
Antti Palosaari977e4442013-07-05 00:24:08 -0300979 */
Antti Palosaaric5a431d2013-08-05 21:33:31 -0300980 reg3 = 0x01000303;
Antti Palosaari22ca6802013-07-31 21:48:51 -0300981 reg4 = 0x00000004;
Antti Palosaari977e4442013-07-05 00:24:08 -0300982
Antti Palosaaric5a431d2013-08-05 21:33:31 -0300983 /* XXX: Filters? AGC? */
984 if (f_sr < 6000000)
985 reg3 |= 0x1 << 20;
986 else if (f_sr < 7000000)
987 reg3 |= 0x5 << 20;
988 else if (f_sr < 8500000)
989 reg3 |= 0x9 << 20;
990 else
991 reg3 |= 0xd << 20;
992
Antti Palosaarib1520852013-07-09 02:44:56 -0300993 for (div_r_out = 4; div_r_out < 16; div_r_out += 2) {
994 f_vco = f_sr * div_r_out * 12;
995 dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n",
996 __func__, div_r_out, f_vco);
997 if (f_vco >= 202000000)
Antti Palosaari977e4442013-07-05 00:24:08 -0300998 break;
999 }
1000
Antti Palosaarib1520852013-07-09 02:44:56 -03001001 div_n = f_vco / (F_REF * DIV_R_IN);
1002 div_m = f_vco % (F_REF * DIV_R_IN);
Antti Palosaari22ca6802013-07-31 21:48:51 -03001003 fract = 0x200000ul * div_m / (F_REF * DIV_R_IN);
Antti Palosaarib1520852013-07-09 02:44:56 -03001004
Antti Palosaari977e4442013-07-05 00:24:08 -03001005 reg3 |= div_n << 16;
Antti Palosaarib1520852013-07-09 02:44:56 -03001006 reg3 |= (div_r_out / 2 - 1) << 10;
Antti Palosaari22ca6802013-07-31 21:48:51 -03001007 reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */
1008 reg4 |= ((fract >> 0) & 0x0fffff) << 8; /* [19:0] */
Antti Palosaari977e4442013-07-05 00:24:08 -03001009
Antti Palosaarib1520852013-07-09 02:44:56 -03001010 dev_dbg(&s->udev->dev,
Antti Palosaari22ca6802013-07-31 21:48:51 -03001011 "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n",
1012 __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg3, reg4);
Antti Palosaari977e4442013-07-05 00:24:08 -03001013
1014 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008);
1015 if (ret)
1016 goto err;
1017
1018 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05);
1019 if (ret)
1020 goto err;
1021
1022 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000);
1023 if (ret)
1024 goto err;
1025
1026 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102);
1027 if (ret)
1028 goto err;
1029
1030 ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008);
1031 if (ret)
1032 goto err;
1033
Antti Palosaari554cbfb2013-08-05 20:43:36 -03001034 ret = msi3101_ctrl_msg(s, CMD_WREG, reg7);
Antti Palosaari977e4442013-07-05 00:24:08 -03001035 if (ret)
1036 goto err;
1037
1038 ret = msi3101_ctrl_msg(s, CMD_WREG, reg4);
1039 if (ret)
1040 goto err;
1041
1042 ret = msi3101_ctrl_msg(s, CMD_WREG, reg3);
1043 if (ret)
1044 goto err;
1045err:
1046 return ret;
1047};
1048
Antti Palosaari977e4442013-07-05 00:24:08 -03001049static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count)
1050{
1051 struct msi3101_state *s = vb2_get_drv_priv(vq);
1052 int ret;
1053 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1054
1055 if (!s->udev)
1056 return -ENODEV;
1057
1058 if (mutex_lock_interruptible(&s->v4l2_lock))
1059 return -ERESTARTSYS;
1060
Antti Palosaari2e68f842014-02-02 23:42:00 -03001061 /* wake-up tuner */
1062 v4l2_subdev_call(s->v4l2_subdev, core, s_power, 1);
1063
Antti Palosaari977e4442013-07-05 00:24:08 -03001064 ret = msi3101_set_usb_adc(s);
1065
1066 ret = msi3101_isoc_init(s);
1067 if (ret)
1068 msi3101_cleanup_queued_bufs(s);
1069
1070 ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0);
1071
1072 mutex_unlock(&s->v4l2_lock);
1073
1074 return ret;
1075}
1076
Hans Verkuile37559b2014-04-17 02:47:21 -03001077static void msi3101_stop_streaming(struct vb2_queue *vq)
Antti Palosaari977e4442013-07-05 00:24:08 -03001078{
1079 struct msi3101_state *s = vb2_get_drv_priv(vq);
Hans Verkuile37559b2014-04-17 02:47:21 -03001080
Antti Palosaari977e4442013-07-05 00:24:08 -03001081 dev_dbg(&s->udev->dev, "%s:\n", __func__);
1082
Hans Verkuile37559b2014-04-17 02:47:21 -03001083 mutex_lock(&s->v4l2_lock);
Antti Palosaari977e4442013-07-05 00:24:08 -03001084
Antti Palosaari977e4442013-07-05 00:24:08 -03001085 if (s->udev)
1086 msi3101_isoc_cleanup(s);
1087
1088 msi3101_cleanup_queued_bufs(s);
Antti Palosaarid0fadf42013-07-11 12:19:32 -03001089
1090 /* according to tests, at least 700us delay is required */
1091 msleep(20);
Hans Verkuile37559b2014-04-17 02:47:21 -03001092 if (!msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0)) {
1093 /* sleep USB IF / ADC */
1094 msi3101_ctrl_msg(s, CMD_WREG, 0x01000003);
1095 }
Antti Palosaarid0fadf42013-07-11 12:19:32 -03001096
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001097 /* sleep tuner */
Hans Verkuile37559b2014-04-17 02:47:21 -03001098 v4l2_subdev_call(s->v4l2_subdev, core, s_power, 0);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001099
Antti Palosaari977e4442013-07-05 00:24:08 -03001100 mutex_unlock(&s->v4l2_lock);
Antti Palosaari977e4442013-07-05 00:24:08 -03001101}
1102
1103static struct vb2_ops msi3101_vb2_ops = {
1104 .queue_setup = msi3101_queue_setup,
Antti Palosaari977e4442013-07-05 00:24:08 -03001105 .buf_queue = msi3101_buf_queue,
1106 .start_streaming = msi3101_start_streaming,
1107 .stop_streaming = msi3101_stop_streaming,
1108 .wait_prepare = vb2_ops_wait_prepare,
1109 .wait_finish = vb2_ops_wait_finish,
1110};
1111
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001112static int msi3101_enum_fmt_sdr_cap(struct file *file, void *priv,
1113 struct v4l2_fmtdesc *f)
Antti Palosaari977e4442013-07-05 00:24:08 -03001114{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001115 struct msi3101_state *s = video_drvdata(file);
1116 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, f->index);
1117
1118 if (f->index >= NUM_FORMATS)
Antti Palosaari977e4442013-07-05 00:24:08 -03001119 return -EINVAL;
1120
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001121 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1122 f->pixelformat = formats[f->index].pixelformat;
Antti Palosaari977e4442013-07-05 00:24:08 -03001123
1124 return 0;
1125}
1126
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001127static int msi3101_g_fmt_sdr_cap(struct file *file, void *priv,
1128 struct v4l2_format *f)
Antti Palosaari977e4442013-07-05 00:24:08 -03001129{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001130 struct msi3101_state *s = video_drvdata(file);
1131 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1132 (char *)&s->pixelformat);
1133
Antti Palosaaric3509122014-03-10 16:30:44 -03001134 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001135 f->fmt.sdr.pixelformat = s->pixelformat;
Antti Palosaari977e4442013-07-05 00:24:08 -03001136
1137 return 0;
1138}
1139
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001140static int msi3101_s_fmt_sdr_cap(struct file *file, void *priv,
1141 struct v4l2_format *f)
Antti Palosaari977e4442013-07-05 00:24:08 -03001142{
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001143 struct msi3101_state *s = video_drvdata(file);
1144 struct vb2_queue *q = &s->vb_queue;
1145 int i;
1146 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1147 (char *)&f->fmt.sdr.pixelformat);
1148
1149 if (vb2_is_busy(q))
1150 return -EBUSY;
1151
Antti Palosaaric3509122014-03-10 16:30:44 -03001152 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001153 for (i = 0; i < NUM_FORMATS; i++) {
1154 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1155 s->pixelformat = f->fmt.sdr.pixelformat;
1156 return 0;
1157 }
1158 }
1159
1160 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1161 s->pixelformat = formats[0].pixelformat;
1162
1163 return 0;
Antti Palosaari977e4442013-07-05 00:24:08 -03001164}
1165
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001166static int msi3101_try_fmt_sdr_cap(struct file *file, void *priv,
1167 struct v4l2_format *f)
1168{
1169 struct msi3101_state *s = video_drvdata(file);
1170 int i;
1171 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1172 (char *)&f->fmt.sdr.pixelformat);
1173
Antti Palosaaric3509122014-03-10 16:30:44 -03001174 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001175 for (i = 0; i < NUM_FORMATS; i++) {
1176 if (formats[i].pixelformat == f->fmt.sdr.pixelformat)
1177 return 0;
1178 }
1179
1180 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1181
1182 return 0;
1183}
1184
1185static int msi3101_s_tuner(struct file *file, void *priv,
Antti Palosaari977e4442013-07-05 00:24:08 -03001186 const struct v4l2_tuner *v)
1187{
1188 struct msi3101_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001189 int ret;
1190 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
Antti Palosaari977e4442013-07-05 00:24:08 -03001191
Antti Palosaari2e68f842014-02-02 23:42:00 -03001192 if (v->index == 0)
1193 ret = 0;
1194 else if (v->index == 1)
1195 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_tuner, v);
1196 else
1197 ret = -EINVAL;
1198
1199 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -03001200}
1201
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001202static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Antti Palosaari977e4442013-07-05 00:24:08 -03001203{
1204 struct msi3101_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001205 int ret;
1206 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
Antti Palosaari977e4442013-07-05 00:24:08 -03001207
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001208 if (v->index == 0) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001209 strlcpy(v->name, "Mirics MSi2500", sizeof(v->name));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001210 v->type = V4L2_TUNER_ADC;
1211 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1212 v->rangelow = 1200000;
1213 v->rangehigh = 15000000;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001214 ret = 0;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001215 } else if (v->index == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001216 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_tuner, v);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001217 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001218 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001219 }
Antti Palosaari977e4442013-07-05 00:24:08 -03001220
Antti Palosaari2e68f842014-02-02 23:42:00 -03001221 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -03001222}
1223
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001224static int msi3101_g_frequency(struct file *file, void *priv,
1225 struct v4l2_frequency *f)
1226{
1227 struct msi3101_state *s = video_drvdata(file);
1228 int ret = 0;
1229 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n",
1230 __func__, f->tuner, f->type);
1231
Antti Palosaari2e68f842014-02-02 23:42:00 -03001232 if (f->tuner == 0) {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001233 f->frequency = s->f_adc;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001234 ret = 0;
1235 } else if (f->tuner == 1) {
Antti Palosaaric3509122014-03-10 16:30:44 -03001236 f->type = V4L2_TUNER_RF;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001237 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_frequency, f);
1238 } else {
1239 ret = -EINVAL;
1240 }
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001241
1242 return ret;
1243}
1244
1245static int msi3101_s_frequency(struct file *file, void *priv,
Antti Palosaari977e4442013-07-05 00:24:08 -03001246 const struct v4l2_frequency *f)
1247{
1248 struct msi3101_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001249 int ret;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001250 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n",
1251 __func__, f->tuner, f->type, f->frequency);
Antti Palosaari977e4442013-07-05 00:24:08 -03001252
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001253 if (f->tuner == 0) {
1254 s->f_adc = clamp_t(unsigned int, f->frequency,
Antti Palosaari2e68f842014-02-02 23:42:00 -03001255 bands[0].rangelow,
1256 bands[0].rangehigh);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001257 dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n",
1258 __func__, s->f_adc);
1259 ret = msi3101_set_usb_adc(s);
1260 } else if (f->tuner == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001261 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_frequency, f);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001262 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001263 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001264 }
1265
1266 return ret;
1267}
1268
1269static int msi3101_enum_freq_bands(struct file *file, void *priv,
1270 struct v4l2_frequency_band *band)
1271{
1272 struct msi3101_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001273 int ret;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001274 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n",
1275 __func__, band->tuner, band->type, band->index);
1276
1277 if (band->tuner == 0) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001278 if (band->index >= ARRAY_SIZE(bands)) {
1279 ret = -EINVAL;
1280 } else {
1281 *band = bands[band->index];
1282 ret = 0;
1283 }
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001284 } else if (band->tuner == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001285 ret = v4l2_subdev_call(s->v4l2_subdev, tuner,
1286 enum_freq_bands, band);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001287 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001288 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001289 }
1290
Antti Palosaari2e68f842014-02-02 23:42:00 -03001291 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -03001292}
1293
Fengguang Wu6648ef12013-09-02 19:09:53 -03001294static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
Antti Palosaari977e4442013-07-05 00:24:08 -03001295 .vidioc_querycap = msi3101_querycap,
1296
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001297 .vidioc_enum_fmt_sdr_cap = msi3101_enum_fmt_sdr_cap,
1298 .vidioc_g_fmt_sdr_cap = msi3101_g_fmt_sdr_cap,
1299 .vidioc_s_fmt_sdr_cap = msi3101_s_fmt_sdr_cap,
1300 .vidioc_try_fmt_sdr_cap = msi3101_try_fmt_sdr_cap,
Antti Palosaari977e4442013-07-05 00:24:08 -03001301
1302 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1303 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1304 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1305 .vidioc_querybuf = vb2_ioctl_querybuf,
1306 .vidioc_qbuf = vb2_ioctl_qbuf,
1307 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1308
1309 .vidioc_streamon = vb2_ioctl_streamon,
1310 .vidioc_streamoff = vb2_ioctl_streamoff,
1311
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001312 .vidioc_g_tuner = msi3101_g_tuner,
1313 .vidioc_s_tuner = msi3101_s_tuner,
1314
1315 .vidioc_g_frequency = msi3101_g_frequency,
1316 .vidioc_s_frequency = msi3101_s_frequency,
1317 .vidioc_enum_freq_bands = msi3101_enum_freq_bands,
Antti Palosaari977e4442013-07-05 00:24:08 -03001318
1319 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1320 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1321 .vidioc_log_status = v4l2_ctrl_log_status,
1322};
1323
1324static const struct v4l2_file_operations msi3101_fops = {
1325 .owner = THIS_MODULE,
1326 .open = v4l2_fh_open,
1327 .release = vb2_fop_release,
1328 .read = vb2_fop_read,
1329 .poll = vb2_fop_poll,
1330 .mmap = vb2_fop_mmap,
1331 .unlocked_ioctl = video_ioctl2,
1332};
1333
1334static struct video_device msi3101_template = {
1335 .name = "Mirics MSi3101 SDR Dongle",
1336 .release = video_device_release_empty,
1337 .fops = &msi3101_fops,
1338 .ioctl_ops = &msi3101_ioctl_ops,
1339};
1340
Antti Palosaari977e4442013-07-05 00:24:08 -03001341static void msi3101_video_release(struct v4l2_device *v)
1342{
1343 struct msi3101_state *s =
1344 container_of(v, struct msi3101_state, v4l2_dev);
1345
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001346 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaari977e4442013-07-05 00:24:08 -03001347 v4l2_device_unregister(&s->v4l2_dev);
1348 kfree(s);
1349}
1350
Antti Palosaari2e68f842014-02-02 23:42:00 -03001351static int msi3101_transfer_one_message(struct spi_master *master,
1352 struct spi_message *m)
1353{
1354 struct msi3101_state *s = spi_master_get_devdata(master);
1355 struct spi_transfer *t;
1356 int ret = 0;
1357 u32 data;
1358
1359 list_for_each_entry(t, &m->transfers, transfer_list) {
1360 dev_dbg(&s->udev->dev, "%s: msg=%*ph\n",
1361 __func__, t->len, t->tx_buf);
1362 data = 0x09; /* reg 9 is SPI adapter */
1363 data |= ((u8 *)t->tx_buf)[0] << 8;
1364 data |= ((u8 *)t->tx_buf)[1] << 16;
1365 data |= ((u8 *)t->tx_buf)[2] << 24;
1366 ret = msi3101_ctrl_msg(s, CMD_WREG, data);
1367 }
1368
1369 m->status = ret;
1370 spi_finalize_current_message(master);
1371 return ret;
1372}
1373
Antti Palosaari977e4442013-07-05 00:24:08 -03001374static int msi3101_probe(struct usb_interface *intf,
1375 const struct usb_device_id *id)
1376{
1377 struct usb_device *udev = interface_to_usbdev(intf);
1378 struct msi3101_state *s = NULL;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001379 struct v4l2_subdev *sd;
1380 struct spi_master *master;
Antti Palosaari977e4442013-07-05 00:24:08 -03001381 int ret;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001382 static struct spi_board_info board_info = {
1383 .modalias = "msi001",
1384 .bus_num = 0,
1385 .chip_select = 0,
1386 .max_speed_hz = 12000000,
Antti Palosaari977e4442013-07-05 00:24:08 -03001387 };
1388
1389 s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1390 if (s == NULL) {
1391 pr_err("Could not allocate memory for msi3101_state\n");
1392 return -ENOMEM;
1393 }
1394
1395 mutex_init(&s->v4l2_lock);
1396 mutex_init(&s->vb_queue_lock);
1397 spin_lock_init(&s->queued_bufs_lock);
1398 INIT_LIST_HEAD(&s->queued_bufs);
Antti Palosaari977e4442013-07-05 00:24:08 -03001399 s->udev = udev;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001400 s->f_adc = bands[0].rangelow;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001401 s->pixelformat = V4L2_SDR_FMT_CU8;
Antti Palosaari977e4442013-07-05 00:24:08 -03001402
1403 /* Init videobuf2 queue structure */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001404 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
Antti Palosaari977e4442013-07-05 00:24:08 -03001405 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1406 s->vb_queue.drv_priv = s;
1407 s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
1408 s->vb_queue.ops = &msi3101_vb2_ops;
1409 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03001410 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Antti Palosaari977e4442013-07-05 00:24:08 -03001411 ret = vb2_queue_init(&s->vb_queue);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001412 if (ret) {
Antti Palosaari977e4442013-07-05 00:24:08 -03001413 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
1414 goto err_free_mem;
1415 }
1416
1417 /* Init video_device structure */
1418 s->vdev = msi3101_template;
1419 s->vdev.queue = &s->vb_queue;
1420 s->vdev.queue->lock = &s->vb_queue_lock;
Antti Palosaari977e4442013-07-05 00:24:08 -03001421 video_set_drvdata(&s->vdev, s);
1422
Antti Palosaari977e4442013-07-05 00:24:08 -03001423 /* Register the v4l2_device structure */
1424 s->v4l2_dev.release = msi3101_video_release;
1425 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1426 if (ret) {
1427 dev_err(&s->udev->dev,
1428 "Failed to register v4l2-device (%d)\n", ret);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001429 goto err_free_mem;
1430 }
1431
1432 /* SPI master adapter */
1433 master = spi_alloc_master(&s->udev->dev, 0);
1434 if (master == NULL) {
1435 ret = -ENOMEM;
1436 goto err_unregister_v4l2_dev;
1437 }
1438
1439 s->master = master;
1440 master->bus_num = 0;
1441 master->num_chipselect = 1;
1442 master->transfer_one_message = msi3101_transfer_one_message;
1443 spi_master_set_devdata(master, s);
1444 ret = spi_register_master(master);
1445 if (ret) {
1446 spi_master_put(master);
1447 goto err_unregister_v4l2_dev;
1448 }
1449
1450 /* load v4l2 subdevice */
1451 sd = v4l2_spi_new_subdev(&s->v4l2_dev, master, &board_info);
1452 s->v4l2_subdev = sd;
1453 if (sd == NULL) {
1454 dev_err(&s->udev->dev, "cannot get v4l2 subdevice\n");
1455 ret = -ENODEV;
1456 goto err_unregister_master;
1457 }
1458
1459 /* Register controls */
1460 v4l2_ctrl_handler_init(&s->hdl, 0);
1461 if (s->hdl.error) {
1462 ret = s->hdl.error;
1463 dev_err(&s->udev->dev, "Could not initialize controls\n");
Antti Palosaari977e4442013-07-05 00:24:08 -03001464 goto err_free_controls;
1465 }
1466
Antti Palosaari2e68f842014-02-02 23:42:00 -03001467 /* currently all controls are from subdev */
1468 v4l2_ctrl_add_handler(&s->hdl, sd->ctrl_handler, NULL);
1469
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001470 s->v4l2_dev.ctrl_handler = &s->hdl;
Antti Palosaari977e4442013-07-05 00:24:08 -03001471 s->vdev.v4l2_dev = &s->v4l2_dev;
1472 s->vdev.lock = &s->v4l2_lock;
1473
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001474 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
1475 if (ret) {
Antti Palosaari977e4442013-07-05 00:24:08 -03001476 dev_err(&s->udev->dev,
1477 "Failed to register as video device (%d)\n",
1478 ret);
1479 goto err_unregister_v4l2_dev;
1480 }
1481 dev_info(&s->udev->dev, "Registered as %s\n",
1482 video_device_node_name(&s->vdev));
1483
1484 return 0;
1485
Antti Palosaari977e4442013-07-05 00:24:08 -03001486err_free_controls:
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001487 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001488err_unregister_master:
1489 spi_unregister_master(s->master);
Antti Palosaari977e4442013-07-05 00:24:08 -03001490err_unregister_v4l2_dev:
1491 v4l2_device_unregister(&s->v4l2_dev);
Antti Palosaari977e4442013-07-05 00:24:08 -03001492err_free_mem:
1493 kfree(s);
1494 return ret;
1495}
1496
1497/* USB device ID list */
1498static struct usb_device_id msi3101_id_table[] = {
Antti Palosaari00e049b2013-08-07 15:06:29 -03001499 { USB_DEVICE(0x1df7, 0x2500) }, /* Mirics MSi3101 SDR Dongle */
Antti Palosaari42fc5b42013-07-17 15:05:27 -03001500 { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */
Antti Palosaari977e4442013-07-05 00:24:08 -03001501 { }
1502};
1503MODULE_DEVICE_TABLE(usb, msi3101_id_table);
1504
1505/* USB subsystem interface */
1506static struct usb_driver msi3101_driver = {
1507 .name = KBUILD_MODNAME,
1508 .probe = msi3101_probe,
1509 .disconnect = msi3101_disconnect,
1510 .id_table = msi3101_id_table,
1511};
1512
1513module_usb_driver(msi3101_driver);
1514
1515MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1516MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1517MODULE_LICENSE("GPL");