airspy.c 28 KB

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