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