blob: efc761c78f7250e5b27a93b0fcb18bbc3fb142a5 [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 Palosaari06ce32c2014-07-22 05:43:06 -030037static bool msi2500_emulated_fmt;
38module_param_named(emulated_formats, msi2500_emulated_fmt, bool, 0644);
Antti Palosaari3912eb62014-07-13 22:27:21 -030039MODULE_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 Palosaaric08de622014-07-22 02:44:12 -030058/*
59 * TODO: These formats should be moved to V4L2 API. Formats are currently
60 * disabled from formats[] table, not visible to userspace.
61 */
62 /* signed 12-bit */
63#define MSI2500_PIX_FMT_SDR_S12 v4l2_fourcc('D', 'S', '1', '2')
64/* Mirics MSi2500 format 384 */
65#define MSI2500_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4')
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030066
Antti Palosaari2e68f842014-02-02 23:42:00 -030067static const struct v4l2_frequency_band bands[] = {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030068 {
69 .tuner = 0,
70 .type = V4L2_TUNER_ADC,
71 .index = 0,
72 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
73 .rangelow = 1200000,
74 .rangehigh = 15000000,
75 },
76};
77
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030078/* stream formats */
Antti Palosaari06ce32c2014-07-22 05:43:06 -030079struct msi2500_format {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030080 char *name;
81 u32 pixelformat;
Antti Palosaaria54e0fe2014-07-18 20:56:01 -030082 u32 buffersize;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030083};
84
85/* format descriptions for capture and preview */
Antti Palosaari06ce32c2014-07-22 05:43:06 -030086static struct msi2500_format formats[] = {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030087 {
Antti Palosaari3912eb62014-07-13 22:27:21 -030088 .name = "Complex S8",
89 .pixelformat = V4L2_SDR_FMT_CS8,
Antti Palosaaria54e0fe2014-07-18 20:56:01 -030090 .buffersize = 3 * 1008,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030091#if 0
92 }, {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030093 .name = "10+2-bit signed",
Antti Palosaaric08de622014-07-22 02:44:12 -030094 .pixelformat = MSI2500_PIX_FMT_SDR_MSI2500_384,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030095 }, {
96 .name = "12-bit signed",
Antti Palosaaric08de622014-07-22 02:44:12 -030097 .pixelformat = MSI2500_PIX_FMT_SDR_S12,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -030098#endif
Antti Palosaari3912eb62014-07-13 22:27:21 -030099 }, {
100 .name = "Complex S14LE",
101 .pixelformat = V4L2_SDR_FMT_CS14LE,
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300102 .buffersize = 3 * 1008,
Antti Palosaari3912eb62014-07-13 22:27:21 -0300103 }, {
104 .name = "Complex U8 (emulated)",
105 .pixelformat = V4L2_SDR_FMT_CU8,
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300106 .buffersize = 3 * 1008,
Antti Palosaari3912eb62014-07-13 22:27:21 -0300107 }, {
108 .name = "Complex U16LE (emulated)",
109 .pixelformat = V4L2_SDR_FMT_CU16LE,
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300110 .buffersize = 3 * 1008,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300111 },
112};
113
114static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
Antti Palosaari977e4442013-07-05 00:24:08 -0300115
116/* intermediate buffers with raw data from the USB device */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300117struct msi2500_frame_buf {
Antti Palosaari977e4442013-07-05 00:24:08 -0300118 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
119 struct list_head list;
Antti Palosaari977e4442013-07-05 00:24:08 -0300120};
121
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300122struct msi2500_state {
Antti Palosaari100b79312014-08-24 22:54:06 -0300123 struct device *dev;
Antti Palosaari977e4442013-07-05 00:24:08 -0300124 struct video_device vdev;
125 struct v4l2_device v4l2_dev;
Antti Palosaari2e68f842014-02-02 23:42:00 -0300126 struct v4l2_subdev *v4l2_subdev;
127 struct spi_master *master;
Antti Palosaari977e4442013-07-05 00:24:08 -0300128
129 /* videobuf2 queue and queued buffers list */
130 struct vb2_queue vb_queue;
131 struct list_head queued_bufs;
132 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
133
134 /* Note if taking both locks v4l2_lock must always be locked first! */
135 struct mutex v4l2_lock; /* Protects everything else */
136 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
137
138 /* Pointer to our usb_device, will be NULL after unplug */
139 struct usb_device *udev; /* Both mutexes most be hold when setting! */
140
Antti Palosaari2e68f842014-02-02 23:42:00 -0300141 unsigned int f_adc;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300142 u32 pixelformat;
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300143 u32 buffersize;
Antti Palosaari3912eb62014-07-13 22:27:21 -0300144 unsigned int num_formats;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300145
Antti Palosaari977e4442013-07-05 00:24:08 -0300146 unsigned int isoc_errors; /* number of contiguous ISOC errors */
147 unsigned int vb_full; /* vb is full and packets dropped */
148
149 struct urb *urbs[MAX_ISO_BUFS];
150
151 /* Controls */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300152 struct v4l2_ctrl_handler hdl;
Antti Palosaari977e4442013-07-05 00:24:08 -0300153
Antti Palosaari34599b92013-07-08 22:31:47 -0300154 u32 next_sample; /* for track lost packets */
155 u32 sample; /* for sample rate calc */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300156 unsigned long jiffies_next;
Antti Palosaari977e4442013-07-05 00:24:08 -0300157};
158
159/* Private functions */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300160static struct msi2500_frame_buf *msi2500_get_next_fill_buf(
161 struct msi2500_state *s)
Antti Palosaari977e4442013-07-05 00:24:08 -0300162{
Antti Palosaarib63ab6b2014-08-24 23:26:23 -0300163 unsigned long flags;
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300164 struct msi2500_frame_buf *buf = NULL;
Antti Palosaari977e4442013-07-05 00:24:08 -0300165
166 spin_lock_irqsave(&s->queued_bufs_lock, flags);
167 if (list_empty(&s->queued_bufs))
168 goto leave;
169
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300170 buf = list_entry(s->queued_bufs.next, struct msi2500_frame_buf, list);
Antti Palosaari977e4442013-07-05 00:24:08 -0300171 list_del(&buf->list);
172leave:
173 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
174 return buf;
175}
176
177/*
Antti Palosaari02004682013-07-10 21:59:58 -0300178 * +===========================================================================
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300179 * | 00-1023 | USB packet type '504'
180 * +===========================================================================
181 * | 00- 03 | sequence number of first sample in that USB packet
182 * +---------------------------------------------------------------------------
183 * | 04- 15 | garbage
184 * +---------------------------------------------------------------------------
185 * | 16-1023 | samples
186 * +---------------------------------------------------------------------------
187 * signed 8-bit sample
188 * 504 * 2 = 1008 samples
Antti Palosaari8591f702014-07-22 05:29:57 -0300189 *
190 *
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300191 * +===========================================================================
Antti Palosaari61198df2013-08-08 15:39:33 -0300192 * | 00-1023 | USB packet type '384'
Antti Palosaari02004682013-07-10 21:59:58 -0300193 * +===========================================================================
194 * | 00- 03 | sequence number of first sample in that USB packet
195 * +---------------------------------------------------------------------------
196 * | 04- 15 | garbage
197 * +---------------------------------------------------------------------------
198 * | 16- 175 | samples
199 * +---------------------------------------------------------------------------
200 * | 176- 179 | control bits for previous samples
201 * +---------------------------------------------------------------------------
202 * | 180- 339 | samples
203 * +---------------------------------------------------------------------------
204 * | 340- 343 | control bits for previous samples
205 * +---------------------------------------------------------------------------
206 * | 344- 503 | samples
207 * +---------------------------------------------------------------------------
208 * | 504- 507 | control bits for previous samples
209 * +---------------------------------------------------------------------------
210 * | 508- 667 | samples
211 * +---------------------------------------------------------------------------
212 * | 668- 671 | control bits for previous samples
213 * +---------------------------------------------------------------------------
214 * | 672- 831 | samples
215 * +---------------------------------------------------------------------------
216 * | 832- 835 | control bits for previous samples
217 * +---------------------------------------------------------------------------
218 * | 836- 995 | samples
219 * +---------------------------------------------------------------------------
220 * | 996- 999 | control bits for previous samples
221 * +---------------------------------------------------------------------------
Antti Palosaari6450e502013-07-31 19:58:12 -0300222 * | 1000-1023 | garbage
Antti Palosaari02004682013-07-10 21:59:58 -0300223 * +---------------------------------------------------------------------------
224 *
225 * Bytes 4 - 7 could have some meaning?
226 *
227 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
228 * numbers. This results one 2-bit number for 8 samples. It is likely used for
229 * for bit shifting sample by given bits, increasing actual sampling resolution.
230 * Number 2 (0b10) was never seen.
231 *
232 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
Antti Palosaari8591f702014-07-22 05:29:57 -0300233 *
234 *
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300235 * +===========================================================================
236 * | 00-1023 | USB packet type '336'
237 * +===========================================================================
238 * | 00- 03 | sequence number of first sample in that USB packet
239 * +---------------------------------------------------------------------------
240 * | 04- 15 | garbage
241 * +---------------------------------------------------------------------------
242 * | 16-1023 | samples
243 * +---------------------------------------------------------------------------
244 * signed 12-bit sample
Antti Palosaari8591f702014-07-22 05:29:57 -0300245 *
246 *
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300247 * +===========================================================================
248 * | 00-1023 | USB packet type '252'
249 * +===========================================================================
250 * | 00- 03 | sequence number of first sample in that USB packet
251 * +---------------------------------------------------------------------------
252 * | 04- 15 | garbage
253 * +---------------------------------------------------------------------------
254 * | 16-1023 | samples
255 * +---------------------------------------------------------------------------
256 * signed 14-bit sample
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300257 */
Antti Palosaari8591f702014-07-22 05:29:57 -0300258
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300259static int msi2500_convert_stream(struct msi2500_state *s, u8 *dst, u8 *src,
Antti Palosaari8591f702014-07-22 05:29:57 -0300260 unsigned int src_len)
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300261{
Antti Palosaari8591f702014-07-22 05:29:57 -0300262 unsigned int i, j, transactions, dst_len = 0;
263 u32 sample[3];
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300264
Antti Palosaari8591f702014-07-22 05:29:57 -0300265 /* There could be 1-3 1024 byte transactions per packet */
266 transactions = src_len / 1024;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300267
Antti Palosaari8591f702014-07-22 05:29:57 -0300268 for (i = 0; i < transactions; i++) {
269 sample[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 |
270 src[0] << 0;
271 if (i == 0 && s->next_sample != sample[0]) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300272 dev_dbg_ratelimited(s->dev,
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300273 "%d samples lost, %d %08x:%08x\n",
Antti Palosaari8591f702014-07-22 05:29:57 -0300274 sample[0] - s->next_sample,
275 src_len, s->next_sample, sample[0]);
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300276 }
277
278 /*
279 * Dump all unknown 'garbage' data - maybe we will discover
280 * someday if there is something rational...
281 */
Antti Palosaari100b79312014-08-24 22:54:06 -0300282 dev_dbg_ratelimited(s->dev, "%*ph\n", 12, &src[4]);
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300283
Antti Palosaari8591f702014-07-22 05:29:57 -0300284 src += 16; /* skip header */
285
286 switch (s->pixelformat) {
287 case V4L2_SDR_FMT_CU8: /* 504 x IQ samples */
288 {
289 s8 *s8src = (s8 *) src;
290 u8 *u8dst = (u8 *) dst;
291
292 for (j = 0; j < 1008; j++)
293 *u8dst++ = *s8src++ + 128;
294
295 src += 1008;
296 dst += 1008;
297 dst_len += 1008;
298 s->next_sample = sample[i] + 504;
299 break;
300 }
301 case V4L2_SDR_FMT_CU16LE: /* 252 x IQ samples */
302 {
303 s16 *s16src = (s16 *) src;
304 u16 *u16dst = (u16 *) dst;
305 struct {signed int x:14; } se; /* sign extension */
306 unsigned int utmp;
307
308 for (j = 0; j < 1008; j += 2) {
309 /* sign extension from 14-bit to signed int */
310 se.x = *s16src++;
311 /* from signed int to unsigned int */
312 utmp = se.x + 8192;
313 /* from 14-bit to 16-bit */
314 *u16dst++ = utmp << 2 | utmp >> 12;
315 }
316
317 src += 1008;
318 dst += 1008;
319 dst_len += 1008;
320 s->next_sample = sample[i] + 252;
321 break;
322 }
323 case MSI2500_PIX_FMT_SDR_MSI2500_384: /* 384 x IQ samples */
324 /* Dump unknown 'garbage' data */
Antti Palosaari100b79312014-08-24 22:54:06 -0300325 dev_dbg_ratelimited(s->dev, "%*ph\n", 24, &src[1000]);
Antti Palosaari8591f702014-07-22 05:29:57 -0300326 memcpy(dst, src, 984);
327 src += 984 + 24;
328 dst += 984;
329 dst_len += 984;
330 s->next_sample = sample[i] + 384;
331 break;
332 case V4L2_SDR_FMT_CS8: /* 504 x IQ samples */
333 memcpy(dst, src, 1008);
334 src += 1008;
335 dst += 1008;
336 dst_len += 1008;
337 s->next_sample = sample[i] + 504;
338 break;
339 case MSI2500_PIX_FMT_SDR_S12: /* 336 x IQ samples */
340 memcpy(dst, src, 1008);
341 src += 1008;
342 dst += 1008;
343 dst_len += 1008;
344 s->next_sample = sample[i] + 336;
345 break;
346 case V4L2_SDR_FMT_CS14LE: /* 252 x IQ samples */
347 memcpy(dst, src, 1008);
348 src += 1008;
349 dst += 1008;
350 dst_len += 1008;
351 s->next_sample = sample[i] + 252;
352 break;
353 default:
354 break;
355 }
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300356 }
357
Antti Palosaari8591f702014-07-22 05:29:57 -0300358 /* calculate sample rate and output it in 10 seconds intervals */
359 if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
360 #define MSECS 10000UL
361 unsigned int msecs = jiffies_to_msecs(jiffies -
362 s->jiffies_next + msecs_to_jiffies(MSECS));
363 unsigned int samples = s->next_sample - s->sample;
Antti Palosaaric08de622014-07-22 02:44:12 -0300364
Antti Palosaari8591f702014-07-22 05:29:57 -0300365 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
366 s->sample = s->next_sample;
Antti Palosaari100b79312014-08-24 22:54:06 -0300367 dev_dbg(s->dev, "size=%u samples=%u msecs=%u sample rate=%lu\n",
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300368 src_len, samples, msecs,
369 samples * 1000UL / msecs);
370 }
371
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300372 return dst_len;
373}
374
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300375/*
Antti Palosaari977e4442013-07-05 00:24:08 -0300376 * This gets called for the Isochronous pipe (stream). This is done in interrupt
377 * time, so it has to be fast, not crash, and not stall. Neat.
378 */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300379static void msi2500_isoc_handler(struct urb *urb)
Antti Palosaari977e4442013-07-05 00:24:08 -0300380{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300381 struct msi2500_state *s = (struct msi2500_state *)urb->context;
Antti Palosaari977e4442013-07-05 00:24:08 -0300382 int i, flen, fstatus;
383 unsigned char *iso_buf = NULL;
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300384 struct msi2500_frame_buf *fbuf;
Antti Palosaari977e4442013-07-05 00:24:08 -0300385
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300386 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
387 urb->status == -ESHUTDOWN)) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300388 dev_dbg(s->dev, "URB (%p) unlinked %ssynchronuously\n",
Antti Palosaari977e4442013-07-05 00:24:08 -0300389 urb, urb->status == -ENOENT ? "" : "a");
390 return;
391 }
392
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300393 if (unlikely(urb->status != 0)) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300394 dev_dbg(s->dev, "called with status %d\n", urb->status);
Antti Palosaari977e4442013-07-05 00:24:08 -0300395 /* Give up after a number of contiguous errors */
396 if (++s->isoc_errors > MAX_ISOC_ERRORS)
Antti Palosaari100b79312014-08-24 22:54:06 -0300397 dev_dbg(s->dev, "Too many ISOC errors, bailing out\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300398 goto handler_end;
399 } else {
400 /* Reset ISOC error counter. We did get here, after all. */
401 s->isoc_errors = 0;
402 }
403
404 /* Compact data */
405 for (i = 0; i < urb->number_of_packets; i++) {
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300406 void *ptr;
407
Antti Palosaari977e4442013-07-05 00:24:08 -0300408 /* Check frame error */
409 fstatus = urb->iso_frame_desc[i].status;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300410 if (unlikely(fstatus)) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300411 dev_dbg_ratelimited(s->dev,
Antti Palosaari977e4442013-07-05 00:24:08 -0300412 "frame=%d/%d has error %d skipping\n",
413 i, urb->number_of_packets, fstatus);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300414 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300415 }
416
417 /* Check if that frame contains data */
418 flen = urb->iso_frame_desc[i].actual_length;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300419 if (unlikely(flen == 0))
420 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300421
422 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
423
424 /* Get free framebuffer */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300425 fbuf = msi2500_get_next_fill_buf(s);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300426 if (unlikely(fbuf == NULL)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300427 s->vb_full++;
Antti Palosaari100b79312014-08-24 22:54:06 -0300428 dev_dbg_ratelimited(s->dev,
Antti Palosaari977e4442013-07-05 00:24:08 -0300429 "videobuf is full, %d packets dropped\n",
430 s->vb_full);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300431 continue;
Antti Palosaari977e4442013-07-05 00:24:08 -0300432 }
433
434 /* fill framebuffer */
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300435 ptr = vb2_plane_vaddr(&fbuf->vb, 0);
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300436 flen = msi2500_convert_stream(s, ptr, iso_buf, flen);
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300437 vb2_set_plane_payload(&fbuf->vb, 0, flen);
Antti Palosaari977e4442013-07-05 00:24:08 -0300438 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
Antti Palosaari977e4442013-07-05 00:24:08 -0300439 }
440
441handler_end:
442 i = usb_submit_urb(urb, GFP_ATOMIC);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300443 if (unlikely(i != 0))
Antti Palosaari100b79312014-08-24 22:54:06 -0300444 dev_dbg(s->dev, "Error (%d) re-submitting urb\n", i);
Antti Palosaari977e4442013-07-05 00:24:08 -0300445}
446
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300447static void msi2500_iso_stop(struct msi2500_state *s)
Antti Palosaari977e4442013-07-05 00:24:08 -0300448{
449 int i;
Antti Palosaaric08de622014-07-22 02:44:12 -0300450
Antti Palosaari100b79312014-08-24 22:54:06 -0300451 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300452
453 /* Unlinking ISOC buffers one by one */
454 for (i = 0; i < MAX_ISO_BUFS; i++) {
455 if (s->urbs[i]) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300456 dev_dbg(s->dev, "Unlinking URB %p\n", s->urbs[i]);
Antti Palosaari977e4442013-07-05 00:24:08 -0300457 usb_kill_urb(s->urbs[i]);
458 }
459 }
460}
461
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300462static void msi2500_iso_free(struct msi2500_state *s)
Antti Palosaari977e4442013-07-05 00:24:08 -0300463{
464 int i;
Antti Palosaaric08de622014-07-22 02:44:12 -0300465
Antti Palosaari100b79312014-08-24 22:54:06 -0300466 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300467
468 /* Freeing ISOC buffers one by one */
469 for (i = 0; i < MAX_ISO_BUFS; i++) {
470 if (s->urbs[i]) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300471 dev_dbg(s->dev, "Freeing URB\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300472 if (s->urbs[i]->transfer_buffer) {
473 usb_free_coherent(s->udev,
474 s->urbs[i]->transfer_buffer_length,
475 s->urbs[i]->transfer_buffer,
476 s->urbs[i]->transfer_dma);
477 }
478 usb_free_urb(s->urbs[i]);
479 s->urbs[i] = NULL;
480 }
481 }
482}
483
484/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300485static void msi2500_isoc_cleanup(struct msi2500_state *s)
Antti Palosaari977e4442013-07-05 00:24:08 -0300486{
Antti Palosaari100b79312014-08-24 22:54:06 -0300487 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300488
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300489 msi2500_iso_stop(s);
490 msi2500_iso_free(s);
Antti Palosaari977e4442013-07-05 00:24:08 -0300491}
492
493/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300494static int msi2500_isoc_init(struct msi2500_state *s)
Antti Palosaari977e4442013-07-05 00:24:08 -0300495{
Antti Palosaari977e4442013-07-05 00:24:08 -0300496 struct urb *urb;
497 int i, j, ret;
Antti Palosaaric08de622014-07-22 02:44:12 -0300498
Antti Palosaari100b79312014-08-24 22:54:06 -0300499 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300500
501 s->isoc_errors = 0;
Antti Palosaari977e4442013-07-05 00:24:08 -0300502
503 ret = usb_set_interface(s->udev, 0, 1);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300504 if (ret)
Antti Palosaari977e4442013-07-05 00:24:08 -0300505 return ret;
506
507 /* Allocate and init Isochronuous urbs */
508 for (i = 0; i < MAX_ISO_BUFS; i++) {
509 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
510 if (urb == NULL) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300511 dev_err(s->dev, "Failed to allocate urb %d\n", i);
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300512 msi2500_isoc_cleanup(s);
Antti Palosaari977e4442013-07-05 00:24:08 -0300513 return -ENOMEM;
514 }
515 s->urbs[i] = urb;
Antti Palosaari100b79312014-08-24 22:54:06 -0300516 dev_dbg(s->dev, "Allocated URB at 0x%p\n", urb);
Antti Palosaari977e4442013-07-05 00:24:08 -0300517
518 urb->interval = 1;
Antti Palosaaride3f2e22014-08-21 12:03:58 -0300519 urb->dev = s->udev;
520 urb->pipe = usb_rcvisocpipe(s->udev, 0x81);
Antti Palosaari977e4442013-07-05 00:24:08 -0300521 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Antti Palosaaride3f2e22014-08-21 12:03:58 -0300522 urb->transfer_buffer = usb_alloc_coherent(s->udev,
523 ISO_BUFFER_SIZE,
Antti Palosaari977e4442013-07-05 00:24:08 -0300524 GFP_KERNEL, &urb->transfer_dma);
525 if (urb->transfer_buffer == NULL) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300526 dev_err(s->dev, "Failed to allocate urb buffer %d\n",
Antti Palosaari977e4442013-07-05 00:24:08 -0300527 i);
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300528 msi2500_isoc_cleanup(s);
Antti Palosaari977e4442013-07-05 00:24:08 -0300529 return -ENOMEM;
530 }
531 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300532 urb->complete = msi2500_isoc_handler;
Antti Palosaari977e4442013-07-05 00:24:08 -0300533 urb->context = s;
534 urb->start_frame = 0;
535 urb->number_of_packets = ISO_FRAMES_PER_DESC;
536 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
537 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
538 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
539 }
540 }
541
542 /* link */
543 for (i = 0; i < MAX_ISO_BUFS; i++) {
544 ret = usb_submit_urb(s->urbs[i], GFP_KERNEL);
545 if (ret) {
Antti Palosaari100b79312014-08-24 22:54:06 -0300546 dev_err(s->dev, "usb_submit_urb %d failed with error %d\n",
Antti Palosaari977e4442013-07-05 00:24:08 -0300547 i, ret);
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300548 msi2500_isoc_cleanup(s);
Antti Palosaari977e4442013-07-05 00:24:08 -0300549 return ret;
550 }
Antti Palosaari100b79312014-08-24 22:54:06 -0300551 dev_dbg(s->dev, "URB 0x%p submitted.\n", s->urbs[i]);
Antti Palosaari977e4442013-07-05 00:24:08 -0300552 }
553
554 /* All is done... */
555 return 0;
556}
557
558/* Must be called with vb_queue_lock hold */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300559static void msi2500_cleanup_queued_bufs(struct msi2500_state *s)
Antti Palosaari977e4442013-07-05 00:24:08 -0300560{
Antti Palosaarib63ab6b2014-08-24 23:26:23 -0300561 unsigned long flags;
Antti Palosaaric08de622014-07-22 02:44:12 -0300562
Antti Palosaari100b79312014-08-24 22:54:06 -0300563 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300564
565 spin_lock_irqsave(&s->queued_bufs_lock, flags);
566 while (!list_empty(&s->queued_bufs)) {
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300567 struct msi2500_frame_buf *buf;
Antti Palosaari977e4442013-07-05 00:24:08 -0300568
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300569 buf = list_entry(s->queued_bufs.next, struct msi2500_frame_buf,
Antti Palosaari977e4442013-07-05 00:24:08 -0300570 list);
571 list_del(&buf->list);
572 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
573 }
574 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
575}
576
577/* The user yanked out the cable... */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300578static void msi2500_disconnect(struct usb_interface *intf)
Antti Palosaari977e4442013-07-05 00:24:08 -0300579{
580 struct v4l2_device *v = usb_get_intfdata(intf);
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300581 struct msi2500_state *s =
582 container_of(v, struct msi2500_state, v4l2_dev);
Antti Palosaaric08de622014-07-22 02:44:12 -0300583
Antti Palosaari100b79312014-08-24 22:54:06 -0300584 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300585
586 mutex_lock(&s->vb_queue_lock);
587 mutex_lock(&s->v4l2_lock);
588 /* No need to keep the urbs around after disconnection */
589 s->udev = NULL;
Antti Palosaari977e4442013-07-05 00:24:08 -0300590 v4l2_device_disconnect(&s->v4l2_dev);
591 video_unregister_device(&s->vdev);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300592 spi_unregister_master(s->master);
Antti Palosaari977e4442013-07-05 00:24:08 -0300593 mutex_unlock(&s->v4l2_lock);
594 mutex_unlock(&s->vb_queue_lock);
595
596 v4l2_device_put(&s->v4l2_dev);
597}
598
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300599static int msi2500_querycap(struct file *file, void *fh,
Antti Palosaari977e4442013-07-05 00:24:08 -0300600 struct v4l2_capability *cap)
601{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300602 struct msi2500_state *s = video_drvdata(file);
Antti Palosaaric08de622014-07-22 02:44:12 -0300603
Antti Palosaari100b79312014-08-24 22:54:06 -0300604 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300605
606 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
607 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
608 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300609 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
610 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
Antti Palosaari977e4442013-07-05 00:24:08 -0300611 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
612 return 0;
613}
614
Antti Palosaari977e4442013-07-05 00:24:08 -0300615/* Videobuf2 operations */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300616static int msi2500_queue_setup(struct vb2_queue *vq,
Antti Palosaari977e4442013-07-05 00:24:08 -0300617 const struct v4l2_format *fmt, unsigned int *nbuffers,
618 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
619{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300620 struct msi2500_state *s = vb2_get_drv_priv(vq);
Antti Palosaaric08de622014-07-22 02:44:12 -0300621
Antti Palosaari100b79312014-08-24 22:54:06 -0300622 dev_dbg(s->dev, "nbuffers=%d\n", *nbuffers);
Antti Palosaari977e4442013-07-05 00:24:08 -0300623
624 /* Absolute min and max number of buffers available for mmap() */
Antti Palosaarid287e4e2014-02-26 20:48:23 -0300625 *nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32);
Antti Palosaari977e4442013-07-05 00:24:08 -0300626 *nplanes = 1;
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300627 sizes[0] = PAGE_ALIGN(s->buffersize);
Antti Palosaari100b79312014-08-24 22:54:06 -0300628 dev_dbg(s->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
Antti Palosaari977e4442013-07-05 00:24:08 -0300629 return 0;
630}
631
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300632static void msi2500_buf_queue(struct vb2_buffer *vb)
Antti Palosaari977e4442013-07-05 00:24:08 -0300633{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300634 struct msi2500_state *s = vb2_get_drv_priv(vb->vb2_queue);
635 struct msi2500_frame_buf *buf =
636 container_of(vb, struct msi2500_frame_buf, vb);
Antti Palosaarib63ab6b2014-08-24 23:26:23 -0300637 unsigned long flags;
Antti Palosaari977e4442013-07-05 00:24:08 -0300638
639 /* Check the device has not disconnected between prep and queuing */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300640 if (unlikely(!s->udev)) {
Antti Palosaari977e4442013-07-05 00:24:08 -0300641 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
642 return;
643 }
644
645 spin_lock_irqsave(&s->queued_bufs_lock, flags);
646 list_add_tail(&buf->list, &s->queued_bufs);
647 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
648}
649
650#define CMD_WREG 0x41
651#define CMD_START_STREAMING 0x43
652#define CMD_STOP_STREAMING 0x45
653#define CMD_READ_UNKNOW 0x48
654
Antti Palosaari100b79312014-08-24 22:54:06 -0300655#define msi2500_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
Antti Palosaaric08de622014-07-22 02:44:12 -0300656 char *_direction; \
657 if (_t & USB_DIR_IN) \
658 _direction = "<<<"; \
Antti Palosaari977e4442013-07-05 00:24:08 -0300659 else \
Antti Palosaaric08de622014-07-22 02:44:12 -0300660 _direction = ">>>"; \
Antti Palosaari100b79312014-08-24 22:54:06 -0300661 dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
662 _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \
663 _l & 0xff, _l >> 8, _direction, _l, _b); \
Antti Palosaari977e4442013-07-05 00:24:08 -0300664}
665
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300666static int msi2500_ctrl_msg(struct msi2500_state *s, u8 cmd, u32 data)
Antti Palosaari977e4442013-07-05 00:24:08 -0300667{
668 int ret;
669 u8 request = cmd;
670 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
671 u16 value = (data >> 0) & 0xffff;
672 u16 index = (data >> 16) & 0xffff;
673
Antti Palosaari100b79312014-08-24 22:54:06 -0300674 msi2500_dbg_usb_control_msg(s->dev,
Antti Palosaari977e4442013-07-05 00:24:08 -0300675 request, requesttype, value, index, NULL, 0);
Antti Palosaarid0fadf42013-07-11 12:19:32 -0300676 ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0),
Antti Palosaari977e4442013-07-05 00:24:08 -0300677 request, requesttype, value, index, NULL, 0, 2000);
Antti Palosaari977e4442013-07-05 00:24:08 -0300678 if (ret)
Antti Palosaari100b79312014-08-24 22:54:06 -0300679 dev_err(s->dev, "failed %d, cmd %02x, data %04x\n",
680 ret, cmd, data);
Antti Palosaari977e4442013-07-05 00:24:08 -0300681
682 return ret;
Antti Palosaari100b79312014-08-24 22:54:06 -0300683}
Antti Palosaari977e4442013-07-05 00:24:08 -0300684
Antti Palosaari977e4442013-07-05 00:24:08 -0300685#define F_REF 24000000
Antti Palosaarib1520852013-07-09 02:44:56 -0300686#define DIV_R_IN 2
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300687static int msi2500_set_usb_adc(struct msi2500_state *s)
Antti Palosaari977e4442013-07-05 00:24:08 -0300688{
Antti Palosaari22ca6802013-07-31 21:48:51 -0300689 int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300690 u32 reg3, reg4, reg7;
Antti Palosaari2e68f842014-02-02 23:42:00 -0300691 struct v4l2_ctrl *bandwidth_auto;
692 struct v4l2_ctrl *bandwidth;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300693
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300694 f_sr = s->f_adc;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300695
Antti Palosaari2e68f842014-02-02 23:42:00 -0300696 /* set tuner, subdev, filters according to sampling rate */
Antti Palosaaric08de622014-07-22 02:44:12 -0300697 bandwidth_auto = v4l2_ctrl_find(&s->hdl,
698 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300699 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
Antti Palosaaric08de622014-07-22 02:44:12 -0300700 bandwidth = v4l2_ctrl_find(&s->hdl,
701 V4L2_CID_RF_TUNER_BANDWIDTH);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300702 v4l2_ctrl_s_ctrl(bandwidth, s->f_adc);
703 }
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300704
705 /* select stream format */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300706 switch (s->pixelformat) {
707 case V4L2_SDR_FMT_CU8:
Antti Palosaari8591f702014-07-22 05:29:57 -0300708 reg7 = 0x000c9407; /* 504 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300709 break;
710 case V4L2_SDR_FMT_CU16LE:
Antti Palosaari8591f702014-07-22 05:29:57 -0300711 reg7 = 0x00009407; /* 252 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300712 break;
Antti Palosaari3912eb62014-07-13 22:27:21 -0300713 case V4L2_SDR_FMT_CS8:
Antti Palosaari8591f702014-07-22 05:29:57 -0300714 reg7 = 0x000c9407; /* 504 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300715 break;
Antti Palosaaric08de622014-07-22 02:44:12 -0300716 case MSI2500_PIX_FMT_SDR_MSI2500_384:
Antti Palosaari8591f702014-07-22 05:29:57 -0300717 reg7 = 0x0000a507; /* 384 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300718 break;
Antti Palosaaric08de622014-07-22 02:44:12 -0300719 case MSI2500_PIX_FMT_SDR_S12:
Antti Palosaari8591f702014-07-22 05:29:57 -0300720 reg7 = 0x00008507; /* 336 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300721 break;
Antti Palosaari3912eb62014-07-13 22:27:21 -0300722 case V4L2_SDR_FMT_CS14LE:
Antti Palosaari8591f702014-07-22 05:29:57 -0300723 reg7 = 0x00009407; /* 252 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300724 break;
725 default:
Antti Palosaari8591f702014-07-22 05:29:57 -0300726 reg7 = 0x000c9407; /* 504 */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300727 break;
Antti Palosaari554cbfb2013-08-05 20:43:36 -0300728 }
729
Antti Palosaari977e4442013-07-05 00:24:08 -0300730 /*
Antti Palosaarib1520852013-07-09 02:44:56 -0300731 * Synthesizer config is just a educated guess...
Antti Palosaari977e4442013-07-05 00:24:08 -0300732 *
Antti Palosaari977e4442013-07-05 00:24:08 -0300733 * [7:0] 0x03, register address
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300734 * [8] 1, power control
735 * [9] ?, power control
Antti Palosaari977e4442013-07-05 00:24:08 -0300736 * [12:10] output divider
737 * [13] 0 ?
738 * [14] 0 ?
Antti Palosaari22ca6802013-07-31 21:48:51 -0300739 * [15] fractional MSB, bit 20
Antti Palosaari977e4442013-07-05 00:24:08 -0300740 * [16:19] N
741 * [23:20] ?
742 * [24:31] 0x01
743 *
744 * output divider
745 * val div
746 * 0 - (invalid)
Antti Palosaarib1520852013-07-09 02:44:56 -0300747 * 1 4
748 * 2 6
749 * 3 8
750 * 4 10
751 * 5 12
752 * 6 14
753 * 7 16
754 *
755 * VCO 202000000 - 720000000++
Antti Palosaari977e4442013-07-05 00:24:08 -0300756 */
Antti Palosaaric5a431d2013-08-05 21:33:31 -0300757 reg3 = 0x01000303;
Antti Palosaari22ca6802013-07-31 21:48:51 -0300758 reg4 = 0x00000004;
Antti Palosaari977e4442013-07-05 00:24:08 -0300759
Antti Palosaaric5a431d2013-08-05 21:33:31 -0300760 /* XXX: Filters? AGC? */
761 if (f_sr < 6000000)
762 reg3 |= 0x1 << 20;
763 else if (f_sr < 7000000)
764 reg3 |= 0x5 << 20;
765 else if (f_sr < 8500000)
766 reg3 |= 0x9 << 20;
767 else
768 reg3 |= 0xd << 20;
769
Antti Palosaarib1520852013-07-09 02:44:56 -0300770 for (div_r_out = 4; div_r_out < 16; div_r_out += 2) {
771 f_vco = f_sr * div_r_out * 12;
Antti Palosaari100b79312014-08-24 22:54:06 -0300772 dev_dbg(s->dev, "div_r_out=%d f_vco=%d\n", div_r_out, f_vco);
Antti Palosaarib1520852013-07-09 02:44:56 -0300773 if (f_vco >= 202000000)
Antti Palosaari977e4442013-07-05 00:24:08 -0300774 break;
775 }
776
Antti Palosaarib1520852013-07-09 02:44:56 -0300777 div_n = f_vco / (F_REF * DIV_R_IN);
778 div_m = f_vco % (F_REF * DIV_R_IN);
Antti Palosaari22ca6802013-07-31 21:48:51 -0300779 fract = 0x200000ul * div_m / (F_REF * DIV_R_IN);
Antti Palosaarib1520852013-07-09 02:44:56 -0300780
Antti Palosaari977e4442013-07-05 00:24:08 -0300781 reg3 |= div_n << 16;
Antti Palosaarib1520852013-07-09 02:44:56 -0300782 reg3 |= (div_r_out / 2 - 1) << 10;
Antti Palosaari22ca6802013-07-31 21:48:51 -0300783 reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */
784 reg4 |= ((fract >> 0) & 0x0fffff) << 8; /* [19:0] */
Antti Palosaari977e4442013-07-05 00:24:08 -0300785
Antti Palosaari100b79312014-08-24 22:54:06 -0300786 dev_dbg(s->dev, "f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n",
787 f_sr, f_vco, div_n, div_m, div_r_out, reg3, reg4);
Antti Palosaari977e4442013-07-05 00:24:08 -0300788
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300789 ret = msi2500_ctrl_msg(s, CMD_WREG, 0x00608008);
Antti Palosaari977e4442013-07-05 00:24:08 -0300790 if (ret)
791 goto err;
792
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300793 ret = msi2500_ctrl_msg(s, CMD_WREG, 0x00000c05);
Antti Palosaari977e4442013-07-05 00:24:08 -0300794 if (ret)
795 goto err;
796
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300797 ret = msi2500_ctrl_msg(s, CMD_WREG, 0x00020000);
Antti Palosaari977e4442013-07-05 00:24:08 -0300798 if (ret)
799 goto err;
800
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300801 ret = msi2500_ctrl_msg(s, CMD_WREG, 0x00480102);
Antti Palosaari977e4442013-07-05 00:24:08 -0300802 if (ret)
803 goto err;
804
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300805 ret = msi2500_ctrl_msg(s, CMD_WREG, 0x00f38008);
Antti Palosaari977e4442013-07-05 00:24:08 -0300806 if (ret)
807 goto err;
808
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300809 ret = msi2500_ctrl_msg(s, CMD_WREG, reg7);
Antti Palosaari977e4442013-07-05 00:24:08 -0300810 if (ret)
811 goto err;
812
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300813 ret = msi2500_ctrl_msg(s, CMD_WREG, reg4);
Antti Palosaari977e4442013-07-05 00:24:08 -0300814 if (ret)
815 goto err;
816
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300817 ret = msi2500_ctrl_msg(s, CMD_WREG, reg3);
Antti Palosaari977e4442013-07-05 00:24:08 -0300818 if (ret)
819 goto err;
820err:
821 return ret;
Antti Palosaari100b79312014-08-24 22:54:06 -0300822}
Antti Palosaari977e4442013-07-05 00:24:08 -0300823
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300824static int msi2500_start_streaming(struct vb2_queue *vq, unsigned int count)
Antti Palosaari977e4442013-07-05 00:24:08 -0300825{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300826 struct msi2500_state *s = vb2_get_drv_priv(vq);
Antti Palosaari977e4442013-07-05 00:24:08 -0300827 int ret;
Antti Palosaaric08de622014-07-22 02:44:12 -0300828
Antti Palosaari100b79312014-08-24 22:54:06 -0300829 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300830
831 if (!s->udev)
832 return -ENODEV;
833
834 if (mutex_lock_interruptible(&s->v4l2_lock))
835 return -ERESTARTSYS;
836
Antti Palosaari2e68f842014-02-02 23:42:00 -0300837 /* wake-up tuner */
838 v4l2_subdev_call(s->v4l2_subdev, core, s_power, 1);
839
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300840 ret = msi2500_set_usb_adc(s);
Antti Palosaari977e4442013-07-05 00:24:08 -0300841
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300842 ret = msi2500_isoc_init(s);
Antti Palosaari977e4442013-07-05 00:24:08 -0300843 if (ret)
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300844 msi2500_cleanup_queued_bufs(s);
Antti Palosaari977e4442013-07-05 00:24:08 -0300845
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300846 ret = msi2500_ctrl_msg(s, CMD_START_STREAMING, 0);
Antti Palosaari977e4442013-07-05 00:24:08 -0300847
848 mutex_unlock(&s->v4l2_lock);
849
850 return ret;
851}
852
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300853static void msi2500_stop_streaming(struct vb2_queue *vq)
Antti Palosaari977e4442013-07-05 00:24:08 -0300854{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300855 struct msi2500_state *s = vb2_get_drv_priv(vq);
Hans Verkuile37559b2014-04-17 02:47:21 -0300856
Antti Palosaari100b79312014-08-24 22:54:06 -0300857 dev_dbg(s->dev, "\n");
Antti Palosaari977e4442013-07-05 00:24:08 -0300858
Hans Verkuile37559b2014-04-17 02:47:21 -0300859 mutex_lock(&s->v4l2_lock);
Antti Palosaari977e4442013-07-05 00:24:08 -0300860
Antti Palosaari977e4442013-07-05 00:24:08 -0300861 if (s->udev)
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300862 msi2500_isoc_cleanup(s);
Antti Palosaari977e4442013-07-05 00:24:08 -0300863
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300864 msi2500_cleanup_queued_bufs(s);
Antti Palosaarid0fadf42013-07-11 12:19:32 -0300865
866 /* according to tests, at least 700us delay is required */
867 msleep(20);
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300868 if (!msi2500_ctrl_msg(s, CMD_STOP_STREAMING, 0)) {
Hans Verkuile37559b2014-04-17 02:47:21 -0300869 /* sleep USB IF / ADC */
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300870 msi2500_ctrl_msg(s, CMD_WREG, 0x01000003);
Hans Verkuile37559b2014-04-17 02:47:21 -0300871 }
Antti Palosaarid0fadf42013-07-11 12:19:32 -0300872
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300873 /* sleep tuner */
Hans Verkuile37559b2014-04-17 02:47:21 -0300874 v4l2_subdev_call(s->v4l2_subdev, core, s_power, 0);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300875
Antti Palosaari977e4442013-07-05 00:24:08 -0300876 mutex_unlock(&s->v4l2_lock);
Antti Palosaari977e4442013-07-05 00:24:08 -0300877}
878
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300879static struct vb2_ops msi2500_vb2_ops = {
880 .queue_setup = msi2500_queue_setup,
881 .buf_queue = msi2500_buf_queue,
882 .start_streaming = msi2500_start_streaming,
883 .stop_streaming = msi2500_stop_streaming,
Antti Palosaari977e4442013-07-05 00:24:08 -0300884 .wait_prepare = vb2_ops_wait_prepare,
885 .wait_finish = vb2_ops_wait_finish,
886};
887
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300888static int msi2500_enum_fmt_sdr_cap(struct file *file, void *priv,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300889 struct v4l2_fmtdesc *f)
Antti Palosaari977e4442013-07-05 00:24:08 -0300890{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300891 struct msi2500_state *s = video_drvdata(file);
Antti Palosaaric08de622014-07-22 02:44:12 -0300892
Antti Palosaari100b79312014-08-24 22:54:06 -0300893 dev_dbg(s->dev, "index=%d\n", f->index);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300894
Antti Palosaari3912eb62014-07-13 22:27:21 -0300895 if (f->index >= s->num_formats)
Antti Palosaari977e4442013-07-05 00:24:08 -0300896 return -EINVAL;
897
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300898 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
899 f->pixelformat = formats[f->index].pixelformat;
Antti Palosaari977e4442013-07-05 00:24:08 -0300900
901 return 0;
902}
903
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300904static int msi2500_g_fmt_sdr_cap(struct file *file, void *priv,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300905 struct v4l2_format *f)
Antti Palosaari977e4442013-07-05 00:24:08 -0300906{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300907 struct msi2500_state *s = video_drvdata(file);
Antti Palosaaric08de622014-07-22 02:44:12 -0300908
Antti Palosaari100b79312014-08-24 22:54:06 -0300909 dev_dbg(s->dev, "pixelformat fourcc %4.4s\n",
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300910 (char *)&s->pixelformat);
911
912 f->fmt.sdr.pixelformat = s->pixelformat;
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300913 f->fmt.sdr.buffersize = s->buffersize;
914 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari977e4442013-07-05 00:24:08 -0300915
916 return 0;
917}
918
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300919static int msi2500_s_fmt_sdr_cap(struct file *file, void *priv,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300920 struct v4l2_format *f)
Antti Palosaari977e4442013-07-05 00:24:08 -0300921{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300922 struct msi2500_state *s = video_drvdata(file);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300923 struct vb2_queue *q = &s->vb_queue;
924 int i;
Antti Palosaaric08de622014-07-22 02:44:12 -0300925
Antti Palosaari100b79312014-08-24 22:54:06 -0300926 dev_dbg(s->dev, "pixelformat fourcc %4.4s\n",
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300927 (char *)&f->fmt.sdr.pixelformat);
928
929 if (vb2_is_busy(q))
930 return -EBUSY;
931
Antti Palosaaric3509122014-03-10 16:30:44 -0300932 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari3912eb62014-07-13 22:27:21 -0300933 for (i = 0; i < s->num_formats; i++) {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300934 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300935 s->pixelformat = formats[i].pixelformat;
936 s->buffersize = formats[i].buffersize;
937 f->fmt.sdr.buffersize = formats[i].buffersize;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300938 return 0;
939 }
940 }
941
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300942 s->pixelformat = formats[0].pixelformat;
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300943 s->buffersize = formats[0].buffersize;
944 f->fmt.sdr.pixelformat = formats[0].pixelformat;
945 f->fmt.sdr.buffersize = formats[0].buffersize;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300946
947 return 0;
Antti Palosaari977e4442013-07-05 00:24:08 -0300948}
949
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300950static int msi2500_try_fmt_sdr_cap(struct file *file, void *priv,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300951 struct v4l2_format *f)
952{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300953 struct msi2500_state *s = video_drvdata(file);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300954 int i;
Antti Palosaaric08de622014-07-22 02:44:12 -0300955
Antti Palosaari100b79312014-08-24 22:54:06 -0300956 dev_dbg(s->dev, "pixelformat fourcc %4.4s\n",
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300957 (char *)&f->fmt.sdr.pixelformat);
958
Antti Palosaaric3509122014-03-10 16:30:44 -0300959 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari3912eb62014-07-13 22:27:21 -0300960 for (i = 0; i < s->num_formats; i++) {
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300961 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
962 f->fmt.sdr.buffersize = formats[i].buffersize;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300963 return 0;
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300964 }
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300965 }
966
967 f->fmt.sdr.pixelformat = formats[0].pixelformat;
Antti Palosaaria54e0fe2014-07-18 20:56:01 -0300968 f->fmt.sdr.buffersize = formats[0].buffersize;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300969
970 return 0;
971}
972
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300973static int msi2500_s_tuner(struct file *file, void *priv,
Antti Palosaari977e4442013-07-05 00:24:08 -0300974 const struct v4l2_tuner *v)
975{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300976 struct msi2500_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300977 int ret;
Antti Palosaaric08de622014-07-22 02:44:12 -0300978
Antti Palosaari100b79312014-08-24 22:54:06 -0300979 dev_dbg(s->dev, "index=%d\n", v->index);
Antti Palosaari977e4442013-07-05 00:24:08 -0300980
Antti Palosaari2e68f842014-02-02 23:42:00 -0300981 if (v->index == 0)
982 ret = 0;
983 else if (v->index == 1)
984 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_tuner, v);
985 else
986 ret = -EINVAL;
987
988 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -0300989}
990
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300991static int msi2500_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
Antti Palosaari977e4442013-07-05 00:24:08 -0300992{
Antti Palosaari06ce32c2014-07-22 05:43:06 -0300993 struct msi2500_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -0300994 int ret;
Antti Palosaaric08de622014-07-22 02:44:12 -0300995
Antti Palosaari100b79312014-08-24 22:54:06 -0300996 dev_dbg(s->dev, "index=%d\n", v->index);
Antti Palosaari977e4442013-07-05 00:24:08 -0300997
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -0300998 if (v->index == 0) {
Antti Palosaari2e68f842014-02-02 23:42:00 -0300999 strlcpy(v->name, "Mirics MSi2500", sizeof(v->name));
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001000 v->type = V4L2_TUNER_ADC;
1001 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1002 v->rangelow = 1200000;
1003 v->rangehigh = 15000000;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001004 ret = 0;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001005 } else if (v->index == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001006 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_tuner, v);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001007 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001008 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001009 }
Antti Palosaari977e4442013-07-05 00:24:08 -03001010
Antti Palosaari2e68f842014-02-02 23:42:00 -03001011 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -03001012}
1013
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001014static int msi2500_g_frequency(struct file *file, void *priv,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001015 struct v4l2_frequency *f)
1016{
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001017 struct msi2500_state *s = video_drvdata(file);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001018 int ret = 0;
Antti Palosaaric08de622014-07-22 02:44:12 -03001019
Antti Palosaari100b79312014-08-24 22:54:06 -03001020 dev_dbg(s->dev, "tuner=%d type=%d\n", f->tuner, f->type);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001021
Antti Palosaari2e68f842014-02-02 23:42:00 -03001022 if (f->tuner == 0) {
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001023 f->frequency = s->f_adc;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001024 ret = 0;
1025 } else if (f->tuner == 1) {
Antti Palosaaric3509122014-03-10 16:30:44 -03001026 f->type = V4L2_TUNER_RF;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001027 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_frequency, f);
1028 } else {
1029 ret = -EINVAL;
1030 }
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001031
1032 return ret;
1033}
1034
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001035static int msi2500_s_frequency(struct file *file, void *priv,
Antti Palosaari977e4442013-07-05 00:24:08 -03001036 const struct v4l2_frequency *f)
1037{
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001038 struct msi2500_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001039 int ret;
Antti Palosaaric08de622014-07-22 02:44:12 -03001040
Antti Palosaari100b79312014-08-24 22:54:06 -03001041 dev_dbg(s->dev, "tuner=%d type=%d frequency=%u\n",
1042 f->tuner, f->type, f->frequency);
Antti Palosaari977e4442013-07-05 00:24:08 -03001043
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001044 if (f->tuner == 0) {
1045 s->f_adc = clamp_t(unsigned int, f->frequency,
Antti Palosaari2e68f842014-02-02 23:42:00 -03001046 bands[0].rangelow,
1047 bands[0].rangehigh);
Antti Palosaari100b79312014-08-24 22:54:06 -03001048 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc);
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001049 ret = msi2500_set_usb_adc(s);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001050 } else if (f->tuner == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001051 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_frequency, f);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001052 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001053 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001054 }
1055
1056 return ret;
1057}
1058
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001059static int msi2500_enum_freq_bands(struct file *file, void *priv,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001060 struct v4l2_frequency_band *band)
1061{
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001062 struct msi2500_state *s = video_drvdata(file);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001063 int ret;
Antti Palosaaric08de622014-07-22 02:44:12 -03001064
Antti Palosaari100b79312014-08-24 22:54:06 -03001065 dev_dbg(s->dev, "tuner=%d type=%d index=%d\n",
1066 band->tuner, band->type, band->index);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001067
1068 if (band->tuner == 0) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001069 if (band->index >= ARRAY_SIZE(bands)) {
1070 ret = -EINVAL;
1071 } else {
1072 *band = bands[band->index];
1073 ret = 0;
1074 }
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001075 } else if (band->tuner == 1) {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001076 ret = v4l2_subdev_call(s->v4l2_subdev, tuner,
1077 enum_freq_bands, band);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001078 } else {
Antti Palosaari2e68f842014-02-02 23:42:00 -03001079 ret = -EINVAL;
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001080 }
1081
Antti Palosaari2e68f842014-02-02 23:42:00 -03001082 return ret;
Antti Palosaari977e4442013-07-05 00:24:08 -03001083}
1084
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001085static const struct v4l2_ioctl_ops msi2500_ioctl_ops = {
1086 .vidioc_querycap = msi2500_querycap,
Antti Palosaari977e4442013-07-05 00:24:08 -03001087
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001088 .vidioc_enum_fmt_sdr_cap = msi2500_enum_fmt_sdr_cap,
1089 .vidioc_g_fmt_sdr_cap = msi2500_g_fmt_sdr_cap,
1090 .vidioc_s_fmt_sdr_cap = msi2500_s_fmt_sdr_cap,
1091 .vidioc_try_fmt_sdr_cap = msi2500_try_fmt_sdr_cap,
Antti Palosaari977e4442013-07-05 00:24:08 -03001092
1093 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1094 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1095 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1096 .vidioc_querybuf = vb2_ioctl_querybuf,
1097 .vidioc_qbuf = vb2_ioctl_qbuf,
1098 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1099
1100 .vidioc_streamon = vb2_ioctl_streamon,
1101 .vidioc_streamoff = vb2_ioctl_streamoff,
1102
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001103 .vidioc_g_tuner = msi2500_g_tuner,
1104 .vidioc_s_tuner = msi2500_s_tuner,
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001105
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001106 .vidioc_g_frequency = msi2500_g_frequency,
1107 .vidioc_s_frequency = msi2500_s_frequency,
1108 .vidioc_enum_freq_bands = msi2500_enum_freq_bands,
Antti Palosaari977e4442013-07-05 00:24:08 -03001109
1110 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1111 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1112 .vidioc_log_status = v4l2_ctrl_log_status,
1113};
1114
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001115static const struct v4l2_file_operations msi2500_fops = {
Antti Palosaari977e4442013-07-05 00:24:08 -03001116 .owner = THIS_MODULE,
1117 .open = v4l2_fh_open,
1118 .release = vb2_fop_release,
1119 .read = vb2_fop_read,
1120 .poll = vb2_fop_poll,
1121 .mmap = vb2_fop_mmap,
1122 .unlocked_ioctl = video_ioctl2,
1123};
1124
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001125static struct video_device msi2500_template = {
Antti Palosaari977e4442013-07-05 00:24:08 -03001126 .name = "Mirics MSi3101 SDR Dongle",
1127 .release = video_device_release_empty,
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001128 .fops = &msi2500_fops,
1129 .ioctl_ops = &msi2500_ioctl_ops,
Antti Palosaari977e4442013-07-05 00:24:08 -03001130};
1131
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001132static void msi2500_video_release(struct v4l2_device *v)
Antti Palosaari977e4442013-07-05 00:24:08 -03001133{
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001134 struct msi2500_state *s =
1135 container_of(v, struct msi2500_state, v4l2_dev);
Antti Palosaari977e4442013-07-05 00:24:08 -03001136
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001137 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaari977e4442013-07-05 00:24:08 -03001138 v4l2_device_unregister(&s->v4l2_dev);
1139 kfree(s);
1140}
1141
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001142static int msi2500_transfer_one_message(struct spi_master *master,
Antti Palosaari2e68f842014-02-02 23:42:00 -03001143 struct spi_message *m)
1144{
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001145 struct msi2500_state *s = spi_master_get_devdata(master);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001146 struct spi_transfer *t;
1147 int ret = 0;
1148 u32 data;
1149
1150 list_for_each_entry(t, &m->transfers, transfer_list) {
Antti Palosaari100b79312014-08-24 22:54:06 -03001151 dev_dbg(s->dev, "msg=%*ph\n", t->len, t->tx_buf);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001152 data = 0x09; /* reg 9 is SPI adapter */
1153 data |= ((u8 *)t->tx_buf)[0] << 8;
1154 data |= ((u8 *)t->tx_buf)[1] << 16;
1155 data |= ((u8 *)t->tx_buf)[2] << 24;
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001156 ret = msi2500_ctrl_msg(s, CMD_WREG, data);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001157 }
1158
1159 m->status = ret;
1160 spi_finalize_current_message(master);
1161 return ret;
1162}
1163
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001164static int msi2500_probe(struct usb_interface *intf,
Antti Palosaari977e4442013-07-05 00:24:08 -03001165 const struct usb_device_id *id)
1166{
Antti Palosaari100b79312014-08-24 22:54:06 -03001167 struct msi2500_state *s;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001168 struct v4l2_subdev *sd;
1169 struct spi_master *master;
Antti Palosaari977e4442013-07-05 00:24:08 -03001170 int ret;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001171 static struct spi_board_info board_info = {
1172 .modalias = "msi001",
1173 .bus_num = 0,
1174 .chip_select = 0,
1175 .max_speed_hz = 12000000,
Antti Palosaari977e4442013-07-05 00:24:08 -03001176 };
1177
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001178 s = kzalloc(sizeof(struct msi2500_state), GFP_KERNEL);
Antti Palosaari977e4442013-07-05 00:24:08 -03001179 if (s == NULL) {
Antti Palosaari100b79312014-08-24 22:54:06 -03001180 dev_err(&intf->dev, "Could not allocate memory for state\n");
Antti Palosaari977e4442013-07-05 00:24:08 -03001181 return -ENOMEM;
1182 }
1183
1184 mutex_init(&s->v4l2_lock);
1185 mutex_init(&s->vb_queue_lock);
1186 spin_lock_init(&s->queued_bufs_lock);
1187 INIT_LIST_HEAD(&s->queued_bufs);
Antti Palosaari100b79312014-08-24 22:54:06 -03001188 s->dev = &intf->dev;
1189 s->udev = interface_to_usbdev(intf);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001190 s->f_adc = bands[0].rangelow;
Antti Palosaari3912eb62014-07-13 22:27:21 -03001191 s->pixelformat = formats[0].pixelformat;
Antti Palosaaria54e0fe2014-07-18 20:56:01 -03001192 s->buffersize = formats[0].buffersize;
Antti Palosaari3912eb62014-07-13 22:27:21 -03001193 s->num_formats = NUM_FORMATS;
Mauro Carvalho Chehabad7b8c02014-09-03 16:19:29 -03001194 if (!msi2500_emulated_fmt)
Antti Palosaari3912eb62014-07-13 22:27:21 -03001195 s->num_formats -= 2;
Antti Palosaari977e4442013-07-05 00:24:08 -03001196
1197 /* Init videobuf2 queue structure */
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001198 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
Antti Palosaari977e4442013-07-05 00:24:08 -03001199 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1200 s->vb_queue.drv_priv = s;
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001201 s->vb_queue.buf_struct_size = sizeof(struct msi2500_frame_buf);
1202 s->vb_queue.ops = &msi2500_vb2_ops;
Antti Palosaari977e4442013-07-05 00:24:08 -03001203 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03001204 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Antti Palosaari977e4442013-07-05 00:24:08 -03001205 ret = vb2_queue_init(&s->vb_queue);
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001206 if (ret) {
Antti Palosaari100b79312014-08-24 22:54:06 -03001207 dev_err(s->dev, "Could not initialize vb2 queue\n");
Antti Palosaari977e4442013-07-05 00:24:08 -03001208 goto err_free_mem;
1209 }
1210
1211 /* Init video_device structure */
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001212 s->vdev = msi2500_template;
Antti Palosaari977e4442013-07-05 00:24:08 -03001213 s->vdev.queue = &s->vb_queue;
1214 s->vdev.queue->lock = &s->vb_queue_lock;
Antti Palosaari977e4442013-07-05 00:24:08 -03001215 video_set_drvdata(&s->vdev, s);
1216
Antti Palosaari977e4442013-07-05 00:24:08 -03001217 /* Register the v4l2_device structure */
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001218 s->v4l2_dev.release = msi2500_video_release;
Antti Palosaari977e4442013-07-05 00:24:08 -03001219 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1220 if (ret) {
Antti Palosaari100b79312014-08-24 22:54:06 -03001221 dev_err(s->dev, "Failed to register v4l2-device (%d)\n", ret);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001222 goto err_free_mem;
1223 }
1224
1225 /* SPI master adapter */
Antti Palosaari100b79312014-08-24 22:54:06 -03001226 master = spi_alloc_master(s->dev, 0);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001227 if (master == NULL) {
1228 ret = -ENOMEM;
1229 goto err_unregister_v4l2_dev;
1230 }
1231
1232 s->master = master;
1233 master->bus_num = 0;
1234 master->num_chipselect = 1;
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001235 master->transfer_one_message = msi2500_transfer_one_message;
Antti Palosaari2e68f842014-02-02 23:42:00 -03001236 spi_master_set_devdata(master, s);
1237 ret = spi_register_master(master);
1238 if (ret) {
1239 spi_master_put(master);
1240 goto err_unregister_v4l2_dev;
1241 }
1242
1243 /* load v4l2 subdevice */
1244 sd = v4l2_spi_new_subdev(&s->v4l2_dev, master, &board_info);
1245 s->v4l2_subdev = sd;
1246 if (sd == NULL) {
Antti Palosaari100b79312014-08-24 22:54:06 -03001247 dev_err(s->dev, "cannot get v4l2 subdevice\n");
Antti Palosaari2e68f842014-02-02 23:42:00 -03001248 ret = -ENODEV;
1249 goto err_unregister_master;
1250 }
1251
1252 /* Register controls */
1253 v4l2_ctrl_handler_init(&s->hdl, 0);
1254 if (s->hdl.error) {
1255 ret = s->hdl.error;
Antti Palosaari100b79312014-08-24 22:54:06 -03001256 dev_err(s->dev, "Could not initialize controls\n");
Antti Palosaari977e4442013-07-05 00:24:08 -03001257 goto err_free_controls;
1258 }
1259
Antti Palosaari2e68f842014-02-02 23:42:00 -03001260 /* currently all controls are from subdev */
1261 v4l2_ctrl_add_handler(&s->hdl, sd->ctrl_handler, NULL);
1262
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001263 s->v4l2_dev.ctrl_handler = &s->hdl;
Antti Palosaari977e4442013-07-05 00:24:08 -03001264 s->vdev.v4l2_dev = &s->v4l2_dev;
1265 s->vdev.lock = &s->v4l2_lock;
1266
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001267 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
1268 if (ret) {
Antti Palosaari100b79312014-08-24 22:54:06 -03001269 dev_err(s->dev, "Failed to register as video device (%d)\n",
Antti Palosaari977e4442013-07-05 00:24:08 -03001270 ret);
1271 goto err_unregister_v4l2_dev;
1272 }
Antti Palosaari100b79312014-08-24 22:54:06 -03001273 dev_info(s->dev, "Registered as %s\n",
Antti Palosaari977e4442013-07-05 00:24:08 -03001274 video_device_node_name(&s->vdev));
Antti Palosaari100b79312014-08-24 22:54:06 -03001275 dev_notice(s->dev, "SDR API is still slightly experimental and functionality changes may follow\n");
Antti Palosaari977e4442013-07-05 00:24:08 -03001276
1277 return 0;
1278
Antti Palosaari977e4442013-07-05 00:24:08 -03001279err_free_controls:
Antti Palosaari3d0c8fa2013-11-17 03:04:51 -03001280 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaari2e68f842014-02-02 23:42:00 -03001281err_unregister_master:
1282 spi_unregister_master(s->master);
Antti Palosaari977e4442013-07-05 00:24:08 -03001283err_unregister_v4l2_dev:
1284 v4l2_device_unregister(&s->v4l2_dev);
Antti Palosaari977e4442013-07-05 00:24:08 -03001285err_free_mem:
1286 kfree(s);
1287 return ret;
1288}
1289
1290/* USB device ID list */
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001291static struct usb_device_id msi2500_id_table[] = {
Antti Palosaari00e049b2013-08-07 15:06:29 -03001292 { USB_DEVICE(0x1df7, 0x2500) }, /* Mirics MSi3101 SDR Dongle */
Antti Palosaari42fc5b42013-07-17 15:05:27 -03001293 { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */
Antti Palosaari977e4442013-07-05 00:24:08 -03001294 { }
1295};
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001296MODULE_DEVICE_TABLE(usb, msi2500_id_table);
Antti Palosaari977e4442013-07-05 00:24:08 -03001297
1298/* USB subsystem interface */
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001299static struct usb_driver msi2500_driver = {
Antti Palosaari977e4442013-07-05 00:24:08 -03001300 .name = KBUILD_MODNAME,
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001301 .probe = msi2500_probe,
1302 .disconnect = msi2500_disconnect,
1303 .id_table = msi2500_id_table,
Antti Palosaari977e4442013-07-05 00:24:08 -03001304};
1305
Antti Palosaari06ce32c2014-07-22 05:43:06 -03001306module_usb_driver(msi2500_driver);
Antti Palosaari977e4442013-07-05 00:24:08 -03001307
1308MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1309MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1310MODULE_LICENSE("GPL");