71 #include "serial/uart_usb_lib.h"
72 #include "sicslow_ethernet.h"
73 #include <avr/pgmspace.h>
80 #include <avr/pgmspace.h>
81 #include <util/delay.h>
90 extern PGM_VOID_P pbuffer;
91 extern U8 data_to_transfer;
98 OID_GEN_SUPPORTED_LIST ,
99 OID_GEN_HARDWARE_STATUS ,
100 OID_GEN_MEDIA_SUPPORTED ,
101 OID_GEN_MEDIA_IN_USE ,
102 OID_GEN_MAXIMUM_FRAME_SIZE ,
104 OID_GEN_TRANSMIT_BLOCK_SIZE ,
105 OID_GEN_RECEIVE_BLOCK_SIZE ,
107 OID_GEN_VENDOR_DESCRIPTION ,
108 OID_GEN_CURRENT_PACKET_FILTER ,
109 OID_GEN_MAXIMUM_TOTAL_SIZE ,
110 OID_GEN_MEDIA_CONNECT_STATUS ,
111 OID_GEN_VENDOR_DRIVER_VERSION ,
112 OID_GEN_PHYSICAL_MEDIUM ,
119 OID_GEN_RCV_NO_BUFFER ,
122 OID_GEN_RNDIS_CONFIG_PARAMETER ,
126 OID_802_3_PERMANENT_ADDRESS ,
127 OID_802_3_CURRENT_ADDRESS ,
128 OID_802_3_MULTICAST_LIST ,
129 OID_802_3_MAXIMUM_LIST_SIZE ,
130 OID_802_3_MAC_OPTIONS ,
131 OID_802_3_RCV_ERROR_ALIGNMENT ,
132 OID_802_3_XMIT_ONE_COLLISION ,
133 OID_802_3_XMIT_MORE_COLLISIONS ,
137 #if USB_ETH_EMULATE_WIFI
141 OID_802_11_NETWORK_TYPE_IN_USE ,
143 OID_802_11_BSSID_LIST ,
144 OID_802_11_BSSID_LIST_SCAN ,
145 OID_802_11_INFRASTRUCTURE_MODE ,
146 OID_802_11_SUPPORTED_RATES ,
147 OID_802_11_CONFIGURATION ,
149 OID_802_11_WEP_STATUS ,
150 OID_802_11_REMOVE_WEP ,
151 OID_802_11_DISASSOCIATE ,
152 OID_802_11_AUTHENTICATION_MODE ,
153 OID_802_11_RELOAD_DEFAULTS ,
158 OID_PNP_CAPABILITIES ,
159 OID_PNP_QUERY_POWER ,
162 OID_PNP_ENABLE_WAKE_UP ,
163 OID_PNP_ADD_WAKE_UP_PATTERN ,
164 OID_PNP_REMOVE_WAKE_UP_PATTERN
167 #define OID_LIST_LENGTH sizeof(OIDSupportedList)/sizeof(*OIDSupportedList)
170 rndis_state_t rndis_state;
172 usb_eth_stat_t usb_eth_stat;
174 uint8_t schedule_interrupt = 0;
183 #define ENC_BUF_SIZE (4*OID_LIST_LENGTH + 32)
186 U8 encapsulated_buffer[ENC_BUF_SIZE];
189 U8 data_to_send = 0x00;
205 if (wLength > ENC_BUF_SIZE)
206 wLength = ENC_BUF_SIZE;
216 nb_counter = EP_CONTROL_LENGTH;
223 while(nb_counter && wLength) {
242 switch(((rndis_generic_msg_t *)encapsulated_buffer)->MessageType)
246 case REMOTE_NDIS_INITIALIZE_MSG:
248 rndis_initialize_cmplt_t * m;
249 m = ((rndis_initialize_cmplt_t *)encapsulated_buffer);
252 m->MessageType = REMOTE_NDIS_INITIALIZE_CMPLT;
253 m->MessageLength =
sizeof(rndis_initialize_cmplt_t);
254 m->MajorVersion = RNDIS_MAJOR_VERSION;
255 m->MinorVersion = RNDIS_MAJOR_VERSION;
256 m->Status = RNDIS_STATUS_SUCCESS;
257 m->DeviceFlags = RNDIS_DF_CONNECTIONLESS;
258 #if USB_ETH_EMULATE_WIFI
259 m->Medium = NDIS_MEDIUM_WIRELESS_LAN;
261 m->Medium = RNDIS_MEDIUM_802_3;
262 #endif // USB_ETH_EMULATE_WIFI
263 m->MaxPacketsPerTransfer = 1;
264 m->MaxTransferSize = USB_ETH_MTU + 58;
266 m->PacketAlignmentFactor = 3;
270 rndis_state = rndis_initialized;
272 data_to_send = m->MessageLength;
275 case REMOTE_NDIS_HALT_MSG:
290 case REMOTE_NDIS_QUERY_MSG:
294 case REMOTE_NDIS_SET_MSG:
300 case REMOTE_NDIS_RESET_MSG:
302 rndis_reset_cmplt_t * m;
303 m = ((rndis_reset_cmplt_t *)encapsulated_buffer);
305 rndis_state = rndis_uninitialized;
307 m->MessageType = REMOTE_NDIS_RESET_CMPLT;
308 m->MessageLength =
sizeof(rndis_reset_cmplt_t);
309 m->Status = RNDIS_STATUS_SUCCESS;
310 m->AddressingReset = 1;
312 data_to_send = m->MessageLength;
316 case REMOTE_NDIS_KEEPALIVE_MSG:
318 rndis_keepalive_cmplt_t * m;
319 m = (rndis_keepalive_cmplt_t *)encapsulated_buffer;
320 m->MessageType = REMOTE_NDIS_KEEPALIVE_CMPLT;
321 m->MessageLength =
sizeof(rndis_keepalive_cmplt_t);
322 m->Status = RNDIS_STATUS_SUCCESS;
325 data_to_send = m->MessageLength;
352 schedule_interrupt = 1;
355 #define INFBUF ((uint32_t *)(encapsulated_buffer + sizeof(rndis_query_cmplt_t)))
357 uint32_t oid_packet_filter = 0x0000000;
359 #if USB_ETH_EMULATE_WIFI
360 uint16_t panid = 0xbaad;
368 rndis_query_msg_t * m;
369 rndis_query_cmplt_t * c;
370 rndis_Status_t status = RNDIS_STATUS_SUCCESS;
372 m = (rndis_query_msg_t *)encapsulated_buffer;
373 c = (rndis_query_cmplt_t *)encapsulated_buffer;
379 c->MessageType = REMOTE_NDIS_QUERY_CMPLT;
381 c->InformationBufferLength = 4;
382 c->InformationBufferOffset = 16;
387 case OID_GEN_SUPPORTED_LIST:
388 c->InformationBufferLength = 4 * OID_LIST_LENGTH;
394 case OID_GEN_HARDWARE_STATUS:
395 *INFBUF = 0x00000000;
398 case OID_GEN_MEDIA_SUPPORTED:
399 case OID_GEN_MEDIA_IN_USE:
400 case OID_GEN_PHYSICAL_MEDIUM:
401 #if USB_ETH_EMULATE_WIFI
402 *INFBUF = NDIS_MEDIUM_WIRELESS_LAN;
404 *INFBUF = NDIS_MEDIUM_802_3;
408 case OID_GEN_MAXIMUM_FRAME_SIZE:
409 *INFBUF = (uint32_t) USB_ETH_MTU-14;
412 case OID_GEN_LINK_SPEED:
413 *INFBUF = (uint32_t) 250000/100;
416 case OID_GEN_TRANSMIT_BLOCK_SIZE:
417 case OID_GEN_RECEIVE_BLOCK_SIZE:
418 *INFBUF = (uint32_t) 102;
421 case OID_GEN_VENDOR_ID:
425 case OID_GEN_VENDOR_DESCRIPTION:
426 c->InformationBufferLength = 8;
427 memcpy_P(INFBUF, PSTR(
"Atmel\0\0\0\0"), 8);
430 case OID_GEN_CURRENT_PACKET_FILTER:
431 *INFBUF = oid_packet_filter;
434 case OID_GEN_MAXIMUM_TOTAL_SIZE:
435 *INFBUF = (uint32_t) USB_ETH_MTU;
438 case OID_GEN_MEDIA_CONNECT_STATUS:
439 *INFBUF = usb_eth_is_active?NDIS_MEDIA_STATE_CONNECTED:NDIS_MEDIA_STATE_DISCONNECTED;
442 case OID_GEN_VENDOR_DRIVER_VERSION:
443 *INFBUF = 0x00001000;
447 case OID_GEN_CURRENT_LOOKAHEAD:
448 *INFBUF = (uint32_t) USB_ETH_MTU-14;
456 case OID_802_3_PERMANENT_ADDRESS:
457 case OID_802_3_CURRENT_ADDRESS:
463 usb_eth_get_mac_address((uint8_t*)INFBUF);
466 c->InformationBufferLength += 2;
470 case OID_802_3_MULTICAST_LIST:
475 case OID_802_3_MAXIMUM_LIST_SIZE:
480 case OID_802_3_MAC_OPTIONS:
485 case OID_802_3_RCV_ERROR_ALIGNMENT:
487 case OID_802_3_XMIT_ONE_COLLISION:
489 case OID_802_3_XMIT_MORE_COLLISIONS:
493 #if USB_ETH_EMULATE_WIFI
495 case OID_802_11_BSSID:
496 *INFBUF = (uint32_t)panid;
500 c->InformationBufferLength += 2;
503 case OID_802_11_SSID:
507 strncpy_P((
char*)(INFBUF + 1), PSTR(
"PANID: 0xBAAD"), 13);
510 case OID_802_11_NETWORK_TYPE_IN_USE:
514 case OID_802_11_RSSI:
515 *((int32_t *) INFBUF) = -20;
518 case OID_802_11_BSSID_LIST:
522 case OID_802_11_INFRASTRUCTURE_MODE:
523 case OID_802_11_SUPPORTED_RATES:
524 case OID_802_11_CONFIGURATION:
525 case OID_802_11_WEP_STATUS:
526 case OID_802_11_AUTHENTICATION_MODE:
528 #endif //USB_ETH_EMULATE_WIFI
533 case OID_GEN_XMIT_OK:
534 *INFBUF = usb_eth_stat.txok;
539 *INFBUF = usb_eth_stat.rxok;
543 case OID_GEN_RCV_ERROR:
544 *INFBUF = usb_eth_stat.rxbad;
548 case OID_GEN_XMIT_ERROR:
549 *INFBUF = usb_eth_stat.txbad;
553 case OID_GEN_RCV_NO_BUFFER:
559 case OID_PNP_CAPABILITIES:
560 c->InformationBufferLength =
sizeof(
struct NDIS_PM_WAKE_UP_CAPABILITIES);
563 memset((
char *)INFBUF, 0,
sizeof(
struct NDIS_PM_WAKE_UP_CAPABILITIES));
566 case OID_PNP_QUERY_POWER:
567 c->InformationBufferLength = 0;
570 case OID_PNP_ENABLE_WAKE_UP:
575 status = RNDIS_STATUS_FAILURE;
576 c->InformationBufferLength = 0;
586 c->MessageLength =
sizeof (rndis_query_cmplt_t) + c->InformationBufferLength;
589 if (c->InformationBufferLength == 0) {
590 c->InformationBufferOffset = 0;
594 data_to_send = c->MessageLength;
599 #define INFBUF ((uint32_t *)((uint8_t *)&(m->RequestId) + m->InformationBufferOffset))
600 #define CFGBUF ((rndis_config_parameter_t *) INFBUF)
601 #define PARMNAME ((uint8_t *)CFGBUF + CFGBUF->ParameterNameOffset)
602 #define PARMVALUE ((uint8_t *)CFGBUF + CFGBUF->ParameterValueOffset)
603 #define PARMVALUELENGTH CFGBUF->ParameterValueLength
604 #define PARM_NAME_LENGTH 25
607 rndis_handle_config_parm(
const char* parmname,
const uint8_t* parmvalue,
size_t parmlength) {
608 if (strncmp_P(parmname, PSTR(
"rawmode"), 7) == 0) {
609 if (parmvalue[0] ==
'0') {
610 usbstick_mode.raw = 0;
612 usbstick_mode.raw = 1;
624 rndis_set_cmplt_t * c;
627 c = ((rndis_set_cmplt_t *)encapsulated_buffer);
628 m = ((rndis_set_msg_t *)encapsulated_buffer);
631 char parmname[PARM_NAME_LENGTH];
638 if (CFGBUF->ParameterNameLength > (PARM_NAME_LENGTH*2)) {
639 parmlength = PARM_NAME_LENGTH * 2;
641 parmlength = CFGBUF->ParameterNameLength;
645 while(parmlength > 0) {
647 parmname[i] = (char)*(PARMNAME + 2*i);
656 case OID_GEN_RNDIS_CONFIG_PARAMETER:
662 rndis_handle_config_parm(parmname,PARMVALUE,PARMVALUELENGTH);
668 case OID_GEN_CURRENT_PACKET_FILTER:
669 oid_packet_filter = *INFBUF;
671 if (oid_packet_filter) {
675 rndis_state = rndis_data_initialized;
677 rndis_state = rndis_initialized;
682 case OID_GEN_CURRENT_LOOKAHEAD:
685 case OID_GEN_PROTOCOL_OPTIONS:
689 case OID_802_3_MULTICAST_LIST:
692 #if USB_ETH_EMULATE_WIFI
694 case OID_802_11_BSSID:
698 case OID_802_11_SSID:
701 #endif // USB_ETH_EMULATE_WIFI
704 case OID_PNP_ADD_WAKE_UP_PATTERN:
705 case OID_PNP_REMOVE_WAKE_UP_PATTERN:
706 case OID_PNP_ENABLE_WAKE_UP:
710 c->MessageType = REMOTE_NDIS_SET_CMPLT;
711 c->MessageLength =
sizeof(rndis_set_cmplt_t);
712 c->Status = RNDIS_STATUS_FAILURE;
713 data_to_send = c->MessageLength;
720 c->MessageType = REMOTE_NDIS_SET_CMPLT;
721 c->MessageLength =
sizeof(rndis_set_cmplt_t);
722 c->Status = RNDIS_STATUS_SUCCESS;
723 data_to_send = c->MessageLength;
746 if ((data_to_send % EP_CONTROL_LENGTH) == 0) { zlp =
TRUE; }
747 else { zlp =
FALSE; }
756 while(data_to_send != 0)
758 if(nb_byte++==EP_CONTROL_LENGTH)
798 rndis_indicate_status_t * m;
799 m = (rndis_indicate_status_t *)encapsulated_buffer;
801 m->MessageType = REMOTE_NDIS_INDICATE_STATUS_MSG;
802 m->MessageLength =
sizeof(rndis_indicate_status_t);
805 for(i = 0; i <
sizeof(rndis_indicate_status_t); i++) {
822 void rndis_configure_endpoints() {
823 usb_configure_endpoint(INT_EP, \
829 usb_configure_endpoint(TX_EP, \
836 usb_configure_endpoint(RX_EP, \
848 extern char usb_busy;
850 #define PBUF ((rndis_data_packet_t *) usb_eth_data_buffer)
853 uint8_t rndis_process(
void)
855 uint8_t bytecounter, headercounter;
856 uint16_t i, dataoffset;
858 if(rndis_state != rndis_data_initialized) {
862 #ifdef USB_ETH_HOOK_INIT
863 static uint8_t doInit = 1;
882 headercounter =
sizeof(rndis_data_packet_t);
887 if (bytecounter < headercounter) {
893 while (headercounter) {
901 if(PBUF->MessageType != REMOTE_NDIS_PACKET_MSG) {
908 if (PBUF->DataLength && (fail == 0)) {
911 #ifdef USB_ETH_HOOK_RX_START
912 USB_ETH_HOOK_RX_START();
916 dataoffset = PBUF->DataOffset;
919 dataoffset += (
sizeof(rndis_MessageType_t) +
sizeof(rndis_MessageLength_t));
922 dataoffset -=
sizeof(rndis_data_packet_t);
934 if (bytecounter == 0) {
952 if (bytecounter == 0)
959 uint8_t * uipdata = uip_buf;
960 uint16_t datalen = PBUF->DataLength;
968 if (bytecounter == 0) {
973 if(usb_endpoint_wait_for_receive_out()!=0) {
974 USB_ETH_HOOK_RX_ERROR(
"Timeout: receive out");
986 #ifdef USB_ETH_HOOK_RX_END
987 USB_ETH_HOOK_RX_END();
991 if(PBUF->DataLength <= USB_ETH_MTU) {
992 USB_ETH_HOOK_HANDLE_INBOUND_PACKET(uip_buf,PBUF->DataLength);
994 USB_ETH_HOOK_RX_ERROR(
"Oversized packet");
1005 uint8_t
rndis_send(uint8_t * senddata, uint16_t sendlen, uint8_t led)
1012 PBUF->MessageType = REMOTE_NDIS_PACKET_MSG;
1013 PBUF->DataOffset =
sizeof(rndis_data_packet_t) -
sizeof(rndis_MessageType_t) -
sizeof(rndis_MessageLength_t);
1014 PBUF->DataLength = sendlen;
1015 PBUF->OOBDataLength = 0;
1016 PBUF->OOBDataOffset = 0;
1017 PBUF->NumOOBDataElements = 0;
1018 PBUF->PerPacketInfoOffset = 0;
1019 PBUF->PerPacketInfoLength = 0;
1020 PBUF->DeviceVcHandle = 0;
1022 PBUF->MessageLength =
sizeof(rndis_data_packet_t) + PBUF->DataLength;
1029 if(usb_endpoint_wait_for_write_enabled()!=0) {
1030 USB_ETH_HOOK_TX_ERROR(
"Timeout: write enabled");
1034 #ifdef USB_ETH_HOOK_TX_START
1035 USB_ETH_HOOK_TX_START();
1039 for(i = 0; i <
sizeof(rndis_data_packet_t); i++) {
1054 if(usb_endpoint_wait_for_write_enabled()!=0) {
1055 USB_ETH_HOOK_TX_ERROR(
"Timeout: write enabled");
1064 #ifdef USB_ETH_HOOK_TX_END
1065 USB_ETH_HOOK_TX_END();
1069 if(usb_endpoint_wait_for_IN_ready()!=0) {
1070 USB_ETH_HOOK_TX_ERROR(
"Timeout: IN ready");
1086 if (newfilter & NDIS_PACKET_TYPE_PROMISCUOUS) {
1087 USB_ETH_HOOK_SET_PROMISCIOUS_MODE(
true);
1089 USB_ETH_HOOK_SET_PROMISCIOUS_MODE(
false);