USB API major change

This commit is contained in:
NIIBE Yutaka
2016-06-08 12:04:05 +09:00
parent 78718e57df
commit 5f1c26ff17
7 changed files with 590 additions and 449 deletions

View File

@@ -1,3 +1,11 @@
2016-06-08 NIIBE Yutaka <gniibe@fsij.org>
* mcu/usb-stm32f103.c: Rewrite to be event driven API.
* example-cdc/usb-cdc.c: Update to new USB API.
* example-cdc/tty.h: Change tty API to be C string (char*)
friendly.
2016-06-02 Niibe Yutaka <gniibe@fsij.org> 2016-06-02 Niibe Yutaka <gniibe@fsij.org>
* contrib/adc-mkl27z.c: Move from mcu. * contrib/adc-mkl27z.c: Move from mcu.

9
NEWS
View File

@@ -1,6 +1,15 @@
NEWS - Noteworthy changes NEWS - Noteworthy changes
* Major changes in Chopstx 0.13
Released 2016-06-xx
** New USB API
Since USB driver is included, it should be good one. It used to be
interrupt based API with callbacks. Now, it's event driven API.
* Major changes in Chopstx 0.12 * Major changes in Chopstx 0.12
Released 2016-05-31 Released 2016-05-31

View File

@@ -117,7 +117,7 @@ main (int argc, const char *argv[])
m = 50; m = 50;
while (1) while (1)
{ {
uint8_t s[LINEBUFSIZE]; char s[LINEBUFSIZE];
u = 1; u = 1;
tty_wait_connection (tty); tty_wait_connection (tty);

View File

@@ -5,5 +5,5 @@ struct tty;
struct tty *tty_open (void); struct tty *tty_open (void);
void tty_wait_configured (struct tty *tty); void tty_wait_configured (struct tty *tty);
void tty_wait_connection (struct tty *tty); void tty_wait_connection (struct tty *tty);
int tty_send (struct tty *tty, uint8_t *buf, int count); int tty_send (struct tty *tty, const char *buf, int count);
int tty_recv (struct tty *tty, uint8_t *buf, uint32_t *timeout); int tty_recv (struct tty *tty, char *buf, uint32_t *timeout);

View File

@@ -30,7 +30,7 @@ static const struct line_coding line_coding0 = {
* *
* In that case, add argument to TTY_OPEN function and * In that case, add argument to TTY_OPEN function and
* modify TTY_GET function to get the TTY structure. Functions which * modify TTY_GET function to get the TTY structure. Functions which
* directy accesses TTY0 (usb_cb_device_reset and usb_cb_handle_event) * directy accesses TTY0 (usb_device_reset and usb_set_configuration)
* should be modified, too. * should be modified, too.
* *
* Modification of TTY_MAIN thread will be also needed to echo back * Modification of TTY_MAIN thread will be also needed to echo back
@@ -234,10 +234,10 @@ static const uint8_t vcom_string3[28] = {
#define NUM_INTERFACES 2 #define NUM_INTERFACES 2
void static void
usb_cb_device_reset (void) usb_device_reset (struct usb_dev *dev)
{ {
usb_lld_reset (VCOM_FEATURE_BUS_POWERED); usb_lld_reset (dev, VCOM_FEATURE_BUS_POWERED);
/* Initialize Endpoint 0 */ /* Initialize Endpoint 0 */
usb_lld_setup_endpoint (ENDP0, EP_CONTROL, 0, ENDP0_RXADDR, ENDP0_TXADDR, 64); usb_lld_setup_endpoint (ENDP0, EP_CONTROL, 0, ENDP0_RXADDR, ENDP0_TXADDR, 64);
@@ -256,13 +256,15 @@ usb_cb_device_reset (void)
#define CDC_CTRL_DTR 0x0001 #define CDC_CTRL_DTR 0x0001
void static void
usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, struct req_args *arg) usb_ctrl_write_finish (struct usb_dev *dev)
{ {
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT); struct device_req *arg = &dev->dev_req;
uint8_t type_rcp = arg->type & (REQUEST_TYPE|RECIPIENT);
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0 if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0
&& USB_SETUP_SET (req) && req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE) && USB_SETUP_SET (arg->type)
&& arg->request == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
{ {
struct tty *t = tty_get (arg->index, 0); struct tty *t = tty_get (arg->index, 0);
@@ -272,63 +274,74 @@ usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, struct req_args *arg)
chopstx_cond_signal (&t->cnd); chopstx_cond_signal (&t->cnd);
chopstx_mutex_unlock (&t->mtx); chopstx_mutex_unlock (&t->mtx);
} }
/*
* The transaction was already finished. So, it is no use to call
* usb_lld_ctrl_error when the condition does not match.
*/
} }
static int static int
vcom_port_data_setup (uint8_t req, uint8_t req_no, struct req_args *arg) vcom_port_data_setup (struct usb_dev *dev)
{ {
if (USB_SETUP_GET (req)) struct device_req *arg = &dev->dev_req;
if (USB_SETUP_GET (arg->type))
{ {
struct tty *t = tty_get (arg->index, 0); struct tty *t = tty_get (arg->index, 0);
if (req_no == USB_CDC_REQ_GET_LINE_CODING) if (arg->request == USB_CDC_REQ_GET_LINE_CODING)
return usb_lld_reply_request (&t->line_coding, return usb_lld_reply_request (dev, &t->line_coding,
sizeof (struct line_coding), arg); sizeof (struct line_coding));
} }
else /* USB_SETUP_SET (req) */ else /* USB_SETUP_SET (req) */
{ {
if (req_no == USB_CDC_REQ_SET_LINE_CODING if (arg->request == USB_CDC_REQ_SET_LINE_CODING
&& arg->len == sizeof (struct line_coding)) && arg->len == sizeof (struct line_coding))
{ {
struct tty *t = tty_get (arg->index, 0); struct tty *t = tty_get (arg->index, 0);
usb_lld_set_data_to_recv (&t->line_coding, return usb_lld_set_data_to_recv (dev, &t->line_coding,
sizeof (struct line_coding)); sizeof (struct line_coding));
return USB_SUCCESS;
} }
else if (req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE) else if (arg->request == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
return USB_SUCCESS; return 0;
} }
return USB_UNSUPPORT; return -1;
} }
int static int
usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg) usb_setup (struct usb_dev *dev)
{ {
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT); struct device_req *arg = &dev->dev_req;
uint8_t type_rcp = arg->type & (REQUEST_TYPE|RECIPIENT);
if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0) if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT) && arg->index == 0)
return vcom_port_data_setup (req, req_no, arg); return vcom_port_data_setup (dev);
return USB_UNSUPPORT; return -1;
} }
int static int
usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index, usb_get_descriptor (struct usb_dev *dev)
struct req_args *arg)
{ {
struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
uint8_t desc_type = (arg->value >> 8);
uint8_t desc_index = (arg->value & 0xff);
if (rcp != DEVICE_RECIPIENT) if (rcp != DEVICE_RECIPIENT)
return USB_UNSUPPORT; return -1;
if (desc_type == DEVICE_DESCRIPTOR) if (desc_type == DEVICE_DESCRIPTOR)
return usb_lld_reply_request (vcom_device_desc, sizeof (vcom_device_desc), return usb_lld_reply_request (dev,
arg); vcom_device_desc, sizeof (vcom_device_desc));
else if (desc_type == CONFIG_DESCRIPTOR) else if (desc_type == CONFIG_DESCRIPTOR)
return usb_lld_reply_request (vcom_config_desc, sizeof (vcom_config_desc), return usb_lld_reply_request (dev,
arg); vcom_config_desc, sizeof (vcom_config_desc));
else if (desc_type == STRING_DESCRIPTOR) else if (desc_type == STRING_DESCRIPTOR)
{ {
const uint8_t *str; const uint8_t *str;
@@ -353,13 +366,13 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
size = sizeof (vcom_string3); size = sizeof (vcom_string3);
break; break;
default: default:
return USB_UNSUPPORT; return -1;
} }
return usb_lld_reply_request (str, size, arg); return usb_lld_reply_request (dev, str, size);
} }
return USB_UNSUPPORT; return -1;
} }
static void static void
@@ -388,83 +401,85 @@ vcom_setup_endpoints_for_interface (uint16_t interface, int stop)
} }
} }
int static int
usb_cb_handle_event (uint8_t event_type, uint16_t value) usb_set_configuration (struct usb_dev *dev)
{ {
int i; int i;
uint8_t current_conf; uint8_t current_conf;
switch (event_type) current_conf = usb_lld_current_configuration (dev);
{
case USB_EVENT_ADDRESS:
chopstx_mutex_lock (&tty0.mtx);
tty0.device_state = ADDRESSED;
chopstx_mutex_unlock (&tty0.mtx);
return USB_SUCCESS;
case USB_EVENT_CONFIG:
current_conf = usb_lld_current_configuration ();
if (current_conf == 0) if (current_conf == 0)
{ {
if (value != 1) if (dev->dev_req.value != 1)
return USB_UNSUPPORT; return -1;
usb_lld_set_configuration (1); usb_lld_set_configuration (dev, 1);
for (i = 0; i < NUM_INTERFACES; i++) for (i = 0; i < NUM_INTERFACES; i++)
vcom_setup_endpoints_for_interface (i, 0); vcom_setup_endpoints_for_interface (i, 0);
chopstx_mutex_lock (&tty0.mtx); chopstx_mutex_lock (&tty0.mtx);
tty0.device_state = CONFIGURED; tty0.device_state = CONFIGURED;
chopstx_cond_signal (&tty0.cnd);
chopstx_mutex_unlock (&tty0.mtx); chopstx_mutex_unlock (&tty0.mtx);
} }
else if (current_conf != value) else if (current_conf != dev->dev_req.value)
{ {
if (value != 0) if (dev->dev_req.value != 0)
return USB_UNSUPPORT; return -1;
usb_lld_set_configuration (0); usb_lld_set_configuration (dev, 0);
for (i = 0; i < NUM_INTERFACES; i++) for (i = 0; i < NUM_INTERFACES; i++)
vcom_setup_endpoints_for_interface (i, 1); vcom_setup_endpoints_for_interface (i, 1);
chopstx_mutex_lock (&tty0.mtx); chopstx_mutex_lock (&tty0.mtx);
tty0.device_state = ADDRESSED; tty0.device_state = ADDRESSED;
chopstx_cond_signal (&tty0.cnd);
chopstx_mutex_unlock (&tty0.mtx); chopstx_mutex_unlock (&tty0.mtx);
} }
/* Do nothing when current_conf == value */
return USB_SUCCESS;
default:
break;
}
return USB_UNSUPPORT; return 0;
} }
int static int
usb_cb_interface (uint8_t cmd, struct req_args *arg) usb_set_interface (struct usb_dev *dev)
{ {
const uint8_t zero = 0; uint16_t interface = dev->dev_req.index;
uint16_t interface = arg->index; uint16_t alt = dev->dev_req.value;
uint16_t alt = arg->value;
if (interface >= NUM_INTERFACES) if (interface >= NUM_INTERFACES)
return USB_UNSUPPORT; return -1;
switch (cmd)
{
case USB_SET_INTERFACE:
if (alt != 0) if (alt != 0)
return USB_UNSUPPORT; return -1;
else else
{ {
vcom_setup_endpoints_for_interface (interface, 0); vcom_setup_endpoints_for_interface (interface, 0);
return USB_SUCCESS; return 0;
} }
}
case USB_GET_INTERFACE: static int
return usb_lld_reply_request (&zero, 1, arg); usb_get_interface (struct usb_dev *dev)
{
const uint8_t zero = 0;
uint16_t interface = dev->dev_req.index;
default: if (interface >= NUM_INTERFACES)
case USB_QUERY_INTERFACE: return -1;
return USB_SUCCESS;
} /* We don't have alternate interface, so, always return 0. */
return usb_lld_reply_request (dev, &zero, 1);
}
static int
usb_get_status_interface (struct usb_dev *dev)
{
const uint16_t status_info = 0;
uint16_t interface = dev->dev_req.index;
if (interface >= NUM_INTERFACES)
return -1;
return usb_lld_reply_request (dev, &status_info, 2);
} }
@@ -515,8 +530,8 @@ tty_echo_char (struct tty *t, int c)
} }
void static void
usb_cb_tx_done (uint8_t ep_num, uint32_t len) usb_tx_done (uint8_t ep_num, uint16_t len)
{ {
struct tty *t = tty_get (-1, ep_num); struct tty *t = tty_get (-1, ep_num);
@@ -598,19 +613,18 @@ tty_input_char (struct tty *t, int c)
return r; return r;
} }
void static void
usb_cb_rx_ready (uint8_t ep_num) usb_rx_ready (uint8_t ep_num, uint16_t len)
{ {
uint8_t recv_buf[64]; uint8_t recv_buf[64];
struct tty *t = tty_get (-1, ep_num); struct tty *t = tty_get (-1, ep_num);
if (ep_num == ENDP3) if (ep_num == ENDP3)
{ {
int i, r; int i;
r = usb_lld_rx_data_len (ENDP3); usb_lld_rxcpy (recv_buf, ep_num, 0, len);
usb_lld_rxcpy (recv_buf, ep_num, 0, r); for (i = 0; i < len; i++)
for (i = 0; i < r; i++)
if (tty_input_char (t, recv_buf[i])) if (tty_input_char (t, recv_buf[i]))
break; break;
@@ -652,6 +666,8 @@ static void *
tty_main (void *arg) tty_main (void *arg)
{ {
struct tty *t = arg; struct tty *t = arg;
struct usb_dev dev;
int e;
#if defined(OLDER_SYS_H) #if defined(OLDER_SYS_H)
/* /*
@@ -660,9 +676,9 @@ tty_main (void *arg)
* *
* When USB interrupt occurs between usb_lld_init (which assumes * When USB interrupt occurs between usb_lld_init (which assumes
* ISR) and chopstx_claim_irq (which clears pending interrupt), * ISR) and chopstx_claim_irq (which clears pending interrupt),
* invocation of usb_interrupt_handler won't occur. * invocation of usb_lld_event_handler won't occur.
* *
* Calling usb_interrupt_handler is no harm even if there were no * Calling usb_lld_event_handler is no harm even if there were no
* interrupts, thus, we call it unconditionally here, just in case * interrupts, thus, we call it unconditionally here, just in case
* if there is a request. * if there is a request.
* *
@@ -671,19 +687,117 @@ tty_main (void *arg)
* chopstx_claim_irq after usb_lld_init overrides that. * chopstx_claim_irq after usb_lld_init overrides that.
* *
*/ */
usb_lld_init (VCOM_FEATURE_BUS_POWERED); usb_lld_init (&dev, VCOM_FEATURE_BUS_POWERED);
chopstx_claim_irq (&usb_intr, INTR_REQ_USB); chopstx_claim_irq (&usb_intr, INTR_REQ_USB);
usb_interrupt_handler (); goto event_handle;
#else #else
chopstx_claim_irq (&usb_intr, INTR_REQ_USB); chopstx_claim_irq (&usb_intr, INTR_REQ_USB);
usb_lld_init (VCOM_FEATURE_BUS_POWERED); usb_lld_init (&dev, VCOM_FEATURE_BUS_POWERED);
#endif #endif
while (1) while (1)
{ {
chopstx_poll (NULL, 1, &usb_intr); chopstx_poll (NULL, 1, &usb_intr);
if (usb_intr.ready) if (usb_intr.ready)
usb_interrupt_handler (); {
uint8_t ep_num;
#if defined(OLDER_SYS_H)
event_handle:
#endif
/*
* When interrupt is detected, call usb_lld_event_handler.
* The event may be one of following:
* (1) Transfer to endpoint (bulk or interrupt)
* In this case EP_NUM is encoded in the variable E.
* (2) "NONE" event: some trasfer was done, but all was
* done by lower layer, no other work is needed in
* upper layer.
* (3) Device events: Reset or Suspend
* (4) Device requests to the endpoint zero.
*
*/
e = usb_lld_event_handler (&dev);
ep_num = USB_EVENT_ENDP (e);
if (ep_num != 0)
{
if (USB_EVENT_TXRX (e))
usb_tx_done (ep_num, USB_EVENT_LEN (e));
else
usb_rx_ready (ep_num, USB_EVENT_LEN (e));
}
else
switch (USB_EVENT_ID (e))
{
case USB_EVENT_DEVICE_RESET:
usb_device_reset (&dev);
continue;
case USB_EVENT_GET_DESCRIPTOR:
if (usb_get_descriptor (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
/* The addres is assigned to the device. We don't
* need to do anything for this actually, but in this
* application, we maintain the USB status of the
* device. Usually, just "continue" as EVENT_NONE is
* OK.
*/
case USB_EVENT_DEVICE_ADDRESSED:
chopstx_mutex_lock (&tty0.mtx);
tty0.device_state = ADDRESSED;
chopstx_cond_signal (&tty0.cnd);
chopstx_mutex_unlock (&tty0.mtx);
continue;
case USB_EVENT_SET_CONFIGURATION:
if (usb_set_configuration (&dev) < 0)
usb_lld_ctrl_error (&dev);
else
usb_lld_ctrl_good (&dev);
continue;
case USB_EVENT_SET_INTERFACE:
if (usb_set_interface (&dev) < 0)
usb_lld_ctrl_error (&dev);
else
usb_lld_ctrl_good (&dev);
continue;
/* Non standard device request. */
case USB_EVENT_CTRL_REQUEST:
if (usb_setup (&dev) < 0)
usb_lld_ctrl_error (&dev);
else
usb_lld_ctrl_good (&dev);
continue;
/* Control WRITE transfer finished. */
case USB_EVENT_CTRL_WRITE_FINISH:
usb_ctrl_write_finish (&dev);
continue;
case USB_EVENT_GET_STATUS_INTERFACE:
if (usb_get_status_interface (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
case USB_EVENT_GET_INTERFACE:
if (usb_get_interface (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
case USB_EVENT_NONE:
case USB_EVENT_SET_FEATURE_DEVICE:
case USB_EVENT_SET_FEATURE_ENDPOINT:
case USB_EVENT_CLEAR_FEATURE_DEVICE:
case USB_EVENT_CLEAR_FEATURE_ENDPOINT:
case USB_EVENT_DEVICE_SUSPEND:
default:
continue;
}
}
chopstx_mutex_lock (&t->mtx); chopstx_mutex_lock (&t->mtx);
if (t->device_state == CONFIGURED && t->flag_connected if (t->device_state == CONFIGURED && t->flag_connected
@@ -743,10 +857,10 @@ check_tx (struct tty *t)
} }
int int
tty_send (struct tty *t, uint8_t *buf, int len) tty_send (struct tty *t, const char *buf, int len)
{ {
int r; int r;
uint8_t *p; const char *p;
int count; int count;
p = buf; p = buf;
@@ -801,7 +915,7 @@ check_rx (void *arg)
* *
*/ */
int int
tty_recv (struct tty *t, uint8_t *buf, uint32_t *timeout) tty_recv (struct tty *t, char *buf, uint32_t *timeout)
{ {
int r; int r;
chopstx_poll_cond_t poll_desc; chopstx_poll_cond_t poll_desc;

View File

@@ -56,50 +56,20 @@ enum STANDARD_REQUESTS
enum CONTROL_STATE enum CONTROL_STATE
{ {
WAIT_SETUP, WAIT_SETUP,
SETTING_UP,
IN_DATA, IN_DATA,
OUT_DATA, OUT_DATA,
LAST_IN_DATA, LAST_IN_DATA,
WAIT_STATUS_IN, WAIT_STATUS_IN,
WAIT_STATUS_OUT, WAIT_STATUS_OUT,
STALLED, STALLED,
PAUSE
}; };
enum FEATURE_SELECTOR enum FEATURE_SELECTOR
{ {
ENDPOINT_STALL, FEATURE_ENDPOINT_HALT=0,
DEVICE_REMOTE_WAKEUP FEATURE_DEVICE_REMOTE_WAKEUP=1
}; };
struct DATA_INFO
{
uint8_t *addr;
uint16_t len;
uint8_t require_zlp;
};
struct DEVICE_INFO
{
uint8_t current_configuration;
uint8_t current_feature;
uint8_t state;
/**/
uint8_t bmRequestType;
uint8_t bRequest;
/**/
uint16_t value;
uint16_t index;
uint16_t len;
};
static struct DEVICE_INFO device_info;
static struct DATA_INFO data_info;
static struct DEVICE_INFO *const dev_p = &device_info;
static struct DATA_INFO *const data_p = &data_info;
#define REG_BASE (0x40005C00UL) /* USB_IP Peripheral Registers base address */ #define REG_BASE (0x40005C00UL) /* USB_IP Peripheral Registers base address */
#define PMA_ADDR (0x40006000UL) /* USB_IP Packet Memory Area base address */ #define PMA_ADDR (0x40006000UL) /* USB_IP Packet Memory Area base address */
@@ -181,7 +151,7 @@ static struct DATA_INFO *const data_p = &data_info;
#define EPRX_DTOG1 (0x1000) /* EndPoint RX Data TOGgle bit1 */ #define EPRX_DTOG1 (0x1000) /* EndPoint RX Data TOGgle bit1 */
#define EPRX_DTOG2 (0x2000) /* EndPoint RX Data TOGgle bit1 */ #define EPRX_DTOG2 (0x2000) /* EndPoint RX Data TOGgle bit1 */
static void usb_handle_transfer (uint16_t istr_value); static int usb_handle_transfer (struct usb_dev *dev, uint16_t istr_value);
static void st103_set_btable (void) static void st103_set_btable (void)
{ {
@@ -376,14 +346,31 @@ static void st103_ep_clear_dtog_tx (uint8_t ep_num)
} }
} }
void usb_lld_init (uint8_t feature) void
usb_lld_ctrl_error (struct usb_dev *dev)
{
dev->state = STALLED;
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_STALL);
}
void
usb_lld_ctrl_good (struct usb_dev *dev)
{
if (dev->dev_req.len == 0)
{
dev->state = WAIT_STATUS_IN;
st103_set_tx_count (ENDP0, 0);
st103_ep_set_rxtx_status (ENDP0, EP_RX_NAK, EP_TX_VALID);
}
}
void usb_lld_init (struct usb_dev *dev, uint8_t feature)
{ {
usb_lld_sys_init (); usb_lld_sys_init ();
dev_p->state = IN_DATA; dev->configuration = 0;
dev->feature = feature;
usb_lld_set_configuration (0); dev->state = WAIT_SETUP;
dev_p->current_feature = feature;
/* Reset USB */ /* Reset USB */
st103_set_cntr (CNTR_FRES); st103_set_cntr (CNTR_FRES);
@@ -406,15 +393,18 @@ void usb_lld_shutdown (void)
usb_lld_sys_shutdown (); usb_lld_sys_shutdown ();
} }
void #define USB_MAKE_EV(event) (event<<24)
usb_interrupt_handler (void) #define USB_MAKE_TXRX(ep_num,txrx,len) ((txrx? (1<<23):0)|(ep_num<<16)|len)
int
usb_lld_event_handler (struct usb_dev *dev)
{ {
uint16_t istr_value = st103_get_istr (); uint16_t istr_value = st103_get_istr ();
if ((istr_value & ISTR_RESET)) if ((istr_value & ISTR_RESET))
{ {
st103_set_istr (CLR_RESET); st103_set_istr (CLR_RESET);
usb_cb_device_reset (); return USB_MAKE_EV (USB_EVENT_DEVICE_RESET);
} }
else else
{ {
@@ -425,42 +415,45 @@ usb_interrupt_handler (void)
st103_set_istr (CLR_ERR); st103_set_istr (CLR_ERR);
if ((istr_value & ISTR_CTR)) if ((istr_value & ISTR_CTR))
usb_handle_transfer (istr_value); return usb_handle_transfer (dev, istr_value);
} }
return 0;
} }
static void handle_datastage_out (void) static void handle_datastage_out (struct usb_dev *dev)
{ {
if (data_p->addr && data_p->len) if (dev->ctrl_data.addr && dev->ctrl_data.len)
{ {
uint32_t len = st103_get_rx_count (ENDP0); uint32_t len = st103_get_rx_count (ENDP0);
if (len > data_p->len) if (len > dev->ctrl_data.len)
len = data_p->len; len = dev->ctrl_data.len;
usb_lld_from_pmabuf (data_p->addr, st103_get_rx_addr (ENDP0), len); usb_lld_from_pmabuf (dev->ctrl_data.addr, st103_get_rx_addr (ENDP0), len);
data_p->len -= len; dev->ctrl_data.len -= len;
data_p->addr += len; dev->ctrl_data.addr += len;
} }
if (data_p->len == 0) if (dev->ctrl_data.len == 0)
{ {
dev_p->state = WAIT_STATUS_IN; dev->state = WAIT_STATUS_IN;
st103_set_tx_count (ENDP0, 0); st103_set_tx_count (ENDP0, 0);
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_VALID); st103_ep_set_tx_status (ENDP0, EP_TX_VALID);
} }
else else
{ {
dev_p->state = OUT_DATA; dev->state = OUT_DATA;
st103_ep_set_rx_status (ENDP0, EP_RX_VALID); st103_ep_set_rx_status (ENDP0, EP_RX_VALID);
} }
} }
static void handle_datastage_in (void) static void handle_datastage_in (struct usb_dev *dev)
{ {
uint32_t len = USB_MAX_PACKET_SIZE;; uint32_t len = USB_MAX_PACKET_SIZE;;
struct ctrl_data *data_p = &dev->ctrl_data;
if ((data_p->len == 0) && (dev_p->state == LAST_IN_DATA)) if ((data_p->len == 0) && (dev->state == LAST_IN_DATA))
{ {
if (data_p->require_zlp) if (data_p->require_zlp)
{ {
@@ -468,19 +461,19 @@ static void handle_datastage_in (void)
/* No more data to send. Send empty packet */ /* No more data to send. Send empty packet */
st103_set_tx_count (ENDP0, 0); st103_set_tx_count (ENDP0, 0);
st103_ep_set_rxtx_status (ENDP0, EP_RX_VALID, EP_TX_VALID); st103_ep_set_tx_status (ENDP0, EP_TX_VALID);
} }
else else
{ {
/* No more data to send, proceed to receive OUT acknowledge.*/ /* No more data to send, proceed to receive OUT acknowledge. */
dev_p->state = WAIT_STATUS_OUT; dev->state = WAIT_STATUS_OUT;
st103_ep_set_rxtx_status (ENDP0, EP_RX_VALID, EP_TX_STALL); st103_ep_set_rx_status (ENDP0, EP_RX_VALID);
} }
return; return;
} }
dev_p->state = (data_p->len <= len) ? LAST_IN_DATA : IN_DATA; dev->state = (data_p->len <= len) ? LAST_IN_DATA : IN_DATA;
if (len > data_p->len) if (len > data_p->len)
len = data_p->len; len = data_p->len;
@@ -492,29 +485,30 @@ static void handle_datastage_in (void)
st103_ep_set_tx_status (ENDP0, EP_TX_VALID); st103_ep_set_tx_status (ENDP0, EP_TX_VALID);
} }
typedef int (*HANDLER) (uint8_t req, struct req_args *arg); typedef int (*HANDLER) (struct usb_dev *dev);
static int std_none (uint8_t req, struct req_args *arg) static int std_none (struct usb_dev *dev)
{ {
(void)req; (void)arg; (void)dev;
return USB_UNSUPPORT; return -1;
} }
static int std_get_status (uint8_t req, struct req_args *arg) static int std_get_status (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
uint8_t rcp = (arg->type & RECIPIENT);
uint16_t status_info = 0; uint16_t status_info = 0;
if (arg->value != 0 || arg->len != 2 || (arg->index >> 8) != 0 if (arg->value != 0 || arg->len != 2 || (arg->index >> 8) != 0
|| USB_SETUP_SET (req)) || USB_SETUP_SET (arg->type))
return USB_UNSUPPORT; return -1;
if (rcp == DEVICE_RECIPIENT) if (rcp == DEVICE_RECIPIENT)
{ {
if (arg->index == 0) if (arg->index == 0)
{ {
/* Get Device Status */ /* Get Device Status */
uint8_t feature = dev_p->current_feature; uint8_t feature = dev->feature;
/* Remote Wakeup enabled */ /* Remote Wakeup enabled */
if ((feature & (1 << 5))) if ((feature & (1 << 5)))
@@ -528,21 +522,15 @@ static int std_get_status (uint8_t req, struct req_args *arg)
else /* Self-powered */ else /* Self-powered */
status_info &= ~1; status_info &= ~1;
return usb_lld_reply_request (&status_info, 2, arg); return usb_lld_reply_request (dev, &status_info, 2);
} }
} }
else if (rcp == INTERFACE_RECIPIENT) else if (rcp == INTERFACE_RECIPIENT)
{ {
int r; if (dev->configuration == 0)
return -1;
if (dev_p->current_configuration == 0) return USB_EVENT_GET_STATUS_INTERFACE;
return USB_UNSUPPORT;
r = usb_cb_interface (USB_QUERY_INTERFACE, arg);
if (r != USB_SUCCESS)
return USB_UNSUPPORT;
return usb_lld_reply_request (&status_info, 2, arg);
} }
else if (rcp == ENDPOINT_RECIPIENT) else if (rcp == ENDPOINT_RECIPIENT)
{ {
@@ -550,13 +538,13 @@ static int std_get_status (uint8_t req, struct req_args *arg)
uint16_t status; uint16_t status;
if ((arg->index & 0x70) || endpoint == ENDP0) if ((arg->index & 0x70) || endpoint == ENDP0)
return USB_UNSUPPORT; return -1;
if ((arg->index & 0x80)) if ((arg->index & 0x80))
{ {
status = st103_ep_get_tx_status (endpoint); status = st103_ep_get_tx_status (endpoint);
if (status == 0) /* Disabled */ if (status == 0) /* Disabled */
return USB_UNSUPPORT; return -1;
else if (status == EP_TX_STALL) else if (status == EP_TX_STALL)
status_info |= 1; /* IN Endpoint stalled */ status_info |= 1; /* IN Endpoint stalled */
} }
@@ -564,33 +552,34 @@ static int std_get_status (uint8_t req, struct req_args *arg)
{ {
status = st103_ep_get_rx_status (endpoint); status = st103_ep_get_rx_status (endpoint);
if (status == 0) /* Disabled */ if (status == 0) /* Disabled */
return USB_UNSUPPORT; return -1;
else if (status == EP_RX_STALL) else if (status == EP_RX_STALL)
status_info |= 1; /* OUT Endpoint stalled */ status_info |= 1; /* OUT Endpoint stalled */
} }
return usb_lld_reply_request (&status_info, 2, arg); return usb_lld_reply_request (dev, &status_info, 2);
} }
return USB_UNSUPPORT; return -1;
} }
static int std_clear_feature (uint8_t req, struct req_args *arg) static int std_clear_feature (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
if (USB_SETUP_GET (req)) if (USB_SETUP_GET (arg->type))
return USB_UNSUPPORT; return -1;
if (rcp == DEVICE_RECIPIENT) if (rcp == DEVICE_RECIPIENT)
{ {
if (arg->len != 0 || arg->index != 0) if (arg->len != 0 || arg->index != 0)
return USB_UNSUPPORT; return -1;
if (arg->value == DEVICE_REMOTE_WAKEUP) if (arg->value == FEATURE_DEVICE_REMOTE_WAKEUP)
{ {
dev_p->current_feature &= ~(1 << 5); dev->feature &= ~(1 << 5);
return USB_SUCCESS; return USB_EVENT_CLEAR_FEATURE_DEVICE;
} }
} }
else if (rcp == ENDPOINT_RECIPIENT) else if (rcp == ENDPOINT_RECIPIENT)
@@ -598,50 +587,49 @@ static int std_clear_feature (uint8_t req, struct req_args *arg)
uint8_t endpoint = (arg->index & 0x0f); uint8_t endpoint = (arg->index & 0x0f);
uint16_t status; uint16_t status;
if (dev_p->current_configuration == 0) if (dev->configuration == 0)
return USB_UNSUPPORT; return -1;
if (arg->len != 0 || (arg->index >> 8) != 0 if (arg->len != 0 || (arg->index >> 8) != 0
|| arg->value != ENDPOINT_STALL || endpoint == ENDP0) || arg->value != FEATURE_ENDPOINT_HALT || endpoint == ENDP0)
return USB_UNSUPPORT; return -1;
if ((arg->index & 0x80)) if ((arg->index & 0x80))
status = st103_ep_get_tx_status (endpoint); status = st103_ep_get_tx_status (endpoint);
else else
status = st103_ep_get_rx_status (endpoint); status = st103_ep_get_rx_status (endpoint);
if (status == 0) /* Disabled */ if (status == 0) /* It's disabled endpoint. */
return USB_UNSUPPORT; return -1;
if (arg->index & 0x80) /* IN endpoint */ if (arg->index & 0x80) /* IN endpoint */
st103_ep_clear_dtog_tx (endpoint); st103_ep_clear_dtog_tx (endpoint);
else /* OUT endpoint */ else /* OUT endpoint */
st103_ep_clear_dtog_rx (endpoint); st103_ep_clear_dtog_rx (endpoint);
// event?? return USB_EVENT_CLEAR_FEATURE_ENDPOINT;
return USB_SUCCESS;
} }
return USB_UNSUPPORT; return -1;
} }
static int std_set_feature (uint8_t req, struct req_args *arg) static int std_set_feature (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
if (USB_SETUP_GET (req)) if (USB_SETUP_GET (arg->type))
return USB_UNSUPPORT; return -1;
if (rcp == DEVICE_RECIPIENT) if (rcp == DEVICE_RECIPIENT)
{ {
if (arg->len != 0 || arg->index != 0) if (arg->len != 0 || arg->index != 0)
return USB_UNSUPPORT; return -1;
if (arg->value == DEVICE_REMOTE_WAKEUP) if (arg->value == FEATURE_DEVICE_REMOTE_WAKEUP)
{ {
dev_p->current_feature |= 1 << 5; dev->feature |= 1 << 5;
// event?? return USB_EVENT_SET_FEATURE_DEVICE;
return USB_SUCCESS;
} }
} }
else if (rcp == ENDPOINT_RECIPIENT) else if (rcp == ENDPOINT_RECIPIENT)
@@ -649,148 +637,151 @@ static int std_set_feature (uint8_t req, struct req_args *arg)
uint8_t endpoint = (arg->index & 0x0f); uint8_t endpoint = (arg->index & 0x0f);
uint32_t status; uint32_t status;
if (dev_p->current_configuration == 0) if (dev->configuration == 0)
return USB_UNSUPPORT; return -1;
if (arg->len != 0 || (arg->index >> 8) != 0 if (arg->len != 0 || (arg->index >> 8) != 0
|| arg->value != 0 || endpoint == ENDP0) || arg->value != FEATURE_ENDPOINT_HALT || endpoint == ENDP0)
return USB_UNSUPPORT; return -1;
if ((arg->index & 0x80)) if ((arg->index & 0x80))
status = st103_ep_get_tx_status (endpoint); status = st103_ep_get_tx_status (endpoint);
else else
status = st103_ep_get_rx_status (endpoint); status = st103_ep_get_rx_status (endpoint);
if (status == 0) /* Disabled */ if (status == 0) /* It's disabled endpoint. */
return USB_UNSUPPORT; return -1;
if (arg->index & 0x80) if (arg->index & 0x80) /* IN endpoint */
/* IN endpoint */
st103_ep_set_tx_status (endpoint, EP_TX_STALL); st103_ep_set_tx_status (endpoint, EP_TX_STALL);
else else /* OUT endpoint */
/* OUT endpoint */
st103_ep_set_rx_status (endpoint, EP_RX_STALL); st103_ep_set_rx_status (endpoint, EP_RX_STALL);
// event?? return USB_EVENT_SET_FEATURE_ENDPOINT;
return USB_SUCCESS;
} }
return USB_UNSUPPORT; return -1;
} }
static int std_set_address (uint8_t req, struct req_args *arg) static int std_set_address (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
if (USB_SETUP_GET (req)) if (USB_SETUP_GET (arg->type))
return USB_UNSUPPORT; return -1;
if (rcp == DEVICE_RECIPIENT && arg->len == 0 && arg->value <= 127 if (rcp == DEVICE_RECIPIENT && arg->len == 0 && arg->value <= 127
&& arg->index == 0 && dev_p->current_configuration == 0) && arg->index == 0 && dev->configuration == 0)
return USB_SUCCESS; return USB_EVENT_NONE;
return USB_UNSUPPORT; return -1;
} }
static int std_get_descriptor (uint8_t req, struct req_args *arg) static int std_get_descriptor (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
if (USB_SETUP_SET (arg->type))
return -1;
if (USB_SETUP_SET (req)) return USB_EVENT_GET_DESCRIPTOR;
return USB_UNSUPPORT;
return usb_cb_get_descriptor (rcp, (arg->value >> 8),
(arg->value & 0xff), arg);
} }
static int std_get_configuration (uint8_t req, struct req_args *arg) static int std_get_configuration (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
if (USB_SETUP_SET (req)) if (USB_SETUP_SET (arg->type))
return USB_UNSUPPORT; return -1;
if (arg->value != 0 || arg->index != 0 || arg->len != 1)
return -1;
if (rcp == DEVICE_RECIPIENT) if (rcp == DEVICE_RECIPIENT)
return usb_lld_reply_request (&dev_p->current_configuration, 1, arg); return usb_lld_reply_request (dev, &dev->configuration, 1);
return USB_UNSUPPORT; return -1;
} }
static int std_set_configuration (uint8_t req, struct req_args *arg) static int std_set_configuration (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
if (USB_SETUP_GET (req)) if (USB_SETUP_GET (arg->type))
return USB_UNSUPPORT; return -1;
if (rcp == DEVICE_RECIPIENT && arg->index == 0 && arg->len == 0) if (arg->index != 0 || arg->len != 0)
return usb_cb_handle_event (USB_EVENT_CONFIG, arg->value); return -1;
return USB_UNSUPPORT; if (rcp == DEVICE_RECIPIENT)
return USB_EVENT_SET_CONFIGURATION;
return -1;
} }
static int std_get_interface (uint8_t req, struct req_args *arg) static int std_get_interface (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
if (USB_SETUP_SET (req)) if (USB_SETUP_SET (arg->type))
return USB_UNSUPPORT; return -1;
if (arg->value != 0 || (arg->index >> 8) != 0 || arg->len != 1)
return -1;
if (dev->configuration == 0)
return -1;
if (rcp == INTERFACE_RECIPIENT) if (rcp == INTERFACE_RECIPIENT)
{ return USB_EVENT_GET_INTERFACE;
if (arg->value != 0 || (arg->index >> 8) != 0 || arg->len != 1)
return USB_UNSUPPORT;
if (dev_p->current_configuration == 0) return -1;
return USB_UNSUPPORT;
return usb_cb_interface (USB_GET_INTERFACE, arg);
}
return USB_UNSUPPORT;
} }
static int std_set_interface (uint8_t req, struct req_args *arg) static int std_set_interface (struct usb_dev *dev)
{ {
uint8_t rcp = req & RECIPIENT; struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
if (USB_SETUP_GET (req) || rcp != INTERFACE_RECIPIENT if (USB_SETUP_GET (arg->type) || rcp != INTERFACE_RECIPIENT
|| arg->len != 0 || (arg->index >> 8) != 0 || arg->len != 0 || (arg->index >> 8) != 0
|| (arg->value >> 8) != 0 || dev_p->current_configuration == 0) || (arg->value >> 8) != 0 || dev->configuration == 0)
return USB_UNSUPPORT; return -1;
return usb_cb_interface (USB_SET_INTERFACE, arg); return USB_EVENT_SET_INTERFACE;
} }
static void handle_setup0 (void) static int handle_setup0 (struct usb_dev *dev)
{ {
const uint16_t *pw; const uint16_t *pw;
uint16_t w; uint16_t w;
uint8_t req_no; uint8_t req_no;
int r = USB_UNSUPPORT;
HANDLER handler; HANDLER handler;
pw = (uint16_t *)(PMA_ADDR + (uint8_t *)(st103_get_rx_addr (ENDP0) * 2)); pw = (uint16_t *)(PMA_ADDR + (uint8_t *)(st103_get_rx_addr (ENDP0) * 2));
w = *pw++; w = *pw++;
dev_p->bmRequestType = w & 0xff; dev->dev_req.type = (w & 0xff);
dev_p->bRequest = req_no = w >> 8; dev->dev_req.request = req_no = (w >> 8);
pw++; pw++;
dev_p->value = *pw++; dev->dev_req.value = *pw++;
pw++; pw++;
dev_p->index = *pw++; dev->dev_req.index = *pw++;
pw++; pw++;
dev_p->len = *pw; dev->dev_req.len = *pw;
data_p->addr = NULL; dev->ctrl_data.addr = NULL;
data_p->len = 0; dev->ctrl_data.len = 0;
data_p->require_zlp = 0; dev->ctrl_data.require_zlp = 0;
if ((dev_p->bmRequestType & REQUEST_TYPE) == STANDARD_REQUEST) if ((dev->dev_req.type & REQUEST_TYPE) == STANDARD_REQUEST)
{
if (req_no < TOTAL_REQUEST)
{ {
int r;
switch (req_no) switch (req_no)
{ {
case 0: handler = std_get_status; break; case 0: handler = std_get_status; break;
@@ -805,126 +796,131 @@ static void handle_setup0 (void)
default: handler = std_none; break; default: handler = std_none; break;
} }
r = (*handler) (dev_p->bmRequestType, if ((r = (*handler) (dev)) <= 0)
(struct req_args *)&dev_p->value); {
} /* Handling finished in standard way. */
if (r == 0)
usb_lld_ctrl_good (dev);
else
usb_lld_ctrl_error (dev);
return 0;
} }
else else
r = usb_cb_setup (dev_p->bmRequestType, req_no, return r;
(struct req_args *)&dev_p->value);
if (r != USB_SUCCESS)
dev_p->state = STALLED;
else
{
if (USB_SETUP_SET (dev_p->bmRequestType))
{
if (dev_p->len == 0)
{
dev_p->state = WAIT_STATUS_IN;
st103_set_tx_count (ENDP0, 0);
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_VALID);
} }
else else
{ return USB_EVENT_CTRL_REQUEST;
dev_p->state = OUT_DATA;
st103_ep_set_rx_status (ENDP0, EP_RX_VALID);
}
}
}
} }
static void handle_in0 (void) static int handle_in0 (struct usb_dev *dev)
{ {
if (dev_p->state == IN_DATA || dev_p->state == LAST_IN_DATA) int r = 0;
handle_datastage_in ();
else if (dev_p->state == WAIT_STATUS_IN) if (dev->state == IN_DATA || dev->state == LAST_IN_DATA)
handle_datastage_in (dev);
else if (dev->state == WAIT_STATUS_IN)
{ {
if ((dev_p->bRequest == SET_ADDRESS) && dev->state = WAIT_SETUP;
((dev_p->bmRequestType & (REQUEST_TYPE | RECIPIENT))
if ((dev->dev_req.request == SET_ADDRESS) &&
((dev->dev_req.type & (REQUEST_TYPE | RECIPIENT))
== (STANDARD_REQUEST | DEVICE_RECIPIENT))) == (STANDARD_REQUEST | DEVICE_RECIPIENT)))
{ {
st103_set_daddr (dev_p->value); st103_set_daddr (dev->dev_req.value);
usb_cb_handle_event (USB_EVENT_ADDRESS, dev_p->value); r = USB_EVENT_DEVICE_ADDRESSED;
} }
else else
usb_cb_ctrl_write_finish (dev_p->bmRequestType, dev_p->bRequest, r = USB_EVENT_CTRL_WRITE_FINISH;
(struct req_args *)&dev_p->value);
dev_p->state = STALLED;
} }
else else
dev_p->state = STALLED;
}
static void handle_out0 (void)
{
if (dev_p->state == IN_DATA || dev_p->state == LAST_IN_DATA)
/* host aborts the transfer before finish */
dev_p->state = STALLED;
else if (dev_p->state == OUT_DATA)
handle_datastage_out ();
else if (dev_p->state == WAIT_STATUS_OUT)
dev_p->state = STALLED;
/* Unexpect state, STALL the endpoint */
else
dev_p->state = STALLED;
}
static void
usb_handle_transfer (uint16_t istr_value)
{
uint16_t ep_value = 0;
uint8_t ep_index;
ep_index = (istr_value & ISTR_EP_ID);
/* Decode and service non control endpoints interrupt */
/* process related endpoint register */
ep_value = st103_get_epreg (ep_index);
if (ep_index == 0)
{ {
if ((ep_value & EP_CTR_TX)) dev->state = STALLED;
{
st103_ep_clear_ctr_tx (ep_index);
handle_in0 ();
}
if ((ep_value & EP_CTR_RX))
{
st103_ep_clear_ctr_rx (ep_index);
if ((ep_value & EP_SETUP))
handle_setup0 ();
else
handle_out0 ();
}
if (dev_p->state == STALLED)
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_STALL); st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_STALL);
} }
return r;
}
static void handle_out0 (struct usb_dev *dev)
{
if (dev->state == OUT_DATA)
/* Usual case. */
handle_datastage_out (dev);
else if (dev->state == WAIT_STATUS_OUT)
/*
* Control READ transfer finished by ZLP.
* Leave ENDP0 status RX_NAK, TX_NAK.
*/
dev->state = WAIT_SETUP;
else else
{ {
/*
* dev->state == IN_DATA || dev->state == LAST_IN_DATA
* (Host aborts the transfer before finish)
* Or else, unexpected state.
* STALL the endpoint, until we receive the next SETUP token.
*/
dev->state = STALLED;
st103_ep_set_rxtx_status (ENDP0, EP_RX_STALL, EP_TX_STALL);
}
}
static int
usb_handle_transfer (struct usb_dev *dev, uint16_t istr_value)
{
uint16_t ep_value = 0;
uint8_t ep_num = (istr_value & ISTR_EP_ID);
ep_value = st103_get_epreg (ep_num);
if (ep_num == 0)
{
if ((ep_value & EP_CTR_TX))
{
st103_ep_clear_ctr_tx (ep_num);
return USB_MAKE_EV (handle_in0 (dev));
}
if ((ep_value & EP_CTR_RX)) if ((ep_value & EP_CTR_RX))
{ {
st103_ep_clear_ctr_rx (ep_index); st103_ep_clear_ctr_rx (ep_num);
usb_cb_rx_ready (ep_index);
if ((ep_value & EP_SETUP))
return USB_MAKE_EV (handle_setup0 (dev));
else
{
handle_out0 (dev);
return 0;
}
}
}
else
{
uint16_t len;
if ((ep_value & EP_CTR_RX))
{
len = st103_get_rx_count (ep_num);
st103_ep_clear_ctr_rx (ep_num);
return USB_MAKE_TXRX (ep_num, 0, len);
} }
if ((ep_value & EP_CTR_TX)) if ((ep_value & EP_CTR_TX))
{ {
uint32_t len = st103_get_tx_count (ep_index); len = st103_get_tx_count (ep_num);
st103_ep_clear_ctr_tx (ep_num);
return USB_MAKE_TXRX (ep_num, 1, len);
}
}
st103_ep_clear_ctr_tx (ep_index); return 0;
usb_cb_tx_done (ep_index, len);
}
}
} }
void usb_lld_reset (uint8_t feature) void usb_lld_reset (struct usb_dev *dev, uint8_t feature)
{ {
usb_lld_set_configuration (0); usb_lld_set_configuration (dev, 0);
dev_p->current_feature = feature; dev->feature = feature;
st103_set_btable (); st103_set_btable ();
st103_set_daddr (0); st103_set_daddr (0);
} }
@@ -954,16 +950,6 @@ void usb_lld_tx_enable (int ep_num, size_t len)
st103_ep_set_tx_status (ep_num, EP_TX_VALID); st103_ep_set_tx_status (ep_num, EP_TX_VALID);
} }
int usb_lld_tx_data_len (int ep_num)
{
return st103_get_tx_count (ep_num);
}
int usb_lld_rx_data_len (int ep_num)
{
return st103_get_rx_count (ep_num);
}
void usb_lld_stall_tx (int ep_num) void usb_lld_stall_tx (int ep_num)
{ {
st103_ep_set_tx_status (ep_num, EP_TX_STALL); st103_ep_set_tx_status (ep_num, EP_TX_STALL);
@@ -1021,20 +1007,22 @@ void usb_lld_setup_endpoint (int ep_num, int ep_type, int ep_kind,
st103_set_epreg (ep_num, epreg_value); st103_set_epreg (ep_num, epreg_value);
} }
void usb_lld_set_configuration (uint8_t config) void usb_lld_set_configuration (struct usb_dev *dev, uint8_t config)
{ {
dev_p->current_configuration = config; dev->configuration = config;
} }
uint8_t usb_lld_current_configuration (void) uint8_t usb_lld_current_configuration (struct usb_dev *dev)
{ {
return dev_p->current_configuration; return dev->configuration;
} }
void usb_lld_set_data_to_recv (void *p, size_t len) int usb_lld_set_data_to_recv (struct usb_dev *dev, void *p, size_t len)
{ {
struct ctrl_data *data_p = &dev->ctrl_data;
data_p->addr = p; data_p->addr = p;
data_p->len = len; data_p->len = len;
return 0;
} }
void usb_lld_to_pmabuf (const void *src, uint16_t addr, size_t n) void usb_lld_to_pmabuf (const void *src, uint16_t addr, size_t n)
@@ -1118,9 +1106,10 @@ void usb_lld_from_pmabuf (void *dst, uint16_t addr, size_t n)
* BUFLEN: size of the data. * BUFLEN: size of the data.
*/ */
int int
usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *ctl) usb_lld_reply_request (struct usb_dev *dev, const void *buf, size_t buflen)
{ {
uint32_t len_asked = ctl->len; struct ctrl_data *data_p = &dev->ctrl_data;
uint32_t len_asked = dev->dev_req.len;
uint32_t len; uint32_t len;
data_p->addr = (void *)buf; data_p->addr = (void *)buf;
@@ -1136,12 +1125,12 @@ usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *ctl)
if (data_p->len < USB_MAX_PACKET_SIZE) if (data_p->len < USB_MAX_PACKET_SIZE)
{ {
len = data_p->len; len = data_p->len;
dev_p->state = LAST_IN_DATA; dev->state = LAST_IN_DATA;
} }
else else
{ {
len = USB_MAX_PACKET_SIZE; len = USB_MAX_PACKET_SIZE;
dev_p->state = IN_DATA; dev->state = IN_DATA;
} }
if (len) if (len)
@@ -1152,6 +1141,6 @@ usb_lld_reply_request (const void *buf, size_t buflen, struct req_args *ctl)
} }
st103_set_tx_count (ENDP0, len); st103_set_tx_count (ENDP0, len);
st103_ep_set_tx_status (ENDP0, EP_TX_VALID); st103_ep_set_rxtx_status (ENDP0, EP_RX_NAK, EP_TX_VALID);
return USB_SUCCESS; return USB_EVENT_NONE;
} }

View File

@@ -37,45 +37,52 @@ enum DESCRIPTOR_TYPE
#define USB_SETUP_SET(req) ((req & REQUEST_DIR) == 0) #define USB_SETUP_SET(req) ((req & REQUEST_DIR) == 0)
#define USB_SETUP_GET(req) ((req & REQUEST_DIR) != 0) #define USB_SETUP_GET(req) ((req & REQUEST_DIR) != 0)
enum struct device_req {
{ uint8_t type;
USB_UNSUPPORT = 0, uint8_t request;
USB_SUCCESS = 1,
};
struct req_args {
uint16_t value; uint16_t value;
uint16_t index; uint16_t index;
uint16_t len; uint16_t len;
}; };
void usb_cb_device_reset (void); struct ctrl_data {
int usb_cb_setup (uint8_t req, uint8_t req_no, struct req_args *arg); uint8_t *addr;
int usb_cb_interface (uint8_t cmd, struct req_args *arg); uint16_t len;
int usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index, uint8_t require_zlp;
struct req_args *arg); };
int usb_cb_handle_event (uint8_t event_type, uint16_t value);
void usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no,
struct req_args *arg);
void usb_cb_tx_done (uint8_t ep_num, uint32_t len);
void usb_cb_rx_ready (uint8_t ep_num);
enum { struct usb_dev {
USB_EVENT_ADDRESS, uint8_t configuration;
USB_EVENT_CONFIG, uint8_t feature;
USB_EVENT_SUSPEND, uint8_t state;
USB_EVENT_WAKEUP, struct device_req dev_req;
USB_EVENT_STALL, struct ctrl_data ctrl_data;
}; };
enum { enum {
USB_SET_INTERFACE, USB_EVENT_NONE=0, /* Processed in lower layer. */
USB_GET_INTERFACE, /* Device reset and suspend. */
USB_QUERY_INTERFACE, USB_EVENT_DEVICE_RESET,
USB_EVENT_DEVICE_SUSPEND,
/* Device Requests (Control WRITE Transfer): Standard */
USB_EVENT_SET_CONFIGURATION,
USB_EVENT_SET_INTERFACE,
USB_EVENT_SET_FEATURE_DEVICE,
USB_EVENT_SET_FEATURE_ENDPOINT,
USB_EVENT_CLEAR_FEATURE_DEVICE,
USB_EVENT_CLEAR_FEATURE_ENDPOINT,
/* Device Requests (Control READ Transfer): Standard */
USB_EVENT_GET_STATUS_INTERFACE,
USB_EVENT_GET_DESCRIPTOR,
USB_EVENT_GET_INTERFACE,
/* Device Requests (Control READ/WRITE Transfer): Non-Standard */
USB_EVENT_CTRL_REQUEST,
USB_EVENT_CTRL_WRITE_FINISH,
/* Device addressed. */
USB_EVENT_DEVICE_ADDRESSED,
}; };
enum DEVICE_STATE enum DEVICE_STATE {
{
UNCONNECTED, UNCONNECTED,
ATTACHED, ATTACHED,
POWERED, POWERED,
@@ -84,17 +91,32 @@ enum DEVICE_STATE
CONFIGURED CONFIGURED
}; };
void usb_lld_init (uint8_t feature); void usb_lld_init (struct usb_dev *dev, uint8_t feature);
int usb_lld_reply_request (const void *buf, size_t buflen,
struct req_args *arg); /*
int usb_lld_rx_data_len (int ep_num); * Return value is encoded integer:
void usb_lld_reset (uint8_t feature); * event-no: 8-bit, 0 if TX/RX
void usb_lld_set_configuration (uint8_t config); * tx/rx-flag: 1-bit, 0 if rx, 1 if tx
uint8_t usb_lld_current_configuration (void); * endpoint no: 7-bit
* length: 16-bit
*/
#define USB_EVENT_TXRX(e) ((e >> 23) & 1)
#define USB_EVENT_LEN(e) (e & 0xffff)
#define USB_EVENT_ENDP(e) ((e >> 16) & 0x7f)
#define USB_EVENT_ID(e) ((e >> 24))
int usb_lld_event_handler (struct usb_dev *dev);
int usb_lld_reply_request (struct usb_dev *dev,
const void *buf, size_t buflen);
int usb_lld_set_data_to_recv (struct usb_dev *dev, void *p, size_t len);
void usb_lld_ctrl_error (struct usb_dev *dev);
void usb_lld_ctrl_good (struct usb_dev *dev);
void usb_lld_reset (struct usb_dev *dev, uint8_t feature);
void usb_lld_set_configuration (struct usb_dev *dev, uint8_t config);
uint8_t usb_lld_current_configuration (struct usb_dev *dev);
void usb_lld_prepare_shutdown (void); void usb_lld_prepare_shutdown (void);
void usb_lld_shutdown (void); void usb_lld_shutdown (void);
void usb_interrupt_handler (void);
void usb_lld_set_data_to_recv (void *p, size_t len);
#ifdef MCU_KINETIS_L #ifdef MCU_KINETIS_L
void usb_lld_tx_enable_buf (int ep_num, const void *buf, size_t len); void usb_lld_tx_enable_buf (int ep_num, const void *buf, size_t len);
@@ -118,10 +140,9 @@ void usb_lld_setup_endpoint (int ep_num, int ep_type, int ep_kind,
void usb_lld_stall_tx (int ep_num); void usb_lld_stall_tx (int ep_num);
void usb_lld_stall_rx (int ep_num); void usb_lld_stall_rx (int ep_num);
int usb_lld_tx_data_len (int ep_num);
void usb_lld_txcpy (const void *src, int ep_num, int offset, size_t len); void usb_lld_txcpy (const void *src, int ep_num, int offset, size_t len);
void usb_lld_write (uint8_t ep_num, const void *buf, size_t len); void usb_lld_write (uint8_t ep_num, const void *buf, size_t len);
void usb_lld_rxcpy (uint8_t *dst, int ep_num, int offset, size_t len);
void usb_lld_to_pmabuf (const void *src, uint16_t addr, size_t n); void usb_lld_to_pmabuf (const void *src, uint16_t addr, size_t n);
void usb_lld_from_pmabuf (void *dst, uint16_t addr, size_t n); void usb_lld_from_pmabuf (void *dst, uint16_t addr, size_t n);
void usb_lld_rxcpy (uint8_t *dst, int ep_num, int offset, size_t len);
#endif #endif