gnuk/src/usb_prop.c

401 lines
9.4 KiB
C
Raw Normal View History

2010-08-18 03:57:45 +00:00
/*
2012-05-10 10:01:01 +00:00
* usb_prop.c - interface code between Gnuk and USB
2010-08-30 02:39:10 +00:00
*
2012-05-10 10:01:01 +00:00
* Copyright (C) 2010, 2011, 2012 Free Software Initiative of Japan
2010-08-30 02:39:10 +00:00
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Gnuk, a GnuPG USB Token implementation.
*
* Gnuk is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
2010-09-09 08:50:34 +00:00
/* Packet size of USB Bulk transfer for full speed */
#define GNUK_MAX_PACKET_SIZE 64
2010-08-30 02:39:10 +00:00
#include "config.h"
2012-05-10 10:01:01 +00:00
#include "ch.h"
#include "usb_lld.h"
2010-08-18 03:57:45 +00:00
#include "usb_conf.h"
2012-05-17 08:02:49 +00:00
#include "gnuk.h"
2010-08-18 03:57:45 +00:00
2010-08-30 02:39:10 +00:00
#ifdef ENABLE_VIRTUAL_COM_PORT
2012-05-10 10:01:01 +00:00
#include "usb-cdc.h"
2011-12-09 08:53:45 +00:00
2012-05-10 10:01:01 +00:00
struct line_coding
{
uint32_t bitrate;
uint8_t format;
uint8_t paritytype;
uint8_t datatype;
};
2011-01-07 07:18:47 +00:00
2012-05-17 08:02:49 +00:00
static struct line_coding line_coding = {
2012-05-10 10:01:01 +00:00
115200, /* baud rate: 115200 */
0x00, /* stop bits: 1 */
0x00, /* parity: none */
0x08 /* bits: 8 */
};
2010-08-18 03:57:45 +00:00
static void
2012-05-17 08:02:49 +00:00
vcom_port_data_setup (uint8_t req, uint8_t req_no)
2010-08-18 03:57:45 +00:00
{
2012-05-17 08:02:49 +00:00
if ((req & REQUEST_DIR) == 1 && req_no == USB_CDC_REQ_GET_LINE_CODING)
usb_lld_set_data_to_send (&line_coding, sizeof(line_coding));
2010-08-18 03:57:45 +00:00
2012-05-17 08:02:49 +00:00
if ((req & REQUEST_DIR) == 0 && req_no == USB_CDC_REQ_SET_LINE_CODING)
usb_lld_set_data_to_recv (&line_coding, sizeof(line_coding));
2010-08-18 03:57:45 +00:00
}
2012-05-10 10:01:01 +00:00
static int
2012-05-17 08:02:49 +00:00
vcom_port_setup_with_nodata (uint8_t req, uint8_t req_no)
2010-08-18 03:57:45 +00:00
{
2012-05-17 08:02:49 +00:00
if ((req & REQUEST_DIR) == 0 && req_no == USB_CDC_REQ_SET_CONTROL_LINE_STATE)
2012-05-10 10:01:01 +00:00
/* Do nothing and success */
return USB_SUCCESS;
2010-08-18 03:57:45 +00:00
2012-05-10 10:01:01 +00:00
return USB_UNSUPPORT;
}
2012-05-14 04:32:47 +00:00
#define VCOM_NUM_INTERFACES 2
#else
#define VCOM_NUM_INTERFACES 0
2010-09-04 09:44:01 +00:00
#endif
2010-08-18 03:57:45 +00:00
2011-12-12 09:12:43 +00:00
#ifdef PINPAD_DND_SUPPORT
2012-05-14 06:38:03 +00:00
#include "usb-msc.h"
2012-05-14 04:32:47 +00:00
#define MSC_NUM_INTERFACES 1
#else
#define MSC_NUM_INTERFACES 0
2011-12-12 09:12:43 +00:00
#endif
2012-05-14 04:32:47 +00:00
#define NUM_INTERFACES (1+VCOM_NUM_INTERFACES+MSC_NUM_INTERFACES)
#define MSC_INTERFACE_NO (1+VCOM_NUM_INTERFACES)
2010-08-18 03:57:45 +00:00
2012-05-10 10:01:01 +00:00
uint32_t bDeviceState = UNCONNECTED; /* USB device status */
2010-08-18 03:57:45 +00:00
static void
2012-05-10 10:01:01 +00:00
gnuk_device_init (void)
2010-08-18 03:57:45 +00:00
{
2012-05-10 10:01:01 +00:00
usb_lld_set_configuration (0);
USB_Cable_Config (1);
bDeviceState = UNCONNECTED;
2010-08-18 03:57:45 +00:00
}
2011-02-01 06:25:36 +00:00
static void
gnuk_setup_endpoints_for_interface (uint16_t interface, int stop)
2011-02-01 06:25:36 +00:00
{
2012-05-10 10:01:01 +00:00
if (interface == 0)
2011-02-01 06:25:36 +00:00
{
if (!stop)
usb_lld_setup_endpoint (ENDP1, EP_BULK, 0, ENDP1_RXADDR, ENDP1_TXADDR,
GNUK_MAX_PACKET_SIZE);
else
{
usb_lld_stall_rx (ENDP1);
usb_lld_stall_tx (ENDP1);
}
2011-02-01 06:25:36 +00:00
}
#ifdef ENABLE_VIRTUAL_COM_PORT
2012-05-10 10:01:01 +00:00
else if (interface == 1)
2011-02-01 06:25:36 +00:00
{
if (!stop)
usb_lld_setup_endpoint (ENDP4, EP_INTERRUPT, 0, 0, ENDP4_TXADDR, 0);
else
usb_lld_stall_tx (ENDP4);
2011-02-01 06:25:36 +00:00
}
2012-05-10 10:01:01 +00:00
else if (interface == 2)
2011-02-01 06:25:36 +00:00
{
if (!stop)
{
usb_lld_setup_endpoint (ENDP3, EP_BULK, 0, 0, ENDP3_TXADDR, 0);
usb_lld_setup_endpoint (ENDP5, EP_BULK, 0, ENDP5_RXADDR, 0,
VIRTUAL_COM_PORT_DATA_SIZE);
}
else
{
usb_lld_stall_tx (ENDP3);
usb_lld_stall_rx (ENDP5);
}
2011-02-01 06:25:36 +00:00
}
#endif
2011-12-12 09:12:43 +00:00
#ifdef PINPAD_DND_SUPPORT
2012-05-14 04:32:47 +00:00
else if (interface == MSC_INTERFACE_NO)
2011-12-12 09:12:43 +00:00
{
if (!stop)
{
usb_lld_setup_endpoint (ENDP6, EP_BULK, 0,
ENDP6_RXADDR, ENDP6_TXADDR, 64);
usb_lld_stall_rx (ENDP6);
}
else
{
usb_lld_stall_tx (ENDP6);
usb_lld_stall_rx (ENDP6);
}
2011-12-12 09:12:43 +00:00
}
#endif
2011-02-01 06:25:36 +00:00
}
2012-05-10 10:01:01 +00:00
static void
gnuk_device_reset (void)
2010-08-18 03:57:45 +00:00
{
2012-05-10 10:01:01 +00:00
int i;
2010-08-18 03:57:45 +00:00
2012-05-10 10:01:01 +00:00
/* Set DEVICE as not configured */
usb_lld_set_configuration (0);
/* Current Feature initialization */
usb_lld_set_feature (Config_Descriptor.Descriptor[7]);
usb_lld_reset ();
/* Initialize Endpoint 0 */
2012-05-11 23:06:33 +00:00
usb_lld_setup_endpoint (ENDP0, EP_CONTROL, 0, ENDP0_RXADDR, ENDP0_TXADDR,
2012-05-10 10:01:01 +00:00
GNUK_MAX_PACKET_SIZE);
for (i = 0; i < NUM_INTERFACES; i++)
gnuk_setup_endpoints_for_interface (i, 0);
2012-05-10 10:01:01 +00:00
bDeviceState = ATTACHED;
2010-08-18 03:57:45 +00:00
}
2010-08-30 02:39:10 +00:00
2010-12-10 07:31:25 +00:00
#define USB_CCID_REQ_ABORT 0x01
#define USB_CCID_REQ_GET_CLOCK_FREQUENCIES 0x02
#define USB_CCID_REQ_GET_DATA_RATES 0x03
static const uint8_t freq_table[] = { 0xf3, 0x0d, 0, 0, }; /* dwDefaultClock */
static const uint8_t data_rate_table[] = { 0x80, 0x25, 0, 0, }; /* dwDataRate */
2011-12-28 03:27:16 +00:00
#if defined(PINPAD_DND_SUPPORT)
2011-12-09 08:53:45 +00:00
static const uint8_t lun_table[] = { 0, 0, 0, 0, };
2011-12-28 03:27:16 +00:00
#endif
2011-12-09 08:53:45 +00:00
2012-05-17 08:02:49 +00:00
static const uint8_t *mem_info[] = { &__heap_base__, &__heap_end__, };
#define USB_FSIJ_GNUK_MEMINFO 0
#define USB_FSIJ_GNUK_DOWNLOAD 1
#define USB_FSIJ_GNUK_EXEC 2
2012-05-10 10:01:01 +00:00
static void
2012-05-17 08:02:49 +00:00
gnuk_setup_with_data (uint8_t req, uint8_t req_no, uint16_t index,
uint16_t len)
2010-09-04 09:44:01 +00:00
{
2012-05-17 08:02:49 +00:00
uint8_t recipient = req & RECIPIENT;
if (recipient == (VENDOR_REQUEST | DEVICE_RECIPIENT))
{
if ((req & REQUEST_DIR) == 1 && req_no == USB_FSIJ_GNUK_MEMINFO)
usb_lld_set_data_to_send (mem_info, sizeof (mem_info));
else if ((req & REQUEST_DIR) == 0 && req_no == USB_FSIJ_GNUK_DOWNLOAD)
{
if (icc_state_p == NULL || *icc_state_p != ICC_STATE_EXITED)
return;
if ((uint32_t)(index * 0x100) < (uint32_t)&__heap_base__
|| (uint32_t)((index * 0x100) + len) > (uint32_t)&__heap_end__)
return;
usb_lld_set_data_to_recv ((void *)0x20000000 + index*0x100, len);
}
}
else if (recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
2012-05-11 00:28:04 +00:00
{
if (index == 0)
{
2012-05-17 08:02:49 +00:00
if ((req & REQUEST_DIR) == 1
&& req_no == USB_CCID_REQ_GET_CLOCK_FREQUENCIES)
2012-05-11 00:28:04 +00:00
usb_lld_set_data_to_send (freq_table, sizeof (freq_table));
2012-05-17 08:02:49 +00:00
else if ((req & REQUEST_DIR) == 1
&& req_no == USB_CCID_REQ_GET_DATA_RATES)
usb_lld_set_data_to_send (data_rate_table,
sizeof (data_rate_table));
2012-05-11 00:28:04 +00:00
}
2012-05-14 04:32:47 +00:00
#ifdef ENABLE_VIRTUAL_COM_PORT
2012-05-11 00:28:04 +00:00
else if (index == 1)
2012-05-17 08:02:49 +00:00
vcom_port_data_setup (req, req_no);
2012-05-14 04:32:47 +00:00
#endif
#ifdef PINPAD_DND_SUPPORT
else if (index == MSC_INTERFACE_NO)
2012-05-11 00:28:04 +00:00
{
2012-05-17 08:02:49 +00:00
if ((req & REQUEST_DIR) == 1 && req_no == MSC_GET_MAX_LUN_COMMAND)
2012-05-11 00:28:04 +00:00
usb_lld_set_data_to_send (lun_table, sizeof (lun_table));
}
2011-12-09 08:53:45 +00:00
#endif
2012-05-11 00:28:04 +00:00
}
2010-09-04 09:44:01 +00:00
}
2010-12-10 07:31:25 +00:00
2011-12-09 08:53:45 +00:00
2012-05-10 10:01:01 +00:00
static int
2012-05-17 08:02:49 +00:00
gnuk_setup_with_nodata (uint8_t req, uint8_t req_no, uint16_t index)
2010-12-10 07:31:25 +00:00
{
2012-05-17 08:02:49 +00:00
uint8_t recipient = req & RECIPIENT;
if ((req & REQUEST_DIR) == 1)
return USB_UNSUPPORT;
if (recipient == (VENDOR_REQUEST | DEVICE_RECIPIENT))
{
if (req_no == USB_FSIJ_GNUK_EXEC)
{
if (icc_state_p == NULL || *icc_state_p != ICC_STATE_EXITED)
return USB_UNSUPPORT;
*icc_state_p = ICC_STATE_EXEC_REQUESTED;
return USB_SUCCESS;
}
}
else if (recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
2012-05-10 10:01:01 +00:00
if (index == 0)
2010-12-10 07:31:25 +00:00
{
2012-05-17 08:02:49 +00:00
if (req_no == USB_CCID_REQ_ABORT)
2010-12-10 07:31:25 +00:00
/* wValue: bSeq, bSlot */
/* Abortion is not supported in Gnuk */
return USB_UNSUPPORT;
else
return USB_UNSUPPORT;
}
2012-05-14 04:32:47 +00:00
#ifdef ENABLE_VIRTUAL_COM_PORT
2012-05-10 10:01:01 +00:00
else if (index == 1)
2012-05-17 08:02:49 +00:00
return vcom_port_setup_with_nodata (req, req_no);
2012-05-14 04:32:47 +00:00
#endif
#ifdef PINPAD_DND_SUPPORT
else if (index == MSC_INTERFACE_NO)
2010-12-10 07:31:25 +00:00
{
2012-05-17 08:02:49 +00:00
if (req_no == MSC_MASS_STORAGE_RESET_COMMAND)
2011-12-09 08:53:45 +00:00
{
/* Should call resetting MSC thread, something like msc_reset() */
return USB_SUCCESS;
}
2010-12-10 07:31:25 +00:00
}
2011-12-09 08:53:45 +00:00
#endif
2012-05-17 08:02:49 +00:00
return USB_UNSUPPORT;
2010-12-10 07:31:25 +00:00
}
2010-09-04 09:44:01 +00:00
2012-05-10 10:01:01 +00:00
static int
gnuk_get_descriptor (uint8_t desc_type, uint16_t index, uint16_t value)
{
(void)index;
if (desc_type == DEVICE_DESCRIPTOR)
{
usb_lld_set_data_to_send (Device_Descriptor.Descriptor,
Device_Descriptor.Descriptor_Size);
return USB_SUCCESS;
}
else if (desc_type == CONFIG_DESCRIPTOR)
{
usb_lld_set_data_to_send (Config_Descriptor.Descriptor,
Config_Descriptor.Descriptor_Size);
return USB_SUCCESS;
}
else if (desc_type == STRING_DESCRIPTOR)
{
uint8_t desc_index = value & 0xff;
if (desc_index < NUM_STRING_DESC)
{
usb_lld_set_data_to_send (String_Descriptors[desc_index].Descriptor,
String_Descriptors[desc_index].Descriptor_Size);
return USB_SUCCESS;
}
}
return USB_UNSUPPORT;
}
static int gnuk_usb_event (uint8_t event_type, uint16_t value)
{
2012-05-17 08:02:49 +00:00
int i;
2012-05-10 10:01:01 +00:00
switch (event_type)
{
case USB_EVENT_RESET:
break;
case USB_EVENT_ADDRESS:
bDeviceState = ADDRESSED;
break;
case USB_EVENT_CONFIG:
if (usb_lld_current_configuration () == 0)
{
if (value != 1)
return USB_UNSUPPORT;
usb_lld_set_configuration (value);
for (i = 0; i < NUM_INTERFACES; i++)
gnuk_setup_endpoints_for_interface (i, 0);
2012-05-10 10:01:01 +00:00
bDeviceState = CONFIGURED;
chEvtSignalI (main_thread, LED_STATUS_MODE);
return USB_SUCCESS;
}
else
{
if (value != 0)
return USB_UNSUPPORT;
usb_lld_set_configuration (0);
for (i = 0; i < NUM_INTERFACES; i++)
gnuk_setup_endpoints_for_interface (i, 1);
2012-05-10 10:01:01 +00:00
bDeviceState = ADDRESSED;
}
default:
break;
}
return USB_UNSUPPORT;
}
static int gnuk_interface (uint8_t cmd, uint16_t interface, uint16_t alt)
{
static uint8_t zero = 0;
if (interface >= NUM_INTERFACES)
return USB_UNSUPPORT;
switch (cmd)
{
case USB_SET_INTERFACE:
if (alt != 0)
return USB_UNSUPPORT;
else
{
gnuk_setup_endpoints_for_interface (interface, 0);
2012-05-10 10:01:01 +00:00
return USB_SUCCESS;
}
case USB_GET_INTERFACE:
usb_lld_set_data_to_send (&zero, 1);
return USB_SUCCESS;
default:
case USB_QUERY_INTERFACE:
return USB_SUCCESS;
}
}
2010-08-18 03:57:45 +00:00
/*
* Interface to USB core
*/
2012-05-10 10:01:01 +00:00
const struct usb_device_method Device_Method = {
2010-08-18 03:57:45 +00:00
gnuk_device_init,
gnuk_device_reset,
2010-12-10 07:31:25 +00:00
gnuk_setup_with_data,
gnuk_setup_with_nodata,
2012-05-10 10:01:01 +00:00
gnuk_get_descriptor,
gnuk_usb_event,
gnuk_interface,
2010-08-18 03:57:45 +00:00
};