Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/usb/template/usb_template_midi.c
/* $FreeBSD$ */ | /* $FreeBSD$ */ | ||||
/*- | /*- | ||||
* Copyright (c) 2015 Hans Petter Selasky. All rights reserved. | * Copyright (c) 2015 Hans Petter Selasky | ||||
* Copyright (c) 2018 The FreeBSD Foundation | |||||
* All rights reserved. | |||||
* | * | ||||
* Portions of this software were developed by Edward Tomasz Napierala | |||||
* under sponsorship from the FreeBSD Foundation. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
* are met: | * are met: | ||||
* 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||
* notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | ||||
* 2. Redistributions in binary form must reproduce the above copyright | * 2. Redistributions in binary form must reproduce the above copyright | ||||
* notice, this list of conditions and the following disclaimer in the | * notice, this list of conditions and the following disclaimer in the | ||||
* documentation and/or other materials provided with the distribution. | * documentation and/or other materials provided with the distribution. | ||||
Show All 35 Lines | |||||
#include <sys/unistd.h> | #include <sys/unistd.h> | ||||
#include <sys/callout.h> | #include <sys/callout.h> | ||||
#include <sys/malloc.h> | #include <sys/malloc.h> | ||||
#include <sys/priv.h> | #include <sys/priv.h> | ||||
#include <dev/usb/usb.h> | #include <dev/usb/usb.h> | ||||
#include <dev/usb/usbdi.h> | #include <dev/usb/usbdi.h> | ||||
#include <dev/usb/usb_core.h> | #include <dev/usb/usb_core.h> | ||||
#include <dev/usb/usb_ioctl.h> | |||||
#include <dev/usb/usb_util.h> | |||||
#include <dev/usb/template/usb_template.h> | #include <dev/usb/template/usb_template.h> | ||||
#endif /* USB_GLOBAL_INCLUDE_FILE */ | #endif /* USB_GLOBAL_INCLUDE_FILE */ | ||||
enum { | enum { | ||||
INDEX_MIDI_LANG, | MIDI_LANG_INDEX, | ||||
INDEX_MIDI_IF, | MIDI_INTERFACE_INDEX, | ||||
INDEX_MIDI_PRODUCT, | MIDI_PRODUCT_INDEX, | ||||
INDEX_MIDI_MAX, | MIDI_MAX_INDEX, | ||||
}; | }; | ||||
#define STRING_MIDI_PRODUCT \ | #define MIDI_DEFAULT_INTERFACE "MIDI interface" | ||||
"M\0I\0D\0I\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e" | #define MIDI_DEFAULT_PRODUCT "MIDI Test Device" | ||||
#define STRING_MIDI_IF \ | static struct usb_string_descriptor midi_interface; | ||||
"M\0I\0D\0I\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" | static struct usb_string_descriptor midi_product; | ||||
/* make the real string descriptors */ | static struct sysctl_ctx_list midi_ctx_list; | ||||
USB_MAKE_STRING_DESC(STRING_MIDI_IF, string_midi_if); | |||||
USB_MAKE_STRING_DESC(STRING_MIDI_PRODUCT, string_midi_product); | |||||
/* prototypes */ | /* prototypes */ | ||||
static const uint8_t midi_desc_raw_0[9] = { | static const uint8_t midi_desc_raw_0[9] = { | ||||
0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01 | ||||
}; | }; | ||||
static const void *midi_descs_0[] = { | static const void *midi_descs_0[] = { | ||||
&midi_desc_raw_0, | &midi_desc_raw_0, | ||||
NULL | NULL | ||||
}; | }; | ||||
static const struct usb_temp_interface_desc midi_iface_0 = { | static const struct usb_temp_interface_desc midi_iface_0 = { | ||||
.ppEndpoints = NULL, /* no endpoints */ | .ppEndpoints = NULL, /* no endpoints */ | ||||
.ppRawDesc = midi_descs_0, | .ppRawDesc = midi_descs_0, | ||||
.bInterfaceClass = UICLASS_AUDIO, | .bInterfaceClass = UICLASS_AUDIO, | ||||
.bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL, | .bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL, | ||||
.bInterfaceProtocol = 0, | .bInterfaceProtocol = 0, | ||||
.iInterface = INDEX_MIDI_IF, | .iInterface = MIDI_INTERFACE_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_packet_size midi_mps = { | static const struct usb_temp_packet_size midi_mps = { | ||||
.mps[USB_SPEED_LOW] = 8, | .mps[USB_SPEED_LOW] = 8, | ||||
.mps[USB_SPEED_FULL] = 64, | .mps[USB_SPEED_FULL] = 64, | ||||
.mps[USB_SPEED_HIGH] = 512, | .mps[USB_SPEED_HIGH] = 512, | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
static const struct usb_temp_interface_desc midi_iface_1 = { | static const struct usb_temp_interface_desc midi_iface_1 = { | ||||
.ppRawDesc = midi_descs_1, | .ppRawDesc = midi_descs_1, | ||||
.ppEndpoints = midi_iface_1_ep, | .ppEndpoints = midi_iface_1_ep, | ||||
.bInterfaceClass = UICLASS_AUDIO, | .bInterfaceClass = UICLASS_AUDIO, | ||||
.bInterfaceSubClass = UISUBCLASS_MIDISTREAM, | .bInterfaceSubClass = UISUBCLASS_MIDISTREAM, | ||||
.bInterfaceProtocol = 0, | .bInterfaceProtocol = 0, | ||||
.iInterface = INDEX_MIDI_IF, | .iInterface = MIDI_INTERFACE_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_interface_desc *midi_interfaces[] = { | static const struct usb_temp_interface_desc *midi_interfaces[] = { | ||||
&midi_iface_0, | &midi_iface_0, | ||||
&midi_iface_1, | &midi_iface_1, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
static const struct usb_temp_config_desc midi_config_desc = { | static const struct usb_temp_config_desc midi_config_desc = { | ||||
.ppIfaceDesc = midi_interfaces, | .ppIfaceDesc = midi_interfaces, | ||||
.bmAttributes = UC_BUS_POWERED, | .bmAttributes = UC_BUS_POWERED, | ||||
.bMaxPower = 25, /* 50 mA */ | .bMaxPower = 25, /* 50 mA */ | ||||
.iConfiguration = INDEX_MIDI_PRODUCT, | .iConfiguration = MIDI_PRODUCT_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_config_desc *midi_configs[] = { | static const struct usb_temp_config_desc *midi_configs[] = { | ||||
&midi_config_desc, | &midi_config_desc, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
static usb_temp_get_string_desc_t midi_get_string_desc; | static usb_temp_get_string_desc_t midi_get_string_desc; | ||||
const struct usb_temp_device_desc usb_template_midi = { | struct usb_temp_device_desc usb_template_midi = { | ||||
.getStringDesc = &midi_get_string_desc, | .getStringDesc = &midi_get_string_desc, | ||||
.ppConfigDesc = midi_configs, | .ppConfigDesc = midi_configs, | ||||
.idVendor = USB_TEMPLATE_VENDOR, | .idVendor = USB_TEMPLATE_VENDOR, | ||||
.idProduct = 0x00BB, | .idProduct = 0x00BB, | ||||
.bcdDevice = 0x0100, | .bcdDevice = 0x0100, | ||||
.bDeviceClass = 0, | .bDeviceClass = 0, | ||||
.bDeviceSubClass = 0, | .bDeviceSubClass = 0, | ||||
.bDeviceProtocol = 0, | .bDeviceProtocol = 0, | ||||
.iManufacturer = 0, | .iManufacturer = 0, | ||||
.iProduct = INDEX_MIDI_PRODUCT, | .iProduct = MIDI_PRODUCT_INDEX, | ||||
.iSerialNumber = 0, | .iSerialNumber = 0, | ||||
}; | }; | ||||
/*------------------------------------------------------------------------* | /*------------------------------------------------------------------------* | ||||
* midi_get_string_desc | * midi_get_string_desc | ||||
* | * | ||||
* Return values: | * Return values: | ||||
* NULL: Failure. No such string. | * NULL: Failure. No such string. | ||||
* Else: Success. Pointer to string descriptor is returned. | * Else: Success. Pointer to string descriptor is returned. | ||||
*------------------------------------------------------------------------*/ | *------------------------------------------------------------------------*/ | ||||
static const void * | static const void * | ||||
midi_get_string_desc(uint16_t lang_id, uint8_t string_index) | midi_get_string_desc(uint16_t lang_id, uint8_t string_index) | ||||
{ | { | ||||
static const void *ptr[INDEX_MIDI_MAX] = { | static const void *ptr[MIDI_MAX_INDEX] = { | ||||
[INDEX_MIDI_LANG] = &usb_string_lang_en, | [MIDI_LANG_INDEX] = &usb_string_lang_en, | ||||
[INDEX_MIDI_IF] = &string_midi_if, | [MIDI_INTERFACE_INDEX] = &midi_interface, | ||||
[INDEX_MIDI_PRODUCT] = &string_midi_product, | [MIDI_PRODUCT_INDEX] = &midi_product, | ||||
}; | }; | ||||
if (string_index == 0) { | if (string_index == 0) { | ||||
return (&usb_string_lang_en); | return (&usb_string_lang_en); | ||||
} | } | ||||
if (lang_id != 0x0409) { | if (lang_id != 0x0409) { | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
if (string_index < INDEX_MIDI_MAX) { | if (string_index < MIDI_MAX_INDEX) { | ||||
return (ptr[string_index]); | return (ptr[string_index]); | ||||
} | } | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
static void | |||||
midi_init(void *arg __unused) | |||||
{ | |||||
struct sysctl_oid *parent; | |||||
char parent_name[3]; | |||||
usb_make_str_desc(&midi_interface, sizeof(midi_interface), | |||||
MIDI_DEFAULT_INTERFACE); | |||||
usb_make_str_desc(&midi_product, sizeof(midi_product), | |||||
MIDI_DEFAULT_PRODUCT); | |||||
snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_MIDI); | |||||
sysctl_ctx_init(&midi_ctx_list); | |||||
parent = SYSCTL_ADD_NODE(&midi_ctx_list, | |||||
SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, | |||||
parent_name, CTLFLAG_RW, | |||||
0, "USB MIDI device side template"); | |||||
SYSCTL_ADD_U16(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"vendor_id", CTLFLAG_RWTUN, | |||||
&usb_template_midi.idVendor, 1, "Vendor identifier"); | |||||
SYSCTL_ADD_U16(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"product_id", CTLFLAG_RWTUN, | |||||
&usb_template_midi.idProduct, 1, "Product identifier"); | |||||
#if 0 | |||||
SYSCTL_ADD_PROC(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"interface", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, | |||||
&midi_interface, sizeof(midi_interface), usb_temp_sysctl, | |||||
"A", "Interface string"); | |||||
#endif | |||||
SYSCTL_ADD_PROC(&midi_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, | |||||
&midi_product, sizeof(midi_product), usb_temp_sysctl, | |||||
"A", "Product string"); | |||||
} | |||||
static void | |||||
midi_uninit(void *arg __unused) | |||||
{ | |||||
sysctl_ctx_free(&midi_ctx_list); | |||||
} | |||||
SYSINIT(midi_init, SI_SUB_LOCK, SI_ORDER_FIRST, midi_init, NULL); | |||||
SYSUNINIT(midi_init, SI_SUB_LOCK, SI_ORDER_FIRST, midi_uninit, NULL); |