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 Uint64 HID_Mouse_Dev_Read(tVFS_Node *Node, Uint64 Offset, Uint64 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 = {
79 .RootNode = {.Type = &gHID_Mouse_RootNodeType}
81 tHID_ReportCallbacks gHID_Mouse_ReportCBs = {
82 .Collection = HID_Mouse_Report_Collection,
83 .EndCollection = HID_Mouse_Report_EndCollection,
84 .Input = HID_Mouse_Report_Input,
86 tMutex glHID_MouseListLock;
87 tHID_Mouse *gpHID_FirstMouse;
88 tHID_Mouse *gpHID_LastMouse = (tHID_Mouse*)&gpHID_FirstMouse;
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
94 char *HID_Mouse_Root_ReadDir(tVFS_Node *Node, int Pos)
97 if(Pos < 0 || Pos >= Node->Size) return NULL;
99 snprintf(data, 3, "%i", Pos);
103 tVFS_Node *HID_Mouse_Root_FindDir(tVFS_Node *Node, const char *Name)
109 if( Name[0] == '\0' )
112 ofs = ParseInt(Name, &ID);
113 if( ofs == 0 || Name[ofs] != '\0' )
116 // Scan list, locate item
117 Mutex_Acquire(&glHID_MouseListLock);
118 for( mouse = gpHID_FirstMouse; mouse && ID --; mouse = mouse->Next ) ;
119 mouse->Node.ReferenceCount ++;
120 Mutex_Release(&glHID_MouseListLock);
125 Uint64 HID_Mouse_Dev_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
127 tHID_Mouse *info = Node->ImplPtr;
129 if( Offset > FILE_SIZE ) return 0;
131 if( Length > FILE_SIZE ) Length = FILE_SIZE;
132 if( Offset + Length > FILE_SIZE ) Length = FILE_SIZE - Offset;
134 memcpy( Buffer, info->FileData + Offset, Length );
139 static const char *csaDevIOCtls[] = {DRV_IOCTLNAMES, DRV_JOY_IOCTLNAMES, NULL};
140 int HID_Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data)
144 BASE_IOCTLS(DRV_TYPE_JOYSTICK, "USBMouse", 0x050, csaDevIOCtls);
148 void HID_Mouse_Dev_Reference(tVFS_Node *Node)
150 Node->ReferenceCount ++;
152 void HID_Mouse_Dev_Close(tVFS_Node *Node)
154 Node->ReferenceCount --;
157 // ----------------------------------------------------------------------------
158 // Data input / Update
159 // ----------------------------------------------------------------------------
161 * \brief Read a set amounts of bits from a stream
162 * \param Data Base of data
163 * \param Offset Bit offset
164 * \param Length Number of bits to read
165 * \return Sign-extended value
167 Sint32 _ReadBits(void *Data, int Offset, int Length)
171 Uint8 *bytes = (Uint8*)Data + Offset / 8;
174 if( Length > 32 ) return 0;
179 if( Length < 8 - (Offset & 7) )
181 rv = (*bytes >> Offset) & ((1 << Length)-1);
185 rv = (*bytes >> Offset);
187 dest_ofs = Offset & 7;
188 Length -= Offset & 7;
195 rv |= *bytes << dest_ofs;
203 rv |= (*bytes & ((1 << Length)-1)) << dest_ofs;
209 if( rv & (1 << (Length-1)) )
210 rv |= 0xFFFFFFFF << Length;
215 * \brief Handle an update from the device
217 void HID_Mouse_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data)
222 info = USB_GetDeviceDataPtr(Dev);
225 Log_Debug("USBMouse", "info = %p", info);
228 for( int i = 0; i < info->nMappings; i ++ )
231 Uint8 dest = info->Mappings[i].Dest;
233 if( ofs + info->Mappings[i].BitSize > Length * 8 )
236 value = _ReadBits(Data, ofs, info->Mappings[i].BitSize);
237 ofs += info->Mappings[i].BitSize;
239 if( dest == 0xFF ) continue ;
244 info->Axies[ dest & 0x7F ].CurValue = value;
249 info->Buttons[ dest & 0x7F ] = (value == 0) ? 0 : 0xFF;
253 // Update axis positions
254 for( int i = 0; i < MAX_AXIES; i ++ )
259 newpos = info->Axies[i].CursorPos + info->Axies[i].CurValue;
261 if(newpos < 0) newpos = 0;
262 if(newpos > info->AxisLimits[i]) newpos = info->AxisLimits[i];
264 info->Axies[i].CursorPos = newpos;
266 Log_Debug("USBMouse", "New Pos (%i,%i,%i)",
267 info->Axies[0].CursorPos, info->Axies[1].CursorPos, info->Axies[2].CursorPos
271 // ----------------------------------------------------------------------------
272 // Device initialisation
273 // ----------------------------------------------------------------------------
275 * \brief Handle the opening of a collection
277 tHID_ReportCallbacks *HID_Mouse_Report_Collection(
279 tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local,
285 info = USB_GetDeviceDataPtr(Dev);
289 info = calloc( sizeof(tHID_Mouse), 1 );
290 info->DataAvail = HID_Mouse_DataAvail;
291 info->Node.ImplPtr = info;
292 info->Node.Type = &gHID_Mouse_DevNodeType;
294 info->FileHeader = (void*)info->FileData;
295 info->Axies = (void*)(info->FileHeader + 1);
296 info->Buttons = (void*)(info->Axies + MAX_AXIES);
298 LOG("Initialised new mouse at %p", info);
300 USB_SetDeviceDataPtr(Dev, info);
304 info->CollectionDepth ++;
307 return &gHID_Mouse_ReportCBs;
311 * \brief Handle the end of a collection
313 void HID_Mouse_Report_EndCollection(tUSBInterface *Dev)
317 info = USB_GetDeviceDataPtr(Dev);
319 Log_Error("HID", "HID is not initialised when _AddInput is called");
323 if( info->CollectionDepth == 0 )
325 // Perform final initialisation steps
326 Mutex_Acquire(&glHID_MouseListLock);
327 gpHID_LastMouse->Next = info;
328 gpHID_LastMouse = info;
329 gHID_Mouse_DevFS.RootNode.Size ++;
330 Mutex_Release(&glHID_MouseListLock);
334 info->CollectionDepth --;
339 * \brief Add a new input mapping
341 void HID_int_AddInput(tUSBInterface *Dev, Uint32 Usage, Uint8 Size, Uint32 Min, Uint32 Max)
346 info = USB_GetDeviceDataPtr(Dev);
348 Log_Error("HID", "HID is not initialised when _AddInput is called");
352 // --- Get the destination for the field ---
355 case 0x00010030: tag = 0x80; break; // Generic Desktop - X
356 case 0x00010031: tag = 0x81; break; // Generic Desktop - Y
357 case 0x00010038: tag = 0x82; break; // Generic Desktop - Wheel
358 case 0x00090001: tag = 0x00; break; // Button 1
359 case 0x00090002: tag = 0x01; break; // Button 2
360 case 0x00090003: tag = 0x02; break; // Button 3
361 case 0x00090004: tag = 0x03; break; // Button 4
362 case 0x00090005: tag = 0x04; break; // Button 5
363 default: tag = 0xFF; break;
366 // --- Add to list of mappings ---
368 info->Mappings = realloc(info->Mappings, info->nMappings * sizeof(info->Mappings[0]));
370 info->Mappings[ info->nMappings - 1].Dest = tag;
371 info->Mappings[ info->nMappings - 1].BitSize = Size;
373 // --- Update Min/Max for Axies ---
375 if( tag != 0xFF && (tag & 0x80) )
377 info->Axies[ tag & 0x7F ].MinValue = Min;
378 info->Axies[ tag & 0x7F ].MaxValue = Max;
383 * \brief Handle an input item in a report
385 void HID_Mouse_Report_Input(
387 tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local,
392 for( int i = 0; i < Global->ReportCount; i ++ )
395 if( i < Local->Usages.nItems )
396 usage = Local->Usages.Items[i];
398 HID_int_AddInput(Dev, usage, Global->ReportSize, Global->LogMin, Global->LogMax);