2 * Mirics MSi3101 SDR Dongle driver
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/input.h>
26 #include <linux/videodev2.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-event.h>
31 #include <linux/usb.h>
32 #include <linux/mutex.h>
33 #include <media/videobuf2-vmalloc.h>
42 /* 60 – 120 MHz band, lna 24dB, mixer 19dB */
43 static const struct msi3101_gain msi3101_gain_lut_120
[] = {
149 /* 120 – 245 MHz band, lna 24dB, mixer 19dB */
150 static const struct msi3101_gain msi3101_gain_lut_245
[] = {
256 /* 420 – 1000 MHz band, lna 7dB, mixer 19dB */
257 static const struct msi3101_gain msi3101_gain_lut_1000
[] = {
349 * bAlternateSetting 1
351 * bEndpointAddress 0x81 EP 1 IN
353 * Transfer Type Isochronous
354 * wMaxPacketSize 0x1400 3x 1024 bytes
357 #define MAX_ISO_BUFS (8)
358 #define ISO_FRAMES_PER_DESC (8)
359 #define ISO_MAX_FRAME_SIZE (3 * 1024)
360 #define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
362 #define MAX_ISOC_ERRORS 20
364 #define MSI3101_CID_SAMPLING_MODE ((V4L2_CID_USER_BASE | 0xf000) + 0)
365 #define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 1)
366 #define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 2)
367 #define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10)
368 #define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11)
369 #define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12)
370 #define MSI3101_CID_TUNER_GAIN ((V4L2_CID_USER_BASE | 0xf000) + 13)
372 /* intermediate buffers with raw data from the USB device */
373 struct msi3101_frame_buf
{
374 struct vb2_buffer vb
; /* common v4l buffer stuff -- must be first */
375 struct list_head list
;
376 void *data
; /* raw data from USB device */
377 int filled
; /* number of bytes filled to *data */
380 struct msi3101_state
{
381 struct video_device vdev
;
382 struct v4l2_device v4l2_dev
;
384 /* videobuf2 queue and queued buffers list */
385 struct vb2_queue vb_queue
;
386 struct list_head queued_bufs
;
387 spinlock_t queued_bufs_lock
; /* Protects queued_bufs */
389 /* Note if taking both locks v4l2_lock must always be locked first! */
390 struct mutex v4l2_lock
; /* Protects everything else */
391 struct mutex vb_queue_lock
; /* Protects vb_queue and capt_file */
393 /* Pointer to our usb_device, will be NULL after unplug */
394 struct usb_device
*udev
; /* Both mutexes most be hold when setting! */
396 unsigned int isoc_errors
; /* number of contiguous ISOC errors */
397 unsigned int vb_full
; /* vb is full and packets dropped */
399 struct urb
*urbs
[MAX_ISO_BUFS
];
400 int (*convert_stream
) (struct msi3101_state
*s
, u32
*dst
, u8
*src
,
401 unsigned int src_len
);
404 struct v4l2_ctrl_handler ctrl_handler
;
405 struct v4l2_ctrl
*ctrl_sampling_rate
;
406 struct v4l2_ctrl
*ctrl_tuner_rf
;
407 struct v4l2_ctrl
*ctrl_tuner_bw
;
408 struct v4l2_ctrl
*ctrl_tuner_if
;
409 struct v4l2_ctrl
*ctrl_tuner_gain
;
411 u32 next_sample
; /* for track lost packets */
412 u32 sample
; /* for sample rate calc */
413 unsigned long jiffies
;
414 unsigned int sample_ctrl_bit
[4];
417 /* Private functions */
418 static struct msi3101_frame_buf
*msi3101_get_next_fill_buf(
419 struct msi3101_state
*s
)
421 unsigned long flags
= 0;
422 struct msi3101_frame_buf
*buf
= NULL
;
424 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
425 if (list_empty(&s
->queued_bufs
))
428 buf
= list_entry(s
->queued_bufs
.next
, struct msi3101_frame_buf
, list
);
429 list_del(&buf
->list
);
431 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
436 * +===========================================================================
437 * | 00-1023 | USB packet
438 * +===========================================================================
439 * | 00- 03 | sequence number of first sample in that USB packet
440 * +---------------------------------------------------------------------------
442 * +---------------------------------------------------------------------------
443 * | 16- 175 | samples
444 * +---------------------------------------------------------------------------
445 * | 176- 179 | control bits for previous samples
446 * +---------------------------------------------------------------------------
447 * | 180- 339 | samples
448 * +---------------------------------------------------------------------------
449 * | 340- 343 | control bits for previous samples
450 * +---------------------------------------------------------------------------
451 * | 344- 503 | samples
452 * +---------------------------------------------------------------------------
453 * | 504- 507 | control bits for previous samples
454 * +---------------------------------------------------------------------------
455 * | 508- 667 | samples
456 * +---------------------------------------------------------------------------
457 * | 668- 671 | control bits for previous samples
458 * +---------------------------------------------------------------------------
459 * | 672- 831 | samples
460 * +---------------------------------------------------------------------------
461 * | 832- 835 | control bits for previous samples
462 * +---------------------------------------------------------------------------
463 * | 836- 995 | samples
464 * +---------------------------------------------------------------------------
465 * | 996- 999 | control bits for previous samples
466 * +---------------------------------------------------------------------------
467 * | 1000-1023 | garbage
468 * +---------------------------------------------------------------------------
470 * Bytes 4 - 7 could have some meaning?
472 * Control bits for previous samples is 32-bit field, containing 16 x 2-bit
473 * numbers. This results one 2-bit number for 8 samples. It is likely used for
474 * for bit shifting sample by given bits, increasing actual sampling resolution.
475 * Number 2 (0b10) was never seen.
477 * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes
481 * Converts signed 10-bit integer into 32-bit IEEE floating point
483 * Will be exact from 0 to 2^24. Above that, we round towards zero
484 * as the fractional bits will not fit in a float. (It would be better to
485 * round towards even as the fpu does, but that is slower.)
487 #define I2F_FRAC_BITS 23
488 #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
489 static u32
msi3101_convert_sample_384(struct msi3101_state
*s
, u16 x
, int shift
)
491 u32 msb
, exponent
, fraction
, sign
;
492 s
->sample_ctrl_bit
[shift
]++;
494 /* Zero is special */
498 /* Convert 10-bit two's complement to 13-bit */
500 x
|= ~0U << 10; /* set all the rest bits to one */
503 x
&= 0xfff; /* result is 12 bit ... + sign */
510 /* Get location of the most significant bit */
514 * Use a rotate instead of a shift because that works both leftwards
515 * and rightwards due to the mod(32) behaviour. This means we don't
516 * need to check to see if we are above 2^24 or not.
518 fraction
= ror32(x
, (msb
- I2F_FRAC_BITS
) & 0x1f) & I2F_MASK
;
519 exponent
= (127 + msb
) << I2F_FRAC_BITS
;
521 return (fraction
+ exponent
) | sign
;
524 #define MSI3101_CONVERT_IN_URB_HANDLER
525 #define MSI3101_EXTENSIVE_DEBUG
526 static int msi3101_convert_stream_384(struct msi3101_state
*s
, u32
*dst
,
527 u8
*src
, unsigned int src_len
)
529 int i
, j
, k
, l
, i_max
, dst_len
= 0;
532 #ifdef MSI3101_EXTENSIVE_DEBUG
535 /* There could be 1-3 1024 bytes URB frames */
536 i_max
= src_len
/ 1024;
537 for (i
= 0; i
< i_max
; i
++) {
538 #ifdef MSI3101_EXTENSIVE_DEBUG
539 sample_num
[i
] = src
[3] << 24 | src
[2] << 16 | src
[1] << 8 | src
[0] << 0;
540 if (i
== 0 && s
->next_sample
!= sample_num
[0]) {
541 dev_dbg(&s
->udev
->dev
,
542 "%d samples lost, %d %08x:%08x\n",
543 sample_num
[0] - s
->next_sample
,
544 src_len
, s
->next_sample
, sample_num
[0]);
548 * Dump all unknown 'garbage' data - maybe we will discover
549 * someday if there is something rational...
551 dev_dbg_ratelimited(&s
->udev
->dev
,
552 "%*ph %*ph\n", 12, &src
[4], 24, &src
[1000]);
553 memset(&src
[4], 0, 12);
554 memset(&src
[1000], 0, 24);
557 for (j
= 0; j
< 6; j
++) {
558 bits
= src
[160 + 3] << 24 | src
[160 + 2] << 16 | src
[160 + 1] << 8 | src
[160 + 0] << 0;
559 for (k
= 0; k
< 16; k
++) {
560 for (l
= 0; l
< 10; l
+= 5) {
561 sample
[0] = (src
[l
+ 0] & 0xff) >> 0 | (src
[l
+ 1] & 0x03) << 8;
562 sample
[1] = (src
[l
+ 1] & 0xfc) >> 2 | (src
[l
+ 2] & 0x0f) << 6;
563 sample
[2] = (src
[l
+ 2] & 0xf0) >> 4 | (src
[l
+ 3] & 0x3f) << 4;
564 sample
[3] = (src
[l
+ 3] & 0xc0) >> 6 | (src
[l
+ 4] & 0xff) << 2;
566 *dst
++ = msi3101_convert_sample_384(s
, sample
[0], (bits
>> (2 * k
)) & 0x3);
567 *dst
++ = msi3101_convert_sample_384(s
, sample
[1], (bits
>> (2 * k
)) & 0x3);
568 *dst
++ = msi3101_convert_sample_384(s
, sample
[2], (bits
>> (2 * k
)) & 0x3);
569 *dst
++ = msi3101_convert_sample_384(s
, sample
[3], (bits
>> (2 * k
)) & 0x3);
571 /* 4 x 32bit float samples */
576 #ifdef MSI3101_EXTENSIVE_DEBUG
577 dev_dbg_ratelimited(&s
->udev
->dev
,
578 "sample control bits %08x\n", bits
);
585 #ifdef MSI3101_EXTENSIVE_DEBUG
586 /* calculate samping rate and output it in 10 seconds intervals */
587 if ((s
->jiffies
+ msecs_to_jiffies(10000)) <= jiffies
) {
588 unsigned long jiffies_now
= jiffies
;
589 unsigned long msecs
= jiffies_to_msecs(jiffies_now
) - jiffies_to_msecs(s
->jiffies
);
590 unsigned int samples
= sample_num
[i_max
- 1] - s
->sample
;
591 s
->jiffies
= jiffies_now
;
592 s
->sample
= sample_num
[i_max
- 1];
593 dev_dbg(&s
->udev
->dev
,
594 "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n",
595 src_len
, samples
, msecs
,
596 samples
* 1000UL / msecs
,
597 s
->sample_ctrl_bit
[0], s
->sample_ctrl_bit
[1],
598 s
->sample_ctrl_bit
[2], s
->sample_ctrl_bit
[3]);
601 /* next sample (sample = sample + i * 384) */
602 s
->next_sample
= sample_num
[i_max
- 1] + 384;
608 * Converts signed 14-bit integer into 32-bit IEEE floating point
610 * Will be exact from 0 to 2^24. Above that, we round towards zero
611 * as the fractional bits will not fit in a float. (It would be better to
612 * round towards even as the fpu does, but that is slower.)
614 #define I2F_FRAC_BITS 23
615 #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)
616 static u32
msi3101_convert_sample_252(struct msi3101_state
*s
, u16 x
)
618 u32 msb
, exponent
, fraction
, sign
;
620 /* Zero is special */
624 /* Negative / positive value */
627 x
&= 0x1fff; /* result is 13 bit ... + sign */
633 /* Get location of the most significant bit */
637 * Use a rotate instead of a shift because that works both leftwards
638 * and rightwards due to the mod(32) behaviour. This means we don't
639 * need to check to see if we are above 2^24 or not.
641 fraction
= ror32(x
, (msb
- I2F_FRAC_BITS
) & 0x1f) & I2F_MASK
;
642 exponent
= (127 + msb
) << I2F_FRAC_BITS
;
644 return (fraction
+ exponent
) | sign
;
647 static int msi3101_convert_stream_252(struct msi3101_state
*s
, u32
*dst
,
648 u8
*src
, unsigned int src_len
)
650 int i
, j
, i_max
, dst_len
= 0;
654 /* There could be 1-3 1024 bytes URB frames */
655 i_max
= src_len
/ 1024;
657 for (i
= 0; i
< i_max
; i
++) {
658 sample_num
[i
] = src
[3] << 24 | src
[2] << 16 | src
[1] << 8 | src
[0] << 0;
659 if (i
== 0 && s
->next_sample
!= sample_num
[0]) {
660 dev_dbg_ratelimited(&s
->udev
->dev
,
661 "%d samples lost, %d %08x:%08x\n",
662 sample_num
[0] - s
->next_sample
,
663 src_len
, s
->next_sample
, sample_num
[0]);
667 * Dump all unknown 'garbage' data - maybe we will discover
668 * someday if there is something rational...
670 dev_dbg_ratelimited(&s
->udev
->dev
, "%*ph\n", 12, &src
[4]);
673 for (j
= 0; j
< 1008; j
+= 4) {
674 sample
[0] = src
[j
+ 0] >> 0 | src
[j
+ 1] << 8;
675 sample
[1] = src
[j
+ 2] >> 0 | src
[j
+ 3] << 8;
677 *dst
++ = msi3101_convert_sample_252(s
, sample
[0]);
678 *dst
++ = msi3101_convert_sample_252(s
, sample
[1]);
680 /* 252 x I+Q 32bit float samples */
681 dst_len
+= 252 * 2 * 4;
685 /* calculate samping rate and output it in 10 seconds intervals */
686 if ((s
->jiffies
+ msecs_to_jiffies(10000)) <= jiffies
) {
687 unsigned long jiffies_now
= jiffies
;
688 unsigned long msecs
= jiffies_to_msecs(jiffies_now
) - jiffies_to_msecs(s
->jiffies
);
689 unsigned int samples
= sample_num
[i_max
- 1] - s
->sample
;
690 s
->jiffies
= jiffies_now
;
691 s
->sample
= sample_num
[i_max
- 1];
692 dev_dbg(&s
->udev
->dev
,
693 "slen=%d samples=%u msecs=%lu sampling rate=%lu\n",
694 src_len
, samples
, msecs
,
695 samples
* 1000UL / msecs
);
698 /* next sample (sample = sample + i * 252) */
699 s
->next_sample
= sample_num
[i_max
- 1] + 252;
705 * This gets called for the Isochronous pipe (stream). This is done in interrupt
706 * time, so it has to be fast, not crash, and not stall. Neat.
708 static void msi3101_isoc_handler(struct urb
*urb
)
710 struct msi3101_state
*s
= (struct msi3101_state
*)urb
->context
;
711 int i
, flen
, fstatus
;
712 unsigned char *iso_buf
= NULL
;
713 struct msi3101_frame_buf
*fbuf
;
715 if (urb
->status
== -ENOENT
|| urb
->status
== -ECONNRESET
||
716 urb
->status
== -ESHUTDOWN
) {
717 dev_dbg(&s
->udev
->dev
, "URB (%p) unlinked %ssynchronuously\n",
718 urb
, urb
->status
== -ENOENT
? "" : "a");
722 if (urb
->status
!= 0) {
723 dev_dbg(&s
->udev
->dev
,
724 "msi3101_isoc_handler() called with status %d\n",
726 /* Give up after a number of contiguous errors */
727 if (++s
->isoc_errors
> MAX_ISOC_ERRORS
)
728 dev_dbg(&s
->udev
->dev
,
729 "Too many ISOC errors, bailing out\n");
732 /* Reset ISOC error counter. We did get here, after all. */
737 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
740 /* Check frame error */
741 fstatus
= urb
->iso_frame_desc
[i
].status
;
743 dev_dbg(&s
->udev
->dev
,
744 "frame=%d/%d has error %d skipping\n",
745 i
, urb
->number_of_packets
, fstatus
);
749 /* Check if that frame contains data */
750 flen
= urb
->iso_frame_desc
[i
].actual_length
;
754 iso_buf
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
756 /* Get free framebuffer */
757 fbuf
= msi3101_get_next_fill_buf(s
);
760 dev_dbg_ratelimited(&s
->udev
->dev
,
761 "videobuf is full, %d packets dropped\n",
766 /* fill framebuffer */
767 #ifdef MSI3101_CONVERT_IN_URB_HANDLER
768 ptr
= vb2_plane_vaddr(&fbuf
->vb
, 0);
769 flen
= s
->convert_stream(s
, ptr
, iso_buf
, flen
);
770 vb2_set_plane_payload(&fbuf
->vb
, 0, flen
);
772 memcpy(fbuf
->data
, iso_buf
, flen
);
775 vb2_buffer_done(&fbuf
->vb
, VB2_BUF_STATE_DONE
);
781 i
= usb_submit_urb(urb
, GFP_ATOMIC
);
783 dev_dbg(&s
->udev
->dev
,
784 "Error (%d) re-submitting urb in msi3101_isoc_handler\n",
788 static void msi3101_iso_stop(struct msi3101_state
*s
)
791 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
793 /* Unlinking ISOC buffers one by one */
794 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
796 dev_dbg(&s
->udev
->dev
, "Unlinking URB %p\n",
798 usb_kill_urb(s
->urbs
[i
]);
803 static void msi3101_iso_free(struct msi3101_state
*s
)
806 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
808 /* Freeing ISOC buffers one by one */
809 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
811 dev_dbg(&s
->udev
->dev
, "Freeing URB\n");
812 if (s
->urbs
[i
]->transfer_buffer
) {
813 usb_free_coherent(s
->udev
,
814 s
->urbs
[i
]->transfer_buffer_length
,
815 s
->urbs
[i
]->transfer_buffer
,
816 s
->urbs
[i
]->transfer_dma
);
818 usb_free_urb(s
->urbs
[i
]);
824 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
825 static void msi3101_isoc_cleanup(struct msi3101_state
*s
)
827 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
833 /* Both v4l2_lock and vb_queue_lock should be locked when calling this */
834 static int msi3101_isoc_init(struct msi3101_state
*s
)
836 struct usb_device
*udev
;
839 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
844 ret
= usb_set_interface(s
->udev
, 0, 1);
848 /* Allocate and init Isochronuous urbs */
849 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
850 urb
= usb_alloc_urb(ISO_FRAMES_PER_DESC
, GFP_KERNEL
);
852 dev_err(&s
->udev
->dev
,
853 "Failed to allocate urb %d\n", i
);
854 msi3101_isoc_cleanup(s
);
858 dev_dbg(&s
->udev
->dev
, "Allocated URB at 0x%p\n", urb
);
862 urb
->pipe
= usb_rcvisocpipe(udev
, 0x81);
863 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
864 urb
->transfer_buffer
= usb_alloc_coherent(udev
, ISO_BUFFER_SIZE
,
865 GFP_KERNEL
, &urb
->transfer_dma
);
866 if (urb
->transfer_buffer
== NULL
) {
867 dev_err(&s
->udev
->dev
,
868 "Failed to allocate urb buffer %d\n",
870 msi3101_isoc_cleanup(s
);
873 urb
->transfer_buffer_length
= ISO_BUFFER_SIZE
;
874 urb
->complete
= msi3101_isoc_handler
;
876 urb
->start_frame
= 0;
877 urb
->number_of_packets
= ISO_FRAMES_PER_DESC
;
878 for (j
= 0; j
< ISO_FRAMES_PER_DESC
; j
++) {
879 urb
->iso_frame_desc
[j
].offset
= j
* ISO_MAX_FRAME_SIZE
;
880 urb
->iso_frame_desc
[j
].length
= ISO_MAX_FRAME_SIZE
;
885 for (i
= 0; i
< MAX_ISO_BUFS
; i
++) {
886 ret
= usb_submit_urb(s
->urbs
[i
], GFP_KERNEL
);
888 dev_err(&s
->udev
->dev
,
889 "isoc_init() submit_urb %d failed with error %d\n",
891 msi3101_isoc_cleanup(s
);
894 dev_dbg(&s
->udev
->dev
, "URB 0x%p submitted.\n", s
->urbs
[i
]);
901 /* Must be called with vb_queue_lock hold */
902 static void msi3101_cleanup_queued_bufs(struct msi3101_state
*s
)
904 unsigned long flags
= 0;
905 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
907 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
908 while (!list_empty(&s
->queued_bufs
)) {
909 struct msi3101_frame_buf
*buf
;
911 buf
= list_entry(s
->queued_bufs
.next
, struct msi3101_frame_buf
,
913 list_del(&buf
->list
);
914 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
916 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
919 /* The user yanked out the cable... */
920 static void msi3101_disconnect(struct usb_interface
*intf
)
922 struct v4l2_device
*v
= usb_get_intfdata(intf
);
923 struct msi3101_state
*s
=
924 container_of(v
, struct msi3101_state
, v4l2_dev
);
925 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
927 mutex_lock(&s
->vb_queue_lock
);
928 mutex_lock(&s
->v4l2_lock
);
929 /* No need to keep the urbs around after disconnection */
932 v4l2_device_disconnect(&s
->v4l2_dev
);
933 video_unregister_device(&s
->vdev
);
934 mutex_unlock(&s
->v4l2_lock
);
935 mutex_unlock(&s
->vb_queue_lock
);
937 v4l2_device_put(&s
->v4l2_dev
);
940 static int msi3101_querycap(struct file
*file
, void *fh
,
941 struct v4l2_capability
*cap
)
943 struct msi3101_state
*s
= video_drvdata(file
);
944 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
946 strlcpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
947 strlcpy(cap
->card
, s
->vdev
.name
, sizeof(cap
->card
));
948 usb_make_path(s
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
949 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
951 cap
->device_caps
= V4L2_CAP_TUNER
;
952 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
957 /* Videobuf2 operations */
958 static int msi3101_queue_setup(struct vb2_queue
*vq
,
959 const struct v4l2_format
*fmt
, unsigned int *nbuffers
,
960 unsigned int *nplanes
, unsigned int sizes
[], void *alloc_ctxs
[])
962 struct msi3101_state
*s
= vb2_get_drv_priv(vq
);
963 dev_dbg(&s
->udev
->dev
, "%s: *nbuffers=%d\n", __func__
, *nbuffers
);
965 /* Absolute min and max number of buffers available for mmap() */
968 sizes
[0] = PAGE_ALIGN(3 * 3072); /* 3 * 768 * 4 */
969 dev_dbg(&s
->udev
->dev
, "%s: nbuffers=%d sizes[0]=%d\n",
970 __func__
, *nbuffers
, sizes
[0]);
974 static int msi3101_buf_init(struct vb2_buffer
*vb
)
976 struct msi3101_state
*s
= vb2_get_drv_priv(vb
->vb2_queue
);
977 struct msi3101_frame_buf
*fbuf
=
978 container_of(vb
, struct msi3101_frame_buf
, vb
);
979 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
981 fbuf
->data
= vzalloc(ISO_MAX_FRAME_SIZE
);
982 if (fbuf
->data
== NULL
)
988 static int msi3101_buf_prepare(struct vb2_buffer
*vb
)
990 struct msi3101_state
*s
= vb2_get_drv_priv(vb
->vb2_queue
);
992 /* Don't allow queing new buffers after device disconnection */
999 #ifdef MSI3101_CONVERT_IN_URB_HANDLER
1000 static int msi3101_buf_finish(struct vb2_buffer
*vb
)
1005 static int msi3101_buf_finish(struct vb2_buffer
*vb
)
1007 struct msi3101_state
*s
= vb2_get_drv_priv(vb
->vb2_queue
);
1008 struct msi3101_frame_buf
*fbuf
=
1009 container_of(vb
, struct msi3101_frame_buf
, vb
);
1011 u32
*dst
= vb2_plane_vaddr(&fbuf
->vb
, 0);
1012 ret
= msi3101_convert_stream_384(s
, dst
, fbuf
->data
, fbuf
->filled
);
1013 vb2_set_plane_payload(&fbuf
->vb
, 0, ret
);
1018 static void msi3101_buf_cleanup(struct vb2_buffer
*vb
)
1020 struct msi3101_state
*s
= vb2_get_drv_priv(vb
->vb2_queue
);
1021 struct msi3101_frame_buf
*buf
=
1022 container_of(vb
, struct msi3101_frame_buf
, vb
);
1023 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
1027 static void msi3101_buf_queue(struct vb2_buffer
*vb
)
1029 struct msi3101_state
*s
= vb2_get_drv_priv(vb
->vb2_queue
);
1030 struct msi3101_frame_buf
*buf
=
1031 container_of(vb
, struct msi3101_frame_buf
, vb
);
1032 unsigned long flags
= 0;
1034 /* Check the device has not disconnected between prep and queuing */
1036 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
1040 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
1041 list_add_tail(&buf
->list
, &s
->queued_bufs
);
1042 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
1045 #define CMD_WREG 0x41
1046 #define CMD_START_STREAMING 0x43
1047 #define CMD_STOP_STREAMING 0x45
1048 #define CMD_READ_UNKNOW 0x48
1050 #define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \
1052 if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \
1053 direction = ">>>"; \
1055 direction = "<<<"; \
1056 dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \
1057 "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \
1058 _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \
1061 static int msi3101_ctrl_msg(struct msi3101_state
*s
, u8 cmd
, u32 data
)
1065 u8 requesttype
= USB_DIR_OUT
| USB_TYPE_VENDOR
;
1066 u16 value
= (data
>> 0) & 0xffff;
1067 u16 index
= (data
>> 16) & 0xffff;
1069 msi3101_dbg_usb_control_msg(s
->udev
,
1070 request
, requesttype
, value
, index
, NULL
, 0);
1072 ret
= usb_control_msg(s
->udev
, usb_sndctrlpipe(s
->udev
, 0),
1073 request
, requesttype
, value
, index
, NULL
, 0, 2000);
1076 dev_err(&s
->udev
->dev
, "%s: failed %d, cmd %02x, data %04x\n",
1077 __func__
, ret
, cmd
, data
);
1082 static int msi3101_tuner_write(struct msi3101_state
*s
, u32 data
)
1084 return msi3101_ctrl_msg(s
, CMD_WREG
, data
<< 8 | 0x09);
1087 #define F_REF 24000000
1089 static int msi3101_set_usb_adc(struct msi3101_state
*s
)
1091 int ret
, div_n
, div_m
, div_r_out
, f_sr
, f_vco
, fract
;
1092 u32 reg3
, reg4
, reg7
;
1094 f_sr
= s
->ctrl_sampling_rate
->val64
;
1096 /* select stream format */
1097 if (f_sr
< 6000000) {
1098 s
->convert_stream
= msi3101_convert_stream_252
;
1101 s
->convert_stream
= msi3101_convert_stream_384
;
1106 * Synthesizer config is just a educated guess...
1108 * [7:0] 0x03, register address
1111 * [12:10] output divider
1114 * [15] fractional MSB, bit 20
1130 * VCO 202000000 - 720000000++
1135 for (div_r_out
= 4; div_r_out
< 16; div_r_out
+= 2) {
1136 f_vco
= f_sr
* div_r_out
* 12;
1137 dev_dbg(&s
->udev
->dev
, "%s: div_r_out=%d f_vco=%d\n",
1138 __func__
, div_r_out
, f_vco
);
1139 if (f_vco
>= 202000000)
1143 div_n
= f_vco
/ (F_REF
* DIV_R_IN
);
1144 div_m
= f_vco
% (F_REF
* DIV_R_IN
);
1145 fract
= 0x200000ul
* div_m
/ (F_REF
* DIV_R_IN
);
1147 reg3
|= div_n
<< 16;
1148 reg3
|= (div_r_out
/ 2 - 1) << 10;
1149 reg3
|= ((fract
>> 20) & 0x000001) << 15; /* [20] */
1150 reg4
|= ((fract
>> 0) & 0x0fffff) << 8; /* [19:0] */
1152 dev_dbg(&s
->udev
->dev
,
1153 "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n",
1154 __func__
, f_sr
, f_vco
, div_n
, div_m
, div_r_out
, reg3
, reg4
);
1156 ret
= msi3101_ctrl_msg(s
, CMD_WREG
, 0x00608008);
1160 ret
= msi3101_ctrl_msg(s
, CMD_WREG
, 0x00000c05);
1164 ret
= msi3101_ctrl_msg(s
, CMD_WREG
, 0x00020000);
1168 ret
= msi3101_ctrl_msg(s
, CMD_WREG
, 0x00480102);
1172 ret
= msi3101_ctrl_msg(s
, CMD_WREG
, 0x00f38008);
1176 ret
= msi3101_ctrl_msg(s
, CMD_WREG
, reg7
);
1180 ret
= msi3101_ctrl_msg(s
, CMD_WREG
, reg4
);
1184 ret
= msi3101_ctrl_msg(s
, CMD_WREG
, reg3
);
1191 static int msi3101_set_tuner(struct msi3101_state
*s
)
1194 unsigned int n
, m
, thresh
, frac
, vco_step
, tmp
;
1198 const struct msi3101_gain
*gain_lut
;
1199 static const struct {
1204 { 30000000, 0x01, 16}, /* AM_MODE1 */
1205 {108000000, 0x02, 32}, /* VHF_MODE */
1206 {240000000, 0x04, 16}, /* B3_MODE */
1207 {960000000, 0x08, 4}, /* B45_MODE */
1208 {167500000, 0x10, 2}, /* BL_MODE */
1210 static const struct {
1214 { 0, 0x03}, /* Zero IF */
1215 { 450000, 0x02}, /* 450 kHz IF */
1216 {1620000, 0x01}, /* 1.62 MHz IF */
1217 {2048000, 0x00}, /* 2.048 MHz IF */
1219 static const struct {
1222 } bandwidth_lut
[] = {
1223 { 200000, 0x00}, /* 200 kHz */
1224 { 300000, 0x01}, /* 300 kHz */
1225 { 600000, 0x02}, /* 600 kHz */
1226 {1536000, 0x03}, /* 1.536 MHz */
1227 {5000000, 0x04}, /* 5 MHz */
1228 {6000000, 0x05}, /* 6 MHz */
1229 {7000000, 0x06}, /* 7 MHz */
1230 {8000000, 0x07}, /* 8 MHz */
1233 unsigned int rf_freq
= s
->ctrl_tuner_rf
->val64
;
1237 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
1239 int bandwidth
= s
->ctrl_tuner_bw
->val
;
1242 * intermediate frequency (Hz)
1243 * 0, 450000, 1620000, 2048000
1245 int if_freq
= s
->ctrl_tuner_if
->val
;
1248 * gain reduction (dB)
1249 * 0 - 102 below 420 MHz
1250 * 0 - 85 above 420 MHz
1252 int gain
= s
->ctrl_tuner_gain
->val
;
1254 dev_dbg(&s
->udev
->dev
,
1255 "%s: rf_freq=%d bandwidth=%d if_freq=%d gain=%d\n",
1256 __func__
, rf_freq
, bandwidth
, if_freq
, gain
);
1260 for (i
= 0; i
< ARRAY_SIZE(band_lut
); i
++) {
1261 if (rf_freq
<= band_lut
[i
].rf
) {
1262 mode
= band_lut
[i
].mode
;
1263 lo_div
= band_lut
[i
].lo_div
;
1268 if (i
== ARRAY_SIZE(band_lut
))
1271 for (i
= 0; i
< ARRAY_SIZE(if_freq_lut
); i
++) {
1272 if (if_freq
== if_freq_lut
[i
].freq
) {
1273 if_freq
= if_freq_lut
[i
].val
;
1278 if (i
== ARRAY_SIZE(if_freq_lut
))
1281 for (i
= 0; i
< ARRAY_SIZE(bandwidth_lut
); i
++) {
1282 if (bandwidth
== bandwidth_lut
[i
].freq
) {
1283 bandwidth
= bandwidth_lut
[i
].val
;
1288 if (i
== ARRAY_SIZE(bandwidth_lut
))
1291 #define F_OUT_STEP 1
1294 f_vco
= (rf_freq
+ F_IF
) * lo_div
;
1295 n
= f_vco
/ (F_REF
* R_REF
);
1296 m
= f_vco
% (F_REF
* R_REF
);
1298 vco_step
= F_OUT_STEP
* lo_div
;
1299 thresh
= (F_REF
* R_REF
) / vco_step
;
1300 frac
= 1ul * thresh
* m
/ (F_REF
* R_REF
);
1302 /* Divide to reg max. After that RF resolution will be +-500Hz. */
1303 tmp
= DIV_ROUND_UP(thresh
, 4095);
1304 thresh
= DIV_ROUND_CLOSEST(thresh
, tmp
);
1305 frac
= DIV_ROUND_CLOSEST(frac
, tmp
);
1307 /* calc real RF set */
1308 tmp
= 1ul * F_REF
* R_REF
* n
;
1309 tmp
+= 1ul * F_REF
* R_REF
* frac
/ thresh
;
1312 dev_dbg(&s
->udev
->dev
,
1313 "%s: rf=%u:%u n=%d thresh=%d frac=%d\n",
1314 __func__
, rf_freq
, tmp
, n
, thresh
, frac
);
1316 ret
= msi3101_tuner_write(s
, 0x00000e);
1317 ret
= msi3101_tuner_write(s
, 0x000003);
1322 reg
|= if_freq
<< 12;
1323 reg
|= bandwidth
<< 14;
1326 ret
= msi3101_tuner_write(s
, reg
);
1334 ret
= msi3101_tuner_write(s
, reg
);
1341 ret
= msi3101_tuner_write(s
, reg
);
1345 if (rf_freq
< 120000000) {
1346 gain_lut
= msi3101_gain_lut_120
;
1347 len
= ARRAY_SIZE(msi3101_gain_lut_120
);
1348 } else if (rf_freq
< 245000000) {
1349 gain_lut
= msi3101_gain_lut_245
;
1350 len
= ARRAY_SIZE(msi3101_gain_lut_120
);
1352 gain_lut
= msi3101_gain_lut_1000
;
1353 len
= ARRAY_SIZE(msi3101_gain_lut_1000
);
1356 for (i
= 0; i
< len
; i
++) {
1357 if (gain_lut
[i
].tot
>= gain
)
1364 dev_dbg(&s
->udev
->dev
,
1365 "%s: gain tot=%d baseband=%d lna=%d mixer=%d\n",
1366 __func__
, gain_lut
[i
].tot
, gain_lut
[i
].baseband
,
1367 gain_lut
[i
].lna
, gain_lut
[i
].mixer
);
1370 reg
|= gain_lut
[i
].baseband
<< 4;
1372 reg
|= gain_lut
[i
].mixer
<< 12;
1373 reg
|= gain_lut
[i
].lna
<< 13;
1376 ret
= msi3101_tuner_write(s
, reg
);
1383 ret
= msi3101_tuner_write(s
, reg
);
1389 dev_dbg(&s
->udev
->dev
, "%s: failed %d\n", __func__
, ret
);
1393 static int msi3101_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
1395 struct msi3101_state
*s
= vb2_get_drv_priv(vq
);
1397 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
1402 if (mutex_lock_interruptible(&s
->v4l2_lock
))
1403 return -ERESTARTSYS
;
1405 ret
= msi3101_set_usb_adc(s
);
1407 ret
= msi3101_isoc_init(s
);
1409 msi3101_cleanup_queued_bufs(s
);
1411 ret
= msi3101_ctrl_msg(s
, CMD_START_STREAMING
, 0);
1413 mutex_unlock(&s
->v4l2_lock
);
1418 static int msi3101_stop_streaming(struct vb2_queue
*vq
)
1420 struct msi3101_state
*s
= vb2_get_drv_priv(vq
);
1421 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
1423 if (mutex_lock_interruptible(&s
->v4l2_lock
))
1424 return -ERESTARTSYS
;
1427 msi3101_isoc_cleanup(s
);
1429 msi3101_cleanup_queued_bufs(s
);
1431 /* according to tests, at least 700us delay is required */
1433 msi3101_ctrl_msg(s
, CMD_STOP_STREAMING
, 0);
1435 mutex_unlock(&s
->v4l2_lock
);
1440 static struct vb2_ops msi3101_vb2_ops
= {
1441 .queue_setup
= msi3101_queue_setup
,
1442 .buf_init
= msi3101_buf_init
,
1443 .buf_prepare
= msi3101_buf_prepare
,
1444 .buf_finish
= msi3101_buf_finish
,
1445 .buf_cleanup
= msi3101_buf_cleanup
,
1446 .buf_queue
= msi3101_buf_queue
,
1447 .start_streaming
= msi3101_start_streaming
,
1448 .stop_streaming
= msi3101_stop_streaming
,
1449 .wait_prepare
= vb2_ops_wait_prepare
,
1450 .wait_finish
= vb2_ops_wait_finish
,
1453 static int msi3101_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*i
)
1458 strlcpy(i
->name
, "SDR data", sizeof(i
->name
));
1459 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1464 static int msi3101_g_input(struct file
*file
, void *fh
, unsigned int *i
)
1471 static int msi3101_s_input(struct file
*file
, void *fh
, unsigned int i
)
1473 return i
? -EINVAL
: 0;
1476 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1477 const struct v4l2_tuner
*v
)
1479 struct msi3101_state
*s
= video_drvdata(file
);
1480 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
1485 static int vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*v
)
1487 struct msi3101_state
*s
= video_drvdata(file
);
1488 dev_dbg(&s
->udev
->dev
, "%s:\n", __func__
);
1490 strcpy(v
->name
, "SDR RX");
1491 v
->capability
= V4L2_TUNER_CAP_LOW
;
1496 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1497 const struct v4l2_frequency
*f
)
1499 struct msi3101_state
*s
= video_drvdata(file
);
1500 dev_dbg(&s
->udev
->dev
, "%s: frequency=%u Hz (%d)\n",
1501 __func__
, f
->frequency
* 625U / 10U, f
->frequency
);
1503 return v4l2_ctrl_s_ctrl_int64(s
->ctrl_tuner_rf
,
1504 f
->frequency
* 625U / 10U);
1507 const struct v4l2_ioctl_ops msi3101_ioctl_ops
= {
1508 .vidioc_querycap
= msi3101_querycap
,
1510 .vidioc_enum_input
= msi3101_enum_input
,
1511 .vidioc_g_input
= msi3101_g_input
,
1512 .vidioc_s_input
= msi3101_s_input
,
1514 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1515 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1516 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1517 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1518 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1519 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1521 .vidioc_streamon
= vb2_ioctl_streamon
,
1522 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1524 .vidioc_g_tuner
= vidioc_g_tuner
,
1525 .vidioc_s_tuner
= vidioc_s_tuner
,
1526 .vidioc_s_frequency
= vidioc_s_frequency
,
1528 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1529 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1530 .vidioc_log_status
= v4l2_ctrl_log_status
,
1533 static const struct v4l2_file_operations msi3101_fops
= {
1534 .owner
= THIS_MODULE
,
1535 .open
= v4l2_fh_open
,
1536 .release
= vb2_fop_release
,
1537 .read
= vb2_fop_read
,
1538 .poll
= vb2_fop_poll
,
1539 .mmap
= vb2_fop_mmap
,
1540 .unlocked_ioctl
= video_ioctl2
,
1543 static struct video_device msi3101_template
= {
1544 .name
= "Mirics MSi3101 SDR Dongle",
1545 .release
= video_device_release_empty
,
1546 .fops
= &msi3101_fops
,
1547 .ioctl_ops
= &msi3101_ioctl_ops
,
1550 static int msi3101_s_ctrl(struct v4l2_ctrl
*ctrl
)
1552 struct msi3101_state
*s
=
1553 container_of(ctrl
->handler
, struct msi3101_state
,
1556 dev_dbg(&s
->udev
->dev
,
1557 "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n",
1558 __func__
, ctrl
->id
, ctrl
->name
, ctrl
->val
,
1559 ctrl
->minimum
, ctrl
->maximum
, ctrl
->step
);
1562 case MSI3101_CID_SAMPLING_MODE
:
1563 case MSI3101_CID_SAMPLING_RATE
:
1564 case MSI3101_CID_SAMPLING_RESOLUTION
:
1567 case MSI3101_CID_TUNER_RF
:
1568 case MSI3101_CID_TUNER_BW
:
1569 case MSI3101_CID_TUNER_IF
:
1570 case MSI3101_CID_TUNER_GAIN
:
1571 ret
= msi3101_set_tuner(s
);
1580 static const struct v4l2_ctrl_ops msi3101_ctrl_ops
= {
1581 .s_ctrl
= msi3101_s_ctrl
,
1584 static void msi3101_video_release(struct v4l2_device
*v
)
1586 struct msi3101_state
*s
=
1587 container_of(v
, struct msi3101_state
, v4l2_dev
);
1589 v4l2_ctrl_handler_free(&s
->ctrl_handler
);
1590 v4l2_device_unregister(&s
->v4l2_dev
);
1594 static int msi3101_probe(struct usb_interface
*intf
,
1595 const struct usb_device_id
*id
)
1597 struct usb_device
*udev
= interface_to_usbdev(intf
);
1598 struct msi3101_state
*s
= NULL
;
1600 static const char * const ctrl_sampling_mode_qmenu_strings
[] = {
1601 "Quadrature Sampling",
1604 static const struct v4l2_ctrl_config ctrl_sampling_mode
= {
1605 .ops
= &msi3101_ctrl_ops
,
1606 .id
= MSI3101_CID_SAMPLING_MODE
,
1607 .type
= V4L2_CTRL_TYPE_MENU
,
1608 .flags
= V4L2_CTRL_FLAG_INACTIVE
,
1609 .name
= "Sampling Mode",
1610 .qmenu
= ctrl_sampling_mode_qmenu_strings
,
1612 static const struct v4l2_ctrl_config ctrl_sampling_rate
= {
1613 .ops
= &msi3101_ctrl_ops
,
1614 .id
= MSI3101_CID_SAMPLING_RATE
,
1615 .type
= V4L2_CTRL_TYPE_INTEGER64
,
1616 .name
= "Sampling Rate",
1622 static const struct v4l2_ctrl_config ctrl_sampling_resolution
= {
1623 .ops
= &msi3101_ctrl_ops
,
1624 .id
= MSI3101_CID_SAMPLING_RESOLUTION
,
1625 .type
= V4L2_CTRL_TYPE_INTEGER
,
1626 .flags
= V4L2_CTRL_FLAG_INACTIVE
,
1627 .name
= "Sampling Resolution",
1633 static const struct v4l2_ctrl_config ctrl_tuner_rf
= {
1634 .ops
= &msi3101_ctrl_ops
,
1635 .id
= MSI3101_CID_TUNER_RF
,
1636 .type
= V4L2_CTRL_TYPE_INTEGER64
,
1643 static const struct v4l2_ctrl_config ctrl_tuner_bw
= {
1644 .ops
= &msi3101_ctrl_ops
,
1645 .id
= MSI3101_CID_TUNER_BW
,
1646 .type
= V4L2_CTRL_TYPE_INTEGER
,
1653 static const struct v4l2_ctrl_config ctrl_tuner_if
= {
1654 .ops
= &msi3101_ctrl_ops
,
1655 .id
= MSI3101_CID_TUNER_IF
,
1656 .type
= V4L2_CTRL_TYPE_INTEGER
,
1657 .flags
= V4L2_CTRL_FLAG_INACTIVE
,
1664 static const struct v4l2_ctrl_config ctrl_tuner_gain
= {
1665 .ops
= &msi3101_ctrl_ops
,
1666 .id
= MSI3101_CID_TUNER_GAIN
,
1667 .type
= V4L2_CTRL_TYPE_INTEGER
,
1668 .name
= "Tuner Gain",
1675 s
= kzalloc(sizeof(struct msi3101_state
), GFP_KERNEL
);
1677 pr_err("Could not allocate memory for msi3101_state\n");
1681 mutex_init(&s
->v4l2_lock
);
1682 mutex_init(&s
->vb_queue_lock
);
1683 spin_lock_init(&s
->queued_bufs_lock
);
1684 INIT_LIST_HEAD(&s
->queued_bufs
);
1688 /* Init videobuf2 queue structure */
1689 s
->vb_queue
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1690 s
->vb_queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
1691 s
->vb_queue
.drv_priv
= s
;
1692 s
->vb_queue
.buf_struct_size
= sizeof(struct msi3101_frame_buf
);
1693 s
->vb_queue
.ops
= &msi3101_vb2_ops
;
1694 s
->vb_queue
.mem_ops
= &vb2_vmalloc_memops
;
1695 s
->vb_queue
.timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1696 ret
= vb2_queue_init(&s
->vb_queue
);
1698 dev_err(&s
->udev
->dev
, "Could not initialize vb2 queue\n");
1702 /* Init video_device structure */
1703 s
->vdev
= msi3101_template
;
1704 s
->vdev
.queue
= &s
->vb_queue
;
1705 s
->vdev
.queue
->lock
= &s
->vb_queue_lock
;
1706 set_bit(V4L2_FL_USE_FH_PRIO
, &s
->vdev
.flags
);
1707 video_set_drvdata(&s
->vdev
, s
);
1709 /* Register controls */
1710 v4l2_ctrl_handler_init(&s
->ctrl_handler
, 7);
1711 v4l2_ctrl_new_custom(&s
->ctrl_handler
, &ctrl_sampling_mode
, NULL
);
1712 s
->ctrl_sampling_rate
= v4l2_ctrl_new_custom(&s
->ctrl_handler
, &ctrl_sampling_rate
, NULL
);
1713 v4l2_ctrl_new_custom(&s
->ctrl_handler
, &ctrl_sampling_resolution
, NULL
);
1714 s
->ctrl_tuner_rf
= v4l2_ctrl_new_custom(&s
->ctrl_handler
, &ctrl_tuner_rf
, NULL
);
1715 s
->ctrl_tuner_bw
= v4l2_ctrl_new_custom(&s
->ctrl_handler
, &ctrl_tuner_bw
, NULL
);
1716 s
->ctrl_tuner_if
= v4l2_ctrl_new_custom(&s
->ctrl_handler
, &ctrl_tuner_if
, NULL
);
1717 s
->ctrl_tuner_gain
= v4l2_ctrl_new_custom(&s
->ctrl_handler
, &ctrl_tuner_gain
, NULL
);
1718 if (s
->ctrl_handler
.error
) {
1719 ret
= s
->ctrl_handler
.error
;
1720 dev_err(&s
->udev
->dev
, "Could not initialize controls\n");
1721 goto err_free_controls
;
1724 /* Register the v4l2_device structure */
1725 s
->v4l2_dev
.release
= msi3101_video_release
;
1726 ret
= v4l2_device_register(&intf
->dev
, &s
->v4l2_dev
);
1728 dev_err(&s
->udev
->dev
,
1729 "Failed to register v4l2-device (%d)\n", ret
);
1730 goto err_free_controls
;
1733 s
->v4l2_dev
.ctrl_handler
= &s
->ctrl_handler
;
1734 s
->vdev
.v4l2_dev
= &s
->v4l2_dev
;
1735 s
->vdev
.lock
= &s
->v4l2_lock
;
1737 ret
= video_register_device(&s
->vdev
, VFL_TYPE_GRABBER
, -1);
1739 dev_err(&s
->udev
->dev
,
1740 "Failed to register as video device (%d)\n",
1742 goto err_unregister_v4l2_dev
;
1744 dev_info(&s
->udev
->dev
, "Registered as %s\n",
1745 video_device_node_name(&s
->vdev
));
1749 err_unregister_v4l2_dev
:
1750 v4l2_device_unregister(&s
->v4l2_dev
);
1752 v4l2_ctrl_handler_free(&s
->ctrl_handler
);
1758 /* USB device ID list */
1759 static struct usb_device_id msi3101_id_table
[] = {
1760 { USB_DEVICE(0x1df7, 0x2500) },
1761 { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */
1764 MODULE_DEVICE_TABLE(usb
, msi3101_id_table
);
1766 /* USB subsystem interface */
1767 static struct usb_driver msi3101_driver
= {
1768 .name
= KBUILD_MODNAME
,
1769 .probe
= msi3101_probe
,
1770 .disconnect
= msi3101_disconnect
,
1771 .id_table
= msi3101_id_table
,
1774 module_usb_driver(msi3101_driver
);
1776 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1777 MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1778 MODULE_LICENSE("GPL");