4 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
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-v4l2.h>
25 #include <media/videobuf2-vmalloc.h>
27 /* HackRF USB API commands (from HackRF Library) */
29 CMD_SET_TRANSCEIVER_MODE = 0x01,
30 CMD_SAMPLE_RATE_SET = 0x06,
31 CMD_BASEBAND_FILTER_BANDWIDTH_SET = 0x07,
32 CMD_BOARD_ID_READ = 0x0e,
33 CMD_VERSION_STRING_READ = 0x0f,
35 CMD_SET_LNA_GAIN = 0x13,
36 CMD_SET_VGA_GAIN = 0x14,
40 * bEndpointAddress 0x81 EP 1 IN
42 * wMaxPacketSize 0x0200 1x 512 bytes
44 #define MAX_BULK_BUFS (6)
45 #define BULK_BUFFER_SIZE (128 * 512)
47 static const struct v4l2_frequency_band bands_adc[] = {
50 .type = V4L2_TUNER_ADC,
52 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
54 .rangehigh = 24000000,
58 static const struct v4l2_frequency_band bands_rf[] = {
61 .type = V4L2_TUNER_RF,
63 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
65 .rangehigh = 4294967294LL, /* max u32, hw goes over 7GHz */
70 struct hackrf_format {
76 /* format descriptions for capture and preview */
77 static struct hackrf_format formats[] = {
80 .pixelformat = V4L2_SDR_FMT_CS8,
81 .buffersize = BULK_BUFFER_SIZE,
85 static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
87 /* intermediate buffers with raw data from the USB device */
88 struct hackrf_frame_buf {
89 /* common v4l buffer stuff -- must be first */
90 struct vb2_v4l2_buffer vb;
91 struct list_head list;
95 #define POWER_ON (1 << 1)
96 #define URB_BUF (1 << 2)
97 #define USB_STATE_URB_BUF (1 << 3)
101 struct usb_device *udev;
102 struct video_device vdev;
103 struct v4l2_device v4l2_dev;
105 /* videobuf2 queue and queued buffers list */
106 struct vb2_queue vb_queue;
107 struct list_head queued_bufs;
108 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
109 unsigned sequence; /* Buffer sequence counter */
110 unsigned int vb_full; /* vb is full and packets dropped */
112 /* Note if taking both locks v4l2_lock must always be locked first! */
113 struct mutex v4l2_lock; /* Protects everything else */
114 struct mutex vb_queue_lock; /* Protects vb_queue */
116 struct urb *urb_list[MAX_BULK_BUFS];
118 unsigned long buf_size;
119 u8 *buf_list[MAX_BULK_BUFS];
120 dma_addr_t dma_addr[MAX_BULK_BUFS];
121 int urbs_initialized;
124 /* USB control message buffer */
128 /* Current configuration */
135 struct v4l2_ctrl_handler hdl;
136 struct v4l2_ctrl *bandwidth_auto;
137 struct v4l2_ctrl *bandwidth;
138 struct v4l2_ctrl *lna_gain;
139 struct v4l2_ctrl *if_gain;
141 /* Sample rate calc */
142 unsigned long jiffies_next;
144 unsigned int sample_measured;
147 #define hackrf_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
149 if (_t & USB_DIR_IN) \
150 _direction = "<<<"; \
152 _direction = ">>>"; \
153 dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
154 _t, _r, _v & 0xff, _v >> 8, _i & 0xff, \
155 _i >> 8, _l & 0xff, _l >> 8, _direction, _l, _b); \
158 /* execute firmware command */
159 static int hackrf_ctrl_msg(struct hackrf_dev *dev, u8 request, u16 value,
160 u16 index, u8 *data, u16 size)
167 case CMD_SET_TRANSCEIVER_MODE:
169 case CMD_SAMPLE_RATE_SET:
170 case CMD_BASEBAND_FILTER_BANDWIDTH_SET:
171 pipe = usb_sndctrlpipe(dev->udev, 0);
172 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
174 case CMD_BOARD_ID_READ:
175 case CMD_VERSION_STRING_READ:
176 case CMD_SET_LNA_GAIN:
177 case CMD_SET_VGA_GAIN:
178 pipe = usb_rcvctrlpipe(dev->udev, 0);
179 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
182 dev_err(dev->dev, "Unknown command %02x\n", request);
188 if (!(requesttype & USB_DIR_IN))
189 memcpy(dev->buf, data, size);
191 ret = usb_control_msg(dev->udev, pipe, request, requesttype, value,
192 index, dev->buf, size, 1000);
193 hackrf_dbg_usb_control_msg(dev->dev, request, requesttype, value,
194 index, dev->buf, size);
196 dev_err(dev->dev, "usb_control_msg() failed %d request %02x\n",
202 if (requesttype & USB_DIR_IN)
203 memcpy(data, dev->buf, size);
210 /* Private functions */
211 static struct hackrf_frame_buf *hackrf_get_next_fill_buf(struct hackrf_dev *dev)
214 struct hackrf_frame_buf *buf = NULL;
216 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
217 if (list_empty(&dev->queued_bufs))
220 buf = list_entry(dev->queued_bufs.next, struct hackrf_frame_buf, list);
221 list_del(&buf->list);
223 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
227 static unsigned int hackrf_convert_stream(struct hackrf_dev *dev,
228 void *dst, void *src, unsigned int src_len)
230 memcpy(dst, src, src_len);
232 /* calculate sample rate and output it in 10 seconds intervals */
233 if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
234 #define MSECS 10000UL
235 unsigned int msecs = jiffies_to_msecs(jiffies -
236 dev->jiffies_next + msecs_to_jiffies(MSECS));
237 unsigned int samples = dev->sample - dev->sample_measured;
239 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
240 dev->sample_measured = dev->sample;
241 dev_dbg(dev->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n",
242 src_len, samples, msecs,
243 samples * 1000UL / msecs);
246 /* total number of samples */
247 dev->sample += src_len / 2;
253 * This gets called for the bulk stream pipe. This is done in interrupt
254 * time, so it has to be fast, not crash, and not stall. Neat.
256 static void hackrf_urb_complete(struct urb *urb)
258 struct hackrf_dev *dev = urb->context;
259 struct hackrf_frame_buf *fbuf;
261 dev_dbg_ratelimited(dev->dev, "status=%d length=%d/%d errors=%d\n",
262 urb->status, urb->actual_length,
263 urb->transfer_buffer_length, urb->error_count);
265 switch (urb->status) {
266 case 0: /* success */
267 case -ETIMEDOUT: /* NAK */
269 case -ECONNRESET: /* kill */
274 dev_err_ratelimited(dev->dev, "URB failed %d\n", urb->status);
278 if (likely(urb->actual_length > 0)) {
281 /* get free framebuffer */
282 fbuf = hackrf_get_next_fill_buf(dev);
283 if (unlikely(fbuf == NULL)) {
285 dev_notice_ratelimited(dev->dev,
286 "videobuf is full, %d packets dropped\n",
291 /* fill framebuffer */
292 ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
293 len = hackrf_convert_stream(dev, ptr, urb->transfer_buffer,
295 vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len);
296 v4l2_get_timestamp(&fbuf->vb.timestamp);
297 fbuf->vb.sequence = dev->sequence++;
298 vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
301 usb_submit_urb(urb, GFP_ATOMIC);
304 static int hackrf_kill_urbs(struct hackrf_dev *dev)
308 for (i = dev->urbs_submitted - 1; i >= 0; i--) {
309 dev_dbg(dev->dev, "kill urb=%d\n", i);
311 usb_kill_urb(dev->urb_list[i]);
313 dev->urbs_submitted = 0;
318 static int hackrf_submit_urbs(struct hackrf_dev *dev)
322 for (i = 0; i < dev->urbs_initialized; i++) {
323 dev_dbg(dev->dev, "submit urb=%d\n", i);
324 ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC);
326 dev_err(dev->dev, "Could not submit URB no. %d - get them all back\n",
328 hackrf_kill_urbs(dev);
331 dev->urbs_submitted++;
337 static int hackrf_free_stream_bufs(struct hackrf_dev *dev)
339 if (dev->flags & USB_STATE_URB_BUF) {
340 while (dev->buf_num) {
342 dev_dbg(dev->dev, "free buf=%d\n", dev->buf_num);
343 usb_free_coherent(dev->udev, dev->buf_size,
344 dev->buf_list[dev->buf_num],
345 dev->dma_addr[dev->buf_num]);
348 dev->flags &= ~USB_STATE_URB_BUF;
353 static int hackrf_alloc_stream_bufs(struct hackrf_dev *dev)
356 dev->buf_size = BULK_BUFFER_SIZE;
358 dev_dbg(dev->dev, "all in all I will use %u bytes for streaming\n",
359 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
361 for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
362 dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
363 BULK_BUFFER_SIZE, GFP_ATOMIC,
364 &dev->dma_addr[dev->buf_num]);
365 if (!dev->buf_list[dev->buf_num]) {
366 dev_dbg(dev->dev, "alloc buf=%d failed\n",
368 hackrf_free_stream_bufs(dev);
372 dev_dbg(dev->dev, "alloc buf=%d %p (dma %llu)\n", dev->buf_num,
373 dev->buf_list[dev->buf_num],
374 (long long)dev->dma_addr[dev->buf_num]);
375 dev->flags |= USB_STATE_URB_BUF;
381 static int hackrf_free_urbs(struct hackrf_dev *dev)
385 hackrf_kill_urbs(dev);
387 for (i = dev->urbs_initialized - 1; i >= 0; i--) {
388 if (dev->urb_list[i]) {
389 dev_dbg(dev->dev, "free urb=%d\n", i);
391 usb_free_urb(dev->urb_list[i]);
394 dev->urbs_initialized = 0;
399 static int hackrf_alloc_urbs(struct hackrf_dev *dev)
403 /* allocate the URBs */
404 for (i = 0; i < MAX_BULK_BUFS; i++) {
405 dev_dbg(dev->dev, "alloc urb=%d\n", i);
406 dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
407 if (!dev->urb_list[i]) {
408 dev_dbg(dev->dev, "failed\n");
409 for (j = 0; j < i; j++)
410 usb_free_urb(dev->urb_list[j]);
413 usb_fill_bulk_urb(dev->urb_list[i],
415 usb_rcvbulkpipe(dev->udev, 0x81),
418 hackrf_urb_complete, dev);
420 dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
421 dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
422 dev->urbs_initialized++;
428 /* Must be called with vb_queue_lock hold */
429 static void hackrf_cleanup_queued_bufs(struct hackrf_dev *dev)
433 dev_dbg(dev->dev, "\n");
435 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
436 while (!list_empty(&dev->queued_bufs)) {
437 struct hackrf_frame_buf *buf;
439 buf = list_entry(dev->queued_bufs.next,
440 struct hackrf_frame_buf, list);
441 list_del(&buf->list);
442 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
444 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
447 /* The user yanked out the cable... */
448 static void hackrf_disconnect(struct usb_interface *intf)
450 struct v4l2_device *v = usb_get_intfdata(intf);
451 struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev);
453 dev_dbg(dev->dev, "\n");
455 mutex_lock(&dev->vb_queue_lock);
456 mutex_lock(&dev->v4l2_lock);
457 /* No need to keep the urbs around after disconnection */
459 v4l2_device_disconnect(&dev->v4l2_dev);
460 video_unregister_device(&dev->vdev);
461 mutex_unlock(&dev->v4l2_lock);
462 mutex_unlock(&dev->vb_queue_lock);
464 v4l2_device_put(&dev->v4l2_dev);
467 /* Videobuf2 operations */
468 static int hackrf_queue_setup(struct vb2_queue *vq,
469 const struct v4l2_format *fmt, unsigned int *nbuffers,
470 unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
472 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
474 dev_dbg(dev->dev, "nbuffers=%d\n", *nbuffers);
476 /* Need at least 8 buffers */
477 if (vq->num_buffers + *nbuffers < 8)
478 *nbuffers = 8 - vq->num_buffers;
480 sizes[0] = PAGE_ALIGN(dev->buffersize);
482 dev_dbg(dev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
486 static void hackrf_buf_queue(struct vb2_buffer *vb)
488 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
489 struct hackrf_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
490 struct hackrf_frame_buf *buf =
491 container_of(vbuf, struct hackrf_frame_buf, vb);
494 spin_lock_irqsave(&dev->queued_bufs_lock, flags);
495 list_add_tail(&buf->list, &dev->queued_bufs);
496 spin_unlock_irqrestore(&dev->queued_bufs_lock, flags);
499 static int hackrf_start_streaming(struct vb2_queue *vq, unsigned int count)
501 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
504 dev_dbg(dev->dev, "\n");
509 mutex_lock(&dev->v4l2_lock);
513 set_bit(POWER_ON, &dev->flags);
515 ret = hackrf_alloc_stream_bufs(dev);
519 ret = hackrf_alloc_urbs(dev);
523 ret = hackrf_submit_urbs(dev);
527 /* start hardware streaming */
528 ret = hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, 1, 0, NULL, 0);
532 goto exit_mutex_unlock;
534 hackrf_kill_urbs(dev);
535 hackrf_free_urbs(dev);
536 hackrf_free_stream_bufs(dev);
537 clear_bit(POWER_ON, &dev->flags);
539 /* return all queued buffers to vb2 */
541 struct hackrf_frame_buf *buf, *tmp;
543 list_for_each_entry_safe(buf, tmp, &dev->queued_bufs, list) {
544 list_del(&buf->list);
545 vb2_buffer_done(&buf->vb.vb2_buf,
546 VB2_BUF_STATE_QUEUED);
551 mutex_unlock(&dev->v4l2_lock);
556 static void hackrf_stop_streaming(struct vb2_queue *vq)
558 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
560 dev_dbg(dev->dev, "\n");
562 mutex_lock(&dev->v4l2_lock);
564 /* stop hardware streaming */
565 hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, 0, 0, NULL, 0);
567 hackrf_kill_urbs(dev);
568 hackrf_free_urbs(dev);
569 hackrf_free_stream_bufs(dev);
571 hackrf_cleanup_queued_bufs(dev);
573 clear_bit(POWER_ON, &dev->flags);
575 mutex_unlock(&dev->v4l2_lock);
578 static struct vb2_ops hackrf_vb2_ops = {
579 .queue_setup = hackrf_queue_setup,
580 .buf_queue = hackrf_buf_queue,
581 .start_streaming = hackrf_start_streaming,
582 .stop_streaming = hackrf_stop_streaming,
583 .wait_prepare = vb2_ops_wait_prepare,
584 .wait_finish = vb2_ops_wait_finish,
587 static int hackrf_querycap(struct file *file, void *fh,
588 struct v4l2_capability *cap)
590 struct hackrf_dev *dev = video_drvdata(file);
592 dev_dbg(dev->dev, "\n");
594 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
595 strlcpy(cap->card, dev->vdev.name, sizeof(cap->card));
596 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
597 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
598 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
599 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
604 static int hackrf_s_fmt_sdr_cap(struct file *file, void *priv,
605 struct v4l2_format *f)
607 struct hackrf_dev *dev = video_drvdata(file);
608 struct vb2_queue *q = &dev->vb_queue;
611 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
612 (char *)&f->fmt.sdr.pixelformat);
617 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
618 for (i = 0; i < NUM_FORMATS; i++) {
619 if (f->fmt.sdr.pixelformat == formats[i].pixelformat) {
620 dev->pixelformat = formats[i].pixelformat;
621 dev->buffersize = formats[i].buffersize;
622 f->fmt.sdr.buffersize = formats[i].buffersize;
627 dev->pixelformat = formats[0].pixelformat;
628 dev->buffersize = formats[0].buffersize;
629 f->fmt.sdr.pixelformat = formats[0].pixelformat;
630 f->fmt.sdr.buffersize = formats[0].buffersize;
635 static int hackrf_g_fmt_sdr_cap(struct file *file, void *priv,
636 struct v4l2_format *f)
638 struct hackrf_dev *dev = video_drvdata(file);
640 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
641 (char *)&dev->pixelformat);
643 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
644 f->fmt.sdr.pixelformat = dev->pixelformat;
645 f->fmt.sdr.buffersize = dev->buffersize;
650 static int hackrf_try_fmt_sdr_cap(struct file *file, void *priv,
651 struct v4l2_format *f)
653 struct hackrf_dev *dev = video_drvdata(file);
656 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
657 (char *)&f->fmt.sdr.pixelformat);
659 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
660 for (i = 0; i < NUM_FORMATS; i++) {
661 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
662 f->fmt.sdr.buffersize = formats[i].buffersize;
667 f->fmt.sdr.pixelformat = formats[0].pixelformat;
668 f->fmt.sdr.buffersize = formats[0].buffersize;
673 static int hackrf_enum_fmt_sdr_cap(struct file *file, void *priv,
674 struct v4l2_fmtdesc *f)
676 struct hackrf_dev *dev = video_drvdata(file);
678 dev_dbg(dev->dev, "index=%d\n", f->index);
680 if (f->index >= NUM_FORMATS)
683 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
684 f->pixelformat = formats[f->index].pixelformat;
689 static int hackrf_s_tuner(struct file *file, void *priv,
690 const struct v4l2_tuner *v)
692 struct hackrf_dev *dev = video_drvdata(file);
695 dev_dbg(dev->dev, "index=%d\n", v->index);
699 else if (v->index == 1)
707 static int hackrf_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
709 struct hackrf_dev *dev = video_drvdata(file);
712 dev_dbg(dev->dev, "index=%d\n", v->index);
715 strlcpy(v->name, "HackRF ADC", sizeof(v->name));
716 v->type = V4L2_TUNER_ADC;
717 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
718 v->rangelow = bands_adc[0].rangelow;
719 v->rangehigh = bands_adc[0].rangehigh;
721 } else if (v->index == 1) {
722 strlcpy(v->name, "HackRF RF", sizeof(v->name));
723 v->type = V4L2_TUNER_RF;
724 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
725 v->rangelow = bands_rf[0].rangelow;
726 v->rangehigh = bands_rf[0].rangehigh;
735 static int hackrf_s_frequency(struct file *file, void *priv,
736 const struct v4l2_frequency *f)
738 struct hackrf_dev *dev = video_drvdata(file);
740 unsigned int upper, lower;
743 dev_dbg(dev->dev, "tuner=%d type=%d frequency=%u\n",
744 f->tuner, f->type, f->frequency);
747 dev->f_adc = clamp_t(unsigned int, f->frequency,
748 bands_adc[0].rangelow, bands_adc[0].rangehigh);
749 dev_dbg(dev->dev, "ADC frequency=%u Hz\n", dev->f_adc);
752 buf[0] = (upper >> 0) & 0xff;
753 buf[1] = (upper >> 8) & 0xff;
754 buf[2] = (upper >> 16) & 0xff;
755 buf[3] = (upper >> 24) & 0xff;
756 buf[4] = (lower >> 0) & 0xff;
757 buf[5] = (lower >> 8) & 0xff;
758 buf[6] = (lower >> 16) & 0xff;
759 buf[7] = (lower >> 24) & 0xff;
760 ret = hackrf_ctrl_msg(dev, CMD_SAMPLE_RATE_SET, 0, 0, buf, 8);
761 } else if (f->tuner == 1) {
762 dev->f_rf = clamp_t(unsigned int, f->frequency,
763 bands_rf[0].rangelow, bands_rf[0].rangehigh);
764 dev_dbg(dev->dev, "RF frequency=%u Hz\n", dev->f_rf);
765 upper = dev->f_rf / 1000000;
766 lower = dev->f_rf % 1000000;
767 buf[0] = (upper >> 0) & 0xff;
768 buf[1] = (upper >> 8) & 0xff;
769 buf[2] = (upper >> 16) & 0xff;
770 buf[3] = (upper >> 24) & 0xff;
771 buf[4] = (lower >> 0) & 0xff;
772 buf[5] = (lower >> 8) & 0xff;
773 buf[6] = (lower >> 16) & 0xff;
774 buf[7] = (lower >> 24) & 0xff;
775 ret = hackrf_ctrl_msg(dev, CMD_SET_FREQ, 0, 0, buf, 8);
783 static int hackrf_g_frequency(struct file *file, void *priv,
784 struct v4l2_frequency *f)
786 struct hackrf_dev *dev = video_drvdata(file);
789 dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
792 f->type = V4L2_TUNER_ADC;
793 f->frequency = dev->f_adc;
795 } else if (f->tuner == 1) {
796 f->type = V4L2_TUNER_RF;
797 f->frequency = dev->f_rf;
806 static int hackrf_enum_freq_bands(struct file *file, void *priv,
807 struct v4l2_frequency_band *band)
809 struct hackrf_dev *dev = video_drvdata(file);
812 dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n",
813 band->tuner, band->type, band->index);
815 if (band->tuner == 0) {
816 if (band->index >= ARRAY_SIZE(bands_adc)) {
819 *band = bands_adc[band->index];
822 } else if (band->tuner == 1) {
823 if (band->index >= ARRAY_SIZE(bands_rf)) {
826 *band = bands_rf[band->index];
836 static const struct v4l2_ioctl_ops hackrf_ioctl_ops = {
837 .vidioc_querycap = hackrf_querycap,
839 .vidioc_s_fmt_sdr_cap = hackrf_s_fmt_sdr_cap,
840 .vidioc_g_fmt_sdr_cap = hackrf_g_fmt_sdr_cap,
841 .vidioc_enum_fmt_sdr_cap = hackrf_enum_fmt_sdr_cap,
842 .vidioc_try_fmt_sdr_cap = hackrf_try_fmt_sdr_cap,
844 .vidioc_reqbufs = vb2_ioctl_reqbufs,
845 .vidioc_create_bufs = vb2_ioctl_create_bufs,
846 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
847 .vidioc_querybuf = vb2_ioctl_querybuf,
848 .vidioc_qbuf = vb2_ioctl_qbuf,
849 .vidioc_dqbuf = vb2_ioctl_dqbuf,
851 .vidioc_streamon = vb2_ioctl_streamon,
852 .vidioc_streamoff = vb2_ioctl_streamoff,
854 .vidioc_s_tuner = hackrf_s_tuner,
855 .vidioc_g_tuner = hackrf_g_tuner,
857 .vidioc_s_frequency = hackrf_s_frequency,
858 .vidioc_g_frequency = hackrf_g_frequency,
859 .vidioc_enum_freq_bands = hackrf_enum_freq_bands,
861 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
862 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
863 .vidioc_log_status = v4l2_ctrl_log_status,
866 static const struct v4l2_file_operations hackrf_fops = {
867 .owner = THIS_MODULE,
868 .open = v4l2_fh_open,
869 .release = vb2_fop_release,
870 .read = vb2_fop_read,
871 .poll = vb2_fop_poll,
872 .mmap = vb2_fop_mmap,
873 .unlocked_ioctl = video_ioctl2,
876 static struct video_device hackrf_template = {
877 .name = "HackRF One",
878 .release = video_device_release_empty,
879 .fops = &hackrf_fops,
880 .ioctl_ops = &hackrf_ioctl_ops,
883 static void hackrf_video_release(struct v4l2_device *v)
885 struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev);
887 v4l2_ctrl_handler_free(&dev->hdl);
888 v4l2_device_unregister(&dev->v4l2_dev);
892 static int hackrf_set_bandwidth(struct hackrf_dev *dev)
896 unsigned int bandwidth;
898 static const struct {
900 } bandwidth_lut[] = {
901 { 1750000}, /* 1.75 MHz */
902 { 2500000}, /* 2.5 MHz */
903 { 3500000}, /* 3.5 MHz */
904 { 5000000}, /* 5 MHz */
905 { 5500000}, /* 5.5 MHz */
906 { 6000000}, /* 6 MHz */
907 { 7000000}, /* 7 MHz */
908 { 8000000}, /* 8 MHz */
909 { 9000000}, /* 9 MHz */
910 {10000000}, /* 10 MHz */
911 {12000000}, /* 12 MHz */
912 {14000000}, /* 14 MHz */
913 {15000000}, /* 15 MHz */
914 {20000000}, /* 20 MHz */
915 {24000000}, /* 24 MHz */
916 {28000000}, /* 28 MHz */
919 dev_dbg(dev->dev, "bandwidth auto=%d->%d val=%d->%d f_adc=%u\n",
920 dev->bandwidth_auto->cur.val,
921 dev->bandwidth_auto->val, dev->bandwidth->cur.val,
922 dev->bandwidth->val, dev->f_adc);
924 if (dev->bandwidth_auto->val == true)
925 bandwidth = dev->f_adc;
927 bandwidth = dev->bandwidth->val;
929 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
930 if (bandwidth <= bandwidth_lut[i].freq) {
931 bandwidth = bandwidth_lut[i].freq;
936 dev->bandwidth->val = bandwidth;
937 dev->bandwidth->cur.val = bandwidth;
939 dev_dbg(dev->dev, "bandwidth selected=%d\n", bandwidth);
942 u16tmp |= ((bandwidth >> 0) & 0xff) << 0;
943 u16tmp |= ((bandwidth >> 8) & 0xff) << 8;
945 u16tmp2 |= ((bandwidth >> 16) & 0xff) << 0;
946 u16tmp2 |= ((bandwidth >> 24) & 0xff) << 8;
948 ret = hackrf_ctrl_msg(dev, CMD_BASEBAND_FILTER_BANDWIDTH_SET,
949 u16tmp, u16tmp2, NULL, 0);
951 dev_dbg(dev->dev, "failed=%d\n", ret);
956 static int hackrf_set_lna_gain(struct hackrf_dev *dev)
961 dev_dbg(dev->dev, "lna val=%d->%d\n",
962 dev->lna_gain->cur.val, dev->lna_gain->val);
964 ret = hackrf_ctrl_msg(dev, CMD_SET_LNA_GAIN, 0, dev->lna_gain->val,
967 dev_dbg(dev->dev, "failed=%d\n", ret);
972 static int hackrf_set_if_gain(struct hackrf_dev *dev)
977 dev_dbg(dev->dev, "val=%d->%d\n",
978 dev->if_gain->cur.val, dev->if_gain->val);
980 ret = hackrf_ctrl_msg(dev, CMD_SET_VGA_GAIN, 0, dev->if_gain->val,
983 dev_dbg(dev->dev, "failed=%d\n", ret);
988 static int hackrf_s_ctrl(struct v4l2_ctrl *ctrl)
990 struct hackrf_dev *dev = container_of(ctrl->handler,
991 struct hackrf_dev, hdl);
995 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
996 case V4L2_CID_RF_TUNER_BANDWIDTH:
997 ret = hackrf_set_bandwidth(dev);
999 case V4L2_CID_RF_TUNER_LNA_GAIN:
1000 ret = hackrf_set_lna_gain(dev);
1002 case V4L2_CID_RF_TUNER_IF_GAIN:
1003 ret = hackrf_set_if_gain(dev);
1006 dev_dbg(dev->dev, "unknown ctrl: id=%d name=%s\n",
1007 ctrl->id, ctrl->name);
1014 static const struct v4l2_ctrl_ops hackrf_ctrl_ops = {
1015 .s_ctrl = hackrf_s_ctrl,
1018 static int hackrf_probe(struct usb_interface *intf,
1019 const struct usb_device_id *id)
1021 struct hackrf_dev *dev;
1023 u8 u8tmp, buf[BUF_SIZE];
1025 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1029 mutex_init(&dev->v4l2_lock);
1030 mutex_init(&dev->vb_queue_lock);
1031 spin_lock_init(&dev->queued_bufs_lock);
1032 INIT_LIST_HEAD(&dev->queued_bufs);
1033 dev->dev = &intf->dev;
1034 dev->udev = interface_to_usbdev(intf);
1035 dev->f_adc = bands_adc[0].rangelow;
1036 dev->f_rf = bands_rf[0].rangelow;
1037 dev->pixelformat = formats[0].pixelformat;
1038 dev->buffersize = formats[0].buffersize;
1041 ret = hackrf_ctrl_msg(dev, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1);
1043 ret = hackrf_ctrl_msg(dev, CMD_VERSION_STRING_READ, 0, 0,
1046 dev_err(dev->dev, "Could not detect board\n");
1050 buf[BUF_SIZE - 1] = '\0';
1052 dev_info(dev->dev, "Board ID: %02x\n", u8tmp);
1053 dev_info(dev->dev, "Firmware version: %s\n", buf);
1055 /* Init videobuf2 queue structure */
1056 dev->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1057 dev->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1058 dev->vb_queue.drv_priv = dev;
1059 dev->vb_queue.buf_struct_size = sizeof(struct hackrf_frame_buf);
1060 dev->vb_queue.ops = &hackrf_vb2_ops;
1061 dev->vb_queue.mem_ops = &vb2_vmalloc_memops;
1062 dev->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1063 ret = vb2_queue_init(&dev->vb_queue);
1065 dev_err(dev->dev, "Could not initialize vb2 queue\n");
1069 /* Init video_device structure */
1070 dev->vdev = hackrf_template;
1071 dev->vdev.queue = &dev->vb_queue;
1072 dev->vdev.queue->lock = &dev->vb_queue_lock;
1073 video_set_drvdata(&dev->vdev, dev);
1075 /* Register the v4l2_device structure */
1076 dev->v4l2_dev.release = hackrf_video_release;
1077 ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev);
1079 dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret);
1083 /* Register controls */
1084 v4l2_ctrl_handler_init(&dev->hdl, 4);
1085 dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &hackrf_ctrl_ops,
1086 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
1087 dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &hackrf_ctrl_ops,
1088 V4L2_CID_RF_TUNER_BANDWIDTH,
1089 1750000, 28000000, 50000, 1750000);
1090 v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
1091 dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &hackrf_ctrl_ops,
1092 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 40, 8, 0);
1093 dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &hackrf_ctrl_ops,
1094 V4L2_CID_RF_TUNER_IF_GAIN, 0, 62, 2, 0);
1095 if (dev->hdl.error) {
1096 ret = dev->hdl.error;
1097 dev_err(dev->dev, "Could not initialize controls\n");
1098 goto err_free_controls;
1101 v4l2_ctrl_handler_setup(&dev->hdl);
1103 dev->v4l2_dev.ctrl_handler = &dev->hdl;
1104 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1105 dev->vdev.lock = &dev->v4l2_lock;
1107 ret = video_register_device(&dev->vdev, VFL_TYPE_SDR, -1);
1109 dev_err(dev->dev, "Failed to register as video device (%d)\n",
1111 goto err_unregister_v4l2_dev;
1113 dev_info(dev->dev, "Registered as %s\n",
1114 video_device_node_name(&dev->vdev));
1115 dev_notice(dev->dev, "SDR API is still slightly experimental and functionality changes may follow\n");
1119 v4l2_ctrl_handler_free(&dev->hdl);
1120 err_unregister_v4l2_dev:
1121 v4l2_device_unregister(&dev->v4l2_dev);
1127 /* USB device ID list */
1128 static struct usb_device_id hackrf_id_table[] = {
1129 { USB_DEVICE(0x1d50, 0x6089) }, /* HackRF One */
1132 MODULE_DEVICE_TABLE(usb, hackrf_id_table);
1134 /* USB subsystem interface */
1135 static struct usb_driver hackrf_driver = {
1136 .name = KBUILD_MODNAME,
1137 .probe = hackrf_probe,
1138 .disconnect = hackrf_disconnect,
1139 .id_table = hackrf_id_table,
1142 module_usb_driver(hackrf_driver);
1144 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1145 MODULE_DESCRIPTION("HackRF");
1146 MODULE_LICENSE("GPL");