From 907d8c7a8e90e1edff274a6537f61fd108a69e88 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Wed, 1 Jun 2016 07:52:56 +0900 Subject: [PATCH] Add ccid_notify_slot_change for the interrupt transfer --- ChangeLog | 6 ++++++ src/gnuk.h | 6 +++--- src/usb-ccid.c | 54 +++++++++++++++++++++++++++++++------------------- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8866f3d..76bd48f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2016-06-01 Niibe Yutaka + + * src/usb-ccid.c (ccid_notify_slot_change): New. + (ccid_thread): Call ccid_notify_slot_change at + interface_reset and EV_CARD_CHANGE. + 2016-05-31 NIIBE Yutaka * src/usb_stm32f103.c, src/stm32f103.h: Remove. diff --git a/src/gnuk.h b/src/gnuk.h index 251420e..401dbac 100644 --- a/src/gnuk.h +++ b/src/gnuk.h @@ -28,9 +28,9 @@ void ccid_usb_reset (int); #define EV_RX_DATA_READY 1 /* USB Rx data available */ #define EV_EXEC_FINISHED 2 /* OpenPGP Execution finished */ #define EV_TX_FINISHED 4 /* CCID Tx finished */ -#define EV_CARD_CHANGE 8 -#define EV_USB_INTERFACE 16 -#define EV_USB_RESET 32 +#define EV_CARD_CHANGE 8 +#define EV_USB_SET_INTERFACE 16 +#define EV_USB_DEVICE_RESET 32 /* OpenPGPcard thread */ #define EV_PINPAD_INPUT_DONE 1 diff --git a/src/usb-ccid.c b/src/usb-ccid.c index ceda005..163965d 100644 --- a/src/usb-ccid.c +++ b/src/usb-ccid.c @@ -1356,10 +1356,31 @@ ccid_card_change_signal (int how) } void -ccid_usb_reset (int all) +ccid_usb_reset (int full) { struct ccid *c = &ccid; - eventflag_signal (&c->ccid_comm, all?EV_USB_RESET:EV_USB_INTERFACE); + + eventflag_signal (&c->ccid_comm, + full ? EV_USB_DEVICE_RESET : EV_USB_SET_INTERFACE); +} + + +#define NOTIFY_SLOT_CHANGE 0x50 +static void +ccid_notify_slot_change (struct ccid *c) +{ + uint8_t msg; + uint8_t notification[2]; + + if (c->ccid_state == CCID_STATE_NOCARD) + msg = 0x02; + else + msg = 0x03; + + notification[0] = NOTIFY_SLOT_CHANGE; + notification[1] = msg; + usb_lld_write (ENDP2, notification, sizeof notification); + led_blink (LED_TWOSHOTS); } @@ -1367,7 +1388,6 @@ ccid_usb_reset (int all) #define GPG_THREAD_TERMINATED 0xffff -#define NOTIFY_SLOT_CHANGE 0x50 #define INTR_REQ_USB 20 @@ -1394,7 +1414,7 @@ ccid_thread (void *arg) chopstx_claim_irq (&interrupt, INTR_REQ_USB); usb_interrupt_handler (); /* For old SYS < 3.0 */ - reset: + device_reset: epi_init (epi, ENDP1, notify_tx, c); epo_init (epo, ENDP1, notify_icc, c); apdu_init (a); @@ -1413,9 +1433,10 @@ ccid_thread (void *arg) /* Ignore event while not-configured. */ } - interface: + interface_reset: timeout = USB_CCID_TIMEOUT; ccid_prepare_receive (c); + ccid_notify_slot_change (c); while (1) { eventflag_prepare_poll (&c->ccid_comm, &poll_desc); @@ -1429,7 +1450,7 @@ ccid_thread (void *arg) timeout = USB_CCID_TIMEOUT; m = eventflag_get (&c->ccid_comm); - if (m == EV_USB_RESET) + if (m == EV_USB_DEVICE_RESET) { if (c->application) { @@ -1437,23 +1458,18 @@ ccid_thread (void *arg) chopstx_join (c->application, NULL); c->application = 0; } - goto reset; + goto device_reset; } - else if (m == EV_USB_INTERFACE) - /* Upon receivable of SET_INTERFACE, we reset endpoint to RX_NAK. + else if (m == EV_USB_SET_INTERFACE) + /* Upon receival of SET_INTERFACE, the endpoint is reset to RX_NAK. * Thus, we need to prepare receive again. */ - goto interface; + goto interface_reset; else if (m == EV_CARD_CHANGE) { - uint8_t int_msg[2]; - - int_msg[0] = NOTIFY_SLOT_CHANGE; if (c->ccid_state == CCID_STATE_NOCARD) - { /* Inserted! */ - c->ccid_state = CCID_STATE_START; - int_msg[1] = 0x03; - } + /* Inserted! */ + c->ccid_state = CCID_STATE_START; else { /* Removed! */ if (c->application) @@ -1464,11 +1480,9 @@ ccid_thread (void *arg) } c->ccid_state = CCID_STATE_NOCARD; - int_msg[1] = 0x02; } - usb_lld_write (ENDP2, int_msg, sizeof int_msg); - led_blink (LED_TWOSHOTS); + ccid_notify_slot_change (c); } else if (m == EV_RX_DATA_READY) c->ccid_state = ccid_handle_data (c);