1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2015 Hans Petter Selasky 4 * Copyright (c) 2018 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * This file contains the USB template for an USB MIDI Device. 34 */ 35 36 #ifdef USB_GLOBAL_INCLUDE_FILE 37 #include USB_GLOBAL_INCLUDE_FILE 38 #else 39 #include <sys/stdint.h> 40 #include <sys/stddef.h> 41 #include <sys/param.h> 42 #include <sys/queue.h> 43 #include <sys/types.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/bus.h> 47 #include <sys/module.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/condvar.h> 51 #include <sys/sysctl.h> 52 #include <sys/sx.h> 53 #include <sys/unistd.h> 54 #include <sys/callout.h> 55 #include <sys/malloc.h> 56 #include <sys/priv.h> 57 58 #include <dev/usb/usb.h> 59 #include <dev/usb/usbdi.h> 60 #include <dev/usb/usb_core.h> 61 #include <dev/usb/usb_ioctl.h> 62 #include <dev/usb/usb_util.h> 63 64 #include <dev/usb/template/usb_template.h> 65 #endif /* USB_GLOBAL_INCLUDE_FILE */ 66 67 enum { 68 MIDI_LANG_INDEX, 69 MIDI_INTERFACE_INDEX, 70 MIDI_PRODUCT_INDEX, 71 MIDI_MAX_INDEX, 72 }; 73 74 #define MIDI_DEFAULT_INTERFACE "MIDI interface" 75 #define MIDI_DEFAULT_PRODUCT "MIDI Test Device" 76 77 static struct usb_string_descriptor midi_interface; 78 static struct usb_string_descriptor midi_product; 79 80 static struct sysctl_ctx_list midi_ctx_list; 81 82 /* prototypes */ 83 84 static const uint8_t midi_desc_raw_0[9] = { 85 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01 86 }; 87 88 static const void *midi_descs_0[] = { 89 &midi_desc_raw_0, 90 NULL 91 }; 92 93 static const struct usb_temp_interface_desc midi_iface_0 = { 94 .ppEndpoints = NULL, /* no endpoints */ 95 .ppRawDesc = midi_descs_0, 96 .bInterfaceClass = UICLASS_AUDIO, 97 .bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL, 98 .bInterfaceProtocol = 0, 99 .iInterface = MIDI_INTERFACE_INDEX, 100 }; 101 102 static const struct usb_temp_packet_size midi_mps = { 103 .mps[USB_SPEED_LOW] = 8, 104 .mps[USB_SPEED_FULL] = 64, 105 .mps[USB_SPEED_HIGH] = 512, 106 }; 107 108 static const uint8_t midi_desc_raw_7[5] = { 109 0x05, 0x25, 0x01, 0x01, 0x01 110 }; 111 112 static const void *midi_descs_2[] = { 113 &midi_desc_raw_7, 114 NULL 115 }; 116 117 static const struct usb_temp_endpoint_desc midi_bulk_out_ep = { 118 .ppRawDesc = midi_descs_2, 119 .pPacketSize = &midi_mps, 120 .bEndpointAddress = UE_DIR_OUT, 121 .bmAttributes = UE_BULK, 122 }; 123 124 static const uint8_t midi_desc_raw_6[5] = { 125 0x05, 0x25, 0x01, 0x01, 0x03, 126 }; 127 128 static const void *midi_descs_3[] = { 129 &midi_desc_raw_6, 130 NULL 131 }; 132 133 static const struct usb_temp_endpoint_desc midi_bulk_in_ep = { 134 .ppRawDesc = midi_descs_3, 135 .pPacketSize = &midi_mps, 136 .bEndpointAddress = UE_DIR_IN, 137 .bmAttributes = UE_BULK, 138 }; 139 140 static const struct usb_temp_endpoint_desc *midi_iface_1_ep[] = { 141 &midi_bulk_out_ep, 142 &midi_bulk_in_ep, 143 NULL, 144 }; 145 146 static const uint8_t midi_desc_raw_1[7] = { 147 0x07, 0x24, 0x01, 0x00, 0x01, /* wTotalLength: */ 0x41, 0x00 148 }; 149 150 static const uint8_t midi_desc_raw_2[6] = { 151 0x06, 0x24, 0x02, 0x01, 0x01, 0x00 152 }; 153 154 static const uint8_t midi_desc_raw_3[6] = { 155 0x06, 0x24, 0x02, 0x02, 0x02, 0x00 156 }; 157 158 static const uint8_t midi_desc_raw_4[9] = { 159 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00 160 }; 161 162 static const uint8_t midi_desc_raw_5[9] = { 163 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x01, 0x01, 0x00 164 }; 165 166 static const void *midi_descs_1[] = { 167 &midi_desc_raw_1, 168 &midi_desc_raw_2, 169 &midi_desc_raw_3, 170 &midi_desc_raw_4, 171 &midi_desc_raw_5, 172 NULL 173 }; 174 175 static const struct usb_temp_interface_desc midi_iface_1 = { 176 .ppRawDesc = midi_descs_1, 177 .ppEndpoints = midi_iface_1_ep, 178 .bInterfaceClass = UICLASS_AUDIO, 179 .bInterfaceSubClass = UISUBCLASS_MIDISTREAM, 180 .bInterfaceProtocol = 0, 181 .iInterface = MIDI_INTERFACE_INDEX, 182 }; 183 184 static const struct usb_temp_interface_desc *midi_interfaces[] = { 185 &midi_iface_0, 186 &midi_iface_1, 187 NULL, 188 }; 189 190 static const struct usb_temp_config_desc midi_config_desc = { 191 .ppIfaceDesc = midi_interfaces, 192 .bmAttributes = UC_BUS_POWERED, 193 .bMaxPower = 25, /* 50 mA */ 194 .iConfiguration = MIDI_PRODUCT_INDEX, 195 }; 196 197 static const struct usb_temp_config_desc *midi_configs[] = { 198 &midi_config_desc, 199 NULL, 200 }; 201 202 static usb_temp_get_string_desc_t midi_get_string_desc; 203 204 struct usb_temp_device_desc usb_template_midi = { 205 .getStringDesc = &midi_get_string_desc, 206 .ppConfigDesc = midi_configs, 207 .idVendor = USB_TEMPLATE_VENDOR, 208 .idProduct = 0x00BB, 209 .bcdDevice = 0x0100, 210 .bDeviceClass = 0, 211 .bDeviceSubClass = 0, 212 .bDeviceProtocol = 0, 213 .iManufacturer = 0, 214 .iProduct = MIDI_PRODUCT_INDEX, 215 .iSerialNumber = 0, 216 }; 217 218 /*------------------------------------------------------------------------* 219 * midi_get_string_desc 220 * 221 * Return values: 222 * NULL: Failure. No such string. 223 * Else: Success. Pointer to string descriptor is returned. 224 *------------------------------------------------------------------------*/ 225 static const void * 226 midi_get_string_desc(uint16_t lang_id, uint8_t string_index) 227 { 228 static const void *ptr[MIDI_MAX_INDEX] = { 229 [MIDI_LANG_INDEX] = &usb_string_lang_en, 230 [MIDI_INTERFACE_INDEX] = &midi_interface, 231 [MIDI_PRODUCT_INDEX] = &midi_product, 232 }; 233 234 if (string_index == 0) { 235 return (&usb_string_lang_en); 236 } 237 if (lang_id != 0x0409) { 238 return (NULL); 239 } 240 if (string_index < MIDI_MAX_INDEX) { 241 return (ptr[string_index]); 242 } 243 return (NULL); 244 } 245 246 static void 247 midi_init(void *arg __unused) 248 { 249 struct sysctl_oid *parent; 250 char parent_name[3]; 251 252 usb_make_str_desc(&midi_interface, sizeof(midi_interface), 253 MIDI_DEFAULT_INTERFACE); 254 usb_make_str_desc(&midi_product, sizeof(midi_product), 255 MIDI_DEFAULT_PRODUCT); 256 257 snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MIDI); 258 sysctl_ctx_init(&midi_ctx_list); 259 260 parent = SYSCTL_ADD_NODE(&midi_ctx_list, 261 SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, 262 parent_name, CTLFLAG_RW, 263 0, "USB MIDI device side template"); 264 SYSCTL_ADD_U16(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 265 "vendor_id", CTLFLAG_RWTUN, 266 &usb_template_midi.idVendor, 1, "Vendor identifier"); 267 SYSCTL_ADD_U16(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 268 "product_id", CTLFLAG_RWTUN, 269 &usb_template_midi.idProduct, 1, "Product identifier"); 270 #if 0 271 SYSCTL_ADD_PROC(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 272 "interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 273 &midi_interface, sizeof(midi_interface), usb_temp_sysctl, 274 "A", "Interface string"); 275 #endif 276 SYSCTL_ADD_PROC(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, 277 "product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 278 &midi_product, sizeof(midi_product), usb_temp_sysctl, 279 "A", "Product string"); 280 } 281 282 static void 283 midi_uninit(void *arg __unused) 284 { 285 286 sysctl_ctx_free(&midi_ctx_list); 287 } 288 289 SYSINIT(midi_init, SI_SUB_LOCK, SI_ORDER_FIRST, midi_init, NULL); 290 SYSUNINIT(midi_init, SI_SUB_LOCK, SI_ORDER_FIRST, midi_uninit, NULL); 291