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