Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Realtek RTL2832U SDR driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Antti Palosaari <crope@iki.fi> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 19 | * |
| 20 | * GNU Radio plugin "gr-kernel" for device usage will be on: |
| 21 | * http://git.linuxtv.org/anttip/gr-kernel.git |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "dvb_frontend.h" |
| 26 | #include "rtl2832_sdr.h" |
| 27 | #include "dvb_usb.h" |
| 28 | |
| 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> |
| 34 | |
| 35 | #include <linux/jiffies.h> |
| 36 | #include <linux/math64.h> |
| 37 | |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 38 | static bool rtl2832_sdr_emulated_fmt; |
| 39 | module_param_named(emulated_formats, rtl2832_sdr_emulated_fmt, bool, 0644); |
| 40 | MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)"); |
| 41 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 42 | #define MAX_BULK_BUFS (10) |
| 43 | #define BULK_BUFFER_SIZE (128 * 512) |
| 44 | |
| 45 | static const struct v4l2_frequency_band bands_adc[] = { |
| 46 | { |
| 47 | .tuner = 0, |
| 48 | .type = V4L2_TUNER_ADC, |
| 49 | .index = 0, |
| 50 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 51 | .rangelow = 300000, |
| 52 | .rangehigh = 300000, |
| 53 | }, |
| 54 | { |
| 55 | .tuner = 0, |
| 56 | .type = V4L2_TUNER_ADC, |
| 57 | .index = 1, |
| 58 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 59 | .rangelow = 900001, |
| 60 | .rangehigh = 2800000, |
| 61 | }, |
| 62 | { |
| 63 | .tuner = 0, |
| 64 | .type = V4L2_TUNER_ADC, |
| 65 | .index = 2, |
| 66 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 67 | .rangelow = 3200000, |
| 68 | .rangehigh = 3200000, |
| 69 | }, |
| 70 | }; |
| 71 | |
| 72 | static const struct v4l2_frequency_band bands_fm[] = { |
| 73 | { |
| 74 | .tuner = 1, |
| 75 | .type = V4L2_TUNER_RF, |
| 76 | .index = 0, |
| 77 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 78 | .rangelow = 50000000, |
| 79 | .rangehigh = 2000000000, |
| 80 | }, |
| 81 | }; |
| 82 | |
| 83 | /* stream formats */ |
| 84 | struct rtl2832_sdr_format { |
| 85 | char *name; |
| 86 | u32 pixelformat; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 87 | u32 buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static struct rtl2832_sdr_format formats[] = { |
| 91 | { |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 92 | .name = "Complex U8", |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 93 | .pixelformat = V4L2_SDR_FMT_CU8, |
| 94 | .buffersize = BULK_BUFFER_SIZE, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 95 | }, { |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 96 | .name = "Complex U16LE (emulated)", |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 97 | .pixelformat = V4L2_SDR_FMT_CU16LE, |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 98 | .buffersize = BULK_BUFFER_SIZE * 2, |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 99 | }, |
| 100 | }; |
| 101 | |
| 102 | static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats); |
| 103 | |
| 104 | /* intermediate buffers with raw data from the USB device */ |
| 105 | struct rtl2832_sdr_frame_buf { |
| 106 | struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */ |
| 107 | struct list_head list; |
| 108 | }; |
| 109 | |
| 110 | struct rtl2832_sdr_state { |
| 111 | #define POWER_ON (1 << 1) |
| 112 | #define URB_BUF (1 << 2) |
| 113 | unsigned long flags; |
| 114 | |
| 115 | const struct rtl2832_config *cfg; |
| 116 | struct dvb_frontend *fe; |
| 117 | struct dvb_usb_device *d; |
| 118 | struct i2c_adapter *i2c; |
| 119 | u8 bank; |
| 120 | |
| 121 | struct video_device vdev; |
| 122 | struct v4l2_device v4l2_dev; |
| 123 | |
| 124 | /* videobuf2 queue and queued buffers list */ |
| 125 | struct vb2_queue vb_queue; |
| 126 | struct list_head queued_bufs; |
| 127 | spinlock_t queued_bufs_lock; /* Protects queued_bufs */ |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 128 | unsigned sequence; /* buffer sequence counter */ |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 129 | |
| 130 | /* Note if taking both locks v4l2_lock must always be locked first! */ |
| 131 | struct mutex v4l2_lock; /* Protects everything else */ |
| 132 | struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */ |
| 133 | |
| 134 | /* Pointer to our usb_device, will be NULL after unplug */ |
| 135 | struct usb_device *udev; /* Both mutexes most be hold when setting! */ |
| 136 | |
| 137 | unsigned int vb_full; /* vb is full and packets dropped */ |
| 138 | |
| 139 | struct urb *urb_list[MAX_BULK_BUFS]; |
| 140 | int buf_num; |
| 141 | unsigned long buf_size; |
| 142 | u8 *buf_list[MAX_BULK_BUFS]; |
| 143 | dma_addr_t dma_addr[MAX_BULK_BUFS]; |
| 144 | int urbs_initialized; |
| 145 | int urbs_submitted; |
| 146 | |
| 147 | unsigned int f_adc, f_tuner; |
| 148 | u32 pixelformat; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 149 | u32 buffersize; |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 150 | unsigned int num_formats; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 151 | |
| 152 | /* Controls */ |
| 153 | struct v4l2_ctrl_handler hdl; |
| 154 | struct v4l2_ctrl *bandwidth_auto; |
| 155 | struct v4l2_ctrl *bandwidth; |
| 156 | |
| 157 | /* for sample rate calc */ |
| 158 | unsigned int sample; |
| 159 | unsigned int sample_measured; |
| 160 | unsigned long jiffies_next; |
| 161 | }; |
| 162 | |
| 163 | /* write multiple hardware registers */ |
| 164 | static int rtl2832_sdr_wr(struct rtl2832_sdr_state *s, u8 reg, const u8 *val, |
| 165 | int len) |
| 166 | { |
| 167 | int ret; |
Antti Palosaari | 02b7220 | 2014-03-14 16:20:40 -0300 | [diff] [blame] | 168 | #define MAX_WR_LEN 24 |
| 169 | #define MAX_WR_XFER_LEN (MAX_WR_LEN + 1) |
| 170 | u8 buf[MAX_WR_XFER_LEN]; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 171 | struct i2c_msg msg[1] = { |
| 172 | { |
| 173 | .addr = s->cfg->i2c_addr, |
| 174 | .flags = 0, |
| 175 | .len = 1 + len, |
| 176 | .buf = buf, |
| 177 | } |
| 178 | }; |
| 179 | |
Antti Palosaari | 02b7220 | 2014-03-14 16:20:40 -0300 | [diff] [blame] | 180 | if (WARN_ON(len > MAX_WR_LEN)) |
| 181 | return -EINVAL; |
| 182 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 183 | buf[0] = reg; |
| 184 | memcpy(&buf[1], val, len); |
| 185 | |
| 186 | ret = i2c_transfer(s->i2c, msg, 1); |
| 187 | if (ret == 1) { |
| 188 | ret = 0; |
| 189 | } else { |
| 190 | dev_err(&s->i2c->dev, |
| 191 | "%s: I2C wr failed=%d reg=%02x len=%d\n", |
| 192 | KBUILD_MODNAME, ret, reg, len); |
| 193 | ret = -EREMOTEIO; |
| 194 | } |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | /* read multiple hardware registers */ |
| 199 | static int rtl2832_sdr_rd(struct rtl2832_sdr_state *s, u8 reg, u8 *val, int len) |
| 200 | { |
| 201 | int ret; |
| 202 | struct i2c_msg msg[2] = { |
| 203 | { |
| 204 | .addr = s->cfg->i2c_addr, |
| 205 | .flags = 0, |
| 206 | .len = 1, |
| 207 | .buf = ®, |
| 208 | }, { |
| 209 | .addr = s->cfg->i2c_addr, |
| 210 | .flags = I2C_M_RD, |
| 211 | .len = len, |
| 212 | .buf = val, |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | ret = i2c_transfer(s->i2c, msg, 2); |
| 217 | if (ret == 2) { |
| 218 | ret = 0; |
| 219 | } else { |
| 220 | dev_err(&s->i2c->dev, |
| 221 | "%s: I2C rd failed=%d reg=%02x len=%d\n", |
| 222 | KBUILD_MODNAME, ret, reg, len); |
| 223 | ret = -EREMOTEIO; |
| 224 | } |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | /* write multiple registers */ |
| 229 | static int rtl2832_sdr_wr_regs(struct rtl2832_sdr_state *s, u16 reg, |
| 230 | const u8 *val, int len) |
| 231 | { |
| 232 | int ret; |
| 233 | u8 reg2 = (reg >> 0) & 0xff; |
| 234 | u8 bank = (reg >> 8) & 0xff; |
| 235 | |
| 236 | /* switch bank if needed */ |
| 237 | if (bank != s->bank) { |
| 238 | ret = rtl2832_sdr_wr(s, 0x00, &bank, 1); |
| 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | s->bank = bank; |
| 243 | } |
| 244 | |
| 245 | return rtl2832_sdr_wr(s, reg2, val, len); |
| 246 | } |
| 247 | |
| 248 | /* read multiple registers */ |
| 249 | static int rtl2832_sdr_rd_regs(struct rtl2832_sdr_state *s, u16 reg, u8 *val, |
| 250 | int len) |
| 251 | { |
| 252 | int ret; |
| 253 | u8 reg2 = (reg >> 0) & 0xff; |
| 254 | u8 bank = (reg >> 8) & 0xff; |
| 255 | |
| 256 | /* switch bank if needed */ |
| 257 | if (bank != s->bank) { |
| 258 | ret = rtl2832_sdr_wr(s, 0x00, &bank, 1); |
| 259 | if (ret) |
| 260 | return ret; |
| 261 | |
| 262 | s->bank = bank; |
| 263 | } |
| 264 | |
| 265 | return rtl2832_sdr_rd(s, reg2, val, len); |
| 266 | } |
| 267 | |
| 268 | /* write single register */ |
| 269 | static int rtl2832_sdr_wr_reg(struct rtl2832_sdr_state *s, u16 reg, u8 val) |
| 270 | { |
| 271 | return rtl2832_sdr_wr_regs(s, reg, &val, 1); |
| 272 | } |
| 273 | |
| 274 | #if 0 |
| 275 | /* read single register */ |
| 276 | static int rtl2832_sdr_rd_reg(struct rtl2832_sdr_state *s, u16 reg, u8 *val) |
| 277 | { |
| 278 | return rtl2832_sdr_rd_regs(s, reg, val, 1); |
| 279 | } |
| 280 | #endif |
| 281 | |
| 282 | /* write single register with mask */ |
| 283 | static int rtl2832_sdr_wr_reg_mask(struct rtl2832_sdr_state *s, u16 reg, |
| 284 | u8 val, u8 mask) |
| 285 | { |
| 286 | int ret; |
| 287 | u8 tmp; |
| 288 | |
| 289 | /* no need for read if whole reg is written */ |
| 290 | if (mask != 0xff) { |
| 291 | ret = rtl2832_sdr_rd_regs(s, reg, &tmp, 1); |
| 292 | if (ret) |
| 293 | return ret; |
| 294 | |
| 295 | val &= mask; |
| 296 | tmp &= ~mask; |
| 297 | val |= tmp; |
| 298 | } |
| 299 | |
| 300 | return rtl2832_sdr_wr_regs(s, reg, &val, 1); |
| 301 | } |
| 302 | |
| 303 | #if 0 |
| 304 | /* read single register with mask */ |
| 305 | static int rtl2832_sdr_rd_reg_mask(struct rtl2832_sdr_state *s, u16 reg, |
| 306 | u8 *val, u8 mask) |
| 307 | { |
| 308 | int ret, i; |
| 309 | u8 tmp; |
| 310 | |
| 311 | ret = rtl2832_sdr_rd_regs(s, reg, &tmp, 1); |
| 312 | if (ret) |
| 313 | return ret; |
| 314 | |
| 315 | tmp &= mask; |
| 316 | |
| 317 | /* find position of the first bit */ |
| 318 | for (i = 0; i < 8; i++) { |
| 319 | if ((mask >> i) & 0x01) |
| 320 | break; |
| 321 | } |
| 322 | *val = tmp >> i; |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | #endif |
| 327 | |
| 328 | /* Private functions */ |
| 329 | static struct rtl2832_sdr_frame_buf *rtl2832_sdr_get_next_fill_buf( |
| 330 | struct rtl2832_sdr_state *s) |
| 331 | { |
| 332 | unsigned long flags = 0; |
| 333 | struct rtl2832_sdr_frame_buf *buf = NULL; |
| 334 | |
| 335 | spin_lock_irqsave(&s->queued_bufs_lock, flags); |
| 336 | if (list_empty(&s->queued_bufs)) |
| 337 | goto leave; |
| 338 | |
| 339 | buf = list_entry(s->queued_bufs.next, |
| 340 | struct rtl2832_sdr_frame_buf, list); |
| 341 | list_del(&buf->list); |
| 342 | leave: |
| 343 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); |
| 344 | return buf; |
| 345 | } |
| 346 | |
| 347 | static unsigned int rtl2832_sdr_convert_stream(struct rtl2832_sdr_state *s, |
| 348 | void *dst, const u8 *src, unsigned int src_len) |
| 349 | { |
| 350 | unsigned int dst_len; |
| 351 | |
| 352 | if (s->pixelformat == V4L2_SDR_FMT_CU8) { |
| 353 | /* native stream, no need to convert */ |
| 354 | memcpy(dst, src, src_len); |
| 355 | dst_len = src_len; |
| 356 | } else if (s->pixelformat == V4L2_SDR_FMT_CU16LE) { |
| 357 | /* convert u8 to u16 */ |
| 358 | unsigned int i; |
| 359 | u16 *u16dst = dst; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 360 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 361 | for (i = 0; i < src_len; i++) |
| 362 | *u16dst++ = (src[i] << 8) | (src[i] >> 0); |
| 363 | dst_len = 2 * src_len; |
| 364 | } else { |
| 365 | dst_len = 0; |
| 366 | } |
| 367 | |
| 368 | /* calculate samping rate and output it in 10 seconds intervals */ |
| 369 | if (unlikely(time_is_before_jiffies(s->jiffies_next))) { |
| 370 | #define MSECS 10000UL |
| 371 | unsigned int samples = s->sample - s->sample_measured; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 372 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 373 | s->jiffies_next = jiffies + msecs_to_jiffies(MSECS); |
| 374 | s->sample_measured = s->sample; |
| 375 | dev_dbg(&s->udev->dev, |
| 376 | "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", |
| 377 | src_len, samples, MSECS, |
| 378 | samples * 1000UL / MSECS); |
| 379 | } |
| 380 | |
| 381 | /* total number of I+Q pairs */ |
| 382 | s->sample += src_len / 2; |
| 383 | |
| 384 | return dst_len; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * This gets called for the bulk stream pipe. This is done in interrupt |
| 389 | * time, so it has to be fast, not crash, and not stall. Neat. |
| 390 | */ |
| 391 | static void rtl2832_sdr_urb_complete(struct urb *urb) |
| 392 | { |
| 393 | struct rtl2832_sdr_state *s = urb->context; |
| 394 | struct rtl2832_sdr_frame_buf *fbuf; |
| 395 | |
| 396 | dev_dbg_ratelimited(&s->udev->dev, |
| 397 | "%s: status=%d length=%d/%d errors=%d\n", |
| 398 | __func__, urb->status, urb->actual_length, |
| 399 | urb->transfer_buffer_length, urb->error_count); |
| 400 | |
| 401 | switch (urb->status) { |
| 402 | case 0: /* success */ |
| 403 | case -ETIMEDOUT: /* NAK */ |
| 404 | break; |
| 405 | case -ECONNRESET: /* kill */ |
| 406 | case -ENOENT: |
| 407 | case -ESHUTDOWN: |
| 408 | return; |
| 409 | default: /* error */ |
| 410 | dev_err_ratelimited(&s->udev->dev, "urb failed=%d\n", |
| 411 | urb->status); |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | if (likely(urb->actual_length > 0)) { |
| 416 | void *ptr; |
| 417 | unsigned int len; |
| 418 | /* get free framebuffer */ |
| 419 | fbuf = rtl2832_sdr_get_next_fill_buf(s); |
| 420 | if (unlikely(fbuf == NULL)) { |
| 421 | s->vb_full++; |
| 422 | dev_notice_ratelimited(&s->udev->dev, |
| 423 | "videobuf is full, %d packets dropped\n", |
| 424 | s->vb_full); |
| 425 | goto skip; |
| 426 | } |
| 427 | |
| 428 | /* fill framebuffer */ |
| 429 | ptr = vb2_plane_vaddr(&fbuf->vb, 0); |
| 430 | len = rtl2832_sdr_convert_stream(s, ptr, urb->transfer_buffer, |
| 431 | urb->actual_length); |
| 432 | vb2_set_plane_payload(&fbuf->vb, 0, len); |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 433 | v4l2_get_timestamp(&fbuf->vb.v4l2_buf.timestamp); |
| 434 | fbuf->vb.v4l2_buf.sequence = s->sequence++; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 435 | vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE); |
| 436 | } |
| 437 | skip: |
| 438 | usb_submit_urb(urb, GFP_ATOMIC); |
| 439 | } |
| 440 | |
| 441 | static int rtl2832_sdr_kill_urbs(struct rtl2832_sdr_state *s) |
| 442 | { |
| 443 | int i; |
| 444 | |
| 445 | for (i = s->urbs_submitted - 1; i >= 0; i--) { |
| 446 | dev_dbg(&s->udev->dev, "%s: kill urb=%d\n", __func__, i); |
| 447 | /* stop the URB */ |
| 448 | usb_kill_urb(s->urb_list[i]); |
| 449 | } |
| 450 | s->urbs_submitted = 0; |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int rtl2832_sdr_submit_urbs(struct rtl2832_sdr_state *s) |
| 456 | { |
| 457 | int i, ret; |
| 458 | |
| 459 | for (i = 0; i < s->urbs_initialized; i++) { |
| 460 | dev_dbg(&s->udev->dev, "%s: submit urb=%d\n", __func__, i); |
| 461 | ret = usb_submit_urb(s->urb_list[i], GFP_ATOMIC); |
| 462 | if (ret) { |
| 463 | dev_err(&s->udev->dev, |
| 464 | "Could not submit urb no. %d - get them all back\n", |
| 465 | i); |
| 466 | rtl2832_sdr_kill_urbs(s); |
| 467 | return ret; |
| 468 | } |
| 469 | s->urbs_submitted++; |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static int rtl2832_sdr_free_stream_bufs(struct rtl2832_sdr_state *s) |
| 476 | { |
| 477 | if (s->flags & USB_STATE_URB_BUF) { |
| 478 | while (s->buf_num) { |
| 479 | s->buf_num--; |
| 480 | dev_dbg(&s->udev->dev, "%s: free buf=%d\n", |
| 481 | __func__, s->buf_num); |
| 482 | usb_free_coherent(s->udev, s->buf_size, |
| 483 | s->buf_list[s->buf_num], |
| 484 | s->dma_addr[s->buf_num]); |
| 485 | } |
| 486 | } |
| 487 | s->flags &= ~USB_STATE_URB_BUF; |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int rtl2832_sdr_alloc_stream_bufs(struct rtl2832_sdr_state *s) |
| 493 | { |
| 494 | s->buf_num = 0; |
| 495 | s->buf_size = BULK_BUFFER_SIZE; |
| 496 | |
| 497 | dev_dbg(&s->udev->dev, |
| 498 | "%s: all in all I will use %u bytes for streaming\n", |
| 499 | __func__, MAX_BULK_BUFS * BULK_BUFFER_SIZE); |
| 500 | |
| 501 | for (s->buf_num = 0; s->buf_num < MAX_BULK_BUFS; s->buf_num++) { |
| 502 | s->buf_list[s->buf_num] = usb_alloc_coherent(s->udev, |
| 503 | BULK_BUFFER_SIZE, GFP_ATOMIC, |
| 504 | &s->dma_addr[s->buf_num]); |
| 505 | if (!s->buf_list[s->buf_num]) { |
| 506 | dev_dbg(&s->udev->dev, "%s: alloc buf=%d failed\n", |
| 507 | __func__, s->buf_num); |
| 508 | rtl2832_sdr_free_stream_bufs(s); |
| 509 | return -ENOMEM; |
| 510 | } |
| 511 | |
| 512 | dev_dbg(&s->udev->dev, "%s: alloc buf=%d %p (dma %llu)\n", |
| 513 | __func__, s->buf_num, |
| 514 | s->buf_list[s->buf_num], |
| 515 | (long long)s->dma_addr[s->buf_num]); |
| 516 | s->flags |= USB_STATE_URB_BUF; |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int rtl2832_sdr_free_urbs(struct rtl2832_sdr_state *s) |
| 523 | { |
| 524 | int i; |
| 525 | |
| 526 | rtl2832_sdr_kill_urbs(s); |
| 527 | |
| 528 | for (i = s->urbs_initialized - 1; i >= 0; i--) { |
| 529 | if (s->urb_list[i]) { |
| 530 | dev_dbg(&s->udev->dev, "%s: free urb=%d\n", |
| 531 | __func__, i); |
| 532 | /* free the URBs */ |
| 533 | usb_free_urb(s->urb_list[i]); |
| 534 | } |
| 535 | } |
| 536 | s->urbs_initialized = 0; |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int rtl2832_sdr_alloc_urbs(struct rtl2832_sdr_state *s) |
| 542 | { |
| 543 | int i, j; |
| 544 | |
| 545 | /* allocate the URBs */ |
| 546 | for (i = 0; i < MAX_BULK_BUFS; i++) { |
| 547 | dev_dbg(&s->udev->dev, "%s: alloc urb=%d\n", __func__, i); |
| 548 | s->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC); |
| 549 | if (!s->urb_list[i]) { |
| 550 | dev_dbg(&s->udev->dev, "%s: failed\n", __func__); |
| 551 | for (j = 0; j < i; j++) |
| 552 | usb_free_urb(s->urb_list[j]); |
| 553 | return -ENOMEM; |
| 554 | } |
| 555 | usb_fill_bulk_urb(s->urb_list[i], |
| 556 | s->udev, |
| 557 | usb_rcvbulkpipe(s->udev, 0x81), |
| 558 | s->buf_list[i], |
| 559 | BULK_BUFFER_SIZE, |
| 560 | rtl2832_sdr_urb_complete, s); |
| 561 | |
| 562 | s->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
| 563 | s->urb_list[i]->transfer_dma = s->dma_addr[i]; |
| 564 | s->urbs_initialized++; |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | /* Must be called with vb_queue_lock hold */ |
| 571 | static void rtl2832_sdr_cleanup_queued_bufs(struct rtl2832_sdr_state *s) |
| 572 | { |
| 573 | unsigned long flags = 0; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 574 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 575 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 576 | |
| 577 | spin_lock_irqsave(&s->queued_bufs_lock, flags); |
| 578 | while (!list_empty(&s->queued_bufs)) { |
| 579 | struct rtl2832_sdr_frame_buf *buf; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 580 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 581 | buf = list_entry(s->queued_bufs.next, |
| 582 | struct rtl2832_sdr_frame_buf, list); |
| 583 | list_del(&buf->list); |
| 584 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 585 | } |
| 586 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); |
| 587 | } |
| 588 | |
| 589 | /* The user yanked out the cable... */ |
| 590 | static void rtl2832_sdr_release_sec(struct dvb_frontend *fe) |
| 591 | { |
| 592 | struct rtl2832_sdr_state *s = fe->sec_priv; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 593 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 594 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 595 | |
| 596 | mutex_lock(&s->vb_queue_lock); |
| 597 | mutex_lock(&s->v4l2_lock); |
| 598 | /* No need to keep the urbs around after disconnection */ |
| 599 | s->udev = NULL; |
| 600 | |
| 601 | v4l2_device_disconnect(&s->v4l2_dev); |
| 602 | video_unregister_device(&s->vdev); |
| 603 | mutex_unlock(&s->v4l2_lock); |
| 604 | mutex_unlock(&s->vb_queue_lock); |
| 605 | |
| 606 | v4l2_device_put(&s->v4l2_dev); |
| 607 | |
| 608 | fe->sec_priv = NULL; |
| 609 | } |
| 610 | |
| 611 | static int rtl2832_sdr_querycap(struct file *file, void *fh, |
| 612 | struct v4l2_capability *cap) |
| 613 | { |
| 614 | struct rtl2832_sdr_state *s = video_drvdata(file); |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 615 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 616 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 617 | |
| 618 | strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); |
| 619 | strlcpy(cap->card, s->vdev.name, sizeof(cap->card)); |
| 620 | usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); |
| 621 | cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING | |
| 622 | V4L2_CAP_READWRITE | V4L2_CAP_TUNER; |
| 623 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* Videobuf2 operations */ |
| 628 | static int rtl2832_sdr_queue_setup(struct vb2_queue *vq, |
| 629 | const struct v4l2_format *fmt, unsigned int *nbuffers, |
| 630 | unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) |
| 631 | { |
| 632 | struct rtl2832_sdr_state *s = vb2_get_drv_priv(vq); |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 633 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 634 | dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers); |
| 635 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 636 | /* Need at least 8 buffers */ |
| 637 | if (vq->num_buffers + *nbuffers < 8) |
| 638 | *nbuffers = 8 - vq->num_buffers; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 639 | *nplanes = 1; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 640 | sizes[0] = PAGE_ALIGN(s->buffersize); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 641 | dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n", |
| 642 | __func__, *nbuffers, sizes[0]); |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static int rtl2832_sdr_buf_prepare(struct vb2_buffer *vb) |
| 647 | { |
| 648 | struct rtl2832_sdr_state *s = vb2_get_drv_priv(vb->vb2_queue); |
| 649 | |
| 650 | /* Don't allow queing new buffers after device disconnection */ |
| 651 | if (!s->udev) |
| 652 | return -ENODEV; |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static void rtl2832_sdr_buf_queue(struct vb2_buffer *vb) |
| 658 | { |
| 659 | struct rtl2832_sdr_state *s = vb2_get_drv_priv(vb->vb2_queue); |
| 660 | struct rtl2832_sdr_frame_buf *buf = |
| 661 | container_of(vb, struct rtl2832_sdr_frame_buf, vb); |
| 662 | unsigned long flags = 0; |
| 663 | |
| 664 | /* Check the device has not disconnected between prep and queuing */ |
| 665 | if (!s->udev) { |
| 666 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | spin_lock_irqsave(&s->queued_bufs_lock, flags); |
| 671 | list_add_tail(&buf->list, &s->queued_bufs); |
| 672 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); |
| 673 | } |
| 674 | |
| 675 | static int rtl2832_sdr_set_adc(struct rtl2832_sdr_state *s) |
| 676 | { |
| 677 | struct dvb_frontend *fe = s->fe; |
| 678 | int ret; |
| 679 | unsigned int f_sr, f_if; |
| 680 | u8 buf[4], u8tmp1, u8tmp2; |
| 681 | u64 u64tmp; |
| 682 | u32 u32tmp; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 683 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 684 | dev_dbg(&s->udev->dev, "%s: f_adc=%u\n", __func__, s->f_adc); |
| 685 | |
| 686 | if (!test_bit(POWER_ON, &s->flags)) |
| 687 | return 0; |
| 688 | |
| 689 | if (s->f_adc == 0) |
| 690 | return 0; |
| 691 | |
| 692 | f_sr = s->f_adc; |
| 693 | |
| 694 | ret = rtl2832_sdr_wr_regs(s, 0x13e, "\x00\x00", 2); |
| 695 | if (ret) |
| 696 | goto err; |
| 697 | |
| 698 | ret = rtl2832_sdr_wr_regs(s, 0x115, "\x00\x00\x00\x00", 4); |
| 699 | if (ret) |
| 700 | goto err; |
| 701 | |
| 702 | /* get IF from tuner */ |
| 703 | if (fe->ops.tuner_ops.get_if_frequency) |
| 704 | ret = fe->ops.tuner_ops.get_if_frequency(fe, &f_if); |
| 705 | else |
| 706 | ret = -EINVAL; |
| 707 | |
| 708 | if (ret) |
| 709 | goto err; |
| 710 | |
| 711 | /* program IF */ |
| 712 | u64tmp = f_if % s->cfg->xtal; |
| 713 | u64tmp *= 0x400000; |
| 714 | u64tmp = div_u64(u64tmp, s->cfg->xtal); |
| 715 | u64tmp = -u64tmp; |
| 716 | u32tmp = u64tmp & 0x3fffff; |
| 717 | |
| 718 | dev_dbg(&s->udev->dev, "%s: f_if=%u if_ctl=%08x\n", |
| 719 | __func__, f_if, u32tmp); |
| 720 | |
| 721 | buf[0] = (u32tmp >> 16) & 0xff; |
| 722 | buf[1] = (u32tmp >> 8) & 0xff; |
| 723 | buf[2] = (u32tmp >> 0) & 0xff; |
| 724 | |
| 725 | ret = rtl2832_sdr_wr_regs(s, 0x119, buf, 3); |
| 726 | if (ret) |
| 727 | goto err; |
| 728 | |
| 729 | /* BB / IF mode */ |
| 730 | /* POR: 0x1b1=0x1f, 0x008=0x0d, 0x006=0x80 */ |
| 731 | if (f_if) { |
| 732 | u8tmp1 = 0x1a; /* disable Zero-IF */ |
| 733 | u8tmp2 = 0x8d; /* enable ADC I */ |
| 734 | } else { |
| 735 | u8tmp1 = 0x1b; /* enable Zero-IF, DC, IQ */ |
| 736 | u8tmp2 = 0xcd; /* enable ADC I, ADC Q */ |
| 737 | } |
| 738 | |
| 739 | ret = rtl2832_sdr_wr_reg(s, 0x1b1, u8tmp1); |
| 740 | if (ret) |
| 741 | goto err; |
| 742 | |
| 743 | ret = rtl2832_sdr_wr_reg(s, 0x008, u8tmp2); |
| 744 | if (ret) |
| 745 | goto err; |
| 746 | |
| 747 | ret = rtl2832_sdr_wr_reg(s, 0x006, 0x80); |
| 748 | if (ret) |
| 749 | goto err; |
| 750 | |
| 751 | /* program sampling rate (resampling down) */ |
| 752 | u32tmp = div_u64(s->cfg->xtal * 0x400000ULL, f_sr * 4U); |
| 753 | u32tmp <<= 2; |
| 754 | buf[0] = (u32tmp >> 24) & 0xff; |
| 755 | buf[1] = (u32tmp >> 16) & 0xff; |
| 756 | buf[2] = (u32tmp >> 8) & 0xff; |
| 757 | buf[3] = (u32tmp >> 0) & 0xff; |
| 758 | ret = rtl2832_sdr_wr_regs(s, 0x19f, buf, 4); |
| 759 | if (ret) |
| 760 | goto err; |
| 761 | |
| 762 | /* low-pass filter */ |
| 763 | ret = rtl2832_sdr_wr_regs(s, 0x11c, |
| 764 | "\xca\xdc\xd7\xd8\xe0\xf2\x0e\x35\x06\x50\x9c\x0d\x71\x11\x14\x71\x74\x19\x41\xa5", |
| 765 | 20); |
| 766 | if (ret) |
| 767 | goto err; |
| 768 | |
| 769 | ret = rtl2832_sdr_wr_regs(s, 0x017, "\x11\x10", 2); |
| 770 | if (ret) |
| 771 | goto err; |
| 772 | |
| 773 | /* mode */ |
| 774 | ret = rtl2832_sdr_wr_regs(s, 0x019, "\x05", 1); |
| 775 | if (ret) |
| 776 | goto err; |
| 777 | |
| 778 | ret = rtl2832_sdr_wr_regs(s, 0x01a, "\x1b\x16\x0d\x06\x01\xff", 6); |
| 779 | if (ret) |
| 780 | goto err; |
| 781 | |
| 782 | /* FSM */ |
| 783 | ret = rtl2832_sdr_wr_regs(s, 0x192, "\x00\xf0\x0f", 3); |
| 784 | if (ret) |
| 785 | goto err; |
| 786 | |
| 787 | /* PID filter */ |
| 788 | ret = rtl2832_sdr_wr_regs(s, 0x061, "\x60", 1); |
| 789 | if (ret) |
| 790 | goto err; |
| 791 | |
| 792 | /* used RF tuner based settings */ |
| 793 | switch (s->cfg->tuner) { |
| 794 | case RTL2832_TUNER_E4000: |
| 795 | ret = rtl2832_sdr_wr_regs(s, 0x112, "\x5a", 1); |
| 796 | ret = rtl2832_sdr_wr_regs(s, 0x102, "\x40", 1); |
| 797 | ret = rtl2832_sdr_wr_regs(s, 0x103, "\x5a", 1); |
| 798 | ret = rtl2832_sdr_wr_regs(s, 0x1c7, "\x30", 1); |
| 799 | ret = rtl2832_sdr_wr_regs(s, 0x104, "\xd0", 1); |
| 800 | ret = rtl2832_sdr_wr_regs(s, 0x105, "\xbe", 1); |
| 801 | ret = rtl2832_sdr_wr_regs(s, 0x1c8, "\x18", 1); |
| 802 | ret = rtl2832_sdr_wr_regs(s, 0x106, "\x35", 1); |
| 803 | ret = rtl2832_sdr_wr_regs(s, 0x1c9, "\x21", 1); |
| 804 | ret = rtl2832_sdr_wr_regs(s, 0x1ca, "\x21", 1); |
| 805 | ret = rtl2832_sdr_wr_regs(s, 0x1cb, "\x00", 1); |
| 806 | ret = rtl2832_sdr_wr_regs(s, 0x107, "\x40", 1); |
| 807 | ret = rtl2832_sdr_wr_regs(s, 0x1cd, "\x10", 1); |
| 808 | ret = rtl2832_sdr_wr_regs(s, 0x1ce, "\x10", 1); |
| 809 | ret = rtl2832_sdr_wr_regs(s, 0x108, "\x80", 1); |
| 810 | ret = rtl2832_sdr_wr_regs(s, 0x109, "\x7f", 1); |
| 811 | ret = rtl2832_sdr_wr_regs(s, 0x10a, "\x80", 1); |
| 812 | ret = rtl2832_sdr_wr_regs(s, 0x10b, "\x7f", 1); |
| 813 | ret = rtl2832_sdr_wr_regs(s, 0x00e, "\xfc", 1); |
| 814 | ret = rtl2832_sdr_wr_regs(s, 0x00e, "\xfc", 1); |
| 815 | ret = rtl2832_sdr_wr_regs(s, 0x011, "\xd4", 1); |
| 816 | ret = rtl2832_sdr_wr_regs(s, 0x1e5, "\xf0", 1); |
| 817 | ret = rtl2832_sdr_wr_regs(s, 0x1d9, "\x00", 1); |
| 818 | ret = rtl2832_sdr_wr_regs(s, 0x1db, "\x00", 1); |
| 819 | ret = rtl2832_sdr_wr_regs(s, 0x1dd, "\x14", 1); |
| 820 | ret = rtl2832_sdr_wr_regs(s, 0x1de, "\xec", 1); |
| 821 | ret = rtl2832_sdr_wr_regs(s, 0x1d8, "\x0c", 1); |
| 822 | ret = rtl2832_sdr_wr_regs(s, 0x1e6, "\x02", 1); |
| 823 | ret = rtl2832_sdr_wr_regs(s, 0x1d7, "\x09", 1); |
| 824 | ret = rtl2832_sdr_wr_regs(s, 0x00d, "\x83", 1); |
| 825 | ret = rtl2832_sdr_wr_regs(s, 0x010, "\x49", 1); |
| 826 | ret = rtl2832_sdr_wr_regs(s, 0x00d, "\x87", 1); |
| 827 | ret = rtl2832_sdr_wr_regs(s, 0x00d, "\x85", 1); |
| 828 | ret = rtl2832_sdr_wr_regs(s, 0x013, "\x02", 1); |
| 829 | break; |
| 830 | case RTL2832_TUNER_FC0012: |
| 831 | case RTL2832_TUNER_FC0013: |
| 832 | ret = rtl2832_sdr_wr_regs(s, 0x112, "\x5a", 1); |
| 833 | ret = rtl2832_sdr_wr_regs(s, 0x102, "\x40", 1); |
| 834 | ret = rtl2832_sdr_wr_regs(s, 0x103, "\x5a", 1); |
| 835 | ret = rtl2832_sdr_wr_regs(s, 0x1c7, "\x2c", 1); |
| 836 | ret = rtl2832_sdr_wr_regs(s, 0x104, "\xcc", 1); |
| 837 | ret = rtl2832_sdr_wr_regs(s, 0x105, "\xbe", 1); |
| 838 | ret = rtl2832_sdr_wr_regs(s, 0x1c8, "\x16", 1); |
| 839 | ret = rtl2832_sdr_wr_regs(s, 0x106, "\x35", 1); |
| 840 | ret = rtl2832_sdr_wr_regs(s, 0x1c9, "\x21", 1); |
| 841 | ret = rtl2832_sdr_wr_regs(s, 0x1ca, "\x21", 1); |
| 842 | ret = rtl2832_sdr_wr_regs(s, 0x1cb, "\x00", 1); |
| 843 | ret = rtl2832_sdr_wr_regs(s, 0x107, "\x40", 1); |
| 844 | ret = rtl2832_sdr_wr_regs(s, 0x1cd, "\x10", 1); |
| 845 | ret = rtl2832_sdr_wr_regs(s, 0x1ce, "\x10", 1); |
| 846 | ret = rtl2832_sdr_wr_regs(s, 0x108, "\x80", 1); |
| 847 | ret = rtl2832_sdr_wr_regs(s, 0x109, "\x7f", 1); |
| 848 | ret = rtl2832_sdr_wr_regs(s, 0x10a, "\x80", 1); |
| 849 | ret = rtl2832_sdr_wr_regs(s, 0x10b, "\x7f", 1); |
| 850 | ret = rtl2832_sdr_wr_regs(s, 0x00e, "\xfc", 1); |
| 851 | ret = rtl2832_sdr_wr_regs(s, 0x00e, "\xfc", 1); |
| 852 | ret = rtl2832_sdr_wr_regs(s, 0x011, "\xe9\xbf", 2); |
| 853 | ret = rtl2832_sdr_wr_regs(s, 0x1e5, "\xf0", 1); |
| 854 | ret = rtl2832_sdr_wr_regs(s, 0x1d9, "\x00", 1); |
| 855 | ret = rtl2832_sdr_wr_regs(s, 0x1db, "\x00", 1); |
| 856 | ret = rtl2832_sdr_wr_regs(s, 0x1dd, "\x11", 1); |
| 857 | ret = rtl2832_sdr_wr_regs(s, 0x1de, "\xef", 1); |
| 858 | ret = rtl2832_sdr_wr_regs(s, 0x1d8, "\x0c", 1); |
| 859 | ret = rtl2832_sdr_wr_regs(s, 0x1e6, "\x02", 1); |
| 860 | ret = rtl2832_sdr_wr_regs(s, 0x1d7, "\x09", 1); |
| 861 | break; |
| 862 | case RTL2832_TUNER_R820T: |
| 863 | ret = rtl2832_sdr_wr_regs(s, 0x112, "\x5a", 1); |
| 864 | ret = rtl2832_sdr_wr_regs(s, 0x102, "\x40", 1); |
| 865 | ret = rtl2832_sdr_wr_regs(s, 0x115, "\x01", 1); |
| 866 | ret = rtl2832_sdr_wr_regs(s, 0x103, "\x80", 1); |
| 867 | ret = rtl2832_sdr_wr_regs(s, 0x1c7, "\x24", 1); |
| 868 | ret = rtl2832_sdr_wr_regs(s, 0x104, "\xcc", 1); |
| 869 | ret = rtl2832_sdr_wr_regs(s, 0x105, "\xbe", 1); |
| 870 | ret = rtl2832_sdr_wr_regs(s, 0x1c8, "\x14", 1); |
| 871 | ret = rtl2832_sdr_wr_regs(s, 0x106, "\x35", 1); |
| 872 | ret = rtl2832_sdr_wr_regs(s, 0x1c9, "\x21", 1); |
| 873 | ret = rtl2832_sdr_wr_regs(s, 0x1ca, "\x21", 1); |
| 874 | ret = rtl2832_sdr_wr_regs(s, 0x1cb, "\x00", 1); |
| 875 | ret = rtl2832_sdr_wr_regs(s, 0x107, "\x40", 1); |
| 876 | ret = rtl2832_sdr_wr_regs(s, 0x1cd, "\x10", 1); |
| 877 | ret = rtl2832_sdr_wr_regs(s, 0x1ce, "\x10", 1); |
| 878 | ret = rtl2832_sdr_wr_regs(s, 0x108, "\x80", 1); |
| 879 | ret = rtl2832_sdr_wr_regs(s, 0x109, "\x7f", 1); |
| 880 | ret = rtl2832_sdr_wr_regs(s, 0x10a, "\x80", 1); |
| 881 | ret = rtl2832_sdr_wr_regs(s, 0x10b, "\x7f", 1); |
| 882 | ret = rtl2832_sdr_wr_regs(s, 0x00e, "\xfc", 1); |
| 883 | ret = rtl2832_sdr_wr_regs(s, 0x00e, "\xfc", 1); |
| 884 | ret = rtl2832_sdr_wr_regs(s, 0x011, "\xf4", 1); |
| 885 | break; |
| 886 | default: |
| 887 | dev_notice(&s->udev->dev, "Unsupported tuner\n"); |
| 888 | } |
| 889 | |
| 890 | /* software reset */ |
| 891 | ret = rtl2832_sdr_wr_reg_mask(s, 0x101, 0x04, 0x04); |
| 892 | if (ret) |
| 893 | goto err; |
| 894 | |
| 895 | ret = rtl2832_sdr_wr_reg_mask(s, 0x101, 0x00, 0x04); |
| 896 | if (ret) |
| 897 | goto err; |
| 898 | err: |
| 899 | return ret; |
| 900 | }; |
| 901 | |
| 902 | static void rtl2832_sdr_unset_adc(struct rtl2832_sdr_state *s) |
| 903 | { |
| 904 | int ret; |
| 905 | |
| 906 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 907 | |
| 908 | /* PID filter */ |
| 909 | ret = rtl2832_sdr_wr_regs(s, 0x061, "\xe0", 1); |
| 910 | if (ret) |
| 911 | goto err; |
| 912 | |
| 913 | /* mode */ |
| 914 | ret = rtl2832_sdr_wr_regs(s, 0x019, "\x20", 1); |
| 915 | if (ret) |
| 916 | goto err; |
| 917 | |
| 918 | ret = rtl2832_sdr_wr_regs(s, 0x017, "\x11\x10", 2); |
| 919 | if (ret) |
| 920 | goto err; |
| 921 | |
| 922 | /* FSM */ |
| 923 | ret = rtl2832_sdr_wr_regs(s, 0x192, "\x00\x0f\xff", 3); |
| 924 | if (ret) |
| 925 | goto err; |
| 926 | |
| 927 | ret = rtl2832_sdr_wr_regs(s, 0x13e, "\x40\x00", 2); |
| 928 | if (ret) |
| 929 | goto err; |
| 930 | |
| 931 | ret = rtl2832_sdr_wr_regs(s, 0x115, "\x06\x3f\xce\xcc", 4); |
| 932 | if (ret) |
| 933 | goto err; |
| 934 | err: |
| 935 | return; |
| 936 | }; |
| 937 | |
| 938 | static int rtl2832_sdr_set_tuner_freq(struct rtl2832_sdr_state *s) |
| 939 | { |
| 940 | struct dvb_frontend *fe = s->fe; |
| 941 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 942 | struct v4l2_ctrl *bandwidth_auto; |
| 943 | struct v4l2_ctrl *bandwidth; |
| 944 | |
| 945 | /* |
| 946 | * tuner RF (Hz) |
| 947 | */ |
| 948 | if (s->f_tuner == 0) |
| 949 | return 0; |
| 950 | |
| 951 | /* |
| 952 | * bandwidth (Hz) |
| 953 | */ |
Anthony DeStefano | 8559496 | 2014-04-09 23:08:49 -0300 | [diff] [blame] | 954 | bandwidth_auto = v4l2_ctrl_find(&s->hdl, |
| 955 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 956 | bandwidth = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH); |
| 957 | if (v4l2_ctrl_g_ctrl(bandwidth_auto)) { |
| 958 | c->bandwidth_hz = s->f_adc; |
| 959 | v4l2_ctrl_s_ctrl(bandwidth, s->f_adc); |
| 960 | } else { |
| 961 | c->bandwidth_hz = v4l2_ctrl_g_ctrl(bandwidth); |
| 962 | } |
| 963 | |
| 964 | c->frequency = s->f_tuner; |
| 965 | c->delivery_system = SYS_DVBT; |
| 966 | |
| 967 | dev_dbg(&s->udev->dev, "%s: frequency=%u bandwidth=%d\n", |
| 968 | __func__, c->frequency, c->bandwidth_hz); |
| 969 | |
| 970 | if (!test_bit(POWER_ON, &s->flags)) |
| 971 | return 0; |
| 972 | |
| 973 | if (fe->ops.tuner_ops.set_params) |
| 974 | fe->ops.tuner_ops.set_params(fe); |
| 975 | |
| 976 | return 0; |
| 977 | }; |
| 978 | |
| 979 | static int rtl2832_sdr_set_tuner(struct rtl2832_sdr_state *s) |
| 980 | { |
| 981 | struct dvb_frontend *fe = s->fe; |
| 982 | |
| 983 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 984 | |
| 985 | if (fe->ops.tuner_ops.init) |
| 986 | fe->ops.tuner_ops.init(fe); |
| 987 | |
| 988 | return 0; |
| 989 | }; |
| 990 | |
| 991 | static void rtl2832_sdr_unset_tuner(struct rtl2832_sdr_state *s) |
| 992 | { |
| 993 | struct dvb_frontend *fe = s->fe; |
| 994 | |
| 995 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 996 | |
| 997 | if (fe->ops.tuner_ops.sleep) |
| 998 | fe->ops.tuner_ops.sleep(fe); |
| 999 | |
| 1000 | return; |
| 1001 | }; |
| 1002 | |
| 1003 | static int rtl2832_sdr_start_streaming(struct vb2_queue *vq, unsigned int count) |
| 1004 | { |
| 1005 | struct rtl2832_sdr_state *s = vb2_get_drv_priv(vq); |
| 1006 | int ret; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1007 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1008 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 1009 | |
| 1010 | if (!s->udev) |
| 1011 | return -ENODEV; |
| 1012 | |
| 1013 | if (mutex_lock_interruptible(&s->v4l2_lock)) |
| 1014 | return -ERESTARTSYS; |
| 1015 | |
| 1016 | if (s->d->props->power_ctrl) |
| 1017 | s->d->props->power_ctrl(s->d, 1); |
| 1018 | |
| 1019 | set_bit(POWER_ON, &s->flags); |
| 1020 | |
| 1021 | ret = rtl2832_sdr_set_tuner(s); |
| 1022 | if (ret) |
| 1023 | goto err; |
| 1024 | |
| 1025 | ret = rtl2832_sdr_set_tuner_freq(s); |
| 1026 | if (ret) |
| 1027 | goto err; |
| 1028 | |
| 1029 | ret = rtl2832_sdr_set_adc(s); |
| 1030 | if (ret) |
| 1031 | goto err; |
| 1032 | |
| 1033 | ret = rtl2832_sdr_alloc_stream_bufs(s); |
| 1034 | if (ret) |
| 1035 | goto err; |
| 1036 | |
| 1037 | ret = rtl2832_sdr_alloc_urbs(s); |
| 1038 | if (ret) |
| 1039 | goto err; |
| 1040 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1041 | s->sequence = 0; |
| 1042 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1043 | ret = rtl2832_sdr_submit_urbs(s); |
| 1044 | if (ret) |
| 1045 | goto err; |
| 1046 | |
| 1047 | err: |
| 1048 | mutex_unlock(&s->v4l2_lock); |
| 1049 | |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1053 | static void rtl2832_sdr_stop_streaming(struct vb2_queue *vq) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1054 | { |
| 1055 | struct rtl2832_sdr_state *s = vb2_get_drv_priv(vq); |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1056 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1057 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 1058 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1059 | mutex_lock(&s->v4l2_lock); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1060 | |
| 1061 | rtl2832_sdr_kill_urbs(s); |
| 1062 | rtl2832_sdr_free_urbs(s); |
| 1063 | rtl2832_sdr_free_stream_bufs(s); |
| 1064 | rtl2832_sdr_cleanup_queued_bufs(s); |
| 1065 | rtl2832_sdr_unset_adc(s); |
| 1066 | rtl2832_sdr_unset_tuner(s); |
| 1067 | |
| 1068 | clear_bit(POWER_ON, &s->flags); |
| 1069 | |
| 1070 | if (s->d->props->power_ctrl) |
| 1071 | s->d->props->power_ctrl(s->d, 0); |
| 1072 | |
| 1073 | mutex_unlock(&s->v4l2_lock); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static struct vb2_ops rtl2832_sdr_vb2_ops = { |
| 1077 | .queue_setup = rtl2832_sdr_queue_setup, |
| 1078 | .buf_prepare = rtl2832_sdr_buf_prepare, |
| 1079 | .buf_queue = rtl2832_sdr_buf_queue, |
| 1080 | .start_streaming = rtl2832_sdr_start_streaming, |
| 1081 | .stop_streaming = rtl2832_sdr_stop_streaming, |
| 1082 | .wait_prepare = vb2_ops_wait_prepare, |
| 1083 | .wait_finish = vb2_ops_wait_finish, |
| 1084 | }; |
| 1085 | |
| 1086 | static int rtl2832_sdr_g_tuner(struct file *file, void *priv, |
| 1087 | struct v4l2_tuner *v) |
| 1088 | { |
| 1089 | struct rtl2832_sdr_state *s = video_drvdata(file); |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1090 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1091 | dev_dbg(&s->udev->dev, "%s: index=%d type=%d\n", |
| 1092 | __func__, v->index, v->type); |
| 1093 | |
| 1094 | if (v->index == 0) { |
| 1095 | strlcpy(v->name, "ADC: Realtek RTL2832", sizeof(v->name)); |
| 1096 | v->type = V4L2_TUNER_ADC; |
| 1097 | v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; |
| 1098 | v->rangelow = 300000; |
| 1099 | v->rangehigh = 3200000; |
| 1100 | } else if (v->index == 1) { |
| 1101 | strlcpy(v->name, "RF: <unknown>", sizeof(v->name)); |
| 1102 | v->type = V4L2_TUNER_RF; |
| 1103 | v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; |
| 1104 | v->rangelow = 50000000; |
| 1105 | v->rangehigh = 2000000000; |
| 1106 | } else { |
| 1107 | return -EINVAL; |
| 1108 | } |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | static int rtl2832_sdr_s_tuner(struct file *file, void *priv, |
| 1114 | const struct v4l2_tuner *v) |
| 1115 | { |
| 1116 | struct rtl2832_sdr_state *s = video_drvdata(file); |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1117 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1118 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 1119 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1120 | if (v->index > 1) |
| 1121 | return -EINVAL; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | static int rtl2832_sdr_enum_freq_bands(struct file *file, void *priv, |
| 1126 | struct v4l2_frequency_band *band) |
| 1127 | { |
| 1128 | struct rtl2832_sdr_state *s = video_drvdata(file); |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1129 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1130 | dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n", |
| 1131 | __func__, band->tuner, band->type, band->index); |
| 1132 | |
| 1133 | if (band->tuner == 0) { |
| 1134 | if (band->index >= ARRAY_SIZE(bands_adc)) |
| 1135 | return -EINVAL; |
| 1136 | |
| 1137 | *band = bands_adc[band->index]; |
| 1138 | } else if (band->tuner == 1) { |
| 1139 | if (band->index >= ARRAY_SIZE(bands_fm)) |
| 1140 | return -EINVAL; |
| 1141 | |
| 1142 | *band = bands_fm[band->index]; |
| 1143 | } else { |
| 1144 | return -EINVAL; |
| 1145 | } |
| 1146 | |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | static int rtl2832_sdr_g_frequency(struct file *file, void *priv, |
| 1151 | struct v4l2_frequency *f) |
| 1152 | { |
| 1153 | struct rtl2832_sdr_state *s = video_drvdata(file); |
| 1154 | int ret = 0; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1155 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1156 | dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n", |
| 1157 | __func__, f->tuner, f->type); |
| 1158 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1159 | if (f->tuner == 0) { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1160 | f->frequency = s->f_adc; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1161 | f->type = V4L2_TUNER_ADC; |
| 1162 | } else if (f->tuner == 1) { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1163 | f->frequency = s->f_tuner; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1164 | f->type = V4L2_TUNER_RF; |
| 1165 | } else { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1166 | return -EINVAL; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1167 | } |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1168 | |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
| 1172 | static int rtl2832_sdr_s_frequency(struct file *file, void *priv, |
| 1173 | const struct v4l2_frequency *f) |
| 1174 | { |
| 1175 | struct rtl2832_sdr_state *s = video_drvdata(file); |
| 1176 | int ret, band; |
| 1177 | |
| 1178 | dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n", |
| 1179 | __func__, f->tuner, f->type, f->frequency); |
| 1180 | |
| 1181 | /* ADC band midpoints */ |
| 1182 | #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2) |
| 1183 | #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2) |
| 1184 | |
| 1185 | if (f->tuner == 0 && f->type == V4L2_TUNER_ADC) { |
| 1186 | if (f->frequency < BAND_ADC_0) |
| 1187 | band = 0; |
| 1188 | else if (f->frequency < BAND_ADC_1) |
| 1189 | band = 1; |
| 1190 | else |
| 1191 | band = 2; |
| 1192 | |
| 1193 | s->f_adc = clamp_t(unsigned int, f->frequency, |
| 1194 | bands_adc[band].rangelow, |
| 1195 | bands_adc[band].rangehigh); |
| 1196 | |
| 1197 | dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n", |
| 1198 | __func__, s->f_adc); |
| 1199 | ret = rtl2832_sdr_set_adc(s); |
| 1200 | } else if (f->tuner == 1) { |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1201 | s->f_tuner = clamp_t(unsigned int, f->frequency, |
| 1202 | bands_fm[0].rangelow, |
| 1203 | bands_fm[0].rangehigh); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1204 | dev_dbg(&s->udev->dev, "%s: RF frequency=%u Hz\n", |
| 1205 | __func__, f->frequency); |
| 1206 | |
| 1207 | ret = rtl2832_sdr_set_tuner_freq(s); |
| 1208 | } else { |
| 1209 | ret = -EINVAL; |
| 1210 | } |
| 1211 | |
| 1212 | return ret; |
| 1213 | } |
| 1214 | |
| 1215 | static int rtl2832_sdr_enum_fmt_sdr_cap(struct file *file, void *priv, |
| 1216 | struct v4l2_fmtdesc *f) |
| 1217 | { |
| 1218 | struct rtl2832_sdr_state *s = video_drvdata(file); |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1219 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1220 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 1221 | |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 1222 | if (f->index >= s->num_formats) |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1223 | return -EINVAL; |
| 1224 | |
| 1225 | strlcpy(f->description, formats[f->index].name, sizeof(f->description)); |
| 1226 | f->pixelformat = formats[f->index].pixelformat; |
| 1227 | |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static int rtl2832_sdr_g_fmt_sdr_cap(struct file *file, void *priv, |
| 1232 | struct v4l2_format *f) |
| 1233 | { |
| 1234 | struct rtl2832_sdr_state *s = video_drvdata(file); |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1235 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1236 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 1237 | |
| 1238 | f->fmt.sdr.pixelformat = s->pixelformat; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1239 | f->fmt.sdr.buffersize = s->buffersize; |
| 1240 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1241 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static int rtl2832_sdr_s_fmt_sdr_cap(struct file *file, void *priv, |
| 1247 | struct v4l2_format *f) |
| 1248 | { |
| 1249 | struct rtl2832_sdr_state *s = video_drvdata(file); |
| 1250 | struct vb2_queue *q = &s->vb_queue; |
| 1251 | int i; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1252 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1253 | dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__, |
| 1254 | (char *)&f->fmt.sdr.pixelformat); |
| 1255 | |
| 1256 | if (vb2_is_busy(q)) |
| 1257 | return -EBUSY; |
| 1258 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1259 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 1260 | for (i = 0; i < s->num_formats; i++) { |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1261 | if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1262 | s->pixelformat = formats[i].pixelformat; |
| 1263 | s->buffersize = formats[i].buffersize; |
| 1264 | f->fmt.sdr.buffersize = formats[i].buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1265 | return 0; |
| 1266 | } |
| 1267 | } |
| 1268 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1269 | s->pixelformat = formats[0].pixelformat; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1270 | s->buffersize = formats[0].buffersize; |
| 1271 | f->fmt.sdr.pixelformat = formats[0].pixelformat; |
| 1272 | f->fmt.sdr.buffersize = formats[0].buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int rtl2832_sdr_try_fmt_sdr_cap(struct file *file, void *priv, |
| 1278 | struct v4l2_format *f) |
| 1279 | { |
| 1280 | struct rtl2832_sdr_state *s = video_drvdata(file); |
| 1281 | int i; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1282 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1283 | dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__, |
| 1284 | (char *)&f->fmt.sdr.pixelformat); |
| 1285 | |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1286 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 1287 | for (i = 0; i < s->num_formats; i++) { |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1288 | if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { |
| 1289 | f->fmt.sdr.buffersize = formats[i].buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1290 | return 0; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1291 | } |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | f->fmt.sdr.pixelformat = formats[0].pixelformat; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1295 | f->fmt.sdr.buffersize = formats[0].buffersize; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | static const struct v4l2_ioctl_ops rtl2832_sdr_ioctl_ops = { |
| 1301 | .vidioc_querycap = rtl2832_sdr_querycap, |
| 1302 | |
| 1303 | .vidioc_enum_fmt_sdr_cap = rtl2832_sdr_enum_fmt_sdr_cap, |
| 1304 | .vidioc_g_fmt_sdr_cap = rtl2832_sdr_g_fmt_sdr_cap, |
| 1305 | .vidioc_s_fmt_sdr_cap = rtl2832_sdr_s_fmt_sdr_cap, |
| 1306 | .vidioc_try_fmt_sdr_cap = rtl2832_sdr_try_fmt_sdr_cap, |
| 1307 | |
| 1308 | .vidioc_reqbufs = vb2_ioctl_reqbufs, |
| 1309 | .vidioc_create_bufs = vb2_ioctl_create_bufs, |
| 1310 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, |
| 1311 | .vidioc_querybuf = vb2_ioctl_querybuf, |
| 1312 | .vidioc_qbuf = vb2_ioctl_qbuf, |
| 1313 | .vidioc_dqbuf = vb2_ioctl_dqbuf, |
| 1314 | |
| 1315 | .vidioc_streamon = vb2_ioctl_streamon, |
| 1316 | .vidioc_streamoff = vb2_ioctl_streamoff, |
| 1317 | |
| 1318 | .vidioc_g_tuner = rtl2832_sdr_g_tuner, |
| 1319 | .vidioc_s_tuner = rtl2832_sdr_s_tuner, |
| 1320 | |
| 1321 | .vidioc_enum_freq_bands = rtl2832_sdr_enum_freq_bands, |
| 1322 | .vidioc_g_frequency = rtl2832_sdr_g_frequency, |
| 1323 | .vidioc_s_frequency = rtl2832_sdr_s_frequency, |
| 1324 | |
| 1325 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 1326 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
| 1327 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 1328 | }; |
| 1329 | |
| 1330 | static const struct v4l2_file_operations rtl2832_sdr_fops = { |
| 1331 | .owner = THIS_MODULE, |
| 1332 | .open = v4l2_fh_open, |
| 1333 | .release = vb2_fop_release, |
| 1334 | .read = vb2_fop_read, |
| 1335 | .poll = vb2_fop_poll, |
| 1336 | .mmap = vb2_fop_mmap, |
| 1337 | .unlocked_ioctl = video_ioctl2, |
| 1338 | }; |
| 1339 | |
| 1340 | static struct video_device rtl2832_sdr_template = { |
| 1341 | .name = "Realtek RTL2832 SDR", |
| 1342 | .release = video_device_release_empty, |
| 1343 | .fops = &rtl2832_sdr_fops, |
| 1344 | .ioctl_ops = &rtl2832_sdr_ioctl_ops, |
| 1345 | }; |
| 1346 | |
| 1347 | static int rtl2832_sdr_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1348 | { |
| 1349 | struct rtl2832_sdr_state *s = |
| 1350 | container_of(ctrl->handler, struct rtl2832_sdr_state, |
| 1351 | hdl); |
| 1352 | struct dvb_frontend *fe = s->fe; |
| 1353 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 1354 | int ret; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1355 | |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1356 | dev_dbg(&s->udev->dev, |
Hans Verkuil | 0ba2aeb | 2014-04-16 09:41:25 -0300 | [diff] [blame] | 1357 | "%s: id=%d name=%s val=%d min=%lld max=%lld step=%lld\n", |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1358 | __func__, ctrl->id, ctrl->name, ctrl->val, |
| 1359 | ctrl->minimum, ctrl->maximum, ctrl->step); |
| 1360 | |
| 1361 | switch (ctrl->id) { |
| 1362 | case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: |
| 1363 | case V4L2_CID_RF_TUNER_BANDWIDTH: |
Antti Palosaari | ea4d04f | 2014-03-10 14:28:45 -0300 | [diff] [blame] | 1364 | /* TODO: these controls should be moved to tuner drivers */ |
| 1365 | if (s->bandwidth_auto->val) { |
| 1366 | /* Round towards the closest legal value */ |
Antti Palosaari | a98ccfc | 2014-07-22 00:18:19 -0300 | [diff] [blame] | 1367 | s32 val = s->f_adc + div_u64(s->bandwidth->step, 2); |
Antti Palosaari | ea4d04f | 2014-03-10 14:28:45 -0300 | [diff] [blame] | 1368 | u32 offset; |
Ovidiu Toader | 941a820 | 2014-06-02 21:38:06 -0300 | [diff] [blame] | 1369 | |
Hans Verkuil | 0ba2aeb | 2014-04-16 09:41:25 -0300 | [diff] [blame] | 1370 | val = clamp_t(s32, val, s->bandwidth->minimum, |
| 1371 | s->bandwidth->maximum); |
Antti Palosaari | ea4d04f | 2014-03-10 14:28:45 -0300 | [diff] [blame] | 1372 | offset = val - s->bandwidth->minimum; |
Anthony DeStefano | 8559496 | 2014-04-09 23:08:49 -0300 | [diff] [blame] | 1373 | offset = s->bandwidth->step * |
Antti Palosaari | a98ccfc | 2014-07-22 00:18:19 -0300 | [diff] [blame] | 1374 | div_u64(offset, s->bandwidth->step); |
Antti Palosaari | ea4d04f | 2014-03-10 14:28:45 -0300 | [diff] [blame] | 1375 | s->bandwidth->val = s->bandwidth->minimum + offset; |
| 1376 | } |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1377 | c->bandwidth_hz = s->bandwidth->val; |
| 1378 | |
| 1379 | if (!test_bit(POWER_ON, &s->flags)) |
| 1380 | return 0; |
| 1381 | |
| 1382 | if (fe->ops.tuner_ops.set_params) |
| 1383 | ret = fe->ops.tuner_ops.set_params(fe); |
| 1384 | else |
| 1385 | ret = 0; |
| 1386 | break; |
| 1387 | default: |
| 1388 | ret = -EINVAL; |
| 1389 | } |
| 1390 | |
| 1391 | return ret; |
| 1392 | } |
| 1393 | |
| 1394 | static const struct v4l2_ctrl_ops rtl2832_sdr_ctrl_ops = { |
| 1395 | .s_ctrl = rtl2832_sdr_s_ctrl, |
| 1396 | }; |
| 1397 | |
| 1398 | static void rtl2832_sdr_video_release(struct v4l2_device *v) |
| 1399 | { |
| 1400 | struct rtl2832_sdr_state *s = |
| 1401 | container_of(v, struct rtl2832_sdr_state, v4l2_dev); |
| 1402 | |
| 1403 | v4l2_ctrl_handler_free(&s->hdl); |
| 1404 | v4l2_device_unregister(&s->v4l2_dev); |
| 1405 | kfree(s); |
| 1406 | } |
| 1407 | |
| 1408 | struct dvb_frontend *rtl2832_sdr_attach(struct dvb_frontend *fe, |
| 1409 | struct i2c_adapter *i2c, const struct rtl2832_config *cfg, |
| 1410 | struct v4l2_subdev *sd) |
| 1411 | { |
| 1412 | int ret; |
| 1413 | struct rtl2832_sdr_state *s; |
| 1414 | const struct v4l2_ctrl_ops *ops = &rtl2832_sdr_ctrl_ops; |
| 1415 | struct dvb_usb_device *d = i2c_get_adapdata(i2c); |
| 1416 | |
| 1417 | s = kzalloc(sizeof(struct rtl2832_sdr_state), GFP_KERNEL); |
| 1418 | if (s == NULL) { |
| 1419 | dev_err(&d->udev->dev, |
| 1420 | "Could not allocate memory for rtl2832_sdr_state\n"); |
| 1421 | return NULL; |
| 1422 | } |
| 1423 | |
| 1424 | /* setup the state */ |
| 1425 | s->fe = fe; |
| 1426 | s->d = d; |
| 1427 | s->udev = d->udev; |
| 1428 | s->i2c = i2c; |
| 1429 | s->cfg = cfg; |
| 1430 | s->f_adc = bands_adc[0].rangelow; |
Hans Verkuil | bc90875 | 2014-03-05 20:21:38 -0300 | [diff] [blame] | 1431 | s->f_tuner = bands_fm[0].rangelow; |
Antti Palosaari | d57fe40 | 2014-07-18 16:22:29 -0300 | [diff] [blame] | 1432 | s->pixelformat = formats[0].pixelformat; |
| 1433 | s->buffersize = formats[0].buffersize; |
Antti Palosaari | 2970c0d | 2014-07-14 21:11:12 -0300 | [diff] [blame] | 1434 | s->num_formats = NUM_FORMATS; |
| 1435 | if (rtl2832_sdr_emulated_fmt == false) |
| 1436 | s->num_formats -= 1; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1437 | |
| 1438 | mutex_init(&s->v4l2_lock); |
| 1439 | mutex_init(&s->vb_queue_lock); |
| 1440 | spin_lock_init(&s->queued_bufs_lock); |
| 1441 | INIT_LIST_HEAD(&s->queued_bufs); |
| 1442 | |
| 1443 | /* Init videobuf2 queue structure */ |
| 1444 | s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; |
| 1445 | s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; |
| 1446 | s->vb_queue.drv_priv = s; |
| 1447 | s->vb_queue.buf_struct_size = sizeof(struct rtl2832_sdr_frame_buf); |
| 1448 | s->vb_queue.ops = &rtl2832_sdr_vb2_ops; |
| 1449 | s->vb_queue.mem_ops = &vb2_vmalloc_memops; |
| 1450 | s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
| 1451 | ret = vb2_queue_init(&s->vb_queue); |
| 1452 | if (ret) { |
| 1453 | dev_err(&s->udev->dev, "Could not initialize vb2 queue\n"); |
| 1454 | goto err_free_mem; |
| 1455 | } |
| 1456 | |
| 1457 | /* Register controls */ |
| 1458 | switch (s->cfg->tuner) { |
| 1459 | case RTL2832_TUNER_E4000: |
Antti Palosaari | 0914300 | 2014-02-10 22:15:01 -0300 | [diff] [blame] | 1460 | v4l2_ctrl_handler_init(&s->hdl, 9); |
| 1461 | if (sd) |
| 1462 | v4l2_ctrl_add_handler(&s->hdl, sd->ctrl_handler, NULL); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1463 | break; |
| 1464 | case RTL2832_TUNER_R820T: |
| 1465 | v4l2_ctrl_handler_init(&s->hdl, 2); |
Anthony DeStefano | 8559496 | 2014-04-09 23:08:49 -0300 | [diff] [blame] | 1466 | s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, ops, |
| 1467 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, |
| 1468 | 0, 1, 1, 1); |
| 1469 | s->bandwidth = v4l2_ctrl_new_std(&s->hdl, ops, |
| 1470 | V4L2_CID_RF_TUNER_BANDWIDTH, |
| 1471 | 0, 8000000, 100000, 0); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1472 | v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false); |
| 1473 | break; |
| 1474 | case RTL2832_TUNER_FC0012: |
| 1475 | case RTL2832_TUNER_FC0013: |
| 1476 | v4l2_ctrl_handler_init(&s->hdl, 2); |
Anthony DeStefano | 8559496 | 2014-04-09 23:08:49 -0300 | [diff] [blame] | 1477 | s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, ops, |
| 1478 | V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, |
| 1479 | 0, 1, 1, 1); |
| 1480 | s->bandwidth = v4l2_ctrl_new_std(&s->hdl, ops, |
| 1481 | V4L2_CID_RF_TUNER_BANDWIDTH, |
| 1482 | 6000000, 8000000, 1000000, |
| 1483 | 6000000); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1484 | v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false); |
| 1485 | break; |
| 1486 | default: |
| 1487 | v4l2_ctrl_handler_init(&s->hdl, 0); |
| 1488 | dev_notice(&s->udev->dev, "%s: Unsupported tuner\n", |
| 1489 | KBUILD_MODNAME); |
| 1490 | goto err_free_controls; |
| 1491 | } |
| 1492 | |
| 1493 | if (s->hdl.error) { |
| 1494 | ret = s->hdl.error; |
| 1495 | dev_err(&s->udev->dev, "Could not initialize controls\n"); |
| 1496 | goto err_free_controls; |
| 1497 | } |
| 1498 | |
| 1499 | /* Init video_device structure */ |
| 1500 | s->vdev = rtl2832_sdr_template; |
| 1501 | s->vdev.queue = &s->vb_queue; |
| 1502 | s->vdev.queue->lock = &s->vb_queue_lock; |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1503 | video_set_drvdata(&s->vdev, s); |
| 1504 | |
| 1505 | /* Register the v4l2_device structure */ |
| 1506 | s->v4l2_dev.release = rtl2832_sdr_video_release; |
| 1507 | ret = v4l2_device_register(&s->udev->dev, &s->v4l2_dev); |
| 1508 | if (ret) { |
| 1509 | dev_err(&s->udev->dev, |
| 1510 | "Failed to register v4l2-device (%d)\n", ret); |
| 1511 | goto err_free_controls; |
| 1512 | } |
| 1513 | |
| 1514 | s->v4l2_dev.ctrl_handler = &s->hdl; |
| 1515 | s->vdev.v4l2_dev = &s->v4l2_dev; |
| 1516 | s->vdev.lock = &s->v4l2_lock; |
| 1517 | s->vdev.vfl_dir = VFL_DIR_RX; |
| 1518 | |
| 1519 | ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1); |
| 1520 | if (ret) { |
| 1521 | dev_err(&s->udev->dev, |
| 1522 | "Failed to register as video device (%d)\n", |
| 1523 | ret); |
| 1524 | goto err_unregister_v4l2_dev; |
| 1525 | } |
| 1526 | dev_info(&s->udev->dev, "Registered as %s\n", |
| 1527 | video_device_node_name(&s->vdev)); |
| 1528 | |
| 1529 | fe->sec_priv = s; |
| 1530 | fe->ops.release_sec = rtl2832_sdr_release_sec; |
| 1531 | |
| 1532 | dev_info(&s->i2c->dev, "%s: Realtek RTL2832 SDR attached\n", |
| 1533 | KBUILD_MODNAME); |
Antti Palosaari | 624ce38 | 2014-07-14 21:16:50 -0300 | [diff] [blame] | 1534 | dev_notice(&s->udev->dev, |
| 1535 | "%s: SDR API is still slightly experimental and functionality changes may follow\n", |
| 1536 | KBUILD_MODNAME); |
Antti Palosaari | 7711389 | 2013-09-10 00:07:56 -0300 | [diff] [blame] | 1537 | return fe; |
| 1538 | |
| 1539 | err_unregister_v4l2_dev: |
| 1540 | v4l2_device_unregister(&s->v4l2_dev); |
| 1541 | err_free_controls: |
| 1542 | v4l2_ctrl_handler_free(&s->hdl); |
| 1543 | err_free_mem: |
| 1544 | kfree(s); |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | EXPORT_SYMBOL(rtl2832_sdr_attach); |
| 1548 | |
| 1549 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1550 | MODULE_DESCRIPTION("Realtek RTL2832 SDR driver"); |
| 1551 | MODULE_LICENSE("GPL"); |