1 /* Some generel USB API commands, common between OpenPCD and OpenPICC
2 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
8 #include <os/req_ctx.h>
9 #include <os/usb_handler.h>
16 #include <rc632_highlevel.h>
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
;
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 */
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
);
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
);
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
);
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
;
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
);
82 case OPENPCD_CMD_SET_ENVIRONMENT
:
83 DEBUGP("CMD_SET_ENVIRONMENT (in_len=%u)\n", len
);
84 gen_setenv(&poh
->data
,len
);
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
);
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
);
100 case OPENPCD_CMD_SET_LED
:
101 DEBUGP("SET LED(%u,%u)\n", poh
->reg
, poh
->val
);
102 led_switch(poh
->reg
, poh
->val
);
105 case OPENPCD_CMD_GET_SERIAL
:
106 DEBUGP("GET SERIAL(");
107 poh
->flags
|= OPENPCD_FLAG_RESPOND
;
110 if (rc632_get_serial(NULL
, (u_int32_t
*)poh
->data
) < 0) {
112 return USB_ERR(USB_ERR_CMD_NOT_IMPL
);
115 DEBUGP("%s)\n", hexdump(poh
->data
, 4));
117 /* FIXME: where to get serial in PICC case */
118 return USB_ERR(USB_ERR_CMD_NOT_IMPL
);
124 return USB_ERR(USB_ERR_CMD_UNKNOWN
);
128 if (poh
->flags
& OPENPCD_FLAG_RESPOND
)
129 return USB_RET_RESPOND
;
133 void usbcmd_gen_init(void)
135 DEBUGP("Inititalizing usbcmd_gen_init\n");
136 /* setup FLASH write support for environment storage */
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
);