Kernel/USB - Still broken, reworking host controller API to give driver more information
[tpg/acess2.git] / KernelLand / Modules / USB / Core / usb_lowlevel.c
index 3450bd8..67381c0 100644 (file)
 #include "usb_proto.h"
 #include "usb_lowlevel.h"
 #include <timers.h>
+#include <events.h>
 
 // === PROTOTYPES ===
 void   *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, int Val, int Indx, int Len, void *Data);
+void   USB_int_WakeThread(void *Thread, void *Data, size_t Length);
  int   USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
  int   USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest);
 char   *USB_int_GetDeviceString(tUSBDevice *Dev, int Endpoint, int Index);
@@ -26,43 +28,62 @@ void *USB_int_Request(tUSBHost *Host, int Addr, int EndPt, int Type, int Req, in
        // TODO: Sanity check (and check that Type is valid)
        struct sDeviceRequest   req;
         int    dest = Addr * 16 + EndPt;       // TODO: Validate
+       tThread *thisthread = Proc_GetCurThread();
+       
+       ENTER("pHost xdest iType iReq iVal iIndx iLen pData",
+               Host, dest, Type, Req, Val, Indx, Len, Data);
+       
        req.ReqType = Type;
        req.Request = Req;
        req.Value = LittleEndian16( Val );
        req.Index = LittleEndian16( Indx );
        req.Length = LittleEndian16( Len );
-       
-       hdl = Host->HostDef->SendSETUP(Host->Ptr, dest, 0, NULL, NULL, &req, sizeof(req));
+
+       Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
+
+       LOG("SETUP");   
+       hdl = Host->HostDef->ControlSETUP(Host->Ptr, dest, 0, &req, sizeof(req));
 
        // TODO: Data toggle?
        // TODO: Multi-packet transfers
-       if( Type & 0x80 )
+       if( Len > 0 )
        {
-               void    *hdl2;
-               
-               hdl = Host->HostDef->SendIN(Host->Ptr, dest, 0, NULL, NULL, Data, Len);
-
-               hdl2 = Host->HostDef->SendOUT(Host->Ptr, dest, 0, NULL, NULL, NULL, 0);
-               while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
-                       Time_Delay(1);
+               if( Type & 0x80 )
+               {
+                       LOG("IN");
+                       hdl = Host->HostDef->ControlIN(Host->Ptr, dest, 1, NULL, NULL, Data, Len);
+       
+                       LOG("OUT (Status)");
+                       hdl = Host->HostDef->ControlOUT(Host->Ptr, dest, 1, USB_int_WakeThread, thisthread, NULL, 0);
+               }
+               else
+               {
+                       LOG("OUT");
+                       Host->HostDef->ControlOUT(Host->Ptr, dest, 1, NULL, NULL, Data, Len);
+                       
+                       // Status phase (DataToggle=1)
+                       LOG("IN (Status)");
+                       hdl = Host->HostDef->ControlIN(Host->Ptr, dest, 1, USB_int_WakeThread, thisthread, NULL, 0);
+               }
        }
        else
        {
-               void    *hdl2;
-               
-               if( Len > 0 )
-                       hdl = Host->HostDef->SendOUT(Host->Ptr, dest, 0, NULL, NULL, Data, Len);
-               else
-                       hdl = NULL;
-               
-               // Status phase (DataToggle=1)
-               hdl2 = Host->HostDef->SendIN(Host->Ptr, dest, 1, NULL, NULL, NULL, 0);
-               while( Host->HostDef->IsOpComplete(Host->Ptr, hdl2) == 0 )
-                       Time_Delay(1);
+               // Zero length, IN status
+               LOG("IN (Status)");
+               hdl = Host->HostDef->ControlIN(Host->Ptr, dest, 1, USB_int_WakeThread, thisthread, NULL, 0);
        }
+       LOG("Wait...");
+       Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
+
+       LEAVE('p', hdl);
        return hdl;
 }
 
+void USB_int_WakeThread(void *Thread, void *Data, size_t Length)
+{
+       Threads_PostEvent(Thread, THREAD_EVENT_SHORTWAIT);
+}
+
 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
 {
        USB_int_Request(Host, 0, 0, 0x00, 5, Address & 0x7F, 0, 0, NULL);
@@ -77,29 +98,26 @@ int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, i
        void    *final;
         int    dest = Dev->Address*16 + Endpoint;
 
+       ENTER("pDev xdest iType iIndex iLength pDest",
+               Dev, dest, Type, Index, Length, Dest);
+
        req.ReqType = 0x80;
-       switch( Type & 0xF00 )
-       {
-       case 0x000:     req.ReqType |= (0 << 5);        break;  // Standard
-       case 0x100:     req.ReqType |= (1 << 5);        break;  // Class
-       case 0x200:     req.ReqType |= (2 << 5);        break;  // Vendor
-       }
+       req.ReqType |= ((Type >> 8) & 0x3) << 5;        // Bits 5/6
+       req.ReqType |= (Type >> 12) & 3;        // Destination (Device, Interface, Endpoint, Other);
 
        req.Request = 6;        // GET_DESCRIPTOR
        req.Value = LittleEndian16( ((Type & 0xFF) << 8) | (Index & 0xFF) );
-       req.Index = LittleEndian16( 0 );        // TODO: Language ID
+       req.Index = LittleEndian16( 0 );        // TODO: Language ID / Interface
        req.Length = LittleEndian16( Length );
-       
-       Dev->Host->HostDef->SendSETUP(
-               Dev->Host->Ptr, dest,
-               0, NULL, NULL,
-               &req, sizeof(req)
-               );
+
+       LOG("SETUP");   
+       Dev->Host->HostDef->ControlSETUP(Dev->Host->Ptr, dest, 0, &req, sizeof(req));
        
        bToggle = 1;
        while( Length > ciMaxPacketSize )
        {
-               Dev->Host->HostDef->SendIN(
+               LOG("IN (%i rem)", Length - ciMaxPacketSize);
+               Dev->Host->HostDef->ControlIN(
                        Dev->Host->Ptr, dest,
                        bToggle, NULL, NULL,
                        Dest, ciMaxPacketSize
@@ -108,15 +126,21 @@ int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, i
                Length -= ciMaxPacketSize;
        }
 
-       final = Dev->Host->HostDef->SendIN(
-               Dev->Host->Ptr, dest,
-               bToggle, INVLPTR, NULL,
-               Dest, Length
+       LOG("IN (final)");
+       Dev->Host->HostDef->ControlIN( Dev->Host->Ptr, dest, bToggle, NULL, NULL, Dest, Length );
+
+       Threads_ClearEvent(THREAD_EVENT_SHORTWAIT);
+       LOG("OUT (Status)");
+       final = Dev->Host->HostDef->ControlOUT(
+               Dev->Host->Ptr, dest, 1,
+               USB_int_WakeThread, Proc_GetCurThread(),
+               NULL, 0
                );
 
-       while( Dev->Host->HostDef->IsOpComplete(Dev->Host->Ptr, final) == 0 )
-               Threads_Yield();
+       LOG("Waiting");
+       Threads_WaitEvents(THREAD_EVENT_SHORTWAIT);
 
+       LEAVE('i', 0);
        return 0;
 }
 

UCC git Repository :: git.ucc.asn.au