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