Merge branch 'master' of github.com:thepowersgang/acess2
[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 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));
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.ParentHub = NULL;
44         host->RootHubDev.Host = host;
45         host->RootHubDev.Address = 0;
46         ASSERT(HostDef->InitControl);
47         host->RootHubDev.EndpointHandles[0] = HostDef->InitControl(ControllerPtr, 0, 64);
48
49 //      host->RootHubIf.Next = NULL;
50         host->RootHubIf.Dev = &host->RootHubDev;
51         host->RootHubIf.Driver = NULL;
52         host->RootHubIf.Data = NULL;
53         host->RootHubIf.nEndpoints = 0;
54
55         host->RootHub.Interface = &host->RootHubIf;
56         host->RootHub.nPorts = nPorts;
57         memset(host->RootHub.Ports, 0, sizeof(tUSBHubPort)*nPorts);
58
59         // Append to list
60         Mutex_Acquire( &glUSB_Hosts );
61         host->Next = gUSB_Hosts;
62         gUSB_Hosts = host;
63         Mutex_Release( &glUSB_Hosts );
64
65         return &host->RootHub;
66 }
67
68 // --- Drivers ---
69 void USB_RegisterDriver(tUSBDriver *Driver)
70 {
71         Mutex_Acquire( &glUSB_InterfaceDrivers );
72         Driver->Next = gpUSB_InterfaceDrivers;
73         gpUSB_InterfaceDrivers = Driver;
74         Mutex_Release( &glUSB_InterfaceDrivers );
75         
76         // TODO: Recheck devices that didn't have a driver
77 }
78
79 tUSBDriver *USB_int_FindDriverByClass(Uint32 ClassCode)
80 {
81         ENTER("xClassCode", ClassCode);
82         for( tUSBDriver *ret = gpUSB_InterfaceDrivers; ret; ret = ret->Next )
83         {
84                 LOG(" 0x%x & 0x%x == 0x%x?", ClassCode, ret->Match.Class.ClassMask, ret->Match.Class.ClassCode);
85                 if( (ClassCode & ret->Match.Class.ClassMask) == ret->Match.Class.ClassCode )
86                 {
87                         LOG("Found '%s'", ret->Name);
88                         LEAVE('p', ret);
89                         return ret;
90                 }
91         }
92         LEAVE('n');
93         return NULL;
94 }
95
96 // --- Hub Registration ---
97 // NOTE: Doesn't do much nowdays
98 tUSBHub *USB_RegisterHub(tUSBInterface *Device, int PortCount)
99 {
100         tUSBHub *ret;
101         
102         ret = malloc(sizeof(tUSBHub) + sizeof(ret->Ports[0])*PortCount);
103         ret->Interface = Device;
104         ret->nPorts = PortCount;
105         memset(ret->Ports, 0, sizeof(ret->Ports[0])*PortCount);
106         return ret;
107 }
108
109 void USB_RemoveHub(tUSBHub *Hub)
110 {
111         for( int i = 0; i < Hub->nPorts; i ++ )
112         {
113                 if( Hub->Ports[i].Dev )
114                 {
115                         USB_DeviceDisconnected( Hub, i );
116                 }
117         }
118         free(Hub);
119 }
120

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