Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
4 *
5 * Written 2013 by Werner Almesberger <werner@almesberger.net>
6 *
7 * Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org>
8 *
9 * Based on at86rf230.c and spi_atusb.c.
10 * at86rf230.c is
11 * Copyright (C) 2009 Siemens AG
12 * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
13 *
14 * spi_atusb.c is
15 * Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com>
16 * Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org>
17 * Copyright (c) 2011 Werner Almesberger <werner@almesberger.net>
18 *
19 * USB initialization is
20 * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com>
21 *
22 * Busware HUL support is
23 * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at>
24 */
25
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>
32
33#include <net/cfg802154.h>
34#include <net/mac802154.h>
35
36#include "at86rf230.h"
37#include "atusb.h"
38
39#define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */
40
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 */
44
45struct atusb {
46 struct ieee802154_hw *hw;
47 struct usb_device *usb_dev;
48 struct atusb_chip_data *data;
49 int shutdown; /* non-zero if shutting down */
50 int err; /* set by first error */
51
52 /* RX variables */
53 struct delayed_work work; /* memory allocations */
54 struct usb_anchor idle_urbs; /* URBs waiting to be submitted */
55 struct usb_anchor rx_urbs; /* URBs waiting for reception */
56
57 /* TX variables */
58 struct usb_ctrlrequest tx_dr;
59 struct urb *tx_urb;
60 struct sk_buff *tx_skb;
61 u8 tx_ack_seq; /* current TX ACK sequence number */
62
63 /* Firmware variable */
64 unsigned char fw_ver_maj; /* Firmware major version number */
65 unsigned char fw_ver_min; /* Firmware minor version number */
66 unsigned char fw_hw_type; /* Firmware hardware type */
67};
68
69struct atusb_chip_data {
70 u16 t_channel_switch;
71 int rssi_base_val;
72
73 int (*set_channel)(struct ieee802154_hw*, u8, u8);
74 int (*set_txpower)(struct ieee802154_hw*, s32);
75};
76
77static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
78 u8 shift, u8 value)
79{
80 struct usb_device *usb_dev = atusb->usb_dev;
81 u8 orig, tmp;
82 int ret = 0;
83
84 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
85
86 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
87 0, reg, &orig, 1, 1000, GFP_KERNEL);
88 if (ret < 0)
89 return ret;
90
91 /* Write the value only into that part of the register which is allowed
92 * by the mask. All other bits stay as before.
93 */
94 tmp = orig & ~mask;
95 tmp |= (value << shift) & mask;
96
97 if (tmp != orig)
98 ret = usb_control_msg_send(usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
99 tmp, reg, NULL, 0, 1000, GFP_KERNEL);
100
101 return ret;
102}
103
104static int atusb_read_subreg(struct atusb *lp,
105 unsigned int addr, unsigned int mask,
106 unsigned int shift)
107{
108 int reg, ret;
109
110 ret = usb_control_msg_recv(lp->usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
111 0, addr, ®, 1, 1000, GFP_KERNEL);
112 if (ret < 0)
113 return ret;
114
115 reg = (reg & mask) >> shift;
116
117 return reg;
118}
119
120static int atusb_get_and_clear_error(struct atusb *atusb)
121{
122 int err = atusb->err;
123
124 atusb->err = 0;
125 return err;
126}
127
128/* ----- skb allocation ---------------------------------------------------- */
129
130#define MAX_PSDU 127
131#define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
132
133#define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
134
135static void atusb_in(struct urb *urb);
136
137static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
138{
139 struct usb_device *usb_dev = atusb->usb_dev;
140 struct sk_buff *skb = urb->context;
141 int ret;
142
143 if (!skb) {
144 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
145 if (!skb) {
146 dev_warn_ratelimited(&usb_dev->dev,
147 "atusb_in: can't allocate skb\n");
148 return -ENOMEM;
149 }
150 skb_put(skb, MAX_RX_XFER);
151 SKB_ATUSB(skb) = atusb;
152 }
153
154 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
155 skb->data, MAX_RX_XFER, atusb_in, skb);
156 usb_anchor_urb(urb, &atusb->rx_urbs);
157
158 ret = usb_submit_urb(urb, GFP_KERNEL);
159 if (ret) {
160 usb_unanchor_urb(urb);
161 kfree_skb(skb);
162 urb->context = NULL;
163 }
164 return ret;
165}
166
167static void atusb_work_urbs(struct work_struct *work)
168{
169 struct atusb *atusb =
170 container_of(to_delayed_work(work), struct atusb, work);
171 struct usb_device *usb_dev = atusb->usb_dev;
172 struct urb *urb;
173 int ret;
174
175 if (atusb->shutdown)
176 return;
177
178 do {
179 urb = usb_get_from_anchor(&atusb->idle_urbs);
180 if (!urb)
181 return;
182 ret = atusb_submit_rx_urb(atusb, urb);
183 } while (!ret);
184
185 usb_anchor_urb(urb, &atusb->idle_urbs);
186 dev_warn_ratelimited(&usb_dev->dev,
187 "atusb_in: can't allocate/submit URB (%d)\n", ret);
188 schedule_delayed_work(&atusb->work,
189 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
190}
191
192/* ----- Asynchronous USB -------------------------------------------------- */
193
194static void atusb_tx_done(struct atusb *atusb, u8 seq, int reason)
195{
196 struct usb_device *usb_dev = atusb->usb_dev;
197 u8 expect = atusb->tx_ack_seq;
198
199 dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
200 if (seq == expect) {
201 /* TODO check for ifs handling in firmware */
202 if (reason == IEEE802154_SUCCESS)
203 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
204 else
205 ieee802154_xmit_error(atusb->hw, atusb->tx_skb, reason);
206 } else {
207 /* TODO I experience this case when atusb has a tx complete
208 * irq before probing, we should fix the firmware it's an
209 * unlikely case now that seq == expect is then true, but can
210 * happen and fail with a tx_skb = NULL;
211 */
212 ieee802154_xmit_hw_error(atusb->hw, atusb->tx_skb);
213 }
214}
215
216static void atusb_in_good(struct urb *urb)
217{
218 struct usb_device *usb_dev = urb->dev;
219 struct sk_buff *skb = urb->context;
220 struct atusb *atusb = SKB_ATUSB(skb);
221 int result = IEEE802154_SUCCESS;
222 u8 len, lqi, trac;
223
224 if (!urb->actual_length) {
225 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
226 return;
227 }
228
229 len = *skb->data;
230
231 switch (urb->actual_length) {
232 case 2:
233 trac = TRAC_MASK(*(skb->data + 1));
234 switch (trac) {
235 case TRAC_SUCCESS:
236 case TRAC_SUCCESS_DATA_PENDING:
237 /* already IEEE802154_SUCCESS */
238 break;
239 case TRAC_CHANNEL_ACCESS_FAILURE:
240 result = IEEE802154_CHANNEL_ACCESS_FAILURE;
241 break;
242 case TRAC_NO_ACK:
243 result = IEEE802154_NO_ACK;
244 break;
245 default:
246 result = IEEE802154_SYSTEM_ERROR;
247 }
248
249 fallthrough;
250 case 1:
251 atusb_tx_done(atusb, len, result);
252 return;
253 }
254
255 if (len + 1 > urb->actual_length - 1) {
256 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
257 len, urb->actual_length);
258 return;
259 }
260
261 if (!ieee802154_is_valid_psdu_len(len)) {
262 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
263 return;
264 }
265
266 lqi = skb->data[len + 1];
267 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
268 skb_pull(skb, 1); /* remove PHR */
269 skb_trim(skb, len); /* get payload only */
270 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
271 urb->context = NULL; /* skb is gone */
272}
273
274static void atusb_in(struct urb *urb)
275{
276 struct usb_device *usb_dev = urb->dev;
277 struct sk_buff *skb = urb->context;
278 struct atusb *atusb = SKB_ATUSB(skb);
279
280 dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
281 urb->status, urb->actual_length);
282 if (urb->status) {
283 if (urb->status == -ENOENT) { /* being killed */
284 kfree_skb(skb);
285 urb->context = NULL;
286 return;
287 }
288 dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
289 } else {
290 atusb_in_good(urb);
291 }
292
293 usb_anchor_urb(urb, &atusb->idle_urbs);
294 if (!atusb->shutdown)
295 schedule_delayed_work(&atusb->work, 0);
296}
297
298/* ----- URB allocation/deallocation --------------------------------------- */
299
300static void atusb_free_urbs(struct atusb *atusb)
301{
302 struct urb *urb;
303
304 while (1) {
305 urb = usb_get_from_anchor(&atusb->idle_urbs);
306 if (!urb)
307 break;
308 kfree_skb(urb->context);
309 usb_free_urb(urb);
310 }
311}
312
313static int atusb_alloc_urbs(struct atusb *atusb, int n)
314{
315 struct urb *urb;
316
317 while (n) {
318 urb = usb_alloc_urb(0, GFP_KERNEL);
319 if (!urb) {
320 atusb_free_urbs(atusb);
321 return -ENOMEM;
322 }
323 usb_anchor_urb(urb, &atusb->idle_urbs);
324 usb_free_urb(urb);
325 n--;
326 }
327 return 0;
328}
329
330/* ----- IEEE 802.15.4 interface operations -------------------------------- */
331
332static void atusb_xmit_complete(struct urb *urb)
333{
334 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
335}
336
337static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
338{
339 struct atusb *atusb = hw->priv;
340 struct usb_device *usb_dev = atusb->usb_dev;
341 int ret;
342
343 dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
344 atusb->tx_skb = skb;
345 atusb->tx_ack_seq++;
346 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
347 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
348
349 usb_fill_control_urb(atusb->tx_urb, usb_dev,
350 usb_sndctrlpipe(usb_dev, 0),
351 (unsigned char *)&atusb->tx_dr, skb->data,
352 skb->len, atusb_xmit_complete, NULL);
353 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
354 dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
355 return ret;
356}
357
358static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
359{
360 WARN_ON(!level);
361 *level = 0xbe;
362 return 0;
363}
364
365static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
366 struct ieee802154_hw_addr_filt *filt,
367 unsigned long changed)
368{
369 struct atusb *atusb = hw->priv;
370 struct device *dev = &atusb->usb_dev->dev;
371
372 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
373 u16 addr = le16_to_cpu(filt->short_addr);
374
375 dev_vdbg(dev, "%s called for saddr\n", __func__);
376 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
377 addr, RG_SHORT_ADDR_0, NULL, 0, 1000, GFP_KERNEL);
378
379 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
380 addr >> 8, RG_SHORT_ADDR_1, NULL, 0, 1000, GFP_KERNEL);
381 }
382
383 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
384 u16 pan = le16_to_cpu(filt->pan_id);
385
386 dev_vdbg(dev, "%s called for pan id\n", __func__);
387 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
388 pan, RG_PAN_ID_0, NULL, 0, 1000, GFP_KERNEL);
389
390 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
391 pan >> 8, RG_PAN_ID_1, NULL, 0, 1000, GFP_KERNEL);
392 }
393
394 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
395 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
396
397 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
398 dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
399 for (i = 0; i < 8; i++)
400 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
401 addr[i], RG_IEEE_ADDR_0 + i, NULL, 0,
402 1000, GFP_KERNEL);
403 }
404
405 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
406 dev_vdbg(dev, "%s called for panc change\n", __func__);
407 if (filt->pan_coord)
408 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
409 else
410 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
411 }
412
413 return atusb_get_and_clear_error(atusb);
414}
415
416static int atusb_start(struct ieee802154_hw *hw)
417{
418 struct atusb *atusb = hw->priv;
419 struct usb_device *usb_dev = atusb->usb_dev;
420 int ret;
421
422 dev_dbg(&usb_dev->dev, "%s\n", __func__);
423 schedule_delayed_work(&atusb->work, 0);
424 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 1, 0,
425 NULL, 0, 1000, GFP_KERNEL);
426 ret = atusb_get_and_clear_error(atusb);
427 if (ret < 0)
428 usb_kill_anchored_urbs(&atusb->idle_urbs);
429 return ret;
430}
431
432static void atusb_stop(struct ieee802154_hw *hw)
433{
434 struct atusb *atusb = hw->priv;
435 struct usb_device *usb_dev = atusb->usb_dev;
436
437 dev_dbg(&usb_dev->dev, "%s\n", __func__);
438 usb_kill_anchored_urbs(&atusb->idle_urbs);
439 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 0, 0,
440 NULL, 0, 1000, GFP_KERNEL);
441 atusb_get_and_clear_error(atusb);
442}
443
444#define ATUSB_MAX_TX_POWERS 0xF
445static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
446 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
447 -900, -1200, -1700,
448};
449
450static int
451atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
452{
453 struct atusb *atusb = hw->priv;
454
455 if (atusb->data)
456 return atusb->data->set_txpower(hw, mbm);
457 else
458 return -ENOTSUPP;
459}
460
461static int
462atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
463{
464 struct atusb *atusb = hw->priv;
465 u32 i;
466
467 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
468 if (hw->phy->supported.tx_powers[i] == mbm)
469 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
470 }
471
472 return -EINVAL;
473}
474
475static int
476hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
477{
478 u32 i;
479
480 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
481 if (hw->phy->supported.tx_powers[i] == mbm)
482 return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
483 }
484
485 return -EINVAL;
486}
487
488#define ATUSB_MAX_ED_LEVELS 0xF
489static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
490 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
491 -7100, -6900, -6700, -6500, -6300, -6100,
492};
493
494#define AT86RF212_MAX_TX_POWERS 0x1F
495static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
496 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
497 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
498 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
499};
500
501#define AT86RF2XX_MAX_ED_LEVELS 0xF
502static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
503 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
504 -8000, -7800, -7600, -7400, -7200, -7000,
505};
506
507static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
508 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
509 -7800, -7600, -7400, -7200, -7000, -6800,
510};
511
512static int
513atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
514{
515 struct atusb *atusb = hw->priv;
516 u8 val;
517
518 /* mapping 802.15.4 to driver spec */
519 switch (cca->mode) {
520 case NL802154_CCA_ENERGY:
521 val = 1;
522 break;
523 case NL802154_CCA_CARRIER:
524 val = 2;
525 break;
526 case NL802154_CCA_ENERGY_CARRIER:
527 switch (cca->opt) {
528 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
529 val = 3;
530 break;
531 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
532 val = 0;
533 break;
534 default:
535 return -EINVAL;
536 }
537 break;
538 default:
539 return -EINVAL;
540 }
541
542 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
543}
544
545static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
546{
547 int cca_ed_thres;
548
549 cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
550 if (cca_ed_thres < 0)
551 return cca_ed_thres;
552
553 switch (rssi_base_val) {
554 case -98:
555 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
556 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
557 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
558 break;
559 case -100:
560 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
561 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
562 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
563 break;
564 default:
565 WARN_ON(1);
566 }
567
568 return 0;
569}
570
571static int
572atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
573{
574 struct atusb *atusb = hw->priv;
575 u32 i;
576
577 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
578 if (hw->phy->supported.cca_ed_levels[i] == mbm)
579 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
580 }
581
582 return -EINVAL;
583}
584
585static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
586{
587 struct atusb *atusb = hw->priv;
588 int ret = -ENOTSUPP;
589
590 if (atusb->data) {
591 ret = atusb->data->set_channel(hw, page, channel);
592 /* @@@ ugly synchronization */
593 msleep(atusb->data->t_channel_switch);
594 }
595
596 return ret;
597}
598
599static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
600{
601 struct atusb *atusb = hw->priv;
602 int ret;
603
604 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
605 if (ret < 0)
606 return ret;
607 return 0;
608}
609
610static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
611{
612 int rc;
613 int rssi_base_val;
614
615 struct atusb *lp = hw->priv;
616
617 if (channel == 0)
618 rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
619 else
620 rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
621 if (rc < 0)
622 return rc;
623
624 if (page == 0) {
625 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
626 rssi_base_val = -100;
627 } else {
628 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
629 rssi_base_val = -98;
630 }
631 if (rc < 0)
632 return rc;
633
634 rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
635 if (rc < 0)
636 return rc;
637
638 return atusb_write_subreg(lp, SR_CHANNEL, channel);
639}
640
641static int
642atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
643{
644 struct atusb *atusb = hw->priv;
645 int ret;
646
647 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
648 if (ret)
649 return ret;
650
651 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
652 if (ret)
653 return ret;
654
655 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
656}
657
658static int
659hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
660{
661 struct atusb *atusb = hw->priv;
662
663 return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
664}
665
666static int
667atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
668{
669 struct atusb *atusb = hw->priv;
670
671 return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
672}
673
674static int
675atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
676{
677 struct atusb *atusb = hw->priv;
678 int ret;
679
680 if (on) {
681 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
682 if (ret < 0)
683 return ret;
684
685 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
686 if (ret < 0)
687 return ret;
688 } else {
689 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
690 if (ret < 0)
691 return ret;
692
693 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
694 if (ret < 0)
695 return ret;
696 }
697
698 return 0;
699}
700
701static struct atusb_chip_data atusb_chip_data = {
702 .t_channel_switch = 1,
703 .rssi_base_val = -91,
704 .set_txpower = atusb_set_txpower,
705 .set_channel = atusb_set_channel,
706};
707
708static struct atusb_chip_data hulusb_chip_data = {
709 .t_channel_switch = 11,
710 .rssi_base_val = -100,
711 .set_txpower = hulusb_set_txpower,
712 .set_channel = hulusb_set_channel,
713};
714
715static const struct ieee802154_ops atusb_ops = {
716 .owner = THIS_MODULE,
717 .xmit_async = atusb_xmit,
718 .ed = atusb_ed,
719 .set_channel = atusb_channel,
720 .start = atusb_start,
721 .stop = atusb_stop,
722 .set_hw_addr_filt = atusb_set_hw_addr_filt,
723 .set_txpower = atusb_txpower,
724 .set_lbt = hulusb_set_lbt,
725 .set_cca_mode = atusb_set_cca_mode,
726 .set_cca_ed_level = atusb_set_cca_ed_level,
727 .set_csma_params = atusb_set_csma_params,
728 .set_frame_retries = atusb_set_frame_retries,
729 .set_promiscuous_mode = atusb_set_promiscuous_mode,
730};
731
732/* ----- Firmware and chip version information ----------------------------- */
733
734static int atusb_get_and_show_revision(struct atusb *atusb)
735{
736 struct usb_device *usb_dev = atusb->usb_dev;
737 char *hw_name;
738 unsigned char buffer[3];
739 int ret;
740
741 /* Get a couple of the ATMega Firmware values */
742 ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
743 buffer, 3, 1000, GFP_KERNEL);
744 if (!ret) {
745 atusb->fw_ver_maj = buffer[0];
746 atusb->fw_ver_min = buffer[1];
747 atusb->fw_hw_type = buffer[2];
748
749 switch (atusb->fw_hw_type) {
750 case ATUSB_HW_TYPE_100813:
751 case ATUSB_HW_TYPE_101216:
752 case ATUSB_HW_TYPE_110131:
753 hw_name = "ATUSB";
754 atusb->data = &atusb_chip_data;
755 break;
756 case ATUSB_HW_TYPE_RZUSB:
757 hw_name = "RZUSB";
758 atusb->data = &atusb_chip_data;
759 break;
760 case ATUSB_HW_TYPE_HULUSB:
761 hw_name = "HULUSB";
762 atusb->data = &hulusb_chip_data;
763 break;
764 default:
765 hw_name = "UNKNOWN";
766 atusb->err = -ENOTSUPP;
767 ret = -ENOTSUPP;
768 break;
769 }
770
771 dev_info(&usb_dev->dev,
772 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
773 atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
774 atusb->fw_hw_type);
775 }
776 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
777 dev_info(&usb_dev->dev,
778 "Firmware version (%u.%u) predates our first public release.",
779 atusb->fw_ver_maj, atusb->fw_ver_min);
780 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
781 }
782
783 return ret;
784}
785
786static int atusb_get_and_show_build(struct atusb *atusb)
787{
788 struct usb_device *usb_dev = atusb->usb_dev;
789 char *build;
790 int ret;
791
792 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
793 if (!build)
794 return -ENOMEM;
795
796 ret = usb_control_msg(atusb->usb_dev, usb_rcvctrlpipe(usb_dev, 0), ATUSB_BUILD,
797 ATUSB_REQ_FROM_DEV, 0, 0, build, ATUSB_BUILD_SIZE, 1000);
798 if (ret >= 0) {
799 build[ret] = 0;
800 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
801 }
802
803 kfree(build);
804 return ret;
805}
806
807static int atusb_get_and_conf_chip(struct atusb *atusb)
808{
809 struct usb_device *usb_dev = atusb->usb_dev;
810 u8 man_id_0, man_id_1, part_num, version_num;
811 const char *chip;
812 struct ieee802154_hw *hw = atusb->hw;
813 int ret;
814
815 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
816 0, RG_MAN_ID_0, &man_id_0, 1, 1000, GFP_KERNEL);
817 if (ret < 0)
818 return ret;
819
820 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
821 0, RG_MAN_ID_1, &man_id_1, 1, 1000, GFP_KERNEL);
822 if (ret < 0)
823 return ret;
824
825 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
826 0, RG_PART_NUM, &part_num, 1, 1000, GFP_KERNEL);
827 if (ret < 0)
828 return ret;
829
830 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
831 0, RG_VERSION_NUM, &version_num, 1, 1000, GFP_KERNEL);
832 if (ret < 0)
833 return ret;
834
835 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
836 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
837
838 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
839 WPAN_PHY_FLAG_CCA_MODE;
840
841 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
842 BIT(NL802154_CCA_CARRIER) |
843 BIT(NL802154_CCA_ENERGY_CARRIER);
844 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
845 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
846
847 hw->phy->cca.mode = NL802154_CCA_ENERGY;
848
849 hw->phy->current_page = 0;
850
851 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
852 dev_err(&usb_dev->dev,
853 "non-Atmel transceiver xxxx%02x%02x\n",
854 man_id_1, man_id_0);
855 goto fail;
856 }
857
858 switch (part_num) {
859 case 2:
860 chip = "AT86RF230";
861 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
862 atusb->hw->phy->current_channel = 11; /* reset default */
863 atusb->hw->phy->supported.tx_powers = atusb_powers;
864 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
865 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
866 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
867 break;
868 case 3:
869 chip = "AT86RF231";
870 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
871 atusb->hw->phy->current_channel = 11; /* reset default */
872 atusb->hw->phy->supported.tx_powers = atusb_powers;
873 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
874 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
875 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
876 break;
877 case 7:
878 chip = "AT86RF212";
879 atusb->hw->flags |= IEEE802154_HW_LBT;
880 atusb->hw->phy->supported.channels[0] = 0x00007FF;
881 atusb->hw->phy->supported.channels[2] = 0x00007FF;
882 atusb->hw->phy->current_channel = 5;
883 atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
884 atusb->hw->phy->supported.tx_powers = at86rf212_powers;
885 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
886 atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
887 atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
888 break;
889 default:
890 dev_err(&usb_dev->dev,
891 "unexpected transceiver, part 0x%02x version 0x%02x\n",
892 part_num, version_num);
893 goto fail;
894 }
895
896 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
897 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
898
899 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
900
901 return 0;
902
903fail:
904 atusb->err = -ENODEV;
905 return -ENODEV;
906}
907
908static int atusb_set_extended_addr(struct atusb *atusb)
909{
910 struct usb_device *usb_dev = atusb->usb_dev;
911 unsigned char buffer[IEEE802154_EXTENDED_ADDR_LEN];
912 __le64 extended_addr;
913 u64 addr;
914 int ret;
915
916 /* Firmware versions before 0.3 do not support the EUI64_READ command.
917 * Just use a random address and be done.
918 */
919 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
920 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
921 return 0;
922 }
923
924 /* Firmware is new enough so we fetch the address from EEPROM */
925 ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
926 buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000, GFP_KERNEL);
927 if (ret < 0) {
928 dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
929 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
930 return ret;
931 }
932
933 memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
934 /* Check if read address is not empty and the unicast bit is set correctly */
935 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
936 dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
937 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
938 } else {
939 atusb->hw->phy->perm_extended_addr = extended_addr;
940 addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
941 dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
942 &addr);
943 }
944
945 return ret;
946}
947
948/* ----- Setup ------------------------------------------------------------- */
949
950static int atusb_probe(struct usb_interface *interface,
951 const struct usb_device_id *id)
952{
953 struct usb_device *usb_dev = interface_to_usbdev(interface);
954 struct ieee802154_hw *hw;
955 struct atusb *atusb = NULL;
956 int ret = -ENOMEM;
957
958 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
959 if (!hw)
960 return -ENOMEM;
961
962 atusb = hw->priv;
963 atusb->hw = hw;
964 atusb->usb_dev = usb_get_dev(usb_dev);
965 usb_set_intfdata(interface, atusb);
966
967 atusb->shutdown = 0;
968 atusb->err = 0;
969 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
970 init_usb_anchor(&atusb->idle_urbs);
971 init_usb_anchor(&atusb->rx_urbs);
972
973 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
974 goto fail;
975
976 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
977 atusb->tx_dr.bRequest = ATUSB_TX;
978 atusb->tx_dr.wValue = cpu_to_le16(0);
979
980 atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
981 if (!atusb->tx_urb)
982 goto fail;
983
984 hw->parent = &usb_dev->dev;
985
986 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RF_RESET, ATUSB_REQ_TO_DEV, 0, 0,
987 NULL, 0, 1000, GFP_KERNEL);
988 atusb_get_and_conf_chip(atusb);
989 atusb_get_and_show_revision(atusb);
990 atusb_get_and_show_build(atusb);
991 atusb_set_extended_addr(atusb);
992
993 if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
994 hw->flags |= IEEE802154_HW_FRAME_RETRIES;
995
996 ret = atusb_get_and_clear_error(atusb);
997 if (ret) {
998 dev_err(&atusb->usb_dev->dev,
999 "%s: initialization failed, error = %d\n",
1000 __func__, ret);
1001 goto fail;
1002 }
1003
1004 ret = ieee802154_register_hw(hw);
1005 if (ret)
1006 goto fail;
1007
1008 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1009 * explicitly. Any resets after that will send us straight to TRX_OFF,
1010 * making the command below redundant.
1011 */
1012 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
1013 STATE_FORCE_TRX_OFF, RG_TRX_STATE, NULL, 0, 1000, GFP_KERNEL);
1014
1015 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1016
1017#if 0
1018 /* Calculating the maximum time available to empty the frame buffer
1019 * on reception:
1020 *
1021 * According to [1], the inter-frame gap is
1022 * R * 20 * 16 us + 128 us
1023 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1024 * times (80 us at 250 kbps) of SHR of the next frame before the
1025 * transceiver begins storing data in the frame buffer.
1026 *
1027 * This yields a minimum time of 208 us between the last data of a
1028 * frame and the first data of the next frame. This time is further
1029 * reduced by interrupt latency in the atusb firmware.
1030 *
1031 * atusb currently needs about 500 us to retrieve a maximum-sized
1032 * frame. We therefore have to allow reception of a new frame to begin
1033 * while we retrieve the previous frame.
1034 *
1035 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1036 * network", Jennic 2006.
1037 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1038 */
1039
1040 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1041#endif
1042 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
1043 0xff, RG_IRQ_MASK, NULL, 0, 1000, GFP_KERNEL);
1044
1045 ret = atusb_get_and_clear_error(atusb);
1046 if (!ret)
1047 return 0;
1048
1049 dev_err(&atusb->usb_dev->dev,
1050 "%s: setup failed, error = %d\n",
1051 __func__, ret);
1052
1053 ieee802154_unregister_hw(hw);
1054fail:
1055 atusb_free_urbs(atusb);
1056 usb_kill_urb(atusb->tx_urb);
1057 usb_free_urb(atusb->tx_urb);
1058 usb_put_dev(usb_dev);
1059 ieee802154_free_hw(hw);
1060 return ret;
1061}
1062
1063static void atusb_disconnect(struct usb_interface *interface)
1064{
1065 struct atusb *atusb = usb_get_intfdata(interface);
1066
1067 dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
1068
1069 atusb->shutdown = 1;
1070 cancel_delayed_work_sync(&atusb->work);
1071
1072 usb_kill_anchored_urbs(&atusb->rx_urbs);
1073 atusb_free_urbs(atusb);
1074 usb_kill_urb(atusb->tx_urb);
1075 usb_free_urb(atusb->tx_urb);
1076
1077 ieee802154_unregister_hw(atusb->hw);
1078
1079 usb_put_dev(atusb->usb_dev);
1080
1081 ieee802154_free_hw(atusb->hw);
1082
1083 usb_set_intfdata(interface, NULL);
1084
1085 pr_debug("%s done\n", __func__);
1086}
1087
1088/* The devices we work with */
1089static const struct usb_device_id atusb_device_table[] = {
1090 {
1091 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1092 USB_DEVICE_ID_MATCH_INT_INFO,
1093 .idVendor = ATUSB_VENDOR_ID,
1094 .idProduct = ATUSB_PRODUCT_ID,
1095 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
1096 },
1097 /* end with null element */
1098 {}
1099};
1100MODULE_DEVICE_TABLE(usb, atusb_device_table);
1101
1102static struct usb_driver atusb_driver = {
1103 .name = "atusb",
1104 .probe = atusb_probe,
1105 .disconnect = atusb_disconnect,
1106 .id_table = atusb_device_table,
1107};
1108module_usb_driver(atusb_driver);
1109
1110MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1111MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1112MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1113MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1114MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1115MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1116MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
4 *
5 * Written 2013 by Werner Almesberger <werner@almesberger.net>
6 *
7 * Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org>
8 *
9 * Based on at86rf230.c and spi_atusb.c.
10 * at86rf230.c is
11 * Copyright (C) 2009 Siemens AG
12 * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
13 *
14 * spi_atusb.c is
15 * Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com>
16 * Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org>
17 * Copyright (c) 2011 Werner Almesberger <werner@almesberger.net>
18 *
19 * USB initialization is
20 * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com>
21 *
22 * Busware HUL support is
23 * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at>
24 */
25
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>
32
33#include <net/cfg802154.h>
34#include <net/mac802154.h>
35
36#include "at86rf230.h"
37#include "atusb.h"
38
39#define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */
40
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 */
44
45struct atusb {
46 struct ieee802154_hw *hw;
47 struct usb_device *usb_dev;
48 struct atusb_chip_data *data;
49 int shutdown; /* non-zero if shutting down */
50 int err; /* set by first error */
51
52 /* RX variables */
53 struct delayed_work work; /* memory allocations */
54 struct usb_anchor idle_urbs; /* URBs waiting to be submitted */
55 struct usb_anchor rx_urbs; /* URBs waiting for reception */
56
57 /* TX variables */
58 struct usb_ctrlrequest tx_dr;
59 struct urb *tx_urb;
60 struct sk_buff *tx_skb;
61 u8 tx_ack_seq; /* current TX ACK sequence number */
62
63 /* Firmware variable */
64 unsigned char fw_ver_maj; /* Firmware major version number */
65 unsigned char fw_ver_min; /* Firmware minor version number */
66 unsigned char fw_hw_type; /* Firmware hardware type */
67};
68
69struct atusb_chip_data {
70 u16 t_channel_switch;
71 int rssi_base_val;
72
73 int (*set_channel)(struct ieee802154_hw*, u8, u8);
74 int (*set_txpower)(struct ieee802154_hw*, s32);
75};
76
77/* ----- USB commands without data ----------------------------------------- */
78
79/* To reduce the number of error checks in the code, we record the first error
80 * in atusb->err and reject all subsequent requests until the error is cleared.
81 */
82
83static int atusb_control_msg(struct atusb *atusb, unsigned int pipe,
84 __u8 request, __u8 requesttype,
85 __u16 value, __u16 index,
86 void *data, __u16 size, int timeout)
87{
88 struct usb_device *usb_dev = atusb->usb_dev;
89 int ret;
90
91 if (atusb->err)
92 return atusb->err;
93
94 ret = usb_control_msg(usb_dev, pipe, request, requesttype,
95 value, index, data, size, timeout);
96 if (ret < 0) {
97 atusb->err = ret;
98 dev_err(&usb_dev->dev,
99 "%s: req 0x%02x val 0x%x idx 0x%x, error %d\n",
100 __func__, request, value, index, ret);
101 }
102 return ret;
103}
104
105static int atusb_command(struct atusb *atusb, u8 cmd, u8 arg)
106{
107 struct usb_device *usb_dev = atusb->usb_dev;
108
109 dev_dbg(&usb_dev->dev, "%s: cmd = 0x%x\n", __func__, cmd);
110 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
111 cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000);
112}
113
114static int atusb_write_reg(struct atusb *atusb, u8 reg, u8 value)
115{
116 struct usb_device *usb_dev = atusb->usb_dev;
117
118 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
119 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
120 ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
121 value, reg, NULL, 0, 1000);
122}
123
124static int atusb_read_reg(struct atusb *atusb, u8 reg)
125{
126 struct usb_device *usb_dev = atusb->usb_dev;
127 int ret;
128 u8 *buffer;
129 u8 value;
130
131 buffer = kmalloc(1, GFP_KERNEL);
132 if (!buffer)
133 return -ENOMEM;
134
135 dev_dbg(&usb_dev->dev, "%s: reg = 0x%x\n", __func__, reg);
136 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
137 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
138 0, reg, buffer, 1, 1000);
139
140 if (ret >= 0) {
141 value = buffer[0];
142 kfree(buffer);
143 return value;
144 } else {
145 kfree(buffer);
146 return ret;
147 }
148}
149
150static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
151 u8 shift, u8 value)
152{
153 struct usb_device *usb_dev = atusb->usb_dev;
154 u8 orig, tmp;
155 int ret = 0;
156
157 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
158
159 orig = atusb_read_reg(atusb, reg);
160
161 /* Write the value only into that part of the register which is allowed
162 * by the mask. All other bits stay as before.
163 */
164 tmp = orig & ~mask;
165 tmp |= (value << shift) & mask;
166
167 if (tmp != orig)
168 ret = atusb_write_reg(atusb, reg, tmp);
169
170 return ret;
171}
172
173static int atusb_read_subreg(struct atusb *lp,
174 unsigned int addr, unsigned int mask,
175 unsigned int shift)
176{
177 int rc;
178
179 rc = atusb_read_reg(lp, addr);
180 rc = (rc & mask) >> shift;
181
182 return rc;
183}
184
185static int atusb_get_and_clear_error(struct atusb *atusb)
186{
187 int err = atusb->err;
188
189 atusb->err = 0;
190 return err;
191}
192
193/* ----- skb allocation ---------------------------------------------------- */
194
195#define MAX_PSDU 127
196#define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
197
198#define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
199
200static void atusb_in(struct urb *urb);
201
202static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
203{
204 struct usb_device *usb_dev = atusb->usb_dev;
205 struct sk_buff *skb = urb->context;
206 int ret;
207
208 if (!skb) {
209 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
210 if (!skb) {
211 dev_warn_ratelimited(&usb_dev->dev,
212 "atusb_in: can't allocate skb\n");
213 return -ENOMEM;
214 }
215 skb_put(skb, MAX_RX_XFER);
216 SKB_ATUSB(skb) = atusb;
217 }
218
219 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
220 skb->data, MAX_RX_XFER, atusb_in, skb);
221 usb_anchor_urb(urb, &atusb->rx_urbs);
222
223 ret = usb_submit_urb(urb, GFP_KERNEL);
224 if (ret) {
225 usb_unanchor_urb(urb);
226 kfree_skb(skb);
227 urb->context = NULL;
228 }
229 return ret;
230}
231
232static void atusb_work_urbs(struct work_struct *work)
233{
234 struct atusb *atusb =
235 container_of(to_delayed_work(work), struct atusb, work);
236 struct usb_device *usb_dev = atusb->usb_dev;
237 struct urb *urb;
238 int ret;
239
240 if (atusb->shutdown)
241 return;
242
243 do {
244 urb = usb_get_from_anchor(&atusb->idle_urbs);
245 if (!urb)
246 return;
247 ret = atusb_submit_rx_urb(atusb, urb);
248 } while (!ret);
249
250 usb_anchor_urb(urb, &atusb->idle_urbs);
251 dev_warn_ratelimited(&usb_dev->dev,
252 "atusb_in: can't allocate/submit URB (%d)\n", ret);
253 schedule_delayed_work(&atusb->work,
254 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
255}
256
257/* ----- Asynchronous USB -------------------------------------------------- */
258
259static void atusb_tx_done(struct atusb *atusb, u8 seq)
260{
261 struct usb_device *usb_dev = atusb->usb_dev;
262 u8 expect = atusb->tx_ack_seq;
263
264 dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
265 if (seq == expect) {
266 /* TODO check for ifs handling in firmware */
267 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
268 } else {
269 /* TODO I experience this case when atusb has a tx complete
270 * irq before probing, we should fix the firmware it's an
271 * unlikely case now that seq == expect is then true, but can
272 * happen and fail with a tx_skb = NULL;
273 */
274 ieee802154_wake_queue(atusb->hw);
275 if (atusb->tx_skb)
276 dev_kfree_skb_irq(atusb->tx_skb);
277 }
278}
279
280static void atusb_in_good(struct urb *urb)
281{
282 struct usb_device *usb_dev = urb->dev;
283 struct sk_buff *skb = urb->context;
284 struct atusb *atusb = SKB_ATUSB(skb);
285 u8 len, lqi;
286
287 if (!urb->actual_length) {
288 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
289 return;
290 }
291
292 len = *skb->data;
293
294 if (urb->actual_length == 1) {
295 atusb_tx_done(atusb, len);
296 return;
297 }
298
299 if (len + 1 > urb->actual_length - 1) {
300 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
301 len, urb->actual_length);
302 return;
303 }
304
305 if (!ieee802154_is_valid_psdu_len(len)) {
306 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
307 return;
308 }
309
310 lqi = skb->data[len + 1];
311 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
312 skb_pull(skb, 1); /* remove PHR */
313 skb_trim(skb, len); /* get payload only */
314 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
315 urb->context = NULL; /* skb is gone */
316}
317
318static void atusb_in(struct urb *urb)
319{
320 struct usb_device *usb_dev = urb->dev;
321 struct sk_buff *skb = urb->context;
322 struct atusb *atusb = SKB_ATUSB(skb);
323
324 dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
325 urb->status, urb->actual_length);
326 if (urb->status) {
327 if (urb->status == -ENOENT) { /* being killed */
328 kfree_skb(skb);
329 urb->context = NULL;
330 return;
331 }
332 dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
333 } else {
334 atusb_in_good(urb);
335 }
336
337 usb_anchor_urb(urb, &atusb->idle_urbs);
338 if (!atusb->shutdown)
339 schedule_delayed_work(&atusb->work, 0);
340}
341
342/* ----- URB allocation/deallocation --------------------------------------- */
343
344static void atusb_free_urbs(struct atusb *atusb)
345{
346 struct urb *urb;
347
348 while (1) {
349 urb = usb_get_from_anchor(&atusb->idle_urbs);
350 if (!urb)
351 break;
352 kfree_skb(urb->context);
353 usb_free_urb(urb);
354 }
355}
356
357static int atusb_alloc_urbs(struct atusb *atusb, int n)
358{
359 struct urb *urb;
360
361 while (n) {
362 urb = usb_alloc_urb(0, GFP_KERNEL);
363 if (!urb) {
364 atusb_free_urbs(atusb);
365 return -ENOMEM;
366 }
367 usb_anchor_urb(urb, &atusb->idle_urbs);
368 usb_free_urb(urb);
369 n--;
370 }
371 return 0;
372}
373
374/* ----- IEEE 802.15.4 interface operations -------------------------------- */
375
376static void atusb_xmit_complete(struct urb *urb)
377{
378 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
379}
380
381static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
382{
383 struct atusb *atusb = hw->priv;
384 struct usb_device *usb_dev = atusb->usb_dev;
385 int ret;
386
387 dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
388 atusb->tx_skb = skb;
389 atusb->tx_ack_seq++;
390 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
391 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
392
393 usb_fill_control_urb(atusb->tx_urb, usb_dev,
394 usb_sndctrlpipe(usb_dev, 0),
395 (unsigned char *)&atusb->tx_dr, skb->data,
396 skb->len, atusb_xmit_complete, NULL);
397 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
398 dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
399 return ret;
400}
401
402static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
403{
404 WARN_ON(!level);
405 *level = 0xbe;
406 return 0;
407}
408
409static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
410 struct ieee802154_hw_addr_filt *filt,
411 unsigned long changed)
412{
413 struct atusb *atusb = hw->priv;
414 struct device *dev = &atusb->usb_dev->dev;
415
416 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
417 u16 addr = le16_to_cpu(filt->short_addr);
418
419 dev_vdbg(dev, "%s called for saddr\n", __func__);
420 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
421 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
422 }
423
424 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
425 u16 pan = le16_to_cpu(filt->pan_id);
426
427 dev_vdbg(dev, "%s called for pan id\n", __func__);
428 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
429 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
430 }
431
432 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
433 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
434
435 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
436 dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
437 for (i = 0; i < 8; i++)
438 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
439 }
440
441 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
442 dev_vdbg(dev, "%s called for panc change\n", __func__);
443 if (filt->pan_coord)
444 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
445 else
446 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
447 }
448
449 return atusb_get_and_clear_error(atusb);
450}
451
452static int atusb_start(struct ieee802154_hw *hw)
453{
454 struct atusb *atusb = hw->priv;
455 struct usb_device *usb_dev = atusb->usb_dev;
456 int ret;
457
458 dev_dbg(&usb_dev->dev, "%s\n", __func__);
459 schedule_delayed_work(&atusb->work, 0);
460 atusb_command(atusb, ATUSB_RX_MODE, 1);
461 ret = atusb_get_and_clear_error(atusb);
462 if (ret < 0)
463 usb_kill_anchored_urbs(&atusb->idle_urbs);
464 return ret;
465}
466
467static void atusb_stop(struct ieee802154_hw *hw)
468{
469 struct atusb *atusb = hw->priv;
470 struct usb_device *usb_dev = atusb->usb_dev;
471
472 dev_dbg(&usb_dev->dev, "%s\n", __func__);
473 usb_kill_anchored_urbs(&atusb->idle_urbs);
474 atusb_command(atusb, ATUSB_RX_MODE, 0);
475 atusb_get_and_clear_error(atusb);
476}
477
478#define ATUSB_MAX_TX_POWERS 0xF
479static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
480 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
481 -900, -1200, -1700,
482};
483
484static int
485atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
486{
487 struct atusb *atusb = hw->priv;
488
489 if (atusb->data)
490 return atusb->data->set_txpower(hw, mbm);
491 else
492 return -ENOTSUPP;
493}
494
495static int
496atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
497{
498 struct atusb *atusb = hw->priv;
499 u32 i;
500
501 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
502 if (hw->phy->supported.tx_powers[i] == mbm)
503 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
504 }
505
506 return -EINVAL;
507}
508
509static int
510hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
511{
512 u32 i;
513
514 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
515 if (hw->phy->supported.tx_powers[i] == mbm)
516 return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
517 }
518
519 return -EINVAL;
520}
521
522#define ATUSB_MAX_ED_LEVELS 0xF
523static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
524 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
525 -7100, -6900, -6700, -6500, -6300, -6100,
526};
527
528#define AT86RF212_MAX_TX_POWERS 0x1F
529static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
530 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
531 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
532 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
533};
534
535#define AT86RF2XX_MAX_ED_LEVELS 0xF
536static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
537 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
538 -8000, -7800, -7600, -7400, -7200, -7000,
539};
540
541static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
542 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
543 -7800, -7600, -7400, -7200, -7000, -6800,
544};
545
546static int
547atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
548{
549 struct atusb *atusb = hw->priv;
550 u8 val;
551
552 /* mapping 802.15.4 to driver spec */
553 switch (cca->mode) {
554 case NL802154_CCA_ENERGY:
555 val = 1;
556 break;
557 case NL802154_CCA_CARRIER:
558 val = 2;
559 break;
560 case NL802154_CCA_ENERGY_CARRIER:
561 switch (cca->opt) {
562 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
563 val = 3;
564 break;
565 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
566 val = 0;
567 break;
568 default:
569 return -EINVAL;
570 }
571 break;
572 default:
573 return -EINVAL;
574 }
575
576 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
577}
578
579static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
580{
581 unsigned int cca_ed_thres;
582
583 cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
584
585 switch (rssi_base_val) {
586 case -98:
587 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
588 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
589 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
590 break;
591 case -100:
592 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
593 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
594 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
595 break;
596 default:
597 WARN_ON(1);
598 }
599
600 return 0;
601}
602
603static int
604atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
605{
606 struct atusb *atusb = hw->priv;
607 u32 i;
608
609 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
610 if (hw->phy->supported.cca_ed_levels[i] == mbm)
611 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
612 }
613
614 return -EINVAL;
615}
616
617static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
618{
619 struct atusb *atusb = hw->priv;
620 int ret = -ENOTSUPP;
621
622 if (atusb->data) {
623 ret = atusb->data->set_channel(hw, page, channel);
624 /* @@@ ugly synchronization */
625 msleep(atusb->data->t_channel_switch);
626 }
627
628 return ret;
629}
630
631static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
632{
633 struct atusb *atusb = hw->priv;
634 int ret;
635
636 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
637 if (ret < 0)
638 return ret;
639 return 0;
640}
641
642static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
643{
644 int rc;
645 int rssi_base_val;
646
647 struct atusb *lp = hw->priv;
648
649 if (channel == 0)
650 rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
651 else
652 rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
653 if (rc < 0)
654 return rc;
655
656 if (page == 0) {
657 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
658 rssi_base_val = -100;
659 } else {
660 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
661 rssi_base_val = -98;
662 }
663 if (rc < 0)
664 return rc;
665
666 rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
667 if (rc < 0)
668 return rc;
669
670 /* This sets the symbol_duration according frequency on the 212.
671 * TODO move this handling while set channel and page in cfg802154.
672 * We can do that, this timings are according 802.15.4 standard.
673 * If we do that in cfg802154, this is a more generic calculation.
674 *
675 * This should also protected from ifs_timer. Means cancel timer and
676 * init with a new value. For now, this is okay.
677 */
678 if (channel == 0) {
679 if (page == 0) {
680 /* SUB:0 and BPSK:0 -> BPSK-20 */
681 lp->hw->phy->symbol_duration = 50;
682 } else {
683 /* SUB:1 and BPSK:0 -> BPSK-40 */
684 lp->hw->phy->symbol_duration = 25;
685 }
686 } else {
687 if (page == 0)
688 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
689 lp->hw->phy->symbol_duration = 40;
690 else
691 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
692 lp->hw->phy->symbol_duration = 16;
693 }
694
695 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
696 lp->hw->phy->symbol_duration;
697 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
698 lp->hw->phy->symbol_duration;
699
700 return atusb_write_subreg(lp, SR_CHANNEL, channel);
701}
702
703static int
704atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
705{
706 struct atusb *atusb = hw->priv;
707 int ret;
708
709 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
710 if (ret)
711 return ret;
712
713 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
714 if (ret)
715 return ret;
716
717 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
718}
719
720static int
721hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
722{
723 struct atusb *atusb = hw->priv;
724
725 return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
726}
727
728static int
729atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
730{
731 struct atusb *atusb = hw->priv;
732
733 return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
734}
735
736static int
737atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
738{
739 struct atusb *atusb = hw->priv;
740 int ret;
741
742 if (on) {
743 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
744 if (ret < 0)
745 return ret;
746
747 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
748 if (ret < 0)
749 return ret;
750 } else {
751 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
752 if (ret < 0)
753 return ret;
754
755 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
756 if (ret < 0)
757 return ret;
758 }
759
760 return 0;
761}
762
763static struct atusb_chip_data atusb_chip_data = {
764 .t_channel_switch = 1,
765 .rssi_base_val = -91,
766 .set_txpower = atusb_set_txpower,
767 .set_channel = atusb_set_channel,
768};
769
770static struct atusb_chip_data hulusb_chip_data = {
771 .t_channel_switch = 11,
772 .rssi_base_val = -100,
773 .set_txpower = hulusb_set_txpower,
774 .set_channel = hulusb_set_channel,
775};
776
777static const struct ieee802154_ops atusb_ops = {
778 .owner = THIS_MODULE,
779 .xmit_async = atusb_xmit,
780 .ed = atusb_ed,
781 .set_channel = atusb_channel,
782 .start = atusb_start,
783 .stop = atusb_stop,
784 .set_hw_addr_filt = atusb_set_hw_addr_filt,
785 .set_txpower = atusb_txpower,
786 .set_lbt = hulusb_set_lbt,
787 .set_cca_mode = atusb_set_cca_mode,
788 .set_cca_ed_level = atusb_set_cca_ed_level,
789 .set_csma_params = atusb_set_csma_params,
790 .set_frame_retries = atusb_set_frame_retries,
791 .set_promiscuous_mode = atusb_set_promiscuous_mode,
792};
793
794/* ----- Firmware and chip version information ----------------------------- */
795
796static int atusb_get_and_show_revision(struct atusb *atusb)
797{
798 struct usb_device *usb_dev = atusb->usb_dev;
799 char *hw_name;
800 unsigned char *buffer;
801 int ret;
802
803 buffer = kmalloc(3, GFP_KERNEL);
804 if (!buffer)
805 return -ENOMEM;
806
807 /* Get a couple of the ATMega Firmware values */
808 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
809 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
810 buffer, 3, 1000);
811 if (ret >= 0) {
812 atusb->fw_ver_maj = buffer[0];
813 atusb->fw_ver_min = buffer[1];
814 atusb->fw_hw_type = buffer[2];
815
816 switch (atusb->fw_hw_type) {
817 case ATUSB_HW_TYPE_100813:
818 case ATUSB_HW_TYPE_101216:
819 case ATUSB_HW_TYPE_110131:
820 hw_name = "ATUSB";
821 atusb->data = &atusb_chip_data;
822 break;
823 case ATUSB_HW_TYPE_RZUSB:
824 hw_name = "RZUSB";
825 atusb->data = &atusb_chip_data;
826 break;
827 case ATUSB_HW_TYPE_HULUSB:
828 hw_name = "HULUSB";
829 atusb->data = &hulusb_chip_data;
830 break;
831 default:
832 hw_name = "UNKNOWN";
833 atusb->err = -ENOTSUPP;
834 ret = -ENOTSUPP;
835 break;
836 }
837
838 dev_info(&usb_dev->dev,
839 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
840 atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
841 atusb->fw_hw_type);
842 }
843 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
844 dev_info(&usb_dev->dev,
845 "Firmware version (%u.%u) predates our first public release.",
846 atusb->fw_ver_maj, atusb->fw_ver_min);
847 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
848 }
849
850 kfree(buffer);
851 return ret;
852}
853
854static int atusb_get_and_show_build(struct atusb *atusb)
855{
856 struct usb_device *usb_dev = atusb->usb_dev;
857 char *build;
858 int ret;
859
860 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
861 if (!build)
862 return -ENOMEM;
863
864 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
865 ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
866 build, ATUSB_BUILD_SIZE, 1000);
867 if (ret >= 0) {
868 build[ret] = 0;
869 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
870 }
871
872 kfree(build);
873 return ret;
874}
875
876static int atusb_get_and_conf_chip(struct atusb *atusb)
877{
878 struct usb_device *usb_dev = atusb->usb_dev;
879 u8 man_id_0, man_id_1, part_num, version_num;
880 const char *chip;
881 struct ieee802154_hw *hw = atusb->hw;
882
883 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
884 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
885 part_num = atusb_read_reg(atusb, RG_PART_NUM);
886 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
887
888 if (atusb->err)
889 return atusb->err;
890
891 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
892 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
893
894 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
895 WPAN_PHY_FLAG_CCA_MODE;
896
897 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
898 BIT(NL802154_CCA_CARRIER) |
899 BIT(NL802154_CCA_ENERGY_CARRIER);
900 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
901 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
902
903 hw->phy->cca.mode = NL802154_CCA_ENERGY;
904
905 hw->phy->current_page = 0;
906
907 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
908 dev_err(&usb_dev->dev,
909 "non-Atmel transceiver xxxx%02x%02x\n",
910 man_id_1, man_id_0);
911 goto fail;
912 }
913
914 switch (part_num) {
915 case 2:
916 chip = "AT86RF230";
917 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
918 atusb->hw->phy->current_channel = 11; /* reset default */
919 atusb->hw->phy->symbol_duration = 16;
920 atusb->hw->phy->supported.tx_powers = atusb_powers;
921 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
922 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
923 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
924 break;
925 case 3:
926 chip = "AT86RF231";
927 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
928 atusb->hw->phy->current_channel = 11; /* reset default */
929 atusb->hw->phy->symbol_duration = 16;
930 atusb->hw->phy->supported.tx_powers = atusb_powers;
931 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
932 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
933 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
934 break;
935 case 7:
936 chip = "AT86RF212";
937 atusb->hw->flags |= IEEE802154_HW_LBT;
938 atusb->hw->phy->supported.channels[0] = 0x00007FF;
939 atusb->hw->phy->supported.channels[2] = 0x00007FF;
940 atusb->hw->phy->current_channel = 5;
941 atusb->hw->phy->symbol_duration = 25;
942 atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
943 atusb->hw->phy->supported.tx_powers = at86rf212_powers;
944 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
945 atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
946 atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
947 break;
948 default:
949 dev_err(&usb_dev->dev,
950 "unexpected transceiver, part 0x%02x version 0x%02x\n",
951 part_num, version_num);
952 goto fail;
953 }
954
955 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
956 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
957
958 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
959
960 return 0;
961
962fail:
963 atusb->err = -ENODEV;
964 return -ENODEV;
965}
966
967static int atusb_set_extended_addr(struct atusb *atusb)
968{
969 struct usb_device *usb_dev = atusb->usb_dev;
970 unsigned char *buffer;
971 __le64 extended_addr;
972 u64 addr;
973 int ret;
974
975 /* Firmware versions before 0.3 do not support the EUI64_READ command.
976 * Just use a random address and be done.
977 */
978 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
979 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
980 return 0;
981 }
982
983 buffer = kmalloc(IEEE802154_EXTENDED_ADDR_LEN, GFP_KERNEL);
984 if (!buffer)
985 return -ENOMEM;
986
987 /* Firmware is new enough so we fetch the address from EEPROM */
988 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
989 ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
990 buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000);
991 if (ret < 0) {
992 dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
993 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
994 kfree(buffer);
995 return ret;
996 }
997
998 memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
999 /* Check if read address is not empty and the unicast bit is set correctly */
1000 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
1001 dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
1002 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
1003 } else {
1004 atusb->hw->phy->perm_extended_addr = extended_addr;
1005 addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
1006 dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
1007 &addr);
1008 }
1009
1010 kfree(buffer);
1011 return ret;
1012}
1013
1014/* ----- Setup ------------------------------------------------------------- */
1015
1016static int atusb_probe(struct usb_interface *interface,
1017 const struct usb_device_id *id)
1018{
1019 struct usb_device *usb_dev = interface_to_usbdev(interface);
1020 struct ieee802154_hw *hw;
1021 struct atusb *atusb = NULL;
1022 int ret = -ENOMEM;
1023
1024 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
1025 if (!hw)
1026 return -ENOMEM;
1027
1028 atusb = hw->priv;
1029 atusb->hw = hw;
1030 atusb->usb_dev = usb_get_dev(usb_dev);
1031 usb_set_intfdata(interface, atusb);
1032
1033 atusb->shutdown = 0;
1034 atusb->err = 0;
1035 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
1036 init_usb_anchor(&atusb->idle_urbs);
1037 init_usb_anchor(&atusb->rx_urbs);
1038
1039 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
1040 goto fail;
1041
1042 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
1043 atusb->tx_dr.bRequest = ATUSB_TX;
1044 atusb->tx_dr.wValue = cpu_to_le16(0);
1045
1046 atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1047 if (!atusb->tx_urb)
1048 goto fail;
1049
1050 hw->parent = &usb_dev->dev;
1051
1052 atusb_command(atusb, ATUSB_RF_RESET, 0);
1053 atusb_get_and_conf_chip(atusb);
1054 atusb_get_and_show_revision(atusb);
1055 atusb_get_and_show_build(atusb);
1056 atusb_set_extended_addr(atusb);
1057
1058 if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
1059 hw->flags |= IEEE802154_HW_FRAME_RETRIES;
1060
1061 ret = atusb_get_and_clear_error(atusb);
1062 if (ret) {
1063 dev_err(&atusb->usb_dev->dev,
1064 "%s: initialization failed, error = %d\n",
1065 __func__, ret);
1066 goto fail;
1067 }
1068
1069 ret = ieee802154_register_hw(hw);
1070 if (ret)
1071 goto fail;
1072
1073 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1074 * explicitly. Any resets after that will send us straight to TRX_OFF,
1075 * making the command below redundant.
1076 */
1077 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
1078 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1079
1080#if 0
1081 /* Calculating the maximum time available to empty the frame buffer
1082 * on reception:
1083 *
1084 * According to [1], the inter-frame gap is
1085 * R * 20 * 16 us + 128 us
1086 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1087 * times (80 us at 250 kbps) of SHR of the next frame before the
1088 * transceiver begins storing data in the frame buffer.
1089 *
1090 * This yields a minimum time of 208 us between the last data of a
1091 * frame and the first data of the next frame. This time is further
1092 * reduced by interrupt latency in the atusb firmware.
1093 *
1094 * atusb currently needs about 500 us to retrieve a maximum-sized
1095 * frame. We therefore have to allow reception of a new frame to begin
1096 * while we retrieve the previous frame.
1097 *
1098 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1099 * network", Jennic 2006.
1100 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1101 */
1102
1103 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1104#endif
1105 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
1106
1107 ret = atusb_get_and_clear_error(atusb);
1108 if (!ret)
1109 return 0;
1110
1111 dev_err(&atusb->usb_dev->dev,
1112 "%s: setup failed, error = %d\n",
1113 __func__, ret);
1114
1115 ieee802154_unregister_hw(hw);
1116fail:
1117 atusb_free_urbs(atusb);
1118 usb_kill_urb(atusb->tx_urb);
1119 usb_free_urb(atusb->tx_urb);
1120 usb_put_dev(usb_dev);
1121 ieee802154_free_hw(hw);
1122 return ret;
1123}
1124
1125static void atusb_disconnect(struct usb_interface *interface)
1126{
1127 struct atusb *atusb = usb_get_intfdata(interface);
1128
1129 dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
1130
1131 atusb->shutdown = 1;
1132 cancel_delayed_work_sync(&atusb->work);
1133
1134 usb_kill_anchored_urbs(&atusb->rx_urbs);
1135 atusb_free_urbs(atusb);
1136 usb_kill_urb(atusb->tx_urb);
1137 usb_free_urb(atusb->tx_urb);
1138
1139 ieee802154_unregister_hw(atusb->hw);
1140
1141 usb_put_dev(atusb->usb_dev);
1142
1143 ieee802154_free_hw(atusb->hw);
1144
1145 usb_set_intfdata(interface, NULL);
1146
1147 pr_debug("%s done\n", __func__);
1148}
1149
1150/* The devices we work with */
1151static const struct usb_device_id atusb_device_table[] = {
1152 {
1153 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1154 USB_DEVICE_ID_MATCH_INT_INFO,
1155 .idVendor = ATUSB_VENDOR_ID,
1156 .idProduct = ATUSB_PRODUCT_ID,
1157 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
1158 },
1159 /* end with null element */
1160 {}
1161};
1162MODULE_DEVICE_TABLE(usb, atusb_device_table);
1163
1164static struct usb_driver atusb_driver = {
1165 .name = "atusb",
1166 .probe = atusb_probe,
1167 .disconnect = atusb_disconnect,
1168 .id_table = atusb_device_table,
1169};
1170module_usb_driver(atusb_driver);
1171
1172MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1173MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1174MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1175MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1176MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1177MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1178MODULE_LICENSE("GPL");