From 07a823e02a00c899379ec92804fc50606cbd4667 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 20 May 2012 11:55:25 +0800 Subject: [PATCH] Modules/USB - Changes to allow MSC to set up correctly --- KernelLand/Modules/USB/Core/Makefile | 2 +- .../Modules/USB/Core/include/usb_core.h | 9 ++- KernelLand/Modules/USB/Core/usb.h | 5 ++ KernelLand/Modules/USB/Core/usb_devinit.c | 12 +++- KernelLand/Modules/USB/Core/usb_info.c | 29 ++++++++++ KernelLand/Modules/USB/Core/usb_io.c | 58 ++++++++++--------- 6 files changed, 84 insertions(+), 31 deletions(-) create mode 100644 KernelLand/Modules/USB/Core/usb_info.c diff --git a/KernelLand/Modules/USB/Core/Makefile b/KernelLand/Modules/USB/Core/Makefile index 4d6e6845..33e311b0 100644 --- a/KernelLand/Modules/USB/Core/Makefile +++ b/KernelLand/Modules/USB/Core/Makefile @@ -2,7 +2,7 @@ # OBJ = main.o -OBJ += usb.o usb_lowlevel.o usb_devinit.o usb_io.o usb_poll.o +OBJ += usb.o usb_lowlevel.o usb_devinit.o usb_io.o usb_poll.o usb_info.o OBJ += hub.o CPPFLAGS = -Iinclude NAME = Core diff --git a/KernelLand/Modules/USB/Core/include/usb_core.h b/KernelLand/Modules/USB/Core/include/usb_core.h index 3cc0e87a..5f494cb0 100644 --- a/KernelLand/Modules/USB/Core/include/usb_core.h +++ b/KernelLand/Modules/USB/Core/include/usb_core.h @@ -53,16 +53,23 @@ struct sUSBDriver extern void USB_RegisterDriver(tUSBDriver *Driver); +// --- Driver Pointer --- extern void *USB_GetDeviceDataPtr(tUSBInterface *Dev); extern void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr); +// --- Device/Interface information --- +extern Uint32 USB_GetInterfaceClass(tUSBInterface *Dev); +extern void USB_GetDeviceVendor(tUSBInterface *Dev, Uint16 *VendorID, Uint16 *DeviceID); +extern char *USB_GetSerialNumber(tUSBInterface *Dev); + +// --- Device IO --- extern void USB_StartPollingEndpoint(tUSBInterface *Dev, int Endpoint); extern void USB_ReadDescriptor(tUSBInterface *Dev, int Type, int Index, int Length, void *Data); extern void USB_Request(tUSBInterface *Dev, int Endpoint, int Type, int Req, int Value, int Index, int Len, void *Data); // TODO: Async extern void USB_SendData(tUSBInterface *Dev, int Endpoint, size_t Length, const void *Data); extern void USB_RecvData(tUSBInterface *Dev, int Endpoint, size_t Length, void *Data); -extern void USB_RecvDataA(tUSBInterface *Dev, int Endpoint, int Length, void *DataBuf, tUSB_DataCallback Callback); +extern void USB_RecvDataA(tUSBInterface *Dev, int Endpoint, size_t Length, void *DataBuf, tUSB_DataCallback Callback); #endif diff --git a/KernelLand/Modules/USB/Core/usb.h b/KernelLand/Modules/USB/Core/usb.h index 5a133630..0b6a3c19 100644 --- a/KernelLand/Modules/USB/Core/usb.h +++ b/KernelLand/Modules/USB/Core/usb.h @@ -11,6 +11,7 @@ #include #include #include +#include "usb_proto.h" typedef struct sUSBHost tUSBHost; typedef struct sUSBDevice tUSBDevice; @@ -53,6 +54,8 @@ struct sUSBInterface tUSBDriver *Driver; void *Data; + + struct sDescriptor_Interface IfaceDesc; int nEndpoints; tUSBEndpoint Endpoints[]; @@ -71,6 +74,8 @@ struct sUSBDevice tUSBHost *Host; int Address; + struct sDescriptor_Device DevDesc; + int nInterfaces; tUSBInterface *Interfaces[]; }; diff --git a/KernelLand/Modules/USB/Core/usb_devinit.c b/KernelLand/Modules/USB/Core/usb_devinit.c index fb44bb04..416e2ba0 100644 --- a/KernelLand/Modules/USB/Core/usb_devinit.c +++ b/KernelLand/Modules/USB/Core/usb_devinit.c @@ -102,6 +102,8 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port) } #endif #endif + + memcpy(&dev->DevDesc, &desc, sizeof(desc)); } // TODO: Support alternate configurations @@ -150,10 +152,10 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port) total_length = LittleEndian16(desc.TotalLength); full_buf = malloc( total_length ); USB_int_ReadDescriptor(dev, 0, 2, i, total_length, full_buf); - ptr_ofs += desc.Length; - // TODO: Interfaces + + // Interfaces! for( int j = 0; ptr_ofs < total_length && j < desc.NumInterfaces; j ++ ) { struct sDescriptor_Interface *iface; @@ -193,11 +195,15 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port) LOG("}"); #endif - dev_if = malloc(sizeof(tUSBInterface) + iface->NumEndpoints*sizeof(dev_if->Endpoints[0])); + dev_if = malloc( + sizeof(tUSBInterface) + + iface->NumEndpoints*sizeof(dev_if->Endpoints[0]) + ); dev_if->Dev = dev; dev_if->Driver = NULL; dev_if->Data = NULL; dev_if->nEndpoints = iface->NumEndpoints; + memcpy(&dev_if->IfaceDesc, iface, sizeof(*iface)); dev->Interfaces[j] = dev_if; // Copy interface data diff --git a/KernelLand/Modules/USB/Core/usb_info.c b/KernelLand/Modules/USB/Core/usb_info.c new file mode 100644 index 00000000..7a2bf824 --- /dev/null +++ b/KernelLand/Modules/USB/Core/usb_info.c @@ -0,0 +1,29 @@ +/* + * Acess 2 USB Stack + * - By John Hodge (thePowersGang) + * + * usb_info.c + * - USB Device Information Functions (helpers for drivers) + */ +#include +#include "usb.h" +#include "usb_lowlevel.h" + +// === CODE === +Uint32 USB_GetInterfaceClass(tUSBInterface *Dev) +{ + return ((Uint32)Dev->IfaceDesc.InterfaceClass << 16) + |((Uint32)Dev->IfaceDesc.InterfaceSubClass << 8) + |((Uint32)Dev->IfaceDesc.InterfaceProtocol << 0); +} + +void USB_GetDeviceVendor(tUSBInterface *Dev, Uint16 *VendorID, Uint16 *DeviceID) +{ + *VendorID = LittleEndian16( Dev->Dev->DevDesc.VendorID ); + *DeviceID = LittleEndian16( Dev->Dev->DevDesc.DeviceID ); +} +char *USB_GetSerialNumber(tUSBInterface *Dev) +{ + return USB_int_GetDeviceString(Dev->Dev, 0, Dev->Dev->DevDesc.SerialNumberStr); +} + diff --git a/KernelLand/Modules/USB/Core/usb_io.c b/KernelLand/Modules/USB/Core/usb_io.c index 992269ac..9d58c750 100644 --- a/KernelLand/Modules/USB/Core/usb_io.c +++ b/KernelLand/Modules/USB/Core/usb_io.c @@ -58,17 +58,17 @@ void USB_SendData(tUSBInterface *Dev, int Endpoint, size_t Length, const void *D ep = &Dev->Endpoints[Endpoint-1]; host = Dev->Dev->Host; - if( Length > ep->MaxPacketSize ) { - Log_Warning("USB", "Max packet size exceeded (%i > %i)", ep->MaxPacketSize); - LEAVE('-'); - } - Threads_ClearEvent(THREAD_EVENT_SHORTWAIT); - host->HostDef->BulkOUT( - host->Ptr, Dev->Dev->Address*16 + Dev->Endpoints[Endpoint-1].EndpointNum, - 0, USB_WakeCallback, Proc_GetCurThread(), - (void*)Data, Length - ); + for( size_t ofs = 0; ofs < Length; ofs += ep->MaxPacketSize ) + { + size_t len = MIN(Length - ofs, ep->MaxPacketSize); + + host->HostDef->BulkOUT( + host->Ptr, Dev->Dev->Address*16 + Dev->Endpoints[Endpoint-1].EndpointNum, + 0, (len == Length - ofs ? USB_WakeCallback : NULL), Proc_GetCurThread(), + (char*)Data + ofs, len + ); + } Threads_WaitEvents(THREAD_EVENT_SHORTWAIT); LEAVE('-'); @@ -83,23 +83,23 @@ void USB_RecvData(tUSBInterface *Dev, int Endpoint, size_t Length, void *Data) ep = &Dev->Endpoints[Endpoint-1]; host = Dev->Dev->Host; - if( Length > ep->MaxPacketSize ) { - Log_Warning("USB", "Max packet size exceeded (%i > %i)", ep->MaxPacketSize); - LEAVE('-'); - } - Threads_ClearEvent(THREAD_EVENT_SHORTWAIT); - host->HostDef->BulkIN( - host->Ptr, Dev->Dev->Address*16 + Dev->Endpoints[Endpoint-1].EndpointNum, - 0, USB_WakeCallback, Proc_GetCurThread(), - Data, Length - ); + for( size_t ofs = 0; ofs < Length; ofs += ep->MaxPacketSize ) + { + size_t len = MIN(Length - ofs, ep->MaxPacketSize); + + host->HostDef->BulkIN( + host->Ptr, Dev->Dev->Address*16 + Dev->Endpoints[Endpoint-1].EndpointNum, + 0, (len == Length - ofs ? USB_WakeCallback : NULL), Proc_GetCurThread(), + (char*)Data + ofs, len + ); + } Threads_WaitEvents(THREAD_EVENT_SHORTWAIT); LEAVE('-'); } -void USB_RecvDataA(tUSBInterface *Dev, int Endpoint, int Length, void *DataBuf, tUSB_DataCallback Callback) +void USB_RecvDataA(tUSBInterface *Dev, int Endpoint, size_t Length, void *DataBuf, tUSB_DataCallback Callback) { tAsyncOp *op; tUSBHost *host; @@ -116,12 +116,18 @@ void USB_RecvDataA(tUSBInterface *Dev, int Endpoint, int Length, void *DataBuf, // TODO: Data toggle host = Dev->Dev->Host; + LOG("IN from %p %i:%i", host->Ptr, Dev->Dev->Address, op->Endpt->EndpointNum); - host->HostDef->BulkIN( - host->Ptr, Dev->Dev->Address*16 + op->Endpt->EndpointNum, - 0, USB_AsyncCallback, op, - DataBuf, Length - ); + for( size_t ofs = 0; ofs < Length; ofs += op->Endpt->MaxPacketSize ) + { + size_t len = MIN(Length - ofs, op->Endpt->MaxPacketSize); + + host->HostDef->BulkIN( + host->Ptr, Dev->Dev->Address*16 + op->Endpt->EndpointNum, + 0, (len == Length - ofs ? USB_AsyncCallback : NULL), op, + (char*)DataBuf + ofs, len + ); + } LEAVE('-'); -- 2.20.1