airspy.c 28 KB

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