atusb.c 30 KB

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