Linux Audio

Check our new training course

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