| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "../software/include/base/version.h" |
| #include "libc.h" |
| #include "progmem.h" |
| #include "comloc.h" |
| #include "io.h" |
| #include "debug.h" |
| #include "sie.h" |
| #include "timer.h" |
| #include "host.h" |
| #include "crc.h" |
|
|
| #define SETUP_DATA_RETRIES 250 |
| #define SETUP_END_RETRIES 100 |
| #define ENUM_RETRIES 4 |
| #define ENUM_RESETS 3 |
|
|
| |
|
|
| #ifdef TRIGGER |
|
|
| #define TRIGGER_ON() wio8(SIE_SEL_TX, 3) |
| #define TRIGGER_OFF() wio8(SIE_SEL_TX, 2) |
|
|
| #else |
|
|
| #define TRIGGER_ON() ((void) 0) |
| #define TRIGGER_OFF() ((void) 0) |
|
|
| #endif |
|
|
| enum { |
| USB_PID_OUT = 0xe1, |
| USB_PID_IN = 0x69, |
| USB_PID_SETUP = 0x2d, |
| USB_PID_DATA0 = 0xc3, |
| USB_PID_DATA1 = 0x4b, |
| USB_PID_SOF = 0xa5, |
| USB_PID_ACK = 0xd2, |
| USB_PID_NAK = 0x5a, |
| }; |
|
|
| enum { |
| USB_DT_DEVICE = 1, |
| USB_DT_CONFIG = 2, |
| USB_DT_INTERFACE = 4, |
| USB_DT_ENDPOINT = 5, |
| USB_DT_HID = 33, |
| }; |
|
|
| enum { |
| USB_CLASS_AUDIO = 1, |
| USB_CLASS_HID = 3, |
| }; |
|
|
| enum { |
| USB_SUBCLASS_BOOT = 1, |
| }; |
|
|
| enum { |
| USB_SUBCLASS_MIDISTREAMING = 3, |
| }; |
|
|
| enum { |
| USB_PROTO_KEYBOARD = 1, |
| USB_PROTO_MOUSE = 2, |
| }; |
|
|
| enum { |
| PORT_STATE_DISCONNECTED = 0, |
| PORT_STATE_BUS_RESET, |
| PORT_STATE_RESET_WAIT, |
| PORT_STATE_SET_ADDRESS, |
| PORT_STATE_GET_EP0_SIZE, |
| PORT_STATE_GET_DEVICE_DESCRIPTOR, |
| PORT_STATE_GET_CONFIGURATION_DESCRIPTOR, |
| PORT_STATE_SET_CONFIGURATION, |
| PORT_STATE_GET_HID_REPORT_DESCRIPTOR, |
| PORT_STATE_RUNNING, |
| PORT_STATE_UNSUPPORTED |
| }; |
|
|
| #define RESET_RECOVERY_MS 50 |
|
|
| struct ep_status { |
| char ep; |
| char itf; |
| unsigned int report_len; |
| unsigned char expected_data; |
| char report_id; |
| }; |
|
|
| struct port_status { |
| char state; |
| char full_speed; |
| char retry_count; |
| char reset_count; |
| unsigned int unreset_frame; |
| unsigned char ep0_size; |
| struct ep_status keyboard; |
| struct ep_status mouse; |
| struct ep_status midi; |
| }; |
|
|
| static struct port_status port_a; |
| static struct port_status port_b; |
|
|
| static unsigned int frame_nr; |
|
|
| #define ADDR_EP(addr, ep) ((addr) | (ep) << 7) |
| #define ADDR 1 |
|
|
|
|
| static void make_usb_token(unsigned char pid, unsigned int elevenbits, unsigned char *out) |
| { |
| out[0] = pid; |
| out[1] = elevenbits & 0xff; |
| out[2] = (elevenbits & 0x700) >> 8; |
| out[2] |= usb_crc5(out[1], out[2]) << 3; |
| } |
|
|
| static void usb_tx_nowait(const unsigned char *buf, unsigned char len) |
| { |
| unsigned char i; |
|
|
| wio8(SIE_TX_DATA, 0x80); |
| for(i=0;i<len;i++) { |
| while(rio8(SIE_TX_PENDING)); |
| wio8(SIE_TX_DATA, buf[i]); |
| } |
| while(rio8(SIE_TX_PENDING)); |
| wio8(SIE_TX_VALID, 0); |
| } |
|
|
| static void usb_tx(const unsigned char *buf, unsigned char len) |
| { |
| usb_tx_nowait(buf, len); |
| while(rio8(SIE_TX_BUSY)); |
| } |
|
|
|
|
| static inline void usb_ack(void) |
| { |
| wio8(SIE_TX_DATA, 0x80); |
| while(rio8(SIE_TX_PENDING)); |
| wio8(SIE_TX_DATA, USB_PID_ACK); |
| while(rio8(SIE_TX_PENDING)); |
| wio8(SIE_TX_VALID, 0); |
| while(rio8(SIE_TX_BUSY)); |
| } |
|
|
| static const char ack_error[] PROGMEM = "ACK: "; |
| static const char timeout_error[] PROGMEM = "RX timeout error\n"; |
| static const char bitstuff_error[] PROGMEM = "RX bitstuff error\n"; |
|
|
| #define WAIT_ACTIVE() \ |
| do { \ |
| unsigned timeout = 0x200; \ |
| while(!rio8(SIE_RX_ACTIVE)) { \ |
| if(!--timeout) \ |
| goto timeout; \ |
| if(rio8(SIE_RX_ERROR)) \ |
| goto error; \ |
| } \ |
| } while (0) |
|
|
| #define WAIT_RX(end) \ |
| do { \ |
| unsigned timeout = 0x200; \ |
| unsigned char status; \ |
| while(1) { \ |
| status = rio8(SIE_RX_STATUS); \ |
| if(status & RX_PENDING) \ |
| break; \ |
| if(!(status & RX_ACTIVE)) \ |
| goto end; \ |
| if(!--timeout) \ |
| goto timeout; \ |
| if(rio8(SIE_RX_ERROR)) \ |
| goto error; \ |
| } \ |
| } while (0) |
|
|
|
|
| static char usb_rx_ack(void) |
| { |
| unsigned char pid; |
| unsigned char i; |
|
|
| |
| WAIT_ACTIVE(); |
|
|
| |
| WAIT_RX(nothing); |
| pid = rio8(SIE_RX_DATA); |
|
|
| |
| for(i = 200; i; i--) |
| if(!rio8(SIE_RX_ACTIVE)) |
| break; |
|
|
| if(pid == USB_PID_ACK) |
| return 1; |
| if(pid == USB_PID_NAK) |
| return 0; |
|
|
| for(i = 200; i; i--) |
| WAIT_RX(out); |
| out: |
| print_string(ack_error); |
| print_hex(pid); |
| print_char('\n'); |
| return -1; |
|
|
| timeout: |
| print_string(timeout_error); |
| nothing: |
| return 0; |
| error: |
| print_string(bitstuff_error); |
| return 0; |
| } |
|
|
| static const char in_reply[] PROGMEM = "IN reply:\n"; |
| static const char datax_mismatch[] PROGMEM = "DATAx mismatch\n"; |
|
|
| static int usb_in(unsigned addr, unsigned char expected_data, |
| unsigned char *buf, unsigned char maxlen) |
| { |
| unsigned char in[3]; |
| unsigned char len = 1; |
| unsigned char i; |
|
|
| |
| make_usb_token(USB_PID_IN, addr, in); |
| usb_tx(in, 3); |
|
|
| |
| WAIT_ACTIVE(); |
|
|
| |
| WAIT_RX(nothing); |
| buf[0] = rio8(SIE_RX_DATA); |
|
|
| if(buf[0] == expected_data) |
| goto receive; |
| if(buf[0] == USB_PID_DATA0 || buf[0] == USB_PID_DATA1) |
| goto ignore; |
| if(buf[0] == USB_PID_NAK) |
| return 0; |
|
|
| |
|
|
| while(len != maxlen) { |
| WAIT_RX(fail); |
| buf[len++] = rio8(SIE_RX_DATA); |
| } |
| fail: |
| print_string(in_reply); |
| dump_hex(buf, len); |
| return -1; |
|
|
| |
|
|
| ignore: |
| for(i = 200; i; i--) |
| WAIT_RX(ignore_eop); |
| goto complain; |
| ignore_eop: |
| usb_ack(); |
| complain: |
| print_string(datax_mismatch); |
| return 0; |
|
|
| |
|
|
| receive: |
| while(1) { |
| WAIT_RX(eop); |
| if(len == maxlen) |
| goto discard; |
| buf[len++] = rio8(SIE_RX_DATA); |
| } |
| eop: |
| usb_ack(); |
| return len; |
|
|
| discard: |
| for(i = 200; i; i--) |
| WAIT_RX(nothing); |
| nothing: |
| return 0; |
|
|
| timeout: |
| |
| return 0; |
|
|
| error: |
| print_string(bitstuff_error); |
| return 0; |
| } |
|
|
| static const char out_reply[] PROGMEM = "OUT/DATA reply:\n"; |
|
|
| static char usb_out(unsigned addr, const unsigned char *buf, unsigned char len) |
| { |
| unsigned char out[3]; |
|
|
| |
| make_usb_token(USB_PID_OUT, addr, out); |
| usb_tx_nowait(out, 3); |
|
|
| |
| usb_tx(buf, len); |
|
|
| |
| return usb_rx_ack(); |
| } |
|
|
| struct setup_packet { |
| unsigned char bmRequestType; |
| unsigned char bRequest; |
| unsigned char wValue[2]; |
| unsigned char wIndex[2]; |
| unsigned char wLength[2]; |
| } __attribute__((packed)); |
|
|
| static inline unsigned char toggle(unsigned char old) |
| { |
| return old ^ USB_PID_DATA0 ^ USB_PID_DATA1; |
| } |
|
|
| static const char setup_ack[] PROGMEM = "SETUP not ACKed\n"; |
|
|
| static int control_transfer(unsigned char addr, struct setup_packet *p, |
| char out, unsigned char *payload, int maxlen, unsigned char ep0_size) |
| { |
| unsigned char setup[11]; |
| unsigned char usb_buffer[64+2+1]; |
| unsigned char expected_data = USB_PID_DATA1; |
| char rxlen; |
| char transferred; |
| char chunklen; |
| unsigned char retry; |
|
|
| |
| make_usb_token(USB_PID_SETUP, addr, setup); |
| |
| usb_buffer[0] = USB_PID_DATA0; |
| memcpy(&usb_buffer[1], p, 8); |
| usb_crc16(&usb_buffer[1], 8, &usb_buffer[9]); |
|
|
| TRIGGER_ON(); |
|
|
| |
| usb_tx_nowait(setup, 3); |
| usb_tx(usb_buffer, 11); |
|
|
| TRIGGER_OFF(); |
|
|
| |
| if(usb_rx_ack() != 1) { |
| print_string(setup_ack); |
| return -1; |
| } |
|
|
| |
| transferred = 0; |
| retry = SETUP_DATA_RETRIES; |
| if(out) { |
| while(1) { |
| chunklen = maxlen - transferred; |
| if(chunklen == 0) |
| break; |
| if(chunklen > ep0_size) |
| chunklen = ep0_size; |
|
|
| |
| usb_buffer[0] = expected_data; |
| memcpy(&usb_buffer[1], payload, chunklen); |
| usb_crc16(&usb_buffer[1], chunklen, &usb_buffer[chunklen+1]); |
| rxlen = usb_out(addr, usb_buffer, chunklen+3); |
| if(!rxlen && retry--) |
| continue; |
| if(rxlen <= 0) |
| return -1; |
|
|
| expected_data = toggle(expected_data); |
| transferred += chunklen; |
| payload += chunklen; |
| if(chunklen < ep0_size) |
| break; |
| } |
| } else |
| while(transferred != maxlen) { |
| rxlen = usb_in(addr, expected_data, usb_buffer, |
| ep0_size+3); |
| if(!rxlen && retry--) |
| continue; |
| if(rxlen <= 0) |
| return -1; |
|
|
| expected_data = toggle(expected_data); |
| chunklen = rxlen - 3; |
| if(chunklen > (maxlen - transferred)) |
| chunklen = maxlen - transferred; |
| memcpy(payload, &usb_buffer[1], chunklen); |
|
|
| transferred += chunklen; |
| payload += chunklen; |
| if(chunklen < ep0_size) |
| break; |
| } |
|
|
| |
| retry = SETUP_END_RETRIES; |
| retry: |
| if(out) { |
| rxlen = usb_in(addr, USB_PID_DATA1, usb_buffer, 11); |
| if(!rxlen && retry--) |
| goto retry; |
| if(rxlen <= 0) |
| return -1; |
| } else { |
| |
| usb_buffer[0] = USB_PID_DATA1; |
| usb_buffer[1] = usb_buffer[2] = 0x00; |
|
|
| rxlen = usb_out(addr, usb_buffer, 3); |
| if(!rxlen && retry--) |
| goto retry; |
| if(rxlen <= 0) |
| return -1; |
| } |
|
|
| return transferred; |
| } |
|
|
| static char process_keyboard(struct ep_status *ep, |
| unsigned char *buf, unsigned char len) |
| { |
| unsigned char m, i; |
|
|
| if(len < 6) |
| return 0; |
| m = COMLOC_KEVT_PRODUCE; |
| for(i = 0; i < 8; i++) |
| COMLOC_KEVT(8*m+i) = buf[i]; |
| COMLOC_KEVT_PRODUCE = (m + 1) & 0x07; |
| return 1; |
| } |
|
|
| static char process_mouse(struct ep_status *ep, unsigned char *buf, |
| unsigned char len) |
| { |
| unsigned char m, i; |
|
|
| if(ep->report_id) { |
| buf++; |
| len--; |
| } |
|
|
| if(len < 3) |
| return 0; |
| |
| |
| |
| |
| |
| if(len == 6) |
| buf[2] = buf[3]; |
|
|
| if(len > 4) |
| len = 4; |
| m = COMLOC_MEVT_PRODUCE; |
| for(i = 0; i < len; i++) |
| COMLOC_MEVT(4*m+i) = buf[i]; |
| while(i < 4) { |
| COMLOC_MEVT(4*m+i) = 0; |
| i++; |
| } |
| COMLOC_MEVT_PRODUCE = (m + 1) & 0x0f; |
| return 1; |
| } |
|
|
| static char process_midi(struct ep_status *ep, unsigned char *buf, |
| unsigned char len) |
| { |
| unsigned char end = len & ~3; |
| unsigned char i, m, j; |
| char sent_something = 0; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| for(i = 0; i != end; i += 4) { |
| unsigned char type = buf[i+1] & 0xf0; |
|
|
| |
| if(type < 0x80 || type == 0xf0) |
| continue; |
| m = COMLOC_MIDI_PRODUCE; |
| for(j = 0; j != 4; j++) |
| COMLOC_MIDI(4*m+j) = buf[i+j]; |
| COMLOC_MIDI_PRODUCE = (m + 1) & 15; |
| sent_something = 1; |
| } |
| return sent_something; |
| } |
|
|
| static void poll(struct ep_status *ep, |
| char (*process)(struct ep_status *ep, unsigned char *buf, |
| unsigned char len)) |
| { |
| unsigned char usb_buffer[1+64+2]; |
| int len; |
|
|
| len = usb_in(ADDR_EP(ADDR, ep->ep), ep->expected_data, usb_buffer, |
| sizeof(usb_buffer)); |
| if(len <= 0) |
| return; |
| ep->expected_data = toggle(ep->expected_data); |
|
|
| if(len <= 3) |
| return; |
| if(process(ep, usb_buffer+1, len-3)) |
| wio8(HOST_IRQ, 1); |
| } |
|
|
| static const char connect_fs[] PROGMEM = "Full speed device on port "; |
| static const char connect_ls[] PROGMEM = "Low speed device on port "; |
| static const char disconnect[] PROGMEM = "Device disconnect on port "; |
|
|
| static void check_discon(struct port_status *p, char name) |
| { |
| char discon; |
|
|
| if(name == 'A') |
| discon = rio8(SIE_LINE_STATUS_A) == 0x00; |
| else |
| discon = rio8(SIE_LINE_STATUS_B) == 0x00; |
| if(discon) { |
| print_string(disconnect); print_char(name); print_char('\n'); |
| p->state = PORT_STATE_DISCONNECTED; |
| p->reset_count = 0; |
| p->keyboard.ep = p->mouse.ep = p->midi.ep = 0; |
| } |
| } |
|
|
| static struct ep_status *identify_protocol(const unsigned char *itf, |
| struct port_status *p) |
| { |
| |
| if(itf[5] == USB_CLASS_HID && itf[6] == USB_SUBCLASS_BOOT) |
| switch(itf[7]) { |
| case USB_PROTO_KEYBOARD: |
| p->keyboard.itf = itf[2]; |
| return &p->keyboard; |
| case USB_PROTO_MOUSE: |
| p->mouse.itf = itf[2]; |
| return &p->mouse; |
| default: |
| |
| return NULL; |
| } |
| if(itf[5] == USB_CLASS_AUDIO && itf[6] == USB_SUBCLASS_MIDISTREAMING) |
| return &p->midi; |
|
|
| return NULL; |
| } |
|
|
| static const char found[] PROGMEM = "Found "; |
| static const char unsupported_device[] PROGMEM = "unsupported device\n"; |
| static const char mouse[] PROGMEM = "mouse\n"; |
| static const char keyboard[] PROGMEM = "keyboard\n"; |
| static const char midi[] PROGMEM = "MIDI\n"; |
|
|
| static char validate_report_descriptor(const unsigned char *descriptor, |
| unsigned int len, struct port_status *p) |
| { |
| unsigned int i; |
|
|
| |
| if(len > 512) |
| return 0; |
|
|
| p->mouse.report_id = 0; |
| for (i = 0; i < len; i+=2) { |
| |
| if((descriptor[i] & 0xfc) == 0x84) { |
| p->mouse.report_id = 1; |
| break; |
| } |
| } |
|
|
| return 1; |
| } |
|
|
| static char validate_configuration_descriptor(const unsigned char *descriptor, |
| char len, struct port_status *p) |
| { |
| struct ep_status *ep = NULL; |
| char offset; |
|
|
| p->keyboard.report_len = 0; |
| p->mouse.report_len = 0; |
|
|
| offset = 0; |
| while(offset < len) { |
| switch(descriptor[offset+1]) { |
| case USB_DT_INTERFACE: |
| ep = identify_protocol(descriptor+offset, p); |
| break; |
| case USB_DT_HID: |
| if(!ep) |
| break; |
| ep->report_len = descriptor[offset+8] << 8 | |
| descriptor[offset+7]; |
| break; |
| case USB_DT_ENDPOINT: |
| if(!ep) |
| break; |
| if(!(descriptor[offset+2] & 0x80)) |
| break; |
| ep->ep = descriptor[offset+2] & 0x7f; |
| ep->expected_data = USB_PID_DATA0; |
| |
| ep = NULL; |
| break; |
| default: |
| break; |
| } |
| offset += descriptor[offset+0]; |
| } |
| if(p->keyboard.ep) { |
| print_string(found); |
| print_string(keyboard); |
| } |
| if(p->mouse.ep) { |
| print_string(found); |
| print_string(mouse); |
| } |
| if(p->midi.ep) { |
| print_string(found); |
| print_string(midi); |
| } |
| return p->keyboard.ep || p->mouse.ep || p->midi.ep; |
| } |
|
|
| static void unsupported(struct port_status *p) |
| { |
| print_string(found); |
| print_string(unsupported_device); |
| p->state = PORT_STATE_UNSUPPORTED; |
| } |
|
|
| static const char retry_reset[] PROGMEM = |
| "Retry count exceeded, resetting device.\n"; |
| static const char retry_exceed[] PROGMEM = |
| "Retry count exceeded, disabling device.\n"; |
|
|
| static void check_retry(struct port_status *p) |
| { |
| if(p->retry_count++ <= ENUM_RETRIES) |
| return; |
| if(p->reset_count++ <= ENUM_RESETS) { |
| print_string(retry_reset); |
| p->state = PORT_STATE_DISCONNECTED; |
| return; |
| } |
| print_string(retry_exceed); |
| p->state = PORT_STATE_UNSUPPORTED; |
| } |
|
|
| static const char vid[] PROGMEM = "VID: "; |
| static const char pid[] PROGMEM = ", PID: "; |
|
|
| static int get_device_descriptor(unsigned char *buf, int size, |
| unsigned char ep0_size) |
| { |
| struct setup_packet packet; |
|
|
| packet.bmRequestType = 0x80; |
| packet.bRequest = 0x06; |
| packet.wValue[0] = 0x00; |
| packet.wValue[1] = USB_DT_DEVICE; |
| packet.wIndex[0] = 0x00; |
| packet.wIndex[1] = 0x00; |
| packet.wLength[0] = size; |
| packet.wLength[1] = 0x00; |
|
|
| return control_transfer(ADDR, &packet, 0, buf, size, ep0_size); |
| } |
|
|
| static char get_ep0_size(struct port_status *p) |
| { |
| unsigned char device_descriptor[8]; |
| unsigned char size; |
|
|
| if(get_device_descriptor(device_descriptor, 8, 8) != 8) |
| return 0; |
|
|
| size = device_descriptor[7]; |
| if(size != 8 && size != 16 && size != 32 && size != 64) |
| return 0; |
|
|
| p->ep0_size = size; |
|
|
| return 1; |
| } |
|
|
| static void port_service(struct port_status *p, char name) |
| { |
| if(p->state > PORT_STATE_BUS_RESET) |
| |
| |
| |
| check_discon(p, name); |
| if(p->full_speed) |
| wio8(SIE_TX_LOW_SPEED, 0); |
| else |
| wio8(SIE_TX_LOW_SPEED, 1); |
| switch(p->state) { |
| case PORT_STATE_DISCONNECTED: { |
| char linestat; |
| if(name == 'A') |
| linestat = rio8(SIE_LINE_STATUS_A); |
| else |
| linestat = rio8(SIE_LINE_STATUS_B); |
| if(linestat == 0x01) { |
| print_string(connect_fs); print_char(name); print_char('\n'); |
| p->full_speed = 1; |
| } |
| if(linestat == 0x02) { |
| print_string(connect_ls); print_char(name); print_char('\n'); |
| p->full_speed = 0; |
| } |
| if((linestat == 0x01)||(linestat == 0x02)) { |
| if(name == 'A') |
| wio8(SIE_TX_BUSRESET, rio8(SIE_TX_BUSRESET) | 0x01); |
| else |
| wio8(SIE_TX_BUSRESET, rio8(SIE_TX_BUSRESET) | 0x02); |
| p->state = PORT_STATE_BUS_RESET; |
| p->unreset_frame = (frame_nr + 350) & 0x7ff; |
| } |
| p->ep0_size = 8; |
| break; |
| } |
| case PORT_STATE_BUS_RESET: |
| if(frame_nr != p->unreset_frame) |
| break; |
| if(name == 'A') |
| wio8(SIE_TX_BUSRESET, rio8(SIE_TX_BUSRESET) & 0x02); |
| else |
| wio8(SIE_TX_BUSRESET, rio8(SIE_TX_BUSRESET) & 0x01); |
| p->state = PORT_STATE_RESET_WAIT; |
| p->unreset_frame = |
| (frame_nr + RESET_RECOVERY_MS) & 0x7ff; |
| break; |
| case PORT_STATE_RESET_WAIT: |
| if(frame_nr != p->unreset_frame) |
| break; |
| p->state = PORT_STATE_SET_ADDRESS; |
| p->retry_count = 0; |
| break; |
| case PORT_STATE_SET_ADDRESS: { |
| struct setup_packet packet; |
|
|
| packet.bmRequestType = 0x00; |
| packet.bRequest = 0x05; |
| packet.wValue[0] = ADDR; |
| packet.wValue[1] = 0x00; |
| packet.wIndex[0] = 0x00; |
| packet.wIndex[1] = 0x00; |
| packet.wLength[0] = 0x00; |
| packet.wLength[1] = 0x00; |
|
|
| if(control_transfer(0x00, &packet, 1, NULL, 0, |
| p->ep0_size) == 0) { |
| p->retry_count = 0; |
| p->state = PORT_STATE_GET_EP0_SIZE; |
| } |
| check_retry(p); |
| break; |
| } |
| case PORT_STATE_GET_EP0_SIZE: |
| if(get_ep0_size(p)) { |
| p->state = PORT_STATE_GET_DEVICE_DESCRIPTOR; |
| p->retry_count = 0; |
| } |
| check_retry(p); |
| break; |
| case PORT_STATE_GET_DEVICE_DESCRIPTOR: { |
| unsigned char device_descriptor[18]; |
|
|
| if(get_device_descriptor(device_descriptor, 18, |
| p->ep0_size) >= 0) { |
| p->retry_count = 0; |
| print_string(vid); |
| print_hex(device_descriptor[9]); |
| print_hex(device_descriptor[8]); |
| print_string(pid); |
| print_hex(device_descriptor[11]); |
| print_hex(device_descriptor[10]); |
| print_char('\n'); |
| |
| |
| |
| if((device_descriptor[4] != 0) || (device_descriptor[5] != 0)) |
| unsupported(p); |
| else |
| p->state = PORT_STATE_GET_CONFIGURATION_DESCRIPTOR; |
| } |
| check_retry(p); |
| break; |
| } |
| case PORT_STATE_GET_CONFIGURATION_DESCRIPTOR: { |
| struct setup_packet packet; |
| unsigned char configuration_descriptor[127]; |
| char len; |
|
|
| packet.bmRequestType = 0x80; |
| packet.bRequest = 0x06; |
| packet.wValue[0] = 0x00; |
| packet.wValue[1] = USB_DT_CONFIG; |
| packet.wIndex[0] = 0x00; |
| packet.wIndex[1] = 0x00; |
| packet.wLength[0] = 127; |
| packet.wLength[1] = 0x00; |
|
|
| len = control_transfer(ADDR, &packet, 0, |
| configuration_descriptor, 127, p->ep0_size); |
| if(len >= 0) { |
| p->retry_count = 0; |
| if(!validate_configuration_descriptor( |
| configuration_descriptor, len, p)) |
| unsupported(p); |
| else |
| p->state = PORT_STATE_SET_CONFIGURATION; |
| } |
| check_retry(p); |
| break; |
| } |
| case PORT_STATE_SET_CONFIGURATION: { |
| struct setup_packet packet; |
|
|
| packet.bmRequestType = 0x00; |
| packet.bRequest = 0x09; |
| packet.wValue[0] = 0x01; |
| packet.wValue[1] = 0x00; |
| packet.wIndex[0] = 0x00; |
| packet.wIndex[1] = 0x00; |
| packet.wLength[0] = 0x00; |
| packet.wLength[1] = 0x00; |
|
|
| if(control_transfer(ADDR, &packet, 1, NULL, 0, |
| p->ep0_size) == 0) { |
| p->retry_count = 0; |
| p->state = PORT_STATE_GET_HID_REPORT_DESCRIPTOR; |
| } |
| check_retry(p); |
| break; |
| } |
| case PORT_STATE_GET_HID_REPORT_DESCRIPTOR: { |
| struct setup_packet packet; |
| unsigned char report_descriptor[512]; |
| int len; |
|
|
| |
| if(!p->mouse.ep) { |
| p->state = PORT_STATE_RUNNING; |
| break; |
| } |
|
|
| packet.bmRequestType = 0x81; |
| packet.bRequest = 0x06; |
| packet.wValue[0] = 0x01; |
| packet.wValue[1] = 0x22; |
| packet.wIndex[0] = p->mouse.itf; |
| packet.wIndex[1] = 0x00; |
| packet.wLength[0] = p->mouse.report_len & 0x00ff; |
| packet.wLength[1] = (p->mouse.report_len & 0xff00) >> 8; |
|
|
| len = control_transfer(ADDR, &packet, 0, |
| report_descriptor, |
| p->mouse.report_len, |
| p->ep0_size); |
|
|
| if(len != -1) { |
| if(!validate_report_descriptor( |
| report_descriptor, p->mouse.report_len, p)) |
| unsupported(p); |
| } |
| p->retry_count = 0; |
| p->state = PORT_STATE_RUNNING;; |
| check_retry(p); |
| break; |
| } |
| case PORT_STATE_RUNNING: |
| if(p->keyboard.ep) |
| poll(&p->keyboard, process_keyboard); |
| if(p->mouse.ep) |
| poll(&p->mouse, process_mouse); |
| if(p->midi.ep) |
| poll(&p->midi, process_midi); |
| break; |
| case PORT_STATE_UNSUPPORTED: |
| break; |
| } |
| while(rio8(SIE_TX_BUSY)); |
| } |
|
|
| static const char banner[] PROGMEM = "softusb-input v"VERSION"\n"; |
|
|
| static void sof(void) |
| { |
| unsigned char mask; |
| unsigned char usb_buffer[3]; |
|
|
| mask = 0; |
| #ifndef TRIGGER |
| if(port_a.full_speed && (port_a.state > PORT_STATE_RESET_WAIT)) |
| mask |= 0x01; |
| #endif |
| if(port_b.full_speed && (port_b.state > PORT_STATE_RESET_WAIT)) |
| mask |= 0x02; |
| if(mask != 0) { |
| wio8(SIE_TX_LOW_SPEED, 0); |
| wio8(SIE_SEL_TX, mask); |
| make_usb_token(USB_PID_SOF, frame_nr, usb_buffer); |
| usb_tx(usb_buffer, 3); |
| } |
| } |
|
|
| static void keepalive(void) |
| { |
| unsigned char mask; |
|
|
| mask = 0; |
| #ifndef TRIGGER |
| if(!port_a.full_speed && (port_a.state == PORT_STATE_RESET_WAIT)) |
| mask |= 0x01; |
| #endif |
| if(!port_b.full_speed && (port_b.state == PORT_STATE_RESET_WAIT)) |
| mask |= 0x02; |
| if(mask != 0) { |
| wio8(SIE_TX_LOW_SPEED, 1); |
| wio8(SIE_SEL_TX, mask); |
| wio8(SIE_GENERATE_EOP, 1); |
| while(rio8(SIE_TX_BUSY)); |
| } |
| } |
|
|
| static void set_rx_speed(void) |
| { |
| unsigned char mask; |
|
|
| mask = 0; |
| if(!port_a.full_speed) mask |= 0x01; |
| if(!port_b.full_speed) mask |= 0x02; |
| wio8(SIE_LOW_SPEED, mask); |
| } |
|
|
| int main(void) |
| { |
| unsigned char i; |
|
|
| print_string(banner); |
|
|
| wio8(TIMER0, 0); |
| while(1) { |
| |
| while((rio8(TIMER2) < 0x01) || (rio8(TIMER1) < 0xbb) || (rio8(TIMER0) < 0x70)); |
| wio8(TIMER0, 0); |
|
|
| sof(); |
| keepalive(); |
|
|
| |
| |
| |
| |
| |
| for(i=0;i<128;i++) |
| asm("nop"); |
|
|
| #ifndef TRIGGER |
| wio8(SIE_SEL_RX, 0); |
| wio8(SIE_SEL_TX, 0x01); |
| port_service(&port_a, 'A'); |
| #endif |
|
|
| wio8(SIE_SEL_RX, 1); |
| wio8(SIE_SEL_TX, 0x02); |
| port_service(&port_b, 'B'); |
|
|
| |
| set_rx_speed(); |
|
|
| frame_nr = (frame_nr + 1) & 0x7ff; |
| } |
| return 0; |
| } |
|
|