Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1 | /* |
| 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 Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 24 | #include <media/videobuf2-v4l2.h> |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 25 | #include <media/videobuf2-vmalloc.h> |
| 26 | |
| 27 | /* HackRF USB API commands (from HackRF Library) */ |
| 28 | enum { |
| 29 | CMD_SET_TRANSCEIVER_MODE = 0x01, |
| 30 | CMD_SAMPLE_RATE_SET = 0x06, |
| 31 | CMD_BASEBAND_FILTER_BANDWIDTH_SET = 0x07, |
| 32 | CMD_BOARD_ID_READ = 0x0e, |
| 33 | CMD_VERSION_STRING_READ = 0x0f, |
| 34 | CMD_SET_FREQ = 0x10, |
Antti Palosaari | b3ae296 | 2015-10-10 13:51:04 -0300 | [diff] [blame] | 35 | CMD_AMP_ENABLE = 0x11, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 36 | CMD_SET_LNA_GAIN = 0x13, |
| 37 | CMD_SET_VGA_GAIN = 0x14, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 38 | CMD_SET_TXVGA_GAIN = 0x15, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * bEndpointAddress 0x81 EP 1 IN |
| 43 | * Transfer Type Bulk |
| 44 | * wMaxPacketSize 0x0200 1x 512 bytes |
| 45 | */ |
| 46 | #define MAX_BULK_BUFS (6) |
| 47 | #define BULK_BUFFER_SIZE (128 * 512) |
| 48 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 49 | static const struct v4l2_frequency_band bands_adc_dac[] = { |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 50 | { |
| 51 | .tuner = 0, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 52 | .type = V4L2_TUNER_SDR, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 53 | .index = 0, |
| 54 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 55 | .rangelow = 200000, |
| 56 | .rangehigh = 24000000, |
| 57 | }, |
| 58 | }; |
| 59 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 60 | static const struct v4l2_frequency_band bands_rx_tx[] = { |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 61 | { |
| 62 | .tuner = 1, |
| 63 | .type = V4L2_TUNER_RF, |
| 64 | .index = 0, |
| 65 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 66 | .rangelow = 1, |
Mauro Carvalho Chehab | 720b055 | 2014-09-21 20:35:05 -0300 | [diff] [blame] | 67 | .rangehigh = 4294967294LL, /* max u32, hw goes over 7GHz */ |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 68 | }, |
| 69 | }; |
| 70 | |
| 71 | /* stream formats */ |
| 72 | struct hackrf_format { |
| 73 | char *name; |
| 74 | u32 pixelformat; |
| 75 | u32 buffersize; |
| 76 | }; |
| 77 | |
| 78 | /* format descriptions for capture and preview */ |
| 79 | static struct hackrf_format formats[] = { |
| 80 | { |
| 81 | .name = "Complex S8", |
| 82 | .pixelformat = V4L2_SDR_FMT_CS8, |
| 83 | .buffersize = BULK_BUFFER_SIZE, |
| 84 | }, |
| 85 | }; |
| 86 | |
| 87 | static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats); |
| 88 | |
| 89 | /* intermediate buffers with raw data from the USB device */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 90 | struct hackrf_buffer { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 91 | struct vb2_v4l2_buffer vb; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 92 | struct list_head list; |
| 93 | }; |
| 94 | |
| 95 | struct hackrf_dev { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 96 | #define USB_STATE_URB_BUF 1 /* XXX: set manually */ |
| 97 | #define RX_ON 4 |
| 98 | #define TX_ON 5 |
| 99 | #define RX_ADC_FREQUENCY 11 |
| 100 | #define TX_DAC_FREQUENCY 12 |
| 101 | #define RX_BANDWIDTH 13 |
| 102 | #define TX_BANDWIDTH 14 |
| 103 | #define RX_RF_FREQUENCY 15 |
| 104 | #define TX_RF_FREQUENCY 16 |
| 105 | #define RX_RF_GAIN 17 |
| 106 | #define TX_RF_GAIN 18 |
| 107 | #define RX_IF_GAIN 19 |
| 108 | #define RX_LNA_GAIN 20 |
| 109 | #define TX_LNA_GAIN 21 |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 110 | unsigned long flags; |
| 111 | |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 112 | struct usb_interface *intf; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 113 | struct device *dev; |
| 114 | struct usb_device *udev; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 115 | struct video_device rx_vdev; |
| 116 | struct video_device tx_vdev; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 117 | struct v4l2_device v4l2_dev; |
| 118 | |
| 119 | /* videobuf2 queue and queued buffers list */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 120 | struct vb2_queue rx_vb2_queue; |
| 121 | struct vb2_queue tx_vb2_queue; |
| 122 | struct list_head rx_buffer_list; |
| 123 | struct list_head tx_buffer_list; |
| 124 | spinlock_t buffer_list_lock; /* Protects buffer_list */ |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 125 | unsigned sequence; /* Buffer sequence counter */ |
| 126 | unsigned int vb_full; /* vb is full and packets dropped */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 127 | unsigned int vb_empty; /* vb is empty and packets dropped */ |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 128 | |
| 129 | /* Note if taking both locks v4l2_lock must always be locked first! */ |
| 130 | struct mutex v4l2_lock; /* Protects everything else */ |
| 131 | struct mutex vb_queue_lock; /* Protects vb_queue */ |
| 132 | |
| 133 | struct urb *urb_list[MAX_BULK_BUFS]; |
| 134 | int buf_num; |
| 135 | unsigned long buf_size; |
| 136 | u8 *buf_list[MAX_BULK_BUFS]; |
| 137 | dma_addr_t dma_addr[MAX_BULK_BUFS]; |
| 138 | int urbs_initialized; |
| 139 | int urbs_submitted; |
| 140 | |
| 141 | /* USB control message buffer */ |
| 142 | #define BUF_SIZE 24 |
| 143 | u8 buf[BUF_SIZE]; |
| 144 | |
| 145 | /* Current configuration */ |
| 146 | unsigned int f_adc; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 147 | unsigned int f_dac; |
| 148 | unsigned int f_rx; |
| 149 | unsigned int f_tx; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 150 | u32 pixelformat; |
| 151 | u32 buffersize; |
| 152 | |
| 153 | /* Controls */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 154 | struct v4l2_ctrl_handler rx_ctrl_handler; |
| 155 | struct v4l2_ctrl *rx_bandwidth_auto; |
| 156 | struct v4l2_ctrl *rx_bandwidth; |
| 157 | struct v4l2_ctrl *rx_rf_gain; |
| 158 | struct v4l2_ctrl *rx_lna_gain; |
| 159 | struct v4l2_ctrl *rx_if_gain; |
| 160 | struct v4l2_ctrl_handler tx_ctrl_handler; |
| 161 | struct v4l2_ctrl *tx_bandwidth_auto; |
| 162 | struct v4l2_ctrl *tx_bandwidth; |
| 163 | struct v4l2_ctrl *tx_rf_gain; |
| 164 | struct v4l2_ctrl *tx_lna_gain; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 165 | |
| 166 | /* Sample rate calc */ |
| 167 | unsigned long jiffies_next; |
| 168 | unsigned int sample; |
| 169 | unsigned int sample_measured; |
| 170 | }; |
| 171 | |
| 172 | #define hackrf_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \ |
| 173 | char *_direction; \ |
| 174 | if (_t & USB_DIR_IN) \ |
| 175 | _direction = "<<<"; \ |
| 176 | else \ |
| 177 | _direction = ">>>"; \ |
| 178 | dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \ |
| 179 | _t, _r, _v & 0xff, _v >> 8, _i & 0xff, \ |
| 180 | _i >> 8, _l & 0xff, _l >> 8, _direction, _l, _b); \ |
| 181 | } |
| 182 | |
| 183 | /* execute firmware command */ |
| 184 | static int hackrf_ctrl_msg(struct hackrf_dev *dev, u8 request, u16 value, |
| 185 | u16 index, u8 *data, u16 size) |
| 186 | { |
| 187 | int ret; |
| 188 | unsigned int pipe; |
| 189 | u8 requesttype; |
| 190 | |
| 191 | switch (request) { |
| 192 | case CMD_SET_TRANSCEIVER_MODE: |
| 193 | case CMD_SET_FREQ: |
Antti Palosaari | b3ae296 | 2015-10-10 13:51:04 -0300 | [diff] [blame] | 194 | case CMD_AMP_ENABLE: |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 195 | case CMD_SAMPLE_RATE_SET: |
| 196 | case CMD_BASEBAND_FILTER_BANDWIDTH_SET: |
| 197 | pipe = usb_sndctrlpipe(dev->udev, 0); |
| 198 | requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); |
| 199 | break; |
| 200 | case CMD_BOARD_ID_READ: |
| 201 | case CMD_VERSION_STRING_READ: |
| 202 | case CMD_SET_LNA_GAIN: |
| 203 | case CMD_SET_VGA_GAIN: |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 204 | case CMD_SET_TXVGA_GAIN: |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 205 | pipe = usb_rcvctrlpipe(dev->udev, 0); |
| 206 | requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); |
| 207 | break; |
| 208 | default: |
| 209 | dev_err(dev->dev, "Unknown command %02x\n", request); |
| 210 | ret = -EINVAL; |
| 211 | goto err; |
| 212 | } |
| 213 | |
| 214 | /* write request */ |
| 215 | if (!(requesttype & USB_DIR_IN)) |
| 216 | memcpy(dev->buf, data, size); |
| 217 | |
| 218 | ret = usb_control_msg(dev->udev, pipe, request, requesttype, value, |
| 219 | index, dev->buf, size, 1000); |
| 220 | hackrf_dbg_usb_control_msg(dev->dev, request, requesttype, value, |
| 221 | index, dev->buf, size); |
| 222 | if (ret < 0) { |
| 223 | dev_err(dev->dev, "usb_control_msg() failed %d request %02x\n", |
| 224 | ret, request); |
| 225 | goto err; |
| 226 | } |
| 227 | |
| 228 | /* read request */ |
| 229 | if (requesttype & USB_DIR_IN) |
| 230 | memcpy(data, dev->buf, size); |
| 231 | |
| 232 | return 0; |
| 233 | err: |
| 234 | return ret; |
| 235 | } |
| 236 | |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 237 | static int hackrf_set_params(struct hackrf_dev *dev) |
| 238 | { |
| 239 | struct usb_interface *intf = dev->intf; |
| 240 | int ret, i; |
| 241 | u8 buf[8], u8tmp; |
| 242 | unsigned int uitmp, uitmp1, uitmp2; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 243 | const bool rx = test_bit(RX_ON, &dev->flags); |
| 244 | const bool tx = test_bit(TX_ON, &dev->flags); |
| 245 | static const struct { |
| 246 | u32 freq; |
| 247 | } bandwidth_lut[] = { |
| 248 | { 1750000}, /* 1.75 MHz */ |
| 249 | { 2500000}, /* 2.5 MHz */ |
| 250 | { 3500000}, /* 3.5 MHz */ |
| 251 | { 5000000}, /* 5 MHz */ |
| 252 | { 5500000}, /* 5.5 MHz */ |
| 253 | { 6000000}, /* 6 MHz */ |
| 254 | { 7000000}, /* 7 MHz */ |
| 255 | { 8000000}, /* 8 MHz */ |
| 256 | { 9000000}, /* 9 MHz */ |
| 257 | {10000000}, /* 10 MHz */ |
| 258 | {12000000}, /* 12 MHz */ |
| 259 | {14000000}, /* 14 MHz */ |
| 260 | {15000000}, /* 15 MHz */ |
| 261 | {20000000}, /* 20 MHz */ |
| 262 | {24000000}, /* 24 MHz */ |
| 263 | {28000000}, /* 28 MHz */ |
| 264 | }; |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 265 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 266 | if (!rx && !tx) { |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 267 | dev_dbg(&intf->dev, "device is sleeping\n"); |
| 268 | return 0; |
| 269 | } |
| 270 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 271 | /* ADC / DAC frequency */ |
| 272 | if (rx && test_and_clear_bit(RX_ADC_FREQUENCY, &dev->flags)) { |
| 273 | dev_dbg(&intf->dev, "RX ADC frequency=%u Hz\n", dev->f_adc); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 274 | uitmp1 = dev->f_adc; |
| 275 | uitmp2 = 1; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 276 | set_bit(TX_DAC_FREQUENCY, &dev->flags); |
| 277 | } else if (tx && test_and_clear_bit(TX_DAC_FREQUENCY, &dev->flags)) { |
| 278 | dev_dbg(&intf->dev, "TX DAC frequency=%u Hz\n", dev->f_dac); |
| 279 | uitmp1 = dev->f_dac; |
| 280 | uitmp2 = 1; |
| 281 | set_bit(RX_ADC_FREQUENCY, &dev->flags); |
| 282 | } else { |
| 283 | uitmp1 = uitmp2 = 0; |
| 284 | } |
| 285 | if (uitmp1 || uitmp2) { |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 286 | buf[0] = (uitmp1 >> 0) & 0xff; |
| 287 | buf[1] = (uitmp1 >> 8) & 0xff; |
| 288 | buf[2] = (uitmp1 >> 16) & 0xff; |
| 289 | buf[3] = (uitmp1 >> 24) & 0xff; |
| 290 | buf[4] = (uitmp2 >> 0) & 0xff; |
| 291 | buf[5] = (uitmp2 >> 8) & 0xff; |
| 292 | buf[6] = (uitmp2 >> 16) & 0xff; |
| 293 | buf[7] = (uitmp2 >> 24) & 0xff; |
| 294 | ret = hackrf_ctrl_msg(dev, CMD_SAMPLE_RATE_SET, 0, 0, buf, 8); |
| 295 | if (ret) |
| 296 | goto err; |
| 297 | } |
| 298 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 299 | /* bandwidth */ |
| 300 | if (rx && test_and_clear_bit(RX_BANDWIDTH, &dev->flags)) { |
| 301 | if (dev->rx_bandwidth_auto->val == true) |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 302 | uitmp = dev->f_adc; |
| 303 | else |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 304 | uitmp = dev->rx_bandwidth->val; |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 305 | |
| 306 | for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) { |
| 307 | if (uitmp <= bandwidth_lut[i].freq) { |
| 308 | uitmp = bandwidth_lut[i].freq; |
| 309 | break; |
| 310 | } |
| 311 | } |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 312 | dev->rx_bandwidth->val = uitmp; |
| 313 | dev->rx_bandwidth->cur.val = uitmp; |
| 314 | dev_dbg(&intf->dev, "RX bandwidth selected=%u\n", uitmp); |
| 315 | set_bit(TX_BANDWIDTH, &dev->flags); |
| 316 | } else if (tx && test_and_clear_bit(TX_BANDWIDTH, &dev->flags)) { |
| 317 | if (dev->tx_bandwidth_auto->val == true) |
| 318 | uitmp = dev->f_dac; |
| 319 | else |
| 320 | uitmp = dev->tx_bandwidth->val; |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 321 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 322 | for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) { |
| 323 | if (uitmp <= bandwidth_lut[i].freq) { |
| 324 | uitmp = bandwidth_lut[i].freq; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | dev->tx_bandwidth->val = uitmp; |
| 329 | dev->tx_bandwidth->cur.val = uitmp; |
| 330 | dev_dbg(&intf->dev, "TX bandwidth selected=%u\n", uitmp); |
| 331 | set_bit(RX_BANDWIDTH, &dev->flags); |
| 332 | } else { |
| 333 | uitmp = 0; |
| 334 | } |
| 335 | if (uitmp) { |
| 336 | uitmp1 = uitmp2 = 0; |
| 337 | uitmp1 |= ((uitmp >> 0) & 0xff) << 0; |
| 338 | uitmp1 |= ((uitmp >> 8) & 0xff) << 8; |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 339 | uitmp2 |= ((uitmp >> 16) & 0xff) << 0; |
| 340 | uitmp2 |= ((uitmp >> 24) & 0xff) << 8; |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 341 | ret = hackrf_ctrl_msg(dev, CMD_BASEBAND_FILTER_BANDWIDTH_SET, |
| 342 | uitmp1, uitmp2, NULL, 0); |
| 343 | if (ret) |
| 344 | goto err; |
| 345 | } |
| 346 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 347 | /* RX / TX RF frequency */ |
| 348 | if (rx && test_and_clear_bit(RX_RF_FREQUENCY, &dev->flags)) { |
| 349 | dev_dbg(&intf->dev, "RX RF frequency=%u Hz\n", dev->f_rx); |
| 350 | uitmp1 = dev->f_rx / 1000000; |
| 351 | uitmp2 = dev->f_rx % 1000000; |
| 352 | set_bit(TX_RF_FREQUENCY, &dev->flags); |
| 353 | } else if (tx && test_and_clear_bit(TX_RF_FREQUENCY, &dev->flags)) { |
| 354 | dev_dbg(&intf->dev, "TX RF frequency=%u Hz\n", dev->f_tx); |
| 355 | uitmp1 = dev->f_tx / 1000000; |
| 356 | uitmp2 = dev->f_tx % 1000000; |
| 357 | set_bit(RX_RF_FREQUENCY, &dev->flags); |
| 358 | } else { |
| 359 | uitmp1 = uitmp2 = 0; |
| 360 | } |
| 361 | if (uitmp1 || uitmp2) { |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 362 | buf[0] = (uitmp1 >> 0) & 0xff; |
| 363 | buf[1] = (uitmp1 >> 8) & 0xff; |
| 364 | buf[2] = (uitmp1 >> 16) & 0xff; |
| 365 | buf[3] = (uitmp1 >> 24) & 0xff; |
| 366 | buf[4] = (uitmp2 >> 0) & 0xff; |
| 367 | buf[5] = (uitmp2 >> 8) & 0xff; |
| 368 | buf[6] = (uitmp2 >> 16) & 0xff; |
| 369 | buf[7] = (uitmp2 >> 24) & 0xff; |
| 370 | ret = hackrf_ctrl_msg(dev, CMD_SET_FREQ, 0, 0, buf, 8); |
| 371 | if (ret) |
| 372 | goto err; |
| 373 | } |
| 374 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 375 | /* RX RF gain */ |
| 376 | if (rx && test_and_clear_bit(RX_RF_GAIN, &dev->flags)) { |
| 377 | dev_dbg(&intf->dev, "RX RF gain val=%d->%d\n", |
| 378 | dev->rx_rf_gain->cur.val, dev->rx_rf_gain->val); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 379 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 380 | u8tmp = (dev->rx_rf_gain->val) ? 1 : 0; |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 381 | ret = hackrf_ctrl_msg(dev, CMD_AMP_ENABLE, u8tmp, 0, NULL, 0); |
| 382 | if (ret) |
| 383 | goto err; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 384 | set_bit(TX_RF_GAIN, &dev->flags); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 385 | } |
| 386 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 387 | /* TX RF gain */ |
| 388 | if (tx && test_and_clear_bit(TX_RF_GAIN, &dev->flags)) { |
| 389 | dev_dbg(&intf->dev, "TX RF gain val=%d->%d\n", |
| 390 | dev->tx_rf_gain->cur.val, dev->tx_rf_gain->val); |
| 391 | |
| 392 | u8tmp = (dev->tx_rf_gain->val) ? 1 : 0; |
| 393 | ret = hackrf_ctrl_msg(dev, CMD_AMP_ENABLE, u8tmp, 0, NULL, 0); |
| 394 | if (ret) |
| 395 | goto err; |
| 396 | set_bit(RX_RF_GAIN, &dev->flags); |
| 397 | } |
| 398 | |
| 399 | /* RX LNA gain */ |
| 400 | if (rx && test_and_clear_bit(RX_LNA_GAIN, &dev->flags)) { |
| 401 | dev_dbg(dev->dev, "RX LNA gain val=%d->%d\n", |
| 402 | dev->rx_lna_gain->cur.val, dev->rx_lna_gain->val); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 403 | |
| 404 | ret = hackrf_ctrl_msg(dev, CMD_SET_LNA_GAIN, 0, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 405 | dev->rx_lna_gain->val, &u8tmp, 1); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 406 | if (ret) |
| 407 | goto err; |
| 408 | } |
| 409 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 410 | /* RX IF gain */ |
| 411 | if (rx && test_and_clear_bit(RX_IF_GAIN, &dev->flags)) { |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 412 | dev_dbg(&intf->dev, "IF gain val=%d->%d\n", |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 413 | dev->rx_if_gain->cur.val, dev->rx_if_gain->val); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 414 | |
| 415 | ret = hackrf_ctrl_msg(dev, CMD_SET_VGA_GAIN, 0, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 416 | dev->rx_if_gain->val, &u8tmp, 1); |
| 417 | if (ret) |
| 418 | goto err; |
| 419 | } |
| 420 | |
| 421 | /* TX LNA gain */ |
| 422 | if (tx && test_and_clear_bit(TX_LNA_GAIN, &dev->flags)) { |
| 423 | dev_dbg(&intf->dev, "TX LNA gain val=%d->%d\n", |
| 424 | dev->tx_lna_gain->cur.val, dev->tx_lna_gain->val); |
| 425 | |
| 426 | ret = hackrf_ctrl_msg(dev, CMD_SET_TXVGA_GAIN, 0, |
| 427 | dev->tx_lna_gain->val, &u8tmp, 1); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 428 | if (ret) |
| 429 | goto err; |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | err: |
| 434 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
| 435 | return ret; |
| 436 | } |
| 437 | |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 438 | /* Private functions */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 439 | static struct hackrf_buffer *hackrf_get_next_buffer(struct hackrf_dev *dev, |
| 440 | struct list_head *buffer_list) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 441 | { |
| 442 | unsigned long flags; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 443 | struct hackrf_buffer *buffer = NULL; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 444 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 445 | spin_lock_irqsave(&dev->buffer_list_lock, flags); |
| 446 | if (list_empty(buffer_list)) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 447 | goto leave; |
| 448 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 449 | buffer = list_entry(buffer_list->next, struct hackrf_buffer, list); |
| 450 | list_del(&buffer->list); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 451 | leave: |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 452 | spin_unlock_irqrestore(&dev->buffer_list_lock, flags); |
| 453 | return buffer; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 454 | } |
| 455 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 456 | static void hackrf_copy_stream(struct hackrf_dev *dev, void *dst, void *src, |
| 457 | unsigned int src_len) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 458 | { |
| 459 | memcpy(dst, src, src_len); |
| 460 | |
| 461 | /* calculate sample rate and output it in 10 seconds intervals */ |
| 462 | if (unlikely(time_is_before_jiffies(dev->jiffies_next))) { |
| 463 | #define MSECS 10000UL |
| 464 | unsigned int msecs = jiffies_to_msecs(jiffies - |
| 465 | dev->jiffies_next + msecs_to_jiffies(MSECS)); |
| 466 | unsigned int samples = dev->sample - dev->sample_measured; |
| 467 | |
| 468 | dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS); |
| 469 | dev->sample_measured = dev->sample; |
| 470 | dev_dbg(dev->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n", |
| 471 | src_len, samples, msecs, |
| 472 | samples * 1000UL / msecs); |
| 473 | } |
| 474 | |
| 475 | /* total number of samples */ |
| 476 | dev->sample += src_len / 2; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* |
| 480 | * This gets called for the bulk stream pipe. This is done in interrupt |
| 481 | * time, so it has to be fast, not crash, and not stall. Neat. |
| 482 | */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 483 | static void hackrf_urb_complete_in(struct urb *urb) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 484 | { |
| 485 | struct hackrf_dev *dev = urb->context; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 486 | struct usb_interface *intf = dev->intf; |
| 487 | struct hackrf_buffer *buffer; |
| 488 | unsigned int len; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 489 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 490 | dev_dbg_ratelimited(&intf->dev, "status=%d length=%u/%u\n", urb->status, |
| 491 | urb->actual_length, urb->transfer_buffer_length); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 492 | |
| 493 | switch (urb->status) { |
| 494 | case 0: /* success */ |
| 495 | case -ETIMEDOUT: /* NAK */ |
| 496 | break; |
| 497 | case -ECONNRESET: /* kill */ |
| 498 | case -ENOENT: |
| 499 | case -ESHUTDOWN: |
| 500 | return; |
| 501 | default: /* error */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 502 | dev_err_ratelimited(&intf->dev, "URB failed %d\n", urb->status); |
| 503 | goto exit_usb_submit_urb; |
| 504 | } |
| 505 | |
| 506 | /* get buffer to write */ |
| 507 | buffer = hackrf_get_next_buffer(dev, &dev->rx_buffer_list); |
| 508 | if (unlikely(buffer == NULL)) { |
| 509 | dev->vb_full++; |
| 510 | dev_notice_ratelimited(&intf->dev, |
| 511 | "buffer is full - %u packets dropped\n", |
| 512 | dev->vb_full); |
| 513 | goto exit_usb_submit_urb; |
| 514 | } |
| 515 | |
| 516 | len = min_t(unsigned long, vb2_plane_size(&buffer->vb.vb2_buf, 0), |
| 517 | urb->actual_length); |
| 518 | hackrf_copy_stream(dev, vb2_plane_vaddr(&buffer->vb.vb2_buf, 0), |
| 519 | urb->transfer_buffer, len); |
| 520 | vb2_set_plane_payload(&buffer->vb.vb2_buf, 0, len); |
| 521 | buffer->vb.sequence = dev->sequence++; |
| 522 | v4l2_get_timestamp(&buffer->vb.timestamp); |
| 523 | vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_DONE); |
| 524 | exit_usb_submit_urb: |
| 525 | usb_submit_urb(urb, GFP_ATOMIC); |
| 526 | } |
| 527 | |
| 528 | static void hackrf_urb_complete_out(struct urb *urb) |
| 529 | { |
| 530 | struct hackrf_dev *dev = urb->context; |
| 531 | struct usb_interface *intf = dev->intf; |
| 532 | struct hackrf_buffer *buffer; |
| 533 | unsigned int len; |
| 534 | |
| 535 | dev_dbg_ratelimited(&intf->dev, "status=%d length=%u/%u\n", urb->status, |
| 536 | urb->actual_length, urb->transfer_buffer_length); |
| 537 | |
| 538 | switch (urb->status) { |
| 539 | case 0: /* success */ |
| 540 | case -ETIMEDOUT: /* NAK */ |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 541 | break; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 542 | case -ECONNRESET: /* kill */ |
| 543 | case -ENOENT: |
| 544 | case -ESHUTDOWN: |
| 545 | return; |
| 546 | default: /* error */ |
| 547 | dev_err_ratelimited(&intf->dev, "URB failed %d\n", urb->status); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 548 | } |
| 549 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 550 | /* get buffer to read */ |
| 551 | buffer = hackrf_get_next_buffer(dev, &dev->tx_buffer_list); |
| 552 | if (unlikely(buffer == NULL)) { |
| 553 | dev->vb_empty++; |
| 554 | dev_notice_ratelimited(&intf->dev, |
| 555 | "buffer is empty - %u packets dropped\n", |
| 556 | dev->vb_empty); |
| 557 | urb->actual_length = 0; |
| 558 | goto exit_usb_submit_urb; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 559 | } |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 560 | |
| 561 | len = min_t(unsigned long, urb->transfer_buffer_length, |
| 562 | vb2_get_plane_payload(&buffer->vb.vb2_buf, 0)); |
| 563 | hackrf_copy_stream(dev, urb->transfer_buffer, |
| 564 | vb2_plane_vaddr(&buffer->vb.vb2_buf, 0), len); |
| 565 | urb->actual_length = len; |
| 566 | buffer->vb.sequence = dev->sequence++; |
| 567 | v4l2_get_timestamp(&buffer->vb.timestamp); |
| 568 | vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_DONE); |
| 569 | exit_usb_submit_urb: |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 570 | usb_submit_urb(urb, GFP_ATOMIC); |
| 571 | } |
| 572 | |
| 573 | static int hackrf_kill_urbs(struct hackrf_dev *dev) |
| 574 | { |
| 575 | int i; |
| 576 | |
| 577 | for (i = dev->urbs_submitted - 1; i >= 0; i--) { |
| 578 | dev_dbg(dev->dev, "kill urb=%d\n", i); |
| 579 | /* stop the URB */ |
| 580 | usb_kill_urb(dev->urb_list[i]); |
| 581 | } |
| 582 | dev->urbs_submitted = 0; |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int hackrf_submit_urbs(struct hackrf_dev *dev) |
| 588 | { |
| 589 | int i, ret; |
| 590 | |
| 591 | for (i = 0; i < dev->urbs_initialized; i++) { |
| 592 | dev_dbg(dev->dev, "submit urb=%d\n", i); |
| 593 | ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC); |
| 594 | if (ret) { |
| 595 | dev_err(dev->dev, "Could not submit URB no. %d - get them all back\n", |
| 596 | i); |
| 597 | hackrf_kill_urbs(dev); |
| 598 | return ret; |
| 599 | } |
| 600 | dev->urbs_submitted++; |
| 601 | } |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static int hackrf_free_stream_bufs(struct hackrf_dev *dev) |
| 607 | { |
| 608 | if (dev->flags & USB_STATE_URB_BUF) { |
| 609 | while (dev->buf_num) { |
| 610 | dev->buf_num--; |
| 611 | dev_dbg(dev->dev, "free buf=%d\n", dev->buf_num); |
| 612 | usb_free_coherent(dev->udev, dev->buf_size, |
| 613 | dev->buf_list[dev->buf_num], |
| 614 | dev->dma_addr[dev->buf_num]); |
| 615 | } |
| 616 | } |
| 617 | dev->flags &= ~USB_STATE_URB_BUF; |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int hackrf_alloc_stream_bufs(struct hackrf_dev *dev) |
| 623 | { |
| 624 | dev->buf_num = 0; |
| 625 | dev->buf_size = BULK_BUFFER_SIZE; |
| 626 | |
| 627 | dev_dbg(dev->dev, "all in all I will use %u bytes for streaming\n", |
| 628 | MAX_BULK_BUFS * BULK_BUFFER_SIZE); |
| 629 | |
| 630 | for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) { |
| 631 | dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev, |
| 632 | BULK_BUFFER_SIZE, GFP_ATOMIC, |
| 633 | &dev->dma_addr[dev->buf_num]); |
| 634 | if (!dev->buf_list[dev->buf_num]) { |
| 635 | dev_dbg(dev->dev, "alloc buf=%d failed\n", |
| 636 | dev->buf_num); |
| 637 | hackrf_free_stream_bufs(dev); |
| 638 | return -ENOMEM; |
| 639 | } |
| 640 | |
| 641 | dev_dbg(dev->dev, "alloc buf=%d %p (dma %llu)\n", dev->buf_num, |
| 642 | dev->buf_list[dev->buf_num], |
| 643 | (long long)dev->dma_addr[dev->buf_num]); |
| 644 | dev->flags |= USB_STATE_URB_BUF; |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int hackrf_free_urbs(struct hackrf_dev *dev) |
| 651 | { |
| 652 | int i; |
| 653 | |
| 654 | hackrf_kill_urbs(dev); |
| 655 | |
| 656 | for (i = dev->urbs_initialized - 1; i >= 0; i--) { |
| 657 | if (dev->urb_list[i]) { |
| 658 | dev_dbg(dev->dev, "free urb=%d\n", i); |
| 659 | /* free the URBs */ |
| 660 | usb_free_urb(dev->urb_list[i]); |
| 661 | } |
| 662 | } |
| 663 | dev->urbs_initialized = 0; |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 668 | static int hackrf_alloc_urbs(struct hackrf_dev *dev, bool rcv) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 669 | { |
| 670 | int i, j; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 671 | unsigned int pipe; |
| 672 | usb_complete_t complete; |
| 673 | |
| 674 | if (rcv) { |
| 675 | pipe = usb_rcvbulkpipe(dev->udev, 0x81); |
| 676 | complete = &hackrf_urb_complete_in; |
| 677 | } else { |
| 678 | pipe = usb_sndbulkpipe(dev->udev, 0x02); |
| 679 | complete = &hackrf_urb_complete_out; |
| 680 | } |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 681 | |
| 682 | /* allocate the URBs */ |
| 683 | for (i = 0; i < MAX_BULK_BUFS; i++) { |
| 684 | dev_dbg(dev->dev, "alloc urb=%d\n", i); |
| 685 | dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC); |
| 686 | if (!dev->urb_list[i]) { |
| 687 | dev_dbg(dev->dev, "failed\n"); |
| 688 | for (j = 0; j < i; j++) |
| 689 | usb_free_urb(dev->urb_list[j]); |
| 690 | return -ENOMEM; |
| 691 | } |
| 692 | usb_fill_bulk_urb(dev->urb_list[i], |
| 693 | dev->udev, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 694 | pipe, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 695 | dev->buf_list[i], |
| 696 | BULK_BUFFER_SIZE, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 697 | complete, dev); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 698 | |
| 699 | dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
| 700 | dev->urb_list[i]->transfer_dma = dev->dma_addr[i]; |
| 701 | dev->urbs_initialized++; |
| 702 | } |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 707 | /* The user yanked out the cable... */ |
| 708 | static void hackrf_disconnect(struct usb_interface *intf) |
| 709 | { |
| 710 | struct v4l2_device *v = usb_get_intfdata(intf); |
| 711 | struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev); |
| 712 | |
| 713 | dev_dbg(dev->dev, "\n"); |
| 714 | |
| 715 | mutex_lock(&dev->vb_queue_lock); |
| 716 | mutex_lock(&dev->v4l2_lock); |
| 717 | /* No need to keep the urbs around after disconnection */ |
| 718 | dev->udev = NULL; |
| 719 | v4l2_device_disconnect(&dev->v4l2_dev); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 720 | video_unregister_device(&dev->tx_vdev); |
| 721 | video_unregister_device(&dev->rx_vdev); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 722 | mutex_unlock(&dev->v4l2_lock); |
| 723 | mutex_unlock(&dev->vb_queue_lock); |
| 724 | |
| 725 | v4l2_device_put(&dev->v4l2_dev); |
| 726 | } |
| 727 | |
| 728 | /* Videobuf2 operations */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 729 | static void hackrf_return_all_buffers(struct vb2_queue *vq, |
| 730 | enum vb2_buffer_state state) |
| 731 | { |
| 732 | struct hackrf_dev *dev = vb2_get_drv_priv(vq); |
| 733 | struct usb_interface *intf = dev->intf; |
| 734 | struct hackrf_buffer *buffer, *node; |
| 735 | struct list_head *buffer_list; |
| 736 | unsigned long flags; |
| 737 | |
| 738 | dev_dbg(&intf->dev, "\n"); |
| 739 | |
| 740 | if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE) |
| 741 | buffer_list = &dev->rx_buffer_list; |
| 742 | else |
| 743 | buffer_list = &dev->tx_buffer_list; |
| 744 | |
| 745 | spin_lock_irqsave(&dev->buffer_list_lock, flags); |
| 746 | list_for_each_entry_safe(buffer, node, buffer_list, list) { |
| 747 | dev_dbg(&intf->dev, "list_for_each_entry_safe\n"); |
| 748 | vb2_buffer_done(&buffer->vb.vb2_buf, state); |
| 749 | list_del(&buffer->list); |
| 750 | } |
| 751 | spin_unlock_irqrestore(&dev->buffer_list_lock, flags); |
| 752 | } |
| 753 | |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 754 | static int hackrf_queue_setup(struct vb2_queue *vq, |
Junghak Sung | 33119e8 | 2015-10-06 06:37:46 -0300 | [diff] [blame] | 755 | const void *parg, unsigned int *nbuffers, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 756 | unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) |
| 757 | { |
| 758 | struct hackrf_dev *dev = vb2_get_drv_priv(vq); |
| 759 | |
| 760 | dev_dbg(dev->dev, "nbuffers=%d\n", *nbuffers); |
| 761 | |
| 762 | /* Need at least 8 buffers */ |
| 763 | if (vq->num_buffers + *nbuffers < 8) |
| 764 | *nbuffers = 8 - vq->num_buffers; |
| 765 | *nplanes = 1; |
| 766 | sizes[0] = PAGE_ALIGN(dev->buffersize); |
| 767 | |
| 768 | dev_dbg(dev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]); |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static void hackrf_buf_queue(struct vb2_buffer *vb) |
| 773 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 774 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 775 | struct vb2_queue *vq = vb->vb2_queue; |
| 776 | struct hackrf_dev *dev = vb2_get_drv_priv(vq); |
| 777 | struct hackrf_buffer *buffer = container_of(vbuf, struct hackrf_buffer, vb); |
| 778 | struct list_head *buffer_list; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 779 | unsigned long flags; |
| 780 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 781 | dev_dbg_ratelimited(&dev->intf->dev, "\n"); |
| 782 | |
| 783 | if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE) |
| 784 | buffer_list = &dev->rx_buffer_list; |
| 785 | else |
| 786 | buffer_list = &dev->tx_buffer_list; |
| 787 | |
| 788 | spin_lock_irqsave(&dev->buffer_list_lock, flags); |
| 789 | list_add_tail(&buffer->list, buffer_list); |
| 790 | spin_unlock_irqrestore(&dev->buffer_list_lock, flags); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | static int hackrf_start_streaming(struct vb2_queue *vq, unsigned int count) |
| 794 | { |
| 795 | struct hackrf_dev *dev = vb2_get_drv_priv(vq); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 796 | struct usb_interface *intf = dev->intf; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 797 | int ret; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 798 | unsigned int mode; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 799 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 800 | dev_dbg(&intf->dev, "count=%i\n", count); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 801 | |
| 802 | mutex_lock(&dev->v4l2_lock); |
| 803 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 804 | /* Allow only RX or TX, not both same time */ |
| 805 | if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE) { |
| 806 | if (test_bit(TX_ON, &dev->flags)) { |
| 807 | ret = -EBUSY; |
| 808 | goto err_hackrf_return_all_buffers; |
| 809 | } |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 810 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 811 | mode = 1; |
| 812 | set_bit(RX_ON, &dev->flags); |
| 813 | } else { |
| 814 | if (test_bit(RX_ON, &dev->flags)) { |
| 815 | ret = -EBUSY; |
| 816 | goto err_hackrf_return_all_buffers; |
| 817 | } |
| 818 | |
| 819 | mode = 2; |
| 820 | set_bit(TX_ON, &dev->flags); |
| 821 | } |
| 822 | |
| 823 | dev->sequence = 0; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 824 | |
| 825 | ret = hackrf_alloc_stream_bufs(dev); |
| 826 | if (ret) |
| 827 | goto err; |
| 828 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 829 | ret = hackrf_alloc_urbs(dev, (mode == 1)); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 830 | if (ret) |
| 831 | goto err; |
| 832 | |
| 833 | ret = hackrf_submit_urbs(dev); |
| 834 | if (ret) |
| 835 | goto err; |
| 836 | |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 837 | ret = hackrf_set_params(dev); |
| 838 | if (ret) |
| 839 | goto err; |
| 840 | |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 841 | /* start hardware streaming */ |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 842 | ret = hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, mode, 0, NULL, 0); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 843 | if (ret) |
| 844 | goto err; |
| 845 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 846 | mutex_unlock(&dev->v4l2_lock); |
| 847 | |
| 848 | return 0; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 849 | err: |
| 850 | hackrf_kill_urbs(dev); |
| 851 | hackrf_free_urbs(dev); |
| 852 | hackrf_free_stream_bufs(dev); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 853 | clear_bit(RX_ON, &dev->flags); |
| 854 | clear_bit(TX_ON, &dev->flags); |
| 855 | err_hackrf_return_all_buffers: |
| 856 | hackrf_return_all_buffers(vq, VB2_BUF_STATE_QUEUED); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 857 | mutex_unlock(&dev->v4l2_lock); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 858 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | static void hackrf_stop_streaming(struct vb2_queue *vq) |
| 863 | { |
| 864 | struct hackrf_dev *dev = vb2_get_drv_priv(vq); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 865 | struct usb_interface *intf = dev->intf; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 866 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 867 | dev_dbg(&intf->dev, "\n"); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 868 | |
| 869 | mutex_lock(&dev->v4l2_lock); |
| 870 | |
| 871 | /* stop hardware streaming */ |
| 872 | hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, 0, 0, NULL, 0); |
| 873 | |
| 874 | hackrf_kill_urbs(dev); |
| 875 | hackrf_free_urbs(dev); |
| 876 | hackrf_free_stream_bufs(dev); |
| 877 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 878 | hackrf_return_all_buffers(vq, VB2_BUF_STATE_ERROR); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 879 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 880 | if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE) |
| 881 | clear_bit(RX_ON, &dev->flags); |
| 882 | else |
| 883 | clear_bit(TX_ON, &dev->flags); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 884 | |
| 885 | mutex_unlock(&dev->v4l2_lock); |
| 886 | } |
| 887 | |
| 888 | static struct vb2_ops hackrf_vb2_ops = { |
| 889 | .queue_setup = hackrf_queue_setup, |
| 890 | .buf_queue = hackrf_buf_queue, |
| 891 | .start_streaming = hackrf_start_streaming, |
| 892 | .stop_streaming = hackrf_stop_streaming, |
| 893 | .wait_prepare = vb2_ops_wait_prepare, |
| 894 | .wait_finish = vb2_ops_wait_finish, |
| 895 | }; |
| 896 | |
| 897 | static int hackrf_querycap(struct file *file, void *fh, |
| 898 | struct v4l2_capability *cap) |
| 899 | { |
| 900 | struct hackrf_dev *dev = video_drvdata(file); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 901 | struct usb_interface *intf = dev->intf; |
| 902 | struct video_device *vdev = video_devdata(file); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 903 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 904 | dev_dbg(&intf->dev, "\n"); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 905 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 906 | if (vdev->vfl_dir == VFL_DIR_RX) |
| 907 | cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER | |
| 908 | V4L2_CAP_STREAMING | V4L2_CAP_READWRITE; |
| 909 | |
| 910 | else |
| 911 | cap->device_caps = V4L2_CAP_SDR_OUTPUT | V4L2_CAP_MODULATOR | |
| 912 | V4L2_CAP_STREAMING | V4L2_CAP_READWRITE; |
| 913 | |
| 914 | cap->capabilities = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER | |
| 915 | V4L2_CAP_SDR_OUTPUT | V4L2_CAP_MODULATOR | |
| 916 | V4L2_CAP_STREAMING | V4L2_CAP_READWRITE | |
| 917 | V4L2_CAP_DEVICE_CAPS; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 918 | strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 919 | strlcpy(cap->card, dev->rx_vdev.name, sizeof(cap->card)); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 920 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 925 | static int hackrf_s_fmt_sdr(struct file *file, void *priv, |
| 926 | struct v4l2_format *f) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 927 | { |
| 928 | struct hackrf_dev *dev = video_drvdata(file); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 929 | struct video_device *vdev = video_devdata(file); |
| 930 | struct vb2_queue *q; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 931 | int i; |
| 932 | |
| 933 | dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n", |
| 934 | (char *)&f->fmt.sdr.pixelformat); |
| 935 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 936 | if (vdev->vfl_dir == VFL_DIR_RX) |
| 937 | q = &dev->rx_vb2_queue; |
| 938 | else |
| 939 | q = &dev->tx_vb2_queue; |
| 940 | |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 941 | if (vb2_is_busy(q)) |
| 942 | return -EBUSY; |
| 943 | |
| 944 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
| 945 | for (i = 0; i < NUM_FORMATS; i++) { |
| 946 | if (f->fmt.sdr.pixelformat == formats[i].pixelformat) { |
| 947 | dev->pixelformat = formats[i].pixelformat; |
| 948 | dev->buffersize = formats[i].buffersize; |
| 949 | f->fmt.sdr.buffersize = formats[i].buffersize; |
| 950 | return 0; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | dev->pixelformat = formats[0].pixelformat; |
| 955 | dev->buffersize = formats[0].buffersize; |
| 956 | f->fmt.sdr.pixelformat = formats[0].pixelformat; |
| 957 | f->fmt.sdr.buffersize = formats[0].buffersize; |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 962 | static int hackrf_g_fmt_sdr(struct file *file, void *priv, |
| 963 | struct v4l2_format *f) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 964 | { |
| 965 | struct hackrf_dev *dev = video_drvdata(file); |
| 966 | |
| 967 | dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n", |
| 968 | (char *)&dev->pixelformat); |
| 969 | |
| 970 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
| 971 | f->fmt.sdr.pixelformat = dev->pixelformat; |
| 972 | f->fmt.sdr.buffersize = dev->buffersize; |
| 973 | |
| 974 | return 0; |
| 975 | } |
| 976 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 977 | static int hackrf_try_fmt_sdr(struct file *file, void *priv, |
| 978 | struct v4l2_format *f) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 979 | { |
| 980 | struct hackrf_dev *dev = video_drvdata(file); |
| 981 | int i; |
| 982 | |
| 983 | dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n", |
| 984 | (char *)&f->fmt.sdr.pixelformat); |
| 985 | |
| 986 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
| 987 | for (i = 0; i < NUM_FORMATS; i++) { |
| 988 | if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { |
| 989 | f->fmt.sdr.buffersize = formats[i].buffersize; |
| 990 | return 0; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | f->fmt.sdr.pixelformat = formats[0].pixelformat; |
| 995 | f->fmt.sdr.buffersize = formats[0].buffersize; |
| 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1000 | static int hackrf_enum_fmt_sdr(struct file *file, void *priv, |
| 1001 | struct v4l2_fmtdesc *f) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1002 | { |
| 1003 | struct hackrf_dev *dev = video_drvdata(file); |
| 1004 | |
| 1005 | dev_dbg(dev->dev, "index=%d\n", f->index); |
| 1006 | |
| 1007 | if (f->index >= NUM_FORMATS) |
| 1008 | return -EINVAL; |
| 1009 | |
| 1010 | strlcpy(f->description, formats[f->index].name, sizeof(f->description)); |
| 1011 | f->pixelformat = formats[f->index].pixelformat; |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static int hackrf_s_tuner(struct file *file, void *priv, |
| 1017 | const struct v4l2_tuner *v) |
| 1018 | { |
| 1019 | struct hackrf_dev *dev = video_drvdata(file); |
| 1020 | int ret; |
| 1021 | |
| 1022 | dev_dbg(dev->dev, "index=%d\n", v->index); |
| 1023 | |
| 1024 | if (v->index == 0) |
| 1025 | ret = 0; |
| 1026 | else if (v->index == 1) |
| 1027 | ret = 0; |
| 1028 | else |
| 1029 | ret = -EINVAL; |
| 1030 | |
| 1031 | return ret; |
| 1032 | } |
| 1033 | |
| 1034 | static int hackrf_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v) |
| 1035 | { |
| 1036 | struct hackrf_dev *dev = video_drvdata(file); |
| 1037 | int ret; |
| 1038 | |
| 1039 | dev_dbg(dev->dev, "index=%d\n", v->index); |
| 1040 | |
| 1041 | if (v->index == 0) { |
| 1042 | strlcpy(v->name, "HackRF ADC", sizeof(v->name)); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1043 | v->type = V4L2_TUNER_SDR; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1044 | v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1045 | v->rangelow = bands_adc_dac[0].rangelow; |
| 1046 | v->rangehigh = bands_adc_dac[0].rangehigh; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1047 | ret = 0; |
| 1048 | } else if (v->index == 1) { |
| 1049 | strlcpy(v->name, "HackRF RF", sizeof(v->name)); |
| 1050 | v->type = V4L2_TUNER_RF; |
| 1051 | v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1052 | v->rangelow = bands_rx_tx[0].rangelow; |
| 1053 | v->rangehigh = bands_rx_tx[0].rangehigh; |
| 1054 | ret = 0; |
| 1055 | } else { |
| 1056 | ret = -EINVAL; |
| 1057 | } |
| 1058 | |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | static int hackrf_s_modulator(struct file *file, void *fh, |
| 1063 | const struct v4l2_modulator *a) |
| 1064 | { |
| 1065 | struct hackrf_dev *dev = video_drvdata(file); |
| 1066 | |
| 1067 | dev_dbg(dev->dev, "index=%d\n", a->index); |
| 1068 | |
| 1069 | return a->index > 1 ? -EINVAL : 0; |
| 1070 | } |
| 1071 | |
| 1072 | static int hackrf_g_modulator(struct file *file, void *fh, |
| 1073 | struct v4l2_modulator *a) |
| 1074 | { |
| 1075 | struct hackrf_dev *dev = video_drvdata(file); |
| 1076 | int ret; |
| 1077 | |
| 1078 | dev_dbg(dev->dev, "index=%d\n", a->index); |
| 1079 | |
| 1080 | if (a->index == 0) { |
| 1081 | strlcpy(a->name, "HackRF DAC", sizeof(a->name)); |
| 1082 | a->type = V4L2_TUNER_SDR; |
| 1083 | a->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; |
| 1084 | a->rangelow = bands_adc_dac[0].rangelow; |
| 1085 | a->rangehigh = bands_adc_dac[0].rangehigh; |
| 1086 | ret = 0; |
| 1087 | } else if (a->index == 1) { |
| 1088 | strlcpy(a->name, "HackRF RF", sizeof(a->name)); |
| 1089 | a->type = V4L2_TUNER_RF; |
| 1090 | a->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; |
| 1091 | a->rangelow = bands_rx_tx[0].rangelow; |
| 1092 | a->rangehigh = bands_rx_tx[0].rangehigh; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1093 | ret = 0; |
| 1094 | } else { |
| 1095 | ret = -EINVAL; |
| 1096 | } |
| 1097 | |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
| 1101 | static int hackrf_s_frequency(struct file *file, void *priv, |
| 1102 | const struct v4l2_frequency *f) |
| 1103 | { |
| 1104 | struct hackrf_dev *dev = video_drvdata(file); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1105 | struct usb_interface *intf = dev->intf; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1106 | struct video_device *vdev = video_devdata(file); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1107 | int ret; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1108 | unsigned int uitmp; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1109 | |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1110 | dev_dbg(&intf->dev, "tuner=%d type=%d frequency=%u\n", |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1111 | f->tuner, f->type, f->frequency); |
| 1112 | |
| 1113 | if (f->tuner == 0) { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1114 | uitmp = clamp(f->frequency, bands_adc_dac[0].rangelow, |
| 1115 | bands_adc_dac[0].rangehigh); |
| 1116 | if (vdev->vfl_dir == VFL_DIR_RX) { |
| 1117 | dev->f_adc = uitmp; |
| 1118 | set_bit(RX_ADC_FREQUENCY, &dev->flags); |
| 1119 | } else { |
| 1120 | dev->f_dac = uitmp; |
| 1121 | set_bit(TX_DAC_FREQUENCY, &dev->flags); |
| 1122 | } |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1123 | } else if (f->tuner == 1) { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1124 | uitmp = clamp(f->frequency, bands_rx_tx[0].rangelow, |
| 1125 | bands_rx_tx[0].rangehigh); |
| 1126 | if (vdev->vfl_dir == VFL_DIR_RX) { |
| 1127 | dev->f_rx = uitmp; |
| 1128 | set_bit(RX_RF_FREQUENCY, &dev->flags); |
| 1129 | } else { |
| 1130 | dev->f_tx = uitmp; |
| 1131 | set_bit(TX_RF_FREQUENCY, &dev->flags); |
| 1132 | } |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1133 | } else { |
| 1134 | ret = -EINVAL; |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1135 | goto err; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1136 | } |
| 1137 | |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1138 | ret = hackrf_set_params(dev); |
| 1139 | if (ret) |
| 1140 | goto err; |
| 1141 | |
| 1142 | return 0; |
| 1143 | err: |
| 1144 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1145 | return ret; |
| 1146 | } |
| 1147 | |
| 1148 | static int hackrf_g_frequency(struct file *file, void *priv, |
| 1149 | struct v4l2_frequency *f) |
| 1150 | { |
| 1151 | struct hackrf_dev *dev = video_drvdata(file); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1152 | struct usb_interface *intf = dev->intf; |
| 1153 | struct video_device *vdev = video_devdata(file); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1154 | int ret; |
| 1155 | |
| 1156 | dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type); |
| 1157 | |
| 1158 | if (f->tuner == 0) { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1159 | f->type = V4L2_TUNER_SDR; |
| 1160 | if (vdev->vfl_dir == VFL_DIR_RX) |
| 1161 | f->frequency = dev->f_adc; |
| 1162 | else |
| 1163 | f->frequency = dev->f_dac; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1164 | } else if (f->tuner == 1) { |
| 1165 | f->type = V4L2_TUNER_RF; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1166 | if (vdev->vfl_dir == VFL_DIR_RX) |
| 1167 | f->frequency = dev->f_rx; |
| 1168 | else |
| 1169 | f->frequency = dev->f_tx; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1170 | } else { |
| 1171 | ret = -EINVAL; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1172 | goto err; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1173 | } |
| 1174 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1175 | return 0; |
| 1176 | err: |
| 1177 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1178 | return ret; |
| 1179 | } |
| 1180 | |
| 1181 | static int hackrf_enum_freq_bands(struct file *file, void *priv, |
| 1182 | struct v4l2_frequency_band *band) |
| 1183 | { |
| 1184 | struct hackrf_dev *dev = video_drvdata(file); |
| 1185 | int ret; |
| 1186 | |
| 1187 | dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n", |
| 1188 | band->tuner, band->type, band->index); |
| 1189 | |
| 1190 | if (band->tuner == 0) { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1191 | if (band->index >= ARRAY_SIZE(bands_adc_dac)) { |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1192 | ret = -EINVAL; |
| 1193 | } else { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1194 | *band = bands_adc_dac[band->index]; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1195 | ret = 0; |
| 1196 | } |
| 1197 | } else if (band->tuner == 1) { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1198 | if (band->index >= ARRAY_SIZE(bands_rx_tx)) { |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1199 | ret = -EINVAL; |
| 1200 | } else { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1201 | *band = bands_rx_tx[band->index]; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1202 | ret = 0; |
| 1203 | } |
| 1204 | } else { |
| 1205 | ret = -EINVAL; |
| 1206 | } |
| 1207 | |
| 1208 | return ret; |
| 1209 | } |
| 1210 | |
| 1211 | static const struct v4l2_ioctl_ops hackrf_ioctl_ops = { |
| 1212 | .vidioc_querycap = hackrf_querycap, |
| 1213 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1214 | .vidioc_s_fmt_sdr_cap = hackrf_s_fmt_sdr, |
| 1215 | .vidioc_g_fmt_sdr_cap = hackrf_g_fmt_sdr, |
| 1216 | .vidioc_enum_fmt_sdr_cap = hackrf_enum_fmt_sdr, |
| 1217 | .vidioc_try_fmt_sdr_cap = hackrf_try_fmt_sdr, |
| 1218 | |
| 1219 | .vidioc_s_fmt_sdr_out = hackrf_s_fmt_sdr, |
| 1220 | .vidioc_g_fmt_sdr_out = hackrf_g_fmt_sdr, |
| 1221 | .vidioc_enum_fmt_sdr_out = hackrf_enum_fmt_sdr, |
| 1222 | .vidioc_try_fmt_sdr_out = hackrf_try_fmt_sdr, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1223 | |
| 1224 | .vidioc_reqbufs = vb2_ioctl_reqbufs, |
| 1225 | .vidioc_create_bufs = vb2_ioctl_create_bufs, |
| 1226 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, |
| 1227 | .vidioc_querybuf = vb2_ioctl_querybuf, |
| 1228 | .vidioc_qbuf = vb2_ioctl_qbuf, |
| 1229 | .vidioc_dqbuf = vb2_ioctl_dqbuf, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1230 | .vidioc_expbuf = vb2_ioctl_expbuf, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1231 | |
| 1232 | .vidioc_streamon = vb2_ioctl_streamon, |
| 1233 | .vidioc_streamoff = vb2_ioctl_streamoff, |
| 1234 | |
| 1235 | .vidioc_s_tuner = hackrf_s_tuner, |
| 1236 | .vidioc_g_tuner = hackrf_g_tuner, |
| 1237 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1238 | .vidioc_s_modulator = hackrf_s_modulator, |
| 1239 | .vidioc_g_modulator = hackrf_g_modulator, |
| 1240 | |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1241 | .vidioc_s_frequency = hackrf_s_frequency, |
| 1242 | .vidioc_g_frequency = hackrf_g_frequency, |
| 1243 | .vidioc_enum_freq_bands = hackrf_enum_freq_bands, |
| 1244 | |
| 1245 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 1246 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
| 1247 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 1248 | }; |
| 1249 | |
| 1250 | static const struct v4l2_file_operations hackrf_fops = { |
| 1251 | .owner = THIS_MODULE, |
| 1252 | .open = v4l2_fh_open, |
| 1253 | .release = vb2_fop_release, |
| 1254 | .read = vb2_fop_read, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1255 | .write = vb2_fop_write, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1256 | .poll = vb2_fop_poll, |
| 1257 | .mmap = vb2_fop_mmap, |
| 1258 | .unlocked_ioctl = video_ioctl2, |
| 1259 | }; |
| 1260 | |
| 1261 | static struct video_device hackrf_template = { |
| 1262 | .name = "HackRF One", |
| 1263 | .release = video_device_release_empty, |
| 1264 | .fops = &hackrf_fops, |
| 1265 | .ioctl_ops = &hackrf_ioctl_ops, |
| 1266 | }; |
| 1267 | |
| 1268 | static void hackrf_video_release(struct v4l2_device *v) |
| 1269 | { |
| 1270 | struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev); |
| 1271 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1272 | dev_dbg(dev->dev, "\n"); |
| 1273 | |
| 1274 | v4l2_ctrl_handler_free(&dev->rx_ctrl_handler); |
| 1275 | v4l2_ctrl_handler_free(&dev->tx_ctrl_handler); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1276 | v4l2_device_unregister(&dev->v4l2_dev); |
| 1277 | kfree(dev); |
| 1278 | } |
| 1279 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1280 | static int hackrf_s_ctrl_rx(struct v4l2_ctrl *ctrl) |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1281 | { |
| 1282 | struct hackrf_dev *dev = container_of(ctrl->handler, |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1283 | struct hackrf_dev, rx_ctrl_handler); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1284 | struct usb_interface *intf = dev->intf; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1285 | int ret; |
| 1286 | |
| 1287 | switch (ctrl->id) { |
| 1288 | case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: |
| 1289 | case V4L2_CID_RF_TUNER_BANDWIDTH: |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1290 | set_bit(RX_BANDWIDTH, &dev->flags); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1291 | break; |
Antti Palosaari | b3ae296 | 2015-10-10 13:51:04 -0300 | [diff] [blame] | 1292 | case V4L2_CID_RF_TUNER_RF_GAIN: |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1293 | set_bit(RX_RF_GAIN, &dev->flags); |
Antti Palosaari | b3ae296 | 2015-10-10 13:51:04 -0300 | [diff] [blame] | 1294 | break; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1295 | case V4L2_CID_RF_TUNER_LNA_GAIN: |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1296 | set_bit(RX_LNA_GAIN, &dev->flags); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1297 | break; |
| 1298 | case V4L2_CID_RF_TUNER_IF_GAIN: |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1299 | set_bit(RX_IF_GAIN, &dev->flags); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1300 | break; |
| 1301 | default: |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1302 | dev_dbg(&intf->dev, "unknown ctrl: id=%d name=%s\n", |
| 1303 | ctrl->id, ctrl->name); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1304 | ret = -EINVAL; |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1305 | goto err; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1306 | } |
| 1307 | |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1308 | ret = hackrf_set_params(dev); |
| 1309 | if (ret) |
| 1310 | goto err; |
| 1311 | |
| 1312 | return 0; |
| 1313 | err: |
| 1314 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1315 | return ret; |
| 1316 | } |
| 1317 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1318 | static int hackrf_s_ctrl_tx(struct v4l2_ctrl *ctrl) |
| 1319 | { |
| 1320 | struct hackrf_dev *dev = container_of(ctrl->handler, |
| 1321 | struct hackrf_dev, tx_ctrl_handler); |
| 1322 | struct usb_interface *intf = dev->intf; |
| 1323 | int ret; |
| 1324 | |
| 1325 | switch (ctrl->id) { |
| 1326 | case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: |
| 1327 | case V4L2_CID_RF_TUNER_BANDWIDTH: |
| 1328 | set_bit(TX_BANDWIDTH, &dev->flags); |
| 1329 | break; |
| 1330 | case V4L2_CID_RF_TUNER_LNA_GAIN: |
| 1331 | set_bit(TX_LNA_GAIN, &dev->flags); |
| 1332 | break; |
| 1333 | case V4L2_CID_RF_TUNER_RF_GAIN: |
| 1334 | set_bit(TX_RF_GAIN, &dev->flags); |
| 1335 | break; |
| 1336 | default: |
| 1337 | dev_dbg(&intf->dev, "unknown ctrl: id=%d name=%s\n", |
| 1338 | ctrl->id, ctrl->name); |
| 1339 | ret = -EINVAL; |
| 1340 | goto err; |
| 1341 | } |
| 1342 | |
| 1343 | ret = hackrf_set_params(dev); |
| 1344 | if (ret) |
| 1345 | goto err; |
| 1346 | |
| 1347 | return 0; |
| 1348 | err: |
| 1349 | dev_dbg(&intf->dev, "failed=%d\n", ret); |
| 1350 | return ret; |
| 1351 | } |
| 1352 | |
| 1353 | static const struct v4l2_ctrl_ops hackrf_ctrl_ops_rx = { |
| 1354 | .s_ctrl = hackrf_s_ctrl_rx, |
| 1355 | }; |
| 1356 | |
| 1357 | static const struct v4l2_ctrl_ops hackrf_ctrl_ops_tx = { |
| 1358 | .s_ctrl = hackrf_s_ctrl_tx, |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1359 | }; |
| 1360 | |
| 1361 | static int hackrf_probe(struct usb_interface *intf, |
| 1362 | const struct usb_device_id *id) |
| 1363 | { |
| 1364 | struct hackrf_dev *dev; |
| 1365 | int ret; |
| 1366 | u8 u8tmp, buf[BUF_SIZE]; |
| 1367 | |
| 1368 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1369 | if (!dev) { |
| 1370 | ret = -ENOMEM; |
| 1371 | goto err; |
| 1372 | } |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1373 | |
| 1374 | mutex_init(&dev->v4l2_lock); |
| 1375 | mutex_init(&dev->vb_queue_lock); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1376 | spin_lock_init(&dev->buffer_list_lock); |
| 1377 | INIT_LIST_HEAD(&dev->rx_buffer_list); |
| 1378 | INIT_LIST_HEAD(&dev->tx_buffer_list); |
Antti Palosaari | eec20f0 | 2015-10-10 13:51:05 -0300 | [diff] [blame] | 1379 | dev->intf = intf; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1380 | dev->dev = &intf->dev; |
| 1381 | dev->udev = interface_to_usbdev(intf); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1382 | dev->pixelformat = formats[0].pixelformat; |
| 1383 | dev->buffersize = formats[0].buffersize; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1384 | dev->f_adc = bands_adc_dac[0].rangelow; |
| 1385 | dev->f_dac = bands_adc_dac[0].rangelow; |
| 1386 | dev->f_rx = bands_rx_tx[0].rangelow; |
| 1387 | dev->f_tx = bands_rx_tx[0].rangelow; |
| 1388 | set_bit(RX_ADC_FREQUENCY, &dev->flags); |
| 1389 | set_bit(TX_DAC_FREQUENCY, &dev->flags); |
| 1390 | set_bit(RX_RF_FREQUENCY, &dev->flags); |
| 1391 | set_bit(TX_RF_FREQUENCY, &dev->flags); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1392 | |
| 1393 | /* Detect device */ |
| 1394 | ret = hackrf_ctrl_msg(dev, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1); |
| 1395 | if (ret == 0) |
| 1396 | ret = hackrf_ctrl_msg(dev, CMD_VERSION_STRING_READ, 0, 0, |
| 1397 | buf, BUF_SIZE); |
| 1398 | if (ret) { |
| 1399 | dev_err(dev->dev, "Could not detect board\n"); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1400 | goto err_kfree; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | buf[BUF_SIZE - 1] = '\0'; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1404 | dev_info(dev->dev, "Board ID: %02x\n", u8tmp); |
| 1405 | dev_info(dev->dev, "Firmware version: %s\n", buf); |
| 1406 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1407 | /* Init vb2 queue structure for receiver */ |
| 1408 | dev->rx_vb2_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; |
| 1409 | dev->rx_vb2_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | |
| 1410 | VB2_READ; |
| 1411 | dev->rx_vb2_queue.ops = &hackrf_vb2_ops; |
| 1412 | dev->rx_vb2_queue.mem_ops = &vb2_vmalloc_memops; |
| 1413 | dev->rx_vb2_queue.drv_priv = dev; |
| 1414 | dev->rx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer); |
| 1415 | dev->rx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
| 1416 | ret = vb2_queue_init(&dev->rx_vb2_queue); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1417 | if (ret) { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1418 | dev_err(dev->dev, "Could not initialize rx vb2 queue\n"); |
| 1419 | goto err_kfree; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1420 | } |
| 1421 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1422 | /* Init vb2 queue structure for transmitter */ |
| 1423 | dev->tx_vb2_queue.type = V4L2_BUF_TYPE_SDR_OUTPUT; |
| 1424 | dev->tx_vb2_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | |
| 1425 | VB2_WRITE; |
| 1426 | dev->tx_vb2_queue.ops = &hackrf_vb2_ops; |
| 1427 | dev->tx_vb2_queue.mem_ops = &vb2_vmalloc_memops; |
| 1428 | dev->tx_vb2_queue.drv_priv = dev; |
| 1429 | dev->tx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer); |
| 1430 | dev->tx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
| 1431 | ret = vb2_queue_init(&dev->tx_vb2_queue); |
| 1432 | if (ret) { |
| 1433 | dev_err(dev->dev, "Could not initialize tx vb2 queue\n"); |
| 1434 | goto err_kfree; |
| 1435 | } |
| 1436 | |
| 1437 | /* Register controls for receiver */ |
| 1438 | v4l2_ctrl_handler_init(&dev->rx_ctrl_handler, 5); |
| 1439 | dev->rx_bandwidth_auto = v4l2_ctrl_new_std(&dev->rx_ctrl_handler, |
| 1440 | &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, |
| 1441 | 0, 1, 0, 1); |
| 1442 | dev->rx_bandwidth = v4l2_ctrl_new_std(&dev->rx_ctrl_handler, |
| 1443 | &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_BANDWIDTH, |
| 1444 | 1750000, 28000000, 50000, 1750000); |
| 1445 | v4l2_ctrl_auto_cluster(2, &dev->rx_bandwidth_auto, 0, false); |
| 1446 | dev->rx_rf_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler, |
| 1447 | &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_RF_GAIN, 0, 12, 12, 0); |
| 1448 | dev->rx_lna_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler, |
| 1449 | &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_LNA_GAIN, 0, 40, 8, 0); |
| 1450 | dev->rx_if_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler, |
| 1451 | &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_IF_GAIN, 0, 62, 2, 0); |
| 1452 | if (dev->rx_ctrl_handler.error) { |
| 1453 | ret = dev->rx_ctrl_handler.error; |
| 1454 | dev_err(dev->dev, "Could not initialize controls\n"); |
| 1455 | goto err_v4l2_ctrl_handler_free_rx; |
| 1456 | } |
| 1457 | v4l2_ctrl_handler_setup(&dev->rx_ctrl_handler); |
| 1458 | |
| 1459 | /* Register controls for transmitter */ |
| 1460 | v4l2_ctrl_handler_init(&dev->tx_ctrl_handler, 4); |
| 1461 | dev->tx_bandwidth_auto = v4l2_ctrl_new_std(&dev->tx_ctrl_handler, |
| 1462 | &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, |
| 1463 | 0, 1, 0, 1); |
| 1464 | dev->tx_bandwidth = v4l2_ctrl_new_std(&dev->tx_ctrl_handler, |
| 1465 | &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_BANDWIDTH, |
| 1466 | 1750000, 28000000, 50000, 1750000); |
| 1467 | v4l2_ctrl_auto_cluster(2, &dev->tx_bandwidth_auto, 0, false); |
| 1468 | dev->tx_lna_gain = v4l2_ctrl_new_std(&dev->tx_ctrl_handler, |
| 1469 | &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_LNA_GAIN, 0, 47, 1, 0); |
| 1470 | dev->tx_rf_gain = v4l2_ctrl_new_std(&dev->tx_ctrl_handler, |
| 1471 | &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_RF_GAIN, 0, 15, 15, 0); |
| 1472 | if (dev->tx_ctrl_handler.error) { |
| 1473 | ret = dev->tx_ctrl_handler.error; |
| 1474 | dev_err(dev->dev, "Could not initialize controls\n"); |
| 1475 | goto err_v4l2_ctrl_handler_free_tx; |
| 1476 | } |
| 1477 | v4l2_ctrl_handler_setup(&dev->tx_ctrl_handler); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1478 | |
| 1479 | /* Register the v4l2_device structure */ |
| 1480 | dev->v4l2_dev.release = hackrf_video_release; |
| 1481 | ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev); |
| 1482 | if (ret) { |
| 1483 | dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1484 | goto err_v4l2_ctrl_handler_free_tx; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1485 | } |
| 1486 | |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1487 | /* Init video_device structure for receiver */ |
| 1488 | dev->rx_vdev = hackrf_template; |
| 1489 | dev->rx_vdev.queue = &dev->rx_vb2_queue; |
| 1490 | dev->rx_vdev.queue->lock = &dev->vb_queue_lock; |
| 1491 | dev->rx_vdev.v4l2_dev = &dev->v4l2_dev; |
| 1492 | dev->rx_vdev.ctrl_handler = &dev->rx_ctrl_handler; |
| 1493 | dev->rx_vdev.lock = &dev->v4l2_lock; |
| 1494 | dev->rx_vdev.vfl_dir = VFL_DIR_RX; |
| 1495 | video_set_drvdata(&dev->rx_vdev, dev); |
| 1496 | ret = video_register_device(&dev->rx_vdev, VFL_TYPE_SDR, -1); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1497 | if (ret) { |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1498 | dev_err(dev->dev, |
| 1499 | "Failed to register as video device (%d)\n", ret); |
| 1500 | goto err_v4l2_device_unregister; |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1501 | } |
| 1502 | dev_info(dev->dev, "Registered as %s\n", |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1503 | video_device_node_name(&dev->rx_vdev)); |
| 1504 | |
| 1505 | /* Init video_device structure for transmitter */ |
| 1506 | dev->tx_vdev = hackrf_template; |
| 1507 | dev->tx_vdev.queue = &dev->tx_vb2_queue; |
| 1508 | dev->tx_vdev.queue->lock = &dev->vb_queue_lock; |
| 1509 | dev->tx_vdev.v4l2_dev = &dev->v4l2_dev; |
| 1510 | dev->tx_vdev.ctrl_handler = &dev->tx_ctrl_handler; |
| 1511 | dev->tx_vdev.lock = &dev->v4l2_lock; |
| 1512 | dev->tx_vdev.vfl_dir = VFL_DIR_TX; |
| 1513 | video_set_drvdata(&dev->tx_vdev, dev); |
| 1514 | ret = video_register_device(&dev->tx_vdev, VFL_TYPE_SDR, -1); |
| 1515 | if (ret) { |
| 1516 | dev_err(dev->dev, |
| 1517 | "Failed to register as video device (%d)\n", ret); |
| 1518 | goto err_video_unregister_device_rx; |
| 1519 | } |
| 1520 | dev_info(dev->dev, "Registered as %s\n", |
| 1521 | video_device_node_name(&dev->tx_vdev)); |
| 1522 | |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1523 | dev_notice(dev->dev, "SDR API is still slightly experimental and functionality changes may follow\n"); |
| 1524 | return 0; |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1525 | err_video_unregister_device_rx: |
| 1526 | video_unregister_device(&dev->rx_vdev); |
| 1527 | err_v4l2_device_unregister: |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1528 | v4l2_device_unregister(&dev->v4l2_dev); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1529 | err_v4l2_ctrl_handler_free_tx: |
| 1530 | v4l2_ctrl_handler_free(&dev->tx_ctrl_handler); |
| 1531 | err_v4l2_ctrl_handler_free_rx: |
| 1532 | v4l2_ctrl_handler_free(&dev->rx_ctrl_handler); |
| 1533 | err_kfree: |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1534 | kfree(dev); |
Antti Palosaari | 8bc4a9e | 2015-10-10 13:51:06 -0300 | [diff] [blame^] | 1535 | err: |
| 1536 | dev_dbg(dev->dev, "failed=%d\n", ret); |
Antti Palosaari | 969ec1f | 2014-08-23 04:40:01 -0300 | [diff] [blame] | 1537 | return ret; |
| 1538 | } |
| 1539 | |
| 1540 | /* USB device ID list */ |
| 1541 | static struct usb_device_id hackrf_id_table[] = { |
| 1542 | { USB_DEVICE(0x1d50, 0x6089) }, /* HackRF One */ |
| 1543 | { } |
| 1544 | }; |
| 1545 | MODULE_DEVICE_TABLE(usb, hackrf_id_table); |
| 1546 | |
| 1547 | /* USB subsystem interface */ |
| 1548 | static struct usb_driver hackrf_driver = { |
| 1549 | .name = KBUILD_MODNAME, |
| 1550 | .probe = hackrf_probe, |
| 1551 | .disconnect = hackrf_disconnect, |
| 1552 | .id_table = hackrf_id_table, |
| 1553 | }; |
| 1554 | |
| 1555 | module_usb_driver(hackrf_driver); |
| 1556 | |
| 1557 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1558 | MODULE_DESCRIPTION("HackRF"); |
| 1559 | MODULE_LICENSE("GPL"); |