1238106Sdes// SPDX-License-Identifier: GPL-2.0-or-later
2238106Sdes/*
3238106Sdes * HackRF driver
4238106Sdes *
5238106Sdes * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
6238106Sdes */
7238106Sdes
8238106Sdes#include <linux/module.h>
9238106Sdes#include <linux/slab.h>
10238106Sdes#include <linux/usb.h>
11238106Sdes#include <media/v4l2-device.h>
12238106Sdes#include <media/v4l2-ioctl.h>
13238106Sdes#include <media/v4l2-ctrls.h>
14238106Sdes#include <media/v4l2-event.h>
15238106Sdes#include <media/videobuf2-v4l2.h>
16238106Sdes#include <media/videobuf2-vmalloc.h>
17238106Sdes
18238106Sdes/*
19238106Sdes * Used Avago MGA-81563 RF amplifier could be destroyed pretty easily with too
20238106Sdes * strong signal or transmitting to bad antenna.
21238106Sdes * Set RF gain control to 'grabbed' state by default for sure.
22238106Sdes */
23238106Sdesstatic bool hackrf_enable_rf_gain_ctrl;
24269257Sdesmodule_param_named(enable_rf_gain_ctrl, hackrf_enable_rf_gain_ctrl, bool, 0644);
25269257SdesMODULE_PARM_DESC(enable_rf_gain_ctrl, "enable RX/TX RF amplifier control (warn: could damage amplifier)");
26269257Sdes
27269257Sdes/* HackRF USB API commands (from HackRF Library) */
28269257Sdesenum {
29269257Sdes	CMD_SET_TRANSCEIVER_MODE           = 0x01,
30269257Sdes	CMD_SAMPLE_RATE_SET                = 0x06,
31269257Sdes	CMD_BASEBAND_FILTER_BANDWIDTH_SET  = 0x07,
32269257Sdes	CMD_BOARD_ID_READ                  = 0x0e,
33269257Sdes	CMD_VERSION_STRING_READ            = 0x0f,
34238106Sdes	CMD_SET_FREQ                       = 0x10,
35238106Sdes	CMD_AMP_ENABLE                     = 0x11,
36238106Sdes	CMD_SET_LNA_GAIN                   = 0x13,
37238106Sdes	CMD_SET_VGA_GAIN                   = 0x14,
38238106Sdes	CMD_SET_TXVGA_GAIN                 = 0x15,
39238106Sdes};
40238106Sdes
41238106Sdes/*
42238106Sdes *       bEndpointAddress     0x81  EP 1 IN
43238106Sdes *         Transfer Type            Bulk
44238106Sdes *       wMaxPacketSize     0x0200  1x 512 bytes
45238106Sdes */
46238106Sdes#define MAX_BULK_BUFS            (6)
47238106Sdes#define BULK_BUFFER_SIZE         (128 * 512)
48238106Sdes
49238106Sdesstatic const struct v4l2_frequency_band bands_adc_dac[] = {
50238106Sdes	{
51238106Sdes		.tuner = 0,
52238106Sdes		.type = V4L2_TUNER_SDR,
53238106Sdes		.index = 0,
54238106Sdes		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
55238106Sdes		.rangelow   =   200000,
56269257Sdes		.rangehigh  = 24000000,
57238106Sdes	},
58238106Sdes};
59238106Sdes
60238106Sdesstatic const struct v4l2_frequency_band bands_rx_tx[] = {
61238106Sdes	{
62238106Sdes		.tuner = 1,
63269257Sdes		.type = V4L2_TUNER_RF,
64238106Sdes		.index = 0,
65238106Sdes		.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
66269257Sdes		.rangelow   =          1,
67238106Sdes		.rangehigh  = 4294967294LL, /* max u32, hw goes over 7GHz */
68238106Sdes	},
69238106Sdes};
70238106Sdes
71238106Sdes/* stream formats */
72238106Sdesstruct hackrf_format {
73238106Sdes	u32	pixelformat;
74238106Sdes	u32	buffersize;
75238106Sdes};
76238106Sdes
77238106Sdes/* format descriptions for capture and preview */
78238106Sdesstatic struct hackrf_format formats[] = {
79238106Sdes	{
80238106Sdes		.pixelformat	= V4L2_SDR_FMT_CS8,
81238106Sdes		.buffersize	= BULK_BUFFER_SIZE,
82238106Sdes	},
83238106Sdes};
84238106Sdes
85238106Sdesstatic const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
86238106Sdes
87238106Sdes/* intermediate buffers with raw data from the USB device */
88238106Sdesstruct hackrf_buffer {
89238106Sdes	struct vb2_v4l2_buffer vb;
90238106Sdes	struct list_head list;
91238106Sdes};
92238106Sdes
93238106Sdesstruct hackrf_dev {
94238106Sdes#define USB_STATE_URB_BUF                1 /* XXX: set manually */
95238106Sdes#define RX_ON                            4
96238106Sdes#define TX_ON                            5
97238106Sdes#define RX_ADC_FREQUENCY                11
98238106Sdes#define TX_DAC_FREQUENCY                12
99238106Sdes#define RX_BANDWIDTH                    13
100238106Sdes#define TX_BANDWIDTH                    14
101238106Sdes#define RX_RF_FREQUENCY                 15
102238106Sdes#define TX_RF_FREQUENCY                 16
103238106Sdes#define RX_RF_GAIN                      17
104238106Sdes#define TX_RF_GAIN                      18
105238106Sdes#define RX_IF_GAIN                      19
106238106Sdes#define RX_LNA_GAIN                     20
107238106Sdes#define TX_LNA_GAIN                     21
108238106Sdes	unsigned long flags;
109238106Sdes
110238106Sdes	struct usb_interface *intf;
111238106Sdes	struct device *dev;
112269257Sdes	struct usb_device *udev;
113238106Sdes	struct video_device rx_vdev;
114238106Sdes	struct video_device tx_vdev;
115238106Sdes	struct v4l2_device v4l2_dev;
116238106Sdes
117238106Sdes	/* videobuf2 queue and queued buffers list */
118238106Sdes	struct vb2_queue rx_vb2_queue;
119238106Sdes	struct vb2_queue tx_vb2_queue;
120238106Sdes	struct list_head rx_buffer_list;
121238106Sdes	struct list_head tx_buffer_list;
122238106Sdes	spinlock_t buffer_list_lock; /* Protects buffer_list */
123238106Sdes	unsigned int sequence;	     /* Buffer sequence counter */
124238106Sdes	unsigned int vb_full;        /* vb is full and packets dropped */
125238106Sdes	unsigned int vb_empty;       /* vb is empty and packets dropped */
126238106Sdes
127238106Sdes	/* Note if taking both locks v4l2_lock must always be locked first! */
128238106Sdes	struct mutex v4l2_lock;      /* Protects everything else */
129238106Sdes	struct mutex vb_queue_lock;  /* Protects vb_queue */
130238106Sdes
131238106Sdes	struct urb     *urb_list[MAX_BULK_BUFS];
132238106Sdes	int            buf_num;
133238106Sdes	unsigned long  buf_size;
134238106Sdes	u8             *buf_list[MAX_BULK_BUFS];
135238106Sdes	dma_addr_t     dma_addr[MAX_BULK_BUFS];
136269257Sdes	int            urbs_initialized;
137238106Sdes	int            urbs_submitted;
138238106Sdes
139238106Sdes	/* USB control message buffer */
140269257Sdes	#define BUF_SIZE 24
141269257Sdes	u8 buf[BUF_SIZE];
142238106Sdes
143269257Sdes	/* Current configuration */
144238106Sdes	unsigned int f_adc;
145238106Sdes	unsigned int f_dac;
146238106Sdes	unsigned int f_rx;
147238106Sdes	unsigned int f_tx;
148238106Sdes	u32 pixelformat;
149238106Sdes	u32 buffersize;
150238106Sdes
151269257Sdes	/* Controls */
152238106Sdes	struct v4l2_ctrl_handler rx_ctrl_handler;
153238106Sdes	struct v4l2_ctrl *rx_bandwidth_auto;
154238106Sdes	struct v4l2_ctrl *rx_bandwidth;
155238106Sdes	struct v4l2_ctrl *rx_rf_gain;
156238106Sdes	struct v4l2_ctrl *rx_lna_gain;
157238106Sdes	struct v4l2_ctrl *rx_if_gain;
158238106Sdes	struct v4l2_ctrl_handler tx_ctrl_handler;
159238106Sdes	struct v4l2_ctrl *tx_bandwidth_auto;
160238106Sdes	struct v4l2_ctrl *tx_bandwidth;
161238106Sdes	struct v4l2_ctrl *tx_rf_gain;
162238106Sdes	struct v4l2_ctrl *tx_lna_gain;
163238106Sdes
164238106Sdes	/* Sample rate calc */
165238106Sdes	unsigned long jiffies_next;
166238106Sdes	unsigned int sample;
167238106Sdes	unsigned int sample_measured;
168238106Sdes};
169238106Sdes
170238106Sdes#define hackrf_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
171238106Sdes	char *_direction; \
172238106Sdes	if (_t & USB_DIR_IN) \
173238106Sdes		_direction = "<<<"; \
174238106Sdes	else \
175238106Sdes		_direction = ">>>"; \
176238106Sdes	dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
177238106Sdes			_t, _r, _v & 0xff, _v >> 8, _i & 0xff, \
178238106Sdes			_i >> 8, _l & 0xff, _l >> 8, _direction, _l, _b); \
179238106Sdes}
180238106Sdes
181238106Sdes/* execute firmware command */
182238106Sdesstatic int hackrf_ctrl_msg(struct hackrf_dev *dev, u8 request, u16 value,
183238106Sdes		u16 index, u8 *data, u16 size)
184238106Sdes{
185238106Sdes	int ret;
186238106Sdes	unsigned int pipe;
187238106Sdes	u8 requesttype;
188238106Sdes
189238106Sdes	switch (request) {
190238106Sdes	case CMD_SET_TRANSCEIVER_MODE:
191238106Sdes	case CMD_SET_FREQ:
192238106Sdes	case CMD_AMP_ENABLE:
193238106Sdes	case CMD_SAMPLE_RATE_SET:
194238106Sdes	case CMD_BASEBAND_FILTER_BANDWIDTH_SET:
195238106Sdes		pipe = usb_sndctrlpipe(dev->udev, 0);
196238106Sdes		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
197238106Sdes		break;
198238106Sdes	case CMD_BOARD_ID_READ:
199238106Sdes	case CMD_VERSION_STRING_READ:
200238106Sdes	case CMD_SET_LNA_GAIN:
201238106Sdes	case CMD_SET_VGA_GAIN:
202238106Sdes	case CMD_SET_TXVGA_GAIN:
203238106Sdes		pipe = usb_rcvctrlpipe(dev->udev, 0);
204238106Sdes		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
205238106Sdes		break;
206238106Sdes	default:
207238106Sdes		dev_err(dev->dev, "Unknown command %02x\n", request);
208238106Sdes		ret = -EINVAL;
209238106Sdes		goto err;
210238106Sdes	}
211238106Sdes
212238106Sdes	/* write request */
213269257Sdes	if (!(requesttype & USB_DIR_IN))
214238106Sdes		memcpy(dev->buf, data, size);
215238106Sdes
216238106Sdes	ret = usb_control_msg(dev->udev, pipe, request, requesttype, value,
217238106Sdes			index, dev->buf, size, 1000);
218238106Sdes	hackrf_dbg_usb_control_msg(dev->dev, request, requesttype, value,
219238106Sdes			index, dev->buf, size);
220238106Sdes	if (ret < 0) {
221238106Sdes		dev_err(dev->dev, "usb_control_msg() failed %d request %02x\n",
222238106Sdes				ret, request);
223238106Sdes		goto err;
224238106Sdes	}
225238106Sdes
226238106Sdes	/* read request */
227238106Sdes	if (requesttype & USB_DIR_IN)
228238106Sdes		memcpy(data, dev->buf, size);
229238106Sdes
230238106Sdes	return 0;
231238106Sdeserr:
232238106Sdes	return ret;
233238106Sdes}
234238106Sdes
235238106Sdesstatic int hackrf_set_params(struct hackrf_dev *dev)
236238106Sdes{
237269257Sdes	struct usb_interface *intf = dev->intf;
238238106Sdes	int ret, i;
239238106Sdes	u8 buf[8], u8tmp;
240238106Sdes	unsigned int uitmp, uitmp1, uitmp2;
241238106Sdes	const bool rx = test_bit(RX_ON, &dev->flags);
242238106Sdes	const bool tx = test_bit(TX_ON, &dev->flags);
243238106Sdes	static const struct {
244238106Sdes		u32 freq;
245238106Sdes	} bandwidth_lut[] = {
246238106Sdes		{ 1750000}, /*  1.75 MHz */
247238106Sdes		{ 2500000}, /*  2.5  MHz */
248238106Sdes		{ 3500000}, /*  3.5  MHz */
249238106Sdes		{ 5000000}, /*  5    MHz */
250238106Sdes		{ 5500000}, /*  5.5  MHz */
251238106Sdes		{ 6000000}, /*  6    MHz */
252238106Sdes		{ 7000000}, /*  7    MHz */
253238106Sdes		{ 8000000}, /*  8    MHz */
254238106Sdes		{ 9000000}, /*  9    MHz */
255238106Sdes		{10000000}, /* 10    MHz */
256238106Sdes		{12000000}, /* 12    MHz */
257238106Sdes		{14000000}, /* 14    MHz */
258238106Sdes		{15000000}, /* 15    MHz */
259238106Sdes		{20000000}, /* 20    MHz */
260238106Sdes		{24000000}, /* 24    MHz */
261238106Sdes		{28000000}, /* 28    MHz */
262238106Sdes	};
263238106Sdes
264238106Sdes	if (!rx && !tx) {
265238106Sdes		dev_dbg(&intf->dev, "device is sleeping\n");
266238106Sdes		return 0;
267238106Sdes	}
268269257Sdes
269269257Sdes	/* ADC / DAC frequency */
270238106Sdes	if (rx && test_and_clear_bit(RX_ADC_FREQUENCY, &dev->flags)) {
271238106Sdes		dev_dbg(&intf->dev, "RX ADC frequency=%u Hz\n", dev->f_adc);
272238106Sdes		uitmp1 = dev->f_adc;
273238106Sdes		uitmp2 = 1;
274238106Sdes		set_bit(TX_DAC_FREQUENCY, &dev->flags);
275238106Sdes	} else if (tx && test_and_clear_bit(TX_DAC_FREQUENCY, &dev->flags)) {
276238106Sdes		dev_dbg(&intf->dev, "TX DAC frequency=%u Hz\n", dev->f_dac);
277238106Sdes		uitmp1 = dev->f_dac;
278238106Sdes		uitmp2 = 1;
279238106Sdes		set_bit(RX_ADC_FREQUENCY, &dev->flags);
280238106Sdes	} else {
281238106Sdes		uitmp1 = uitmp2 = 0;
282238106Sdes	}
283238106Sdes	if (uitmp1 || uitmp2) {
284238106Sdes		buf[0] = (uitmp1 >>  0) & 0xff;
285238106Sdes		buf[1] = (uitmp1 >>  8) & 0xff;
286238106Sdes		buf[2] = (uitmp1 >> 16) & 0xff;
287238106Sdes		buf[3] = (uitmp1 >> 24) & 0xff;
288238106Sdes		buf[4] = (uitmp2 >>  0) & 0xff;
289238106Sdes		buf[5] = (uitmp2 >>  8) & 0xff;
290238106Sdes		buf[6] = (uitmp2 >> 16) & 0xff;
291269257Sdes		buf[7] = (uitmp2 >> 24) & 0xff;
292238106Sdes		ret = hackrf_ctrl_msg(dev, CMD_SAMPLE_RATE_SET, 0, 0, buf, 8);
293238106Sdes		if (ret)
294238106Sdes			goto err;
295238106Sdes	}
296238106Sdes
297238106Sdes	/* bandwidth */
298238106Sdes	if (rx && test_and_clear_bit(RX_BANDWIDTH, &dev->flags)) {
299238106Sdes		if (dev->rx_bandwidth_auto->val == true)
300238106Sdes			uitmp = dev->f_adc;
301238106Sdes		else
302238106Sdes			uitmp = dev->rx_bandwidth->val;
303269257Sdes
304238106Sdes		for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
305238106Sdes			if (uitmp <= bandwidth_lut[i].freq) {
306238106Sdes				uitmp = bandwidth_lut[i].freq;
307238106Sdes				break;
308238106Sdes			}
309238106Sdes		}
310238106Sdes		dev->rx_bandwidth->val = uitmp;
311238106Sdes		dev->rx_bandwidth->cur.val = uitmp;
312269257Sdes		dev_dbg(&intf->dev, "RX bandwidth selected=%u\n", uitmp);
313238106Sdes		set_bit(TX_BANDWIDTH, &dev->flags);
314238106Sdes	} else if (tx && test_and_clear_bit(TX_BANDWIDTH, &dev->flags)) {
315238106Sdes		if (dev->tx_bandwidth_auto->val == true)
316238106Sdes			uitmp = dev->f_dac;
317238106Sdes		else
318238106Sdes			uitmp = dev->tx_bandwidth->val;
319238106Sdes
320238106Sdes		for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
321238106Sdes			if (uitmp <= bandwidth_lut[i].freq) {
322238106Sdes				uitmp = bandwidth_lut[i].freq;
323238106Sdes				break;
324238106Sdes			}
325238106Sdes		}
326238106Sdes		dev->tx_bandwidth->val = uitmp;
327238106Sdes		dev->tx_bandwidth->cur.val = uitmp;
328238106Sdes		dev_dbg(&intf->dev, "TX bandwidth selected=%u\n", uitmp);
329238106Sdes		set_bit(RX_BANDWIDTH, &dev->flags);
330238106Sdes	} else {
331269257Sdes		uitmp = 0;
332238106Sdes	}
333238106Sdes	if (uitmp) {
334238106Sdes		uitmp1 = uitmp2 = 0;
335238106Sdes		uitmp1 |= ((uitmp >>  0) & 0xff) << 0;
336238106Sdes		uitmp1 |= ((uitmp >>  8) & 0xff) << 8;
337238106Sdes		uitmp2 |= ((uitmp >> 16) & 0xff) << 0;
338238106Sdes		uitmp2 |= ((uitmp >> 24) & 0xff) << 8;
339238106Sdes		ret = hackrf_ctrl_msg(dev, CMD_BASEBAND_FILTER_BANDWIDTH_SET,
340238106Sdes				      uitmp1, uitmp2, NULL, 0);
341238106Sdes		if (ret)
342238106Sdes			goto err;
343238106Sdes	}
344238106Sdes
345238106Sdes	/* RX / TX RF frequency */
346238106Sdes	if (rx && test_and_clear_bit(RX_RF_FREQUENCY, &dev->flags)) {
347238106Sdes		dev_dbg(&intf->dev, "RX RF frequency=%u Hz\n", dev->f_rx);
348238106Sdes		uitmp1 = dev->f_rx / 1000000;
349238106Sdes		uitmp2 = dev->f_rx % 1000000;
350238106Sdes		set_bit(TX_RF_FREQUENCY, &dev->flags);
351238106Sdes	} else if (tx && test_and_clear_bit(TX_RF_FREQUENCY, &dev->flags)) {
352238106Sdes		dev_dbg(&intf->dev, "TX RF frequency=%u Hz\n", dev->f_tx);
353238106Sdes		uitmp1 = dev->f_tx / 1000000;
354238106Sdes		uitmp2 = dev->f_tx % 1000000;
355238106Sdes		set_bit(RX_RF_FREQUENCY, &dev->flags);
356238106Sdes	} else {
357238106Sdes		uitmp1 = uitmp2 = 0;
358238106Sdes	}
359238106Sdes	if (uitmp1 || uitmp2) {
360238106Sdes		buf[0] = (uitmp1 >>  0) & 0xff;
361238106Sdes		buf[1] = (uitmp1 >>  8) & 0xff;
362238106Sdes		buf[2] = (uitmp1 >> 16) & 0xff;
363238106Sdes		buf[3] = (uitmp1 >> 24) & 0xff;
364238106Sdes		buf[4] = (uitmp2 >>  0) & 0xff;
365238106Sdes		buf[5] = (uitmp2 >>  8) & 0xff;
366238106Sdes		buf[6] = (uitmp2 >> 16) & 0xff;
367238106Sdes		buf[7] = (uitmp2 >> 24) & 0xff;
368238106Sdes		ret = hackrf_ctrl_msg(dev, CMD_SET_FREQ, 0, 0, buf, 8);
369238106Sdes		if (ret)
370238106Sdes			goto err;
371238106Sdes	}
372238106Sdes
373238106Sdes	/* RX RF gain */
374238106Sdes	if (rx && test_and_clear_bit(RX_RF_GAIN, &dev->flags)) {
375238106Sdes		dev_dbg(&intf->dev, "RX RF gain val=%d->%d\n",
376238106Sdes			dev->rx_rf_gain->cur.val, dev->rx_rf_gain->val);
377238106Sdes
378238106Sdes		u8tmp = (dev->rx_rf_gain->val) ? 1 : 0;
379238106Sdes		ret = hackrf_ctrl_msg(dev, CMD_AMP_ENABLE, u8tmp, 0, NULL, 0);
380238106Sdes		if (ret)
381238106Sdes			goto err;
382238106Sdes		set_bit(TX_RF_GAIN, &dev->flags);
383238106Sdes	}
384238106Sdes
385238106Sdes	/* TX RF gain */
386238106Sdes	if (tx && test_and_clear_bit(TX_RF_GAIN, &dev->flags)) {
387238106Sdes		dev_dbg(&intf->dev, "TX RF gain val=%d->%d\n",
388238106Sdes			dev->tx_rf_gain->cur.val, dev->tx_rf_gain->val);
389238106Sdes
390238106Sdes		u8tmp = (dev->tx_rf_gain->val) ? 1 : 0;
391238106Sdes		ret = hackrf_ctrl_msg(dev, CMD_AMP_ENABLE, u8tmp, 0, NULL, 0);
392238106Sdes		if (ret)
393238106Sdes			goto err;
394238106Sdes		set_bit(RX_RF_GAIN, &dev->flags);
395238106Sdes	}
396238106Sdes
397238106Sdes	/* RX LNA gain */
398238106Sdes	if (rx && test_and_clear_bit(RX_LNA_GAIN, &dev->flags)) {
399238106Sdes		dev_dbg(dev->dev, "RX LNA gain val=%d->%d\n",
400238106Sdes			dev->rx_lna_gain->cur.val, dev->rx_lna_gain->val);
401238106Sdes
402238106Sdes		ret = hackrf_ctrl_msg(dev, CMD_SET_LNA_GAIN, 0,
403238106Sdes				      dev->rx_lna_gain->val, &u8tmp, 1);
404238106Sdes		if (ret)
405238106Sdes			goto err;
406238106Sdes	}
407238106Sdes
408238106Sdes	/* RX IF gain */
409238106Sdes	if (rx && test_and_clear_bit(RX_IF_GAIN, &dev->flags)) {
410238106Sdes		dev_dbg(&intf->dev, "IF gain val=%d->%d\n",
411238106Sdes			dev->rx_if_gain->cur.val, dev->rx_if_gain->val);
412238106Sdes
413238106Sdes		ret = hackrf_ctrl_msg(dev, CMD_SET_VGA_GAIN, 0,
414238106Sdes				      dev->rx_if_gain->val, &u8tmp, 1);
415238106Sdes		if (ret)
416238106Sdes			goto err;
417238106Sdes	}
418238106Sdes
419238106Sdes	/* TX LNA gain */
420238106Sdes	if (tx && test_and_clear_bit(TX_LNA_GAIN, &dev->flags)) {
421238106Sdes		dev_dbg(&intf->dev, "TX LNA gain val=%d->%d\n",
422238106Sdes			dev->tx_lna_gain->cur.val, dev->tx_lna_gain->val);
423238106Sdes
424238106Sdes		ret = hackrf_ctrl_msg(dev, CMD_SET_TXVGA_GAIN, 0,
425238106Sdes				      dev->tx_lna_gain->val, &u8tmp, 1);
426238106Sdes		if (ret)
427238106Sdes			goto err;
428238106Sdes	}
429238106Sdes
430238106Sdes	return 0;
431238106Sdeserr:
432238106Sdes	dev_dbg(&intf->dev, "failed=%d\n", ret);
433238106Sdes	return ret;
434238106Sdes}
435238106Sdes
436238106Sdes/* Private functions */
437238106Sdesstatic struct hackrf_buffer *hackrf_get_next_buffer(struct hackrf_dev *dev,
438238106Sdes						    struct list_head *buffer_list)
439238106Sdes{
440238106Sdes	unsigned long flags;
441238106Sdes	struct hackrf_buffer *buffer = NULL;
442238106Sdes
443238106Sdes	spin_lock_irqsave(&dev->buffer_list_lock, flags);
444238106Sdes	if (list_empty(buffer_list))
445238106Sdes		goto leave;
446238106Sdes
447238106Sdes	buffer = list_entry(buffer_list->next, struct hackrf_buffer, list);
448238106Sdes	list_del(&buffer->list);
449238106Sdesleave:
450238106Sdes	spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
451238106Sdes	return buffer;
452238106Sdes}
453238106Sdes
454238106Sdesstatic void hackrf_copy_stream(struct hackrf_dev *dev, void *dst, void *src,
455238106Sdes			       unsigned int src_len)
456238106Sdes{
457238106Sdes	memcpy(dst, src, src_len);
458238106Sdes
459238106Sdes	/* calculate sample rate and output it in 10 seconds intervals */
460238106Sdes	if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
461238106Sdes		#define MSECS 10000UL
462238106Sdes		unsigned int msecs = jiffies_to_msecs(jiffies -
463238106Sdes				dev->jiffies_next + msecs_to_jiffies(MSECS));
464238106Sdes		unsigned int samples = dev->sample - dev->sample_measured;
465238106Sdes
466238106Sdes		dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
467238106Sdes		dev->sample_measured = dev->sample;
468238106Sdes		dev_dbg(dev->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n",
469238106Sdes				src_len, samples, msecs,
470238106Sdes				samples * 1000UL / msecs);
471238106Sdes	}
472238106Sdes
473238106Sdes	/* total number of samples */
474238106Sdes	dev->sample += src_len / 2;
475238106Sdes}
476238106Sdes
477238106Sdes/*
478238106Sdes * This gets called for the bulk stream pipe. This is done in interrupt
479238106Sdes * time, so it has to be fast, not crash, and not stall. Neat.
480238106Sdes */
481238106Sdesstatic void hackrf_urb_complete_in(struct urb *urb)
482238106Sdes{
483238106Sdes	struct hackrf_dev *dev = urb->context;
484238106Sdes	struct usb_interface *intf = dev->intf;
485238106Sdes	struct hackrf_buffer *buffer;
486238106Sdes	unsigned int len;
487238106Sdes
488238106Sdes	dev_dbg_ratelimited(&intf->dev, "status=%d length=%u/%u\n", urb->status,
489238106Sdes			    urb->actual_length, urb->transfer_buffer_length);
490238106Sdes
491238106Sdes	switch (urb->status) {
492238106Sdes	case 0:             /* success */
493238106Sdes	case -ETIMEDOUT:    /* NAK */
494238106Sdes		break;
495238106Sdes	case -ECONNRESET:   /* kill */
496238106Sdes	case -ENOENT:
497238106Sdes	case -ESHUTDOWN:
498238106Sdes		return;
499238106Sdes	default:            /* error */
500238106Sdes		dev_err_ratelimited(&intf->dev, "URB failed %d\n", urb->status);
501238106Sdes		goto exit_usb_submit_urb;
502238106Sdes	}
503238106Sdes
504238106Sdes	/* get buffer to write */
505238106Sdes	buffer = hackrf_get_next_buffer(dev, &dev->rx_buffer_list);
506238106Sdes	if (unlikely(buffer == NULL)) {
507238106Sdes		dev->vb_full++;
508238106Sdes		dev_notice_ratelimited(&intf->dev,
509238106Sdes				       "buffer is full - %u packets dropped\n",
510238106Sdes				       dev->vb_full);
511238106Sdes		goto exit_usb_submit_urb;
512238106Sdes	}
513238106Sdes
514238106Sdes	len = min_t(unsigned long, vb2_plane_size(&buffer->vb.vb2_buf, 0),
515238106Sdes		    urb->actual_length);
516238106Sdes	hackrf_copy_stream(dev, vb2_plane_vaddr(&buffer->vb.vb2_buf, 0),
517238106Sdes		    urb->transfer_buffer, len);
518269257Sdes	vb2_set_plane_payload(&buffer->vb.vb2_buf, 0, len);
519238106Sdes	buffer->vb.sequence = dev->sequence++;
520238106Sdes	buffer->vb.vb2_buf.timestamp = ktime_get_ns();
521238106Sdes	vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_DONE);
522238106Sdesexit_usb_submit_urb:
523238106Sdes	usb_submit_urb(urb, GFP_ATOMIC);
524269257Sdes}
525238106Sdes
526238106Sdesstatic void hackrf_urb_complete_out(struct urb *urb)
527238106Sdes{
528238106Sdes	struct hackrf_dev *dev = urb->context;
529238106Sdes	struct usb_interface *intf = dev->intf;
530238106Sdes	struct hackrf_buffer *buffer;
531238106Sdes	unsigned int len;
532238106Sdes
533238106Sdes	dev_dbg_ratelimited(&intf->dev, "status=%d length=%u/%u\n", urb->status,
534238106Sdes			    urb->actual_length, urb->transfer_buffer_length);
535238106Sdes
536238106Sdes	switch (urb->status) {
537238106Sdes	case 0:             /* success */
538238106Sdes	case -ETIMEDOUT:    /* NAK */
539238106Sdes		break;
540238106Sdes	case -ECONNRESET:   /* kill */
541238106Sdes	case -ENOENT:
542238106Sdes	case -ESHUTDOWN:
543238106Sdes		return;
544238106Sdes	default:            /* error */
545238106Sdes		dev_err_ratelimited(&intf->dev, "URB failed %d\n", urb->status);
546238106Sdes	}
547238106Sdes
548238106Sdes	/* get buffer to read */
549238106Sdes	buffer = hackrf_get_next_buffer(dev, &dev->tx_buffer_list);
550238106Sdes	if (unlikely(buffer == NULL)) {
551238106Sdes		dev->vb_empty++;
552238106Sdes		dev_notice_ratelimited(&intf->dev,
553238106Sdes				       "buffer is empty - %u packets dropped\n",
554238106Sdes				       dev->vb_empty);
555238106Sdes		urb->actual_length = 0;
556238106Sdes		goto exit_usb_submit_urb;
557238106Sdes	}
558238106Sdes
559238106Sdes	len = min_t(unsigned long, urb->transfer_buffer_length,
560238106Sdes		    vb2_get_plane_payload(&buffer->vb.vb2_buf, 0));
561238106Sdes	hackrf_copy_stream(dev, urb->transfer_buffer,
562238106Sdes			   vb2_plane_vaddr(&buffer->vb.vb2_buf, 0), len);
563238106Sdes	urb->actual_length = len;
564238106Sdes	buffer->vb.sequence = dev->sequence++;
565238106Sdes	buffer->vb.vb2_buf.timestamp = ktime_get_ns();
566238106Sdes	vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_DONE);
567238106Sdesexit_usb_submit_urb:
568238106Sdes	usb_submit_urb(urb, GFP_ATOMIC);
569269257Sdes}
570238106Sdes
571238106Sdesstatic int hackrf_kill_urbs(struct hackrf_dev *dev)
572238106Sdes{
573238106Sdes	int i;
574238106Sdes
575238106Sdes	for (i = dev->urbs_submitted - 1; i >= 0; i--) {
576238106Sdes		dev_dbg(dev->dev, "kill urb=%d\n", i);
577238106Sdes		/* stop the URB */
578238106Sdes		usb_kill_urb(dev->urb_list[i]);
579238106Sdes	}
580238106Sdes	dev->urbs_submitted = 0;
581238106Sdes
582238106Sdes	return 0;
583238106Sdes}
584238106Sdes
585238106Sdesstatic int hackrf_submit_urbs(struct hackrf_dev *dev)
586238106Sdes{
587238106Sdes	int i, ret;
588238106Sdes
589238106Sdes	for (i = 0; i < dev->urbs_initialized; i++) {
590238106Sdes		dev_dbg(dev->dev, "submit urb=%d\n", i);
591238106Sdes		ret = usb_submit_urb(dev->urb_list[i], GFP_KERNEL);
592238106Sdes		if (ret) {
593238106Sdes			dev_err(dev->dev, "Could not submit URB no. %d - get them all back\n",
594238106Sdes					i);
595238106Sdes			hackrf_kill_urbs(dev);
596238106Sdes			return ret;
597238106Sdes		}
598269257Sdes		dev->urbs_submitted++;
599238106Sdes	}
600238106Sdes
601238106Sdes	return 0;
602238106Sdes}
603238106Sdes
604238106Sdesstatic int hackrf_free_stream_bufs(struct hackrf_dev *dev)
605238106Sdes{
606238106Sdes	if (dev->flags & USB_STATE_URB_BUF) {
607238106Sdes		while (dev->buf_num) {
608238106Sdes			dev->buf_num--;
609238106Sdes			dev_dbg(dev->dev, "free buf=%d\n", dev->buf_num);
610238106Sdes			usb_free_coherent(dev->udev, dev->buf_size,
611238106Sdes					  dev->buf_list[dev->buf_num],
612238106Sdes					  dev->dma_addr[dev->buf_num]);
613238106Sdes		}
614238106Sdes	}
615238106Sdes	dev->flags &= ~USB_STATE_URB_BUF;
616238106Sdes
617238106Sdes	return 0;
618238106Sdes}
619238106Sdes
620238106Sdesstatic int hackrf_alloc_stream_bufs(struct hackrf_dev *dev)
621238106Sdes{
622238106Sdes	dev->buf_num = 0;
623238106Sdes	dev->buf_size = BULK_BUFFER_SIZE;
624238106Sdes
625238106Sdes	dev_dbg(dev->dev, "all in all I will use %u bytes for streaming\n",
626238106Sdes			MAX_BULK_BUFS * BULK_BUFFER_SIZE);
627238106Sdes
628238106Sdes	for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
629238106Sdes		dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
630238106Sdes				BULK_BUFFER_SIZE, GFP_KERNEL,
631238106Sdes				&dev->dma_addr[dev->buf_num]);
632238106Sdes		if (!dev->buf_list[dev->buf_num]) {
633238106Sdes			dev_dbg(dev->dev, "alloc buf=%d failed\n",
634238106Sdes					dev->buf_num);
635238106Sdes			hackrf_free_stream_bufs(dev);
636238106Sdes			return -ENOMEM;
637238106Sdes		}
638238106Sdes
639238106Sdes		dev_dbg(dev->dev, "alloc buf=%d %p (dma %llu)\n", dev->buf_num,
640238106Sdes				dev->buf_list[dev->buf_num],
641238106Sdes				(long long)dev->dma_addr[dev->buf_num]);
642238106Sdes		dev->flags |= USB_STATE_URB_BUF;
643238106Sdes	}
644238106Sdes
645238106Sdes	return 0;
646238106Sdes}
647238106Sdes
648238106Sdesstatic int hackrf_free_urbs(struct hackrf_dev *dev)
649238106Sdes{
650269257Sdes	int i;
651238106Sdes
652238106Sdes	hackrf_kill_urbs(dev);
653238106Sdes
654269257Sdes	for (i = dev->urbs_initialized - 1; i >= 0; i--) {
655269257Sdes		if (dev->urb_list[i]) {
656269257Sdes			dev_dbg(dev->dev, "free urb=%d\n", i);
657269257Sdes			/* free the URBs */
658269257Sdes			usb_free_urb(dev->urb_list[i]);
659269257Sdes		}
660238106Sdes	}
661238106Sdes	dev->urbs_initialized = 0;
662238106Sdes
663238106Sdes	return 0;
664238106Sdes}
665238106Sdes
666238106Sdesstatic int hackrf_alloc_urbs(struct hackrf_dev *dev, bool rcv)
667238106Sdes{
668238106Sdes	int i, j;
669238106Sdes	unsigned int pipe;
670238106Sdes	usb_complete_t complete;
671238106Sdes
672238106Sdes	if (rcv) {
673238106Sdes		pipe = usb_rcvbulkpipe(dev->udev, 0x81);
674238106Sdes		complete = &hackrf_urb_complete_in;
675238106Sdes	} else {
676238106Sdes		pipe = usb_sndbulkpipe(dev->udev, 0x02);
677238106Sdes		complete = &hackrf_urb_complete_out;
678238106Sdes	}
679238106Sdes
680238106Sdes	/* allocate the URBs */
681238106Sdes	for (i = 0; i < MAX_BULK_BUFS; i++) {
682238106Sdes		dev_dbg(dev->dev, "alloc urb=%d\n", i);
683285206Sdes		dev->urb_list[i] = usb_alloc_urb(0, GFP_KERNEL);
684285206Sdes		if (!dev->urb_list[i]) {
685285206Sdes			for (j = 0; j < i; j++)
686238106Sdes				usb_free_urb(dev->urb_list[j]);
687238106Sdes			return -ENOMEM;
688238106Sdes		}
689238106Sdes		usb_fill_bulk_urb(dev->urb_list[i],
690238106Sdes				dev->udev,
691238106Sdes				pipe,
692238106Sdes				dev->buf_list[i],
693238106Sdes				BULK_BUFFER_SIZE,
694238106Sdes				complete, dev);
695238106Sdes
696238106Sdes		dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
697238106Sdes		dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
698238106Sdes		dev->urbs_initialized++;
699238106Sdes	}
700238106Sdes
701238106Sdes	return 0;
702238106Sdes}
703238106Sdes
704238106Sdes/* The user yanked out the cable... */
705238106Sdesstatic void hackrf_disconnect(struct usb_interface *intf)
706238106Sdes{
707238106Sdes	struct v4l2_device *v = usb_get_intfdata(intf);
708238106Sdes	struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev);
709238106Sdes
710238106Sdes	dev_dbg(dev->dev, "\n");
711238106Sdes
712238106Sdes	mutex_lock(&dev->vb_queue_lock);
713238106Sdes	mutex_lock(&dev->v4l2_lock);
714238106Sdes	/* No need to keep the urbs around after disconnection */
715238106Sdes	dev->udev = NULL;
716238106Sdes	v4l2_device_disconnect(&dev->v4l2_dev);
717238106Sdes	video_unregister_device(&dev->tx_vdev);
718238106Sdes	video_unregister_device(&dev->rx_vdev);
719269257Sdes	mutex_unlock(&dev->v4l2_lock);
720238106Sdes	mutex_unlock(&dev->vb_queue_lock);
721238106Sdes
722238106Sdes	v4l2_device_put(&dev->v4l2_dev);
723238106Sdes}
724238106Sdes
725238106Sdes/* Videobuf2 operations */
726238106Sdesstatic void hackrf_return_all_buffers(struct vb2_queue *vq,
727238106Sdes				      enum vb2_buffer_state state)
728238106Sdes{
729238106Sdes	struct hackrf_dev *dev = vb2_get_drv_priv(vq);
730238106Sdes	struct usb_interface *intf = dev->intf;
731238106Sdes	struct hackrf_buffer *buffer, *node;
732238106Sdes	struct list_head *buffer_list;
733238106Sdes	unsigned long flags;
734238106Sdes
735238106Sdes	dev_dbg(&intf->dev, "\n");
736238106Sdes
737238106Sdes	if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
738238106Sdes		buffer_list = &dev->rx_buffer_list;
739238106Sdes	else
740238106Sdes		buffer_list = &dev->tx_buffer_list;
741238106Sdes
742238106Sdes	spin_lock_irqsave(&dev->buffer_list_lock, flags);
743238106Sdes	list_for_each_entry_safe(buffer, node, buffer_list, list) {
744238106Sdes		dev_dbg(&intf->dev, "list_for_each_entry_safe\n");
745238106Sdes		vb2_buffer_done(&buffer->vb.vb2_buf, state);
746238106Sdes		list_del(&buffer->list);
747238106Sdes	}
748238106Sdes	spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
749238106Sdes}
750238106Sdes
751238106Sdesstatic int hackrf_queue_setup(struct vb2_queue *vq,
752238106Sdes		unsigned int *nbuffers,
753238106Sdes		unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
754238106Sdes{
755238106Sdes	struct hackrf_dev *dev = vb2_get_drv_priv(vq);
756238106Sdes	unsigned int q_num_bufs = vb2_get_num_buffers(vq);
757
758	dev_dbg(dev->dev, "nbuffers=%d\n", *nbuffers);
759
760	/* Need at least 8 buffers */
761	if (q_num_bufs + *nbuffers < 8)
762		*nbuffers = 8 - q_num_bufs;
763	*nplanes = 1;
764	sizes[0] = PAGE_ALIGN(dev->buffersize);
765
766	dev_dbg(dev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
767	return 0;
768}
769
770static void hackrf_buf_queue(struct vb2_buffer *vb)
771{
772	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
773	struct vb2_queue *vq = vb->vb2_queue;
774	struct hackrf_dev *dev = vb2_get_drv_priv(vq);
775	struct hackrf_buffer *buffer = container_of(vbuf, struct hackrf_buffer, vb);
776	struct list_head *buffer_list;
777	unsigned long flags;
778
779	dev_dbg_ratelimited(&dev->intf->dev, "\n");
780
781	if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
782		buffer_list = &dev->rx_buffer_list;
783	else
784		buffer_list = &dev->tx_buffer_list;
785
786	spin_lock_irqsave(&dev->buffer_list_lock, flags);
787	list_add_tail(&buffer->list, buffer_list);
788	spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
789}
790
791static int hackrf_start_streaming(struct vb2_queue *vq, unsigned int count)
792{
793	struct hackrf_dev *dev = vb2_get_drv_priv(vq);
794	struct usb_interface *intf = dev->intf;
795	int ret;
796	unsigned int mode;
797
798	dev_dbg(&intf->dev, "count=%i\n", count);
799
800	mutex_lock(&dev->v4l2_lock);
801
802	/* Allow only RX or TX, not both same time */
803	if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE) {
804		if (test_bit(TX_ON, &dev->flags)) {
805			ret = -EBUSY;
806			goto err_hackrf_return_all_buffers;
807		}
808
809		mode = 1;
810		set_bit(RX_ON, &dev->flags);
811	} else {
812		if (test_bit(RX_ON, &dev->flags)) {
813			ret = -EBUSY;
814			goto err_hackrf_return_all_buffers;
815		}
816
817		mode = 2;
818		set_bit(TX_ON, &dev->flags);
819	}
820
821	dev->sequence = 0;
822
823	ret = hackrf_alloc_stream_bufs(dev);
824	if (ret)
825		goto err;
826
827	ret = hackrf_alloc_urbs(dev, (mode == 1));
828	if (ret)
829		goto err;
830
831	ret = hackrf_submit_urbs(dev);
832	if (ret)
833		goto err;
834
835	ret = hackrf_set_params(dev);
836	if (ret)
837		goto err;
838
839	/* start hardware streaming */
840	ret = hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, mode, 0, NULL, 0);
841	if (ret)
842		goto err;
843
844	mutex_unlock(&dev->v4l2_lock);
845
846	return 0;
847err:
848	hackrf_kill_urbs(dev);
849	hackrf_free_urbs(dev);
850	hackrf_free_stream_bufs(dev);
851	clear_bit(RX_ON, &dev->flags);
852	clear_bit(TX_ON, &dev->flags);
853err_hackrf_return_all_buffers:
854	hackrf_return_all_buffers(vq, VB2_BUF_STATE_QUEUED);
855	mutex_unlock(&dev->v4l2_lock);
856	dev_dbg(&intf->dev, "failed=%d\n", ret);
857	return ret;
858}
859
860static void hackrf_stop_streaming(struct vb2_queue *vq)
861{
862	struct hackrf_dev *dev = vb2_get_drv_priv(vq);
863	struct usb_interface *intf = dev->intf;
864
865	dev_dbg(&intf->dev, "\n");
866
867	mutex_lock(&dev->v4l2_lock);
868
869	/* stop hardware streaming */
870	hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, 0, 0, NULL, 0);
871
872	hackrf_kill_urbs(dev);
873	hackrf_free_urbs(dev);
874	hackrf_free_stream_bufs(dev);
875
876	hackrf_return_all_buffers(vq, VB2_BUF_STATE_ERROR);
877
878	if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
879		clear_bit(RX_ON, &dev->flags);
880	else
881		clear_bit(TX_ON, &dev->flags);
882
883	mutex_unlock(&dev->v4l2_lock);
884}
885
886static const struct vb2_ops hackrf_vb2_ops = {
887	.queue_setup            = hackrf_queue_setup,
888	.buf_queue              = hackrf_buf_queue,
889	.start_streaming        = hackrf_start_streaming,
890	.stop_streaming         = hackrf_stop_streaming,
891	.wait_prepare           = vb2_ops_wait_prepare,
892	.wait_finish            = vb2_ops_wait_finish,
893};
894
895static int hackrf_querycap(struct file *file, void *fh,
896		struct v4l2_capability *cap)
897{
898	struct hackrf_dev *dev = video_drvdata(file);
899	struct usb_interface *intf = dev->intf;
900
901	dev_dbg(&intf->dev, "\n");
902
903	cap->capabilities = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
904			    V4L2_CAP_SDR_OUTPUT | V4L2_CAP_MODULATOR |
905			    V4L2_CAP_STREAMING | V4L2_CAP_READWRITE |
906			    V4L2_CAP_DEVICE_CAPS;
907	strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
908	strscpy(cap->card, dev->rx_vdev.name, sizeof(cap->card));
909	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
910
911	return 0;
912}
913
914static int hackrf_s_fmt_sdr(struct file *file, void *priv,
915			    struct v4l2_format *f)
916{
917	struct hackrf_dev *dev = video_drvdata(file);
918	struct video_device *vdev = video_devdata(file);
919	struct vb2_queue *q;
920	int i;
921
922	dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
923			(char *)&f->fmt.sdr.pixelformat);
924
925	if (vdev->vfl_dir == VFL_DIR_RX)
926		q = &dev->rx_vb2_queue;
927	else
928		q = &dev->tx_vb2_queue;
929
930	if (vb2_is_busy(q))
931		return -EBUSY;
932
933	for (i = 0; i < NUM_FORMATS; i++) {
934		if (f->fmt.sdr.pixelformat == formats[i].pixelformat) {
935			dev->pixelformat = formats[i].pixelformat;
936			dev->buffersize = formats[i].buffersize;
937			f->fmt.sdr.buffersize = formats[i].buffersize;
938			return 0;
939		}
940	}
941
942	dev->pixelformat = formats[0].pixelformat;
943	dev->buffersize = formats[0].buffersize;
944	f->fmt.sdr.pixelformat = formats[0].pixelformat;
945	f->fmt.sdr.buffersize = formats[0].buffersize;
946
947	return 0;
948}
949
950static int hackrf_g_fmt_sdr(struct file *file, void *priv,
951			    struct v4l2_format *f)
952{
953	struct hackrf_dev *dev = video_drvdata(file);
954
955	dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
956			(char *)&dev->pixelformat);
957
958	f->fmt.sdr.pixelformat = dev->pixelformat;
959	f->fmt.sdr.buffersize = dev->buffersize;
960
961	return 0;
962}
963
964static int hackrf_try_fmt_sdr(struct file *file, void *priv,
965			      struct v4l2_format *f)
966{
967	struct hackrf_dev *dev = video_drvdata(file);
968	int i;
969
970	dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
971			(char *)&f->fmt.sdr.pixelformat);
972
973	for (i = 0; i < NUM_FORMATS; i++) {
974		if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
975			f->fmt.sdr.buffersize = formats[i].buffersize;
976			return 0;
977		}
978	}
979
980	f->fmt.sdr.pixelformat = formats[0].pixelformat;
981	f->fmt.sdr.buffersize = formats[0].buffersize;
982
983	return 0;
984}
985
986static int hackrf_enum_fmt_sdr(struct file *file, void *priv,
987			       struct v4l2_fmtdesc *f)
988{
989	struct hackrf_dev *dev = video_drvdata(file);
990
991	dev_dbg(dev->dev, "index=%d\n", f->index);
992
993	if (f->index >= NUM_FORMATS)
994		return -EINVAL;
995
996	f->pixelformat = formats[f->index].pixelformat;
997
998	return 0;
999}
1000
1001static int hackrf_s_tuner(struct file *file, void *priv,
1002		const struct v4l2_tuner *v)
1003{
1004	struct hackrf_dev *dev = video_drvdata(file);
1005	int ret;
1006
1007	dev_dbg(dev->dev, "index=%d\n", v->index);
1008
1009	if (v->index == 0)
1010		ret = 0;
1011	else if (v->index == 1)
1012		ret = 0;
1013	else
1014		ret = -EINVAL;
1015
1016	return ret;
1017}
1018
1019static int hackrf_g_tuner(struct file *file, void *priv, 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		strscpy(v->name, "HackRF ADC", sizeof(v->name));
1028		v->type = V4L2_TUNER_SDR;
1029		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1030		v->rangelow  = bands_adc_dac[0].rangelow;
1031		v->rangehigh = bands_adc_dac[0].rangehigh;
1032		ret = 0;
1033	} else if (v->index == 1) {
1034		strscpy(v->name, "HackRF RF", sizeof(v->name));
1035		v->type = V4L2_TUNER_RF;
1036		v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1037		v->rangelow  = bands_rx_tx[0].rangelow;
1038		v->rangehigh = bands_rx_tx[0].rangehigh;
1039		ret = 0;
1040	} else {
1041		ret = -EINVAL;
1042	}
1043
1044	return ret;
1045}
1046
1047static int hackrf_s_modulator(struct file *file, void *fh,
1048			      const struct v4l2_modulator *a)
1049{
1050	struct hackrf_dev *dev = video_drvdata(file);
1051
1052	dev_dbg(dev->dev, "index=%d\n", a->index);
1053
1054	return a->index > 1 ? -EINVAL : 0;
1055}
1056
1057static int hackrf_g_modulator(struct file *file, void *fh,
1058			      struct v4l2_modulator *a)
1059{
1060	struct hackrf_dev *dev = video_drvdata(file);
1061	int ret;
1062
1063	dev_dbg(dev->dev, "index=%d\n", a->index);
1064
1065	if (a->index == 0) {
1066		strscpy(a->name, "HackRF DAC", sizeof(a->name));
1067		a->type = V4L2_TUNER_SDR;
1068		a->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1069		a->rangelow  = bands_adc_dac[0].rangelow;
1070		a->rangehigh = bands_adc_dac[0].rangehigh;
1071		ret = 0;
1072	} else if (a->index == 1) {
1073		strscpy(a->name, "HackRF RF", sizeof(a->name));
1074		a->type = V4L2_TUNER_RF;
1075		a->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1076		a->rangelow  = bands_rx_tx[0].rangelow;
1077		a->rangehigh = bands_rx_tx[0].rangehigh;
1078		ret = 0;
1079	} else {
1080		ret = -EINVAL;
1081	}
1082
1083	return ret;
1084}
1085
1086static int hackrf_s_frequency(struct file *file, void *priv,
1087		const struct v4l2_frequency *f)
1088{
1089	struct hackrf_dev *dev = video_drvdata(file);
1090	struct usb_interface *intf = dev->intf;
1091	struct video_device *vdev = video_devdata(file);
1092	int ret;
1093	unsigned int uitmp;
1094
1095	dev_dbg(&intf->dev, "tuner=%d type=%d frequency=%u\n",
1096			f->tuner, f->type, f->frequency);
1097
1098	if (f->tuner == 0) {
1099		uitmp = clamp(f->frequency, bands_adc_dac[0].rangelow,
1100			      bands_adc_dac[0].rangehigh);
1101		if (vdev->vfl_dir == VFL_DIR_RX) {
1102			dev->f_adc = uitmp;
1103			set_bit(RX_ADC_FREQUENCY, &dev->flags);
1104		} else {
1105			dev->f_dac = uitmp;
1106			set_bit(TX_DAC_FREQUENCY, &dev->flags);
1107		}
1108	} else if (f->tuner == 1) {
1109		uitmp = clamp(f->frequency, bands_rx_tx[0].rangelow,
1110			      bands_rx_tx[0].rangehigh);
1111		if (vdev->vfl_dir == VFL_DIR_RX) {
1112			dev->f_rx = uitmp;
1113			set_bit(RX_RF_FREQUENCY, &dev->flags);
1114		} else {
1115			dev->f_tx = uitmp;
1116			set_bit(TX_RF_FREQUENCY, &dev->flags);
1117		}
1118	} else {
1119		ret = -EINVAL;
1120		goto err;
1121	}
1122
1123	ret = hackrf_set_params(dev);
1124	if (ret)
1125		goto err;
1126
1127	return 0;
1128err:
1129	dev_dbg(&intf->dev, "failed=%d\n", ret);
1130	return ret;
1131}
1132
1133static int hackrf_g_frequency(struct file *file, void *priv,
1134		struct v4l2_frequency *f)
1135{
1136	struct hackrf_dev *dev = video_drvdata(file);
1137	struct usb_interface *intf = dev->intf;
1138	struct video_device *vdev = video_devdata(file);
1139	int ret;
1140
1141	dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
1142
1143	if (f->tuner == 0) {
1144		f->type = V4L2_TUNER_SDR;
1145		if (vdev->vfl_dir == VFL_DIR_RX)
1146			f->frequency = dev->f_adc;
1147		else
1148			f->frequency = dev->f_dac;
1149	} else if (f->tuner == 1) {
1150		f->type = V4L2_TUNER_RF;
1151		if (vdev->vfl_dir == VFL_DIR_RX)
1152			f->frequency = dev->f_rx;
1153		else
1154			f->frequency = dev->f_tx;
1155	} else {
1156		ret = -EINVAL;
1157		goto err;
1158	}
1159
1160	return 0;
1161err:
1162	dev_dbg(&intf->dev, "failed=%d\n", ret);
1163	return ret;
1164}
1165
1166static int hackrf_enum_freq_bands(struct file *file, void *priv,
1167		struct v4l2_frequency_band *band)
1168{
1169	struct hackrf_dev *dev = video_drvdata(file);
1170	int ret;
1171
1172	dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n",
1173			band->tuner, band->type, band->index);
1174
1175	if (band->tuner == 0) {
1176		if (band->index >= ARRAY_SIZE(bands_adc_dac)) {
1177			ret = -EINVAL;
1178		} else {
1179			*band = bands_adc_dac[band->index];
1180			ret = 0;
1181		}
1182	} else if (band->tuner == 1) {
1183		if (band->index >= ARRAY_SIZE(bands_rx_tx)) {
1184			ret = -EINVAL;
1185		} else {
1186			*band = bands_rx_tx[band->index];
1187			ret = 0;
1188		}
1189	} else {
1190		ret = -EINVAL;
1191	}
1192
1193	return ret;
1194}
1195
1196static const struct v4l2_ioctl_ops hackrf_ioctl_ops = {
1197	.vidioc_querycap          = hackrf_querycap,
1198
1199	.vidioc_s_fmt_sdr_cap     = hackrf_s_fmt_sdr,
1200	.vidioc_g_fmt_sdr_cap     = hackrf_g_fmt_sdr,
1201	.vidioc_enum_fmt_sdr_cap  = hackrf_enum_fmt_sdr,
1202	.vidioc_try_fmt_sdr_cap   = hackrf_try_fmt_sdr,
1203
1204	.vidioc_s_fmt_sdr_out     = hackrf_s_fmt_sdr,
1205	.vidioc_g_fmt_sdr_out     = hackrf_g_fmt_sdr,
1206	.vidioc_enum_fmt_sdr_out  = hackrf_enum_fmt_sdr,
1207	.vidioc_try_fmt_sdr_out   = hackrf_try_fmt_sdr,
1208
1209	.vidioc_reqbufs           = vb2_ioctl_reqbufs,
1210	.vidioc_create_bufs       = vb2_ioctl_create_bufs,
1211	.vidioc_prepare_buf       = vb2_ioctl_prepare_buf,
1212	.vidioc_querybuf          = vb2_ioctl_querybuf,
1213	.vidioc_qbuf              = vb2_ioctl_qbuf,
1214	.vidioc_dqbuf             = vb2_ioctl_dqbuf,
1215	.vidioc_expbuf            = vb2_ioctl_expbuf,
1216
1217	.vidioc_streamon          = vb2_ioctl_streamon,
1218	.vidioc_streamoff         = vb2_ioctl_streamoff,
1219
1220	.vidioc_s_tuner           = hackrf_s_tuner,
1221	.vidioc_g_tuner           = hackrf_g_tuner,
1222
1223	.vidioc_s_modulator       = hackrf_s_modulator,
1224	.vidioc_g_modulator       = hackrf_g_modulator,
1225
1226	.vidioc_s_frequency       = hackrf_s_frequency,
1227	.vidioc_g_frequency       = hackrf_g_frequency,
1228	.vidioc_enum_freq_bands   = hackrf_enum_freq_bands,
1229
1230	.vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
1231	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1232	.vidioc_log_status        = v4l2_ctrl_log_status,
1233};
1234
1235static const struct v4l2_file_operations hackrf_fops = {
1236	.owner                    = THIS_MODULE,
1237	.open                     = v4l2_fh_open,
1238	.release                  = vb2_fop_release,
1239	.read                     = vb2_fop_read,
1240	.write                    = vb2_fop_write,
1241	.poll                     = vb2_fop_poll,
1242	.mmap                     = vb2_fop_mmap,
1243	.unlocked_ioctl           = video_ioctl2,
1244};
1245
1246static const struct video_device hackrf_template = {
1247	.name                     = "HackRF One",
1248	.release                  = video_device_release_empty,
1249	.fops                     = &hackrf_fops,
1250	.ioctl_ops                = &hackrf_ioctl_ops,
1251};
1252
1253static void hackrf_video_release(struct v4l2_device *v)
1254{
1255	struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev);
1256
1257	dev_dbg(dev->dev, "\n");
1258
1259	v4l2_ctrl_handler_free(&dev->rx_ctrl_handler);
1260	v4l2_ctrl_handler_free(&dev->tx_ctrl_handler);
1261	v4l2_device_unregister(&dev->v4l2_dev);
1262	kfree(dev);
1263}
1264
1265static int hackrf_s_ctrl_rx(struct v4l2_ctrl *ctrl)
1266{
1267	struct hackrf_dev *dev = container_of(ctrl->handler,
1268			struct hackrf_dev, rx_ctrl_handler);
1269	struct usb_interface *intf = dev->intf;
1270	int ret;
1271
1272	switch (ctrl->id) {
1273	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1274	case V4L2_CID_RF_TUNER_BANDWIDTH:
1275		set_bit(RX_BANDWIDTH, &dev->flags);
1276		break;
1277	case  V4L2_CID_RF_TUNER_RF_GAIN:
1278		set_bit(RX_RF_GAIN, &dev->flags);
1279		break;
1280	case  V4L2_CID_RF_TUNER_LNA_GAIN:
1281		set_bit(RX_LNA_GAIN, &dev->flags);
1282		break;
1283	case  V4L2_CID_RF_TUNER_IF_GAIN:
1284		set_bit(RX_IF_GAIN, &dev->flags);
1285		break;
1286	default:
1287		dev_dbg(&intf->dev, "unknown ctrl: id=%d name=%s\n",
1288			ctrl->id, ctrl->name);
1289		ret = -EINVAL;
1290		goto err;
1291	}
1292
1293	ret = hackrf_set_params(dev);
1294	if (ret)
1295		goto err;
1296
1297	return 0;
1298err:
1299	dev_dbg(&intf->dev, "failed=%d\n", ret);
1300	return ret;
1301}
1302
1303static int hackrf_s_ctrl_tx(struct v4l2_ctrl *ctrl)
1304{
1305	struct hackrf_dev *dev = container_of(ctrl->handler,
1306			struct hackrf_dev, tx_ctrl_handler);
1307	struct usb_interface *intf = dev->intf;
1308	int ret;
1309
1310	switch (ctrl->id) {
1311	case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
1312	case V4L2_CID_RF_TUNER_BANDWIDTH:
1313		set_bit(TX_BANDWIDTH, &dev->flags);
1314		break;
1315	case  V4L2_CID_RF_TUNER_LNA_GAIN:
1316		set_bit(TX_LNA_GAIN, &dev->flags);
1317		break;
1318	case  V4L2_CID_RF_TUNER_RF_GAIN:
1319		set_bit(TX_RF_GAIN, &dev->flags);
1320		break;
1321	default:
1322		dev_dbg(&intf->dev, "unknown ctrl: id=%d name=%s\n",
1323			ctrl->id, ctrl->name);
1324		ret = -EINVAL;
1325		goto err;
1326	}
1327
1328	ret = hackrf_set_params(dev);
1329	if (ret)
1330		goto err;
1331
1332	return 0;
1333err:
1334	dev_dbg(&intf->dev, "failed=%d\n", ret);
1335	return ret;
1336}
1337
1338static const struct v4l2_ctrl_ops hackrf_ctrl_ops_rx = {
1339	.s_ctrl = hackrf_s_ctrl_rx,
1340};
1341
1342static const struct v4l2_ctrl_ops hackrf_ctrl_ops_tx = {
1343	.s_ctrl = hackrf_s_ctrl_tx,
1344};
1345
1346static int hackrf_probe(struct usb_interface *intf,
1347		const struct usb_device_id *id)
1348{
1349	struct hackrf_dev *dev;
1350	int ret;
1351	u8 u8tmp, buf[BUF_SIZE];
1352
1353	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1354	if (!dev) {
1355		ret = -ENOMEM;
1356		goto err;
1357	}
1358
1359	mutex_init(&dev->v4l2_lock);
1360	mutex_init(&dev->vb_queue_lock);
1361	spin_lock_init(&dev->buffer_list_lock);
1362	INIT_LIST_HEAD(&dev->rx_buffer_list);
1363	INIT_LIST_HEAD(&dev->tx_buffer_list);
1364	dev->intf = intf;
1365	dev->dev = &intf->dev;
1366	dev->udev = interface_to_usbdev(intf);
1367	dev->pixelformat = formats[0].pixelformat;
1368	dev->buffersize = formats[0].buffersize;
1369	dev->f_adc = bands_adc_dac[0].rangelow;
1370	dev->f_dac = bands_adc_dac[0].rangelow;
1371	dev->f_rx = bands_rx_tx[0].rangelow;
1372	dev->f_tx = bands_rx_tx[0].rangelow;
1373	set_bit(RX_ADC_FREQUENCY, &dev->flags);
1374	set_bit(TX_DAC_FREQUENCY, &dev->flags);
1375	set_bit(RX_RF_FREQUENCY, &dev->flags);
1376	set_bit(TX_RF_FREQUENCY, &dev->flags);
1377
1378	/* Detect device */
1379	ret = hackrf_ctrl_msg(dev, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1);
1380	if (ret == 0)
1381		ret = hackrf_ctrl_msg(dev, CMD_VERSION_STRING_READ, 0, 0,
1382				buf, BUF_SIZE);
1383	if (ret) {
1384		dev_err(dev->dev, "Could not detect board\n");
1385		goto err_kfree;
1386	}
1387
1388	buf[BUF_SIZE - 1] = '\0';
1389	dev_info(dev->dev, "Board ID: %02x\n", u8tmp);
1390	dev_info(dev->dev, "Firmware version: %s\n", buf);
1391
1392	/* Init vb2 queue structure for receiver */
1393	dev->rx_vb2_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1394	dev->rx_vb2_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF |
1395				     VB2_READ;
1396	dev->rx_vb2_queue.ops = &hackrf_vb2_ops;
1397	dev->rx_vb2_queue.mem_ops = &vb2_vmalloc_memops;
1398	dev->rx_vb2_queue.drv_priv = dev;
1399	dev->rx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
1400	dev->rx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1401	ret = vb2_queue_init(&dev->rx_vb2_queue);
1402	if (ret) {
1403		dev_err(dev->dev, "Could not initialize rx vb2 queue\n");
1404		goto err_kfree;
1405	}
1406
1407	/* Init vb2 queue structure for transmitter */
1408	dev->tx_vb2_queue.type = V4L2_BUF_TYPE_SDR_OUTPUT;
1409	dev->tx_vb2_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF |
1410				     VB2_WRITE;
1411	dev->tx_vb2_queue.ops = &hackrf_vb2_ops;
1412	dev->tx_vb2_queue.mem_ops = &vb2_vmalloc_memops;
1413	dev->tx_vb2_queue.drv_priv = dev;
1414	dev->tx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
1415	dev->tx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1416	ret = vb2_queue_init(&dev->tx_vb2_queue);
1417	if (ret) {
1418		dev_err(dev->dev, "Could not initialize tx vb2 queue\n");
1419		goto err_kfree;
1420	}
1421
1422	/* Register controls for receiver */
1423	v4l2_ctrl_handler_init(&dev->rx_ctrl_handler, 5);
1424	dev->rx_bandwidth_auto = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1425		&hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1426		0, 1, 0, 1);
1427	dev->rx_bandwidth = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1428		&hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_BANDWIDTH,
1429		1750000, 28000000, 50000, 1750000);
1430	v4l2_ctrl_auto_cluster(2, &dev->rx_bandwidth_auto, 0, false);
1431	dev->rx_rf_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1432		&hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_RF_GAIN, 0, 12, 12, 0);
1433	dev->rx_lna_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1434		&hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_LNA_GAIN, 0, 40, 8, 0);
1435	dev->rx_if_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
1436		&hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_IF_GAIN, 0, 62, 2, 0);
1437	if (dev->rx_ctrl_handler.error) {
1438		ret = dev->rx_ctrl_handler.error;
1439		dev_err(dev->dev, "Could not initialize controls\n");
1440		goto err_v4l2_ctrl_handler_free_rx;
1441	}
1442	v4l2_ctrl_grab(dev->rx_rf_gain, !hackrf_enable_rf_gain_ctrl);
1443	v4l2_ctrl_handler_setup(&dev->rx_ctrl_handler);
1444
1445	/* Register controls for transmitter */
1446	v4l2_ctrl_handler_init(&dev->tx_ctrl_handler, 4);
1447	dev->tx_bandwidth_auto = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
1448		&hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
1449		0, 1, 0, 1);
1450	dev->tx_bandwidth = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
1451		&hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_BANDWIDTH,
1452		1750000, 28000000, 50000, 1750000);
1453	v4l2_ctrl_auto_cluster(2, &dev->tx_bandwidth_auto, 0, false);
1454	dev->tx_lna_gain = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
1455		&hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_LNA_GAIN, 0, 47, 1, 0);
1456	dev->tx_rf_gain = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
1457		&hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_RF_GAIN, 0, 15, 15, 0);
1458	if (dev->tx_ctrl_handler.error) {
1459		ret = dev->tx_ctrl_handler.error;
1460		dev_err(dev->dev, "Could not initialize controls\n");
1461		goto err_v4l2_ctrl_handler_free_tx;
1462	}
1463	v4l2_ctrl_grab(dev->tx_rf_gain, !hackrf_enable_rf_gain_ctrl);
1464	v4l2_ctrl_handler_setup(&dev->tx_ctrl_handler);
1465
1466	/* Register the v4l2_device structure */
1467	dev->v4l2_dev.release = hackrf_video_release;
1468	ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev);
1469	if (ret) {
1470		dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret);
1471		goto err_v4l2_ctrl_handler_free_tx;
1472	}
1473
1474	/* Init video_device structure for receiver */
1475	dev->rx_vdev = hackrf_template;
1476	dev->rx_vdev.queue = &dev->rx_vb2_queue;
1477	dev->rx_vdev.queue->lock = &dev->vb_queue_lock;
1478	dev->rx_vdev.v4l2_dev = &dev->v4l2_dev;
1479	dev->rx_vdev.ctrl_handler = &dev->rx_ctrl_handler;
1480	dev->rx_vdev.lock = &dev->v4l2_lock;
1481	dev->rx_vdev.vfl_dir = VFL_DIR_RX;
1482	dev->rx_vdev.device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE |
1483				   V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
1484	video_set_drvdata(&dev->rx_vdev, dev);
1485	ret = video_register_device(&dev->rx_vdev, VFL_TYPE_SDR, -1);
1486	if (ret) {
1487		dev_err(dev->dev,
1488			"Failed to register as video device (%d)\n", ret);
1489		goto err_v4l2_device_unregister;
1490	}
1491	dev_info(dev->dev, "Registered as %s\n",
1492		 video_device_node_name(&dev->rx_vdev));
1493
1494	/* Init video_device structure for transmitter */
1495	dev->tx_vdev = hackrf_template;
1496	dev->tx_vdev.queue = &dev->tx_vb2_queue;
1497	dev->tx_vdev.queue->lock = &dev->vb_queue_lock;
1498	dev->tx_vdev.v4l2_dev = &dev->v4l2_dev;
1499	dev->tx_vdev.ctrl_handler = &dev->tx_ctrl_handler;
1500	dev->tx_vdev.lock = &dev->v4l2_lock;
1501	dev->tx_vdev.vfl_dir = VFL_DIR_TX;
1502	dev->tx_vdev.device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE |
1503				   V4L2_CAP_SDR_OUTPUT | V4L2_CAP_MODULATOR;
1504	video_set_drvdata(&dev->tx_vdev, dev);
1505	ret = video_register_device(&dev->tx_vdev, VFL_TYPE_SDR, -1);
1506	if (ret) {
1507		dev_err(dev->dev,
1508			"Failed to register as video device (%d)\n", ret);
1509		goto err_video_unregister_device_rx;
1510	}
1511	dev_info(dev->dev, "Registered as %s\n",
1512		 video_device_node_name(&dev->tx_vdev));
1513
1514	dev_notice(dev->dev, "SDR API is still slightly experimental and functionality changes may follow\n");
1515	return 0;
1516err_video_unregister_device_rx:
1517	video_unregister_device(&dev->rx_vdev);
1518err_v4l2_device_unregister:
1519	v4l2_device_unregister(&dev->v4l2_dev);
1520err_v4l2_ctrl_handler_free_tx:
1521	v4l2_ctrl_handler_free(&dev->tx_ctrl_handler);
1522err_v4l2_ctrl_handler_free_rx:
1523	v4l2_ctrl_handler_free(&dev->rx_ctrl_handler);
1524err_kfree:
1525	kfree(dev);
1526err:
1527	dev_dbg(&intf->dev, "failed=%d\n", ret);
1528	return ret;
1529}
1530
1531/* USB device ID list */
1532static const struct usb_device_id hackrf_id_table[] = {
1533	{ USB_DEVICE(0x1d50, 0x6089) }, /* HackRF One */
1534	{ }
1535};
1536MODULE_DEVICE_TABLE(usb, hackrf_id_table);
1537
1538/* USB subsystem interface */
1539static struct usb_driver hackrf_driver = {
1540	.name                     = KBUILD_MODNAME,
1541	.probe                    = hackrf_probe,
1542	.disconnect               = hackrf_disconnect,
1543	.id_table                 = hackrf_id_table,
1544};
1545
1546module_usb_driver(hackrf_driver);
1547
1548MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1549MODULE_DESCRIPTION("HackRF");
1550MODULE_LICENSE("GPL");
1551