コード例 #1
0
        public override bool ReadPipe(UsbEndpointBase endPointBase,
                                      IntPtr buffer,
                                      int bufferLength,
                                      out int lengthTransferred,
                                      int isoPacketSize,
                                      IntPtr pOverlapped)
        {
            LibUsbRequest req = new LibUsbRequest();

            req.Endpoint.ID         = endPointBase.EpNum;
            req.Endpoint.PacketSize = isoPacketSize;
            req.Timeout             = UsbConstants.DEFAULT_TIMEOUT;

            int cltCode = endPointBase.Type == EndpointType.Isochronous ? LibUsbIoCtl.ISOCHRONOUS_READ : LibUsbIoCtl.INTERRUPT_OR_BULK_READ;


            return(Kernel32.DeviceIoControl(endPointBase.Device.Handle,
                                            cltCode,
                                            req,
                                            LibUsbRequest.Size,
                                            buffer,
                                            bufferLength,
                                            out lengthTransferred,
                                            pOverlapped));
        }
コード例 #2
0
        public bool ResetDevice(SafeHandle interfaceHandle)
        {
            LibUsbRequest req = new LibUsbRequest();

            int ret;

            req.Timeout = UsbConstants.DEFAULT_TIMEOUT;
            return(LibUsbDriverIO.UsbIOSync(interfaceHandle, LibUsbIoCtl.RESET_DEVICE, req, LibUsbRequest.Size, IntPtr.Zero, 0, out ret));
        }
コード例 #3
0
        public override bool AbortPipe(SafeHandle interfaceHandle, byte pipeID)
        {
            LibUsbRequest req = new LibUsbRequest();

            int ret;

            req.Endpoint.ID = pipeID;
            req.Timeout     = UsbConstants.DEFAULT_TIMEOUT;
            return(LibUsbDriverIO.UsbIOSync(interfaceHandle, LibUsbIoCtl.ABORT_ENDPOINT, req, LibUsbRequest.Size, IntPtr.Zero, 0, out ret));
        }
コード例 #4
0
        internal static bool ControlTransferEx(SafeHandle interfaceHandle,
                                               UsbSetupPacket setupPacket,
                                               IntPtr buffer,
                                               int bufferLength,
                                               out int lengthTransferred,
                                               int timeout)
        {
            lengthTransferred = 0;
            LibUsbRequest req = new LibUsbRequest();

            req.Timeout             = timeout;
            req.Control.RequestType = (byte)setupPacket.RequestType;
            req.Control.Request     = (byte)setupPacket.Request;
            req.Control.Value       = (ushort)setupPacket.Value;
            req.Control.Index       = (ushort)setupPacket.Index;
            req.Control.Length      = (ushort)setupPacket.Length;

            /* in request? */
            Byte[] reqBytes = req.Bytes;
            Byte[] inBytes  = reqBytes;
            if ((setupPacket.RequestType & (byte)UsbEndpointDirection.EndpointIn) == 0)
            {
                inBytes = new byte[LibUsbRequest.Size + bufferLength];
                reqBytes.CopyTo(inBytes, 0);
                if (buffer != IntPtr.Zero)
                {
                    Marshal.Copy(buffer, inBytes, LibUsbRequest.Size, bufferLength);
                }

                buffer       = IntPtr.Zero;
                bufferLength = 0;
            }

            if (UsbIOSync(interfaceHandle, LibUsbIoCtl.CONTROL_TRANSFER, inBytes, inBytes.Length, buffer, bufferLength, out lengthTransferred))
            {
                /* in request? */
                if ((setupPacket.RequestType & (byte)UsbEndpointDirection.EndpointIn) == 0)
                {
                    lengthTransferred = (inBytes.Length - reqBytes.Length);
                }

                return(true);
            }
            return(false);
        }
コード例 #5
0
        public override bool GetDescriptor(SafeHandle interfaceHandle,
                                           byte descriptorType,
                                           byte index,
                                           ushort languageID,
                                           IntPtr buffer,
                                           int bufferLength,
                                           out int lengthTransferred)
        {
            LibUsbRequest req = new LibUsbRequest();

            req.Descriptor.Index     = index;
            req.Descriptor.LangID    = languageID;
            req.Descriptor.Recipient = (byte)UsbEndpointDirection.EndpointIn & 0x1F;
            req.Descriptor.Type      = descriptorType;
            return(LibUsbDriverIO.UsbIOSync(interfaceHandle,
                                            LibUsbIoCtl.GET_DESCRIPTOR,
                                            req,
                                            LibUsbRequest.Size,
                                            buffer,
                                            bufferLength,
                                            out lengthTransferred));
        }
コード例 #6
0
        internal static bool ControlTransfer(SafeHandle interfaceHandle,
                                             UsbSetupPacket setupPacket,
                                             IntPtr buffer,
                                             int bufferLength,
                                             out int lengthTransferred,
                                             int timeout)
        {
            lengthTransferred = 0;
            LibUsbRequest req = new LibUsbRequest();
            int           code;

            req.Timeout = timeout;

            switch ((UsbRequestType)(setupPacket.RequestType & (0x03 << 5)))
            {
            case UsbRequestType.TypeStandard:
                switch ((UsbStandardRequest)setupPacket.Request)
                {
                case UsbStandardRequest.GetStatus:
                    req.Status.Recipient = (int)setupPacket.RequestType & 0x1F;
                    req.Status.Index     = setupPacket.Index;
                    code = LibUsbIoCtl.GET_STATUS;
                    break;

                case UsbStandardRequest.ClearFeature:
                    req.Feature.Recipient = (int)setupPacket.RequestType & 0x1F;
                    req.Feature.ID        = setupPacket.Value;
                    req.Feature.Index     = setupPacket.Index;
                    code = LibUsbIoCtl.CLEAR_FEATURE;
                    break;

                case UsbStandardRequest.SetFeature:
                    req.Feature.Recipient = (int)setupPacket.RequestType & 0x1F;
                    req.Feature.ID        = setupPacket.Value;
                    req.Feature.Index     = setupPacket.Index;
                    code = LibUsbIoCtl.SET_FEATURE;
                    break;

                case UsbStandardRequest.GetDescriptor:
                    req.Descriptor.Recipient = (int)setupPacket.RequestType & 0x1F;
                    req.Descriptor.Type      = (setupPacket.Value >> 8) & 0xFF;
                    req.Descriptor.Index     = setupPacket.Value & 0xFF;
                    req.Descriptor.LangID    = setupPacket.Index;
                    code = LibUsbIoCtl.GET_DESCRIPTOR;
                    break;

                case UsbStandardRequest.SetDescriptor:
                    req.Descriptor.Recipient = (int)setupPacket.RequestType & 0x1F;
                    req.Descriptor.Type      = (setupPacket.Value >> 8) & 0xFF;
                    req.Descriptor.Index     = setupPacket.Value & 0xFF;
                    req.Descriptor.LangID    = setupPacket.Index;
                    code = LibUsbIoCtl.SET_DESCRIPTOR;
                    break;

                case UsbStandardRequest.GetConfiguration:
                    code = LibUsbIoCtl.GET_CONFIGURATION;
                    break;

                case UsbStandardRequest.SetConfiguration:
                    req.Config.ID = setupPacket.Value;
                    code          = LibUsbIoCtl.SET_CONFIGURATION;
                    break;

                case UsbStandardRequest.GetInterface:
                    req.Iface.ID = setupPacket.Index;
                    code         = LibUsbIoCtl.GET_INTERFACE;
                    break;

                case UsbStandardRequest.SetInterface:
                    req.Iface.ID          = setupPacket.Index;
                    req.Iface.AlternateID = setupPacket.Value;
                    code = LibUsbIoCtl.SET_INTERFACE;
                    break;

                default:
                    UsbError.Error(ErrorCode.IoControlMessage, 0,
                                   String.Format("Invalid request: 0x{0:X8}", setupPacket.Request),
                                   typeof(LibUsbDriverIO));
                    return(false);
                }
                break;

            case UsbRequestType.TypeVendor:
            case UsbRequestType.TypeClass:

                req.Vendor.Type      = ((byte)setupPacket.RequestType >> 5) & 0x03;
                req.Vendor.Recipient = (int)setupPacket.RequestType & 0x1F;
                req.Vendor.Request   = (int)setupPacket.Request;
                req.Vendor.ID        = setupPacket.Value;
                req.Vendor.Index     = setupPacket.Index;

                code = ((byte)setupPacket.RequestType & 0x80) > 0 ? LibUsbIoCtl.VENDOR_READ : LibUsbIoCtl.VENDOR_WRITE;
                break;

            case UsbRequestType.TypeReserved:
            default:
                UsbError.Error(ErrorCode.IoControlMessage, 0,
                               String.Format("invalid or unsupported request type: 0x{0:X8}", setupPacket.RequestType),
                               typeof(LibUsbDriverIO));
                return(false);
            }

            /* in request? */
            Byte[] reqBytes = req.Bytes;
            Byte[] inBytes  = reqBytes;
            if ((setupPacket.RequestType & (byte)UsbEndpointDirection.EndpointIn) == 0)
            {
                inBytes = new byte[LibUsbRequest.Size + bufferLength];
                reqBytes.CopyTo(inBytes, 0);
                if (buffer != IntPtr.Zero)
                {
                    Marshal.Copy(buffer, inBytes, LibUsbRequest.Size, bufferLength);
                }

                buffer       = IntPtr.Zero;
                bufferLength = 0;
            }

            if (UsbIOSync(interfaceHandle, code, inBytes, inBytes.Length, buffer, bufferLength, out lengthTransferred))
            {
                /* in request? */
                if ((setupPacket.RequestType & (byte)UsbEndpointDirection.EndpointIn) == 0)
                {
                    lengthTransferred = (inBytes.Length - reqBytes.Length);
                }

                return(true);
            }
            return(false);
        }