1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/usb.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-ioctl.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-event.h>
15 #include <media/videobuf2-v4l2.h>
16 #include <media/videobuf2-vmalloc.h>
18 /* AirSpy USB API commands (from AirSpy Library) */
21 CMD_RECEIVER_MODE
= 0x01,
22 CMD_SI5351C_WRITE
= 0x02,
23 CMD_SI5351C_READ
= 0x03,
24 CMD_R820T_WRITE
= 0x04,
25 CMD_R820T_READ
= 0x05,
26 CMD_SPIFLASH_ERASE
= 0x06,
27 CMD_SPIFLASH_WRITE
= 0x07,
28 CMD_SPIFLASH_READ
= 0x08,
29 CMD_BOARD_ID_READ
= 0x09,
30 CMD_VERSION_STRING_READ
= 0x0a,
31 CMD_BOARD_PARTID_SERIALNO_READ
= 0x0b,
32 CMD_SET_SAMPLE_RATE
= 0x0c,
34 CMD_SET_LNA_GAIN
= 0x0e,
35 CMD_SET_MIXER_GAIN
= 0x0f,
36 CMD_SET_VGA_GAIN
= 0x10,
37 CMD_SET_LNA_AGC
= 0x11,
38 CMD_SET_MIXER_AGC
= 0x12,
39 CMD_SET_PACKING
= 0x13,
43 * bEndpointAddress 0x81 EP 1 IN
45 * wMaxPacketSize 0x0200 1x 512 bytes
47 #define MAX_BULK_BUFS (6)
48 #define BULK_BUFFER_SIZE (128 * 512)
50 static const struct v4l2_frequency_band bands
[] = {
53 .type
= V4L2_TUNER_ADC
,
55 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
57 .rangehigh
= 20000000,
61 static const struct v4l2_frequency_band bands_rf
[] = {
64 .type
= V4L2_TUNER_RF
,
66 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
68 .rangehigh
= 1750000000,
73 struct airspy_format
{
78 /* format descriptions for capture and preview */
79 static struct airspy_format formats
[] = {
81 .pixelformat
= V4L2_SDR_FMT_RU12LE
,
82 .buffersize
= BULK_BUFFER_SIZE
,
86 static const unsigned int NUM_FORMATS
= ARRAY_SIZE(formats
);
88 /* intermediate buffers with raw data from the USB device */
89 struct airspy_frame_buf
{
90 /* common v4l buffer stuff -- must be first */
91 struct vb2_v4l2_buffer vb
;
92 struct list_head list
;
97 #define USB_STATE_URB_BUF 2
101 struct usb_device
*udev
;
102 struct video_device vdev
;
103 struct v4l2_device v4l2_dev
;
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 */
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 and capt_file */
116 struct urb
*urb_list
[MAX_BULK_BUFS
];
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
;
124 /* USB control message buffer */
128 /* Current configuration */
135 struct v4l2_ctrl_handler hdl
;
136 struct v4l2_ctrl
*lna_gain_auto
;
137 struct v4l2_ctrl
*lna_gain
;
138 struct v4l2_ctrl
*mixer_gain_auto
;
139 struct v4l2_ctrl
*mixer_gain
;
140 struct v4l2_ctrl
*if_gain
;
142 /* Sample rate calc */
143 unsigned long jiffies_next
;
145 unsigned int sample_measured
;
148 #define airspy_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
150 if (_t & USB_DIR_IN) \
151 _direction = "<<<"; \
153 _direction = ">>>"; \
154 dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
155 _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \
156 _l & 0xff, _l >> 8, _direction, _l, _b); \
159 /* execute firmware command */
160 static int airspy_ctrl_msg(struct airspy
*s
, u8 request
, u16 value
, u16 index
,
168 case CMD_RECEIVER_MODE
:
170 pipe
= usb_sndctrlpipe(s
->udev
, 0);
171 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
173 case CMD_BOARD_ID_READ
:
174 case CMD_VERSION_STRING_READ
:
175 case CMD_BOARD_PARTID_SERIALNO_READ
:
176 case CMD_SET_LNA_GAIN
:
177 case CMD_SET_MIXER_GAIN
:
178 case CMD_SET_VGA_GAIN
:
179 case CMD_SET_LNA_AGC
:
180 case CMD_SET_MIXER_AGC
:
181 pipe
= usb_rcvctrlpipe(s
->udev
, 0);
182 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
185 dev_err(s
->dev
, "Unknown command %02x\n", request
);
191 if (!(requesttype
& USB_DIR_IN
))
192 memcpy(s
->buf
, data
, size
);
194 ret
= usb_control_msg(s
->udev
, pipe
, request
, requesttype
, value
,
195 index
, s
->buf
, size
, 1000);
196 airspy_dbg_usb_control_msg(s
->dev
, request
, requesttype
, value
,
197 index
, s
->buf
, size
);
199 dev_err(s
->dev
, "usb_control_msg() failed %d request %02x\n",
205 if (requesttype
& USB_DIR_IN
)
206 memcpy(data
, s
->buf
, size
);
213 /* Private functions */
214 static struct airspy_frame_buf
*airspy_get_next_fill_buf(struct airspy
*s
)
217 struct airspy_frame_buf
*buf
= NULL
;
219 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
220 if (list_empty(&s
->queued_bufs
))
223 buf
= list_entry(s
->queued_bufs
.next
,
224 struct airspy_frame_buf
, list
);
225 list_del(&buf
->list
);
227 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
231 static unsigned int airspy_convert_stream(struct airspy
*s
,
232 void *dst
, void *src
, unsigned int src_len
)
234 unsigned int dst_len
;
236 if (s
->pixelformat
== V4L2_SDR_FMT_RU12LE
) {
237 memcpy(dst
, src
, src_len
);
243 /* calculate sample rate and output it in 10 seconds intervals */
244 if (unlikely(time_is_before_jiffies(s
->jiffies_next
))) {
245 #define MSECS 10000UL
246 unsigned int msecs
= jiffies_to_msecs(jiffies
-
247 s
->jiffies_next
+ msecs_to_jiffies(MSECS
));
248 unsigned int samples
= s
->sample
- s
->sample_measured
;
250 s
->jiffies_next
= jiffies
+ msecs_to_jiffies(MSECS
);
251 s
->sample_measured
= s
->sample
;
252 dev_dbg(s
->dev
, "slen=%u samples=%u msecs=%u sample rate=%lu\n",
253 src_len
, samples
, msecs
,
254 samples
* 1000UL / msecs
);
257 /* total number of samples */
258 s
->sample
+= src_len
/ 2;
264 * This gets called for the bulk stream pipe. This is done in interrupt
265 * time, so it has to be fast, not crash, and not stall. Neat.
267 static void airspy_urb_complete(struct urb
*urb
)
269 struct airspy
*s
= urb
->context
;
270 struct airspy_frame_buf
*fbuf
;
272 dev_dbg_ratelimited(s
->dev
, "status=%d length=%d/%d errors=%d\n",
273 urb
->status
, urb
->actual_length
,
274 urb
->transfer_buffer_length
, urb
->error_count
);
276 switch (urb
->status
) {
277 case 0: /* success */
278 case -ETIMEDOUT
: /* NAK */
280 case -ECONNRESET
: /* kill */
285 dev_err_ratelimited(s
->dev
, "URB failed %d\n", urb
->status
);
289 if (likely(urb
->actual_length
> 0)) {
292 /* get free framebuffer */
293 fbuf
= airspy_get_next_fill_buf(s
);
294 if (unlikely(fbuf
== NULL
)) {
296 dev_notice_ratelimited(s
->dev
,
297 "videobuf is full, %d packets dropped\n",
302 /* fill framebuffer */
303 ptr
= vb2_plane_vaddr(&fbuf
->vb
.vb2_buf
, 0);
304 len
= airspy_convert_stream(s
, ptr
, urb
->transfer_buffer
,
306 vb2_set_plane_payload(&fbuf
->vb
.vb2_buf
, 0, len
);
307 fbuf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
308 fbuf
->vb
.sequence
= s
->sequence
++;
309 vb2_buffer_done(&fbuf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
312 usb_submit_urb(urb
, GFP_ATOMIC
);
315 static int airspy_kill_urbs(struct airspy
*s
)
319 for (i
= s
->urbs_submitted
- 1; i
>= 0; i
--) {
320 dev_dbg(s
->dev
, "kill urb=%d\n", i
);
322 usb_kill_urb(s
->urb_list
[i
]);
324 s
->urbs_submitted
= 0;
329 static int airspy_submit_urbs(struct airspy
*s
)
333 for (i
= 0; i
< s
->urbs_initialized
; i
++) {
334 dev_dbg(s
->dev
, "submit urb=%d\n", i
);
335 ret
= usb_submit_urb(s
->urb_list
[i
], GFP_ATOMIC
);
337 dev_err(s
->dev
, "Could not submit URB no. %d - get them all back\n",
348 static int airspy_free_stream_bufs(struct airspy
*s
)
350 if (test_bit(USB_STATE_URB_BUF
, &s
->flags
)) {
353 dev_dbg(s
->dev
, "free buf=%d\n", s
->buf_num
);
354 usb_free_coherent(s
->udev
, s
->buf_size
,
355 s
->buf_list
[s
->buf_num
],
356 s
->dma_addr
[s
->buf_num
]);
359 clear_bit(USB_STATE_URB_BUF
, &s
->flags
);
364 static int airspy_alloc_stream_bufs(struct airspy
*s
)
367 s
->buf_size
= BULK_BUFFER_SIZE
;
369 dev_dbg(s
->dev
, "all in all I will use %u bytes for streaming\n",
370 MAX_BULK_BUFS
* BULK_BUFFER_SIZE
);
372 for (s
->buf_num
= 0; s
->buf_num
< MAX_BULK_BUFS
; s
->buf_num
++) {
373 s
->buf_list
[s
->buf_num
] = usb_alloc_coherent(s
->udev
,
374 BULK_BUFFER_SIZE
, GFP_ATOMIC
,
375 &s
->dma_addr
[s
->buf_num
]);
376 if (!s
->buf_list
[s
->buf_num
]) {
377 dev_dbg(s
->dev
, "alloc buf=%d failed\n", s
->buf_num
);
378 airspy_free_stream_bufs(s
);
382 dev_dbg(s
->dev
, "alloc buf=%d %p (dma %llu)\n", s
->buf_num
,
383 s
->buf_list
[s
->buf_num
],
384 (long long)s
->dma_addr
[s
->buf_num
]);
385 set_bit(USB_STATE_URB_BUF
, &s
->flags
);
391 static int airspy_free_urbs(struct airspy
*s
)
397 for (i
= s
->urbs_initialized
- 1; i
>= 0; i
--) {
398 if (s
->urb_list
[i
]) {
399 dev_dbg(s
->dev
, "free urb=%d\n", i
);
401 usb_free_urb(s
->urb_list
[i
]);
404 s
->urbs_initialized
= 0;
409 static int airspy_alloc_urbs(struct airspy
*s
)
413 /* allocate the URBs */
414 for (i
= 0; i
< MAX_BULK_BUFS
; i
++) {
415 dev_dbg(s
->dev
, "alloc urb=%d\n", i
);
416 s
->urb_list
[i
] = usb_alloc_urb(0, GFP_ATOMIC
);
417 if (!s
->urb_list
[i
]) {
418 for (j
= 0; j
< i
; j
++)
419 usb_free_urb(s
->urb_list
[j
]);
422 usb_fill_bulk_urb(s
->urb_list
[i
],
424 usb_rcvbulkpipe(s
->udev
, 0x81),
427 airspy_urb_complete
, s
);
429 s
->urb_list
[i
]->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
430 s
->urb_list
[i
]->transfer_dma
= s
->dma_addr
[i
];
431 s
->urbs_initialized
++;
437 /* Must be called with vb_queue_lock hold */
438 static void airspy_cleanup_queued_bufs(struct airspy
*s
)
442 dev_dbg(s
->dev
, "\n");
444 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
445 while (!list_empty(&s
->queued_bufs
)) {
446 struct airspy_frame_buf
*buf
;
448 buf
= list_entry(s
->queued_bufs
.next
,
449 struct airspy_frame_buf
, list
);
450 list_del(&buf
->list
);
451 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
453 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
456 /* The user yanked out the cable... */
457 static void airspy_disconnect(struct usb_interface
*intf
)
459 struct v4l2_device
*v
= usb_get_intfdata(intf
);
460 struct airspy
*s
= container_of(v
, struct airspy
, v4l2_dev
);
462 dev_dbg(s
->dev
, "\n");
464 mutex_lock(&s
->vb_queue_lock
);
465 mutex_lock(&s
->v4l2_lock
);
466 /* No need to keep the urbs around after disconnection */
468 v4l2_device_disconnect(&s
->v4l2_dev
);
469 video_unregister_device(&s
->vdev
);
470 mutex_unlock(&s
->v4l2_lock
);
471 mutex_unlock(&s
->vb_queue_lock
);
473 v4l2_device_put(&s
->v4l2_dev
);
476 /* Videobuf2 operations */
477 static int airspy_queue_setup(struct vb2_queue
*vq
,
478 unsigned int *nbuffers
,
479 unsigned int *nplanes
, unsigned int sizes
[], struct device
*alloc_devs
[])
481 struct airspy
*s
= vb2_get_drv_priv(vq
);
483 dev_dbg(s
->dev
, "nbuffers=%d\n", *nbuffers
);
485 /* Need at least 8 buffers */
486 if (vq
->num_buffers
+ *nbuffers
< 8)
487 *nbuffers
= 8 - vq
->num_buffers
;
489 sizes
[0] = PAGE_ALIGN(s
->buffersize
);
491 dev_dbg(s
->dev
, "nbuffers=%d sizes[0]=%d\n", *nbuffers
, sizes
[0]);
495 static void airspy_buf_queue(struct vb2_buffer
*vb
)
497 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
498 struct airspy
*s
= vb2_get_drv_priv(vb
->vb2_queue
);
499 struct airspy_frame_buf
*buf
=
500 container_of(vbuf
, struct airspy_frame_buf
, vb
);
503 /* Check the device has not disconnected between prep and queuing */
504 if (unlikely(!s
->udev
)) {
505 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
509 spin_lock_irqsave(&s
->queued_bufs_lock
, flags
);
510 list_add_tail(&buf
->list
, &s
->queued_bufs
);
511 spin_unlock_irqrestore(&s
->queued_bufs_lock
, flags
);
514 static int airspy_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
516 struct airspy
*s
= vb2_get_drv_priv(vq
);
519 dev_dbg(s
->dev
, "\n");
524 mutex_lock(&s
->v4l2_lock
);
528 set_bit(POWER_ON
, &s
->flags
);
530 ret
= airspy_alloc_stream_bufs(s
);
534 ret
= airspy_alloc_urbs(s
);
536 goto err_free_stream_bufs
;
538 ret
= airspy_submit_urbs(s
);
542 /* start hardware streaming */
543 ret
= airspy_ctrl_msg(s
, CMD_RECEIVER_MODE
, 1, 0, NULL
, 0);
547 goto exit_mutex_unlock
;
553 err_free_stream_bufs
:
554 airspy_free_stream_bufs(s
);
556 clear_bit(POWER_ON
, &s
->flags
);
558 /* return all queued buffers to vb2 */
560 struct airspy_frame_buf
*buf
, *tmp
;
562 list_for_each_entry_safe(buf
, tmp
, &s
->queued_bufs
, list
) {
563 list_del(&buf
->list
);
564 vb2_buffer_done(&buf
->vb
.vb2_buf
,
565 VB2_BUF_STATE_QUEUED
);
570 mutex_unlock(&s
->v4l2_lock
);
575 static void airspy_stop_streaming(struct vb2_queue
*vq
)
577 struct airspy
*s
= vb2_get_drv_priv(vq
);
579 dev_dbg(s
->dev
, "\n");
581 mutex_lock(&s
->v4l2_lock
);
583 /* stop hardware streaming */
584 airspy_ctrl_msg(s
, CMD_RECEIVER_MODE
, 0, 0, NULL
, 0);
588 airspy_free_stream_bufs(s
);
590 airspy_cleanup_queued_bufs(s
);
592 clear_bit(POWER_ON
, &s
->flags
);
594 mutex_unlock(&s
->v4l2_lock
);
597 static const struct vb2_ops airspy_vb2_ops
= {
598 .queue_setup
= airspy_queue_setup
,
599 .buf_queue
= airspy_buf_queue
,
600 .start_streaming
= airspy_start_streaming
,
601 .stop_streaming
= airspy_stop_streaming
,
602 .wait_prepare
= vb2_ops_wait_prepare
,
603 .wait_finish
= vb2_ops_wait_finish
,
606 static int airspy_querycap(struct file
*file
, void *fh
,
607 struct v4l2_capability
*cap
)
609 struct airspy
*s
= video_drvdata(file
);
611 strscpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
612 strscpy(cap
->card
, s
->vdev
.name
, sizeof(cap
->card
));
613 usb_make_path(s
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
617 static int airspy_enum_fmt_sdr_cap(struct file
*file
, void *priv
,
618 struct v4l2_fmtdesc
*f
)
620 if (f
->index
>= NUM_FORMATS
)
623 f
->pixelformat
= formats
[f
->index
].pixelformat
;
628 static int airspy_g_fmt_sdr_cap(struct file
*file
, void *priv
,
629 struct v4l2_format
*f
)
631 struct airspy
*s
= video_drvdata(file
);
633 f
->fmt
.sdr
.pixelformat
= s
->pixelformat
;
634 f
->fmt
.sdr
.buffersize
= s
->buffersize
;
635 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
640 static int airspy_s_fmt_sdr_cap(struct file
*file
, void *priv
,
641 struct v4l2_format
*f
)
643 struct airspy
*s
= video_drvdata(file
);
644 struct vb2_queue
*q
= &s
->vb_queue
;
650 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
651 for (i
= 0; i
< NUM_FORMATS
; i
++) {
652 if (formats
[i
].pixelformat
== f
->fmt
.sdr
.pixelformat
) {
653 s
->pixelformat
= formats
[i
].pixelformat
;
654 s
->buffersize
= formats
[i
].buffersize
;
655 f
->fmt
.sdr
.buffersize
= formats
[i
].buffersize
;
660 s
->pixelformat
= formats
[0].pixelformat
;
661 s
->buffersize
= formats
[0].buffersize
;
662 f
->fmt
.sdr
.pixelformat
= formats
[0].pixelformat
;
663 f
->fmt
.sdr
.buffersize
= formats
[0].buffersize
;
668 static int airspy_try_fmt_sdr_cap(struct file
*file
, void *priv
,
669 struct v4l2_format
*f
)
673 memset(f
->fmt
.sdr
.reserved
, 0, sizeof(f
->fmt
.sdr
.reserved
));
674 for (i
= 0; i
< NUM_FORMATS
; i
++) {
675 if (formats
[i
].pixelformat
== f
->fmt
.sdr
.pixelformat
) {
676 f
->fmt
.sdr
.buffersize
= formats
[i
].buffersize
;
681 f
->fmt
.sdr
.pixelformat
= formats
[0].pixelformat
;
682 f
->fmt
.sdr
.buffersize
= formats
[0].buffersize
;
687 static int airspy_s_tuner(struct file
*file
, void *priv
,
688 const struct v4l2_tuner
*v
)
694 else if (v
->index
== 1)
702 static int airspy_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*v
)
707 strscpy(v
->name
, "AirSpy ADC", sizeof(v
->name
));
708 v
->type
= V4L2_TUNER_ADC
;
709 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
710 v
->rangelow
= bands
[0].rangelow
;
711 v
->rangehigh
= bands
[0].rangehigh
;
713 } else if (v
->index
== 1) {
714 strscpy(v
->name
, "AirSpy RF", sizeof(v
->name
));
715 v
->type
= V4L2_TUNER_RF
;
716 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
717 v
->rangelow
= bands_rf
[0].rangelow
;
718 v
->rangehigh
= bands_rf
[0].rangehigh
;
727 static int airspy_g_frequency(struct file
*file
, void *priv
,
728 struct v4l2_frequency
*f
)
730 struct airspy
*s
= video_drvdata(file
);
734 f
->type
= V4L2_TUNER_ADC
;
735 f
->frequency
= s
->f_adc
;
736 dev_dbg(s
->dev
, "ADC frequency=%u Hz\n", s
->f_adc
);
738 } else if (f
->tuner
== 1) {
739 f
->type
= V4L2_TUNER_RF
;
740 f
->frequency
= s
->f_rf
;
741 dev_dbg(s
->dev
, "RF frequency=%u Hz\n", s
->f_rf
);
750 static int airspy_s_frequency(struct file
*file
, void *priv
,
751 const struct v4l2_frequency
*f
)
753 struct airspy
*s
= video_drvdata(file
);
758 s
->f_adc
= clamp_t(unsigned int, f
->frequency
,
761 dev_dbg(s
->dev
, "ADC frequency=%u Hz\n", s
->f_adc
);
763 } else if (f
->tuner
== 1) {
764 s
->f_rf
= clamp_t(unsigned int, f
->frequency
,
765 bands_rf
[0].rangelow
,
766 bands_rf
[0].rangehigh
);
767 dev_dbg(s
->dev
, "RF frequency=%u Hz\n", s
->f_rf
);
768 buf
[0] = (s
->f_rf
>> 0) & 0xff;
769 buf
[1] = (s
->f_rf
>> 8) & 0xff;
770 buf
[2] = (s
->f_rf
>> 16) & 0xff;
771 buf
[3] = (s
->f_rf
>> 24) & 0xff;
772 ret
= airspy_ctrl_msg(s
, CMD_SET_FREQ
, 0, 0, buf
, 4);
780 static int airspy_enum_freq_bands(struct file
*file
, void *priv
,
781 struct v4l2_frequency_band
*band
)
785 if (band
->tuner
== 0) {
786 if (band
->index
>= ARRAY_SIZE(bands
)) {
789 *band
= bands
[band
->index
];
792 } else if (band
->tuner
== 1) {
793 if (band
->index
>= ARRAY_SIZE(bands_rf
)) {
796 *band
= bands_rf
[band
->index
];
806 static const struct v4l2_ioctl_ops airspy_ioctl_ops
= {
807 .vidioc_querycap
= airspy_querycap
,
809 .vidioc_enum_fmt_sdr_cap
= airspy_enum_fmt_sdr_cap
,
810 .vidioc_g_fmt_sdr_cap
= airspy_g_fmt_sdr_cap
,
811 .vidioc_s_fmt_sdr_cap
= airspy_s_fmt_sdr_cap
,
812 .vidioc_try_fmt_sdr_cap
= airspy_try_fmt_sdr_cap
,
814 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
815 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
816 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
817 .vidioc_querybuf
= vb2_ioctl_querybuf
,
818 .vidioc_qbuf
= vb2_ioctl_qbuf
,
819 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
821 .vidioc_streamon
= vb2_ioctl_streamon
,
822 .vidioc_streamoff
= vb2_ioctl_streamoff
,
824 .vidioc_g_tuner
= airspy_g_tuner
,
825 .vidioc_s_tuner
= airspy_s_tuner
,
827 .vidioc_g_frequency
= airspy_g_frequency
,
828 .vidioc_s_frequency
= airspy_s_frequency
,
829 .vidioc_enum_freq_bands
= airspy_enum_freq_bands
,
831 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
832 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
833 .vidioc_log_status
= v4l2_ctrl_log_status
,
836 static const struct v4l2_file_operations airspy_fops
= {
837 .owner
= THIS_MODULE
,
838 .open
= v4l2_fh_open
,
839 .release
= vb2_fop_release
,
840 .read
= vb2_fop_read
,
841 .poll
= vb2_fop_poll
,
842 .mmap
= vb2_fop_mmap
,
843 .unlocked_ioctl
= video_ioctl2
,
846 static const struct video_device airspy_template
= {
847 .name
= "AirSpy SDR",
848 .release
= video_device_release_empty
,
849 .fops
= &airspy_fops
,
850 .ioctl_ops
= &airspy_ioctl_ops
,
853 static void airspy_video_release(struct v4l2_device
*v
)
855 struct airspy
*s
= container_of(v
, struct airspy
, v4l2_dev
);
857 v4l2_ctrl_handler_free(&s
->hdl
);
858 v4l2_device_unregister(&s
->v4l2_dev
);
862 static int airspy_set_lna_gain(struct airspy
*s
)
867 dev_dbg(s
->dev
, "lna auto=%d->%d val=%d->%d\n",
868 s
->lna_gain_auto
->cur
.val
, s
->lna_gain_auto
->val
,
869 s
->lna_gain
->cur
.val
, s
->lna_gain
->val
);
871 ret
= airspy_ctrl_msg(s
, CMD_SET_LNA_AGC
, 0, s
->lna_gain_auto
->val
,
876 if (s
->lna_gain_auto
->val
== false) {
877 ret
= airspy_ctrl_msg(s
, CMD_SET_LNA_GAIN
, 0, s
->lna_gain
->val
,
884 dev_dbg(s
->dev
, "failed=%d\n", ret
);
889 static int airspy_set_mixer_gain(struct airspy
*s
)
894 dev_dbg(s
->dev
, "mixer auto=%d->%d val=%d->%d\n",
895 s
->mixer_gain_auto
->cur
.val
, s
->mixer_gain_auto
->val
,
896 s
->mixer_gain
->cur
.val
, s
->mixer_gain
->val
);
898 ret
= airspy_ctrl_msg(s
, CMD_SET_MIXER_AGC
, 0, s
->mixer_gain_auto
->val
,
903 if (s
->mixer_gain_auto
->val
== false) {
904 ret
= airspy_ctrl_msg(s
, CMD_SET_MIXER_GAIN
, 0,
905 s
->mixer_gain
->val
, &u8tmp
, 1);
911 dev_dbg(s
->dev
, "failed=%d\n", ret
);
916 static int airspy_set_if_gain(struct airspy
*s
)
921 dev_dbg(s
->dev
, "val=%d->%d\n", s
->if_gain
->cur
.val
, s
->if_gain
->val
);
923 ret
= airspy_ctrl_msg(s
, CMD_SET_VGA_GAIN
, 0, s
->if_gain
->val
,
926 dev_dbg(s
->dev
, "failed=%d\n", ret
);
931 static int airspy_s_ctrl(struct v4l2_ctrl
*ctrl
)
933 struct airspy
*s
= container_of(ctrl
->handler
, struct airspy
, hdl
);
937 case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
:
938 case V4L2_CID_RF_TUNER_LNA_GAIN
:
939 ret
= airspy_set_lna_gain(s
);
941 case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
:
942 case V4L2_CID_RF_TUNER_MIXER_GAIN
:
943 ret
= airspy_set_mixer_gain(s
);
945 case V4L2_CID_RF_TUNER_IF_GAIN
:
946 ret
= airspy_set_if_gain(s
);
949 dev_dbg(s
->dev
, "unknown ctrl: id=%d name=%s\n",
950 ctrl
->id
, ctrl
->name
);
957 static const struct v4l2_ctrl_ops airspy_ctrl_ops
= {
958 .s_ctrl
= airspy_s_ctrl
,
961 static int airspy_probe(struct usb_interface
*intf
,
962 const struct usb_device_id
*id
)
966 u8 u8tmp
, buf
[BUF_SIZE
];
968 s
= kzalloc(sizeof(struct airspy
), GFP_KERNEL
);
970 dev_err(&intf
->dev
, "Could not allocate memory for state\n");
974 mutex_init(&s
->v4l2_lock
);
975 mutex_init(&s
->vb_queue_lock
);
976 spin_lock_init(&s
->queued_bufs_lock
);
977 INIT_LIST_HEAD(&s
->queued_bufs
);
979 s
->udev
= interface_to_usbdev(intf
);
980 s
->f_adc
= bands
[0].rangelow
;
981 s
->f_rf
= bands_rf
[0].rangelow
;
982 s
->pixelformat
= formats
[0].pixelformat
;
983 s
->buffersize
= formats
[0].buffersize
;
986 ret
= airspy_ctrl_msg(s
, CMD_BOARD_ID_READ
, 0, 0, &u8tmp
, 1);
988 ret
= airspy_ctrl_msg(s
, CMD_VERSION_STRING_READ
, 0, 0,
991 dev_err(s
->dev
, "Could not detect board\n");
995 buf
[BUF_SIZE
- 1] = '\0';
997 dev_info(s
->dev
, "Board ID: %02x\n", u8tmp
);
998 dev_info(s
->dev
, "Firmware version: %s\n", buf
);
1000 /* Init videobuf2 queue structure */
1001 s
->vb_queue
.type
= V4L2_BUF_TYPE_SDR_CAPTURE
;
1002 s
->vb_queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
1003 s
->vb_queue
.drv_priv
= s
;
1004 s
->vb_queue
.buf_struct_size
= sizeof(struct airspy_frame_buf
);
1005 s
->vb_queue
.ops
= &airspy_vb2_ops
;
1006 s
->vb_queue
.mem_ops
= &vb2_vmalloc_memops
;
1007 s
->vb_queue
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1008 ret
= vb2_queue_init(&s
->vb_queue
);
1010 dev_err(s
->dev
, "Could not initialize vb2 queue\n");
1014 /* Init video_device structure */
1015 s
->vdev
= airspy_template
;
1016 s
->vdev
.queue
= &s
->vb_queue
;
1017 s
->vdev
.queue
->lock
= &s
->vb_queue_lock
;
1018 video_set_drvdata(&s
->vdev
, s
);
1020 /* Register the v4l2_device structure */
1021 s
->v4l2_dev
.release
= airspy_video_release
;
1022 ret
= v4l2_device_register(&intf
->dev
, &s
->v4l2_dev
);
1024 dev_err(s
->dev
, "Failed to register v4l2-device (%d)\n", ret
);
1028 /* Register controls */
1029 v4l2_ctrl_handler_init(&s
->hdl
, 5);
1030 s
->lna_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1031 V4L2_CID_RF_TUNER_LNA_GAIN_AUTO
, 0, 1, 1, 0);
1032 s
->lna_gain
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1033 V4L2_CID_RF_TUNER_LNA_GAIN
, 0, 14, 1, 8);
1034 v4l2_ctrl_auto_cluster(2, &s
->lna_gain_auto
, 0, false);
1035 s
->mixer_gain_auto
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1036 V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO
, 0, 1, 1, 0);
1037 s
->mixer_gain
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1038 V4L2_CID_RF_TUNER_MIXER_GAIN
, 0, 15, 1, 8);
1039 v4l2_ctrl_auto_cluster(2, &s
->mixer_gain_auto
, 0, false);
1040 s
->if_gain
= v4l2_ctrl_new_std(&s
->hdl
, &airspy_ctrl_ops
,
1041 V4L2_CID_RF_TUNER_IF_GAIN
, 0, 15, 1, 0);
1044 dev_err(s
->dev
, "Could not initialize controls\n");
1045 goto err_free_controls
;
1048 v4l2_ctrl_handler_setup(&s
->hdl
);
1050 s
->v4l2_dev
.ctrl_handler
= &s
->hdl
;
1051 s
->vdev
.v4l2_dev
= &s
->v4l2_dev
;
1052 s
->vdev
.lock
= &s
->v4l2_lock
;
1053 s
->vdev
.device_caps
= V4L2_CAP_SDR_CAPTURE
| V4L2_CAP_STREAMING
|
1054 V4L2_CAP_READWRITE
| V4L2_CAP_TUNER
;
1056 ret
= video_register_device(&s
->vdev
, VFL_TYPE_SDR
, -1);
1058 dev_err(s
->dev
, "Failed to register as video device (%d)\n",
1060 goto err_free_controls
;
1062 dev_info(s
->dev
, "Registered as %s\n",
1063 video_device_node_name(&s
->vdev
));
1064 dev_notice(s
->dev
, "SDR API is still slightly experimental and functionality changes may follow\n");
1068 v4l2_ctrl_handler_free(&s
->hdl
);
1069 v4l2_device_unregister(&s
->v4l2_dev
);
1075 /* USB device ID list */
1076 static const struct usb_device_id airspy_id_table
[] = {
1077 { USB_DEVICE(0x1d50, 0x60a1) }, /* AirSpy */
1080 MODULE_DEVICE_TABLE(usb
, airspy_id_table
);
1082 /* USB subsystem interface */
1083 static struct usb_driver airspy_driver
= {
1084 .name
= KBUILD_MODNAME
,
1085 .probe
= airspy_probe
,
1086 .disconnect
= airspy_disconnect
,
1087 .id_table
= airspy_id_table
,
1090 module_usb_driver(airspy_driver
);
1092 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1093 MODULE_DESCRIPTION("AirSpy SDR");
1094 MODULE_LICENSE("GPL");