1 /* $FreeBSD: head/sys/dev/usb/template/usb_template_cdce.c 246125 2013-01-30 16:05:54Z hselasky $ */
3 * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file contains the USB templates for a CDC USB ethernet device.
32 #ifdef USB_GLOBAL_INCLUDE_FILE
33 #include USB_GLOBAL_INCLUDE_FILE
35 #include <sys/stdint.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <bus/u4b/usb.h>
52 #include <bus/u4b/usbdi.h>
53 #include <bus/u4b/usb_core.h>
54 #include <bus/u4b/usb_cdc.h>
56 #include <bus/u4b/template/usb_template.h>
57 #endif /* USB_GLOBAL_INCLUDE_FILE */
62 STRING_ETH_CONTROL_INDEX
,
63 STRING_ETH_DATA_INDEX
,
64 STRING_ETH_CONFIG_INDEX
,
65 STRING_ETH_VENDOR_INDEX
,
66 STRING_ETH_PRODUCT_INDEX
,
67 STRING_ETH_SERIAL_INDEX
,
72 "2\0A\0002\0003\0004\0005\0006\0007\08\09\0A\0B"
74 #define STRING_ETH_CONTROL \
75 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 " \
76 "\0C\0o\0m\0m\0 \0I\0n\0t\0e\0r\0f\0a\0c\0e"
78 #define STRING_ETH_DATA \
79 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0D\0a\0t\0a\0 " \
80 "\0I\0n\0t\0e\0r\0f\0a\0c\0e"
82 #define STRING_ETH_CONFIG \
83 "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g"
85 #define STRING_ETH_VENDOR \
86 "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n"
88 #define STRING_ETH_PRODUCT \
89 "U\0S\0B\0 \0E\0t\0h\0e\0r\0n\0e\0t\0 \0A\0d\0a\0p\0t\0e\0r"
91 #define STRING_ETH_SERIAL \
92 "D\0e\0c\0e\0m\0b\0e\0r\0 \0002\0000\0000\0007"
94 /* make the real string descriptors */
96 USB_MAKE_STRING_DESC(STRING_MAC
, string_mac
);
97 USB_MAKE_STRING_DESC(STRING_ETH_CONTROL
, string_eth_control
);
98 USB_MAKE_STRING_DESC(STRING_ETH_DATA
, string_eth_data
);
99 USB_MAKE_STRING_DESC(STRING_ETH_CONFIG
, string_eth_config
);
100 USB_MAKE_STRING_DESC(STRING_ETH_VENDOR
, string_eth_vendor
);
101 USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT
, string_eth_product
);
102 USB_MAKE_STRING_DESC(STRING_ETH_SERIAL
, string_eth_serial
);
106 static usb_temp_get_string_desc_t eth_get_string_desc
;
108 static const struct usb_cdc_union_descriptor eth_union_desc
= {
109 .bLength
= sizeof(eth_union_desc
),
110 .bDescriptorType
= UDESC_CS_INTERFACE
,
111 .bDescriptorSubtype
= UDESCSUB_CDC_UNION
,
112 .bMasterInterface
= 0, /* this is automatically updated */
113 .bSlaveInterface
[0] = 1, /* this is automatically updated */
116 static const struct usb_cdc_header_descriptor eth_header_desc
= {
117 .bLength
= sizeof(eth_header_desc
),
118 .bDescriptorType
= UDESC_CS_INTERFACE
,
119 .bDescriptorSubtype
= UDESCSUB_CDC_HEADER
,
124 static const struct usb_cdc_ethernet_descriptor eth_enf_desc
= {
125 .bLength
= sizeof(eth_enf_desc
),
126 .bDescriptorType
= UDESC_CS_INTERFACE
,
127 .bDescriptorSubtype
= UDESCSUB_CDC_ENF
,
128 .iMacAddress
= STRING_MAC_INDEX
,
129 .bmEthernetStatistics
= {0, 0, 0, 0},
130 .wMaxSegmentSize
= {0xEA, 0x05},/* 1514 bytes */
131 .wNumberMCFilters
= {0, 0},
132 .bNumberPowerFilters
= 0,
135 static const void *eth_control_if_desc
[] = {
142 static const struct usb_temp_packet_size bulk_mps
= {
143 .mps
[USB_SPEED_FULL
] = 64,
144 .mps
[USB_SPEED_HIGH
] = 512,
147 static const struct usb_temp_packet_size intr_mps
= {
148 .mps
[USB_SPEED_FULL
] = 8,
149 .mps
[USB_SPEED_HIGH
] = 8,
152 static const struct usb_temp_endpoint_desc bulk_in_ep
= {
153 .pPacketSize
= &bulk_mps
,
154 #ifdef USB_HIP_IN_EP_0
155 .bEndpointAddress
= USB_HIP_IN_EP_0
,
157 .bEndpointAddress
= UE_DIR_IN
,
159 .bmAttributes
= UE_BULK
,
162 static const struct usb_temp_endpoint_desc bulk_out_ep
= {
163 .pPacketSize
= &bulk_mps
,
164 #ifdef USB_HIP_OUT_EP_0
165 .bEndpointAddress
= USB_HIP_OUT_EP_0
,
167 .bEndpointAddress
= UE_DIR_OUT
,
169 .bmAttributes
= UE_BULK
,
172 static const struct usb_temp_endpoint_desc intr_in_ep
= {
173 .pPacketSize
= &intr_mps
,
174 .bEndpointAddress
= UE_DIR_IN
,
175 .bmAttributes
= UE_INTERRUPT
,
178 static const struct usb_temp_endpoint_desc
*eth_intr_endpoints
[] = {
183 static const struct usb_temp_interface_desc eth_control_interface
= {
184 .ppEndpoints
= eth_intr_endpoints
,
185 .ppRawDesc
= eth_control_if_desc
,
186 .bInterfaceClass
= UICLASS_CDC
,
187 .bInterfaceSubClass
= UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL
,
188 .bInterfaceProtocol
= 0,
189 .iInterface
= STRING_ETH_CONTROL_INDEX
,
192 static const struct usb_temp_endpoint_desc
*eth_data_endpoints
[] = {
198 static const struct usb_temp_interface_desc eth_data_null_interface
= {
199 .ppEndpoints
= NULL
, /* no endpoints */
200 .bInterfaceClass
= UICLASS_CDC_DATA
,
201 .bInterfaceSubClass
= 0,
202 .bInterfaceProtocol
= 0,
203 .iInterface
= STRING_ETH_DATA_INDEX
,
206 static const struct usb_temp_interface_desc eth_data_interface
= {
207 .ppEndpoints
= eth_data_endpoints
,
208 .bInterfaceClass
= UICLASS_CDC_DATA
,
209 .bInterfaceSubClass
= UISUBCLASS_DATA
,
210 .bInterfaceProtocol
= 0,
211 .iInterface
= STRING_ETH_DATA_INDEX
,
212 .isAltInterface
= 1, /* this is an alternate setting */
215 static const struct usb_temp_interface_desc
*eth_interfaces
[] = {
216 ð_control_interface
,
217 ð_data_null_interface
,
222 static const struct usb_temp_config_desc eth_config_desc
= {
223 .ppIfaceDesc
= eth_interfaces
,
224 .bmAttributes
= UC_BUS_POWERED
,
225 .bMaxPower
= 25, /* 50 mA */
226 .iConfiguration
= STRING_ETH_CONFIG_INDEX
,
229 static const struct usb_temp_config_desc
*eth_configs
[] = {
234 const struct usb_temp_device_desc usb_template_cdce
= {
235 .getStringDesc
= ð_get_string_desc
,
236 .ppConfigDesc
= eth_configs
,
237 .idVendor
= USB_TEMPLATE_VENDOR
,
240 .bDeviceClass
= UDCLASS_COMM
,
241 .bDeviceSubClass
= 0,
242 .bDeviceProtocol
= 0,
243 .iManufacturer
= STRING_ETH_VENDOR_INDEX
,
244 .iProduct
= STRING_ETH_PRODUCT_INDEX
,
245 .iSerialNumber
= STRING_ETH_SERIAL_INDEX
,
248 /*------------------------------------------------------------------------*
249 * eth_get_string_desc
252 * NULL: Failure. No such string.
253 * Else: Success. Pointer to string descriptor is returned.
254 *------------------------------------------------------------------------*/
256 eth_get_string_desc(uint16_t lang_id
, uint8_t string_index
)
258 static const void *ptr
[STRING_ETH_MAX
] = {
259 [STRING_LANG_INDEX
] = &usb_string_lang_en
,
260 [STRING_MAC_INDEX
] = &string_mac
,
261 [STRING_ETH_CONTROL_INDEX
] = &string_eth_control
,
262 [STRING_ETH_DATA_INDEX
] = &string_eth_data
,
263 [STRING_ETH_CONFIG_INDEX
] = &string_eth_config
,
264 [STRING_ETH_VENDOR_INDEX
] = &string_eth_vendor
,
265 [STRING_ETH_PRODUCT_INDEX
] = &string_eth_product
,
266 [STRING_ETH_SERIAL_INDEX
] = &string_eth_serial
,
269 if (string_index
== 0) {
270 return (&usb_string_lang_en
);
272 if (lang_id
!= 0x0409) {
275 if (string_index
< STRING_ETH_MAX
) {
276 return (ptr
[string_index
]);