Linux Audio

Check our new training course

Loading...
v4.17
   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 * Busware HUL support is
  26 * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at>
  27 */
  28
  29#include <linux/kernel.h>
  30#include <linux/slab.h>
  31#include <linux/module.h>
  32#include <linux/jiffies.h>
  33#include <linux/usb.h>
  34#include <linux/skbuff.h>
  35
  36#include <net/cfg802154.h>
  37#include <net/mac802154.h>
  38
  39#include "at86rf230.h"
  40#include "atusb.h"
  41
  42#define ATUSB_JEDEC_ATMEL	0x1f	/* JEDEC manufacturer ID */
  43
  44#define ATUSB_NUM_RX_URBS	4	/* allow for a bit of local latency */
  45#define ATUSB_ALLOC_DELAY_MS	100	/* delay after failed allocation */
  46#define ATUSB_TX_TIMEOUT_MS	200	/* on the air timeout */
  47
  48struct atusb {
  49	struct ieee802154_hw *hw;
  50	struct usb_device *usb_dev;
  51	struct atusb_chip_data *data;
  52	int shutdown;			/* non-zero if shutting down */
  53	int err;			/* set by first error */
  54
  55	/* RX variables */
  56	struct delayed_work work;	/* memory allocations */
  57	struct usb_anchor idle_urbs;	/* URBs waiting to be submitted */
  58	struct usb_anchor rx_urbs;	/* URBs waiting for reception */
  59
  60	/* TX variables */
  61	struct usb_ctrlrequest tx_dr;
  62	struct urb *tx_urb;
  63	struct sk_buff *tx_skb;
  64	u8 tx_ack_seq;		/* current TX ACK sequence number */
  65
  66	/* Firmware variable */
  67	unsigned char fw_ver_maj;	/* Firmware major version number */
  68	unsigned char fw_ver_min;	/* Firmware minor version number */
  69	unsigned char fw_hw_type;	/* Firmware hardware type */
  70};
  71
  72struct atusb_chip_data {
  73	u16 t_channel_switch;
  74	int rssi_base_val;
  75
  76	int (*set_channel)(struct ieee802154_hw*, u8, u8);
  77	int (*set_txpower)(struct ieee802154_hw*, s32);
  78};
  79
  80/* ----- USB commands without data ----------------------------------------- */
  81
  82/* To reduce the number of error checks in the code, we record the first error
  83 * in atusb->err and reject all subsequent requests until the error is cleared.
  84 */
  85
  86static int atusb_control_msg(struct atusb *atusb, unsigned int pipe,
  87			     __u8 request, __u8 requesttype,
  88			     __u16 value, __u16 index,
  89			     void *data, __u16 size, int timeout)
  90{
  91	struct usb_device *usb_dev = atusb->usb_dev;
  92	int ret;
  93
  94	if (atusb->err)
  95		return atusb->err;
  96
  97	ret = usb_control_msg(usb_dev, pipe, request, requesttype,
  98			      value, index, data, size, timeout);
  99	if (ret < 0) {
 100		atusb->err = ret;
 101		dev_err(&usb_dev->dev,
 102			"%s: req 0x%02x val 0x%x idx 0x%x, error %d\n",
 103			__func__, request, value, index, ret);
 104	}
 105	return ret;
 106}
 107
 108static int atusb_command(struct atusb *atusb, u8 cmd, u8 arg)
 109{
 110	struct usb_device *usb_dev = atusb->usb_dev;
 111
 112	dev_dbg(&usb_dev->dev, "%s: cmd = 0x%x\n", __func__, cmd);
 113	return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
 114				 cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000);
 115}
 116
 117static int atusb_write_reg(struct atusb *atusb, u8 reg, u8 value)
 118{
 119	struct usb_device *usb_dev = atusb->usb_dev;
 120
 121	dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
 
 122	return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
 123				 ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
 124				 value, reg, NULL, 0, 1000);
 125}
 126
 127static int atusb_read_reg(struct atusb *atusb, u8 reg)
 128{
 129	struct usb_device *usb_dev = atusb->usb_dev;
 130	int ret;
 131	u8 *buffer;
 132	u8 value;
 133
 134	buffer = kmalloc(1, GFP_KERNEL);
 135	if (!buffer)
 136		return -ENOMEM;
 137
 138	dev_dbg(&usb_dev->dev, "%s: reg = 0x%x\n", __func__, reg);
 139	ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
 140				ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
 141				0, reg, buffer, 1, 1000);
 142
 143	if (ret >= 0) {
 144		value = buffer[0];
 145		kfree(buffer);
 146		return value;
 147	} else {
 148		kfree(buffer);
 149		return ret;
 150	}
 151}
 152
 153static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
 154			      u8 shift, u8 value)
 155{
 156	struct usb_device *usb_dev = atusb->usb_dev;
 157	u8 orig, tmp;
 158	int ret = 0;
 159
 160	dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
 
 161
 162	orig = atusb_read_reg(atusb, reg);
 163
 164	/* Write the value only into that part of the register which is allowed
 165	 * by the mask. All other bits stay as before.
 166	 */
 167	tmp = orig & ~mask;
 168	tmp |= (value << shift) & mask;
 169
 170	if (tmp != orig)
 171		ret = atusb_write_reg(atusb, reg, tmp);
 172
 173	return ret;
 174}
 175
 176static int atusb_read_subreg(struct atusb *lp,
 177			     unsigned int addr, unsigned int mask,
 178			     unsigned int shift)
 179{
 180	int rc;
 181
 182	rc = atusb_read_reg(lp, addr);
 183	rc = (rc & mask) >> shift;
 184
 185	return rc;
 186}
 187
 188static int atusb_get_and_clear_error(struct atusb *atusb)
 189{
 190	int err = atusb->err;
 191
 192	atusb->err = 0;
 193	return err;
 194}
 195
 196/* ----- skb allocation ---------------------------------------------------- */
 197
 198#define MAX_PSDU	127
 199#define MAX_RX_XFER	(1 + MAX_PSDU + 2 + 1)	/* PHR+PSDU+CRC+LQI */
 200
 201#define SKB_ATUSB(skb)	(*(struct atusb **)(skb)->cb)
 202
 203static void atusb_in(struct urb *urb);
 204
 205static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
 206{
 207	struct usb_device *usb_dev = atusb->usb_dev;
 208	struct sk_buff *skb = urb->context;
 209	int ret;
 210
 211	if (!skb) {
 212		skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
 213		if (!skb) {
 214			dev_warn_ratelimited(&usb_dev->dev,
 215					     "atusb_in: can't allocate skb\n");
 216			return -ENOMEM;
 217		}
 218		skb_put(skb, MAX_RX_XFER);
 219		SKB_ATUSB(skb) = atusb;
 220	}
 221
 222	usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
 223			  skb->data, MAX_RX_XFER, atusb_in, skb);
 224	usb_anchor_urb(urb, &atusb->rx_urbs);
 225
 226	ret = usb_submit_urb(urb, GFP_KERNEL);
 227	if (ret) {
 228		usb_unanchor_urb(urb);
 229		kfree_skb(skb);
 230		urb->context = NULL;
 231	}
 232	return ret;
 233}
 234
 235static void atusb_work_urbs(struct work_struct *work)
 236{
 237	struct atusb *atusb =
 238	    container_of(to_delayed_work(work), struct atusb, work);
 239	struct usb_device *usb_dev = atusb->usb_dev;
 240	struct urb *urb;
 241	int ret;
 242
 243	if (atusb->shutdown)
 244		return;
 245
 246	do {
 247		urb = usb_get_from_anchor(&atusb->idle_urbs);
 248		if (!urb)
 249			return;
 250		ret = atusb_submit_rx_urb(atusb, urb);
 251	} while (!ret);
 252
 253	usb_anchor_urb(urb, &atusb->idle_urbs);
 254	dev_warn_ratelimited(&usb_dev->dev,
 255			     "atusb_in: can't allocate/submit URB (%d)\n", ret);
 256	schedule_delayed_work(&atusb->work,
 257			      msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
 258}
 259
 260/* ----- Asynchronous USB -------------------------------------------------- */
 261
 262static void atusb_tx_done(struct atusb *atusb, u8 seq)
 263{
 264	struct usb_device *usb_dev = atusb->usb_dev;
 265	u8 expect = atusb->tx_ack_seq;
 266
 267	dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
 268	if (seq == expect) {
 269		/* TODO check for ifs handling in firmware */
 270		ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
 271	} else {
 272		/* TODO I experience this case when atusb has a tx complete
 273		 * irq before probing, we should fix the firmware it's an
 274		 * unlikely case now that seq == expect is then true, but can
 275		 * happen and fail with a tx_skb = NULL;
 276		 */
 277		ieee802154_wake_queue(atusb->hw);
 278		if (atusb->tx_skb)
 279			dev_kfree_skb_irq(atusb->tx_skb);
 280	}
 281}
 282
 283static void atusb_in_good(struct urb *urb)
 284{
 285	struct usb_device *usb_dev = urb->dev;
 286	struct sk_buff *skb = urb->context;
 287	struct atusb *atusb = SKB_ATUSB(skb);
 288	u8 len, lqi;
 289
 290	if (!urb->actual_length) {
 291		dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
 292		return;
 293	}
 294
 295	len = *skb->data;
 296
 297	if (urb->actual_length == 1) {
 298		atusb_tx_done(atusb, len);
 299		return;
 300	}
 301
 302	if (len + 1 > urb->actual_length - 1) {
 303		dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
 304			len, urb->actual_length);
 305		return;
 306	}
 307
 308	if (!ieee802154_is_valid_psdu_len(len)) {
 309		dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
 310		return;
 311	}
 312
 313	lqi = skb->data[len + 1];
 314	dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
 315	skb_pull(skb, 1);	/* remove PHR */
 316	skb_trim(skb, len);	/* get payload only */
 317	ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
 318	urb->context = NULL;	/* skb is gone */
 319}
 320
 321static void atusb_in(struct urb *urb)
 322{
 323	struct usb_device *usb_dev = urb->dev;
 324	struct sk_buff *skb = urb->context;
 325	struct atusb *atusb = SKB_ATUSB(skb);
 326
 327	dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
 328		urb->status, urb->actual_length);
 329	if (urb->status) {
 330		if (urb->status == -ENOENT) { /* being killed */
 331			kfree_skb(skb);
 332			urb->context = NULL;
 333			return;
 334		}
 335		dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
 336	} else {
 337		atusb_in_good(urb);
 338	}
 339
 340	usb_anchor_urb(urb, &atusb->idle_urbs);
 341	if (!atusb->shutdown)
 342		schedule_delayed_work(&atusb->work, 0);
 343}
 344
 345/* ----- URB allocation/deallocation --------------------------------------- */
 346
 347static void atusb_free_urbs(struct atusb *atusb)
 348{
 349	struct urb *urb;
 350
 351	while (1) {
 352		urb = usb_get_from_anchor(&atusb->idle_urbs);
 353		if (!urb)
 354			break;
 355		kfree_skb(urb->context);
 356		usb_free_urb(urb);
 357	}
 358}
 359
 360static int atusb_alloc_urbs(struct atusb *atusb, int n)
 361{
 362	struct urb *urb;
 363
 364	while (n) {
 365		urb = usb_alloc_urb(0, GFP_KERNEL);
 366		if (!urb) {
 367			atusb_free_urbs(atusb);
 368			return -ENOMEM;
 369		}
 370		usb_anchor_urb(urb, &atusb->idle_urbs);
 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	ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
 867				ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
 868				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	ieee802154_free_hw(atusb->hw);
1144
1145	usb_set_intfdata(interface, NULL);
1146	usb_put_dev(atusb->usb_dev);
1147
1148	pr_debug("%s done\n", __func__);
1149}
1150
1151/* The devices we work with */
1152static const struct usb_device_id atusb_device_table[] = {
1153	{
1154		.match_flags		= USB_DEVICE_ID_MATCH_DEVICE |
1155					  USB_DEVICE_ID_MATCH_INT_INFO,
1156		.idVendor		= ATUSB_VENDOR_ID,
1157		.idProduct		= ATUSB_PRODUCT_ID,
1158		.bInterfaceClass	= USB_CLASS_VENDOR_SPEC
1159	},
1160	/* end with null element */
1161	{}
1162};
1163MODULE_DEVICE_TABLE(usb, atusb_device_table);
1164
1165static struct usb_driver atusb_driver = {
1166	.name		= "atusb",
1167	.probe		= atusb_probe,
1168	.disconnect	= atusb_disconnect,
1169	.id_table	= atusb_device_table,
1170};
1171module_usb_driver(atusb_driver);
1172
1173MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1174MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1175MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1176MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1177MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1178MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1179MODULE_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");