From 1444fbf268c4dd8c77790ea28bf7560b815c5b28 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Wed, 26 Jun 2013 10:37:36 -0300 Subject: [media] staging: lirc: clean error handling in probe() We have reorganized the error handling into a simpler and more canonical format. Additionally we removed extra empty lines, switched to devm_kzalloc(), and substitute 'minor' by 'ret' in the igorplugusb_remote_probe() function. Signed-off-by: Dan Carpenter Signed-off-by: Andy Shevchenko Acked-by: Dan Carpenter Signed-off-by: Dan Carpenter Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/lirc/lirc_igorplugusb.c | 56 +++++++-------------------- 1 file changed, 14 insertions(+), 42 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/lirc/lirc_igorplugusb.c b/drivers/staging/media/lirc/lirc_igorplugusb.c index 2faa391..28c8b0b 100644 --- a/drivers/staging/media/lirc/lirc_igorplugusb.c +++ b/drivers/staging/media/lirc/lirc_igorplugusb.c @@ -240,10 +240,6 @@ static int unregister_from_lirc(struct igorplug *ir) dprintk(DRIVER_NAME "[%d]: calling lirc_unregister_driver\n", devnum); lirc_unregister_driver(d->minor); - kfree(d); - ir->d = NULL; - kfree(ir); - return devnum; } @@ -377,20 +373,16 @@ static int igorplugusb_remote_poll(void *data, struct lirc_buffer *buf) return -ENODATA; } - - static int igorplugusb_remote_probe(struct usb_interface *intf, const struct usb_device_id *id) { - struct usb_device *dev = NULL; + struct usb_device *dev; struct usb_host_interface *idesc = NULL; struct usb_endpoint_descriptor *ep; struct igorplug *ir = NULL; struct lirc_driver *driver = NULL; int devnum, pipe, maxp; - int minor = 0; char buf[63], name[128] = ""; - int mem_failure = 0; int ret; dprintk(DRIVER_NAME ": usb probe called.\n"); @@ -416,24 +408,18 @@ static int igorplugusb_remote_probe(struct usb_interface *intf, dprintk(DRIVER_NAME "[%d]: bytes_in_key=%zu maxp=%d\n", devnum, CODE_LENGTH, maxp); - mem_failure = 0; - ir = kzalloc(sizeof(struct igorplug), GFP_KERNEL); - if (!ir) { - mem_failure = 1; - goto mem_failure_switch; - } - driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL); - if (!driver) { - mem_failure = 2; - goto mem_failure_switch; - } + ir = devm_kzalloc(&intf->dev, sizeof(*ir), GFP_KERNEL); + if (!ir) + return -ENOMEM; + + driver = devm_kzalloc(&intf->dev, sizeof(*driver), GFP_KERNEL); + if (!driver) + return -ENOMEM; ir->buf_in = usb_alloc_coherent(dev, DEVICE_BUFLEN + DEVICE_HEADERLEN, GFP_ATOMIC, &ir->dma_in); - if (!ir->buf_in) { - mem_failure = 3; - goto mem_failure_switch; - } + if (!ir->buf_in) + return -ENOMEM; strcpy(driver->name, DRIVER_NAME " "); driver->minor = -1; @@ -449,27 +435,14 @@ static int igorplugusb_remote_probe(struct usb_interface *intf, driver->dev = &intf->dev; driver->owner = THIS_MODULE; - minor = lirc_register_driver(driver); - if (minor < 0) - mem_failure = 9; - -mem_failure_switch: - - switch (mem_failure) { - case 9: + ret = lirc_register_driver(driver); + if (ret < 0) { usb_free_coherent(dev, DEVICE_BUFLEN + DEVICE_HEADERLEN, ir->buf_in, ir->dma_in); - case 3: - kfree(driver); - case 2: - kfree(ir); - case 1: - printk(DRIVER_NAME "[%d]: out of memory (code=%d)\n", - devnum, mem_failure); - return -ENOMEM; + return ret; } - driver->minor = minor; + driver->minor = ret; ir->d = driver; ir->devnum = devnum; ir->usbdev = dev; @@ -502,7 +475,6 @@ mem_failure_switch: return 0; } - static void igorplugusb_remote_disconnect(struct usb_interface *intf) { struct usb_device *usbdev = interface_to_usbdev(intf); -- cgit v1.1 From 977e444f59ad16eedf66acae4f357011c74da291 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Fri, 5 Jul 2013 00:24:08 -0300 Subject: [media] Mirics MSi3101 SDR Dongle driver It is first Kernel software defined radio receiver driver. That device is sold as a digital television solution. MSi3101 is reference design which consists of two chips: 1) MSi2500 USB ADC 2) MSi001 RF-tuner Driver is put to staging as API is not ready. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/Kconfig | 2 + drivers/staging/media/Makefile | 1 + drivers/staging/media/msi3101/Kconfig | 3 + drivers/staging/media/msi3101/Makefile | 1 + drivers/staging/media/msi3101/sdr-msi3101.c | 1618 +++++++++++++++++++++++++++ 5 files changed, 1625 insertions(+) create mode 100644 drivers/staging/media/msi3101/Kconfig create mode 100644 drivers/staging/media/msi3101/Makefile create mode 100644 drivers/staging/media/msi3101/sdr-msi3101.c (limited to 'drivers/staging') diff --git a/drivers/staging/media/Kconfig b/drivers/staging/media/Kconfig index ae0abc3..46f1e61 100644 --- a/drivers/staging/media/Kconfig +++ b/drivers/staging/media/Kconfig @@ -29,6 +29,8 @@ source "drivers/staging/media/dt3155v4l/Kconfig" source "drivers/staging/media/go7007/Kconfig" +source "drivers/staging/media/msi3101/Kconfig" + source "drivers/staging/media/solo6x10/Kconfig" # Keep LIRC at the end, as it has sub-menus diff --git a/drivers/staging/media/Makefile b/drivers/staging/media/Makefile index 2b97cae..eb7f30b 100644 --- a/drivers/staging/media/Makefile +++ b/drivers/staging/media/Makefile @@ -4,4 +4,5 @@ obj-$(CONFIG_LIRC_STAGING) += lirc/ obj-$(CONFIG_SOLO6X10) += solo6x10/ obj-$(CONFIG_VIDEO_DT3155) += dt3155v4l/ obj-$(CONFIG_VIDEO_GO7007) += go7007/ +obj-$(CONFIG_USB_MSI3101) += msi3101/ obj-$(CONFIG_VIDEO_DM365_VPFE) += davinci_vpfe/ diff --git a/drivers/staging/media/msi3101/Kconfig b/drivers/staging/media/msi3101/Kconfig new file mode 100644 index 0000000..b94a95a --- /dev/null +++ b/drivers/staging/media/msi3101/Kconfig @@ -0,0 +1,3 @@ +config USB_MSI3101 + tristate "Mirics MSi3101 SDR Dongle" + depends on USB && VIDEO_DEV && VIDEO_V4L2 diff --git a/drivers/staging/media/msi3101/Makefile b/drivers/staging/media/msi3101/Makefile new file mode 100644 index 0000000..3730654 --- /dev/null +++ b/drivers/staging/media/msi3101/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_USB_MSI3101) += sdr-msi3101.o diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c new file mode 100644 index 0000000..46fdb6c --- /dev/null +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -0,0 +1,1618 @@ +/* + * Mirics MSi3101 SDR Dongle driver + * + * Copyright (C) 2013 Antti Palosaari + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct msi3101_gain { + u8 tot:7; + u8 baseband:6; + bool lna:1; + bool mixer:1; +}; + +/* 60 – 120 MHz band, lna 24dB, mixer 19dB */ +static const struct msi3101_gain msi3101_gain_lut_120[] = { + { 0, 0, 0, 0}, + { 1, 1, 0, 0}, + { 2, 2, 0, 0}, + { 3, 3, 0, 0}, + { 4, 4, 0, 0}, + { 5, 5, 0, 0}, + { 6, 6, 0, 0}, + { 7, 7, 0, 0}, + { 8, 8, 0, 0}, + { 9, 9, 0, 0}, + { 10, 10, 0, 0}, + { 11, 11, 0, 0}, + { 12, 12, 0, 0}, + { 13, 13, 0, 0}, + { 14, 14, 0, 0}, + { 15, 15, 0, 0}, + { 16, 16, 0, 0}, + { 17, 17, 0, 0}, + { 18, 18, 0, 0}, + { 19, 19, 0, 0}, + { 20, 20, 0, 0}, + { 21, 21, 0, 0}, + { 22, 22, 0, 0}, + { 23, 23, 0, 0}, + { 24, 24, 0, 0}, + { 25, 25, 0, 0}, + { 26, 26, 0, 0}, + { 27, 27, 0, 0}, + { 28, 28, 0, 0}, + { 29, 5, 1, 0}, + { 30, 6, 1, 0}, + { 31, 7, 1, 0}, + { 32, 8, 1, 0}, + { 33, 9, 1, 0}, + { 34, 10, 1, 0}, + { 35, 11, 1, 0}, + { 36, 12, 1, 0}, + { 37, 13, 1, 0}, + { 38, 14, 1, 0}, + { 39, 15, 1, 0}, + { 40, 16, 1, 0}, + { 41, 17, 1, 0}, + { 42, 18, 1, 0}, + { 43, 19, 1, 0}, + { 44, 20, 1, 0}, + { 45, 21, 1, 0}, + { 46, 22, 1, 0}, + { 47, 23, 1, 0}, + { 48, 24, 1, 0}, + { 49, 25, 1, 0}, + { 50, 26, 1, 0}, + { 51, 27, 1, 0}, + { 52, 28, 1, 0}, + { 53, 29, 1, 0}, + { 54, 30, 1, 0}, + { 55, 31, 1, 0}, + { 56, 32, 1, 0}, + { 57, 33, 1, 0}, + { 58, 34, 1, 0}, + { 59, 35, 1, 0}, + { 60, 36, 1, 0}, + { 61, 37, 1, 0}, + { 62, 38, 1, 0}, + { 63, 39, 1, 0}, + { 64, 40, 1, 0}, + { 65, 41, 1, 0}, + { 66, 42, 1, 0}, + { 67, 43, 1, 0}, + { 68, 44, 1, 0}, + { 69, 45, 1, 0}, + { 70, 46, 1, 0}, + { 71, 47, 1, 0}, + { 72, 48, 1, 0}, + { 73, 49, 1, 0}, + { 74, 50, 1, 0}, + { 75, 51, 1, 0}, + { 76, 52, 1, 0}, + { 77, 53, 1, 0}, + { 78, 54, 1, 0}, + { 79, 55, 1, 0}, + { 80, 56, 1, 0}, + { 81, 57, 1, 0}, + { 82, 58, 1, 0}, + { 83, 40, 1, 1}, + { 84, 41, 1, 1}, + { 85, 42, 1, 1}, + { 86, 43, 1, 1}, + { 87, 44, 1, 1}, + { 88, 45, 1, 1}, + { 89, 46, 1, 1}, + { 90, 47, 1, 1}, + { 91, 48, 1, 1}, + { 92, 49, 1, 1}, + { 93, 50, 1, 1}, + { 94, 51, 1, 1}, + { 95, 52, 1, 1}, + { 96, 53, 1, 1}, + { 97, 54, 1, 1}, + { 98, 55, 1, 1}, + { 99, 56, 1, 1}, + {100, 57, 1, 1}, + {101, 58, 1, 1}, + {102, 59, 1, 1}, +}; + +/* 120 – 245 MHz band, lna 24dB, mixer 19dB */ +static const struct msi3101_gain msi3101_gain_lut_245[] = { + { 0, 0, 0, 0}, + { 1, 1, 0, 0}, + { 2, 2, 0, 0}, + { 3, 3, 0, 0}, + { 4, 4, 0, 0}, + { 5, 5, 0, 0}, + { 6, 6, 0, 0}, + { 7, 7, 0, 0}, + { 8, 8, 0, 0}, + { 9, 9, 0, 0}, + { 10, 10, 0, 0}, + { 11, 11, 0, 0}, + { 12, 12, 0, 0}, + { 13, 13, 0, 0}, + { 14, 14, 0, 0}, + { 15, 15, 0, 0}, + { 16, 16, 0, 0}, + { 17, 17, 0, 0}, + { 18, 18, 0, 0}, + { 19, 19, 0, 0}, + { 20, 20, 0, 0}, + { 21, 21, 0, 0}, + { 22, 22, 0, 0}, + { 23, 23, 0, 0}, + { 24, 24, 0, 0}, + { 25, 25, 0, 0}, + { 26, 26, 0, 0}, + { 27, 27, 0, 0}, + { 28, 28, 0, 0}, + { 29, 5, 1, 0}, + { 30, 6, 1, 0}, + { 31, 7, 1, 0}, + { 32, 8, 1, 0}, + { 33, 9, 1, 0}, + { 34, 10, 1, 0}, + { 35, 11, 1, 0}, + { 36, 12, 1, 0}, + { 37, 13, 1, 0}, + { 38, 14, 1, 0}, + { 39, 15, 1, 0}, + { 40, 16, 1, 0}, + { 41, 17, 1, 0}, + { 42, 18, 1, 0}, + { 43, 19, 1, 0}, + { 44, 20, 1, 0}, + { 45, 21, 1, 0}, + { 46, 22, 1, 0}, + { 47, 23, 1, 0}, + { 48, 24, 1, 0}, + { 49, 25, 1, 0}, + { 50, 26, 1, 0}, + { 51, 27, 1, 0}, + { 52, 28, 1, 0}, + { 53, 29, 1, 0}, + { 54, 30, 1, 0}, + { 55, 31, 1, 0}, + { 56, 32, 1, 0}, + { 57, 33, 1, 0}, + { 58, 34, 1, 0}, + { 59, 35, 1, 0}, + { 60, 36, 1, 0}, + { 61, 37, 1, 0}, + { 62, 38, 1, 0}, + { 63, 39, 1, 0}, + { 64, 40, 1, 0}, + { 65, 41, 1, 0}, + { 66, 42, 1, 0}, + { 67, 43, 1, 0}, + { 68, 44, 1, 0}, + { 69, 45, 1, 0}, + { 70, 46, 1, 0}, + { 71, 47, 1, 0}, + { 72, 48, 1, 0}, + { 73, 49, 1, 0}, + { 74, 50, 1, 0}, + { 75, 51, 1, 0}, + { 76, 52, 1, 0}, + { 77, 53, 1, 0}, + { 78, 54, 1, 0}, + { 79, 55, 1, 0}, + { 80, 56, 1, 0}, + { 81, 57, 1, 0}, + { 82, 58, 1, 0}, + { 83, 40, 1, 1}, + { 84, 41, 1, 1}, + { 85, 42, 1, 1}, + { 86, 43, 1, 1}, + { 87, 44, 1, 1}, + { 88, 45, 1, 1}, + { 89, 46, 1, 1}, + { 90, 47, 1, 1}, + { 91, 48, 1, 1}, + { 92, 49, 1, 1}, + { 93, 50, 1, 1}, + { 94, 51, 1, 1}, + { 95, 52, 1, 1}, + { 96, 53, 1, 1}, + { 97, 54, 1, 1}, + { 98, 55, 1, 1}, + { 99, 56, 1, 1}, + {100, 57, 1, 1}, + {101, 58, 1, 1}, + {102, 59, 1, 1}, +}; + +/* 420 – 1000 MHz band, lna 7dB, mixer 19dB */ +static const struct msi3101_gain msi3101_gain_lut_1000[] = { + { 0, 0, 0, 0}, + { 1, 1, 0, 0}, + { 2, 2, 0, 0}, + { 3, 3, 0, 0}, + { 4, 4, 0, 0}, + { 5, 5, 0, 0}, + { 6, 6, 0, 0}, + { 7, 7, 0, 0}, + { 8, 8, 0, 0}, + { 9, 9, 0, 0}, + { 10, 10, 0, 0}, + { 11, 11, 0, 0}, + { 12, 5, 1, 0}, + { 13, 6, 1, 0}, + { 14, 7, 1, 0}, + { 15, 8, 1, 0}, + { 16, 9, 1, 0}, + { 17, 10, 1, 0}, + { 18, 11, 1, 0}, + { 19, 12, 1, 0}, + { 20, 13, 1, 0}, + { 21, 14, 1, 0}, + { 22, 15, 1, 0}, + { 23, 16, 1, 0}, + { 24, 17, 1, 0}, + { 25, 18, 1, 0}, + { 26, 19, 1, 0}, + { 27, 20, 1, 0}, + { 28, 21, 1, 0}, + { 29, 22, 1, 0}, + { 30, 23, 1, 0}, + { 31, 24, 1, 0}, + { 32, 25, 1, 0}, + { 33, 26, 1, 0}, + { 34, 27, 1, 0}, + { 35, 28, 1, 0}, + { 36, 29, 1, 0}, + { 37, 30, 1, 0}, + { 38, 31, 1, 0}, + { 39, 32, 1, 0}, + { 40, 33, 1, 0}, + { 41, 34, 1, 0}, + { 42, 35, 1, 0}, + { 43, 36, 1, 0}, + { 44, 37, 1, 0}, + { 45, 38, 1, 0}, + { 46, 39, 1, 0}, + { 47, 40, 1, 0}, + { 48, 41, 1, 0}, + { 49, 42, 1, 0}, + { 50, 43, 1, 0}, + { 51, 44, 1, 0}, + { 52, 45, 1, 0}, + { 53, 46, 1, 0}, + { 54, 47, 1, 0}, + { 55, 48, 1, 0}, + { 56, 49, 1, 0}, + { 57, 50, 1, 0}, + { 58, 51, 1, 0}, + { 59, 52, 1, 0}, + { 60, 53, 1, 0}, + { 61, 54, 1, 0}, + { 62, 55, 1, 0}, + { 63, 56, 1, 0}, + { 64, 57, 1, 0}, + { 65, 58, 1, 0}, + { 66, 40, 1, 1}, + { 67, 41, 1, 1}, + { 68, 42, 1, 1}, + { 69, 43, 1, 1}, + { 70, 44, 1, 1}, + { 71, 45, 1, 1}, + { 72, 46, 1, 1}, + { 73, 47, 1, 1}, + { 74, 48, 1, 1}, + { 75, 49, 1, 1}, + { 76, 50, 1, 1}, + { 77, 51, 1, 1}, + { 78, 52, 1, 1}, + { 79, 53, 1, 1}, + { 80, 54, 1, 1}, + { 81, 55, 1, 1}, + { 82, 56, 1, 1}, + { 83, 57, 1, 1}, + { 84, 58, 1, 1}, + { 85, 59, 1, 1}, +}; + +/* + * iConfiguration 0 + * bInterfaceNumber 0 + * bAlternateSetting 1 + * bNumEndpoints 1 + * bEndpointAddress 0x81 EP 1 IN + * bmAttributes 1 + * Transfer Type Isochronous + * wMaxPacketSize 0x1400 3x 1024 bytes + * bInterval 1 + */ +#define MAX_ISO_BUFS (8) +#define ISO_FRAMES_PER_DESC (8) +#define ISO_MAX_FRAME_SIZE (3 * 1024) +#define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE) + +#define MAX_ISOC_ERRORS 20 + +#define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 0) +#define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 1) +#define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10) +#define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11) +#define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12) +#define MSI3101_CID_TUNER_GAIN ((V4L2_CID_USER_BASE | 0xf000) + 13) + +/* intermediate buffers with raw data from the USB device */ +struct msi3101_frame_buf { + struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */ + struct list_head list; + void *data; /* raw data from USB device */ + int filled; /* number of bytes filled to *data */ +}; + +struct msi3101_state { + struct video_device vdev; + struct v4l2_device v4l2_dev; + + /* videobuf2 queue and queued buffers list */ + struct vb2_queue vb_queue; + struct list_head queued_bufs; + spinlock_t queued_bufs_lock; /* Protects queued_bufs */ + + /* Note if taking both locks v4l2_lock must always be locked first! */ + struct mutex v4l2_lock; /* Protects everything else */ + struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */ + + /* Pointer to our usb_device, will be NULL after unplug */ + struct usb_device *udev; /* Both mutexes most be hold when setting! */ + + unsigned int isoc_errors; /* number of contiguous ISOC errors */ + unsigned int vb_full; /* vb is full and packets dropped */ + + struct urb *urbs[MAX_ISO_BUFS]; + + /* Controls */ + struct v4l2_ctrl_handler ctrl_handler; + struct v4l2_ctrl *ctrl_sampling_rate; + struct v4l2_ctrl *ctrl_tuner_rf; + struct v4l2_ctrl *ctrl_tuner_bw; + struct v4l2_ctrl *ctrl_tuner_if; + struct v4l2_ctrl *ctrl_tuner_gain; + + u32 symbol_received; /* for track lost packets */ + u32 symbol; /* for symbol rate calc */ + unsigned long jiffies; +}; + +/* Private functions */ +static struct msi3101_frame_buf *msi3101_get_next_fill_buf( + struct msi3101_state *s) +{ + unsigned long flags = 0; + struct msi3101_frame_buf *buf = NULL; + + spin_lock_irqsave(&s->queued_bufs_lock, flags); + if (list_empty(&s->queued_bufs)) + goto leave; + + buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list); + list_del(&buf->list); +leave: + spin_unlock_irqrestore(&s->queued_bufs_lock, flags); + return buf; +} + +/* + * Converts signed 10-bit integer into 32-bit IEEE floating point + * representation. + * Will be exact from 0 to 2^24. Above that, we round towards zero + * as the fractional bits will not fit in a float. (It would be better to + * round towards even as the fpu does, but that is slower.) + */ +#define I2F_FRAC_BITS 23 +#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) +static uint32_t msi3101_int2float(uint32_t x) +{ + uint32_t msb, exponent, fraction, sign; + + /* Zero is special */ + if (!x) + return 0; + + /* Negative / positive value */ + if (x & 0x200) { + x = -x; + x &= 0x3ff; + sign = 1 << 31; + } else { + sign = 0 << 31; + } + + /* Get location of the most significant bit */ + msb = __fls(x); + + /* + * Use a rotate instead of a shift because that works both leftwards + * and rightwards due to the mod(32) behaviour. This means we don't + * need to check to see if we are above 2^24 or not. + */ + fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; + exponent = (127 + msb) << I2F_FRAC_BITS; + + return (fraction + exponent) | sign; +} + +#define MSI3101_CONVERT_IN_URB_HANDLER +#define MSI3101_EXTENSIVE_DEBUG +static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, + const u8 *src, unsigned int src_len) +{ + int i, j, k, l, i_max, dst_len = 0; + u16 sample[4]; +#ifdef MSI3101_EXTENSIVE_DEBUG + u32 symbol[3]; +#endif + /* There could be 1-3 1024 bytes URB frames */ + i_max = src_len / 1024; + for (i = 0; i < i_max; i++) { +#ifdef MSI3101_EXTENSIVE_DEBUG + symbol[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; + if (i == 0 && s->symbol_received != symbol[0]) { + dev_dbg(&s->udev->dev, + "%d symbols lost, %d %08x:%08x\n", + symbol[0] - s->symbol_received, + src_len, s->symbol_received, symbol[0]); + } +#endif + src += 16; + for (j = 0; j < 6; j++) { + for (k = 0; k < 16; k++) { + for (l = 0; l < 10; l += 5) { + sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8; + sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6; + sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4; + sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2; + + *dst++ = msi3101_int2float(sample[0]); + *dst++ = msi3101_int2float(sample[1]); + *dst++ = msi3101_int2float(sample[2]); + *dst++ = msi3101_int2float(sample[3]); + + /* 4 x 32bit float samples */ + dst_len += 4 * 4; + } + src += 10; + } +#ifdef MSI3101_EXTENSIVE_DEBUG + if (memcmp(src, "\xff\xff\xff\xff", 4) && memcmp(src, "\x00\x00\x00\x00", 4)) + dev_dbg_ratelimited(&s->udev->dev, + "padding %*ph\n", 4, src); +#endif + src += 4; + } + src += 24; + } + +#ifdef MSI3101_EXTENSIVE_DEBUG + /* calculate symbol rate and output it in 10 seconds intervals */ + if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { + unsigned long jiffies_now = jiffies; + unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); + unsigned int symbols = symbol[i_max - 1] - s->symbol; + s->jiffies = jiffies_now; + s->symbol = symbol[i_max - 1]; + dev_dbg(&s->udev->dev, + "slen=%d symbols=%u msecs=%lu symbolrate=%lu\n", + src_len, symbols, msecs, + symbols * 1000UL / msecs); + } + + /* last received symbol (symbol = symbol + i * 384) */ + s->symbol_received = symbol[i_max - 1] + 384; +#endif + return dst_len; +} + +/* + * This gets called for the Isochronous pipe (stream). This is done in interrupt + * time, so it has to be fast, not crash, and not stall. Neat. + */ +static void msi3101_isoc_handler(struct urb *urb) +{ + struct msi3101_state *s = (struct msi3101_state *)urb->context; + int i, flen, fstatus; + unsigned char *iso_buf = NULL; + struct msi3101_frame_buf *fbuf; + + if (urb->status == -ENOENT || urb->status == -ECONNRESET || + urb->status == -ESHUTDOWN) { + dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n", + urb, urb->status == -ENOENT ? "" : "a"); + return; + } + + if (urb->status != 0) { + dev_dbg(&s->udev->dev, + "msi3101_isoc_handler() called with status %d\n", + urb->status); + /* Give up after a number of contiguous errors */ + if (++s->isoc_errors > MAX_ISOC_ERRORS) + dev_dbg(&s->udev->dev, + "Too many ISOC errors, bailing out\n"); + goto handler_end; + } else { + /* Reset ISOC error counter. We did get here, after all. */ + s->isoc_errors = 0; + } + + /* Compact data */ + for (i = 0; i < urb->number_of_packets; i++) { + /* Check frame error */ + fstatus = urb->iso_frame_desc[i].status; + if (fstatus) { + dev_dbg(&s->udev->dev, + "frame=%d/%d has error %d skipping\n", + i, urb->number_of_packets, fstatus); + goto skip; + } + + /* Check if that frame contains data */ + flen = urb->iso_frame_desc[i].actual_length; + if (flen == 0) + goto skip; + + iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset; + + /* Get free framebuffer */ + fbuf = msi3101_get_next_fill_buf(s); + if (fbuf == NULL) { + s->vb_full++; + dev_dbg_ratelimited(&s->udev->dev, + "videobuf is full, %d packets dropped\n", + s->vb_full); + goto skip; + } + + /* fill framebuffer */ +#ifdef MSI3101_CONVERT_IN_URB_HANDLER + vb2_set_plane_payload(&fbuf->vb, 0, + msi3101_convert_stream(s, + vb2_plane_vaddr(&fbuf->vb, 0), iso_buf, flen)); +#else + memcpy(fbuf->data, iso_buf, flen); + fbuf->filled = flen; +#endif + vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE); +skip: + ; + } + +handler_end: + i = usb_submit_urb(urb, GFP_ATOMIC); + if (i != 0) + dev_dbg(&s->udev->dev, + "Error (%d) re-submitting urb in msi3101_isoc_handler\n", + i); +} + +static void msi3101_iso_stop(struct msi3101_state *s) +{ + int i; + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + /* Unlinking ISOC buffers one by one */ + for (i = 0; i < MAX_ISO_BUFS; i++) { + if (s->urbs[i]) { + dev_dbg(&s->udev->dev, "Unlinking URB %p\n", + s->urbs[i]); + usb_kill_urb(s->urbs[i]); + } + } +} + +static void msi3101_iso_free(struct msi3101_state *s) +{ + int i; + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + /* Freeing ISOC buffers one by one */ + for (i = 0; i < MAX_ISO_BUFS; i++) { + if (s->urbs[i]) { + dev_dbg(&s->udev->dev, "Freeing URB\n"); + if (s->urbs[i]->transfer_buffer) { + usb_free_coherent(s->udev, + s->urbs[i]->transfer_buffer_length, + s->urbs[i]->transfer_buffer, + s->urbs[i]->transfer_dma); + } + usb_free_urb(s->urbs[i]); + s->urbs[i] = NULL; + } + } +} + +/* Both v4l2_lock and vb_queue_lock should be locked when calling this */ +static void msi3101_isoc_cleanup(struct msi3101_state *s) +{ + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + msi3101_iso_stop(s); + msi3101_iso_free(s); +} + +/* Both v4l2_lock and vb_queue_lock should be locked when calling this */ +static int msi3101_isoc_init(struct msi3101_state *s) +{ + struct usb_device *udev; + struct urb *urb; + int i, j, ret; + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + s->isoc_errors = 0; + udev = s->udev; + + ret = usb_set_interface(s->udev, 0, 1); + if (ret < 0) + return ret; + + /* Allocate and init Isochronuous urbs */ + for (i = 0; i < MAX_ISO_BUFS; i++) { + urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL); + if (urb == NULL) { + dev_err(&s->udev->dev, + "Failed to allocate urb %d\n", i); + msi3101_isoc_cleanup(s); + return -ENOMEM; + } + s->urbs[i] = urb; + dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb); + + urb->interval = 1; + urb->dev = udev; + urb->pipe = usb_rcvisocpipe(udev, 0x81); + urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; + urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE, + GFP_KERNEL, &urb->transfer_dma); + if (urb->transfer_buffer == NULL) { + dev_err(&s->udev->dev, + "Failed to allocate urb buffer %d\n", + i); + msi3101_isoc_cleanup(s); + return -ENOMEM; + } + urb->transfer_buffer_length = ISO_BUFFER_SIZE; + urb->complete = msi3101_isoc_handler; + urb->context = s; + urb->start_frame = 0; + urb->number_of_packets = ISO_FRAMES_PER_DESC; + for (j = 0; j < ISO_FRAMES_PER_DESC; j++) { + urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE; + urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE; + } + } + + /* link */ + for (i = 0; i < MAX_ISO_BUFS; i++) { + ret = usb_submit_urb(s->urbs[i], GFP_KERNEL); + if (ret) { + dev_err(&s->udev->dev, + "isoc_init() submit_urb %d failed with error %d\n", + i, ret); + msi3101_isoc_cleanup(s); + return ret; + } + dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]); + } + + /* All is done... */ + return 0; +} + +/* Must be called with vb_queue_lock hold */ +static void msi3101_cleanup_queued_bufs(struct msi3101_state *s) +{ + unsigned long flags = 0; + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + spin_lock_irqsave(&s->queued_bufs_lock, flags); + while (!list_empty(&s->queued_bufs)) { + struct msi3101_frame_buf *buf; + + buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, + list); + list_del(&buf->list); + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); + } + spin_unlock_irqrestore(&s->queued_bufs_lock, flags); +} + +/* The user yanked out the cable... */ +static void msi3101_disconnect(struct usb_interface *intf) +{ + struct v4l2_device *v = usb_get_intfdata(intf); + struct msi3101_state *s = + container_of(v, struct msi3101_state, v4l2_dev); + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + mutex_lock(&s->vb_queue_lock); + mutex_lock(&s->v4l2_lock); + /* No need to keep the urbs around after disconnection */ + s->udev = NULL; + + v4l2_device_disconnect(&s->v4l2_dev); + video_unregister_device(&s->vdev); + mutex_unlock(&s->v4l2_lock); + mutex_unlock(&s->vb_queue_lock); + + v4l2_device_put(&s->v4l2_dev); +} + +static int msi3101_querycap(struct file *file, void *fh, + struct v4l2_capability *cap) +{ + struct msi3101_state *s = video_drvdata(file); + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); + strlcpy(cap->card, s->vdev.name, sizeof(cap->card)); + usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); + cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | + V4L2_CAP_READWRITE; + cap->device_caps = V4L2_CAP_TUNER; + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; + return 0; +} + + +/* Videobuf2 operations */ +static int msi3101_queue_setup(struct vb2_queue *vq, + const struct v4l2_format *fmt, unsigned int *nbuffers, + unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) +{ + struct msi3101_state *s = vb2_get_drv_priv(vq); + dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers); + + /* Absolute min and max number of buffers available for mmap() */ + *nbuffers = 32; + *nplanes = 1; + sizes[0] = PAGE_ALIGN(3 * 3072); /* 3 * 768 * 4 */ + dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n", + __func__, *nbuffers, sizes[0]); + return 0; +} + +static int msi3101_buf_init(struct vb2_buffer *vb) +{ + struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); + struct msi3101_frame_buf *fbuf = + container_of(vb, struct msi3101_frame_buf, vb); + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + fbuf->data = vzalloc(ISO_MAX_FRAME_SIZE); + if (fbuf->data == NULL) + return -ENOMEM; + + return 0; +} + +static int msi3101_buf_prepare(struct vb2_buffer *vb) +{ + struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); + + /* Don't allow queing new buffers after device disconnection */ + if (!s->udev) + return -ENODEV; + + return 0; +} + +/* + * +=========================================================================== + * | 00-1024 | USB packet + * +=========================================================================== + * | 00- 03 | packet address + * +--------------------------------------------------------------------------- + * | 04- 15 | garbage + * +--------------------------------------------------------------------------- + * | 16- 175 | samples + * +--------------------------------------------------------------------------- + * | 176- 179 | padding + * +--------------------------------------------------------------------------- + * | 180- 339 | samples + * +--------------------------------------------------------------------------- + * | 340- 343 | padding + * +--------------------------------------------------------------------------- + * | 344- 503 | samples + * +--------------------------------------------------------------------------- + * | 504- 507 | padding + * +--------------------------------------------------------------------------- + * | 508- 667 | samples + * +--------------------------------------------------------------------------- + * | 668- 671 | padding + * +--------------------------------------------------------------------------- + * | 672- 831 | samples + * +--------------------------------------------------------------------------- + * | 832- 835 | padding + * +--------------------------------------------------------------------------- + * | 836- 995 | samples + * +--------------------------------------------------------------------------- + * | 996- 999 | padding + * +--------------------------------------------------------------------------- + * | 1000-1024 | garbage + * +--------------------------------------------------------------------------- + * + * bytes 4 - 7 could have some meaning? + * padding is "00 00 00 00" or "ff ff ff ff" + * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes + */ +#ifdef MSI3101_CONVERT_IN_URB_HANDLER +static int msi3101_buf_finish(struct vb2_buffer *vb) +{ + return 0; +} +#else +static int msi3101_buf_finish(struct vb2_buffer *vb) +{ + struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); + struct msi3101_frame_buf *fbuf = + container_of(vb, struct msi3101_frame_buf, vb); + int ret; + u32 *dst = vb2_plane_vaddr(&fbuf->vb, 0); + ret = msi3101_convert_stream(s, dst, fbuf->data, fbuf->filled); + vb2_set_plane_payload(&fbuf->vb, 0, ret); + return 0; +} +#endif + +static void msi3101_buf_cleanup(struct vb2_buffer *vb) +{ + struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); + struct msi3101_frame_buf *buf = + container_of(vb, struct msi3101_frame_buf, vb); + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + vfree(buf->data); +} +static void msi3101_buf_queue(struct vb2_buffer *vb) +{ + struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); + struct msi3101_frame_buf *buf = + container_of(vb, struct msi3101_frame_buf, vb); + unsigned long flags = 0; + + /* Check the device has not disconnected between prep and queuing */ + if (!s->udev) { + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); + return; + } + + spin_lock_irqsave(&s->queued_bufs_lock, flags); + list_add_tail(&buf->list, &s->queued_bufs); + spin_unlock_irqrestore(&s->queued_bufs_lock, flags); +} + +#define CMD_WREG 0x41 +#define CMD_START_STREAMING 0x43 +#define CMD_STOP_STREAMING 0x45 +#define CMD_READ_UNKNOW 0x48 + +#define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \ + char *direction; \ + if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \ + direction = ">>>"; \ + else \ + direction = "<<<"; \ + dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \ + "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \ + _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \ +} + +static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data) +{ + int ret; + u8 request = cmd; + u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR; + u16 value = (data >> 0) & 0xffff; + u16 index = (data >> 16) & 0xffff; + + msi3101_dbg_usb_control_msg(s->udev, + request, requesttype, value, index, NULL, 0); + + ret = usb_control_msg(s->udev, usb_rcvctrlpipe(s->udev, 0), + request, requesttype, value, index, NULL, 0, 2000); + + if (ret) + dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n", + __func__, ret, cmd, data); + + return ret; +}; + +static int msi3101_tuner_write(struct msi3101_state *s, u32 data) +{ + return msi3101_ctrl_msg(s, CMD_WREG, data << 8 | 0x09); +}; + +#define F_REF 24000000 +static int msi3101_set_usb_adc(struct msi3101_state *s) +{ + int ret, div_n, div_m, div_r_out, f_sr; + u32 reg4, reg3; + /* + * FIXME: Synthesizer config is just a educated guess... + * It seems to give reasonable values when N is 5-12 and output + * divider R is 2, which means sampling rates 5-12 Msps in practise. + * + * reg 3 ADC synthesizer config + * [7:0] 0x03, register address + * [8] 1, always + * [9] ? + * [12:10] output divider + * [13] 0 ? + * [14] 0 ? + * [15] increase sr by max fract + * [16:19] N + * [23:20] ? + * [24:31] 0x01 + * + * output divider + * val div + * 0 - (invalid) + * 1 2 + * 2 3 + * 3 4 + * 4 5 + * 5 6 + * 6 7 + * 7 8 + */ + + f_sr = s->ctrl_sampling_rate->val64; + reg3 = 0x01c00303; + + for (div_n = 12; div_n > 5; div_n--) { + if (f_sr >= div_n * 1000000) + break; + } + + reg3 |= div_n << 16; + + for (div_r_out = 2; div_r_out < 8; div_r_out++) { + if (f_sr >= div_n * F_REF / div_r_out / 12) + break; + } + + reg3 |= (div_r_out - 1) << 10; + div_m = f_sr % (div_n * F_REF / div_r_out / 12); + + if (div_m >= 500000) { + reg3 |= 1 << 15; + div_m -= 500000; + } + + reg4 = ((div_m * 0x0ffffful / 500000) << 8) | 0x04; + + dev_dbg(&s->udev->dev, "%s: sr=%d n=%d m=%d r_out=%d reg4=%08x\n", + __func__, f_sr, div_n, div_m, div_r_out, reg4); + + ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008); + if (ret) + goto err; + + ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05); + if (ret) + goto err; + + ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000); + if (ret) + goto err; + + ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102); + if (ret) + goto err; + + ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008); + if (ret) + goto err; + + ret = msi3101_ctrl_msg(s, CMD_WREG, 0x0000a507); + if (ret) + goto err; + + ret = msi3101_ctrl_msg(s, CMD_WREG, reg4); + if (ret) + goto err; + + ret = msi3101_ctrl_msg(s, CMD_WREG, reg3); + if (ret) + goto err; +err: + return ret; +}; + +static int msi3101_set_tuner(struct msi3101_state *s) +{ + int i, ret, len; + u32 reg, synthstep, thresh, n, frac; + u64 fsynth; + u8 mode, lo_div; + const struct msi3101_gain *gain_lut; + static const struct { + u32 rf; + u8 mode; + u8 lo_div; + } band_lut[] = { + { 30000000, 0x01, 16}, /* AM_MODE1 */ + {108000000, 0x02, 32}, /* VHF_MODE */ + {240000000, 0x04, 16}, /* B3_MODE */ + {960000000, 0x08, 4}, /* B45_MODE */ + {167500000, 0x10, 2}, /* BL_MODE */ + }; + static const struct { + u32 freq; + u8 val; + } if_freq_lut[] = { + { 0, 0x03}, /* Zero IF */ + { 450000, 0x02}, /* 450 kHz IF */ + {1620000, 0x01}, /* 1.62 MHz IF */ + {2048000, 0x00}, /* 2.048 MHz IF */ + }; + static const struct { + u32 freq; + u8 val; + } bandwidth_lut[] = { + { 200000, 0x00}, /* 200 kHz */ + { 300000, 0x01}, /* 300 kHz */ + { 600000, 0x02}, /* 600 kHz */ + {1536000, 0x03}, /* 1.536 MHz */ + {5000000, 0x04}, /* 5 MHz */ + {6000000, 0x05}, /* 6 MHz */ + {7000000, 0x06}, /* 7 MHz */ + {8000000, 0x07}, /* 8 MHz */ + }; + + unsigned int rf_freq = s->ctrl_tuner_rf->val64; + + /* + * bandwidth (Hz) + * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000 + */ + int bandwidth = s->ctrl_tuner_bw->val; + + /* + * intermediate frequency (Hz) + * 0, 450000, 1620000, 2048000 + */ + int if_freq = s->ctrl_tuner_if->val; + + /* + * gain reduction (dB) + * 0 - 102 below 420 MHz + * 0 - 85 above 420 MHz + */ + int gain = s->ctrl_tuner_gain->val; + + dev_dbg(&s->udev->dev, + "%s: rf_freq=%d bandwidth=%d if_freq=%d gain=%d\n", + __func__, rf_freq, bandwidth, if_freq, gain); + + ret = -EINVAL; + + for (i = 0; i < ARRAY_SIZE(band_lut); i++) { + if (rf_freq <= band_lut[i].rf) { + mode = band_lut[i].mode; + lo_div = band_lut[i].lo_div; + break; + } + } + + if (i == ARRAY_SIZE(band_lut)) + goto err; + + for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) { + if (if_freq == if_freq_lut[i].freq) { + if_freq = if_freq_lut[i].val; + break; + } + } + + if (i == ARRAY_SIZE(if_freq_lut)) + goto err; + + for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) { + if (bandwidth == bandwidth_lut[i].freq) { + bandwidth = bandwidth_lut[i].val; + break; + } + } + + if (i == ARRAY_SIZE(bandwidth_lut)) + goto err; + + #define FSTEP 10000 + #define FREF1 24000000 + fsynth = (rf_freq + 0) * lo_div; + synthstep = FSTEP * lo_div; + thresh = (FREF1 * 4) / synthstep; + n = fsynth / (FREF1 * 4); + frac = thresh * (fsynth % (FREF1 * 4)) / (FREF1 * 4); + + if (thresh > 4095 || n > 63 || frac > 4095) { + dev_dbg(&s->udev->dev, + "%s: synth setup failed rf=%d thresh=%d n=%d frac=%d\n", + __func__, rf_freq, thresh, n, frac); + ret = -EINVAL; + goto err; + } + + ret = msi3101_tuner_write(s, 0x00000e); + ret = msi3101_tuner_write(s, 0x000003); + + reg = 0 << 0; + reg |= mode << 4; + reg |= 1 << 10; + reg |= if_freq << 12; + reg |= bandwidth << 14; + reg |= 0x02 << 17; + reg |= 0x00 << 20; + ret = msi3101_tuner_write(s, reg); + if (ret) + goto err; + + reg = 5 << 0; + reg |= thresh << 4; + reg |= 1 << 19; + reg |= 1 << 21; + ret = msi3101_tuner_write(s, reg); + if (ret) + goto err; + + reg = 2 << 0; + reg |= frac << 4; + reg |= n << 16; + ret = msi3101_tuner_write(s, reg); + if (ret) + goto err; + + if (rf_freq < 120000000) { + gain_lut = msi3101_gain_lut_120; + len = ARRAY_SIZE(msi3101_gain_lut_120); + } else if (rf_freq < 245000000) { + gain_lut = msi3101_gain_lut_245; + len = ARRAY_SIZE(msi3101_gain_lut_120); + } else { + gain_lut = msi3101_gain_lut_1000; + len = ARRAY_SIZE(msi3101_gain_lut_1000); + } + + for (i = 0; i < len; i++) { + if (gain_lut[i].tot >= gain) + break; + } + + if (i == len) + goto err; + + dev_dbg(&s->udev->dev, + "%s: gain tot=%d baseband=%d lna=%d mixer=%d\n", + __func__, gain_lut[i].tot, gain_lut[i].baseband, + gain_lut[i].lna, gain_lut[i].mixer); + + reg = 1 << 0; + reg |= gain_lut[i].baseband << 4; + reg |= 0 << 10; + reg |= gain_lut[i].mixer << 12; + reg |= gain_lut[i].lna << 13; + reg |= 4 << 14; + reg |= 0 << 17; + ret = msi3101_tuner_write(s, reg); + if (ret) + goto err; + + reg = 6 << 0; + reg |= 63 << 4; + reg |= 4095 << 10; + ret = msi3101_tuner_write(s, reg); + if (ret) + goto err; + + return 0; +err: + dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret); + return ret; +}; + +static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count) +{ + struct msi3101_state *s = vb2_get_drv_priv(vq); + int ret; + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + if (!s->udev) + return -ENODEV; + + if (mutex_lock_interruptible(&s->v4l2_lock)) + return -ERESTARTSYS; + + ret = msi3101_set_usb_adc(s); + + ret = msi3101_isoc_init(s); + if (ret) + msi3101_cleanup_queued_bufs(s); + + ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0); + + mutex_unlock(&s->v4l2_lock); + + return ret; +} + +static int msi3101_stop_streaming(struct vb2_queue *vq) +{ + struct msi3101_state *s = vb2_get_drv_priv(vq); + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + if (mutex_lock_interruptible(&s->v4l2_lock)) + return -ERESTARTSYS; + + msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0); + + if (s->udev) + msi3101_isoc_cleanup(s); + + msi3101_cleanup_queued_bufs(s); + mutex_unlock(&s->v4l2_lock); + + return 0; +} + +static struct vb2_ops msi3101_vb2_ops = { + .queue_setup = msi3101_queue_setup, + .buf_init = msi3101_buf_init, + .buf_prepare = msi3101_buf_prepare, + .buf_finish = msi3101_buf_finish, + .buf_cleanup = msi3101_buf_cleanup, + .buf_queue = msi3101_buf_queue, + .start_streaming = msi3101_start_streaming, + .stop_streaming = msi3101_stop_streaming, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i) +{ + if (i->index != 0) + return -EINVAL; + + strlcpy(i->name, "SDR data", sizeof(i->name)); + i->type = V4L2_INPUT_TYPE_CAMERA; + + return 0; +} + +static int msi3101_g_input(struct file *file, void *fh, unsigned int *i) +{ + *i = 0; + + return 0; +} + +static int msi3101_s_input(struct file *file, void *fh, unsigned int i) +{ + return i ? -EINVAL : 0; +} + +static int vidioc_s_tuner(struct file *file, void *priv, + const struct v4l2_tuner *v) +{ + struct msi3101_state *s = video_drvdata(file); + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + return 0; +} + +static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v) +{ + struct msi3101_state *s = video_drvdata(file); + dev_dbg(&s->udev->dev, "%s:\n", __func__); + + strcpy(v->name, "SDR RX"); + v->capability = V4L2_TUNER_CAP_LOW; + + return 0; +} + +static int vidioc_s_frequency(struct file *file, void *priv, + const struct v4l2_frequency *f) +{ + struct msi3101_state *s = video_drvdata(file); + dev_dbg(&s->udev->dev, "%s: frequency=%u Hz (%d)\n", + __func__, f->frequency * 625U / 10U, f->frequency); + + return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf, + f->frequency * 625U / 10U); +} + +const struct v4l2_ioctl_ops msi3101_ioctl_ops = { + .vidioc_querycap = msi3101_querycap, + + .vidioc_enum_input = msi3101_enum_input, + .vidioc_g_input = msi3101_g_input, + .vidioc_s_input = msi3101_s_input, + + .vidioc_reqbufs = vb2_ioctl_reqbufs, + .vidioc_create_bufs = vb2_ioctl_create_bufs, + .vidioc_prepare_buf = vb2_ioctl_prepare_buf, + .vidioc_querybuf = vb2_ioctl_querybuf, + .vidioc_qbuf = vb2_ioctl_qbuf, + .vidioc_dqbuf = vb2_ioctl_dqbuf, + + .vidioc_streamon = vb2_ioctl_streamon, + .vidioc_streamoff = vb2_ioctl_streamoff, + + .vidioc_g_tuner = vidioc_g_tuner, + .vidioc_s_tuner = vidioc_s_tuner, + .vidioc_s_frequency = vidioc_s_frequency, + + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, + .vidioc_log_status = v4l2_ctrl_log_status, +}; + +static const struct v4l2_file_operations msi3101_fops = { + .owner = THIS_MODULE, + .open = v4l2_fh_open, + .release = vb2_fop_release, + .read = vb2_fop_read, + .poll = vb2_fop_poll, + .mmap = vb2_fop_mmap, + .unlocked_ioctl = video_ioctl2, +}; + +static struct video_device msi3101_template = { + .name = "Mirics MSi3101 SDR Dongle", + .release = video_device_release_empty, + .fops = &msi3101_fops, + .ioctl_ops = &msi3101_ioctl_ops, +}; + +static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct msi3101_state *s = + container_of(ctrl->handler, struct msi3101_state, + ctrl_handler); + int ret; + dev_dbg(&s->udev->dev, + "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n", + __func__, ctrl->id, ctrl->name, ctrl->val, + ctrl->minimum, ctrl->maximum, ctrl->step); + + switch (ctrl->id) { + case MSI3101_CID_SAMPLING_RATE: + case MSI3101_CID_SAMPLING_RESOLUTION: + ret = 0; + break; + case MSI3101_CID_TUNER_RF: + case MSI3101_CID_TUNER_BW: + case MSI3101_CID_TUNER_IF: + case MSI3101_CID_TUNER_GAIN: + ret = msi3101_set_tuner(s); + break; + default: + ret = -EINVAL; + } + + return ret; +} + +static const struct v4l2_ctrl_ops msi3101_ctrl_ops = { + .s_ctrl = msi3101_s_ctrl, +}; + +static void msi3101_video_release(struct v4l2_device *v) +{ + struct msi3101_state *s = + container_of(v, struct msi3101_state, v4l2_dev); + + v4l2_ctrl_handler_free(&s->ctrl_handler); + v4l2_device_unregister(&s->v4l2_dev); + kfree(s); +} + +static int msi3101_probe(struct usb_interface *intf, + const struct usb_device_id *id) +{ + struct usb_device *udev = interface_to_usbdev(intf); + struct msi3101_state *s = NULL; + int ret; + static const struct v4l2_ctrl_config ctrl_sampling_rate = { + .ops = &msi3101_ctrl_ops, + .id = MSI3101_CID_SAMPLING_RATE, + .type = V4L2_CTRL_TYPE_INTEGER64, + .name = "Sampling Rate", + .min = 500000, + .max = 12000000, + .def = 2048000, + .step = 1, + }; + static const struct v4l2_ctrl_config ctrl_sampling_resolution = { + .ops = &msi3101_ctrl_ops, + .id = MSI3101_CID_SAMPLING_RESOLUTION, + .type = V4L2_CTRL_TYPE_INTEGER, + .flags = V4L2_CTRL_FLAG_INACTIVE, + .name = "Sampling Resolution", + .min = 10, + .max = 10, + .def = 10, + .step = 1, + }; + static const struct v4l2_ctrl_config ctrl_tuner_rf = { + .ops = &msi3101_ctrl_ops, + .id = MSI3101_CID_TUNER_RF, + .type = V4L2_CTRL_TYPE_INTEGER64, + .name = "Tuner RF", + .min = 40000000, + .max = 2000000000, + .def = 100000000, + .step = 1, + }; + static const struct v4l2_ctrl_config ctrl_tuner_bw = { + .ops = &msi3101_ctrl_ops, + .id = MSI3101_CID_TUNER_BW, + .type = V4L2_CTRL_TYPE_INTEGER, + .name = "Tuner BW", + .min = 200000, + .max = 8000000, + .def = 600000, + .step = 1, + }; + static const struct v4l2_ctrl_config ctrl_tuner_if = { + .ops = &msi3101_ctrl_ops, + .id = MSI3101_CID_TUNER_IF, + .type = V4L2_CTRL_TYPE_INTEGER, + .flags = V4L2_CTRL_FLAG_INACTIVE, + .name = "Tuner IF", + .min = 0, + .max = 2048000, + .def = 0, + .step = 1, + }; + static const struct v4l2_ctrl_config ctrl_tuner_gain = { + .ops = &msi3101_ctrl_ops, + .id = MSI3101_CID_TUNER_GAIN, + .type = V4L2_CTRL_TYPE_INTEGER, + .name = "Tuner Gain", + .min = 0, + .max = 102, + .def = 0, + .step = 1, + }; + + s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL); + if (s == NULL) { + pr_err("Could not allocate memory for msi3101_state\n"); + return -ENOMEM; + } + + mutex_init(&s->v4l2_lock); + mutex_init(&s->vb_queue_lock); + spin_lock_init(&s->queued_bufs_lock); + INIT_LIST_HEAD(&s->queued_bufs); + + s->udev = udev; + + /* Init videobuf2 queue structure */ + s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; + s->vb_queue.drv_priv = s; + s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf); + s->vb_queue.ops = &msi3101_vb2_ops; + s->vb_queue.mem_ops = &vb2_vmalloc_memops; + s->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; + ret = vb2_queue_init(&s->vb_queue); + if (ret < 0) { + dev_err(&s->udev->dev, "Could not initialize vb2 queue\n"); + goto err_free_mem; + } + + /* Init video_device structure */ + s->vdev = msi3101_template; + s->vdev.queue = &s->vb_queue; + s->vdev.queue->lock = &s->vb_queue_lock; + set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags); + video_set_drvdata(&s->vdev, s); + + /* Register controls */ + v4l2_ctrl_handler_init(&s->ctrl_handler, 6); + s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL); + v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL); + s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL); + s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL); + s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL); + s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL); + if (s->ctrl_handler.error) { + ret = s->ctrl_handler.error; + dev_err(&s->udev->dev, "Could not initialize controls\n"); + goto err_free_controls; + } + + /* Register the v4l2_device structure */ + s->v4l2_dev.release = msi3101_video_release; + ret = v4l2_device_register(&intf->dev, &s->v4l2_dev); + if (ret) { + dev_err(&s->udev->dev, + "Failed to register v4l2-device (%d)\n", ret); + goto err_free_controls; + } + + s->v4l2_dev.ctrl_handler = &s->ctrl_handler; + s->vdev.v4l2_dev = &s->v4l2_dev; + s->vdev.lock = &s->v4l2_lock; + + ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1); + if (ret < 0) { + dev_err(&s->udev->dev, + "Failed to register as video device (%d)\n", + ret); + goto err_unregister_v4l2_dev; + } + dev_info(&s->udev->dev, "Registered as %s\n", + video_device_node_name(&s->vdev)); + + return 0; + +err_unregister_v4l2_dev: + v4l2_device_unregister(&s->v4l2_dev); +err_free_controls: + v4l2_ctrl_handler_free(&s->ctrl_handler); +err_free_mem: + kfree(s); + return ret; +} + +/* USB device ID list */ +static struct usb_device_id msi3101_id_table[] = { + { USB_DEVICE(0x1df7, 0x2500) }, + { } +}; +MODULE_DEVICE_TABLE(usb, msi3101_id_table); + +/* USB subsystem interface */ +static struct usb_driver msi3101_driver = { + .name = KBUILD_MODNAME, + .probe = msi3101_probe, + .disconnect = msi3101_disconnect, + .id_table = msi3101_id_table, +}; + +module_usb_driver(msi3101_driver); + +MODULE_AUTHOR("Antti Palosaari "); +MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle"); +MODULE_LICENSE("GPL"); -- cgit v1.1 From 34599b9b5305eb70644d54fe22e6df4aa5fef65c Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Mon, 8 Jul 2013 22:31:47 -0300 Subject: [media] msi3101: sample is correct term for sample Just fix wrong term name. No functional changes. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 32 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 46fdb6c..87896ee 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -405,8 +405,8 @@ struct msi3101_state { struct v4l2_ctrl *ctrl_tuner_if; struct v4l2_ctrl *ctrl_tuner_gain; - u32 symbol_received; /* for track lost packets */ - u32 symbol; /* for symbol rate calc */ + u32 next_sample; /* for track lost packets */ + u32 sample; /* for sample rate calc */ unsigned long jiffies; }; @@ -476,18 +476,18 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, int i, j, k, l, i_max, dst_len = 0; u16 sample[4]; #ifdef MSI3101_EXTENSIVE_DEBUG - u32 symbol[3]; + u32 sample_num[3]; #endif /* There could be 1-3 1024 bytes URB frames */ i_max = src_len / 1024; for (i = 0; i < i_max; i++) { #ifdef MSI3101_EXTENSIVE_DEBUG - symbol[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; - if (i == 0 && s->symbol_received != symbol[0]) { + sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; + if (i == 0 && s->next_sample != sample_num[0]) { dev_dbg(&s->udev->dev, - "%d symbols lost, %d %08x:%08x\n", - symbol[0] - s->symbol_received, - src_len, s->symbol_received, symbol[0]); + "%d samples lost, %d %08x:%08x\n", + sample_num[0] - s->next_sample, + src_len, s->next_sample, sample_num[0]); } #endif src += 16; @@ -520,21 +520,21 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, } #ifdef MSI3101_EXTENSIVE_DEBUG - /* calculate symbol rate and output it in 10 seconds intervals */ + /* calculate samping rate and output it in 10 seconds intervals */ if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { unsigned long jiffies_now = jiffies; unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); - unsigned int symbols = symbol[i_max - 1] - s->symbol; + unsigned int samples = sample_num[i_max - 1] - s->sample; s->jiffies = jiffies_now; - s->symbol = symbol[i_max - 1]; + s->sample = sample_num[i_max - 1]; dev_dbg(&s->udev->dev, - "slen=%d symbols=%u msecs=%lu symbolrate=%lu\n", - src_len, symbols, msecs, - symbols * 1000UL / msecs); + "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", + src_len, samples, msecs, + samples * 1000UL / msecs); } - /* last received symbol (symbol = symbol + i * 384) */ - s->symbol_received = symbol[i_max - 1] + 384; + /* next sample (sample = sample + i * 384) */ + s->next_sample = sample_num[i_max - 1] + 384; #endif return dst_len; } -- cgit v1.1 From b15208573d926e2fdf46a753ae167235d73e1bf6 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Tue, 9 Jul 2013 02:44:56 -0300 Subject: [media] msi3101: fix sampling rate calculation These calculations seem to give 100% correct results. Calculation formulas could be still a little bit wrong as I have no knowledge what kind of dividers, multipliers and VCO limits there really is. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 58 +++++++++++++++-------------- 1 file changed, 31 insertions(+), 27 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 87896ee..4de4f50 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -960,16 +960,14 @@ static int msi3101_tuner_write(struct msi3101_state *s, u32 data) }; #define F_REF 24000000 +#define DIV_R_IN 2 static int msi3101_set_usb_adc(struct msi3101_state *s) { - int ret, div_n, div_m, div_r_out, f_sr; + int ret, div_n, div_m, div_r_out, f_sr, f_vco; u32 reg4, reg3; /* - * FIXME: Synthesizer config is just a educated guess... - * It seems to give reasonable values when N is 5-12 and output - * divider R is 2, which means sampling rates 5-12 Msps in practise. + * Synthesizer config is just a educated guess... * - * reg 3 ADC synthesizer config * [7:0] 0x03, register address * [8] 1, always * [9] ? @@ -984,42 +982,48 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) * output divider * val div * 0 - (invalid) - * 1 2 - * 2 3 - * 3 4 - * 4 5 - * 5 6 - * 6 7 - * 7 8 + * 1 4 + * 2 6 + * 3 8 + * 4 10 + * 5 12 + * 6 14 + * 7 16 + * + * VCO 202000000 - 720000000++ */ f_sr = s->ctrl_sampling_rate->val64; reg3 = 0x01c00303; - for (div_n = 12; div_n > 5; div_n--) { - if (f_sr >= div_n * 1000000) + for (div_r_out = 4; div_r_out < 16; div_r_out += 2) { + f_vco = f_sr * div_r_out * 12; + dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n", + __func__, div_r_out, f_vco); + if (f_vco >= 202000000) break; } - reg3 |= div_n << 16; - - for (div_r_out = 2; div_r_out < 8; div_r_out++) { - if (f_sr >= div_n * F_REF / div_r_out / 12) - break; - } + div_n = f_vco / (F_REF * DIV_R_IN); + div_m = f_vco % (F_REF * DIV_R_IN); - reg3 |= (div_r_out - 1) << 10; - div_m = f_sr % (div_n * F_REF / div_r_out / 12); + reg3 |= div_n << 16; + reg3 |= (div_r_out / 2 - 1) << 10; + reg4 = 0x0ffffful * div_m / F_REF; - if (div_m >= 500000) { + if (reg4 >= 0x0ffffful) { + dev_dbg(&s->udev->dev, + "%s: extending fractional part value %08x\n", + __func__, reg4); + reg4 -= 0x0ffffful; reg3 |= 1 << 15; - div_m -= 500000; } - reg4 = ((div_m * 0x0ffffful / 500000) << 8) | 0x04; + reg4 = (reg4 << 8) | 0x04; - dev_dbg(&s->udev->dev, "%s: sr=%d n=%d m=%d r_out=%d reg4=%08x\n", - __func__, f_sr, div_n, div_m, div_r_out, reg4); + dev_dbg(&s->udev->dev, + "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg4=%08x\n", + __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg4); ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008); if (ret) -- cgit v1.1 From 3673b35cfaed18a4f6ef2f302333a5e12dbe3d25 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Tue, 9 Jul 2013 16:19:15 -0300 Subject: [media] msi3101: add sampling mode control Whilst Quadrature Sampling is most common sampling technique used in radio technology there is others too. Add control user could select sampling mode. We currently support only quadrature sampling. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 4de4f50..b6a8939 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -361,8 +361,9 @@ static const struct msi3101_gain msi3101_gain_lut_1000[] = { #define MAX_ISOC_ERRORS 20 -#define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 0) -#define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 1) +#define MSI3101_CID_SAMPLING_MODE ((V4L2_CID_USER_BASE | 0xf000) + 0) +#define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 1) +#define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 2) #define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10) #define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11) #define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12) @@ -1418,6 +1419,7 @@ static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl) ctrl->minimum, ctrl->maximum, ctrl->step); switch (ctrl->id) { + case MSI3101_CID_SAMPLING_MODE: case MSI3101_CID_SAMPLING_RATE: case MSI3101_CID_SAMPLING_RESOLUTION: ret = 0; @@ -1455,6 +1457,18 @@ static int msi3101_probe(struct usb_interface *intf, struct usb_device *udev = interface_to_usbdev(intf); struct msi3101_state *s = NULL; int ret; + static const char * const ctrl_sampling_mode_qmenu_strings[] = { + "Quadrature Sampling", + NULL, + }; + static const struct v4l2_ctrl_config ctrl_sampling_mode = { + .ops = &msi3101_ctrl_ops, + .id = MSI3101_CID_SAMPLING_MODE, + .type = V4L2_CTRL_TYPE_MENU, + .flags = V4L2_CTRL_FLAG_INACTIVE, + .name = "Sampling Mode", + .qmenu = ctrl_sampling_mode_qmenu_strings, + }; static const struct v4l2_ctrl_config ctrl_sampling_rate = { .ops = &msi3101_ctrl_ops, .id = MSI3101_CID_SAMPLING_RATE, @@ -1553,7 +1567,8 @@ static int msi3101_probe(struct usb_interface *intf, video_set_drvdata(&s->vdev, s); /* Register controls */ - v4l2_ctrl_handler_init(&s->ctrl_handler, 6); + v4l2_ctrl_handler_init(&s->ctrl_handler, 7); + v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_mode, NULL); s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL); v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL); s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL); -- cgit v1.1 From 020046812f2f53fc952f6ea56accf1599d4b3ba0 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Wed, 10 Jul 2013 21:59:58 -0300 Subject: [media] msi3101: enhance sampling results It looks like there is some extra data carried to enhance sampling results. When you tune to some some weak/empty channel those bits are always zeroes. When you tune to some channel where is very strong signals those bits are all zeroes. Examining those 32-bits reveals shortly there is 16 pieces of 2-bit numbers. Number seen are 0, 1 and 3 - for some reason 2 is not used. I used that number to shift bits given amount to left, "increasing" sampling resolution by 3-bits. It may be wrong, but after some testing it still provides better signal. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 120 ++++++++++++++++------------ 1 file changed, 67 insertions(+), 53 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index b6a8939..c73f1d9 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -409,6 +409,7 @@ struct msi3101_state { u32 next_sample; /* for track lost packets */ u32 sample; /* for sample rate calc */ unsigned long jiffies; + unsigned int sample_ctrl_bit[4]; }; /* Private functions */ @@ -430,6 +431,51 @@ leave: } /* + * +=========================================================================== + * | 00-1024 | USB packet + * +=========================================================================== + * | 00- 03 | sequence number of first sample in that USB packet + * +--------------------------------------------------------------------------- + * | 04- 15 | garbage + * +--------------------------------------------------------------------------- + * | 16- 175 | samples + * +--------------------------------------------------------------------------- + * | 176- 179 | control bits for previous samples + * +--------------------------------------------------------------------------- + * | 180- 339 | samples + * +--------------------------------------------------------------------------- + * | 340- 343 | control bits for previous samples + * +--------------------------------------------------------------------------- + * | 344- 503 | samples + * +--------------------------------------------------------------------------- + * | 504- 507 | control bits for previous samples + * +--------------------------------------------------------------------------- + * | 508- 667 | samples + * +--------------------------------------------------------------------------- + * | 668- 671 | control bits for previous samples + * +--------------------------------------------------------------------------- + * | 672- 831 | samples + * +--------------------------------------------------------------------------- + * | 832- 835 | control bits for previous samples + * +--------------------------------------------------------------------------- + * | 836- 995 | samples + * +--------------------------------------------------------------------------- + * | 996- 999 | control bits for previous samples + * +--------------------------------------------------------------------------- + * | 1000-1024 | garbage + * +--------------------------------------------------------------------------- + * + * Bytes 4 - 7 could have some meaning? + * + * Control bits for previous samples is 32-bit field, containing 16 x 2-bit + * numbers. This results one 2-bit number for 8 samples. It is likely used for + * for bit shifting sample by given bits, increasing actual sampling resolution. + * Number 2 (0b10) was never seen. + * + * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes + */ + +/* * Converts signed 10-bit integer into 32-bit IEEE floating point * representation. * Will be exact from 0 to 2^24. Above that, we round towards zero @@ -438,20 +484,24 @@ leave: */ #define I2F_FRAC_BITS 23 #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) -static uint32_t msi3101_int2float(uint32_t x) +static u32 msi3101_convert_sample(struct msi3101_state *s, u16 x, int shift) { - uint32_t msb, exponent, fraction, sign; + u32 msb, exponent, fraction, sign; + s->sample_ctrl_bit[shift]++; /* Zero is special */ if (!x) return 0; - /* Negative / positive value */ - if (x & 0x200) { + /* Convert 10-bit two's complement to 13-bit */ + if (x & (1 << 9)) { + x |= ~0U << 10; /* set all the rest bits to one */ + x <<= shift; x = -x; - x &= 0x3ff; + x &= 0xfff; /* result is 12 bit ... + sign */ sign = 1 << 31; } else { + x <<= shift; sign = 0 << 31; } @@ -476,6 +526,7 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, { int i, j, k, l, i_max, dst_len = 0; u16 sample[4]; + u32 bits; #ifdef MSI3101_EXTENSIVE_DEBUG u32 sample_num[3]; #endif @@ -493,6 +544,7 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, #endif src += 16; for (j = 0; j < 6; j++) { + bits = src[160 + 3] << 24 | src[160 + 2] << 16 | src[160 + 1] << 8 | src[160 + 0] << 0; for (k = 0; k < 16; k++) { for (l = 0; l < 10; l += 5) { sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8; @@ -500,10 +552,10 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4; sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2; - *dst++ = msi3101_int2float(sample[0]); - *dst++ = msi3101_int2float(sample[1]); - *dst++ = msi3101_int2float(sample[2]); - *dst++ = msi3101_int2float(sample[3]); + *dst++ = msi3101_convert_sample(s, sample[0], (bits >> (2 * k)) & 0x3); + *dst++ = msi3101_convert_sample(s, sample[1], (bits >> (2 * k)) & 0x3); + *dst++ = msi3101_convert_sample(s, sample[2], (bits >> (2 * k)) & 0x3); + *dst++ = msi3101_convert_sample(s, sample[3], (bits >> (2 * k)) & 0x3); /* 4 x 32bit float samples */ dst_len += 4 * 4; @@ -511,9 +563,8 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, src += 10; } #ifdef MSI3101_EXTENSIVE_DEBUG - if (memcmp(src, "\xff\xff\xff\xff", 4) && memcmp(src, "\x00\x00\x00\x00", 4)) - dev_dbg_ratelimited(&s->udev->dev, - "padding %*ph\n", 4, src); + dev_dbg_ratelimited(&s->udev->dev, + "sample control bits %08x\n", bits); #endif src += 4; } @@ -529,9 +580,11 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, s->jiffies = jiffies_now; s->sample = sample_num[i_max - 1]; dev_dbg(&s->udev->dev, - "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", + "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n", src_len, samples, msecs, - samples * 1000UL / msecs); + samples * 1000UL / msecs, + s->sample_ctrl_bit[0], s->sample_ctrl_bit[1], + s->sample_ctrl_bit[2], s->sample_ctrl_bit[3]); } /* next sample (sample = sample + i * 384) */ @@ -833,45 +886,6 @@ static int msi3101_buf_prepare(struct vb2_buffer *vb) return 0; } -/* - * +=========================================================================== - * | 00-1024 | USB packet - * +=========================================================================== - * | 00- 03 | packet address - * +--------------------------------------------------------------------------- - * | 04- 15 | garbage - * +--------------------------------------------------------------------------- - * | 16- 175 | samples - * +--------------------------------------------------------------------------- - * | 176- 179 | padding - * +--------------------------------------------------------------------------- - * | 180- 339 | samples - * +--------------------------------------------------------------------------- - * | 340- 343 | padding - * +--------------------------------------------------------------------------- - * | 344- 503 | samples - * +--------------------------------------------------------------------------- - * | 504- 507 | padding - * +--------------------------------------------------------------------------- - * | 508- 667 | samples - * +--------------------------------------------------------------------------- - * | 668- 671 | padding - * +--------------------------------------------------------------------------- - * | 672- 831 | samples - * +--------------------------------------------------------------------------- - * | 832- 835 | padding - * +--------------------------------------------------------------------------- - * | 836- 995 | samples - * +--------------------------------------------------------------------------- - * | 996- 999 | padding - * +--------------------------------------------------------------------------- - * | 1000-1024 | garbage - * +--------------------------------------------------------------------------- - * - * bytes 4 - 7 could have some meaning? - * padding is "00 00 00 00" or "ff ff ff ff" - * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes - */ #ifdef MSI3101_CONVERT_IN_URB_HANDLER static int msi3101_buf_finish(struct vb2_buffer *vb) { -- cgit v1.1 From d0fadf40338fc551f4bbb82fc4c223d2a8de2c1d Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Thu, 11 Jul 2013 12:19:32 -0300 Subject: [media] msi3101: fix stream re-start halt Restarting stream fails quite often. Small delay is between urb killing and stream stop command - likely to give harware some time to process killed urbs. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index c73f1d9..2180bf8 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -959,7 +959,7 @@ static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data) msi3101_dbg_usb_control_msg(s->udev, request, requesttype, value, index, NULL, 0); - ret = usb_control_msg(s->udev, usb_rcvctrlpipe(s->udev, 0), + ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0), request, requesttype, value, index, NULL, 0, 2000); if (ret) @@ -1300,12 +1300,15 @@ static int msi3101_stop_streaming(struct vb2_queue *vq) if (mutex_lock_interruptible(&s->v4l2_lock)) return -ERESTARTSYS; - msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0); - if (s->udev) msi3101_isoc_cleanup(s); msi3101_cleanup_queued_bufs(s); + + /* according to tests, at least 700us delay is required */ + msleep(20); + msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0); + mutex_unlock(&s->v4l2_lock); return 0; -- cgit v1.1 From 42fc5b42300b0795d418ba567ae6a7fb4504d022 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Wed, 17 Jul 2013 15:05:27 -0300 Subject: [media] msi3101: add 2040:d300 Hauppauge WinTV 133559 LF It is based Mirics MSi3101 reference design and will just work without any changes. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 2180bf8..152415a 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -1635,6 +1635,7 @@ err_free_mem: /* USB device ID list */ static struct usb_device_id msi3101_id_table[] = { { USB_DEVICE(0x1df7, 0x2500) }, + { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */ { } }; MODULE_DEVICE_TABLE(usb, msi3101_id_table); -- cgit v1.1 From 6450e5091c6dfc12afc164e8225b7677b1b1a422 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Wed, 31 Jul 2013 19:58:12 -0300 Subject: [media] msi3101: add debug dump for unknown stream data Dump all unknown 'garbage' data - maybe we will discover someday if there is something rational... Also fix comment in USB frame description. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 152415a..2b73fc1 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -432,7 +432,7 @@ leave: /* * +=========================================================================== - * | 00-1024 | USB packet + * | 00-1023 | USB packet * +=========================================================================== * | 00- 03 | sequence number of first sample in that USB packet * +--------------------------------------------------------------------------- @@ -462,7 +462,7 @@ leave: * +--------------------------------------------------------------------------- * | 996- 999 | control bits for previous samples * +--------------------------------------------------------------------------- - * | 1000-1024 | garbage + * | 1000-1023 | garbage * +--------------------------------------------------------------------------- * * Bytes 4 - 7 could have some meaning? @@ -522,7 +522,7 @@ static u32 msi3101_convert_sample(struct msi3101_state *s, u16 x, int shift) #define MSI3101_CONVERT_IN_URB_HANDLER #define MSI3101_EXTENSIVE_DEBUG static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, - const u8 *src, unsigned int src_len) + u8 *src, unsigned int src_len) { int i, j, k, l, i_max, dst_len = 0; u16 sample[4]; @@ -541,6 +541,15 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, sample_num[0] - s->next_sample, src_len, s->next_sample, sample_num[0]); } + + /* + * Dump all unknown 'garbage' data - maybe we will discover + * someday if there is something rational... + */ + dev_dbg_ratelimited(&s->udev->dev, + "%*ph %*ph\n", 12, &src[4], 24, &src[1000]); + memset(&src[4], 0, 12); + memset(&src[1000], 0, 24); #endif src += 16; for (j = 0; j < 6; j++) { -- cgit v1.1 From 22ca680e02aceb88d939b1eb1bbe3e94b71bf555 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Wed, 31 Jul 2013 21:48:51 -0300 Subject: [media] msi3101: correct ADC sampling rate calc a little bit No need to compare numbers, we could just store that fractional value MSB directly. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 2b73fc1..04bbbdf 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -987,7 +987,7 @@ static int msi3101_tuner_write(struct msi3101_state *s, u32 data) #define DIV_R_IN 2 static int msi3101_set_usb_adc(struct msi3101_state *s) { - int ret, div_n, div_m, div_r_out, f_sr, f_vco; + int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract; u32 reg4, reg3; /* * Synthesizer config is just a educated guess... @@ -998,7 +998,7 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) * [12:10] output divider * [13] 0 ? * [14] 0 ? - * [15] increase sr by max fract + * [15] fractional MSB, bit 20 * [16:19] N * [23:20] ? * [24:31] 0x01 @@ -1019,6 +1019,7 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) f_sr = s->ctrl_sampling_rate->val64; reg3 = 0x01c00303; + reg4 = 0x00000004; for (div_r_out = 4; div_r_out < 16; div_r_out += 2) { f_vco = f_sr * div_r_out * 12; @@ -1030,24 +1031,16 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) div_n = f_vco / (F_REF * DIV_R_IN); div_m = f_vco % (F_REF * DIV_R_IN); + fract = 0x200000ul * div_m / (F_REF * DIV_R_IN); reg3 |= div_n << 16; reg3 |= (div_r_out / 2 - 1) << 10; - reg4 = 0x0ffffful * div_m / F_REF; - - if (reg4 >= 0x0ffffful) { - dev_dbg(&s->udev->dev, - "%s: extending fractional part value %08x\n", - __func__, reg4); - reg4 -= 0x0ffffful; - reg3 |= 1 << 15; - } - - reg4 = (reg4 << 8) | 0x04; + reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */ + reg4 |= ((fract >> 0) & 0x0fffff) << 8; /* [19:0] */ dev_dbg(&s->udev->dev, - "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg4=%08x\n", - __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg4); + "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n", + __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg3, reg4); ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008); if (ret) -- cgit v1.1 From 23df427e3333882beea2e65d27fd2a581e37fae4 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Fri, 2 Aug 2013 21:49:49 -0300 Subject: [media] msi3101: improve tuner synth calc step size Allow stepless synthesizer configuration. With that change we lose precision a little bit, as it is now between +-500Hz from the target. It could be better but on that case calculation algorithm goes more complex and atm there is more important things to do. Two approach to improve which comes to my mind are: 1) select and use biggest suitable step 2) use greatest common divisor algo to find divisor for thresh & frac when possible to avoid rounding errors, which is root of cause of current +-500Hz inaccuracy. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 44 ++++++++++++++++++----------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 04bbbdf..4ff6030 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -1079,9 +1079,10 @@ err: static int msi3101_set_tuner(struct msi3101_state *s) { - int i, ret, len; - u32 reg, synthstep, thresh, n, frac; - u64 fsynth; + int ret, i, len; + unsigned int n, m, thresh, frac, vco_step, tmp; + u32 reg; + u64 f_vco; u8 mode, lo_div; const struct msi3101_gain *gain_lut; static const struct { @@ -1176,21 +1177,30 @@ static int msi3101_set_tuner(struct msi3101_state *s) if (i == ARRAY_SIZE(bandwidth_lut)) goto err; - #define FSTEP 10000 - #define FREF1 24000000 - fsynth = (rf_freq + 0) * lo_div; - synthstep = FSTEP * lo_div; - thresh = (FREF1 * 4) / synthstep; - n = fsynth / (FREF1 * 4); - frac = thresh * (fsynth % (FREF1 * 4)) / (FREF1 * 4); +#define F_OUT_STEP 1 +#define R_REF 4 +#define F_IF 0 + f_vco = (rf_freq + F_IF) * lo_div; + n = f_vco / (F_REF * R_REF); + m = f_vco % (F_REF * R_REF); - if (thresh > 4095 || n > 63 || frac > 4095) { - dev_dbg(&s->udev->dev, - "%s: synth setup failed rf=%d thresh=%d n=%d frac=%d\n", - __func__, rf_freq, thresh, n, frac); - ret = -EINVAL; - goto err; - } + vco_step = F_OUT_STEP * lo_div; + thresh = (F_REF * R_REF) / vco_step; + frac = 1ul * thresh * m / (F_REF * R_REF); + + /* Divide to reg max. After that RF resolution will be +-500Hz. */ + tmp = DIV_ROUND_UP(thresh, 4095); + thresh = DIV_ROUND_CLOSEST(thresh, tmp); + frac = DIV_ROUND_CLOSEST(frac, tmp); + + /* calc real RF set */ + tmp = 1ul * F_REF * R_REF * n; + tmp += 1ul * F_REF * R_REF * frac / thresh; + tmp /= lo_div; + + dev_dbg(&s->udev->dev, + "%s: rf=%u:%u n=%d thresh=%d frac=%d\n", + __func__, rf_freq, tmp, n, thresh, frac); ret = msi3101_tuner_write(s, 0x00000e); ret = msi3101_tuner_write(s, 0x000003); -- cgit v1.1 From 554cbfbe3b99d25caa3ab794a8619ccdea2b2935 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Mon, 5 Aug 2013 20:43:36 -0300 Subject: [media] msi3101: add support for stream format "252" I+Q per frame That one seems to have 14-bit ADC resolution, wow! It is now used when sampling rate is below 6 Msps. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 139 +++++++++++++++++++++++++--- 1 file changed, 125 insertions(+), 14 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 4ff6030..a937d00 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -397,6 +397,8 @@ struct msi3101_state { unsigned int vb_full; /* vb is full and packets dropped */ struct urb *urbs[MAX_ISO_BUFS]; + int (*convert_stream) (struct msi3101_state *s, u32 *dst, u8 *src, + unsigned int src_len); /* Controls */ struct v4l2_ctrl_handler ctrl_handler; @@ -484,7 +486,7 @@ leave: */ #define I2F_FRAC_BITS 23 #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) -static u32 msi3101_convert_sample(struct msi3101_state *s, u16 x, int shift) +static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift) { u32 msb, exponent, fraction, sign; s->sample_ctrl_bit[shift]++; @@ -521,7 +523,7 @@ static u32 msi3101_convert_sample(struct msi3101_state *s, u16 x, int shift) #define MSI3101_CONVERT_IN_URB_HANDLER #define MSI3101_EXTENSIVE_DEBUG -static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, +static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst, u8 *src, unsigned int src_len) { int i, j, k, l, i_max, dst_len = 0; @@ -561,10 +563,10 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4; sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2; - *dst++ = msi3101_convert_sample(s, sample[0], (bits >> (2 * k)) & 0x3); - *dst++ = msi3101_convert_sample(s, sample[1], (bits >> (2 * k)) & 0x3); - *dst++ = msi3101_convert_sample(s, sample[2], (bits >> (2 * k)) & 0x3); - *dst++ = msi3101_convert_sample(s, sample[3], (bits >> (2 * k)) & 0x3); + *dst++ = msi3101_convert_sample_384(s, sample[0], (bits >> (2 * k)) & 0x3); + *dst++ = msi3101_convert_sample_384(s, sample[1], (bits >> (2 * k)) & 0x3); + *dst++ = msi3101_convert_sample_384(s, sample[2], (bits >> (2 * k)) & 0x3); + *dst++ = msi3101_convert_sample_384(s, sample[3], (bits >> (2 * k)) & 0x3); /* 4 x 32bit float samples */ dst_len += 4 * 4; @@ -603,6 +605,103 @@ static int msi3101_convert_stream(struct msi3101_state *s, u32 *dst, } /* + * Converts signed 14-bit integer into 32-bit IEEE floating point + * representation. + * Will be exact from 0 to 2^24. Above that, we round towards zero + * as the fractional bits will not fit in a float. (It would be better to + * round towards even as the fpu does, but that is slower.) + */ +#define I2F_FRAC_BITS 23 +#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) +static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x) +{ + u32 msb, exponent, fraction, sign; + + /* Zero is special */ + if (!x) + return 0; + + /* Negative / positive value */ + if (x & (1 << 13)) { + x = -x; + x &= 0x1fff; /* result is 13 bit ... + sign */ + sign = 1 << 31; + } else { + sign = 0 << 31; + } + + /* Get location of the most significant bit */ + msb = __fls(x); + + /* + * Use a rotate instead of a shift because that works both leftwards + * and rightwards due to the mod(32) behaviour. This means we don't + * need to check to see if we are above 2^24 or not. + */ + fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; + exponent = (127 + msb) << I2F_FRAC_BITS; + + return (fraction + exponent) | sign; +} + +static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst, + u8 *src, unsigned int src_len) +{ + int i, j, i_max, dst_len = 0; + u16 sample[2]; + u32 sample_num[3]; + + /* There could be 1-3 1024 bytes URB frames */ + i_max = src_len / 1024; + + for (i = 0; i < i_max; i++) { + sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; + if (i == 0 && s->next_sample != sample_num[0]) { + dev_dbg_ratelimited(&s->udev->dev, + "%d samples lost, %d %08x:%08x\n", + sample_num[0] - s->next_sample, + src_len, s->next_sample, sample_num[0]); + } + + /* + * Dump all unknown 'garbage' data - maybe we will discover + * someday if there is something rational... + */ + dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); + + src += 16; + for (j = 0; j < 1008; j += 4) { + sample[0] = src[j + 0] >> 0 | src[j + 1] << 8; + sample[1] = src[j + 2] >> 0 | src[j + 3] << 8; + + *dst++ = msi3101_convert_sample_252(s, sample[0]); + *dst++ = msi3101_convert_sample_252(s, sample[1]); + } + /* 252 x I+Q 32bit float samples */ + dst_len += 252 * 2 * 4; + src += 1008; + } + + /* calculate samping rate and output it in 10 seconds intervals */ + if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { + unsigned long jiffies_now = jiffies; + unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); + unsigned int samples = sample_num[i_max - 1] - s->sample; + s->jiffies = jiffies_now; + s->sample = sample_num[i_max - 1]; + dev_dbg(&s->udev->dev, + "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", + src_len, samples, msecs, + samples * 1000UL / msecs); + } + + /* next sample (sample = sample + i * 252) */ + s->next_sample = sample_num[i_max - 1] + 252; + + return dst_len; +} + +/* * This gets called for the Isochronous pipe (stream). This is done in interrupt * time, so it has to be fast, not crash, and not stall. Neat. */ @@ -636,6 +735,8 @@ static void msi3101_isoc_handler(struct urb *urb) /* Compact data */ for (i = 0; i < urb->number_of_packets; i++) { + void *ptr; + /* Check frame error */ fstatus = urb->iso_frame_desc[i].status; if (fstatus) { @@ -664,9 +765,9 @@ static void msi3101_isoc_handler(struct urb *urb) /* fill framebuffer */ #ifdef MSI3101_CONVERT_IN_URB_HANDLER - vb2_set_plane_payload(&fbuf->vb, 0, - msi3101_convert_stream(s, - vb2_plane_vaddr(&fbuf->vb, 0), iso_buf, flen)); + ptr = vb2_plane_vaddr(&fbuf->vb, 0); + flen = s->convert_stream(s, ptr, iso_buf, flen); + vb2_set_plane_payload(&fbuf->vb, 0, flen); #else memcpy(fbuf->data, iso_buf, flen); fbuf->filled = flen; @@ -908,7 +1009,7 @@ static int msi3101_buf_finish(struct vb2_buffer *vb) container_of(vb, struct msi3101_frame_buf, vb); int ret; u32 *dst = vb2_plane_vaddr(&fbuf->vb, 0); - ret = msi3101_convert_stream(s, dst, fbuf->data, fbuf->filled); + ret = msi3101_convert_stream_384(s, dst, fbuf->data, fbuf->filled); vb2_set_plane_payload(&fbuf->vb, 0, ret); return 0; } @@ -988,7 +1089,19 @@ static int msi3101_tuner_write(struct msi3101_state *s, u32 data) static int msi3101_set_usb_adc(struct msi3101_state *s) { int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract; - u32 reg4, reg3; + u32 reg3, reg4, reg7; + + f_sr = s->ctrl_sampling_rate->val64; + + /* select stream format */ + if (f_sr < 6000000) { + s->convert_stream = msi3101_convert_stream_252; + reg7 = 0x00009407; + } else { + s->convert_stream = msi3101_convert_stream_384; + reg7 = 0x0000a507; + } + /* * Synthesizer config is just a educated guess... * @@ -1016,8 +1129,6 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) * * VCO 202000000 - 720000000++ */ - - f_sr = s->ctrl_sampling_rate->val64; reg3 = 0x01c00303; reg4 = 0x00000004; @@ -1062,7 +1173,7 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) if (ret) goto err; - ret = msi3101_ctrl_msg(s, CMD_WREG, 0x0000a507); + ret = msi3101_ctrl_msg(s, CMD_WREG, reg7); if (ret) goto err; -- cgit v1.1 From c5a431d02cdcafaf12edff770c294c5fbadcdf54 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Mon, 5 Aug 2013 21:33:31 -0300 Subject: [media] msi3101: init bits 23:20 on PLL register It is not sure what is meaning of that number, but it is better to try do as Windows driver. It seems to have small effect for signal when looking FM FFT. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index a937d00..93168db 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -1129,9 +1129,19 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) * * VCO 202000000 - 720000000++ */ - reg3 = 0x01c00303; + reg3 = 0x01000303; reg4 = 0x00000004; + /* XXX: Filters? AGC? */ + if (f_sr < 6000000) + reg3 |= 0x1 << 20; + else if (f_sr < 7000000) + reg3 |= 0x5 << 20; + else if (f_sr < 8500000) + reg3 |= 0x9 << 20; + else + reg3 |= 0xd << 20; + for (div_r_out = 4; div_r_out < 16; div_r_out += 2) { f_vco = f_sr * div_r_out * 12; dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n", -- cgit v1.1 From 35111f5bc94e790a2ac236dcaf3009c14c5e9f5d Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Wed, 7 Aug 2013 00:02:40 -0300 Subject: [media] msi3101: fix overflow in freq setting Higher frequencies were not possible to set correctly as that value overflows. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 93168db..56f9757 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -1507,11 +1507,11 @@ static int vidioc_s_frequency(struct file *file, void *priv, const struct v4l2_frequency *f) { struct msi3101_state *s = video_drvdata(file); - dev_dbg(&s->udev->dev, "%s: frequency=%u Hz (%d)\n", - __func__, f->frequency * 625U / 10U, f->frequency); + dev_dbg(&s->udev->dev, "%s: frequency=%lu Hz (%u)\n", + __func__, f->frequency * 625UL / 10UL, f->frequency); return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf, - f->frequency * 625U / 10U); + f->frequency * 625UL / 10UL); } const struct v4l2_ioctl_ops msi3101_ioctl_ops = { -- cgit v1.1 From 8e2efac7d2873ed81b536b8b1462b5e1eeefc2ab Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Wed, 7 Aug 2013 11:27:21 -0300 Subject: [media] msi3101: add stream format 336 I+Q pairs per frame That one seem to have 12-bit resolution. Use it for streams that has sampling rate 6 <= rate (Msps) < 8, between 6 and 8Msps. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 95 +++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 56f9757..c4bd963 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -605,6 +605,98 @@ static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst, } /* + * Converts signed 12-bit integer into 32-bit IEEE floating point + * representation. + */ +static u32 msi3101_convert_sample_336(struct msi3101_state *s, u16 x) +{ + u32 msb, exponent, fraction, sign; + + /* Zero is special */ + if (!x) + return 0; + + /* Negative / positive value */ + if (x & (1 << 11)) { + x = -x; + x &= 0x7ff; /* result is 11 bit ... + sign */ + sign = 1 << 31; + } else { + sign = 0 << 31; + } + + /* Get location of the most significant bit */ + msb = __fls(x); + + /* + * Use a rotate instead of a shift because that works both leftwards + * and rightwards due to the mod(32) behaviour. This means we don't + * need to check to see if we are above 2^24 or not. + */ + fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; + exponent = (127 + msb) << I2F_FRAC_BITS; + + return (fraction + exponent) | sign; +} + +static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst, + u8 *src, unsigned int src_len) +{ + int i, j, i_max, dst_len = 0; + u16 sample[2]; + u32 sample_num[3]; + + /* There could be 1-3 1024 bytes URB frames */ + i_max = src_len / 1024; + + for (i = 0; i < i_max; i++) { + sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; + if (i == 0 && s->next_sample != sample_num[0]) { + dev_dbg_ratelimited(&s->udev->dev, + "%d samples lost, %d %08x:%08x\n", + sample_num[0] - s->next_sample, + src_len, s->next_sample, sample_num[0]); + } + + /* + * Dump all unknown 'garbage' data - maybe we will discover + * someday if there is something rational... + */ + dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); + + src += 16; + for (j = 0; j < 1008; j += 3) { + sample[0] = (src[j + 0] & 0xff) >> 0 | (src[j + 1] & 0x0f) << 8; + sample[1] = (src[j + 1] & 0xf0) >> 4 | (src[j + 2] & 0xff) << 4; + + *dst++ = msi3101_convert_sample_336(s, sample[0]); + *dst++ = msi3101_convert_sample_336(s, sample[1]); + } + /* 336 x I+Q 32bit float samples */ + dst_len += 336 * 2 * 4; + src += 1008; + } + + /* calculate samping rate and output it in 10 seconds intervals */ + if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { + unsigned long jiffies_now = jiffies; + unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); + unsigned int samples = sample_num[i_max - 1] - s->sample; + s->jiffies = jiffies_now; + s->sample = sample_num[i_max - 1]; + dev_dbg(&s->udev->dev, + "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", + src_len, samples, msecs, + samples * 1000UL / msecs); + } + + /* next sample (sample = sample + i * 336) */ + s->next_sample = sample_num[i_max - 1] + 336; + + return dst_len; +} + +/* * Converts signed 14-bit integer into 32-bit IEEE floating point * representation. * Will be exact from 0 to 2^24. Above that, we round towards zero @@ -1097,6 +1189,9 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) if (f_sr < 6000000) { s->convert_stream = msi3101_convert_stream_252; reg7 = 0x00009407; + } else if (f_sr < 8000000) { + s->convert_stream = msi3101_convert_stream_336; + reg7 = 0x00008507; } else { s->convert_stream = msi3101_convert_stream_384; reg7 = 0x0000a507; -- cgit v1.1 From 093cdc79a96d598ab31a829e12b4547d8ca15d4a Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Wed, 7 Aug 2013 13:20:29 -0300 Subject: [media] msi3101: changes for tuner PLL freq limits I made some tuner freq limit tests against RF signal generator. Adjust some PLL limits according to these test results. Here are the results, taken from two different devices. Ranges are measured RF limits + calculated VCO limits. Mirics MSi3101 SDR Dongle: VHF_MODE 52 - 132 1664 - 4224 B3_MODE 103 - 263 1648 - 4208 B45_MODE 413 - 960 1652 - 3840 Hauppauge WinTV 133559 LF: VHF_MODE 49 - 130 1568 - 4160 B3_MODE 98 - 259 1568 - 4144 B45_MODE 391 - 960 1564 - 3840 Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index c4bd963..e7a21a2 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -1306,11 +1306,11 @@ static int msi3101_set_tuner(struct msi3101_state *s) u8 mode; u8 lo_div; } band_lut[] = { - { 30000000, 0x01, 16}, /* AM_MODE1 */ + { 47000000, 0x01, 16}, /* AM_MODE1 */ {108000000, 0x02, 32}, /* VHF_MODE */ - {240000000, 0x04, 16}, /* B3_MODE */ + {330000000, 0x04, 16}, /* B3_MODE */ {960000000, 0x08, 4}, /* B45_MODE */ - {167500000, 0x10, 2}, /* BL_MODE */ + { ~0U, 0x10, 2}, /* BL_MODE */ }; static const struct { u32 freq; -- cgit v1.1 From 00e049b05a4fe4c7a1503f2a46aced2548867631 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Wed, 7 Aug 2013 15:06:29 -0300 Subject: [media] msi3101: a lot of small cleanups Add comments, remove useless code and so. House cleaning party. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 141 ++++++++-------------------- 1 file changed, 40 insertions(+), 101 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index e7a21a2..a3cc4c6 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -16,20 +16,36 @@ * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * That driver is somehow based of pwc driver: + * (C) 1999-2004 Nemosoft Unv. + * (C) 2004-2006 Luc Saillard (luc@saillard.org) + * (C) 2011 Hans de Goede + * + * Development tree of that driver will be on: + * http://git.linuxtv.org/anttip/media_tree.git/shortlog/refs/heads/mirics + * + * GNU Radio plugin "gr-kernel" for device usage will be on: + * http://git.linuxtv.org/anttip/gr-kernel.git + * + * TODO: + * I will look these: + * - split RF tuner and USB ADC interface to own drivers (msi2500 and msi001) + * - move controls to V4L2 API + * + * Help is very highly welcome for these + all the others you could imagine: + * - use libv4l2 for stream format conversions + * - gr-kernel: switch to v4l2_mmap (current read eats a lot of cpu) + * - SDRSharp support */ -#include #include -#include #include -#include -#include #include #include #include #include #include -#include #include struct msi3101_gain { @@ -358,9 +374,9 @@ static const struct msi3101_gain msi3101_gain_lut_1000[] = { #define ISO_FRAMES_PER_DESC (8) #define ISO_MAX_FRAME_SIZE (3 * 1024) #define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE) - #define MAX_ISOC_ERRORS 20 +/* TODO: These should be moved to V4L2 API */ #define MSI3101_CID_SAMPLING_MODE ((V4L2_CID_USER_BASE | 0xf000) + 0) #define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 1) #define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 2) @@ -373,8 +389,6 @@ static const struct msi3101_gain msi3101_gain_lut_1000[] = { struct msi3101_frame_buf { struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */ struct list_head list; - void *data; /* raw data from USB device */ - int filled; /* number of bytes filled to *data */ }; struct msi3101_state { @@ -478,14 +492,19 @@ leave: */ /* - * Converts signed 10-bit integer into 32-bit IEEE floating point - * representation. - * Will be exact from 0 to 2^24. Above that, we round towards zero - * as the fractional bits will not fit in a float. (It would be better to - * round towards even as the fpu does, but that is slower.) + * Integer to 32-bit IEEE floating point representation routine is taken + * from Radeon R600 driver (drivers/gpu/drm/radeon/r600_blit_kms.c). + * + * TODO: Currently we do conversion here in Kernel, but in future that will + * be moved to the libv4l2 library as video format conversions are. */ #define I2F_FRAC_BITS 23 #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) + +/* + * Converts signed ~10+3-bit integer into 32-bit IEEE floating point + * representation. + */ static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift) { u32 msb, exponent, fraction, sign; @@ -510,35 +529,26 @@ static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift) /* Get location of the most significant bit */ msb = __fls(x); - /* - * Use a rotate instead of a shift because that works both leftwards - * and rightwards due to the mod(32) behaviour. This means we don't - * need to check to see if we are above 2^24 or not. - */ fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; exponent = (127 + msb) << I2F_FRAC_BITS; return (fraction + exponent) | sign; } -#define MSI3101_CONVERT_IN_URB_HANDLER -#define MSI3101_EXTENSIVE_DEBUG static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst, u8 *src, unsigned int src_len) { int i, j, k, l, i_max, dst_len = 0; u16 sample[4]; u32 bits; -#ifdef MSI3101_EXTENSIVE_DEBUG u32 sample_num[3]; -#endif + /* There could be 1-3 1024 bytes URB frames */ i_max = src_len / 1024; for (i = 0; i < i_max; i++) { -#ifdef MSI3101_EXTENSIVE_DEBUG sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; if (i == 0 && s->next_sample != sample_num[0]) { - dev_dbg(&s->udev->dev, + dev_dbg_ratelimited(&s->udev->dev, "%d samples lost, %d %08x:%08x\n", sample_num[0] - s->next_sample, src_len, s->next_sample, sample_num[0]); @@ -550,9 +560,7 @@ static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst, */ dev_dbg_ratelimited(&s->udev->dev, "%*ph %*ph\n", 12, &src[4], 24, &src[1000]); - memset(&src[4], 0, 12); - memset(&src[1000], 0, 24); -#endif + src += 16; for (j = 0; j < 6; j++) { bits = src[160 + 3] << 24 | src[160 + 2] << 16 | src[160 + 1] << 8 | src[160 + 0] << 0; @@ -567,22 +575,18 @@ static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst, *dst++ = msi3101_convert_sample_384(s, sample[1], (bits >> (2 * k)) & 0x3); *dst++ = msi3101_convert_sample_384(s, sample[2], (bits >> (2 * k)) & 0x3); *dst++ = msi3101_convert_sample_384(s, sample[3], (bits >> (2 * k)) & 0x3); - - /* 4 x 32bit float samples */ - dst_len += 4 * 4; } src += 10; } -#ifdef MSI3101_EXTENSIVE_DEBUG dev_dbg_ratelimited(&s->udev->dev, "sample control bits %08x\n", bits); -#endif src += 4; } + /* 384 x I+Q 32bit float samples */ + dst_len += 384 * 2 * 4; src += 24; } -#ifdef MSI3101_EXTENSIVE_DEBUG /* calculate samping rate and output it in 10 seconds intervals */ if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { unsigned long jiffies_now = jiffies; @@ -600,7 +604,7 @@ static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst, /* next sample (sample = sample + i * 384) */ s->next_sample = sample_num[i_max - 1] + 384; -#endif + return dst_len; } @@ -628,11 +632,6 @@ static u32 msi3101_convert_sample_336(struct msi3101_state *s, u16 x) /* Get location of the most significant bit */ msb = __fls(x); - /* - * Use a rotate instead of a shift because that works both leftwards - * and rightwards due to the mod(32) behaviour. This means we don't - * need to check to see if we are above 2^24 or not. - */ fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; exponent = (127 + msb) << I2F_FRAC_BITS; @@ -699,12 +698,7 @@ static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst, /* * Converts signed 14-bit integer into 32-bit IEEE floating point * representation. - * Will be exact from 0 to 2^24. Above that, we round towards zero - * as the fractional bits will not fit in a float. (It would be better to - * round towards even as the fpu does, but that is slower.) */ -#define I2F_FRAC_BITS 23 -#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x) { u32 msb, exponent, fraction, sign; @@ -725,11 +719,6 @@ static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x) /* Get location of the most significant bit */ msb = __fls(x); - /* - * Use a rotate instead of a shift because that works both leftwards - * and rightwards due to the mod(32) behaviour. This means we don't - * need to check to see if we are above 2^24 or not. - */ fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; exponent = (127 + msb) << I2F_FRAC_BITS; @@ -832,7 +821,7 @@ static void msi3101_isoc_handler(struct urb *urb) /* Check frame error */ fstatus = urb->iso_frame_desc[i].status; if (fstatus) { - dev_dbg(&s->udev->dev, + dev_dbg_ratelimited(&s->udev->dev, "frame=%d/%d has error %d skipping\n", i, urb->number_of_packets, fstatus); goto skip; @@ -856,14 +845,9 @@ static void msi3101_isoc_handler(struct urb *urb) } /* fill framebuffer */ -#ifdef MSI3101_CONVERT_IN_URB_HANDLER ptr = vb2_plane_vaddr(&fbuf->vb, 0); flen = s->convert_stream(s, ptr, iso_buf, flen); vb2_set_plane_payload(&fbuf->vb, 0, flen); -#else - memcpy(fbuf->data, iso_buf, flen); - fbuf->filled = flen; -#endif vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE); skip: ; @@ -1063,20 +1047,6 @@ static int msi3101_queue_setup(struct vb2_queue *vq, return 0; } -static int msi3101_buf_init(struct vb2_buffer *vb) -{ - struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); - struct msi3101_frame_buf *fbuf = - container_of(vb, struct msi3101_frame_buf, vb); - dev_dbg(&s->udev->dev, "%s:\n", __func__); - - fbuf->data = vzalloc(ISO_MAX_FRAME_SIZE); - if (fbuf->data == NULL) - return -ENOMEM; - - return 0; -} - static int msi3101_buf_prepare(struct vb2_buffer *vb) { struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); @@ -1088,34 +1058,6 @@ static int msi3101_buf_prepare(struct vb2_buffer *vb) return 0; } -#ifdef MSI3101_CONVERT_IN_URB_HANDLER -static int msi3101_buf_finish(struct vb2_buffer *vb) -{ - return 0; -} -#else -static int msi3101_buf_finish(struct vb2_buffer *vb) -{ - struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); - struct msi3101_frame_buf *fbuf = - container_of(vb, struct msi3101_frame_buf, vb); - int ret; - u32 *dst = vb2_plane_vaddr(&fbuf->vb, 0); - ret = msi3101_convert_stream_384(s, dst, fbuf->data, fbuf->filled); - vb2_set_plane_payload(&fbuf->vb, 0, ret); - return 0; -} -#endif - -static void msi3101_buf_cleanup(struct vb2_buffer *vb) -{ - struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); - struct msi3101_frame_buf *buf = - container_of(vb, struct msi3101_frame_buf, vb); - dev_dbg(&s->udev->dev, "%s:\n", __func__); - - vfree(buf->data); -} static void msi3101_buf_queue(struct vb2_buffer *vb) { struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); @@ -1544,10 +1486,7 @@ static int msi3101_stop_streaming(struct vb2_queue *vq) static struct vb2_ops msi3101_vb2_ops = { .queue_setup = msi3101_queue_setup, - .buf_init = msi3101_buf_init, .buf_prepare = msi3101_buf_prepare, - .buf_finish = msi3101_buf_finish, - .buf_cleanup = msi3101_buf_cleanup, .buf_queue = msi3101_buf_queue, .start_streaming = msi3101_start_streaming, .stop_streaming = msi3101_stop_streaming, @@ -1862,7 +1801,7 @@ err_free_mem: /* USB device ID list */ static struct usb_device_id msi3101_id_table[] = { - { USB_DEVICE(0x1df7, 0x2500) }, + { USB_DEVICE(0x1df7, 0x2500) }, /* Mirics MSi3101 SDR Dongle */ { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */ { } }; -- cgit v1.1 From 61198dfd08969a662767072aafeebcb493909338 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Thu, 8 Aug 2013 15:39:33 -0300 Subject: [media] msi3101: implement stream format 504 That stream format carries 504 x I+Q samples per 1024 USB frame. Sample resolution is 8-bit signed. Default it when sampling rate is 9Msps or over. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 94 ++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index a3cc4c6..bf735f9 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -448,7 +448,7 @@ leave: /* * +=========================================================================== - * | 00-1023 | USB packet + * | 00-1023 | USB packet type '384' * +=========================================================================== * | 00- 03 | sequence number of first sample in that USB packet * +--------------------------------------------------------------------------- @@ -502,6 +502,93 @@ leave: #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) /* + * Converts signed 8-bit integer into 32-bit IEEE floating point + * representation. + */ +static u32 msi3101_convert_sample_504(struct msi3101_state *s, u16 x) +{ + u32 msb, exponent, fraction, sign; + + /* Zero is special */ + if (!x) + return 0; + + /* Negative / positive value */ + if (x & (1 << 7)) { + x = -x; + x &= 0x7f; /* result is 7 bit ... + sign */ + sign = 1 << 31; + } else { + sign = 0 << 31; + } + + /* Get location of the most significant bit */ + msb = __fls(x); + + fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; + exponent = (127 + msb) << I2F_FRAC_BITS; + + return (fraction + exponent) | sign; +} + +static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst, + u8 *src, unsigned int src_len) +{ + int i, j, i_max, dst_len = 0; + u16 sample[2]; + u32 sample_num[3]; + + /* There could be 1-3 1024 bytes URB frames */ + i_max = src_len / 1024; + + for (i = 0; i < i_max; i++) { + sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; + if (i == 0 && s->next_sample != sample_num[0]) { + dev_dbg_ratelimited(&s->udev->dev, + "%d samples lost, %d %08x:%08x\n", + sample_num[0] - s->next_sample, + src_len, s->next_sample, sample_num[0]); + } + + /* + * Dump all unknown 'garbage' data - maybe we will discover + * someday if there is something rational... + */ + dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); + + src += 16; + for (j = 0; j < 1008; j += 2) { + sample[0] = src[j + 0]; + sample[1] = src[j + 1]; + + *dst++ = msi3101_convert_sample_504(s, sample[0]); + *dst++ = msi3101_convert_sample_504(s, sample[1]); + } + /* 504 x I+Q 32bit float samples */ + dst_len += 504 * 2 * 4; + src += 1008; + } + + /* calculate samping rate and output it in 10 seconds intervals */ + if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { + unsigned long jiffies_now = jiffies; + unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); + unsigned int samples = sample_num[i_max - 1] - s->sample; + s->jiffies = jiffies_now; + s->sample = sample_num[i_max - 1]; + dev_dbg(&s->udev->dev, + "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", + src_len, samples, msecs, + samples * 1000UL / msecs); + } + + /* next sample (sample = sample + i * 504) */ + s->next_sample = sample_num[i_max - 1] + 504; + + return dst_len; +} + +/* * Converts signed ~10+3-bit integer into 32-bit IEEE floating point * representation. */ @@ -1134,9 +1221,12 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) } else if (f_sr < 8000000) { s->convert_stream = msi3101_convert_stream_336; reg7 = 0x00008507; - } else { + } else if (f_sr < 9000000) { s->convert_stream = msi3101_convert_stream_384; reg7 = 0x0000a507; + } else { + s->convert_stream = msi3101_convert_stream_504; + reg7 = 0x000c9407; } /* -- cgit v1.1 From 0046079812cfcf6c725e067edb683e472d388552 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Sun, 11 Aug 2013 19:07:02 -0300 Subject: [media] msi3101: change stream format 384 After feeding different signal levels using RF generator and looking GNU Radio FFT sink I made decision to change bit shift 3 to bit shift 2 as there was very (too) huge visible leap in FFT sink GUI. Now it looks more natural. Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index bf735f9..839e601 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -589,7 +589,7 @@ static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst, } /* - * Converts signed ~10+3-bit integer into 32-bit IEEE floating point + * Converts signed ~10+2-bit integer into 32-bit IEEE floating point * representation. */ static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift) @@ -601,12 +601,15 @@ static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift) if (!x) return 0; - /* Convert 10-bit two's complement to 13-bit */ + if (shift == 3) + shift = 2; + + /* Convert 10-bit two's complement to 12-bit */ if (x & (1 << 9)) { x |= ~0U << 10; /* set all the rest bits to one */ x <<= shift; x = -x; - x &= 0xfff; /* result is 12 bit ... + sign */ + x &= 0x7ff; /* result is 11 bit ... + sign */ sign = 1 << 31; } else { x <<= shift; -- cgit v1.1 From c59e6d5650e3c0690b6f4f62ebd965e1aa466ea2 Mon Sep 17 00:00:00 2001 From: Antti Palosaari Date: Sat, 17 Aug 2013 19:30:18 -0300 Subject: [media] msi3101: few improvements for RF tuner * Fix AM_MODE. Now it could work at least in theory, cannot test. * Use greatest common divisor algo to divide PLL fractional parts. * Fix IF frequency mode. * + some very minor "style" issues Signed-off-by: Antti Palosaari Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 69 +++++++++++++++++------------ 1 file changed, 41 insertions(+), 28 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index 839e601..eebe1d0 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -29,11 +29,9 @@ * http://git.linuxtv.org/anttip/gr-kernel.git * * TODO: - * I will look these: - * - split RF tuner and USB ADC interface to own drivers (msi2500 and msi001) - * - move controls to V4L2 API - * * Help is very highly welcome for these + all the others you could imagine: + * - split USB ADC interface and RF tuner to own drivers (msi2500 and msi001) + * - move controls to V4L2 API * - use libv4l2 for stream format conversions * - gr-kernel: switch to v4l2_mmap (current read eats a lot of cpu) * - SDRSharp support @@ -41,6 +39,7 @@ #include #include +#include #include #include #include @@ -1331,25 +1330,25 @@ err: static int msi3101_set_tuner(struct msi3101_state *s) { int ret, i, len; - unsigned int n, m, thresh, frac, vco_step, tmp; + unsigned int n, m, thresh, frac, vco_step, tmp, f_if1; u32 reg; u64 f_vco; - u8 mode, lo_div; + u8 mode, filter_mode, lo_div; const struct msi3101_gain *gain_lut; static const struct { u32 rf; u8 mode; u8 lo_div; } band_lut[] = { - { 47000000, 0x01, 16}, /* AM_MODE1 */ - {108000000, 0x02, 32}, /* VHF_MODE */ - {330000000, 0x04, 16}, /* B3_MODE */ - {960000000, 0x08, 4}, /* B45_MODE */ - { ~0U, 0x10, 2}, /* BL_MODE */ + { 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */ + {108000000, 0x42, 32}, /* VHF_MODE */ + {330000000, 0x44, 16}, /* B3_MODE */ + {960000000, 0x48, 4}, /* B45_MODE */ + { ~0U, 0x50, 2}, /* BL_MODE */ }; static const struct { u32 freq; - u8 val; + u8 filter_mode; } if_freq_lut[] = { { 0, 0x03}, /* Zero IF */ { 450000, 0x02}, /* 450 kHz IF */ @@ -1370,19 +1369,19 @@ static int msi3101_set_tuner(struct msi3101_state *s) {8000000, 0x07}, /* 8 MHz */ }; - unsigned int rf_freq = s->ctrl_tuner_rf->val64; + unsigned int f_rf = s->ctrl_tuner_rf->val64; /* * bandwidth (Hz) * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000 */ - int bandwidth = s->ctrl_tuner_bw->val; + unsigned int bandwidth = s->ctrl_tuner_bw->val; /* * intermediate frequency (Hz) * 0, 450000, 1620000, 2048000 */ - int if_freq = s->ctrl_tuner_if->val; + unsigned int f_if = s->ctrl_tuner_if->val; /* * gain reduction (dB) @@ -1392,13 +1391,13 @@ static int msi3101_set_tuner(struct msi3101_state *s) int gain = s->ctrl_tuner_gain->val; dev_dbg(&s->udev->dev, - "%s: rf_freq=%d bandwidth=%d if_freq=%d gain=%d\n", - __func__, rf_freq, bandwidth, if_freq, gain); + "%s: f_rf=%d bandwidth=%d f_if=%d gain=%d\n", + __func__, f_rf, bandwidth, f_if, gain); ret = -EINVAL; for (i = 0; i < ARRAY_SIZE(band_lut); i++) { - if (rf_freq <= band_lut[i].rf) { + if (f_rf <= band_lut[i].rf) { mode = band_lut[i].mode; lo_div = band_lut[i].lo_div; break; @@ -1408,9 +1407,15 @@ static int msi3101_set_tuner(struct msi3101_state *s) if (i == ARRAY_SIZE(band_lut)) goto err; + /* AM_MODE is upconverted */ + if ((mode >> 0) & 0x1) + f_if1 = 5 * F_REF; + else + f_if1 = 0; + for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) { - if (if_freq == if_freq_lut[i].freq) { - if_freq = if_freq_lut[i].val; + if (f_if == if_freq_lut[i].freq) { + filter_mode = if_freq_lut[i].filter_mode; break; } } @@ -1430,8 +1435,7 @@ static int msi3101_set_tuner(struct msi3101_state *s) #define F_OUT_STEP 1 #define R_REF 4 -#define F_IF 0 - f_vco = (rf_freq + F_IF) * lo_div; + f_vco = (f_rf + f_if + f_if1) * lo_div; n = f_vco / (F_REF * R_REF); m = f_vco % (F_REF * R_REF); @@ -1439,7 +1443,12 @@ static int msi3101_set_tuner(struct msi3101_state *s) thresh = (F_REF * R_REF) / vco_step; frac = 1ul * thresh * m / (F_REF * R_REF); - /* Divide to reg max. After that RF resolution will be +-500Hz. */ + /* Find out greatest common divisor and divide to smaller. */ + tmp = gcd(thresh, frac); + thresh /= tmp; + frac /= tmp; + + /* Force divide to reg max. Resolution will be reduced. */ tmp = DIV_ROUND_UP(thresh, 4095); thresh = DIV_ROUND_CLOSEST(thresh, tmp); frac = DIV_ROUND_CLOSEST(frac, tmp); @@ -1451,15 +1460,19 @@ static int msi3101_set_tuner(struct msi3101_state *s) dev_dbg(&s->udev->dev, "%s: rf=%u:%u n=%d thresh=%d frac=%d\n", - __func__, rf_freq, tmp, n, thresh, frac); + __func__, f_rf, tmp, n, thresh, frac); ret = msi3101_tuner_write(s, 0x00000e); + if (ret) + goto err; + ret = msi3101_tuner_write(s, 0x000003); + if (ret) + goto err; reg = 0 << 0; reg |= mode << 4; - reg |= 1 << 10; - reg |= if_freq << 12; + reg |= filter_mode << 12; reg |= bandwidth << 14; reg |= 0x02 << 17; reg |= 0x00 << 20; @@ -1482,10 +1495,10 @@ static int msi3101_set_tuner(struct msi3101_state *s) if (ret) goto err; - if (rf_freq < 120000000) { + if (f_rf < 120000000) { gain_lut = msi3101_gain_lut_120; len = ARRAY_SIZE(msi3101_gain_lut_120); - } else if (rf_freq < 245000000) { + } else if (f_rf < 245000000) { gain_lut = msi3101_gain_lut_245; len = ARRAY_SIZE(msi3101_gain_lut_120); } else { -- cgit v1.1 From 04e40bdd67afc5ab94d983417f18bb1f20b08eac Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Sat, 24 Aug 2013 13:35:14 -0300 Subject: [media] msi3101: Fix compilation on i386 as reported by: kbuild test robot : [linuxtv-media:master 459/499] sdr-msi3101.c:undefined reference to `__umoddi3' Reported-by: kbuild test robot Signed-off-by: Mauro Carvalho Chehab --- drivers/staging/media/msi3101/sdr-msi3101.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'drivers/staging') diff --git a/drivers/staging/media/msi3101/sdr-msi3101.c b/drivers/staging/media/msi3101/sdr-msi3101.c index eebe1d0..24c7b70 100644 --- a/drivers/staging/media/msi3101/sdr-msi3101.c +++ b/drivers/staging/media/msi3101/sdr-msi3101.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -1332,7 +1333,7 @@ static int msi3101_set_tuner(struct msi3101_state *s) int ret, i, len; unsigned int n, m, thresh, frac, vco_step, tmp, f_if1; u32 reg; - u64 f_vco; + u64 f_vco, tmp64; u8 mode, filter_mode, lo_div; const struct msi3101_gain *gain_lut; static const struct { @@ -1436,8 +1437,10 @@ static int msi3101_set_tuner(struct msi3101_state *s) #define F_OUT_STEP 1 #define R_REF 4 f_vco = (f_rf + f_if + f_if1) * lo_div; - n = f_vco / (F_REF * R_REF); - m = f_vco % (F_REF * R_REF); + + tmp64 = f_vco; + m = do_div(tmp64, F_REF * R_REF); + n = (unsigned int) tmp64; vco_step = F_OUT_STEP * lo_div; thresh = (F_REF * R_REF) / vco_step; -- cgit v1.1