airspy.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AirSpy SDR driver
  4. *
  5. * Copyright (C) 2014 Antti Palosaari <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/usb.h>
  10. #include <media/v4l2-device.h>
  11. #include <media/v4l2-ioctl.h>
  12. #include <media/v4l2-ctrls.h>
  13. #include <media/v4l2-event.h>
  14. #include <media/videobuf2-v4l2.h>
  15. #include <media/videobuf2-vmalloc.h>
  16. /* AirSpy USB API commands (from AirSpy Library) */
  17. enum {
  18. CMD_INVALID = 0x00,
  19. CMD_RECEIVER_MODE = 0x01,
  20. CMD_SI5351C_WRITE = 0x02,
  21. CMD_SI5351C_READ = 0x03,
  22. CMD_R820T_WRITE = 0x04,
  23. CMD_R820T_READ = 0x05,
  24. CMD_SPIFLASH_ERASE = 0x06,
  25. CMD_SPIFLASH_WRITE = 0x07,
  26. CMD_SPIFLASH_READ = 0x08,
  27. CMD_BOARD_ID_READ = 0x09,
  28. CMD_VERSION_STRING_READ = 0x0a,
  29. CMD_BOARD_PARTID_SERIALNO_READ = 0x0b,
  30. CMD_SET_SAMPLE_RATE = 0x0c,
  31. CMD_SET_FREQ = 0x0d,
  32. CMD_SET_LNA_GAIN = 0x0e,
  33. CMD_SET_MIXER_GAIN = 0x0f,
  34. CMD_SET_VGA_GAIN = 0x10,
  35. CMD_SET_LNA_AGC = 0x11,
  36. CMD_SET_MIXER_AGC = 0x12,
  37. CMD_SET_PACKING = 0x13,
  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. static const struct v4l2_frequency_band bands[] = {
  47. {
  48. .tuner = 0,
  49. .type = V4L2_TUNER_ADC,
  50. .index = 0,
  51. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  52. .rangelow = 20000000,
  53. .rangehigh = 20000000,
  54. },
  55. };
  56. static const struct v4l2_frequency_band bands_rf[] = {
  57. {
  58. .tuner = 1,
  59. .type = V4L2_TUNER_RF,
  60. .index = 0,
  61. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  62. .rangelow = 24000000,
  63. .rangehigh = 1750000000,
  64. },
  65. };
  66. /* stream formats */
  67. struct airspy_format {
  68. u32 pixelformat;
  69. u32 buffersize;
  70. };
  71. /* format descriptions for capture and preview */
  72. static struct airspy_format formats[] = {
  73. {
  74. .pixelformat = V4L2_SDR_FMT_RU12LE,
  75. .buffersize = BULK_BUFFER_SIZE,
  76. },
  77. };
  78. static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
  79. /* intermediate buffers with raw data from the USB device */
  80. struct airspy_frame_buf {
  81. /* common v4l buffer stuff -- must be first */
  82. struct vb2_v4l2_buffer vb;
  83. struct list_head list;
  84. };
  85. struct airspy {
  86. #define POWER_ON 1
  87. #define USB_STATE_URB_BUF 2
  88. unsigned long flags;
  89. struct device *dev;
  90. struct usb_device *udev;
  91. struct video_device vdev;
  92. struct v4l2_device v4l2_dev;
  93. /* videobuf2 queue and queued buffers list */
  94. struct vb2_queue vb_queue;
  95. struct list_head queued_bufs;
  96. spinlock_t queued_bufs_lock; /* Protects queued_bufs */
  97. unsigned sequence; /* Buffer sequence counter */
  98. unsigned int vb_full; /* vb is full and packets dropped */
  99. /* Note if taking both locks v4l2_lock must always be locked first! */
  100. struct mutex v4l2_lock; /* Protects everything else */
  101. struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */
  102. struct urb *urb_list[MAX_BULK_BUFS];
  103. int buf_num;
  104. unsigned long buf_size;
  105. u8 *buf_list[MAX_BULK_BUFS];
  106. dma_addr_t dma_addr[MAX_BULK_BUFS];
  107. int urbs_initialized;
  108. int urbs_submitted;
  109. /* USB control message buffer */
  110. #define BUF_SIZE 128
  111. u8 *buf;
  112. /* Current configuration */
  113. unsigned int f_adc;
  114. unsigned int f_rf;
  115. u32 pixelformat;
  116. u32 buffersize;
  117. /* Controls */
  118. struct v4l2_ctrl_handler hdl;
  119. struct v4l2_ctrl *lna_gain_auto;
  120. struct v4l2_ctrl *lna_gain;
  121. struct v4l2_ctrl *mixer_gain_auto;
  122. struct v4l2_ctrl *mixer_gain;
  123. struct v4l2_ctrl *if_gain;
  124. /* Sample rate calc */
  125. unsigned long jiffies_next;
  126. unsigned int sample;
  127. unsigned int sample_measured;
  128. };
  129. #define airspy_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
  130. char *_direction; \
  131. if (_t & USB_DIR_IN) \
  132. _direction = "<<<"; \
  133. else \
  134. _direction = ">>>"; \
  135. dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
  136. _t, _r, _v & 0xff, _v >> 8, _i & 0xff, _i >> 8, \
  137. _l & 0xff, _l >> 8, _direction, _l, _b); \
  138. }
  139. /* execute firmware command */
  140. static int airspy_ctrl_msg(struct airspy *s, u8 request, u16 value, u16 index,
  141. u8 *data, u16 size)
  142. {
  143. int ret;
  144. unsigned int pipe;
  145. u8 requesttype;
  146. switch (request) {
  147. case CMD_RECEIVER_MODE:
  148. case CMD_SET_FREQ:
  149. pipe = usb_sndctrlpipe(s->udev, 0);
  150. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  151. break;
  152. case CMD_BOARD_ID_READ:
  153. case CMD_VERSION_STRING_READ:
  154. case CMD_BOARD_PARTID_SERIALNO_READ:
  155. case CMD_SET_LNA_GAIN:
  156. case CMD_SET_MIXER_GAIN:
  157. case CMD_SET_VGA_GAIN:
  158. case CMD_SET_LNA_AGC:
  159. case CMD_SET_MIXER_AGC:
  160. pipe = usb_rcvctrlpipe(s->udev, 0);
  161. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  162. break;
  163. default:
  164. dev_err(s->dev, "Unknown command %02x\n", request);
  165. ret = -EINVAL;
  166. goto err;
  167. }
  168. /* write request */
  169. if (!(requesttype & USB_DIR_IN))
  170. memcpy(s->buf, data, size);
  171. ret = usb_control_msg(s->udev, pipe, request, requesttype, value,
  172. index, s->buf, size, 1000);
  173. airspy_dbg_usb_control_msg(s->dev, request, requesttype, value,
  174. index, s->buf, size);
  175. if (ret < 0) {
  176. dev_err(s->dev, "usb_control_msg() failed %d request %02x\n",
  177. ret, request);
  178. goto err;
  179. }
  180. /* read request */
  181. if (requesttype & USB_DIR_IN)
  182. memcpy(data, s->buf, size);
  183. return 0;
  184. err:
  185. return ret;
  186. }
  187. /* Private functions */
  188. static struct airspy_frame_buf *airspy_get_next_fill_buf(struct airspy *s)
  189. {
  190. unsigned long flags;
  191. struct airspy_frame_buf *buf = NULL;
  192. spin_lock_irqsave(&s->queued_bufs_lock, flags);
  193. if (list_empty(&s->queued_bufs))
  194. goto leave;
  195. buf = list_entry(s->queued_bufs.next,
  196. struct airspy_frame_buf, list);
  197. list_del(&buf->list);
  198. leave:
  199. spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
  200. return buf;
  201. }
  202. static unsigned int airspy_convert_stream(struct airspy *s,
  203. void *dst, void *src, unsigned int src_len)
  204. {
  205. unsigned int dst_len;
  206. if (s->pixelformat == V4L2_SDR_FMT_RU12LE) {
  207. memcpy(dst, src, src_len);
  208. dst_len = src_len;
  209. } else {
  210. dst_len = 0;
  211. }
  212. /* calculate sample rate and output it in 10 seconds intervals */
  213. if (unlikely(time_is_before_jiffies(s->jiffies_next))) {
  214. #define MSECS 10000UL
  215. unsigned int msecs = jiffies_to_msecs(jiffies -
  216. s->jiffies_next + msecs_to_jiffies(MSECS));
  217. unsigned int samples = s->sample - s->sample_measured;
  218. s->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
  219. s->sample_measured = s->sample;
  220. dev_dbg(s->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n",
  221. src_len, samples, msecs,
  222. samples * 1000UL / msecs);
  223. }
  224. /* total number of samples */
  225. s->sample += src_len / 2;
  226. return dst_len;
  227. }
  228. /*
  229. * This gets called for the bulk stream pipe. This is done in interrupt
  230. * time, so it has to be fast, not crash, and not stall. Neat.
  231. */
  232. static void airspy_urb_complete(struct urb *urb)
  233. {
  234. struct airspy *s = urb->context;
  235. struct airspy_frame_buf *fbuf;
  236. dev_dbg_ratelimited(s->dev, "status=%d length=%d/%d errors=%d\n",
  237. urb->status, urb->actual_length,
  238. urb->transfer_buffer_length, urb->error_count);
  239. switch (urb->status) {
  240. case 0: /* success */
  241. case -ETIMEDOUT: /* NAK */
  242. break;
  243. case -ECONNRESET: /* kill */
  244. case -ENOENT:
  245. case -ESHUTDOWN:
  246. return;
  247. default: /* error */
  248. dev_err_ratelimited(s->dev, "URB failed %d\n", urb->status);
  249. break;
  250. }
  251. if (likely(urb->actual_length > 0)) {
  252. void *ptr;
  253. unsigned int len;
  254. /* get free framebuffer */
  255. fbuf = airspy_get_next_fill_buf(s);
  256. if (unlikely(fbuf == NULL)) {
  257. s->vb_full++;
  258. dev_notice_ratelimited(s->dev,
  259. "video buffer is full, %d packets dropped\n",
  260. s->vb_full);
  261. goto skip;
  262. }
  263. /* fill framebuffer */
  264. ptr = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
  265. len = airspy_convert_stream(s, ptr, urb->transfer_buffer,
  266. urb->actual_length);
  267. vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0, len);
  268. fbuf->vb.vb2_buf.timestamp = ktime_get_ns();
  269. fbuf->vb.sequence = s->sequence++;
  270. vb2_buffer_done(&fbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  271. }
  272. skip:
  273. usb_submit_urb(urb, GFP_ATOMIC);
  274. }
  275. static int airspy_kill_urbs(struct airspy *s)
  276. {
  277. int i;
  278. for (i = s->urbs_submitted - 1; i >= 0; i--) {
  279. dev_dbg(s->dev, "kill urb=%d\n", i);
  280. /* stop the URB */
  281. usb_kill_urb(s->urb_list[i]);
  282. }
  283. s->urbs_submitted = 0;
  284. return 0;
  285. }
  286. static int airspy_submit_urbs(struct airspy *s)
  287. {
  288. int i, ret;
  289. for (i = 0; i < s->urbs_initialized; i++) {
  290. dev_dbg(s->dev, "submit urb=%d\n", i);
  291. ret = usb_submit_urb(s->urb_list[i], GFP_ATOMIC);
  292. if (ret) {
  293. dev_err(s->dev, "Could not submit URB no. %d - get them all back\n",
  294. i);
  295. airspy_kill_urbs(s);
  296. return ret;
  297. }
  298. s->urbs_submitted++;
  299. }
  300. return 0;
  301. }
  302. static int airspy_free_stream_bufs(struct airspy *s)
  303. {
  304. if (test_bit(USB_STATE_URB_BUF, &s->flags)) {
  305. while (s->buf_num) {
  306. s->buf_num--;
  307. dev_dbg(s->dev, "free buf=%d\n", s->buf_num);
  308. usb_free_coherent(s->udev, s->buf_size,
  309. s->buf_list[s->buf_num],
  310. s->dma_addr[s->buf_num]);
  311. }
  312. }
  313. clear_bit(USB_STATE_URB_BUF, &s->flags);
  314. return 0;
  315. }
  316. static int airspy_alloc_stream_bufs(struct airspy *s)
  317. {
  318. s->buf_num = 0;
  319. s->buf_size = BULK_BUFFER_SIZE;
  320. dev_dbg(s->dev, "all in all I will use %u bytes for streaming\n",
  321. MAX_BULK_BUFS * BULK_BUFFER_SIZE);
  322. for (s->buf_num = 0; s->buf_num < MAX_BULK_BUFS; s->buf_num++) {
  323. s->buf_list[s->buf_num] = usb_alloc_coherent(s->udev,
  324. BULK_BUFFER_SIZE, GFP_ATOMIC,
  325. &s->dma_addr[s->buf_num]);
  326. if (!s->buf_list[s->buf_num]) {
  327. dev_dbg(s->dev, "alloc buf=%d failed\n", s->buf_num);
  328. airspy_free_stream_bufs(s);
  329. return -ENOMEM;
  330. }
  331. dev_dbg(s->dev, "alloc buf=%d %p (dma %llu)\n", s->buf_num,
  332. s->buf_list[s->buf_num],
  333. (long long)s->dma_addr[s->buf_num]);
  334. set_bit(USB_STATE_URB_BUF, &s->flags);
  335. }
  336. return 0;
  337. }
  338. static int airspy_free_urbs(struct airspy *s)
  339. {
  340. int i;
  341. airspy_kill_urbs(s);
  342. for (i = s->urbs_initialized - 1; i >= 0; i--) {
  343. if (s->urb_list[i]) {
  344. dev_dbg(s->dev, "free urb=%d\n", i);
  345. /* free the URBs */
  346. usb_free_urb(s->urb_list[i]);
  347. }
  348. }
  349. s->urbs_initialized = 0;
  350. return 0;
  351. }
  352. static int airspy_alloc_urbs(struct airspy *s)
  353. {
  354. int i, j;
  355. /* allocate the URBs */
  356. for (i = 0; i < MAX_BULK_BUFS; i++) {
  357. dev_dbg(s->dev, "alloc urb=%d\n", i);
  358. s->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
  359. if (!s->urb_list[i]) {
  360. for (j = 0; j < i; j++) {
  361. usb_free_urb(s->urb_list[j]);
  362. s->urb_list[j] = NULL;
  363. }
  364. s->urbs_initialized = 0;
  365. return -ENOMEM;
  366. }
  367. usb_fill_bulk_urb(s->urb_list[i],
  368. s->udev,
  369. usb_rcvbulkpipe(s->udev, 0x81),
  370. s->buf_list[i],
  371. BULK_BUFFER_SIZE,
  372. airspy_urb_complete, s);
  373. s->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  374. s->urb_list[i]->transfer_dma = s->dma_addr[i];
  375. s->urbs_initialized++;
  376. }
  377. return 0;
  378. }
  379. /* Must be called with vb_queue_lock hold */
  380. static void airspy_cleanup_queued_bufs(struct airspy *s)
  381. {
  382. unsigned long flags;
  383. dev_dbg(s->dev, "\n");
  384. spin_lock_irqsave(&s->queued_bufs_lock, flags);
  385. while (!list_empty(&s->queued_bufs)) {
  386. struct airspy_frame_buf *buf;
  387. buf = list_entry(s->queued_bufs.next,
  388. struct airspy_frame_buf, list);
  389. list_del(&buf->list);
  390. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  391. }
  392. spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
  393. }
  394. /* The user yanked out the cable... */
  395. static void airspy_disconnect(struct usb_interface *intf)
  396. {
  397. struct v4l2_device *v = usb_get_intfdata(intf);
  398. struct airspy *s = container_of(v, struct airspy, v4l2_dev);
  399. dev_dbg(s->dev, "\n");
  400. mutex_lock(&s->vb_queue_lock);
  401. mutex_lock(&s->v4l2_lock);
  402. /* No need to keep the urbs around after disconnection */
  403. s->udev = NULL;
  404. v4l2_device_disconnect(&s->v4l2_dev);
  405. video_unregister_device(&s->vdev);
  406. mutex_unlock(&s->v4l2_lock);
  407. mutex_unlock(&s->vb_queue_lock);
  408. v4l2_device_put(&s->v4l2_dev);
  409. }
  410. /* Videobuf2 operations */
  411. static int airspy_queue_setup(struct vb2_queue *vq,
  412. unsigned int *nbuffers,
  413. unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
  414. {
  415. struct airspy *s = vb2_get_drv_priv(vq);
  416. dev_dbg(s->dev, "nbuffers=%d\n", *nbuffers);
  417. /* Need at least 8 buffers */
  418. if (vq->num_buffers + *nbuffers < 8)
  419. *nbuffers = 8 - vq->num_buffers;
  420. *nplanes = 1;
  421. sizes[0] = PAGE_ALIGN(s->buffersize);
  422. dev_dbg(s->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
  423. return 0;
  424. }
  425. static void airspy_buf_queue(struct vb2_buffer *vb)
  426. {
  427. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  428. struct airspy *s = vb2_get_drv_priv(vb->vb2_queue);
  429. struct airspy_frame_buf *buf =
  430. container_of(vbuf, struct airspy_frame_buf, vb);
  431. unsigned long flags;
  432. /* Check the device has not disconnected between prep and queuing */
  433. if (unlikely(!s->udev)) {
  434. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  435. return;
  436. }
  437. spin_lock_irqsave(&s->queued_bufs_lock, flags);
  438. list_add_tail(&buf->list, &s->queued_bufs);
  439. spin_unlock_irqrestore(&s->queued_bufs_lock, flags);
  440. }
  441. static int airspy_start_streaming(struct vb2_queue *vq, unsigned int count)
  442. {
  443. struct airspy *s = vb2_get_drv_priv(vq);
  444. int ret;
  445. dev_dbg(s->dev, "\n");
  446. if (!s->udev)
  447. return -ENODEV;
  448. mutex_lock(&s->v4l2_lock);
  449. s->sequence = 0;
  450. set_bit(POWER_ON, &s->flags);
  451. ret = airspy_alloc_stream_bufs(s);
  452. if (ret)
  453. goto err_clear_bit;
  454. ret = airspy_alloc_urbs(s);
  455. if (ret)
  456. goto err_free_stream_bufs;
  457. ret = airspy_submit_urbs(s);
  458. if (ret)
  459. goto err_free_urbs;
  460. /* start hardware streaming */
  461. ret = airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 1, 0, NULL, 0);
  462. if (ret)
  463. goto err_kill_urbs;
  464. goto exit_mutex_unlock;
  465. err_kill_urbs:
  466. airspy_kill_urbs(s);
  467. err_free_urbs:
  468. airspy_free_urbs(s);
  469. err_free_stream_bufs:
  470. airspy_free_stream_bufs(s);
  471. err_clear_bit:
  472. clear_bit(POWER_ON, &s->flags);
  473. /* return all queued buffers to vb2 */
  474. {
  475. struct airspy_frame_buf *buf, *tmp;
  476. list_for_each_entry_safe(buf, tmp, &s->queued_bufs, list) {
  477. list_del(&buf->list);
  478. vb2_buffer_done(&buf->vb.vb2_buf,
  479. VB2_BUF_STATE_QUEUED);
  480. }
  481. }
  482. exit_mutex_unlock:
  483. mutex_unlock(&s->v4l2_lock);
  484. return ret;
  485. }
  486. static void airspy_stop_streaming(struct vb2_queue *vq)
  487. {
  488. struct airspy *s = vb2_get_drv_priv(vq);
  489. dev_dbg(s->dev, "\n");
  490. mutex_lock(&s->v4l2_lock);
  491. /* stop hardware streaming */
  492. airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 0, 0, NULL, 0);
  493. airspy_kill_urbs(s);
  494. airspy_free_urbs(s);
  495. airspy_free_stream_bufs(s);
  496. airspy_cleanup_queued_bufs(s);
  497. clear_bit(POWER_ON, &s->flags);
  498. mutex_unlock(&s->v4l2_lock);
  499. }
  500. static const struct vb2_ops airspy_vb2_ops = {
  501. .queue_setup = airspy_queue_setup,
  502. .buf_queue = airspy_buf_queue,
  503. .start_streaming = airspy_start_streaming,
  504. .stop_streaming = airspy_stop_streaming,
  505. .wait_prepare = vb2_ops_wait_prepare,
  506. .wait_finish = vb2_ops_wait_finish,
  507. };
  508. static int airspy_querycap(struct file *file, void *fh,
  509. struct v4l2_capability *cap)
  510. {
  511. struct airspy *s = video_drvdata(file);
  512. strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  513. strscpy(cap->card, s->vdev.name, sizeof(cap->card));
  514. usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info));
  515. return 0;
  516. }
  517. static int airspy_enum_fmt_sdr_cap(struct file *file, void *priv,
  518. struct v4l2_fmtdesc *f)
  519. {
  520. if (f->index >= NUM_FORMATS)
  521. return -EINVAL;
  522. f->pixelformat = formats[f->index].pixelformat;
  523. return 0;
  524. }
  525. static int airspy_g_fmt_sdr_cap(struct file *file, void *priv,
  526. struct v4l2_format *f)
  527. {
  528. struct airspy *s = video_drvdata(file);
  529. f->fmt.sdr.pixelformat = s->pixelformat;
  530. f->fmt.sdr.buffersize = s->buffersize;
  531. return 0;
  532. }
  533. static int airspy_s_fmt_sdr_cap(struct file *file, void *priv,
  534. struct v4l2_format *f)
  535. {
  536. struct airspy *s = video_drvdata(file);
  537. struct vb2_queue *q = &s->vb_queue;
  538. int i;
  539. if (vb2_is_busy(q))
  540. return -EBUSY;
  541. for (i = 0; i < NUM_FORMATS; i++) {
  542. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  543. s->pixelformat = formats[i].pixelformat;
  544. s->buffersize = formats[i].buffersize;
  545. f->fmt.sdr.buffersize = formats[i].buffersize;
  546. return 0;
  547. }
  548. }
  549. s->pixelformat = formats[0].pixelformat;
  550. s->buffersize = formats[0].buffersize;
  551. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  552. f->fmt.sdr.buffersize = formats[0].buffersize;
  553. return 0;
  554. }
  555. static int airspy_try_fmt_sdr_cap(struct file *file, void *priv,
  556. struct v4l2_format *f)
  557. {
  558. int i;
  559. for (i = 0; i < NUM_FORMATS; i++) {
  560. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  561. f->fmt.sdr.buffersize = formats[i].buffersize;
  562. return 0;
  563. }
  564. }
  565. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  566. f->fmt.sdr.buffersize = formats[0].buffersize;
  567. return 0;
  568. }
  569. static int airspy_s_tuner(struct file *file, void *priv,
  570. const struct v4l2_tuner *v)
  571. {
  572. int ret;
  573. if (v->index == 0)
  574. ret = 0;
  575. else if (v->index == 1)
  576. ret = 0;
  577. else
  578. ret = -EINVAL;
  579. return ret;
  580. }
  581. static int airspy_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
  582. {
  583. int ret;
  584. if (v->index == 0) {
  585. strscpy(v->name, "AirSpy ADC", sizeof(v->name));
  586. v->type = V4L2_TUNER_ADC;
  587. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  588. v->rangelow = bands[0].rangelow;
  589. v->rangehigh = bands[0].rangehigh;
  590. ret = 0;
  591. } else if (v->index == 1) {
  592. strscpy(v->name, "AirSpy RF", sizeof(v->name));
  593. v->type = V4L2_TUNER_RF;
  594. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  595. v->rangelow = bands_rf[0].rangelow;
  596. v->rangehigh = bands_rf[0].rangehigh;
  597. ret = 0;
  598. } else {
  599. ret = -EINVAL;
  600. }
  601. return ret;
  602. }
  603. static int airspy_g_frequency(struct file *file, void *priv,
  604. struct v4l2_frequency *f)
  605. {
  606. struct airspy *s = video_drvdata(file);
  607. int ret;
  608. if (f->tuner == 0) {
  609. f->type = V4L2_TUNER_ADC;
  610. f->frequency = s->f_adc;
  611. dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc);
  612. ret = 0;
  613. } else if (f->tuner == 1) {
  614. f->type = V4L2_TUNER_RF;
  615. f->frequency = s->f_rf;
  616. dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf);
  617. ret = 0;
  618. } else {
  619. ret = -EINVAL;
  620. }
  621. return ret;
  622. }
  623. static int airspy_s_frequency(struct file *file, void *priv,
  624. const struct v4l2_frequency *f)
  625. {
  626. struct airspy *s = video_drvdata(file);
  627. int ret;
  628. u8 buf[4];
  629. if (f->tuner == 0) {
  630. s->f_adc = clamp_t(unsigned int, f->frequency,
  631. bands[0].rangelow,
  632. bands[0].rangehigh);
  633. dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc);
  634. ret = 0;
  635. } else if (f->tuner == 1) {
  636. s->f_rf = clamp_t(unsigned int, f->frequency,
  637. bands_rf[0].rangelow,
  638. bands_rf[0].rangehigh);
  639. dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf);
  640. buf[0] = (s->f_rf >> 0) & 0xff;
  641. buf[1] = (s->f_rf >> 8) & 0xff;
  642. buf[2] = (s->f_rf >> 16) & 0xff;
  643. buf[3] = (s->f_rf >> 24) & 0xff;
  644. ret = airspy_ctrl_msg(s, CMD_SET_FREQ, 0, 0, buf, 4);
  645. } else {
  646. ret = -EINVAL;
  647. }
  648. return ret;
  649. }
  650. static int airspy_enum_freq_bands(struct file *file, void *priv,
  651. struct v4l2_frequency_band *band)
  652. {
  653. int ret;
  654. if (band->tuner == 0) {
  655. if (band->index >= ARRAY_SIZE(bands)) {
  656. ret = -EINVAL;
  657. } else {
  658. *band = bands[band->index];
  659. ret = 0;
  660. }
  661. } else if (band->tuner == 1) {
  662. if (band->index >= ARRAY_SIZE(bands_rf)) {
  663. ret = -EINVAL;
  664. } else {
  665. *band = bands_rf[band->index];
  666. ret = 0;
  667. }
  668. } else {
  669. ret = -EINVAL;
  670. }
  671. return ret;
  672. }
  673. static const struct v4l2_ioctl_ops airspy_ioctl_ops = {
  674. .vidioc_querycap = airspy_querycap,
  675. .vidioc_enum_fmt_sdr_cap = airspy_enum_fmt_sdr_cap,
  676. .vidioc_g_fmt_sdr_cap = airspy_g_fmt_sdr_cap,
  677. .vidioc_s_fmt_sdr_cap = airspy_s_fmt_sdr_cap,
  678. .vidioc_try_fmt_sdr_cap = airspy_try_fmt_sdr_cap,
  679. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  680. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  681. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  682. .vidioc_querybuf = vb2_ioctl_querybuf,
  683. .vidioc_qbuf = vb2_ioctl_qbuf,
  684. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  685. .vidioc_streamon = vb2_ioctl_streamon,
  686. .vidioc_streamoff = vb2_ioctl_streamoff,
  687. .vidioc_g_tuner = airspy_g_tuner,
  688. .vidioc_s_tuner = airspy_s_tuner,
  689. .vidioc_g_frequency = airspy_g_frequency,
  690. .vidioc_s_frequency = airspy_s_frequency,
  691. .vidioc_enum_freq_bands = airspy_enum_freq_bands,
  692. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  693. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  694. .vidioc_log_status = v4l2_ctrl_log_status,
  695. };
  696. static const struct v4l2_file_operations airspy_fops = {
  697. .owner = THIS_MODULE,
  698. .open = v4l2_fh_open,
  699. .release = vb2_fop_release,
  700. .read = vb2_fop_read,
  701. .poll = vb2_fop_poll,
  702. .mmap = vb2_fop_mmap,
  703. .unlocked_ioctl = video_ioctl2,
  704. };
  705. static const struct video_device airspy_template = {
  706. .name = "AirSpy SDR",
  707. .release = video_device_release_empty,
  708. .fops = &airspy_fops,
  709. .ioctl_ops = &airspy_ioctl_ops,
  710. };
  711. static void airspy_video_release(struct v4l2_device *v)
  712. {
  713. struct airspy *s = container_of(v, struct airspy, v4l2_dev);
  714. v4l2_ctrl_handler_free(&s->hdl);
  715. v4l2_device_unregister(&s->v4l2_dev);
  716. kfree(s->buf);
  717. kfree(s);
  718. }
  719. static int airspy_set_lna_gain(struct airspy *s)
  720. {
  721. int ret;
  722. u8 u8tmp;
  723. dev_dbg(s->dev, "lna auto=%d->%d val=%d->%d\n",
  724. s->lna_gain_auto->cur.val, s->lna_gain_auto->val,
  725. s->lna_gain->cur.val, s->lna_gain->val);
  726. ret = airspy_ctrl_msg(s, CMD_SET_LNA_AGC, 0, s->lna_gain_auto->val,
  727. &u8tmp, 1);
  728. if (ret)
  729. goto err;
  730. if (s->lna_gain_auto->val == false) {
  731. ret = airspy_ctrl_msg(s, CMD_SET_LNA_GAIN, 0, s->lna_gain->val,
  732. &u8tmp, 1);
  733. if (ret)
  734. goto err;
  735. }
  736. err:
  737. if (ret)
  738. dev_dbg(s->dev, "failed=%d\n", ret);
  739. return ret;
  740. }
  741. static int airspy_set_mixer_gain(struct airspy *s)
  742. {
  743. int ret;
  744. u8 u8tmp;
  745. dev_dbg(s->dev, "mixer auto=%d->%d val=%d->%d\n",
  746. s->mixer_gain_auto->cur.val, s->mixer_gain_auto->val,
  747. s->mixer_gain->cur.val, s->mixer_gain->val);
  748. ret = airspy_ctrl_msg(s, CMD_SET_MIXER_AGC, 0, s->mixer_gain_auto->val,
  749. &u8tmp, 1);
  750. if (ret)
  751. goto err;
  752. if (s->mixer_gain_auto->val == false) {
  753. ret = airspy_ctrl_msg(s, CMD_SET_MIXER_GAIN, 0,
  754. s->mixer_gain->val, &u8tmp, 1);
  755. if (ret)
  756. goto err;
  757. }
  758. err:
  759. if (ret)
  760. dev_dbg(s->dev, "failed=%d\n", ret);
  761. return ret;
  762. }
  763. static int airspy_set_if_gain(struct airspy *s)
  764. {
  765. int ret;
  766. u8 u8tmp;
  767. dev_dbg(s->dev, "val=%d->%d\n", s->if_gain->cur.val, s->if_gain->val);
  768. ret = airspy_ctrl_msg(s, CMD_SET_VGA_GAIN, 0, s->if_gain->val,
  769. &u8tmp, 1);
  770. if (ret)
  771. dev_dbg(s->dev, "failed=%d\n", ret);
  772. return ret;
  773. }
  774. static int airspy_s_ctrl(struct v4l2_ctrl *ctrl)
  775. {
  776. struct airspy *s = container_of(ctrl->handler, struct airspy, hdl);
  777. int ret;
  778. switch (ctrl->id) {
  779. case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
  780. case V4L2_CID_RF_TUNER_LNA_GAIN:
  781. ret = airspy_set_lna_gain(s);
  782. break;
  783. case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
  784. case V4L2_CID_RF_TUNER_MIXER_GAIN:
  785. ret = airspy_set_mixer_gain(s);
  786. break;
  787. case V4L2_CID_RF_TUNER_IF_GAIN:
  788. ret = airspy_set_if_gain(s);
  789. break;
  790. default:
  791. dev_dbg(s->dev, "unknown ctrl: id=%d name=%s\n",
  792. ctrl->id, ctrl->name);
  793. ret = -EINVAL;
  794. }
  795. return ret;
  796. }
  797. static const struct v4l2_ctrl_ops airspy_ctrl_ops = {
  798. .s_ctrl = airspy_s_ctrl,
  799. };
  800. static int airspy_probe(struct usb_interface *intf,
  801. const struct usb_device_id *id)
  802. {
  803. struct airspy *s;
  804. int ret;
  805. u8 u8tmp, *buf;
  806. buf = NULL;
  807. ret = -ENOMEM;
  808. s = kzalloc(sizeof(struct airspy), GFP_KERNEL);
  809. if (s == NULL) {
  810. dev_err(&intf->dev, "Could not allocate memory for state\n");
  811. return -ENOMEM;
  812. }
  813. s->buf = kzalloc(BUF_SIZE, GFP_KERNEL);
  814. if (!s->buf)
  815. goto err_free_mem;
  816. buf = kzalloc(BUF_SIZE, GFP_KERNEL);
  817. if (!buf)
  818. goto err_free_mem;
  819. mutex_init(&s->v4l2_lock);
  820. mutex_init(&s->vb_queue_lock);
  821. spin_lock_init(&s->queued_bufs_lock);
  822. INIT_LIST_HEAD(&s->queued_bufs);
  823. s->dev = &intf->dev;
  824. s->udev = interface_to_usbdev(intf);
  825. s->f_adc = bands[0].rangelow;
  826. s->f_rf = bands_rf[0].rangelow;
  827. s->pixelformat = formats[0].pixelformat;
  828. s->buffersize = formats[0].buffersize;
  829. /* Detect device */
  830. ret = airspy_ctrl_msg(s, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1);
  831. if (ret == 0)
  832. ret = airspy_ctrl_msg(s, CMD_VERSION_STRING_READ, 0, 0,
  833. buf, BUF_SIZE);
  834. if (ret) {
  835. dev_err(s->dev, "Could not detect board\n");
  836. goto err_free_mem;
  837. }
  838. buf[BUF_SIZE - 1] = '\0';
  839. dev_info(s->dev, "Board ID: %02x\n", u8tmp);
  840. dev_info(s->dev, "Firmware version: %s\n", buf);
  841. /* Init videobuf2 queue structure */
  842. s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
  843. s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  844. s->vb_queue.drv_priv = s;
  845. s->vb_queue.buf_struct_size = sizeof(struct airspy_frame_buf);
  846. s->vb_queue.ops = &airspy_vb2_ops;
  847. s->vb_queue.mem_ops = &vb2_vmalloc_memops;
  848. s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  849. ret = vb2_queue_init(&s->vb_queue);
  850. if (ret) {
  851. dev_err(s->dev, "Could not initialize vb2 queue\n");
  852. goto err_free_mem;
  853. }
  854. /* Init video_device structure */
  855. s->vdev = airspy_template;
  856. s->vdev.queue = &s->vb_queue;
  857. s->vdev.queue->lock = &s->vb_queue_lock;
  858. video_set_drvdata(&s->vdev, s);
  859. /* Register the v4l2_device structure */
  860. s->v4l2_dev.release = airspy_video_release;
  861. ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
  862. if (ret) {
  863. dev_err(s->dev, "Failed to register v4l2-device (%d)\n", ret);
  864. goto err_free_mem;
  865. }
  866. /* Register controls */
  867. v4l2_ctrl_handler_init(&s->hdl, 5);
  868. s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
  869. V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 0);
  870. s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
  871. V4L2_CID_RF_TUNER_LNA_GAIN, 0, 14, 1, 8);
  872. v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false);
  873. s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
  874. V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 0);
  875. s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
  876. V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 15, 1, 8);
  877. v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false);
  878. s->if_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops,
  879. V4L2_CID_RF_TUNER_IF_GAIN, 0, 15, 1, 0);
  880. if (s->hdl.error) {
  881. ret = s->hdl.error;
  882. dev_err(s->dev, "Could not initialize controls\n");
  883. goto err_free_controls;
  884. }
  885. v4l2_ctrl_handler_setup(&s->hdl);
  886. s->v4l2_dev.ctrl_handler = &s->hdl;
  887. s->vdev.v4l2_dev = &s->v4l2_dev;
  888. s->vdev.lock = &s->v4l2_lock;
  889. s->vdev.device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
  890. V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
  891. ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
  892. if (ret) {
  893. dev_err(s->dev, "Failed to register as video device (%d)\n",
  894. ret);
  895. goto err_free_controls;
  896. }
  897. /* Free buf if success*/
  898. kfree(buf);
  899. dev_info(s->dev, "Registered as %s\n",
  900. video_device_node_name(&s->vdev));
  901. dev_notice(s->dev, "SDR API is still slightly experimental and functionality changes may follow\n");
  902. return 0;
  903. err_free_controls:
  904. v4l2_ctrl_handler_free(&s->hdl);
  905. v4l2_device_unregister(&s->v4l2_dev);
  906. err_free_mem:
  907. kfree(buf);
  908. kfree(s->buf);
  909. kfree(s);
  910. return ret;
  911. }
  912. /* USB device ID list */
  913. static const struct usb_device_id airspy_id_table[] = {
  914. { USB_DEVICE(0x1d50, 0x60a1) }, /* AirSpy */
  915. { }
  916. };
  917. MODULE_DEVICE_TABLE(usb, airspy_id_table);
  918. /* USB subsystem interface */
  919. static struct usb_driver airspy_driver = {
  920. .name = KBUILD_MODNAME,
  921. .probe = airspy_probe,
  922. .disconnect = airspy_disconnect,
  923. .id_table = airspy_id_table,
  924. };
  925. module_usb_driver(airspy_driver);
  926. MODULE_AUTHOR("Antti Palosaari <[email protected]>");
  927. MODULE_DESCRIPTION("AirSpy SDR");
  928. MODULE_LICENSE("GPL");