3 * Copyright 2006,2009 Free Software Foundation, Inc.
5 * This file is part of GNU Radio.
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
24 * The following code was taken from LIBUSB verion 0.1.10a,
25 * and makes the fusb_darwin codes do-able in the current GR
26 * programming framework. Parts and pieces were taken from
27 * usbi.h, darwin.c, and error.h .
29 * LIBUSB version 0.1.10a is covered by the LGPL, version 2;
30 * These codes are used with permission from:
31 * (c) 2000-2003 Johannes Erdfelt <johannes@erdfelt.com>
32 * (c) 2002-2005 Nathan Hjelm <hjelmn@users.sourceforge.net>
33 * All rights reserved.
36 #ifndef __DARWIN_LIBUSB_H__
37 #define __DARWIN_LIBUSB_H__
39 #include <IOKit/IOCFBundle.h>
40 #include <IOKit/IOCFPlugIn.h>
41 #include <IOKit/usb/IOUSBLib.h>
42 #include <IOKit/IOKitLib.h>
46 static const char* darwin_error_strings[] = {
48 "device not opened for exclusive access",
49 "no connection to an IOService",
50 "no asyc port has been opened for interface",
51 "another process has device opened for exclusive access",
53 "could not establish a connection to Darin kernel",
59 darwin_error_str (int result)
62 case kIOReturnSuccess:
63 return (darwin_error_strings[0]);
64 case kIOReturnNotOpen:
65 return (darwin_error_strings[1]);
66 case kIOReturnNoDevice:
67 return (darwin_error_strings[2]);
68 case kIOUSBNoAsyncPortErr:
69 return (darwin_error_strings[3]);
70 case kIOReturnExclusiveAccess:
71 return (darwin_error_strings[4]);
72 case kIOUSBPipeStalled:
73 return (darwin_error_strings[5]);
75 return (darwin_error_strings[6]);
76 case kIOReturnBadArgument:
77 return (darwin_error_strings[7]);
79 return (darwin_error_strings[8]);
83 /* not a valid errorno outside darwin.c */
84 #define LUSBDARWINSTALL (ELAST+1)
87 darwin_to_errno (int result)
90 case kIOReturnSuccess:
92 case kIOReturnNotOpen:
94 case kIOReturnNoDevice:
95 case kIOUSBNoAsyncPortErr:
97 case kIOReturnExclusiveAccess:
99 case kIOUSBPipeStalled:
100 return LUSBDARWINSTALL;
101 case kIOReturnBadArgument:
110 USB_ERROR_TYPE_NONE = 0,
111 USB_ERROR_TYPE_STRING,
112 USB_ERROR_TYPE_ERRNO,
115 extern char usb_error_str[1024];
116 extern int usb_error_errno;
117 extern usb_error_type_t usb_error_type;
119 #define USB_ERROR(r, x) \
121 usb_error_type = USB_ERROR_TYPE_ERRNO; \
122 usb_error_errno = x; \
126 #define USB_ERROR_STR(r, x, format, args...) \
128 usb_error_type = USB_ERROR_TYPE_STRING; \
129 snprintf (usb_error_str, sizeof (usb_error_str) - 1, \
132 std::cerr << "USB error: " << usb_error_str << std::cerr; \
137 #define USB_ERROR_STR_ORIG(x, format, args...) \
139 usb_error_type = USB_ERROR_TYPE_STRING; \
140 snprintf (usb_error_str, sizeof (usb_error_str) - 1, \
143 std::cerr << "USB error: " << usb_error_str << std::endl; \
148 #define USB_ERROR_STR_NO_RET(x, format, args...) \
150 usb_error_type = USB_ERROR_TYPE_STRING; \
151 snprintf (usb_error_str, sizeof (usb_error_str) - 1, \
154 std::cerr << "USB error: " << usb_error_str << std::endl; \
159 * simple function that figures out what pipeRef
160 * is associated with an endpoint
162 static int ep_to_pipeRef (darwin_dev_handle *device, int ep)
165 UInt8 numep, direction, number;
166 UInt8 dont_care1, dont_care3;
171 std::cerr << "Converting ep address to pipeRef." << std::endl;
174 /* retrieve the total number of endpoints on this interface */
175 ret = (*(device->interface))->GetNumEndpoints(device->interface, &numep);
177 if ( usb_debug > 3 ) {
178 std::cerr << "ep_to_pipeRef: interface is "
179 << device->interface << std::endl;
181 USB_ERROR_STR_ORIG ( -ret, "ep_to_pipeRef: can't get number of "
182 "endpoints for interface" );
185 /* iterate through the pipeRefs until we find the correct one */
186 for (i = 1 ; i <= numep ; i++) {
187 ret = (*(device->interface))->GetPipeProperties
188 (device->interface, i, &direction, &number,
189 &dont_care1, &dont_care2, &dont_care3);
191 if (ret != kIOReturnSuccess) {
192 std::cerr << "ep_to_pipeRef: an error occurred getting "
193 << "pipe information on pipe " << i << std::endl;
195 USB_ERROR_STR_ORIG (-darwin_to_errno(ret),
196 "ep_to_pipeRef(GetPipeProperties): %s",
197 darwin_error_str(ret));
201 std::cerr << "ep_to_pipeRef: Pipe " << i << ": DIR: "
202 << direction << " number: " << number << std::endl;
205 /* calculate the endpoint of the pipe and check it versus
206 the requested endpoint */
207 if ( ((direction << 7 & USB_ENDPOINT_DIR_MASK) |
208 (number & USB_ENDPOINT_ADDRESS_MASK)) == ep ) {
210 std::cerr << "ep_to_pipeRef: pipeRef for ep address "
211 << ep << " found: " << i << std::endl;
218 std::cerr << "ep_to_pipeRef: No pipeRef found with endpoint address "
222 /* none of the found pipes match the requested endpoint */
227 #endif /* __DARWIN_LIBUSB_H__ */