IOReturn DownloadToAnchorDevice(IOUSBDeviceInterface245 **dev) { int i; UInt8 writeVal; IOReturn kr; // Assert reset writeVal = 1; kr = AnchorWrite(dev, k8051_USBCS, 1, &writeVal); if (kIOReturnSuccess != kr) { printf("AnchorWrite reset returned err 0x%x!\n", kr); (*dev)->USBDeviceClose(dev); (*dev)->Release(dev); return kr; } i = 0; // Download code while (bulktest[i].Type == 0) { kr = AnchorWrite(dev, bulktest[i].Address, bulktest[i].Length, bulktest[i].Data); if (kIOReturnSuccess != kr) { printf("AnchorWrite download %i returned err 0x%x!\n", i, kr); (*dev)->USBDeviceClose(dev); (*dev)->Release(dev); return kr; } i++; } // De-assert reset writeVal = 0; kr = AnchorWrite(dev, k8051_USBCS, 1, &writeVal); if (kIOReturnSuccess != kr) { printf("AnchorWrite run returned err 0x%x!\n", kr); } return kr; }
// // start // when this method is called, I have been selected as the driver for this device. // I can still return false to allow a different driver to load // bool com_apple_AnchorUSB::start(IOService *provider) { IOReturn err; UInt8 writeVal; int i = 0; const IOUSBConfigurationDescriptor *cd; // Do all the work here, on an IOKit matching thread. IOLog("%s(%p)::start!\n", getName(), this); fDevice = OSDynamicCast(IOUSBDevice, provider); if(!fDevice) { IOLog("%s(%p)::start - Provider isn't a USB device!!!\n", getName(), this); return false; } // Find the first config/interface if (fDevice->GetNumConfigurations() < 1) { IOLog("%s(%p)::start - no composite configurations\n", getName(), this); return false; } cd = fDevice->GetFullConfigurationDescriptor(0); // set the configuration to the first config if (!cd) { IOLog("%s(%p)::start - no config descriptor\n", getName(), this); return false; } if (!fDevice->open(this)) { IOLog("%s(%p)::start - unable to open device for configuration\n", getName(), this); return false; } err = fDevice->SetConfiguration(this, cd->bConfigurationValue, true); if (err) { IOLog("%s(%p)::start - unable to set the configuration\n", getName(), this); fDevice->close(this); return false; } // Assert reset writeVal = 1; err = AnchorWrite(k8051_USBCS, 1, &writeVal); if(kIOReturnSuccess != err) { IOLog("%s(%p)::start - AnchorWrite reset returned err 0x%x!\n", getName(), this, err); fDevice->close(this); return false; } // Download code while(demotonehex[i].Type == 0) { err = AnchorWrite(demotonehex[i].Address, demotonehex[i].Length, demotonehex[i].Data); if(kIOReturnSuccess != err) { IOLog("%s(%p)::start - AnchorWrite download %i returned err 0x%x!\n", getName(), this, i, err); fDevice->close(this); return false; } i++; } // De-assert reset writeVal = 0; err = AnchorWrite(k8051_USBCS, 1, &writeVal); if(kIOReturnSuccess != err) { IOLog("%s(%p)::start - AnchorWrite run returned err 0x%x!\n", getName(), this, err); fDevice->close(this); return false; } // leave the device open for now, to maintain exclusive access return true; }