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