2 * Acess2 USB Stack HID Driver
3 * - By John Hodge (thePowersGang)
10 #include "hid_reports.h"
12 #include <api_drv_joystick.h>
15 #define MAX_AXIES 3 // X, Y, Scroll
16 #define MAX_BUTTONS 5 // Left, Right, Middle, ...
17 #define FILE_SIZE (sizeof(tJoystick_FileHeader) + MAX_AXIES*sizeof(tJoystick_Axis) + MAX_BUTTONS)
20 typedef struct sHID_Mouse tHID_Mouse;
25 tUSB_DataCallback DataAvail;
31 Uint8 FileData[ FILE_SIZE ];
32 tJoystick_FileHeader *FileHeader;
33 tJoystick_Axis *Axies;
36 // Limits for axis positions
37 Uint16 AxisLimits[MAX_AXIES];
42 Uint8 Dest; // 0x00-0x7F = Buttons, 0x80-0xFE = Axies, 0xFF = Ignore
46 // - Initialisation only data
51 char *HID_Mouse_Root_ReadDir(tVFS_Node *Node, int Pos);
52 tVFS_Node *HID_Mouse_Root_FindDir(tVFS_Node *Node, const char *Name);
53 size_t HID_Mouse_Dev_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
54 int HID_Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data);
55 void HID_Mouse_Dev_Reference(tVFS_Node *Node);
56 void HID_Mouse_Dev_Close(tVFS_Node *Node);
58 void HID_Mouse_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data);
60 tHID_ReportCallbacks *HID_Mouse_Report_Collection(tUSBInterface *Dev, tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local, Uint32 Value);
61 void HID_Mouse_Report_EndCollection(tUSBInterface *Dev);
62 void HID_Mouse_Report_Input(tUSBInterface *Dev, tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local, Uint32 Value);
65 tVFS_NodeType gHID_Mouse_RootNodeType = {
66 .TypeName = "HID Mouse Root",
67 .ReadDir = HID_Mouse_Root_ReadDir,
68 .FindDir = HID_Mouse_Root_FindDir
70 tVFS_NodeType gHID_Mouse_DevNodeType = {
71 .TypeName = "HID Mouse Dev",
72 .Read = HID_Mouse_Dev_Read,
73 .IOCtl = HID_Mouse_Dev_IOCtl,
74 .Reference = HID_Mouse_Dev_Reference,
75 .Close = HID_Mouse_Dev_Close,
77 tDevFS_Driver gHID_Mouse_DevFS = {
80 .Type = &gHID_Mouse_RootNodeType,
81 .Flags = VFS_FFLAG_DIRECTORY
84 tHID_ReportCallbacks gHID_Mouse_ReportCBs = {
85 .Collection = HID_Mouse_Report_Collection,
86 .EndCollection = HID_Mouse_Report_EndCollection,
87 .Input = HID_Mouse_Report_Input,
89 tMutex glHID_MouseListLock;
90 tHID_Mouse *gpHID_FirstMouse;
91 tHID_Mouse *gpHID_LastMouse = (tHID_Mouse*)&gpHID_FirstMouse;
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
97 char *HID_Mouse_Root_ReadDir(tVFS_Node *Node, int Pos)
100 if(Pos < 0 || Pos >= Node->Size) return NULL;
102 snprintf(data, 3, "%i", Pos);
106 tVFS_Node *HID_Mouse_Root_FindDir(tVFS_Node *Node, const char *Name)
112 if( Name[0] == '\0' )
115 ofs = ParseInt(Name, &ID);
116 if( ofs == 0 || Name[ofs] != '\0' )
119 // Scan list, locate item
120 Mutex_Acquire(&glHID_MouseListLock);
121 for( mouse = gpHID_FirstMouse; mouse && ID --; mouse = mouse->Next ) ;
122 mouse->Node.ReferenceCount ++;
123 Mutex_Release(&glHID_MouseListLock);
128 size_t HID_Mouse_Dev_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
130 tHID_Mouse *info = Node->ImplPtr;
132 if( Offset > FILE_SIZE ) return 0;
134 if( Length > FILE_SIZE ) Length = FILE_SIZE;
135 if( Offset + Length > FILE_SIZE ) Length = FILE_SIZE - Offset;
137 memcpy( Buffer, info->FileData + Offset, Length );
139 VFS_MarkAvaliable( &info->Node, 0 );
144 static const char *csaDevIOCtls[] = {DRV_IOCTLNAMES, DRV_JOY_IOCTLNAMES, NULL};
145 int HID_Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data)
147 tJoystick_NumValue *numval = Data;
148 tHID_Mouse *info = Node->ImplPtr;
151 BASE_IOCTLS(DRV_TYPE_JOYSTICK, "USBMouse", 0x050, csaDevIOCtls);
153 case JOY_IOCTL_GETSETAXISLIMIT:
154 if( !numval || !CheckMem(numval, sizeof(*numval)) )
156 if(numval->Num < 0 || numval->Num >= MAX_AXIES)
158 if(numval->Value != -1)
159 info->AxisLimits[numval->Num] = numval->Value;
160 return info->AxisLimits[numval->Num];
162 case JOY_IOCTL_GETSETAXISPOSITION:
163 if( !numval || !CheckMem(numval, sizeof(*numval)) )
165 if(numval->Num < 0 || numval->Num >= MAX_AXIES)
167 if(numval->Value != -1)
168 info->Axies[numval->Num].CursorPos = numval->Value;
169 return info->Axies[numval->Num].CursorPos;
173 void HID_Mouse_Dev_Reference(tVFS_Node *Node)
175 Node->ReferenceCount ++;
177 void HID_Mouse_Dev_Close(tVFS_Node *Node)
179 Node->ReferenceCount --;
182 // ----------------------------------------------------------------------------
183 // Data input / Update
184 // ----------------------------------------------------------------------------
186 * \brief Read a set amounts of bits from a stream
187 * \param Data Base of data
188 * \param Offset Bit offset
189 * \param Length Number of bits to read
190 * \return Sign-extended value
192 Sint32 _ReadBits(void *Data, int Offset, int Length)
197 Uint8 *bytes = (Uint8*)Data + Offset / 8;
200 if( Length > 32 ) return 0;
205 if( Length < 8 - (Offset & 7) )
207 rv = (*bytes >> Offset) & ((1 << Length)-1);
211 rv = (*bytes >> Offset);
213 dest_ofs = Offset & 7;
214 rem = Length - (Offset & 7);
221 rv |= *bytes << dest_ofs;
229 rv |= (*bytes & ((1 << rem)-1)) << dest_ofs;
234 if( rv & (1 << (Length-1)) )
235 rv |= 0xFFFFFFFF << Length;
240 * \brief Handle an update from the device
242 void HID_Mouse_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data)
247 info = USB_GetDeviceDataPtr(Dev);
251 for( int i = 0; i < info->nMappings; i ++ )
254 Uint8 dest = info->Mappings[i].Dest;
256 if( ofs + info->Mappings[i].BitSize > Length * 8 )
259 value = _ReadBits(Data, ofs, info->Mappings[i].BitSize);
260 LOG("%i+%i: value = %i", ofs, info->Mappings[i].BitSize, value);
261 ofs += info->Mappings[i].BitSize;
263 if( dest == 0xFF ) continue ;
268 info->Axies[ dest & 0x7F ].CurValue = value;
273 info->Buttons[ dest & 0x7F ] = (value == 0) ? 0 : 0xFF;
277 // Update axis positions
278 for( int i = 0; i < MAX_AXIES; i ++ )
283 newpos = info->Axies[i].CursorPos + info->Axies[i].CurValue;
285 if(newpos < 0) newpos = 0;
286 if(newpos > info->AxisLimits[i]) newpos = info->AxisLimits[i];
288 info->Axies[i].CursorPos = newpos;
290 // Log_Debug("USBMouse", "New Pos (%i,%i,%i)",
291 // info->Axies[0].CursorPos, info->Axies[1].CursorPos, info->Axies[2].CursorPos
293 VFS_MarkAvaliable( &info->Node, 1 );
296 // ----------------------------------------------------------------------------
297 // Device initialisation
298 // ----------------------------------------------------------------------------
300 * \brief Handle the opening of a collection
302 tHID_ReportCallbacks *HID_Mouse_Report_Collection(
304 tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local,
310 info = USB_GetDeviceDataPtr(Dev);
314 info = calloc( sizeof(tHID_Mouse), 1 );
315 info->DataAvail = HID_Mouse_DataAvail;
316 info->Node.ImplPtr = info;
317 info->Node.Type = &gHID_Mouse_DevNodeType;
319 info->FileHeader = (void*)info->FileData;
320 info->Axies = (void*)(info->FileHeader + 1);
321 info->Buttons = (void*)(info->Axies + MAX_AXIES);
322 info->FileHeader->NAxies = MAX_AXIES;
323 info->FileHeader->NButtons = MAX_BUTTONS;
325 for( int i = 0; i < MAX_AXIES; i ++ ) {
326 info->Axies[i].MinValue = -10;
327 info->Axies[i].MaxValue = 10;
331 LOG("Initialised new mouse at %p", info);
333 USB_SetDeviceDataPtr(Dev, info);
337 info->CollectionDepth ++;
340 return &gHID_Mouse_ReportCBs;
344 * \brief Handle the end of a collection
346 void HID_Mouse_Report_EndCollection(tUSBInterface *Dev)
350 info = USB_GetDeviceDataPtr(Dev);
352 Log_Error("HID", "HID is not initialised when _AddInput is called");
356 if( info->CollectionDepth == 0 )
358 // Perform final initialisation steps
359 Mutex_Acquire(&glHID_MouseListLock);
360 gpHID_LastMouse->Next = info;
361 gpHID_LastMouse = info;
362 gHID_Mouse_DevFS.RootNode.Size ++;
363 Mutex_Release(&glHID_MouseListLock);
367 info->CollectionDepth --;
372 * \brief Add a new input mapping
374 void HID_int_AddInput(tUSBInterface *Dev, Uint32 Usage, Uint8 Size, Uint32 Min, Uint32 Max)
379 info = USB_GetDeviceDataPtr(Dev);
381 Log_Error("HID", "HID is not initialised when _AddInput is called");
385 // --- Get the destination for the field ---
388 case 0x00010030: tag = 0x80; break; // Generic Desktop - X
389 case 0x00010031: tag = 0x81; break; // Generic Desktop - Y
390 case 0x00010038: tag = 0x82; break; // Generic Desktop - Wheel
391 case 0x00090001: tag = 0x00; break; // Button 1
392 case 0x00090002: tag = 0x01; break; // Button 2
393 case 0x00090003: tag = 0x02; break; // Button 3
394 case 0x00090004: tag = 0x03; break; // Button 4
395 case 0x00090005: tag = 0x04; break; // Button 5
396 default: tag = 0xFF; break;
399 // --- Add to list of mappings ---
401 info->Mappings = realloc(info->Mappings, info->nMappings * sizeof(info->Mappings[0]));
403 info->Mappings[ info->nMappings - 1].Dest = tag;
404 info->Mappings[ info->nMappings - 1].BitSize = Size;
406 // --- Update Min/Max for Axies ---
408 if( tag != 0xFF && (tag & 0x80) )
410 info->Axies[ tag & 0x7F ].MinValue = Min;
411 info->Axies[ tag & 0x7F ].MaxValue = Max;
416 * \brief Handle an input item in a report
418 void HID_Mouse_Report_Input(
420 tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local,
425 for( int i = 0; i < Global->ReportCount; i ++ )
428 if( i < Local->Usages.nItems )
429 usage = Local->Usages.Items[i];
431 HID_int_AddInput(Dev, usage, Global->ReportSize, Global->LogMin, Global->LogMax);