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

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