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