hackrf.c 29 KB

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