blob: f186e0460cde311092f2ae8298e83060317bef5f [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;
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100115 uint8_t *buffer;
Alexander Aring7490b002015-05-17 21:44:57 +0200116 uint8_t value;
117
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100118 buffer = kmalloc(1, GFP_KERNEL);
119 if (!buffer)
120 return -ENOMEM;
121
Alexander Aring7490b002015-05-17 21:44:57 +0200122 dev_dbg(&usb_dev->dev, "atusb: reg = 0x%x\n", reg);
123 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
124 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100125 0, reg, buffer, 1, 1000);
126
127 if (ret >= 0) {
128 value = buffer[0];
129 kfree(buffer);
130 return value;
131 } else {
132 kfree(buffer);
133 return ret;
134 }
Alexander Aring7490b002015-05-17 21:44:57 +0200135}
136
Stefan Schmidtbdc78732015-05-29 10:51:25 +0200137static int atusb_write_subreg(struct atusb *atusb, uint8_t reg, uint8_t mask,
138 uint8_t shift, uint8_t value)
139{
140 struct usb_device *usb_dev = atusb->usb_dev;
141 uint8_t orig, tmp;
142 int ret = 0;
143
144 dev_dbg(&usb_dev->dev, "atusb_write_subreg: 0x%02x <- 0x%02x\n",
145 reg, value);
146
147 orig = atusb_read_reg(atusb, reg);
148
149 /* Write the value only into that part of the register which is allowed
150 * by the mask. All other bits stay as before.
151 */
152 tmp = orig & ~mask;
153 tmp |= (value << shift) & mask;
154
155 if (tmp != orig)
156 ret = atusb_write_reg(atusb, reg, tmp);
157
158 return ret;
159}
160
Alexander Aring7490b002015-05-17 21:44:57 +0200161static int atusb_get_and_clear_error(struct atusb *atusb)
162{
163 int err = atusb->err;
164
165 atusb->err = 0;
166 return err;
167}
168
169/* ----- skb allocation ---------------------------------------------------- */
170
171#define MAX_PSDU 127
172#define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
173
174#define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
175
176static void atusb_in(struct urb *urb);
177
178static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
179{
180 struct usb_device *usb_dev = atusb->usb_dev;
181 struct sk_buff *skb = urb->context;
182 int ret;
183
184 if (!skb) {
185 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
186 if (!skb) {
187 dev_warn_ratelimited(&usb_dev->dev,
188 "atusb_in: can't allocate skb\n");
189 return -ENOMEM;
190 }
191 skb_put(skb, MAX_RX_XFER);
192 SKB_ATUSB(skb) = atusb;
193 }
194
195 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
196 skb->data, MAX_RX_XFER, atusb_in, skb);
197 usb_anchor_urb(urb, &atusb->rx_urbs);
198
199 ret = usb_submit_urb(urb, GFP_KERNEL);
200 if (ret) {
201 usb_unanchor_urb(urb);
202 kfree_skb(skb);
203 urb->context = NULL;
204 }
205 return ret;
206}
207
208static void atusb_work_urbs(struct work_struct *work)
209{
210 struct atusb *atusb =
211 container_of(to_delayed_work(work), struct atusb, work);
212 struct usb_device *usb_dev = atusb->usb_dev;
213 struct urb *urb;
214 int ret;
215
216 if (atusb->shutdown)
217 return;
218
219 do {
220 urb = usb_get_from_anchor(&atusb->idle_urbs);
221 if (!urb)
222 return;
223 ret = atusb_submit_rx_urb(atusb, urb);
224 } while (!ret);
225
226 usb_anchor_urb(urb, &atusb->idle_urbs);
227 dev_warn_ratelimited(&usb_dev->dev,
228 "atusb_in: can't allocate/submit URB (%d)\n", ret);
229 schedule_delayed_work(&atusb->work,
230 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
231}
232
233/* ----- Asynchronous USB -------------------------------------------------- */
234
235static void atusb_tx_done(struct atusb *atusb, uint8_t seq)
236{
237 struct usb_device *usb_dev = atusb->usb_dev;
238 uint8_t expect = atusb->tx_ack_seq;
239
240 dev_dbg(&usb_dev->dev, "atusb_tx_done (0x%02x/0x%02x)\n", seq, expect);
241 if (seq == expect) {
242 /* TODO check for ifs handling in firmware */
243 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
244 } else {
245 /* TODO I experience this case when atusb has a tx complete
246 * irq before probing, we should fix the firmware it's an
247 * unlikely case now that seq == expect is then true, but can
248 * happen and fail with a tx_skb = NULL;
249 */
250 ieee802154_wake_queue(atusb->hw);
251 if (atusb->tx_skb)
252 dev_kfree_skb_irq(atusb->tx_skb);
253 }
254}
255
256static void atusb_in_good(struct urb *urb)
257{
258 struct usb_device *usb_dev = urb->dev;
259 struct sk_buff *skb = urb->context;
260 struct atusb *atusb = SKB_ATUSB(skb);
261 uint8_t len, lqi;
262
263 if (!urb->actual_length) {
264 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
265 return;
266 }
267
268 len = *skb->data;
269
270 if (urb->actual_length == 1) {
271 atusb_tx_done(atusb, len);
272 return;
273 }
274
275 if (len + 1 > urb->actual_length - 1) {
276 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
277 len, urb->actual_length);
278 return;
279 }
280
281 if (!ieee802154_is_valid_psdu_len(len)) {
282 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
283 return;
284 }
285
286 lqi = skb->data[len + 1];
287 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
288 skb_pull(skb, 1); /* remove PHR */
289 skb_trim(skb, len); /* get payload only */
290 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
291 urb->context = NULL; /* skb is gone */
292}
293
294static void atusb_in(struct urb *urb)
295{
296 struct usb_device *usb_dev = urb->dev;
297 struct sk_buff *skb = urb->context;
298 struct atusb *atusb = SKB_ATUSB(skb);
299
300 dev_dbg(&usb_dev->dev, "atusb_in: status %d len %d\n",
301 urb->status, urb->actual_length);
302 if (urb->status) {
303 if (urb->status == -ENOENT) { /* being killed */
304 kfree_skb(skb);
305 urb->context = NULL;
306 return;
307 }
308 dev_dbg(&usb_dev->dev, "atusb_in: URB error %d\n", urb->status);
309 } else {
310 atusb_in_good(urb);
311 }
312
313 usb_anchor_urb(urb, &atusb->idle_urbs);
314 if (!atusb->shutdown)
315 schedule_delayed_work(&atusb->work, 0);
316}
317
318/* ----- URB allocation/deallocation --------------------------------------- */
319
320static void atusb_free_urbs(struct atusb *atusb)
321{
322 struct urb *urb;
323
324 while (1) {
325 urb = usb_get_from_anchor(&atusb->idle_urbs);
326 if (!urb)
327 break;
Markus Elfring418814652015-12-10 23:44:34 +0100328 kfree_skb(urb->context);
Alexander Aring7490b002015-05-17 21:44:57 +0200329 usb_free_urb(urb);
330 }
331}
332
333static int atusb_alloc_urbs(struct atusb *atusb, int n)
334{
335 struct urb *urb;
336
337 while (n) {
338 urb = usb_alloc_urb(0, GFP_KERNEL);
339 if (!urb) {
340 atusb_free_urbs(atusb);
341 return -ENOMEM;
342 }
343 usb_anchor_urb(urb, &atusb->idle_urbs);
344 n--;
345 }
346 return 0;
347}
348
349/* ----- IEEE 802.15.4 interface operations -------------------------------- */
350
351static void atusb_xmit_complete(struct urb *urb)
352{
353 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
354}
355
356static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
357{
358 struct atusb *atusb = hw->priv;
359 struct usb_device *usb_dev = atusb->usb_dev;
360 int ret;
361
362 dev_dbg(&usb_dev->dev, "atusb_xmit (%d)\n", skb->len);
363 atusb->tx_skb = skb;
364 atusb->tx_ack_seq++;
365 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
366 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
367
368 usb_fill_control_urb(atusb->tx_urb, usb_dev,
369 usb_sndctrlpipe(usb_dev, 0),
370 (unsigned char *)&atusb->tx_dr, skb->data,
371 skb->len, atusb_xmit_complete, NULL);
372 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
373 dev_dbg(&usb_dev->dev, "atusb_xmit done (%d)\n", ret);
374 return ret;
375}
376
377static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
378{
379 struct atusb *atusb = hw->priv;
380 int ret;
381
Alexander Aring38961292016-07-06 23:32:29 +0200382 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
Alexander Aring7490b002015-05-17 21:44:57 +0200383 if (ret < 0)
384 return ret;
385 msleep(1); /* @@@ ugly synchronization */
386 return 0;
387}
388
389static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
390{
Stefan Schmidt2d8cbd32015-05-21 16:51:37 +0200391 BUG_ON(!level);
392 *level = 0xbe;
Alexander Aring7490b002015-05-17 21:44:57 +0200393 return 0;
394}
395
396static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
397 struct ieee802154_hw_addr_filt *filt,
398 unsigned long changed)
399{
400 struct atusb *atusb = hw->priv;
401 struct device *dev = &atusb->usb_dev->dev;
Alexander Aring7490b002015-05-17 21:44:57 +0200402
403 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
404 u16 addr = le16_to_cpu(filt->short_addr);
405
406 dev_vdbg(dev, "atusb_set_hw_addr_filt called for saddr\n");
407 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
408 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
409 }
410
411 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
412 u16 pan = le16_to_cpu(filt->pan_id);
413
414 dev_vdbg(dev, "atusb_set_hw_addr_filt called for pan id\n");
415 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
416 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
417 }
418
419 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
420 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
421
422 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
423 dev_vdbg(dev, "atusb_set_hw_addr_filt called for IEEE addr\n");
424 for (i = 0; i < 8; i++)
425 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
426 }
427
428 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
429 dev_vdbg(dev,
430 "atusb_set_hw_addr_filt called for panc change\n");
Alexander Aring7490b002015-05-17 21:44:57 +0200431 if (filt->pan_coord)
Stefan Schmidtbdc78732015-05-29 10:51:25 +0200432 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
Alexander Aring7490b002015-05-17 21:44:57 +0200433 else
Stefan Schmidtbdc78732015-05-29 10:51:25 +0200434 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
Alexander Aring7490b002015-05-17 21:44:57 +0200435 }
436
437 return atusb_get_and_clear_error(atusb);
438}
439
440static int atusb_start(struct ieee802154_hw *hw)
441{
442 struct atusb *atusb = hw->priv;
443 struct usb_device *usb_dev = atusb->usb_dev;
444 int ret;
445
446 dev_dbg(&usb_dev->dev, "atusb_start\n");
447 schedule_delayed_work(&atusb->work, 0);
448 atusb_command(atusb, ATUSB_RX_MODE, 1);
449 ret = atusb_get_and_clear_error(atusb);
450 if (ret < 0)
451 usb_kill_anchored_urbs(&atusb->idle_urbs);
452 return ret;
453}
454
455static void atusb_stop(struct ieee802154_hw *hw)
456{
457 struct atusb *atusb = hw->priv;
458 struct usb_device *usb_dev = atusb->usb_dev;
459
460 dev_dbg(&usb_dev->dev, "atusb_stop\n");
461 usb_kill_anchored_urbs(&atusb->idle_urbs);
462 atusb_command(atusb, ATUSB_RX_MODE, 0);
463 atusb_get_and_clear_error(atusb);
464}
465
Stefan Schmidt8702cb02015-05-29 10:51:26 +0200466#define ATUSB_MAX_TX_POWERS 0xF
467static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
468 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
469 -900, -1200, -1700,
470};
471
472static int
473atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
474{
475 struct atusb *atusb = hw->priv;
476 u32 i;
477
478 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
479 if (hw->phy->supported.tx_powers[i] == mbm)
480 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
481 }
482
483 return -EINVAL;
484}
485
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200486#define ATUSB_MAX_ED_LEVELS 0xF
487static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
488 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
489 -7100, -6900, -6700, -6500, -6300, -6100,
490};
491
492static int
Stefan Schmidt308dbb72016-04-19 16:28:54 +0200493atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
494{
495 struct atusb *atusb = hw->priv;
496 u8 val;
497
498 /* mapping 802.15.4 to driver spec */
499 switch (cca->mode) {
500 case NL802154_CCA_ENERGY:
501 val = 1;
502 break;
503 case NL802154_CCA_CARRIER:
504 val = 2;
505 break;
506 case NL802154_CCA_ENERGY_CARRIER:
507 switch (cca->opt) {
508 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
509 val = 3;
510 break;
511 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
512 val = 0;
513 break;
514 default:
515 return -EINVAL;
516 }
517 break;
518 default:
519 return -EINVAL;
520 }
521
522 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
523}
524
525static int
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200526atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
527{
528 struct atusb *atusb = hw->priv;
529 u32 i;
530
531 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
532 if (hw->phy->supported.cca_ed_levels[i] == mbm)
533 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
534 }
535
536 return -EINVAL;
537}
538
Stefan Schmidtc61c9bd2015-05-29 10:51:27 +0200539static int
Stefan Schmidtfb7c5792016-04-19 16:28:52 +0200540atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
541{
542 struct atusb *atusb = hw->priv;
543 int ret;
544
545 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
546 if (ret)
547 return ret;
548
549 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
550 if (ret)
551 return ret;
552
553 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
554}
555
556static int
Stefan Schmidtc61c9bd2015-05-29 10:51:27 +0200557atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
558{
559 struct atusb *atusb = hw->priv;
560 int ret;
561
562 if (on) {
563 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
564 if (ret < 0)
565 return ret;
566
567 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
568 if (ret < 0)
569 return ret;
570 } else {
571 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
572 if (ret < 0)
573 return ret;
574
575 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
576 if (ret < 0)
577 return ret;
578 }
579
580 return 0;
581}
582
Alexander Aring7490b002015-05-17 21:44:57 +0200583static struct ieee802154_ops atusb_ops = {
584 .owner = THIS_MODULE,
585 .xmit_async = atusb_xmit,
586 .ed = atusb_ed,
587 .set_channel = atusb_channel,
588 .start = atusb_start,
589 .stop = atusb_stop,
590 .set_hw_addr_filt = atusb_set_hw_addr_filt,
Stefan Schmidt8702cb02015-05-29 10:51:26 +0200591 .set_txpower = atusb_set_txpower,
Stefan Schmidt308dbb72016-04-19 16:28:54 +0200592 .set_cca_mode = atusb_set_cca_mode,
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200593 .set_cca_ed_level = atusb_set_cca_ed_level,
Stefan Schmidtfb7c5792016-04-19 16:28:52 +0200594 .set_csma_params = atusb_set_csma_params,
Stefan Schmidtc61c9bd2015-05-29 10:51:27 +0200595 .set_promiscuous_mode = atusb_set_promiscuous_mode,
Alexander Aring7490b002015-05-17 21:44:57 +0200596};
597
598/* ----- Firmware and chip version information ----------------------------- */
599
600static int atusb_get_and_show_revision(struct atusb *atusb)
601{
602 struct usb_device *usb_dev = atusb->usb_dev;
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100603 unsigned char *buffer;
Alexander Aring7490b002015-05-17 21:44:57 +0200604 int ret;
605
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100606 buffer = kmalloc(3, GFP_KERNEL);
607 if (!buffer)
608 return -ENOMEM;
609
Alexander Aring7490b002015-05-17 21:44:57 +0200610 /* Get a couple of the ATMega Firmware values */
611 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
612 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
613 buffer, 3, 1000);
614 if (ret >= 0)
615 dev_info(&usb_dev->dev,
616 "Firmware: major: %u, minor: %u, hardware type: %u\n",
617 buffer[0], buffer[1], buffer[2]);
Stefan Schmidt33a238a2015-05-21 16:51:35 +0200618 if (buffer[0] == 0 && buffer[1] < 2) {
619 dev_info(&usb_dev->dev,
620 "Firmware version (%u.%u) is predates our first public release.",
621 buffer[0], buffer[1]);
622 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
623 }
Alexander Aring7490b002015-05-17 21:44:57 +0200624
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100625 kfree(buffer);
Alexander Aring7490b002015-05-17 21:44:57 +0200626 return ret;
627}
628
629static int atusb_get_and_show_build(struct atusb *atusb)
630{
631 struct usb_device *usb_dev = atusb->usb_dev;
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100632 char *build;
Alexander Aring7490b002015-05-17 21:44:57 +0200633 int ret;
634
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100635 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
636 if (!build)
637 return -ENOMEM;
638
Alexander Aring7490b002015-05-17 21:44:57 +0200639 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
640 ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
641 build, ATUSB_BUILD_SIZE, 1000);
642 if (ret >= 0) {
643 build[ret] = 0;
644 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
645 }
646
Stefan Schmidt86249ae2016-12-15 18:40:14 +0100647 kfree(build);
Alexander Aring7490b002015-05-17 21:44:57 +0200648 return ret;
649}
650
651static int atusb_get_and_show_chip(struct atusb *atusb)
652{
653 struct usb_device *usb_dev = atusb->usb_dev;
654 uint8_t man_id_0, man_id_1, part_num, version_num;
Alexander Aring9def9af2015-09-05 12:27:59 +0200655 const char *chip;
Alexander Aring7490b002015-05-17 21:44:57 +0200656
657 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
658 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
659 part_num = atusb_read_reg(atusb, RG_PART_NUM);
660 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
661
662 if (atusb->err)
663 return atusb->err;
664
665 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
666 dev_err(&usb_dev->dev,
667 "non-Atmel transceiver xxxx%02x%02x\n",
668 man_id_1, man_id_0);
669 goto fail;
670 }
Alexander Aring9def9af2015-09-05 12:27:59 +0200671
672 switch (part_num) {
673 case 2:
674 chip = "AT86RF230";
675 break;
676 case 3:
677 chip = "AT86RF231";
678 break;
679 default:
Alexander Aring7490b002015-05-17 21:44:57 +0200680 dev_err(&usb_dev->dev,
681 "unexpected transceiver, part 0x%02x version 0x%02x\n",
682 part_num, version_num);
683 goto fail;
684 }
685
Alexander Aring9def9af2015-09-05 12:27:59 +0200686 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
Alexander Aring7490b002015-05-17 21:44:57 +0200687
688 return 0;
689
690fail:
691 atusb->err = -ENODEV;
692 return -ENODEV;
693}
694
695/* ----- Setup ------------------------------------------------------------- */
696
697static int atusb_probe(struct usb_interface *interface,
698 const struct usb_device_id *id)
699{
700 struct usb_device *usb_dev = interface_to_usbdev(interface);
701 struct ieee802154_hw *hw;
702 struct atusb *atusb = NULL;
703 int ret = -ENOMEM;
704
705 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
706 if (!hw)
707 return -ENOMEM;
708
709 atusb = hw->priv;
710 atusb->hw = hw;
711 atusb->usb_dev = usb_get_dev(usb_dev);
712 usb_set_intfdata(interface, atusb);
713
714 atusb->shutdown = 0;
715 atusb->err = 0;
716 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
717 init_usb_anchor(&atusb->idle_urbs);
718 init_usb_anchor(&atusb->rx_urbs);
719
720 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
721 goto fail;
722
723 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
724 atusb->tx_dr.bRequest = ATUSB_TX;
725 atusb->tx_dr.wValue = cpu_to_le16(0);
726
727 atusb->tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
728 if (!atusb->tx_urb)
729 goto fail;
730
731 hw->parent = &usb_dev->dev;
Stefan Schmidtf1a718862015-05-21 16:51:36 +0200732 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
Stefan Schmidtfb7c5792016-04-19 16:28:52 +0200733 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
Alexander Aring7490b002015-05-17 21:44:57 +0200734
Stefan Schmidt308dbb72016-04-19 16:28:54 +0200735 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
736 WPAN_PHY_FLAG_CCA_MODE;
737
738 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
739 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
740 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
741 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200742
743 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
744 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
Stefan Schmidt8702cb02015-05-29 10:51:26 +0200745
Stefan Schmidt308dbb72016-04-19 16:28:54 +0200746 hw->phy->cca.mode = NL802154_CCA_ENERGY;
747
Alexander Aring7490b002015-05-17 21:44:57 +0200748 hw->phy->current_page = 0;
749 hw->phy->current_channel = 11; /* reset default */
750 hw->phy->supported.channels[0] = 0x7FFF800;
Stefan Schmidt8702cb02015-05-29 10:51:26 +0200751 hw->phy->supported.tx_powers = atusb_powers;
752 hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
753 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
Alexander Aring7490b002015-05-17 21:44:57 +0200754 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
Stefan Schmidt0f4715c2016-04-19 16:28:53 +0200755 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
Alexander Aring7490b002015-05-17 21:44:57 +0200756
757 atusb_command(atusb, ATUSB_RF_RESET, 0);
758 atusb_get_and_show_chip(atusb);
759 atusb_get_and_show_revision(atusb);
760 atusb_get_and_show_build(atusb);
761 ret = atusb_get_and_clear_error(atusb);
762 if (ret) {
763 dev_err(&atusb->usb_dev->dev,
764 "%s: initialization failed, error = %d\n",
765 __func__, ret);
766 goto fail;
767 }
768
769 ret = ieee802154_register_hw(hw);
770 if (ret)
771 goto fail;
772
773 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
774 * explicitly. Any resets after that will send us straight to TRX_OFF,
775 * making the command below redundant.
776 */
777 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
778 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
779
780#if 0
781 /* Calculating the maximum time available to empty the frame buffer
782 * on reception:
783 *
784 * According to [1], the inter-frame gap is
785 * R * 20 * 16 us + 128 us
786 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
787 * times (80 us at 250 kbps) of SHR of the next frame before the
788 * transceiver begins storing data in the frame buffer.
789 *
790 * This yields a minimum time of 208 us between the last data of a
791 * frame and the first data of the next frame. This time is further
792 * reduced by interrupt latency in the atusb firmware.
793 *
794 * atusb currently needs about 500 us to retrieve a maximum-sized
795 * frame. We therefore have to allow reception of a new frame to begin
796 * while we retrieve the previous frame.
797 *
798 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
799 * network", Jennic 2006.
800 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
801 */
802
Stefan Schmidtbdc78732015-05-29 10:51:25 +0200803 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
Alexander Aring7490b002015-05-17 21:44:57 +0200804#endif
805 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
806
807 ret = atusb_get_and_clear_error(atusb);
808 if (!ret)
809 return 0;
810
811 dev_err(&atusb->usb_dev->dev,
812 "%s: setup failed, error = %d\n",
813 __func__, ret);
814
815 ieee802154_unregister_hw(hw);
816fail:
817 atusb_free_urbs(atusb);
818 usb_kill_urb(atusb->tx_urb);
819 usb_free_urb(atusb->tx_urb);
820 usb_put_dev(usb_dev);
821 ieee802154_free_hw(hw);
822 return ret;
823}
824
825static void atusb_disconnect(struct usb_interface *interface)
826{
827 struct atusb *atusb = usb_get_intfdata(interface);
828
829 dev_dbg(&atusb->usb_dev->dev, "atusb_disconnect\n");
830
831 atusb->shutdown = 1;
832 cancel_delayed_work_sync(&atusb->work);
833
834 usb_kill_anchored_urbs(&atusb->rx_urbs);
835 atusb_free_urbs(atusb);
836 usb_kill_urb(atusb->tx_urb);
837 usb_free_urb(atusb->tx_urb);
838
839 ieee802154_unregister_hw(atusb->hw);
840
841 ieee802154_free_hw(atusb->hw);
842
843 usb_set_intfdata(interface, NULL);
844 usb_put_dev(atusb->usb_dev);
845
846 pr_debug("atusb_disconnect done\n");
847}
848
849/* The devices we work with */
850static const struct usb_device_id atusb_device_table[] = {
851 {
852 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
853 USB_DEVICE_ID_MATCH_INT_INFO,
854 .idVendor = ATUSB_VENDOR_ID,
855 .idProduct = ATUSB_PRODUCT_ID,
856 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
857 },
858 /* end with null element */
859 {}
860};
861MODULE_DEVICE_TABLE(usb, atusb_device_table);
862
863static struct usb_driver atusb_driver = {
864 .name = "atusb",
865 .probe = atusb_probe,
866 .disconnect = atusb_disconnect,
867 .id_table = atusb_device_table,
868};
869module_usb_driver(atusb_driver);
870
871MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
872MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
873MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
874MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
875MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
876MODULE_LICENSE("GPL");