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 */
48 struct atusb_chip_data
;
51 struct ieee802154_hw
*hw
;
52 struct usb_device
*usb_dev
;
53 struct atusb_chip_data
*data
;
54 int shutdown
; /* non-zero if shutting down */
55 int err
; /* set by first error */
58 struct delayed_work work
; /* memory allocations */
59 struct usb_anchor idle_urbs
; /* URBs waiting to be submitted */
60 struct usb_anchor rx_urbs
; /* URBs waiting for reception */
63 struct usb_ctrlrequest tx_dr
;
65 struct sk_buff
*tx_skb
;
66 uint8_t tx_ack_seq
; /* current TX ACK sequence number */
68 /* Firmware variable */
69 unsigned char fw_ver_maj
; /* Firmware major version number */
70 unsigned char fw_ver_min
; /* Firmware minor version number */
71 unsigned char fw_hw_type
; /* Firmware hardware type */
74 struct atusb_chip_data
{
78 int (*set_channel
)(struct ieee802154_hw
*, u8
, u8
);
79 int (*set_txpower
)(struct ieee802154_hw
*, s32
);
82 /* ----- USB commands without data ----------------------------------------- */
84 /* To reduce the number of error checks in the code, we record the first error
85 * in atusb->err and reject all subsequent requests until the error is cleared.
88 static int atusb_control_msg(struct atusb
*atusb
, unsigned int pipe
,
89 __u8 request
, __u8 requesttype
,
90 __u16 value
, __u16 index
,
91 void *data
, __u16 size
, int timeout
)
93 struct usb_device
*usb_dev
= atusb
->usb_dev
;
99 ret
= usb_control_msg(usb_dev
, pipe
, request
, requesttype
,
100 value
, index
, data
, size
, timeout
);
103 dev_err(&usb_dev
->dev
,
104 "atusb_control_msg: req 0x%02x val 0x%x idx 0x%x, error %d\n",
105 request
, value
, index
, ret
);
110 static int atusb_command(struct atusb
*atusb
, uint8_t cmd
, uint8_t arg
)
112 struct usb_device
*usb_dev
= atusb
->usb_dev
;
114 dev_dbg(&usb_dev
->dev
, "atusb_command: cmd = 0x%x\n", cmd
);
115 return atusb_control_msg(atusb
, usb_sndctrlpipe(usb_dev
, 0),
116 cmd
, ATUSB_REQ_TO_DEV
, arg
, 0, NULL
, 0, 1000);
119 static int atusb_write_reg(struct atusb
*atusb
, uint8_t reg
, uint8_t value
)
121 struct usb_device
*usb_dev
= atusb
->usb_dev
;
123 dev_dbg(&usb_dev
->dev
, "atusb_write_reg: 0x%02x <- 0x%02x\n",
125 return atusb_control_msg(atusb
, usb_sndctrlpipe(usb_dev
, 0),
126 ATUSB_REG_WRITE
, ATUSB_REQ_TO_DEV
,
127 value
, reg
, NULL
, 0, 1000);
130 static int atusb_read_reg(struct atusb
*atusb
, uint8_t reg
)
132 struct usb_device
*usb_dev
= atusb
->usb_dev
;
137 buffer
= kmalloc(1, GFP_KERNEL
);
141 dev_dbg(&usb_dev
->dev
, "atusb: reg = 0x%x\n", reg
);
142 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
143 ATUSB_REG_READ
, ATUSB_REQ_FROM_DEV
,
144 0, reg
, buffer
, 1, 1000);
156 static int atusb_write_subreg(struct atusb
*atusb
, uint8_t reg
, uint8_t mask
,
157 uint8_t shift
, uint8_t value
)
159 struct usb_device
*usb_dev
= atusb
->usb_dev
;
163 dev_dbg(&usb_dev
->dev
, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
166 orig
= atusb_read_reg(atusb
, reg
);
168 /* Write the value only into that part of the register which is allowed
169 * by the mask. All other bits stay as before.
172 tmp
|= (value
<< shift
) & mask
;
175 ret
= atusb_write_reg(atusb
, reg
, tmp
);
180 static int atusb_read_subreg(struct atusb
*lp
,
181 unsigned int addr
, unsigned int mask
,
186 rc
= atusb_read_reg(lp
, addr
);
187 rc
= (rc
& mask
) >> shift
;
192 static int atusb_get_and_clear_error(struct atusb
*atusb
)
194 int err
= atusb
->err
;
200 /* ----- skb allocation ---------------------------------------------------- */
203 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
205 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
207 static void atusb_in(struct urb
*urb
);
209 static int atusb_submit_rx_urb(struct atusb
*atusb
, struct urb
*urb
)
211 struct usb_device
*usb_dev
= atusb
->usb_dev
;
212 struct sk_buff
*skb
= urb
->context
;
216 skb
= alloc_skb(MAX_RX_XFER
, GFP_KERNEL
);
218 dev_warn_ratelimited(&usb_dev
->dev
,
219 "atusb_in: can't allocate skb\n");
222 skb_put(skb
, MAX_RX_XFER
);
223 SKB_ATUSB(skb
) = atusb
;
226 usb_fill_bulk_urb(urb
, usb_dev
, usb_rcvbulkpipe(usb_dev
, 1),
227 skb
->data
, MAX_RX_XFER
, atusb_in
, skb
);
228 usb_anchor_urb(urb
, &atusb
->rx_urbs
);
230 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
232 usb_unanchor_urb(urb
);
239 static void atusb_work_urbs(struct work_struct
*work
)
241 struct atusb
*atusb
=
242 container_of(to_delayed_work(work
), struct atusb
, work
);
243 struct usb_device
*usb_dev
= atusb
->usb_dev
;
251 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
254 ret
= atusb_submit_rx_urb(atusb
, urb
);
257 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
258 dev_warn_ratelimited(&usb_dev
->dev
,
259 "atusb_in: can't allocate/submit URB (%d)\n", ret
);
260 schedule_delayed_work(&atusb
->work
,
261 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS
) + 1);
264 /* ----- Asynchronous USB -------------------------------------------------- */
266 static void atusb_tx_done(struct atusb
*atusb
, uint8_t seq
)
268 struct usb_device
*usb_dev
= atusb
->usb_dev
;
269 uint8_t expect
= atusb
->tx_ack_seq
;
271 dev_dbg(&usb_dev
->dev
, "atusb_tx_done (0x%02x/0x%02x)\n", seq
, expect
);
273 /* TODO check for ifs handling in firmware */
274 ieee802154_xmit_complete(atusb
->hw
, atusb
->tx_skb
, false);
276 /* TODO I experience this case when atusb has a tx complete
277 * irq before probing, we should fix the firmware it's an
278 * unlikely case now that seq == expect is then true, but can
279 * happen and fail with a tx_skb = NULL;
281 ieee802154_wake_queue(atusb
->hw
);
283 dev_kfree_skb_irq(atusb
->tx_skb
);
287 static void atusb_in_good(struct urb
*urb
)
289 struct usb_device
*usb_dev
= urb
->dev
;
290 struct sk_buff
*skb
= urb
->context
;
291 struct atusb
*atusb
= SKB_ATUSB(skb
);
294 if (!urb
->actual_length
) {
295 dev_dbg(&usb_dev
->dev
, "atusb_in: zero-sized URB ?\n");
301 if (urb
->actual_length
== 1) {
302 atusb_tx_done(atusb
, len
);
306 if (len
+ 1 > urb
->actual_length
- 1) {
307 dev_dbg(&usb_dev
->dev
, "atusb_in: frame len %d+1 > URB %u-1\n",
308 len
, urb
->actual_length
);
312 if (!ieee802154_is_valid_psdu_len(len
)) {
313 dev_dbg(&usb_dev
->dev
, "atusb_in: frame corrupted\n");
317 lqi
= skb
->data
[len
+ 1];
318 dev_dbg(&usb_dev
->dev
, "atusb_in: rx len %d lqi 0x%02x\n", len
, lqi
);
319 skb_pull(skb
, 1); /* remove PHR */
320 skb_trim(skb
, len
); /* get payload only */
321 ieee802154_rx_irqsafe(atusb
->hw
, skb
, lqi
);
322 urb
->context
= NULL
; /* skb is gone */
325 static void atusb_in(struct urb
*urb
)
327 struct usb_device
*usb_dev
= urb
->dev
;
328 struct sk_buff
*skb
= urb
->context
;
329 struct atusb
*atusb
= SKB_ATUSB(skb
);
331 dev_dbg(&usb_dev
->dev
, "atusb_in: status %d len %d\n",
332 urb
->status
, urb
->actual_length
);
334 if (urb
->status
== -ENOENT
) { /* being killed */
339 dev_dbg(&usb_dev
->dev
, "atusb_in: URB error %d\n", urb
->status
);
344 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
345 if (!atusb
->shutdown
)
346 schedule_delayed_work(&atusb
->work
, 0);
349 /* ----- URB allocation/deallocation --------------------------------------- */
351 static void atusb_free_urbs(struct atusb
*atusb
)
356 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
359 kfree_skb(urb
->context
);
364 static int atusb_alloc_urbs(struct atusb
*atusb
, int n
)
369 urb
= usb_alloc_urb(0, GFP_KERNEL
);
371 atusb_free_urbs(atusb
);
374 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
380 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
382 static void atusb_xmit_complete(struct urb
*urb
)
384 dev_dbg(&urb
->dev
->dev
, "atusb_xmit urb completed");
387 static int atusb_xmit(struct ieee802154_hw
*hw
, struct sk_buff
*skb
)
389 struct atusb
*atusb
= hw
->priv
;
390 struct usb_device
*usb_dev
= atusb
->usb_dev
;
393 dev_dbg(&usb_dev
->dev
, "atusb_xmit (%d)\n", skb
->len
);
396 atusb
->tx_dr
.wIndex
= cpu_to_le16(atusb
->tx_ack_seq
);
397 atusb
->tx_dr
.wLength
= cpu_to_le16(skb
->len
);
399 usb_fill_control_urb(atusb
->tx_urb
, usb_dev
,
400 usb_sndctrlpipe(usb_dev
, 0),
401 (unsigned char *)&atusb
->tx_dr
, skb
->data
,
402 skb
->len
, atusb_xmit_complete
, NULL
);
403 ret
= usb_submit_urb(atusb
->tx_urb
, GFP_ATOMIC
);
404 dev_dbg(&usb_dev
->dev
, "atusb_xmit done (%d)\n", ret
);
408 static int atusb_ed(struct ieee802154_hw
*hw
, u8
*level
)
415 static int atusb_set_hw_addr_filt(struct ieee802154_hw
*hw
,
416 struct ieee802154_hw_addr_filt
*filt
,
417 unsigned long changed
)
419 struct atusb
*atusb
= hw
->priv
;
420 struct device
*dev
= &atusb
->usb_dev
->dev
;
422 if (changed
& IEEE802154_AFILT_SADDR_CHANGED
) {
423 u16 addr
= le16_to_cpu(filt
->short_addr
);
425 dev_vdbg(dev
, "atusb_set_hw_addr_filt called for saddr\n");
426 atusb_write_reg(atusb
, RG_SHORT_ADDR_0
, addr
);
427 atusb_write_reg(atusb
, RG_SHORT_ADDR_1
, addr
>> 8);
430 if (changed
& IEEE802154_AFILT_PANID_CHANGED
) {
431 u16 pan
= le16_to_cpu(filt
->pan_id
);
433 dev_vdbg(dev
, "atusb_set_hw_addr_filt called for pan id\n");
434 atusb_write_reg(atusb
, RG_PAN_ID_0
, pan
);
435 atusb_write_reg(atusb
, RG_PAN_ID_1
, pan
>> 8);
438 if (changed
& IEEE802154_AFILT_IEEEADDR_CHANGED
) {
439 u8 i
, addr
[IEEE802154_EXTENDED_ADDR_LEN
];
441 memcpy(addr
, &filt
->ieee_addr
, IEEE802154_EXTENDED_ADDR_LEN
);
442 dev_vdbg(dev
, "atusb_set_hw_addr_filt called for IEEE addr\n");
443 for (i
= 0; i
< 8; i
++)
444 atusb_write_reg(atusb
, RG_IEEE_ADDR_0
+ i
, addr
[i
]);
447 if (changed
& IEEE802154_AFILT_PANC_CHANGED
) {
449 "atusb_set_hw_addr_filt called for panc change\n");
451 atusb_write_subreg(atusb
, SR_AACK_I_AM_COORD
, 1);
453 atusb_write_subreg(atusb
, SR_AACK_I_AM_COORD
, 0);
456 return atusb_get_and_clear_error(atusb
);
459 static int atusb_start(struct ieee802154_hw
*hw
)
461 struct atusb
*atusb
= hw
->priv
;
462 struct usb_device
*usb_dev
= atusb
->usb_dev
;
465 dev_dbg(&usb_dev
->dev
, "atusb_start\n");
466 schedule_delayed_work(&atusb
->work
, 0);
467 atusb_command(atusb
, ATUSB_RX_MODE
, 1);
468 ret
= atusb_get_and_clear_error(atusb
);
470 usb_kill_anchored_urbs(&atusb
->idle_urbs
);
474 static void atusb_stop(struct ieee802154_hw
*hw
)
476 struct atusb
*atusb
= hw
->priv
;
477 struct usb_device
*usb_dev
= atusb
->usb_dev
;
479 dev_dbg(&usb_dev
->dev
, "atusb_stop\n");
480 usb_kill_anchored_urbs(&atusb
->idle_urbs
);
481 atusb_command(atusb
, ATUSB_RX_MODE
, 0);
482 atusb_get_and_clear_error(atusb
);
485 #define ATUSB_MAX_TX_POWERS 0xF
486 static const s32 atusb_powers
[ATUSB_MAX_TX_POWERS
+ 1] = {
487 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
492 atusb_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
494 struct atusb
*atusb
= hw
->priv
;
497 return atusb
->data
->set_txpower(hw
, mbm
);
503 atusb_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
505 struct atusb
*atusb
= hw
->priv
;
508 for (i
= 0; i
< hw
->phy
->supported
.tx_powers_size
; i
++) {
509 if (hw
->phy
->supported
.tx_powers
[i
] == mbm
)
510 return atusb_write_subreg(atusb
, SR_TX_PWR_23X
, i
);
517 hulusb_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
521 for (i
= 0; i
< hw
->phy
->supported
.tx_powers_size
; i
++) {
522 if (hw
->phy
->supported
.tx_powers
[i
] == mbm
)
523 return atusb_write_subreg(hw
->priv
, SR_TX_PWR_212
, i
);
529 #define ATUSB_MAX_ED_LEVELS 0xF
530 static const s32 atusb_ed_levels
[ATUSB_MAX_ED_LEVELS
+ 1] = {
531 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
532 -7100, -6900, -6700, -6500, -6300, -6100,
535 #define AT86RF212_MAX_TX_POWERS 0x1F
536 static const s32 at86rf212_powers
[AT86RF212_MAX_TX_POWERS
+ 1] = {
537 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
538 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
539 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
542 #define AT86RF2XX_MAX_ED_LEVELS 0xF
543 static const s32 at86rf212_ed_levels_100
[AT86RF2XX_MAX_ED_LEVELS
+ 1] = {
544 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
545 -8000, -7800, -7600, -7400, -7200, -7000,
548 static const s32 at86rf212_ed_levels_98
[AT86RF2XX_MAX_ED_LEVELS
+ 1] = {
549 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
550 -7800, -7600, -7400, -7200, -7000, -6800,
554 atusb_set_cca_mode(struct ieee802154_hw
*hw
, const struct wpan_phy_cca
*cca
)
556 struct atusb
*atusb
= hw
->priv
;
559 /* mapping 802.15.4 to driver spec */
561 case NL802154_CCA_ENERGY
:
564 case NL802154_CCA_CARRIER
:
567 case NL802154_CCA_ENERGY_CARRIER
:
569 case NL802154_CCA_OPT_ENERGY_CARRIER_AND
:
572 case NL802154_CCA_OPT_ENERGY_CARRIER_OR
:
583 return atusb_write_subreg(atusb
, SR_CCA_MODE
, val
);
586 static int hulusb_set_cca_ed_level(struct atusb
*lp
, int rssi_base_val
)
588 unsigned int cca_ed_thres
;
590 cca_ed_thres
= atusb_read_subreg(lp
, SR_CCA_ED_THRES
);
592 switch (rssi_base_val
) {
594 lp
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_98
;
595 lp
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_98
);
596 lp
->hw
->phy
->cca_ed_level
= at86rf212_ed_levels_98
[cca_ed_thres
];
599 lp
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_100
;
600 lp
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_100
);
601 lp
->hw
->phy
->cca_ed_level
= at86rf212_ed_levels_100
[cca_ed_thres
];
611 atusb_set_cca_ed_level(struct ieee802154_hw
*hw
, s32 mbm
)
613 struct atusb
*atusb
= hw
->priv
;
616 for (i
= 0; i
< hw
->phy
->supported
.cca_ed_levels_size
; i
++) {
617 if (hw
->phy
->supported
.cca_ed_levels
[i
] == mbm
)
618 return atusb_write_subreg(atusb
, SR_CCA_ED_THRES
, i
);
624 static int atusb_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
626 struct atusb
*atusb
= hw
->priv
;
630 ret
= atusb
->data
->set_channel(hw
, page
, channel
);
631 /* @@@ ugly synchronization */
632 msleep(atusb
->data
->t_channel_switch
);
638 static int atusb_set_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
640 struct atusb
*atusb
= hw
->priv
;
643 ret
= atusb_write_subreg(atusb
, SR_CHANNEL
, channel
);
649 static int hulusb_set_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
654 struct atusb
*lp
= hw
->priv
;
657 rc
= atusb_write_subreg(lp
, SR_SUB_MODE
, 0);
659 rc
= atusb_write_subreg(lp
, SR_SUB_MODE
, 1);
664 rc
= atusb_write_subreg(lp
, SR_BPSK_QPSK
, 0);
665 rssi_base_val
= -100;
667 rc
= atusb_write_subreg(lp
, SR_BPSK_QPSK
, 1);
673 rc
= hulusb_set_cca_ed_level(lp
, rssi_base_val
);
677 /* This sets the symbol_duration according frequency on the 212.
678 * TODO move this handling while set channel and page in cfg802154.
679 * We can do that, this timings are according 802.15.4 standard.
680 * If we do that in cfg802154, this is a more generic calculation.
682 * This should also protected from ifs_timer. Means cancel timer and
683 * init with a new value. For now, this is okay.
687 /* SUB:0 and BPSK:0 -> BPSK-20 */
688 lp
->hw
->phy
->symbol_duration
= 50;
690 /* SUB:1 and BPSK:0 -> BPSK-40 */
691 lp
->hw
->phy
->symbol_duration
= 25;
695 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
696 lp
->hw
->phy
->symbol_duration
= 40;
698 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
699 lp
->hw
->phy
->symbol_duration
= 16;
702 lp
->hw
->phy
->lifs_period
= IEEE802154_LIFS_PERIOD
*
703 lp
->hw
->phy
->symbol_duration
;
704 lp
->hw
->phy
->sifs_period
= IEEE802154_SIFS_PERIOD
*
705 lp
->hw
->phy
->symbol_duration
;
707 return atusb_write_subreg(lp
, SR_CHANNEL
, channel
);
711 atusb_set_csma_params(struct ieee802154_hw
*hw
, u8 min_be
, u8 max_be
, u8 retries
)
713 struct atusb
*atusb
= hw
->priv
;
716 ret
= atusb_write_subreg(atusb
, SR_MIN_BE
, min_be
);
720 ret
= atusb_write_subreg(atusb
, SR_MAX_BE
, max_be
);
724 return atusb_write_subreg(atusb
, SR_MAX_CSMA_RETRIES
, retries
);
728 hulusb_set_lbt(struct ieee802154_hw
*hw
, bool on
)
730 struct atusb
*atusb
= hw
->priv
;
732 return atusb_write_subreg(atusb
, SR_CSMA_LBT_MODE
, on
);
736 atusb_set_frame_retries(struct ieee802154_hw
*hw
, s8 retries
)
738 struct atusb
*atusb
= hw
->priv
;
740 return atusb_write_subreg(atusb
, SR_MAX_FRAME_RETRIES
, retries
);
744 atusb_set_promiscuous_mode(struct ieee802154_hw
*hw
, const bool on
)
746 struct atusb
*atusb
= hw
->priv
;
750 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 1);
754 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 1);
758 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 0);
762 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 0);
770 struct atusb_chip_data atusb_chip_data
= {
771 .t_channel_switch
= 1,
772 .rssi_base_val
= -91,
773 .set_txpower
= atusb_set_txpower
,
774 .set_channel
= atusb_set_channel
,
777 struct atusb_chip_data hulusb_chip_data
= {
778 .t_channel_switch
= 11,
779 .rssi_base_val
= -100,
780 .set_txpower
= hulusb_set_txpower
,
781 .set_channel
= hulusb_set_channel
,
784 static const struct ieee802154_ops atusb_ops
= {
785 .owner
= THIS_MODULE
,
786 .xmit_async
= atusb_xmit
,
788 .set_channel
= atusb_channel
,
789 .start
= atusb_start
,
791 .set_hw_addr_filt
= atusb_set_hw_addr_filt
,
792 .set_txpower
= atusb_txpower
,
793 .set_lbt
= hulusb_set_lbt
,
794 .set_cca_mode
= atusb_set_cca_mode
,
795 .set_cca_ed_level
= atusb_set_cca_ed_level
,
796 .set_csma_params
= atusb_set_csma_params
,
797 .set_frame_retries
= atusb_set_frame_retries
,
798 .set_promiscuous_mode
= atusb_set_promiscuous_mode
,
801 /* ----- Firmware and chip version information ----------------------------- */
803 static int atusb_get_and_show_revision(struct atusb
*atusb
)
805 struct usb_device
*usb_dev
= atusb
->usb_dev
;
807 unsigned char *buffer
;
810 buffer
= kmalloc(3, GFP_KERNEL
);
814 /* Get a couple of the ATMega Firmware values */
815 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
816 ATUSB_ID
, ATUSB_REQ_FROM_DEV
, 0, 0,
819 atusb
->fw_ver_maj
= buffer
[0];
820 atusb
->fw_ver_min
= buffer
[1];
821 atusb
->fw_hw_type
= buffer
[2];
823 switch (atusb
->fw_hw_type
) {
824 case ATUSB_HW_TYPE_100813
:
825 case ATUSB_HW_TYPE_101216
:
826 case ATUSB_HW_TYPE_110131
:
828 atusb
->data
= &atusb_chip_data
;
830 case ATUSB_HW_TYPE_RZUSB
:
832 atusb
->data
= &atusb_chip_data
;
834 case ATUSB_HW_TYPE_HULUSB
:
836 atusb
->data
= &hulusb_chip_data
;
840 atusb
->err
= -ENOTSUPP
;
845 dev_info(&usb_dev
->dev
,
846 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
847 atusb
->fw_ver_maj
, atusb
->fw_ver_min
, hw_name
, atusb
->fw_hw_type
);
849 if (atusb
->fw_ver_maj
== 0 && atusb
->fw_ver_min
< 2) {
850 dev_info(&usb_dev
->dev
,
851 "Firmware version (%u.%u) predates our first public release.",
852 atusb
->fw_ver_maj
, atusb
->fw_ver_min
);
853 dev_info(&usb_dev
->dev
, "Please update to version 0.2 or newer");
860 static int atusb_get_and_show_build(struct atusb
*atusb
)
862 struct usb_device
*usb_dev
= atusb
->usb_dev
;
866 build
= kmalloc(ATUSB_BUILD_SIZE
+ 1, GFP_KERNEL
);
870 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
871 ATUSB_BUILD
, ATUSB_REQ_FROM_DEV
, 0, 0,
872 build
, ATUSB_BUILD_SIZE
, 1000);
875 dev_info(&usb_dev
->dev
, "Firmware: build %s\n", build
);
882 static int atusb_get_and_conf_chip(struct atusb
*atusb
)
884 struct usb_device
*usb_dev
= atusb
->usb_dev
;
885 uint8_t man_id_0
, man_id_1
, part_num
, version_num
;
887 struct ieee802154_hw
*hw
= atusb
->hw
;
889 man_id_0
= atusb_read_reg(atusb
, RG_MAN_ID_0
);
890 man_id_1
= atusb_read_reg(atusb
, RG_MAN_ID_1
);
891 part_num
= atusb_read_reg(atusb
, RG_PART_NUM
);
892 version_num
= atusb_read_reg(atusb
, RG_VERSION_NUM
);
897 hw
->flags
= IEEE802154_HW_TX_OMIT_CKSUM
| IEEE802154_HW_AFILT
|
898 IEEE802154_HW_PROMISCUOUS
| IEEE802154_HW_CSMA_PARAMS
;
900 hw
->phy
->flags
= WPAN_PHY_FLAG_TXPOWER
| WPAN_PHY_FLAG_CCA_ED_LEVEL
|
901 WPAN_PHY_FLAG_CCA_MODE
;
903 hw
->phy
->supported
.cca_modes
= BIT(NL802154_CCA_ENERGY
) |
904 BIT(NL802154_CCA_CARRIER
) |
905 BIT(NL802154_CCA_ENERGY_CARRIER
);
906 hw
->phy
->supported
.cca_opts
= BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND
) |
907 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR
);
909 hw
->phy
->cca
.mode
= NL802154_CCA_ENERGY
;
911 hw
->phy
->current_page
= 0;
913 if ((man_id_1
<< 8 | man_id_0
) != ATUSB_JEDEC_ATMEL
) {
914 dev_err(&usb_dev
->dev
,
915 "non-Atmel transceiver xxxx%02x%02x\n",
923 atusb
->hw
->phy
->supported
.channels
[0] = 0x7FFF800;
924 atusb
->hw
->phy
->current_channel
= 11; /* reset default */
925 atusb
->hw
->phy
->symbol_duration
= 16;
926 atusb
->hw
->phy
->supported
.tx_powers
= atusb_powers
;
927 atusb
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(atusb_powers
);
928 hw
->phy
->supported
.cca_ed_levels
= atusb_ed_levels
;
929 hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(atusb_ed_levels
);
933 atusb
->hw
->phy
->supported
.channels
[0] = 0x7FFF800;
934 atusb
->hw
->phy
->current_channel
= 11; /* reset default */
935 atusb
->hw
->phy
->symbol_duration
= 16;
936 atusb
->hw
->phy
->supported
.tx_powers
= atusb_powers
;
937 atusb
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(atusb_powers
);
938 hw
->phy
->supported
.cca_ed_levels
= atusb_ed_levels
;
939 hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(atusb_ed_levels
);
943 atusb
->hw
->flags
|= IEEE802154_HW_LBT
;
944 atusb
->hw
->phy
->supported
.channels
[0] = 0x00007FF;
945 atusb
->hw
->phy
->supported
.channels
[2] = 0x00007FF;
946 atusb
->hw
->phy
->current_channel
= 5;
947 atusb
->hw
->phy
->symbol_duration
= 25;
948 atusb
->hw
->phy
->supported
.lbt
= NL802154_SUPPORTED_BOOL_BOTH
;
949 atusb
->hw
->phy
->supported
.tx_powers
= at86rf212_powers
;
950 atusb
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(at86rf212_powers
);
951 atusb
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_100
;
952 atusb
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_100
);
955 dev_err(&usb_dev
->dev
,
956 "unexpected transceiver, part 0x%02x version 0x%02x\n",
957 part_num
, version_num
);
961 hw
->phy
->transmit_power
= hw
->phy
->supported
.tx_powers
[0];
962 hw
->phy
->cca_ed_level
= hw
->phy
->supported
.cca_ed_levels
[7];
964 dev_info(&usb_dev
->dev
, "ATUSB: %s version %d\n", chip
, version_num
);
969 atusb
->err
= -ENODEV
;
973 static int atusb_set_extended_addr(struct atusb
*atusb
)
975 struct usb_device
*usb_dev
= atusb
->usb_dev
;
976 unsigned char *buffer
;
977 __le64 extended_addr
;
981 /* Firmware versions before 0.3 do not support the EUI64_READ command.
982 * Just use a random address and be done */
983 if (atusb
->fw_ver_maj
== 0 && atusb
->fw_ver_min
< 3) {
984 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
988 buffer
= kmalloc(IEEE802154_EXTENDED_ADDR_LEN
, GFP_KERNEL
);
992 /* Firmware is new enough so we fetch the address from EEPROM */
993 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
994 ATUSB_EUI64_READ
, ATUSB_REQ_FROM_DEV
, 0, 0,
995 buffer
, IEEE802154_EXTENDED_ADDR_LEN
, 1000);
997 dev_err(&usb_dev
->dev
, "failed to fetch extended address, random address set\n");
998 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
1003 memcpy(&extended_addr
, buffer
, IEEE802154_EXTENDED_ADDR_LEN
);
1004 /* Check if read address is not empty and the unicast bit is set correctly */
1005 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr
)) {
1006 dev_info(&usb_dev
->dev
, "no permanent extended address found, random address set\n");
1007 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
1009 atusb
->hw
->phy
->perm_extended_addr
= extended_addr
;
1010 addr
= swab64((__force u64
)atusb
->hw
->phy
->perm_extended_addr
);
1011 dev_info(&usb_dev
->dev
, "Read permanent extended address %8phC from device\n",
1019 /* ----- Setup ------------------------------------------------------------- */
1021 static int atusb_probe(struct usb_interface
*interface
,
1022 const struct usb_device_id
*id
)
1024 struct usb_device
*usb_dev
= interface_to_usbdev(interface
);
1025 struct ieee802154_hw
*hw
;
1026 struct atusb
*atusb
= NULL
;
1029 hw
= ieee802154_alloc_hw(sizeof(struct atusb
), &atusb_ops
);
1035 atusb
->usb_dev
= usb_get_dev(usb_dev
);
1036 usb_set_intfdata(interface
, atusb
);
1038 atusb
->shutdown
= 0;
1040 INIT_DELAYED_WORK(&atusb
->work
, atusb_work_urbs
);
1041 init_usb_anchor(&atusb
->idle_urbs
);
1042 init_usb_anchor(&atusb
->rx_urbs
);
1044 if (atusb_alloc_urbs(atusb
, ATUSB_NUM_RX_URBS
))
1047 atusb
->tx_dr
.bRequestType
= ATUSB_REQ_TO_DEV
;
1048 atusb
->tx_dr
.bRequest
= ATUSB_TX
;
1049 atusb
->tx_dr
.wValue
= cpu_to_le16(0);
1051 atusb
->tx_urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1055 hw
->parent
= &usb_dev
->dev
;
1057 atusb_command(atusb
, ATUSB_RF_RESET
, 0);
1058 atusb_get_and_conf_chip(atusb
);
1059 atusb_get_and_show_revision(atusb
);
1060 atusb_get_and_show_build(atusb
);
1061 atusb_set_extended_addr(atusb
);
1063 if (atusb
->fw_ver_maj
>= 0 && atusb
->fw_ver_min
>= 3)
1064 hw
->flags
|= IEEE802154_HW_FRAME_RETRIES
;
1066 ret
= atusb_get_and_clear_error(atusb
);
1068 dev_err(&atusb
->usb_dev
->dev
,
1069 "%s: initialization failed, error = %d\n",
1074 ret
= ieee802154_register_hw(hw
);
1078 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1079 * explicitly. Any resets after that will send us straight to TRX_OFF,
1080 * making the command below redundant.
1082 atusb_write_reg(atusb
, RG_TRX_STATE
, STATE_FORCE_TRX_OFF
);
1083 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1086 /* Calculating the maximum time available to empty the frame buffer
1089 * According to [1], the inter-frame gap is
1090 * R * 20 * 16 us + 128 us
1091 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1092 * times (80 us at 250 kbps) of SHR of the next frame before the
1093 * transceiver begins storing data in the frame buffer.
1095 * This yields a minimum time of 208 us between the last data of a
1096 * frame and the first data of the next frame. This time is further
1097 * reduced by interrupt latency in the atusb firmware.
1099 * atusb currently needs about 500 us to retrieve a maximum-sized
1100 * frame. We therefore have to allow reception of a new frame to begin
1101 * while we retrieve the previous frame.
1103 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1104 * network", Jennic 2006.
1105 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1108 atusb_write_subreg(atusb
, SR_RX_SAFE_MODE
, 1);
1110 atusb_write_reg(atusb
, RG_IRQ_MASK
, 0xff);
1112 ret
= atusb_get_and_clear_error(atusb
);
1116 dev_err(&atusb
->usb_dev
->dev
,
1117 "%s: setup failed, error = %d\n",
1120 ieee802154_unregister_hw(hw
);
1122 atusb_free_urbs(atusb
);
1123 usb_kill_urb(atusb
->tx_urb
);
1124 usb_free_urb(atusb
->tx_urb
);
1125 usb_put_dev(usb_dev
);
1126 ieee802154_free_hw(hw
);
1130 static void atusb_disconnect(struct usb_interface
*interface
)
1132 struct atusb
*atusb
= usb_get_intfdata(interface
);
1134 dev_dbg(&atusb
->usb_dev
->dev
, "atusb_disconnect\n");
1136 atusb
->shutdown
= 1;
1137 cancel_delayed_work_sync(&atusb
->work
);
1139 usb_kill_anchored_urbs(&atusb
->rx_urbs
);
1140 atusb_free_urbs(atusb
);
1141 usb_kill_urb(atusb
->tx_urb
);
1142 usb_free_urb(atusb
->tx_urb
);
1144 ieee802154_unregister_hw(atusb
->hw
);
1146 ieee802154_free_hw(atusb
->hw
);
1148 usb_set_intfdata(interface
, NULL
);
1149 usb_put_dev(atusb
->usb_dev
);
1151 pr_debug("atusb_disconnect done\n");
1154 /* The devices we work with */
1155 static const struct usb_device_id atusb_device_table
[] = {
1157 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
|
1158 USB_DEVICE_ID_MATCH_INT_INFO
,
1159 .idVendor
= ATUSB_VENDOR_ID
,
1160 .idProduct
= ATUSB_PRODUCT_ID
,
1161 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
1163 /* end with null element */
1166 MODULE_DEVICE_TABLE(usb
, atusb_device_table
);
1168 static struct usb_driver atusb_driver
= {
1170 .probe
= atusb_probe
,
1171 .disconnect
= atusb_disconnect
,
1172 .id_table
= atusb_device_table
,
1174 module_usb_driver(atusb_driver
);
1176 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1177 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1178 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1179 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1180 MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1181 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1182 MODULE_LICENSE("GPL");