4 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
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.
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.
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>
24 #include <media/videobuf2-v4l2.h>
25 #include <media/videobuf2-vmalloc.h>
27 /* AirSpy USB API commands (from AirSpy Library) */
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,
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,
52 * bEndpointAddress 0x81 EP 1 IN
54 * wMaxPacketSize 0x0200 1x 512 bytes
56 #define MAX_BULK_BUFS (6)
57 #define BULK_BUFFER_SIZE (128 * 512)
59 static const struct v4l2_frequency_band bands
[] = {
62 .type
= V4L2_TUNER_ADC
,
64 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
66 .rangehigh
= 20000000,
70 static const struct v4l2_frequency_band bands_rf
[] = {
73 .type
= V4L2_TUNER_RF
,
75 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
77 .rangehigh
= 1750000000,
82 struct airspy_format
{
88 /* format descriptions for capture and preview */
89 static struct airspy_format formats
[] = {
92 .pixelformat
= V4L2_SDR_FMT_RU12LE
,
93 .buffersize
= BULK_BUFFER_SIZE
,
97 static const unsigned int NUM_FORMATS
= ARRAY_SIZE(formats
);
99 /* intermediate buffers with raw data from the USB device */
100 struct airspy_frame_buf
{
101 /* common v4l buffer stuff -- must be first */
102 struct vb2_v4l2_buffer vb
;
103 struct list_head list
;
107 #define POWER_ON (1 << 1)
108 #define URB_BUF (1 << 2)
109 #define USB_STATE_URB_BUF (1 << 3)
113 struct usb_device
*udev
;
114 struct video_device vdev
;
115 struct v4l2_device v4l2_dev
;
117 /* videobuf2 queue and queued buffers list */
118 struct vb2_queue vb_queue
;
119 struct list_head queued_bufs
;
120 spinlock_t queued_bufs_lock
; /* Protects queued_bufs */
121 unsigned sequence
; /* Buffer sequence counter */
122 unsigned int vb_full
; /* vb is full and packets dropped */
124 /* Note if taking both locks v4l2_lock must always be locked first! */
125 struct mutex v4l2_lock
; /* Protects everything else */
126 struct mutex vb_queue_lock
; /* Protects vb_queue and capt_file */
128 struct urb
*urb_list
[MAX_BULK_BUFS
];
130 unsigned long buf_size
;
131 u8
*buf_list
[MAX_BULK_BUFS
];
132 dma_addr_t dma_addr
[MAX_BULK_BUFS
];
133 int urbs_initialized
;
136 /* USB control message buffer */
140 /* Current configuration */
147 struct v4l2_ctrl_handler hdl
;
148 struct v4l2_ctrl
*lna_gain_auto
;
149 struct v4l2_ctrl
*lna_gain
;
150 struct v4l2_ctrl
*mixer_gain_auto
;
151 struct v4l2_ctrl
*mixer_gain
;
152 struct v4l2_ctrl
*if_gain
;
154 /* Sample rate calc */
155 unsigned long jiffies_next
;
157 unsigned int sample_measured
;
160 #define airspy_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
162 if (_t & USB_DIR_IN) \
163 _direction = "<<<"; \
165 _direction = ">>>"; \
166 dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
167 _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \
168 _l & 0xff, _l >> 8, _direction, _l, _b); \
171 /* execute firmware command */
172 static int airspy_ctrl_msg(struct airspy
*s
, u8 request
, u16 value
, u16 index
,
180 case CMD_RECEIVER_MODE
:
182 pipe
= usb_sndctrlpipe(s
->udev
, 0);
183 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
185 case CMD_BOARD_ID_READ
:
186 case CMD_VERSION_STRING_READ
:
187 case CMD_BOARD_PARTID_SERIALNO_READ
:
188 case CMD_SET_LNA_GAIN
:
189 case CMD_SET_MIXER_GAIN
:
190 case CMD_SET_VGA_GAIN
:
191 case CMD_SET_LNA_AGC
:
192 case CMD_SET_MIXER_AGC
:
193 pipe
= usb_rcvctrlpipe(s
->udev
, 0);
194 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
197 dev_err(s
->dev
, "Unknown command %02x\n", request
);
203 if (!(requesttype
& USB_DIR_IN
))
204 memcpy(s
->buf
, data
, size
);
206 ret
= usb_control_msg(s
->udev
, pipe
, request
, requesttype
, value
,
207 index
, s
->buf
, size
, 1000);
208 airspy_dbg_usb_control_msg(s
->dev
, request
, requesttype
, value
,
209 index
, s
->buf
, size
);
211 dev_err(s
->dev
, "usb_control_msg() failed %d request %02x\n",
217 if (requesttype
& USB_DIR_IN
)
218 memcpy(data
, s
->buf
, size
);
225 /* Private functions */
226 static struct airspy_frame_buf
*airspy_get_next_fill_buf(struct airspy
*s
)
229 struct airspy_frame_buf
*buf
= NULL
;
231 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
232 if (list_empty(&s
->queued_bufs
))
235 buf
= list_entry(s
->queued_bufs
.next
,
236 struct airspy_frame_buf
, list
);
237 list_del(&buf
->list
);
239 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
243 static unsigned int airspy_convert_stream(struct airspy
*s
,
244 void *dst
, void *src
, unsigned int src_len
)
246 unsigned int dst_len
;
248 if (s
->pixelformat
== V4L2_SDR_FMT_RU12LE
) {
249 memcpy(dst
, src
, src_len
);
255 /* calculate sample rate and output it in 10 seconds intervals */
256 if (unlikely(time_is_before_jiffies(s
->jiffies_next
))) {
257 #define MSECS 10000UL
258 unsigned int msecs
= jiffies_to_msecs(jiffies
-
259 s
->jiffies_next
+ msecs_to_jiffies(MSECS
));
260 unsigned int samples
= s
->sample
- s
->sample_measured
;
262 s
->jiffies_next
= jiffies
+ msecs_to_jiffies(MSECS
);
263 s
->sample_measured
= s
->sample
;
264 dev_dbg(s
->dev
, "slen=%u samples=%u msecs=%u sample rate=%lu\n",
265 src_len
, samples
, msecs
,
266 samples
* 1000UL / msecs
);
269 /* total number of samples */
270 s
->sample
+= src_len
/ 2;
276 * This gets called for the bulk stream pipe. This is done in interrupt
277 * time, so it has to be fast, not crash, and not stall. Neat.
279 static void airspy_urb_complete(struct urb
*urb
)
281 struct airspy
*s
= urb
->context
;
282 struct airspy_frame_buf
*fbuf
;
284 dev_dbg_ratelimited(s
->dev
, "status=%d length=%d/%d errors=%d\n",
285 urb
->status
, urb
->actual_length
,
286 urb
->transfer_buffer_length
, urb
->error_count
);
288 switch (urb
->status
) {
289 case 0: /* success */
290 case -ETIMEDOUT
: /* NAK */
292 case -ECONNRESET
: /* kill */
297 dev_err_ratelimited(s
->dev
, "URB failed %d\n", urb
->status
);
301 if (likely(urb
->actual_length
> 0)) {
304 /* get free framebuffer */
305 fbuf
= airspy_get_next_fill_buf(s
);
306 if (unlikely(fbuf
== NULL
)) {
308 dev_notice_ratelimited(s
->dev
,
309 "videobuf is full, %d packets dropped\n",
314 /* fill framebuffer */
315 ptr
= vb2_plane_vaddr(&fbuf
->vb
.vb2_buf
, 0);
316 len
= airspy_convert_stream(s
, ptr
, urb
->transfer_buffer
,
318 vb2_set_plane_payload(&fbuf
->vb
.vb2_buf
, 0, len
);
319 v4l2_get_timestamp(&fbuf
->vb
.timestamp
);
320 fbuf
->vb
.sequence
= s
->sequence
++;
321 vb2_buffer_done(&fbuf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
324 usb_submit_urb(urb
, GFP_ATOMIC
);
327 static int airspy_kill_urbs(struct airspy
*s
)
331 for (i
= s
->urbs_submitted
- 1; i
>= 0; i
--) {
332 dev_dbg(s
->dev
, "kill urb=%d\n", i
);
334 usb_kill_urb(s
->urb_list
[i
]);
336 s
->urbs_submitted
= 0;
341 static int airspy_submit_urbs(struct airspy
*s
)
345 for (i
= 0; i
< s
->urbs_initialized
; i
++) {
346 dev_dbg(s
->dev
, "submit urb=%d\n", i
);
347 ret
= usb_submit_urb(s
->urb_list
[i
], GFP_ATOMIC
);
349 dev_err(s
->dev
, "Could not submit URB no. %d - get them all back\n",
360 static int airspy_free_stream_bufs(struct airspy
*s
)
362 if (s
->flags
& USB_STATE_URB_BUF
) {
365 dev_dbg(s
->dev
, "free buf=%d\n", s
->buf_num
);
366 usb_free_coherent(s
->udev
, s
->buf_size
,
367 s
->buf_list
[s
->buf_num
],
368 s
->dma_addr
[s
->buf_num
]);
371 s
->flags
&= ~USB_STATE_URB_BUF
;
376 static int airspy_alloc_stream_bufs(struct airspy
*s
)
379 s
->buf_size
= BULK_BUFFER_SIZE
;
381 dev_dbg(s
->dev
, "all in all I will use %u bytes for streaming\n",
382 MAX_BULK_BUFS
* BULK_BUFFER_SIZE
);
384 for (s
->buf_num
= 0; s
->buf_num
< MAX_BULK_BUFS
; s
->buf_num
++) {
385 s
->buf_list
[s
->buf_num
] = usb_alloc_coherent(s
->udev
,
386 BULK_BUFFER_SIZE
, GFP_ATOMIC
,
387 &s
->dma_addr
[s
->buf_num
]);
388 if (!s
->buf_list
[s
->buf_num
]) {
389 dev_dbg(s
->dev
, "alloc buf=%d failed\n", s
->buf_num
);
390 airspy_free_stream_bufs(s
);
394 dev_dbg(s
->dev
, "alloc buf=%d %p (dma %llu)\n", s
->buf_num
,
395 s
->buf_list
[s
->buf_num
],
396 (long long)s
->dma_addr
[s
->buf_num
]);
397 s
->flags
|= USB_STATE_URB_BUF
;
403 static int airspy_free_urbs(struct airspy
*s
)
409 for (i
= s
->urbs_initialized
- 1; i
>= 0; i
--) {
410 if (s
->urb_list
[i
]) {
411 dev_dbg(s
->dev
, "free urb=%d\n", i
);
413 usb_free_urb(s
->urb_list
[i
]);
416 s
->urbs_initialized
= 0;
421 static int airspy_alloc_urbs(struct airspy
*s
)
425 /* allocate the URBs */
426 for (i
= 0; i
< MAX_BULK_BUFS
; i
++) {
427 dev_dbg(s
->dev
, "alloc urb=%d\n", i
);
428 s
->urb_list
[i
] = usb_alloc_urb(0, GFP_ATOMIC
);
429 if (!s
->urb_list
[i
]) {
430 dev_dbg(s
->dev
, "failed\n");
431 for (j
= 0; j
< i
; j
++)
432 usb_free_urb(s
->urb_list
[j
]);
435 usb_fill_bulk_urb(s
->urb_list
[i
],
437 usb_rcvbulkpipe(s
->udev
, 0x81),
440 airspy_urb_complete
, s
);
442 s
->urb_list
[i
]->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
443 s
->urb_list
[i
]->transfer_dma
= s
->dma_addr
[i
];
444 s
->urbs_initialized
++;
450 /* Must be called with vb_queue_lock hold */
451 static void airspy_cleanup_queued_bufs(struct airspy
*s
)
455 dev_dbg(s
->dev
, "\n");
457 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
458 while (!list_empty(&s
->queued_bufs
)) {
459 struct airspy_frame_buf
*buf
;
461 buf
= list_entry(s
->queued_bufs
.next
,
462 struct airspy_frame_buf
, list
);
463 list_del(&buf
->list
);
464 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
466 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
469 /* The user yanked out the cable... */
470 static void airspy_disconnect(struct usb_interface
*intf
)
472 struct v4l2_device
*v
= usb_get_intfdata(intf
);
473 struct airspy
*s
= container_of(v
, struct airspy
, v4l2_dev
);
475 dev_dbg(s
->dev
, "\n");
477 mutex_lock(&s
->vb_queue_lock
);
478 mutex_lock(&s
->v4l2_lock
);
479 /* No need to keep the urbs around after disconnection */
481 v4l2_device_disconnect(&s
->v4l2_dev
);
482 video_unregister_device(&s
->vdev
);
483 mutex_unlock(&s
->v4l2_lock
);
484 mutex_unlock(&s
->vb_queue_lock
);
486 v4l2_device_put(&s
->v4l2_dev
);
489 /* Videobuf2 operations */
490 static int airspy_queue_setup(struct vb2_queue
*vq
,
491 const void *parg
, unsigned int *nbuffers
,
492 unsigned int *nplanes
, unsigned int sizes
[], void *alloc_ctxs
[])
494 struct airspy
*s
= vb2_get_drv_priv(vq
);
496 dev_dbg(s
->dev
, "nbuffers=%d\n", *nbuffers
);
498 /* Need at least 8 buffers */
499 if (vq
->num_buffers
+ *nbuffers
< 8)
500 *nbuffers
= 8 - vq
->num_buffers
;
502 sizes
[0] = PAGE_ALIGN(s
->buffersize
);
504 dev_dbg(s
->dev
, "nbuffers=%d sizes[0]=%d\n", *nbuffers
, sizes
[0]);
508 static void airspy_buf_queue(struct vb2_buffer
*vb
)
510 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
511 struct airspy
*s
= vb2_get_drv_priv(vb
->vb2_queue
);
512 struct airspy_frame_buf
*buf
=
513 container_of(vbuf
, struct airspy_frame_buf
, vb
);
516 /* Check the device has not disconnected between prep and queuing */
517 if (unlikely(!s
->udev
)) {
518 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
522 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
523 list_add_tail(&buf
->list
, &s
->queued_bufs
);
524 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
527 static int airspy_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
529 struct airspy
*s
= vb2_get_drv_priv(vq
);
532 dev_dbg(s
->dev
, "\n");
537 mutex_lock(&s
->v4l2_lock
);
541 set_bit(POWER_ON
, &s
->flags
);
543 ret
= airspy_alloc_stream_bufs(s
);
547 ret
= airspy_alloc_urbs(s
);
549 goto err_free_stream_bufs
;
551 ret
= airspy_submit_urbs(s
);
555 /* start hardware streaming */
556 ret
= airspy_ctrl_msg(s
, CMD_RECEIVER_MODE
, 1, 0, NULL
, 0);
560 goto exit_mutex_unlock
;
566 err_free_stream_bufs
:
567 airspy_free_stream_bufs(s
);
569 clear_bit(POWER_ON
, &s
->flags
);
571 /* return all queued buffers to vb2 */
573 struct airspy_frame_buf
*buf
, *tmp
;
575 list_for_each_entry_safe(buf
, tmp
, &s
->queued_bufs
, list
) {
576 list_del(&buf
->list
);
577 vb2_buffer_done(&buf
->vb
.vb2_buf
,
578 VB2_BUF_STATE_QUEUED
);
583 mutex_unlock(&s
->v4l2_lock
);
588 static void airspy_stop_streaming(struct vb2_queue
*vq
)
590 struct airspy
*s
= vb2_get_drv_priv(vq
);
592 dev_dbg(s
->dev
, "\n");
594 mutex_lock(&s
->v4l2_lock
);
596 /* stop hardware streaming */
597 airspy_ctrl_msg(s
, CMD_RECEIVER_MODE
, 0, 0, NULL
, 0);
601 airspy_free_stream_bufs(s
);
603 airspy_cleanup_queued_bufs(s
);
605 clear_bit(POWER_ON
, &s
->flags
);
607 mutex_unlock(&s
->v4l2_lock
);
610 static struct vb2_ops airspy_vb2_ops
= {
611 .queue_setup
= airspy_queue_setup
,
612 .buf_queue
= airspy_buf_queue
,
613 .start_streaming
= airspy_start_streaming
,
614 .stop_streaming
= airspy_stop_streaming
,
615 .wait_prepare
= vb2_ops_wait_prepare
,
616 .wait_finish
= vb2_ops_wait_finish
,
619 static int airspy_querycap(struct file
*file
, void *fh
,
620 struct v4l2_capability
*cap
)
622 struct airspy
*s
= video_drvdata(file
);
624 strlcpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
625 strlcpy(cap
->card
, s
->vdev
.name
, sizeof(cap
->card
));
626 usb_make_path(s
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
627 cap
->device_caps
= V4L2_CAP_SDR_CAPTURE
| V4L2_CAP_STREAMING
|
628 V4L2_CAP_READWRITE
| V4L2_CAP_TUNER
;
629 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
634 static int airspy_enum_fmt_sdr_cap(struct file
*file
, void *priv
,
635 struct v4l2_fmtdesc
*f
)
637 if (f
->index
>= NUM_FORMATS
)
640 strlcpy(f
->description
, formats
[f
->index
].name
, sizeof(f
->description
));
641 f
->pixelformat
= formats
[f
->index
].pixelformat
;
646 static int airspy_g_fmt_sdr_cap(struct file
*file
, void *priv
,
647 struct v4l2_format
*f
)
649 struct airspy
*s
= video_drvdata(file
);
651 f
->fmt
.sdr
.pixelformat
= s
->pixelformat
;
652 f
->fmt
.sdr
.buffersize
= s
->buffersize
;
653 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
658 static int airspy_s_fmt_sdr_cap(struct file
*file
, void *priv
,
659 struct v4l2_format
*f
)
661 struct airspy
*s
= video_drvdata(file
);
662 struct vb2_queue
*q
= &s
->vb_queue
;
668 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
669 for (i
= 0; i
< NUM_FORMATS
; i
++) {
670 if (formats
[i
].pixelformat
== f
->fmt
.sdr
.pixelformat
) {
671 s
->pixelformat
= formats
[i
].pixelformat
;
672 s
->buffersize
= formats
[i
].buffersize
;
673 f
->fmt
.sdr
.buffersize
= formats
[i
].buffersize
;
678 s
->pixelformat
= formats
[0].pixelformat
;
679 s
->buffersize
= formats
[0].buffersize
;
680 f
->fmt
.sdr
.pixelformat
= formats
[0].pixelformat
;
681 f
->fmt
.sdr
.buffersize
= formats
[0].buffersize
;
686 static int airspy_try_fmt_sdr_cap(struct file
*file
, void *priv
,
687 struct v4l2_format
*f
)
691 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
692 for (i
= 0; i
< NUM_FORMATS
; i
++) {
693 if (formats
[i
].pixelformat
== f
->fmt
.sdr
.pixelformat
) {
694 f
->fmt
.sdr
.buffersize
= formats
[i
].buffersize
;
699 f
->fmt
.sdr
.pixelformat
= formats
[0].pixelformat
;
700 f
->fmt
.sdr
.buffersize
= formats
[0].buffersize
;
705 static int airspy_s_tuner(struct file
*file
, void *priv
,
706 const struct v4l2_tuner
*v
)
712 else if (v
->index
== 1)
720 static int airspy_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*v
)
725 strlcpy(v
->name
, "AirSpy ADC", sizeof(v
->name
));
726 v
->type
= V4L2_TUNER_ADC
;
727 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
728 v
->rangelow
= bands
[0].rangelow
;
729 v
->rangehigh
= bands
[0].rangehigh
;
731 } else if (v
->index
== 1) {
732 strlcpy(v
->name
, "AirSpy RF", sizeof(v
->name
));
733 v
->type
= V4L2_TUNER_RF
;
734 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
735 v
->rangelow
= bands_rf
[0].rangelow
;
736 v
->rangehigh
= bands_rf
[0].rangehigh
;
745 static int airspy_g_frequency(struct file
*file
, void *priv
,
746 struct v4l2_frequency
*f
)
748 struct airspy
*s
= video_drvdata(file
);
752 f
->type
= V4L2_TUNER_ADC
;
753 f
->frequency
= s
->f_adc
;
754 dev_dbg(s
->dev
, "ADC frequency=%u Hz\n", s
->f_adc
);
756 } else if (f
->tuner
== 1) {
757 f
->type
= V4L2_TUNER_RF
;
758 f
->frequency
= s
->f_rf
;
759 dev_dbg(s
->dev
, "RF frequency=%u Hz\n", s
->f_rf
);
768 static int airspy_s_frequency(struct file
*file
, void *priv
,
769 const struct v4l2_frequency
*f
)
771 struct airspy
*s
= video_drvdata(file
);
776 s
->f_adc
= clamp_t(unsigned int, f
->frequency
,
779 dev_dbg(s
->dev
, "ADC frequency=%u Hz\n", s
->f_adc
);
781 } else if (f
->tuner
== 1) {
782 s
->f_rf
= clamp_t(unsigned int, f
->frequency
,
783 bands_rf
[0].rangelow
,
784 bands_rf
[0].rangehigh
);
785 dev_dbg(s
->dev
, "RF frequency=%u Hz\n", s
->f_rf
);
786 buf
[0] = (s
->f_rf
>> 0) & 0xff;
787 buf
[1] = (s
->f_rf
>> 8) & 0xff;
788 buf
[2] = (s
->f_rf
>> 16) & 0xff;
789 buf
[3] = (s
->f_rf
>> 24) & 0xff;
790 ret
= airspy_ctrl_msg(s
, CMD_SET_FREQ
, 0, 0, buf
, 4);
798 static int airspy_enum_freq_bands(struct file
*file
, void *priv
,
799 struct v4l2_frequency_band
*band
)
803 if (band
->tuner
== 0) {
804 if (band
->index
>= ARRAY_SIZE(bands
)) {
807 *band
= bands
[band
->index
];
810 } else if (band
->tuner
== 1) {
811 if (band
->index
>= ARRAY_SIZE(bands_rf
)) {
814 *band
= bands_rf
[band
->index
];
824 static const struct v4l2_ioctl_ops airspy_ioctl_ops
= {
825 .vidioc_querycap
= airspy_querycap
,
827 .vidioc_enum_fmt_sdr_cap
= airspy_enum_fmt_sdr_cap
,
828 .vidioc_g_fmt_sdr_cap
= airspy_g_fmt_sdr_cap
,
829 .vidioc_s_fmt_sdr_cap
= airspy_s_fmt_sdr_cap
,
830 .vidioc_try_fmt_sdr_cap
= airspy_try_fmt_sdr_cap
,
832 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
833 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
834 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
835 .vidioc_querybuf
= vb2_ioctl_querybuf
,
836 .vidioc_qbuf
= vb2_ioctl_qbuf
,
837 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
839 .vidioc_streamon
= vb2_ioctl_streamon
,
840 .vidioc_streamoff
= vb2_ioctl_streamoff
,
842 .vidioc_g_tuner
= airspy_g_tuner
,
843 .vidioc_s_tuner
= airspy_s_tuner
,
845 .vidioc_g_frequency
= airspy_g_frequency
,
846 .vidioc_s_frequency
= airspy_s_frequency
,
847 .vidioc_enum_freq_bands
= airspy_enum_freq_bands
,
849 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
850 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
851 .vidioc_log_status
= v4l2_ctrl_log_status
,
854 static const struct v4l2_file_operations airspy_fops
= {
855 .owner
= THIS_MODULE
,
856 .open
= v4l2_fh_open
,
857 .release
= vb2_fop_release
,
858 .read
= vb2_fop_read
,
859 .poll
= vb2_fop_poll
,
860 .mmap
= vb2_fop_mmap
,
861 .unlocked_ioctl
= video_ioctl2
,
864 static struct video_device airspy_template
= {
865 .name
= "AirSpy SDR",
866 .release
= video_device_release_empty
,
867 .fops
= &airspy_fops
,
868 .ioctl_ops
= &airspy_ioctl_ops
,
871 static void airspy_video_release(struct v4l2_device
*v
)
873 struct airspy
*s
= container_of(v
, struct airspy
, v4l2_dev
);
875 v4l2_ctrl_handler_free(&s
->hdl
);
876 v4l2_device_unregister(&s
->v4l2_dev
);
880 static int airspy_set_lna_gain(struct airspy
*s
)
885 dev_dbg(s
->dev
, "lna auto=%d->%d val=%d->%d\n",
886 s
->lna_gain_auto
->cur
.val
, s
->lna_gain_auto
->val
,
887 s
->lna_gain
->cur
.val
, s
->lna_gain
->val
);
889 ret
= airspy_ctrl_msg(s
, CMD_SET_LNA_AGC
, 0, s
->lna_gain_auto
->val
,
894 if (s
->lna_gain_auto
->val
== false) {
895 ret
= airspy_ctrl_msg(s
, CMD_SET_LNA_GAIN
, 0, s
->lna_gain
->val
,
902 dev_dbg(s
->dev
, "failed=%d\n", ret
);
907 static int airspy_set_mixer_gain(struct airspy
*s
)
912 dev_dbg(s
->dev
, "mixer auto=%d->%d val=%d->%d\n",
913 s
->mixer_gain_auto
->cur
.val
, s
->mixer_gain_auto
->val
,
914 s
->mixer_gain
->cur
.val
, s
->mixer_gain
->val
);
916 ret
= airspy_ctrl_msg(s
, CMD_SET_MIXER_AGC
, 0, s
->mixer_gain_auto
->val
,
921 if (s
->mixer_gain_auto
->val
== false) {
922 ret
= airspy_ctrl_msg(s
, CMD_SET_MIXER_GAIN
, 0,
923 s
->mixer_gain
->val
, &u8tmp
, 1);
929 dev_dbg(s
->dev
, "failed=%d\n", ret
);
934 static int airspy_set_if_gain(struct airspy
*s
)
939 dev_dbg(s
->dev
, "val=%d->%d\n", s
->if_gain
->cur
.val
, s
->if_gain
->val
);
941 ret
= airspy_ctrl_msg(s
, CMD_SET_VGA_GAIN
, 0, s
->if_gain
->val
,
944 dev_dbg(s
->dev
, "failed=%d\n", ret
);
949 static int airspy_s_ctrl(struct v4l2_ctrl
*ctrl
)
951 struct airspy
*s
= container_of(ctrl
->handler
, struct airspy
, hdl
);
955 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
:
956 case V4L2_CID_RF_TUNER_LNA_GAIN
:
957 ret
= airspy_set_lna_gain(s
);
959 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
:
960 case V4L2_CID_RF_TUNER_MIXER_GAIN
:
961 ret
= airspy_set_mixer_gain(s
);
963 case V4L2_CID_RF_TUNER_IF_GAIN
:
964 ret
= airspy_set_if_gain(s
);
967 dev_dbg(s
->dev
, "unknown ctrl: id=%d name=%s\n",
968 ctrl
->id
, ctrl
->name
);
975 static const struct v4l2_ctrl_ops airspy_ctrl_ops
= {
976 .s_ctrl
= airspy_s_ctrl
,
979 static int airspy_probe(struct usb_interface
*intf
,
980 const struct usb_device_id
*id
)
984 u8 u8tmp
, buf
[BUF_SIZE
];
986 s
= kzalloc(sizeof(struct airspy
), GFP_KERNEL
);
988 dev_err(&intf
->dev
, "Could not allocate memory for state\n");
992 mutex_init(&s
->v4l2_lock
);
993 mutex_init(&s
->vb_queue_lock
);
994 spin_lock_init(&s
->queued_bufs_lock
);
995 INIT_LIST_HEAD(&s
->queued_bufs
);
997 s
->udev
= interface_to_usbdev(intf
);
998 s
->f_adc
= bands
[0].rangelow
;
999 s
->f_rf
= bands_rf
[0].rangelow
;
1000 s
->pixelformat
= formats
[0].pixelformat
;
1001 s
->buffersize
= formats
[0].buffersize
;
1004 ret
= airspy_ctrl_msg(s
, CMD_BOARD_ID_READ
, 0, 0, &u8tmp
, 1);
1006 ret
= airspy_ctrl_msg(s
, CMD_VERSION_STRING_READ
, 0, 0,
1009 dev_err(s
->dev
, "Could not detect board\n");
1013 buf
[BUF_SIZE
- 1] = '\0';
1015 dev_info(s
->dev
, "Board ID: %02x\n", u8tmp
);
1016 dev_info(s
->dev
, "Firmware version: %s\n", buf
);
1018 /* Init videobuf2 queue structure */
1019 s
->vb_queue
.type
= V4L2_BUF_TYPE_SDR_CAPTURE
;
1020 s
->vb_queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
1021 s
->vb_queue
.drv_priv
= s
;
1022 s
->vb_queue
.buf_struct_size
= sizeof(struct airspy_frame_buf
);
1023 s
->vb_queue
.ops
= &airspy_vb2_ops
;
1024 s
->vb_queue
.mem_ops
= &vb2_vmalloc_memops
;
1025 s
->vb_queue
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1026 ret
= vb2_queue_init(&s
->vb_queue
);
1028 dev_err(s
->dev
, "Could not initialize vb2 queue\n");
1032 /* Init video_device structure */
1033 s
->vdev
= airspy_template
;
1034 s
->vdev
.queue
= &s
->vb_queue
;
1035 s
->vdev
.queue
->lock
= &s
->vb_queue_lock
;
1036 video_set_drvdata(&s
->vdev
, s
);
1038 /* Register the v4l2_device structure */
1039 s
->v4l2_dev
.release
= airspy_video_release
;
1040 ret
= v4l2_device_register(&intf
->dev
, &s
->v4l2_dev
);
1042 dev_err(s
->dev
, "Failed to register v4l2-device (%d)\n", ret
);
1046 /* Register controls */
1047 v4l2_ctrl_handler_init(&s
->hdl
, 5);
1048 s
->lna_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1049 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
, 0, 1, 1, 0);
1050 s
->lna_gain
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1051 V4L2_CID_RF_TUNER_LNA_GAIN
, 0, 14, 1, 8);
1052 v4l2_ctrl_auto_cluster(2, &s
->lna_gain_auto
, 0, false);
1053 s
->mixer_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1054 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
, 0, 1, 1, 0);
1055 s
->mixer_gain
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1056 V4L2_CID_RF_TUNER_MIXER_GAIN
, 0, 15, 1, 8);
1057 v4l2_ctrl_auto_cluster(2, &s
->mixer_gain_auto
, 0, false);
1058 s
->if_gain
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1059 V4L2_CID_RF_TUNER_IF_GAIN
, 0, 15, 1, 0);
1062 dev_err(s
->dev
, "Could not initialize controls\n");
1063 goto err_free_controls
;
1066 v4l2_ctrl_handler_setup(&s
->hdl
);
1068 s
->v4l2_dev
.ctrl_handler
= &s
->hdl
;
1069 s
->vdev
.v4l2_dev
= &s
->v4l2_dev
;
1070 s
->vdev
.lock
= &s
->v4l2_lock
;
1072 ret
= video_register_device(&s
->vdev
, VFL_TYPE_SDR
, -1);
1074 dev_err(s
->dev
, "Failed to register as video device (%d)\n",
1076 goto err_unregister_v4l2_dev
;
1078 dev_info(s
->dev
, "Registered as %s\n",
1079 video_device_node_name(&s
->vdev
));
1080 dev_notice(s
->dev
, "SDR API is still slightly experimental and functionality changes may follow\n");
1084 v4l2_ctrl_handler_free(&s
->hdl
);
1085 err_unregister_v4l2_dev
:
1086 v4l2_device_unregister(&s
->v4l2_dev
);
1092 /* USB device ID list */
1093 static struct usb_device_id airspy_id_table
[] = {
1094 { USB_DEVICE(0x1d50, 0x60a1) }, /* AirSpy */
1097 MODULE_DEVICE_TABLE(usb
, airspy_id_table
);
1099 /* USB subsystem interface */
1100 static struct usb_driver airspy_driver
= {
1101 .name
= KBUILD_MODNAME
,
1102 .probe
= airspy_probe
,
1103 .disconnect
= airspy_disconnect
,
1104 .id_table
= airspy_id_table
,
1107 module_usb_driver(airspy_driver
);
1109 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1110 MODULE_DESCRIPTION("AirSpy SDR");
1111 MODULE_LICENSE("GPL");