Merge branch 'hid'

This commit is contained in:
NIIBE Yutaka
2013-11-26 15:15:45 +09:00
5 changed files with 313 additions and 52 deletions

View File

@@ -1,3 +1,26 @@
2013-11-26 Niibe Yutaka <gniibe@fsij.org>
* src/usb_desc.c (hid_report_desc): New.
(ICC_TOTAL_LENGTH): Update.
(HID_TOTAL_LENGTH, HID_NUM_INTERFACES): New.
(W_TOTAL_LENGTH, NUM_INTERFACES): Update.
(gnukConfigDescriptor): Add IN2 interrupt endpoint descriptor.
Add HID interface descriptor.
(usb_cb_get_descriptor): Handle HID.
* src/usb_ctrl.c (NUM_INTERFACES, MSC_INTERFACE_NO): Add 1.
(USB_HID_REQ_*, HID_LED_STATUS_NUMLOCK): New.
(gnuk_setup_endpoints_for_interface): Add ENDP2 interrupt
endpoint.
(usb_cb_setup): Handle HID requests.
(usb_cb_ctrl_write_finish): Likewise.
* src/usb-icc.c (ccid_card_change_signal): New.
(ccid_thread): Handle card change.
(icc_error, icc_send_status): Handle ICC_STATE_NOCARD state.
(icc_handle_data): Add the case of ICC_STATE_NOCARD.
(EP2_IN_Callback): New.
2013-11-26 Niibe Yutaka <gniibe@fsij.org>
* src/pin-dial.c: Remove.

View File

@@ -17,6 +17,7 @@ struct apdu {
};
extern struct apdu apdu;
void ccid_card_change_signal (void);
/* CCID thread */
#define EV_RX_DATA_READY (1) /* USB Rx data available */

View File

@@ -722,12 +722,12 @@ static void icc_error (struct ccid *c, int offset)
icc_reply[4] = 0x00;
icc_reply[5] = 0x00; /* Slot */
icc_reply[ICC_MSG_SEQ_OFFSET] = c->icc_header.seq;
if (c->icc_state == ICC_STATE_START)
/* 1: ICC present but not activated 2: No ICC present */
icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
if (c->icc_state == ICC_STATE_NOCARD)
icc_reply[ICC_MSG_STATUS_OFFSET] = 2; /* 2: No ICC present */
else if (c->icc_state == ICC_STATE_START)
icc_reply[ICC_MSG_STATUS_OFFSET] = 1; /* 1: ICC present but not activated */
else
/* An ICC is present and active */
icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
icc_reply[ICC_MSG_STATUS_OFFSET] = 0; /* An ICC is present and active */
icc_reply[ICC_MSG_STATUS_OFFSET] |= ICC_CMD_STATUS_ERROR; /* Failed */
icc_reply[ICC_MSG_ERROR_OFFSET] = offset;
icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
@@ -793,12 +793,12 @@ icc_send_status (struct ccid *c)
icc_reply[4] = 0x00;
icc_reply[5] = 0x00; /* Slot */
icc_reply[ICC_MSG_SEQ_OFFSET] = c->icc_header.seq;
if (c->icc_state == ICC_STATE_START)
/* 1: ICC present but not activated 2: No ICC present */
icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
if (c->icc_state == ICC_STATE_NOCARD)
icc_reply[ICC_MSG_STATUS_OFFSET] = 2; /* 2: No ICC present */
else if (c->icc_state == ICC_STATE_START)
icc_reply[ICC_MSG_STATUS_OFFSET] = 1; /* 1: ICC present but not activated */
else
/* An ICC is present and active */
icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
icc_reply[ICC_MSG_STATUS_OFFSET] = 0; /* An ICC is present and active */
icc_reply[ICC_MSG_ERROR_OFFSET] = 0x00;
icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
@@ -1075,6 +1075,15 @@ icc_handle_data (struct ccid *c)
switch (c->icc_state)
{
case ICC_STATE_NOCARD:
if (c->icc_header.msg_type == ICC_SLOT_STATUS)
icc_send_status (c);
else
{
DEBUG_INFO ("ERR00\r\n");
icc_error (c, ICC_OFFSET_CMD_NOT_SUPPORTED);
}
break;
case ICC_STATE_START:
if (c->icc_header.msg_type == ICC_POWER_ON)
{
@@ -1288,9 +1297,17 @@ icc_handle_timeout (struct ccid *c)
return next_state;
}
#define USB_ICC_TIMEOUT (1950*1000)
/*
* Another Tx done callback
*/
void
EP2_IN_Callback (void)
{
}
#define USB_ICC_TIMEOUT (1950*1000)
static struct ccid ccid;
#define GPG_THREAD_TERMINATED 0xffff
@@ -1307,6 +1324,15 @@ USBthread (void *arg)
return ccid_thread (thd);
}
void
ccid_card_change_signal (void)
{
struct ccid *c = &ccid;
eventflag_signal (&c->ccid_comm, EV_CARD_CHANGE);
}
static void * __attribute__ ((noinline))
ccid_thread (chopstx_t thd)
{
@@ -1314,6 +1340,7 @@ ccid_thread (chopstx_t thd)
struct ep_out *epo = &endpoint_out;
struct ccid *c = &ccid;
struct apdu *a = &apdu;
int card_change_requested = 0;
epi_init (epi, ENDP1, notify_tx, c);
epo_init (epo, ENDP1, notify_icc, c);
@@ -1327,7 +1354,38 @@ ccid_thread (chopstx_t thd)
m = eventflag_wait_timeout (&c->ccid_comm, USB_ICC_TIMEOUT);
if (m == EV_RX_DATA_READY)
if (m == EV_CARD_CHANGE)
{
if (card_change_requested)
{
uint8_t notify_slot_change[2] = { 0x50, 0x02 };
led_blink (LED_TWOSHOTS);
if (c->icc_state == ICC_STATE_NOCARD)
{ /* Inserted! */
c->icc_state = ICC_STATE_START;
notify_slot_change[1] |= 1;
}
else
{
if (c->application)
{
eventflag_signal (&c->openpgp_comm, EV_EXIT);
chopstx_join (c->application, NULL);
c->application = 0;
}
c->icc_state = ICC_STATE_NOCARD;
}
card_change_requested = 0;
usb_lld_write (ENDP2, notify_slot_change, 2);
}
else
card_change_requested = 1;
}
else if (m == EV_RX_DATA_READY)
c->icc_state = icc_handle_data (c);
else if (m == EV_EXEC_FINISHED)
if (c->icc_state == ICC_STATE_EXECUTE)
@@ -1383,7 +1441,10 @@ ccid_thread (chopstx_t thd)
icc_prepare_receive (c);
}
else /* Timeout */
c->icc_state = icc_handle_timeout (c);
{
c->icc_state = icc_handle_timeout (c);
card_change_requested = 0;
}
}
if (c->application)

View File

@@ -116,34 +116,58 @@ vcom_port_data_setup (uint8_t req, uint8_t req_no, uint16_t value)
#define MSC_NUM_INTERFACES 0
#endif
#define NUM_INTERFACES (1+VCOM_NUM_INTERFACES+MSC_NUM_INTERFACES)
#define MSC_INTERFACE_NO (1+VCOM_NUM_INTERFACES)
#define NUM_INTERFACES (2+VCOM_NUM_INTERFACES+MSC_NUM_INTERFACES)
#define MSC_INTERFACE_NO (2+VCOM_NUM_INTERFACES)
uint32_t bDeviceState = UNCONNECTED; /* USB device status */
#define USB_HID_REQ_GET_REPORT 1
#define USB_HID_REQ_GET_IDLE 2
#define USB_HID_REQ_GET_PROTOCOL 3
#define USB_HID_REQ_SET_REPORT 9
#define USB_HID_REQ_SET_IDLE 10
#define USB_HID_REQ_SET_PROTOCOL 11
#define HID_LED_STATUS_NUMLOCK 0x01
static uint8_t hid_idle_rate; /* in 4ms */
static uint8_t hid_report_saved;
static uint16_t hid_report;
static void
gnuk_setup_endpoints_for_interface (uint16_t interface, int stop)
{
if (interface == 0)
{
if (!stop)
usb_lld_setup_endpoint (ENDP1, EP_BULK, 0, ENDP1_RXADDR, ENDP1_TXADDR,
GNUK_MAX_PACKET_SIZE);
{
usb_lld_setup_endpoint (ENDP1, EP_BULK, 0, ENDP1_RXADDR,
ENDP1_TXADDR, GNUK_MAX_PACKET_SIZE);
usb_lld_setup_endpoint (ENDP2, EP_INTERRUPT, 0, 0, ENDP2_TXADDR, 0);
}
else
{
usb_lld_stall_rx (ENDP1);
usb_lld_stall_tx (ENDP1);
usb_lld_stall_tx (ENDP2);
}
}
#ifdef ENABLE_VIRTUAL_COM_PORT
else if (interface == 1)
{
if (!stop)
usb_lld_setup_endpoint (ENDP7, EP_INTERRUPT, 0, 0, ENDP7_TXADDR, 0);
else
usb_lld_stall_tx (ENDP7);
}
#ifdef ENABLE_VIRTUAL_COM_PORT
else if (interface == 2)
{
if (!stop)
usb_lld_setup_endpoint (ENDP4, EP_INTERRUPT, 0, 0, ENDP4_TXADDR, 0);
else
usb_lld_stall_tx (ENDP4);
}
else if (interface == 2)
else if (interface == 3)
{
if (!stop)
{
@@ -312,8 +336,39 @@ usb_cb_setup (uint8_t req, uint8_t req_no,
return USB_UNSUPPORT;
}
}
#ifdef ENABLE_VIRTUAL_COM_PORT
else if (index == 1)
{
switch (req_no)
{
case USB_HID_REQ_GET_IDLE:
usb_lld_set_data_to_send (&hid_idle_rate, 1);
return USB_SUCCESS;
case USB_HID_REQ_SET_IDLE:
usb_lld_set_data_to_recv (&hid_idle_rate, 1);
return USB_SUCCESS;
case USB_HID_REQ_GET_REPORT:
/* Request of LED status and key press */
usb_lld_set_data_to_send (&hid_report, 2);
return USB_SUCCESS;
case USB_HID_REQ_SET_REPORT:
/* Received LED set request */
if (len == 1)
usb_lld_set_data_to_recv (&hid_report, len);
return USB_SUCCESS;
case USB_HID_REQ_GET_PROTOCOL:
case USB_HID_REQ_SET_PROTOCOL:
/* This driver doesn't support boot protocol. */
return USB_UNSUPPORT;
default:
return USB_UNSUPPORT;
}
}
#ifdef ENABLE_VIRTUAL_COM_PORT
else if (index == 2)
return vcom_port_data_setup (req, req_no, value);
#endif
#ifdef PINPAD_DND_SUPPORT
@@ -338,20 +393,34 @@ usb_cb_setup (uint8_t req, uint8_t req_no,
return USB_UNSUPPORT;
}
void usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, uint16_t value,
uint16_t index, uint16_t len)
void
usb_cb_ctrl_write_finish (uint8_t req, uint8_t req_no, uint16_t value,
uint16_t index, uint16_t len)
{
uint8_t type_rcp = req & (REQUEST_TYPE|RECIPIENT);
if (type_rcp == (VENDOR_REQUEST | DEVICE_RECIPIENT)
&& USB_SETUP_SET (req) && req_no == USB_FSIJ_GNUK_EXEC && len == 0)
if (type_rcp == (VENDOR_REQUEST | DEVICE_RECIPIENT))
{
if (icc_state_p == NULL || *icc_state_p != ICC_STATE_EXITED)
return;
if (USB_SETUP_SET (req) && req_no == USB_FSIJ_GNUK_EXEC && len == 0)
{
if (icc_state_p == NULL || *icc_state_p != ICC_STATE_EXITED)
return;
(void)value; (void)index;
usb_lld_prepare_shutdown (); /* No further USB communication */
*icc_state_p = ICC_STATE_EXEC_REQUESTED;
(void)value; (void)index;
usb_lld_prepare_shutdown (); /* No further USB communication */
*icc_state_p = ICC_STATE_EXEC_REQUESTED;
}
}
else if (type_rcp == (CLASS_REQUEST | INTERFACE_RECIPIENT))
{
if (index == 1 && req_no == USB_HID_REQ_SET_REPORT)
{
if ((hid_report ^ hid_report_saved) & HID_LED_STATUS_NUMLOCK)
ccid_card_change_signal ();
hid_report_saved = hid_report;
}
}
}
@@ -399,7 +468,7 @@ int usb_cb_handle_event (uint8_t event_type, uint16_t value)
int usb_cb_interface (uint8_t cmd, uint16_t interface, uint16_t alt)
{
static uint8_t zero = 0;
static const uint8_t zero = 0;
if (interface >= NUM_INTERFACES)
return USB_UNSUPPORT;

View File

@@ -12,6 +12,45 @@
#include "usb_conf.h"
#include "usb-cdc.h"
/* HID report descriptor. */
#define HID_REPORT_DESC_SIZE (sizeof (hid_report_desc))
static const uint8_t hid_report_desc[] = {
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x07, /* USAGE (Keypad) */
0xa1, 0x01, /* COLLECTION (Application) */
0x05, 0x07, /* USAGE_PAGE (Key Codes) */
0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */
0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x08, /* REPORT_COUNT (8) */
0x81, 0x02, /* INPUT (Data,Var,Abs); Modifier byte */
/*
* NumLock, CapsLock, ScrollLock, Compose, Kana, Power, Shift,
* Do Not Disturb
*/
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x19, 0x01, /* USAGE_MINIMUM (1) */
0x29, 0x08, /* USAGE_MAXIMUM (8) */
0x95, 0x08, /* REPORT_COUNT (8) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs); LED report */
0x05, 0x07, /* USAGE_PAGE (Key Codes) */
0x19, 0x00, /* USAGE_MINIMUM (Reserved (no event indicated)) */
0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x65, /* LOGICAL_MAXIMUM (101) */
0x95, 0x06, /* REPORT_COUNT (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x81, 0x00, /* INPUT (Data,Ary,Abs); Key arrays (1 bytes) */
0xc0 /* END_COLLECTION */
};
#define USB_ICC_INTERFACE_CLASS 0x0B
#define USB_ICC_INTERFACE_SUBCLASS 0x00
#define USB_ICC_INTERFACE_BULK_PROTOCOL 0x00
@@ -33,9 +72,12 @@ static const uint8_t gnukDeviceDescriptor[] = {
0x01 /* bNumConfigurations */
};
#define ICC_TOTAL_LENGTH (9+9+54+7+7)
#define ICC_TOTAL_LENGTH (9+9+54+7+7+7)
#define ICC_NUM_INTERFACES 1
#define HID_TOTAL_LENGTH (9+9+7)
#define HID_NUM_INTERFACES 1
#ifdef ENABLE_VIRTUAL_COM_PORT
#define VCOM_TOTAL_LENGTH (9+5+5+4+5+7+9+7+7)
#define VCOM_NUM_INTERFACES 2
@@ -52,8 +94,10 @@ static const uint8_t gnukDeviceDescriptor[] = {
#define MSC_NUM_INTERFACES 0
#endif
#define W_TOTAL_LENGTH (ICC_TOTAL_LENGTH+VCOM_TOTAL_LENGTH+MSC_TOTAL_LENGTH)
#define NUM_INTERFACES (ICC_NUM_INTERFACES+VCOM_NUM_INTERFACES+MSC_NUM_INTERFACES)
#define W_TOTAL_LENGTH (ICC_TOTAL_LENGTH + HID_TOTAL_LENGTH \
+ VCOM_TOTAL_LENGTH + MSC_TOTAL_LENGTH)
#define NUM_INTERFACES (ICC_NUM_INTERFACES + HID_NUM_INTERFACES \
+ VCOM_NUM_INTERFACES + MSC_NUM_INTERFACES)
@@ -69,15 +113,15 @@ static const uint8_t gnukConfigDescriptor[] = {
50, /* MaxPower 100 mA */
/* Interface Descriptor */
9, /* bLength: Interface Descriptor size */
9, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface */
0, /* bInterfaceNumber: Index of this interface */
0, /* Alternate setting for this interface */
2, /* bNumEndpoints: Bulk-IN, Bulk-OUT */
0, /* bInterfaceNumber: Index of this interface */
0, /* Alternate setting for this interface */
3, /* bNumEndpoints: Bulk-IN, Bulk-OUT, Intr-IN */
USB_ICC_INTERFACE_CLASS,
USB_ICC_INTERFACE_SUBCLASS,
USB_ICC_INTERFACE_BULK_PROTOCOL,
0, /* string index for interface */
0, /* string index for interface */
/* ICC Descriptor */
54, /* bLength: */
@@ -144,11 +188,46 @@ static const uint8_t gnukConfigDescriptor[] = {
0x02, /* bmAttributes: Bulk */
USB_ICC_DATA_SIZE, 0x00, /* wMaxPacketSize: */
0x00, /* bInterval */
/*Endpoint IN2 Descriptor*/
7, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x82, /* bEndpointAddress: (IN2) */
0x03, /* bmAttributes: Interrupt */
4, 0x00, /* wMaxPacketSize: */
0xFF, /* bInterval (255ms) */
/* Interface Descriptor */
9, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface */
0x01, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoint used */
0x03, /* bInterfaceClass: HID */
0x00, /* bInterfaceSubClass: no boot */
0x00, /* bInterfaceProtocol: 0=none */
0x00, /* iInterface: no string for this interface */
9, /* bLength: HID Descriptor size */
0x21, /* bDescriptorType: HID */
0x10, 0x01, /* bcdHID: HID Class Spec release number */
0x00, /* bCountryCode: Hardware target country */
0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
0x22, /* bDescriptorType */
HID_REPORT_DESC_SIZE, 0, /* wItemLength: Total length of Report descriptor */
/*Endpoint IN7 Descriptor*/
7, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x87, /* bEndpointAddress: (IN7) */
0x03, /* bmAttributes: Interrupt */
0x02, 0x00, /* wMaxPacketSize: 2 */
0x20, /* bInterval (32ms) */
#ifdef ENABLE_VIRTUAL_COM_PORT
/* Interface Descriptor */
9, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface */
0x01, /* bInterfaceNumber: Number of Interface */
0x02, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
@@ -189,7 +268,7 @@ static const uint8_t gnukConfigDescriptor[] = {
/*Data class interface descriptor*/
9, /* bLength: Endpoint Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: */
0x02, /* bInterfaceNumber: Number of Interface */
0x03, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
@@ -216,9 +295,9 @@ static const uint8_t gnukConfigDescriptor[] = {
9, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface */
#ifdef ENABLE_VIRTUAL_COM_PORT
0x03, /* bInterfaceNumber. */
0x04, /* bInterfaceNumber. */
#else
0x01, /* bInterfaceNumber. */
0x02, /* bInterfaceNumber. */
#endif
0x00, /* bAlternateSetting. */
0x02, /* bNumEndpoints. */
@@ -230,19 +309,19 @@ static const uint8_t gnukConfigDescriptor[] = {
Mass Storage, MSCO chapter 3). */
0x00, /* iInterface. */
/* Endpoint Descriptor.*/
7, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x86, /* bEndpointAddress: (IN6) */
7, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x86, /* bEndpointAddress: (IN6) */
0x02, /* bmAttributes (Bulk). */
0x40, 0x00, /* wMaxPacketSize. */
0x00, /* bInterval (ignored for bulk). */
/* Endpoint Descriptor.*/
7, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x06, /* bEndpointAddress: (OUT6) */
0x02, /* bmAttributes (Bulk). */
0x40, 0x00, /* wMaxPacketSize. */
0x00, /* bInterval (ignored for bulk). */
7, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x06, /* bEndpointAddress: (OUT6) */
0x02, /* bmAttributes (Bulk). */
0x40, 0x00, /* wMaxPacketSize. */
0x00, /* bInterval (ignored for bulk). */
#endif
};
@@ -273,11 +352,13 @@ static const struct desc String_Descriptors[NUM_STRING_DESC] = {
{sys_version, sizeof (sys_version)},
};
#define USB_DT_HID 0x21
#define USB_DT_REPORT 0x22
int
usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
uint16_t index)
{
(void)index;
if (rcp == DEVICE_RECIPIENT)
{
if (desc_type == DEVICE_DESCRIPTOR)
@@ -302,6 +383,32 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index,
}
}
}
else if (rcp == INTERFACE_RECIPIENT)
{
if (index == 1)
{
if (desc_type == USB_DT_HID)
{
usb_lld_set_data_to_send (gnukConfigDescriptor+ICC_TOTAL_LENGTH+9,
9);
return USB_SUCCESS;
}
else if (desc_type == USB_DT_REPORT)
{
usb_lld_set_data_to_send (hid_report_desc, HID_REPORT_DESC_SIZE);
return USB_SUCCESS;
}
}
else if (desc_type == STRING_DESCRIPTOR)
{
if (desc_index < NUM_STRING_DESC)
{
usb_lld_set_data_to_send (String_Descriptors[desc_index].desc,
String_Descriptors[desc_index].size);
return USB_SUCCESS;
}
}
}
return USB_UNSUPPORT;
}