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