Debian Codegraph @

p5glove/libp5glove/usb_hid_macosx.c

raw symbols SCIP
1 /*
2 Copyright (c) 2004 Tim Kreger <[email protected]>
3
4 Macintosh OS X USB HID I/O routines
5
6 Created by Tim Kreger on Wed Mar 17 2004.
7 */
8 /*
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24
25
26 #include <stdio.h>
27 #include <math.h>
28 #include <stdlib.h>
29
30
31 // Mac Headers
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 // todo: index and flags are currently ignored, see win32 for example implementation.
47
48 IOUSBDeviceInterface **dev;
49 IOCFPlugInInterface **iodev;
50 kern_return_t err;
51 CFMutableDictionaryRef matchingDictionary = 0; // requires <IOKit/IOKitLib.h>
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; // requires <mach/mach.h>
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 // configure matching dictionary
80
81 matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName); // requires <IOKit/usb/IOUSBLib.h>
82 if (!matchingDictionary)
83 {
84 fprintf( stderr, "USBSimpleExample: could not create matching dictionary\n");
85 return 0;
86 }
87
88 if( 1 ){ //todo: use flag to decide whether to add vendor id
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 ){ // todo: use flag to decide whether to add product id
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 // get iterator to matching devices
116
117 err = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
118 matchingDictionary = 0;
119
120 // get first matching device
121
122 if((usbDeviceRef = IOIteratorNext(iterator)))
123 {
124 // open device
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); // done with this
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); // get the first config desc (index 0)
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 // set criteria for interface iterator
182
183 interfaceRequest.bInterfaceClass = 3; // requested class (3==hid class)
184 interfaceRequest.bInterfaceSubClass = 0; // requested subclass
185 interfaceRequest.bInterfaceProtocol = kIOUSBFindInterfaceDontCare; // requested protocol
186 interfaceRequest.bAlternateSetting = kIOUSBFindInterfaceDontCare; // requested alt setting
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 // get first interface matching criteria
198
199 if( (usbInterfaceRef = IOIteratorNext(iterator)) )
200 {
201 // open interface
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); // done with this
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 // find pipe with transfer type kUSBInterrupt and direction kUSBIn
245 // store pipe number in inPipeRef
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 ); // 1 should be inPipeRef
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 // 0x21 for set, 0xA1for get
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 // (*theDev)->DeviceRequest(theDev, &request);
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, /* Request Type */
333 0x01, /* HID_REPORT_GET */
334 ReportId+(0x03<<8), /* HID_REPORT_TYPE_FEATURE */
335 1, /* Index */
336 raw_buf, ReportSize);
337 // if (err == -EPIPE)
338 // goto retry;
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, /* Request Type */
347 0x09, /* HID_REPORT_SET */
348 ReportId+(0x03<<8), /* HID_REPORT_TYPE_FEATURE */
349 1, /* Index */
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