1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Mirics MSi2500 driver
4 * Mirics MSi3101 SDR Dongle driver
6 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
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>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <asm/div64.h>
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>
22 #include <media/videobuf2-v4l2.h>
23 #include <media/videobuf2-vmalloc.h>
24 #include <linux/spi/spi.h>
26 static bool msi2500_emulated_fmt
;
27 module_param_named(emulated_formats
, msi2500_emulated_fmt
, bool, 0644);
28 MODULE_PARM_DESC(emulated_formats
, "enable emulated formats (disappears in future)");
35 * bEndpointAddress 0x81 EP 1 IN
37 * Transfer Type Isochronous
38 * wMaxPacketSize 0x1400 3x 1024 bytes
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)
45 #define MAX_ISOC_ERRORS 20
48 * TODO: These formats should be moved to V4L2 API. Formats are currently
49 * disabled from formats[] table, not visible to userspace.
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')
56 static const struct v4l2_frequency_band bands
[] = {
59 .type
= V4L2_TUNER_ADC
,
61 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
63 .rangehigh
= 15000000,
68 struct msi2500_format
{
73 /* format descriptions for capture and preview */
74 static struct msi2500_format formats
[] = {
76 .pixelformat
= V4L2_SDR_FMT_CS8
,
77 .buffersize
= 3 * 1008,
80 .pixelformat
= MSI2500_PIX_FMT_SDR_MSI2500_384
,
82 .pixelformat
= MSI2500_PIX_FMT_SDR_S12
,
85 .pixelformat
= V4L2_SDR_FMT_CS14LE
,
86 .buffersize
= 3 * 1008,
88 .pixelformat
= V4L2_SDR_FMT_CU8
,
89 .buffersize
= 3 * 1008,
91 .pixelformat
= V4L2_SDR_FMT_CU16LE
,
92 .buffersize
= 3 * 1008,
96 static const unsigned int NUM_FORMATS
= ARRAY_SIZE(formats
);
98 /* intermediate buffers with raw data from the USB device */
99 struct msi2500_frame_buf
{
100 /* common v4l buffer stuff -- must be first */
101 struct vb2_v4l2_buffer vb
;
102 struct list_head list
;
107 struct video_device vdev
;
108 struct v4l2_device v4l2_dev
;
109 struct v4l2_subdev
*v4l2_subdev
;
110 struct spi_master
*master
;
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 */
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 */
121 /* Pointer to our usb_device, will be NULL after unplug */
122 struct usb_device
*udev
; /* Both mutexes most be hold when setting! */
127 unsigned int num_formats
;
129 unsigned int isoc_errors
; /* number of contiguous ISOC errors */
130 unsigned int vb_full
; /* vb is full and packets dropped */
132 struct urb
*urbs
[MAX_ISO_BUFS
];
135 struct v4l2_ctrl_handler hdl
;
137 u32 next_sample
; /* for track lost packets */
138 u32 sample
; /* for sample rate calc */
139 unsigned long jiffies_next
;
142 /* Private functions */
143 static struct msi2500_frame_buf
*msi2500_get_next_fill_buf(
144 struct msi2500_dev
*dev
)
147 struct msi2500_frame_buf
*buf
= NULL
;
149 spin_lock_irqsave(&dev
->queued_bufs_lock
, flags
);
150 if (list_empty(&dev
->queued_bufs
))
153 buf
= list_entry(dev
->queued_bufs
.next
, struct msi2500_frame_buf
, list
);
154 list_del(&buf
->list
);
156 spin_unlock_irqrestore(&dev
->queued_bufs_lock
, flags
);
161 * +===========================================================================
162 * | 00-1023 | USB packet type '504'
163 * +===========================================================================
164 * | 00- 03 | sequence number of first sample in that USB packet
165 * +---------------------------------------------------------------------------
167 * +---------------------------------------------------------------------------
168 * | 16-1023 | samples
169 * +---------------------------------------------------------------------------
170 * signed 8-bit sample
171 * 504 * 2 = 1008 samples
174 * +===========================================================================
175 * | 00-1023 | USB packet type '384'
176 * +===========================================================================
177 * | 00- 03 | sequence number of first sample in that USB packet
178 * +---------------------------------------------------------------------------
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 * +---------------------------------------------------------------------------
208 * Bytes 4 - 7 could have some meaning?
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.
215 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
218 * +===========================================================================
219 * | 00-1023 | USB packet type '336'
220 * +===========================================================================
221 * | 00- 03 | sequence number of first sample in that USB packet
222 * +---------------------------------------------------------------------------
224 * +---------------------------------------------------------------------------
225 * | 16-1023 | samples
226 * +---------------------------------------------------------------------------
227 * signed 12-bit sample
230 * +===========================================================================
231 * | 00-1023 | USB packet type '252'
232 * +===========================================================================
233 * | 00- 03 | sequence number of first sample in that USB packet
234 * +---------------------------------------------------------------------------
236 * +---------------------------------------------------------------------------
237 * | 16-1023 | samples
238 * +---------------------------------------------------------------------------
239 * signed 14-bit sample
242 static int msi2500_convert_stream(struct msi2500_dev
*dev
, u8
*dst
, u8
*src
,
243 unsigned int src_len
)
245 unsigned int i
, j
, transactions
, dst_len
= 0;
248 /* There could be 1-3 1024 byte transactions per packet */
249 transactions
= src_len
/ 1024;
251 for (i
= 0; i
< transactions
; i
++) {
252 sample
[i
] = src
[3] << 24 | src
[2] << 16 | src
[1] << 8 |
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
,
263 * Dump all unknown 'garbage' data - maybe we will discover
264 * someday if there is something rational...
266 dev_dbg_ratelimited(dev
->dev
, "%*ph\n", 12, &src
[4]);
268 src
+= 16; /* skip header */
270 switch (dev
->pixelformat
) {
271 case V4L2_SDR_FMT_CU8
: /* 504 x IQ samples */
273 s8
*s8src
= (s8
*)src
;
274 u8
*u8dst
= (u8
*)dst
;
276 for (j
= 0; j
< 1008; j
++)
277 *u8dst
++ = *s8src
++ + 128;
282 dev
->next_sample
= sample
[i
] + 504;
285 case V4L2_SDR_FMT_CU16LE
: /* 252 x IQ samples */
287 s16
*s16src
= (s16
*)src
;
288 u16
*u16dst
= (u16
*)dst
;
289 struct {signed int x
:14; } se
; /* sign extension */
292 for (j
= 0; j
< 1008; j
+= 2) {
293 /* sign extension from 14-bit to signed int */
295 /* from signed int to unsigned int */
297 /* from 14-bit to 16-bit */
298 *u16dst
++ = utmp
<< 2 | utmp
>> 12;
304 dev
->next_sample
= sample
[i
] + 252;
307 case MSI2500_PIX_FMT_SDR_MSI2500_384
: /* 384 x IQ samples */
308 /* Dump unknown 'garbage' data */
309 dev_dbg_ratelimited(dev
->dev
, "%*ph\n", 24, &src
[1000]);
310 memcpy(dst
, src
, 984);
314 dev
->next_sample
= sample
[i
] + 384;
316 case V4L2_SDR_FMT_CS8
: /* 504 x IQ samples */
317 memcpy(dst
, src
, 1008);
321 dev
->next_sample
= sample
[i
] + 504;
323 case MSI2500_PIX_FMT_SDR_S12
: /* 336 x IQ samples */
324 memcpy(dst
, src
, 1008);
328 dev
->next_sample
= sample
[i
] + 336;
330 case V4L2_SDR_FMT_CS14LE
: /* 252 x IQ samples */
331 memcpy(dst
, src
, 1008);
335 dev
->next_sample
= sample
[i
] + 252;
342 /* calculate sample rate and output it in 10 seconds intervals */
343 if (unlikely(time_is_before_jiffies(dev
->jiffies_next
))) {
344 #define MSECS 10000UL
345 unsigned int msecs
= jiffies_to_msecs(jiffies
-
346 dev
->jiffies_next
+ msecs_to_jiffies(MSECS
));
347 unsigned int samples
= dev
->next_sample
- dev
->sample
;
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
);
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.
363 static void msi2500_isoc_handler(struct urb
*urb
)
365 struct msi2500_dev
*dev
= (struct msi2500_dev
*)urb
->context
;
366 int i
, flen
, fstatus
;
367 unsigned char *iso_buf
= NULL
;
368 struct msi2500_frame_buf
*fbuf
;
370 if (unlikely(urb
->status
== -ENOENT
||
371 urb
->status
== -ECONNRESET
||
372 urb
->status
== -ESHUTDOWN
)) {
373 dev_dbg(dev
->dev
, "URB (%p) unlinked %ssynchronously\n",
374 urb
, urb
->status
== -ENOENT
? "" : "a");
378 if (unlikely(urb
->status
!= 0)) {
379 dev_dbg(dev
->dev
, "called with status %d\n", urb
->status
);
380 /* Give up after a number of contiguous errors */
381 if (++dev
->isoc_errors
> MAX_ISOC_ERRORS
)
382 dev_dbg(dev
->dev
, "Too many ISOC errors, bailing out\n");
385 /* Reset ISOC error counter. We did get here, after all. */
386 dev
->isoc_errors
= 0;
390 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
393 /* Check frame error */
394 fstatus
= urb
->iso_frame_desc
[i
].status
;
395 if (unlikely(fstatus
)) {
396 dev_dbg_ratelimited(dev
->dev
,
397 "frame=%d/%d has error %d skipping\n",
398 i
, urb
->number_of_packets
, fstatus
);
402 /* Check if that frame contains data */
403 flen
= urb
->iso_frame_desc
[i
].actual_length
;
404 if (unlikely(flen
== 0))
407 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
409 /* Get free framebuffer */
410 fbuf
= msi2500_get_next_fill_buf(dev
);
411 if (unlikely(fbuf
== NULL
)) {
413 dev_dbg_ratelimited(dev
->dev
,
414 "videobuf is full, %d packets dropped\n",
419 /* fill framebuffer */
420 ptr
= vb2_plane_vaddr(&fbuf
->vb
.vb2_buf
, 0);
421 flen
= msi2500_convert_stream(dev
, ptr
, iso_buf
, flen
);
422 vb2_set_plane_payload(&fbuf
->vb
.vb2_buf
, 0, flen
);
423 vb2_buffer_done(&fbuf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
427 i
= usb_submit_urb(urb
, GFP_ATOMIC
);
428 if (unlikely(i
!= 0))
429 dev_dbg(dev
->dev
, "Error (%d) re-submitting urb\n", i
);
432 static void msi2500_iso_stop(struct msi2500_dev
*dev
)
436 dev_dbg(dev
->dev
, "\n");
438 /* Unlinking ISOC buffers one by one */
439 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
441 dev_dbg(dev
->dev
, "Unlinking URB %p\n", dev
->urbs
[i
]);
442 usb_kill_urb(dev
->urbs
[i
]);
447 static void msi2500_iso_free(struct msi2500_dev
*dev
)
451 dev_dbg(dev
->dev
, "\n");
453 /* Freeing ISOC buffers one by one */
454 for (i
= 0; i
< MAX_ISO_BUFS
; 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
);
463 usb_free_urb(dev
->urbs
[i
]);
469 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
470 static void msi2500_isoc_cleanup(struct msi2500_dev
*dev
)
472 dev_dbg(dev
->dev
, "\n");
474 msi2500_iso_stop(dev
);
475 msi2500_iso_free(dev
);
478 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
479 static int msi2500_isoc_init(struct msi2500_dev
*dev
)
484 dev_dbg(dev
->dev
, "\n");
486 dev
->isoc_errors
= 0;
488 ret
= usb_set_interface(dev
->udev
, 0, 1);
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
);
496 msi2500_isoc_cleanup(dev
);
500 dev_dbg(dev
->dev
, "Allocated URB at 0x%p\n", urb
);
503 urb
->dev
= dev
->udev
;
504 urb
->pipe
= usb_rcvisocpipe(dev
->udev
, 0x81);
505 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
506 urb
->transfer_buffer
= usb_alloc_coherent(dev
->udev
,
508 GFP_KERNEL
, &urb
->transfer_dma
);
509 if (urb
->transfer_buffer
== NULL
) {
511 "Failed to allocate urb buffer %d\n", i
);
512 msi2500_isoc_cleanup(dev
);
515 urb
->transfer_buffer_length
= ISO_BUFFER_SIZE
;
516 urb
->complete
= msi2500_isoc_handler
;
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
;
527 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
528 ret
= usb_submit_urb(dev
->urbs
[i
], GFP_KERNEL
);
531 "usb_submit_urb %d failed with error %d\n",
533 msi2500_isoc_cleanup(dev
);
536 dev_dbg(dev
->dev
, "URB 0x%p submitted.\n", dev
->urbs
[i
]);
543 /* Must be called with vb_queue_lock hold */
544 static void msi2500_cleanup_queued_bufs(struct msi2500_dev
*dev
)
548 dev_dbg(dev
->dev
, "\n");
550 spin_lock_irqsave(&dev
->queued_bufs_lock
, flags
);
551 while (!list_empty(&dev
->queued_bufs
)) {
552 struct msi2500_frame_buf
*buf
;
554 buf
= list_entry(dev
->queued_bufs
.next
,
555 struct msi2500_frame_buf
, list
);
556 list_del(&buf
->list
);
557 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
559 spin_unlock_irqrestore(&dev
->queued_bufs_lock
, flags
);
562 /* The user yanked out the cable... */
563 static void msi2500_disconnect(struct usb_interface
*intf
)
565 struct v4l2_device
*v
= usb_get_intfdata(intf
);
566 struct msi2500_dev
*dev
=
567 container_of(v
, struct msi2500_dev
, v4l2_dev
);
569 dev_dbg(dev
->dev
, "\n");
571 mutex_lock(&dev
->vb_queue_lock
);
572 mutex_lock(&dev
->v4l2_lock
);
573 /* No need to keep the urbs around after disconnection */
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
);
581 v4l2_device_put(&dev
->v4l2_dev
);
584 static int msi2500_querycap(struct file
*file
, void *fh
,
585 struct v4l2_capability
*cap
)
587 struct msi2500_dev
*dev
= video_drvdata(file
);
589 dev_dbg(dev
->dev
, "\n");
591 strscpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
592 strscpy(cap
->card
, dev
->vdev
.name
, sizeof(cap
->card
));
593 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
597 /* Videobuf2 operations */
598 static int msi2500_queue_setup(struct vb2_queue
*vq
,
599 unsigned int *nbuffers
,
600 unsigned int *nplanes
, unsigned int sizes
[],
601 struct device
*alloc_devs
[])
603 struct msi2500_dev
*dev
= vb2_get_drv_priv(vq
);
605 dev_dbg(dev
->dev
, "nbuffers=%d\n", *nbuffers
);
607 /* Absolute min and max number of buffers available for mmap() */
608 *nbuffers
= clamp_t(unsigned int, *nbuffers
, 8, 32);
610 sizes
[0] = PAGE_ALIGN(dev
->buffersize
);
611 dev_dbg(dev
->dev
, "nbuffers=%d sizes[0]=%d\n", *nbuffers
, sizes
[0]);
615 static void msi2500_buf_queue(struct vb2_buffer
*vb
)
617 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
618 struct msi2500_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
619 struct msi2500_frame_buf
*buf
= container_of(vbuf
,
620 struct msi2500_frame_buf
,
624 /* Check the device has not disconnected between prep and queuing */
625 if (unlikely(!dev
->udev
)) {
626 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
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
);
635 #define CMD_WREG 0x41
636 #define CMD_START_STREAMING 0x43
637 #define CMD_STOP_STREAMING 0x45
638 #define CMD_READ_UNKNOWN 0x48
640 #define msi2500_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
642 if (_t & USB_DIR_IN) \
643 _direction = "<<<"; \
645 _direction = ">>>"; \
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); \
651 static int msi2500_ctrl_msg(struct msi2500_dev
*dev
, u8 cmd
, u32 data
)
655 u8 requesttype
= USB_DIR_OUT
| USB_TYPE_VENDOR
;
656 u16 value
= (data
>> 0) & 0xffff;
657 u16 index
= (data
>> 16) & 0xffff;
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);
664 dev_err(dev
->dev
, "failed %d, cmd %02x, data %04x\n",
670 static int msi2500_set_usb_adc(struct msi2500_dev
*dev
)
673 unsigned int f_vco
, f_sr
, div_n
, k
, k_cw
, div_out
;
674 u32 reg3
, reg4
, reg7
;
675 struct v4l2_ctrl
*bandwidth_auto
;
676 struct v4l2_ctrl
*bandwidth
;
680 /* set tuner, subdev, filters according to sampling rate */
681 bandwidth_auto
= v4l2_ctrl_find(&dev
->hdl
,
682 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
);
683 if (v4l2_ctrl_g_ctrl(bandwidth_auto
)) {
684 bandwidth
= v4l2_ctrl_find(&dev
->hdl
,
685 V4L2_CID_RF_TUNER_BANDWIDTH
);
686 v4l2_ctrl_s_ctrl(bandwidth
, dev
->f_adc
);
689 /* select stream format */
690 switch (dev
->pixelformat
) {
691 case V4L2_SDR_FMT_CU8
:
692 reg7
= 0x000c9407; /* 504 */
694 case V4L2_SDR_FMT_CU16LE
:
695 reg7
= 0x00009407; /* 252 */
697 case V4L2_SDR_FMT_CS8
:
698 reg7
= 0x000c9407; /* 504 */
700 case MSI2500_PIX_FMT_SDR_MSI2500_384
:
701 reg7
= 0x0000a507; /* 384 */
703 case MSI2500_PIX_FMT_SDR_S12
:
704 reg7
= 0x00008507; /* 336 */
706 case V4L2_SDR_FMT_CS14LE
:
707 reg7
= 0x00009407; /* 252 */
710 reg7
= 0x000c9407; /* 504 */
715 * Fractional-N synthesizer
717 * +----------------------------------------+
719 * Fref +----+ +-------+ +-----+ +------+ +---+
720 * ------> | PD | --> | VCO | --> | /2 | ------> | /N.F | <-- | K |
721 * +----+ +-------+ +-----+ +------+ +---+
725 * +-------+ +-----+ Fout
726 * | /Rout | --> | /12 | ------>
730 * Synthesizer config is just a educated guess...
732 * [7:0] 0x03, register address
733 * [8] 1, power control
734 * [9] ?, power control
735 * [12:10] output divider
738 * [15] fractional MSB, bit 20
754 * VCO 202000000 - 720000000++
757 #define F_REF 24000000
759 #define DIV_LO_OUT 12
763 /* XXX: Filters? AGC? VCO band? */
766 else if (f_sr
< 7000000)
768 else if (f_sr
< 8500000)
773 for (div_out
= 4; div_out
< 16; div_out
+= 2) {
774 f_vco
= f_sr
* div_out
* DIV_LO_OUT
;
775 dev_dbg(dev
->dev
, "div_out=%u f_vco=%u\n", div_out
, f_vco
);
776 if (f_vco
>= 202000000)
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
);
785 reg3
|= (div_out
/ 2 - 1) << 10;
786 reg3
|= ((k_cw
>> 20) & 0x000001) << 15; /* [20] */
787 reg4
|= ((k_cw
>> 0) & 0x0fffff) << 8; /* [19:0] */
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
);
793 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, 0x00608008);
797 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, 0x00000c05);
801 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, 0x00020000);
805 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, 0x00480102);
809 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, 0x00f38008);
813 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, reg7
);
817 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, reg4
);
821 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, reg3
);
826 static int msi2500_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
828 struct msi2500_dev
*dev
= vb2_get_drv_priv(vq
);
831 dev_dbg(dev
->dev
, "\n");
836 if (mutex_lock_interruptible(&dev
->v4l2_lock
))
840 v4l2_subdev_call(dev
->v4l2_subdev
, core
, s_power
, 1);
842 ret
= msi2500_set_usb_adc(dev
);
844 ret
= msi2500_isoc_init(dev
);
846 msi2500_cleanup_queued_bufs(dev
);
848 ret
= msi2500_ctrl_msg(dev
, CMD_START_STREAMING
, 0);
850 mutex_unlock(&dev
->v4l2_lock
);
855 static void msi2500_stop_streaming(struct vb2_queue
*vq
)
857 struct msi2500_dev
*dev
= vb2_get_drv_priv(vq
);
859 dev_dbg(dev
->dev
, "\n");
861 mutex_lock(&dev
->v4l2_lock
);
864 msi2500_isoc_cleanup(dev
);
866 msi2500_cleanup_queued_bufs(dev
);
868 /* according to tests, at least 700us delay is required */
870 if (!msi2500_ctrl_msg(dev
, CMD_STOP_STREAMING
, 0)) {
871 /* sleep USB IF / ADC */
872 msi2500_ctrl_msg(dev
, CMD_WREG
, 0x01000003);
876 v4l2_subdev_call(dev
->v4l2_subdev
, core
, s_power
, 0);
878 mutex_unlock(&dev
->v4l2_lock
);
881 static const struct vb2_ops msi2500_vb2_ops
= {
882 .queue_setup
= msi2500_queue_setup
,
883 .buf_queue
= msi2500_buf_queue
,
884 .start_streaming
= msi2500_start_streaming
,
885 .stop_streaming
= msi2500_stop_streaming
,
886 .wait_prepare
= vb2_ops_wait_prepare
,
887 .wait_finish
= vb2_ops_wait_finish
,
890 static int msi2500_enum_fmt_sdr_cap(struct file
*file
, void *priv
,
891 struct v4l2_fmtdesc
*f
)
893 struct msi2500_dev
*dev
= video_drvdata(file
);
895 dev_dbg(dev
->dev
, "index=%d\n", f
->index
);
897 if (f
->index
>= dev
->num_formats
)
900 f
->pixelformat
= formats
[f
->index
].pixelformat
;
905 static int msi2500_g_fmt_sdr_cap(struct file
*file
, void *priv
,
906 struct v4l2_format
*f
)
908 struct msi2500_dev
*dev
= video_drvdata(file
);
910 dev_dbg(dev
->dev
, "pixelformat fourcc %4.4s\n",
911 (char *)&dev
->pixelformat
);
913 f
->fmt
.sdr
.pixelformat
= dev
->pixelformat
;
914 f
->fmt
.sdr
.buffersize
= dev
->buffersize
;
915 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
920 static int msi2500_s_fmt_sdr_cap(struct file
*file
, void *priv
,
921 struct v4l2_format
*f
)
923 struct msi2500_dev
*dev
= video_drvdata(file
);
924 struct vb2_queue
*q
= &dev
->vb_queue
;
927 dev_dbg(dev
->dev
, "pixelformat fourcc %4.4s\n",
928 (char *)&f
->fmt
.sdr
.pixelformat
);
933 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
934 for (i
= 0; i
< dev
->num_formats
; i
++) {
935 if (formats
[i
].pixelformat
== f
->fmt
.sdr
.pixelformat
) {
936 dev
->pixelformat
= formats
[i
].pixelformat
;
937 dev
->buffersize
= formats
[i
].buffersize
;
938 f
->fmt
.sdr
.buffersize
= formats
[i
].buffersize
;
943 dev
->pixelformat
= formats
[0].pixelformat
;
944 dev
->buffersize
= formats
[0].buffersize
;
945 f
->fmt
.sdr
.pixelformat
= formats
[0].pixelformat
;
946 f
->fmt
.sdr
.buffersize
= formats
[0].buffersize
;
951 static int msi2500_try_fmt_sdr_cap(struct file
*file
, void *priv
,
952 struct v4l2_format
*f
)
954 struct msi2500_dev
*dev
= video_drvdata(file
);
957 dev_dbg(dev
->dev
, "pixelformat fourcc %4.4s\n",
958 (char *)&f
->fmt
.sdr
.pixelformat
);
960 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
961 for (i
= 0; i
< dev
->num_formats
; i
++) {
962 if (formats
[i
].pixelformat
== f
->fmt
.sdr
.pixelformat
) {
963 f
->fmt
.sdr
.buffersize
= formats
[i
].buffersize
;
968 f
->fmt
.sdr
.pixelformat
= formats
[0].pixelformat
;
969 f
->fmt
.sdr
.buffersize
= formats
[0].buffersize
;
974 static int msi2500_s_tuner(struct file
*file
, void *priv
,
975 const struct v4l2_tuner
*v
)
977 struct msi2500_dev
*dev
= video_drvdata(file
);
980 dev_dbg(dev
->dev
, "index=%d\n", v
->index
);
984 else if (v
->index
== 1)
985 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, s_tuner
, v
);
992 static int msi2500_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*v
)
994 struct msi2500_dev
*dev
= video_drvdata(file
);
997 dev_dbg(dev
->dev
, "index=%d\n", v
->index
);
1000 strscpy(v
->name
, "Mirics MSi2500", sizeof(v
->name
));
1001 v
->type
= V4L2_TUNER_ADC
;
1002 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
1003 v
->rangelow
= 1200000;
1004 v
->rangehigh
= 15000000;
1006 } else if (v
->index
== 1) {
1007 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, g_tuner
, v
);
1015 static int msi2500_g_frequency(struct file
*file
, void *priv
,
1016 struct v4l2_frequency
*f
)
1018 struct msi2500_dev
*dev
= video_drvdata(file
);
1021 dev_dbg(dev
->dev
, "tuner=%d type=%d\n", f
->tuner
, f
->type
);
1023 if (f
->tuner
== 0) {
1024 f
->frequency
= dev
->f_adc
;
1026 } else if (f
->tuner
== 1) {
1027 f
->type
= V4L2_TUNER_RF
;
1028 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, g_frequency
, f
);
1036 static int msi2500_s_frequency(struct file
*file
, void *priv
,
1037 const struct v4l2_frequency
*f
)
1039 struct msi2500_dev
*dev
= video_drvdata(file
);
1042 dev_dbg(dev
->dev
, "tuner=%d type=%d frequency=%u\n",
1043 f
->tuner
, f
->type
, f
->frequency
);
1045 if (f
->tuner
== 0) {
1046 dev
->f_adc
= clamp_t(unsigned int, f
->frequency
,
1048 bands
[0].rangehigh
);
1049 dev_dbg(dev
->dev
, "ADC frequency=%u Hz\n", dev
->f_adc
);
1050 ret
= msi2500_set_usb_adc(dev
);
1051 } else if (f
->tuner
== 1) {
1052 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
, s_frequency
, f
);
1060 static int msi2500_enum_freq_bands(struct file
*file
, void *priv
,
1061 struct v4l2_frequency_band
*band
)
1063 struct msi2500_dev
*dev
= video_drvdata(file
);
1066 dev_dbg(dev
->dev
, "tuner=%d type=%d index=%d\n",
1067 band
->tuner
, band
->type
, band
->index
);
1069 if (band
->tuner
== 0) {
1070 if (band
->index
>= ARRAY_SIZE(bands
)) {
1073 *band
= bands
[band
->index
];
1076 } else if (band
->tuner
== 1) {
1077 ret
= v4l2_subdev_call(dev
->v4l2_subdev
, tuner
,
1078 enum_freq_bands
, band
);
1086 static const struct v4l2_ioctl_ops msi2500_ioctl_ops
= {
1087 .vidioc_querycap
= msi2500_querycap
,
1089 .vidioc_enum_fmt_sdr_cap
= msi2500_enum_fmt_sdr_cap
,
1090 .vidioc_g_fmt_sdr_cap
= msi2500_g_fmt_sdr_cap
,
1091 .vidioc_s_fmt_sdr_cap
= msi2500_s_fmt_sdr_cap
,
1092 .vidioc_try_fmt_sdr_cap
= msi2500_try_fmt_sdr_cap
,
1094 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1095 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1096 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1097 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1098 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1099 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1101 .vidioc_streamon
= vb2_ioctl_streamon
,
1102 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1104 .vidioc_g_tuner
= msi2500_g_tuner
,
1105 .vidioc_s_tuner
= msi2500_s_tuner
,
1107 .vidioc_g_frequency
= msi2500_g_frequency
,
1108 .vidioc_s_frequency
= msi2500_s_frequency
,
1109 .vidioc_enum_freq_bands
= msi2500_enum_freq_bands
,
1111 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1112 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1113 .vidioc_log_status
= v4l2_ctrl_log_status
,
1116 static const struct v4l2_file_operations msi2500_fops
= {
1117 .owner
= THIS_MODULE
,
1118 .open
= v4l2_fh_open
,
1119 .release
= vb2_fop_release
,
1120 .read
= vb2_fop_read
,
1121 .poll
= vb2_fop_poll
,
1122 .mmap
= vb2_fop_mmap
,
1123 .unlocked_ioctl
= video_ioctl2
,
1126 static const struct video_device msi2500_template
= {
1127 .name
= "Mirics MSi3101 SDR Dongle",
1128 .release
= video_device_release_empty
,
1129 .fops
= &msi2500_fops
,
1130 .ioctl_ops
= &msi2500_ioctl_ops
,
1133 static void msi2500_video_release(struct v4l2_device
*v
)
1135 struct msi2500_dev
*dev
= container_of(v
, struct msi2500_dev
, v4l2_dev
);
1137 v4l2_ctrl_handler_free(&dev
->hdl
);
1138 v4l2_device_unregister(&dev
->v4l2_dev
);
1142 static int msi2500_transfer_one_message(struct spi_master
*master
,
1143 struct spi_message
*m
)
1145 struct msi2500_dev
*dev
= spi_master_get_devdata(master
);
1146 struct spi_transfer
*t
;
1150 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
1151 dev_dbg(dev
->dev
, "msg=%*ph\n", t
->len
, t
->tx_buf
);
1152 data
= 0x09; /* reg 9 is SPI adapter */
1153 data
|= ((u8
*)t
->tx_buf
)[0] << 8;
1154 data
|= ((u8
*)t
->tx_buf
)[1] << 16;
1155 data
|= ((u8
*)t
->tx_buf
)[2] << 24;
1156 ret
= msi2500_ctrl_msg(dev
, CMD_WREG
, data
);
1160 spi_finalize_current_message(master
);
1164 static int msi2500_probe(struct usb_interface
*intf
,
1165 const struct usb_device_id
*id
)
1167 struct msi2500_dev
*dev
;
1168 struct v4l2_subdev
*sd
;
1169 struct spi_master
*master
;
1171 static struct spi_board_info board_info
= {
1172 .modalias
= "msi001",
1175 .max_speed_hz
= 12000000,
1178 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1184 mutex_init(&dev
->v4l2_lock
);
1185 mutex_init(&dev
->vb_queue_lock
);
1186 spin_lock_init(&dev
->queued_bufs_lock
);
1187 INIT_LIST_HEAD(&dev
->queued_bufs
);
1188 dev
->dev
= &intf
->dev
;
1189 dev
->udev
= interface_to_usbdev(intf
);
1190 dev
->f_adc
= bands
[0].rangelow
;
1191 dev
->pixelformat
= formats
[0].pixelformat
;
1192 dev
->buffersize
= formats
[0].buffersize
;
1193 dev
->num_formats
= NUM_FORMATS
;
1194 if (!msi2500_emulated_fmt
)
1195 dev
->num_formats
-= 2;
1197 /* Init videobuf2 queue structure */
1198 dev
->vb_queue
.type
= V4L2_BUF_TYPE_SDR_CAPTURE
;
1199 dev
->vb_queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
1200 dev
->vb_queue
.drv_priv
= dev
;
1201 dev
->vb_queue
.buf_struct_size
= sizeof(struct msi2500_frame_buf
);
1202 dev
->vb_queue
.ops
= &msi2500_vb2_ops
;
1203 dev
->vb_queue
.mem_ops
= &vb2_vmalloc_memops
;
1204 dev
->vb_queue
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1205 ret
= vb2_queue_init(&dev
->vb_queue
);
1207 dev_err(dev
->dev
, "Could not initialize vb2 queue\n");
1211 /* Init video_device structure */
1212 dev
->vdev
= msi2500_template
;
1213 dev
->vdev
.queue
= &dev
->vb_queue
;
1214 dev
->vdev
.queue
->lock
= &dev
->vb_queue_lock
;
1215 video_set_drvdata(&dev
->vdev
, dev
);
1217 /* Register the v4l2_device structure */
1218 dev
->v4l2_dev
.release
= msi2500_video_release
;
1219 ret
= v4l2_device_register(&intf
->dev
, &dev
->v4l2_dev
);
1221 dev_err(dev
->dev
, "Failed to register v4l2-device (%d)\n", ret
);
1225 /* SPI master adapter */
1226 master
= spi_alloc_master(dev
->dev
, 0);
1227 if (master
== NULL
) {
1229 goto err_unregister_v4l2_dev
;
1232 dev
->master
= master
;
1233 master
->bus_num
= 0;
1234 master
->num_chipselect
= 1;
1235 master
->transfer_one_message
= msi2500_transfer_one_message
;
1236 spi_master_set_devdata(master
, dev
);
1237 ret
= spi_register_master(master
);
1239 spi_master_put(master
);
1240 goto err_unregister_v4l2_dev
;
1243 /* load v4l2 subdevice */
1244 sd
= v4l2_spi_new_subdev(&dev
->v4l2_dev
, master
, &board_info
);
1245 dev
->v4l2_subdev
= sd
;
1247 dev_err(dev
->dev
, "cannot get v4l2 subdevice\n");
1249 goto err_unregister_master
;
1252 /* Register controls */
1253 v4l2_ctrl_handler_init(&dev
->hdl
, 0);
1254 if (dev
->hdl
.error
) {
1255 ret
= dev
->hdl
.error
;
1256 dev_err(dev
->dev
, "Could not initialize controls\n");
1257 goto err_free_controls
;
1260 /* currently all controls are from subdev */
1261 v4l2_ctrl_add_handler(&dev
->hdl
, sd
->ctrl_handler
, NULL
, true);
1263 dev
->v4l2_dev
.ctrl_handler
= &dev
->hdl
;
1264 dev
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1265 dev
->vdev
.lock
= &dev
->v4l2_lock
;
1266 dev
->vdev
.device_caps
= V4L2_CAP_SDR_CAPTURE
| V4L2_CAP_STREAMING
|
1267 V4L2_CAP_READWRITE
| V4L2_CAP_TUNER
;
1269 ret
= video_register_device(&dev
->vdev
, VFL_TYPE_SDR
, -1);
1272 "Failed to register as video device (%d)\n", ret
);
1273 goto err_unregister_v4l2_dev
;
1275 dev_info(dev
->dev
, "Registered as %s\n",
1276 video_device_node_name(&dev
->vdev
));
1277 dev_notice(dev
->dev
,
1278 "SDR API is still slightly experimental and functionality changes may follow\n");
1281 v4l2_ctrl_handler_free(&dev
->hdl
);
1282 err_unregister_master
:
1283 spi_unregister_master(dev
->master
);
1284 err_unregister_v4l2_dev
:
1285 v4l2_device_unregister(&dev
->v4l2_dev
);
1292 /* USB device ID list */
1293 static const struct usb_device_id msi2500_id_table
[] = {
1294 {USB_DEVICE(0x1df7, 0x2500)}, /* Mirics MSi3101 SDR Dongle */
1295 {USB_DEVICE(0x2040, 0xd300)}, /* Hauppauge WinTV 133559 LF */
1298 MODULE_DEVICE_TABLE(usb
, msi2500_id_table
);
1300 /* USB subsystem interface */
1301 static struct usb_driver msi2500_driver
= {
1302 .name
= KBUILD_MODNAME
,
1303 .probe
= msi2500_probe
,
1304 .disconnect
= msi2500_disconnect
,
1305 .id_table
= msi2500_id_table
,
1308 module_usb_driver(msi2500_driver
);
1310 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1311 MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1312 MODULE_LICENSE("GPL");