2010-08-18 03:57:45 +00:00
|
|
|
/*
|
2010-08-30 02:39:10 +00:00
|
|
|
* usb_prop.c - glue/interface code between Gnuk and USB-FS-Device_Lib
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Free Software Initiative of Japan
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2010-08-18 03:57:45 +00:00
|
|
|
#include "usb_lib.h"
|
|
|
|
#include "usb_conf.h"
|
|
|
|
#include "usb_prop.h"
|
|
|
|
#include "usb_desc.h"
|
|
|
|
#include "usb_pwr.h"
|
|
|
|
#include "hw_config.h"
|
|
|
|
|
2010-08-30 02:39:10 +00:00
|
|
|
#ifdef ENABLE_VIRTUAL_COM_PORT
|
|
|
|
#include "usb-cdc-vport.c"
|
2010-08-18 03:57:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
gnuk_device_init (void)
|
|
|
|
{
|
2010-08-30 02:39:10 +00:00
|
|
|
/*
|
|
|
|
* Update the serial number string descriptor (if needed)
|
|
|
|
*/
|
2010-08-18 03:57:45 +00:00
|
|
|
Get_SerialNum ();
|
|
|
|
|
|
|
|
pInformation->Current_Configuration = 0;
|
|
|
|
|
|
|
|
/* Connect the device */
|
|
|
|
PowerOn ();
|
|
|
|
|
|
|
|
/* Perform basic device initialization operations */
|
|
|
|
USB_SIL_Init ();
|
|
|
|
|
|
|
|
bDeviceState = UNCONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gnuk_device_reset (void)
|
|
|
|
{
|
2010-08-30 02:39:10 +00:00
|
|
|
/* Set DEVICE as not configured */
|
2010-08-18 03:57:45 +00:00
|
|
|
pInformation->Current_Configuration = 0;
|
|
|
|
|
|
|
|
/* Current Feature initialization */
|
|
|
|
pInformation->Current_Feature = Config_Descriptor.Descriptor[7];
|
|
|
|
|
2010-08-30 02:39:10 +00:00
|
|
|
/* Set DEVICE with the default Interface*/
|
2010-08-18 03:57:45 +00:00
|
|
|
pInformation->Current_Interface = 0;
|
|
|
|
|
|
|
|
SetBTABLE (BTABLE_ADDRESS);
|
|
|
|
|
|
|
|
/* Initialize Endpoint 0 */
|
|
|
|
SetEPType (ENDP0, EP_CONTROL);
|
|
|
|
SetEPTxStatus (ENDP0, EP_TX_STALL);
|
|
|
|
SetEPRxAddr (ENDP0, ENDP0_RXADDR);
|
|
|
|
SetEPTxAddr (ENDP0, ENDP0_TXADDR);
|
|
|
|
Clear_Status_Out (ENDP0);
|
|
|
|
SetEPRxCount (ENDP0, GNUK_MAX_PACKET_SIZE);
|
|
|
|
SetEPRxValid (ENDP0);
|
|
|
|
|
|
|
|
/* Initialize Endpoint 1 */
|
|
|
|
SetEPType (ENDP1, EP_BULK);
|
|
|
|
SetEPTxAddr (ENDP1, ENDP1_TXADDR);
|
|
|
|
SetEPTxStatus (ENDP1, EP_TX_NAK);
|
|
|
|
SetEPRxStatus (ENDP1, EP_RX_DIS);
|
|
|
|
|
|
|
|
/* Initialize Endpoint 2 */
|
2010-09-04 09:44:01 +00:00
|
|
|
SetEPType (ENDP2, EP_BULK);
|
|
|
|
SetEPRxAddr (ENDP2, ENDP2_RXADDR);
|
|
|
|
SetEPRxCount (ENDP2, GNUK_MAX_PACKET_SIZE);
|
|
|
|
SetEPRxStatus (ENDP2, EP_RX_VALID);
|
|
|
|
SetEPTxStatus (ENDP2, EP_TX_DIS);
|
2010-08-18 03:57:45 +00:00
|
|
|
|
2010-09-04 09:44:01 +00:00
|
|
|
#ifdef ENABLE_VIRTUAL_COM_PORT
|
2010-08-18 03:57:45 +00:00
|
|
|
/* Initialize Endpoint 3 */
|
|
|
|
SetEPType (ENDP3, EP_BULK);
|
2010-09-04 09:44:01 +00:00
|
|
|
SetEPTxAddr (ENDP3, ENDP3_TXADDR);
|
|
|
|
SetEPTxStatus (ENDP3, EP_TX_NAK);
|
|
|
|
SetEPRxStatus (ENDP3, EP_RX_DIS);
|
2010-08-18 03:57:45 +00:00
|
|
|
|
|
|
|
/* Initialize Endpoint 4 */
|
2010-09-04 09:44:01 +00:00
|
|
|
SetEPType (ENDP4, EP_INTERRUPT);
|
2010-08-18 03:57:45 +00:00
|
|
|
SetEPTxAddr (ENDP4, ENDP4_TXADDR);
|
|
|
|
SetEPTxStatus (ENDP4, EP_TX_NAK);
|
|
|
|
SetEPRxStatus (ENDP4, EP_RX_DIS);
|
|
|
|
|
|
|
|
/* Initialize Endpoint 5 */
|
|
|
|
SetEPType (ENDP5, EP_BULK);
|
|
|
|
SetEPRxAddr (ENDP5, ENDP5_RXADDR);
|
2010-09-04 09:44:01 +00:00
|
|
|
SetEPRxCount (ENDP5, VIRTUAL_COM_PORT_DATA_SIZE);
|
2010-08-18 03:57:45 +00:00
|
|
|
SetEPRxStatus (ENDP5, EP_RX_VALID);
|
|
|
|
SetEPTxStatus (ENDP5, EP_TX_DIS);
|
2010-09-04 09:44:01 +00:00
|
|
|
#endif
|
2010-08-18 03:57:45 +00:00
|
|
|
|
|
|
|
/* Set this device to response on default address */
|
|
|
|
SetDeviceAddress (0);
|
|
|
|
|
|
|
|
bDeviceState = ATTACHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gnuk_device_SetConfiguration (void)
|
|
|
|
{
|
|
|
|
DEVICE_INFO *pInfo = &Device_Info;
|
|
|
|
|
|
|
|
if (pInfo->Current_Configuration != 0)
|
|
|
|
/* Device configured */
|
|
|
|
bDeviceState = CONFIGURED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gnuk_device_SetDeviceAddress (void)
|
|
|
|
{
|
|
|
|
bDeviceState = ADDRESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IN from port 0 */
|
|
|
|
static void
|
|
|
|
gnuk_device_Status_In (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OUT to port 0 */
|
|
|
|
static void
|
|
|
|
gnuk_device_Status_Out (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *
|
|
|
|
gnuk_device_GetDeviceDescriptor (uint16_t Length)
|
|
|
|
{
|
2010-09-04 09:44:01 +00:00
|
|
|
return Standard_GetDescriptorData (Length,
|
|
|
|
(PONE_DESCRIPTOR)&Device_Descriptor);
|
2010-08-18 03:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *
|
|
|
|
gnuk_device_GetConfigDescriptor (uint16_t Length)
|
|
|
|
{
|
2010-09-04 09:44:01 +00:00
|
|
|
return Standard_GetDescriptorData (Length,
|
|
|
|
(PONE_DESCRIPTOR)&Config_Descriptor);
|
2010-08-18 03:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *
|
|
|
|
gnuk_device_GetStringDescriptor (uint16_t Length)
|
|
|
|
{
|
|
|
|
uint8_t wValue0 = pInformation->USBwValue0;
|
|
|
|
|
|
|
|
if (wValue0 > (sizeof (String_Descriptor) / sizeof (ONE_DESCRIPTOR)))
|
|
|
|
return NULL;
|
|
|
|
else
|
2010-09-04 09:44:01 +00:00
|
|
|
return Standard_GetDescriptorData (Length,
|
|
|
|
(PONE_DESCRIPTOR)&String_Descriptor[wValue0]);
|
2010-08-18 03:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static RESULT
|
|
|
|
gnuk_device_Get_Interface_Setting (uint8_t Interface, uint8_t AlternateSetting)
|
|
|
|
{
|
|
|
|
if (AlternateSetting > 0)
|
|
|
|
return USB_UNSUPPORT;
|
|
|
|
else if (Interface > 1)
|
|
|
|
return USB_UNSUPPORT;
|
|
|
|
|
|
|
|
return USB_SUCCESS;
|
|
|
|
}
|
2010-08-30 02:39:10 +00:00
|
|
|
|
2010-09-04 09:44:01 +00:00
|
|
|
#if !defined(ENABLE_VIRTUAL_COM_PORT)
|
|
|
|
static RESULT
|
|
|
|
gnuk_nothing_todo (uint8_t RequestNo)
|
|
|
|
{
|
|
|
|
(void)RequestNo;
|
|
|
|
return USB_UNSUPPORT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-18 03:57:45 +00:00
|
|
|
/*
|
|
|
|
* Interface to USB core
|
|
|
|
*/
|
|
|
|
|
2010-09-04 04:48:26 +00:00
|
|
|
const DEVICE_PROP Device_Property = {
|
2010-08-18 03:57:45 +00:00
|
|
|
gnuk_device_init,
|
|
|
|
gnuk_device_reset,
|
|
|
|
gnuk_device_Status_In,
|
|
|
|
gnuk_device_Status_Out,
|
2010-08-30 02:39:10 +00:00
|
|
|
#ifdef ENABLE_VIRTUAL_COM_PORT
|
2010-08-18 03:57:45 +00:00
|
|
|
Virtual_Com_Port_Data_Setup,
|
|
|
|
Virtual_Com_Port_NoData_Setup,
|
2010-08-30 02:39:10 +00:00
|
|
|
#else
|
2010-09-04 09:44:01 +00:00
|
|
|
gnuk_nothing_todo,
|
|
|
|
gnuk_nothing_todo,
|
2010-08-30 02:39:10 +00:00
|
|
|
#endif
|
2010-08-18 03:57:45 +00:00
|
|
|
gnuk_device_Get_Interface_Setting,
|
|
|
|
gnuk_device_GetDeviceDescriptor,
|
|
|
|
gnuk_device_GetConfigDescriptor,
|
|
|
|
gnuk_device_GetStringDescriptor,
|
|
|
|
0,
|
|
|
|
GNUK_MAX_PACKET_SIZE
|
|
|
|
};
|
|
|
|
|
2010-09-04 04:48:26 +00:00
|
|
|
const DEVICE Device_Table = {
|
2010-08-18 03:57:45 +00:00
|
|
|
EP_NUM,
|
|
|
|
1
|
|
|
|
};
|
|
|
|
|
2010-09-04 04:48:26 +00:00
|
|
|
const USER_STANDARD_REQUESTS User_Standard_Requests = {
|
2010-08-18 03:57:45 +00:00
|
|
|
NOP_Process, /* GetConfiguration */
|
|
|
|
gnuk_device_SetConfiguration,
|
|
|
|
NOP_Process, /* GetInterface */
|
|
|
|
NOP_Process, /* SetInterface */
|
|
|
|
NOP_Process, /* GetStatus */
|
|
|
|
NOP_Process, /* ClearFeature */
|
|
|
|
NOP_Process, /* SetEndPointFeature */
|
|
|
|
NOP_Process, /* SetDeviceFeature */
|
|
|
|
gnuk_device_SetDeviceAddress
|
|
|
|
};
|