blob: 1056ed142411d3c75082fced68c59f1b269d0532 [file] [log] [blame]
Alexander Aring7490b002015-05-17 21:44:57 +02001/*
2 * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
3 *
4 * Written 2013 by Werner Almesberger <werner@almesberger.net>
5 *
Stefan Schmidt151c37b2016-04-19 16:28:55 +02006 * Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org>
7 *
Alexander Aring7490b002015-05-17 21:44:57 +02008 * 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
11 *
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>
16 *
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>
21 *
22 * USB initialization is
23 * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com>
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 int shutdown; /* non-zero if shutting down */
49 int err; /* set by first error */
50
51 /* RX variables */
52 struct delayed_work work; /* memory allocations */
53 struct usb_anchor idle_urbs; /* URBs waiting to be submitted */
54 struct usb_anchor rx_urbs; /* URBs waiting for reception */
55
56 /* TX variables */
57 struct usb_ctrlrequest tx_dr;
58 struct urb *tx_urb;
59 struct sk_buff *tx_skb;
60 uint8_t tx_ack_seq; /* current TX ACK sequence number */
61};
62
Alexander Aring7490b002015-05-17 21:44:57 +020063/* ----- USB commands without data ----------------------------------------- */
64
65/* To reduce the number of error checks in the code, we record the first error
66 * in atusb->err and reject all subsequent requests until the error is cleared.
67 */
68
69static int atusb_control_msg(struct atusb *atusb, unsigned int pipe,
70 __u8 request, __u8 requesttype,
71 __u16 value, __u16 index,
72 void *data, __u16 size, int timeout)
73{
74 struct usb_device *usb_dev = atusb->usb_dev;
75 int ret;
76
77 if (atusb->err)
78 return atusb->err;
79
80 ret = usb_control_msg(usb_dev, pipe, request, requesttype,
81 value, index, data, size, timeout);
82 if (ret < 0) {
83 atusb->err = ret;
84 dev_err(&usb_dev->dev,
85 "atusb_control_msg: req 0x%02x val 0x%x idx 0x%x, error %d\n",
86 request, value, index, ret);
87 }
88 return ret;
89}
90
91static int atusb_command(struct atusb *atusb, uint8_t cmd, uint8_t arg)
92{
93 struct usb_device *usb_dev = atusb->usb_dev;
94
95 dev_dbg(&usb_dev->dev, "atusb_command: cmd = 0x%x\n", cmd);
96 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
97 cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000);
98}
99
100static int atusb_write_reg(struct atusb *atusb, uint8_t reg, uint8_t value)
101{
102 struct usb_device *usb_dev = atusb->usb_dev;
103
104 dev_dbg(&usb_dev->dev, "atusb_write_reg: 0x%02x <- 0x%02x\n",
105 reg, value);
106 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
107 ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
108 value, reg, NULL, 0, 1000);
109}
110
111static int atusb_read_reg(struct atusb *atusb, uint8_t reg)
112{
113 struct usb_device *usb_dev = atusb->usb_dev;
114 int ret;
115 uint8_t value;
116
117 dev_dbg(&usb_dev->dev, "atusb: reg = 0x%x\n", reg);
118 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
119 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
120 0, reg, &value, 1, 1000);
121 return ret >= 0 ? value : ret;
122}
123
Stefan Schmidtbdc78732015-05-29 10:51:25 +0200124static int atusb_write_subreg(struct atusb *atusb, uint8_t reg, uint8_t mask,
125 uint8_t shift, uint8_t value)
126{
127 struct usb_device *usb_dev = atusb->usb_dev;
128 uint8_t orig, tmp;
129 int ret = 0;
130
131 dev_dbg(&usb_dev->dev, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
132 reg, value);
133
134 orig = atusb_read_reg(atusb, reg);
135
136 /* Write the value only into that part of the register which is allowed
137 * by the mask. All other bits stay as before.
138 */
139 tmp = orig & ~mask;
140 tmp |= (value << shift) & mask;
141
142 if (tmp != orig)
143 ret = atusb_write_reg(atusb, reg, tmp);
144
145 return ret;
146}
147
Alexander Aring7490b002015-05-17 21:44:57 +0200148static int atusb_get_and_clear_error(struct atusb *atusb)
149{
150 int err = atusb->err;
151
152 atusb->err = 0;
153 return err;
154}
155
156/* ----- skb allocation ---------------------------------------------------- */
157
158#define MAX_PSDU 127
159#define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
160
161#define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
162
163static void atusb_in(struct urb *urb);
164
165static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
166{
167 struct usb_device *usb_dev = atusb->usb_dev;
168 struct sk_buff *skb = urb->context;
169 int ret;
170
171 if (!skb) {
172 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
173 if (!skb) {
174 dev_warn_ratelimited(&usb_dev->dev,
175 "atusb_in: can't allocate skb\n");
176 return -ENOMEM;
177 }
178 skb_put(skb, MAX_RX_XFER);
179 SKB_ATUSB(skb) = atusb;
180 }
181
182 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
183 skb->data, MAX_RX_XFER, atusb_in, skb);
184 usb_anchor_urb(urb, &atusb->rx_urbs);
185
186 ret = usb_submit_urb(urb, GFP_KERNEL);
187 if (ret) {
188 usb_unanchor_urb(urb);
189 kfree_skb(skb);
190 urb->context = NULL;
191 }
192 return ret;
193}
194
195static void atusb_work_urbs(struct work_struct *work)
196{
197 struct atusb *atusb =
198 container_of(to_delayed_work(work), struct atusb, work);
199 struct usb_device *usb_dev = atusb->usb_dev;
200 struct urb *urb;
201 int ret;
202
203 if (atusb->shutdown)
204 return;
205
206 do {
207 urb = usb_get_from_anchor(&atusb->idle_urbs);
208 if (!urb)
209 return;
210 ret = atusb_submit_rx_urb(atusb, urb);
211 } while (!ret);
212
213 usb_anchor_urb(urb, &atusb->idle_urbs);
214 dev_warn_ratelimited(&usb_dev->dev,
215 "atusb_in: can't allocate/submit URB (%d)\n", ret);
216 schedule_delayed_work(&atusb->work,
217 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
218}
219
220/* ----- Asynchronous USB -------------------------------------------------- */
221
222static void atusb_tx_done(struct atusb *atusb, uint8_t seq)
223{
224 struct usb_device *usb_dev = atusb->usb_dev;
225 uint8_t expect = atusb->tx_ack_seq;
226
227 dev_dbg(&usb_dev->dev, "atusb_tx_done (0x%02x/0x%02x)\n", seq, expect);
228 if (seq == expect) {
229 /* TODO check for ifs handling in firmware */
230 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
231 } else {
232 /* TODO I experience this case when atusb has a tx complete
233 * irq before probing, we should fix the firmware it's an
234 * unlikely case now that seq == expect is then true, but can
235 * happen and fail with a tx_skb = NULL;
236 */
237 ieee802154_wake_queue(atusb->hw);
238 if (atusb->tx_skb)
239 dev_kfree_skb_irq(atusb->tx_skb);
240 }
241}
242
243static void atusb_in_good(struct urb *urb)
244{
245 struct usb_device *usb_dev = urb->dev;
246 struct sk_buff *skb = urb->context;
247 struct atusb *atusb = SKB_ATUSB(skb);
248 uint8_t len, lqi;
249
250 if (!urb->actual_length) {
251 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
252 return;
253 }
254
255 len = *skb->data;
256
257 if (urb->actual_length == 1) {
258 atusb_tx_done(atusb, len);
259 return;
260 }
261
262 if (len + 1 > urb->actual_length - 1) {
263 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
264 len, urb->actual_length);
265 return;
266 }
267
268 if (!ieee802154_is_valid_psdu_len(len)) {
269 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
270 return;
271 }
272
273 lqi = skb->data[len + 1];
274 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
275 skb_pull(skb, 1); /* remove PHR */
276 skb_trim(skb, len); /* get payload only */
277 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
278 urb->context = NULL; /* skb is gone */
279}
280
281static void atusb_in(struct urb *urb)
282{
283 struct usb_device *usb_dev = urb->dev;
284 struct sk_buff *skb = urb->context;
285 struct atusb *atusb = SKB_ATUSB(skb);
286
287 dev_dbg(&usb_dev->dev, "atusb_in: status %d len %d\n",
288 urb->status, urb->actual_length);
289 if (urb->status) {
290 if (urb->status == -ENOENT) { /* being killed */
291 kfree_skb(skb);
292 urb->context = NULL;
293 return;
294 }
295 dev_dbg(&usb_dev->dev, "atusb_in: URB error %d\n", urb->status);
296 } else {
297 atusb_in_good(urb);
298 }
299
300 usb_anchor_urb(urb, &atusb->idle_urbs);
301 if (!atusb->shutdown)
302 schedule_delayed_work(&atusb->work, 0);
303}
304
305/* ----- URB allocation/deallocation --------------------------------------- */
306
307static void atusb_free_urbs(struct atusb *atusb)
308{
309 struct urb *urb;
310
311 while (1) {
312 urb = usb_get_from_anchor(&atusb->idle_urbs);
313 if (!urb)
314 break;
Markus Elfring41881462015-12-10 23:44:34 +0100315 kfree_skb(urb->context);
Alexander Aring7490b002015-05-17 21:44:57 +0200316 usb_free_urb(urb);
317 }
318}
319
320static int atusb_alloc_urbs(struct atusb *atusb, int n)
321{
322 struct urb *urb;
323
324 while (n) {
325 urb = usb_alloc_urb(0, GFP_KERNEL);
326 if (!urb) {
327 atusb_free_urbs(atusb);
328 return -ENOMEM;
329 }
330 usb_anchor_urb(urb, &atusb->idle_urbs);
331 n--;
332 }
333 return 0;
334}
335
336/* ----- IEEE 802.15.4 interface operations -------------------------------- */
337
338static void atusb_xmit_complete(struct urb *urb)
339{
340 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
341}
342
343static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
344{
345 struct atusb *atusb = hw->priv;
346 struct usb_device *usb_dev = atusb->usb_dev;
347 int ret;
348
349 dev_dbg(&usb_dev->dev, "atusb_xmit (%d)\n", skb->len);
350 atusb->tx_skb = skb;
351 atusb->tx_ack_seq++;
352 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
353 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
354
355 usb_fill_control_urb(atusb->tx_urb, usb_dev,
356 usb_sndctrlpipe(usb_dev, 0),
357 (unsigned char *)&atusb->tx_dr, skb->data,
358 skb->len, atusb_xmit_complete, NULL);
359 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
360 dev_dbg(&usb_dev->dev, "atusb_xmit done (%d)\n", ret);
361 return ret;
362}
363
364static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
365{
366 struct atusb *atusb = hw->priv;
367 int ret;
368
Alexander Aring38961292016-07-06 23:32:29 +0200369 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
Alexander Aring7490b002015-05-17 21:44:57 +0200370 if (ret < 0)
371 return ret;
372 msleep(1); /* @@@ ugly synchronization */
373 return 0;
374}
375
376static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
377{
Stefan Schmidt2d8cbd32015-05-21 16:51:37 +0200378 BUG_ON(!level);
379 *level = 0xbe;
Alexander Aring7490b002015-05-17 21:44:57 +0200380 return 0;
381}
382
383static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
384 struct ieee802154_hw_addr_filt *filt,
385 unsigned long changed)
386{
387 struct atusb *atusb = hw->priv;
388 struct device *dev = &atusb->usb_dev->dev;
Alexander Aring7490b002015-05-17 21:44:57 +0200389
390 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
391 u16 addr = le16_to_cpu(filt->short_addr);
392
393 dev_vdbg(dev, "atusb_set_hw_addr_filt called for saddr\n");
394 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
395 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
396 }
397
398 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
399 u16 pan = le16_to_cpu(filt->pan_id);
400
401 dev_vdbg(dev, "atusb_set_hw_addr_filt called for pan id\n");
402 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
403 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
404 }
405
406 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
407 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
408
409 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
410 dev_vdbg(dev, "atusb_set_hw_addr_filt called for IEEE addr\n");
411 for (i = 0; i < 8; i++)
412 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
413 }
414
415 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
416 dev_vdbg(dev,
417 "atusb_set_hw_addr_filt called for panc change\n");
Alexander Aring7490b002015-05-17 21:44:57 +0200418 if (filt->pan_coord)
Stefan Schmidtbdc78732015-05-29 10:51:25 +0200419 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
Alexander Aring7490b002015-05-17 21:44:57 +0200420 else
Stefan Schmidtbdc78732015-05-29 10:51:25 +0200421 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
Alexander Aring7490b002015-05-17 21:44:57 +0200422 }
423
424 return atusb_get_and_clear_error(atusb);
425}
426
427static int atusb_start(struct ieee802154_hw *hw)
428{
429 struct atusb *atusb = hw->priv;
430 struct usb_device *usb_dev = atusb->usb_dev;
431 int ret;
432
433 dev_dbg(&usb_dev->dev, "atusb_start\n");
434 schedule_delayed_work(&atusb->work, 0);
435 atusb_command(atusb, ATUSB_RX_MODE, 1);
436 ret = atusb_get_and_clear_error(atusb);
437 if (ret < 0)
438 usb_kill_anchored_urbs(&atusb->idle_urbs);
439 return ret;
440}
441
442static void atusb_stop(struct ieee802154_hw *hw)
443{
444 struct atusb *atusb = hw->priv;
445 struct usb_device *usb_dev = atusb->usb_dev;
446
447 dev_dbg(&usb_dev->dev, "atusb_stop\n");
448 usb_kill_anchored_urbs(&atusb->idle_urbs);
449 atusb_command(atusb, ATUSB_RX_MODE, 0);
450 atusb_get_and_clear_error(atusb);
451}
452
Stefan Schmidt8702cb02015-05-29 10:51:26 +0200453#define ATUSB_MAX_TX_POWERS 0xF
454static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
455 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
456 -900, -1200, -1700,
457};
458
459static int
460atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
461{
462 struct atusb *atusb = hw->priv;
463 u32 i;
464
465 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
466 if (hw->phy->supported.tx_powers[i] == mbm)
467 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
468 }
469
470 return -EINVAL;
471}
472
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200473#define ATUSB_MAX_ED_LEVELS 0xF
474static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
475 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
476 -7100, -6900, -6700, -6500, -6300, -6100,
477};
478
479static int
Stefan Schmidt308dbb72016-04-19 16:28:54 +0200480atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
481{
482 struct atusb *atusb = hw->priv;
483 u8 val;
484
485 /* mapping 802.15.4 to driver spec */
486 switch (cca->mode) {
487 case NL802154_CCA_ENERGY:
488 val = 1;
489 break;
490 case NL802154_CCA_CARRIER:
491 val = 2;
492 break;
493 case NL802154_CCA_ENERGY_CARRIER:
494 switch (cca->opt) {
495 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
496 val = 3;
497 break;
498 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
499 val = 0;
500 break;
501 default:
502 return -EINVAL;
503 }
504 break;
505 default:
506 return -EINVAL;
507 }
508
509 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
510}
511
512static int
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200513atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
514{
515 struct atusb *atusb = hw->priv;
516 u32 i;
517
518 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
519 if (hw->phy->supported.cca_ed_levels[i] == mbm)
520 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
521 }
522
523 return -EINVAL;
524}
525
Stefan Schmidtc61c9bd2015-05-29 10:51:27 +0200526static int
Stefan Schmidtfb7c5792016-04-19 16:28:52 +0200527atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
528{
529 struct atusb *atusb = hw->priv;
530 int ret;
531
532 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
533 if (ret)
534 return ret;
535
536 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
537 if (ret)
538 return ret;
539
540 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
541}
542
543static int
Stefan Schmidtc61c9bd2015-05-29 10:51:27 +0200544atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
545{
546 struct atusb *atusb = hw->priv;
547 int ret;
548
549 if (on) {
550 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
551 if (ret < 0)
552 return ret;
553
554 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
555 if (ret < 0)
556 return ret;
557 } else {
558 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
559 if (ret < 0)
560 return ret;
561
562 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
563 if (ret < 0)
564 return ret;
565 }
566
567 return 0;
568}
569
Alexander Aring7490b002015-05-17 21:44:57 +0200570static struct ieee802154_ops atusb_ops = {
571 .owner = THIS_MODULE,
572 .xmit_async = atusb_xmit,
573 .ed = atusb_ed,
574 .set_channel = atusb_channel,
575 .start = atusb_start,
576 .stop = atusb_stop,
577 .set_hw_addr_filt = atusb_set_hw_addr_filt,
Stefan Schmidt8702cb02015-05-29 10:51:26 +0200578 .set_txpower = atusb_set_txpower,
Stefan Schmidt308dbb72016-04-19 16:28:54 +0200579 .set_cca_mode = atusb_set_cca_mode,
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200580 .set_cca_ed_level = atusb_set_cca_ed_level,
Stefan Schmidtfb7c5792016-04-19 16:28:52 +0200581 .set_csma_params = atusb_set_csma_params,
Stefan Schmidtc61c9bd2015-05-29 10:51:27 +0200582 .set_promiscuous_mode = atusb_set_promiscuous_mode,
Alexander Aring7490b002015-05-17 21:44:57 +0200583};
584
585/* ----- Firmware and chip version information ----------------------------- */
586
587static int atusb_get_and_show_revision(struct atusb *atusb)
588{
589 struct usb_device *usb_dev = atusb->usb_dev;
590 unsigned char buffer[3];
591 int ret;
592
593 /* Get a couple of the ATMega Firmware values */
594 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
595 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
596 buffer, 3, 1000);
597 if (ret >= 0)
598 dev_info(&usb_dev->dev,
599 "Firmware: major: %u, minor: %u, hardware type: %u\n",
600 buffer[0], buffer[1], buffer[2]);
Stefan Schmidt33a238a2015-05-21 16:51:35 +0200601 if (buffer[0] == 0 && buffer[1] < 2) {
602 dev_info(&usb_dev->dev,
603 "Firmware version (%u.%u) is predates our first public release.",
604 buffer[0], buffer[1]);
605 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
606 }
Alexander Aring7490b002015-05-17 21:44:57 +0200607
608 return ret;
609}
610
611static int atusb_get_and_show_build(struct atusb *atusb)
612{
613 struct usb_device *usb_dev = atusb->usb_dev;
614 char build[ATUSB_BUILD_SIZE + 1];
615 int ret;
616
617 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
618 ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
619 build, ATUSB_BUILD_SIZE, 1000);
620 if (ret >= 0) {
621 build[ret] = 0;
622 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
623 }
624
625 return ret;
626}
627
628static int atusb_get_and_show_chip(struct atusb *atusb)
629{
630 struct usb_device *usb_dev = atusb->usb_dev;
631 uint8_t man_id_0, man_id_1, part_num, version_num;
Alexander Aring9def9af2015-09-05 12:27:59 +0200632 const char *chip;
Alexander Aring7490b002015-05-17 21:44:57 +0200633
634 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
635 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
636 part_num = atusb_read_reg(atusb, RG_PART_NUM);
637 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
638
639 if (atusb->err)
640 return atusb->err;
641
642 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
643 dev_err(&usb_dev->dev,
644 "non-Atmel transceiver xxxx%02x%02x\n",
645 man_id_1, man_id_0);
646 goto fail;
647 }
Alexander Aring9def9af2015-09-05 12:27:59 +0200648
649 switch (part_num) {
650 case 2:
651 chip = "AT86RF230";
652 break;
653 case 3:
654 chip = "AT86RF231";
655 break;
656 default:
Alexander Aring7490b002015-05-17 21:44:57 +0200657 dev_err(&usb_dev->dev,
658 "unexpected transceiver, part 0x%02x version 0x%02x\n",
659 part_num, version_num);
660 goto fail;
661 }
662
Alexander Aring9def9af2015-09-05 12:27:59 +0200663 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
Alexander Aring7490b002015-05-17 21:44:57 +0200664
665 return 0;
666
667fail:
668 atusb->err = -ENODEV;
669 return -ENODEV;
670}
671
672/* ----- Setup ------------------------------------------------------------- */
673
674static int atusb_probe(struct usb_interface *interface,
675 const struct usb_device_id *id)
676{
677 struct usb_device *usb_dev = interface_to_usbdev(interface);
678 struct ieee802154_hw *hw;
679 struct atusb *atusb = NULL;
680 int ret = -ENOMEM;
681
682 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
683 if (!hw)
684 return -ENOMEM;
685
686 atusb = hw->priv;
687 atusb->hw = hw;
688 atusb->usb_dev = usb_get_dev(usb_dev);
689 usb_set_intfdata(interface, atusb);
690
691 atusb->shutdown = 0;
692 atusb->err = 0;
693 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
694 init_usb_anchor(&atusb->idle_urbs);
695 init_usb_anchor(&atusb->rx_urbs);
696
697 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
698 goto fail;
699
700 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
701 atusb->tx_dr.bRequest = ATUSB_TX;
702 atusb->tx_dr.wValue = cpu_to_le16(0);
703
704 atusb->tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
705 if (!atusb->tx_urb)
706 goto fail;
707
708 hw->parent = &usb_dev->dev;
Stefan Schmidtf1a718862015-05-21 16:51:36 +0200709 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
Stefan Schmidtfb7c5792016-04-19 16:28:52 +0200710 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
Alexander Aring7490b002015-05-17 21:44:57 +0200711
Stefan Schmidt308dbb72016-04-19 16:28:54 +0200712 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
713 WPAN_PHY_FLAG_CCA_MODE;
714
715 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
716 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
717 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
718 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200719
720 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
721 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
Stefan Schmidt8702cb02015-05-29 10:51:26 +0200722
Stefan Schmidt308dbb72016-04-19 16:28:54 +0200723 hw->phy->cca.mode = NL802154_CCA_ENERGY;
724
Alexander Aring7490b002015-05-17 21:44:57 +0200725 hw->phy->current_page = 0;
726 hw->phy->current_channel = 11; /* reset default */
727 hw->phy->supported.channels[0] = 0x7FFF800;
Stefan Schmidt8702cb02015-05-29 10:51:26 +0200728 hw->phy->supported.tx_powers = atusb_powers;
729 hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
730 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
Alexander Aring7490b002015-05-17 21:44:57 +0200731 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200732 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
Alexander Aring7490b002015-05-17 21:44:57 +0200733
734 atusb_command(atusb, ATUSB_RF_RESET, 0);
735 atusb_get_and_show_chip(atusb);
736 atusb_get_and_show_revision(atusb);
737 atusb_get_and_show_build(atusb);
738 ret = atusb_get_and_clear_error(atusb);
739 if (ret) {
740 dev_err(&atusb->usb_dev->dev,
741 "%s: initialization failed, error = %d\n",
742 __func__, ret);
743 goto fail;
744 }
745
746 ret = ieee802154_register_hw(hw);
747 if (ret)
748 goto fail;
749
750 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
751 * explicitly. Any resets after that will send us straight to TRX_OFF,
752 * making the command below redundant.
753 */
754 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
755 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
756
757#if 0
758 /* Calculating the maximum time available to empty the frame buffer
759 * on reception:
760 *
761 * According to [1], the inter-frame gap is
762 * R * 20 * 16 us + 128 us
763 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
764 * times (80 us at 250 kbps) of SHR of the next frame before the
765 * transceiver begins storing data in the frame buffer.
766 *
767 * This yields a minimum time of 208 us between the last data of a
768 * frame and the first data of the next frame. This time is further
769 * reduced by interrupt latency in the atusb firmware.
770 *
771 * atusb currently needs about 500 us to retrieve a maximum-sized
772 * frame. We therefore have to allow reception of a new frame to begin
773 * while we retrieve the previous frame.
774 *
775 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
776 * network", Jennic 2006.
777 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
778 */
779
Stefan Schmidtbdc78732015-05-29 10:51:25 +0200780 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
Alexander Aring7490b002015-05-17 21:44:57 +0200781#endif
782 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
783
784 ret = atusb_get_and_clear_error(atusb);
785 if (!ret)
786 return 0;
787
788 dev_err(&atusb->usb_dev->dev,
789 "%s: setup failed, error = %d\n",
790 __func__, ret);
791
792 ieee802154_unregister_hw(hw);
793fail:
794 atusb_free_urbs(atusb);
795 usb_kill_urb(atusb->tx_urb);
796 usb_free_urb(atusb->tx_urb);
797 usb_put_dev(usb_dev);
798 ieee802154_free_hw(hw);
799 return ret;
800}
801
802static void atusb_disconnect(struct usb_interface *interface)
803{
804 struct atusb *atusb = usb_get_intfdata(interface);
805
806 dev_dbg(&atusb->usb_dev->dev, "atusb_disconnect\n");
807
808 atusb->shutdown = 1;
809 cancel_delayed_work_sync(&atusb->work);
810
811 usb_kill_anchored_urbs(&atusb->rx_urbs);
812 atusb_free_urbs(atusb);
813 usb_kill_urb(atusb->tx_urb);
814 usb_free_urb(atusb->tx_urb);
815
816 ieee802154_unregister_hw(atusb->hw);
817
818 ieee802154_free_hw(atusb->hw);
819
820 usb_set_intfdata(interface, NULL);
821 usb_put_dev(atusb->usb_dev);
822
823 pr_debug("atusb_disconnect done\n");
824}
825
826/* The devices we work with */
827static const struct usb_device_id atusb_device_table[] = {
828 {
829 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
830 USB_DEVICE_ID_MATCH_INT_INFO,
831 .idVendor = ATUSB_VENDOR_ID,
832 .idProduct = ATUSB_PRODUCT_ID,
833 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
834 },
835 /* end with null element */
836 {}
837};
838MODULE_DEVICE_TABLE(usb, atusb_device_table);
839
840static struct usb_driver atusb_driver = {
841 .name = "atusb",
842 .probe = atusb_probe,
843 .disconnect = atusb_disconnect,
844 .id_table = atusb_device_table,
845};
846module_usb_driver(atusb_driver);
847
848MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
849MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
850MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
851MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
852MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
853MODULE_LICENSE("GPL");