]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/usb/msi2500/msi2500.c
[media] media: videobuf2: Restructure vb2_buffer
[mirror_ubuntu-artful-kernel.git] / drivers / media / usb / msi2500 / msi2500.c
CommitLineData
977e444f 1/*
f7e5a655 2 * Mirics MSi2500 driver
977e444f
AP
3 * Mirics MSi3101 SDR Dongle driver
4 *
5 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
00e049b0
AP
17 * That driver is somehow based of pwc driver:
18 * (C) 1999-2004 Nemosoft Unv.
19 * (C) 2004-2006 Luc Saillard (luc@saillard.org)
20 * (C) 2011 Hans de Goede <hdegoede@redhat.com>
977e444f
AP
21 */
22
977e444f 23#include <linux/module.h>
977e444f 24#include <linux/slab.h>
04e40bdd 25#include <asm/div64.h>
977e444f
AP
26#include <media/v4l2-device.h>
27#include <media/v4l2-ioctl.h>
28#include <media/v4l2-ctrls.h>
29#include <media/v4l2-event.h>
30#include <linux/usb.h>
2d700715 31#include <media/videobuf2-v4l2.h>
977e444f 32#include <media/videobuf2-vmalloc.h>
2e68f841 33#include <linux/spi/spi.h>
977e444f 34
06ce32cb
AP
35static bool msi2500_emulated_fmt;
36module_param_named(emulated_formats, msi2500_emulated_fmt, bool, 0644);
3912eb6d
AP
37MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
38
977e444f
AP
39/*
40 * iConfiguration 0
41 * bInterfaceNumber 0
42 * bAlternateSetting 1
43 * bNumEndpoints 1
44 * bEndpointAddress 0x81 EP 1 IN
45 * bmAttributes 1
46 * Transfer Type Isochronous
47 * wMaxPacketSize 0x1400 3x 1024 bytes
48 * bInterval 1
49 */
50#define MAX_ISO_BUFS (8)
51#define ISO_FRAMES_PER_DESC (8)
52#define ISO_MAX_FRAME_SIZE (3 * 1024)
53#define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
977e444f
AP
54#define MAX_ISOC_ERRORS 20
55
c08de62f
AP
56/*
57 * TODO: These formats should be moved to V4L2 API. Formats are currently
58 * disabled from formats[] table, not visible to userspace.
59 */
60 /* signed 12-bit */
61#define MSI2500_PIX_FMT_SDR_S12 v4l2_fourcc('D', 'S', '1', '2')
62/* Mirics MSi2500 format 384 */
63#define MSI2500_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4')
3d0c8fa3 64
2e68f841 65static const struct v4l2_frequency_band bands[] = {
3d0c8fa3
AP
66 {
67 .tuner = 0,
68 .type = V4L2_TUNER_ADC,
69 .index = 0,
70 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
71 .rangelow = 1200000,
72 .rangehigh = 15000000,
73 },
74};
75
3d0c8fa3 76/* stream formats */
06ce32cb 77struct msi2500_format {
3d0c8fa3
AP
78 char *name;
79 u32 pixelformat;
a54e0fee 80 u32 buffersize;
3d0c8fa3
AP
81};
82
83/* format descriptions for capture and preview */
06ce32cb 84static struct msi2500_format formats[] = {
3d0c8fa3 85 {
3912eb6d
AP
86 .name = "Complex S8",
87 .pixelformat = V4L2_SDR_FMT_CS8,
a54e0fee 88 .buffersize = 3 * 1008,
3d0c8fa3 89#if 0
3d0c8fa3
AP
90 }, {
91 .name = "10+2-bit signed",
c08de62f 92 .pixelformat = MSI2500_PIX_FMT_SDR_MSI2500_384,
3d0c8fa3
AP
93 }, {
94 .name = "12-bit signed",
c08de62f 95 .pixelformat = MSI2500_PIX_FMT_SDR_S12,
3d0c8fa3 96#endif
3912eb6d
AP
97 }, {
98 .name = "Complex S14LE",
99 .pixelformat = V4L2_SDR_FMT_CS14LE,
a54e0fee 100 .buffersize = 3 * 1008,
3912eb6d
AP
101 }, {
102 .name = "Complex U8 (emulated)",
103 .pixelformat = V4L2_SDR_FMT_CU8,
a54e0fee 104 .buffersize = 3 * 1008,
3912eb6d
AP
105 }, {
106 .name = "Complex U16LE (emulated)",
107 .pixelformat = V4L2_SDR_FMT_CU16LE,
a54e0fee 108 .buffersize = 3 * 1008,
3d0c8fa3
AP
109 },
110};
111
112static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
977e444f
AP
113
114/* intermediate buffers with raw data from the USB device */
06ce32cb 115struct msi2500_frame_buf {
2d700715
JS
116 /* common v4l buffer stuff -- must be first */
117 struct vb2_v4l2_buffer vb;
977e444f 118 struct list_head list;
977e444f
AP
119};
120
f7e5a655 121struct msi2500_dev {
100b7931 122 struct device *dev;
977e444f
AP
123 struct video_device vdev;
124 struct v4l2_device v4l2_dev;
2e68f841
AP
125 struct v4l2_subdev *v4l2_subdev;
126 struct spi_master *master;
977e444f
AP
127
128 /* videobuf2 queue and queued buffers list */
129 struct vb2_queue vb_queue;
130 struct list_head queued_bufs;
131 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
132
133 /* Note if taking both locks v4l2_lock must always be locked first! */
134 struct mutex v4l2_lock; /* Protects everything else */
135 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
136
137 /* Pointer to our usb_device, will be NULL after unplug */
138 struct usb_device *udev; /* Both mutexes most be hold when setting! */
139
2e68f841 140 unsigned int f_adc;
3d0c8fa3 141 u32 pixelformat;
a54e0fee 142 u32 buffersize;
3912eb6d 143 unsigned int num_formats;
3d0c8fa3 144
977e444f
AP
145 unsigned int isoc_errors; /* number of contiguous ISOC errors */
146 unsigned int vb_full; /* vb is full and packets dropped */
147
148 struct urb *urbs[MAX_ISO_BUFS];
149
150 /* Controls */
3d0c8fa3 151 struct v4l2_ctrl_handler hdl;
977e444f 152
34599b9b
AP
153 u32 next_sample; /* for track lost packets */
154 u32 sample; /* for sample rate calc */
3d0c8fa3 155 unsigned long jiffies_next;
977e444f
AP
156};
157
158/* Private functions */
06ce32cb 159static struct msi2500_frame_buf *msi2500_get_next_fill_buf(
f7e5a655 160 struct msi2500_dev *dev)
977e444f 161{
b63ab6b0 162 unsigned long flags;
06ce32cb 163 struct msi2500_frame_buf *buf = NULL;
977e444f 164
f7e5a655
AP
165 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
166 if (list_empty(&dev->queued_bufs))
977e444f
AP
167 goto leave;
168
f7e5a655 169 buf = list_entry(dev->queued_bufs.next, struct msi2500_frame_buf, list);
977e444f
AP
170 list_del(&buf->list);
171leave:
f7e5a655 172 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
977e444f
AP
173 return buf;
174}
175
02004681
AP
176/*
177 * +===========================================================================
3d0c8fa3 178 * | 00-1023 | USB packet type '504'
02004681
AP
179 * +===========================================================================
180 * | 00- 03 | sequence number of first sample in that USB packet
181 * +---------------------------------------------------------------------------
182 * | 04- 15 | garbage
183 * +---------------------------------------------------------------------------
3d0c8fa3 184 * | 16-1023 | samples
02004681 185 * +---------------------------------------------------------------------------
3d0c8fa3
AP
186 * signed 8-bit sample
187 * 504 * 2 = 1008 samples
8591f708
AP
188 *
189 *
3d0c8fa3
AP
190 * +===========================================================================
191 * | 00-1023 | USB packet type '384'
192 * +===========================================================================
193 * | 00- 03 | sequence number of first sample in that USB packet
194 * +---------------------------------------------------------------------------
195 * | 04- 15 | garbage
196 * +---------------------------------------------------------------------------
197 * | 16- 175 | samples
198 * +---------------------------------------------------------------------------
199 * | 176- 179 | control bits for previous samples
200 * +---------------------------------------------------------------------------
201 * | 180- 339 | samples
202 * +---------------------------------------------------------------------------
203 * | 340- 343 | control bits for previous samples
204 * +---------------------------------------------------------------------------
205 * | 344- 503 | samples
206 * +---------------------------------------------------------------------------
207 * | 504- 507 | control bits for previous samples
208 * +---------------------------------------------------------------------------
209 * | 508- 667 | samples
210 * +---------------------------------------------------------------------------
211 * | 668- 671 | control bits for previous samples
212 * +---------------------------------------------------------------------------
213 * | 672- 831 | samples
214 * +---------------------------------------------------------------------------
215 * | 832- 835 | control bits for previous samples
216 * +---------------------------------------------------------------------------
217 * | 836- 995 | samples
218 * +---------------------------------------------------------------------------
219 * | 996- 999 | control bits for previous samples
220 * +---------------------------------------------------------------------------
221 * | 1000-1023 | garbage
222 * +---------------------------------------------------------------------------
223 *
224 * Bytes 4 - 7 could have some meaning?
225 *
226 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
227 * numbers. This results one 2-bit number for 8 samples. It is likely used for
228 * for bit shifting sample by given bits, increasing actual sampling resolution.
229 * Number 2 (0b10) was never seen.
230 *
231 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
8591f708
AP
232 *
233 *
3d0c8fa3
AP
234 * +===========================================================================
235 * | 00-1023 | USB packet type '336'
236 * +===========================================================================
237 * | 00- 03 | sequence number of first sample in that USB packet
238 * +---------------------------------------------------------------------------
239 * | 04- 15 | garbage
240 * +---------------------------------------------------------------------------
241 * | 16-1023 | samples
242 * +---------------------------------------------------------------------------
243 * signed 12-bit sample
8591f708
AP
244 *
245 *
3d0c8fa3
AP
246 * +===========================================================================
247 * | 00-1023 | USB packet type '252'
248 * +===========================================================================
249 * | 00- 03 | sequence number of first sample in that USB packet
250 * +---------------------------------------------------------------------------
251 * | 04- 15 | garbage
252 * +---------------------------------------------------------------------------
253 * | 16-1023 | samples
254 * +---------------------------------------------------------------------------
255 * signed 14-bit sample
554cbfbe 256 */
3d0c8fa3 257
f7e5a655
AP
258static int msi2500_convert_stream(struct msi2500_dev *dev, u8 *dst, u8 *src,
259 unsigned int src_len)
554cbfbe 260{
8591f708
AP
261 unsigned int i, j, transactions, dst_len = 0;
262 u32 sample[3];
554cbfbe 263
8591f708
AP
264 /* There could be 1-3 1024 byte transactions per packet */
265 transactions = src_len / 1024;
554cbfbe 266
8591f708
AP
267 for (i = 0; i < transactions; i++) {
268 sample[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 |
269 src[0] << 0;
f7e5a655
AP
270 if (i == 0 && dev->next_sample != sample[0]) {
271 dev_dbg_ratelimited(dev->dev,
272 "%d samples lost, %d %08x:%08x\n",
273 sample[0] - dev->next_sample,
274 src_len, dev->next_sample,
275 sample[0]);
554cbfbe
AP
276 }
277
278 /*
279 * Dump all unknown 'garbage' data - maybe we will discover
280 * someday if there is something rational...
281 */
f7e5a655 282 dev_dbg_ratelimited(dev->dev, "%*ph\n", 12, &src[4]);
554cbfbe 283
8591f708 284 src += 16; /* skip header */
554cbfbe 285
f7e5a655 286 switch (dev->pixelformat) {
8591f708
AP
287 case V4L2_SDR_FMT_CU8: /* 504 x IQ samples */
288 {
f7e5a655
AP
289 s8 *s8src = (s8 *)src;
290 u8 *u8dst = (u8 *)dst;
554cbfbe 291
8591f708
AP
292 for (j = 0; j < 1008; j++)
293 *u8dst++ = *s8src++ + 128;
3d0c8fa3 294
8591f708
AP
295 src += 1008;
296 dst += 1008;
297 dst_len += 1008;
f7e5a655 298 dev->next_sample = sample[i] + 504;
8591f708 299 break;
554cbfbe 300 }
8591f708
AP
301 case V4L2_SDR_FMT_CU16LE: /* 252 x IQ samples */
302 {
f7e5a655
AP
303 s16 *s16src = (s16 *)src;
304 u16 *u16dst = (u16 *)dst;
8591f708
AP
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 }
3d0c8fa3 316
8591f708
AP
317 src += 1008;
318 dst += 1008;
319 dst_len += 1008;
f7e5a655 320 dev->next_sample = sample[i] + 252;
8591f708
AP
321 break;
322 }
323 case MSI2500_PIX_FMT_SDR_MSI2500_384: /* 384 x IQ samples */
324 /* Dump unknown 'garbage' data */
f7e5a655 325 dev_dbg_ratelimited(dev->dev, "%*ph\n", 24, &src[1000]);
8591f708
AP
326 memcpy(dst, src, 984);
327 src += 984 + 24;
328 dst += 984;
329 dst_len += 984;
f7e5a655 330 dev->next_sample = sample[i] + 384;
8591f708
AP
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;
f7e5a655 337 dev->next_sample = sample[i] + 504;
8591f708
AP
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;
f7e5a655 344 dev->next_sample = sample[i] + 336;
8591f708
AP
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;
f7e5a655 351 dev->next_sample = sample[i] + 252;
8591f708
AP
352 break;
353 default:
354 break;
355 }
554cbfbe
AP
356 }
357
8591f708 358 /* calculate sample rate and output it in 10 seconds intervals */
f7e5a655 359 if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
8591f708
AP
360 #define MSECS 10000UL
361 unsigned int msecs = jiffies_to_msecs(jiffies -
f7e5a655
AP
362 dev->jiffies_next + msecs_to_jiffies(MSECS));
363 unsigned int samples = dev->next_sample - dev->sample;
364
365 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
366 dev->sample = dev->next_sample;
367 dev_dbg(dev->dev, "size=%u samples=%u msecs=%u sample rate=%lu\n",
368 src_len, samples, msecs,
369 samples * 1000UL / msecs);
554cbfbe
AP
370 }
371
554cbfbe
AP
372 return dst_len;
373}
374
977e444f
AP
375/*
376 * 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 */
06ce32cb 379static void msi2500_isoc_handler(struct urb *urb)
977e444f 380{
f7e5a655 381 struct msi2500_dev *dev = (struct msi2500_dev *)urb->context;
977e444f
AP
382 int i, flen, fstatus;
383 unsigned char *iso_buf = NULL;
06ce32cb 384 struct msi2500_frame_buf *fbuf;
977e444f 385
f7e5a655
AP
386 if (unlikely(urb->status == -ENOENT ||
387 urb->status == -ECONNRESET ||
388 urb->status == -ESHUTDOWN)) {
389 dev_dbg(dev->dev, "URB (%p) unlinked %ssynchronuously\n",
390 urb, urb->status == -ENOENT ? "" : "a");
977e444f
AP
391 return;
392 }
393
3d0c8fa3 394 if (unlikely(urb->status != 0)) {
f7e5a655 395 dev_dbg(dev->dev, "called with status %d\n", urb->status);
977e444f 396 /* Give up after a number of contiguous errors */
f7e5a655
AP
397 if (++dev->isoc_errors > MAX_ISOC_ERRORS)
398 dev_dbg(dev->dev, "Too many ISOC errors, bailing out\n");
977e444f
AP
399 goto handler_end;
400 } else {
401 /* Reset ISOC error counter. We did get here, after all. */
f7e5a655 402 dev->isoc_errors = 0;
977e444f
AP
403 }
404
405 /* Compact data */
406 for (i = 0; i < urb->number_of_packets; i++) {
554cbfbe
AP
407 void *ptr;
408
977e444f
AP
409 /* Check frame error */
410 fstatus = urb->iso_frame_desc[i].status;
3d0c8fa3 411 if (unlikely(fstatus)) {
f7e5a655
AP
412 dev_dbg_ratelimited(dev->dev,
413 "frame=%d/%d has error %d skipping\n",
414 i, urb->number_of_packets, fstatus);
3d0c8fa3 415 continue;
977e444f
AP
416 }
417
418 /* Check if that frame contains data */
419 flen = urb->iso_frame_desc[i].actual_length;
3d0c8fa3
AP
420 if (unlikely(flen == 0))
421 continue;
977e444f
AP
422
423 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
424
425 /* Get free framebuffer */
f7e5a655 426 fbuf = msi2500_get_next_fill_buf(dev);
3d0c8fa3 427 if (unlikely(fbuf == NULL)) {
f7e5a655
AP
428 dev->vb_full++;
429 dev_dbg_ratelimited(dev->dev,
430 "videobuf is full, %d packets dropped\n",
431 dev->vb_full);
3d0c8fa3 432 continue;
977e444f
AP
433 }
434
435 /* fill framebuffer */
2d700715 436 ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
f7e5a655 437 flen = msi2500_convert_stream(dev, ptr, iso_buf, flen);
2d700715
JS
438 vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, flen);
439 vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
977e444f
AP
440 }
441
442handler_end:
443 i = usb_submit_urb(urb, GFP_ATOMIC);
3d0c8fa3 444 if (unlikely(i != 0))
f7e5a655 445 dev_dbg(dev->dev, "Error (%d) re-submitting urb\n", i);
977e444f
AP
446}
447
f7e5a655 448static void msi2500_iso_stop(struct msi2500_dev *dev)
977e444f
AP
449{
450 int i;
c08de62f 451
f7e5a655 452 dev_dbg(dev->dev, "\n");
977e444f
AP
453
454 /* Unlinking ISOC buffers one by one */
455 for (i = 0; i < MAX_ISO_BUFS; i++) {
f7e5a655
AP
456 if (dev->urbs[i]) {
457 dev_dbg(dev->dev, "Unlinking URB %p\n", dev->urbs[i]);
458 usb_kill_urb(dev->urbs[i]);
977e444f
AP
459 }
460 }
461}
462
f7e5a655 463static void msi2500_iso_free(struct msi2500_dev *dev)
977e444f
AP
464{
465 int i;
c08de62f 466
f7e5a655 467 dev_dbg(dev->dev, "\n");
977e444f
AP
468
469 /* Freeing ISOC buffers one by one */
470 for (i = 0; i < MAX_ISO_BUFS; i++) {
f7e5a655
AP
471 if (dev->urbs[i]) {
472 dev_dbg(dev->dev, "Freeing URB\n");
473 if (dev->urbs[i]->transfer_buffer) {
474 usb_free_coherent(dev->udev,
475 dev->urbs[i]->transfer_buffer_length,
476 dev->urbs[i]->transfer_buffer,
477 dev->urbs[i]->transfer_dma);
977e444f 478 }
f7e5a655
AP
479 usb_free_urb(dev->urbs[i]);
480 dev->urbs[i] = NULL;
977e444f
AP
481 }
482 }
483}
484
485/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
f7e5a655 486static void msi2500_isoc_cleanup(struct msi2500_dev *dev)
977e444f 487{
f7e5a655 488 dev_dbg(dev->dev, "\n");
977e444f 489
f7e5a655
AP
490 msi2500_iso_stop(dev);
491 msi2500_iso_free(dev);
977e444f
AP
492}
493
494/* Both v4l2_lock and vb_queue_lock should be locked when calling this */
f7e5a655 495static int msi2500_isoc_init(struct msi2500_dev *dev)
977e444f 496{
977e444f
AP
497 struct urb *urb;
498 int i, j, ret;
c08de62f 499
f7e5a655 500 dev_dbg(dev->dev, "\n");
977e444f 501
f7e5a655 502 dev->isoc_errors = 0;
977e444f 503
f7e5a655 504 ret = usb_set_interface(dev->udev, 0, 1);
3d0c8fa3 505 if (ret)
977e444f
AP
506 return ret;
507
508 /* Allocate and init Isochronuous urbs */
509 for (i = 0; i < MAX_ISO_BUFS; i++) {
510 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
511 if (urb == NULL) {
f7e5a655
AP
512 dev_err(dev->dev, "Failed to allocate urb %d\n", i);
513 msi2500_isoc_cleanup(dev);
977e444f
AP
514 return -ENOMEM;
515 }
f7e5a655
AP
516 dev->urbs[i] = urb;
517 dev_dbg(dev->dev, "Allocated URB at 0x%p\n", urb);
977e444f
AP
518
519 urb->interval = 1;
f7e5a655
AP
520 urb->dev = dev->udev;
521 urb->pipe = usb_rcvisocpipe(dev->udev, 0x81);
977e444f 522 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
f7e5a655 523 urb->transfer_buffer = usb_alloc_coherent(dev->udev,
de3f2e2e 524 ISO_BUFFER_SIZE,
977e444f
AP
525 GFP_KERNEL, &urb->transfer_dma);
526 if (urb->transfer_buffer == NULL) {
f7e5a655
AP
527 dev_err(dev->dev,
528 "Failed to allocate urb buffer %d\n", i);
529 msi2500_isoc_cleanup(dev);
977e444f
AP
530 return -ENOMEM;
531 }
532 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
06ce32cb 533 urb->complete = msi2500_isoc_handler;
f7e5a655 534 urb->context = dev;
977e444f
AP
535 urb->start_frame = 0;
536 urb->number_of_packets = ISO_FRAMES_PER_DESC;
537 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
538 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
539 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
540 }
541 }
542
543 /* link */
544 for (i = 0; i < MAX_ISO_BUFS; i++) {
f7e5a655 545 ret = usb_submit_urb(dev->urbs[i], GFP_KERNEL);
977e444f 546 if (ret) {
f7e5a655
AP
547 dev_err(dev->dev,
548 "usb_submit_urb %d failed with error %d\n",
549 i, ret);
550 msi2500_isoc_cleanup(dev);
977e444f
AP
551 return ret;
552 }
f7e5a655 553 dev_dbg(dev->dev, "URB 0x%p submitted.\n", dev->urbs[i]);
977e444f
AP
554 }
555
556 /* All is done... */
557 return 0;
558}
559
560/* Must be called with vb_queue_lock hold */
f7e5a655 561static void msi2500_cleanup_queued_bufs(struct msi2500_dev *dev)
977e444f 562{
b63ab6b0 563 unsigned long flags;
c08de62f 564
f7e5a655 565 dev_dbg(dev->dev, "\n");
977e444f 566
f7e5a655
AP
567 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
568 while (!list_empty(&dev->queued_bufs)) {
06ce32cb 569 struct msi2500_frame_buf *buf;
977e444f 570
f7e5a655
AP
571 buf = list_entry(dev->queued_bufs.next,
572 struct msi2500_frame_buf, list);
977e444f 573 list_del(&buf->list);
2d700715 574 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
977e444f 575 }
f7e5a655 576 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
977e444f
AP
577}
578
579/* The user yanked out the cable... */
06ce32cb 580static void msi2500_disconnect(struct usb_interface *intf)
977e444f
AP
581{
582 struct v4l2_device *v = usb_get_intfdata(intf);
f7e5a655
AP
583 struct msi2500_dev *dev =
584 container_of(v, struct msi2500_dev, v4l2_dev);
c08de62f 585
f7e5a655 586 dev_dbg(dev->dev, "\n");
977e444f 587
f7e5a655
AP
588 mutex_lock(&dev->vb_queue_lock);
589 mutex_lock(&dev->v4l2_lock);
977e444f 590 /* No need to keep the urbs around after disconnection */
f7e5a655
AP
591 dev->udev = NULL;
592 v4l2_device_disconnect(&dev->v4l2_dev);
593 video_unregister_device(&dev->vdev);
594 spi_unregister_master(dev->master);
595 mutex_unlock(&dev->v4l2_lock);
596 mutex_unlock(&dev->vb_queue_lock);
597
598 v4l2_device_put(&dev->v4l2_dev);
977e444f
AP
599}
600
06ce32cb 601static int msi2500_querycap(struct file *file, void *fh,
f7e5a655 602 struct v4l2_capability *cap)
977e444f 603{
f7e5a655 604 struct msi2500_dev *dev = video_drvdata(file);
c08de62f 605
f7e5a655 606 dev_dbg(dev->dev, "\n");
977e444f
AP
607
608 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
f7e5a655
AP
609 strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
610 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
3d0c8fa3
AP
611 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
612 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
977e444f
AP
613 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
614 return 0;
615}
616
977e444f 617/* Videobuf2 operations */
06ce32cb 618static int msi2500_queue_setup(struct vb2_queue *vq,
f7e5a655
AP
619 const struct v4l2_format *fmt,
620 unsigned int *nbuffers,
621 unsigned int *nplanes, unsigned int sizes[],
622 void *alloc_ctxs[])
977e444f 623{
f7e5a655 624 struct msi2500_dev *dev = vb2_get_drv_priv(vq);
c08de62f 625
f7e5a655 626 dev_dbg(dev->dev, "nbuffers=%d\n", *nbuffers);
977e444f
AP
627
628 /* Absolute min and max number of buffers available for mmap() */
d287e4e3 629 *nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32);
977e444f 630 *nplanes = 1;
f7e5a655
AP
631 sizes[0] = PAGE_ALIGN(dev->buffersize);
632 dev_dbg(dev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
977e444f
AP
633 return 0;
634}
635
06ce32cb 636static void msi2500_buf_queue(struct vb2_buffer *vb)
977e444f 637{
2d700715 638 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
f7e5a655 639 struct msi2500_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
2d700715 640 struct msi2500_frame_buf *buf = container_of(vbuf,
f7e5a655
AP
641 struct msi2500_frame_buf,
642 vb);
b63ab6b0 643 unsigned long flags;
977e444f
AP
644
645 /* Check the device has not disconnected between prep and queuing */
f7e5a655 646 if (unlikely(!dev->udev)) {
2d700715 647 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
977e444f
AP
648 return;
649 }
650
f7e5a655
AP
651 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
652 list_add_tail(&buf->list, &dev->queued_bufs);
653 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
977e444f
AP
654}
655
656#define CMD_WREG 0x41
657#define CMD_START_STREAMING 0x43
658#define CMD_STOP_STREAMING 0x45
f7e5a655 659#define CMD_READ_UNKNOWN 0x48
977e444f 660
100b7931 661#define msi2500_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
c08de62f
AP
662 char *_direction; \
663 if (_t & USB_DIR_IN) \
664 _direction = "<<<"; \
977e444f 665 else \
c08de62f 666 _direction = ">>>"; \
100b7931
AP
667 dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
668 _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \
669 _l & 0xff, _l >> 8, _direction, _l, _b); \
977e444f
AP
670}
671
f7e5a655 672static int msi2500_ctrl_msg(struct msi2500_dev *dev, u8 cmd, u32 data)
977e444f
AP
673{
674 int ret;
675 u8 request = cmd;
676 u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR;
677 u16 value = (data >> 0) & 0xffff;
678 u16 index = (data >> 16) & 0xffff;
679
f7e5a655
AP
680 msi2500_dbg_usb_control_msg(dev->dev, request, requesttype,
681 value, index, NULL, 0);
682 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), request,
683 requesttype, value, index, NULL, 0, 2000);
977e444f 684 if (ret)
f7e5a655
AP
685 dev_err(dev->dev, "failed %d, cmd %02x, data %04x\n",
686 ret, cmd, data);
977e444f
AP
687
688 return ret;
100b7931 689}
977e444f 690
f7e5a655 691static int msi2500_set_usb_adc(struct msi2500_dev *dev)
977e444f 692{
faf22b13
AP
693 int ret;
694 unsigned int f_vco, f_sr, div_n, k, k_cw, div_out;
554cbfbe 695 u32 reg3, reg4, reg7;
2e68f841
AP
696 struct v4l2_ctrl *bandwidth_auto;
697 struct v4l2_ctrl *bandwidth;
554cbfbe 698
f7e5a655 699 f_sr = dev->f_adc;
554cbfbe 700
2e68f841 701 /* set tuner, subdev, filters according to sampling rate */
f7e5a655 702 bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
c08de62f 703 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
2e68f841 704 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
f7e5a655 705 bandwidth = v4l2_ctrl_find(&dev->hdl,
c08de62f 706 V4L2_CID_RF_TUNER_BANDWIDTH);
f7e5a655 707 v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
2e68f841 708 }
554cbfbe
AP
709
710 /* select stream format */
f7e5a655 711 switch (dev->pixelformat) {
3d0c8fa3 712 case V4L2_SDR_FMT_CU8:
8591f708 713 reg7 = 0x000c9407; /* 504 */
3d0c8fa3
AP
714 break;
715 case V4L2_SDR_FMT_CU16LE:
8591f708 716 reg7 = 0x00009407; /* 252 */
3d0c8fa3 717 break;
3912eb6d 718 case V4L2_SDR_FMT_CS8:
8591f708 719 reg7 = 0x000c9407; /* 504 */
3d0c8fa3 720 break;
c08de62f 721 case MSI2500_PIX_FMT_SDR_MSI2500_384:
8591f708 722 reg7 = 0x0000a507; /* 384 */
3d0c8fa3 723 break;
c08de62f 724 case MSI2500_PIX_FMT_SDR_S12:
8591f708 725 reg7 = 0x00008507; /* 336 */
3d0c8fa3 726 break;
3912eb6d 727 case V4L2_SDR_FMT_CS14LE:
8591f708 728 reg7 = 0x00009407; /* 252 */
3d0c8fa3
AP
729 break;
730 default:
8591f708 731 reg7 = 0x000c9407; /* 504 */
3d0c8fa3 732 break;
554cbfbe
AP
733 }
734
faf22b13
AP
735 /*
736 * Fractional-N synthesizer
737 *
738 * +----------------------------------------+
739 * v |
740 * Fref +----+ +-------+ +-----+ +------+ +---+
741 * ------> | PD | --> | VCO | --> | /2 | ------> | /N.F | <-- | K |
742 * +----+ +-------+ +-----+ +------+ +---+
743 * |
744 * |
745 * v
746 * +-------+ +-----+ Fout
747 * | /Rout | --> | /12 | ------>
748 * +-------+ +-----+
749 */
977e444f 750 /*
b1520857 751 * Synthesizer config is just a educated guess...
977e444f 752 *
977e444f 753 * [7:0] 0x03, register address
3d0c8fa3
AP
754 * [8] 1, power control
755 * [9] ?, power control
977e444f
AP
756 * [12:10] output divider
757 * [13] 0 ?
758 * [14] 0 ?
22ca680e 759 * [15] fractional MSB, bit 20
977e444f
AP
760 * [16:19] N
761 * [23:20] ?
762 * [24:31] 0x01
763 *
764 * output divider
765 * val div
766 * 0 - (invalid)
b1520857
AP
767 * 1 4
768 * 2 6
769 * 3 8
770 * 4 10
771 * 5 12
772 * 6 14
773 * 7 16
774 *
775 * VCO 202000000 - 720000000++
977e444f 776 */
faf22b13
AP
777
778 #define F_REF 24000000
779 #define DIV_PRE_N 2
780 #define DIV_LO_OUT 12
c5a431d0 781 reg3 = 0x01000303;
22ca680e 782 reg4 = 0x00000004;
977e444f 783
faf22b13 784 /* XXX: Filters? AGC? VCO band? */
c5a431d0
AP
785 if (f_sr < 6000000)
786 reg3 |= 0x1 << 20;
787 else if (f_sr < 7000000)
788 reg3 |= 0x5 << 20;
789 else if (f_sr < 8500000)
790 reg3 |= 0x9 << 20;
791 else
792 reg3 |= 0xd << 20;
793
faf22b13
AP
794 for (div_out = 4; div_out < 16; div_out += 2) {
795 f_vco = f_sr * div_out * DIV_LO_OUT;
f7e5a655 796 dev_dbg(dev->dev, "div_out=%u f_vco=%u\n", div_out, f_vco);
b1520857 797 if (f_vco >= 202000000)
977e444f
AP
798 break;
799 }
800
faf22b13
AP
801 /* Calculate PLL integer and fractional control word. */
802 div_n = div_u64_rem(f_vco, DIV_PRE_N * F_REF, &k);
803 k_cw = div_u64((u64) k * 0x200000, DIV_PRE_N * F_REF);
977e444f 804
b1520857 805 reg3 |= div_n << 16;
faf22b13
AP
806 reg3 |= (div_out / 2 - 1) << 10;
807 reg3 |= ((k_cw >> 20) & 0x000001) << 15; /* [20] */
808 reg4 |= ((k_cw >> 0) & 0x0fffff) << 8; /* [19:0] */
977e444f 809
f7e5a655 810 dev_dbg(dev->dev,
faf22b13
AP
811 "f_sr=%u f_vco=%u div_n=%u k=%u div_out=%u reg3=%08x reg4=%08x\n",
812 f_sr, f_vco, div_n, k, div_out, reg3, reg4);
977e444f 813
f7e5a655 814 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00608008);
977e444f
AP
815 if (ret)
816 goto err;
817
f7e5a655 818 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00000c05);
977e444f
AP
819 if (ret)
820 goto err;
821
f7e5a655 822 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00020000);
977e444f
AP
823 if (ret)
824 goto err;
825
f7e5a655 826 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00480102);
977e444f
AP
827 if (ret)
828 goto err;
829
f7e5a655 830 ret = msi2500_ctrl_msg(dev, CMD_WREG, 0x00f38008);
977e444f
AP
831 if (ret)
832 goto err;
833
f7e5a655 834 ret = msi2500_ctrl_msg(dev, CMD_WREG, reg7);
977e444f
AP
835 if (ret)
836 goto err;
837
f7e5a655 838 ret = msi2500_ctrl_msg(dev, CMD_WREG, reg4);
977e444f
AP
839 if (ret)
840 goto err;
841
f7e5a655 842 ret = msi2500_ctrl_msg(dev, CMD_WREG, reg3);
977e444f
AP
843 if (ret)
844 goto err;
845err:
846 return ret;
100b7931 847}
977e444f 848
06ce32cb 849static int msi2500_start_streaming(struct vb2_queue *vq, unsigned int count)
977e444f 850{
f7e5a655 851 struct msi2500_dev *dev = vb2_get_drv_priv(vq);
977e444f 852 int ret;
c08de62f 853
f7e5a655 854 dev_dbg(dev->dev, "\n");
977e444f 855
f7e5a655 856 if (!dev->udev)
977e444f
AP
857 return -ENODEV;
858
f7e5a655 859 if (mutex_lock_interruptible(&dev->v4l2_lock))
977e444f
AP
860 return -ERESTARTSYS;
861
2e68f841 862 /* wake-up tuner */
f7e5a655 863 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
2e68f841 864
f7e5a655 865 ret = msi2500_set_usb_adc(dev);
977e444f 866
f7e5a655 867 ret = msi2500_isoc_init(dev);
977e444f 868 if (ret)
f7e5a655 869 msi2500_cleanup_queued_bufs(dev);
977e444f 870
f7e5a655 871 ret = msi2500_ctrl_msg(dev, CMD_START_STREAMING, 0);
977e444f 872
f7e5a655 873 mutex_unlock(&dev->v4l2_lock);
977e444f
AP
874
875 return ret;
876}
877
06ce32cb 878static void msi2500_stop_streaming(struct vb2_queue *vq)
977e444f 879{
f7e5a655 880 struct msi2500_dev *dev = vb2_get_drv_priv(vq);
e37559b2 881
f7e5a655 882 dev_dbg(dev->dev, "\n");
977e444f 883
f7e5a655 884 mutex_lock(&dev->v4l2_lock);
977e444f 885
f7e5a655
AP
886 if (dev->udev)
887 msi2500_isoc_cleanup(dev);
977e444f 888
f7e5a655 889 msi2500_cleanup_queued_bufs(dev);
d0fadf40
AP
890
891 /* according to tests, at least 700us delay is required */
892 msleep(20);
f7e5a655 893 if (!msi2500_ctrl_msg(dev, CMD_STOP_STREAMING, 0)) {
e37559b2 894 /* sleep USB IF / ADC */
f7e5a655 895 msi2500_ctrl_msg(dev, CMD_WREG, 0x01000003);
e37559b2 896 }
3d0c8fa3
AP
897
898 /* sleep tuner */
f7e5a655 899 v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
3d0c8fa3 900
f7e5a655 901 mutex_unlock(&dev->v4l2_lock);
977e444f
AP
902}
903
06ce32cb
AP
904static struct vb2_ops msi2500_vb2_ops = {
905 .queue_setup = msi2500_queue_setup,
906 .buf_queue = msi2500_buf_queue,
907 .start_streaming = msi2500_start_streaming,
908 .stop_streaming = msi2500_stop_streaming,
977e444f
AP
909 .wait_prepare = vb2_ops_wait_prepare,
910 .wait_finish = vb2_ops_wait_finish,
911};
912
06ce32cb 913static int msi2500_enum_fmt_sdr_cap(struct file *file, void *priv,
f7e5a655 914 struct v4l2_fmtdesc *f)
977e444f 915{
f7e5a655 916 struct msi2500_dev *dev = video_drvdata(file);
c08de62f 917
f7e5a655 918 dev_dbg(dev->dev, "index=%d\n", f->index);
3d0c8fa3 919
f7e5a655 920 if (f->index >= dev->num_formats)
977e444f
AP
921 return -EINVAL;
922
3d0c8fa3
AP
923 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
924 f->pixelformat = formats[f->index].pixelformat;
925
926 return 0;
927}
928
06ce32cb 929static int msi2500_g_fmt_sdr_cap(struct file *file, void *priv,
f7e5a655 930 struct v4l2_format *f)
3d0c8fa3 931{
f7e5a655 932 struct msi2500_dev *dev = video_drvdata(file);
c08de62f 933
f7e5a655
AP
934 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
935 (char *)&dev->pixelformat);
3d0c8fa3 936
f7e5a655
AP
937 f->fmt.sdr.pixelformat = dev->pixelformat;
938 f->fmt.sdr.buffersize = dev->buffersize;
a54e0fee 939 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
977e444f
AP
940
941 return 0;
942}
943
06ce32cb 944static int msi2500_s_fmt_sdr_cap(struct file *file, void *priv,
f7e5a655 945 struct v4l2_format *f)
977e444f 946{
f7e5a655
AP
947 struct msi2500_dev *dev = video_drvdata(file);
948 struct vb2_queue *q = &dev->vb_queue;
3d0c8fa3 949 int i;
c08de62f 950
f7e5a655
AP
951 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
952 (char *)&f->fmt.sdr.pixelformat);
3d0c8fa3
AP
953
954 if (vb2_is_busy(q))
955 return -EBUSY;
956
c350912c 957 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
f7e5a655 958 for (i = 0; i < dev->num_formats; i++) {
3d0c8fa3 959 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
f7e5a655
AP
960 dev->pixelformat = formats[i].pixelformat;
961 dev->buffersize = formats[i].buffersize;
a54e0fee 962 f->fmt.sdr.buffersize = formats[i].buffersize;
3d0c8fa3
AP
963 return 0;
964 }
965 }
966
f7e5a655
AP
967 dev->pixelformat = formats[0].pixelformat;
968 dev->buffersize = formats[0].buffersize;
a54e0fee
AP
969 f->fmt.sdr.pixelformat = formats[0].pixelformat;
970 f->fmt.sdr.buffersize = formats[0].buffersize;
977e444f
AP
971
972 return 0;
973}
974
06ce32cb 975static int msi2500_try_fmt_sdr_cap(struct file *file, void *priv,
f7e5a655 976 struct v4l2_format *f)
977e444f 977{
f7e5a655 978 struct msi2500_dev *dev = video_drvdata(file);
3d0c8fa3 979 int i;
c08de62f 980
f7e5a655
AP
981 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
982 (char *)&f->fmt.sdr.pixelformat);
3d0c8fa3 983
c350912c 984 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
f7e5a655 985 for (i = 0; i < dev->num_formats; i++) {
a54e0fee
AP
986 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
987 f->fmt.sdr.buffersize = formats[i].buffersize;
3d0c8fa3 988 return 0;
a54e0fee 989 }
3d0c8fa3
AP
990 }
991
992 f->fmt.sdr.pixelformat = formats[0].pixelformat;
a54e0fee 993 f->fmt.sdr.buffersize = formats[0].buffersize;
3d0c8fa3
AP
994
995 return 0;
977e444f
AP
996}
997
06ce32cb 998static int msi2500_s_tuner(struct file *file, void *priv,
f7e5a655 999 const struct v4l2_tuner *v)
977e444f 1000{
f7e5a655 1001 struct msi2500_dev *dev = video_drvdata(file);
2e68f841 1002 int ret;
c08de62f 1003
f7e5a655 1004 dev_dbg(dev->dev, "index=%d\n", v->index);
977e444f 1005
2e68f841
AP
1006 if (v->index == 0)
1007 ret = 0;
1008 else if (v->index == 1)
f7e5a655 1009 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
2e68f841
AP
1010 else
1011 ret = -EINVAL;
1012
1013 return ret;
977e444f
AP
1014}
1015
06ce32cb 1016static int msi2500_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
977e444f 1017{
f7e5a655 1018 struct msi2500_dev *dev = video_drvdata(file);
2e68f841 1019 int ret;
c08de62f 1020
f7e5a655 1021 dev_dbg(dev->dev, "index=%d\n", v->index);
977e444f 1022
3d0c8fa3 1023 if (v->index == 0) {
2e68f841 1024 strlcpy(v->name, "Mirics MSi2500", sizeof(v->name));
3d0c8fa3
AP
1025 v->type = V4L2_TUNER_ADC;
1026 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1027 v->rangelow = 1200000;
1028 v->rangehigh = 15000000;
2e68f841 1029 ret = 0;
3d0c8fa3 1030 } else if (v->index == 1) {
f7e5a655 1031 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
3d0c8fa3 1032 } else {
2e68f841 1033 ret = -EINVAL;
3d0c8fa3 1034 }
977e444f 1035
2e68f841 1036 return ret;
977e444f 1037}
977e444f 1038
06ce32cb 1039static int msi2500_g_frequency(struct file *file, void *priv,
f7e5a655 1040 struct v4l2_frequency *f)
3d0c8fa3 1041{
f7e5a655 1042 struct msi2500_dev *dev = video_drvdata(file);
3d0c8fa3 1043 int ret = 0;
c08de62f 1044
f7e5a655 1045 dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
3d0c8fa3 1046
2e68f841 1047 if (f->tuner == 0) {
f7e5a655 1048 f->frequency = dev->f_adc;
2e68f841
AP
1049 ret = 0;
1050 } else if (f->tuner == 1) {
c350912c 1051 f->type = V4L2_TUNER_RF;
f7e5a655 1052 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
2e68f841
AP
1053 } else {
1054 ret = -EINVAL;
1055 }
3d0c8fa3
AP
1056
1057 return ret;
977e444f
AP
1058}
1059
06ce32cb 1060static int msi2500_s_frequency(struct file *file, void *priv,
f7e5a655 1061 const struct v4l2_frequency *f)
977e444f 1062{
f7e5a655 1063 struct msi2500_dev *dev = video_drvdata(file);
2e68f841 1064 int ret;
c08de62f 1065
f7e5a655
AP
1066 dev_dbg(dev->dev, "tuner=%d type=%d frequency=%u\n",
1067 f->tuner, f->type, f->frequency);
3d0c8fa3
AP
1068
1069 if (f->tuner == 0) {
f7e5a655
AP
1070 dev->f_adc = clamp_t(unsigned int, f->frequency,
1071 bands[0].rangelow,
1072 bands[0].rangehigh);
1073 dev_dbg(dev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
1074 ret = msi2500_set_usb_adc(dev);
3d0c8fa3 1075 } else if (f->tuner == 1) {
f7e5a655 1076 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
3d0c8fa3 1077 } else {
2e68f841 1078 ret = -EINVAL;
3d0c8fa3 1079 }
977e444f 1080
3d0c8fa3
AP
1081 return ret;
1082}
1083
06ce32cb 1084static int msi2500_enum_freq_bands(struct file *file, void *priv,
f7e5a655 1085 struct v4l2_frequency_band *band)
3d0c8fa3 1086{
f7e5a655 1087 struct msi2500_dev *dev = video_drvdata(file);
2e68f841 1088 int ret;
c08de62f 1089
f7e5a655
AP
1090 dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n",
1091 band->tuner, band->type, band->index);
3d0c8fa3
AP
1092
1093 if (band->tuner == 0) {
2e68f841
AP
1094 if (band->index >= ARRAY_SIZE(bands)) {
1095 ret = -EINVAL;
1096 } else {
1097 *band = bands[band->index];
1098 ret = 0;
1099 }
3d0c8fa3 1100 } else if (band->tuner == 1) {
f7e5a655
AP
1101 ret = v4l2_subdev_call(dev->v4l2_subdev, tuner,
1102 enum_freq_bands, band);
3d0c8fa3 1103 } else {
2e68f841 1104 ret = -EINVAL;
3d0c8fa3
AP
1105 }
1106
2e68f841 1107 return ret;
977e444f
AP
1108}
1109
06ce32cb
AP
1110static const struct v4l2_ioctl_ops msi2500_ioctl_ops = {
1111 .vidioc_querycap = msi2500_querycap,
977e444f 1112
06ce32cb
AP
1113 .vidioc_enum_fmt_sdr_cap = msi2500_enum_fmt_sdr_cap,
1114 .vidioc_g_fmt_sdr_cap = msi2500_g_fmt_sdr_cap,
1115 .vidioc_s_fmt_sdr_cap = msi2500_s_fmt_sdr_cap,
1116 .vidioc_try_fmt_sdr_cap = msi2500_try_fmt_sdr_cap,
977e444f
AP
1117
1118 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1119 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1120 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1121 .vidioc_querybuf = vb2_ioctl_querybuf,
1122 .vidioc_qbuf = vb2_ioctl_qbuf,
1123 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1124
1125 .vidioc_streamon = vb2_ioctl_streamon,
1126 .vidioc_streamoff = vb2_ioctl_streamoff,
1127
06ce32cb
AP
1128 .vidioc_g_tuner = msi2500_g_tuner,
1129 .vidioc_s_tuner = msi2500_s_tuner,
3d0c8fa3 1130
06ce32cb
AP
1131 .vidioc_g_frequency = msi2500_g_frequency,
1132 .vidioc_s_frequency = msi2500_s_frequency,
1133 .vidioc_enum_freq_bands = msi2500_enum_freq_bands,
977e444f
AP
1134
1135 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1136 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1137 .vidioc_log_status = v4l2_ctrl_log_status,
1138};
1139
06ce32cb 1140static const struct v4l2_file_operations msi2500_fops = {
977e444f
AP
1141 .owner = THIS_MODULE,
1142 .open = v4l2_fh_open,
1143 .release = vb2_fop_release,
1144 .read = vb2_fop_read,
1145 .poll = vb2_fop_poll,
1146 .mmap = vb2_fop_mmap,
1147 .unlocked_ioctl = video_ioctl2,
1148};
1149
06ce32cb 1150static struct video_device msi2500_template = {
977e444f
AP
1151 .name = "Mirics MSi3101 SDR Dongle",
1152 .release = video_device_release_empty,
06ce32cb
AP
1153 .fops = &msi2500_fops,
1154 .ioctl_ops = &msi2500_ioctl_ops,
977e444f
AP
1155};
1156
06ce32cb 1157static void msi2500_video_release(struct v4l2_device *v)
977e444f 1158{
f7e5a655 1159 struct msi2500_dev *dev = container_of(v, struct msi2500_dev, v4l2_dev);
977e444f 1160
f7e5a655
AP
1161 v4l2_ctrl_handler_free(&dev->hdl);
1162 v4l2_device_unregister(&dev->v4l2_dev);
1163 kfree(dev);
977e444f
AP
1164}
1165
06ce32cb 1166static int msi2500_transfer_one_message(struct spi_master *master,
f7e5a655 1167 struct spi_message *m)
2e68f841 1168{
f7e5a655 1169 struct msi2500_dev *dev = spi_master_get_devdata(master);
2e68f841
AP
1170 struct spi_transfer *t;
1171 int ret = 0;
1172 u32 data;
1173
1174 list_for_each_entry(t, &m->transfers, transfer_list) {
f7e5a655 1175 dev_dbg(dev->dev, "msg=%*ph\n", t->len, t->tx_buf);
2e68f841
AP
1176 data = 0x09; /* reg 9 is SPI adapter */
1177 data |= ((u8 *)t->tx_buf)[0] << 8;
1178 data |= ((u8 *)t->tx_buf)[1] << 16;
1179 data |= ((u8 *)t->tx_buf)[2] << 24;
f7e5a655 1180 ret = msi2500_ctrl_msg(dev, CMD_WREG, data);
2e68f841
AP
1181 }
1182
1183 m->status = ret;
1184 spi_finalize_current_message(master);
1185 return ret;
1186}
1187
06ce32cb 1188static int msi2500_probe(struct usb_interface *intf,
f7e5a655 1189 const struct usb_device_id *id)
977e444f 1190{
f7e5a655 1191 struct msi2500_dev *dev;
2e68f841
AP
1192 struct v4l2_subdev *sd;
1193 struct spi_master *master;
977e444f 1194 int ret;
2e68f841
AP
1195 static struct spi_board_info board_info = {
1196 .modalias = "msi001",
1197 .bus_num = 0,
1198 .chip_select = 0,
1199 .max_speed_hz = 12000000,
977e444f
AP
1200 };
1201
f7e5a655
AP
1202 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1203 if (!dev) {
1204 ret = -ENOMEM;
1205 goto err;
977e444f
AP
1206 }
1207
f7e5a655
AP
1208 mutex_init(&dev->v4l2_lock);
1209 mutex_init(&dev->vb_queue_lock);
1210 spin_lock_init(&dev->queued_bufs_lock);
1211 INIT_LIST_HEAD(&dev->queued_bufs);
1212 dev->dev = &intf->dev;
1213 dev->udev = interface_to_usbdev(intf);
1214 dev->f_adc = bands[0].rangelow;
1215 dev->pixelformat = formats[0].pixelformat;
1216 dev->buffersize = formats[0].buffersize;
1217 dev->num_formats = NUM_FORMATS;
ad7b8c02 1218 if (!msi2500_emulated_fmt)
f7e5a655 1219 dev->num_formats -= 2;
977e444f
AP
1220
1221 /* Init videobuf2 queue structure */
f7e5a655
AP
1222 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1223 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1224 dev->vb_queue.drv_priv = dev;
1225 dev->vb_queue.buf_struct_size = sizeof(struct msi2500_frame_buf);
1226 dev->vb_queue.ops = &msi2500_vb2_ops;
1227 dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1228 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1229 ret = vb2_queue_init(&dev->vb_queue);
3d0c8fa3 1230 if (ret) {
f7e5a655 1231 dev_err(dev->dev, "Could not initialize vb2 queue\n");
977e444f
AP
1232 goto err_free_mem;
1233 }
1234
1235 /* Init video_device structure */
f7e5a655
AP
1236 dev->vdev = msi2500_template;
1237 dev->vdev.queue = &dev->vb_queue;
1238 dev->vdev.queue->lock = &dev->vb_queue_lock;
1239 video_set_drvdata(&dev->vdev, dev);
977e444f 1240
977e444f 1241 /* Register the v4l2_device structure */
f7e5a655
AP
1242 dev->v4l2_dev.release = msi2500_video_release;
1243 ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev);
977e444f 1244 if (ret) {
f7e5a655 1245 dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret);
2e68f841
AP
1246 goto err_free_mem;
1247 }
1248
1249 /* SPI master adapter */
f7e5a655 1250 master = spi_alloc_master(dev->dev, 0);
2e68f841
AP
1251 if (master == NULL) {
1252 ret = -ENOMEM;
1253 goto err_unregister_v4l2_dev;
1254 }
1255
f7e5a655 1256 dev->master = master;
2e68f841
AP
1257 master->bus_num = 0;
1258 master->num_chipselect = 1;
06ce32cb 1259 master->transfer_one_message = msi2500_transfer_one_message;
f7e5a655 1260 spi_master_set_devdata(master, dev);
2e68f841
AP
1261 ret = spi_register_master(master);
1262 if (ret) {
1263 spi_master_put(master);
1264 goto err_unregister_v4l2_dev;
1265 }
1266
1267 /* load v4l2 subdevice */
f7e5a655
AP
1268 sd = v4l2_spi_new_subdev(&dev->v4l2_dev, master, &board_info);
1269 dev->v4l2_subdev = sd;
2e68f841 1270 if (sd == NULL) {
f7e5a655 1271 dev_err(dev->dev, "cannot get v4l2 subdevice\n");
2e68f841
AP
1272 ret = -ENODEV;
1273 goto err_unregister_master;
1274 }
1275
1276 /* Register controls */
f7e5a655
AP
1277 v4l2_ctrl_handler_init(&dev->hdl, 0);
1278 if (dev->hdl.error) {
1279 ret = dev->hdl.error;
1280 dev_err(dev->dev, "Could not initialize controls\n");
977e444f
AP
1281 goto err_free_controls;
1282 }
1283
2e68f841 1284 /* currently all controls are from subdev */
f7e5a655 1285 v4l2_ctrl_add_handler(&dev->hdl, sd->ctrl_handler, NULL);
2e68f841 1286
f7e5a655
AP
1287 dev->v4l2_dev.ctrl_handler = &dev->hdl;
1288 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1289 dev->vdev.lock = &dev->v4l2_lock;
977e444f 1290
f7e5a655 1291 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
3d0c8fa3 1292 if (ret) {
f7e5a655
AP
1293 dev_err(dev->dev,
1294 "Failed to register as video device (%d)\n", ret);
977e444f
AP
1295 goto err_unregister_v4l2_dev;
1296 }
f7e5a655
AP
1297 dev_info(dev->dev, "Registered as %s\n",
1298 video_device_node_name(&dev->vdev));
1299 dev_notice(dev->dev,
1300 "SDR API is still slightly experimental and functionality changes may follow\n");
977e444f 1301 return 0;
977e444f 1302err_free_controls:
f7e5a655 1303 v4l2_ctrl_handler_free(&dev->hdl);
2e68f841 1304err_unregister_master:
f7e5a655 1305 spi_unregister_master(dev->master);
977e444f 1306err_unregister_v4l2_dev:
f7e5a655 1307 v4l2_device_unregister(&dev->v4l2_dev);
977e444f 1308err_free_mem:
f7e5a655
AP
1309 kfree(dev);
1310err:
977e444f
AP
1311 return ret;
1312}
1313
1314/* USB device ID list */
06ce32cb 1315static struct usb_device_id msi2500_id_table[] = {
f7e5a655
AP
1316 {USB_DEVICE(0x1df7, 0x2500)}, /* Mirics MSi3101 SDR Dongle */
1317 {USB_DEVICE(0x2040, 0xd300)}, /* Hauppauge WinTV 133559 LF */
1318 {}
977e444f 1319};
06ce32cb 1320MODULE_DEVICE_TABLE(usb, msi2500_id_table);
977e444f
AP
1321
1322/* USB subsystem interface */
06ce32cb 1323static struct usb_driver msi2500_driver = {
977e444f 1324 .name = KBUILD_MODNAME,
06ce32cb
AP
1325 .probe = msi2500_probe,
1326 .disconnect = msi2500_disconnect,
1327 .id_table = msi2500_id_table,
977e444f
AP
1328};
1329
06ce32cb 1330module_usb_driver(msi2500_driver);
977e444f
AP
1331
1332MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1333MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1334MODULE_LICENSE("GPL");