Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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