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