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