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