From mboxrd@z Thu Jan 1 00:00:00 1970 From: Igor Russkikh Subject: [PATCH net-next 03/19] net: usb: aqc111: Add implementation of read and write commands Date: Fri, 5 Oct 2018 10:24:44 +0000 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Cc: "linux-usb@vger.kernel.org" , "netdev@vger.kernel.org" , Igor Russkikh , Dmitry Bezrukov To: "David S . Miller" Return-path: Received: from mail-bl2nam02on0086.outbound.protection.outlook.com ([104.47.38.86]:26494 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727601AbeJERXg (ORCPT ); Fri, 5 Oct 2018 13:23:36 -0400 In-Reply-To: Content-Language: en-US Sender: netdev-owner@vger.kernel.org List-ID: From: Dmitry Bezrukov Read/write command register defines and functions Signed-off-by: Dmitry Bezrukov Signed-off-by: Igor Russkikh --- drivers/net/usb/aqc111.c | 124 +++++++++++++++++++++++++++++++++++++++++++= ++++ drivers/net/usb/aqc111.h | 19 ++++++++ 2 files changed, 143 insertions(+) create mode 100644 drivers/net/usb/aqc111.h diff --git a/drivers/net/usb/aqc111.c b/drivers/net/usb/aqc111.c index c914e19387f2..7f3e5a615750 100644 --- a/drivers/net/usb/aqc111.c +++ b/drivers/net/usb/aqc111.c @@ -14,6 +14,130 @@ #include #include =20 +#include "aqc111.h" + +static int __aqc111_read_cmd(struct usbnet *dev, u8 cmd, u16 value, + u16 index, u16 size, void *data, int nopm) +{ + int ret; + int (*fn)(struct usbnet *dev, u8 cmd, u8 reqtype, u16 value, + u16 index, void *data, u16 size); + + if (nopm) + fn =3D usbnet_read_cmd_nopm; + else + fn =3D usbnet_read_cmd; + + ret =3D fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, + value, index, data, size); + if (size =3D=3D 2) + le16_to_cpus(data); + + if (unlikely(ret < 0)) + netdev_warn(dev->net, + "Failed to read(0x%x) reg index 0x%04x: %d\n", + cmd, index, ret); + return ret; +} + +static int aqc111_read_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value, + u16 index, u16 size, void *data) +{ + return __aqc111_read_cmd(dev, cmd, value, index, size, data, 1); +} + +static int aqc111_read_cmd(struct usbnet *dev, u8 cmd, u16 value, + u16 index, u16 size, void *data) +{ + return __aqc111_read_cmd(dev, cmd, value, index, size, data, 0); +} + +static int __aq_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, + u16 value, u16 index, const void *data, u16 size) +{ + void *buf =3D NULL; + int err =3D -ENOMEM; + + netdev_dbg(dev->net, + "%s cmd=3D%#x reqtype=3D%#x value=3D%#x index=3D%#x size=3D%d\n", + __func__, cmd, reqtype, value, index, size); + + if (data) { + buf =3D kmemdup(data, size, GFP_KERNEL); + if (!buf) + goto out; + } + + if (size =3D=3D 2) + cpu_to_le16s(buf); + else if (size =3D=3D 4) + cpu_to_le32s(buf); + + err =3D usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), + cmd, reqtype, value, index, buf, size, + (cmd =3D=3D AQ_PHY_POWER) ? AQ_USB_PHY_SET_TIMEOUT : + AQ_USB_SET_TIMEOUT); + + kfree(buf); + +out: + return err; +} + +static int aq_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, + u16 value, u16 index, const void *data, u16 size) +{ + return __aq_write_cmd(dev, cmd, reqtype, value, index, + data, size); +} + +static int aq_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, + u16 value, u16 index, const void *data, u16 size) +{ + int ret; + + if (usb_autopm_get_interface(dev->intf) < 0) + return -ENODEV; + ret =3D __aq_write_cmd(dev, cmd, reqtype, value, index, data, size); + usb_autopm_put_interface(dev->intf); + return ret; +} + +static int __aqc111_write_cmd(struct usbnet *dev, u8 cmd, u16 value, + u16 index, u16 size, void *data, int nopm) +{ + int ret; + int (*fn)(struct usbnet *dev, u8 cmd, u8 reqtype, u16 value, + u16 index, const void *data, u16 size); + + if (nopm) + fn =3D aq_write_cmd_nopm; + else + fn =3D aq_write_cmd; + + ret =3D fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, + value, index, data, size); + + if (unlikely(ret < 0)) + netdev_warn(dev->net, + "Failed to write(0x%x) reg 0x%04x: %d\n", + cmd, value, ret); + + return ret; +} + +static int aqc111_write_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value, + u16 index, u16 size, void *data) +{ + return __aqc111_write_cmd(dev, cmd, value, index, size, data, 1); +} + +static int aqc111_write_cmd(struct usbnet *dev, u8 cmd, u16 value, + u16 index, u16 size, void *data) +{ + return __aqc111_write_cmd(dev, cmd, value, index, size, data, 0); +} + static const struct net_device_ops aqc111_netdev_ops =3D { .ndo_open =3D usbnet_open, .ndo_stop =3D usbnet_stop, diff --git a/drivers/net/usb/aqc111.h b/drivers/net/usb/aqc111.h new file mode 100644 index 000000000000..1698a6a104fe --- /dev/null +++ b/drivers/net/usb/aqc111.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * Aquantia Corp. Aquantia AQtion USB to 5GbE Controller + * Copyright (C) 2003-2005 David Hollis + * Copyright (C) 2005 Phil Chang + * Copyright (C) 2002-2003 TiVo Inc. + * Copyright (C) 2017-2018 ASIX + * Copyright (C) 2018 Aquantia Corp. + */ + +#ifndef __LINUX_USBNET_AQC111_H +#define __LINUX_USBNET_AQC111_H + +#define AQ_PHY_POWER 0x31 + +#define AQ_USB_PHY_SET_TIMEOUT 10000 +#define AQ_USB_SET_TIMEOUT 4000 + +#endif /* __LINUX_USBNET_AQC111_H */ + --=20 2.7.4