Initial commit
[kk_librfid.git] / firmware / src / os / usbcmd_generic.c
blob77b992f94a45de373aa929f40bf81af301796db5
1 /* Some generel USB API commands, common between OpenPCD and OpenPICC
2 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
3 */
5 #include <string.h>
6 #include <sys/types.h>
7 #include <openpcd.h>
8 #include <os/req_ctx.h>
9 #include <os/usb_handler.h>
10 #include <os/led.h>
11 #include <os/dbgu.h>
12 #include <os/main.h>
13 #include <os/flash.h>
14 #include <board.h>
15 #ifdef PCD
16 #include <rc632_highlevel.h>
17 #endif/*PCD*/
19 #define OPENPCD_API_VERSION (0x01)
20 #define CONFIG_AREA_ADDR ((void*)(AT91C_IFLASH + AT91C_IFLASH_SIZE - ENVIRONMENT_SIZE))
21 #define CONFIG_AREA_WORDS ( AT91C_IFLASH_PAGE_SIZE/sizeof(u_int32_t) )
23 volatile u_int32_t config_stack[ CONFIG_AREA_WORDS ];
25 static int gen_setenv(const void* buffer,int len)
27 volatile unsigned int i;
28 u_int32_t *dst;
30 if( len >= sizeof(config_stack) )
31 len=sizeof(config_stack);
33 memcpy(&config_stack,buffer,len);
35 /* retrieve current content to allow partial flashing */
37 /* flash changes */
38 dst=(u_int32_t*)CONFIG_AREA_ADDR;
39 for(i=0;i<CONFIG_AREA_WORDS;i++)
40 *dst++=config_stack[i];
42 flash_page(CONFIG_AREA_ADDR);
44 return len;
47 static int gen_getenv(void* buffer,int len)
49 if( len >= sizeof(config_stack) )
50 len=sizeof(config_stack);
52 memcpy(buffer,&config_stack,len);
54 return len;
57 static int gen_usb_rx(struct req_ctx *rctx)
59 struct openpcd_hdr *poh = (struct openpcd_hdr *) rctx->data;
60 struct openpcd_compile_version *ver =
61 (struct openpcd_compile_version *)poh->data;
62 u_int32_t len = rctx->tot_len-sizeof(*poh);
64 /* initialize transmit length to header length */
65 rctx->tot_len = sizeof(*poh);
67 switch (poh->cmd) {
69 case OPENPCD_CMD_GET_API_VERSION:
70 DEBUGP("CMD_GET_API_VERSION\n");
71 poh->flags &= OPENPCD_FLAG_RESPOND;
72 poh->val = OPENPCD_API_VERSION;
73 break;
75 case OPENPCD_CMD_GET_ENVIRONMENT:
76 poh->flags &= OPENPCD_FLAG_RESPOND;
77 poh->val = gen_getenv(&poh->data,poh->val);
78 rctx->tot_len += poh->val;
79 DEBUGP("CMD_GET_ENVIRONMENT(res_len=%u)\n", poh->val);
80 break;
82 case OPENPCD_CMD_SET_ENVIRONMENT:
83 DEBUGP("CMD_SET_ENVIRONMENT (in_len=%u)\n", len);
84 gen_setenv(&poh->data,len);
85 break;
87 case OPENPCD_CMD_RESET:
88 DEBUGP("CMD_RESET\n");
89 AT91F_RSTSoftReset(AT91C_BASE_RSTC, AT91C_RSTC_PROCRST|
90 AT91C_RSTC_PERRST|AT91C_RSTC_EXTRST);
91 break;
93 case OPENPCD_CMD_GET_VERSION:
94 DEBUGP("GET_VERSION\n");
95 poh->flags |= OPENPCD_FLAG_RESPOND;
96 memcpy(ver, &opcd_version, sizeof(*ver));
97 rctx->tot_len += sizeof(*ver);
98 break;
100 case OPENPCD_CMD_SET_LED:
101 DEBUGP("SET LED(%u,%u)\n", poh->reg, poh->val);
102 led_switch(poh->reg, poh->val);
103 break;
105 case OPENPCD_CMD_GET_SERIAL:
106 DEBUGP("GET SERIAL(");
107 poh->flags |= OPENPCD_FLAG_RESPOND;
108 #ifdef PCD
109 rctx->tot_len += 4;
110 if (rc632_get_serial(NULL, (u_int32_t *)poh->data) < 0) {
111 DEBUGP("ERROR) ");
112 return USB_ERR(USB_ERR_CMD_NOT_IMPL);
115 DEBUGP("%s)\n", hexdump(poh->data, 4));
116 #else
117 /* FIXME: where to get serial in PICC case */
118 return USB_ERR(USB_ERR_CMD_NOT_IMPL);
119 #endif
120 break;
122 default:
123 DEBUGP("UNKNOWN\n");
124 return USB_ERR(USB_ERR_CMD_UNKNOWN);
125 break;
128 if (poh->flags & OPENPCD_FLAG_RESPOND)
129 return USB_RET_RESPOND;
130 return 0;
133 void usbcmd_gen_init(void)
135 DEBUGP("Inititalizing usbcmd_gen_init\n");
136 /* setup FLASH write support for environment storage */
137 flash_init();
139 /* retrieve default data from flash */
140 memcpy(&config_stack,CONFIG_AREA_ADDR,sizeof(config_stack));
142 usb_hdlr_register(&gen_usb_rx, OPENPCD_CMD_CLS_GENERIC);