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 );
142 static const char *csaDevIOCtls[] = {DRV_IOCTLNAMES, DRV_JOY_IOCTLNAMES, NULL};
143 int HID_Mouse_Dev_IOCtl(tVFS_Node *Node, int ID, void *Data)
147 BASE_IOCTLS(DRV_TYPE_JOYSTICK, "USBMouse", 0x050, csaDevIOCtls);
151 void HID_Mouse_Dev_Reference(tVFS_Node *Node)
153 Node->ReferenceCount ++;
155 void HID_Mouse_Dev_Close(tVFS_Node *Node)
157 Node->ReferenceCount --;
160 // ----------------------------------------------------------------------------
161 // Data input / Update
162 // ----------------------------------------------------------------------------
164 * \brief Read a set amounts of bits from a stream
165 * \param Data Base of data
166 * \param Offset Bit offset
167 * \param Length Number of bits to read
168 * \return Sign-extended value
170 Sint32 _ReadBits(void *Data, int Offset, int Length)
174 Uint8 *bytes = (Uint8*)Data + Offset / 8;
177 if( Length > 32 ) return 0;
182 if( Length < 8 - (Offset & 7) )
184 rv = (*bytes >> Offset) & ((1 << Length)-1);
188 rv = (*bytes >> Offset);
190 dest_ofs = Offset & 7;
191 Length -= Offset & 7;
198 rv |= *bytes << dest_ofs;
206 rv |= (*bytes & ((1 << Length)-1)) << dest_ofs;
212 if( rv & (1 << (Length-1)) )
213 rv |= 0xFFFFFFFF << Length;
218 * \brief Handle an update from the device
220 void HID_Mouse_DataAvail(tUSBInterface *Dev, int EndPt, int Length, void *Data)
225 info = USB_GetDeviceDataPtr(Dev);
228 Log_Debug("USBMouse", "info = %p", info);
231 for( int i = 0; i < info->nMappings; i ++ )
234 Uint8 dest = info->Mappings[i].Dest;
236 if( ofs + info->Mappings[i].BitSize > Length * 8 )
239 value = _ReadBits(Data, ofs, info->Mappings[i].BitSize);
240 ofs += info->Mappings[i].BitSize;
242 if( dest == 0xFF ) continue ;
247 info->Axies[ dest & 0x7F ].CurValue = value;
252 info->Buttons[ dest & 0x7F ] = (value == 0) ? 0 : 0xFF;
256 // Update axis positions
257 for( int i = 0; i < MAX_AXIES; i ++ )
262 newpos = info->Axies[i].CursorPos + info->Axies[i].CurValue;
264 if(newpos < 0) newpos = 0;
265 if(newpos > info->AxisLimits[i]) newpos = info->AxisLimits[i];
267 info->Axies[i].CursorPos = newpos;
269 Log_Debug("USBMouse", "New Pos (%i,%i,%i)",
270 info->Axies[0].CursorPos, info->Axies[1].CursorPos, info->Axies[2].CursorPos
274 // ----------------------------------------------------------------------------
275 // Device initialisation
276 // ----------------------------------------------------------------------------
278 * \brief Handle the opening of a collection
280 tHID_ReportCallbacks *HID_Mouse_Report_Collection(
282 tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local,
288 info = USB_GetDeviceDataPtr(Dev);
292 info = calloc( sizeof(tHID_Mouse), 1 );
293 info->DataAvail = HID_Mouse_DataAvail;
294 info->Node.ImplPtr = info;
295 info->Node.Type = &gHID_Mouse_DevNodeType;
297 info->FileHeader = (void*)info->FileData;
298 info->Axies = (void*)(info->FileHeader + 1);
299 info->Buttons = (void*)(info->Axies + MAX_AXIES);
301 LOG("Initialised new mouse at %p", info);
303 USB_SetDeviceDataPtr(Dev, info);
307 info->CollectionDepth ++;
310 return &gHID_Mouse_ReportCBs;
314 * \brief Handle the end of a collection
316 void HID_Mouse_Report_EndCollection(tUSBInterface *Dev)
320 info = USB_GetDeviceDataPtr(Dev);
322 Log_Error("HID", "HID is not initialised when _AddInput is called");
326 if( info->CollectionDepth == 0 )
328 // Perform final initialisation steps
329 Mutex_Acquire(&glHID_MouseListLock);
330 gpHID_LastMouse->Next = info;
331 gpHID_LastMouse = info;
332 gHID_Mouse_DevFS.RootNode.Size ++;
333 Mutex_Release(&glHID_MouseListLock);
337 info->CollectionDepth --;
342 * \brief Add a new input mapping
344 void HID_int_AddInput(tUSBInterface *Dev, Uint32 Usage, Uint8 Size, Uint32 Min, Uint32 Max)
349 info = USB_GetDeviceDataPtr(Dev);
351 Log_Error("HID", "HID is not initialised when _AddInput is called");
355 // --- Get the destination for the field ---
358 case 0x00010030: tag = 0x80; break; // Generic Desktop - X
359 case 0x00010031: tag = 0x81; break; // Generic Desktop - Y
360 case 0x00010038: tag = 0x82; break; // Generic Desktop - Wheel
361 case 0x00090001: tag = 0x00; break; // Button 1
362 case 0x00090002: tag = 0x01; break; // Button 2
363 case 0x00090003: tag = 0x02; break; // Button 3
364 case 0x00090004: tag = 0x03; break; // Button 4
365 case 0x00090005: tag = 0x04; break; // Button 5
366 default: tag = 0xFF; break;
369 // --- Add to list of mappings ---
371 info->Mappings = realloc(info->Mappings, info->nMappings * sizeof(info->Mappings[0]));
373 info->Mappings[ info->nMappings - 1].Dest = tag;
374 info->Mappings[ info->nMappings - 1].BitSize = Size;
376 // --- Update Min/Max for Axies ---
378 if( tag != 0xFF && (tag & 0x80) )
380 info->Axies[ tag & 0x7F ].MinValue = Min;
381 info->Axies[ tag & 0x7F ].MaxValue = Max;
386 * \brief Handle an input item in a report
388 void HID_Mouse_Report_Input(
390 tHID_ReportGlobalState *Global, tHID_ReportLocalState *Local,
395 for( int i = 0; i < Global->ReportCount; i ++ )
398 if( i < Local->Usages.nItems )
399 usage = Local->Usages.Items[i];
401 HID_int_AddInput(Dev, usage, Global->ReportSize, Global->LogMin, Global->LogMax);