UsbEndpointDescriptor.java revision b5eaa809da69865cbde156007ae5363f9209f932
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.usb.descriptors;
17
18/**
19 * @hide
20 * A Usb Endpoint Descriptor.
21 * see usb11.pdf section 9.6.4
22 */
23public class UsbEndpointDescriptor extends UsbDescriptor {
24    private static final String TAG = "EndPoint";
25
26    public static final byte MASK_ENDPOINT_ADDRESS     = 0b0001111;
27    public static final byte MASK_ENDPOINT_DIRECTION   = (byte) 0b10000000;
28    public static final byte DIRECTION_OUTPUT          = 0x00;
29    public static final byte DIRECTION_INPUT           = (byte) 0x80;
30
31    public static final byte MASK_ATTRIBS_TRANSTYPE = 0b00000011;
32    public static final byte TRANSTYPE_CONTROL     = 0x00;
33    public static final byte TRANSTYPE_ISO         = 0x01;
34    public static final byte TRANSTYPE_BULK        = 0x02;
35    public static final byte TRANSTYPE_INTERRUPT   = 0x03;
36
37    public static final byte MASK_ATTRIBS_SYNCTYPE  = 0b00001100;
38    public static final byte SYNCTYPE_NONE          = 0b00000000;
39    public static final byte SYNCTYPE_ASYNC         = 0b00000100;
40    public static final byte SYNCTYPE_ADAPTSYNC     = 0b00001000;
41    public static final byte SYNCTYPE_RESERVED      = 0b00001100;
42
43    public static final byte MASK_ATTRIBS_USEAGE    = 0b00110000;
44    public static final byte USEAGE_DATA            = 0b00000000;
45    public static final byte USEAGE_FEEDBACK        = 0b00010000;
46    public static final byte USEAGE_EXPLICIT        = 0b00100000;
47    public static final byte USEAGE_RESERVED        = 0b00110000;
48
49    private byte mEndpointAddress;  // 2:1 Endpoint Address
50                                    // Bits 0..3b Endpoint Number.
51                                    // Bits 4..6b Reserved. Set to Zero
52                                    // Bits 7 Direction 0 = Out, 1 = In
53                                    // (Ignored for Control Endpoints)
54    private byte mAttributes;   // 3:1 Various flags
55                                // Bits 0..1 Transfer Type:
56                                //     00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt
57                                // Bits 2..7 are reserved. If Isochronous endpoint,
58                                // Bits 3..2 = Synchronisation Type (Iso Mode)
59                                //  00 = No Synchonisation
60                                //  01 = Asynchronous
61                                //  10 = Adaptive
62                                //  11 = Synchronous
63                                // Bits 5..4 = Usage Type (Iso Mode)
64                                //  00: Data Endpoint
65                                //  01:Feedback Endpoint 10
66                                //  Explicit Feedback Data Endpoint
67                                //  11: Reserved
68    private int mPacketSize;    // 4:2 Maximum Packet Size this endpoint is capable of
69                                // sending or receiving
70    private byte mInterval;     // 6:1 Interval for polling endpoint data transfers. Value in
71                                // frame counts.
72                                // Ignored for Bulk & Control Endpoints. Isochronous must equal
73                                // 1 and field may range from 1 to 255 for interrupt endpoints.
74    private byte mRefresh;
75    private byte mSyncAddress;
76
77    public UsbEndpointDescriptor(int length, byte type) {
78        super(length, type);
79    }
80
81    public byte getEndpointAddress() {
82        return mEndpointAddress;
83    }
84
85    public byte getAttributes() {
86        return mAttributes;
87    }
88
89    public int getPacketSize() {
90        return mPacketSize;
91    }
92
93    public byte getInterval() {
94        return mInterval;
95    }
96
97    public byte getRefresh() {
98        return mRefresh;
99    }
100
101    public byte getSyncAddress() {
102        return mSyncAddress;
103    }
104
105    @Override
106    public int parseRawDescriptors(ByteStream stream) {
107        mEndpointAddress = stream.getByte();
108        mAttributes = stream.getByte();
109        mPacketSize = stream.unpackUsbWord();
110        mInterval = stream.getByte();
111        if (mLength == 9) {
112            mRefresh = stream.getByte();
113            mSyncAddress = stream.getByte();
114        }
115        return mLength;
116    }
117}
118