hackrf.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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-v4l2.h>
  24. #include <media/videobuf2-vmalloc.h>
  25. /*
  26. * Used Avago MGA-81563 RF amplifier could be destroyed pretty easily with too
  27. * strong signal or transmitting to bad antenna.
  28. * Set RF gain control to 'grabbed' state by default for sure.
  29. */
  30. static bool hackrf_enable_rf_gain_ctrl;
  31. module_param_named(enable_rf_gain_ctrl, hackrf_enable_rf_gain_ctrl, bool, 0644);
  32. MODULE_PARM_DESC(enable_rf_gain_ctrl, "enable RX/TX RF amplifier control (warn: could damage amplifier)");
  33. /* HackRF USB API commands (from HackRF Library) */
  34. enum {
  35. CMD_SET_TRANSCEIVER_MODE = 0x01,
  36. CMD_SAMPLE_RATE_SET = 0x06,
  37. CMD_BASEBAND_FILTER_BANDWIDTH_SET = 0x07,
  38. CMD_BOARD_ID_READ = 0x0e,
  39. CMD_VERSION_STRING_READ = 0x0f,
  40. CMD_SET_FREQ = 0x10,
  41. CMD_AMP_ENABLE = 0x11,
  42. CMD_SET_LNA_GAIN = 0x13,
  43. CMD_SET_VGA_GAIN = 0x14,
  44. CMD_SET_TXVGA_GAIN = 0x15,
  45. };
  46. /*
  47. * bEndpointAddress 0x81 EP 1 IN
  48. * Transfer Type Bulk
  49. * wMaxPacketSize 0x0200 1x 512 bytes
  50. */
  51. #define MAX_BULK_BUFS (6)
  52. #define BULK_BUFFER_SIZE (128 * 512)
  53. static const struct v4l2_frequency_band bands_adc_dac[] = {
  54. {
  55. .tuner = 0,
  56. .type = V4L2_TUNER_SDR,
  57. .index = 0,
  58. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  59. .rangelow = 200000,
  60. .rangehigh = 24000000,
  61. },
  62. };
  63. static const struct v4l2_frequency_band bands_rx_tx[] = {
  64. {
  65. .tuner = 1,
  66. .type = V4L2_TUNER_RF,
  67. .index = 0,
  68. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  69. .rangelow = 1,
  70. .rangehigh = 4294967294LL, /* max u32, hw goes over 7GHz */
  71. },
  72. };
  73. /* stream formats */
  74. struct hackrf_format {
  75. u32 pixelformat;
  76. u32 buffersize;
  77. };
  78. /* format descriptions for capture and preview */
  79. static struct hackrf_format formats[] = {
  80. {
  81. .pixelformat = V4L2_SDR_FMT_CS8,
  82. .buffersize = BULK_BUFFER_SIZE,
  83. },
  84. };
  85. static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats);
  86. /* intermediate buffers with raw data from the USB device */
  87. struct hackrf_buffer {
  88. struct vb2_v4l2_buffer vb;
  89. struct list_head list;
  90. };
  91. struct hackrf_dev {
  92. #define USB_STATE_URB_BUF 1 /* XXX: set manually */
  93. #define RX_ON 4
  94. #define TX_ON 5
  95. #define RX_ADC_FREQUENCY 11
  96. #define TX_DAC_FREQUENCY 12
  97. #define RX_BANDWIDTH 13
  98. #define TX_BANDWIDTH 14
  99. #define RX_RF_FREQUENCY 15
  100. #define TX_RF_FREQUENCY 16
  101. #define RX_RF_GAIN 17
  102. #define TX_RF_GAIN 18
  103. #define RX_IF_GAIN 19
  104. #define RX_LNA_GAIN 20
  105. #define TX_LNA_GAIN 21
  106. unsigned long flags;
  107. struct usb_interface *intf;
  108. struct device *dev;
  109. struct usb_device *udev;
  110. struct video_device rx_vdev;
  111. struct video_device tx_vdev;
  112. struct v4l2_device v4l2_dev;
  113. /* videobuf2 queue and queued buffers list */
  114. struct vb2_queue rx_vb2_queue;
  115. struct vb2_queue tx_vb2_queue;
  116. struct list_head rx_buffer_list;
  117. struct list_head tx_buffer_list;
  118. spinlock_t buffer_list_lock; /* Protects buffer_list */
  119. unsigned sequence; /* Buffer sequence counter */
  120. unsigned int vb_full; /* vb is full and packets dropped */
  121. unsigned int vb_empty; /* vb is empty and packets dropped */
  122. /* Note if taking both locks v4l2_lock must always be locked first! */
  123. struct mutex v4l2_lock; /* Protects everything else */
  124. struct mutex vb_queue_lock; /* Protects vb_queue */
  125. struct urb *urb_list[MAX_BULK_BUFS];
  126. int buf_num;
  127. unsigned long buf_size;
  128. u8 *buf_list[MAX_BULK_BUFS];
  129. dma_addr_t dma_addr[MAX_BULK_BUFS];
  130. int urbs_initialized;
  131. int urbs_submitted;
  132. /* USB control message buffer */
  133. #define BUF_SIZE 24
  134. u8 buf[BUF_SIZE];
  135. /* Current configuration */
  136. unsigned int f_adc;
  137. unsigned int f_dac;
  138. unsigned int f_rx;
  139. unsigned int f_tx;
  140. u32 pixelformat;
  141. u32 buffersize;
  142. /* Controls */
  143. struct v4l2_ctrl_handler rx_ctrl_handler;
  144. struct v4l2_ctrl *rx_bandwidth_auto;
  145. struct v4l2_ctrl *rx_bandwidth;
  146. struct v4l2_ctrl *rx_rf_gain;
  147. struct v4l2_ctrl *rx_lna_gain;
  148. struct v4l2_ctrl *rx_if_gain;
  149. struct v4l2_ctrl_handler tx_ctrl_handler;
  150. struct v4l2_ctrl *tx_bandwidth_auto;
  151. struct v4l2_ctrl *tx_bandwidth;
  152. struct v4l2_ctrl *tx_rf_gain;
  153. struct v4l2_ctrl *tx_lna_gain;
  154. /* Sample rate calc */
  155. unsigned long jiffies_next;
  156. unsigned int sample;
  157. unsigned int sample_measured;
  158. };
  159. #define hackrf_dbg_usb_control_msg(_dev, _r, _t, _v, _i, _b, _l) { \
  160. char *_direction; \
  161. if (_t & USB_DIR_IN) \
  162. _direction = "<<<"; \
  163. else \
  164. _direction = ">>>"; \
  165. dev_dbg(_dev, "%02x %02x %02x %02x %02x %02x %02x %02x %s %*ph\n", \
  166. _t, _r, _v & 0xff, _v >> 8, _i & 0xff, \
  167. _i >> 8, _l & 0xff, _l >> 8, _direction, _l, _b); \
  168. }
  169. /* execute firmware command */
  170. static int hackrf_ctrl_msg(struct hackrf_dev *dev, u8 request, u16 value,
  171. u16 index, u8 *data, u16 size)
  172. {
  173. int ret;
  174. unsigned int pipe;
  175. u8 requesttype;
  176. switch (request) {
  177. case CMD_SET_TRANSCEIVER_MODE:
  178. case CMD_SET_FREQ:
  179. case CMD_AMP_ENABLE:
  180. case CMD_SAMPLE_RATE_SET:
  181. case CMD_BASEBAND_FILTER_BANDWIDTH_SET:
  182. pipe = usb_sndctrlpipe(dev->udev, 0);
  183. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  184. break;
  185. case CMD_BOARD_ID_READ:
  186. case CMD_VERSION_STRING_READ:
  187. case CMD_SET_LNA_GAIN:
  188. case CMD_SET_VGA_GAIN:
  189. case CMD_SET_TXVGA_GAIN:
  190. pipe = usb_rcvctrlpipe(dev->udev, 0);
  191. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  192. break;
  193. default:
  194. dev_err(dev->dev, "Unknown command %02x\n", request);
  195. ret = -EINVAL;
  196. goto err;
  197. }
  198. /* write request */
  199. if (!(requesttype & USB_DIR_IN))
  200. memcpy(dev->buf, data, size);
  201. ret = usb_control_msg(dev->udev, pipe, request, requesttype, value,
  202. index, dev->buf, size, 1000);
  203. hackrf_dbg_usb_control_msg(dev->dev, request, requesttype, value,
  204. index, dev->buf, size);
  205. if (ret < 0) {
  206. dev_err(dev->dev, "usb_control_msg() failed %d request %02x\n",
  207. ret, request);
  208. goto err;
  209. }
  210. /* read request */
  211. if (requesttype & USB_DIR_IN)
  212. memcpy(data, dev->buf, size);
  213. return 0;
  214. err:
  215. return ret;
  216. }
  217. static int hackrf_set_params(struct hackrf_dev *dev)
  218. {
  219. struct usb_interface *intf = dev->intf;
  220. int ret, i;
  221. u8 buf[8], u8tmp;
  222. unsigned int uitmp, uitmp1, uitmp2;
  223. const bool rx = test_bit(RX_ON, &dev->flags);
  224. const bool tx = test_bit(TX_ON, &dev->flags);
  225. static const struct {
  226. u32 freq;
  227. } bandwidth_lut[] = {
  228. { 1750000}, /* 1.75 MHz */
  229. { 2500000}, /* 2.5 MHz */
  230. { 3500000}, /* 3.5 MHz */
  231. { 5000000}, /* 5 MHz */
  232. { 5500000}, /* 5.5 MHz */
  233. { 6000000}, /* 6 MHz */
  234. { 7000000}, /* 7 MHz */
  235. { 8000000}, /* 8 MHz */
  236. { 9000000}, /* 9 MHz */
  237. {10000000}, /* 10 MHz */
  238. {12000000}, /* 12 MHz */
  239. {14000000}, /* 14 MHz */
  240. {15000000}, /* 15 MHz */
  241. {20000000}, /* 20 MHz */
  242. {24000000}, /* 24 MHz */
  243. {28000000}, /* 28 MHz */
  244. };
  245. if (!rx && !tx) {
  246. dev_dbg(&intf->dev, "device is sleeping\n");
  247. return 0;
  248. }
  249. /* ADC / DAC frequency */
  250. if (rx && test_and_clear_bit(RX_ADC_FREQUENCY, &dev->flags)) {
  251. dev_dbg(&intf->dev, "RX ADC frequency=%u Hz\n", dev->f_adc);
  252. uitmp1 = dev->f_adc;
  253. uitmp2 = 1;
  254. set_bit(TX_DAC_FREQUENCY, &dev->flags);
  255. } else if (tx && test_and_clear_bit(TX_DAC_FREQUENCY, &dev->flags)) {
  256. dev_dbg(&intf->dev, "TX DAC frequency=%u Hz\n", dev->f_dac);
  257. uitmp1 = dev->f_dac;
  258. uitmp2 = 1;
  259. set_bit(RX_ADC_FREQUENCY, &dev->flags);
  260. } else {
  261. uitmp1 = uitmp2 = 0;
  262. }
  263. if (uitmp1 || uitmp2) {
  264. buf[0] = (uitmp1 >> 0) & 0xff;
  265. buf[1] = (uitmp1 >> 8) & 0xff;
  266. buf[2] = (uitmp1 >> 16) & 0xff;
  267. buf[3] = (uitmp1 >> 24) & 0xff;
  268. buf[4] = (uitmp2 >> 0) & 0xff;
  269. buf[5] = (uitmp2 >> 8) & 0xff;
  270. buf[6] = (uitmp2 >> 16) & 0xff;
  271. buf[7] = (uitmp2 >> 24) & 0xff;
  272. ret = hackrf_ctrl_msg(dev, CMD_SAMPLE_RATE_SET, 0, 0, buf, 8);
  273. if (ret)
  274. goto err;
  275. }
  276. /* bandwidth */
  277. if (rx && test_and_clear_bit(RX_BANDWIDTH, &dev->flags)) {
  278. if (dev->rx_bandwidth_auto->val == true)
  279. uitmp = dev->f_adc;
  280. else
  281. uitmp = dev->rx_bandwidth->val;
  282. for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
  283. if (uitmp <= bandwidth_lut[i].freq) {
  284. uitmp = bandwidth_lut[i].freq;
  285. break;
  286. }
  287. }
  288. dev->rx_bandwidth->val = uitmp;
  289. dev->rx_bandwidth->cur.val = uitmp;
  290. dev_dbg(&intf->dev, "RX bandwidth selected=%u\n", uitmp);
  291. set_bit(TX_BANDWIDTH, &dev->flags);
  292. } else if (tx && test_and_clear_bit(TX_BANDWIDTH, &dev->flags)) {
  293. if (dev->tx_bandwidth_auto->val == true)
  294. uitmp = dev->f_dac;
  295. else
  296. uitmp = dev->tx_bandwidth->val;
  297. for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
  298. if (uitmp <= bandwidth_lut[i].freq) {
  299. uitmp = bandwidth_lut[i].freq;
  300. break;
  301. }
  302. }
  303. dev->tx_bandwidth->val = uitmp;
  304. dev->tx_bandwidth->cur.val = uitmp;
  305. dev_dbg(&intf->dev, "TX bandwidth selected=%u\n", uitmp);
  306. set_bit(RX_BANDWIDTH, &dev->flags);
  307. } else {
  308. uitmp = 0;
  309. }
  310. if (uitmp) {
  311. uitmp1 = uitmp2 = 0;
  312. uitmp1 |= ((uitmp >> 0) & 0xff) << 0;
  313. uitmp1 |= ((uitmp >> 8) & 0xff) << 8;
  314. uitmp2 |= ((uitmp >> 16) & 0xff) << 0;
  315. uitmp2 |= ((uitmp >> 24) & 0xff) << 8;
  316. ret = hackrf_ctrl_msg(dev, CMD_BASEBAND_FILTER_BANDWIDTH_SET,
  317. uitmp1, uitmp2, NULL, 0);
  318. if (ret)
  319. goto err;
  320. }
  321. /* RX / TX RF frequency */
  322. if (rx && test_and_clear_bit(RX_RF_FREQUENCY, &dev->flags)) {
  323. dev_dbg(&intf->dev, "RX RF frequency=%u Hz\n", dev->f_rx);
  324. uitmp1 = dev->f_rx / 1000000;
  325. uitmp2 = dev->f_rx % 1000000;
  326. set_bit(TX_RF_FREQUENCY, &dev->flags);
  327. } else if (tx && test_and_clear_bit(TX_RF_FREQUENCY, &dev->flags)) {
  328. dev_dbg(&intf->dev, "TX RF frequency=%u Hz\n", dev->f_tx);
  329. uitmp1 = dev->f_tx / 1000000;
  330. uitmp2 = dev->f_tx % 1000000;
  331. set_bit(RX_RF_FREQUENCY, &dev->flags);
  332. } else {
  333. uitmp1 = uitmp2 = 0;
  334. }
  335. if (uitmp1 || uitmp2) {
  336. buf[0] = (uitmp1 >> 0) & 0xff;
  337. buf[1] = (uitmp1 >> 8) & 0xff;
  338. buf[2] = (uitmp1 >> 16) & 0xff;
  339. buf[3] = (uitmp1 >> 24) & 0xff;
  340. buf[4] = (uitmp2 >> 0) & 0xff;
  341. buf[5] = (uitmp2 >> 8) & 0xff;
  342. buf[6] = (uitmp2 >> 16) & 0xff;
  343. buf[7] = (uitmp2 >> 24) & 0xff;
  344. ret = hackrf_ctrl_msg(dev, CMD_SET_FREQ, 0, 0, buf, 8);
  345. if (ret)
  346. goto err;
  347. }
  348. /* RX RF gain */
  349. if (rx && test_and_clear_bit(RX_RF_GAIN, &dev->flags)) {
  350. dev_dbg(&intf->dev, "RX RF gain val=%d->%d\n",
  351. dev->rx_rf_gain->cur.val, dev->rx_rf_gain->val);
  352. u8tmp = (dev->rx_rf_gain->val) ? 1 : 0;
  353. ret = hackrf_ctrl_msg(dev, CMD_AMP_ENABLE, u8tmp, 0, NULL, 0);
  354. if (ret)
  355. goto err;
  356. set_bit(TX_RF_GAIN, &dev->flags);
  357. }
  358. /* TX RF gain */
  359. if (tx && test_and_clear_bit(TX_RF_GAIN, &dev->flags)) {
  360. dev_dbg(&intf->dev, "TX RF gain val=%d->%d\n",
  361. dev->tx_rf_gain->cur.val, dev->tx_rf_gain->val);
  362. u8tmp = (dev->tx_rf_gain->val) ? 1 : 0;
  363. ret = hackrf_ctrl_msg(dev, CMD_AMP_ENABLE, u8tmp, 0, NULL, 0);
  364. if (ret)
  365. goto err;
  366. set_bit(RX_RF_GAIN, &dev->flags);
  367. }
  368. /* RX LNA gain */
  369. if (rx && test_and_clear_bit(RX_LNA_GAIN, &dev->flags)) {
  370. dev_dbg(dev->dev, "RX LNA gain val=%d->%d\n",
  371. dev->rx_lna_gain->cur.val, dev->rx_lna_gain->val);
  372. ret = hackrf_ctrl_msg(dev, CMD_SET_LNA_GAIN, 0,
  373. dev->rx_lna_gain->val, &u8tmp, 1);
  374. if (ret)
  375. goto err;
  376. }
  377. /* RX IF gain */
  378. if (rx && test_and_clear_bit(RX_IF_GAIN, &dev->flags)) {
  379. dev_dbg(&intf->dev, "IF gain val=%d->%d\n",
  380. dev->rx_if_gain->cur.val, dev->rx_if_gain->val);
  381. ret = hackrf_ctrl_msg(dev, CMD_SET_VGA_GAIN, 0,
  382. dev->rx_if_gain->val, &u8tmp, 1);
  383. if (ret)
  384. goto err;
  385. }
  386. /* TX LNA gain */
  387. if (tx && test_and_clear_bit(TX_LNA_GAIN, &dev->flags)) {
  388. dev_dbg(&intf->dev, "TX LNA gain val=%d->%d\n",
  389. dev->tx_lna_gain->cur.val, dev->tx_lna_gain->val);
  390. ret = hackrf_ctrl_msg(dev, CMD_SET_TXVGA_GAIN, 0,
  391. dev->tx_lna_gain->val, &u8tmp, 1);
  392. if (ret)
  393. goto err;
  394. }
  395. return 0;
  396. err:
  397. dev_dbg(&intf->dev, "failed=%d\n", ret);
  398. return ret;
  399. }
  400. /* Private functions */
  401. static struct hackrf_buffer *hackrf_get_next_buffer(struct hackrf_dev *dev,
  402. struct list_head *buffer_list)
  403. {
  404. unsigned long flags;
  405. struct hackrf_buffer *buffer = NULL;
  406. spin_lock_irqsave(&dev->buffer_list_lock, flags);
  407. if (list_empty(buffer_list))
  408. goto leave;
  409. buffer = list_entry(buffer_list->next, struct hackrf_buffer, list);
  410. list_del(&buffer->list);
  411. leave:
  412. spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
  413. return buffer;
  414. }
  415. static void hackrf_copy_stream(struct hackrf_dev *dev, void *dst, void *src,
  416. unsigned int src_len)
  417. {
  418. memcpy(dst, src, src_len);
  419. /* calculate sample rate and output it in 10 seconds intervals */
  420. if (unlikely(time_is_before_jiffies(dev->jiffies_next))) {
  421. #define MSECS 10000UL
  422. unsigned int msecs = jiffies_to_msecs(jiffies -
  423. dev->jiffies_next + msecs_to_jiffies(MSECS));
  424. unsigned int samples = dev->sample - dev->sample_measured;
  425. dev->jiffies_next = jiffies + msecs_to_jiffies(MSECS);
  426. dev->sample_measured = dev->sample;
  427. dev_dbg(dev->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n",
  428. src_len, samples, msecs,
  429. samples * 1000UL / msecs);
  430. }
  431. /* total number of samples */
  432. dev->sample += src_len / 2;
  433. }
  434. /*
  435. * This gets called for the bulk stream pipe. This is done in interrupt
  436. * time, so it has to be fast, not crash, and not stall. Neat.
  437. */
  438. static void hackrf_urb_complete_in(struct urb *urb)
  439. {
  440. struct hackrf_dev *dev = urb->context;
  441. struct usb_interface *intf = dev->intf;
  442. struct hackrf_buffer *buffer;
  443. unsigned int len;
  444. dev_dbg_ratelimited(&intf->dev, "status=%d length=%u/%u\n", urb->status,
  445. urb->actual_length, urb->transfer_buffer_length);
  446. switch (urb->status) {
  447. case 0: /* success */
  448. case -ETIMEDOUT: /* NAK */
  449. break;
  450. case -ECONNRESET: /* kill */
  451. case -ENOENT:
  452. case -ESHUTDOWN:
  453. return;
  454. default: /* error */
  455. dev_err_ratelimited(&intf->dev, "URB failed %d\n", urb->status);
  456. goto exit_usb_submit_urb;
  457. }
  458. /* get buffer to write */
  459. buffer = hackrf_get_next_buffer(dev, &dev->rx_buffer_list);
  460. if (unlikely(buffer == NULL)) {
  461. dev->vb_full++;
  462. dev_notice_ratelimited(&intf->dev,
  463. "buffer is full - %u packets dropped\n",
  464. dev->vb_full);
  465. goto exit_usb_submit_urb;
  466. }
  467. len = min_t(unsigned long, vb2_plane_size(&buffer->vb.vb2_buf, 0),
  468. urb->actual_length);
  469. hackrf_copy_stream(dev, vb2_plane_vaddr(&buffer->vb.vb2_buf, 0),
  470. urb->transfer_buffer, len);
  471. vb2_set_plane_payload(&buffer->vb.vb2_buf, 0, len);
  472. buffer->vb.sequence = dev->sequence++;
  473. v4l2_get_timestamp(&buffer->vb.timestamp);
  474. vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_DONE);
  475. exit_usb_submit_urb:
  476. usb_submit_urb(urb, GFP_ATOMIC);
  477. }
  478. static void hackrf_urb_complete_out(struct urb *urb)
  479. {
  480. struct hackrf_dev *dev = urb->context;
  481. struct usb_interface *intf = dev->intf;
  482. struct hackrf_buffer *buffer;
  483. unsigned int len;
  484. dev_dbg_ratelimited(&intf->dev, "status=%d length=%u/%u\n", urb->status,
  485. urb->actual_length, urb->transfer_buffer_length);
  486. switch (urb->status) {
  487. case 0: /* success */
  488. case -ETIMEDOUT: /* NAK */
  489. break;
  490. case -ECONNRESET: /* kill */
  491. case -ENOENT:
  492. case -ESHUTDOWN:
  493. return;
  494. default: /* error */
  495. dev_err_ratelimited(&intf->dev, "URB failed %d\n", urb->status);
  496. }
  497. /* get buffer to read */
  498. buffer = hackrf_get_next_buffer(dev, &dev->tx_buffer_list);
  499. if (unlikely(buffer == NULL)) {
  500. dev->vb_empty++;
  501. dev_notice_ratelimited(&intf->dev,
  502. "buffer is empty - %u packets dropped\n",
  503. dev->vb_empty);
  504. urb->actual_length = 0;
  505. goto exit_usb_submit_urb;
  506. }
  507. len = min_t(unsigned long, urb->transfer_buffer_length,
  508. vb2_get_plane_payload(&buffer->vb.vb2_buf, 0));
  509. hackrf_copy_stream(dev, urb->transfer_buffer,
  510. vb2_plane_vaddr(&buffer->vb.vb2_buf, 0), len);
  511. urb->actual_length = len;
  512. buffer->vb.sequence = dev->sequence++;
  513. v4l2_get_timestamp(&buffer->vb.timestamp);
  514. vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_DONE);
  515. exit_usb_submit_urb:
  516. usb_submit_urb(urb, GFP_ATOMIC);
  517. }
  518. static int hackrf_kill_urbs(struct hackrf_dev *dev)
  519. {
  520. int i;
  521. for (i = dev->urbs_submitted - 1; i >= 0; i--) {
  522. dev_dbg(dev->dev, "kill urb=%d\n", i);
  523. /* stop the URB */
  524. usb_kill_urb(dev->urb_list[i]);
  525. }
  526. dev->urbs_submitted = 0;
  527. return 0;
  528. }
  529. static int hackrf_submit_urbs(struct hackrf_dev *dev)
  530. {
  531. int i, ret;
  532. for (i = 0; i < dev->urbs_initialized; i++) {
  533. dev_dbg(dev->dev, "submit urb=%d\n", i);
  534. ret = usb_submit_urb(dev->urb_list[i], GFP_ATOMIC);
  535. if (ret) {
  536. dev_err(dev->dev, "Could not submit URB no. %d - get them all back\n",
  537. i);
  538. hackrf_kill_urbs(dev);
  539. return ret;
  540. }
  541. dev->urbs_submitted++;
  542. }
  543. return 0;
  544. }
  545. static int hackrf_free_stream_bufs(struct hackrf_dev *dev)
  546. {
  547. if (dev->flags & USB_STATE_URB_BUF) {
  548. while (dev->buf_num) {
  549. dev->buf_num--;
  550. dev_dbg(dev->dev, "free buf=%d\n", dev->buf_num);
  551. usb_free_coherent(dev->udev, dev->buf_size,
  552. dev->buf_list[dev->buf_num],
  553. dev->dma_addr[dev->buf_num]);
  554. }
  555. }
  556. dev->flags &= ~USB_STATE_URB_BUF;
  557. return 0;
  558. }
  559. static int hackrf_alloc_stream_bufs(struct hackrf_dev *dev)
  560. {
  561. dev->buf_num = 0;
  562. dev->buf_size = BULK_BUFFER_SIZE;
  563. dev_dbg(dev->dev, "all in all I will use %u bytes for streaming\n",
  564. MAX_BULK_BUFS * BULK_BUFFER_SIZE);
  565. for (dev->buf_num = 0; dev->buf_num < MAX_BULK_BUFS; dev->buf_num++) {
  566. dev->buf_list[dev->buf_num] = usb_alloc_coherent(dev->udev,
  567. BULK_BUFFER_SIZE, GFP_ATOMIC,
  568. &dev->dma_addr[dev->buf_num]);
  569. if (!dev->buf_list[dev->buf_num]) {
  570. dev_dbg(dev->dev, "alloc buf=%d failed\n",
  571. dev->buf_num);
  572. hackrf_free_stream_bufs(dev);
  573. return -ENOMEM;
  574. }
  575. dev_dbg(dev->dev, "alloc buf=%d %p (dma %llu)\n", dev->buf_num,
  576. dev->buf_list[dev->buf_num],
  577. (long long)dev->dma_addr[dev->buf_num]);
  578. dev->flags |= USB_STATE_URB_BUF;
  579. }
  580. return 0;
  581. }
  582. static int hackrf_free_urbs(struct hackrf_dev *dev)
  583. {
  584. int i;
  585. hackrf_kill_urbs(dev);
  586. for (i = dev->urbs_initialized - 1; i >= 0; i--) {
  587. if (dev->urb_list[i]) {
  588. dev_dbg(dev->dev, "free urb=%d\n", i);
  589. /* free the URBs */
  590. usb_free_urb(dev->urb_list[i]);
  591. }
  592. }
  593. dev->urbs_initialized = 0;
  594. return 0;
  595. }
  596. static int hackrf_alloc_urbs(struct hackrf_dev *dev, bool rcv)
  597. {
  598. int i, j;
  599. unsigned int pipe;
  600. usb_complete_t complete;
  601. if (rcv) {
  602. pipe = usb_rcvbulkpipe(dev->udev, 0x81);
  603. complete = &hackrf_urb_complete_in;
  604. } else {
  605. pipe = usb_sndbulkpipe(dev->udev, 0x02);
  606. complete = &hackrf_urb_complete_out;
  607. }
  608. /* allocate the URBs */
  609. for (i = 0; i < MAX_BULK_BUFS; i++) {
  610. dev_dbg(dev->dev, "alloc urb=%d\n", i);
  611. dev->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC);
  612. if (!dev->urb_list[i]) {
  613. dev_dbg(dev->dev, "failed\n");
  614. for (j = 0; j < i; j++)
  615. usb_free_urb(dev->urb_list[j]);
  616. return -ENOMEM;
  617. }
  618. usb_fill_bulk_urb(dev->urb_list[i],
  619. dev->udev,
  620. pipe,
  621. dev->buf_list[i],
  622. BULK_BUFFER_SIZE,
  623. complete, dev);
  624. dev->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  625. dev->urb_list[i]->transfer_dma = dev->dma_addr[i];
  626. dev->urbs_initialized++;
  627. }
  628. return 0;
  629. }
  630. /* The user yanked out the cable... */
  631. static void hackrf_disconnect(struct usb_interface *intf)
  632. {
  633. struct v4l2_device *v = usb_get_intfdata(intf);
  634. struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev);
  635. dev_dbg(dev->dev, "\n");
  636. mutex_lock(&dev->vb_queue_lock);
  637. mutex_lock(&dev->v4l2_lock);
  638. /* No need to keep the urbs around after disconnection */
  639. dev->udev = NULL;
  640. v4l2_device_disconnect(&dev->v4l2_dev);
  641. video_unregister_device(&dev->tx_vdev);
  642. video_unregister_device(&dev->rx_vdev);
  643. mutex_unlock(&dev->v4l2_lock);
  644. mutex_unlock(&dev->vb_queue_lock);
  645. v4l2_device_put(&dev->v4l2_dev);
  646. }
  647. /* Videobuf2 operations */
  648. static void hackrf_return_all_buffers(struct vb2_queue *vq,
  649. enum vb2_buffer_state state)
  650. {
  651. struct hackrf_dev *dev = vb2_get_drv_priv(vq);
  652. struct usb_interface *intf = dev->intf;
  653. struct hackrf_buffer *buffer, *node;
  654. struct list_head *buffer_list;
  655. unsigned long flags;
  656. dev_dbg(&intf->dev, "\n");
  657. if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
  658. buffer_list = &dev->rx_buffer_list;
  659. else
  660. buffer_list = &dev->tx_buffer_list;
  661. spin_lock_irqsave(&dev->buffer_list_lock, flags);
  662. list_for_each_entry_safe(buffer, node, buffer_list, list) {
  663. dev_dbg(&intf->dev, "list_for_each_entry_safe\n");
  664. vb2_buffer_done(&buffer->vb.vb2_buf, state);
  665. list_del(&buffer->list);
  666. }
  667. spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
  668. }
  669. static int hackrf_queue_setup(struct vb2_queue *vq,
  670. const void *parg, unsigned int *nbuffers,
  671. unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
  672. {
  673. struct hackrf_dev *dev = vb2_get_drv_priv(vq);
  674. dev_dbg(dev->dev, "nbuffers=%d\n", *nbuffers);
  675. /* Need at least 8 buffers */
  676. if (vq->num_buffers + *nbuffers < 8)
  677. *nbuffers = 8 - vq->num_buffers;
  678. *nplanes = 1;
  679. sizes[0] = PAGE_ALIGN(dev->buffersize);
  680. dev_dbg(dev->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]);
  681. return 0;
  682. }
  683. static void hackrf_buf_queue(struct vb2_buffer *vb)
  684. {
  685. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  686. struct vb2_queue *vq = vb->vb2_queue;
  687. struct hackrf_dev *dev = vb2_get_drv_priv(vq);
  688. struct hackrf_buffer *buffer = container_of(vbuf, struct hackrf_buffer, vb);
  689. struct list_head *buffer_list;
  690. unsigned long flags;
  691. dev_dbg_ratelimited(&dev->intf->dev, "\n");
  692. if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
  693. buffer_list = &dev->rx_buffer_list;
  694. else
  695. buffer_list = &dev->tx_buffer_list;
  696. spin_lock_irqsave(&dev->buffer_list_lock, flags);
  697. list_add_tail(&buffer->list, buffer_list);
  698. spin_unlock_irqrestore(&dev->buffer_list_lock, flags);
  699. }
  700. static int hackrf_start_streaming(struct vb2_queue *vq, unsigned int count)
  701. {
  702. struct hackrf_dev *dev = vb2_get_drv_priv(vq);
  703. struct usb_interface *intf = dev->intf;
  704. int ret;
  705. unsigned int mode;
  706. dev_dbg(&intf->dev, "count=%i\n", count);
  707. mutex_lock(&dev->v4l2_lock);
  708. /* Allow only RX or TX, not both same time */
  709. if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE) {
  710. if (test_bit(TX_ON, &dev->flags)) {
  711. ret = -EBUSY;
  712. goto err_hackrf_return_all_buffers;
  713. }
  714. mode = 1;
  715. set_bit(RX_ON, &dev->flags);
  716. } else {
  717. if (test_bit(RX_ON, &dev->flags)) {
  718. ret = -EBUSY;
  719. goto err_hackrf_return_all_buffers;
  720. }
  721. mode = 2;
  722. set_bit(TX_ON, &dev->flags);
  723. }
  724. dev->sequence = 0;
  725. ret = hackrf_alloc_stream_bufs(dev);
  726. if (ret)
  727. goto err;
  728. ret = hackrf_alloc_urbs(dev, (mode == 1));
  729. if (ret)
  730. goto err;
  731. ret = hackrf_submit_urbs(dev);
  732. if (ret)
  733. goto err;
  734. ret = hackrf_set_params(dev);
  735. if (ret)
  736. goto err;
  737. /* start hardware streaming */
  738. ret = hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, mode, 0, NULL, 0);
  739. if (ret)
  740. goto err;
  741. mutex_unlock(&dev->v4l2_lock);
  742. return 0;
  743. err:
  744. hackrf_kill_urbs(dev);
  745. hackrf_free_urbs(dev);
  746. hackrf_free_stream_bufs(dev);
  747. clear_bit(RX_ON, &dev->flags);
  748. clear_bit(TX_ON, &dev->flags);
  749. err_hackrf_return_all_buffers:
  750. hackrf_return_all_buffers(vq, VB2_BUF_STATE_QUEUED);
  751. mutex_unlock(&dev->v4l2_lock);
  752. dev_dbg(&intf->dev, "failed=%d\n", ret);
  753. return ret;
  754. }
  755. static void hackrf_stop_streaming(struct vb2_queue *vq)
  756. {
  757. struct hackrf_dev *dev = vb2_get_drv_priv(vq);
  758. struct usb_interface *intf = dev->intf;
  759. dev_dbg(&intf->dev, "\n");
  760. mutex_lock(&dev->v4l2_lock);
  761. /* stop hardware streaming */
  762. hackrf_ctrl_msg(dev, CMD_SET_TRANSCEIVER_MODE, 0, 0, NULL, 0);
  763. hackrf_kill_urbs(dev);
  764. hackrf_free_urbs(dev);
  765. hackrf_free_stream_bufs(dev);
  766. hackrf_return_all_buffers(vq, VB2_BUF_STATE_ERROR);
  767. if (vq->type == V4L2_BUF_TYPE_SDR_CAPTURE)
  768. clear_bit(RX_ON, &dev->flags);
  769. else
  770. clear_bit(TX_ON, &dev->flags);
  771. mutex_unlock(&dev->v4l2_lock);
  772. }
  773. static struct vb2_ops hackrf_vb2_ops = {
  774. .queue_setup = hackrf_queue_setup,
  775. .buf_queue = hackrf_buf_queue,
  776. .start_streaming = hackrf_start_streaming,
  777. .stop_streaming = hackrf_stop_streaming,
  778. .wait_prepare = vb2_ops_wait_prepare,
  779. .wait_finish = vb2_ops_wait_finish,
  780. };
  781. static int hackrf_querycap(struct file *file, void *fh,
  782. struct v4l2_capability *cap)
  783. {
  784. struct hackrf_dev *dev = video_drvdata(file);
  785. struct usb_interface *intf = dev->intf;
  786. struct video_device *vdev = video_devdata(file);
  787. dev_dbg(&intf->dev, "\n");
  788. if (vdev->vfl_dir == VFL_DIR_RX)
  789. cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
  790. V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  791. else
  792. cap->device_caps = V4L2_CAP_SDR_OUTPUT | V4L2_CAP_MODULATOR |
  793. V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  794. cap->capabilities = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER |
  795. V4L2_CAP_SDR_OUTPUT | V4L2_CAP_MODULATOR |
  796. V4L2_CAP_STREAMING | V4L2_CAP_READWRITE |
  797. V4L2_CAP_DEVICE_CAPS;
  798. strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  799. strlcpy(cap->card, dev->rx_vdev.name, sizeof(cap->card));
  800. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  801. return 0;
  802. }
  803. static int hackrf_s_fmt_sdr(struct file *file, void *priv,
  804. struct v4l2_format *f)
  805. {
  806. struct hackrf_dev *dev = video_drvdata(file);
  807. struct video_device *vdev = video_devdata(file);
  808. struct vb2_queue *q;
  809. int i;
  810. dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
  811. (char *)&f->fmt.sdr.pixelformat);
  812. if (vdev->vfl_dir == VFL_DIR_RX)
  813. q = &dev->rx_vb2_queue;
  814. else
  815. q = &dev->tx_vb2_queue;
  816. if (vb2_is_busy(q))
  817. return -EBUSY;
  818. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  819. for (i = 0; i < NUM_FORMATS; i++) {
  820. if (f->fmt.sdr.pixelformat == formats[i].pixelformat) {
  821. dev->pixelformat = formats[i].pixelformat;
  822. dev->buffersize = formats[i].buffersize;
  823. f->fmt.sdr.buffersize = formats[i].buffersize;
  824. return 0;
  825. }
  826. }
  827. dev->pixelformat = formats[0].pixelformat;
  828. dev->buffersize = formats[0].buffersize;
  829. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  830. f->fmt.sdr.buffersize = formats[0].buffersize;
  831. return 0;
  832. }
  833. static int hackrf_g_fmt_sdr(struct file *file, void *priv,
  834. struct v4l2_format *f)
  835. {
  836. struct hackrf_dev *dev = video_drvdata(file);
  837. dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
  838. (char *)&dev->pixelformat);
  839. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  840. f->fmt.sdr.pixelformat = dev->pixelformat;
  841. f->fmt.sdr.buffersize = dev->buffersize;
  842. return 0;
  843. }
  844. static int hackrf_try_fmt_sdr(struct file *file, void *priv,
  845. struct v4l2_format *f)
  846. {
  847. struct hackrf_dev *dev = video_drvdata(file);
  848. int i;
  849. dev_dbg(dev->dev, "pixelformat fourcc %4.4s\n",
  850. (char *)&f->fmt.sdr.pixelformat);
  851. memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
  852. for (i = 0; i < NUM_FORMATS; i++) {
  853. if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
  854. f->fmt.sdr.buffersize = formats[i].buffersize;
  855. return 0;
  856. }
  857. }
  858. f->fmt.sdr.pixelformat = formats[0].pixelformat;
  859. f->fmt.sdr.buffersize = formats[0].buffersize;
  860. return 0;
  861. }
  862. static int hackrf_enum_fmt_sdr(struct file *file, void *priv,
  863. struct v4l2_fmtdesc *f)
  864. {
  865. struct hackrf_dev *dev = video_drvdata(file);
  866. dev_dbg(dev->dev, "index=%d\n", f->index);
  867. if (f->index >= NUM_FORMATS)
  868. return -EINVAL;
  869. f->pixelformat = formats[f->index].pixelformat;
  870. return 0;
  871. }
  872. static int hackrf_s_tuner(struct file *file, void *priv,
  873. const struct v4l2_tuner *v)
  874. {
  875. struct hackrf_dev *dev = video_drvdata(file);
  876. int ret;
  877. dev_dbg(dev->dev, "index=%d\n", v->index);
  878. if (v->index == 0)
  879. ret = 0;
  880. else if (v->index == 1)
  881. ret = 0;
  882. else
  883. ret = -EINVAL;
  884. return ret;
  885. }
  886. static int hackrf_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
  887. {
  888. struct hackrf_dev *dev = video_drvdata(file);
  889. int ret;
  890. dev_dbg(dev->dev, "index=%d\n", v->index);
  891. if (v->index == 0) {
  892. strlcpy(v->name, "HackRF ADC", sizeof(v->name));
  893. v->type = V4L2_TUNER_SDR;
  894. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  895. v->rangelow = bands_adc_dac[0].rangelow;
  896. v->rangehigh = bands_adc_dac[0].rangehigh;
  897. ret = 0;
  898. } else if (v->index == 1) {
  899. strlcpy(v->name, "HackRF RF", sizeof(v->name));
  900. v->type = V4L2_TUNER_RF;
  901. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  902. v->rangelow = bands_rx_tx[0].rangelow;
  903. v->rangehigh = bands_rx_tx[0].rangehigh;
  904. ret = 0;
  905. } else {
  906. ret = -EINVAL;
  907. }
  908. return ret;
  909. }
  910. static int hackrf_s_modulator(struct file *file, void *fh,
  911. const struct v4l2_modulator *a)
  912. {
  913. struct hackrf_dev *dev = video_drvdata(file);
  914. dev_dbg(dev->dev, "index=%d\n", a->index);
  915. return a->index > 1 ? -EINVAL : 0;
  916. }
  917. static int hackrf_g_modulator(struct file *file, void *fh,
  918. struct v4l2_modulator *a)
  919. {
  920. struct hackrf_dev *dev = video_drvdata(file);
  921. int ret;
  922. dev_dbg(dev->dev, "index=%d\n", a->index);
  923. if (a->index == 0) {
  924. strlcpy(a->name, "HackRF DAC", sizeof(a->name));
  925. a->type = V4L2_TUNER_SDR;
  926. a->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  927. a->rangelow = bands_adc_dac[0].rangelow;
  928. a->rangehigh = bands_adc_dac[0].rangehigh;
  929. ret = 0;
  930. } else if (a->index == 1) {
  931. strlcpy(a->name, "HackRF RF", sizeof(a->name));
  932. a->type = V4L2_TUNER_RF;
  933. a->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  934. a->rangelow = bands_rx_tx[0].rangelow;
  935. a->rangehigh = bands_rx_tx[0].rangehigh;
  936. ret = 0;
  937. } else {
  938. ret = -EINVAL;
  939. }
  940. return ret;
  941. }
  942. static int hackrf_s_frequency(struct file *file, void *priv,
  943. const struct v4l2_frequency *f)
  944. {
  945. struct hackrf_dev *dev = video_drvdata(file);
  946. struct usb_interface *intf = dev->intf;
  947. struct video_device *vdev = video_devdata(file);
  948. int ret;
  949. unsigned int uitmp;
  950. dev_dbg(&intf->dev, "tuner=%d type=%d frequency=%u\n",
  951. f->tuner, f->type, f->frequency);
  952. if (f->tuner == 0) {
  953. uitmp = clamp(f->frequency, bands_adc_dac[0].rangelow,
  954. bands_adc_dac[0].rangehigh);
  955. if (vdev->vfl_dir == VFL_DIR_RX) {
  956. dev->f_adc = uitmp;
  957. set_bit(RX_ADC_FREQUENCY, &dev->flags);
  958. } else {
  959. dev->f_dac = uitmp;
  960. set_bit(TX_DAC_FREQUENCY, &dev->flags);
  961. }
  962. } else if (f->tuner == 1) {
  963. uitmp = clamp(f->frequency, bands_rx_tx[0].rangelow,
  964. bands_rx_tx[0].rangehigh);
  965. if (vdev->vfl_dir == VFL_DIR_RX) {
  966. dev->f_rx = uitmp;
  967. set_bit(RX_RF_FREQUENCY, &dev->flags);
  968. } else {
  969. dev->f_tx = uitmp;
  970. set_bit(TX_RF_FREQUENCY, &dev->flags);
  971. }
  972. } else {
  973. ret = -EINVAL;
  974. goto err;
  975. }
  976. ret = hackrf_set_params(dev);
  977. if (ret)
  978. goto err;
  979. return 0;
  980. err:
  981. dev_dbg(&intf->dev, "failed=%d\n", ret);
  982. return ret;
  983. }
  984. static int hackrf_g_frequency(struct file *file, void *priv,
  985. struct v4l2_frequency *f)
  986. {
  987. struct hackrf_dev *dev = video_drvdata(file);
  988. struct usb_interface *intf = dev->intf;
  989. struct video_device *vdev = video_devdata(file);
  990. int ret;
  991. dev_dbg(dev->dev, "tuner=%d type=%d\n", f->tuner, f->type);
  992. if (f->tuner == 0) {
  993. f->type = V4L2_TUNER_SDR;
  994. if (vdev->vfl_dir == VFL_DIR_RX)
  995. f->frequency = dev->f_adc;
  996. else
  997. f->frequency = dev->f_dac;
  998. } else if (f->tuner == 1) {
  999. f->type = V4L2_TUNER_RF;
  1000. if (vdev->vfl_dir == VFL_DIR_RX)
  1001. f->frequency = dev->f_rx;
  1002. else
  1003. f->frequency = dev->f_tx;
  1004. } else {
  1005. ret = -EINVAL;
  1006. goto err;
  1007. }
  1008. return 0;
  1009. err:
  1010. dev_dbg(&intf->dev, "failed=%d\n", ret);
  1011. return ret;
  1012. }
  1013. static int hackrf_enum_freq_bands(struct file *file, void *priv,
  1014. struct v4l2_frequency_band *band)
  1015. {
  1016. struct hackrf_dev *dev = video_drvdata(file);
  1017. int ret;
  1018. dev_dbg(dev->dev, "tuner=%d type=%d index=%d\n",
  1019. band->tuner, band->type, band->index);
  1020. if (band->tuner == 0) {
  1021. if (band->index >= ARRAY_SIZE(bands_adc_dac)) {
  1022. ret = -EINVAL;
  1023. } else {
  1024. *band = bands_adc_dac[band->index];
  1025. ret = 0;
  1026. }
  1027. } else if (band->tuner == 1) {
  1028. if (band->index >= ARRAY_SIZE(bands_rx_tx)) {
  1029. ret = -EINVAL;
  1030. } else {
  1031. *band = bands_rx_tx[band->index];
  1032. ret = 0;
  1033. }
  1034. } else {
  1035. ret = -EINVAL;
  1036. }
  1037. return ret;
  1038. }
  1039. static const struct v4l2_ioctl_ops hackrf_ioctl_ops = {
  1040. .vidioc_querycap = hackrf_querycap,
  1041. .vidioc_s_fmt_sdr_cap = hackrf_s_fmt_sdr,
  1042. .vidioc_g_fmt_sdr_cap = hackrf_g_fmt_sdr,
  1043. .vidioc_enum_fmt_sdr_cap = hackrf_enum_fmt_sdr,
  1044. .vidioc_try_fmt_sdr_cap = hackrf_try_fmt_sdr,
  1045. .vidioc_s_fmt_sdr_out = hackrf_s_fmt_sdr,
  1046. .vidioc_g_fmt_sdr_out = hackrf_g_fmt_sdr,
  1047. .vidioc_enum_fmt_sdr_out = hackrf_enum_fmt_sdr,
  1048. .vidioc_try_fmt_sdr_out = hackrf_try_fmt_sdr,
  1049. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1050. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1051. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1052. .vidioc_querybuf = vb2_ioctl_querybuf,
  1053. .vidioc_qbuf = vb2_ioctl_qbuf,
  1054. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1055. .vidioc_expbuf = vb2_ioctl_expbuf,
  1056. .vidioc_streamon = vb2_ioctl_streamon,
  1057. .vidioc_streamoff = vb2_ioctl_streamoff,
  1058. .vidioc_s_tuner = hackrf_s_tuner,
  1059. .vidioc_g_tuner = hackrf_g_tuner,
  1060. .vidioc_s_modulator = hackrf_s_modulator,
  1061. .vidioc_g_modulator = hackrf_g_modulator,
  1062. .vidioc_s_frequency = hackrf_s_frequency,
  1063. .vidioc_g_frequency = hackrf_g_frequency,
  1064. .vidioc_enum_freq_bands = hackrf_enum_freq_bands,
  1065. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1066. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1067. .vidioc_log_status = v4l2_ctrl_log_status,
  1068. };
  1069. static const struct v4l2_file_operations hackrf_fops = {
  1070. .owner = THIS_MODULE,
  1071. .open = v4l2_fh_open,
  1072. .release = vb2_fop_release,
  1073. .read = vb2_fop_read,
  1074. .write = vb2_fop_write,
  1075. .poll = vb2_fop_poll,
  1076. .mmap = vb2_fop_mmap,
  1077. .unlocked_ioctl = video_ioctl2,
  1078. };
  1079. static struct video_device hackrf_template = {
  1080. .name = "HackRF One",
  1081. .release = video_device_release_empty,
  1082. .fops = &hackrf_fops,
  1083. .ioctl_ops = &hackrf_ioctl_ops,
  1084. };
  1085. static void hackrf_video_release(struct v4l2_device *v)
  1086. {
  1087. struct hackrf_dev *dev = container_of(v, struct hackrf_dev, v4l2_dev);
  1088. dev_dbg(dev->dev, "\n");
  1089. v4l2_ctrl_handler_free(&dev->rx_ctrl_handler);
  1090. v4l2_ctrl_handler_free(&dev->tx_ctrl_handler);
  1091. v4l2_device_unregister(&dev->v4l2_dev);
  1092. kfree(dev);
  1093. }
  1094. static int hackrf_s_ctrl_rx(struct v4l2_ctrl *ctrl)
  1095. {
  1096. struct hackrf_dev *dev = container_of(ctrl->handler,
  1097. struct hackrf_dev, rx_ctrl_handler);
  1098. struct usb_interface *intf = dev->intf;
  1099. int ret;
  1100. switch (ctrl->id) {
  1101. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  1102. case V4L2_CID_RF_TUNER_BANDWIDTH:
  1103. set_bit(RX_BANDWIDTH, &dev->flags);
  1104. break;
  1105. case V4L2_CID_RF_TUNER_RF_GAIN:
  1106. set_bit(RX_RF_GAIN, &dev->flags);
  1107. break;
  1108. case V4L2_CID_RF_TUNER_LNA_GAIN:
  1109. set_bit(RX_LNA_GAIN, &dev->flags);
  1110. break;
  1111. case V4L2_CID_RF_TUNER_IF_GAIN:
  1112. set_bit(RX_IF_GAIN, &dev->flags);
  1113. break;
  1114. default:
  1115. dev_dbg(&intf->dev, "unknown ctrl: id=%d name=%s\n",
  1116. ctrl->id, ctrl->name);
  1117. ret = -EINVAL;
  1118. goto err;
  1119. }
  1120. ret = hackrf_set_params(dev);
  1121. if (ret)
  1122. goto err;
  1123. return 0;
  1124. err:
  1125. dev_dbg(&intf->dev, "failed=%d\n", ret);
  1126. return ret;
  1127. }
  1128. static int hackrf_s_ctrl_tx(struct v4l2_ctrl *ctrl)
  1129. {
  1130. struct hackrf_dev *dev = container_of(ctrl->handler,
  1131. struct hackrf_dev, tx_ctrl_handler);
  1132. struct usb_interface *intf = dev->intf;
  1133. int ret;
  1134. switch (ctrl->id) {
  1135. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  1136. case V4L2_CID_RF_TUNER_BANDWIDTH:
  1137. set_bit(TX_BANDWIDTH, &dev->flags);
  1138. break;
  1139. case V4L2_CID_RF_TUNER_LNA_GAIN:
  1140. set_bit(TX_LNA_GAIN, &dev->flags);
  1141. break;
  1142. case V4L2_CID_RF_TUNER_RF_GAIN:
  1143. set_bit(TX_RF_GAIN, &dev->flags);
  1144. break;
  1145. default:
  1146. dev_dbg(&intf->dev, "unknown ctrl: id=%d name=%s\n",
  1147. ctrl->id, ctrl->name);
  1148. ret = -EINVAL;
  1149. goto err;
  1150. }
  1151. ret = hackrf_set_params(dev);
  1152. if (ret)
  1153. goto err;
  1154. return 0;
  1155. err:
  1156. dev_dbg(&intf->dev, "failed=%d\n", ret);
  1157. return ret;
  1158. }
  1159. static const struct v4l2_ctrl_ops hackrf_ctrl_ops_rx = {
  1160. .s_ctrl = hackrf_s_ctrl_rx,
  1161. };
  1162. static const struct v4l2_ctrl_ops hackrf_ctrl_ops_tx = {
  1163. .s_ctrl = hackrf_s_ctrl_tx,
  1164. };
  1165. static int hackrf_probe(struct usb_interface *intf,
  1166. const struct usb_device_id *id)
  1167. {
  1168. struct hackrf_dev *dev;
  1169. int ret;
  1170. u8 u8tmp, buf[BUF_SIZE];
  1171. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1172. if (!dev) {
  1173. ret = -ENOMEM;
  1174. goto err;
  1175. }
  1176. mutex_init(&dev->v4l2_lock);
  1177. mutex_init(&dev->vb_queue_lock);
  1178. spin_lock_init(&dev->buffer_list_lock);
  1179. INIT_LIST_HEAD(&dev->rx_buffer_list);
  1180. INIT_LIST_HEAD(&dev->tx_buffer_list);
  1181. dev->intf = intf;
  1182. dev->dev = &intf->dev;
  1183. dev->udev = interface_to_usbdev(intf);
  1184. dev->pixelformat = formats[0].pixelformat;
  1185. dev->buffersize = formats[0].buffersize;
  1186. dev->f_adc = bands_adc_dac[0].rangelow;
  1187. dev->f_dac = bands_adc_dac[0].rangelow;
  1188. dev->f_rx = bands_rx_tx[0].rangelow;
  1189. dev->f_tx = bands_rx_tx[0].rangelow;
  1190. set_bit(RX_ADC_FREQUENCY, &dev->flags);
  1191. set_bit(TX_DAC_FREQUENCY, &dev->flags);
  1192. set_bit(RX_RF_FREQUENCY, &dev->flags);
  1193. set_bit(TX_RF_FREQUENCY, &dev->flags);
  1194. /* Detect device */
  1195. ret = hackrf_ctrl_msg(dev, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1);
  1196. if (ret == 0)
  1197. ret = hackrf_ctrl_msg(dev, CMD_VERSION_STRING_READ, 0, 0,
  1198. buf, BUF_SIZE);
  1199. if (ret) {
  1200. dev_err(dev->dev, "Could not detect board\n");
  1201. goto err_kfree;
  1202. }
  1203. buf[BUF_SIZE - 1] = '\0';
  1204. dev_info(dev->dev, "Board ID: %02x\n", u8tmp);
  1205. dev_info(dev->dev, "Firmware version: %s\n", buf);
  1206. /* Init vb2 queue structure for receiver */
  1207. dev->rx_vb2_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
  1208. dev->rx_vb2_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF |
  1209. VB2_READ;
  1210. dev->rx_vb2_queue.ops = &hackrf_vb2_ops;
  1211. dev->rx_vb2_queue.mem_ops = &vb2_vmalloc_memops;
  1212. dev->rx_vb2_queue.drv_priv = dev;
  1213. dev->rx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
  1214. dev->rx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1215. ret = vb2_queue_init(&dev->rx_vb2_queue);
  1216. if (ret) {
  1217. dev_err(dev->dev, "Could not initialize rx vb2 queue\n");
  1218. goto err_kfree;
  1219. }
  1220. /* Init vb2 queue structure for transmitter */
  1221. dev->tx_vb2_queue.type = V4L2_BUF_TYPE_SDR_OUTPUT;
  1222. dev->tx_vb2_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF |
  1223. VB2_WRITE;
  1224. dev->tx_vb2_queue.ops = &hackrf_vb2_ops;
  1225. dev->tx_vb2_queue.mem_ops = &vb2_vmalloc_memops;
  1226. dev->tx_vb2_queue.drv_priv = dev;
  1227. dev->tx_vb2_queue.buf_struct_size = sizeof(struct hackrf_buffer);
  1228. dev->tx_vb2_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1229. ret = vb2_queue_init(&dev->tx_vb2_queue);
  1230. if (ret) {
  1231. dev_err(dev->dev, "Could not initialize tx vb2 queue\n");
  1232. goto err_kfree;
  1233. }
  1234. /* Register controls for receiver */
  1235. v4l2_ctrl_handler_init(&dev->rx_ctrl_handler, 5);
  1236. dev->rx_bandwidth_auto = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
  1237. &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
  1238. 0, 1, 0, 1);
  1239. dev->rx_bandwidth = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
  1240. &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_BANDWIDTH,
  1241. 1750000, 28000000, 50000, 1750000);
  1242. v4l2_ctrl_auto_cluster(2, &dev->rx_bandwidth_auto, 0, false);
  1243. dev->rx_rf_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
  1244. &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_RF_GAIN, 0, 12, 12, 0);
  1245. dev->rx_lna_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
  1246. &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_LNA_GAIN, 0, 40, 8, 0);
  1247. dev->rx_if_gain = v4l2_ctrl_new_std(&dev->rx_ctrl_handler,
  1248. &hackrf_ctrl_ops_rx, V4L2_CID_RF_TUNER_IF_GAIN, 0, 62, 2, 0);
  1249. if (dev->rx_ctrl_handler.error) {
  1250. ret = dev->rx_ctrl_handler.error;
  1251. dev_err(dev->dev, "Could not initialize controls\n");
  1252. goto err_v4l2_ctrl_handler_free_rx;
  1253. }
  1254. v4l2_ctrl_grab(dev->rx_rf_gain, !hackrf_enable_rf_gain_ctrl);
  1255. v4l2_ctrl_handler_setup(&dev->rx_ctrl_handler);
  1256. /* Register controls for transmitter */
  1257. v4l2_ctrl_handler_init(&dev->tx_ctrl_handler, 4);
  1258. dev->tx_bandwidth_auto = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
  1259. &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO,
  1260. 0, 1, 0, 1);
  1261. dev->tx_bandwidth = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
  1262. &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_BANDWIDTH,
  1263. 1750000, 28000000, 50000, 1750000);
  1264. v4l2_ctrl_auto_cluster(2, &dev->tx_bandwidth_auto, 0, false);
  1265. dev->tx_lna_gain = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
  1266. &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_LNA_GAIN, 0, 47, 1, 0);
  1267. dev->tx_rf_gain = v4l2_ctrl_new_std(&dev->tx_ctrl_handler,
  1268. &hackrf_ctrl_ops_tx, V4L2_CID_RF_TUNER_RF_GAIN, 0, 15, 15, 0);
  1269. if (dev->tx_ctrl_handler.error) {
  1270. ret = dev->tx_ctrl_handler.error;
  1271. dev_err(dev->dev, "Could not initialize controls\n");
  1272. goto err_v4l2_ctrl_handler_free_tx;
  1273. }
  1274. v4l2_ctrl_grab(dev->tx_rf_gain, !hackrf_enable_rf_gain_ctrl);
  1275. v4l2_ctrl_handler_setup(&dev->tx_ctrl_handler);
  1276. /* Register the v4l2_device structure */
  1277. dev->v4l2_dev.release = hackrf_video_release;
  1278. ret = v4l2_device_register(&intf->dev, &dev->v4l2_dev);
  1279. if (ret) {
  1280. dev_err(dev->dev, "Failed to register v4l2-device (%d)\n", ret);
  1281. goto err_v4l2_ctrl_handler_free_tx;
  1282. }
  1283. /* Init video_device structure for receiver */
  1284. dev->rx_vdev = hackrf_template;
  1285. dev->rx_vdev.queue = &dev->rx_vb2_queue;
  1286. dev->rx_vdev.queue->lock = &dev->vb_queue_lock;
  1287. dev->rx_vdev.v4l2_dev = &dev->v4l2_dev;
  1288. dev->rx_vdev.ctrl_handler = &dev->rx_ctrl_handler;
  1289. dev->rx_vdev.lock = &dev->v4l2_lock;
  1290. dev->rx_vdev.vfl_dir = VFL_DIR_RX;
  1291. video_set_drvdata(&dev->rx_vdev, dev);
  1292. ret = video_register_device(&dev->rx_vdev, VFL_TYPE_SDR, -1);
  1293. if (ret) {
  1294. dev_err(dev->dev,
  1295. "Failed to register as video device (%d)\n", ret);
  1296. goto err_v4l2_device_unregister;
  1297. }
  1298. dev_info(dev->dev, "Registered as %s\n",
  1299. video_device_node_name(&dev->rx_vdev));
  1300. /* Init video_device structure for transmitter */
  1301. dev->tx_vdev = hackrf_template;
  1302. dev->tx_vdev.queue = &dev->tx_vb2_queue;
  1303. dev->tx_vdev.queue->lock = &dev->vb_queue_lock;
  1304. dev->tx_vdev.v4l2_dev = &dev->v4l2_dev;
  1305. dev->tx_vdev.ctrl_handler = &dev->tx_ctrl_handler;
  1306. dev->tx_vdev.lock = &dev->v4l2_lock;
  1307. dev->tx_vdev.vfl_dir = VFL_DIR_TX;
  1308. video_set_drvdata(&dev->tx_vdev, dev);
  1309. ret = video_register_device(&dev->tx_vdev, VFL_TYPE_SDR, -1);
  1310. if (ret) {
  1311. dev_err(dev->dev,
  1312. "Failed to register as video device (%d)\n", ret);
  1313. goto err_video_unregister_device_rx;
  1314. }
  1315. dev_info(dev->dev, "Registered as %s\n",
  1316. video_device_node_name(&dev->tx_vdev));
  1317. dev_notice(dev->dev, "SDR API is still slightly experimental and functionality changes may follow\n");
  1318. return 0;
  1319. err_video_unregister_device_rx:
  1320. video_unregister_device(&dev->rx_vdev);
  1321. err_v4l2_device_unregister:
  1322. v4l2_device_unregister(&dev->v4l2_dev);
  1323. err_v4l2_ctrl_handler_free_tx:
  1324. v4l2_ctrl_handler_free(&dev->tx_ctrl_handler);
  1325. err_v4l2_ctrl_handler_free_rx:
  1326. v4l2_ctrl_handler_free(&dev->rx_ctrl_handler);
  1327. err_kfree:
  1328. kfree(dev);
  1329. err:
  1330. dev_dbg(&intf->dev, "failed=%d\n", ret);
  1331. return ret;
  1332. }
  1333. /* USB device ID list */
  1334. static struct usb_device_id hackrf_id_table[] = {
  1335. { USB_DEVICE(0x1d50, 0x6089) }, /* HackRF One */
  1336. { }
  1337. };
  1338. MODULE_DEVICE_TABLE(usb, hackrf_id_table);
  1339. /* USB subsystem interface */
  1340. static struct usb_driver hackrf_driver = {
  1341. .name = KBUILD_MODNAME,
  1342. .probe = hackrf_probe,
  1343. .disconnect = hackrf_disconnect,
  1344. .id_table = hackrf_id_table,
  1345. };
  1346. module_usb_driver(hackrf_driver);
  1347. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1348. MODULE_DESCRIPTION("HackRF");
  1349. MODULE_LICENSE("GPL");