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