airspy.c 28 KB

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