Linux 4.9.89
[linux/fpc-iii.git] / drivers / net / ieee802154 / atusb.c
blobf186e0460cde311092f2ae8298e83060317bef5f
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 *buffer;
116 uint8_t value;
118 buffer = kmalloc(1, GFP_KERNEL);
119 if (!buffer)
120 return -ENOMEM;
122 dev_dbg(&usb_dev->dev, "atusb: reg = 0x%x\n", reg);
123 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
124 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
125 0, reg, buffer, 1, 1000);
127 if (ret >= 0) {
128 value = buffer[0];
129 kfree(buffer);
130 return value;
131 } else {
132 kfree(buffer);
133 return ret;
137 static int atusb_write_subreg(struct atusb *atusb, uint8_t reg, uint8_t mask,
138 uint8_t shift, uint8_t value)
140 struct usb_device *usb_dev = atusb->usb_dev;
141 uint8_t orig, tmp;
142 int ret = 0;
144 dev_dbg(&usb_dev->dev, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
145 reg, value);
147 orig = atusb_read_reg(atusb, reg);
149 /* Write the value only into that part of the register which is allowed
150 * by the mask. All other bits stay as before.
152 tmp = orig & ~mask;
153 tmp |= (value << shift) & mask;
155 if (tmp != orig)
156 ret = atusb_write_reg(atusb, reg, tmp);
158 return ret;
161 static int atusb_get_and_clear_error(struct atusb *atusb)
163 int err = atusb->err;
165 atusb->err = 0;
166 return err;
169 /* ----- skb allocation ---------------------------------------------------- */
171 #define MAX_PSDU 127
172 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
174 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
176 static void atusb_in(struct urb *urb);
178 static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
180 struct usb_device *usb_dev = atusb->usb_dev;
181 struct sk_buff *skb = urb->context;
182 int ret;
184 if (!skb) {
185 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
186 if (!skb) {
187 dev_warn_ratelimited(&usb_dev->dev,
188 "atusb_in: can't allocate skb\n");
189 return -ENOMEM;
191 skb_put(skb, MAX_RX_XFER);
192 SKB_ATUSB(skb) = atusb;
195 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
196 skb->data, MAX_RX_XFER, atusb_in, skb);
197 usb_anchor_urb(urb, &atusb->rx_urbs);
199 ret = usb_submit_urb(urb, GFP_KERNEL);
200 if (ret) {
201 usb_unanchor_urb(urb);
202 kfree_skb(skb);
203 urb->context = NULL;
205 return ret;
208 static void atusb_work_urbs(struct work_struct *work)
210 struct atusb *atusb =
211 container_of(to_delayed_work(work), struct atusb, work);
212 struct usb_device *usb_dev = atusb->usb_dev;
213 struct urb *urb;
214 int ret;
216 if (atusb->shutdown)
217 return;
219 do {
220 urb = usb_get_from_anchor(&atusb->idle_urbs);
221 if (!urb)
222 return;
223 ret = atusb_submit_rx_urb(atusb, urb);
224 } while (!ret);
226 usb_anchor_urb(urb, &atusb->idle_urbs);
227 dev_warn_ratelimited(&usb_dev->dev,
228 "atusb_in: can't allocate/submit URB (%d)\n", ret);
229 schedule_delayed_work(&atusb->work,
230 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
233 /* ----- Asynchronous USB -------------------------------------------------- */
235 static void atusb_tx_done(struct atusb *atusb, uint8_t seq)
237 struct usb_device *usb_dev = atusb->usb_dev;
238 uint8_t expect = atusb->tx_ack_seq;
240 dev_dbg(&usb_dev->dev, "atusb_tx_done (0x%02x/0x%02x)\n", seq, expect);
241 if (seq == expect) {
242 /* TODO check for ifs handling in firmware */
243 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
244 } else {
245 /* TODO I experience this case when atusb has a tx complete
246 * irq before probing, we should fix the firmware it's an
247 * unlikely case now that seq == expect is then true, but can
248 * happen and fail with a tx_skb = NULL;
250 ieee802154_wake_queue(atusb->hw);
251 if (atusb->tx_skb)
252 dev_kfree_skb_irq(atusb->tx_skb);
256 static void atusb_in_good(struct urb *urb)
258 struct usb_device *usb_dev = urb->dev;
259 struct sk_buff *skb = urb->context;
260 struct atusb *atusb = SKB_ATUSB(skb);
261 uint8_t len, lqi;
263 if (!urb->actual_length) {
264 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
265 return;
268 len = *skb->data;
270 if (urb->actual_length == 1) {
271 atusb_tx_done(atusb, len);
272 return;
275 if (len + 1 > urb->actual_length - 1) {
276 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
277 len, urb->actual_length);
278 return;
281 if (!ieee802154_is_valid_psdu_len(len)) {
282 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
283 return;
286 lqi = skb->data[len + 1];
287 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
288 skb_pull(skb, 1); /* remove PHR */
289 skb_trim(skb, len); /* get payload only */
290 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
291 urb->context = NULL; /* skb is gone */
294 static void atusb_in(struct urb *urb)
296 struct usb_device *usb_dev = urb->dev;
297 struct sk_buff *skb = urb->context;
298 struct atusb *atusb = SKB_ATUSB(skb);
300 dev_dbg(&usb_dev->dev, "atusb_in: status %d len %d\n",
301 urb->status, urb->actual_length);
302 if (urb->status) {
303 if (urb->status == -ENOENT) { /* being killed */
304 kfree_skb(skb);
305 urb->context = NULL;
306 return;
308 dev_dbg(&usb_dev->dev, "atusb_in: URB error %d\n", urb->status);
309 } else {
310 atusb_in_good(urb);
313 usb_anchor_urb(urb, &atusb->idle_urbs);
314 if (!atusb->shutdown)
315 schedule_delayed_work(&atusb->work, 0);
318 /* ----- URB allocation/deallocation --------------------------------------- */
320 static void atusb_free_urbs(struct atusb *atusb)
322 struct urb *urb;
324 while (1) {
325 urb = usb_get_from_anchor(&atusb->idle_urbs);
326 if (!urb)
327 break;
328 kfree_skb(urb->context);
329 usb_free_urb(urb);
333 static int atusb_alloc_urbs(struct atusb *atusb, int n)
335 struct urb *urb;
337 while (n) {
338 urb = usb_alloc_urb(0, GFP_KERNEL);
339 if (!urb) {
340 atusb_free_urbs(atusb);
341 return -ENOMEM;
343 usb_anchor_urb(urb, &atusb->idle_urbs);
344 n--;
346 return 0;
349 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
351 static void atusb_xmit_complete(struct urb *urb)
353 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
356 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
358 struct atusb *atusb = hw->priv;
359 struct usb_device *usb_dev = atusb->usb_dev;
360 int ret;
362 dev_dbg(&usb_dev->dev, "atusb_xmit (%d)\n", skb->len);
363 atusb->tx_skb = skb;
364 atusb->tx_ack_seq++;
365 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
366 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
368 usb_fill_control_urb(atusb->tx_urb, usb_dev,
369 usb_sndctrlpipe(usb_dev, 0),
370 (unsigned char *)&atusb->tx_dr, skb->data,
371 skb->len, atusb_xmit_complete, NULL);
372 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
373 dev_dbg(&usb_dev->dev, "atusb_xmit done (%d)\n", ret);
374 return ret;
377 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
379 struct atusb *atusb = hw->priv;
380 int ret;
382 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
383 if (ret < 0)
384 return ret;
385 msleep(1); /* @@@ ugly synchronization */
386 return 0;
389 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
391 BUG_ON(!level);
392 *level = 0xbe;
393 return 0;
396 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
397 struct ieee802154_hw_addr_filt *filt,
398 unsigned long changed)
400 struct atusb *atusb = hw->priv;
401 struct device *dev = &atusb->usb_dev->dev;
403 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
404 u16 addr = le16_to_cpu(filt->short_addr);
406 dev_vdbg(dev, "atusb_set_hw_addr_filt called for saddr\n");
407 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
408 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
411 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
412 u16 pan = le16_to_cpu(filt->pan_id);
414 dev_vdbg(dev, "atusb_set_hw_addr_filt called for pan id\n");
415 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
416 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
419 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
420 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
422 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
423 dev_vdbg(dev, "atusb_set_hw_addr_filt called for IEEE addr\n");
424 for (i = 0; i < 8; i++)
425 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
428 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
429 dev_vdbg(dev,
430 "atusb_set_hw_addr_filt called for panc change\n");
431 if (filt->pan_coord)
432 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
433 else
434 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
437 return atusb_get_and_clear_error(atusb);
440 static int atusb_start(struct ieee802154_hw *hw)
442 struct atusb *atusb = hw->priv;
443 struct usb_device *usb_dev = atusb->usb_dev;
444 int ret;
446 dev_dbg(&usb_dev->dev, "atusb_start\n");
447 schedule_delayed_work(&atusb->work, 0);
448 atusb_command(atusb, ATUSB_RX_MODE, 1);
449 ret = atusb_get_and_clear_error(atusb);
450 if (ret < 0)
451 usb_kill_anchored_urbs(&atusb->idle_urbs);
452 return ret;
455 static void atusb_stop(struct ieee802154_hw *hw)
457 struct atusb *atusb = hw->priv;
458 struct usb_device *usb_dev = atusb->usb_dev;
460 dev_dbg(&usb_dev->dev, "atusb_stop\n");
461 usb_kill_anchored_urbs(&atusb->idle_urbs);
462 atusb_command(atusb, ATUSB_RX_MODE, 0);
463 atusb_get_and_clear_error(atusb);
466 #define ATUSB_MAX_TX_POWERS 0xF
467 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
468 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
469 -900, -1200, -1700,
472 static int
473 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
475 struct atusb *atusb = hw->priv;
476 u32 i;
478 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
479 if (hw->phy->supported.tx_powers[i] == mbm)
480 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
483 return -EINVAL;
486 #define ATUSB_MAX_ED_LEVELS 0xF
487 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
488 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
489 -7100, -6900, -6700, -6500, -6300, -6100,
492 static int
493 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
495 struct atusb *atusb = hw->priv;
496 u8 val;
498 /* mapping 802.15.4 to driver spec */
499 switch (cca->mode) {
500 case NL802154_CCA_ENERGY:
501 val = 1;
502 break;
503 case NL802154_CCA_CARRIER:
504 val = 2;
505 break;
506 case NL802154_CCA_ENERGY_CARRIER:
507 switch (cca->opt) {
508 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
509 val = 3;
510 break;
511 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
512 val = 0;
513 break;
514 default:
515 return -EINVAL;
517 break;
518 default:
519 return -EINVAL;
522 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
525 static int
526 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
528 struct atusb *atusb = hw->priv;
529 u32 i;
531 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
532 if (hw->phy->supported.cca_ed_levels[i] == mbm)
533 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
536 return -EINVAL;
539 static int
540 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
542 struct atusb *atusb = hw->priv;
543 int ret;
545 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
546 if (ret)
547 return ret;
549 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
550 if (ret)
551 return ret;
553 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
556 static int
557 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
559 struct atusb *atusb = hw->priv;
560 int ret;
562 if (on) {
563 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
564 if (ret < 0)
565 return ret;
567 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
568 if (ret < 0)
569 return ret;
570 } else {
571 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
572 if (ret < 0)
573 return ret;
575 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
576 if (ret < 0)
577 return ret;
580 return 0;
583 static struct ieee802154_ops atusb_ops = {
584 .owner = THIS_MODULE,
585 .xmit_async = atusb_xmit,
586 .ed = atusb_ed,
587 .set_channel = atusb_channel,
588 .start = atusb_start,
589 .stop = atusb_stop,
590 .set_hw_addr_filt = atusb_set_hw_addr_filt,
591 .set_txpower = atusb_set_txpower,
592 .set_cca_mode = atusb_set_cca_mode,
593 .set_cca_ed_level = atusb_set_cca_ed_level,
594 .set_csma_params = atusb_set_csma_params,
595 .set_promiscuous_mode = atusb_set_promiscuous_mode,
598 /* ----- Firmware and chip version information ----------------------------- */
600 static int atusb_get_and_show_revision(struct atusb *atusb)
602 struct usb_device *usb_dev = atusb->usb_dev;
603 unsigned char *buffer;
604 int ret;
606 buffer = kmalloc(3, GFP_KERNEL);
607 if (!buffer)
608 return -ENOMEM;
610 /* Get a couple of the ATMega Firmware values */
611 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
612 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
613 buffer, 3, 1000);
614 if (ret >= 0)
615 dev_info(&usb_dev->dev,
616 "Firmware: major: %u, minor: %u, hardware type: %u\n",
617 buffer[0], buffer[1], buffer[2]);
618 if (buffer[0] == 0 && buffer[1] < 2) {
619 dev_info(&usb_dev->dev,
620 "Firmware version (%u.%u) is predates our first public release.",
621 buffer[0], buffer[1]);
622 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
625 kfree(buffer);
626 return ret;
629 static int atusb_get_and_show_build(struct atusb *atusb)
631 struct usb_device *usb_dev = atusb->usb_dev;
632 char *build;
633 int ret;
635 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
636 if (!build)
637 return -ENOMEM;
639 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
640 ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
641 build, ATUSB_BUILD_SIZE, 1000);
642 if (ret >= 0) {
643 build[ret] = 0;
644 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
647 kfree(build);
648 return ret;
651 static int atusb_get_and_show_chip(struct atusb *atusb)
653 struct usb_device *usb_dev = atusb->usb_dev;
654 uint8_t man_id_0, man_id_1, part_num, version_num;
655 const char *chip;
657 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
658 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
659 part_num = atusb_read_reg(atusb, RG_PART_NUM);
660 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
662 if (atusb->err)
663 return atusb->err;
665 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
666 dev_err(&usb_dev->dev,
667 "non-Atmel transceiver xxxx%02x%02x\n",
668 man_id_1, man_id_0);
669 goto fail;
672 switch (part_num) {
673 case 2:
674 chip = "AT86RF230";
675 break;
676 case 3:
677 chip = "AT86RF231";
678 break;
679 default:
680 dev_err(&usb_dev->dev,
681 "unexpected transceiver, part 0x%02x version 0x%02x\n",
682 part_num, version_num);
683 goto fail;
686 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
688 return 0;
690 fail:
691 atusb->err = -ENODEV;
692 return -ENODEV;
695 /* ----- Setup ------------------------------------------------------------- */
697 static int atusb_probe(struct usb_interface *interface,
698 const struct usb_device_id *id)
700 struct usb_device *usb_dev = interface_to_usbdev(interface);
701 struct ieee802154_hw *hw;
702 struct atusb *atusb = NULL;
703 int ret = -ENOMEM;
705 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
706 if (!hw)
707 return -ENOMEM;
709 atusb = hw->priv;
710 atusb->hw = hw;
711 atusb->usb_dev = usb_get_dev(usb_dev);
712 usb_set_intfdata(interface, atusb);
714 atusb->shutdown = 0;
715 atusb->err = 0;
716 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
717 init_usb_anchor(&atusb->idle_urbs);
718 init_usb_anchor(&atusb->rx_urbs);
720 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
721 goto fail;
723 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
724 atusb->tx_dr.bRequest = ATUSB_TX;
725 atusb->tx_dr.wValue = cpu_to_le16(0);
727 atusb->tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
728 if (!atusb->tx_urb)
729 goto fail;
731 hw->parent = &usb_dev->dev;
732 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
733 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
735 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
736 WPAN_PHY_FLAG_CCA_MODE;
738 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
739 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
740 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
741 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
743 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
744 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
746 hw->phy->cca.mode = NL802154_CCA_ENERGY;
748 hw->phy->current_page = 0;
749 hw->phy->current_channel = 11; /* reset default */
750 hw->phy->supported.channels[0] = 0x7FFF800;
751 hw->phy->supported.tx_powers = atusb_powers;
752 hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
753 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
754 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
755 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
757 atusb_command(atusb, ATUSB_RF_RESET, 0);
758 atusb_get_and_show_chip(atusb);
759 atusb_get_and_show_revision(atusb);
760 atusb_get_and_show_build(atusb);
761 ret = atusb_get_and_clear_error(atusb);
762 if (ret) {
763 dev_err(&atusb->usb_dev->dev,
764 "%s: initialization failed, error = %d\n",
765 __func__, ret);
766 goto fail;
769 ret = ieee802154_register_hw(hw);
770 if (ret)
771 goto fail;
773 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
774 * explicitly. Any resets after that will send us straight to TRX_OFF,
775 * making the command below redundant.
777 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
778 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
780 #if 0
781 /* Calculating the maximum time available to empty the frame buffer
782 * on reception:
784 * According to [1], the inter-frame gap is
785 * R * 20 * 16 us + 128 us
786 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
787 * times (80 us at 250 kbps) of SHR of the next frame before the
788 * transceiver begins storing data in the frame buffer.
790 * This yields a minimum time of 208 us between the last data of a
791 * frame and the first data of the next frame. This time is further
792 * reduced by interrupt latency in the atusb firmware.
794 * atusb currently needs about 500 us to retrieve a maximum-sized
795 * frame. We therefore have to allow reception of a new frame to begin
796 * while we retrieve the previous frame.
798 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
799 * network", Jennic 2006.
800 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
803 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
804 #endif
805 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
807 ret = atusb_get_and_clear_error(atusb);
808 if (!ret)
809 return 0;
811 dev_err(&atusb->usb_dev->dev,
812 "%s: setup failed, error = %d\n",
813 __func__, ret);
815 ieee802154_unregister_hw(hw);
816 fail:
817 atusb_free_urbs(atusb);
818 usb_kill_urb(atusb->tx_urb);
819 usb_free_urb(atusb->tx_urb);
820 usb_put_dev(usb_dev);
821 ieee802154_free_hw(hw);
822 return ret;
825 static void atusb_disconnect(struct usb_interface *interface)
827 struct atusb *atusb = usb_get_intfdata(interface);
829 dev_dbg(&atusb->usb_dev->dev, "atusb_disconnect\n");
831 atusb->shutdown = 1;
832 cancel_delayed_work_sync(&atusb->work);
834 usb_kill_anchored_urbs(&atusb->rx_urbs);
835 atusb_free_urbs(atusb);
836 usb_kill_urb(atusb->tx_urb);
837 usb_free_urb(atusb->tx_urb);
839 ieee802154_unregister_hw(atusb->hw);
841 ieee802154_free_hw(atusb->hw);
843 usb_set_intfdata(interface, NULL);
844 usb_put_dev(atusb->usb_dev);
846 pr_debug("atusb_disconnect done\n");
849 /* The devices we work with */
850 static const struct usb_device_id atusb_device_table[] = {
852 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
853 USB_DEVICE_ID_MATCH_INT_INFO,
854 .idVendor = ATUSB_VENDOR_ID,
855 .idProduct = ATUSB_PRODUCT_ID,
856 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
858 /* end with null element */
861 MODULE_DEVICE_TABLE(usb, atusb_device_table);
863 static struct usb_driver atusb_driver = {
864 .name = "atusb",
865 .probe = atusb_probe,
866 .disconnect = atusb_disconnect,
867 .id_table = atusb_device_table,
869 module_usb_driver(atusb_driver);
871 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
872 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
873 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
874 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
875 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
876 MODULE_LICENSE("GPL");