Modules/USB - Disabled debug in core.c
[tpg/acess2.git] / KernelLand / Modules / USB / Core / usb.c
1 /*
2  * Acess2 USB Stack
3  * - By John Hodge (thePowersGang)
4  *
5  * usb.c
6  * - USB Structure
7  */
8 #define DEBUG   0
9 #include <acess.h>
10 #include <vfs.h>
11 #include <drv_pci.h>
12 #include "usb.h"
13
14 // === IMPORTS ===
15 extern tUSBHost *gUSB_Hosts;
16 extern tUSBDriver gUSBHub_Driver;
17
18 // === STRUCTURES ===
19
20 // === PROTOTYPES ===
21 tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts);
22
23 // === GLOBALS ===
24 tMutex  glUSB_Hosts;
25 tUSBHost        *gUSB_Hosts = NULL;
26 tMutex  glUSB_InterfaceDrivers;
27 tUSBDriver      *gpUSB_InterfaceDrivers = &gUSBHub_Driver;
28
29 // === CODE ===
30 tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts)
31 {
32         tUSBHost        *host;
33         
34         host = malloc(sizeof(tUSBHost) + nPorts*sizeof(tUSBHubPort) + sizeof(tUSBDevice) + sizeof(tUSBInterface));
35         if(!host) {
36                 // Oh, bugger.
37                 return NULL;
38         }
39         host->HostDef = HostDef;
40         host->Ptr = ControllerPtr;
41         memset(host->AddressBitmap, 0, sizeof(host->AddressBitmap));
42
43         host->RootHubDev = (void*)(host->RootHub.Ports + nPorts);
44         host->RootHubDev->ParentHub = NULL;
45         host->RootHubDev->Host = host;
46         host->RootHubDev->Address = 0;
47         ASSERT(HostDef->InitControl);
48         host->RootHubDev->EndpointHandles[0] = HostDef->InitControl(ControllerPtr, 0, 64);
49         host->RootHubDev->nInterfaces = 0;
50
51         host->RootHubIf = (void*)(host->RootHubDev + 1);
52         host->RootHubIf->Dev = host->RootHubDev;
53         host->RootHubIf->Driver = NULL;
54         host->RootHubIf->Data = NULL;
55         host->RootHubIf->nEndpoints = 0;
56
57         host->RootHub.Interface = host->RootHubIf;
58         host->RootHub.nPorts = nPorts;
59         memset(host->RootHub.Ports, 0, sizeof(tUSBHubPort)*nPorts);
60
61         // Append to list
62         Mutex_Acquire( &glUSB_Hosts );
63         host->Next = gUSB_Hosts;
64         gUSB_Hosts = host;
65         Mutex_Release( &glUSB_Hosts );
66
67         return &host->RootHub;
68 }
69
70 // --- Drivers ---
71 void USB_RegisterDriver(tUSBDriver *Driver)
72 {
73         Mutex_Acquire( &glUSB_InterfaceDrivers );
74         Driver->Next = gpUSB_InterfaceDrivers;
75         gpUSB_InterfaceDrivers = Driver;
76         Mutex_Release( &glUSB_InterfaceDrivers );
77         
78         // TODO: Recheck devices that didn't have a driver
79 }
80
81 tUSBDriver *USB_int_FindDriverByClass(Uint32 ClassCode)
82 {
83         ENTER("xClassCode", ClassCode);
84         for( tUSBDriver *ret = gpUSB_InterfaceDrivers; ret; ret = ret->Next )
85         {
86                 LOG(" 0x%x & 0x%x == 0x%x?", ClassCode, ret->Match.Class.ClassMask, ret->Match.Class.ClassCode);
87                 if( (ClassCode & ret->Match.Class.ClassMask) == ret->Match.Class.ClassCode )
88                 {
89                         LOG("Found '%s'", ret->Name);
90                         LEAVE('p', ret);
91                         return ret;
92                 }
93         }
94         LEAVE('n');
95         return NULL;
96 }
97
98 // --- Hub Registration ---
99 // NOTE: Doesn't do much nowdays
100 tUSBHub *USB_RegisterHub(tUSBInterface *Device, int PortCount)
101 {
102         tUSBHub *ret;
103         
104         ret = malloc(sizeof(tUSBHub) + sizeof(ret->Ports[0])*PortCount);
105         ret->Interface = Device;
106         ret->nPorts = PortCount;
107         memset(ret->Ports, 0, sizeof(ret->Ports[0])*PortCount);
108         return ret;
109 }
110
111 void USB_RemoveHub(tUSBHub *Hub)
112 {
113         for( int i = 0; i < Hub->nPorts; i ++ )
114         {
115                 if( Hub->Ports[i].Dev )
116                 {
117                         USB_DeviceDisconnected( Hub, i );
118                 }
119         }
120         free(Hub);
121 }
122

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