2022-01-15 17:11:38 +08:00
|
|
|
/**
|
|
|
|
|
* @file usbd_audio.c
|
|
|
|
|
* @brief
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2022 sakumisu
|
|
|
|
|
*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-07-10 18:31:58 +08:00
|
|
|
#include "usbd_core.h"
|
|
|
|
|
#include "usbd_audio.h"
|
|
|
|
|
|
|
|
|
|
struct usbd_audio_control_info audio_control_info = { 0xdb00, 0x0000, 0x0100, 0xf600, 0 };
|
|
|
|
|
|
|
|
|
|
int audio_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
|
|
|
|
|
{
|
2022-01-15 17:07:54 +08:00
|
|
|
USB_LOG_DBG("AUDIO Class request: "
|
2021-11-09 10:28:10 +08:00
|
|
|
"bRequest 0x%02x\r\n",
|
|
|
|
|
setup->bRequest);
|
2021-07-10 18:31:58 +08:00
|
|
|
|
|
|
|
|
switch (setup->bRequest) {
|
|
|
|
|
case AUDIO_REQUEST_SET_CUR:
|
|
|
|
|
|
2021-11-26 23:41:59 +08:00
|
|
|
if (LO_BYTE(setup->wValue) == 0x01) {
|
|
|
|
|
if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_MUTE) {
|
2021-07-10 18:31:58 +08:00
|
|
|
memcpy(&audio_control_info.mute, *data, *len);
|
2021-11-26 23:41:59 +08:00
|
|
|
} else if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_VOLUME) {
|
2021-07-10 18:31:58 +08:00
|
|
|
memcpy(&audio_control_info.vol_current, *data, *len);
|
2021-11-26 23:41:59 +08:00
|
|
|
int vol;
|
2021-09-28 10:51:20 +08:00
|
|
|
if (audio_control_info.vol_current == 0) {
|
|
|
|
|
vol = 100;
|
|
|
|
|
} else {
|
|
|
|
|
vol = (audio_control_info.vol_current - 0xDB00 + 1) * 100 / (0xFFFF - 0xDB00);
|
|
|
|
|
}
|
|
|
|
|
usbd_audio_set_volume(vol);
|
2022-01-15 17:07:54 +08:00
|
|
|
USB_LOG_INFO("current audio volume:%d\r\n", vol);
|
2021-07-10 18:31:58 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AUDIO_REQUEST_GET_CUR:
|
2021-11-26 23:41:59 +08:00
|
|
|
if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_MUTE) {
|
2021-07-10 18:31:58 +08:00
|
|
|
*data = (uint8_t *)&audio_control_info.mute;
|
|
|
|
|
*len = 1;
|
2021-11-26 23:41:59 +08:00
|
|
|
} else if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_VOLUME) {
|
2021-07-10 18:31:58 +08:00
|
|
|
*data = (uint8_t *)&audio_control_info.vol_current;
|
|
|
|
|
*len = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AUDIO_REQUEST_SET_RES:
|
|
|
|
|
break;
|
|
|
|
|
|
2021-11-09 10:28:10 +08:00
|
|
|
case AUDIO_REQUEST_SET_MEM:
|
|
|
|
|
break;
|
|
|
|
|
|
2021-07-10 18:31:58 +08:00
|
|
|
case AUDIO_REQUEST_GET_MIN:
|
|
|
|
|
*data = (uint8_t *)&audio_control_info.vol_min;
|
|
|
|
|
*len = 2;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AUDIO_REQUEST_GET_MAX:
|
|
|
|
|
*data = (uint8_t *)&audio_control_info.vol_max;
|
|
|
|
|
*len = 2;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AUDIO_REQUEST_GET_RES:
|
|
|
|
|
*data = (uint8_t *)&audio_control_info.vol_res;
|
|
|
|
|
*len = 2;
|
|
|
|
|
break;
|
2021-11-09 10:28:10 +08:00
|
|
|
case AUDIO_REQUEST_GET_MEM:
|
|
|
|
|
*data[0] = 0;
|
|
|
|
|
*len = 1;
|
|
|
|
|
break;
|
2021-07-10 18:31:58 +08:00
|
|
|
|
|
|
|
|
default:
|
2022-01-15 17:07:54 +08:00
|
|
|
USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x\r\n", setup->bRequest);
|
2021-11-13 13:09:22 +08:00
|
|
|
return -1;
|
2021-07-10 18:31:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void audio_notify_handler(uint8_t event, void *arg)
|
|
|
|
|
{
|
|
|
|
|
switch (event) {
|
2021-11-26 23:41:59 +08:00
|
|
|
case USBD_EVENT_RESET:
|
2021-07-10 18:31:58 +08:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
2021-11-26 23:41:59 +08:00
|
|
|
case USBD_EVENT_SOF:
|
2021-07-10 18:31:58 +08:00
|
|
|
break;
|
|
|
|
|
|
2021-11-26 23:41:59 +08:00
|
|
|
case USBD_EVENT_SET_INTERFACE:
|
2021-07-10 18:31:58 +08:00
|
|
|
usbd_audio_set_interface_callback(((uint8_t *)arg)[3]);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-09 10:28:10 +08:00
|
|
|
|
2022-01-30 21:13:58 +08:00
|
|
|
void usbd_audio_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
|
2021-07-10 18:31:58 +08:00
|
|
|
{
|
|
|
|
|
static usbd_class_t *last_class = NULL;
|
|
|
|
|
|
2022-01-30 21:13:58 +08:00
|
|
|
if (last_class != devclass) {
|
|
|
|
|
last_class = devclass;
|
|
|
|
|
usbd_class_register(devclass);
|
2021-07-10 18:31:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intf->class_handler = audio_class_request_handler;
|
|
|
|
|
intf->custom_handler = NULL;
|
|
|
|
|
intf->vendor_handler = NULL;
|
|
|
|
|
intf->notify_handler = audio_notify_handler;
|
2022-01-30 21:13:58 +08:00
|
|
|
usbd_class_add_interface(devclass, intf);
|
2021-11-26 23:41:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__WEAK void usbd_audio_set_volume(uint8_t vol)
|
|
|
|
|
{
|
|
|
|
|
}
|