Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/usb/template/usb_template_audio.c
/* $FreeBSD$ */ | /* $FreeBSD$ */ | ||||
/*- | /*- | ||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | * SPDX-License-Identifier: BSD-2-Clause-FreeBSD | ||||
* | * | ||||
* Copyright (c) 2010 Hans Petter Selasky. All rights reserved. | * Copyright (c) 2010 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 36 Lines | |||||
#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_cdc.h> | #include <dev/usb/usb_cdc.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_AUDIO_LANG, | AUDIO_LANG_INDEX, | ||||
INDEX_AUDIO_MIXER, | AUDIO_MIXER_INDEX, | ||||
INDEX_AUDIO_RECORD, | AUDIO_RECORD_INDEX, | ||||
INDEX_AUDIO_PLAYBACK, | AUDIO_PLAYBACK_INDEX, | ||||
INDEX_AUDIO_PRODUCT, | AUDIO_PRODUCT_INDEX, | ||||
INDEX_AUDIO_MAX, | AUDIO_MAX_INDEX, | ||||
}; | }; | ||||
#define STRING_AUDIO_PRODUCT \ | #define AUDIO_DEFAULT_PRODUCT "Audio Test Device" | ||||
"A\0u\0d\0i\0o\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e" | #define AUDIO_DEFAULT_MIXER "Mixer interface" | ||||
#define AUDIO_DEFAULT_RECORD "Record interface" | |||||
#define AUDIO_DEFAULT_PLAYBACK "Playback interface" | |||||
#define STRING_AUDIO_MIXER \ | static struct usb_string_descriptor audio_mixer; | ||||
"M\0i\0x\0e\0r\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" | static struct usb_string_descriptor audio_record; | ||||
static struct usb_string_descriptor audio_playback; | |||||
static struct usb_string_descriptor audio_product; | |||||
#define STRING_AUDIO_RECORD \ | static struct sysctl_ctx_list audio_ctx_list; | ||||
"R\0e\0c\0o\0r\0d\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" | |||||
#define STRING_AUDIO_PLAYBACK \ | |||||
"P\0l\0a\0y\0b\0a\0c\0k\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e" | |||||
/* make the real string descriptors */ | |||||
USB_MAKE_STRING_DESC(STRING_AUDIO_MIXER, string_audio_mixer); | |||||
USB_MAKE_STRING_DESC(STRING_AUDIO_RECORD, string_audio_record); | |||||
USB_MAKE_STRING_DESC(STRING_AUDIO_PLAYBACK, string_audio_playback); | |||||
USB_MAKE_STRING_DESC(STRING_AUDIO_PRODUCT, string_audio_product); | |||||
/* prototypes */ | /* prototypes */ | ||||
/* | /* | ||||
* Audio Mixer description structures | * Audio Mixer description structures | ||||
* | * | ||||
* Some of the audio descriptors were dumped | * Some of the audio descriptors were dumped | ||||
* from a Creative Labs USB audio device. | * from a Creative Labs USB audio device. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
static const struct usb_temp_interface_desc audio_iface_0 = { | static const struct usb_temp_interface_desc audio_iface_0 = { | ||||
.ppEndpoints = NULL, /* no endpoints */ | .ppEndpoints = NULL, /* no endpoints */ | ||||
.ppRawDesc = audio_raw_iface_0_desc, | .ppRawDesc = audio_raw_iface_0_desc, | ||||
.bInterfaceClass = UICLASS_AUDIO, | .bInterfaceClass = UICLASS_AUDIO, | ||||
.bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL, | .bInterfaceSubClass = UISUBCLASS_AUDIOCONTROL, | ||||
.bInterfaceProtocol = 0, | .bInterfaceProtocol = 0, | ||||
.iInterface = INDEX_AUDIO_MIXER, | .iInterface = AUDIO_MIXER_INDEX, | ||||
}; | }; | ||||
static const uint8_t audio_raw_desc_20[] = { | static const uint8_t audio_raw_desc_20[] = { | ||||
0x07, 0x24, 0x01, 0x01, 0x03, 0x01, 0x00 | 0x07, 0x24, 0x01, 0x01, 0x03, 0x01, 0x00 | ||||
}; | }; | ||||
static const uint8_t audio_raw_desc_21[] = { | static const uint8_t audio_raw_desc_21[] = { | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
static const struct usb_temp_interface_desc audio_iface_1_alt_0 = { | static const struct usb_temp_interface_desc audio_iface_1_alt_0 = { | ||||
.ppEndpoints = NULL, /* no endpoints */ | .ppEndpoints = NULL, /* no endpoints */ | ||||
.ppRawDesc = NULL, /* no raw descriptors */ | .ppRawDesc = NULL, /* no raw descriptors */ | ||||
.bInterfaceClass = UICLASS_AUDIO, | .bInterfaceClass = UICLASS_AUDIO, | ||||
.bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, | .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, | ||||
.bInterfaceProtocol = 0, | .bInterfaceProtocol = 0, | ||||
.iInterface = INDEX_AUDIO_PLAYBACK, | .iInterface = AUDIO_PLAYBACK_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_interface_desc audio_iface_1_alt_1 = { | static const struct usb_temp_interface_desc audio_iface_1_alt_1 = { | ||||
.ppEndpoints = audio_iface_1_ep, | .ppEndpoints = audio_iface_1_ep, | ||||
.ppRawDesc = audio_raw_iface_1_desc, | .ppRawDesc = audio_raw_iface_1_desc, | ||||
.bInterfaceClass = UICLASS_AUDIO, | .bInterfaceClass = UICLASS_AUDIO, | ||||
.bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, | .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, | ||||
.bInterfaceProtocol = 0, | .bInterfaceProtocol = 0, | ||||
.iInterface = INDEX_AUDIO_PLAYBACK, | .iInterface = AUDIO_PLAYBACK_INDEX, | ||||
.isAltInterface = 1, /* this is an alternate setting */ | .isAltInterface = 1, /* this is an alternate setting */ | ||||
}; | }; | ||||
static const uint8_t audio_raw_desc_30[] = { | static const uint8_t audio_raw_desc_30[] = { | ||||
0x07, 0x24, 0x01, 0x07, 0x01, 0x01, 0x00 | 0x07, 0x24, 0x01, 0x07, 0x01, 0x01, 0x00 | ||||
}; | }; | ||||
Show All 32 Lines | |||||
}; | }; | ||||
static const struct usb_temp_interface_desc audio_iface_2_alt_0 = { | static const struct usb_temp_interface_desc audio_iface_2_alt_0 = { | ||||
.ppEndpoints = NULL, /* no endpoints */ | .ppEndpoints = NULL, /* no endpoints */ | ||||
.ppRawDesc = NULL, /* no raw descriptors */ | .ppRawDesc = NULL, /* no raw descriptors */ | ||||
.bInterfaceClass = UICLASS_AUDIO, | .bInterfaceClass = UICLASS_AUDIO, | ||||
.bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, | .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, | ||||
.bInterfaceProtocol = 0, | .bInterfaceProtocol = 0, | ||||
.iInterface = INDEX_AUDIO_RECORD, | .iInterface = AUDIO_RECORD_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_interface_desc audio_iface_2_alt_1 = { | static const struct usb_temp_interface_desc audio_iface_2_alt_1 = { | ||||
.ppEndpoints = audio_iface_2_ep, | .ppEndpoints = audio_iface_2_ep, | ||||
.ppRawDesc = audio_raw_iface_2_desc, | .ppRawDesc = audio_raw_iface_2_desc, | ||||
.bInterfaceClass = UICLASS_AUDIO, | .bInterfaceClass = UICLASS_AUDIO, | ||||
.bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, | .bInterfaceSubClass = UISUBCLASS_AUDIOSTREAM, | ||||
.bInterfaceProtocol = 0, | .bInterfaceProtocol = 0, | ||||
.iInterface = INDEX_AUDIO_RECORD, | .iInterface = AUDIO_RECORD_INDEX, | ||||
.isAltInterface = 1, /* this is an alternate setting */ | .isAltInterface = 1, /* this is an alternate setting */ | ||||
}; | }; | ||||
static const struct usb_temp_interface_desc *audio_interfaces[] = { | static const struct usb_temp_interface_desc *audio_interfaces[] = { | ||||
&audio_iface_0, | &audio_iface_0, | ||||
&audio_iface_1_alt_0, | &audio_iface_1_alt_0, | ||||
&audio_iface_1_alt_1, | &audio_iface_1_alt_1, | ||||
&audio_iface_2_alt_0, | &audio_iface_2_alt_0, | ||||
&audio_iface_2_alt_1, | &audio_iface_2_alt_1, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
static const struct usb_temp_config_desc audio_config_desc = { | static const struct usb_temp_config_desc audio_config_desc = { | ||||
.ppIfaceDesc = audio_interfaces, | .ppIfaceDesc = audio_interfaces, | ||||
.bmAttributes = UC_BUS_POWERED, | .bmAttributes = UC_BUS_POWERED, | ||||
.bMaxPower = 25, /* 50 mA */ | .bMaxPower = 25, /* 50 mA */ | ||||
.iConfiguration = INDEX_AUDIO_PRODUCT, | .iConfiguration = AUDIO_PRODUCT_INDEX, | ||||
}; | }; | ||||
static const struct usb_temp_config_desc *audio_configs[] = { | static const struct usb_temp_config_desc *audio_configs[] = { | ||||
&audio_config_desc, | &audio_config_desc, | ||||
NULL, | NULL, | ||||
}; | }; | ||||
static usb_temp_get_string_desc_t audio_get_string_desc; | static usb_temp_get_string_desc_t audio_get_string_desc; | ||||
const struct usb_temp_device_desc usb_template_audio = { | struct usb_temp_device_desc usb_template_audio = { | ||||
.getStringDesc = &audio_get_string_desc, | .getStringDesc = &audio_get_string_desc, | ||||
.ppConfigDesc = audio_configs, | .ppConfigDesc = audio_configs, | ||||
.idVendor = USB_TEMPLATE_VENDOR, | .idVendor = USB_TEMPLATE_VENDOR, | ||||
.idProduct = 0x000A, | .idProduct = 0x000A, | ||||
.bcdDevice = 0x0100, | .bcdDevice = 0x0100, | ||||
.bDeviceClass = UDCLASS_COMM, | .bDeviceClass = UDCLASS_COMM, | ||||
.bDeviceSubClass = 0, | .bDeviceSubClass = 0, | ||||
.bDeviceProtocol = 0, | .bDeviceProtocol = 0, | ||||
.iManufacturer = 0, | .iProduct = AUDIO_PRODUCT_INDEX, | ||||
.iProduct = INDEX_AUDIO_PRODUCT, | |||||
.iSerialNumber = 0, | |||||
}; | }; | ||||
/*------------------------------------------------------------------------* | /*------------------------------------------------------------------------* | ||||
* audio_get_string_desc | * audio_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 * | ||||
audio_get_string_desc(uint16_t lang_id, uint8_t string_index) | audio_get_string_desc(uint16_t lang_id, uint8_t string_index) | ||||
{ | { | ||||
static const void *ptr[INDEX_AUDIO_MAX] = { | static const void *ptr[AUDIO_MAX_INDEX] = { | ||||
[INDEX_AUDIO_LANG] = &usb_string_lang_en, | [AUDIO_LANG_INDEX] = &usb_string_lang_en, | ||||
[INDEX_AUDIO_MIXER] = &string_audio_mixer, | [AUDIO_MIXER_INDEX] = &audio_mixer, | ||||
[INDEX_AUDIO_RECORD] = &string_audio_record, | [AUDIO_RECORD_INDEX] = &audio_record, | ||||
[INDEX_AUDIO_PLAYBACK] = &string_audio_playback, | [AUDIO_PLAYBACK_INDEX] = &audio_playback, | ||||
[INDEX_AUDIO_PRODUCT] = &string_audio_product, | [AUDIO_PRODUCT_INDEX] = &audio_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_AUDIO_MAX) { | if (string_index < AUDIO_MAX_INDEX) { | ||||
return (ptr[string_index]); | return (ptr[string_index]); | ||||
} | } | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
static void | |||||
audio_init(void *arg __unused) | |||||
{ | |||||
struct sysctl_oid *parent; | |||||
char parent_name[3]; | |||||
usb_make_str_desc(&audio_mixer, sizeof(audio_mixer), | |||||
AUDIO_DEFAULT_MIXER); | |||||
usb_make_str_desc(&audio_record, sizeof(audio_record), | |||||
AUDIO_DEFAULT_RECORD); | |||||
usb_make_str_desc(&audio_playback, sizeof(audio_playback), | |||||
AUDIO_DEFAULT_PLAYBACK); | |||||
usb_make_str_desc(&audio_product, sizeof(audio_product), | |||||
AUDIO_DEFAULT_PRODUCT); | |||||
snprintf(parent_name, sizeof(parent_name), "%d", USB_TEMP_AUDIO); | |||||
sysctl_ctx_init(&audio_ctx_list); | |||||
parent = SYSCTL_ADD_NODE(&audio_ctx_list, | |||||
SYSCTL_STATIC_CHILDREN(_hw_usb_templates), OID_AUTO, | |||||
parent_name, CTLFLAG_RW, | |||||
0, "USB Audio Interface device side template"); | |||||
SYSCTL_ADD_U16(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"vendor_id", CTLFLAG_RWTUN, &usb_template_audio.idVendor, | |||||
1, "Vendor identifier"); | |||||
SYSCTL_ADD_U16(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"product_id", CTLFLAG_RWTUN, &usb_template_audio.idProduct, | |||||
1, "Product identifier"); | |||||
#if 0 | |||||
SYSCTL_ADD_PROC(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"mixer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, | |||||
&audio_mixer, sizeof(audio_mixer), usb_temp_sysctl, | |||||
"A", "Mixer interface string"); | |||||
SYSCTL_ADD_PROC(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"record", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, | |||||
&audio_record, sizeof(audio_record), usb_temp_sysctl, | |||||
"A", "Record interface string"); | |||||
SYSCTL_ADD_PROC(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"playback", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, | |||||
&audio_playback, sizeof(audio_playback), usb_temp_sysctl, | |||||
"A", "Playback interface string"); | |||||
#endif | |||||
SYSCTL_ADD_PROC(&audio_ctx_list, SYSCTL_CHILDREN(parent), OID_AUTO, | |||||
"product", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, | |||||
&audio_product, sizeof(audio_product), usb_temp_sysctl, | |||||
"A", "Product string"); | |||||
} | |||||
static void | |||||
audio_uninit(void *arg __unused) | |||||
{ | |||||
sysctl_ctx_free(&audio_ctx_list); | |||||
} | |||||
SYSINIT(audio_init, SI_SUB_LOCK, SI_ORDER_FIRST, audio_init, NULL); | |||||
SYSUNINIT(audio_init, SI_SUB_LOCK, SI_ORDER_FIRST, audio_uninit, NULL); |