fd14ed1ba34dcf5078d486c570f59894cb9d9e49
[tpg/acess2.git] / KernelLand / Kernel / drv / zero-one.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * drv/zero-one.c
6  * - /Devices/{null,zero,one}
7  */
8 #define DEBUG   0
9 #include <acess.h>
10 #include <modules.h>
11 #include <fs_devfs.h>
12
13 // === PROTOTYPES ===
14  int    CoreDevs_Install(char **Arguments);
15 size_t  CoreDevs_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer);
16 size_t  CoreDevs_Read_Zero(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
17 size_t  CoreDevs_Read_One (tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
18 size_t  CoreDevs_Read_Null(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
19 size_t  CoreDevs_Read_FRandom(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
20 size_t  CoreDevs_Read_GRandom(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
21
22 // === GLOBALS ===
23 MODULE_DEFINE(0, 0x0100, CoreDevs, CoreDevs_Install, NULL, NULL);
24 tVFS_NodeType   gCoreDevs_NT_Null = {
25         .TypeName = "CoreDevs-null",
26         .Read  = CoreDevs_Read_Null,
27         .Write = CoreDevs_Write
28 };
29 tVFS_NodeType   gCoreDevs_NT_Zero = {
30         .TypeName = "CoreDevs-zero",
31         .Read  = CoreDevs_Read_Zero,
32         .Write = CoreDevs_Write
33 };
34 tVFS_NodeType   gCoreDevs_NT_One = {
35         .TypeName = "CoreDevs-one",
36         .Read  = CoreDevs_Read_One,
37         .Write = CoreDevs_Write
38 };
39 tVFS_NodeType   gCoreDevs_NT_FRandom = {
40         .TypeName = "CoreDevs-frandom",
41         .Read  = CoreDevs_Read_FRandom,
42         .Write = CoreDevs_Write
43 };
44 tVFS_NodeType   gCoreDevs_NT_GRandom = {
45         .TypeName = "CoreDevs-grandom",
46         .Read  = CoreDevs_Read_GRandom,
47         .Write = CoreDevs_Write
48 };
49 tDevFS_Driver   gCoreDevs_Null = {
50         NULL, "null",
51         {
52         .Size = 0, .NumACLs = 1,
53         .ACLs = &gVFS_ACL_EveryoneRW,
54         .Type = &gCoreDevs_NT_Null
55         }
56 };
57 tDevFS_Driver   gCoreDevs_Zero = {
58         NULL, "zero",
59         {
60         .Size = 0, .NumACLs = 1,
61         .ACLs = &gVFS_ACL_EveryoneRW,
62         .Type = &gCoreDevs_NT_Zero
63         }
64 };
65 tDevFS_Driver   gCoreDevs_One = {
66         NULL, "one",
67         {
68         .Size = 0, .NumACLs = 1,
69         .ACLs = &gVFS_ACL_EveryoneRW,
70         .Type = &gCoreDevs_NT_One
71         }
72 };
73 tDevFS_Driver   gCoreDevs_FRandom = {
74         NULL, "frandom",
75         {
76         .Size = 0, .NumACLs = 1,
77         .ACLs = &gVFS_ACL_EveryoneRW,
78         .Type = &gCoreDevs_NT_FRandom
79         }
80 };
81 tDevFS_Driver   gCoreDevs_GRandom = {
82         NULL, "grandom",
83         {
84         .Size = 0, .NumACLs = 1,
85         .ACLs = &gVFS_ACL_EveryoneRW,
86         .Type = &gCoreDevs_NT_GRandom
87         }
88 };
89
90 // === CODE ===
91 /**
92  * \brief Installs the CoreDevs driver
93  */
94 int CoreDevs_Install(char **Options)
95 {
96         DevFS_AddDevice( &gCoreDevs_Null );
97         DevFS_AddDevice( &gCoreDevs_Zero );
98         DevFS_AddDevice( &gCoreDevs_One  );
99         DevFS_AddDevice( &gCoreDevs_FRandom  );
100         //DevFS_AddDevice( &gCoreDevs_GRandom  );
101         return MODULE_ERR_OK;
102 }
103
104 size_t CoreDevs_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
105 {
106         return Length;  // Ignore
107 }
108
109 size_t CoreDevs_Read_Zero(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
110 {
111         memset(Buffer, 0, Length);
112         return Length;
113 }
114
115 size_t CoreDevs_Read_One (tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
116 {
117         Uint8   *ptr = Buffer;
118         size_t  rem;
119         for( rem = Length; rem --; ptr ++ )
120                 *ptr = 0xFF;
121         return Length;
122 }
123
124 size_t CoreDevs_Read_Null(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
125 {
126         return 0;
127 }
128
129 //! Fast random number generator
130 size_t CoreDevs_Read_FRandom(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
131 {
132         Uint8   *cbuf = Buffer;
133         for( int i = 0; i < Length; i ++ )
134                 *cbuf++ = rand();
135         return Length;
136 }
137
138 size_t CoreDevs_Read_GRandom(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
139 {
140         Log_Error("CoreDevs", "GRandom is unimplimented");
141         return -1;
142 }
143

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