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

View File

@@ -1,171 +1,171 @@
/** /**
* @file usbd_cdc.c * @file usbd_cdc.c
* @brief * @brief
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#include "usbd_core.h" #include "usbd_core.h"
#include "usbd_cdc.h" #include "usbd_cdc.h"
const char *stop_name[] = { "1", "1.5", "2" }; const char *stop_name[] = { "1", "1.5", "2" };
const char *parity_name[] = { "N", "O", "E", "M", "S" }; const char *parity_name[] = { "N", "O", "E", "M", "S" };
/* Device data structure */ /* Device data structure */
struct cdc_acm_cfg_private { struct cdc_acm_cfg_private {
/* CDC ACM line coding properties. LE order */ /* CDC ACM line coding properties. LE order */
struct cdc_line_coding line_coding; struct cdc_line_coding line_coding;
/* CDC ACM line state bitmap, DTE side */ /* CDC ACM line state bitmap, DTE side */
uint8_t line_state; uint8_t line_state;
/* CDC ACM serial state bitmap, DCE side */ /* CDC ACM serial state bitmap, DCE side */
uint8_t serial_state; uint8_t serial_state;
/* CDC ACM notification sent status */ /* CDC ACM notification sent status */
uint8_t notification_sent; uint8_t notification_sent;
/* CDC ACM configured flag */ /* CDC ACM configured flag */
bool configured; bool configured;
/* CDC ACM suspended flag */ /* CDC ACM suspended flag */
bool suspended; bool suspended;
uint32_t uart_first_init_flag; uint32_t uart_first_init_flag;
} usbd_cdc_acm_cfg; } usbd_cdc_acm_cfg;
static void usbd_cdc_acm_reset(void) static void usbd_cdc_acm_reset(void)
{ {
usbd_cdc_acm_cfg.line_coding.dwDTERate = 2000000; usbd_cdc_acm_cfg.line_coding.dwDTERate = 2000000;
usbd_cdc_acm_cfg.line_coding.bDataBits = 8; usbd_cdc_acm_cfg.line_coding.bDataBits = 8;
usbd_cdc_acm_cfg.line_coding.bParityType = 0; usbd_cdc_acm_cfg.line_coding.bParityType = 0;
usbd_cdc_acm_cfg.line_coding.bCharFormat = 0; usbd_cdc_acm_cfg.line_coding.bCharFormat = 0;
usbd_cdc_acm_cfg.configured = false; usbd_cdc_acm_cfg.configured = false;
usbd_cdc_acm_cfg.uart_first_init_flag = 0; usbd_cdc_acm_cfg.uart_first_init_flag = 0;
} }
/** /**
* @brief Handler called for Class requests not handled by the USB stack. * @brief Handler called for Class requests not handled by the USB stack.
* *
* @param setup Information about the request to execute. * @param setup Information about the request to execute.
* @param len Size of the buffer. * @param len Size of the buffer.
* @param data Buffer containing the request result. * @param data Buffer containing the request result.
* *
* @return 0 on success, negative errno code on fail. * @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) static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{ {
USB_LOG_DBG("CDC Class request: " USB_LOG_DBG("CDC Class request: "
"bRequest 0x%02x\r\n", "bRequest 0x%02x\r\n",
setup->bRequest); setup->bRequest);
switch (setup->bRequest) { switch (setup->bRequest) {
case CDC_REQUEST_SET_LINE_CODING: case CDC_REQUEST_SET_LINE_CODING:
/*******************************************************************************/ /*******************************************************************************/
/* Line Coding Structure */ /* Line Coding Structure */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Offset | Field | Size | Value | Description */ /* Offset | Field | Size | Value | Description */
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
/* 4 | bCharFormat | 1 | Number | Stop bits */ /* 4 | bCharFormat | 1 | Number | Stop bits */
/* 0 - 1 Stop bit */ /* 0 - 1 Stop bit */
/* 1 - 1.5 Stop bits */ /* 1 - 1.5 Stop bits */
/* 2 - 2 Stop bits */ /* 2 - 2 Stop bits */
/* 5 | bParityType | 1 | Number | Parity */ /* 5 | bParityType | 1 | Number | Parity */
/* 0 - None */ /* 0 - None */
/* 1 - Odd */ /* 1 - Odd */
/* 2 - Even */ /* 2 - Even */
/* 3 - Mark */ /* 3 - Mark */
/* 4 - Space */ /* 4 - Space */
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/ /*******************************************************************************/
if (usbd_cdc_acm_cfg.uart_first_init_flag == 0) { if (usbd_cdc_acm_cfg.uart_first_init_flag == 0) {
usbd_cdc_acm_cfg.uart_first_init_flag = 1; usbd_cdc_acm_cfg.uart_first_init_flag = 1;
return 0; return 0;
} }
memcpy(&usbd_cdc_acm_cfg.line_coding, *data, sizeof(usbd_cdc_acm_cfg.line_coding)); 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", 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.dwDTERate,
usbd_cdc_acm_cfg.line_coding.bDataBits, usbd_cdc_acm_cfg.line_coding.bDataBits,
parity_name[usbd_cdc_acm_cfg.line_coding.bParityType], parity_name[usbd_cdc_acm_cfg.line_coding.bParityType],
stop_name[usbd_cdc_acm_cfg.line_coding.bCharFormat]); 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_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); usbd_cdc_acm_cfg.line_coding.bParityType, usbd_cdc_acm_cfg.line_coding.bCharFormat);
break; break;
case CDC_REQUEST_SET_CONTROL_LINE_STATE: case CDC_REQUEST_SET_CONTROL_LINE_STATE:
usbd_cdc_acm_cfg.line_state = (uint8_t)setup->wValue; usbd_cdc_acm_cfg.line_state = (uint8_t)setup->wValue;
bool dtr = (setup->wValue & 0x01); bool dtr = (setup->wValue & 0x01);
bool rts = (setup->wValue & 0x02); bool rts = (setup->wValue & 0x02);
USB_LOG_DBG("DTR 0x%x,RTS 0x%x\r\n", USB_LOG_DBG("DTR 0x%x,RTS 0x%x\r\n",
dtr, rts); dtr, rts);
usbd_cdc_acm_set_dtr(dtr); usbd_cdc_acm_set_dtr(dtr);
usbd_cdc_acm_set_rts(rts); usbd_cdc_acm_set_rts(rts);
break; break;
case CDC_REQUEST_GET_LINE_CODING: case CDC_REQUEST_GET_LINE_CODING:
*data = (uint8_t *)(&usbd_cdc_acm_cfg.line_coding); *data = (uint8_t *)(&usbd_cdc_acm_cfg.line_coding);
*len = sizeof(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", 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.dwDTERate,
usbd_cdc_acm_cfg.line_coding.bCharFormat, usbd_cdc_acm_cfg.line_coding.bCharFormat,
usbd_cdc_acm_cfg.line_coding.bParityType, usbd_cdc_acm_cfg.line_coding.bParityType,
usbd_cdc_acm_cfg.line_coding.bDataBits); usbd_cdc_acm_cfg.line_coding.bDataBits);
break; break;
default: default:
USB_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest); USB_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest);
return -1; return -1;
} }
return 0; return 0;
} }
static void cdc_notify_handler(uint8_t event, void *arg) static void cdc_notify_handler(uint8_t event, void *arg)
{ {
switch (event) { switch (event) {
case USBD_EVENT_RESET: case USBD_EVENT_RESET:
usbd_cdc_acm_reset(); usbd_cdc_acm_reset();
break; break;
default: default:
break; 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_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_dtr(bool dtr)
{ {
} }
__WEAK void usbd_cdc_acm_set_rts(bool rts) __WEAK void usbd_cdc_acm_set_rts(bool rts)
{ {
} }
void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf) void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{ {
static usbd_class_t *last_class = NULL; static usbd_class_t *last_class = NULL;
if (last_class != devclass) { if (last_class != devclass) {
last_class = devclass; last_class = devclass;
usbd_class_register(devclass); usbd_class_register(devclass);
} }
intf->class_handler = cdc_acm_class_request_handler; intf->class_handler = cdc_acm_class_request_handler;
intf->custom_handler = NULL; intf->custom_handler = NULL;
intf->vendor_handler = NULL; intf->vendor_handler = NULL;
intf->notify_handler = cdc_notify_handler; intf->notify_handler = cdc_notify_handler;
usbd_class_add_interface(devclass, intf); usbd_class_add_interface(devclass, intf);
} }

View File

@@ -1,336 +1,336 @@
/** /**
* @file usbh_cdc_acm.c * @file usbh_cdc_acm.c
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#include "usbh_core.h" #include "usbh_core.h"
#include "usbh_cdc_acm.h" #include "usbh_cdc_acm.h"
#define DEV_FORMAT "/dev/ttyACM%d" #define DEV_FORMAT "/dev/ttyACM%d"
#define DEV_NAMELEN 16 #define DEV_NAMELEN 16
static uint32_t g_devinuse = 0; static uint32_t g_devinuse = 0;
void usbh_cdc_acm_callback(void *arg, int nbytes); void usbh_cdc_acm_callback(void *arg, int nbytes);
/**************************************************************************** /****************************************************************************
* Name: usbh_cdc_acm_devno_alloc * Name: usbh_cdc_acm_devno_alloc
* *
* Description: * Description:
* Allocate a unique /dev/ttyACM[n] minor number in the range 0-31. * 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) static int usbh_cdc_acm_devno_alloc(struct usbh_cdc_acm *priv)
{ {
uint32_t flags; uint32_t flags;
int devno; int devno;
flags = usb_osal_enter_critical_section(); flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 32; devno++) { for (devno = 0; devno < 32; devno++) {
uint32_t bitno = 1 << devno; uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) { if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno; g_devinuse |= bitno;
priv->minor = devno; priv->minor = devno;
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
return 0; return 0;
} }
} }
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
return -EMFILE; return -EMFILE;
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_cdc_acm_devno_free * Name: usbh_cdc_acm_devno_free
* *
* Description: * Description:
* Free a /dev/ttyACM[n] minor number so that it can be used. * 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) static void usbh_cdc_acm_devno_free(struct usbh_cdc_acm *priv)
{ {
int devno = priv->minor; int devno = priv->minor;
if (devno >= 0 && devno < 32) { if (devno >= 0 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section(); uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno); g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
} }
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_cdc_acm_mkdevname * Name: usbh_cdc_acm_mkdevname
* *
* Description: * Description:
* Format a /dev/ttyACM[n] device name given a minor number. * 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) static inline void usbh_cdc_acm_mkdevname(struct usbh_cdc_acm *priv, char *devname)
{ {
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor); 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 usbh_cdc_acm_set_line_coding(struct usbh_hubport *hport, uint8_t intf, struct cdc_line_coding *line_coding)
{ {
int ret; int ret;
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv; struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup; setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) { if (cdc_acm_class->ctrl_intf != intf) {
return -1; return -1;
} }
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE; setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_SET_LINE_CODING; setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
setup->wValue = 0; setup->wValue = 0;
setup->wIndex = intf; setup->wIndex = intf;
setup->wLength = 7; setup->wLength = 7;
ret = usbh_control_transfer(hport->ep0, setup, (uint8_t *)line_coding); ret = usbh_control_transfer(hport->ep0, setup, (uint8_t *)line_coding);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding)); memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding));
return 0; return 0;
} }
int usbh_cdc_acm_get_line_coding(struct usbh_hubport *hport, uint8_t intf, struct cdc_line_coding *line_coding) int usbh_cdc_acm_get_line_coding(struct usbh_hubport *hport, uint8_t intf, struct cdc_line_coding *line_coding)
{ {
int ret; int ret;
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv; struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup; setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) { if (cdc_acm_class->ctrl_intf != intf) {
return -1; return -1;
} }
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE; setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_GET_LINE_CODING; setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
setup->wValue = 0; setup->wValue = 0;
setup->wIndex = intf; setup->wIndex = intf;
setup->wLength = 7; setup->wLength = 7;
ret = usbh_control_transfer(hport->ep0, setup, (uint8_t *)line_coding); ret = usbh_control_transfer(hport->ep0, setup, (uint8_t *)line_coding);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding)); memcpy(cdc_acm_class->linecoding, line_coding, sizeof(struct cdc_line_coding));
return 0; return 0;
} }
int usbh_cdc_acm_set_line_state(struct usbh_hubport *hport, uint8_t intf, bool dtr, bool rts) int usbh_cdc_acm_set_line_state(struct usbh_hubport *hport, uint8_t intf, bool dtr, bool rts)
{ {
int ret; int ret;
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv; struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
setup = cdc_acm_class->setup; setup = cdc_acm_class->setup;
if (cdc_acm_class->ctrl_intf != intf) { if (cdc_acm_class->ctrl_intf != intf) {
return -1; return -1;
} }
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE; setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE; setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;
setup->wValue = (dtr << 0) | (rts << 1); setup->wValue = (dtr << 0) | (rts << 1);
setup->wIndex = intf; setup->wIndex = intf;
setup->wLength = 0; setup->wLength = 0;
ret = usbh_control_transfer(hport->ep0, setup, NULL); ret = usbh_control_transfer(hport->ep0, setup, NULL);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
cdc_acm_class->dtr = dtr; cdc_acm_class->dtr = dtr;
cdc_acm_class->rts = rts; cdc_acm_class->rts = rts;
return 0; return 0;
} }
USB_NOCACHE_RAM_SECTION uint8_t cdc_buffer[4096]; USB_NOCACHE_RAM_SECTION uint8_t cdc_buffer[4096];
int usbh_cdc_acm_connect(struct usbh_hubport *hport, uint8_t intf) int usbh_cdc_acm_connect(struct usbh_hubport *hport, uint8_t intf)
{ {
struct usbh_endpoint_cfg ep_cfg = { 0 }; struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc; struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN]; char devname[DEV_NAMELEN];
int ret; int ret;
struct usbh_cdc_acm *cdc_acm_class = usb_malloc(sizeof(struct usbh_cdc_acm)); struct usbh_cdc_acm *cdc_acm_class = usb_malloc(sizeof(struct usbh_cdc_acm));
if (cdc_acm_class == NULL) { if (cdc_acm_class == NULL) {
USB_LOG_ERR("Fail to alloc cdc_acm_class\r\n"); USB_LOG_ERR("Fail to alloc cdc_acm_class\r\n");
return -ENOMEM; return -ENOMEM;
} }
memset(cdc_acm_class, 0, sizeof(struct usbh_cdc_acm)); memset(cdc_acm_class, 0, sizeof(struct usbh_cdc_acm));
usbh_cdc_acm_devno_alloc(cdc_acm_class); usbh_cdc_acm_devno_alloc(cdc_acm_class);
usbh_cdc_acm_mkdevname(cdc_acm_class, devname); usbh_cdc_acm_mkdevname(cdc_acm_class, devname);
hport->config.intf[intf].priv = cdc_acm_class; hport->config.intf[intf].priv = cdc_acm_class;
hport->config.intf[intf + 1].priv = cdc_acm_class; hport->config.intf[intf + 1].priv = cdc_acm_class;
cdc_acm_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet)); cdc_acm_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (cdc_acm_class->setup == NULL) { if (cdc_acm_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n"); USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM; return -ENOMEM;
} }
cdc_acm_class->linecoding = usb_iomalloc(sizeof(struct cdc_line_coding)); cdc_acm_class->linecoding = usb_iomalloc(sizeof(struct cdc_line_coding));
if (cdc_acm_class->linecoding == NULL) { if (cdc_acm_class->linecoding == NULL) {
USB_LOG_ERR("Fail to alloc linecoding\r\n"); USB_LOG_ERR("Fail to alloc linecoding\r\n");
return -ENOMEM; return -ENOMEM;
} }
cdc_acm_class->ctrl_intf = intf; cdc_acm_class->ctrl_intf = intf;
cdc_acm_class->data_intf = intf + 1; cdc_acm_class->data_intf = intf + 1;
cdc_acm_class->linecoding->dwDTERate = 115200; cdc_acm_class->linecoding->dwDTERate = 115200;
cdc_acm_class->linecoding->bDataBits = 8; cdc_acm_class->linecoding->bDataBits = 8;
cdc_acm_class->linecoding->bParityType = 0; cdc_acm_class->linecoding->bParityType = 0;
cdc_acm_class->linecoding->bCharFormat = 0; cdc_acm_class->linecoding->bCharFormat = 0;
ret = usbh_cdc_acm_set_line_coding(hport, intf, cdc_acm_class->linecoding); ret = usbh_cdc_acm_set_line_coding(hport, intf, cdc_acm_class->linecoding);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = usbh_cdc_acm_set_line_state(hport, intf, true, true); ret = usbh_cdc_acm_set_line_state(hport, intf, true, true);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
#if 0 #if 0
ep_desc = &hport->config.intf[intf].ep[0].ep_desc; ep_desc = &hport->config.intf[intf].ep[0].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress; ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize; ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval; ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport; ep_cfg.hport = hport;
usbh_ep_alloc(&cdc_acm_class->intin, &ep_cfg); usbh_ep_alloc(&cdc_acm_class->intin, &ep_cfg);
#endif #endif
for (uint8_t i = 0; i < hport->config.intf[intf + 1].intf_desc.bNumEndpoints; i++) { 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_desc = &hport->config.intf[intf + 1].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress; ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize; ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval; ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport; ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) { if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&cdc_acm_class->bulkin, &ep_cfg); usbh_ep_alloc(&cdc_acm_class->bulkin, &ep_cfg);
} else { } else {
usbh_ep_alloc(&cdc_acm_class->bulkout, &ep_cfg); usbh_ep_alloc(&cdc_acm_class->bulkout, &ep_cfg);
} }
} }
USB_LOG_INFO("Register CDC ACM Class:%s\r\n", devname); USB_LOG_INFO("Register CDC ACM Class:%s\r\n", devname);
memset(cdc_buffer, 0, 512); memset(cdc_buffer, 0, 512);
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512); ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512);
if (ret < 0) { if (ret < 0) {
printf("bulk in error\r\n"); printf("bulk in error\r\n");
return ret; return ret;
} }
printf("recv over:%d\r\n", ret); printf("recv over:%d\r\n", ret);
for (size_t i = 0; i < ret; i++) { for (size_t i = 0; i < ret; i++) {
printf("0x%02x ", cdc_buffer[i]); printf("0x%02x ", cdc_buffer[i]);
} }
printf("\r\n"); printf("\r\n");
const uint8_t data1[10] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x14 }; const uint8_t data1[10] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x14 };
memcpy(cdc_buffer, data1, 8); memcpy(cdc_buffer, data1, 8);
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkout, cdc_buffer, 8); ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkout, cdc_buffer, 8);
if (ret < 0) { if (ret < 0) {
printf("bulk out error\r\n"); printf("bulk out error\r\n");
return ret; return ret;
} }
printf("send over:%d\r\n", ret); printf("send over:%d\r\n", ret);
#if 0 #if 0
usbh_ep_bulk_async_transfer(cdc_acm_class->bulkin, cdc_buffer, 512, usbh_cdc_acm_callback, NULL); usbh_ep_bulk_async_transfer(cdc_acm_class->bulkin, cdc_buffer, 512, usbh_cdc_acm_callback, NULL);
#else #else
ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512); ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512);
if (ret < 0) { if (ret < 0) {
printf("bulk in error\r\n"); printf("bulk in error\r\n");
return ret; return ret;
} }
printf("recv over:%d\r\n", ret); printf("recv over:%d\r\n", ret);
for (size_t i = 0; i < ret; i++) { for (size_t i = 0; i < ret; i++) {
printf("0x%02x ", cdc_buffer[i]); printf("0x%02x ", cdc_buffer[i]);
} }
printf("\r\n"); printf("\r\n");
#endif #endif
return ret; return ret;
} }
int usbh_cdc_acm_disconnect(struct usbh_hubport *hport, uint8_t intf) int usbh_cdc_acm_disconnect(struct usbh_hubport *hport, uint8_t intf)
{ {
char devname[DEV_NAMELEN]; char devname[DEV_NAMELEN];
int ret = 0; int ret = 0;
struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv; struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)hport->config.intf[intf].priv;
if (cdc_acm_class) { if (cdc_acm_class) {
usbh_cdc_acm_devno_free(cdc_acm_class); usbh_cdc_acm_devno_free(cdc_acm_class);
usbh_cdc_acm_mkdevname(cdc_acm_class, devname); usbh_cdc_acm_mkdevname(cdc_acm_class, devname);
if (cdc_acm_class->bulkin) { if (cdc_acm_class->bulkin) {
ret = usb_ep_cancel(cdc_acm_class->bulkin); ret = usb_ep_cancel(cdc_acm_class->bulkin);
if (ret < 0) { if (ret < 0) {
} }
usbh_ep_free(cdc_acm_class->bulkin); usbh_ep_free(cdc_acm_class->bulkin);
} }
if (cdc_acm_class->bulkout) { if (cdc_acm_class->bulkout) {
ret = usb_ep_cancel(cdc_acm_class->bulkout); ret = usb_ep_cancel(cdc_acm_class->bulkout);
if (ret < 0) { if (ret < 0) {
} }
usbh_ep_free(cdc_acm_class->bulkout); usbh_ep_free(cdc_acm_class->bulkout);
} }
if (cdc_acm_class->setup) if (cdc_acm_class->setup)
usb_iofree(cdc_acm_class->setup); usb_iofree(cdc_acm_class->setup);
if (cdc_acm_class->linecoding) if (cdc_acm_class->linecoding)
usb_iofree(cdc_acm_class->linecoding); usb_iofree(cdc_acm_class->linecoding);
usb_free(cdc_acm_class); usb_free(cdc_acm_class);
hport->config.intf[intf].priv = NULL; hport->config.intf[intf].priv = NULL;
hport->config.intf[intf + 1].priv = NULL; hport->config.intf[intf + 1].priv = NULL;
USB_LOG_INFO("Unregister CDC ACM Class:%s\r\n", devname); USB_LOG_INFO("Unregister CDC ACM Class:%s\r\n", devname);
} }
return ret; return ret;
} }
void usbh_cdc_acm_callback(void *arg, int result) void usbh_cdc_acm_callback(void *arg, int result)
{ {
printf("result:%d\r\n", result); printf("result:%d\r\n", result);
} }
const struct usbh_class_driver cdc_acm_class_driver = { const struct usbh_class_driver cdc_acm_class_driver = {
.driver_name = "cdc_acm", .driver_name = "cdc_acm",
.connect = usbh_cdc_acm_connect, .connect = usbh_cdc_acm_connect,
.disconnect = usbh_cdc_acm_disconnect .disconnect = usbh_cdc_acm_disconnect
}; };

View File

@@ -1,52 +1,52 @@
/** /**
* @file usbh_cdc_acm.h * @file usbh_cdc_acm.h
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#ifndef _USBH_CDC_ACM_H #ifndef _USBH_CDC_ACM_H
#define _USBH_CDC_ACM_H #define _USBH_CDC_ACM_H
#include "usb_cdc.h" #include "usb_cdc.h"
struct usbh_cdc_acm { struct usbh_cdc_acm {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct cdc_line_coding *linecoding; struct cdc_line_coding *linecoding;
uint8_t ctrl_intf; /* Control interface number */ uint8_t ctrl_intf; /* Control interface number */
uint8_t data_intf; /* Data interface number */ uint8_t data_intf; /* Data interface number */
bool dtr; bool dtr;
bool rts; bool rts;
uint8_t minor; uint8_t minor;
usbh_epinfo_t bulkin; /* Bulk IN endpoint */ usbh_epinfo_t bulkin; /* Bulk IN endpoint */
usbh_epinfo_t bulkout; /* Bulk OUT endpoint */ usbh_epinfo_t bulkout; /* Bulk OUT endpoint */
#ifdef HAVE_INTIN_ENDPOINT #ifdef HAVE_INTIN_ENDPOINT
usbh_epinfo_t intin; /* Interrupt IN endpoint (optional) */ usbh_epinfo_t intin; /* Interrupt IN endpoint (optional) */
#endif #endif
}; };
extern const struct usbh_class_driver cdc_acm_class_driver; extern const struct usbh_class_driver cdc_acm_class_driver;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@@ -1,286 +1,286 @@
/** /**
* @file usbd_hid.c * @file usbd_hid.c
* @brief * @brief
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#include "usbd_core.h" #include "usbd_core.h"
#include "usbd_hid.h" #include "usbd_hid.h"
#define HID_STATE_IDLE 0 #define HID_STATE_IDLE 0
#define HID_STATE_BUSY 1 #define HID_STATE_BUSY 1
struct usbd_hid_cfg_private { struct usbd_hid_cfg_private {
const uint8_t *hid_descriptor; const uint8_t *hid_descriptor;
const uint8_t *hid_report_descriptor; const uint8_t *hid_report_descriptor;
uint32_t hid_report_descriptor_len; uint32_t hid_report_descriptor_len;
uint8_t current_intf_num; uint8_t current_intf_num;
uint8_t hid_state; uint8_t hid_state;
uint8_t report; uint8_t report;
uint8_t idle_state; uint8_t idle_state;
uint8_t protocol; uint8_t protocol;
uint8_t (*get_report_callback)(uint8_t report_id, uint8_t report_type); 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); 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); uint8_t (*get_idle_callback)(uint8_t report_id);
void (*set_idle_callback)(uint8_t report_id, uint8_t duration); void (*set_idle_callback)(uint8_t report_id, uint8_t duration);
void (*set_protocol_callback)(uint8_t protocol); void (*set_protocol_callback)(uint8_t protocol);
uint8_t (*get_protocol_callback)(void); uint8_t (*get_protocol_callback)(void);
usb_slist_t list; usb_slist_t list;
} usbd_hid_cfg[4]; } usbd_hid_cfg[4];
static usb_slist_t usbd_hid_class_head = USB_SLIST_OBJECT_INIT(usbd_hid_class_head); static usb_slist_t usbd_hid_class_head = USB_SLIST_OBJECT_INIT(usbd_hid_class_head);
static void usbd_hid_reset(void) static void usbd_hid_reset(void)
{ {
usb_slist_t *i; usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head) 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); 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->hid_state = HID_STATE_IDLE;
hid_intf->report = 0; hid_intf->report = 0;
hid_intf->idle_state = 0; hid_intf->idle_state = 0;
hid_intf->protocol = 0; hid_intf->protocol = 0;
} }
} }
int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) int hid_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{ {
USB_LOG_DBG("HID Custom request: " USB_LOG_DBG("HID Custom request: "
"bRequest 0x%02x\r\n", "bRequest 0x%02x\r\n",
setup->bRequest); setup->bRequest);
if (((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_IN) && if (((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_IN) &&
setup->bRequest == USB_REQUEST_GET_DESCRIPTOR) { setup->bRequest == USB_REQUEST_GET_DESCRIPTOR) {
uint8_t value = (uint8_t)(setup->wValue >> 8); uint8_t value = (uint8_t)(setup->wValue >> 8);
uint8_t intf_num = (uint8_t)setup->wIndex; uint8_t intf_num = (uint8_t)setup->wIndex;
struct usbd_hid_cfg_private *current_hid_intf = NULL; struct usbd_hid_cfg_private *current_hid_intf = NULL;
usb_slist_t *i; usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head) 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); 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 (hid_intf->current_intf_num == intf_num) {
current_hid_intf = hid_intf; current_hid_intf = hid_intf;
break; break;
} }
} }
if (current_hid_intf == NULL) { if (current_hid_intf == NULL) {
return -2; return -2;
} }
switch (value) { switch (value) {
case HID_DESCRIPTOR_TYPE_HID: case HID_DESCRIPTOR_TYPE_HID:
USB_LOG_INFO("get HID Descriptor\r\n"); USB_LOG_INFO("get HID Descriptor\r\n");
*data = (uint8_t *)current_hid_intf->hid_descriptor; *data = (uint8_t *)current_hid_intf->hid_descriptor;
*len = current_hid_intf->hid_descriptor[0]; *len = current_hid_intf->hid_descriptor[0];
break; break;
case HID_DESCRIPTOR_TYPE_HID_REPORT: case HID_DESCRIPTOR_TYPE_HID_REPORT:
USB_LOG_INFO("get Report Descriptor\r\n"); USB_LOG_INFO("get Report Descriptor\r\n");
*data = (uint8_t *)current_hid_intf->hid_report_descriptor; *data = (uint8_t *)current_hid_intf->hid_report_descriptor;
*len = current_hid_intf->hid_report_descriptor_len; *len = current_hid_intf->hid_report_descriptor_len;
break; break;
case HID_DESCRIPTOR_TYPE_HID_PHYSICAL: case HID_DESCRIPTOR_TYPE_HID_PHYSICAL:
USB_LOG_INFO("get PHYSICAL Descriptor\r\n"); USB_LOG_INFO("get PHYSICAL Descriptor\r\n");
break; break;
default: default:
return -2; return -2;
} }
return 0; return 0;
} }
return -1; return -1;
} }
int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) int hid_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{ {
USB_LOG_DBG("HID Class request: " USB_LOG_DBG("HID Class request: "
"bRequest 0x%02x\r\n", "bRequest 0x%02x\r\n",
setup->bRequest); setup->bRequest);
struct usbd_hid_cfg_private *current_hid_intf = NULL; struct usbd_hid_cfg_private *current_hid_intf = NULL;
usb_slist_t *i; usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head) 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); 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; uint8_t intf_num = (uint8_t)setup->wIndex;
if (hid_intf->current_intf_num == intf_num) { if (hid_intf->current_intf_num == intf_num) {
current_hid_intf = hid_intf; current_hid_intf = hid_intf;
break; break;
} }
} }
if (current_hid_intf == NULL) { if (current_hid_intf == NULL) {
return -2; return -2;
} }
switch (setup->bRequest) { switch (setup->bRequest) {
case HID_REQUEST_GET_REPORT: case HID_REQUEST_GET_REPORT:
if (current_hid_intf->get_report_callback) 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*/ 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; *data = (uint8_t *)&current_hid_intf->report;
*len = 1; *len = 1;
break; break;
case HID_REQUEST_GET_IDLE: case HID_REQUEST_GET_IDLE:
if (current_hid_intf->get_idle_callback) if (current_hid_intf->get_idle_callback)
current_hid_intf->idle_state = current_hid_intf->get_idle_callback(LO_BYTE(setup->wValue)); current_hid_intf->idle_state = current_hid_intf->get_idle_callback(LO_BYTE(setup->wValue));
*data = (uint8_t *)&current_hid_intf->idle_state; *data = (uint8_t *)&current_hid_intf->idle_state;
*len = 1; *len = 1;
break; break;
case HID_REQUEST_GET_PROTOCOL: case HID_REQUEST_GET_PROTOCOL:
if (current_hid_intf->get_protocol_callback) if (current_hid_intf->get_protocol_callback)
current_hid_intf->protocol = current_hid_intf->get_protocol_callback(); current_hid_intf->protocol = current_hid_intf->get_protocol_callback();
*data = (uint8_t *)&current_hid_intf->protocol; *data = (uint8_t *)&current_hid_intf->protocol;
*len = 1; *len = 1;
break; break;
case HID_REQUEST_SET_REPORT: case HID_REQUEST_SET_REPORT:
if (current_hid_intf->set_report_callback) 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->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; current_hid_intf->report = **data;
break; break;
case HID_REQUEST_SET_IDLE: case HID_REQUEST_SET_IDLE:
if (current_hid_intf->set_idle_callback) 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->set_idle_callback(LO_BYTE(setup->wValue), HI_BYTE(setup->wIndex)); /*report id ,duration*/
current_hid_intf->idle_state = HI_BYTE(setup->wIndex); current_hid_intf->idle_state = HI_BYTE(setup->wIndex);
break; break;
case HID_REQUEST_SET_PROTOCOL: case HID_REQUEST_SET_PROTOCOL:
if (current_hid_intf->set_protocol_callback) if (current_hid_intf->set_protocol_callback)
current_hid_intf->set_protocol_callback(LO_BYTE(setup->wValue)); /*protocol*/ current_hid_intf->set_protocol_callback(LO_BYTE(setup->wValue)); /*protocol*/
current_hid_intf->protocol = LO_BYTE(setup->wValue); current_hid_intf->protocol = LO_BYTE(setup->wValue);
break; break;
default: default:
USB_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest); USB_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest);
return -1; return -1;
} }
return 0; return 0;
} }
static void hid_notify_handler(uint8_t event, void *arg) static void hid_notify_handler(uint8_t event, void *arg)
{ {
switch (event) { switch (event) {
case USBD_EVENT_RESET: case USBD_EVENT_RESET:
usbd_hid_reset(); usbd_hid_reset();
break; break;
default: default:
break; break;
} }
} }
void usbd_hid_reset_state(void) void usbd_hid_reset_state(void)
{ {
// usbd_hid_cfg.hid_state = HID_STATE_IDLE; // usbd_hid_cfg.hid_state = HID_STATE_IDLE;
} }
void usbd_hid_send_report(uint8_t ep, uint8_t *data, uint8_t len) void usbd_hid_send_report(uint8_t ep, uint8_t *data, uint8_t len)
{ {
// if(usbd_hid_cfg.hid_state == HID_STATE_IDLE) // if(usbd_hid_cfg.hid_state == HID_STATE_IDLE)
// { // {
// usbd_hid_cfg.hid_state = HID_STATE_BUSY; // usbd_hid_cfg.hid_state = HID_STATE_BUSY;
// usbd_ep_write(ep, data, len, NULL); // usbd_ep_write(ep, data, len, NULL);
// } // }
} }
void usbd_hid_descriptor_register(uint8_t intf_num, const uint8_t *desc) void usbd_hid_descriptor_register(uint8_t intf_num, const uint8_t *desc)
{ {
// usbd_hid_cfg.hid_descriptor = 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) void usbd_hid_report_descriptor_register(uint8_t intf_num, const uint8_t *desc, uint32_t desc_len)
{ {
usb_slist_t *i; usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head) 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); 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 (hid_intf->current_intf_num == intf_num) {
hid_intf->hid_report_descriptor = desc; hid_intf->hid_report_descriptor = desc;
hid_intf->hid_report_descriptor_len = desc_len; hid_intf->hid_report_descriptor_len = desc_len;
return; return;
} }
} }
} }
// clang-format off // clang-format off
void usbd_hid_set_request_callback( uint8_t intf_num, void usbd_hid_set_request_callback( uint8_t intf_num,
uint8_t (*get_report_callback)(uint8_t report_id, uint8_t report_type), 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), 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), uint8_t (*get_idle_callback)(uint8_t report_id),
void (*set_idle_callback)(uint8_t report_id, uint8_t duration), void (*set_idle_callback)(uint8_t report_id, uint8_t duration),
void (*set_protocol_callback)(uint8_t protocol), void (*set_protocol_callback)(uint8_t protocol),
uint8_t (*get_protocol_callback)(void)) uint8_t (*get_protocol_callback)(void))
// clang-format on // clang-format on
{ {
usb_slist_t *i; usb_slist_t *i;
usb_slist_for_each(i, &usbd_hid_class_head) 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); 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 (hid_intf->current_intf_num == intf_num) {
if (get_report_callback) if (get_report_callback)
hid_intf->get_report_callback = get_report_callback; hid_intf->get_report_callback = get_report_callback;
if (set_report_callback) if (set_report_callback)
hid_intf->set_report_callback = set_report_callback; hid_intf->set_report_callback = set_report_callback;
if (get_idle_callback) if (get_idle_callback)
hid_intf->get_idle_callback = get_idle_callback; hid_intf->get_idle_callback = get_idle_callback;
if (set_idle_callback) if (set_idle_callback)
hid_intf->set_idle_callback = set_idle_callback; hid_intf->set_idle_callback = set_idle_callback;
if (set_protocol_callback) if (set_protocol_callback)
hid_intf->set_protocol_callback = set_protocol_callback; hid_intf->set_protocol_callback = set_protocol_callback;
if (get_protocol_callback) if (get_protocol_callback)
hid_intf->get_protocol_callback = get_protocol_callback; hid_intf->get_protocol_callback = get_protocol_callback;
return; return;
} }
} }
} }
void usbd_hid_add_interface(usbd_class_t *devclass, usbd_interface_t *intf) void usbd_hid_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{ {
static usbd_class_t *last_class = NULL; static usbd_class_t *last_class = NULL;
static uint8_t hid_num = 0; static uint8_t hid_num = 0;
if (last_class != devclass) { if (last_class != devclass) {
last_class = devclass; last_class = devclass;
usbd_class_register(devclass); usbd_class_register(devclass);
} }
intf->class_handler = hid_class_request_handler; intf->class_handler = hid_class_request_handler;
intf->custom_handler = hid_custom_request_handler; intf->custom_handler = hid_custom_request_handler;
intf->vendor_handler = NULL; intf->vendor_handler = NULL;
intf->notify_handler = hid_notify_handler; intf->notify_handler = hid_notify_handler;
usbd_class_add_interface(devclass, intf); usbd_class_add_interface(devclass, intf);
usbd_hid_cfg[hid_num].current_intf_num = intf->intf_num; 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); usb_slist_add_tail(&usbd_hid_class_head, &usbd_hid_cfg[hid_num].list);
hid_num++; hid_num++;
} }

View File

@@ -1,275 +1,275 @@
/** /**
* @file usbh_hid.c * @file usbh_hid.c
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#include "usbh_core.h" #include "usbh_core.h"
#include "usbh_hid.h" #include "usbh_hid.h"
#define DEV_FORMAT "/dev/input%d" #define DEV_FORMAT "/dev/input%d"
#define DEV_NAMELEN 16 #define DEV_NAMELEN 16
static uint32_t g_devinuse = 0; static uint32_t g_devinuse = 0;
/**************************************************************************** /****************************************************************************
* Name: usbh_hid_devno_alloc * Name: usbh_hid_devno_alloc
* *
* Description: * Description:
* Allocate a unique /dev/hid[n] minor number in the range 0-31. * Allocate a unique /dev/hid[n] minor number in the range 0-31.
* *
****************************************************************************/ ****************************************************************************/
static int usbh_hid_devno_alloc(struct usbh_hid *priv) static int usbh_hid_devno_alloc(struct usbh_hid *priv)
{ {
uint32_t flags; uint32_t flags;
int devno; int devno;
flags = usb_osal_enter_critical_section(); flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 32; devno++) { for (devno = 0; devno < 32; devno++) {
uint32_t bitno = 1 << devno; uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) { if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno; g_devinuse |= bitno;
priv->minor = devno; priv->minor = devno;
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
return 0; return 0;
} }
} }
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
return -EMFILE; return -EMFILE;
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_hid_devno_free * Name: usbh_hid_devno_free
* *
* Description: * Description:
* Free a /dev/hid[n] minor number so that it can be used. * Free a /dev/hid[n] minor number so that it can be used.
* *
****************************************************************************/ ****************************************************************************/
static void usbh_hid_devno_free(struct usbh_hid *priv) static void usbh_hid_devno_free(struct usbh_hid *priv)
{ {
int devno = priv->minor; int devno = priv->minor;
if (devno >= 0 && devno < 32) { if (devno >= 0 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section(); uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno); g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
} }
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_hid_mkdevname * Name: usbh_hid_mkdevname
* *
* Description: * Description:
* Format a /dev/hid[n] device name given a minor number. * Format a /dev/hid[n] device name given a minor number.
* *
****************************************************************************/ ****************************************************************************/
static inline void usbh_hid_mkdevname(struct usbh_hid *priv, char *devname) static inline void usbh_hid_mkdevname(struct usbh_hid *priv, char *devname)
{ {
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor); snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor);
} }
int usbh_hid_get_report_descriptor(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer) int usbh_hid_get_report_descriptor(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{ {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv; struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup; setup = hid_class->setup;
if (hid_class->intf != intf) { if (hid_class->intf != intf) {
return -1; return -1;
} }
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE; setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = USB_REQUEST_GET_DESCRIPTOR; setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8; setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
setup->wIndex = intf; setup->wIndex = intf;
setup->wLength = 128; setup->wLength = 128;
return usbh_control_transfer(hport->ep0, setup, buffer); 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 usbh_hid_set_idle(struct usbh_hubport *hport, uint8_t intf, uint8_t report_id, uint8_t duration)
{ {
int ret; int ret;
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv; struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup; setup = hid_class->setup;
if (hid_class->intf != intf) { if (hid_class->intf != intf) {
return -1; return -1;
} }
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE; setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = HID_REQUEST_SET_IDLE; setup->bRequest = HID_REQUEST_SET_IDLE;
setup->wValue = report_id; setup->wValue = report_id;
setup->wIndex = (duration << 8) | intf; setup->wIndex = (duration << 8) | intf;
setup->wLength = 0; setup->wLength = 0;
ret = usbh_control_transfer(hport->ep0, setup, NULL); ret = usbh_control_transfer(hport->ep0, setup, NULL);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
return 0; return 0;
} }
int usbh_hid_get_idle(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer) int usbh_hid_get_idle(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{ {
int ret; int ret;
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv; struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
setup = hid_class->setup; setup = hid_class->setup;
if (hid_class->intf != intf) { if (hid_class->intf != intf) {
return -1; return -1;
} }
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE; setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = HID_REQUEST_GET_IDLE; setup->bRequest = HID_REQUEST_GET_IDLE;
setup->wValue = 0; setup->wValue = 0;
setup->wIndex = intf; setup->wIndex = intf;
setup->wLength = 1; setup->wLength = 1;
ret = usbh_control_transfer(hport->ep0, setup, buffer); ret = usbh_control_transfer(hport->ep0, setup, buffer);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
return 0; return 0;
} }
USB_NOCACHE_RAM_SECTION uint8_t report_buffer[128]; USB_NOCACHE_RAM_SECTION uint8_t report_buffer[128];
void usbh_hid_callback(void *arg, int nbytes) void usbh_hid_callback(void *arg, int nbytes)
{ {
printf("nbytes:%d\r\n", nbytes); printf("nbytes:%d\r\n", nbytes);
} }
int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf) int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf)
{ {
struct usbh_endpoint_cfg ep_cfg = { 0 }; struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc; struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN]; char devname[DEV_NAMELEN];
int ret; int ret;
struct usbh_hid *hid_class = usb_malloc(sizeof(struct usbh_hid)); struct usbh_hid *hid_class = usb_malloc(sizeof(struct usbh_hid));
if (hid_class == NULL) { if (hid_class == NULL) {
USB_LOG_ERR("Fail to alloc hid_class\r\n"); USB_LOG_ERR("Fail to alloc hid_class\r\n");
return -ENOMEM; return -ENOMEM;
} }
memset(hid_class, 0, sizeof(struct usbh_hid)); memset(hid_class, 0, sizeof(struct usbh_hid));
usbh_hid_devno_alloc(hid_class); usbh_hid_devno_alloc(hid_class);
usbh_hid_mkdevname(hid_class, devname); usbh_hid_mkdevname(hid_class, devname);
hport->config.intf[intf].priv = hid_class; hport->config.intf[intf].priv = hid_class;
hid_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet)); hid_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (hid_class->setup == NULL) { if (hid_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n"); USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM; return -ENOMEM;
} }
hid_class->intf = intf; hid_class->intf = intf;
ret = usbh_hid_set_idle(hport, intf, 0, 0); ret = usbh_hid_set_idle(hport, intf, 0, 0);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = usbh_hid_get_report_descriptor(hport, intf, report_buffer); ret = usbh_hid_get_report_descriptor(hport, intf, report_buffer);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
for (uint8_t i = 0; i < hport->config.intf[intf].intf_desc.bNumEndpoints; i++) { 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_desc = &hport->config.intf[intf].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress; ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize; ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval; ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport; ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) { if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&hid_class->intin, &ep_cfg); usbh_ep_alloc(&hid_class->intin, &ep_cfg);
} else { } else {
usbh_ep_alloc(&hid_class->intout, &ep_cfg); usbh_ep_alloc(&hid_class->intout, &ep_cfg);
} }
} }
USB_LOG_INFO("Register HID Class:%s\r\n", devname); 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); ret = usbh_ep_intr_async_transfer(hid_class->intin, report_buffer, 128, usbh_hid_callback, NULL);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
#if 0 #if 0
ret = usbh_ep_intr_transfer(hid_class->intin, report_buffer, 128); ret = usbh_ep_intr_transfer(hid_class->intin, report_buffer, 128);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
USB_LOG_INFO("recv len:%d\r\n", ret); USB_LOG_INFO("recv len:%d\r\n", ret);
#endif #endif
return 0; return 0;
} }
int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf) int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf)
{ {
char devname[DEV_NAMELEN]; char devname[DEV_NAMELEN];
int ret = 0; int ret = 0;
struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv; struct usbh_hid *hid_class = (struct usbh_hid *)hport->config.intf[intf].priv;
if (hid_class) { if (hid_class) {
usbh_hid_devno_free(hid_class); usbh_hid_devno_free(hid_class);
usbh_hid_mkdevname(hid_class, devname); usbh_hid_mkdevname(hid_class, devname);
if (hid_class->intin) { if (hid_class->intin) {
ret = usb_ep_cancel(hid_class->intin); ret = usb_ep_cancel(hid_class->intin);
if (ret < 0) { if (ret < 0) {
} }
usbh_ep_free(hid_class->intin); usbh_ep_free(hid_class->intin);
} }
if (hid_class->intout) { if (hid_class->intout) {
ret = usb_ep_cancel(hid_class->intout); ret = usb_ep_cancel(hid_class->intout);
if (ret < 0) { if (ret < 0) {
} }
usbh_ep_free(hid_class->intout); usbh_ep_free(hid_class->intout);
} }
if (hid_class->setup) if (hid_class->setup)
usb_iofree(hid_class->setup); usb_iofree(hid_class->setup);
usb_free(hid_class); usb_free(hid_class);
hport->config.intf[intf].priv = NULL; hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister HID Class:%s\r\n", devname); USB_LOG_INFO("Unregister HID Class:%s\r\n", devname);
} }
return ret; return ret;
} }
const struct usbh_class_driver hid_class_driver = { const struct usbh_class_driver hid_class_driver = {
.driver_name = "hid", .driver_name = "hid",
.connect = usbh_hid_connect, .connect = usbh_hid_connect,
.disconnect = usbh_hid_disconnect .disconnect = usbh_hid_disconnect
}; };

View File

@@ -1,45 +1,45 @@
/** /**
* @file usbh_hid.h * @file usbh_hid.h
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#ifndef _USBH_HID_H #ifndef _USBH_HID_H
#define _USBH_HID_H #define _USBH_HID_H
#include "usb_hid.h" #include "usb_hid.h"
struct usbh_hid { struct usbh_hid {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
uint8_t intf; /* interface number */ uint8_t intf; /* interface number */
uint8_t minor; uint8_t minor;
usbh_epinfo_t intin; /* INTR IN endpoint */ usbh_epinfo_t intin; /* INTR IN endpoint */
usbh_epinfo_t intout; /* INTR OUT endpoint */ usbh_epinfo_t intout; /* INTR OUT endpoint */
}; };
extern const struct usbh_class_driver hid_class_driver; extern const struct usbh_class_driver hid_class_driver;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@@ -1,497 +1,497 @@
/** /**
* @file usbh_hub.c * @file usbh_hub.c
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#include "usbh_core.h" #include "usbh_core.h"
#include "usbh_hub.h" #include "usbh_hub.h"
#define DEV_FORMAT "/dev/hub%d" #define DEV_FORMAT "/dev/hub%d"
#define DEV_NAMELEN 16 #define DEV_NAMELEN 16
static uint32_t g_devinuse = 0; static uint32_t g_devinuse = 0;
usb_slist_t hub_class_head = USB_SLIST_OBJECT_INIT(hub_class_head); 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]; 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_connect(struct usbh_hubport *hport);
extern void usbh_external_hport_disconnect(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_activate(struct usbh_hubport *hport);
extern void usbh_hport_deactivate(struct usbh_hubport *hport); extern void usbh_hport_deactivate(struct usbh_hubport *hport);
static void usbh_external_hub_callback(void *arg, int nbytes); static void usbh_external_hub_callback(void *arg, int nbytes);
static inline void usbh_hub_register(struct usbh_hub *hub) static inline void usbh_hub_register(struct usbh_hub *hub)
{ {
usb_slist_add_tail(&hub_class_head, &hub->list); usb_slist_add_tail(&hub_class_head, &hub->list);
} }
static inline void usbh_hub_unregister(struct usbh_hub *hub) static inline void usbh_hub_unregister(struct usbh_hub *hub)
{ {
usb_slist_remove(&hub_class_head, &hub->list); usb_slist_remove(&hub_class_head, &hub->list);
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_hub_devno_alloc * Name: usbh_hub_devno_alloc
* *
* Description: * Description:
* Allocate a unique /dev/hub[n] minor number in the range 2-31. * Allocate a unique /dev/hub[n] minor number in the range 2-31.
* *
****************************************************************************/ ****************************************************************************/
static int usbh_hub_devno_alloc(struct usbh_hub *hub) static int usbh_hub_devno_alloc(struct usbh_hub *hub)
{ {
uint32_t flags; uint32_t flags;
int devno; int devno;
flags = usb_osal_enter_critical_section(); flags = usb_osal_enter_critical_section();
for (devno = 2; devno < 32; devno++) { for (devno = 2; devno < 32; devno++) {
uint32_t bitno = 1 << devno; uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) { if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno; g_devinuse |= bitno;
hub->index = devno; hub->index = devno;
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
return 0; return 0;
} }
} }
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
return -EMFILE; return -EMFILE;
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_hub_devno_free * Name: usbh_hub_devno_free
* *
* Description: * Description:
* Free a /dev/hub[n] minor number so that it can be used. * Free a /dev/hub[n] minor number so that it can be used.
* *
****************************************************************************/ ****************************************************************************/
static void usbh_hub_devno_free(struct usbh_hub *hub) static void usbh_hub_devno_free(struct usbh_hub *hub)
{ {
int devno = hub->index; int devno = hub->index;
if (devno >= 2 && devno < 32) { if (devno >= 2 && devno < 32) {
uint32_t flags = usb_osal_enter_critical_section(); uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno); g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
} }
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_hub_mkdevname * Name: usbh_hub_mkdevname
* *
* Description: * Description:
* Format a /dev/hub[n] device name given a minor number. * Format a /dev/hub[n] device name given a minor number.
* *
****************************************************************************/ ****************************************************************************/
static inline void usbh_hub_mkdevname(struct usbh_hub *hub, char *devname) static inline void usbh_hub_mkdevname(struct usbh_hub *hub, char *devname)
{ {
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, hub->index); snprintf(devname, DEV_NAMELEN, DEV_FORMAT, hub->index);
} }
int usbh_hub_get_hub_descriptor(struct usbh_hub *hub, uint8_t *buffer) int usbh_hub_get_hub_descriptor(struct usbh_hub *hub, uint8_t *buffer)
{ {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
setup = hub->setup; setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE; setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = USB_REQUEST_GET_DESCRIPTOR; setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
setup->wValue = HUB_DESCRIPTOR_TYPE_HUB << 8; setup->wValue = HUB_DESCRIPTOR_TYPE_HUB << 8;
setup->wIndex = 0; setup->wIndex = 0;
setup->wLength = USB_SIZEOF_HUB_DESC; setup->wLength = USB_SIZEOF_HUB_DESC;
return usbh_control_transfer(hub->parent->ep0, setup, buffer); return usbh_control_transfer(hub->parent->ep0, setup, buffer);
} }
int usbh_hub_get_status(struct usbh_hub *hub, uint8_t *buffer) int usbh_hub_get_status(struct usbh_hub *hub, uint8_t *buffer)
{ {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
setup = hub->setup; setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE; setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = HUB_REQUEST_GET_STATUS; setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0; setup->wValue = 0;
setup->wIndex = 0; setup->wIndex = 0;
setup->wLength = 2; setup->wLength = 2;
return usbh_control_transfer(hub->parent->ep0, setup, buffer); 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) int usbh_hub_get_portstatus(struct usbh_hub *hub, uint8_t port, struct hub_port_status *port_status)
{ {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
setup = hub->setup; setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER; setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_GET_STATUS; setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0; setup->wValue = 0;
setup->wIndex = port; setup->wIndex = port;
setup->wLength = 4; setup->wLength = 4;
return usbh_control_transfer(hub->parent->ep0, setup, (uint8_t *)port_status); 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) int usbh_hub_set_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
{ {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
setup = hub->setup; setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER; setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_SET_FEATURE; setup->bRequest = HUB_REQUEST_SET_FEATURE;
setup->wValue = feature; setup->wValue = feature;
setup->wIndex = port; setup->wIndex = port;
setup->wLength = 0; setup->wLength = 0;
return usbh_control_transfer(hub->parent->ep0, setup, NULL); return usbh_control_transfer(hub->parent->ep0, setup, NULL);
} }
int usbh_hub_clear_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature) int usbh_hub_clear_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
{ {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
setup = hub->setup; setup = hub->setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER; setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_CLEAR_FEATURE; setup->bRequest = HUB_REQUEST_CLEAR_FEATURE;
setup->wValue = feature; setup->wValue = feature;
setup->wIndex = port; setup->wIndex = port;
setup->wLength = 0; setup->wLength = 0;
return usbh_control_transfer(hub->parent->ep0, setup, NULL); return usbh_control_transfer(hub->parent->ep0, setup, NULL);
} }
static int parse_hub_descriptor(struct usb_hub_descriptor *desc, uint16_t length) static int parse_hub_descriptor(struct usb_hub_descriptor *desc, uint16_t length)
{ {
if (desc->bLength != USB_SIZEOF_HUB_DESC) { if (desc->bLength != USB_SIZEOF_HUB_DESC) {
USB_LOG_ERR("invalid device bLength 0x%02x\r\n", desc->bLength); USB_LOG_ERR("invalid device bLength 0x%02x\r\n", desc->bLength);
return -1; return -1;
} else if (desc->bDescriptorType != HUB_DESCRIPTOR_TYPE_HUB) { } else if (desc->bDescriptorType != HUB_DESCRIPTOR_TYPE_HUB) {
USB_LOG_ERR("unexpected descriptor 0x%02x\r\n", desc->bDescriptorType); USB_LOG_ERR("unexpected descriptor 0x%02x\r\n", desc->bDescriptorType);
return -2; return -2;
} else { } else {
USB_LOG_INFO("Device Descriptor:\r\n"); USB_LOG_INFO("Device Descriptor:\r\n");
USB_LOG_INFO("bLength: 0x%02x \r\n", desc->bLength); USB_LOG_INFO("bLength: 0x%02x \r\n", desc->bLength);
USB_LOG_INFO("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType); USB_LOG_INFO("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType);
USB_LOG_INFO("bNbrPorts: 0x%02x \r\n", desc->bNbrPorts); USB_LOG_INFO("bNbrPorts: 0x%02x \r\n", desc->bNbrPorts);
USB_LOG_INFO("wHubCharacteristics: 0x%04x \r\n", desc->wHubCharacteristics); USB_LOG_INFO("wHubCharacteristics: 0x%04x \r\n", desc->wHubCharacteristics);
USB_LOG_INFO("bPwrOn2PwrGood: 0x%02x \r\n", desc->bPwrOn2PwrGood); USB_LOG_INFO("bPwrOn2PwrGood: 0x%02x \r\n", desc->bPwrOn2PwrGood);
USB_LOG_INFO("bHubContrCurrent: 0x%02x \r\n", desc->bHubContrCurrent); USB_LOG_INFO("bHubContrCurrent: 0x%02x \r\n", desc->bHubContrCurrent);
USB_LOG_INFO("DeviceRemovable: 0x%02x \r\n", desc->DeviceRemovable); USB_LOG_INFO("DeviceRemovable: 0x%02x \r\n", desc->DeviceRemovable);
USB_LOG_INFO("PortPwrCtrlMask: 0x%02x \r\n", desc->PortPwrCtrlMask); USB_LOG_INFO("PortPwrCtrlMask: 0x%02x \r\n", desc->PortPwrCtrlMask);
} }
return 0; return 0;
} }
int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf) int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf)
{ {
struct usbh_endpoint_cfg ep_cfg = { 0 }; struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc; struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN]; char devname[DEV_NAMELEN];
int ret; int ret;
uint8_t *hub_desc_buffer; uint8_t *hub_desc_buffer;
struct usbh_hub *hub_class; struct usbh_hub *hub_class;
hub_class = usb_malloc(sizeof(struct usbh_hub)); hub_class = usb_malloc(sizeof(struct usbh_hub));
if (hub_class == NULL) { if (hub_class == NULL) {
USB_LOG_ERR("Fail to alloc hub_class\r\n"); USB_LOG_ERR("Fail to alloc hub_class\r\n");
return -ENOMEM; return -ENOMEM;
} }
memset(hub_class, 0, sizeof(struct usbh_hub)); memset(hub_class, 0, sizeof(struct usbh_hub));
hub_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet)); hub_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (hub_class->setup == NULL) { if (hub_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n"); USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM; return -ENOMEM;
} }
hub_class->port_status = usb_iomalloc(sizeof(struct hub_port_status)); hub_class->port_status = usb_iomalloc(sizeof(struct hub_port_status));
if (hub_class->port_status == NULL) { if (hub_class->port_status == NULL) {
USB_LOG_ERR("Fail to alloc port_status\r\n"); USB_LOG_ERR("Fail to alloc port_status\r\n");
return -ENOMEM; return -ENOMEM;
} }
hub_desc_buffer = usb_iomalloc(32); hub_desc_buffer = usb_iomalloc(32);
usbh_hub_devno_alloc(hub_class); usbh_hub_devno_alloc(hub_class);
usbh_hub_mkdevname(hub_class, devname); usbh_hub_mkdevname(hub_class, devname);
hub_class->dev_addr = hport->dev_addr; hub_class->dev_addr = hport->dev_addr;
hub_class->parent = hport; hub_class->parent = hport;
hport->config.intf[0].priv = hub_class; hport->config.intf[0].priv = hub_class;
ret = usbh_hub_get_hub_descriptor(hub_class, hub_desc_buffer); ret = usbh_hub_get_hub_descriptor(hub_class, hub_desc_buffer);
if (ret != 0) { if (ret != 0) {
usb_iofree(hub_desc_buffer); usb_iofree(hub_desc_buffer);
return ret; return ret;
} }
parse_hub_descriptor((struct usb_hub_descriptor *)hub_desc_buffer, USB_SIZEOF_HUB_DESC); 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); memcpy(&hub_class->hub_desc, hub_desc_buffer, USB_SIZEOF_HUB_DESC);
usb_iofree(hub_desc_buffer); usb_iofree(hub_desc_buffer);
hub_class->nports = hub_class->hub_desc.bNbrPorts; hub_class->nports = hub_class->hub_desc.bNbrPorts;
for (uint8_t port = 1; port <= hub_class->nports; port++) { for (uint8_t port = 1; port <= hub_class->nports; port++) {
hub_class->child[port - 1].port = port; hub_class->child[port - 1].port = port;
hub_class->child[port - 1].parent = hub_class; hub_class->child[port - 1].parent = hub_class;
} }
hub_class->int_buffer = int_buffer[hub_class->index - 2]; hub_class->int_buffer = int_buffer[hub_class->index - 2];
usbh_hub_register(hub_class); usbh_hub_register(hub_class);
ep_desc = &hport->config.intf[intf].ep[0].ep_desc; ep_desc = &hport->config.intf[intf].ep[0].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress; ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize; ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval; ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport; ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) { if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&hub_class->intin, &ep_cfg); usbh_ep_alloc(&hub_class->intin, &ep_cfg);
} else { } else {
return -1; return -1;
} }
for (uint8_t port = 1; port <= hub_class->nports; port++) { for (uint8_t port = 1; port <= hub_class->nports; port++) {
ret = usbh_hub_set_feature(hub_class, 1, HUB_PORT_FEATURE_POWER); ret = usbh_hub_set_feature(hub_class, 1, HUB_PORT_FEATURE_POWER);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
} }
for (uint8_t port = 1; port <= hub_class->nports; port++) { for (uint8_t port = 1; port <= hub_class->nports; port++) {
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status); 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); 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) { if (ret < 0) {
return ret; return ret;
} }
} }
USB_LOG_INFO("Register HUB Class:%s\r\n", devname); 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); 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; return 0;
} }
int usbh_hub_disconnect(struct usbh_hubport *hport, uint8_t intf) int usbh_hub_disconnect(struct usbh_hubport *hport, uint8_t intf)
{ {
struct usbh_hubport *child; struct usbh_hubport *child;
char devname[DEV_NAMELEN]; char devname[DEV_NAMELEN];
int ret = 0; int ret = 0;
struct usbh_hub *hub_class = (struct usbh_hub *)hport->config.intf[intf].priv; struct usbh_hub *hub_class = (struct usbh_hub *)hport->config.intf[intf].priv;
if (hub_class) { if (hub_class) {
usbh_hub_devno_free(hub_class); usbh_hub_devno_free(hub_class);
usbh_hub_mkdevname(hub_class, devname); usbh_hub_mkdevname(hub_class, devname);
if (hub_class->intin) { if (hub_class->intin) {
ret = usb_ep_cancel(hub_class->intin); ret = usb_ep_cancel(hub_class->intin);
if (ret < 0) { if (ret < 0) {
} }
usbh_ep_free(hub_class->intin); usbh_ep_free(hub_class->intin);
} }
if (hub_class->setup) if (hub_class->setup)
usb_iofree(hub_class->setup); usb_iofree(hub_class->setup);
if (hub_class->port_status) if (hub_class->port_status)
usb_iofree(hub_class->port_status); usb_iofree(hub_class->port_status);
for (uint8_t port = 1; port <= hub_class->nports; port++) { for (uint8_t port = 1; port <= hub_class->nports; port++) {
child = &hub_class->child[port - 1]; child = &hub_class->child[port - 1];
usbh_hport_deactivate(child); usbh_hport_deactivate(child);
for (uint8_t i = 0; i < child->config.config_desc.bNumInterfaces; i++) { 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) { if (child->config.intf[i].class_driver && child->config.intf[i].class_driver->disconnect) {
ret = CLASS_DISCONNECT(child, i); ret = CLASS_DISCONNECT(child, i);
} }
} }
child->config.config_desc.bNumInterfaces = 0; child->config.config_desc.bNumInterfaces = 0;
child->parent = NULL; child->parent = NULL;
} }
usbh_hub_unregister(hub_class); usbh_hub_unregister(hub_class);
usb_free(hub_class); usb_free(hub_class);
hport->config.intf[intf].priv = NULL; hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister HUB Class:%s\r\n", devname); USB_LOG_INFO("Unregister HUB Class:%s\r\n", devname);
} }
return ret; return ret;
} }
static void usbh_extern_hub_psc_event(void *arg) static void usbh_extern_hub_psc_event(void *arg)
{ {
struct usbh_hub *hub_class; struct usbh_hub *hub_class;
struct usbh_hubport *connport; struct usbh_hubport *connport;
uint8_t port_change; uint8_t port_change;
uint16_t status; uint16_t status;
uint16_t change; uint16_t change;
uint16_t mask; uint16_t mask;
uint16_t feat; uint16_t feat;
uint32_t flags; uint32_t flags;
int ret; int ret;
hub_class = (struct usbh_hub *)arg; hub_class = (struct usbh_hub *)arg;
/* Has the hub been disconnected? */ /* Has the hub been disconnected? */
if (!hub_class->parent->connected) { if (!hub_class->parent->connected) {
return; return;
} }
port_change = hub_class->int_buffer[0]; port_change = hub_class->int_buffer[0];
USB_LOG_DBG("port_change:0x%02x\r\n", port_change); USB_LOG_DBG("port_change:0x%02x\r\n", port_change);
/* Check for status change on any port */ /* Check for status change on any port */
for (uint8_t port = USBH_HUB_PORT_START_INDEX; port <= hub_class->nports; port++) { for (uint8_t port = USBH_HUB_PORT_START_INDEX; port <= hub_class->nports; port++) {
/* Check if port status has changed */ /* Check if port status has changed */
if ((port_change & (1 << port)) == 0) { if ((port_change & (1 << port)) == 0) {
continue; continue;
} }
USB_LOG_DBG("Port %d change\r\n", port); USB_LOG_DBG("Port %d change\r\n", port);
/* Port status changed, check what happened */ /* Port status changed, check what happened */
port_change &= ~(1 << port); port_change &= ~(1 << port);
/* Read hub port status */ /* Read hub port status */
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status); ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) { if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret); USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
continue; continue;
} }
status = hub_class->port_status->wPortStatus; status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange; change = hub_class->port_status->wPortChange;
USB_LOG_DBG("Port:%d, status:0x%02x, change:0x%02x\r\n", port, status, change); USB_LOG_DBG("Port:%d, status:0x%02x, change:0x%02x\r\n", port, status, change);
/* First, clear all change bits */ /* First, clear all change bits */
mask = 1; mask = 1;
feat = HUB_PORT_FEATURE_C_CONNECTION; feat = HUB_PORT_FEATURE_C_CONNECTION;
while (change) { while (change) {
if (change & mask) { if (change & mask) {
ret = usbh_hub_clear_feature(hub_class, port, feat); ret = usbh_hub_clear_feature(hub_class, port, feat);
if (ret < 0) { if (ret < 0) {
USB_LOG_ERR("Failed to clear port:%d, change mask:%04x, errorcode:%d\r\n", port, mask, ret); USB_LOG_ERR("Failed to clear port:%d, change mask:%04x, errorcode:%d\r\n", port, mask, ret);
} }
change &= (~mask); change &= (~mask);
} }
mask <<= 1; mask <<= 1;
feat++; feat++;
} }
change = hub_class->port_status->wPortChange; change = hub_class->port_status->wPortChange;
/* Handle connect or disconnect, no power management */ /* Handle connect or disconnect, no power management */
if (change & HUB_PORT_STATUS_C_CONNECTION) { if (change & HUB_PORT_STATUS_C_CONNECTION) {
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status); ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) { if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret); USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
} }
status = hub_class->port_status->wPortStatus; status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange; change = hub_class->port_status->wPortChange;
if (status & HUB_PORT_STATUS_CONNECTION) { if (status & HUB_PORT_STATUS_CONNECTION) {
/* Device connected to a port on the hub */ /* Device connected to a port on the hub */
//USB_LOG_INFO("Connection on port:%d\n", port); //USB_LOG_INFO("Connection on port:%d\n", port);
ret = usbh_hub_set_feature(hub_class, port, HUB_PORT_FEATURE_RESET); ret = usbh_hub_set_feature(hub_class, port, HUB_PORT_FEATURE_RESET);
if (ret < 0) { if (ret < 0) {
USB_LOG_ERR("Failed to reset port:%d,errorcode:%d\r\n", port, ret); USB_LOG_ERR("Failed to reset port:%d,errorcode:%d\r\n", port, ret);
continue; continue;
} }
usb_osal_msleep(100); usb_osal_msleep(100);
ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status); ret = usbh_hub_get_portstatus(hub_class, port, hub_class->port_status);
if (ret < 0) { if (ret < 0) {
USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret); USB_LOG_ERR("Failed to read port:%d status, errorcode: %d\r\n", port, ret);
continue; continue;
} }
status = hub_class->port_status->wPortStatus; status = hub_class->port_status->wPortStatus;
change = hub_class->port_status->wPortChange; change = hub_class->port_status->wPortChange;
USB_LOG_DBG("Port:%d, status:0x%02x, change:0x%02x after reset\r\n", port, status, change); 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 ((status & HUB_PORT_STATUS_RESET) == 0 && (status & HUB_PORT_STATUS_ENABLE) != 0) {
if (change & HUB_PORT_STATUS_C_RESET) { if (change & HUB_PORT_STATUS_C_RESET) {
ret = usbh_hub_clear_feature(hub_class, port, HUB_PORT_FEATURE_C_RESET); ret = usbh_hub_clear_feature(hub_class, port, HUB_PORT_FEATURE_C_RESET);
if (ret < 0) { if (ret < 0) {
USB_LOG_ERR("Failed to clear port:%d reset change, errorcode: %d\r\n", port, ret); USB_LOG_ERR("Failed to clear port:%d reset change, errorcode: %d\r\n", port, ret);
} }
} }
connport = &hub_class->child[port - 1]; connport = &hub_class->child[port - 1];
if (status & HUB_PORT_STATUS_HIGH_SPEED) { if (status & HUB_PORT_STATUS_HIGH_SPEED) {
connport->speed = USB_SPEED_HIGH; connport->speed = USB_SPEED_HIGH;
} else if (status & HUB_PORT_STATUS_LOW_SPEED) { } else if (status & HUB_PORT_STATUS_LOW_SPEED) {
connport->speed = USB_SPEED_LOW; connport->speed = USB_SPEED_LOW;
} else { } else {
connport->speed = USB_SPEED_FULL; connport->speed = USB_SPEED_FULL;
} }
/* Device connected from a port on the hub, wakeup psc thread. */ /* Device connected from a port on the hub, wakeup psc thread. */
usbh_external_hport_connect(connport); usbh_external_hport_connect(connport);
} else { } else {
USB_LOG_ERR("Failed to enable port:%d\r\n", port); USB_LOG_ERR("Failed to enable port:%d\r\n", port);
continue; continue;
} }
} else { } else {
/* Device disconnected from a port on the hub, wakeup psc thread. */ /* Device disconnected from a port on the hub, wakeup psc thread. */
connport = &hub_class->child[port - 1]; connport = &hub_class->child[port - 1];
usbh_external_hport_disconnect(connport); usbh_external_hport_disconnect(connport);
} }
} else { } else {
USB_LOG_WRN("status %04x change %04x not handled\r\n", status, change); USB_LOG_WRN("status %04x change %04x not handled\r\n", status, change);
} }
} }
/* Check for hub status change */ /* Check for hub status change */
if ((port_change & 1) != 0) { if ((port_change & 1) != 0) {
/* Hub status changed */ /* Hub status changed */
USB_LOG_WRN("Hub status changed, not handled\n"); USB_LOG_WRN("Hub status changed, not handled\n");
} }
flags = usb_osal_enter_critical_section(); flags = usb_osal_enter_critical_section();
if (hub_class->parent->connected) { 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); 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); usb_osal_leave_critical_section(flags);
} }
static void usbh_external_hub_callback(void *arg, int nbytes) static void usbh_external_hub_callback(void *arg, int nbytes)
{ {
struct usbh_hub *hub_class = (struct usbh_hub *)arg; struct usbh_hub *hub_class = (struct usbh_hub *)arg;
uint32_t delay = 0; uint32_t delay = 0;
if (nbytes < 0) { if (nbytes < 0) {
hub_class->int_buffer[0] = 0; hub_class->int_buffer[0] = 0;
delay = 100; delay = 100;
} }
if (hub_class->parent->connected) { if (hub_class->parent->connected) {
usb_workqueue_submit(&g_lpworkq, &hub_class->work, usbh_extern_hub_psc_event, (void *)hub_class, delay); 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 = { const struct usbh_class_driver hub_class_driver = {
.driver_name = "hub", .driver_name = "hub",
.connect = usbh_hub_connect, .connect = usbh_hub_connect,
.disconnect = usbh_hub_disconnect .disconnect = usbh_hub_disconnect
}; };

View File

@@ -1,42 +1,42 @@
/** /**
* @file usbh_hub.h * @file usbh_hub.h
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#ifndef _USBH_HUB_H_ #ifndef _USBH_HUB_H_
#define _USBH_HUB_H_ #define _USBH_HUB_H_
#include "usb_hub.h" #include "usb_hub.h"
#define USBH_HUB_MAX_PORTS 4 #define USBH_HUB_MAX_PORTS 4
/* Maximum size of an interrupt IN transfer */ /* Maximum size of an interrupt IN transfer */
#define USBH_HUB_INTIN_BUFSIZE ((USBH_HUB_MAX_PORTS + 8) >> 3) #define USBH_HUB_INTIN_BUFSIZE ((USBH_HUB_MAX_PORTS + 8) >> 3)
extern const struct usbh_class_driver hub_class_driver; extern const struct usbh_class_driver hub_class_driver;
extern usb_slist_t hub_class_head; extern usb_slist_t hub_class_head;
extern usb_osal_thread_t hub_thread; extern usb_osal_thread_t hub_thread;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
int usbh_hub_initialize(void); int usbh_hub_initialize(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* _USBH_HUB_H_ */ #endif /* _USBH_HUB_H_ */

File diff suppressed because it is too large Load Diff

View File

@@ -1,438 +1,438 @@
/** /**
* @file usbh_msc.c * @file usbh_msc.c
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#include "usbh_core.h" #include "usbh_core.h"
#include "usbh_msc.h" #include "usbh_msc.h"
#include "scsi.h" #include "scsi.h"
#define DEV_FORMAT "/dev/sd%c" #define DEV_FORMAT "/dev/sd%c"
#define DEV_NAMELEN 16 #define DEV_NAMELEN 16
static uint32_t g_devinuse = 0; static uint32_t g_devinuse = 0;
/**************************************************************************** /****************************************************************************
* Name: usbh_msc_devno_alloc * Name: usbh_msc_devno_alloc
* *
* Description: * Description:
* Allocate a unique /dev/ttyACM[n] minor number in the range 0-31. * Allocate a unique /dev/ttyACM[n] minor number in the range 0-31.
* *
****************************************************************************/ ****************************************************************************/
static int usbh_msc_devno_alloc(struct usbh_msc *priv) static int usbh_msc_devno_alloc(struct usbh_msc *priv)
{ {
uint32_t flags; uint32_t flags;
int devno; int devno;
flags = usb_osal_enter_critical_section(); flags = usb_osal_enter_critical_section();
for (devno = 0; devno < 26; devno++) { for (devno = 0; devno < 26; devno++) {
uint32_t bitno = 1 << devno; uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) { if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno; g_devinuse |= bitno;
priv->sdchar = 'a' + devno; priv->sdchar = 'a' + devno;
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
return 0; return 0;
} }
} }
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
return -EMFILE; return -EMFILE;
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_msc_devno_free * Name: usbh_msc_devno_free
* *
* Description: * Description:
* Free a /dev/sd[n] minor number so that it can be used. * Free a /dev/sd[n] minor number so that it can be used.
* *
****************************************************************************/ ****************************************************************************/
static void usbh_msc_devno_free(struct usbh_msc *priv) static void usbh_msc_devno_free(struct usbh_msc *priv)
{ {
int devno = priv->sdchar - 'a'; int devno = priv->sdchar - 'a';
if (devno >= 0 && devno < 26) { if (devno >= 0 && devno < 26) {
uint32_t flags = usb_osal_enter_critical_section(); uint32_t flags = usb_osal_enter_critical_section();
g_devinuse &= ~(1 << devno); g_devinuse &= ~(1 << devno);
usb_osal_leave_critical_section(flags); usb_osal_leave_critical_section(flags);
} }
} }
/**************************************************************************** /****************************************************************************
* Name: usbh_msc_mkdevname * Name: usbh_msc_mkdevname
* *
* Description: * Description:
* Format a /dev/sd[n] device name given a minor number. * Format a /dev/sd[n] device name given a minor number.
* *
****************************************************************************/ ****************************************************************************/
static inline void usbh_msc_mkdevname(struct usbh_msc *priv, char *devname) static inline void usbh_msc_mkdevname(struct usbh_msc *priv, char *devname)
{ {
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->sdchar); snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->sdchar);
} }
static int usbh_msc_get_maxlun(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer) static int usbh_msc_get_maxlun(struct usbh_hubport *hport, uint8_t intf, uint8_t *buffer)
{ {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv; struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv;
setup = msc_class->setup; setup = msc_class->setup;
if (msc_class->intf != intf) { if (msc_class->intf != intf) {
return -1; return -1;
} }
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE; setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
setup->bRequest = MSC_REQUEST_GET_MAX_LUN; setup->bRequest = MSC_REQUEST_GET_MAX_LUN;
setup->wValue = 0; setup->wValue = 0;
setup->wIndex = intf; setup->wIndex = intf;
setup->wLength = 1; setup->wLength = 1;
return usbh_control_transfer(hport->ep0, setup, buffer); return usbh_control_transfer(hport->ep0, setup, buffer);
} }
static void usbh_msc_cbw_dump(struct CBW *cbw) static void usbh_msc_cbw_dump(struct CBW *cbw)
{ {
int i; int i;
USB_LOG_INFO("CBW:\r\n"); USB_LOG_INFO("CBW:\r\n");
USB_LOG_INFO(" signature: 0x%08x\r\n", (unsigned int)cbw->dSignature); 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(" tag: 0x%08x\r\n", (unsigned int)cbw->dTag);
USB_LOG_INFO(" datlen: 0x%08x\r\n", (unsigned int)cbw->dDataLength); 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(" flags: 0x%02x\r\n", cbw->bmFlags);
USB_LOG_INFO(" lun: 0x%02x\r\n", cbw->bLUN); USB_LOG_INFO(" lun: 0x%02x\r\n", cbw->bLUN);
USB_LOG_INFO(" cblen: 0x%02x\r\n", cbw->bCBLength); USB_LOG_INFO(" cblen: 0x%02x\r\n", cbw->bCBLength);
USB_LOG_INFO("CB:\r\n"); USB_LOG_INFO("CB:\r\n");
for (i = 0; i < cbw->bCBLength; i += 8) { 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", 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], cbw->CB[i + 1], cbw->CB[i + 2],
cbw->CB[i + 3], cbw->CB[i + 4], cbw->CB[i + 5], cbw->CB[i + 3], cbw->CB[i + 4], cbw->CB[i + 5],
cbw->CB[i + 6], cbw->CB[i + 7]); cbw->CB[i + 6], cbw->CB[i + 7]);
} }
} }
static void usbh_msc_csw_dump(struct CSW *csw) static void usbh_msc_csw_dump(struct CSW *csw)
{ {
USB_LOG_INFO("CSW:\r\n"); USB_LOG_INFO("CSW:\r\n");
USB_LOG_INFO(" signature: 0x%08x\r\n", (unsigned int)csw->dSignature); 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(" tag: 0x%08x\r\n", (unsigned int)csw->dTag);
USB_LOG_INFO(" residue: 0x%08x\r\n", (unsigned int)csw->dDataResidue); USB_LOG_INFO(" residue: 0x%08x\r\n", (unsigned int)csw->dDataResidue);
USB_LOG_INFO(" status: 0x%02x\r\n", csw->bStatus); USB_LOG_INFO(" status: 0x%02x\r\n", csw->bStatus);
} }
static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class) static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class)
{ {
int nbytes; int nbytes;
struct CBW *cbw; struct CBW *cbw;
/* Construct the CBW */ /* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer; cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW); memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature; cbw->dSignature = MSC_CBW_Signature;
cbw->bCBLength = SCSICMD_TESTUNITREADY_SIZEOF; cbw->bCBLength = SCSICMD_TESTUNITREADY_SIZEOF;
cbw->CB[0] = SCSI_CMD_TESTUNITREADY; cbw->CB[0] = SCSI_CMD_TESTUNITREADY;
/* Send the CBW */ /* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the CSW */ /* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) { if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer); usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
} }
} }
return nbytes < 0 ? (int)nbytes : 0; return nbytes < 0 ? (int)nbytes : 0;
} }
static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class) static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class)
{ {
int nbytes; int nbytes;
struct CBW *cbw; struct CBW *cbw;
/* Construct the CBW */ /* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer; cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW); memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature; cbw->dSignature = MSC_CBW_Signature;
cbw->bmFlags = 0x80; cbw->bmFlags = 0x80;
cbw->bCBLength = SCSIRESP_FIXEDSENSEDATA_SIZEOF; cbw->bCBLength = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
cbw->dDataLength = SCSICMD_REQUESTSENSE_SIZEOF; cbw->dDataLength = SCSICMD_REQUESTSENSE_SIZEOF;
cbw->CB[0] = SCSI_REQUEST_SENSE; cbw->CB[0] = SCSI_REQUEST_SENSE;
cbw->CB[4] = SCSIRESP_FIXEDSENSEDATA_SIZEOF; cbw->CB[4] = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
/* Send the CBW */ /* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the sense data response */ /* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_FIXEDSENSEDATA_SIZEOF); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_FIXEDSENSEDATA_SIZEOF);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the CSW */ /* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) { if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer); usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
} }
} }
} }
return nbytes < 0 ? (int)nbytes : 0; return nbytes < 0 ? (int)nbytes : 0;
} }
static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class) static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class)
{ {
int nbytes; int nbytes;
struct CBW *cbw; struct CBW *cbw;
/* Construct the CBW */ /* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer; cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW); memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature; cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = SCSIRESP_INQUIRY_SIZEOF; cbw->dDataLength = SCSIRESP_INQUIRY_SIZEOF;
cbw->bmFlags = 0x80; cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_INQUIRY_SIZEOF; cbw->bCBLength = SCSICMD_INQUIRY_SIZEOF;
cbw->CB[0] = SCSI_INQUIRY; cbw->CB[0] = SCSI_INQUIRY;
cbw->CB[4] = SCSIRESP_INQUIRY_SIZEOF; cbw->CB[4] = SCSIRESP_INQUIRY_SIZEOF;
/* Send the CBW */ /* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the sense data response */ /* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_INQUIRY_SIZEOF); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_INQUIRY_SIZEOF);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the CSW */ /* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) { if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer); usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
} }
} }
} }
return nbytes < 0 ? (int)nbytes : 0; return nbytes < 0 ? (int)nbytes : 0;
} }
static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class) static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class)
{ {
int nbytes; int nbytes;
struct CBW *cbw; struct CBW *cbw;
/* Construct the CBW */ /* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer; cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW); memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature; cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = SCSIRESP_READCAPACITY10_SIZEOF; cbw->dDataLength = SCSIRESP_READCAPACITY10_SIZEOF;
cbw->bmFlags = 0x80; cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_READCAPACITY10_SIZEOF; cbw->bCBLength = SCSICMD_READCAPACITY10_SIZEOF;
cbw->CB[0] = SCSI_READ_CAPACITY10; cbw->CB[0] = SCSI_READ_CAPACITY10;
/* Send the CBW */ /* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the sense data response */ /* Receive the sense data response */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_READCAPACITY10_SIZEOF); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_READCAPACITY10_SIZEOF);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Save the capacity information */ /* Save the capacity information */
msc_class->blocknum = GET_BE32(&msc_class->tx_buffer[0]) + 1; msc_class->blocknum = GET_BE32(&msc_class->tx_buffer[0]) + 1;
msc_class->blocksize = GET_BE32(&msc_class->tx_buffer[4]); msc_class->blocksize = GET_BE32(&msc_class->tx_buffer[4]);
USB_LOG_INFO("capacity info:\r\n"); 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); USB_LOG_INFO("block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
/* Receive the CSW */ /* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) { if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer); usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
} }
} }
} }
return nbytes < 0 ? (int)nbytes : 0; 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 usbh_msc_mem_write(struct usbh_msc *msc_class, uint32_t sector, const uint8_t *buffer, uint32_t nsectors)
{ {
int nbytes; int nbytes;
struct CBW *cbw; struct CBW *cbw;
/* Construct the CBW */ /* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer; cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW); memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature; cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = (msc_class->blocksize * nsectors); cbw->dDataLength = (msc_class->blocksize * nsectors);
cbw->bCBLength = SCSICMD_WRITE10_SIZEOF; cbw->bCBLength = SCSICMD_WRITE10_SIZEOF;
cbw->CB[0] = SCSI_WRITE10; cbw->CB[0] = SCSI_WRITE10;
SET_BE24(&cbw->CB[2], sector); SET_BE24(&cbw->CB[2], sector);
SET_BE24(&cbw->CB[7], nsectors); SET_BE24(&cbw->CB[7], nsectors);
/* Send the CBW */ /* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Send the user data */ /* Send the user data */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)buffer, msc_class->blocksize * nsectors); nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)buffer, msc_class->blocksize * nsectors);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the CSW */ /* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) { if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer); usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
} }
} }
} }
return nbytes < 0 ? (int)nbytes : 0; 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 usbh_msc_mem_read(struct usbh_msc *msc_class, uint32_t sector, const uint8_t *buffer, uint32_t nsectors)
{ {
int nbytes; int nbytes;
struct CBW *cbw; struct CBW *cbw;
/* Construct the CBW */ /* Construct the CBW */
cbw = (struct CBW *)msc_class->tx_buffer; cbw = (struct CBW *)msc_class->tx_buffer;
memset(cbw, 0, USB_SIZEOF_MSC_CBW); memset(cbw, 0, USB_SIZEOF_MSC_CBW);
cbw->dSignature = MSC_CBW_Signature; cbw->dSignature = MSC_CBW_Signature;
cbw->dDataLength = (msc_class->blocksize * nsectors); cbw->dDataLength = (msc_class->blocksize * nsectors);
cbw->bmFlags = 0x80; cbw->bmFlags = 0x80;
cbw->bCBLength = SCSICMD_READ10_SIZEOF; cbw->bCBLength = SCSICMD_READ10_SIZEOF;
cbw->CB[0] = SCSI_READ10; cbw->CB[0] = SCSI_READ10;
SET_BE24(&cbw->CB[2], sector); SET_BE24(&cbw->CB[2], sector);
SET_BE24(&cbw->CB[7], nsectors); SET_BE24(&cbw->CB[7], nsectors);
/* Send the CBW */ /* Send the CBW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the user data */ /* Receive the user data */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, (uint8_t *)buffer, msc_class->blocksize * nsectors); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, (uint8_t *)buffer, msc_class->blocksize * nsectors);
if (nbytes >= 0) { if (nbytes >= 0) {
/* Receive the CSW */ /* Receive the CSW */
nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW);
if (nbytes >= 0) { if (nbytes >= 0) {
usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer); usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer);
} }
} }
} }
return nbytes < 0 ? (int)nbytes : 0; return nbytes < 0 ? (int)nbytes : 0;
} }
int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf) int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf)
{ {
struct usbh_endpoint_cfg ep_cfg = { 0 }; struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc; struct usb_endpoint_descriptor *ep_desc;
char devname[DEV_NAMELEN]; char devname[DEV_NAMELEN];
int ret; int ret;
struct usbh_msc *msc_class = usb_malloc(sizeof(struct usbh_msc)); struct usbh_msc *msc_class = usb_malloc(sizeof(struct usbh_msc));
if (msc_class == NULL) { if (msc_class == NULL) {
USB_LOG_ERR("Fail to alloc msc_class\r\n"); USB_LOG_ERR("Fail to alloc msc_class\r\n");
return -ENOMEM; return -ENOMEM;
} }
memset(msc_class, 0, sizeof(struct usbh_msc)); memset(msc_class, 0, sizeof(struct usbh_msc));
usbh_msc_devno_alloc(msc_class); usbh_msc_devno_alloc(msc_class);
usbh_msc_mkdevname(msc_class, devname); usbh_msc_mkdevname(msc_class, devname);
hport->config.intf[intf].priv = msc_class; hport->config.intf[intf].priv = msc_class;
msc_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet)); msc_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
if (msc_class->setup == NULL) { if (msc_class->setup == NULL) {
USB_LOG_ERR("Fail to alloc setup\r\n"); USB_LOG_ERR("Fail to alloc setup\r\n");
return -ENOMEM; return -ENOMEM;
} }
msc_class->tx_buffer = usb_iomalloc(128); msc_class->tx_buffer = usb_iomalloc(128);
if (msc_class->tx_buffer == NULL) { if (msc_class->tx_buffer == NULL) {
USB_LOG_ERR("Fail to alloc tx_buffer\r\n"); USB_LOG_ERR("Fail to alloc tx_buffer\r\n");
return -ENOMEM; return -ENOMEM;
} }
ret = usbh_msc_get_maxlun(hport, intf, msc_class->tx_buffer); ret = usbh_msc_get_maxlun(hport, intf, msc_class->tx_buffer);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
USB_LOG_INFO("Get max LUN:%u\r\n", msc_class->tx_buffer[0]); 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++) { 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_desc = &hport->config.intf[intf].ep[i].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress; ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize; ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
ep_cfg.ep_interval = ep_desc->bInterval; ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport; ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) { if (ep_desc->bEndpointAddress & 0x80) {
usbh_ep_alloc(&msc_class->bulkin, &ep_cfg); usbh_ep_alloc(&msc_class->bulkin, &ep_cfg);
} else { } else {
usbh_ep_alloc(&msc_class->bulkout, &ep_cfg); usbh_ep_alloc(&msc_class->bulkout, &ep_cfg);
} }
} }
USB_LOG_INFO("Register MSC Class:%s\r\n", devname); USB_LOG_INFO("Register MSC Class:%s\r\n", devname);
ret = usbh_msc_scsi_testunitready(msc_class); ret = usbh_msc_scsi_testunitready(msc_class);
ret = usbh_msc_scsi_inquiry(msc_class); ret = usbh_msc_scsi_inquiry(msc_class);
ret = usbh_msc_scsi_readcapacity10(msc_class); ret = usbh_msc_scsi_readcapacity10(msc_class);
return ret; return ret;
} }
int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf) int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf)
{ {
char devname[DEV_NAMELEN]; char devname[DEV_NAMELEN];
int ret = 0; int ret = 0;
struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv; struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv;
if (msc_class) { if (msc_class) {
usbh_msc_devno_free(msc_class); usbh_msc_devno_free(msc_class);
usbh_msc_mkdevname(msc_class, devname); usbh_msc_mkdevname(msc_class, devname);
if (msc_class->bulkin) { if (msc_class->bulkin) {
ret = usb_ep_cancel(msc_class->bulkin); ret = usb_ep_cancel(msc_class->bulkin);
if (ret < 0) { if (ret < 0) {
} }
usbh_ep_free(msc_class->bulkin); usbh_ep_free(msc_class->bulkin);
} }
if (msc_class->bulkout) { if (msc_class->bulkout) {
ret = usb_ep_cancel(msc_class->bulkout); ret = usb_ep_cancel(msc_class->bulkout);
if (ret < 0) { if (ret < 0) {
} }
usbh_ep_free(msc_class->bulkout); usbh_ep_free(msc_class->bulkout);
} }
if (msc_class->setup) if (msc_class->setup)
usb_iofree(msc_class->setup); usb_iofree(msc_class->setup);
if (msc_class->tx_buffer) if (msc_class->tx_buffer)
usb_iofree(msc_class->tx_buffer); usb_iofree(msc_class->tx_buffer);
usb_free(msc_class); usb_free(msc_class);
hport->config.intf[intf].priv = NULL; hport->config.intf[intf].priv = NULL;
USB_LOG_INFO("Unregister MSC Class:%s\r\n", devname); USB_LOG_INFO("Unregister MSC Class:%s\r\n", devname);
} }
return ret; return ret;
} }
const struct usbh_class_driver msc_class_driver = { const struct usbh_class_driver msc_class_driver = {
.driver_name = "msc", .driver_name = "msc",
.connect = usbh_msc_connect, .connect = usbh_msc_connect,
.disconnect = usbh_msc_disconnect .disconnect = usbh_msc_disconnect
}; };

View File

@@ -1,49 +1,49 @@
/** /**
* @file usbh_msc.h * @file usbh_msc.h
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#ifndef _USBH_MSC_H #ifndef _USBH_MSC_H
#define _USBH_MSC_H #define _USBH_MSC_H
#include "usb_msc.h" #include "usb_msc.h"
#include "usb_scsi.h" #include "usb_scsi.h"
struct usbh_msc { struct usbh_msc {
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
uint8_t intf; /* Data interface number */ uint8_t intf; /* Data interface number */
uint8_t sdchar; uint8_t sdchar;
usbh_epinfo_t bulkin; /* Bulk IN endpoint */ usbh_epinfo_t bulkin; /* Bulk IN endpoint */
usbh_epinfo_t bulkout; /* Bulk OUT endpoint */ usbh_epinfo_t bulkout; /* Bulk OUT endpoint */
uint8_t *tx_buffer; uint8_t *tx_buffer;
uint32_t blocknum; /* Number of blocks on the USB mass storage device */ uint32_t blocknum; /* Number of blocks on the USB mass storage device */
uint16_t blocksize; /* Block size of USB mass storage device */ uint16_t blocksize; /* Block size of USB mass storage device */
}; };
extern const struct usbh_class_driver msc_class_driver; extern const struct usbh_class_driver msc_class_driver;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@@ -1,134 +1,134 @@
/** /**
* @file usbd_video.c * @file usbd_video.c
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#include "usbd_core.h" #include "usbd_core.h"
#include "usbd_video.h" #include "usbd_video.h"
extern struct video_probe_and_commit_controls probe; extern struct video_probe_and_commit_controls probe;
extern struct video_probe_and_commit_controls commit; extern struct video_probe_and_commit_controls commit;
int video_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) int video_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{ {
USB_LOG_DBG("Video Class request: " USB_LOG_DBG("Video Class request: "
"bRequest 0x%02x\r\n", "bRequest 0x%02x\r\n",
setup->bRequest); setup->bRequest);
switch (setup->bRequest) { switch (setup->bRequest) {
case VIDEO_REQUEST_SET_CUR: case VIDEO_REQUEST_SET_CUR:
if (setup->wValue == 256) { if (setup->wValue == 256) {
memcpy((uint8_t *)&probe, *data, setup->wLength); memcpy((uint8_t *)&probe, *data, setup->wLength);
} else if (setup->wValue == 512) { } else if (setup->wValue == 512) {
memcpy((uint8_t *)&commit, *data, setup->wLength); memcpy((uint8_t *)&commit, *data, setup->wLength);
} }
break; break;
case VIDEO_REQUEST_GET_CUR: case VIDEO_REQUEST_GET_CUR:
if (setup->wValue == 256) { if (setup->wValue == 256) {
*data = (uint8_t *)&probe; *data = (uint8_t *)&probe;
} else if (setup->wValue == 512) { } else if (setup->wValue == 512) {
*data = (uint8_t *)&commit; *data = (uint8_t *)&commit;
} }
break; break;
case VIDEO_REQUEST_GET_MIN: case VIDEO_REQUEST_GET_MIN:
if (setup->wValue == 256) { if (setup->wValue == 256) {
*data = (uint8_t *)&probe; *data = (uint8_t *)&probe;
} else if (setup->wValue == 512) { } else if (setup->wValue == 512) {
*data = (uint8_t *)&commit; *data = (uint8_t *)&commit;
} }
break; break;
case VIDEO_REQUEST_GET_MAX: case VIDEO_REQUEST_GET_MAX:
if (setup->wValue == 256) { if (setup->wValue == 256) {
*data = (uint8_t *)&probe; *data = (uint8_t *)&probe;
} else if (setup->wValue == 512) { } else if (setup->wValue == 512) {
*data = (uint8_t *)&commit; *data = (uint8_t *)&commit;
} }
break; break;
case VIDEO_REQUEST_GET_RES: case VIDEO_REQUEST_GET_RES:
break; break;
case VIDEO_REQUEST_GET_LEN: case VIDEO_REQUEST_GET_LEN:
break; break;
case VIDEO_REQUEST_GET_INFO: case VIDEO_REQUEST_GET_INFO:
break; break;
case VIDEO_REQUEST_GET_DEF: case VIDEO_REQUEST_GET_DEF:
if (setup->wLength == 256) { if (setup->wLength == 256) {
*data = (uint8_t *)&probe; *data = (uint8_t *)&probe;
} else if (setup->wLength == 512) { } else if (setup->wLength == 512) {
*data = (uint8_t *)&commit; *data = (uint8_t *)&commit;
} }
break; break;
default: default:
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest); USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
return -1; return -1;
} }
return 0; return 0;
} }
void video_notify_handler(uint8_t event, void *arg) void video_notify_handler(uint8_t event, void *arg)
{ {
switch (event) { switch (event) {
case USBD_EVENT_RESET: case USBD_EVENT_RESET:
break; break;
case USBD_EVENT_SOF: case USBD_EVENT_SOF:
usbd_video_sof_callback(); usbd_video_sof_callback();
break; break;
case USBD_EVENT_SET_INTERFACE: case USBD_EVENT_SET_INTERFACE:
usbd_video_set_interface_callback(((uint8_t *)arg)[3]); usbd_video_set_interface_callback(((uint8_t *)arg)[3]);
break; break;
default: default:
break; break;
} }
} }
void usbd_video_add_interface(usbd_class_t *devclass, usbd_interface_t *intf) void usbd_video_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{ {
static usbd_class_t *last_class = NULL; static usbd_class_t *last_class = NULL;
if (last_class != devclass) { if (last_class != devclass) {
last_class = devclass; last_class = devclass;
usbd_class_register(devclass); usbd_class_register(devclass);
} }
intf->class_handler = video_class_request_handler; intf->class_handler = video_class_request_handler;
intf->custom_handler = NULL; intf->custom_handler = NULL;
intf->vendor_handler = NULL; intf->vendor_handler = NULL;
intf->notify_handler = video_notify_handler; intf->notify_handler = video_notify_handler;
usbd_class_add_interface(devclass, intf); usbd_class_add_interface(devclass, intf);
} }

View File

@@ -1,200 +1,200 @@
/** /**
* @file usb_dc.h * @file usb_dc.h
* @brief * @brief
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#ifndef _USB_DC_H #ifndef _USB_DC_H
#define _USB_DC_H #define _USB_DC_H
#include <stdint.h> #include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/** /**
* USB endpoint direction and number. * USB endpoint direction and number.
*/ */
#define USB_EP_DIR_MASK 0x80U #define USB_EP_DIR_MASK 0x80U
#define USB_EP_DIR_IN 0x80U #define USB_EP_DIR_IN 0x80U
#define USB_EP_DIR_OUT 0x00U #define USB_EP_DIR_OUT 0x00U
/** Get endpoint index (number) from endpoint address */ /** Get endpoint index (number) from endpoint address */
#define USB_EP_GET_IDX(ep) ((ep) & ~USB_EP_DIR_MASK) #define USB_EP_GET_IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
/** Get direction from endpoint address */ /** Get direction from endpoint address */
#define USB_EP_GET_DIR(ep) ((ep)&USB_EP_DIR_MASK) #define USB_EP_GET_DIR(ep) ((ep)&USB_EP_DIR_MASK)
/** Get endpoint address from endpoint index and direction */ /** Get endpoint address from endpoint index and direction */
#define USB_EP_GET_ADDR(idx, dir) ((idx) | ((dir)&USB_EP_DIR_MASK)) #define USB_EP_GET_ADDR(idx, dir) ((idx) | ((dir)&USB_EP_DIR_MASK))
/** True if the endpoint is an IN endpoint */ /** True if the endpoint is an IN endpoint */
#define USB_EP_DIR_IS_IN(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_IN) #define USB_EP_DIR_IS_IN(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_IN)
/** True if the endpoint is an OUT endpoint */ /** True if the endpoint is an OUT endpoint */
#define USB_EP_DIR_IS_OUT(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_OUT) #define USB_EP_DIR_IS_OUT(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_OUT)
/* Default USB control EP, always 0 and 0x80 */ /* Default USB control EP, always 0 and 0x80 */
#define USB_CONTROL_OUT_EP0 0 #define USB_CONTROL_OUT_EP0 0
#define USB_CONTROL_IN_EP0 0x80 #define USB_CONTROL_IN_EP0 0x80
/**< maximum packet size (MPS) for EP 0 */ /**< maximum packet size (MPS) for EP 0 */
#define USB_CTRL_EP_MPS 64 #define USB_CTRL_EP_MPS 64
/** /**
* USB endpoint Transfer Type mask. * USB endpoint Transfer Type mask.
*/ */
#define USBD_EP_TYPE_CTRL 0 #define USBD_EP_TYPE_CTRL 0
#define USBD_EP_TYPE_ISOC 1 #define USBD_EP_TYPE_ISOC 1
#define USBD_EP_TYPE_BULK 2 #define USBD_EP_TYPE_BULK 2
#define USBD_EP_TYPE_INTR 3 #define USBD_EP_TYPE_INTR 3
#define USBD_EP_TYPE_MASK 3 #define USBD_EP_TYPE_MASK 3
/** /**
* @brief USB Endpoint Configuration. * @brief USB Endpoint Configuration.
* *
* Structure containing the USB endpoint configuration. * Structure containing the USB endpoint configuration.
*/ */
struct usbd_endpoint_cfg { struct usbd_endpoint_cfg {
/** The number associated with the EP in the device /** The number associated with the EP in the device
* configuration structure * configuration structure
* IN EP = 0x80 | \<endpoint number\> * IN EP = 0x80 | \<endpoint number\>
* OUT EP = 0x00 | \<endpoint number\> * OUT EP = 0x00 | \<endpoint number\>
*/ */
uint8_t ep_addr; uint8_t ep_addr;
/** Endpoint Transfer Type. /** Endpoint Transfer Type.
* May be Bulk, Interrupt, Control or Isochronous * May be Bulk, Interrupt, Control or Isochronous
*/ */
uint8_t ep_type; uint8_t ep_type;
/** Endpoint max packet size */ /** Endpoint max packet size */
uint16_t ep_mps; uint16_t ep_mps;
}; };
/** /**
* @brief USB Device Core Layer API * @brief USB Device Core Layer API
* @defgroup _usb_device_core_api USB Device Core API * @defgroup _usb_device_core_api USB Device Core API
* @{ * @{
*/ */
/** /**
* @brief Set USB device address * @brief Set USB device address
* *
* @param[in] addr Device address * @param[in] addr Device address
* *
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */
int usbd_set_address(const uint8_t addr); int usbd_set_address(const uint8_t addr);
/* /*
* @brief configure and enable endpoint * @brief configure and enable endpoint
* *
* This function sets endpoint configuration according to one specified in USB * This function sets endpoint configuration according to one specified in USB
* endpoint descriptor and then enables it for data transfers. * endpoint descriptor and then enables it for data transfers.
* *
* @param [in] ep_desc Endpoint descriptor byte array * @param [in] ep_desc Endpoint descriptor byte array
* *
* @return true if successfully configured and enabled * @return true if successfully configured and enabled
*/ */
int usbd_ep_open(const struct usbd_endpoint_cfg *ep_cfg); int usbd_ep_open(const struct usbd_endpoint_cfg *ep_cfg);
/** /**
* @brief Disable the selected endpoint * @brief Disable the selected endpoint
* *
* Function to disable the selected endpoint. Upon success interrupts are * Function to disable the selected endpoint. Upon success interrupts are
* disabled for the corresponding endpoint and the endpoint is no longer able * disabled for the corresponding endpoint and the endpoint is no longer able
* for transmitting/receiving data. * for transmitting/receiving data.
* *
* @param[in] ep Endpoint address corresponding to the one * @param[in] ep Endpoint address corresponding to the one
* listed in the device configuration table * listed in the device configuration table
* *
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */
int usbd_ep_close(const uint8_t ep); int usbd_ep_close(const uint8_t ep);
/** /**
* @brief Set stall condition for the selected endpoint * @brief Set stall condition for the selected endpoint
* *
* @param[in] ep Endpoint address corresponding to the one * @param[in] ep Endpoint address corresponding to the one
* listed in the device configuration table * listed in the device configuration table
* *
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */
int usbd_ep_set_stall(const uint8_t ep); int usbd_ep_set_stall(const uint8_t ep);
/** /**
* @brief Clear stall condition for the selected endpoint * @brief Clear stall condition for the selected endpoint
* *
* @param[in] ep Endpoint address corresponding to the one * @param[in] ep Endpoint address corresponding to the one
* listed in the device configuration table * listed in the device configuration table
* *
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */
int usbd_ep_clear_stall(const uint8_t ep); int usbd_ep_clear_stall(const uint8_t ep);
/** /**
* @brief Check if the selected endpoint is stalled * @brief Check if the selected endpoint is stalled
* *
* @param[in] ep Endpoint address corresponding to the one * @param[in] ep Endpoint address corresponding to the one
* listed in the device configuration table * listed in the device configuration table
* @param[out] stalled Endpoint stall status * @param[out] stalled Endpoint stall status
* *
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */
int usbd_ep_is_stalled(const uint8_t ep, uint8_t *stalled); int usbd_ep_is_stalled(const uint8_t ep, uint8_t *stalled);
/** /**
* @brief Write data to the specified endpoint * @brief Write data to the specified endpoint
* *
* This function is called to write data to the specified endpoint. The * This function is called to write data to the specified endpoint. The
* supplied usbd_endpoint_callback function will be called when data is transmitted * supplied usbd_endpoint_callback function will be called when data is transmitted
* out. * out.
* *
* @param[in] ep Endpoint address corresponding to the one * @param[in] ep Endpoint address corresponding to the one
* listed in the device configuration table * listed in the device configuration table
* @param[in] data Pointer to data to write * @param[in] data Pointer to data to write
* @param[in] data_len Length of the data requested to write. This may * @param[in] data_len Length of the data requested to write. This may
* be zero for a zero length status packet. * be zero for a zero length status packet.
* @param[out] ret_bytes Bytes scheduled for transmission. This value * @param[out] ret_bytes Bytes scheduled for transmission. This value
* may be NULL if the application expects all * may be NULL if the application expects all
* bytes to be written * bytes to be written
* *
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */
int usbd_ep_write(const uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t *ret_bytes); int usbd_ep_write(const uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t *ret_bytes);
/** /**
* @brief Read data from the specified endpoint * @brief Read data from the specified endpoint
* *
* This is similar to usb_dc_ep_read, the difference being that, it doesn't * This is similar to usb_dc_ep_read, the difference being that, it doesn't
* clear the endpoint NAKs so that the consumer is not bogged down by further * clear the endpoint NAKs so that the consumer is not bogged down by further
* upcalls till he is done with the processing of the data. The caller should * upcalls till he is done with the processing of the data. The caller should
* reactivate ep by setting max_data_len 0 do so. * reactivate ep by setting max_data_len 0 do so.
* *
* @param[in] ep Endpoint address corresponding to the one * @param[in] ep Endpoint address corresponding to the one
* listed in the device configuration table * listed in the device configuration table
* @param[in] data Pointer to data buffer to write to * @param[in] data Pointer to data buffer to write to
* @param[in] max_data_len Max length of data to read * @param[in] max_data_len Max length of data to read
* @param[out] read_bytes Number of bytes read. If data is NULL and * @param[out] read_bytes Number of bytes read. If data is NULL and
* max_data_len is 0 the number of bytes * max_data_len is 0 the number of bytes
* available for read should be returned. * available for read should be returned.
* *
* @return 0 on success, negative errno code on fail. * @return 0 on success, negative errno code on fail.
*/ */
int usbd_ep_read(const uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *read_bytes); int usbd_ep_read(const uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *read_bytes);
/** /**
* @} * @}
*/ */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,300 +1,300 @@
/** /**
* @file usb_util.h * @file usb_util.h
* @brief * @brief
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#ifndef _USB_UTIL_H #ifndef _USB_UTIL_H
#define _USB_UTIL_H #define _USB_UTIL_H
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "usb_errno.h" #include "usb_errno.h"
#include "usb_list.h" #include "usb_list.h"
#include "usb_mem.h" #include "usb_mem.h"
#if defined(__CC_ARM) #if defined(__CC_ARM)
#ifndef __USED #ifndef __USED
#define __USED __attribute__((used)) #define __USED __attribute__((used))
#endif #endif
#ifndef __WEAK #ifndef __WEAK
#define __WEAK __attribute__((weak)) #define __WEAK __attribute__((weak))
#endif #endif
#ifndef __PACKED #ifndef __PACKED
#define __PACKED __attribute__((packed)) #define __PACKED __attribute__((packed))
#endif #endif
#ifndef __PACKED_STRUCT #ifndef __PACKED_STRUCT
#define __PACKED_STRUCT __packed struct #define __PACKED_STRUCT __packed struct
#endif #endif
#ifndef __PACKED_UNION #ifndef __PACKED_UNION
#define __PACKED_UNION __packed union #define __PACKED_UNION __packed union
#endif #endif
#ifndef __ALIGNED #ifndef __ALIGNED
#define __ALIGNED(x) __attribute__((aligned(x))) #define __ALIGNED(x) __attribute__((aligned(x)))
#endif #endif
#elif defined(__GNUC__) #elif defined(__GNUC__)
#ifndef __USED #ifndef __USED
#define __USED __attribute__((used)) #define __USED __attribute__((used))
#endif #endif
#ifndef __WEAK #ifndef __WEAK
#define __WEAK __attribute__((weak)) #define __WEAK __attribute__((weak))
#endif #endif
#ifndef __PACKED #ifndef __PACKED
#define __PACKED __attribute__((packed, aligned(1))) #define __PACKED __attribute__((packed, aligned(1)))
#endif #endif
#ifndef __PACKED_STRUCT #ifndef __PACKED_STRUCT
#define __PACKED_STRUCT struct __attribute__((packed, aligned(1))) #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
#endif #endif
#ifndef __PACKED_UNION #ifndef __PACKED_UNION
#define __PACKED_UNION union __attribute__((packed, aligned(1))) #define __PACKED_UNION union __attribute__((packed, aligned(1)))
#endif #endif
#ifndef __ALIGNED #ifndef __ALIGNED
#define __ALIGNED(x) __attribute__((aligned(x))) #define __ALIGNED(x) __attribute__((aligned(x)))
#endif #endif
#elif defined(__ICCARM__) #elif defined(__ICCARM__)
#ifndef __USED #ifndef __USED
#if __ICCARM_V8 #if __ICCARM_V8
#define __USED __attribute__((used)) #define __USED __attribute__((used))
#else #else
#define __USED _Pragma("__root") #define __USED _Pragma("__root")
#endif #endif
#endif #endif
#ifndef __WEAK #ifndef __WEAK
#if __ICCARM_V8 #if __ICCARM_V8
#define __WEAK __attribute__((weak)) #define __WEAK __attribute__((weak))
#else #else
#define __WEAK _Pragma("__weak") #define __WEAK _Pragma("__weak")
#endif #endif
#endif #endif
#ifndef __PACKED #ifndef __PACKED
#if __ICCARM_V8 #if __ICCARM_V8
#define __PACKED __attribute__((packed, aligned(1))) #define __PACKED __attribute__((packed, aligned(1)))
#else #else
/* Needs IAR language extensions */ /* Needs IAR language extensions */
#define __PACKED __packed #define __PACKED __packed
#endif #endif
#endif #endif
#ifndef __PACKED_STRUCT #ifndef __PACKED_STRUCT
#if __ICCARM_V8 #if __ICCARM_V8
#define __PACKED_STRUCT struct __attribute__((packed, aligned(1))) #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
#else #else
/* Needs IAR language extensions */ /* Needs IAR language extensions */
#define __PACKED_STRUCT __packed struct #define __PACKED_STRUCT __packed struct
#endif #endif
#endif #endif
#ifndef __PACKED_UNION #ifndef __PACKED_UNION
#if __ICCARM_V8 #if __ICCARM_V8
#define __PACKED_UNION union __attribute__((packed, aligned(1))) #define __PACKED_UNION union __attribute__((packed, aligned(1)))
#else #else
/* Needs IAR language extensions */ /* Needs IAR language extensions */
#define __PACKED_UNION __packed union #define __PACKED_UNION __packed union
#endif #endif
#endif #endif
#ifndef __ALIGNED #ifndef __ALIGNED
#if __ICCARM_V8 #if __ICCARM_V8
#define __ALIGNED(x) __attribute__((aligned(x))) #define __ALIGNED(x) __attribute__((aligned(x)))
#elif (__VER__ >= 7080000) #elif (__VER__ >= 7080000)
/* Needs IAR language extensions */ /* Needs IAR language extensions */
#define __ALIGNED(x) __attribute__((aligned(x))) #define __ALIGNED(x) __attribute__((aligned(x)))
#else #else
#warning No compiler specific solution for __ALIGNED.__ALIGNED is ignored. #warning No compiler specific solution for __ALIGNED.__ALIGNED is ignored.
#define __ALIGNED(x) #define __ALIGNED(x)
#endif #endif
#endif #endif
#endif #endif
#ifndef __ALIGN_BEGIN #ifndef __ALIGN_BEGIN
#define __ALIGN_BEGIN #define __ALIGN_BEGIN
#endif #endif
#ifndef __ALIGN_END #ifndef __ALIGN_END
#define __ALIGN_END __attribute__((aligned(4))) #define __ALIGN_END __attribute__((aligned(4)))
#endif #endif
#ifndef ARG_UNUSED #ifndef ARG_UNUSED
#define ARG_UNUSED(x) (void)(x) #define ARG_UNUSED(x) (void)(x)
#endif #endif
#ifndef LO_BYTE #ifndef LO_BYTE
#define LO_BYTE(x) ((uint8_t)(x & 0x00FF)) #define LO_BYTE(x) ((uint8_t)(x & 0x00FF))
#endif #endif
#ifndef HI_BYTE #ifndef HI_BYTE
#define HI_BYTE(x) ((uint8_t)((x & 0xFF00) >> 8)) #define HI_BYTE(x) ((uint8_t)((x & 0xFF00) >> 8))
#endif #endif
/** /**
* @def MAX * @def MAX
* @brief The larger value between @p a and @p b. * @brief The larger value between @p a and @p b.
* @note Arguments are evaluated twice. * @note Arguments are evaluated twice.
*/ */
#ifndef MAX #ifndef MAX
/* Use Z_MAX for a GCC-only, single evaluation version */ /* Use Z_MAX for a GCC-only, single evaluation version */
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif #endif
/** /**
* @def MIN * @def MIN
* @brief The smaller value between @p a and @p b. * @brief The smaller value between @p a and @p b.
* @note Arguments are evaluated twice. * @note Arguments are evaluated twice.
*/ */
#ifndef MIN #ifndef MIN
/* Use Z_MIN for a GCC-only, single evaluation version */ /* Use Z_MIN for a GCC-only, single evaluation version */
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif #endif
#ifndef BCD #ifndef BCD
#define BCD(x) ((((x) / 10) << 4) | ((x) % 10)) #define BCD(x) ((((x) / 10) << 4) | ((x) % 10))
#endif #endif
#ifdef BIT #ifdef BIT
#undef BIT #undef BIT
#define BIT(n) (1UL << (n)) #define BIT(n) (1UL << (n))
#else #else
#define BIT(n) (1UL << (n)) #define BIT(n) (1UL << (n))
#endif #endif
#ifndef ARRAY_SIZE #ifndef ARRAY_SIZE
#define ARRAY_SIZE(array) \ #define ARRAY_SIZE(array) \
((int)((sizeof(array) / sizeof((array)[0])))) ((int)((sizeof(array) / sizeof((array)[0]))))
#endif #endif
#ifndef BSWAP16 #ifndef BSWAP16
#define BSWAP16(u16) (__builtin_bswap16(u16)) #define BSWAP16(u16) (__builtin_bswap16(u16))
#endif #endif
#ifndef BSWAP32 #ifndef BSWAP32
#define BSWAP32(u32) (__builtin_bswap32(u32)) #define BSWAP32(u32) (__builtin_bswap32(u32))
#endif #endif
#define GET_BE16(field) \ #define GET_BE16(field) \
(((uint16_t)(field)[0] << 8) | ((uint16_t)(field)[1])) (((uint16_t)(field)[0] << 8) | ((uint16_t)(field)[1]))
#define GET_BE32(field) \ #define GET_BE32(field) \
(((uint32_t)(field)[0] << 24) | ((uint32_t)(field)[1] << 16) | ((uint32_t)(field)[2] << 8) | ((uint32_t)(field)[3] << 0)) (((uint32_t)(field)[0] << 24) | ((uint32_t)(field)[1] << 16) | ((uint32_t)(field)[2] << 8) | ((uint32_t)(field)[3] << 0))
#define SET_BE16(field, value) \ #define SET_BE16(field, value) \
do { \ do { \
(field)[0] = (uint8_t)((value) >> 8); \ (field)[0] = (uint8_t)((value) >> 8); \
(field)[1] = (uint8_t)((value) >> 0); \ (field)[1] = (uint8_t)((value) >> 0); \
} while (0) } while (0)
#define SET_BE24(field, value) \ #define SET_BE24(field, value) \
do { \ do { \
(field)[0] = (uint8_t)((value) >> 16); \ (field)[0] = (uint8_t)((value) >> 16); \
(field)[1] = (uint8_t)((value) >> 8); \ (field)[1] = (uint8_t)((value) >> 8); \
(field)[2] = (uint8_t)((value) >> 0); \ (field)[2] = (uint8_t)((value) >> 0); \
} while (0) } while (0)
#define SET_BE32(field, value) \ #define SET_BE32(field, value) \
do { \ do { \
(field)[0] = (uint8_t)((value) >> 24); \ (field)[0] = (uint8_t)((value) >> 24); \
(field)[1] = (uint8_t)((value) >> 16); \ (field)[1] = (uint8_t)((value) >> 16); \
(field)[2] = (uint8_t)((value) >> 8); \ (field)[2] = (uint8_t)((value) >> 8); \
(field)[3] = (uint8_t)((value) >> 0); \ (field)[3] = (uint8_t)((value) >> 0); \
} while (0) } while (0)
#define REQTYPE_GET_DIR(x) (((x) >> 7) & 0x01) #define REQTYPE_GET_DIR(x) (((x) >> 7) & 0x01)
#define REQTYPE_GET_TYPE(x) (((x) >> 5) & 0x03U) #define REQTYPE_GET_TYPE(x) (((x) >> 5) & 0x03U)
#define REQTYPE_GET_RECIP(x) ((x)&0x1F) #define REQTYPE_GET_RECIP(x) ((x)&0x1F)
#define GET_DESC_TYPE(x) (((x) >> 8) & 0xFFU) #define GET_DESC_TYPE(x) (((x) >> 8) & 0xFFU)
#define GET_DESC_INDEX(x) ((x)&0xFFU) #define GET_DESC_INDEX(x) ((x)&0xFFU)
#define WBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF) #define WBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF)
#define DBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF), ((x >> 16) & 0xFF), ((x >> 24) & 0xFF) #define DBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF), ((x >> 16) & 0xFF), ((x >> 24) & 0xFF)
#define USB_DESC_SECTION __attribute__((section("usb_desc"))) __USED __ALIGNED(1) #define USB_DESC_SECTION __attribute__((section("usb_desc"))) __USED __ALIGNED(1)
/* DEBUG level */ /* DEBUG level */
#define USB_DBG_ERROR 0 #define USB_DBG_ERROR 0
#define USB_DBG_WARNING 1 #define USB_DBG_WARNING 1
#define USB_DBG_INFO 2 #define USB_DBG_INFO 2
#define USB_DBG_LOG 3 #define USB_DBG_LOG 3
#ifndef USB_DBG_LEVEL #ifndef USB_DBG_LEVEL
#define USB_DBG_LEVEL USB_DBG_INFO #define USB_DBG_LEVEL USB_DBG_INFO
#endif #endif
#ifndef USB_DBG_TAG #ifndef USB_DBG_TAG
#define USB_DBG_TAG "USB" #define USB_DBG_TAG "USB"
#endif #endif
/* /*
* The color for terminal (foreground) * The color for terminal (foreground)
* BLACK 30 * BLACK 30
* RED 31 * RED 31
* GREEN 32 * GREEN 32
* YELLOW 33 * YELLOW 33
* BLUE 34 * BLUE 34
* PURPLE 35 * PURPLE 35
* CYAN 36 * CYAN 36
* WHITE 37 * WHITE 37
*/ */
#define _USB_DBG_COLOR(n) printf("\033[" #n "m") #define _USB_DBG_COLOR(n) printf("\033[" #n "m")
#define _USB_DBG_LOG_HDR(lvl_name, color_n) \ #define _USB_DBG_LOG_HDR(lvl_name, color_n) \
printf("\033[" #color_n "m[" lvl_name "/" USB_DBG_TAG "] ") printf("\033[" #color_n "m[" lvl_name "/" USB_DBG_TAG "] ")
#define _USB_DBG_LOG_X_END \ #define _USB_DBG_LOG_X_END \
printf("\033[0m") printf("\033[0m")
#define usb_dbg_log_line(lvl, color_n, fmt, ...) \ #define usb_dbg_log_line(lvl, color_n, fmt, ...) \
do { \ do { \
_USB_DBG_LOG_HDR(lvl, color_n); \ _USB_DBG_LOG_HDR(lvl, color_n); \
printf(fmt, ##__VA_ARGS__); \ printf(fmt, ##__VA_ARGS__); \
_USB_DBG_LOG_X_END; \ _USB_DBG_LOG_X_END; \
} while (0) } while (0)
#if (USB_DBG_LEVEL >= USB_DBG_LOG) #if (USB_DBG_LEVEL >= USB_DBG_LOG)
#define USB_LOG_DBG(fmt, ...) usb_dbg_log_line("D", 0, fmt, ##__VA_ARGS__) #define USB_LOG_DBG(fmt, ...) usb_dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
#else #else
#define USB_LOG_DBG(...) #define USB_LOG_DBG(...)
#endif #endif
#if (USB_DBG_LEVEL >= USB_DBG_INFO) #if (USB_DBG_LEVEL >= USB_DBG_INFO)
#define USB_LOG_INFO(fmt, ...) usb_dbg_log_line("I", 32, fmt, ##__VA_ARGS__) #define USB_LOG_INFO(fmt, ...) usb_dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
#else #else
#define USB_LOG_INFO(...) #define USB_LOG_INFO(...)
#endif #endif
#if (USB_DBG_LEVEL >= USB_DBG_WARNING) #if (USB_DBG_LEVEL >= USB_DBG_WARNING)
#define USB_LOG_WRN(fmt, ...) usb_dbg_log_line("W", 33, fmt, ##__VA_ARGS__) #define USB_LOG_WRN(fmt, ...) usb_dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
#else #else
#define USB_LOG_WRN(...) #define USB_LOG_WRN(...)
#endif #endif
#if (USB_DBG_LEVEL >= USB_DBG_ERROR) #if (USB_DBG_LEVEL >= USB_DBG_ERROR)
#define USB_LOG_ERR(fmt, ...) usb_dbg_log_line("E", 31, fmt, ##__VA_ARGS__) #define USB_LOG_ERR(fmt, ...) usb_dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
#else #else
#define USB_LOG_ERR(...) #define USB_LOG_ERR(...)
#endif #endif
void usb_assert(const char *filename, int linenum); void usb_assert(const char *filename, int linenum);
#define USB_ASSERT(f) \ #define USB_ASSERT(f) \
do { \ do { \
if (!(f)) \ if (!(f)) \
usb_assert(__FILE__, __LINE__); \ usb_assert(__FILE__, __LINE__); \
} while (0) } while (0)
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,138 +1,138 @@
/** /**
* @file usbd_core.h * @file usbd_core.h
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#ifndef _USBD_CORE_H #ifndef _USBD_CORE_H
#define _USBD_CORE_H #define _USBD_CORE_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "usb_util.h" #include "usb_util.h"
#include "usb_def.h" #include "usb_def.h"
#include "usb_dc.h" #include "usb_dc.h"
enum usbd_event_type { enum usbd_event_type {
/** USB error reported by the controller */ /** USB error reported by the controller */
USBD_EVENT_ERROR, USBD_EVENT_ERROR,
/** USB reset */ /** USB reset */
USBD_EVENT_RESET, USBD_EVENT_RESET,
/** Start of Frame received */ /** Start of Frame received */
USBD_EVENT_SOF, USBD_EVENT_SOF,
/** USB connection established, hardware enumeration is completed */ /** USB connection established, hardware enumeration is completed */
USBD_EVENT_CONNECTED, USBD_EVENT_CONNECTED,
/** USB configuration done */ /** USB configuration done */
USBD_EVENT_CONFIGURED, USBD_EVENT_CONFIGURED,
/** USB connection suspended by the HOST */ /** USB connection suspended by the HOST */
USBD_EVENT_SUSPEND, USBD_EVENT_SUSPEND,
/** USB connection lost */ /** USB connection lost */
USBD_EVENT_DISCONNECTED, USBD_EVENT_DISCONNECTED,
/** USB connection resumed by the HOST */ /** USB connection resumed by the HOST */
USBD_EVENT_RESUME, USBD_EVENT_RESUME,
/** USB interface selected */ /** USB interface selected */
USBD_EVENT_SET_INTERFACE, USBD_EVENT_SET_INTERFACE,
/** USB interface selected */ /** USB interface selected */
USBD_EVENT_SET_REMOTE_WAKEUP, USBD_EVENT_SET_REMOTE_WAKEUP,
/** USB interface selected */ /** USB interface selected */
USBD_EVENT_CLEAR_REMOTE_WAKEUP, USBD_EVENT_CLEAR_REMOTE_WAKEUP,
/** Set Feature ENDPOINT_HALT received */ /** Set Feature ENDPOINT_HALT received */
USBD_EVENT_SET_HALT, USBD_EVENT_SET_HALT,
/** Clear Feature ENDPOINT_HALT received */ /** Clear Feature ENDPOINT_HALT received */
USBD_EVENT_CLEAR_HALT, USBD_EVENT_CLEAR_HALT,
/** setup packet received */ /** setup packet received */
USBD_EVENT_SETUP_NOTIFY, USBD_EVENT_SETUP_NOTIFY,
/** ep0 in packet received */ /** ep0 in packet received */
USBD_EVENT_EP0_IN_NOTIFY, USBD_EVENT_EP0_IN_NOTIFY,
/** ep0 out packet received */ /** ep0 out packet received */
USBD_EVENT_EP0_OUT_NOTIFY, USBD_EVENT_EP0_OUT_NOTIFY,
/** ep in packet except ep0 received */ /** ep in packet except ep0 received */
USBD_EVENT_EP_IN_NOTIFY, USBD_EVENT_EP_IN_NOTIFY,
/** ep out packet except ep0 received */ /** ep out packet except ep0 received */
USBD_EVENT_EP_OUT_NOTIFY, USBD_EVENT_EP_OUT_NOTIFY,
/** Initial USB connection status */ /** Initial USB connection status */
USBD_EVENT_UNKNOWN USBD_EVENT_UNKNOWN
}; };
/** /**
* @brief Callback function signature for the USB Endpoint status * @brief Callback function signature for the USB Endpoint status
*/ */
typedef void (*usbd_endpoint_callback)(uint8_t ep); typedef void (*usbd_endpoint_callback)(uint8_t ep);
/** /**
* @brief Callback function signature for class specific requests * @brief Callback function signature for class specific requests
* *
* Function which handles Class specific requests corresponding to an * Function which handles Class specific requests corresponding to an
* interface number specified in the device descriptor table. For host * interface number specified in the device descriptor table. For host
* to device direction the 'len' and 'payload_data' contain the length * to device direction the 'len' and 'payload_data' contain the length
* of the received data and the pointer to the received data respectively. * of the received data and the pointer to the received data respectively.
* For device to host class requests, 'len' and 'payload_data' should be * For device to host class requests, 'len' and 'payload_data' should be
* set by the callback function with the length and the address of the * set by the callback function with the length and the address of the
* data to be transmitted buffer respectively. * data to be transmitted buffer respectively.
*/ */
typedef int (*usbd_request_handler)(struct usb_setup_packet *setup, typedef int (*usbd_request_handler)(struct usb_setup_packet *setup,
uint8_t **data, uint32_t *transfer_len); uint8_t **data, uint32_t *transfer_len);
/* callback function pointer structure for Application to handle events */ /* callback function pointer structure for Application to handle events */
typedef void (*usbd_notify_handler)(uint8_t event, void *arg); typedef void (*usbd_notify_handler)(uint8_t event, void *arg);
typedef struct usbd_endpoint { typedef struct usbd_endpoint {
usb_slist_t list; usb_slist_t list;
uint8_t ep_addr; uint8_t ep_addr;
usbd_endpoint_callback ep_cb; usbd_endpoint_callback ep_cb;
} usbd_endpoint_t; } usbd_endpoint_t;
typedef struct usbd_interface { typedef struct usbd_interface {
usb_slist_t list; usb_slist_t list;
/** Handler for USB Class specific commands*/ /** Handler for USB Class specific commands*/
usbd_request_handler class_handler; usbd_request_handler class_handler;
/** Handler for USB Vendor specific commands */ /** Handler for USB Vendor specific commands */
usbd_request_handler vendor_handler; usbd_request_handler vendor_handler;
/** Handler for USB custom specific commands */ /** Handler for USB custom specific commands */
usbd_request_handler custom_handler; usbd_request_handler custom_handler;
/** Handler for USB event notify commands */ /** Handler for USB event notify commands */
usbd_notify_handler notify_handler; usbd_notify_handler notify_handler;
uint8_t intf_num; uint8_t intf_num;
usb_slist_t ep_list; usb_slist_t ep_list;
} usbd_interface_t; } usbd_interface_t;
typedef struct usbd_class { typedef struct usbd_class {
usb_slist_t list; usb_slist_t list;
const char *name; const char *name;
usb_slist_t intf_list; usb_slist_t intf_list;
} usbd_class_t; } usbd_class_t;
void usbd_event_notify_handler(uint8_t event, void *arg); void usbd_event_notify_handler(uint8_t event, void *arg);
void usbd_desc_register(const uint8_t *desc); void usbd_desc_register(const uint8_t *desc);
void usbd_class_register(usbd_class_t *devclass); void usbd_class_register(usbd_class_t *devclass);
void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc); void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc);
void usbd_msosv2_desc_register(struct usb_msosv2_descriptor *desc); void usbd_msosv2_desc_register(struct usb_msosv2_descriptor *desc);
void usbd_bos_desc_register(struct usb_bos_descriptor *desc); void usbd_bos_desc_register(struct usb_bos_descriptor *desc);
void usbd_class_add_interface(usbd_class_t *devclass, usbd_interface_t *intf); void usbd_class_add_interface(usbd_class_t *devclass, usbd_interface_t *intf);
void usbd_interface_add_endpoint(usbd_interface_t *intf, usbd_endpoint_t *ep); void usbd_interface_add_endpoint(usbd_interface_t *intf, usbd_endpoint_t *ep);
bool usb_device_is_configured(void); bool usb_device_is_configured(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,128 +1,128 @@
/** /**
* @file usbh_core.h * @file usbh_core.h
* *
* Copyright (c) 2022 sakumisu * Copyright (c) 2022 sakumisu
* *
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The * this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (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 not use this file except in compliance with the
* License. You may obtain a copy of the License at * License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ */
#ifndef _USBH_CORE_H #ifndef _USBH_CORE_H
#define _USBH_CORE_H #define _USBH_CORE_H
#include "usb_util.h" #include "usb_util.h"
#include "usb_def.h" #include "usb_def.h"
#include "usb_hc.h" #include "usb_hc.h"
#include "usb_osal.h" #include "usb_osal.h"
#include "usb_workq.h" #include "usb_workq.h"
#include "usbh_hub.h" #include "usbh_hub.h"
#include "usb_config.h" #include "usb_config.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define USBH_ROOT_HUB_INDEX 1 /* roothub index*/ #define USBH_ROOT_HUB_INDEX 1 /* roothub index*/
#define USBH_EX_HUB_INDEX 2 /* external hub index */ #define USBH_EX_HUB_INDEX 2 /* external hub index */
#define USBH_HUB_PORT_START_INDEX 1 /* first hub port index */ #define USBH_HUB_PORT_START_INDEX 1 /* first hub port index */
#ifdef CONFIG_USBHOST_HUB #ifdef CONFIG_USBHOST_HUB
#define ROOTHUB(hport) ((hport)->parent == NULL) #define ROOTHUB(hport) ((hport)->parent == NULL)
#else #else
#define ROOTHUB(hport) true #define ROOTHUB(hport) true
#endif #endif
#define CLASS_CONNECT(hport,i) ((hport)->config.intf[i].class_driver->connect(hport, i)) #define CLASS_CONNECT(hport,i) ((hport)->config.intf[i].class_driver->connect(hport, i))
#define CLASS_DISCONNECT(hport,i) ((hport)->config.intf[i].class_driver->disconnect(hport, i)) #define CLASS_DISCONNECT(hport,i) ((hport)->config.intf[i].class_driver->disconnect(hport, i))
enum usbh_event_type { enum usbh_event_type {
USBH_EVENT_ATTACHED, USBH_EVENT_ATTACHED,
USBH_EVENT_REMOVED, USBH_EVENT_REMOVED,
}; };
struct usbh_class_info { struct usbh_class_info {
uint8_t class; /* Base device class code */ uint8_t class; /* Base device class code */
uint8_t subclass; /* Sub-class, depends on base class. Eg. */ uint8_t subclass; /* Sub-class, depends on base class. Eg. */
uint8_t protocol; /* Protocol, depends on base class. Eg. */ uint8_t protocol; /* Protocol, depends on base class. Eg. */
uint16_t vid; /* Vendor ID (for vendor/product specific devices) */ uint16_t vid; /* Vendor ID (for vendor/product specific devices) */
uint16_t pid; /* Product ID (for vendor/product specific devices) */ uint16_t pid; /* Product ID (for vendor/product specific devices) */
const struct usbh_class_driver *class_driver; const struct usbh_class_driver *class_driver;
}; };
struct usbh_hubport; struct usbh_hubport;
struct usbh_class_driver { struct usbh_class_driver {
const char *driver_name; const char *driver_name;
int (*connect)(struct usbh_hubport *hport, uint8_t intf); int (*connect)(struct usbh_hubport *hport, uint8_t intf);
int (*disconnect)(struct usbh_hubport *hport, uint8_t intf); int (*disconnect)(struct usbh_hubport *hport, uint8_t intf);
}; };
typedef struct usbh_endpoint { typedef struct usbh_endpoint {
struct usb_endpoint_descriptor ep_desc; struct usb_endpoint_descriptor ep_desc;
} usbh_endpoint_t; } usbh_endpoint_t;
typedef struct usbh_interface { typedef struct usbh_interface {
struct usb_interface_descriptor intf_desc; struct usb_interface_descriptor intf_desc;
struct usbh_endpoint ep[CONFIG_USBHOST_EP_NUM]; struct usbh_endpoint ep[CONFIG_USBHOST_EP_NUM];
struct usbh_class_driver *class_driver; struct usbh_class_driver *class_driver;
void *priv; void *priv;
} usbh_interface_t; } usbh_interface_t;
typedef struct usbh_configuration { typedef struct usbh_configuration {
struct usb_configuration_descriptor config_desc; struct usb_configuration_descriptor config_desc;
struct usbh_interface intf[CONFIG_USBHOST_INTF_NUM]; struct usbh_interface intf[CONFIG_USBHOST_INTF_NUM];
} usbh_configuration_t; } usbh_configuration_t;
typedef struct usbh_hubport { typedef struct usbh_hubport {
bool connected; /* True: device connected; false: disconnected */ bool connected; /* True: device connected; false: disconnected */
bool port_change; /* True: port changed; false: port do not change */ bool port_change; /* True: port changed; false: port do not change */
uint8_t port; /* Hub port index */ uint8_t port; /* Hub port index */
uint8_t dev_addr; /* device address */ uint8_t dev_addr; /* device address */
uint8_t speed; /* device speed */ uint8_t speed; /* device speed */
usbh_epinfo_t ep0; /* control ep info */ usbh_epinfo_t ep0; /* control ep info */
struct usb_device_descriptor device_desc; struct usb_device_descriptor device_desc;
struct usbh_configuration config; struct usbh_configuration config;
#if 0 #if 0
uint8_t* config_desc; uint8_t* config_desc;
#endif #endif
struct usbh_hub *parent; /*if NULL, is roothub*/ struct usbh_hub *parent; /*if NULL, is roothub*/
} usbh_hubport_t; } usbh_hubport_t;
typedef struct usbh_hub { typedef struct usbh_hub {
usb_slist_t list; usb_slist_t list;
uint8_t index; /* Hub index */ uint8_t index; /* Hub index */
uint8_t nports; /* Hub port number */ uint8_t nports; /* Hub port number */
uint8_t dev_addr; /* Hub device address */ uint8_t dev_addr; /* Hub device address */
usbh_epinfo_t intin; usbh_epinfo_t intin;
uint8_t *int_buffer; uint8_t *int_buffer;
struct usb_setup_packet *setup; struct usb_setup_packet *setup;
struct hub_port_status *port_status; struct hub_port_status *port_status;
struct usb_hub_descriptor hub_desc; struct usb_hub_descriptor hub_desc;
struct usbh_hubport child[CONFIG_USBHOST_EHPORTS]; struct usbh_hubport child[CONFIG_USBHOST_EHPORTS];
struct usbh_hubport *parent; /* Parent hub port */ struct usbh_hubport *parent; /* Parent hub port */
struct usb_work work; struct usb_work work;
} usbh_hub_t; } usbh_hub_t;
void usbh_event_notify_handler(uint8_t event, uint8_t rhport); void usbh_event_notify_handler(uint8_t event, uint8_t rhport);
int usbh_initialize(void); int usbh_initialize(void);
int lsusb(int argc, char **argv); int lsusb(int argc, char **argv);
struct usbh_hubport *usbh_get_hubport(uint8_t dev_addr); struct usbh_hubport *usbh_get_hubport(uint8_t dev_addr);
void *usbh_get_class(uint8_t dev_addr, uint8_t intf); void *usbh_get_class(uint8_t dev_addr, uint8_t intf);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif

View File

@@ -1,46 +1,46 @@
#ifndef _USB_CONFIG_H #ifndef _USB_CONFIG_H
#define _USB_CONFIG_H #define _USB_CONFIG_H
/* USB DEVICE Configuration */ /* USB DEVICE Configuration */
/* USB HOST Configuration */ /* USB HOST Configuration */
#ifndef CONFIG_USBHOST_RHPORTS #ifndef CONFIG_USBHOST_RHPORTS
#define CONFIG_USBHOST_RHPORTS 1 #define CONFIG_USBHOST_RHPORTS 1
#endif #endif
#ifndef CONFIG_USBHOST_EHPORTS #ifndef CONFIG_USBHOST_EHPORTS
#define CONFIG_USBHOST_EHPORTS 4 #define CONFIG_USBHOST_EHPORTS 4
#endif #endif
#ifndef CONFIG_USBHOST_INTF_NUM #ifndef CONFIG_USBHOST_INTF_NUM
#define CONFIG_USBHOST_INTF_NUM 6 #define CONFIG_USBHOST_INTF_NUM 6
#endif #endif
#ifndef CONFIG_USBHOST_EP_NUM #ifndef CONFIG_USBHOST_EP_NUM
#define CONFIG_USBHOST_EP_NUM 2 #define CONFIG_USBHOST_EP_NUM 2
#endif #endif
#ifndef CONFIG_USBHOST_HPWORKQ_PRIO #ifndef CONFIG_USBHOST_HPWORKQ_PRIO
#define CONFIG_USBHOST_HPWORKQ_PRIO 5 #define CONFIG_USBHOST_HPWORKQ_PRIO 5
#endif #endif
#ifndef CONFIG_USBHOST_HPWORKQ_STACKSIZE #ifndef CONFIG_USBHOST_HPWORKQ_STACKSIZE
#define CONFIG_USBHOST_HPWORKQ_STACKSIZE 2048 #define CONFIG_USBHOST_HPWORKQ_STACKSIZE 2048
#endif #endif
#ifndef CONFIG_USBHOST_LPWORKQ_PRIO #ifndef CONFIG_USBHOST_LPWORKQ_PRIO
#define CONFIG_USBHOST_LPWORKQ_PRIO 1 #define CONFIG_USBHOST_LPWORKQ_PRIO 1
#endif #endif
#ifndef CONFIG_USBHOST_LPWORKQ_STACKSIZE #ifndef CONFIG_USBHOST_LPWORKQ_STACKSIZE
#define CONFIG_USBHOST_LPWORKQ_STACKSIZE 2048 #define CONFIG_USBHOST_LPWORKQ_STACKSIZE 2048
#endif #endif
#ifndef CONFIG_USBHOST_PSC_PRIO #ifndef CONFIG_USBHOST_PSC_PRIO
#define CONFIG_USBHOST_PSC_PRIO 4 #define CONFIG_USBHOST_PSC_PRIO 4
#endif #endif
#ifndef CONFIG_USBHOST_PSC_STACKSIZE #ifndef CONFIG_USBHOST_PSC_STACKSIZE
#define CONFIG_USBHOST_PSC_STACKSIZE 2048 #define CONFIG_USBHOST_PSC_STACKSIZE 2048
#endif #endif
#define CONFIG_USBHOST_ASYNCH #define CONFIG_USBHOST_ASYNCH
#endif #endif