Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /* * Ethernet over USB device * * Copyright (c) 2017 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #define SYS_LOG_LEVEL 3 #define SYS_LOG_DOMAIN "netusb" #include <logging/sys_log.h> #include <init.h> #include <usb_device.h> #include <usb_common.h> #include <class/usb_cdc.h> #include <net_private.h> #include "../../usb_descriptor.h" #include "../../composite.h" #include "netusb.h" static struct __netusb { struct net_if *iface; bool configured; struct netusb_function *func; } netusb; static int netusb_send(struct net_if *iface, struct net_pkt *pkt) { int ret; SYS_LOG_DBG("Send pkt, len %u", net_pkt_get_len(pkt)); if (!netusb.configured) { SYS_LOG_ERR("Unconfigured device, conf %u", netusb.configured); return -ENODEV; } ret = netusb.func->send_pkt(pkt); if (ret) { return ret; } net_pkt_unref(pkt); return 0; } void netusb_recv(struct net_pkt *pkt) { SYS_LOG_DBG("Recv pkt, len %u", net_pkt_get_len(pkt)); if (net_recv_data(netusb.iface, pkt)) { SYS_LOG_ERR("Queueing packet %p failed", pkt); net_pkt_unref(pkt); } } static int netusb_connect_media(void) { if (!netusb.func->connect_media) { return -ENOTSUP; } return netusb.func->connect_media(true); } static int netusb_disconnect_media(void) { if (!netusb.func->connect_media) { return -ENOTSUP; } return netusb.func->connect_media(false); } static void netusb_status_configured(u8_t *conf) { SYS_LOG_INF("Enable configuration number %u", *conf); switch (*conf) { case 1: netusb.configured = true; net_if_up(netusb.iface); netusb_connect_media(); break; case 0: netusb.configured = false; netusb_disconnect_media(); net_if_down(netusb.iface); break; default: SYS_LOG_ERR("Wrong configuration chosen: %u", *conf); netusb.configured = false; break; } } static void netusb_status_cb(enum usb_dc_status_code status, u8_t *param) { /* Check the USB status and do needed action if required */ switch (status) { case USB_DC_ERROR: SYS_LOG_DBG("USB device error"); break; case USB_DC_RESET: SYS_LOG_DBG("USB device reset detected"); break; case USB_DC_CONNECTED: SYS_LOG_DBG("USB device connected"); break; case USB_DC_CONFIGURED: SYS_LOG_DBG("USB device configured"); netusb_status_configured(param); break; case USB_DC_DISCONNECTED: SYS_LOG_DBG("USB device disconnected"); break; case USB_DC_SUSPEND: SYS_LOG_DBG("USB device suspended"); break; case USB_DC_RESUME: SYS_LOG_DBG("USB device resumed"); break; case USB_DC_UNKNOWN: default: SYS_LOG_DBG("USB unknown state"); break; } } static int netusb_class_handler(struct usb_setup_packet *setup, s32_t *len, u8_t **data) { SYS_LOG_DBG("len %d req_type 0x%x req 0x%x conf %u", *len, setup->bmRequestType, setup->bRequest, netusb.configured); if (!netusb.configured) { SYS_LOG_ERR("Unconfigured device, conf %u", netusb.configured); return -ENODEV; } return netusb.func->class_handler(setup, len, data); } static int netusb_custom_handler(struct usb_setup_packet *setup, s32_t *len, u8_t **data) { /** * FIXME: * Keep custom handler for now in a case some data is sent * over this endpoint, at the moment return to higher layer. */ return -ENOTSUP; } #if !defined(CONFIG_USB_COMPOSITE_DEVICE) /* TODO: FIXME: correct buffer size */ static u8_t interface_data[300]; #endif static struct usb_cfg_data netusb_config = { .usb_device_description = NULL, .cb_usb_status = netusb_status_cb, .interface = { .class_handler = netusb_class_handler, .custom_handler = netusb_custom_handler, .custom_handler = NULL, .vendor_handler = NULL, .payload_data = NULL, }, }; int try_write(u8_t ep, u8_t *data, u16_t len) { u8_t tries = 10; int ret = 0; net_hexdump("USB <", data, len); while (len) { u32_t wrote; ret = usb_write(ep, data, len, &wrote); switch (ret) { case -EAGAIN: /* * In a case when host has not yet enabled endpoint * to get this message we might get No Space Available * error from the controller, try only several times. */ if (tries--) { SYS_LOG_WRN("Error: EAGAIN. Another try"); continue; } return ret; case 0: /* Check wrote bytes */ break; /* TODO: Handle other error codes */ default: return ret; } len -= wrote; data += wrote; #if VERBOSE_DEBUG SYS_LOG_DBG("Wrote %u bytes, remaining %u", wrote, len); #endif if (len) { SYS_LOG_WRN("Remaining bytes %d wrote %d", len, wrote); } } return ret; } static void netusb_init(struct net_if *iface) { static u8_t mac[6] = { 0x00, 0x00, 0x5E, 0x00, 0x53, 0x00 }; int ret; SYS_LOG_DBG("netusb device initialization"); SYS_LOG_DBG("iface %p", iface); netusb.iface = iface; SYS_LOG_DBG("iface %p", iface); net_if_set_link_addr(iface, mac, sizeof(mac), NET_LINK_ETHERNET); net_if_down(iface); #if defined(CONFIG_USB_DEVICE_NETWORK_ECM) netusb.func = &ecm_function; #endif netusb_config.endpoint = netusb.func->ep; netusb_config.num_endpoints = netusb.func->num_ep; #if defined(CONFIG_USB_COMPOSITE_DEVICE) ret = composite_add_function(&netusb_config, NETUSB_IFACE_IDX); if (ret < 0) { SYS_LOG_ERR("Failed to add CDC ECM function"); return; } #else /* CONFIG_USB_COMPOSITE_DEVICE */ netusb_config.interface.payload_data = interface_data; netusb_config.usb_device_description = usb_get_device_descriptor(); ret = usb_set_config(&netusb_config); if (ret < 0) { SYS_LOG_ERR("Failed to configure USB device"); return; } ret = usb_enable(&netusb_config); if (ret < 0) { SYS_LOG_ERR("Failed to enable USB"); return; } #endif /* CONFIG_USB_COMPOSITE_DEVICE */ SYS_LOG_INF("netusb initialized"); } static struct net_if_api api_funcs = { .init = netusb_init, .send = netusb_send, }; static int netusb_init_dev(struct device *dev) { ARG_UNUSED(dev); return 0; } NET_DEVICE_INIT(eth_netusb, "eth_netusb", netusb_init_dev, NULL, NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs, ETHERNET_L2, NET_L2_GET_CTX_TYPE(ETHERNET_L2), 1500); |