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