405a508102da7b510d5c55eda4bf03f58c9f4929
[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   1
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 tUSBDriver      *gpUSB_InterfaceDrivers = &gUSBHub_Driver;
25
26 // === CODE ===
27 void USB_RegisterDriver(tUSBDriver *Driver)
28 {
29         Driver->Next = gpUSB_InterfaceDrivers;
30         gpUSB_InterfaceDrivers = Driver;
31 }
32
33 tUSBHub *USB_RegisterHost(tUSBHostDef *HostDef, void *ControllerPtr, int nPorts)
34 {
35         tUSBHost        *host;
36         
37         host = malloc(sizeof(tUSBHost) + nPorts*sizeof(void*));
38         if(!host) {
39                 // Oh, bugger.
40                 return NULL;
41         }
42         host->HostDef = HostDef;
43         host->Ptr = ControllerPtr;
44         memset(host->AddressBitmap, 0, sizeof(host->AddressBitmap));
45
46         host->RootHubDev.ParentHub = NULL;
47         host->RootHubDev.Host = host;
48         host->RootHubDev.Address = 0;
49
50 //      host->RootHubIf.Next = NULL;
51         host->RootHubIf.Dev = &host->RootHubDev;
52         host->RootHubIf.Driver = NULL;
53         host->RootHubIf.Data = NULL;
54         host->RootHubIf.nEndpoints = 0;
55
56         host->RootHub.Interface = &host->RootHubIf;
57         host->RootHub.nPorts = nPorts;
58         memset(host->RootHub.Devices, 0, sizeof(void*)*nPorts);
59
60         // TODO: Lock
61         host->Next = gUSB_Hosts;
62         gUSB_Hosts = host;
63
64         return &host->RootHub;
65 }
66
67 // --- Drivers ---
68 void USB_RegisterDriver(tUSBDriver *Driver)
69 {
70         Log_Warning("USB", "TODO: Implement USB_RegisterDriver");
71 }
72
73 tUSBDriver *USB_int_FindDriverByClass(Uint32 ClassCode)
74 {
75         ENTER("xClassCode", ClassCode);
76         for( tUSBDriver *ret = gpUSB_InterfaceDrivers; ret; ret = ret->Next )
77         {
78                 LOG(" 0x%x & 0x%x == 0x%x?", ClassCode, ret->Match.Class.ClassMask, ret->Match.Class.ClassCode);
79                 if( (ClassCode & ret->Match.Class.ClassMask) == ret->Match.Class.ClassCode )
80                 {
81                         LOG("Found '%s'", ret->Name);
82                         LEAVE('p', ret);
83                         return ret;
84                 }
85         }
86         LEAVE('n');
87         return NULL;
88 }
89
90 // --- Hub Registration ---
91 // NOTE: Doesn't do much nowdays
92 tUSBHub *USB_RegisterHub(tUSBInterface *Device, int PortCount)
93 {
94         tUSBHub *ret;
95         
96         ret = malloc(sizeof(tUSBHub) + sizeof(ret->Devices[0])*PortCount);
97         ret->Interface = Device;
98         ret->nPorts = PortCount;
99         memset(ret->Devices, 0, sizeof(ret->Devices[0])*PortCount);
100         return ret;
101 }
102
103 void USB_RemoveHub(tUSBHub *Hub)
104 {
105         for( int i = 0; i < Hub->nPorts; i ++ )
106         {
107                 if( Hub->Devices[i] )
108                 {
109                         USB_DeviceDisconnected( Hub, i );
110                 }
111         }
112         free(Hub);
113 }
114

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