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