Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Realtek RTL2832U SDR driver
0004  *
0005  * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
0006  *
0007  * GNU Radio plugin "gr-kernel" for device usage will be on:
0008  * https://git.linuxtv.org/anttip/gr-kernel.git
0009  */
0010 
0011 #include "rtl2832_sdr.h"
0012 #include "dvb_usb.h"
0013 
0014 #include <media/v4l2-device.h>
0015 #include <media/v4l2-ioctl.h>
0016 #include <media/v4l2-ctrls.h>
0017 #include <media/v4l2-event.h>
0018 #include <media/videobuf2-v4l2.h>
0019 #include <media/videobuf2-vmalloc.h>
0020 
0021 #include <linux/platform_device.h>
0022 #include <linux/jiffies.h>
0023 #include <linux/math64.h>
0024 #include <linux/regmap.h>
0025 
0026 static bool rtl2832_sdr_emulated_fmt;
0027 module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
0028 MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
0029 
0030 /* Original macro does not contain enough null pointer checks for our need */
0031 #define V4L2_SUBDEV_HAS_OP(sd, o, f) \
0032     ((sd) && (sd)->ops && (sd)->ops->o && (sd)->ops->o->f)
0033 
0034 #define MAX_BULK_BUFS            (10)
0035 #define BULK_BUFFER_SIZE         (128 * 512)
0036 
0037 static const struct v4l2_frequency_band bands_adc[] = {
0038     {
0039         .tuner = 0,
0040         .type = V4L2_TUNER_ADC,
0041         .index = 0,
0042         .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
0043         .rangelow   =  300000,
0044         .rangehigh  =  300000,
0045     },
0046     {
0047         .tuner = 0,
0048         .type = V4L2_TUNER_ADC,
0049         .index = 1,
0050         .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
0051         .rangelow   =  900001,
0052         .rangehigh  = 2800000,
0053     },
0054     {
0055         .tuner = 0,
0056         .type = V4L2_TUNER_ADC,
0057         .index = 2,
0058         .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
0059         .rangelow   = 3200000,
0060         .rangehigh  = 3200000,
0061     },
0062 };
0063 
0064 static const struct v4l2_frequency_band bands_fm[] = {
0065     {
0066         .tuner = 1,
0067         .type = V4L2_TUNER_RF,
0068         .index = 0,
0069         .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
0070         .rangelow   =    50000000,
0071         .rangehigh  =  2000000000,
0072     },
0073 };
0074 
0075 /* stream formats */
0076 struct rtl2832_sdr_format {
0077     char    *name;
0078     u32 pixelformat;
0079     u32 buffersize;
0080 };
0081 
0082 static struct rtl2832_sdr_format formats[] = {
0083     {
0084         .pixelformat    = V4L2_SDR_FMT_CU8,
0085         .buffersize = BULK_BUFFER_SIZE,
0086     }, {
0087         .pixelformat    = V4L2_SDR_FMT_CU16LE,
0088         .buffersize = BULK_BUFFER_SIZE * 2,
0089     },
0090 };
0091 
0092 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
0093 
0094 /* intermediate buffers with raw data from the USB device */
0095 struct rtl2832_sdr_frame_buf {
0096     /* common v4l buffer stuff -- must be first */
0097     struct vb2_v4l2_buffer vb;
0098     struct list_head list;
0099 };
0100 
0101 struct rtl2832_sdr_dev {
0102 #define POWER_ON           0  /* BIT(0) */
0103 #define URB_BUF            1  /* BIT(1) */
0104     unsigned long flags;
0105 
0106     struct platform_device *pdev;
0107     struct regmap *regmap;
0108 
0109     struct video_device vdev;
0110     struct v4l2_device v4l2_dev;
0111     struct v4l2_subdev *v4l2_subdev;
0112 
0113     /* videobuf2 queue and queued buffers list */
0114     struct vb2_queue vb_queue;
0115     struct list_head queued_bufs;
0116     spinlock_t queued_bufs_lock; /* Protects queued_bufs */
0117     unsigned sequence;       /* buffer sequence counter */
0118 
0119     /* Note if taking both locks v4l2_lock must always be locked first! */
0120     struct mutex v4l2_lock;      /* Protects everything else */
0121     struct mutex vb_queue_lock;  /* Protects vb_queue and capt_file */
0122 
0123     /* Pointer to our usb_device, will be NULL after unplug */
0124     struct usb_device *udev; /* Both mutexes most be hold when setting! */
0125 
0126     unsigned int vb_full; /* vb is full and packets dropped */
0127 
0128     struct urb     *urb_list[MAX_BULK_BUFS];
0129     int            buf_num;
0130     unsigned long  buf_size;
0131     u8             *buf_list[MAX_BULK_BUFS];
0132     dma_addr_t     dma_addr[MAX_BULK_BUFS];
0133     int urbs_initialized;
0134     int urbs_submitted;
0135 
0136     unsigned int f_adc, f_tuner;
0137     u32 pixelformat;
0138     u32 buffersize;
0139     unsigned int num_formats;
0140 
0141     /* Controls */
0142     struct v4l2_ctrl_handler hdl;
0143     struct v4l2_ctrl *bandwidth_auto;
0144     struct v4l2_ctrl *bandwidth;
0145 
0146     /* for sample rate calc */
0147     unsigned int sample;
0148     unsigned int sample_measured;
0149     unsigned long jiffies_next;
0150 };
0151 
0152 /* Private functions */
0153 static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
0154         struct rtl2832_sdr_dev *dev)
0155 {
0156     unsigned long flags;
0157     struct rtl2832_sdr_frame_buf *buf = NULL;
0158 
0159     spin_lock_irqsave(&dev->queued_bufs_lock, flags);
0160     if (list_empty(&dev->queued_bufs))
0161         goto leave;
0162 
0163     buf = list_entry(dev->queued_bufs.next,
0164             struct rtl2832_sdr_frame_buf, list);
0165     list_del(&buf->list);
0166 leave:
0167     spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
0168     return buf;
0169 }
0170 
0171 static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
0172         void *dst, const u8 *src, unsigned int src_len)
0173 {
0174     struct platform_device *pdev = dev->pdev;
0175     unsigned int dst_len;
0176 
0177     if (dev->pixelformat ==  V4L2_SDR_FMT_CU8) {
0178         /* native stream, no need to convert */
0179         memcpy(dst, src, src_len);
0180         dst_len = src_len;
0181     } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
0182         /* convert u8 to u16 */
0183         unsigned int i;
0184         u16 *u16dst = dst;
0185 
0186         for (i = 0; i < src_len; i++)
0187             *u16dst++ = (src[i] << 8) | (src[i] >> 0);
0188         dst_len = 2 * src_len;
0189     } else {
0190         dst_len = 0;
0191     }
0192 
0193     /* calculate sample rate and output it in 10 seconds intervals */
0194     if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
0195         #define MSECS 10000UL
0196         unsigned int msecs = jiffies_to_msecs(jiffies -
0197                 dev->jiffies_next + msecs_to_jiffies(MSECS));
0198         unsigned int samples = dev->sample - dev->sample_measured;
0199 
0200         dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
0201         dev->sample_measured = dev->sample;
0202         dev_dbg(&pdev->dev,
0203             "slen=%u samples=%u msecs=%u sample rate=%lu\n",
0204             src_len, samples, msecs, samples * 1000UL / msecs);
0205     }
0206 
0207     /* total number of I+Q pairs */
0208     dev->sample += src_len / 2;
0209 
0210     return dst_len;
0211 }
0212 
0213 /*
0214  * This gets called for the bulk stream pipe. This is done in interrupt
0215  * time, so it has to be fast, not crash, and not stall. Neat.
0216  */
0217 static void rtl2832_sdr_urb_complete(struct urb *urb)
0218 {
0219     struct rtl2832_sdr_dev *dev = urb->context;
0220     struct platform_device *pdev = dev->pdev;
0221     struct rtl2832_sdr_frame_buf *fbuf;
0222 
0223     dev_dbg_ratelimited(&pdev->dev, "status=%d length=%d/%d errors=%d\n",
0224                 urb->status, urb->actual_length,
0225                 urb->transfer_buffer_length, urb->error_count);
0226 
0227     switch (urb->status) {
0228     case 0:             /* success */
0229     case -ETIMEDOUT:    /* NAK */
0230         break;
0231     case -ECONNRESET:   /* kill */
0232     case -ENOENT:
0233     case -ESHUTDOWN:
0234         return;
0235     default:            /* error */
0236         dev_err_ratelimited(&pdev->dev, "urb failed=%d\n", urb->status);
0237         break;
0238     }
0239 
0240     if (likely(urb->actual_length > 0)) {
0241         void *ptr;
0242         unsigned int len;
0243         /* get free framebuffer */
0244         fbuf = rtl2832_sdr_get_next_fill_buf(dev);
0245         if (unlikely(fbuf == NULL)) {
0246             dev->vb_full++;
0247             dev_notice_ratelimited(&pdev->dev,
0248                            "videobuf is full, %d packets dropped\n",
0249                            dev->vb_full);
0250             goto skip;
0251         }
0252 
0253         /* fill framebuffer */
0254         ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
0255         len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
0256                 urb->actual_length);
0257         vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len);
0258         fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
0259         fbuf->vb.sequence = dev->sequence++;
0260         vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
0261     }
0262 skip:
0263     usb_submit_urb(urb, GFP_ATOMIC);
0264 }
0265 
0266 static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
0267 {
0268     struct platform_device *pdev = dev->pdev;
0269     int i;
0270 
0271     for (i = dev->urbs_submitted - 1; i >= 0; i--) {
0272         dev_dbg(&pdev->dev, "kill urb=%d\n", i);
0273         /* stop the URB */
0274         usb_kill_urb(dev->urb_list[i]);
0275     }
0276     dev->urbs_submitted = 0;
0277 
0278     return 0;
0279 }
0280 
0281 static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
0282 {
0283     struct platform_device *pdev = dev->pdev;
0284     int i, ret;
0285 
0286     for (i = 0; i < dev->urbs_initialized; i++) {
0287         dev_dbg(&pdev->dev, "submit urb=%d\n", i);
0288         ret = usb_submit_urb(dev->urb_list[i], GFP_KERNEL);
0289         if (ret) {
0290             dev_err(&pdev->dev,
0291                 "Could not submit urb no. %d - get them all back\n",
0292                 i);
0293             rtl2832_sdr_kill_urbs(dev);
0294             return ret;
0295         }
0296         dev->urbs_submitted++;
0297     }
0298 
0299     return 0;
0300 }
0301 
0302 static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
0303 {
0304     struct platform_device *pdev = dev->pdev;
0305 
0306     if (test_bit(URB_BUF, &dev->flags)) {
0307         while (dev->buf_num) {
0308             dev->buf_num--;
0309             dev_dbg(&pdev->dev, "free buf=%d\n", dev->buf_num);
0310             usb_free_coherent(dev->udev, dev->buf_size,
0311                       dev->buf_list[dev->buf_num],
0312                       dev->dma_addr[dev->buf_num]);
0313         }
0314     }
0315     clear_bit(URB_BUF, &dev->flags);
0316 
0317     return 0;
0318 }
0319 
0320 static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
0321 {
0322     struct platform_device *pdev = dev->pdev;
0323 
0324     dev->buf_num = 0;
0325     dev->buf_size = BULK_BUFFER_SIZE;
0326 
0327     dev_dbg(&pdev->dev, "all in all I will use %u bytes for streaming\n",
0328         MAX_BULK_BUFS * BULK_BUFFER_SIZE);
0329 
0330     for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
0331         dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
0332                 BULK_BUFFER_SIZE, GFP_KERNEL,
0333                 &dev->dma_addr[dev->buf_num]);
0334         if (!dev->buf_list[dev->buf_num]) {
0335             dev_dbg(&pdev->dev, "alloc buf=%d failed\n",
0336                 dev->buf_num);
0337             rtl2832_sdr_free_stream_bufs(dev);
0338             return -ENOMEM;
0339         }
0340 
0341         dev_dbg(&pdev->dev, "alloc buf=%d %p (dma %llu)\n",
0342             dev->buf_num, dev->buf_list[dev->buf_num],
0343             (long long)dev->dma_addr[dev->buf_num]);
0344         set_bit(URB_BUF, &dev->flags);
0345     }
0346 
0347     return 0;
0348 }
0349 
0350 static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
0351 {
0352     struct platform_device *pdev = dev->pdev;
0353     int i;
0354 
0355     rtl2832_sdr_kill_urbs(dev);
0356 
0357     for (i = dev->urbs_initialized - 1; i >= 0; i--) {
0358         if (dev->urb_list[i]) {
0359             dev_dbg(&pdev->dev, "free urb=%d\n", i);
0360             /* free the URBs */
0361             usb_free_urb(dev->urb_list[i]);
0362         }
0363     }
0364     dev->urbs_initialized = 0;
0365 
0366     return 0;
0367 }
0368 
0369 static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
0370 {
0371     struct platform_device *pdev = dev->pdev;
0372     int i, j;
0373 
0374     /* allocate the URBs */
0375     for (i = 0; i < MAX_BULK_BUFS; i++) {
0376         dev_dbg(&pdev->dev, "alloc urb=%d\n", i);
0377         dev->urb_list[i] = usb_alloc_urb(0, GFP_KERNEL);
0378         if (!dev->urb_list[i]) {
0379             for (j = 0; j < i; j++) {
0380                 usb_free_urb(dev->urb_list[j]);
0381                 dev->urb_list[j] = NULL;
0382             }
0383             dev->urbs_initialized = 0;
0384             return -ENOMEM;
0385         }
0386         usb_fill_bulk_urb(dev->urb_list[i],
0387                 dev->udev,
0388                 usb_rcvbulkpipe(dev->udev, 0x81),
0389                 dev->buf_list[i],
0390                 BULK_BUFFER_SIZE,
0391                 rtl2832_sdr_urb_complete, dev);
0392 
0393         dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
0394         dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
0395         dev->urbs_initialized++;
0396     }
0397 
0398     return 0;
0399 }
0400 
0401 /* Must be called with vb_queue_lock hold */
0402 static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
0403 {
0404     struct platform_device *pdev = dev->pdev;
0405     unsigned long flags;
0406 
0407     dev_dbg(&pdev->dev, "\n");
0408 
0409     spin_lock_irqsave(&dev->queued_bufs_lock, flags);
0410     while (!list_empty(&dev->queued_bufs)) {
0411         struct rtl2832_sdr_frame_buf *buf;
0412 
0413         buf = list_entry(dev->queued_bufs.next,
0414                 struct rtl2832_sdr_frame_buf, list);
0415         list_del(&buf->list);
0416         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0417     }
0418     spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
0419 }
0420 
0421 static int rtl2832_sdr_querycap(struct file *file, void *fh,
0422         struct v4l2_capability *cap)
0423 {
0424     struct rtl2832_sdr_dev *dev = video_drvdata(file);
0425     struct platform_device *pdev = dev->pdev;
0426 
0427     dev_dbg(&pdev->dev, "\n");
0428 
0429     strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
0430     strscpy(cap->card, dev->vdev.name, sizeof(cap->card));
0431     usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
0432     return 0;
0433 }
0434 
0435 /* Videobuf2 operations */
0436 static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
0437         unsigned int *nbuffers,
0438         unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
0439 {
0440     struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
0441     struct platform_device *pdev = dev->pdev;
0442 
0443     dev_dbg(&pdev->dev, "nbuffers=%d\n", *nbuffers);
0444 
0445     /* Need at least 8 buffers */
0446     if (vq->num_buffers + *nbuffers < 8)
0447         *nbuffers = 8 - vq->num_buffers;
0448     *nplanes = 1;
0449     sizes[0] = PAGE_ALIGN(dev->buffersize);
0450     dev_dbg(&pdev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
0451     return 0;
0452 }
0453 
0454 static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
0455 {
0456     struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
0457 
0458     /* Don't allow queueing new buffers after device disconnection */
0459     if (!dev->udev)
0460         return -ENODEV;
0461 
0462     return 0;
0463 }
0464 
0465 static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
0466 {
0467     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0468     struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
0469     struct rtl2832_sdr_frame_buf *buf =
0470             container_of(vbuf, struct rtl2832_sdr_frame_buf, vb);
0471     unsigned long flags;
0472 
0473     /* Check the device has not disconnected between prep and queuing */
0474     if (!dev->udev) {
0475         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0476         return;
0477     }
0478 
0479     spin_lock_irqsave(&dev->queued_bufs_lock, flags);
0480     list_add_tail(&buf->list, &dev->queued_bufs);
0481     spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
0482 }
0483 
0484 static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
0485 {
0486     struct platform_device *pdev = dev->pdev;
0487     struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
0488     struct dvb_frontend *fe = pdata->dvb_frontend;
0489     int ret;
0490     unsigned int f_sr, f_if;
0491     u8 buf[4], u8tmp1, u8tmp2;
0492     u64 u64tmp;
0493     u32 u32tmp;
0494 
0495     dev_dbg(&pdev->dev, "f_adc=%u\n", dev->f_adc);
0496 
0497     if (!test_bit(POWER_ON, &dev->flags))
0498         return 0;
0499 
0500     if (dev->f_adc == 0)
0501         return 0;
0502 
0503     f_sr = dev->f_adc;
0504 
0505     ret = regmap_bulk_write(dev->regmap, 0x13e, "\x00\x00", 2);
0506     if (ret)
0507         goto err;
0508 
0509     ret = regmap_bulk_write(dev->regmap, 0x115, "\x00\x00\x00\x00", 4);
0510     if (ret)
0511         goto err;
0512 
0513     /* get IF from tuner */
0514     if (fe->ops.tuner_ops.get_if_frequency)
0515         ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
0516     else
0517         ret = -EINVAL;
0518 
0519     if (ret)
0520         goto err;
0521 
0522     /* program IF */
0523     u64tmp = f_if % pdata->clk;
0524     u64tmp *= 0x400000;
0525     u64tmp = div_u64(u64tmp, pdata->clk);
0526     u64tmp = -u64tmp;
0527     u32tmp = u64tmp & 0x3fffff;
0528 
0529     dev_dbg(&pdev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
0530 
0531     buf[0] = (u32tmp >> 16) & 0xff;
0532     buf[1] = (u32tmp >>  8) & 0xff;
0533     buf[2] = (u32tmp >>  0) & 0xff;
0534 
0535     ret = regmap_bulk_write(dev->regmap, 0x119, buf, 3);
0536     if (ret)
0537         goto err;
0538 
0539     /* BB / IF mode */
0540     /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
0541     if (f_if) {
0542         u8tmp1 = 0x1a; /* disable Zero-IF */
0543         u8tmp2 = 0x8d; /* enable ADC I */
0544     } else {
0545         u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
0546         u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
0547     }
0548 
0549     ret = regmap_write(dev->regmap, 0x1b1, u8tmp1);
0550     if (ret)
0551         goto err;
0552 
0553     ret = regmap_write(dev->regmap, 0x008, u8tmp2);
0554     if (ret)
0555         goto err;
0556 
0557     ret = regmap_write(dev->regmap, 0x006, 0x80);
0558     if (ret)
0559         goto err;
0560 
0561     /* program sampling rate (resampling down) */
0562     u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
0563     u32tmp <<= 2;
0564     buf[0] = (u32tmp >> 24) & 0xff;
0565     buf[1] = (u32tmp >> 16) & 0xff;
0566     buf[2] = (u32tmp >>  8) & 0xff;
0567     buf[3] = (u32tmp >>  0) & 0xff;
0568     ret = regmap_bulk_write(dev->regmap, 0x19f, buf, 4);
0569     if (ret)
0570         goto err;
0571 
0572     /* low-pass filter */
0573     ret = regmap_bulk_write(dev->regmap, 0x11c,
0574                 "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
0575                 20);
0576     if (ret)
0577         goto err;
0578 
0579     ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
0580     if (ret)
0581         goto err;
0582 
0583     /* mode */
0584     ret = regmap_write(dev->regmap, 0x019, 0x05);
0585     if (ret)
0586         goto err;
0587 
0588     ret = regmap_bulk_write(dev->regmap, 0x01a,
0589                 "\x1b\x16\x0d\x06\x01\xff", 6);
0590     if (ret)
0591         goto err;
0592 
0593     /* FSM */
0594     ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\xf0\x0f", 3);
0595     if (ret)
0596         goto err;
0597 
0598     /* PID filter */
0599     ret = regmap_write(dev->regmap, 0x061, 0x60);
0600     if (ret)
0601         goto err;
0602 
0603     /* used RF tuner based settings */
0604     switch (pdata->tuner) {
0605     case RTL2832_SDR_TUNER_E4000:
0606         ret = regmap_write(dev->regmap, 0x112, 0x5a);
0607         ret = regmap_write(dev->regmap, 0x102, 0x40);
0608         ret = regmap_write(dev->regmap, 0x103, 0x5a);
0609         ret = regmap_write(dev->regmap, 0x1c7, 0x30);
0610         ret = regmap_write(dev->regmap, 0x104, 0xd0);
0611         ret = regmap_write(dev->regmap, 0x105, 0xbe);
0612         ret = regmap_write(dev->regmap, 0x1c8, 0x18);
0613         ret = regmap_write(dev->regmap, 0x106, 0x35);
0614         ret = regmap_write(dev->regmap, 0x1c9, 0x21);
0615         ret = regmap_write(dev->regmap, 0x1ca, 0x21);
0616         ret = regmap_write(dev->regmap, 0x1cb, 0x00);
0617         ret = regmap_write(dev->regmap, 0x107, 0x40);
0618         ret = regmap_write(dev->regmap, 0x1cd, 0x10);
0619         ret = regmap_write(dev->regmap, 0x1ce, 0x10);
0620         ret = regmap_write(dev->regmap, 0x108, 0x80);
0621         ret = regmap_write(dev->regmap, 0x109, 0x7f);
0622         ret = regmap_write(dev->regmap, 0x10a, 0x80);
0623         ret = regmap_write(dev->regmap, 0x10b, 0x7f);
0624         ret = regmap_write(dev->regmap, 0x00e, 0xfc);
0625         ret = regmap_write(dev->regmap, 0x00e, 0xfc);
0626         ret = regmap_write(dev->regmap, 0x011, 0xd4);
0627         ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
0628         ret = regmap_write(dev->regmap, 0x1d9, 0x00);
0629         ret = regmap_write(dev->regmap, 0x1db, 0x00);
0630         ret = regmap_write(dev->regmap, 0x1dd, 0x14);
0631         ret = regmap_write(dev->regmap, 0x1de, 0xec);
0632         ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
0633         ret = regmap_write(dev->regmap, 0x1e6, 0x02);
0634         ret = regmap_write(dev->regmap, 0x1d7, 0x09);
0635         ret = regmap_write(dev->regmap, 0x00d, 0x83);
0636         ret = regmap_write(dev->regmap, 0x010, 0x49);
0637         ret = regmap_write(dev->regmap, 0x00d, 0x87);
0638         ret = regmap_write(dev->regmap, 0x00d, 0x85);
0639         ret = regmap_write(dev->regmap, 0x013, 0x02);
0640         break;
0641     case RTL2832_SDR_TUNER_FC0012:
0642     case RTL2832_SDR_TUNER_FC0013:
0643         ret = regmap_write(dev->regmap, 0x112, 0x5a);
0644         ret = regmap_write(dev->regmap, 0x102, 0x40);
0645         ret = regmap_write(dev->regmap, 0x103, 0x5a);
0646         ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
0647         ret = regmap_write(dev->regmap, 0x104, 0xcc);
0648         ret = regmap_write(dev->regmap, 0x105, 0xbe);
0649         ret = regmap_write(dev->regmap, 0x1c8, 0x16);
0650         ret = regmap_write(dev->regmap, 0x106, 0x35);
0651         ret = regmap_write(dev->regmap, 0x1c9, 0x21);
0652         ret = regmap_write(dev->regmap, 0x1ca, 0x21);
0653         ret = regmap_write(dev->regmap, 0x1cb, 0x00);
0654         ret = regmap_write(dev->regmap, 0x107, 0x40);
0655         ret = regmap_write(dev->regmap, 0x1cd, 0x10);
0656         ret = regmap_write(dev->regmap, 0x1ce, 0x10);
0657         ret = regmap_write(dev->regmap, 0x108, 0x80);
0658         ret = regmap_write(dev->regmap, 0x109, 0x7f);
0659         ret = regmap_write(dev->regmap, 0x10a, 0x80);
0660         ret = regmap_write(dev->regmap, 0x10b, 0x7f);
0661         ret = regmap_write(dev->regmap, 0x00e, 0xfc);
0662         ret = regmap_write(dev->regmap, 0x00e, 0xfc);
0663         ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xbf", 2);
0664         ret = regmap_write(dev->regmap, 0x1e5, 0xf0);
0665         ret = regmap_write(dev->regmap, 0x1d9, 0x00);
0666         ret = regmap_write(dev->regmap, 0x1db, 0x00);
0667         ret = regmap_write(dev->regmap, 0x1dd, 0x11);
0668         ret = regmap_write(dev->regmap, 0x1de, 0xef);
0669         ret = regmap_write(dev->regmap, 0x1d8, 0x0c);
0670         ret = regmap_write(dev->regmap, 0x1e6, 0x02);
0671         ret = regmap_write(dev->regmap, 0x1d7, 0x09);
0672         break;
0673     case RTL2832_SDR_TUNER_R820T:
0674     case RTL2832_SDR_TUNER_R828D:
0675         ret = regmap_write(dev->regmap, 0x112, 0x5a);
0676         ret = regmap_write(dev->regmap, 0x102, 0x40);
0677         ret = regmap_write(dev->regmap, 0x115, 0x01);
0678         ret = regmap_write(dev->regmap, 0x103, 0x80);
0679         ret = regmap_write(dev->regmap, 0x1c7, 0x24);
0680         ret = regmap_write(dev->regmap, 0x104, 0xcc);
0681         ret = regmap_write(dev->regmap, 0x105, 0xbe);
0682         ret = regmap_write(dev->regmap, 0x1c8, 0x14);
0683         ret = regmap_write(dev->regmap, 0x106, 0x35);
0684         ret = regmap_write(dev->regmap, 0x1c9, 0x21);
0685         ret = regmap_write(dev->regmap, 0x1ca, 0x21);
0686         ret = regmap_write(dev->regmap, 0x1cb, 0x00);
0687         ret = regmap_write(dev->regmap, 0x107, 0x40);
0688         ret = regmap_write(dev->regmap, 0x1cd, 0x10);
0689         ret = regmap_write(dev->regmap, 0x1ce, 0x10);
0690         ret = regmap_write(dev->regmap, 0x108, 0x80);
0691         ret = regmap_write(dev->regmap, 0x109, 0x7f);
0692         ret = regmap_write(dev->regmap, 0x10a, 0x80);
0693         ret = regmap_write(dev->regmap, 0x10b, 0x7f);
0694         ret = regmap_write(dev->regmap, 0x00e, 0xfc);
0695         ret = regmap_write(dev->regmap, 0x00e, 0xfc);
0696         ret = regmap_write(dev->regmap, 0x011, 0xf4);
0697         break;
0698     case RTL2832_SDR_TUNER_FC2580:
0699         ret = regmap_write(dev->regmap, 0x112, 0x39);
0700         ret = regmap_write(dev->regmap, 0x102, 0x40);
0701         ret = regmap_write(dev->regmap, 0x103, 0x5a);
0702         ret = regmap_write(dev->regmap, 0x1c7, 0x2c);
0703         ret = regmap_write(dev->regmap, 0x104, 0xcc);
0704         ret = regmap_write(dev->regmap, 0x105, 0xbe);
0705         ret = regmap_write(dev->regmap, 0x1c8, 0x16);
0706         ret = regmap_write(dev->regmap, 0x106, 0x35);
0707         ret = regmap_write(dev->regmap, 0x1c9, 0x21);
0708         ret = regmap_write(dev->regmap, 0x1ca, 0x21);
0709         ret = regmap_write(dev->regmap, 0x1cb, 0x00);
0710         ret = regmap_write(dev->regmap, 0x107, 0x40);
0711         ret = regmap_write(dev->regmap, 0x1cd, 0x10);
0712         ret = regmap_write(dev->regmap, 0x1ce, 0x10);
0713         ret = regmap_write(dev->regmap, 0x108, 0x80);
0714         ret = regmap_write(dev->regmap, 0x109, 0x7f);
0715         ret = regmap_write(dev->regmap, 0x10a, 0x9c);
0716         ret = regmap_write(dev->regmap, 0x10b, 0x7f);
0717         ret = regmap_write(dev->regmap, 0x00e, 0xfc);
0718         ret = regmap_write(dev->regmap, 0x00e, 0xfc);
0719         ret = regmap_bulk_write(dev->regmap, 0x011, "\xe9\xf4", 2);
0720         break;
0721     default:
0722         dev_notice(&pdev->dev, "Unsupported tuner\n");
0723     }
0724 
0725     /* software reset */
0726     ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x04);
0727     if (ret)
0728         goto err;
0729 
0730     ret = regmap_update_bits(dev->regmap, 0x101, 0x04, 0x00);
0731     if (ret)
0732         goto err;
0733 err:
0734     return ret;
0735 };
0736 
0737 static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
0738 {
0739     struct platform_device *pdev = dev->pdev;
0740     int ret;
0741 
0742     dev_dbg(&pdev->dev, "\n");
0743 
0744     /* PID filter */
0745     ret = regmap_write(dev->regmap, 0x061, 0xe0);
0746     if (ret)
0747         goto err;
0748 
0749     /* mode */
0750     ret = regmap_write(dev->regmap, 0x019, 0x20);
0751     if (ret)
0752         goto err;
0753 
0754     ret = regmap_bulk_write(dev->regmap, 0x017, "\x11\x10", 2);
0755     if (ret)
0756         goto err;
0757 
0758     /* FSM */
0759     ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3);
0760     if (ret)
0761         goto err;
0762 
0763     ret = regmap_bulk_write(dev->regmap, 0x13e, "\x40\x00", 2);
0764     if (ret)
0765         goto err;
0766 
0767     ret = regmap_bulk_write(dev->regmap, 0x115, "\x06\x3f\xce\xcc", 4);
0768     if (ret)
0769         goto err;
0770 err:
0771     return;
0772 };
0773 
0774 static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
0775 {
0776     struct platform_device *pdev = dev->pdev;
0777     struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
0778     struct dvb_frontend *fe = pdata->dvb_frontend;
0779     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0780     struct v4l2_ctrl *bandwidth_auto;
0781     struct v4l2_ctrl *bandwidth;
0782 
0783     /*
0784      * tuner RF (Hz)
0785      */
0786     if (dev->f_tuner == 0)
0787         return 0;
0788 
0789     /*
0790      * bandwidth (Hz)
0791      */
0792     bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
0793                     V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
0794     bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
0795     if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
0796         c->bandwidth_hz = dev->f_adc;
0797         v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
0798     } else {
0799         c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
0800     }
0801 
0802     c->frequency = dev->f_tuner;
0803     c->delivery_system = SYS_DVBT;
0804 
0805     dev_dbg(&pdev->dev, "frequency=%u bandwidth=%d\n",
0806         c->frequency, c->bandwidth_hz);
0807 
0808     if (!test_bit(POWER_ON, &dev->flags))
0809         return 0;
0810 
0811     if (!V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
0812         if (fe->ops.tuner_ops.set_params)
0813             fe->ops.tuner_ops.set_params(fe);
0814     }
0815 
0816     return 0;
0817 };
0818 
0819 static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
0820 {
0821     struct platform_device *pdev = dev->pdev;
0822     struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
0823     struct dvb_frontend *fe = pdata->dvb_frontend;
0824 
0825     dev_dbg(&pdev->dev, "\n");
0826 
0827     if (fe->ops.tuner_ops.init)
0828         fe->ops.tuner_ops.init(fe);
0829 
0830     return 0;
0831 };
0832 
0833 static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
0834 {
0835     struct platform_device *pdev = dev->pdev;
0836     struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
0837     struct dvb_frontend *fe = pdata->dvb_frontend;
0838 
0839     dev_dbg(&pdev->dev, "\n");
0840 
0841     if (fe->ops.tuner_ops.sleep)
0842         fe->ops.tuner_ops.sleep(fe);
0843 
0844     return;
0845 };
0846 
0847 static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
0848 {
0849     struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
0850     struct platform_device *pdev = dev->pdev;
0851     struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
0852     struct dvb_usb_device *d = pdata->dvb_usb_device;
0853     int ret;
0854 
0855     dev_dbg(&pdev->dev, "\n");
0856 
0857     if (!dev->udev)
0858         return -ENODEV;
0859 
0860     if (mutex_lock_interruptible(&dev->v4l2_lock))
0861         return -ERESTARTSYS;
0862 
0863     if (d->props->power_ctrl)
0864         d->props->power_ctrl(d, 1);
0865 
0866     /* enable ADC */
0867     if (d->props->frontend_ctrl)
0868         d->props->frontend_ctrl(pdata->dvb_frontend, 1);
0869 
0870     set_bit(POWER_ON, &dev->flags);
0871 
0872     /* wake-up tuner */
0873     if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
0874         ret = v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 1);
0875     else
0876         ret = rtl2832_sdr_set_tuner(dev);
0877     if (ret)
0878         goto err;
0879 
0880     ret = rtl2832_sdr_set_tuner_freq(dev);
0881     if (ret)
0882         goto err;
0883 
0884     ret = rtl2832_sdr_set_adc(dev);
0885     if (ret)
0886         goto err;
0887 
0888     ret = rtl2832_sdr_alloc_stream_bufs(dev);
0889     if (ret)
0890         goto err;
0891 
0892     ret = rtl2832_sdr_alloc_urbs(dev);
0893     if (ret)
0894         goto err;
0895 
0896     dev->sequence = 0;
0897 
0898     ret = rtl2832_sdr_submit_urbs(dev);
0899     if (ret)
0900         goto err;
0901 
0902 err:
0903     mutex_unlock(&dev->v4l2_lock);
0904 
0905     return ret;
0906 }
0907 
0908 static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
0909 {
0910     struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
0911     struct platform_device *pdev = dev->pdev;
0912     struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
0913     struct dvb_usb_device *d = pdata->dvb_usb_device;
0914 
0915     dev_dbg(&pdev->dev, "\n");
0916 
0917     mutex_lock(&dev->v4l2_lock);
0918 
0919     rtl2832_sdr_kill_urbs(dev);
0920     rtl2832_sdr_free_urbs(dev);
0921     rtl2832_sdr_free_stream_bufs(dev);
0922     rtl2832_sdr_cleanup_queued_bufs(dev);
0923     rtl2832_sdr_unset_adc(dev);
0924 
0925     /* sleep tuner */
0926     if (V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, core, s_power))
0927         v4l2_subdev_call(dev->v4l2_subdev, core, s_power, 0);
0928     else
0929         rtl2832_sdr_unset_tuner(dev);
0930 
0931     clear_bit(POWER_ON, &dev->flags);
0932 
0933     /* disable ADC */
0934     if (d->props->frontend_ctrl)
0935         d->props->frontend_ctrl(pdata->dvb_frontend, 0);
0936 
0937     if (d->props->power_ctrl)
0938         d->props->power_ctrl(d, 0);
0939 
0940     mutex_unlock(&dev->v4l2_lock);
0941 }
0942 
0943 static const struct vb2_ops rtl2832_sdr_vb2_ops = {
0944     .queue_setup            = rtl2832_sdr_queue_setup,
0945     .buf_prepare            = rtl2832_sdr_buf_prepare,
0946     .buf_queue              = rtl2832_sdr_buf_queue,
0947     .start_streaming        = rtl2832_sdr_start_streaming,
0948     .stop_streaming         = rtl2832_sdr_stop_streaming,
0949     .wait_prepare           = vb2_ops_wait_prepare,
0950     .wait_finish            = vb2_ops_wait_finish,
0951 };
0952 
0953 static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
0954         struct v4l2_tuner *v)
0955 {
0956     struct rtl2832_sdr_dev *dev = video_drvdata(file);
0957     struct platform_device *pdev = dev->pdev;
0958     int ret;
0959 
0960     dev_dbg(&pdev->dev, "index=%d type=%d\n", v->index, v->type);
0961 
0962     if (v->index == 0) {
0963         strscpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
0964         v->type = V4L2_TUNER_ADC;
0965         v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
0966         v->rangelow =   300000;
0967         v->rangehigh = 3200000;
0968         ret = 0;
0969     } else if (v->index == 1 &&
0970            V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_tuner)) {
0971         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_tuner, v);
0972     } else if (v->index == 1) {
0973         strscpy(v->name, "RF: <unknown>", sizeof(v->name));
0974         v->type = V4L2_TUNER_RF;
0975         v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
0976         v->rangelow =    50000000;
0977         v->rangehigh = 2000000000;
0978         ret = 0;
0979     } else {
0980         ret = -EINVAL;
0981     }
0982     return ret;
0983 }
0984 
0985 static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
0986         const struct v4l2_tuner *v)
0987 {
0988     struct rtl2832_sdr_dev *dev = video_drvdata(file);
0989     struct platform_device *pdev = dev->pdev;
0990     int ret;
0991 
0992     dev_dbg(&pdev->dev, "\n");
0993 
0994     if (v->index == 0) {
0995         ret = 0;
0996     } else if (v->index == 1 &&
0997            V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_tuner)) {
0998         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_tuner, v);
0999     } else if (v->index == 1) {
1000         ret = 0;
1001     } else {
1002         ret = -EINVAL;
1003     }
1004     return ret;
1005 }
1006 
1007 static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
1008         struct v4l2_frequency_band *band)
1009 {
1010     struct rtl2832_sdr_dev *dev = video_drvdata(file);
1011     struct platform_device *pdev = dev->pdev;
1012     int ret;
1013 
1014     dev_dbg(&pdev->dev, "tuner=%d type=%d index=%d\n",
1015         band->tuner, band->type, band->index);
1016 
1017     if (band->tuner == 0) {
1018         if (band->index >= ARRAY_SIZE(bands_adc))
1019             return -EINVAL;
1020 
1021         *band = bands_adc[band->index];
1022         ret = 0;
1023     } else if (band->tuner == 1 &&
1024            V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, enum_freq_bands)) {
1025         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, enum_freq_bands, band);
1026     } else if (band->tuner == 1) {
1027         if (band->index >= ARRAY_SIZE(bands_fm))
1028             return -EINVAL;
1029 
1030         *band = bands_fm[band->index];
1031         ret = 0;
1032     } else {
1033         ret = -EINVAL;
1034     }
1035     return ret;
1036 }
1037 
1038 static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
1039         struct v4l2_frequency *f)
1040 {
1041     struct rtl2832_sdr_dev *dev = video_drvdata(file);
1042     struct platform_device *pdev = dev->pdev;
1043     int ret;
1044 
1045     dev_dbg(&pdev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
1046 
1047     if (f->tuner == 0) {
1048         f->frequency = dev->f_adc;
1049         f->type = V4L2_TUNER_ADC;
1050         ret = 0;
1051     } else if (f->tuner == 1 &&
1052            V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, g_frequency)) {
1053         f->type = V4L2_TUNER_RF;
1054         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, g_frequency, f);
1055     } else if (f->tuner == 1) {
1056         f->frequency = dev->f_tuner;
1057         f->type = V4L2_TUNER_RF;
1058         ret = 0;
1059     } else {
1060         ret = -EINVAL;
1061     }
1062     return ret;
1063 }
1064 
1065 static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
1066         const struct v4l2_frequency *f)
1067 {
1068     struct rtl2832_sdr_dev *dev = video_drvdata(file);
1069     struct platform_device *pdev = dev->pdev;
1070     int ret, band;
1071 
1072     dev_dbg(&pdev->dev, "tuner=%d type=%d frequency=%u\n",
1073         f->tuner, f->type, f->frequency);
1074 
1075     /* ADC band midpoints */
1076     #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
1077     #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
1078 
1079     if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
1080         if (f->frequency < BAND_ADC_0)
1081             band = 0;
1082         else if (f->frequency < BAND_ADC_1)
1083             band = 1;
1084         else
1085             band = 2;
1086 
1087         dev->f_adc = clamp_t(unsigned int, f->frequency,
1088                      bands_adc[band].rangelow,
1089                      bands_adc[band].rangehigh);
1090 
1091         dev_dbg(&pdev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
1092         ret = rtl2832_sdr_set_adc(dev);
1093     } else if (f->tuner == 1 &&
1094            V4L2_SUBDEV_HAS_OP(dev->v4l2_subdev, tuner, s_frequency)) {
1095         ret = v4l2_subdev_call(dev->v4l2_subdev, tuner, s_frequency, f);
1096     } else if (f->tuner == 1) {
1097         dev->f_tuner = clamp_t(unsigned int, f->frequency,
1098                 bands_fm[0].rangelow,
1099                 bands_fm[0].rangehigh);
1100         dev_dbg(&pdev->dev, "RF frequency=%u Hz\n", f->frequency);
1101 
1102         ret = rtl2832_sdr_set_tuner_freq(dev);
1103     } else {
1104         ret = -EINVAL;
1105     }
1106     return ret;
1107 }
1108 
1109 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
1110         struct v4l2_fmtdesc *f)
1111 {
1112     struct rtl2832_sdr_dev *dev = video_drvdata(file);
1113     struct platform_device *pdev = dev->pdev;
1114 
1115     dev_dbg(&pdev->dev, "\n");
1116 
1117     if (f->index >= dev->num_formats)
1118         return -EINVAL;
1119 
1120     f->pixelformat = formats[f->index].pixelformat;
1121 
1122     return 0;
1123 }
1124 
1125 static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
1126         struct v4l2_format *f)
1127 {
1128     struct rtl2832_sdr_dev *dev = video_drvdata(file);
1129     struct platform_device *pdev = dev->pdev;
1130 
1131     dev_dbg(&pdev->dev, "\n");
1132 
1133     f->fmt.sdr.pixelformat = dev->pixelformat;
1134     f->fmt.sdr.buffersize = dev->buffersize;
1135 
1136     return 0;
1137 }
1138 
1139 static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
1140         struct v4l2_format *f)
1141 {
1142     struct rtl2832_sdr_dev *dev = video_drvdata(file);
1143     struct platform_device *pdev = dev->pdev;
1144     struct vb2_queue *q = &dev->vb_queue;
1145     int i;
1146 
1147     dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1148         (char *)&f->fmt.sdr.pixelformat);
1149 
1150     if (vb2_is_busy(q))
1151         return -EBUSY;
1152 
1153     for (i = 0; i < dev->num_formats; i++) {
1154         if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1155             dev->pixelformat = formats[i].pixelformat;
1156             dev->buffersize = formats[i].buffersize;
1157             f->fmt.sdr.buffersize = formats[i].buffersize;
1158             return 0;
1159         }
1160     }
1161 
1162     dev->pixelformat = formats[0].pixelformat;
1163     dev->buffersize = formats[0].buffersize;
1164     f->fmt.sdr.pixelformat = formats[0].pixelformat;
1165     f->fmt.sdr.buffersize = formats[0].buffersize;
1166 
1167     return 0;
1168 }
1169 
1170 static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
1171         struct v4l2_format *f)
1172 {
1173     struct rtl2832_sdr_dev *dev = video_drvdata(file);
1174     struct platform_device *pdev = dev->pdev;
1175     int i;
1176 
1177     dev_dbg(&pdev->dev, "pixelformat fourcc %4.4s\n",
1178         (char *)&f->fmt.sdr.pixelformat);
1179 
1180     for (i = 0; i < dev->num_formats; i++) {
1181         if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1182             f->fmt.sdr.buffersize = formats[i].buffersize;
1183             return 0;
1184         }
1185     }
1186 
1187     f->fmt.sdr.pixelformat = formats[0].pixelformat;
1188     f->fmt.sdr.buffersize = formats[0].buffersize;
1189 
1190     return 0;
1191 }
1192 
1193 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
1194     .vidioc_querycap          = rtl2832_sdr_querycap,
1195 
1196     .vidioc_enum_fmt_sdr_cap  = rtl2832_sdr_enum_fmt_sdr_cap,
1197     .vidioc_g_fmt_sdr_cap     = rtl2832_sdr_g_fmt_sdr_cap,
1198     .vidioc_s_fmt_sdr_cap     = rtl2832_sdr_s_fmt_sdr_cap,
1199     .vidioc_try_fmt_sdr_cap   = rtl2832_sdr_try_fmt_sdr_cap,
1200 
1201     .vidioc_reqbufs           = vb2_ioctl_reqbufs,
1202     .vidioc_create_bufs       = vb2_ioctl_create_bufs,
1203     .vidioc_prepare_buf       = vb2_ioctl_prepare_buf,
1204     .vidioc_querybuf          = vb2_ioctl_querybuf,
1205     .vidioc_qbuf              = vb2_ioctl_qbuf,
1206     .vidioc_dqbuf             = vb2_ioctl_dqbuf,
1207 
1208     .vidioc_streamon          = vb2_ioctl_streamon,
1209     .vidioc_streamoff         = vb2_ioctl_streamoff,
1210 
1211     .vidioc_g_tuner           = rtl2832_sdr_g_tuner,
1212     .vidioc_s_tuner           = rtl2832_sdr_s_tuner,
1213 
1214     .vidioc_enum_freq_bands   = rtl2832_sdr_enum_freq_bands,
1215     .vidioc_g_frequency       = rtl2832_sdr_g_frequency,
1216     .vidioc_s_frequency       = rtl2832_sdr_s_frequency,
1217 
1218     .vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
1219     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1220     .vidioc_log_status        = v4l2_ctrl_log_status,
1221 };
1222 
1223 static const struct v4l2_file_operations rtl2832_sdr_fops = {
1224     .owner                    = THIS_MODULE,
1225     .open                     = v4l2_fh_open,
1226     .release                  = vb2_fop_release,
1227     .read                     = vb2_fop_read,
1228     .poll                     = vb2_fop_poll,
1229     .mmap                     = vb2_fop_mmap,
1230     .unlocked_ioctl           = video_ioctl2,
1231 };
1232 
1233 static struct video_device rtl2832_sdr_template = {
1234     .name                     = "Realtek RTL2832 SDR",
1235     .release                  = video_device_release_empty,
1236     .fops                     = &rtl2832_sdr_fops,
1237     .ioctl_ops                = &rtl2832_sdr_ioctl_ops,
1238     .device_caps          = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
1239                     V4L2_CAP_READWRITE | V4L2_CAP_TUNER,
1240 };
1241 
1242 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
1243 {
1244     struct rtl2832_sdr_dev *dev =
1245             container_of(ctrl->handler, struct rtl2832_sdr_dev,
1246                     hdl);
1247     struct platform_device *pdev = dev->pdev;
1248     struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1249     struct dvb_frontend *fe = pdata->dvb_frontend;
1250     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1251     int ret;
1252 
1253     dev_dbg(&pdev->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1254         ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
1255         ctrl->step);
1256 
1257     switch (ctrl->id) {
1258     case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1259     case V4L2_CID_RF_TUNER_BANDWIDTH:
1260         /* TODO: these controls should be moved to tuner drivers */
1261         if (dev->bandwidth_auto->val) {
1262             /* Round towards the closest legal value */
1263             s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
1264             u32 offset;
1265 
1266             val = clamp_t(s32, val, dev->bandwidth->minimum,
1267                       dev->bandwidth->maximum);
1268             offset = val - dev->bandwidth->minimum;
1269             offset = dev->bandwidth->step *
1270                 div_u64(offset, dev->bandwidth->step);
1271             dev->bandwidth->val = dev->bandwidth->minimum + offset;
1272         }
1273         c->bandwidth_hz = dev->bandwidth->val;
1274 
1275         if (!test_bit(POWER_ON, &dev->flags))
1276             return 0;
1277 
1278         if (fe->ops.tuner_ops.set_params)
1279             ret = fe->ops.tuner_ops.set_params(fe);
1280         else
1281             ret = 0;
1282         break;
1283     default:
1284         ret = -EINVAL;
1285     }
1286 
1287     return ret;
1288 }
1289 
1290 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
1291     .s_ctrl = rtl2832_sdr_s_ctrl,
1292 };
1293 
1294 static void rtl2832_sdr_video_release(struct v4l2_device *v)
1295 {
1296     struct rtl2832_sdr_dev *dev =
1297             container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
1298     struct platform_device *pdev = dev->pdev;
1299 
1300     dev_dbg(&pdev->dev, "\n");
1301 
1302     v4l2_ctrl_handler_free(&dev->hdl);
1303     v4l2_device_unregister(&dev->v4l2_dev);
1304     kfree(dev);
1305 }
1306 
1307 /* Platform driver interface */
1308 static int rtl2832_sdr_probe(struct platform_device *pdev)
1309 {
1310     struct rtl2832_sdr_dev *dev;
1311     struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1312     const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
1313     struct v4l2_subdev *subdev;
1314     int ret;
1315 
1316     dev_dbg(&pdev->dev, "\n");
1317 
1318     if (!pdata) {
1319         dev_err(&pdev->dev, "Cannot proceed without platform data\n");
1320         ret = -EINVAL;
1321         goto err;
1322     }
1323     if (!pdev->dev.parent->driver) {
1324         dev_dbg(&pdev->dev, "No parent device\n");
1325         ret = -EINVAL;
1326         goto err;
1327     }
1328     /* try to refcount host drv since we are the consumer */
1329     if (!try_module_get(pdev->dev.parent->driver->owner)) {
1330         dev_err(&pdev->dev, "Refcount fail");
1331         ret = -EINVAL;
1332         goto err;
1333     }
1334     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1335     if (dev == NULL) {
1336         ret = -ENOMEM;
1337         goto err_module_put;
1338     }
1339 
1340     /* setup the state */
1341     subdev = pdata->v4l2_subdev;
1342     dev->v4l2_subdev = pdata->v4l2_subdev;
1343     dev->pdev = pdev;
1344     dev->regmap = pdata->regmap;
1345     dev->udev = pdata->dvb_usb_device->udev;
1346     dev->f_adc = bands_adc[0].rangelow;
1347     dev->f_tuner = bands_fm[0].rangelow;
1348     dev->pixelformat = formats[0].pixelformat;
1349     dev->buffersize = formats[0].buffersize;
1350     dev->num_formats = NUM_FORMATS;
1351     if (!rtl2832_sdr_emulated_fmt)
1352         dev->num_formats -= 1;
1353 
1354     mutex_init(&dev->v4l2_lock);
1355     mutex_init(&dev->vb_queue_lock);
1356     spin_lock_init(&dev->queued_bufs_lock);
1357     INIT_LIST_HEAD(&dev->queued_bufs);
1358 
1359     /* Init videobuf2 queue structure */
1360     dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1361     dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1362     dev->vb_queue.drv_priv = dev;
1363     dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
1364     dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
1365     dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1366     dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1367     ret = vb2_queue_init(&dev->vb_queue);
1368     if (ret) {
1369         dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
1370         goto err_kfree;
1371     }
1372 
1373     /* Register controls */
1374     switch (pdata->tuner) {
1375     case RTL2832_SDR_TUNER_E4000:
1376         v4l2_ctrl_handler_init(&dev->hdl, 9);
1377         if (subdev)
1378             v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1379                           NULL, true);
1380         break;
1381     case RTL2832_SDR_TUNER_R820T:
1382     case RTL2832_SDR_TUNER_R828D:
1383         v4l2_ctrl_handler_init(&dev->hdl, 2);
1384         dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1385                             V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1386                             0, 1, 1, 1);
1387         dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1388                            V4L2_CID_RF_TUNER_BANDWIDTH,
1389                            0, 8000000, 100000, 0);
1390         v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1391         break;
1392     case RTL2832_SDR_TUNER_FC0012:
1393     case RTL2832_SDR_TUNER_FC0013:
1394         v4l2_ctrl_handler_init(&dev->hdl, 2);
1395         dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1396                             V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1397                             0, 1, 1, 1);
1398         dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1399                            V4L2_CID_RF_TUNER_BANDWIDTH,
1400                            6000000, 8000000, 1000000,
1401                            6000000);
1402         v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1403         break;
1404     case RTL2832_SDR_TUNER_FC2580:
1405         v4l2_ctrl_handler_init(&dev->hdl, 2);
1406         if (subdev)
1407             v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler,
1408                           NULL, true);
1409         break;
1410     default:
1411         v4l2_ctrl_handler_init(&dev->hdl, 0);
1412         dev_err(&pdev->dev, "Unsupported tuner\n");
1413         ret = -ENODEV;
1414         goto err_v4l2_ctrl_handler_free;
1415     }
1416     if (dev->hdl.error) {
1417         ret = dev->hdl.error;
1418         dev_err(&pdev->dev, "Could not initialize controls\n");
1419         goto err_v4l2_ctrl_handler_free;
1420     }
1421 
1422     /* Init video_device structure */
1423     dev->vdev = rtl2832_sdr_template;
1424     dev->vdev.queue = &dev->vb_queue;
1425     dev->vdev.queue->lock = &dev->vb_queue_lock;
1426     video_set_drvdata(&dev->vdev, dev);
1427 
1428     /* Register the v4l2_device structure */
1429     dev->v4l2_dev.release = rtl2832_sdr_video_release;
1430     ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1431     if (ret) {
1432         dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
1433         goto err_v4l2_ctrl_handler_free;
1434     }
1435 
1436     dev->v4l2_dev.ctrl_handler = &dev->hdl;
1437     dev->vdev.v4l2_dev = &dev->v4l2_dev;
1438     dev->vdev.lock = &dev->v4l2_lock;
1439     dev->vdev.vfl_dir = VFL_DIR_RX;
1440 
1441     ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1442     if (ret) {
1443         dev_err(&pdev->dev, "Failed to register as video device %d\n",
1444             ret);
1445         goto err_v4l2_device_unregister;
1446     }
1447     dev_info(&pdev->dev, "Registered as %s\n",
1448          video_device_node_name(&dev->vdev));
1449     dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
1450     dev_notice(&pdev->dev,
1451            "SDR API is still slightly experimental and functionality changes may follow\n");
1452     platform_set_drvdata(pdev, dev);
1453     return 0;
1454 err_v4l2_device_unregister:
1455     v4l2_device_unregister(&dev->v4l2_dev);
1456 err_v4l2_ctrl_handler_free:
1457     v4l2_ctrl_handler_free(&dev->hdl);
1458 err_kfree:
1459     kfree(dev);
1460 err_module_put:
1461     module_put(pdev->dev.parent->driver->owner);
1462 err:
1463     return ret;
1464 }
1465 
1466 static int rtl2832_sdr_remove(struct platform_device *pdev)
1467 {
1468     struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
1469 
1470     dev_dbg(&pdev->dev, "\n");
1471 
1472     mutex_lock(&dev->vb_queue_lock);
1473     mutex_lock(&dev->v4l2_lock);
1474     /* No need to keep the urbs around after disconnection */
1475     dev->udev = NULL;
1476     v4l2_device_disconnect(&dev->v4l2_dev);
1477     video_unregister_device(&dev->vdev);
1478     mutex_unlock(&dev->v4l2_lock);
1479     mutex_unlock(&dev->vb_queue_lock);
1480     v4l2_device_put(&dev->v4l2_dev);
1481     module_put(pdev->dev.parent->driver->owner);
1482 
1483     return 0;
1484 }
1485 
1486 static struct platform_driver rtl2832_sdr_driver = {
1487     .driver = {
1488         .name   = "rtl2832_sdr",
1489     },
1490     .probe          = rtl2832_sdr_probe,
1491     .remove         = rtl2832_sdr_remove,
1492 };
1493 module_platform_driver(rtl2832_sdr_driver);
1494 
1495 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1496 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1497 MODULE_LICENSE("GPL");