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

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