samples/bpf: add a test for bpf_override_return
[linux/fpc-iii.git] / drivers / net / ieee802154 / atusb.c
blobd5584063049ff9237d0d88e997906249b058e2a3
1 /*
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.
13 * at86rf230.c is
14 * Copyright (C) 2009 Siemens AG
15 * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
17 * spi_atusb.c is
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"
40 #include "atusb.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 {
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 */
55 /* RX variables */
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 */
60 /* TX variables */
61 struct usb_ctrlrequest tx_dr;
62 struct urb *tx_urb;
63 struct sk_buff *tx_skb;
64 uint8_t 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 {
73 u16 t_channel_switch;
74 int rssi_base_val;
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;
92 int ret;
94 if (atusb->err)
95 return atusb->err;
97 ret = usb_control_msg(usb_dev, pipe, request, requesttype,
98 value, index, data, size, timeout);
99 if (ret < 0) {
100 atusb->err = ret;
101 dev_err(&usb_dev->dev,
102 "atusb_control_msg: req 0x%02x val 0x%x idx 0x%x, error %d\n",
103 request, value, index, ret);
105 return ret;
108 static int atusb_command(struct atusb *atusb, uint8_t cmd, uint8_t arg)
110 struct usb_device *usb_dev = atusb->usb_dev;
112 dev_dbg(&usb_dev->dev, "atusb_command: cmd = 0x%x\n", 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, uint8_t reg, uint8_t value)
119 struct usb_device *usb_dev = atusb->usb_dev;
121 dev_dbg(&usb_dev->dev, "atusb_write_reg: 0x%02x <- 0x%02x\n",
122 reg, value);
123 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
124 ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
125 value, reg, NULL, 0, 1000);
128 static int atusb_read_reg(struct atusb *atusb, uint8_t reg)
130 struct usb_device *usb_dev = atusb->usb_dev;
131 int ret;
132 uint8_t *buffer;
133 uint8_t value;
135 buffer = kmalloc(1, GFP_KERNEL);
136 if (!buffer)
137 return -ENOMEM;
139 dev_dbg(&usb_dev->dev, "atusb: reg = 0x%x\n", reg);
140 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
141 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
142 0, reg, buffer, 1, 1000);
144 if (ret >= 0) {
145 value = buffer[0];
146 kfree(buffer);
147 return value;
148 } else {
149 kfree(buffer);
150 return ret;
154 static int atusb_write_subreg(struct atusb *atusb, uint8_t reg, uint8_t mask,
155 uint8_t shift, uint8_t value)
157 struct usb_device *usb_dev = atusb->usb_dev;
158 uint8_t orig, tmp;
159 int ret = 0;
161 dev_dbg(&usb_dev->dev, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
162 reg, value);
164 orig = atusb_read_reg(atusb, reg);
166 /* Write the value only into that part of the register which is allowed
167 * by the mask. All other bits stay as before.
169 tmp = orig & ~mask;
170 tmp |= (value << shift) & mask;
172 if (tmp != orig)
173 ret = atusb_write_reg(atusb, reg, tmp);
175 return ret;
178 static int atusb_read_subreg(struct atusb *lp,
179 unsigned int addr, unsigned int mask,
180 unsigned int shift)
182 int rc;
184 rc = atusb_read_reg(lp, addr);
185 rc = (rc & mask) >> shift;
187 return rc;
190 static int atusb_get_and_clear_error(struct atusb *atusb)
192 int err = atusb->err;
194 atusb->err = 0;
195 return err;
198 /* ----- skb allocation ---------------------------------------------------- */
200 #define MAX_PSDU 127
201 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
203 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
205 static void atusb_in(struct urb *urb);
207 static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
209 struct usb_device *usb_dev = atusb->usb_dev;
210 struct sk_buff *skb = urb->context;
211 int ret;
213 if (!skb) {
214 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
215 if (!skb) {
216 dev_warn_ratelimited(&usb_dev->dev,
217 "atusb_in: can't allocate skb\n");
218 return -ENOMEM;
220 skb_put(skb, MAX_RX_XFER);
221 SKB_ATUSB(skb) = atusb;
224 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
225 skb->data, MAX_RX_XFER, atusb_in, skb);
226 usb_anchor_urb(urb, &atusb->rx_urbs);
228 ret = usb_submit_urb(urb, GFP_KERNEL);
229 if (ret) {
230 usb_unanchor_urb(urb);
231 kfree_skb(skb);
232 urb->context = NULL;
234 return ret;
237 static void atusb_work_urbs(struct work_struct *work)
239 struct atusb *atusb =
240 container_of(to_delayed_work(work), struct atusb, work);
241 struct usb_device *usb_dev = atusb->usb_dev;
242 struct urb *urb;
243 int ret;
245 if (atusb->shutdown)
246 return;
248 do {
249 urb = usb_get_from_anchor(&atusb->idle_urbs);
250 if (!urb)
251 return;
252 ret = atusb_submit_rx_urb(atusb, urb);
253 } while (!ret);
255 usb_anchor_urb(urb, &atusb->idle_urbs);
256 dev_warn_ratelimited(&usb_dev->dev,
257 "atusb_in: can't allocate/submit URB (%d)\n", ret);
258 schedule_delayed_work(&atusb->work,
259 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
262 /* ----- Asynchronous USB -------------------------------------------------- */
264 static void atusb_tx_done(struct atusb *atusb, uint8_t seq)
266 struct usb_device *usb_dev = atusb->usb_dev;
267 uint8_t expect = atusb->tx_ack_seq;
269 dev_dbg(&usb_dev->dev, "atusb_tx_done (0x%02x/0x%02x)\n", seq, expect);
270 if (seq == expect) {
271 /* TODO check for ifs handling in firmware */
272 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
273 } else {
274 /* TODO I experience this case when atusb has a tx complete
275 * irq before probing, we should fix the firmware it's an
276 * unlikely case now that seq == expect is then true, but can
277 * happen and fail with a tx_skb = NULL;
279 ieee802154_wake_queue(atusb->hw);
280 if (atusb->tx_skb)
281 dev_kfree_skb_irq(atusb->tx_skb);
285 static void atusb_in_good(struct urb *urb)
287 struct usb_device *usb_dev = urb->dev;
288 struct sk_buff *skb = urb->context;
289 struct atusb *atusb = SKB_ATUSB(skb);
290 uint8_t len, lqi;
292 if (!urb->actual_length) {
293 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
294 return;
297 len = *skb->data;
299 if (urb->actual_length == 1) {
300 atusb_tx_done(atusb, len);
301 return;
304 if (len + 1 > urb->actual_length - 1) {
305 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
306 len, urb->actual_length);
307 return;
310 if (!ieee802154_is_valid_psdu_len(len)) {
311 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
312 return;
315 lqi = skb->data[len + 1];
316 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
317 skb_pull(skb, 1); /* remove PHR */
318 skb_trim(skb, len); /* get payload only */
319 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
320 urb->context = NULL; /* skb is gone */
323 static void atusb_in(struct urb *urb)
325 struct usb_device *usb_dev = urb->dev;
326 struct sk_buff *skb = urb->context;
327 struct atusb *atusb = SKB_ATUSB(skb);
329 dev_dbg(&usb_dev->dev, "atusb_in: status %d len %d\n",
330 urb->status, urb->actual_length);
331 if (urb->status) {
332 if (urb->status == -ENOENT) { /* being killed */
333 kfree_skb(skb);
334 urb->context = NULL;
335 return;
337 dev_dbg(&usb_dev->dev, "atusb_in: URB error %d\n", urb->status);
338 } else {
339 atusb_in_good(urb);
342 usb_anchor_urb(urb, &atusb->idle_urbs);
343 if (!atusb->shutdown)
344 schedule_delayed_work(&atusb->work, 0);
347 /* ----- URB allocation/deallocation --------------------------------------- */
349 static void atusb_free_urbs(struct atusb *atusb)
351 struct urb *urb;
353 while (1) {
354 urb = usb_get_from_anchor(&atusb->idle_urbs);
355 if (!urb)
356 break;
357 kfree_skb(urb->context);
358 usb_free_urb(urb);
362 static int atusb_alloc_urbs(struct atusb *atusb, int n)
364 struct urb *urb;
366 while (n) {
367 urb = usb_alloc_urb(0, GFP_KERNEL);
368 if (!urb) {
369 atusb_free_urbs(atusb);
370 return -ENOMEM;
372 usb_anchor_urb(urb, &atusb->idle_urbs);
373 n--;
375 return 0;
378 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
380 static void atusb_xmit_complete(struct urb *urb)
382 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
385 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
387 struct atusb *atusb = hw->priv;
388 struct usb_device *usb_dev = atusb->usb_dev;
389 int ret;
391 dev_dbg(&usb_dev->dev, "atusb_xmit (%d)\n", skb->len);
392 atusb->tx_skb = skb;
393 atusb->tx_ack_seq++;
394 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
395 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
397 usb_fill_control_urb(atusb->tx_urb, usb_dev,
398 usb_sndctrlpipe(usb_dev, 0),
399 (unsigned char *)&atusb->tx_dr, skb->data,
400 skb->len, atusb_xmit_complete, NULL);
401 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
402 dev_dbg(&usb_dev->dev, "atusb_xmit done (%d)\n", ret);
403 return ret;
406 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
408 BUG_ON(!level);
409 *level = 0xbe;
410 return 0;
413 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
414 struct ieee802154_hw_addr_filt *filt,
415 unsigned long changed)
417 struct atusb *atusb = hw->priv;
418 struct device *dev = &atusb->usb_dev->dev;
420 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
421 u16 addr = le16_to_cpu(filt->short_addr);
423 dev_vdbg(dev, "atusb_set_hw_addr_filt called for saddr\n");
424 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
425 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
428 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
429 u16 pan = le16_to_cpu(filt->pan_id);
431 dev_vdbg(dev, "atusb_set_hw_addr_filt called for pan id\n");
432 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
433 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
436 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
437 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
439 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
440 dev_vdbg(dev, "atusb_set_hw_addr_filt called for IEEE addr\n");
441 for (i = 0; i < 8; i++)
442 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
445 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
446 dev_vdbg(dev,
447 "atusb_set_hw_addr_filt called for panc change\n");
448 if (filt->pan_coord)
449 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
450 else
451 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
454 return atusb_get_and_clear_error(atusb);
457 static int atusb_start(struct ieee802154_hw *hw)
459 struct atusb *atusb = hw->priv;
460 struct usb_device *usb_dev = atusb->usb_dev;
461 int ret;
463 dev_dbg(&usb_dev->dev, "atusb_start\n");
464 schedule_delayed_work(&atusb->work, 0);
465 atusb_command(atusb, ATUSB_RX_MODE, 1);
466 ret = atusb_get_and_clear_error(atusb);
467 if (ret < 0)
468 usb_kill_anchored_urbs(&atusb->idle_urbs);
469 return ret;
472 static void atusb_stop(struct ieee802154_hw *hw)
474 struct atusb *atusb = hw->priv;
475 struct usb_device *usb_dev = atusb->usb_dev;
477 dev_dbg(&usb_dev->dev, "atusb_stop\n");
478 usb_kill_anchored_urbs(&atusb->idle_urbs);
479 atusb_command(atusb, ATUSB_RX_MODE, 0);
480 atusb_get_and_clear_error(atusb);
483 #define ATUSB_MAX_TX_POWERS 0xF
484 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
485 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
486 -900, -1200, -1700,
489 static int
490 atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
492 struct atusb *atusb = hw->priv;
494 if (atusb->data)
495 return atusb->data->set_txpower(hw, mbm);
496 else
497 return -ENOTSUPP;
500 static int
501 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
503 struct atusb *atusb = hw->priv;
504 u32 i;
506 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
507 if (hw->phy->supported.tx_powers[i] == mbm)
508 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
511 return -EINVAL;
514 static int
515 hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
517 u32 i;
519 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
520 if (hw->phy->supported.tx_powers[i] == mbm)
521 return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
524 return -EINVAL;
527 #define ATUSB_MAX_ED_LEVELS 0xF
528 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
529 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
530 -7100, -6900, -6700, -6500, -6300, -6100,
533 #define AT86RF212_MAX_TX_POWERS 0x1F
534 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
535 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
536 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
537 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
540 #define AT86RF2XX_MAX_ED_LEVELS 0xF
541 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
542 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
543 -8000, -7800, -7600, -7400, -7200, -7000,
546 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
547 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
548 -7800, -7600, -7400, -7200, -7000, -6800,
551 static int
552 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
554 struct atusb *atusb = hw->priv;
555 u8 val;
557 /* mapping 802.15.4 to driver spec */
558 switch (cca->mode) {
559 case NL802154_CCA_ENERGY:
560 val = 1;
561 break;
562 case NL802154_CCA_CARRIER:
563 val = 2;
564 break;
565 case NL802154_CCA_ENERGY_CARRIER:
566 switch (cca->opt) {
567 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
568 val = 3;
569 break;
570 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
571 val = 0;
572 break;
573 default:
574 return -EINVAL;
576 break;
577 default:
578 return -EINVAL;
581 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
584 static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
586 unsigned int cca_ed_thres;
588 cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
590 switch (rssi_base_val) {
591 case -98:
592 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
593 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
594 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
595 break;
596 case -100:
597 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
598 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
599 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
600 break;
601 default:
602 WARN_ON(1);
605 return 0;
608 static int
609 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
611 struct atusb *atusb = hw->priv;
612 u32 i;
614 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
615 if (hw->phy->supported.cca_ed_levels[i] == mbm)
616 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
619 return -EINVAL;
622 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
624 struct atusb *atusb = hw->priv;
625 int ret = -ENOTSUPP;
627 if (atusb->data) {
628 ret = atusb->data->set_channel(hw, page, channel);
629 /* @@@ ugly synchronization */
630 msleep(atusb->data->t_channel_switch);
633 return ret;
636 static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
638 struct atusb *atusb = hw->priv;
639 int ret;
641 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
642 if (ret < 0)
643 return ret;
644 return 0;
647 static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
649 int rc;
650 int rssi_base_val;
652 struct atusb *lp = hw->priv;
654 if (channel == 0)
655 rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
656 else
657 rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
658 if (rc < 0)
659 return rc;
661 if (page == 0) {
662 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
663 rssi_base_val = -100;
664 } else {
665 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
666 rssi_base_val = -98;
668 if (rc < 0)
669 return rc;
671 rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
672 if (rc < 0)
673 return rc;
675 /* This sets the symbol_duration according frequency on the 212.
676 * TODO move this handling while set channel and page in cfg802154.
677 * We can do that, this timings are according 802.15.4 standard.
678 * If we do that in cfg802154, this is a more generic calculation.
680 * This should also protected from ifs_timer. Means cancel timer and
681 * init with a new value. For now, this is okay.
683 if (channel == 0) {
684 if (page == 0) {
685 /* SUB:0 and BPSK:0 -> BPSK-20 */
686 lp->hw->phy->symbol_duration = 50;
687 } else {
688 /* SUB:1 and BPSK:0 -> BPSK-40 */
689 lp->hw->phy->symbol_duration = 25;
691 } else {
692 if (page == 0)
693 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
694 lp->hw->phy->symbol_duration = 40;
695 else
696 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
697 lp->hw->phy->symbol_duration = 16;
700 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
701 lp->hw->phy->symbol_duration;
702 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
703 lp->hw->phy->symbol_duration;
705 return atusb_write_subreg(lp, SR_CHANNEL, channel);
708 static int
709 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
711 struct atusb *atusb = hw->priv;
712 int ret;
714 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
715 if (ret)
716 return ret;
718 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
719 if (ret)
720 return ret;
722 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
725 static int
726 hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
728 struct atusb *atusb = hw->priv;
730 return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
733 static int
734 atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
736 struct atusb *atusb = hw->priv;
738 return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
741 static int
742 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
744 struct atusb *atusb = hw->priv;
745 int ret;
747 if (on) {
748 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
749 if (ret < 0)
750 return ret;
752 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
753 if (ret < 0)
754 return ret;
755 } else {
756 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
757 if (ret < 0)
758 return ret;
760 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
761 if (ret < 0)
762 return ret;
765 return 0;
768 static struct atusb_chip_data atusb_chip_data = {
769 .t_channel_switch = 1,
770 .rssi_base_val = -91,
771 .set_txpower = atusb_set_txpower,
772 .set_channel = atusb_set_channel,
775 static struct atusb_chip_data hulusb_chip_data = {
776 .t_channel_switch = 11,
777 .rssi_base_val = -100,
778 .set_txpower = hulusb_set_txpower,
779 .set_channel = hulusb_set_channel,
782 static const struct ieee802154_ops atusb_ops = {
783 .owner = THIS_MODULE,
784 .xmit_async = atusb_xmit,
785 .ed = atusb_ed,
786 .set_channel = atusb_channel,
787 .start = atusb_start,
788 .stop = atusb_stop,
789 .set_hw_addr_filt = atusb_set_hw_addr_filt,
790 .set_txpower = atusb_txpower,
791 .set_lbt = hulusb_set_lbt,
792 .set_cca_mode = atusb_set_cca_mode,
793 .set_cca_ed_level = atusb_set_cca_ed_level,
794 .set_csma_params = atusb_set_csma_params,
795 .set_frame_retries = atusb_set_frame_retries,
796 .set_promiscuous_mode = atusb_set_promiscuous_mode,
799 /* ----- Firmware and chip version information ----------------------------- */
801 static int atusb_get_and_show_revision(struct atusb *atusb)
803 struct usb_device *usb_dev = atusb->usb_dev;
804 char *hw_name;
805 unsigned char *buffer;
806 int ret;
808 buffer = kmalloc(3, GFP_KERNEL);
809 if (!buffer)
810 return -ENOMEM;
812 /* Get a couple of the ATMega Firmware values */
813 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
814 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
815 buffer, 3, 1000);
816 if (ret >= 0) {
817 atusb->fw_ver_maj = buffer[0];
818 atusb->fw_ver_min = buffer[1];
819 atusb->fw_hw_type = buffer[2];
821 switch (atusb->fw_hw_type) {
822 case ATUSB_HW_TYPE_100813:
823 case ATUSB_HW_TYPE_101216:
824 case ATUSB_HW_TYPE_110131:
825 hw_name = "ATUSB";
826 atusb->data = &atusb_chip_data;
827 break;
828 case ATUSB_HW_TYPE_RZUSB:
829 hw_name = "RZUSB";
830 atusb->data = &atusb_chip_data;
831 break;
832 case ATUSB_HW_TYPE_HULUSB:
833 hw_name = "HULUSB";
834 atusb->data = &hulusb_chip_data;
835 break;
836 default:
837 hw_name = "UNKNOWN";
838 atusb->err = -ENOTSUPP;
839 ret = -ENOTSUPP;
840 break;
843 dev_info(&usb_dev->dev,
844 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
845 atusb->fw_ver_maj, atusb->fw_ver_min, hw_name, atusb->fw_hw_type);
847 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
848 dev_info(&usb_dev->dev,
849 "Firmware version (%u.%u) predates our first public release.",
850 atusb->fw_ver_maj, atusb->fw_ver_min);
851 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
854 kfree(buffer);
855 return ret;
858 static int atusb_get_and_show_build(struct atusb *atusb)
860 struct usb_device *usb_dev = atusb->usb_dev;
861 char *build;
862 int ret;
864 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
865 if (!build)
866 return -ENOMEM;
868 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
869 ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
870 build, ATUSB_BUILD_SIZE, 1000);
871 if (ret >= 0) {
872 build[ret] = 0;
873 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
876 kfree(build);
877 return ret;
880 static int atusb_get_and_conf_chip(struct atusb *atusb)
882 struct usb_device *usb_dev = atusb->usb_dev;
883 uint8_t man_id_0, man_id_1, part_num, version_num;
884 const char *chip;
885 struct ieee802154_hw *hw = atusb->hw;
887 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
888 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
889 part_num = atusb_read_reg(atusb, RG_PART_NUM);
890 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
892 if (atusb->err)
893 return atusb->err;
895 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
896 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
898 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
899 WPAN_PHY_FLAG_CCA_MODE;
901 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
902 BIT(NL802154_CCA_CARRIER) |
903 BIT(NL802154_CCA_ENERGY_CARRIER);
904 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
905 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
907 hw->phy->cca.mode = NL802154_CCA_ENERGY;
909 hw->phy->current_page = 0;
911 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
912 dev_err(&usb_dev->dev,
913 "non-Atmel transceiver xxxx%02x%02x\n",
914 man_id_1, man_id_0);
915 goto fail;
918 switch (part_num) {
919 case 2:
920 chip = "AT86RF230";
921 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
922 atusb->hw->phy->current_channel = 11; /* reset default */
923 atusb->hw->phy->symbol_duration = 16;
924 atusb->hw->phy->supported.tx_powers = atusb_powers;
925 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
926 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
927 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
928 break;
929 case 3:
930 chip = "AT86RF231";
931 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
932 atusb->hw->phy->current_channel = 11; /* reset default */
933 atusb->hw->phy->symbol_duration = 16;
934 atusb->hw->phy->supported.tx_powers = atusb_powers;
935 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
936 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
937 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
938 break;
939 case 7:
940 chip = "AT86RF212";
941 atusb->hw->flags |= IEEE802154_HW_LBT;
942 atusb->hw->phy->supported.channels[0] = 0x00007FF;
943 atusb->hw->phy->supported.channels[2] = 0x00007FF;
944 atusb->hw->phy->current_channel = 5;
945 atusb->hw->phy->symbol_duration = 25;
946 atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
947 atusb->hw->phy->supported.tx_powers = at86rf212_powers;
948 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
949 atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
950 atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
951 break;
952 default:
953 dev_err(&usb_dev->dev,
954 "unexpected transceiver, part 0x%02x version 0x%02x\n",
955 part_num, version_num);
956 goto fail;
959 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
960 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
962 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
964 return 0;
966 fail:
967 atusb->err = -ENODEV;
968 return -ENODEV;
971 static int atusb_set_extended_addr(struct atusb *atusb)
973 struct usb_device *usb_dev = atusb->usb_dev;
974 unsigned char *buffer;
975 __le64 extended_addr;
976 u64 addr;
977 int ret;
979 /* Firmware versions before 0.3 do not support the EUI64_READ command.
980 * Just use a random address and be done */
981 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
982 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
983 return 0;
986 buffer = kmalloc(IEEE802154_EXTENDED_ADDR_LEN, GFP_KERNEL);
987 if (!buffer)
988 return -ENOMEM;
990 /* Firmware is new enough so we fetch the address from EEPROM */
991 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
992 ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
993 buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000);
994 if (ret < 0) {
995 dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
996 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
997 kfree(buffer);
998 return ret;
1001 memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
1002 /* Check if read address is not empty and the unicast bit is set correctly */
1003 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
1004 dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
1005 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
1006 } else {
1007 atusb->hw->phy->perm_extended_addr = extended_addr;
1008 addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
1009 dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
1010 &addr);
1013 kfree(buffer);
1014 return ret;
1017 /* ----- Setup ------------------------------------------------------------- */
1019 static int atusb_probe(struct usb_interface *interface,
1020 const struct usb_device_id *id)
1022 struct usb_device *usb_dev = interface_to_usbdev(interface);
1023 struct ieee802154_hw *hw;
1024 struct atusb *atusb = NULL;
1025 int ret = -ENOMEM;
1027 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
1028 if (!hw)
1029 return -ENOMEM;
1031 atusb = hw->priv;
1032 atusb->hw = hw;
1033 atusb->usb_dev = usb_get_dev(usb_dev);
1034 usb_set_intfdata(interface, atusb);
1036 atusb->shutdown = 0;
1037 atusb->err = 0;
1038 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
1039 init_usb_anchor(&atusb->idle_urbs);
1040 init_usb_anchor(&atusb->rx_urbs);
1042 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
1043 goto fail;
1045 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
1046 atusb->tx_dr.bRequest = ATUSB_TX;
1047 atusb->tx_dr.wValue = cpu_to_le16(0);
1049 atusb->tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
1050 if (!atusb->tx_urb)
1051 goto fail;
1053 hw->parent = &usb_dev->dev;
1055 atusb_command(atusb, ATUSB_RF_RESET, 0);
1056 atusb_get_and_conf_chip(atusb);
1057 atusb_get_and_show_revision(atusb);
1058 atusb_get_and_show_build(atusb);
1059 atusb_set_extended_addr(atusb);
1061 if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
1062 hw->flags |= IEEE802154_HW_FRAME_RETRIES;
1064 ret = atusb_get_and_clear_error(atusb);
1065 if (ret) {
1066 dev_err(&atusb->usb_dev->dev,
1067 "%s: initialization failed, error = %d\n",
1068 __func__, ret);
1069 goto fail;
1072 ret = ieee802154_register_hw(hw);
1073 if (ret)
1074 goto fail;
1076 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1077 * explicitly. Any resets after that will send us straight to TRX_OFF,
1078 * making the command below redundant.
1080 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
1081 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1083 #if 0
1084 /* Calculating the maximum time available to empty the frame buffer
1085 * on reception:
1087 * According to [1], the inter-frame gap is
1088 * R * 20 * 16 us + 128 us
1089 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1090 * times (80 us at 250 kbps) of SHR of the next frame before the
1091 * transceiver begins storing data in the frame buffer.
1093 * This yields a minimum time of 208 us between the last data of a
1094 * frame and the first data of the next frame. This time is further
1095 * reduced by interrupt latency in the atusb firmware.
1097 * atusb currently needs about 500 us to retrieve a maximum-sized
1098 * frame. We therefore have to allow reception of a new frame to begin
1099 * while we retrieve the previous frame.
1101 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1102 * network", Jennic 2006.
1103 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1106 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1107 #endif
1108 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
1110 ret = atusb_get_and_clear_error(atusb);
1111 if (!ret)
1112 return 0;
1114 dev_err(&atusb->usb_dev->dev,
1115 "%s: setup failed, error = %d\n",
1116 __func__, ret);
1118 ieee802154_unregister_hw(hw);
1119 fail:
1120 atusb_free_urbs(atusb);
1121 usb_kill_urb(atusb->tx_urb);
1122 usb_free_urb(atusb->tx_urb);
1123 usb_put_dev(usb_dev);
1124 ieee802154_free_hw(hw);
1125 return ret;
1128 static void atusb_disconnect(struct usb_interface *interface)
1130 struct atusb *atusb = usb_get_intfdata(interface);
1132 dev_dbg(&atusb->usb_dev->dev, "atusb_disconnect\n");
1134 atusb->shutdown = 1;
1135 cancel_delayed_work_sync(&atusb->work);
1137 usb_kill_anchored_urbs(&atusb->rx_urbs);
1138 atusb_free_urbs(atusb);
1139 usb_kill_urb(atusb->tx_urb);
1140 usb_free_urb(atusb->tx_urb);
1142 ieee802154_unregister_hw(atusb->hw);
1144 ieee802154_free_hw(atusb->hw);
1146 usb_set_intfdata(interface, NULL);
1147 usb_put_dev(atusb->usb_dev);
1149 pr_debug("atusb_disconnect done\n");
1152 /* The devices we work with */
1153 static const struct usb_device_id atusb_device_table[] = {
1155 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1156 USB_DEVICE_ID_MATCH_INT_INFO,
1157 .idVendor = ATUSB_VENDOR_ID,
1158 .idProduct = ATUSB_PRODUCT_ID,
1159 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
1161 /* end with null element */
1164 MODULE_DEVICE_TABLE(usb, atusb_device_table);
1166 static struct usb_driver atusb_driver = {
1167 .name = "atusb",
1168 .probe = atusb_probe,
1169 .disconnect = atusb_disconnect,
1170 .id_table = atusb_device_table,
1172 module_usb_driver(atusb_driver);
1174 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1175 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1176 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1177 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1178 MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1179 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1180 MODULE_LICENSE("GPL");