#define ITRANSFER_CTX(transfer) \
(TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
+#define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
+#define IS_EPOUT(ep) (!IS_EPIN(ep))
+#define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
+#define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
+
/* Internal abstractions for thread synchronization and poll */
#if defined(THREADS_POSIX)
#include <os/threads_posix.h>
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)transfer->dev_handle->os_priv;
IOReturn ret;
- uint8_t is_read; /* 0 = we're reading, 1 = we're writing */
uint8_t transferType;
/* None of the values below are used in libusb for bulk transfers */
uint8_t direction, number, interval, pipeRef, iface;
struct darwin_interface *cInterface;
- /* are we reading or writing? */
- is_read = transfer->endpoint & LIBUSB_ENDPOINT_IN;
-
- if (!is_read && transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
+ if (IS_XFEROUT(transfer) && transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
return LIBUSB_ERROR_NOT_SUPPORTED;
if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, &iface) != 0) {
/* submit the request */
/* timeouts are unavailable on interrupt endpoints */
if (transferType == kUSBInterrupt) {
- if (is_read)
+ if (IS_XFERIN(transfer))
ret = (*(cInterface->interface))->ReadPipeAsync(cInterface->interface, pipeRef, transfer->buffer,
transfer->length, darwin_async_io_callback, itransfer);
else
} else {
itransfer->flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
- if (is_read)
+ if (IS_XFERIN(transfer))
ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
transfer->length, transfer->timeout, transfer->timeout,
darwin_async_io_callback, (void *)itransfer);
}
if (ret)
- usbi_err (TRANSFER_CTX (transfer), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", is_read ? "In" : "Out",
+ usbi_err (TRANSFER_CTX (transfer), "bulk transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out",
darwin_error_str(ret), ret);
return darwin_to_libusb (ret);
struct darwin_device_handle_priv *priv = (struct darwin_device_handle_priv *)transfer->dev_handle->os_priv;
IOReturn kresult;
- uint8_t is_read; /* 0 = we're writing, 1 = we're reading */
uint8_t pipeRef, iface;
UInt64 frame;
AbsoluteTime atTime;
struct darwin_interface *cInterface;
- /* are we reading or writing? */
- is_read = transfer->endpoint & LIBUSB_ENDPOINT_IN;
-
/* construct an array of IOUSBIsocFrames, reuse the old one if possible */
if (tpriv->isoc_framelist && tpriv->num_iso_packets != transfer->num_iso_packets) {
free(tpriv->isoc_framelist);
frame = cInterface->frames[transfer->endpoint];
/* submit the request */
- if (is_read)
+ if (IS_XFERIN(transfer))
kresult = (*(cInterface->interface))->ReadIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame,
transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback,
itransfer);
cInterface->frames[transfer->endpoint] = frame + transfer->num_iso_packets / 8;
if (kresult != kIOReturnSuccess) {
- usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", is_read ? "In" : "Out",
+ usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", IS_XFERIN(transfer) ? "In" : "Out",
darwin_error_str(kresult));
free (tpriv->isoc_framelist);
tpriv->isoc_framelist = NULL;
err = _sync_control_transfer(itransfer);
break;
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
- if (0 == transfer->endpoint & LIBUSB_ENDPOINT_IN) {
+ if (IS_XFEROUT(transfer)) {
/* Isochronous write is not supported */
err = LIBUSB_ERROR_NOT_SUPPORTED;
break;
break;
case LIBUSB_TRANSFER_TYPE_BULK:
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
- if (0 == transfer->endpoint & LIBUSB_ENDPOINT_IN &&
+ if (IS_XFEROUT(transfer) &&
transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
err = LIBUSB_ERROR_NOT_SUPPORTED;
break;
dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
endpt = UE_GET_ADDR(transfer->endpoint);
- mode = (transfer->endpoint & LIBUSB_ENDPOINT_IN) ? O_RDONLY : O_WRONLY;
+ mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
usbi_dbg("endpoint %d mode %d", endpt, mode);
if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
return _errno_to_libusb(errno);
- if (transfer->endpoint & LIBUSB_ENDPOINT_IN) {
+ if (IS_XFERIN(transfer)) {
if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
return _errno_to_libusb(errno);
}
usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd,
- (short)((transfer->endpoint & LIBUSB_ENDPOINT_IN)?POLLIN:POLLOUT));
+ (short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
#if !defined(DYNAMIC_FDS)
usbi_fd_notification(ctx);
#endif
}
usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd,
- (short)((transfer->endpoint & LIBUSB_ENDPOINT_IN)?POLLIN:POLLOUT));
+ (short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
#if !defined(DYNAMIC_FDS)
usbi_fd_notification(ctx);
#endif
return submit_control_transfer(itransfer);
case LIBUSB_TRANSFER_TYPE_BULK:
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
- if (0 == transfer->endpoint & LIBUSB_ENDPOINT_IN &&
+ if (IS_XFEROUT(transfer) &&
transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
return LIBUSB_ERROR_NOT_SUPPORTED;
return submit_bulk_transfer(itransfer);
struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
HANDLE winusb_handle;
- bool direction_in, ret;
+ bool ret;
int current_interface;
struct winfd wfd;
usbi_dbg("matched endpoint %02X with interface %d", transfer->endpoint, current_interface);
winusb_handle = handle_priv->interface_handle[current_interface].api_handle;
- direction_in = transfer->endpoint & LIBUSB_ENDPOINT_IN;
- wfd = usbi_create_fd(winusb_handle, direction_in?_O_RDONLY:_O_WRONLY);
+ wfd = usbi_create_fd(winusb_handle, IS_XFERIN(transfer) ? _O_RDONLY : _O_WRONLY);
// Always use the handle returned from usbi_create_fd (wfd.handle)
if (wfd.fd < 0) {
return LIBUSB_ERROR_NO_MEM;
}
- if (direction_in) {
+ if (IS_XFERIN(transfer)) {
usbi_dbg("reading %d bytes", transfer->length);
ret = WinUsb_ReadPipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
} else {
priv->usb_interface[i].endpoint[j], windows_error_str(0));
}
// FlushPipe seems to fail on OUT pipes
- if ( (priv->usb_interface[i].endpoint[j] & LIBUSB_ENDPOINT_IN)
+ if (IS_EPIN(priv->usb_interface[i].endpoint[j])
&& (!WinUsb_FlushPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) ) {
usbi_err(ctx, "WinUsb_FlushPipe (pipe address %02X) failed: %s",
priv->usb_interface[i].endpoint[j], windows_error_str(0));