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