3 * Copyright (c) 2015 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This file contains the USB template for an USB MIDI Device.
31 #ifdef USB_GLOBAL_INCLUDE_FILE
32 #include USB_GLOBAL_INCLUDE_FILE
34 #include <sys/stdint.h>
35 #include <sys/stddef.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/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usb_core.h>
57 #include <dev/usb/template/usb_template.h>
58 #endif /* USB_GLOBAL_INCLUDE_FILE */
67 #define STRING_MIDI_PRODUCT \
68 "M\0I\0D\0I\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e"
70 #define STRING_MIDI_IF \
71 "M\0I\0D\0I\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
73 /* make the real string descriptors */
75 USB_MAKE_STRING_DESC(STRING_MIDI_IF, string_midi_if);
76 USB_MAKE_STRING_DESC(STRING_MIDI_PRODUCT, string_midi_product);
80 static const uint8_t midi_desc_raw_0[9] = {
81 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01
84 static const void *midi_descs_0[] = {
89 static const struct usb_temp_interface_desc midi_iface_0 = {
90 .ppEndpoints = NULL, /* no endpoints */
91 .ppRawDesc = midi_descs_0,
92 .bInterfaceClass = UICLASS_AUDIO,
93 .bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL,
94 .bInterfaceProtocol = 0,
95 .iInterface = INDEX_MIDI_IF,
98 static const struct usb_temp_packet_size midi_mps = {
99 .mps[USB_SPEED_LOW] = 8,
100 .mps[USB_SPEED_FULL] = 64,
101 .mps[USB_SPEED_HIGH] = 512,
104 static const uint8_t midi_desc_raw_7[5] = {
105 0x05, 0x25, 0x01, 0x01, 0x01
108 static const void *midi_descs_2[] = {
113 static const struct usb_temp_endpoint_desc midi_bulk_out_ep = {
114 .ppRawDesc = midi_descs_2,
115 .pPacketSize = &midi_mps,
116 .bEndpointAddress = UE_DIR_OUT,
117 .bmAttributes = UE_BULK,
120 static const uint8_t midi_desc_raw_6[5] = {
121 0x05, 0x25, 0x01, 0x01, 0x03,
124 static const void *midi_descs_3[] = {
129 static const struct usb_temp_endpoint_desc midi_bulk_in_ep = {
130 .ppRawDesc = midi_descs_3,
131 .pPacketSize = &midi_mps,
132 .bEndpointAddress = UE_DIR_IN,
133 .bmAttributes = UE_BULK,
136 static const struct usb_temp_endpoint_desc *midi_iface_1_ep[] = {
142 static const uint8_t midi_desc_raw_1[7] = {
143 0x07, 0x24, 0x01, 0x00, 0x01, /* wTotalLength: */ 0x41, 0x00
146 static const uint8_t midi_desc_raw_2[6] = {
147 0x06, 0x24, 0x02, 0x01, 0x01, 0x00
150 static const uint8_t midi_desc_raw_3[6] = {
151 0x06, 0x24, 0x02, 0x02, 0x02, 0x00
154 static const uint8_t midi_desc_raw_4[9] = {
155 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00
158 static const uint8_t midi_desc_raw_5[9] = {
159 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x01, 0x01, 0x00
162 static const void *midi_descs_1[] = {
171 static const struct usb_temp_interface_desc midi_iface_1 = {
172 .ppRawDesc = midi_descs_1,
173 .ppEndpoints = midi_iface_1_ep,
174 .bInterfaceClass = UICLASS_AUDIO,
175 .bInterfaceSubClass = UISUBCLASS_MIDISTREAM,
176 .bInterfaceProtocol = 0,
177 .iInterface = INDEX_MIDI_IF,
180 static const struct usb_temp_interface_desc *midi_interfaces[] = {
186 static const struct usb_temp_config_desc midi_config_desc = {
187 .ppIfaceDesc = midi_interfaces,
188 .bmAttributes = UC_BUS_POWERED,
189 .bMaxPower = 25, /* 50 mA */
190 .iConfiguration = INDEX_MIDI_PRODUCT,
193 static const struct usb_temp_config_desc *midi_configs[] = {
198 static usb_temp_get_string_desc_t midi_get_string_desc;
200 const struct usb_temp_device_desc usb_template_midi = {
201 .getStringDesc = &midi_get_string_desc,
202 .ppConfigDesc = midi_configs,
203 .idVendor = USB_TEMPLATE_VENDOR,
207 .bDeviceSubClass = 0,
208 .bDeviceProtocol = 0,
210 .iProduct = INDEX_MIDI_PRODUCT,
214 /*------------------------------------------------------------------------*
215 * midi_get_string_desc
218 * NULL: Failure. No such string.
219 * Else: Success. Pointer to string descriptor is returned.
220 *------------------------------------------------------------------------*/
222 midi_get_string_desc(uint16_t lang_id, uint8_t string_index)
224 static const void *ptr[INDEX_MIDI_MAX] = {
225 [INDEX_MIDI_LANG] = &usb_string_lang_en,
226 [INDEX_MIDI_IF] = &string_midi_if,
227 [INDEX_MIDI_PRODUCT] = &string_midi_product,
230 if (string_index == 0) {
231 return (&usb_string_lang_en);
233 if (lang_id != 0x0409) {
236 if (string_index < INDEX_MIDI_MAX) {
237 return (ptr[string_index]);