aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/src/os/usbcmd_generic.c
blob: aff13ebb09446e6da8cec1ff5920ee294f28d99c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Some generel USB API commands, common between OpenPCD and OpenPICC
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
 */

#include <string.h>
#include <sys/types.h>
#include <openpcd.h>
#include <os/req_ctx.h>
#include <os/usb_handler.h>
#include <os/led.h>
#include <os/dbgu.h>
#include <os/main.h>
#include <os/flash.h>
#include <board.h>
#ifdef  PCD
#include <rc632_highlevel.h>
#endif/*PCD*/

#define OPENPCD_API_VERSION (0x01)
#define CONFIG_AREA_ADDR ((void*)(AT91C_IFLASH + AT91C_IFLASH_SIZE - ENVIRONMENT_SIZE))
#define CONFIG_AREA_WORDS ( AT91C_IFLASH_PAGE_SIZE/sizeof(u_int32_t) )

volatile u_int32_t config_stack[ CONFIG_AREA_WORDS ];

static int gen_setenv(const void* buffer,int len)
{
    volatile unsigned int i;
    u_int32_t *dst;

    if( len >= sizeof(config_stack) )
	len=sizeof(config_stack);
	
    memcpy(&config_stack,buffer,len);
    
    /* retrieve current content to allow partial flashing */    
    
    /* flash changes */
    dst=(u_int32_t*)CONFIG_AREA_ADDR;
    for(i=0;i<CONFIG_AREA_WORDS;i++)
	*dst++=config_stack[i];

    flash_page(CONFIG_AREA_ADDR);
	
    return len;
}

static int gen_getenv(void* buffer,int len)
{
    if( len >= sizeof(config_stack) )
	len=sizeof(config_stack);

    memcpy(buffer,&config_stack,len);

    return len;
}

static int gen_usb_rx(struct req_ctx *rctx)
{
	struct openpcd_hdr *poh = (struct openpcd_hdr *) rctx->data;
	struct openpcd_compile_version *ver =
	    (struct openpcd_compile_version *)poh->data; 
	u_int32_t len = rctx->tot_len-sizeof(*poh);

        /* initialize transmit length to header length */
        rctx->tot_len = sizeof(*poh);

	switch (poh->cmd) {

	case OPENPCD_CMD_GET_API_VERSION:
		DEBUGP("CMD_GET_API_VERSION\n");
		poh->flags &= OPENPCD_FLAG_RESPOND;
		poh->val = OPENPCD_API_VERSION;
		break;
		
	case OPENPCD_CMD_GET_ENVIRONMENT:
		poh->flags &= OPENPCD_FLAG_RESPOND;
		poh->val = gen_getenv(&poh->data,poh->val);
		rctx->tot_len += poh->val;
		DEBUGP("CMD_GET_ENVIRONMENT(res_len=%u)\n", poh->val);
		break;
		
	case OPENPCD_CMD_SET_ENVIRONMENT:
		DEBUGP("CMD_SET_ENVIRONMENT (in_len=%u)\n", len);
		gen_setenv(&poh->data,len);
		break;
		
	case OPENPCD_CMD_RESET:
		DEBUGP("CMD_RESET\n");
		AT91F_RSTSoftReset(AT91C_BASE_RSTC, AT91C_RSTC_PROCRST|
				   AT91C_RSTC_PERRST|AT91C_RSTC_EXTRST);
		break;

	case OPENPCD_CMD_GET_VERSION:
		DEBUGP("GET_VERSION\n");
		poh->flags |= OPENPCD_FLAG_RESPOND;
		memcpy(ver, &opcd_version, sizeof(*ver));
		rctx->tot_len += sizeof(*ver);
		break;

	case OPENPCD_CMD_SET_LED:
		DEBUGP("SET LED(%u,%u)\n", poh->reg, poh->val);
		led_switch(poh->reg, poh->val);
		break;

	case OPENPCD_CMD_GET_SERIAL:
		DEBUGP("GET SERIAL(");
		poh->flags |= OPENPCD_FLAG_RESPOND;
#ifdef PCD
		rctx->tot_len += 4;
		if (rc632_get_serial(NULL, (u_int32_t *)poh->data) < 0) {
			DEBUGP("ERROR) ");
			return USB_ERR(USB_ERR_CMD_NOT_IMPL);
		}

		DEBUGP("%s)\n", hexdump(poh->data, 4));
#else
		/* FIXME: where to get serial in PICC case */
		return USB_ERR(USB_ERR_CMD_NOT_IMPL);
#endif
		break;

	default:
		DEBUGP("UNKNOWN\n");
		return USB_ERR(USB_ERR_CMD_UNKNOWN);
		break;
	}

	if (poh->flags & OPENPCD_FLAG_RESPOND)
		return USB_RET_RESPOND;
	return 0;
}

void usbcmd_gen_init(void)
{
	DEBUGP("Inititalizing usbcmd_gen_init\n\r");
	/* setup FLASH write support for environment storage */
	flash_init();
	
	/* retrieve default data from flash */
	memcpy(&config_stack,CONFIG_AREA_ADDR,sizeof(config_stack));
	
	usb_hdlr_register(&gen_usb_rx, OPENPCD_CMD_CLS_GENERIC);
}