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