blob: 3c556ee306cd33cee0e3e6a3199209540b6a78c9 [file] [log] [blame]
Antti Palosaari634fe502014-06-27 21:30:55 -03001/*
2 * AirSpy SDR driver
3 *
4 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/usb.h>
20#include <media/v4l2-device.h>
21#include <media/v4l2-ioctl.h>
22#include <media/v4l2-ctrls.h>
23#include <media/v4l2-event.h>
Junghak Sung2d700712015-09-22 10:30:30 -030024#include <media/videobuf2-v4l2.h>
Antti Palosaari634fe502014-06-27 21:30:55 -030025#include <media/videobuf2-vmalloc.h>
26
27/* AirSpy USB API commands (from AirSpy Library) */
28enum {
29 CMD_INVALID = 0x00,
30 CMD_RECEIVER_MODE = 0x01,
31 CMD_SI5351C_WRITE = 0x02,
32 CMD_SI5351C_READ = 0x03,
33 CMD_R820T_WRITE = 0x04,
34 CMD_R820T_READ = 0x05,
35 CMD_SPIFLASH_ERASE = 0x06,
36 CMD_SPIFLASH_WRITE = 0x07,
37 CMD_SPIFLASH_READ = 0x08,
38 CMD_BOARD_ID_READ = 0x09,
39 CMD_VERSION_STRING_READ = 0x0a,
40 CMD_BOARD_PARTID_SERIALNO_READ = 0x0b,
41 CMD_SET_SAMPLE_RATE = 0x0c,
42 CMD_SET_FREQ = 0x0d,
43 CMD_SET_LNA_GAIN = 0x0e,
44 CMD_SET_MIXER_GAIN = 0x0f,
45 CMD_SET_VGA_GAIN = 0x10,
46 CMD_SET_LNA_AGC = 0x11,
47 CMD_SET_MIXER_AGC = 0x12,
48 CMD_SET_PACKING = 0x13,
49};
50
51/*
52 * bEndpointAddress 0x81 EP 1 IN
53 * Transfer Type Bulk
54 * wMaxPacketSize 0x0200 1x 512 bytes
55 */
56#define MAX_BULK_BUFS (6)
57#define BULK_BUFFER_SIZE (128 * 512)
58
59static const struct v4l2_frequency_band bands[] = {
60 {
61 .tuner = 0,
62 .type = V4L2_TUNER_ADC,
63 .index = 0,
64 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
65 .rangelow = 20000000,
Antti Palosaarieb83eab2014-07-17 21:15:11 -030066 .rangehigh = 20000000,
Antti Palosaari634fe502014-06-27 21:30:55 -030067 },
68};
69
70static const struct v4l2_frequency_band bands_rf[] = {
71 {
72 .tuner = 1,
73 .type = V4L2_TUNER_RF,
74 .index = 0,
75 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
76 .rangelow = 24000000,
77 .rangehigh = 1750000000,
78 },
79};
80
81/* stream formats */
82struct airspy_format {
83 char *name;
84 u32 pixelformat;
Antti Palosaari1b303e12014-07-18 21:22:08 -030085 u32 buffersize;
Antti Palosaari634fe502014-06-27 21:30:55 -030086};
87
88/* format descriptions for capture and preview */
89static struct airspy_format formats[] = {
90 {
91 .name = "Real U12LE",
Antti Palosaari1b303e12014-07-18 21:22:08 -030092 .pixelformat = V4L2_SDR_FMT_RU12LE,
93 .buffersize = BULK_BUFFER_SIZE,
Antti Palosaari634fe502014-06-27 21:30:55 -030094 },
95};
96
97static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
98
99/* intermediate buffers with raw data from the USB device */
100struct airspy_frame_buf {
Junghak Sung2d700712015-09-22 10:30:30 -0300101 /* common v4l buffer stuff -- must be first */
102 struct vb2_v4l2_buffer vb;
Antti Palosaari634fe502014-06-27 21:30:55 -0300103 struct list_head list;
104};
105
106struct airspy {
Mauro Carvalho Chehabf6f7b582016-02-27 07:51:07 -0300107#define POWER_ON 1
108#define USB_STATE_URB_BUF 2
Antti Palosaari634fe502014-06-27 21:30:55 -0300109 unsigned long flags;
110
Antti Palosaari617123a2014-08-24 19:14:32 -0300111 struct device *dev;
Antti Palosaari634fe502014-06-27 21:30:55 -0300112 struct usb_device *udev;
113 struct video_device vdev;
114 struct v4l2_device v4l2_dev;
115
116 /* videobuf2 queue and queued buffers list */
117 struct vb2_queue vb_queue;
118 struct list_head queued_bufs;
119 spinlock_t queued_bufs_lock; /* Protects queued_bufs */
120 unsigned sequence; /* Buffer sequence counter */
121 unsigned int vb_full; /* vb is full and packets dropped */
122
123 /* Note if taking both locks v4l2_lock must always be locked first! */
124 struct mutex v4l2_lock; /* Protects everything else */
125 struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
126
127 struct urb *urb_list[MAX_BULK_BUFS];
128 int buf_num;
129 unsigned long buf_size;
130 u8 *buf_list[MAX_BULK_BUFS];
131 dma_addr_t dma_addr[MAX_BULK_BUFS];
132 int urbs_initialized;
133 int urbs_submitted;
134
135 /* USB control message buffer */
Antti Palosaariaa0850e2015-10-26 18:58:14 -0200136 #define BUF_SIZE 128
Antti Palosaari634fe502014-06-27 21:30:55 -0300137 u8 buf[BUF_SIZE];
138
139 /* Current configuration */
140 unsigned int f_adc;
141 unsigned int f_rf;
142 u32 pixelformat;
Antti Palosaari1b303e12014-07-18 21:22:08 -0300143 u32 buffersize;
Antti Palosaari634fe502014-06-27 21:30:55 -0300144
145 /* Controls */
146 struct v4l2_ctrl_handler hdl;
147 struct v4l2_ctrl *lna_gain_auto;
148 struct v4l2_ctrl *lna_gain;
149 struct v4l2_ctrl *mixer_gain_auto;
150 struct v4l2_ctrl *mixer_gain;
151 struct v4l2_ctrl *if_gain;
152
153 /* Sample rate calc */
154 unsigned long jiffies_next;
155 unsigned int sample;
156 unsigned int sample_measured;
157};
158
Antti Palosaari617123a2014-08-24 19:14:32 -0300159#define airspy_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
Antti Palosaari634fe502014-06-27 21:30:55 -0300160 char *_direction; \
161 if (_t & USB_DIR_IN) \
162 _direction = "<<<"; \
163 else \
164 _direction = ">>>"; \
Antti Palosaari617123a2014-08-24 19:14:32 -0300165 dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
166 _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \
167 _l & 0xff, _l >> 8, _direction, _l, _b); \
Antti Palosaari634fe502014-06-27 21:30:55 -0300168}
169
170/* execute firmware command */
171static int airspy_ctrl_msg(struct airspy *s, u8 request, u16 value, u16 index,
172 u8 *data, u16 size)
173{
174 int ret;
175 unsigned int pipe;
176 u8 requesttype;
177
178 switch (request) {
179 case CMD_RECEIVER_MODE:
180 case CMD_SET_FREQ:
181 pipe = usb_sndctrlpipe(s->udev, 0);
182 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
183 break;
184 case CMD_BOARD_ID_READ:
185 case CMD_VERSION_STRING_READ:
186 case CMD_BOARD_PARTID_SERIALNO_READ:
187 case CMD_SET_LNA_GAIN:
188 case CMD_SET_MIXER_GAIN:
189 case CMD_SET_VGA_GAIN:
190 case CMD_SET_LNA_AGC:
191 case CMD_SET_MIXER_AGC:
192 pipe = usb_rcvctrlpipe(s->udev, 0);
193 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
194 break;
195 default:
Antti Palosaari617123a2014-08-24 19:14:32 -0300196 dev_err(s->dev, "Unknown command %02x\n", request);
Antti Palosaari634fe502014-06-27 21:30:55 -0300197 ret = -EINVAL;
198 goto err;
199 }
200
201 /* write request */
202 if (!(requesttype & USB_DIR_IN))
203 memcpy(s->buf, data, size);
204
205 ret = usb_control_msg(s->udev, pipe, request, requesttype, value,
206 index, s->buf, size, 1000);
Antti Palosaari617123a2014-08-24 19:14:32 -0300207 airspy_dbg_usb_control_msg(s->dev, request, requesttype, value,
Antti Palosaari634fe502014-06-27 21:30:55 -0300208 index, s->buf, size);
209 if (ret < 0) {
Antti Palosaari617123a2014-08-24 19:14:32 -0300210 dev_err(s->dev, "usb_control_msg() failed %d request %02x\n",
Antti Palosaari634fe502014-06-27 21:30:55 -0300211 ret, request);
212 goto err;
213 }
214
215 /* read request */
216 if (requesttype & USB_DIR_IN)
217 memcpy(data, s->buf, size);
218
219 return 0;
220err:
221 return ret;
222}
223
224/* Private functions */
225static struct airspy_frame_buf *airspy_get_next_fill_buf(struct airspy *s)
226{
Antti Palosaari8880f2c2014-08-24 19:27:43 -0300227 unsigned long flags;
Antti Palosaari634fe502014-06-27 21:30:55 -0300228 struct airspy_frame_buf *buf = NULL;
229
230 spin_lock_irqsave(&s->queued_bufs_lock, flags);
231 if (list_empty(&s->queued_bufs))
232 goto leave;
233
234 buf = list_entry(s->queued_bufs.next,
235 struct airspy_frame_buf, list);
236 list_del(&buf->list);
237leave:
238 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
239 return buf;
240}
241
242static unsigned int airspy_convert_stream(struct airspy *s,
243 void *dst, void *src, unsigned int src_len)
244{
245 unsigned int dst_len;
246
247 if (s->pixelformat == V4L2_SDR_FMT_RU12LE) {
248 memcpy(dst, src, src_len);
249 dst_len = src_len;
250 } else {
251 dst_len = 0;
252 }
253
Antti Palosaarib8843c72014-08-24 20:11:27 -0300254 /* calculate sample rate and output it in 10 seconds intervals */
Antti Palosaari634fe502014-06-27 21:30:55 -0300255 if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
256 #define MSECS 10000UL
Antti Palosaarib8843c72014-08-24 20:11:27 -0300257 unsigned int msecs = jiffies_to_msecs(jiffies -
258 s->jiffies_next + msecs_to_jiffies(MSECS));
Antti Palosaari634fe502014-06-27 21:30:55 -0300259 unsigned int samples = s->sample - s->sample_measured;
Antti Palosaari70570052014-08-24 18:31:52 -0300260
Antti Palosaari634fe502014-06-27 21:30:55 -0300261 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
262 s->sample_measured = s->sample;
Antti Palosaarib8843c72014-08-24 20:11:27 -0300263 dev_dbg(s->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n",
264 src_len, samples, msecs,
265 samples * 1000UL / msecs);
Antti Palosaari634fe502014-06-27 21:30:55 -0300266 }
267
268 /* total number of samples */
269 s->sample += src_len / 2;
270
271 return dst_len;
272}
273
274/*
275 * This gets called for the bulk stream pipe. This is done in interrupt
276 * time, so it has to be fast, not crash, and not stall. Neat.
277 */
278static void airspy_urb_complete(struct urb *urb)
279{
280 struct airspy *s = urb->context;
281 struct airspy_frame_buf *fbuf;
282
Antti Palosaari617123a2014-08-24 19:14:32 -0300283 dev_dbg_ratelimited(s->dev, "status=%d length=%d/%d errors=%d\n",
284 urb->status, urb->actual_length,
Antti Palosaari634fe502014-06-27 21:30:55 -0300285 urb->transfer_buffer_length, urb->error_count);
286
287 switch (urb->status) {
288 case 0: /* success */
289 case -ETIMEDOUT: /* NAK */
290 break;
291 case -ECONNRESET: /* kill */
292 case -ENOENT:
293 case -ESHUTDOWN:
294 return;
295 default: /* error */
Antti Palosaari617123a2014-08-24 19:14:32 -0300296 dev_err_ratelimited(s->dev, "URB failed %d\n", urb->status);
Antti Palosaari634fe502014-06-27 21:30:55 -0300297 break;
298 }
299
300 if (likely(urb->actual_length > 0)) {
301 void *ptr;
302 unsigned int len;
303 /* get free framebuffer */
304 fbuf = airspy_get_next_fill_buf(s);
305 if (unlikely(fbuf == NULL)) {
306 s->vb_full++;
Antti Palosaari617123a2014-08-24 19:14:32 -0300307 dev_notice_ratelimited(s->dev,
Antti Palosaari634fe502014-06-27 21:30:55 -0300308 "videobuf is full, %d packets dropped\n",
309 s->vb_full);
310 goto skip;
311 }
312
313 /* fill framebuffer */
Junghak Sung2d700712015-09-22 10:30:30 -0300314 ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
Antti Palosaari634fe502014-06-27 21:30:55 -0300315 len = airspy_convert_stream(s, ptr, urb->transfer_buffer,
316 urb->actual_length);
Junghak Sung2d700712015-09-22 10:30:30 -0300317 vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len);
Junghak Sungd6dd6452015-11-03 08:16:37 -0200318 fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300319 fbuf->vb.sequence = s->sequence++;
320 vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
Antti Palosaari634fe502014-06-27 21:30:55 -0300321 }
322skip:
323 usb_submit_urb(urb, GFP_ATOMIC);
324}
325
326static int airspy_kill_urbs(struct airspy *s)
327{
328 int i;
329
330 for (i = s->urbs_submitted - 1; i >= 0; i--) {
Antti Palosaari617123a2014-08-24 19:14:32 -0300331 dev_dbg(s->dev, "kill urb=%d\n", i);
Antti Palosaari634fe502014-06-27 21:30:55 -0300332 /* stop the URB */
333 usb_kill_urb(s->urb_list[i]);
334 }
335 s->urbs_submitted = 0;
336
337 return 0;
338}
339
340static int airspy_submit_urbs(struct airspy *s)
341{
342 int i, ret;
343
344 for (i = 0; i < s->urbs_initialized; i++) {
Antti Palosaari617123a2014-08-24 19:14:32 -0300345 dev_dbg(s->dev, "submit urb=%d\n", i);
Antti Palosaari634fe502014-06-27 21:30:55 -0300346 ret = usb_submit_urb(s->urb_list[i], GFP_ATOMIC);
347 if (ret) {
Antti Palosaari617123a2014-08-24 19:14:32 -0300348 dev_err(s->dev, "Could not submit URB no. %d - get them all back\n",
Antti Palosaari634fe502014-06-27 21:30:55 -0300349 i);
350 airspy_kill_urbs(s);
351 return ret;
352 }
353 s->urbs_submitted++;
354 }
355
356 return 0;
357}
358
359static int airspy_free_stream_bufs(struct airspy *s)
360{
Mauro Carvalho Chehabf6f7b582016-02-27 07:51:07 -0300361 if (test_bit(USB_STATE_URB_BUF, &s->flags)) {
Antti Palosaari634fe502014-06-27 21:30:55 -0300362 while (s->buf_num) {
363 s->buf_num--;
Antti Palosaari617123a2014-08-24 19:14:32 -0300364 dev_dbg(s->dev, "free buf=%d\n", s->buf_num);
Antti Palosaari634fe502014-06-27 21:30:55 -0300365 usb_free_coherent(s->udev, s->buf_size,
366 s->buf_list[s->buf_num],
367 s->dma_addr[s->buf_num]);
368 }
369 }
Mauro Carvalho Chehabf6f7b582016-02-27 07:51:07 -0300370 clear_bit(USB_STATE_URB_BUF, &s->flags);
Antti Palosaari634fe502014-06-27 21:30:55 -0300371
372 return 0;
373}
374
375static int airspy_alloc_stream_bufs(struct airspy *s)
376{
377 s->buf_num = 0;
378 s->buf_size = BULK_BUFFER_SIZE;
379
Antti Palosaari617123a2014-08-24 19:14:32 -0300380 dev_dbg(s->dev, "all in all I will use %u bytes for streaming\n",
381 MAX_BULK_BUFS * BULK_BUFFER_SIZE);
Antti Palosaari634fe502014-06-27 21:30:55 -0300382
383 for (s->buf_num = 0; s->buf_num < MAX_BULK_BUFS; s->buf_num++) {
384 s->buf_list[s->buf_num] = usb_alloc_coherent(s->udev,
385 BULK_BUFFER_SIZE, GFP_ATOMIC,
386 &s->dma_addr[s->buf_num]);
387 if (!s->buf_list[s->buf_num]) {
Antti Palosaari617123a2014-08-24 19:14:32 -0300388 dev_dbg(s->dev, "alloc buf=%d failed\n", s->buf_num);
Antti Palosaari634fe502014-06-27 21:30:55 -0300389 airspy_free_stream_bufs(s);
390 return -ENOMEM;
391 }
392
Antti Palosaari617123a2014-08-24 19:14:32 -0300393 dev_dbg(s->dev, "alloc buf=%d %p (dma %llu)\n", s->buf_num,
Antti Palosaari634fe502014-06-27 21:30:55 -0300394 s->buf_list[s->buf_num],
395 (long long)s->dma_addr[s->buf_num]);
Mauro Carvalho Chehabf6f7b582016-02-27 07:51:07 -0300396 set_bit(USB_STATE_URB_BUF, &s->flags);
Antti Palosaari634fe502014-06-27 21:30:55 -0300397 }
398
399 return 0;
400}
401
402static int airspy_free_urbs(struct airspy *s)
403{
404 int i;
405
406 airspy_kill_urbs(s);
407
408 for (i = s->urbs_initialized - 1; i >= 0; i--) {
409 if (s->urb_list[i]) {
Antti Palosaari617123a2014-08-24 19:14:32 -0300410 dev_dbg(s->dev, "free urb=%d\n", i);
Antti Palosaari634fe502014-06-27 21:30:55 -0300411 /* free the URBs */
412 usb_free_urb(s->urb_list[i]);
413 }
414 }
415 s->urbs_initialized = 0;
416
417 return 0;
418}
419
420static int airspy_alloc_urbs(struct airspy *s)
421{
422 int i, j;
423
424 /* allocate the URBs */
425 for (i = 0; i < MAX_BULK_BUFS; i++) {
Antti Palosaari617123a2014-08-24 19:14:32 -0300426 dev_dbg(s->dev, "alloc urb=%d\n", i);
Antti Palosaari634fe502014-06-27 21:30:55 -0300427 s->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
428 if (!s->urb_list[i]) {
Antti Palosaari634fe502014-06-27 21:30:55 -0300429 for (j = 0; j < i; j++)
430 usb_free_urb(s->urb_list[j]);
431 return -ENOMEM;
432 }
433 usb_fill_bulk_urb(s->urb_list[i],
434 s->udev,
435 usb_rcvbulkpipe(s->udev, 0x81),
436 s->buf_list[i],
437 BULK_BUFFER_SIZE,
438 airspy_urb_complete, s);
439
440 s->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
441 s->urb_list[i]->transfer_dma = s->dma_addr[i];
442 s->urbs_initialized++;
443 }
444
445 return 0;
446}
447
448/* Must be called with vb_queue_lock hold */
449static void airspy_cleanup_queued_bufs(struct airspy *s)
450{
Antti Palosaari8880f2c2014-08-24 19:27:43 -0300451 unsigned long flags;
Antti Palosaari634fe502014-06-27 21:30:55 -0300452
Antti Palosaari617123a2014-08-24 19:14:32 -0300453 dev_dbg(s->dev, "\n");
Antti Palosaari634fe502014-06-27 21:30:55 -0300454
455 spin_lock_irqsave(&s->queued_bufs_lock, flags);
456 while (!list_empty(&s->queued_bufs)) {
457 struct airspy_frame_buf *buf;
Antti Palosaari70570052014-08-24 18:31:52 -0300458
Antti Palosaari634fe502014-06-27 21:30:55 -0300459 buf = list_entry(s->queued_bufs.next,
460 struct airspy_frame_buf, list);
461 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300462 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Antti Palosaari634fe502014-06-27 21:30:55 -0300463 }
464 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
465}
466
467/* The user yanked out the cable... */
468static void airspy_disconnect(struct usb_interface *intf)
469{
470 struct v4l2_device *v = usb_get_intfdata(intf);
471 struct airspy *s = container_of(v, struct airspy, v4l2_dev);
472
Antti Palosaari617123a2014-08-24 19:14:32 -0300473 dev_dbg(s->dev, "\n");
Antti Palosaari634fe502014-06-27 21:30:55 -0300474
475 mutex_lock(&s->vb_queue_lock);
476 mutex_lock(&s->v4l2_lock);
477 /* No need to keep the urbs around after disconnection */
478 s->udev = NULL;
479 v4l2_device_disconnect(&s->v4l2_dev);
480 video_unregister_device(&s->vdev);
481 mutex_unlock(&s->v4l2_lock);
482 mutex_unlock(&s->vb_queue_lock);
483
484 v4l2_device_put(&s->v4l2_dev);
485}
486
487/* Videobuf2 operations */
488static int airspy_queue_setup(struct vb2_queue *vq,
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200489 unsigned int *nbuffers,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300490 unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
Antti Palosaari634fe502014-06-27 21:30:55 -0300491{
492 struct airspy *s = vb2_get_drv_priv(vq);
493
Antti Palosaari617123a2014-08-24 19:14:32 -0300494 dev_dbg(s->dev, "nbuffers=%d\n", *nbuffers);
Antti Palosaari634fe502014-06-27 21:30:55 -0300495
496 /* Need at least 8 buffers */
497 if (vq->num_buffers + *nbuffers < 8)
498 *nbuffers = 8 - vq->num_buffers;
499 *nplanes = 1;
Antti Palosaari1b303e12014-07-18 21:22:08 -0300500 sizes[0] = PAGE_ALIGN(s->buffersize);
Antti Palosaari634fe502014-06-27 21:30:55 -0300501
Antti Palosaari617123a2014-08-24 19:14:32 -0300502 dev_dbg(s->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
Antti Palosaari634fe502014-06-27 21:30:55 -0300503 return 0;
504}
505
506static void airspy_buf_queue(struct vb2_buffer *vb)
507{
Junghak Sung2d700712015-09-22 10:30:30 -0300508 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Antti Palosaari634fe502014-06-27 21:30:55 -0300509 struct airspy *s = vb2_get_drv_priv(vb->vb2_queue);
510 struct airspy_frame_buf *buf =
Junghak Sung2d700712015-09-22 10:30:30 -0300511 container_of(vbuf, struct airspy_frame_buf, vb);
Antti Palosaari8880f2c2014-08-24 19:27:43 -0300512 unsigned long flags;
Antti Palosaari634fe502014-06-27 21:30:55 -0300513
514 /* Check the device has not disconnected between prep and queuing */
515 if (unlikely(!s->udev)) {
Junghak Sung2d700712015-09-22 10:30:30 -0300516 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Antti Palosaari634fe502014-06-27 21:30:55 -0300517 return;
518 }
519
520 spin_lock_irqsave(&s->queued_bufs_lock, flags);
521 list_add_tail(&buf->list, &s->queued_bufs);
522 spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
523}
524
525static int airspy_start_streaming(struct vb2_queue *vq, unsigned int count)
526{
527 struct airspy *s = vb2_get_drv_priv(vq);
528 int ret;
529
Antti Palosaari617123a2014-08-24 19:14:32 -0300530 dev_dbg(s->dev, "\n");
Antti Palosaari634fe502014-06-27 21:30:55 -0300531
532 if (!s->udev)
533 return -ENODEV;
534
535 mutex_lock(&s->v4l2_lock);
536
Antti Palosaari634fe502014-06-27 21:30:55 -0300537 s->sequence = 0;
538
Antti Palosaari6b831d72014-08-24 21:59:36 -0300539 set_bit(POWER_ON, &s->flags);
540
Antti Palosaari634fe502014-06-27 21:30:55 -0300541 ret = airspy_alloc_stream_bufs(s);
542 if (ret)
Antti Palosaari6b831d72014-08-24 21:59:36 -0300543 goto err_clear_bit;
Antti Palosaari634fe502014-06-27 21:30:55 -0300544
545 ret = airspy_alloc_urbs(s);
546 if (ret)
Antti Palosaari6b831d72014-08-24 21:59:36 -0300547 goto err_free_stream_bufs;
Antti Palosaari634fe502014-06-27 21:30:55 -0300548
549 ret = airspy_submit_urbs(s);
550 if (ret)
Antti Palosaari6b831d72014-08-24 21:59:36 -0300551 goto err_free_urbs;
Antti Palosaari634fe502014-06-27 21:30:55 -0300552
553 /* start hardware streaming */
554 ret = airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 1, 0, NULL, 0);
555 if (ret)
Antti Palosaari6b831d72014-08-24 21:59:36 -0300556 goto err_kill_urbs;
557
558 goto exit_mutex_unlock;
559
560err_kill_urbs:
561 airspy_kill_urbs(s);
562err_free_urbs:
563 airspy_free_urbs(s);
564err_free_stream_bufs:
565 airspy_free_stream_bufs(s);
566err_clear_bit:
567 clear_bit(POWER_ON, &s->flags);
568
569 /* return all queued buffers to vb2 */
570 {
571 struct airspy_frame_buf *buf, *tmp;
572
573 list_for_each_entry_safe(buf, tmp, &s->queued_bufs, list) {
574 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300575 vb2_buffer_done(&buf->vb.vb2_buf,
576 VB2_BUF_STATE_QUEUED);
Antti Palosaari6b831d72014-08-24 21:59:36 -0300577 }
578 }
579
580exit_mutex_unlock:
Antti Palosaari634fe502014-06-27 21:30:55 -0300581 mutex_unlock(&s->v4l2_lock);
582
583 return ret;
584}
585
586static void airspy_stop_streaming(struct vb2_queue *vq)
587{
588 struct airspy *s = vb2_get_drv_priv(vq);
Antti Palosaari634fe502014-06-27 21:30:55 -0300589
Antti Palosaari617123a2014-08-24 19:14:32 -0300590 dev_dbg(s->dev, "\n");
Antti Palosaari634fe502014-06-27 21:30:55 -0300591
592 mutex_lock(&s->v4l2_lock);
593
594 /* stop hardware streaming */
Mauro Carvalho Chehabd9d889d2014-07-21 20:29:54 -0300595 airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 0, 0, NULL, 0);
Antti Palosaari634fe502014-06-27 21:30:55 -0300596
597 airspy_kill_urbs(s);
598 airspy_free_urbs(s);
599 airspy_free_stream_bufs(s);
600
601 airspy_cleanup_queued_bufs(s);
602
603 clear_bit(POWER_ON, &s->flags);
604
605 mutex_unlock(&s->v4l2_lock);
606}
607
608static struct vb2_ops airspy_vb2_ops = {
609 .queue_setup = airspy_queue_setup,
610 .buf_queue = airspy_buf_queue,
611 .start_streaming = airspy_start_streaming,
612 .stop_streaming = airspy_stop_streaming,
613 .wait_prepare = vb2_ops_wait_prepare,
614 .wait_finish = vb2_ops_wait_finish,
615};
616
617static int airspy_querycap(struct file *file, void *fh,
618 struct v4l2_capability *cap)
619{
620 struct airspy *s = video_drvdata(file);
621
Antti Palosaari634fe502014-06-27 21:30:55 -0300622 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
623 strlcpy(cap->card, s->vdev.name, sizeof(cap->card));
624 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
625 cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
626 V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
627 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
628
629 return 0;
630}
631
632static int airspy_enum_fmt_sdr_cap(struct file *file, void *priv,
633 struct v4l2_fmtdesc *f)
634{
Antti Palosaari634fe502014-06-27 21:30:55 -0300635 if (f->index >= NUM_FORMATS)
636 return -EINVAL;
637
638 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
639 f->pixelformat = formats[f->index].pixelformat;
640
641 return 0;
642}
643
644static int airspy_g_fmt_sdr_cap(struct file *file, void *priv,
645 struct v4l2_format *f)
646{
647 struct airspy *s = video_drvdata(file);
648
Antti Palosaari634fe502014-06-27 21:30:55 -0300649 f->fmt.sdr.pixelformat = s->pixelformat;
Antti Palosaari1b303e12014-07-18 21:22:08 -0300650 f->fmt.sdr.buffersize = s->buffersize;
651 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
Antti Palosaari634fe502014-06-27 21:30:55 -0300652
653 return 0;
654}
655
656static int airspy_s_fmt_sdr_cap(struct file *file, void *priv,
657 struct v4l2_format *f)
658{
659 struct airspy *s = video_drvdata(file);
660 struct vb2_queue *q = &s->vb_queue;
661 int i;
662
Antti Palosaari634fe502014-06-27 21:30:55 -0300663 if (vb2_is_busy(q))
664 return -EBUSY;
665
666 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
667 for (i = 0; i < NUM_FORMATS; i++) {
668 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
Antti Palosaari1b303e12014-07-18 21:22:08 -0300669 s->pixelformat = formats[i].pixelformat;
670 s->buffersize = formats[i].buffersize;
671 f->fmt.sdr.buffersize = formats[i].buffersize;
Antti Palosaari634fe502014-06-27 21:30:55 -0300672 return 0;
673 }
674 }
675
Antti Palosaari634fe502014-06-27 21:30:55 -0300676 s->pixelformat = formats[0].pixelformat;
Antti Palosaari1b303e12014-07-18 21:22:08 -0300677 s->buffersize = formats[0].buffersize;
678 f->fmt.sdr.pixelformat = formats[0].pixelformat;
679 f->fmt.sdr.buffersize = formats[0].buffersize;
Antti Palosaari634fe502014-06-27 21:30:55 -0300680
681 return 0;
682}
683
684static int airspy_try_fmt_sdr_cap(struct file *file, void *priv,
685 struct v4l2_format *f)
686{
Antti Palosaari634fe502014-06-27 21:30:55 -0300687 int i;
688
Antti Palosaari634fe502014-06-27 21:30:55 -0300689 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
690 for (i = 0; i < NUM_FORMATS; i++) {
Antti Palosaari1b303e12014-07-18 21:22:08 -0300691 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
692 f->fmt.sdr.buffersize = formats[i].buffersize;
Antti Palosaari634fe502014-06-27 21:30:55 -0300693 return 0;
Antti Palosaari1b303e12014-07-18 21:22:08 -0300694 }
Antti Palosaari634fe502014-06-27 21:30:55 -0300695 }
696
697 f->fmt.sdr.pixelformat = formats[0].pixelformat;
Antti Palosaari1b303e12014-07-18 21:22:08 -0300698 f->fmt.sdr.buffersize = formats[0].buffersize;
Antti Palosaari634fe502014-06-27 21:30:55 -0300699
700 return 0;
701}
702
703static int airspy_s_tuner(struct file *file, void *priv,
704 const struct v4l2_tuner *v)
705{
Antti Palosaari634fe502014-06-27 21:30:55 -0300706 int ret;
707
Antti Palosaari634fe502014-06-27 21:30:55 -0300708 if (v->index == 0)
709 ret = 0;
710 else if (v->index == 1)
711 ret = 0;
712 else
713 ret = -EINVAL;
714
715 return ret;
716}
717
718static int airspy_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
719{
Antti Palosaari634fe502014-06-27 21:30:55 -0300720 int ret;
721
Antti Palosaari634fe502014-06-27 21:30:55 -0300722 if (v->index == 0) {
723 strlcpy(v->name, "AirSpy ADC", sizeof(v->name));
724 v->type = V4L2_TUNER_ADC;
725 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
726 v->rangelow = bands[0].rangelow;
727 v->rangehigh = bands[0].rangehigh;
728 ret = 0;
729 } else if (v->index == 1) {
730 strlcpy(v->name, "AirSpy RF", sizeof(v->name));
731 v->type = V4L2_TUNER_RF;
732 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
733 v->rangelow = bands_rf[0].rangelow;
734 v->rangehigh = bands_rf[0].rangehigh;
735 ret = 0;
736 } else {
737 ret = -EINVAL;
738 }
739
740 return ret;
741}
742
743static int airspy_g_frequency(struct file *file, void *priv,
744 struct v4l2_frequency *f)
745{
746 struct airspy *s = video_drvdata(file);
Antti Palosaari617123a2014-08-24 19:14:32 -0300747 int ret;
Antti Palosaari634fe502014-06-27 21:30:55 -0300748
749 if (f->tuner == 0) {
750 f->type = V4L2_TUNER_ADC;
751 f->frequency = s->f_adc;
Antti Palosaari617123a2014-08-24 19:14:32 -0300752 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc);
Antti Palosaari634fe502014-06-27 21:30:55 -0300753 ret = 0;
754 } else if (f->tuner == 1) {
755 f->type = V4L2_TUNER_RF;
756 f->frequency = s->f_rf;
Antti Palosaari617123a2014-08-24 19:14:32 -0300757 dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf);
758 ret = 0;
Antti Palosaari634fe502014-06-27 21:30:55 -0300759 } else {
760 ret = -EINVAL;
761 }
762
763 return ret;
764}
765
766static int airspy_s_frequency(struct file *file, void *priv,
767 const struct v4l2_frequency *f)
768{
769 struct airspy *s = video_drvdata(file);
770 int ret;
771 u8 buf[4];
772
Antti Palosaari634fe502014-06-27 21:30:55 -0300773 if (f->tuner == 0) {
774 s->f_adc = clamp_t(unsigned int, f->frequency,
775 bands[0].rangelow,
776 bands[0].rangehigh);
Antti Palosaari617123a2014-08-24 19:14:32 -0300777 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc);
Antti Palosaari634fe502014-06-27 21:30:55 -0300778 ret = 0;
779 } else if (f->tuner == 1) {
780 s->f_rf = clamp_t(unsigned int, f->frequency,
781 bands_rf[0].rangelow,
782 bands_rf[0].rangehigh);
Antti Palosaari617123a2014-08-24 19:14:32 -0300783 dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf);
Antti Palosaari634fe502014-06-27 21:30:55 -0300784 buf[0] = (s->f_rf >> 0) & 0xff;
785 buf[1] = (s->f_rf >> 8) & 0xff;
786 buf[2] = (s->f_rf >> 16) & 0xff;
787 buf[3] = (s->f_rf >> 24) & 0xff;
788 ret = airspy_ctrl_msg(s, CMD_SET_FREQ, 0, 0, buf, 4);
789 } else {
790 ret = -EINVAL;
791 }
792
793 return ret;
794}
795
796static int airspy_enum_freq_bands(struct file *file, void *priv,
797 struct v4l2_frequency_band *band)
798{
Antti Palosaari634fe502014-06-27 21:30:55 -0300799 int ret;
Antti Palosaari70570052014-08-24 18:31:52 -0300800
Antti Palosaari634fe502014-06-27 21:30:55 -0300801 if (band->tuner == 0) {
802 if (band->index >= ARRAY_SIZE(bands)) {
803 ret = -EINVAL;
804 } else {
805 *band = bands[band->index];
806 ret = 0;
807 }
808 } else if (band->tuner == 1) {
809 if (band->index >= ARRAY_SIZE(bands_rf)) {
810 ret = -EINVAL;
811 } else {
812 *band = bands_rf[band->index];
813 ret = 0;
814 }
815 } else {
816 ret = -EINVAL;
817 }
818
819 return ret;
820}
821
822static const struct v4l2_ioctl_ops airspy_ioctl_ops = {
823 .vidioc_querycap = airspy_querycap,
824
825 .vidioc_enum_fmt_sdr_cap = airspy_enum_fmt_sdr_cap,
826 .vidioc_g_fmt_sdr_cap = airspy_g_fmt_sdr_cap,
827 .vidioc_s_fmt_sdr_cap = airspy_s_fmt_sdr_cap,
828 .vidioc_try_fmt_sdr_cap = airspy_try_fmt_sdr_cap,
829
830 .vidioc_reqbufs = vb2_ioctl_reqbufs,
831 .vidioc_create_bufs = vb2_ioctl_create_bufs,
832 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
833 .vidioc_querybuf = vb2_ioctl_querybuf,
834 .vidioc_qbuf = vb2_ioctl_qbuf,
835 .vidioc_dqbuf = vb2_ioctl_dqbuf,
836
837 .vidioc_streamon = vb2_ioctl_streamon,
838 .vidioc_streamoff = vb2_ioctl_streamoff,
839
840 .vidioc_g_tuner = airspy_g_tuner,
841 .vidioc_s_tuner = airspy_s_tuner,
842
843 .vidioc_g_frequency = airspy_g_frequency,
844 .vidioc_s_frequency = airspy_s_frequency,
845 .vidioc_enum_freq_bands = airspy_enum_freq_bands,
846
847 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
848 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
849 .vidioc_log_status = v4l2_ctrl_log_status,
850};
851
852static const struct v4l2_file_operations airspy_fops = {
853 .owner = THIS_MODULE,
854 .open = v4l2_fh_open,
855 .release = vb2_fop_release,
856 .read = vb2_fop_read,
857 .poll = vb2_fop_poll,
858 .mmap = vb2_fop_mmap,
859 .unlocked_ioctl = video_ioctl2,
860};
861
862static struct video_device airspy_template = {
863 .name = "AirSpy SDR",
864 .release = video_device_release_empty,
865 .fops = &airspy_fops,
866 .ioctl_ops = &airspy_ioctl_ops,
867};
868
869static void airspy_video_release(struct v4l2_device *v)
870{
871 struct airspy *s = container_of(v, struct airspy, v4l2_dev);
872
873 v4l2_ctrl_handler_free(&s->hdl);
874 v4l2_device_unregister(&s->v4l2_dev);
875 kfree(s);
876}
877
878static int airspy_set_lna_gain(struct airspy *s)
879{
880 int ret;
881 u8 u8tmp;
882
Antti Palosaari617123a2014-08-24 19:14:32 -0300883 dev_dbg(s->dev, "lna auto=%d->%d val=%d->%d\n",
884 s->lna_gain_auto->cur.val, s->lna_gain_auto->val,
885 s->lna_gain->cur.val, s->lna_gain->val);
Antti Palosaari634fe502014-06-27 21:30:55 -0300886
887 ret = airspy_ctrl_msg(s, CMD_SET_LNA_AGC, 0, s->lna_gain_auto->val,
888 &u8tmp, 1);
889 if (ret)
890 goto err;
891
892 if (s->lna_gain_auto->val == false) {
893 ret = airspy_ctrl_msg(s, CMD_SET_LNA_GAIN, 0, s->lna_gain->val,
894 &u8tmp, 1);
895 if (ret)
896 goto err;
897 }
898err:
899 if (ret)
Antti Palosaari617123a2014-08-24 19:14:32 -0300900 dev_dbg(s->dev, "failed=%d\n", ret);
Antti Palosaari634fe502014-06-27 21:30:55 -0300901
902 return ret;
903}
904
905static int airspy_set_mixer_gain(struct airspy *s)
906{
907 int ret;
908 u8 u8tmp;
909
Antti Palosaari617123a2014-08-24 19:14:32 -0300910 dev_dbg(s->dev, "mixer auto=%d->%d val=%d->%d\n",
911 s->mixer_gain_auto->cur.val, s->mixer_gain_auto->val,
912 s->mixer_gain->cur.val, s->mixer_gain->val);
Antti Palosaari634fe502014-06-27 21:30:55 -0300913
914 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_AGC, 0, s->mixer_gain_auto->val,
915 &u8tmp, 1);
916 if (ret)
917 goto err;
918
919 if (s->mixer_gain_auto->val == false) {
920 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_GAIN, 0,
921 s->mixer_gain->val, &u8tmp, 1);
922 if (ret)
923 goto err;
924 }
925err:
926 if (ret)
Antti Palosaari617123a2014-08-24 19:14:32 -0300927 dev_dbg(s->dev, "failed=%d\n", ret);
Antti Palosaari634fe502014-06-27 21:30:55 -0300928
929 return ret;
930}
931
932static int airspy_set_if_gain(struct airspy *s)
933{
934 int ret;
935 u8 u8tmp;
936
Antti Palosaari617123a2014-08-24 19:14:32 -0300937 dev_dbg(s->dev, "val=%d->%d\n", s->if_gain->cur.val, s->if_gain->val);
Antti Palosaari634fe502014-06-27 21:30:55 -0300938
939 ret = airspy_ctrl_msg(s, CMD_SET_VGA_GAIN, 0, s->if_gain->val,
940 &u8tmp, 1);
941 if (ret)
Antti Palosaari617123a2014-08-24 19:14:32 -0300942 dev_dbg(s->dev, "failed=%d\n", ret);
Antti Palosaari634fe502014-06-27 21:30:55 -0300943
944 return ret;
945}
946
947static int airspy_s_ctrl(struct v4l2_ctrl *ctrl)
948{
949 struct airspy *s = container_of(ctrl->handler, struct airspy, hdl);
950 int ret;
951
952 switch (ctrl->id) {
953 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
954 case V4L2_CID_RF_TUNER_LNA_GAIN:
955 ret = airspy_set_lna_gain(s);
956 break;
957 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
958 case V4L2_CID_RF_TUNER_MIXER_GAIN:
959 ret = airspy_set_mixer_gain(s);
960 break;
961 case V4L2_CID_RF_TUNER_IF_GAIN:
962 ret = airspy_set_if_gain(s);
963 break;
964 default:
Antti Palosaari617123a2014-08-24 19:14:32 -0300965 dev_dbg(s->dev, "unknown ctrl: id=%d name=%s\n",
966 ctrl->id, ctrl->name);
Antti Palosaari634fe502014-06-27 21:30:55 -0300967 ret = -EINVAL;
968 }
969
970 return ret;
971}
972
973static const struct v4l2_ctrl_ops airspy_ctrl_ops = {
974 .s_ctrl = airspy_s_ctrl,
975};
976
977static int airspy_probe(struct usb_interface *intf,
978 const struct usb_device_id *id)
979{
Antti Palosaari617123a2014-08-24 19:14:32 -0300980 struct airspy *s;
Antti Palosaari634fe502014-06-27 21:30:55 -0300981 int ret;
982 u8 u8tmp, buf[BUF_SIZE];
983
984 s = kzalloc(sizeof(struct airspy), GFP_KERNEL);
985 if (s == NULL) {
Antti Palosaari617123a2014-08-24 19:14:32 -0300986 dev_err(&intf->dev, "Could not allocate memory for state\n");
Antti Palosaari634fe502014-06-27 21:30:55 -0300987 return -ENOMEM;
988 }
989
990 mutex_init(&s->v4l2_lock);
991 mutex_init(&s->vb_queue_lock);
992 spin_lock_init(&s->queued_bufs_lock);
993 INIT_LIST_HEAD(&s->queued_bufs);
Antti Palosaari617123a2014-08-24 19:14:32 -0300994 s->dev = &intf->dev;
995 s->udev = interface_to_usbdev(intf);
Antti Palosaari634fe502014-06-27 21:30:55 -0300996 s->f_adc = bands[0].rangelow;
997 s->f_rf = bands_rf[0].rangelow;
Antti Palosaari1b303e12014-07-18 21:22:08 -0300998 s->pixelformat = formats[0].pixelformat;
999 s->buffersize = formats[0].buffersize;
Antti Palosaari634fe502014-06-27 21:30:55 -03001000
1001 /* Detect device */
1002 ret = airspy_ctrl_msg(s, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1);
1003 if (ret == 0)
1004 ret = airspy_ctrl_msg(s, CMD_VERSION_STRING_READ, 0, 0,
1005 buf, BUF_SIZE);
1006 if (ret) {
Antti Palosaari617123a2014-08-24 19:14:32 -03001007 dev_err(s->dev, "Could not detect board\n");
Antti Palosaari634fe502014-06-27 21:30:55 -03001008 goto err_free_mem;
1009 }
1010
1011 buf[BUF_SIZE - 1] = '\0';
1012
Antti Palosaari617123a2014-08-24 19:14:32 -03001013 dev_info(s->dev, "Board ID: %02x\n", u8tmp);
1014 dev_info(s->dev, "Firmware version: %s\n", buf);
Antti Palosaari634fe502014-06-27 21:30:55 -03001015
1016 /* Init videobuf2 queue structure */
1017 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1018 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1019 s->vb_queue.drv_priv = s;
1020 s->vb_queue.buf_struct_size = sizeof(struct airspy_frame_buf);
1021 s->vb_queue.ops = &airspy_vb2_ops;
1022 s->vb_queue.mem_ops = &vb2_vmalloc_memops;
1023 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1024 ret = vb2_queue_init(&s->vb_queue);
1025 if (ret) {
Antti Palosaari617123a2014-08-24 19:14:32 -03001026 dev_err(s->dev, "Could not initialize vb2 queue\n");
Antti Palosaari634fe502014-06-27 21:30:55 -03001027 goto err_free_mem;
1028 }
1029
1030 /* Init video_device structure */
1031 s->vdev = airspy_template;
1032 s->vdev.queue = &s->vb_queue;
1033 s->vdev.queue->lock = &s->vb_queue_lock;
Antti Palosaari634fe502014-06-27 21:30:55 -03001034 video_set_drvdata(&s->vdev, s);
1035
1036 /* Register the v4l2_device structure */
1037 s->v4l2_dev.release = airspy_video_release;
1038 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1039 if (ret) {
Antti Palosaari617123a2014-08-24 19:14:32 -03001040 dev_err(s->dev, "Failed to register v4l2-device (%d)\n", ret);
Antti Palosaari634fe502014-06-27 21:30:55 -03001041 goto err_free_mem;
1042 }
1043
1044 /* Register controls */
1045 v4l2_ctrl_handler_init(&s->hdl, 5);
1046 s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
1047 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 0);
1048 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
1049 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 14, 1, 8);
1050 v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false);
1051 s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
1052 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 0);
1053 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
1054 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 15, 1, 8);
1055 v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false);
1056 s->if_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
1057 V4L2_CID_RF_TUNER_IF_GAIN, 0, 15, 1, 0);
1058 if (s->hdl.error) {
1059 ret = s->hdl.error;
Antti Palosaari617123a2014-08-24 19:14:32 -03001060 dev_err(s->dev, "Could not initialize controls\n");
Antti Palosaari634fe502014-06-27 21:30:55 -03001061 goto err_free_controls;
1062 }
1063
1064 v4l2_ctrl_handler_setup(&s->hdl);
1065
1066 s->v4l2_dev.ctrl_handler = &s->hdl;
1067 s->vdev.v4l2_dev = &s->v4l2_dev;
1068 s->vdev.lock = &s->v4l2_lock;
1069
1070 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
1071 if (ret) {
Antti Palosaari617123a2014-08-24 19:14:32 -03001072 dev_err(s->dev, "Failed to register as video device (%d)\n",
Antti Palosaari634fe502014-06-27 21:30:55 -03001073 ret);
James Patrick-Evansaa93d1f2016-07-15 16:40:45 +01001074 goto err_free_controls;
Antti Palosaari634fe502014-06-27 21:30:55 -03001075 }
Antti Palosaari617123a2014-08-24 19:14:32 -03001076 dev_info(s->dev, "Registered as %s\n",
Antti Palosaari634fe502014-06-27 21:30:55 -03001077 video_device_node_name(&s->vdev));
Antti Palosaari617123a2014-08-24 19:14:32 -03001078 dev_notice(s->dev, "SDR API is still slightly experimental and functionality changes may follow\n");
Antti Palosaari634fe502014-06-27 21:30:55 -03001079 return 0;
1080
1081err_free_controls:
1082 v4l2_ctrl_handler_free(&s->hdl);
Antti Palosaari634fe502014-06-27 21:30:55 -03001083 v4l2_device_unregister(&s->v4l2_dev);
1084err_free_mem:
1085 kfree(s);
1086 return ret;
1087}
1088
1089/* USB device ID list */
1090static struct usb_device_id airspy_id_table[] = {
1091 { USB_DEVICE(0x1d50, 0x60a1) }, /* AirSpy */
1092 { }
1093};
1094MODULE_DEVICE_TABLE(usb, airspy_id_table);
1095
1096/* USB subsystem interface */
1097static struct usb_driver airspy_driver = {
1098 .name = KBUILD_MODNAME,
1099 .probe = airspy_probe,
1100 .disconnect = airspy_disconnect,
1101 .id_table = airspy_id_table,
1102};
1103
1104module_usb_driver(airspy_driver);
1105
1106MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1107MODULE_DESCRIPTION("AirSpy SDR");
1108MODULE_LICENSE("GPL");