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>
25 * Busware HUL support is
26 * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/jiffies.h>
33 #include <linux/usb.h>
34 #include <linux/skbuff.h>
36 #include <net/cfg802154.h>
37 #include <net/mac802154.h>
39 #include "at86rf230.h"
42 #define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */
44 #define ATUSB_NUM_RX_URBS 4 /* allow for a bit of local latency */
45 #define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */
46 #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */
49 struct ieee802154_hw
*hw
;
50 struct usb_device
*usb_dev
;
51 struct atusb_chip_data
*data
;
52 int shutdown
; /* non-zero if shutting down */
53 int err
; /* set by first error */
56 struct delayed_work work
; /* memory allocations */
57 struct usb_anchor idle_urbs
; /* URBs waiting to be submitted */
58 struct usb_anchor rx_urbs
; /* URBs waiting for reception */
61 struct usb_ctrlrequest tx_dr
;
63 struct sk_buff
*tx_skb
;
64 u8 tx_ack_seq
; /* current TX ACK sequence number */
66 /* Firmware variable */
67 unsigned char fw_ver_maj
; /* Firmware major version number */
68 unsigned char fw_ver_min
; /* Firmware minor version number */
69 unsigned char fw_hw_type
; /* Firmware hardware type */
72 struct atusb_chip_data
{
76 int (*set_channel
)(struct ieee802154_hw
*, u8
, u8
);
77 int (*set_txpower
)(struct ieee802154_hw
*, s32
);
80 /* ----- USB commands without data ----------------------------------------- */
82 /* To reduce the number of error checks in the code, we record the first error
83 * in atusb->err and reject all subsequent requests until the error is cleared.
86 static int atusb_control_msg(struct atusb
*atusb
, unsigned int pipe
,
87 __u8 request
, __u8 requesttype
,
88 __u16 value
, __u16 index
,
89 void *data
, __u16 size
, int timeout
)
91 struct usb_device
*usb_dev
= atusb
->usb_dev
;
97 ret
= usb_control_msg(usb_dev
, pipe
, request
, requesttype
,
98 value
, index
, data
, size
, timeout
);
101 dev_err(&usb_dev
->dev
,
102 "%s: req 0x%02x val 0x%x idx 0x%x, error %d\n",
103 __func__
, request
, value
, index
, ret
);
108 static int atusb_command(struct atusb
*atusb
, u8 cmd
, u8 arg
)
110 struct usb_device
*usb_dev
= atusb
->usb_dev
;
112 dev_dbg(&usb_dev
->dev
, "%s: cmd = 0x%x\n", __func__
, cmd
);
113 return atusb_control_msg(atusb
, usb_sndctrlpipe(usb_dev
, 0),
114 cmd
, ATUSB_REQ_TO_DEV
, arg
, 0, NULL
, 0, 1000);
117 static int atusb_write_reg(struct atusb
*atusb
, u8 reg
, u8 value
)
119 struct usb_device
*usb_dev
= atusb
->usb_dev
;
121 dev_dbg(&usb_dev
->dev
, "%s: 0x%02x <- 0x%02x\n", __func__
, reg
, value
);
122 return atusb_control_msg(atusb
, usb_sndctrlpipe(usb_dev
, 0),
123 ATUSB_REG_WRITE
, ATUSB_REQ_TO_DEV
,
124 value
, reg
, NULL
, 0, 1000);
127 static int atusb_read_reg(struct atusb
*atusb
, u8 reg
)
129 struct usb_device
*usb_dev
= atusb
->usb_dev
;
134 buffer
= kmalloc(1, GFP_KERNEL
);
138 dev_dbg(&usb_dev
->dev
, "%s: reg = 0x%x\n", __func__
, reg
);
139 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
140 ATUSB_REG_READ
, ATUSB_REQ_FROM_DEV
,
141 0, reg
, buffer
, 1, 1000);
153 static int atusb_write_subreg(struct atusb
*atusb
, u8 reg
, u8 mask
,
156 struct usb_device
*usb_dev
= atusb
->usb_dev
;
160 dev_dbg(&usb_dev
->dev
, "%s: 0x%02x <- 0x%02x\n", __func__
, reg
, value
);
162 orig
= atusb_read_reg(atusb
, reg
);
164 /* Write the value only into that part of the register which is allowed
165 * by the mask. All other bits stay as before.
168 tmp
|= (value
<< shift
) & mask
;
171 ret
= atusb_write_reg(atusb
, reg
, tmp
);
176 static int atusb_read_subreg(struct atusb
*lp
,
177 unsigned int addr
, unsigned int mask
,
182 rc
= atusb_read_reg(lp
, addr
);
183 rc
= (rc
& mask
) >> shift
;
188 static int atusb_get_and_clear_error(struct atusb
*atusb
)
190 int err
= atusb
->err
;
196 /* ----- skb allocation ---------------------------------------------------- */
199 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
201 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
203 static void atusb_in(struct urb
*urb
);
205 static int atusb_submit_rx_urb(struct atusb
*atusb
, struct urb
*urb
)
207 struct usb_device
*usb_dev
= atusb
->usb_dev
;
208 struct sk_buff
*skb
= urb
->context
;
212 skb
= alloc_skb(MAX_RX_XFER
, GFP_KERNEL
);
214 dev_warn_ratelimited(&usb_dev
->dev
,
215 "atusb_in: can't allocate skb\n");
218 skb_put(skb
, MAX_RX_XFER
);
219 SKB_ATUSB(skb
) = atusb
;
222 usb_fill_bulk_urb(urb
, usb_dev
, usb_rcvbulkpipe(usb_dev
, 1),
223 skb
->data
, MAX_RX_XFER
, atusb_in
, skb
);
224 usb_anchor_urb(urb
, &atusb
->rx_urbs
);
226 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
228 usb_unanchor_urb(urb
);
235 static void atusb_work_urbs(struct work_struct
*work
)
237 struct atusb
*atusb
=
238 container_of(to_delayed_work(work
), struct atusb
, work
);
239 struct usb_device
*usb_dev
= atusb
->usb_dev
;
247 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
250 ret
= atusb_submit_rx_urb(atusb
, urb
);
253 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
254 dev_warn_ratelimited(&usb_dev
->dev
,
255 "atusb_in: can't allocate/submit URB (%d)\n", ret
);
256 schedule_delayed_work(&atusb
->work
,
257 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS
) + 1);
260 /* ----- Asynchronous USB -------------------------------------------------- */
262 static void atusb_tx_done(struct atusb
*atusb
, u8 seq
)
264 struct usb_device
*usb_dev
= atusb
->usb_dev
;
265 u8 expect
= atusb
->tx_ack_seq
;
267 dev_dbg(&usb_dev
->dev
, "%s (0x%02x/0x%02x)\n", __func__
, seq
, expect
);
269 /* TODO check for ifs handling in firmware */
270 ieee802154_xmit_complete(atusb
->hw
, atusb
->tx_skb
, false);
272 /* TODO I experience this case when atusb has a tx complete
273 * irq before probing, we should fix the firmware it's an
274 * unlikely case now that seq == expect is then true, but can
275 * happen and fail with a tx_skb = NULL;
277 ieee802154_wake_queue(atusb
->hw
);
279 dev_kfree_skb_irq(atusb
->tx_skb
);
283 static void atusb_in_good(struct urb
*urb
)
285 struct usb_device
*usb_dev
= urb
->dev
;
286 struct sk_buff
*skb
= urb
->context
;
287 struct atusb
*atusb
= SKB_ATUSB(skb
);
290 if (!urb
->actual_length
) {
291 dev_dbg(&usb_dev
->dev
, "atusb_in: zero-sized URB ?\n");
297 if (urb
->actual_length
== 1) {
298 atusb_tx_done(atusb
, len
);
302 if (len
+ 1 > urb
->actual_length
- 1) {
303 dev_dbg(&usb_dev
->dev
, "atusb_in: frame len %d+1 > URB %u-1\n",
304 len
, urb
->actual_length
);
308 if (!ieee802154_is_valid_psdu_len(len
)) {
309 dev_dbg(&usb_dev
->dev
, "atusb_in: frame corrupted\n");
313 lqi
= skb
->data
[len
+ 1];
314 dev_dbg(&usb_dev
->dev
, "atusb_in: rx len %d lqi 0x%02x\n", len
, lqi
);
315 skb_pull(skb
, 1); /* remove PHR */
316 skb_trim(skb
, len
); /* get payload only */
317 ieee802154_rx_irqsafe(atusb
->hw
, skb
, lqi
);
318 urb
->context
= NULL
; /* skb is gone */
321 static void atusb_in(struct urb
*urb
)
323 struct usb_device
*usb_dev
= urb
->dev
;
324 struct sk_buff
*skb
= urb
->context
;
325 struct atusb
*atusb
= SKB_ATUSB(skb
);
327 dev_dbg(&usb_dev
->dev
, "%s: status %d len %d\n", __func__
,
328 urb
->status
, urb
->actual_length
);
330 if (urb
->status
== -ENOENT
) { /* being killed */
335 dev_dbg(&usb_dev
->dev
, "%s: URB error %d\n", __func__
, urb
->status
);
340 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
341 if (!atusb
->shutdown
)
342 schedule_delayed_work(&atusb
->work
, 0);
345 /* ----- URB allocation/deallocation --------------------------------------- */
347 static void atusb_free_urbs(struct atusb
*atusb
)
352 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
355 kfree_skb(urb
->context
);
360 static int atusb_alloc_urbs(struct atusb
*atusb
, int n
)
365 urb
= usb_alloc_urb(0, GFP_KERNEL
);
367 atusb_free_urbs(atusb
);
370 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
376 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
378 static void atusb_xmit_complete(struct urb
*urb
)
380 dev_dbg(&urb
->dev
->dev
, "atusb_xmit urb completed");
383 static int atusb_xmit(struct ieee802154_hw
*hw
, struct sk_buff
*skb
)
385 struct atusb
*atusb
= hw
->priv
;
386 struct usb_device
*usb_dev
= atusb
->usb_dev
;
389 dev_dbg(&usb_dev
->dev
, "%s (%d)\n", __func__
, skb
->len
);
392 atusb
->tx_dr
.wIndex
= cpu_to_le16(atusb
->tx_ack_seq
);
393 atusb
->tx_dr
.wLength
= cpu_to_le16(skb
->len
);
395 usb_fill_control_urb(atusb
->tx_urb
, usb_dev
,
396 usb_sndctrlpipe(usb_dev
, 0),
397 (unsigned char *)&atusb
->tx_dr
, skb
->data
,
398 skb
->len
, atusb_xmit_complete
, NULL
);
399 ret
= usb_submit_urb(atusb
->tx_urb
, GFP_ATOMIC
);
400 dev_dbg(&usb_dev
->dev
, "%s done (%d)\n", __func__
, ret
);
404 static int atusb_ed(struct ieee802154_hw
*hw
, u8
*level
)
411 static int atusb_set_hw_addr_filt(struct ieee802154_hw
*hw
,
412 struct ieee802154_hw_addr_filt
*filt
,
413 unsigned long changed
)
415 struct atusb
*atusb
= hw
->priv
;
416 struct device
*dev
= &atusb
->usb_dev
->dev
;
418 if (changed
& IEEE802154_AFILT_SADDR_CHANGED
) {
419 u16 addr
= le16_to_cpu(filt
->short_addr
);
421 dev_vdbg(dev
, "%s called for saddr\n", __func__
);
422 atusb_write_reg(atusb
, RG_SHORT_ADDR_0
, addr
);
423 atusb_write_reg(atusb
, RG_SHORT_ADDR_1
, addr
>> 8);
426 if (changed
& IEEE802154_AFILT_PANID_CHANGED
) {
427 u16 pan
= le16_to_cpu(filt
->pan_id
);
429 dev_vdbg(dev
, "%s called for pan id\n", __func__
);
430 atusb_write_reg(atusb
, RG_PAN_ID_0
, pan
);
431 atusb_write_reg(atusb
, RG_PAN_ID_1
, pan
>> 8);
434 if (changed
& IEEE802154_AFILT_IEEEADDR_CHANGED
) {
435 u8 i
, addr
[IEEE802154_EXTENDED_ADDR_LEN
];
437 memcpy(addr
, &filt
->ieee_addr
, IEEE802154_EXTENDED_ADDR_LEN
);
438 dev_vdbg(dev
, "%s called for IEEE addr\n", __func__
);
439 for (i
= 0; i
< 8; i
++)
440 atusb_write_reg(atusb
, RG_IEEE_ADDR_0
+ i
, addr
[i
]);
443 if (changed
& IEEE802154_AFILT_PANC_CHANGED
) {
444 dev_vdbg(dev
, "%s called for panc change\n", __func__
);
446 atusb_write_subreg(atusb
, SR_AACK_I_AM_COORD
, 1);
448 atusb_write_subreg(atusb
, SR_AACK_I_AM_COORD
, 0);
451 return atusb_get_and_clear_error(atusb
);
454 static int atusb_start(struct ieee802154_hw
*hw
)
456 struct atusb
*atusb
= hw
->priv
;
457 struct usb_device
*usb_dev
= atusb
->usb_dev
;
460 dev_dbg(&usb_dev
->dev
, "%s\n", __func__
);
461 schedule_delayed_work(&atusb
->work
, 0);
462 atusb_command(atusb
, ATUSB_RX_MODE
, 1);
463 ret
= atusb_get_and_clear_error(atusb
);
465 usb_kill_anchored_urbs(&atusb
->idle_urbs
);
469 static void atusb_stop(struct ieee802154_hw
*hw
)
471 struct atusb
*atusb
= hw
->priv
;
472 struct usb_device
*usb_dev
= atusb
->usb_dev
;
474 dev_dbg(&usb_dev
->dev
, "%s\n", __func__
);
475 usb_kill_anchored_urbs(&atusb
->idle_urbs
);
476 atusb_command(atusb
, ATUSB_RX_MODE
, 0);
477 atusb_get_and_clear_error(atusb
);
480 #define ATUSB_MAX_TX_POWERS 0xF
481 static const s32 atusb_powers
[ATUSB_MAX_TX_POWERS
+ 1] = {
482 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
487 atusb_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
489 struct atusb
*atusb
= hw
->priv
;
492 return atusb
->data
->set_txpower(hw
, mbm
);
498 atusb_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
500 struct atusb
*atusb
= hw
->priv
;
503 for (i
= 0; i
< hw
->phy
->supported
.tx_powers_size
; i
++) {
504 if (hw
->phy
->supported
.tx_powers
[i
] == mbm
)
505 return atusb_write_subreg(atusb
, SR_TX_PWR_23X
, i
);
512 hulusb_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
516 for (i
= 0; i
< hw
->phy
->supported
.tx_powers_size
; i
++) {
517 if (hw
->phy
->supported
.tx_powers
[i
] == mbm
)
518 return atusb_write_subreg(hw
->priv
, SR_TX_PWR_212
, i
);
524 #define ATUSB_MAX_ED_LEVELS 0xF
525 static const s32 atusb_ed_levels
[ATUSB_MAX_ED_LEVELS
+ 1] = {
526 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
527 -7100, -6900, -6700, -6500, -6300, -6100,
530 #define AT86RF212_MAX_TX_POWERS 0x1F
531 static const s32 at86rf212_powers
[AT86RF212_MAX_TX_POWERS
+ 1] = {
532 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
533 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
534 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
537 #define AT86RF2XX_MAX_ED_LEVELS 0xF
538 static const s32 at86rf212_ed_levels_100
[AT86RF2XX_MAX_ED_LEVELS
+ 1] = {
539 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
540 -8000, -7800, -7600, -7400, -7200, -7000,
543 static const s32 at86rf212_ed_levels_98
[AT86RF2XX_MAX_ED_LEVELS
+ 1] = {
544 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
545 -7800, -7600, -7400, -7200, -7000, -6800,
549 atusb_set_cca_mode(struct ieee802154_hw
*hw
, const struct wpan_phy_cca
*cca
)
551 struct atusb
*atusb
= hw
->priv
;
554 /* mapping 802.15.4 to driver spec */
556 case NL802154_CCA_ENERGY
:
559 case NL802154_CCA_CARRIER
:
562 case NL802154_CCA_ENERGY_CARRIER
:
564 case NL802154_CCA_OPT_ENERGY_CARRIER_AND
:
567 case NL802154_CCA_OPT_ENERGY_CARRIER_OR
:
578 return atusb_write_subreg(atusb
, SR_CCA_MODE
, val
);
581 static int hulusb_set_cca_ed_level(struct atusb
*lp
, int rssi_base_val
)
583 unsigned int cca_ed_thres
;
585 cca_ed_thres
= atusb_read_subreg(lp
, SR_CCA_ED_THRES
);
587 switch (rssi_base_val
) {
589 lp
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_98
;
590 lp
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_98
);
591 lp
->hw
->phy
->cca_ed_level
= at86rf212_ed_levels_98
[cca_ed_thres
];
594 lp
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_100
;
595 lp
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_100
);
596 lp
->hw
->phy
->cca_ed_level
= at86rf212_ed_levels_100
[cca_ed_thres
];
606 atusb_set_cca_ed_level(struct ieee802154_hw
*hw
, s32 mbm
)
608 struct atusb
*atusb
= hw
->priv
;
611 for (i
= 0; i
< hw
->phy
->supported
.cca_ed_levels_size
; i
++) {
612 if (hw
->phy
->supported
.cca_ed_levels
[i
] == mbm
)
613 return atusb_write_subreg(atusb
, SR_CCA_ED_THRES
, i
);
619 static int atusb_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
621 struct atusb
*atusb
= hw
->priv
;
625 ret
= atusb
->data
->set_channel(hw
, page
, channel
);
626 /* @@@ ugly synchronization */
627 msleep(atusb
->data
->t_channel_switch
);
633 static int atusb_set_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
635 struct atusb
*atusb
= hw
->priv
;
638 ret
= atusb_write_subreg(atusb
, SR_CHANNEL
, channel
);
644 static int hulusb_set_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
649 struct atusb
*lp
= hw
->priv
;
652 rc
= atusb_write_subreg(lp
, SR_SUB_MODE
, 0);
654 rc
= atusb_write_subreg(lp
, SR_SUB_MODE
, 1);
659 rc
= atusb_write_subreg(lp
, SR_BPSK_QPSK
, 0);
660 rssi_base_val
= -100;
662 rc
= atusb_write_subreg(lp
, SR_BPSK_QPSK
, 1);
668 rc
= hulusb_set_cca_ed_level(lp
, rssi_base_val
);
672 /* This sets the symbol_duration according frequency on the 212.
673 * TODO move this handling while set channel and page in cfg802154.
674 * We can do that, this timings are according 802.15.4 standard.
675 * If we do that in cfg802154, this is a more generic calculation.
677 * This should also protected from ifs_timer. Means cancel timer and
678 * init with a new value. For now, this is okay.
682 /* SUB:0 and BPSK:0 -> BPSK-20 */
683 lp
->hw
->phy
->symbol_duration
= 50;
685 /* SUB:1 and BPSK:0 -> BPSK-40 */
686 lp
->hw
->phy
->symbol_duration
= 25;
690 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
691 lp
->hw
->phy
->symbol_duration
= 40;
693 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
694 lp
->hw
->phy
->symbol_duration
= 16;
697 lp
->hw
->phy
->lifs_period
= IEEE802154_LIFS_PERIOD
*
698 lp
->hw
->phy
->symbol_duration
;
699 lp
->hw
->phy
->sifs_period
= IEEE802154_SIFS_PERIOD
*
700 lp
->hw
->phy
->symbol_duration
;
702 return atusb_write_subreg(lp
, SR_CHANNEL
, channel
);
706 atusb_set_csma_params(struct ieee802154_hw
*hw
, u8 min_be
, u8 max_be
, u8 retries
)
708 struct atusb
*atusb
= hw
->priv
;
711 ret
= atusb_write_subreg(atusb
, SR_MIN_BE
, min_be
);
715 ret
= atusb_write_subreg(atusb
, SR_MAX_BE
, max_be
);
719 return atusb_write_subreg(atusb
, SR_MAX_CSMA_RETRIES
, retries
);
723 hulusb_set_lbt(struct ieee802154_hw
*hw
, bool on
)
725 struct atusb
*atusb
= hw
->priv
;
727 return atusb_write_subreg(atusb
, SR_CSMA_LBT_MODE
, on
);
731 atusb_set_frame_retries(struct ieee802154_hw
*hw
, s8 retries
)
733 struct atusb
*atusb
= hw
->priv
;
735 return atusb_write_subreg(atusb
, SR_MAX_FRAME_RETRIES
, retries
);
739 atusb_set_promiscuous_mode(struct ieee802154_hw
*hw
, const bool on
)
741 struct atusb
*atusb
= hw
->priv
;
745 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 1);
749 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 1);
753 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 0);
757 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 0);
765 static struct atusb_chip_data atusb_chip_data
= {
766 .t_channel_switch
= 1,
767 .rssi_base_val
= -91,
768 .set_txpower
= atusb_set_txpower
,
769 .set_channel
= atusb_set_channel
,
772 static struct atusb_chip_data hulusb_chip_data
= {
773 .t_channel_switch
= 11,
774 .rssi_base_val
= -100,
775 .set_txpower
= hulusb_set_txpower
,
776 .set_channel
= hulusb_set_channel
,
779 static const struct ieee802154_ops atusb_ops
= {
780 .owner
= THIS_MODULE
,
781 .xmit_async
= atusb_xmit
,
783 .set_channel
= atusb_channel
,
784 .start
= atusb_start
,
786 .set_hw_addr_filt
= atusb_set_hw_addr_filt
,
787 .set_txpower
= atusb_txpower
,
788 .set_lbt
= hulusb_set_lbt
,
789 .set_cca_mode
= atusb_set_cca_mode
,
790 .set_cca_ed_level
= atusb_set_cca_ed_level
,
791 .set_csma_params
= atusb_set_csma_params
,
792 .set_frame_retries
= atusb_set_frame_retries
,
793 .set_promiscuous_mode
= atusb_set_promiscuous_mode
,
796 /* ----- Firmware and chip version information ----------------------------- */
798 static int atusb_get_and_show_revision(struct atusb
*atusb
)
800 struct usb_device
*usb_dev
= atusb
->usb_dev
;
802 unsigned char *buffer
;
805 buffer
= kmalloc(3, GFP_KERNEL
);
809 /* Get a couple of the ATMega Firmware values */
810 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
811 ATUSB_ID
, ATUSB_REQ_FROM_DEV
, 0, 0,
814 atusb
->fw_ver_maj
= buffer
[0];
815 atusb
->fw_ver_min
= buffer
[1];
816 atusb
->fw_hw_type
= buffer
[2];
818 switch (atusb
->fw_hw_type
) {
819 case ATUSB_HW_TYPE_100813
:
820 case ATUSB_HW_TYPE_101216
:
821 case ATUSB_HW_TYPE_110131
:
823 atusb
->data
= &atusb_chip_data
;
825 case ATUSB_HW_TYPE_RZUSB
:
827 atusb
->data
= &atusb_chip_data
;
829 case ATUSB_HW_TYPE_HULUSB
:
831 atusb
->data
= &hulusb_chip_data
;
835 atusb
->err
= -ENOTSUPP
;
840 dev_info(&usb_dev
->dev
,
841 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
842 atusb
->fw_ver_maj
, atusb
->fw_ver_min
, hw_name
,
845 if (atusb
->fw_ver_maj
== 0 && atusb
->fw_ver_min
< 2) {
846 dev_info(&usb_dev
->dev
,
847 "Firmware version (%u.%u) predates our first public release.",
848 atusb
->fw_ver_maj
, atusb
->fw_ver_min
);
849 dev_info(&usb_dev
->dev
, "Please update to version 0.2 or newer");
856 static int atusb_get_and_show_build(struct atusb
*atusb
)
858 struct usb_device
*usb_dev
= atusb
->usb_dev
;
862 build
= kmalloc(ATUSB_BUILD_SIZE
+ 1, GFP_KERNEL
);
866 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
867 ATUSB_BUILD
, ATUSB_REQ_FROM_DEV
, 0, 0,
868 build
, ATUSB_BUILD_SIZE
, 1000);
871 dev_info(&usb_dev
->dev
, "Firmware: build %s\n", build
);
878 static int atusb_get_and_conf_chip(struct atusb
*atusb
)
880 struct usb_device
*usb_dev
= atusb
->usb_dev
;
881 u8 man_id_0
, man_id_1
, part_num
, version_num
;
883 struct ieee802154_hw
*hw
= atusb
->hw
;
885 man_id_0
= atusb_read_reg(atusb
, RG_MAN_ID_0
);
886 man_id_1
= atusb_read_reg(atusb
, RG_MAN_ID_1
);
887 part_num
= atusb_read_reg(atusb
, RG_PART_NUM
);
888 version_num
= atusb_read_reg(atusb
, RG_VERSION_NUM
);
893 hw
->flags
= IEEE802154_HW_TX_OMIT_CKSUM
| IEEE802154_HW_AFILT
|
894 IEEE802154_HW_PROMISCUOUS
| IEEE802154_HW_CSMA_PARAMS
;
896 hw
->phy
->flags
= WPAN_PHY_FLAG_TXPOWER
| WPAN_PHY_FLAG_CCA_ED_LEVEL
|
897 WPAN_PHY_FLAG_CCA_MODE
;
899 hw
->phy
->supported
.cca_modes
= BIT(NL802154_CCA_ENERGY
) |
900 BIT(NL802154_CCA_CARRIER
) |
901 BIT(NL802154_CCA_ENERGY_CARRIER
);
902 hw
->phy
->supported
.cca_opts
= BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND
) |
903 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR
);
905 hw
->phy
->cca
.mode
= NL802154_CCA_ENERGY
;
907 hw
->phy
->current_page
= 0;
909 if ((man_id_1
<< 8 | man_id_0
) != ATUSB_JEDEC_ATMEL
) {
910 dev_err(&usb_dev
->dev
,
911 "non-Atmel transceiver xxxx%02x%02x\n",
919 atusb
->hw
->phy
->supported
.channels
[0] = 0x7FFF800;
920 atusb
->hw
->phy
->current_channel
= 11; /* reset default */
921 atusb
->hw
->phy
->symbol_duration
= 16;
922 atusb
->hw
->phy
->supported
.tx_powers
= atusb_powers
;
923 atusb
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(atusb_powers
);
924 hw
->phy
->supported
.cca_ed_levels
= atusb_ed_levels
;
925 hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(atusb_ed_levels
);
929 atusb
->hw
->phy
->supported
.channels
[0] = 0x7FFF800;
930 atusb
->hw
->phy
->current_channel
= 11; /* reset default */
931 atusb
->hw
->phy
->symbol_duration
= 16;
932 atusb
->hw
->phy
->supported
.tx_powers
= atusb_powers
;
933 atusb
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(atusb_powers
);
934 hw
->phy
->supported
.cca_ed_levels
= atusb_ed_levels
;
935 hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(atusb_ed_levels
);
939 atusb
->hw
->flags
|= IEEE802154_HW_LBT
;
940 atusb
->hw
->phy
->supported
.channels
[0] = 0x00007FF;
941 atusb
->hw
->phy
->supported
.channels
[2] = 0x00007FF;
942 atusb
->hw
->phy
->current_channel
= 5;
943 atusb
->hw
->phy
->symbol_duration
= 25;
944 atusb
->hw
->phy
->supported
.lbt
= NL802154_SUPPORTED_BOOL_BOTH
;
945 atusb
->hw
->phy
->supported
.tx_powers
= at86rf212_powers
;
946 atusb
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(at86rf212_powers
);
947 atusb
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_100
;
948 atusb
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_100
);
951 dev_err(&usb_dev
->dev
,
952 "unexpected transceiver, part 0x%02x version 0x%02x\n",
953 part_num
, version_num
);
957 hw
->phy
->transmit_power
= hw
->phy
->supported
.tx_powers
[0];
958 hw
->phy
->cca_ed_level
= hw
->phy
->supported
.cca_ed_levels
[7];
960 dev_info(&usb_dev
->dev
, "ATUSB: %s version %d\n", chip
, version_num
);
965 atusb
->err
= -ENODEV
;
969 static int atusb_set_extended_addr(struct atusb
*atusb
)
971 struct usb_device
*usb_dev
= atusb
->usb_dev
;
972 unsigned char *buffer
;
973 __le64 extended_addr
;
977 /* Firmware versions before 0.3 do not support the EUI64_READ command.
978 * Just use a random address and be done.
980 if (atusb
->fw_ver_maj
== 0 && atusb
->fw_ver_min
< 3) {
981 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
985 buffer
= kmalloc(IEEE802154_EXTENDED_ADDR_LEN
, GFP_KERNEL
);
989 /* Firmware is new enough so we fetch the address from EEPROM */
990 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
991 ATUSB_EUI64_READ
, ATUSB_REQ_FROM_DEV
, 0, 0,
992 buffer
, IEEE802154_EXTENDED_ADDR_LEN
, 1000);
994 dev_err(&usb_dev
->dev
, "failed to fetch extended address, random address set\n");
995 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
1000 memcpy(&extended_addr
, buffer
, IEEE802154_EXTENDED_ADDR_LEN
);
1001 /* Check if read address is not empty and the unicast bit is set correctly */
1002 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr
)) {
1003 dev_info(&usb_dev
->dev
, "no permanent extended address found, random address set\n");
1004 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
1006 atusb
->hw
->phy
->perm_extended_addr
= extended_addr
;
1007 addr
= swab64((__force u64
)atusb
->hw
->phy
->perm_extended_addr
);
1008 dev_info(&usb_dev
->dev
, "Read permanent extended address %8phC from device\n",
1016 /* ----- Setup ------------------------------------------------------------- */
1018 static int atusb_probe(struct usb_interface
*interface
,
1019 const struct usb_device_id
*id
)
1021 struct usb_device
*usb_dev
= interface_to_usbdev(interface
);
1022 struct ieee802154_hw
*hw
;
1023 struct atusb
*atusb
= NULL
;
1026 hw
= ieee802154_alloc_hw(sizeof(struct atusb
), &atusb_ops
);
1032 atusb
->usb_dev
= usb_get_dev(usb_dev
);
1033 usb_set_intfdata(interface
, atusb
);
1035 atusb
->shutdown
= 0;
1037 INIT_DELAYED_WORK(&atusb
->work
, atusb_work_urbs
);
1038 init_usb_anchor(&atusb
->idle_urbs
);
1039 init_usb_anchor(&atusb
->rx_urbs
);
1041 if (atusb_alloc_urbs(atusb
, ATUSB_NUM_RX_URBS
))
1044 atusb
->tx_dr
.bRequestType
= ATUSB_REQ_TO_DEV
;
1045 atusb
->tx_dr
.bRequest
= ATUSB_TX
;
1046 atusb
->tx_dr
.wValue
= cpu_to_le16(0);
1048 atusb
->tx_urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1052 hw
->parent
= &usb_dev
->dev
;
1054 atusb_command(atusb
, ATUSB_RF_RESET
, 0);
1055 atusb_get_and_conf_chip(atusb
);
1056 atusb_get_and_show_revision(atusb
);
1057 atusb_get_and_show_build(atusb
);
1058 atusb_set_extended_addr(atusb
);
1060 if ((atusb
->fw_ver_maj
== 0 && atusb
->fw_ver_min
>= 3) || atusb
->fw_ver_maj
> 0)
1061 hw
->flags
|= IEEE802154_HW_FRAME_RETRIES
;
1063 ret
= atusb_get_and_clear_error(atusb
);
1065 dev_err(&atusb
->usb_dev
->dev
,
1066 "%s: initialization failed, error = %d\n",
1071 ret
= ieee802154_register_hw(hw
);
1075 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1076 * explicitly. Any resets after that will send us straight to TRX_OFF,
1077 * making the command below redundant.
1079 atusb_write_reg(atusb
, RG_TRX_STATE
, STATE_FORCE_TRX_OFF
);
1080 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1083 /* Calculating the maximum time available to empty the frame buffer
1086 * According to [1], the inter-frame gap is
1087 * R * 20 * 16 us + 128 us
1088 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1089 * times (80 us at 250 kbps) of SHR of the next frame before the
1090 * transceiver begins storing data in the frame buffer.
1092 * This yields a minimum time of 208 us between the last data of a
1093 * frame and the first data of the next frame. This time is further
1094 * reduced by interrupt latency in the atusb firmware.
1096 * atusb currently needs about 500 us to retrieve a maximum-sized
1097 * frame. We therefore have to allow reception of a new frame to begin
1098 * while we retrieve the previous frame.
1100 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1101 * network", Jennic 2006.
1102 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1105 atusb_write_subreg(atusb
, SR_RX_SAFE_MODE
, 1);
1107 atusb_write_reg(atusb
, RG_IRQ_MASK
, 0xff);
1109 ret
= atusb_get_and_clear_error(atusb
);
1113 dev_err(&atusb
->usb_dev
->dev
,
1114 "%s: setup failed, error = %d\n",
1117 ieee802154_unregister_hw(hw
);
1119 atusb_free_urbs(atusb
);
1120 usb_kill_urb(atusb
->tx_urb
);
1121 usb_free_urb(atusb
->tx_urb
);
1122 usb_put_dev(usb_dev
);
1123 ieee802154_free_hw(hw
);
1127 static void atusb_disconnect(struct usb_interface
*interface
)
1129 struct atusb
*atusb
= usb_get_intfdata(interface
);
1131 dev_dbg(&atusb
->usb_dev
->dev
, "%s\n", __func__
);
1133 atusb
->shutdown
= 1;
1134 cancel_delayed_work_sync(&atusb
->work
);
1136 usb_kill_anchored_urbs(&atusb
->rx_urbs
);
1137 atusb_free_urbs(atusb
);
1138 usb_kill_urb(atusb
->tx_urb
);
1139 usb_free_urb(atusb
->tx_urb
);
1141 ieee802154_unregister_hw(atusb
->hw
);
1143 ieee802154_free_hw(atusb
->hw
);
1145 usb_set_intfdata(interface
, NULL
);
1146 usb_put_dev(atusb
->usb_dev
);
1148 pr_debug("%s done\n", __func__
);
1151 /* The devices we work with */
1152 static const struct usb_device_id atusb_device_table
[] = {
1154 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
|
1155 USB_DEVICE_ID_MATCH_INT_INFO
,
1156 .idVendor
= ATUSB_VENDOR_ID
,
1157 .idProduct
= ATUSB_PRODUCT_ID
,
1158 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
1160 /* end with null element */
1163 MODULE_DEVICE_TABLE(usb
, atusb_device_table
);
1165 static struct usb_driver atusb_driver
= {
1167 .probe
= atusb_probe
,
1168 .disconnect
= atusb_disconnect
,
1169 .id_table
= atusb_device_table
,
1171 module_usb_driver(atusb_driver
);
1173 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1174 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1175 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1176 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1177 MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1178 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1179 MODULE_LICENSE("GPL");