comparison USBLib/Communication/USBIO/USBIODevice.cs @ 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 5a2b51b0d71b
children abe0d55a2201
comparison
equal deleted inserted replaced
45:8df7f4dc5615 46:053cc617af54
25 static readonly int IOCTL_USBIO_UNCONFIGURE_DEVICE = _USBIO_IOCTL_CODE(10, METHOD_BUFFERED); 25 static readonly int IOCTL_USBIO_UNCONFIGURE_DEVICE = _USBIO_IOCTL_CODE(10, METHOD_BUFFERED);
26 static readonly int IOCTL_USBIO_CLASS_OR_VENDOR_IN_REQUEST = _USBIO_IOCTL_CODE(12, METHOD_OUT_DIRECT); 26 static readonly int IOCTL_USBIO_CLASS_OR_VENDOR_IN_REQUEST = _USBIO_IOCTL_CODE(12, METHOD_OUT_DIRECT);
27 static readonly int IOCTL_USBIO_CLASS_OR_VENDOR_OUT_REQUEST = _USBIO_IOCTL_CODE(13, METHOD_IN_DIRECT); 27 static readonly int IOCTL_USBIO_CLASS_OR_VENDOR_OUT_REQUEST = _USBIO_IOCTL_CODE(13, METHOD_IN_DIRECT);
28 static readonly int IOCTL_USBIO_RESET_DEVICE = _USBIO_IOCTL_CODE(21, METHOD_BUFFERED); 28 static readonly int IOCTL_USBIO_RESET_DEVICE = _USBIO_IOCTL_CODE(21, METHOD_BUFFERED);
29 static readonly int IOCTL_USBIO_BIND_PIPE = _USBIO_IOCTL_CODE(30, METHOD_BUFFERED); 29 static readonly int IOCTL_USBIO_BIND_PIPE = _USBIO_IOCTL_CODE(30, METHOD_BUFFERED);
30 static readonly int IOCTL_USBIO_RESET_PIPE = _USBIO_IOCTL_CODE(32, METHOD_BUFFERED);
30 31
31 [DllImport("kernel32.dll", SetLastError = true)] 32 [DllImport("kernel32.dll", SetLastError = true)]
32 static unsafe extern bool ReadFile(SafeFileHandle hFile, byte* lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped); 33 static unsafe extern bool ReadFile(SafeFileHandle hFile, byte* lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);
33 [DllImport("kernel32.dll", SetLastError = true)] 34 [DllImport("kernel32.dll", SetLastError = true)]
34 static unsafe extern bool WriteFile(SafeFileHandle hFile, byte* lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, IntPtr lpOverlapped); 35 static unsafe extern bool WriteFile(SafeFileHandle hFile, byte* lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, IntPtr lpOverlapped);
153 return PipeRead(endpoint, buffer, offset, length); 154 return PipeRead(endpoint, buffer, offset, length);
154 } 155 }
155 public override int BulkWrite(byte endpoint, byte[] buffer, int offset, int length) { 156 public override int BulkWrite(byte endpoint, byte[] buffer, int offset, int length) {
156 return PipeWrite(endpoint, buffer, offset, length); 157 return PipeWrite(endpoint, buffer, offset, length);
157 } 158 }
159 public override void BulkReset(byte endpoint) {
160 PipeReset(endpoint);
161 }
158 public override int InterruptRead(byte endpoint, byte[] buffer, int offset, int length) { 162 public override int InterruptRead(byte endpoint, byte[] buffer, int offset, int length) {
159 return PipeRead(endpoint, buffer, offset, length); 163 return PipeRead(endpoint, buffer, offset, length);
160 } 164 }
161 public override int InterruptWrite(byte endpoint, byte[] buffer, int offset, int length) { 165 public override int InterruptWrite(byte endpoint, byte[] buffer, int offset, int length) {
162 return PipeWrite(endpoint, buffer, offset, length); 166 return PipeWrite(endpoint, buffer, offset, length);
167 }
168 public override void InterruptReset(byte endpoint) {
169 PipeReset(endpoint);
163 } 170 }
164 public unsafe override int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) { 171 public unsafe override int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) {
165 return ControlTransfer(requestType, request, value, index, buffer, offset, length); 172 return ControlTransfer(requestType, request, value, index, buffer, offset, length);
166 } 173 }
167 public override int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) { 174 public override int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) {
255 if (!WriteFile(handle, b + offset, (uint)length, out ret, IntPtr.Zero)) throw new Win32Exception(Marshal.GetLastWin32Error()); 262 if (!WriteFile(handle, b + offset, (uint)length, out ret, IntPtr.Zero)) throw new Win32Exception(Marshal.GetLastWin32Error());
256 } 263 }
257 return (int)ret; 264 return (int)ret;
258 } 265 }
259 public void PipeReset(byte pipeID) { 266 public void PipeReset(byte pipeID) {
260 throw new NotImplementedException(); 267 SafeFileHandle handle = GetHandleForPipe(pipeID);
268 DeviceIoControl(handle, IOCTL_USBIO_RESET_PIPE, IntPtr.Zero, 0, IntPtr.Zero, 0);
261 } 269 }
262 270
263 private unsafe int DeviceIoControl(SafeHandle hDevice, int IoControlCode, IntPtr InBuffer, int nInBufferSize, IntPtr OutBuffer, int nOutBufferSize) { 271 private unsafe int DeviceIoControl(SafeHandle hDevice, int IoControlCode, IntPtr InBuffer, int nInBufferSize, IntPtr OutBuffer, int nOutBufferSize) {
264 int pBytesReturned; 272 int pBytesReturned;
265 if (Kernel32.DeviceIoControl(hDevice, IoControlCode, InBuffer, nInBufferSize, OutBuffer, nOutBufferSize, out pBytesReturned, null)) 273 if (Kernel32.DeviceIoControl(hDevice, IoControlCode, InBuffer, nInBufferSize, OutBuffer, nOutBufferSize, out pBytesReturned, null))