bool EMUUSBMIDIDevice::MatchDevice( USBDevice * inUSBDevice) { const IOUSBDeviceDescriptor * devDesc = inUSBDevice->GetDeviceDescriptor(); debugIOLog("starting to match"); if (USBToHostWord(devDesc->idVendor) == kMyVendorID) { UInt16 devProduct = USBToHostWord(devDesc->idProduct); if (devProduct == kMyProductID) { debugIOLog("Found it"); return true; } } return false; }
void AppleUSBUHCI::RHDumpHubPortStatus(IOUSBHubPortStatus *status) { UInt16 value; char buf[128]; static struct { UInt16 mask; const char *string; } strings[] = { {kHubPortConnection, "kHubPortConnection "}, {kHubPortEnabled, "kHubPortEnabled "}, {kHubPortSuspend, "kHubPortSuspend "}, {kHubPortOverCurrent,"kHubPortOverCurrent "}, {kHubPortBeingReset, "kHubPortBeingReset "}, {kHubPortPower, "kHubPortPower "}, {kHubPortLowSpeed, "kHubPortLowSpeed "}, {kHubPortHighSpeed, "kHubPortHighSpeed "}, {kHubPortTestMode, "kHubPortTestMode "}, {kHubPortIndicator, "kHubPortIndicator "}, {0,0} }; int i; buf[0] = '\0'; value = USBToHostWord(status->statusFlags); for (i=0; strings[i].string != 0; i++) { if ((value & strings[i].mask) != 0) { strlcat(buf, strings[i].string, sizeof(buf)); } } USBLog(5, "AppleUSBUHCI[%p]: Hub port status: %s", this, buf); buf[0] = '\0'; value = USBToHostWord(status->changeFlags); for (i=0; strings[i].string != 0; i++) { if ((value & strings[i].mask) != 0) { strlcat(buf, strings[i].string, sizeof(buf)); } } USBLog(5, "AppleUSBUHCI[%p]: Hub port change: %s", this, buf); }
static int get_string_desc(IOUSBDeviceInterface197 **devIf, uint8_t index, UniChar buf[128]) { IOUSBDevRequest req; langid_arr lid; IOReturn ior; int i; /* start by opening the device */ ior = (*devIf)->USBDeviceOpenSeize(devIf); if(ior != kIOReturnSuccess) { uvcc_err("get_string_desc: USBDeviceOpenSeize", ior); return -1; } req.bmRequestType = USB_RT_TD_GET | USB_RT_TY_STD | USB_RT_RE_DEVICE; req.bRequest = USB_REQ_GET_DESC; /* first find out if we got any string descs (by getting langids) */ req.wValue = (USB_DT_STRING << 8) | 0; req.wIndex = 0; req.pData = &lid; req.wLength = sizeof(lid); ior = (*devIf)->DeviceRequest(devIf, &req); if(ior != kIOReturnSuccess && ior != kIOReturnOverrun) { /* apperantly overrun is normal for string descs */ uvcc_err("get_string_desc: DeviceRequest", ior); return -1; } /* here we could check for a preferred lang-id arg.. we could. */ req.wValue = (USB_DT_STRING << 8) | index; req.wIndex = lid.bString[0]; /* lets just reuse lid */ req.pData = &lid; req.wLength = sizeof(lid.bString); /* should this be done twice (second time with the given length)? */ ior = (*devIf)->DeviceRequest(devIf, &req); /* close only returns error if connection is no longer valid */ (*devIf)->USBDeviceClose(devIf); if(ior != kIOReturnSuccess && ior != kIOReturnOverrun) { /* apperantly overrun is normal for string descs */ uvcc_err("get_string_desc: DeviceRequest", ior); return -1; } for(i = 0; i < (lid.bLength-2)/2; i++) buf[i] = USBToHostWord(lid.bString[i]); /* returned strings are not null terminated */ buf[i] = 0; return (lid.bLength-2)/2; }
OSStatus USBMIDIDeviceManager::UseDeviceAndInterface(USBDevice * usbDevice, USBInterface * usbInterface) { // Match the device that was just located with what is in the current state MIDIDeviceRef midiDevice = NULL; IOUSBDeviceInterface **devIntf = usbDevice->GetPluginInterface(); const IOUSBDeviceDescriptor *devDesc = usbDevice->GetDeviceDescriptor(); bool deviceInSetup = false; UInt32 vendorProduct = ((UInt32)USBToHostWord(devDesc->idVendor) << 16) | USBToHostWord(devDesc->idProduct); CFStringRef serialNumber = usbDevice->GetString(devDesc->iSerialNumber); OSStatus err; UInt32 locationID; require_noerr(err = (*devIntf)->GetLocationID(devIntf, &locationID), errexit); { // See if it's already in the setup MIDIDeviceListRef curDevices = MIDIGetDriverDeviceList(mDriver->Self()); int nDevices = MIDIDeviceListGetNumberOfDevices(curDevices), firstPass, lastPass; if (serialNumber == NULL) { firstPass = 2; lastPass = 3; } else { firstPass = 1; lastPass = 1; } for (int pass = firstPass; pass <= lastPass && !deviceInSetup; ++pass) { // pass 1: match by serial number if present (skipped if not) // pass 2: match by locationID // pass 3: match by order found for (int iDevice = 0; iDevice < nDevices; ++iDevice) { SInt32 prevLocation, prevVendorProduct, isOffline; midiDevice = MIDIDeviceListGetDevice(curDevices, iDevice); err = MIDIObjectGetIntegerProperty(midiDevice, kUSBVendorProductProperty, &prevVendorProduct); if (!err && UInt32(prevVendorProduct) == vendorProduct) { switch (pass) { case 1: { CFStringRef prevSerial; err = MIDIObjectGetStringProperty(midiDevice, kSerialNumberProperty, &prevSerial); if (!err) { if (CFEqual(prevSerial, serialNumber)) deviceInSetup = true; CFRelease(prevSerial); } } break; case 2: err = MIDIObjectGetIntegerProperty(midiDevice, kUSBLocationProperty, &prevLocation); if (!err && UInt32(prevLocation) == locationID) deviceInSetup = true; break; case 3: err = MIDIObjectGetIntegerProperty(midiDevice, kMIDIPropertyOffline, &isOffline); if (!err && isOffline) deviceInSetup = true; break; } } if (deviceInSetup) break; } } MIDIDeviceListDispose(curDevices); } if (!deviceInSetup) { #if VERBOSE printf("creating new device\n"); #endif midiDevice = mDriver->CreateDevice(usbDevice, usbInterface); require_noerr(err = MIDISetupAddDevice(midiDevice), errexit); } else { #if VERBOSE printf("old device found\n"); #endif mDriver->PreExistingDeviceFound(midiDevice, usbDevice, usbInterface); } // set device properties unconditionally MIDIObjectSetIntegerProperty(midiDevice, kUSBVendorProductProperty, vendorProduct); MIDIObjectSetIntegerProperty(midiDevice, kUSBLocationProperty, locationID); if (serialNumber != NULL) MIDIObjectSetStringProperty(midiDevice, kSerialNumberProperty, serialNumber); // Create a USBMIDIDevice (or subclass), starting it for I/O { USBMIDIDevice *ioDev = mDriver->CreateUSBMIDIDevice(usbDevice, usbInterface, midiDevice); if (ioDev == NULL) goto errexit; if (!ioDev->Initialize()) delete ioDev; else { if (mUSBMIDIDeviceList.size() == 0) mUSBMIDIDeviceList.reserve(4); mUSBMIDIDeviceList.push_back(ioDev); MIDIObjectSetIntegerProperty(midiDevice, kMIDIPropertyOffline, false); } } errexit: if (serialNumber != NULL) CFRelease(serialNumber); return err; }