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
;
118 buffer
= kmalloc(1, GFP_KERNEL
);
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);
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
;
144 dev_dbg(&usb_dev
->dev
, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
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.
153 tmp
|= (value
<< shift
) & mask
;
156 ret
= atusb_write_reg(atusb
, reg
, tmp
);
161 static int atusb_get_and_clear_error(struct atusb
*atusb
)
163 int err
= atusb
->err
;
169 /* ----- skb allocation ---------------------------------------------------- */
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
;
185 skb
= alloc_skb(MAX_RX_XFER
, GFP_KERNEL
);
187 dev_warn_ratelimited(&usb_dev
->dev
,
188 "atusb_in: can't allocate skb\n");
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
);
201 usb_unanchor_urb(urb
);
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
;
220 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
223 ret
= atusb_submit_rx_urb(atusb
, urb
);
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
);
242 /* TODO check for ifs handling in firmware */
243 ieee802154_xmit_complete(atusb
->hw
, atusb
->tx_skb
, false);
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
);
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
);
263 if (!urb
->actual_length
) {
264 dev_dbg(&usb_dev
->dev
, "atusb_in: zero-sized URB ?\n");
270 if (urb
->actual_length
== 1) {
271 atusb_tx_done(atusb
, len
);
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
);
281 if (!ieee802154_is_valid_psdu_len(len
)) {
282 dev_dbg(&usb_dev
->dev
, "atusb_in: frame corrupted\n");
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
);
303 if (urb
->status
== -ENOENT
) { /* being killed */
308 dev_dbg(&usb_dev
->dev
, "atusb_in: URB error %d\n", urb
->status
);
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
)
325 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
328 kfree_skb(urb
->context
);
333 static int atusb_alloc_urbs(struct atusb
*atusb
, int n
)
338 urb
= usb_alloc_urb(0, GFP_KERNEL
);
340 atusb_free_urbs(atusb
);
343 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
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
;
362 dev_dbg(&usb_dev
->dev
, "atusb_xmit (%d)\n", skb
->len
);
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
);
377 static int atusb_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
379 struct atusb
*atusb
= hw
->priv
;
382 ret
= atusb_write_subreg(atusb
, SR_CHANNEL
, channel
);
385 msleep(1); /* @@@ ugly synchronization */
389 static int atusb_ed(struct ieee802154_hw
*hw
, u8
*level
)
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
) {
430 "atusb_set_hw_addr_filt called for panc change\n");
432 atusb_write_subreg(atusb
, SR_AACK_I_AM_COORD
, 1);
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
;
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
);
451 usb_kill_anchored_urbs(&atusb
->idle_urbs
);
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,
473 atusb_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
475 struct atusb
*atusb
= hw
->priv
;
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
);
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,
493 atusb_set_cca_mode(struct ieee802154_hw
*hw
, const struct wpan_phy_cca
*cca
)
495 struct atusb
*atusb
= hw
->priv
;
498 /* mapping 802.15.4 to driver spec */
500 case NL802154_CCA_ENERGY
:
503 case NL802154_CCA_CARRIER
:
506 case NL802154_CCA_ENERGY_CARRIER
:
508 case NL802154_CCA_OPT_ENERGY_CARRIER_AND
:
511 case NL802154_CCA_OPT_ENERGY_CARRIER_OR
:
522 return atusb_write_subreg(atusb
, SR_CCA_MODE
, val
);
526 atusb_set_cca_ed_level(struct ieee802154_hw
*hw
, s32 mbm
)
528 struct atusb
*atusb
= hw
->priv
;
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
);
540 atusb_set_csma_params(struct ieee802154_hw
*hw
, u8 min_be
, u8 max_be
, u8 retries
)
542 struct atusb
*atusb
= hw
->priv
;
545 ret
= atusb_write_subreg(atusb
, SR_MIN_BE
, min_be
);
549 ret
= atusb_write_subreg(atusb
, SR_MAX_BE
, max_be
);
553 return atusb_write_subreg(atusb
, SR_MAX_CSMA_RETRIES
, retries
);
557 atusb_set_promiscuous_mode(struct ieee802154_hw
*hw
, const bool on
)
559 struct atusb
*atusb
= hw
->priv
;
563 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 1);
567 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 1);
571 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 0);
575 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 0);
583 static struct ieee802154_ops atusb_ops
= {
584 .owner
= THIS_MODULE
,
585 .xmit_async
= atusb_xmit
,
587 .set_channel
= atusb_channel
,
588 .start
= atusb_start
,
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
;
606 buffer
= kmalloc(3, GFP_KERNEL
);
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,
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");
629 static int atusb_get_and_show_build(struct atusb
*atusb
)
631 struct usb_device
*usb_dev
= atusb
->usb_dev
;
635 build
= kmalloc(ATUSB_BUILD_SIZE
+ 1, GFP_KERNEL
);
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);
644 dev_info(&usb_dev
->dev
, "Firmware: build %s\n", build
);
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
;
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
);
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",
680 dev_err(&usb_dev
->dev
,
681 "unexpected transceiver, part 0x%02x version 0x%02x\n",
682 part_num
, version_num
);
686 dev_info(&usb_dev
->dev
, "ATUSB: %s version %d\n", chip
, version_num
);
691 atusb
->err
= -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
;
705 hw
= ieee802154_alloc_hw(sizeof(struct atusb
), &atusb_ops
);
711 atusb
->usb_dev
= usb_get_dev(usb_dev
);
712 usb_set_intfdata(interface
, atusb
);
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
))
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
);
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
);
763 dev_err(&atusb
->usb_dev
->dev
,
764 "%s: initialization failed, error = %d\n",
769 ret
= ieee802154_register_hw(hw
);
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 */
781 /* Calculating the maximum time available to empty the frame buffer
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);
805 atusb_write_reg(atusb
, RG_IRQ_MASK
, 0xff);
807 ret
= atusb_get_and_clear_error(atusb
);
811 dev_err(&atusb
->usb_dev
->dev
,
812 "%s: setup failed, error = %d\n",
815 ieee802154_unregister_hw(hw
);
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
);
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");
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
= {
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");