10 #include "usb_proto.h"
13 extern tUSBHost *gUSB_Hosts;
18 tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts);
19 void USB_DeviceConnected(tUSBHub *Hub, int Port);
20 void USB_DeviceDisconnected(tUSBHub *Hub, int Port);
21 void *USB_GetDeviceDataPtr(tUSBDevice *Dev);
22 void USB_SetDeviceDataPtr(tUSBDevice *Dev, void *Ptr);
23 int USB_int_AllocateAddress(tUSBHost *Host);
24 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address);
27 tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts)
31 host = malloc(sizeof(tUSBHost) + nPorts*sizeof(void*));
36 host->HostDef = HostDef;
37 host->Ptr = ControllerPtr;
38 memset(host->AddressBitmap, 0, sizeof(host->AddressBitmap));
40 host->RootHubDev.Next = NULL;
41 host->RootHubDev.ParentHub = NULL;
42 host->RootHubDev.Host = host;
43 host->RootHubDev.Address = 0;
44 host->RootHubDev.Driver = NULL;
45 host->RootHubDev.Data = NULL;
47 host->RootHub.Device = NULL;
48 host->RootHub.CheckPorts = NULL;
49 host->RootHub.nPorts = nPorts;
50 memset(host->RootHub.Devices, 0, sizeof(void*)*nPorts);
53 host->Next = gUSB_Hosts;
57 HostDef->CheckPorts(ControllerPtr);
59 return &host->RootHub;
62 void USB_DeviceConnected(tUSBHub *Hub, int Port)
65 if( Port >= Hub->nPorts ) return ;
66 if( Hub->Devices[Port] ) return ;
68 ENTER("pHub iPort", Hub, Port);
70 // 0. Perform port init? (done in hub?)
73 dev = malloc(sizeof(tUSBDevice));
76 dev->Host = Hub->Device->Host;
81 // 1. Assign an address
82 dev->Address = USB_int_AllocateAddress(dev->Host);
83 if(dev->Address == 0) {
84 Log_Error("USB", "No addresses avaliable on host %p", dev->Host);
89 USB_int_SendSetupSetAddress(dev->Host, dev->Address);
90 LOG("Assigned address %i", dev->Address);
92 // 2. Get device information
98 void USB_DeviceDisconnected(tUSBHub *Hub, int Port)
103 void *USB_GetDeviceDataPtr(tUSBDevice *Dev) { return Dev->Data; }
104 void USB_SetDeviceDataPtr(tUSBDevice *Dev, void *Ptr) { Dev->Data = Ptr; }
106 int USB_int_AllocateAddress(tUSBHost *Host)
109 for( i = 1; i < 128; i ++ )
111 if(Host->AddressBitmap[i/8] & (1 << i))
118 int USB_int_SendSetupSetAddress(tUSBHost *Host, int Address)
120 struct sDeviceRequest req;
121 req.ReqType = 0; // bmRequestType
122 req.Request = 5; // SET_ADDRESS
123 req.Value = Address & 0x7F; // wValue
124 req.Index = 0; // wIndex
125 req.Length = 0; // wLength
127 // Addr 0:0, Data Toggle = 0, no interrupt
128 return Host->HostDef->SendSETUP(Host->Ptr, 0, 0, 0, FALSE, &req, sizeof(req)) == NULL;
131 int USB_int_ReadDescriptor(tUSBDevice *Dev, int Endpoint, int Type, int Index, int Length, void *Dest)
133 struct sDeviceRequest req;
134 const int ciMaxPacketSize = 0x400;
136 req.Request = 6; // GET_DESCRIPTOR
137 req.Value = ((Type & 0xFF) << 8) | (Index & 0xFF);
138 req.Index = 0; // TODO: Language ID
141 Dev->Host->HostDef->SendSETUP(
142 Dev->Host->Ptr, Dev->Address, Endpoint,
147 while( Length > ciMaxPacketSize )
149 Dev->Host->HostDef->SendIN(
150 Dev->Host->Ptr, Dev->Address, Endpoint,
152 Dest, ciMaxPacketSize
154 Length -= ciMaxPacketSize;
157 // TODO: Complete and get completion