📄 darwin.c
字号:
bytes + retrieved, (UInt32 *)&ret_size, 0, timeout); if (result != kIOReturnSuccess) USB_ERROR_STR(-darwin_to_errno(result), "usb_bulk_read(ReadPipeTO): error reading from bulk endpoint %02x: %s", ep, darwin_error_str(result)); retrieved = ret_size; }#endif /* Will this ever be true? -N */ if (retrieved < 0) USB_ERROR_STR(retrieved, "usb_bulk_read: error reading from endpoint %02x", ep); return retrieved;}/* interrupt endpoints seem to be treated just like any other endpoint under OSX/Darwin */int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout){ return usb_bulk_write (dev, ep, bytes, size, timeout);}int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout){ return usb_bulk_read (dev, ep, bytes, size, timeout);}int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout){ struct darwin_dev_handle *device = dev->impl_info;#if !defined (LIBUSB_NO_TIMEOUT_DEVICE) IOUSBDevRequestTO urequest;#else IOUSBDevRequest urequest;#endif io_return_t result; if (usb_debug >= 3) fprintf(stderr, "usb_control_msg: %d %d %d %d %p %d %d\n", requesttype, request, value, index, bytes, size, timeout); bzero(&urequest, sizeof(IOUSBDevRequestTO)); urequest.bmRequestType = requesttype; urequest.bRequest = request; urequest.wValue = value; urequest.wIndex = index; urequest.wLength = size; urequest.pData = bytes;#if !defined (LIBUSB_NO_TIMEOUT_DEVICE) urequest.completionTimeout = timeout; result = (*(device->device))->DeviceRequestTO(device->device, &urequest);#else result = (*(device->device))->DeviceRequest(device->device, &urequest);#endif if (result != kIOReturnSuccess) USB_ERROR_STR(-darwin_to_errno(result), "usb_control_msg(DeviceRequestTO): %s", darwin_error_str(result)); /* Bytes transfered is stored in the wLenDone field*/ return urequest.wLenDone;}int usb_os_find_busses(struct usb_bus **busses){ struct usb_bus *fbus = NULL; io_iterator_t deviceIterator; io_service_t usbDevice; usb_device_t **device; IOCFPlugInInterface **plugInInterface = NULL; io_return_t result; SInt32 score; UInt16 address; UInt32 location; char buf[20]; int i = 1; /* Create a master port for communication with IOKit (this should have been created if the user called usb_init() )*/ if (!masterPort) { usb_init (); if (result || !masterPort) USB_ERROR(-ENOENT); } /* set up the matching dictionary for class IOUSBRootHubDevice and its subclasses. It will be consumed by the next call */ if ((matchingDict = IOServiceMatching("IOUSBRootHubDevice")) == NULL) { darwin_cleanup (); USB_ERROR(-ENOMEM); } result = IOServiceGetMatchingServices(masterPort, matchingDict, &deviceIterator); matchingDict = NULL; if (result != kIOReturnSuccess) USB_ERROR_STR(-darwin_to_errno (result), "usb_os_find_busses(IOServiceGetMatchingServices): %s", darwin_error_str(result)); while (IOIteratorIsValid(deviceIterator) && (usbDevice = IOIteratorNext(deviceIterator))) { struct usb_bus *bus; /* Create an intermediate plug-in */ result = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score); result = IOObjectRelease(usbDevice); if (result || !plugInInterface) continue; (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(DeviceInterfaceID), (LPVOID)&device); /* done with this */ (*plugInInterface)->Stop(plugInInterface); IODestroyPlugInInterface (plugInInterface); plugInInterface = NULL; if (!device) continue; result = (*(device))->GetLocationID(device, &location); bus = malloc(sizeof(*bus)); if (!bus) USB_ERROR(-ENOMEM); memset((void *)bus, 0, sizeof(*bus)); sprintf(buf, "%03i", i++); bus->location = location; strncpy(bus->dirname, buf, sizeof(bus->dirname) - 1); bus->dirname[sizeof(bus->dirname) - 1] = 0; LIST_ADD(fbus, bus); if (usb_debug >= 2) fprintf(stderr, "usb_os_find_busses: Found %s\n", bus->dirname); (*(device))->Release(device); } IOObjectRelease(deviceIterator); *busses = fbus; return 0;}int usb_os_find_devices(struct usb_bus *bus, struct usb_device **devices){ struct usb_device *fdev = NULL; io_iterator_t deviceIterator; io_service_t usbDevice; usb_device_t **device; IOCFPlugInInterface **plugInInterface = NULL; io_return_t result; SInt32 score; UInt16 address; UInt32 location; UInt32 bus_loc = bus->location; /* for use in retrieving device description */ IOUSBDevRequest req; char buf[20]; int busnum = 0; /* a master port should have been created by usb_os_init */ if (!masterPort) USB_ERROR(-ENOENT); /* set up the matching dictionary for class IOUSBDevice and its subclasses. It will be consumed by the next call */ if ((matchingDict = IOServiceMatching(kIOUSBDeviceClassName)) == NULL) { darwin_cleanup (); USB_ERROR(-ENOMEM); } result = IOServiceGetMatchingServices(masterPort, matchingDict, &deviceIterator); matchingDict = NULL; if (result != kIOReturnSuccess) USB_ERROR_STR(-darwin_to_errno (result), "usb_os_find_devices(IOServiceGetMatchingServices): %s", darwin_error_str(result)); req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); req.bRequest = kUSBRqGetDescriptor; req.wValue = kUSBDeviceDesc << 8; req.wIndex = 0; req.wLength = sizeof(IOUSBDeviceDescriptor); while (IOIteratorIsValid (deviceIterator) && (usbDevice = IOIteratorNext(deviceIterator))) { /* Create an intermediate plug-in */ result = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score); result = IOObjectRelease(usbDevice); if (result || !plugInInterface) continue; (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(DeviceInterfaceID), (LPVOID)&device); /* done with this */ (*plugInInterface)->Stop(plugInInterface); IODestroyPlugInInterface (plugInInterface); plugInInterface = NULL; if (!device) continue; result = (*(device))->GetDeviceAddress(device, (USBDeviceAddress *)&address); result = (*(device))->GetLocationID(device, &location); if (usb_debug >= 2) fprintf(stderr, "usb_os_find_devices: Found USB device at location 0x%08x\n", location); /* first byte of location appears to be associated with the device's bus */ if (location >> 24 == bus_loc >> 24) { struct usb_device *dev; dev = malloc(sizeof(*dev)); if (!dev) { USB_ERROR(-ENOMEM); } memset((void *)dev, 0, sizeof(*dev)); dev->bus = bus; req.pData = &(dev->descriptor); result = (*(device))->DeviceRequest(device, &req); USB_LE16_TO_CPU(dev->descriptor.bcdUSB); USB_LE16_TO_CPU(dev->descriptor.idVendor); USB_LE16_TO_CPU(dev->descriptor.idProduct); USB_LE16_TO_CPU(dev->descriptor.bcdDevice); sprintf(dev->filename, "%03i-%04x-%04x-%02x-%02x", address, dev->descriptor.idVendor, dev->descriptor.idProduct, dev->descriptor.bDeviceClass, dev->descriptor.bDeviceSubClass); dev->dev = (USBDeviceAddress *)malloc(4); memcpy(dev->dev, &location, 4); LIST_ADD(fdev, dev); if (usb_debug >= 2) fprintf(stderr, "usb_os_find_devices: Found %s on %s at location 0x%08x\n", dev->filename, bus->dirname, location); } /* release the device now */ (*(device))->Release(device); } IOObjectRelease(deviceIterator); *devices = fdev; return 0;}void usb_os_init(void){ if (masterPort == NULL) { IOMasterPort(MACH_PORT_NULL, &masterPort); gNotifyPort = IONotificationPortCreate(masterPort); }}void usb_os_cleanup (void){ if (masterPort != NULL) { mach_port_deallocate(mach_task_self(), masterPort); masterPort = NULL; }}int usb_resetep(usb_dev_handle *dev, unsigned int ep){ struct darwin_dev_handle *device; io_return_t result = -1; int pipeRef; if (!dev) USB_ERROR(-ENXIO); if ((device = dev->impl_info) == NULL) USB_ERROR(-ENOENT); /* interface is not open */ if (!device->interface) USB_ERROR_STR(-EACCES, "usb_resetep: interface used without being claimed"); if ((pipeRef = ep_to_pipeRef(device, ep)) == -1) USB_ERROR(-EINVAL); result = (*(device->interface))->ResetPipe(device->interface, pipeRef); if (result != kIOReturnSuccess) USB_ERROR_STR(-darwin_to_errno(result), "usb_resetep(ResetPipe): %s", darwin_error_str(result)); return 0;}int usb_clear_halt(usb_dev_handle *dev, unsigned int ep){ struct darwin_dev_handle *device; io_return_t result = -1; int pipeRef; if (!dev) USB_ERROR(-ENXIO); if ((device = dev->impl_info) == NULL) USB_ERROR(-ENOENT); /* interface is not open */ if (!device->interface) USB_ERROR_STR(-EACCES, "usb_clear_halt: interface used without being claimed"); if ((pipeRef = ep_to_pipeRef(device, ep)) == -1) USB_ERROR(-EINVAL); result = (*(device->interface))->ClearPipeStall(device->interface, pipeRef); if (result != kIOReturnSuccess) USB_ERROR_STR(-darwin_to_errno(result), "usb_clear_halt(ClearPipeStall): %s", darwin_error_str(result)); return 0;}int usb_reset(usb_dev_handle *dev){ struct darwin_dev_handle *device; int result; if (!dev) USB_ERROR(-ENXIO); if ((device = dev->impl_info) == NULL) USB_ERROR(-ENOENT); if (!device->device) USB_ERROR_STR(-ENOENT, "usb_reset: no such device"); result = (*(device->device))->ResetDevice(device->device); if (result != kIOReturnSuccess) USB_ERROR_STR(-darwin_to_errno(result), "usb_reset(ResetDevice): %s", darwin_error_str(result)); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -