blob: d437852066224c4ca7936aa0610333f98f1b0720 [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
Antti Palosaarid47fa532015-10-23 20:01:31 -020027/*
28 * Used Avago MGA-81563 RF amplifier could be destroyed pretty easily with too
29 * strong signal or transmitting to bad antenna.
30 * Set RF gain control to 'grabbed' state by default for sure.
31 */
32static bool hackrf_enable_rf_gain_ctrl;
33module_param_named(enable_rf_gain_ctrl, hackrf_enable_rf_gain_ctrl, bool, 0644);
34MODULE_PARM_DESC(enable_rf_gain_ctrl, "enable RX/TX RF amplifier control (warn: could damage amplifier)");
35
Antti Palosaari969ec1f2014-08-23 04:40:01 -030036/* HackRF USB API commands (from HackRF Library) */
37enum {
38 CMD_SET_TRANSCEIVER_MODE = 0x01,
39 CMD_SAMPLE_RATE_SET = 0x06,
40 CMD_BASEBAND_FILTER_BANDWIDTH_SET = 0x07,
41 CMD_BOARD_ID_READ = 0x0e,
42 CMD_VERSION_STRING_READ = 0x0f,
43 CMD_SET_FREQ = 0x10,
Antti Palosaarib3ae2962015-10-10 13:51:04 -030044 CMD_AMP_ENABLE = 0x11,
Antti Palosaari969ec1f2014-08-23 04:40:01 -030045 CMD_SET_LNA_GAIN = 0x13,
46 CMD_SET_VGA_GAIN = 0x14,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -030047 CMD_SET_TXVGA_GAIN = 0x15,
Antti Palosaari969ec1f2014-08-23 04:40:01 -030048};
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
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -030058static const struct v4l2_frequency_band bands_adc_dac[] = {
Antti Palosaari969ec1f2014-08-23 04:40:01 -030059 {
60 .tuner = 0,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -030061 .type = V4L2_TUNER_SDR,
Antti Palosaari969ec1f2014-08-23 04:40:01 -030062 .index = 0,
63 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
64 .rangelow = 200000,
65 .rangehigh = 24000000,
66 },
67};
68
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -030069static const struct v4l2_frequency_band bands_rx_tx[] = {
Antti Palosaari969ec1f2014-08-23 04:40:01 -030070 {
71 .tuner = 1,
72 .type = V4L2_TUNER_RF,
73 .index = 0,
74 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
75 .rangelow = 1,
Mauro Carvalho Chehab720b0552014-09-21 20:35:05 -030076 .rangehigh = 4294967294LL, /* max u32, hw goes over 7GHz */
Antti Palosaari969ec1f2014-08-23 04:40:01 -030077 },
78};
79
80/* stream formats */
81struct hackrf_format {
Antti Palosaari969ec1f2014-08-23 04:40:01 -030082 u32 pixelformat;
83 u32 buffersize;
84};
85
86/* format descriptions for capture and preview */
87static struct hackrf_format formats[] = {
88 {
Antti Palosaari969ec1f2014-08-23 04:40:01 -030089 .pixelformat = V4L2_SDR_FMT_CS8,
90 .buffersize = BULK_BUFFER_SIZE,
91 },
92};
93
94static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
95
96/* intermediate buffers with raw data from the USB device */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -030097struct hackrf_buffer {
Junghak Sung2d700712015-09-22 10:30:30 -030098 struct vb2_v4l2_buffer vb;
Antti Palosaari969ec1f2014-08-23 04:40:01 -030099 struct list_head list;
100};
101
102struct hackrf_dev {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300103#define USB_STATE_URB_BUF 1 /* XXX: set manually */
104#define RX_ON 4
105#define TX_ON 5
106#define RX_ADC_FREQUENCY 11
107#define TX_DAC_FREQUENCY 12
108#define RX_BANDWIDTH 13
109#define TX_BANDWIDTH 14
110#define RX_RF_FREQUENCY 15
111#define TX_RF_FREQUENCY 16
112#define RX_RF_GAIN 17
113#define TX_RF_GAIN 18
114#define RX_IF_GAIN 19
115#define RX_LNA_GAIN 20
116#define TX_LNA_GAIN 21
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300117 unsigned long flags;
118
Antti Palosaarieec20f02015-10-10 13:51:05 -0300119 struct usb_interface *intf;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300120 struct device *dev;
121 struct usb_device *udev;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300122 struct video_device rx_vdev;
123 struct video_device tx_vdev;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300124 struct v4l2_device v4l2_dev;
125
126 /* videobuf2 queue and queued buffers list */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300127 struct vb2_queue rx_vb2_queue;
128 struct vb2_queue tx_vb2_queue;
129 struct list_head rx_buffer_list;
130 struct list_head tx_buffer_list;
131 spinlock_t buffer_list_lock; /* Protects buffer_list */
Jannik Bechera769de02016-08-21 15:44:20 -0300132 unsigned int sequence; /* Buffer sequence counter */
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300133 unsigned int vb_full; /* vb is full and packets dropped */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300134 unsigned int vb_empty; /* vb is empty and packets dropped */
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300135
136 /* Note if taking both locks v4l2_lock must always be locked first! */
137 struct mutex v4l2_lock; /* Protects everything else */
138 struct mutex vb_queue_lock; /* Protects vb_queue */
139
140 struct urb *urb_list[MAX_BULK_BUFS];
141 int buf_num;
142 unsigned long buf_size;
143 u8 *buf_list[MAX_BULK_BUFS];
144 dma_addr_t dma_addr[MAX_BULK_BUFS];
145 int urbs_initialized;
146 int urbs_submitted;
147
148 /* USB control message buffer */
149 #define BUF_SIZE 24
150 u8 buf[BUF_SIZE];
151
152 /* Current configuration */
153 unsigned int f_adc;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300154 unsigned int f_dac;
155 unsigned int f_rx;
156 unsigned int f_tx;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300157 u32 pixelformat;
158 u32 buffersize;
159
160 /* Controls */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300161 struct v4l2_ctrl_handler rx_ctrl_handler;
162 struct v4l2_ctrl *rx_bandwidth_auto;
163 struct v4l2_ctrl *rx_bandwidth;
164 struct v4l2_ctrl *rx_rf_gain;
165 struct v4l2_ctrl *rx_lna_gain;
166 struct v4l2_ctrl *rx_if_gain;
167 struct v4l2_ctrl_handler tx_ctrl_handler;
168 struct v4l2_ctrl *tx_bandwidth_auto;
169 struct v4l2_ctrl *tx_bandwidth;
170 struct v4l2_ctrl *tx_rf_gain;
171 struct v4l2_ctrl *tx_lna_gain;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300172
173 /* Sample rate calc */
174 unsigned long jiffies_next;
175 unsigned int sample;
176 unsigned int sample_measured;
177};
178
179#define hackrf_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
180 char *_direction; \
181 if (_t & USB_DIR_IN) \
182 _direction = "<<<"; \
183 else \
184 _direction = ">>>"; \
185 dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
186 _t, _r, _v & 0xff, _v >> 8, _i & 0xff, \
187 _i >> 8, _l & 0xff, _l >> 8, _direction, _l, _b); \
188}
189
190/* execute firmware command */
191static int hackrf_ctrl_msg(struct hackrf_dev *dev, u8 request, u16 value,
192 u16 index, u8 *data, u16 size)
193{
194 int ret;
195 unsigned int pipe;
196 u8 requesttype;
197
198 switch (request) {
199 case CMD_SET_TRANSCEIVER_MODE:
200 case CMD_SET_FREQ:
Antti Palosaarib3ae2962015-10-10 13:51:04 -0300201 case CMD_AMP_ENABLE:
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300202 case CMD_SAMPLE_RATE_SET:
203 case CMD_BASEBAND_FILTER_BANDWIDTH_SET:
204 pipe = usb_sndctrlpipe(dev->udev, 0);
205 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
206 break;
207 case CMD_BOARD_ID_READ:
208 case CMD_VERSION_STRING_READ:
209 case CMD_SET_LNA_GAIN:
210 case CMD_SET_VGA_GAIN:
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300211 case CMD_SET_TXVGA_GAIN:
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300212 pipe = usb_rcvctrlpipe(dev->udev, 0);
213 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
214 break;
215 default:
216 dev_err(dev->dev, "Unknown command %02x\n", request);
217 ret = -EINVAL;
218 goto err;
219 }
220
221 /* write request */
222 if (!(requesttype & USB_DIR_IN))
223 memcpy(dev->buf, data, size);
224
225 ret = usb_control_msg(dev->udev, pipe, request, requesttype, value,
226 index, dev->buf, size, 1000);
227 hackrf_dbg_usb_control_msg(dev->dev, request, requesttype, value,
228 index, dev->buf, size);
229 if (ret < 0) {
230 dev_err(dev->dev, "usb_control_msg() failed %d request %02x\n",
231 ret, request);
232 goto err;
233 }
234
235 /* read request */
236 if (requesttype & USB_DIR_IN)
237 memcpy(data, dev->buf, size);
238
239 return 0;
240err:
241 return ret;
242}
243
Antti Palosaarieec20f02015-10-10 13:51:05 -0300244static int hackrf_set_params(struct hackrf_dev *dev)
245{
246 struct usb_interface *intf = dev->intf;
247 int ret, i;
248 u8 buf[8], u8tmp;
249 unsigned int uitmp, uitmp1, uitmp2;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300250 const bool rx = test_bit(RX_ON, &dev->flags);
251 const bool tx = test_bit(TX_ON, &dev->flags);
252 static const struct {
253 u32 freq;
254 } bandwidth_lut[] = {
255 { 1750000}, /* 1.75 MHz */
256 { 2500000}, /* 2.5 MHz */
257 { 3500000}, /* 3.5 MHz */
258 { 5000000}, /* 5 MHz */
259 { 5500000}, /* 5.5 MHz */
260 { 6000000}, /* 6 MHz */
261 { 7000000}, /* 7 MHz */
262 { 8000000}, /* 8 MHz */
263 { 9000000}, /* 9 MHz */
264 {10000000}, /* 10 MHz */
265 {12000000}, /* 12 MHz */
266 {14000000}, /* 14 MHz */
267 {15000000}, /* 15 MHz */
268 {20000000}, /* 20 MHz */
269 {24000000}, /* 24 MHz */
270 {28000000}, /* 28 MHz */
271 };
Antti Palosaarieec20f02015-10-10 13:51:05 -0300272
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300273 if (!rx && !tx) {
Antti Palosaarieec20f02015-10-10 13:51:05 -0300274 dev_dbg(&intf->dev, "device is sleeping\n");
275 return 0;
276 }
277
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300278 /* ADC / DAC frequency */
279 if (rx && test_and_clear_bit(RX_ADC_FREQUENCY, &dev->flags)) {
280 dev_dbg(&intf->dev, "RX ADC frequency=%u Hz\n", dev->f_adc);
Antti Palosaarieec20f02015-10-10 13:51:05 -0300281 uitmp1 = dev->f_adc;
282 uitmp2 = 1;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300283 set_bit(TX_DAC_FREQUENCY, &dev->flags);
284 } else if (tx && test_and_clear_bit(TX_DAC_FREQUENCY, &dev->flags)) {
285 dev_dbg(&intf->dev, "TX DAC frequency=%u Hz\n", dev->f_dac);
286 uitmp1 = dev->f_dac;
287 uitmp2 = 1;
288 set_bit(RX_ADC_FREQUENCY, &dev->flags);
289 } else {
290 uitmp1 = uitmp2 = 0;
291 }
292 if (uitmp1 || uitmp2) {
Antti Palosaarieec20f02015-10-10 13:51:05 -0300293 buf[0] = (uitmp1 >> 0) & 0xff;
294 buf[1] = (uitmp1 >> 8) & 0xff;
295 buf[2] = (uitmp1 >> 16) & 0xff;
296 buf[3] = (uitmp1 >> 24) & 0xff;
297 buf[4] = (uitmp2 >> 0) & 0xff;
298 buf[5] = (uitmp2 >> 8) & 0xff;
299 buf[6] = (uitmp2 >> 16) & 0xff;
300 buf[7] = (uitmp2 >> 24) & 0xff;
301 ret = hackrf_ctrl_msg(dev, CMD_SAMPLE_RATE_SET, 0, 0, buf, 8);
302 if (ret)
303 goto err;
304 }
305
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300306 /* bandwidth */
307 if (rx && test_and_clear_bit(RX_BANDWIDTH, &dev->flags)) {
308 if (dev->rx_bandwidth_auto->val == true)
Antti Palosaarieec20f02015-10-10 13:51:05 -0300309 uitmp = dev->f_adc;
310 else
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300311 uitmp = dev->rx_bandwidth->val;
Antti Palosaarieec20f02015-10-10 13:51:05 -0300312
313 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
314 if (uitmp <= bandwidth_lut[i].freq) {
315 uitmp = bandwidth_lut[i].freq;
316 break;
317 }
318 }
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300319 dev->rx_bandwidth->val = uitmp;
320 dev->rx_bandwidth->cur.val = uitmp;
321 dev_dbg(&intf->dev, "RX bandwidth selected=%u\n", uitmp);
322 set_bit(TX_BANDWIDTH, &dev->flags);
323 } else if (tx && test_and_clear_bit(TX_BANDWIDTH, &dev->flags)) {
324 if (dev->tx_bandwidth_auto->val == true)
325 uitmp = dev->f_dac;
326 else
327 uitmp = dev->tx_bandwidth->val;
Antti Palosaarieec20f02015-10-10 13:51:05 -0300328
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300329 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
330 if (uitmp <= bandwidth_lut[i].freq) {
331 uitmp = bandwidth_lut[i].freq;
332 break;
333 }
334 }
335 dev->tx_bandwidth->val = uitmp;
336 dev->tx_bandwidth->cur.val = uitmp;
337 dev_dbg(&intf->dev, "TX bandwidth selected=%u\n", uitmp);
338 set_bit(RX_BANDWIDTH, &dev->flags);
339 } else {
340 uitmp = 0;
341 }
342 if (uitmp) {
343 uitmp1 = uitmp2 = 0;
344 uitmp1 |= ((uitmp >> 0) & 0xff) << 0;
345 uitmp1 |= ((uitmp >> 8) & 0xff) << 8;
Antti Palosaarieec20f02015-10-10 13:51:05 -0300346 uitmp2 |= ((uitmp >> 16) & 0xff) << 0;
347 uitmp2 |= ((uitmp >> 24) & 0xff) << 8;
Antti Palosaarieec20f02015-10-10 13:51:05 -0300348 ret = hackrf_ctrl_msg(dev, CMD_BASEBAND_FILTER_BANDWIDTH_SET,
349 uitmp1, uitmp2, NULL, 0);
350 if (ret)
351 goto err;
352 }
353
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300354 /* RX / TX RF frequency */
355 if (rx && test_and_clear_bit(RX_RF_FREQUENCY, &dev->flags)) {
356 dev_dbg(&intf->dev, "RX RF frequency=%u Hz\n", dev->f_rx);
357 uitmp1 = dev->f_rx / 1000000;
358 uitmp2 = dev->f_rx % 1000000;
359 set_bit(TX_RF_FREQUENCY, &dev->flags);
360 } else if (tx && test_and_clear_bit(TX_RF_FREQUENCY, &dev->flags)) {
361 dev_dbg(&intf->dev, "TX RF frequency=%u Hz\n", dev->f_tx);
362 uitmp1 = dev->f_tx / 1000000;
363 uitmp2 = dev->f_tx % 1000000;
364 set_bit(RX_RF_FREQUENCY, &dev->flags);
365 } else {
366 uitmp1 = uitmp2 = 0;
367 }
368 if (uitmp1 || uitmp2) {
Antti Palosaarieec20f02015-10-10 13:51:05 -0300369 buf[0] = (uitmp1 >> 0) & 0xff;
370 buf[1] = (uitmp1 >> 8) & 0xff;
371 buf[2] = (uitmp1 >> 16) & 0xff;
372 buf[3] = (uitmp1 >> 24) & 0xff;
373 buf[4] = (uitmp2 >> 0) & 0xff;
374 buf[5] = (uitmp2 >> 8) & 0xff;
375 buf[6] = (uitmp2 >> 16) & 0xff;
376 buf[7] = (uitmp2 >> 24) & 0xff;
377 ret = hackrf_ctrl_msg(dev, CMD_SET_FREQ, 0, 0, buf, 8);
378 if (ret)
379 goto err;
380 }
381
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300382 /* RX RF gain */
383 if (rx && test_and_clear_bit(RX_RF_GAIN, &dev->flags)) {
384 dev_dbg(&intf->dev, "RX RF gain val=%d->%d\n",
385 dev->rx_rf_gain->cur.val, dev->rx_rf_gain->val);
Antti Palosaarieec20f02015-10-10 13:51:05 -0300386
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300387 u8tmp = (dev->rx_rf_gain->val) ? 1 : 0;
Antti Palosaarieec20f02015-10-10 13:51:05 -0300388 ret = hackrf_ctrl_msg(dev, CMD_AMP_ENABLE, u8tmp, 0, NULL, 0);
389 if (ret)
390 goto err;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300391 set_bit(TX_RF_GAIN, &dev->flags);
Antti Palosaarieec20f02015-10-10 13:51:05 -0300392 }
393
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300394 /* TX RF gain */
395 if (tx && test_and_clear_bit(TX_RF_GAIN, &dev->flags)) {
396 dev_dbg(&intf->dev, "TX RF gain val=%d->%d\n",
397 dev->tx_rf_gain->cur.val, dev->tx_rf_gain->val);
398
399 u8tmp = (dev->tx_rf_gain->val) ? 1 : 0;
400 ret = hackrf_ctrl_msg(dev, CMD_AMP_ENABLE, u8tmp, 0, NULL, 0);
401 if (ret)
402 goto err;
403 set_bit(RX_RF_GAIN, &dev->flags);
404 }
405
406 /* RX LNA gain */
407 if (rx && test_and_clear_bit(RX_LNA_GAIN, &dev->flags)) {
408 dev_dbg(dev->dev, "RX LNA gain val=%d->%d\n",
409 dev->rx_lna_gain->cur.val, dev->rx_lna_gain->val);
Antti Palosaarieec20f02015-10-10 13:51:05 -0300410
411 ret = hackrf_ctrl_msg(dev, CMD_SET_LNA_GAIN, 0,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300412 dev->rx_lna_gain->val, &u8tmp, 1);
Antti Palosaarieec20f02015-10-10 13:51:05 -0300413 if (ret)
414 goto err;
415 }
416
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300417 /* RX IF gain */
418 if (rx && test_and_clear_bit(RX_IF_GAIN, &dev->flags)) {
Antti Palosaarieec20f02015-10-10 13:51:05 -0300419 dev_dbg(&intf->dev, "IF gain val=%d->%d\n",
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300420 dev->rx_if_gain->cur.val, dev->rx_if_gain->val);
Antti Palosaarieec20f02015-10-10 13:51:05 -0300421
422 ret = hackrf_ctrl_msg(dev, CMD_SET_VGA_GAIN, 0,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300423 dev->rx_if_gain->val, &u8tmp, 1);
424 if (ret)
425 goto err;
426 }
427
428 /* TX LNA gain */
429 if (tx && test_and_clear_bit(TX_LNA_GAIN, &dev->flags)) {
430 dev_dbg(&intf->dev, "TX LNA gain val=%d->%d\n",
431 dev->tx_lna_gain->cur.val, dev->tx_lna_gain->val);
432
433 ret = hackrf_ctrl_msg(dev, CMD_SET_TXVGA_GAIN, 0,
434 dev->tx_lna_gain->val, &u8tmp, 1);
Antti Palosaarieec20f02015-10-10 13:51:05 -0300435 if (ret)
436 goto err;
437 }
438
439 return 0;
440err:
441 dev_dbg(&intf->dev, "failed=%d\n", ret);
442 return ret;
443}
444
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300445/* Private functions */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300446static struct hackrf_buffer *hackrf_get_next_buffer(struct hackrf_dev *dev,
447 struct list_head *buffer_list)
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300448{
449 unsigned long flags;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300450 struct hackrf_buffer *buffer = NULL;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300451
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300452 spin_lock_irqsave(&dev->buffer_list_lock, flags);
453 if (list_empty(buffer_list))
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300454 goto leave;
455
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300456 buffer = list_entry(buffer_list->next, struct hackrf_buffer, list);
457 list_del(&buffer->list);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300458leave:
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300459 spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
460 return buffer;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300461}
462
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300463static void hackrf_copy_stream(struct hackrf_dev *dev, void *dst, void *src,
464 unsigned int src_len)
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300465{
466 memcpy(dst, src, src_len);
467
468 /* calculate sample rate and output it in 10 seconds intervals */
469 if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
470 #define MSECS 10000UL
471 unsigned int msecs = jiffies_to_msecs(jiffies -
472 dev->jiffies_next + msecs_to_jiffies(MSECS));
473 unsigned int samples = dev->sample - dev->sample_measured;
474
475 dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
476 dev->sample_measured = dev->sample;
477 dev_dbg(dev->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n",
478 src_len, samples, msecs,
479 samples * 1000UL / msecs);
480 }
481
482 /* total number of samples */
483 dev->sample += src_len / 2;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300484}
485
486/*
487 * This gets called for the bulk stream pipe. This is done in interrupt
488 * time, so it has to be fast, not crash, and not stall. Neat.
489 */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300490static void hackrf_urb_complete_in(struct urb *urb)
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300491{
492 struct hackrf_dev *dev = urb->context;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300493 struct usb_interface *intf = dev->intf;
494 struct hackrf_buffer *buffer;
495 unsigned int len;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300496
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300497 dev_dbg_ratelimited(&intf->dev, "status=%d length=%u/%u\n", urb->status,
498 urb->actual_length, urb->transfer_buffer_length);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300499
500 switch (urb->status) {
501 case 0: /* success */
502 case -ETIMEDOUT: /* NAK */
503 break;
504 case -ECONNRESET: /* kill */
505 case -ENOENT:
506 case -ESHUTDOWN:
507 return;
508 default: /* error */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300509 dev_err_ratelimited(&intf->dev, "URB failed %d\n", urb->status);
510 goto exit_usb_submit_urb;
511 }
512
513 /* get buffer to write */
514 buffer = hackrf_get_next_buffer(dev, &dev->rx_buffer_list);
515 if (unlikely(buffer == NULL)) {
516 dev->vb_full++;
517 dev_notice_ratelimited(&intf->dev,
518 "buffer is full - %u packets dropped\n",
519 dev->vb_full);
520 goto exit_usb_submit_urb;
521 }
522
523 len = min_t(unsigned long, vb2_plane_size(&buffer->vb.vb2_buf, 0),
524 urb->actual_length);
525 hackrf_copy_stream(dev, vb2_plane_vaddr(&buffer->vb.vb2_buf, 0),
526 urb->transfer_buffer, len);
527 vb2_set_plane_payload(&buffer->vb.vb2_buf, 0, len);
528 buffer->vb.sequence = dev->sequence++;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200529 buffer->vb.vb2_buf.timestamp = ktime_get_ns();
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300530 vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_DONE);
531exit_usb_submit_urb:
532 usb_submit_urb(urb, GFP_ATOMIC);
533}
534
535static void hackrf_urb_complete_out(struct urb *urb)
536{
537 struct hackrf_dev *dev = urb->context;
538 struct usb_interface *intf = dev->intf;
539 struct hackrf_buffer *buffer;
540 unsigned int len;
541
542 dev_dbg_ratelimited(&intf->dev, "status=%d length=%u/%u\n", urb->status,
543 urb->actual_length, urb->transfer_buffer_length);
544
545 switch (urb->status) {
546 case 0: /* success */
547 case -ETIMEDOUT: /* NAK */
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300548 break;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300549 case -ECONNRESET: /* kill */
550 case -ENOENT:
551 case -ESHUTDOWN:
552 return;
553 default: /* error */
554 dev_err_ratelimited(&intf->dev, "URB failed %d\n", urb->status);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300555 }
556
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300557 /* get buffer to read */
558 buffer = hackrf_get_next_buffer(dev, &dev->tx_buffer_list);
559 if (unlikely(buffer == NULL)) {
560 dev->vb_empty++;
561 dev_notice_ratelimited(&intf->dev,
562 "buffer is empty - %u packets dropped\n",
563 dev->vb_empty);
564 urb->actual_length = 0;
565 goto exit_usb_submit_urb;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300566 }
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300567
568 len = min_t(unsigned long, urb->transfer_buffer_length,
569 vb2_get_plane_payload(&buffer->vb.vb2_buf, 0));
570 hackrf_copy_stream(dev, urb->transfer_buffer,
571 vb2_plane_vaddr(&buffer->vb.vb2_buf, 0), len);
572 urb->actual_length = len;
573 buffer->vb.sequence = dev->sequence++;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200574 buffer->vb.vb2_buf.timestamp = ktime_get_ns();
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300575 vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_DONE);
576exit_usb_submit_urb:
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300577 usb_submit_urb(urb, GFP_ATOMIC);
578}
579
580static int hackrf_kill_urbs(struct hackrf_dev *dev)
581{
582 int i;
583
584 for (i = dev->urbs_submitted - 1; i >= 0; i--) {
585 dev_dbg(dev->dev, "kill urb=%d\n", i);
586 /* stop the URB */
587 usb_kill_urb(dev->urb_list[i]);
588 }
589 dev->urbs_submitted = 0;
590
591 return 0;
592}
593
594static int hackrf_submit_urbs(struct hackrf_dev *dev)
595{
596 int i, ret;
597
598 for (i = 0; i < dev->urbs_initialized; i++) {
599 dev_dbg(dev->dev, "submit urb=%d\n", i);
Jia-Ju Bai2c3449f2018-07-30 05:33:20 -0400600 ret = usb_submit_urb(dev->urb_list[i], GFP_KERNEL);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300601 if (ret) {
602 dev_err(dev->dev, "Could not submit URB no. %d - get them all back\n",
603 i);
604 hackrf_kill_urbs(dev);
605 return ret;
606 }
607 dev->urbs_submitted++;
608 }
609
610 return 0;
611}
612
613static int hackrf_free_stream_bufs(struct hackrf_dev *dev)
614{
615 if (dev->flags & USB_STATE_URB_BUF) {
616 while (dev->buf_num) {
617 dev->buf_num--;
618 dev_dbg(dev->dev, "free buf=%d\n", dev->buf_num);
619 usb_free_coherent(dev->udev, dev->buf_size,
620 dev->buf_list[dev->buf_num],
621 dev->dma_addr[dev->buf_num]);
622 }
623 }
624 dev->flags &= ~USB_STATE_URB_BUF;
625
626 return 0;
627}
628
629static int hackrf_alloc_stream_bufs(struct hackrf_dev *dev)
630{
631 dev->buf_num = 0;
632 dev->buf_size = BULK_BUFFER_SIZE;
633
634 dev_dbg(dev->dev, "all in all I will use %u bytes for streaming\n",
635 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
636
637 for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
638 dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
Jia-Ju Bai2c3449f2018-07-30 05:33:20 -0400639 BULK_BUFFER_SIZE, GFP_KERNEL,
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300640 &dev->dma_addr[dev->buf_num]);
641 if (!dev->buf_list[dev->buf_num]) {
642 dev_dbg(dev->dev, "alloc buf=%d failed\n",
643 dev->buf_num);
644 hackrf_free_stream_bufs(dev);
645 return -ENOMEM;
646 }
647
648 dev_dbg(dev->dev, "alloc buf=%d %p (dma %llu)\n", dev->buf_num,
649 dev->buf_list[dev->buf_num],
650 (long long)dev->dma_addr[dev->buf_num]);
651 dev->flags |= USB_STATE_URB_BUF;
652 }
653
654 return 0;
655}
656
657static int hackrf_free_urbs(struct hackrf_dev *dev)
658{
659 int i;
660
661 hackrf_kill_urbs(dev);
662
663 for (i = dev->urbs_initialized - 1; i >= 0; i--) {
664 if (dev->urb_list[i]) {
665 dev_dbg(dev->dev, "free urb=%d\n", i);
666 /* free the URBs */
667 usb_free_urb(dev->urb_list[i]);
668 }
669 }
670 dev->urbs_initialized = 0;
671
672 return 0;
673}
674
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300675static int hackrf_alloc_urbs(struct hackrf_dev *dev, bool rcv)
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300676{
677 int i, j;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300678 unsigned int pipe;
679 usb_complete_t complete;
680
681 if (rcv) {
682 pipe = usb_rcvbulkpipe(dev->udev, 0x81);
683 complete = &hackrf_urb_complete_in;
684 } else {
685 pipe = usb_sndbulkpipe(dev->udev, 0x02);
686 complete = &hackrf_urb_complete_out;
687 }
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300688
689 /* allocate the URBs */
690 for (i = 0; i < MAX_BULK_BUFS; i++) {
691 dev_dbg(dev->dev, "alloc urb=%d\n", i);
Jia-Ju Bai2c3449f2018-07-30 05:33:20 -0400692 dev->urb_list[i] = usb_alloc_urb(0, GFP_KERNEL);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300693 if (!dev->urb_list[i]) {
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300694 for (j = 0; j < i; j++)
695 usb_free_urb(dev->urb_list[j]);
696 return -ENOMEM;
697 }
698 usb_fill_bulk_urb(dev->urb_list[i],
699 dev->udev,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300700 pipe,
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300701 dev->buf_list[i],
702 BULK_BUFFER_SIZE,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300703 complete, dev);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300704
705 dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
706 dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
707 dev->urbs_initialized++;
708 }
709
710 return 0;
711}
712
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300713/* The user yanked out the cable... */
714static void hackrf_disconnect(struct usb_interface *intf)
715{
716 struct v4l2_device *v = usb_get_intfdata(intf);
717 struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev);
718
719 dev_dbg(dev->dev, "\n");
720
721 mutex_lock(&dev->vb_queue_lock);
722 mutex_lock(&dev->v4l2_lock);
723 /* No need to keep the urbs around after disconnection */
724 dev->udev = NULL;
725 v4l2_device_disconnect(&dev->v4l2_dev);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300726 video_unregister_device(&dev->tx_vdev);
727 video_unregister_device(&dev->rx_vdev);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300728 mutex_unlock(&dev->v4l2_lock);
729 mutex_unlock(&dev->vb_queue_lock);
730
731 v4l2_device_put(&dev->v4l2_dev);
732}
733
734/* Videobuf2 operations */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300735static void hackrf_return_all_buffers(struct vb2_queue *vq,
736 enum vb2_buffer_state state)
737{
738 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
739 struct usb_interface *intf = dev->intf;
740 struct hackrf_buffer *buffer, *node;
741 struct list_head *buffer_list;
742 unsigned long flags;
743
744 dev_dbg(&intf->dev, "\n");
745
746 if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
747 buffer_list = &dev->rx_buffer_list;
748 else
749 buffer_list = &dev->tx_buffer_list;
750
751 spin_lock_irqsave(&dev->buffer_list_lock, flags);
752 list_for_each_entry_safe(buffer, node, buffer_list, list) {
753 dev_dbg(&intf->dev, "list_for_each_entry_safe\n");
754 vb2_buffer_done(&buffer->vb.vb2_buf, state);
755 list_del(&buffer->list);
756 }
757 spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
758}
759
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300760static int hackrf_queue_setup(struct vb2_queue *vq,
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200761 unsigned int *nbuffers,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300762 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300763{
764 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
765
766 dev_dbg(dev->dev, "nbuffers=%d\n", *nbuffers);
767
768 /* Need at least 8 buffers */
769 if (vq->num_buffers + *nbuffers < 8)
770 *nbuffers = 8 - vq->num_buffers;
771 *nplanes = 1;
772 sizes[0] = PAGE_ALIGN(dev->buffersize);
773
774 dev_dbg(dev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
775 return 0;
776}
777
778static void hackrf_buf_queue(struct vb2_buffer *vb)
779{
Junghak Sung2d700712015-09-22 10:30:30 -0300780 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300781 struct vb2_queue *vq = vb->vb2_queue;
782 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
783 struct hackrf_buffer *buffer = container_of(vbuf, struct hackrf_buffer, vb);
784 struct list_head *buffer_list;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300785 unsigned long flags;
786
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300787 dev_dbg_ratelimited(&dev->intf->dev, "\n");
788
789 if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
790 buffer_list = &dev->rx_buffer_list;
791 else
792 buffer_list = &dev->tx_buffer_list;
793
794 spin_lock_irqsave(&dev->buffer_list_lock, flags);
795 list_add_tail(&buffer->list, buffer_list);
796 spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300797}
798
799static int hackrf_start_streaming(struct vb2_queue *vq, unsigned int count)
800{
801 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300802 struct usb_interface *intf = dev->intf;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300803 int ret;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300804 unsigned int mode;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300805
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300806 dev_dbg(&intf->dev, "count=%i\n", count);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300807
808 mutex_lock(&dev->v4l2_lock);
809
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300810 /* Allow only RX or TX, not both same time */
811 if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE) {
812 if (test_bit(TX_ON, &dev->flags)) {
813 ret = -EBUSY;
814 goto err_hackrf_return_all_buffers;
815 }
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300816
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300817 mode = 1;
818 set_bit(RX_ON, &dev->flags);
819 } else {
820 if (test_bit(RX_ON, &dev->flags)) {
821 ret = -EBUSY;
822 goto err_hackrf_return_all_buffers;
823 }
824
825 mode = 2;
826 set_bit(TX_ON, &dev->flags);
827 }
828
829 dev->sequence = 0;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300830
831 ret = hackrf_alloc_stream_bufs(dev);
832 if (ret)
833 goto err;
834
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300835 ret = hackrf_alloc_urbs(dev, (mode == 1));
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300836 if (ret)
837 goto err;
838
839 ret = hackrf_submit_urbs(dev);
840 if (ret)
841 goto err;
842
Antti Palosaarieec20f02015-10-10 13:51:05 -0300843 ret = hackrf_set_params(dev);
844 if (ret)
845 goto err;
846
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300847 /* start hardware streaming */
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300848 ret = hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, mode, 0, NULL, 0);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300849 if (ret)
850 goto err;
851
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300852 mutex_unlock(&dev->v4l2_lock);
853
854 return 0;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300855err:
856 hackrf_kill_urbs(dev);
857 hackrf_free_urbs(dev);
858 hackrf_free_stream_bufs(dev);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300859 clear_bit(RX_ON, &dev->flags);
860 clear_bit(TX_ON, &dev->flags);
861err_hackrf_return_all_buffers:
862 hackrf_return_all_buffers(vq, VB2_BUF_STATE_QUEUED);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300863 mutex_unlock(&dev->v4l2_lock);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300864 dev_dbg(&intf->dev, "failed=%d\n", ret);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300865 return ret;
866}
867
868static void hackrf_stop_streaming(struct vb2_queue *vq)
869{
870 struct hackrf_dev *dev = vb2_get_drv_priv(vq);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300871 struct usb_interface *intf = dev->intf;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300872
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300873 dev_dbg(&intf->dev, "\n");
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300874
875 mutex_lock(&dev->v4l2_lock);
876
877 /* stop hardware streaming */
878 hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, 0, 0, NULL, 0);
879
880 hackrf_kill_urbs(dev);
881 hackrf_free_urbs(dev);
882 hackrf_free_stream_bufs(dev);
883
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300884 hackrf_return_all_buffers(vq, VB2_BUF_STATE_ERROR);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300885
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300886 if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
887 clear_bit(RX_ON, &dev->flags);
888 else
889 clear_bit(TX_ON, &dev->flags);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300890
891 mutex_unlock(&dev->v4l2_lock);
892}
893
Julia Lawall1bc17712016-09-08 20:59:01 -0300894static const struct vb2_ops hackrf_vb2_ops = {
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300895 .queue_setup = hackrf_queue_setup,
896 .buf_queue = hackrf_buf_queue,
897 .start_streaming = hackrf_start_streaming,
898 .stop_streaming = hackrf_stop_streaming,
899 .wait_prepare = vb2_ops_wait_prepare,
900 .wait_finish = vb2_ops_wait_finish,
901};
902
903static int hackrf_querycap(struct file *file, void *fh,
904 struct v4l2_capability *cap)
905{
906 struct hackrf_dev *dev = video_drvdata(file);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300907 struct usb_interface *intf = dev->intf;
908 struct video_device *vdev = video_devdata(file);
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300909
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300910 dev_dbg(&intf->dev, "\n");
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300911
Gustavo Padovanf1d00602018-05-04 16:05:59 -0400912 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300913 if (vdev->vfl_dir == VFL_DIR_RX)
Gustavo Padovanf1d00602018-05-04 16:05:59 -0400914 cap->device_caps |= V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300915 else
Gustavo Padovanf1d00602018-05-04 16:05:59 -0400916 cap->device_caps |= V4L2_CAP_SDR_OUTPUT | V4L2_CAP_MODULATOR;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300917
918 cap->capabilities = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
919 V4L2_CAP_SDR_OUTPUT | V4L2_CAP_MODULATOR |
Gustavo Padovanf1d00602018-05-04 16:05:59 -0400920 V4L2_CAP_DEVICE_CAPS | cap->device_caps;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400921 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
922 strscpy(cap->card, dev->rx_vdev.name, sizeof(cap->card));
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300923 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300924
925 return 0;
926}
927
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300928static int hackrf_s_fmt_sdr(struct file *file, void *priv,
929 struct v4l2_format *f)
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300930{
931 struct hackrf_dev *dev = video_drvdata(file);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300932 struct video_device *vdev = video_devdata(file);
933 struct vb2_queue *q;
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300934 int i;
935
936 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
937 (char *)&f->fmt.sdr.pixelformat);
938
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300939 if (vdev->vfl_dir == VFL_DIR_RX)
940 q = &dev->rx_vb2_queue;
941 else
942 q = &dev->tx_vb2_queue;
943
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300944 if (vb2_is_busy(q))
945 return -EBUSY;
946
947 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
948 for (i = 0; i < NUM_FORMATS; i++) {
949 if (f->fmt.sdr.pixelformat == formats[i].pixelformat) {
950 dev->pixelformat = formats[i].pixelformat;
951 dev->buffersize = formats[i].buffersize;
952 f->fmt.sdr.buffersize = formats[i].buffersize;
953 return 0;
954 }
955 }
956
957 dev->pixelformat = formats[0].pixelformat;
958 dev->buffersize = formats[0].buffersize;
959 f->fmt.sdr.pixelformat = formats[0].pixelformat;
960 f->fmt.sdr.buffersize = formats[0].buffersize;
961
962 return 0;
963}
964
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300965static int hackrf_g_fmt_sdr(struct file *file, void *priv,
966 struct v4l2_format *f)
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300967{
968 struct hackrf_dev *dev = video_drvdata(file);
969
970 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
971 (char *)&dev->pixelformat);
972
973 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
974 f->fmt.sdr.pixelformat = dev->pixelformat;
975 f->fmt.sdr.buffersize = dev->buffersize;
976
977 return 0;
978}
979
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -0300980static int hackrf_try_fmt_sdr(struct file *file, void *priv,
981 struct v4l2_format *f)
Antti Palosaari969ec1f2014-08-23 04:40:01 -0300982{
983 struct hackrf_dev *dev = video_drvdata(file);
984 int i;
985
986 dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
987 (char *)&f->fmt.sdr.pixelformat);
988
989 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
990 for (i = 0; i < NUM_FORMATS; i++) {
991 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
992 f->fmt.sdr.buffersize = formats[i].buffersize;
993 return 0;
994 }
995 }
996
997 f->fmt.sdr.pixelformat = formats[0].pixelformat;
998 f->fmt.sdr.buffersize = formats[0].buffersize;
999
1000 return 0;
1001}
1002
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001003static int hackrf_enum_fmt_sdr(struct file *file, void *priv,
1004 struct v4l2_fmtdesc *f)
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001005{
1006 struct hackrf_dev *dev = video_drvdata(file);
1007
1008 dev_dbg(dev->dev, "index=%d\n", f->index);
1009
1010 if (f->index >= NUM_FORMATS)
1011 return -EINVAL;
1012
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001013 f->pixelformat = formats[f->index].pixelformat;
1014
1015 return 0;
1016}
1017
1018static int hackrf_s_tuner(struct file *file, void *priv,
1019 const struct v4l2_tuner *v)
1020{
1021 struct hackrf_dev *dev = video_drvdata(file);
1022 int ret;
1023
1024 dev_dbg(dev->dev, "index=%d\n", v->index);
1025
1026 if (v->index == 0)
1027 ret = 0;
1028 else if (v->index == 1)
1029 ret = 0;
1030 else
1031 ret = -EINVAL;
1032
1033 return ret;
1034}
1035
1036static int hackrf_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
1037{
1038 struct hackrf_dev *dev = video_drvdata(file);
1039 int ret;
1040
1041 dev_dbg(dev->dev, "index=%d\n", v->index);
1042
1043 if (v->index == 0) {
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001044 strscpy(v->name, "HackRF ADC", sizeof(v->name));
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001045 v->type = V4L2_TUNER_SDR;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001046 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001047 v->rangelow = bands_adc_dac[0].rangelow;
1048 v->rangehigh = bands_adc_dac[0].rangehigh;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001049 ret = 0;
1050 } else if (v->index == 1) {
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001051 strscpy(v->name, "HackRF RF", sizeof(v->name));
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001052 v->type = V4L2_TUNER_RF;
1053 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001054 v->rangelow = bands_rx_tx[0].rangelow;
1055 v->rangehigh = bands_rx_tx[0].rangehigh;
1056 ret = 0;
1057 } else {
1058 ret = -EINVAL;
1059 }
1060
1061 return ret;
1062}
1063
1064static int hackrf_s_modulator(struct file *file, void *fh,
1065 const struct v4l2_modulator *a)
1066{
1067 struct hackrf_dev *dev = video_drvdata(file);
1068
1069 dev_dbg(dev->dev, "index=%d\n", a->index);
1070
1071 return a->index > 1 ? -EINVAL : 0;
1072}
1073
1074static int hackrf_g_modulator(struct file *file, void *fh,
1075 struct v4l2_modulator *a)
1076{
1077 struct hackrf_dev *dev = video_drvdata(file);
1078 int ret;
1079
1080 dev_dbg(dev->dev, "index=%d\n", a->index);
1081
1082 if (a->index == 0) {
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001083 strscpy(a->name, "HackRF DAC", sizeof(a->name));
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001084 a->type = V4L2_TUNER_SDR;
1085 a->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1086 a->rangelow = bands_adc_dac[0].rangelow;
1087 a->rangehigh = bands_adc_dac[0].rangehigh;
1088 ret = 0;
1089 } else if (a->index == 1) {
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001090 strscpy(a->name, "HackRF RF", sizeof(a->name));
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001091 a->type = V4L2_TUNER_RF;
1092 a->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1093 a->rangelow = bands_rx_tx[0].rangelow;
1094 a->rangehigh = bands_rx_tx[0].rangehigh;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001095 ret = 0;
1096 } else {
1097 ret = -EINVAL;
1098 }
1099
1100 return ret;
1101}
1102
1103static int hackrf_s_frequency(struct file *file, void *priv,
1104 const struct v4l2_frequency *f)
1105{
1106 struct hackrf_dev *dev = video_drvdata(file);
Antti Palosaarieec20f02015-10-10 13:51:05 -03001107 struct usb_interface *intf = dev->intf;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001108 struct video_device *vdev = video_devdata(file);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001109 int ret;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001110 unsigned int uitmp;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001111
Antti Palosaarieec20f02015-10-10 13:51:05 -03001112 dev_dbg(&intf->dev, "tuner=%d type=%d frequency=%u\n",
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001113 f->tuner, f->type, f->frequency);
1114
1115 if (f->tuner == 0) {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001116 uitmp = clamp(f->frequency, bands_adc_dac[0].rangelow,
1117 bands_adc_dac[0].rangehigh);
1118 if (vdev->vfl_dir == VFL_DIR_RX) {
1119 dev->f_adc = uitmp;
1120 set_bit(RX_ADC_FREQUENCY, &dev->flags);
1121 } else {
1122 dev->f_dac = uitmp;
1123 set_bit(TX_DAC_FREQUENCY, &dev->flags);
1124 }
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001125 } else if (f->tuner == 1) {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001126 uitmp = clamp(f->frequency, bands_rx_tx[0].rangelow,
1127 bands_rx_tx[0].rangehigh);
1128 if (vdev->vfl_dir == VFL_DIR_RX) {
1129 dev->f_rx = uitmp;
1130 set_bit(RX_RF_FREQUENCY, &dev->flags);
1131 } else {
1132 dev->f_tx = uitmp;
1133 set_bit(TX_RF_FREQUENCY, &dev->flags);
1134 }
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001135 } else {
1136 ret = -EINVAL;
Antti Palosaarieec20f02015-10-10 13:51:05 -03001137 goto err;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001138 }
1139
Antti Palosaarieec20f02015-10-10 13:51:05 -03001140 ret = hackrf_set_params(dev);
1141 if (ret)
1142 goto err;
1143
1144 return 0;
1145err:
1146 dev_dbg(&intf->dev, "failed=%d\n", ret);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001147 return ret;
1148}
1149
1150static int hackrf_g_frequency(struct file *file, void *priv,
1151 struct v4l2_frequency *f)
1152{
1153 struct hackrf_dev *dev = video_drvdata(file);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001154 struct usb_interface *intf = dev->intf;
1155 struct video_device *vdev = video_devdata(file);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001156 int ret;
1157
1158 dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
1159
1160 if (f->tuner == 0) {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001161 f->type = V4L2_TUNER_SDR;
1162 if (vdev->vfl_dir == VFL_DIR_RX)
1163 f->frequency = dev->f_adc;
1164 else
1165 f->frequency = dev->f_dac;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001166 } else if (f->tuner == 1) {
1167 f->type = V4L2_TUNER_RF;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001168 if (vdev->vfl_dir == VFL_DIR_RX)
1169 f->frequency = dev->f_rx;
1170 else
1171 f->frequency = dev->f_tx;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001172 } else {
1173 ret = -EINVAL;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001174 goto err;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001175 }
1176
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001177 return 0;
1178err:
1179 dev_dbg(&intf->dev, "failed=%d\n", ret);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001180 return ret;
1181}
1182
1183static int hackrf_enum_freq_bands(struct file *file, void *priv,
1184 struct v4l2_frequency_band *band)
1185{
1186 struct hackrf_dev *dev = video_drvdata(file);
1187 int ret;
1188
1189 dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n",
1190 band->tuner, band->type, band->index);
1191
1192 if (band->tuner == 0) {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001193 if (band->index >= ARRAY_SIZE(bands_adc_dac)) {
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001194 ret = -EINVAL;
1195 } else {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001196 *band = bands_adc_dac[band->index];
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001197 ret = 0;
1198 }
1199 } else if (band->tuner == 1) {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001200 if (band->index >= ARRAY_SIZE(bands_rx_tx)) {
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001201 ret = -EINVAL;
1202 } else {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001203 *band = bands_rx_tx[band->index];
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001204 ret = 0;
1205 }
1206 } else {
1207 ret = -EINVAL;
1208 }
1209
1210 return ret;
1211}
1212
1213static const struct v4l2_ioctl_ops hackrf_ioctl_ops = {
1214 .vidioc_querycap = hackrf_querycap,
1215
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001216 .vidioc_s_fmt_sdr_cap = hackrf_s_fmt_sdr,
1217 .vidioc_g_fmt_sdr_cap = hackrf_g_fmt_sdr,
1218 .vidioc_enum_fmt_sdr_cap = hackrf_enum_fmt_sdr,
1219 .vidioc_try_fmt_sdr_cap = hackrf_try_fmt_sdr,
1220
1221 .vidioc_s_fmt_sdr_out = hackrf_s_fmt_sdr,
1222 .vidioc_g_fmt_sdr_out = hackrf_g_fmt_sdr,
1223 .vidioc_enum_fmt_sdr_out = hackrf_enum_fmt_sdr,
1224 .vidioc_try_fmt_sdr_out = hackrf_try_fmt_sdr,
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001225
1226 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1227 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1228 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1229 .vidioc_querybuf = vb2_ioctl_querybuf,
1230 .vidioc_qbuf = vb2_ioctl_qbuf,
1231 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001232 .vidioc_expbuf = vb2_ioctl_expbuf,
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001233
1234 .vidioc_streamon = vb2_ioctl_streamon,
1235 .vidioc_streamoff = vb2_ioctl_streamoff,
1236
1237 .vidioc_s_tuner = hackrf_s_tuner,
1238 .vidioc_g_tuner = hackrf_g_tuner,
1239
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001240 .vidioc_s_modulator = hackrf_s_modulator,
1241 .vidioc_g_modulator = hackrf_g_modulator,
1242
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001243 .vidioc_s_frequency = hackrf_s_frequency,
1244 .vidioc_g_frequency = hackrf_g_frequency,
1245 .vidioc_enum_freq_bands = hackrf_enum_freq_bands,
1246
1247 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1248 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1249 .vidioc_log_status = v4l2_ctrl_log_status,
1250};
1251
1252static const struct v4l2_file_operations hackrf_fops = {
1253 .owner = THIS_MODULE,
1254 .open = v4l2_fh_open,
1255 .release = vb2_fop_release,
1256 .read = vb2_fop_read,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001257 .write = vb2_fop_write,
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001258 .poll = vb2_fop_poll,
1259 .mmap = vb2_fop_mmap,
1260 .unlocked_ioctl = video_ioctl2,
1261};
1262
Bhumika Goyal86844942017-08-26 09:11:30 -04001263static const struct video_device hackrf_template = {
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001264 .name = "HackRF One",
1265 .release = video_device_release_empty,
1266 .fops = &hackrf_fops,
1267 .ioctl_ops = &hackrf_ioctl_ops,
1268};
1269
1270static void hackrf_video_release(struct v4l2_device *v)
1271{
1272 struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev);
1273
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001274 dev_dbg(dev->dev, "\n");
1275
1276 v4l2_ctrl_handler_free(&dev->rx_ctrl_handler);
1277 v4l2_ctrl_handler_free(&dev->tx_ctrl_handler);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001278 v4l2_device_unregister(&dev->v4l2_dev);
1279 kfree(dev);
1280}
1281
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001282static int hackrf_s_ctrl_rx(struct v4l2_ctrl *ctrl)
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001283{
1284 struct hackrf_dev *dev = container_of(ctrl->handler,
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001285 struct hackrf_dev, rx_ctrl_handler);
Antti Palosaarieec20f02015-10-10 13:51:05 -03001286 struct usb_interface *intf = dev->intf;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001287 int ret;
1288
1289 switch (ctrl->id) {
1290 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1291 case V4L2_CID_RF_TUNER_BANDWIDTH:
Antti Palosaarieec20f02015-10-10 13:51:05 -03001292 set_bit(RX_BANDWIDTH, &dev->flags);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001293 break;
Antti Palosaarib3ae2962015-10-10 13:51:04 -03001294 case V4L2_CID_RF_TUNER_RF_GAIN:
Antti Palosaarieec20f02015-10-10 13:51:05 -03001295 set_bit(RX_RF_GAIN, &dev->flags);
Antti Palosaarib3ae2962015-10-10 13:51:04 -03001296 break;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001297 case V4L2_CID_RF_TUNER_LNA_GAIN:
Antti Palosaarieec20f02015-10-10 13:51:05 -03001298 set_bit(RX_LNA_GAIN, &dev->flags);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001299 break;
1300 case V4L2_CID_RF_TUNER_IF_GAIN:
Antti Palosaarieec20f02015-10-10 13:51:05 -03001301 set_bit(RX_IF_GAIN, &dev->flags);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001302 break;
1303 default:
Antti Palosaarieec20f02015-10-10 13:51:05 -03001304 dev_dbg(&intf->dev, "unknown ctrl: id=%d name=%s\n",
1305 ctrl->id, ctrl->name);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001306 ret = -EINVAL;
Antti Palosaarieec20f02015-10-10 13:51:05 -03001307 goto err;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001308 }
1309
Antti Palosaarieec20f02015-10-10 13:51:05 -03001310 ret = hackrf_set_params(dev);
1311 if (ret)
1312 goto err;
1313
1314 return 0;
1315err:
1316 dev_dbg(&intf->dev, "failed=%d\n", ret);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001317 return ret;
1318}
1319
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001320static int hackrf_s_ctrl_tx(struct v4l2_ctrl *ctrl)
1321{
1322 struct hackrf_dev *dev = container_of(ctrl->handler,
1323 struct hackrf_dev, tx_ctrl_handler);
1324 struct usb_interface *intf = dev->intf;
1325 int ret;
1326
1327 switch (ctrl->id) {
1328 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1329 case V4L2_CID_RF_TUNER_BANDWIDTH:
1330 set_bit(TX_BANDWIDTH, &dev->flags);
1331 break;
1332 case V4L2_CID_RF_TUNER_LNA_GAIN:
1333 set_bit(TX_LNA_GAIN, &dev->flags);
1334 break;
1335 case V4L2_CID_RF_TUNER_RF_GAIN:
1336 set_bit(TX_RF_GAIN, &dev->flags);
1337 break;
1338 default:
1339 dev_dbg(&intf->dev, "unknown ctrl: id=%d name=%s\n",
1340 ctrl->id, ctrl->name);
1341 ret = -EINVAL;
1342 goto err;
1343 }
1344
1345 ret = hackrf_set_params(dev);
1346 if (ret)
1347 goto err;
1348
1349 return 0;
1350err:
1351 dev_dbg(&intf->dev, "failed=%d\n", ret);
1352 return ret;
1353}
1354
1355static const struct v4l2_ctrl_ops hackrf_ctrl_ops_rx = {
1356 .s_ctrl = hackrf_s_ctrl_rx,
1357};
1358
1359static const struct v4l2_ctrl_ops hackrf_ctrl_ops_tx = {
1360 .s_ctrl = hackrf_s_ctrl_tx,
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001361};
1362
1363static int hackrf_probe(struct usb_interface *intf,
1364 const struct usb_device_id *id)
1365{
1366 struct hackrf_dev *dev;
1367 int ret;
1368 u8 u8tmp, buf[BUF_SIZE];
1369
1370 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001371 if (!dev) {
1372 ret = -ENOMEM;
1373 goto err;
1374 }
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001375
1376 mutex_init(&dev->v4l2_lock);
1377 mutex_init(&dev->vb_queue_lock);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001378 spin_lock_init(&dev->buffer_list_lock);
1379 INIT_LIST_HEAD(&dev->rx_buffer_list);
1380 INIT_LIST_HEAD(&dev->tx_buffer_list);
Antti Palosaarieec20f02015-10-10 13:51:05 -03001381 dev->intf = intf;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001382 dev->dev = &intf->dev;
1383 dev->udev = interface_to_usbdev(intf);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001384 dev->pixelformat = formats[0].pixelformat;
1385 dev->buffersize = formats[0].buffersize;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001386 dev->f_adc = bands_adc_dac[0].rangelow;
1387 dev->f_dac = bands_adc_dac[0].rangelow;
1388 dev->f_rx = bands_rx_tx[0].rangelow;
1389 dev->f_tx = bands_rx_tx[0].rangelow;
1390 set_bit(RX_ADC_FREQUENCY, &dev->flags);
1391 set_bit(TX_DAC_FREQUENCY, &dev->flags);
1392 set_bit(RX_RF_FREQUENCY, &dev->flags);
1393 set_bit(TX_RF_FREQUENCY, &dev->flags);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001394
1395 /* Detect device */
1396 ret = hackrf_ctrl_msg(dev, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1);
1397 if (ret == 0)
1398 ret = hackrf_ctrl_msg(dev, CMD_VERSION_STRING_READ, 0, 0,
1399 buf, BUF_SIZE);
1400 if (ret) {
1401 dev_err(dev->dev, "Could not detect board\n");
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001402 goto err_kfree;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001403 }
1404
1405 buf[BUF_SIZE - 1] = '\0';
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001406 dev_info(dev->dev, "Board ID: %02x\n", u8tmp);
1407 dev_info(dev->dev, "Firmware version: %s\n", buf);
1408
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001409 /* Init vb2 queue structure for receiver */
1410 dev->rx_vb2_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1411 dev->rx_vb2_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF |
1412 VB2_READ;
1413 dev->rx_vb2_queue.ops = &hackrf_vb2_ops;
1414 dev->rx_vb2_queue.mem_ops = &vb2_vmalloc_memops;
1415 dev->rx_vb2_queue.drv_priv = dev;
1416 dev->rx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
1417 dev->rx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1418 ret = vb2_queue_init(&dev->rx_vb2_queue);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001419 if (ret) {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001420 dev_err(dev->dev, "Could not initialize rx vb2 queue\n");
1421 goto err_kfree;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001422 }
1423
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001424 /* Init vb2 queue structure for transmitter */
1425 dev->tx_vb2_queue.type = V4L2_BUF_TYPE_SDR_OUTPUT;
1426 dev->tx_vb2_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF |
1427 VB2_WRITE;
1428 dev->tx_vb2_queue.ops = &hackrf_vb2_ops;
1429 dev->tx_vb2_queue.mem_ops = &vb2_vmalloc_memops;
1430 dev->tx_vb2_queue.drv_priv = dev;
1431 dev->tx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
1432 dev->tx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1433 ret = vb2_queue_init(&dev->tx_vb2_queue);
1434 if (ret) {
1435 dev_err(dev->dev, "Could not initialize tx vb2 queue\n");
1436 goto err_kfree;
1437 }
1438
1439 /* Register controls for receiver */
1440 v4l2_ctrl_handler_init(&dev->rx_ctrl_handler, 5);
1441 dev->rx_bandwidth_auto = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1442 &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1443 0, 1, 0, 1);
1444 dev->rx_bandwidth = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1445 &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_BANDWIDTH,
1446 1750000, 28000000, 50000, 1750000);
1447 v4l2_ctrl_auto_cluster(2, &dev->rx_bandwidth_auto, 0, false);
1448 dev->rx_rf_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1449 &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_RF_GAIN, 0, 12, 12, 0);
1450 dev->rx_lna_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1451 &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_LNA_GAIN, 0, 40, 8, 0);
1452 dev->rx_if_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1453 &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_IF_GAIN, 0, 62, 2, 0);
1454 if (dev->rx_ctrl_handler.error) {
1455 ret = dev->rx_ctrl_handler.error;
1456 dev_err(dev->dev, "Could not initialize controls\n");
1457 goto err_v4l2_ctrl_handler_free_rx;
1458 }
Antti Palosaarid47fa532015-10-23 20:01:31 -02001459 v4l2_ctrl_grab(dev->rx_rf_gain, !hackrf_enable_rf_gain_ctrl);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001460 v4l2_ctrl_handler_setup(&dev->rx_ctrl_handler);
1461
1462 /* Register controls for transmitter */
1463 v4l2_ctrl_handler_init(&dev->tx_ctrl_handler, 4);
1464 dev->tx_bandwidth_auto = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
1465 &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1466 0, 1, 0, 1);
1467 dev->tx_bandwidth = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
1468 &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_BANDWIDTH,
1469 1750000, 28000000, 50000, 1750000);
1470 v4l2_ctrl_auto_cluster(2, &dev->tx_bandwidth_auto, 0, false);
1471 dev->tx_lna_gain = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
1472 &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_LNA_GAIN, 0, 47, 1, 0);
1473 dev->tx_rf_gain = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
1474 &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_RF_GAIN, 0, 15, 15, 0);
1475 if (dev->tx_ctrl_handler.error) {
1476 ret = dev->tx_ctrl_handler.error;
1477 dev_err(dev->dev, "Could not initialize controls\n");
1478 goto err_v4l2_ctrl_handler_free_tx;
1479 }
Antti Palosaarid47fa532015-10-23 20:01:31 -02001480 v4l2_ctrl_grab(dev->tx_rf_gain, !hackrf_enable_rf_gain_ctrl);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001481 v4l2_ctrl_handler_setup(&dev->tx_ctrl_handler);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001482
1483 /* Register the v4l2_device structure */
1484 dev->v4l2_dev.release = hackrf_video_release;
1485 ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev);
1486 if (ret) {
1487 dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001488 goto err_v4l2_ctrl_handler_free_tx;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001489 }
1490
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001491 /* Init video_device structure for receiver */
1492 dev->rx_vdev = hackrf_template;
1493 dev->rx_vdev.queue = &dev->rx_vb2_queue;
1494 dev->rx_vdev.queue->lock = &dev->vb_queue_lock;
1495 dev->rx_vdev.v4l2_dev = &dev->v4l2_dev;
1496 dev->rx_vdev.ctrl_handler = &dev->rx_ctrl_handler;
1497 dev->rx_vdev.lock = &dev->v4l2_lock;
1498 dev->rx_vdev.vfl_dir = VFL_DIR_RX;
1499 video_set_drvdata(&dev->rx_vdev, dev);
1500 ret = video_register_device(&dev->rx_vdev, VFL_TYPE_SDR, -1);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001501 if (ret) {
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001502 dev_err(dev->dev,
1503 "Failed to register as video device (%d)\n", ret);
1504 goto err_v4l2_device_unregister;
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001505 }
1506 dev_info(dev->dev, "Registered as %s\n",
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001507 video_device_node_name(&dev->rx_vdev));
1508
1509 /* Init video_device structure for transmitter */
1510 dev->tx_vdev = hackrf_template;
1511 dev->tx_vdev.queue = &dev->tx_vb2_queue;
1512 dev->tx_vdev.queue->lock = &dev->vb_queue_lock;
1513 dev->tx_vdev.v4l2_dev = &dev->v4l2_dev;
1514 dev->tx_vdev.ctrl_handler = &dev->tx_ctrl_handler;
1515 dev->tx_vdev.lock = &dev->v4l2_lock;
1516 dev->tx_vdev.vfl_dir = VFL_DIR_TX;
1517 video_set_drvdata(&dev->tx_vdev, dev);
1518 ret = video_register_device(&dev->tx_vdev, VFL_TYPE_SDR, -1);
1519 if (ret) {
1520 dev_err(dev->dev,
1521 "Failed to register as video device (%d)\n", ret);
1522 goto err_video_unregister_device_rx;
1523 }
1524 dev_info(dev->dev, "Registered as %s\n",
1525 video_device_node_name(&dev->tx_vdev));
1526
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001527 dev_notice(dev->dev, "SDR API is still slightly experimental and functionality changes may follow\n");
1528 return 0;
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001529err_video_unregister_device_rx:
1530 video_unregister_device(&dev->rx_vdev);
1531err_v4l2_device_unregister:
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001532 v4l2_device_unregister(&dev->v4l2_dev);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001533err_v4l2_ctrl_handler_free_tx:
1534 v4l2_ctrl_handler_free(&dev->tx_ctrl_handler);
1535err_v4l2_ctrl_handler_free_rx:
1536 v4l2_ctrl_handler_free(&dev->rx_ctrl_handler);
1537err_kfree:
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001538 kfree(dev);
Antti Palosaari8bc4a9e2015-10-10 13:51:06 -03001539err:
Antti Palosaarieb35cf42015-10-21 19:02:41 -02001540 dev_dbg(&intf->dev, "failed=%d\n", ret);
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001541 return ret;
1542}
1543
1544/* USB device ID list */
Arvind Yadav7fb2e072017-08-13 04:54:43 -04001545static const struct usb_device_id hackrf_id_table[] = {
Antti Palosaari969ec1f2014-08-23 04:40:01 -03001546 { USB_DEVICE(0x1d50, 0x6089) }, /* HackRF One */
1547 { }
1548};
1549MODULE_DEVICE_TABLE(usb, hackrf_id_table);
1550
1551/* USB subsystem interface */
1552static struct usb_driver hackrf_driver = {
1553 .name = KBUILD_MODNAME,
1554 .probe = hackrf_probe,
1555 .disconnect = hackrf_disconnect,
1556 .id_table = hackrf_id_table,
1557};
1558
1559module_usb_driver(hackrf_driver);
1560
1561MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1562MODULE_DESCRIPTION("HackRF");
1563MODULE_LICENSE("GPL");