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 */
62 /* Firmware variable */
63 unsigned char fw_ver_maj
; /* Firmware major version number */
64 unsigned char fw_ver_min
; /* Firmware minor version number */
65 unsigned char fw_hw_type
; /* Firmware hardware type */
68 /* ----- USB commands without data ----------------------------------------- */
70 /* To reduce the number of error checks in the code, we record the first error
71 * in atusb->err and reject all subsequent requests until the error is cleared.
74 static int atusb_control_msg(struct atusb
*atusb
, unsigned int pipe
,
75 __u8 request
, __u8 requesttype
,
76 __u16 value
, __u16 index
,
77 void *data
, __u16 size
, int timeout
)
79 struct usb_device
*usb_dev
= atusb
->usb_dev
;
85 ret
= usb_control_msg(usb_dev
, pipe
, request
, requesttype
,
86 value
, index
, data
, size
, timeout
);
89 dev_err(&usb_dev
->dev
,
90 "atusb_control_msg: req 0x%02x val 0x%x idx 0x%x, error %d\n",
91 request
, value
, index
, ret
);
96 static int atusb_command(struct atusb
*atusb
, uint8_t cmd
, uint8_t arg
)
98 struct usb_device
*usb_dev
= atusb
->usb_dev
;
100 dev_dbg(&usb_dev
->dev
, "atusb_command: cmd = 0x%x\n", cmd
);
101 return atusb_control_msg(atusb
, usb_sndctrlpipe(usb_dev
, 0),
102 cmd
, ATUSB_REQ_TO_DEV
, arg
, 0, NULL
, 0, 1000);
105 static int atusb_write_reg(struct atusb
*atusb
, uint8_t reg
, uint8_t value
)
107 struct usb_device
*usb_dev
= atusb
->usb_dev
;
109 dev_dbg(&usb_dev
->dev
, "atusb_write_reg: 0x%02x <- 0x%02x\n",
111 return atusb_control_msg(atusb
, usb_sndctrlpipe(usb_dev
, 0),
112 ATUSB_REG_WRITE
, ATUSB_REQ_TO_DEV
,
113 value
, reg
, NULL
, 0, 1000);
116 static int atusb_read_reg(struct atusb
*atusb
, uint8_t reg
)
118 struct usb_device
*usb_dev
= atusb
->usb_dev
;
123 buffer
= kmalloc(1, GFP_KERNEL
);
127 dev_dbg(&usb_dev
->dev
, "atusb: reg = 0x%x\n", reg
);
128 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
129 ATUSB_REG_READ
, ATUSB_REQ_FROM_DEV
,
130 0, reg
, buffer
, 1, 1000);
142 static int atusb_write_subreg(struct atusb
*atusb
, uint8_t reg
, uint8_t mask
,
143 uint8_t shift
, uint8_t value
)
145 struct usb_device
*usb_dev
= atusb
->usb_dev
;
149 dev_dbg(&usb_dev
->dev
, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
152 orig
= atusb_read_reg(atusb
, reg
);
154 /* Write the value only into that part of the register which is allowed
155 * by the mask. All other bits stay as before.
158 tmp
|= (value
<< shift
) & mask
;
161 ret
= atusb_write_reg(atusb
, reg
, tmp
);
166 static int atusb_get_and_clear_error(struct atusb
*atusb
)
168 int err
= atusb
->err
;
174 /* ----- skb allocation ---------------------------------------------------- */
177 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
179 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
181 static void atusb_in(struct urb
*urb
);
183 static int atusb_submit_rx_urb(struct atusb
*atusb
, struct urb
*urb
)
185 struct usb_device
*usb_dev
= atusb
->usb_dev
;
186 struct sk_buff
*skb
= urb
->context
;
190 skb
= alloc_skb(MAX_RX_XFER
, GFP_KERNEL
);
192 dev_warn_ratelimited(&usb_dev
->dev
,
193 "atusb_in: can't allocate skb\n");
196 skb_put(skb
, MAX_RX_XFER
);
197 SKB_ATUSB(skb
) = atusb
;
200 usb_fill_bulk_urb(urb
, usb_dev
, usb_rcvbulkpipe(usb_dev
, 1),
201 skb
->data
, MAX_RX_XFER
, atusb_in
, skb
);
202 usb_anchor_urb(urb
, &atusb
->rx_urbs
);
204 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
206 usb_unanchor_urb(urb
);
213 static void atusb_work_urbs(struct work_struct
*work
)
215 struct atusb
*atusb
=
216 container_of(to_delayed_work(work
), struct atusb
, work
);
217 struct usb_device
*usb_dev
= atusb
->usb_dev
;
225 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
228 ret
= atusb_submit_rx_urb(atusb
, urb
);
231 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
232 dev_warn_ratelimited(&usb_dev
->dev
,
233 "atusb_in: can't allocate/submit URB (%d)\n", ret
);
234 schedule_delayed_work(&atusb
->work
,
235 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS
) + 1);
238 /* ----- Asynchronous USB -------------------------------------------------- */
240 static void atusb_tx_done(struct atusb
*atusb
, uint8_t seq
)
242 struct usb_device
*usb_dev
= atusb
->usb_dev
;
243 uint8_t expect
= atusb
->tx_ack_seq
;
245 dev_dbg(&usb_dev
->dev
, "atusb_tx_done (0x%02x/0x%02x)\n", seq
, expect
);
247 /* TODO check for ifs handling in firmware */
248 ieee802154_xmit_complete(atusb
->hw
, atusb
->tx_skb
, false);
250 /* TODO I experience this case when atusb has a tx complete
251 * irq before probing, we should fix the firmware it's an
252 * unlikely case now that seq == expect is then true, but can
253 * happen and fail with a tx_skb = NULL;
255 ieee802154_wake_queue(atusb
->hw
);
257 dev_kfree_skb_irq(atusb
->tx_skb
);
261 static void atusb_in_good(struct urb
*urb
)
263 struct usb_device
*usb_dev
= urb
->dev
;
264 struct sk_buff
*skb
= urb
->context
;
265 struct atusb
*atusb
= SKB_ATUSB(skb
);
268 if (!urb
->actual_length
) {
269 dev_dbg(&usb_dev
->dev
, "atusb_in: zero-sized URB ?\n");
275 if (urb
->actual_length
== 1) {
276 atusb_tx_done(atusb
, len
);
280 if (len
+ 1 > urb
->actual_length
- 1) {
281 dev_dbg(&usb_dev
->dev
, "atusb_in: frame len %d+1 > URB %u-1\n",
282 len
, urb
->actual_length
);
286 if (!ieee802154_is_valid_psdu_len(len
)) {
287 dev_dbg(&usb_dev
->dev
, "atusb_in: frame corrupted\n");
291 lqi
= skb
->data
[len
+ 1];
292 dev_dbg(&usb_dev
->dev
, "atusb_in: rx len %d lqi 0x%02x\n", len
, lqi
);
293 skb_pull(skb
, 1); /* remove PHR */
294 skb_trim(skb
, len
); /* get payload only */
295 ieee802154_rx_irqsafe(atusb
->hw
, skb
, lqi
);
296 urb
->context
= NULL
; /* skb is gone */
299 static void atusb_in(struct urb
*urb
)
301 struct usb_device
*usb_dev
= urb
->dev
;
302 struct sk_buff
*skb
= urb
->context
;
303 struct atusb
*atusb
= SKB_ATUSB(skb
);
305 dev_dbg(&usb_dev
->dev
, "atusb_in: status %d len %d\n",
306 urb
->status
, urb
->actual_length
);
308 if (urb
->status
== -ENOENT
) { /* being killed */
313 dev_dbg(&usb_dev
->dev
, "atusb_in: URB error %d\n", urb
->status
);
318 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
319 if (!atusb
->shutdown
)
320 schedule_delayed_work(&atusb
->work
, 0);
323 /* ----- URB allocation/deallocation --------------------------------------- */
325 static void atusb_free_urbs(struct atusb
*atusb
)
330 urb
= usb_get_from_anchor(&atusb
->idle_urbs
);
333 kfree_skb(urb
->context
);
338 static int atusb_alloc_urbs(struct atusb
*atusb
, int n
)
343 urb
= usb_alloc_urb(0, GFP_KERNEL
);
345 atusb_free_urbs(atusb
);
348 usb_anchor_urb(urb
, &atusb
->idle_urbs
);
354 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
356 static void atusb_xmit_complete(struct urb
*urb
)
358 dev_dbg(&urb
->dev
->dev
, "atusb_xmit urb completed");
361 static int atusb_xmit(struct ieee802154_hw
*hw
, struct sk_buff
*skb
)
363 struct atusb
*atusb
= hw
->priv
;
364 struct usb_device
*usb_dev
= atusb
->usb_dev
;
367 dev_dbg(&usb_dev
->dev
, "atusb_xmit (%d)\n", skb
->len
);
370 atusb
->tx_dr
.wIndex
= cpu_to_le16(atusb
->tx_ack_seq
);
371 atusb
->tx_dr
.wLength
= cpu_to_le16(skb
->len
);
373 usb_fill_control_urb(atusb
->tx_urb
, usb_dev
,
374 usb_sndctrlpipe(usb_dev
, 0),
375 (unsigned char *)&atusb
->tx_dr
, skb
->data
,
376 skb
->len
, atusb_xmit_complete
, NULL
);
377 ret
= usb_submit_urb(atusb
->tx_urb
, GFP_ATOMIC
);
378 dev_dbg(&usb_dev
->dev
, "atusb_xmit done (%d)\n", ret
);
382 static int atusb_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
384 struct atusb
*atusb
= hw
->priv
;
387 ret
= atusb_write_subreg(atusb
, SR_CHANNEL
, channel
);
390 msleep(1); /* @@@ ugly synchronization */
394 static int atusb_ed(struct ieee802154_hw
*hw
, u8
*level
)
401 static int atusb_set_hw_addr_filt(struct ieee802154_hw
*hw
,
402 struct ieee802154_hw_addr_filt
*filt
,
403 unsigned long changed
)
405 struct atusb
*atusb
= hw
->priv
;
406 struct device
*dev
= &atusb
->usb_dev
->dev
;
408 if (changed
& IEEE802154_AFILT_SADDR_CHANGED
) {
409 u16 addr
= le16_to_cpu(filt
->short_addr
);
411 dev_vdbg(dev
, "atusb_set_hw_addr_filt called for saddr\n");
412 atusb_write_reg(atusb
, RG_SHORT_ADDR_0
, addr
);
413 atusb_write_reg(atusb
, RG_SHORT_ADDR_1
, addr
>> 8);
416 if (changed
& IEEE802154_AFILT_PANID_CHANGED
) {
417 u16 pan
= le16_to_cpu(filt
->pan_id
);
419 dev_vdbg(dev
, "atusb_set_hw_addr_filt called for pan id\n");
420 atusb_write_reg(atusb
, RG_PAN_ID_0
, pan
);
421 atusb_write_reg(atusb
, RG_PAN_ID_1
, pan
>> 8);
424 if (changed
& IEEE802154_AFILT_IEEEADDR_CHANGED
) {
425 u8 i
, addr
[IEEE802154_EXTENDED_ADDR_LEN
];
427 memcpy(addr
, &filt
->ieee_addr
, IEEE802154_EXTENDED_ADDR_LEN
);
428 dev_vdbg(dev
, "atusb_set_hw_addr_filt called for IEEE addr\n");
429 for (i
= 0; i
< 8; i
++)
430 atusb_write_reg(atusb
, RG_IEEE_ADDR_0
+ i
, addr
[i
]);
433 if (changed
& IEEE802154_AFILT_PANC_CHANGED
) {
435 "atusb_set_hw_addr_filt called for panc change\n");
437 atusb_write_subreg(atusb
, SR_AACK_I_AM_COORD
, 1);
439 atusb_write_subreg(atusb
, SR_AACK_I_AM_COORD
, 0);
442 return atusb_get_and_clear_error(atusb
);
445 static int atusb_start(struct ieee802154_hw
*hw
)
447 struct atusb
*atusb
= hw
->priv
;
448 struct usb_device
*usb_dev
= atusb
->usb_dev
;
451 dev_dbg(&usb_dev
->dev
, "atusb_start\n");
452 schedule_delayed_work(&atusb
->work
, 0);
453 atusb_command(atusb
, ATUSB_RX_MODE
, 1);
454 ret
= atusb_get_and_clear_error(atusb
);
456 usb_kill_anchored_urbs(&atusb
->idle_urbs
);
460 static void atusb_stop(struct ieee802154_hw
*hw
)
462 struct atusb
*atusb
= hw
->priv
;
463 struct usb_device
*usb_dev
= atusb
->usb_dev
;
465 dev_dbg(&usb_dev
->dev
, "atusb_stop\n");
466 usb_kill_anchored_urbs(&atusb
->idle_urbs
);
467 atusb_command(atusb
, ATUSB_RX_MODE
, 0);
468 atusb_get_and_clear_error(atusb
);
471 #define ATUSB_MAX_TX_POWERS 0xF
472 static const s32 atusb_powers
[ATUSB_MAX_TX_POWERS
+ 1] = {
473 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
478 atusb_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
480 struct atusb
*atusb
= hw
->priv
;
483 for (i
= 0; i
< hw
->phy
->supported
.tx_powers_size
; i
++) {
484 if (hw
->phy
->supported
.tx_powers
[i
] == mbm
)
485 return atusb_write_subreg(atusb
, SR_TX_PWR_23X
, i
);
491 #define ATUSB_MAX_ED_LEVELS 0xF
492 static const s32 atusb_ed_levels
[ATUSB_MAX_ED_LEVELS
+ 1] = {
493 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
494 -7100, -6900, -6700, -6500, -6300, -6100,
498 atusb_set_cca_mode(struct ieee802154_hw
*hw
, const struct wpan_phy_cca
*cca
)
500 struct atusb
*atusb
= hw
->priv
;
503 /* mapping 802.15.4 to driver spec */
505 case NL802154_CCA_ENERGY
:
508 case NL802154_CCA_CARRIER
:
511 case NL802154_CCA_ENERGY_CARRIER
:
513 case NL802154_CCA_OPT_ENERGY_CARRIER_AND
:
516 case NL802154_CCA_OPT_ENERGY_CARRIER_OR
:
527 return atusb_write_subreg(atusb
, SR_CCA_MODE
, val
);
531 atusb_set_cca_ed_level(struct ieee802154_hw
*hw
, s32 mbm
)
533 struct atusb
*atusb
= hw
->priv
;
536 for (i
= 0; i
< hw
->phy
->supported
.cca_ed_levels_size
; i
++) {
537 if (hw
->phy
->supported
.cca_ed_levels
[i
] == mbm
)
538 return atusb_write_subreg(atusb
, SR_CCA_ED_THRES
, i
);
545 atusb_set_csma_params(struct ieee802154_hw
*hw
, u8 min_be
, u8 max_be
, u8 retries
)
547 struct atusb
*atusb
= hw
->priv
;
550 ret
= atusb_write_subreg(atusb
, SR_MIN_BE
, min_be
);
554 ret
= atusb_write_subreg(atusb
, SR_MAX_BE
, max_be
);
558 return atusb_write_subreg(atusb
, SR_MAX_CSMA_RETRIES
, retries
);
562 atusb_set_frame_retries(struct ieee802154_hw
*hw
, s8 retries
)
564 struct atusb
*atusb
= hw
->priv
;
566 return atusb_write_subreg(atusb
, SR_MAX_FRAME_RETRIES
, retries
);
570 atusb_set_promiscuous_mode(struct ieee802154_hw
*hw
, const bool on
)
572 struct atusb
*atusb
= hw
->priv
;
576 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 1);
580 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 1);
584 ret
= atusb_write_subreg(atusb
, SR_AACK_PROM_MODE
, 0);
588 ret
= atusb_write_subreg(atusb
, SR_AACK_DIS_ACK
, 0);
596 static const struct ieee802154_ops atusb_ops
= {
597 .owner
= THIS_MODULE
,
598 .xmit_async
= atusb_xmit
,
600 .set_channel
= atusb_channel
,
601 .start
= atusb_start
,
603 .set_hw_addr_filt
= atusb_set_hw_addr_filt
,
604 .set_txpower
= atusb_set_txpower
,
605 .set_cca_mode
= atusb_set_cca_mode
,
606 .set_cca_ed_level
= atusb_set_cca_ed_level
,
607 .set_csma_params
= atusb_set_csma_params
,
608 .set_frame_retries
= atusb_set_frame_retries
,
609 .set_promiscuous_mode
= atusb_set_promiscuous_mode
,
612 /* ----- Firmware and chip version information ----------------------------- */
614 static int atusb_get_and_show_revision(struct atusb
*atusb
)
616 struct usb_device
*usb_dev
= atusb
->usb_dev
;
617 unsigned char *buffer
;
620 buffer
= kmalloc(3, GFP_KERNEL
);
624 /* Get a couple of the ATMega Firmware values */
625 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
626 ATUSB_ID
, ATUSB_REQ_FROM_DEV
, 0, 0,
629 atusb
->fw_ver_maj
= buffer
[0];
630 atusb
->fw_ver_min
= buffer
[1];
631 atusb
->fw_hw_type
= buffer
[2];
633 dev_info(&usb_dev
->dev
,
634 "Firmware: major: %u, minor: %u, hardware type: %u\n",
635 atusb
->fw_ver_maj
, atusb
->fw_ver_min
, atusb
->fw_hw_type
);
637 if (atusb
->fw_ver_maj
== 0 && atusb
->fw_ver_min
< 2) {
638 dev_info(&usb_dev
->dev
,
639 "Firmware version (%u.%u) predates our first public release.",
640 atusb
->fw_ver_maj
, atusb
->fw_ver_min
);
641 dev_info(&usb_dev
->dev
, "Please update to version 0.2 or newer");
648 static int atusb_get_and_show_build(struct atusb
*atusb
)
650 struct usb_device
*usb_dev
= atusb
->usb_dev
;
654 build
= kmalloc(ATUSB_BUILD_SIZE
+ 1, GFP_KERNEL
);
658 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
659 ATUSB_BUILD
, ATUSB_REQ_FROM_DEV
, 0, 0,
660 build
, ATUSB_BUILD_SIZE
, 1000);
663 dev_info(&usb_dev
->dev
, "Firmware: build %s\n", build
);
670 static int atusb_get_and_show_chip(struct atusb
*atusb
)
672 struct usb_device
*usb_dev
= atusb
->usb_dev
;
673 uint8_t man_id_0
, man_id_1
, part_num
, version_num
;
676 man_id_0
= atusb_read_reg(atusb
, RG_MAN_ID_0
);
677 man_id_1
= atusb_read_reg(atusb
, RG_MAN_ID_1
);
678 part_num
= atusb_read_reg(atusb
, RG_PART_NUM
);
679 version_num
= atusb_read_reg(atusb
, RG_VERSION_NUM
);
684 if ((man_id_1
<< 8 | man_id_0
) != ATUSB_JEDEC_ATMEL
) {
685 dev_err(&usb_dev
->dev
,
686 "non-Atmel transceiver xxxx%02x%02x\n",
699 dev_err(&usb_dev
->dev
,
700 "unexpected transceiver, part 0x%02x version 0x%02x\n",
701 part_num
, version_num
);
705 dev_info(&usb_dev
->dev
, "ATUSB: %s version %d\n", chip
, version_num
);
710 atusb
->err
= -ENODEV
;
714 static int atusb_set_extended_addr(struct atusb
*atusb
)
716 struct usb_device
*usb_dev
= atusb
->usb_dev
;
717 unsigned char *buffer
;
718 __le64 extended_addr
;
722 /* Firmware versions before 0.3 do not support the EUI64_READ command.
723 * Just use a random address and be done */
724 if (atusb
->fw_ver_maj
== 0 && atusb
->fw_ver_min
< 3) {
725 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
729 buffer
= kmalloc(IEEE802154_EXTENDED_ADDR_LEN
, GFP_KERNEL
);
733 /* Firmware is new enough so we fetch the address from EEPROM */
734 ret
= atusb_control_msg(atusb
, usb_rcvctrlpipe(usb_dev
, 0),
735 ATUSB_EUI64_READ
, ATUSB_REQ_FROM_DEV
, 0, 0,
736 buffer
, IEEE802154_EXTENDED_ADDR_LEN
, 1000);
738 dev_err(&usb_dev
->dev
, "failed to fetch extended address, random address set\n");
739 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
744 memcpy(&extended_addr
, buffer
, IEEE802154_EXTENDED_ADDR_LEN
);
745 /* Check if read address is not empty and the unicast bit is set correctly */
746 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr
)) {
747 dev_info(&usb_dev
->dev
, "no permanent extended address found, random address set\n");
748 ieee802154_random_extended_addr(&atusb
->hw
->phy
->perm_extended_addr
);
750 atusb
->hw
->phy
->perm_extended_addr
= extended_addr
;
751 addr
= swab64((__force u64
)atusb
->hw
->phy
->perm_extended_addr
);
752 dev_info(&usb_dev
->dev
, "Read permanent extended address %8phC from device\n",
760 /* ----- Setup ------------------------------------------------------------- */
762 static int atusb_probe(struct usb_interface
*interface
,
763 const struct usb_device_id
*id
)
765 struct usb_device
*usb_dev
= interface_to_usbdev(interface
);
766 struct ieee802154_hw
*hw
;
767 struct atusb
*atusb
= NULL
;
770 hw
= ieee802154_alloc_hw(sizeof(struct atusb
), &atusb_ops
);
776 atusb
->usb_dev
= usb_get_dev(usb_dev
);
777 usb_set_intfdata(interface
, atusb
);
781 INIT_DELAYED_WORK(&atusb
->work
, atusb_work_urbs
);
782 init_usb_anchor(&atusb
->idle_urbs
);
783 init_usb_anchor(&atusb
->rx_urbs
);
785 if (atusb_alloc_urbs(atusb
, ATUSB_NUM_RX_URBS
))
788 atusb
->tx_dr
.bRequestType
= ATUSB_REQ_TO_DEV
;
789 atusb
->tx_dr
.bRequest
= ATUSB_TX
;
790 atusb
->tx_dr
.wValue
= cpu_to_le16(0);
792 atusb
->tx_urb
= usb_alloc_urb(0, GFP_ATOMIC
);
796 hw
->parent
= &usb_dev
->dev
;
797 hw
->flags
= IEEE802154_HW_TX_OMIT_CKSUM
| IEEE802154_HW_AFILT
|
798 IEEE802154_HW_PROMISCUOUS
| IEEE802154_HW_CSMA_PARAMS
;
800 hw
->phy
->flags
= WPAN_PHY_FLAG_TXPOWER
| WPAN_PHY_FLAG_CCA_ED_LEVEL
|
801 WPAN_PHY_FLAG_CCA_MODE
;
803 hw
->phy
->supported
.cca_modes
= BIT(NL802154_CCA_ENERGY
) |
804 BIT(NL802154_CCA_CARRIER
) | BIT(NL802154_CCA_ENERGY_CARRIER
);
805 hw
->phy
->supported
.cca_opts
= BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND
) |
806 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR
);
808 hw
->phy
->supported
.cca_ed_levels
= atusb_ed_levels
;
809 hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(atusb_ed_levels
);
811 hw
->phy
->cca
.mode
= NL802154_CCA_ENERGY
;
813 hw
->phy
->current_page
= 0;
814 hw
->phy
->current_channel
= 11; /* reset default */
815 hw
->phy
->supported
.channels
[0] = 0x7FFF800;
816 hw
->phy
->supported
.tx_powers
= atusb_powers
;
817 hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(atusb_powers
);
818 hw
->phy
->transmit_power
= hw
->phy
->supported
.tx_powers
[0];
819 hw
->phy
->cca_ed_level
= hw
->phy
->supported
.cca_ed_levels
[7];
821 atusb_command(atusb
, ATUSB_RF_RESET
, 0);
822 atusb_get_and_show_chip(atusb
);
823 atusb_get_and_show_revision(atusb
);
824 atusb_get_and_show_build(atusb
);
825 atusb_set_extended_addr(atusb
);
827 if (atusb
->fw_ver_maj
>= 0 && atusb
->fw_ver_min
>= 3)
828 hw
->flags
|= IEEE802154_HW_FRAME_RETRIES
;
830 ret
= atusb_get_and_clear_error(atusb
);
832 dev_err(&atusb
->usb_dev
->dev
,
833 "%s: initialization failed, error = %d\n",
838 ret
= ieee802154_register_hw(hw
);
842 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
843 * explicitly. Any resets after that will send us straight to TRX_OFF,
844 * making the command below redundant.
846 atusb_write_reg(atusb
, RG_TRX_STATE
, STATE_FORCE_TRX_OFF
);
847 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
850 /* Calculating the maximum time available to empty the frame buffer
853 * According to [1], the inter-frame gap is
854 * R * 20 * 16 us + 128 us
855 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
856 * times (80 us at 250 kbps) of SHR of the next frame before the
857 * transceiver begins storing data in the frame buffer.
859 * This yields a minimum time of 208 us between the last data of a
860 * frame and the first data of the next frame. This time is further
861 * reduced by interrupt latency in the atusb firmware.
863 * atusb currently needs about 500 us to retrieve a maximum-sized
864 * frame. We therefore have to allow reception of a new frame to begin
865 * while we retrieve the previous frame.
867 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
868 * network", Jennic 2006.
869 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
872 atusb_write_subreg(atusb
, SR_RX_SAFE_MODE
, 1);
874 atusb_write_reg(atusb
, RG_IRQ_MASK
, 0xff);
876 ret
= atusb_get_and_clear_error(atusb
);
880 dev_err(&atusb
->usb_dev
->dev
,
881 "%s: setup failed, error = %d\n",
884 ieee802154_unregister_hw(hw
);
886 atusb_free_urbs(atusb
);
887 usb_kill_urb(atusb
->tx_urb
);
888 usb_free_urb(atusb
->tx_urb
);
889 usb_put_dev(usb_dev
);
890 ieee802154_free_hw(hw
);
894 static void atusb_disconnect(struct usb_interface
*interface
)
896 struct atusb
*atusb
= usb_get_intfdata(interface
);
898 dev_dbg(&atusb
->usb_dev
->dev
, "atusb_disconnect\n");
901 cancel_delayed_work_sync(&atusb
->work
);
903 usb_kill_anchored_urbs(&atusb
->rx_urbs
);
904 atusb_free_urbs(atusb
);
905 usb_kill_urb(atusb
->tx_urb
);
906 usb_free_urb(atusb
->tx_urb
);
908 ieee802154_unregister_hw(atusb
->hw
);
910 ieee802154_free_hw(atusb
->hw
);
912 usb_set_intfdata(interface
, NULL
);
913 usb_put_dev(atusb
->usb_dev
);
915 pr_debug("atusb_disconnect done\n");
918 /* The devices we work with */
919 static const struct usb_device_id atusb_device_table
[] = {
921 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
|
922 USB_DEVICE_ID_MATCH_INT_INFO
,
923 .idVendor
= ATUSB_VENDOR_ID
,
924 .idProduct
= ATUSB_PRODUCT_ID
,
925 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
927 /* end with null element */
930 MODULE_DEVICE_TABLE(usb
, atusb_device_table
);
932 static struct usb_driver atusb_driver
= {
934 .probe
= atusb_probe
,
935 .disconnect
= atusb_disconnect
,
936 .id_table
= atusb_device_table
,
938 module_usb_driver(atusb_driver
);
940 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
941 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
942 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
943 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
944 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
945 MODULE_LICENSE("GPL");