From 478fc5baed82e5573938041aed0f1a4f73f32128 Mon Sep 17 00:00:00 2001 From: Frank Gevaerts Date: Fri, 3 Oct 2008 22:43:16 +0000 Subject: reorganise the USB stack a bit to allow for easier integration of non-ARC controller drivers git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18703 a1c6a512-1295-4272-9138-f99709370657 --- firmware/export/usb.h | 2 +- firmware/export/usb_core.h | 8 +++- firmware/export/usb_drv.h | 2 + firmware/target/arm/usb-drv-arc.c | 49 +++++++++++++++----- firmware/usbstack/usb_charging_only.c | 7 +-- firmware/usbstack/usb_charging_only.h | 2 +- firmware/usbstack/usb_class_driver.h | 13 +++--- firmware/usbstack/usb_core.c | 84 ++++++++++++++++++++++++----------- firmware/usbstack/usb_serial.c | 39 ++++++++++------ firmware/usbstack/usb_serial.h | 4 +- firmware/usbstack/usb_storage.c | 70 +++++++++++++++++------------ firmware/usbstack/usb_storage.h | 4 +- 12 files changed, 185 insertions(+), 99 deletions(-) diff --git a/firmware/export/usb.h b/firmware/export/usb.h index 9952a074bb..08ae27999a 100644 --- a/firmware/export/usb.h +++ b/firmware/export/usb.h @@ -85,7 +85,7 @@ enum { struct usb_transfer_completion_event_data { unsigned char endpoint; - bool in; + int dir; int status; int length; void* data; diff --git a/firmware/export/usb_core.h b/firmware/export/usb_core.h index 6aada54399..9d9e327bb3 100644 --- a/firmware/export/usb_core.h +++ b/firmware/export/usb_core.h @@ -45,10 +45,12 @@ extern int usb_max_pkt_size; +struct usb_class_driver; + void usb_core_init(void); void usb_core_exit(void); void usb_core_control_request(struct usb_ctrlrequest* req); -void usb_core_transfer_complete(int endpoint, bool in, int status, int length); +void usb_core_transfer_complete(int endpoint, int dir, int status, int length); void usb_core_bus_reset(void); bool usb_core_exclusive_connection(void); void usb_core_enable_driver(int driver,bool enabled); @@ -56,6 +58,10 @@ bool usb_core_driver_enabled (int driver); void usb_core_handle_transfer_completion( struct usb_transfer_completion_event_data* event); int usb_core_ack_control(struct usb_ctrlrequest* req); + +int usb_core_request_endpoint(int dir, struct usb_class_driver *drv); +void usb_core_release_endpoint(int dir); + #ifdef HAVE_HOTSWAP void usb_core_hotswap_event(int volume,bool inserted); #endif diff --git a/firmware/export/usb_drv.h b/firmware/export/usb_drv.h index 3db82efd6a..3bd8e1fdf4 100644 --- a/firmware/export/usb_drv.h +++ b/firmware/export/usb_drv.h @@ -41,5 +41,7 @@ int usb_drv_port_speed(void); void usb_drv_cancel_all_transfers(void); void usb_drv_set_test_mode(int mode); bool usb_drv_connected(void); +int usb_drv_request_endpoint(int dir); +void usb_drv_release_endpoint(int ep); #endif diff --git a/firmware/target/arm/usb-drv-arc.c b/firmware/target/arm/usb-drv-arc.c index a30e1c5139..529f6cf7f8 100644 --- a/firmware/target/arm/usb-drv-arc.c +++ b/firmware/target/arm/usb-drv-arc.c @@ -363,6 +363,8 @@ static const unsigned int pipe2mask[] = { 0x10, 0x100000, }; +static char ep_allocation[NUM_ENDPOINTS]; + /*-------------------------------------------------------------------------*/ static void transfer_completed(void); static void control_received(void); @@ -534,23 +536,23 @@ void usb_drv_int(void) bool usb_drv_stalled(int endpoint,bool in) { if(in) { - return ((REG_ENDPTCTRL(endpoint) & EPCTRL_TX_EP_STALL)!=0); + return ((REG_ENDPTCTRL(endpoint&0x7f) & EPCTRL_TX_EP_STALL)!=0); } else { - return ((REG_ENDPTCTRL(endpoint) & EPCTRL_RX_EP_STALL)!=0); + return ((REG_ENDPTCTRL(endpoint&0x7f) & EPCTRL_RX_EP_STALL)!=0); } } void usb_drv_stall(int endpoint, bool stall,bool in) { - logf("%sstall %d", stall?"":"un", endpoint); + logf("%sstall %d", stall?"":"un", endpoint&0x7f); if(in) { if (stall) { - REG_ENDPTCTRL(endpoint) |= EPCTRL_TX_EP_STALL; + REG_ENDPTCTRL(endpoint&0x7f) |= EPCTRL_TX_EP_STALL; } else { - REG_ENDPTCTRL(endpoint) &= ~EPCTRL_TX_EP_STALL; + REG_ENDPTCTRL(endpoint&0x7f) &= ~EPCTRL_TX_EP_STALL; } } else { @@ -565,23 +567,23 @@ void usb_drv_stall(int endpoint, bool stall,bool in) int usb_drv_send_nonblocking(int endpoint, void* ptr, int length) { - return prime_transfer(endpoint, ptr, length, true, false); + return prime_transfer(endpoint&0x7f, ptr, length, true, false); } int usb_drv_send(int endpoint, void* ptr, int length) { - return prime_transfer(endpoint, ptr, length, true, true); + return prime_transfer(endpoint&0x7f, ptr, length, true, true); } int usb_drv_recv(int endpoint, void* ptr, int length) { //logf("usbrecv(%x, %d)", ptr, length); - return prime_transfer(endpoint, ptr, length, false, false); + return prime_transfer(endpoint&0x7f, ptr, length, false, false); } void usb_drv_wait(int endpoint, bool send) { - int pipe = endpoint * 2 + (send ? 1 : 0); + int pipe = (endpoint&0x7f) * 2 + (send ? 1 : 0); struct queue_head* qh = &qh_array[pipe]; while (qh->dtd.size_ioc_sts & QH_STATUS_ACTIVE) { @@ -609,7 +611,7 @@ void usb_drv_set_address(int address) void usb_drv_reset_endpoint(int endpoint, bool send) { - int pipe = endpoint * 2 + (send ? 1 : 0); + int pipe = (endpoint&0x7f) * 2 + (send ? 1 : 0); unsigned int mask = pipe2mask[pipe]; REG_ENDPTFLUSH = mask; while (REG_ENDPTFLUSH & mask); @@ -753,6 +755,29 @@ void usb_drv_cancel_all_transfers(void) } } +int usb_drv_request_endpoint(int dir) +{ + int i, bit; + + bit=(dir & USB_DIR_IN)? 2:1; + + for (i=1; i < NUM_ENDPOINTS; i++) { + if((ep_allocation[i] & bit)!=0) + continue; + ep_allocation[i] |= bit; + return i | dir; + } + + return -1; +} + +void usb_drv_release_endpoint(int ep) +{ + int mask = (ep & USB_DIR_IN)? ~2:~1; + ep_allocation[ep & 0x7f] &= mask; +} + + static void prepare_td(struct transfer_descriptor* td, struct transfer_descriptor* previous_td, void *ptr, int len,int pipe) @@ -831,7 +856,7 @@ static void transfer_completed(void) qh->wait=0; wakeup_signal(&transfer_completion_signal[pipe]); } - usb_core_transfer_complete(ep, dir, qh->status, qh->length); + usb_core_transfer_complete(ep, dir?USB_DIR_IN:USB_DIR_OUT, qh->status, qh->length); } } } @@ -900,6 +925,7 @@ static void init_bulk_queue_heads(void) rx_packetsize = 64; tx_packetsize = 64; } + /* TODO: this should take ep_allocation into account */ /*** bulk ***/ for(i=1;iendpoint; + switch(ep) { case EP_CONTROL: logf("ctrl handled %ld",current_tick); @@ -346,8 +350,8 @@ void usb_core_handle_transfer_completion( (struct usb_ctrlrequest*)event->data); break; default: - if(ep_data[ep].completion_handler != NULL) - ep_data[ep].completion_handler(ep,event->in, + if(ep_data[ep].completion_handler[event->dir>>7] != NULL) + ep_data[ep].completion_handler[event->dir>>7](ep,event->dir, event->status,event->length); break; } @@ -388,34 +392,60 @@ static void usb_core_set_serial_function_id(void) usb_string_iSerial.wString[0] = hex[id]; } +int usb_core_request_endpoint(int dir, struct usb_class_driver *drv) +{ + int ret, ep; + + ret = usb_drv_request_endpoint(dir); + + if (ret == -1) + return -1; + + ep = ret & 0x7f; + dir = ret >> 7; + + ep_data[ep].completion_handler[dir] = drv->transfer_complete; + ep_data[ep].control_handler[dir] = drv->control_request; + + return ret; +} + +void usb_core_release_endpoint(int ep) +{ + int dir; + + usb_drv_release_endpoint(ep); + + dir = ep >> 7; + ep &= 0x7f; + + ep_data[ep].completion_handler[dir] = NULL; + ep_data[ep].control_handler[dir] = NULL; +} + static void allocate_interfaces_and_endpoints(void) { - int i,j; + int i; int interface=0; - int endpoint=1; memset(ep_data,0,sizeof(ep_data)); - for(i=0;iNUM_ENDPOINTS) { + if (drivers[i].request_endpoints(&drivers[i])) { drivers[i].enabled = false; continue; } + interface = drivers[i].set_first_interface(interface); drivers[i].last_interface = interface; - - for(j=oldendpoint;jNUM_ENDPOINTS) { - drivers[i].enabled = false; } } usb_core_num_interfaces = interface; @@ -666,8 +696,8 @@ static void usb_core_control_request_handler(struct usb_ctrlrequest* req) break; default: { bool handled=false; - if(ep_data[req->wIndex & 0xf].control_handler != NULL) - handled = ep_data[req->wIndex & 0xf].control_handler(req); + if(ep_data[req->wIndex & 0xf].control_handler[0] != NULL) + handled = ep_data[req->wIndex & 0xf].control_handler[0](req); if(!handled) { /* nope. flag error */ logf("usb bad req %d", req->bRequest); @@ -689,7 +719,7 @@ void usb_core_bus_reset(void) } /* called by usb_drv_transfer_completed() */ -void usb_core_transfer_complete(int endpoint, bool in, int status,int length) +void usb_core_transfer_complete(int endpoint, int dir, int status,int length) { switch (endpoint) { case EP_CONTROL: @@ -698,7 +728,7 @@ void usb_core_transfer_complete(int endpoint, bool in, int status,int length) default: ep_data[endpoint].completion_event.endpoint=endpoint; - ep_data[endpoint].completion_event.in=in; + ep_data[endpoint].completion_event.dir=dir; ep_data[endpoint].completion_event.data=0; ep_data[endpoint].completion_event.status=status; ep_data[endpoint].completion_event.length=length; @@ -712,7 +742,7 @@ void usb_core_transfer_complete(int endpoint, bool in, int status,int length) void usb_core_control_request(struct usb_ctrlrequest* req) { ep_data[0].completion_event.endpoint=0; - ep_data[0].completion_event.in=0; + ep_data[0].completion_event.dir=0; ep_data[0].completion_event.data=(void *)req; ep_data[0].completion_event.status=0; ep_data[0].completion_event.length=0; diff --git a/firmware/usbstack/usb_serial.c b/firmware/usbstack/usb_serial.c index 8d6ce67823..1b143d56eb 100644 --- a/firmware/usbstack/usb_serial.c +++ b/firmware/usbstack/usb_serial.c @@ -72,7 +72,7 @@ static int buffer_start; static int buffer_length; static bool active = false; -static int usb_endpoint; +static int ep_in, ep_out; static int usb_interface; static struct mutex sendlock SHAREDBSS_ATTR; @@ -82,22 +82,33 @@ static void sendout(void) if(buffer_start+buffer_length > BUFFER_SIZE) { /* Buffer wraps. Only send the first part */ - usb_drv_send_nonblocking(usb_endpoint, &send_buffer[buffer_start], + usb_drv_send_nonblocking(ep_in, &send_buffer[buffer_start], (BUFFER_SIZE - buffer_start)); } else { /* Send everything */ - usb_drv_send_nonblocking(usb_endpoint, &send_buffer[buffer_start], + usb_drv_send_nonblocking(ep_in, &send_buffer[buffer_start], buffer_length); } busy_sending=true; } -int usb_serial_set_first_endpoint(int endpoint) +int usb_serial_request_endpoints(struct usb_class_driver *drv) { - usb_endpoint = endpoint; - return endpoint + 1; + ep_in = usb_core_request_endpoint(USB_DIR_IN, drv); + + if (ep_in < 0) + return -1; + + ep_out = usb_core_request_endpoint(USB_DIR_OUT, drv); + + if (ep_out < 0) { + usb_core_release_endpoint(ep_in); + return -1; + } + + return 0; } int usb_serial_set_first_interface(int interface) @@ -117,11 +128,11 @@ int usb_serial_get_config_descriptor(unsigned char *dest,int max_packet_size) memcpy(dest,&interface_descriptor,sizeof(struct usb_interface_descriptor)); dest+=sizeof(struct usb_interface_descriptor); - endpoint_descriptor.bEndpointAddress = usb_endpoint | USB_DIR_IN; + endpoint_descriptor.bEndpointAddress = ep_in; memcpy(dest,&endpoint_descriptor,sizeof(struct usb_endpoint_descriptor)); dest+=sizeof(struct usb_endpoint_descriptor); - endpoint_descriptor.bEndpointAddress = usb_endpoint | USB_DIR_OUT; + endpoint_descriptor.bEndpointAddress = ep_out; memcpy(dest,&endpoint_descriptor,sizeof(struct usb_endpoint_descriptor)); dest+=sizeof(struct usb_endpoint_descriptor); @@ -131,7 +142,7 @@ int usb_serial_get_config_descriptor(unsigned char *dest,int max_packet_size) void usb_serial_init_connection(void) { /* prime rx endpoint */ - usb_drv_recv(usb_endpoint, receive_buffer, sizeof receive_buffer); + usb_drv_recv(ep_out, receive_buffer, sizeof receive_buffer); /* we come here too after a bus reset, so reset some data */ mutex_lock(&sendlock); @@ -202,17 +213,17 @@ void usb_serial_send(unsigned char *data,int length) } /* called by usb_core_transfer_complete() */ -void usb_serial_transfer_complete(int ep,bool in, int status, int length) +void usb_serial_transfer_complete(int ep,int dir, int status, int length) { (void)ep; - switch (in) { - case false: + switch (dir) { + case USB_DIR_OUT: logf("serial: %s", receive_buffer); /* Data received. TODO : Do something with it ? */ - usb_drv_recv(usb_endpoint, receive_buffer, sizeof receive_buffer); + usb_drv_recv(ep_out, receive_buffer, sizeof receive_buffer); break; - case true: + case USB_DIR_IN: mutex_lock(&sendlock); /* Data sent out. Update circular buffer */ if(status == 0) diff --git a/firmware/usbstack/usb_serial.h b/firmware/usbstack/usb_serial.h index 69ad3fd647..94decdc42b 100644 --- a/firmware/usbstack/usb_serial.h +++ b/firmware/usbstack/usb_serial.h @@ -23,13 +23,13 @@ #include "usb_ch9.h" -int usb_serial_set_first_endpoint(int endpoint); +int usb_serial_request_endpoints(struct usb_class_driver *); int usb_serial_set_first_interface(int interface); int usb_serial_get_config_descriptor(unsigned char *dest,int max_packet_size); void usb_serial_init_connection(void); void usb_serial_init(void); void usb_serial_disconnect(void); -void usb_serial_transfer_complete(int ep,bool in, int status, int length); +void usb_serial_transfer_complete(int ep,int dir, int status, int length); bool usb_serial_control_request(struct usb_ctrlrequest* req); void usb_serial_send(unsigned char *data,int length); diff --git a/firmware/usbstack/usb_storage.c b/firmware/usbstack/usb_storage.c index f774d14cbc..9c524a37b8 100644 --- a/firmware/usbstack/usb_storage.c +++ b/firmware/usbstack/usb_storage.c @@ -22,7 +22,7 @@ #include "system.h" #include "usb_core.h" #include "usb_drv.h" -//#define LOGF_ENABLE +#define LOGF_ENABLE #include "logf.h" #include "ata.h" #include "hotswap.h" @@ -263,8 +263,8 @@ static void identify2inquiry(int lun); static void send_and_read_next(void); static bool ejected[NUM_VOLUMES]; -static int usb_endpoint; static int usb_interface; +static int ep_in, ep_out; static enum { WAITING_FOR_COMMAND, @@ -321,7 +321,7 @@ void usb_storage_reconnect(void) && usb_inserted()) { for(i=0;icommand_block[0x0]); - usb_drv_stall(usb_endpoint, true,true); + usb_drv_stall(ep_in, true,true); send_csw(UMS_STATUS_FAIL); break; } @@ -980,25 +992,25 @@ static void handle_scsi(struct command_block_wrapper* cbw) static void send_block_data(void *data,int size) { - usb_drv_send_nonblocking(usb_endpoint, data,size); + usb_drv_send_nonblocking(ep_in, data,size); state = SENDING_BLOCKS; } static void send_command_result(void *data,int size) { - usb_drv_send_nonblocking(usb_endpoint, data,size); + usb_drv_send_nonblocking(ep_in, data,size); state = SENDING_RESULT; } static void send_command_failed_result(void) { - usb_drv_send_nonblocking(usb_endpoint, NULL, 0); + usb_drv_send_nonblocking(ep_in, NULL, 0); state = SENDING_FAILED_RESULT; } static void receive_block_data(void *data,int size) { - usb_drv_recv(usb_endpoint, data, size); + usb_drv_recv(ep_out, data, size); state = RECEIVING_BLOCKS; } @@ -1009,7 +1021,7 @@ static void send_csw(int status) tb.csw->data_residue = 0; tb.csw->status = status; - usb_drv_send_nonblocking(usb_endpoint, tb.csw, + usb_drv_send_nonblocking(ep_in, tb.csw, sizeof(struct command_status_wrapper)); state = SENDING_CSW; //logf("CSW: %X",status); diff --git a/firmware/usbstack/usb_storage.h b/firmware/usbstack/usb_storage.h index 7054d24501..4c89039bfd 100644 --- a/firmware/usbstack/usb_storage.h +++ b/firmware/usbstack/usb_storage.h @@ -23,12 +23,12 @@ #include "usb_ch9.h" -int usb_storage_set_first_endpoint(int endpoint); +int usb_storage_request_endpoints(struct usb_class_driver *); int usb_storage_set_first_interface(int interface); int usb_storage_get_config_descriptor(unsigned char *dest,int max_packet_size); void usb_storage_init_connection(void); void usb_storage_init(void); -void usb_storage_transfer_complete(int ep,bool in,int state,int length); +void usb_storage_transfer_complete(int ep,int dir,int state,int length); bool usb_storage_control_request(struct usb_ctrlrequest* req); #ifdef HAVE_HOTSWAP void usb_storage_notify_hotswap(int volume,bool inserted); -- cgit v1.2.3