// SPDX-License-Identifier: GPL-2.0-only
/*
* atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
*
* Written 2013 by Werner Almesberger <werner@almesberger.net>
*
* Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org>
*
* Based on at86rf230.c and spi_atusb.c.
* at86rf230.c is
* Copyright (C) 2009 Siemens AG
* Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
*
* spi_atusb.c is
* Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com>
* Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org>
* Copyright (c) 2011 Werner Almesberger <werner@almesberger.net>
*
* USB initialization is
* Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com>
*
* Busware HUL support is
* Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at>
*/
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/jiffies.h>
#include <linux/usb.h>
#include <linux/skbuff.h>
#include <net/cfg802154.h>
#include <net/mac802154.h>
#include "at86rf230.h"
#include "atusb.h"
#define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */
#define ATUSB_NUM_RX_URBS 4 /* allow for a bit of local latency */
#define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */
#define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */
struct atusb {
struct ieee802154_hw *hw;
struct usb_device *usb_dev;
struct atusb_chip_data *data;
int shutdown; /* non-zero if shutting down */
int err; /* set by first error */
/* RX variables */
struct delayed_work work; /* memory allocations */
struct usb_anchor idle_urbs; /* URBs waiting to be submitted */
struct usb_anchor rx_urbs; /* URBs waiting for reception */
/* TX variables */
struct usb_ctrlrequest tx_dr;
struct urb *tx_urb;
struct sk_buff *tx_skb;
u8 tx_ack_seq; /* current TX ACK sequence number */
/* Firmware variable */
unsigned char fw_ver_maj; /* Firmware major version number */
unsigned char fw_ver_min; /* Firmware minor version number */
unsigned char fw_hw_type; /* Firmware hardware type */
};
struct atusb_chip_data {
u16 t_channel_switch;
int rssi_base_val;
int (*set_channel)(struct ieee802154_hw*, u8, u8);
int (*set_txpower)(struct ieee802154_hw*, s32);
};
/* ----- USB commands without data ----------------------------------------- */
/* To reduce the number of error checks in the code, we record the first error
* in atusb->err and reject all subsequent requests until the error is cleared.
*/
static int atusb_control_msg(struct atusb *atusb, unsigned int pipe,
__u8 request, __u8 requesttype,
__u16 value, __u16 index,
void *data, __u16 size, int timeout)
{
struct usb_device *usb_dev = atusb->usb_dev;
int ret;
if (atusb->err)
return atusb->err;
ret = usb_control_msg(usb_dev, pipe, request, requesttype,
value, index, data, size, timeout);
if (ret < 0) {
atusb->err = ret;
dev_err(&usb_dev->dev,
"%s: req 0x%02x val 0x%x idx 0x%x, error %d\n",
__func__, request, value, index, ret);
}
return ret;
}
static int atusb_command(struct atusb *atusb, u8 cmd, u8 arg)
{
struct usb_device *usb_dev = atusb->usb_dev;
dev_dbg(&usb_dev->dev, "%s: cmd = 0x%x\n", __func__, cmd);
return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000);
}
static int atusb_write_reg(struct atusb *atusb, u8 reg, u8 value)
{