2 * Realtek RTL2832U SDR driver
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * GNU Radio plugin "gr-kernel" for device usage will be on:
21 * http://git.linuxtv.org/anttip/gr-kernel.git
25 #include "dvb_frontend.h"
26 #include "rtl2832_sdr.h"
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-event.h>
33 #include <media/videobuf2-vmalloc.h>
35 #include <linux/platform_device.h>
36 #include <linux/jiffies.h>
37 #include <linux/math64.h>
39 static bool rtl2832_sdr_emulated_fmt;
40 module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644);
41 MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)");
43 #define MAX_BULK_BUFS (10)
44 #define BULK_BUFFER_SIZE (128 * 512)
46 static const struct v4l2_frequency_band bands_adc[] = {
49 .type = V4L2_TUNER_ADC,
51 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
57 .type = V4L2_TUNER_ADC,
59 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
65 .type = V4L2_TUNER_ADC,
67 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
73 static const struct v4l2_frequency_band bands_fm[] = {
76 .type = V4L2_TUNER_RF,
78 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
80 .rangehigh = 2000000000,
85 struct rtl2832_sdr_format {
91 static struct rtl2832_sdr_format formats[] = {
94 .pixelformat = V4L2_SDR_FMT_CU8,
95 .buffersize = BULK_BUFFER_SIZE,
97 .name = "Complex U16LE (emulated)",
98 .pixelformat = V4L2_SDR_FMT_CU16LE,
99 .buffersize = BULK_BUFFER_SIZE * 2,
103 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
105 /* intermediate buffers with raw data from the USB device */
106 struct rtl2832_sdr_frame_buf {
107 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */
108 struct list_head list;
111 struct rtl2832_sdr_dev {
112 #define POWER_ON (1 << 1)
113 #define URB_BUF (1 << 2)
116 struct platform_device *pdev;
117 struct dvb_frontend *fe;
118 struct dvb_usb_device *d;
119 struct i2c_adapter *i2c;
122 struct video_device vdev;
123 struct v4l2_device v4l2_dev;
125 /* videobuf2 queue and queued buffers list */
126 struct vb2_queue vb_queue;
127 struct list_head queued_bufs;
128 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
129 unsigned sequence; /* buffer sequence counter */
131 /* Note if taking both locks v4l2_lock must always be locked first! */
132 struct mutex v4l2_lock; /* Protects everything else */
133 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
135 /* Pointer to our usb_device, will be NULL after unplug */
136 struct usb_device *udev; /* Both mutexes most be hold when setting! */
138 unsigned int vb_full; /* vb is full and packets dropped */
140 struct urb *urb_list[MAX_BULK_BUFS];
142 unsigned long buf_size;
143 u8 *buf_list[MAX_BULK_BUFS];
144 dma_addr_t dma_addr[MAX_BULK_BUFS];
145 int urbs_initialized;
148 unsigned int f_adc, f_tuner;
151 unsigned int num_formats;
154 struct v4l2_ctrl_handler hdl;
155 struct v4l2_ctrl *bandwidth_auto;
156 struct v4l2_ctrl *bandwidth;
158 /* for sample rate calc */
160 unsigned int sample_measured;
161 unsigned long jiffies_next;
164 /* write multiple registers */
165 static int rtl2832_sdr_wr_regs(struct rtl2832_sdr_dev *dev, u16 reg,
166 const u8 *val, int len)
168 struct platform_device *pdev = dev->pdev;
169 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
170 struct i2c_client *client = pdata->i2c_client;
172 return pdata->bulk_write(client, reg, val, len);
176 /* read multiple registers */
177 static int rtl2832_sdr_rd_regs(struct rtl2832_sdr_dev *dev, u16 reg, u8 *val,
180 struct platform_device *pdev = dev->pdev;
181 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
182 struct i2c_client *client = pdata->i2c_client;
184 return pdata->bulk_read(client, reg, val, len);
188 /* write single register */
189 static int rtl2832_sdr_wr_reg(struct rtl2832_sdr_dev *dev, u16 reg, u8 val)
191 return rtl2832_sdr_wr_regs(dev, reg, &val, 1);
194 /* write single register with mask */
195 static int rtl2832_sdr_wr_reg_mask(struct rtl2832_sdr_dev *dev, u16 reg,
198 struct platform_device *pdev = dev->pdev;
199 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
200 struct i2c_client *client = pdata->i2c_client;
202 return pdata->update_bits(client, reg, mask, val);
205 /* Private functions */
206 static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf(
207 struct rtl2832_sdr_dev *dev)
210 struct rtl2832_sdr_frame_buf *buf = NULL;
212 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
213 if (list_empty(&dev->queued_bufs))
216 buf = list_entry(dev->queued_bufs.next,
217 struct rtl2832_sdr_frame_buf, list);
218 list_del(&buf->list);
220 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
224 static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_dev *dev,
225 void *dst, const u8 *src, unsigned int src_len)
227 unsigned int dst_len;
229 if (dev->pixelformat == V4L2_SDR_FMT_CU8) {
230 /* native stream, no need to convert */
231 memcpy(dst, src, src_len);
233 } else if (dev->pixelformat == V4L2_SDR_FMT_CU16LE) {
234 /* convert u8 to u16 */
238 for (i = 0; i < src_len; i++)
239 *u16dst++ = (src[i] << 8) | (src[i] >> 0);
240 dst_len = 2 * src_len;
245 /* calculate sample rate and output it in 10 seconds intervals */
246 if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
247 #define MSECS 10000UL
248 unsigned int msecs = jiffies_to_msecs(jiffies -
249 dev->jiffies_next + msecs_to_jiffies(MSECS));
250 unsigned int samples = dev->sample - dev->sample_measured;
252 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
253 dev->sample_measured = dev->sample;
254 dev_dbg(&dev->udev->dev,
255 "slen=%u samples=%u msecs=%u sample rate=%lu\n",
256 src_len, samples, msecs,
257 samples * 1000UL / msecs);
260 /* total number of I+Q pairs */
261 dev->sample += src_len / 2;
267 * This gets called for the bulk stream pipe. This is done in interrupt
268 * time, so it has to be fast, not crash, and not stall. Neat.
270 static void rtl2832_sdr_urb_complete(struct urb *urb)
272 struct rtl2832_sdr_dev *dev = urb->context;
273 struct rtl2832_sdr_frame_buf *fbuf;
275 dev_dbg_ratelimited(&dev->udev->dev,
276 "status=%d length=%d/%d errors=%d\n",
277 urb->status, urb->actual_length,
278 urb->transfer_buffer_length, urb->error_count);
280 switch (urb->status) {
281 case 0: /* success */
282 case -ETIMEDOUT: /* NAK */
284 case -ECONNRESET: /* kill */
289 dev_err_ratelimited(&dev->udev->dev, "urb failed=%d\n",
294 if (likely(urb->actual_length > 0)) {
297 /* get free framebuffer */
298 fbuf = rtl2832_sdr_get_next_fill_buf(dev);
299 if (unlikely(fbuf == NULL)) {
301 dev_notice_ratelimited(&dev->udev->dev,
302 "videobuf is full, %d packets dropped\n",
307 /* fill framebuffer */
308 ptr = vb2_plane_vaddr(&fbuf->vb, 0);
309 len = rtl2832_sdr_convert_stream(dev, ptr, urb->transfer_buffer,
311 vb2_set_plane_payload(&fbuf->vb, 0, len);
312 v4l2_get_timestamp(&fbuf->vb.v4l2_buf.timestamp);
313 fbuf->vb.v4l2_buf.sequence = dev->sequence++;
314 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
317 usb_submit_urb(urb, GFP_ATOMIC);
320 static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_dev *dev)
324 for (i = dev->urbs_submitted - 1; i >= 0; i--) {
325 dev_dbg(&dev->udev->dev, "kill urb=%d\n", i);
327 usb_kill_urb(dev->urb_list[i]);
329 dev->urbs_submitted = 0;
334 static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_dev *dev)
338 for (i = 0; i < dev->urbs_initialized; i++) {
339 dev_dbg(&dev->udev->dev, "submit urb=%d\n", i);
340 ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC);
342 dev_err(&dev->udev->dev,
343 "Could not submit urb no. %d - get them all back\n",
345 rtl2832_sdr_kill_urbs(dev);
348 dev->urbs_submitted++;
354 static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_dev *dev)
356 if (dev->flags & USB_STATE_URB_BUF) {
357 while (dev->buf_num) {
359 dev_dbg(&dev->udev->dev, "free buf=%d\n", dev->buf_num);
360 usb_free_coherent(dev->udev, dev->buf_size,
361 dev->buf_list[dev->buf_num],
362 dev->dma_addr[dev->buf_num]);
365 dev->flags &= ~USB_STATE_URB_BUF;
370 static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_dev *dev)
373 dev->buf_size = BULK_BUFFER_SIZE;
375 dev_dbg(&dev->udev->dev, "all in all I will use %u bytes for streaming\n",
376 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
378 for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
379 dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
380 BULK_BUFFER_SIZE, GFP_ATOMIC,
381 &dev->dma_addr[dev->buf_num]);
382 if (!dev->buf_list[dev->buf_num]) {
383 dev_dbg(&dev->udev->dev, "alloc buf=%d failed\n",
385 rtl2832_sdr_free_stream_bufs(dev);
389 dev_dbg(&dev->udev->dev, "alloc buf=%d %p (dma %llu)\n",
390 dev->buf_num, dev->buf_list[dev->buf_num],
391 (long long)dev->dma_addr[dev->buf_num]);
392 dev->flags |= USB_STATE_URB_BUF;
398 static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_dev *dev)
402 rtl2832_sdr_kill_urbs(dev);
404 for (i = dev->urbs_initialized - 1; i >= 0; i--) {
405 if (dev->urb_list[i]) {
406 dev_dbg(&dev->udev->dev, "free urb=%d\n", i);
408 usb_free_urb(dev->urb_list[i]);
411 dev->urbs_initialized = 0;
416 static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_dev *dev)
420 /* allocate the URBs */
421 for (i = 0; i < MAX_BULK_BUFS; i++) {
422 dev_dbg(&dev->udev->dev, "alloc urb=%d\n", i);
423 dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
424 if (!dev->urb_list[i]) {
425 dev_dbg(&dev->udev->dev, "failed\n");
426 for (j = 0; j < i; j++)
427 usb_free_urb(dev->urb_list[j]);
430 usb_fill_bulk_urb(dev->urb_list[i],
432 usb_rcvbulkpipe(dev->udev, 0x81),
435 rtl2832_sdr_urb_complete, dev);
437 dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
438 dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
439 dev->urbs_initialized++;
445 /* Must be called with vb_queue_lock hold */
446 static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_dev *dev)
450 dev_dbg(&dev->udev->dev, "\n");
452 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
453 while (!list_empty(&dev->queued_bufs)) {
454 struct rtl2832_sdr_frame_buf *buf;
456 buf = list_entry(dev->queued_bufs.next,
457 struct rtl2832_sdr_frame_buf, list);
458 list_del(&buf->list);
459 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
461 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
464 static int rtl2832_sdr_querycap(struct file *file, void *fh,
465 struct v4l2_capability *cap)
467 struct rtl2832_sdr_dev *dev = video_drvdata(file);
469 dev_dbg(&dev->udev->dev, "\n");
471 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
472 strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
473 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
474 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
475 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
476 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
480 /* Videobuf2 operations */
481 static int rtl2832_sdr_queue_setup(struct vb2_queue *vq,
482 const struct v4l2_format *fmt, unsigned int *nbuffers,
483 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
485 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
487 dev_dbg(&dev->udev->dev, "nbuffers=%d\n", *nbuffers);
489 /* Need at least 8 buffers */
490 if (vq->num_buffers + *nbuffers < 8)
491 *nbuffers = 8 - vq->num_buffers;
493 sizes[0] = PAGE_ALIGN(dev->buffersize);
494 dev_dbg(&dev->udev->dev, "nbuffers=%d sizes[0]=%d\n",
495 *nbuffers, sizes[0]);
499 static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb)
501 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
503 /* Don't allow queing new buffers after device disconnection */
510 static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb)
512 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
513 struct rtl2832_sdr_frame_buf *buf =
514 container_of(vb, struct rtl2832_sdr_frame_buf, vb);
517 /* Check the device has not disconnected between prep and queuing */
519 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
523 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
524 list_add_tail(&buf->list, &dev->queued_bufs);
525 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
528 static int rtl2832_sdr_set_adc(struct rtl2832_sdr_dev *dev)
530 struct platform_device *pdev = dev->pdev;
531 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
532 struct dvb_frontend *fe = dev->fe;
534 unsigned int f_sr, f_if;
535 u8 buf[4], u8tmp1, u8tmp2;
539 dev_dbg(&dev->udev->dev, "f_adc=%u\n", dev->f_adc);
541 if (!test_bit(POWER_ON, &dev->flags))
549 ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x00\x00", 2);
553 ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x00\x00\x00\x00", 4);
557 /* get IF from tuner */
558 if (fe->ops.tuner_ops.get_if_frequency)
559 ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if);
567 u64tmp = f_if % pdata->clk;
569 u64tmp = div_u64(u64tmp, pdata->clk);
571 u32tmp = u64tmp & 0x3fffff;
573 dev_dbg(&dev->udev->dev, "f_if=%u if_ctl=%08x\n", f_if, u32tmp);
575 buf[0] = (u32tmp >> 16) & 0xff;
576 buf[1] = (u32tmp >> 8) & 0xff;
577 buf[2] = (u32tmp >> 0) & 0xff;
579 ret = rtl2832_sdr_wr_regs(dev, 0x119, buf, 3);
584 /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */
586 u8tmp1 = 0x1a; /* disable Zero-IF */
587 u8tmp2 = 0x8d; /* enable ADC I */
589 u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */
590 u8tmp2 = 0xcd; /* enable ADC I, ADC Q */
593 ret = rtl2832_sdr_wr_reg(dev, 0x1b1, u8tmp1);
597 ret = rtl2832_sdr_wr_reg(dev, 0x008, u8tmp2);
601 ret = rtl2832_sdr_wr_reg(dev, 0x006, 0x80);
605 /* program sampling rate (resampling down) */
606 u32tmp = div_u64(pdata->clk * 0x400000ULL, f_sr * 4U);
608 buf[0] = (u32tmp >> 24) & 0xff;
609 buf[1] = (u32tmp >> 16) & 0xff;
610 buf[2] = (u32tmp >> 8) & 0xff;
611 buf[3] = (u32tmp >> 0) & 0xff;
612 ret = rtl2832_sdr_wr_regs(dev, 0x19f, buf, 4);
616 /* low-pass filter */
617 ret = rtl2832_sdr_wr_regs(dev, 0x11c,
618 "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5",
623 ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
628 ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x05", 1);
632 ret = rtl2832_sdr_wr_regs(dev, 0x01a, "\x1b\x16\x0d\x06\x01\xff", 6);
637 ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\xf0\x0f", 3);
642 ret = rtl2832_sdr_wr_regs(dev, 0x061, "\x60", 1);
646 /* used RF tuner based settings */
647 switch (pdata->tuner) {
648 case RTL2832_SDR_TUNER_E4000:
649 ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
650 ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
651 ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
652 ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x30", 1);
653 ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xd0", 1);
654 ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
655 ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x18", 1);
656 ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
657 ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
658 ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
659 ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
660 ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
661 ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
662 ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
663 ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
664 ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
665 ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
666 ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
667 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
668 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
669 ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xd4", 1);
670 ret = rtl2832_sdr_wr_regs(dev, 0x1e5, "\xf0", 1);
671 ret = rtl2832_sdr_wr_regs(dev, 0x1d9, "\x00", 1);
672 ret = rtl2832_sdr_wr_regs(dev, 0x1db, "\x00", 1);
673 ret = rtl2832_sdr_wr_regs(dev, 0x1dd, "\x14", 1);
674 ret = rtl2832_sdr_wr_regs(dev, 0x1de, "\xec", 1);
675 ret = rtl2832_sdr_wr_regs(dev, 0x1d8, "\x0c", 1);
676 ret = rtl2832_sdr_wr_regs(dev, 0x1e6, "\x02", 1);
677 ret = rtl2832_sdr_wr_regs(dev, 0x1d7, "\x09", 1);
678 ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x83", 1);
679 ret = rtl2832_sdr_wr_regs(dev, 0x010, "\x49", 1);
680 ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x87", 1);
681 ret = rtl2832_sdr_wr_regs(dev, 0x00d, "\x85", 1);
682 ret = rtl2832_sdr_wr_regs(dev, 0x013, "\x02", 1);
684 case RTL2832_SDR_TUNER_FC0012:
685 case RTL2832_SDR_TUNER_FC0013:
686 ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
687 ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
688 ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x5a", 1);
689 ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x2c", 1);
690 ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
691 ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
692 ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x16", 1);
693 ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
694 ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
695 ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
696 ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
697 ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
698 ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
699 ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
700 ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
701 ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
702 ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
703 ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
704 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
705 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
706 ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xe9\xbf", 2);
707 ret = rtl2832_sdr_wr_regs(dev, 0x1e5, "\xf0", 1);
708 ret = rtl2832_sdr_wr_regs(dev, 0x1d9, "\x00", 1);
709 ret = rtl2832_sdr_wr_regs(dev, 0x1db, "\x00", 1);
710 ret = rtl2832_sdr_wr_regs(dev, 0x1dd, "\x11", 1);
711 ret = rtl2832_sdr_wr_regs(dev, 0x1de, "\xef", 1);
712 ret = rtl2832_sdr_wr_regs(dev, 0x1d8, "\x0c", 1);
713 ret = rtl2832_sdr_wr_regs(dev, 0x1e6, "\x02", 1);
714 ret = rtl2832_sdr_wr_regs(dev, 0x1d7, "\x09", 1);
716 case RTL2832_SDR_TUNER_R820T:
717 case RTL2832_SDR_TUNER_R828D:
718 ret = rtl2832_sdr_wr_regs(dev, 0x112, "\x5a", 1);
719 ret = rtl2832_sdr_wr_regs(dev, 0x102, "\x40", 1);
720 ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x01", 1);
721 ret = rtl2832_sdr_wr_regs(dev, 0x103, "\x80", 1);
722 ret = rtl2832_sdr_wr_regs(dev, 0x1c7, "\x24", 1);
723 ret = rtl2832_sdr_wr_regs(dev, 0x104, "\xcc", 1);
724 ret = rtl2832_sdr_wr_regs(dev, 0x105, "\xbe", 1);
725 ret = rtl2832_sdr_wr_regs(dev, 0x1c8, "\x14", 1);
726 ret = rtl2832_sdr_wr_regs(dev, 0x106, "\x35", 1);
727 ret = rtl2832_sdr_wr_regs(dev, 0x1c9, "\x21", 1);
728 ret = rtl2832_sdr_wr_regs(dev, 0x1ca, "\x21", 1);
729 ret = rtl2832_sdr_wr_regs(dev, 0x1cb, "\x00", 1);
730 ret = rtl2832_sdr_wr_regs(dev, 0x107, "\x40", 1);
731 ret = rtl2832_sdr_wr_regs(dev, 0x1cd, "\x10", 1);
732 ret = rtl2832_sdr_wr_regs(dev, 0x1ce, "\x10", 1);
733 ret = rtl2832_sdr_wr_regs(dev, 0x108, "\x80", 1);
734 ret = rtl2832_sdr_wr_regs(dev, 0x109, "\x7f", 1);
735 ret = rtl2832_sdr_wr_regs(dev, 0x10a, "\x80", 1);
736 ret = rtl2832_sdr_wr_regs(dev, 0x10b, "\x7f", 1);
737 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
738 ret = rtl2832_sdr_wr_regs(dev, 0x00e, "\xfc", 1);
739 ret = rtl2832_sdr_wr_regs(dev, 0x011, "\xf4", 1);
742 dev_notice(&dev->udev->dev, "Unsupported tuner\n");
746 ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x04, 0x04);
750 ret = rtl2832_sdr_wr_reg_mask(dev, 0x101, 0x00, 0x04);
757 static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_dev *dev)
761 dev_dbg(&dev->udev->dev, "\n");
764 ret = rtl2832_sdr_wr_regs(dev, 0x061, "\xe0", 1);
769 ret = rtl2832_sdr_wr_regs(dev, 0x019, "\x20", 1);
773 ret = rtl2832_sdr_wr_regs(dev, 0x017, "\x11\x10", 2);
778 ret = rtl2832_sdr_wr_regs(dev, 0x192, "\x00\x0f\xff", 3);
782 ret = rtl2832_sdr_wr_regs(dev, 0x13e, "\x40\x00", 2);
786 ret = rtl2832_sdr_wr_regs(dev, 0x115, "\x06\x3f\xce\xcc", 4);
793 static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_dev *dev)
795 struct dvb_frontend *fe = dev->fe;
796 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
797 struct v4l2_ctrl *bandwidth_auto;
798 struct v4l2_ctrl *bandwidth;
803 if (dev->f_tuner == 0)
809 bandwidth_auto = v4l2_ctrl_find(&dev->hdl,
810 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
811 bandwidth = v4l2_ctrl_find(&dev->hdl, V4L2_CID_RF_TUNER_BANDWIDTH);
812 if (v4l2_ctrl_g_ctrl(bandwidth_auto)) {
813 c->bandwidth_hz = dev->f_adc;
814 v4l2_ctrl_s_ctrl(bandwidth, dev->f_adc);
816 c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth);
819 c->frequency = dev->f_tuner;
820 c->delivery_system = SYS_DVBT;
822 dev_dbg(&dev->udev->dev, "frequency=%u bandwidth=%d\n",
823 c->frequency, c->bandwidth_hz);
825 if (!test_bit(POWER_ON, &dev->flags))
828 if (fe->ops.tuner_ops.set_params)
829 fe->ops.tuner_ops.set_params(fe);
834 static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_dev *dev)
836 struct dvb_frontend *fe = dev->fe;
838 dev_dbg(&dev->udev->dev, "\n");
840 if (fe->ops.tuner_ops.init)
841 fe->ops.tuner_ops.init(fe);
846 static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_dev *dev)
848 struct dvb_frontend *fe = dev->fe;
850 dev_dbg(&dev->udev->dev, "\n");
852 if (fe->ops.tuner_ops.sleep)
853 fe->ops.tuner_ops.sleep(fe);
858 static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count)
860 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
863 dev_dbg(&dev->udev->dev, "\n");
868 if (mutex_lock_interruptible(&dev->v4l2_lock))
871 if (dev->d->props->power_ctrl)
872 dev->d->props->power_ctrl(dev->d, 1);
875 if (dev->d->props->frontend_ctrl)
876 dev->d->props->frontend_ctrl(dev->fe, 1);
878 set_bit(POWER_ON, &dev->flags);
880 ret = rtl2832_sdr_set_tuner(dev);
884 ret = rtl2832_sdr_set_tuner_freq(dev);
888 ret = rtl2832_sdr_set_adc(dev);
892 ret = rtl2832_sdr_alloc_stream_bufs(dev);
896 ret = rtl2832_sdr_alloc_urbs(dev);
902 ret = rtl2832_sdr_submit_urbs(dev);
907 mutex_unlock(&dev->v4l2_lock);
912 static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq)
914 struct rtl2832_sdr_dev *dev = vb2_get_drv_priv(vq);
916 dev_dbg(&dev->udev->dev, "\n");
918 mutex_lock(&dev->v4l2_lock);
920 rtl2832_sdr_kill_urbs(dev);
921 rtl2832_sdr_free_urbs(dev);
922 rtl2832_sdr_free_stream_bufs(dev);
923 rtl2832_sdr_cleanup_queued_bufs(dev);
924 rtl2832_sdr_unset_adc(dev);
925 rtl2832_sdr_unset_tuner(dev);
927 clear_bit(POWER_ON, &dev->flags);
930 if (dev->d->props->frontend_ctrl)
931 dev->d->props->frontend_ctrl(dev->fe, 0);
933 if (dev->d->props->power_ctrl)
934 dev->d->props->power_ctrl(dev->d, 0);
936 mutex_unlock(&dev->v4l2_lock);
939 static struct vb2_ops rtl2832_sdr_vb2_ops = {
940 .queue_setup = rtl2832_sdr_queue_setup,
941 .buf_prepare = rtl2832_sdr_buf_prepare,
942 .buf_queue = rtl2832_sdr_buf_queue,
943 .start_streaming = rtl2832_sdr_start_streaming,
944 .stop_streaming = rtl2832_sdr_stop_streaming,
945 .wait_prepare = vb2_ops_wait_prepare,
946 .wait_finish = vb2_ops_wait_finish,
949 static int rtl2832_sdr_g_tuner(struct file *file, void *priv,
950 struct v4l2_tuner *v)
952 struct rtl2832_sdr_dev *dev = video_drvdata(file);
954 dev_dbg(&dev->udev->dev, "index=%d type=%d\n", v->index, v->type);
957 strlcpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name));
958 v->type = V4L2_TUNER_ADC;
959 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
960 v->rangelow = 300000;
961 v->rangehigh = 3200000;
962 } else if (v->index == 1) {
963 strlcpy(v->name, "RF: <unknown>", sizeof(v->name));
964 v->type = V4L2_TUNER_RF;
965 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
966 v->rangelow = 50000000;
967 v->rangehigh = 2000000000;
975 static int rtl2832_sdr_s_tuner(struct file *file, void *priv,
976 const struct v4l2_tuner *v)
978 struct rtl2832_sdr_dev *dev = video_drvdata(file);
980 dev_dbg(&dev->udev->dev, "\n");
987 static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv,
988 struct v4l2_frequency_band *band)
990 struct rtl2832_sdr_dev *dev = video_drvdata(file);
992 dev_dbg(&dev->udev->dev, "tuner=%d type=%d index=%d\n",
993 band->tuner, band->type, band->index);
995 if (band->tuner == 0) {
996 if (band->index >= ARRAY_SIZE(bands_adc))
999 *band = bands_adc[band->index];
1000 } else if (band->tuner == 1) {
1001 if (band->index >= ARRAY_SIZE(bands_fm))
1004 *band = bands_fm[band->index];
1012 static int rtl2832_sdr_g_frequency(struct file *file, void *priv,
1013 struct v4l2_frequency *f)
1015 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1018 dev_dbg(&dev->udev->dev, "tuner=%d type=%d\n",
1021 if (f->tuner == 0) {
1022 f->frequency = dev->f_adc;
1023 f->type = V4L2_TUNER_ADC;
1024 } else if (f->tuner == 1) {
1025 f->frequency = dev->f_tuner;
1026 f->type = V4L2_TUNER_RF;
1034 static int rtl2832_sdr_s_frequency(struct file *file, void *priv,
1035 const struct v4l2_frequency *f)
1037 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1040 dev_dbg(&dev->udev->dev, "tuner=%d type=%d frequency=%u\n",
1041 f->tuner, f->type, f->frequency);
1043 /* ADC band midpoints */
1044 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
1045 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
1047 if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) {
1048 if (f->frequency < BAND_ADC_0)
1050 else if (f->frequency < BAND_ADC_1)
1055 dev->f_adc = clamp_t(unsigned int, f->frequency,
1056 bands_adc[band].rangelow,
1057 bands_adc[band].rangehigh);
1059 dev_dbg(&dev->udev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
1060 ret = rtl2832_sdr_set_adc(dev);
1061 } else if (f->tuner == 1) {
1062 dev->f_tuner = clamp_t(unsigned int, f->frequency,
1063 bands_fm[0].rangelow,
1064 bands_fm[0].rangehigh);
1065 dev_dbg(&dev->udev->dev, "RF frequency=%u Hz\n", f->frequency);
1067 ret = rtl2832_sdr_set_tuner_freq(dev);
1075 static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv,
1076 struct v4l2_fmtdesc *f)
1078 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1080 dev_dbg(&dev->udev->dev, "\n");
1082 if (f->index >= dev->num_formats)
1085 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1086 f->pixelformat = formats[f->index].pixelformat;
1091 static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv,
1092 struct v4l2_format *f)
1094 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1096 dev_dbg(&dev->udev->dev, "\n");
1098 f->fmt.sdr.pixelformat = dev->pixelformat;
1099 f->fmt.sdr.buffersize = dev->buffersize;
1101 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1106 static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv,
1107 struct v4l2_format *f)
1109 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1110 struct vb2_queue *q = &dev->vb_queue;
1113 dev_dbg(&dev->udev->dev, "pixelformat fourcc %4.4s\n",
1114 (char *)&f->fmt.sdr.pixelformat);
1119 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1120 for (i = 0; i < dev->num_formats; i++) {
1121 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1122 dev->pixelformat = formats[i].pixelformat;
1123 dev->buffersize = formats[i].buffersize;
1124 f->fmt.sdr.buffersize = formats[i].buffersize;
1129 dev->pixelformat = formats[0].pixelformat;
1130 dev->buffersize = formats[0].buffersize;
1131 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1132 f->fmt.sdr.buffersize = formats[0].buffersize;
1137 static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv,
1138 struct v4l2_format *f)
1140 struct rtl2832_sdr_dev *dev = video_drvdata(file);
1143 dev_dbg(&dev->udev->dev, "pixelformat fourcc %4.4s\n",
1144 (char *)&f->fmt.sdr.pixelformat);
1146 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1147 for (i = 0; i < dev->num_formats; i++) {
1148 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1149 f->fmt.sdr.buffersize = formats[i].buffersize;
1154 f->fmt.sdr.pixelformat = formats[0].pixelformat;
1155 f->fmt.sdr.buffersize = formats[0].buffersize;
1160 static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = {
1161 .vidioc_querycap = rtl2832_sdr_querycap,
1163 .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap,
1164 .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap,
1165 .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap,
1166 .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap,
1168 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1169 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1170 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1171 .vidioc_querybuf = vb2_ioctl_querybuf,
1172 .vidioc_qbuf = vb2_ioctl_qbuf,
1173 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1175 .vidioc_streamon = vb2_ioctl_streamon,
1176 .vidioc_streamoff = vb2_ioctl_streamoff,
1178 .vidioc_g_tuner = rtl2832_sdr_g_tuner,
1179 .vidioc_s_tuner = rtl2832_sdr_s_tuner,
1181 .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands,
1182 .vidioc_g_frequency = rtl2832_sdr_g_frequency,
1183 .vidioc_s_frequency = rtl2832_sdr_s_frequency,
1185 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1186 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1187 .vidioc_log_status = v4l2_ctrl_log_status,
1190 static const struct v4l2_file_operations rtl2832_sdr_fops = {
1191 .owner = THIS_MODULE,
1192 .open = v4l2_fh_open,
1193 .release = vb2_fop_release,
1194 .read = vb2_fop_read,
1195 .poll = vb2_fop_poll,
1196 .mmap = vb2_fop_mmap,
1197 .unlocked_ioctl = video_ioctl2,
1200 static struct video_device rtl2832_sdr_template = {
1201 .name = "Realtek RTL2832 SDR",
1202 .release = video_device_release_empty,
1203 .fops = &rtl2832_sdr_fops,
1204 .ioctl_ops = &rtl2832_sdr_ioctl_ops,
1207 static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl)
1209 struct rtl2832_sdr_dev *dev =
1210 container_of(ctrl->handler, struct rtl2832_sdr_dev,
1212 struct dvb_frontend *fe = dev->fe;
1213 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1216 dev_dbg(&dev->udev->dev,
1217 "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
1218 ctrl->id, ctrl->name, ctrl->val,
1219 ctrl->minimum, ctrl->maximum, ctrl->step);
1222 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1223 case V4L2_CID_RF_TUNER_BANDWIDTH:
1224 /* TODO: these controls should be moved to tuner drivers */
1225 if (dev->bandwidth_auto->val) {
1226 /* Round towards the closest legal value */
1227 s32 val = dev->f_adc + div_u64(dev->bandwidth->step, 2);
1230 val = clamp_t(s32, val, dev->bandwidth->minimum,
1231 dev->bandwidth->maximum);
1232 offset = val - dev->bandwidth->minimum;
1233 offset = dev->bandwidth->step *
1234 div_u64(offset, dev->bandwidth->step);
1235 dev->bandwidth->val = dev->bandwidth->minimum + offset;
1237 c->bandwidth_hz = dev->bandwidth->val;
1239 if (!test_bit(POWER_ON, &dev->flags))
1242 if (fe->ops.tuner_ops.set_params)
1243 ret = fe->ops.tuner_ops.set_params(fe);
1254 static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = {
1255 .s_ctrl = rtl2832_sdr_s_ctrl,
1258 static void rtl2832_sdr_video_release(struct v4l2_device *v)
1260 struct rtl2832_sdr_dev *dev =
1261 container_of(v, struct rtl2832_sdr_dev, v4l2_dev);
1262 struct platform_device *pdev = dev->pdev;
1264 dev_dbg(&pdev->dev, "\n");
1266 v4l2_ctrl_handler_free(&dev->hdl);
1267 v4l2_device_unregister(&dev->v4l2_dev);
1271 /* Platform driver interface */
1272 static int rtl2832_sdr_probe(struct platform_device *pdev)
1274 struct rtl2832_sdr_dev *dev;
1275 struct rtl2832_sdr_platform_data *pdata = pdev->dev.platform_data;
1276 const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops;
1277 struct v4l2_subdev *subdev;
1280 dev_dbg(&pdev->dev, "\n");
1283 dev_err(&pdev->dev, "Cannot proceed without platform data\n");
1287 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1290 "Could not allocate memory for rtl2832_sdr_dev\n");
1295 /* setup the state */
1296 subdev = pdata->v4l2_subdev;
1298 dev->fe = pdata->dvb_frontend;
1299 dev->d = pdata->dvb_usb_device;
1300 dev->udev = pdata->dvb_usb_device->udev;
1301 dev->i2c = pdata->i2c_client->adapter;
1302 dev->f_adc = bands_adc[0].rangelow;
1303 dev->f_tuner = bands_fm[0].rangelow;
1304 dev->pixelformat = formats[0].pixelformat;
1305 dev->buffersize = formats[0].buffersize;
1306 dev->num_formats = NUM_FORMATS;
1307 if (!rtl2832_sdr_emulated_fmt)
1308 dev->num_formats -= 1;
1310 mutex_init(&dev->v4l2_lock);
1311 mutex_init(&dev->vb_queue_lock);
1312 spin_lock_init(&dev->queued_bufs_lock);
1313 INIT_LIST_HEAD(&dev->queued_bufs);
1315 /* Init videobuf2 queue structure */
1316 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1317 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1318 dev->vb_queue.drv_priv = dev;
1319 dev->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf);
1320 dev->vb_queue.ops = &rtl2832_sdr_vb2_ops;
1321 dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1322 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1323 ret = vb2_queue_init(&dev->vb_queue);
1325 dev_err(&pdev->dev, "Could not initialize vb2 queue\n");
1329 /* Register controls */
1330 switch (pdata->tuner) {
1331 case RTL2832_SDR_TUNER_E4000:
1332 v4l2_ctrl_handler_init(&dev->hdl, 9);
1334 v4l2_ctrl_add_handler(&dev->hdl, subdev->ctrl_handler, NULL);
1336 case RTL2832_SDR_TUNER_R820T:
1337 case RTL2832_SDR_TUNER_R828D:
1338 v4l2_ctrl_handler_init(&dev->hdl, 2);
1339 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1340 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1342 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1343 V4L2_CID_RF_TUNER_BANDWIDTH,
1344 0, 8000000, 100000, 0);
1345 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1347 case RTL2832_SDR_TUNER_FC0012:
1348 case RTL2832_SDR_TUNER_FC0013:
1349 v4l2_ctrl_handler_init(&dev->hdl, 2);
1350 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, ops,
1351 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1353 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, ops,
1354 V4L2_CID_RF_TUNER_BANDWIDTH,
1355 6000000, 8000000, 1000000,
1357 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1360 v4l2_ctrl_handler_init(&dev->hdl, 0);
1361 dev_err(&pdev->dev, "Unsupported tuner\n");
1362 goto err_v4l2_ctrl_handler_free;
1364 if (dev->hdl.error) {
1365 ret = dev->hdl.error;
1366 dev_err(&pdev->dev, "Could not initialize controls\n");
1367 goto err_v4l2_ctrl_handler_free;
1370 /* Init video_device structure */
1371 dev->vdev = rtl2832_sdr_template;
1372 dev->vdev.queue = &dev->vb_queue;
1373 dev->vdev.queue->lock = &dev->vb_queue_lock;
1374 video_set_drvdata(&dev->vdev, dev);
1376 /* Register the v4l2_device structure */
1377 dev->v4l2_dev.release = rtl2832_sdr_video_release;
1378 ret = v4l2_device_register(&dev->udev->dev, &dev->v4l2_dev);
1380 dev_err(&pdev->dev, "Failed to register v4l2-device %d\n", ret);
1381 goto err_v4l2_ctrl_handler_free;
1384 dev->v4l2_dev.ctrl_handler = &dev->hdl;
1385 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1386 dev->vdev.lock = &dev->v4l2_lock;
1387 dev->vdev.vfl_dir = VFL_DIR_RX;
1389 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1391 dev_err(&pdev->dev, "Failed to register as video device %d\n",
1393 goto err_v4l2_device_unregister;
1395 dev_info(&pdev->dev, "Registered as %s\n",
1396 video_device_node_name(&dev->vdev));
1397 dev_info(&pdev->dev, "Realtek RTL2832 SDR attached\n");
1398 dev_notice(&pdev->dev,
1399 "SDR API is still slightly experimental and functionality changes may follow\n");
1400 platform_set_drvdata(pdev, dev);
1402 err_v4l2_device_unregister:
1403 v4l2_device_unregister(&dev->v4l2_dev);
1404 err_v4l2_ctrl_handler_free:
1405 v4l2_ctrl_handler_free(&dev->hdl);
1412 static int rtl2832_sdr_remove(struct platform_device *pdev)
1414 struct rtl2832_sdr_dev *dev = platform_get_drvdata(pdev);
1416 dev_dbg(&pdev->dev, "\n");
1418 mutex_lock(&dev->vb_queue_lock);
1419 mutex_lock(&dev->v4l2_lock);
1420 /* No need to keep the urbs around after disconnection */
1422 v4l2_device_disconnect(&dev->v4l2_dev);
1423 video_unregister_device(&dev->vdev);
1424 mutex_unlock(&dev->v4l2_lock);
1425 mutex_unlock(&dev->vb_queue_lock);
1427 v4l2_device_put(&dev->v4l2_dev);
1432 static struct platform_driver rtl2832_sdr_driver = {
1434 .name = "rtl2832_sdr",
1435 .owner = THIS_MODULE,
1437 .probe = rtl2832_sdr_probe,
1438 .remove = rtl2832_sdr_remove,
1440 module_platform_driver(rtl2832_sdr_driver);
1442 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1443 MODULE_DESCRIPTION("Realtek RTL2832 SDR driver");
1444 MODULE_LICENSE("GPL");