format codes

This commit is contained in:
sakimisu
2022-02-08 11:44:46 +08:00
parent feb5a8fb52
commit 1edbae8e07
21 changed files with 7215 additions and 7215 deletions

View File

@@ -1,136 +1,136 @@
/**
* @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.
*
*/
#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)
{
USB_LOG_DBG("AUDIO Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
switch (setup->bRequest) {
case AUDIO_REQUEST_SET_CUR:
if (LO_BYTE(setup->wValue) == 0x01) {
if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_MUTE) {
memcpy(&audio_control_info.mute, *data, *len);
} else if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_VOLUME) {
memcpy(&audio_control_info.vol_current, *data, *len);
int vol;
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);
USB_LOG_INFO("current audio volume:%d\r\n", vol);
}
}
break;
case AUDIO_REQUEST_GET_CUR:
if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_MUTE) {
*data = (uint8_t *)&audio_control_info.mute;
*len = 1;
} else if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_VOLUME) {
*data = (uint8_t *)&audio_control_info.vol_current;
*len = 2;
}
break;
case AUDIO_REQUEST_SET_RES:
break;
case AUDIO_REQUEST_SET_MEM:
break;
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;
case AUDIO_REQUEST_GET_MEM:
*data[0] = 0;
*len = 1;
break;
default:
USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
void audio_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
break;
case USBD_EVENT_SOF:
break;
case USBD_EVENT_SET_INTERFACE:
usbd_audio_set_interface_callback(((uint8_t *)arg)[3]);
break;
default:
break;
}
}
void usbd_audio_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static usbd_class_t *last_class = NULL;
if (last_class != devclass) {
last_class = devclass;
usbd_class_register(devclass);
}
intf->class_handler = audio_class_request_handler;
intf->custom_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = audio_notify_handler;
usbd_class_add_interface(devclass, intf);
}
__WEAK void usbd_audio_set_volume(uint8_t vol)
{
}
/**
* @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.
*
*/
#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)
{
USB_LOG_DBG("AUDIO Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
switch (setup->bRequest) {
case AUDIO_REQUEST_SET_CUR:
if (LO_BYTE(setup->wValue) == 0x01) {
if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_MUTE) {
memcpy(&audio_control_info.mute, *data, *len);
} else if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_VOLUME) {
memcpy(&audio_control_info.vol_current, *data, *len);
int vol;
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);
USB_LOG_INFO("current audio volume:%d\r\n", vol);
}
}
break;
case AUDIO_REQUEST_GET_CUR:
if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_MUTE) {
*data = (uint8_t *)&audio_control_info.mute;
*len = 1;
} else if (HI_BYTE(setup->wValue) == AUDIO_FU_CONTROL_VOLUME) {
*data = (uint8_t *)&audio_control_info.vol_current;
*len = 2;
}
break;
case AUDIO_REQUEST_SET_RES:
break;
case AUDIO_REQUEST_SET_MEM:
break;
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;
case AUDIO_REQUEST_GET_MEM:
*data[0] = 0;
*len = 1;
break;
default:
USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
void audio_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
break;
case USBD_EVENT_SOF:
break;
case USBD_EVENT_SET_INTERFACE:
usbd_audio_set_interface_callback(((uint8_t *)arg)[3]);
break;
default:
break;
}
}
void usbd_audio_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static usbd_class_t *last_class = NULL;
if (last_class != devclass) {
last_class = devclass;
usbd_class_register(devclass);
}
intf->class_handler = audio_class_request_handler;
intf->custom_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = audio_notify_handler;
usbd_class_add_interface(devclass, intf);
}
__WEAK void usbd_audio_set_volume(uint8_t vol)
{
}

View File

@@ -1,171 +1,171 @@
/**
* @file usbd_cdc.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.
*
*/
#include "usbd_core.h"
#include "usbd_cdc.h"
const char *stop_name[] = { "1", "1.5", "2" };
const char *parity_name[] = { "N", "O", "E", "M", "S" };
/* Device data structure */
struct cdc_acm_cfg_private {
/* CDC ACM line coding properties. LE order */
struct cdc_line_coding line_coding;
/* CDC ACM line state bitmap, DTE side */
uint8_t line_state;
/* CDC ACM serial state bitmap, DCE side */
uint8_t serial_state;
/* CDC ACM notification sent status */
uint8_t notification_sent;
/* CDC ACM configured flag */
bool configured;
/* CDC ACM suspended flag */
bool suspended;
uint32_t uart_first_init_flag;
} usbd_cdc_acm_cfg;
static void usbd_cdc_acm_reset(void)
{
usbd_cdc_acm_cfg.line_coding.dwDTERate = 2000000;
usbd_cdc_acm_cfg.line_coding.bDataBits = 8;
usbd_cdc_acm_cfg.line_coding.bParityType = 0;
usbd_cdc_acm_cfg.line_coding.bCharFormat = 0;
usbd_cdc_acm_cfg.configured = false;
usbd_cdc_acm_cfg.uart_first_init_flag = 0;
}
/**
* @brief Handler called for Class requests not handled by the USB stack.
*
* @param setup Information about the request to execute.
* @param len Size of the buffer.
* @param data Buffer containing the request result.
*
* @return 0 on success, negative errno code on fail.
*/
static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("CDC Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
switch (setup->bRequest) {
case CDC_REQUEST_SET_LINE_CODING:
/*******************************************************************************/
/* Line Coding Structure */
/*-----------------------------------------------------------------------------*/
/* Offset | Field | Size | Value | Description */
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
/* 4 | bCharFormat | 1 | Number | Stop bits */
/* 0 - 1 Stop bit */
/* 1 - 1.5 Stop bits */
/* 2 - 2 Stop bits */
/* 5 | bParityType | 1 | Number | Parity */
/* 0 - None */
/* 1 - Odd */
/* 2 - Even */
/* 3 - Mark */
/* 4 - Space */
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/
if (usbd_cdc_acm_cfg.uart_first_init_flag == 0) {
usbd_cdc_acm_cfg.uart_first_init_flag = 1;
return 0;
}
memcpy(&usbd_cdc_acm_cfg.line_coding, *data, sizeof(usbd_cdc_acm_cfg.line_coding));
USB_LOG_DBG("CDC_SET_LINE_CODING <%d %d %s %s>\r\n",
usbd_cdc_acm_cfg.line_coding.dwDTERate,
usbd_cdc_acm_cfg.line_coding.bDataBits,
parity_name[usbd_cdc_acm_cfg.line_coding.bParityType],
stop_name[usbd_cdc_acm_cfg.line_coding.bCharFormat]);
usbd_cdc_acm_set_line_coding(usbd_cdc_acm_cfg.line_coding.dwDTERate, usbd_cdc_acm_cfg.line_coding.bDataBits,
usbd_cdc_acm_cfg.line_coding.bParityType, usbd_cdc_acm_cfg.line_coding.bCharFormat);
break;
case CDC_REQUEST_SET_CONTROL_LINE_STATE:
usbd_cdc_acm_cfg.line_state = (uint8_t)setup->wValue;
bool dtr = (setup->wValue & 0x01);
bool rts = (setup->wValue & 0x02);
USB_LOG_DBG("DTR 0x%x,RTS 0x%x\r\n",
dtr, rts);
usbd_cdc_acm_set_dtr(dtr);
usbd_cdc_acm_set_rts(rts);
break;
case CDC_REQUEST_GET_LINE_CODING:
*data = (uint8_t *)(&usbd_cdc_acm_cfg.line_coding);
*len = sizeof(usbd_cdc_acm_cfg.line_coding);
USB_LOG_DBG("CDC_GET_LINE_CODING %d %d %d %d\r\n",
usbd_cdc_acm_cfg.line_coding.dwDTERate,
usbd_cdc_acm_cfg.line_coding.bCharFormat,
usbd_cdc_acm_cfg.line_coding.bParityType,
usbd_cdc_acm_cfg.line_coding.bDataBits);
break;
default:
USB_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
static void cdc_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
usbd_cdc_acm_reset();
break;
default:
break;
}
}
__WEAK void usbd_cdc_acm_set_line_coding(uint32_t baudrate, uint8_t databits, uint8_t parity, uint8_t stopbits)
{
}
__WEAK void usbd_cdc_acm_set_dtr(bool dtr)
{
}
__WEAK void usbd_cdc_acm_set_rts(bool rts)
{
}
void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static usbd_class_t *last_class = NULL;
if (last_class != devclass) {
last_class = devclass;
usbd_class_register(devclass);
}
intf->class_handler = cdc_acm_class_request_handler;
intf->custom_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = cdc_notify_handler;
usbd_class_add_interface(devclass, intf);
}
/**
* @file usbd_cdc.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.
*
*/
#include "usbd_core.h"
#include "usbd_cdc.h"
const char *stop_name[] = { "1", "1.5", "2" };
const char *parity_name[] = { "N", "O", "E", "M", "S" };
/* Device data structure */
struct cdc_acm_cfg_private {
/* CDC ACM line coding properties. LE order */
struct cdc_line_coding line_coding;
/* CDC ACM line state bitmap, DTE side */
uint8_t line_state;
/* CDC ACM serial state bitmap, DCE side */
uint8_t serial_state;
/* CDC ACM notification sent status */
uint8_t notification_sent;
/* CDC ACM configured flag */
bool configured;
/* CDC ACM suspended flag */
bool suspended;
uint32_t uart_first_init_flag;
} usbd_cdc_acm_cfg;
static void usbd_cdc_acm_reset(void)
{
usbd_cdc_acm_cfg.line_coding.dwDTERate = 2000000;
usbd_cdc_acm_cfg.line_coding.bDataBits = 8;
usbd_cdc_acm_cfg.line_coding.bParityType = 0;
usbd_cdc_acm_cfg.line_coding.bCharFormat = 0;
usbd_cdc_acm_cfg.configured = false;
usbd_cdc_acm_cfg.uart_first_init_flag = 0;
}
/**
* @brief Handler called for Class requests not handled by the USB stack.
*
* @param setup Information about the request to execute.
* @param len Size of the buffer.
* @param data Buffer containing the request result.
*
* @return 0 on success, negative errno code on fail.
*/
static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("CDC Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
switch (setup->bRequest) {
case CDC_REQUEST_SET_LINE_CODING:
/*******************************************************************************/
/* Line Coding Structure */
/*-----------------------------------------------------------------------------*/
/* Offset | Field | Size | Value | Description */
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
/* 4 | bCharFormat | 1 | Number | Stop bits */
/* 0 - 1 Stop bit */
/* 1 - 1.5 Stop bits */
/* 2 - 2 Stop bits */
/* 5 | bParityType | 1 | Number | Parity */
/* 0 - None */
/* 1 - Odd */
/* 2 - Even */
/* 3 - Mark */
/* 4 - Space */
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/
if (usbd_cdc_acm_cfg.uart_first_init_flag == 0) {
usbd_cdc_acm_cfg.uart_first_init_flag = 1;
return 0;
}
memcpy(&usbd_cdc_acm_cfg.line_coding, *data, sizeof(usbd_cdc_acm_cfg.line_coding));
USB_LOG_DBG("CDC_SET_LINE_CODING <%d %d %s %s>\r\n",
usbd_cdc_acm_cfg.line_coding.dwDTERate,
usbd_cdc_acm_cfg.line_coding.bDataBits,
parity_name[usbd_cdc_acm_cfg.line_coding.bParityType],
stop_name[usbd_cdc_acm_cfg.line_coding.bCharFormat]);
usbd_cdc_acm_set_line_coding(usbd_cdc_acm_cfg.line_coding.dwDTERate, usbd_cdc_acm_cfg.line_coding.bDataBits,
usbd_cdc_acm_cfg.line_coding.bParityType, usbd_cdc_acm_cfg.line_coding.bCharFormat);
break;
case CDC_REQUEST_SET_CONTROL_LINE_STATE:
usbd_cdc_acm_cfg.line_state = (uint8_t)setup->wValue;
bool dtr = (setup->wValue & 0x01);
bool rts = (setup->wValue & 0x02);
USB_LOG_DBG("DTR 0x%x,RTS 0x%x\r\n",
dtr, rts);
usbd_cdc_acm_set_dtr(dtr);
usbd_cdc_acm_set_rts(rts);
break;
case CDC_REQUEST_GET_LINE_CODING:
*data = (uint8_t *)(&usbd_cdc_acm_cfg.line_coding);
*len = sizeof(usbd_cdc_acm_cfg.line_coding);
USB_LOG_DBG("CDC_GET_LINE_CODING %d %d %d %d\r\n",
usbd_cdc_acm_cfg.line_coding.dwDTERate,
usbd_cdc_acm_cfg.line_coding.bCharFormat,
usbd_cdc_acm_cfg.line_coding.bParityType,
usbd_cdc_acm_cfg.line_coding.bDataBits);
break;
default:
USB_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
static void cdc_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
usbd_cdc_acm_reset();
break;
default:
break;
}
}
__WEAK void usbd_cdc_acm_set_line_coding(uint32_t baudrate, uint8_t databits, uint8_t parity, uint8_t stopbits)
{
}
__WEAK void usbd_cdc_acm_set_dtr(bool dtr)
{
}
__WEAK void usbd_cdc_acm_set_rts(bool rts)
{
}
void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static usbd_class_t *last_class = NULL;
if (last_class != devclass) {
last_class = devclass;
usbd_class_register(devclass);
}
intf->class_handler = cdc_acm_class_request_handler;
intf->custom_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = cdc_notify_handler;
usbd_class_add_interface(devclass, intf);
}

View File

@@ -1,336 +1,336 @@
/**
* @file usbh_cdc_acm.c
*
* 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.
*
*/
#include "usbh_core.h"
#include "usbh_cdc_acm.h"
#define DEV_FORMAT "/dev/ttyACM%d"
#define DEV_NAMELEN 16
static uint32_t g_devinuse = 0;
void usbh_cdc_acm_callback(void *arg, int nbytes);
/****************************************************************************
* Name: usbh_cdc_acm_devno_alloc
*
* Description:
* Allocate a unique /dev/ttyACM[n] minor number in the range 0-31.
*
****************************************************************************/
static int usbh_cdc_acm_devno_alloc(struct usbh_cdc_acm *priv)
{
uint32_t flags;
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 32; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
priv->minor = devno;
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_cdc_acm_devno_free
*
* Description:
* Free a /dev/ttyACM[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_cdc_acm_devno_free(struct usbh_cdc_acm *priv)
{
int devno = priv->minor;
if (devno >= 0 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
/****************************************************************************
* Name: usbh_cdc_acm_mkdevname
*
* Description:
* Format a /dev/ttyACM[n] device name given a minor number.
*
****************************************************************************/
static inline void usbh_cdc_acm_mkdevname(struct usbh_cdc_acm *priv, char *devname)
{
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor);
}
int usbh_cdc_acm_set_line_coding(struct usbh_hubport *hport, uint8_t intf, struct cdc_line_coding *line_coding)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
setup->wValue = 0;
setup->wIndex = intf;
setup->wLength = 7;
ret = usbh_control_transfer(hport->ep0, setup, (uint8_t *)line_coding);
if (ret < 0) {
return ret;
}
memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding));
return 0;
}
int usbh_cdc_acm_get_line_coding(struct usbh_hubport *hport, uint8_t intf, struct cdc_line_coding *line_coding)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
setup->wValue = 0;
setup->wIndex = intf;
setup->wLength = 7;
ret = usbh_control_transfer(hport->ep0, setup, (uint8_t *)line_coding);
if (ret < 0) {
return ret;
}
memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding));
return 0;
}
int usbh_cdc_acm_set_line_state(struct usbh_hubport *hport, uint8_t intf, bool dtr, bool rts)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;
setup->wValue = (dtr << 0) | (rts << 1);
setup->wIndex = intf;
setup->wLength = 0;
ret = usbh_control_transfer(hport->ep0, setup, NULL);
if (ret < 0) {
return ret;
}
cdc_acm_class->dtr = dtr;
cdc_acm_class->rts = rts;
return 0;
}
USB_NOCACHE_RAM_SECTION uint8_t cdc_buffer[4096];
int usbh_cdc_acm_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN];
int ret;
struct usbh_cdc_acm *cdc_acm_class = usb_malloc(sizeof(struct usbh_cdc_acm));
if (cdc_acm_class == NULL) {
USB_LOG_ERR("Fail to alloc cdc_acm_class\r\n");
return -ENOMEM;
}
memset(cdc_acm_class, 0, sizeof(struct usbh_cdc_acm));
usbh_cdc_acm_devno_alloc(cdc_acm_class);
usbh_cdc_acm_mkdevname(cdc_acm_class, devname);
hport->config.intf[intf].priv = cdc_acm_class;
hport->config.intf[intf + 1].priv = cdc_acm_class;
cdc_acm_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (cdc_acm_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM;
}
cdc_acm_class->linecoding = usb_iomalloc(sizeof(struct cdc_line_coding));
if (cdc_acm_class->linecoding == NULL) {
USB_LOG_ERR("Fail to alloc linecoding\r\n");
return -ENOMEM;
}
cdc_acm_class->ctrl_intf = intf;
cdc_acm_class->data_intf = intf + 1;
cdc_acm_class->linecoding->dwDTERate = 115200;
cdc_acm_class->linecoding->bDataBits = 8;
cdc_acm_class->linecoding->bParityType = 0;
cdc_acm_class->linecoding->bCharFormat = 0;
ret = usbh_cdc_acm_set_line_coding(hport, intf, cdc_acm_class->linecoding);
if (ret < 0) {
return ret;
}
ret = usbh_cdc_acm_set_line_state(hport, intf, true, true);
if (ret < 0) {
return ret;
}
#if 0
ep_desc = &hport->config.intf[intf].ep[0].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
usbh_ep_alloc(&cdc_acm_class->intin, &ep_cfg);
#endif
for (uint8_t i = 0; i < hport->config.intf[intf + 1].intf_desc.bNumEndpoints; i++) {
ep_desc = &hport->config.intf[intf + 1].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&cdc_acm_class->bulkin, &ep_cfg);
} else {
usbh_ep_alloc(&cdc_acm_class->bulkout, &ep_cfg);
}
}
USB_LOG_INFO("Register CDC ACM Class:%s\r\n", devname);
memset(cdc_buffer, 0, 512);
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512);
if (ret < 0) {
printf("bulk in error\r\n");
return ret;
}
printf("recv over:%d\r\n", ret);
for (size_t i = 0; i < ret; i++) {
printf("0x%02x ", cdc_buffer[i]);
}
printf("\r\n");
const uint8_t data1[10] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x14 };
memcpy(cdc_buffer, data1, 8);
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkout, cdc_buffer, 8);
if (ret < 0) {
printf("bulk out error\r\n");
return ret;
}
printf("send over:%d\r\n", ret);
#if 0
usbh_ep_bulk_async_transfer(cdc_acm_class->bulkin, cdc_buffer, 512, usbh_cdc_acm_callback, NULL);
#else
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512);
if (ret < 0) {
printf("bulk in error\r\n");
return ret;
}
printf("recv over:%d\r\n", ret);
for (size_t i = 0; i < ret; i++) {
printf("0x%02x ", cdc_buffer[i]);
}
printf("\r\n");
#endif
return ret;
}
int usbh_cdc_acm_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
char devname[DEV_NAMELEN];
int ret = 0;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
if (cdc_acm_class) {
usbh_cdc_acm_devno_free(cdc_acm_class);
usbh_cdc_acm_mkdevname(cdc_acm_class, devname);
if (cdc_acm_class->bulkin) {
ret = usb_ep_cancel(cdc_acm_class->bulkin);
if (ret < 0) {
}
usbh_ep_free(cdc_acm_class->bulkin);
}
if (cdc_acm_class->bulkout) {
ret = usb_ep_cancel(cdc_acm_class->bulkout);
if (ret < 0) {
}
usbh_ep_free(cdc_acm_class->bulkout);
}
if (cdc_acm_class->setup)
usb_iofree(cdc_acm_class->setup);
if (cdc_acm_class->linecoding)
usb_iofree(cdc_acm_class->linecoding);
usb_free(cdc_acm_class);
hport->config.intf[intf].priv = NULL;
hport->config.intf[intf + 1].priv = NULL;
USB_LOG_INFO("Unregister CDC ACM Class:%s\r\n", devname);
}
return ret;
}
void usbh_cdc_acm_callback(void *arg, int result)
{
printf("result:%d\r\n", result);
}
const struct usbh_class_driver cdc_acm_class_driver = {
.driver_name = "cdc_acm",
.connect = usbh_cdc_acm_connect,
.disconnect = usbh_cdc_acm_disconnect
/**
* @file usbh_cdc_acm.c
*
* 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.
*
*/
#include "usbh_core.h"
#include "usbh_cdc_acm.h"
#define DEV_FORMAT "/dev/ttyACM%d"
#define DEV_NAMELEN 16
static uint32_t g_devinuse = 0;
void usbh_cdc_acm_callback(void *arg, int nbytes);
/****************************************************************************
* Name: usbh_cdc_acm_devno_alloc
*
* Description:
* Allocate a unique /dev/ttyACM[n] minor number in the range 0-31.
*
****************************************************************************/
static int usbh_cdc_acm_devno_alloc(struct usbh_cdc_acm *priv)
{
uint32_t flags;
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 32; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
priv->minor = devno;
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_cdc_acm_devno_free
*
* Description:
* Free a /dev/ttyACM[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_cdc_acm_devno_free(struct usbh_cdc_acm *priv)
{
int devno = priv->minor;
if (devno >= 0 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
/****************************************************************************
* Name: usbh_cdc_acm_mkdevname
*
* Description:
* Format a /dev/ttyACM[n] device name given a minor number.
*
****************************************************************************/
static inline void usbh_cdc_acm_mkdevname(struct usbh_cdc_acm *priv, char *devname)
{
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor);
}
int usbh_cdc_acm_set_line_coding(struct usbh_hubport *hport, uint8_t intf, struct cdc_line_coding *line_coding)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
setup->wValue = 0;
setup->wIndex = intf;
setup->wLength = 7;
ret = usbh_control_transfer(hport->ep0, setup, (uint8_t *)line_coding);
if (ret < 0) {
return ret;
}
memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding));
return 0;
}
int usbh_cdc_acm_get_line_coding(struct usbh_hubport *hport, uint8_t intf, struct cdc_line_coding *line_coding)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
setup->wValue = 0;
setup->wIndex = intf;
setup->wLength = 7;
ret = usbh_control_transfer(hport->ep0, setup, (uint8_t *)line_coding);
if (ret < 0) {
return ret;
}
memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding));
return 0;
}
int usbh_cdc_acm_set_line_state(struct usbh_hubport *hport, uint8_t intf, bool dtr, bool rts)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;
setup->wValue = (dtr << 0) | (rts << 1);
setup->wIndex = intf;
setup->wLength = 0;
ret = usbh_control_transfer(hport->ep0, setup, NULL);
if (ret < 0) {
return ret;
}
cdc_acm_class->dtr = dtr;
cdc_acm_class->rts = rts;
return 0;
}
USB_NOCACHE_RAM_SECTION uint8_t cdc_buffer[4096];
int usbh_cdc_acm_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN];
int ret;
struct usbh_cdc_acm *cdc_acm_class = usb_malloc(sizeof(struct usbh_cdc_acm));
if (cdc_acm_class == NULL) {
USB_LOG_ERR("Fail to alloc cdc_acm_class\r\n");
return -ENOMEM;
}
memset(cdc_acm_class, 0, sizeof(struct usbh_cdc_acm));
usbh_cdc_acm_devno_alloc(cdc_acm_class);
usbh_cdc_acm_mkdevname(cdc_acm_class, devname);
hport->config.intf[intf].priv = cdc_acm_class;
hport->config.intf[intf + 1].priv = cdc_acm_class;
cdc_acm_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (cdc_acm_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM;
}
cdc_acm_class->linecoding = usb_iomalloc(sizeof(struct cdc_line_coding));
if (cdc_acm_class->linecoding == NULL) {
USB_LOG_ERR("Fail to alloc linecoding\r\n");
return -ENOMEM;
}
cdc_acm_class->ctrl_intf = intf;
cdc_acm_class->data_intf = intf + 1;
cdc_acm_class->linecoding->dwDTERate = 115200;
cdc_acm_class->linecoding->bDataBits = 8;
cdc_acm_class->linecoding->bParityType = 0;
cdc_acm_class->linecoding->bCharFormat = 0;
ret = usbh_cdc_acm_set_line_coding(hport, intf, cdc_acm_class->linecoding);
if (ret < 0) {
return ret;
}
ret = usbh_cdc_acm_set_line_state(hport, intf, true, true);
if (ret < 0) {
return ret;
}
#if 0
ep_desc = &hport->config.intf[intf].ep[0].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
usbh_ep_alloc(&cdc_acm_class->intin, &ep_cfg);
#endif
for (uint8_t i = 0; i < hport->config.intf[intf + 1].intf_desc.bNumEndpoints; i++) {
ep_desc = &hport->config.intf[intf + 1].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&cdc_acm_class->bulkin, &ep_cfg);
} else {
usbh_ep_alloc(&cdc_acm_class->bulkout, &ep_cfg);
}
}
USB_LOG_INFO("Register CDC ACM Class:%s\r\n", devname);
memset(cdc_buffer, 0, 512);
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512);
if (ret < 0) {
printf("bulk in error\r\n");
return ret;
}
printf("recv over:%d\r\n", ret);
for (size_t i = 0; i < ret; i++) {
printf("0x%02x ", cdc_buffer[i]);
}
printf("\r\n");
const uint8_t data1[10] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x14 };
memcpy(cdc_buffer, data1, 8);
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkout, cdc_buffer, 8);
if (ret < 0) {
printf("bulk out error\r\n");
return ret;
}
printf("send over:%d\r\n", ret);
#if 0
usbh_ep_bulk_async_transfer(cdc_acm_class->bulkin, cdc_buffer, 512, usbh_cdc_acm_callback, NULL);
#else
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512);
if (ret < 0) {
printf("bulk in error\r\n");
return ret;
}
printf("recv over:%d\r\n", ret);
for (size_t i = 0; i < ret; i++) {
printf("0x%02x ", cdc_buffer[i]);
}
printf("\r\n");
#endif
return ret;
}
int usbh_cdc_acm_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
char devname[DEV_NAMELEN];
int ret = 0;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
if (cdc_acm_class) {
usbh_cdc_acm_devno_free(cdc_acm_class);
usbh_cdc_acm_mkdevname(cdc_acm_class, devname);
if (cdc_acm_class->bulkin) {
ret = usb_ep_cancel(cdc_acm_class->bulkin);
if (ret < 0) {
}
usbh_ep_free(cdc_acm_class->bulkin);
}
if (cdc_acm_class->bulkout) {
ret = usb_ep_cancel(cdc_acm_class->bulkout);
if (ret < 0) {
}
usbh_ep_free(cdc_acm_class->bulkout);
}
if (cdc_acm_class->setup)
usb_iofree(cdc_acm_class->setup);
if (cdc_acm_class->linecoding)
usb_iofree(cdc_acm_class->linecoding);
usb_free(cdc_acm_class);
hport->config.intf[intf].priv = NULL;
hport->config.intf[intf + 1].priv = NULL;
USB_LOG_INFO("Unregister CDC ACM Class:%s\r\n", devname);
}
return ret;
}
void usbh_cdc_acm_callback(void *arg, int result)
{
printf("result:%d\r\n", result);
}
const struct usbh_class_driver cdc_acm_class_driver = {
.driver_name = "cdc_acm",
.connect = usbh_cdc_acm_connect,
.disconnect = usbh_cdc_acm_disconnect
};

View File

@@ -1,52 +1,52 @@
/**
* @file usbh_cdc_acm.h
*
* 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.
*
*/
#ifndef _USBH_CDC_ACM_H
#define _USBH_CDC_ACM_H
#include "usb_cdc.h"
struct usbh_cdc_acm {
struct usb_setup_packet *setup;
struct cdc_line_coding *linecoding;
uint8_t ctrl_intf; /* Control interface number */
uint8_t data_intf; /* Data interface number */
bool dtr;
bool rts;
uint8_t minor;
usbh_epinfo_t bulkin; /* Bulk IN endpoint */
usbh_epinfo_t bulkout; /* Bulk OUT endpoint */
#ifdef HAVE_INTIN_ENDPOINT
usbh_epinfo_t intin; /* Interrupt IN endpoint (optional) */
#endif
};
extern const struct usbh_class_driver cdc_acm_class_driver;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
/**
* @file usbh_cdc_acm.h
*
* 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.
*
*/
#ifndef _USBH_CDC_ACM_H
#define _USBH_CDC_ACM_H
#include "usb_cdc.h"
struct usbh_cdc_acm {
struct usb_setup_packet *setup;
struct cdc_line_coding *linecoding;
uint8_t ctrl_intf; /* Control interface number */
uint8_t data_intf; /* Data interface number */
bool dtr;
bool rts;
uint8_t minor;
usbh_epinfo_t bulkin; /* Bulk IN endpoint */
usbh_epinfo_t bulkout; /* Bulk OUT endpoint */
#ifdef HAVE_INTIN_ENDPOINT
usbh_epinfo_t intin; /* Interrupt IN endpoint (optional) */
#endif
};
extern const struct usbh_class_driver cdc_acm_class_driver;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,286 +1,286 @@
/**
* @file usbd_hid.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.
*
*/
#include "usbd_core.h"
#include "usbd_hid.h"
#define HID_STATE_IDLE 0
#define HID_STATE_BUSY 1
struct usbd_hid_cfg_private {
const uint8_t *hid_descriptor;
const uint8_t *hid_report_descriptor;
uint32_t hid_report_descriptor_len;
uint8_t current_intf_num;
uint8_t hid_state;
uint8_t report;
uint8_t idle_state;
uint8_t protocol;
uint8_t (*get_report_callback)(uint8_t report_id, uint8_t report_type);
void (*set_report_callback)(uint8_t report_id, uint8_t report_type, uint8_t *report, uint8_t report_len);
uint8_t (*get_idle_callback)(uint8_t report_id);
void (*set_idle_callback)(uint8_t report_id, uint8_t duration);
void (*set_protocol_callback)(uint8_t protocol);
uint8_t (*get_protocol_callback)(void);
usb_slist_t list;
} usbd_hid_cfg[4];
static usb_slist_t usbd_hid_class_head = USB_SLIST_OBJECT_INIT(usbd_hid_class_head);
static void usbd_hid_reset(void)
{
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
hid_intf->hid_state = HID_STATE_IDLE;
hid_intf->report = 0;
hid_intf->idle_state = 0;
hid_intf->protocol = 0;
}
}
int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("HID Custom request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
if (((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_IN) &&
setup->bRequest == USB_REQUEST_GET_DESCRIPTOR) {
uint8_t value = (uint8_t)(setup->wValue >> 8);
uint8_t intf_num = (uint8_t)setup->wIndex;
struct usbd_hid_cfg_private *current_hid_intf = NULL;
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
if (hid_intf->current_intf_num == intf_num) {
current_hid_intf = hid_intf;
break;
}
}
if (current_hid_intf == NULL) {
return -2;
}
switch (value) {
case HID_DESCRIPTOR_TYPE_HID:
USB_LOG_INFO("get HID Descriptor\r\n");
*data = (uint8_t *)current_hid_intf->hid_descriptor;
*len = current_hid_intf->hid_descriptor[0];
break;
case HID_DESCRIPTOR_TYPE_HID_REPORT:
USB_LOG_INFO("get Report Descriptor\r\n");
*data = (uint8_t *)current_hid_intf->hid_report_descriptor;
*len = current_hid_intf->hid_report_descriptor_len;
break;
case HID_DESCRIPTOR_TYPE_HID_PHYSICAL:
USB_LOG_INFO("get PHYSICAL Descriptor\r\n");
break;
default:
return -2;
}
return 0;
}
return -1;
}
int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("HID Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
struct usbd_hid_cfg_private *current_hid_intf = NULL;
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
uint8_t intf_num = (uint8_t)setup->wIndex;
if (hid_intf->current_intf_num == intf_num) {
current_hid_intf = hid_intf;
break;
}
}
if (current_hid_intf == NULL) {
return -2;
}
switch (setup->bRequest) {
case HID_REQUEST_GET_REPORT:
if (current_hid_intf->get_report_callback)
current_hid_intf->report = current_hid_intf->get_report_callback(LO_BYTE(setup->wValue), HI_BYTE(setup->wValue)); /*report id ,report type*/
*data = (uint8_t *)&current_hid_intf->report;
*len = 1;
break;
case HID_REQUEST_GET_IDLE:
if (current_hid_intf->get_idle_callback)
current_hid_intf->idle_state = current_hid_intf->get_idle_callback(LO_BYTE(setup->wValue));
*data = (uint8_t *)&current_hid_intf->idle_state;
*len = 1;
break;
case HID_REQUEST_GET_PROTOCOL:
if (current_hid_intf->get_protocol_callback)
current_hid_intf->protocol = current_hid_intf->get_protocol_callback();
*data = (uint8_t *)&current_hid_intf->protocol;
*len = 1;
break;
case HID_REQUEST_SET_REPORT:
if (current_hid_intf->set_report_callback)
current_hid_intf->set_report_callback(LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), *data, *len); /*report id ,report type,report,report len*/
current_hid_intf->report = **data;
break;
case HID_REQUEST_SET_IDLE:
if (current_hid_intf->set_idle_callback)
current_hid_intf->set_idle_callback(LO_BYTE(setup->wValue), HI_BYTE(setup->wIndex)); /*report id ,duration*/
current_hid_intf->idle_state = HI_BYTE(setup->wIndex);
break;
case HID_REQUEST_SET_PROTOCOL:
if (current_hid_intf->set_protocol_callback)
current_hid_intf->set_protocol_callback(LO_BYTE(setup->wValue)); /*protocol*/
current_hid_intf->protocol = LO_BYTE(setup->wValue);
break;
default:
USB_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
static void hid_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
usbd_hid_reset();
break;
default:
break;
}
}
void usbd_hid_reset_state(void)
{
// usbd_hid_cfg.hid_state = HID_STATE_IDLE;
}
void usbd_hid_send_report(uint8_t ep, uint8_t *data, uint8_t len)
{
// if(usbd_hid_cfg.hid_state == HID_STATE_IDLE)
// {
// usbd_hid_cfg.hid_state = HID_STATE_BUSY;
// usbd_ep_write(ep, data, len, NULL);
// }
}
void usbd_hid_descriptor_register(uint8_t intf_num, const uint8_t *desc)
{
// usbd_hid_cfg.hid_descriptor = desc;
}
void usbd_hid_report_descriptor_register(uint8_t intf_num, const uint8_t *desc, uint32_t desc_len)
{
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
if (hid_intf->current_intf_num == intf_num) {
hid_intf->hid_report_descriptor = desc;
hid_intf->hid_report_descriptor_len = desc_len;
return;
}
}
}
// clang-format off
void usbd_hid_set_request_callback( uint8_t intf_num,
uint8_t (*get_report_callback)(uint8_t report_id, uint8_t report_type),
void (*set_report_callback)(uint8_t report_id, uint8_t report_type, uint8_t *report, uint8_t report_len),
uint8_t (*get_idle_callback)(uint8_t report_id),
void (*set_idle_callback)(uint8_t report_id, uint8_t duration),
void (*set_protocol_callback)(uint8_t protocol),
uint8_t (*get_protocol_callback)(void))
// clang-format on
{
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
if (hid_intf->current_intf_num == intf_num) {
if (get_report_callback)
hid_intf->get_report_callback = get_report_callback;
if (set_report_callback)
hid_intf->set_report_callback = set_report_callback;
if (get_idle_callback)
hid_intf->get_idle_callback = get_idle_callback;
if (set_idle_callback)
hid_intf->set_idle_callback = set_idle_callback;
if (set_protocol_callback)
hid_intf->set_protocol_callback = set_protocol_callback;
if (get_protocol_callback)
hid_intf->get_protocol_callback = get_protocol_callback;
return;
}
}
}
void usbd_hid_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static usbd_class_t *last_class = NULL;
static uint8_t hid_num = 0;
if (last_class != devclass) {
last_class = devclass;
usbd_class_register(devclass);
}
intf->class_handler = hid_class_request_handler;
intf->custom_handler = hid_custom_request_handler;
intf->vendor_handler = NULL;
intf->notify_handler = hid_notify_handler;
usbd_class_add_interface(devclass, intf);
usbd_hid_cfg[hid_num].current_intf_num = intf->intf_num;
usb_slist_add_tail(&usbd_hid_class_head, &usbd_hid_cfg[hid_num].list);
hid_num++;
}
/**
* @file usbd_hid.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.
*
*/
#include "usbd_core.h"
#include "usbd_hid.h"
#define HID_STATE_IDLE 0
#define HID_STATE_BUSY 1
struct usbd_hid_cfg_private {
const uint8_t *hid_descriptor;
const uint8_t *hid_report_descriptor;
uint32_t hid_report_descriptor_len;
uint8_t current_intf_num;
uint8_t hid_state;
uint8_t report;
uint8_t idle_state;
uint8_t protocol;
uint8_t (*get_report_callback)(uint8_t report_id, uint8_t report_type);
void (*set_report_callback)(uint8_t report_id, uint8_t report_type, uint8_t *report, uint8_t report_len);
uint8_t (*get_idle_callback)(uint8_t report_id);
void (*set_idle_callback)(uint8_t report_id, uint8_t duration);
void (*set_protocol_callback)(uint8_t protocol);
uint8_t (*get_protocol_callback)(void);
usb_slist_t list;
} usbd_hid_cfg[4];
static usb_slist_t usbd_hid_class_head = USB_SLIST_OBJECT_INIT(usbd_hid_class_head);
static void usbd_hid_reset(void)
{
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
hid_intf->hid_state = HID_STATE_IDLE;
hid_intf->report = 0;
hid_intf->idle_state = 0;
hid_intf->protocol = 0;
}
}
int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("HID Custom request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
if (((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_IN) &&
setup->bRequest == USB_REQUEST_GET_DESCRIPTOR) {
uint8_t value = (uint8_t)(setup->wValue >> 8);
uint8_t intf_num = (uint8_t)setup->wIndex;
struct usbd_hid_cfg_private *current_hid_intf = NULL;
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
if (hid_intf->current_intf_num == intf_num) {
current_hid_intf = hid_intf;
break;
}
}
if (current_hid_intf == NULL) {
return -2;
}
switch (value) {
case HID_DESCRIPTOR_TYPE_HID:
USB_LOG_INFO("get HID Descriptor\r\n");
*data = (uint8_t *)current_hid_intf->hid_descriptor;
*len = current_hid_intf->hid_descriptor[0];
break;
case HID_DESCRIPTOR_TYPE_HID_REPORT:
USB_LOG_INFO("get Report Descriptor\r\n");
*data = (uint8_t *)current_hid_intf->hid_report_descriptor;
*len = current_hid_intf->hid_report_descriptor_len;
break;
case HID_DESCRIPTOR_TYPE_HID_PHYSICAL:
USB_LOG_INFO("get PHYSICAL Descriptor\r\n");
break;
default:
return -2;
}
return 0;
}
return -1;
}
int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("HID Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
struct usbd_hid_cfg_private *current_hid_intf = NULL;
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
uint8_t intf_num = (uint8_t)setup->wIndex;
if (hid_intf->current_intf_num == intf_num) {
current_hid_intf = hid_intf;
break;
}
}
if (current_hid_intf == NULL) {
return -2;
}
switch (setup->bRequest) {
case HID_REQUEST_GET_REPORT:
if (current_hid_intf->get_report_callback)
current_hid_intf->report = current_hid_intf->get_report_callback(LO_BYTE(setup->wValue), HI_BYTE(setup->wValue)); /*report id ,report type*/
*data = (uint8_t *)&current_hid_intf->report;
*len = 1;
break;
case HID_REQUEST_GET_IDLE:
if (current_hid_intf->get_idle_callback)
current_hid_intf->idle_state = current_hid_intf->get_idle_callback(LO_BYTE(setup->wValue));
*data = (uint8_t *)&current_hid_intf->idle_state;
*len = 1;
break;
case HID_REQUEST_GET_PROTOCOL:
if (current_hid_intf->get_protocol_callback)
current_hid_intf->protocol = current_hid_intf->get_protocol_callback();
*data = (uint8_t *)&current_hid_intf->protocol;
*len = 1;
break;
case HID_REQUEST_SET_REPORT:
if (current_hid_intf->set_report_callback)
current_hid_intf->set_report_callback(LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), *data, *len); /*report id ,report type,report,report len*/
current_hid_intf->report = **data;
break;
case HID_REQUEST_SET_IDLE:
if (current_hid_intf->set_idle_callback)
current_hid_intf->set_idle_callback(LO_BYTE(setup->wValue), HI_BYTE(setup->wIndex)); /*report id ,duration*/
current_hid_intf->idle_state = HI_BYTE(setup->wIndex);
break;
case HID_REQUEST_SET_PROTOCOL:
if (current_hid_intf->set_protocol_callback)
current_hid_intf->set_protocol_callback(LO_BYTE(setup->wValue)); /*protocol*/
current_hid_intf->protocol = LO_BYTE(setup->wValue);
break;
default:
USB_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
static void hid_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
usbd_hid_reset();
break;
default:
break;
}
}
void usbd_hid_reset_state(void)
{
// usbd_hid_cfg.hid_state = HID_STATE_IDLE;
}
void usbd_hid_send_report(uint8_t ep, uint8_t *data, uint8_t len)
{
// if(usbd_hid_cfg.hid_state == HID_STATE_IDLE)
// {
// usbd_hid_cfg.hid_state = HID_STATE_BUSY;
// usbd_ep_write(ep, data, len, NULL);
// }
}
void usbd_hid_descriptor_register(uint8_t intf_num, const uint8_t *desc)
{
// usbd_hid_cfg.hid_descriptor = desc;
}
void usbd_hid_report_descriptor_register(uint8_t intf_num, const uint8_t *desc, uint32_t desc_len)
{
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
if (hid_intf->current_intf_num == intf_num) {
hid_intf->hid_report_descriptor = desc;
hid_intf->hid_report_descriptor_len = desc_len;
return;
}
}
}
// clang-format off
void usbd_hid_set_request_callback( uint8_t intf_num,
uint8_t (*get_report_callback)(uint8_t report_id, uint8_t report_type),
void (*set_report_callback)(uint8_t report_id, uint8_t report_type, uint8_t *report, uint8_t report_len),
uint8_t (*get_idle_callback)(uint8_t report_id),
void (*set_idle_callback)(uint8_t report_id, uint8_t duration),
void (*set_protocol_callback)(uint8_t protocol),
uint8_t (*get_protocol_callback)(void))
// clang-format on
{
usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head)
{
struct usbd_hid_cfg_private *hid_intf = usb_slist_entry(i, struct usbd_hid_cfg_private, list);
if (hid_intf->current_intf_num == intf_num) {
if (get_report_callback)
hid_intf->get_report_callback = get_report_callback;
if (set_report_callback)
hid_intf->set_report_callback = set_report_callback;
if (get_idle_callback)
hid_intf->get_idle_callback = get_idle_callback;
if (set_idle_callback)
hid_intf->set_idle_callback = set_idle_callback;
if (set_protocol_callback)
hid_intf->set_protocol_callback = set_protocol_callback;
if (get_protocol_callback)
hid_intf->get_protocol_callback = get_protocol_callback;
return;
}
}
}
void usbd_hid_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static usbd_class_t *last_class = NULL;
static uint8_t hid_num = 0;
if (last_class != devclass) {
last_class = devclass;
usbd_class_register(devclass);
}
intf->class_handler = hid_class_request_handler;
intf->custom_handler = hid_custom_request_handler;
intf->vendor_handler = NULL;
intf->notify_handler = hid_notify_handler;
usbd_class_add_interface(devclass, intf);
usbd_hid_cfg[hid_num].current_intf_num = intf->intf_num;
usb_slist_add_tail(&usbd_hid_class_head, &usbd_hid_cfg[hid_num].list);
hid_num++;
}

View File

@@ -1,275 +1,275 @@
/**
* @file usbh_hid.c
*
* 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.
*
*/
#include "usbh_core.h"
#include "usbh_hid.h"
#define DEV_FORMAT "/dev/input%d"
#define DEV_NAMELEN 16
static uint32_t g_devinuse = 0;
/****************************************************************************
* Name: usbh_hid_devno_alloc
*
* Description:
* Allocate a unique /dev/hid[n] minor number in the range 0-31.
*
****************************************************************************/
static int usbh_hid_devno_alloc(struct usbh_hid *priv)
{
uint32_t flags;
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 32; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
priv->minor = devno;
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_hid_devno_free
*
* Description:
* Free a /dev/hid[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_hid_devno_free(struct usbh_hid *priv)
{
int devno = priv->minor;
if (devno >= 0 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
/****************************************************************************
* Name: usbh_hid_mkdevname
*
* Description:
* Format a /dev/hid[n] device name given a minor number.
*
****************************************************************************/
static inline void usbh_hid_mkdevname(struct usbh_hid *priv, char *devname)
{
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor);
}
int usbh_hid_get_report_descriptor(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{
struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup;
if (hid_class->intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
setup->wIndex = intf;
setup->wLength = 128;
return usbh_control_transfer(hport->ep0, setup, buffer);
}
int usbh_hid_set_idle(struct usbh_hubport *hport, uint8_t intf, uint8_t report_id, uint8_t duration)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup;
if (hid_class->intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = HID_REQUEST_SET_IDLE;
setup->wValue = report_id;
setup->wIndex = (duration << 8) | intf;
setup->wLength = 0;
ret = usbh_control_transfer(hport->ep0, setup, NULL);
if (ret < 0) {
return ret;
}
return 0;
}
int usbh_hid_get_idle(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup;
if (hid_class->intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = HID_REQUEST_GET_IDLE;
setup->wValue = 0;
setup->wIndex = intf;
setup->wLength = 1;
ret = usbh_control_transfer(hport->ep0, setup, buffer);
if (ret < 0) {
return ret;
}
return 0;
}
USB_NOCACHE_RAM_SECTION uint8_t report_buffer[128];
void usbh_hid_callback(void *arg, int nbytes)
{
printf("nbytes:%d\r\n", nbytes);
}
int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN];
int ret;
struct usbh_hid *hid_class = usb_malloc(sizeof(struct usbh_hid));
if (hid_class == NULL) {
USB_LOG_ERR("Fail to alloc hid_class\r\n");
return -ENOMEM;
}
memset(hid_class, 0, sizeof(struct usbh_hid));
usbh_hid_devno_alloc(hid_class);
usbh_hid_mkdevname(hid_class, devname);
hport->config.intf[intf].priv = hid_class;
hid_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (hid_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM;
}
hid_class->intf = intf;
ret = usbh_hid_set_idle(hport, intf, 0, 0);
if (ret < 0) {
return ret;
}
ret = usbh_hid_get_report_descriptor(hport, intf, report_buffer);
if (ret < 0) {
return ret;
}
for (uint8_t i = 0; i < hport->config.intf[intf].intf_desc.bNumEndpoints; i++) {
ep_desc = &hport->config.intf[intf].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&hid_class->intin, &ep_cfg);
} else {
usbh_ep_alloc(&hid_class->intout, &ep_cfg);
}
}
USB_LOG_INFO("Register HID Class:%s\r\n", devname);
ret = usbh_ep_intr_async_transfer(hid_class->intin, report_buffer, 128, usbh_hid_callback, NULL);
if (ret < 0) {
return ret;
}
#if 0
ret = usbh_ep_intr_transfer(hid_class->intin, report_buffer, 128);
if (ret < 0) {
return ret;
}
USB_LOG_INFO("recv len:%d\r\n", ret);
#endif
return 0;
}
int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
char devname[DEV_NAMELEN];
int ret = 0;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
if (hid_class) {
usbh_hid_devno_free(hid_class);
usbh_hid_mkdevname(hid_class, devname);
if (hid_class->intin) {
ret = usb_ep_cancel(hid_class->intin);
if (ret < 0) {
}
usbh_ep_free(hid_class->intin);
}
if (hid_class->intout) {
ret = usb_ep_cancel(hid_class->intout);
if (ret < 0) {
}
usbh_ep_free(hid_class->intout);
}
if (hid_class->setup)
usb_iofree(hid_class->setup);
usb_free(hid_class);
hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister HID Class:%s\r\n", devname);
}
return ret;
}
const struct usbh_class_driver hid_class_driver = {
.driver_name = "hid",
.connect = usbh_hid_connect,
.disconnect = usbh_hid_disconnect
/**
* @file usbh_hid.c
*
* 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.
*
*/
#include "usbh_core.h"
#include "usbh_hid.h"
#define DEV_FORMAT "/dev/input%d"
#define DEV_NAMELEN 16
static uint32_t g_devinuse = 0;
/****************************************************************************
* Name: usbh_hid_devno_alloc
*
* Description:
* Allocate a unique /dev/hid[n] minor number in the range 0-31.
*
****************************************************************************/
static int usbh_hid_devno_alloc(struct usbh_hid *priv)
{
uint32_t flags;
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 32; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
priv->minor = devno;
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_hid_devno_free
*
* Description:
* Free a /dev/hid[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_hid_devno_free(struct usbh_hid *priv)
{
int devno = priv->minor;
if (devno >= 0 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
/****************************************************************************
* Name: usbh_hid_mkdevname
*
* Description:
* Format a /dev/hid[n] device name given a minor number.
*
****************************************************************************/
static inline void usbh_hid_mkdevname(struct usbh_hid *priv, char *devname)
{
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor);
}
int usbh_hid_get_report_descriptor(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{
struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup;
if (hid_class->intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
setup->wIndex = intf;
setup->wLength = 128;
return usbh_control_transfer(hport->ep0, setup, buffer);
}
int usbh_hid_set_idle(struct usbh_hubport *hport, uint8_t intf, uint8_t report_id, uint8_t duration)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup;
if (hid_class->intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = HID_REQUEST_SET_IDLE;
setup->wValue = report_id;
setup->wIndex = (duration << 8) | intf;
setup->wLength = 0;
ret = usbh_control_transfer(hport->ep0, setup, NULL);
if (ret < 0) {
return ret;
}
return 0;
}
int usbh_hid_get_idle(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{
int ret;
struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup;
if (hid_class->intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = HID_REQUEST_GET_IDLE;
setup->wValue = 0;
setup->wIndex = intf;
setup->wLength = 1;
ret = usbh_control_transfer(hport->ep0, setup, buffer);
if (ret < 0) {
return ret;
}
return 0;
}
USB_NOCACHE_RAM_SECTION uint8_t report_buffer[128];
void usbh_hid_callback(void *arg, int nbytes)
{
printf("nbytes:%d\r\n", nbytes);
}
int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN];
int ret;
struct usbh_hid *hid_class = usb_malloc(sizeof(struct usbh_hid));
if (hid_class == NULL) {
USB_LOG_ERR("Fail to alloc hid_class\r\n");
return -ENOMEM;
}
memset(hid_class, 0, sizeof(struct usbh_hid));
usbh_hid_devno_alloc(hid_class);
usbh_hid_mkdevname(hid_class, devname);
hport->config.intf[intf].priv = hid_class;
hid_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (hid_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM;
}
hid_class->intf = intf;
ret = usbh_hid_set_idle(hport, intf, 0, 0);
if (ret < 0) {
return ret;
}
ret = usbh_hid_get_report_descriptor(hport, intf, report_buffer);
if (ret < 0) {
return ret;
}
for (uint8_t i = 0; i < hport->config.intf[intf].intf_desc.bNumEndpoints; i++) {
ep_desc = &hport->config.intf[intf].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&hid_class->intin, &ep_cfg);
} else {
usbh_ep_alloc(&hid_class->intout, &ep_cfg);
}
}
USB_LOG_INFO("Register HID Class:%s\r\n", devname);
ret = usbh_ep_intr_async_transfer(hid_class->intin, report_buffer, 128, usbh_hid_callback, NULL);
if (ret < 0) {
return ret;
}
#if 0
ret = usbh_ep_intr_transfer(hid_class->intin, report_buffer, 128);
if (ret < 0) {
return ret;
}
USB_LOG_INFO("recv len:%d\r\n", ret);
#endif
return 0;
}
int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
char devname[DEV_NAMELEN];
int ret = 0;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
if (hid_class) {
usbh_hid_devno_free(hid_class);
usbh_hid_mkdevname(hid_class, devname);
if (hid_class->intin) {
ret = usb_ep_cancel(hid_class->intin);
if (ret < 0) {
}
usbh_ep_free(hid_class->intin);
}
if (hid_class->intout) {
ret = usb_ep_cancel(hid_class->intout);
if (ret < 0) {
}
usbh_ep_free(hid_class->intout);
}
if (hid_class->setup)
usb_iofree(hid_class->setup);
usb_free(hid_class);
hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister HID Class:%s\r\n", devname);
}
return ret;
}
const struct usbh_class_driver hid_class_driver = {
.driver_name = "hid",
.connect = usbh_hid_connect,
.disconnect = usbh_hid_disconnect
};

View File

@@ -1,45 +1,45 @@
/**
* @file usbh_hid.h
*
* 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.
*
*/
#ifndef _USBH_HID_H
#define _USBH_HID_H
#include "usb_hid.h"
struct usbh_hid {
struct usb_setup_packet *setup;
uint8_t intf; /* interface number */
uint8_t minor;
usbh_epinfo_t intin; /* INTR IN endpoint */
usbh_epinfo_t intout; /* INTR OUT endpoint */
};
extern const struct usbh_class_driver hid_class_driver;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
/**
* @file usbh_hid.h
*
* 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.
*
*/
#ifndef _USBH_HID_H
#define _USBH_HID_H
#include "usb_hid.h"
struct usbh_hid {
struct usb_setup_packet *setup;
uint8_t intf; /* interface number */
uint8_t minor;
usbh_epinfo_t intin; /* INTR IN endpoint */
usbh_epinfo_t intout; /* INTR OUT endpoint */
};
extern const struct usbh_class_driver hid_class_driver;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,497 +1,497 @@
/**
* @file usbh_hub.c
*
* 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.
*
*/
#include "usbh_core.h"
#include "usbh_hub.h"
#define DEV_FORMAT "/dev/hub%d"
#define DEV_NAMELEN 16
static uint32_t g_devinuse = 0;
usb_slist_t hub_class_head = USB_SLIST_OBJECT_INIT(hub_class_head);
USB_NOCACHE_RAM_SECTION uint8_t int_buffer[6][USBH_HUB_INTIN_BUFSIZE];
extern void usbh_external_hport_connect(struct usbh_hubport *hport);
extern void usbh_external_hport_disconnect(struct usbh_hubport *hport);
extern void usbh_hport_activate(struct usbh_hubport *hport);
extern void usbh_hport_deactivate(struct usbh_hubport *hport);
static void usbh_external_hub_callback(void *arg, int nbytes);
static inline void usbh_hub_register(struct usbh_hub *hub)
{
usb_slist_add_tail(&hub_class_head, &hub->list);
}
static inline void usbh_hub_unregister(struct usbh_hub *hub)
{
usb_slist_remove(&hub_class_head, &hub->list);
}
/****************************************************************************
* Name: usbh_hub_devno_alloc
*
* Description:
* Allocate a unique /dev/hub[n] minor number in the range 2-31.
*
****************************************************************************/
static int usbh_hub_devno_alloc(struct usbh_hub *hub)
{
uint32_t flags;
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 2; devno < 32; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
hub->index = devno;
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_hub_devno_free
*
* Description:
* Free a /dev/hub[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_hub_devno_free(struct usbh_hub *hub)
{
int devno = hub->index;
if (devno >= 2 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
/****************************************************************************
* Name: usbh_hub_mkdevname
*
* Description:
* Format a /dev/hub[n] device name given a minor number.
*
****************************************************************************/
static inline void usbh_hub_mkdevname(struct usbh_hub *hub, char *devname)
{
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, hub->index);
}
int usbh_hub_get_hub_descriptor(struct usbh_hub *hub, uint8_t *buffer)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
setup->wValue = HUB_DESCRIPTOR_TYPE_HUB << 8;
setup->wIndex = 0;
setup->wLength = USB_SIZEOF_HUB_DESC;
return usbh_control_transfer(hub->parent->ep0, setup, buffer);
}
int usbh_hub_get_status(struct usbh_hub *hub, uint8_t *buffer)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0;
setup->wIndex = 0;
setup->wLength = 2;
return usbh_control_transfer(hub->parent->ep0, setup, buffer);
}
int usbh_hub_get_portstatus(struct usbh_hub *hub, uint8_t port, struct hub_port_status *port_status)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0;
setup->wIndex = port;
setup->wLength = 4;
return usbh_control_transfer(hub->parent->ep0, setup, (uint8_t *)port_status);
}
int usbh_hub_set_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_SET_FEATURE;
setup->wValue = feature;
setup->wIndex = port;
setup->wLength = 0;
return usbh_control_transfer(hub->parent->ep0, setup, NULL);
}
int usbh_hub_clear_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_CLEAR_FEATURE;
setup->wValue = feature;
setup->wIndex = port;
setup->wLength = 0;
return usbh_control_transfer(hub->parent->ep0, setup, NULL);
}
static int parse_hub_descriptor(struct usb_hub_descriptor *desc, uint16_t length)
{
if (desc->bLength != USB_SIZEOF_HUB_DESC) {
USB_LOG_ERR("invalid device bLength 0x%02x\r\n", desc->bLength);
return -1;
} else if (desc->bDescriptorType != HUB_DESCRIPTOR_TYPE_HUB) {
USB_LOG_ERR("unexpected descriptor 0x%02x\r\n", desc->bDescriptorType);
return -2;
} else {
USB_LOG_INFO("Device Descriptor:\r\n");
USB_LOG_INFO("bLength: 0x%02x \r\n", desc->bLength);
USB_LOG_INFO("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType);
USB_LOG_INFO("bNbrPorts: 0x%02x \r\n", desc->bNbrPorts);
USB_LOG_INFO("wHubCharacteristics: 0x%04x \r\n", desc->wHubCharacteristics);
USB_LOG_INFO("bPwrOn2PwrGood: 0x%02x \r\n", desc->bPwrOn2PwrGood);
USB_LOG_INFO("bHubContrCurrent: 0x%02x \r\n", desc->bHubContrCurrent);
USB_LOG_INFO("DeviceRemovable: 0x%02x \r\n", desc->DeviceRemovable);
USB_LOG_INFO("PortPwrCtrlMask: 0x%02x \r\n", desc->PortPwrCtrlMask);
}
return 0;
}
int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN];
int ret;
uint8_t *hub_desc_buffer;
struct usbh_hub *hub_class;
hub_class = usb_malloc(sizeof(struct usbh_hub));
if (hub_class == NULL) {
USB_LOG_ERR("Fail to alloc hub_class\r\n");
return -ENOMEM;
}
memset(hub_class, 0, sizeof(struct usbh_hub));
hub_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (hub_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM;
}
hub_class->port_status = usb_iomalloc(sizeof(struct hub_port_status));
if (hub_class->port_status == NULL) {
USB_LOG_ERR("Fail to alloc port_status\r\n");
return -ENOMEM;
}
hub_desc_buffer = usb_iomalloc(32);
usbh_hub_devno_alloc(hub_class);
usbh_hub_mkdevname(hub_class, devname);
hub_class->dev_addr = hport->dev_addr;
hub_class->parent = hport;
hport->config.intf[0].priv = hub_class;
ret = usbh_hub_get_hub_descriptor(hub_class, hub_desc_buffer);
if (ret != 0) {
usb_iofree(hub_desc_buffer);
return ret;
}
parse_hub_descriptor((struct usb_hub_descriptor *)hub_desc_buffer, USB_SIZEOF_HUB_DESC);
memcpy(&hub_class->hub_desc, hub_desc_buffer, USB_SIZEOF_HUB_DESC);
usb_iofree(hub_desc_buffer);
hub_class->nports = hub_class->hub_desc.bNbrPorts;
for (uint8_t port = 1; port <= hub_class->nports; port++) {
hub_class->child[port - 1].port = port;
hub_class->child[port - 1].parent = hub_class;
}
hub_class->int_buffer = int_buffer[hub_class->index - 2];
usbh_hub_register(hub_class);
ep_desc = &hport->config.intf[intf].ep[0].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&hub_class->intin, &ep_cfg);
} else {
return -1;
}
for (uint8_t port = 1; port <= hub_class->nports; port++) {
ret = usbh_hub_set_feature(hub_class, 1, HUB_PORT_FEATURE_POWER);
if (ret < 0) {
return ret;
}
}
for (uint8_t port = 1; port <= hub_class->nports; port++) {
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
USB_LOG_INFO("Port:%d, status:0x%02x, change:0x%02x\r\n", port, hub_class->port_status->wPortStatus, hub_class->port_status->wPortChange);
if (ret < 0) {
return ret;
}
}
USB_LOG_INFO("Register HUB Class:%s\r\n", devname);
ret = usbh_ep_intr_async_transfer(hub_class->intin, hub_class->int_buffer, USBH_HUB_INTIN_BUFSIZE, usbh_external_hub_callback, hub_class);
return 0;
}
int usbh_hub_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_hubport *child;
char devname[DEV_NAMELEN];
int ret = 0;
struct usbh_hub *hub_class = (struct usbh_hub *)hport->config.intf[intf].priv;
if (hub_class) {
usbh_hub_devno_free(hub_class);
usbh_hub_mkdevname(hub_class, devname);
if (hub_class->intin) {
ret = usb_ep_cancel(hub_class->intin);
if (ret < 0) {
}
usbh_ep_free(hub_class->intin);
}
if (hub_class->setup)
usb_iofree(hub_class->setup);
if (hub_class->port_status)
usb_iofree(hub_class->port_status);
for (uint8_t port = 1; port <= hub_class->nports; port++) {
child = &hub_class->child[port - 1];
usbh_hport_deactivate(child);
for (uint8_t i = 0; i < child->config.config_desc.bNumInterfaces; i++) {
if (child->config.intf[i].class_driver && child->config.intf[i].class_driver->disconnect) {
ret = CLASS_DISCONNECT(child, i);
}
}
child->config.config_desc.bNumInterfaces = 0;
child->parent = NULL;
}
usbh_hub_unregister(hub_class);
usb_free(hub_class);
hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister HUB Class:%s\r\n", devname);
}
return ret;
}
static void usbh_extern_hub_psc_event(void *arg)
{
struct usbh_hub *hub_class;
struct usbh_hubport *connport;
uint8_t port_change;
uint16_t status;
uint16_t change;
uint16_t mask;
uint16_t feat;
uint32_t flags;
int ret;
hub_class = (struct usbh_hub *)arg;
/* Has the hub been disconnected? */
if (!hub_class->parent->connected) {
return;
}
port_change = hub_class->int_buffer[0];
USB_LOG_DBG("port_change:0x%02x\r\n", port_change);
/* Check for status change on any port */
for (uint8_t port = USBH_HUB_PORT_START_INDEX; port <= hub_class->nports; port++) {
/* Check if port status has changed */
if ((port_change & (1 << port)) == 0) {
continue;
}
USB_LOG_DBG("Port %d change\r\n", port);
/* Port status changed, check what happened */
port_change &= ~(1 << port);
/* Read hub port status */
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
continue;
}
status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange;
USB_LOG_DBG("Port:%d, status:0x%02x, change:0x%02x\r\n", port, status, change);
/* First, clear all change bits */
mask = 1;
feat = HUB_PORT_FEATURE_C_CONNECTION;
while (change) {
if (change & mask) {
ret = usbh_hub_clear_feature(hub_class, port, feat);
if (ret < 0) {
USB_LOG_ERR("Failed to clear port:%d, change mask:%04x, errorcode:%d\r\n", port, mask, ret);
}
change &= (~mask);
}
mask <<= 1;
feat++;
}
change = hub_class->port_status->wPortChange;
/* Handle connect or disconnect, no power management */
if (change & HUB_PORT_STATUS_C_CONNECTION) {
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
}
status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange;
if (status & HUB_PORT_STATUS_CONNECTION) {
/* Device connected to a port on the hub */
//USB_LOG_INFO("Connection on port:%d\n", port);
ret = usbh_hub_set_feature(hub_class, port, HUB_PORT_FEATURE_RESET);
if (ret < 0) {
USB_LOG_ERR("Failed to reset port:%d,errorcode:%d\r\n", port, ret);
continue;
}
usb_osal_msleep(100);
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
continue;
}
status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange;
USB_LOG_DBG("Port:%d, status:0x%02x, change:0x%02x after reset\r\n", port, status, change);
if ((status & HUB_PORT_STATUS_RESET) == 0 && (status & HUB_PORT_STATUS_ENABLE) != 0) {
if (change & HUB_PORT_STATUS_C_RESET) {
ret = usbh_hub_clear_feature(hub_class, port, HUB_PORT_FEATURE_C_RESET);
if (ret < 0) {
USB_LOG_ERR("Failed to clear port:%d reset change, errorcode: %d\r\n", port, ret);
}
}
connport = &hub_class->child[port - 1];
if (status & HUB_PORT_STATUS_HIGH_SPEED) {
connport->speed = USB_SPEED_HIGH;
} else if (status & HUB_PORT_STATUS_LOW_SPEED) {
connport->speed = USB_SPEED_LOW;
} else {
connport->speed = USB_SPEED_FULL;
}
/* Device connected from a port on the hub, wakeup psc thread. */
usbh_external_hport_connect(connport);
} else {
USB_LOG_ERR("Failed to enable port:%d\r\n", port);
continue;
}
} else {
/* Device disconnected from a port on the hub, wakeup psc thread. */
connport = &hub_class->child[port - 1];
usbh_external_hport_disconnect(connport);
}
} else {
USB_LOG_WRN("status %04x change %04x not handled\r\n", status, change);
}
}
/* Check for hub status change */
if ((port_change & 1) != 0) {
/* Hub status changed */
USB_LOG_WRN("Hub status changed, not handled\n");
}
flags = usb_osal_enter_critical_section();
if (hub_class->parent->connected) {
ret = usbh_ep_intr_async_transfer(hub_class->intin, hub_class->int_buffer, USBH_HUB_INTIN_BUFSIZE, usbh_external_hub_callback, hub_class);
}
usb_osal_leave_critical_section(flags);
}
static void usbh_external_hub_callback(void *arg, int nbytes)
{
struct usbh_hub *hub_class = (struct usbh_hub *)arg;
uint32_t delay = 0;
if (nbytes < 0) {
hub_class->int_buffer[0] = 0;
delay = 100;
}
if (hub_class->parent->connected) {
usb_workqueue_submit(&g_lpworkq, &hub_class->work, usbh_extern_hub_psc_event, (void *)hub_class, delay);
}
}
const struct usbh_class_driver hub_class_driver = {
.driver_name = "hub",
.connect = usbh_hub_connect,
.disconnect = usbh_hub_disconnect
/**
* @file usbh_hub.c
*
* 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.
*
*/
#include "usbh_core.h"
#include "usbh_hub.h"
#define DEV_FORMAT "/dev/hub%d"
#define DEV_NAMELEN 16
static uint32_t g_devinuse = 0;
usb_slist_t hub_class_head = USB_SLIST_OBJECT_INIT(hub_class_head);
USB_NOCACHE_RAM_SECTION uint8_t int_buffer[6][USBH_HUB_INTIN_BUFSIZE];
extern void usbh_external_hport_connect(struct usbh_hubport *hport);
extern void usbh_external_hport_disconnect(struct usbh_hubport *hport);
extern void usbh_hport_activate(struct usbh_hubport *hport);
extern void usbh_hport_deactivate(struct usbh_hubport *hport);
static void usbh_external_hub_callback(void *arg, int nbytes);
static inline void usbh_hub_register(struct usbh_hub *hub)
{
usb_slist_add_tail(&hub_class_head, &hub->list);
}
static inline void usbh_hub_unregister(struct usbh_hub *hub)
{
usb_slist_remove(&hub_class_head, &hub->list);
}
/****************************************************************************
* Name: usbh_hub_devno_alloc
*
* Description:
* Allocate a unique /dev/hub[n] minor number in the range 2-31.
*
****************************************************************************/
static int usbh_hub_devno_alloc(struct usbh_hub *hub)
{
uint32_t flags;
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 2; devno < 32; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
hub->index = devno;
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_hub_devno_free
*
* Description:
* Free a /dev/hub[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_hub_devno_free(struct usbh_hub *hub)
{
int devno = hub->index;
if (devno >= 2 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
/****************************************************************************
* Name: usbh_hub_mkdevname
*
* Description:
* Format a /dev/hub[n] device name given a minor number.
*
****************************************************************************/
static inline void usbh_hub_mkdevname(struct usbh_hub *hub, char *devname)
{
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, hub->index);
}
int usbh_hub_get_hub_descriptor(struct usbh_hub *hub, uint8_t *buffer)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
setup->wValue = HUB_DESCRIPTOR_TYPE_HUB << 8;
setup->wIndex = 0;
setup->wLength = USB_SIZEOF_HUB_DESC;
return usbh_control_transfer(hub->parent->ep0, setup, buffer);
}
int usbh_hub_get_status(struct usbh_hub *hub, uint8_t *buffer)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0;
setup->wIndex = 0;
setup->wLength = 2;
return usbh_control_transfer(hub->parent->ep0, setup, buffer);
}
int usbh_hub_get_portstatus(struct usbh_hub *hub, uint8_t port, struct hub_port_status *port_status)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0;
setup->wIndex = port;
setup->wLength = 4;
return usbh_control_transfer(hub->parent->ep0, setup, (uint8_t *)port_status);
}
int usbh_hub_set_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_SET_FEATURE;
setup->wValue = feature;
setup->wIndex = port;
setup->wLength = 0;
return usbh_control_transfer(hub->parent->ep0, setup, NULL);
}
int usbh_hub_clear_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
{
struct usb_setup_packet *setup;
setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_CLEAR_FEATURE;
setup->wValue = feature;
setup->wIndex = port;
setup->wLength = 0;
return usbh_control_transfer(hub->parent->ep0, setup, NULL);
}
static int parse_hub_descriptor(struct usb_hub_descriptor *desc, uint16_t length)
{
if (desc->bLength != USB_SIZEOF_HUB_DESC) {
USB_LOG_ERR("invalid device bLength 0x%02x\r\n", desc->bLength);
return -1;
} else if (desc->bDescriptorType != HUB_DESCRIPTOR_TYPE_HUB) {
USB_LOG_ERR("unexpected descriptor 0x%02x\r\n", desc->bDescriptorType);
return -2;
} else {
USB_LOG_INFO("Device Descriptor:\r\n");
USB_LOG_INFO("bLength: 0x%02x \r\n", desc->bLength);
USB_LOG_INFO("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType);
USB_LOG_INFO("bNbrPorts: 0x%02x \r\n", desc->bNbrPorts);
USB_LOG_INFO("wHubCharacteristics: 0x%04x \r\n", desc->wHubCharacteristics);
USB_LOG_INFO("bPwrOn2PwrGood: 0x%02x \r\n", desc->bPwrOn2PwrGood);
USB_LOG_INFO("bHubContrCurrent: 0x%02x \r\n", desc->bHubContrCurrent);
USB_LOG_INFO("DeviceRemovable: 0x%02x \r\n", desc->DeviceRemovable);
USB_LOG_INFO("PortPwrCtrlMask: 0x%02x \r\n", desc->PortPwrCtrlMask);
}
return 0;
}
int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN];
int ret;
uint8_t *hub_desc_buffer;
struct usbh_hub *hub_class;
hub_class = usb_malloc(sizeof(struct usbh_hub));
if (hub_class == NULL) {
USB_LOG_ERR("Fail to alloc hub_class\r\n");
return -ENOMEM;
}
memset(hub_class, 0, sizeof(struct usbh_hub));
hub_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (hub_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM;
}
hub_class->port_status = usb_iomalloc(sizeof(struct hub_port_status));
if (hub_class->port_status == NULL) {
USB_LOG_ERR("Fail to alloc port_status\r\n");
return -ENOMEM;
}
hub_desc_buffer = usb_iomalloc(32);
usbh_hub_devno_alloc(hub_class);
usbh_hub_mkdevname(hub_class, devname);
hub_class->dev_addr = hport->dev_addr;
hub_class->parent = hport;
hport->config.intf[0].priv = hub_class;
ret = usbh_hub_get_hub_descriptor(hub_class, hub_desc_buffer);
if (ret != 0) {
usb_iofree(hub_desc_buffer);
return ret;
}
parse_hub_descriptor((struct usb_hub_descriptor *)hub_desc_buffer, USB_SIZEOF_HUB_DESC);
memcpy(&hub_class->hub_desc, hub_desc_buffer, USB_SIZEOF_HUB_DESC);
usb_iofree(hub_desc_buffer);
hub_class->nports = hub_class->hub_desc.bNbrPorts;
for (uint8_t port = 1; port <= hub_class->nports; port++) {
hub_class->child[port - 1].port = port;
hub_class->child[port - 1].parent = hub_class;
}
hub_class->int_buffer = int_buffer[hub_class->index - 2];
usbh_hub_register(hub_class);
ep_desc = &hport->config.intf[intf].ep[0].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&hub_class->intin, &ep_cfg);
} else {
return -1;
}
for (uint8_t port = 1; port <= hub_class->nports; port++) {
ret = usbh_hub_set_feature(hub_class, 1, HUB_PORT_FEATURE_POWER);
if (ret < 0) {
return ret;
}
}
for (uint8_t port = 1; port <= hub_class->nports; port++) {
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
USB_LOG_INFO("Port:%d, status:0x%02x, change:0x%02x\r\n", port, hub_class->port_status->wPortStatus, hub_class->port_status->wPortChange);
if (ret < 0) {
return ret;
}
}
USB_LOG_INFO("Register HUB Class:%s\r\n", devname);
ret = usbh_ep_intr_async_transfer(hub_class->intin, hub_class->int_buffer, USBH_HUB_INTIN_BUFSIZE, usbh_external_hub_callback, hub_class);
return 0;
}
int usbh_hub_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_hubport *child;
char devname[DEV_NAMELEN];
int ret = 0;
struct usbh_hub *hub_class = (struct usbh_hub *)hport->config.intf[intf].priv;
if (hub_class) {
usbh_hub_devno_free(hub_class);
usbh_hub_mkdevname(hub_class, devname);
if (hub_class->intin) {
ret = usb_ep_cancel(hub_class->intin);
if (ret < 0) {
}
usbh_ep_free(hub_class->intin);
}
if (hub_class->setup)
usb_iofree(hub_class->setup);
if (hub_class->port_status)
usb_iofree(hub_class->port_status);
for (uint8_t port = 1; port <= hub_class->nports; port++) {
child = &hub_class->child[port - 1];
usbh_hport_deactivate(child);
for (uint8_t i = 0; i < child->config.config_desc.bNumInterfaces; i++) {
if (child->config.intf[i].class_driver && child->config.intf[i].class_driver->disconnect) {
ret = CLASS_DISCONNECT(child, i);
}
}
child->config.config_desc.bNumInterfaces = 0;
child->parent = NULL;
}
usbh_hub_unregister(hub_class);
usb_free(hub_class);
hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister HUB Class:%s\r\n", devname);
}
return ret;
}
static void usbh_extern_hub_psc_event(void *arg)
{
struct usbh_hub *hub_class;
struct usbh_hubport *connport;
uint8_t port_change;
uint16_t status;
uint16_t change;
uint16_t mask;
uint16_t feat;
uint32_t flags;
int ret;
hub_class = (struct usbh_hub *)arg;
/* Has the hub been disconnected? */
if (!hub_class->parent->connected) {
return;
}
port_change = hub_class->int_buffer[0];
USB_LOG_DBG("port_change:0x%02x\r\n", port_change);
/* Check for status change on any port */
for (uint8_t port = USBH_HUB_PORT_START_INDEX; port <= hub_class->nports; port++) {
/* Check if port status has changed */
if ((port_change & (1 << port)) == 0) {
continue;
}
USB_LOG_DBG("Port %d change\r\n", port);
/* Port status changed, check what happened */
port_change &= ~(1 << port);
/* Read hub port status */
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
continue;
}
status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange;
USB_LOG_DBG("Port:%d, status:0x%02x, change:0x%02x\r\n", port, status, change);
/* First, clear all change bits */
mask = 1;
feat = HUB_PORT_FEATURE_C_CONNECTION;
while (change) {
if (change & mask) {
ret = usbh_hub_clear_feature(hub_class, port, feat);
if (ret < 0) {
USB_LOG_ERR("Failed to clear port:%d, change mask:%04x, errorcode:%d\r\n", port, mask, ret);
}
change &= (~mask);
}
mask <<= 1;
feat++;
}
change = hub_class->port_status->wPortChange;
/* Handle connect or disconnect, no power management */
if (change & HUB_PORT_STATUS_C_CONNECTION) {
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
}
status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange;
if (status & HUB_PORT_STATUS_CONNECTION) {
/* Device connected to a port on the hub */
//USB_LOG_INFO("Connection on port:%d\n", port);
ret = usbh_hub_set_feature(hub_class, port, HUB_PORT_FEATURE_RESET);
if (ret < 0) {
USB_LOG_ERR("Failed to reset port:%d,errorcode:%d\r\n", port, ret);
continue;
}
usb_osal_msleep(100);
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
continue;
}
status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange;
USB_LOG_DBG("Port:%d, status:0x%02x, change:0x%02x after reset\r\n", port, status, change);
if ((status & HUB_PORT_STATUS_RESET) == 0 && (status & HUB_PORT_STATUS_ENABLE) != 0) {
if (change & HUB_PORT_STATUS_C_RESET) {
ret = usbh_hub_clear_feature(hub_class, port, HUB_PORT_FEATURE_C_RESET);
if (ret < 0) {
USB_LOG_ERR("Failed to clear port:%d reset change, errorcode: %d\r\n", port, ret);
}
}
connport = &hub_class->child[port - 1];
if (status & HUB_PORT_STATUS_HIGH_SPEED) {
connport->speed = USB_SPEED_HIGH;
} else if (status & HUB_PORT_STATUS_LOW_SPEED) {
connport->speed = USB_SPEED_LOW;
} else {
connport->speed = USB_SPEED_FULL;
}
/* Device connected from a port on the hub, wakeup psc thread. */
usbh_external_hport_connect(connport);
} else {
USB_LOG_ERR("Failed to enable port:%d\r\n", port);
continue;
}
} else {
/* Device disconnected from a port on the hub, wakeup psc thread. */
connport = &hub_class->child[port - 1];
usbh_external_hport_disconnect(connport);
}
} else {
USB_LOG_WRN("status %04x change %04x not handled\r\n", status, change);
}
}
/* Check for hub status change */
if ((port_change & 1) != 0) {
/* Hub status changed */
USB_LOG_WRN("Hub status changed, not handled\n");
}
flags = usb_osal_enter_critical_section();
if (hub_class->parent->connected) {
ret = usbh_ep_intr_async_transfer(hub_class->intin, hub_class->int_buffer, USBH_HUB_INTIN_BUFSIZE, usbh_external_hub_callback, hub_class);
}
usb_osal_leave_critical_section(flags);
}
static void usbh_external_hub_callback(void *arg, int nbytes)
{
struct usbh_hub *hub_class = (struct usbh_hub *)arg;
uint32_t delay = 0;
if (nbytes < 0) {
hub_class->int_buffer[0] = 0;
delay = 100;
}
if (hub_class->parent->connected) {
usb_workqueue_submit(&g_lpworkq, &hub_class->work, usbh_extern_hub_psc_event, (void *)hub_class, delay);
}
}
const struct usbh_class_driver hub_class_driver = {
.driver_name = "hub",
.connect = usbh_hub_connect,
.disconnect = usbh_hub_disconnect
};

View File

@@ -1,42 +1,42 @@
/**
* @file usbh_hub.h
*
* 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.
*
*/
#ifndef _USBH_HUB_H_
#define _USBH_HUB_H_
#include "usb_hub.h"
#define USBH_HUB_MAX_PORTS 4
/* Maximum size of an interrupt IN transfer */
#define USBH_HUB_INTIN_BUFSIZE ((USBH_HUB_MAX_PORTS + 8) >> 3)
extern const struct usbh_class_driver hub_class_driver;
extern usb_slist_t hub_class_head;
extern usb_osal_thread_t hub_thread;
#ifdef __cplusplus
extern "C" {
#endif
int usbh_hub_initialize(void);
#ifdef __cplusplus
}
#endif
#endif /* _USBH_HUB_H_ */
/**
* @file usbh_hub.h
*
* 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.
*
*/
#ifndef _USBH_HUB_H_
#define _USBH_HUB_H_
#include "usb_hub.h"
#define USBH_HUB_MAX_PORTS 4
/* Maximum size of an interrupt IN transfer */
#define USBH_HUB_INTIN_BUFSIZE ((USBH_HUB_MAX_PORTS + 8) >> 3)
extern const struct usbh_class_driver hub_class_driver;
extern usb_slist_t hub_class_head;
extern usb_osal_thread_t hub_thread;
#ifdef __cplusplus
extern "C" {
#endif
int usbh_hub_initialize(void);
#ifdef __cplusplus
}
#endif
#endif /* _USBH_HUB_H_ */

File diff suppressed because it is too large Load Diff

View File

@@ -1,438 +1,438 @@
/**
* @file usbh_msc.c
*
* 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.
*
*/
#include "usbh_core.h"
#include "usbh_msc.h"
#include "scsi.h"
#define DEV_FORMAT "/dev/sd%c"
#define DEV_NAMELEN 16
static uint32_t g_devinuse = 0;
/****************************************************************************
* Name: usbh_msc_devno_alloc
*
* Description:
* Allocate a unique /dev/ttyACM[n] minor number in the range 0-31.
*
****************************************************************************/
static int usbh_msc_devno_alloc(struct usbh_msc *priv)
{
uint32_t flags;
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 26; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
priv->sdchar = 'a' + devno;
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_msc_devno_free
*
* Description:
* Free a /dev/sd[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_msc_devno_free(struct usbh_msc *priv)
{
int devno = priv->sdchar - 'a';
if (devno >= 0 && devno < 26) {
uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
/****************************************************************************
* Name: usbh_msc_mkdevname
*
* Description:
* Format a /dev/sd[n] device name given a minor number.
*
****************************************************************************/
static inline void usbh_msc_mkdevname(struct usbh_msc *priv, char *devname)
{
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->sdchar);
}
static int usbh_msc_get_maxlun(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{
struct usb_setup_packet *setup;
struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv;
setup = msc_class->setup;
if (msc_class->intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = MSC_REQUEST_GET_MAX_LUN;
setup->wValue = 0;
setup->wIndex = intf;
setup->wLength = 1;
return usbh_control_transfer(hport->ep0, setup, buffer);
}
static void usbh_msc_cbw_dump(struct CBW *cbw)
{
int i;
USB_LOG_INFO("CBW:\r\n");
USB_LOG_INFO(" signature: 0x%08x\r\n", (unsigned int)cbw->dSignature);
USB_LOG_INFO(" tag: 0x%08x\r\n", (unsigned int)cbw->dTag);
USB_LOG_INFO(" datlen: 0x%08x\r\n", (unsigned int)cbw->dDataLength);
USB_LOG_INFO(" flags: 0x%02x\r\n", cbw->bmFlags);
USB_LOG_INFO(" lun: 0x%02x\r\n", cbw->bLUN);
USB_LOG_INFO(" cblen: 0x%02x\r\n", cbw->bCBLength);
USB_LOG_INFO("CB:\r\n");
for (i = 0; i < cbw->bCBLength; i += 8) {
USB_LOG_INFO(" 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n",
cbw->CB[i], cbw->CB[i + 1], cbw->CB[i + 2],
cbw->CB[i + 3], cbw->CB[i + 4], cbw->CB[i + 5],
cbw->CB[i + 6], cbw->CB[i + 7]);
}
}
static void usbh_msc_csw_dump(struct CSW *csw)
{
USB_LOG_INFO("CSW:\r\n");
USB_LOG_INFO(" signature: 0x%08x\r\n", (unsigned int)csw->dSignature);
USB_LOG_INFO(" tag: 0x%08x\r\n", (unsigned int)csw->dTag);
USB_LOG_INFO(" residue: 0x%08x\r\n", (unsigned int)csw->dDataResidue);
USB_LOG_INFO(" status: 0x%02x\r\n", csw->bStatus);
}
static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->bCBLength = SCSICMD_TESTUNITREADY_SIZEOF;
cbw->CB[0] = SCSI_CMD_TESTUNITREADY;
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->bmFlags = 0x80;
cbw->bCBLength = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
cbw->dDataLength = SCSICMD_REQUESTSENSE_SIZEOF;
cbw->CB[0] = SCSI_REQUEST_SENSE;
cbw->CB[4] = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_FIXEDSENSEDATA_SIZEOF);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = SCSIRESP_INQUIRY_SIZEOF;
cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_INQUIRY_SIZEOF;
cbw->CB[0] = SCSI_INQUIRY;
cbw->CB[4] = SCSIRESP_INQUIRY_SIZEOF;
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_INQUIRY_SIZEOF);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = SCSIRESP_READCAPACITY10_SIZEOF;
cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_READCAPACITY10_SIZEOF;
cbw->CB[0] = SCSI_READ_CAPACITY10;
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_READCAPACITY10_SIZEOF);
if (nbytes >= 0) {
/* Save the capacity information */
msc_class->blocknum = GET_BE32(&msc_class->tx_buffer[0]) + 1;
msc_class->blocksize = GET_BE32(&msc_class->tx_buffer[4]);
USB_LOG_INFO("capacity info:\r\n");
USB_LOG_INFO("block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
int usbh_msc_mem_write(struct usbh_msc *msc_class, uint32_t sector, const uint8_t *buffer, uint32_t nsectors)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = (msc_class->blocksize * nsectors);
cbw->bCBLength = SCSICMD_WRITE10_SIZEOF;
cbw->CB[0] = SCSI_WRITE10;
SET_BE24(&cbw->CB[2], sector);
SET_BE24(&cbw->CB[7], nsectors);
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Send the user data */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)buffer, msc_class->blocksize * nsectors);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
int usbh_msc_mem_read(struct usbh_msc *msc_class, uint32_t sector, const uint8_t *buffer, uint32_t nsectors)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = (msc_class->blocksize * nsectors);
cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_READ10_SIZEOF;
cbw->CB[0] = SCSI_READ10;
SET_BE24(&cbw->CB[2], sector);
SET_BE24(&cbw->CB[7], nsectors);
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the user data */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, (uint8_t *)buffer, msc_class->blocksize * nsectors);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN];
int ret;
struct usbh_msc *msc_class = usb_malloc(sizeof(struct usbh_msc));
if (msc_class == NULL) {
USB_LOG_ERR("Fail to alloc msc_class\r\n");
return -ENOMEM;
}
memset(msc_class, 0, sizeof(struct usbh_msc));
usbh_msc_devno_alloc(msc_class);
usbh_msc_mkdevname(msc_class, devname);
hport->config.intf[intf].priv = msc_class;
msc_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (msc_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM;
}
msc_class->tx_buffer = usb_iomalloc(128);
if (msc_class->tx_buffer == NULL) {
USB_LOG_ERR("Fail to alloc tx_buffer\r\n");
return -ENOMEM;
}
ret = usbh_msc_get_maxlun(hport, intf, msc_class->tx_buffer);
if (ret < 0) {
return ret;
}
USB_LOG_INFO("Get max LUN:%u\r\n", msc_class->tx_buffer[0]);
for (uint8_t i = 0; i < hport->config.intf[intf].intf_desc.bNumEndpoints; i++) {
ep_desc = &hport->config.intf[intf].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&msc_class->bulkin, &ep_cfg);
} else {
usbh_ep_alloc(&msc_class->bulkout, &ep_cfg);
}
}
USB_LOG_INFO("Register MSC Class:%s\r\n", devname);
ret = usbh_msc_scsi_testunitready(msc_class);
ret = usbh_msc_scsi_inquiry(msc_class);
ret = usbh_msc_scsi_readcapacity10(msc_class);
return ret;
}
int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
char devname[DEV_NAMELEN];
int ret = 0;
struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv;
if (msc_class) {
usbh_msc_devno_free(msc_class);
usbh_msc_mkdevname(msc_class, devname);
if (msc_class->bulkin) {
ret = usb_ep_cancel(msc_class->bulkin);
if (ret < 0) {
}
usbh_ep_free(msc_class->bulkin);
}
if (msc_class->bulkout) {
ret = usb_ep_cancel(msc_class->bulkout);
if (ret < 0) {
}
usbh_ep_free(msc_class->bulkout);
}
if (msc_class->setup)
usb_iofree(msc_class->setup);
if (msc_class->tx_buffer)
usb_iofree(msc_class->tx_buffer);
usb_free(msc_class);
hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister MSC Class:%s\r\n", devname);
}
return ret;
}
const struct usbh_class_driver msc_class_driver = {
.driver_name = "msc",
.connect = usbh_msc_connect,
.disconnect = usbh_msc_disconnect
/**
* @file usbh_msc.c
*
* 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.
*
*/
#include "usbh_core.h"
#include "usbh_msc.h"
#include "scsi.h"
#define DEV_FORMAT "/dev/sd%c"
#define DEV_NAMELEN 16
static uint32_t g_devinuse = 0;
/****************************************************************************
* Name: usbh_msc_devno_alloc
*
* Description:
* Allocate a unique /dev/ttyACM[n] minor number in the range 0-31.
*
****************************************************************************/
static int usbh_msc_devno_alloc(struct usbh_msc *priv)
{
uint32_t flags;
int devno;
flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 26; devno++) {
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
priv->sdchar = 'a' + devno;
usb_osal_leave_critical_section(flags);
return 0;
}
}
usb_osal_leave_critical_section(flags);
return -EMFILE;
}
/****************************************************************************
* Name: usbh_msc_devno_free
*
* Description:
* Free a /dev/sd[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_msc_devno_free(struct usbh_msc *priv)
{
int devno = priv->sdchar - 'a';
if (devno >= 0 && devno < 26) {
uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags);
}
}
/****************************************************************************
* Name: usbh_msc_mkdevname
*
* Description:
* Format a /dev/sd[n] device name given a minor number.
*
****************************************************************************/
static inline void usbh_msc_mkdevname(struct usbh_msc *priv, char *devname)
{
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->sdchar);
}
static int usbh_msc_get_maxlun(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{
struct usb_setup_packet *setup;
struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv;
setup = msc_class->setup;
if (msc_class->intf != intf) {
return -1;
}
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = MSC_REQUEST_GET_MAX_LUN;
setup->wValue = 0;
setup->wIndex = intf;
setup->wLength = 1;
return usbh_control_transfer(hport->ep0, setup, buffer);
}
static void usbh_msc_cbw_dump(struct CBW *cbw)
{
int i;
USB_LOG_INFO("CBW:\r\n");
USB_LOG_INFO(" signature: 0x%08x\r\n", (unsigned int)cbw->dSignature);
USB_LOG_INFO(" tag: 0x%08x\r\n", (unsigned int)cbw->dTag);
USB_LOG_INFO(" datlen: 0x%08x\r\n", (unsigned int)cbw->dDataLength);
USB_LOG_INFO(" flags: 0x%02x\r\n", cbw->bmFlags);
USB_LOG_INFO(" lun: 0x%02x\r\n", cbw->bLUN);
USB_LOG_INFO(" cblen: 0x%02x\r\n", cbw->bCBLength);
USB_LOG_INFO("CB:\r\n");
for (i = 0; i < cbw->bCBLength; i += 8) {
USB_LOG_INFO(" 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n",
cbw->CB[i], cbw->CB[i + 1], cbw->CB[i + 2],
cbw->CB[i + 3], cbw->CB[i + 4], cbw->CB[i + 5],
cbw->CB[i + 6], cbw->CB[i + 7]);
}
}
static void usbh_msc_csw_dump(struct CSW *csw)
{
USB_LOG_INFO("CSW:\r\n");
USB_LOG_INFO(" signature: 0x%08x\r\n", (unsigned int)csw->dSignature);
USB_LOG_INFO(" tag: 0x%08x\r\n", (unsigned int)csw->dTag);
USB_LOG_INFO(" residue: 0x%08x\r\n", (unsigned int)csw->dDataResidue);
USB_LOG_INFO(" status: 0x%02x\r\n", csw->bStatus);
}
static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->bCBLength = SCSICMD_TESTUNITREADY_SIZEOF;
cbw->CB[0] = SCSI_CMD_TESTUNITREADY;
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->bmFlags = 0x80;
cbw->bCBLength = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
cbw->dDataLength = SCSICMD_REQUESTSENSE_SIZEOF;
cbw->CB[0] = SCSI_REQUEST_SENSE;
cbw->CB[4] = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_FIXEDSENSEDATA_SIZEOF);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = SCSIRESP_INQUIRY_SIZEOF;
cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_INQUIRY_SIZEOF;
cbw->CB[0] = SCSI_INQUIRY;
cbw->CB[4] = SCSIRESP_INQUIRY_SIZEOF;
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_INQUIRY_SIZEOF);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = SCSIRESP_READCAPACITY10_SIZEOF;
cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_READCAPACITY10_SIZEOF;
cbw->CB[0] = SCSI_READ_CAPACITY10;
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_READCAPACITY10_SIZEOF);
if (nbytes >= 0) {
/* Save the capacity information */
msc_class->blocknum = GET_BE32(&msc_class->tx_buffer[0]) + 1;
msc_class->blocksize = GET_BE32(&msc_class->tx_buffer[4]);
USB_LOG_INFO("capacity info:\r\n");
USB_LOG_INFO("block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
int usbh_msc_mem_write(struct usbh_msc *msc_class, uint32_t sector, const uint8_t *buffer, uint32_t nsectors)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = (msc_class->blocksize * nsectors);
cbw->bCBLength = SCSICMD_WRITE10_SIZEOF;
cbw->CB[0] = SCSI_WRITE10;
SET_BE24(&cbw->CB[2], sector);
SET_BE24(&cbw->CB[7], nsectors);
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Send the user data */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)buffer, msc_class->blocksize * nsectors);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
int usbh_msc_mem_read(struct usbh_msc *msc_class, uint32_t sector, const uint8_t *buffer, uint32_t nsectors)
{
int nbytes;
struct CBW *cbw;
/* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = (msc_class->blocksize * nsectors);
cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_READ10_SIZEOF;
cbw->CB[0] = SCSI_READ10;
SET_BE24(&cbw->CB[2], sector);
SET_BE24(&cbw->CB[7], nsectors);
/* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) {
/* Receive the user data */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, (uint8_t *)buffer, msc_class->blocksize * nsectors);
if (nbytes >= 0) {
/* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
}
}
}
return nbytes < 0 ? (int)nbytes : 0;
}
int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf)
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN];
int ret;
struct usbh_msc *msc_class = usb_malloc(sizeof(struct usbh_msc));
if (msc_class == NULL) {
USB_LOG_ERR("Fail to alloc msc_class\r\n");
return -ENOMEM;
}
memset(msc_class, 0, sizeof(struct usbh_msc));
usbh_msc_devno_alloc(msc_class);
usbh_msc_mkdevname(msc_class, devname);
hport->config.intf[intf].priv = msc_class;
msc_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (msc_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM;
}
msc_class->tx_buffer = usb_iomalloc(128);
if (msc_class->tx_buffer == NULL) {
USB_LOG_ERR("Fail to alloc tx_buffer\r\n");
return -ENOMEM;
}
ret = usbh_msc_get_maxlun(hport, intf, msc_class->tx_buffer);
if (ret < 0) {
return ret;
}
USB_LOG_INFO("Get max LUN:%u\r\n", msc_class->tx_buffer[0]);
for (uint8_t i = 0; i < hport->config.intf[intf].intf_desc.bNumEndpoints; i++) {
ep_desc = &hport->config.intf[intf].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&msc_class->bulkin, &ep_cfg);
} else {
usbh_ep_alloc(&msc_class->bulkout, &ep_cfg);
}
}
USB_LOG_INFO("Register MSC Class:%s\r\n", devname);
ret = usbh_msc_scsi_testunitready(msc_class);
ret = usbh_msc_scsi_inquiry(msc_class);
ret = usbh_msc_scsi_readcapacity10(msc_class);
return ret;
}
int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
char devname[DEV_NAMELEN];
int ret = 0;
struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv;
if (msc_class) {
usbh_msc_devno_free(msc_class);
usbh_msc_mkdevname(msc_class, devname);
if (msc_class->bulkin) {
ret = usb_ep_cancel(msc_class->bulkin);
if (ret < 0) {
}
usbh_ep_free(msc_class->bulkin);
}
if (msc_class->bulkout) {
ret = usb_ep_cancel(msc_class->bulkout);
if (ret < 0) {
}
usbh_ep_free(msc_class->bulkout);
}
if (msc_class->setup)
usb_iofree(msc_class->setup);
if (msc_class->tx_buffer)
usb_iofree(msc_class->tx_buffer);
usb_free(msc_class);
hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister MSC Class:%s\r\n", devname);
}
return ret;
}
const struct usbh_class_driver msc_class_driver = {
.driver_name = "msc",
.connect = usbh_msc_connect,
.disconnect = usbh_msc_disconnect
};

View File

@@ -1,49 +1,49 @@
/**
* @file usbh_msc.h
*
* 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.
*
*/
#ifndef _USBH_MSC_H
#define _USBH_MSC_H
#include "usb_msc.h"
#include "usb_scsi.h"
struct usbh_msc {
struct usb_setup_packet *setup;
uint8_t intf; /* Data interface number */
uint8_t sdchar;
usbh_epinfo_t bulkin; /* Bulk IN endpoint */
usbh_epinfo_t bulkout; /* Bulk OUT endpoint */
uint8_t *tx_buffer;
uint32_t blocknum; /* Number of blocks on the USB mass storage device */
uint16_t blocksize; /* Block size of USB mass storage device */
};
extern const struct usbh_class_driver msc_class_driver;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
/**
* @file usbh_msc.h
*
* 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.
*
*/
#ifndef _USBH_MSC_H
#define _USBH_MSC_H
#include "usb_msc.h"
#include "usb_scsi.h"
struct usbh_msc {
struct usb_setup_packet *setup;
uint8_t intf; /* Data interface number */
uint8_t sdchar;
usbh_epinfo_t bulkin; /* Bulk IN endpoint */
usbh_epinfo_t bulkout; /* Bulk OUT endpoint */
uint8_t *tx_buffer;
uint32_t blocknum; /* Number of blocks on the USB mass storage device */
uint16_t blocksize; /* Block size of USB mass storage device */
};
extern const struct usbh_class_driver msc_class_driver;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,134 +1,134 @@
/**
* @file usbd_video.c
*
* 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.
*
*/
#include "usbd_core.h"
#include "usbd_video.h"
extern struct video_probe_and_commit_controls probe;
extern struct video_probe_and_commit_controls commit;
int video_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("Video Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
switch (setup->bRequest) {
case VIDEO_REQUEST_SET_CUR:
if (setup->wValue == 256) {
memcpy((uint8_t *)&probe, *data, setup->wLength);
} else if (setup->wValue == 512) {
memcpy((uint8_t *)&commit, *data, setup->wLength);
}
break;
case VIDEO_REQUEST_GET_CUR:
if (setup->wValue == 256) {
*data = (uint8_t *)&probe;
} else if (setup->wValue == 512) {
*data = (uint8_t *)&commit;
}
break;
case VIDEO_REQUEST_GET_MIN:
if (setup->wValue == 256) {
*data = (uint8_t *)&probe;
} else if (setup->wValue == 512) {
*data = (uint8_t *)&commit;
}
break;
case VIDEO_REQUEST_GET_MAX:
if (setup->wValue == 256) {
*data = (uint8_t *)&probe;
} else if (setup->wValue == 512) {
*data = (uint8_t *)&commit;
}
break;
case VIDEO_REQUEST_GET_RES:
break;
case VIDEO_REQUEST_GET_LEN:
break;
case VIDEO_REQUEST_GET_INFO:
break;
case VIDEO_REQUEST_GET_DEF:
if (setup->wLength == 256) {
*data = (uint8_t *)&probe;
} else if (setup->wLength == 512) {
*data = (uint8_t *)&commit;
}
break;
default:
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
void video_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
break;
case USBD_EVENT_SOF:
usbd_video_sof_callback();
break;
case USBD_EVENT_SET_INTERFACE:
usbd_video_set_interface_callback(((uint8_t *)arg)[3]);
break;
default:
break;
}
}
void usbd_video_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static usbd_class_t *last_class = NULL;
if (last_class != devclass) {
last_class = devclass;
usbd_class_register(devclass);
}
intf->class_handler = video_class_request_handler;
intf->custom_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = video_notify_handler;
usbd_class_add_interface(devclass, intf);
/**
* @file usbd_video.c
*
* 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.
*
*/
#include "usbd_core.h"
#include "usbd_video.h"
extern struct video_probe_and_commit_controls probe;
extern struct video_probe_and_commit_controls commit;
int video_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("Video Class request: "
"bRequest 0x%02x\r\n",
setup->bRequest);
switch (setup->bRequest) {
case VIDEO_REQUEST_SET_CUR:
if (setup->wValue == 256) {
memcpy((uint8_t *)&probe, *data, setup->wLength);
} else if (setup->wValue == 512) {
memcpy((uint8_t *)&commit, *data, setup->wLength);
}
break;
case VIDEO_REQUEST_GET_CUR:
if (setup->wValue == 256) {
*data = (uint8_t *)&probe;
} else if (setup->wValue == 512) {
*data = (uint8_t *)&commit;
}
break;
case VIDEO_REQUEST_GET_MIN:
if (setup->wValue == 256) {
*data = (uint8_t *)&probe;
} else if (setup->wValue == 512) {
*data = (uint8_t *)&commit;
}
break;
case VIDEO_REQUEST_GET_MAX:
if (setup->wValue == 256) {
*data = (uint8_t *)&probe;
} else if (setup->wValue == 512) {
*data = (uint8_t *)&commit;
}
break;
case VIDEO_REQUEST_GET_RES:
break;
case VIDEO_REQUEST_GET_LEN:
break;
case VIDEO_REQUEST_GET_INFO:
break;
case VIDEO_REQUEST_GET_DEF:
if (setup->wLength == 256) {
*data = (uint8_t *)&probe;
} else if (setup->wLength == 512) {
*data = (uint8_t *)&commit;
}
break;
default:
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
void video_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
break;
case USBD_EVENT_SOF:
usbd_video_sof_callback();
break;
case USBD_EVENT_SET_INTERFACE:
usbd_video_set_interface_callback(((uint8_t *)arg)[3]);
break;
default:
break;
}
}
void usbd_video_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static usbd_class_t *last_class = NULL;
if (last_class != devclass) {
last_class = devclass;
usbd_class_register(devclass);
}
intf->class_handler = video_class_request_handler;
intf->custom_handler = NULL;
intf->vendor_handler = NULL;
intf->notify_handler = video_notify_handler;
usbd_class_add_interface(devclass, intf);
}