atusb.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
  4. *
  5. * Written 2013 by Werner Almesberger <[email protected]>
  6. *
  7. * Copyright (c) 2015 - 2016 Stefan Schmidt <[email protected]>
  8. *
  9. * Based on at86rf230.c and spi_atusb.c.
  10. * at86rf230.c is
  11. * Copyright (C) 2009 Siemens AG
  12. * Written by: Dmitry Eremin-Solenikov <[email protected]>
  13. *
  14. * spi_atusb.c is
  15. * Copyright (c) 2011 Richard Sharpe <[email protected]>
  16. * Copyright (c) 2011 Stefan Schmidt <[email protected]>
  17. * Copyright (c) 2011 Werner Almesberger <[email protected]>
  18. *
  19. * USB initialization is
  20. * Copyright (c) 2013 Alexander Aring <[email protected]>
  21. *
  22. * Busware HUL support is
  23. * Copyright (c) 2017 Josef Filzmaier <[email protected]>
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/usb.h>
  30. #include <linux/skbuff.h>
  31. #include <net/cfg802154.h>
  32. #include <net/mac802154.h>
  33. #include "at86rf230.h"
  34. #include "atusb.h"
  35. #define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */
  36. #define ATUSB_NUM_RX_URBS 4 /* allow for a bit of local latency */
  37. #define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */
  38. #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */
  39. struct atusb {
  40. struct ieee802154_hw *hw;
  41. struct usb_device *usb_dev;
  42. struct atusb_chip_data *data;
  43. int shutdown; /* non-zero if shutting down */
  44. int err; /* set by first error */
  45. /* RX variables */
  46. struct delayed_work work; /* memory allocations */
  47. struct usb_anchor idle_urbs; /* URBs waiting to be submitted */
  48. struct usb_anchor rx_urbs; /* URBs waiting for reception */
  49. /* TX variables */
  50. struct usb_ctrlrequest tx_dr;
  51. struct urb *tx_urb;
  52. struct sk_buff *tx_skb;
  53. u8 tx_ack_seq; /* current TX ACK sequence number */
  54. /* Firmware variable */
  55. unsigned char fw_ver_maj; /* Firmware major version number */
  56. unsigned char fw_ver_min; /* Firmware minor version number */
  57. unsigned char fw_hw_type; /* Firmware hardware type */
  58. };
  59. struct atusb_chip_data {
  60. u16 t_channel_switch;
  61. int rssi_base_val;
  62. int (*set_channel)(struct ieee802154_hw*, u8, u8);
  63. int (*set_txpower)(struct ieee802154_hw*, s32);
  64. };
  65. static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
  66. u8 shift, u8 value)
  67. {
  68. struct usb_device *usb_dev = atusb->usb_dev;
  69. u8 orig, tmp;
  70. int ret = 0;
  71. dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
  72. ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
  73. 0, reg, &orig, 1, 1000, GFP_KERNEL);
  74. if (ret < 0)
  75. return ret;
  76. /* Write the value only into that part of the register which is allowed
  77. * by the mask. All other bits stay as before.
  78. */
  79. tmp = orig & ~mask;
  80. tmp |= (value << shift) & mask;
  81. if (tmp != orig)
  82. ret = usb_control_msg_send(usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
  83. tmp, reg, NULL, 0, 1000, GFP_KERNEL);
  84. return ret;
  85. }
  86. static int atusb_read_subreg(struct atusb *lp,
  87. unsigned int addr, unsigned int mask,
  88. unsigned int shift)
  89. {
  90. int reg, ret;
  91. ret = usb_control_msg_recv(lp->usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
  92. 0, addr, &reg, 1, 1000, GFP_KERNEL);
  93. if (ret < 0)
  94. return ret;
  95. reg = (reg & mask) >> shift;
  96. return reg;
  97. }
  98. static int atusb_get_and_clear_error(struct atusb *atusb)
  99. {
  100. int err = atusb->err;
  101. atusb->err = 0;
  102. return err;
  103. }
  104. /* ----- skb allocation ---------------------------------------------------- */
  105. #define MAX_PSDU 127
  106. #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
  107. #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
  108. static void atusb_in(struct urb *urb);
  109. static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
  110. {
  111. struct usb_device *usb_dev = atusb->usb_dev;
  112. struct sk_buff *skb = urb->context;
  113. int ret;
  114. if (!skb) {
  115. skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
  116. if (!skb) {
  117. dev_warn_ratelimited(&usb_dev->dev,
  118. "atusb_in: can't allocate skb\n");
  119. return -ENOMEM;
  120. }
  121. skb_put(skb, MAX_RX_XFER);
  122. SKB_ATUSB(skb) = atusb;
  123. }
  124. usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
  125. skb->data, MAX_RX_XFER, atusb_in, skb);
  126. usb_anchor_urb(urb, &atusb->rx_urbs);
  127. ret = usb_submit_urb(urb, GFP_KERNEL);
  128. if (ret) {
  129. usb_unanchor_urb(urb);
  130. kfree_skb(skb);
  131. urb->context = NULL;
  132. }
  133. return ret;
  134. }
  135. static void atusb_work_urbs(struct work_struct *work)
  136. {
  137. struct atusb *atusb =
  138. container_of(to_delayed_work(work), struct atusb, work);
  139. struct usb_device *usb_dev = atusb->usb_dev;
  140. struct urb *urb;
  141. int ret;
  142. if (atusb->shutdown)
  143. return;
  144. do {
  145. urb = usb_get_from_anchor(&atusb->idle_urbs);
  146. if (!urb)
  147. return;
  148. ret = atusb_submit_rx_urb(atusb, urb);
  149. } while (!ret);
  150. usb_anchor_urb(urb, &atusb->idle_urbs);
  151. dev_warn_ratelimited(&usb_dev->dev,
  152. "atusb_in: can't allocate/submit URB (%d)\n", ret);
  153. schedule_delayed_work(&atusb->work,
  154. msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
  155. }
  156. /* ----- Asynchronous USB -------------------------------------------------- */
  157. static void atusb_tx_done(struct atusb *atusb, u8 seq)
  158. {
  159. struct usb_device *usb_dev = atusb->usb_dev;
  160. u8 expect = atusb->tx_ack_seq;
  161. dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
  162. if (seq == expect) {
  163. /* TODO check for ifs handling in firmware */
  164. ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
  165. } else {
  166. /* TODO I experience this case when atusb has a tx complete
  167. * irq before probing, we should fix the firmware it's an
  168. * unlikely case now that seq == expect is then true, but can
  169. * happen and fail with a tx_skb = NULL;
  170. */
  171. ieee802154_xmit_hw_error(atusb->hw, atusb->tx_skb);
  172. }
  173. }
  174. static void atusb_in_good(struct urb *urb)
  175. {
  176. struct usb_device *usb_dev = urb->dev;
  177. struct sk_buff *skb = urb->context;
  178. struct atusb *atusb = SKB_ATUSB(skb);
  179. u8 len, lqi;
  180. if (!urb->actual_length) {
  181. dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
  182. return;
  183. }
  184. len = *skb->data;
  185. if (urb->actual_length == 1) {
  186. atusb_tx_done(atusb, len);
  187. return;
  188. }
  189. if (len + 1 > urb->actual_length - 1) {
  190. dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
  191. len, urb->actual_length);
  192. return;
  193. }
  194. if (!ieee802154_is_valid_psdu_len(len)) {
  195. dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
  196. return;
  197. }
  198. lqi = skb->data[len + 1];
  199. dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
  200. skb_pull(skb, 1); /* remove PHR */
  201. skb_trim(skb, len); /* get payload only */
  202. ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
  203. urb->context = NULL; /* skb is gone */
  204. }
  205. static void atusb_in(struct urb *urb)
  206. {
  207. struct usb_device *usb_dev = urb->dev;
  208. struct sk_buff *skb = urb->context;
  209. struct atusb *atusb = SKB_ATUSB(skb);
  210. dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
  211. urb->status, urb->actual_length);
  212. if (urb->status) {
  213. if (urb->status == -ENOENT) { /* being killed */
  214. kfree_skb(skb);
  215. urb->context = NULL;
  216. return;
  217. }
  218. dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
  219. } else {
  220. atusb_in_good(urb);
  221. }
  222. usb_anchor_urb(urb, &atusb->idle_urbs);
  223. if (!atusb->shutdown)
  224. schedule_delayed_work(&atusb->work, 0);
  225. }
  226. /* ----- URB allocation/deallocation --------------------------------------- */
  227. static void atusb_free_urbs(struct atusb *atusb)
  228. {
  229. struct urb *urb;
  230. while (1) {
  231. urb = usb_get_from_anchor(&atusb->idle_urbs);
  232. if (!urb)
  233. break;
  234. kfree_skb(urb->context);
  235. usb_free_urb(urb);
  236. }
  237. }
  238. static int atusb_alloc_urbs(struct atusb *atusb, int n)
  239. {
  240. struct urb *urb;
  241. while (n) {
  242. urb = usb_alloc_urb(0, GFP_KERNEL);
  243. if (!urb) {
  244. atusb_free_urbs(atusb);
  245. return -ENOMEM;
  246. }
  247. usb_anchor_urb(urb, &atusb->idle_urbs);
  248. usb_free_urb(urb);
  249. n--;
  250. }
  251. return 0;
  252. }
  253. /* ----- IEEE 802.15.4 interface operations -------------------------------- */
  254. static void atusb_xmit_complete(struct urb *urb)
  255. {
  256. dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
  257. }
  258. static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
  259. {
  260. struct atusb *atusb = hw->priv;
  261. struct usb_device *usb_dev = atusb->usb_dev;
  262. int ret;
  263. dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
  264. atusb->tx_skb = skb;
  265. atusb->tx_ack_seq++;
  266. atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
  267. atusb->tx_dr.wLength = cpu_to_le16(skb->len);
  268. usb_fill_control_urb(atusb->tx_urb, usb_dev,
  269. usb_sndctrlpipe(usb_dev, 0),
  270. (unsigned char *)&atusb->tx_dr, skb->data,
  271. skb->len, atusb_xmit_complete, NULL);
  272. ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
  273. dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
  274. return ret;
  275. }
  276. static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
  277. {
  278. WARN_ON(!level);
  279. *level = 0xbe;
  280. return 0;
  281. }
  282. static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
  283. struct ieee802154_hw_addr_filt *filt,
  284. unsigned long changed)
  285. {
  286. struct atusb *atusb = hw->priv;
  287. struct device *dev = &atusb->usb_dev->dev;
  288. if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
  289. u16 addr = le16_to_cpu(filt->short_addr);
  290. dev_vdbg(dev, "%s called for saddr\n", __func__);
  291. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
  292. addr, RG_SHORT_ADDR_0, NULL, 0, 1000, GFP_KERNEL);
  293. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
  294. addr >> 8, RG_SHORT_ADDR_1, NULL, 0, 1000, GFP_KERNEL);
  295. }
  296. if (changed & IEEE802154_AFILT_PANID_CHANGED) {
  297. u16 pan = le16_to_cpu(filt->pan_id);
  298. dev_vdbg(dev, "%s called for pan id\n", __func__);
  299. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
  300. pan, RG_PAN_ID_0, NULL, 0, 1000, GFP_KERNEL);
  301. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
  302. pan >> 8, RG_PAN_ID_1, NULL, 0, 1000, GFP_KERNEL);
  303. }
  304. if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
  305. u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
  306. memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
  307. dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
  308. for (i = 0; i < 8; i++)
  309. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
  310. addr[i], RG_IEEE_ADDR_0 + i, NULL, 0,
  311. 1000, GFP_KERNEL);
  312. }
  313. if (changed & IEEE802154_AFILT_PANC_CHANGED) {
  314. dev_vdbg(dev, "%s called for panc change\n", __func__);
  315. if (filt->pan_coord)
  316. atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
  317. else
  318. atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
  319. }
  320. return atusb_get_and_clear_error(atusb);
  321. }
  322. static int atusb_start(struct ieee802154_hw *hw)
  323. {
  324. struct atusb *atusb = hw->priv;
  325. struct usb_device *usb_dev = atusb->usb_dev;
  326. int ret;
  327. dev_dbg(&usb_dev->dev, "%s\n", __func__);
  328. schedule_delayed_work(&atusb->work, 0);
  329. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 1, 0,
  330. NULL, 0, 1000, GFP_KERNEL);
  331. ret = atusb_get_and_clear_error(atusb);
  332. if (ret < 0)
  333. usb_kill_anchored_urbs(&atusb->idle_urbs);
  334. return ret;
  335. }
  336. static void atusb_stop(struct ieee802154_hw *hw)
  337. {
  338. struct atusb *atusb = hw->priv;
  339. struct usb_device *usb_dev = atusb->usb_dev;
  340. dev_dbg(&usb_dev->dev, "%s\n", __func__);
  341. usb_kill_anchored_urbs(&atusb->idle_urbs);
  342. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 0, 0,
  343. NULL, 0, 1000, GFP_KERNEL);
  344. atusb_get_and_clear_error(atusb);
  345. }
  346. #define ATUSB_MAX_TX_POWERS 0xF
  347. static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
  348. 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
  349. -900, -1200, -1700,
  350. };
  351. static int
  352. atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
  353. {
  354. struct atusb *atusb = hw->priv;
  355. if (atusb->data)
  356. return atusb->data->set_txpower(hw, mbm);
  357. else
  358. return -ENOTSUPP;
  359. }
  360. static int
  361. atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
  362. {
  363. struct atusb *atusb = hw->priv;
  364. u32 i;
  365. for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
  366. if (hw->phy->supported.tx_powers[i] == mbm)
  367. return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
  368. }
  369. return -EINVAL;
  370. }
  371. static int
  372. hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
  373. {
  374. u32 i;
  375. for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
  376. if (hw->phy->supported.tx_powers[i] == mbm)
  377. return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
  378. }
  379. return -EINVAL;
  380. }
  381. #define ATUSB_MAX_ED_LEVELS 0xF
  382. static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
  383. -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
  384. -7100, -6900, -6700, -6500, -6300, -6100,
  385. };
  386. #define AT86RF212_MAX_TX_POWERS 0x1F
  387. static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
  388. 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
  389. -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
  390. -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
  391. };
  392. #define AT86RF2XX_MAX_ED_LEVELS 0xF
  393. static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
  394. -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
  395. -8000, -7800, -7600, -7400, -7200, -7000,
  396. };
  397. static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
  398. -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
  399. -7800, -7600, -7400, -7200, -7000, -6800,
  400. };
  401. static int
  402. atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
  403. {
  404. struct atusb *atusb = hw->priv;
  405. u8 val;
  406. /* mapping 802.15.4 to driver spec */
  407. switch (cca->mode) {
  408. case NL802154_CCA_ENERGY:
  409. val = 1;
  410. break;
  411. case NL802154_CCA_CARRIER:
  412. val = 2;
  413. break;
  414. case NL802154_CCA_ENERGY_CARRIER:
  415. switch (cca->opt) {
  416. case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
  417. val = 3;
  418. break;
  419. case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
  420. val = 0;
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. break;
  426. default:
  427. return -EINVAL;
  428. }
  429. return atusb_write_subreg(atusb, SR_CCA_MODE, val);
  430. }
  431. static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
  432. {
  433. int cca_ed_thres;
  434. cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
  435. if (cca_ed_thres < 0)
  436. return cca_ed_thres;
  437. switch (rssi_base_val) {
  438. case -98:
  439. lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
  440. lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
  441. lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
  442. break;
  443. case -100:
  444. lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
  445. lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
  446. lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
  447. break;
  448. default:
  449. WARN_ON(1);
  450. }
  451. return 0;
  452. }
  453. static int
  454. atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
  455. {
  456. struct atusb *atusb = hw->priv;
  457. u32 i;
  458. for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
  459. if (hw->phy->supported.cca_ed_levels[i] == mbm)
  460. return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
  461. }
  462. return -EINVAL;
  463. }
  464. static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  465. {
  466. struct atusb *atusb = hw->priv;
  467. int ret = -ENOTSUPP;
  468. if (atusb->data) {
  469. ret = atusb->data->set_channel(hw, page, channel);
  470. /* @@@ ugly synchronization */
  471. msleep(atusb->data->t_channel_switch);
  472. }
  473. return ret;
  474. }
  475. static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  476. {
  477. struct atusb *atusb = hw->priv;
  478. int ret;
  479. ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
  480. if (ret < 0)
  481. return ret;
  482. return 0;
  483. }
  484. static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  485. {
  486. int rc;
  487. int rssi_base_val;
  488. struct atusb *lp = hw->priv;
  489. if (channel == 0)
  490. rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
  491. else
  492. rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
  493. if (rc < 0)
  494. return rc;
  495. if (page == 0) {
  496. rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
  497. rssi_base_val = -100;
  498. } else {
  499. rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
  500. rssi_base_val = -98;
  501. }
  502. if (rc < 0)
  503. return rc;
  504. rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
  505. if (rc < 0)
  506. return rc;
  507. return atusb_write_subreg(lp, SR_CHANNEL, channel);
  508. }
  509. static int
  510. atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
  511. {
  512. struct atusb *atusb = hw->priv;
  513. int ret;
  514. ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
  515. if (ret)
  516. return ret;
  517. ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
  518. if (ret)
  519. return ret;
  520. return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
  521. }
  522. static int
  523. hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
  524. {
  525. struct atusb *atusb = hw->priv;
  526. return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
  527. }
  528. static int
  529. atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
  530. {
  531. struct atusb *atusb = hw->priv;
  532. return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
  533. }
  534. static int
  535. atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
  536. {
  537. struct atusb *atusb = hw->priv;
  538. int ret;
  539. if (on) {
  540. ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
  541. if (ret < 0)
  542. return ret;
  543. ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
  544. if (ret < 0)
  545. return ret;
  546. } else {
  547. ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
  548. if (ret < 0)
  549. return ret;
  550. ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
  551. if (ret < 0)
  552. return ret;
  553. }
  554. return 0;
  555. }
  556. static struct atusb_chip_data atusb_chip_data = {
  557. .t_channel_switch = 1,
  558. .rssi_base_val = -91,
  559. .set_txpower = atusb_set_txpower,
  560. .set_channel = atusb_set_channel,
  561. };
  562. static struct atusb_chip_data hulusb_chip_data = {
  563. .t_channel_switch = 11,
  564. .rssi_base_val = -100,
  565. .set_txpower = hulusb_set_txpower,
  566. .set_channel = hulusb_set_channel,
  567. };
  568. static const struct ieee802154_ops atusb_ops = {
  569. .owner = THIS_MODULE,
  570. .xmit_async = atusb_xmit,
  571. .ed = atusb_ed,
  572. .set_channel = atusb_channel,
  573. .start = atusb_start,
  574. .stop = atusb_stop,
  575. .set_hw_addr_filt = atusb_set_hw_addr_filt,
  576. .set_txpower = atusb_txpower,
  577. .set_lbt = hulusb_set_lbt,
  578. .set_cca_mode = atusb_set_cca_mode,
  579. .set_cca_ed_level = atusb_set_cca_ed_level,
  580. .set_csma_params = atusb_set_csma_params,
  581. .set_frame_retries = atusb_set_frame_retries,
  582. .set_promiscuous_mode = atusb_set_promiscuous_mode,
  583. };
  584. /* ----- Firmware and chip version information ----------------------------- */
  585. static int atusb_get_and_show_revision(struct atusb *atusb)
  586. {
  587. struct usb_device *usb_dev = atusb->usb_dev;
  588. char *hw_name;
  589. unsigned char buffer[3];
  590. int ret;
  591. /* Get a couple of the ATMega Firmware values */
  592. ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
  593. buffer, 3, 1000, GFP_KERNEL);
  594. if (!ret) {
  595. atusb->fw_ver_maj = buffer[0];
  596. atusb->fw_ver_min = buffer[1];
  597. atusb->fw_hw_type = buffer[2];
  598. switch (atusb->fw_hw_type) {
  599. case ATUSB_HW_TYPE_100813:
  600. case ATUSB_HW_TYPE_101216:
  601. case ATUSB_HW_TYPE_110131:
  602. hw_name = "ATUSB";
  603. atusb->data = &atusb_chip_data;
  604. break;
  605. case ATUSB_HW_TYPE_RZUSB:
  606. hw_name = "RZUSB";
  607. atusb->data = &atusb_chip_data;
  608. break;
  609. case ATUSB_HW_TYPE_HULUSB:
  610. hw_name = "HULUSB";
  611. atusb->data = &hulusb_chip_data;
  612. break;
  613. default:
  614. hw_name = "UNKNOWN";
  615. atusb->err = -ENOTSUPP;
  616. ret = -ENOTSUPP;
  617. break;
  618. }
  619. dev_info(&usb_dev->dev,
  620. "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
  621. atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
  622. atusb->fw_hw_type);
  623. }
  624. if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
  625. dev_info(&usb_dev->dev,
  626. "Firmware version (%u.%u) predates our first public release.",
  627. atusb->fw_ver_maj, atusb->fw_ver_min);
  628. dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
  629. }
  630. return ret;
  631. }
  632. static int atusb_get_and_show_build(struct atusb *atusb)
  633. {
  634. struct usb_device *usb_dev = atusb->usb_dev;
  635. char *build;
  636. int ret;
  637. build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
  638. if (!build)
  639. return -ENOMEM;
  640. ret = usb_control_msg(atusb->usb_dev, usb_rcvctrlpipe(usb_dev, 0), ATUSB_BUILD,
  641. ATUSB_REQ_FROM_DEV, 0, 0, build, ATUSB_BUILD_SIZE, 1000);
  642. if (ret >= 0) {
  643. build[ret] = 0;
  644. dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
  645. }
  646. kfree(build);
  647. return ret;
  648. }
  649. static int atusb_get_and_conf_chip(struct atusb *atusb)
  650. {
  651. struct usb_device *usb_dev = atusb->usb_dev;
  652. u8 man_id_0, man_id_1, part_num, version_num;
  653. const char *chip;
  654. struct ieee802154_hw *hw = atusb->hw;
  655. int ret;
  656. ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
  657. 0, RG_MAN_ID_0, &man_id_0, 1, 1000, GFP_KERNEL);
  658. if (ret < 0)
  659. return ret;
  660. ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
  661. 0, RG_MAN_ID_1, &man_id_1, 1, 1000, GFP_KERNEL);
  662. if (ret < 0)
  663. return ret;
  664. ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
  665. 0, RG_PART_NUM, &part_num, 1, 1000, GFP_KERNEL);
  666. if (ret < 0)
  667. return ret;
  668. ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
  669. 0, RG_VERSION_NUM, &version_num, 1, 1000, GFP_KERNEL);
  670. if (ret < 0)
  671. return ret;
  672. hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
  673. IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
  674. hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
  675. WPAN_PHY_FLAG_CCA_MODE;
  676. hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
  677. BIT(NL802154_CCA_CARRIER) |
  678. BIT(NL802154_CCA_ENERGY_CARRIER);
  679. hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
  680. BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
  681. hw->phy->cca.mode = NL802154_CCA_ENERGY;
  682. hw->phy->current_page = 0;
  683. if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
  684. dev_err(&usb_dev->dev,
  685. "non-Atmel transceiver xxxx%02x%02x\n",
  686. man_id_1, man_id_0);
  687. goto fail;
  688. }
  689. switch (part_num) {
  690. case 2:
  691. chip = "AT86RF230";
  692. atusb->hw->phy->supported.channels[0] = 0x7FFF800;
  693. atusb->hw->phy->current_channel = 11; /* reset default */
  694. atusb->hw->phy->supported.tx_powers = atusb_powers;
  695. atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
  696. hw->phy->supported.cca_ed_levels = atusb_ed_levels;
  697. hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
  698. break;
  699. case 3:
  700. chip = "AT86RF231";
  701. atusb->hw->phy->supported.channels[0] = 0x7FFF800;
  702. atusb->hw->phy->current_channel = 11; /* reset default */
  703. atusb->hw->phy->supported.tx_powers = atusb_powers;
  704. atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
  705. hw->phy->supported.cca_ed_levels = atusb_ed_levels;
  706. hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
  707. break;
  708. case 7:
  709. chip = "AT86RF212";
  710. atusb->hw->flags |= IEEE802154_HW_LBT;
  711. atusb->hw->phy->supported.channels[0] = 0x00007FF;
  712. atusb->hw->phy->supported.channels[2] = 0x00007FF;
  713. atusb->hw->phy->current_channel = 5;
  714. atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
  715. atusb->hw->phy->supported.tx_powers = at86rf212_powers;
  716. atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
  717. atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
  718. atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
  719. break;
  720. default:
  721. dev_err(&usb_dev->dev,
  722. "unexpected transceiver, part 0x%02x version 0x%02x\n",
  723. part_num, version_num);
  724. goto fail;
  725. }
  726. hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
  727. hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
  728. dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
  729. return 0;
  730. fail:
  731. atusb->err = -ENODEV;
  732. return -ENODEV;
  733. }
  734. static int atusb_set_extended_addr(struct atusb *atusb)
  735. {
  736. struct usb_device *usb_dev = atusb->usb_dev;
  737. unsigned char buffer[IEEE802154_EXTENDED_ADDR_LEN];
  738. __le64 extended_addr;
  739. u64 addr;
  740. int ret;
  741. /* Firmware versions before 0.3 do not support the EUI64_READ command.
  742. * Just use a random address and be done.
  743. */
  744. if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
  745. ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
  746. return 0;
  747. }
  748. /* Firmware is new enough so we fetch the address from EEPROM */
  749. ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
  750. buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000, GFP_KERNEL);
  751. if (ret < 0) {
  752. dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
  753. ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
  754. return ret;
  755. }
  756. memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
  757. /* Check if read address is not empty and the unicast bit is set correctly */
  758. if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
  759. dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
  760. ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
  761. } else {
  762. atusb->hw->phy->perm_extended_addr = extended_addr;
  763. addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
  764. dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
  765. &addr);
  766. }
  767. return ret;
  768. }
  769. /* ----- Setup ------------------------------------------------------------- */
  770. static int atusb_probe(struct usb_interface *interface,
  771. const struct usb_device_id *id)
  772. {
  773. struct usb_device *usb_dev = interface_to_usbdev(interface);
  774. struct ieee802154_hw *hw;
  775. struct atusb *atusb = NULL;
  776. int ret = -ENOMEM;
  777. hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
  778. if (!hw)
  779. return -ENOMEM;
  780. atusb = hw->priv;
  781. atusb->hw = hw;
  782. atusb->usb_dev = usb_get_dev(usb_dev);
  783. usb_set_intfdata(interface, atusb);
  784. atusb->shutdown = 0;
  785. atusb->err = 0;
  786. INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
  787. init_usb_anchor(&atusb->idle_urbs);
  788. init_usb_anchor(&atusb->rx_urbs);
  789. if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
  790. goto fail;
  791. atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
  792. atusb->tx_dr.bRequest = ATUSB_TX;
  793. atusb->tx_dr.wValue = cpu_to_le16(0);
  794. atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  795. if (!atusb->tx_urb)
  796. goto fail;
  797. hw->parent = &usb_dev->dev;
  798. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RF_RESET, ATUSB_REQ_TO_DEV, 0, 0,
  799. NULL, 0, 1000, GFP_KERNEL);
  800. atusb_get_and_conf_chip(atusb);
  801. atusb_get_and_show_revision(atusb);
  802. atusb_get_and_show_build(atusb);
  803. atusb_set_extended_addr(atusb);
  804. if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
  805. hw->flags |= IEEE802154_HW_FRAME_RETRIES;
  806. ret = atusb_get_and_clear_error(atusb);
  807. if (ret) {
  808. dev_err(&atusb->usb_dev->dev,
  809. "%s: initialization failed, error = %d\n",
  810. __func__, ret);
  811. goto fail;
  812. }
  813. ret = ieee802154_register_hw(hw);
  814. if (ret)
  815. goto fail;
  816. /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
  817. * explicitly. Any resets after that will send us straight to TRX_OFF,
  818. * making the command below redundant.
  819. */
  820. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
  821. STATE_FORCE_TRX_OFF, RG_TRX_STATE, NULL, 0, 1000, GFP_KERNEL);
  822. msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
  823. #if 0
  824. /* Calculating the maximum time available to empty the frame buffer
  825. * on reception:
  826. *
  827. * According to [1], the inter-frame gap is
  828. * R * 20 * 16 us + 128 us
  829. * where R is a random number from 0 to 7. Furthermore, we have 20 bit
  830. * times (80 us at 250 kbps) of SHR of the next frame before the
  831. * transceiver begins storing data in the frame buffer.
  832. *
  833. * This yields a minimum time of 208 us between the last data of a
  834. * frame and the first data of the next frame. This time is further
  835. * reduced by interrupt latency in the atusb firmware.
  836. *
  837. * atusb currently needs about 500 us to retrieve a maximum-sized
  838. * frame. We therefore have to allow reception of a new frame to begin
  839. * while we retrieve the previous frame.
  840. *
  841. * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
  842. * network", Jennic 2006.
  843. * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
  844. */
  845. atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
  846. #endif
  847. usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
  848. 0xff, RG_IRQ_MASK, NULL, 0, 1000, GFP_KERNEL);
  849. ret = atusb_get_and_clear_error(atusb);
  850. if (!ret)
  851. return 0;
  852. dev_err(&atusb->usb_dev->dev,
  853. "%s: setup failed, error = %d\n",
  854. __func__, ret);
  855. ieee802154_unregister_hw(hw);
  856. fail:
  857. atusb_free_urbs(atusb);
  858. usb_kill_urb(atusb->tx_urb);
  859. usb_free_urb(atusb->tx_urb);
  860. usb_put_dev(usb_dev);
  861. ieee802154_free_hw(hw);
  862. return ret;
  863. }
  864. static void atusb_disconnect(struct usb_interface *interface)
  865. {
  866. struct atusb *atusb = usb_get_intfdata(interface);
  867. dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
  868. atusb->shutdown = 1;
  869. cancel_delayed_work_sync(&atusb->work);
  870. usb_kill_anchored_urbs(&atusb->rx_urbs);
  871. atusb_free_urbs(atusb);
  872. usb_kill_urb(atusb->tx_urb);
  873. usb_free_urb(atusb->tx_urb);
  874. ieee802154_unregister_hw(atusb->hw);
  875. usb_put_dev(atusb->usb_dev);
  876. ieee802154_free_hw(atusb->hw);
  877. usb_set_intfdata(interface, NULL);
  878. pr_debug("%s done\n", __func__);
  879. }
  880. /* The devices we work with */
  881. static const struct usb_device_id atusb_device_table[] = {
  882. {
  883. .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
  884. USB_DEVICE_ID_MATCH_INT_INFO,
  885. .idVendor = ATUSB_VENDOR_ID,
  886. .idProduct = ATUSB_PRODUCT_ID,
  887. .bInterfaceClass = USB_CLASS_VENDOR_SPEC
  888. },
  889. /* end with null element */
  890. {}
  891. };
  892. MODULE_DEVICE_TABLE(usb, atusb_device_table);
  893. static struct usb_driver atusb_driver = {
  894. .name = "atusb",
  895. .probe = atusb_probe,
  896. .disconnect = atusb_disconnect,
  897. .id_table = atusb_device_table,
  898. };
  899. module_usb_driver(atusb_driver);
  900. MODULE_AUTHOR("Alexander Aring <[email protected]>");
  901. MODULE_AUTHOR("Richard Sharpe <[email protected]>");
  902. MODULE_AUTHOR("Stefan Schmidt <[email protected]>");
  903. MODULE_AUTHOR("Werner Almesberger <[email protected]>");
  904. MODULE_AUTHOR("Josef Filzmaier <[email protected]>");
  905. MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
  906. MODULE_LICENSE("GPL");