airspy.c revision 7057005db6482516099e7b48b3bebaf9a3f213cb
1/* 2 * AirSpy SDR driver 3 * 4 * Copyright (C) 2014 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 17#include <linux/module.h> 18#include <linux/slab.h> 19#include <linux/usb.h> 20#include <media/v4l2-device.h> 21#include <media/v4l2-ioctl.h> 22#include <media/v4l2-ctrls.h> 23#include <media/v4l2-event.h> 24#include <media/videobuf2-vmalloc.h> 25 26/* AirSpy USB API commands (from AirSpy Library) */ 27enum { 28 CMD_INVALID = 0x00, 29 CMD_RECEIVER_MODE = 0x01, 30 CMD_SI5351C_WRITE = 0x02, 31 CMD_SI5351C_READ = 0x03, 32 CMD_R820T_WRITE = 0x04, 33 CMD_R820T_READ = 0x05, 34 CMD_SPIFLASH_ERASE = 0x06, 35 CMD_SPIFLASH_WRITE = 0x07, 36 CMD_SPIFLASH_READ = 0x08, 37 CMD_BOARD_ID_READ = 0x09, 38 CMD_VERSION_STRING_READ = 0x0a, 39 CMD_BOARD_PARTID_SERIALNO_READ = 0x0b, 40 CMD_SET_SAMPLE_RATE = 0x0c, 41 CMD_SET_FREQ = 0x0d, 42 CMD_SET_LNA_GAIN = 0x0e, 43 CMD_SET_MIXER_GAIN = 0x0f, 44 CMD_SET_VGA_GAIN = 0x10, 45 CMD_SET_LNA_AGC = 0x11, 46 CMD_SET_MIXER_AGC = 0x12, 47 CMD_SET_PACKING = 0x13, 48}; 49 50/* 51 * bEndpointAddress 0x81 EP 1 IN 52 * Transfer Type Bulk 53 * wMaxPacketSize 0x0200 1x 512 bytes 54 */ 55#define MAX_BULK_BUFS (6) 56#define BULK_BUFFER_SIZE (128 * 512) 57 58static const struct v4l2_frequency_band bands[] = { 59 { 60 .tuner = 0, 61 .type = V4L2_TUNER_ADC, 62 .index = 0, 63 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 64 .rangelow = 20000000, 65 .rangehigh = 20000000, 66 }, 67}; 68 69static const struct v4l2_frequency_band bands_rf[] = { 70 { 71 .tuner = 1, 72 .type = V4L2_TUNER_RF, 73 .index = 0, 74 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 75 .rangelow = 24000000, 76 .rangehigh = 1750000000, 77 }, 78}; 79 80/* stream formats */ 81struct airspy_format { 82 char *name; 83 u32 pixelformat; 84 u32 buffersize; 85}; 86 87/* format descriptions for capture and preview */ 88static struct airspy_format formats[] = { 89 { 90 .name = "Real U12LE", 91 .pixelformat = V4L2_SDR_FMT_RU12LE, 92 .buffersize = BULK_BUFFER_SIZE, 93 }, 94}; 95 96static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats); 97 98/* intermediate buffers with raw data from the USB device */ 99struct airspy_frame_buf { 100 struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */ 101 struct list_head list; 102}; 103 104struct airspy { 105#define POWER_ON (1 << 1) 106#define URB_BUF (1 << 2) 107#define USB_STATE_URB_BUF (1 << 3) 108 unsigned long flags; 109 110 struct usb_device *udev; 111 struct video_device vdev; 112 struct v4l2_device v4l2_dev; 113 114 /* videobuf2 queue and queued buffers list */ 115 struct vb2_queue vb_queue; 116 struct list_head queued_bufs; 117 spinlock_t queued_bufs_lock; /* Protects queued_bufs */ 118 unsigned sequence; /* Buffer sequence counter */ 119 unsigned int vb_full; /* vb is full and packets dropped */ 120 121 /* Note if taking both locks v4l2_lock must always be locked first! */ 122 struct mutex v4l2_lock; /* Protects everything else */ 123 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */ 124 125 struct urb *urb_list[MAX_BULK_BUFS]; 126 int buf_num; 127 unsigned long buf_size; 128 u8 *buf_list[MAX_BULK_BUFS]; 129 dma_addr_t dma_addr[MAX_BULK_BUFS]; 130 int urbs_initialized; 131 int urbs_submitted; 132 133 /* USB control message buffer */ 134 #define BUF_SIZE 24 135 u8 buf[BUF_SIZE]; 136 137 /* Current configuration */ 138 unsigned int f_adc; 139 unsigned int f_rf; 140 u32 pixelformat; 141 u32 buffersize; 142 143 /* Controls */ 144 struct v4l2_ctrl_handler hdl; 145 struct v4l2_ctrl *lna_gain_auto; 146 struct v4l2_ctrl *lna_gain; 147 struct v4l2_ctrl *mixer_gain_auto; 148 struct v4l2_ctrl *mixer_gain; 149 struct v4l2_ctrl *if_gain; 150 151 /* Sample rate calc */ 152 unsigned long jiffies_next; 153 unsigned int sample; 154 unsigned int sample_measured; 155}; 156 157#define airspy_dbg_usb_control_msg(_udev, _r, _t, _v, _i, _b, _l) { \ 158 char *_direction; \ 159 if (_t & USB_DIR_IN) \ 160 _direction = "<<<"; \ 161 else \ 162 _direction = ">>>"; \ 163 dev_dbg(&_udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \ 164 "%s %*ph\n", __func__, _t, _r, _v & 0xff, _v >> 8, \ 165 _i & 0xff, _i >> 8, _l & 0xff, _l >> 8, _direction, \ 166 _l, _b); \ 167} 168 169/* execute firmware command */ 170static int airspy_ctrl_msg(struct airspy *s, u8 request, u16 value, u16 index, 171 u8 *data, u16 size) 172{ 173 int ret; 174 unsigned int pipe; 175 u8 requesttype; 176 177 switch (request) { 178 case CMD_RECEIVER_MODE: 179 case CMD_SET_FREQ: 180 pipe = usb_sndctrlpipe(s->udev, 0); 181 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); 182 break; 183 case CMD_BOARD_ID_READ: 184 case CMD_VERSION_STRING_READ: 185 case CMD_BOARD_PARTID_SERIALNO_READ: 186 case CMD_SET_LNA_GAIN: 187 case CMD_SET_MIXER_GAIN: 188 case CMD_SET_VGA_GAIN: 189 case CMD_SET_LNA_AGC: 190 case CMD_SET_MIXER_AGC: 191 pipe = usb_rcvctrlpipe(s->udev, 0); 192 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); 193 break; 194 default: 195 dev_err(&s->udev->dev, "Unknown command %02x\n", request); 196 ret = -EINVAL; 197 goto err; 198 } 199 200 /* write request */ 201 if (!(requesttype & USB_DIR_IN)) 202 memcpy(s->buf, data, size); 203 204 ret = usb_control_msg(s->udev, pipe, request, requesttype, value, 205 index, s->buf, size, 1000); 206 airspy_dbg_usb_control_msg(s->udev, request, requesttype, value, 207 index, s->buf, size); 208 if (ret < 0) { 209 dev_err(&s->udev->dev, 210 "usb_control_msg() failed %d request %02x\n", 211 ret, request); 212 goto err; 213 } 214 215 /* read request */ 216 if (requesttype & USB_DIR_IN) 217 memcpy(data, s->buf, size); 218 219 return 0; 220err: 221 return ret; 222} 223 224/* Private functions */ 225static struct airspy_frame_buf *airspy_get_next_fill_buf(struct airspy *s) 226{ 227 unsigned long flags = 0; 228 struct airspy_frame_buf *buf = NULL; 229 230 spin_lock_irqsave(&s->queued_bufs_lock, flags); 231 if (list_empty(&s->queued_bufs)) 232 goto leave; 233 234 buf = list_entry(s->queued_bufs.next, 235 struct airspy_frame_buf, list); 236 list_del(&buf->list); 237leave: 238 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); 239 return buf; 240} 241 242static unsigned int airspy_convert_stream(struct airspy *s, 243 void *dst, void *src, unsigned int src_len) 244{ 245 unsigned int dst_len; 246 247 if (s->pixelformat == V4L2_SDR_FMT_RU12LE) { 248 memcpy(dst, src, src_len); 249 dst_len = src_len; 250 } else { 251 dst_len = 0; 252 } 253 254 /* calculate samping rate and output it in 10 seconds intervals */ 255 if (unlikely(time_is_before_jiffies(s->jiffies_next))) { 256 #define MSECS 10000UL 257 unsigned int samples = s->sample - s->sample_measured; 258 259 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS); 260 s->sample_measured = s->sample; 261 dev_dbg(&s->udev->dev, 262 "slen=%d samples=%u msecs=%lu sample rate=%lu\n", 263 src_len, samples, MSECS, 264 samples * 1000UL / MSECS); 265 } 266 267 /* total number of samples */ 268 s->sample += src_len / 2; 269 270 return dst_len; 271} 272 273/* 274 * This gets called for the bulk stream pipe. This is done in interrupt 275 * time, so it has to be fast, not crash, and not stall. Neat. 276 */ 277static void airspy_urb_complete(struct urb *urb) 278{ 279 struct airspy *s = urb->context; 280 struct airspy_frame_buf *fbuf; 281 282 dev_dbg_ratelimited(&s->udev->dev, 283 "%s: status=%d length=%d/%d errors=%d\n", 284 __func__, urb->status, urb->actual_length, 285 urb->transfer_buffer_length, urb->error_count); 286 287 switch (urb->status) { 288 case 0: /* success */ 289 case -ETIMEDOUT: /* NAK */ 290 break; 291 case -ECONNRESET: /* kill */ 292 case -ENOENT: 293 case -ESHUTDOWN: 294 return; 295 default: /* error */ 296 dev_err_ratelimited(&s->udev->dev, "URB failed %d\n", 297 urb->status); 298 break; 299 } 300 301 if (likely(urb->actual_length > 0)) { 302 void *ptr; 303 unsigned int len; 304 /* get free framebuffer */ 305 fbuf = airspy_get_next_fill_buf(s); 306 if (unlikely(fbuf == NULL)) { 307 s->vb_full++; 308 dev_notice_ratelimited(&s->udev->dev, 309 "videobuf is full, %d packets dropped\n", 310 s->vb_full); 311 goto skip; 312 } 313 314 /* fill framebuffer */ 315 ptr = vb2_plane_vaddr(&fbuf->vb, 0); 316 len = airspy_convert_stream(s, ptr, urb->transfer_buffer, 317 urb->actual_length); 318 vb2_set_plane_payload(&fbuf->vb, 0, len); 319 v4l2_get_timestamp(&fbuf->vb.v4l2_buf.timestamp); 320 fbuf->vb.v4l2_buf.sequence = s->sequence++; 321 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE); 322 } 323skip: 324 usb_submit_urb(urb, GFP_ATOMIC); 325} 326 327static int airspy_kill_urbs(struct airspy *s) 328{ 329 int i; 330 331 for (i = s->urbs_submitted - 1; i >= 0; i--) { 332 dev_dbg(&s->udev->dev, "%s: kill urb=%d\n", __func__, i); 333 /* stop the URB */ 334 usb_kill_urb(s->urb_list[i]); 335 } 336 s->urbs_submitted = 0; 337 338 return 0; 339} 340 341static int airspy_submit_urbs(struct airspy *s) 342{ 343 int i, ret; 344 345 for (i = 0; i < s->urbs_initialized; i++) { 346 dev_dbg(&s->udev->dev, "%s: submit urb=%d\n", __func__, i); 347 ret = usb_submit_urb(s->urb_list[i], GFP_ATOMIC); 348 if (ret) { 349 dev_err(&s->udev->dev, 350 "Could not submit URB no. %d - get them all back\n", 351 i); 352 airspy_kill_urbs(s); 353 return ret; 354 } 355 s->urbs_submitted++; 356 } 357 358 return 0; 359} 360 361static int airspy_free_stream_bufs(struct airspy *s) 362{ 363 if (s->flags & USB_STATE_URB_BUF) { 364 while (s->buf_num) { 365 s->buf_num--; 366 dev_dbg(&s->udev->dev, "%s: free buf=%d\n", 367 __func__, s->buf_num); 368 usb_free_coherent(s->udev, s->buf_size, 369 s->buf_list[s->buf_num], 370 s->dma_addr[s->buf_num]); 371 } 372 } 373 s->flags &= ~USB_STATE_URB_BUF; 374 375 return 0; 376} 377 378static int airspy_alloc_stream_bufs(struct airspy *s) 379{ 380 s->buf_num = 0; 381 s->buf_size = BULK_BUFFER_SIZE; 382 383 dev_dbg(&s->udev->dev, 384 "%s: all in all I will use %u bytes for streaming\n", 385 __func__, MAX_BULK_BUFS * BULK_BUFFER_SIZE); 386 387 for (s->buf_num = 0; s->buf_num < MAX_BULK_BUFS; s->buf_num++) { 388 s->buf_list[s->buf_num] = usb_alloc_coherent(s->udev, 389 BULK_BUFFER_SIZE, GFP_ATOMIC, 390 &s->dma_addr[s->buf_num]); 391 if (!s->buf_list[s->buf_num]) { 392 dev_dbg(&s->udev->dev, "%s: alloc buf=%d failed\n", 393 __func__, s->buf_num); 394 airspy_free_stream_bufs(s); 395 return -ENOMEM; 396 } 397 398 dev_dbg(&s->udev->dev, "%s: alloc buf=%d %p (dma %llu)\n", 399 __func__, s->buf_num, 400 s->buf_list[s->buf_num], 401 (long long)s->dma_addr[s->buf_num]); 402 s->flags |= USB_STATE_URB_BUF; 403 } 404 405 return 0; 406} 407 408static int airspy_free_urbs(struct airspy *s) 409{ 410 int i; 411 412 airspy_kill_urbs(s); 413 414 for (i = s->urbs_initialized - 1; i >= 0; i--) { 415 if (s->urb_list[i]) { 416 dev_dbg(&s->udev->dev, "%s: free urb=%d\n", 417 __func__, i); 418 /* free the URBs */ 419 usb_free_urb(s->urb_list[i]); 420 } 421 } 422 s->urbs_initialized = 0; 423 424 return 0; 425} 426 427static int airspy_alloc_urbs(struct airspy *s) 428{ 429 int i, j; 430 431 /* allocate the URBs */ 432 for (i = 0; i < MAX_BULK_BUFS; i++) { 433 dev_dbg(&s->udev->dev, "%s: alloc urb=%d\n", __func__, i); 434 s->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC); 435 if (!s->urb_list[i]) { 436 dev_dbg(&s->udev->dev, "%s: failed\n", __func__); 437 for (j = 0; j < i; j++) 438 usb_free_urb(s->urb_list[j]); 439 return -ENOMEM; 440 } 441 usb_fill_bulk_urb(s->urb_list[i], 442 s->udev, 443 usb_rcvbulkpipe(s->udev, 0x81), 444 s->buf_list[i], 445 BULK_BUFFER_SIZE, 446 airspy_urb_complete, s); 447 448 s->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 449 s->urb_list[i]->transfer_dma = s->dma_addr[i]; 450 s->urbs_initialized++; 451 } 452 453 return 0; 454} 455 456/* Must be called with vb_queue_lock hold */ 457static void airspy_cleanup_queued_bufs(struct airspy *s) 458{ 459 unsigned long flags = 0; 460 461 dev_dbg(&s->udev->dev, "%s:\n", __func__); 462 463 spin_lock_irqsave(&s->queued_bufs_lock, flags); 464 while (!list_empty(&s->queued_bufs)) { 465 struct airspy_frame_buf *buf; 466 467 buf = list_entry(s->queued_bufs.next, 468 struct airspy_frame_buf, list); 469 list_del(&buf->list); 470 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); 471 } 472 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); 473} 474 475/* The user yanked out the cable... */ 476static void airspy_disconnect(struct usb_interface *intf) 477{ 478 struct v4l2_device *v = usb_get_intfdata(intf); 479 struct airspy *s = container_of(v, struct airspy, v4l2_dev); 480 481 dev_dbg(&s->udev->dev, "%s:\n", __func__); 482 483 mutex_lock(&s->vb_queue_lock); 484 mutex_lock(&s->v4l2_lock); 485 /* No need to keep the urbs around after disconnection */ 486 s->udev = NULL; 487 v4l2_device_disconnect(&s->v4l2_dev); 488 video_unregister_device(&s->vdev); 489 mutex_unlock(&s->v4l2_lock); 490 mutex_unlock(&s->vb_queue_lock); 491 492 v4l2_device_put(&s->v4l2_dev); 493} 494 495/* Videobuf2 operations */ 496static int airspy_queue_setup(struct vb2_queue *vq, 497 const struct v4l2_format *fmt, unsigned int *nbuffers, 498 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) 499{ 500 struct airspy *s = vb2_get_drv_priv(vq); 501 502 dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers); 503 504 /* Need at least 8 buffers */ 505 if (vq->num_buffers + *nbuffers < 8) 506 *nbuffers = 8 - vq->num_buffers; 507 *nplanes = 1; 508 sizes[0] = PAGE_ALIGN(s->buffersize); 509 510 dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n", 511 __func__, *nbuffers, sizes[0]); 512 return 0; 513} 514 515static void airspy_buf_queue(struct vb2_buffer *vb) 516{ 517 struct airspy *s = vb2_get_drv_priv(vb->vb2_queue); 518 struct airspy_frame_buf *buf = 519 container_of(vb, struct airspy_frame_buf, vb); 520 unsigned long flags = 0; 521 522 /* Check the device has not disconnected between prep and queuing */ 523 if (unlikely(!s->udev)) { 524 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); 525 return; 526 } 527 528 spin_lock_irqsave(&s->queued_bufs_lock, flags); 529 list_add_tail(&buf->list, &s->queued_bufs); 530 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); 531} 532 533static int airspy_start_streaming(struct vb2_queue *vq, unsigned int count) 534{ 535 struct airspy *s = vb2_get_drv_priv(vq); 536 int ret; 537 538 dev_dbg(&s->udev->dev, "%s:\n", __func__); 539 540 if (!s->udev) 541 return -ENODEV; 542 543 mutex_lock(&s->v4l2_lock); 544 545 s->sequence = 0; 546 547 set_bit(POWER_ON, &s->flags); 548 549 ret = airspy_alloc_stream_bufs(s); 550 if (ret) 551 goto err_clear_bit; 552 553 ret = airspy_alloc_urbs(s); 554 if (ret) 555 goto err_free_stream_bufs; 556 557 ret = airspy_submit_urbs(s); 558 if (ret) 559 goto err_free_urbs; 560 561 /* start hardware streaming */ 562 ret = airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 1, 0, NULL, 0); 563 if (ret) 564 goto err_kill_urbs; 565 566 goto exit_mutex_unlock; 567 568err_kill_urbs: 569 airspy_kill_urbs(s); 570err_free_urbs: 571 airspy_free_urbs(s); 572err_free_stream_bufs: 573 airspy_free_stream_bufs(s); 574err_clear_bit: 575 clear_bit(POWER_ON, &s->flags); 576 577 /* return all queued buffers to vb2 */ 578 { 579 struct airspy_frame_buf *buf, *tmp; 580 581 list_for_each_entry_safe(buf, tmp, &s->queued_bufs, list) { 582 list_del(&buf->list); 583 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED); 584 } 585 } 586 587exit_mutex_unlock: 588 mutex_unlock(&s->v4l2_lock); 589 590 return ret; 591} 592 593static void airspy_stop_streaming(struct vb2_queue *vq) 594{ 595 struct airspy *s = vb2_get_drv_priv(vq); 596 597 dev_dbg(&s->udev->dev, "%s:\n", __func__); 598 599 mutex_lock(&s->v4l2_lock); 600 601 /* stop hardware streaming */ 602 airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 0, 0, NULL, 0); 603 604 airspy_kill_urbs(s); 605 airspy_free_urbs(s); 606 airspy_free_stream_bufs(s); 607 608 airspy_cleanup_queued_bufs(s); 609 610 clear_bit(POWER_ON, &s->flags); 611 612 mutex_unlock(&s->v4l2_lock); 613} 614 615static struct vb2_ops airspy_vb2_ops = { 616 .queue_setup = airspy_queue_setup, 617 .buf_queue = airspy_buf_queue, 618 .start_streaming = airspy_start_streaming, 619 .stop_streaming = airspy_stop_streaming, 620 .wait_prepare = vb2_ops_wait_prepare, 621 .wait_finish = vb2_ops_wait_finish, 622}; 623 624static int airspy_querycap(struct file *file, void *fh, 625 struct v4l2_capability *cap) 626{ 627 struct airspy *s = video_drvdata(file); 628 629 dev_dbg(&s->udev->dev, "%s:\n", __func__); 630 631 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); 632 strlcpy(cap->card, s->vdev.name, sizeof(cap->card)); 633 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); 634 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING | 635 V4L2_CAP_READWRITE | V4L2_CAP_TUNER; 636 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 637 638 return 0; 639} 640 641static int airspy_enum_fmt_sdr_cap(struct file *file, void *priv, 642 struct v4l2_fmtdesc *f) 643{ 644 struct airspy *s = video_drvdata(file); 645 646 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, f->index); 647 648 if (f->index >= NUM_FORMATS) 649 return -EINVAL; 650 651 strlcpy(f->description, formats[f->index].name, sizeof(f->description)); 652 f->pixelformat = formats[f->index].pixelformat; 653 654 return 0; 655} 656 657static int airspy_g_fmt_sdr_cap(struct file *file, void *priv, 658 struct v4l2_format *f) 659{ 660 struct airspy *s = video_drvdata(file); 661 662 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__, 663 (char *)&s->pixelformat); 664 665 f->fmt.sdr.pixelformat = s->pixelformat; 666 f->fmt.sdr.buffersize = s->buffersize; 667 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); 668 669 return 0; 670} 671 672static int airspy_s_fmt_sdr_cap(struct file *file, void *priv, 673 struct v4l2_format *f) 674{ 675 struct airspy *s = video_drvdata(file); 676 struct vb2_queue *q = &s->vb_queue; 677 int i; 678 679 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__, 680 (char *)&f->fmt.sdr.pixelformat); 681 682 if (vb2_is_busy(q)) 683 return -EBUSY; 684 685 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); 686 for (i = 0; i < NUM_FORMATS; i++) { 687 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 688 s->pixelformat = formats[i].pixelformat; 689 s->buffersize = formats[i].buffersize; 690 f->fmt.sdr.buffersize = formats[i].buffersize; 691 return 0; 692 } 693 } 694 695 s->pixelformat = formats[0].pixelformat; 696 s->buffersize = formats[0].buffersize; 697 f->fmt.sdr.pixelformat = formats[0].pixelformat; 698 f->fmt.sdr.buffersize = formats[0].buffersize; 699 700 return 0; 701} 702 703static int airspy_try_fmt_sdr_cap(struct file *file, void *priv, 704 struct v4l2_format *f) 705{ 706 struct airspy *s = video_drvdata(file); 707 int i; 708 709 dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__, 710 (char *)&f->fmt.sdr.pixelformat); 711 712 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); 713 for (i = 0; i < NUM_FORMATS; i++) { 714 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 715 f->fmt.sdr.buffersize = formats[i].buffersize; 716 return 0; 717 } 718 } 719 720 f->fmt.sdr.pixelformat = formats[0].pixelformat; 721 f->fmt.sdr.buffersize = formats[0].buffersize; 722 723 return 0; 724} 725 726static int airspy_s_tuner(struct file *file, void *priv, 727 const struct v4l2_tuner *v) 728{ 729 struct airspy *s = video_drvdata(file); 730 int ret; 731 732 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index); 733 734 if (v->index == 0) 735 ret = 0; 736 else if (v->index == 1) 737 ret = 0; 738 else 739 ret = -EINVAL; 740 741 return ret; 742} 743 744static int airspy_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v) 745{ 746 struct airspy *s = video_drvdata(file); 747 int ret; 748 749 dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index); 750 751 if (v->index == 0) { 752 strlcpy(v->name, "AirSpy ADC", sizeof(v->name)); 753 v->type = V4L2_TUNER_ADC; 754 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 755 v->rangelow = bands[0].rangelow; 756 v->rangehigh = bands[0].rangehigh; 757 ret = 0; 758 } else if (v->index == 1) { 759 strlcpy(v->name, "AirSpy RF", sizeof(v->name)); 760 v->type = V4L2_TUNER_RF; 761 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 762 v->rangelow = bands_rf[0].rangelow; 763 v->rangehigh = bands_rf[0].rangehigh; 764 ret = 0; 765 } else { 766 ret = -EINVAL; 767 } 768 769 return ret; 770} 771 772static int airspy_g_frequency(struct file *file, void *priv, 773 struct v4l2_frequency *f) 774{ 775 struct airspy *s = video_drvdata(file); 776 int ret = 0; 777 778 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n", 779 __func__, f->tuner, f->type); 780 781 if (f->tuner == 0) { 782 f->type = V4L2_TUNER_ADC; 783 f->frequency = s->f_adc; 784 ret = 0; 785 } else if (f->tuner == 1) { 786 f->type = V4L2_TUNER_RF; 787 f->frequency = s->f_rf; 788 } else { 789 ret = -EINVAL; 790 } 791 792 return ret; 793} 794 795static int airspy_s_frequency(struct file *file, void *priv, 796 const struct v4l2_frequency *f) 797{ 798 struct airspy *s = video_drvdata(file); 799 int ret; 800 u8 buf[4]; 801 802 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n", 803 __func__, f->tuner, f->type, f->frequency); 804 805 if (f->tuner == 0) { 806 s->f_adc = clamp_t(unsigned int, f->frequency, 807 bands[0].rangelow, 808 bands[0].rangehigh); 809 dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n", 810 __func__, s->f_adc); 811 ret = 0; 812 } else if (f->tuner == 1) { 813 s->f_rf = clamp_t(unsigned int, f->frequency, 814 bands_rf[0].rangelow, 815 bands_rf[0].rangehigh); 816 dev_dbg(&s->udev->dev, "%s: RF frequency=%u Hz\n", 817 __func__, s->f_rf); 818 buf[0] = (s->f_rf >> 0) & 0xff; 819 buf[1] = (s->f_rf >> 8) & 0xff; 820 buf[2] = (s->f_rf >> 16) & 0xff; 821 buf[3] = (s->f_rf >> 24) & 0xff; 822 ret = airspy_ctrl_msg(s, CMD_SET_FREQ, 0, 0, buf, 4); 823 } else { 824 ret = -EINVAL; 825 } 826 827 return ret; 828} 829 830static int airspy_enum_freq_bands(struct file *file, void *priv, 831 struct v4l2_frequency_band *band) 832{ 833 struct airspy *s = video_drvdata(file); 834 int ret; 835 836 dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n", 837 __func__, band->tuner, band->type, band->index); 838 839 if (band->tuner == 0) { 840 if (band->index >= ARRAY_SIZE(bands)) { 841 ret = -EINVAL; 842 } else { 843 *band = bands[band->index]; 844 ret = 0; 845 } 846 } else if (band->tuner == 1) { 847 if (band->index >= ARRAY_SIZE(bands_rf)) { 848 ret = -EINVAL; 849 } else { 850 *band = bands_rf[band->index]; 851 ret = 0; 852 } 853 } else { 854 ret = -EINVAL; 855 } 856 857 return ret; 858} 859 860static const struct v4l2_ioctl_ops airspy_ioctl_ops = { 861 .vidioc_querycap = airspy_querycap, 862 863 .vidioc_enum_fmt_sdr_cap = airspy_enum_fmt_sdr_cap, 864 .vidioc_g_fmt_sdr_cap = airspy_g_fmt_sdr_cap, 865 .vidioc_s_fmt_sdr_cap = airspy_s_fmt_sdr_cap, 866 .vidioc_try_fmt_sdr_cap = airspy_try_fmt_sdr_cap, 867 868 .vidioc_reqbufs = vb2_ioctl_reqbufs, 869 .vidioc_create_bufs = vb2_ioctl_create_bufs, 870 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 871 .vidioc_querybuf = vb2_ioctl_querybuf, 872 .vidioc_qbuf = vb2_ioctl_qbuf, 873 .vidioc_dqbuf = vb2_ioctl_dqbuf, 874 875 .vidioc_streamon = vb2_ioctl_streamon, 876 .vidioc_streamoff = vb2_ioctl_streamoff, 877 878 .vidioc_g_tuner = airspy_g_tuner, 879 .vidioc_s_tuner = airspy_s_tuner, 880 881 .vidioc_g_frequency = airspy_g_frequency, 882 .vidioc_s_frequency = airspy_s_frequency, 883 .vidioc_enum_freq_bands = airspy_enum_freq_bands, 884 885 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 886 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 887 .vidioc_log_status = v4l2_ctrl_log_status, 888}; 889 890static const struct v4l2_file_operations airspy_fops = { 891 .owner = THIS_MODULE, 892 .open = v4l2_fh_open, 893 .release = vb2_fop_release, 894 .read = vb2_fop_read, 895 .poll = vb2_fop_poll, 896 .mmap = vb2_fop_mmap, 897 .unlocked_ioctl = video_ioctl2, 898}; 899 900static struct video_device airspy_template = { 901 .name = "AirSpy SDR", 902 .release = video_device_release_empty, 903 .fops = &airspy_fops, 904 .ioctl_ops = &airspy_ioctl_ops, 905}; 906 907static void airspy_video_release(struct v4l2_device *v) 908{ 909 struct airspy *s = container_of(v, struct airspy, v4l2_dev); 910 911 v4l2_ctrl_handler_free(&s->hdl); 912 v4l2_device_unregister(&s->v4l2_dev); 913 kfree(s); 914} 915 916static int airspy_set_lna_gain(struct airspy *s) 917{ 918 int ret; 919 u8 u8tmp; 920 921 dev_dbg(&s->udev->dev, "%s: lna auto=%d->%d val=%d->%d\n", 922 __func__, s->lna_gain_auto->cur.val, 923 s->lna_gain_auto->val, s->lna_gain->cur.val, 924 s->lna_gain->val); 925 926 ret = airspy_ctrl_msg(s, CMD_SET_LNA_AGC, 0, s->lna_gain_auto->val, 927 &u8tmp, 1); 928 if (ret) 929 goto err; 930 931 if (s->lna_gain_auto->val == false) { 932 ret = airspy_ctrl_msg(s, CMD_SET_LNA_GAIN, 0, s->lna_gain->val, 933 &u8tmp, 1); 934 if (ret) 935 goto err; 936 } 937err: 938 if (ret) 939 dev_dbg(&s->udev->dev, "%s: failed=%d\n", __func__, ret); 940 941 return ret; 942} 943 944static int airspy_set_mixer_gain(struct airspy *s) 945{ 946 int ret; 947 u8 u8tmp; 948 949 dev_dbg(&s->udev->dev, "%s: mixer auto=%d->%d val=%d->%d\n", 950 __func__, s->mixer_gain_auto->cur.val, 951 s->mixer_gain_auto->val, s->mixer_gain->cur.val, 952 s->mixer_gain->val); 953 954 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_AGC, 0, s->mixer_gain_auto->val, 955 &u8tmp, 1); 956 if (ret) 957 goto err; 958 959 if (s->mixer_gain_auto->val == false) { 960 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_GAIN, 0, 961 s->mixer_gain->val, &u8tmp, 1); 962 if (ret) 963 goto err; 964 } 965err: 966 if (ret) 967 dev_dbg(&s->udev->dev, "%s: failed=%d\n", __func__, ret); 968 969 return ret; 970} 971 972static int airspy_set_if_gain(struct airspy *s) 973{ 974 int ret; 975 u8 u8tmp; 976 977 dev_dbg(&s->udev->dev, "%s: val=%d->%d\n", 978 __func__, s->if_gain->cur.val, s->if_gain->val); 979 980 ret = airspy_ctrl_msg(s, CMD_SET_VGA_GAIN, 0, s->if_gain->val, 981 &u8tmp, 1); 982 if (ret) 983 goto err; 984err: 985 if (ret) 986 dev_dbg(&s->udev->dev, "%s: failed=%d\n", __func__, ret); 987 988 return ret; 989} 990 991static int airspy_s_ctrl(struct v4l2_ctrl *ctrl) 992{ 993 struct airspy *s = container_of(ctrl->handler, struct airspy, hdl); 994 int ret; 995 996 switch (ctrl->id) { 997 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO: 998 case V4L2_CID_RF_TUNER_LNA_GAIN: 999 ret = airspy_set_lna_gain(s); 1000 break; 1001 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO: 1002 case V4L2_CID_RF_TUNER_MIXER_GAIN: 1003 ret = airspy_set_mixer_gain(s); 1004 break; 1005 case V4L2_CID_RF_TUNER_IF_GAIN: 1006 ret = airspy_set_if_gain(s); 1007 break; 1008 default: 1009 dev_dbg(&s->udev->dev, "%s: unknown ctrl: id=%d name=%s\n", 1010 __func__, ctrl->id, ctrl->name); 1011 ret = -EINVAL; 1012 } 1013 1014 return ret; 1015} 1016 1017static const struct v4l2_ctrl_ops airspy_ctrl_ops = { 1018 .s_ctrl = airspy_s_ctrl, 1019}; 1020 1021static int airspy_probe(struct usb_interface *intf, 1022 const struct usb_device_id *id) 1023{ 1024 struct usb_device *udev = interface_to_usbdev(intf); 1025 struct airspy *s = NULL; 1026 int ret; 1027 u8 u8tmp, buf[BUF_SIZE]; 1028 1029 s = kzalloc(sizeof(struct airspy), GFP_KERNEL); 1030 if (s == NULL) { 1031 dev_err(&udev->dev, 1032 "Could not allocate memory for airspy state\n"); 1033 return -ENOMEM; 1034 } 1035 1036 mutex_init(&s->v4l2_lock); 1037 mutex_init(&s->vb_queue_lock); 1038 spin_lock_init(&s->queued_bufs_lock); 1039 INIT_LIST_HEAD(&s->queued_bufs); 1040 s->udev = udev; 1041 s->f_adc = bands[0].rangelow; 1042 s->f_rf = bands_rf[0].rangelow; 1043 s->pixelformat = formats[0].pixelformat; 1044 s->buffersize = formats[0].buffersize; 1045 1046 /* Detect device */ 1047 ret = airspy_ctrl_msg(s, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1); 1048 if (ret == 0) 1049 ret = airspy_ctrl_msg(s, CMD_VERSION_STRING_READ, 0, 0, 1050 buf, BUF_SIZE); 1051 if (ret) { 1052 dev_err(&s->udev->dev, "Could not detect board\n"); 1053 goto err_free_mem; 1054 } 1055 1056 buf[BUF_SIZE - 1] = '\0'; 1057 1058 dev_info(&s->udev->dev, "Board ID: %02x\n", u8tmp); 1059 dev_info(&s->udev->dev, "Firmware version: %s\n", buf); 1060 1061 /* Init videobuf2 queue structure */ 1062 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; 1063 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; 1064 s->vb_queue.drv_priv = s; 1065 s->vb_queue.buf_struct_size = sizeof(struct airspy_frame_buf); 1066 s->vb_queue.ops = &airspy_vb2_ops; 1067 s->vb_queue.mem_ops = &vb2_vmalloc_memops; 1068 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1069 ret = vb2_queue_init(&s->vb_queue); 1070 if (ret) { 1071 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n"); 1072 goto err_free_mem; 1073 } 1074 1075 /* Init video_device structure */ 1076 s->vdev = airspy_template; 1077 s->vdev.queue = &s->vb_queue; 1078 s->vdev.queue->lock = &s->vb_queue_lock; 1079 video_set_drvdata(&s->vdev, s); 1080 1081 /* Register the v4l2_device structure */ 1082 s->v4l2_dev.release = airspy_video_release; 1083 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev); 1084 if (ret) { 1085 dev_err(&s->udev->dev, 1086 "Failed to register v4l2-device (%d)\n", ret); 1087 goto err_free_mem; 1088 } 1089 1090 /* Register controls */ 1091 v4l2_ctrl_handler_init(&s->hdl, 5); 1092 s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, 1093 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 0); 1094 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, 1095 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 14, 1, 8); 1096 v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false); 1097 s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, 1098 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 0); 1099 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, 1100 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 15, 1, 8); 1101 v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false); 1102 s->if_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, 1103 V4L2_CID_RF_TUNER_IF_GAIN, 0, 15, 1, 0); 1104 if (s->hdl.error) { 1105 ret = s->hdl.error; 1106 dev_err(&s->udev->dev, "Could not initialize controls\n"); 1107 goto err_free_controls; 1108 } 1109 1110 v4l2_ctrl_handler_setup(&s->hdl); 1111 1112 s->v4l2_dev.ctrl_handler = &s->hdl; 1113 s->vdev.v4l2_dev = &s->v4l2_dev; 1114 s->vdev.lock = &s->v4l2_lock; 1115 1116 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1); 1117 if (ret) { 1118 dev_err(&s->udev->dev, 1119 "Failed to register as video device (%d)\n", 1120 ret); 1121 goto err_unregister_v4l2_dev; 1122 } 1123 dev_info(&s->udev->dev, "Registered as %s\n", 1124 video_device_node_name(&s->vdev)); 1125 dev_notice(&s->udev->dev, 1126 "%s: SDR API is still slightly experimental and functionality changes may follow\n", 1127 KBUILD_MODNAME); 1128 return 0; 1129 1130err_free_controls: 1131 v4l2_ctrl_handler_free(&s->hdl); 1132err_unregister_v4l2_dev: 1133 v4l2_device_unregister(&s->v4l2_dev); 1134err_free_mem: 1135 kfree(s); 1136 return ret; 1137} 1138 1139/* USB device ID list */ 1140static struct usb_device_id airspy_id_table[] = { 1141 { USB_DEVICE(0x1d50, 0x60a1) }, /* AirSpy */ 1142 { } 1143}; 1144MODULE_DEVICE_TABLE(usb, airspy_id_table); 1145 1146/* USB subsystem interface */ 1147static struct usb_driver airspy_driver = { 1148 .name = KBUILD_MODNAME, 1149 .probe = airspy_probe, 1150 .disconnect = airspy_disconnect, 1151 .id_table = airspy_id_table, 1152}; 1153 1154module_usb_driver(airspy_driver); 1155 1156MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1157MODULE_DESCRIPTION("AirSpy SDR"); 1158MODULE_LICENSE("GPL"); 1159