1 /*
2 * Ethernet over USB device
3 *
4 * Copyright (c) 2017 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define LOG_LEVEL CONFIG_USB_DEVICE_NETWORK_LOG_LEVEL
10 #include <logging/log.h>
11 LOG_MODULE_REGISTER(usb_net);
12
13 #include <init.h>
14
15 #include <net/ethernet.h>
16 #include <net_private.h>
17
18 #include <usb_device.h>
19 #include <usb_descriptor.h>
20
21 #include "netusb.h"
22
23 static struct __netusb {
24 struct net_if *iface;
25 const struct netusb_function *func;
26 } netusb;
27
netusb_send(const struct device * dev,struct net_pkt * pkt)28 static int netusb_send(const struct device *dev, struct net_pkt *pkt)
29 {
30 int ret;
31
32 ARG_UNUSED(dev);
33
34 LOG_DBG("Send pkt, len %zu", net_pkt_get_len(pkt));
35
36 if (!netusb_enabled()) {
37 LOG_ERR("interface disabled");
38 return -ENODEV;
39 }
40
41 ret = netusb.func->send_pkt(pkt);
42 if (ret) {
43 return ret;
44 }
45
46 return 0;
47 }
48
netusb_net_iface(void)49 struct net_if *netusb_net_iface(void)
50 {
51 return netusb.iface;
52 }
53
netusb_recv(struct net_pkt * pkt)54 void netusb_recv(struct net_pkt *pkt)
55 {
56 LOG_DBG("Recv pkt, len %zu", net_pkt_get_len(pkt));
57
58 if (net_recv_data(netusb.iface, pkt) < 0) {
59 LOG_ERR("Packet %p dropped by NET stack", pkt);
60 net_pkt_unref(pkt);
61 }
62 }
63
netusb_connect_media(void)64 static int netusb_connect_media(void)
65 {
66 LOG_DBG("");
67
68 if (!netusb_enabled()) {
69 LOG_ERR("interface disabled");
70 return -ENODEV;
71 }
72
73 if (!netusb.func->connect_media) {
74 return -ENOTSUP;
75 }
76
77 return netusb.func->connect_media(true);
78 }
79
netusb_disconnect_media(void)80 static int netusb_disconnect_media(void)
81 {
82 LOG_DBG("");
83
84 if (!netusb_enabled()) {
85 LOG_ERR("interface disabled");
86 return -ENODEV;
87 }
88
89 if (!netusb.func->connect_media) {
90 return -ENOTSUP;
91 }
92
93 return netusb.func->connect_media(false);
94 }
95
netusb_enable(const struct netusb_function * func)96 void netusb_enable(const struct netusb_function *func)
97 {
98 LOG_DBG("");
99
100 netusb.func = func;
101
102 net_if_up(netusb.iface);
103 netusb_connect_media();
104 }
105
netusb_disable(void)106 void netusb_disable(void)
107 {
108 LOG_DBG("");
109
110 if (!netusb_enabled()) {
111 return;
112 }
113
114 netusb.func = NULL;
115
116 netusb_disconnect_media();
117 net_if_down(netusb.iface);
118 }
119
netusb_enabled(void)120 bool netusb_enabled(void)
121 {
122 return !!netusb.func;
123 }
124
netusb_init(struct net_if * iface)125 static void netusb_init(struct net_if *iface)
126 {
127 static uint8_t mac[6] = { 0x00, 0x00, 0x5E, 0x00, 0x53, 0x00 };
128
129 LOG_DBG("netusb device initialization");
130
131 netusb.iface = iface;
132
133 ethernet_init(iface);
134 net_if_flag_set(iface, NET_IF_NO_AUTO_START);
135
136 net_if_set_link_addr(iface, mac, sizeof(mac), NET_LINK_ETHERNET);
137
138 LOG_INF("netusb initialized");
139 }
140
141 static const struct ethernet_api netusb_api_funcs = {
142 .iface_api.init = netusb_init,
143
144 .get_capabilities = NULL,
145 .send = netusb_send,
146 };
147
netusb_init_dev(const struct device * dev)148 static int netusb_init_dev(const struct device *dev)
149 {
150 ARG_UNUSED(dev);
151 return 0;
152 }
153
154 NET_DEVICE_INIT(eth_netusb, "eth_netusb", netusb_init_dev, NULL, NULL, NULL,
155 CONFIG_ETH_INIT_PRIORITY, &netusb_api_funcs, ETHERNET_L2,
156 NET_L2_GET_CTX_TYPE(ETHERNET_L2), NET_ETH_MTU);
157