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.
14 * Copyright (C) 2009 Siemens AG
15 * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
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"
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 */
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 */
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 */
57 struct usb_ctrlrequest tx_dr
;
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
;
80 ret
= usb_control_msg(usb_dev
, pipe
, request
, requesttype
,
81 value
, index
, data
, size
, timeout
);
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
);
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",
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
;
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
;
131 dev_dbg(&usb_dev
->dev
, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
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.
140 tmp
|= (value
<< shift
) & mask
;
143 ret
= atusb_write_reg(atusb
, reg
, tmp
);
148 static int atusb_get_and_clear_error(struct atusb
*atusb
)
150 int err
= atusb
->err
;
156 /* ----- skb allocation ---------------------------------------------------- */
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
;
172 skb
= alloc_skb(MAX_RX_XFER
, GFP_KERNEL
);
174 dev_warn_ratelimited(&usb_dev
->dev
,
175 "atusb_in: can't allocate skb\n");
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
);
188 usb_unanchor_urb(urb
);
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
;
207 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
210 ret
= atusb_submit_rx_urb(atusb
, urb
);
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
);
229 /* TODO check for ifs handling in firmware */
230 ieee802154_xmit_complete(atusb
->hw
, atusb
->tx_skb
, false);
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
);
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
);
250 if (!urb
->actual_length
) {
251 dev_dbg(&usb_dev
->dev
, "atusb_in: zero-sized URB ?\n");
257 if (urb
->actual_length
== 1) {
258 atusb_tx_done(atusb
, len
);
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
);
268 if (!ieee802154_is_valid_psdu_len(len
)) {
269 dev_dbg(&usb_dev
->dev
, "atusb_in: frame corrupted\n");
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
);
290 if (urb
->status
== -ENOENT
) { /* being killed */
295 dev_dbg(&usb_dev
->dev
, "atusb_in: URB error %d\n", urb
->status
);
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
)
312 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
315 kfree_skb(urb
->context
);
320 static int atusb_alloc_urbs(struct atusb
*atusb
, int n
)
325 urb
= usb_alloc_urb(0, GFP_KERNEL
);
327 atusb_free_urbs(atusb
);
330 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
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
;
349 dev_dbg(&usb_dev
->dev
, "atusb_xmit (%d)\n", skb
->len
);
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
);
364 static int atusb_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
366 struct atusb
*atusb
= hw
->priv
;
369 ret
= atusb_write_subreg(atusb
, SR_CHANNEL
, channel
);
372 msleep(1); /* @@@ ugly synchronization */
376 static int atusb_ed(struct ieee802154_hw
*hw
, u8
*level
)
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
) {
417 "atusb_set_hw_addr_filt called for panc change\n");
419 atusb_write_subreg(atusb
, SR_AACK_I_AM_COORD
, 1);
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
;
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
);
438 usb_kill_anchored_urbs(&atusb
->idle_urbs
);
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,
460 atusb_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
462 struct atusb
*atusb
= hw
->priv
;
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
);
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,
480 atusb_set_cca_mode(struct ieee802154_hw
*hw
, const struct wpan_phy_cca
*cca
)
482 struct atusb
*atusb
= hw
->priv
;
485 /* mapping 802.15.4 to driver spec */
487 case NL802154_CCA_ENERGY
:
490 case NL802154_CCA_CARRIER
:
493 case NL802154_CCA_ENERGY_CARRIER
:
495 case NL802154_CCA_OPT_ENERGY_CARRIER_AND
:
498 case NL802154_CCA_OPT_ENERGY_CARRIER_OR
:
509 return atusb_write_subreg(atusb
, SR_CCA_MODE
, val
);
513 atusb_set_cca_ed_level(struct ieee802154_hw
*hw
, s32 mbm
)
515 struct atusb
*atusb
= hw
->priv
;
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
);
527 atusb_set_csma_params(struct ieee802154_hw
*hw
, u8 min_be
, u8 max_be
, u8 retries
)
529 struct atusb
*atusb
= hw
->priv
;
532 ret
= atusb_write_subreg(atusb
, SR_MIN_BE
, min_be
);
536 ret
= atusb_write_subreg(atusb
, SR_MAX_BE
, max_be
);
540 return atusb_write_subreg(atusb
, SR_MAX_CSMA_RETRIES
, retries
);
544 atusb_set_promiscuous_mode(struct ieee802154_hw
*hw
, const bool on
)
546 struct atusb
*atusb
= hw
->priv
;
550 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 1);
554 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 1);
558 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 0);
562 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 0);
570 static struct ieee802154_ops atusb_ops
= {
571 .owner
= THIS_MODULE
,
572 .xmit_async
= atusb_xmit
,
574 .set_channel
= atusb_channel
,
575 .start
= atusb_start
,
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];
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,
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");
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];
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);
622 dev_info(&usb_dev
->dev
, "Firmware: build %s\n", build
);
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
;
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
);
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",
657 dev_err(&usb_dev
->dev
,
658 "unexpected transceiver, part 0x%02x version 0x%02x\n",
659 part_num
, version_num
);
663 dev_info(&usb_dev
->dev
, "ATUSB: %s version %d\n", chip
, version_num
);
668 atusb
->err
= -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
;
682 hw
= ieee802154_alloc_hw(sizeof(struct atusb
), &atusb_ops
);
688 atusb
->usb_dev
= usb_get_dev(usb_dev
);
689 usb_set_intfdata(interface
, atusb
);
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
))
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
);
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
);
740 dev_err(&atusb
->usb_dev
->dev
,
741 "%s: initialization failed, error = %d\n",
746 ret
= ieee802154_register_hw(hw
);
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 */
758 /* Calculating the maximum time available to empty the frame buffer
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);
782 atusb_write_reg(atusb
, RG_IRQ_MASK
, 0xff);
784 ret
= atusb_get_and_clear_error(atusb
);
788 dev_err(&atusb
->usb_dev
->dev
,
789 "%s: setup failed, error = %d\n",
792 ieee802154_unregister_hw(hw
);
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
);
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");
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
= {
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");