Merge tag 'regmap-fix-v4.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / net / ieee802154 / atusb.c
blob1056ed142411d3c75082fced68c59f1b269d0532
1 /*
2 * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
4 * Written 2013 by Werner Almesberger <werner@almesberger.net>
6 * Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2
12 * Based on at86rf230.c and spi_atusb.c.
13 * at86rf230.c is
14 * Copyright (C) 2009 Siemens AG
15 * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
17 * spi_atusb.c is
18 * Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com>
19 * Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org>
20 * Copyright (c) 2011 Werner Almesberger <werner@almesberger.net>
22 * USB initialization is
23 * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/jiffies.h>
30 #include <linux/usb.h>
31 #include <linux/skbuff.h>
33 #include <net/cfg802154.h>
34 #include <net/mac802154.h>
36 #include "at86rf230.h"
37 #include "atusb.h"
39 #define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */
41 #define ATUSB_NUM_RX_URBS 4 /* allow for a bit of local latency */
42 #define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */
43 #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */
45 struct atusb {
46 struct ieee802154_hw *hw;
47 struct usb_device *usb_dev;
48 int shutdown; /* non-zero if shutting down */
49 int err; /* set by first error */
51 /* RX variables */
52 struct delayed_work work; /* memory allocations */
53 struct usb_anchor idle_urbs; /* URBs waiting to be submitted */
54 struct usb_anchor rx_urbs; /* URBs waiting for reception */
56 /* TX variables */
57 struct usb_ctrlrequest tx_dr;
58 struct urb *tx_urb;
59 struct sk_buff *tx_skb;
60 uint8_t tx_ack_seq; /* current TX ACK sequence number */
63 /* ----- USB commands without data ----------------------------------------- */
65 /* To reduce the number of error checks in the code, we record the first error
66 * in atusb->err and reject all subsequent requests until the error is cleared.
69 static int atusb_control_msg(struct atusb *atusb, unsigned int pipe,
70 __u8 request, __u8 requesttype,
71 __u16 value, __u16 index,
72 void *data, __u16 size, int timeout)
74 struct usb_device *usb_dev = atusb->usb_dev;
75 int ret;
77 if (atusb->err)
78 return atusb->err;
80 ret = usb_control_msg(usb_dev, pipe, request, requesttype,
81 value, index, data, size, timeout);
82 if (ret < 0) {
83 atusb->err = ret;
84 dev_err(&usb_dev->dev,
85 "atusb_control_msg: req 0x%02x val 0x%x idx 0x%x, error %d\n",
86 request, value, index, ret);
88 return ret;
91 static int atusb_command(struct atusb *atusb, uint8_t cmd, uint8_t arg)
93 struct usb_device *usb_dev = atusb->usb_dev;
95 dev_dbg(&usb_dev->dev, "atusb_command: cmd = 0x%x\n", cmd);
96 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
97 cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000);
100 static int atusb_write_reg(struct atusb *atusb, uint8_t reg, uint8_t value)
102 struct usb_device *usb_dev = atusb->usb_dev;
104 dev_dbg(&usb_dev->dev, "atusb_write_reg: 0x%02x <- 0x%02x\n",
105 reg, value);
106 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
107 ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
108 value, reg, NULL, 0, 1000);
111 static int atusb_read_reg(struct atusb *atusb, uint8_t reg)
113 struct usb_device *usb_dev = atusb->usb_dev;
114 int ret;
115 uint8_t value;
117 dev_dbg(&usb_dev->dev, "atusb: reg = 0x%x\n", reg);
118 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
119 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
120 0, reg, &value, 1, 1000);
121 return ret >= 0 ? value : ret;
124 static int atusb_write_subreg(struct atusb *atusb, uint8_t reg, uint8_t mask,
125 uint8_t shift, uint8_t value)
127 struct usb_device *usb_dev = atusb->usb_dev;
128 uint8_t orig, tmp;
129 int ret = 0;
131 dev_dbg(&usb_dev->dev, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
132 reg, value);
134 orig = atusb_read_reg(atusb, reg);
136 /* Write the value only into that part of the register which is allowed
137 * by the mask. All other bits stay as before.
139 tmp = orig & ~mask;
140 tmp |= (value << shift) & mask;
142 if (tmp != orig)
143 ret = atusb_write_reg(atusb, reg, tmp);
145 return ret;
148 static int atusb_get_and_clear_error(struct atusb *atusb)
150 int err = atusb->err;
152 atusb->err = 0;
153 return err;
156 /* ----- skb allocation ---------------------------------------------------- */
158 #define MAX_PSDU 127
159 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
161 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
163 static void atusb_in(struct urb *urb);
165 static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
167 struct usb_device *usb_dev = atusb->usb_dev;
168 struct sk_buff *skb = urb->context;
169 int ret;
171 if (!skb) {
172 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
173 if (!skb) {
174 dev_warn_ratelimited(&usb_dev->dev,
175 "atusb_in: can't allocate skb\n");
176 return -ENOMEM;
178 skb_put(skb, MAX_RX_XFER);
179 SKB_ATUSB(skb) = atusb;
182 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
183 skb->data, MAX_RX_XFER, atusb_in, skb);
184 usb_anchor_urb(urb, &atusb->rx_urbs);
186 ret = usb_submit_urb(urb, GFP_KERNEL);
187 if (ret) {
188 usb_unanchor_urb(urb);
189 kfree_skb(skb);
190 urb->context = NULL;
192 return ret;
195 static void atusb_work_urbs(struct work_struct *work)
197 struct atusb *atusb =
198 container_of(to_delayed_work(work), struct atusb, work);
199 struct usb_device *usb_dev = atusb->usb_dev;
200 struct urb *urb;
201 int ret;
203 if (atusb->shutdown)
204 return;
206 do {
207 urb = usb_get_from_anchor(&atusb->idle_urbs);
208 if (!urb)
209 return;
210 ret = atusb_submit_rx_urb(atusb, urb);
211 } while (!ret);
213 usb_anchor_urb(urb, &atusb->idle_urbs);
214 dev_warn_ratelimited(&usb_dev->dev,
215 "atusb_in: can't allocate/submit URB (%d)\n", ret);
216 schedule_delayed_work(&atusb->work,
217 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
220 /* ----- Asynchronous USB -------------------------------------------------- */
222 static void atusb_tx_done(struct atusb *atusb, uint8_t seq)
224 struct usb_device *usb_dev = atusb->usb_dev;
225 uint8_t expect = atusb->tx_ack_seq;
227 dev_dbg(&usb_dev->dev, "atusb_tx_done (0x%02x/0x%02x)\n", seq, expect);
228 if (seq == expect) {
229 /* TODO check for ifs handling in firmware */
230 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
231 } else {
232 /* TODO I experience this case when atusb has a tx complete
233 * irq before probing, we should fix the firmware it's an
234 * unlikely case now that seq == expect is then true, but can
235 * happen and fail with a tx_skb = NULL;
237 ieee802154_wake_queue(atusb->hw);
238 if (atusb->tx_skb)
239 dev_kfree_skb_irq(atusb->tx_skb);
243 static void atusb_in_good(struct urb *urb)
245 struct usb_device *usb_dev = urb->dev;
246 struct sk_buff *skb = urb->context;
247 struct atusb *atusb = SKB_ATUSB(skb);
248 uint8_t len, lqi;
250 if (!urb->actual_length) {
251 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
252 return;
255 len = *skb->data;
257 if (urb->actual_length == 1) {
258 atusb_tx_done(atusb, len);
259 return;
262 if (len + 1 > urb->actual_length - 1) {
263 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
264 len, urb->actual_length);
265 return;
268 if (!ieee802154_is_valid_psdu_len(len)) {
269 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
270 return;
273 lqi = skb->data[len + 1];
274 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
275 skb_pull(skb, 1); /* remove PHR */
276 skb_trim(skb, len); /* get payload only */
277 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
278 urb->context = NULL; /* skb is gone */
281 static void atusb_in(struct urb *urb)
283 struct usb_device *usb_dev = urb->dev;
284 struct sk_buff *skb = urb->context;
285 struct atusb *atusb = SKB_ATUSB(skb);
287 dev_dbg(&usb_dev->dev, "atusb_in: status %d len %d\n",
288 urb->status, urb->actual_length);
289 if (urb->status) {
290 if (urb->status == -ENOENT) { /* being killed */
291 kfree_skb(skb);
292 urb->context = NULL;
293 return;
295 dev_dbg(&usb_dev->dev, "atusb_in: URB error %d\n", urb->status);
296 } else {
297 atusb_in_good(urb);
300 usb_anchor_urb(urb, &atusb->idle_urbs);
301 if (!atusb->shutdown)
302 schedule_delayed_work(&atusb->work, 0);
305 /* ----- URB allocation/deallocation --------------------------------------- */
307 static void atusb_free_urbs(struct atusb *atusb)
309 struct urb *urb;
311 while (1) {
312 urb = usb_get_from_anchor(&atusb->idle_urbs);
313 if (!urb)
314 break;
315 kfree_skb(urb->context);
316 usb_free_urb(urb);
320 static int atusb_alloc_urbs(struct atusb *atusb, int n)
322 struct urb *urb;
324 while (n) {
325 urb = usb_alloc_urb(0, GFP_KERNEL);
326 if (!urb) {
327 atusb_free_urbs(atusb);
328 return -ENOMEM;
330 usb_anchor_urb(urb, &atusb->idle_urbs);
331 n--;
333 return 0;
336 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
338 static void atusb_xmit_complete(struct urb *urb)
340 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
343 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
345 struct atusb *atusb = hw->priv;
346 struct usb_device *usb_dev = atusb->usb_dev;
347 int ret;
349 dev_dbg(&usb_dev->dev, "atusb_xmit (%d)\n", skb->len);
350 atusb->tx_skb = skb;
351 atusb->tx_ack_seq++;
352 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
353 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
355 usb_fill_control_urb(atusb->tx_urb, usb_dev,
356 usb_sndctrlpipe(usb_dev, 0),
357 (unsigned char *)&atusb->tx_dr, skb->data,
358 skb->len, atusb_xmit_complete, NULL);
359 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
360 dev_dbg(&usb_dev->dev, "atusb_xmit done (%d)\n", ret);
361 return ret;
364 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
366 struct atusb *atusb = hw->priv;
367 int ret;
369 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
370 if (ret < 0)
371 return ret;
372 msleep(1); /* @@@ ugly synchronization */
373 return 0;
376 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
378 BUG_ON(!level);
379 *level = 0xbe;
380 return 0;
383 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
384 struct ieee802154_hw_addr_filt *filt,
385 unsigned long changed)
387 struct atusb *atusb = hw->priv;
388 struct device *dev = &atusb->usb_dev->dev;
390 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
391 u16 addr = le16_to_cpu(filt->short_addr);
393 dev_vdbg(dev, "atusb_set_hw_addr_filt called for saddr\n");
394 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
395 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
398 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
399 u16 pan = le16_to_cpu(filt->pan_id);
401 dev_vdbg(dev, "atusb_set_hw_addr_filt called for pan id\n");
402 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
403 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
406 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
407 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
409 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
410 dev_vdbg(dev, "atusb_set_hw_addr_filt called for IEEE addr\n");
411 for (i = 0; i < 8; i++)
412 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
415 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
416 dev_vdbg(dev,
417 "atusb_set_hw_addr_filt called for panc change\n");
418 if (filt->pan_coord)
419 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
420 else
421 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
424 return atusb_get_and_clear_error(atusb);
427 static int atusb_start(struct ieee802154_hw *hw)
429 struct atusb *atusb = hw->priv;
430 struct usb_device *usb_dev = atusb->usb_dev;
431 int ret;
433 dev_dbg(&usb_dev->dev, "atusb_start\n");
434 schedule_delayed_work(&atusb->work, 0);
435 atusb_command(atusb, ATUSB_RX_MODE, 1);
436 ret = atusb_get_and_clear_error(atusb);
437 if (ret < 0)
438 usb_kill_anchored_urbs(&atusb->idle_urbs);
439 return ret;
442 static void atusb_stop(struct ieee802154_hw *hw)
444 struct atusb *atusb = hw->priv;
445 struct usb_device *usb_dev = atusb->usb_dev;
447 dev_dbg(&usb_dev->dev, "atusb_stop\n");
448 usb_kill_anchored_urbs(&atusb->idle_urbs);
449 atusb_command(atusb, ATUSB_RX_MODE, 0);
450 atusb_get_and_clear_error(atusb);
453 #define ATUSB_MAX_TX_POWERS 0xF
454 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
455 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
456 -900, -1200, -1700,
459 static int
460 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
462 struct atusb *atusb = hw->priv;
463 u32 i;
465 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
466 if (hw->phy->supported.tx_powers[i] == mbm)
467 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
470 return -EINVAL;
473 #define ATUSB_MAX_ED_LEVELS 0xF
474 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
475 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
476 -7100, -6900, -6700, -6500, -6300, -6100,
479 static int
480 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
482 struct atusb *atusb = hw->priv;
483 u8 val;
485 /* mapping 802.15.4 to driver spec */
486 switch (cca->mode) {
487 case NL802154_CCA_ENERGY:
488 val = 1;
489 break;
490 case NL802154_CCA_CARRIER:
491 val = 2;
492 break;
493 case NL802154_CCA_ENERGY_CARRIER:
494 switch (cca->opt) {
495 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
496 val = 3;
497 break;
498 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
499 val = 0;
500 break;
501 default:
502 return -EINVAL;
504 break;
505 default:
506 return -EINVAL;
509 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
512 static int
513 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
515 struct atusb *atusb = hw->priv;
516 u32 i;
518 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
519 if (hw->phy->supported.cca_ed_levels[i] == mbm)
520 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
523 return -EINVAL;
526 static int
527 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
529 struct atusb *atusb = hw->priv;
530 int ret;
532 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
533 if (ret)
534 return ret;
536 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
537 if (ret)
538 return ret;
540 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
543 static int
544 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
546 struct atusb *atusb = hw->priv;
547 int ret;
549 if (on) {
550 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
551 if (ret < 0)
552 return ret;
554 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
555 if (ret < 0)
556 return ret;
557 } else {
558 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
559 if (ret < 0)
560 return ret;
562 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
563 if (ret < 0)
564 return ret;
567 return 0;
570 static struct ieee802154_ops atusb_ops = {
571 .owner = THIS_MODULE,
572 .xmit_async = atusb_xmit,
573 .ed = atusb_ed,
574 .set_channel = atusb_channel,
575 .start = atusb_start,
576 .stop = atusb_stop,
577 .set_hw_addr_filt = atusb_set_hw_addr_filt,
578 .set_txpower = atusb_set_txpower,
579 .set_cca_mode = atusb_set_cca_mode,
580 .set_cca_ed_level = atusb_set_cca_ed_level,
581 .set_csma_params = atusb_set_csma_params,
582 .set_promiscuous_mode = atusb_set_promiscuous_mode,
585 /* ----- Firmware and chip version information ----------------------------- */
587 static int atusb_get_and_show_revision(struct atusb *atusb)
589 struct usb_device *usb_dev = atusb->usb_dev;
590 unsigned char buffer[3];
591 int ret;
593 /* Get a couple of the ATMega Firmware values */
594 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
595 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
596 buffer, 3, 1000);
597 if (ret >= 0)
598 dev_info(&usb_dev->dev,
599 "Firmware: major: %u, minor: %u, hardware type: %u\n",
600 buffer[0], buffer[1], buffer[2]);
601 if (buffer[0] == 0 && buffer[1] < 2) {
602 dev_info(&usb_dev->dev,
603 "Firmware version (%u.%u) is predates our first public release.",
604 buffer[0], buffer[1]);
605 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
608 return ret;
611 static int atusb_get_and_show_build(struct atusb *atusb)
613 struct usb_device *usb_dev = atusb->usb_dev;
614 char build[ATUSB_BUILD_SIZE + 1];
615 int ret;
617 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
618 ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
619 build, ATUSB_BUILD_SIZE, 1000);
620 if (ret >= 0) {
621 build[ret] = 0;
622 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
625 return ret;
628 static int atusb_get_and_show_chip(struct atusb *atusb)
630 struct usb_device *usb_dev = atusb->usb_dev;
631 uint8_t man_id_0, man_id_1, part_num, version_num;
632 const char *chip;
634 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
635 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
636 part_num = atusb_read_reg(atusb, RG_PART_NUM);
637 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
639 if (atusb->err)
640 return atusb->err;
642 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
643 dev_err(&usb_dev->dev,
644 "non-Atmel transceiver xxxx%02x%02x\n",
645 man_id_1, man_id_0);
646 goto fail;
649 switch (part_num) {
650 case 2:
651 chip = "AT86RF230";
652 break;
653 case 3:
654 chip = "AT86RF231";
655 break;
656 default:
657 dev_err(&usb_dev->dev,
658 "unexpected transceiver, part 0x%02x version 0x%02x\n",
659 part_num, version_num);
660 goto fail;
663 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
665 return 0;
667 fail:
668 atusb->err = -ENODEV;
669 return -ENODEV;
672 /* ----- Setup ------------------------------------------------------------- */
674 static int atusb_probe(struct usb_interface *interface,
675 const struct usb_device_id *id)
677 struct usb_device *usb_dev = interface_to_usbdev(interface);
678 struct ieee802154_hw *hw;
679 struct atusb *atusb = NULL;
680 int ret = -ENOMEM;
682 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
683 if (!hw)
684 return -ENOMEM;
686 atusb = hw->priv;
687 atusb->hw = hw;
688 atusb->usb_dev = usb_get_dev(usb_dev);
689 usb_set_intfdata(interface, atusb);
691 atusb->shutdown = 0;
692 atusb->err = 0;
693 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
694 init_usb_anchor(&atusb->idle_urbs);
695 init_usb_anchor(&atusb->rx_urbs);
697 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
698 goto fail;
700 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
701 atusb->tx_dr.bRequest = ATUSB_TX;
702 atusb->tx_dr.wValue = cpu_to_le16(0);
704 atusb->tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
705 if (!atusb->tx_urb)
706 goto fail;
708 hw->parent = &usb_dev->dev;
709 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
710 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
712 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
713 WPAN_PHY_FLAG_CCA_MODE;
715 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
716 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
717 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
718 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
720 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
721 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
723 hw->phy->cca.mode = NL802154_CCA_ENERGY;
725 hw->phy->current_page = 0;
726 hw->phy->current_channel = 11; /* reset default */
727 hw->phy->supported.channels[0] = 0x7FFF800;
728 hw->phy->supported.tx_powers = atusb_powers;
729 hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
730 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
731 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
732 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
734 atusb_command(atusb, ATUSB_RF_RESET, 0);
735 atusb_get_and_show_chip(atusb);
736 atusb_get_and_show_revision(atusb);
737 atusb_get_and_show_build(atusb);
738 ret = atusb_get_and_clear_error(atusb);
739 if (ret) {
740 dev_err(&atusb->usb_dev->dev,
741 "%s: initialization failed, error = %d\n",
742 __func__, ret);
743 goto fail;
746 ret = ieee802154_register_hw(hw);
747 if (ret)
748 goto fail;
750 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
751 * explicitly. Any resets after that will send us straight to TRX_OFF,
752 * making the command below redundant.
754 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
755 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
757 #if 0
758 /* Calculating the maximum time available to empty the frame buffer
759 * on reception:
761 * According to [1], the inter-frame gap is
762 * R * 20 * 16 us + 128 us
763 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
764 * times (80 us at 250 kbps) of SHR of the next frame before the
765 * transceiver begins storing data in the frame buffer.
767 * This yields a minimum time of 208 us between the last data of a
768 * frame and the first data of the next frame. This time is further
769 * reduced by interrupt latency in the atusb firmware.
771 * atusb currently needs about 500 us to retrieve a maximum-sized
772 * frame. We therefore have to allow reception of a new frame to begin
773 * while we retrieve the previous frame.
775 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
776 * network", Jennic 2006.
777 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
780 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
781 #endif
782 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
784 ret = atusb_get_and_clear_error(atusb);
785 if (!ret)
786 return 0;
788 dev_err(&atusb->usb_dev->dev,
789 "%s: setup failed, error = %d\n",
790 __func__, ret);
792 ieee802154_unregister_hw(hw);
793 fail:
794 atusb_free_urbs(atusb);
795 usb_kill_urb(atusb->tx_urb);
796 usb_free_urb(atusb->tx_urb);
797 usb_put_dev(usb_dev);
798 ieee802154_free_hw(hw);
799 return ret;
802 static void atusb_disconnect(struct usb_interface *interface)
804 struct atusb *atusb = usb_get_intfdata(interface);
806 dev_dbg(&atusb->usb_dev->dev, "atusb_disconnect\n");
808 atusb->shutdown = 1;
809 cancel_delayed_work_sync(&atusb->work);
811 usb_kill_anchored_urbs(&atusb->rx_urbs);
812 atusb_free_urbs(atusb);
813 usb_kill_urb(atusb->tx_urb);
814 usb_free_urb(atusb->tx_urb);
816 ieee802154_unregister_hw(atusb->hw);
818 ieee802154_free_hw(atusb->hw);
820 usb_set_intfdata(interface, NULL);
821 usb_put_dev(atusb->usb_dev);
823 pr_debug("atusb_disconnect done\n");
826 /* The devices we work with */
827 static const struct usb_device_id atusb_device_table[] = {
829 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
830 USB_DEVICE_ID_MATCH_INT_INFO,
831 .idVendor = ATUSB_VENDOR_ID,
832 .idProduct = ATUSB_PRODUCT_ID,
833 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
835 /* end with null element */
838 MODULE_DEVICE_TABLE(usb, atusb_device_table);
840 static struct usb_driver atusb_driver = {
841 .name = "atusb",
842 .probe = atusb_probe,
843 .disconnect = atusb_disconnect,
844 .id_table = atusb_device_table,
846 module_usb_driver(atusb_driver);
848 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
849 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
850 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
851 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
852 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
853 MODULE_LICENSE("GPL");