| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <math.h> |
| 28 |
#include <stdlib.h> |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include <mach/mach.h> |
| 34 |
#include <CoreFoundation/CFNumber.h> |
| 35 |
#include <IOKit/IOKitLib.h> |
| 36 |
#include <IOKit/IOCFPlugIn.h> |
| 37 |
#include <IOKit/usb/IOUSBLib.h> |
| 38 |
|
| 39 |
|
| 40 |
#include "macosx_usb_hid.h" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
USBHIDHandle OpenUSBHID( int index, int idVendor, int idProduct, int versionNumber, int flags ) |
| 45 |
{ |
| 46 |
|
| 47 |
|
| 48 |
IOUSBDeviceInterface **dev; |
| 49 |
IOCFPlugInInterface **iodev; |
| 50 |
kern_return_t err; |
| 51 |
CFMutableDictionaryRef matchingDictionary = 0; |
| 52 |
CFNumberRef numberRef; |
| 53 |
UInt8 inPipeRef; |
| 54 |
io_service_t usbDeviceRef; |
| 55 |
io_iterator_t iterator = 0; |
| 56 |
|
| 57 |
SInt32 score; |
| 58 |
|
| 59 |
UInt8 numConf; |
| 60 |
IOUSBConfigurationDescriptorPtr confDesc; |
| 61 |
io_service_t usbInterfaceRef; |
| 62 |
IOUSBFindInterfaceRequest interfaceRequest; |
| 63 |
IOUSBInterfaceInterface183 **intf = 0; |
| 64 |
UInt8 numPipes; |
| 65 |
UInt8 direction, number, transferType, interval; |
| 66 |
int i; |
| 67 |
UInt16 maxPacketSize; |
| 68 |
|
| 69 |
mach_port_t masterPort; |
| 70 |
|
| 71 |
|
| 72 |
err = IOMasterPort(MACH_PORT_NULL, &masterPort); |
| 73 |
if (err) |
| 74 |
{ |
| 75 |
fprintf( stderr, "USBSimpleExample: could not create master port, err = %08x\n", err); |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName); |
| 82 |
if (!matchingDictionary) |
| 83 |
{ |
| 84 |
fprintf( stderr, "USBSimpleExample: could not create matching dictionary\n"); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
if( 1 ){ |
| 89 |
|
| 90 |
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idVendor); |
| 91 |
if (!numberRef) |
| 92 |
{ |
| 93 |
fprintf( stderr, "USBSimpleExample: could not create CFNumberRef for vendor\n"); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBVendorID), numberRef); |
| 98 |
CFRelease(numberRef); |
| 99 |
numberRef = 0; |
| 100 |
} |
| 101 |
|
| 102 |
if( 1 ){ |
| 103 |
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idProduct); |
| 104 |
if (!numberRef) |
| 105 |
{ |
| 106 |
fprintf( stderr, "USBSimpleExample: could not create CFNumberRef for product\n"); |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBProductID), numberRef); |
| 111 |
CFRelease(numberRef); |
| 112 |
numberRef = 0; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
err = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator); |
| 118 |
matchingDictionary = 0; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if((usbDeviceRef = IOIteratorNext(iterator))) |
| 123 |
{ |
| 124 |
|
| 125 |
|
| 126 |
err = IOCreatePlugInInterfaceForService(usbDeviceRef, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score); |
| 127 |
if (err || !iodev) |
| 128 |
{ |
| 129 |
fprintf( stderr, "dealWithDevice: unable to create plugin. ret = %08x, iodev = %p\n", err, iodev); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
err = (*iodev)->QueryInterface(iodev, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID)&dev); |
| 134 |
(*iodev)->Release(iodev); |
| 135 |
if (err || !dev) |
| 136 |
{ |
| 137 |
fprintf( stderr, "dealWithDevice: unable to create a device interface. ret = %08x, dev = %p\n", err, dev); |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
err = (*dev)->USBDeviceOpen(dev); |
| 142 |
if (err) |
| 143 |
{ |
| 144 |
fprintf( stderr, "dealWithDevice: unable to open device. ret = %08x\n", err); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
err = (*dev)->GetNumberOfConfigurations(dev, &numConf); |
| 149 |
if (err || !numConf) |
| 150 |
{ |
| 151 |
fprintf( stderr, "dealWithDevice: unable to obtain the number of configurations. ret = %08x\n", err); |
| 152 |
(*dev)->USBDeviceClose(dev); |
| 153 |
(*dev)->Release(dev); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
fprintf( stderr, "dealWithDevice: found %d configurations\n", numConf); |
| 158 |
|
| 159 |
err = (*dev)->GetConfigurationDescriptorPtr(dev, 0, &confDesc); |
| 160 |
if (err) |
| 161 |
{ |
| 162 |
fprintf( stderr, "dealWithDevice:unable to get config descriptor for index 0\n"); |
| 163 |
(*dev)->USBDeviceClose(dev); |
| 164 |
(*dev)->Release(dev); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
err = (*dev)->SetConfiguration(dev, confDesc->bConfigurationValue); |
| 169 |
if (err) |
| 170 |
{ |
| 171 |
fprintf( stderr, "dealWithDevice: unable to set the configuration\n"); |
| 172 |
(*dev)->USBDeviceClose(dev); |
| 173 |
(*dev)->Release(dev); |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
}else{ |
| 178 |
return intf; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
interfaceRequest.bInterfaceClass = 3; |
| 184 |
interfaceRequest.bInterfaceSubClass = 0; |
| 185 |
interfaceRequest.bInterfaceProtocol = kIOUSBFindInterfaceDontCare; |
| 186 |
interfaceRequest.bAlternateSetting = kIOUSBFindInterfaceDontCare; |
| 187 |
|
| 188 |
err = (*dev)->CreateInterfaceIterator(dev, &interfaceRequest, &iterator); |
| 189 |
if (err) |
| 190 |
{ |
| 191 |
fprintf( stderr, "dealWithDevice: unable to create interface iterator\n"); |
| 192 |
(*dev)->USBDeviceClose(dev); |
| 193 |
(*dev)->Release(dev); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if( (usbInterfaceRef = IOIteratorNext(iterator)) ) |
| 200 |
{ |
| 201 |
|
| 202 |
|
| 203 |
fprintf( stderr, "found interface: %u\n", usbInterfaceRef); |
| 204 |
|
| 205 |
err = IOCreatePlugInInterfaceForService(usbInterfaceRef, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score); |
| 206 |
if (err || !iodev) |
| 207 |
{ |
| 208 |
fprintf( stderr, "dealWithInterface: unable to create plugin. ret = %08x, iodev = %p\n", err, iodev); |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
err = (*iodev)->QueryInterface(iodev, CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID183), (LPVOID)&intf); |
| 213 |
(*iodev)->Release(iodev); |
| 214 |
if (err || !intf) |
| 215 |
{ |
| 216 |
fprintf( stderr, "dealWithInterface: unable to create a device interface. ret = %08x, intf = %p\n", err, intf); |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
err = (*intf)->USBInterfaceOpenSeize(intf); |
| 221 |
if (err) |
| 222 |
{ |
| 223 |
(*intf)->Release( intf ); |
| 224 |
fprintf( stderr, "dealWithInterface: unable to open interface. ret = %08x, score = %d \n", err, (int)score); |
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
}else{ |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
err = (*intf)->GetNumEndpoints(intf, &numPipes); |
| 234 |
if (err) |
| 235 |
{ |
| 236 |
fprintf( stderr, "dealWithInterface: unable to get number of endpoints. ret = %08x\n", err); |
| 237 |
(*intf)->USBInterfaceClose(intf); |
| 238 |
(*intf)->Release(intf); |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
fprintf( stderr, "dealWithInterface: found %d pipes\n", numPipes); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
for (i=1; i <= numPipes; i++) |
| 248 |
{ |
| 249 |
err = (*intf)->GetPipeProperties(intf, i, &direction, &number, &transferType, &maxPacketSize, &interval); |
| 250 |
if (err) |
| 251 |
{ |
| 252 |
(*intf)->USBInterfaceClose(intf); |
| 253 |
(*intf)->Release(intf); |
| 254 |
fprintf( stderr, "dealWithPipes: unable to get pipe properties for pipe %d, err = %08x\n", i, err); |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
if (transferType != kUSBInterrupt) |
| 259 |
{ |
| 260 |
fprintf( stderr, "dealWithPipes: skipping pipe %d because it is not an interupt pipe\n", i); |
| 261 |
fprintf( stderr, "Pipe is type %d\n", transferType); |
| 262 |
|
| 263 |
continue; |
| 264 |
} |
| 265 |
|
| 266 |
if (direction == kUSBIn) |
| 267 |
{ |
| 268 |
fprintf( stderr, "dealWithPipes: grabbing BULK IN pipe index %d, number %d, inteveral %d \n",i, number, interval); |
| 269 |
inPipeRef = i; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return intf; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
void CloseUSBHID( USBHIDHandle usbHidHandle ) |
| 280 |
{ |
| 281 |
IOUSBInterfaceInterface183 **intf = usbHidHandle; |
| 282 |
|
| 283 |
(*intf)->USBInterfaceClose(intf); |
| 284 |
(*intf)->Release(intf); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
int ReadUSBHID( USBHIDHandle usbHidHandle, void *data, int count ) |
| 290 |
{ |
| 291 |
IOUSBInterfaceInterface183 **intf = usbHidHandle; |
| 292 |
UInt32 readSize = count; |
| 293 |
|
| 294 |
(*intf)->ReadPipe(intf, 1, data, &readSize ); |
| 295 |
|
| 296 |
return readSize; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
int HIrequest(void *usbHidHandle, UInt8 RequestType, UInt16 Request, UInt16 ReportId, int index, unsigned char *raw_buf, int ReportSize ) |
| 301 |
{ |
| 302 |
IOUSBDevRequest request; |
| 303 |
IOUSBInterfaceInterface183 **intf = usbHidHandle; |
| 304 |
IOUSBDeviceInterface **device; |
| 305 |
io_service_t usbDeviceRef; |
| 306 |
IOReturn success = 0 ; |
| 307 |
char test[2]; |
| 308 |
|
| 309 |
|
| 310 |
request.bmRequestType = RequestType; |
| 311 |
request.bRequest = Request; |
| 312 |
request.wValue = ReportId; |
| 313 |
request.wIndex = index; |
| 314 |
request.wLength = ReportSize; |
| 315 |
request.pData = raw_buf; |
| 316 |
|
| 317 |
|
| 318 |
success = (*intf)->ControlRequest(intf, 0, &request); |
| 319 |
|
| 320 |
if(success != kIOReturnSuccess) |
| 321 |
printf("Hi Request Failed \n"); |
| 322 |
return success; |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
int usb_get_report(void *usbHidHandle,int ReportId, unsigned char *raw_buf, int ReportSize ) |
| 328 |
{ |
| 329 |
int err; |
| 330 |
|
| 331 |
err = HIrequest(usbHidHandle, |
| 332 |
0xA1, |
| 333 |
0x01, |
| 334 |
ReportId+(0x03<<8), |
| 335 |
1, |
| 336 |
raw_buf, ReportSize); |
| 337 |
|
| 338 |
|
| 339 |
return err; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
int usb_set_report(void *usbHidHandle,int ReportId, unsigned char *raw_buf, int ReportSize ) |
| 344 |
{ |
| 345 |
return HIrequest(usbHidHandle, |
| 346 |
0x21, |
| 347 |
0x09, |
| 348 |
ReportId+(0x03<<8), |
| 349 |
1, |
| 350 |
raw_buf, ReportSize); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
int SetUSBHIDFeature( USBHIDHandle handle, char *report, int count ) |
| 355 |
{ |
| 356 |
return usb_set_report(handle, report[0]&0xf, (unsigned char*) report, count ); |
| 357 |
|
| 358 |
} |
| 359 |
|
| 360 |
int GetUSBHIDFeature( USBHIDHandle handle, char *report, int count ) |
| 361 |
{ |
| 362 |
return usb_get_report(handle, report[0]&0xf, (unsigned char*) report, count ); |
| 363 |
|
| 364 |
} |
| 365 |
|