xref: /freebsd/sys/dev/usb/template/usb_template.h (revision 5c1d97100348ef19878fa14671a9b70f3d963ed4)
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 /* USB templates are used to build up real USB descriptors */
31 
32 #ifndef _USB_TEMPLATE_H_
33 #define	_USB_TEMPLATE_H_
34 
35 #ifndef USB_TEMPLATE_VENDOR
36 /*
37  * https://github.com/obdev/v-usb/blob/master/usbdrv/USB-IDs-for-free.txt
38  */
39 #define	USB_TEMPLATE_VENDOR		0x16c0
40 #define	USB_TEMPLATE_MANUFACTURER	\
41 	"The FreeBSD Project (https://www.FreeBSD.org)"
42 #endif
43 
44 typedef const void *(usb_temp_get_string_desc_t)(uint16_t lang_id, uint8_t string_index);
45 typedef const void *(usb_temp_get_vendor_desc_t)(const struct usb_device_request *req, uint16_t *plen);
46 
47 struct usb_temp_packet_size {
48 	uint16_t mps[USB_SPEED_MAX];
49 };
50 
51 struct usb_temp_interval {
52 	uint8_t	bInterval[USB_SPEED_MAX];
53 };
54 
55 struct usb_temp_endpoint_desc {
56 	const void **ppRawDesc;
57 	const struct usb_temp_packet_size *pPacketSize;
58 	const struct usb_temp_interval *pIntervals;
59 	/*
60 	 * If (bEndpointAddress & UE_ADDR) is non-zero the endpoint number
61 	 * is pre-selected for this endpoint descriptor. Else an endpoint
62 	 * number is automatically chosen.
63 	 */
64 	uint8_t	bEndpointAddress;	/* UE_DIR_IN or UE_DIR_OUT */
65 	uint8_t	bmAttributes;
66 };
67 
68 struct usb_temp_interface_desc {
69 	const void **ppRawDesc;
70 	const struct usb_temp_endpoint_desc **ppEndpoints;
71 	uint8_t	bInterfaceClass;
72 	uint8_t	bInterfaceSubClass;
73 	uint8_t	bInterfaceProtocol;
74 	uint8_t	iInterface;
75 	uint8_t	isAltInterface;
76 };
77 
78 struct usb_temp_config_desc {
79 	const struct usb_temp_interface_desc **ppIfaceDesc;
80 	uint8_t	bmAttributes;
81 	uint8_t	bMaxPower;
82 	uint8_t	iConfiguration;
83 };
84 
85 struct usb_temp_device_desc {
86 	usb_temp_get_string_desc_t *getStringDesc;
87 	usb_temp_get_vendor_desc_t *getVendorDesc;
88 	const struct usb_temp_config_desc **ppConfigDesc;
89 	uint16_t idVendor;
90 	uint16_t idProduct;
91 	uint16_t bcdDevice;
92 	uint8_t	bDeviceClass;
93 	uint8_t	bDeviceSubClass;
94 	uint8_t	bDeviceProtocol;
95 	uint8_t	iManufacturer;
96 	uint8_t	iProduct;
97 	uint8_t	iSerialNumber;
98 };
99 
100 struct usb_temp_data {
101 	const struct usb_temp_device_desc *tdd;
102 	struct usb_device_descriptor udd;	/* device descriptor */
103 	struct usb_device_qualifier udq;	/* device qualifier */
104 };
105 
106 /* prototypes */
107 
108 extern struct usb_temp_device_desc usb_template_audio;
109 extern struct usb_temp_device_desc usb_template_cdce;
110 extern struct usb_temp_device_desc usb_template_kbd;
111 extern struct usb_temp_device_desc usb_template_modem;
112 extern struct usb_temp_device_desc usb_template_mouse;
113 extern struct usb_temp_device_desc usb_template_msc;
114 extern struct usb_temp_device_desc usb_template_mtp;
115 extern struct usb_temp_device_desc usb_template_phone;
116 extern struct usb_temp_device_desc usb_template_serialnet;
117 extern struct usb_temp_device_desc usb_template_midi;
118 extern struct usb_temp_device_desc usb_template_multi;
119 extern struct usb_temp_device_desc usb_template_cdceem;
120 
121 void		usb_decode_str_desc(struct usb_string_descriptor *sd,
122 		    char *buf, size_t buflen);
123 usb_error_t	usb_temp_setup(struct usb_device *,
124 		    const struct usb_temp_device_desc *);
125 void		usb_temp_unsetup(struct usb_device *);
126 int		usb_temp_sysctl(SYSCTL_HANDLER_ARGS);
127 
128 SYSCTL_DECL(_hw_usb_templates);
129 
130 #endif					/* _USB_TEMPLATE_H_ */
131