]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/template/usb_template_cdce.c
sys/dev: further adoption of SPDX licensing ID tags.
[FreeBSD/FreeBSD.git] / sys / dev / usb / template / usb_template_cdce.c
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /*
31  * This file contains the USB templates for a CDC USB ethernet device.
32  */
33
34 #ifdef USB_GLOBAL_INCLUDE_FILE
35 #include USB_GLOBAL_INCLUDE_FILE
36 #else
37 #include <sys/stdint.h>
38 #include <sys/stddef.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/bus.h>
45 #include <sys/module.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/sx.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
54 #include <sys/priv.h>
55
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_cdc.h>
60
61 #include <dev/usb/template/usb_template.h>
62 #endif                  /* USB_GLOBAL_INCLUDE_FILE */
63
64 enum {
65         STRING_LANG_INDEX,
66         STRING_MAC_INDEX,
67         STRING_ETH_CONTROL_INDEX,
68         STRING_ETH_DATA_INDEX,
69         STRING_ETH_CONFIG_INDEX,
70         STRING_ETH_VENDOR_INDEX,
71         STRING_ETH_PRODUCT_INDEX,
72         STRING_ETH_SERIAL_INDEX,
73         STRING_ETH_MAX,
74 };
75
76 #define STRING_MAC \
77   "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B"
78
79 #define STRING_ETH_CONTROL \
80   "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \
81   "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
82
83 #define STRING_ETH_DATA \
84   "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \
85   "\0I\0n\0t\0e\0r\0f\0a\0c\0e"
86
87 #define STRING_ETH_CONFIG \
88   "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g"
89
90 #define STRING_ETH_VENDOR \
91   "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n"
92
93 #define STRING_ETH_PRODUCT \
94   "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0A\0d\0a\0p\0t\0e\0r"
95
96 #define STRING_ETH_SERIAL \
97   "D\0e\0c\0e\0m\0b\0e\0r\0 \0002\0000\0000\0007"
98
99 /* make the real string descriptors */
100
101 USB_MAKE_STRING_DESC(STRING_MAC, string_mac);
102 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control);
103 USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data);
104 USB_MAKE_STRING_DESC(STRING_ETH_CONFIG, string_eth_config);
105 USB_MAKE_STRING_DESC(STRING_ETH_VENDOR, string_eth_vendor);
106 USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT, string_eth_product);
107 USB_MAKE_STRING_DESC(STRING_ETH_SERIAL, string_eth_serial);
108
109 /* prototypes */
110
111 static usb_temp_get_string_desc_t eth_get_string_desc;
112
113 static const struct usb_cdc_union_descriptor eth_union_desc = {
114         .bLength = sizeof(eth_union_desc),
115         .bDescriptorType = UDESC_CS_INTERFACE,
116         .bDescriptorSubtype = UDESCSUB_CDC_UNION,
117         .bMasterInterface = 0,          /* this is automatically updated */
118         .bSlaveInterface[0] = 1,        /* this is automatically updated */
119 };
120
121 static const struct usb_cdc_header_descriptor eth_header_desc = {
122         .bLength = sizeof(eth_header_desc),
123         .bDescriptorType = UDESC_CS_INTERFACE,
124         .bDescriptorSubtype = UDESCSUB_CDC_HEADER,
125         .bcdCDC[0] = 0x10,
126         .bcdCDC[1] = 0x01,
127 };
128
129 static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
130         .bLength = sizeof(eth_enf_desc),
131         .bDescriptorType = UDESC_CS_INTERFACE,
132         .bDescriptorSubtype = UDESCSUB_CDC_ENF,
133         .iMacAddress = STRING_MAC_INDEX,
134         .bmEthernetStatistics = {0, 0, 0, 0},
135         .wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
136         .wNumberMCFilters = {0, 0},
137         .bNumberPowerFilters = 0,
138 };
139
140 static const void *eth_control_if_desc[] = {
141         &eth_union_desc,
142         &eth_header_desc,
143         &eth_enf_desc,
144         NULL,
145 };
146
147 static const struct usb_temp_packet_size bulk_mps = {
148         .mps[USB_SPEED_FULL] = 64,
149         .mps[USB_SPEED_HIGH] = 512,
150 };
151
152 static const struct usb_temp_packet_size intr_mps = {
153         .mps[USB_SPEED_FULL] = 8,
154         .mps[USB_SPEED_HIGH] = 8,
155 };
156
157 static const struct usb_temp_endpoint_desc bulk_in_ep = {
158         .pPacketSize = &bulk_mps,
159 #ifdef USB_HIP_IN_EP_0
160         .bEndpointAddress = USB_HIP_IN_EP_0,
161 #else
162         .bEndpointAddress = UE_DIR_IN,
163 #endif
164         .bmAttributes = UE_BULK,
165 };
166
167 static const struct usb_temp_endpoint_desc bulk_out_ep = {
168         .pPacketSize = &bulk_mps,
169 #ifdef USB_HIP_OUT_EP_0
170         .bEndpointAddress = USB_HIP_OUT_EP_0,
171 #else
172         .bEndpointAddress = UE_DIR_OUT,
173 #endif
174         .bmAttributes = UE_BULK,
175 };
176
177 static const struct usb_temp_endpoint_desc intr_in_ep = {
178         .pPacketSize = &intr_mps,
179         .bEndpointAddress = UE_DIR_IN,
180         .bmAttributes = UE_INTERRUPT,
181 };
182
183 static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
184         &intr_in_ep,
185         NULL,
186 };
187
188 static const struct usb_temp_interface_desc eth_control_interface = {
189         .ppEndpoints = eth_intr_endpoints,
190         .ppRawDesc = eth_control_if_desc,
191         .bInterfaceClass = UICLASS_CDC,
192         .bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
193         .bInterfaceProtocol = 0,
194         .iInterface = STRING_ETH_CONTROL_INDEX,
195 };
196
197 static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
198         &bulk_in_ep,
199         &bulk_out_ep,
200         NULL,
201 };
202
203 static const struct usb_temp_interface_desc eth_data_null_interface = {
204         .ppEndpoints = NULL,            /* no endpoints */
205         .bInterfaceClass = UICLASS_CDC_DATA,
206         .bInterfaceSubClass = 0,
207         .bInterfaceProtocol = 0,
208         .iInterface = STRING_ETH_DATA_INDEX,
209 };
210
211 static const struct usb_temp_interface_desc eth_data_interface = {
212         .ppEndpoints = eth_data_endpoints,
213         .bInterfaceClass = UICLASS_CDC_DATA,
214         .bInterfaceSubClass = UISUBCLASS_DATA,
215         .bInterfaceProtocol = 0,
216         .iInterface = STRING_ETH_DATA_INDEX,
217         .isAltInterface = 1,            /* this is an alternate setting */
218 };
219
220 static const struct usb_temp_interface_desc *eth_interfaces[] = {
221         &eth_control_interface,
222         &eth_data_null_interface,
223         &eth_data_interface,
224         NULL,
225 };
226
227 static const struct usb_temp_config_desc eth_config_desc = {
228         .ppIfaceDesc = eth_interfaces,
229         .bmAttributes = UC_BUS_POWERED,
230         .bMaxPower = 25,                /* 50 mA */
231         .iConfiguration = STRING_ETH_CONFIG_INDEX,
232 };
233
234 static const struct usb_temp_config_desc *eth_configs[] = {
235         &eth_config_desc,
236         NULL,
237 };
238
239 const struct usb_temp_device_desc usb_template_cdce = {
240         .getStringDesc = &eth_get_string_desc,
241         .ppConfigDesc = eth_configs,
242         .idVendor = USB_TEMPLATE_VENDOR,
243         .idProduct = 0x0001,
244         .bcdDevice = 0x0100,
245         .bDeviceClass = UDCLASS_COMM,
246         .bDeviceSubClass = 0,
247         .bDeviceProtocol = 0,
248         .iManufacturer = STRING_ETH_VENDOR_INDEX,
249         .iProduct = STRING_ETH_PRODUCT_INDEX,
250         .iSerialNumber = STRING_ETH_SERIAL_INDEX,
251 };
252
253 /*------------------------------------------------------------------------*
254  *      eth_get_string_desc
255  *
256  * Return values:
257  * NULL: Failure. No such string.
258  * Else: Success. Pointer to string descriptor is returned.
259  *------------------------------------------------------------------------*/
260 static const void *
261 eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
262 {
263         static const void *ptr[STRING_ETH_MAX] = {
264                 [STRING_LANG_INDEX] = &usb_string_lang_en,
265                 [STRING_MAC_INDEX] = &string_mac,
266                 [STRING_ETH_CONTROL_INDEX] = &string_eth_control,
267                 [STRING_ETH_DATA_INDEX] = &string_eth_data,
268                 [STRING_ETH_CONFIG_INDEX] = &string_eth_config,
269                 [STRING_ETH_VENDOR_INDEX] = &string_eth_vendor,
270                 [STRING_ETH_PRODUCT_INDEX] = &string_eth_product,
271                 [STRING_ETH_SERIAL_INDEX] = &string_eth_serial,
272         };
273
274         if (string_index == 0) {
275                 return (&usb_string_lang_en);
276         }
277         if (lang_id != 0x0409) {
278                 return (NULL);
279         }
280         if (string_index < STRING_ETH_MAX) {
281                 return (ptr[string_index]);
282         }
283         return (NULL);
284 }