changeset 46:053cc617af54

USBLib: added functions to clear USB endpoint halt state
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 30 Jun 2013 16:28:36 +0200
parents 8df7f4dc5615
children 15ddb1e0e2a5
files USBLib/Communication/IUsbDevice.cs USBLib/Communication/LibUsb0/LibUsbDevice.cs USBLib/Communication/LibUsb1/LibUsb1Device.cs USBLib/Communication/LibUsb1/libusb1.cs USBLib/Communication/USBIO/USBIODevice.cs USBLib/Communication/UsbInterface.cs USBLib/Communication/WinUsb/WinUsbDevice.cs
diffstat 7 files changed, 38 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/USBLib/Communication/IUsbDevice.cs	Wed Jun 12 23:20:21 2013 +0200
+++ b/USBLib/Communication/IUsbDevice.cs	Sun Jun 30 16:28:36 2013 +0200
@@ -21,8 +21,10 @@
 
 		int BulkWrite(Byte endpoint, Byte[] buffer, int offset, int length);
 		int BulkRead(Byte endpoint, Byte[] buffer, int offset, int length);
+		void BulkReset(Byte endpoint);
 		int InterruptWrite(Byte endpoint, Byte[] buffer, int offset, int length);
 		int InterruptRead(Byte endpoint, Byte[] buffer, int offset, int length);
+		void InterruptReset(Byte endpoint);
 		int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index, Byte[] buffer, int offset, int length);
 		int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, Byte[] buffer, int offset, int length);
 
--- a/USBLib/Communication/LibUsb0/LibUsbDevice.cs	Wed Jun 12 23:20:21 2013 +0200
+++ b/USBLib/Communication/LibUsb0/LibUsbDevice.cs	Sun Jun 30 16:28:36 2013 +0200
@@ -87,12 +87,18 @@
 		public override int BulkWrite(byte endpoint, byte[] buffer, int offset, int length) {
 			return PipeTransfer(endpoint, true, false, buffer, offset, length, 0);
 		}
+		public override void BulkReset(byte endpoint) {
+			PipeReset(endpoint);
+		}
 		public override int InterruptRead(byte endpoint, byte[] buffer, int offset, int length) {
 			return PipeTransfer(endpoint, false, false, buffer, offset, length, 0);
 		}
 		public override int InterruptWrite(byte endpoint, byte[] buffer, int offset, int length) {
 			return PipeTransfer(endpoint, true, false, buffer, offset, length, 0);
 		}
+		public override void InterruptReset(byte endpoint) {
+			PipeReset(endpoint);
+		}
 		public unsafe override int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) {
 			if (buffer == null) buffer = new Byte[0];
 			if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer length");
--- a/USBLib/Communication/LibUsb1/LibUsb1Device.cs	Wed Jun 12 23:20:21 2013 +0200
+++ b/USBLib/Communication/LibUsb1/LibUsb1Device.cs	Sun Jun 30 16:28:36 2013 +0200
@@ -23,6 +23,10 @@
 		public override int BulkRead(byte endpoint, byte[] buffer, int offset, int length) {
 			return BulkTransfer(endpoint, buffer, offset, length);
 		}
+		public override void BulkReset(byte endpoint) {
+			int ret = libusb1.libusb_clear_halt(Handle, endpoint);
+			if (ret < 0) throw new Exception("libusb_clear_halt returned " + ret.ToString());
+		}
 		private int BulkTransfer(byte endpoint, byte[] buffer, int offset, int length) {
 			if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer length");
 			if (length == 0) return 0;
@@ -39,6 +43,9 @@
 		public override int InterruptRead(byte endpoint, byte[] buffer, int offset, int length) {
 			return InterruptTransfer(endpoint, buffer, offset, length);
 		}
+		public override void InterruptReset(byte endpoint) {
+			BulkReset(endpoint);
+		}
 		private int InterruptTransfer(byte endpoint, byte[] buffer, int offset, int length) {
 			if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer length");
 			if (length == 0) return 0;
--- a/USBLib/Communication/LibUsb1/libusb1.cs	Wed Jun 12 23:20:21 2013 +0200
+++ b/USBLib/Communication/LibUsb1/libusb1.cs	Sun Jun 30 16:28:36 2013 +0200
@@ -158,7 +158,7 @@
 		[DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)]
 		static extern int libusb_set_interface_alt_setting(libusb_device_handle dev, int interface_number, int alternate_setting);
 		[DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)]
-		static extern int libusb_clear_halt(libusb_device_handle dev, Byte endpoint);
+		public static extern int libusb_clear_halt(libusb_device_handle dev, Byte endpoint);
 		[DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)]
 		public static extern int libusb_reset_device(libusb_device_handle dev);
 
--- a/USBLib/Communication/USBIO/USBIODevice.cs	Wed Jun 12 23:20:21 2013 +0200
+++ b/USBLib/Communication/USBIO/USBIODevice.cs	Sun Jun 30 16:28:36 2013 +0200
@@ -27,6 +27,7 @@
 		static readonly int IOCTL_USBIO_CLASS_OR_VENDOR_OUT_REQUEST = _USBIO_IOCTL_CODE(13, METHOD_IN_DIRECT);
 		static readonly int IOCTL_USBIO_RESET_DEVICE = _USBIO_IOCTL_CODE(21, METHOD_BUFFERED);
 		static readonly int IOCTL_USBIO_BIND_PIPE = _USBIO_IOCTL_CODE(30, METHOD_BUFFERED);
+		static readonly int IOCTL_USBIO_RESET_PIPE = _USBIO_IOCTL_CODE(32, METHOD_BUFFERED);
 
 		[DllImport("kernel32.dll", SetLastError = true)]
 		static unsafe extern bool ReadFile(SafeFileHandle hFile, byte* lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);
@@ -155,12 +156,18 @@
 		public override int BulkWrite(byte endpoint, byte[] buffer, int offset, int length) {
 			return PipeWrite(endpoint, buffer, offset, length);
 		}
+		public override void BulkReset(byte endpoint) {
+			PipeReset(endpoint);
+		}
 		public override int InterruptRead(byte endpoint, byte[] buffer, int offset, int length) {
 			return PipeRead(endpoint, buffer, offset, length);
 		}
 		public override int InterruptWrite(byte endpoint, byte[] buffer, int offset, int length) {
 			return PipeWrite(endpoint, buffer, offset, length);
 		}
+		public override void InterruptReset(byte endpoint) {
+			PipeReset(endpoint);
+		}
 		public unsafe override int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) {
 			return ControlTransfer(requestType, request, value, index, buffer, offset, length);
 		}
@@ -257,7 +264,8 @@
 			return (int)ret;
 		}
 		public void PipeReset(byte pipeID) {
-			throw new NotImplementedException();
+			SafeFileHandle handle = GetHandleForPipe(pipeID);
+			DeviceIoControl(handle, IOCTL_USBIO_RESET_PIPE, IntPtr.Zero, 0, IntPtr.Zero, 0);
 		}
 
 		private unsafe int DeviceIoControl(SafeHandle hDevice, int IoControlCode, IntPtr InBuffer, int nInBufferSize, IntPtr OutBuffer, int nOutBufferSize) {
--- a/USBLib/Communication/UsbInterface.cs	Wed Jun 12 23:20:21 2013 +0200
+++ b/USBLib/Communication/UsbInterface.cs	Sun Jun 30 16:28:36 2013 +0200
@@ -38,8 +38,10 @@
 
 		public abstract int BulkWrite(Byte endpoint, Byte[] buffer, int offset, int length);
 		public abstract int BulkRead(Byte endpoint, Byte[] buffer, int offset, int length);
+		public virtual void BulkReset(Byte endpoint) { throw new NotImplementedException(); }
 		public abstract int InterruptWrite(Byte endpoint, Byte[] buffer, int offset, int length);
 		public abstract int InterruptRead(Byte endpoint, Byte[] buffer, int offset, int length);
+		public virtual void InterruptReset(Byte endpoint) { throw new NotImplementedException(); }
 		public abstract int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length);
 		public abstract int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length);
 
--- a/USBLib/Communication/WinUsb/WinUsbDevice.cs	Wed Jun 12 23:20:21 2013 +0200
+++ b/USBLib/Communication/WinUsb/WinUsbDevice.cs	Sun Jun 30 16:28:36 2013 +0200
@@ -200,17 +200,28 @@
 			return length;
 		}
 
+		public void PipeReset(byte endpoint) {
+			SafeWinUsbInterfaceHandle ih = GetInterfaceHandleForEndpoint(endpoint);
+			WinUsb_ResetPipe(ih, endpoint);
+		}
+
 		public override int BulkWrite(Byte endpoint, Byte[] buffer, int offset, int length) {
 			return PipeWrite(endpoint, buffer, offset, length);
 		}
 		public override int BulkRead(Byte endpoint, Byte[] buffer, int offset, int length) {
 			return PipeRead(endpoint, buffer, offset, length);
 		}
+		public override void BulkReset(Byte endpoint) {
+			PipeReset(endpoint);
+		}
 		public override int InterruptWrite(Byte endpoint, Byte[] buffer, int offset, int length) {
 			return PipeWrite(endpoint, buffer, offset, length);
 		}
 		public override int InterruptRead(Byte endpoint, Byte[] buffer, int offset, int length) {
 			return PipeRead(endpoint, buffer, offset, length);
 		}
+		public override void InterruptReset(Byte endpoint) {
+			PipeReset(endpoint);
+		}
 	}
 }
\ No newline at end of file