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