123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825 |
- /*
- * Copyright (c) 2024, sakumisu
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #include "usbh_core.h"
- #include "usbh_asix.h"
- #include "usb_cdc.h"
- #undef USB_DBG_TAG
- #define USB_DBG_TAG "asix"
- #include "usb_log.h"
- #define DEV_FORMAT "/dev/asix"
- static struct usbh_asix g_asix_class;
- static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_rx_buffer[CONFIG_USBHOST_ASIX_ETH_MAX_TX_SIZE];
- static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_tx_buffer[CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE];
- static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_inttx_buffer[16];
- USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_asix_buf[32];
- #define ETH_ALEN 6
- #define PHY_MODE_MARVELL 0x0000
- #define MII_MARVELL_LED_CTRL 0x0018
- #define MII_MARVELL_STATUS 0x001b
- #define MII_MARVELL_CTRL 0x0014
- #define MARVELL_LED_MANUAL 0x0019
- #define MARVELL_STATUS_HWCFG 0x0004
- #define MARVELL_CTRL_TXDELAY 0x0002
- #define MARVELL_CTRL_RXDELAY 0x0080
- #define PHY_MODE_RTL8211CL 0x000C
- #define AX88772A_PHY14H 0x14
- #define AX88772A_PHY14H_DEFAULT 0x442C
- #define AX88772A_PHY15H 0x15
- #define AX88772A_PHY15H_DEFAULT 0x03C8
- #define AX88772A_PHY16H 0x16
- #define AX88772A_PHY16H_DEFAULT 0x4044
- #define SPEED_100 0
- #define SPEED_10 1
- static int usbh_asix_read_cmd(struct usbh_asix *asix_class,
- uint8_t cmd,
- uint16_t value,
- uint16_t index,
- void *data,
- uint16_t size)
- {
- struct usb_setup_packet *setup;
- int ret;
- if (!asix_class || !asix_class->hport) {
- return -USB_ERR_INVAL;
- }
- setup = asix_class->hport->setup;
- setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
- setup->bRequest = cmd;
- setup->wValue = value;
- setup->wIndex = index;
- setup->wLength = size;
- ret = usbh_control_transfer(asix_class->hport, setup, g_asix_buf);
- if (ret < 8) {
- return ret;
- }
- memcpy(data, g_asix_buf, ret - 8);
- return ret;
- }
- static int usbh_asix_write_cmd(struct usbh_asix *asix_class,
- uint8_t cmd,
- uint16_t value,
- uint16_t index,
- void *data,
- uint16_t size)
- {
- struct usb_setup_packet *setup;
- if (!asix_class || !asix_class->hport) {
- return -USB_ERR_INVAL;
- }
- setup = asix_class->hport->setup;
- setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
- setup->bRequest = cmd;
- setup->wValue = value;
- setup->wIndex = index;
- setup->wLength = size;
- if (data && size) {
- memcpy(g_asix_buf, data, size);
- return usbh_control_transfer(asix_class->hport, setup, g_asix_buf);
- } else {
- return usbh_control_transfer(asix_class->hport, setup, NULL);
- }
- }
- static int usbh_asix_mdio_write(struct usbh_asix *asix_class, int phy_id, int loc, int val)
- {
- uint8_t smsr;
- uint16_t res = (uint16_t)val;
- int ret;
- for (uint8_t i = 0; i < 10; i++) {
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_SET_SW_MII, 0, 0, NULL, 0);
- if (ret < 0) {
- return ret;
- }
- usb_osal_msleep(1);
- ret = usbh_asix_read_cmd(asix_class, AX_CMD_STATMNGSTS_REG, 0, 0, &smsr, 1);
- if (ret < 0) {
- return ret;
- }
- if (smsr & AX_HOST_EN) {
- break;
- }
- }
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_MII_REG, phy_id, loc, &res, 2);
- if (ret < 0) {
- return ret;
- }
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_SET_HW_MII, 0, 0, NULL, 0);
- if (ret < 0) {
- return ret;
- }
- return 0;
- }
- static int usbh_asix_mdio_read(struct usbh_asix *asix_class, int phy_id, int loc)
- {
- uint8_t smsr;
- uint16_t res;
- int ret;
- for (uint8_t i = 0; i < 10; i++) {
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_SET_SW_MII, 0, 0, NULL, 0);
- if (ret < 0) {
- return ret;
- }
- usb_osal_msleep(1);
- ret = usbh_asix_read_cmd(asix_class, AX_CMD_STATMNGSTS_REG, 0, 0, &smsr, 1);
- if (ret < 0) {
- return ret;
- }
- if (smsr & AX_HOST_EN) {
- break;
- }
- }
- ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_MII_REG, phy_id, loc, &res, 2);
- if (ret < 0) {
- return ret;
- }
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_SET_HW_MII, 0, 0, NULL, 0);
- if (ret < 0) {
- return ret;
- }
- return res;
- }
- static int usbh_asix_read_phy_addr(struct usbh_asix *asix_class, bool internal)
- {
- int ret, offset;
- uint8_t buf[2];
- ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_PHY_ID, 0, 0, buf, 2);
- if (ret < 0) {
- return ret;
- }
- offset = (internal ? 1 : 0);
- ret = buf[offset];
- USB_LOG_INFO("%s PHY address 0x%x\r\n", internal ? "internal" : "external", ret);
- return ret;
- }
- static int usbh_asix_sw_reset(struct usbh_asix *asix_class, uint8_t flags)
- {
- int ret;
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_SW_RESET, flags, 0, NULL, 0);
- if (ret < 0)
- USB_LOG_ERR("Failed to send software reset: %d\r\n", ret);
- return ret;
- }
- static uint16_t usbh_asix_read_rx_ctl(struct usbh_asix *asix_class)
- {
- uint16_t v;
- int ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_RX_CTL, 0, 0, &v, 2);
- if (ret < 0) {
- return ret;
- }
- return v;
- }
- static int usbh_asix_write_rx_ctl(struct usbh_asix *asix_class, uint16_t mode)
- {
- int ret;
- USB_LOG_DBG("asix_write_rx_ctl() - mode = 0x%04x\r\n", mode);
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_RX_CTL, mode, 0, NULL, 0);
- if (ret < 0)
- USB_LOG_ERR("Failed to write RX_CTL mode to 0x%04x: %02x\r\n",
- mode, ret);
- return ret;
- }
- static uint16_t usbh_asix_read_medium_status(struct usbh_asix *asix_class)
- {
- uint16_t v;
- int ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_MEDIUM_STATUS, 0, 0, &v, 2);
- if (ret < 0) {
- USB_LOG_ERR("Error reading Medium Status register: %02x\r\n",
- ret);
- return ret; /* TODO: callers not checking for error ret */
- }
- return v;
- }
- static int usbh_asix_write_medium_mode(struct usbh_asix *asix_class, uint16_t mode)
- {
- int ret;
- USB_LOG_DBG("asix_write_medium_mode() - mode = 0x%04x\r\n", mode);
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, NULL, 0);
- if (ret < 0)
- USB_LOG_ERR("Failed to write Medium Mode mode to 0x%04x: %02x\r\n",
- mode, ret);
- return ret;
- }
- static int usbh_asix_write_gpio(struct usbh_asix *asix_class, uint16_t value, int sleep)
- {
- int ret;
- USB_LOG_DBG("asix_write_gpio() - value = 0x%04x\r\n", value);
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_GPIOS, value, 0, NULL, 0);
- if (ret < 0)
- USB_LOG_ERR("Failed to write GPIO value 0x%04x: %d\r\n",
- value, ret);
- if (sleep)
- usb_osal_msleep(sleep);
- return ret;
- }
- /*
- * AX88772 & AX88178 have a 16-bit RX_CTL value
- */
- static void usbh_asix_set_multicast(struct usbh_asix *asix_class)
- {
- uint16_t rx_ctl = AX_DEFAULT_RX_CTL | AX_RX_CTL_AM;
- #if CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE == 4096
- rx_ctl |= AX_RX_CTL_MFB_4096;
- #elif CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE == 8192
- rx_ctl |= AX_RX_CTL_MFB_8192;
- #elif CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE == 16384
- rx_ctl |= AX_RX_CTL_MFB_16384;
- #else
- rx_ctl |= AX_RX_CTL_MFB_2048;
- #endif
- const uint8_t multi_filter[] = { 0x00, 0x00, 0x20, 0x80, 0x00, 0x00, 0x00, 0x40 };
- usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_MULTI_FILTER, 0, 0, (uint8_t *)multi_filter, AX_MCAST_FILTER_SIZE);
- usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, NULL, 0);
- }
- static int usbh_ax88772_hw_reset(struct usbh_asix *asix_class)
- {
- uint16_t rx_ctl;
- int ret;
- ret = usbh_asix_write_gpio(asix_class, AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5);
- if (ret < 0)
- goto out;
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_SW_PHY_SELECT, asix_class->embd_phy,
- 0, NULL, 0);
- if (ret < 0) {
- USB_LOG_ERR("Select PHY #1 failed: %d\r\n", ret);
- goto out;
- }
- if (asix_class->embd_phy) {
- ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPPD);
- if (ret < 0)
- goto out;
- usb_osal_msleep(10);
- ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_CLEAR);
- if (ret < 0)
- goto out;
- usb_osal_msleep(60);
- ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPRL | AX_SWRESET_PRL);
- if (ret < 0)
- goto out;
- } else {
- ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPPD | AX_SWRESET_PRL);
- if (ret < 0)
- goto out;
- }
- usb_osal_msleep(150);
- ret = usbh_asix_write_rx_ctl(asix_class, AX_DEFAULT_RX_CTL);
- if (ret < 0)
- goto out;
- ret = usbh_asix_write_medium_mode(asix_class, AX88772_MEDIUM_DEFAULT);
- if (ret < 0)
- goto out;
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_IPG0,
- AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
- AX88772_IPG2_DEFAULT, NULL, 0);
- if (ret < 0) {
- USB_LOG_ERR("Write IPG,IPG1,IPG2 failed: %d\r\n", ret);
- goto out;
- }
- /* Rewrite MAC address */
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_NODE_ID, 0, 0, asix_class->mac, ETH_ALEN);
- if (ret < 0)
- goto out;
- /* Set RX_CTL to default values with 2k buffer, and enable cactus */
- ret = usbh_asix_write_rx_ctl(asix_class, AX_DEFAULT_RX_CTL);
- if (ret < 0)
- goto out;
- rx_ctl = usbh_asix_read_rx_ctl(asix_class);
- USB_LOG_INFO("RX_CTL is 0x%04x after all initializations\r\n",
- rx_ctl);
- rx_ctl = usbh_asix_read_medium_status(asix_class);
- USB_LOG_INFO("Medium Status is 0x%04x after all initializations\r\n",
- rx_ctl);
- return 0;
- out:
- return ret;
- }
- static int usbh_ax88772a_hw_reset(struct usbh_asix *asix_class)
- {
- uint16_t rx_ctl, phy14h, phy15h, phy16h;
- int ret;
- ret = usbh_asix_write_gpio(asix_class, AX_GPIO_RSE, 5);
- if (ret < 0)
- goto out;
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_SW_PHY_SELECT, asix_class->embd_phy | AX_PHYSEL_SSEN, 0, NULL, 0);
- if (ret < 0) {
- USB_LOG_ERR("Select PHY #1 failed: %d\r\n", ret);
- goto out;
- }
- usb_osal_msleep(10);
- ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPPD | AX_SWRESET_IPRL);
- if (ret < 0)
- goto out;
- usb_osal_msleep(10);
- ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPRL);
- if (ret < 0)
- goto out;
- usb_osal_msleep(160);
- ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_CLEAR);
- if (ret < 0)
- goto out;
- ret = usbh_asix_sw_reset(asix_class, AX_SWRESET_IPRL);
- if (ret < 0)
- goto out;
- usb_osal_msleep(200);
- if (asix_class->chipcode == AX_AX88772B_CHIPCODE) {
- ret = usbh_asix_write_cmd(asix_class, AX_QCTCTRL, 0x8000, 0x8001, NULL, 0);
- if (ret < 0) {
- USB_LOG_ERR("Write BQ setting failed: %d\r\n", ret);
- goto out;
- }
- } else if (asix_class->chipcode == AX_AX88772A_CHIPCODE) {
- /* Check if the PHY registers have default settings */
- phy14h = usbh_asix_mdio_read(asix_class, asix_class->phy_addr,
- AX88772A_PHY14H);
- phy15h = usbh_asix_mdio_read(asix_class, asix_class->phy_addr,
- AX88772A_PHY15H);
- phy16h = usbh_asix_mdio_read(asix_class, asix_class->phy_addr,
- AX88772A_PHY16H);
- USB_LOG_DBG("772a_hw_reset: MR20=0x%x MR21=0x%x MR22=0x%x\r\n",
- phy14h, phy15h, phy16h);
- /* Restore PHY registers default setting if not */
- if (phy14h != AX88772A_PHY14H_DEFAULT)
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr,
- AX88772A_PHY14H,
- AX88772A_PHY14H_DEFAULT);
- if (phy15h != AX88772A_PHY15H_DEFAULT)
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr,
- AX88772A_PHY15H,
- AX88772A_PHY15H_DEFAULT);
- if (phy16h != AX88772A_PHY16H_DEFAULT)
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr,
- AX88772A_PHY16H,
- AX88772A_PHY16H_DEFAULT);
- }
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_IPG0,
- AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
- AX88772_IPG2_DEFAULT, NULL, 0);
- if (ret < 0) {
- USB_LOG_ERR("Write IPG,IPG1,IPG2 failed: %d\r\n", ret);
- goto out;
- }
- /* Rewrite MAC address */
- ret = usbh_asix_write_cmd(asix_class, AX_CMD_WRITE_NODE_ID, 0, 0, asix_class->mac, ETH_ALEN);
- if (ret < 0)
- goto out;
- /* Set RX_CTL to default values with 2k buffer, and enable cactus */
- ret = usbh_asix_write_rx_ctl(asix_class, AX_DEFAULT_RX_CTL);
- if (ret < 0)
- goto out;
- ret = usbh_asix_write_medium_mode(asix_class, AX88772_MEDIUM_DEFAULT);
- if (ret < 0)
- return ret;
- /* Set RX_CTL to default values with 2k buffer, and enable cactus */
- ret = usbh_asix_write_rx_ctl(asix_class, AX_DEFAULT_RX_CTL);
- if (ret < 0)
- goto out;
- rx_ctl = usbh_asix_read_rx_ctl(asix_class);
- USB_LOG_INFO("RX_CTL is 0x%04x after all initializations\r\n", rx_ctl);
- rx_ctl = usbh_asix_read_medium_status(asix_class);
- USB_LOG_INFO("Medium Status is 0x%04x after all initializations\r\n", rx_ctl);
- return 0;
- out:
- return ret;
- }
- static void usbh_ax88772_mac_link_down(struct usbh_asix *asix_class)
- {
- usbh_asix_write_medium_mode(asix_class, 0);
- }
- static void usbh_ax88772_mac_link_up(struct usbh_asix *asix_class, int speed, int duplex, bool tx_pause, bool rx_pause)
- {
- uint16_t m = AX_MEDIUM_AC | AX_MEDIUM_RE;
- m |= duplex ? AX_MEDIUM_FD : 0;
- switch (speed) {
- case SPEED_100:
- m |= AX_MEDIUM_PS;
- break;
- case SPEED_10:
- break;
- default:
- return;
- }
- if (tx_pause)
- m |= AX_MEDIUM_TFC;
- if (rx_pause)
- m |= AX_MEDIUM_RFC;
- usbh_asix_write_medium_mode(asix_class, m);
- }
- static int usbh_asix_connect(struct usbh_hubport *hport, uint8_t intf)
- {
- struct usb_endpoint_descriptor *ep_desc;
- int ret;
- struct usbh_asix *asix_class = &g_asix_class;
- memset(asix_class, 0, sizeof(struct usbh_asix));
- asix_class->hport = hport;
- asix_class->intf = intf;
- hport->config.intf[intf].priv = asix_class;
- if ((hport->device_desc.idVendor == 0x0b95) && (hport->device_desc.idProduct == 0x772b)) {
- asix_class->name = "ASIX AX88772B";
- } else if ((hport->device_desc.idVendor == 0x0b95) && (hport->device_desc.idProduct == 0x7720)) {
- asix_class->name = "ASIX AX88772";
- } else if ((hport->device_desc.idVendor == 0x0b95) && (hport->device_desc.idProduct == 0x1780)) {
- asix_class->name = "ASIX AX88178";
- }
- for (uint8_t i = 0; i < (ETH_ALEN >> 1); i++) {
- ret = usbh_asix_read_cmd(asix_class, AX_CMD_READ_EEPROM,
- 0x04 + i, 0, &asix_class->mac[i * 2], 2);
- if (ret < 0) {
- return ret;
- }
- }
- USB_LOG_INFO("asix MAC address %02x:%02x:%02x:%02x:%02x:%02x\r\n",
- asix_class->mac[0],
- asix_class->mac[1],
- asix_class->mac[2],
- asix_class->mac[3],
- asix_class->mac[4],
- asix_class->mac[5]);
- ret = usbh_asix_read_phy_addr(asix_class, true);
- if (ret < 0) {
- USB_LOG_ERR("Failed to read phy addr: %d\r\n", ret);
- return ret;
- }
- asix_class->phy_addr = ret;
- asix_class->embd_phy = ((ret & 0x1f) == AX_EMBD_PHY_ADDR);
- ret = usbh_asix_read_cmd(asix_class, AX_CMD_STATMNGSTS_REG, 0, 0, &asix_class->chipcode, 1);
- if (ret < 0) {
- USB_LOG_ERR("Failed to read STATMNGSTS_REG: %d\r\n", ret);
- return ret;
- }
- asix_class->chipcode &= AX_CHIPCODE_MASK;
- USB_LOG_INFO("asix chipcode 0x%x\r\n", asix_class->chipcode);
- if (asix_class->chipcode == AX_AX88772_CHIPCODE) {
- usbh_ax88772_hw_reset(asix_class);
- } else {
- usbh_ax88772a_hw_reset(asix_class);
- }
- for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
- ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
- if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
- if (ep_desc->bEndpointAddress & 0x80) {
- USBH_EP_INIT(asix_class->intin, ep_desc);
- } else {
- return -USB_ERR_NOTSUPP;
- }
- } else {
- if (ep_desc->bEndpointAddress & 0x80) {
- USBH_EP_INIT(asix_class->bulkin, ep_desc);
- } else {
- USBH_EP_INIT(asix_class->bulkout, ep_desc);
- }
- }
- }
- if (asix_class->chipcode == AX_AX88772B_CHIPCODE) {
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0);
- usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 0);
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0x8200);
- usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 0);
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0x3900);
- usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 0);
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0x3100);
- usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 4);
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 4, 0x01e1);
- usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 1);
- usbh_asix_mdio_write(asix_class, asix_class->phy_addr, 0, 0x3300);
- usbh_asix_mdio_read(asix_class, asix_class->phy_addr, 0);
- }
- USB_LOG_INFO("Init %s done\r\n", asix_class->name);
- strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
- USB_LOG_INFO("Register ASIX Class:%s\r\n", hport->config.intf[intf].devname);
- usbh_asix_run(asix_class);
- return ret;
- }
- static int usbh_asix_disconnect(struct usbh_hubport *hport, uint8_t intf)
- {
- int ret = 0;
- struct usbh_asix *asix_class = (struct usbh_asix *)hport->config.intf[intf].priv;
- if (asix_class) {
- if (asix_class->bulkin) {
- usbh_kill_urb(&asix_class->bulkin_urb);
- }
- if (asix_class->bulkout) {
- usbh_kill_urb(&asix_class->bulkout_urb);
- }
- if (asix_class->intin) {
- usbh_kill_urb(&asix_class->intin_urb);
- }
- if (hport->config.intf[intf].devname[0] != '\0') {
- USB_LOG_INFO("Unregister ASIX Class:%s\r\n", hport->config.intf[intf].devname);
- usbh_asix_stop(asix_class);
- }
- memset(asix_class, 0, sizeof(struct usbh_asix));
- }
- return ret;
- }
- int usbh_asix_get_connect_status(struct usbh_asix *asix_class)
- {
- int ret;
- usbh_int_urb_fill(&asix_class->intin_urb, asix_class->hport, asix_class->intin, g_asix_inttx_buffer, 8, USB_OSAL_WAITING_FOREVER, NULL, NULL);
- ret = usbh_submit_urb(&asix_class->intin_urb);
- if (ret < 0) {
- return ret;
- }
- if (g_asix_inttx_buffer[1] == 0x00) {
- if (g_asix_inttx_buffer[2] & 0x01) {
- asix_class->connect_status = true;
- usbh_ax88772_mac_link_up(asix_class, SPEED_100, 1, 1, 1);
- usbh_asix_set_multicast(asix_class);
- } else {
- asix_class->connect_status = false;
- usbh_ax88772_mac_link_down(asix_class);
- }
- }
- return 0;
- }
- void usbh_asix_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
- {
- uint32_t g_asix_rx_length;
- int ret;
- uint16_t len;
- uint16_t len_crc;
- uint32_t data_offset;
- #if CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE <= (16 * 1024)
- uint32_t transfer_size = CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE;
- #else
- uint32_t transfer_size = (16 * 1024);
- #endif
- (void)CONFIG_USB_OSAL_THREAD_GET_ARGV;
- USB_LOG_INFO("Create asix rx thread\r\n");
- // clang-format off
- find_class:
- // clang-format on
- g_asix_class.connect_status = false;
- if (usbh_find_class_instance("/dev/asix") == NULL) {
- goto delete;
- }
- while (g_asix_class.connect_status == false) {
- ret = usbh_asix_get_connect_status(&g_asix_class);
- if (ret < 0) {
- usb_osal_msleep(100);
- goto find_class;
- }
- usb_osal_msleep(128);
- }
- g_asix_rx_length = 0;
- while (1) {
- usbh_bulk_urb_fill(&g_asix_class.bulkin_urb, g_asix_class.hport, g_asix_class.bulkin, &g_asix_rx_buffer[g_asix_rx_length], transfer_size, USB_OSAL_WAITING_FOREVER, NULL, NULL);
- ret = usbh_submit_urb(&g_asix_class.bulkin_urb);
- if (ret < 0) {
- goto find_class;
- }
- g_asix_rx_length += g_asix_class.bulkin_urb.actual_length;
- /* A transfer is complete because last packet is a short packet.
- * Short packet is not zero, match g_asix_rx_length % USB_GET_MAXPACKETSIZE(g_asix_class.bulkin->wMaxPacketSize).
- * Short packet is zero, check if g_asix_class.bulkin_urb.actual_length < transfer_size, for example transfer is complete with size is 1024 < 2048.
- */
- if (g_asix_rx_length % USB_GET_MAXPACKETSIZE(g_asix_class.bulkin->wMaxPacketSize) ||
- (g_asix_class.bulkin_urb.actual_length < transfer_size)) {
- USB_LOG_DBG("rxlen:%d\r\n", g_asix_rx_length);
- data_offset = 0;
- while (g_asix_rx_length > 0) {
- len = ((uint16_t)g_asix_rx_buffer[data_offset + 0] | ((uint16_t)(g_asix_rx_buffer[data_offset + 1]) << 8)) & 0x7ff;
- len_crc = g_asix_rx_buffer[data_offset + 2] | ((uint16_t)(g_asix_rx_buffer[data_offset + 3]) << 8);
- if (len != (~len_crc & 0x7ff)) {
- USB_LOG_ERR("rx header error\r\n");
- g_asix_rx_length = 0;
- continue;
- }
- uint8_t *buf = (uint8_t *)&g_asix_rx_buffer[data_offset + 4];
- usbh_asix_eth_input(buf, len);
- g_asix_rx_length -= (len + 4);
- data_offset += (len + 4);
- if (g_asix_rx_length < 4) {
- g_asix_rx_length = 0;
- }
- }
- } else {
- #if CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE <= (16 * 1024)
- if (g_asix_rx_length == CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE) {
- #else
- if ((g_asix_rx_length + (16 * 1024)) > CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE) {
- #endif
- USB_LOG_ERR("Rx packet is overflow, please reduce tcp window size or increase CONFIG_USBHOST_ASIX_ETH_MAX_RX_SIZE\r\n");
- while (1) {
- }
- }
- }
- }
- // clang-format off
- delete:
- USB_LOG_INFO("Delete asix rx thread\r\n");
- usb_osal_thread_delete(NULL);
- // clang-format on
- }
- uint8_t *usbh_asix_get_eth_txbuf(void)
- {
- return &g_asix_tx_buffer[4];
- }
- int usbh_asix_eth_output(uint32_t buflen)
- {
- uint16_t actual_len;
- if (g_asix_class.connect_status == false) {
- return -USB_ERR_NOTCONN;
- }
- g_asix_tx_buffer[0] = buflen & 0xff;
- g_asix_tx_buffer[1] = (buflen >> 8) & 0xff;
- g_asix_tx_buffer[2] = ~g_asix_tx_buffer[0];
- g_asix_tx_buffer[3] = ~g_asix_tx_buffer[1];
- if (!(buflen + 4) % USB_GET_MAXPACKETSIZE(g_asix_class.bulkout->wMaxPacketSize)) {
- USB_LOG_DBG("txlen:%d\r\n", buflen + 8);
- g_asix_tx_buffer[buflen + 4 + 0] = 0x00;
- g_asix_tx_buffer[buflen + 4 + 1] = 0x00;
- g_asix_tx_buffer[buflen + 4 + 2] = 0xff;
- g_asix_tx_buffer[buflen + 4 + 3] = 0xff;
- actual_len = buflen + 8;
- } else {
- USB_LOG_DBG("txlen:%d\r\n", buflen + 4);
- actual_len = buflen + 4;
- }
- usbh_bulk_urb_fill(&g_asix_class.bulkout_urb, g_asix_class.hport, g_asix_class.bulkout, g_asix_tx_buffer, actual_len, USB_OSAL_WAITING_FOREVER, NULL, NULL);
- return usbh_submit_urb(&g_asix_class.bulkout_urb);
- }
- __WEAK void usbh_asix_run(struct usbh_asix *asix_class)
- {
- (void)asix_class;
- }
- __WEAK void usbh_asix_stop(struct usbh_asix *asix_class)
- {
- (void)asix_class;
- }
- static const uint16_t asix_id_table[][2] = {
- { 0x0B95, 0x772B },
- { 0x0B95, 0x7720 },
- { 0, 0 },
- };
- static const struct usbh_class_driver asix_class_driver = {
- .driver_name = "asix",
- .connect = usbh_asix_connect,
- .disconnect = usbh_asix_disconnect
- };
- CLASS_INFO_DEFINE const struct usbh_class_info asix_class_info = {
- .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
- .bInterfaceClass = 0xff,
- .bInterfaceSubClass = 0x00,
- .bInterfaceProtocol = 0x00,
- .id_table = asix_id_table,
- .class_driver = &asix_class_driver
- };
|