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