xref: /freebsd/sys/dev/usb/template/usb_template_cdce.c (revision a593f6b8de8b956bd0dd22f74805ede942e3d6a9)
102ac6454SAndrew Thompson #include <sys/cdefs.h>
202ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
302ac6454SAndrew Thompson 
402ac6454SAndrew Thompson /*-
55d38a4d4SEd Schouten  * Copyright (c) 2007 Hans Petter Selasky <hselasky@FreeBSD.org>
602ac6454SAndrew Thompson  * All rights reserved.
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
902ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1002ac6454SAndrew Thompson  * are met:
1102ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1302ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1502ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1602ac6454SAndrew Thompson  *
1702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1802ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1902ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2002ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2102ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2202ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2302ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2402ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2502ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2602ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2702ac6454SAndrew Thompson  * SUCH DAMAGE.
2802ac6454SAndrew Thompson  */
2902ac6454SAndrew Thompson 
3002ac6454SAndrew Thompson /*
3102ac6454SAndrew Thompson  * This file contains the USB templates for a CDC USB ethernet device.
3202ac6454SAndrew Thompson  */
3302ac6454SAndrew Thompson 
3402ac6454SAndrew Thompson #include <dev/usb/usb.h>
3502ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
3602ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
3702ac6454SAndrew Thompson 
3802ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
3902ac6454SAndrew Thompson 
4002ac6454SAndrew Thompson #include <dev/usb/template/usb_template.h>
4102ac6454SAndrew Thompson 
4202ac6454SAndrew Thompson enum {
4302ac6454SAndrew Thompson 	STRING_LANG_INDEX,
4402ac6454SAndrew Thompson 	STRING_MAC_INDEX,
4502ac6454SAndrew Thompson 	STRING_ETH_CONTROL_INDEX,
4602ac6454SAndrew Thompson 	STRING_ETH_DATA_INDEX,
4702ac6454SAndrew Thompson 	STRING_ETH_CONFIG_INDEX,
4802ac6454SAndrew Thompson 	STRING_ETH_VENDOR_INDEX,
4902ac6454SAndrew Thompson 	STRING_ETH_PRODUCT_INDEX,
5002ac6454SAndrew Thompson 	STRING_ETH_SERIAL_INDEX,
5102ac6454SAndrew Thompson 	STRING_ETH_MAX,
5202ac6454SAndrew Thompson };
5302ac6454SAndrew Thompson 
5402ac6454SAndrew Thompson #define	STRING_LANG \
5502ac6454SAndrew Thompson   0x09, 0x04,				/* American English */
5602ac6454SAndrew Thompson 
5702ac6454SAndrew Thompson #define	STRING_MAC \
5802ac6454SAndrew Thompson   '2', 0, 'A', 0, '2', 0, '3', 0, \
5902ac6454SAndrew Thompson   '4', 0, '5', 0, '6', 0, '7', 0, \
6002ac6454SAndrew Thompson   '8', 0, '9', 0, 'A', 0, 'B', 0,
6102ac6454SAndrew Thompson 
6202ac6454SAndrew Thompson #define	STRING_ETH_CONTROL \
6302ac6454SAndrew Thompson   'U', 0, 'S', 0, 'B', 0, ' ', 0, \
6402ac6454SAndrew Thompson   'E', 0, 't', 0, 'h', 0, 'e', 0, \
6502ac6454SAndrew Thompson   'r', 0, 'n', 0, 'e', 0, 't', 0, \
6602ac6454SAndrew Thompson   ' ', 0, 'C', 0, 'o', 0, 'm', 0, \
6702ac6454SAndrew Thompson   'm', 0, ' ', 0, 'i', 0, 'n', 0, \
6802ac6454SAndrew Thompson   't', 0, 'e', 0, 'r', 0, 'f', 0, \
6902ac6454SAndrew Thompson   'a', 0, 'c', 0, 'e', 0,
7002ac6454SAndrew Thompson 
7102ac6454SAndrew Thompson #define	STRING_ETH_DATA \
7202ac6454SAndrew Thompson   'U', 0, 'S', 0, 'B', 0, ' ', 0, \
7302ac6454SAndrew Thompson   'E', 0, 't', 0, 'h', 0, 'e', 0, \
7402ac6454SAndrew Thompson   'r', 0, 'n', 0, 'e', 0, 't', 0, \
7502ac6454SAndrew Thompson   ' ', 0, 'D', 0, 'a', 0, 't', 0, \
7602ac6454SAndrew Thompson   'a', 0, ' ', 0, 'i', 0, 'n', 0, \
7702ac6454SAndrew Thompson   't', 0, 'e', 0, 'r', 0, 'f', 0, \
7802ac6454SAndrew Thompson   'a', 0, 'c', 0, 'e', 0,
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson #define	STRING_ETH_CONFIG \
8102ac6454SAndrew Thompson   'D', 0, 'e', 0, 'f', 0, 'a', 0, \
8202ac6454SAndrew Thompson   'u', 0, 'l', 0, 't', 0, ' ', 0, \
8302ac6454SAndrew Thompson   'c', 0, 'o', 0, 'n', 0, 'f', 0, \
8402ac6454SAndrew Thompson   'i', 0, 'g', 0,
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson #define	STRING_ETH_VENDOR \
8702ac6454SAndrew Thompson   'F', 0, 'r', 0, 'e', 0, 'e', 0, \
8802ac6454SAndrew Thompson   'B', 0, 'S', 0, 'D', 0, ' ', 0, \
8902ac6454SAndrew Thompson   'f', 0, 'o', 0, 'u', 0, 'n', 0, \
9002ac6454SAndrew Thompson   'd', 0, 'a', 0, 't', 0, 'i', 0, \
9102ac6454SAndrew Thompson   'o', 0, 'n', 0,
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson #define	STRING_ETH_PRODUCT \
9402ac6454SAndrew Thompson   'U', 0, 'S', 0, 'B', 0, ' ', 0, \
9502ac6454SAndrew Thompson   'E', 0, 't', 0, 'h', 0, 'e', 0, \
9602ac6454SAndrew Thompson   'r', 0, 'n', 0, 'e', 0, 't', 0, \
9702ac6454SAndrew Thompson   ' ', 0, 'A', 0, 'd', 0, 'a', 0, \
9802ac6454SAndrew Thompson   'p', 0, 't', 0, 'e', 0, 'r', 0,
9902ac6454SAndrew Thompson 
10002ac6454SAndrew Thompson #define	STRING_ETH_SERIAL \
10102ac6454SAndrew Thompson   'D', 0, 'e', 0, 'c', 0, 'e', 0, \
10202ac6454SAndrew Thompson   'm', 0, 'b', 0, 'e', 0, 'r', 0, \
10302ac6454SAndrew Thompson   ' ', 0, '2', 0, '0', 0, '0', 0, \
10402ac6454SAndrew Thompson   '7', 0,
10502ac6454SAndrew Thompson 
10602ac6454SAndrew Thompson /* make the real string descriptors */
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
10902ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_MAC, string_mac);
11002ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_ETH_CONTROL, string_eth_control);
11102ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_ETH_DATA, string_eth_data);
11202ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_ETH_CONFIG, string_eth_config);
11302ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_ETH_VENDOR, string_eth_vendor);
11402ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_ETH_PRODUCT, string_eth_product);
11502ac6454SAndrew Thompson USB_MAKE_STRING_DESC(STRING_ETH_SERIAL, string_eth_serial);
11602ac6454SAndrew Thompson 
11702ac6454SAndrew Thompson /* prototypes */
11802ac6454SAndrew Thompson 
119a593f6b8SAndrew Thompson static usb_temp_get_string_desc_t eth_get_string_desc;
12002ac6454SAndrew Thompson 
121760bc48eSAndrew Thompson static const struct usb_cdc_union_descriptor eth_union_desc = {
12202ac6454SAndrew Thompson 	.bLength = sizeof(eth_union_desc),
12302ac6454SAndrew Thompson 	.bDescriptorType = UDESC_CS_INTERFACE,
12402ac6454SAndrew Thompson 	.bDescriptorSubtype = UDESCSUB_CDC_UNION,
12502ac6454SAndrew Thompson 	.bMasterInterface = 0,		/* this is automatically updated */
12602ac6454SAndrew Thompson 	.bSlaveInterface[0] = 1,	/* this is automatically updated */
12702ac6454SAndrew Thompson };
12802ac6454SAndrew Thompson 
129760bc48eSAndrew Thompson static const struct usb_cdc_header_descriptor eth_header_desc = {
13002ac6454SAndrew Thompson 	.bLength = sizeof(eth_header_desc),
13102ac6454SAndrew Thompson 	.bDescriptorType = UDESC_CS_INTERFACE,
13202ac6454SAndrew Thompson 	.bDescriptorSubtype = UDESCSUB_CDC_HEADER,
13302ac6454SAndrew Thompson 	.bcdCDC[0] = 0x10,
13402ac6454SAndrew Thompson 	.bcdCDC[1] = 0x01,
13502ac6454SAndrew Thompson };
13602ac6454SAndrew Thompson 
137760bc48eSAndrew Thompson static const struct usb_cdc_ethernet_descriptor eth_enf_desc = {
13802ac6454SAndrew Thompson 	.bLength = sizeof(eth_enf_desc),
13902ac6454SAndrew Thompson 	.bDescriptorType = UDESC_CS_INTERFACE,
14002ac6454SAndrew Thompson 	.bDescriptorSubtype = UDESCSUB_CDC_ENF,
14102ac6454SAndrew Thompson 	.iMacAddress = STRING_MAC_INDEX,
14202ac6454SAndrew Thompson 	.bmEthernetStatistics = {0, 0, 0, 0},
14302ac6454SAndrew Thompson 	.wMaxSegmentSize = {0xEA, 0x05},/* 1514 bytes */
14402ac6454SAndrew Thompson 	.wNumberMCFilters = {0, 0},
14502ac6454SAndrew Thompson 	.bNumberPowerFilters = 0,
14602ac6454SAndrew Thompson };
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson static const void *eth_control_if_desc[] = {
14902ac6454SAndrew Thompson 	&eth_union_desc,
15002ac6454SAndrew Thompson 	&eth_header_desc,
15102ac6454SAndrew Thompson 	&eth_enf_desc,
15202ac6454SAndrew Thompson 	NULL,
15302ac6454SAndrew Thompson };
15402ac6454SAndrew Thompson 
155760bc48eSAndrew Thompson static const struct usb_temp_packet_size bulk_mps = {
15602ac6454SAndrew Thompson 	.mps[USB_SPEED_FULL] = 64,
15702ac6454SAndrew Thompson 	.mps[USB_SPEED_HIGH] = 512,
15802ac6454SAndrew Thompson };
15902ac6454SAndrew Thompson 
160760bc48eSAndrew Thompson static const struct usb_temp_packet_size intr_mps = {
16102ac6454SAndrew Thompson 	.mps[USB_SPEED_FULL] = 8,
16202ac6454SAndrew Thompson 	.mps[USB_SPEED_HIGH] = 8,
16302ac6454SAndrew Thompson };
16402ac6454SAndrew Thompson 
165760bc48eSAndrew Thompson static const struct usb_temp_endpoint_desc bulk_in_ep = {
16602ac6454SAndrew Thompson 	.pPacketSize = &bulk_mps,
16702ac6454SAndrew Thompson #ifdef USB_HIP_IN_EP_0
16802ac6454SAndrew Thompson 	.bEndpointAddress = USB_HIP_IN_EP_0,
16902ac6454SAndrew Thompson #else
17002ac6454SAndrew Thompson 	.bEndpointAddress = UE_DIR_IN,
17102ac6454SAndrew Thompson #endif
17202ac6454SAndrew Thompson 	.bmAttributes = UE_BULK,
17302ac6454SAndrew Thompson };
17402ac6454SAndrew Thompson 
175760bc48eSAndrew Thompson static const struct usb_temp_endpoint_desc bulk_out_ep = {
17602ac6454SAndrew Thompson 	.pPacketSize = &bulk_mps,
17702ac6454SAndrew Thompson #ifdef USB_HIP_OUT_EP_0
17802ac6454SAndrew Thompson 	.bEndpointAddress = USB_HIP_OUT_EP_0,
17902ac6454SAndrew Thompson #else
18002ac6454SAndrew Thompson 	.bEndpointAddress = UE_DIR_OUT,
18102ac6454SAndrew Thompson #endif
18202ac6454SAndrew Thompson 	.bmAttributes = UE_BULK,
18302ac6454SAndrew Thompson };
18402ac6454SAndrew Thompson 
185760bc48eSAndrew Thompson static const struct usb_temp_endpoint_desc intr_in_ep = {
18602ac6454SAndrew Thompson 	.pPacketSize = &intr_mps,
18702ac6454SAndrew Thompson 	.bEndpointAddress = UE_DIR_IN,
18802ac6454SAndrew Thompson 	.bmAttributes = UE_INTERRUPT,
18902ac6454SAndrew Thompson };
19002ac6454SAndrew Thompson 
191760bc48eSAndrew Thompson static const struct usb_temp_endpoint_desc *eth_intr_endpoints[] = {
19202ac6454SAndrew Thompson 	&intr_in_ep,
19302ac6454SAndrew Thompson 	NULL,
19402ac6454SAndrew Thompson };
19502ac6454SAndrew Thompson 
196760bc48eSAndrew Thompson static const struct usb_temp_interface_desc eth_control_interface = {
19702ac6454SAndrew Thompson 	.ppEndpoints = eth_intr_endpoints,
19802ac6454SAndrew Thompson 	.ppRawDesc = eth_control_if_desc,
19902ac6454SAndrew Thompson 	.bInterfaceClass = UICLASS_CDC,
20002ac6454SAndrew Thompson 	.bInterfaceSubClass = UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL,
20102ac6454SAndrew Thompson 	.bInterfaceProtocol = 0,
20202ac6454SAndrew Thompson 	.iInterface = STRING_ETH_CONTROL_INDEX,
20302ac6454SAndrew Thompson };
20402ac6454SAndrew Thompson 
205760bc48eSAndrew Thompson static const struct usb_temp_endpoint_desc *eth_data_endpoints[] = {
20602ac6454SAndrew Thompson 	&bulk_in_ep,
20702ac6454SAndrew Thompson 	&bulk_out_ep,
20802ac6454SAndrew Thompson 	NULL,
20902ac6454SAndrew Thompson };
21002ac6454SAndrew Thompson 
211760bc48eSAndrew Thompson static const struct usb_temp_interface_desc eth_data_null_interface = {
21202ac6454SAndrew Thompson 	.ppEndpoints = NULL,		/* no endpoints */
21302ac6454SAndrew Thompson 	.bInterfaceClass = UICLASS_CDC_DATA,
21402ac6454SAndrew Thompson 	.bInterfaceSubClass = 0,
21502ac6454SAndrew Thompson 	.bInterfaceProtocol = 0,
21602ac6454SAndrew Thompson 	.iInterface = STRING_ETH_DATA_INDEX,
21702ac6454SAndrew Thompson };
21802ac6454SAndrew Thompson 
219760bc48eSAndrew Thompson static const struct usb_temp_interface_desc eth_data_interface = {
22002ac6454SAndrew Thompson 	.ppEndpoints = eth_data_endpoints,
22102ac6454SAndrew Thompson 	.bInterfaceClass = UICLASS_CDC_DATA,
22202ac6454SAndrew Thompson 	.bInterfaceSubClass = UISUBCLASS_DATA,
22302ac6454SAndrew Thompson 	.bInterfaceProtocol = 0,
22402ac6454SAndrew Thompson 	.iInterface = STRING_ETH_DATA_INDEX,
22502ac6454SAndrew Thompson 	.isAltInterface = 1,		/* this is an alternate setting */
22602ac6454SAndrew Thompson };
22702ac6454SAndrew Thompson 
228760bc48eSAndrew Thompson static const struct usb_temp_interface_desc *eth_interfaces[] = {
22902ac6454SAndrew Thompson 	&eth_control_interface,
23002ac6454SAndrew Thompson 	&eth_data_null_interface,
23102ac6454SAndrew Thompson 	&eth_data_interface,
23202ac6454SAndrew Thompson 	NULL,
23302ac6454SAndrew Thompson };
23402ac6454SAndrew Thompson 
235760bc48eSAndrew Thompson static const struct usb_temp_config_desc eth_config_desc = {
23602ac6454SAndrew Thompson 	.ppIfaceDesc = eth_interfaces,
23702ac6454SAndrew Thompson 	.bmAttributes = UC_BUS_POWERED,
23802ac6454SAndrew Thompson 	.bMaxPower = 25,		/* 50 mA */
23902ac6454SAndrew Thompson 	.iConfiguration = STRING_ETH_CONFIG_INDEX,
24002ac6454SAndrew Thompson };
24102ac6454SAndrew Thompson 
242760bc48eSAndrew Thompson static const struct usb_temp_config_desc *eth_configs[] = {
24302ac6454SAndrew Thompson 	&eth_config_desc,
24402ac6454SAndrew Thompson 	NULL,
24502ac6454SAndrew Thompson };
24602ac6454SAndrew Thompson 
247a593f6b8SAndrew Thompson const struct usb_temp_device_desc usb_template_cdce = {
24802ac6454SAndrew Thompson 	.getStringDesc = &eth_get_string_desc,
24902ac6454SAndrew Thompson 	.ppConfigDesc = eth_configs,
25002ac6454SAndrew Thompson 	.idVendor = 0x0001,
25102ac6454SAndrew Thompson 	.idProduct = 0x0001,
25202ac6454SAndrew Thompson 	.bcdDevice = 0x0100,
25302ac6454SAndrew Thompson 	.bDeviceClass = UDCLASS_COMM,
25402ac6454SAndrew Thompson 	.bDeviceSubClass = 0,
25502ac6454SAndrew Thompson 	.bDeviceProtocol = 0,
25602ac6454SAndrew Thompson 	.iManufacturer = STRING_ETH_VENDOR_INDEX,
25702ac6454SAndrew Thompson 	.iProduct = STRING_ETH_PRODUCT_INDEX,
25802ac6454SAndrew Thompson 	.iSerialNumber = STRING_ETH_SERIAL_INDEX,
25902ac6454SAndrew Thompson };
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
26202ac6454SAndrew Thompson  *	eth_get_string_desc
26302ac6454SAndrew Thompson  *
26402ac6454SAndrew Thompson  * Return values:
26502ac6454SAndrew Thompson  * NULL: Failure. No such string.
26602ac6454SAndrew Thompson  * Else: Success. Pointer to string descriptor is returned.
26702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
26802ac6454SAndrew Thompson static const void *
26902ac6454SAndrew Thompson eth_get_string_desc(uint16_t lang_id, uint8_t string_index)
27002ac6454SAndrew Thompson {
27102ac6454SAndrew Thompson 	static const void *ptr[STRING_ETH_MAX] = {
27202ac6454SAndrew Thompson 		[STRING_LANG_INDEX] = &string_lang,
27302ac6454SAndrew Thompson 		[STRING_MAC_INDEX] = &string_mac,
27402ac6454SAndrew Thompson 		[STRING_ETH_CONTROL_INDEX] = &string_eth_control,
27502ac6454SAndrew Thompson 		[STRING_ETH_DATA_INDEX] = &string_eth_data,
27602ac6454SAndrew Thompson 		[STRING_ETH_CONFIG_INDEX] = &string_eth_config,
27702ac6454SAndrew Thompson 		[STRING_ETH_VENDOR_INDEX] = &string_eth_vendor,
27802ac6454SAndrew Thompson 		[STRING_ETH_PRODUCT_INDEX] = &string_eth_product,
27902ac6454SAndrew Thompson 		[STRING_ETH_SERIAL_INDEX] = &string_eth_serial,
28002ac6454SAndrew Thompson 	};
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 	if (string_index == 0) {
28302ac6454SAndrew Thompson 		return (&string_lang);
28402ac6454SAndrew Thompson 	}
28502ac6454SAndrew Thompson 	if (lang_id != 0x0409) {
28602ac6454SAndrew Thompson 		return (NULL);
28702ac6454SAndrew Thompson 	}
28802ac6454SAndrew Thompson 	if (string_index < STRING_ETH_MAX) {
28902ac6454SAndrew Thompson 		return (ptr[string_index]);
29002ac6454SAndrew Thompson 	}
29102ac6454SAndrew Thompson 	return (NULL);
29202ac6454SAndrew Thompson }
293