Mercurial > hg > ucis.core
comparison USBLib/Communication/WinUsb/WinUsbDevice.cs @ 67:2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Wed, 16 Oct 2013 01:11:49 +0200 |
parents | abe0d55a2201 |
children | e811297f5aa4 |
comparison
equal
deleted
inserted
replaced
66:b7bc27c6734e | 67:2d16447eff12 |
---|---|
143 if (value == base.Configuration) return; | 143 if (value == base.Configuration) return; |
144 throw new NotSupportedException(); | 144 throw new NotSupportedException(); |
145 } | 145 } |
146 } | 146 } |
147 | 147 |
148 public unsafe int ControlTransfer(byte requestType, byte request, short value, short index, byte[] buffer, int offset, int length) { | 148 public override unsafe int ControlTransfer(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) { |
149 if (buffer == null) buffer = new Byte[0]; | 149 if (buffer == null) buffer = new Byte[0]; |
150 if (offset < 0 || length < 0 || length > short.MaxValue || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length"); | 150 if (offset < 0 || length < 0 || length > short.MaxValue || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length"); |
151 SafeWinUsbInterfaceHandle ih = InterfaceHandles[0]; | 151 SafeWinUsbInterfaceHandle ih = InterfaceHandles[0]; |
152 switch ((UsbControlRequestType)requestType & UsbControlRequestType.RecipMask) { | 152 switch ((UsbControlRequestType)requestType & UsbControlRequestType.RecipMask) { |
153 case UsbControlRequestType.RecipInterface: ih = GetInterfaceHandle(index & 0xff); break; | 153 case UsbControlRequestType.RecipInterface: ih = GetInterfaceHandle(index & 0xff); break; |
154 case UsbControlRequestType.RecipEndpoint: ih = GetInterfaceHandleForEndpoint(index & 0xff); break; | 154 case UsbControlRequestType.RecipEndpoint: ih = GetInterfaceHandleForEndpoint(index & 0xff); break; |
155 case UsbControlRequestType.RecipOther: break; | 155 case UsbControlRequestType.RecipOther: break; |
156 } | 156 } |
157 fixed (Byte* b = buffer) { | 157 fixed (Byte* b = buffer) { |
158 if (!WinUsb_ControlTransfer(ih, | 158 if (!WinUsb_ControlTransfer(ih, |
159 new UsbSetupPacket(requestType, request, value, index, (short)length), | 159 new UsbSetupPacket((byte)requestType, request, value, index, (short)length), |
160 (IntPtr)(b + offset), length, out length, IntPtr.Zero)) | 160 (IntPtr)(b + offset), length, out length, IntPtr.Zero)) |
161 throw new Win32Exception(Marshal.GetLastWin32Error(), "Control transfer failed"); | 161 throw new Win32Exception(Marshal.GetLastWin32Error(), "Control transfer failed"); |
162 return length; | 162 return length; |
163 } | 163 } |
164 } | |
165 | |
166 public override int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) { | |
167 return ControlTransfer((byte)(requestType | UsbControlRequestType.EndpointOut), request, value, index, buffer, offset, length); | |
168 } | |
169 | |
170 public override int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) { | |
171 return ControlTransfer((byte)(requestType | UsbControlRequestType.EndpointIn), request, value, index, buffer, offset, length); | |
172 } | 164 } |
173 | 165 |
174 public unsafe override int GetDescriptor(byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) { | 166 public unsafe override int GetDescriptor(byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) { |
175 if (length > short.MaxValue || offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length"); | 167 if (length > short.MaxValue || offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length"); |
176 fixed (Byte* b = buffer) { | 168 fixed (Byte* b = buffer) { |
178 throw new Win32Exception(Marshal.GetLastWin32Error(), "Descriptor transfer failed"); | 170 throw new Win32Exception(Marshal.GetLastWin32Error(), "Descriptor transfer failed"); |
179 } | 171 } |
180 return length; | 172 return length; |
181 } | 173 } |
182 | 174 |
183 public unsafe int PipeWrite(byte endpoint, byte[] buffer, int offset, int length) { | 175 public override unsafe int PipeTransfer(byte endpoint, byte[] buffer, int offset, int length) { |
184 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer length"); | 176 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer length"); |
185 SafeWinUsbInterfaceHandle ih = GetInterfaceHandleForEndpoint(endpoint); | 177 SafeWinUsbInterfaceHandle ih = GetInterfaceHandleForEndpoint(endpoint); |
186 fixed (Byte* b = buffer) { | 178 fixed (Byte* b = buffer) { |
187 if (!WinUsb_WritePipe(ih, endpoint, (IntPtr)(b + offset), length, out length, IntPtr.Zero)) | 179 Boolean success; |
188 throw new Win32Exception(Marshal.GetLastWin32Error()); | 180 if ((endpoint & (Byte)UsbControlRequestType.EndpointMask) == (Byte)UsbControlRequestType.EndpointOut) |
181 success = WinUsb_WritePipe(ih, endpoint, (IntPtr)(b + offset), length, out length, IntPtr.Zero); | |
182 else | |
183 success = WinUsb_ReadPipe(ih, endpoint, (IntPtr)(b + offset), length, out length, IntPtr.Zero); | |
184 if (!success) throw new Win32Exception(Marshal.GetLastWin32Error()); | |
189 } | 185 } |
190 return length; | 186 return length; |
191 } | 187 } |
192 | 188 |
193 public unsafe int PipeRead(byte endpoint, byte[] buffer, int offset, int length) { | 189 public override void PipeReset(byte endpoint) { |
194 if (offset < 0 || length < 0 || offset + length > buffer.Length) throw new ArgumentOutOfRangeException("length", "The specified offset and length exceed the buffer length"); | |
195 SafeWinUsbInterfaceHandle ih = GetInterfaceHandleForEndpoint(endpoint); | |
196 fixed (Byte* b = buffer) { | |
197 if (!WinUsb_ReadPipe(ih, endpoint, (IntPtr)(b + offset), length, out length, IntPtr.Zero)) | |
198 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
199 } | |
200 return length; | |
201 } | |
202 | |
203 public void PipeReset(byte endpoint) { | |
204 SafeWinUsbInterfaceHandle ih = GetInterfaceHandleForEndpoint(endpoint); | 190 SafeWinUsbInterfaceHandle ih = GetInterfaceHandleForEndpoint(endpoint); |
205 WinUsb_ResetPipe(ih, endpoint); | 191 WinUsb_ResetPipe(ih, endpoint); |
206 } | 192 } |
207 | 193 public override void PipeAbort(byte endpoint) { |
208 public override int BulkWrite(Byte endpoint, Byte[] buffer, int offset, int length) { | 194 SafeWinUsbInterfaceHandle ih = GetInterfaceHandleForEndpoint(endpoint); |
209 return PipeWrite(endpoint, buffer, offset, length); | 195 WinUsb_AbortPipe(ih, endpoint); |
210 } | |
211 public override int BulkRead(Byte endpoint, Byte[] buffer, int offset, int length) { | |
212 return PipeRead(endpoint, buffer, offset, length); | |
213 } | |
214 public override void BulkReset(Byte endpoint) { | |
215 PipeReset(endpoint); | |
216 } | |
217 public override int InterruptWrite(Byte endpoint, Byte[] buffer, int offset, int length) { | |
218 return PipeWrite(endpoint, buffer, offset, length); | |
219 } | |
220 public override int InterruptRead(Byte endpoint, Byte[] buffer, int offset, int length) { | |
221 return PipeRead(endpoint, buffer, offset, length); | |
222 } | |
223 public override void InterruptReset(Byte endpoint) { | |
224 PipeReset(endpoint); | |
225 } | 196 } |
226 } | 197 } |
227 } | 198 } |