From 55e9678ab87dbd70f56d0914ef9d76a5346d571b Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Mon, 30 Aug 2010 11:39:46 +0900 Subject: [PATCH] more --- src/config.h | 4 ++ src/flash.c | 56 +++++++++++++++++++++++++ src/usb-cdc-vport.c | 100 ++++++++++++++++++++++++++++++++++++++++++++ src/usb-cdc.h | 7 ++++ 4 files changed, 167 insertions(+) create mode 100644 src/config.h create mode 100644 src/flash.c create mode 100644 src/usb-cdc-vport.c create mode 100644 src/usb-cdc.h diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..9393c23 --- /dev/null +++ b/src/config.h @@ -0,0 +1,4 @@ +#define DEBUG 1 +#define ENABLE_VIRTUAL_COM_PORT 1 + +#define GNUK_MAX_PACKET_SIZE 64 /* USB */ diff --git a/src/flash.c b/src/flash.c new file mode 100644 index 0000000..8f173dc --- /dev/null +++ b/src/flash.c @@ -0,0 +1,56 @@ +/* + * flash.c -- Data Objects (DO) and GPG Key handling on Flash ROM + * + * Copyright (C) 2010 Free Software Initiative of Japan + * Author: NIIBE Yutaka + * + * 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 . + * + */ + +#include "ch.h" +#include "gnuk.h" + +uint8_t * +flash_do_write (uint16_t tag, uint8_t *data, int len) +{ + static uint8_t do_pool[1024]; + static uint8_t *last_p = do_pool; + uint8_t *p = last_p; + + if (last_p - do_pool + len + 2 + 3 > 1024) + return NULL; + + *last_p++ = (tag >> 8); + *last_p++ = (tag & 0xff); + if (len < 128) + *last_p++ = len; + else if (len < 256) + { + *last_p++ = 0x81; + *last_p++ = len; + } + else + { + *last_p++ = 0x82; + *last_p++ = (len >> 8); + *last_p++ = (len & 0xff); + } + memcpy (last_p, data, len); + last_p += len; + + return p + 2; +} diff --git a/src/usb-cdc-vport.c b/src/usb-cdc-vport.c new file mode 100644 index 0000000..5ca2df6 --- /dev/null +++ b/src/usb-cdc-vport.c @@ -0,0 +1,100 @@ +/* + * This file is included by usb_prop.c to provide Virtual COM port feature + */ + +/* Original is ../Virtual_COM_Port/usb_prop.c by STMicroelectronics */ +/* Chopped and modified for Gnuk */ + +#include "usb-cdc.h" + +/* Original copyright notice is following: */ +/******************** (C) COPYRIGHT 2010 STMicroelectronics ******************** +* File Name : usb_prop.c +* Author : MCD Application Team +* Version : V3.1.1 +* Date : 04/07/2010 +* Description : All processing related to Virtual Com Port Demo +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +typedef struct +{ + uint32_t bitrate; + uint8_t format; + uint8_t paritytype; + uint8_t datatype; +} LINE_CODING; + +static LINE_CODING linecoding = { + 115200, /* baud rate*/ + 0x00, /* stop bits-1*/ + 0x00, /* parity - none*/ + 0x08 /* no. of bits 8*/ +}; + +static uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length) +{ + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding); + return NULL; + } + + return (uint8_t *)&linecoding; +} + +static uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length) +{ + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding); + return NULL; + } + + return (uint8_t *)&linecoding; +} + +static RESULT +Virtual_Com_Port_Data_Setup (uint8_t RequestNo) +{ + uint8_t *(*CopyRoutine)(uint16_t); + + CopyRoutine = NULL; + + if (RequestNo == USB_CDC_REQ_GET_LINE_CODING) + { + if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) + CopyRoutine = Virtual_Com_Port_GetLineCoding; + } + else if (RequestNo == USB_CDC_REQ_SET_LINE_CODING) + { + if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) + CopyRoutine = Virtual_Com_Port_SetLineCoding; + } + + if (CopyRoutine == NULL) + return USB_UNSUPPORT; + + pInformation->Ctrl_Info.CopyData = CopyRoutine; + pInformation->Ctrl_Info.Usb_wOffset = 0; + (*CopyRoutine) (0); /* Set Ctrl_Info.Usb_wLength */ + + return USB_SUCCESS; +} + +static RESULT +Virtual_Com_Port_NoData_Setup (uint8_t RequestNo) +{ + if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT) + && RequestNo == USB_CDC_REQ_SET_CONTROL_LINE_STATE) + /* Do nothing and success */ + return USB_SUCCESS; + + return USB_UNSUPPORT; +} diff --git a/src/usb-cdc.h b/src/usb-cdc.h new file mode 100644 index 0000000..93fcd4e --- /dev/null +++ b/src/usb-cdc.h @@ -0,0 +1,7 @@ +/* + * Class specific control requests for CDC + */ +#define USB_CDC_REQ_SET_LINE_CODING 0x20 +#define USB_CDC_REQ_GET_LINE_CODING 0x21 +#define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22 +#define USB_CDC_REQ_SEND_BREAK 0x23