clean up USB API 2

This commit is contained in:
NIIBE Yutaka 2012-01-16 13:24:31 +09:00
parent 00541d7627
commit cbb2c6aab5
5 changed files with 48 additions and 33 deletions

View File

@ -1,17 +1,29 @@
2012-01-16 Niibe Yutaka <gniibe@fsij.org> 2012-01-16 Niibe Yutaka <gniibe@fsij.org>
Adopt new API. Adopt new USB API.
* src/usb_msc.c (usb_start_transmit): Use usb_lld_write.
(EP6_IN_Callback): Use usb_lld_tx_data_len and usb_lld_write.
(usb_start_receive): Use usb_lld_rx_enable.
(EP7_OUT_Callback): Use usb_lld_rx_data_len, usb_lld_rxcpy
and usb_lld_rx_enable
(msc_handle_command): Use usb_lld_stall_rx and usb_lld_stall_tx.
* src/usb_lld.h (usb_lld_stall_tx, usb_lld_stall_rx)
(usb_lld_tx_data_len): New.
* src/main.c (STDOUTthread): Use usb_lld_write.
* src/usb-icc.c (EP1_IN_Callback, icc_error, icc_power_on) * src/usb-icc.c (EP1_IN_Callback, icc_error, icc_power_on)
(icc_send_status, icc_send_data_block, icc_send_params): Use (icc_send_status, icc_send_data_block, icc_send_params): Use
usb_lld_write (was: USB_SIL_Write). usb_lld_write (was: USB_SIL_Write).
(EP2_OUT_Callback): Use usb_lld_get_data_len, usb_lld_rxcpy, (EP2_OUT_Callback): Use usb_lld_rx_data_len, usb_lld_rxcpy,
and usb_lld_rx_enable (was: USB_SIL_Read and SetEPRxValid). and usb_lld_rx_enable (was: USB_SIL_Read and SetEPRxValid).
(icc_prepare_receive): Use usb_lld_rx_enable. (icc_prepare_receive): Use usb_lld_rx_enable.
* src/stmusb.mk (STMUSBSRC): Dont' include usb_sil.c. * src/stmusb.mk (STMUSBSRC): Dont' include usb_sil.c.
* src/usb_lld.h (usb_lld_txcpy, usb_lld_tx_enable) * src/usb_lld.h (usb_lld_txcpy, usb_lld_tx_enable)
(usb_lld_write, usb_lld_rx_enable, usb_lld_get_data_len) (usb_lld_write, usb_lld_rx_enable, usb_lld_rx_data_len)
(usb_lld_rxcpy): New. (usb_lld_rxcpy): New.
* src/usb_prop.c (SetEPRxCount_allocated_size): Fix the * src/usb_prop.c (SetEPRxCount_allocated_size): Fix the

View File

@ -132,8 +132,7 @@ STDOUTthread (void *arg)
chEvtClear (EV_TX_READY); chEvtClear (EV_TX_READY);
USB_SIL_Write (EP3_IN, buffer_in, count_in); usb_lld_write (ENDP3, buffer_in, count_in);
SetEPTxValid (ENDP3);
chEvtWaitOne (EV_TX_READY); chEvtWaitOne (EV_TX_READY);
} }

View File

@ -1,7 +1,7 @@
/* /*
* usb-icc.c -- USB CCID/ICCD protocol handling * usb-icc.c -- USB CCID/ICCD protocol handling
* *
* Copyright (C) 2010, 2011 Free Software Initiative of Japan * Copyright (C) 2010, 2011, 2012 Free Software Initiative of Japan
* Author: NIIBE Yutaka <gniibe@fsij.org> * Author: NIIBE Yutaka <gniibe@fsij.org>
* *
* This file is a part of Gnuk, a GnuPG USB Token implementation. * This file is a part of Gnuk, a GnuPG USB Token implementation.
@ -172,7 +172,7 @@ EP2_OUT_Callback (void)
int data_len_so_far; int data_len_so_far;
int data_len; int data_len;
len = usb_lld_get_data_len (ENDP2); len = usb_lld_rx_data_len (ENDP2);
usb_lld_rxcpy (icc_next_p, ENDP2, 0, len); usb_lld_rxcpy (icc_next_p, ENDP2, 0, len);
if (len == 0) if (len == 0)
{ /* Just ignore Zero Length Packet (ZLP), if any */ { /* Just ignore Zero Length Packet (ZLP), if any */

View File

@ -1,6 +1,21 @@
#define STM32_USB_IRQ_PRIORITY 11 #define STM32_USB_IRQ_PRIORITY 11
void usb_lld_init (void); void usb_lld_init (void);
extern inline void usb_lld_stall_tx (int ep_num)
{
SetEPTxStatus (ep_num, EP_TX_STALL);
}
extern inline void usb_lld_stall_rx (int ep_num)
{
SetEPRxStatus (ep_num, EP_RX_STALL);
}
extern inline int usb_lld_tx_data_len (int ep_num)
{
return GetEPTxCount (ep_num);
}
extern inline void usb_lld_txcpy (const uint8_t *src, extern inline void usb_lld_txcpy (const uint8_t *src,
int ep_num, int offset, size_t len) int ep_num, int offset, size_t len)
{ {
@ -25,7 +40,7 @@ extern inline void usb_lld_rx_enable (int ep_num)
SetEPRxValid (ep_num); SetEPRxValid (ep_num);
} }
extern inline int usb_lld_get_data_len (int ep_num) extern inline int usb_lld_rx_data_len (int ep_num)
{ {
return GetEPRxCount (ep_num); return GetEPRxCount (ep_num);
} }

View File

@ -1,7 +1,7 @@
/* /*
* usb_msc.c -- USB Mass Storage Class protocol handling * usb_msc.c -- USB Mass Storage Class protocol handling
* *
* Copyright (C) 2011 Free Software Initiative of Japan * Copyright (C) 2011, 2012 Free Software Initiative of Japan
* Author: NIIBE Yutaka <gniibe@fsij.org> * Author: NIIBE Yutaka <gniibe@fsij.org>
* *
* This file is a part of Gnuk, a GnuPG USB Token implementation. * This file is a part of Gnuk, a GnuPG USB Token implementation.
@ -26,6 +26,7 @@
#include "config.h" #include "config.h"
#include "ch.h" #include "ch.h"
#include "gnuk.h" #include "gnuk.h"
#include "usb_lld.h"
#include "usb_msc.h" #include "usb_msc.h"
struct usb_endp_in { struct usb_endp_in {
@ -50,11 +51,6 @@ static Thread *the_thread;
static uint8_t msc_state; static uint8_t msc_state;
static void usb_stall_transmit (void)
{
SetEPTxStatus (ENDP6, EP_TX_STALL);
}
static void usb_start_transmit (const uint8_t *p, size_t n) static void usb_start_transmit (const uint8_t *p, size_t n)
{ {
size_t pkt_len = n > ENDP_MAX_SIZE ? ENDP_MAX_SIZE : n; size_t pkt_len = n > ENDP_MAX_SIZE ? ENDP_MAX_SIZE : n;
@ -63,14 +59,13 @@ static void usb_start_transmit (const uint8_t *p, size_t n)
ep6_in.txsize = n; ep6_in.txsize = n;
ep6_in.txcnt = 0; ep6_in.txcnt = 0;
USB_SIL_Write (EP6_IN, (uint8_t *)ep6_in.txbuf, pkt_len); usb_lld_write (ENDP6, (uint8_t *)ep6_in.txbuf, pkt_len);
SetEPTxValid (ENDP6);
} }
/* "Data Transmitted" callback */ /* "Data Transmitted" callback */
void EP6_IN_Callback (void) void EP6_IN_Callback (void)
{ {
size_t n = (size_t)GetEPTxCount (ENDP6); size_t n = (size_t)usb_lld_tx_data_len (ENDP6);
ep6_in.txbuf += n; ep6_in.txbuf += n;
ep6_in.txcnt += n; ep6_in.txcnt += n;
@ -82,8 +77,7 @@ void EP6_IN_Callback (void)
n = ENDP_MAX_SIZE; n = ENDP_MAX_SIZE;
else else
n = ep6_in.txsize; n = ep6_in.txsize;
USB_SIL_Write (EP6_IN, (uint8_t *)ep6_in.txbuf, n); usb_lld_write (ENDP6, (uint8_t *)ep6_in.txbuf, n);
SetEPTxValid (ENDP6);
return; return;
} }
@ -108,39 +102,34 @@ void EP6_IN_Callback (void)
} }
static void usb_stall_receive (void)
{
SetEPRxStatus (ENDP7, EP_RX_STALL);
}
static void usb_start_receive (uint8_t *p, size_t n) static void usb_start_receive (uint8_t *p, size_t n)
{ {
ep7_out.rxbuf = p; ep7_out.rxbuf = p;
ep7_out.rxsize = n; ep7_out.rxsize = n;
ep7_out.rxcnt = 0; ep7_out.rxcnt = 0;
SetEPRxValid (ENDP7); usb_lld_rx_enable (ENDP7);
} }
/* "Data Received" call back */ /* "Data Received" call back */
void EP7_OUT_Callback (void) void EP7_OUT_Callback (void)
{ {
size_t n = (size_t)GetEPRxCount (ENDP7); size_t n = (size_t)usb_lld_rx_data_len (ENDP7);
int err = 0; int err = 0;
if (n > ep7_out.rxsize) if (n > ep7_out.rxsize)
{ /* buffer overflow */ { /* buffer overflow */
err = 1; err = 1;
SetEPRxCount (ENDP7, ep7_out.rxsize); n = ep7_out.rxsize;
} }
n = USB_SIL_Read (EP7_OUT, ep7_out.rxbuf); usb_lld_rxcpy (ep7_out.rxbuf, ENDP7, 0, n);
ep7_out.rxbuf += n; ep7_out.rxbuf += n;
ep7_out.rxcnt += n; ep7_out.rxcnt += n;
ep7_out.rxsize -= n; ep7_out.rxsize -= n;
if (n == ENDP_MAX_SIZE && ep7_out.rxsize != 0) if (n == ENDP_MAX_SIZE && ep7_out.rxsize != 0)
{ /* More data to be received */ { /* More data to be received */
SetEPRxValid (ENDP7); usb_lld_rx_enable (ENDP7);
return; return;
} }
@ -329,7 +318,7 @@ void msc_handle_command (void)
if (msg != RDY_TIMEOUT) if (msg != RDY_TIMEOUT)
{ {
chSysLock (); chSysLock ();
usb_stall_receive (); usb_lld_stall_rx (ENDP7);
chSysUnlock (); chSysUnlock ();
} }
return; return;
@ -341,7 +330,7 @@ void msc_handle_command (void)
{ {
msc_state = MSC_ERROR; msc_state = MSC_ERROR;
chSysLock (); chSysLock ();
usb_stall_receive (); usb_lld_stall_rx (ENDP7);
chSysUnlock (); chSysUnlock ();
return; return;
} }
@ -444,8 +433,8 @@ void msc_handle_command (void)
{ {
msc_state = MSC_ERROR; msc_state = MSC_ERROR;
chSysLock (); chSysLock ();
usb_stall_transmit (); usb_lld_stall_tx (ENDP6);
usb_stall_receive (); usb_lld_stall_rx (ENDP7);
chSysUnlock (); chSysUnlock ();
return; return;
} }