void XPFIODevice::Initialize () { static bool doneOnce = false; if (doneOnce) return; doneOnce = true; #if __MACH__ mach_port_t iokitPort; IOMasterPort (MACH_PORT_NULL, &iokitPort); io_iterator_t iterator = NULL; IORegistryCreateIterator (iokitPort, kIODeviceTreePlane, kIORegistryIterateRecursively, &iterator); if (!iterator) return; io_object_t entry; while ((entry = IOIteratorNext (iterator)) != NULL) { EvaluateDevice (entry); IOObjectRelease (entry); } IOObjectRelease (iterator); #else RegEntryIter cookie; RegEntryID entry; Boolean done = false; RegEntryIterationOp iterOp = kRegIterDescendants; OSStatus err = RegistryEntryIterateCreate (&cookie); try { while (true) { err = RegistryEntryIterate (&cookie, iterOp, &entry, &done); if (!done && (err == noErr)) { EvaluateDevice (&entry); RegistryEntryIDDispose (&entry); } else { break; } iterOp = kRegIterContinue; } } catch (...) { gLogFile << "Error initializing XPFIODevice" << endl_AC; } RegistryEntryIterateDispose (&cookie); #endif }
io_object_t getInterfaceWithName( mach_port_t masterPort, char *className ) { io_iterator_t ite; io_object_t obj = 0; io_name_t name; kern_return_t rc; kern_return_t kr; kr = IORegistryCreateIterator( masterPort, kIOServicePlane, true, /* recursive */ &ite ); if ( kr != kIOReturnSuccess ) { printf( "IORegistryCreateIterator() error %08lx\n", (unsigned long)kr ); return 0; } while ( (obj = IOIteratorNext( ite )) ) { if ( IOObjectConformsTo( obj, (char*)className ) ) { // printf( "Found Realtek UserClient !!\n" ); break; } else { rc = IOObjectGetClass( obj, name ); if ( rc == kIOReturnSuccess ) { // printf( "Skipping class %s\n", name ); } } IOObjectRelease( obj ); obj = 0; } IORegistryDisposeEnumerator( ite ); return obj; }/* end getInterfaceWithName */
int musb_open(MUSB_INTERFACE **musb_interface, int vendor, int product, int instance, int configuration, int usbinterface) { #if defined(HAVE_LIBUSB) struct usb_bus *bus; struct usb_device *dev; int count = 0; usb_init(); usb_find_busses(); usb_find_devices(); usb_set_debug(3); for (bus = usb_get_busses(); bus; bus = bus->next) for (dev = bus->devices; dev; dev = dev->next) if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) { if (count == instance) { int status; usb_dev_handle *udev; udev = usb_open(dev); if (!udev) { fprintf(stderr, "musb_open: usb_open() error\n"); return MUSB_ACCESS_ERROR; } status = usb_set_configuration(udev, configuration); if (status < 0) { fprintf(stderr, "musb_open: usb_set_configuration() error %d (%s)\n", status, strerror(-status)); fprintf(stderr, "musb_open: Found USB device 0x%04x:0x%04x instance %d, but cannot initialize it: please check permissions on \"/proc/bus/usb/%s/%s\" and \"/dev/bus/usb/%s/%s\"\n", vendor, product, instance, bus->dirname, dev->filename, bus->dirname, dev->filename); return MUSB_ACCESS_ERROR; } /* see if we have write access */ status = usb_claim_interface(udev, usbinterface); if (status < 0) { fprintf(stderr, "musb_open: usb_claim_interface() error %d (%s)\n", status, strerror(-status)); #ifdef _MSC_VER fprintf(stderr, "musb_open: Found USB device 0x%04x:0x%04x instance %d, but cannot initialize it:\nDevice is probably used by another program\n", vendor, product, instance); #else fprintf(stderr, "musb_open: Found USB device 0x%04x:0x%04x instance %d, but cannot initialize it: please check permissions on \"/proc/bus/usb/%s/%s\"\n", vendor, product, instance, bus->dirname, dev->filename); #endif return MUSB_ACCESS_ERROR; } *musb_interface = (MUSB_INTERFACE*)calloc(1, sizeof(MUSB_INTERFACE)); (*musb_interface)->dev = udev; (*musb_interface)->usb_configuration = configuration; (*musb_interface)->usb_interface = usbinterface; return MUSB_SUCCESS; } count++; } return MUSB_NOT_FOUND; #elif defined(HAVE_LIBUSB10) static int first_call = 1; libusb_device **dev_list; libusb_device_handle *dev; struct libusb_device_descriptor desc; int status, i, n; int count = 0; if (first_call) { first_call = 0; libusb_init(NULL); // libusb_set_debug(NULL, 3); } n = libusb_get_device_list(NULL, &dev_list); for (i=0 ; i<n ; i++) { status = libusb_get_device_descriptor(dev_list[i], &desc); if (desc.idVendor == vendor && desc.idProduct == product) { if (count == instance) { status = libusb_open(dev_list[i], &dev); if (status < 0) { fprintf(stderr, "musb_open: libusb_open() error %d\n", status); return MUSB_ACCESS_ERROR; } status = libusb_set_configuration(dev, configuration); if (status < 0) { fprintf(stderr, "musb_open: usb_set_configuration() error %d\n", status); fprintf(stderr, "musb_open: Found USB device 0x%04x:0x%04x instance %d, but cannot initialize it: please check permissions on \"/proc/bus/usb/%d/%d\" and \"/dev/bus/usb/%d/%d\"\n", vendor, product, instance, libusb_get_bus_number(dev_list[i]), libusb_get_device_address(dev_list[i]), libusb_get_bus_number(dev_list[i]), libusb_get_device_address(dev_list[i])); return MUSB_ACCESS_ERROR; } /* see if we have write access */ status = libusb_claim_interface(dev, usbinterface); if (status < 0) { fprintf(stderr, "musb_open: libusb_claim_interface() error %d\n", status); #ifdef _MSC_VER fprintf(stderr, "musb_open: Found USB device 0x%04x:0x%04x instance %d, but cannot initialize it:\nDevice is probably used by another program\n", vendor, product, instance); #else fprintf(stderr, "musb_open: Found USB device 0x%04x:0x%04x instance %d, but cannot initialize it: please check permissions on \"/proc/bus/usb/%d/%d\"\n", vendor, product, instance, libusb_get_bus_number(dev_list[i]), libusb_get_device_address(dev_list[i])); #endif return MUSB_ACCESS_ERROR; } *musb_interface = (MUSB_INTERFACE*)calloc(1, sizeof(MUSB_INTERFACE)); (*musb_interface)->dev = dev; (*musb_interface)->usb_configuration = configuration; (*musb_interface)->usb_interface = usbinterface; return MUSB_SUCCESS; } count++; } } libusb_free_device_list(dev_list, 1); return MUSB_NOT_FOUND; #elif defined(OS_DARWIN) kern_return_t status; io_iterator_t iter; io_service_t service; IOCFPlugInInterface **plugin; SInt32 score; IOUSBDeviceInterface **device; UInt16 xvendor, xproduct; int count = 0; *musb_interface = calloc(1, sizeof(MUSB_INTERFACE)); status = IORegistryCreateIterator(kIOMasterPortDefault, kIOUSBPlane, kIORegistryIterateRecursively, &iter); assert(status == kIOReturnSuccess); while ((service = IOIteratorNext(iter))) { status = IOCreatePlugInInterfaceForService(service, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score); assert(status == kIOReturnSuccess); status = IOObjectRelease(service); assert(status == kIOReturnSuccess); status = (*plugin)->QueryInterface(plugin, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (void *) &device); assert(status == kIOReturnSuccess); status = (*plugin)->Release(plugin); status = (*device)->GetDeviceVendor(device, &xvendor); assert(status == kIOReturnSuccess); status = (*device)->GetDeviceProduct(device, &xproduct); assert(status == kIOReturnSuccess); //fprintf(stderr, "musb_open: Found USB device: vendor 0x%04x, product 0x%04x\n", xvendor, xproduct); if (xvendor == vendor && xproduct == product) { if (count == instance) { fprintf(stderr, "musb_open: Found USB device: vendor 0x%04x, product 0x%04x, instance %d\n", xvendor, xproduct, instance); status = (*device)->USBDeviceOpen(device); fprintf(stderr, "musb_open: USBDeviceOpen status 0x%x\n", status); assert(status == kIOReturnSuccess); (*musb_interface)->usb_configuration = configuration; (*musb_interface)->usb_interface = usbinterface; (*musb_interface)->device = (void*)device; (*musb_interface)->interface = NULL; status = darwin_configure_device(*musb_interface); if (status == kIOReturnSuccess) return MUSB_SUCCESS; fprintf(stderr, "musb_open: USB device exists, but configuration fails!"); return MUSB_NOT_FOUND; } count++; } (*device)->Release(device); } return MUSB_NOT_FOUND; #elif defined(_MSC_VER) GUID guid; HDEVINFO hDevInfoList; SP_DEVICE_INTERFACE_DATA deviceInfoData; PSP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData; ULONG predictedLength, requiredLength; int status; char device_name[256], str[256]; *musb_interface = (MUSB_INTERFACE *)calloc(1, sizeof(MUSB_INTERFACE)); guid = GUID_CLASS_MSCB_BULK; // Retrieve device list for GUID that has been specified. hDevInfoList = SetupDiGetClassDevs(&guid, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); status = FALSE; if (hDevInfoList != NULL) { // Clear data structure memset(&deviceInfoData, 0, sizeof(deviceInfoData)); deviceInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); // retrieves a context structure for a device interface of a device information set. if (SetupDiEnumDeviceInterfaces(hDevInfoList, 0, &guid, instance, &deviceInfoData)) { // Must get the detailed information in two steps // First get the length of the detailed information and allocate the buffer // retrieves detailed information about a specified device interface. functionClassDeviceData = NULL; predictedLength = requiredLength = 0; SetupDiGetDeviceInterfaceDetail(hDevInfoList, &deviceInfoData, NULL, // Not yet allocated 0, // Set output buffer length to zero &requiredLength, // Find out memory requirement NULL); predictedLength = requiredLength; functionClassDeviceData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(predictedLength); functionClassDeviceData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); // Second, get the detailed information if (SetupDiGetDeviceInterfaceDetail(hDevInfoList, &deviceInfoData, functionClassDeviceData, predictedLength, &requiredLength, NULL)) { // Save the device name for subsequent pipe open calls strcpy(device_name, functionClassDeviceData->DevicePath); free(functionClassDeviceData); // Signal device found status = TRUE; } else free(functionClassDeviceData); } } // SetupDiDestroyDeviceInfoList() destroys a device information set // and frees all associated memory. SetupDiDestroyDeviceInfoList(hDevInfoList); if (status) { // Get the read handle sprintf(str, "%s\\PIPE00", device_name); (*musb_interface)->rhandle = CreateFile(str, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if ((*musb_interface)->rhandle == INVALID_HANDLE_VALUE) return MUSB_ACCESS_ERROR; // Get the write handle sprintf(str, "%s\\PIPE01", device_name); (*musb_interface)->whandle = CreateFile(str, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if ((*musb_interface)->whandle == INVALID_HANDLE_VALUE) return MUSB_ACCESS_ERROR; return MUSB_SUCCESS; } return MUSB_NOT_FOUND; #endif }